aboutsummaryrefslogtreecommitdiff
path: root/src/vulkan/pipeline.h
blob: 0b806bf511d263dfa2ac66aaa2f5bab776a5453f (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
101
102
// 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_PIPELINE_H_
#define SRC_VULKAN_PIPELINE_H_

#include <memory>
#include <vector>

#include "amber/result.h"
#include "src/engine.h"
#include "src/vulkan/command.h"
#include "src/vulkan/descriptor.h"
#include "vulkan/vulkan.h"

namespace amber {

class BufferCommand;

namespace vulkan {

class ComputePipeline;
class GraphicsPipeline;

class Pipeline {
 public:
  virtual ~Pipeline();

  bool IsGraphics() const { return pipeline_type_ == PipelineType::kGraphics; }
  bool IsCompute() const { return pipeline_type_ == PipelineType::kCompute; }

  GraphicsPipeline* AsGraphics();
  ComputePipeline* AsCompute();

  Result AddDescriptor(const BufferCommand*);

  virtual void Shutdown();

 protected:
  Pipeline(
      PipelineType type,
      VkDevice device,
      const VkPhysicalDeviceMemoryProperties& properties,
      uint32_t fence_timeout_ms,
      const std::vector<VkPipelineShaderStageCreateInfo>& shader_stage_info);
  Result InitializeCommandBuffer(VkCommandPool pool, VkQueue queue);
  Result CreateVkDescriptorRelatedObjects();

  Result SendDescriptorDataToGPUIfNeeded();
  void BindVkPipeline();
  void BindVkDescriptorSets();

  const std::vector<VkPipelineShaderStageCreateInfo>& GetShaderStageInfo()
      const {
    return shader_stage_info_;
  }

  uint32_t GetFenceTimeout() const { return fence_timeout_ms_; }

  VkPipelineCache pipeline_cache_ = VK_NULL_HANDLE;
  VkPipeline pipeline_ = VK_NULL_HANDLE;
  VkPipelineLayout pipeline_layout_ = VK_NULL_HANDLE;

  std::vector<VkDescriptorSetLayout> descriptor_set_layouts_;
  std::vector<VkDescriptorPool> descriptor_pools_;
  std::vector<VkDescriptorSet> descriptor_sets_;

  VkDevice device_ = VK_NULL_HANDLE;
  VkPhysicalDeviceMemoryProperties memory_properties_;
  std::unique_ptr<CommandBuffer> command_;

 private:
  Result CreatePipelineLayout();

  Result CreateDescriptorSetLayouts();
  Result CreateDescriptorPools();
  Result CreateDescriptorSets();

  void DestoryDescriptorSetLayouts();
  void DestoryDescriptorPools();

  PipelineType pipeline_type_;
  std::vector<std::unique_ptr<Descriptor>> descriptors_;
  std::vector<VkPipelineShaderStageCreateInfo> shader_stage_info_;
  uint32_t fence_timeout_ms_ = 100;
};

}  // namespace vulkan
}  // namespace amber

#endif  // SRC_VULKAN_PIPELINE_H_