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       const std::vector<amber::Pipeline::BufferInfo>& resolve_targets,
47       uint32_t fence_timeout_ms,
48       bool pipeline_runtime_layer_enabled,
49       const std::vector<VkPipelineShaderStageCreateInfo>&);
50   ~GraphicsPipeline() override;
51 
52   Result Initialize(uint32_t width, uint32_t height, CommandPool* pool);
53 
54   Result SetIndexBuffer(Buffer* buffer);
55 
56   Result Clear();
57 
58   Result SetClearColor(float r, float g, float b, float a);
59   Result SetClearStencil(uint32_t stencil);
60   Result SetClearDepth(float depth);
61 
62   Result Draw(const DrawArraysCommand* command, VertexBuffer* vertex_buffer);
63 
GetVkRenderPass()64   VkRenderPass GetVkRenderPass() const { return render_pass_; }
GetFrameBuffer()65   FrameBuffer* GetFrameBuffer() const { return frame_.get(); }
66 
GetWidth()67   uint32_t GetWidth() const { return frame_width_; }
GetHeight()68   uint32_t GetHeight() const { return frame_height_; }
69 
SetPatchControlPoints(uint32_t points)70   void SetPatchControlPoints(uint32_t points) {
71     patch_control_points_ = points;
72   }
73 
74  private:
75   Result CreateVkGraphicsPipeline(const PipelineData* pipeline_data,
76                                   VkPrimitiveTopology topology,
77                                   const VertexBuffer* vertex_buffer,
78                                   const VkPipelineLayout& pipeline_layout,
79                                   VkPipeline* pipeline);
80   Result CreateRenderPass();
81   Result SendVertexBufferDataIfNeeded(VertexBuffer* vertex_buffer);
82 
83   VkPipelineDepthStencilStateCreateInfo GetVkPipelineDepthStencilInfo(
84       const PipelineData* pipeline_data);
85   std::vector<VkPipelineColorBlendAttachmentState>
86   GetVkPipelineColorBlendAttachmentState(const PipelineData* pipeline_data);
87 
88   VkRenderPass render_pass_ = VK_NULL_HANDLE;
89   std::unique_ptr<FrameBuffer> frame_;
90 
91   // color buffers and resolve targets are owned by the amber::Pipeline.
92   std::vector<const amber::Pipeline::BufferInfo*> color_buffers_;
93   std::vector<const amber::Pipeline::BufferInfo*> resolve_targets_;
94   amber::Pipeline::BufferInfo depth_stencil_buffer_;
95   std::unique_ptr<IndexBuffer> index_buffer_;
96 
97   uint32_t frame_width_ = 0;
98   uint32_t frame_height_ = 0;
99 
100   float clear_color_r_ = 0;
101   float clear_color_g_ = 0;
102   float clear_color_b_ = 0;
103   float clear_color_a_ = 0;
104   uint32_t clear_stencil_ = 0;
105   float clear_depth_ = 1.0f;
106   uint32_t patch_control_points_ = 3;
107 };
108 
109 }  // namespace vulkan
110 }  // namespace amber
111 
112 #endif  // SRC_VULKAN_GRAPHICS_PIPELINE_H_
113