From 07bd9c06aa2823cf056757534b6ffa27ddd34137 Mon Sep 17 00:00:00 2001 From: RSDuck Date: Mon, 17 Apr 2023 20:53:41 +0200 Subject: [PATCH] fix UB also misc changes to use more unsigned multiplication also fix framebuffer resize --- src/GPU3D_Compute.cpp | 6 ++++-- src/GPU3D_Compute.h | 2 +- src/GPU3D_Compute_shaders.h | 16 ++++++++-------- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/GPU3D_Compute.cpp b/src/GPU3D_Compute.cpp index 5421708e..bfb10e92 100644 --- a/src/GPU3D_Compute.cpp +++ b/src/GPU3D_Compute.cpp @@ -89,7 +89,6 @@ bool ComputeRenderer::Init() glGenBuffers(1, &TileMemory); glGenTextures(1, &YSpanIndicesTexture); - glGenTextures(1, &Framebuffer); glGenTextures(1, &LowResFramebuffer); glBindTexture(GL_TEXTURE_2D, LowResFramebuffer); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8UI, 256, 192); @@ -230,6 +229,9 @@ void ComputeRenderer::SetRenderSettings(GPU::RenderSettings& settings) glBindBuffer(GL_SHADER_STORAGE_BUFFER, BinResultMemory); glBufferData(GL_SHADER_STORAGE_BUFFER, binResultSize, nullptr, GL_DYNAMIC_DRAW); + if (Framebuffer != 0) + glDeleteTextures(1, &Framebuffer); + glGenTextures(1, &Framebuffer); glBindTexture(GL_TEXTURE_2D, Framebuffer); glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA8, ScreenWidth, ScreenHeight); @@ -1391,7 +1393,7 @@ void ComputeRenderer::RenderFrame() for (int i = 0; i < numVariants; i++) { GLuint shader = 0; - if (variants[i].Texture == -1) + if (variants[i].Texture == 0) { shader = shadersNoTexture[variants[i].BlendMode]; } diff --git a/src/GPU3D_Compute.h b/src/GPU3D_Compute.h index 7a3af101..fc1b7c00 100644 --- a/src/GPU3D_Compute.h +++ b/src/GPU3D_Compute.h @@ -213,7 +213,7 @@ private: u32 TextureDecodingBuffer[1024*1024]; - GLuint Framebuffer; + GLuint Framebuffer = 0; GLuint LowResFramebuffer; GLuint PixelBuffer; diff --git a/src/GPU3D_Compute_shaders.h b/src/GPU3D_Compute_shaders.h index 26e7adeb..65ec4f12 100644 --- a/src/GPU3D_Compute_shaders.h +++ b/src/GPU3D_Compute_shaders.h @@ -299,11 +299,11 @@ uint Div64_32_32(uint numHi, uint numLo, uint den) // there's a 1 in the MSB. // We also shift numer by the same amount. This cannot overflow because numHi < den. // The expression (-shift & 63) is the same as (64 - shift), except it avoids the UB of shifting - // by 64. <---- in C. I'm pretty sure shifts are masked in GLSL, but whatever. + // by 64. (it's also UB in GLSL!!!!) uint shift = 31 - findMSB(den); den <<= shift; numHi <<= shift; - numHi |= (numLo >> (-shift & 63U)) & uint(-int(shift) >> 63); + numHi |= (numLo >> (-shift & 31U)) & uint(-int(shift) >> 31); numLo <<= shift; // Extract the low digits of the numerator and both digits of the denominator. @@ -347,12 +347,12 @@ int CalcYFactorY(YSpanSetup span, int i) /* maybe it would be better to do use a 32x32=64 multiplication? */ - uint numLo = abs(i * span.W0n); + uint numLo = uint(abs(i)) * uint(span.W0n); uint numHi = 0U; - numHi |= numLo >> (32-YFactorShift); + numHi |= numLo >> (32U-YFactorShift); numLo <<= YFactorShift; - uint den = abs(i * span.W0d + (span.I1 - span.I0 - i) * span.W1d); + uint den = uint(abs(i)) * uint(span.W0d) + uint(abs(span.I1 - span.I0 - i)) * span.W1d; if (den == 0) { @@ -370,12 +370,12 @@ int CalcYFactorX(XSpanSetup span, int x) if (span.X0 != span.X1) { - uint numLo = uint(x) * span.W0; + uint numLo = uint(x) * uint(span.W0); uint numHi = 0U; - numHi |= numLo >> (32-YFactorShift); + numHi |= numLo >> (32U-YFactorShift); numLo <<= YFactorShift; - uint den = (uint(x) * span.W0) + (uint(span.X1 - span.X0 - x) * span.W1); + uint den = uint(x) * uint(span.W0) + uint(span.X1 - span.X0 - x) * uint(span.W1); if (den == 0) return 0;