18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 10:05:23 +01:00
Files
maszyna/rendering/opengl46/opengl46cabprobe.cpp
Hirek 99b9265110 fix(gl46): keep the storage buffer binding points under 16 for Intel graphics
The drivers of integrated Intel graphics offer 16 shader storage buffer
binding points (GL_MAX_SHADER_STORAGE_BUFFER_BINDINGS). The OpenGL 4.6
renderer used 16 (the water of the windscreens), 17 (the spherical
harmonics of the cab probe) and 18 (the cells of the rain). Every material
shader includes light_common.glsl, which declares the cab probe block, so
none of them compiled on Intel and the whole scenery was drawn pink
("shader storage buffer binding must be less than ... (16)").

- the sky light (sky_ubo) and the light of the cab probe (cab_probe_ubo)
  are uniform blocks now: 9 vec4 each, which every fragment reads the
  same of, and which std140 lays out as std430 did. cabprobe_sh.comp still
  writes the probe's buffer as a storage buffer, bound right before its
  dispatch (gl::ssbo::bind_uniform() binds the same buffer as the block)
- the result of the atmosphere and the write of the cab probe share
  binding point 9 with the other compute passes, which bind theirs right
  before their dispatch; the sky no longer keeps the atmosphere result bound
- the water of the windscreens takes point 5 and the rain cells point 7
- gl::MAX_SSBO_BINDINGS (16) with a static_assert over every point, and
  the log gives the points the driver offers at start, with an error when
  there are fewer

No extra binds per draw: the cost on NVIDIA and AMD stays what it was.
The SPIR-V binaries of shaders/ogl46bin/ compiled from the old common.glsl
are out of date and the shaders are compiled from the sources until they
are made again (shaders/compile_spirv.py).

feat/gl46-migration has the same points (water 16, cab probe 17) and needs
the same change.
2026-12-02 15:15:11 +01:00

190 lines
6.6 KiB
C++

