Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/pr-changelogs/3482.md
Original file line number Diff line number Diff line change
@@ -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.
21 changes: 16 additions & 5 deletions rts/Lua/LuaFBOs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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, &currentFBO);
GLint currentReadFBO;
GLint currentDrawFBO;
glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING_EXT, &currentReadFBO);
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING_EXT, &currentDrawFBO);

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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down
13 changes: 8 additions & 5 deletions rts/Lua/LuaOpenGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down Expand Up @@ -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, &currentFBO);
}
GLint currentReadFBO = 0;
GLint currentDrawFBO = 0;
glGetIntegerv(GL_READ_FRAMEBUFFER_BINDING_EXT, &currentReadFBO);
glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING_EXT, &currentDrawFBO);

glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, tex->fbo);

Expand All @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions rts/Lua/LuaOpenGLUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const void*>(textures.GetIdx(image));
texUnit.data = reinterpret_cast<const void*>(texIdx);
} break;

case '%': {
Expand Down
10 changes: 8 additions & 2 deletions rts/Lua/LuaTextures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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));
Expand Down
Loading