]> git.sesse.net Git - movit/blobdiff - flat_input.cpp
Convert a loop to range-based for.
[movit] / flat_input.cpp
index f6ae827338d7f75c9b76c4c9fe1612dedc75fbfc..6e3db34cfceb1e772566f10f5e3ab82cbe675a68 100644 (file)
@@ -21,13 +21,15 @@ FlatInput::FlatInput(ImageFormat image_format, MovitPixelFormat pixel_format_in,
          width(width),
          height(height),
          pitch(width),
-         pixel_data(NULL),
+         owns_texture(false),
+         pixel_data(nullptr),
          fixup_swap_rb(false),
          fixup_red_to_grayscale(false)
 {
        assert(type == GL_FLOAT || type == GL_HALF_FLOAT || type == GL_UNSIGNED_SHORT || type == GL_UNSIGNED_BYTE);
        register_int("output_linear_gamma", &output_linear_gamma);
        register_int("needs_mipmaps", &needs_mipmaps);
+       register_uniform_sampler2d("tex", &uniform_tex);
 
        // Some types are not supported in all GL versions (e.g. GLES),
        // and will corrected into the right format in the shader.
@@ -56,9 +58,7 @@ FlatInput::FlatInput(ImageFormat image_format, MovitPixelFormat pixel_format_in,
 
 FlatInput::~FlatInput()
 {
-       if (texture_num != 0) {
-               resource_pool->release_2d_texture(texture_num);
-       }
+       possibly_release_texture();
 }
 
 void FlatInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsigned *sampler_num)
@@ -66,7 +66,7 @@ void FlatInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsi
        glActiveTexture(GL_TEXTURE0 + *sampler_num);
        check_error();
 
-       if (texture_num == 0) {
+       if (texture_num == 0 && (pbo != 0 || pixel_data != nullptr)) {
                // Translate the input format to OpenGL's enums.
                GLint internal_format;
                GLenum format;
@@ -162,13 +162,16 @@ void FlatInput::set_gl_state(GLuint glsl_program_num, const string& prefix, unsi
                check_error();
                glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
                check_error();
+               owns_texture = true;
        } else {
                glBindTexture(GL_TEXTURE_2D, texture_num);
                check_error();
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, needs_mipmaps ? GL_LINEAR_MIPMAP_NEAREST : GL_LINEAR);
+               check_error();
        }
 
        // Bind it to a sampler.
-       set_uniform_int(glsl_program_num, prefix, "tex", *sampler_num);
+       uniform_tex = *sampler_num;
        ++*sampler_num;
 }
 
@@ -182,9 +185,15 @@ string FlatInput::output_fragment_shader()
 
 void FlatInput::invalidate_pixel_data()
 {
-       if (texture_num != 0) {
+       possibly_release_texture();
+}
+
+void FlatInput::possibly_release_texture()
+{
+       if (texture_num != 0 && owns_texture) {
                resource_pool->release_2d_texture(texture_num);
                texture_num = 0;
+               owns_texture = false;
        }
 }