[Fix] (Diligent/Framebuffer): Correct GL_FRAMEBUFFER target handling.

This commit is contained in:
BZLZHH
2025-06-21 02:02:51 +08:00
parent 5d6bf42397
commit 6992c541af
@@ -333,24 +333,10 @@ namespace MG_GL::GL {
MG_Util::Debug::LogE("Framebuffer bind error: %s", MG_Util::Debug::GLEnumToString(result));
}
void FramebufferTexture2D(GLenum target, GLenum attachment, GLenum textarget,
GLuint texture, GLint level) {
MG_Util::Debug::LogD("glFramebufferTexture2D, target: %s, attach: %s, textarget: %s, tex: %u, level: %d",
MG_Util::Debug::GLEnumToString(target),
MG_Util::Debug::GLEnumToString(attachment),
MG_Util::Debug::GLEnumToString(textarget),
texture, level);
GLuint currentFB = MG_State_T::framebufferState->currentBindings_[target];
GLenum result = MG_State::AttachTexture2DToFramebuffer(
target, attachment, textarget, texture, level
);
if (result == GL_NO_ERROR) {
auto it = MG_Diligent::g_FramebufferMap.find(currentFB);
void AttachTexture2DToFramebuffer(GLuint fbo, GLuint texture, GLenum attachment, GLint level) {
auto it = MG_Diligent::g_FramebufferMap.find(fbo);
if (it == MG_Diligent::g_FramebufferMap.end()) {
MG_Util::Debug::LogE("Framebuffer %u not found in map", currentFB);
MG_Util::Debug::LogE("Framebuffer %u not found in map", fbo);
return;
}
@@ -383,6 +369,14 @@ namespace MG_GL::GL {
if (index < fbInfo.ColorRTVs.size() && fbInfo.ColorRTVs[index]) {
fbInfo.ColorRTVs[index]->Release();
fbInfo.ColorRTVs[index] = nullptr;
bool allNull = true;
for (const auto& rtv : fbInfo.ColorRTVs) {
if (rtv != nullptr) {
allNull = false;
break;
}
}
if (allNull) fbInfo.ColorRTVs.clear();
MG_Util::Debug::LogD("Released color attachment %zu", index);
}
}
@@ -408,6 +402,7 @@ namespace MG_GL::GL {
if (fbInfo.pDepthStencilRTV) {
fbInfo.pDepthStencilRTV->Release();
fbInfo.pDepthStencilRTV = nullptr;
MG_Util::Debug::LogD("Released existing depth/stencil attachment");
}
@@ -432,6 +427,7 @@ namespace MG_GL::GL {
if (fbInfo.ColorRTVs[index]) {
fbInfo.ColorRTVs[index]->Release();
fbInfo.ColorRTVs[index] = nullptr;
MG_Util::Debug::LogD("Released existing color attachment %zu", index);
}
@@ -445,8 +441,51 @@ namespace MG_GL::GL {
}
}
}
}
MG_Util::Debug::LogD("FramebufferTexture2D completed successfully");
void FramebufferTexture2D(GLenum target, GLenum attachment, GLenum textarget,
GLuint texture, GLint level) {
MG_Util::Debug::LogD("glFramebufferTexture2D, target: %s, attach: %s, textarget: %s, tex: %u, level: %d",
MG_Util::Debug::GLEnumToString(target),
MG_Util::Debug::GLEnumToString(attachment),
MG_Util::Debug::GLEnumToString(textarget),
texture,
level);
MG_Util::Debug::LogD(" Current READ_FRAMEBUFFER binding: %u", MG_State_T::framebufferState->currentBindings_[GL_READ_FRAMEBUFFER]);
MG_Util::Debug::LogD(" Current DRAW_FRAMEBUFFER binding: %u", MG_State_T::framebufferState->currentBindings_[GL_DRAW_FRAMEBUFFER]);
GLenum result = MG_State::AttachTexture2DToFramebuffer(
target, attachment, textarget, texture, level
);
if (result == GL_NO_ERROR) {
if (target == GL_FRAMEBUFFER) {
if (MG_State_T::framebufferState->currentBindings_[GL_READ_FRAMEBUFFER] == MG_State_T::framebufferState->currentBindings_[GL_DRAW_FRAMEBUFFER]) {
MG_Util::Debug::LogD(" Target is GL_FRAMEBUFFER and READ/DRAW bindings are the same (%u). Applying to GL_DRAW_FRAMEBUFFER.", MG_State_T::framebufferState->currentBindings_[GL_DRAW_FRAMEBUFFER]);
target = GL_DRAW_FRAMEBUFFER;
} else {
MG_Util::Debug::LogD(" Target is GL_FRAMEBUFFER and READ/DRAW bindings differ. Applying to both if non-zero.");
if (MG_State_T::framebufferState->currentBindings_[GL_READ_FRAMEBUFFER] != 0) {
MG_Util::Debug::LogD(" Attaching to READ_FRAMEBUFFER: %u", MG_State_T::framebufferState->currentBindings_[GL_READ_FRAMEBUFFER]);
AttachTexture2DToFramebuffer(MG_State_T::framebufferState->currentBindings_[GL_READ_FRAMEBUFFER],
texture, attachment, level);
}
if (MG_State_T::framebufferState->currentBindings_[GL_DRAW_FRAMEBUFFER] != 0) {
MG_Util::Debug::LogD(" Attaching to DRAW_FRAMEBUFFER: %u", MG_State_T::framebufferState->currentBindings_[GL_DRAW_FRAMEBUFFER]);
AttachTexture2DToFramebuffer(MG_State_T::framebufferState->currentBindings_[GL_DRAW_FRAMEBUFFER],
texture, attachment, level);
}
MG_Util::Debug::LogD("FramebufferTexture2D completed successfully after separate READ/DRAW attachments.");
return;
}
}
GLuint fboToModify = MG_State_T::framebufferState->currentBindings_[target];
MG_Util::Debug::LogD(" Target is %s. Applying to bound FBO: %u",
MG_Util::Debug::GLEnumToString(target), fboToModify);
AttachTexture2DToFramebuffer(fboToModify,
texture, attachment, level);
MG_Util::Debug::LogD("FramebufferTexture2D completed successfully for target %s (FBO %u).", MG_Util::Debug::GLEnumToString(target), fboToModify);
return;
}
MG_State::SetError(result);