1 /*------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
4 *
5 * Copyright (c) 2016 The Khronos Group Inc.
6 * Copyright (c) 2017 Google 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 Inverted depth ranges tests.
23 *//*--------------------------------------------------------------------*/
24
25 #include "vktDrawInvertedDepthRangesTests.hpp"
26 #include "vktDrawCreateInfoUtil.hpp"
27 #include "vktDrawImageObjectUtil.hpp"
28 #include "vktDrawBufferObjectUtil.hpp"
29 #include "vktTestGroupUtil.hpp"
30 #include "vktTestCaseUtil.hpp"
31
32 #include "vkPrograms.hpp"
33 #include "vkTypeUtil.hpp"
34 #include "vkImageUtil.hpp"
35 #include "vkCmdUtil.hpp"
36
37 #include "tcuVector.hpp"
38 #include "tcuTextureUtil.hpp"
39 #include "tcuImageCompare.hpp"
40 #include "tcuTestLog.hpp"
41
42 #include "deSharedPtr.hpp"
43
44 #include <utility>
45 #include <array>
46 #include <vector>
47 #include <iterator>
48
49 namespace vkt
50 {
51 namespace Draw
52 {
53 namespace
54 {
55 using namespace vk;
56 using tcu::Vec4;
57 using de::SharedPtr;
58 using de::MovePtr;
59
60 struct TestParams
61 {
62 float minDepth;
63 float maxDepth;
64 VkBool32 depthClampEnable;
65 VkBool32 depthBiasEnable;
66 float depthBiasClamp;
67
68 };
69
70 constexpr deUint32 kImageDim = 256u;
71 const VkExtent3D kImageExtent = makeExtent3D(kImageDim, kImageDim, 1u);
72 const Vec4 kClearColor (0.0f, 0.0f, 0.0f, 1.0f);
73 constexpr float kClearDepth = 1.0f;
74 constexpr int kClearStencil = 0;
75 constexpr int kMaskedStencil = 1;
76 constexpr float kDepthEpsilon = 0.00025f; // Used to decide if a calculated depth passes the depth test.
77 constexpr float kDepthThreshold = 0.0025f; // Used when checking depth buffer values. Less than depth delta in each pixel (~= 1.4/205).
78 constexpr float kMargin = 0.2f; // Space between triangle and image border. See kVertices.
79 constexpr float kDiagonalMargin = 0.00125f; // Makes sure the image diagonal falls inside the triangle. See kVertices.
80 const Vec4 kVertexColor (0.0f, 0.5f, 0.5f, 1.0f); // Note: the first component will vary.
81
82 // Maximum depth slope is constant for triangle and the value here is true only for triangle used it this tests.
83 constexpr float kMaxDepthSlope = 1.4f / 205;
84
85 const std::array<Vec4, 3u> kVertices =
86 {{
87 Vec4(-1.0f + kMargin, -1.0f + kMargin, -0.2f, 1.0f), // 0-----2
88 Vec4(-1.0f + kMargin, 1.0f - kMargin + kDiagonalMargin, 0.0f, 1.0f), // | /
89 Vec4( 1.0f - kMargin + kDiagonalMargin, -1.0f + kMargin, 1.2f, 1.0f), // 1|/
90 }};
91
92
93 class InvertedDepthRangesTestInstance : public TestInstance
94 {
95 public:
96 enum class ReferenceImageType
97 {
98 COLOR = 0,
99 DEPTH,
100 };
101
102 using ColorAndDepth = std::pair<tcu::ConstPixelBufferAccess, tcu::ConstPixelBufferAccess>;
103
104 InvertedDepthRangesTestInstance (Context& context, const TestParams& params);
105 tcu::TestStatus iterate (void);
106 ColorAndDepth draw (const VkViewport viewport);
107 MovePtr<tcu::TextureLevel> generateReferenceImage (ReferenceImageType refType) const;
108
109 private:
110 const TestParams m_params;
111 const VkFormat m_colorAttachmentFormat;
112 const VkFormat m_depthAttachmentFormat;
113 SharedPtr<Image> m_colorTargetImage;
114 Move<VkImageView> m_colorTargetView;
115 SharedPtr<Image> m_depthTargetImage;
116 Move<VkImageView> m_depthTargetView;
117 SharedPtr<Buffer> m_vertexBuffer;
118 Move<VkRenderPass> m_renderPass;
119 Move<VkFramebuffer> m_framebuffer;
120 Move<VkPipelineLayout> m_pipelineLayout;
121 Move<VkPipeline> m_pipeline;
122 };
123
InvertedDepthRangesTestInstance(Context & context,const TestParams & params)124 InvertedDepthRangesTestInstance::InvertedDepthRangesTestInstance (Context& context, const TestParams& params)
125 : TestInstance (context)
126 , m_params (params)
127 , m_colorAttachmentFormat (VK_FORMAT_R8G8B8A8_UNORM)
128 , m_depthAttachmentFormat (VK_FORMAT_D16_UNORM)
129 {
130 const DeviceInterface& vk = m_context.getDeviceInterface();
131 const VkDevice device = m_context.getDevice();
132 auto& alloc = m_context.getDefaultAllocator();
133 auto qIndex = m_context.getUniversalQueueFamilyIndex();
134
135 // Vertex data
136 {
137 const auto dataSize = static_cast<VkDeviceSize>(kVertices.size() * sizeof(decltype(kVertices)::value_type));
138 m_vertexBuffer = Buffer::createAndAlloc(vk, device, BufferCreateInfo(dataSize, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT),
139 alloc, MemoryRequirement::HostVisible);
140
141 deMemcpy(m_vertexBuffer->getBoundMemory().getHostPtr(), kVertices.data(), static_cast<size_t>(dataSize));
142 flushMappedMemoryRange(vk, device, m_vertexBuffer->getBoundMemory().getMemory(), m_vertexBuffer->getBoundMemory().getOffset(), VK_WHOLE_SIZE);
143 }
144
145 // Render pass
146 {
147 const VkImageUsageFlags targetImageUsageFlags = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
148 const VkImageUsageFlags depthTargeUsageFlags = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
149
150 const ImageCreateInfo targetImageCreateInfo(
151 VK_IMAGE_TYPE_2D, // imageType,
152 m_colorAttachmentFormat, // format,
153 kImageExtent, // extent,
154 1u, // mipLevels,
155 1u, // arrayLayers,
156 VK_SAMPLE_COUNT_1_BIT, // samples,
157 VK_IMAGE_TILING_OPTIMAL, // tiling,
158 targetImageUsageFlags); // usage,
159
160 m_colorTargetImage = Image::createAndAlloc(vk, device, targetImageCreateInfo, alloc, qIndex);
161
162 const ImageCreateInfo depthTargetImageCreateInfo(
163 VK_IMAGE_TYPE_2D, // imageType,
164 m_depthAttachmentFormat, // format,
165 kImageExtent, // extent,
166 1u, // mipLevels,
167 1u, // arrayLayers,
168 VK_SAMPLE_COUNT_1_BIT, // samples,
169 VK_IMAGE_TILING_OPTIMAL, // tiling,
170 depthTargeUsageFlags); // usage,
171
172 m_depthTargetImage = Image::createAndAlloc(vk, device, depthTargetImageCreateInfo, alloc, qIndex);
173
174 RenderPassCreateInfo renderPassCreateInfo;
175 renderPassCreateInfo.addAttachment(AttachmentDescription(
176 m_colorAttachmentFormat, // format
177 VK_SAMPLE_COUNT_1_BIT, // samples
178 VK_ATTACHMENT_LOAD_OP_LOAD, // loadOp
179 VK_ATTACHMENT_STORE_OP_STORE, // storeOp
180 VK_ATTACHMENT_LOAD_OP_DONT_CARE, // stencilLoadOp
181 VK_ATTACHMENT_STORE_OP_DONT_CARE, // stencilStoreOp
182 VK_IMAGE_LAYOUT_GENERAL, // initialLayout
183 VK_IMAGE_LAYOUT_GENERAL)); // finalLayout
184
185 renderPassCreateInfo.addAttachment(AttachmentDescription(
186 m_depthAttachmentFormat, // format
187 VK_SAMPLE_COUNT_1_BIT, // samples
188 VK_ATTACHMENT_LOAD_OP_LOAD, // loadOp
189 VK_ATTACHMENT_STORE_OP_STORE, // storeOp
190 VK_ATTACHMENT_LOAD_OP_DONT_CARE, // stencilLoadOp
191 VK_ATTACHMENT_STORE_OP_DONT_CARE, // stencilStoreOp
192 VK_IMAGE_LAYOUT_GENERAL, // initialLayout
193 VK_IMAGE_LAYOUT_GENERAL)); // finalLayout
194
195 const VkAttachmentReference colorAttachmentReference =
196 {
197 0u,
198 VK_IMAGE_LAYOUT_GENERAL
199 };
200
201 const VkAttachmentReference depthAttachmentReference =
202 {
203 1u,
204 VK_IMAGE_LAYOUT_GENERAL
205 };
206
207 renderPassCreateInfo.addSubpass(SubpassDescription(
208 VK_PIPELINE_BIND_POINT_GRAPHICS, // pipelineBindPoint
209 (VkSubpassDescriptionFlags)0, // flags
210 0u, // inputAttachmentCount
211 DE_NULL, // inputAttachments
212 1u, // colorAttachmentCount
213 &colorAttachmentReference, // colorAttachments
214 DE_NULL, // resolveAttachments
215 depthAttachmentReference, // depthStencilAttachment
216 0u, // preserveAttachmentCount
217 DE_NULL)); // preserveAttachments
218
219 m_renderPass = createRenderPass(vk, device, &renderPassCreateInfo);
220 }
221
222 // Framebuffer
223 {
224 const ImageViewCreateInfo colorTargetViewInfo (m_colorTargetImage->object(), VK_IMAGE_VIEW_TYPE_2D, m_colorAttachmentFormat);
225 m_colorTargetView = createImageView(vk, device, &colorTargetViewInfo);
226
227 const ImageViewCreateInfo depthTargetViewInfo (m_depthTargetImage->object(), VK_IMAGE_VIEW_TYPE_2D, m_depthAttachmentFormat);
228 m_depthTargetView = createImageView(vk, device, &depthTargetViewInfo);
229
230 std::vector<VkImageView> fbAttachments(2);
231 fbAttachments[0] = *m_colorTargetView;
232 fbAttachments[1] = *m_depthTargetView;
233
234 const FramebufferCreateInfo framebufferCreateInfo(*m_renderPass, fbAttachments, kImageExtent.width, kImageExtent.height, 1u);
235 m_framebuffer = createFramebuffer(vk, device, &framebufferCreateInfo);
236 }
237
238 // Vertex input
239
240 const VkVertexInputBindingDescription vertexInputBindingDescription =
241 {
242 0u, // uint32_t binding;
243 sizeof(Vec4), // uint32_t stride;
244 VK_VERTEX_INPUT_RATE_VERTEX, // VkVertexInputRate inputRate;
245 };
246
247 const VkVertexInputAttributeDescription vertexInputAttributeDescription =
248 {
249 0u, // uint32_t location;
250 0u, // uint32_t binding;
251 VK_FORMAT_R32G32B32A32_SFLOAT, // VkFormat format;
252 0u // uint32_t offset;
253 };
254
255 const PipelineCreateInfo::VertexInputState vertexInputState = PipelineCreateInfo::VertexInputState(1, &vertexInputBindingDescription,
256 1, &vertexInputAttributeDescription);
257
258 // Graphics pipeline
259
260 const auto scissor = makeRect2D(kImageExtent);
261
262 std::vector<VkDynamicState> dynamicStates;
263 dynamicStates.push_back(VK_DYNAMIC_STATE_VIEWPORT);
264
265 const Unique<VkShaderModule> vertexModule (createShaderModule(vk, device, m_context.getBinaryCollection().get("vert"), 0));
266 const Unique<VkShaderModule> fragmentModule (createShaderModule(vk, device, m_context.getBinaryCollection().get("frag"), 0));
267
268 const PipelineLayoutCreateInfo pipelineLayoutCreateInfo;
269 m_pipelineLayout = createPipelineLayout(vk, device, &pipelineLayoutCreateInfo);
270
271 const PipelineCreateInfo::ColorBlendState::Attachment colorBlendAttachmentState;
272
273 PipelineCreateInfo pipelineCreateInfo(*m_pipelineLayout, *m_renderPass, 0, (VkPipelineCreateFlags)0);
274 pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*vertexModule, "main", VK_SHADER_STAGE_VERTEX_BIT));
275 pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*fragmentModule, "main", VK_SHADER_STAGE_FRAGMENT_BIT));
276 pipelineCreateInfo.addState (PipelineCreateInfo::VertexInputState (vertexInputState));
277 pipelineCreateInfo.addState (PipelineCreateInfo::InputAssemblerState(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST));
278 pipelineCreateInfo.addState (PipelineCreateInfo::ColorBlendState (1, &colorBlendAttachmentState));
279 pipelineCreateInfo.addState (PipelineCreateInfo::ViewportState (1, std::vector<VkViewport>(), std::vector<VkRect2D>(1, scissor)));
280 pipelineCreateInfo.addState (PipelineCreateInfo::DepthStencilState (true, true));
281 pipelineCreateInfo.addState (PipelineCreateInfo::RasterizerState (
282 m_params.depthClampEnable, // depthClampEnable
283 VK_FALSE, // rasterizerDiscardEnable
284 VK_POLYGON_MODE_FILL, // polygonMode
285 VK_CULL_MODE_NONE, // cullMode
286 VK_FRONT_FACE_CLOCKWISE, // frontFace
287 m_params.depthBiasEnable, // depthBiasEnable
288 0.0f, // depthBiasConstantFactor
289 m_params.depthBiasEnable ? m_params.depthBiasClamp : 0.0f, // depthBiasClamp
290 m_params.depthBiasEnable ? 1.0f : 0.0f, // depthBiasSlopeFactor
291 1.0f)); // lineWidth
292 pipelineCreateInfo.addState (PipelineCreateInfo::MultiSampleState ());
293 pipelineCreateInfo.addState (PipelineCreateInfo::DynamicState (dynamicStates));
294
295 m_pipeline = createGraphicsPipeline(vk, device, DE_NULL, &pipelineCreateInfo);
296 }
297
draw(const VkViewport viewport)298 InvertedDepthRangesTestInstance::ColorAndDepth InvertedDepthRangesTestInstance::draw (const VkViewport viewport)
299 {
300 const DeviceInterface& vk = m_context.getDeviceInterface();
301 const VkDevice device = m_context.getDevice();
302 const VkQueue queue = m_context.getUniversalQueue();
303 const deUint32 queueFamilyIndex = m_context.getUniversalQueueFamilyIndex();
304 auto& alloc = m_context.getDefaultAllocator();
305
306 // Command buffer
307
308 const CmdPoolCreateInfo cmdPoolCreateInfo (queueFamilyIndex);
309 const Unique<VkCommandPool> cmdPool (createCommandPool(vk, device, &cmdPoolCreateInfo));
310 const Unique<VkCommandBuffer> cmdBuffer (allocateCommandBuffer(vk, device, *cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
311
312 // Draw
313
314 beginCommandBuffer(vk, *cmdBuffer);
315
316 vk.cmdSetViewport(*cmdBuffer, 0u, 1u, &viewport);
317
318 {
319 const VkClearColorValue clearColor = makeClearValueColor(kClearColor).color;
320 const ImageSubresourceRange subresourceRange (VK_IMAGE_ASPECT_COLOR_BIT);
321
322 const VkClearDepthStencilValue clearDepth = makeClearValueDepthStencil(kClearDepth, 0u).depthStencil;
323 const ImageSubresourceRange depthSubresourceRange (VK_IMAGE_ASPECT_DEPTH_BIT);
324
325 initialTransitionColor2DImage(vk, *cmdBuffer, m_colorTargetImage->object(), VK_IMAGE_LAYOUT_GENERAL, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT);
326 initialTransitionDepth2DImage(vk, *cmdBuffer, m_depthTargetImage->object(), VK_IMAGE_LAYOUT_GENERAL, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT);
327 vk.cmdClearColorImage(*cmdBuffer, m_colorTargetImage->object(), VK_IMAGE_LAYOUT_GENERAL, &clearColor, 1, &subresourceRange);
328 vk.cmdClearDepthStencilImage(*cmdBuffer, m_depthTargetImage->object(), VK_IMAGE_LAYOUT_GENERAL, &clearDepth, 1u, &depthSubresourceRange);
329 }
330 {
331 const VkMemoryBarrier memBarrier =
332 {
333 VK_STRUCTURE_TYPE_MEMORY_BARRIER, // VkStructureType sType;
334 DE_NULL, // const void* pNext;
335 VK_ACCESS_TRANSFER_WRITE_BIT, // VkAccessFlags srcAccessMask;
336 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT // VkAccessFlags dstAccessMask;
337 };
338
339 const VkMemoryBarrier depthBarrier =
340 {
341 VK_STRUCTURE_TYPE_MEMORY_BARRIER, // VkStructureType sType;
342 DE_NULL, // const void* pNext;
343 VK_ACCESS_TRANSFER_WRITE_BIT, // VkAccessFlags srcAccessMask;
344 VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT // VkAccessFlags dstAccessMask;
345 };
346
347 vk.cmdPipelineBarrier(*cmdBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 1, &memBarrier, 0, DE_NULL, 0, DE_NULL);
348 vk.cmdPipelineBarrier(*cmdBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, (VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT), 0, 1, &depthBarrier, 0, DE_NULL, 0, DE_NULL);
349 }
350
351 beginRenderPass(vk, *cmdBuffer, *m_renderPass, *m_framebuffer, makeRect2D(kImageExtent));
352
353 {
354 const VkDeviceSize offset = 0;
355 const VkBuffer buffer = m_vertexBuffer->object();
356
357 vk.cmdBindVertexBuffers(*cmdBuffer, 0, 1, &buffer, &offset);
358 }
359
360 vk.cmdBindPipeline(*cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, *m_pipeline);
361 vk.cmdDraw(*cmdBuffer, 3, 1, 0, 0);
362 endRenderPass(vk, *cmdBuffer);
363 endCommandBuffer(vk, *cmdBuffer);
364
365 // Submit
366 submitCommandsAndWait(vk, device, queue, cmdBuffer.get());
367
368 // Get result
369 {
370 const auto zeroOffset = makeOffset3D(0, 0, 0);
371 const auto iWidth = static_cast<int>(kImageExtent.width);
372 const auto iHeight = static_cast<int>(kImageExtent.height);
373 const auto colorPixels = m_colorTargetImage->readSurface(queue, alloc, VK_IMAGE_LAYOUT_GENERAL, zeroOffset, iWidth, iHeight, VK_IMAGE_ASPECT_COLOR_BIT);
374 const auto depthPixels = m_depthTargetImage->readSurface(queue, alloc, VK_IMAGE_LAYOUT_GENERAL, zeroOffset, iWidth, iHeight, VK_IMAGE_ASPECT_DEPTH_BIT);
375
376 return ColorAndDepth(colorPixels, depthPixels);
377 }
378 }
379
generateReferenceImage(ReferenceImageType refType) const380 MovePtr<tcu::TextureLevel> InvertedDepthRangesTestInstance::generateReferenceImage (ReferenceImageType refType) const
381 {
382 const auto iWidth = static_cast<int>(kImageExtent.width);
383 const auto iHeight = static_cast<int>(kImageExtent.height);
384 const bool color = (refType == ReferenceImageType::COLOR);
385 const auto tcuFormat = mapVkFormat(color ? m_colorAttachmentFormat : VK_FORMAT_D16_UNORM_S8_UINT);
386 MovePtr<tcu::TextureLevel> image (new tcu::TextureLevel(tcuFormat, iWidth, iHeight));
387 const tcu::PixelBufferAccess access (image->getAccess());
388 const float fImageDim = static_cast<float>(kImageDim);
389 const float p1f = fImageDim * kMargin / 2.0f;
390 const float p2f = fImageDim * (2.0f - kMargin + kDiagonalMargin) / 2.0f;
391 const float triangleSide = fImageDim * (2.0f - (2.0f*kMargin - kDiagonalMargin)) / 2.0f;
392 const float clampMin = de::min(m_params.minDepth, m_params.maxDepth);
393 const float clampMax = de::max(m_params.minDepth, m_params.maxDepth);
394 std::array<float, 3> depthValues;
395 float depthBias = 0.0f;
396
397 // Depth value of each vertex in kVertices.
398 DE_ASSERT(depthValues.size() == kVertices.size());
399 std::transform(begin(kVertices), end(kVertices), begin(depthValues), [](const Vec4& coord) { return coord.z(); });
400
401 if (color)
402 tcu::clear(access, kClearColor);
403 else
404 {
405 tcu::clearDepth(access, kClearDepth);
406 tcu::clearStencil(access, kClearStencil);
407
408 if (m_params.depthBiasEnable)
409 {
410 const float depthBiasSlopeFactor = 1.0f;
411 const float r = 0.000030518f; // minimum resolvable difference is an implementation-dependent parameter
412 const float depthBiasConstantFactor = 0.0f; // so we use factor 0.0 to not include it; same as in PipelineCreateInfo
413
414 // Equations taken from vkCmdSetDepthBias manual page
415 depthBias = kMaxDepthSlope * depthBiasSlopeFactor + r * depthBiasConstantFactor;
416
417 // dbclamp(x) function depends on the sign of the depthBiasClamp
418 if (m_params.depthBiasClamp < 0.0f)
419 depthBias = de::max(depthBias, m_params.depthBiasClamp);
420 else if (m_params.depthBiasClamp > 0.0f)
421 depthBias = de::min(depthBias, m_params.depthBiasClamp);
422
423 if (m_params.maxDepth < m_params.minDepth)
424 depthBias *= -1.0f;
425 }
426 }
427
428 for (int y = 0; y < iHeight; ++y)
429 for (int x = 0; x < iWidth; ++x)
430 {
431 const float xcoord = static_cast<float>(x) + 0.5f;
432 const float ycoord = static_cast<float>(y) + 0.5f;
433
434 if (xcoord < p1f || xcoord > p2f)
435 continue;
436
437 if (ycoord < p1f || ycoord > p2f)
438 continue;
439
440 if (ycoord > -xcoord + fImageDim)
441 continue;
442
443 // Interpolate depth value taking the 3 triangle corners into account.
444 const float b = (ycoord - p1f) / triangleSide;
445 const float c = (xcoord - p1f) / triangleSide;
446 const float a = 1.0f - b - c;
447 const float depth = a * depthValues[0] + b * depthValues[1] + c * depthValues[2];
448
449 // Depth values are always limited to the range [0,1] by clamping after depth bias addition is performed
450 const float depthClamped = de::clamp(depth + depthBias, 0.0f, 1.0f);
451 const float depthFinal = depthClamped * m_params.maxDepth + (1.0f - depthClamped) * m_params.minDepth;
452 const float storedDepth = (m_params.depthClampEnable ? de::clamp(depthFinal, clampMin, clampMax) : depthFinal);
453
454 if (m_params.depthClampEnable || de::inRange(depth, -kDepthEpsilon, 1.0f + kDepthEpsilon))
455 {
456 if (color)
457 access.setPixel(Vec4(depthFinal, kVertexColor.y(), kVertexColor.z(), kVertexColor.w()), x, y);
458 else
459 {
460 if (!m_params.depthClampEnable &&
461 (de::inRange(depth, -kDepthEpsilon, kDepthEpsilon) ||
462 de::inRange(depth, 1.0f - kDepthEpsilon, 1.0f + kDepthEpsilon)))
463 {
464 // We should avoid comparing this pixel due to possible rounding problems.
465 // Pixels that should not be compared will be marked in the stencil aspect.
466 access.setPixStencil(kMaskedStencil, x, y);
467 }
468 access.setPixDepth(storedDepth, x, y);
469 }
470 }
471 }
472
473 return image;
474 }
475
iterate(void)476 tcu::TestStatus InvertedDepthRangesTestInstance::iterate (void)
477 {
478 // Set up the viewport and draw
479
480 const VkViewport viewport =
481 {
482 0.0f, // float x;
483 0.0f, // float y;
484 static_cast<float>(kImageExtent.width), // float width;
485 static_cast<float>(kImageExtent.height), // float height;
486 m_params.minDepth, // float minDepth;
487 m_params.maxDepth, // float maxDepth;
488 };
489
490 ColorAndDepth results = draw(viewport);
491 auto& resultImage = results.first;
492 auto& resultDepth = results.second;
493
494 // Verify results
495 auto& log = m_context.getTestContext().getLog();
496 auto referenceImage = generateReferenceImage(ReferenceImageType::COLOR);
497 auto referenceDepth = generateReferenceImage(ReferenceImageType::DEPTH);
498
499 bool fail = false;
500 // Color aspect.
501 if (!tcu::fuzzyCompare(log, "Image compare", "Image compare", referenceImage->getAccess(), resultImage, 0.02f, tcu::COMPARE_LOG_RESULT))
502 fail = true;
503
504 // Depth aspect.
505 bool depthFail = false;
506
507 const auto refWidth = referenceDepth->getWidth();
508 const auto refHeight = referenceDepth->getHeight();
509 const auto refAccess = referenceDepth->getAccess();
510
511 tcu::TextureLevel errorMask (mapVkFormat(VK_FORMAT_R8G8B8_UNORM), refWidth, refHeight);
512 auto errorAccess = errorMask.getAccess();
513 const tcu::Vec4 kGreen (0.0f, 1.0f, 0.0f, 1.0f);
514 const tcu::Vec4 kRed (1.0f, 0.0f, 0.0f, 1.0f);
515
516 tcu::clear(errorAccess, kGreen);
517
518 for (int y = 0; y < refHeight; ++y)
519 for (int x = 0; x < refWidth; ++x)
520 {
521 // Ignore pixels that could be too close to having or not having coverage.
522 const auto stencil = refAccess.getPixStencil(x, y);
523 if (stencil == kMaskedStencil)
524 continue;
525
526 // Compare the rest using a known threshold.
527 const auto refValue = refAccess.getPixDepth(x, y);
528 const auto resValue = resultDepth.getPixDepth(x, y);
529 if (!de::inRange(resValue, refValue - kDepthThreshold, refValue + kDepthThreshold))
530 {
531 depthFail = true;
532 errorAccess.setPixel(kRed, x, y);
533 }
534 }
535
536 if (depthFail)
537 {
538 log << tcu::TestLog::Message << "Depth Image comparison failed" << tcu::TestLog::EndMessage;
539 log << tcu::TestLog::Image("Result", "Result", resultDepth)
540 << tcu::TestLog::Image("Reference", "Reference", refAccess)
541 << tcu::TestLog::Image("ErrorMask", "Error mask", errorAccess);
542 }
543
544 if (fail || depthFail)
545 return tcu::TestStatus::fail("Result images are incorrect");
546
547 return tcu::TestStatus::pass("Pass");
548 }
549
550 class InvertedDepthRangesTest : public TestCase
551 {
552 public:
InvertedDepthRangesTest(tcu::TestContext & testCtx,const std::string & name,const std::string & description,const TestParams & params)553 InvertedDepthRangesTest (tcu::TestContext& testCtx, const std::string& name, const std::string& description, const TestParams& params)
554 : TestCase (testCtx, name, description)
555 , m_params (params)
556 {
557 }
558
initPrograms(SourceCollections & programCollection) const559 void initPrograms (SourceCollections& programCollection) const
560 {
561 // Vertex shader
562 {
563 std::ostringstream src;
564 src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_450) << "\n"
565 << "\n"
566 << "layout(location = 0) in highp vec4 in_position;\n"
567 << "\n"
568 << "out gl_PerVertex {\n"
569 << " highp vec4 gl_Position;\n"
570 << "};\n"
571 << "\n"
572 << "void main(void)\n"
573 << "{\n"
574 << " gl_Position = in_position;\n"
575 << "}\n";
576
577 programCollection.glslSources.add("vert") << glu::VertexSource(src.str());
578 }
579
580 // Fragment shader
581 {
582 std::ostringstream src;
583 src << glu::getGLSLVersionDeclaration(glu::GLSL_VERSION_450) << "\n"
584 << "\n"
585 << "layout(location = 0) out highp vec4 out_color;\n"
586 << "\n"
587 << "void main(void)\n"
588 << "{\n"
589 << " out_color = vec4(gl_FragCoord.z, " << kVertexColor.y() << ", " << kVertexColor.z() << ", " << kVertexColor.w() << ");\n"
590 << "}\n";
591
592 programCollection.glslSources.add("frag") << glu::FragmentSource(src.str());
593 }
594 }
595
checkSupport(Context & context) const596 virtual void checkSupport (Context& context) const
597 {
598 if (m_params.depthClampEnable)
599 context.requireDeviceCoreFeature(DEVICE_CORE_FEATURE_DEPTH_CLAMP);
600
601 if (m_params.minDepth > 1.0f || m_params.minDepth < 0.0f || m_params.maxDepth > 1.0f || m_params.maxDepth < 0.0f)
602 context.requireDeviceFunctionality("VK_EXT_depth_range_unrestricted");
603 }
604
createInstance(Context & context) const605 virtual TestInstance* createInstance (Context& context) const
606 {
607 return new InvertedDepthRangesTestInstance(context, m_params);
608 }
609
610 private:
611 const TestParams m_params;
612 };
613
populateTestGroup(tcu::TestCaseGroup * testGroup)614 void populateTestGroup (tcu::TestCaseGroup* testGroup)
615 {
616 const struct
617 {
618 std::string name;
619 VkBool32 depthClamp;
620 } depthClamp[] =
621 {
622 { "depthclamp", VK_TRUE },
623 { "nodepthclamp", VK_FALSE },
624 };
625
626 const struct
627 {
628 std::string name;
629 float delta;
630 VkBool32 depthBiasEnable;
631 float depthBiasClamp;
632 } depthParams[] =
633 {
634 { "deltazero", 0.0f, DE_FALSE, 0.0f },
635 { "deltasmall", 0.3f, DE_FALSE, 0.0f },
636 { "deltaone", 1.0f, DE_FALSE, 0.0f },
637
638 // depthBiasClamp must be smaller then maximum depth slope to make a difference
639 { "deltaone_bias_clamp_neg", 1.0f, DE_TRUE, -0.003f },
640 { "deltasmall_bias_clamp_pos", 0.3f, DE_TRUE, 0.003f },
641
642 // Range > 1.0 requires VK_EXT_depth_range_unrestricted extension
643 { "depth_range_unrestricted", 2.7f, DE_FALSE, 0.0f },
644 };
645
646 for (int ndxDepthClamp = 0; ndxDepthClamp < DE_LENGTH_OF_ARRAY(depthClamp); ++ndxDepthClamp)
647 for (int ndxParams = 0; ndxParams < DE_LENGTH_OF_ARRAY(depthParams); ++ndxParams)
648 {
649 const auto& cDepthClamp = depthClamp[ndxDepthClamp];
650 const auto& cDepthParams = depthParams[ndxParams];
651 const float minDepth = 0.5f + cDepthParams.delta / 2.0f;
652 const float maxDepth = minDepth - cDepthParams.delta;
653 DE_ASSERT(minDepth >= maxDepth);
654
655 const TestParams params =
656 {
657 minDepth,
658 maxDepth,
659 cDepthClamp.depthClamp,
660 cDepthParams.depthBiasEnable,
661 cDepthParams.depthBiasClamp,
662 };
663
664 std::string name = cDepthClamp.name + "_" + cDepthParams.name;
665 testGroup->addChild(new InvertedDepthRangesTest(testGroup->getTestContext(), name, "", params));
666 }
667 }
668
669 } // anonymous
670
createInvertedDepthRangesTests(tcu::TestContext & testCtx)671 tcu::TestCaseGroup* createInvertedDepthRangesTests (tcu::TestContext& testCtx)
672 {
673 return createTestGroup(testCtx, "inverted_depth_ranges", "Inverted depth ranges", populateTestGroup);
674 }
675
676 } // Draw
677 } // vkt
678