dEQP-VK.renderpass: Set IMAGE_USAGE_TRANSFER_SRC_BIT when needed
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / shaderexecutor / vktShaderExecutor.hpp
1 #ifndef _VKTSHADEREXECUTOR_HPP
2 #define _VKTSHADEREXECUTOR_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 ShaderExecutor
36  *//*--------------------------------------------------------------------*/
37
38 #include "deSharedPtr.hpp"
39
40 #include "vktTestCase.hpp"
41 #include "vkMemUtil.hpp"
42 #include "vkBuilderUtil.hpp"
43
44 #include "gluVarType.hpp"
45
46 #include "tcuTexture.hpp"
47
48 #include <vector>
49
50 namespace vkt
51 {
52 namespace shaderexecutor
53 {
54
55 using namespace vk;
56
57 //! Shader input / output variable declaration.
58 struct Symbol
59 {
60         std::string                             name;           //!< Symbol name.
61         glu::VarType                    varType;        //!< Symbol type.
62
63         Symbol (void) {}
64         Symbol (const std::string& name_, const glu::VarType& varType_) : name(name_), varType(varType_) {}
65 };
66
67 //! Complete shader specification.
68 struct ShaderSpec
69 {
70         std::vector<Symbol>             inputs;
71         std::vector<Symbol>             outputs;
72         std::string                             globalDeclarations;     //!< These are placed into global scope. Can contain uniform declarations for example.
73         std::string                             source;                         //!< Source snippet to be executed.
74
75         ShaderSpec (void) {}
76 };
77
78 // UniformSetup
79
80 class UniformDataBase;
81 class ShaderExecutor;
82
83 typedef de::SharedPtr<de::UniquePtr<UniformDataBase> > UniformDataSp;
84
85 class UniformSetup
86 {
87 public:
88                                                                                 UniformSetup            (void) {}
89         virtual                                                         ~UniformSetup           (void) {}
90
91         void                                                            addData                         (UniformDataBase* uniformData)
92                                                                                 {
93                                                                                         m_uniforms.push_back(UniformDataSp(new de::UniquePtr<UniformDataBase>(uniformData)));
94                                                                                 }
95
96         const std::vector<UniformDataSp>&       uniforms                        (void) const
97                                                                                 {
98                                                                                         return m_uniforms;
99                                                                                 }
100
101 private:
102                                                                                 UniformSetup            (const UniformSetup&);  // not allowed!
103         UniformSetup&                                           operator=                       (const UniformSetup&);  // not allowed!
104
105         std::vector<UniformDataSp>                      m_uniforms;
106 };
107
108 //! Base class for shader executor.
109 class ShaderExecutor
110 {
111 public:
112         virtual                                 ~ShaderExecutor         (void);
113
114         //! Log executor details (program etc.).
115         virtual void                    log                                     (tcu::TestLog& log) const = 0;
116
117         //! Execute
118         virtual void                    execute                         (const Context& ctx, int numValues, const void* const* inputs, void* const* outputs) = 0;
119
120         virtual void                    setShaderSources        (SourceCollections& programCollection) const = 0;
121
122         void                                    setUniforms                     (const UniformSetup* uniformSetup)
123                                                                                                 {
124                                                                                                         m_uniformSetup = de::MovePtr<const UniformSetup>(uniformSetup);
125                                                                                                 };
126
127         void                                    setupUniformData        (const VkDevice&                        vkDevice,
128                                                                                                  const DeviceInterface&         vk,
129                                                                                                  const deUint32                         queueFamilyIndex,
130                                                                                                  Allocator&                                     memAlloc,
131                                                                                                  deUint32                                       bindingLocation,
132                                                                                                  VkDescriptorType                       descriptorType,
133                                                                                                  deUint32                                       size,
134                                                                                                  const void*                            dataPtr);
135
136         void                                    setupSamplerData        (const VkDevice&                        vkDevice,
137                                                                                                  const DeviceInterface&         vk,
138                                                                                                  const deUint32                         queueFamilyIndex,
139                                                                                                  Allocator&                                     memAlloc,
140                                                                                                  deUint32                                       bindingLocation,
141                                                                                                  deUint32                                       numSamplers,
142                                                                                                  const tcu::Sampler&            refSampler,
143                                                                                                  const tcu::TextureFormat&      texFormat,
144                                                                                                  const tcu::IVec3&                      texSize,
145                                                                                                  VkImageType                            imageType,
146                                                                                                  VkImageViewType                        imageViewType,
147                                                                                                  const void*                            data);
148
149 protected:
150                                                         ShaderExecutor          (const ShaderSpec& shaderSpec, glu::ShaderType shaderType);
151
152         void                                    addUniforms                     (const VkDevice& vkDevice, const DeviceInterface& vk, const deUint32 queueFamilyIndex, Allocator& memAlloc);
153
154         void                                    uploadUniforms          (DescriptorSetUpdateBuilder& descriptorSetUpdateBuilder, VkDescriptorSet descriptorSet);
155
156         class UniformInfo;
157         typedef de::SharedPtr<de::UniquePtr<UniformInfo> >                      UniformInfoSp;
158
159         class SamplerUniform;
160         typedef de::SharedPtr<de::UniquePtr<SamplerUniform> >           SamplerUniformSp;
161
162         typedef de::SharedPtr<Unique<VkBuffer> >                        VkBufferSp;
163         typedef de::SharedPtr<Unique<VkImage> >                         VkImageSp;
164         typedef de::SharedPtr<Unique<VkImageView> >                     VkImageViewSp;
165         typedef de::SharedPtr<Unique<VkSampler> >                       VkSamplerSp;
166         typedef de::SharedPtr<Allocation>                                       AllocationSp;
167
168         class UniformInfo
169         {
170         public:
171                                                                         UniformInfo                     (void) {}
172                 virtual                                         ~UniformInfo            (void) {}
173                 virtual bool                            isSamplerArray          (void) const { return false; }
174                 virtual bool                            isBufferUniform         (void) const { return false; }
175                 virtual bool                            isSamplerUniform        (void) const { return false; }
176
177                 VkDescriptorType                        type;
178                 deUint32                                        location;
179         };
180
181         class BufferUniform : public UniformInfo
182         {
183         public:
184                                                                         BufferUniform           (void) {}
185                 virtual                                         ~BufferUniform          (void) {}
186                 virtual bool                            isBufferUniform         (void) const { return true; }
187
188                 VkBufferSp                                      buffer;
189                 AllocationSp                            alloc;
190                 VkDescriptorBufferInfo          descriptor;
191         };
192
193         class SamplerUniform : public UniformInfo
194         {
195         public:
196                                                                         SamplerUniform          (void) {}
197                 virtual                                         ~SamplerUniform         (void) {}
198                 virtual bool                            isSamplerUniform        (void) const { return true; }
199                 VkImageSp                                       image;
200                 VkImageViewSp                           imageView;
201                 VkSamplerSp                                     sampler;
202                 AllocationSp                            alloc;
203                 VkDescriptorImageInfo           descriptor;
204         };
205
206         class SamplerArrayUniform : public UniformInfo
207         {
208         public:
209                                                                                         SamplerArrayUniform             (void) {}
210                 virtual                                                         ~SamplerArrayUniform    (void) {}
211                 virtual bool                                            isSamplerArray                  (void) const { return true; }
212
213                 std::vector<SamplerUniformSp>           uniforms;
214         };
215
216         Move<VkImage>                                                   createCombinedImage                     (const VkDevice&                                vkDevice,
217                                                                                                                                                  const DeviceInterface&                 vk,
218                                                                                                                                                  const deUint32                                 queueFamilyIndex,
219                                                                                                                                                  const tcu::IVec3&                              texSize,
220                                                                                                                                                  const VkFormat                                 format,
221                                                                                                                                                  const VkImageType                              imageType,
222                                                                                                                                                  const VkImageViewType                  imageViewType,
223                                                                                                                                                  const VkImageUsageFlags                usage,
224                                                                                                                                                  const VkImageTiling                    tiling);
225
226         de::MovePtr<Allocation>                                 uploadImage                                     (const VkDevice&                                vkDevice,
227                                                                                                                                                  const DeviceInterface&                 vk,
228                                                                                                                                                  Allocator&                                             memAlloc,
229                                                                                                                                                  const tcu::TextureFormat&              texFormat,
230                                                                                                                                                  const tcu::IVec3&                              texSize,
231                                                                                                                                                  const void*                                    data,
232                                                                                                                                                  const VkImage&                                 vkTexture,
233                                                                                                                                                  const VkImageAspectFlags               aspectMask);
234
235         de::MovePtr<SamplerUniform>                             createSamplerUniform            (const VkDevice&                                vkDevice,
236                                                                                                                                                  const DeviceInterface&                 vk,
237                                                                                                                                                  const deUint32                                 queueFamilyIndex,
238                                                                                                                                                  Allocator&                                             memAlloc,
239                                                                                                                                                  deUint32                                               bindingLocation,
240                                                                                                                                                  const tcu::Sampler&                    refSampler,
241                                                                                                                                                  const tcu::TextureFormat&              texFormat,
242                                                                                                                                                  const tcu::IVec3&                              texSize,
243                                                                                                                                                  VkImageType                                    imageType,
244                                                                                                                                                  VkImageViewType                                imageViewType,
245                                                                                                                                                  const void*                                    data);
246
247         const ShaderSpec                                                                        m_shaderSpec;
248         const glu::ShaderType                                                           m_shaderType;
249
250         std::vector<UniformInfoSp>                                                      m_uniformInfos;
251         de::MovePtr<const UniformSetup>                                         m_uniformSetup;
252         DescriptorSetLayoutBuilder                                                      m_descriptorSetLayoutBuilder;
253         DescriptorPoolBuilder                                                           m_descriptorPoolBuilder;
254
255 };
256
257 inline tcu::TestLog& operator<< (tcu::TestLog& log, const ShaderExecutor* executor) { executor->log(log); return log; }
258 inline tcu::TestLog& operator<< (tcu::TestLog& log, const ShaderExecutor& executor) { executor.log(log); return log; }
259
260 ShaderExecutor* createExecutor(glu::ShaderType shaderType, const ShaderSpec& shaderSpec);
261
262 class UniformDataBase
263 {
264 public:
265                                                         UniformDataBase         (deUint32 bindingLocation)
266                                                                                                         : m_bindingLocation             (bindingLocation)
267                                                                                                 {
268                                                                                                 }
269         virtual                                 ~UniformDataBase        (void) {}
270         virtual void                    setup                           (ShaderExecutor&, const VkDevice&, const DeviceInterface&, const deUint32, Allocator&) const = 0;
271
272 protected:
273         const deUint32                  m_bindingLocation;
274 };
275
276 template<typename T>
277 class UniformData : public UniformDataBase
278 {
279 public:
280                                                         UniformData                     (deUint32 bindingLocation, VkDescriptorType descriptorType, const T data);
281         virtual                                 ~UniformData            (void);
282         virtual void                    setup                           (ShaderExecutor& executor, const VkDevice& vkDevice, const DeviceInterface& vk, const deUint32 queueFamilyIndex, Allocator& memAlloc) const;
283
284 private:
285         VkDescriptorType                m_descriptorType;
286         T                                               m_data;
287 };
288
289 template<typename T>
290 UniformData<T>::UniformData (deUint32 bindingLocation, VkDescriptorType descriptorType, const T data)
291         : UniformDataBase               (bindingLocation)
292         , m_descriptorType              (descriptorType)
293         , m_data                                (data)
294 {
295 }
296
297 template<typename T>
298 UniformData<T>::~UniformData (void)
299 {
300 }
301
302 template<typename T>
303 void UniformData<T>::setup (ShaderExecutor& executor, const VkDevice& vkDevice, const DeviceInterface& vk, const deUint32 queueFamilyIndex, Allocator& memAlloc) const
304 {
305         executor.setupUniformData(vkDevice, vk, queueFamilyIndex, memAlloc, m_bindingLocation, m_descriptorType, sizeof(T), &m_data);
306 }
307
308 class SamplerUniformData : public UniformDataBase
309 {
310 public:
311                                                         SamplerUniformData      (deUint32                                               bindingLocation,
312                                                                                                  deUint32                                               numSamplers,
313                                                                                                  const tcu::Sampler&                    refSampler,
314                                                                                                  const tcu::TextureFormat&              texFormat,
315                                                                                                  const tcu::IVec3&                              texSize,
316                                                                                                  VkImageType                                    imageType,
317                                                                                                  VkImageViewType                                imageViewType,
318                                                                                                  const void*                                    data);
319         virtual                                 ~SamplerUniformData     (void);
320         virtual void                    setup                           (ShaderExecutor& executor, const VkDevice& vkDevice, const DeviceInterface& vk, const deUint32 queueFamilyIndex, Allocator& memAlloc) const;
321
322 private:
323         deUint32                                        m_numSamplers;
324         const tcu::Sampler                      m_refSampler;
325         const tcu::TextureFormat        m_texFormat;
326         const tcu::IVec3                        m_texSize;
327         VkImageType                                     m_imageType;
328         VkImageViewType                         m_imageViewType;
329         const void*                                     m_data;
330 };
331
332 } // shaderexecutor
333 } // vkt
334
335 #endif // _VKTSHADEREXECUTOR_HPP