aboutsummaryrefslogtreecommitdiff
path: root/src/vulkan/engine_vulkan.h
blob: 76668fb8e2484fdd57d9397162c81c5d4ddfdcc6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2018 The Amber Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef SRC_VULKAN_ENGINE_VULKAN_H_
#define SRC_VULKAN_ENGINE_VULKAN_H_

#include <map>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#include "amber/vulkan_header.h"
#include "src/cast_hash.h"
#include "src/engine.h"
#include "src/pipeline.h"
#include "src/vulkan/buffer_descriptor.h"
#include "src/vulkan/command_pool.h"
#include "src/vulkan/device.h"
#include "src/vulkan/pipeline.h"
#include "src/vulkan/vertex_buffer.h"

namespace amber {
namespace vulkan {

/// Engine implementation based on Vulkan.
class EngineVulkan : public Engine {
 public:
  EngineVulkan();
  ~EngineVulkan() override;

  // Engine
  Result Initialize(EngineConfig* config,
                    Delegate* delegate,
                    const std::vector<std::string>& features,
                    const std::vector<std::string>& instance_extensions,
                    const std::vector<std::string>& device_extensions) override;
  Result CreatePipeline(amber::Pipeline* type) override;

  Result DoClearColor(const ClearColorCommand* cmd) override;
  Result DoClearStencil(const ClearStencilCommand* cmd) override;
  Result DoClearDepth(const ClearDepthCommand* cmd) override;
  Result DoClear(const ClearCommand* cmd) override;
  Result DoDrawRect(const DrawRectCommand* cmd) override;
  Result DoDrawGrid(const DrawGridCommand* cmd) override;
  Result DoDrawArrays(const DrawArraysCommand* cmd) override;
  Result DoCompute(const ComputeCommand* cmd) override;
  Result DoEntryPoint(const EntryPointCommand* cmd) override;
  Result DoPatchParameterVertices(
      const PatchParameterVerticesCommand* cmd) override;
  Result DoBuffer(const BufferCommand* cmd) override;

 private:
  struct PipelineInfo {
    std::unique_ptr<Pipeline> vk_pipeline;
    std::unique_ptr<VertexBuffer> vertex_buffer;
    struct ShaderInfo {
      VkShaderModule shader;
      std::unique_ptr<std::vector<VkSpecializationMapEntry>>
          specialization_entries;
      std::unique_ptr<std::vector<uint32_t>> specialization_data;
      std::unique_ptr<VkSpecializationInfo> specialization_info;
      uint32_t required_subgroup_size;
      VkPipelineShaderStageCreateFlags create_flags;
    };
    std::unordered_map<ShaderType, ShaderInfo, CastHash<ShaderType>>
        shader_info;
  };

  Result GetVkShaderStageInfo(
      amber::Pipeline* pipeline,
      std::vector<VkPipelineShaderStageCreateInfo>* out);

  Result SetShader(amber::Pipeline* pipeline,
                   const amber::Pipeline::ShaderInfo& shader);

  std::unique_ptr<Device> device_;
  std::unique_ptr<CommandPool> pool_;

  std::map<amber::Pipeline*, PipelineInfo> pipeline_map_;

  std::map<std::string, VkShaderModule> shaders_;
};

}  // namespace vulkan
}  // namespace amber

#endif  // SRC_VULKAN_ENGINE_VULKAN_H_