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 "SkyManager.h"
21 :
22 : #include "graphics/Camera.h"
23 : #include "graphics/LightEnv.h"
24 : #include "graphics/ShaderManager.h"
25 : #include "graphics/ShaderTechnique.h"
26 : #include "graphics/ShaderTechniquePtr.h"
27 : #include "graphics/TextureManager.h"
28 : #include "lib/code_generation.h"
29 : #include "lib/file/file_system.h"
30 : #include "lib/file/vfs/vfs.h"
31 : #include "lib/file/vfs/vfs_path.h"
32 : #include "lib/path.h"
33 : #include "lib/status.h"
34 : #include "lib/tex/tex.h"
35 : #include "maths/Matrix3D.h"
36 : #include "maths/Vector3D.h"
37 : #include "ps/CLogger.h"
38 : #include "ps/CStr.h"
39 : #include "ps/CStrIntern.h"
40 : #include "ps/CStrInternStatic.h"
41 : #include "ps/ConfigDB.h"
42 : #include "ps/Filesystem.h"
43 : #include "renderer/Renderer.h"
44 : #include "renderer/SceneRenderer.h"
45 : #include "renderer/backend/Format.h"
46 : #include "renderer/backend/IBuffer.h"
47 : #include "renderer/backend/IDevice.h"
48 : #include "renderer/backend/IDeviceCommandContext.h"
49 : #include "renderer/backend/IShaderProgram.h"
50 : #include "renderer/backend/ITexture.h"
51 : #include "renderer/backend/Sampler.h"
52 :
53 : #include <algorithm>
54 : #include <array>
55 : #include <bit>
56 : #include <cmath>
57 : #include <cstddef>
58 : #include <cstdint>
59 : #include <memory>
60 : #include <numbers>
61 : #include <string>
62 : #include <utility>
63 :
64 10 : SkyManager::SkyManager() :
65 10 : m_VertexArray{Renderer::Backend::IBuffer::Type::VERTEX,
66 : Renderer::Backend::IBuffer::Usage::TRANSFER_DST},
67 10 : m_SkyVisible{g_ConfigDB.Get("showsky", true)}
68 : {
69 10 : }
70 :
71 0 : void SkyManager::LoadAndUploadSkyTexturesIfNeeded(
72 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext)
73 : {
74 0 : if (m_VertexArray.GetNumberOfVertices() == 0)
75 0 : CreateSkyCube();
76 :
77 0 : if (m_SkyTextureCube)
78 0 : return;
79 :
80 0 : m_SkyTextureCube = g_Renderer.GetTextureManager().GetBlackTextureCube();
81 :
82 0 : GPU_SCOPED_LABEL(deviceCommandContext, "Load Sky Textures");
83 0 : static const CStrW images[NUMBER_OF_TEXTURES + 1] = {
84 : L"front",
85 : L"back",
86 : L"top",
87 : L"top",
88 : L"right",
89 : L"left"
90 0 : };
91 :
92 : /*for (size_t i = 0; i < ARRAY_SIZE(m_SkyTexture); ++i)
93 : {
94 : VfsPath path = VfsPath("art/textures/skies") / m_SkySet / (Path::String(s_imageNames[i])+L".dds");
95 :
96 : CTextureProperties textureProps(path);
97 : textureProps.SetWrap(GL_CLAMP_TO_EDGE);
98 : CTexturePtr texture = g_Renderer.GetTextureManager().CreateTexture(textureProps);
99 : texture->Prefetch();
100 : m_SkyTexture[i] = texture;
101 : }*/
102 :
103 : ///////////////////////////////////////////////////////////////////////////
104 : // HACK: THE HORRIBLENESS HERE IS OVER 9000. The following code is a HUGE hack and will be removed completely
105 : // as soon as all the hardcoded GL_TEXTURE_2D references are corrected in the TextureManager/OGL/tex libs.
106 :
107 0 : Tex textures[NUMBER_OF_TEXTURES + 1];
108 :
109 0 : for (size_t i = 0; i < NUMBER_OF_TEXTURES + 1; ++i)
110 : {
111 0 : VfsPath path = VfsPath("art/textures/skies") / m_SkySet / (Path::String(images[i]) + L".dds");
112 :
113 0 : std::shared_ptr<std::uint8_t> file;
114 0 : size_t fileSize;
115 0 : if (g_VFS->LoadFile(path, file, fileSize) != INFO::OK)
116 : {
117 0 : path = VfsPath("art/textures/skies") / m_SkySet / (Path::String(images[i]) + L".dds.cached.dds");
118 0 : if (g_VFS->LoadFile(path, file, fileSize) != INFO::OK)
119 : {
120 0 : LOGERROR("Error creating sky cubemap '{}', can't load file: '{}'.",
121 : m_SkySet.ToUTF8(), path.string8());
122 0 : return;
123 : }
124 : }
125 :
126 0 : if (textures[i].decode(file, fileSize) != INFO::OK ||
127 0 : textures[i].transform_to((textures[i].m_Flags | TEX_BOTTOM_UP | TEX_ALPHA) & ~(TEX_DXT | TEX_MIPMAPS)) != INFO::OK)
128 : {
129 0 : LOGERROR("Error creating sky cubemap '{}', can't decode file: '{}'.", m_SkySet.ToUTF8(),
130 : path.string8());
131 0 : return;
132 : }
133 :
134 0 : if (!std::has_single_bit(textures[i].m_Width) || !std::has_single_bit(textures[i].m_Height))
135 : {
136 0 : LOGERROR("Error creating sky cubemap '{}', cube textures should have power of 2 sizes.",
137 : m_SkySet.ToUTF8());
138 0 : return;
139 : }
140 :
141 0 : if (textures[i].m_Width != textures[0].m_Width || textures[i].m_Height != textures[0].m_Height)
142 : {
143 0 : LOGERROR("Error creating sky cubemap '{}', cube textures have different sizes.",
144 : m_SkySet.ToUTF8());
145 0 : return;
146 : }
147 0 : }
148 :
149 0 : std::unique_ptr<Renderer::Backend::ITexture> skyCubeMap =
150 0 : deviceCommandContext->GetDevice()->CreateTexture("SkyCubeMap",
151 : Renderer::Backend::ITexture::Type::TEXTURE_CUBE,
152 : Renderer::Backend::ITexture::Usage::TRANSFER_DST |
153 : Renderer::Backend::ITexture::Usage::SAMPLED,
154 0 : Renderer::Backend::Format::R8G8B8A8_UNORM, textures[0].m_Width, textures[0].m_Height,
155 0 : Renderer::Backend::Sampler::MakeDefaultSampler(
156 : Renderer::Backend::Sampler::Filter::LINEAR,
157 0 : Renderer::Backend::Sampler::AddressMode::CLAMP_TO_EDGE), 1, 1);
158 :
159 0 : std::vector<std::uint8_t> rotated;
160 0 : for (size_t i = 0; i < NUMBER_OF_TEXTURES + 1; ++i)
161 : {
162 0 : std::uint8_t* data = textures[i].get_data();
163 :
164 : // We need to rotate the side if it's looking up or down.
165 : // TODO: maybe it should be done during texture conversion.
166 0 : if (i == 2 || i == 3)
167 : {
168 0 : rotated.resize(textures[i].m_DataSize);
169 :
170 0 : for (size_t y = 0; y < textures[i].m_Height; ++y)
171 : {
172 0 : for (size_t x = 0; x < textures[i].m_Width; ++x)
173 : {
174 0 : const size_t invX = y;
175 0 : const size_t invY = textures[i].m_Width - x - 1;
176 :
177 0 : rotated[(y * textures[i].m_Width + x) * 4 + 0] = data[(invY * textures[i].m_Width + invX) * 4 + 0];
178 0 : rotated[(y * textures[i].m_Width + x) * 4 + 1] = data[(invY * textures[i].m_Width + invX) * 4 + 1];
179 0 : rotated[(y * textures[i].m_Width + x) * 4 + 2] = data[(invY * textures[i].m_Width + invX) * 4 + 2];
180 0 : rotated[(y * textures[i].m_Width + x) * 4 + 3] = data[(invY * textures[i].m_Width + invX) * 4 + 3];
181 : }
182 : }
183 :
184 0 : deviceCommandContext->UploadTexture(
185 : skyCubeMap.get(), Renderer::Backend::Format::R8G8B8A8_UNORM,
186 0 : &rotated[0], textures[i].m_DataSize, 0, i);
187 : }
188 : else
189 : {
190 0 : deviceCommandContext->UploadTexture(
191 : skyCubeMap.get(), Renderer::Backend::Format::R8G8B8A8_UNORM,
192 : data, textures[i].m_DataSize, 0, i);
193 : }
194 : }
195 :
196 0 : m_SkyTextureCube = g_Renderer.GetTextureManager().WrapBackendTexture(std::move(skyCubeMap));
197 : ///////////////////////////////////////////////////////////////////////////
198 0 : }
199 :
200 0 : Renderer::Backend::ITexture* SkyManager::GetSkyCube()
201 : {
202 0 : return m_SkyTextureCube->GetBackendTexture();
203 : }
204 :
205 0 : void SkyManager::SetSkySet(const CStrW& newSet)
206 : {
207 0 : if (newSet == m_SkySet)
208 : return;
209 :
210 0 : m_SkyTextureCube.reset();
211 :
212 0 : m_SkySet = newSet;
213 : }
214 :
215 0 : std::vector<CStrW> SkyManager::GetSkySets() const
216 : {
217 0 : std::vector<CStrW> skies;
218 :
219 : // Find all subdirectories in art/textures/skies
220 :
221 0 : const VfsPath path(L"art/textures/skies/");
222 0 : DirectoryNames subdirectories;
223 0 : if (g_VFS->GetDirectoryEntries(path, 0, &subdirectories) != INFO::OK)
224 : {
225 0 : LOGERROR("Error opening directory '{}'", path.string8());
226 0 : return std::vector<CStrW>(1, GetSkySet()); // just return what we currently have
227 : }
228 :
229 0 : for(size_t i = 0; i < subdirectories.size(); i++)
230 0 : skies.push_back(subdirectories[i].string());
231 0 : sort(skies.begin(), skies.end());
232 :
233 0 : return skies;
234 0 : }
235 :
236 0 : void SkyManager::RenderSky(
237 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext)
238 : {
239 0 : GPU_SCOPED_LABEL(deviceCommandContext, "Render sky");
240 :
241 0 : const CTexturePtr& skyTextureCube =
242 0 : !m_SkyVisible || m_SkySet.empty() || !m_SkyTextureCube
243 0 : ? g_Renderer.GetTextureManager().GetBlackTextureCube()
244 0 : : m_SkyTextureCube;
245 :
246 0 : const CCamera& camera = g_Renderer.GetSceneRenderer().GetViewCamera();
247 :
248 0 : CShaderTechniquePtr skytech =
249 0 : g_Renderer.GetShaderManager().LoadEffect(str_sky_simple);
250 0 : deviceCommandContext->SetGraphicsPipelineState(
251 : skytech->GetGraphicsPipelineState());
252 0 : deviceCommandContext->BeginPass();
253 0 : Renderer::Backend::IShaderProgram* shader = skytech->GetShader();
254 0 : deviceCommandContext->SetTexture(
255 0 : shader->GetBindingSlot(str_baseTex), skyTextureCube->GetBackendTexture());
256 :
257 : // Translate so the sky center is at the camera space origin.
258 0 : CMatrix3D translate;
259 0 : translate.SetTranslation(camera.GetOrientation().GetTranslation());
260 :
261 : // Currently we have a hardcoded near plane in the projection matrix.
262 0 : CMatrix3D scale;
263 0 : scale.SetScaling(10.0f, 10.0f, 10.0f);
264 :
265 : // Rotate so that the "left" face, which contains the brightest part of
266 : // each skymap, is in the direction of the sun from our light
267 : // environment.
268 0 : CMatrix3D rotate;
269 0 : rotate.SetYRotation(std::numbers::pi_v<float> + g_Renderer.GetSceneRenderer().GetLightEnv().GetRotation());
270 :
271 0 : const CMatrix3D transform = camera.GetViewProjection() * translate * rotate * scale;
272 0 : deviceCommandContext->SetUniform(
273 0 : shader->GetBindingSlot(str_transform), transform.AsFloatArray());
274 :
275 0 : const uint32_t stride = m_VertexArray.GetStride();
276 0 : const uint32_t firstVertexOffset = m_VertexArray.GetOffset() * stride;
277 :
278 0 : deviceCommandContext->SetVertexInputLayout(m_VertexInputLayout);
279 :
280 0 : deviceCommandContext->SetVertexBuffer(
281 : 0, m_VertexArray.GetBuffer(), firstVertexOffset);
282 :
283 0 : deviceCommandContext->Draw(0, m_VertexArray.GetNumberOfVertices());
284 :
285 0 : deviceCommandContext->EndPass();
286 0 : }
287 :
288 0 : void SkyManager::CreateSkyCube()
289 : {
290 0 : m_AttributePosition.format = Renderer::Backend::Format::R32G32B32_SFLOAT;
291 0 : m_VertexArray.AddAttribute(&m_AttributePosition);
292 :
293 0 : m_AttributeUV.format = Renderer::Backend::Format::R32G32B32_SFLOAT;
294 0 : m_VertexArray.AddAttribute(&m_AttributeUV);
295 :
296 : // 6 sides of cube with 6 vertices.
297 0 : m_VertexArray.SetNumberOfVertices(6 * 6);
298 0 : m_VertexArray.Layout();
299 :
300 0 : VertexArrayIterator<CVector3D> attrPosition = m_AttributePosition.GetIterator<CVector3D>();
301 0 : VertexArrayIterator<CVector3D> attrUV = m_AttributeUV.GetIterator<CVector3D>();
302 :
303 : #define ADD_VERTEX(U, V, W, VX, VY, VZ) \
304 : STMT( \
305 : attrPosition->X = VX; \
306 : attrPosition->Y = VY; \
307 : attrPosition->Z = VZ; \
308 : ++attrPosition; \
309 : attrUV->X = U; \
310 : attrUV->Y = V; \
311 : attrUV->Z = W; \
312 : ++attrUV;)
313 :
314 : // Axis -X
315 0 : ADD_VERTEX(+1, +1, +1, -1.0f, -1.0f, -1.0f);
316 0 : ADD_VERTEX(+1, +1, -1, -1.0f, -1.0f, +1.0f);
317 0 : ADD_VERTEX(+1, -1, -1, -1.0f, +1.0f, +1.0f);
318 0 : ADD_VERTEX(+1, +1, +1, -1.0f, -1.0f, -1.0f);
319 0 : ADD_VERTEX(+1, -1, -1, -1.0f, +1.0f, +1.0f);
320 0 : ADD_VERTEX(+1, -1, +1, -1.0f, +1.0f, -1.0f);
321 :
322 : // Axis +X
323 0 : ADD_VERTEX(-1, +1, -1, +1.0f, -1.0f, +1.0f);
324 0 : ADD_VERTEX(-1, +1, +1, +1.0f, -1.0f, -1.0f);
325 0 : ADD_VERTEX(-1, -1, +1, +1.0f, +1.0f, -1.0f);
326 0 : ADD_VERTEX(-1, +1, -1, +1.0f, -1.0f, +1.0f);
327 0 : ADD_VERTEX(-1, -1, +1, +1.0f, +1.0f, -1.0f);
328 0 : ADD_VERTEX(-1, -1, -1, +1.0f, +1.0f, +1.0f);
329 :
330 : // Axis -Y
331 0 : ADD_VERTEX(-1, +1, +1, +1.0f, -1.0f, -1.0f);
332 0 : ADD_VERTEX(-1, +1, -1, +1.0f, -1.0f, +1.0f);
333 0 : ADD_VERTEX(+1, +1, -1, -1.0f, -1.0f, +1.0f);
334 0 : ADD_VERTEX(-1, +1, +1, +1.0f, -1.0f, -1.0f);
335 0 : ADD_VERTEX(+1, +1, -1, -1.0f, -1.0f, +1.0f);
336 0 : ADD_VERTEX(+1, +1, +1, -1.0f, -1.0f, -1.0f);
337 :
338 : // Axis +Y
339 0 : ADD_VERTEX(+1, -1, +1, -1.0f, +1.0f, -1.0f);
340 0 : ADD_VERTEX(+1, -1, -1, -1.0f, +1.0f, +1.0f);
341 0 : ADD_VERTEX(-1, -1, -1, +1.0f, +1.0f, +1.0f);
342 0 : ADD_VERTEX(+1, -1, +1, -1.0f, +1.0f, -1.0f);
343 0 : ADD_VERTEX(-1, -1, -1, +1.0f, +1.0f, +1.0f);
344 0 : ADD_VERTEX(-1, -1, +1, +1.0f, +1.0f, -1.0f);
345 :
346 : // Axis -Z
347 0 : ADD_VERTEX(-1, +1, +1, +1.0f, -1.0f, -1.0f);
348 0 : ADD_VERTEX(+1, +1, +1, -1.0f, -1.0f, -1.0f);
349 0 : ADD_VERTEX(+1, -1, +1, -1.0f, +1.0f, -1.0f);
350 0 : ADD_VERTEX(-1, +1, +1, +1.0f, -1.0f, -1.0f);
351 0 : ADD_VERTEX(+1, -1, +1, -1.0f, +1.0f, -1.0f);
352 0 : ADD_VERTEX(-1, -1, +1, +1.0f, +1.0f, -1.0f);
353 :
354 : // Axis +Z
355 0 : ADD_VERTEX(+1, +1, -1, -1.0f, -1.0f, +1.0f);
356 0 : ADD_VERTEX(-1, +1, -1, +1.0f, -1.0f, +1.0f);
357 0 : ADD_VERTEX(-1, -1, -1, +1.0f, +1.0f, +1.0f);
358 0 : ADD_VERTEX(+1, +1, -1, -1.0f, -1.0f, +1.0f);
359 0 : ADD_VERTEX(-1, -1, -1, +1.0f, +1.0f, +1.0f);
360 0 : ADD_VERTEX(+1, -1, -1, -1.0f, +1.0f, +1.0f);
361 : #undef ADD_VERTEX
362 :
363 0 : m_VertexArray.Upload();
364 0 : m_VertexArray.FreeBackingStore();
365 :
366 0 : const uint32_t stride = m_VertexArray.GetStride();
367 0 : const std::array<Renderer::Backend::SVertexAttributeFormat, 2> attributes{{
368 : {Renderer::Backend::VertexAttributeStream::POSITION,
369 0 : m_AttributePosition.format, m_AttributePosition.offset, stride,
370 : Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0},
371 : {Renderer::Backend::VertexAttributeStream::UV0,
372 0 : m_AttributeUV.format, m_AttributeUV.offset, stride,
373 : Renderer::Backend::VertexAttributeRate::PER_VERTEX, 0}
374 0 : }};
375 0 : m_VertexInputLayout = g_Renderer.GetVertexInputLayout(attributes);
376 0 : }
|