ShaderRenderCase: remove UniquePtr from the AllocationSp typedef and use UniquePtrs...
[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
53 namespace vkt
54 {
55 namespace shaderrendercase
56 {
57
58 class QuadGrid;
59 class ShaderRenderCaseInstance;
60
61 class TextureBinding
62 {
63 public:
64         enum Type
65         {
66                 TYPE_NONE = 0,
67                 TYPE_2D,
68                 TYPE_CUBE_MAP,
69                 TYPE_2D_ARRAY,
70                 TYPE_3D,
71
72                 TYPE_LAST
73         };
74
75                                                                                 TextureBinding          (const tcu::Archive&    archive,
76                                                                                                                         const char*                             filename,
77                                                                                                                         const Type                              type,
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; }
83
84 private:
85                                                                                 TextureBinding          (const TextureBinding&);        // not allowed!
86         TextureBinding&                                         operator=                       (const TextureBinding&);        // not allowed!
87
88         static de::MovePtr<tcu::Texture2D>      loadTexture2D           (const tcu::Archive& archive, const char* filename);
89
90         Type                                                            m_type;
91         tcu::Sampler                                            m_sampler;
92
93         union
94         {
95                 const tcu::Texture2D*   tex2D;
96         } m_binding;
97 };
98
99 typedef de::SharedPtr<TextureBinding> TextureBindingSp;
100
101 // ShaderEvalContext.
102
103 class ShaderEvalContext
104 {
105 public:
106         // Limits.
107         enum
108         {
109                 MAX_USER_ATTRIBS        = 4,
110                 MAX_TEXTURES            = 4
111         };
112
113         struct ShaderSampler
114         {
115                 tcu::Sampler                            sampler;
116                 const tcu::Texture2D*           tex2D;
117                 const tcu::TextureCube*         texCube;
118                 const tcu::Texture2DArray*      tex2DArray;
119                 const tcu::Texture3D*           tex3D;
120
121                 inline ShaderSampler (void)
122                         : tex2D         (DE_NULL)
123                         , texCube       (DE_NULL)
124                         , tex2DArray(DE_NULL)
125                         , tex3D         (DE_NULL)
126                 {
127                 }
128         };
129
130                                                         ShaderEvalContext               (const QuadGrid& quadGrid);
131                                                         ~ShaderEvalContext              (void);
132
133         void                                    reset                                   (float sx, float sy);
134
135         // Inputs.
136         tcu::Vec4                               coords;
137         tcu::Vec4                               unitCoords;
138         tcu::Vec4                               constCoords;
139
140         tcu::Vec4                               in[MAX_USER_ATTRIBS];
141         ShaderSampler                   textures[MAX_TEXTURES];
142
143         // Output.
144         tcu::Vec4                               color;
145         bool                                    isDiscarded;
146
147         // Functions.
148         inline void                             discard                                 (void)  { isDiscarded = true; }
149         tcu::Vec4                               texture2D                               (int unitNdx, const tcu::Vec2& coords);
150
151 private:
152         const QuadGrid&                 m_quadGrid;
153 };
154
155 typedef void (*ShaderEvalFunc) (ShaderEvalContext& c);
156
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); }
162
163 // ShaderEvaluator
164 // Either inherit a class with overridden evaluate() or just pass in an evalFunc.
165
166 class ShaderEvaluator
167 {
168 public:
169                                                         ShaderEvaluator                 (void);
170                                                         ShaderEvaluator                 (const ShaderEvalFunc evalFunc);
171         virtual                                 ~ShaderEvaluator                (void);
172
173         virtual void                    evaluate                                (ShaderEvalContext& ctx) const;
174
175 private:
176                                                         ShaderEvaluator                 (const ShaderEvaluator&);   // not allowed!
177         ShaderEvaluator&                operator=                               (const ShaderEvaluator&);   // not allowed!
178
179         const ShaderEvalFunc    m_evalFunc;
180 };
181
182 // UniformSetup
183
184 typedef void (*UniformSetupFunc) (ShaderRenderCaseInstance& instance, const tcu::Vec4& constCoords);
185
186 class UniformSetup
187 {
188 public:
189                                                         UniformSetup                    (void);
190                                                         UniformSetup                    (const UniformSetupFunc setup);
191         virtual                                 ~UniformSetup                   (void);
192         virtual void                    setup                                   (ShaderRenderCaseInstance& instance, const tcu::Vec4& constCoords) const;
193
194 private:
195                                                         UniformSetup                    (const UniformSetup&);  // not allowed!
196         UniformSetup&                   operator=                               (const UniformSetup&);  // not allowed!
197
198         const UniformSetupFunc  m_setupFunc;
199 };
200
201 typedef void (*AttributeSetupFunc) (ShaderRenderCaseInstance& instance, deUint32 numVertices);
202
203 class ShaderRenderCase : public vkt::TestCase
204 {
205 public:
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);
213
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);
221
222
223         virtual                                                                                 ~ShaderRenderCase       (void);
224         virtual void                                                                    initPrograms            (vk::SourceCollections& programCollection) const;
225         virtual TestInstance*                                                   createInstance          (Context& context) const;
226
227 protected:
228         std::string                                                                             m_vertShaderSource;
229         std::string                                                                             m_fragShaderSource;
230
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;
235 };
236
237
238 enum BaseUniformType
239 {
240 // Bool
241         UB_FALSE,
242         UB_TRUE,
243
244 // BVec4
245         UB4_FALSE,
246         UB4_TRUE,
247
248 // Integers
249         UI_ZERO,
250         UI_ONE,
251         UI_TWO,
252         UI_THREE,
253         UI_FOUR,
254         UI_FIVE,
255         UI_SIX,
256         UI_SEVEN,
257         UI_EIGHT,
258         UI_ONEHUNDREDONE,
259
260 // IVec2
261         UI2_MINUS_ONE,
262         UI2_ZERO,
263         UI2_ONE,
264         UI2_TWO,
265         UI2_THREE,
266         UI2_FOUR,
267         UI2_FIVE,
268
269 // IVec3
270         UI3_MINUS_ONE,
271         UI3_ZERO,
272         UI3_ONE,
273         UI3_TWO,
274         UI3_THREE,
275         UI3_FOUR,
276         UI3_FIVE,
277
278 // IVec4
279         UI4_MINUS_ONE,
280         UI4_ZERO,
281         UI4_ONE,
282         UI4_TWO,
283         UI4_THREE,
284         UI4_FOUR,
285         UI4_FIVE,
286
287 // Float
288         UF_ZERO,
289         UF_ONE,
290         UF_TWO,
291         UF_THREE,
292         UF_FOUR,
293         UF_FIVE,
294         UF_SIX,
295         UF_SEVEN,
296         UF_EIGHT,
297
298         UF_HALF,
299         UF_THIRD,
300         UF_FOURTH,
301         UF_FIFTH,
302         UF_SIXTH,
303         UF_SEVENTH,
304         UF_EIGHTH,
305
306 // Vec2
307         UV2_MINUS_ONE,
308         UV2_ZERO,
309         UV2_ONE,
310         UV2_TWO,
311         UV2_THREE,
312
313         UV2_HALF,
314
315 // Vec3
316         UV3_MINUS_ONE,
317         UV3_ZERO,
318         UV3_ONE,
319         UV3_TWO,
320         UV3_THREE,
321
322         UV3_HALF,
323
324 // Vec4
325         UV4_MINUS_ONE,
326         UV4_ZERO,
327         UV4_ONE,
328         UV4_TWO,
329         UV4_THREE,
330
331         UV4_HALF,
332
333         UV4_BLACK,
334         UV4_GRAY,
335         UV4_WHITE
336 };
337
338 enum BaseAttributeType
339 {
340 // User attributes
341         A_IN0,
342         A_IN1,
343         A_IN2,
344         A_IN3,
345
346 // Matrices
347         MAT2,
348         MAT2x3,
349         MAT2x4,
350         MAT3x2,
351         MAT3,
352         MAT3x4,
353         MAT4x2,
354         MAT4x3,
355         MAT4
356 };
357
358 // ShaderRenderCaseInstance.
359
360 class ShaderRenderCaseInstance : public vkt::TestInstance
361 {
362 public:
363                                                                                                                 ShaderRenderCaseInstance        (Context&                                       context,
364                                                                                                                                                                         const bool                                      isVertexCase,
365                                                                                                                                                                         const ShaderEvaluator&          evaluator,
366                                                                                                                                                                         const UniformSetup&                     uniformSetup,
367                                                                                                                                                                         const AttributeSetupFunc        attribFunc);
368
369         virtual                                                                                         ~ShaderRenderCaseInstance       (void);
370         virtual tcu::TestStatus                                                         iterate                                         (void);
371
372         void                                                                                            addAttribute                            (deUint32                       bindingLocation,
373                                                                                                                                                                         vk::VkFormat            format,
374                                                                                                                                                                         deUint32                        sizePerElement,
375                                                                                                                                                                         deUint32                        count,
376                                                                                                                                                                         const void*                     data);
377         void                                                                                            useAttribute                            (deUint32                       bindingLocation,
378                                                                                                                                                                         BaseAttributeType       type);
379
380         template<typename T>
381         void                                                                                            addUniform                                      (deUint32                               bindingLocation,
382                                                                                                                                                                         vk::VkDescriptorType    descriptorType,
383                                                                                                                                                                         const T&                                data);
384         void                                                                                            addUniform                                      (deUint32                               bindingLocation,
385                                                                                                                                                                         vk::VkDescriptorType    descriptorType,
386                                                                                                                                                                         deUint32                                dataSize,
387                                                                                                                                                                         const void*                             data);
388         void                                                                                            useUniform                                      (deUint32                               bindingLocation,
389                                                                                                                                                                         BaseUniformType                 type);
390         void                                                                                            useSampler2D                            (deUint32                               bindingLocation,
391                                                                                                                                                                         deUint32                                textureId);
392
393 protected:
394         virtual void                                                                            setup                                           (void);
395         virtual void                                                                            setupUniforms                           (const tcu::Vec4& constCoords);
396
397         const tcu::IVec2                                                                        getViewportSize                         (void) const;
398
399         std::vector<tcu::Mat4>                                                          m_userAttribTransforms;
400         const tcu::Vec4                                                                         m_clearColor;
401         std::vector<TextureBindingSp>                                           m_textures;
402
403         vk::Allocator&                                                                          m_memAlloc;
404
405 private:
406
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,
416                                                                                                                                                                          deInt32                        width,
417                                                                                                                                                                          deInt32                        height);
418
419         void                                                                                            setupUniformData                        (deUint32 bindingLocation, deUint32 size, const void* dataPtr);
420         void                                                                                            setupDefaultInputs                      (const QuadGrid& quadGrid);
421
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);
428
429         const bool                                                                                      m_isVertexCase;
430         const ShaderEvaluator&                                                          m_evaluator;
431         const UniformSetup&                                                                     m_uniformSetup;
432         const AttributeSetupFunc                                                        m_attribFunc;
433
434         struct EnabledBaseAttribute
435         {
436                 deUint32                        location;
437                 BaseAttributeType       type;
438         };
439         std::vector<EnabledBaseAttribute>                                       m_enabledBaseAttributes;
440
441         const tcu::IVec2                                                                        m_renderSize;
442         const vk::VkFormat                                                                      m_colorFormat;
443
444         vk::Move<vk::VkImage>                                                           m_colorImage;
445         de::MovePtr<vk::Allocation>                                                     m_colorImageAlloc;
446         vk::Move<vk::VkImageView>                                                       m_colorImageView;
447
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;
452
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;
457
458         vk::Move<vk::VkBuffer>                                                          m_indiceBuffer;
459         de::MovePtr<vk::Allocation>                                                     m_indiceBufferAlloc;
460
461         vk::Move<vk::VkDescriptorSetLayout>                                     m_descriptorSetLayout;
462
463         vk::Move<vk::VkDescriptorPool>                                          m_descriptorPool;
464         vk::Move<vk::VkDescriptorSet>                                           m_descriptorSet;
465
466         vk::Move<vk::VkCmdPool>                                                         m_cmdPool;
467         vk::Move<vk::VkCmdBuffer>                                                       m_cmdBuffer;
468
469         vk::Move<vk::VkFence>                                                           m_fence;
470
471         vk::DescriptorSetLayoutBuilder                                          m_descriptorSetLayoutBuilder;
472         vk::DescriptorPoolBuilder                                                       m_descriptorPoolBuilder;
473         vk::DescriptorSetUpdateBuilder                                          m_descriptorSetUpdateBuilder;
474
475         typedef de::SharedPtr<vk::Unique<vk::VkBuffer> >                VkBufferSp;
476         typedef de::SharedPtr<vk::Unique<vk::VkBufferView> >    VkBufferViewSp;
477
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;
482
483         class UniformInfo
484         {
485         public:
486                                                                         UniformInfo             (void) {}
487                 virtual                                         ~UniformInfo    (void) {}
488
489                 vk::VkDescriptorType            type;
490                 vk::VkDescriptorInfo            descriptor;
491                 deUint32                                        location;
492         };
493
494         class BufferUniform : public UniformInfo
495         {
496         public:
497                                                                         BufferUniform   (void) {}
498                 virtual                                         ~BufferUniform  (void) {}
499
500                 VkBufferSp                                      buffer;
501                 VkBufferViewSp                          bufferView;
502                 AllocationSp                            alloc;
503         };
504
505         class SamplerUniform : public UniformInfo
506         {
507         public:
508                                                                         SamplerUniform  (void) {}
509                 virtual                                         ~SamplerUniform (void) {}
510
511                 VkImageSp                                       image;
512                 VkImageViewSp                           imageView;
513                 VkSamplerSp                                     sampler;
514                 AllocationSp                            alloc;
515         };
516
517         typedef de::SharedPtr<de::UniquePtr<UniformInfo> >      UniformInfoSp;
518         std::vector<UniformInfoSp>                                                      m_uniformInfos;
519
520         std::vector<vk::VkVertexInputBindingDescription>        m_vertexBindingDescription;
521         std::vector<vk::VkVertexInputAttributeDescription>      m_vertexattributeDescription;
522
523         std::vector<VkBufferSp>                                                         m_vertexBuffers;
524         std::vector<AllocationSp>                                                       m_vertexBufferAllocs;
525 };
526
527 template<typename T>
528 void ShaderRenderCaseInstance::addUniform (deUint32 bindingLocation, vk::VkDescriptorType descriptorType, const T& data)
529 {
530         addUniform(bindingLocation, descriptorType, sizeof(T), &data);
531 }
532
533 } // shaderrendercase
534 } // vkt
535
536 #endif // _VKTSHADERRENDERCASE_HPP