From cd446554e998204eaba22504b7b28227feb8edbb Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 30 Nov 2010 13:10:50 +0000 Subject: [PATCH] Remove support for texture borders Implement missing Texture3D::sub_image Add allocate methods to Texture2D and Texture3D Fix a bug in Texture --- maketex.py | 2 +- mesh_export.py | 2 +- source/bloom.cpp | 2 +- source/pipeline.cpp | 2 +- source/pixelformat.cpp | 20 +++++++++++++ source/pixelformat.h | 2 ++ source/shadowmap.cpp | 2 +- source/texture.cpp | 2 +- source/texture2d.cpp | 67 ++++++++++++++++++++++++++++++++++-------- source/texture2d.h | 23 +++++++++------ source/texture3d.cpp | 64 +++++++++++++++++++++++++++++++++++----- source/texture3d.h | 12 +++++--- 12 files changed, 160 insertions(+), 40 deletions(-) diff --git a/maketex.py b/maketex.py index 017aa121..fa8e70d8 100755 --- a/maketex.py +++ b/maketex.py @@ -26,7 +26,7 @@ def make_tex(fn): elif fmt=="L": fmt="LUMINANCE" - result="storage %s %d %d 0;\n"%(fmt, img.size[0], img.size[1]) + result="storage %s %d %d;\n"%(fmt, img.size[0], img.size[1]) result+="min_filter LINEAR;\n" result+="raw_data \"" data=list(img.getdata()) diff --git a/mesh_export.py b/mesh_export.py index 9e09ef4d..1e0047dc 100644 --- a/mesh_export.py +++ b/mesh_export.py @@ -671,7 +671,7 @@ class Exporter: self.out_file.begin("texture2d") self.out_file.write("min_filter", "NEAREST") self.out_file.write("mag_filter", "NEAREST") - self.out_file.write("storage", "RGB", len(mesh.materials), 1, 0) + self.out_file.write("storage", "RGB", len(mesh.materials), 1) texdata = '"' for m in mesh.materials: texdata += "\\x%02X\\x%02X\\x%02X"%(int(m.R*255), int(m.G*255), int(m.B*255)) diff --git a/source/bloom.cpp b/source/bloom.cpp index 040fd6c7..7c4338f8 100644 --- a/source/bloom.cpp +++ b/source/bloom.cpp @@ -76,7 +76,7 @@ Bloom::Bloom(unsigned w, unsigned h): { blur_shdata[i].uniform(loc, 0); tex[i].set_min_filter(NEAREST); - tex[i].storage(RGB16F, w, h, 0); + tex[i].storage(RGB16F, w, h); tex[i].image(0, RGB, UNSIGNED_BYTE, 0); } diff --git a/source/pipeline.cpp b/source/pipeline.cpp index f05d15b8..95f96536 100644 --- a/source/pipeline.cpp +++ b/source/pipeline.cpp @@ -119,7 +119,7 @@ void Pipeline::add_postprocessor(PostProcessor &pp) color_buf = new Texture2D; color_buf->set_min_filter(NEAREST); color_buf->set_mag_filter(NEAREST); - color_buf->storage((hdr ? RGB16F : RGB), width, height, 0); + color_buf->storage((hdr ? RGB16F : RGB), width, height); color_buf->image(0, RGB, UNSIGNED_BYTE, 0); fbo->attach(COLOR_ATTACHMENT0, *color_buf, 0); depth_buf = new Renderbuffer; diff --git a/source/pixelformat.cpp b/source/pixelformat.cpp index 27a814e2..91f10226 100644 --- a/source/pixelformat.cpp +++ b/source/pixelformat.cpp @@ -65,5 +65,25 @@ PixelFormat pixelformat_from_graphics(Graphics::PixelFormat pf) } } +PixelFormat get_base_pixelformat(PixelFormat pf) +{ + switch(pf) + { + case RGB8: + case RGB16F: + case RGB32F: return RGB; + case RGBA8: + case RGBA16F: + case RGBA32F: return RGBA; + case LUMINANCE8: + case LUMINANCE16F: + case LUMINANCE32F: return LUMINANCE; + case LUMINANCE_ALPHA8: + case LUMINANCE_ALPHA16F: + case LUMINANCE_ALPHA32F: return LUMINANCE_ALPHA; + default: return pf; + } +} + } // namespace GL } // namespace Msp diff --git a/source/pixelformat.h b/source/pixelformat.h index 6063145a..81662cb9 100644 --- a/source/pixelformat.h +++ b/source/pixelformat.h @@ -49,6 +49,8 @@ std::istream &operator>>(std::istream &, PixelFormat &); PixelFormat pixelformat_from_graphics(Graphics::PixelFormat); +PixelFormat get_base_pixelformat(PixelFormat); + } // namespace GL } // namespace Msp diff --git a/source/shadowmap.cpp b/source/shadowmap.cpp index c8730426..dbc4bd56 100644 --- a/source/shadowmap.cpp +++ b/source/shadowmap.cpp @@ -30,7 +30,7 @@ ShadowMap::ShadowMap(unsigned s, const Scene &c, const Light &l): depth_buf.set_compare_enabled(true); depth_buf.set_compare_func(LEQUAL); depth_buf.set_wrap(CLAMP_TO_EDGE); - depth_buf.storage(DEPTH_COMPONENT, size, size, 0); + depth_buf.storage(DEPTH_COMPONENT, size, size); depth_buf.image(0, DEPTH_COMPONENT, UNSIGNED_BYTE, 0); fbo.attach(DEPTH_ATTACHMENT, depth_buf, 0); } diff --git a/source/texture.cpp b/source/texture.cpp index dc69732f..52ad0742 100644 --- a/source/texture.cpp +++ b/source/texture.cpp @@ -76,7 +76,7 @@ void Texture::update_parameter(int mask) const glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, gen_mipmap); if(mask&COMPARE) glTexParameteri(target, GL_TEXTURE_COMPARE_MODE, (compare ? GL_COMPARE_R_TO_TEXTURE : GL_NONE)); - if(mask&cmp_func) + if(mask&COMPARE_FUNC) glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC, cmp_func); } else diff --git a/source/texture2d.cpp b/source/texture2d.cpp index 8935933f..c92099ce 100644 --- a/source/texture2d.cpp +++ b/source/texture2d.cpp @@ -17,10 +17,11 @@ namespace GL { Texture2D::Texture2D(): Texture(GL_TEXTURE_2D), width(0), - height(0) + height(0), + allocated(0) { } -void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht, int brd) +void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht) { if(width>0) throw InvalidState("Texture storage may only be specified once"); @@ -30,22 +31,39 @@ void Texture2D::storage(PixelFormat fmt, unsigned wd, unsigned ht, int brd) ifmt = fmt; width = wd; height = ht; - border = brd; } -void Texture2D::image(int level, PixelFormat fmt, DataType type, const void *data) +void Texture2D::allocate(unsigned level) { - if(width==0) - throw InvalidState("Texture storage has not been specified"); + if(allocated&(1<>=1, h>>=1, ++level) ; + allocated |= (1<>= level; + h >>= level; + + if(!w && h) + w = 1; + else if(!h && w) + h = 1; +} + Texture2D::Loader::Loader(Texture2D &t): Texture::Loader(t) @@ -79,6 +114,7 @@ Texture2D::Loader::Loader(Texture2D &t): add("image_data", &Loader::image_data); add("raw_data", &Loader::raw_data); add("storage", &Loader::storage); + add("storage", &Loader::storage_b); } void Texture2D::Loader::image_data(const string &data) @@ -95,9 +131,14 @@ void Texture2D::Loader::raw_data(const string &data) t2d.image(0, t2d.ifmt, UNSIGNED_BYTE, data.data()); } -void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h, unsigned b) +void Texture2D::Loader::storage(PixelFormat fmt, unsigned w, unsigned h) +{ + static_cast(obj).storage(fmt, w, h); +} + +void Texture2D::Loader::storage_b(PixelFormat fmt, unsigned w, unsigned h, unsigned) { - static_cast(obj).storage(fmt, w, h, b); + storage(fmt, w, h); } } // namespace GL diff --git a/source/texture2d.h b/source/texture2d.h index 01271852..8508b043 100644 --- a/source/texture2d.h +++ b/source/texture2d.h @@ -30,14 +30,15 @@ public: private: void image_data(const std::string &); void raw_data(const std::string &); - void storage(PixelFormat, unsigned, unsigned, unsigned); + void storage(PixelFormat, unsigned, unsigned); + void storage_b(PixelFormat, unsigned, unsigned, unsigned); }; private: PixelFormat ifmt; unsigned width; unsigned height; - int border; + unsigned allocated; public: Texture2D(); @@ -46,20 +47,22 @@ public: Defines the texture storage. This function may only be successfully called once. */ - void storage(PixelFormat fmt, unsigned wd, unsigned ht, int brd); + void storage(PixelFormat fmt, unsigned wd, unsigned ht); + + /** Allocates texture storage. If storage has already been allocated, this + function does nothing. */ + void allocate(unsigned level); - /** - Uploads an image to the texture. storage() must have been called prior to + /** Uploads an image to the texture. storage() must have been called prior to this, and the image must have dimensions conforming to the specified - storage. - */ - void image(int level, PixelFormat fmt, DataType type, const void *data); + storage. For level>0, mipmapping rules apply to the image dimensions. */ + void image(unsigned level, PixelFormat fmt, DataType type, const void *data); /** Uploads a sub-image into the texture. Unlike full image upload, there are no constraints on the size of the sub-image. */ - void sub_image(int level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data); + void sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data); /** Loads an image from a file and uploads it to the texture. If storage() has @@ -72,6 +75,8 @@ public: private: void image(const Graphics::Image &); + void require_storage(); + void get_level_size(unsigned, unsigned &, unsigned &); }; } // namespace GL diff --git a/source/texture3d.cpp b/source/texture3d.cpp index 7edc9080..6515946b 100644 --- a/source/texture3d.cpp +++ b/source/texture3d.cpp @@ -22,12 +22,13 @@ Texture3D::Texture3D(): Texture(GL_TEXTURE_3D), width(0), height(0), - depth(0) + depth(0), + allocated(0) { static RequireVersion _ver(1, 2); } -void Texture3D::storage(PixelFormat f, unsigned w, unsigned h, unsigned d, int b) +void Texture3D::storage(PixelFormat f, unsigned w, unsigned h, unsigned d) { if(width>0) throw InvalidState("Textures storage may only be specified once"); @@ -38,18 +39,45 @@ void Texture3D::storage(PixelFormat f, unsigned w, unsigned h, unsigned d, int b height = h; depth = d; ifmt = f; - border = b; image(0, ifmt, UNSIGNED_BYTE, 0); } -void Texture3D::image(int level, PixelFormat fmt, DataType type, const void *data) +void Texture3D::allocate(unsigned level) { - if(width==0) - throw InvalidState("Texture storage has not been specified"); + if(allocated&(1<>=1, h>>=1, d>>=1, ++level) ; + allocated |= (1<>= level; + h >>= level; + d >>= level; + + if(!w && (h || d)) + w = 1; + if(!h && (w || d)) + h = 1; + if(!d && (w || h)) + d = 1; +} + } // namespace GL } // namespace Msp diff --git a/source/texture3d.h b/source/texture3d.h index 50e4555a..15d244de 100644 --- a/source/texture3d.h +++ b/source/texture3d.h @@ -23,17 +23,21 @@ private: unsigned width; unsigned height; unsigned depth; - int border; + unsigned allocated; public: Texture3D(); - void storage(PixelFormat, unsigned, unsigned, unsigned, int); - void image(int, PixelFormat, DataType, const void *); - void sub_image(int, int, int, unsigned, unsigned, unsigned, PixelFormat, DataType, const void *); + void storage(PixelFormat, unsigned, unsigned, unsigned); + void allocate(unsigned); + void image(unsigned, PixelFormat, DataType, const void *); + void sub_image(unsigned, int, int, int, unsigned, unsigned, unsigned, PixelFormat, DataType, const void *); void load_image(const std::string &fn, int dp = -1); unsigned get_width() const { return width; } unsigned get_height() const { return height; } unsigned get_depth() const { return depth; } +private: + void require_storage(); + void get_level_size(unsigned, unsigned &, unsigned &, unsigned &); }; } // namespace GL -- 2.45.2