From 960038bb81631f70d4afb55fb7c0a3beaffcbd79 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Fri, 26 Aug 2016 01:36:21 +0200 Subject: [PATCH 1/3] Clear global state tracking variables when the OpenGL context is (re)created --- src/gl/renderer/gl_renderstate.cpp | 25 +++++++++++++++++++++++++ src/gl/system/gl_framebuffer.cpp | 5 +++++ src/gl/textures/gl_hwtexture.h | 4 ++-- src/gl/textures/gl_material.cpp | 6 ++++++ src/gl/textures/gl_material.h | 1 + 5 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/gl/renderer/gl_renderstate.cpp b/src/gl/renderer/gl_renderstate.cpp index 83303d61e7..56b319f008 100644 --- a/src/gl/renderer/gl_renderstate.cpp +++ b/src/gl/renderer/gl_renderstate.cpp @@ -80,19 +80,44 @@ void FRenderState::Reset() mDstBlend = GL_ONE_MINUS_SRC_ALPHA; mAlphaThreshold = 0.5f; mBlendEquation = GL_FUNC_ADD; + mModelMatrixEnabled = false; + mTextureMatrixEnabled = false; mObjectColor = 0xffffffff; mVertexBuffer = mCurrentVertexBuffer = NULL; mColormapState = CM_DEFAULT; + mSoftLight = 0; + mLightParms[0] = mLightParms[1] = mLightParms[2] = 0.0f; mLightParms[3] = -1.f; mSpecialEffect = EFF_NONE; mClipHeight = 0.f; mClipHeightDirection = 0.f; + mShaderTimer = 0.0f; ClearClipSplit(); stSrcBlend = stDstBlend = -1; stBlendEquation = -1; stAlphaThreshold = -1.f; + stAlphaTest = 0; mLastDepthClamp = true; + mInterpolationFactor = 0.0f; + + mColor.Set(0.0f, 0.0f, 0.0f, 0.0f); + mCameraPos.Set(0.0f, 0.0f, 0.0f, 0.0f); + mGlowTop.Set(0.0f, 0.0f, 0.0f, 0.0f); + mGlowBottom.Set(0.0f, 0.0f, 0.0f, 0.0f); + mGlowTopPlane.Set(0.0f, 0.0f, 0.0f, 0.0f); + mGlowBottomPlane.Set(0.0f, 0.0f, 0.0f, 0.0f); + mSplitTopPlane.Set(0.0f, 0.0f, 0.0f, 0.0f); + mSplitBottomPlane.Set(0.0f, 0.0f, 0.0f, 0.0f); + mClipLine.Set(0.0f, 0.0f, 0.0f, 0.0f); + mDynColor.Set(0.0f, 0.0f, 0.0f, 0.0f); + mClipSplit[0] = mClipSplit[1] = 0.0f; + mEffectState = 0; + activeShader = nullptr; + mProjectionMatrix.loadIdentity(); + mViewMatrix.loadIdentity(); + mModelMatrix.loadIdentity(); + mTextureMatrix.loadIdentity(); } //========================================================================== diff --git a/src/gl/system/gl_framebuffer.cpp b/src/gl/system/gl_framebuffer.cpp index 6d56c1b27f..ec8b3f599e 100644 --- a/src/gl/system/gl_framebuffer.cpp +++ b/src/gl/system/gl_framebuffer.cpp @@ -99,6 +99,11 @@ OpenGLFrameBuffer::OpenGLFrameBuffer(void *hMonitor, int width, int height, int // If wglSwapIntervalEXT is called after glBindFramebuffer in a frame the setting is not changed! SetVSync(vid_vsync); + // Make sure all global variables tracking OpenGL context state are reset.. + FHardwareTexture::InitGlobalState(); + FMaterial::InitGlobalState(); + gl_RenderState.Reset(); + GLRenderer = new FGLRenderer(this); memcpy (SourcePalette, GPalette.BaseColors, sizeof(PalEntry)*256); UpdatePalette (); diff --git a/src/gl/textures/gl_hwtexture.h b/src/gl/textures/gl_hwtexture.h index 9b7028f8cd..4c00af272c 100644 --- a/src/gl/textures/gl_hwtexture.h +++ b/src/gl/textures/gl_hwtexture.h @@ -48,11 +48,11 @@ private: public: static unsigned int lastbound[MAX_TEXTURES]; - static int lastactivetexture; - static int max_texturesize; static int GetTexDimension(int value); + static void InitGlobalState() { for (int i = 0; i < MAX_TEXTURES; i++) lastbound[i] = 0; } + private: short texwidth, texheight; diff --git a/src/gl/textures/gl_material.cpp b/src/gl/textures/gl_material.cpp index 8e0ab73e69..6ba1fe4b0c 100644 --- a/src/gl/textures/gl_material.cpp +++ b/src/gl/textures/gl_material.cpp @@ -669,6 +669,12 @@ static FMaterial *last; static int lastclamp; static int lasttrans; +void FMaterial::InitGlobalState() +{ + last = nullptr; + lastclamp = 0; + lasttrans = 0; +} void FMaterial::Bind(int clampmode, int translation) { diff --git a/src/gl/textures/gl_material.h b/src/gl/textures/gl_material.h index dea0d4cf57..d44a845f6f 100644 --- a/src/gl/textures/gl_material.h +++ b/src/gl/textures/gl_material.h @@ -263,6 +263,7 @@ public: static FMaterial *ValidateTexture(FTextureID no, bool expand, bool trans); static void ClearLastTexture(); + static void InitGlobalState(); }; #endif From b68bbaf617974d669d3cd13c69efcfdbdf4bd1fb Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Fri, 26 Aug 2016 01:40:28 +0200 Subject: [PATCH 2/3] Fix uninitialized memory access if a FShaderProgram is destroyed without being fully compiled and linked --- src/gl/shaders/gl_shaderprogram.cpp | 6 ++++++ src/gl/shaders/gl_shaderprogram.h | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/src/gl/shaders/gl_shaderprogram.cpp b/src/gl/shaders/gl_shaderprogram.cpp index 63ce092614..959315a9d1 100644 --- a/src/gl/shaders/gl_shaderprogram.cpp +++ b/src/gl/shaders/gl_shaderprogram.cpp @@ -52,6 +52,12 @@ #include "i_system.h" #include "doomerrors.h" +FShaderProgram::FShaderProgram() +{ + for (int i = 0; i < NumShaderTypes; i++) + mShaders[i] = 0; +} + //========================================================================== // // Free shader program resources diff --git a/src/gl/shaders/gl_shaderprogram.h b/src/gl/shaders/gl_shaderprogram.h index 16684ddff5..64a36db494 100644 --- a/src/gl/shaders/gl_shaderprogram.h +++ b/src/gl/shaders/gl_shaderprogram.h @@ -6,6 +6,7 @@ class FShaderProgram { public: + FShaderProgram(); ~FShaderProgram(); enum ShaderType @@ -30,6 +31,9 @@ public: static void PatchFragShader(FString &code); private: + FShaderProgram(const FShaderProgram &) = delete; + FShaderProgram &operator=(const FShaderProgram &) = delete; + static FString PatchShader(ShaderType type, const FString &code, const char *defines, int maxGlslVersion); static void PatchCommon(FString &code); From 5f09d3b7ef7b8915d9e69b310fa2bf12fd5b4ce6 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Fri, 26 Aug 2016 01:46:39 +0200 Subject: [PATCH 3/3] Add FBufferedUniformSampler because sampler uniforms may default bind to other locations than zero --- src/gl/shaders/gl_bloomshader.h | 4 ++-- src/gl/shaders/gl_lensshader.h | 2 +- src/gl/shaders/gl_presentshader.h | 2 +- src/gl/shaders/gl_shader.h | 22 ++++++++++++++++++++++ src/gl/shaders/gl_tonemapshader.h | 4 ++-- 5 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/gl/shaders/gl_bloomshader.h b/src/gl/shaders/gl_bloomshader.h index cbc740ab60..b20277a42d 100644 --- a/src/gl/shaders/gl_bloomshader.h +++ b/src/gl/shaders/gl_bloomshader.h @@ -8,7 +8,7 @@ class FBloomExtractShader public: void Bind(); - FBufferedUniform1i SceneTexture; + FBufferedUniformSampler SceneTexture; FBufferedUniform1f Exposure; FBufferedUniform2f Scale; FBufferedUniform2f Offset; @@ -22,7 +22,7 @@ class FBloomCombineShader public: void Bind(); - FBufferedUniform1i BloomTexture; + FBufferedUniformSampler BloomTexture; private: FShaderProgram mShader; diff --git a/src/gl/shaders/gl_lensshader.h b/src/gl/shaders/gl_lensshader.h index ef0810e4ea..7e9ef63926 100644 --- a/src/gl/shaders/gl_lensshader.h +++ b/src/gl/shaders/gl_lensshader.h @@ -8,7 +8,7 @@ class FLensShader public: void Bind(); - FBufferedUniform1i InputTexture; + FBufferedUniformSampler InputTexture; FBufferedUniform1f AspectRatio; FBufferedUniform1f Scale; FBufferedUniform4f LensDistortionCoefficient; diff --git a/src/gl/shaders/gl_presentshader.h b/src/gl/shaders/gl_presentshader.h index b54fa2e36e..6f4e899bb7 100644 --- a/src/gl/shaders/gl_presentshader.h +++ b/src/gl/shaders/gl_presentshader.h @@ -8,7 +8,7 @@ class FPresentShader public: void Bind(); - FBufferedUniform1i InputTexture; + FBufferedUniformSampler InputTexture; FBufferedUniform1f InvGamma; FBufferedUniform1f Contrast; FBufferedUniform1f Brightness; diff --git a/src/gl/shaders/gl_shader.h b/src/gl/shaders/gl_shader.h index 75e4b4e5e5..52ca59ec78 100644 --- a/src/gl/shaders/gl_shader.h +++ b/src/gl/shaders/gl_shader.h @@ -201,6 +201,28 @@ public: } }; +class FBufferedUniformSampler +{ + int mBuffer; + int mIndex; + +public: + void Init(GLuint hShader, const GLchar *name) + { + mIndex = glGetUniformLocation(hShader, name); + mBuffer = -1; + } + + void Set(int newvalue) + { + if (newvalue != mBuffer) + { + mBuffer = newvalue; + glUniform1i(mIndex, newvalue); + } + } +}; + class FShader { diff --git a/src/gl/shaders/gl_tonemapshader.h b/src/gl/shaders/gl_tonemapshader.h index 9d427713fe..7ec24117b1 100644 --- a/src/gl/shaders/gl_tonemapshader.h +++ b/src/gl/shaders/gl_tonemapshader.h @@ -8,9 +8,9 @@ class FTonemapShader public: void Bind(); - FBufferedUniform1i SceneTexture; + FBufferedUniformSampler SceneTexture; FBufferedUniform1f Exposure; - FBufferedUniform1i PaletteLUT; + FBufferedUniformSampler PaletteLUT; static bool IsPaletteMode();