Merge branch 'jekstrand_renderpass_transfer_bit_fix' into 'master'
[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         const void*                             getBufferPtr            (const deUint32 bindingLocation) const;
150
151 protected:
152                                                         ShaderExecutor          (const ShaderSpec& shaderSpec, glu::ShaderType shaderType);
153
154         void                                    addUniforms                     (const VkDevice& vkDevice, const DeviceInterface& vk, const deUint32 queueFamilyIndex, Allocator& memAlloc);
155
156         void                                    uploadUniforms          (DescriptorSetUpdateBuilder& descriptorSetUpdateBuilder, VkDescriptorSet descriptorSet);
157
158         class UniformInfo;
159         typedef de::SharedPtr<de::UniquePtr<UniformInfo> >                      UniformInfoSp;
160
161         class SamplerUniform;
162         typedef de::SharedPtr<de::UniquePtr<SamplerUniform> >           SamplerUniformSp;
163
164         typedef de::SharedPtr<Unique<VkBuffer> >                        VkBufferSp;
165         typedef de::SharedPtr<Unique<VkImage> >                         VkImageSp;
166         typedef de::SharedPtr<Unique<VkImageView> >                     VkImageViewSp;
167         typedef de::SharedPtr<Unique<VkSampler> >                       VkSamplerSp;
168         typedef de::SharedPtr<Allocation>                                       AllocationSp;
169
170         class UniformInfo
171         {
172         public:
173                                                                         UniformInfo                     (void) {}
174                 virtual                                         ~UniformInfo            (void) {}
175                 virtual bool                            isSamplerArray          (void) const { return false; }
176                 virtual bool                            isBufferUniform         (void) const { return false; }
177                 virtual bool                            isSamplerUniform        (void) const { return false; }
178
179                 VkDescriptorType                        type;
180                 deUint32                                        location;
181         };
182
183         class BufferUniform : public UniformInfo
184         {
185         public:
186                                                                         BufferUniform           (void) {}
187                 virtual                                         ~BufferUniform          (void) {}
188                 virtual bool                            isBufferUniform         (void) const { return true; }
189
190                 VkBufferSp                                      buffer;
191                 AllocationSp                            alloc;
192                 VkDescriptorBufferInfo          descriptor;
193         };
194
195         class SamplerUniform : public UniformInfo
196         {
197         public:
198                                                                         SamplerUniform          (void) {}
199                 virtual                                         ~SamplerUniform         (void) {}
200                 virtual bool                            isSamplerUniform        (void) const { return true; }
201                 VkImageSp                                       image;
202                 VkImageViewSp                           imageView;
203                 VkSamplerSp                                     sampler;
204                 AllocationSp                            alloc;
205                 VkDescriptorImageInfo           descriptor;
206         };
207
208         class SamplerArrayUniform : public UniformInfo
209         {
210         public:
211                                                                                         SamplerArrayUniform             (void) {}
212                 virtual                                                         ~SamplerArrayUniform    (void) {}
213                 virtual bool                                            isSamplerArray                  (void) const { return true; }
214
215                 std::vector<SamplerUniformSp>           uniforms;
216         };
217
218         Move<VkImage>                                                   createCombinedImage                     (const VkDevice&                                vkDevice,
219                                                                                                                                                  const DeviceInterface&                 vk,
220                                                                                                                                                  const deUint32                                 queueFamilyIndex,
221                                                                                                                                                  const tcu::IVec3&                              texSize,
222                                                                                                                                                  const VkFormat                                 format,
223                                                                                                                                                  const VkImageType                              imageType,
224                                                                                                                                                  const VkImageViewType                  imageViewType,
225                                                                                                                                                  const VkImageUsageFlags                usage,
226                                                                                                                                                  const VkImageTiling                    tiling);
227
228         de::MovePtr<Allocation>                                 uploadImage                                     (const VkDevice&                                vkDevice,
229                                                                                                                                                  const DeviceInterface&                 vk,
230                                                                                                                                                  Allocator&                                             memAlloc,
231                                                                                                                                                  const tcu::TextureFormat&              texFormat,
232                                                                                                                                                  const tcu::IVec3&                              texSize,
233                                                                                                                                                  const void*                                    data,
234                                                                                                                                                  const VkImage&                                 vkTexture,
235                                                                                                                                                  const VkImageAspectFlags               aspectMask);
236
237         de::MovePtr<SamplerUniform>                             createSamplerUniform            (const VkDevice&                                vkDevice,
238                                                                                                                                                  const DeviceInterface&                 vk,
239                                                                                                                                                  const deUint32                                 queueFamilyIndex,
240                                                                                                                                                  Allocator&                                             memAlloc,
241                                                                                                                                                  deUint32                                               bindingLocation,
242                                                                                                                                                  const tcu::Sampler&                    refSampler,
243                                                                                                                                                  const tcu::TextureFormat&              texFormat,
244                                                                                                                                                  const tcu::IVec3&                              texSize,
245                                                                                                                                                  VkImageType                                    imageType,
246                                                                                                                                                  VkImageViewType                                imageViewType,
247                                                                                                                                                  const void*                                    data);
248
249         const ShaderSpec                                                                        m_shaderSpec;
250         const glu::ShaderType                                                           m_shaderType;
251
252         std::vector<UniformInfoSp>                                                      m_uniformInfos;
253         de::MovePtr<const UniformSetup>                                         m_uniformSetup;
254         DescriptorSetLayoutBuilder                                                      m_descriptorSetLayoutBuilder;
255         DescriptorPoolBuilder                                                           m_descriptorPoolBuilder;
256
257 };
258
259 inline tcu::TestLog& operator<< (tcu::TestLog& log, const ShaderExecutor* executor) { executor->log(log); return log; }
260 inline tcu::TestLog& operator<< (tcu::TestLog& log, const ShaderExecutor& executor) { executor.log(log); return log; }
261
262 ShaderExecutor* createExecutor(glu::ShaderType shaderType, const ShaderSpec& shaderSpec);
263
264 class UniformDataBase
265 {
266 public:
267                                                         UniformDataBase         (deUint32 bindingLocation)
268                                                                                                         : m_bindingLocation             (bindingLocation)
269                                                                                                 {
270                                                                                                 }
271         virtual                                 ~UniformDataBase        (void) {}
272         virtual void                    setup                           (ShaderExecutor&, const VkDevice&, const DeviceInterface&, const deUint32, Allocator&) const = 0;
273
274 protected:
275         const deUint32                  m_bindingLocation;
276 };
277
278 template<typename T>
279 class UniformData : public UniformDataBase
280 {
281 public:
282                                                         UniformData                     (deUint32 bindingLocation, VkDescriptorType descriptorType, const T data);
283         virtual                                 ~UniformData            (void);
284         virtual void                    setup                           (ShaderExecutor& executor, const VkDevice& vkDevice, const DeviceInterface& vk, const deUint32 queueFamilyIndex, Allocator& memAlloc) const;
285
286 private:
287         VkDescriptorType                m_descriptorType;
288         T                                               m_data;
289 };
290
291 template<typename T>
292 UniformData<T>::UniformData (deUint32 bindingLocation, VkDescriptorType descriptorType, const T data)
293         : UniformDataBase               (bindingLocation)
294         , m_descriptorType              (descriptorType)
295         , m_data                                (data)
296 {
297 }
298
299 template<typename T>
300 UniformData<T>::~UniformData (void)
301 {
302 }
303
304 template<typename T>
305 void UniformData<T>::setup (ShaderExecutor& executor, const VkDevice& vkDevice, const DeviceInterface& vk, const deUint32 queueFamilyIndex, Allocator& memAlloc) const
306 {
307         executor.setupUniformData(vkDevice, vk, queueFamilyIndex, memAlloc, m_bindingLocation, m_descriptorType, sizeof(T), &m_data);
308 }
309
310 class SamplerUniformData : public UniformDataBase
311 {
312 public:
313                                                         SamplerUniformData      (deUint32                                               bindingLocation,
314                                                                                                  deUint32                                               numSamplers,
315                                                                                                  const tcu::Sampler&                    refSampler,
316                                                                                                  const tcu::TextureFormat&              texFormat,
317                                                                                                  const tcu::IVec3&                              texSize,
318                                                                                                  VkImageType                                    imageType,
319                                                                                                  VkImageViewType                                imageViewType,
320                                                                                                  const void*                                    data);
321         virtual                                 ~SamplerUniformData     (void);
322         virtual void                    setup                           (ShaderExecutor& executor, const VkDevice& vkDevice, const DeviceInterface& vk, const deUint32 queueFamilyIndex, Allocator& memAlloc) const;
323
324 private:
325         deUint32                                        m_numSamplers;
326         const tcu::Sampler                      m_refSampler;
327         const tcu::TextureFormat        m_texFormat;
328         const tcu::IVec3                        m_texSize;
329         VkImageType                                     m_imageType;
330         VkImageViewType                         m_imageViewType;
331         const void*                                     m_data;
332 };
333
334 } // shaderexecutor
335 } // vkt
336
337 #endif // _VKTSHADEREXECUTOR_HPP