Cherry-pick SPIR-V assembly test improvements
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / spirv_assembly / vktSpvAsmComputeShaderTestUtil.hpp
1 #ifndef _VKTSPVASMCOMPUTESHADERTESTUTIL_HPP
2 #define _VKTSPVASMCOMPUTESHADERTESTUTIL_HPP
3 /*-------------------------------------------------------------------------
4  * Vulkan Conformance Tests
5  * ------------------------
6  *
7  * Copyright (c) 2015 Google Inc.
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  *//*!
22  * \file
23  * \brief Compute Shader Based Test Case Utility Structs/Functions
24  *//*--------------------------------------------------------------------*/
25
26 #include "deDefs.h"
27 #include "deFloat16.h"
28 #include "deSharedPtr.hpp"
29 #include "tcuTestLog.hpp"
30 #include "tcuVector.hpp"
31 #include "vkMemUtil.hpp"
32 #include "vktSpvAsmUtils.hpp"
33
34 #include <string>
35 #include <vector>
36 #include <map>
37
38 using namespace vk;
39
40 namespace vkt
41 {
42 namespace SpirVAssembly
43 {
44
45 typedef de::MovePtr<vk::Allocation>                     AllocationMp;
46 typedef de::SharedPtr<vk::Allocation>           AllocationSp;
47
48 /*--------------------------------------------------------------------*//*!
49  * \brief Abstract class for an input/output storage buffer object
50  *//*--------------------------------------------------------------------*/
51 class BufferInterface
52 {
53 public:
54         virtual                         ~BufferInterface        (void)                          {}
55
56         virtual size_t          getNumBytes                     (void) const = 0;
57         virtual const void*     data                            (void) const = 0;
58 };
59
60 typedef de::SharedPtr<BufferInterface>          BufferSp;
61
62 /*--------------------------------------------------------------------*//*!
63  * \brief Concrete class for an input/output storage buffer object
64  *//*--------------------------------------------------------------------*/
65 template<typename E>
66 class Buffer : public BufferInterface
67 {
68 public:
69                                                 Buffer                          (const std::vector<E>& elements)
70                                                         : m_elements(elements)
71                                                 {}
72
73         size_t                          getNumBytes                     (void) const            { return m_elements.size() * sizeof(E); }
74         const void*                     data                            (void) const            { return &m_elements.front(); }
75
76 private:
77         std::vector<E>          m_elements;
78 };
79
80 DE_STATIC_ASSERT(sizeof(tcu::Vec4) == 4 * sizeof(float));
81
82 typedef Buffer<float>           Float32Buffer;
83 typedef Buffer<deInt32>         Int32Buffer;
84 typedef Buffer<tcu::Vec4>       Vec4Buffer;
85
86 typedef bool (*ComputeVerifyIOFunc) (const std::vector<BufferSp>&               inputs,
87                                                                          const std::vector<AllocationSp>&       outputAllocations,
88                                                                          const std::vector<BufferSp>&           expectedOutputs,
89                                                                          tcu::TestLog&                                          log);
90
91 /*--------------------------------------------------------------------*//*!
92  * \brief Specification for a compute shader.
93  *
94  * This struct bundles SPIR-V assembly code, input and expected output
95  * together.
96  *//*--------------------------------------------------------------------*/
97 struct ComputeShaderSpec
98 {
99         std::string                                                             assembly;
100         std::string                                                             entryPoint;
101         std::vector<BufferSp>                                   inputs;
102         // Mapping from input index (in the inputs field) to the descriptor type.
103         std::map<deUint32, VkDescriptorType>    inputTypes;
104         std::vector<BufferSp>                                   outputs;
105         tcu::IVec3                                                              numWorkGroups;
106         std::vector<deUint32>                                   specConstants;
107         BufferSp                                                                pushConstants;
108         std::vector<std::string>                                extensions;
109         ExtensionFeatures                                               requestedExtensionFeatures;
110         qpTestResult                                                    failResult;
111         std::string                                                             failMessage;
112         // If null, a default verification will be performed by comparing the memory pointed to by outputAllocations
113         // and the contents of expectedOutputs. Otherwise the function pointed to by verifyIO will be called.
114         // If true is returned, then the test case is assumed to have passed, if false is returned, then the test
115         // case is assumed to have failed. Exact meaning of failure can be customized with failResult.
116         ComputeVerifyIOFunc                                             verifyIO;
117
118                                                                                         ComputeShaderSpec (void)
119                                                                                                 : entryPoint                                    ("main")
120                                                                                                 , pushConstants                                 (DE_NULL)
121                                                                                                 , requestedExtensionFeatures    ()
122                                                                                                 , failResult                                    (QP_TEST_RESULT_FAIL)
123                                                                                                 , failMessage                                   ("Output doesn't match with expected")
124                                                                                                 , verifyIO                                              (DE_NULL)
125                                                                                         {}
126 };
127
128 /*--------------------------------------------------------------------*//*!
129  * \brief Helper functions for SPIR-V assembly shared by various tests
130  *//*--------------------------------------------------------------------*/
131
132 const char* getComputeAsmShaderPreamble                         (void);
133 const char* getComputeAsmCommonTypes                            (void);
134
135 /*--------------------------------------------------------------------*//*!
136  * Declares two uniform variables (indata, outdata) of type
137  * "struct { float[] }". Depends on type "f32arr" (for "float[]").
138  *//*--------------------------------------------------------------------*/
139 const char* getComputeAsmInputOutputBuffer                      (void);
140 /*--------------------------------------------------------------------*//*!
141  * Declares buffer type and layout for uniform variables indata and
142  * outdata. Both of them are SSBO bounded to descriptor set 0.
143  * indata is at binding point 0, while outdata is at 1.
144  *//*--------------------------------------------------------------------*/
145 const char* getComputeAsmInputOutputBufferTraits        (void);
146
147 } // SpirVAssembly
148 } // vkt
149
150 #endif // _VKTSPVASMCOMPUTESHADERTESTUTIL_HPP