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