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 
32 #include "tcuImageCompare.hpp"
33 #include "tcuTextureUtil.hpp"
34 #include "tcuRGBA.hpp"
35 
36 namespace vkt
37 {
38 namespace DynamicState
39 {
40 namespace
41 {
42 
43 class BlendConstantsTestInstance : public DynamicStateBaseClass
44 {
45 public:
BlendConstantsTestInstance(Context & context,ShaderMap shaders)46 	BlendConstantsTestInstance (Context& context, ShaderMap shaders)
47 		: DynamicStateBaseClass	(context, shaders[glu::SHADERTYPE_VERTEX], shaders[glu::SHADERTYPE_FRAGMENT])
48 	{
49 		m_topology = vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
50 
51 		m_data.push_back(PositionColorVertex(tcu::Vec4(-1.0f, 1.0f, 1.0f, 1.0f), tcu::RGBA::green().toVec()));
52 		m_data.push_back(PositionColorVertex(tcu::Vec4(1.0f, 1.0f, 1.0f, 1.0f), tcu::RGBA::green().toVec()));
53 		m_data.push_back(PositionColorVertex(tcu::Vec4(-1.0f, -1.0f, 1.0f, 1.0f), tcu::RGBA::green().toVec()));
54 		m_data.push_back(PositionColorVertex(tcu::Vec4(1.0f, -1.0f, 1.0f, 1.0f), tcu::RGBA::green().toVec()));
55 
56 		DynamicStateBaseClass::initialize();
57 	}
58 
initPipeline(const vk::VkDevice device)59 	virtual void initPipeline (const vk::VkDevice device)
60 	{
61 		const vk::Unique<vk::VkShaderModule> vs (createShaderModule(m_vk, device, m_context.getBinaryCollection().get(m_vertexShaderName), 0));
62 		const vk::Unique<vk::VkShaderModule> fs (createShaderModule(m_vk, device, m_context.getBinaryCollection().get(m_fragmentShaderName), 0));
63 
64 		const vk::VkPipelineColorBlendAttachmentState VkPipelineColorBlendAttachmentState =
65 			PipelineCreateInfo::ColorBlendState::Attachment(vk::VK_TRUE,
66 															vk::VK_BLEND_FACTOR_SRC_ALPHA, vk::VK_BLEND_FACTOR_CONSTANT_COLOR, vk::VK_BLEND_OP_ADD,
67 															vk::VK_BLEND_FACTOR_SRC_ALPHA, vk::VK_BLEND_FACTOR_CONSTANT_ALPHA, vk::VK_BLEND_OP_ADD);
68 
69 		PipelineCreateInfo pipelineCreateInfo(*m_pipelineLayout, *m_renderPass, 0, 0);
70 		pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*vs, "main", vk::VK_SHADER_STAGE_VERTEX_BIT));
71 		pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*fs, "main", vk::VK_SHADER_STAGE_FRAGMENT_BIT));
72 		pipelineCreateInfo.addState(PipelineCreateInfo::VertexInputState(m_vertexInputState));
73 		pipelineCreateInfo.addState(PipelineCreateInfo::InputAssemblerState(m_topology));
74 		pipelineCreateInfo.addState(PipelineCreateInfo::ColorBlendState(1, &VkPipelineColorBlendAttachmentState));
75 		pipelineCreateInfo.addState(PipelineCreateInfo::ViewportState(1));
76 		pipelineCreateInfo.addState(PipelineCreateInfo::DepthStencilState());
77 		pipelineCreateInfo.addState(PipelineCreateInfo::RasterizerState());
78 		pipelineCreateInfo.addState(PipelineCreateInfo::MultiSampleState());
79 		pipelineCreateInfo.addState(PipelineCreateInfo::DynamicState());
80 
81 		m_pipeline = vk::createGraphicsPipeline(m_vk, device, DE_NULL, &pipelineCreateInfo);
82 	}
83 
iterate(void)84 	virtual tcu::TestStatus iterate (void)
85 	{
86 		tcu::TestLog &log = m_context.getTestContext().getLog();
87 		const vk::VkQueue queue = m_context.getUniversalQueue();
88 
89 		const vk::VkClearColorValue clearColor = { { 1.0f, 1.0f, 1.0f, 1.0f } };
90 		beginRenderPassWithClearColor(clearColor);
91 
92 		// bind states here
93 		setDynamicViewportState(WIDTH, HEIGHT);
94 		setDynamicRasterizationState();
95 		setDynamicDepthStencilState();
96 		setDynamicBlendState(0.33f, 0.1f, 0.66f, 0.5f);
97 
98 		m_vk.cmdBindPipeline(*m_cmdBuffer, vk::VK_PIPELINE_BIND_POINT_GRAPHICS, *m_pipeline);
99 
100 		const vk::VkDeviceSize vertexBufferOffset = 0;
101 		const vk::VkBuffer vertexBuffer = m_vertexBuffer->object();
102 		m_vk.cmdBindVertexBuffers(*m_cmdBuffer, 0, 1, &vertexBuffer, &vertexBufferOffset);
103 
104 		m_vk.cmdDraw(*m_cmdBuffer, static_cast<deUint32>(m_data.size()), 1, 0, 0);
105 
106 		m_vk.cmdEndRenderPass(*m_cmdBuffer);
107 		m_vk.endCommandBuffer(*m_cmdBuffer);
108 
109 		vk::VkSubmitInfo submitInfo =
110 		{
111 			vk::VK_STRUCTURE_TYPE_SUBMIT_INFO,	// VkStructureType			sType;
112 			DE_NULL,							// const void*				pNext;
113 			0,									// deUint32					waitSemaphoreCount;
114 			DE_NULL,							// const VkSemaphore*		pWaitSemaphores;
115 			(const vk::VkPipelineStageFlags*)DE_NULL,
116 			1,									// deUint32					commandBufferCount;
117 			&m_cmdBuffer.get(),					// const VkCommandBuffer*	pCommandBuffers;
118 			0,									// deUint32					signalSemaphoreCount;
119 			DE_NULL								// const VkSemaphore*		pSignalSemaphores;
120 		};
121 		m_vk.queueSubmit(queue, 1, &submitInfo, DE_NULL);
122 		VK_CHECK(m_vk.queueWaitIdle(queue));
123 
124 		//validation
125 		{
126 			tcu::Texture2D referenceFrame(vk::mapVkFormat(m_colorAttachmentFormat), (int)(0.5 + WIDTH), (int)(0.5 + HEIGHT));
127 			referenceFrame.allocLevel(0);
128 
129 			const deInt32 frameWidth = referenceFrame.getWidth();
130 			const deInt32 frameHeight = referenceFrame.getHeight();
131 
132 			tcu::clear(referenceFrame.getLevel(0), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
133 
134 			for (int y = 0; y < frameHeight; y++)
135 			{
136 				const float yCoord = (float)(y / (0.5*frameHeight)) - 1.0f;
137 
138 				for (int x = 0; x < frameWidth; x++)
139 				{
140 					const float xCoord = (float)(x / (0.5*frameWidth)) - 1.0f;
141 
142 					if ((yCoord >= -1.0f && yCoord <= 1.0f && xCoord >= -1.0f && xCoord <= 1.0f))
143 						referenceFrame.getLevel(0).setPixel(tcu::Vec4(0.33f, 1.0f, 0.66f, 1.0f), x, y);
144 				}
145 			}
146 
147 			const vk::VkOffset3D zeroOffset = { 0, 0, 0 };
148 			const tcu::ConstPixelBufferAccess renderedFrame = m_colorTargetImage->readSurface(queue, m_context.getDefaultAllocator(),
149 																							  vk::VK_IMAGE_LAYOUT_GENERAL, zeroOffset, WIDTH, HEIGHT, vk::VK_IMAGE_ASPECT_COLOR_BIT);
150 
151 			if (!tcu::fuzzyCompare(log, "Result", "Image comparison result",
152 				referenceFrame.getLevel(0), renderedFrame, 0.05f,
153 				tcu::COMPARE_LOG_RESULT))
154 			{
155 				return tcu::TestStatus(QP_TEST_RESULT_FAIL, "Image verification failed");
156 			}
157 
158 			return tcu::TestStatus(QP_TEST_RESULT_PASS, "Image verification passed");
159 		}
160 	}
161 };
162 
163 } //anonymous
164 
DynamicStateCBTests(tcu::TestContext & testCtx)165 DynamicStateCBTests::DynamicStateCBTests (tcu::TestContext& testCtx)
166 	: TestCaseGroup (testCtx, "cb_state", "Tests for color blend state")
167 {
168 	/* Left blank on purpose */
169 }
170 
~DynamicStateCBTests(void)171 DynamicStateCBTests::~DynamicStateCBTests (void) {}
172 
init(void)173 void DynamicStateCBTests::init (void)
174 {
175 	ShaderMap shaderPaths;
176 	shaderPaths[glu::SHADERTYPE_VERTEX] = "vulkan/dynamic_state/VertexFetch.vert";
177 	shaderPaths[glu::SHADERTYPE_FRAGMENT] = "vulkan/dynamic_state/VertexFetch.frag";
178 	addChild(new InstanceFactory<BlendConstantsTestInstance>(m_testCtx, "blend_constants", "Check if blend constants are working properly", shaderPaths));
179 }
180 
181 } // DynamicState
182 } // vkt
183