Repository navigation
Expand file tree
/
Copy pathMaterial.cpp
More file actions
282 lines (259 loc) · 9.99 KB
/
Copy pathMaterial.cpp
File metadata and controls
282 lines (259 loc) · 9.99 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include "Material.h"
#include "ZShader.h"
#include "Texture.h"
#include "RenderAPI.h"
#include "GlobalData.h"
#include "MaterialData.h"
#include "RenderEngineProperties.h"
#include "Time.h"
namespace ZXEngine
{
Material::Material(MaterialStruct* matStruct)
{
name = matStruct->name;
path = matStruct->path;
isShareShader = false;
type = matStruct->type;
data = new MaterialData(type);
CopyMaterialStructToMaterialData(matStruct, data);
if (type == MaterialType::Forward)
{
shader = new Shader(matStruct->shaderPath, matStruct->shaderCode, FrameBufferType::Normal);
renderQueue = (int)shader->reference->shaderInfo.stateSet.renderQueue;
renderQueueType = shader->reference->shaderInfo.stateSet.renderQueue;
RenderAPI::GetInstance()->SetUpMaterial(this);
}
else if (type == MaterialType::Deferred)
{
renderQueue = (int)RenderQueueType::Deferred;
renderQueueType = RenderQueueType::Deferred;
}
else if (type == MaterialType::RayTracing)
{
hitGroupIdx = matStruct->hitGroupIdx;
// 光追管线暂时不需要区分队列,用一个默认的Opaque队列
renderQueue = (int)RenderQueueType::Opaque;
renderQueueType = RenderQueueType::Opaque;
RenderAPI::GetInstance()->SetUpRayTracingMaterialData(this);
}
}
Material::Material(Shader* shader)
{
// 这里要注意一个问题,如果调用这个构造函数时,是直接把new Shader写在参数里的,如:
// new Material(new Shader(...))
// 那么会有一个问题,new的时候没有保存引用,最终会少一个delete,导致referenceCount至少大于1,这个shader永远不会被正真销毁
isShareShader = true;
shader->reference->referenceCount++;
this->shader = shader;
renderQueue = (int)shader->reference->shaderInfo.stateSet.renderQueue;
// 通过这个构造函数初始化的材质,默认为光栅化渲染管线的材质
type = MaterialType::Forward;
data = new MaterialData(type);
RenderAPI::GetInstance()->SetUpMaterial(this);
}
Material::~Material()
{
delete data;
if (shader)
{
if (isShareShader)
shader->reference->referenceCount--;
else
delete shader;
}
}
void Material::Use()
{
data->Use();
shader->Use();
}
int Material::GetRenderQueue() const
{
return renderQueue;
}
RenderQueueType Material::GetRenderQueueType() const
{
return renderQueueType;
}
void Material::SetEngineProperties()
{
for (auto& property : shader->reference->shaderInfo.vertProperties.baseProperties)
SetEngineProperty(property.name, property.type);
for (auto& property : shader->reference->shaderInfo.vertProperties.textureProperties)
SetEngineProperty(property.name, property.type);
for (auto& property : shader->reference->shaderInfo.fragProperties.baseProperties)
SetEngineProperty(property.name, property.type);
for (auto& property : shader->reference->shaderInfo.fragProperties.textureProperties)
SetEngineProperty(property.name, property.type);
}
void Material::SetEngineProperty(const string& name, ShaderPropertyType type)
{
auto engineProperties = RenderEngineProperties::GetInstance();
if (type == ShaderPropertyType::ENGINE_MODEL)
SetMatrix(name, engineProperties->matM);
else if (type == ShaderPropertyType::ENGINE_VIEW)
SetMatrix(name, engineProperties->matV);
else if (type == ShaderPropertyType::ENGINE_PROJECTION)
SetMatrix(name, engineProperties->matP);
else if (type == ShaderPropertyType::ENGINE_CAMERA_POS)
SetVector(name, engineProperties->camPos);
else if (type == ShaderPropertyType::ENGINE_LIGHT_MAT)
SetMatrix(name, engineProperties->lightMat);
else if (type == ShaderPropertyType::ENGINE_LIGHT_POS)
SetVector(name, engineProperties->lightPos);
else if (type == ShaderPropertyType::ENGINE_LIGHT_DIR)
SetVector(name, engineProperties->lightDir);
else if (type == ShaderPropertyType::ENGINE_LIGHT_COLOR)
SetVector(name, engineProperties->lightColor);
else if (type == ShaderPropertyType::ENGINE_LIGHT_INTENSITY)
SetScalar(name, engineProperties->lightIntensity);
else if (type == ShaderPropertyType::ENGINE_FAR_PLANE)
SetScalar(name, GlobalData::shadowCubeMapFarPlane);
else if (type == ShaderPropertyType::ENGINE_MODEL_INV)
SetMatrix(name, engineProperties->matM_Inv);
else if (type == ShaderPropertyType::ENGINE_VIEW_INV)
SetMatrix(name, engineProperties->matV_Inv);
else if (type == ShaderPropertyType::ENGINE_PROJECTION_INV)
SetMatrix(name, engineProperties->matP_Inv);
else if (type == ShaderPropertyType::ENGINE_TIME)
SetVector(name, Vector2(Time::curTime, Time::deltaTime));
else if (type == ShaderPropertyType::ENGINE_DEPTH_MAP)
{
SetTexture(name, engineProperties->depthMap, textureIdx, false, true);
textureIdx++;
}
else if (type == ShaderPropertyType::ENGINE_SHADOW_MAP)
{
SetTexture(name, engineProperties->shadowMap, textureIdx, false, engineProperties->isShadowMapBuffer);
textureIdx++;
}
else if (type == ShaderPropertyType::ENGINE_SHADOW_CUBE_MAP)
{
// 先设置SetMaterialProperties获得引擎纹理的初始textureIdx,然后++
SetCubeMap(name, engineProperties->shadowCubeMap, textureIdx, false, engineProperties->isShadowCubeMapBuffer);
textureIdx++;
}
}
void Material::SetMaterialProperties()
{
if (data->isDirty)
{
for (auto& iter : data->floatDatas)
SetScalar(iter.first, iter.second);
for (auto& iter : data->uintDatas)
SetScalar(iter.first, iter.second);
for (auto& iter : data->vec2Datas)
SetVector(iter.first, iter.second);
for (auto& iter : data->vec3Datas)
SetVector(iter.first, iter.second);
for (auto& iter : data->vec4Datas)
SetVector(iter.first, iter.second);
for (auto& iter : data->colorDatas)
SetVector(iter.first, iter.second);
data->isDirty = false;
}
uint32_t textureNum = static_cast<uint32_t>(data->textures.size());
for (uint32_t i = 0; i < textureNum; i++)
{
if (data->textures[i].second->type == TextureType::ZX_2D)
SetTexture(data->textures[i].first, data->textures[i].second->GetID(), i);
else if (data->textures[i].second->type == TextureType::ZX_Cube)
SetCubeMap(data->textures[i].first, data->textures[i].second->GetID(), i);
}
textureIdx = textureNum;
}
void Material::SetMaterialProperty(const string& name, ShaderPropertyType type)
{
}
void Material::SetScalar(const string& name, bool value, bool allBuffer)
{
RenderAPI::GetInstance()->SetShaderScalar(this, name, value, allBuffer);
}
void Material::SetScalar(const string& name, float value, bool allBuffer)
{
RenderAPI::GetInstance()->SetShaderScalar(this, name, value, allBuffer);
}
void Material::SetScalar(const string& name, int32_t value, bool allBuffer)
{
RenderAPI::GetInstance()->SetShaderScalar(this, name, value, allBuffer);
}
void Material::SetScalar(const string& name, uint32_t value, bool allBuffer)
{
RenderAPI::GetInstance()->SetShaderScalar(this, name, value, allBuffer);
}
void Material::SetVector(const string& name, const Vector2& value, bool allBuffer)
{
RenderAPI::GetInstance()->SetShaderVector(this, name, value, allBuffer);
}
void Material::SetVector(const string& name, const Vector2& value, uint32_t idx, bool allBuffer)
{
RenderAPI::GetInstance()->SetShaderVector(this, name, value, idx, allBuffer);
}
void Material::SetVector(const string& name, const Vector3& value, bool allBuffer)
{
RenderAPI::GetInstance()->SetShaderVector(this, name, value, allBuffer);
}
void Material::SetVector(const string& name, const Vector3& value, uint32_t idx, bool allBuffer)
{
RenderAPI::GetInstance()->SetShaderVector(this, name, value, idx, allBuffer);
}
void Material::SetVector(const string& name, const Vector4& value, bool allBuffer)
{
RenderAPI::GetInstance()->SetShaderVector(this, name, value, allBuffer);
}
void Material::SetVector(const string& name, const Vector4& value, uint32_t idx, bool allBuffer)
{
RenderAPI::GetInstance()->SetShaderVector(this, name, value, idx, allBuffer);
}
void Material::SetVector(const string& name, const Vector4* value, uint32_t count, bool allBuffer)
{
RenderAPI::GetInstance()->SetShaderVector(this, name, value, count, allBuffer);
}
void Material::SetMatrix(const string& name, const Matrix3& value, bool allBuffer)
{
RenderAPI::GetInstance()->SetShaderMatrix(this, name, value, allBuffer);
}
void Material::SetMatrix(const string& name, const Matrix3& value, uint32_t idx, bool allBuffer)
{
RenderAPI::GetInstance()->SetShaderMatrix(this, name, value, idx, allBuffer);
}
void Material::SetMatrix(const string& name, const Matrix4& value, bool allBuffer)
{
RenderAPI::GetInstance()->SetShaderMatrix(this, name, value, allBuffer);
}
void Material::SetMatrix(const string& name, const Matrix4& value, uint32_t idx, bool allBuffer)
{
RenderAPI::GetInstance()->SetShaderMatrix(this, name, value, idx, allBuffer);
}
void Material::SetMatrix(const string& name, const Matrix4* value, uint32_t count, bool allBuffer)
{
RenderAPI::GetInstance()->SetShaderMatrix(this, name, value, count, allBuffer);
}
void Material::SetTexture(const string& name, uint32_t ID, uint32_t idx, bool allBuffer, bool isBuffer)
{
RenderAPI::GetInstance()->SetShaderTexture(this, name, ID, idx, allBuffer, isBuffer);
}
void Material::SetCubeMap(const string& name, uint32_t ID, uint32_t idx, bool allBuffer, bool isBuffer)
{
RenderAPI::GetInstance()->SetShaderCubeMap(this, name, ID, idx, allBuffer, isBuffer);
}
void Material::CopyMaterialStructToMaterialData(MaterialStruct* matStruct, MaterialData* data)
{
data->floatDatas = matStruct->floatDatas;
data->uintDatas = matStruct->uintDatas;
data->vec2Datas = matStruct->vec2Datas;
data->vec3Datas = matStruct->vec3Datas;
data->vec4Datas = matStruct->vec4Datas;
data->colorDatas = matStruct->colorDatas;
for (auto textureStruct : matStruct->textures)
{
Texture* texture = new Texture(textureStruct->data);
data->textures.push_back(make_pair(textureStruct->uniformName, texture));
}
for (auto cubeMapStruct : matStruct->cubeMaps)
{
Texture* texture = new Texture(cubeMapStruct->data);
data->textures.push_back(make_pair(cubeMapStruct->uniformName, texture));
}
}
}