• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2015 The Khronos Group Inc.
6  * Copyright (c) 2015 Intel Corporation
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 Dynamic CB State Tests
23  *//*--------------------------------------------------------------------*/
24 
25 #include "vktDynamicStateCBTests.hpp"
26 
27 #include "vktDynamicStateBaseClass.hpp"
28 #include "vktDynamicStateTestCaseUtil.hpp"
29 
30 #include "vkImageUtil.hpp"
31 #include "vkCmdUtil.hpp"
32 
33 #include "tcuImageCompare.hpp"
34 #include "tcuTextureUtil.hpp"
35 #include "tcuRGBA.hpp"
36 
37 namespace vkt
38 {
39 namespace DynamicState
40 {
41 
42 using namespace Draw;
43 
44 namespace
45 {
46 
47 class BlendConstantsTestInstance : public DynamicStateBaseClass
48 {
49 public:
BlendConstantsTestInstance(Context & context,vk::PipelineConstructionType pipelineConstructionType,const ShaderMap & shaders)50 	BlendConstantsTestInstance (Context& context, vk::PipelineConstructionType pipelineConstructionType, const ShaderMap& shaders)
51 		: DynamicStateBaseClass	(context, pipelineConstructionType, shaders.at(glu::SHADERTYPE_VERTEX), shaders.at(glu::SHADERTYPE_FRAGMENT), shaders.at(glu::SHADERTYPE_MESH))
52 	{
53 		m_topology = vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
54 
55 		m_data.push_back(PositionColorVertex(tcu::Vec4(-1.0f, 1.0f, 1.0f, 1.0f), tcu::RGBA::green().toVec()));
56 		m_data.push_back(PositionColorVertex(tcu::Vec4(1.0f, 1.0f, 1.0f, 1.0f), tcu::RGBA::green().toVec()));
57 		m_data.push_back(PositionColorVertex(tcu::Vec4(-1.0f, -1.0f, 1.0f, 1.0f), tcu::RGBA::green().toVec()));
58 		m_data.push_back(PositionColorVertex(tcu::Vec4(1.0f, -1.0f, 1.0f, 1.0f), tcu::RGBA::green().toVec()));
59 
60 		DynamicStateBaseClass::initialize();
61 	}
62 
initPipeline(const vk::VkDevice device)63 	virtual void initPipeline (const vk::VkDevice device)
64 	{
65 		const auto&							binaries	= m_context.getBinaryCollection();
66 		const vk::Move<vk::VkShaderModule>	ms			(m_isMesh ? createShaderModule(m_vk, device, binaries.get(m_meshShaderName), 0) : vk::Move<vk::VkShaderModule>());
67 		const vk::Move<vk::VkShaderModule>	vs			(m_isMesh ? vk::Move<vk::VkShaderModule>() : createShaderModule(m_vk, device, binaries.get(m_vertexShaderName), 0));
68 		const vk::Move<vk::VkShaderModule>	fs			(createShaderModule(m_vk, device, binaries.get(m_fragmentShaderName), 0));
69 		std::vector<vk::VkViewport>			viewports	{ { 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f } };
70 		std::vector<vk::VkRect2D>			scissors	{ { { 0u, 0u }, { 0u, 0u } } };
71 
72 		const PipelineCreateInfo::ColorBlendState::Attachment	attachmentState(VK_TRUE,
73 																				vk::VK_BLEND_FACTOR_SRC_ALPHA, vk::VK_BLEND_FACTOR_CONSTANT_COLOR, vk::VK_BLEND_OP_ADD,
74 																				vk::VK_BLEND_FACTOR_SRC_ALPHA, vk::VK_BLEND_FACTOR_CONSTANT_ALPHA, vk::VK_BLEND_OP_ADD);
75 		const PipelineCreateInfo::ColorBlendState				colorBlendState(1, static_cast<const vk::VkPipelineColorBlendAttachmentState*>(&attachmentState));
76 		const PipelineCreateInfo::RasterizerState				rasterizerState;
77 		const PipelineCreateInfo::DepthStencilState				depthStencilState;
78 		const PipelineCreateInfo::DynamicState					dynamicState;
79 
80 		m_pipeline.setDefaultTopology(m_topology)
81 				  .setDynamicState(static_cast<const vk::VkPipelineDynamicStateCreateInfo*>(&dynamicState))
82 				  .setDefaultMultisampleState();
83 
84 #ifndef CTS_USES_VULKANSC
85 		if (m_isMesh)
86 		{
87 			m_pipeline
88 				  .setupPreRasterizationMeshShaderState(viewports,
89 														scissors,
90 														*m_pipelineLayout,
91 														*m_renderPass,
92 														0u,
93 														DE_NULL,
94 														*ms,
95 														static_cast<const vk::VkPipelineRasterizationStateCreateInfo*>(&rasterizerState));
96 		}
97 		else
98 #endif // CTS_USES_VULKANSC
99 		{
100 			m_pipeline
101 				  .setupVertexInputState(&m_vertexInputState)
102 				  .setupPreRasterizationShaderState(viewports,
103 													scissors,
104 													*m_pipelineLayout,
105 													*m_renderPass,
106 													0u,
107 													*vs,
108 													static_cast<const vk::VkPipelineRasterizationStateCreateInfo*>(&rasterizerState));
109 		}
110 
111 		m_pipeline.setupFragmentShaderState(*m_pipelineLayout, *m_renderPass, 0u, *fs, static_cast<const vk::VkPipelineDepthStencilStateCreateInfo*>(&depthStencilState))
112 				  .setupFragmentOutputState(*m_renderPass, 0u, static_cast<const vk::VkPipelineColorBlendStateCreateInfo*>(&colorBlendState))
113 				  .setMonolithicPipelineLayout(*m_pipelineLayout)
114 				  .buildPipeline();
115 	}
116 
iterate(void)117 	virtual tcu::TestStatus iterate (void)
118 	{
119 		tcu::TestLog&		log		= m_context.getTestContext().getLog();
120 		const vk::VkQueue	queue	= m_context.getUniversalQueue();
121 		const vk::VkDevice	device	= m_context.getDevice();
122 
123 		const vk::VkClearColorValue clearColor = { { 1.0f, 1.0f, 1.0f, 1.0f } };
124 		beginRenderPassWithClearColor(clearColor);
125 
126 		m_vk.cmdBindPipeline(*m_cmdBuffer, vk::VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipeline.getPipeline());
127 
128 		// bind states here
129 		setDynamicViewportState(WIDTH, HEIGHT);
130 		setDynamicRasterizationState();
131 		setDynamicDepthStencilState();
132 		setDynamicBlendState(0.33f, 0.1f, 0.66f, 0.5f);
133 
134 #ifndef CTS_USES_VULKANSC
135 		if (m_isMesh)
136 		{
137 			const auto numVert = static_cast<uint32_t>(m_data.size());
138 			DE_ASSERT(numVert >= 2u);
139 
140 			m_vk.cmdBindDescriptorSets(*m_cmdBuffer, vk::VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineLayout.get(), 0u, 1u, &m_descriptorSet.get(), 0u, nullptr);
141 			pushVertexOffset(0u, *m_pipelineLayout);
142 			m_vk.cmdDrawMeshTasksEXT(*m_cmdBuffer, numVert - 2u, 1u, 1u);
143 		}
144 		else
145 #endif // CTS_USES_VULKANSC
146 		{
147 			const vk::VkDeviceSize	vertexBufferOffset	= 0;
148 			const vk::VkBuffer		vertexBuffer		= m_vertexBuffer->object();
149 
150 			m_vk.cmdBindVertexBuffers(*m_cmdBuffer, 0, 1, &vertexBuffer, &vertexBufferOffset);
151 			m_vk.cmdDraw(*m_cmdBuffer, static_cast<deUint32>(m_data.size()), 1, 0, 0);
152 		}
153 
154 		endRenderPass(m_vk, *m_cmdBuffer);
155 		endCommandBuffer(m_vk, *m_cmdBuffer);
156 
157 		submitCommandsAndWait(m_vk, device, queue, m_cmdBuffer.get());
158 
159 		//validation
160 		{
161 			tcu::Texture2D referenceFrame(vk::mapVkFormat(m_colorAttachmentFormat), (int)(0.5f + static_cast<float>(WIDTH)), (int)(0.5f + static_cast<float>(HEIGHT)));
162 			referenceFrame.allocLevel(0);
163 
164 			const deInt32 frameWidth = referenceFrame.getWidth();
165 			const deInt32 frameHeight = referenceFrame.getHeight();
166 
167 			tcu::clear(referenceFrame.getLevel(0), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
168 
169 			for (int y = 0; y < frameHeight; y++)
170 			{
171 				const float yCoord = (float)(y / (0.5*frameHeight)) - 1.0f;
172 
173 				for (int x = 0; x < frameWidth; x++)
174 				{
175 					const float xCoord = (float)(x / (0.5*frameWidth)) - 1.0f;
176 
177 					if ((yCoord >= -1.0f && yCoord <= 1.0f && xCoord >= -1.0f && xCoord <= 1.0f))
178 						referenceFrame.getLevel(0).setPixel(tcu::Vec4(0.33f, 1.0f, 0.66f, 1.0f), x, y);
179 				}
180 			}
181 
182 			const vk::VkOffset3D zeroOffset = { 0, 0, 0 };
183 			const tcu::ConstPixelBufferAccess renderedFrame = m_colorTargetImage->readSurface(queue, m_context.getDefaultAllocator(),
184 																							  vk::VK_IMAGE_LAYOUT_GENERAL, zeroOffset, WIDTH, HEIGHT, vk::VK_IMAGE_ASPECT_COLOR_BIT);
185 
186 			if (!tcu::fuzzyCompare(log, "Result", "Image comparison result",
187 				referenceFrame.getLevel(0), renderedFrame, 0.05f,
188 				tcu::COMPARE_LOG_RESULT))
189 			{
190 				return tcu::TestStatus(QP_TEST_RESULT_FAIL, "Image verification failed");
191 			}
192 
193 			return tcu::TestStatus(QP_TEST_RESULT_PASS, "Image verification passed");
194 		}
195 	}
196 };
197 
198 #ifndef CTS_USES_VULKANSC
checkMeshShaderSupport(Context & context)199 void checkMeshShaderSupport (Context& context)
200 {
201 	context.requireDeviceFunctionality("VK_EXT_mesh_shader");
202 }
203 #endif // CTS_USES_VULKANSC
204 
205 } //anonymous
206 
DynamicStateCBTests(tcu::TestContext & testCtx,vk::PipelineConstructionType pipelineConstructionType)207 DynamicStateCBTests::DynamicStateCBTests (tcu::TestContext& testCtx, vk::PipelineConstructionType pipelineConstructionType)
208 	: TestCaseGroup					(testCtx, "cb_state", "Tests for color blend state")
209 	, m_pipelineConstructionType	(pipelineConstructionType)
210 {
211 	/* Left blank on purpose */
212 }
213 
~DynamicStateCBTests(void)214 DynamicStateCBTests::~DynamicStateCBTests (void) {}
215 
init(void)216 void DynamicStateCBTests::init (void)
217 {
218 	ShaderMap pathsBase;
219 	pathsBase[glu::SHADERTYPE_FRAGMENT] = "vulkan/dynamic_state/VertexFetch.frag";
220 	pathsBase[glu::SHADERTYPE_VERTEX] = nullptr;
221 	pathsBase[glu::SHADERTYPE_MESH] = nullptr;
222 
223 	{
224 		ShaderMap shaderPaths(pathsBase);
225 		shaderPaths[glu::SHADERTYPE_VERTEX] = "vulkan/dynamic_state/VertexFetch.vert";
226 		addChild(new InstanceFactory<BlendConstantsTestInstance>(m_testCtx, "blend_constants", "Check if blend constants are working properly", m_pipelineConstructionType, shaderPaths));
227 	}
228 #ifndef CTS_USES_VULKANSC
229 	{
230 		ShaderMap shaderPaths(pathsBase);
231 		shaderPaths[glu::SHADERTYPE_MESH] = "vulkan/dynamic_state/VertexFetch.mesh";
232 		addChild(new InstanceFactory<BlendConstantsTestInstance, FunctionSupport0>(m_testCtx, "blend_constants_mesh", "Check if blend constants are working properly in mesh shaders", m_pipelineConstructionType, shaderPaths, checkMeshShaderSupport));
233 	}
234 #endif // CTS_USES_VULKANSC
235 }
236 
237 } // DynamicState
238 } // vkt
239