Skip to content

Conversation

VReaperV
Copy link
Contributor

@VReaperV VReaperV commented Mar 11, 2025

Requires #1586

Changes GLShader constructors, removing unnecessary stuff from them, and making it only have 2 distinct constructors - for rendering shaders and compute shaders.

Changes the relevant shaders to use a new screenSpace shader, which is almost a no-op.

NUKE the the other .glsl shaders that just repeat the same functionality.

Also NUKES the u_ModelViewProjectionMatrix from them, because the result is always a full-screen quad anyway.

Adds Tess_InstantScreenSpaceQuad() for use with this, because with some other shaders we still do need u_ModelViewProjectionMatrix. This actually draws a single triangle that gets clipped to the screen (this has a good explanation why).

The screen-space shaders in this pr are ones that always process the whole screen, and where the vertex shaders were the same anyway, while sending the u_ModelViewProjectionMatrix uniform and multiplying the vertex position with it, and, indeed, even having a VBO for them at all, was useless.

I didn't touch the screen shader here, although it really is just another screen-space shader. It is, however, also used for ST_SCREENMAP stage, which I don't think we use anywhere, and seems to be intended for use on something like an in-game camera screen. That wouldn't work properly though, due to how the texture coordinates are calculated (relative to the screen position of the fragment, not the world/model position). We could probably just use the generic shader for that.

Also cleaned-up the formatting and ordering of shaders/shader files.

