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())
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))
{
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);
}
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;
}
}
+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
PixelFormat pixelformat_from_graphics(Graphics::PixelFormat);
+PixelFormat get_base_pixelformat(PixelFormat);
+
} // namespace GL
} // namespace Msp
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);
}
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
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");
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<<level))
+ return;
+
+ image(level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
+}
+
+void Texture2D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
+{
+ require_storage();
+
+ unsigned w = width;
+ unsigned h = height;
+ get_level_size(level, w, h);
Bind _bind(this, true);
- glTexImage2D(target, level, ifmt, width, height, border, fmt, type, data);
+ glTexImage2D(target, level, ifmt, w, h, 0, fmt, type, data);
+
+ allocated |= 1<<level;
+ if(gen_mipmap && level==0)
+ {
+ for(; (w || h); w>>=1, h>>=1, ++level) ;
+ allocated |= (1<<level)-1;
+ }
}
-void Texture2D::sub_image(int level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
+void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht, PixelFormat fmt, DataType type, const void *data)
{
- if(width==0)
- throw InvalidState("Texture storage has not been specified");
+ require_storage();
+ allocate(level);
Bind _bind(this, true);
glTexSubImage2D(target, level, x, y, wd, ht, fmt, type, data);
unsigned h = img.get_height();
PixelFormat fmt = pixelformat_from_graphics(img.get_format());
if(width==0)
- storage(fmt, w, h, 0);
+ storage(fmt, w, h);
else if(w!=width || h!=height)
throw IncompatibleData("Image does not match texture storage");
image(0, fmt, UNSIGNED_BYTE, img.get_data());
}
+void Texture2D::require_storage()
+{
+ if(width==0 || height==0)
+ throw InvalidState("Texture storage has not been specified");
+}
+
+void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h)
+{
+ w >>= level;
+ h >>= level;
+
+ if(!w && h)
+ w = 1;
+ else if(!h && w)
+ h = 1;
+}
+
Texture2D::Loader::Loader(Texture2D &t):
Texture::Loader(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)
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<Texture2D &>(obj).storage(fmt, w, h);
+}
+
+void Texture2D::Loader::storage_b(PixelFormat fmt, unsigned w, unsigned h, unsigned)
{
- static_cast<Texture2D &>(obj).storage(fmt, w, h, b);
+ storage(fmt, w, h);
}
} // namespace GL
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();
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
private:
void image(const Graphics::Image &);
+ void require_storage();
+ void get_level_size(unsigned, unsigned &, unsigned &);
};
} // namespace GL
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");
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<<level))
+ return;
+
+ image(level, get_base_pixelformat(ifmt), UNSIGNED_BYTE, 0);
+}
+
+void Texture3D::image(unsigned level, PixelFormat fmt, DataType type, const void *data)
+{
+ require_storage();
+
+ unsigned w = width;
+ unsigned h = height;
+ unsigned d = depth;
+ get_level_size(level, w, h, d);
+
+ Bind _bind(this, true);
+ glTexImage3D(target, level, ifmt, width, height, depth, 0, fmt, type, data);
+
+ allocated |= 1<<level;
+ if(gen_mipmap && level==0)
+ {
+ for(; (w || h || d); w>>=1, h>>=1, d>>=1, ++level) ;
+ allocated |= (1<<level)-1;
+ }
+}
+
+void Texture3D::sub_image(unsigned level, int x, int y, int z, unsigned wd, unsigned ht, unsigned dp, PixelFormat fmt, DataType type, const void *data)
+{
+ require_storage();
+ allocate(level);
Bind _bind(this, true);
- glTexImage3D(target, level, ifmt, width, height, depth, border, fmt, type, data);
+ glTexSubImage3D(target, level, x, y, z, wd, ht, dp, fmt, type, data);
}
void Texture3D::load_image(const string &fn, int dp)
PixelFormat fmt = pixelformat_from_graphics(img.get_format());
if(width==0)
- storage(fmt, w, h, d, 0);
+ storage(fmt, w, h, d);
else if(w!=width || h!=height || d!=depth)
throw IncompatibleData("Image does not match texture storage");
image(0, fmt, UNSIGNED_INT, img.get_data());
}
+void Texture3D::require_storage()
+{
+ if(width==0 || height==0 || depth==0)
+ throw InvalidState("Texture storage has not been specified");
+}
+
+void Texture3D::get_level_size(unsigned level, unsigned &w, unsigned &h, unsigned &d)
+{
+ w >>= 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
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