From 9677db03da37a61248c2ced49a9a5e53c872cc63 Mon Sep 17 00:00:00 2001
From: ReinUsesLisp <reinuseslisp@airmail.cc>
Date: Thu, 26 Dec 2019 04:01:11 -0300
Subject: [PATCH] gl_state: Remove texture and sampler tracking

---
 .../renderer_opengl/gl_rasterizer.cpp         | 11 +++--
 .../renderer_opengl/gl_resource_manager.cpp   |  3 --
 src/video_core/renderer_opengl/gl_state.cpp   | 42 -------------------
 src/video_core/renderer_opengl/gl_state.h     |  7 ----
 .../renderer_opengl/renderer_opengl.cpp       |  5 ++-
 5 files changed, 8 insertions(+), 60 deletions(-)

diff --git a/src/video_core/renderer_opengl/gl_rasterizer.cpp b/src/video_core/renderer_opengl/gl_rasterizer.cpp
index f916f348ff..669a7c3352 100644
--- a/src/video_core/renderer_opengl/gl_rasterizer.cpp
+++ b/src/video_core/renderer_opengl/gl_rasterizer.cpp
@@ -633,7 +633,6 @@ void RasterizerOpenGL::DispatchCompute(GPUVAddr code_addr) {
     bind_ubo_pushbuffer.Bind();
     bind_ssbo_pushbuffer.Bind();
 
-    state.ApplyTextures();
     state.ApplyImages();
     state.ApplyShaderProgram();
     state.ApplyProgramPipeline();
@@ -861,20 +860,20 @@ void RasterizerOpenGL::SetupTexture(u32 binding, const Tegra::Texture::FullTextu
     const auto view = texture_cache.GetTextureSurface(texture.tic, entry);
     if (!view) {
         // Can occur when texture addr is null or its memory is unmapped/invalid
-        state.samplers[binding] = 0;
-        state.textures[binding] = 0;
+        glBindSampler(binding, 0);
+        glBindTextureUnit(binding, 0);
         return;
     }
-    state.textures[binding] = view->GetTexture();
+    glBindTextureUnit(binding, view->GetTexture());
 
     if (view->GetSurfaceParams().IsBuffer()) {
         return;
     }
-    state.samplers[binding] = sampler_cache.GetSampler(texture.tsc);
-
     // Apply swizzle to textures that are not buffers.
     view->ApplySwizzle(texture.tic.x_source, texture.tic.y_source, texture.tic.z_source,
                        texture.tic.w_source);
+
+    glBindSampler(binding, sampler_cache.GetSampler(texture.tsc));
 }
 
 void RasterizerOpenGL::SetupDrawImages(std::size_t stage_index, const Shader& shader) {
diff --git a/src/video_core/renderer_opengl/gl_resource_manager.cpp b/src/video_core/renderer_opengl/gl_resource_manager.cpp
index 00355c1daa..80666a9ed5 100644
--- a/src/video_core/renderer_opengl/gl_resource_manager.cpp
+++ b/src/video_core/renderer_opengl/gl_resource_manager.cpp
@@ -47,7 +47,6 @@ void OGLTexture::Release() {
 
     MICROPROFILE_SCOPE(OpenGL_ResourceDeletion);
     glDeleteTextures(1, &handle);
-    OpenGLState::GetCurState().UnbindTexture(handle).Apply();
     handle = 0;
 }
 
@@ -65,7 +64,6 @@ void OGLTextureView::Release() {
 
     MICROPROFILE_SCOPE(OpenGL_ResourceDeletion);
     glDeleteTextures(1, &handle);
-    OpenGLState::GetCurState().UnbindTexture(handle).Apply();
     handle = 0;
 }
 
@@ -83,7 +81,6 @@ void OGLSampler::Release() {
 
     MICROPROFILE_SCOPE(OpenGL_ResourceDeletion);
     glDeleteSamplers(1, &handle);
-    OpenGLState::GetCurState().ResetSampler(handle).Apply();
     handle = 0;
 }
 
diff --git a/src/video_core/renderer_opengl/gl_state.cpp b/src/video_core/renderer_opengl/gl_state.cpp
index 3cdb9b4a05..98de72b5f1 100644
--- a/src/video_core/renderer_opengl/gl_state.cpp
+++ b/src/video_core/renderer_opengl/gl_state.cpp
@@ -113,28 +113,6 @@ void OpenGLState::ApplyRenderBuffer() {
     }
 }
 
-void OpenGLState::ApplyTextures() {
-    const std::size_t size = std::size(textures);
-    for (std::size_t i = 0; i < size; ++i) {
-        if (UpdateValue(cur_state.textures[i], textures[i])) {
-            // BindTextureUnit doesn't support binding null textures, skip those binds.
-            // TODO(Rodrigo): Stop using null textures
-            if (textures[i] != 0) {
-                glBindTextureUnit(static_cast<GLuint>(i), textures[i]);
-            }
-        }
-    }
-}
-
-void OpenGLState::ApplySamplers() {
-    const std::size_t size = std::size(samplers);
-    for (std::size_t i = 0; i < size; ++i) {
-        if (UpdateValue(cur_state.samplers[i], samplers[i])) {
-            glBindSampler(static_cast<GLuint>(i), samplers[i]);
-        }
-    }
-}
-
 void OpenGLState::ApplyImages() {
     if (const auto update = UpdateArray(cur_state.images, images)) {
         glBindImageTextures(update->first, update->second, images.data() + update->first);
@@ -146,30 +124,10 @@ void OpenGLState::Apply() {
     ApplyFramebufferState();
     ApplyShaderProgram();
     ApplyProgramPipeline();
-    ApplyTextures();
-    ApplySamplers();
     ApplyImages();
     ApplyRenderBuffer();
 }
 
-OpenGLState& OpenGLState::UnbindTexture(GLuint handle) {
-    for (auto& texture : textures) {
-        if (texture == handle) {
-            texture = 0;
-        }
-    }
-    return *this;
-}
-
-OpenGLState& OpenGLState::ResetSampler(GLuint handle) {
-    for (auto& sampler : samplers) {
-        if (sampler == handle) {
-            sampler = 0;
-        }
-    }
-    return *this;
-}
-
 OpenGLState& OpenGLState::ResetProgram(GLuint handle) {
     if (draw.shader_program == handle) {
         draw.shader_program = 0;
diff --git a/src/video_core/renderer_opengl/gl_state.h b/src/video_core/renderer_opengl/gl_state.h
index 29126b80a5..25dd56452a 100644
--- a/src/video_core/renderer_opengl/gl_state.h
+++ b/src/video_core/renderer_opengl/gl_state.h
@@ -13,10 +13,7 @@ namespace OpenGL {
 
 class OpenGLState {
 public:
-    static constexpr std::size_t NumSamplers = 32 * 5;
     static constexpr std::size_t NumImages = 8 * 5;
-    std::array<GLuint, NumSamplers> textures = {};
-    std::array<GLuint, NumSamplers> samplers = {};
     std::array<GLuint, NumImages> images = {};
 
     struct {
@@ -41,14 +38,10 @@ public:
     void ApplyFramebufferState();
     void ApplyShaderProgram();
     void ApplyProgramPipeline();
-    void ApplyTextures();
-    void ApplySamplers();
     void ApplyImages();
     void ApplyRenderBuffer();
 
     /// Resets any references to the given resource
-    OpenGLState& UnbindTexture(GLuint handle);
-    OpenGLState& ResetSampler(GLuint handle);
     OpenGLState& ResetProgram(GLuint handle);
     OpenGLState& ResetPipeline(GLuint handle);
     OpenGLState& ResetFramebuffer(GLuint handle);
diff --git a/src/video_core/renderer_opengl/renderer_opengl.cpp b/src/video_core/renderer_opengl/renderer_opengl.cpp
index 053d8602b4..1295121f5b 100644
--- a/src/video_core/renderer_opengl/renderer_opengl.cpp
+++ b/src/video_core/renderer_opengl/renderer_opengl.cpp
@@ -566,7 +566,6 @@ void RendererOpenGL::DrawScreen(const Layout::FramebufferLayout& layout) {
     };
     glNamedBufferSubData(vertex_buffer.handle, 0, sizeof(vertices), std::data(vertices));
 
-    state.textures[0] = screen_info.display_texture;
     state.Apply();
 
     // TODO: Signal state tracker about these changes
@@ -598,11 +597,13 @@ void RendererOpenGL::DrawScreen(const Layout::FramebufferLayout& layout) {
     glVertexAttribBinding(TexCoordLocation, 0);
     glBindVertexBuffer(0, vertex_buffer.handle, 0, sizeof(ScreenRectVertex));
 
+    glBindTextureUnit(0, screen_info.display_texture);
+    glBindSampler(0, 0);
+
     glClear(GL_COLOR_BUFFER_BIT);
     glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
 
     // Restore default state
-    state.textures[0] = 0;
     state.Apply();
 }