Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
PatchRData.h
Go to the documentation of this file.
1/* Copyright (C) 2026 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
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.
8 *
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.
13 *
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/>.
16 */
17
18#ifndef INCLUDED_PATCHRDATA
19#define INCLUDED_PATCHRDATA
20
21#include "graphics/Material.h"
22#include "graphics/Patch.h"
24#include "lib/code_annotation.h"
27#include "maths/Vector2D.h"
28#include "maths/Vector3D.h"
30
31#include <cstddef>
32#include <vector>
33
34class CShaderDefines;
35class CSimulation2;
37class CTextRenderer;
38class ShadowMap;
39namespace Renderer::Backend { class IDeviceCommandContext; }
40namespace Renderer::Backend { class IVertexInputLayout; }
41
42//////////////////////////////////////////////////////////////////////////////////////////////////
43// CPatchRData: class encapsulating logic for rendering terrain patches; holds per
44// patch data, plus some supporting static functions for batching, etc
46{
47public:
48 CPatchRData(CPatch* patch, CSimulation2* simulation);
50
54 const bool bindPositionAsTexCoord);
56
58 const bool bindWaterData);
60
61 void Update(CSimulation2* simulation);
63 void RenderPriorities(CTextRenderer& textRenderer);
64
66 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
67 Renderer::Backend::IVertexInputLayout* vertexInputLayout);
69 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
70 Renderer::Backend::IVertexInputLayout* vertexInputLayout);
71
72 CPatch* GetPatch() { return m_Patch; }
73
75
76 static void RenderBases(
77 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
79 const std::vector<CPatchRData*>& patches, const CShaderDefines& context,
80 ShadowMap* shadow, const CMaterial::Pass materialPass);
81 static void RenderBlends(
82 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
84 const std::vector<CPatchRData*>& patches, const CShaderDefines& context,
85 ShadowMap* shadow, const CMaterial::Pass materialPass);
86 static void RenderStreams(
87 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
89 const std::vector<CPatchRData*>& patches);
90 static void RenderSides(
91 Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
93 const std::vector<CPatchRData*>& patches);
94
95 static void PrepareShader(ShadowMap* shadow);
96
97private:
98 friend struct SBlendStackItem;
99
100 struct SSplat
101 {
103
104 // texture to apply during splat
106 // offset into the index array for this patch where splat starts
108 // number of indices used by splat
110 };
111
113 {
114 // vertex position
117 // pad to a power of two
118 std::uint8_t m_Padding[8];
119 };
120 cassert(sizeof(SBaseVertex) == 32);
121
123 {
124 // vertex position
126 // pad to a power of two
127 std::uint8_t m_Padding[4];
128 };
129 cassert(sizeof(SSideVertex) == 16);
130
132 {
133 // vertex position
135 // vertex uvs for alpha texture
136 float m_AlphaUVs[2];
138 };
139 cassert(sizeof(SBlendVertex) == 32);
140
141 // Mixed Fancy/Simple water vertex description data structure
143 {
144 // vertex position
147 // pad to a power of two
148 std::uint8_t m_Padding[12];
149 };
150 cassert(sizeof(SWaterVertex) == 32);
151
152 // build this renderdata object
153 void Build();
154
155 void AddBlend(std::vector<SBlendVertex>& blendVertices, std::vector<std::uint16_t>& blendIndices,
156 std::uint16_t i, std::uint16_t j, std::uint8_t shape, CTerrainTextureEntry* texture);
157
158 void BuildBlends();
159 void BuildIndices();
160 void BuildVertices();
161 void BuildSides();
162
163 void BuildSide(std::vector<SSideVertex>& vertices, CPatchSideFlags side);
164
165 // owner patch
167
168 // vertex buffer handle for side vertices
170
171 // vertex buffer handle for base vertices
173
174 // vertex buffer handle for base vertex indices
176
177 // vertex buffer handle for blend vertices
179
180 // vertex buffer handle for blend vertex indices
182
183 // list of base splats to apply to this patch
184 std::vector<SSplat> m_Splats;
185
186 // splats used in blend pass
187 std::vector<SSplat> m_BlendSplats;
188
189 // boundary of water in this patch
191
192 // Water vertex buffer
195
196 // Water indices buffer
199
201
202 // Build water vertices and indices (vertex buffer and data vector)
203 void BuildWater();
204
205 // parameter allowing a varying number of triangles per patch for LOD
206 // MUST be an exact divisor of PATCH_SIZE
207 // compiled const for the moment until/if dynamic water LOD is offered
208 // savings would be mostly beneficial for GPU or simple water
209 static const ssize_t water_cell_size = 1;
210};
211
212#endif // INCLUDED_PATCHRDATA
CPatchSideFlags
Definition: Patch.h:40
Definition: BoundingBoxAligned.h:35
Pass
Definition: Material.h:35
Definition: PatchRData.h:46
void BuildSides()
Definition: PatchRData.cpp:776
void BuildVertices()
Definition: PatchRData.cpp:674
static void PrepareShader(ShadowMap *shadow)
CSimulation2 * m_Simulation
Definition: PatchRData.h:200
static Renderer::Backend::IVertexInputLayout * GetSideVertexInputLayout()
Definition: PatchRData.cpp:169
void BuildIndices()
Definition: PatchRData.cpp:576
static Renderer::Backend::IVertexInputLayout * GetBaseVertexInputLayout()
Definition: PatchRData.cpp:92
static Renderer::Backend::IVertexInputLayout * GetWaterShoreVertexInputLayout()
Definition: PatchRData.cpp:214
CPatch * m_Patch
Definition: PatchRData.h:166
static Renderer::Backend::IVertexInputLayout * GetStreamVertexInputLayout(const bool bindPositionAsTexCoord)
Definition: PatchRData.cpp:138
std::vector< SSplat > m_BlendSplats
Definition: PatchRData.h:187
static Renderer::Backend::IVertexInputLayout * GetWaterSurfaceVertexInputLayout(const bool bindWaterData)
Definition: PatchRData.cpp:182
CVertexBufferManager::Handle m_VBWaterIndices
Definition: PatchRData.h:197
CVertexBufferManager::Handle m_VBBase
Definition: PatchRData.h:172
void RenderWaterShore(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, Renderer::Backend::IVertexInputLayout *vertexInputLayout)
Definition: PatchRData.cpp:1646
static const ssize_t water_cell_size
Definition: PatchRData.h:209
const CBoundingBoxAligned & GetWaterBounds() const
Definition: PatchRData.h:74
CVertexBufferManager::Handle m_VBBlendIndices
Definition: PatchRData.h:181
CPatch * GetPatch()
Definition: PatchRData.h:72
void BuildBlends()
Definition: PatchRData.cpp:295
CVertexBufferManager::Handle m_VBWaterIndicesShore
Definition: PatchRData.h:198
void Build()
Definition: PatchRData.cpp:817
void RenderPriorities(CTextRenderer &textRenderer)
Definition: PatchRData.cpp:1388
static void RenderStreams(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, Renderer::Backend::IVertexInputLayout *vertexInputLayout, const std::vector< CPatchRData * > &patches)
Definition: PatchRData.cpp:1265
CVertexBufferManager::Handle m_VBBaseIndices
Definition: PatchRData.h:175
void Update(CSimulation2 *simulation)
Definition: PatchRData.cpp:826
void AddBlend(std::vector< SBlendVertex > &blendVertices, std::vector< std::uint16_t > &blendIndices, std::uint16_t i, std::uint16_t j, std::uint8_t shape, CTerrainTextureEntry *texture)
Definition: PatchRData.cpp:468
CVertexBufferManager::Handle m_VBBlends
Definition: PatchRData.h:178
CBoundingBoxAligned m_WaterBounds
Definition: PatchRData.h:190
static void RenderSides(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, Renderer::Backend::IVertexInputLayout *vertexInputLayout, const std::vector< CPatchRData * > &patches)
Definition: PatchRData.cpp:1354
CPatchRData(CPatch *patch, CSimulation2 *simulation)
Definition: PatchRData.cpp:230
cassert(sizeof(SSideVertex)==16)
cassert(sizeof(SWaterVertex)==32)
CVertexBufferManager::Handle m_VBSides
Definition: PatchRData.h:169
void RenderWaterSurface(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, Renderer::Backend::IVertexInputLayout *vertexInputLayout)
Definition: PatchRData.cpp:1619
void BuildWater()
Definition: PatchRData.cpp:1420
static void RenderBlends(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, Renderer::Backend::IVertexInputLayout *vertexInputLayout, const std::vector< CPatchRData * > &patches, const CShaderDefines &context, ShadowMap *shadow, const CMaterial::Pass materialPass)
Definition: PatchRData.cpp:1051
static Renderer::Backend::IVertexInputLayout * GetBlendVertexInputLayout()
Definition: PatchRData.cpp:113
std::vector< SSplat > m_Splats
Definition: PatchRData.h:184
void RenderOutline(Renderer::Backend::IDeviceCommandContext &deviceCommandContext)
Definition: PatchRData.cpp:1322
cassert(sizeof(SBaseVertex)==32)
void BuildSide(std::vector< SSideVertex > &vertices, CPatchSideFlags side)
Definition: PatchRData.cpp:723
static void RenderBases(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, Renderer::Backend::IVertexInputLayout *vertexInputLayout, const std::vector< CPatchRData * > &patches, const CShaderDefines &context, ShadowMap *shadow, const CMaterial::Pass materialPass)
Definition: PatchRData.cpp:888
CVertexBufferManager::Handle m_VBWater
Definition: PatchRData.h:193
CVertexBufferManager::Handle m_VBWaterShore
Definition: PatchRData.h:194
cassert(sizeof(SBlendVertex)==32)
Definition: Patch.h:50
Definition: RenderableObject.h:41
Represents a mapping of name strings to value strings, for use with #if and #ifdef and similar condit...
Definition: ShaderDefines.h:153
Public API for simulation system.
Definition: Simulation2.h:57
Definition: TerrainTextureEntry.h:35
Definition: TextRenderer.h:38
Definition: Vector2D.h:32
Definition: Vector3D.h:32
Definition: VertexBufferManager.h:53
Definition: IDeviceCommandContext.h:46
IVertexInputLayout stores precompiled list of vertex attributes.
Definition: IShaderProgram.h:78
Class ShadowMap: Maintain the shadow map texture and perform necessary OpenGL setup,...
Definition: ShadowMap.h:38
Definition: Canvas2D.h:30
Definition: PatchRData.h:113
std::uint8_t m_Padding[8]
Definition: PatchRData.h:118
CVector3D m_Normal
Definition: PatchRData.h:116
CVector3D m_Position
Definition: PatchRData.h:115
Definition: PatchRData.h:132
CVector3D m_Position
Definition: PatchRData.h:134
float m_AlphaUVs[2]
Definition: PatchRData.h:136
CVector3D m_Normal
Definition: PatchRData.h:137
Definition: PatchRData.h:123
std::uint8_t m_Padding[4]
Definition: PatchRData.h:127
CVector3D m_Position
Definition: PatchRData.h:125
Definition: PatchRData.h:101
SSplat()
Definition: PatchRData.h:102
size_t m_IndexCount
Definition: PatchRData.h:109
CTerrainTextureEntry * m_Texture
Definition: PatchRData.h:105
size_t m_IndexStart
Definition: PatchRData.h:107
Definition: PatchRData.h:143
CVector3D m_Position
Definition: PatchRData.h:145
std::uint8_t m_Padding[12]
Definition: PatchRData.h:148
CVector2D m_WaterData
Definition: PatchRData.h:146
Helper structure for RenderBlends.
Definition: PatchRData.cpp:1038
intptr_t ssize_t
Definition: wposix_types.h:36