Skip to content

Commit

Permalink
Remove glDrawElementsInstancedBaseInstance support
Browse files Browse the repository at this point in the history
This extension is unimportant, and actually severely degrades ANGLE
performance over Direct3D to unacceptable levels. This also removes the
alternative sprite batching codepath making use of this functionality.
The renderer API hasn't changed yet; attempting to use the extension
will trigger an assertion failure.
  • Loading branch information
Akaricchi committed Apr 30, 2021
1 parent a41dc26 commit 5de530e
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 119 deletions.
32 changes: 4 additions & 28 deletions src/renderer/common/sprite_batch.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,7 @@ void _r_sprite_batch_init(void) {
#undef VERTEX_OFS
#undef INSTANCE_OFS

uint capacity;

if(r_supports(RFEAT_DRAW_INSTANCED_BASE_INSTANCE)) {
capacity = 1 << 15;
} else {
capacity = 1 << 11;
}
uint capacity = 1 << 11;

_r_sprite_batch.vbuf = r_vertex_buffer_create(sz_attr * capacity, NULL);
r_vertex_buffer_set_debug_label(_r_sprite_batch.vbuf, "Sprite batch vertex buffer");
Expand Down Expand Up @@ -165,22 +159,8 @@ void r_flush_sprites(void) {
r_cull(_r_sprite_batch.cull_mode);
}

if(_r_sprite_batch.renderer_features & r_feature_bit(RFEAT_DRAW_INSTANCED_BASE_INSTANCE)) {
r_draw_model_ptr(&_r_sprite_batch.quad, pending, _r_sprite_batch.base_instance);
_r_sprite_batch.base_instance += pending;

SDL_RWops *stream = r_vertex_buffer_get_stream(_r_sprite_batch.vbuf);
size_t remaining = SDL_RWsize(stream) - SDL_RWtell(stream);

if(remaining < SIZEOF_SPRITE_ATTRIBS) {
// log_debug("Invalidating after %u sprites", _r_sprite_batch.base_instance);
r_vertex_buffer_invalidate(_r_sprite_batch.vbuf);
_r_sprite_batch.base_instance = 0;
}
} else {
r_draw_model_ptr(&_r_sprite_batch.quad, pending, 0);
r_vertex_buffer_invalidate(_r_sprite_batch.vbuf);
}
r_draw_model_ptr(&_r_sprite_batch.quad, pending, 0);
r_vertex_buffer_invalidate(_r_sprite_batch.vbuf);

r_mat_proj_pop();
r_state_pop();
Expand Down Expand Up @@ -365,11 +345,7 @@ static SDL_RWops *_r_sprite_batch_prepare_buffer(void) {

if(remaining < SIZEOF_SPRITE_ATTRIBS) {
// TODO: maybe it is better to grow the buffer instead?

if(!r_supports(RFEAT_DRAW_INSTANCED_BASE_INSTANCE)) {
log_warn("Vertex buffer exhausted (%zu needed for next sprite, %zu remaining), flush forced", SIZEOF_SPRITE_ATTRIBS, remaining);
}

log_warn("Vertex buffer exhausted (%zu needed for next sprite, %zu remaining), flush forced", SIZEOF_SPRITE_ATTRIBS, remaining);
r_flush_sprites();
}

Expand Down
26 changes: 4 additions & 22 deletions src/renderer/gl33/gl33.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,6 @@ static void gl33_init_context(SDL_Window *window) {

if(glext.instanced_arrays) {
R.features |= r_feature_bit(RFEAT_DRAW_INSTANCED);

if(glext.base_instance) {
R.features |= r_feature_bit(RFEAT_DRAW_INSTANCED_BASE_INSTANCE);
}
}

if(glext.depth_texture) {
Expand Down Expand Up @@ -985,21 +981,14 @@ void gl33_end_draw(void *state) {

static void gl33_draw(VertexArray *varr, Primitive prim, uint firstvert, uint count, uint instances, uint base_instance) {
assert(count > 0);
assert(base_instance == 0);
GLuint gl_prim = gl33_prim_to_gl_prim(prim);

void *state;
gl33_begin_draw(varr, &state);

if(instances) {
if(base_instance) {
#ifdef STATIC_GLES3
log_fatal("base_instance is not supported");
#else
glDrawArraysInstancedBaseInstance(gl_prim, firstvert, count, instances, base_instance);
#endif
} else {
glDrawArraysInstanced(gl_prim, firstvert, count, instances);
}
glDrawArraysInstanced(gl_prim, firstvert, count, instances);
} else {
glDrawArrays(gl_prim, firstvert, count);
}
Expand All @@ -1009,6 +998,7 @@ static void gl33_draw(VertexArray *varr, Primitive prim, uint firstvert, uint co

static void gl33_draw_indexed(VertexArray *varr, Primitive prim, uint firstidx, uint count, uint instances, uint base_instance) {
assert(count > 0);
assert(base_instance == 0);
assert(varr->index_attachment != NULL);
GLuint gl_prim = gl33_prim_to_gl_prim(prim);

Expand All @@ -1018,15 +1008,7 @@ static void gl33_draw_indexed(VertexArray *varr, Primitive prim, uint firstidx,
uintptr_t iofs = firstidx * sizeof(gl33_ibo_index_t);

if(instances) {
if(base_instance) {
#ifdef STATIC_GLES3
log_fatal("base_instance is not supported");
#else
glDrawElementsInstancedBaseInstance(gl_prim, count, GL33_IBO_GL_DATATYPE, (void*)iofs, instances, base_instance);
#endif
} else {
glDrawElementsInstanced(gl_prim, count, GL33_IBO_GL_DATATYPE, (void*)iofs, instances);
}
glDrawElementsInstanced(gl_prim, count, GL33_IBO_GL_DATATYPE, (void*)iofs, instances);
} else {
glDrawElements(gl_prim, count, GL33_IBO_GL_DATATYPE, (void*)iofs);
}
Expand Down
63 changes: 0 additions & 63 deletions src/renderer/glcommon/opengl.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,6 @@ struct glext_s glext;

typedef void (*glad_glproc_ptr)(void);

#ifndef STATIC_GLES3
//
// Extension not yet handled by glad
//

// GL_ANGLE_base_vertex_base_instance
typedef void (APIENTRYP PFNGLDRAWARRAYSINSTANCEDBASEINSTANCEANGLEPROC)(GLenum mode, GLint first, GLsizei count, GLsizei instanceCount, GLuint baseInstance);
static PFNGLDRAWARRAYSINSTANCEDBASEINSTANCEANGLEPROC glad_glDrawArraysInstancedBaseInstanceANGLE;
typedef void (APIENTRYP PFNGLDRAWELEMENTSINSTANCEDBASEVERTEXBASEINSTANCEANGLEPROC)(GLenum mode, GLsizei count, GLenum type, const void *indices, GLsizei instancecount, GLint basevertex, GLuint baseinstance);
static PFNGLDRAWELEMENTSINSTANCEDBASEVERTEXBASEINSTANCEANGLEPROC glad_glDrawElementsInstancedBaseVertexBaseInstanceANGLE;

APIENTRY
static void glad_glDrawElementsInstancedBaseInstanceANGLE(GLenum mode, GLsizei count, GLenum type, const void *indices, GLsizei instancecount, GLuint baseinstance) {
// shim
glad_glDrawElementsInstancedBaseVertexBaseInstanceANGLE(mode, count, type, indices, instancecount, 0, baseinstance);
}
#endif

// WEBGL_debug_renderer_info
const GLenum GL_UNMASKED_VENDOR_WEBGL = 0x9245;
const GLenum GL_UNMASKED_RENDERER_WEBGL = 0x9246;
Expand Down Expand Up @@ -235,47 +217,6 @@ static void glcommon_ext_debug_output(void) {
log_warn("Extension not supported");
}

static void glcommon_ext_base_instance(void) {
#ifndef STATIC_GLES3
if(
GL_ATLEAST(4, 2)
&& (glext.DrawArraysInstancedBaseInstance = GL_FUNC(DrawArraysInstancedBaseInstance))
&& (glext.DrawElementsInstancedBaseInstance = GL_FUNC(DrawElementsInstancedBaseInstance))
) {
glext.base_instance = TSGL_EXTFLAG_NATIVE;
log_info("Using core functionality");
return;
}

if((glext.base_instance = glcommon_check_extension("GL_ARB_base_instance"))
&& (glext.DrawArraysInstancedBaseInstance = GL_FUNC(DrawArraysInstancedBaseInstance))
&& (glext.DrawElementsInstancedBaseInstance = GL_FUNC(DrawElementsInstancedBaseInstance))
) {
log_info("Using GL_ARB_base_instance");
return;
}

if((glext.base_instance = glcommon_check_extension("GL_EXT_base_instance"))
&& (glext.DrawArraysInstancedBaseInstance = GL_FUNC(DrawArraysInstancedBaseInstanceEXT))
&& (glext.DrawElementsInstancedBaseInstance = GL_FUNC(DrawElementsInstancedBaseInstanceEXT))
) {
log_info("Using GL_EXT_base_instance");
return;
}

if((glext.base_instance = glcommon_check_extension("GL_ANGLE_base_vertex_base_instance"))
&& (glext.DrawArraysInstancedBaseInstance = GL_FUNC(DrawArraysInstancedBaseInstanceANGLE))
&& (glext.DrawElementsInstancedBaseInstance = GL_FUNC(DrawElementsInstancedBaseInstanceANGLE))
) {
log_info("Using GL_ANGLE_base_vertex_base_instance");
return;
}
#endif

glext.base_instance = 0;
log_warn("Extension not supported");
}

static void glcommon_ext_pixel_buffer_object(void) {
#ifdef STATIC_GLES3
glext.pixel_buffer_object = TSGL_EXTFLAG_NATIVE;
Expand Down Expand Up @@ -913,7 +854,6 @@ void glcommon_check_capabilities(void) {
SDL_RWclose(writer);
}

glcommon_ext_base_instance();
glcommon_ext_clear_texture();
glcommon_ext_color_buffer_float();
glcommon_ext_debug_output();
Expand Down Expand Up @@ -996,9 +936,6 @@ void glcommon_load_functions(void) {
log_fatal("Failed to load OpenGL functions");
}
}

glad_glDrawArraysInstancedBaseInstanceANGLE = (PFNGLDRAWARRAYSINSTANCEDBASEINSTANCEANGLEPROC)load_func("glDrawArraysInstancedBaseInstanceANGLE");
glad_glDrawElementsInstancedBaseVertexBaseInstanceANGLE = (PFNGLDRAWELEMENTSINSTANCEDBASEVERTEXBASEINSTANCEANGLEPROC)load_func("glDrawElementsInstancedBaseVertexBaseInstanceANGLE");
#endif
}

Expand Down
1 change: 0 additions & 1 deletion src/renderer/glcommon/opengl.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ struct glext_s {
uchar avoid_sampler_uniform_updates : 1;
} issues;

ext_flag_t base_instance;
ext_flag_t clear_texture;
ext_flag_t color_buffer_float;
ext_flag_t debug_output;
Expand Down
7 changes: 2 additions & 5 deletions src/renderer/gles20/gles20.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ static void gles20_init_context(SDL_Window *w) {

static void gles20_draw_indexed(VertexArray *varr, Primitive prim, uint firstidx, uint count, uint instances, uint base_instance) {
assert(count > 0);
assert(base_instance == 0);
assert(varr->index_attachment != NULL);
GLuint gl_prim = gl33_prim_to_gl_prim(prim);

Expand All @@ -40,11 +41,7 @@ static void gles20_draw_indexed(VertexArray *varr, Primitive prim, uint firstidx
assert(indices < ibuf->elements + ibuf->num_elements);

if(instances) {
if(base_instance) {
glDrawElementsInstancedBaseInstance(gl_prim, count, GLES20_IBO_GL_DATATYPE, indices, instances, base_instance);
} else {
glDrawElementsInstanced(gl_prim, count, GLES20_IBO_GL_DATATYPE, indices, instances);
}
glDrawElementsInstanced(gl_prim, count, GLES20_IBO_GL_DATATYPE, indices, instances);
} else {
glDrawElements(gl_prim, count, GLES20_IBO_GL_DATATYPE, indices);
}
Expand Down

0 comments on commit 5de530e

Please sign in to comment.