Merged mage-CompressedData into default

This commit is contained in:
Alex Szpakowski
2013-04-09 23:44:00 -03:00
41 changed files with 2485 additions and 342 deletions
+29 -1
View File
@@ -225,6 +225,9 @@ void Graphics::present()
void Graphics::setIcon(Image *image)
{
if (image->isCompressed())
throw love::Exception("Cannot use compressed image data to set an icon.");
currentWindow->setIcon(image->getData());
}
@@ -331,9 +334,11 @@ int Graphics::getScissor(lua_State *L) const
void Graphics::defineStencil()
{
// Disable color writes but don't save the mask values.
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glEnable(GL_STENCIL_TEST);
glClear(GL_STENCIL_BUFFER_BIT);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
}
@@ -374,6 +379,29 @@ Image *Graphics::newImage(love::image::ImageData *data)
return image;
}
Image *Graphics::newImage(love::image::CompressedData *cdata)
{
// Create the image.
Image *image = new Image(cdata);
bool success;
try
{
success = image->load();
}
catch(love::Exception &)
{
image->release();
throw;
}
if (!success)
{
image->release();
return 0;
}
return image;
}
Quad *Graphics::newQuad(float x, float y, float w, float h, float sw, float sh)
{
Quad::Viewport v;
+1 -1
View File
@@ -251,8 +251,8 @@ public:
/**
* Creates an Image object with padding and/or optimization.
**/
Image *newImage(love::filesystem::File *file);
Image *newImage(love::image::ImageData *data);
Image *newImage(love::image::CompressedData *cdata);
/**
* Creates a Quad object.
+253 -108
View File
@@ -24,8 +24,6 @@
#include <cstring> // For memcpy
#include <algorithm> // for min/max
#include <iostream>
namespace love
{
namespace graphics
@@ -39,43 +37,39 @@ Image::FilterMode Image::defaultMipmapFilter = Image::FILTER_NONE;
float Image::defaultMipmapSharpness = 0.0f;
Image::Image(love::image::ImageData *data)
: width((float)(data->getWidth()))
: cdata(0)
, width((float)(data->getWidth()))
, height((float)(data->getHeight()))
, texture(0)
, mipmapSharpness(defaultMipmapSharpness)
, mipmapsCreated(false)
, compressed(false)
{
data->retain();
this->data = data;
preload();
}
memset(vertices, 255, sizeof(vertex)*4);
vertices[0].x = 0;
vertices[0].y = 0;
vertices[1].x = 0;
vertices[1].y = height;
vertices[2].x = width;
vertices[2].y = height;
vertices[3].x = width;
vertices[3].y = 0;
vertices[0].s = 0;
vertices[0].t = 0;
vertices[1].s = 0;
vertices[1].t = 1;
vertices[2].s = 1;
vertices[2].t = 1;
vertices[3].s = 1;
vertices[3].t = 0;
filter = getDefaultFilter();
filter.mipmap = defaultMipmapFilter;
Image::Image(love::image::CompressedData *cdata)
: data(0)
, width((float)(cdata->getWidth(0)))
, height((float)(cdata->getHeight(0)))
, texture(0)
, mipmapSharpness(defaultMipmapSharpness)
, mipmapsCreated(false)
, compressed(true)
{
cdata->retain();
this->cdata = cdata;
preload();
}
Image::~Image()
{
if (data != 0)
data->release();
if (cdata != 0)
cdata->release();
unload();
}
@@ -99,40 +93,6 @@ love::image::ImageData *Image::getData() const
return data;
}
void Image::getRectangleVertices(int x, int y, int w, int h, vertex *vertices) const
{
// Check upper.
x = (x+w > (int)width) ? (int)width-w : x;
y = (y+h > (int)height) ? (int)height-h : y;
// Check lower.
x = (x < 0) ? 0 : x;
y = (y < 0) ? 0 : y;
vertices[0].x = 0;
vertices[0].y = 0;
vertices[1].x = 0;
vertices[1].y = (float)h;
vertices[2].x = (float)w;
vertices[2].y = (float)h;
vertices[3].x = (float)w;
vertices[3].y = 0;
float tx = (float)x/width;
float ty = (float)y/height;
float tw = (float)w/width;
float th = (float)h/height;
vertices[0].s = tx;
vertices[0].t = ty;
vertices[1].s = tx;
vertices[1].t = ty+th;
vertices[2].s = tx+tw;
vertices[2].t = ty+th;
vertices[3].s = tx+tw;
vertices[3].t = ty;
}
void Image::draw(float x, float y, float angle, float sx, float sy, float ox, float oy, float kx, float ky) const
{
static Matrix t;
@@ -150,19 +110,57 @@ void Image::drawq(love::graphics::Quad *quad, float x, float y, float angle, flo
drawv(t, v);
}
void Image::checkMipmapsCreated()
void Image::uploadCompressedMipmaps()
{
if (mipmapsCreated || (filter.mipmap != FILTER_NEAREST && filter.mipmap != FILTER_LINEAR))
if (!isCompressed() || !cdata || !hasCompressedTextureSupport(cdata->getType()))
return;
bind();
int numMipmaps = cdata->getNumMipmaps();
// We have to inform OpenGL if the image doesn't have all mipmap levels.
if (GLEE_VERSION_1_2 || GLEE_SGIS_texture_lod)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, numMipmaps - 1);
}
else if (cdata->getWidth(numMipmaps-1) > 1 || cdata->getHeight(numMipmaps-1) > 1)
{
// Telling OpenGL to ignore certain levels isn't always supported.
throw love::Exception("Cannot load mipmaps: "
"compressed image does not have all required levels.");
}
for (int i = 1; i < numMipmaps; i++)
{
glCompressedTexImage2DARB(GL_TEXTURE_2D,
i,
getCompressedFormat(cdata->getType()),
cdata->getWidth(i),
cdata->getHeight(i),
0,
GLsizei(cdata->getSize(i)),
cdata->getData(i));
}
}
void Image::createMipmaps()
{
if (!data)
return;
if (!hasMipmapSupport())
throw love::Exception("Mipmap filtering is not supported on this system.");
// Some old drivers claim support for NPOT textures, but fail when creating mipmaps.
// we can't detect which systems will do this, so we fail gracefully for all NPOT images.
// Some old drivers claim support for NPOT textures, but fail when creating
// mipmaps. We can't detect which systems will do this, so we fail gracefully
// for all NPOT images.
int w = int(width), h = int(height);
if (w != next_p2(w) || h != next_p2(h))
throw love::Exception("Cannot create mipmaps: image does not have power of two dimensions.");
{
throw love::Exception("Cannot create mipmaps: "
"image does not have power of two dimensions.");
}
bind();
@@ -171,14 +169,14 @@ void Image::checkMipmapsCreated()
// AMD/ATI drivers have several bugs when generating mipmaps,
// re-uploading the entire base image seems to be required.
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA8,
(GLsizei)width,
(GLsizei)height,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
data->getData());
0,
GL_RGBA8,
(GLsizei)width,
(GLsizei)height,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
data->getData());
// More bugs: http://www.opengl.org/wiki/Common_Mistakes#Automatic_mipmap_generation
glEnable(GL_TEXTURE_2D);
@@ -188,15 +186,28 @@ void Image::checkMipmapsCreated()
{
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
glTexSubImage2D(GL_TEXTURE_2D,
0,
0,
0,
(GLsizei)width,
(GLsizei)height,
GL_RGBA,
GL_UNSIGNED_BYTE,
data->getData());
0,
0,
0,
(GLsizei)width,
(GLsizei)height,
GL_RGBA,
GL_UNSIGNED_BYTE,
data->getData());
}
}
void Image::checkMipmapsCreated()
{
if (mipmapsCreated || filter.mipmap == FILTER_NONE)
return;
if (isCompressed() && cdata && hasCompressedTextureSupport(cdata->getType()))
uploadCompressedMipmaps();
else if (data)
createMipmaps();
else
return;
mipmapsCreated = true;
}
@@ -236,7 +247,9 @@ void Image::setMipmapSharpness(float sharpness)
mipmapSharpness = std::min(std::max(sharpness, -maxMipmapSharpness + 0.01f), maxMipmapSharpness - 0.01f);
bind();
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, -mipmapSharpness); // negative bias is sharper
// negative bias is sharper
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, -mipmapSharpness);
}
else
mipmapSharpness = 0.0f;
@@ -255,6 +268,32 @@ void Image::bind() const
bindTexture(texture);
}
void Image::preload()
{
memset(vertices, 255, sizeof(vertex)*4);
vertices[0].x = 0;
vertices[0].y = 0;
vertices[1].x = 0;
vertices[1].y = height;
vertices[2].x = width;
vertices[2].y = height;
vertices[3].x = width;
vertices[3].y = 0;
vertices[0].s = 0;
vertices[0].t = 0;
vertices[1].s = 0;
vertices[1].t = 1;
vertices[2].s = 1;
vertices[2].t = 1;
vertices[3].s = 1;
vertices[3].t = 0;
filter = getDefaultFilter();
filter.mipmap = defaultMipmapFilter;
}
bool Image::load()
{
return loadVolatile();
@@ -267,6 +306,18 @@ void Image::unload()
bool Image::loadVolatile()
{
if (isCompressed() && cdata && !hasCompressedTextureSupport(cdata->getType()))
{
const char *str;
if (image::CompressedData::getConstant(cdata->getType(), str))
{
throw love::Exception("Cannot create image: "
"%s compressed images are not supported on this system.", str);
}
else
throw love::Exception("cannot create image: format is not supported on this system.");
}
if (hasMipmapSharpnessSupport() && maxMipmapSharpness == 0.0f)
glGetFloatv(GL_MAX_TEXTURE_LOD_BIAS, &maxMipmapSharpness);
@@ -296,25 +347,45 @@ bool Image::loadVolatilePOT()
while (glGetError() != GL_NO_ERROR); // clear errors
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA8,
(GLsizei)p2width,
(GLsizei)p2height,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
0);
if (isCompressed() && cdata)
{
if (s < 1.0f || t < 1.0f)
{
throw love::Exception("Cannot create image: "
"compressed NPOT images are not supported on this system.");
}
glTexSubImage2D(GL_TEXTURE_2D,
0,
0,
0,
(GLsizei)width,
(GLsizei)height,
GL_RGBA,
GL_UNSIGNED_BYTE,
data->getData());
glCompressedTexImage2DARB(GL_TEXTURE_2D,
0,
getCompressedFormat(cdata->getType()),
cdata->getWidth(0),
cdata->getHeight(0),
0,
GLsizei(cdata->getSize(0)),
cdata->getData(0));
}
else if (data)
{
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA8,
(GLsizei)p2width,
(GLsizei)p2height,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
0);
glTexSubImage2D(GL_TEXTURE_2D,
0,
0,
0,
(GLsizei)width,
(GLsizei)height,
GL_RGBA,
GL_UNSIGNED_BYTE,
data->getData());
}
if (glGetError() != GL_NO_ERROR)
throw love::Exception("Cannot create image: size may be too large for this system.");
@@ -337,15 +408,30 @@ bool Image::loadVolatileNPOT()
while (glGetError() != GL_NO_ERROR); // clear errors
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA8,
(GLsizei)width,
(GLsizei)height,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
data->getData());
if (isCompressed() && cdata)
{
GLenum format = getCompressedFormat(cdata->getType());
glCompressedTexImage2DARB(GL_TEXTURE_2D,
0,
format,
cdata->getWidth(0),
cdata->getHeight(0),
0,
GLsizei(cdata->getSize(0)),
cdata->getData(0));
}
else if (data)
{
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA8,
(GLsizei)width,
(GLsizei)height,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
data->getData());
}
if (glGetError() != GL_NO_ERROR)
throw love::Exception("Cannot create image: size may be too large for this system.");
@@ -407,6 +493,34 @@ Image::FilterMode Image::getDefaultMipmapFilter()
return defaultMipmapFilter;
}
bool Image::isCompressed() const
{
return compressed;
}
GLenum Image::getCompressedFormat(image::CompressedData::TextureType type) const
{
switch (type)
{
case image::CompressedData::TYPE_DXT1:
return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
case image::CompressedData::TYPE_DXT3:
return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
case image::CompressedData::TYPE_DXT5:
return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
case image::CompressedData::TYPE_BC5:
return GL_COMPRESSED_RG_RGTC2;
case image::CompressedData::TYPE_BC5s:
return GL_COMPRESSED_SIGNED_RG_RGTC2;
case image::CompressedData::TYPE_BC7:
return GL_COMPRESSED_RGBA_BPTC_UNORM_ARB;
case image::CompressedData::TYPE_BC7srgb:
return GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB;
default:
return GL_RGBA8;
}
}
bool Image::hasNpot()
{
return GLEE_VERSION_2_0 || GLEE_ARB_texture_non_power_of_two;
@@ -427,6 +541,37 @@ bool Image::hasMipmapSharpnessSupport()
return GLEE_VERSION_1_4 || GLEE_EXT_texture_lod_bias;
}
bool Image::hasCompressedTextureSupport()
{
return GLEE_VERSION_1_3 || GLEE_ARB_texture_compression;
}
bool Image::hasCompressedTextureSupport(image::CompressedData::TextureType type)
{
if (!hasCompressedTextureSupport())
return false;
switch (type)
{
case image::CompressedData::TYPE_DXT1:
case image::CompressedData::TYPE_DXT3:
case image::CompressedData::TYPE_DXT5:
return GLEE_EXT_texture_compression_s3tc;
case image::CompressedData::TYPE_BC5:
case image::CompressedData::TYPE_BC5s:
return (GLEE_VERSION_3_0 || GLEE_ARB_texture_compression_rgtc || GLEE_EXT_texture_compression_rgtc);
case image::CompressedData::TYPE_BC7:
case image::CompressedData::TYPE_BC7srgb:
return (GLEE_VERSION_4_2 || GLEE_ARB_texture_compression_bptc);
default:
return false;
}
}
} // opengl
} // graphics
} // love
+33 -16
View File
@@ -26,6 +26,7 @@
#include "common/math.h"
#include "common/config.h"
#include "image/ImageData.h"
#include "image/CompressedData.h"
#include "graphics/Image.h"
// OpenGL
@@ -52,10 +53,17 @@ public:
* Creates a new Image. Not that anything is ready to use
* before load is called.
*
* @param file The file from which to load the image.
* @param data The data from which to load the image.
**/
Image(love::image::ImageData *data);
/**
* Creates a new Image with compressed image data.
*
* @param cdata The compressed data from which to load the image.
**/
Image(love::image::CompressedData *cdata);
/**
* Destructor. Deletes the hardware texture and other resources.
**/
@@ -68,20 +76,6 @@ public:
love::image::ImageData *getData() const;
/**
* Generate vertices according to a subimage.
*
* Note: out-of-range values will be clamped.
* Note: the vertex colors will not be changed.
*
* @param x The top-left corner of the subimage along the x-axis.
* @param y The top-left corner of the subimage along the y-axis.
* @param w The width of the subimage.
* @param h The height of the subimage.
* @param vertices A vertex array of size four.
**/
void getRectangleVertices(int x, int y, int w, int h, vertex *vertices) const;
/**
* @copydoc Drawable::draw()
**/
@@ -107,6 +101,11 @@ public:
void setMipmapSharpness(float sharpness);
float getMipmapSharpness() const;
/**
* Whether this Image is using a compressed texture (via CompressedData).
**/
bool isCompressed() const;
void bind() const;
bool load();
@@ -126,6 +125,9 @@ public:
static bool hasMipmapSupport();
static bool hasMipmapSharpnessSupport();
static bool hasCompressedTextureSupport();
static bool hasCompressedTextureSupport(image::CompressedData::TextureType type);
private:
void drawv(const Matrix &t, const vertex *v) const;
@@ -135,9 +137,15 @@ private:
{
return texture;
}
// The ImageData from which the texture is created.
// The ImageData from which the texture is created. May be null if
// Compressed image data was used to create the texture.
love::image::ImageData *data;
// Or the Compressed Image Data from which the texture is created. May be
// null if raw ImageData was used to create the texture.
love::image::CompressedData *cdata;
// Width and height of the hardware texture.
float width, height;
@@ -153,15 +161,22 @@ private:
// True if mipmaps have been created for this Image.
bool mipmapsCreated;
// Whether this Image is using a compressed texture.
bool compressed;
// The image's filter mode
Image::Filter filter;
// The image's wrap mode
Image::Wrap wrap;
void preload();
bool loadVolatilePOT();
bool loadVolatileNPOT();
void uploadCompressedMipmaps();
void createMipmaps();
void checkMipmapsCreated();
static float maxMipmapSharpness;
@@ -169,6 +184,8 @@ private:
static FilterMode defaultMipmapFilter;
static float defaultMipmapSharpness;
GLenum getCompressedFormat(image::CompressedData::TextureType type) const;
}; // Image
} // opengl
+8
View File
@@ -97,6 +97,14 @@ void initializeContext()
glUnmapBufferARB = (GLEEPFNGLUNMAPBUFFERARBPROC) glUnmapBuffer;
}
// Same deal for compressed textures.
if (GLEE_VERSION_1_3 && !GLEE_ARB_texture_compression)
{
glCompressedTexImage2DARB = (GLEEPFNGLCOMPRESSEDTEXIMAGE2DARBPROC) glCompressedTexImage2D;
glCompressedTexSubImage2DARB = (GLEEPFNGLCOMPRESSEDTEXSUBIMAGE2DARBPROC) glCompressedTexSubImage2D;
glGetCompressedTexImageARB = (GLEEPFNGLGETCOMPRESSEDTEXIMAGEARBPROC) glGetCompressedTexImage;
}
if (GLEE_EXT_texture_filter_anisotropic)
glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAnisotropy);
else
+55 -10
View File
@@ -158,7 +158,14 @@ int w_present(lua_State *)
int w_setIcon(lua_State *L)
{
Image *image = luax_checktype<Image>(L, 1, "Image", GRAPHICS_IMAGE_T);
instance->setIcon(image);
try
{
instance->setIcon(image);
}
catch (love::Exception &e)
{
return luaL_error(L, "%s", e.what());
}
return 0;
}
@@ -269,21 +276,45 @@ int w_setInvertedStencil(lua_State *L)
int w_newImage(lua_State *L)
{
// Convert to File, if necessary.
if (lua_isstring(L, 1))
luax_convobj(L, 1, "filesystem", "newFile");
love::image::ImageData *data = 0;
love::image::CompressedData *cdata = 0;
// Convert to ImageData, if necessary.
if (luax_istype(L, 1, FILESYSTEM_FILE_T))
luax_convobj(L, 1, "image", "newImageData");
// Convert to FileData, if necessary.
if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T))
luax_convobj(L, 1, "filesystem", "newFileData");
love::image::ImageData *data = luax_checktype<love::image::ImageData>(L, 1, "ImageData", IMAGE_IMAGE_DATA_T);
// Convert to ImageData/CompressedData, if necessary.
if (luax_istype(L, 1, FILESYSTEM_FILE_DATA_T))
{
// Determine whether to convert to ImageData or CompressedData.
luax_getfunction(L, "image", "isCompressed");
lua_pushvalue(L, 1);
lua_call(L, 1, 1);
bool compressed = luax_toboolean(L, -1);
lua_pop(L, 1);
if (compressed)
luax_convobj(L, 1, "image", "newCompressedData");
else
luax_convobj(L, 1, "image", "newImageData");
}
if (luax_istype(L, 1, IMAGE_COMPRESSED_DATA_T))
cdata = luax_checktype<love::image::CompressedData>(L, 1, "CompressedData", IMAGE_COMPRESSED_DATA_T);
else
data = luax_checktype<love::image::ImageData>(L, 1, "ImageData", IMAGE_IMAGE_DATA_T);
// Create the image.
Image *image = 0;
try
{
image = instance->newImage(data);
if (cdata)
image = instance->newImage(cdata);
else if (data)
image = instance->newImage(data);
else
throw love::Exception("Error creating image.");
}
catch(love::Exception &e)
{
@@ -387,7 +418,7 @@ int w_newImageFont(lua_State *L)
int startIndex = 2;
// Convert to ImageData if necessary.
if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T) || (luax_istype(L, 1, DATA_T) && !luax_istype(L, 1, IMAGE_IMAGE_DATA_T)))
if (lua_isstring(L, 1) || luax_istype(L, 1, FILESYSTEM_FILE_T) || luax_istype(L, 1, FILESYSTEM_FILE_DATA_T))
luax_convobj(L, 1, "image", "newImageData");
else if (luax_istype(L, 1, GRAPHICS_IMAGE_T))
{
@@ -395,6 +426,8 @@ int w_newImageFont(lua_State *L)
img_filter = i->getFilter();
setFilter = true;
love::image::ImageData *id = i->getData();
if (!id)
return luaL_argerror(L, 1, "image cannot be compressed");
luax_newtype(L, "ImageData", IMAGE_IMAGE_DATA_T, (void *)id, false);
lua_replace(L, 1);
}
@@ -1110,6 +1143,18 @@ int w_isSupported(lua_State *L)
if (!Image::hasMipmapSupport())
supported = false;
break;
case Graphics::SUPPORT_DXT:
if (!Image::hasCompressedTextureSupport(image::CompressedData::TYPE_DXT5))
supported = false;
break;
case Graphics::SUPPORT_BC5:
if (!Image::hasCompressedTextureSupport(image::CompressedData::TYPE_BC5))
supported = false;
break;
case Graphics::SUPPORT_BC7:
if (!Image::hasCompressedTextureSupport(image::CompressedData::TYPE_BC7))
supported = false;
break;
default:
supported = false;
}
@@ -175,6 +175,13 @@ int w_Image_getWrap(lua_State *L)
return 2;
}
int w_Image_isCompressed(lua_State *L)
{
Image *i = luax_checkimage(L, 1);
luax_pushboolean(L, i->isCompressed());
return 1;
}
static const luaL_Reg functions[] =
{
{ "getWidth", w_Image_getWidth },
@@ -186,6 +193,7 @@ static const luaL_Reg functions[] =
{ "getWrap", w_Image_getWrap },
{ "setMipmapFilter", w_Image_setMipmapFilter },
{ "getMipmapFilter", w_Image_getMipmapFilter },
{ "isCompressed", w_Image_isCompressed },
{ 0, 0 }
};
+1
View File
@@ -42,6 +42,7 @@ int w_Image_setMipmapFilter(lua_State *L);
int w_Image_getMipmapFilter(lua_State *L);
int w_Image_setWrap(lua_State *L);
int w_Image_getWrap(lua_State *L);
int w_Image_isCompressed(lua_State *L);
extern "C" int luaopen_image(lua_State *L);
} // opengl