[glsl] Update particle arrays when maximum changes

The cvar setup for particles is a bit wonky in that the arrays get
initialized using the default max particle count but never updated.
Though things could be improved some more, this solution works (and has
been more or less copied to gl, but I couldn't reproduce the crash
there, or even the valgrind error).
This commit is contained in:
Bill Currie 2022-03-25 14:48:01 +09:00
parent 286344c7b6
commit 25e6865fa5
2 changed files with 42 additions and 23 deletions

View file

@ -63,23 +63,23 @@ static int pVAsize;
static int *pVAindices; static int *pVAindices;
static varray_t2f_c4ub_v3f_t *particleVertexArray; static varray_t2f_c4ub_v3f_t *particleVertexArray;
void static void
gl_R_InitParticles (void) alloc_arrays (psystem_t *ps)
{ {
int i; int i;
if (r_psystem.maxparticles && r_init) { if (ps->maxparticles && r_init) {
if (vaelements) { if (vaelements) {
partUseVA = 0; partUseVA = 0;
pVAsize = r_psystem.maxparticles * 4; pVAsize = ps->maxparticles * 4;
Sys_MaskPrintf (SYS_dev, Sys_MaskPrintf (SYS_dev,
"Particles: Vertex Array use disabled.\n"); "Particles: Vertex Array use disabled.\n");
} else { } else {
if (vaelements > 3) if (vaelements > 3)
pVAsize = min ((unsigned int) (vaelements - (vaelements % 4)), pVAsize = min ((unsigned int) (vaelements - (vaelements % 4)),
r_psystem.maxparticles * 4); ps->maxparticles * 4);
else if (vaelements >= 0) else if (vaelements >= 0)
pVAsize = r_psystem.maxparticles * 4; pVAsize = ps->maxparticles * 4;
Sys_MaskPrintf (SYS_dev, Sys_MaskPrintf (SYS_dev,
"Particles: %i maximum vertex elements.\n", "Particles: %i maximum vertex elements.\n",
pVAsize); pVAsize);
@ -109,6 +109,12 @@ gl_R_InitParticles (void)
} }
} }
void
gl_R_InitParticles (void)
{
alloc_arrays (&r_psystem);
}
void void
gl_R_DrawParticles (psystem_t *psystem) gl_R_DrawParticles (psystem_t *psystem)
{ {
@ -238,12 +244,14 @@ static void
r_particles_f (cvar_t *var) r_particles_f (cvar_t *var)
{ {
R_MaxParticlesCheck (var, r_particles_max); R_MaxParticlesCheck (var, r_particles_max);
alloc_arrays (&r_psystem);
} }
static void static void
r_particles_max_f (cvar_t *var) r_particles_max_f (cvar_t *var)
{ {
R_MaxParticlesCheck (r_particles, var); R_MaxParticlesCheck (r_particles, var);
alloc_arrays (&r_psystem);
} }
void void

View file

@ -63,6 +63,7 @@
# define GL_VERTEX_PROGRAM_POINT_SIZE 0x8642 # define GL_VERTEX_PROGRAM_POINT_SIZE 0x8642
#endif #endif
static uint32_t maxparticles;
static GLushort *pVAindices; static GLushort *pVAindices;
static partvert_t *particleVertexArray; static partvert_t *particleVertexArray;
@ -130,11 +131,34 @@ static struct {
{"fog", 1}, {"fog", 1},
}; };
static void
alloc_arrays (psystem_t *ps)
{
if (ps->maxparticles > maxparticles) {
maxparticles = ps->maxparticles;
if (particleVertexArray)
free (particleVertexArray);
particleVertexArray = calloc (ps->maxparticles * 4,
sizeof (partvert_t));
if (pVAindices)
free (pVAindices);
pVAindices = calloc (ps->maxparticles * 6, sizeof (GLushort));
for (uint32_t i = 0; i < ps->maxparticles; i++) {
pVAindices[i * 6 + 0] = i * 4 + 0;
pVAindices[i * 6 + 1] = i * 4 + 1;
pVAindices[i * 6 + 2] = i * 4 + 2;
pVAindices[i * 6 + 3] = i * 4 + 0;
pVAindices[i * 6 + 4] = i * 4 + 2;
pVAindices[i * 6 + 5] = i * 4 + 3;
}
}
}
void void
glsl_R_InitParticles (void) glsl_R_InitParticles (void)
{ {
shader_t *vert_shader, *frag_shader; shader_t *vert_shader, *frag_shader;
unsigned i;
int vert; int vert;
int frag; int frag;
float v[2] = {0, 0}; float v[2] = {0, 0};
@ -196,22 +220,7 @@ glsl_R_InitParticles (void)
GL_UNSIGNED_BYTE, tex->data); GL_UNSIGNED_BYTE, tex->data);
free (tex); free (tex);
if (particleVertexArray) alloc_arrays (&r_psystem);
free (particleVertexArray);
particleVertexArray = calloc (r_psystem.maxparticles * 4,
sizeof (partvert_t));
if (pVAindices)
free (pVAindices);
pVAindices = calloc (r_psystem.maxparticles * 6, sizeof (GLushort));
for (i = 0; i < r_psystem.maxparticles; i++) {
pVAindices[i * 6 + 0] = i * 4 + 0;
pVAindices[i * 6 + 1] = i * 4 + 1;
pVAindices[i * 6 + 2] = i * 4 + 2;
pVAindices[i * 6 + 3] = i * 4 + 0;
pVAindices[i * 6 + 4] = i * 4 + 2;
pVAindices[i * 6 + 5] = i * 4 + 3;
}
} }
static void static void
@ -426,12 +435,14 @@ static void
r_particles_f (cvar_t *var) r_particles_f (cvar_t *var)
{ {
R_MaxParticlesCheck (var, r_particles_max); R_MaxParticlesCheck (var, r_particles_max);
alloc_arrays (&r_psystem);
} }
static void static void
r_particles_max_f (cvar_t *var) r_particles_max_f (cvar_t *var)
{ {
R_MaxParticlesCheck (r_particles, var); R_MaxParticlesCheck (r_particles, var);
alloc_arrays (&r_psystem);
} }
void void