Skip to content

Commit

Permalink
drivers/glrend/renderer: implement basic sampler cache
Browse files Browse the repository at this point in the history
Not used for anything yet(tm).
  • Loading branch information
vs49688 committed Oct 10, 2024
1 parent 69f9a42 commit 7cab1c4
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
4 changes: 4 additions & 0 deletions drivers/glrend/drv.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ extern "C" {
#define BR_GLREND_DEBUG_INTERNAL_PREFIX "glrend:internal:"
#define BR_GLREND_DEBUG_USER_PREFIX "glrend:user:"

/*
* The maximum number of samplers. Probably overkill.
*/
#define BR_GLREND_MAX_SAMPLERS 256

/*
* Maximum number of possible entries on a CLUT.
Expand Down
2 changes: 2 additions & 0 deletions drivers/glrend/drv_ip.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ void RendererGLUnrefState(br_renderer *self, state_stack *state);

br_int_32 RendererGLNextImmTri(br_renderer *self, struct v11group *group, br_vector3_u16 fp);

GLuint RendererGLGetSampler(br_renderer *self, const br_sampler_info_gl *info);

/*
* devpixmp.c
*/
Expand Down
47 changes: 47 additions & 0 deletions drivers/glrend/renderer.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ br_renderer *RendererGLAllocate(br_device *device, br_renderer_facility *facilit

RendererGLInitImm(self, &dest->screen->asFront.video);

BrMemSet(self->sampler_pool, 0, sizeof(self->sampler_pool));
self->sampler_pool_size = BR_GLREND_MAX_SAMPLERS;
self->sampler_count = 0;

ObjectContainerAddFront(facility, (br_object *)self);

StateGLInit(&self->state, self->device);
Expand Down Expand Up @@ -181,6 +185,11 @@ static void BR_CMETHOD_DECL(br_renderer_gl, free)(br_object *_self)

RendererGLFreeImm(self);

for(br_size_t i = 0; i < self->sampler_pool_size; ++i) {
if(self->sampler_pool[i].sampler != 0)
glDeleteSamplers(1, &self->sampler_pool[i].sampler);
}

BrPoolFree(self->state_pool);

ObjectContainerRemove(self->renderer_facility, (br_object *)self);
Expand Down Expand Up @@ -772,3 +781,41 @@ br_int_32 RendererGLNextImmTri(br_renderer *self, struct v11group *group, br_vec

return (br_int_32)base;
}

static int sampler_comp(const void *a, const void *b)
{
const br_sampler_info_gl *key = a;
const br_sampler_gl *sampler = b;

return BrMemCmp(key, &sampler->key, sizeof(br_sampler_info_gl));
}

GLuint RendererGLGetSampler(br_renderer *self, const br_sampler_info_gl *info)
{
br_sampler_gl *smp;

smp = BrBSearch(info, self->sampler_pool, self->sampler_count, sizeof(br_sampler_gl), sampler_comp);
if(smp != NULL)
return smp->sampler;

if(self->sampler_count == self->sampler_pool_size)
return 0;

smp = self->sampler_pool + self->sampler_count++;
smp->key = *info;

glGenSamplers(1, &smp->sampler);

glSamplerParameteri(smp->sampler, GL_TEXTURE_WRAP_S, info->wrap_s);
glSamplerParameteri(smp->sampler, GL_TEXTURE_WRAP_T, info->wrap_t);
glSamplerParameteri(smp->sampler, GL_TEXTURE_MIN_FILTER, info->filter_min);
glSamplerParameteri(smp->sampler, GL_TEXTURE_MAG_FILTER, info->filter_mag);

if(GLAD_GL_ARB_texture_filter_anisotropic && info->filter_min != GL_NEAREST && info->filter_mag != GL_NEAREST) {
glSamplerParameterf(smp->sampler, GL_TEXTURE_MAX_ANISOTROPY, self->pixelmap->screen->asFront.video.maxAnisotropy);
}

BrQsort(self->sampler_pool, self->sampler_count, sizeof(br_sampler_gl), sampler_comp);

return smp->sampler;
}
16 changes: 16 additions & 0 deletions drivers/glrend/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ typedef struct br_immvert_gl {
} br_immvert_gl;
#pragma pack(pop)

typedef struct br_sampler_info_gl {
GLint filter_min;
GLint filter_mag;
GLint wrap_s;
GLint wrap_t;
} br_sampler_info_gl;

typedef struct br_sampler_gl {
br_sampler_info_gl key;
GLuint sampler;
} br_sampler_gl;

#ifdef BR_RENDERER_PRIVATE

typedef struct br_renderer {
Expand Down Expand Up @@ -58,6 +70,10 @@ typedef struct br_renderer {

state_all state;

br_sampler_gl sampler_pool[BR_GLREND_MAX_SAMPLERS];
br_size_t sampler_pool_size;
br_size_t sampler_count;

int has_begun;

struct {
Expand Down

0 comments on commit 7cab1c4

Please sign in to comment.