-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSceneRendererExtend.cpp
206 lines (182 loc) · 8.87 KB
/
SceneRendererExtend.cpp
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
#include "SceneRendererExtend.h"
#include "ModelResource.h"
#include "ModelViewer.h"
SceneRendererExtend::SceneRendererExtend(const Scene::SharedPtr& pScene) :SceneRenderer(pScene)
{
mpOutlineMaterial = std::make_shared<MaterialInstance>("OutlinePass");
ModelViewer::getMaterialFuncMap().at("OutlinePass")(mpOutlineMaterial);
}
void SceneRendererExtend::renderScene(RenderContext* pContext, const Fbo::SharedPtr& fbo, const Camera* pCamera, bool renderDepth)
{
mpFbo = fbo;
mSetShaderForEachMaterial = renderDepth;
CurrentWorkingData currentData;
currentData.pContext = pContext;
currentData.pState = pContext->getGraphicsState().get();
currentData.pVars = pContext->getGraphicsVars().get();
currentData.pCamera = pCamera;
currentData.pMaterial = nullptr;
currentData.pModel = nullptr;
currentData.drawID = 0;
render(currentData);
}
void SceneRendererExtend::setPerFrameData(const CurrentWorkingData& currentData)
{
ConstantBuffer* pCB = currentData.pContext->getGraphicsVars()->getConstantBuffer(kPerFrameCbName).get();
if (pCB)
{
// Set camera
if (currentData.pCamera)
{
currentData.pCamera->setIntoConstantBuffer(pCB, sCameraDataOffset);
}
// Set lights
if (sLightArrayOffset != ConstantBuffer::kInvalidOffset)
{
assert(mpScene->getLightCount() <= MAX_LIGHT_SOURCES); // Max array size in the shader
for (uint32_t i = 0; i < mpScene->getLightCount(); i++)
{
mpScene->getLight(i)->setIntoProgramVars(currentData.pVars, pCB, sLightArrayOffset + (i * Light::getShaderStructSize()));
}
}
if (sLightCountOffset != ConstantBuffer::kInvalidOffset)
{
pCB->setVariable(sLightCountOffset, mpScene->getLightCount());
}
if (mpScene->getLightProbeCount() > 0)
{
// #TODO Support multiple light probes
LightProbe::setCommonIntoProgramVars(currentData.pVars, kProbeSharedVarName);
mpScene->getLightProbe(0)->setIntoProgramVars(currentData.pVars, pCB, kProbeVarName);
}
}
if (mpScene->getAreaLightCount() > 0)
{
const ParameterBlockReflection* pBlock = currentData.pVars->getReflection()->getDefaultParameterBlock().get();
// If area lights have been declared
const ReflectionVar* pVar = pBlock->getResource(kAreaLightCbName).get();
if (pVar != nullptr)
{
const ReflectionVar* pAreaLightVar = pVar->getType()->findMember("gAreaLights").get();
assert(pAreaLightVar != nullptr);
uint32_t areaLightArraySize = pAreaLightVar->getType()->asArrayType()->getArraySize();
for (uint32_t i = 0; i < min(areaLightArraySize, mpScene->getAreaLightCount()); i++)
{
std::string varName = "gAreaLights[" + std::to_string(i) + "]";
mpScene->getAreaLight(i)->setIntoProgramVars(currentData.pVars, currentData.pVars->getConstantBuffer(kAreaLightCbName).get(), varName.c_str());
}
}
}
}
bool SceneRendererExtend::setPerModelData(const CurrentWorkingData & currentData)
{
const Model* pModel = currentData.pModel;
// Set bones
if (pModel->hasBones())
{
ConstantBuffer* pCB = currentData.pContext->getGraphicsVars()->getConstantBuffer(kBoneCbName).get();
if (pCB != nullptr)
{
if (sBonesOffset == ConstantBuffer::kInvalidOffset || sBonesInvTransposeOffset == ConstantBuffer::kInvalidOffset)
{
sBonesOffset = pCB->getVariableOffset("gBoneMat[0]");
sBonesInvTransposeOffset = pCB->getVariableOffset("gInvTransposeBoneMat[0]");
}
assert(pModel->getBoneCount() <= MAX_BONES);
pCB->setVariableArray(sBonesOffset, pModel->getBoneMatrices(), pModel->getBoneCount());
pCB->setVariableArray(sBonesInvTransposeOffset, pModel->getBoneInvTransposeMatrices(), pModel->getBoneCount());
}
}
return true;
}
bool SceneRendererExtend::setPerMeshInstanceData(const CurrentWorkingData & currentData, const Scene::ModelInstance * pModelInstance, const Model::MeshInstance * pMeshInstance, uint32_t drawInstanceID)
{
ConstantBuffer* pCB = currentData.pContext->getGraphicsVars()->getConstantBuffer(kPerMeshCbName).get();
if (pCB)
{
const Mesh* pMesh = pMeshInstance->getObject().get();
glm::mat4 worldMat = pModelInstance->getTransformMatrix();
glm::mat4 prevWorldMat = pModelInstance->getPrevTransformMatrix();
if (pMesh->hasBones() == false)
{
worldMat = worldMat * pMeshInstance->getTransformMatrix();
prevWorldMat = prevWorldMat * pMeshInstance->getPrevTransformMatrix();
}
glm::mat3x4 worldInvTransposeMat = transpose(inverse(glm::mat3(worldMat)));
assert(drawInstanceID < sWorldMatArraySize);
pCB->setBlob(&worldMat, sWorldMatOffset + drawInstanceID * sizeof(glm::mat4), sizeof(glm::mat4));
pCB->setBlob(&worldInvTransposeMat, sWorldInvTransposeMatOffset + drawInstanceID * sizeof(glm::mat3x4), sizeof(glm::mat3x4)); // HLSL uses column-major and packing rules require 16B alignment, hence use glm:mat3x4
pCB->setBlob(&prevWorldMat, sPrevWorldMatOffset + drawInstanceID * sizeof(glm::mat4), sizeof(glm::mat4));
// Set mesh id
pCB->setVariable(sMeshIdOffset, pMesh->getId());
}
return true;
}
bool SceneRendererExtend::setPerMaterialData(const CurrentWorkingData & currentData, const Material * pMaterial)
{
currentData.pContext->getGraphicsVars()->setParameterBlock("gMaterial", pMaterial->getParameterBlock());
return true;
}
void SceneRendererExtend::render(CurrentWorkingData & currentData)
{
mSavedInstance.clear();
for (uint32_t modelID = 0; modelID < mpScene->getModelCount(); modelID++)
{
currentData.pModel = mpScene->getModel(modelID).get();
if (setPerModelData(currentData))
{
for (uint32_t instanceID = 0; instanceID < mpScene->getModelInstanceCount(modelID); instanceID++)
{
const auto pInstance = mpScene->getModelInstance(modelID, instanceID).get();
if (pInstance->isVisible())
{
if (setPerModelInstanceData(currentData, pInstance, instanceID))
{
mpLastMaterial = nullptr;
ModelResource& res = getScene()->getModelResource(modelID);
pInstance->setTranslation(res.Translation, true);
pInstance->setRotation(res.Rotation);
pInstance->setScaling(res.Scale);
// Loop over the meshes
for (uint32_t meshID = 0; meshID < pInstance->getObject()->getMeshCount(); meshID++)
{
if (mSetShaderForEachMaterial) {
auto& mat = res.getMaterialInstance(meshID);
currentData.pState = mat->get_state().get();
currentData.pVars = mat->get_programVars().get();
mSavedInstance.emplace_back(mat.get(), pInstance, meshID, currentData);
}
else {//render depth buffer
updateVariableOffsets(currentData.pVars->getReflection().get());
setPerFrameData(currentData);
renderMeshInstances(currentData, pInstance, meshID);
}
}
}
}
}
}
}
if (mSetShaderForEachMaterial) {
std::sort(mSavedInstance.begin(), mSavedInstance.end(), [](const MaterialInstanceBuffer & l, const MaterialInstanceBuffer & r)->bool {
return l.pMaterialInstance->ComaparsionQueue(r.pMaterialInstance);
});
for (auto& mat : mSavedInstance) {
if (mat.pMaterialInstance->get_renderOutline()) {
//ModelViewer::getMaterialFunc("OutlinePass")();
mat.data.pState = mpOutlineMaterial->get_state().get();
mat.data.pVars = mpOutlineMaterial->get_programVars().get();
mat.data.pState->setFbo(mpFbo);
mpOutlineMaterial->onRender(mat.data.pContext);
updateVariableOffsets(mpOutlineMaterial->get_programVars()->getReflection().get());
setPerFrameData(mat.data);
renderMeshInstances(mat.data, mat.pModelInstance, mat.meshId);
}
mat.data.pState->setFbo(mpFbo);
mat.pMaterialInstance->onRender(mat.data.pContext);
updateVariableOffsets(mat.data.pVars->getReflection().get());
setPerFrameData(mat.data);
renderMeshInstances(mat.data, mat.pModelInstance, mat.meshId);
}
}
}