SleakEngine 0.1.0
C++23 multi-backend game engine
Loading...
Searching...
No Matches
OpenGLSceneRead.cpp
Go to the documentation of this file.
1#include <Camera/Camera.hpp>
3#include <Core/GameBase.hpp>
4#include <Core/SceneBase.hpp>
5#include <Runtime/Skybox.hpp>
6#include <algorithm>
7#include <cstring>
8
12
13namespace Sleak {
14namespace RenderEngine {
15
16namespace {
17
18constexpr GLenum CUBE_FACE_COUNT = 6;
19constexpr GLsizei STAND_IN_SIZE = 1;
20
21} // namespace
22
23// ==================================================================
24// Scene read for transparent formats (VertexLayoutDesc::transparentReadsScene)
25// ==================================================================
26
27/// Returns the active scene's skybox cubemap, or 0 when there is none.
28GLuint OpenGLRenderer::FindSkyboxCubemap() const {
29 auto* app = Application::GetInstance();
30 auto* game = app ? app->GetGame() : nullptr;
31 auto* scene = game ? game->GetActiveScene() : nullptr;
32 auto* skybox = scene ? scene->GetSkybox() : nullptr;
33 if (!skybox || !skybox->IsInitialized()) return 0;
34 auto* cube =
35 dynamic_cast<OpenGLCubemapTexture*>(skybox->GetCubemapTexture());
36 return cube ? cube->GetGLTexture() : 0;
37}
38
39/// Snapshots the scene once per frame on the first scene-reading draw and
40/// binds the snapshot, depth, sky, and SceneReadUBO for every such draw.
41void OpenGLRenderer::PrepareSceneRead() {
42 // Creation binds go to the units rebound below
43 if (m_sceneFallbackCube == 0) {
44 glGenTextures(1, &m_sceneFallbackCube);
45 glActiveTexture(GL_TEXTURE0 + SCENE_SKY_UNIT);
46 glBindTexture(GL_TEXTURE_CUBE_MAP, m_sceneFallbackCube);
47 const uint32_t black = 0;
48 for (GLenum face = 0; face < CUBE_FACE_COUNT; ++face)
49 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, GL_RGBA8,
50 STAND_IN_SIZE, STAND_IN_SIZE, 0, GL_RGBA,
51 GL_UNSIGNED_BYTE, &black);
52 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
53 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
54 glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, 0);
55 }
56 if (m_sceneCopyTex == 0) {
57 glGenTextures(1, &m_sceneCopyTex);
58 glActiveTexture(GL_TEXTURE0 + SCENE_COLOR_UNIT);
59 glBindTexture(GL_TEXTURE_2D, m_sceneCopyTex);
60 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
61 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
62 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
63 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
64 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
65 }
66 if (m_sceneReadUBO == 0) {
67 glGenBuffers(1, &m_sceneReadUBO);
68 glBindBuffer(GL_UNIFORM_BUFFER, m_sceneReadUBO);
69 glBufferData(GL_UNIFORM_BUFFER, sizeof(SceneReadUBO), nullptr,
70 GL_DYNAMIC_DRAW);
71 }
72
73 if (!m_sceneReadPrepared) {
74 m_sceneReadPrepared = true;
75 m_sceneSkyCube = FindSkyboxCubemap();
76 const int w = m_gbufferWidth;
77 const int h = m_gbufferHeight;
78 const bool valid = m_gbufferCreated && IsDeferredEnabled() &&
79 m_gbufferDepth != 0 && w > 0 && h > 0;
80
81 // Copy of the default framebuffer as drawn so far
82 if (valid) {
83 glActiveTexture(GL_TEXTURE0 + SCENE_COLOR_UNIT);
84 glBindTexture(GL_TEXTURE_2D, m_sceneCopyTex);
85 if (w != m_sceneCopyWidth || h != m_sceneCopyHeight) {
86 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA,
87 GL_UNSIGNED_BYTE, nullptr);
88 m_sceneCopyWidth = w;
89 m_sceneCopyHeight = h;
90 }
91 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, w, h);
92 }
93
94 // Camera block
95 const Math::Matrix4 viewProj =
97 SceneReadUBO ubo{};
98 std::memcpy(ubo.ViewProj, &viewProj(0, 0), sizeof(ubo.ViewProj));
99 std::memcpy(ubo.InvViewProj, m_cachedInvViewProj,
100 sizeof(ubo.InvViewProj));
101 ubo.ScreenWidth = static_cast<float>(std::max(w, 1));
102 ubo.ScreenHeight = static_cast<float>(std::max(h, 1));
103 ubo.InvScreenWidth = 1.0f / ubo.ScreenWidth;
104 ubo.InvScreenHeight = 1.0f / ubo.ScreenHeight;
105 ubo.NearPlane = m_cachedNearPlane;
106 ubo.FarPlane = m_cachedFarPlane;
107 ubo.SceneValid = static_cast<float>(valid);
108 ubo.SkyValid = static_cast<float>(m_sceneSkyCube != 0);
109 glBindBuffer(GL_UNIFORM_BUFFER, m_sceneReadUBO);
110 glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(ubo), &ubo);
111 }
112
113 glBindBufferBase(GL_UNIFORM_BUFFER, SCENE_READ_UBO_BINDING, m_sceneReadUBO);
114 glActiveTexture(GL_TEXTURE0 + SCENE_COLOR_UNIT);
115 glBindTexture(GL_TEXTURE_2D, m_sceneCopyTex);
116 glActiveTexture(GL_TEXTURE0 + SCENE_DEPTH_UNIT);
117 glBindTexture(GL_TEXTURE_2D, m_gbufferDepth);
118 glActiveTexture(GL_TEXTURE0 + SCENE_SKY_UNIT);
119 glBindTexture(GL_TEXTURE_CUBE_MAP,
120 m_sceneSkyCube != 0 ? m_sceneSkyCube : m_sceneFallbackCube);
121 glActiveTexture(GL_TEXTURE0);
122}
123
124/// Deletes the snapshot texture, stand-in cubemap, and UBO.
125void OpenGLRenderer::CleanupSceneRead() {
126 if (m_sceneCopyTex) glDeleteTextures(1, &m_sceneCopyTex);
127 if (m_sceneFallbackCube) glDeleteTextures(1, &m_sceneFallbackCube);
128 if (m_sceneReadUBO) glDeleteBuffers(1, &m_sceneReadUBO);
129 m_sceneCopyTex = 0;
130 m_sceneFallbackCube = 0;
131 m_sceneReadUBO = 0;
132 m_sceneCopyWidth = 0;
133 m_sceneCopyHeight = 0;
134}
135
136} // namespace RenderEngine
137} // namespace Sleak
static Application * GetInstance()
The one Application for this process, or null before construction.
static const Math::Matrix4 & GetMainProjectionMatrix()
Definition Camera.hpp:107
static const Math::Matrix4 & GetMainViewMatrix()
Definition Camera.hpp:103
Matrix< float, 4, 4 > Matrix4
Definition Matrix.hpp:404
Backend-facing rendering layer shared by the four graphics backends.
Root namespace for everything the engine exposes.
Definition Camera.hpp:10