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