X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=effect_chain.cpp;h=5c1ba69e64a71ad65fe34ad3d7c53d76f4471fd9;hb=db1fbb73b18b68850f61d1c8b89f2e4d3f7aadc1;hp=85832db88541cfb90748dd55f8aca171a6621673;hpb=fdf859f872d87d0e2ae27e3ff34a8751145c2ae9;p=movit diff --git a/effect_chain.cpp b/effect_chain.cpp index 85832db..5c1ba69 100644 --- a/effect_chain.cpp +++ b/effect_chain.cpp @@ -165,7 +165,7 @@ void EffectChain::finalize() frag_shader += "\n"; frag_shader += std::string("#define FUNCNAME ") + effect_id + "\n"; frag_shader += replace_prefix(effects[i]->output_convenience_uniforms(), effect_id); - frag_shader += replace_prefix(effects[i]->output_glsl(), effect_id); + frag_shader += replace_prefix(effects[i]->output_fragment_shader(), effect_id); frag_shader += "#undef PREFIX\n"; frag_shader += "#undef FUNCNAME\n"; frag_shader += "#undef LAST_INPUT\n"; @@ -185,6 +185,46 @@ void EffectChain::finalize() glLinkProgram(glsl_program_num); check_error(); + // Translate the format to OpenGL's enums. + GLenum internal_format; + if (use_srgb_texture_format) { + internal_format = GL_SRGB8; + } else { + internal_format = GL_RGBA8; + } + if (input_format.pixel_format == FORMAT_RGB) { + format = GL_RGB; + bytes_per_pixel = 3; + } else if (input_format.pixel_format == FORMAT_RGBA) { + format = GL_RGBA; + bytes_per_pixel = 4; + } else if (input_format.pixel_format == FORMAT_BGR) { + format = GL_BGR; + bytes_per_pixel = 3; + } else if (input_format.pixel_format == FORMAT_BGRA) { + format = GL_BGRA; + bytes_per_pixel = 4; + } else { + assert(false); + } + + // Create PBO to hold the texture, and then the texture itself. + glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 2); + check_error(); + glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, width * height * bytes_per_pixel, NULL, GL_STREAM_DRAW); + check_error(); + + void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY); + memset(mapped_pbo, 0, width * height * bytes_per_pixel); + glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB); + + glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE); + check_error(); + glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0)); + check_error(); + glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0); + check_error(); + finalized = true; } @@ -192,28 +232,27 @@ void EffectChain::render_to_screen(unsigned char *src) { assert(finalized); + // Copy the pixel data into the PBO. + glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 2); check_error(); - glUseProgram(glsl_program_num); + void *mapped_pbo = glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY); + memcpy(mapped_pbo, src, width * height * bytes_per_pixel); + glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB); check_error(); + // Re-upload the texture from the PBO. glActiveTexture(GL_TEXTURE0); + check_error(); glBindTexture(GL_TEXTURE_2D, SOURCE_IMAGE); + check_error(); + glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, format, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0)); + check_error(); + glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0); + check_error(); - GLenum format, internal_format; - if (use_srgb_texture_format) { - internal_format = GL_SRGB8; - } else { - internal_format = GL_RGBA8; - } - if (input_format.pixel_format == FORMAT_RGB) { - format = GL_RGB; - } else if (input_format.pixel_format == FORMAT_RGBA) { - format = GL_RGBA; - } else { - assert(false); - } + glUseProgram(glsl_program_num); + check_error(); - glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, src); check_error(); glUniform1i(glGetUniformLocation(glsl_program_num, "input_tex"), 0);