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(deUint32 renderWidth_, deUint32 renderHeight_); 48 49 vk::VkFormat colorFormat = vk::VK_FORMAT_R8G8B8A8_UNORM; 50 vk::VkFormat depthFormat = vk::VK_FORMAT_UNDEFINED; 51 tcu::UVec2 renderSize; 52 deUint32 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 deUint32 numPatchControlPoints = 0; 69 bool sampleShadingEnable = false; 70 int subpixelBits; 71 72 // VK_EXT_depth_clip_enable 73 bool explicitDepthClipEnable = false; 74 bool depthClipEnable = false; 75 }; 76 77 struct DrawCallData 78 { 79 DrawCallData() = delete; 80 DrawCallData(const vk::VkPrimitiveTopology topology_, const std::vector<tcu::Vec4>& vertices_); 81 82 vk::VkPrimitiveTopology topology; 83 const std::vector<tcu::Vec4>& vertices; 84 }; 85 86 //! Sets up a graphics pipeline and enables simple draw calls to predefined attachments. 87 //! Clip volume uses wc = 1.0, which gives clip coord ranges: x = [-1, 1], y = [-1, 1], z = [0, 1] 88 //! Clip coords (-1,-1) map to viewport coords (0, 0). 89 class DrawContext 90 { 91 public: DrawContext(const FrameBufferState & framebufferState)92 DrawContext (const FrameBufferState& framebufferState) 93 : 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 protected: 103 const FrameBufferState& m_framebufferState; 104 std::vector<PipelineState> m_pipelineStates; 105 std::vector<DrawCallData> m_drawCallData; 106 }; 107 108 class ReferenceDrawContext : public DrawContext 109 { 110 public: 111 ReferenceDrawContext (const FrameBufferState& framebufferState ); 112 virtual ~ReferenceDrawContext (void); 113 114 void registerDrawObject (const PipelineState& pipelineState, 115 std::shared_ptr<rr::VertexShader>& vertexShader, 116 std::shared_ptr<rr::FragmentShader>& fragmentShader, 117 const DrawCallData& drawCallData); 118 virtual void draw (void); 119 virtual tcu::ConstPixelBufferAccess getColorPixels (void) const; 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 deUint32 vertexCount; 156 vk::VkDescriptorSetLayout descriptorSetLayout = 0; 157 vk::VkDescriptorSet descriptorSet = 0; 158 159 }; 160 161 class VulkanDrawContext : public DrawContext 162 { 163 public: 164 VulkanDrawContext (Context& context, 165 const FrameBufferState& frameBufferState); 166 virtual ~VulkanDrawContext (void); 167 168 void registerDrawObject (const PipelineState& pipelineState, 169 const VulkanProgram& vulkanProgram, 170 const DrawCallData& drawCallData); 171 172 virtual void draw (void); 173 virtual tcu::ConstPixelBufferAccess getColorPixels (void) const; 174 private: 175 Context& m_context; 176 de::MovePtr<vk::ImageWithMemory> m_colorImage; 177 de::MovePtr<vk::ImageWithMemory> m_resolveImage; 178 de::MovePtr<vk::ImageWithMemory> m_depthImage; 179 de::MovePtr<vk::BufferWithMemory> m_colorAttachmentBuffer; 180 vk::refdetails::Move<vk::VkImageView> m_colorImageView; 181 vk::refdetails::Move<vk::VkImageView> m_depthImageView; 182 vk::refdetails::Move<vk::VkRenderPass> m_renderPass; 183 vk::refdetails::Move<vk::VkFramebuffer> m_framebuffer; 184 vk::refdetails::Move<vk::VkCommandPool> m_cmdPool; 185 vk::refdetails::Move<vk::VkCommandBuffer> m_cmdBuffer; 186 187 std::vector<std::shared_ptr<RenderObject>> m_renderObjects; 188 }; 189 190 std::string getPrimitiveTopologyShortName (const vk::VkPrimitiveTopology topology); 191 192 } // drawutil 193 } // vkt 194 195 #endif // _VKTDRAWUTIL_HPP 196