1 /*------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
4 *
5 * Copyright (c) 2014 The Android Open Source Project
6 * Copyright (c) 2016 The Khronos Group Inc.
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 Tessellation Geometry Interaction - Grid render (limits, scatter)
23 *//*--------------------------------------------------------------------*/
24
25 #include "vktTessellationGeometryGridRenderTests.hpp"
26 #include "vktTestCaseUtil.hpp"
27 #include "vktTessellationUtil.hpp"
28
29 #include "tcuTestLog.hpp"
30 #include "tcuTextureUtil.hpp"
31 #include "tcuSurface.hpp"
32 #include "tcuRGBA.hpp"
33
34 #include "vkDefs.hpp"
35 #include "vkBarrierUtil.hpp"
36 #include "vkQueryUtil.hpp"
37 #include "vkBuilderUtil.hpp"
38 #include "vkTypeUtil.hpp"
39 #include "vkImageUtil.hpp"
40 #include "vkCmdUtil.hpp"
41 #include "vkObjUtil.hpp"
42
43 #include "deUniquePtr.hpp"
44
45 #include <string>
46 #include <vector>
47
48 namespace vkt
49 {
50 namespace tessellation
51 {
52
53 using namespace vk;
54
55 namespace
56 {
57
58 enum Constants
59 {
60 RENDER_SIZE = 256,
61 };
62
63 enum FlagBits
64 {
65 FLAG_TESSELLATION_MAX_SPEC = 1u << 0,
66 FLAG_GEOMETRY_MAX_SPEC = 1u << 1,
67 FLAG_GEOMETRY_INVOCATIONS_MAX_SPEC = 1u << 2,
68
69 FLAG_GEOMETRY_SCATTER_INSTANCES = 1u << 3,
70 FLAG_GEOMETRY_SCATTER_PRIMITIVES = 1u << 4,
71 FLAG_GEOMETRY_SEPARATE_PRIMITIVES = 1u << 5, //!< if set, geometry shader outputs separate grid cells and not continuous slices
72 FLAG_GEOMETRY_SCATTER_LAYERS = 1u << 6,
73 };
74 typedef deUint32 Flags;
75
76 class GridRenderTestCase : public TestCase
77 {
78 public:
79 void initPrograms (vk::SourceCollections& programCollection) const;
80 TestInstance* createInstance (Context& context) const;
81
82 GridRenderTestCase (tcu::TestContext& testCtx, const std::string& name, const std::string& description, const Flags flags);
83
84 private:
85 const Flags m_flags;
86 const int m_tessGenLevel;
87 const int m_numGeometryInvocations;
88 const int m_numLayers;
89 int m_numGeometryPrimitivesPerInvocation;
90 };
91
GridRenderTestCase(tcu::TestContext & testCtx,const std::string & name,const std::string & description,const Flags flags)92 GridRenderTestCase::GridRenderTestCase (tcu::TestContext& testCtx, const std::string& name, const std::string& description, const Flags flags)
93 : TestCase (testCtx, name, description)
94 , m_flags (flags)
95 , m_tessGenLevel ((m_flags & FLAG_TESSELLATION_MAX_SPEC) ? 64 : 5)
96 , m_numGeometryInvocations ((m_flags & FLAG_GEOMETRY_INVOCATIONS_MAX_SPEC) ? 32 : 4)
97 , m_numLayers ((m_flags & FLAG_GEOMETRY_SCATTER_LAYERS) ? 8 : 1)
98 {
99 DE_ASSERT(((flags & (FLAG_GEOMETRY_SCATTER_PRIMITIVES | FLAG_GEOMETRY_SCATTER_LAYERS)) != 0) == ((flags & FLAG_GEOMETRY_SEPARATE_PRIMITIVES) != 0));
100
101 int geometryOutputVertices = 0;
102 int geometryTotalOutputComponents = 0;
103
104 if (m_flags & FLAG_GEOMETRY_MAX_SPEC)
105 {
106 geometryOutputVertices = 256;
107 geometryTotalOutputComponents = 1024;
108 }
109 else
110 {
111 geometryOutputVertices = 16;
112 geometryTotalOutputComponents = 1024;
113 }
114
115 const bool separatePrimitives = (m_flags & FLAG_GEOMETRY_SEPARATE_PRIMITIVES) != 0;
116 const int numComponentsPerVertex = 8; // vec4 pos, vec4 color
117
118 if (separatePrimitives)
119 {
120 const int numComponentLimit = geometryTotalOutputComponents / (4 * numComponentsPerVertex);
121 const int numOutputLimit = geometryOutputVertices / 4;
122
123 m_numGeometryPrimitivesPerInvocation = de::min(numComponentLimit, numOutputLimit);
124 }
125 else
126 {
127 // If FLAG_GEOMETRY_SEPARATE_PRIMITIVES is not set, geometry shader fills a rectangle area in slices.
128 // Each slice is a triangle strip and is generated by a single shader invocation.
129 // One slice with 4 segment ends (nodes) and 3 segments:
130 // .__.__.__.
131 // |\ |\ |\ |
132 // |_\|_\|_\|
133
134 const int numSliceNodesComponentLimit = geometryTotalOutputComponents / (2 * numComponentsPerVertex + 2); // each node 2 vertices
135 const int numSliceNodesOutputLimit = geometryOutputVertices / 2; // each node 2 vertices
136 const int numSliceNodes = de::min(numSliceNodesComponentLimit, numSliceNodesOutputLimit);
137
138 m_numGeometryPrimitivesPerInvocation = (numSliceNodes - 1) * 2;
139 }
140
141 }
142
initPrograms(SourceCollections & programCollection) const143 void GridRenderTestCase::initPrograms (SourceCollections& programCollection) const
144 {
145 // Vertex shader
146 {
147 std::ostringstream src;
148 src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_310_ES) << "\n"
149 << "\n"
150 << "void main (void)\n"
151 << "{\n"
152 << " gl_Position = vec4(0.0, 0.0, 0.0, 1.0);\n"
153 << "}\n";
154
155 programCollection.glslSources.add("vert") << glu::VertexSource(src.str());
156 }
157
158 // Fragment shader
159 {
160 std::ostringstream src;
161 src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_310_ES) << "\n"
162 << "layout(location = 0) flat in highp vec4 v_color;\n"
163 << "layout(location = 0) out mediump vec4 fragColor;\n"
164 << "\n"
165 << "void main (void)\n"
166 << "{\n"
167 << " fragColor = v_color;\n"
168 << "}\n";
169
170 programCollection.glslSources.add("frag") << glu::FragmentSource(src.str());
171 }
172
173 // Tessellation control
174 {
175 std::ostringstream src;
176 src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_310_ES) << "\n"
177 "#extension GL_EXT_tessellation_shader : require\n"
178 "layout(vertices = 1) out;\n"
179 "\n"
180 "void main (void)\n"
181 "{\n"
182 " gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;\n"
183 " gl_TessLevelInner[0] = float(" << m_tessGenLevel << ");\n"
184 " gl_TessLevelInner[1] = float(" << m_tessGenLevel << ");\n"
185 " gl_TessLevelOuter[0] = float(" << m_tessGenLevel << ");\n"
186 " gl_TessLevelOuter[1] = float(" << m_tessGenLevel << ");\n"
187 " gl_TessLevelOuter[2] = float(" << m_tessGenLevel << ");\n"
188 " gl_TessLevelOuter[3] = float(" << m_tessGenLevel << ");\n"
189 "}\n";
190
191 programCollection.glslSources.add("tesc") << glu::TessellationControlSource(src.str());
192 }
193
194 // Tessellation evaluation
195 {
196 std::ostringstream src;
197 src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_310_ES) << "\n"
198 << "#extension GL_EXT_tessellation_shader : require\n"
199 << "layout(quads) in;\n"
200 << "\n"
201 << "layout(location = 0) out mediump ivec2 v_tessellationGridPosition;\n"
202 << "\n"
203 << "// note: No need to use precise gl_Position since position does not depend on order\n"
204 << "void main (void)\n"
205 << "{\n";
206
207 if (m_flags & (FLAG_GEOMETRY_SCATTER_INSTANCES | FLAG_GEOMETRY_SCATTER_PRIMITIVES | FLAG_GEOMETRY_SCATTER_LAYERS))
208 src << " // Cover only a small area in a corner. The area will be expanded in geometry shader to cover whole viewport\n"
209 << " gl_Position = vec4(gl_TessCoord.x * 0.3 - 1.0, gl_TessCoord.y * 0.3 - 1.0, 0.0, 1.0);\n";
210 else
211 src << " // Fill the whole viewport\n"
212 << " gl_Position = vec4(gl_TessCoord.x * 2.0 - 1.0, gl_TessCoord.y * 2.0 - 1.0, 0.0, 1.0);\n";
213
214 src << " // Calculate position in tessellation grid\n"
215 << " v_tessellationGridPosition = ivec2(round(gl_TessCoord.xy * float(" << m_tessGenLevel << ")));\n"
216 << "}\n";
217
218 programCollection.glslSources.add("tese") << glu::TessellationEvaluationSource(src.str());
219 }
220
221 // Geometry shader
222 {
223 const int numInvocations = m_numGeometryInvocations;
224 const int numPrimitives = m_numGeometryPrimitivesPerInvocation;
225
226 std::ostringstream src;
227
228 src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_310_ES) << "\n"
229 << "#extension GL_EXT_geometry_shader : require\n"
230 << "layout(triangles, invocations = " << numInvocations << ") in;\n"
231 << "layout(triangle_strip, max_vertices = " << ((m_flags & FLAG_GEOMETRY_SEPARATE_PRIMITIVES) ? (4 * numPrimitives) : (numPrimitives + 2)) << ") out;\n"
232 << "\n"
233 << "layout(location = 0) in mediump ivec2 v_tessellationGridPosition[];\n"
234 << "layout(location = 0) flat out highp vec4 v_color;\n"
235 << "\n"
236 << "void main (void)\n"
237 << "{\n"
238 << " const float equalThreshold = 0.001;\n"
239 << " const float gapOffset = 0.0001; // subdivision performed by the geometry shader might produce gaps. Fill potential gaps by enlarging the output slice a little.\n"
240 << "\n"
241 << " // Input triangle is generated from an axis-aligned rectangle by splitting it in half\n"
242 << " // Original rectangle can be found by finding the bounding AABB of the triangle\n"
243 << " vec4 aabb = vec4(min(gl_in[0].gl_Position.x, min(gl_in[1].gl_Position.x, gl_in[2].gl_Position.x)),\n"
244 << " min(gl_in[0].gl_Position.y, min(gl_in[1].gl_Position.y, gl_in[2].gl_Position.y)),\n"
245 << " max(gl_in[0].gl_Position.x, max(gl_in[1].gl_Position.x, gl_in[2].gl_Position.x)),\n"
246 << " max(gl_in[0].gl_Position.y, max(gl_in[1].gl_Position.y, gl_in[2].gl_Position.y)));\n"
247 << "\n"
248 << " // Location in tessellation grid\n"
249 << " ivec2 gridPosition = ivec2(min(v_tessellationGridPosition[0], min(v_tessellationGridPosition[1], v_tessellationGridPosition[2])));\n"
250 << "\n"
251 << " // Which triangle of the two that split the grid cell\n"
252 << " int numVerticesOnBottomEdge = 0;\n"
253 << " for (int ndx = 0; ndx < 3; ++ndx)\n"
254 << " if (abs(gl_in[ndx].gl_Position.y - aabb.w) < equalThreshold)\n"
255 << " ++numVerticesOnBottomEdge;\n"
256 << " bool isBottomTriangle = numVerticesOnBottomEdge == 2;\n"
257 << "\n";
258
259 if (m_flags & FLAG_GEOMETRY_SCATTER_PRIMITIVES)
260 {
261 // scatter primitives
262 src << " // Draw grid cells\n"
263 << " int inputTriangleNdx = gl_InvocationID * 2 + ((isBottomTriangle) ? (1) : (0));\n"
264 << " for (int ndx = 0; ndx < " << numPrimitives << "; ++ndx)\n"
265 << " {\n"
266 << " ivec2 dstGridSize = ivec2(" << m_tessGenLevel << " * " << numPrimitives << ", 2 * " << m_tessGenLevel << " * " << numInvocations << ");\n"
267 << " ivec2 dstGridNdx = ivec2(" << m_tessGenLevel << " * ndx + gridPosition.x, " << m_tessGenLevel << " * inputTriangleNdx + 2 * gridPosition.y + ndx * 127) % dstGridSize;\n"
268 << " vec4 dstArea;\n"
269 << " dstArea.x = float(dstGridNdx.x) / float(dstGridSize.x) * 2.0 - 1.0 - gapOffset;\n"
270 << " dstArea.y = float(dstGridNdx.y) / float(dstGridSize.y) * 2.0 - 1.0 - gapOffset;\n"
271 << " dstArea.z = float(dstGridNdx.x+1) / float(dstGridSize.x) * 2.0 - 1.0 + gapOffset;\n"
272 << " dstArea.w = float(dstGridNdx.y+1) / float(dstGridSize.y) * 2.0 - 1.0 + gapOffset;\n"
273 << "\n"
274 << " vec4 green = vec4(0.0, 1.0, 0.0, 1.0);\n"
275 << " vec4 yellow = vec4(1.0, 1.0, 0.0, 1.0);\n"
276 << " vec4 outputColor = (((dstGridNdx.y + dstGridNdx.x) % 2) == 0) ? (green) : (yellow);\n"
277 << "\n"
278 << " gl_Position = vec4(dstArea.x, dstArea.y, 0.0, 1.0);\n"
279 << " v_color = outputColor;\n"
280 << " EmitVertex();\n"
281 << "\n"
282 << " gl_Position = vec4(dstArea.x, dstArea.w, 0.0, 1.0);\n"
283 << " v_color = outputColor;\n"
284 << " EmitVertex();\n"
285 << "\n"
286 << " gl_Position = vec4(dstArea.z, dstArea.y, 0.0, 1.0);\n"
287 << " v_color = outputColor;\n"
288 << " EmitVertex();\n"
289 << "\n"
290 << " gl_Position = vec4(dstArea.z, dstArea.w, 0.0, 1.0);\n"
291 << " v_color = outputColor;\n"
292 << " EmitVertex();\n"
293 << " EndPrimitive();\n"
294 << " }\n";
295 }
296 else if (m_flags & FLAG_GEOMETRY_SCATTER_LAYERS)
297 {
298 // Number of subrectangle instances = num layers
299 DE_ASSERT(m_numLayers == numInvocations * 2);
300
301 src << " // Draw grid cells, send each primitive to a separate layer\n"
302 << " int baseLayer = gl_InvocationID * 2 + ((isBottomTriangle) ? (1) : (0));\n"
303 << " for (int ndx = 0; ndx < " << numPrimitives << "; ++ndx)\n"
304 << " {\n"
305 << " ivec2 dstGridSize = ivec2(" << m_tessGenLevel << " * " << numPrimitives << ", " << m_tessGenLevel << ");\n"
306 << " ivec2 dstGridNdx = ivec2((gridPosition.x * " << numPrimitives << " * 7 + ndx)*13, (gridPosition.y * 127 + ndx) * 19) % dstGridSize;\n"
307 << " vec4 dstArea;\n"
308 << " dstArea.x = float(dstGridNdx.x) / float(dstGridSize.x) * 2.0 - 1.0 - gapOffset;\n"
309 << " dstArea.y = float(dstGridNdx.y) / float(dstGridSize.y) * 2.0 - 1.0 - gapOffset;\n"
310 << " dstArea.z = float(dstGridNdx.x+1) / float(dstGridSize.x) * 2.0 - 1.0 + gapOffset;\n"
311 << " dstArea.w = float(dstGridNdx.y+1) / float(dstGridSize.y) * 2.0 - 1.0 + gapOffset;\n"
312 << "\n"
313 << " vec4 green = vec4(0.0, 1.0, 0.0, 1.0);\n"
314 << " vec4 yellow = vec4(1.0, 1.0, 0.0, 1.0);\n"
315 << " vec4 outputColor = (((dstGridNdx.y + dstGridNdx.x) % 2) == 0) ? (green) : (yellow);\n"
316 << "\n"
317 << " gl_Position = vec4(dstArea.x, dstArea.y, 0.0, 1.0);\n"
318 << " v_color = outputColor;\n"
319 << " gl_Layer = ((baseLayer + ndx) * 11) % " << m_numLayers << ";\n"
320 << " EmitVertex();\n"
321 << "\n"
322 << " gl_Position = vec4(dstArea.x, dstArea.w, 0.0, 1.0);\n"
323 << " v_color = outputColor;\n"
324 << " gl_Layer = ((baseLayer + ndx) * 11) % " << m_numLayers << ";\n"
325 << " EmitVertex();\n"
326 << "\n"
327 << " gl_Position = vec4(dstArea.z, dstArea.y, 0.0, 1.0);\n"
328 << " v_color = outputColor;\n"
329 << " gl_Layer = ((baseLayer + ndx) * 11) % " << m_numLayers << ";\n"
330 << " EmitVertex();\n"
331 << "\n"
332 << " gl_Position = vec4(dstArea.z, dstArea.w, 0.0, 1.0);\n"
333 << " v_color = outputColor;\n"
334 << " gl_Layer = ((baseLayer + ndx) * 11) % " << m_numLayers << ";\n"
335 << " EmitVertex();\n"
336 << " EndPrimitive();\n"
337 << " }\n";
338 }
339 else
340 {
341 if (m_flags & FLAG_GEOMETRY_SCATTER_INSTANCES)
342 {
343 src << " // Scatter slices\n"
344 << " int inputTriangleNdx = gl_InvocationID * 2 + ((isBottomTriangle) ? (1) : (0));\n"
345 << " ivec2 srcSliceNdx = ivec2(gridPosition.x, gridPosition.y * " << (numInvocations*2) << " + inputTriangleNdx);\n"
346 << " ivec2 dstSliceNdx = ivec2(7 * srcSliceNdx.x, 127 * srcSliceNdx.y) % ivec2(" << m_tessGenLevel << ", " << m_tessGenLevel << " * " << (numInvocations*2) << ");\n"
347 << "\n"
348 << " // Draw slice to the dstSlice slot\n"
349 << " vec4 outputSliceArea;\n"
350 << " outputSliceArea.x = float(dstSliceNdx.x) / float(" << m_tessGenLevel << ") * 2.0 - 1.0 - gapOffset;\n"
351 << " outputSliceArea.y = float(dstSliceNdx.y) / float(" << (m_tessGenLevel * numInvocations * 2) << ") * 2.0 - 1.0 - gapOffset;\n"
352 << " outputSliceArea.z = float(dstSliceNdx.x+1) / float(" << m_tessGenLevel << ") * 2.0 - 1.0 + gapOffset;\n"
353 << " outputSliceArea.w = float(dstSliceNdx.y+1) / float(" << (m_tessGenLevel * numInvocations * 2) << ") * 2.0 - 1.0 + gapOffset;\n";
354 }
355 else
356 {
357 src << " // Fill the input area with slices\n"
358 << " // Upper triangle produces slices only to the upper half of the quad and vice-versa\n"
359 << " float triangleOffset = (isBottomTriangle) ? ((aabb.w + aabb.y) / 2.0) : (aabb.y);\n"
360 << " // Each slice is a invocation\n"
361 << " float sliceHeight = (aabb.w - aabb.y) / float(2 * " << numInvocations << ");\n"
362 << " float invocationOffset = float(gl_InvocationID) * sliceHeight;\n"
363 << "\n"
364 << " vec4 outputSliceArea;\n"
365 << " outputSliceArea.x = aabb.x - gapOffset;\n"
366 << " outputSliceArea.y = triangleOffset + invocationOffset - gapOffset;\n"
367 << " outputSliceArea.z = aabb.z + gapOffset;\n"
368 << " outputSliceArea.w = triangleOffset + invocationOffset + sliceHeight + gapOffset;\n";
369 }
370
371 src << "\n"
372 << " // Draw slice\n"
373 << " for (int ndx = 0; ndx < " << ((numPrimitives+2)/2) << "; ++ndx)\n"
374 << " {\n"
375 << " vec4 green = vec4(0.0, 1.0, 0.0, 1.0);\n"
376 << " vec4 yellow = vec4(1.0, 1.0, 0.0, 1.0);\n"
377 << " vec4 outputColor = (((gl_InvocationID + ndx) % 2) == 0) ? (green) : (yellow);\n"
378 << " float xpos = mix(outputSliceArea.x, outputSliceArea.z, float(ndx) / float(" << (numPrimitives/2) << "));\n"
379 << "\n"
380 << " gl_Position = vec4(xpos, outputSliceArea.y, 0.0, 1.0);\n"
381 << " v_color = outputColor;\n"
382 << " EmitVertex();\n"
383 << "\n"
384 << " gl_Position = vec4(xpos, outputSliceArea.w, 0.0, 1.0);\n"
385 << " v_color = outputColor;\n"
386 << " EmitVertex();\n"
387 << " }\n";
388 }
389
390 src << "}\n";
391
392 programCollection.glslSources.add("geom") << glu::GeometrySource(src.str());
393 }
394 }
395
396 class GridRenderTestInstance : public TestInstance
397 {
398 public:
399 struct Params
400 {
401 tcu::TestContext& testCtx;
402 Flags flags;
403 const char* description;
404 int tessGenLevel;
405 int numGeometryInvocations;
406 int numLayers;
407 int numGeometryPrimitivesPerInvocation;
408
Paramsvkt::tessellation::__anonddca29f70111::GridRenderTestInstance::Params409 Params (tcu::TestContext& testContext) : testCtx(testContext), flags(), description(), tessGenLevel(), numGeometryInvocations(), numLayers(), numGeometryPrimitivesPerInvocation() {}
410 };
411 GridRenderTestInstance (Context& context, const Params& params);
412 tcu::TestStatus iterate (void);
413
414 private:
415 Params m_params;
416 };
417
GridRenderTestInstance(Context & context,const Params & params)418 GridRenderTestInstance::GridRenderTestInstance (Context& context, const Params& params) : TestInstance(context), m_params(params)
419 {
420 tcu::TestContext& testCtx = m_params.testCtx;
421 testCtx.getLog()
422 << tcu::TestLog::Message
423 << "Testing tessellation and geometry shaders that output a large number of primitives.\n"
424 << m_params.description
425 << tcu::TestLog::EndMessage;
426
427 if (m_params.flags & FLAG_GEOMETRY_SCATTER_LAYERS)
428 testCtx.getLog() << tcu::TestLog::Message << "Rendering to 2d texture array, numLayers = " << m_params.numLayers << tcu::TestLog::EndMessage;
429
430 testCtx.getLog()
431 << tcu::TestLog::Message
432 << "Tessellation level: " << m_params.tessGenLevel << ", mode = quad.\n"
433 << "\tEach input patch produces " << (m_params.tessGenLevel * m_params.tessGenLevel) << " (" << (m_params.tessGenLevel * m_params.tessGenLevel * 2) << " triangles)\n"
434 << tcu::TestLog::EndMessage;
435
436 int geometryOutputComponents = 0;
437 int geometryOutputVertices = 0;
438 int geometryTotalOutputComponents = 0;
439
440 if (m_params.flags & FLAG_GEOMETRY_MAX_SPEC)
441 {
442 testCtx.getLog() << tcu::TestLog::Message << "Using geometry shader minimum maximum output limits." << tcu::TestLog::EndMessage;
443
444 geometryOutputComponents = 64;
445 geometryOutputVertices = 256;
446 geometryTotalOutputComponents = 1024;
447 }
448 else
449 {
450 geometryOutputComponents = 64;
451 geometryOutputVertices = 16;
452 geometryTotalOutputComponents = 1024;
453 }
454
455 if ((m_params.flags & FLAG_GEOMETRY_MAX_SPEC) || (m_params.flags & FLAG_GEOMETRY_INVOCATIONS_MAX_SPEC))
456 {
457 tcu::MessageBuilder msg(&testCtx.getLog());
458
459 msg << "Geometry shader, targeting following limits:\n";
460
461 if (m_params.flags & FLAG_GEOMETRY_MAX_SPEC)
462 msg << "\tmaxGeometryOutputComponents = " << geometryOutputComponents << "\n"
463 << "\tmaxGeometryOutputVertices = " << geometryOutputVertices << "\n"
464 << "\tmaxGeometryTotalOutputComponents = " << geometryTotalOutputComponents << "\n";
465
466 if (m_params.flags & FLAG_GEOMETRY_INVOCATIONS_MAX_SPEC)
467 msg << "\tmaxGeometryShaderInvocations = " << m_params.numGeometryInvocations;
468
469 msg << tcu::TestLog::EndMessage;
470 }
471
472 const bool separatePrimitives = (m_params.flags & FLAG_GEOMETRY_SEPARATE_PRIMITIVES) != 0;
473 const int numComponentsPerVertex = 8; // vec4 pos, vec4 color
474 int numVerticesPerInvocation = 0;
475 int geometryVerticesPerPrimitive = 0;
476 int geometryPrimitivesOutPerPrimitive = 0;
477
478 if (separatePrimitives)
479 {
480 numVerticesPerInvocation = m_params.numGeometryPrimitivesPerInvocation * 4;
481 }
482 else
483 {
484 // If FLAG_GEOMETRY_SEPARATE_PRIMITIVES is not set, geometry shader fills a rectangle area in slices.
485 // Each slice is a triangle strip and is generated by a single shader invocation.
486 // One slice with 4 segment ends (nodes) and 3 segments:
487 // .__.__.__.
488 // |\ |\ |\ |
489 // |_\|_\|_\|
490
491 const int numSliceNodesComponentLimit = geometryTotalOutputComponents / (2 * numComponentsPerVertex); // each node 2 vertices
492 const int numSliceNodesOutputLimit = geometryOutputVertices / 2; // each node 2 vertices
493 const int numSliceNodes = de::min(numSliceNodesComponentLimit, numSliceNodesOutputLimit);
494
495 numVerticesPerInvocation = numSliceNodes * 2;
496 }
497
498 geometryVerticesPerPrimitive = numVerticesPerInvocation * m_params.numGeometryInvocations;
499 geometryPrimitivesOutPerPrimitive = m_params.numGeometryPrimitivesPerInvocation * m_params.numGeometryInvocations;
500
501 testCtx.getLog()
502 << tcu::TestLog::Message
503 << "Geometry shader:\n"
504 << "\tTotal output vertex count per invocation: " << numVerticesPerInvocation << "\n"
505 << "\tTotal output primitive count per invocation: " << m_params.numGeometryPrimitivesPerInvocation << "\n"
506 << "\tNumber of invocations per primitive: " << m_params.numGeometryInvocations << "\n"
507 << "\tTotal output vertex count per input primitive: " << geometryVerticesPerPrimitive << "\n"
508 << "\tTotal output primitive count per input primitive: " << geometryPrimitivesOutPerPrimitive << "\n"
509 << tcu::TestLog::EndMessage;
510
511 testCtx.getLog()
512 << tcu::TestLog::Message
513 << "Program:\n"
514 << "\tTotal program output vertices count per input patch: " << (m_params.tessGenLevel * m_params.tessGenLevel * 2 * geometryVerticesPerPrimitive) << "\n"
515 << "\tTotal program output primitive count per input patch: " << (m_params.tessGenLevel * m_params.tessGenLevel * 2 * geometryPrimitivesOutPerPrimitive) << "\n"
516 << tcu::TestLog::EndMessage;
517 }
518
createInstance(Context & context) const519 TestInstance* GridRenderTestCase::createInstance (Context& context) const
520 {
521 GridRenderTestInstance::Params params(m_testCtx);
522
523 params.flags = m_flags;
524 params.description = getDescription();
525 params.tessGenLevel = m_tessGenLevel;
526 params.numGeometryInvocations = m_numGeometryInvocations;
527 params.numLayers = m_numLayers;
528 params.numGeometryPrimitivesPerInvocation = m_numGeometryPrimitivesPerInvocation;
529
530 return new GridRenderTestInstance(context, params);
531 }
532
verifyResultLayer(tcu::TestLog & log,const tcu::ConstPixelBufferAccess & image,const int layerNdx)533 bool verifyResultLayer (tcu::TestLog& log, const tcu::ConstPixelBufferAccess& image, const int layerNdx)
534 {
535 tcu::Surface errorMask (image.getWidth(), image.getHeight());
536 bool foundError = false;
537
538 tcu::clear(errorMask.getAccess(), tcu::Vec4(0.0f, 1.0f, 0.0f, 1.0f));
539
540 log << tcu::TestLog::Message << "Verifying output layer " << layerNdx << tcu::TestLog::EndMessage;
541
542 for (int y = 0; y < image.getHeight(); ++y)
543 for (int x = 0; x < image.getWidth(); ++x)
544 {
545 const int threshold = 8;
546 const tcu::RGBA color (image.getPixel(x, y));
547
548 // Color must be a linear combination of green and yellow
549 if (color.getGreen() < 255 - threshold || color.getBlue() > threshold)
550 {
551 errorMask.setPixel(x, y, tcu::RGBA::red());
552 foundError = true;
553 }
554 }
555
556 if (!foundError)
557 {
558 log << tcu::TestLog::Message << "Image valid." << tcu::TestLog::EndMessage
559 << tcu::TestLog::ImageSet("ImageVerification", "Image verification")
560 << tcu::TestLog::Image("Result", "Rendered result", image)
561 << tcu::TestLog::EndImageSet;
562 return true;
563 }
564 else
565 {
566 log << tcu::TestLog::Message << "Image verification failed, found invalid pixels." << tcu::TestLog::EndMessage
567 << tcu::TestLog::ImageSet("ImageVerification", "Image verification")
568 << tcu::TestLog::Image("Result", "Rendered result", image)
569 << tcu::TestLog::Image("ErrorMask", "Error mask", errorMask.getAccess())
570 << tcu::TestLog::EndImageSet;
571 return false;
572 }
573 }
574
iterate(void)575 tcu::TestStatus GridRenderTestInstance::iterate (void)
576 {
577 requireFeatures(m_context.getInstanceInterface(), m_context.getPhysicalDevice(), FEATURE_TESSELLATION_SHADER | FEATURE_GEOMETRY_SHADER);
578
579 m_context.getTestContext().getLog()
580 << tcu::TestLog::Message
581 << "Rendering single point at the origin. Expecting yellow and green colored grid-like image. (High-frequency grid may appear unicolored)."
582 << tcu::TestLog::EndMessage;
583
584 const DeviceInterface& vk = m_context.getDeviceInterface();
585 const VkDevice device = m_context.getDevice();
586 const VkQueue queue = m_context.getUniversalQueue();
587 const deUint32 queueFamilyIndex = m_context.getUniversalQueueFamilyIndex();
588 Allocator& allocator = m_context.getDefaultAllocator();
589
590 // Color attachment
591
592 const tcu::IVec2 renderSize = tcu::IVec2(RENDER_SIZE, RENDER_SIZE);
593 const VkFormat colorFormat = VK_FORMAT_R8G8B8A8_UNORM;
594 const VkImageSubresourceRange colorImageAllLayersRange = makeImageSubresourceRange(VK_IMAGE_ASPECT_COLOR_BIT, 0u, 1u, 0u, m_params.numLayers);
595 const VkImageCreateInfo colorImageCreateInfo = makeImageCreateInfo(renderSize, colorFormat, VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT, m_params.numLayers);
596 const VkImageViewType colorAttachmentViewType = (m_params.numLayers == 1 ? VK_IMAGE_VIEW_TYPE_2D : VK_IMAGE_VIEW_TYPE_2D_ARRAY);
597 const Image colorAttachmentImage (vk, device, allocator, colorImageCreateInfo, MemoryRequirement::Any);
598
599 // Color output buffer: image will be copied here for verification (big enough for all layers).
600
601 const VkDeviceSize colorBufferSizeBytes = renderSize.x()*renderSize.y() * m_params.numLayers * tcu::getPixelSize(mapVkFormat(colorFormat));
602 const Buffer colorBuffer (vk, device, allocator, makeBufferCreateInfo(colorBufferSizeBytes, VK_BUFFER_USAGE_TRANSFER_DST_BIT), MemoryRequirement::HostVisible);
603
604 // Pipeline: no vertex input attributes nor descriptors.
605
606 const Unique<VkImageView> colorAttachmentView (makeImageView (vk, device, *colorAttachmentImage, colorAttachmentViewType, colorFormat, colorImageAllLayersRange));
607 const Unique<VkRenderPass> renderPass (makeRenderPass (vk, device, colorFormat));
608 const Unique<VkFramebuffer> framebuffer (makeFramebuffer (vk, device, *renderPass, *colorAttachmentView, renderSize.x(), renderSize.y(), m_params.numLayers));
609 const Unique<VkPipelineLayout> pipelineLayout (makePipelineLayout (vk, device));
610 const Unique<VkCommandPool> cmdPool (makeCommandPool (vk, device, queueFamilyIndex));
611 const Unique<VkCommandBuffer> cmdBuffer (allocateCommandBuffer (vk, device, *cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
612
613 const Unique<VkPipeline> pipeline (GraphicsPipelineBuilder()
614 .setRenderSize (renderSize)
615 .setShader (vk, device, VK_SHADER_STAGE_VERTEX_BIT, m_context.getBinaryCollection().get("vert"), DE_NULL)
616 .setShader (vk, device, VK_SHADER_STAGE_FRAGMENT_BIT, m_context.getBinaryCollection().get("frag"), DE_NULL)
617 .setShader (vk, device, VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT, m_context.getBinaryCollection().get("tesc"), DE_NULL)
618 .setShader (vk, device, VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT, m_context.getBinaryCollection().get("tese"), DE_NULL)
619 .setShader (vk, device, VK_SHADER_STAGE_GEOMETRY_BIT, m_context.getBinaryCollection().get("geom"), DE_NULL)
620 .build (vk, device, *pipelineLayout, *renderPass));
621
622 beginCommandBuffer(vk, *cmdBuffer);
623
624 // Change color attachment image layout
625 {
626 const VkImageMemoryBarrier colorAttachmentLayoutBarrier = makeImageMemoryBarrier(
627 (VkAccessFlags)0, VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
628 VK_IMAGE_LAYOUT_UNDEFINED, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
629 *colorAttachmentImage, colorImageAllLayersRange);
630
631 vk.cmdPipelineBarrier(*cmdBuffer, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0u,
632 0u, DE_NULL, 0u, DE_NULL, 1u, &colorAttachmentLayoutBarrier);
633 }
634
635 // Begin render pass
636 {
637 const VkRect2D renderArea = makeRect2D(renderSize);
638 const tcu::Vec4 clearColor (0.0f, 0.0f, 0.0f, 1.0f);
639
640 beginRenderPass(vk, *cmdBuffer, *renderPass, *framebuffer, renderArea, clearColor);
641 }
642
643 vk.cmdBindPipeline(*cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, *pipeline);
644
645 vk.cmdDraw(*cmdBuffer, 1u, 1u, 0u, 0u);
646 endRenderPass(vk, *cmdBuffer);
647
648 // Copy render result to a host-visible buffer
649 copyImageToBuffer(vk, *cmdBuffer, *colorAttachmentImage, *colorBuffer, renderSize, VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, m_params.numLayers);
650
651 endCommandBuffer(vk, *cmdBuffer);
652 submitCommandsAndWait(vk, device, queue, *cmdBuffer);
653
654 // Verify results
655 {
656 const Allocation& alloc (colorBuffer.getAllocation());
657
658 invalidateAlloc(vk, device, alloc);
659
660 const tcu::ConstPixelBufferAccess imageAllLayers (mapVkFormat(colorFormat), renderSize.x(), renderSize.y(), m_params.numLayers, alloc.getHostPtr());
661 bool allOk (true);
662
663 for (int ndx = 0; ndx < m_params.numLayers; ++ndx)
664 allOk = allOk && verifyResultLayer(m_context.getTestContext().getLog(),
665 tcu::getSubregion(imageAllLayers, 0, 0, ndx, renderSize.x(), renderSize.y(), 1),
666 ndx);
667
668 return (allOk ? tcu::TestStatus::pass("OK") : tcu::TestStatus::fail("Image comparison failed"));
669 }
670 }
671
672 struct TestCaseDescription
673 {
674 const char* name;
675 const char* desc;
676 Flags flags;
677 };
678
679 } // anonymous
680
681 //! Ported from dEQP-GLES31.functional.tessellation_geometry_interaction.render.limits.*
682 //! \note Tests that check implementation defined limits were omitted, because they rely on runtime shader source generation
683 //! (e.g. changing the number of vertices output from geometry shader). CTS currently doesn't support that,
684 //! because some platforms require precompiled shaders.
createGeometryGridRenderLimitsTests(tcu::TestContext & testCtx)685 tcu::TestCaseGroup* createGeometryGridRenderLimitsTests (tcu::TestContext& testCtx)
686 {
687 de::MovePtr<tcu::TestCaseGroup> group (new tcu::TestCaseGroup(testCtx, "limits", "Render with properties near their limits"));
688
689 static const TestCaseDescription cases[] =
690 {
691 {
692 "output_required_max_tessellation",
693 "Minimum maximum tessellation level",
694 FLAG_TESSELLATION_MAX_SPEC
695 },
696 {
697 "output_required_max_geometry",
698 "Output minimum maximum number of vertices the geometry shader",
699 FLAG_GEOMETRY_MAX_SPEC
700 },
701 {
702 "output_required_max_invocations",
703 "Minimum maximum number of geometry shader invocations",
704 FLAG_GEOMETRY_INVOCATIONS_MAX_SPEC
705 },
706 };
707
708 for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(cases); ++ndx)
709 group->addChild(new GridRenderTestCase(testCtx, cases[ndx].name, cases[ndx].desc, cases[ndx].flags));
710
711 return group.release();
712 }
713
714 //! Ported from dEQP-GLES31.functional.tessellation_geometry_interaction.render.scatter.*
createGeometryGridRenderScatterTests(tcu::TestContext & testCtx)715 tcu::TestCaseGroup* createGeometryGridRenderScatterTests (tcu::TestContext& testCtx)
716 {
717 de::MovePtr<tcu::TestCaseGroup> group (new tcu::TestCaseGroup(testCtx, "scatter", "Scatter output primitives"));
718
719 static const TestCaseDescription cases[] =
720 {
721 {
722 "geometry_scatter_instances",
723 "Each geometry shader instance outputs its primitives far from other instances of the same execution",
724 FLAG_GEOMETRY_SCATTER_INSTANCES
725 },
726 {
727 "geometry_scatter_primitives",
728 "Each geometry shader instance outputs its primitives far from other primitives of the same instance",
729 FLAG_GEOMETRY_SCATTER_PRIMITIVES | FLAG_GEOMETRY_SEPARATE_PRIMITIVES
730 },
731 {
732 "geometry_scatter_layers",
733 "Each geometry shader instance outputs its primitives to multiple layers and far from other primitives of the same instance",
734 FLAG_GEOMETRY_SCATTER_LAYERS | FLAG_GEOMETRY_SEPARATE_PRIMITIVES
735 },
736 };
737
738 for (int ndx = 0; ndx < DE_LENGTH_OF_ARRAY(cases); ++ndx)
739 group->addChild(new GridRenderTestCase(testCtx, cases[ndx].name, cases[ndx].desc, cases[ndx].flags));
740
741 return group.release();
742 }
743
744 } // tessellation
745 } // vkt
746