1 #ifndef _VKTSHADERRENDERCASE_HPP
2 #define _VKTSHADERRENDERCASE_HPP
3 /*------------------------------------------------------------------------
4 * Vulkan Conformance Tests
5 * ------------------------
7 * Copyright (c) 2015 The Khronos Group Inc.
8 * Copyright (c) 2015 Samsung Electronics Co., Ltd.
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and/or associated documentation files (the
12 * "Materials"), to deal in the Materials without restriction, including
13 * without limitation the rights to use, copy, modify, merge, publish,
14 * distribute, sublicense, and/or sell copies of the Materials, and to
15 * permit persons to whom the Materials are furnished to do so, subject to
16 * the following conditions:
18 * The above copyright notice(s) and this permission notice shall be included
19 * in all copies or substantial portions of the Materials.
21 * The Materials are Confidential Information as defined by the
22 * Khronos Membership Agreement until designated non-confidential by Khronos,
23 * at which point this condition clause shall be removed.
25 * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
28 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
29 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
30 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
31 * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
35 * \brief Vulkan ShaderRenderCase
36 *//*--------------------------------------------------------------------*/
38 #include "tcuTexture.hpp"
39 #include "tcuSurface.hpp"
42 #include "deSharedPtr.hpp"
43 #include "deUniquePtr.hpp"
46 #include "vkPrograms.hpp"
48 #include "vkMemUtil.hpp"
49 #include "vkBuilderUtil.hpp"
51 #include "vktTestCaseUtil.hpp"
55 namespace shaderrendercase
59 class ShaderRenderCaseInstance;
75 TextureBinding (const tcu::Archive& archive,
78 const tcu::Sampler& sampler);
79 ~TextureBinding (void);
80 Type getType (void) const { return m_type; }
81 const tcu::Sampler& getSampler (void) const { return m_sampler; }
82 const tcu::Texture2D& get2D (void) const { DE_ASSERT(getType() == TYPE_2D && m_binding.tex2D !=NULL); return *m_binding.tex2D; }
85 TextureBinding (const TextureBinding&); // not allowed!
86 TextureBinding& operator= (const TextureBinding&); // not allowed!
88 static de::MovePtr<tcu::Texture2D> loadTexture2D (const tcu::Archive& archive, const char* filename);
91 tcu::Sampler m_sampler;
95 const tcu::Texture2D* tex2D;
99 typedef de::SharedPtr<TextureBinding> TextureBindingSp;
101 // ShaderEvalContext.
103 class ShaderEvalContext
109 MAX_USER_ATTRIBS = 4,
115 tcu::Sampler sampler;
116 const tcu::Texture2D* tex2D;
117 const tcu::TextureCube* texCube;
118 const tcu::Texture2DArray* tex2DArray;
119 const tcu::Texture3D* tex3D;
121 inline ShaderSampler (void)
124 , tex2DArray(DE_NULL)
130 ShaderEvalContext (const QuadGrid& quadGrid);
131 ~ShaderEvalContext (void);
133 void reset (float sx, float sy);
137 tcu::Vec4 unitCoords;
138 tcu::Vec4 constCoords;
140 tcu::Vec4 in[MAX_USER_ATTRIBS];
141 ShaderSampler textures[MAX_TEXTURES];
148 inline void discard (void) { isDiscarded = true; }
149 tcu::Vec4 texture2D (int unitNdx, const tcu::Vec2& coords);
152 const QuadGrid& m_quadGrid;
155 typedef void (*ShaderEvalFunc) (ShaderEvalContext& c);
157 inline void evalCoordsPassthroughX (ShaderEvalContext& c) { c.color.x() = c.coords.x(); }
158 inline void evalCoordsPassthroughXY (ShaderEvalContext& c) { c.color.xy() = c.coords.swizzle(0,1); }
159 inline void evalCoordsPassthroughXYZ (ShaderEvalContext& c) { c.color.xyz() = c.coords.swizzle(0,1,2); }
160 inline void evalCoordsPassthrough (ShaderEvalContext& c) { c.color = c.coords; }
161 inline void evalCoordsSwizzleWZYX (ShaderEvalContext& c) { c.color = c.coords.swizzle(3,2,1,0); }
164 // Either inherit a class with overridden evaluate() or just pass in an evalFunc.
166 class ShaderEvaluator
169 ShaderEvaluator (void);
170 ShaderEvaluator (const ShaderEvalFunc evalFunc);
171 virtual ~ShaderEvaluator (void);
173 virtual void evaluate (ShaderEvalContext& ctx) const;
176 ShaderEvaluator (const ShaderEvaluator&); // not allowed!
177 ShaderEvaluator& operator= (const ShaderEvaluator&); // not allowed!
179 const ShaderEvalFunc m_evalFunc;
184 typedef void (*UniformSetupFunc) (ShaderRenderCaseInstance& instance, const tcu::Vec4& constCoords);
190 UniformSetup (const UniformSetupFunc setup);
191 virtual ~UniformSetup (void);
192 virtual void setup (ShaderRenderCaseInstance& instance, const tcu::Vec4& constCoords) const;
195 UniformSetup (const UniformSetup&); // not allowed!
196 UniformSetup& operator= (const UniformSetup&); // not allowed!
198 const UniformSetupFunc m_setupFunc;
201 typedef void (*AttributeSetupFunc) (ShaderRenderCaseInstance& instance, deUint32 numVertices);
203 class ShaderRenderCase : public vkt::TestCase
206 ShaderRenderCase (tcu::TestContext& testCtx,
207 const std::string& name,
208 const std::string& description,
209 const bool isVertexCase,
210 const ShaderEvalFunc evalFunc,
211 const UniformSetup* uniformSetup,
212 const AttributeSetupFunc attribFunc);
214 ShaderRenderCase (tcu::TestContext& testCtx,
215 const std::string& name,
216 const std::string& description,
217 const bool isVertexCase,
218 const ShaderEvaluator* evaluator,
219 const UniformSetup* uniformSetup,
220 const AttributeSetupFunc attribFunc);
223 virtual ~ShaderRenderCase (void);
224 virtual void initPrograms (vk::SourceCollections& programCollection) const;
225 virtual TestInstance* createInstance (Context& context) const;
228 std::string m_vertShaderSource;
229 std::string m_fragShaderSource;
231 const bool m_isVertexCase;
232 const de::UniquePtr<const ShaderEvaluator> m_evaluator;
233 const de::UniquePtr<const UniformSetup> m_uniformSetup;
234 const AttributeSetupFunc m_attribFunc;
338 enum BaseAttributeType
358 // ShaderRenderCaseInstance.
360 class ShaderRenderCaseInstance : public vkt::TestInstance
363 ShaderRenderCaseInstance (Context& context,
364 const bool isVertexCase,
365 const ShaderEvaluator& evaluator,
366 const UniformSetup& uniformSetup,
367 const AttributeSetupFunc attribFunc);
369 virtual ~ShaderRenderCaseInstance (void);
370 virtual tcu::TestStatus iterate (void);
372 void addAttribute (deUint32 bindingLocation,
374 deUint32 sizePerElement,
377 void useAttribute (deUint32 bindingLocation,
378 BaseAttributeType type);
381 void addUniform (deUint32 bindingLocation,
382 vk::VkDescriptorType descriptorType,
384 void addUniform (deUint32 bindingLocation,
385 vk::VkDescriptorType descriptorType,
388 void useUniform (deUint32 bindingLocation,
389 BaseUniformType type);
390 void useSampler2D (deUint32 bindingLocation,
394 virtual void setup (void);
395 virtual void setupUniforms (const tcu::Vec4& constCoords);
397 const tcu::IVec2 getViewportSize (void) const;
399 std::vector<tcu::Mat4> m_userAttribTransforms;
400 const tcu::Vec4 m_clearColor;
401 std::vector<TextureBindingSp> m_textures;
403 vk::Allocator& m_memAlloc;
407 void setupTextures (void);
408 de::MovePtr<vk::Allocation> uploadImage2D (const tcu::Texture2D& refTexture,
409 const vk::VkImage& vkTexture);
410 vk::Move<vk::VkImage> createImage2D (const tcu::Texture2D& texture,
411 const vk::VkFormat format,
412 const vk::VkImageUsageFlags usage,
413 const vk::VkImageTiling tiling);
414 void copyTilingImageToOptimal (const vk::VkImage& srcImage,
415 const vk::VkImage& dstImage,
419 void setupUniformData (deUint32 bindingLocation, deUint32 size, const void* dataPtr);
420 void setupDefaultInputs (const QuadGrid& quadGrid);
422 void render (tcu::Surface& result, const QuadGrid& quadGrid);
423 void computeVertexReference (tcu::Surface& result, const QuadGrid& quadGrid);
424 void computeFragmentReference (tcu::Surface& result, const QuadGrid& quadGrid);
425 bool compareImages (const tcu::Surface& resImage,
426 const tcu::Surface& refImage,
427 float errorThreshold);
429 const bool m_isVertexCase;
430 const ShaderEvaluator& m_evaluator;
431 const UniformSetup& m_uniformSetup;
432 const AttributeSetupFunc m_attribFunc;
434 struct EnabledBaseAttribute
437 BaseAttributeType type;
439 std::vector<EnabledBaseAttribute> m_enabledBaseAttributes;
441 const tcu::IVec2 m_renderSize;
442 const vk::VkFormat m_colorFormat;
444 vk::Move<vk::VkImage> m_colorImage;
445 de::MovePtr<vk::Allocation> m_colorImageAlloc;
446 vk::Move<vk::VkImageView> m_colorImageView;
448 vk::Move<vk::VkRenderPass> m_renderPass;
449 vk::Move<vk::VkFramebuffer> m_framebuffer;
450 vk::Move<vk::VkPipelineLayout> m_pipelineLayout;
451 vk::Move<vk::VkPipeline> m_graphicsPipeline;
453 vk::Move<vk::VkShaderModule> m_vertexShaderModule;
454 vk::Move<vk::VkShaderModule> m_fragmentShaderModule;
455 vk::Move<vk::VkShader> m_vertexShader;
456 vk::Move<vk::VkShader> m_fragmentShader;
458 vk::Move<vk::VkBuffer> m_indiceBuffer;
459 de::MovePtr<vk::Allocation> m_indiceBufferAlloc;
461 vk::Move<vk::VkDescriptorSetLayout> m_descriptorSetLayout;
463 vk::Move<vk::VkDescriptorPool> m_descriptorPool;
464 vk::Move<vk::VkDescriptorSet> m_descriptorSet;
466 vk::Move<vk::VkCmdPool> m_cmdPool;
467 vk::Move<vk::VkCmdBuffer> m_cmdBuffer;
469 vk::Move<vk::VkFence> m_fence;
471 vk::DescriptorSetLayoutBuilder m_descriptorSetLayoutBuilder;
472 vk::DescriptorPoolBuilder m_descriptorPoolBuilder;
473 vk::DescriptorSetUpdateBuilder m_descriptorSetUpdateBuilder;
475 typedef de::SharedPtr<vk::Unique<vk::VkBuffer> > VkBufferSp;
476 typedef de::SharedPtr<vk::Unique<vk::VkBufferView> > VkBufferViewSp;
478 typedef de::SharedPtr<vk::Unique<vk::VkImage> > VkImageSp;
479 typedef de::SharedPtr<vk::Unique<vk::VkImageView> > VkImageViewSp;
480 typedef de::SharedPtr<vk::Unique<vk::VkSampler> > VkSamplerSp;
481 typedef de::SharedPtr<vk::Allocation> AllocationSp;
486 UniformInfo (void) {}
487 virtual ~UniformInfo (void) {}
489 vk::VkDescriptorType type;
490 vk::VkDescriptorInfo descriptor;
494 class BufferUniform : public UniformInfo
497 BufferUniform (void) {}
498 virtual ~BufferUniform (void) {}
501 VkBufferViewSp bufferView;
505 class SamplerUniform : public UniformInfo
508 SamplerUniform (void) {}
509 virtual ~SamplerUniform (void) {}
512 VkImageViewSp imageView;
517 typedef de::SharedPtr<de::UniquePtr<UniformInfo> > UniformInfoSp;
518 std::vector<UniformInfoSp> m_uniformInfos;
520 std::vector<vk::VkVertexInputBindingDescription> m_vertexBindingDescription;
521 std::vector<vk::VkVertexInputAttributeDescription> m_vertexattributeDescription;
523 std::vector<VkBufferSp> m_vertexBuffers;
524 std::vector<AllocationSp> m_vertexBufferAllocs;
528 void ShaderRenderCaseInstance::addUniform (deUint32 bindingLocation, vk::VkDescriptorType descriptorType, const T& data)
530 addUniform(bindingLocation, descriptorType, sizeof(T), &data);
533 } // shaderrendercase
536 #endif // _VKTSHADERRENDERCASE_HPP