From 4679487640c5c80d9d6bee66370ebf89b53de700 Mon Sep 17 00:00:00 2001 From: Markus Wick Date: Sat, 19 May 2018 15:09:57 +0200 Subject: [PATCH] gl_rasterizer: Use the shared texture buffer for the lighting lut. --- .../renderer_opengl/gl_rasterizer.cpp | 47 ++++++++++--------- .../renderer_opengl/gl_rasterizer.h | 2 - .../renderer_opengl/gl_shader_gen.cpp | 3 +- .../renderer_opengl/gl_shader_manager.cpp | 1 - src/video_core/renderer_opengl/gl_state.cpp | 10 ---- src/video_core/renderer_opengl/gl_state.h | 5 -- 6 files changed, 27 insertions(+), 41 deletions(-) diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp index 84232e7f47..a7e8126e36 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.cpp +++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp @@ -78,9 +78,6 @@ RasterizerOpenGL::RasterizerOpenGL() uniform_block_data.proctex_lut_dirty = true; uniform_block_data.proctex_diff_lut_dirty = true; - for (int i = 0; i < 24; i++) - uniform_block_data.data.lighting_lut_offset[i / 4][i % 4] = 256 * i; - glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &uniform_buffer_alignment); uniform_size_aligned_vs = Common::AlignUp(sizeof(VSUniformData), uniform_buffer_alignment); @@ -138,18 +135,6 @@ RasterizerOpenGL::RasterizerOpenGL() glActiveTexture(TextureUnits::TextureBufferLUT_RGBA.Enum()); glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA32F, texture_buffer.GetHandle()); - // Allocate and bind lighting lut textures - lighting_lut.Create(); - state.lighting_lut.texture_buffer = lighting_lut.handle; - state.Apply(); - lighting_lut_buffer.Create(); - glBindBuffer(GL_TEXTURE_BUFFER, lighting_lut_buffer.handle); - glBufferData(GL_TEXTURE_BUFFER, - sizeof(GLfloat) * 2 * 256 * Pica::LightingRegs::NumLightingSampler, nullptr, - GL_DYNAMIC_DRAW); - glActiveTexture(TextureUnits::LightingLUT.Enum()); - glTexBuffer(GL_TEXTURE_BUFFER, GL_RG32F, lighting_lut_buffer.handle); - // Setup the LUT for the fog fog_lut.Create(); state.fog_lut.texture_buffer = fog_lut.handle; @@ -1951,10 +1936,25 @@ void RasterizerOpenGL::SyncAndUploadLUTs() { sizeof(GLvec4) * 256 + // proctex sizeof(GLvec4) * 256; // proctex diff + if (!uniform_block_data.lighting_lut_dirty_any && !uniform_block_data.fog_lut_dirty && + !uniform_block_data.proctex_noise_lut_dirty && + !uniform_block_data.proctex_color_map_dirty && + !uniform_block_data.proctex_alpha_map_dirty && !uniform_block_data.proctex_lut_dirty && + !uniform_block_data.proctex_diff_lut_dirty) { + return; + } + + u8* buffer; + GLintptr offset; + bool invalidate; + size_t bytes_used = 0; + glBindBuffer(GL_TEXTURE_BUFFER, texture_buffer.GetHandle()); + std::tie(buffer, offset, invalidate) = texture_buffer.Map(max_size, sizeof(GLvec4)); + // Sync the lighting luts - if (uniform_block_data.lighting_lut_dirty_any) { + if (uniform_block_data.lighting_lut_dirty_any || invalidate) { for (unsigned index = 0; index < uniform_block_data.lighting_lut_dirty.size(); index++) { - if (uniform_block_data.lighting_lut_dirty[index]) { + if (uniform_block_data.lighting_lut_dirty[index] || invalidate) { std::array new_data; const auto& source_lut = Pica::g_state.lighting.luts[index]; std::transform(source_lut.begin(), source_lut.end(), new_data.begin(), @@ -1962,11 +1962,14 @@ void RasterizerOpenGL::SyncAndUploadLUTs() { return GLvec2{entry.ToFloat(), entry.DiffToFloat()}; }); - if (new_data != lighting_lut_data[index]) { + if (new_data != lighting_lut_data[index] || invalidate) { lighting_lut_data[index] = new_data; - glBindBuffer(GL_TEXTURE_BUFFER, lighting_lut_buffer.handle); - glBufferSubData(GL_TEXTURE_BUFFER, index * new_data.size() * sizeof(GLvec2), - new_data.size() * sizeof(GLvec2), new_data.data()); + std::memcpy(buffer + bytes_used, new_data.data(), + new_data.size() * sizeof(GLvec2)); + uniform_block_data.data.lighting_lut_offset[index / 4][index % 4] = + (offset + bytes_used) / sizeof(GLvec2); + uniform_block_data.dirty = true; + bytes_used += new_data.size() * sizeof(GLvec2); } uniform_block_data.lighting_lut_dirty[index] = false; } @@ -2068,6 +2071,8 @@ void RasterizerOpenGL::SyncAndUploadLUTs() { } uniform_block_data.proctex_diff_lut_dirty = false; } + + texture_buffer.Unmap(bytes_used); } void RasterizerOpenGL::UploadUniforms(bool accelerate_draw, bool use_gs) { diff --git a/src/video_core/renderer_opengl/gl_rasterizer.h b/src/video_core/renderer_opengl/gl_rasterizer.h index 8b33c95738..8eb78a4f9f 100644 --- a/src/video_core/renderer_opengl/gl_rasterizer.h +++ b/src/video_core/renderer_opengl/gl_rasterizer.h @@ -289,8 +289,6 @@ private: OGLTexture texture_buffer_lut_rg; OGLTexture texture_buffer_lut_rgba; - OGLBuffer lighting_lut_buffer; - OGLTexture lighting_lut; std::array, Pica::LightingRegs::NumLightingSampler> lighting_lut_data{}; OGLBuffer fog_lut_buffer; diff --git a/src/video_core/renderer_opengl/gl_shader_gen.cpp b/src/video_core/renderer_opengl/gl_shader_gen.cpp index 0d3a955a43..312a41f001 100644 --- a/src/video_core/renderer_opengl/gl_shader_gen.cpp +++ b/src/video_core/renderer_opengl/gl_shader_gen.cpp @@ -1224,7 +1224,6 @@ uniform sampler2D tex2; uniform samplerCube tex_cube; uniform samplerBuffer texture_buffer_lut_rg; uniform samplerBuffer texture_buffer_lut_rgba; -uniform samplerBuffer lighting_lut; uniform samplerBuffer fog_lut; uniform samplerBuffer proctex_noise_lut; uniform samplerBuffer proctex_color_map; @@ -1252,7 +1251,7 @@ vec3 quaternion_rotate(vec4 q, vec3 v) { } float LookupLightingLUT(int lut_index, int index, float delta) { - vec2 entry = texelFetch(lighting_lut, lighting_lut_offset[lut_index >> 2][lut_index & 3] + index).rg; + vec2 entry = texelFetch(texture_buffer_lut_rg, lighting_lut_offset[lut_index >> 2][lut_index & 3] + index).rg; return entry.r + entry.g * delta; } diff --git a/src/video_core/renderer_opengl/gl_shader_manager.cpp b/src/video_core/renderer_opengl/gl_shader_manager.cpp index 66b2db9edc..c14744e6b8 100644 --- a/src/video_core/renderer_opengl/gl_shader_manager.cpp +++ b/src/video_core/renderer_opengl/gl_shader_manager.cpp @@ -57,7 +57,6 @@ static void SetShaderSamplerBindings(GLuint shader) { // Set the texture samplers to correspond to different lookup table texture units SetShaderSamplerBinding(shader, "texture_buffer_lut_rg", TextureUnits::TextureBufferLUT_RG); SetShaderSamplerBinding(shader, "texture_buffer_lut_rgba", TextureUnits::TextureBufferLUT_RGBA); - SetShaderSamplerBinding(shader, "lighting_lut", TextureUnits::LightingLUT); SetShaderSamplerBinding(shader, "fog_lut", TextureUnits::FogLUT); SetShaderSamplerBinding(shader, "proctex_noise_lut", TextureUnits::ProcTexNoiseLUT); SetShaderSamplerBinding(shader, "proctex_color_map", TextureUnits::ProcTexColorMap); diff --git a/src/video_core/renderer_opengl/gl_state.cpp b/src/video_core/renderer_opengl/gl_state.cpp index 217cdb3163..005523ce3f 100644 --- a/src/video_core/renderer_opengl/gl_state.cpp +++ b/src/video_core/renderer_opengl/gl_state.cpp @@ -58,8 +58,6 @@ OpenGLState::OpenGLState() { texture_buffer_lut_rg.texture_buffer = 0; texture_buffer_lut_rgba.texture_buffer = 0; - lighting_lut.texture_buffer = 0; - fog_lut.texture_buffer = 0; proctex_lut.texture_buffer = 0; @@ -237,12 +235,6 @@ void OpenGLState::Apply() const { glBindTexture(GL_TEXTURE_BUFFER, texture_buffer_lut_rgba.texture_buffer); } - // Lighting LUTs - if (lighting_lut.texture_buffer != cur_state.lighting_lut.texture_buffer) { - glActiveTexture(TextureUnits::LightingLUT.Enum()); - glBindTexture(GL_TEXTURE_BUFFER, lighting_lut.texture_buffer); - } - // Fog LUT if (fog_lut.texture_buffer != cur_state.fog_lut.texture_buffer) { glActiveTexture(TextureUnits::FogLUT.Enum()); @@ -394,8 +386,6 @@ OpenGLState& OpenGLState::ResetTexture(GLuint handle) { texture_buffer_lut_rg.texture_buffer = 0; if (texture_buffer_lut_rgba.texture_buffer == handle) texture_buffer_lut_rgba.texture_buffer = 0; - if (lighting_lut.texture_buffer == handle) - lighting_lut.texture_buffer = 0; if (fog_lut.texture_buffer == handle) fog_lut.texture_buffer = 0; if (proctex_noise_lut.texture_buffer == handle) diff --git a/src/video_core/renderer_opengl/gl_state.h b/src/video_core/renderer_opengl/gl_state.h index 64ded0b805..4a07457200 100644 --- a/src/video_core/renderer_opengl/gl_state.h +++ b/src/video_core/renderer_opengl/gl_state.h @@ -20,7 +20,6 @@ constexpr TextureUnit PicaTexture(int unit) { return TextureUnit{unit}; } -constexpr TextureUnit LightingLUT{3}; constexpr TextureUnit FogLUT{4}; constexpr TextureUnit ProcTexNoiseLUT{5}; constexpr TextureUnit ProcTexColorMap{6}; @@ -113,10 +112,6 @@ public: GLuint texture_buffer; // GL_TEXTURE_BINDING_BUFFER } texture_buffer_lut_rgba; - struct { - GLuint texture_buffer; // GL_TEXTURE_BINDING_BUFFER - } lighting_lut; - struct { GLuint texture_buffer; // GL_TEXTURE_BINDING_BUFFER } fog_lut;