renderer: Set up texture bindings on render()

shader-damage-experiments
Andri Yngvason 2020-03-25 21:27:57 +00:00
parent a8fd0d6765
commit 557f0f365b
2 changed files with 21 additions and 4 deletions

View File

@ -48,6 +48,8 @@ struct renderer {
GLuint tex[2]; GLuint tex[2];
int tex_index; int tex_index;
GLenum tex_target;
struct { struct {
GLuint program; GLuint program;
GLint u_tex0; GLint u_tex0;

View File

@ -355,6 +355,13 @@ GLuint renderer_next_tex(struct renderer* self)
return tex; return tex;
} }
GLuint renderer_current_tex(const struct renderer* self)
{
size_t len = ARRAY_LEN(self->tex);
size_t index = (len + self->tex_index - 1) % len;
return self->tex[index];
}
GLuint renderer_last_tex(const struct renderer* self) GLuint renderer_last_tex(const struct renderer* self)
{ {
size_t len = ARRAY_LEN(self->tex); size_t len = ARRAY_LEN(self->tex);
@ -370,6 +377,11 @@ void gl_clear(void)
void render(struct renderer* self) void render(struct renderer* self)
{ {
glActiveTexture(GL_TEXTURE0);
glBindTexture(self->tex_target, renderer_current_tex(self));
glActiveTexture(GL_TEXTURE1);
glBindTexture(self->tex_target, renderer_last_tex(self));
glUseProgram(self->shader.program); glUseProgram(self->shader.program);
glUniform1i(self->shader.u_tex0, 0); glUniform1i(self->shader.u_tex0, 0);
@ -519,12 +531,16 @@ int renderer_init(struct renderer* self, const struct output* output,
switch (input_type) { switch (input_type) {
case RENDERER_INPUT_DMABUF: case RENDERER_INPUT_DMABUF:
self->tex_target = GL_TEXTURE_EXTERNAL_OES;
if (gl_compile_shader_program(&self->shader.program, if (gl_compile_shader_program(&self->shader.program,
"dmabuf-vertex.glsl", "dmabuf-vertex.glsl",
"dmabuf-fragment.glsl") < 0) "dmabuf-fragment.glsl") < 0)
goto shader_failure; goto shader_failure;
break; break;
case RENDERER_INPUT_FB: case RENDERER_INPUT_FB:
self->tex_target = GL_TEXTURE_2D;
if (gl_compile_shader_program(&self->shader.program, if (gl_compile_shader_program(&self->shader.program,
"texture-vertex.glsl", "texture-vertex.glsl",
"texture-fragment.glsl") < 0) "texture-fragment.glsl") < 0)
@ -608,11 +624,11 @@ int renderer_import_dmabuf_frame(struct renderer* self, GLuint tex,
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex); glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex);
glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, image); glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, image);
eglDestroyImageKHR(self->display, image); eglDestroyImageKHR(self->display, image);
glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_EXTERNAL_OES, 0);
glBindTexture(GL_TEXTURE_2D, renderer_last_tex(self));
return 0; return 0;
} }
@ -635,8 +651,7 @@ int renderer_import_framebuffer(struct renderer* self, GLuint tex,
glGenerateMipmap(GL_TEXTURE_2D); glGenerateMipmap(GL_TEXTURE_2D);
glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0); glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, 0);
glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, 0);
glBindTexture(GL_TEXTURE_2D, renderer_last_tex(self));
return 0; return 0;
} }