gsk: Don't store the uniform and attribute location twice

We should keep the ShaderBuilder around and use it to query the various
uniform and attribute locations when needed, instead of storing those
offsets into the Renderer instance, and copying them. This allows a bit
more flexibility, once we have more than one program built into the
renderer.
This commit is contained in:
Emmanuele Bassi 2016-07-03 22:54:30 +01:00
parent 12db9ee164
commit 9e3b0f5aa0

View File

@ -13,6 +13,10 @@
#include <epoxy/gl.h> #include <epoxy/gl.h>
#define SHADER_VERSION_GLES 110
#define SHADER_VERSION_GL_LEGACY 120
#define SHADER_VERSION_GL3 150
typedef struct { typedef struct {
guint vao_id; guint vao_id;
guint buffer_id; guint buffer_id;
@ -51,6 +55,21 @@ typedef struct {
RenderData *parent_data; RenderData *parent_data;
} RenderItem; } RenderItem;
enum {
MVP,
MAP,
PARENT_MAP,
ALPHA,
BLEND_MODE,
N_UNIFORMS
};
enum {
POSITION,
UV,
N_ATTRIBUTES
};
struct _GskGLRenderer struct _GskGLRenderer
{ {
GskRenderer parent_instance; GskRenderer parent_instance;
@ -65,14 +84,10 @@ struct _GskGLRenderer
guint depth_stencil_buffer; guint depth_stencil_buffer;
guint texture_id; guint texture_id;
guint program_id; GQuark uniforms[N_UNIFORMS];
guint mvp_location; GQuark attributes[N_ATTRIBUTES];
guint map_location;
guint parentMap_location; GskShaderBuilder *blend_program;
guint uv_location;
guint position_location;
guint alpha_location;
guint blendMode_location;
guint vao_id; guint vao_id;
@ -283,41 +298,21 @@ gsk_gl_renderer_create_programs (GskGLRenderer *self)
const char *vertex_preamble; const char *vertex_preamble;
const char *fragment_preamble; const char *fragment_preamble;
int vertex_id = -1, fragment_id = -1; int vertex_id = -1, fragment_id = -1;
int program_id = -1;
GError *error = NULL; GError *error = NULL;
gboolean res = FALSE; gboolean res = FALSE;
enum {
MVP,
MAP,
PARENT_MAP,
ALPHA,
BLEND_MODE,
N_UNIFORMS
};
enum {
POSITION,
UV,
N_ATTRIBUTES
};
GQuark uniforms[N_UNIFORMS];
GQuark attributes[N_ATTRIBUTES];
builder = gsk_shader_builder_new (); builder = gsk_shader_builder_new ();
gsk_shader_builder_set_resource_base_path (builder, "/org/gtk/libgsk/glsl"); gsk_shader_builder_set_resource_base_path (builder, "/org/gtk/libgsk/glsl");
uniforms[MVP] = gsk_shader_builder_add_uniform (builder, "mvp"); self->uniforms[MVP] = gsk_shader_builder_add_uniform (builder, "mvp");
uniforms[MAP] = gsk_shader_builder_add_uniform (builder, "map"); self->uniforms[MAP] = gsk_shader_builder_add_uniform (builder, "map");
uniforms[PARENT_MAP] = gsk_shader_builder_add_uniform (builder, "parentMap"); self->uniforms[PARENT_MAP] = gsk_shader_builder_add_uniform (builder, "parentMap");
uniforms[ALPHA] = gsk_shader_builder_add_uniform (builder, "alpha"); self->uniforms[ALPHA] = gsk_shader_builder_add_uniform (builder, "alpha");
uniforms[BLEND_MODE] = gsk_shader_builder_add_uniform (builder, "blendMode"); self->uniforms[BLEND_MODE] = gsk_shader_builder_add_uniform (builder, "blendMode");
attributes[POSITION] = gsk_shader_builder_add_attribute (builder, "position"); self->attributes[POSITION] = gsk_shader_builder_add_attribute (builder, "position");
attributes[UV] = gsk_shader_builder_add_attribute (builder, "uv"); self->attributes[UV] = gsk_shader_builder_add_attribute (builder, "uv");
#define SHADER_VERSION_GLES 110
#define SHADER_VERSION_GL_LEGACY 120
#define SHADER_VERSION_GL3 150
if (gdk_gl_context_get_use_es (self->context)) if (gdk_gl_context_get_use_es (self->context))
{ {
@ -371,9 +366,7 @@ gsk_gl_renderer_create_programs (GskGLRenderer *self)
goto out; goto out;
} }
program_id = gsk_shader_builder_create_program (builder, gsk_shader_builder_create_program (builder, vertex_id, fragment_id, &error);
vertex_id, fragment_id,
&error);
if (error != NULL) if (error != NULL)
{ {
g_critical ("Unable to create program: %s", error->message); g_critical ("Unable to create program: %s", error->message);
@ -381,35 +374,31 @@ gsk_gl_renderer_create_programs (GskGLRenderer *self)
goto out; goto out;
} }
self->program_id = program_id; self->blend_program = g_object_ref (builder);
self->mvp_location = gsk_shader_builder_get_uniform_location (builder, uniforms[MVP]);
self->map_location = gsk_shader_builder_get_uniform_location (builder, uniforms[MAP]);
self->parentMap_location = gsk_shader_builder_get_uniform_location (builder, uniforms[PARENT_MAP]);
self->alpha_location = gsk_shader_builder_get_uniform_location (builder, uniforms[ALPHA]);
self->blendMode_location = gsk_shader_builder_get_uniform_location (builder, uniforms[BLEND_MODE]);
self->position_location = gsk_shader_builder_get_attribute_location (builder, attributes[POSITION]);
self->uv_location = gsk_shader_builder_get_attribute_location (builder, attributes[UV]);
res = TRUE; res = TRUE;
out: out:
g_object_unref (builder);
if (vertex_id > 0) if (vertex_id > 0)
glDeleteShader (vertex_id); glDeleteShader (vertex_id);
if (fragment_id > 0) if (fragment_id > 0)
glDeleteShader (fragment_id); glDeleteShader (fragment_id);
g_object_unref (builder);
return res; return res;
} }
static void static void
gsk_gl_renderer_destroy_programs (GskGLRenderer *self) gsk_gl_renderer_destroy_programs (GskGLRenderer *self)
{ {
if (self->program_id != 0) if (self->blend_program != NULL)
{ {
glDeleteProgram (self->program_id); int program_id = gsk_shader_builder_get_program (self->blend_program);
self->program_id = 0;
glDeleteProgram (program_id);
g_clear_object (&self->blend_program);
} }
} }
@ -777,14 +766,24 @@ gsk_gl_renderer_add_render_item (GskGLRenderer *self,
/* GL objects */ /* GL objects */
item.render_data.vao_id = self->vao_id; item.render_data.vao_id = self->vao_id;
item.render_data.buffer_id = 0; item.render_data.buffer_id = 0;
item.render_data.program_id = self->program_id;
item.render_data.map_location = self->map_location; item.render_data.program_id = gsk_shader_builder_get_program (self->blend_program);
item.render_data.parentMap_location = self->parentMap_location;
item.render_data.mvp_location = self->mvp_location; item.render_data.map_location =
item.render_data.uv_location = self->uv_location; gsk_shader_builder_get_uniform_location (self->blend_program, self->uniforms[MAP]);
item.render_data.position_location = self->position_location; item.render_data.parentMap_location =
item.render_data.alpha_location = self->alpha_location; gsk_shader_builder_get_uniform_location (self->blend_program, self->uniforms[PARENT_MAP]);
item.render_data.blendMode_location = self->blendMode_location; item.render_data.mvp_location =
gsk_shader_builder_get_uniform_location (self->blend_program, self->uniforms[MVP]);
item.render_data.alpha_location =
gsk_shader_builder_get_uniform_location (self->blend_program, self->uniforms[ALPHA]);
item.render_data.blendMode_location =
gsk_shader_builder_get_uniform_location (self->blend_program, self->uniforms[BLEND_MODE]);
item.render_data.position_location =
gsk_shader_builder_get_attribute_location (self->blend_program, self->attributes[POSITION]);
item.render_data.uv_location =
gsk_shader_builder_get_attribute_location (self->blend_program, self->attributes[UV]);
if (parent != NULL) if (parent != NULL)
item.parent_data = &(parent->render_data); item.parent_data = &(parent->render_data);