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