-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDrawable.cpp
More file actions
154 lines (141 loc) · 4.05 KB
/
Copy pathDrawable.cpp
File metadata and controls
154 lines (141 loc) · 4.05 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
#include "Drawable.h"
#include "SkinnedMesh.h"
#include "Core/ThreadSyncManager.h"
#include <cassert>
#include <variant>
namespace sh::render
{
Drawable::Drawable(const Material& mat, const Mesh& mesh) :
mat(&mat), mesh(&mesh)
{
topology[core::ThreadType::Game] = Mesh::Topology::Face;
topology[core::ThreadType::Render] = Mesh::Topology::Face;
modelMatrix[core::ThreadType::Game] = glm::mat4{ 1.f };
modelMatrix[core::ThreadType::Render] = glm::mat4{ 1.f };
priority[core::ThreadType::Game] = 0;
priority[core::ThreadType::Render] = 0;
if (mesh.GetType().IsChildOf(SkinnedMesh::GetStaticType()))
bSkinned = true;
}
Drawable::Drawable(Drawable&& other) noexcept :
mat(other.mat), mesh(other.mesh), modelMatrix(other.modelMatrix),
materialData(std::move(other.materialData)),
renderTag(other.renderTag),
priority(other.priority),
topology(other.topology),
subMeshIndex(other.subMeshIndex),
syncDatas(other.syncDatas),
bSkinned(other.bSkinned),
bDirty(other.bDirty),
bMatrixDirty(other.bMatrixDirty)
{
other.bDirty = false;
}
Drawable::~Drawable()
{
}
SH_RENDER_API void Drawable::Build(const IRenderContext& context)
{
this->context = &context;
if (core::IsValid(mat) && core::IsValid(mat->GetShader()))
materialData.Create(context, *mat->GetShader(), true); // sync타이밍에 이뤄짐
}
SH_RENDER_API void Drawable::SetMesh(const Mesh& mesh)
{
if (this->mesh == &mesh)
return;
SyncData data{};
data.changed = &mesh;
syncDatas[1] = data;
SyncDirty();
}
SH_RENDER_API void Drawable::SetMaterial(const Material& mat)
{
if (this->mat == &mat)
return;
SyncData data{};
data.changed = &mat;
syncDatas[0]= data;
SyncDirty();
if (context != nullptr && core::IsValid(mat.GetShader()))
materialData.Create(*context, *mat.GetShader(), true); // 얘도 sync타이밍에 이뤄짐
}
SH_RENDER_API void Drawable::SetModelMatrix(const glm::mat4& mat)
{
modelMatrix[core::ThreadType::Game] = mat;
bMatrixDirty = true;
SyncDirty();
}
SH_RENDER_API void Drawable::SetRenderTagId(uint32_t tagId)
{
renderTag = tagId;
}
SH_RENDER_API void Drawable::SetTopology(Mesh::Topology topology)
{
assert(core::ThreadSyncManager::IsMainThread());
if (this->topology[core::ThreadType::Game] != topology)
{
this->topology[core::ThreadType::Game] = topology;
syncDatas[2].changed = topology;
SyncDirty();
}
}
SH_RENDER_API void Drawable::SetPriority(int priority)
{
assert(core::ThreadSyncManager::IsMainThread());
if (this->priority[core::ThreadType::Game] != priority)
{
this->priority[core::ThreadType::Game] = priority;
syncDatas[3].changed = priority;
SyncDirty();
}
}
SH_RENDER_API void Drawable::SetSubMeshIndex(uint32_t idx)
{
subMeshIndex = idx;
}
SH_RENDER_API auto Drawable::CheckAssetValid() const -> bool
{
return core::IsValid(mesh) && core::IsValid(mat) && core::IsValid(mesh);
}
SH_RENDER_API void Drawable::SyncDirty()
{
if (bDirty)
return;
core::ThreadSyncManager::PushSyncable(*this);
bDirty = true;
}
SH_RENDER_API void Drawable::Sync()
{
for (auto& syncData : syncDatas)
{
if (std::holds_alternative<std::monostate>(syncData.changed))
{
continue;
}
else if (std::holds_alternative<const Material*>(syncData.changed)) // 메테리얼이 변경됨
{
mat = std::get<const Material*>(syncData.changed);
}
else if (std::holds_alternative<const Mesh*>(syncData.changed))
{
mesh = std::get<const Mesh*>(syncData.changed);
if (mesh->GetType().IsChildOf(SkinnedMesh::GetStaticType()))
bSkinned = true;
}
else if (std::holds_alternative<Mesh::Topology>(syncData.changed))
{
topology[core::ThreadType::Render] = std::get<Mesh::Topology>(syncData.changed);
}
else if (std::holds_alternative<int>(syncData.changed))
{
priority[core::ThreadType::Render] = std::get<int>(syncData.changed);
}
syncData.changed = std::monostate{};
}
if (bMatrixDirty)
std::swap(modelMatrix[core::ThreadType::Game], modelMatrix[core::ThreadType::Render]);
bMatrixDirty = false;
bDirty = false;
}
}//namespace