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_ENGINE_VULKAN_H_
16 #define SRC_VULKAN_ENGINE_VULKAN_H_
17 
18 #include <map>
19 #include <memory>
20 #include <string>
21 #include <unordered_map>
22 #include <utility>
23 #include <vector>
24 
25 #include "amber/vulkan_header.h"
26 #include "src/cast_hash.h"
27 #include "src/engine.h"
28 #include "src/pipeline.h"
29 #include "src/vulkan/buffer_descriptor.h"
30 #include "src/vulkan/command_pool.h"
31 #include "src/vulkan/device.h"
32 #include "src/vulkan/pipeline.h"
33 #include "src/vulkan/vertex_buffer.h"
34 
35 namespace amber {
36 namespace vulkan {
37 
38 /// Engine implementation based on Vulkan.
39 class EngineVulkan : public Engine {
40  public:
41   EngineVulkan();
42   ~EngineVulkan() override;
43 
44   // Engine
45   Result Initialize(EngineConfig* config,
46                     Delegate* delegate,
47                     const std::vector<std::string>& features,
48                     const std::vector<std::string>& instance_extensions,
49                     const std::vector<std::string>& device_extensions) override;
50   Result CreatePipeline(amber::Pipeline* type) override;
51 
52   Result DoClearColor(const ClearColorCommand* cmd) override;
53   Result DoClearStencil(const ClearStencilCommand* cmd) override;
54   Result DoClearDepth(const ClearDepthCommand* cmd) override;
55   Result DoClear(const ClearCommand* cmd) override;
56   Result DoDrawRect(const DrawRectCommand* cmd) override;
57   Result DoDrawGrid(const DrawGridCommand* cmd) override;
58   Result DoDrawArrays(const DrawArraysCommand* cmd) override;
59   Result DoCompute(const ComputeCommand* cmd) override;
60   Result DoEntryPoint(const EntryPointCommand* cmd) override;
61   Result DoPatchParameterVertices(
62       const PatchParameterVerticesCommand* cmd) override;
63   Result DoBuffer(const BufferCommand* cmd) override;
64 
65  private:
66   struct PipelineInfo {
67     std::unique_ptr<Pipeline> vk_pipeline;
68     std::unique_ptr<VertexBuffer> vertex_buffer;
69     struct ShaderInfo {
70       VkShaderModule shader;
71       std::unique_ptr<std::vector<VkSpecializationMapEntry>>
72           specialization_entries;
73       std::unique_ptr<std::vector<uint32_t>> specialization_data;
74       std::unique_ptr<VkSpecializationInfo> specialization_info;
75       uint32_t required_subgroup_size;
76       VkPipelineShaderStageCreateFlags create_flags;
77     };
78     std::unordered_map<ShaderType, ShaderInfo, CastHash<ShaderType>>
79         shader_info;
80   };
81 
82   Result GetVkShaderStageInfo(
83       amber::Pipeline* pipeline,
84       std::vector<VkPipelineShaderStageCreateInfo>* out);
85 
86   Result SetShader(amber::Pipeline* pipeline,
87                    const amber::Pipeline::ShaderInfo& shader);
88 
89   std::unique_ptr<Device> device_;
90   std::unique_ptr<CommandPool> pool_;
91 
92   std::map<amber::Pipeline*, PipelineInfo> pipeline_map_;
93 
94   std::map<std::string, VkShaderModule> shaders_;
95 };
96 
97 }  // namespace vulkan
98 }  // namespace amber
99 
100 #endif  // SRC_VULKAN_ENGINE_VULKAN_H_
101