Line data Source code
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" 23 : #include "graphics/RenderableObject.h" 24 : #include "lib/code_annotation.h" 25 : #include "lib/posix/posix_types.h" 26 : #include "maths/BoundingBoxAligned.h" 27 : #include "maths/Vector2D.h" 28 : #include "maths/Vector3D.h" 29 : #include "renderer/VertexBufferManager.h" 30 : 31 : #include <cstddef> 32 : #include <vector> 33 : 34 : class CShaderDefines; 35 : class CSimulation2; 36 : class CTerrainTextureEntry; 37 : class CTextRenderer; 38 : class ShadowMap; 39 : namespace Renderer::Backend { class IDeviceCommandContext; } 40 : namespace 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 45 : class CPatchRData : public CRenderData 46 : { 47 : public: 48 : CPatchRData(CPatch* patch, CSimulation2* simulation); 49 : ~CPatchRData(); 50 : 51 : static Renderer::Backend::IVertexInputLayout* GetBaseVertexInputLayout(); 52 : static Renderer::Backend::IVertexInputLayout* GetBlendVertexInputLayout(); 53 : static Renderer::Backend::IVertexInputLayout* GetStreamVertexInputLayout( 54 : const bool bindPositionAsTexCoord); 55 : static Renderer::Backend::IVertexInputLayout* GetSideVertexInputLayout(); 56 : 57 : static Renderer::Backend::IVertexInputLayout* GetWaterSurfaceVertexInputLayout( 58 : const bool bindWaterData); 59 : static Renderer::Backend::IVertexInputLayout* GetWaterShoreVertexInputLayout(); 60 : 61 : void Update(CSimulation2* simulation); 62 : void RenderOutline(Renderer::Backend::IDeviceCommandContext& deviceCommandContext); 63 : void RenderPriorities(CTextRenderer& textRenderer); 64 : 65 : void RenderWaterSurface( 66 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext, 67 : Renderer::Backend::IVertexInputLayout* vertexInputLayout); 68 : void RenderWaterShore( 69 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext, 70 : Renderer::Backend::IVertexInputLayout* vertexInputLayout); 71 : 72 : CPatch* GetPatch() { return m_Patch; } 73 : 74 0 : const CBoundingBoxAligned& GetWaterBounds() const { return m_WaterBounds; } 75 : 76 : static void RenderBases( 77 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext, 78 : Renderer::Backend::IVertexInputLayout* vertexInputLayout, 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, 83 : Renderer::Backend::IVertexInputLayout* vertexInputLayout, 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, 88 : Renderer::Backend::IVertexInputLayout* vertexInputLayout, 89 : const std::vector<CPatchRData*>& patches); 90 : static void RenderSides( 91 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext, 92 : Renderer::Backend::IVertexInputLayout* vertexInputLayout, 93 : const std::vector<CPatchRData*>& patches); 94 : 95 : static void PrepareShader(ShadowMap* shadow); 96 : 97 : private: 98 : friend struct SBlendStackItem; 99 : 100 : struct SSplat 101 : { 102 0 : SSplat() : m_Texture(0), m_IndexCount(0) {} 103 : 104 : // texture to apply during splat 105 : CTerrainTextureEntry* m_Texture; 106 : // offset into the index array for this patch where splat starts 107 : size_t m_IndexStart; 108 : // number of indices used by splat 109 : size_t m_IndexCount; 110 : }; 111 : 112 0 : struct SBaseVertex 113 : { 114 : // vertex position 115 : CVector3D m_Position; 116 : CVector3D m_Normal; 117 : // pad to a power of two 118 : std::uint8_t m_Padding[8]; 119 : }; 120 : cassert(sizeof(SBaseVertex) == 32); 121 : 122 0 : struct SSideVertex 123 : { 124 : // vertex position 125 : CVector3D m_Position; 126 : // pad to a power of two 127 : std::uint8_t m_Padding[4]; 128 : }; 129 : cassert(sizeof(SSideVertex) == 16); 130 : 131 0 : struct SBlendVertex 132 : { 133 : // vertex position 134 : CVector3D m_Position; 135 : // vertex uvs for alpha texture 136 : float m_AlphaUVs[2]; 137 : CVector3D m_Normal; 138 : }; 139 : cassert(sizeof(SBlendVertex) == 32); 140 : 141 : // Mixed Fancy/Simple water vertex description data structure 142 0 : struct SWaterVertex 143 : { 144 : // vertex position 145 : CVector3D m_Position; 146 : CVector2D m_WaterData; 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 166 : CPatch* m_Patch; 167 : 168 : // vertex buffer handle for side vertices 169 : CVertexBufferManager::Handle m_VBSides; 170 : 171 : // vertex buffer handle for base vertices 172 : CVertexBufferManager::Handle m_VBBase; 173 : 174 : // vertex buffer handle for base vertex indices 175 : CVertexBufferManager::Handle m_VBBaseIndices; 176 : 177 : // vertex buffer handle for blend vertices 178 : CVertexBufferManager::Handle m_VBBlends; 179 : 180 : // vertex buffer handle for blend vertex indices 181 : CVertexBufferManager::Handle m_VBBlendIndices; 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 190 : CBoundingBoxAligned m_WaterBounds; 191 : 192 : // Water vertex buffer 193 : CVertexBufferManager::Handle m_VBWater; 194 : CVertexBufferManager::Handle m_VBWaterShore; 195 : 196 : // Water indices buffer 197 : CVertexBufferManager::Handle m_VBWaterIndices; 198 : CVertexBufferManager::Handle m_VBWaterIndicesShore; 199 : 200 : CSimulation2* m_Simulation; 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