/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#include "stdafx.h"
#include "rendering/opengl46/opengl46cabprobe.h"
#include "gl/framebuffer.h"
#include "gl/ubo.h"
#include "model/Texture.h"
#include "utilities/Globals.h"
#include "utilities/Logs.h"
#include "utilities/utilities.h"
#include <format>
namespace
{
// uniform location of cabprobe_sh.comp: the level of the probe read and its size
GLint const SH_LEVEL_LOCATION{0};
// cube map of Size x Size faces with its mipmaps
GLuint make_cube(GLenum const Format, int const Size, bool const Mipmaps)
{
GLuint texture{0};
glCreateTextures(GL_TEXTURE_CUBE_MAP, 1, &texture);
auto const levels{Mipmaps ? static_cast<GLsizei>(std::floor(std::log2(static_cast<float>(std::max(Size, 1))))) + 1 : 1};
glTextureStorage2D(texture, levels, Format, Size, Size);
glTextureParameteri(texture, GL_TEXTURE_MIN_FILTER, Mipmaps ? GL_LINEAR_MIPMAP_LINEAR : GL_NEAREST);
glTextureParameteri(texture, GL_TEXTURE_MAG_FILTER, Mipmaps ? GL_LINEAR : GL_NEAREST);
glTextureParameteri(texture, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTextureParameteri(texture, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTextureParameteri(texture, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
return texture;
}
} // namespace
opengl46_cab_probe::~opengl46_cab_probe()
{
gpu_release();
if (m_empty != 0)
glDeleteTextures(1, &m_empty);
if (m_white != 0)
glDeleteTextures(1, &m_white);
}
void opengl46_cab_probe::init()
{
m_sh = std::make_unique<gl::ssbo>(static_cast<GLsizeiptr>(sizeof(gl::cab_probe_data)), gl::CAB_PROBE_SSBO_BINDING);
m_sh->update(gl::cab_probe_data{});
// a sampler with no texture to read from is undefined, also where the shading doesn't read it
m_empty = make_cube(GL_RGBA8, 1, false);
std::uint8_t const nothing[4]{0, 0, 0, 0};
glClearTexImage(m_empty, 0, GL_RGBA, GL_UNSIGNED_BYTE, nothing);
glCreateTextures(GL_TEXTURE_2D, 1, &m_white);
glTextureStorage2D(m_white, 1, GL_RGBA8, 1, 1);
std::uint8_t const white[4]{255, 255, 255, 255};
glClearTexImage(m_white, 0, GL_RGBA, GL_UNSIGNED_BYTE, white);
try
{
gl::shader vertex("cabprobe.vert");
gl::shader geometry("cabprobe.geom");
{
gl::shader fragment("cabprobe.frag");
m_program = std::make_unique<gl::program>(std::vector<std::reference_wrapper<const gl::shader>>{vertex, geometry, fragment});
}
{
gl::shader fragment("cabprobe_alpha.frag");
m_alpha_program = std::make_unique<gl::program>(std::vector<std::reference_wrapper<const gl::shader>>{vertex, geometry, fragment});
}
gl::shader compute("cabprobe_sh.comp");
m_sh_program = std::make_unique<gl::program>(std::vector<std::reference_wrapper<const gl::shader>>{compute});
}
catch (gl::shader_exception const &e)
{
// the shading reads the stand-ins; no maps are made (gpu_allocate())
ErrorLog(std::format("cab probe not available: {}", e.what()));
m_program.reset();
m_alpha_program.reset();
m_sh_program.reset();
Global.gfx_cabprobe = 0;
}
}
void opengl46_cab_probe::bind()
{
glBindTextureUnit(gl::CAB_PROBE_TEX, (m_light != 0) ? m_light : m_empty);
glBindTextureUnit(gl::CAB_PROBE_ALBEDO_TEX, (m_albedo != 0) ? m_albedo : m_empty);
glBindTextureUnit(gl::CAB_PROBE_MASK_TEX, (m_mask != 0) ? m_mask : m_empty);
if (m_sh)
m_sh->bind_uniform(gl::CAB_PROBE_UBO_BINDING);
}
bool opengl46_cab_probe::gpu_allocate(int const Size)
{
if (m_sh_program == nullptr)
return false;
m_light = make_cube(GL_RGBA16F, Size, true);
m_albedo = make_cube(GL_RGBA8, Size, true);
m_depth = make_cube(GL_DEPTH_COMPONENT32F, Size, false);
// the mask: the level of the albedo of at most CAB_PROBE_MASK_SIZE pixels a face, as mask_size() has it once the size is set
auto masksize{Size};
while (masksize > static_cast<int>(gl::CAB_PROBE_MASK_SIZE))
masksize >>= 1;
m_mask = make_cube(GL_RGBA8, std::max(masksize, 1), false);
glTextureParameteri(m_mask, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTextureParameteri(m_mask, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// all the faces at once, the geometry shader picks the face
glCreateFramebuffers(1, &m_framebuffer);
glNamedFramebufferTexture(m_framebuffer, GL_COLOR_ATTACHMENT0, m_light, 0);
glNamedFramebufferTexture(m_framebuffer, GL_COLOR_ATTACHMENT1, m_albedo, 0);
glNamedFramebufferTexture(m_framebuffer, GL_DEPTH_ATTACHMENT, m_depth, 0);
GLenum const buffers[2]{GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};
glNamedFramebufferDrawBuffers(m_framebuffer, 2, buffers);
glNamedFramebufferReadBuffer(m_framebuffer, GL_NONE);
return (glCheckNamedFramebufferStatus(m_framebuffer, GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
}
void opengl46_cab_probe::gpu_release()
{
if (m_framebuffer != 0)
glDeleteFramebuffers(1, &m_framebuffer);
for (auto *texture : {&m_light, &m_albedo, &m_depth, &m_mask})
{
if (*texture != 0)
glDeleteTextures(1, texture);
*texture = 0;
}
m_framebuffer = 0;
if (m_sh)
m_sh->update(gl::cab_probe_data{});
}
void opengl46_cab_probe::gpu_begin()
{
m_timer.begin();
// the framebuffer is bound past the cache of gl::framebuffer, which the end of the drawing resets
gl::framebuffer::unbind();
glBindFramebuffer(GL_FRAMEBUFFER, m_framebuffer);
glViewport(0, 0, m_size, m_size);
float const nothing[4]{0.f, 0.f, 0.f, 0.f};
glClearNamedFramebufferfv(m_framebuffer, GL_COLOR, 0, nothing);
glClearNamedFramebufferfv(m_framebuffer, GL_COLOR, 1, nothing);
float const farthest{0.f};
glClearNamedFramebufferfv(m_framebuffer, GL_DEPTH, 0, &farthest);
glDisable(GL_BLEND);
glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);
// one sided walls face the probe from either side
glDisable(GL_CULL_FACE);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
void opengl46_cab_probe::gpu_end()
{
glEnable(GL_CULL_FACE);
gl::framebuffer::unbind();
glGenerateTextureMipmap(m_light);
glGenerateTextureMipmap(m_albedo);
// where the windows are, for the next drawing
auto const masksize{mask_size()};
glCopyImageSubData(m_albedo, GL_TEXTURE_CUBE_MAP, mask_level(), 0, 0, 0, m_mask, GL_TEXTURE_CUBE_MAP, 0, 0, 0, 0, masksize, masksize, 6);
// the light the cab lights itself with again, from a small level of the light reflected
glBindTextureUnit(0, m_light);
glProgramUniform2i(*m_sh_program, SH_LEVEL_LOCATION, sh_level(), sh_size());
m_sh->bind_base();
m_sh_program->dispatch(1);
// the shading reads it as a uniform block
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_UNIFORM_BARRIER_BIT);
opengl_texture::reset_unit_cache();
m_timer.end();
}
std::string opengl46_cab_probe::info_times()
{
std::string text;
if (auto const time = m_timer.result())
text += " cab probe: " + to_string(static_cast<double>(*time) / 1000000.0, 3) + " ms\n";
return text;
}