Merge branch 'jekstrand_renderpass_transfer_bit_fix' into '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  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and/or associated documentation files (the
10  * "Materials"), to deal in the Materials without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Materials, and to
13  * permit persons to whom the Materials are furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice(s) and this permission notice shall be included
17  * in all copies or substantial portions of the Materials.
18  *
19  * The Materials are Confidential Information as defined by the
20  * Khronos Membership Agreement until designated non-confidential by Khronos,
21  * at which point this condition clause shall be removed.
22  *
23  * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
26  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
27  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
28  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
29  * MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
30  *
31  *//*!
32  * \file
33  * \brief Dynamic CB State Tests
34  *//*--------------------------------------------------------------------*/
35
36 #include "vktDynamicStateCBTests.hpp"
37
38 #include "vktDynamicStateBaseClass.hpp"
39 #include "vktDynamicStateTestCaseUtil.hpp"
40
41 #include "vkImageUtil.hpp"
42
43 #include "tcuImageCompare.hpp"
44 #include "tcuTextureUtil.hpp"
45 #include "tcuRGBA.hpp"
46
47 namespace vkt
48 {
49 namespace DynamicState
50 {
51 namespace
52 {
53
54 class BlendConstantsTestInstance : public DynamicStateBaseClass
55 {
56 public:
57         BlendConstantsTestInstance (Context& context, ShaderMap shaders)
58                 : DynamicStateBaseClass (context, shaders[glu::SHADERTYPE_VERTEX], shaders[glu::SHADERTYPE_FRAGMENT])
59         {
60                 m_topology = vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
61
62                 m_data.push_back(PositionColorVertex(tcu::Vec4(-1.0f, 1.0f, 1.0f, 1.0f), tcu::RGBA::green().toVec()));
63                 m_data.push_back(PositionColorVertex(tcu::Vec4(1.0f, 1.0f, 1.0f, 1.0f), tcu::RGBA::green().toVec()));
64                 m_data.push_back(PositionColorVertex(tcu::Vec4(-1.0f, -1.0f, 1.0f, 1.0f), tcu::RGBA::green().toVec()));
65                 m_data.push_back(PositionColorVertex(tcu::Vec4(1.0f, -1.0f, 1.0f, 1.0f), tcu::RGBA::green().toVec()));
66
67                 DynamicStateBaseClass::initialize();
68         }
69
70         virtual void initPipeline (const vk::VkDevice device)
71         {
72                 const vk::Unique<vk::VkShaderModule> vs (createShaderModule(m_vk, device, m_context.getBinaryCollection().get(m_vertexShaderName), 0));
73                 const vk::Unique<vk::VkShaderModule> fs (createShaderModule(m_vk, device, m_context.getBinaryCollection().get(m_fragmentShaderName), 0));
74
75                 const vk::VkPipelineColorBlendAttachmentState VkPipelineColorBlendAttachmentState =
76                         PipelineCreateInfo::ColorBlendState::Attachment(vk::VK_TRUE,
77                                                                                                                         vk::VK_BLEND_FACTOR_SRC_ALPHA, vk::VK_BLEND_FACTOR_CONSTANT_COLOR, vk::VK_BLEND_OP_ADD,
78                                                                                                                         vk::VK_BLEND_FACTOR_SRC_ALPHA, vk::VK_BLEND_FACTOR_CONSTANT_ALPHA, vk::VK_BLEND_OP_ADD);
79
80                 PipelineCreateInfo pipelineCreateInfo(*m_pipelineLayout, *m_renderPass, 0, 0);
81                 pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*vs, "main", vk::VK_SHADER_STAGE_VERTEX_BIT));
82                 pipelineCreateInfo.addShader(PipelineCreateInfo::PipelineShaderStage(*fs, "main", vk::VK_SHADER_STAGE_FRAGMENT_BIT));
83                 pipelineCreateInfo.addState(PipelineCreateInfo::VertexInputState(m_vertexInputState));
84                 pipelineCreateInfo.addState(PipelineCreateInfo::InputAssemblerState(m_topology));
85                 pipelineCreateInfo.addState(PipelineCreateInfo::ColorBlendState(1, &VkPipelineColorBlendAttachmentState));
86                 pipelineCreateInfo.addState(PipelineCreateInfo::ViewportState(1));
87                 pipelineCreateInfo.addState(PipelineCreateInfo::DepthStencilState());
88                 pipelineCreateInfo.addState(PipelineCreateInfo::RasterizerState());
89                 pipelineCreateInfo.addState(PipelineCreateInfo::MultiSampleState());
90                 pipelineCreateInfo.addState(PipelineCreateInfo::DynamicState());
91
92                 m_pipeline = vk::createGraphicsPipeline(m_vk, device, DE_NULL, &pipelineCreateInfo);
93         }
94
95         virtual tcu::TestStatus iterate (void)
96         {
97                 tcu::TestLog &log = m_context.getTestContext().getLog();
98                 const vk::VkQueue queue = m_context.getUniversalQueue();
99
100                 const vk::VkClearColorValue clearColor = { { 1.0f, 1.0f, 1.0f, 1.0f } };
101                 beginRenderPassWithClearColor(clearColor);
102
103                 // bind states here
104                 setDynamicViewportState(WIDTH, HEIGHT);
105                 setDynamicRasterizationState();
106                 setDynamicDepthStencilState();
107                 setDynamicBlendState(0.33f, 0.1f, 0.66f, 0.5f);
108
109                 m_vk.cmdBindPipeline(*m_cmdBuffer, vk::VK_PIPELINE_BIND_POINT_GRAPHICS, *m_pipeline);
110
111                 const vk::VkDeviceSize vertexBufferOffset = 0;
112                 const vk::VkBuffer vertexBuffer = m_vertexBuffer->object();
113                 m_vk.cmdBindVertexBuffers(*m_cmdBuffer, 0, 1, &vertexBuffer, &vertexBufferOffset);
114
115                 m_vk.cmdDraw(*m_cmdBuffer, static_cast<deUint32>(m_data.size()), 1, 0, 0);
116
117                 m_vk.cmdEndRenderPass(*m_cmdBuffer);
118                 m_vk.endCommandBuffer(*m_cmdBuffer);
119
120                 vk::VkSubmitInfo submitInfo =
121                 {
122                         vk::VK_STRUCTURE_TYPE_SUBMIT_INFO,      // VkStructureType                      sType;
123                         DE_NULL,                                                        // const void*                          pNext;
124                         0,                                                                      // deUint32                                     waitSemaphoreCount;
125                         DE_NULL,                                                        // const VkSemaphore*           pWaitSemaphores;
126                         (const vk::VkPipelineStageFlags*)DE_NULL,
127                         1,                                                                      // deUint32                                     commandBufferCount;
128                         &m_cmdBuffer.get(),                                     // const VkCommandBuffer*       pCommandBuffers;
129                         0,                                                                      // deUint32                                     signalSemaphoreCount;
130                         DE_NULL                                                         // const VkSemaphore*           pSignalSemaphores;
131                 };
132                 m_vk.queueSubmit(queue, 1, &submitInfo, DE_NULL);
133                 VK_CHECK(m_vk.queueWaitIdle(queue));
134
135                 //validation
136                 {
137                         tcu::Texture2D referenceFrame(vk::mapVkFormat(m_colorAttachmentFormat), (int)(0.5 + WIDTH), (int)(0.5 + HEIGHT));
138                         referenceFrame.allocLevel(0);
139
140                         const deInt32 frameWidth = referenceFrame.getWidth();
141                         const deInt32 frameHeight = referenceFrame.getHeight();
142
143                         tcu::clear(referenceFrame.getLevel(0), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
144
145                         for (int y = 0; y < frameHeight; y++)
146                         {
147                                 const float yCoord = (float)(y / (0.5*frameHeight)) - 1.0f;
148
149                                 for (int x = 0; x < frameWidth; x++)
150                                 {
151                                         const float xCoord = (float)(x / (0.5*frameWidth)) - 1.0f;
152
153                                         if ((yCoord >= -1.0f && yCoord <= 1.0f && xCoord >= -1.0f && xCoord <= 1.0f))
154                                                 referenceFrame.getLevel(0).setPixel(tcu::Vec4(0.33f, 1.0f, 0.66f, 1.0f), x, y);
155                                 }
156                         }
157
158                         const vk::VkOffset3D zeroOffset = { 0, 0, 0 };
159                         const tcu::ConstPixelBufferAccess renderedFrame = m_colorTargetImage->readSurface(queue, m_context.getDefaultAllocator(),
160                                                                                                                                                                                           vk::VK_IMAGE_LAYOUT_GENERAL, zeroOffset, WIDTH, HEIGHT, vk::VK_IMAGE_ASPECT_COLOR_BIT);
161
162                         if (!tcu::fuzzyCompare(log, "Result", "Image comparison result",
163                                 referenceFrame.getLevel(0), renderedFrame, 0.05f,
164                                 tcu::COMPARE_LOG_RESULT))
165                         {
166                                 return tcu::TestStatus(QP_TEST_RESULT_FAIL, "Image verification failed");
167                         }
168
169                         return tcu::TestStatus(QP_TEST_RESULT_PASS, "Image verification passed");
170                 }
171         }
172 };
173
174 } //anonymous
175
176 DynamicStateCBTests::DynamicStateCBTests (tcu::TestContext& testCtx)
177         : TestCaseGroup (testCtx, "cb_state", "Tests for color blend state")
178 {
179         /* Left blank on purpose */
180 }
181
182 DynamicStateCBTests::~DynamicStateCBTests (void) {}
183
184 void DynamicStateCBTests::init (void)
185 {
186         ShaderMap shaderPaths;
187         shaderPaths[glu::SHADERTYPE_VERTEX] = "vulkan/dynamic_state/VertexFetch.vert";
188         shaderPaths[glu::SHADERTYPE_FRAGMENT] = "vulkan/dynamic_state/VertexFetch.frag";
189         addChild(new InstanceFactory<BlendConstantsTestInstance>(m_testCtx, "blend_constants", "Check if blend constants are working properly", shaderPaths));
190 }
191
192 } // DynamicState
193 } // vkt