18constexpr GLenum CUBE_FACE_COUNT = 6;
19constexpr GLsizei STAND_IN_SIZE = 1;
28GLuint OpenGLRenderer::FindSkyboxCubemap()
const {
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;
35 dynamic_cast<OpenGLCubemapTexture*
>(skybox->GetCubemapTexture());
36 return cube ? cube->GetGLTexture() : 0;
41void OpenGLRenderer::PrepareSceneRead() {
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);
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);
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,
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;
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);
89 m_sceneCopyHeight = h;
91 glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, w, h);
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);
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);
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);
130 m_sceneFallbackCube = 0;
132 m_sceneCopyWidth = 0;
133 m_sceneCopyHeight = 0;
static Application * GetInstance()
The one Application for this process, or null before construction.
static const Math::Matrix4 & GetMainProjectionMatrix()
static const Math::Matrix4 & GetMainViewMatrix()
Matrix< float, 4, 4 > Matrix4
Backend-facing rendering layer shared by the four graphics backends.
Root namespace for everything the engine exposes.