]> git.sesse.net Git - movit/blobdiff - resource_pool.cpp
Add proper formats for sRGB without alpha.
[movit] / resource_pool.cpp
index b6f6a3145455e69de69ab43559c21ef15436ae0a..2298c3c7342d5ee7b264c17800aedb311319e1a1 100644 (file)
@@ -6,8 +6,8 @@
 #include <map>
 #include <string>
 #include <utility>
+#include <epoxy/gl.h>
 
-#include "glew.h"
 #include "init.h"
 #include "resource_pool.h"
 #include "util.h"
@@ -200,10 +200,19 @@ GLuint ResourcePool::create_2d_texture(GLint internal_format, GLsizei width, GLs
        case GL_SRGB8_ALPHA8:
                format = GL_RGBA;
                break;
+       case GL_RGB32F:
+       case GL_RGB16F:
+       case GL_RGB8:
+       case GL_SRGB8:
+               format = GL_RGB;
+               break;
        case GL_RG32F:
        case GL_RG16F:
+       case GL_RG8:
                format = GL_RG;
                break;
+       case GL_R32F:
+       case GL_R16F:
        case GL_R8:
                format = GL_RED;
                break;
@@ -212,12 +221,39 @@ GLuint ResourcePool::create_2d_texture(GLint internal_format, GLsizei width, GLs
                assert(false);
        }
 
+       // Same with type; GLES is stricter than desktop OpenGL here.
+       GLenum type;
+       switch (internal_format) {
+       case GL_RGBA32F_ARB:
+       case GL_RGBA16F_ARB:
+       case GL_RGB32F:
+       case GL_RGB16F:
+       case GL_RG32F:
+       case GL_RG16F:
+       case GL_R32F:
+       case GL_R16F:
+               type = GL_FLOAT;
+               break;
+       case GL_SRGB8_ALPHA8:
+       case GL_SRGB8:
+       case GL_RGBA8:
+       case GL_RGB8:
+       case GL_RG8:
+       case GL_R8:
+               type = GL_UNSIGNED_BYTE;
+               break;
+       default:
+               // TODO: Add more here as needed.
+               assert(false);
+       }
+
+
        GLuint texture_num;
        glGenTextures(1, &texture_num);
        check_error();
        glBindTexture(GL_TEXTURE_2D, texture_num);
        check_error();
-       glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);
+       glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, type, NULL);
        check_error();
        glBindTexture(GL_TEXTURE_2D, 0);
        check_error();
@@ -248,11 +284,25 @@ void ResourcePool::release_2d_texture(GLuint texture_num)
                texture_formats.erase(free_texture_num);
                glDeleteTextures(1, &free_texture_num);
                check_error();
+
+               // Delete any FBO related to this texture.
+               for (list<GLuint>::iterator fbo_freelist_it = fbo_freelist.begin();
+                    fbo_freelist_it != fbo_freelist.end(); ) {
+                       GLuint fbo_num = *fbo_freelist_it;
+                       map<GLuint, FBO>::const_iterator format_it = fbo_formats.find(fbo_num);
+                       assert(format_it != fbo_formats.end());
+                       if (format_it->second.texture_num == free_texture_num) {
+                               glDeleteFramebuffers(1, &fbo_num);
+                               fbo_freelist.erase(fbo_freelist_it++);
+                       } else {
+                               ++fbo_freelist_it;
+                       }
+               }
        }
        pthread_mutex_unlock(&lock);
 }
 
-GLuint ResourcePool::create_fbo(void *context, GLint internal_format, GLsizei width, GLsizei height)
+GLuint ResourcePool::create_fbo(void *context, GLuint texture_num)
 {
        pthread_mutex_lock(&lock);
        // See if there's an FBO on the freelist we can use.
@@ -263,9 +313,7 @@ GLuint ResourcePool::create_fbo(void *context, GLint internal_format, GLsizei wi
                map<GLuint, FBO>::const_iterator format_it = fbo_formats.find(fbo_num);
                assert(format_it != fbo_formats.end());
                if (format_it->second.context == context &&
-                   format_it->second.internal_format == internal_format &&
-                   format_it->second.width == width &&
-                   format_it->second.height == height) {
+                   format_it->second.texture_num == texture_num) {
                        fbo_freelist.erase(freelist_it);
                        pthread_mutex_unlock(&lock);
                        return fbo_num;
@@ -276,12 +324,23 @@ GLuint ResourcePool::create_fbo(void *context, GLint internal_format, GLsizei wi
        GLuint fbo_num;
        glGenFramebuffers(1, &fbo_num);
        check_error();
+       glBindFramebuffer(GL_FRAMEBUFFER, fbo_num);
+       check_error();
+       glFramebufferTexture2D(
+               GL_FRAMEBUFFER,
+               GL_COLOR_ATTACHMENT0,
+               GL_TEXTURE_2D,
+               texture_num,
+               0);
+       check_error();
+       GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
+       assert(status == GL_FRAMEBUFFER_COMPLETE);
+       glBindFramebuffer(GL_FRAMEBUFFER, 0);
+       check_error();
 
        FBO fbo_format;
        fbo_format.context = context;
-       fbo_format.internal_format = internal_format;
-       fbo_format.width = width;
-       fbo_format.height = height;
+       fbo_format.texture_num = texture_num;
        assert(fbo_formats.count(fbo_num) == 0);
        fbo_formats.insert(make_pair(fbo_num, fbo_format));
 
@@ -317,16 +376,32 @@ size_t ResourcePool::estimate_texture_size(const Texture2D &texture_format)
        case GL_RGBA16F_ARB:
                bytes_per_pixel = 8;
                break;
+       case GL_RGB32F_ARB:
+               bytes_per_pixel = 12;
+               break;
+       case GL_RGB16F_ARB:
+               bytes_per_pixel = 6;
+               break;
        case GL_RGBA8:
        case GL_SRGB8_ALPHA8:
                bytes_per_pixel = 4;
                break;
+       case GL_RGB8:
+       case GL_SRGB8:
+               bytes_per_pixel = 3;
+               break;
        case GL_RG32F:
                bytes_per_pixel = 8;
                break;
        case GL_RG16F:
                bytes_per_pixel = 4;
                break;
+       case GL_R32F:
+               bytes_per_pixel = 4;
+               break;
+       case GL_R16F:
+               bytes_per_pixel = 2;
+               break;
        case GL_R8:
                bytes_per_pixel = 1;
                break;