[Fix] (Diligent/Texture): Fix glTexSubImage2D crashing.

This commit is contained in:
BZLZHH
2025-06-07 22:14:34 +08:00
parent 2caade5a36
commit ad6b9c1ca7
@@ -440,16 +440,36 @@ namespace MG_GL::GL {
GLuint boundTextureID = unitState->GetBoundTexture(target); GLuint boundTextureID = unitState->GetBoundTexture(target);
if (boundTextureID == 0) { if (boundTextureID == 0) {
MG_Util::Debug::LogD("TexSubImage2D: No texture bound. Skipping update."); MG_Util::Debug::LogD("TexSubImage2D: No texture bound to active unit. Skipping update.");
return; return;
} }
Diligent::ITexture* pTexture = MG_Diligent::g_TextureMap[boundTextureID]; Diligent::ITexture* pTexture = nullptr;
auto texIt = MG_Diligent::g_TextureMap.find(boundTextureID);
if (texIt != MG_Diligent::g_TextureMap.end()) {
pTexture = texIt->second;
}
if (!pTexture) { if (!pTexture) {
MG_Util::Debug::LogW("TexSubImage2D: Diligent texture not found for GL name %u", boundTextureID); MG_Util::Debug::LogW("TexSubImage2D: Diligent texture not found for GL name %u", boundTextureID);
return; return;
} }
const auto& texDesc = pTexture->GetDesc();
if (!(texDesc.BindFlags & Diligent::BIND_SHADER_RESOURCE)) {
MG_Util::Debug::LogE("TexSubImage2D: Texture %u does not have BIND_SHADER_RESOURCE flag", boundTextureID);
return;
}
Diligent::TextureDesc desc = pTexture->GetDesc();
if (xoffset < 0 || yoffset < 0 ||
(xoffset + width) > desc.Width ||
(yoffset + height) > desc.Height) {
MG_Util::Debug::LogE("TexSubImage2D: Update region out of bounds: [%d, %d, %d, %d] vs texture size [%u, %u]",
xoffset, yoffset, width, height, desc.Width, desc.Height);
return;
}
Diligent::TextureSubResData SubResData; Diligent::TextureSubResData SubResData;
SubResData.pData = pixels; SubResData.pData = pixels;
SubResData.Stride = width * GetBytesPerPixel(format, type); SubResData.Stride = width * GetBytesPerPixel(format, type);
@@ -459,21 +479,31 @@ namespace MG_GL::GL {
UpdateBox.MaxX = xoffset + width; UpdateBox.MaxX = xoffset + width;
UpdateBox.MinY = yoffset; UpdateBox.MinY = yoffset;
UpdateBox.MaxY = yoffset + height; UpdateBox.MaxY = yoffset + height;
UpdateBox.MinZ = 0;
UpdateBox.MaxZ = 1;
MG_Util::Debug::LogD("TexSubImage2D: Updating region [%d,%d]-[%d,%d] at level %d", MG_Util::Debug::LogD("TexSubImage2D: Updating region [%d,%d]-[%d,%d] at level %d",
xoffset, yoffset, xoffset+width, yoffset+height, level); xoffset, yoffset, xoffset+width, yoffset+height, level);
if (MG_Diligent::IsInRenderPass) {
MG_Diligent::g_pContext->EndRenderPass();
MG_Diligent::IsInRenderPass = false;
}
MG_Diligent::g_pContext->UpdateTexture( MG_Diligent::g_pContext->UpdateTexture(
pTexture, pTexture,
level, level,
0, 0,
UpdateBox, UpdateBox,
SubResData, SubResData,
Diligent::RESOURCE_STATE_TRANSITION_MODE_TRANSITION, Diligent::RESOURCE_STATE_TRANSITION_MODE_TRANSITION,
Diligent::RESOURCE_STATE_TRANSITION_MODE_TRANSITION Diligent::RESOURCE_STATE_TRANSITION_MODE_TRANSITION
); );
MG_Util::Debug::LogD("TexSubImage2D: Update completed successfully");
} }
void GetTexLevelParameteriv(GLenum target, GLint level, GLenum pname, GLint* params) { void GetTexLevelParameteriv(GLenum target, GLint level, GLenum pname, GLint* params) {
MG_Util::Debug::LogD("glGetTexLevelParameteriv, target: %d, level: %d, pname: %d, params: %p", MG_Util::Debug::LogD("glGetTexLevelParameteriv, target: %d, level: %d, pname: %d, params: %p",
target, level, pname, params); target, level, pname, params);