forked from Shell4026/ShellEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVulkanPipelineManager.cpp
More file actions
144 lines (126 loc) · 4.34 KB
/
Copy pathVulkanPipelineManager.cpp
File metadata and controls
144 lines (126 loc) · 4.34 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "pch.h"
#include "VulkanPipelineManager.h"
#include "Mesh.h"
#include "VulkanShader.h"
#include "VulkanVertexBuffer.h"
namespace sh::render::vk
{
VulkanPipelineManager::VulkanPipelineManager(VkDevice device) :
device(device)
{
shaderListener.SetCallback([&](core::SObject* obj)
{
auto shaderIt = shaderIdxs.find(static_cast<const VulkanShader*>(obj));
if (shaderIt != shaderIdxs.end())
{
for (auto idx : shaderIt->second)
{
pipelines[idx][core::ThreadType::Game].reset();
auto& info = pipelinesInfo[idx];
if (auto infoIt = infoIdx.find(info); infoIt != infoIdx.end())
infoIdx.erase(infoIt);
if (auto meshIt = meshIdxs.find(info.mesh); meshIt != meshIdxs.end())
meshIdxs.erase(meshIt);
}
shaderIdxs.erase(shaderIt);
}
}
);
meshListener.SetCallback([&](core::SObject* obj)
{
auto meshIt = meshIdxs.find(static_cast<const Mesh*>(obj));
if (meshIt != meshIdxs.end())
{
for (auto idx : meshIt->second)
{
pipelines[idx][core::ThreadType::Game].reset();
auto& info = pipelinesInfo[idx];
if (auto infoIt = infoIdx.find(info); infoIt != infoIdx.end())
infoIdx.erase(infoIt);
if (auto shaderIt = shaderIdxs.find(info.shader); shaderIt != shaderIdxs.end())
shaderIdxs.erase(shaderIt);
}
meshIdxs.erase(meshIt);
}
}
);
}
auto VulkanPipelineManager::BuildPipeline(const VkRenderPass& pass, VulkanShader& shader, Mesh& mesh) -> std::unique_ptr<VulkanPipeline>
{
shader.onDestroy.Register(shaderListener);
mesh.onDestroy.Register(meshListener);
auto pipeline = std::make_unique<VulkanPipeline>(device, pass);
VulkanPipeline::Topology topology = VulkanPipeline::Topology::Triangle;
switch (mesh.GetTopology())
{
case Mesh::Topology::Point:
topology = VulkanPipeline::Topology::Point;
break;
case Mesh::Topology::Line:
topology = VulkanPipeline::Topology::Line;
break;
case Mesh::Topology::Face:
topology = VulkanPipeline::Topology::Triangle;
break;
}
pipeline->SetTopology(topology);
pipeline->SetShader(&shader);
pipeline->
AddShaderStage(VulkanPipeline::ShaderStage::Vertex).
AddShaderStage(VulkanPipeline::ShaderStage::Fragment);
//Attribute
auto& bindings = static_cast<VulkanVertexBuffer*>(mesh.GetVertexBuffer())->bindingDescriptions;
auto& attrs = static_cast<VulkanVertexBuffer*>(mesh.GetVertexBuffer())->attribDescriptions;
// binding 0과 attribute 0은 버텍스
for (int i = 0; i < attrs.size(); ++i)
{
auto data = shader.GetAttribute(mesh.attributes[i]->name);
if (!data)
continue;
if (data->typeName != mesh.attributes[i]->typeName)
continue;
VkVertexInputAttributeDescription attrDesc = attrs[i];
attrDesc.location = data->idx;
pipeline->AddBindingDescription(bindings[i]);
pipeline->AddAttributeDescription(attrDesc);
}
pipeline->SetLineWidth(mesh.lineWidth);
auto result = pipeline->Build(shader.GetPipelineLayout());
assert(result == VkResult::VK_SUCCESS);
return pipeline;
}
SH_RENDER_API auto VulkanPipelineManager::GetPipeline(core::ThreadType thr, const VkRenderPass& pass, VulkanShader& shader, Mesh& mesh) -> VulkanPipeline*
{
PipelineInfo info{ &pass, &shader, &mesh };
auto it = infoIdx.find(info);
if (it == infoIdx.end())
{
core::SyncArray<std::unique_ptr<VulkanPipeline>> syncArray{ nullptr, nullptr };
syncArray[thr] = BuildPipeline(pass, shader, mesh);
pipelines.push_back(std::move(syncArray));
pipelinesInfo.push_back(info);
std::size_t idx = pipelines.size() - 1;
infoIdx.insert({ info, idx });
if (auto it = renderpassIdxs.find(&pass); it == renderpassIdxs.end())
renderpassIdxs.insert({ &pass, std::vector<std::size_t>{idx} });
else
it->second.push_back(idx);
if (auto it = shaderIdxs.find(&shader); it == shaderIdxs.end())
shaderIdxs.insert({ &shader, std::vector<std::size_t>{idx} });
else
it->second.push_back(idx);
if (auto it = meshIdxs.find(&mesh); it == meshIdxs.end())
meshIdxs.insert({ &mesh, std::vector<std::size_t>{idx} });
else
it->second.push_back(idx);
return pipelines[idx][thr].get();
}
else
{
VulkanPipeline* pipeline = pipelines[it->second][thr].get();
if (pipeline == nullptr)
pipelines[it->second][thr] = BuildPipeline(pass, shader, mesh);
return pipelines[it->second][thr].get();
}
}
}//namespace