• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 Depth clamp tests.
23  *//*--------------------------------------------------------------------*/
24 
25 #include "vkDefs.hpp"
26 #include "vktDrawDepthClampTests.hpp"
27 #include "vktTestGroupUtil.hpp"
28 #include "vktTestCaseUtil.hpp"
29 #include "vktDrawCreateInfoUtil.hpp"
30 #include "vktDrawBufferObjectUtil.hpp"
31 #include "vktDrawImageObjectUtil.hpp"
32 #include "vkPrograms.hpp"
33 #include "vkTypeUtil.hpp"
34 #include "vkRefUtil.hpp"
35 #include "vkCmdUtil.hpp"
36 #include "vkBuilderUtil.hpp"
37 #include "vkImageUtil.hpp"
38 #include "vkQueryUtil.hpp"
39 #include "tcuTextureUtil.hpp"
40 #include "tcuTestLog.hpp"
41 
42 #include <cmath>
43 #include <limits>
44 #include "deMath.h"
45 
46 namespace vkt
47 {
48 namespace Draw
49 {
50 namespace {
51 using namespace vk;
52 using namespace de;
53 using std::string;
54 using tcu::Vec4;
55 
56 static const int					WIDTH				= 256;
57 static const int					HEIGHT				= 256;
58 
59 struct ViewportData
60 {
61 	float minDepth;
62 	float maxDepth;
63 	float depthValue;
64 	float expectedValue;
65 };
66 
67 struct TestParams
68 {
69 	string							testNameSuffix;
70 	std::vector<ViewportData>		viewportData;
71 	bool							enableDepthBias;
72 	float							depthBiasConstantFactor;
73 	bool							skipUNorm;
74 	bool							skipSNorm;
75 	std::vector<const char*>		requiredExtensions;
76 };
77 
78 const VkFormat		depthStencilImageFormatsToTest[]	=
79 {
80 	VK_FORMAT_D16_UNORM,
81 	VK_FORMAT_X8_D24_UNORM_PACK32,
82 	VK_FORMAT_D32_SFLOAT,
83 	VK_FORMAT_D16_UNORM_S8_UINT,
84 	VK_FORMAT_D24_UNORM_S8_UINT,
85 	VK_FORMAT_D32_SFLOAT_S8_UINT
86 };
87 const float			depthEpsilonValuesByFormat[]		=
88 {
89 	1e-5f,
90 	std::numeric_limits<float>::epsilon(),
91 	std::numeric_limits<float>::epsilon(),
92 	1e-5f,
93 	std::numeric_limits<float>::epsilon(),
94 	std::numeric_limits<float>::epsilon()
95 };
96 
97 const float			initialClearDepth					= 0.5f;
98 const TestParams	depthClearValuesToTest[]			=
99 {
100 	{
101 		"",											// testNameSuffix
102 		{ {											// viewportData
103 			0.0f,									//   minDepth
104 			1.0f,									//   maxDepth
105 			0.3f,									//   depthValue
106 			0.3f,									//   expectedValue
107 		} },
108 		false,										// enableDepthBias
109 		0.0f,										// depthBiasConstantFactor
110 		false,										// skipUNorm
111 		false,										// skipSNorm
112 		{},											// requiredExtensions
113 	},
114 	{
115 		"_clamp_input_negative",					// testNameSuffix
116 		{ {											// viewportData
117 			0.0f,									//   minDepth
118 			1.0f,									//   maxDepth
119 		   -1e6f,									//   depthValue
120 			0.0f,									//   expectedValue
121 		} },
122 		false,										// enableDepthBias
123 		0.0f,										// depthBiasConstantFactor
124 		false,										// skipUNorm
125 		false,										// skipSNorm
126 		{},											// requiredExtensions
127 	},
128 	{
129 		"_clamp_input_positive",					// testNameSuffix
130 		{ {											// viewportData
131 			0.0f,									//   minDepth
132 			1.0f,									//   maxDepth
133 			1.e6f,									//   depthValue
134 			1.0f,									//   expectedValue
135 		} },
136 		false,										// enableDepthBias
137 		0.0f,										// depthBiasConstantFactor
138 		false,										// skipUNorm
139 		false,										// skipSNorm
140 		{},											// requiredExtensions
141 	},
142 	{
143 		"_depth_bias_clamp_input_negative",			// testNameSuffix
144 		{ {											// viewportData
145 			0.0f,									//   minDepth
146 			1.0f,									//   maxDepth
147 			0.3f,									//   depthValue
148 			0.0f,									//   expectedValue
149 		} },
150 		true,										// enableDepthBias
151 		-2e11f,										// depthBiasConstantFactor
152 		false,										// skipUNorm
153 		false,										// skipSNorm
154 		{},											// requiredExtensions
155 	},
156 	{
157 		"_depth_bias_clamp_input_positive",			// testNameSuffix
158 		{ {											// viewportData
159 			0.0f,									//   minDepth
160 			1.0f,									//   maxDepth
161 			0.7f,									//   depthValue
162 			1.0f,									//   expectedValue
163 		} },
164 		true,										// enableDepthBias
165 		2e11f,										// depthBiasConstantFactor
166 		false,										// skipUNorm
167 		false,										// skipSNorm
168 		{},											// requiredExtensions
169 	},
170 	{
171 		"_depth_range_unrestricted_negative",		// testNameSuffix
172 		{ {											// viewportData
173 		   -1.5f,									//   minDepth
174 			1.0f,									//   maxDepth
175 		   -1.5f,									//   depthValue
176 		   -1.5f,									//   expectedValue
177 		} },
178 		false,										// enableDepthBias
179 		0.0f,										// depthBiasConstantFactor
180 		true,										// skipUNorm
181 		true,										// skipSNorm
182 		{
183 			"VK_EXT_depth_range_unrestricted"		// requiredExtensions[0]
184 		},
185 	},
186 	{
187 		"_depth_range_unrestricted_positive",		// testNameSuffix
188 		{ {											// viewportData
189 			0.0f,									//   minDepth
190 			1.5f,									//   maxDepth
191 			1.5f,									//   depthValue
192 			1.5f,									//   expectedValue
193 		} },
194 		false,										// enableDepthBias
195 		0.0f,										// depthBiasConstantFactor
196 		true,										// skipUNorm
197 		true,										// skipSNorm
198 		{
199 			"VK_EXT_depth_range_unrestricted"		// requiredExtensions[0]
200 		},
201 	},
202 	{
203 		"_clamp_four_viewports",					// testNameSuffix
204 		{											// viewportData
205 			{
206 				0.0f,								//   minDepth
207 				0.5f,								//   maxDepth
208 				0.7f,								//   depthValue
209 				0.35f,								//   expectedValue: 0.7 * 0.5 + (1.0 - 0.7) * 0.0) = 0.35
210 			},
211 			{
212 				0.9f,								//   minDepth
213 				1.0f,								//   maxDepth
214 				1.0f,								//   depthValue
215 				1.0f,								//   expectedValue: 1.0 * 1.0 + (1.0 - 1.0) * 0.9 = 1.0
216 			},
217 			{
218 				0.5f,								//   minDepth
219 				1.0f,								//   maxDepth
220 				0.9f,								//   depthValue
221 				0.95f,								//   expectedValue: 0.9 * 1.0 + (1.0 - 0.9) * 0.5 = 0.95
222 			},
223 			{
224 				0.5f,								//   minDepth
225 				0.9f,								//   maxDepth
226 				0.4f,								//   depthValue
227 				0.66f,								//   expectedValue: 0.4 * 0.9 + (1.0 - 0.4) * 0.5 = 0.66
228 			},
229 		},
230 		false,										// enableDepthBias
231 		0.0f,										// depthBiasConstantFactor
232 		true,										// skipUNorm
233 		true,										// skipSNorm
234 		{},
235 	}
236 };
237 
isUnormDepthFormat(VkFormat format)238 bool isUnormDepthFormat(VkFormat format)
239 {
240 	switch (format)
241 	{
242 		case VK_FORMAT_D24_UNORM_S8_UINT:
243 		case VK_FORMAT_X8_D24_UNORM_PACK32:
244 		case VK_FORMAT_D16_UNORM_S8_UINT:
245 			/* Special case for combined depth-stencil-unorm modes for which tcu::getTextureChannelClass()
246 			   returns TEXTURECHANNELCLASS_LAST */
247 			return true;
248 		default:
249 			return vk::isUnormFormat(format);
250 	}
251 }
252 
253 class DepthClampTestInstance : public TestInstance {
254 public:
255 								DepthClampTestInstance	(Context& context, const TestParams& params, const VkFormat format, const float epsilon, bool useDynamicRendering);
256 	tcu::TestStatus				iterate					();
257 
258 private:
259 	tcu::ConstPixelBufferAccess draw					();
260 
261 	const TestParams									m_params;
262 	const VkFormat										m_format;
263 	const float											m_epsilon;
264 	std::vector<VkViewport>								m_viewportVect;
265 	std::vector<VkRect2D>								m_scissorVect;
266 	const bool											m_useDynamicRendering;
267 	SharedPtr<Image>									m_depthTargetImage;
268 	Move<VkImageView>									m_depthTargetView;
269 	SharedPtr<Buffer>									m_vertexBuffer;
270 	Move<VkRenderPass>									m_renderPass;
271 	Move<VkFramebuffer>									m_framebuffer;
272 	Move<VkPipelineLayout>								m_pipelineLayout;
273 	Move<VkPipeline>									m_pipeline;
274 };
275 
276 static const Vec4					vertices[]			= {
277 	Vec4(-1.0f, -1.0f,  0.5f, 1.0f),	// 0 -- 2
278 	Vec4(-1.0f,  1.0f,  0.5f, 1.0f),	// |  / |
279 	Vec4( 1.0f, -1.0f,  0.5f, 1.0f),	// | /  |
280 	Vec4( 1.0f,  1.0f,  0.5f, 1.0f)		// 1 -- 3
281 };
282 static const VkPrimitiveTopology	verticesTopology	= VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
283 
DepthClampTestInstance(Context & context,const TestParams & params,const VkFormat format,const float epsilon,bool useDynamicRendering)284 DepthClampTestInstance::DepthClampTestInstance (Context& context, const TestParams& params, const VkFormat format, const float epsilon, bool useDynamicRendering)
285 	: TestInstance(context)
286 	, m_params(params)
287 	, m_format(format)
288 	, m_epsilon(epsilon)
289 	, m_viewportVect(params.viewportData.size(), VkViewport())
290 	, m_scissorVect(params.viewportData.size(), VkRect2D())
291 	, m_useDynamicRendering(useDynamicRendering)
292 {
293 	const DeviceInterface&		vk								= m_context.getDeviceInterface();
294 	const VkDevice				device							= m_context.getDevice();
295 	const deUint32				queueFamilyIndex				= m_context.getUniversalQueueFamilyIndex();
296 	const deUint32				viewportCount					= static_cast<deUint32>(m_params.viewportData.size());
297 
298 	// create viewport grid
299 	{
300 		const deUint32	columnCount		= deCeilFloatToInt32(deFloatSqrt(static_cast<float>(viewportCount)));
301 		const deUint32	rowCount		= deCeilFloatToInt32(static_cast<float>(viewportCount) / static_cast<float>(columnCount));
302 		const deUint32	rectWidth		= WIDTH / columnCount;
303 		const deUint32	rectHeight		= HEIGHT / rowCount;
304 
305 		VkOffset2D		pos				{ 0, 0 };
306 
307 		for (deUint32 viewportIndex = 0; viewportIndex < viewportCount; ++viewportIndex)
308 		{
309 			// move to next row
310 			if ((viewportIndex != 0) && (viewportIndex % columnCount == 0))
311 			{
312 				pos.x = 0;
313 				pos.y += rectHeight;
314 			}
315 
316 			m_viewportVect[viewportIndex] =
317 			{
318 				static_cast<float>(pos.x),							// float	x;
319 				static_cast<float>(pos.y),							// float	y;
320 				static_cast<float>(rectWidth),						// float	width;
321 				static_cast<float>(rectHeight),						// float	height;
322 				m_params.viewportData[viewportIndex].minDepth,		// float	minDepth;
323 				m_params.viewportData[viewportIndex].maxDepth,		// float	maxDepth;
324 			};
325 
326 			m_scissorVect[viewportIndex] =
327 			{
328 				pos,
329 				{rectWidth, rectHeight}
330 			};
331 
332 			pos.x += rectWidth;
333 		}
334 	}
335 
336 	DescriptorPoolBuilder		descriptorPoolBuilder;
337 	DescriptorSetLayoutBuilder	descriptorSetLayoutBuilder;
338 	// Vertex data
339 	{
340 		const size_t			verticesCount					= DE_LENGTH_OF_ARRAY(vertices);
341 		const VkDeviceSize		dataSize						= verticesCount * sizeof(Vec4);
342 		m_vertexBuffer											= Buffer::createAndAlloc(vk, device, BufferCreateInfo(dataSize, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT),
343 																m_context.getDefaultAllocator(), MemoryRequirement::HostVisible);
344 
345 		Vec4 testVertices[verticesCount];
346 		deMemcpy(testVertices, vertices, dataSize);
347 		for(size_t i = 0; i < verticesCount; ++i)
348 			testVertices[i][2] = params.viewportData[0].depthValue;
349 		deMemcpy(m_vertexBuffer->getBoundMemory().getHostPtr(), testVertices, static_cast<std::size_t>(dataSize));
350 		flushMappedMemoryRange(vk, device, m_vertexBuffer->getBoundMemory().getMemory(), m_vertexBuffer->getBoundMemory().getOffset(), VK_WHOLE_SIZE);
351 	}
352 
353 	const VkImageUsageFlags		targetImageUsageFlags						= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
354 	const ImageCreateInfo		targetImageCreateInfo						(VK_IMAGE_TYPE_2D, m_format, { WIDTH, HEIGHT, 1u }, 1u,	1u,	VK_SAMPLE_COUNT_1_BIT, VK_IMAGE_TILING_OPTIMAL, targetImageUsageFlags);
355 	m_depthTargetImage														= Image::createAndAlloc(vk, device, targetImageCreateInfo, m_context.getDefaultAllocator(), queueFamilyIndex);
356 
357 	const ImageViewCreateInfo	depthTargetViewInfo							(m_depthTargetImage->object(), VK_IMAGE_VIEW_TYPE_2D, m_format);
358 	m_depthTargetView = createImageView(vk, device, &depthTargetViewInfo);
359 
360 	// Render pass and framebuffer
361 	if (!m_useDynamicRendering)
362 	{
363 		RenderPassCreateInfo		renderPassCreateInfo;
364 		renderPassCreateInfo.addAttachment(AttachmentDescription(
365 			m_format,												// format
366 			VK_SAMPLE_COUNT_1_BIT,									// samples
367 			VK_ATTACHMENT_LOAD_OP_LOAD,								// loadOp
368 			VK_ATTACHMENT_STORE_OP_STORE,							// storeOp
369 			VK_ATTACHMENT_LOAD_OP_DONT_CARE,						// stencilLoadOp
370 			VK_ATTACHMENT_STORE_OP_DONT_CARE,						// stencilStoreOp
371 			VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,		// initialLayout
372 			VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL));		// finalLayout
373 		const VkAttachmentReference depthAttachmentReference					= makeAttachmentReference(0u, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
374 		renderPassCreateInfo.addSubpass(SubpassDescription(
375 			VK_PIPELINE_BIND_POINT_GRAPHICS,	// pipelineBindPoint
376 			(VkSubpassDescriptionFlags)0,		// flags
377 			0u,									// inputAttachmentCount
378 			DE_NULL,							// inputAttachments
379 			0u,									// colorAttachmentCount
380 			DE_NULL,							// colorAttachments
381 			DE_NULL,							// resolveAttachments
382 			depthAttachmentReference,			// depthStencilAttachment
383 			0u,									// preserveAttachmentCount
384 			DE_NULL));							// preserveAttachments
385 		m_renderPass															= createRenderPass(vk, device, &renderPassCreateInfo);
386 
387 		const std::vector<VkImageView>				depthAttachments			{ *m_depthTargetView };
388 		FramebufferCreateInfo						framebufferCreateInfo		(*m_renderPass, depthAttachments, WIDTH, HEIGHT, 1);
389 		m_framebuffer															= createFramebuffer(vk, device, &framebufferCreateInfo);
390 	}
391 
392 	// Vertex input
393 	const VkVertexInputBindingDescription		vertexInputBindingDescription	=
394 	{
395 		0u,										// uint32_t             binding;
396 		sizeof(Vec4),							// uint32_t             stride;
397 		VK_VERTEX_INPUT_RATE_VERTEX,			// VkVertexInputRate    inputRate;
398 	};
399 
400 	const VkVertexInputAttributeDescription		vertexInputAttributeDescription =
401 	{
402 		0u,										// uint32_t    location;
403 		0u,										// uint32_t    binding;
404 		VK_FORMAT_R32G32B32A32_SFLOAT,			// VkFormat    format;
405 		0u										// uint32_t    offset;
406 	};
407 
408 	const PipelineCreateInfo::VertexInputState	vertexInputState				= PipelineCreateInfo::VertexInputState(1, &vertexInputBindingDescription,
409 																													   1, &vertexInputAttributeDescription);
410 
411 	// Graphics pipeline
412 	const Unique<VkShaderModule>	vertexModule								(createShaderModule(vk, device, m_context.getBinaryCollection().get("vert"), 0));
413 	Move<VkShaderModule>			geometryModule;
414 	const Unique<VkShaderModule>	fragmentModule								(createShaderModule(vk, device, m_context.getBinaryCollection().get("frag"), 0));
415 
416 	if (viewportCount > 1)
417 		geometryModule = createShaderModule(vk, device, m_context.getBinaryCollection().get("geom"), 0);
418 
419 	const PipelineLayoutCreateInfo	pipelineLayoutCreateInfo					(0u, DE_NULL, 0u, DE_NULL);
420 	m_pipelineLayout															= createPipelineLayout(vk, device, &pipelineLayoutCreateInfo);
421 	std::vector<VkDynamicState>		dynamicStates								{ VK_DYNAMIC_STATE_VIEWPORT, VK_DYNAMIC_STATE_SCISSOR };
422 
423 	PipelineCreateInfo pipelineCreateInfo(*m_pipelineLayout, *m_renderPass, 0, (VkPipelineCreateFlags)0);
424 	pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*vertexModule,   "main", VK_SHADER_STAGE_VERTEX_BIT));
425 	if (*geometryModule != 0)
426 		pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*geometryModule, "main", VK_SHADER_STAGE_GEOMETRY_BIT));
427 	pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*fragmentModule, "main", VK_SHADER_STAGE_FRAGMENT_BIT));
428 	pipelineCreateInfo.addState (PipelineCreateInfo::VertexInputState	(vertexInputState));
429 	pipelineCreateInfo.addState (PipelineCreateInfo::InputAssemblerState(verticesTopology));
430 	pipelineCreateInfo.addState (PipelineCreateInfo::ViewportState		(viewportCount, m_viewportVect, m_scissorVect));
431 	pipelineCreateInfo.addState (PipelineCreateInfo::DepthStencilState	(VK_TRUE, VK_TRUE, VK_COMPARE_OP_ALWAYS, VK_FALSE, VK_FALSE));
432 	pipelineCreateInfo.addState (PipelineCreateInfo::RasterizerState	(
433 		VK_TRUE,										// depthClampEnable
434 		VK_FALSE,										// rasterizerDiscardEnable
435 		VK_POLYGON_MODE_FILL,							// polygonMode
436 		VK_CULL_MODE_NONE,								// cullMode
437 		VK_FRONT_FACE_CLOCKWISE,						// frontFace
438 		m_params.enableDepthBias ? VK_TRUE : VK_FALSE,	// depthBiasEnable
439 		m_params.depthBiasConstantFactor,				// depthBiasConstantFactor
440 		0.0f,											// depthBiasClamp
441 		0.0f,											// depthBiasSlopeFactor
442 		1.0f));											// lineWidth
443 	pipelineCreateInfo.addState (PipelineCreateInfo::MultiSampleState	());
444 	pipelineCreateInfo.addState (PipelineCreateInfo::DynamicState		(dynamicStates));
445 
446 	VkPipelineRenderingCreateInfoKHR renderingCreateInfo
447 	{
448 		VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO_KHR,
449 		DE_NULL,
450 		0u,
451 		0u,
452 		DE_NULL,
453 		m_format,
454 		m_format
455 	};
456 
457 	if (m_useDynamicRendering)
458 		pipelineCreateInfo.pNext = &renderingCreateInfo;
459 
460 	m_pipeline = createGraphicsPipeline(vk, device, DE_NULL, &pipelineCreateInfo);
461 }
462 
draw()463 tcu::ConstPixelBufferAccess DepthClampTestInstance::draw ()
464 {
465 	const DeviceInterface&				vk					= m_context.getDeviceInterface();
466 	const VkDevice						device				= m_context.getDevice();
467 	const VkQueue						queue				= m_context.getUniversalQueue();
468 	const deUint32						queueFamilyIndex	= m_context.getUniversalQueueFamilyIndex();
469 
470 	const CmdPoolCreateInfo				cmdPoolCreateInfo	(queueFamilyIndex);
471 	const Unique<VkCommandPool>			cmdPool				(createCommandPool(vk, device, &cmdPoolCreateInfo));
472 	const Unique<VkCommandBuffer>		cmdBuffer			(allocateCommandBuffer(vk, device, *cmdPool, VK_COMMAND_BUFFER_LEVEL_PRIMARY));
473 
474 	const bool							isCombinedType		= tcu::isCombinedDepthStencilType(mapVkFormat(m_format).type) && m_format != VK_FORMAT_X8_D24_UNORM_PACK32;
475 
476 	beginCommandBuffer(vk, *cmdBuffer);
477 
478 	if (isCombinedType)
479 		initialTransitionDepthStencil2DImage(vk, *cmdBuffer, m_depthTargetImage->object(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT);
480 	else
481 		initialTransitionDepth2DImage(vk, *cmdBuffer, m_depthTargetImage->object(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_ACCESS_TRANSFER_WRITE_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT);
482 
483 	const VkImageAspectFlagBits			aspectBits			= (VkImageAspectFlagBits)(isCombinedType ? VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT : VK_IMAGE_ASPECT_DEPTH_BIT);
484 	const ImageSubresourceRange			subresourceRange	(aspectBits);
485 	const VkClearValue					clearDepth			= makeClearValueDepthStencil(initialClearDepth, 0u);
486 
487 	vk.cmdClearDepthStencilImage(*cmdBuffer, m_depthTargetImage->object(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, &clearDepth.depthStencil, 1, &subresourceRange);
488 
489 	transition2DImage(vk, *cmdBuffer, m_depthTargetImage->object(), aspectBits,
490 					  VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
491 					  VK_ACCESS_TRANSFER_WRITE_BIT		  , VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT,
492 					  VK_PIPELINE_STAGE_TRANSFER_BIT	  , VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT);
493 
494 	{
495 		const VkMemoryBarrier memBarrier					= { VK_STRUCTURE_TYPE_MEMORY_BARRIER, DE_NULL, VK_ACCESS_TRANSFER_WRITE_BIT, VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT };
496 		vk.cmdPipelineBarrier(*cmdBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, 0, 1, &memBarrier, 0, DE_NULL, 0, DE_NULL);
497 	}
498 
499 	// if there is more then one viewport we are also checking
500 	// proper behaviour of cmdSetViewport/Scissor - there was
501 	// a driver bug that caused invalid behaviour of those
502 	// functions when firstViewport/Scissor had a non 0 value
503 	deUint32 indexCount = static_cast<deUint32>(m_viewportVect.size());
504 	for (deUint32 index = 0 ; index < indexCount ; ++index)
505 	{
506 		vk.cmdSetViewport(*cmdBuffer, index, 1u, &m_viewportVect[index]);
507 		vk.cmdSetScissor (*cmdBuffer, index, 1u, &m_scissorVect[index]);
508 	}
509 
510 	const VkRect2D renderArea = makeRect2D(0, 0, WIDTH, HEIGHT);
511 	if (m_useDynamicRendering)
512 	{
513 		VkRenderingAttachmentInfoKHR depthAttachment
514 		{
515 			VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO_KHR,								// VkStructureType						sType;
516 			DE_NULL,																		// const void*							pNext;
517 			*m_depthTargetView,																// VkImageView							imageView;
518 			VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,								// VkImageLayout						imageLayout;
519 			VK_RESOLVE_MODE_NONE,															// VkResolveModeFlagBits				resolveMode;
520 			DE_NULL,																		// VkImageView							resolveImageView;
521 			VK_IMAGE_LAYOUT_UNDEFINED,														// VkImageLayout						resolveImageLayout;
522 			VK_ATTACHMENT_LOAD_OP_LOAD,														// VkAttachmentLoadOp					loadOp;
523 			VK_ATTACHMENT_STORE_OP_STORE,													// VkAttachmentStoreOp					storeOp;
524 			clearDepth																		// VkClearValue							clearValue;
525 		};
526 
527 		VkRenderingInfoKHR renderingInfo
528 		{
529 			VK_STRUCTURE_TYPE_RENDERING_INFO_KHR,
530 			DE_NULL,
531 			0u,																				// VkRenderingFlagsKHR					flags;
532 			renderArea,																		// VkRect2D								renderArea;
533 			1u,																				// deUint32								layerCount;
534 			0u,																				// deUint32								viewMask;
535 			0u,																				// deUint32								colorAttachmentCount;
536 			DE_NULL,																		// const VkRenderingAttachmentInfoKHR*	pColorAttachments;
537 			&depthAttachment,																// const VkRenderingAttachmentInfoKHR*	pDepthAttachment;
538 			DE_NULL,																		// const VkRenderingAttachmentInfoKHR*	pStencilAttachment;
539 		};
540 
541 		vk.cmdBeginRenderingKHR(*cmdBuffer, &renderingInfo);
542 	}
543 	else
544 		beginRenderPass(vk, *cmdBuffer, *m_renderPass, *m_framebuffer, makeRect2D(0, 0, WIDTH, HEIGHT));
545 
546 	vk.cmdBindPipeline(*cmdBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, *m_pipeline);
547 	const VkDeviceSize		offset							= 0;
548 	const VkBuffer			buffer							= m_vertexBuffer->object();
549 	vk.cmdBindVertexBuffers(*cmdBuffer, 0, 1, &buffer, &offset);
550 	vk.cmdDraw(*cmdBuffer, DE_LENGTH_OF_ARRAY(vertices), 1, 0, 0);
551 
552 	if (m_useDynamicRendering)
553 		endRendering(vk, *cmdBuffer);
554 	else
555 		endRenderPass(vk, *cmdBuffer);
556 
557 	transition2DImage(vk, *cmdBuffer, m_depthTargetImage->object(), aspectBits,
558 					  VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
559 					  VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT	  , VK_ACCESS_MEMORY_READ_BIT,
560 					  VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT		  , VK_PIPELINE_STAGE_HOST_BIT);
561 
562 	endCommandBuffer(vk, *cmdBuffer);
563 	submitCommandsAndWait(vk, device, queue, *cmdBuffer);
564 
565 	VK_CHECK(vk.queueWaitIdle(queue));
566 
567 	return m_depthTargetImage->readDepth(queue, m_context.getDefaultAllocator(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, { 0, 0, 0 }, WIDTH, HEIGHT, VK_IMAGE_ASPECT_DEPTH_BIT);
568 }
569 
iterate(void)570 tcu::TestStatus DepthClampTestInstance::iterate (void)
571 {
572 	const tcu::ConstPixelBufferAccess resultImage = draw();
573 
574 	DE_ASSERT((isUnormDepthFormat(m_format) == false) ||
575 		(m_params.viewportData[0].expectedValue >= 0.0f && m_params.viewportData[0].expectedValue <= 1.0f));
576 
577 	for(deUint32 viewportIndex = 0 ; viewportIndex < m_scissorVect.size() ; ++viewportIndex)
578 	{
579 		const float		expectedValue	= m_params.viewportData[viewportIndex].expectedValue;
580 		const VkRect2D&	viewRect		= m_scissorVect[viewportIndex];
581 
582 		deInt32 xStart	= viewRect.offset.x;
583 		deInt32 xEnd	= xStart + viewRect.extent.width;
584 		deInt32 yStart	= viewRect.offset.y;
585 		deInt32 yEnd	= yStart + viewRect.extent.height;
586 
587 		for (int y = yStart; y < yEnd; ++y)
588 		for (int x = xStart; x < xEnd; ++x)
589 		{
590 			if (std::abs(expectedValue - resultImage.getPixDepth(x, y, 0)) >= m_epsilon)
591 			{
592 				tcu::TestLog& log = m_context.getTestContext().getLog();
593 				log << tcu::TestLog::ImageSet("Result of rendering", "")
594 					<< tcu::TestLog::Image("Result", "", resultImage)
595 					<< tcu::TestLog::EndImageSet;
596 
597 				std::ostringstream msg;
598 				msg << "Depth value mismatch, expected: " << expectedValue
599 					<< ", got: " << resultImage.getPixDepth(x, y, 0) << " at (" << x << ", " << y << ", 0)";
600 
601 				return tcu::TestStatus::fail(msg.str());
602 			}
603 		}
604 	}
605 	return tcu::TestStatus::pass("Pass");
606 }
607 
608 class DepthClampTest : public TestCase
609 {
610 public:
DepthClampTest(tcu::TestContext & testCtx,const string & name,const string & description,const TestParams & params,const VkFormat format,const float epsilon,bool useDynamicRendering)611 	DepthClampTest (tcu::TestContext &testCtx, const string& name, const string& description, const TestParams &params, const VkFormat format, const float epsilon, bool useDynamicRendering)
612 		: TestCase	(testCtx, name, description)
613 		, m_params(params)
614 		, m_format(format)
615 		, m_epsilon(epsilon)
616 		, m_useDynamicRendering(useDynamicRendering)
617 	{
618 	}
619 
initPrograms(SourceCollections & programCollection) const620 	virtual void initPrograms (SourceCollections& programCollection) const
621 	{
622 		programCollection.glslSources.add("vert") << glu::VertexSource(
623 			"#version 450\n"
624 			"\n"
625 			"layout(location = 0) in vec4 in_position;\n"
626 			"void main(void)\n"
627 			"{\n"
628 			"    gl_Position = in_position;\n"
629 			"}\n");
630 
631 		if (m_params.viewportData.size() > 1)
632 		{
633 			// gl_ViewportIndex built-in variable is available only to the geometry shader
634 
635 			std::string depthValues = "";
636 			for (const auto& vd : m_params.viewportData)
637 				depthValues += std::to_string(vd.depthValue) + ", ";
638 
639 			// this geometry shader draws the same quad but with diferent depth to all viewports
640 			programCollection.glslSources.add("geom") << glu::GeometrySource(
641 				std::string("#version 450\n") +
642 				"#extension GL_EXT_geometry_shader : require\n"
643 				"layout(invocations = " + std::to_string(m_params.viewportData.size()) + ") in;\n"
644 				"layout(triangles) in;\n"
645 				"layout(triangle_strip, max_vertices = 4) out;\n"
646 				"void main()\n"
647 				"{\n"
648 				"  const float depthValues[] = { " + depthValues + " 0.0 };\n"
649 				"  for (int i = 0; i < gl_in.length(); i++)\n"
650 				"  {\n"
651 				"    gl_ViewportIndex = gl_InvocationID;\n"
652 				"    gl_Position      = gl_in[i].gl_Position;\n"
653 				"    gl_Position.z    = depthValues[gl_InvocationID];\n"
654 				"    EmitVertex();\n"
655 				"  }\n"
656 				"  EndPrimitive();\n"
657 				"}");
658 		}
659 
660 		programCollection.glslSources.add("frag") << glu::FragmentSource(
661 			"#version 450\n"
662 			"void main(void)\n"
663 			"{\n"
664 			"}\n");
665 	}
666 
checkSupport(Context & context) const667 	virtual void checkSupport (Context& context) const
668 	{
669 		context.requireDeviceCoreFeature(DEVICE_CORE_FEATURE_DEPTH_CLAMP);
670 		for(const auto& extensionName : m_params.requiredExtensions)
671 			context.requireDeviceFunctionality(extensionName);
672 
673 		if (m_params.viewportData.size() > 1)
674 			context.requireDeviceCoreFeature(DEVICE_CORE_FEATURE_MULTI_VIEWPORT);
675 
676 		VkImageFormatProperties imageFormatProperties;
677 		const auto&	vki		= context.getInstanceInterface();
678 		const auto&	vkd		= context.getPhysicalDevice();
679 		const auto	usage	= VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
680 		if (vki.getPhysicalDeviceImageFormatProperties(vkd, m_format, VK_IMAGE_TYPE_2D, VK_IMAGE_TILING_OPTIMAL, usage, 0u, &imageFormatProperties) == VK_ERROR_FORMAT_NOT_SUPPORTED)
681 		{
682 			TCU_THROW(NotSupportedError, "Format not supported");
683 		}
684 
685 		if (m_useDynamicRendering)
686 			context.requireDeviceFunctionality("VK_KHR_dynamic_rendering");
687 	}
688 
createInstance(Context & context) const689 	virtual TestInstance* createInstance (Context& context) const
690 	{
691 		return new DepthClampTestInstance(context, m_params, m_format, m_epsilon, m_useDynamicRendering);
692 	}
693 
694 private:
695 	const TestParams	m_params;
696 	const VkFormat		m_format;
697 	const float			m_epsilon;
698 	const bool			m_useDynamicRendering;
699 };
700 
getFormatCaseName(VkFormat format)701 std::string getFormatCaseName (VkFormat format)
702 {
703 	return de::toLower(de::toString(getFormatStr(format)).substr(10));
704 }
705 
createTests(tcu::TestCaseGroup * testGroup,bool useDynamicRendering)706 void createTests (tcu::TestCaseGroup* testGroup, bool useDynamicRendering)
707 {
708 	for(int i = 0; i < DE_LENGTH_OF_ARRAY(depthStencilImageFormatsToTest); ++i)
709 	{
710 		const auto		format			= depthStencilImageFormatsToTest[i];
711 		const float		epsilon			= depthEpsilonValuesByFormat[i];
712 		const auto		formatCaseName	= getFormatCaseName(format);
713 		for(const auto& params : depthClearValuesToTest)
714 		{
715 			if ((params.skipSNorm && vk::isSnormFormat(format)) || (params.skipUNorm && isUnormDepthFormat(format)))
716 				continue;
717 			const auto	testCaseName	= formatCaseName + params.testNameSuffix;
718 			testGroup->addChild(new DepthClampTest(testGroup->getTestContext(), testCaseName, "Depth clamp", params, format, epsilon, useDynamicRendering));
719 		}
720 	}
721 }
722 }	// anonymous
723 
createDepthClampTests(tcu::TestContext & testCtx,bool useDynamicRendering)724 tcu::TestCaseGroup*	createDepthClampTests (tcu::TestContext& testCtx, bool useDynamicRendering)
725 {
726 	return createTestGroup(testCtx, "depth_clamp", "Depth Clamp Tests", createTests, useDynamicRendering);
727 }
728 }	// Draw
729 }	// vkt
730