Skip to content

Commit

Permalink
Rend2 updates (#196)
Browse files Browse the repository at this point in the history
* [rend2] Fix typo and fix ubos in sp port

* [rend2] Fix vertex lit shaders with lightmap stage

* [rend2] Fix specular image gen related memory leak

* [rend2] Rework precaching to init the renderer now

* [rend2] Get rid of alphatest and glow shader permutations

Those are handled via uniforms now. Less shaders to compile speeds up load times, having to switch less between glsl shaders speeds up ingame performance. Alphatest cases only commented out for now, will put these behind a cvar later, as some gpus might require to know if a discard can happen or not.

[rend2] Fix sky glowing when last rendered surface had a glow stage

---------

Co-authored-by: SomaZ <17459161+somaz@users.noreply.github.com>
  • Loading branch information
taysta and SomaZ authored May 4, 2024
1 parent 52eb7b3 commit 05f1ef9
Show file tree
Hide file tree
Showing 14 changed files with 130 additions and 113 deletions.
34 changes: 21 additions & 13 deletions codemp/rd-rend2/tr_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1712,18 +1712,18 @@ static void R_InitBackEndFrameData()
GLuint timerQueries[MAX_GPU_TIMERS*MAX_FRAMES];
qglGenQueries(MAX_GPU_TIMERS*MAX_FRAMES, timerQueries);

GLuint ubos[MAX_FRAMES * MAX_SCENCES];
qglGenBuffers(MAX_FRAMES * MAX_SCENCES, ubos);
GLuint ubos[MAX_FRAMES * MAX_SCENES];
qglGenBuffers(MAX_FRAMES * MAX_SCENES, ubos);

for ( int i = 0; i < MAX_FRAMES; i++ )
{
gpuFrame_t *frame = backEndData->frames + i;
const GLbitfield mapBits = GL_MAP_WRITE_BIT | GL_MAP_COHERENT_BIT | GL_MAP_PERSISTENT_BIT;

for (byte j = 0; j < MAX_SCENCES; j++)
for (byte j = 0; j < MAX_SCENES; j++)
{
size_t BUFFER_SIZE = j == 0 ? FRAME_UNIFORM_BUFFER_SIZE : FRAME_SCENE_UNIFORM_BUFFER_SIZE;
frame->ubo[j] = ubos[i * MAX_SCENCES + j];
frame->ubo[j] = ubos[i * MAX_SCENES + j];
frame->uboWriteOffset[j] = 0;
frame->uboSize[j] = BUFFER_SIZE;
qglBindBuffer(GL_UNIFORM_BUFFER, frame->ubo[j]);
Expand Down Expand Up @@ -1929,7 +1929,7 @@ static void R_ShutdownBackEndFrameData()
frame->sync = NULL;
}

qglDeleteBuffers(MAX_SCENCES, frame->ubo);
qglDeleteBuffers(MAX_SCENES, frame->ubo);

if ( glRefConfig.immutableBuffers )
{
Expand All @@ -1947,6 +1947,7 @@ static void R_ShutdownBackEndFrameData()
}
}

static bool r_inited = false;
/*
===============
R_Init
Expand All @@ -1956,6 +1957,9 @@ void R_Init( void ) {
byte *ptr;
int i;

if (r_inited)
return;

ri.Printf( PRINT_ALL, "----- R_Init -----\n" );

// clear all our internal state
Expand Down Expand Up @@ -2069,6 +2073,7 @@ void R_Init( void ) {

// print info
GfxInfo_f();
r_inited = true;
ri.Printf( PRINT_ALL, "----- finished R_Init -----\n" );
}

Expand All @@ -2094,21 +2099,23 @@ void RE_Shutdown( qboolean destroyWindow, qboolean restarting ) {
R_ShutdownWeatherSystem();

R_ShutdownFonts();
if ( tr.registered ) {

if (r_inited)
{
R_ShutDownQueries();
FBO_Shutdown();
R_DeleteTextures();
R_DestroyGPUBuffers();
GLSL_ShutdownGPUShaders();
}

if ( destroyWindow && restarting )
{
ri.Z_Free((void *)glConfig.extensions_string);
ri.Z_Free((void *)glConfigExt.originalExtensionString);
if (destroyWindow && restarting && tr.registered)
{
ri.Z_Free((void *)glConfig.extensions_string);
ri.Z_Free((void *)glConfigExt.originalExtensionString);

qglDeleteVertexArrays(1, &tr.globalVao);
SaveGhoul2InfoArray();
}
qglDeleteVertexArrays(1, &tr.globalVao);
SaveGhoul2InfoArray();
}

// shut down platform specific OpenGL stuff
Expand All @@ -2117,6 +2124,7 @@ void RE_Shutdown( qboolean destroyWindow, qboolean restarting ) {
}

tr.registered = qfalse;
r_inited = false;
backEndData = NULL;
}

Expand Down
9 changes: 3 additions & 6 deletions shared/rd-rend2/glsl/generic.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,8 @@ uniform sampler2D u_DiffuseMap;
uniform int u_AlphaTestType;
#endif
uniform int u_FogIndex;
// x = glow out, y = deluxe, z = screen shadow, w = cube
uniform vec4 u_EnableTextures;

in vec2 var_DiffuseTex;
in vec4 var_Color;
Expand Down Expand Up @@ -599,10 +601,5 @@ void main()
#endif

out_Color = vec4(color.rgb * var_Color.rgb, color.a);

#if defined(USE_GLOW_BUFFER)
out_Glow = out_Color;
#else
out_Glow = vec4(0.0);
#endif
out_Glow = mix(vec4(0.0, 0.0, 0.0, out_Color.a), out_Color, u_EnableTextures.x);
}
11 changes: 2 additions & 9 deletions shared/rd-rend2/glsl/lightall.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,8 @@ uniform samplerCube u_CubeMap;
uniform sampler2D u_EnvBrdfMap;
#endif

#if defined(USE_NORMALMAP) || defined(USE_DELUXEMAP) || defined(USE_SPECULARMAP) || defined(USE_CUBEMAP)
// y = deluxe, w = cube
// x = glow out, y = deluxe, z = screen shadow, w = cube
uniform vec4 u_EnableTextures;
#endif

uniform vec4 u_NormalScale;
uniform vec4 u_SpecularScale;
Expand Down Expand Up @@ -1195,10 +1193,5 @@ void main()
#endif

out_Color.a = diffuse.a;

#if defined(USE_GLOW_BUFFER)
out_Glow = out_Color;
#else
out_Glow = vec4(0.0, 0.0, 0.0, out_Color.a);
#endif
out_Glow = mix(vec4(0.0, 0.0, 0.0, out_Color.a), out_Color, u_EnableTextures.x);
}
6 changes: 3 additions & 3 deletions shared/rd-rend2/glsl/surface_sprites.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ uniform int u_FogIndex;
uniform vec4 u_FogColorMask;
#endif

#if defined(ALPHA_TEST)
#if defined(USE_ALPHA_TEST)
uniform int u_AlphaTestType;
#endif

Expand Down Expand Up @@ -223,7 +223,7 @@ float CalcFog(in vec3 viewOrigin, in vec3 position, in Fog fog)

void main()
{
#if defined(ALPHA_TEST)
#if defined(USE_ALPHA_TEST)
float alphaTestValue = 0.5;
if (u_AlphaTestType == ALPHA_TEST_GT0)
{
Expand Down Expand Up @@ -256,7 +256,7 @@ void main()
}
#endif

#if defined(ALPHA_TEST)
#if defined(USE_ALPHA_TEST)
if (u_AlphaTestType == ALPHA_TEST_GT0)
{
if (out_Color.a == 0.0)
Expand Down
4 changes: 2 additions & 2 deletions shared/rd-rend2/tr_cmds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ void RE_BeginFrame( stereoFrame_t stereoFrame ) {
qglDeleteSync( sync );
thisFrame->sync = NULL;

for (byte i = 0; i < MAX_SCENCES; i++)
for (byte i = 0; i < MAX_SCENES; i++)
{
thisFrame->uboWriteOffset[i] = 0;
}
Expand All @@ -579,7 +579,7 @@ void RE_BeginFrame( stereoFrame_t stereoFrame ) {
R_SaveScreenshot(&thisFrame->screenshotReadback);

// Resets resources
for (byte i = 0; i < MAX_SCENCES; i++)
for (byte i = 0; i < MAX_SCENES; i++)
{
qglBindBuffer(GL_UNIFORM_BUFFER, thisFrame->ubo[i]);
glState.currentGlobalUBO = thisFrame->ubo[i];
Expand Down
40 changes: 21 additions & 19 deletions shared/rd-rend2/tr_glsl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,8 @@ static size_t GLSL_GetShaderHeader(
ALPHA_TEST_GE128,
ALPHA_TEST_GE192));

Q_strcat(dest, size, "#define USE_ALPHA_TEST\n");

Q_strcat(dest, size,
va("#define MAX_G2_BONES %i\n",
MAX_G2_BONES));
Expand Down Expand Up @@ -1476,11 +1478,11 @@ static int GLSL_LoadGPUProgramGeneric(
if (i & GENERICDEF_USE_RGBAGEN)
Q_strcat(extradefines, sizeof(extradefines), "#define USE_RGBAGEN\n");

if (i & GENERICDEF_USE_GLOW_BUFFER)
Q_strcat(extradefines, sizeof(extradefines), "#define USE_GLOW_BUFFER\n");
/*if (i & GENERICDEF_USE_GLOW_BUFFER)
Q_strcat(extradefines, sizeof(extradefines), "#define USE_GLOW_BUFFER\n");*/

if (i & GENERICDEF_USE_ALPHA_TEST)
Q_strcat(extradefines, sizeof(extradefines), "#define USE_ALPHA_TEST\n");
/*if (i & GENERICDEF_USE_ALPHA_TEST)
Q_strcat(extradefines, sizeof(extradefines), "#define USE_ALPHA_TEST\n");*/

if (!GLSL_LoadGPUShader(builder, &tr.genericShader[i], "generic", attribs, NO_XFB_VARS,
extradefines, *programDesc))
Expand Down Expand Up @@ -1542,8 +1544,8 @@ static int GLSL_LoadGPUProgramFogPass(
if (i & FOGDEF_USE_FALLBACK_GLOBAL_FOG)
Q_strcat(extradefines, sizeof(extradefines), "#define USE_FALLBACK_GLOBAL_FOG\n");

if (i & FOGDEF_USE_ALPHA_TEST)
Q_strcat(extradefines, sizeof(extradefines), "#define USE_ALPHA_TEST\n");
/*if (i & FOGDEF_USE_ALPHA_TEST)
Q_strcat(extradefines, sizeof(extradefines), "#define USE_ALPHA_TEST\n");*/

if (!GLSL_LoadGPUShader(builder, &tr.fogShader[i], "fogpass", attribs, NO_XFB_VARS,
extradefines, *programDesc))
Expand All @@ -1554,7 +1556,7 @@ static int GLSL_LoadGPUProgramFogPass(
GLSL_InitUniforms(&tr.fogShader[i]);

qglUseProgram(tr.fogShader[i].program);
if (i & FOGDEF_USE_ALPHA_TEST)
//if (i & FOGDEF_USE_ALPHA_TEST)
GLSL_SetUniformInt(&tr.fogShader[i], UNIFORM_DIFFUSEMAP, 0);
qglUseProgram(0);

Expand Down Expand Up @@ -1605,8 +1607,8 @@ static int GLSL_LoadGPUProgramRefraction(
if (i & REFRACTIONDEF_USE_RGBAGEN)
Q_strcat(extradefines, sizeof(extradefines), "#define USE_RGBAGEN\n");

if (i & REFRACTIONDEF_USE_ALPHA_TEST)
Q_strcat(extradefines, sizeof(extradefines), "#define USE_ALPHA_TEST\n");
/*if (i & REFRACTIONDEF_USE_ALPHA_TEST)
Q_strcat(extradefines, sizeof(extradefines), "#define USE_ALPHA_TEST\n");*/

if (i & REFRACTIONDEF_USE_SRGB_TRANSFORM)
Q_strcat(extradefines, sizeof(extradefines), "#define USE_LINEAR_LIGHT\n");
Expand Down Expand Up @@ -1771,11 +1773,11 @@ static int GLSL_LoadGPUProgramLightAll(
attribs |= ATTR_BONE_INDEXES | ATTR_BONE_WEIGHTS;
}

if (i & LIGHTDEF_USE_ALPHA_TEST)
Q_strcat(extradefines, sizeof(extradefines), "#define USE_ALPHA_TEST\n");
/*if (i & LIGHTDEF_USE_ALPHA_TEST)
Q_strcat(extradefines, sizeof(extradefines), "#define USE_ALPHA_TEST\n");*/

if (i & LIGHTDEF_USE_GLOW_BUFFER)
Q_strcat(extradefines, sizeof(extradefines), "#define USE_GLOW_BUFFER\n");
/*if (i & LIGHTDEF_USE_GLOW_BUFFER)
Q_strcat(extradefines, sizeof(extradefines), "#define USE_GLOW_BUFFER\n");*/

if (!GLSL_LoadGPUShader(builder, &tr.lightallShader[i], "lightall", attribs, NO_XFB_VARS,
extradefines, *programDesc))
Expand Down Expand Up @@ -2240,9 +2242,9 @@ static int GLSL_LoadGPUProgramSurfaceSprites(
Q_strcat(extradefines, sizeof(extradefines),
"#define USE_FOG\n");

if ( i & SSDEF_ALPHA_TEST )
/*if ( i & SSDEF_ALPHA_TEST )
Q_strcat(extradefines, sizeof(extradefines),
"#define ALPHA_TEST\n");
"#define USE_ALPHA_TEST\n");*/

if (i & SSDEF_ADDITIVE)
Q_strcat(extradefines, sizeof(extradefines),
Expand Down Expand Up @@ -2586,8 +2588,8 @@ shaderProgram_t *GLSL_GetGenericShaderProgram(int stage)
shaderStage_t *pStage = tess.xstages[stage];
int shaderAttribs = 0;

if ( pStage->alphaTestType != ALPHA_TEST_NONE )
shaderAttribs |= GENERICDEF_USE_ALPHA_TEST;
/*if ( pStage->alphaTestType != ALPHA_TEST_NONE )
shaderAttribs |= GENERICDEF_USE_ALPHA_TEST;*/

if (backEnd.currentEntity->e.renderfx & (RF_DISINTEGRATE1 | RF_DISINTEGRATE2))
shaderAttribs |= GENERICDEF_USE_RGBAGEN;
Expand Down Expand Up @@ -2644,10 +2646,10 @@ shaderProgram_t *GLSL_GetGenericShaderProgram(int stage)
shaderAttribs |= GENERICDEF_USE_TCGEN_AND_TCMOD;
}

if (pStage->glow)
/*if (pStage->glow)
{
shaderAttribs |= GENERICDEF_USE_GLOW_BUFFER;
}
}*/

return &tr.genericShader[shaderAttribs];
}
7 changes: 5 additions & 2 deletions shared/rd-rend2/tr_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2838,7 +2838,10 @@ image_t *R_BuildSDRSpecGlossImage(shaderStage_t *stage, const char *specImageNam
}
Hunk_FreeTempMemory(specPic);

return R_CreateImage(sdrName, sdrSpecPic, specWidth, specHeight, IMGTYPE_COLORALPHA, flags & ~IMGFLAG_SRGB, 0);
image_t *outImage = R_CreateImage(sdrName, sdrSpecPic, specWidth, specHeight, IMGTYPE_COLORALPHA, flags & ~IMGFLAG_SRGB, 0);
Hunk_FreeTempMemory(sdrSpecPic);

return outImage;
}

static void R_CreateNormalMap ( const char *name, byte *pic, int width, int height, int flags )
Expand All @@ -2864,7 +2867,7 @@ static void R_CreateNormalMap ( const char *name, byte *pic, int width, int heig

normalWidth = width;
normalHeight = height;
normalPic = (byte *)R_Malloc(width * height * 4, TAG_GENERAL);
normalPic = (byte *)R_Malloc(width * height * 4, TAG_TEMP_WORKSPACE);
RGBAtoNormal(pic, normalPic, width, height, (qboolean)(flags & IMGFLAG_CLAMPTOEDGE));

#if 1
Expand Down
Loading

0 comments on commit 05f1ef9

Please sign in to comment.