0adc5285c5d2d9789aea5bd40db6a06926933d91
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / shaderrendercase / vktShaderRenderCase.hpp
1 #ifndef _VKTSHADERRENDERCASE_HPP
2 #define _VKTSHADERRENDERCASE_HPP
3 /*------------------------------------------------------------------------
4  * Vulkan Conformance Tests
5  * ------------------------
6  *
7  * Copyright (c) 2015 The Khronos Group Inc.
8  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
9  *
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:
17  *
18  * The above copyright notice(s) and this permission notice shall be included
19  * in all copies or substantial portions of the Materials.
20  *
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.
24  *
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.
32  *
33  *//*!
34  * \file
35  * \brief Vulkan ShaderRenderCase
36  *//*--------------------------------------------------------------------*/
37
38 #include "tcuTexture.hpp"
39 #include "tcuSurface.hpp"
40
41 #include "deMemory.h"
42 #include "deSharedPtr.hpp"
43 #include "deUniquePtr.hpp"
44
45 #include "vkDefs.hpp"
46 #include "vkPrograms.hpp"
47 #include "vkRef.hpp"
48 #include "vkMemUtil.hpp"
49 #include "vkBuilderUtil.hpp"
50
51 #include "vktTestCaseUtil.hpp"
52 #include "vktTexture.hpp"
53
54 namespace vkt
55 {
56 namespace shaderrendercase
57 {
58
59 class QuadGrid;
60 class ShaderRenderCaseInstance;
61
62 class TextureBinding
63 {
64 public:
65         enum Type
66         {
67                 TYPE_NONE = 0,
68                 TYPE_2D,
69                 TYPE_CUBE_MAP,
70                 TYPE_2D_ARRAY,
71                 TYPE_3D,
72
73                 TYPE_LAST
74         };
75
76                                                         TextureBinding          (const tcu::Archive&    archive,
77                                                                                                  const char*                    filename,
78                                                                                                  const Type                             type,
79                                                                                                  const tcu::Sampler&    sampler);
80
81         Type                                    getType                         (void) const { return m_type;           }
82         const tcu::Sampler&             getSampler                      (void) const { return m_sampler;        }
83         const tcu::Texture2D*   get2D                           (void) const { DE_ASSERT(getType() == TYPE_2D); return m_binding.tex2D; }
84
85 private:
86         static tcu::Texture2D*  loadTexture2D           (const tcu::Archive& archive, const char* filename);
87
88         Type                                    m_type;
89         tcu::Sampler                    m_sampler;
90         union
91         {
92                 const tcu::Texture2D*   tex2D;
93         } m_binding;
94 };
95
96 // ShaderEvalContext.
97
98 class ShaderEvalContext
99 {
100 public:
101         // Limits.
102         enum
103         {
104                 MAX_USER_ATTRIBS        = 4,
105                 MAX_TEXTURES            = 4
106         };
107
108         struct ShaderSampler
109         {
110                 tcu::Sampler                            sampler;
111                 const tcu::Texture2D*           tex2D;
112                 const tcu::TextureCube*         texCube;
113                 const tcu::Texture2DArray*      tex2DArray;
114                 const tcu::Texture3D*           tex3D;
115
116                 inline ShaderSampler (void)
117                         : tex2D         (DE_NULL)
118                         , texCube       (DE_NULL)
119                         , tex2DArray(DE_NULL)
120                         , tex3D         (DE_NULL)
121                 {
122                 }
123         };
124
125                                                         ShaderEvalContext               (const QuadGrid& quadGrid);
126                                                         ~ShaderEvalContext              (void);
127
128         void                                    reset                                   (float sx, float sy);
129
130         // Inputs.
131         tcu::Vec4                               coords;
132         tcu::Vec4                               unitCoords;
133         tcu::Vec4                               constCoords;
134
135         tcu::Vec4                               in[MAX_USER_ATTRIBS];
136         ShaderSampler                   textures[MAX_TEXTURES];
137
138         // Output.
139         tcu::Vec4                               color;
140         bool                                    isDiscarded;
141
142         // Functions.
143         inline void                             discard                                 (void)  { isDiscarded = true; }
144         tcu::Vec4                               texture2D                               (int unitNdx, const tcu::Vec2& coords);
145
146 private:
147         const QuadGrid&                 m_quadGrid;
148 };
149
150 typedef void (*ShaderEvalFunc) (ShaderEvalContext& c);
151
152 inline void evalCoordsPassthroughX              (ShaderEvalContext& c) { c.color.x() = c.coords.x(); }
153 inline void evalCoordsPassthroughXY             (ShaderEvalContext& c) { c.color.xy() = c.coords.swizzle(0,1); }
154 inline void evalCoordsPassthroughXYZ    (ShaderEvalContext& c) { c.color.xyz() = c.coords.swizzle(0,1,2); }
155 inline void evalCoordsPassthrough               (ShaderEvalContext& c) { c.color = c.coords; }
156 inline void evalCoordsSwizzleWZYX               (ShaderEvalContext& c) { c.color = c.coords.swizzle(3,2,1,0); }
157
158 // ShaderEvaluator
159 // Either inherit a class with overridden evaluate() or just pass in an evalFunc.
160
161 class ShaderEvaluator
162 {
163 public:
164                                                         ShaderEvaluator                 (void);
165                                                         ShaderEvaluator                 (const ShaderEvalFunc evalFunc);
166         virtual                                 ~ShaderEvaluator                (void);
167
168         virtual void                    evaluate                                (ShaderEvalContext& ctx) const;
169
170 private:
171                                                         ShaderEvaluator                 (const ShaderEvaluator&);   // not allowed!
172         ShaderEvaluator&                operator=                               (const ShaderEvaluator&);   // not allowed!
173
174         const ShaderEvalFunc    m_evalFunc;
175 };
176
177 // UniformSetup
178
179 typedef void (*UniformSetupFunc) (ShaderRenderCaseInstance& instance, const tcu::Vec4& constCoords);
180
181 class UniformSetup
182 {
183 public:
184                                                         UniformSetup                    (void);
185                                                         UniformSetup                    (const UniformSetupFunc setup);
186         virtual                                 ~UniformSetup                   (void);
187         virtual void                    setup                                   (ShaderRenderCaseInstance& instance, const tcu::Vec4& constCoords) const;
188
189 private:
190                                                         UniformSetup                    (const UniformSetup&);  // not allowed!
191         UniformSetup&                   operator=                               (const UniformSetup&);  // not allowed!
192
193         const UniformSetupFunc  m_setupFunc;
194 };
195
196 typedef void (*AttributeSetupFunc) (ShaderRenderCaseInstance& instance, deUint32 numVertices);
197
198 class ShaderRenderCase : public vkt::TestCase
199 {
200 public:
201                                                                 ShaderRenderCase        (tcu::TestContext&                      testCtx,
202                                                                                                          const std::string&                     name,
203                                                                                                          const std::string&                     description,
204                                                                                                          const bool                                     isVertexCase,
205                                                                                                          const ShaderEvalFunc           evalFunc,
206                                                                                                          const UniformSetup*            uniformSetup,
207                                                                                                          const AttributeSetupFunc       attribFunc);
208
209                                                                 ShaderRenderCase        (tcu::TestContext&                      testCtx,
210                                                                                                          const std::string&                     name,
211                                                                                                          const std::string&                     description,
212                                                                                                          const bool                                     isVertexCase,
213                                                                                                          const ShaderEvaluator*         evaluator,
214                                                                                                          const UniformSetup*            uniformSetup,
215                                                                                                          const AttributeSetupFunc       attribFunc);
216
217
218         virtual                                         ~ShaderRenderCase       (void);
219         virtual void                            initPrograms            (vk::ProgramCollection<glu::ProgramSources>& programCollection) const;
220         virtual TestInstance*           createInstance          (Context& context) const;
221
222 protected:
223         std::string                                     m_vertShaderSource;
224         std::string                                     m_fragShaderSource;
225
226         const bool                                      m_isVertexCase;
227         const ShaderEvaluator*          m_evaluator;
228         const UniformSetup*                     m_uniformSetup;
229         const AttributeSetupFunc        m_attribFunc;
230 };
231
232
233 enum BaseUniformType
234 {
235 // Bool
236         UB_FALSE,
237         UB_TRUE,
238
239 // BVec4
240         UB4_FALSE,
241         UB4_TRUE,
242
243 // Integers
244         UI_ZERO,
245         UI_ONE,
246         UI_TWO,
247         UI_THREE,
248         UI_FOUR,
249         UI_FIVE,
250         UI_SIX,
251         UI_SEVEN,
252         UI_EIGHT,
253         UI_ONEHUNDREDONE,
254
255 // IVec2
256         UI2_MINUS_ONE,
257         UI2_ZERO,
258         UI2_ONE,
259         UI2_TWO,
260         UI2_THREE,
261         UI2_FOUR,
262         UI2_FIVE,
263
264 // IVec3
265         UI3_MINUS_ONE,
266         UI3_ZERO,
267         UI3_ONE,
268         UI3_TWO,
269         UI3_THREE,
270         UI3_FOUR,
271         UI3_FIVE,
272
273 // IVec4
274         UI4_MINUS_ONE,
275         UI4_ZERO,
276         UI4_ONE,
277         UI4_TWO,
278         UI4_THREE,
279         UI4_FOUR,
280         UI4_FIVE,
281
282 // Float
283         UF_ZERO,
284         UF_ONE,
285         UF_TWO,
286         UF_THREE,
287         UF_FOUR,
288         UF_FIVE,
289         UF_SIX,
290         UF_SEVEN,
291         UF_EIGHT,
292
293         UF_HALF,
294         UF_THIRD,
295         UF_FOURTH,
296         UF_FIFTH,
297         UF_SIXTH,
298         UF_SEVENTH,
299         UF_EIGHTH,
300
301 // Vec2
302         UV2_MINUS_ONE,
303         UV2_ZERO,
304         UV2_ONE,
305         UV2_TWO,
306         UV2_THREE,
307
308         UV2_HALF,
309
310 // Vec3
311         UV3_MINUS_ONE,
312         UV3_ZERO,
313         UV3_ONE,
314         UV3_TWO,
315         UV3_THREE,
316
317         UV3_HALF,
318
319 // Vec4
320         UV4_MINUS_ONE,
321         UV4_ZERO,
322         UV4_ONE,
323         UV4_TWO,
324         UV4_THREE,
325
326         UV4_HALF,
327
328         UV4_BLACK,
329         UV4_GRAY,
330         UV4_WHITE
331 };
332
333 enum BaseAttributeType
334 {
335 // User attributes
336         A_IN0,
337         A_IN1,
338         A_IN2,
339         A_IN3,
340
341 // Matrices
342         MAT2,
343         MAT2x3,
344         MAT2x4,
345         MAT3x2,
346         MAT3,
347         MAT3x4,
348         MAT4x2,
349         MAT4x3,
350         MAT4
351 };
352
353 // ShaderRenderCaseInstance.
354
355 class ShaderRenderCaseInstance : public vkt::TestInstance
356 {
357 public:
358                                                                                                                 ShaderRenderCaseInstance        (Context&                                       context,
359                                                                                                                                                                         const bool                                      isVertexCase,
360                                                                                                                                                                         const ShaderEvaluator&          evaluator,
361                                                                                                                                                                         const UniformSetup&                     uniformSetup,
362                                                                                                                                                                         const AttributeSetupFunc        attribFunc);
363
364         virtual                                                                                         ~ShaderRenderCaseInstance       (void);
365         virtual tcu::TestStatus                                                         iterate                                         (void);
366
367         void                                                                                            addAttribute                            (deUint32                       bindingLocation,
368                                                                                                                                                                         vk::VkFormat            format,
369                                                                                                                                                                         deUint32                        sizePerElement,
370                                                                                                                                                                         deUint32                        count,
371                                                                                                                                                                         const void*                     data);
372         void                                                                                            useAttribute                            (deUint32                       bindingLocation,
373                                                                                                                                                                         BaseAttributeType       type);
374
375         template<typename T>
376         void                                                                                            addUniform                                      (deUint32                               bindingLocation,
377                                                                                                                                                                         vk::VkDescriptorType    descriptorType,
378                                                                                                                                                                         const T&                                data);
379         void                                                                                            addUniform                                      (deUint32                               bindingLocation,
380                                                                                                                                                                         vk::VkDescriptorType    descriptorType,
381                                                                                                                                                                         deUint32                                dataSize,
382                                                                                                                                                                         const void*                             data);
383         void                                                                                            useUniform                                      (deUint32                               bindingLocation,
384                                                                                                                                                                         BaseUniformType                 type);
385         void                                                                                            useSampler2D                            (deUint32                               bindingLocation,
386                                                                                                                                                                         deUint32                                textureId);
387
388 protected:
389         virtual void                                                                            setup                                           (void);
390         virtual void                                                                            setupUniforms                           (const tcu::Vec4& constCoords);
391
392         const tcu::IVec2                                                                        getViewportSize                         (void) const;
393
394         std::vector<tcu::Mat4>                                                          m_userAttribTransforms;
395         const tcu::Vec4                                                                         m_clearColor;
396         std::vector<TextureBinding>                                                     m_textures;
397
398         vk::SimpleAllocator                                                                     m_memAlloc;
399
400 private:
401
402         void                                                                                            setupTextures                           (void);
403         vk::Move<vk::VkImage>                                                           createImage2D                           (const tcu::Texture2D& texture, const vk::VkFormat format);
404         void                                                                                            setupUniformData                        (deUint32 bindingLocation, deUint32 size, const void* dataPtr);
405         void                                                                                            setupDefaultInputs                      (const QuadGrid& quadGrid);
406
407         void                                                                                            render                                          (tcu::Surface& result, const QuadGrid& quadGrid);
408         void                                                                                            computeVertexReference          (tcu::Surface& result, const QuadGrid& quadGrid);
409         void                                                                                            computeFragmentReference        (tcu::Surface& result, const QuadGrid& quadGrid);
410         bool                                                                                            compareImages                           (const tcu::Surface&    resImage,
411                                                                                                                                                                          const tcu::Surface&    refImage,
412                                                                                                                                                                          float                                  errorThreshold);
413
414         const bool                                                                                      m_isVertexCase;
415         const ShaderEvaluator&                                                          m_evaluator;
416         const UniformSetup&                                                                     m_uniformSetup;
417         const AttributeSetupFunc                                                        m_attribFunc;
418
419         struct EnabledBaseAttribute
420         {
421                 deUint32                        location;
422                 BaseAttributeType       type;
423         };
424         std::vector<EnabledBaseAttribute>                                       m_enabledBaseAttributes;
425
426         const tcu::IVec2                                                                        m_renderSize;
427         const vk::VkFormat                                                                      m_colorFormat;
428
429         vk::Move<vk::VkImage>                                                           m_colorImage;
430         de::MovePtr<vk::Allocation>                                                     m_colorImageAlloc;
431         vk::Move<vk::VkAttachmentView>                                          m_colorAttachmentView;
432
433         vk::Move<vk::VkRenderPass>                                                      m_renderPass;
434         vk::Move<vk::VkFramebuffer>                                                     m_framebuffer;
435         vk::Move<vk::VkPipelineLayout>                                          m_pipelineLayout;
436         vk::Move<vk::VkPipeline>                                                        m_graphicsPipeline;
437
438         vk::Move<vk::VkShaderModule>                                            m_vertexShaderModule;
439         vk::Move<vk::VkShaderModule>                                            m_fragmentShaderModule;
440         vk::Move<vk::VkShader>                                                          m_vertexShader;
441         vk::Move<vk::VkShader>                                                          m_fragmentShader;
442
443         vk::Move<vk::VkDynamicViewportState>                            m_viewportState;
444         vk::Move<vk::VkDynamicRasterState>                                      m_rasterState;
445         vk::Move<vk::VkDynamicColorBlendState>                          m_colorBlendState;
446
447         vk::Move<vk::VkBuffer>                                                          m_indiceBuffer;
448         de::MovePtr<vk::Allocation>                                                     m_indiceBufferAlloc;
449
450         vk::Move<vk::VkDescriptorSetLayout>                                     m_descriptorSetLayout;
451
452         vk::Move<vk::VkDescriptorPool>                                          m_descriptorPool;
453         vk::Move<vk::VkDescriptorSet>                                           m_descriptorSet;
454
455         vk::Move<vk::VkCmdPool>                                                         m_cmdPool;
456         vk::Move<vk::VkCmdBuffer>                                                       m_cmdBuffer;
457
458         vk::Move<vk::VkFence>                                                           m_fence;
459
460         vk::DescriptorSetLayoutBuilder                                          m_descriptorSetLayoutBuilder;
461         vk::DescriptorPoolBuilder                                                       m_descriptorPoolBuilder;
462         vk::DescriptorSetUpdateBuilder                                          m_descriptorSetUpdateBuilder;
463
464         typedef de::SharedPtr<vk::Unique<vk::VkBuffer> >                VkBufferSp;
465         typedef de::SharedPtr<vk::Unique<vk::VkBufferView> >    VkBufferViewSp;
466
467         typedef de::SharedPtr<vk::Unique<vk::VkImage> >                 VkImageSp;
468         typedef de::SharedPtr<vk::Unique<vk::VkImageView> >             VkImageViewSp;
469         typedef de::SharedPtr<vk::Unique<vk::VkSampler> >               VkSamplerSp;
470         typedef de::SharedPtr<de::UniquePtr<vk::Allocation> >   AllocationSp;
471
472         class UniformInfo
473         {
474         public:
475                                                                         UniformInfo             (void) {}
476                 virtual                                         ~UniformInfo    (void) {}
477
478                 vk::VkDescriptorType            type;
479                 vk::VkDescriptorInfo            descriptor;
480                 deUint32                                        location;
481         };
482
483         class BufferUniform : public UniformInfo
484         {
485         public:
486                                                                         BufferUniform   (void) {}
487                 virtual                                         ~BufferUniform  (void) {}
488
489                 VkBufferSp                                      buffer;
490                 VkBufferViewSp                          bufferView;
491                 AllocationSp                            alloc;
492         };
493
494         class SamplerUniform : public UniformInfo
495         {
496         public:
497                                                                         SamplerUniform  (void) {}
498                 virtual                                         ~SamplerUniform (void) {}
499
500                 VkImageSp                                       image;
501                 VkImageViewSp                           imageView;
502                 VkSamplerSp                                     sampler;
503                 AllocationSp                            alloc;
504         };
505
506         typedef de::SharedPtr<de::UniquePtr<UniformInfo> >      UniformInfoSp;
507         std::vector<UniformInfoSp>                                                      m_uniformInfos;
508
509         std::vector<vk::VkVertexInputBindingDescription>        m_vertexBindingDescription;
510         std::vector<vk::VkVertexInputAttributeDescription>      m_vertexattributeDescription;
511
512         std::vector<VkBufferSp>                                                         m_vertexBuffers;
513         std::vector<AllocationSp>                                                       m_vertexBufferAllocs;
514 };
515
516 template<typename T>
517 void ShaderRenderCaseInstance::addUniform (deUint32 bindingLocation, vk::VkDescriptorType descriptorType, const T& data)
518 {
519         addUniform(bindingLocation, descriptorType, sizeof(T), &data);
520 }
521
522 } // shaderrendercase
523 } // vkt
524
525 #endif // _VKTSHADERRENDERCASE_HPP