forked from Shell4026/ShellEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVulkanDrawable.cpp
More file actions
229 lines (195 loc) · 6.77 KB
/
Copy pathVulkanDrawable.cpp
File metadata and controls
229 lines (195 loc) · 6.77 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "pch.h"
#include "VulkanDrawable.h"
#include "Material.h"
#include "Mesh.h"
#include "BufferFactory.h"
#include "VulkanRenderer.h"
#include "VulkanShader.h"
#include "VulkanTextureBuffer.h"
#include "VulkanFramebuffer.h"
#include "VulkanDescriptorPool.h"
#include "VulkanVertexBuffer.h"
#include "VulkanBuffer.h"
#include "VulkanPipelineManager.h"
#include "Core/Reflection.hpp"
#include <cstring>
#include <utility>
namespace sh::render::vk
{
SH_RENDER_API VulkanDrawable::VulkanDrawable(VulkanRenderer& renderer) :
renderer(renderer),
mat(nullptr), mesh(nullptr), camera(nullptr),
localDescSet(),
bInit(false), bDirty(false), bBufferDirty(false), bPipelineDirty(false)
{
}
SH_RENDER_API VulkanDrawable::VulkanDrawable(VulkanDrawable&& other) noexcept :
renderer(other.renderer),
mat(other.mat), mesh(other.mesh), camera(other.camera),
pipeline(std::move(other.pipeline)),
localVertBuffer(std::move(other.localVertBuffer)),
localFragBuffer(std::move(other.localFragBuffer)),
localDescSet(std::move(other.localDescSet)),
bInit(other.bInit), bDirty(other.bDirty), bBufferDirty(other.bBufferDirty), bPipelineDirty(other.bPipelineDirty)
{
other.mat = nullptr;
other.mesh = nullptr;
other.camera = nullptr;
other.bInit = false;
other.bDirty = false;
other.bBufferDirty = false;
other.bPipelineDirty = false;
}
SH_RENDER_API VulkanDrawable::~VulkanDrawable() noexcept
{
Clean(core::ThreadType::Game);
Clean(core::ThreadType::Render);
SH_INFO("~Drawable");
}
void VulkanDrawable::Clean(core::ThreadType thr)
{
localVertBuffer[thr].clear();
localFragBuffer[thr].clear();
localDescSet[thr].reset();
pipeline[thr] = nullptr;
}
void VulkanDrawable::CreateBuffer(core::ThreadType thr)
{
VulkanShader* shader = static_cast<VulkanShader*>(mat->GetShader());
// 버텍스 유니폼
for (auto& uniformBlock : shader->GetVertexUniforms())
{
if (uniformBlock.type == Shader::UniformType::Material)
continue;
std::size_t lastOffset = uniformBlock.data.back().offset + uniformBlock.data.back().size;
std::size_t size = core::Util::AlignTo(lastOffset, uniformBlock.align);
auto ptr = static_cast<VulkanBuffer*>(BufferFactory::Create(renderer, size).release());
localVertBuffer[thr].insert_or_assign(uniformBlock.binding, std::unique_ptr<VulkanBuffer>(ptr));
}
// 픽셀 유니폼
for (auto& uniformBlock : shader->GetFragmentUniforms())
{
if (uniformBlock.type == Shader::UniformType::Material)
continue;
std::size_t lastOffset = uniformBlock.data.back().offset + uniformBlock.data.back().size;
std::size_t size = core::Util::AlignTo(lastOffset, uniformBlock.align);
auto ptr = static_cast<VulkanBuffer*>(BufferFactory::Create(renderer, size).release());
localFragBuffer[thr].insert_or_assign(uniformBlock.binding, std::unique_ptr<VulkanBuffer>(ptr));
}
auto ptr = static_cast<VulkanUniformBuffer*>(BufferFactory::CreateUniformBuffer(renderer, *this->mat->GetShader(), Shader::UniformType::Object).release());
localDescSet[thr] = std::unique_ptr<VulkanUniformBuffer>(ptr);
}
void VulkanDrawable::GetPipelineFromManager(core::ThreadType thr)
{
const VulkanFramebuffer* vkFrameBuffer = nullptr;
if (camera->GetRenderTexture() == nullptr)
vkFrameBuffer = static_cast<const VulkanFramebuffer*>(renderer.GetMainFramebuffer());
else
vkFrameBuffer = static_cast<const VulkanFramebuffer*>(camera->GetRenderTexture()->GetFramebuffer(core::ThreadType::Game));
VulkanShader* shader = static_cast<VulkanShader*>(mat->GetShader());
assert(shader != nullptr);
pipeline[thr] = renderer.GetPipelineManager().GetPipeline(thr, vkFrameBuffer->GetRenderPass(), *shader, *mesh);
}
SH_RENDER_API void VulkanDrawable::Build(Camera& camera, Mesh* mesh, Material* mat)
{
this->mat = mat;
this->mesh = mesh;
this->camera = &camera;
assert(mesh);
assert(mat);
VulkanShader* shader = static_cast<VulkanShader*>(mat->GetShader());
assert(shader);
Clean(core::ThreadType::Game);
int thrCount = bInit ? 1 : 2; // 첫 초기화 시에는 두 번 반복, 아니면 Game스레드만
for (int thrIdx = 0; thrIdx < thrCount; ++thrIdx)
{
core::ThreadType thr = static_cast<core::ThreadType>(thrIdx);
CreateBuffer(thr);
GetPipelineFromManager(thr);
}
if (bInit)
{
bBufferDirty = true;
bPipelineDirty = true;
SetDirty();
}
else
bInit = true;
}
SH_RENDER_API void VulkanDrawable::SetUniformData(uint32_t binding, const void* data, Stage stage)
{
if (stage == Stage::Vertex)
{
auto it = localVertBuffer[core::ThreadType::Game].find(binding);
if (it == localVertBuffer[core::ThreadType::Game].end())
return;
it->second->SetData(data);
localDescSet[core::ThreadType::Game]->Update(binding, *it->second);
bBufferDirty = true;
}
else if (stage == Stage::Fragment)
{
auto it = localFragBuffer[core::ThreadType::Game].find(binding);
if (it == localFragBuffer[core::ThreadType::Game].end())
return;
it->second->SetData(data);
localDescSet[core::ThreadType::Game]->Update(binding, *it->second);
bBufferDirty = true;
}
SetDirty();
}
SH_RENDER_API auto VulkanDrawable::GetMaterial() const -> const Material*
{
return mat;
}
SH_RENDER_API auto VulkanDrawable::GetMesh() const -> const Mesh*
{
return mesh;
}
SH_RENDER_API auto VulkanDrawable::GetCamera() const -> Camera*
{
return camera;
}
SH_RENDER_API auto VulkanDrawable::GetPipeline(core::ThreadType thr) const -> VulkanPipeline*
{
return pipeline[static_cast<int>(thr)];
}
SH_RENDER_API auto VulkanDrawable::GetLocalUniformBuffer(core::ThreadType thr) const -> VulkanUniformBuffer*
{
return localDescSet[thr].get();
}
SH_RENDER_API auto VulkanDrawable::GetDescriptorSet(core::ThreadType thr) const -> VkDescriptorSet
{
return localDescSet[thr]->GetVkDescriptorSet();
}
SH_RENDER_API void VulkanDrawable::SetDirty()
{
if (bDirty)
return;
renderer.GetThreadSyncManager().PushSyncable(*this);
bDirty = true;
}
SH_RENDER_API void VulkanDrawable::Sync()
{
if (bBufferDirty)
{
std::swap(localVertBuffer[core::ThreadType::Game], localVertBuffer[core::ThreadType::Render]);
std::swap(localFragBuffer[core::ThreadType::Game], localFragBuffer[core::ThreadType::Render]);
std::swap(localDescSet[core::ThreadType::Game], localDescSet[core::ThreadType::Render]);
}
if (bPipelineDirty)
{
// 새로 빌드 했다는 뜻이므로 버퍼 교환 후 파이프라인을 새로 빌드 해준다.
std::swap(pipeline[core::ThreadType::Game], pipeline[core::ThreadType::Render]);
GetPipelineFromManager(core::ThreadType::Game);
CreateBuffer(core::ThreadType::Game);
}
bPipelineDirty = false;
bBufferDirty = false;
bDirty = false;
}
SH_RENDER_API bool VulkanDrawable::CheckAssetValid() const
{
return core::IsValid(mesh) && core::IsValid(mat) && core::IsValid(mat->GetShader());
}
}