diff --git a/core/renderer/backend/opengl/OpenGLState.h b/core/renderer/backend/opengl/OpenGLState.h index 75a59a3de5be..f2b75b5b3f09 100644 --- a/core/renderer/backend/opengl/OpenGLState.h +++ b/core/renderer/backend/opengl/OpenGLState.h @@ -117,6 +117,7 @@ struct OpenGLState }; constexpr static int MAX_VERTEX_ATTRIBS = 16; + constexpr static int MAX_TEXTURE_UNITS = 16; template static inline void try_enable(GLenum target, _Left& opt) @@ -259,12 +260,20 @@ struct OpenGLState void stencilMaskFront(GLuint v) { try_callu(glStencilMaskSeparate, GL_FRONT, _stencilMaskFront, v); } void stencilMaskBack(GLuint v) { try_callu(glStencilMaskSeparate, GL_BACK, _stencilMaskBack, v); } void activeTexture(GLenum v) { try_call(glActiveTexture, _activeTexture, v); } - void bindTexture(GLenum target, GLuint handle) { try_callx(glBindTexture, _textureBinding, target, handle); } + void bindTexture(GLenum target, GLuint handle) { + auto activeLayer = _activeTexture.has_value() ? _activeTexture.value() - GL_TEXTURE0 : 0; + if(activeLayer < MAX_TEXTURE_UNITS) + try_callx(glBindTexture, _textureBindings[activeLayer], target, handle); + } void deleteTexture(GLenum target, GLuint handle) { glDeleteTextures(1, &handle); - if (_textureBinding.has_value() && _textureBinding->handle == handle) - _textureBinding.reset(); + + for (auto& textureBinding : _textureBindings) + { + if (textureBinding.has_value() && textureBinding->handle == handle) + textureBinding.reset(); + } } GLenum bindBuffer(BufferType type, GLuint buffer) { @@ -388,6 +397,7 @@ struct OpenGLState uint32_t _attribBits{0}; // vertexAttribArray bitset uint32_t _divisorBits{0}; // divisor bitset std::optional _bufferBindings[(int)BufferType::COUNT]; + std::optional _textureBindings[MAX_TEXTURE_UNITS]; std::optional _viewPort; std::optional _winding; @@ -416,7 +426,6 @@ struct OpenGLState std::optional _stencilMaskFront; std::optional _stencilMaskBack; std::optional _activeTexture; - std::optional _textureBinding; std::optional _uniformBufferState; }; diff --git a/core/renderer/backend/opengl/TextureGL.cpp b/core/renderer/backend/opengl/TextureGL.cpp index 7cd1fdb2cb79..9c1af853c25b 100644 --- a/core/renderer/backend/opengl/TextureGL.cpp +++ b/core/renderer/backend/opengl/TextureGL.cpp @@ -122,7 +122,7 @@ void TextureInfoGL::recreateAll(GLenum target) { if (texID) { - glDeleteTextures(1, &texID); + __gl->deleteTexture(target, texID); texID = 0; ensure(idx, target); }