diff --git a/doc/pr-changelogs/3482.md b/doc/pr-changelogs/3482.md new file mode 100644 index 0000000000..4dbe15abc7 --- /dev/null +++ b/doc/pr-changelogs/3482.md @@ -0,0 +1 @@ + * fix FBO table attachments set by assignment, RBOs given to `gl.CreateFBO` being collected, framebuffer bindings after `gl.BlitFBO` and `gl.RenderToTexture`, numeric and stencil attachment clears, cube map textures without storage, and `lodBias` that could not be reset to 0. diff --git a/rts/Lua/LuaFBOs.cpp b/rts/Lua/LuaFBOs.cpp index 36cccbf714..6bb3c82ee1 100644 --- a/rts/Lua/LuaFBOs.cpp +++ b/rts/Lua/LuaFBOs.cpp @@ -390,7 +390,7 @@ bool LuaFBOs::ApplyAttachment( if (!lua_istable(L, index)) return AttachObject(__func__, L, index, fbo, attachID); - const int table = (index < 0) ? index : (lua_gettop(L) + index + 1); + const int table = (index > 0) ? index : (lua_gettop(L) + index + 1); GLenum target = 0; GLint level = 0; @@ -522,6 +522,11 @@ int LuaFBOs::CreateFBO(lua_State* L) ApplyDrawBuffers(L, -1); } } + lua_rawgeti(L, LUA_REGISTRYINDEX, fboPtr->luaRef); + lua_pushvalue(L, -3); // key + lua_pushvalue(L, -3); // value + lua_rawset(L, -3); + lua_pop(L, 1); } } @@ -791,15 +796,18 @@ int LuaFBOs::BlitFBO(lua_State* L) const GLbitfield mask = (GLbitfield)luaL_optint(L, 11, GL_COLOR_BUFFER_BIT); const GLenum filter = (GLenum)luaL_optint(L, 12, GL_NEAREST); - GLint currentFBO; - glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, ¤tFBO); + GLint currentReadFBO; + GLint currentDrawFBO; + glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING_EXT, ¤tReadFBO); + glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING_EXT, ¤tDrawFBO); glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, (fboSrc == nullptr)? 0: fboSrc->id); glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, (fboDst == nullptr)? 0: fboDst->id); glBlitFramebufferEXT(x0Src, y0Src, x1Src, y1Src, x0Dst, y0Dst, x1Dst, y1Dst, mask, filter); - glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, currentFBO); + glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, currentReadFBO); + glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, currentDrawFBO); return 0; } @@ -869,7 +877,7 @@ int LuaFBOs::ClearAttachmentFBO(lua_State* L) GLenum attachment = 0; GLenum drawBuffer = 0; - if (lua_isstring(L, nextArg)) { + if (lua_israwstring(L, nextArg)) { const char* attachmentStr = luaL_checkstring(L, nextArg++); switch (hashString(attachmentStr)) { case hashString("color0"): { bufferType = GL_COLOR; attachment = GL_COLOR_ATTACHMENT0; drawBuffer = 0; } break; @@ -933,6 +941,9 @@ int LuaFBOs::ClearAttachmentFBO(lua_State* L) GLint componentType = 0; glGetFramebufferAttachmentParameteriv(target, attachment, GL_FRAMEBUFFER_ATTACHMENT_COMPONENT_TYPE, &componentType); + if (bufferType == GL_STENCIL) + componentType = GL_INT; // stencil clears require glClearBufferiv + switch (componentType) { case GL_INT: diff --git a/rts/Lua/LuaOpenGL.cpp b/rts/Lua/LuaOpenGL.cpp index 361312834d..62906d46bb 100644 --- a/rts/Lua/LuaOpenGL.cpp +++ b/rts/Lua/LuaOpenGL.cpp @@ -598,6 +598,8 @@ const GLbitfield AttribBits = GL_LINE_BIT | GL_POINT_BIT | GL_POLYGON_BIT | + GL_SCISSOR_BIT | + GL_STENCIL_BUFFER_BIT | GL_VIEWPORT_BIT; @@ -4610,10 +4612,10 @@ int LuaOpenGL::RenderToTexture(lua_State* L) if ((tex == nullptr) || (tex->fbo == 0)) return 0; - GLint currentFBO = 0; - if (drawMode == DRAW_WORLD_SHADOW) { - glGetIntegerv(GL_FRAMEBUFFER_BINDING_EXT, ¤tFBO); - } + GLint currentReadFBO = 0; + GLint currentDrawFBO = 0; + glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING_EXT, ¤tReadFBO); + glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING_EXT, ¤tDrawFBO); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, tex->fbo); @@ -4628,7 +4630,8 @@ int LuaOpenGL::RenderToTexture(lua_State* L) glMatrixMode(GL_MODELVIEW); glPopMatrix(); glPopAttrib(); - glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, currentFBO); + glBindFramebufferEXT(GL_READ_FRAMEBUFFER_EXT, currentReadFBO); + glBindFramebufferEXT(GL_DRAW_FRAMEBUFFER_EXT, currentDrawFBO); if (error != 0) { LOG_L(L_ERROR, "gl.RenderToTexture: error(%i) = %s", diff --git a/rts/Lua/LuaOpenGLUtils.cpp b/rts/Lua/LuaOpenGLUtils.cpp index ce94175f34..46abd0364a 100644 --- a/rts/Lua/LuaOpenGLUtils.cpp +++ b/rts/Lua/LuaOpenGLUtils.cpp @@ -386,13 +386,14 @@ bool LuaOpenGLUtils::ParseTextureImage(lua_State* L, LuaMatTexture& texUnit, con // dynamic texture const LuaTextures& textures = CLuaHandle::GetActiveTextures(L); - const LuaTextures::Texture* texInfo = textures.GetInfo(image); + const size_t texIdx = textures.GetIdx(image); + const LuaTextures::Texture* texInfo = textures.GetInfo(texIdx); if (texInfo == nullptr) return false; texUnit.type = LuaMatTexture::LUATEX_LUATEXTURE; - texUnit.data = reinterpret_cast(textures.GetIdx(image)); + texUnit.data = reinterpret_cast(texIdx); } break; case '%': { diff --git a/rts/Lua/LuaTextures.cpp b/rts/Lua/LuaTextures.cpp index 0ae691d63c..7471d17d1a 100644 --- a/rts/Lua/LuaTextures.cpp +++ b/rts/Lua/LuaTextures.cpp @@ -67,6 +67,10 @@ std::string LuaTextures::Create(const Texture& tex) case GL_TEXTURE_2D: { glTexImage2D(tex.target, 0, tex.format, tex.xsize, tex.ysize , tex.border, dataFormat, dataType, nullptr); } break; + case GL_TEXTURE_CUBE_MAP: { + for (GLenum face = GL_TEXTURE_CUBE_MAP_POSITIVE_X; face <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; ++face) + glTexImage2D(face, 0, tex.format, tex.xsize, tex.ysize, tex.border, dataFormat, dataType, nullptr); + } break; case GL_TEXTURE_2D_ARRAY: [[fallthrough]]; case GL_TEXTURE_3D: { glTexImage3D(tex.target, 0, tex.format, tex.xsize, tex.ysize, tex.zsize, tex.border, dataFormat, dataType, nullptr); @@ -243,6 +247,9 @@ void LuaTextures::FreeAll() void LuaTextures::ApplyParams(const Texture& tex) const { + if (tex.target == GL_TEXTURE_2D_MULTISAMPLE) + return; + glTexParameteri(tex.target, GL_TEXTURE_WRAP_S, tex.wrap_s); glTexParameteri(tex.target, GL_TEXTURE_WRAP_T, tex.wrap_t); glTexParameteri(tex.target, GL_TEXTURE_WRAP_R, tex.wrap_r); @@ -257,8 +264,7 @@ void LuaTextures::ApplyParams(const Texture& tex) const glTexParameteri(tex.target, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); //sensible default } - if (tex.lodBias != 0.0f) - glTexParameterf(tex.target, GL_TEXTURE_LOD_BIAS, tex.lodBias); + glTexParameterf(tex.target, GL_TEXTURE_LOD_BIAS, tex.lodBias); if (tex.aniso != 0.0f && GLAD_GL_EXT_texture_filter_anisotropic) glTexParameterf(tex.target, GL_TEXTURE_MAX_ANISOTROPY_EXT, std::clamp(tex.aniso, 1.0f, globalRendering->maxTexAnisoLvl));