Cleanup naming of shader params/uniforms

This commit is contained in:
Marek Roszko 2022-05-18 07:55:52 -04:00
parent 294b8e9051
commit 417f2f357c
4 changed files with 62 additions and 60 deletions

View File

@ -72,7 +72,7 @@ GPU_MANAGER::~GPU_MANAGER()
void GPU_MANAGER::SetShader( SHADER& aShader )
{
m_shader = &aShader;
m_shaderAttrib = m_shader->GetAttribute( "attrShaderParams" );
m_shaderAttrib = m_shader->GetAttribute( "a_shaderParams" );
if( m_shaderAttrib == -1 )
{

View File

@ -534,12 +534,12 @@ void OPENGL_GAL::BeginDrawing()
}
// Set shader parameter
GLint ufm_fontTexture = m_shader->AddParameter( "fontTexture" );
GLint ufm_fontTextureWidth = m_shader->AddParameter( "fontTextureWidth" );
ufm_worldPixelSize = m_shader->AddParameter( "worldPixelSize" );
ufm_screenPixelSize = m_shader->AddParameter( "screenPixelSize" );
ufm_pixelSizeMultiplier = m_shader->AddParameter( "pixelSizeMultiplier" );
ufm_antialiasingOffset = m_shader->AddParameter( "antialiasingOffset" );
GLint ufm_fontTexture = m_shader->AddParameter( "u_fontTexture" );
GLint ufm_fontTextureWidth = m_shader->AddParameter( "u_fontTextureWidth" );
ufm_worldPixelSize = m_shader->AddParameter( "u_worldPixelSize" );
ufm_screenPixelSize = m_shader->AddParameter( "u_screenPixelSize" );
ufm_pixelSizeMultiplier = m_shader->AddParameter( "u_pixelSizeMultiplier" );
ufm_antialiasingOffset = m_shader->AddParameter( "u_antialiasingOffset" );
m_shader->Use();
m_shader->SetParameter( ufm_fontTexture, (int) FONT_TEXTURE_UNIT );

View File

@ -36,13 +36,14 @@ const float SHADER_STROKED_CIRCLE = 3.0;
const float SHADER_FONT = 4.0;
const float SHADER_LINE_A = 5.0;
varying vec4 shaderParams;
varying vec2 circleCoords;
uniform sampler2D fontTexture;
uniform float worldPixelSize;
varying vec4 v_shaderParams;
varying vec2 v_circleCoords;
uniform sampler2D u_fontTexture;
uniform float u_worldPixelSize;
// Needed to reconstruct the mipmap level / texel derivative
uniform int fontTextureWidth;
uniform int u_fontTextureWidth;
void filledCircle( vec2 aCoord )
{
@ -54,7 +55,7 @@ void filledCircle( vec2 aCoord )
float pixelSegDistance( vec2 aCoord )
{
float aspect = shaderParams[1];
float aspect = v_shaderParams[1];
float dist;
vec2 v = vec2( 1.0 - ( aspect - abs( aCoord.s ) ), aCoord.t );
@ -109,7 +110,7 @@ void main()
{
// VS to FS pipeline does math that means we can't rely on the mode
// parameter being bit-exact without rounding it first.
float mode = floor( shaderParams[0] + 0.5 );
float mode = floor( v_shaderParams[0] + 0.5 );
if( mode == SHADER_LINE_A )
{
@ -117,24 +118,24 @@ void main()
}
else if( mode == SHADER_FILLED_CIRCLE )
{
filledCircle( circleCoords );
filledCircle( v_circleCoords );
}
else if( mode == SHADER_STROKED_CIRCLE )
{
strokedCircle( circleCoords, shaderParams[2], shaderParams[3] );
strokedCircle( v_circleCoords, v_shaderParams[2], v_shaderParams[3] );
}
else if( mode == SHADER_FONT )
{
vec2 tex = shaderParams.yz;
vec2 tex = v_shaderParams.yz;
// Unless we're stretching chars it is okay to consider
// one derivative for filtering
float derivative = length( dFdx( tex ) ) * fontTextureWidth / 4;
float derivative = length( dFdx( tex ) ) * u_fontTextureWidth / 4;
#ifdef USE_MSDF
float dist = median( texture2D( fontTexture, tex ).rgb );
float dist = median( texture2D( u_fontTexture, tex ).rgb );
#else
float dist = texture2D( fontTexture, tex ).r;
float dist = texture2D( u_fontTexture, tex ).r;
#endif
// use the derivative for zoom-adaptive filtering

View File

@ -40,14 +40,15 @@ const float SHADER_LINE_F = 10.0;
// Minimum line width
const float MIN_WIDTH = 1.0;
attribute vec4 attrShaderParams;
varying vec4 shaderParams;
varying vec2 circleCoords;
uniform float worldPixelSize;
uniform vec2 screenPixelSize;
uniform float pixelSizeMultiplier;
uniform float minLinePixelWidth;
uniform vec2 antialiasingOffset;
attribute vec4 a_shaderParams;
varying vec4 v_shaderParams;
varying vec2 v_circleCoords;
uniform float u_worldPixelSize;
uniform vec2 u_screenPixelSize;
uniform float u_pixelSizeMultiplier;
uniform float u_minLinePixelWidth;
uniform vec2 u_antialiasingOffset;
float roundr( float f, float r )
@ -64,8 +65,8 @@ void computeLineCoords( bool posture, vec2 vs, vec2 vp, vec2 texcoord, vec2 dir,
{
float lineLength = length(vs);
vec4 screenPos = gl_ModelViewProjectionMatrix * gl_Vertex + vec4(1, 1, 0, 0);
float w = ((lineWidth == 0.0) ? worldPixelSize : lineWidth );
float pixelWidth = roundr( w / worldPixelSize, 1.0 );
float w = ((lineWidth == 0.0) ? u_worldPixelSize : lineWidth );
float pixelWidth = roundr( w / u_worldPixelSize, 1.0 );
float aspect = ( lineLength + w ) / w;
vec4 color = gl_Color;
vec2 s = sign( vec2( gl_ModelViewProjectionMatrix[0][0], gl_ModelViewProjectionMatrix[1][1] ) );
@ -74,39 +75,39 @@ void computeLineCoords( bool posture, vec2 vs, vec2 vp, vec2 texcoord, vec2 dir,
if( pixelWidth < 1.0 )
pixelWidth = 1.0;
if ( pixelWidth > 1.0 || pixelSizeMultiplier > 1.0 )
if ( pixelWidth > 1.0 || u_pixelSizeMultiplier > 1.0 )
{
vec2 offsetNorm = (vs + vp) * pixelWidth / lineLength * 0.5;
vec4 screenOffset = vec4( s.x * offsetNorm.x * screenPixelSize.x, s.y * offsetNorm.y * screenPixelSize.y , 0, 0);
vec4 screenOffset = vec4( s.x * offsetNorm.x * u_screenPixelSize.x, s.y * offsetNorm.y * u_screenPixelSize.y , 0, 0);
vec4 adjust = vec4(-1, -1, 0, 0);
if( mod( pixelWidth * pixelSizeMultiplier, 2.0 ) > 0.9 )
if( mod( pixelWidth * u_pixelSizeMultiplier, 2.0 ) > 0.9 )
{
adjust += vec4( screenPixelSize.x, screenPixelSize.y, 0, 0 ) * 0.5;
adjust += vec4( u_screenPixelSize.x, u_screenPixelSize.y, 0, 0 ) * 0.5;
}
gl_Position = roundv(screenPos, screenPixelSize) + adjust + screenOffset;
gl_Position = roundv(screenPos, u_screenPixelSize) + adjust + screenOffset;
shaderParams[0] = SHADER_LINE_A;
v_shaderParams[0] = SHADER_LINE_A;
}
else {
vec4 pos0 = screenPos;
pos0.xy += ( posture ? dir.xy : dir.yx ) * screenPixelSize / 2.0;
pos0.xy += ( posture ? dir.xy : dir.yx ) * u_screenPixelSize / 2.0;
if(posture)
{
pos0.y -= screenPixelSize.y * sign(vs.y) * 0.5;
pos0.y -= u_screenPixelSize.y * sign(vs.y) * 0.5;
}
else
{
pos0.x += screenPixelSize.x * sign(vs.x) * 0.5;
pos0.x += u_screenPixelSize.x * sign(vs.x) * 0.5;
}
gl_Position = pos0 - vec4(1, 1, 0, 0);
shaderParams[0] = SHADER_LINE_B;
v_shaderParams[0] = SHADER_LINE_B;
}
shaderParams[1] = aspect;
v_shaderParams[1] = aspect;
gl_TexCoord[0].st = vec2(aspect * texcoord.x, texcoord.y);
gl_FrontColor = gl_Color;
@ -116,9 +117,9 @@ void computeLineCoords( bool posture, vec2 vs, vec2 vp, vec2 texcoord, vec2 dir,
void computeCircleCoords( float mode, float vertexIndex, float radius, float lineWidth )
{
vec4 delta;
vec4 center = roundv( gl_ModelViewProjectionMatrix * gl_Vertex + vec4(1, 1, 0, 0), screenPixelSize );
float pixelWidth = roundr( lineWidth / worldPixelSize, 1.0);
float pixelR = roundr( radius / worldPixelSize, 1.0);
vec4 center = roundv( gl_ModelViewProjectionMatrix * gl_Vertex + vec4(1, 1, 0, 0), u_screenPixelSize );
float pixelWidth = roundr( lineWidth / u_worldPixelSize, 1.0);
float pixelR = roundr( radius / u_worldPixelSize, 1.0);
if( mode == SHADER_STROKED_CIRCLE)
pixelR += pixelWidth / 2.0;
@ -130,40 +131,40 @@ void computeCircleCoords( float mode, float vertexIndex, float radius, float lin
if( vertexIndex == 1.0 )
{
circleCoords = vec2( -sqrt( 3.0 ), -1.0 );
v_circleCoords = vec2( -sqrt( 3.0 ), -1.0 );
delta = vec4( -pixelR * sqrt(3.0), -pixelR, 0, 0 );
}
else if( vertexIndex == 2.0 )
{
circleCoords = vec2( sqrt( 3.0 ), -1.0 );
v_circleCoords = vec2( sqrt( 3.0 ), -1.0 );
delta = vec4( pixelR * sqrt( 3.0 ), -pixelR, 0, 0 );
}
else if( vertexIndex == 3.0 )
{
circleCoords = vec2( 0.0, 2.0 );
v_circleCoords = vec2( 0.0, 2.0 );
delta = vec4( 0, 2 * pixelR, 0, 0 );
}
else if( vertexIndex == 4.0 )
{
circleCoords = vec2( -sqrt( 3.0 ), 0.0 );
v_circleCoords = vec2( -sqrt( 3.0 ), 0.0 );
delta = vec4( 0, 0, 0, 0 );
}
else if( vertexIndex == 5.0 )
{
circleCoords = vec2( sqrt( 3.0 ), 0.0 );
v_circleCoords = vec2( sqrt( 3.0 ), 0.0 );
delta = vec4( 0, 0, 0, 0 );
}
else if( vertexIndex == 6.0 )
{
circleCoords = vec2( 0.0, 2.0 );
v_circleCoords = vec2( 0.0, 2.0 );
delta = vec4( 0, 0, 0, 0 );
}
shaderParams[2] = pixelR;
shaderParams[3] = pixelWidth;
v_shaderParams[2] = pixelR;
v_shaderParams[3] = pixelWidth;
delta.x *= screenPixelSize.x;
delta.y *= screenPixelSize.y;
delta.x *= u_screenPixelSize.x;
delta.y *= u_screenPixelSize.y;
gl_Position = center + delta + adjust;
gl_FrontColor = gl_Color;
@ -172,13 +173,13 @@ void computeCircleCoords( float mode, float vertexIndex, float radius, float lin
void main()
{
float mode = attrShaderParams[0];
float mode = a_shaderParams[0];
// Pass attributes to the fragment shader
shaderParams = attrShaderParams;
v_shaderParams = a_shaderParams;
float lineWidth = shaderParams.y;
vec2 vs = shaderParams.zw;
float lineWidth = v_shaderParams.y;
vec2 vs = v_shaderParams.zw;
vec2 vp = vec2(-vs.y, vs.x);
bool posture = abs( vs.x ) < abs(vs.y);
@ -195,7 +196,7 @@ void main()
else if( mode == SHADER_LINE_F )
computeLineCoords( posture, -vs, vp, vec2( 1, 1 ), vec2( -1, 0 ), lineWidth, false );
else if( mode == SHADER_FILLED_CIRCLE || mode == SHADER_STROKED_CIRCLE)
computeCircleCoords( mode, shaderParams.y, shaderParams.z, shaderParams.w );
computeCircleCoords( mode, v_shaderParams.y, v_shaderParams.z, v_shaderParams.w );
else
{
// Pass through the coordinates like in the fixed pipeline
@ -204,5 +205,5 @@ void main()
}
gl_Position.xy += antialiasingOffset;
gl_Position.xy += u_antialiasingOffset;
}