#include <map>
#include <string>
#include <utility>
+#include <epoxy/gl.h>
-#include "glew.h"
#include "init.h"
#include "resource_pool.h"
#include "util.h"
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;
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();
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.
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;
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));
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;