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