]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/texture.cpp
Load textures in a type-generic way
[libs/gl.git] / source / core / texture.cpp
index c27e850676b83153de81ef09b4d76282e481e091..54577aacd55e06f389361f0eda96305b42a63057 100644 (file)
@@ -1,71 +1,26 @@
-#include <msp/gl/extensions/arb_direct_state_access.h>
-#include <msp/gl/extensions/arb_texture_swizzle.h>
-#include <msp/gl/extensions/ext_framebuffer_object.h>
-#include <msp/gl/extensions/khr_debug.h>
 #include <msp/io/memory.h>
 #include "error.h"
 #include "resourcemanager.h"
 #include "texture.h"
+#include "texture1d.h"
+#include "texture2d.h"
+#include "texture2darray.h"
+#include "texture3d.h"
+#include "texturecube.h"
 
 using namespace std;
 
 namespace Msp {
 namespace GL {
 
-const int Texture::swizzle_orders[] =
-{
-       GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA,
-       GL_RED, GL_RED, GL_RED, GL_ONE,
-       GL_RED, GL_RED, GL_RED, GL_GREEN,
-       GL_BLUE, GL_GREEN, GL_RED, GL_ALPHA
-};
-
-Texture *Texture::scratch_binding = 0;
-
-Texture::Texture(GLenum t, ResourceManager *m):
-       id(0),
-       target(t),
+Texture::Texture(unsigned t):
+       TextureBackend(t),
        format(NO_PIXELFORMAT),
        storage_fmt(format),
        swizzle(NO_SWIZZLE),
        use_srgb_format(false),
        auto_gen_mipmap(false)
-{
-       if(m)
-               set_manager(m);
-       else
-               generate_id();
-
-       static bool alignment_init = false;
-       if(!alignment_init)
-       {
-               glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
-               alignment_init = true;
-       }
-}
-
-Texture::~Texture()
-{
-       if(this==scratch_binding)
-               unbind_scratch();
-       if(id)
-               glDeleteTextures(1, &id);
-}
-
-void Texture::generate_id()
-{
-       if(id)
-               throw invalid_operation("Texture::generate_id");
-       if(ARB_direct_state_access)
-               glCreateTextures(target, 1, &id);
-       else
-               glGenTextures(1, &id);
-
-#ifdef DEBUG
-       if(!debug_name.empty() && KHR_debug)
-               glObjectLabel(GL_TEXTURE, id, debug_name.size(), debug_name.c_str());
-#endif
-}
+{ }
 
 void Texture::set_format(PixelFormat fmt)
 {
@@ -96,56 +51,13 @@ void Texture::set_format(PixelFormat fmt)
        PixelFormat st_fmt = make_pixelformat(st_comp, get_component_type(fmt), is_srgb(fmt));
        require_pixelformat(st_fmt);
        if(swiz!=NO_SWIZZLE)
-               static Require _req(ARB_texture_swizzle);
+               require_swizzle();
 
        format = fmt;
        storage_fmt = st_fmt;
        swizzle = swiz;
 }
 
-void Texture::apply_swizzle()
-{
-       if(swizzle==NO_SWIZZLE)
-               return;
-
-       if(get_backend_api()==OPENGL_ES)
-       {
-               set_parameter_i(GL_TEXTURE_SWIZZLE_R, swizzle_orders[swizzle*4]);
-               set_parameter_i(GL_TEXTURE_SWIZZLE_G, swizzle_orders[swizzle*4+1]);
-               set_parameter_i(GL_TEXTURE_SWIZZLE_B, swizzle_orders[swizzle*4+2]);
-               set_parameter_i(GL_TEXTURE_SWIZZLE_A, swizzle_orders[swizzle*4+3]);
-       }
-       else
-       {
-               if(ARB_direct_state_access)
-                       glTextureParameteriv(id, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
-               else
-                       glTexParameteriv(target, GL_TEXTURE_SWIZZLE_RGBA, swizzle_orders+swizzle*4);
-       }
-}
-
-void Texture::set_parameter_i(unsigned param, int value) const
-{
-       if(ARB_direct_state_access)
-               glTextureParameteri(id, param, value);
-       else
-               glTexParameteri(target, param, value);
-}
-
-void Texture::generate_mipmap()
-{
-       // glGenerateMipmap is defined here
-       static Require _req(EXT_framebuffer_object);
-
-       if(ARB_direct_state_access)
-               glGenerateTextureMipmap(id);
-       else
-       {
-               bind_scratch();
-               glGenerateMipmap(target);
-       }
-}
-
 void Texture::load_image(const string &fn, unsigned lv)
 {
        Graphics::Image img;
@@ -154,56 +66,27 @@ void Texture::load_image(const string &fn, unsigned lv)
        image(img, lv);
 }
 
-void Texture::set_debug_name(const string &name)
+Texture::GenericLoader::TypeRegistry &Texture::get_texture_registry()
 {
-#ifdef DEBUG
-       debug_name = name;
-       if(id && KHR_debug)
-               glObjectLabel(GL_TEXTURE, id, name.size(), name.c_str());
-#else
-       (void)name;
-#endif
-}
-
-void Texture::bind_scratch()
-{
-       if(!scratch_binding)
-               glActiveTexture(GL_TEXTURE0);
-       if(scratch_binding!=this)
+       static GenericLoader::TypeRegistry registry;
+       static bool initialized = false;
+       if(!initialized)
        {
-               if(scratch_binding && scratch_binding->target!=target)
-                       glBindTexture(scratch_binding->target, 0);
-               glBindTexture(target, id);
-               scratch_binding = this;
+               initialized = true;
+               registry.register_type<Texture1D>("1d");
+               registry.register_type<Texture2D>("2d");
+               registry.register_type<Texture3D>("3d");
+               registry.register_type<Texture2DArray>("2d_array");
+               registry.register_type<TextureCube>("cube");
        }
+       return registry;
 }
 
-void Texture::unbind_scratch()
-{
-       if(scratch_binding)
-       {
-               glBindTexture(scratch_binding->target, 0);
-               scratch_binding = 0;
-       }
-}
-
-
-Texture::Loader::Loader(Texture &t):
-       DataFile::CollectionObjectLoader<Texture>(t, 0)
-{
-       init();
-}
 
-Texture::Loader::Loader(Texture &t, Collection &c):
-       DataFile::CollectionObjectLoader<Texture>(t, &c)
+Texture::Loader::Loader(Texture &t, Collection *c):
+       CollectionObjectLoader<Texture>(t, c),
+       levels(0)
 {
-       init();
-}
-
-void Texture::Loader::init()
-{
-       levels = 0;
-
        add("external_image", &Loader::external_image);
        add("external_image_srgb", &Loader::external_image_srgb);
        add("generate_mipmap", &Loader::generate_mipmap);
@@ -257,11 +140,7 @@ void Texture::Loader::generate_mipmap(bool gm)
 void Texture::Loader::image_data(const string &data)
 {
        if(obj.manager)
-       {
                obj.set_manager(0);
-               if(!obj.id)
-                       obj.generate_id();
-       }
 
        Graphics::Image img;
        IO::Memory mem(data.data(), data.size());