Fix shader bugs in OOB tests am: cf65c56fb4 am: baf5ae40a9 am: afc0b6a272
[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<deFloat16>       Float16Buffer;
84 typedef Buffer<deInt64>         Int64Buffer;
85 typedef Buffer<deInt32>         Int32Buffer;
86 typedef Buffer<deInt16>         Int16Buffer;
87 typedef Buffer<tcu::Vec4>       Vec4Buffer;
88
89 typedef bool (*ComputeVerifyIOFunc) (const std::vector<BufferSp>&               inputs,
90                                                                          const std::vector<AllocationSp>&       outputAllocations,
91                                                                          const std::vector<BufferSp>&           expectedOutputs,
92                                                                          tcu::TestLog&                                          log);
93
94 /*--------------------------------------------------------------------*//*!
95  * \brief Specification for a compute shader.
96  *
97  * This struct bundles SPIR-V assembly code, input and expected output
98  * together.
99  *//*--------------------------------------------------------------------*/
100 struct ComputeShaderSpec
101 {
102         std::string                                                             assembly;
103         std::string                                                             entryPoint;
104         std::vector<BufferSp>                                   inputs;
105         // Mapping from input index (in the inputs field) to the descriptor type.
106         std::map<deUint32, VkDescriptorType>    inputTypes;
107         std::vector<BufferSp>                                   outputs;
108         tcu::IVec3                                                              numWorkGroups;
109         std::vector<deUint32>                                   specConstants;
110         BufferSp                                                                pushConstants;
111         std::vector<std::string>                                extensions;
112         VulkanFeatures                                                  requestedVulkanFeatures;
113         qpTestResult                                                    failResult;
114         std::string                                                             failMessage;
115         // If null, a default verification will be performed by comparing the memory pointed to by outputAllocations
116         // and the contents of expectedOutputs. Otherwise the function pointed to by verifyIO will be called.
117         // If true is returned, then the test case is assumed to have passed, if false is returned, then the test
118         // case is assumed to have failed. Exact meaning of failure can be customized with failResult.
119         ComputeVerifyIOFunc                                             verifyIO;
120
121                                                                                         ComputeShaderSpec (void)
122                                                                                                 : entryPoint                                    ("main")
123                                                                                                 , pushConstants                                 (DE_NULL)
124                                                                                                 , requestedVulkanFeatures               ()
125                                                                                                 , failResult                                    (QP_TEST_RESULT_FAIL)
126                                                                                                 , failMessage                                   ("Output doesn't match with expected")
127                                                                                                 , verifyIO                                              (DE_NULL)
128                                                                                         {}
129 };
130
131 /*--------------------------------------------------------------------*//*!
132  * \brief Helper functions for SPIR-V assembly shared by various tests
133  *//*--------------------------------------------------------------------*/
134
135 const char* getComputeAsmShaderPreamble                         (void);
136 const char* getComputeAsmCommonTypes                            (void);
137 const char*     getComputeAsmCommonInt64Types                   (void);
138
139 /*--------------------------------------------------------------------*//*!
140  * Declares two uniform variables (indata, outdata) of type
141  * "struct { float[] }". Depends on type "f32arr" (for "float[]").
142  *//*--------------------------------------------------------------------*/
143 const char* getComputeAsmInputOutputBuffer                      (void);
144 /*--------------------------------------------------------------------*//*!
145  * Declares buffer type and layout for uniform variables indata and
146  * outdata. Both of them are SSBO bounded to descriptor set 0.
147  * indata is at binding point 0, while outdata is at 1.
148  *//*--------------------------------------------------------------------*/
149 const char* getComputeAsmInputOutputBufferTraits        (void);
150
151 } // SpirVAssembly
152 } // vkt
153
154 #endif // _VKTSPVASMCOMPUTESHADERTESTUTIL_HPP