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:		return "uvec4";
50 		case TYPE_INT:		return "ivec4";
51 		case TYPE_FLOAT:	return "vec4";
52 
53 		default: DE_FATAL("Incorrect vector type format"); return "";
54 	}
55 }
56 
generateShaderBufferString(TestType testType,BufferType bufferType)57 const char* generateShaderBufferString (TestType testType, BufferType bufferType)
58 {
59 	if (bufferType == STORAGE_BUFFER)
60 		return "buffer";
61 
62 	DE_ASSERT(bufferType == SAMPLER_BUFFER);
63 
64 	switch (testType) {
65 		case TYPE_UINT:		return "uniform usamplerBuffer";
66 		case TYPE_INT:		return "uniform isamplerBuffer";
67 		case TYPE_FLOAT:	return "uniform samplerBuffer";
68 
69 		default: DE_FATAL("Incorrect sampler buffer format"); return "";
70 	}
71 }
72 
73 } // anonymous
74 
initBufferValidatorPrograms(vk::SourceCollections & programCollection,TestType testType,BufferType bufferType)75 void initBufferValidatorPrograms (vk::SourceCollections& programCollection, TestType testType, BufferType bufferType)
76 {
77 	// Layout:
78 	//  set = 0, location = 0 -> buffer|uniform usamplerBuffer|uniform isamplerBuffer|uniform samplerBuffersampler2D u_protectedBuffer
79 	//  set = 0, location = 1 -> buffer ProtectedHelper (2 * uint)
80 	//  set = 0, location = 2 -> uniform Data (2 * vec2 + 4 * vec4|ivec4|uvec4)
81 	const char* validatorShaderTemplateSamplerBuffer = "#version 450\n"
82 					  "layout(local_size_x = 1) in;\n"
83 					  "\n"
84 					  "layout(set=0, binding=0) ${BUFFER_TYPE} u_protectedBuffer;\n"
85 					  "\n"
86 					  "layout(set=0, binding=1) buffer ProtectedHelper\n"
87 					  "{\n"
88 					  "    highp uint zero; // set to 0\n"
89 					  "    highp uint dummyOut;\n"
90 					  "} helper;\n"
91 					  "\n"
92 					  "layout(set=0, binding=2) uniform Data\n"
93 					  "{\n"
94 					  "    highp ivec4 protectedBufferPosition[4];\n"
95 					  "    highp ${VAR_TYPE} protectedBufferRef[4];\n"
96 					  "};\n"
97 					  "\n"
98 					  "void error ()\n"
99 					  "{\n"
100 					  "    for (uint x = 0; x < 10; x += helper.zero)\n"
101 					  "        atomicAdd(helper.dummyOut, 1u);\n"
102 					  "}\n"
103 					  "\n"
104 					  "bool compare (${VAR_TYPE} a, ${VAR_TYPE} b, float threshold)\n"
105 					  "{\n"
106 					  "    return all(lessThanEqual(abs(a - b), ${VAR_TYPE}(threshold)));\n"
107 					  "}\n"
108 					  "\n"
109 					  "void main (void)\n"
110 					  "{\n"
111 					  "    float threshold = 0.1;\n"
112 					  "    for (uint i = 0; i < 4; i++)\n"
113 					  "    {\n"
114 					  "        ${VAR_TYPE} v = texelFetch(u_protectedBuffer, protectedBufferPosition[i].x);\n"
115 					  "        if (!compare(v, protectedBufferRef[i], threshold))\n"
116 					  "            error();\n"
117 					  "    }\n"
118 					  "}\n";
119 
120 	const char* validatorShaderTemplateStorageBuffer = "#version 450\n"
121 					  "layout(local_size_x = 1) in;\n"
122 					  "\n"
123 					  "layout(set=0, binding=0) ${BUFFER_TYPE} u_protectedBuffer\n"
124 					  "{\n"
125 					  "    highp ${VAR_TYPE} protectedTestValues;\n"
126 					  "} testBuffer;\n"
127 					  "\n"
128 					  "layout(set=0, binding=1) buffer ProtectedHelper\n"
129 					  "{\n"
130 					  "    highp uint zero; // set to 0\n"
131 					  "    highp uint dummyOut;\n"
132 					  "} helper;\n"
133 					  "\n"
134 					  "layout(set=0, binding=2) uniform Data\n"
135 					  "{\n"
136 					  "    highp ${VAR_TYPE} protectedReferenceValues;\n"
137 					  "};\n"
138 					  "\n"
139 					  "void error ()\n"
140 					  "{\n"
141 					  "    for (uint x = 0; x < 10; x += helper.zero)\n"
142 					  "        atomicAdd(helper.dummyOut, 1u);\n"
143 					  "}\n"
144 					  "\n"
145 					  "bool compare (${VAR_TYPE} a, ${VAR_TYPE} b, float threshold)\n"
146 					  "{\n"
147 					  "    return all(lessThanEqual(abs(a - b), ${VAR_TYPE}(threshold)));\n"
148 					  "}\n"
149 					  "\n"
150 					  "void main (void)\n"
151 					  "{\n"
152 					  "    float threshold = 0.1;\n"
153 					  "    if (!compare(testBuffer.protectedTestValues, protectedReferenceValues, threshold))\n"
154 					  "        error();\n"
155 					  "}\n";
156 
157 	tcu::StringTemplate validatorShaderTemplate(bufferType == SAMPLER_BUFFER ? validatorShaderTemplateSamplerBuffer : validatorShaderTemplateStorageBuffer);
158 
159 	std::map<std::string, std::string> validatorParams;
160 	validatorParams["VAR_TYPE"]		= generateShaderVarString(testType);
161 	validatorParams["BUFFER_TYPE"]	= generateShaderBufferString(testType, bufferType);
162 	std::string validatorShader		= validatorShaderTemplate.specialize(validatorParams);
163 
164 	const char* resetSSBOShader = "#version 450\n"
165 					  "layout(local_size_x = 1) in;\n"
166 					  "\n"
167 					  "layout(set=0, binding=1) buffer ProtectedHelper\n"
168 					  "{\n"
169 					  "    highp uint zero; // set to 0\n"
170 					  "    highp uint dummyOut;\n"
171 					  "} helper;\n"
172 					  "\n"
173 					  "void main (void)\n"
174 					  "{\n"
175 					  "    helper.zero = 0;\n"
176 					  "}\n";
177 
178 	programCollection.glslSources.add("ResetSSBO") << glu::ComputeSource(resetSSBOShader);
179 	programCollection.glslSources.add("BufferValidator") << glu::ComputeSource(validatorShader);
180 }
181 
getDescriptorType(BufferType bufferType)182 vk::VkDescriptorType getDescriptorType (BufferType bufferType)
183 {
184 	switch (bufferType)
185 	{
186 		case STORAGE_BUFFER: return vk::VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
187 		case SAMPLER_BUFFER: return vk::VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER;
188 		default: DE_FATAL("Incorrect buffer type specified"); return (vk::VkDescriptorType)0;
189 	}
190 }
191 
192 } // ProtectedMem
193 } // vkt
194