• 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,ShaderMap shaders)50 	BlendConstantsTestInstance (Context& context, ShaderMap shaders)
51 		: DynamicStateBaseClass	(context, shaders[glu::SHADERTYPE_VERTEX], shaders[glu::SHADERTYPE_FRAGMENT])
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 vk::Unique<vk::VkShaderModule> vs (createShaderModule(m_vk, device, m_context.getBinaryCollection().get(m_vertexShaderName), 0));
66 		const vk::Unique<vk::VkShaderModule> fs (createShaderModule(m_vk, device, m_context.getBinaryCollection().get(m_fragmentShaderName), 0));
67 
68 		const vk::VkPipelineColorBlendAttachmentState VkPipelineColorBlendAttachmentState =
69 			PipelineCreateInfo::ColorBlendState::Attachment(VK_TRUE,
70 															vk::VK_BLEND_FACTOR_SRC_ALPHA, vk::VK_BLEND_FACTOR_CONSTANT_COLOR, vk::VK_BLEND_OP_ADD,
71 															vk::VK_BLEND_FACTOR_SRC_ALPHA, vk::VK_BLEND_FACTOR_CONSTANT_ALPHA, vk::VK_BLEND_OP_ADD);
72 
73 		PipelineCreateInfo pipelineCreateInfo(*m_pipelineLayout, *m_renderPass, 0, 0);
74 		pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*vs, "main", vk::VK_SHADER_STAGE_VERTEX_BIT));
75 		pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*fs, "main", vk::VK_SHADER_STAGE_FRAGMENT_BIT));
76 		pipelineCreateInfo.addState(PipelineCreateInfo::VertexInputState(m_vertexInputState));
77 		pipelineCreateInfo.addState(PipelineCreateInfo::InputAssemblerState(m_topology));
78 		pipelineCreateInfo.addState(PipelineCreateInfo::ColorBlendState(1, &VkPipelineColorBlendAttachmentState));
79 		pipelineCreateInfo.addState(PipelineCreateInfo::ViewportState(1));
80 		pipelineCreateInfo.addState(PipelineCreateInfo::DepthStencilState());
81 		pipelineCreateInfo.addState(PipelineCreateInfo::RasterizerState());
82 		pipelineCreateInfo.addState(PipelineCreateInfo::MultiSampleState());
83 		pipelineCreateInfo.addState(PipelineCreateInfo::DynamicState());
84 
85 		m_pipeline = vk::createGraphicsPipeline(m_vk, device, DE_NULL, &pipelineCreateInfo);
86 	}
87 
iterate(void)88 	virtual tcu::TestStatus iterate (void)
89 	{
90 		tcu::TestLog&		log		= m_context.getTestContext().getLog();
91 		const vk::VkQueue	queue	= m_context.getUniversalQueue();
92 		const vk::VkDevice	device	= m_context.getDevice();
93 
94 		const vk::VkClearColorValue clearColor = { { 1.0f, 1.0f, 1.0f, 1.0f } };
95 		beginRenderPassWithClearColor(clearColor);
96 
97 		m_vk.cmdBindPipeline(*m_cmdBuffer, vk::VK_PIPELINE_BIND_POINT_GRAPHICS, *m_pipeline);
98 
99 		// bind states here
100 		setDynamicViewportState(WIDTH, HEIGHT);
101 		setDynamicRasterizationState();
102 		setDynamicDepthStencilState();
103 		setDynamicBlendState(0.33f, 0.1f, 0.66f, 0.5f);
104 
105 		const vk::VkDeviceSize vertexBufferOffset = 0;
106 		const vk::VkBuffer vertexBuffer = m_vertexBuffer->object();
107 		m_vk.cmdBindVertexBuffers(*m_cmdBuffer, 0, 1, &vertexBuffer, &vertexBufferOffset);
108 
109 		m_vk.cmdDraw(*m_cmdBuffer, static_cast<deUint32>(m_data.size()), 1, 0, 0);
110 
111 		endRenderPass(m_vk, *m_cmdBuffer);
112 		endCommandBuffer(m_vk, *m_cmdBuffer);
113 
114 		submitCommandsAndWait(m_vk, device, queue, m_cmdBuffer.get());
115 
116 		//validation
117 		{
118 			tcu::Texture2D referenceFrame(vk::mapVkFormat(m_colorAttachmentFormat), (int)(0.5f + static_cast<float>(WIDTH)), (int)(0.5f + static_cast<float>(HEIGHT)));
119 			referenceFrame.allocLevel(0);
120 
121 			const deInt32 frameWidth = referenceFrame.getWidth();
122 			const deInt32 frameHeight = referenceFrame.getHeight();
123 
124 			tcu::clear(referenceFrame.getLevel(0), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
125 
126 			for (int y = 0; y < frameHeight; y++)
127 			{
128 				const float yCoord = (float)(y / (0.5*frameHeight)) - 1.0f;
129 
130 				for (int x = 0; x < frameWidth; x++)
131 				{
132 					const float xCoord = (float)(x / (0.5*frameWidth)) - 1.0f;
133 
134 					if ((yCoord >= -1.0f && yCoord <= 1.0f && xCoord >= -1.0f && xCoord <= 1.0f))
135 						referenceFrame.getLevel(0).setPixel(tcu::Vec4(0.33f, 1.0f, 0.66f, 1.0f), x, y);
136 				}
137 			}
138 
139 			const vk::VkOffset3D zeroOffset = { 0, 0, 0 };
140 			const tcu::ConstPixelBufferAccess renderedFrame = m_colorTargetImage->readSurface(queue, m_context.getDefaultAllocator(),
141 																							  vk::VK_IMAGE_LAYOUT_GENERAL, zeroOffset, WIDTH, HEIGHT, vk::VK_IMAGE_ASPECT_COLOR_BIT);
142 
143 			if (!tcu::fuzzyCompare(log, "Result", "Image comparison result",
144 				referenceFrame.getLevel(0), renderedFrame, 0.05f,
145 				tcu::COMPARE_LOG_RESULT))
146 			{
147 				return tcu::TestStatus(QP_TEST_RESULT_FAIL, "Image verification failed");
148 			}
149 
150 			return tcu::TestStatus(QP_TEST_RESULT_PASS, "Image verification passed");
151 		}
152 	}
153 };
154 
155 } //anonymous
156 
DynamicStateCBTests(tcu::TestContext & testCtx)157 DynamicStateCBTests::DynamicStateCBTests (tcu::TestContext& testCtx)
158 	: TestCaseGroup (testCtx, "cb_state", "Tests for color blend state")
159 {
160 	/* Left blank on purpose */
161 }
162 
~DynamicStateCBTests(void)163 DynamicStateCBTests::~DynamicStateCBTests (void) {}
164 
init(void)165 void DynamicStateCBTests::init (void)
166 {
167 	ShaderMap shaderPaths;
168 	shaderPaths[glu::SHADERTYPE_VERTEX] = "vulkan/dynamic_state/VertexFetch.vert";
169 	shaderPaths[glu::SHADERTYPE_FRAGMENT] = "vulkan/dynamic_state/VertexFetch.frag";
170 	addChild(new InstanceFactory<BlendConstantsTestInstance>(m_testCtx, "blend_constants", "Check if blend constants are working properly", shaderPaths));
171 }
172 
173 } // DynamicState
174 } // vkt
175