1 #ifndef _VKTSPVASMGRAPHICSSHADERTESTUTIL_HPP
2 #define _VKTSPVASMGRAPHICSSHADERTESTUTIL_HPP
3 /*-------------------------------------------------------------------------
4 * Vulkan Conformance Tests
5 * ------------------------
7 * Copyright (c) 2017 Google Inc.
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
23 * \brief Graphics pipeline and helper functions for SPIR-V assembly tests
24 *//*--------------------------------------------------------------------*/
26 #include "tcuCommandLine.hpp"
27 #include "tcuRGBA.hpp"
29 #include "vkPrograms.hpp"
30 #include "vktSpvAsmComputeShaderTestUtil.hpp"
31 #include "vktSpvAsmUtils.hpp"
32 #include "vktTestCaseUtil.hpp"
34 #include "deRandom.hpp"
35 #include "deSharedPtr.hpp"
44 namespace SpirVAssembly
47 typedef vk::Unique<VkBuffer> BufferHandleUp;
48 typedef vk::Unique<VkImage> ImageHandleUp;
49 typedef vk::Unique<VkImageView> ImageViewHandleUp;
50 typedef vk::Unique<VkSampler> SamplerHandleUp;
51 typedef de::SharedPtr<BufferHandleUp> BufferHandleSp;
52 typedef de::SharedPtr<ImageHandleUp> ImageHandleSp;
53 typedef de::SharedPtr<ImageViewHandleUp> ImageViewHandleSp;
54 typedef de::SharedPtr<SamplerHandleUp> SamplerHandleSp;
55 typedef vk::Unique<vk::VkShaderModule> ModuleHandleUp;
56 typedef de::SharedPtr<ModuleHandleUp> ModuleHandleSp;
57 typedef std::pair<std::string, vk::VkShaderStageFlagBits> EntryToStage;
58 typedef std::map<std::string, std::vector<EntryToStage> > ModuleMap;
59 typedef std::map<vk::VkShaderStageFlagBits, std::vector<deInt32> > StageToSpecConstantMap;
60 typedef std::pair<vk::VkDescriptorType, BufferSp> Resource;
67 NUMBERTYPE_END32, // Marks the end of 32-bit scalar types
73 typedef enum RoundingModeFlags_e
75 ROUNDINGMODE_RTE = 0x1, // Round to nearest even
76 ROUNDINGMODE_RTZ = 0x2, // Round to zero
79 typedef bool (*GraphicsVerifyIOFunc) (const std::vector<Resource>& inputs,
80 const std::vector<AllocationSp>& outputAllocations,
81 const std::vector<Resource>& expectedOutputs,
84 // Resources used by graphics-pipeline-based tests.
85 struct GraphicsResources
87 // Resources used as inputs.
88 std::vector<Resource> inputs;
89 // Resources used as outputs. The data supplied will be used as
90 // the expected outputs for the corresponding bindings by default.
91 // If other behaviors are needed, please provide a custom verifyIO.
92 std::vector<Resource> outputs;
93 // If null, a default verification will be performed by comparing the
94 // memory pointed to by outputAllocations and the contents of
95 // expectedOutputs. Otherwise the function pointed to by verifyIO will
96 // be called. If true is returned, then the test case is assumed to
97 // have passed, if false is returned, then the test case is assumed
99 GraphicsVerifyIOFunc verifyIO;
106 // Interface data type.
109 IFDataType (deUint32 numE, NumberType elementT)
111 , elementType (elementT)
113 DE_ASSERT(numE > 0 && numE < 5);
114 DE_ASSERT(elementT != NUMBERTYPE_END32);
117 IFDataType (const IFDataType& that)
118 : numElements (that.numElements)
119 , elementType (that.elementType)
122 deUint32 getElementNumBytes (void) const;
123 deUint32 getNumBytes (void) const { return numElements * getElementNumBytes(); }
125 vk::VkFormat getVkFormat (void) const;
127 tcu::TextureFormat getTextureFormat (void) const;
129 std::string str (void) const;
131 bool elementIs32bit (void) const { return elementType < NUMBERTYPE_END32; }
132 bool isVector (void) const { return numElements > 1; }
134 deUint32 numElements;
135 NumberType elementType;
138 typedef std::pair<IFDataType, BufferSp> Interface;
140 // Interface variables used by graphics-pipeline-based tests.
141 class GraphicsInterfaces
144 GraphicsInterfaces ()
145 : rndMode (static_cast<RoundingModeFlags>(0))
148 GraphicsInterfaces (const GraphicsInterfaces& that)
149 : inputs (that.inputs)
150 , outputs (that.outputs)
151 , rndMode (that.rndMode)
154 void setInputOutput (const Interface& input, const Interface& output)
158 inputs.push_back(input);
159 outputs.push_back(output);
162 const IFDataType& getInputType (void) const
164 DE_ASSERT(inputs.size() == 1);
165 return inputs.front().first;
168 const IFDataType& getOutputType (void) const
170 DE_ASSERT(outputs.size() == 1);
171 return outputs.front().first;
174 const BufferSp& getInputBuffer (void) const
176 DE_ASSERT(inputs.size() == 1);
177 return inputs.front().second;
180 const BufferSp& getOutputBuffer (void) const
182 DE_ASSERT(outputs.size() == 1);
183 return outputs.front().second;
186 bool empty (void) const
188 return inputs.size() == 0;
191 void setRoundingMode (RoundingModeFlags flag)
195 RoundingModeFlags getRoundingMode (void) const
200 // vector<Interface> acts as a null-able Interface here. Canonically we should use
201 // std::unique_ptr, but sadly we cannot leverage C++11 in dEQP. dEQP has its own
202 // de::UniquePtr, but still cumbersome to use in InstanceContext and do copies
203 // at various places.
204 // Public methods should make sure that there are less than two elements in both
205 // members and both members have the same number of elements.
206 std::vector<Interface> inputs;
207 std::vector<Interface> outputs;
208 RoundingModeFlags rndMode;
218 PushConstants (const PushConstants& that)
222 void setPushConstant (const BufferSp& pc)
228 bool empty (void) const
233 const BufferSp& getBuffer(void) const
235 DE_ASSERT(pcs.size() == 1);
240 // Right now we only support one field in the push constant block.
241 std::vector<BufferSp> pcs;
244 // Returns the corresponding buffer usage flag bit for the given descriptor type.
245 VkBufferUsageFlagBits getMatchingBufferUsageFlagBit(VkDescriptorType dType);
247 // Context for a specific test instantiation. For example, an instantiation
248 // may test colors yellow/magenta/cyan/mauve in a tesselation shader
249 // with an entry point named 'main_to_the_main'
250 struct InstanceContext
252 // Map of modules to what entry_points we care to use from those modules.
254 tcu::RGBA inputColors[4];
255 tcu::RGBA outputColors[4];
256 // Concrete SPIR-V code to test via boilerplate specialization.
257 std::map<std::string, std::string> testCodeFragments;
258 StageToSpecConstantMap specConstants;
259 bool hasTessellation;
260 vk::VkShaderStageFlagBits requiredStages;
261 std::vector<std::string> requiredDeviceExtensions;
262 std::vector<std::string> requiredDeviceFeatures;
263 VulkanFeatures requestedFeatures;
264 PushConstants pushConstants;
265 // Specifies the (one or more) stages that use a customized shader code.
266 VkShaderStageFlags customizedStages;
267 // Possible resources used by the graphics pipeline.
268 // If it is not empty, a single descriptor set (number 0) will be allocated
269 // to point to all resources specified. Binding numbers are allocated in
270 // accord with the resources' order in the vector; outputs are allocated
272 GraphicsResources resources;
273 // Possible interface variables use by the graphics pipeline.
274 // If it is not empty, input/output variables will be set up for shader stages
275 // in the test. Both the input and output variable will take location #2 in the
276 // pipeline for all stages, except that the output variable in the fragment
277 // stage will take location #1.
278 GraphicsInterfaces interfaces;
279 qpTestResult failResult;
280 std::string failMessageTemplate; //!< ${reason} in the template will be replaced with a detailed failure message
282 InstanceContext (const tcu::RGBA (&inputs)[4],
283 const tcu::RGBA (&outputs)[4],
284 const std::map<std::string, std::string>& testCodeFragments_,
285 const StageToSpecConstantMap& specConstants_,
286 const PushConstants& pushConsants_,
287 const GraphicsResources& resources_,
288 const GraphicsInterfaces& interfaces_,
289 const std::vector<std::string>& extensions_,
290 const std::vector<std::string>& features_,
291 VulkanFeatures vulkanFeatures_,
292 VkShaderStageFlags customizedStages_);
294 InstanceContext (const InstanceContext& other);
296 std::string getSpecializedFailMessage (const std::string& failureReason);
299 // A description of a shader to be used for a single stage of the graphics pipeline.
302 // The module that contains this shader entrypoint.
303 std::string moduleName;
305 // The name of the entrypoint.
306 std::string entryName;
308 // Which shader stage this entry point represents.
309 vk::VkShaderStageFlagBits stage;
311 ShaderElement (const std::string& moduleName_, const std::string& entryPoint_, vk::VkShaderStageFlagBits shaderStage_);
314 template <typename T>
315 const std::string numberToString (T number)
317 std::stringstream ss;
322 // Performs a bitwise copy of source to the destination type Dest.
323 template <typename Dest, typename Src>
324 Dest bitwiseCast(Src source)
327 DE_STATIC_ASSERT(sizeof(source) == sizeof(dest));
328 deMemcpy(&dest, &source, sizeof(dest));
332 template<typename T> T randomScalar (de::Random& rnd, T minValue, T maxValue);
333 template<> inline float randomScalar (de::Random& rnd, float minValue, float maxValue) { return rnd.getFloat(minValue, maxValue); }
334 template<> inline deInt32 randomScalar (de::Random& rnd, deInt32 minValue, deInt32 maxValue) { return rnd.getInt(minValue, maxValue); }
337 void getDefaultColors (tcu::RGBA (&colors)[4]);
339 void getHalfColorsFullAlpha (tcu::RGBA (&colors)[4]);
341 void getInvertedDefaultColors (tcu::RGBA (&colors)[4]);
343 // Creates fragments that specialize into a simple pass-through shader (of any kind).
344 std::map<std::string, std::string> passthruFragments(void);
346 void createCombinedModule(vk::SourceCollections& dst, InstanceContext);
348 // This has two shaders of each stage. The first
349 // is a passthrough, the second inverts the color.
350 void createMultipleEntries(vk::SourceCollections& dst, InstanceContext);
352 // Turns a statically sized array of ShaderElements into an instance-context
353 // by setting up the mapping of modules to their contained shaders and stages.
354 // The inputs and expected outputs are given by inputColors and outputColors
356 InstanceContext createInstanceContext (const ShaderElement (&elements)[N],
357 const tcu::RGBA (&inputColors)[4],
358 const tcu::RGBA (&outputColors)[4],
359 const std::map<std::string, std::string>& testCodeFragments,
360 const StageToSpecConstantMap& specConstants,
361 const PushConstants& pushConstants,
362 const GraphicsResources& resources,
363 const GraphicsInterfaces& interfaces,
364 const std::vector<std::string>& extensions,
365 const std::vector<std::string>& features,
366 VulkanFeatures vulkanFeatures,
367 VkShaderStageFlags customizedStages,
368 const qpTestResult failResult = QP_TEST_RESULT_FAIL,
369 const std::string& failMessageTemplate = std::string())
371 InstanceContext ctx (inputColors, outputColors, testCodeFragments, specConstants, pushConstants, resources, interfaces, extensions, features, vulkanFeatures, customizedStages);
372 for (size_t i = 0; i < N; ++i)
374 ctx.moduleMap[elements[i].moduleName].push_back(std::make_pair(elements[i].entryName, elements[i].stage));
375 ctx.requiredStages = static_cast<VkShaderStageFlagBits>(ctx.requiredStages | elements[i].stage);
377 ctx.failResult = failResult;
378 if (!failMessageTemplate.empty())
379 ctx.failMessageTemplate = failMessageTemplate;
383 // The same as createInstanceContext above, without extensions, spec constants, and resources.
385 inline InstanceContext createInstanceContext (const ShaderElement (&elements)[N],
386 tcu::RGBA (&inputColors)[4],
387 const tcu::RGBA (&outputColors)[4],
388 const std::map<std::string, std::string>& testCodeFragments)
390 return createInstanceContext(elements, inputColors, outputColors, testCodeFragments,
391 StageToSpecConstantMap(), PushConstants(), GraphicsResources(),
392 GraphicsInterfaces(), std::vector<std::string>(), std::vector<std::string>(),
393 VulkanFeatures(), vk::VK_SHADER_STAGE_ALL);
396 // The same as createInstanceContext above, but with default colors.
398 InstanceContext createInstanceContext (const ShaderElement (&elements)[N],
399 const std::map<std::string, std::string>& testCodeFragments)
401 tcu::RGBA defaultColors[4];
402 getDefaultColors(defaultColors);
403 return createInstanceContext(elements, defaultColors, defaultColors, testCodeFragments);
407 void createTestsForAllStages (const std::string& name,
408 const tcu::RGBA (&inputColors)[4],
409 const tcu::RGBA (&outputColors)[4],
410 const std::map<std::string, std::string>& testCodeFragments,
411 const std::vector<deInt32>& specConstants,
412 const PushConstants& pushConstants,
413 const GraphicsResources& resources,
414 const GraphicsInterfaces& interfaces,
415 const std::vector<std::string>& extensions,
416 const std::vector<std::string>& features,
417 VulkanFeatures vulkanFeatures,
418 tcu::TestCaseGroup* tests,
419 const qpTestResult failResult = QP_TEST_RESULT_FAIL,
420 const std::string& failMessageTemplate = std::string());
422 inline void createTestsForAllStages (const std::string& name,
423 const tcu::RGBA (&inputColors)[4],
424 const tcu::RGBA (&outputColors)[4],
425 const std::map<std::string, std::string>& testCodeFragments,
426 tcu::TestCaseGroup* tests,
427 const qpTestResult failResult = QP_TEST_RESULT_FAIL,
428 const std::string& failMessageTemplate = std::string())
430 std::vector<deInt32> noSpecConstants;
431 PushConstants noPushConstants;
432 GraphicsResources noResources;
433 GraphicsInterfaces noInterfaces;
434 std::vector<std::string> noExtensions;
435 std::vector<std::string> noFeatures;
437 createTestsForAllStages(
438 name, inputColors, outputColors, testCodeFragments, noSpecConstants, noPushConstants,
439 noResources, noInterfaces, noExtensions, noFeatures, VulkanFeatures(),
440 tests, failResult, failMessageTemplate);
443 inline void createTestsForAllStages (const std::string& name,
444 const tcu::RGBA (&inputColors)[4],
445 const tcu::RGBA (&outputColors)[4],
446 const std::map<std::string, std::string>& testCodeFragments,
447 const std::vector<deInt32>& specConstants,
448 tcu::TestCaseGroup* tests,
449 const qpTestResult failResult = QP_TEST_RESULT_FAIL,
450 const std::string& failMessageTemplate = std::string())
452 PushConstants noPushConstants;
453 GraphicsResources noResources;
454 GraphicsInterfaces noInterfaces;
455 std::vector<std::string> noExtensions;
456 std::vector<std::string> noFeatures;
458 createTestsForAllStages(
459 name, inputColors, outputColors, testCodeFragments, specConstants, noPushConstants,
460 noResources, noInterfaces, noExtensions, noFeatures, VulkanFeatures(),
461 tests, failResult, failMessageTemplate);
464 inline void createTestsForAllStages (const std::string& name,
465 const tcu::RGBA (&inputColors)[4],
466 const tcu::RGBA (&outputColors)[4],
467 const std::map<std::string, std::string>& testCodeFragments,
468 const GraphicsResources& resources,
469 const std::vector<std::string>& extensions,
470 tcu::TestCaseGroup* tests,
471 VulkanFeatures vulkanFeatures = VulkanFeatures(),
472 const qpTestResult failResult = QP_TEST_RESULT_FAIL,
473 const std::string& failMessageTemplate = std::string())
475 std::vector<deInt32> noSpecConstants;
476 PushConstants noPushConstants;
477 GraphicsInterfaces noInterfaces;
478 std::vector<std::string> noFeatures;
480 createTestsForAllStages(
481 name, inputColors, outputColors, testCodeFragments, noSpecConstants, noPushConstants,
482 resources, noInterfaces, extensions, noFeatures, vulkanFeatures,
483 tests, failResult, failMessageTemplate);
486 inline void createTestsForAllStages (const std::string& name,
487 const tcu::RGBA (&inputColors)[4],
488 const tcu::RGBA (&outputColors)[4],
489 const std::map<std::string, std::string>& testCodeFragments,
490 const GraphicsInterfaces interfaces,
491 const std::vector<std::string>& extensions,
492 tcu::TestCaseGroup* tests,
493 VulkanFeatures vulkanFeatures = VulkanFeatures(),
494 const qpTestResult failResult = QP_TEST_RESULT_FAIL,
495 const std::string& failMessageTemplate = std::string())
497 GraphicsResources noResources;
498 std::vector<deInt32> noSpecConstants;
499 std::vector<std::string> noFeatures;
500 PushConstants noPushConstants;
502 createTestsForAllStages(
503 name, inputColors, outputColors, testCodeFragments, noSpecConstants, noPushConstants,
504 noResources, interfaces, extensions, noFeatures, vulkanFeatures,
505 tests, failResult, failMessageTemplate);
508 inline void createTestsForAllStages (const std::string& name,
509 const tcu::RGBA (&inputColors)[4],
510 const tcu::RGBA (&outputColors)[4],
511 const std::map<std::string, std::string>& testCodeFragments,
512 const PushConstants& pushConstants,
513 const GraphicsResources& resources,
514 const std::vector<std::string>& extensions,
515 tcu::TestCaseGroup* tests,
516 VulkanFeatures vulkanFeatures = VulkanFeatures(),
517 const qpTestResult failResult = QP_TEST_RESULT_FAIL,
518 const std::string& failMessageTemplate = std::string())
520 std::vector<deInt32> noSpecConstants;
521 GraphicsInterfaces noInterfaces;
522 std::vector<std::string> noFeatures;
524 createTestsForAllStages(
525 name, inputColors, outputColors, testCodeFragments, noSpecConstants, pushConstants,
526 resources, noInterfaces, extensions, noFeatures, vulkanFeatures,
527 tests, failResult, failMessageTemplate);
530 // Sets up and runs a Vulkan pipeline, then spot-checks the resulting image.
531 // Feeds the pipeline a set of colored triangles, which then must occur in the
532 // rendered image. The surface is cleared before executing the pipeline, so
533 // whatever the shaders draw can be directly spot-checked.
534 tcu::TestStatus runAndVerifyDefaultPipeline (Context& context, InstanceContext instance);
536 // Adds a new test to group using custom fragments for the tessellation-control
537 // stage and passthrough fragments for all other stages. Uses default colors
538 // for input and expected output.
539 void addTessCtrlTest(tcu::TestCaseGroup* group, const char* name, const std::map<std::string, std::string>& fragments);
541 // Given the original 32-bit float value, computes the corresponding 16-bit
542 // float value under the given rounding mode flags and compares with the
543 // returned 16-bit float value. Returns true if they are considered as equal.
545 // The following equivalence criteria are respected:
546 // * Positive and negative zeros are considered equivalent.
547 // * Denormalized floats are allowed to be flushed to zeros, including
548 // * Inputted 32bit denormalized float
549 // * Generated 16bit denormalized float
550 // * Different bit patterns of NaNs are allowed.
551 // * For the rest, require exactly the same bit pattern.
552 bool compare16BitFloat (float original, deUint16 returned, RoundingModeFlags flags, tcu::TestLog& log);
554 // Compare the returned 32-bit float against its expected value.
556 // The following equivalence criteria are respected:
557 // * Denormalized floats are allowed to be flushed to zeros, including
558 // * The expected value itself is a denormalized float
559 // * The expected value is a denormalized float if converted to 16bit
560 // * Different bit patterns of NaNs/Infs are allowed.
561 // * For the rest, use C++ float equivalence check.
562 bool compare32BitFloat (float expected, float returned, tcu::TestLog& log);
567 #endif // _VKTSPVASMGRAPHICSSHADERTESTUTIL_HPP