1 // Copyright 2018 The Amber Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef SRC_VULKAN_GRAPHICS_PIPELINE_H_ 16 #define SRC_VULKAN_GRAPHICS_PIPELINE_H_ 17 18 #include <memory> 19 #include <vector> 20 21 #include "amber/result.h" 22 #include "amber/value.h" 23 #include "amber/vulkan_header.h" 24 #include "src/format.h" 25 #include "src/pipeline.h" 26 #include "src/vulkan/frame_buffer.h" 27 #include "src/vulkan/index_buffer.h" 28 #include "src/vulkan/pipeline.h" 29 #include "src/vulkan/vertex_buffer.h" 30 31 namespace amber { 32 33 class ProbeCommand; 34 35 namespace vulkan { 36 37 class CommandPool; 38 39 /// Wrapper around a graphics pipeline. 40 class GraphicsPipeline : public Pipeline { 41 public: 42 GraphicsPipeline( 43 Device* device, 44 const std::vector<amber::Pipeline::BufferInfo>& color_buffers, 45 amber::Pipeline::BufferInfo depth_stencil_buffer, 46 uint32_t fence_timeout_ms, 47 const std::vector<VkPipelineShaderStageCreateInfo>&); 48 ~GraphicsPipeline() override; 49 50 Result Initialize(uint32_t width, uint32_t height, CommandPool* pool); 51 52 Result SetIndexBuffer(Buffer* buffer); 53 54 Result Clear(); 55 56 Result SetClearColor(float r, float g, float b, float a); 57 Result SetClearStencil(uint32_t stencil); 58 Result SetClearDepth(float depth); 59 60 Result Draw(const DrawArraysCommand* command, VertexBuffer* vertex_buffer); 61 GetVkRenderPass()62 VkRenderPass GetVkRenderPass() const { return render_pass_; } GetFrameBuffer()63 FrameBuffer* GetFrameBuffer() const { return frame_.get(); } 64 GetWidth()65 uint32_t GetWidth() const { return frame_width_; } GetHeight()66 uint32_t GetHeight() const { return frame_height_; } 67 SetPatchControlPoints(uint32_t points)68 void SetPatchControlPoints(uint32_t points) { 69 patch_control_points_ = points; 70 } 71 72 private: 73 Result CreateVkGraphicsPipeline(const PipelineData* pipeline_data, 74 VkPrimitiveTopology topology, 75 const VertexBuffer* vertex_buffer, 76 const VkPipelineLayout& pipeline_layout, 77 VkPipeline* pipeline); 78 Result CreateRenderPass(); 79 Result SendVertexBufferDataIfNeeded(VertexBuffer* vertex_buffer); 80 81 VkPipelineDepthStencilStateCreateInfo GetVkPipelineDepthStencilInfo( 82 const PipelineData* pipeline_data); 83 std::vector<VkPipelineColorBlendAttachmentState> 84 GetVkPipelineColorBlendAttachmentState(const PipelineData* pipeline_data); 85 86 VkRenderPass render_pass_ = VK_NULL_HANDLE; 87 std::unique_ptr<FrameBuffer> frame_; 88 89 // color buffers are owned by the amber::Pipeline. 90 std::vector<const amber::Pipeline::BufferInfo*> color_buffers_; 91 amber::Pipeline::BufferInfo depth_stencil_buffer_; 92 std::unique_ptr<IndexBuffer> index_buffer_; 93 94 uint32_t frame_width_ = 0; 95 uint32_t frame_height_ = 0; 96 97 float clear_color_r_ = 0; 98 float clear_color_g_ = 0; 99 float clear_color_b_ = 0; 100 float clear_color_a_ = 0; 101 uint32_t clear_stencil_ = 0; 102 float clear_depth_ = 1.0f; 103 uint32_t patch_control_points_ = 3; 104 }; 105 106 } // namespace vulkan 107 } // namespace amber 108 109 #endif // SRC_VULKAN_GRAPHICS_PIPELINE_H_ 110