render: Load shaders from files

shader-damage
Andri Yngvason 2019-10-27 22:22:43 +00:00
parent 3d8bb78920
commit 527d0caea7
5 changed files with 51 additions and 43 deletions

View File

@ -0,0 +1,12 @@
#extension GL_OES_EGL_image_external: require
precision mediump float;
uniform samplerExternalOES u_tex;
varying vec2 v_texture;
void main()
{
gl_FragColor = texture2D(u_tex, v_texture);
}

View File

@ -0,0 +1,10 @@
attribute vec2 pos;
attribute vec2 texture;
varying vec2 v_texture;
void main()
{
v_texture = vec2(texture.s, 1.0 - texture.t);
gl_Position = vec4(pos, 0, 1);
}

View File

@ -0,0 +1,10 @@
precision mediump float;
uniform sampler2D u_tex;
varying vec2 v_texture;
void main()
{
gl_FragColor = texture2D(u_tex, v_texture);
}

View File

@ -0,0 +1,10 @@
attribute vec2 pos;
attribute vec2 texture;
varying vec2 v_texture;
void main()
{
v_texture = texture;
gl_Position = vec4(pos, 0, 1);
}

View File

@ -158,41 +158,6 @@ static int gl_load_shader(GLuint* dst, const char* source, GLenum type)
return 0; return 0;
} }
static const char dmabuf_vertex_src[] =
"attribute vec2 pos;\n"
"attribute vec2 texture;\n"
"varying vec2 v_texture;\n"
"void main() {\n"
" v_texture = vec2(texture.s, 1.0 - texture.t);\n"
" gl_Position = vec4(pos, 0, 1);\n"
"}\n";
static const char dmabuf_fragment_src[] =
"#extension GL_OES_EGL_image_external: require\n\n"
"precision mediump float;\n"
"uniform samplerExternalOES u_tex;\n"
"varying vec2 v_texture;\n"
"void main() {\n"
" gl_FragColor = texture2D(u_tex, v_texture);\n"
"}\n";
static const char texture_vertex_src[] =
"attribute vec2 pos;\n"
"attribute vec2 texture;\n"
"varying vec2 v_texture;\n"
"void main() {\n"
" v_texture = texture;\n"
" gl_Position = vec4(pos, 0, 1);\n"
"}\n";
static const char texture_fragment_src[] =
"precision mediump float;\n"
"uniform sampler2D u_tex;\n"
"varying vec2 v_texture;\n"
"void main() {\n"
" gl_FragColor = texture2D(u_tex, v_texture);\n"
"}\n";
static char* read_file(const char* path) static char* read_file(const char* path)
{ {
FILE* stream = fopen(path, "r"); FILE* stream = fopen(path, "r");
@ -242,16 +207,17 @@ static int gl_load_shader_from_file(GLuint* dst, const char* path, GLenum type)
return rc; return rc;
} }
static int gl_compile_shader_program(GLuint* dst, const char* vertex_src, static int gl_compile_shader_program(GLuint* dst, const char* vertex_path,
const char* fragment_src) const char* fragment_path)
{ {
int rc = -1; int rc = -1;
GLuint vertex, fragment; GLuint vertex, fragment;
if (gl_load_shader(&vertex, vertex_src, GL_VERTEX_SHADER) < 0) if (gl_load_shader_from_file(&vertex, vertex_path, GL_VERTEX_SHADER) < 0)
return -1; return -1;
if (gl_load_shader(&fragment, fragment_src, GL_FRAGMENT_SHADER) < 0) if (gl_load_shader_from_file(&fragment, fragment_path,
GL_FRAGMENT_SHADER) < 0)
goto fragment_failure; goto fragment_failure;
GLuint program = glCreateProgram(); GLuint program = glCreateProgram();
@ -399,13 +365,13 @@ int renderer_init(struct renderer* self, uint32_t width, uint32_t height)
goto late_extension_failure; goto late_extension_failure;
if (gl_compile_shader_program(&self->dmabuf_shader_program, if (gl_compile_shader_program(&self->dmabuf_shader_program,
dmabuf_vertex_src, "shaders/dmabuf-vertex.glsl",
dmabuf_fragment_src) < 0) "shaders/dmabuf-fragment.glsl") < 0)
goto shader_failure; goto shader_failure;
if (gl_compile_shader_program(&self->texture_shader_program, if (gl_compile_shader_program(&self->texture_shader_program,
texture_vertex_src, "shaders/texture-vertex.glsl",
texture_fragment_src) < 0) "shaders/texture-fragment.glsl") < 0)
goto shader_failure; goto shader_failure;
self->width = width; self->width = width;