xref: /aosp_15_r20/external/deqp/external/vulkancts/modules/vulkan/util/vktDrawUtil.hpp (revision 35238bce31c2a825756842865a792f8cf7f89930)
1 #ifndef _VKTDRAWUTIL_HPP
2 #define _VKTDRAWUTIL_HPP
3 /*-------------------------------------------------------------------------
4  * Vulkan CTS Framework
5  * --------------------
6  *
7  * Copyright (c) 2016 The Khronos Group Inc.
8  * Copyright (c) 2016 Google Inc.
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 Utility for generating simple work
25  *//*--------------------------------------------------------------------*/
26 
27 #include "vkDefs.hpp"
28 
29 #include "deUniquePtr.hpp"
30 #include "vkBufferWithMemory.hpp"
31 #include "vkImageWithMemory.hpp"
32 #include "vkImageUtil.hpp"
33 #include "vkPrograms.hpp"
34 #include "vktTestCase.hpp"
35 #include "vkTypeUtil.hpp"
36 #include "rrRenderer.hpp"
37 #include <memory>
38 
39 namespace vkt
40 {
41 namespace drawutil
42 {
43 
44 struct FrameBufferState
45 {
46     FrameBufferState() = delete;
47     FrameBufferState(uint32_t renderWidth_, uint32_t renderHeight_);
48 
49     vk::VkFormat colorFormat = vk::VK_FORMAT_R8G8B8A8_UNORM;
50     vk::VkFormat depthFormat = vk::VK_FORMAT_UNDEFINED;
51     tcu::UVec2 renderSize;
52     uint32_t numSamples            = vk::VK_SAMPLE_COUNT_1_BIT;
53     vk::VkImageView depthImageView = 0;
54 };
55 
56 struct PipelineState
57 {
58     PipelineState() = delete;
59     PipelineState(const int subpixelBits);
60 
61     bool depthClampEnable          = false;
62     bool depthTestEnable           = false;
63     bool depthWriteEnable          = false;
64     rr::TestFunc compareOp         = rr::TESTFUNC_LESS;
65     bool depthBoundsTestEnable     = false;
66     bool blendEnable               = false;
67     float lineWidth                = 1.0;
68     uint32_t numPatchControlPoints = 0;
69     bool sampleShadingEnable       = false;
70     int subpixelBits;
71     std::vector<uint32_t> sampleMasks;
72 
73     // VK_EXT_depth_clip_enable
74     bool explicitDepthClipEnable = false;
75     bool depthClipEnable         = false;
76 };
77 
78 struct DrawCallData
79 {
80     DrawCallData() = delete;
81     DrawCallData(const vk::VkPrimitiveTopology topology_, const std::vector<tcu::Vec4> &vertices_);
82 
83     vk::VkPrimitiveTopology topology;
84     const std::vector<tcu::Vec4> &vertices;
85 };
86 
87 //! Sets up a graphics pipeline and enables simple draw calls to predefined attachments.
88 //! Clip volume uses wc = 1.0, which gives clip coord ranges: x = [-1, 1], y = [-1, 1], z = [0, 1]
89 //! Clip coords (-1,-1) map to viewport coords (0, 0).
90 class DrawContext
91 {
92 public:
DrawContext(const FrameBufferState & framebufferState)93     DrawContext(const FrameBufferState &framebufferState) : m_framebufferState(framebufferState)
94     {
95     }
~DrawContext(void)96     virtual ~DrawContext(void)
97     {
98     }
99 
100     virtual void draw(void)                                        = 0;
101     virtual tcu::ConstPixelBufferAccess getColorPixels(void) const = 0;
102 
103 protected:
104     const FrameBufferState &m_framebufferState;
105     std::vector<PipelineState> m_pipelineStates;
106     std::vector<DrawCallData> m_drawCallData;
107 };
108 
109 class ReferenceDrawContext : public DrawContext
110 {
111 public:
112     ReferenceDrawContext(const FrameBufferState &framebufferState);
113     virtual ~ReferenceDrawContext(void);
114 
115     void registerDrawObject(const PipelineState &pipelineState, std::shared_ptr<rr::VertexShader> &vertexShader,
116                             std::shared_ptr<rr::FragmentShader> &fragmentShader, const DrawCallData &drawCallData);
117     virtual void draw(void);
118     virtual tcu::ConstPixelBufferAccess getColorPixels(void) const;
119 
120 private:
121     std::vector<std::shared_ptr<rr::VertexShader>> m_vertexShaders;
122     std::vector<std::shared_ptr<rr::FragmentShader>> m_fragmentShaders;
123     tcu::TextureLevel m_refImage;
124 };
125 
126 struct VulkanShader
127 {
128     VulkanShader() = delete;
129     VulkanShader(const vk::VkShaderStageFlagBits stage_, const vk::ProgramBinary &binary_);
130 
131     vk::VkShaderStageFlagBits stage;
132     const vk::ProgramBinary *binary;
133 };
134 
135 struct VulkanProgram
136 {
137     VulkanProgram(const std::vector<VulkanShader> &shaders_);
138 
139     std::vector<VulkanShader> shaders;
140     vk::VkDescriptorSetLayout descriptorSetLayout = 0;
141     vk::VkDescriptorSet descriptorSet             = 0;
142 };
143 
144 struct RenderObject
145 {
146     enum VulkanContants
147     {
148         MAX_NUM_SHADER_MODULES = 5,
149     };
150 
151     vk::refdetails::Move<vk::VkPipelineLayout> pipelineLayout;
152     vk::refdetails::Move<vk::VkPipeline> pipeline;
153     vk::refdetails::Move<vk::VkShaderModule> shaderModules[MAX_NUM_SHADER_MODULES];
154     de::MovePtr<vk::BufferWithMemory> vertexBuffer;
155     uint32_t vertexCount;
156     vk::VkDescriptorSetLayout descriptorSetLayout = 0;
157     vk::VkDescriptorSet descriptorSet             = 0;
158 };
159 
160 class VulkanDrawContext : public DrawContext
161 {
162 public:
163     VulkanDrawContext(Context &context, const FrameBufferState &frameBufferState);
164     virtual ~VulkanDrawContext(void);
165 
166     void registerDrawObject(const PipelineState &pipelineState, const VulkanProgram &vulkanProgram,
167                             const DrawCallData &drawCallData);
168 
169     virtual void draw(void);
170     virtual tcu::ConstPixelBufferAccess getColorPixels(void) const;
171 
172 private:
173     Context &m_context;
174     de::MovePtr<vk::ImageWithMemory> m_colorImage;
175     de::MovePtr<vk::ImageWithMemory> m_resolveImage;
176     de::MovePtr<vk::ImageWithMemory> m_depthImage;
177     de::MovePtr<vk::BufferWithMemory> m_colorAttachmentBuffer;
178     vk::refdetails::Move<vk::VkImageView> m_colorImageView;
179     vk::refdetails::Move<vk::VkImageView> m_depthImageView;
180     vk::refdetails::Move<vk::VkRenderPass> m_renderPass;
181     vk::refdetails::Move<vk::VkFramebuffer> m_framebuffer;
182     vk::refdetails::Move<vk::VkCommandPool> m_cmdPool;
183     vk::refdetails::Move<vk::VkCommandBuffer> m_cmdBuffer;
184 
185     std::vector<std::shared_ptr<RenderObject>> m_renderObjects;
186 };
187 
188 std::string getPrimitiveTopologyShortName(const vk::VkPrimitiveTopology topology);
189 
190 } // namespace drawutil
191 } // namespace vkt
192 
193 #endif // _VKTDRAWUTIL_HPP
194