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 : #include "precompiled.h"
19 :
20 : #include "TerritoryTexture.h"
21 :
22 : #include "graphics/Color.h"
23 : #include "graphics/Terrain.h"
24 : #include "lib/debug.h"
25 : #include "ps/Profile.h"
26 : #include "renderer/backend/Format.h"
27 : #include "renderer/backend/IDevice.h"
28 : #include "renderer/backend/IDeviceCommandContext.h"
29 : #include "renderer/backend/ITexture.h"
30 : #include "renderer/backend/Sampler.h"
31 : #include "simulation2/components/ICmpPlayer.h"
32 : #include "simulation2/components/ICmpPlayerManager.h"
33 : #include "simulation2/components/ICmpTerrain.h"
34 : #include "simulation2/components/ICmpTerritoryManager.h"
35 : #include "simulation2/helpers/Grid.h"
36 : #include "simulation2/helpers/Pathfinding.h"
37 : #include "simulation2/system/CmpPtr.h"
38 : #include "simulation2/system/Entity.h"
39 :
40 : #include <algorithm>
41 : #include <bit>
42 : #include <cstdint>
43 : #include <cstring>
44 : #include <vector>
45 :
46 : // TODO: There's a lot of duplication with CLOSTexture - might be nice to refactor a bit
47 :
48 0 : CTerritoryTexture::CTerritoryTexture(CSimulation2& simulation) :
49 0 : m_Simulation(simulation), m_DirtyID(0), m_MapSize(0)
50 : {
51 0 : }
52 :
53 0 : CTerritoryTexture::~CTerritoryTexture()
54 : {
55 0 : DeleteTexture();
56 0 : }
57 :
58 0 : void CTerritoryTexture::DeleteTexture()
59 : {
60 0 : m_Texture.reset();
61 0 : }
62 :
63 0 : bool CTerritoryTexture::UpdateDirty()
64 : {
65 0 : CmpPtr<ICmpTerritoryManager> cmpTerritoryManager(m_Simulation, SYSTEM_ENTITY);
66 0 : return cmpTerritoryManager && cmpTerritoryManager->NeedUpdateTexture(&m_DirtyID);
67 : }
68 :
69 0 : Renderer::Backend::ITexture* CTerritoryTexture::GetTexture()
70 : {
71 0 : ENSURE(!UpdateDirty());
72 0 : return m_Texture.get();
73 : }
74 :
75 0 : const CMatrix3D& CTerritoryTexture::GetTextureMatrix()
76 : {
77 0 : ENSURE(!UpdateDirty());
78 0 : return m_TextureMatrix;
79 : }
80 :
81 0 : const CMatrix3D& CTerritoryTexture::GetMinimapTextureMatrix()
82 : {
83 0 : ENSURE(!UpdateDirty());
84 0 : return m_MinimapTextureMatrix;
85 : }
86 :
87 0 : void CTerritoryTexture::ConstructTexture(Renderer::Backend::IDeviceCommandContext* deviceCommandContext)
88 : {
89 0 : CmpPtr<ICmpTerrain> cmpTerrain(m_Simulation, SYSTEM_ENTITY);
90 0 : if (!cmpTerrain)
91 0 : return;
92 :
93 : // Convert size from terrain tiles to territory tiles
94 0 : m_MapSize = cmpTerrain->GetMapSize() * Pathfinding::NAVCELL_SIZE_INT / ICmpTerritoryManager::NAVCELLS_PER_TERRITORY_TILE;
95 :
96 0 : const uint32_t textureSize = std::bit_ceil(static_cast<uint32_t>(m_MapSize));
97 :
98 0 : m_Texture = deviceCommandContext->GetDevice()->CreateTexture2D("TerritoryTexture",
99 : Renderer::Backend::ITexture::Usage::TRANSFER_DST |
100 : Renderer::Backend::ITexture::Usage::SAMPLED,
101 : Renderer::Backend::Format::R8G8B8A8_UNORM, textureSize, textureSize,
102 0 : Renderer::Backend::Sampler::MakeDefaultSampler(
103 : Renderer::Backend::Sampler::Filter::LINEAR,
104 0 : Renderer::Backend::Sampler::AddressMode::CLAMP_TO_EDGE));
105 :
106 : // Initialise texture with transparency, for the areas we don't
107 : // overwrite with uploading later.
108 0 : std::unique_ptr<std::uint8_t[]> texData = std::make_unique<std::uint8_t[]>(textureSize *
109 0 : textureSize * 4);
110 0 : memset(texData.get(), 0x00, textureSize * textureSize * 4);
111 0 : deviceCommandContext->UploadTexture(
112 : m_Texture.get(), Renderer::Backend::Format::R8G8B8A8_UNORM,
113 0 : texData.get(), textureSize * textureSize * 4);
114 0 : texData.reset();
115 :
116 0 : {
117 : // Texture matrix: We want to map
118 : // world pos (0, y, 0) (i.e. bottom-left of first tile)
119 : // onto texcoord (0, 0) (i.e. bottom-left of first texel);
120 : // world pos (mapsize*cellsize, y, mapsize*cellsize) (i.e. top-right of last tile)
121 : // onto texcoord (mapsize / texsize, mapsize / texsize) (i.e. top-right of last texel)
122 :
123 0 : float s = 1.f / static_cast<float>(textureSize * TERRAIN_TILE_SIZE);
124 0 : float t = 0.f;
125 0 : m_TextureMatrix.SetZero();
126 0 : m_TextureMatrix._11 = s;
127 0 : m_TextureMatrix._23 = s;
128 0 : m_TextureMatrix._14 = t;
129 0 : m_TextureMatrix._24 = t;
130 0 : m_TextureMatrix._44 = 1;
131 : }
132 :
133 0 : {
134 : // Minimap matrix: We want to map UV (0,0)-(1,1) onto (0,0)-(mapsize/texsize, mapsize/texsize)
135 :
136 0 : float s = m_MapSize / static_cast<float>(textureSize);
137 0 : m_MinimapTextureMatrix.SetZero();
138 0 : m_MinimapTextureMatrix._11 = s;
139 0 : m_MinimapTextureMatrix._22 = s;
140 0 : m_MinimapTextureMatrix._44 = 1;
141 : }
142 0 : }
143 :
144 0 : void CTerritoryTexture::RecomputeTexture(Renderer::Backend::IDeviceCommandContext* deviceCommandContext)
145 : {
146 : // If the map was resized, delete and regenerate the texture
147 0 : if (m_Texture)
148 : {
149 0 : CmpPtr<ICmpTerrain> cmpTerrain(m_Simulation, SYSTEM_ENTITY);
150 0 : if (cmpTerrain && m_MapSize != (ssize_t)cmpTerrain->GetVerticesPerSide())
151 0 : DeleteTexture();
152 : }
153 :
154 0 : if (!m_Texture)
155 0 : ConstructTexture(deviceCommandContext);
156 :
157 0 : PROFILE("recompute territory texture");
158 :
159 0 : CmpPtr<ICmpTerritoryManager> cmpTerritoryManager(m_Simulation, SYSTEM_ENTITY);
160 0 : if (!cmpTerritoryManager || !cmpTerritoryManager->IsVisible())
161 0 : return;
162 :
163 0 : std::unique_ptr<std::uint8_t[]> bitmap = std::make_unique<std::uint8_t[]>(m_MapSize * m_MapSize * 4);
164 0 : GenerateBitmap(cmpTerritoryManager->GetTerritoryGrid(), bitmap.get(), m_MapSize, m_MapSize);
165 :
166 0 : deviceCommandContext->UploadTextureRegion(
167 0 : m_Texture.get(), Renderer::Backend::Format::R8G8B8A8_UNORM, bitmap.get(), m_MapSize * m_MapSize * 4,
168 0 : 0, 0, m_MapSize, m_MapSize);
169 0 : }
170 :
171 0 : void CTerritoryTexture::GenerateBitmap(const Grid<std::uint8_t>& territories, std::uint8_t* bitmap,
172 : ssize_t w, ssize_t h)
173 : {
174 0 : int alphaMax = 0xC0;
175 0 : int alphaFalloff = 0x20;
176 :
177 0 : CmpPtr<ICmpPlayerManager> cmpPlayerManager(m_Simulation, SYSTEM_ENTITY);
178 :
179 0 : std::vector<CColor> colors;
180 0 : std::int32_t numPlayers = cmpPlayerManager->GetNumPlayers();
181 0 : for (std::int32_t p = 0; p < numPlayers; ++p)
182 : {
183 0 : CColor color(1, 0, 1, 1);
184 0 : CmpPtr<ICmpPlayer> cmpPlayer(m_Simulation, cmpPlayerManager->GetPlayerByID(p));
185 0 : if (cmpPlayer)
186 0 : color = cmpPlayer->GetDisplayedColor();
187 0 : colors.push_back(color);
188 : }
189 :
190 : std::uint8_t* p = bitmap;
191 0 : for (ssize_t j = 0; j < h; ++j)
192 0 : for (ssize_t i = 0; i < w; ++i)
193 : {
194 0 : std::uint8_t val = territories.get(i, j) & ICmpTerritoryManager::TERRITORY_PLAYER_MASK;
195 :
196 0 : CColor color(1, 0, 1, 1);
197 0 : if (val < colors.size())
198 0 : color = colors[val];
199 :
200 0 : *p++ = (int)(color.r * 255.f);
201 0 : *p++ = (int)(color.g * 255.f);
202 0 : *p++ = (int)(color.b * 255.f);
203 :
204 : // Use alphaMax for borders and gaia territory; these tiles will be deleted later
205 0 : if (val == 0 ||
206 0 : (i > 0 && (territories.get(i-1, j) & ICmpTerritoryManager::TERRITORY_PLAYER_MASK) != val) ||
207 0 : (i < w-1 && (territories.get(i+1, j) & ICmpTerritoryManager::TERRITORY_PLAYER_MASK) != val) ||
208 0 : (j > 0 && (territories.get(i, j-1) & ICmpTerritoryManager::TERRITORY_PLAYER_MASK) != val) ||
209 0 : (j < h-1 && (territories.get(i, j+1) & ICmpTerritoryManager::TERRITORY_PLAYER_MASK) != val))
210 0 : *p++ = alphaMax;
211 : else
212 0 : *p++ = 0x00;
213 : }
214 :
215 : // Do a low-quality cheap blur effect
216 :
217 0 : for (ssize_t j = 0; j < h; ++j)
218 : {
219 : int a;
220 :
221 : a = 0;
222 0 : for (ssize_t i = 0; i < w; ++i)
223 : {
224 0 : a = std::max(a - alphaFalloff, (int)bitmap[(j*w+i)*4 + 3]);
225 0 : bitmap[(j*w+i)*4 + 3] = a;
226 : }
227 :
228 0 : a = 0;
229 0 : for (ssize_t i = w-1; i >= 0; --i)
230 : {
231 0 : a = std::max(a - alphaFalloff, (int)bitmap[(j*w+i)*4 + 3]);
232 0 : bitmap[(j*w+i)*4 + 3] = a;
233 : }
234 : }
235 :
236 0 : for (ssize_t i = 0; i < w; ++i)
237 : {
238 : int a;
239 :
240 : a = 0;
241 0 : for (ssize_t j = 0; j < w; ++j)
242 : {
243 0 : a = std::max(a - alphaFalloff, (int)bitmap[(j*w+i)*4 + 3]);
244 0 : bitmap[(j*w+i)*4 + 3] = a;
245 : }
246 :
247 0 : a = 0;
248 0 : for (ssize_t j = w-1; j >= 0; --j)
249 : {
250 0 : a = std::max(a - alphaFalloff, (int)bitmap[(j*w+i)*4 + 3]);
251 0 : bitmap[(j*w+i)*4 + 3] = a;
252 : }
253 : }
254 :
255 : // Add a gap between the boundaries, by deleting the max-alpha tiles
256 0 : for (ssize_t j = 0; j < h; ++j)
257 0 : for (ssize_t i = 0; i < w; ++i)
258 0 : if (bitmap[(j*w+i)*4 + 3] == alphaMax)
259 0 : bitmap[(j*w+i)*4 + 3] = 0;
260 0 : }
261 :
262 0 : void CTerritoryTexture::UpdateIfNeeded(Renderer::Backend::IDeviceCommandContext* deviceCommandContext)
263 : {
264 0 : if (UpdateDirty())
265 0 : RecomputeTexture(deviceCommandContext);
266 0 : }
|