Merge 'remotes/trunk'
[0ad.git] / source / renderer / InstancingModelRenderer.cpp
blob4c6f5cb00a46799ab902cc556bbba3ab9193b932
1 /* Copyright (C) 2023 Wildfire Games.
2 * This file is part of 0 A.D.
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
18 #include "precompiled.h"
19 #include "renderer/InstancingModelRenderer.h"
21 #include "graphics/Color.h"
22 #include "graphics/LightEnv.h"
23 #include "graphics/Model.h"
24 #include "graphics/ModelDef.h"
25 #include "maths/Vector3D.h"
26 #include "maths/Vector4D.h"
27 #include "ps/CLogger.h"
28 #include "ps/containers/StaticVector.h"
29 #include "ps/CStrInternStatic.h"
30 #include "renderer/Renderer.h"
31 #include "renderer/RenderModifiers.h"
32 #include "renderer/VertexArray.h"
33 #include "third_party/mikktspace/weldmesh.h"
36 struct IModelDef : public CModelDefRPrivate
38 /// Static per-CModel vertex array
39 VertexArray m_Array;
41 /// Position and normals are static
42 VertexArray::Attribute m_Position;
43 VertexArray::Attribute m_Normal;
44 VertexArray::Attribute m_Tangent;
45 VertexArray::Attribute m_BlendJoints; // valid iff gpuSkinning == true
46 VertexArray::Attribute m_BlendWeights; // valid iff gpuSkinning == true
48 /// The number of UVs is determined by the model
49 std::vector<VertexArray::Attribute> m_UVs;
51 Renderer::Backend::IVertexInputLayout* m_VertexInputLayout = nullptr;
53 /// Indices are the same for all models, so share them
54 VertexIndexArray m_IndexArray;
56 IModelDef(const CModelDefPtr& mdef, bool gpuSkinning, bool calculateTangents);
60 IModelDef::IModelDef(const CModelDefPtr& mdef, bool gpuSkinning, bool calculateTangents)
61 : m_IndexArray(false), m_Array(Renderer::Backend::IBuffer::Type::VERTEX, false)
63 size_t numVertices = mdef->GetNumVertices();
65 m_Position.format = Renderer::Backend::Format::R32G32B32_SFLOAT;
66 m_Array.AddAttribute(&m_Position);
68 m_Normal.format = Renderer::Backend::Format::R32G32B32_SFLOAT;
69 m_Array.AddAttribute(&m_Normal);
71 m_UVs.resize(mdef->GetNumUVsPerVertex());
72 for (size_t i = 0; i < mdef->GetNumUVsPerVertex(); i++)
74 m_UVs[i].format = Renderer::Backend::Format::R32G32_SFLOAT;
75 m_Array.AddAttribute(&m_UVs[i]);
78 if (gpuSkinning)
80 // We can't use a lot of bones because it costs uniform memory. Recommended
81 // number of bones per model is 32.
82 // Add 1 to NumBones because of the special 'root' bone.
83 if (mdef->GetNumBones() + 1 > 64)
84 LOGERROR("Model '%s' has too many bones %zu/64", mdef->GetName().string8().c_str(), mdef->GetNumBones() + 1);
85 ENSURE(mdef->GetNumBones() + 1 <= 64);
87 m_BlendJoints.format = Renderer::Backend::Format::R8G8B8A8_UINT;
88 m_Array.AddAttribute(&m_BlendJoints);
90 m_BlendWeights.format = Renderer::Backend::Format::R8G8B8A8_UNORM;
91 m_Array.AddAttribute(&m_BlendWeights);
94 if (calculateTangents)
96 // Generate tangents for the geometry:-
98 m_Tangent.format = Renderer::Backend::Format::R32G32B32A32_SFLOAT;
99 m_Array.AddAttribute(&m_Tangent);
101 // floats per vertex; position + normal + tangent + UV*sets [+ GPUskinning]
102 int numVertexAttrs = 3 + 3 + 4 + 2 * mdef->GetNumUVsPerVertex();
103 if (gpuSkinning)
105 numVertexAttrs += 8;
108 // the tangent generation can increase the number of vertices temporarily
109 // so reserve a bit more memory to avoid reallocations in GenTangents (in most cases)
110 std::vector<float> newVertices;
111 newVertices.reserve(numVertexAttrs * numVertices * 2);
113 // Generate the tangents
114 ModelRenderer::GenTangents(mdef, newVertices, gpuSkinning);
116 // how many vertices do we have after generating tangents?
117 int newNumVert = newVertices.size() / numVertexAttrs;
119 std::vector<int> remapTable(newNumVert);
120 std::vector<float> vertexDataOut(newNumVert * numVertexAttrs);
122 // re-weld the mesh to remove duplicated vertices
123 int numVertices2 = WeldMesh(&remapTable[0], &vertexDataOut[0],
124 &newVertices[0], newNumVert, numVertexAttrs);
126 // Copy the model data to graphics memory:-
128 m_Array.SetNumberOfVertices(numVertices2);
129 m_Array.Layout();
131 VertexArrayIterator<CVector3D> Position = m_Position.GetIterator<CVector3D>();
132 VertexArrayIterator<CVector3D> Normal = m_Normal.GetIterator<CVector3D>();
133 VertexArrayIterator<CVector4D> Tangent = m_Tangent.GetIterator<CVector4D>();
135 VertexArrayIterator<u8[4]> BlendJoints;
136 VertexArrayIterator<u8[4]> BlendWeights;
137 if (gpuSkinning)
139 BlendJoints = m_BlendJoints.GetIterator<u8[4]>();
140 BlendWeights = m_BlendWeights.GetIterator<u8[4]>();
143 // copy everything into the vertex array
144 for (int i = 0; i < numVertices2; i++)
146 int q = numVertexAttrs * i;
148 Position[i] = CVector3D(vertexDataOut[q + 0], vertexDataOut[q + 1], vertexDataOut[q + 2]);
149 q += 3;
151 Normal[i] = CVector3D(vertexDataOut[q + 0], vertexDataOut[q + 1], vertexDataOut[q + 2]);
152 q += 3;
154 Tangent[i] = CVector4D(vertexDataOut[q + 0], vertexDataOut[q + 1], vertexDataOut[q + 2],
155 vertexDataOut[q + 3]);
156 q += 4;
158 if (gpuSkinning)
160 for (size_t j = 0; j < 4; ++j)
162 BlendJoints[i][j] = (u8)vertexDataOut[q + 0 + 2 * j];
163 BlendWeights[i][j] = (u8)vertexDataOut[q + 1 + 2 * j];
165 q += 8;
168 for (size_t j = 0; j < mdef->GetNumUVsPerVertex(); j++)
170 VertexArrayIterator<float[2]> UVit = m_UVs[j].GetIterator<float[2]>();
171 UVit[i][0] = vertexDataOut[q + 0 + 2 * j];
172 UVit[i][1] = vertexDataOut[q + 1 + 2 * j];
176 // upload vertex data
177 m_Array.Upload();
178 m_Array.FreeBackingStore();
180 m_IndexArray.SetNumberOfVertices(mdef->GetNumFaces() * 3);
181 m_IndexArray.Layout();
183 VertexArrayIterator<u16> Indices = m_IndexArray.GetIterator();
185 size_t idxidx = 0;
187 // reindex geometry and upload index
188 for (size_t j = 0; j < mdef->GetNumFaces(); ++j)
190 Indices[idxidx++] = remapTable[j * 3 + 0];
191 Indices[idxidx++] = remapTable[j * 3 + 1];
192 Indices[idxidx++] = remapTable[j * 3 + 2];
195 m_IndexArray.Upload();
196 m_IndexArray.FreeBackingStore();
198 else
200 // Upload model without calculating tangents:-
202 m_Array.SetNumberOfVertices(numVertices);
203 m_Array.Layout();
205 VertexArrayIterator<CVector3D> Position = m_Position.GetIterator<CVector3D>();
206 VertexArrayIterator<CVector3D> Normal = m_Normal.GetIterator<CVector3D>();
208 ModelRenderer::CopyPositionAndNormals(mdef, Position, Normal);
210 for (size_t i = 0; i < mdef->GetNumUVsPerVertex(); i++)
212 VertexArrayIterator<float[2]> UVit = m_UVs[i].GetIterator<float[2]>();
213 ModelRenderer::BuildUV(mdef, UVit, i);
216 if (gpuSkinning)
218 VertexArrayIterator<u8[4]> BlendJoints = m_BlendJoints.GetIterator<u8[4]>();
219 VertexArrayIterator<u8[4]> BlendWeights = m_BlendWeights.GetIterator<u8[4]>();
220 for (size_t i = 0; i < numVertices; ++i)
222 const SModelVertex& vtx = mdef->GetVertices()[i];
223 for (size_t j = 0; j < 4; ++j)
225 BlendJoints[i][j] = vtx.m_Blend.m_Bone[j];
226 BlendWeights[i][j] = (u8)(255.f * vtx.m_Blend.m_Weight[j]);
231 m_Array.Upload();
232 m_Array.FreeBackingStore();
234 m_IndexArray.SetNumberOfVertices(mdef->GetNumFaces()*3);
235 m_IndexArray.Layout();
236 ModelRenderer::BuildIndices(mdef, m_IndexArray.GetIterator());
237 m_IndexArray.Upload();
238 m_IndexArray.FreeBackingStore();
241 const uint32_t stride = m_Array.GetStride();
242 constexpr size_t MAX_UV = 2;
244 PS::StaticVector<Renderer::Backend::SVertexAttributeFormat, 5 + MAX_UV> attributes{
245 {Renderer::Backend::VertexAttributeStream::POSITION,
246 m_Position.format, m_Position.offset, stride,
247 Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0},
248 {Renderer::Backend::VertexAttributeStream::NORMAL,
249 m_Normal.format, m_Normal.offset, stride,
250 Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0}
253 for (size_t uv = 0; uv < std::min(MAX_UV, mdef->GetNumUVsPerVertex()); ++uv)
255 const Renderer::Backend::VertexAttributeStream stream =
256 static_cast<Renderer::Backend::VertexAttributeStream>(
257 static_cast<int>(Renderer::Backend::VertexAttributeStream::UV0) + uv);
258 attributes.push_back({
259 stream, m_UVs[uv].format, m_UVs[uv].offset, stride,
260 Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0});
263 // GPU skinning requires extra attributes to compute positions/normals.
264 if (gpuSkinning)
266 attributes.push_back({
267 Renderer::Backend::VertexAttributeStream::UV2,
268 m_BlendJoints.format, m_BlendJoints.offset, stride,
269 Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0});
270 attributes.push_back({
271 Renderer::Backend::VertexAttributeStream::UV3,
272 m_BlendWeights.format, m_BlendWeights.offset, stride,
273 Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0});
276 if (calculateTangents)
278 attributes.push_back({
279 Renderer::Backend::VertexAttributeStream::UV4,
280 m_Tangent.format, m_Tangent.offset, stride,
281 Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0});
284 m_VertexInputLayout = g_Renderer.GetVertexInputLayout({attributes.begin(), attributes.end()});
287 struct InstancingModelRendererInternals
289 bool gpuSkinning;
291 bool calculateTangents;
293 /// Previously prepared modeldef
294 IModelDef* imodeldef;
296 /// Index base for imodeldef
297 u8* imodeldefIndexBase;
301 // Construction and Destruction
302 InstancingModelRenderer::InstancingModelRenderer(bool gpuSkinning, bool calculateTangents)
304 m = new InstancingModelRendererInternals;
305 m->gpuSkinning = gpuSkinning;
306 m->calculateTangents = calculateTangents;
307 m->imodeldef = 0;
310 InstancingModelRenderer::~InstancingModelRenderer()
312 delete m;
316 // Build modeldef data if necessary - we have no per-CModel data
317 CModelRData* InstancingModelRenderer::CreateModelData(const void* key, CModel* model)
319 CModelDefPtr mdef = model->GetModelDef();
320 IModelDef* imodeldef = (IModelDef*)mdef->GetRenderData(m);
322 if (m->gpuSkinning)
323 ENSURE(model->IsSkinned());
324 else
325 ENSURE(!model->IsSkinned());
327 if (!imodeldef)
329 imodeldef = new IModelDef(mdef, m->gpuSkinning, m->calculateTangents);
330 mdef->SetRenderData(m, imodeldef);
333 return new CModelRData(key);
337 void InstancingModelRenderer::UpdateModelData(CModel* UNUSED(model), CModelRData* UNUSED(data), int UNUSED(updateflags))
339 // We have no per-CModel data
342 void InstancingModelRenderer::UploadModelData(
343 Renderer::Backend::IDeviceCommandContext* UNUSED(deviceCommandContext),
344 CModel* UNUSED(model), CModelRData* UNUSED(data))
346 // Data uploaded once during creation as we don't update it dynamically.
349 // Prepare UV coordinates for this modeldef
350 void InstancingModelRenderer::PrepareModelDef(
351 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
352 const CModelDef& def)
354 m->imodeldef = (IModelDef*)def.GetRenderData(m);
355 ENSURE(m->imodeldef);
357 deviceCommandContext->SetVertexInputLayout(m->imodeldef->m_VertexInputLayout);
359 deviceCommandContext->SetIndexBuffer(m->imodeldef->m_IndexArray.GetBuffer());
361 const uint32_t stride = m->imodeldef->m_Array.GetStride();
362 const uint32_t firstVertexOffset = m->imodeldef->m_Array.GetOffset() * stride;
364 deviceCommandContext->SetVertexBuffer(
365 0, m->imodeldef->m_Array.GetBuffer(), firstVertexOffset);
369 // Render one model
370 void InstancingModelRenderer::RenderModel(
371 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
372 Renderer::Backend::IShaderProgram* shader, CModel* model, CModelRData* UNUSED(data))
374 const CModelDefPtr& mdldef = model->GetModelDef();
376 if (m->gpuSkinning)
378 // Bind matrices for current animation state.
379 // Add 1 to NumBones because of the special 'root' bone.
380 deviceCommandContext->SetUniform(
381 shader->GetBindingSlot(str_skinBlendMatrices),
382 PS::span<const float>(
383 model->GetAnimatedBoneMatrices()[0]._data,
384 model->GetAnimatedBoneMatrices()[0].AsFloatArray().size() * (mdldef->GetNumBones() + 1)));
387 // Render the lot.
388 const size_t numberOfFaces = mdldef->GetNumFaces();
390 deviceCommandContext->DrawIndexedInRange(
391 m->imodeldef->m_IndexArray.GetOffset(), numberOfFaces * 3, 0, m->imodeldef->m_Array.GetNumberOfVertices() - 1);
393 // Bump stats.
394 g_Renderer.m_Stats.m_DrawCalls++;
395 g_Renderer.m_Stats.m_ModelTris += numberOfFaces;