Merge 'remotes/trunk'
[0ad.git] / source / renderer / InstancingModelRenderer.cpp
blob6a7acef8712e966c94e72d8301c2858105ff5bc1
1 /* Copyright (C) 2024 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(Renderer::Backend::IBuffer::Usage::TRANSFER_DST),
62 m_Array(Renderer::Backend::IBuffer::Type::VERTEX, Renderer::Backend::IBuffer::Usage::TRANSFER_DST)
64 size_t numVertices = mdef->GetNumVertices();
66 m_Position.format = Renderer::Backend::Format::R32G32B32_SFLOAT;
67 m_Array.AddAttribute(&m_Position);
69 m_Normal.format = Renderer::Backend::Format::R32G32B32_SFLOAT;
70 m_Array.AddAttribute(&m_Normal);
72 m_UVs.resize(mdef->GetNumUVsPerVertex());
73 for (size_t i = 0; i < mdef->GetNumUVsPerVertex(); i++)
75 m_UVs[i].format = Renderer::Backend::Format::R32G32_SFLOAT;
76 m_Array.AddAttribute(&m_UVs[i]);
79 if (gpuSkinning)
81 // We can't use a lot of bones because it costs uniform memory. Recommended
82 // number of bones per model is 32.
83 // Add 1 to NumBones because of the special 'root' bone.
84 if (mdef->GetNumBones() + 1 > 64)
85 LOGERROR("Model '%s' has too many bones %zu/64", mdef->GetName().string8().c_str(), mdef->GetNumBones() + 1);
86 ENSURE(mdef->GetNumBones() + 1 <= 64);
88 m_BlendJoints.format = Renderer::Backend::Format::R8G8B8A8_UINT;
89 m_Array.AddAttribute(&m_BlendJoints);
91 m_BlendWeights.format = Renderer::Backend::Format::R8G8B8A8_UNORM;
92 m_Array.AddAttribute(&m_BlendWeights);
95 if (calculateTangents)
97 // Generate tangents for the geometry:-
99 m_Tangent.format = Renderer::Backend::Format::R32G32B32A32_SFLOAT;
100 m_Array.AddAttribute(&m_Tangent);
102 // floats per vertex; position + normal + tangent + UV*sets [+ GPUskinning]
103 int numVertexAttrs = 3 + 3 + 4 + 2 * mdef->GetNumUVsPerVertex();
104 if (gpuSkinning)
106 numVertexAttrs += 8;
109 // the tangent generation can increase the number of vertices temporarily
110 // so reserve a bit more memory to avoid reallocations in GenTangents (in most cases)
111 std::vector<float> newVertices;
112 newVertices.reserve(numVertexAttrs * numVertices * 2);
114 // Generate the tangents
115 ModelRenderer::GenTangents(mdef, newVertices, gpuSkinning);
117 // how many vertices do we have after generating tangents?
118 int newNumVert = newVertices.size() / numVertexAttrs;
120 std::vector<int> remapTable(newNumVert);
121 std::vector<float> vertexDataOut(newNumVert * numVertexAttrs);
123 // re-weld the mesh to remove duplicated vertices
124 int numVertices2 = WeldMesh(&remapTable[0], &vertexDataOut[0],
125 &newVertices[0], newNumVert, numVertexAttrs);
127 // Copy the model data to graphics memory:-
129 m_Array.SetNumberOfVertices(numVertices2);
130 m_Array.Layout();
132 VertexArrayIterator<CVector3D> Position = m_Position.GetIterator<CVector3D>();
133 VertexArrayIterator<CVector3D> Normal = m_Normal.GetIterator<CVector3D>();
134 VertexArrayIterator<CVector4D> Tangent = m_Tangent.GetIterator<CVector4D>();
136 VertexArrayIterator<u8[4]> BlendJoints;
137 VertexArrayIterator<u8[4]> BlendWeights;
138 if (gpuSkinning)
140 BlendJoints = m_BlendJoints.GetIterator<u8[4]>();
141 BlendWeights = m_BlendWeights.GetIterator<u8[4]>();
144 // copy everything into the vertex array
145 for (int i = 0; i < numVertices2; i++)
147 int q = numVertexAttrs * i;
149 Position[i] = CVector3D(vertexDataOut[q + 0], vertexDataOut[q + 1], vertexDataOut[q + 2]);
150 q += 3;
152 Normal[i] = CVector3D(vertexDataOut[q + 0], vertexDataOut[q + 1], vertexDataOut[q + 2]);
153 q += 3;
155 Tangent[i] = CVector4D(vertexDataOut[q + 0], vertexDataOut[q + 1], vertexDataOut[q + 2],
156 vertexDataOut[q + 3]);
157 q += 4;
159 if (gpuSkinning)
161 for (size_t j = 0; j < 4; ++j)
163 BlendJoints[i][j] = (u8)vertexDataOut[q + 0 + 2 * j];
164 BlendWeights[i][j] = (u8)vertexDataOut[q + 1 + 2 * j];
166 q += 8;
169 for (size_t j = 0; j < mdef->GetNumUVsPerVertex(); j++)
171 VertexArrayIterator<float[2]> UVit = m_UVs[j].GetIterator<float[2]>();
172 UVit[i][0] = vertexDataOut[q + 0 + 2 * j];
173 UVit[i][1] = vertexDataOut[q + 1 + 2 * j];
177 // upload vertex data
178 m_Array.Upload();
179 m_Array.FreeBackingStore();
181 m_IndexArray.SetNumberOfVertices(mdef->GetNumFaces() * 3);
182 m_IndexArray.Layout();
184 VertexArrayIterator<u16> Indices = m_IndexArray.GetIterator();
186 size_t idxidx = 0;
188 // reindex geometry and upload index
189 for (size_t j = 0; j < mdef->GetNumFaces(); ++j)
191 Indices[idxidx++] = remapTable[j * 3 + 0];
192 Indices[idxidx++] = remapTable[j * 3 + 1];
193 Indices[idxidx++] = remapTable[j * 3 + 2];
196 m_IndexArray.Upload();
197 m_IndexArray.FreeBackingStore();
199 else
201 // Upload model without calculating tangents:-
203 m_Array.SetNumberOfVertices(numVertices);
204 m_Array.Layout();
206 VertexArrayIterator<CVector3D> Position = m_Position.GetIterator<CVector3D>();
207 VertexArrayIterator<CVector3D> Normal = m_Normal.GetIterator<CVector3D>();
209 ModelRenderer::CopyPositionAndNormals(mdef, Position, Normal);
211 for (size_t i = 0; i < mdef->GetNumUVsPerVertex(); i++)
213 VertexArrayIterator<float[2]> UVit = m_UVs[i].GetIterator<float[2]>();
214 ModelRenderer::BuildUV(mdef, UVit, i);
217 if (gpuSkinning)
219 VertexArrayIterator<u8[4]> BlendJoints = m_BlendJoints.GetIterator<u8[4]>();
220 VertexArrayIterator<u8[4]> BlendWeights = m_BlendWeights.GetIterator<u8[4]>();
221 for (size_t i = 0; i < numVertices; ++i)
223 const SModelVertex& vtx = mdef->GetVertices()[i];
224 for (size_t j = 0; j < 4; ++j)
226 BlendJoints[i][j] = vtx.m_Blend.m_Bone[j];
227 BlendWeights[i][j] = (u8)(255.f * vtx.m_Blend.m_Weight[j]);
232 m_Array.Upload();
233 m_Array.FreeBackingStore();
235 m_IndexArray.SetNumberOfVertices(mdef->GetNumFaces()*3);
236 m_IndexArray.Layout();
237 ModelRenderer::BuildIndices(mdef, m_IndexArray.GetIterator());
238 m_IndexArray.Upload();
239 m_IndexArray.FreeBackingStore();
242 const uint32_t stride = m_Array.GetStride();
243 constexpr size_t MAX_UV = 2;
245 PS::StaticVector<Renderer::Backend::SVertexAttributeFormat, 5 + MAX_UV> attributes{
246 {Renderer::Backend::VertexAttributeStream::POSITION,
247 m_Position.format, m_Position.offset, stride,
248 Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0},
249 {Renderer::Backend::VertexAttributeStream::NORMAL,
250 m_Normal.format, m_Normal.offset, stride,
251 Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0}
254 for (size_t uv = 0; uv < std::min(MAX_UV, mdef->GetNumUVsPerVertex()); ++uv)
256 const Renderer::Backend::VertexAttributeStream stream =
257 static_cast<Renderer::Backend::VertexAttributeStream>(
258 static_cast<int>(Renderer::Backend::VertexAttributeStream::UV0) + uv);
259 attributes.push_back({
260 stream, m_UVs[uv].format, m_UVs[uv].offset, stride,
261 Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0});
264 // GPU skinning requires extra attributes to compute positions/normals.
265 if (gpuSkinning)
267 attributes.push_back({
268 Renderer::Backend::VertexAttributeStream::UV2,
269 m_BlendJoints.format, m_BlendJoints.offset, stride,
270 Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0});
271 attributes.push_back({
272 Renderer::Backend::VertexAttributeStream::UV3,
273 m_BlendWeights.format, m_BlendWeights.offset, stride,
274 Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0});
277 if (calculateTangents)
279 attributes.push_back({
280 Renderer::Backend::VertexAttributeStream::UV4,
281 m_Tangent.format, m_Tangent.offset, stride,
282 Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0});
285 m_VertexInputLayout = g_Renderer.GetVertexInputLayout({attributes.begin(), attributes.end()});
288 struct InstancingModelRendererInternals
290 bool gpuSkinning;
292 bool calculateTangents;
294 /// Previously prepared modeldef
295 IModelDef* imodeldef;
297 /// Index base for imodeldef
298 u8* imodeldefIndexBase;
302 // Construction and Destruction
303 InstancingModelRenderer::InstancingModelRenderer(bool gpuSkinning, bool calculateTangents)
305 m = new InstancingModelRendererInternals;
306 m->gpuSkinning = gpuSkinning;
307 m->calculateTangents = calculateTangents;
308 m->imodeldef = 0;
311 InstancingModelRenderer::~InstancingModelRenderer()
313 delete m;
317 // Build modeldef data if necessary - we have no per-CModel data
318 CModelRData* InstancingModelRenderer::CreateModelData(const void* key, CModel* model)
320 CModelDefPtr mdef = model->GetModelDef();
321 IModelDef* imodeldef = (IModelDef*)mdef->GetRenderData(m);
323 if (m->gpuSkinning)
324 ENSURE(model->IsSkinned());
325 else
326 ENSURE(!model->IsSkinned());
328 if (!imodeldef)
330 imodeldef = new IModelDef(mdef, m->gpuSkinning, m->calculateTangents);
331 mdef->SetRenderData(m, imodeldef);
334 return new CModelRData(key);
338 void InstancingModelRenderer::UpdateModelData(CModel* UNUSED(model), CModelRData* UNUSED(data), int UNUSED(updateflags))
340 // We have no per-CModel data
343 void InstancingModelRenderer::UploadModelData(
344 Renderer::Backend::IDeviceCommandContext* UNUSED(deviceCommandContext),
345 CModel* UNUSED(model), CModelRData* UNUSED(data))
347 // Data uploaded once during creation as we don't update it dynamically.
350 // Prepare UV coordinates for this modeldef
351 void InstancingModelRenderer::PrepareModelDef(
352 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
353 const CModelDef& def)
355 m->imodeldef = (IModelDef*)def.GetRenderData(m);
356 ENSURE(m->imodeldef);
358 deviceCommandContext->SetVertexInputLayout(m->imodeldef->m_VertexInputLayout);
360 deviceCommandContext->SetIndexBuffer(m->imodeldef->m_IndexArray.GetBuffer());
362 const uint32_t stride = m->imodeldef->m_Array.GetStride();
363 const uint32_t firstVertexOffset = m->imodeldef->m_Array.GetOffset() * stride;
365 deviceCommandContext->SetVertexBuffer(
366 0, m->imodeldef->m_Array.GetBuffer(), firstVertexOffset);
370 // Render one model
371 void InstancingModelRenderer::RenderModel(
372 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
373 Renderer::Backend::IShaderProgram* shader, CModel* model, CModelRData* UNUSED(data))
375 const CModelDefPtr& mdldef = model->GetModelDef();
377 if (m->gpuSkinning)
379 // Bind matrices for current animation state.
380 // Add 1 to NumBones because of the special 'root' bone.
381 deviceCommandContext->SetUniform(
382 shader->GetBindingSlot(str_skinBlendMatrices),
383 PS::span<const float>(
384 model->GetAnimatedBoneMatrices()[0]._data,
385 model->GetAnimatedBoneMatrices()[0].AsFloatArray().size() * (mdldef->GetNumBones() + 1)));
388 // Render the lot.
389 const size_t numberOfFaces = mdldef->GetNumFaces();
391 deviceCommandContext->DrawIndexedInRange(
392 m->imodeldef->m_IndexArray.GetOffset(), numberOfFaces * 3, 0, m->imodeldef->m_Array.GetNumberOfVertices() - 1);
394 // Bump stats.
395 g_Renderer.m_Stats.m_DrawCalls++;
396 g_Renderer.m_Stats.m_ModelTris += numberOfFaces;