From 13a7b7d4b2bc277c3982ed277311344d9fe055b5 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Fri, 3 Jan 2020 18:34:43 +0100 Subject: [PATCH] Hook up gl_ssao --- source/common/gamecvars.h | 1 + .../gl/renderer/gl_renderbuffers.cpp | 3 ++- .../rendering/gl/system/gl_framebuffer.cpp | 26 ++++++++++++++++++- source/glbackend/glbackend.cpp | 3 ++- wadsrc/static/engine/shaders/glsl/polymost.fp | 8 ++++++ wadsrc/static/engine/shaders/glsl/polymost.vp | 2 ++ 6 files changed, 40 insertions(+), 3 deletions(-) diff --git a/source/common/gamecvars.h b/source/common/gamecvars.h index a31d8b741..61ae4f9c9 100644 --- a/source/common/gamecvars.h +++ b/source/common/gamecvars.h @@ -83,6 +83,7 @@ EXTERN_CVAR(Float, vid_gamma) EXTERN_CVAR(Float, vid_contrast) EXTERN_CVAR(Float, vid_brightness) EXTERN_CVAR(Int, gl_multisample) +EXTERN_CVAR(Int, gl_ssao) EXTERN_CVAR(Bool, in_joystick) EXTERN_CVAR(Int, in_mouse) diff --git a/source/common/rendering/gl/renderer/gl_renderbuffers.cpp b/source/common/rendering/gl/renderer/gl_renderbuffers.cpp index 7547075be..37617cee8 100644 --- a/source/common/rendering/gl/renderer/gl_renderbuffers.cpp +++ b/source/common/rendering/gl/renderer/gl_renderbuffers.cpp @@ -128,6 +128,7 @@ void FGLRenderBuffers::Setup(int width, int height, int sceneWidth, int sceneHei I_FatalError("Requested invalid render buffer sizes: screen = %dx%d", width, height); int samples = clamp((int)gl_multisample, 0, mMaxSamples); + bool needsSceneTextures = (gl_ssao != 0); GLint activeTex; GLint textureBinding; @@ -139,7 +140,7 @@ void FGLRenderBuffers::Setup(int width, int height, int sceneWidth, int sceneHei CreatePipeline(width, height); if (width != mWidth || height != mHeight || mSamples != samples) - CreateScene(width, height, samples, false); + CreateScene(width, height, samples, needsSceneTextures); mWidth = width; mHeight = height; diff --git a/source/common/rendering/gl/system/gl_framebuffer.cpp b/source/common/rendering/gl/system/gl_framebuffer.cpp index f5cf18d38..047e116a1 100644 --- a/source/common/rendering/gl/system/gl_framebuffer.cpp +++ b/source/common/rendering/gl/system/gl_framebuffer.cpp @@ -65,6 +65,7 @@ EXTERN_CVAR (Bool, vid_vsync) EXTERN_CVAR(Bool, r_drawvoxels) EXTERN_CVAR(Int, gl_tonemap) EXTERN_CVAR(Bool, gl_texture_usehires) +EXTERN_CVAR(Int, gl_ssao) void gl_LoadExtensions(); void gl_PrintStartupLog(); @@ -431,6 +432,21 @@ void OpenGLFrameBuffer::PostProcessScene(int fixedcm, const std::functionAmbientOccludeScene(GLInterface.GetMatrix(Matrix_Projection).get()[5]); + glViewport(screen->mSceneViewport.left, screen->mSceneViewport.top, screen->mSceneViewport.width, screen->mSceneViewport.height); + OpenGLRenderer::GLRenderer->mBuffers->BindSceneFB(true); + glDrawBuffers(3, buffers); + + // To do: the translucent part of the scene should be drawn here + + glDrawBuffers(1, buffers); + } + OpenGLRenderer::GLRenderer->mBuffers->BlitSceneToTexture(); // Copy the resulting scene to the current post process texture screen->PostProcessScene(0, []() { GLInterface.Draw2D(&twodpsp); // draws the weapon sprites @@ -438,7 +454,15 @@ void videoShowFrame(int32_t w) screen->Update(); // After finishing the frame, reset everything for the next frame. This needs to be done better. screen->BeginFrame(); - OpenGLRenderer::GLRenderer->mBuffers->BindSceneFB(false); + if (gl_ssao) + { + OpenGLRenderer::GLRenderer->mBuffers->BindSceneFB(true); + glDrawBuffers(3, buffers); + } + else + { + OpenGLRenderer::GLRenderer->mBuffers->BindSceneFB(false); + } twodpsp.Clear(); twodgen.Clear(); } diff --git a/source/glbackend/glbackend.cpp b/source/glbackend/glbackend.cpp index f92a5d09c..82bce0a56 100644 --- a/source/glbackend/glbackend.cpp +++ b/source/glbackend/glbackend.cpp @@ -172,7 +172,8 @@ void GLInstance::InitGLState(int fogmode, int multisample) // This is a bad place to call this but without deconstructing the entire render loops in all front ends there is no way to have a well defined spot for this stuff. // Before doing that the backend needs to work in some fashion, so we have to make sure everything is set up when the first render call is performed. screen->BeginFrame(); - OpenGLRenderer::GLRenderer->mBuffers->BindSceneFB(false); + bool useSSAO = (gl_ssao != 0); + OpenGLRenderer::GLRenderer->mBuffers->BindSceneFB(useSSAO); } void GLInstance::Deinit() diff --git a/wadsrc/static/engine/shaders/glsl/polymost.fp b/wadsrc/static/engine/shaders/glsl/polymost.fp index 989ddcf47..ff1d73a7a 100644 --- a/wadsrc/static/engine/shaders/glsl/polymost.fp +++ b/wadsrc/static/engine/shaders/glsl/polymost.fp @@ -48,6 +48,7 @@ in float v_distance; in vec4 v_texCoord; in vec4 v_detailCoord; in float v_fogCoord; +in vec4 v_eyeCoordPosition; const float c_basepalScale = 255.0/256.0; const float c_basepalOffset = 0.5/256.0; @@ -59,6 +60,8 @@ const vec4 c_vec4_one = vec4(c_one); const float c_wrapThreshold = 0.9; layout(location=0) out vec4 fragColor; +layout(location=1) out vec4 fragFog; +layout(location=2) out vec4 fragNormal; //=========================================================================== // @@ -232,4 +235,9 @@ void main() color.rgb = pow(color.rgb, vec3(u_brightness)); fragColor = color; + fragFog = vec4(0.0, 0.0, 0.0, 1.0); // Does build have colored fog? + vec3 normal = normalize(cross(dFdx(v_eyeCoordPosition.xyz), dFdy(v_eyeCoordPosition.xyz))); + normal.x = -normal.x; + normal.y = -normal.y; + fragNormal = vec4(normal * 0.5 + 0.5, 1.0); } diff --git a/wadsrc/static/engine/shaders/glsl/polymost.vp b/wadsrc/static/engine/shaders/glsl/polymost.vp index 6ba731b59..b8b0b8f17 100644 --- a/wadsrc/static/engine/shaders/glsl/polymost.vp +++ b/wadsrc/static/engine/shaders/glsl/polymost.vp @@ -5,6 +5,7 @@ out float v_distance; out vec4 v_texCoord; out vec4 v_detailCoord; out float v_fogCoord; +out vec4 v_eyeCoordPosition; uniform float u_usePalette; uniform mat4 u_rotMatrix; @@ -25,6 +26,7 @@ void main() { vec4 vertex = u_rotMatrix * i_vertPos; vec4 eyeCoordPosition = u_modelMatrix * vertex; + v_eyeCoordPosition = eyeCoordPosition; gl_Position = u_projectionMatrix * eyeCoordPosition; eyeCoordPosition.xyz /= eyeCoordPosition.w;