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 std::vector<deUint32> 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) 94 : m_framebufferState (framebufferState) 95 { 96 } ~DrawContext(void)97 virtual ~DrawContext (void) 98 { 99 } 100 101 virtual void draw (void) = 0; 102 virtual tcu::ConstPixelBufferAccess getColorPixels (void) const = 0; 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, 116 std::shared_ptr<rr::VertexShader>& vertexShader, 117 std::shared_ptr<rr::FragmentShader>& fragmentShader, 118 const DrawCallData& drawCallData); 119 virtual void draw (void); 120 virtual tcu::ConstPixelBufferAccess getColorPixels (void) const; 121 private: 122 std::vector<std::shared_ptr<rr::VertexShader>> m_vertexShaders; 123 std::vector< std::shared_ptr<rr::FragmentShader>> m_fragmentShaders; 124 tcu::TextureLevel m_refImage; 125 }; 126 127 struct VulkanShader 128 { 129 VulkanShader () = delete; 130 VulkanShader (const vk::VkShaderStageFlagBits stage_, const vk::ProgramBinary& binary_); 131 132 vk::VkShaderStageFlagBits stage; 133 const vk::ProgramBinary* binary; 134 }; 135 136 struct VulkanProgram 137 { 138 VulkanProgram (const std::vector<VulkanShader>& shaders_); 139 140 std::vector<VulkanShader> shaders; 141 vk::VkDescriptorSetLayout descriptorSetLayout = 0; 142 vk::VkDescriptorSet descriptorSet = 0; 143 }; 144 145 struct RenderObject 146 { 147 enum VulkanContants 148 { 149 MAX_NUM_SHADER_MODULES = 5, 150 }; 151 152 vk::refdetails::Move<vk::VkPipelineLayout> pipelineLayout; 153 vk::refdetails::Move<vk::VkPipeline> pipeline; 154 vk::refdetails::Move<vk::VkShaderModule> shaderModules[MAX_NUM_SHADER_MODULES]; 155 de::MovePtr<vk::BufferWithMemory> vertexBuffer; 156 deUint32 vertexCount; 157 vk::VkDescriptorSetLayout descriptorSetLayout = 0; 158 vk::VkDescriptorSet descriptorSet = 0; 159 160 }; 161 162 class VulkanDrawContext : public DrawContext 163 { 164 public: 165 VulkanDrawContext (Context& context, 166 const FrameBufferState& frameBufferState); 167 virtual ~VulkanDrawContext (void); 168 169 void registerDrawObject (const PipelineState& pipelineState, 170 const VulkanProgram& vulkanProgram, 171 const DrawCallData& drawCallData); 172 173 virtual void draw (void); 174 virtual tcu::ConstPixelBufferAccess getColorPixels (void) const; 175 private: 176 Context& m_context; 177 de::MovePtr<vk::ImageWithMemory> m_colorImage; 178 de::MovePtr<vk::ImageWithMemory> m_resolveImage; 179 de::MovePtr<vk::ImageWithMemory> m_depthImage; 180 de::MovePtr<vk::BufferWithMemory> m_colorAttachmentBuffer; 181 vk::refdetails::Move<vk::VkImageView> m_colorImageView; 182 vk::refdetails::Move<vk::VkImageView> m_depthImageView; 183 vk::refdetails::Move<vk::VkRenderPass> m_renderPass; 184 vk::refdetails::Move<vk::VkFramebuffer> m_framebuffer; 185 vk::refdetails::Move<vk::VkCommandPool> m_cmdPool; 186 vk::refdetails::Move<vk::VkCommandBuffer> m_cmdBuffer; 187 188 std::vector<std::shared_ptr<RenderObject>> m_renderObjects; 189 }; 190 191 std::string getPrimitiveTopologyShortName (const vk::VkPrimitiveTopology topology); 192 193 } // drawutil 194 } // vkt 195 196 #endif // _VKTDRAWUTIL_HPP 197