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   std::pair<Debugger*, Result> GetDebugger(VirtualFileStore*) override;
66 
67  private:
68   struct PipelineInfo {
69     std::unique_ptr<Pipeline> vk_pipeline;
70     std::unique_ptr<VertexBuffer> vertex_buffer;
71     struct ShaderInfo {
72       VkShaderModule shader;
73       std::unique_ptr<std::vector<VkSpecializationMapEntry>>
74           specialization_entries;
75       std::unique_ptr<std::vector<uint32_t>> specialization_data;
76       std::unique_ptr<VkSpecializationInfo> specialization_info;
77       uint32_t required_subgroup_size;
78       VkPipelineShaderStageCreateFlags create_flags;
79     };
80     std::unordered_map<ShaderType, ShaderInfo, CastHash<ShaderType>>
81         shader_info;
82   };
83 
84   Result GetVkShaderStageInfo(
85       amber::Pipeline* pipeline,
86       std::vector<VkPipelineShaderStageCreateInfo>* out);
87 
88   Result SetShader(amber::Pipeline* pipeline,
89                    const amber::Pipeline::ShaderInfo& shader);
90 
91   std::unique_ptr<Device> device_;
92   std::unique_ptr<CommandPool> pool_;
93 
94   std::map<amber::Pipeline*, PipelineInfo> pipeline_map_;
95 
96   std::unique_ptr<Debugger> debugger_;
97   std::map<std::string, VkShaderModule> shaders_;
98 };
99 
100 }  // namespace vulkan
101 }  // namespace amber
102 
103 #endif  // SRC_VULKAN_ENGINE_VULKAN_H_
104