compositor: Refactor shader setup a bit
This commit is contained in:
+41
-36
@@ -608,8 +608,10 @@ wlsc_output_repaint(struct wlsc_output *output)
|
|||||||
|
|
||||||
glViewport(0, 0, output->width, output->height);
|
glViewport(0, 0, output->width, output->height);
|
||||||
|
|
||||||
glUniformMatrix4fv(ec->proj_uniform, 1, GL_FALSE, output->matrix.d);
|
glUseProgram(ec->texture_shader.program);
|
||||||
glUniform1i(ec->tex_uniform, 0);
|
glUniformMatrix4fv(ec->texture_shader.proj_uniform,
|
||||||
|
1, GL_FALSE, output->matrix.d);
|
||||||
|
glUniform1i(ec->texture_shader.tex_uniform, 0);
|
||||||
|
|
||||||
pixman_region32_init(&new_damage);
|
pixman_region32_init(&new_damage);
|
||||||
pixman_region32_init(&total_damage);
|
pixman_region32_init(&total_damage);
|
||||||
@@ -1508,7 +1510,7 @@ static const char vertex_shader[] =
|
|||||||
" v_texcoord = texcoord;\n"
|
" v_texcoord = texcoord;\n"
|
||||||
"}\n";
|
"}\n";
|
||||||
|
|
||||||
static const char fragment_shader[] =
|
static const char texture_fragment_shader[] =
|
||||||
"precision mediump float;\n"
|
"precision mediump float;\n"
|
||||||
"varying vec2 v_texcoord;\n"
|
"varying vec2 v_texcoord;\n"
|
||||||
"uniform sampler2D tex;\n"
|
"uniform sampler2D tex;\n"
|
||||||
@@ -1518,52 +1520,53 @@ static const char fragment_shader[] =
|
|||||||
"}\n";
|
"}\n";
|
||||||
|
|
||||||
static int
|
static int
|
||||||
init_shaders(struct wlsc_compositor *ec)
|
compile_shader(GLenum type, const char *source)
|
||||||
{
|
{
|
||||||
GLuint v, f, program;
|
GLuint s;
|
||||||
const char *p;
|
|
||||||
char msg[512];
|
char msg[512];
|
||||||
GLint status;
|
GLint status;
|
||||||
|
|
||||||
p = vertex_shader;
|
s = glCreateShader(type);
|
||||||
v = glCreateShader(GL_VERTEX_SHADER);
|
glShaderSource(s, 1, &source, NULL);
|
||||||
glShaderSource(v, 1, &p, NULL);
|
glCompileShader(s);
|
||||||
glCompileShader(v);
|
glGetShaderiv(s, GL_COMPILE_STATUS, &status);
|
||||||
glGetShaderiv(v, GL_COMPILE_STATUS, &status);
|
|
||||||
if (!status) {
|
if (!status) {
|
||||||
glGetShaderInfoLog(v, sizeof msg, NULL, msg);
|
glGetShaderInfoLog(s, sizeof msg, NULL, msg);
|
||||||
fprintf(stderr, "vertex shader info: %s\n", msg);
|
fprintf(stderr, "shader info: %s\n", msg);
|
||||||
return -1;
|
return GL_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
p = fragment_shader;
|
return s;
|
||||||
f = glCreateShader(GL_FRAGMENT_SHADER);
|
}
|
||||||
glShaderSource(f, 1, &p, NULL);
|
|
||||||
glCompileShader(f);
|
|
||||||
glGetShaderiv(f, GL_COMPILE_STATUS, &status);
|
|
||||||
if (!status) {
|
|
||||||
glGetShaderInfoLog(f, sizeof msg, NULL, msg);
|
|
||||||
fprintf(stderr, "fragment shader info: %s\n", msg);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
program = glCreateProgram();
|
static int
|
||||||
glAttachShader(program, v);
|
wlsc_shader_init(struct wlsc_shader *shader,
|
||||||
glAttachShader(program, f);
|
const char *vertex_source, const char *fragment_source)
|
||||||
glBindAttribLocation(program, 0, "position");
|
{
|
||||||
glBindAttribLocation(program, 1, "texcoord");
|
char msg[512];
|
||||||
|
GLint status;
|
||||||
|
|
||||||
glLinkProgram(program);
|
shader->vertex_shader =
|
||||||
glGetProgramiv(program, GL_LINK_STATUS, &status);
|
compile_shader(GL_VERTEX_SHADER, vertex_source);
|
||||||
|
shader->fragment_shader =
|
||||||
|
compile_shader(GL_FRAGMENT_SHADER, fragment_source);
|
||||||
|
|
||||||
|
shader->program = glCreateProgram();
|
||||||
|
glAttachShader(shader->program, shader->vertex_shader);
|
||||||
|
glAttachShader(shader->program, shader->fragment_shader);
|
||||||
|
glBindAttribLocation(shader->program, 0, "position");
|
||||||
|
glBindAttribLocation(shader->program, 1, "texcoord");
|
||||||
|
|
||||||
|
glLinkProgram(shader->program);
|
||||||
|
glGetProgramiv(shader->program, GL_LINK_STATUS, &status);
|
||||||
if (!status) {
|
if (!status) {
|
||||||
glGetProgramInfoLog(program, sizeof msg, NULL, msg);
|
glGetProgramInfoLog(shader->program, sizeof msg, NULL, msg);
|
||||||
fprintf(stderr, "link info: %s\n", msg);
|
fprintf(stderr, "link info: %s\n", msg);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
glUseProgram(program);
|
shader->proj_uniform = glGetUniformLocation(shader->program, "proj");
|
||||||
ec->proj_uniform = glGetUniformLocation(program, "proj");
|
shader->tex_uniform = glGetUniformLocation(shader->program, "tex");
|
||||||
ec->tex_uniform = glGetUniformLocation(program, "tex");
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -1720,7 +1723,9 @@ wlsc_compositor_init(struct wlsc_compositor *ec, struct wl_display *display)
|
|||||||
}
|
}
|
||||||
|
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
if (init_shaders(ec) < 0)
|
|
||||||
|
if (wlsc_shader_init(&ec->texture_shader,
|
||||||
|
vertex_shader, texture_fragment_shader) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
loop = wl_display_get_event_loop(ec->wl_display);
|
loop = wl_display_get_event_loop(ec->wl_display);
|
||||||
|
|||||||
@@ -90,6 +90,14 @@ struct wlsc_sprite {
|
|||||||
int height;
|
int height;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct wlsc_shader {
|
||||||
|
GLuint program;
|
||||||
|
GLuint vertex_shader, fragment_shader;
|
||||||
|
GLuint proj_uniform;
|
||||||
|
GLuint tex_uniform;
|
||||||
|
GLuint color_uniform;
|
||||||
|
};
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
WLSC_COMPOSITOR_ACTIVE,
|
WLSC_COMPOSITOR_ACTIVE,
|
||||||
WLSC_COMPOSITOR_SLEEPING
|
WLSC_COMPOSITOR_SLEEPING
|
||||||
@@ -106,6 +114,8 @@ struct wlsc_compositor {
|
|||||||
GLuint fbo;
|
GLuint fbo;
|
||||||
GLuint proj_uniform, tex_uniform;
|
GLuint proj_uniform, tex_uniform;
|
||||||
struct wlsc_sprite **pointer_sprites;
|
struct wlsc_sprite **pointer_sprites;
|
||||||
|
struct wlsc_shader texture_shader;
|
||||||
|
struct wlsc_shader solid_shader;
|
||||||
struct wl_display *wl_display;
|
struct wl_display *wl_display;
|
||||||
|
|
||||||
/* We implement the shell interface. */
|
/* We implement the shell interface. */
|
||||||
|
|||||||
Reference in New Issue
Block a user