Add the support to device connection via TCP/IP am: 4ccc9fd56c
[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  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  *//*!
23  * \file
24  * \brief Vulkan ShaderExecutor
25  *//*--------------------------------------------------------------------*/
26
27 #include "deSharedPtr.hpp"
28
29 #include "vktTestCase.hpp"
30 #include "vkMemUtil.hpp"
31 #include "vkBuilderUtil.hpp"
32
33 #include "gluVarType.hpp"
34
35 #include "tcuTexture.hpp"
36
37 #include <vector>
38
39 namespace vkt
40 {
41 namespace shaderexecutor
42 {
43
44 using namespace vk;
45
46 //! Shader input / output variable declaration.
47 struct Symbol
48 {
49         std::string                             name;           //!< Symbol name.
50         glu::VarType                    varType;        //!< Symbol type.
51
52         Symbol (void) {}
53         Symbol (const std::string& name_, const glu::VarType& varType_) : name(name_), varType(varType_) {}
54 };
55
56 //! Complete shader specification.
57 struct ShaderSpec
58 {
59         std::vector<Symbol>             inputs;
60         std::vector<Symbol>             outputs;
61         std::string                             globalDeclarations;     //!< These are placed into global scope. Can contain uniform declarations for example.
62         std::string                             source;                         //!< Source snippet to be executed.
63
64         ShaderSpec (void) {}
65 };
66
67 // UniformSetup
68
69 class UniformDataBase;
70 class ShaderExecutor;
71
72 typedef de::SharedPtr<de::UniquePtr<UniformDataBase> > UniformDataSp;
73
74 class UniformSetup
75 {
76 public:
77                                                                                 UniformSetup            (void) {}
78         virtual                                                         ~UniformSetup           (void) {}
79
80         void                                                            addData                         (UniformDataBase* uniformData)
81                                                                                 {
82                                                                                         m_uniforms.push_back(UniformDataSp(new de::UniquePtr<UniformDataBase>(uniformData)));
83                                                                                 }
84
85         const std::vector<UniformDataSp>&       uniforms                        (void) const
86                                                                                 {
87                                                                                         return m_uniforms;
88                                                                                 }
89
90 private:
91                                                                                 UniformSetup            (const UniformSetup&);  // not allowed!
92         UniformSetup&                                           operator=                       (const UniformSetup&);  // not allowed!
93
94         std::vector<UniformDataSp>                      m_uniforms;
95 };
96
97 //! Base class for shader executor.
98 class ShaderExecutor
99 {
100 public:
101         virtual                                 ~ShaderExecutor         (void);
102
103         //! Log executor details (program etc.).
104         virtual void                    log                                     (tcu::TestLog& log) const = 0;
105
106         //! Execute
107         virtual void                    execute                         (const Context& ctx, int numValues, const void* const* inputs, void* const* outputs) = 0;
108
109         virtual void                    setShaderSources        (SourceCollections& programCollection) const = 0;
110
111         void                                    setUniforms                     (const UniformSetup* uniformSetup)
112                                                                                                 {
113                                                                                                         m_uniformSetup = de::MovePtr<const UniformSetup>(uniformSetup);
114                                                                                                 };
115
116         void                                    setupUniformData        (const VkDevice&                        vkDevice,
117                                                                                                  const DeviceInterface&         vk,
118                                                                                                  const VkQueue                          queue,
119                                                                                                  const deUint32                         queueFamilyIndex,
120                                                                                                  Allocator&                                     memAlloc,
121                                                                                                  deUint32                                       bindingLocation,
122                                                                                                  VkDescriptorType                       descriptorType,
123                                                                                                  deUint32                                       size,
124                                                                                                  const void*                            dataPtr);
125
126         void                                    setupUniformArray       (const VkDevice&                        vkDevice,
127                                                                                                  const DeviceInterface&         vk,
128                                                                                                  const VkQueue                          queue,
129                                                                                                  const deUint32                         queueFamilyIndex,
130                                                                                                  Allocator&                                     memAlloc,
131                                                                                                  deUint32                                       bindingLocation,
132                                                                                                  VkDescriptorType                       descriptorType,
133                                                                                                  deUint32                                       arraySize,
134                                                                                                  deUint32                                       size,
135                                                                                                  const void*                            dataPtr);
136
137         void                                    setupSamplerData        (const VkDevice&                        vkDevice,
138                                                                                                  const DeviceInterface&         vk,
139                                                                                                  const VkQueue                          queue,
140                                                                                                  const deUint32                         queueFamilyIndex,
141                                                                                                  Allocator&                                     memAlloc,
142                                                                                                  deUint32                                       bindingLocation,
143                                                                                                  deUint32                                       numSamplers,
144                                                                                                  const tcu::Sampler&            refSampler,
145                                                                                                  const tcu::TextureFormat&      texFormat,
146                                                                                                  const tcu::IVec3&                      texSize,
147                                                                                                  VkImageType                            imageType,
148                                                                                                  VkImageViewType                        imageViewType,
149                                                                                                  const void*                            data);
150
151         void                                    addSamplerUniform       (deUint32                                       bindingLocation,
152                                                                                                  VkImageView                            imageView,
153                                                                                                  VkSampler                                      sampler);
154
155         const void*                             getBufferPtr            (const deUint32 bindingLocation) const;
156
157 protected:
158                                                         ShaderExecutor          (const ShaderSpec& shaderSpec, glu::ShaderType shaderType);
159
160         void                                    addUniforms                     (const VkDevice& vkDevice, const DeviceInterface& vk, const VkQueue queue, const deUint32 queueFamilyIndex, Allocator& memAlloc);
161
162         void                                    uploadUniforms          (DescriptorSetUpdateBuilder& descriptorSetUpdateBuilder, VkDescriptorSet descriptorSet);
163
164         class UniformInfo;
165         typedef de::SharedPtr<de::UniquePtr<UniformInfo> >                      UniformInfoSp;
166         class BufferUniform;
167         typedef de::SharedPtr<de::UniquePtr<BufferUniform> >            BufferUniformSp;
168         class SamplerUniform;
169         typedef de::SharedPtr<de::UniquePtr<SamplerUniform> >           SamplerUniformSp;
170
171         typedef de::SharedPtr<Unique<VkBuffer> >                        VkBufferSp;
172         typedef de::SharedPtr<Unique<VkImage> >                         VkImageSp;
173         typedef de::SharedPtr<Unique<VkImageView> >                     VkImageViewSp;
174         typedef de::SharedPtr<Unique<VkSampler> >                       VkSamplerSp;
175         typedef de::SharedPtr<Allocation>                                       AllocationSp;
176
177         class UniformInfo
178         {
179         public:
180                 enum UniformType
181                 {
182                         UNIFORM_TYPE_BUFFER = 0,
183                         UNIFORM_TYPE_SAMPLER,
184                         UNIFORM_TYPE_UNMANAGED_SAMPLER,
185                         UNIFORM_TYPE_BUFFER_ARRAY,
186                         UNIFORM_TYPE_SAMPLER_ARRAY,
187
188                         UNIFORM_TYPE_LAST
189                 };
190
191                                                                         UniformInfo                     (void) {}
192                 virtual                                         ~UniformInfo            (void) {}
193                 virtual UniformType                     getType                         (void) const = 0;
194
195                 VkDescriptorType                        type;
196                 deUint32                                        location;
197         };
198
199         class BufferUniform : public UniformInfo
200         {
201         public:
202                                                                         BufferUniform           (void) {}
203                 virtual                                         ~BufferUniform          (void) {}
204                 virtual UniformType                     getType                         (void) const { return UNIFORM_TYPE_BUFFER; }
205
206                 VkBufferSp                                      buffer;
207                 AllocationSp                            alloc;
208                 VkDescriptorBufferInfo          descriptor;
209         };
210
211         class SamplerUniform : public UniformInfo
212         {
213         public:
214                                                                         SamplerUniform          (void) {}
215                 virtual                                         ~SamplerUniform         (void) {}
216                 virtual UniformType                     getType                         (void) const { return UNIFORM_TYPE_SAMPLER; }
217
218                 VkImageSp                                       image;
219                 VkImageViewSp                           imageView;
220                 VkSamplerSp                                     sampler;
221                 AllocationSp                            alloc;
222                 VkDescriptorImageInfo           descriptor;
223         };
224
225         class UnmanagedSamplerUniform : public UniformInfo
226         {
227         public:
228                                                                 UnmanagedSamplerUniform  (void) {}
229                 virtual                                 ~UnmanagedSamplerUniform (void) {}
230                 virtual UniformType             getType                  (void) const { return UNIFORM_TYPE_UNMANAGED_SAMPLER; }
231
232                 VkImageView                             imageView;
233                 VkSampler                               sampler;
234                 VkDescriptorImageInfo   descriptor;
235         };
236
237         class BufferArrayUniform : public UniformInfo
238         {
239         public:
240                                                                                         BufferArrayUniform              (void) {}
241                 virtual                                                         ~BufferArrayUniform             (void) {}
242                 virtual UniformType                                     getType                                 (void) const { return UNIFORM_TYPE_BUFFER_ARRAY; }
243
244                 std::vector<BufferUniformSp>            uniforms;
245         };
246
247         class SamplerArrayUniform : public UniformInfo
248         {
249         public:
250                                                                                         SamplerArrayUniform             (void) {}
251                 virtual                                                         ~SamplerArrayUniform    (void) {}
252                 virtual UniformType                                     getType                                 (void) const { return UNIFORM_TYPE_SAMPLER_ARRAY; }
253
254                 std::vector<SamplerUniformSp>           uniforms;
255         };
256
257         void                                                                    uploadImage                                     (const VkDevice&                                vkDevice,
258                                                                                                                                                  const DeviceInterface&                 vk,
259                                                                                                                                                  const VkQueue                                  queue,
260                                                                                                                                                  const deUint32                                 queueFamilyIndex,
261                                                                                                                                                  Allocator&                                             memAlloc,
262                                                                                                                                                  const tcu::TextureFormat&              texFormat,
263                                                                                                                                                  const tcu::IVec3&                              texSize,
264                                                                                                                                                  const void*                                    data,
265                                                                                                                                                  const deUint32                                 arraySize,
266                                                                                                                                                  const VkImageAspectFlags               aspectMask,
267                                                                                                                                                  VkImage                                                destImage);
268
269         de::MovePtr<SamplerUniform>                             createSamplerUniform            (const VkDevice&                                vkDevice,
270                                                                                                                                                  const DeviceInterface&                 vk,
271                                                                                                                                                  const VkQueue                                  queue,
272                                                                                                                                                  const deUint32                                 queueFamilyIndex,
273                                                                                                                                                  Allocator&                                             memAlloc,
274                                                                                                                                                  deUint32                                               bindingLocation,
275                                                                                                                                                  const tcu::Sampler&                    refSampler,
276                                                                                                                                                  const tcu::TextureFormat&              texFormat,
277                                                                                                                                                  const tcu::IVec3&                              texSize,
278                                                                                                                                                  VkImageType                                    imageType,
279                                                                                                                                                  VkImageViewType                                imageViewType,
280                                                                                                                                                  const void*                                    data);
281
282         de::MovePtr<BufferUniform>                              createBufferUniform                     (const VkDevice&                                vkDevice,
283                                                                                                                                                  const DeviceInterface&                 vk,
284                                                                                                                                                  const VkQueue                                  queue,
285                                                                                                                                                  const deUint32                                 queueFamilyIndex,
286                                                                                                                                                  Allocator&                                             memAlloc,
287                                                                                                                                                  deUint32                                               bindingLocation,
288                                                                                                                                                  VkDescriptorType                               descriptorType,
289                                                                                                                                                  deUint32                                               size,
290                                                                                                                                                  const void*                                    dataPtr);
291
292         const ShaderSpec                                                                        m_shaderSpec;
293         const glu::ShaderType                                                           m_shaderType;
294
295         std::vector<UniformInfoSp>                                                      m_uniformInfos;
296         de::MovePtr<const UniformSetup>                                         m_uniformSetup;
297         DescriptorSetLayoutBuilder                                                      m_descriptorSetLayoutBuilder;
298         DescriptorPoolBuilder                                                           m_descriptorPoolBuilder;
299
300 };
301
302 inline tcu::TestLog& operator<< (tcu::TestLog& log, const ShaderExecutor* executor) { executor->log(log); return log; }
303 inline tcu::TestLog& operator<< (tcu::TestLog& log, const ShaderExecutor& executor) { executor.log(log); return log; }
304
305 ShaderExecutor* createExecutor(glu::ShaderType shaderType, const ShaderSpec& shaderSpec);
306
307 class UniformDataBase
308 {
309 public:
310                                                         UniformDataBase         (deUint32 bindingLocation)
311                                                                                                         : m_bindingLocation             (bindingLocation)
312                                                                                                 {
313                                                                                                 }
314         virtual                                 ~UniformDataBase        (void) {}
315         virtual void                    setup                           (ShaderExecutor&, const VkDevice&, const DeviceInterface&, const VkQueue, const deUint32, Allocator&) const = 0;
316
317 protected:
318         const deUint32                  m_bindingLocation;
319 };
320
321 template<typename T>
322 class UniformData : public UniformDataBase
323 {
324 public:
325                                                         UniformData                     (deUint32 bindingLocation, VkDescriptorType descriptorType, const T data);
326         virtual                                 ~UniformData            (void);
327         virtual void                    setup                           (ShaderExecutor& executor, const VkDevice& vkDevice, const DeviceInterface& vk, const VkQueue queue, const deUint32 queueFamilyIndex, Allocator& memAlloc) const;
328
329 private:
330         VkDescriptorType                m_descriptorType;
331         T                                               m_data;
332 };
333
334 template<typename T>
335 UniformData<T>::UniformData (deUint32 bindingLocation, VkDescriptorType descriptorType, const T data)
336         : UniformDataBase               (bindingLocation)
337         , m_descriptorType              (descriptorType)
338         , m_data                                (data)
339 {
340 }
341
342 template<typename T>
343 UniformData<T>::~UniformData (void)
344 {
345 }
346
347 template<typename T>
348 void UniformData<T>::setup (ShaderExecutor& executor, const VkDevice& vkDevice, const DeviceInterface& vk, const VkQueue queue, const deUint32 queueFamilyIndex, Allocator& memAlloc) const
349 {
350         executor.setupUniformData(vkDevice, vk, queue, queueFamilyIndex, memAlloc, m_bindingLocation, m_descriptorType, sizeof(T), &m_data);
351 }
352
353 template<typename T>
354 class UniformArrayData : public UniformDataBase
355 {
356 public:
357                                                         UniformArrayData        (deUint32 bindingLocation, VkDescriptorType descriptorType, const std::vector<T>& data);
358         virtual                                 ~UniformArrayData       (void);
359         virtual void                    setup                           (ShaderExecutor& executor, const VkDevice& vkDevice, const DeviceInterface& vk, const VkQueue queue, const deUint32 queueFamilyIndex, Allocator& memAlloc) const;
360
361 private:
362         VkDescriptorType                m_descriptorType;
363         std::vector<T>                  m_data;
364 };
365
366 template<typename T>
367 UniformArrayData<T>::UniformArrayData (deUint32 bindingLocation, VkDescriptorType descriptorType, const std::vector<T>& data)
368         : UniformDataBase               (bindingLocation)
369         , m_descriptorType              (descriptorType)
370         , m_data                                (data)
371 {
372 }
373
374 template<typename T>
375 UniformArrayData<T>::~UniformArrayData (void)
376 {
377 }
378
379 template<typename T>
380 void UniformArrayData<T>::setup (ShaderExecutor& executor, const VkDevice& vkDevice, const DeviceInterface& vk, const VkQueue queue, const deUint32 queueFamilyIndex, Allocator& memAlloc) const
381 {
382         DE_ASSERT(!m_data.empty());
383         executor.setupUniformArray(vkDevice, vk, queue, queueFamilyIndex, memAlloc, m_bindingLocation, m_descriptorType, (deUint32)m_data.size(), sizeof(T), &m_data[0]);
384 }
385
386 class SamplerUniformData : public UniformDataBase
387 {
388 public:
389                                                         SamplerUniformData      (deUint32                                               bindingLocation,
390                                                                                                  deUint32                                               numSamplers,
391                                                                                                  const tcu::Sampler&                    refSampler,
392                                                                                                  const tcu::TextureFormat&              texFormat,
393                                                                                                  const tcu::IVec3&                              texSize,
394                                                                                                  VkImageType                                    imageType,
395                                                                                                  VkImageViewType                                imageViewType,
396                                                                                                  const void*                                    data);
397         virtual                                 ~SamplerUniformData     (void);
398         virtual void                    setup                           (ShaderExecutor& executor, const VkDevice& vkDevice, const DeviceInterface& vk, const VkQueue queue, const deUint32 queueFamilyIndex, Allocator& memAlloc) const;
399
400 private:
401         deUint32                                        m_numSamplers;
402         const tcu::Sampler                      m_refSampler;
403         const tcu::TextureFormat        m_texFormat;
404         const tcu::IVec3                        m_texSize;
405         VkImageType                                     m_imageType;
406         VkImageViewType                         m_imageViewType;
407         const void*                                     m_data;
408 };
409
410 } // shaderexecutor
411 } // vkt
412
413 #endif // _VKTSHADEREXECUTOR_HPP