1 /*------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
5 * Copyright (c) 2016 The Khronos Group Inc.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * \brief Clipping tests
22 *//*--------------------------------------------------------------------*/
24 #include "vktClippingTests.hpp"
25 #include "vktTestCase.hpp"
26 #include "vktTestGroupUtil.hpp"
27 #include "vktTestCaseUtil.hpp"
28 #include "vktDrawUtil.hpp"
29 #include "vkRefUtil.hpp"
30 #include "vkTypeUtil.hpp"
31 #include "vkImageUtil.hpp"
32 #include "tcuTestLog.hpp"
33 #include "deUniquePtr.hpp"
34 #include "deStringUtil.hpp"
35 #include "deRandom.hpp"
48 using namespace drawutil;
53 RENDER_SIZE_LARGE = 128,
54 NUM_RENDER_PIXELS = RENDER_SIZE * RENDER_SIZE,
55 NUM_PATCH_CONTROL_POINTS = 3,
56 MAX_CLIP_DISTANCES = 8,
57 MAX_CULL_DISTANCES = 8,
58 MAX_COMBINED_CLIP_AND_CULL_DISTANCES = 8,
63 FEATURE_TESSELLATION_SHADER = 1u << 0,
64 FEATURE_GEOMETRY_SHADER = 1u << 1,
65 FEATURE_SHADER_FLOAT_64 = 1u << 2,
66 FEATURE_VERTEX_PIPELINE_STORES_AND_ATOMICS = 1u << 3,
67 FEATURE_FRAGMENT_STORES_AND_ATOMICS = 1u << 4,
68 FEATURE_SHADER_TESSELLATION_AND_GEOMETRY_POINT_SIZE = 1u << 5,
69 FEATURE_DEPTH_CLAMP = 1u << 6,
70 FEATURE_LARGE_POINTS = 1u << 7,
71 FEATURE_WIDE_LINES = 1u << 8,
72 FEATURE_SHADER_CLIP_DISTANCE = 1u << 9,
73 FEATURE_SHADER_CULL_DISTANCE = 1u << 10,
75 typedef deUint32 FeatureFlags;
77 void requireFeatures (const InstanceInterface& vki, const VkPhysicalDevice physDevice, const FeatureFlags flags)
79 const VkPhysicalDeviceFeatures features = getPhysicalDeviceFeatures(vki, physDevice);
81 if (((flags & FEATURE_TESSELLATION_SHADER) != 0) && !features.tessellationShader)
82 throw tcu::NotSupportedError("Tessellation shader not supported");
84 if (((flags & FEATURE_GEOMETRY_SHADER) != 0) && !features.geometryShader)
85 throw tcu::NotSupportedError("Geometry shader not supported");
87 if (((flags & FEATURE_SHADER_FLOAT_64) != 0) && !features.shaderFloat64)
88 throw tcu::NotSupportedError("Double-precision floats not supported");
90 if (((flags & FEATURE_VERTEX_PIPELINE_STORES_AND_ATOMICS) != 0) && !features.vertexPipelineStoresAndAtomics)
91 throw tcu::NotSupportedError("SSBO and image writes not supported in vertex pipeline");
93 if (((flags & FEATURE_FRAGMENT_STORES_AND_ATOMICS) != 0) && !features.fragmentStoresAndAtomics)
94 throw tcu::NotSupportedError("SSBO and image writes not supported in fragment shader");
96 if (((flags & FEATURE_SHADER_TESSELLATION_AND_GEOMETRY_POINT_SIZE) != 0) && !features.shaderTessellationAndGeometryPointSize)
97 throw tcu::NotSupportedError("Tessellation and geometry shaders don't support PointSize built-in");
99 if (((flags & FEATURE_DEPTH_CLAMP) != 0) && !features.depthClamp)
100 throw tcu::NotSupportedError("Depth clamp not supported");
102 if (((flags & FEATURE_LARGE_POINTS) != 0) && !features.largePoints)
103 throw tcu::NotSupportedError("Large points not supported");
105 if (((flags & FEATURE_WIDE_LINES) != 0) && !features.wideLines)
106 throw tcu::NotSupportedError("Wide lines not supported");
108 if (((flags & FEATURE_SHADER_CLIP_DISTANCE) != 0) && !features.shaderClipDistance)
109 throw tcu::NotSupportedError("Shader ClipDistance not supported");
111 if (((flags & FEATURE_SHADER_CULL_DISTANCE) != 0) && !features.shaderCullDistance)
112 throw tcu::NotSupportedError("Shader CullDistance not supported");
115 std::vector<Vec4> genVertices (const VkPrimitiveTopology topology, const Vec4& offset, const float slope)
117 const float p = 1.0f;
118 const float hp = 0.5f;
119 const float z = 0.0f;
120 const float w = 1.0f;
122 std::vector<Vec4> vertices;
124 // We're setting adjacent vertices to zero where needed, as we don't use them in meaningful way.
128 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
129 vertices.push_back(offset + Vec4(0.0f, 0.0f, slope/2.0f + z, w));
130 vertices.push_back(offset + Vec4( -hp, -hp, z, w));
131 vertices.push_back(offset + Vec4( hp, -hp, slope + z, w));
132 vertices.push_back(offset + Vec4( -hp, hp, z, w));
133 vertices.push_back(offset + Vec4( hp, hp, slope + z, w));
136 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
137 vertices.push_back(offset + Vec4(-p, -p, z, w));
138 vertices.push_back(offset + Vec4( p, p, slope + z, w)); // line 0
139 vertices.push_back(offset + Vec4( p, p, slope + z, w));
140 vertices.push_back(offset + Vec4( p, -p, slope + z, w)); // line 1
141 vertices.push_back(offset + Vec4( p, -p, slope + z, w));
142 vertices.push_back(offset + Vec4(-p, p, z, w)); // line 2
145 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
146 vertices.push_back(Vec4());
147 vertices.push_back(offset + Vec4(-p, -p, z, w));
148 vertices.push_back(offset + Vec4( p, p, slope + z, w)); // line 0
149 vertices.push_back(Vec4());
150 vertices.push_back(Vec4());
151 vertices.push_back(offset + Vec4( p, p, slope + z, w));
152 vertices.push_back(offset + Vec4( p, -p, slope + z, w)); // line 1
153 vertices.push_back(Vec4());
154 vertices.push_back(Vec4());
155 vertices.push_back(offset + Vec4( p, -p, slope + z, w));
156 vertices.push_back(offset + Vec4(-p, p, z, w)); // line 2
157 vertices.push_back(Vec4());
160 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
161 vertices.push_back(offset + Vec4(-p, -p, z, w));
162 vertices.push_back(offset + Vec4( p, p, slope + z, w)); // line 0
163 vertices.push_back(offset + Vec4( p, -p, slope + z, w)); // line 1
164 vertices.push_back(offset + Vec4(-p, p, z, w)); // line 2
167 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
168 vertices.push_back(Vec4());
169 vertices.push_back(offset + Vec4(-p, -p, z, w));
170 vertices.push_back(offset + Vec4( p, p, slope + z, w)); // line 0
171 vertices.push_back(offset + Vec4( p, -p, slope + z, w)); // line 1
172 vertices.push_back(offset + Vec4(-p, p, z, w)); // line 2
173 vertices.push_back(Vec4());
176 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
177 vertices.push_back(offset + Vec4( p, -p, slope + z, w));
178 vertices.push_back(offset + Vec4(-p, -p, z, w));
179 vertices.push_back(offset + Vec4(-p, p, z, w)); // triangle 0
180 vertices.push_back(offset + Vec4(-p, p, z, w));
181 vertices.push_back(offset + Vec4( p, p, slope + z, w));
182 vertices.push_back(offset + Vec4( p, -p, slope + z, w)); // triangle 1
185 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
186 vertices.push_back(offset + Vec4( p, -p, slope + z, w));
187 vertices.push_back(Vec4());
188 vertices.push_back(offset + Vec4(-p, -p, z, w));
189 vertices.push_back(Vec4());
190 vertices.push_back(offset + Vec4(-p, p, z, w)); // triangle 0
191 vertices.push_back(Vec4());
192 vertices.push_back(offset + Vec4(-p, p, z, w));
193 vertices.push_back(Vec4());
194 vertices.push_back(offset + Vec4( p, p, slope + z, w));
195 vertices.push_back(Vec4());
196 vertices.push_back(offset + Vec4( p, -p, slope + z, w)); // triangle 1
197 vertices.push_back(Vec4());
200 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
201 vertices.push_back(offset + Vec4(-p, -p, z, w));
202 vertices.push_back(offset + Vec4(-p, p, z, w));
203 vertices.push_back(offset + Vec4( p, -p, slope + z, w)); // triangle 0
204 vertices.push_back(offset + Vec4( p, p, slope + z, w)); // triangle 1
207 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
208 vertices.push_back(offset + Vec4(-p, -p, z, w));
209 vertices.push_back(Vec4());
210 vertices.push_back(offset + Vec4(-p, p, z, w));
211 vertices.push_back(Vec4());
212 vertices.push_back(offset + Vec4( p, -p, slope + z, w)); // triangle 0
213 vertices.push_back(Vec4());
214 vertices.push_back(offset + Vec4( p, p, slope + z, w)); // triangle 1
215 vertices.push_back(Vec4());
218 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
219 vertices.push_back(offset + Vec4( p, -p, slope + z, w));
220 vertices.push_back(offset + Vec4(-p, -p, z, w));
221 vertices.push_back(offset + Vec4(-p, p, z, w)); // triangle 0
222 vertices.push_back(offset + Vec4( p, p, slope + z, w)); // triangle 1
225 case VK_PRIMITIVE_TOPOLOGY_PATCH_LIST:
236 bool inline isColorInRange (const Vec4& color, const Vec4& minColor, const Vec4& maxColor)
238 return (minColor.x() <= color.x() && color.x() <= maxColor.x())
239 && (minColor.y() <= color.y() && color.y() <= maxColor.y())
240 && (minColor.z() <= color.z() && color.z() <= maxColor.z())
241 && (minColor.w() <= color.w() && color.w() <= maxColor.w());
244 //! Count pixels that match color within threshold, in the specified region.
245 int countPixels (const tcu::ConstPixelBufferAccess pixels, const IVec2& regionOffset, const IVec2& regionSize, const Vec4& color, const Vec4& colorThreshold)
247 const Vec4 minColor = color - colorThreshold;
248 const Vec4 maxColor = color + colorThreshold;
249 const int xEnd = regionOffset.x() + regionSize.x();
250 const int yEnd = regionOffset.y() + regionSize.y();
253 DE_ASSERT(xEnd <= pixels.getWidth());
254 DE_ASSERT(yEnd <= pixels.getHeight());
256 for (int y = regionOffset.y(); y < yEnd; ++y)
257 for (int x = regionOffset.x(); x < xEnd; ++x)
259 if (isColorInRange(pixels.getPixel(x, y), minColor, maxColor))
266 int countPixels (const tcu::ConstPixelBufferAccess pixels, const Vec4& color, const Vec4& colorThreshold)
268 return countPixels(pixels, IVec2(), IVec2(pixels.getWidth(), pixels.getHeight()), color, colorThreshold);
271 //! Clipping against the default clip volume.
275 //! Used by wide lines test.
278 LINE_ORIENTATION_AXIS_ALIGNED,
279 LINE_ORIENTATION_DIAGONAL,
282 const VkPointClippingBehaviorKHR invalidClippingBehavior = VK_POINT_CLIPPING_BEHAVIOR_KHR_LAST;
284 VkPointClippingBehaviorKHR getClippingBehavior (const InstanceInterface& vk, VkPhysicalDevice physicalDevice)
286 VkPhysicalDevicePointClippingPropertiesKHR behaviorProperties =
288 VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_POINT_CLIPPING_PROPERTIES_KHR, // VkStructureType sType
289 DE_NULL, // void* pNext
290 invalidClippingBehavior // VkPointClippingBehaviorKHR pointClippingBehavior
292 VkPhysicalDeviceProperties2KHR properties2;
294 DE_ASSERT(getPointClippingBehaviorKHRName(invalidClippingBehavior) == DE_NULL);
296 deMemset(&properties2, 0, sizeof(properties2));
298 properties2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2_KHR;
299 properties2.pNext = &behaviorProperties;
301 vk.getPhysicalDeviceProperties2KHR(physicalDevice, &properties2);
303 return behaviorProperties.pointClippingBehavior;
306 void addSimplePrograms (SourceCollections& programCollection, const float pointSize = 0.0f)
310 const bool usePointSize = pointSize > 0.0f;
312 std::ostringstream src;
313 src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_450) << "\n"
315 << "layout(location = 0) in vec4 v_position;\n"
317 << "out gl_PerVertex {\n"
318 << " vec4 gl_Position;\n"
319 << (usePointSize ? " float gl_PointSize;\n" : "")
322 << "void main (void)\n"
324 << " gl_Position = v_position;\n"
325 << (usePointSize ? " gl_PointSize = " + de::floatToString(pointSize, 1) + ";\n" : "")
328 programCollection.glslSources.add("vert") << glu::VertexSource(src.str());
333 std::ostringstream src;
334 src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_450) << "\n"
336 << "layout(location = 0) out vec4 o_color;\n"
338 << "void main (void)\n"
340 << " o_color = vec4(1.0, gl_FragCoord.z, 0.0, 1.0);\n"
343 programCollection.glslSources.add("frag") << glu::FragmentSource(src.str());
347 void initPrograms (SourceCollections& programCollection, const VkPrimitiveTopology topology)
349 const float pointSize = (topology == VK_PRIMITIVE_TOPOLOGY_POINT_LIST ? 1.0f : 0.0f);
350 addSimplePrograms(programCollection, pointSize);
353 void initPrograms (SourceCollections& programCollection, const LineOrientation lineOrientation)
355 DE_UNREF(lineOrientation);
356 addSimplePrograms(programCollection);
359 void initProgramsPointSize (SourceCollections& programCollection)
361 addSimplePrograms(programCollection, 0.75f * RENDER_SIZE);
364 //! Primitives fully inside the clip volume.
365 tcu::TestStatus testPrimitivesInside (Context& context, const VkPrimitiveTopology topology)
367 int minExpectedBlackPixels = 0;
371 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
372 // We draw only 5 points.
373 minExpectedBlackPixels = NUM_RENDER_PIXELS - 5;
376 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
377 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
378 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
379 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
380 // Allow for some error.
381 minExpectedBlackPixels = NUM_RENDER_PIXELS - 3 * RENDER_SIZE;
384 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
385 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
386 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
387 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
388 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
389 // All render area should be covered.
390 minExpectedBlackPixels = 0;
398 std::vector<VulkanShader> shaders;
399 shaders.push_back(VulkanShader(VK_SHADER_STAGE_VERTEX_BIT, context.getBinaryCollection().get("vert")));
400 shaders.push_back(VulkanShader(VK_SHADER_STAGE_FRAGMENT_BIT, context.getBinaryCollection().get("frag")));
402 tcu::TestLog& log = context.getTestContext().getLog();
407 const char* const desc;
411 { "Draw primitives at near clipping plane, z = 0.0", 0.0f, },
412 { "Draw primitives at z = 0.5", 0.5f, },
413 { "Draw primitives at far clipping plane, z = 1.0", 1.0f, },
416 for (int caseNdx = 0; caseNdx < DE_LENGTH_OF_ARRAY(cases); ++caseNdx)
418 log << tcu::TestLog::Message << cases[caseNdx].desc << tcu::TestLog::EndMessage;
420 const std::vector<Vec4> vertices = genVertices(topology, Vec4(0.0f, 0.0f, cases[caseNdx].zPos, 0.0f), 0.0f);
421 DrawState drawState (topology, RENDER_SIZE, RENDER_SIZE);
422 DrawCallData drawCallData (vertices);
423 VulkanProgram vulkanProgram (shaders);
425 VulkanDrawContext drawContext(context, drawState, drawCallData, vulkanProgram);
428 const int numBlackPixels = countPixels(drawContext.getColorPixels(), Vec4(0.0f, 0.0f, 0.0f, 1.0f), Vec4());
429 if (numBlackPixels >= minExpectedBlackPixels)
433 return (numPassed == DE_LENGTH_OF_ARRAY(cases) ? tcu::TestStatus::pass("OK") : tcu::TestStatus::fail("Rendered image(s) are incorrect"));
436 //! Primitives fully outside the clip volume.
437 tcu::TestStatus testPrimitivesOutside (Context& context, const VkPrimitiveTopology topology)
439 std::vector<VulkanShader> shaders;
440 shaders.push_back(VulkanShader(VK_SHADER_STAGE_VERTEX_BIT, context.getBinaryCollection().get("vert")));
441 shaders.push_back(VulkanShader(VK_SHADER_STAGE_FRAGMENT_BIT, context.getBinaryCollection().get("frag")));
443 tcu::TestLog& log = context.getTestContext().getLog();
448 const char* const desc;
452 { "Draw primitives in front of the near clipping plane, z < 0.0", -0.5f, },
453 { "Draw primitives behind the far clipping plane, z > 1.0", 1.5f, },
456 log << tcu::TestLog::Message << "Drawing primitives outside the clip volume. Expecting an empty image." << tcu::TestLog::EndMessage;
458 for (int caseNdx = 0; caseNdx < DE_LENGTH_OF_ARRAY(cases); ++caseNdx)
460 log << tcu::TestLog::Message << cases[caseNdx].desc << tcu::TestLog::EndMessage;
462 const std::vector<Vec4> vertices = genVertices(topology, Vec4(0.0f, 0.0f, cases[caseNdx].zPos, 0.0f), 0.0f);
463 DrawState drawState (topology, RENDER_SIZE, RENDER_SIZE);
464 DrawCallData drawCallData (vertices);
465 VulkanProgram vulkanProgram (shaders);
467 VulkanDrawContext drawContext(context, drawState, drawCallData, vulkanProgram);
470 // All pixels must be black -- nothing is drawn.
471 const int numBlackPixels = countPixels(drawContext.getColorPixels(), Vec4(0.0f, 0.0f, 0.0f, 1.0f), Vec4());
472 if (numBlackPixels == NUM_RENDER_PIXELS)
476 return (numPassed == DE_LENGTH_OF_ARRAY(cases) ? tcu::TestStatus::pass("OK") : tcu::TestStatus::fail("Rendered image(s) are incorrect"));
479 //! Primitives partially outside the clip volume, but depth clamped
480 tcu::TestStatus testPrimitivesDepthClamp (Context& context, const VkPrimitiveTopology topology)
482 requireFeatures(context.getInstanceInterface(), context.getPhysicalDevice(), FEATURE_DEPTH_CLAMP);
484 std::vector<VulkanShader> shaders;
485 shaders.push_back(VulkanShader(VK_SHADER_STAGE_VERTEX_BIT, context.getBinaryCollection().get("vert")));
486 shaders.push_back(VulkanShader(VK_SHADER_STAGE_FRAGMENT_BIT, context.getBinaryCollection().get("frag")));
488 const int numCases = 4;
489 const IVec2 regionSize = IVec2(RENDER_SIZE/2, RENDER_SIZE); //! size of the clamped region
490 const int regionPixels = regionSize.x() * regionSize.y();
491 tcu::TestLog& log = context.getTestContext().getLog();
496 const char* const desc;
498 bool depthClampEnable;
503 { "Draw primitives intersecting the near clipping plane, depth clamp disabled", -0.5f, false, IVec2(0, 0), Vec4(0.0f, 0.0f, 0.0f, 1.0f) },
504 { "Draw primitives intersecting the near clipping plane, depth clamp enabled", -0.5f, true, IVec2(0, 0), Vec4(1.0f, 0.0f, 0.0f, 1.0f) },
505 { "Draw primitives intersecting the far clipping plane, depth clamp disabled", 0.5f, false, IVec2(RENDER_SIZE/2, 0), Vec4(0.0f, 0.0f, 0.0f, 1.0f) },
506 { "Draw primitives intersecting the far clipping plane, depth clamp enabled", 0.5f, true, IVec2(RENDER_SIZE/2, 0), Vec4(1.0f, 1.0f, 0.0f, 1.0f) },
509 // Per case minimum number of colored pixels.
510 int caseMinPixels[numCases] = { 0, 0, 0, 0 };
514 case VK_PRIMITIVE_TOPOLOGY_POINT_LIST:
515 caseMinPixels[0] = caseMinPixels[2] = regionPixels - 1;
516 caseMinPixels[1] = caseMinPixels[3] = 2;
519 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST:
520 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP:
521 case VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY:
522 case VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY:
523 caseMinPixels[0] = regionPixels;
524 caseMinPixels[1] = RENDER_SIZE - 2;
525 caseMinPixels[2] = regionPixels;
526 caseMinPixels[3] = 2 * (RENDER_SIZE - 2);
529 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST:
530 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP:
531 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN:
532 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY:
533 case VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY:
534 caseMinPixels[0] = caseMinPixels[1] = caseMinPixels[2] = caseMinPixels[3] = regionPixels;
542 for (int caseNdx = 0; caseNdx < numCases; ++caseNdx)
544 log << tcu::TestLog::Message << cases[caseNdx].desc << tcu::TestLog::EndMessage;
546 const std::vector<Vec4> vertices = genVertices(topology, Vec4(0.0f, 0.0f, cases[caseNdx].zPos, 0.0f), 1.0f);
548 DrawState drawState (topology, RENDER_SIZE, RENDER_SIZE);
549 DrawCallData drawCallData (vertices);
550 VulkanProgram vulkanProgram (shaders);
551 drawState.depthClampEnable = cases[caseNdx].depthClampEnable;
553 VulkanDrawContext drawContext(context, drawState, drawCallData, vulkanProgram);
556 const int numPixels = countPixels(drawContext.getColorPixels(), cases[caseNdx].regionOffset, regionSize, cases[caseNdx].color, Vec4());
558 if (numPixels >= caseMinPixels[caseNdx])
562 return (numPassed == numCases ? tcu::TestStatus::pass("OK") : tcu::TestStatus::fail("Rendered image(s) are incorrect"));
565 //! Large point clipping
566 //! Spec: If the primitive under consideration is a point, then clipping passes it unchanged if it lies within the clip volume;
567 //! otherwise, it is discarded.
568 tcu::TestStatus testLargePoints (Context& context)
570 requireFeatures(context.getInstanceInterface(), context.getPhysicalDevice(), FEATURE_LARGE_POINTS);
572 bool pointClippingOutside = true;
574 if (de::contains(context.getDeviceExtensions().begin(), context.getDeviceExtensions().end(), "VK_KHR_maintenance2"))
576 VkPointClippingBehaviorKHR clippingBehavior = getClippingBehavior(context.getInstanceInterface(), context.getPhysicalDevice());
578 switch (clippingBehavior)
580 case VK_POINT_CLIPPING_BEHAVIOR_ALL_CLIP_PLANES_KHR: pointClippingOutside = true; break;
581 case VK_POINT_CLIPPING_BEHAVIOR_USER_CLIP_PLANES_ONLY_KHR: pointClippingOutside = false; break;
582 case invalidClippingBehavior: TCU_FAIL("Clipping behavior read failure"); break;
585 TCU_FAIL("Unexpected clipping behavior reported");
590 std::vector<VulkanShader> shaders;
591 shaders.push_back(VulkanShader(VK_SHADER_STAGE_VERTEX_BIT, context.getBinaryCollection().get("vert")));
592 shaders.push_back(VulkanShader(VK_SHADER_STAGE_FRAGMENT_BIT, context.getBinaryCollection().get("frag")));
594 std::vector<Vec4> vertices;
596 const float delta = 0.1f; // much smaller than the point size
597 const float p = 1.0f + delta;
599 vertices.push_back(Vec4( -p, -p, 0.1f, 1.0f));
600 vertices.push_back(Vec4( -p, p, 0.2f, 1.0f));
601 vertices.push_back(Vec4( p, p, 0.4f, 1.0f));
602 vertices.push_back(Vec4( p, -p, 0.6f, 1.0f));
603 vertices.push_back(Vec4(0.0f, -p, 0.8f, 1.0f));
604 vertices.push_back(Vec4( p, 0.0f, 0.7f, 1.0f));
605 vertices.push_back(Vec4(0.0f, p, 0.5f, 1.0f));
606 vertices.push_back(Vec4( -p, 0.0f, 0.3f, 1.0f));
609 tcu::TestLog& log = context.getTestContext().getLog();
611 log << tcu::TestLog::Message << "Drawing several large points just outside the clip volume. Expecting an empty image or all points rendered." << tcu::TestLog::EndMessage;
613 DrawState drawState (VK_PRIMITIVE_TOPOLOGY_POINT_LIST, RENDER_SIZE, RENDER_SIZE);
614 DrawCallData drawCallData (vertices);
615 VulkanProgram vulkanProgram (shaders);
617 VulkanDrawContext drawContext(context, drawState, drawCallData, vulkanProgram);
620 // Popful case: All pixels must be black -- nothing is drawn.
621 const int numBlackPixels = countPixels(drawContext.getColorPixels(), Vec4(0.0f, 0.0f, 0.0f, 1.0f), Vec4());
624 // Pop-free case: All points must be rendered.
625 bool allPointsRendered = true;
626 for (std::vector<Vec4>::iterator i = vertices.begin(); i != vertices.end(); ++i)
628 if (countPixels(drawContext.getColorPixels(), Vec4(1.0f, i->z(), 0.0f, 1.0f), Vec4(0.01f)) == 0)
629 allPointsRendered = false;
632 if (pointClippingOutside)
634 result = (numBlackPixels == NUM_RENDER_PIXELS || allPointsRendered);
638 // Rendering pixels without clipping: all points should be drawn.
639 result = (allPointsRendered == true);
642 return (result ? tcu::TestStatus::pass("OK") : tcu::TestStatus::fail("Rendered image(s) are incorrect"));
645 //! Wide line clipping
646 //! Spec: If the primitive is a line segment, then clipping does nothing to it if it lies entirely within the clip volume, and discards it
647 //! if it lies entirely outside the volume.
648 tcu::TestStatus testWideLines (Context& context, const LineOrientation lineOrientation)
650 requireFeatures(context.getInstanceInterface(), context.getPhysicalDevice(), FEATURE_WIDE_LINES);
652 std::vector<VulkanShader> shaders;
653 shaders.push_back(VulkanShader(VK_SHADER_STAGE_VERTEX_BIT, context.getBinaryCollection().get("vert")));
654 shaders.push_back(VulkanShader(VK_SHADER_STAGE_FRAGMENT_BIT, context.getBinaryCollection().get("frag")));
656 const float delta = 0.1f; // much smaller than the line width
658 std::vector<Vec4> vertices;
659 if (lineOrientation == LINE_ORIENTATION_AXIS_ALIGNED)
661 // Axis-aligned lines just outside the clip volume.
662 const float p = 1.0f + delta;
663 const float q = 0.9f;
665 vertices.push_back(Vec4(-p, -q, 0.1f, 1.0f));
666 vertices.push_back(Vec4(-p, q, 0.9f, 1.0f)); // line 0
667 vertices.push_back(Vec4(-q, p, 0.1f, 1.0f));
668 vertices.push_back(Vec4( q, p, 0.9f, 1.0f)); // line 1
669 vertices.push_back(Vec4( p, q, 0.1f, 1.0f));
670 vertices.push_back(Vec4( p, -q, 0.9f, 1.0f)); // line 2
671 vertices.push_back(Vec4( q, -p, 0.1f, 1.0f));
672 vertices.push_back(Vec4(-q, -p, 0.9f, 1.0f)); // line 3
674 else if (lineOrientation == LINE_ORIENTATION_DIAGONAL)
676 // Diagonal lines just outside the clip volume.
677 const float p = 2.0f + delta;
679 vertices.push_back(Vec4( -p, 0.0f, 0.1f, 1.0f));
680 vertices.push_back(Vec4(0.0f, -p, 0.9f, 1.0f)); // line 0
681 vertices.push_back(Vec4(0.0f, -p, 0.1f, 1.0f));
682 vertices.push_back(Vec4( p, 0.0f, 0.9f, 1.0f)); // line 1
683 vertices.push_back(Vec4( p, 0.0f, 0.1f, 1.0f));
684 vertices.push_back(Vec4(0.0f, p, 0.9f, 1.0f)); // line 2
685 vertices.push_back(Vec4(0.0f, p, 0.1f, 1.0f));
686 vertices.push_back(Vec4( -p, 0.0f, 0.9f, 1.0f)); // line 3
691 const VkPhysicalDeviceLimits limits = getPhysicalDeviceProperties(context.getInstanceInterface(), context.getPhysicalDevice()).limits;
693 const float lineWidth = std::min(static_cast<float>(RENDER_SIZE), limits.lineWidthRange[1]);
694 tcu::TestLog& log = context.getTestContext().getLog();
696 log << tcu::TestLog::Message << "Drawing several wide lines just outside the clip volume. Expecting an empty image." << tcu::TestLog::EndMessage
697 << tcu::TestLog::Message << "Line width is " << lineWidth << "." << tcu::TestLog::EndMessage;
699 DrawState drawState (VK_PRIMITIVE_TOPOLOGY_LINE_LIST, RENDER_SIZE, RENDER_SIZE);
700 DrawCallData drawCallData (vertices);
701 VulkanProgram vulkanProgram (shaders);
702 drawState.lineWidth = lineWidth;
704 VulkanDrawContext drawContext(context, drawState, drawCallData, vulkanProgram);
707 // All pixels must be black -- nothing is drawn.
708 const int numBlackPixels = countPixels(drawContext.getColorPixels(), Vec4(0.0f, 0.0f, 0.0f, 1.0f), Vec4());
710 return (numBlackPixels == NUM_RENDER_PIXELS ? tcu::TestStatus::pass("OK") : tcu::TestStatus::fail("Rendered image(s) are incorrect"));
715 namespace ClipDistance
718 struct CaseDefinition
720 const VkPrimitiveTopology topology;
721 const bool dynamicIndexing;
722 const bool enableTessellation;
723 const bool enableGeometry;
724 const int numClipDistances;
725 const int numCullDistances;
727 CaseDefinition (const VkPrimitiveTopology topology_,
728 const int numClipDistances_,
729 const int numCullDistances_,
730 const bool enableTessellation_,
731 const bool enableGeometry_,
732 const bool dynamicIndexing_)
733 : topology (topology_)
734 , dynamicIndexing (dynamicIndexing_)
735 , enableTessellation (enableTessellation_)
736 , enableGeometry (enableGeometry_)
737 , numClipDistances (numClipDistances_)
738 , numCullDistances (numCullDistances_)
743 void initPrograms (SourceCollections& programCollection, const CaseDefinition caseDef)
745 DE_ASSERT(caseDef.numClipDistances + caseDef.numCullDistances <= MAX_COMBINED_CLIP_AND_CULL_DISTANCES);
747 std::string perVertexBlock;
749 std::ostringstream str;
750 str << "gl_PerVertex {\n"
751 << " vec4 gl_Position;\n";
752 if (caseDef.numClipDistances > 0)
753 str << " float gl_ClipDistance[" << caseDef.numClipDistances << "];\n";
754 if (caseDef.numCullDistances > 0)
755 str << " float gl_CullDistance[" << caseDef.numCullDistances << "];\n";
757 perVertexBlock = str.str();
762 std::ostringstream src;
763 src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_450) << "\n"
765 << "layout(location = 0) in vec4 v_position;\n"
766 << "layout(location = 0) out vec4 out_color;\n"
768 << "out " << perVertexBlock << ";\n"
770 << "void main (void)\n"
772 << " gl_Position = v_position;\n"
773 << " out_color = vec4(1.0, 0.5 * (v_position.x + 1.0), 0.0, 1.0);\n"
775 << " const int barNdx = gl_VertexIndex / 6;\n";
776 if (caseDef.dynamicIndexing)
778 if (caseDef.numClipDistances > 0)
779 src << " for (int i = 0; i < " << caseDef.numClipDistances << "; ++i)\n"
780 << " gl_ClipDistance[i] = (barNdx == i ? v_position.y : 0.0);\n";
781 if (caseDef.numCullDistances > 0)
782 src << " for (int i = 0; i < " << caseDef.numCullDistances << "; ++i)\n"
783 << " gl_CullDistance[i] = 0.0;\n";
787 for (int i = 0; i < caseDef.numClipDistances; ++i)
788 src << " gl_ClipDistance[" << i << "] = (barNdx == " << i << " ? v_position.y : 0.0);\n";
789 for (int i = 0; i < caseDef.numCullDistances; ++i)
790 src << " gl_CullDistance[" << i << "] = 0.0;\n"; // don't cull anything
794 programCollection.glslSources.add("vert") << glu::VertexSource(src.str());
797 if (caseDef.enableTessellation)
799 std::ostringstream src;
800 src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_450) << "\n"
802 << "layout(vertices = " << NUM_PATCH_CONTROL_POINTS << ") out;\n"
804 << "layout(location = 0) in vec4 in_color[];\n"
805 << "layout(location = 0) out vec4 out_color[];\n"
807 << "in " << perVertexBlock << " gl_in[gl_MaxPatchVertices];\n"
809 << "out " << perVertexBlock << " gl_out[];\n"
811 << "void main (void)\n"
813 << " gl_TessLevelInner[0] = 1.0;\n"
814 << " gl_TessLevelInner[1] = 1.0;\n"
816 << " gl_TessLevelOuter[0] = 1.0;\n"
817 << " gl_TessLevelOuter[1] = 1.0;\n"
818 << " gl_TessLevelOuter[2] = 1.0;\n"
819 << " gl_TessLevelOuter[3] = 1.0;\n"
821 << " gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;\n"
822 << " out_color[gl_InvocationID] = in_color[gl_InvocationID];\n"
824 if (caseDef.dynamicIndexing)
826 if (caseDef.numClipDistances > 0)
827 src << " for (int i = 0; i < " << caseDef.numClipDistances << "; ++i)\n"
828 << " gl_out[gl_InvocationID].gl_ClipDistance[i] = gl_in[gl_InvocationID].gl_ClipDistance[i];\n";
829 if (caseDef.numCullDistances > 0)
830 src << " for (int i = 0; i < " << caseDef.numCullDistances << "; ++i)\n"
831 << " gl_out[gl_InvocationID].gl_CullDistance[i] = gl_in[gl_InvocationID].gl_CullDistance[i];\n";
835 for (int i = 0; i < caseDef.numClipDistances; ++i)
836 src << " gl_out[gl_InvocationID].gl_ClipDistance[" << i << "] = gl_in[gl_InvocationID].gl_ClipDistance[" << i << "];\n";
837 for (int i = 0; i < caseDef.numCullDistances; ++i)
838 src << " gl_out[gl_InvocationID].gl_CullDistance[" << i << "] = gl_in[gl_InvocationID].gl_CullDistance[" << i << "];\n";
842 programCollection.glslSources.add("tesc") << glu::TessellationControlSource(src.str());
845 if (caseDef.enableTessellation)
847 DE_ASSERT(NUM_PATCH_CONTROL_POINTS == 3); // assumed in shader code
849 std::ostringstream src;
850 src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_450) << "\n"
852 << "layout(triangles, equal_spacing, ccw) in;\n"
854 << "layout(location = 0) in vec4 in_color[];\n"
855 << "layout(location = 0) out vec4 out_color;\n"
857 << "in " << perVertexBlock << " gl_in[gl_MaxPatchVertices];\n"
859 << "out " << perVertexBlock << ";\n"
861 << "void main (void)\n"
863 << " vec3 px = gl_TessCoord.x * gl_in[0].gl_Position.xyz;\n"
864 << " vec3 py = gl_TessCoord.y * gl_in[1].gl_Position.xyz;\n"
865 << " vec3 pz = gl_TessCoord.z * gl_in[2].gl_Position.xyz;\n"
866 << " gl_Position = vec4(px + py + pz, 1.0);\n"
867 << " out_color = (in_color[0] + in_color[1] + in_color[2]) / 3.0;\n"
869 if (caseDef.dynamicIndexing)
871 if (caseDef.numClipDistances > 0)
872 src << " for (int i = 0; i < " << caseDef.numClipDistances << "; ++i)\n"
873 << " gl_ClipDistance[i] = gl_TessCoord.x * gl_in[0].gl_ClipDistance[i]\n"
874 << " + gl_TessCoord.y * gl_in[1].gl_ClipDistance[i]\n"
875 << " + gl_TessCoord.z * gl_in[2].gl_ClipDistance[i];\n";
876 if (caseDef.numCullDistances > 0)
877 src << " for (int i = 0; i < " << caseDef.numCullDistances << "; ++i)\n"
878 << " gl_CullDistance[i] = gl_TessCoord.x * gl_in[0].gl_CullDistance[i]\n"
879 << " + gl_TessCoord.y * gl_in[1].gl_CullDistance[i]\n"
880 << " + gl_TessCoord.z * gl_in[2].gl_CullDistance[i];\n";
884 for (int i = 0; i < caseDef.numClipDistances; ++i)
885 src << " gl_ClipDistance[" << i << "] = gl_TessCoord.x * gl_in[0].gl_ClipDistance[" << i << "]\n"
886 << " + gl_TessCoord.y * gl_in[1].gl_ClipDistance[" << i << "]\n"
887 << " + gl_TessCoord.z * gl_in[2].gl_ClipDistance[" << i << "];\n";
888 for (int i = 0; i < caseDef.numCullDistances; ++i)
889 src << " gl_CullDistance[" << i << "] = gl_TessCoord.x * gl_in[0].gl_CullDistance[" << i << "]\n"
890 << " + gl_TessCoord.y * gl_in[1].gl_CullDistance[" << i << "]\n"
891 << " + gl_TessCoord.z * gl_in[2].gl_CullDistance[" << i << "];\n";
895 programCollection.glslSources.add("tese") << glu::TessellationEvaluationSource(src.str());
898 if (caseDef.enableGeometry)
900 std::ostringstream src;
901 src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_450) << "\n"
903 << "layout(triangles) in;\n"
904 << "layout(triangle_strip, max_vertices = 3) out;\n"
906 << "layout(location = 0) in vec4 in_color[];\n"
907 << "layout(location = 0) out vec4 out_color;\n"
909 << "in " << perVertexBlock << " gl_in[];\n"
911 << "out " << perVertexBlock << ";\n"
913 << "void main (void)\n"
915 for (int vertNdx = 0; vertNdx < 3; ++vertNdx)
919 src << " gl_Position = gl_in[" << vertNdx << "].gl_Position;\n"
920 << " out_color = in_color[" << vertNdx << "];\n";
921 if (caseDef.dynamicIndexing)
923 if (caseDef.numClipDistances > 0)
924 src << " for (int i = 0; i < " << caseDef.numClipDistances << "; ++i)\n"
925 << " gl_ClipDistance[i] = gl_in[" << vertNdx << "].gl_ClipDistance[i];\n";
926 if (caseDef.numCullDistances > 0)
927 src << " for (int i = 0; i < " << caseDef.numCullDistances << "; ++i)\n"
928 << " gl_CullDistance[i] = gl_in[" << vertNdx << "].gl_CullDistance[i];\n";
932 for (int i = 0; i < caseDef.numClipDistances; ++i)
933 src << " gl_ClipDistance[" << i << "] = gl_in[" << vertNdx << "].gl_ClipDistance[" << i << "];\n";
934 for (int i = 0; i < caseDef.numCullDistances; ++i)
935 src << " gl_CullDistance[" << i << "] = gl_in[" << vertNdx << "].gl_CullDistance[" << i << "];\n";
937 src << " EmitVertex();\n";
941 programCollection.glslSources.add("geom") << glu::GeometrySource(src.str());
946 std::ostringstream src;
947 src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_450) << "\n"
949 << "layout(location = 0) in flat vec4 in_color;\n"
950 << "layout(location = 0) out vec4 o_color;\n"
952 << "void main (void)\n"
954 << " o_color = vec4(in_color.rgb + vec3(0.0, 0.0, 0.5), 1.0);\n" // mix with a constant color in case variable wasn't passed correctly through stages
957 programCollection.glslSources.add("frag") << glu::FragmentSource(src.str());
961 tcu::TestStatus testClipDistance (Context& context, const CaseDefinition caseDef)
963 // Check test requirements
965 const InstanceInterface& vki = context.getInstanceInterface();
966 const VkPhysicalDevice physDevice = context.getPhysicalDevice();
967 const VkPhysicalDeviceLimits limits = getPhysicalDeviceProperties(vki, physDevice).limits;
969 FeatureFlags requirements = (FeatureFlags)0;
971 if (caseDef.numClipDistances > 0)
972 requirements |= FEATURE_SHADER_CLIP_DISTANCE;
973 if (caseDef.numCullDistances > 0)
974 requirements |= FEATURE_SHADER_CULL_DISTANCE;
975 if (caseDef.enableTessellation)
976 requirements |= FEATURE_TESSELLATION_SHADER;
977 if (caseDef.enableGeometry)
978 requirements |= FEATURE_GEOMETRY_SHADER;
980 requireFeatures(vki, physDevice, requirements);
982 // Check limits for supported features
984 if (caseDef.numClipDistances > 0 && limits.maxClipDistances < MAX_CLIP_DISTANCES)
985 return tcu::TestStatus::fail("maxClipDistances smaller than the minimum required by the spec");
986 if (caseDef.numCullDistances > 0 && limits.maxCullDistances < MAX_CULL_DISTANCES)
987 return tcu::TestStatus::fail("maxCullDistances smaller than the minimum required by the spec");
988 if (caseDef.numCullDistances > 0 && limits.maxCombinedClipAndCullDistances < MAX_COMBINED_CLIP_AND_CULL_DISTANCES)
989 return tcu::TestStatus::fail("maxCombinedClipAndCullDistances smaller than the minimum required by the spec");
992 std::vector<VulkanShader> shaders;
993 shaders.push_back(VulkanShader(VK_SHADER_STAGE_VERTEX_BIT, context.getBinaryCollection().get("vert")));
994 shaders.push_back(VulkanShader(VK_SHADER_STAGE_FRAGMENT_BIT, context.getBinaryCollection().get("frag")));
995 if (caseDef.enableTessellation)
997 shaders.push_back(VulkanShader(VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT, context.getBinaryCollection().get("tesc")));
998 shaders.push_back(VulkanShader(VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT, context.getBinaryCollection().get("tese")));
1000 if (caseDef.enableGeometry)
1001 shaders.push_back(VulkanShader(VK_SHADER_STAGE_GEOMETRY_BIT, context.getBinaryCollection().get("geom")));
1003 const int numBars = MAX_COMBINED_CLIP_AND_CULL_DISTANCES;
1005 std::vector<Vec4> vertices;
1007 const float dx = 2.0f / numBars;
1008 for (int i = 0; i < numBars; ++i)
1010 const float x = -1.0f + dx * static_cast<float>(i);
1012 vertices.push_back(Vec4(x, -1.0f, 0.0f, 1.0f));
1013 vertices.push_back(Vec4(x, 1.0f, 0.0f, 1.0f));
1014 vertices.push_back(Vec4(x + dx, -1.0f, 0.0f, 1.0f));
1016 vertices.push_back(Vec4(x, 1.0f, 0.0f, 1.0f));
1017 vertices.push_back(Vec4(x + dx, 1.0f, 0.0f, 1.0f));
1018 vertices.push_back(Vec4(x + dx, -1.0f, 0.0f, 1.0f));
1022 tcu::TestLog& log = context.getTestContext().getLog();
1024 log << tcu::TestLog::Message << "Drawing " << numBars << " colored bars, clipping the first " << caseDef.numClipDistances << tcu::TestLog::EndMessage
1025 << tcu::TestLog::Message << "Using " << caseDef.numClipDistances << " ClipDistance(s) and " << caseDef.numCullDistances << " CullDistance(s)" << tcu::TestLog::EndMessage
1026 << tcu::TestLog::Message << "Expecting upper half of the clipped bars to be black." << tcu::TestLog::EndMessage;
1028 DrawState drawState (caseDef.topology, RENDER_SIZE, RENDER_SIZE);
1029 DrawCallData drawCallData (vertices);
1030 VulkanProgram vulkanProgram (shaders);
1032 if (caseDef.enableTessellation)
1033 drawState.numPatchControlPoints = NUM_PATCH_CONTROL_POINTS;
1035 VulkanDrawContext drawContext(context, drawState, drawCallData, vulkanProgram);
1038 // Count black pixels in the whole image.
1039 const int numBlackPixels = countPixels(drawContext.getColorPixels(), Vec4(0.0f, 0.0f, 0.0f, 1.0f), Vec4());
1040 const IVec2 clipRegion = IVec2(caseDef.numClipDistances * RENDER_SIZE / numBars, RENDER_SIZE / 2);
1041 const int expectedClippedPixels = clipRegion.x() * clipRegion.y();
1042 // Make sure the bottom half has no black pixels (possible if image became corrupted).
1043 const int guardPixels = countPixels(drawContext.getColorPixels(), IVec2(0, RENDER_SIZE/2), clipRegion, Vec4(0.0f, 0.0f, 0.0f, 1.0f), Vec4());
1045 return (numBlackPixels == expectedClippedPixels && guardPixels == 0 ? tcu::TestStatus::pass("OK")
1046 : tcu::TestStatus::fail("Rendered image(s) are incorrect"));
1049 } // ClipDistance ns
1051 namespace ClipDistanceComplementarity
1054 void initPrograms (SourceCollections& programCollection, const int numClipDistances)
1058 DE_ASSERT(numClipDistances > 0);
1059 const int clipDistanceLastNdx = numClipDistances - 1;
1061 std::ostringstream src;
1062 src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_450) << "\n"
1064 << "layout(location = 0) in vec4 v_position; // we are passing ClipDistance in w component\n"
1066 << "out gl_PerVertex {\n"
1067 << " vec4 gl_Position;\n"
1068 << " float gl_ClipDistance[" << numClipDistances << "];\n"
1071 << "void main (void)\n"
1073 << " gl_Position = vec4(v_position.xyz, 1.0);\n";
1074 for (int i = 0; i < clipDistanceLastNdx; ++i)
1075 src << " gl_ClipDistance[" << i << "] = 0.0;\n";
1076 src << " gl_ClipDistance[" << clipDistanceLastNdx << "] = v_position.w;\n"
1079 programCollection.glslSources.add("vert") << glu::VertexSource(src.str());
1084 std::ostringstream src;
1085 src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_450) << "\n"
1087 << "layout(location = 0) out vec4 o_color;\n"
1089 << "void main (void)\n"
1091 << " o_color = vec4(1.0, 1.0, 1.0, 0.5);\n"
1094 programCollection.glslSources.add("frag") << glu::FragmentSource(src.str());
1098 tcu::TestStatus testComplementarity (Context& context, const int numClipDistances)
1100 // Check test requirements
1102 const InstanceInterface& vki = context.getInstanceInterface();
1103 const VkPhysicalDevice physDevice = context.getPhysicalDevice();
1105 requireFeatures(vki, physDevice, FEATURE_SHADER_CLIP_DISTANCE);
1108 std::vector<VulkanShader> shaders;
1109 shaders.push_back(VulkanShader(VK_SHADER_STAGE_VERTEX_BIT, context.getBinaryCollection().get("vert")));
1110 shaders.push_back(VulkanShader(VK_SHADER_STAGE_FRAGMENT_BIT, context.getBinaryCollection().get("frag")));
1112 std::vector<Vec4> vertices;
1114 de::Random rnd (1234);
1115 const int numSections = 16;
1116 const int numVerticesPerSection = 4; // logical verticies, due to triangle list topology we actually use 6 per section
1118 DE_ASSERT(RENDER_SIZE_LARGE % numSections == 0);
1120 std::vector<float> clipDistances(numVerticesPerSection * numSections);
1121 for (int i = 0; i < static_cast<int>(clipDistances.size()); ++i)
1122 clipDistances[i] = rnd.getFloat(-1.0f, 1.0f);
1124 // Two sets of identical primitives, but with a different ClipDistance sign.
1125 for (int setNdx = 0; setNdx < 2; ++setNdx)
1127 const float sign = (setNdx == 0 ? 1.0f : -1.0f);
1128 const float dx = 2.0f / static_cast<float>(numSections);
1130 for (int i = 0; i < numSections; ++i)
1132 const int ndxBase = numVerticesPerSection * i;
1133 const float x = -1.0f + dx * static_cast<float>(i);
1134 const Vec4 p0 = Vec4(x, -1.0f, 0.0f, sign * clipDistances[ndxBase + 0]);
1135 const Vec4 p1 = Vec4(x, 1.0f, 0.0f, sign * clipDistances[ndxBase + 1]);
1136 const Vec4 p2 = Vec4(x + dx, 1.0f, 0.0f, sign * clipDistances[ndxBase + 2]);
1137 const Vec4 p3 = Vec4(x + dx, -1.0f, 0.0f, sign * clipDistances[ndxBase + 3]);
1139 vertices.push_back(p0);
1140 vertices.push_back(p1);
1141 vertices.push_back(p2);
1143 vertices.push_back(p2);
1144 vertices.push_back(p3);
1145 vertices.push_back(p0);
1150 tcu::TestLog& log = context.getTestContext().getLog();
1152 log << tcu::TestLog::Message << "Draw two sets of primitives with blending, differing only with ClipDistance sign." << tcu::TestLog::EndMessage
1153 << tcu::TestLog::Message << "Using " << numClipDistances << " clipping plane(s), one of them possibly having negative values." << tcu::TestLog::EndMessage
1154 << tcu::TestLog::Message << "Expecting a uniform gray area, no missing (black) nor overlapped (white) pixels." << tcu::TestLog::EndMessage;
1156 DrawState drawState (VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST, RENDER_SIZE_LARGE, RENDER_SIZE_LARGE);
1157 DrawCallData drawCallData (vertices);
1158 VulkanProgram vulkanProgram (shaders);
1159 drawState.blendEnable = true;
1161 VulkanDrawContext drawContext(context, drawState, drawCallData, vulkanProgram);
1164 const int numGrayPixels = countPixels(drawContext.getColorPixels(), Vec4(0.5f, 0.5f, 0.5f, 1.0f), Vec4(0.02f, 0.02f, 0.02f, 0.0f));
1165 const int numExpectedPixels = RENDER_SIZE_LARGE * RENDER_SIZE_LARGE;
1167 return (numGrayPixels == numExpectedPixels ? tcu::TestStatus::pass("OK") : tcu::TestStatus::fail("Rendered image(s) are incorrect"));
1170 } // ClipDistanceComplementarity ns
1172 void addClippingTests (tcu::TestCaseGroup* clippingTestsGroup)
1174 tcu::TestContext& testCtx = clippingTestsGroup->getTestContext();
1176 // Clipping against the clip volume
1178 using namespace ClipVolume;
1180 static const VkPrimitiveTopology cases[] =
1182 VK_PRIMITIVE_TOPOLOGY_POINT_LIST,
1183 VK_PRIMITIVE_TOPOLOGY_LINE_LIST,
1184 VK_PRIMITIVE_TOPOLOGY_LINE_LIST_WITH_ADJACENCY,
1185 VK_PRIMITIVE_TOPOLOGY_LINE_STRIP,
1186 VK_PRIMITIVE_TOPOLOGY_LINE_STRIP_WITH_ADJACENCY,
1187 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST,
1188 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST_WITH_ADJACENCY,
1189 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP,
1190 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP_WITH_ADJACENCY,
1191 VK_PRIMITIVE_TOPOLOGY_TRIANGLE_FAN,
1194 MovePtr<tcu::TestCaseGroup> clipVolumeGroup(new tcu::TestCaseGroup(testCtx, "clip_volume", "clipping with the clip volume"));
1196 // Fully inside the clip volume
1198 MovePtr<tcu::TestCaseGroup> group(new tcu::TestCaseGroup(testCtx, "inside", ""));
1200 for (int caseNdx = 0; caseNdx < DE_LENGTH_OF_ARRAY(cases); ++caseNdx)
1201 addFunctionCaseWithPrograms<VkPrimitiveTopology>(
1202 group.get(), getPrimitiveTopologyShortName(cases[caseNdx]), "", initPrograms, testPrimitivesInside, cases[caseNdx]);
1204 clipVolumeGroup->addChild(group.release());
1207 // Fully outside the clip volume
1209 MovePtr<tcu::TestCaseGroup> group(new tcu::TestCaseGroup(testCtx, "outside", ""));
1211 for (int caseNdx = 0; caseNdx < DE_LENGTH_OF_ARRAY(cases); ++caseNdx)
1212 addFunctionCaseWithPrograms<VkPrimitiveTopology>(
1213 group.get(), getPrimitiveTopologyShortName(cases[caseNdx]), "", initPrograms, testPrimitivesOutside, cases[caseNdx]);
1215 clipVolumeGroup->addChild(group.release());
1220 MovePtr<tcu::TestCaseGroup> group(new tcu::TestCaseGroup(testCtx, "depth_clamp", ""));
1222 for (int caseNdx = 0; caseNdx < DE_LENGTH_OF_ARRAY(cases); ++caseNdx)
1223 addFunctionCaseWithPrograms<VkPrimitiveTopology>(
1224 group.get(), getPrimitiveTopologyShortName(cases[caseNdx]), "", initPrograms, testPrimitivesDepthClamp, cases[caseNdx]);
1226 clipVolumeGroup->addChild(group.release());
1229 // Large points and wide lines
1231 // \note For both points and lines, if an unsupported size/width is selected, the nearest supported size will be chosen.
1232 // We do have to check for feature support though.
1234 MovePtr<tcu::TestCaseGroup> group(new tcu::TestCaseGroup(testCtx, "clipped", ""));
1236 addFunctionCaseWithPrograms(group.get(), "large_points", "", initProgramsPointSize, testLargePoints);
1238 addFunctionCaseWithPrograms<LineOrientation>(group.get(), "wide_lines_axis_aligned", "", initPrograms, testWideLines, LINE_ORIENTATION_AXIS_ALIGNED);
1239 addFunctionCaseWithPrograms<LineOrientation>(group.get(), "wide_lines_diagonal", "", initPrograms, testWideLines, LINE_ORIENTATION_DIAGONAL);
1241 clipVolumeGroup->addChild(group.release());
1244 clippingTestsGroup->addChild(clipVolumeGroup.release());
1247 // User-defined clip planes
1249 MovePtr<tcu::TestCaseGroup> clipDistanceGroup(new tcu::TestCaseGroup(testCtx, "user_defined", "user-defined clip planes"));
1251 // ClipDistance, CullDistance and maxCombinedClipAndCullDistances usage
1253 using namespace ClipDistance;
1257 const char* const groupName;
1258 const char* const description;
1259 bool useCullDistance;
1262 { "clip_distance", "use ClipDistance", false },
1263 { "clip_cull_distance", "use ClipDistance and CullDistance at the same time", true },
1266 const deUint32 flagTessellation = 1u << 0;
1267 const deUint32 flagGeometry = 1u << 1;
1269 for (int groupNdx = 0; groupNdx < DE_LENGTH_OF_ARRAY(caseGroups); ++groupNdx)
1270 for (int indexingMode = 0; indexingMode < 2; ++indexingMode)
1272 const bool dynamicIndexing = (indexingMode == 1);
1273 const std::string mainGroupName = de::toString(caseGroups[groupNdx].groupName) + (dynamicIndexing ? "_dynamic_index" : "");
1275 MovePtr<tcu::TestCaseGroup> mainGroup(new tcu::TestCaseGroup(testCtx, mainGroupName.c_str(), ""));
1277 for (deUint32 shaderMask = 0u; shaderMask <= (flagTessellation | flagGeometry); ++shaderMask)
1279 const bool useTessellation = (shaderMask & flagTessellation) != 0;
1280 const bool useGeometry = (shaderMask & flagGeometry) != 0;
1281 const std::string shaderGroupName = std::string("vert") + (useTessellation ? "_tess" : "") + (useGeometry ? "_geom" : "");
1283 MovePtr<tcu::TestCaseGroup> shaderGroup(new tcu::TestCaseGroup(testCtx, shaderGroupName.c_str(), ""));
1285 for (int numClipPlanes = 1; numClipPlanes <= MAX_CLIP_DISTANCES; ++numClipPlanes)
1287 const int numCullPlanes = (caseGroups[groupNdx].useCullDistance
1288 ? std::min(static_cast<int>(MAX_CULL_DISTANCES), MAX_COMBINED_CLIP_AND_CULL_DISTANCES - numClipPlanes)
1290 const std::string caseName = de::toString(numClipPlanes) + (numCullPlanes > 0 ? "_" + de::toString(numCullPlanes) : "");
1291 const VkPrimitiveTopology topology = (useTessellation ? VK_PRIMITIVE_TOPOLOGY_PATCH_LIST : VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST);
1293 addFunctionCaseWithPrograms<CaseDefinition>(
1294 shaderGroup.get(), caseName, caseGroups[groupNdx].description, initPrograms, testClipDistance,
1295 CaseDefinition(topology, numClipPlanes, numCullPlanes, useTessellation, useGeometry, dynamicIndexing));
1297 mainGroup->addChild(shaderGroup.release());
1299 clipDistanceGroup->addChild(mainGroup.release());
1303 // Complementarity criterion (i.e. clipped and not clipped areas must add up to a complete primitive with no holes nor overlap)
1305 using namespace ClipDistanceComplementarity;
1307 MovePtr<tcu::TestCaseGroup> group(new tcu::TestCaseGroup(testCtx, "complementarity", ""));
1309 for (int numClipDistances = 1; numClipDistances <= MAX_CLIP_DISTANCES; ++numClipDistances)
1310 addFunctionCaseWithPrograms<int>(group.get(), de::toString(numClipDistances).c_str(), "", initPrograms, testComplementarity, numClipDistances);
1312 clippingTestsGroup->addChild(group.release());
1315 clippingTestsGroup->addChild(clipDistanceGroup.release());
1321 tcu::TestCaseGroup* createTests (tcu::TestContext& testCtx)
1323 return createTestGroup(testCtx, "clipping", "Clipping tests", addClippingTests);