1 /*------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2017 The Khronos Group Inc.
6  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  *//*!
21  * \file
22  * \brief Protected content buffer validator helper
23  *//*--------------------------------------------------------------------*/
24 
25 #include "vktProtectedMemBufferValidator.hpp"
26 
27 #include "tcuTestLog.hpp"
28 
29 #include "vkBuilderUtil.hpp"
30 #include "vkPrograms.hpp"
31 #include "vkTypeUtil.hpp"
32 #include "vktTestCase.hpp"
33 #include "vktTestGroupUtil.hpp"
34 #include "tcuStringTemplate.hpp"
35 
36 #include "vktProtectedMemUtils.hpp"
37 
38 namespace vkt
39 {
40 namespace ProtectedMem
41 {
42 namespace
43 {
44 
generateShaderVarString(TestType testType)45 const char *generateShaderVarString(TestType testType)
46 {
47     switch (testType)
48     {
49     case TYPE_UINT:
50         return "uvec4";
51     case TYPE_INT:
52         return "ivec4";
53     case TYPE_FLOAT:
54         return "vec4";
55 
56     default:
57         DE_FATAL("Incorrect vector type format");
58         return "";
59     }
60 }
61 
generateShaderBufferString(TestType testType,BufferType bufferType)62 const char *generateShaderBufferString(TestType testType, BufferType bufferType)
63 {
64     if (bufferType == STORAGE_BUFFER)
65         return "buffer";
66 
67     DE_ASSERT(bufferType == SAMPLER_BUFFER);
68 
69     switch (testType)
70     {
71     case TYPE_UINT:
72         return "uniform usamplerBuffer";
73     case TYPE_INT:
74         return "uniform isamplerBuffer";
75     case TYPE_FLOAT:
76         return "uniform samplerBuffer";
77 
78     default:
79         DE_FATAL("Incorrect sampler buffer format");
80         return "";
81     }
82 }
83 
84 } // namespace
85 
initBufferValidatorPrograms(vk::SourceCollections & programCollection,TestType testType,BufferType bufferType)86 void initBufferValidatorPrograms(vk::SourceCollections &programCollection, TestType testType, BufferType bufferType)
87 {
88     // Layout:
89     //  set = 0, location = 0 -> buffer|uniform usamplerBuffer|uniform isamplerBuffer|uniform samplerBuffersampler2D u_protectedBuffer
90     //  set = 0, location = 1 -> buffer ProtectedHelper (2 * uint)
91     //  set = 0, location = 2 -> uniform Data (2 * vec2 + 4 * vec4|ivec4|uvec4)
92     const char *validatorShaderTemplateSamplerBuffer =
93         "#version 450\n"
94         "layout(local_size_x = 1) in;\n"
95         "\n"
96         "layout(set=0, binding=0) ${BUFFER_TYPE} u_protectedBuffer;\n"
97         "\n"
98         "layout(set=0, binding=1) buffer ProtectedHelper\n"
99         "{\n"
100         "    highp uint zero; // set to 0\n"
101         "    highp uint unusedOut;\n"
102         "} helper;\n"
103         "\n"
104         "layout(set=0, binding=2) uniform Data\n"
105         "{\n"
106         "    highp ivec4 protectedBufferPosition[4];\n"
107         "    highp ${VAR_TYPE} protectedBufferRef[4];\n"
108         "};\n"
109         "\n"
110         "void error ()\n"
111         "{\n"
112         "    for (uint x = 0; x < 10; x += helper.zero)\n"
113         "        atomicAdd(helper.unusedOut, 1u);\n"
114         "}\n"
115         "\n"
116         "bool compare (${VAR_TYPE} a, ${VAR_TYPE} b, float threshold)\n"
117         "{\n"
118         "    return all(lessThanEqual(abs(a - b), ${VAR_TYPE}(threshold)));\n"
119         "}\n"
120         "\n"
121         "void main (void)\n"
122         "{\n"
123         "    float threshold = 0.1;\n"
124         "    for (uint i = 0; i < 4; i++)\n"
125         "    {\n"
126         "        ${VAR_TYPE} v = texelFetch(u_protectedBuffer, protectedBufferPosition[i].x);\n"
127         "        if (!compare(v, protectedBufferRef[i], threshold))\n"
128         "            error();\n"
129         "    }\n"
130         "}\n";
131 
132     const char *validatorShaderTemplateStorageBuffer =
133         "#version 450\n"
134         "layout(local_size_x = 1) in;\n"
135         "\n"
136         "layout(set=0, binding=0) ${BUFFER_TYPE} u_protectedBuffer\n"
137         "{\n"
138         "    highp ${VAR_TYPE} protectedTestValues;\n"
139         "} testBuffer;\n"
140         "\n"
141         "layout(set=0, binding=1) buffer ProtectedHelper\n"
142         "{\n"
143         "    highp uint zero; // set to 0\n"
144         "    highp uint unusedOut;\n"
145         "} helper;\n"
146         "\n"
147         "layout(set=0, binding=2) uniform Data\n"
148         "{\n"
149         "    highp ${VAR_TYPE} protectedReferenceValues;\n"
150         "};\n"
151         "\n"
152         "void error ()\n"
153         "{\n"
154         "    for (uint x = 0; x < 10; x += helper.zero)\n"
155         "        atomicAdd(helper.unusedOut, 1u);\n"
156         "}\n"
157         "\n"
158         "bool compare (${VAR_TYPE} a, ${VAR_TYPE} b, float threshold)\n"
159         "{\n"
160         "    return all(lessThanEqual(abs(a - b), ${VAR_TYPE}(threshold)));\n"
161         "}\n"
162         "\n"
163         "void main (void)\n"
164         "{\n"
165         "    float threshold = 0.1;\n"
166         "    if (!compare(testBuffer.protectedTestValues, protectedReferenceValues, threshold))\n"
167         "        error();\n"
168         "}\n";
169 
170     tcu::StringTemplate validatorShaderTemplate(bufferType == SAMPLER_BUFFER ? validatorShaderTemplateSamplerBuffer :
171                                                                                validatorShaderTemplateStorageBuffer);
172 
173     std::map<std::string, std::string> validatorParams;
174     validatorParams["VAR_TYPE"]    = generateShaderVarString(testType);
175     validatorParams["BUFFER_TYPE"] = generateShaderBufferString(testType, bufferType);
176     std::string validatorShader    = validatorShaderTemplate.specialize(validatorParams);
177 
178     const char *resetSSBOShader = "#version 450\n"
179                                   "layout(local_size_x = 1) in;\n"
180                                   "\n"
181                                   "layout(set=0, binding=1) buffer ProtectedHelper\n"
182                                   "{\n"
183                                   "    highp uint zero; // set to 0\n"
184                                   "    highp uint unusedOut;\n"
185                                   "} helper;\n"
186                                   "\n"
187                                   "void main (void)\n"
188                                   "{\n"
189                                   "    helper.zero = 0;\n"
190                                   "}\n";
191 
192     programCollection.glslSources.add("ResetSSBO") << glu::ComputeSource(resetSSBOShader);
193     programCollection.glslSources.add("BufferValidator") << glu::ComputeSource(validatorShader);
194 }
195 
getDescriptorType(BufferType bufferType)196 vk::VkDescriptorType getDescriptorType(BufferType bufferType)
197 {
198     switch (bufferType)
199     {
200     case STORAGE_BUFFER:
201         return vk::VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
202     case SAMPLER_BUFFER:
203         return vk::VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER;
204     default:
205         DE_FATAL("Incorrect buffer type specified");
206         return (vk::VkDescriptorType)0;
207     }
208 }
209 
210 } // namespace ProtectedMem
211 } // namespace vkt
212