| /*------------------------------------------------------------------------ |
| * Vulkan Conformance Tests |
| * ------------------------ |
| * |
| * Copyright (c) 2019 The Khronos Group Inc. |
| * Copyright (c) 2019 Google Inc. |
| * Copyright (c) 2017 Codeplay Software Ltd. |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| * |
| */ /*! |
| * \file |
| * \brief Subgroups Tests |
| */ /*--------------------------------------------------------------------*/ |
| |
| #include "vktSubgroupsArithmeticTests.hpp" |
| #include "vktSubgroupsScanHelpers.hpp" |
| #include "vktSubgroupsTestsUtils.hpp" |
| |
| #include <string> |
| #include <vector> |
| |
| using namespace tcu; |
| using namespace std; |
| using namespace vk; |
| using namespace vkt; |
| |
| namespace |
| { |
| enum OpType |
| { |
| OPTYPE_ADD = 0, |
| OPTYPE_MUL, |
| OPTYPE_MIN, |
| OPTYPE_MAX, |
| OPTYPE_AND, |
| OPTYPE_OR, |
| OPTYPE_XOR, |
| OPTYPE_INCLUSIVE_ADD, |
| OPTYPE_INCLUSIVE_MUL, |
| OPTYPE_INCLUSIVE_MIN, |
| OPTYPE_INCLUSIVE_MAX, |
| OPTYPE_INCLUSIVE_AND, |
| OPTYPE_INCLUSIVE_OR, |
| OPTYPE_INCLUSIVE_XOR, |
| OPTYPE_EXCLUSIVE_ADD, |
| OPTYPE_EXCLUSIVE_MUL, |
| OPTYPE_EXCLUSIVE_MIN, |
| OPTYPE_EXCLUSIVE_MAX, |
| OPTYPE_EXCLUSIVE_AND, |
| OPTYPE_EXCLUSIVE_OR, |
| OPTYPE_EXCLUSIVE_XOR, |
| OPTYPE_LAST |
| }; |
| |
| static Operator getOperator(OpType t) |
| { |
| switch (t) |
| { |
| case OPTYPE_ADD: |
| case OPTYPE_INCLUSIVE_ADD: |
| case OPTYPE_EXCLUSIVE_ADD: |
| return OPERATOR_ADD; |
| case OPTYPE_MUL: |
| case OPTYPE_INCLUSIVE_MUL: |
| case OPTYPE_EXCLUSIVE_MUL: |
| return OPERATOR_MUL; |
| case OPTYPE_MIN: |
| case OPTYPE_INCLUSIVE_MIN: |
| case OPTYPE_EXCLUSIVE_MIN: |
| return OPERATOR_MIN; |
| case OPTYPE_MAX: |
| case OPTYPE_INCLUSIVE_MAX: |
| case OPTYPE_EXCLUSIVE_MAX: |
| return OPERATOR_MAX; |
| case OPTYPE_AND: |
| case OPTYPE_INCLUSIVE_AND: |
| case OPTYPE_EXCLUSIVE_AND: |
| return OPERATOR_AND; |
| case OPTYPE_OR: |
| case OPTYPE_INCLUSIVE_OR: |
| case OPTYPE_EXCLUSIVE_OR: |
| return OPERATOR_OR; |
| case OPTYPE_XOR: |
| case OPTYPE_INCLUSIVE_XOR: |
| case OPTYPE_EXCLUSIVE_XOR: |
| return OPERATOR_XOR; |
| default: |
| DE_FATAL("Unsupported op type"); |
| return OPERATOR_ADD; |
| } |
| } |
| |
| static ScanType getScanType(OpType t) |
| { |
| switch (t) |
| { |
| case OPTYPE_ADD: |
| case OPTYPE_MUL: |
| case OPTYPE_MIN: |
| case OPTYPE_MAX: |
| case OPTYPE_AND: |
| case OPTYPE_OR: |
| case OPTYPE_XOR: |
| return SCAN_REDUCE; |
| case OPTYPE_INCLUSIVE_ADD: |
| case OPTYPE_INCLUSIVE_MUL: |
| case OPTYPE_INCLUSIVE_MIN: |
| case OPTYPE_INCLUSIVE_MAX: |
| case OPTYPE_INCLUSIVE_AND: |
| case OPTYPE_INCLUSIVE_OR: |
| case OPTYPE_INCLUSIVE_XOR: |
| return SCAN_INCLUSIVE; |
| case OPTYPE_EXCLUSIVE_ADD: |
| case OPTYPE_EXCLUSIVE_MUL: |
| case OPTYPE_EXCLUSIVE_MIN: |
| case OPTYPE_EXCLUSIVE_MAX: |
| case OPTYPE_EXCLUSIVE_AND: |
| case OPTYPE_EXCLUSIVE_OR: |
| case OPTYPE_EXCLUSIVE_XOR: |
| return SCAN_EXCLUSIVE; |
| default: |
| DE_FATAL("Unsupported op type"); |
| return SCAN_REDUCE; |
| } |
| } |
| |
| static bool checkVertexPipelineStages(const void* internalData, std::vector<const void*> datas, |
| deUint32 width, deUint32) |
| { |
| DE_UNREF(internalData); |
| return vkt::subgroups::check(datas, width, 0x3); |
| } |
| |
| static bool checkCompute(const void* internalData, std::vector<const void*> datas, |
| const deUint32 numWorkgroups[3], const deUint32 localSize[3], |
| deUint32) |
| { |
| DE_UNREF(internalData); |
| return vkt::subgroups::checkCompute(datas, numWorkgroups, localSize, 0x3); |
| } |
| |
| std::string getOpTypeName(Operator op, ScanType scanType) |
| { |
| return getScanOpName("subgroup", "", op, scanType); |
| } |
| |
| struct CaseDefinition |
| { |
| Operator op; |
| ScanType scanType; |
| VkShaderStageFlags shaderStage; |
| VkFormat format; |
| de::SharedPtr<bool> geometryPointSizeSupported; |
| deBool requiredSubgroupSize; |
| }; |
| |
| std::string getExtHeader(CaseDefinition caseDef) |
| { |
| return "#extension GL_KHR_shader_subgroup_arithmetic: enable\n" |
| "#extension GL_KHR_shader_subgroup_ballot: enable\n" + |
| subgroups::getAdditionalExtensionForFormat(caseDef.format); |
| } |
| |
| std::string getIndexVars(CaseDefinition caseDef) |
| { |
| switch (caseDef.scanType) |
| { |
| case SCAN_REDUCE: |
| return " uint start = 0, end = gl_SubgroupSize;\n"; |
| case SCAN_INCLUSIVE: |
| return " uint start = 0, end = gl_SubgroupInvocationID + 1;\n"; |
| case SCAN_EXCLUSIVE: |
| return " uint start = 0, end = gl_SubgroupInvocationID;\n"; |
| } |
| DE_FATAL("Unreachable"); |
| return ""; |
| } |
| |
| std::string getTestSrc(CaseDefinition caseDef) |
| { |
| std::string indexVars = getIndexVars(caseDef); |
| |
| return " uvec4 mask = subgroupBallot(true);\n" |
| + indexVars + |
| " " + subgroups::getFormatNameForGLSL(caseDef.format) + " ref = " |
| + getIdentity(caseDef.op, caseDef.format) + ";\n" |
| " tempRes = 0;\n" |
| " for (uint index = start; index < end; index++)\n" |
| " {\n" |
| " if (subgroupBallotBitExtract(mask, index))\n" |
| " {\n" |
| " ref = " + getOpOperation(caseDef.op, caseDef.format, "ref", "data[index]") + ";\n" |
| " }\n" |
| " }\n" |
| " tempRes = " + getCompare(caseDef.op, caseDef.format, "ref", getOpTypeName(caseDef.op, caseDef.scanType) + "(data[gl_SubgroupInvocationID])") + " ? 0x1 : 0;\n" |
| " if (1 == (gl_SubgroupInvocationID % 2))\n" |
| " {\n" |
| " mask = subgroupBallot(true);\n" |
| " ref = " + getIdentity(caseDef.op, caseDef.format) + ";\n" |
| " for (uint index = start; index < end; index++)\n" |
| " {\n" |
| " if (subgroupBallotBitExtract(mask, index))\n" |
| " {\n" |
| " ref = " + getOpOperation(caseDef.op, caseDef.format, "ref", "data[index]") + ";\n" |
| " }\n" |
| " }\n" |
| " tempRes |= " + getCompare(caseDef.op, caseDef.format, "ref", getOpTypeName(caseDef.op, caseDef.scanType) + "(data[gl_SubgroupInvocationID])") + " ? 0x2 : 0;\n" |
| " }\n" |
| " else\n" |
| " {\n" |
| " tempRes |= 0x2;\n" |
| " }\n"; |
| } |
| |
| void initFrameBufferPrograms(SourceCollections& programCollection, CaseDefinition caseDef) |
| { |
| const vk::ShaderBuildOptions buildOptions (programCollection.usedVulkanVersion, vk::SPIRV_VERSION_1_3, 0u); |
| |
| std::string extHeader = getExtHeader(caseDef); |
| std::string testSrc = getTestSrc(caseDef); |
| |
| subgroups::initStdFrameBufferPrograms(programCollection, buildOptions, caseDef.shaderStage, caseDef.format, *caseDef.geometryPointSizeSupported, extHeader, testSrc, ""); |
| } |
| |
| void initPrograms(SourceCollections& programCollection, CaseDefinition caseDef) |
| { |
| const vk::ShaderBuildOptions buildOptions (programCollection.usedVulkanVersion, vk::SPIRV_VERSION_1_3, 0u); |
| |
| std::string extHeader = getExtHeader(caseDef); |
| std::string testSrc = getTestSrc(caseDef); |
| |
| subgroups::initStdPrograms(programCollection, buildOptions, caseDef.shaderStage, caseDef.format, extHeader, testSrc, ""); |
| } |
| |
| void supportedCheck (Context& context, CaseDefinition caseDef) |
| { |
| if (!subgroups::isSubgroupSupported(context)) |
| TCU_THROW(NotSupportedError, "Subgroup operations are not supported"); |
| |
| if (!subgroups::isSubgroupFeatureSupportedForDevice(context, VK_SUBGROUP_FEATURE_ARITHMETIC_BIT)) |
| TCU_THROW(NotSupportedError, "Device does not support subgroup arithmetic operations"); |
| |
| if (!subgroups::isFormatSupportedForDevice(context, caseDef.format)) |
| TCU_THROW(NotSupportedError, "Device does not support the specified format in subgroup operations"); |
| |
| if (caseDef.requiredSubgroupSize) |
| { |
| if (!context.requireDeviceFunctionality("VK_EXT_subgroup_size_control")) |
| TCU_THROW(NotSupportedError, "Device does not support VK_EXT_subgroup_size_control extension"); |
| VkPhysicalDeviceSubgroupSizeControlFeaturesEXT subgroupSizeControlFeatures; |
| subgroupSizeControlFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_FEATURES_EXT; |
| subgroupSizeControlFeatures.pNext = DE_NULL; |
| |
| VkPhysicalDeviceFeatures2 features; |
| features.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2; |
| features.pNext = &subgroupSizeControlFeatures; |
| |
| context.getInstanceInterface().getPhysicalDeviceFeatures2(context.getPhysicalDevice(), &features); |
| |
| if (subgroupSizeControlFeatures.subgroupSizeControl == DE_FALSE) |
| TCU_THROW(NotSupportedError, "Device does not support varying subgroup sizes nor required subgroup size"); |
| |
| if (subgroupSizeControlFeatures.computeFullSubgroups == DE_FALSE) |
| TCU_THROW(NotSupportedError, "Device does not support full subgroups in compute shaders"); |
| } |
| |
| *caseDef.geometryPointSizeSupported = subgroups::isTessellationAndGeometryPointSizeSupported(context); |
| } |
| |
| tcu::TestStatus noSSBOtest (Context& context, const CaseDefinition caseDef) |
| { |
| if (!subgroups::areSubgroupOperationsSupportedForStage(context, caseDef.shaderStage)) |
| { |
| if (subgroups::areSubgroupOperationsRequiredForStage(caseDef.shaderStage)) |
| { |
| return tcu::TestStatus::fail( |
| "Shader stage " + |
| subgroups::getShaderStageName(caseDef.shaderStage) + |
| " is required to support subgroup operations!"); |
| } |
| else |
| { |
| TCU_THROW(NotSupportedError, "Device does not support subgroup operations for this stage"); |
| } |
| } |
| |
| subgroups::SSBOData inputData; |
| inputData.format = caseDef.format; |
| inputData.layout = subgroups::SSBOData::LayoutStd140; |
| inputData.numElements = subgroups::maxSupportedSubgroupSize(); |
| inputData.initializeType = subgroups::SSBOData::InitializeNonZero; |
| |
| if (VK_SHADER_STAGE_VERTEX_BIT == caseDef.shaderStage) |
| return subgroups::makeVertexFrameBufferTest(context, VK_FORMAT_R32_UINT, &inputData, 1, DE_NULL, checkVertexPipelineStages); |
| else if (VK_SHADER_STAGE_GEOMETRY_BIT == caseDef.shaderStage) |
| return subgroups::makeGeometryFrameBufferTest(context, VK_FORMAT_R32_UINT, &inputData, 1, DE_NULL, checkVertexPipelineStages); |
| else if (VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT == caseDef.shaderStage) |
| return subgroups::makeTessellationEvaluationFrameBufferTest(context, VK_FORMAT_R32_UINT, &inputData, 1, DE_NULL, checkVertexPipelineStages, VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT); |
| else if (VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT == caseDef.shaderStage) |
| return subgroups::makeTessellationEvaluationFrameBufferTest(context, VK_FORMAT_R32_UINT, &inputData, 1, DE_NULL, checkVertexPipelineStages, VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT); |
| else |
| TCU_THROW(InternalError, "Unhandled shader stage"); |
| } |
| |
| |
| tcu::TestStatus test(Context& context, const CaseDefinition caseDef) |
| { |
| if (VK_SHADER_STAGE_COMPUTE_BIT == caseDef.shaderStage) |
| { |
| if (!subgroups::areSubgroupOperationsSupportedForStage(context, caseDef.shaderStage)) |
| { |
| return tcu::TestStatus::fail( |
| "Shader stage " + |
| subgroups::getShaderStageName(caseDef.shaderStage) + |
| " is required to support subgroup operations!"); |
| } |
| subgroups::SSBOData inputData; |
| inputData.format = caseDef.format; |
| inputData.layout = subgroups::SSBOData::LayoutStd430; |
| inputData.numElements = subgroups::maxSupportedSubgroupSize(); |
| inputData.initializeType = subgroups::SSBOData::InitializeNonZero; |
| |
| if (caseDef.requiredSubgroupSize == DE_FALSE) |
| return subgroups::makeComputeTest(context, VK_FORMAT_R32_UINT, &inputData, 1, DE_NULL, checkCompute); |
| |
| tcu::TestLog& log = context.getTestContext().getLog(); |
| VkPhysicalDeviceSubgroupSizeControlPropertiesEXT subgroupSizeControlProperties; |
| subgroupSizeControlProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_SIZE_CONTROL_PROPERTIES_EXT; |
| subgroupSizeControlProperties.pNext = DE_NULL; |
| VkPhysicalDeviceProperties2 properties; |
| properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2; |
| properties.pNext = &subgroupSizeControlProperties; |
| |
| context.getInstanceInterface().getPhysicalDeviceProperties2(context.getPhysicalDevice(), &properties); |
| |
| log << tcu::TestLog::Message << "Testing required subgroup size range [" << subgroupSizeControlProperties.minSubgroupSize << ", " |
| << subgroupSizeControlProperties.maxSubgroupSize << "]" << tcu::TestLog::EndMessage; |
| |
| // According to the spec, requiredSubgroupSize must be a power-of-two integer. |
| for (deUint32 size = subgroupSizeControlProperties.minSubgroupSize; size <= subgroupSizeControlProperties.maxSubgroupSize; size *= 2) |
| { |
| tcu::TestStatus result = subgroups::makeComputeTest(context, VK_FORMAT_R32_UINT, &inputData, 1, DE_NULL, checkCompute, |
| size, VK_PIPELINE_SHADER_STAGE_CREATE_REQUIRE_FULL_SUBGROUPS_BIT_EXT); |
| if (result.getCode() != QP_TEST_RESULT_PASS) |
| { |
| log << tcu::TestLog::Message << "subgroupSize " << size << " failed" << tcu::TestLog::EndMessage; |
| return result; |
| } |
| } |
| |
| return tcu::TestStatus::pass("OK"); |
| } |
| else |
| { |
| VkPhysicalDeviceSubgroupProperties subgroupProperties; |
| subgroupProperties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SUBGROUP_PROPERTIES; |
| subgroupProperties.pNext = DE_NULL; |
| |
| VkPhysicalDeviceProperties2 properties; |
| properties.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2; |
| properties.pNext = &subgroupProperties; |
| |
| context.getInstanceInterface().getPhysicalDeviceProperties2(context.getPhysicalDevice(), &properties); |
| |
| VkShaderStageFlagBits stages = (VkShaderStageFlagBits)(caseDef.shaderStage & subgroupProperties.supportedStages); |
| |
| if (VK_SHADER_STAGE_FRAGMENT_BIT != stages && !subgroups::isVertexSSBOSupportedForDevice(context)) |
| { |
| if ( (stages & VK_SHADER_STAGE_FRAGMENT_BIT) == 0) |
| TCU_THROW(NotSupportedError, "Device does not support vertex stage SSBO writes"); |
| else |
| stages = VK_SHADER_STAGE_FRAGMENT_BIT; |
| } |
| |
| if ((VkShaderStageFlagBits)0u == stages) |
| TCU_THROW(NotSupportedError, "Subgroup operations are not supported for any graphic shader"); |
| |
| subgroups::SSBOData inputData; |
| inputData.format = caseDef.format; |
| inputData.layout = subgroups::SSBOData::LayoutStd430; |
| inputData.numElements = subgroups::maxSupportedSubgroupSize(); |
| inputData.initializeType = subgroups::SSBOData::InitializeNonZero; |
| inputData.binding = 4u; |
| inputData.stages = stages; |
| |
| return subgroups::allStages(context, VK_FORMAT_R32_UINT, &inputData, |
| 1, DE_NULL, checkVertexPipelineStages, stages); |
| } |
| } |
| } |
| |
| namespace vkt |
| { |
| namespace subgroups |
| { |
| tcu::TestCaseGroup* createSubgroupsArithmeticTests(tcu::TestContext& testCtx) |
| { |
| de::MovePtr<tcu::TestCaseGroup> graphicGroup(new tcu::TestCaseGroup( |
| testCtx, "graphics", "Subgroup arithmetic category tests: graphics")); |
| de::MovePtr<tcu::TestCaseGroup> computeGroup(new tcu::TestCaseGroup( |
| testCtx, "compute", "Subgroup arithmetic category tests: compute")); |
| de::MovePtr<tcu::TestCaseGroup> framebufferGroup(new tcu::TestCaseGroup( |
| testCtx, "framebuffer", "Subgroup arithmetic category tests: framebuffer")); |
| |
| const VkShaderStageFlags stages[] = |
| { |
| VK_SHADER_STAGE_VERTEX_BIT, |
| VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT, |
| VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT, |
| VK_SHADER_STAGE_GEOMETRY_BIT, |
| }; |
| |
| const std::vector<VkFormat> formats = subgroups::getAllFormats(); |
| |
| for (size_t formatIndex = 0; formatIndex < formats.size(); ++formatIndex) |
| { |
| const VkFormat format = formats[formatIndex]; |
| |
| for (int opTypeIndex = 0; opTypeIndex < OPTYPE_LAST; ++opTypeIndex) |
| { |
| bool isBool = subgroups::isFormatBool(format); |
| bool isFloat = subgroups::isFormatFloat(format); |
| |
| OpType opType = static_cast<OpType>(opTypeIndex); |
| Operator op = getOperator(opType); |
| ScanType st = getScanType(opType); |
| |
| bool isBitwiseOp = (op == OPERATOR_AND || op == OPERATOR_OR || op == OPERATOR_XOR); |
| |
| // Skip float with bitwise category. |
| if (isFloat && isBitwiseOp) |
| continue; |
| |
| // Skip bool when its not the bitwise category. |
| if (isBool && !isBitwiseOp) |
| continue; |
| |
| const std::string name = de::toLower(getOpTypeName(op, st)) + "_" + subgroups::getFormatNameForGLSL(format); |
| |
| { |
| CaseDefinition caseDef = {op, st, VK_SHADER_STAGE_COMPUTE_BIT, format, de::SharedPtr<bool>(new bool), DE_FALSE}; |
| addFunctionCaseWithPrograms(computeGroup.get(), name, |
| "", supportedCheck, initPrograms, test, caseDef); |
| caseDef.requiredSubgroupSize = DE_TRUE; |
| addFunctionCaseWithPrograms(computeGroup.get(), name + "_requiredsubgroupsize", |
| "", supportedCheck, initPrograms, test, caseDef); |
| } |
| |
| { |
| const CaseDefinition caseDef = {op, st, VK_SHADER_STAGE_ALL_GRAPHICS, format, de::SharedPtr<bool>(new bool), DE_FALSE}; |
| addFunctionCaseWithPrograms(graphicGroup.get(), name, |
| "", supportedCheck, initPrograms, test, caseDef); |
| } |
| |
| for (int stageIndex = 0; stageIndex < DE_LENGTH_OF_ARRAY(stages); ++stageIndex) |
| { |
| const CaseDefinition caseDef = {op, st, stages[stageIndex], format, de::SharedPtr<bool>(new bool), DE_FALSE}; |
| addFunctionCaseWithPrograms(framebufferGroup.get(), name + |
| "_" + getShaderStageName(caseDef.shaderStage), "", |
| supportedCheck, initFrameBufferPrograms, noSSBOtest, caseDef); |
| } |
| } |
| } |
| |
| de::MovePtr<tcu::TestCaseGroup> group(new tcu::TestCaseGroup( |
| testCtx, "arithmetic", "Subgroup arithmetic category tests")); |
| |
| group->addChild(graphicGroup.release()); |
| group->addChild(computeGroup.release()); |
| group->addChild(framebufferGroup.release()); |
| |
| return group.release(); |
| } |
| } // subgroups |
| } // vkt |