render: Replaced SDL_RenderFlush with SDL_FlushRenderer.

This uses the same `SDL_VerbNoun` format as the rest of SDL3, and also
adds stronger effort to invalidate cached state in the backend, so cooperation
improves with apps that are using lowlevel rendering APIs directly.

Fixes #367.
This commit is contained in:
Ryan C. Gordon 2023-11-25 22:41:23 -05:00
parent eef5c53668
commit dfee3f9e92
19 changed files with 156 additions and 18 deletions

View file

@ -75,10 +75,13 @@ typedef struct
SDL_bool cliprect_dirty;
SDL_Rect cliprect;
SDL_bool texturing;
SDL_bool texturing_dirty;
SDL_bool vertex_array;
SDL_bool color_array;
SDL_bool texture_array;
SDL_bool color_dirty;
Uint32 color;
SDL_bool clear_color_dirty;
Uint32 clear_color;
} GL_DrawStateCache;
@ -450,7 +453,7 @@ static int GL_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SDL_Pr
GL_ActivateRenderer(renderer);
renderdata->drawstate.texture = NULL; /* we trash this state. */
renderdata->drawstate.texturing = SDL_FALSE; /* we trash this state. */
renderdata->drawstate.texturing_dirty = SDL_TRUE; /* we trash this state. */
if (texture->access == SDL_TEXTUREACCESS_TARGET &&
!renderdata->GL_EXT_framebuffer_object_supported) {
@ -1108,7 +1111,7 @@ static int SetDrawState(GL_RenderData *data, const SDL_RenderCommand *cmd, const
data->drawstate.shader = shader;
}
if ((cmd->data.draw.texture != NULL) != data->drawstate.texturing) {
if (data->drawstate.texturing_dirty || ((cmd->data.draw.texture != NULL) != data->drawstate.texturing)) {
if (!cmd->data.draw.texture) {
data->glDisable(data->textype);
data->drawstate.texturing = SDL_FALSE;
@ -1116,6 +1119,7 @@ static int SetDrawState(GL_RenderData *data, const SDL_RenderCommand *cmd, const
data->glEnable(data->textype);
data->drawstate.texturing = SDL_TRUE;
}
data->drawstate.texturing_dirty = SDL_FALSE;
}
vertex_array = cmd->command == SDL_RENDERCMD_DRAW_POINTS || cmd->command == SDL_RENDERCMD_DRAW_LINES || cmd->command == SDL_RENDERCMD_GEOMETRY;
@ -1193,6 +1197,25 @@ static int SetCopyState(GL_RenderData *data, const SDL_RenderCommand *cmd)
return 0;
}
static void GL_InvalidateCachedState(SDL_Renderer *renderer)
{
GL_DrawStateCache *cache = &((GL_RenderData *)renderer->driverdata)->drawstate;
cache->viewport_dirty = SDL_TRUE;
cache->texture = NULL;
cache->drawablew = 0;
cache->drawableh = 0;
cache->blend = SDL_BLENDMODE_INVALID;
cache->shader = SHADER_INVALID;
cache->cliprect_enabled_dirty = SDL_TRUE;
cache->cliprect_dirty = SDL_TRUE;
cache->texturing_dirty = SDL_TRUE;
cache->vertex_array = SDL_FALSE; /* !!! FIXME: this resets to false at the end of GL_RunCommandQueue, but we could cache this more aggressively. */
cache->color_array = SDL_FALSE; /* !!! FIXME: this resets to false at the end of GL_RunCommandQueue, but we could cache this more aggressively. */
cache->texture_array = SDL_FALSE; /* !!! FIXME: this resets to false at the end of GL_RunCommandQueue, but we could cache this more aggressively. */
cache->color_dirty = SDL_TRUE;
cache->clear_color_dirty = SDL_TRUE;
}
static int GL_RunCommandQueue(SDL_Renderer *renderer, SDL_RenderCommand *cmd, void *vertices, size_t vertsize)
{
/* !!! FIXME: it'd be nice to use a vertex buffer instead of immediate mode... */
@ -1230,9 +1253,10 @@ static int GL_RunCommandQueue(SDL_Renderer *renderer, SDL_RenderCommand *cmd, vo
const Uint8 b = cmd->data.color.b;
const Uint8 a = cmd->data.color.a;
const Uint32 color = (((Uint32)a << 24) | (r << 16) | (g << 8) | b);
if (color != data->drawstate.color) {
if ((data->drawstate.color_dirty) || (color != data->drawstate.color)) {
data->glColor4ub((GLubyte)r, (GLubyte)g, (GLubyte)b, (GLubyte)a);
data->drawstate.color = color;
data->drawstate.color_dirty = SDL_FALSE;
}
break;
}
@ -1269,13 +1293,14 @@ static int GL_RunCommandQueue(SDL_Renderer *renderer, SDL_RenderCommand *cmd, vo
const Uint8 b = cmd->data.color.b;
const Uint8 a = cmd->data.color.a;
const Uint32 color = (((Uint32)a << 24) | (r << 16) | (g << 8) | b);
if (color != data->drawstate.clear_color) {
if ((data->drawstate.clear_color_dirty) || (color != data->drawstate.clear_color)) {
const GLfloat fr = ((GLfloat)r) * inv255f;
const GLfloat fg = ((GLfloat)g) * inv255f;
const GLfloat fb = ((GLfloat)b) * inv255f;
const GLfloat fa = ((GLfloat)a) * inv255f;
data->glClearColor(fr, fg, fb, fa);
data->drawstate.clear_color = color;
data->drawstate.clear_color_dirty = SDL_FALSE;
}
if (data->drawstate.cliprect_enabled || data->drawstate.cliprect_enabled_dirty) {
@ -1797,6 +1822,7 @@ static SDL_Renderer *GL_CreateRenderer(SDL_Window *window, SDL_PropertiesID crea
renderer->QueueDrawPoints = GL_QueueDrawPoints;
renderer->QueueDrawLines = GL_QueueDrawLines;
renderer->QueueGeometry = GL_QueueGeometry;
renderer->InvalidateCachedState = GL_InvalidateCachedState;
renderer->RunCommandQueue = GL_RunCommandQueue;
renderer->RenderReadPixels = GL_RenderReadPixels;
renderer->RenderPresent = GL_RenderPresent;