This brings down the total amount of shader compilation, shader program linking (with #1587), and shader files.

@VReaperV VReaperV added T-Improvement Improvement for an existing feature A-Renderer T-Cleanup labels Mar 11, 2025
@illwieckz
Copy link
Member

Wow, all those copy-pasted shaders looked like cargo cult. Thank you very much for deduplicating that!

@VReaperV VReaperV force-pushed the shader-screenspace branch from 386e4c8 to 461af9a Compare March 12, 2025 17:43
@VReaperV
Copy link
Contributor Author

Wow, all those copy-pasted shaders looked like cargo cult. Thank you very much for deduplicating that!

Yeah, it's been on my mind for some time now, especially as I keep needing to either re-build existing shaders with different graphics settings, or create new ones.

@VReaperV
Copy link
Contributor Author

I've also added a couple more commits with some clean-up.

@VReaperV VReaperV force-pushed the shader-screenspace branch from 6a89a29 to e14fc72 Compare March 12, 2025 21:08
@VReaperV VReaperV changed the title Improve scren-space shaders, some minor clean-up Improve screen-space shaders, some minor clean-up Mar 12, 2025
@VReaperV VReaperV force-pushed the shader-screenspace branch 4 times, most recently from 7714ca5 to 5c823f8 Compare March 14, 2025 15:36
@slipher
Copy link
Member

slipher commented Mar 15, 2025

GLSL doesn't build

^3Warn: Compile log:
0(180) : error C1503: undefined variable "outputColor"

^3Warn: Unhandled exception (15ShaderException): Couldn't compile fragment shader: cameraEffects

@VReaperV VReaperV force-pushed the shader-screenspace branch from 5c823f8 to e0f0efc Compare March 15, 2025 08:57
@VReaperV
Copy link
Contributor Author

GLSL doesn't build

^3Warn: Compile log:
0(180) : error C1503: undefined variable "outputColor"

^3Warn: Unhandled exception (15ShaderException): Couldn't compile fragment shader: cameraEffects

Should be fixed now.

@VReaperV VReaperV force-pushed the shader-screenspace branch from e0f0efc to 10fc03d Compare March 15, 2025 21:42
@slipher
Copy link
Member

slipher commented Mar 16, 2025

I'm getting crashes when enabling bloom.

@VReaperV
Copy link
Contributor Author

I'm getting crashes when enabling bloom.

Fixed now.

Changes `GLShader` constructors, removing unnecessary stuff from them, and making it only have 2 distinct constructors - for rendering shaders and compute shaders.

Changes the relevant shaders to use a new `screenSpace` shader, which is almost a no-op.

NUKE the the other .glsl shaders that just repeat the same functionality.

Also NUKES the `u_ModelViewProjectionMatrix` from them, because the result is always a full-screen quad anyway.

Adds `Tess_InstantScreenSpaceQuad()` for use with this, because with some other shaders we still do need `u_ModelViewProjectionMatrix`.
Also NUKE `u_ModelViewProjectionMatrix` and `u_TextureMatrix`, because they have no effect there.
Fix screen-space shaders. Use a triangle IBO with vertexes being set directly in the vertex shader based on `gl_VertexID`.

Re-order the shader stuff that was becoming a bit of a mess.
@VReaperV VReaperV force-pushed the shader-screenspace branch from f0bf2a3 to a67058e Compare March 16, 2025 20:22
@VReaperV
Copy link
Contributor Author

Removed ATTR_TEXCOORD from cameraEffects too, since it's not actually used there.

@illwieckz
Copy link
Member

I got this with the lowest graphics preset:

Warn: Source for shader program cameraEffects:
   0: #version 460 core
   1: 
   2: #define HAVE_EXT_gpu_shader4 1
   3: #define HAVE_ARB_gpu_shader5 1
   4: #define HAVE_ARB_texture_float 1
   5: #define HAVE_ARB_texture_gather 1
   6: #define HAVE_EXT_texture_integer 1
   7: #define HAVE_ARB_texture_rg 1
   8: #define HAVE_ARB_uniform_buffer_object 1
   9: #extension GL_ARB_shader_draw_parameters : require
  10: #define HAVE_ARB_shader_draw_parameters 1
  11: #define HAVE_ARB_shader_storage_buffer_object 1
  12: #ifndef r_AmbientScale
  13: #define r_AmbientScale 1.00000000e+00
  14: #endif
  15: #ifndef r_SpecularScale
  16: #define r_SpecularScale 1.00000000e+00
  17: #endif
  18: #ifndef r_zNear
  19: #define r_zNear 3.00000000e+00
  20: #endif
  21: #ifndef M_PI
  22: #define M_PI 3.14159274e+00
  23: #endif
  24: #ifndef MAX_SHADOWMAPS
  25: #define MAX_SHADOWMAPS 5
  26: #endif
  27: #ifndef MAX_REF_LIGHTS
  28: #define MAX_REF_LIGHTS 1024
  29: #endif
  30: #ifndef NUM_LIGHT_LAYERS
  31: #define NUM_LIGHT_LAYERS 4
  32: #endif
  33: #ifndef TILE_SIZE
  34: #define TILE_SIZE 16
  35: #endif
  36: #ifndef r_FBufSize
  37: #define r_FBufSize vec2(3.84000000e+03, 2.16000000e+03)
  38: #endif
  39: #ifndef r_tileStep
  40: #define r_tileStep vec2(4.16666642e-03, 7.40740728e-03)
  41: #endif
  42: #ifndef r_vertexSkinning
  43: #define r_vertexSkinning 1
  44: #endif
  45: const int MAX_GLSL_BONES = 256;
  46: #ifndef r_halfLambertLighting
  47: #define r_halfLambertLighting 1
  48: #endif
  49: #ifndef r_glowMapping
  50: #define r_glowMapping 1
  51: #endif
  52: #define IN(mode) mode in
  53: #define DECLARE_OUTPUT(type) out type outputColor;
  54: #define textureCube texture
  55: #define texture2D texture
  56: #define texture2DProj textureProj
  57: #define texture3D texture
  58: IN(flat) int in_drawID;
  59: IN(flat) int in_baseInstance;
  60: #define drawID in_drawID
  61: #define baseInstance in_baseInstance
  62: 
  63: /*
  64: ===========================================================================
  65: Copyright (C) 2009-2010 Robert Beckebans <[email protected]>
  66: 
  67: This file is part of XreaL source code.
  68: 
  69: XreaL source code is free software; you can redistribute it
  70: and/or modify it under the terms of the GNU General Public License as
  71: published by the Free Software Foundation; either version 2 of the License,
  72: or (at your option) any later version.
  73: 
  74: XreaL source code is distributed in the hope that it will be
  75: useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  76: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  77: GNU General Public License for more details.
  78: 
  79: You should have received a copy of the GNU General Public License
  80: along with XreaL source code; if not, write to the Free Software
  81: Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  82: ===========================================================================
  83: */
  84: 
  85: /* cameraEffects_fp.glsl */
  86: 
  87: uniform sampler2D u_CurrentMap;
  88: 
  89: #if defined(r_colorGrading)
  90: uniform sampler3D u_ColorMap3D;
  91: #endif
  92: 
  93: uniform vec4      u_ColorModulate;
  94: uniform float     u_GlobalLightFactor; // 1 / tr.identityLight
  95: uniform float     u_InverseGamma;
  96: 
  97: // Tone mapping is not available when high-precision float framebuffer isn't enabled or supported.
  98: #if defined(r_highPrecisionRendering) && defined(HAVE_ARB_texture_float)
  99: /* x: contrast
 100: y: highlightsCompressionSpeed
 101: z: shoulderClip
 102: w: highlightsCompression */
 103: uniform bool u_Tonemap;
 104: uniform vec4 u_TonemapParms;
 105: uniform float u_TonemapExposure;
 106: 
 107: DECLARE_OUTPUT(vec4)
 108: 
 109: vec3 TonemapLottes( vec3 color ) {
 110:   // Lottes 2016, "Advanced Techniques and Optimization of HDR Color Pipelines"
 111:   return pow( color, vec3( u_TonemapParms[0] ) )
 112:          / ( pow( color, vec3( u_TonemapParms[0] * u_TonemapParms[1] ) ) * u_TonemapParms[2] + u_TonemapParms[3] );
 113: }
 114: #endif
 115: 
 116: void main()
 117: {
 118: 	// calculate the screen texcoord in the 0.0 to 1.0 range
 119: 	vec2 st = gl_FragCoord.st / r_FBufSize;
 120: 
 121: 	vec4 color = texture2D(u_CurrentMap, st);
 122: 	color *= u_GlobalLightFactor;
 123: 
 124: #if defined(r_highPrecisionRendering) && defined(HAVE_ARB_texture_float)
 125: 	if( u_Tonemap ) {
 126: 		color.rgb = TonemapLottes( color.rgb * u_TonemapExposure );
 127: 	}
 128: #endif
 129: 
 130: 	color.rgb = clamp( color.rgb, vec3( 0.0f ), vec3( 1.0f ) );
 131: 
 132: #if defined(r_colorGrading)
 133: 	// apply color grading
 134: 	vec3 colCoord = color.rgb * 15.0 / 16.0 + 0.5 / 16.0;
 135: 	colCoord.z *= 0.25;
 136: 	color.rgb = u_ColorModulate.x * texture3D(u_ColorMap3D, colCoord).rgb;
 137: 	color.rgb += u_ColorModulate.y * texture3D(u_ColorMap3D, colCoord + vec3(0.0, 0.0, 0.25)).rgb;
 138: 	color.rgb += u_ColorModulate.z * texture3D(u_ColorMap3D, colCoord + vec3(0.0, 0.0, 0.50)).rgb;
 139: 	color.rgb += u_ColorModulate.w * texture3D(u_ColorMap3D, colCoord + vec3(0.0, 0.0, 0.75)).rgb;
 140: #endif
 141: 
 142: 	color.xyz = pow(color.xyz, vec3(u_InverseGamma));
 143: 
 144: 	outputColor = color;
 145: }
^^^^
0:145(2): error: `outputColor' undeclared

^^^^
0:145(2): error: value of type vec4 cannot be assigned to variable of type error

 
Warn: Compile log:
0:145(2): error: `outputColor' undeclared
0:145(2): error: value of type vec4 cannot be assigned to variable of type error
 
Warn: Unhandled exception (15ShaderException): Couldn't compile fragment shader: cameraEffects 

@VReaperV
Copy link
Contributor Author

I got this with the lowest graphics preset:

Fixed.

Copy link
Member

@illwieckz illwieckz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@VReaperV VReaperV merged commit d8b3fe8 into DaemonEngine:master Mar 17, 2025
9 checks passed
@VReaperV VReaperV deleted the shader-screenspace branch March 17, 2025 22:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Renderer T-Cleanup T-Improvement Improvement for an existing feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants