Avoid redundant state changes in the GLES2 renderer.

This commit is contained in:
Ryan C. Gordon 2013-10-05 00:29:57 -04:00
parent 500e4f6f9a
commit 5607cc45c5
2 changed files with 119 additions and 64 deletions

View File

@ -81,6 +81,7 @@ typedef struct GLES2_ShaderCacheEntry
GLES2_ShaderType type; GLES2_ShaderType type;
const GLES2_ShaderInstance *instance; const GLES2_ShaderInstance *instance;
int references; int references;
Uint8 modulation_r, modulation_g, modulation_b, modulation_a;
struct GLES2_ShaderCacheEntry *prev; struct GLES2_ShaderCacheEntry *prev;
struct GLES2_ShaderCacheEntry *next; struct GLES2_ShaderCacheEntry *next;
} GLES2_ShaderCacheEntry; } GLES2_ShaderCacheEntry;
@ -98,6 +99,9 @@ typedef struct GLES2_ProgramCacheEntry
GLES2_ShaderCacheEntry *vertex_shader; GLES2_ShaderCacheEntry *vertex_shader;
GLES2_ShaderCacheEntry *fragment_shader; GLES2_ShaderCacheEntry *fragment_shader;
GLuint uniform_locations[16]; GLuint uniform_locations[16];
Uint8 color_r, color_g, color_b, color_a;
Uint8 modulation_r, modulation_g, modulation_b, modulation_a;
GLfloat projection[4][4];
struct GLES2_ProgramCacheEntry *prev; struct GLES2_ProgramCacheEntry *prev;
struct GLES2_ProgramCacheEntry *next; struct GLES2_ProgramCacheEntry *next;
} GLES2_ProgramCacheEntry; } GLES2_ProgramCacheEntry;
@ -156,6 +160,7 @@ typedef struct GLES2_DriverContext
GLES2_ShaderCache shader_cache; GLES2_ShaderCache shader_cache;
GLES2_ProgramCache program_cache; GLES2_ProgramCache program_cache;
GLES2_ProgramCacheEntry *current_program; GLES2_ProgramCacheEntry *current_program;
Uint8 clear_r, clear_g, clear_b, clear_a;
} GLES2_DriverContext; } GLES2_DriverContext;
#define GLES2_MAX_CACHED_PROGRAMS 8 #define GLES2_MAX_CACHED_PROGRAMS 8
@ -492,8 +497,6 @@ GLES2_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture)
return -1; return -1;
} }
texture->driverdata = data; texture->driverdata = data;
renderdata->glActiveTexture(GL_TEXTURE0);
renderdata->glBindTexture(data->texture_type, data->texture); renderdata->glBindTexture(data->texture_type, data->texture);
renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_MIN_FILTER, scaleMode); renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_MIN_FILTER, scaleMode);
renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_MAG_FILTER, scaleMode); renderdata->glTexParameteri(data->texture_type, GL_TEXTURE_MAG_FILTER, scaleMode);
@ -549,9 +552,7 @@ GLES2_UpdateTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect
} }
/* Create a texture subimage with the supplied data */ /* Create a texture subimage with the supplied data */
data->glActiveTexture(GL_TEXTURE0);
data->glBindTexture(tdata->texture_type, tdata->texture); data->glBindTexture(tdata->texture_type, tdata->texture);
data->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
data->glTexSubImage2D(tdata->texture_type, data->glTexSubImage2D(tdata->texture_type,
0, 0,
rect->x, rect->x,
@ -720,7 +721,16 @@ GLES2_CacheProgram(SDL_Renderer *renderer, GLES2_ShaderCacheEntry *vertex,
entry->uniform_locations[GLES2_UNIFORM_MODULATION] = entry->uniform_locations[GLES2_UNIFORM_MODULATION] =
data->glGetUniformLocation(entry->id, "u_modulation"); data->glGetUniformLocation(entry->id, "u_modulation");
entry->uniform_locations[GLES2_UNIFORM_COLOR] = entry->uniform_locations[GLES2_UNIFORM_COLOR] =
rdata->glGetUniformLocation(entry->id, "u_color"); data->glGetUniformLocation(entry->id, "u_color");
entry->modulation_r = entry->modulation_g = entry->modulation_b = entry->modulation_a = 1.0f;
entry->color_r = entry->color_g = entry->color_b = entry->color_a = 1.0f;
data->glUseProgram(entry->id);
data->glUniformMatrix4fv(entry->uniform_locations[GLES2_UNIFORM_PROJECTION], 1, GL_FALSE, (GLfloat *)entry->projection);
data->glUniform1i(entry->uniform_locations[GLES2_UNIFORM_TEXTURE], 0); /* always texture unit 0. */
data->glUniform4f(entry->uniform_locations[GLES2_UNIFORM_MODULATION], 1.0f, 1.0f, 1.0f, 1.0f);
data->glUniform4f(entry->uniform_locations[GLES2_UNIFORM_COLOR], 1.0f, 1.0f, 1.0f, 1.0f);
/* Cache the linked program */ /* Cache the linked program */
if (data->program_cache.head) if (data->program_cache.head)
@ -959,7 +969,6 @@ GLES2_SetOrthographicProjection(SDL_Renderer *renderer)
{ {
GLES2_DriverContext *data = (GLES2_DriverContext *)renderer->driverdata; GLES2_DriverContext *data = (GLES2_DriverContext *)renderer->driverdata;
GLfloat projection[4][4]; GLfloat projection[4][4];
GLuint locProjection;
if (!renderer->viewport.w || !renderer->viewport.h) { if (!renderer->viewport.w || !renderer->viewport.h) {
return 0; return 0;
@ -992,8 +1001,12 @@ GLES2_SetOrthographicProjection(SDL_Renderer *renderer)
projection[3][3] = 1.0f; projection[3][3] = 1.0f;
/* Set the projection matrix */ /* Set the projection matrix */
locProjection = data->current_program->uniform_locations[GLES2_UNIFORM_PROJECTION]; if (SDL_memcmp(data->current_program->projection, projection, sizeof (projection)) != 0) {
data->glUniformMatrix4fv(locProjection, 1, GL_FALSE, (GLfloat *)projection); const GLuint locProjection = data->current_program->uniform_locations[GLES2_UNIFORM_PROJECTION];
data->glUniformMatrix4fv(locProjection, 1, GL_FALSE, (GLfloat *)projection);
SDL_memcpy(data->current_program->projection, projection, sizeof (projection));
}
return 0; return 0;
} }
@ -1024,10 +1037,18 @@ GLES2_RenderClear(SDL_Renderer * renderer)
GLES2_ActivateRenderer(renderer); GLES2_ActivateRenderer(renderer);
data->glClearColor((GLfloat) renderer->r * inv255f, /* !!! FIXME: it'd be nice to do a single 32-bit compare here. */
(GLfloat) renderer->g * inv255f, if ( (data->clear_r != renderer->r) || (data->clear_g != renderer->g) ||
(GLfloat) renderer->b * inv255f, (data->clear_b != renderer->b) || (data->clear_a != renderer->a) ) {
(GLfloat) renderer->a * inv255f); data->glClearColor((GLfloat) renderer->r * inv255f,
(GLfloat) renderer->g * inv255f,
(GLfloat) renderer->b * inv255f,
(GLfloat) renderer->a * inv255f);
data->clear_r = renderer->r;
data->clear_g = renderer->g;
data->clear_b = renderer->b;
data->clear_a = renderer->a;
}
data->glClear(GL_COLOR_BUFFER_BIT); data->glClear(GL_COLOR_BUFFER_BIT);
@ -1077,8 +1098,9 @@ static int
GLES2_SetDrawingState(SDL_Renderer * renderer) GLES2_SetDrawingState(SDL_Renderer * renderer)
{ {
GLES2_DriverContext *data = (GLES2_DriverContext *)renderer->driverdata; GLES2_DriverContext *data = (GLES2_DriverContext *)renderer->driverdata;
int blendMode = renderer->blendMode; const int blendMode = renderer->blendMode;
GLuint locColor; GLES2_ProgramCacheEntry *program;
Uint8 r, g, b, a;
GLES2_ActivateRenderer(renderer); GLES2_ActivateRenderer(renderer);
@ -1087,26 +1109,35 @@ GLES2_SetDrawingState(SDL_Renderer * renderer)
GLES2_SetTexCoords(data, SDL_FALSE); GLES2_SetTexCoords(data, SDL_FALSE);
/* Activate an appropriate shader and set the projection matrix */ /* Activate an appropriate shader and set the projection matrix */
if (GLES2_SelectProgram(renderer, GLES2_IMAGESOURCE_SOLID, blendMode) < 0) if (GLES2_SelectProgram(renderer, GLES2_IMAGESOURCE_SOLID, blendMode) < 0) {
return -1; return -1;
}
/* Select the color to draw with */ /* Select the color to draw with */
locColor = data->current_program->uniform_locations[GLES2_UNIFORM_COLOR]; g = renderer->g;
a = renderer->a;
if (renderer->target && if (renderer->target &&
(renderer->target->format == SDL_PIXELFORMAT_ARGB8888 || (renderer->target->format == SDL_PIXELFORMAT_ARGB8888 ||
renderer->target->format == SDL_PIXELFORMAT_RGB888)) { renderer->target->format == SDL_PIXELFORMAT_RGB888)) {
data->glUniform4f(locColor, r = renderer->b;
renderer->b * inv255f, b = renderer->r;
renderer->g * inv255f, } else {
renderer->r * inv255f, r = renderer->r;
renderer->a * inv255f); b = renderer->b;
} else { }
data->glUniform4f(locColor,
renderer->r * inv255f, program = data->current_program;
renderer->g * inv255f, /* !!! FIXME: it'd be nice to do a single 32-bit compare here. */
renderer->b * inv255f, if ( (program->color_r != r) || (program->color_g != g) || (program->color_b != b) || (program->color_a != a) ) {
renderer->a * inv255f); /* Select the color to draw with */
data->glUniform4f(program->uniform_locations[GLES2_UNIFORM_COLOR], r * inv255f, g * inv255f, b * inv255f, a * inv255f);
program->color_r = r;
program->color_g = g;
program->color_b = b;
program->color_a = a;
} }
return 0; return 0;
} }
@ -1213,8 +1244,8 @@ GLES2_RenderCopy(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *s
SDL_BlendMode blendMode; SDL_BlendMode blendMode;
GLfloat vertices[8]; GLfloat vertices[8];
GLfloat texCoords[8]; GLfloat texCoords[8];
GLuint locTexture; GLES2_ProgramCacheEntry *program;
GLuint locModulation; Uint8 r, g, b, a;
GLES2_ActivateRenderer(renderer); GLES2_ActivateRenderer(renderer);
@ -1300,31 +1331,37 @@ GLES2_RenderCopy(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect *s
return -1; return -1;
} }
} }
if (GLES2_SelectProgram(renderer, sourceType, blendMode) < 0)
if (GLES2_SelectProgram(renderer, sourceType, blendMode) < 0) {
return -1; return -1;
}
/* Select the target texture */ /* Select the target texture */
locTexture = data->current_program->uniform_locations[GLES2_UNIFORM_TEXTURE];
data->glActiveTexture(GL_TEXTURE0);
data->glBindTexture(tdata->texture_type, tdata->texture); data->glBindTexture(tdata->texture_type, tdata->texture);
data->glUniform1i(locTexture, 0);
/* Configure color modulation */ /* Configure color modulation */
locModulation = data->current_program->uniform_locations[GLES2_UNIFORM_MODULATION]; g = texture->g;
a = texture->a;
if (renderer->target && if (renderer->target &&
(renderer->target->format == SDL_PIXELFORMAT_ARGB8888 || (renderer->target->format == SDL_PIXELFORMAT_ARGB8888 ||
renderer->target->format == SDL_PIXELFORMAT_RGB888)) { renderer->target->format == SDL_PIXELFORMAT_RGB888)) {
data->glUniform4f(locModulation, r = texture->b;
texture->b * inv255f, b = texture->r;
texture->g * inv255f,
texture->r * inv255f,
texture->a * inv255f);
} else { } else {
data->glUniform4f(locModulation, r = texture->r;
texture->r * inv255f, b = texture->b;
texture->g * inv255f, }
texture->b * inv255f,
texture->a * inv255f); program = data->current_program;
/* !!! FIXME: it'd be nice to do a single 32-bit compare here. */
if ( (program->modulation_r != r) || (program->modulation_g != g) || (program->modulation_b != b) || (program->modulation_a != a) ) {
data->glUniform4f(program->uniform_locations[GLES2_UNIFORM_MODULATION], r * inv255f, g * inv255f, b * inv255f, a * inv255f);
program->modulation_r = r;
program->modulation_g = g;
program->modulation_b = b;
program->modulation_a = a;
} }
/* Configure texture blending */ /* Configure texture blending */
@ -1363,11 +1400,11 @@ GLES2_RenderCopyEx(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect
GLES2_DriverContext *data = (GLES2_DriverContext *)renderer->driverdata; GLES2_DriverContext *data = (GLES2_DriverContext *)renderer->driverdata;
GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata; GLES2_TextureData *tdata = (GLES2_TextureData *)texture->driverdata;
GLES2_ImageSource sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR; GLES2_ImageSource sourceType = GLES2_IMAGESOURCE_TEXTURE_ABGR;
GLES2_ProgramCacheEntry *program;
Uint8 r, g, b, a;
SDL_BlendMode blendMode; SDL_BlendMode blendMode;
GLfloat vertices[8]; GLfloat vertices[8];
GLfloat texCoords[8]; GLfloat texCoords[8];
GLuint locTexture;
GLuint locModulation;
GLfloat translate[8]; GLfloat translate[8];
GLfloat fAngle[4]; GLfloat fAngle[4];
GLfloat tmp; GLfloat tmp;
@ -1467,27 +1504,32 @@ GLES2_RenderCopyEx(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_Rect
return -1; return -1;
/* Select the target texture */ /* Select the target texture */
locTexture = data->current_program->uniform_locations[GLES2_UNIFORM_TEXTURE];
data->glActiveTexture(GL_TEXTURE0);
data->glBindTexture(tdata->texture_type, tdata->texture); data->glBindTexture(tdata->texture_type, tdata->texture);
data->glUniform1i(locTexture, 0);
/* Configure color modulation */ /* Configure color modulation */
locModulation = data->current_program->uniform_locations[GLES2_UNIFORM_MODULATION]; /* !!! FIXME: grep for glUniform4f(), move that stuff to a subroutine, it's a lot of copy/paste. */
g = texture->g;
a = texture->a;
if (renderer->target && if (renderer->target &&
(renderer->target->format == SDL_PIXELFORMAT_ARGB8888 || (renderer->target->format == SDL_PIXELFORMAT_ARGB8888 ||
renderer->target->format == SDL_PIXELFORMAT_RGB888)) { renderer->target->format == SDL_PIXELFORMAT_RGB888)) {
data->glUniform4f(locModulation, r = texture->b;
texture->b * inv255f, b = texture->r;
texture->g * inv255f,
texture->r * inv255f,
texture->a * inv255f);
} else { } else {
data->glUniform4f(locModulation, r = texture->r;
texture->r * inv255f, b = texture->b;
texture->g * inv255f, }
texture->b * inv255f,
texture->a * inv255f); program = data->current_program;
/* !!! FIXME: it'd be nice to do a single 32-bit compare here. */
if ( (program->modulation_r != r) || (program->modulation_g != g) || (program->modulation_b != b) || (program->modulation_a != a) ) {
data->glUniform4f(program->uniform_locations[GLES2_UNIFORM_MODULATION], r * inv255f, g * inv255f, b * inv255f, a * inv255f);
program->modulation_r = r;
program->modulation_g = g;
program->modulation_b = b;
program->modulation_a = a;
} }
/* Configure texture blending */ /* Configure texture blending */
@ -1557,8 +1599,6 @@ GLES2_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
SDL_GetRendererOutputSize(renderer, &w, &h); SDL_GetRendererOutputSize(renderer, &w, &h);
data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
data->glReadPixels(rect->x, (h-rect->y)-rect->h, rect->w, rect->h, data->glReadPixels(rect->x, (h-rect->y)-rect->h, rect->w, rect->h,
GL_RGBA, GL_UNSIGNED_BYTE, temp_pixels); GL_RGBA, GL_UNSIGNED_BYTE, temp_pixels);
if (GL_CheckError("glReadPixels()", renderer) < 0) { if (GL_CheckError("glReadPixels()", renderer) < 0) {
@ -1650,6 +1690,15 @@ GLES2_ResetState(SDL_Renderer *renderer)
data->current.blendMode = -1; data->current.blendMode = -1;
data->current.tex_coords = SDL_FALSE; data->current.tex_coords = SDL_FALSE;
data->glActiveTexture(GL_TEXTURE0);
data->glPixelStorei(GL_PACK_ALIGNMENT, 1);
data->glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
data->glClearColor((GLfloat) data->clear_r * inv255f,
(GLfloat) data->clear_g * inv255f,
(GLfloat) data->clear_b * inv255f,
(GLfloat) data->clear_a * inv255f);
data->glEnableVertexAttribArray(GLES2_ATTRIBUTE_POSITION); data->glEnableVertexAttribArray(GLES2_ATTRIBUTE_POSITION);
data->glDisableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD); data->glDisableVertexAttribArray(GLES2_ATTRIBUTE_TEXCOORD);

View File

@ -121,6 +121,8 @@
if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) { if (glCheckFramebufferStatusOES(GL_FRAMEBUFFER_OES) != GL_FRAMEBUFFER_COMPLETE_OES) {
return NO; return NO;
} }
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
/* end create buffers */ /* end create buffers */
self.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight); self.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
@ -148,6 +150,8 @@
glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer); glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
glRenderbufferStorageOES(GL_RENDERBUFFER_OES, depthBufferFormat, backingWidth, backingHeight); glRenderbufferStorageOES(GL_RENDERBUFFER_OES, depthBufferFormat, backingWidth, backingHeight);
} }
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer);
} }
- (void)setAnimationCallback:(int)interval - (void)setAnimationCallback:(int)interval
@ -197,7 +201,9 @@
- (void)swapBuffers - (void)swapBuffers
{ {
glBindRenderbufferOES(GL_RENDERBUFFER_OES, viewRenderbuffer); /* viewRenderbuffer should always be bound here. Code that binds something
else is responsible for rebinding viewRenderbuffer, to reduce
duplicate state changes. */
[context presentRenderbuffer:GL_RENDERBUFFER_OES]; [context presentRenderbuffer:GL_RENDERBUFFER_OES];
} }