Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
LOSTexture.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_LOSTEXTURE
19#define INCLUDED_LOSTEXTURE
20
22#include "lib/code_annotation.h"
23#include "maths/Matrix3D.h"
25#include <cstddef>
26#include <cstdint>
27#include <memory>
28
29class CLosQuerier;
30class CSimulation2;
31namespace Renderer::Backend { class IDeviceCommandContext; }
32namespace Renderer::Backend { class IFramebuffer; }
33namespace Renderer::Backend { class ITexture; }
34namespace Renderer::Backend { class IVertexInputLayout; }
35
36/**
37 * Maintains the LOS (fog-of-war / shroud-of-darkness) texture, used for
38 * rendering and for the minimap.
39 */
41{
43 friend class TestLOSTexture;
44
45public:
46 CLOSTexture(CSimulation2& simulation);
48
49 /**
50 * Marks the LOS texture as needing recomputation. Call this after each
51 * simulation update, to ensure responsive updates.
52 */
53 void MakeDirty();
54
55 /**
56 * Recomputes the LOS texture if necessary, and returns the texture handle.
57 * Also potentially switches the current active texture unit, and enables texturing on it.
58 * The texture is in 8-bit ALPHA format.
59 */
62
64
65 /**
66 * Returns a matrix to map (x,y,z) world coordinates onto (u,v) LOS texture
67 * coordinates, in the form expected by a matrix uniform.
68 * This must only be called after InterpolateLOS.
69 */
71
72 /**
73 * Returns a matrix to map (0,0)-(1,1) texture coordinates onto LOS texture
74 * coordinates, in the form expected by a matrix uniform.
75 * This must only be called after InterpolateLOS.
76 */
78
79private:
80 void DeleteTexture();
82 void RecomputeTexture(Renderer::Backend::IDeviceCommandContext* deviceCommandContext, bool shouldUpdateSmoothTextures);
83
84 size_t GetBitmapSize(size_t w, size_t h, size_t* pitch);
85 void GenerateBitmap(const CLosQuerier& los, std::uint8_t* losData, size_t w, size_t h, size_t pitch);
86
88
89 bool m_Dirty = true;
90 // Whether smooth LOS was enabled last frame and m_SmoothTextures are therefore up-to-date.
92
93 // We need to choose the smallest format. We always use the red channel but
94 // R8_UNORM might be unavailable on some platforms. So we fallback to
95 // R8G8B8A8_UNORM.
99 std::unique_ptr<Renderer::Backend::ITexture>
102
103 uint32_t m_WhichTexture = 0;
105
106 // We update textures once a frame, so we change a Framebuffer once a frame.
107 // That allows us to use two ping-pong FBOs instead of checking completeness
108 // of Framebuffer each frame.
109 std::unique_ptr<Renderer::Backend::IFramebuffer>
112
113 size_t m_MapSize = 0; // vertexes per side
114
117};
118
119#endif // INCLUDED_LOSTEXTURE
std::shared_ptr< CShaderTechnique > CShaderTechniquePtr
Definition: ShaderTechniquePtr.h:30
Maintains the LOS (fog-of-war / shroud-of-darkness) texture, used for rendering and for the minimap.
Definition: LOSTexture.h:41
uint32_t m_WhichTexture
Definition: LOSTexture.h:103
const CMatrix3D & GetTextureMatrix()
Returns a matrix to map (x,y,z) world coordinates onto (u,v) LOS texture coordinates,...
Definition: LOSTexture.cpp:222
void ConstructTexture(Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
Definition: LOSTexture.cpp:234
CMatrix3D m_TextureMatrix
Definition: LOSTexture.h:115
size_t m_TextureFormatStride
Definition: LOSTexture.h:98
size_t GetBitmapSize(size_t w, size_t h, size_t *pitch)
Definition: LOSTexture.cpp:400
std::unique_ptr< Renderer::Backend::IFramebuffer > m_SmoothFramebuffers[2]
Definition: LOSTexture.h:110
~CLOSTexture()
Definition: LOSTexture.cpp:104
void DeleteTexture()
Definition: LOSTexture.cpp:113
void InterpolateLOS(Renderer::Backend::IDeviceCommandContext *deviceCommandContext)
Definition: LOSTexture.cpp:133
void RecomputeTexture(Renderer::Backend::IDeviceCommandContext *deviceCommandContext, bool shouldUpdateSmoothTextures)
Definition: LOSTexture.cpp:336
CMatrix3D m_MinimapTextureMatrix
Definition: LOSTexture.h:116
const CMatrix3D & GetMinimapTextureMatrix()
Returns a matrix to map (0,0)-(1,1) texture coordinates onto LOS texture coordinates,...
Definition: LOSTexture.cpp:228
double m_LastTextureRecomputeTime
Definition: LOSTexture.h:104
std::unique_ptr< Renderer::Backend::ITexture > m_SmoothTextures[2]
Definition: LOSTexture.h:100
bool m_Dirty
Definition: LOSTexture.h:89
NONCOPYABLE(CLOSTexture)
void MakeDirty()
Marks the LOS texture as needing recomputation.
Definition: LOSTexture.cpp:120
size_t m_MapSize
Definition: LOSTexture.h:113
bool m_WasSmoothLOSEnabled
Definition: LOSTexture.h:91
CShaderTechniquePtr m_SmoothTech
Definition: LOSTexture.h:111
Renderer::Backend::ITexture * GetTextureSmooth()
Definition: LOSTexture.cpp:125
std::unique_ptr< Renderer::Backend::ITexture > m_Texture
Definition: LOSTexture.h:100
Renderer::Backend::Format m_TextureFormat
Definition: LOSTexture.h:96
Renderer::Backend::IVertexInputLayout * m_VertexInputLayout
Definition: LOSTexture.h:101
friend class TestLOSTexture
Definition: LOSTexture.h:43
CLOSTexture(CSimulation2 &simulation)
Definition: LOSTexture.cpp:80
Renderer::Backend::ITexture * GetTexture()
Recomputes the LOS texture if necessary, and returns the texture handle.
Definition: LOSTexture.cpp:216
void GenerateBitmap(const CLosQuerier &los, std::uint8_t *losData, size_t w, size_t h, size_t pitch)
Definition: LOSTexture.cpp:406
CSimulation2 & m_Simulation
Definition: LOSTexture.h:87
Object providing efficient abstracted access to the LOS state.
Definition: Los.h:48
Definition: Matrix3D.h:36
Public API for simulation system.
Definition: Simulation2.h:57
Definition: IDeviceCommandContext.h:46
Definition: ITexture.h:33
IVertexInputLayout stores precompiled list of vertex attributes.
Definition: IShaderProgram.h:78
Definition: Canvas2D.h:30
Format
Definition: Format.h:28