xref: /aosp_15_r20/external/deqp/external/vulkancts/modules/vulkan/dynamic_state/vktDynamicStateCBTests.cpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
1 /*------------------------------------------------------------------------
2  * Vulkan Conformance Tests
3  * ------------------------
4  *
5  * Copyright (c) 2015 The Khronos Group Inc.
6  * Copyright (c) 2015 Intel Corporation
7  * Copyright (c) 2023 LunarG, Inc.
8  * Copyright (c) 2023 Nintendo
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  *//*!
23  * \file
24  * \brief Dynamic CB State Tests
25  *//*--------------------------------------------------------------------*/
26 
27 #include "vktDynamicStateCBTests.hpp"
28 
29 #include "vktDynamicStateBaseClass.hpp"
30 #include "vktDynamicStateTestCaseUtil.hpp"
31 
32 #include "vkImageUtil.hpp"
33 #include "vkCmdUtil.hpp"
34 
35 #include "tcuImageCompare.hpp"
36 #include "tcuTextureUtil.hpp"
37 #include "tcuRGBA.hpp"
38 
39 namespace vkt
40 {
41 namespace DynamicState
42 {
43 
44 using namespace Draw;
45 
46 namespace
47 {
48 
49 class BlendConstantsTestInstance : public DynamicStateBaseClass
50 {
51 public:
BlendConstantsTestInstance(Context & context,vk::PipelineConstructionType pipelineConstructionType,const ShaderMap & shaders)52     BlendConstantsTestInstance(Context &context, vk::PipelineConstructionType pipelineConstructionType,
53                                const ShaderMap &shaders)
54         : DynamicStateBaseClass(context, pipelineConstructionType, shaders.at(glu::SHADERTYPE_VERTEX),
55                                 shaders.at(glu::SHADERTYPE_FRAGMENT), shaders.at(glu::SHADERTYPE_MESH))
56     {
57         m_topology = vk::VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
58 
59         m_data.push_back(PositionColorVertex(tcu::Vec4(-1.0f, 1.0f, 1.0f, 1.0f), tcu::RGBA::green().toVec()));
60         m_data.push_back(PositionColorVertex(tcu::Vec4(1.0f, 1.0f, 1.0f, 1.0f), tcu::RGBA::green().toVec()));
61         m_data.push_back(PositionColorVertex(tcu::Vec4(-1.0f, -1.0f, 1.0f, 1.0f), tcu::RGBA::green().toVec()));
62         m_data.push_back(PositionColorVertex(tcu::Vec4(1.0f, -1.0f, 1.0f, 1.0f), tcu::RGBA::green().toVec()));
63 
64         DynamicStateBaseClass::initialize();
65     }
66 
initPipeline(const vk::VkDevice device)67     virtual void initPipeline(const vk::VkDevice device)
68     {
69         const auto &binaries = m_context.getBinaryCollection();
70         const vk::ShaderWrapper ms(m_isMesh ? vk::ShaderWrapper(m_vk, device, binaries.get(m_meshShaderName), 0) :
71                                               vk::ShaderWrapper());
72         const vk::ShaderWrapper vs(m_isMesh ? vk::ShaderWrapper() :
73                                               vk::ShaderWrapper(m_vk, device, binaries.get(m_vertexShaderName), 0));
74         const vk::ShaderWrapper fs(vk::ShaderWrapper(m_vk, device, binaries.get(m_fragmentShaderName), 0));
75         std::vector<vk::VkViewport> viewports{{0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f}};
76         std::vector<vk::VkRect2D> scissors{{{0u, 0u}, {0u, 0u}}};
77 
78         const PipelineCreateInfo::ColorBlendState::Attachment attachmentState(
79             VK_TRUE, vk::VK_BLEND_FACTOR_SRC_ALPHA, vk::VK_BLEND_FACTOR_CONSTANT_COLOR, vk::VK_BLEND_OP_ADD,
80             vk::VK_BLEND_FACTOR_SRC_ALPHA, vk::VK_BLEND_FACTOR_CONSTANT_ALPHA, vk::VK_BLEND_OP_ADD);
81         const PipelineCreateInfo::ColorBlendState colorBlendState(
82             1, static_cast<const vk::VkPipelineColorBlendAttachmentState *>(&attachmentState));
83         const PipelineCreateInfo::RasterizerState rasterizerState;
84         const PipelineCreateInfo::DepthStencilState depthStencilState;
85         const PipelineCreateInfo::DynamicState dynamicState;
86 
87         m_pipeline.setDefaultTopology(m_topology)
88             .setDynamicState(static_cast<const vk::VkPipelineDynamicStateCreateInfo *>(&dynamicState))
89             .setDefaultMultisampleState();
90 
91 #ifndef CTS_USES_VULKANSC
92         if (m_isMesh)
93         {
94             m_pipeline.setupPreRasterizationMeshShaderState(
95                 viewports, scissors, m_pipelineLayout, *m_renderPass, 0u, vk::ShaderWrapper(), ms,
96                 static_cast<const vk::VkPipelineRasterizationStateCreateInfo *>(&rasterizerState));
97         }
98         else
99 #endif // CTS_USES_VULKANSC
100         {
101             m_pipeline.setupVertexInputState(&m_vertexInputState)
102                 .setupPreRasterizationShaderState(
103                     viewports, scissors, m_pipelineLayout, *m_renderPass, 0u, vs,
104                     static_cast<const vk::VkPipelineRasterizationStateCreateInfo *>(&rasterizerState));
105         }
106 
107         m_pipeline
108             .setupFragmentShaderState(
109                 m_pipelineLayout, *m_renderPass, 0u, fs,
110                 static_cast<const vk::VkPipelineDepthStencilStateCreateInfo *>(&depthStencilState))
111             .setupFragmentOutputState(*m_renderPass, 0u,
112                                       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_pipeline.bind(*m_cmdBuffer);
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,
141                                        1u, &m_descriptorSet.get(), 0u, nullptr);
142             pushVertexOffset(0u, *m_pipelineLayout);
143             m_vk.cmdDrawMeshTasksEXT(*m_cmdBuffer, numVert - 2u, 1u, 1u);
144         }
145         else
146 #endif // CTS_USES_VULKANSC
147         {
148             const vk::VkDeviceSize vertexBufferOffset = 0;
149             const vk::VkBuffer vertexBuffer           = m_vertexBuffer->object();
150 
151             m_vk.cmdBindVertexBuffers(*m_cmdBuffer, 0, 1, &vertexBuffer, &vertexBufferOffset);
152             m_vk.cmdDraw(*m_cmdBuffer, static_cast<uint32_t>(m_data.size()), 1, 0, 0);
153         }
154 
155         m_renderPass.end(m_vk, *m_cmdBuffer);
156         endCommandBuffer(m_vk, *m_cmdBuffer);
157 
158         submitCommandsAndWait(m_vk, device, queue, m_cmdBuffer.get());
159 
160         //validation
161         {
162             tcu::Texture2D referenceFrame(vk::mapVkFormat(m_colorAttachmentFormat),
163                                           (int)(0.5f + static_cast<float>(WIDTH)),
164                                           (int)(0.5f + static_cast<float>(HEIGHT)));
165             referenceFrame.allocLevel(0);
166 
167             const int32_t frameWidth  = referenceFrame.getWidth();
168             const int32_t frameHeight = referenceFrame.getHeight();
169 
170             tcu::clear(referenceFrame.getLevel(0), tcu::Vec4(0.0f, 0.0f, 0.0f, 1.0f));
171 
172             for (int y = 0; y < frameHeight; y++)
173             {
174                 const float yCoord = (float)(y / (0.5 * frameHeight)) - 1.0f;
175 
176                 for (int x = 0; x < frameWidth; x++)
177                 {
178                     const float xCoord = (float)(x / (0.5 * frameWidth)) - 1.0f;
179 
180                     if ((yCoord >= -1.0f && yCoord <= 1.0f && xCoord >= -1.0f && xCoord <= 1.0f))
181                         referenceFrame.getLevel(0).setPixel(tcu::Vec4(0.33f, 1.0f, 0.66f, 1.0f), x, y);
182                 }
183             }
184 
185             const vk::VkOffset3D zeroOffset = {0, 0, 0};
186             const tcu::ConstPixelBufferAccess renderedFrame =
187                 m_colorTargetImage->readSurface(queue, m_context.getDefaultAllocator(), vk::VK_IMAGE_LAYOUT_GENERAL,
188                                                 zeroOffset, WIDTH, HEIGHT, vk::VK_IMAGE_ASPECT_COLOR_BIT);
189 
190             if (!tcu::fuzzyCompare(log, "Result", "Image comparison result", referenceFrame.getLevel(0), renderedFrame,
191                                    0.05f, tcu::COMPARE_LOG_RESULT))
192             {
193                 return tcu::TestStatus(QP_TEST_RESULT_FAIL, "Image verification failed");
194             }
195 
196             return tcu::TestStatus(QP_TEST_RESULT_PASS, "Image verification passed");
197         }
198     }
199 };
200 
201 #ifndef CTS_USES_VULKANSC
checkMeshShaderSupport(Context & context)202 void checkMeshShaderSupport(Context &context)
203 {
204     context.requireDeviceFunctionality("VK_EXT_mesh_shader");
205 }
206 #endif // CTS_USES_VULKANSC
207 
208 } // namespace
209 
DynamicStateCBTests(tcu::TestContext & testCtx,vk::PipelineConstructionType pipelineConstructionType)210 DynamicStateCBTests::DynamicStateCBTests(tcu::TestContext &testCtx,
211                                          vk::PipelineConstructionType pipelineConstructionType)
212     : TestCaseGroup(testCtx, "cb_state")
213     , m_pipelineConstructionType(pipelineConstructionType)
214 {
215     /* Left blank on purpose */
216 }
217 
~DynamicStateCBTests(void)218 DynamicStateCBTests::~DynamicStateCBTests(void)
219 {
220 }
221 
init(void)222 void DynamicStateCBTests::init(void)
223 {
224     ShaderMap pathsBase;
225     pathsBase[glu::SHADERTYPE_FRAGMENT] = "vulkan/dynamic_state/VertexFetch.frag";
226     pathsBase[glu::SHADERTYPE_VERTEX]   = nullptr;
227     pathsBase[glu::SHADERTYPE_MESH]     = nullptr;
228 
229     {
230         ShaderMap shaderPaths(pathsBase);
231         shaderPaths[glu::SHADERTYPE_VERTEX] = "vulkan/dynamic_state/VertexFetch.vert";
232         // Check if blend constants are working properly
233         addChild(new InstanceFactory<BlendConstantsTestInstance>(m_testCtx, "blend_constants",
234                                                                  m_pipelineConstructionType, shaderPaths));
235     }
236 #ifndef CTS_USES_VULKANSC
237     {
238         ShaderMap shaderPaths(pathsBase);
239         shaderPaths[glu::SHADERTYPE_MESH] = "vulkan/dynamic_state/VertexFetch.mesh";
240         // Check if blend constants are working properly in mesh shaders
241         addChild(new InstanceFactory<BlendConstantsTestInstance, FunctionSupport0>(
242             m_testCtx, "blend_constants_mesh", m_pipelineConstructionType, shaderPaths, checkMeshShaderSupport));
243     }
244 #endif // CTS_USES_VULKANSC
245 }
246 
247 } // namespace DynamicState
248 } // namespace vkt
249