Skip to content

Commit

Permalink
Merge pull request #4 from SomaZ/MBII-rend2
Browse files Browse the repository at this point in the history
Latest rend2 updates
  • Loading branch information
MaceMadunusus authored Apr 24, 2024
2 parents f8bac77 + 0c33c82 commit a07b190
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 47 deletions.
3 changes: 2 additions & 1 deletion codemp/rd-rend2/tr_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ static void R_Splash()
{
const GLfloat black[] = { 0.0f, 0.0f, 0.0f, 1.0f };

qglViewport( 0, 0, glConfig.vidWidth, glConfig.vidHeight );
GL_SetViewportAndScissor( 0, 0, glConfig.vidWidth, glConfig.vidHeight );
qglClearBufferfv(GL_COLOR, 0, black);
qglClear(GL_DEPTH_BUFFER_BIT);

Expand Down Expand Up @@ -1203,6 +1203,7 @@ void GL_SetDefaultState( void )
qglEnable(GL_PROGRAM_POINT_SIZE);
qglDisable( GL_CULL_FACE );
qglDisable( GL_BLEND );
glState.blend = false;

qglEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);

Expand Down
10 changes: 6 additions & 4 deletions shared/rd-rend2/glsl/generic.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ vec2 GenTexCoords(int TCGen, vec3 position, vec3 normal, vec3 TCGenVector0, vec3

case TCGEN_ENVIRONMENT_MAPPED:
{
vec3 viewer = normalize(u_ViewOrigin - position);
vec3 localOrigin = (inverse(u_ModelMatrix) * vec4(u_ViewOrigin, 1.0)).xyz;
vec3 viewer = normalize(localOrigin - position);
vec2 ref = reflect(viewer, normal).yz;
tex.s = ref.x * -0.5 + 0.5;
tex.t = ref.y * 0.5 + 0.5;
Expand Down Expand Up @@ -362,7 +363,8 @@ vec4 CalcColor(vec3 position, vec3 normal)
return color;
}

vec3 viewer = u_ViewOrigin - position;
vec3 localOrigin = (inverse(u_ModelMatrix) * vec4(u_ViewOrigin, 1.0)).xyz;
vec3 viewer = localOrigin - position;

if (u_AlphaGen == AGEN_LIGHTING_SPECULAR)
{
Expand Down Expand Up @@ -424,7 +426,7 @@ void main()
gl_Position = u_viewProjectionMatrix * wsPosition;

#if defined(USE_TCGEN)
vec2 tex = GenTexCoords(u_TCGen0, wsPosition.xyz, normal, u_TCGen0Vector0, u_TCGen0Vector1);
vec2 tex = GenTexCoords(u_TCGen0, position.xyz, normal, u_TCGen0Vector0, u_TCGen0Vector1);
#else
vec2 tex = attr_TexCoord0.st;
#endif
Expand All @@ -447,7 +449,7 @@ void main()
else
{
#if defined(USE_RGBAGEN)
var_Color = CalcColor(wsPosition.xyz, normal);
var_Color = CalcColor(position.xyz, normal);
#else
var_Color = u_VertColor * attr_Color + u_BaseColor;
#endif
Expand Down
5 changes: 3 additions & 2 deletions shared/rd-rend2/glsl/lightall.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ vec2 GenTexCoords(int TCGen, vec3 position, vec3 normal, vec3 TCGenVector0, vec3

case TCGEN_ENVIRONMENT_MAPPED:
{
vec3 viewer = normalize(u_ViewOrigin - position);
vec3 localOrigin = (inverse(u_ModelMatrix) * vec4(u_ViewOrigin, 1.0)).xyz;
vec3 viewer = normalize(localOrigin - position);
vec2 ref = reflect(viewer, normal).yz;
tex.s = ref.x * -0.5 + 0.5;
tex.t = ref.y * 0.5 + 0.5;
Expand Down Expand Up @@ -242,7 +243,7 @@ void main()
vec4 wsPosition = u_ModelMatrix * vec4(position, 1.0);

#if defined(USE_TCGEN)
vec2 texCoords = GenTexCoords(u_TCGen0, wsPosition.xyz, normal, u_TCGen0Vector0, u_TCGen0Vector1);
vec2 texCoords = GenTexCoords(u_TCGen0, position.xyz, normal, u_TCGen0Vector0, u_TCGen0Vector1);
#else
vec2 texCoords = attr_TexCoord0.st;
#endif
Expand Down
60 changes: 33 additions & 27 deletions shared/rd-rend2/tr_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,20 @@ void GL_State( uint32_t stateBits )
break;
}

qglEnable( GL_BLEND );
if (!glState.blend)
{
qglEnable(GL_BLEND);
glState.blend = true;
}
qglBlendFunc( srcFactor, dstFactor );
}
else
{
qglDisable( GL_BLEND );
if (glState.blend)
{
qglDisable(GL_BLEND);
glState.blend = false;
}
}
}

Expand Down Expand Up @@ -479,13 +487,28 @@ void GL_SetProjectionMatrix(matrix_t matrix)
Matrix16Multiply(glState.projection, glState.modelview, glState.modelviewProjection);
}


void GL_SetModelviewMatrix(matrix_t matrix)
{
Matrix16Copy(matrix, glState.modelview);
Matrix16Multiply(glState.projection, glState.modelview, glState.modelviewProjection);
}

void GL_SetViewportAndScissor(int viewportX, int viewportY, int viewportWidth, int viewportHeight)
{
if (glState.viewportOrigin[0] == viewportX &&
glState.viewportOrigin[1] == viewportY &&
glState.viewportSize[0] == viewportWidth &&
glState.viewportSize[1] == viewportHeight)
return;

qglViewport(viewportX, viewportY, viewportWidth, viewportHeight);
qglScissor(viewportX, viewportY, viewportWidth, viewportHeight);

glState.viewportOrigin[0] = viewportX;
glState.viewportOrigin[1] = viewportY;
glState.viewportSize[0] = viewportWidth;
glState.viewportSize[1] = viewportHeight;
}

/*
================
Expand All @@ -505,20 +528,8 @@ static void SetViewportAndScissor( void ) {
GL_SetProjectionMatrix( backEnd.viewParms.projectionMatrix );

// set the window clipping
qglViewport( backEnd.viewParms.viewportX, backEnd.viewParms.viewportY,
backEnd.viewParms.viewportWidth, backEnd.viewParms.viewportHeight );

if ( !backEnd.viewParms.scissorX && !backEnd.viewParms.scissorY &&
!backEnd.viewParms.scissorWidth && !backEnd.viewParms.scissorHeight )
{
qglScissor( backEnd.viewParms.viewportX, backEnd.viewParms.viewportY,
backEnd.viewParms.viewportWidth, backEnd.viewParms.viewportHeight );
}
else
{
qglScissor( backEnd.viewParms.scissorX, backEnd.viewParms.scissorY,
backEnd.viewParms.scissorWidth, backEnd.viewParms.scissorHeight );
}
GL_SetViewportAndScissor(backEnd.viewParms.viewportX, backEnd.viewParms.viewportY,
backEnd.viewParms.viewportWidth, backEnd.viewParms.viewportHeight);
}

/*
Expand Down Expand Up @@ -1507,8 +1518,7 @@ void RB_SetGL2D (void) {
}

// set 2D virtual screen size
qglViewport( 0, 0, width, height );
qglScissor( 0, 0, width, height );
GL_SetViewportAndScissor(0, 0, width, height);

Matrix16Ortho(0, 640, 480, 0, 0, 1, matrix);
GL_SetProjectionMatrix(matrix);
Expand Down Expand Up @@ -1975,8 +1985,7 @@ static const void *RB_PrefilterEnvMap(const void *data) {
GL_BindToTMU(tr.renderCubeImage, TB_CUBEMAP);
GLSL_BindProgram(&tr.prefilterEnvMapShader);

qglViewport(0, 0, width, height);
qglScissor(0, 0, width, height);
GL_SetViewportAndScissor(0, 0, width, height);

vec4_t viewInfo;
VectorSet4(viewInfo, 0, level, roughnessMips, level / roughnessMips);
Expand All @@ -1998,8 +2007,7 @@ static void RB_RenderSSAO()

FBO_Bind(tr.quarterFbo[0]);

qglViewport(0, 0, tr.quarterFbo[0]->width, tr.quarterFbo[0]->height);
qglScissor(0, 0, tr.quarterFbo[0]->width, tr.quarterFbo[0]->height);
GL_SetViewportAndScissor(0, 0, tr.quarterFbo[0]->width, tr.quarterFbo[0]->height);

GL_State( GLS_DEPTHTEST_DISABLE );

Expand All @@ -2012,8 +2020,7 @@ static void RB_RenderSSAO()

FBO_Bind(tr.quarterFbo[1]);

qglViewport(0, 0, tr.quarterFbo[1]->width, tr.quarterFbo[1]->height);
qglScissor(0, 0, tr.quarterFbo[1]->width, tr.quarterFbo[1]->height);
GL_SetViewportAndScissor(0, 0, tr.quarterFbo[1]->width, tr.quarterFbo[1]->height);

GLSL_BindProgram(&tr.depthBlurShader[0]);

Expand All @@ -2025,8 +2032,7 @@ static void RB_RenderSSAO()

FBO_Bind(tr.screenSsaoFbo);

qglViewport(0, 0, tr.screenSsaoFbo->width, tr.screenSsaoFbo->height);
qglScissor(0, 0, tr.screenSsaoFbo->width, tr.screenSsaoFbo->height);
GL_SetViewportAndScissor(0, 0, tr.screenSsaoFbo->width, tr.screenSsaoFbo->height);

GLSL_BindProgram(&tr.depthBlurShader[1]);

Expand Down
3 changes: 1 addition & 2 deletions shared/rd-rend2/tr_fbo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -819,8 +819,7 @@ void FBO_BlitFromTexture(struct image_s *src, vec4i_t inSrcBox, vec2_t inSrcTexS
height = glConfig.vidHeight;
}

qglViewport( 0, 0, width, height );
qglScissor( 0, 0, width, height );
GL_SetViewportAndScissor(0, 0, width, height);

Matrix16Ortho(0, width, height, 0, 0, 1, projection);

Expand Down
5 changes: 4 additions & 1 deletion shared/rd-rend2/tr_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -1550,7 +1550,6 @@ typedef struct {
int frameCount; // copied from tr.frameCount
cplane_t portalPlane; // clip anything behind this if mirroring
int viewportX, viewportY, viewportWidth, viewportHeight;
int scissorX, scissorY, scissorWidth, scissorHeight;
FBO_t *targetFbo;
int targetFboLayer;
float fovX, fovY;
Expand Down Expand Up @@ -2223,8 +2222,11 @@ typedef struct glstate_s {
int currenttmu;
int texEnv[2];
int faceCulling;
bool blend;
float minDepth;
float maxDepth;
ivec2_t viewportOrigin;
ivec2_t viewportSize;
uint32_t glStateBits;
uint32_t vertexAttribsState;
vertexAttribute_t currentVaoAttribs[ATTR_INDEX_MAX];
Expand Down Expand Up @@ -2884,6 +2886,7 @@ void GL_CheckErrs( const char *file, int line );
void GL_State( uint32_t stateVector );
void GL_SetProjectionMatrix(matrix_t matrix);
void GL_SetModelviewMatrix(matrix_t matrix);
void GL_SetViewportAndScissor(int viewportX, int viewportY, int viewportWidth, int viewportHeight);
void GL_Cull( int cullType );
void GL_DepthRange( float min, float max );
void GL_VertexAttribPointers(size_t numAttributes,
Expand Down
6 changes: 2 additions & 4 deletions shared/rd-rend2/tr_postprocess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,7 @@ void RB_BloomDownscale(image_t *sourceImage, FBO_t *destFBO)

FBO_Bind(destFBO);
GL_State(GLS_SRCBLEND_ONE | GLS_DSTBLEND_ZERO);

qglViewport(0, 0, destFBO->width, destFBO->height);
GL_SetViewportAndScissor(0, 0, destFBO->width, destFBO->height);
qglClearBufferfv(GL_COLOR, 0, colorBlack);

GLSL_BindProgram(&tr.dglowDownsample);
Expand All @@ -515,8 +514,7 @@ void RB_BloomUpscale(FBO_t *sourceFBO, FBO_t *destFBO)

FBO_Bind(destFBO);
GL_State(GLS_SRCBLEND_ONE | GLS_DSTBLEND_ZERO);

glViewport(0, 0, destFBO->width, destFBO->height);
GL_SetViewportAndScissor(0, 0, destFBO->width, destFBO->height);
qglClearBufferfv(GL_COLOR, 0, colorBlack);

GLSL_BindProgram(&tr.dglowUpsample);
Expand Down
22 changes: 22 additions & 0 deletions shared/rd-rend2/tr_shade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,11 @@ static shaderProgram_t *SelectShaderProgram( int stageIndex, shaderStage_t *stag
{
index |= LIGHTDEF_USE_ALPHA_TEST;
}

// TODO: remove light vertex def and fix parallax usage on unlit stages like glow stages
if (stage->glslShaderIndex & LIGHTDEF_USE_PARALLAXMAP &&
stage->glslShaderIndex & LIGHTDEF_LIGHTTYPE_MASK)
index |= LIGHTDEF_USE_PARALLAXMAP | LIGHTDEF_USE_LIGHT_VERTEX;

result = &stage->glslShaderGroup[index];
backEnd.pc.c_lightallDraws++;
Expand Down Expand Up @@ -1649,6 +1654,23 @@ static void RB_IterateStagesGeneric( shaderCommands_t *input, const VertexArrays
samplerBindingsWriter.AddStaticImage(tr.whiteImage, 0);
else if ( pStage->bundle[TB_COLORMAP].image[0] != 0 )
samplerBindingsWriter.AddAnimatedImage(&pStage->bundle[TB_COLORMAP], TB_COLORMAP);

// TODO: remove light requirement
if (pStage->glslShaderIndex & LIGHTDEF_USE_PARALLAXMAP &&
pStage->glslShaderIndex & LIGHTDEF_LIGHTTYPE_MASK)
{
vec4_t enableTextures = {};
if (pStage->bundle[TB_NORMALMAP].image[0])
{
samplerBindingsWriter.AddAnimatedImage(&pStage->bundle[TB_NORMALMAP], TB_NORMALMAP);
enableTextures[0] = 1.0f;
}
else if (r_normalMapping->integer)
{
samplerBindingsWriter.AddStaticImage(tr.whiteImage, TB_NORMALMAP);
}
uniformDataWriter.SetUniformVec4(UNIFORM_ENABLETEXTURES, enableTextures);
}
}
else if ( pStage->glslShaderGroup == tr.lightallShader )
{
Expand Down
5 changes: 1 addition & 4 deletions shared/rd-rend2/tr_shadows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,12 @@ void RB_ShadowFinish(void) {
return;
}

GL_Cull(CT_TWO_SIDED);

GL_BindToTMU(tr.whiteImage, TB_COLORMAP);

GL_State(GLS_STENCILTEST_ENABLE | GLS_DEPTHMASK_TRUE | GLS_SRCBLEND_DST_COLOR | GLS_DSTBLEND_ZERO);

qglStencilFunc(GL_NOTEQUAL, 0, 0xff);
qglViewport(0, 0, glConfig.vidWidth, glConfig.vidHeight);
qglScissor(0, 0, glConfig.vidWidth, glConfig.vidHeight);
GL_SetViewportAndScissor(0, 0, glConfig.vidWidth, glConfig.vidHeight);
matrix_t projection;
Matrix16Ortho(0, glConfig.vidWidth, glConfig.vidHeight, 0, 0, 1, projection);

Expand Down
3 changes: 1 addition & 2 deletions shared/rd-rend2/tr_weather.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ namespace
{
FBO_Bind(tr.weatherDepthFbo);

qglViewport(0, 0, tr.weatherDepthFbo->width, tr.weatherDepthFbo->height);
qglScissor(0, 0, tr.weatherDepthFbo->width, tr.weatherDepthFbo->height);
GL_SetViewportAndScissor(0, 0, tr.weatherDepthFbo->width, tr.weatherDepthFbo->height);

if (tr.weatherSystem->weatherBrushType == WEATHER_BRUSHES_OUTSIDE) // used outside brushes
{
Expand Down

0 comments on commit a07b190

Please sign in to comment.