1 /*------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
4 *
5 * Copyright (c) 2021 The Khronos Group Inc.
6 * Copyright (c) 2021 Valve Corporation.
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 *//*!
21 * \file
22 * \brief Tests using non-uniform arguments with traceRayExt().
23 *//*--------------------------------------------------------------------*/
24
25 #include "vktRayQueryNonUniformArgsTests.hpp"
26 #include "vktTestCase.hpp"
27
28 #include "vkRayTracingUtil.hpp"
29 #include "vkObjUtil.hpp"
30 #include "vkCmdUtil.hpp"
31 #include "vkBuilderUtil.hpp"
32 #include "vkTypeUtil.hpp"
33 #include "vkBarrierUtil.hpp"
34
35 #include "tcuTestLog.hpp"
36
37 #include <vector>
38 #include <iostream>
39
40 namespace vkt
41 {
42 namespace RayQuery
43 {
44 namespace
45 {
46
47 using namespace vk;
48
49 // Causes for hitting the miss shader due to argument values.
50 enum class MissCause
51 {
52 NONE = 0,
53 FLAGS,
54 CULL_MASK,
55 ORIGIN,
56 TMIN,
57 DIRECTION,
58 TMAX,
59 CAUSE_COUNT,
60 };
61
62 struct NonUniformParams
63 {
64 MissCause missCause;
65 };
66
67 class NonUniformArgsCase : public TestCase
68 {
69 public:
70 NonUniformArgsCase (tcu::TestContext& testCtx, const std::string& name, const std::string& description, const NonUniformParams& params);
~NonUniformArgsCase(void)71 virtual ~NonUniformArgsCase (void) {}
72
73 virtual void checkSupport (Context& context) const;
74 virtual void initPrograms (vk::SourceCollections& programCollection) const;
75 virtual TestInstance* createInstance (Context& context) const;
76
77 protected:
78 NonUniformParams m_params;
79 };
80
81 class NonUniformArgsInstance : public TestInstance
82 {
83 public:
84 NonUniformArgsInstance (Context& context, const NonUniformParams& params);
~NonUniformArgsInstance(void)85 virtual ~NonUniformArgsInstance (void) {}
86
87 virtual tcu::TestStatus iterate (void);
88
89 protected:
90 NonUniformParams m_params;
91 };
92
NonUniformArgsCase(tcu::TestContext & testCtx,const std::string & name,const std::string & description,const NonUniformParams & params)93 NonUniformArgsCase::NonUniformArgsCase (tcu::TestContext& testCtx, const std::string& name, const std::string& description, const NonUniformParams& params)
94 : TestCase (testCtx, name, description)
95 , m_params (params)
96 {}
97
checkSupport(Context & context) const98 void NonUniformArgsCase::checkSupport (Context& context) const
99 {
100 context.requireDeviceFunctionality("VK_KHR_acceleration_structure");
101 context.requireDeviceFunctionality("VK_KHR_ray_query");
102 }
103
104 struct ArgsBufferData
105 {
106 tcu::Vec4 origin;
107 tcu::Vec4 direction;
108 float Tmin;
109 float Tmax;
110 deUint32 rayFlags;
111 deUint32 cullMask;
112 };
113
initPrograms(vk::SourceCollections & programCollection) const114 void NonUniformArgsCase::initPrograms (vk::SourceCollections& programCollection) const
115 {
116 const ShaderBuildOptions buildOptions(programCollection.usedVulkanVersion, vk::SPIRV_VERSION_1_4, 0u, true);
117
118 std::ostringstream comp;
119 comp
120 << "#version 460 core\n"
121 << "#extension GL_EXT_ray_query : require\n"
122 << "\n"
123 << "layout(local_size_x=1, local_size_y=1, local_size_z=1) in;\n"
124 << "\n"
125 << "layout(set=0, binding=0) uniform accelerationStructureEXT topLevelAS;\n"
126 << "layout(set=0, binding=1, std430) buffer ArgumentsBlock {\n" // Must match ArgsBufferData.
127 << " vec4 origin;\n"
128 << " vec4 direction;\n"
129 << " float Tmin;\n"
130 << " float Tmax;\n"
131 << " uint rayFlags;\n"
132 << " uint cullMask;\n"
133 << "} args;\n"
134 << "layout(set=0, binding=2, std430) buffer ResultBlock {\n"
135 << " uint candidateFound;\n"
136 << "} result;\n"
137 << "\n"
138 << "void main()\n"
139 << "{\n"
140 << " uint candidateFoundVal = 0u;\n"
141 << " rayQueryEXT rq;\n"
142 << " rayQueryInitializeEXT(rq, topLevelAS, args.rayFlags, args.cullMask, args.origin.xyz, args.Tmin, args.direction.xyz, args.Tmax);\n"
143 << " while (rayQueryProceedEXT(rq)) {\n"
144 << " const uint candidateType = rayQueryGetIntersectionTypeEXT(rq, false);\n"
145 << " if (candidateType == gl_RayQueryCandidateIntersectionTriangleEXT) {\n"
146 << " candidateFoundVal = 1u;\n"
147 << " }\n"
148 << " else if (candidateType == gl_RayQueryCandidateIntersectionAABBEXT) {\n"
149 << " candidateFoundVal = 2u;\n"
150 << " break;\n"
151 << " }\n"
152 << " else {\n"
153 << " candidateFoundVal = 3u;\n"
154 << " break;\n"
155 << " }\n"
156 << " }\n"
157 << " result.candidateFound = candidateFoundVal;\n"
158 << "}\n";
159
160 programCollection.glslSources.add("comp") << glu::ComputeSource(comp.str()) << buildOptions;
161 }
162
createInstance(Context & context) const163 TestInstance* NonUniformArgsCase::createInstance (Context& context) const
164 {
165 return new NonUniformArgsInstance(context, m_params);
166 }
167
NonUniformArgsInstance(Context & context,const NonUniformParams & params)168 NonUniformArgsInstance::NonUniformArgsInstance (Context& context, const NonUniformParams& params)
169 : TestInstance (context)
170 , m_params (params)
171 {}
172
iterate(void)173 tcu::TestStatus NonUniformArgsInstance::iterate (void)
174 {
175 const auto& vkd = m_context.getDeviceInterface();
176 const auto device = m_context.getDevice();
177 auto& alloc = m_context.getDefaultAllocator();
178 const auto qIndex = m_context.getUniversalQueueFamilyIndex();
179 const auto queue = m_context.getUniversalQueue();
180 const auto stages = VK_SHADER_STAGE_COMPUTE_BIT;
181
182 // Geometry data constants.
183 const std::vector<tcu::Vec3> kOffscreenTriangle =
184 {
185 // Triangle around (x=0, y=2) z=-5
186 tcu::Vec3( 0.0f, 2.5f, -5.0f),
187 tcu::Vec3(-0.5f, 1.5f, -5.0f),
188 tcu::Vec3( 0.5f, 1.5f, -5.0f),
189 };
190 const std::vector<tcu::Vec3> kOnscreenTriangle =
191 {
192 // Triangle around (x=0, y=2) z=5
193 tcu::Vec3( 0.0f, 2.5f, 5.0f),
194 tcu::Vec3(-0.5f, 1.5f, 5.0f),
195 tcu::Vec3( 0.5f, 1.5f, 5.0f),
196 };
197 const tcu::Vec4 kGoodOrigin (0.0f, 2.0f, 0.0f, 0.0f); // Around (x=0, y=2) z=0.
198 const tcu::Vec4 kBadOrigin (0.0f, 8.0f, 0.0f, 0.0f); // Too high, around (x=0, y=8) depth 0.
199 const tcu::Vec4 kGoodDirection (0.0f, 0.0f, 1.0f, 0.0f); // Towards +z.
200 const tcu::Vec4 kBadDirection (1.0f, 0.0f, 0.0f, 0.0f); // Towards +x.
201 const float kGoodTmin = 4.0f; // Good to travel from z=0 to z=5.
202 const float kGoodTmax = 6.0f; // Ditto.
203 const float kBadTmin = 5.5f; // Tmin after triangle.
204 const float kBadTmax = 4.5f; // Tmax before triangle.
205 const deUint32 kGoodFlags = 0u; // MaskNone
206 const deUint32 kBadFlags = 256u; // SkipTrianglesKHR
207 const deUint32 kGoodCullMask = 0x0Fu; // Matches instance.
208 const deUint32 kBadCullMask = 0xF0u; // Does not match instance.
209
210 // Command pool and buffer.
211 const auto cmdPool = makeCommandPool(vkd, device, qIndex);
212 const auto cmdBufferPtr = allocateCommandBuffer(vkd, device, cmdPool.get(), VK_COMMAND_BUFFER_LEVEL_PRIMARY);
213 const auto cmdBuffer = cmdBufferPtr.get();
214
215 beginCommandBuffer(vkd, cmdBuffer);
216
217 // Build acceleration structures.
218 auto topLevelAS = makeTopLevelAccelerationStructure();
219 auto bottomLevelAS = makeBottomLevelAccelerationStructure();
220
221 // Putting the offscreen triangle first makes sure hits have a geometryIndex=1, meaning sbtRecordStride matters.
222 std::vector<const std::vector<tcu::Vec3>*> geometries;
223 geometries.push_back(&kOffscreenTriangle);
224 geometries.push_back(&kOnscreenTriangle);
225
226 for (const auto& geometryPtr : geometries)
227 bottomLevelAS->addGeometry(*geometryPtr, true /* is triangles */);
228
229 bottomLevelAS->createAndBuild(vkd, device, cmdBuffer, alloc);
230
231 de::SharedPtr<BottomLevelAccelerationStructure> blasSharedPtr (bottomLevelAS.release());
232 topLevelAS->setInstanceCount(1);
233 topLevelAS->addInstance(blasSharedPtr, identityMatrix3x4, 0u, kGoodCullMask, 0u, VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR);
234 topLevelAS->createAndBuild(vkd, device, cmdBuffer, alloc);
235
236 // Input storage buffer.
237 const auto inputBufferSize = static_cast<VkDeviceSize>(sizeof(ArgsBufferData));
238 const auto inputBufferInfo = makeBufferCreateInfo(inputBufferSize, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT);
239 BufferWithMemory inputBuffer (vkd, device, alloc, inputBufferInfo, MemoryRequirement::HostVisible);
240 auto& inputBufferAlloc = inputBuffer.getAllocation();
241
242 // Output storage buffer.
243 const auto outputBufferSize = static_cast<VkDeviceSize>(sizeof(deUint32));
244 const auto outputBufferInfo = makeBufferCreateInfo(outputBufferSize, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT);
245 BufferWithMemory outputBuffer (vkd, device, alloc, outputBufferInfo, MemoryRequirement::HostVisible);
246 auto& outputBufferAlloc = outputBuffer.getAllocation();
247
248 // Fill output buffer with an initial invalid value.
249 deMemset(outputBufferAlloc.getHostPtr(), 42, static_cast<size_t>(outputBufferSize));
250 flushAlloc(vkd, device, outputBufferAlloc);
251
252 // Descriptor set layout and pipeline layout.
253 DescriptorSetLayoutBuilder setLayoutBuilder;
254 setLayoutBuilder.addSingleBinding(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR, stages);
255 setLayoutBuilder.addSingleBinding(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, stages);
256 setLayoutBuilder.addSingleBinding(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, stages);
257 const auto setLayout = setLayoutBuilder.build(vkd, device);
258 const auto pipelineLayout = makePipelineLayout(vkd, device, setLayout.get());
259
260 // Descriptor pool and set.
261 DescriptorPoolBuilder poolBuilder;
262 poolBuilder.addType(VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR);
263 poolBuilder.addType(VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 2u);
264 const auto descriptorPool = poolBuilder.build(vkd, device, VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT, 1u);
265 const auto descriptorSet = makeDescriptorSet(vkd, device, descriptorPool.get(), setLayout.get());
266
267 // Update descriptor set.
268 {
269 const VkWriteDescriptorSetAccelerationStructureKHR accelDescInfo =
270 {
271 VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_KHR,
272 nullptr,
273 1u,
274 topLevelAS.get()->getPtr(),
275 };
276
277 const auto inputBufferDescInfo = makeDescriptorBufferInfo(inputBuffer.get(), 0ull, VK_WHOLE_SIZE);
278 const auto outputBufferDescInfo = makeDescriptorBufferInfo(outputBuffer.get(), 0ull, VK_WHOLE_SIZE);
279
280 DescriptorSetUpdateBuilder updateBuilder;
281 updateBuilder.writeSingle(descriptorSet.get(), DescriptorSetUpdateBuilder::Location::binding(0u), VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR, &accelDescInfo);
282 updateBuilder.writeSingle(descriptorSet.get(), DescriptorSetUpdateBuilder::Location::binding(1u), VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, &inputBufferDescInfo);
283 updateBuilder.writeSingle(descriptorSet.get(), DescriptorSetUpdateBuilder::Location::binding(2u), VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, &outputBufferDescInfo);
284 updateBuilder.update(vkd, device);
285 }
286
287 // Shader modules.
288 const auto compModule = createShaderModule(vkd, device, m_context.getBinaryCollection().get("comp"), 0u);
289
290 // Generate ids for the closest hit and miss shaders according to the test parameters.
291 DE_ASSERT(geometries.size() > 0u);
292
293 const VkPipelineShaderStageCreateInfo stageCreateInfo =
294 {
295 VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, // VkStructureType sType;
296 nullptr, // const void* pNext;
297 0u, // VkPipelineShaderStageCreateFlags flags;
298 VK_SHADER_STAGE_COMPUTE_BIT, // VkShaderStageFlagBits stage;
299 compModule.get(), // VkShaderModule module;
300 "main", // const char* pName;
301 nullptr, // const VkSpecializationInfo* pSpecializationInfo;
302 };
303
304 const VkComputePipelineCreateInfo pipelineCreateInfo =
305 {
306 VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO, // VkStructureType sType;
307 nullptr, // const void* pNext;
308 0u, // VkPipelineCreateFlags flags;
309 stageCreateInfo, // VkPipelineShaderStageCreateInfo stage;
310 pipelineLayout.get(), // VkPipelineLayout layout;
311 DE_NULL, // VkPipeline basePipelineHandle;
312 0, // deInt32 basePipelineIndex;
313 };
314
315 const auto pipeline = createComputePipeline(vkd, device, DE_NULL, &pipelineCreateInfo);
316
317 // Fill input buffer values.
318 {
319 const ArgsBufferData argsBufferData =
320 {
321 ((m_params.missCause == MissCause::ORIGIN) ? kBadOrigin : kGoodOrigin),
322 ((m_params.missCause == MissCause::DIRECTION) ? kBadDirection : kGoodDirection),
323 ((m_params.missCause == MissCause::TMIN) ? kBadTmin : kGoodTmin),
324 ((m_params.missCause == MissCause::TMAX) ? kBadTmax : kGoodTmax),
325 ((m_params.missCause == MissCause::FLAGS) ? kBadFlags : kGoodFlags),
326 ((m_params.missCause == MissCause::CULL_MASK) ? kBadCullMask : kGoodCullMask),
327 };
328
329 deMemcpy(inputBufferAlloc.getHostPtr(), &argsBufferData, sizeof(argsBufferData));
330 flushAlloc(vkd, device, inputBufferAlloc);
331 }
332
333 // Trace rays.
334 vkd.cmdBindPipeline(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipeline.get());
335 vkd.cmdBindDescriptorSets(cmdBuffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipelineLayout.get(), 0u, 1u, &descriptorSet.get(), 0u, nullptr);
336 vkd.cmdDispatch(cmdBuffer, 1u, 1u, 1u);
337
338 // Barrier for the output buffer.
339 const auto bufferBarrier = makeMemoryBarrier(VK_ACCESS_SHADER_WRITE_BIT, VK_ACCESS_HOST_READ_BIT);
340 vkd.cmdPipelineBarrier(cmdBuffer, VK_PIPELINE_STAGE_RAY_TRACING_SHADER_BIT_KHR, VK_PIPELINE_STAGE_HOST_BIT, 0u, 1u, &bufferBarrier, 0u, nullptr, 0u, nullptr);
341
342 endCommandBuffer(vkd, cmdBuffer);
343 submitCommandsAndWait(vkd, device, queue, cmdBuffer);
344
345 // Check output value.
346 invalidateAlloc(vkd, device, outputBufferAlloc);
347 deUint32 outputVal = std::numeric_limits<deUint32>::max();
348 deMemcpy(&outputVal, outputBufferAlloc.getHostPtr(), sizeof(outputVal));
349 const auto expectedVal = ((m_params.missCause == MissCause::NONE) ? 1u : 0u);
350
351 std::ostringstream msg;
352 msg << "Output value: " << outputVal << " (expected " << expectedVal << ")";
353
354 if (outputVal != expectedVal)
355 return tcu::TestStatus::fail(msg.str());
356
357 auto& log = m_context.getTestContext().getLog();
358 log << tcu::TestLog::Message << msg.str() << tcu::TestLog::EndMessage;
359
360 return tcu::TestStatus::pass("Pass");
361 }
362
363 } // anonymous
364
createNonUniformArgsTests(tcu::TestContext & testCtx)365 tcu::TestCaseGroup* createNonUniformArgsTests (tcu::TestContext& testCtx)
366 {
367 de::MovePtr<tcu::TestCaseGroup> nonUniformGroup(new tcu::TestCaseGroup(testCtx, "non_uniform_args", "Test non-uniform arguments in traceRayExt()"));
368
369 NonUniformParams params;
370 for (int causeIdx = static_cast<int>(MissCause::NONE); causeIdx < static_cast<int>(MissCause::CAUSE_COUNT); ++causeIdx)
371 {
372 params.missCause = static_cast<MissCause>(causeIdx);
373 const std::string testName = ((params.missCause == MissCause::NONE) ? std::string("no_miss") : "miss_cause_" + de::toString(causeIdx));
374 nonUniformGroup->addChild(new NonUniformArgsCase(testCtx, testName, "", params));
375 }
376
377 return nonUniformGroup.release();
378 }
379
380 } // RayQuery
381 } // vkt
382