Refactor: Compatible compute and graphics VerifyIO
[platform/upstream/VK-GL-CTS.git] / external / vulkancts / modules / vulkan / spirv_assembly / vktSpvAsmComputeShaderTestUtil.cpp
1 /*-------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2015 Google Inc.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  *//*!
20  * \file
21  * \brief Compute Shader Based Test Case Utility Structs/Functions
22  *//*--------------------------------------------------------------------*/
23
24 #include "vktSpvAsmComputeShaderTestUtil.hpp"
25
26 namespace vkt
27 {
28 namespace SpirVAssembly
29 {
30 namespace
31 {
32 bool verifyOutputWithEpsilon (const std::vector<AllocationSp>& outputAllocs, const std::vector<Resource>& expectedOutputs, tcu::TestLog& log, const float epsilon)
33 {
34         DE_ASSERT(outputAllocs.size() != 0);
35         DE_ASSERT(outputAllocs.size() == expectedOutputs.size());
36
37         for (size_t outputNdx = 0; outputNdx < outputAllocs.size(); ++outputNdx)
38         {
39                 std::vector<deUint8>    expectedBytes;
40                 expectedOutputs[outputNdx].getBytes(expectedBytes);
41
42                 std::vector<float>      expectedFloats  (expectedBytes.size() / sizeof (float));
43                 std::vector<float>      actualFloats    (expectedBytes.size() / sizeof (float));
44
45                 memcpy(&expectedFloats[0], &expectedBytes.front(), expectedBytes.size());
46                 memcpy(&actualFloats[0], outputAllocs[outputNdx]->getHostPtr(), expectedBytes.size());
47                 for (size_t floatNdx = 0; floatNdx < actualFloats.size(); ++floatNdx)
48                 {
49                         // Use custom epsilon because of the float->string conversion
50                         if (fabs(expectedFloats[floatNdx] - actualFloats[floatNdx]) > epsilon)
51                         {
52                                 log << tcu::TestLog::Message << "Error: The actual and expected values not matching."
53                                         << " Expected: " << expectedFloats[floatNdx] << " Actual: " << actualFloats[floatNdx] << " Epsilon: " << epsilon << tcu::TestLog::EndMessage;
54                                 return false;
55                         }
56                 }
57         }
58         return true;
59 }
60 }
61
62 const char* getComputeAsmShaderPreamble (void)
63 {
64         return
65                 "OpCapability Shader\n"
66                 "OpMemoryModel Logical GLSL450\n"
67                 "OpEntryPoint GLCompute %main \"main\" %id\n"
68                 "OpExecutionMode %main LocalSize 1 1 1\n";
69 }
70
71 const char* getComputeAsmShaderPreambleWithoutLocalSize (void)
72 {
73         return
74                 "OpCapability Shader\n"
75                 "OpMemoryModel Logical GLSL450\n"
76                 "OpEntryPoint GLCompute %main \"main\" %id\n";
77 }
78
79 std::string getComputeAsmCommonTypes (std::string blockStorageClass)
80 {
81         return std::string(
82                 "%bool      = OpTypeBool\n"
83                 "%void      = OpTypeVoid\n"
84                 "%voidf     = OpTypeFunction %void\n"
85                 "%u32       = OpTypeInt 32 0\n"
86                 "%i32       = OpTypeInt 32 1\n"
87                 "%f32       = OpTypeFloat 32\n"
88                 "%uvec3     = OpTypeVector %u32 3\n"
89                 "%fvec3     = OpTypeVector %f32 3\n"
90                 "%uvec3ptr  = OpTypePointer Input %uvec3\n") +
91                 "%i32ptr    = OpTypePointer " + blockStorageClass + " %i32\n"
92                 "%f32ptr    = OpTypePointer " + blockStorageClass + " %f32\n"
93                 "%i32arr    = OpTypeRuntimeArray %i32\n"
94                 "%f32arr    = OpTypeRuntimeArray %f32\n";
95 }
96
97 const char* getComputeAsmCommonInt64Types (void)
98 {
99         return
100                 "%i64       = OpTypeInt 64 1\n"
101                 "%i64ptr    = OpTypePointer Uniform %i64\n"
102                 "%i64arr    = OpTypeRuntimeArray %i64\n";
103 }
104
105 const char* getComputeAsmInputOutputBuffer (void)
106 {
107         return
108                 "%buf     = OpTypeStruct %f32arr\n"
109                 "%bufptr  = OpTypePointer Uniform %buf\n"
110                 "%indata    = OpVariable %bufptr Uniform\n"
111                 "%outdata   = OpVariable %bufptr Uniform\n";
112 }
113
114 const char* getComputeAsmInputOutputBufferTraits (void)
115 {
116         return
117                 "OpDecorate %buf BufferBlock\n"
118                 "OpDecorate %indata DescriptorSet 0\n"
119                 "OpDecorate %indata Binding 0\n"
120                 "OpDecorate %outdata DescriptorSet 0\n"
121                 "OpDecorate %outdata Binding 1\n"
122                 "OpDecorate %f32arr ArrayStride 4\n"
123                 "OpMemberDecorate %buf 0 Offset 0\n";
124 }
125
126 bool verifyOutput (const std::vector<Resource>&, const std::vector<AllocationSp>& outputAllocs, const std::vector<Resource>& expectedOutputs, tcu::TestLog& log)
127 {
128         const float     epsilon = 0.001f;
129         return verifyOutputWithEpsilon(outputAllocs, expectedOutputs, log, epsilon);
130 }
131
132 } // SpirVAssembly
133 } // vkt