mirror of
https://github.com/love2d/love.git
synced 2026-08-17 19:23:38 +02:00
Changed love.graphics.newImage's optional second argument to be a table of flags. Current flags are 'mipmaps' and 'srgb'.
If mipmaps are enabled on image creation, the image will use the default mipmap filter (now 'nearest' by default.) Image:setMipmapFilter will error if the image was not created with mipmaps enabled. --HG-- branch : minor
This commit is contained in:
@@ -89,6 +89,23 @@ const Texture::Filter &Texture::getDefaultFilter()
|
|||||||
return defaultFilter;
|
return defaultFilter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Texture::validateFilter(const Filter &f, bool mipmapsAllowed)
|
||||||
|
{
|
||||||
|
if (!mipmapsAllowed && f.mipmap != FILTER_NONE)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (f.mag != FILTER_LINEAR && f.mag != FILTER_NEAREST)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (f.min != FILTER_LINEAR && f.min != FILTER_NEAREST)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (f.mipmap != FILTER_LINEAR && f.mipmap != FILTER_NEAREST && f.mipmap != FILTER_NONE)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool Texture::getConstant(const char *in, FilterMode &out)
|
bool Texture::getConstant(const char *in, FilterMode &out)
|
||||||
{
|
{
|
||||||
return filterModes.find(in, out);
|
return filterModes.find(in, out);
|
||||||
@@ -113,6 +130,7 @@ StringMap<Texture::FilterMode, Texture::FILTER_MAX_ENUM>::Entry Texture::filterM
|
|||||||
{
|
{
|
||||||
{ "linear", Texture::FILTER_LINEAR },
|
{ "linear", Texture::FILTER_LINEAR },
|
||||||
{ "nearest", Texture::FILTER_NEAREST },
|
{ "nearest", Texture::FILTER_NEAREST },
|
||||||
|
{ "none", Texture::FILTER_NONE },
|
||||||
};
|
};
|
||||||
|
|
||||||
StringMap<Texture::FilterMode, Texture::FILTER_MAX_ENUM> Texture::filterModes(Texture::filterModeEntries, sizeof(Texture::filterModeEntries));
|
StringMap<Texture::FilterMode, Texture::FILTER_MAX_ENUM> Texture::filterModes(Texture::filterModeEntries, sizeof(Texture::filterModeEntries));
|
||||||
|
|||||||
@@ -105,6 +105,8 @@ public:
|
|||||||
static void setDefaultFilter(const Filter &f);
|
static void setDefaultFilter(const Filter &f);
|
||||||
static const Filter &getDefaultFilter();
|
static const Filter &getDefaultFilter();
|
||||||
|
|
||||||
|
static bool validateFilter(const Filter &f, bool mipmapsAllowed);
|
||||||
|
|
||||||
static bool getConstant(const char *in, FilterMode &out);
|
static bool getConstant(const char *in, FilterMode &out);
|
||||||
static bool getConstant(FilterMode in, const char *&out);
|
static bool getConstant(FilterMode in, const char *&out);
|
||||||
|
|
||||||
|
|||||||
@@ -637,6 +637,9 @@ void Canvas::drawq(Quad *quad, float x, float y, float angle, float sx, float sy
|
|||||||
|
|
||||||
void Canvas::setFilter(const Texture::Filter &f)
|
void Canvas::setFilter(const Texture::Filter &f)
|
||||||
{
|
{
|
||||||
|
if (!validateFilter(f, false))
|
||||||
|
throw love::Exception("Invalid texture filter.");
|
||||||
|
|
||||||
filter = f;
|
filter = f;
|
||||||
gl.bindTexture(texture);
|
gl.bindTexture(texture);
|
||||||
gl.setTextureFilter(filter);
|
gl.setTextureFilter(filter);
|
||||||
|
|||||||
@@ -528,6 +528,9 @@ float Font::getSpacing() const
|
|||||||
|
|
||||||
void Font::setFilter(const Texture::Filter &f)
|
void Font::setFilter(const Texture::Filter &f)
|
||||||
{
|
{
|
||||||
|
if (!Texture::validateFilter(f, false))
|
||||||
|
throw love::Exception("Invalid texture filter.");
|
||||||
|
|
||||||
filter = f;
|
filter = f;
|
||||||
|
|
||||||
for (auto it = textures.begin(); it != textures.end(); ++it)
|
for (auto it = textures.begin(); it != textures.end(); ++it)
|
||||||
|
|||||||
@@ -423,10 +423,10 @@ void Graphics::discardStencil()
|
|||||||
activeStencil = false;
|
activeStencil = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Image *Graphics::newImage(love::image::ImageData *data, Image::Format format)
|
Image *Graphics::newImage(love::image::ImageData *data, const Image::Flags &flags)
|
||||||
{
|
{
|
||||||
// Create the image.
|
// Create the image.
|
||||||
Image *image = new Image(data, format);
|
Image *image = new Image(data, flags);
|
||||||
|
|
||||||
if (!isCreated())
|
if (!isCreated())
|
||||||
return image;
|
return image;
|
||||||
@@ -444,16 +444,16 @@ Image *Graphics::newImage(love::image::ImageData *data, Image::Format format)
|
|||||||
if (!success)
|
if (!success)
|
||||||
{
|
{
|
||||||
image->release();
|
image->release();
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
Image *Graphics::newImage(love::image::CompressedData *cdata, Image::Format format)
|
Image *Graphics::newImage(love::image::CompressedData *cdata, const Image::Flags &flags)
|
||||||
{
|
{
|
||||||
// Create the image.
|
// Create the image.
|
||||||
Image *image = new Image(cdata, format);
|
Image *image = new Image(cdata, flags);
|
||||||
|
|
||||||
if (!isCreated())
|
if (!isCreated())
|
||||||
return image;
|
return image;
|
||||||
@@ -471,7 +471,7 @@ Image *Graphics::newImage(love::image::CompressedData *cdata, Image::Format form
|
|||||||
if (!success)
|
if (!success)
|
||||||
{
|
{
|
||||||
image->release();
|
image->release();
|
||||||
return 0;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
return image;
|
return image;
|
||||||
|
|||||||
@@ -192,8 +192,8 @@ public:
|
|||||||
/**
|
/**
|
||||||
* Creates an Image object with padding and/or optimization.
|
* Creates an Image object with padding and/or optimization.
|
||||||
**/
|
**/
|
||||||
Image *newImage(love::image::ImageData *data, Image::Format format = Image::FORMAT_NORMAL);
|
Image *newImage(love::image::ImageData *data, const Image::Flags &flags);
|
||||||
Image *newImage(love::image::CompressedData *cdata, Image::Format format = Image::FORMAT_NORMAL);
|
Image *newImage(love::image::CompressedData *cdata, const Image::Flags &flags);
|
||||||
|
|
||||||
Quad *newQuad(Quad::Viewport v, float sw, float sh);
|
Quad *newQuad(Quad::Viewport v, float sw, float sh);
|
||||||
|
|
||||||
|
|||||||
@@ -33,17 +33,16 @@ namespace opengl
|
|||||||
|
|
||||||
float Image::maxMipmapSharpness = 0.0f;
|
float Image::maxMipmapSharpness = 0.0f;
|
||||||
|
|
||||||
Texture::FilterMode Image::defaultMipmapFilter = Texture::FILTER_NONE;
|
Texture::FilterMode Image::defaultMipmapFilter = Texture::FILTER_NEAREST;
|
||||||
float Image::defaultMipmapSharpness = 0.0f;
|
float Image::defaultMipmapSharpness = 0.0f;
|
||||||
|
|
||||||
Image::Image(love::image::ImageData *data, Format format)
|
Image::Image(love::image::ImageData *data, const Flags &flags)
|
||||||
: data(data)
|
: data(data)
|
||||||
, cdata(nullptr)
|
, cdata(nullptr)
|
||||||
, texture(0)
|
, texture(0)
|
||||||
, mipmapSharpness(defaultMipmapSharpness)
|
, mipmapSharpness(defaultMipmapSharpness)
|
||||||
, mipmapsCreated(false)
|
|
||||||
, compressed(false)
|
, compressed(false)
|
||||||
, format(format)
|
, flags(flags)
|
||||||
, usingDefaultTexture(false)
|
, usingDefaultTexture(false)
|
||||||
{
|
{
|
||||||
width = data->getWidth();
|
width = data->getWidth();
|
||||||
@@ -53,14 +52,13 @@ Image::Image(love::image::ImageData *data, Format format)
|
|||||||
preload();
|
preload();
|
||||||
}
|
}
|
||||||
|
|
||||||
Image::Image(love::image::CompressedData *cdata, Format format)
|
Image::Image(love::image::CompressedData *cdata, const Flags &flags)
|
||||||
: data(nullptr)
|
: data(nullptr)
|
||||||
, cdata(cdata)
|
, cdata(cdata)
|
||||||
, texture(0)
|
, texture(0)
|
||||||
, mipmapSharpness(defaultMipmapSharpness)
|
, mipmapSharpness(defaultMipmapSharpness)
|
||||||
, mipmapsCreated(false)
|
|
||||||
, compressed(true)
|
, compressed(true)
|
||||||
, format(format)
|
, flags(flags)
|
||||||
, usingDefaultTexture(false)
|
, usingDefaultTexture(false)
|
||||||
{
|
{
|
||||||
width = cdata->getWidth(0);
|
width = cdata->getWidth(0);
|
||||||
@@ -119,99 +117,16 @@ GLuint Image::getGLTexture() const
|
|||||||
return texture;
|
return texture;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Image::uploadCompressedMipmaps()
|
|
||||||
{
|
|
||||||
if (!isCompressed() || !cdata || !hasCompressedTextureSupport(cdata->getFormat()))
|
|
||||||
return;
|
|
||||||
|
|
||||||
bind();
|
|
||||||
|
|
||||||
int count = cdata->getMipmapCount();
|
|
||||||
|
|
||||||
// We have to inform OpenGL if the image doesn't have all mipmap levels.
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, count - 1);
|
|
||||||
|
|
||||||
for (int i = 1; i < count; i++)
|
|
||||||
{
|
|
||||||
glCompressedTexImage2D(GL_TEXTURE_2D,
|
|
||||||
i,
|
|
||||||
getCompressedFormat(cdata->getFormat()),
|
|
||||||
cdata->getWidth(i),
|
|
||||||
cdata->getHeight(i),
|
|
||||||
0,
|
|
||||||
GLsizei(cdata->getSize(i)),
|
|
||||||
cdata->getData(i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Image::createMipmaps()
|
|
||||||
{
|
|
||||||
// Only valid for Images created with ImageData.
|
|
||||||
if (!data || isCompressed())
|
|
||||||
return;
|
|
||||||
|
|
||||||
// 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.");
|
|
||||||
}
|
|
||||||
|
|
||||||
bind();
|
|
||||||
|
|
||||||
// Prevent other threads from changing the ImageData while we upload it.
|
|
||||||
love::thread::Lock lock(data->getMutex());
|
|
||||||
|
|
||||||
if (GLEE_VERSION_3_0 || GLEE_ARB_framebuffer_object)
|
|
||||||
{
|
|
||||||
if (gl.getVendor() == OpenGL::VENDOR_ATI_AMD)
|
|
||||||
{
|
|
||||||
// AMD/ATI drivers have several bugs when generating mipmaps,
|
|
||||||
// re-uploading the entire base image seems to be required.
|
|
||||||
uploadTexture();
|
|
||||||
|
|
||||||
// More bugs: http://www.opengl.org/wiki/Common_Mistakes#Automatic_mipmap_generation
|
|
||||||
glEnable(GL_TEXTURE_2D);
|
|
||||||
}
|
|
||||||
|
|
||||||
glGenerateMipmap(GL_TEXTURE_2D);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
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());
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Image::checkMipmapsCreated()
|
|
||||||
{
|
|
||||||
if (mipmapsCreated || filter.mipmap == FILTER_NONE || usingDefaultTexture)
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (isCompressed() && cdata && hasCompressedTextureSupport(cdata->getFormat()))
|
|
||||||
uploadCompressedMipmaps();
|
|
||||||
else if (data)
|
|
||||||
createMipmaps();
|
|
||||||
else
|
|
||||||
return;
|
|
||||||
|
|
||||||
mipmapsCreated = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Image::setFilter(const Texture::Filter &f)
|
void Image::setFilter(const Texture::Filter &f)
|
||||||
{
|
{
|
||||||
|
if (!validateFilter(f, flags.mipmaps))
|
||||||
|
{
|
||||||
|
if (f.mipmap != FILTER_NONE && !flags.mipmaps)
|
||||||
|
throw love::Exception("Non-mipmapped image cannot have mipmap filtering.");
|
||||||
|
else
|
||||||
|
throw love::Exception("Invalid texture filter.");
|
||||||
|
}
|
||||||
|
|
||||||
filter = f;
|
filter = f;
|
||||||
|
|
||||||
// We don't want filtering or (attempted) mipmaps on the default texture.
|
// We don't want filtering or (attempted) mipmaps on the default texture.
|
||||||
@@ -223,7 +138,6 @@ void Image::setFilter(const Texture::Filter &f)
|
|||||||
|
|
||||||
bind();
|
bind();
|
||||||
gl.setTextureFilter(filter);
|
gl.setTextureFilter(filter);
|
||||||
checkMipmapsCreated();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Image::setWrap(const Texture::Wrap &w)
|
void Image::setWrap(const Texture::Wrap &w)
|
||||||
@@ -260,6 +174,7 @@ void Image::bind() const
|
|||||||
|
|
||||||
void Image::preload()
|
void Image::preload()
|
||||||
{
|
{
|
||||||
|
// For colors.
|
||||||
memset(vertices, 255, sizeof(Vertex)*4);
|
memset(vertices, 255, sizeof(Vertex)*4);
|
||||||
|
|
||||||
vertices[0].x = 0;
|
vertices[0].x = 0;
|
||||||
@@ -280,7 +195,8 @@ void Image::preload()
|
|||||||
vertices[3].s = 1;
|
vertices[3].s = 1;
|
||||||
vertices[3].t = 0;
|
vertices[3].t = 0;
|
||||||
|
|
||||||
filter.mipmap = defaultMipmapFilter;
|
if (flags.mipmaps)
|
||||||
|
filter.mipmap = defaultMipmapFilter;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Image::load()
|
bool Image::load()
|
||||||
@@ -293,9 +209,85 @@ void Image::unload()
|
|||||||
return unloadVolatile();
|
return unloadVolatile();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Image::uploadCompressedData()
|
||||||
|
{
|
||||||
|
GLenum format = getCompressedFormat(cdata->getFormat());
|
||||||
|
int count = flags.mipmaps ? cdata->getMipmapCount() : 1;
|
||||||
|
|
||||||
|
// We have to inform OpenGL if the image doesn't have all mipmap levels.
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, count - 1);
|
||||||
|
|
||||||
|
for (int i = 0; i < count; i++)
|
||||||
|
{
|
||||||
|
glCompressedTexImage2D(GL_TEXTURE_2D,
|
||||||
|
i,
|
||||||
|
format,
|
||||||
|
cdata->getWidth(i),
|
||||||
|
cdata->getHeight(i),
|
||||||
|
0,
|
||||||
|
GLsizei(cdata->getSize(i)),
|
||||||
|
cdata->getData(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Image::uploadImageData()
|
||||||
|
{
|
||||||
|
if (flags.mipmaps)
|
||||||
|
{
|
||||||
|
// NPOT mipmap generation isn't always supported on old GPUs/drivers...
|
||||||
|
if (width != next_p2(width) || height != next_p2(height))
|
||||||
|
{
|
||||||
|
throw love::Exception("Cannot create mipmaps: "
|
||||||
|
"image does not have power-of-two dimensions.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!GLEE_VERSION_3_0 && !GLEE_ARB_framebuffer_object)
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
GLenum iformat = flags.sRGB ? GL_SRGB8_ALPHA8 : GL_RGBA8;
|
||||||
|
|
||||||
|
{
|
||||||
|
love::thread::Lock lock(data->getMutex());
|
||||||
|
glTexImage2D(GL_TEXTURE_2D,
|
||||||
|
0,
|
||||||
|
iformat,
|
||||||
|
(GLsizei)width,
|
||||||
|
(GLsizei)height,
|
||||||
|
0,
|
||||||
|
GL_RGBA,
|
||||||
|
GL_UNSIGNED_BYTE,
|
||||||
|
data->getData());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (flags.mipmaps)
|
||||||
|
{
|
||||||
|
if (GLEE_VERSION_3_0 || GLEE_ARB_framebuffer_object)
|
||||||
|
{
|
||||||
|
// Driver bug: http://www.opengl.org/wiki/Common_Mistakes#Automatic_mipmap_generation
|
||||||
|
if (gl.getVendor() == OpenGL::VENDOR_ATI_AMD)
|
||||||
|
glEnable(GL_TEXTURE_2D);
|
||||||
|
|
||||||
|
glGenerateMipmap(GL_TEXTURE_2D);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_FALSE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Image::uploadTexture()
|
||||||
|
{
|
||||||
|
bind();
|
||||||
|
|
||||||
|
if (isCompressed() && cdata)
|
||||||
|
uploadCompressedData();
|
||||||
|
else if (data)
|
||||||
|
uploadImageData();
|
||||||
|
}
|
||||||
|
|
||||||
bool Image::loadVolatile()
|
bool Image::loadVolatile()
|
||||||
{
|
{
|
||||||
if (format == FORMAT_SRGB && !hasSRGBSupport())
|
if (flags.sRGB && !hasSRGBSupport())
|
||||||
throw love::Exception("sRGB images are not supported on this system.");
|
throw love::Exception("sRGB images are not supported on this system.");
|
||||||
|
|
||||||
if (isCompressed() && cdata && !hasCompressedTextureSupport(cdata->getFormat()))
|
if (isCompressed() && cdata && !hasCompressedTextureSupport(cdata->getFormat()))
|
||||||
@@ -320,6 +312,9 @@ bool Image::loadVolatile()
|
|||||||
gl.setTextureWrap(wrap);
|
gl.setTextureWrap(wrap);
|
||||||
setMipmapSharpness(mipmapSharpness);
|
setMipmapSharpness(mipmapSharpness);
|
||||||
|
|
||||||
|
if (!flags.mipmaps)
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
|
||||||
|
|
||||||
// Use a default texture if the size is too big for the system.
|
// Use a default texture if the size is too big for the system.
|
||||||
if (width > gl.getMaxTextureSize() || height > gl.getMaxTextureSize())
|
if (width > gl.getMaxTextureSize() || height > gl.getMaxTextureSize())
|
||||||
{
|
{
|
||||||
@@ -327,55 +322,27 @@ bool Image::loadVolatile()
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mutex lock will potentially cover texture loading and mipmap creation.
|
|
||||||
love::thread::EmptyLock lock;
|
|
||||||
if (data)
|
|
||||||
lock.setLock(data->getMutex());
|
|
||||||
|
|
||||||
while (glGetError() != GL_NO_ERROR); // Clear errors.
|
while (glGetError() != GL_NO_ERROR); // Clear errors.
|
||||||
|
|
||||||
uploadTexture();
|
try
|
||||||
|
{
|
||||||
|
uploadTexture();
|
||||||
|
|
||||||
GLenum glerr = glGetError();
|
GLenum glerr = glGetError();
|
||||||
if (glerr != GL_NO_ERROR)
|
if (glerr != GL_NO_ERROR)
|
||||||
throw love::Exception("Cannot create image (error code 0x%x)", glerr);
|
throw love::Exception("Cannot create image (error code 0x%x)", glerr);
|
||||||
|
}
|
||||||
|
catch (love::Exception &)
|
||||||
|
{
|
||||||
|
gl.deleteTexture(texture);
|
||||||
|
texture = 0;
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
|
||||||
usingDefaultTexture = false;
|
usingDefaultTexture = false;
|
||||||
mipmapsCreated = false;
|
|
||||||
checkMipmapsCreated();
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Image::uploadTexture()
|
|
||||||
{
|
|
||||||
if (isCompressed() && cdata)
|
|
||||||
{
|
|
||||||
GLenum format = getCompressedFormat(cdata->getFormat());
|
|
||||||
glCompressedTexImage2D(GL_TEXTURE_2D,
|
|
||||||
0,
|
|
||||||
format,
|
|
||||||
cdata->getWidth(0),
|
|
||||||
cdata->getHeight(0),
|
|
||||||
0,
|
|
||||||
GLsizei(cdata->getSize(0)),
|
|
||||||
cdata->getData(0));
|
|
||||||
}
|
|
||||||
else if (data)
|
|
||||||
{
|
|
||||||
GLenum iformat = (format == FORMAT_SRGB) ? GL_SRGB8_ALPHA8 : GL_RGBA8;
|
|
||||||
glTexImage2D(GL_TEXTURE_2D,
|
|
||||||
0,
|
|
||||||
iformat,
|
|
||||||
(GLsizei)width,
|
|
||||||
(GLsizei)height,
|
|
||||||
0,
|
|
||||||
GL_RGBA,
|
|
||||||
GL_UNSIGNED_BYTE,
|
|
||||||
data->getData());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void Image::unloadVolatile()
|
void Image::unloadVolatile()
|
||||||
{
|
{
|
||||||
// Delete the hardware texture.
|
// Delete the hardware texture.
|
||||||
@@ -398,32 +365,20 @@ bool Image::refresh()
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// We want this lock to potentially cover mipmap creation as well.
|
|
||||||
love::thread::EmptyLock lock;
|
|
||||||
|
|
||||||
bind();
|
|
||||||
|
|
||||||
if (data && !isCompressed())
|
|
||||||
lock.setLock(data->getMutex());
|
|
||||||
|
|
||||||
while (glGetError() != GL_NO_ERROR); // Clear errors.
|
while (glGetError() != GL_NO_ERROR); // Clear errors.
|
||||||
|
|
||||||
uploadTexture();
|
uploadTexture();
|
||||||
|
|
||||||
if (glGetError() != GL_NO_ERROR)
|
GLenum glerr = glGetError();
|
||||||
uploadDefaultTexture();
|
if (glerr != GL_NO_ERROR)
|
||||||
else
|
throw love::Exception("Cannot refresh image (error code 0x%x)", glerr);
|
||||||
usingDefaultTexture = false;
|
|
||||||
|
|
||||||
mipmapsCreated = false;
|
|
||||||
checkMipmapsCreated();
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Image::Format Image::getFormat() const
|
const Image::Flags &Image::getFlags() const
|
||||||
{
|
{
|
||||||
return format;
|
return flags;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Image::uploadDefaultTexture()
|
void Image::uploadDefaultTexture()
|
||||||
@@ -492,22 +447,20 @@ bool Image::isCompressed() const
|
|||||||
|
|
||||||
GLenum Image::getCompressedFormat(image::CompressedData::Format cformat) const
|
GLenum Image::getCompressedFormat(image::CompressedData::Format cformat) const
|
||||||
{
|
{
|
||||||
bool srgb = format == FORMAT_SRGB;
|
|
||||||
|
|
||||||
switch (cformat)
|
switch (cformat)
|
||||||
{
|
{
|
||||||
case image::CompressedData::FORMAT_DXT1:
|
case image::CompressedData::FORMAT_DXT1:
|
||||||
if (srgb)
|
if (flags.sRGB)
|
||||||
return GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
|
return GL_COMPRESSED_SRGB_S3TC_DXT1_EXT;
|
||||||
else
|
else
|
||||||
return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
|
return GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
|
||||||
case image::CompressedData::FORMAT_DXT3:
|
case image::CompressedData::FORMAT_DXT3:
|
||||||
if (srgb)
|
if (flags.sRGB)
|
||||||
return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
|
return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT;
|
||||||
else
|
else
|
||||||
return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
|
return GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
|
||||||
case image::CompressedData::FORMAT_DXT5:
|
case image::CompressedData::FORMAT_DXT5:
|
||||||
if (srgb)
|
if (flags.sRGB)
|
||||||
return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
|
return GL_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT;
|
||||||
else
|
else
|
||||||
return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
|
return GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
|
||||||
@@ -520,7 +473,7 @@ GLenum Image::getCompressedFormat(image::CompressedData::Format cformat) const
|
|||||||
case image::CompressedData::FORMAT_BC5s:
|
case image::CompressedData::FORMAT_BC5s:
|
||||||
return GL_COMPRESSED_SIGNED_RG_RGTC2;
|
return GL_COMPRESSED_SIGNED_RG_RGTC2;
|
||||||
default:
|
default:
|
||||||
if (srgb)
|
if (flags.sRGB)
|
||||||
return GL_SRGB8_ALPHA8;
|
return GL_SRGB8_ALPHA8;
|
||||||
else
|
else
|
||||||
return GL_RGBA8;
|
return GL_RGBA8;
|
||||||
@@ -557,23 +510,23 @@ bool Image::hasSRGBSupport()
|
|||||||
return GLEE_VERSION_2_1 || GLEE_EXT_texture_sRGB;
|
return GLEE_VERSION_2_1 || GLEE_EXT_texture_sRGB;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Image::getConstant(const char *in, Format &out)
|
bool Image::getConstant(const char *in, FlagType &out)
|
||||||
{
|
{
|
||||||
return formats.find(in, out);
|
return flagNames.find(in, out);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Image::getConstant(Format in, const char *&out)
|
bool Image::getConstant(FlagType in, const char *&out)
|
||||||
{
|
{
|
||||||
return formats.find(in, out);
|
return flagNames.find(in, out);
|
||||||
}
|
}
|
||||||
|
|
||||||
StringMap<Image::Format, Image::FORMAT_MAX_ENUM>::Entry Image::formatEntries[] =
|
StringMap<Image::FlagType, Image::FLAG_TYPE_MAX_ENUM>::Entry Image::flagNameEntries[] =
|
||||||
{
|
{
|
||||||
{"normal", Image::FORMAT_NORMAL},
|
{"mipmaps", Image::FLAG_TYPE_MIPMAPS},
|
||||||
{"srgb", Image::FORMAT_SRGB},
|
{"srgb", Image::FLAG_TYPE_SRGB},
|
||||||
};
|
};
|
||||||
|
|
||||||
StringMap<Image::Format, Image::FORMAT_MAX_ENUM> Image::formats(Image::formatEntries, sizeof(Image::formatEntries));
|
StringMap<Image::FlagType, Image::FLAG_TYPE_MAX_ENUM> Image::flagNames(Image::flagNameEntries, sizeof(Image::flagNameEntries));
|
||||||
|
|
||||||
} // opengl
|
} // opengl
|
||||||
} // graphics
|
} // graphics
|
||||||
|
|||||||
@@ -51,11 +51,19 @@ class Image : public Texture
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
enum Format
|
enum FlagType
|
||||||
{
|
{
|
||||||
FORMAT_NORMAL,
|
FLAG_TYPE_MIPMAPS,
|
||||||
FORMAT_SRGB,
|
FLAG_TYPE_SRGB,
|
||||||
FORMAT_MAX_ENUM
|
FLAG_TYPE_MAX_ENUM
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Flags
|
||||||
|
{
|
||||||
|
bool mipmaps;
|
||||||
|
bool sRGB;
|
||||||
|
|
||||||
|
Flags() : mipmaps(false), sRGB(false) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -64,14 +72,14 @@ public:
|
|||||||
*
|
*
|
||||||
* @param data The data from which to load the image.
|
* @param data The data from which to load the image.
|
||||||
**/
|
**/
|
||||||
Image(love::image::ImageData *data, Format format = FORMAT_NORMAL);
|
Image(love::image::ImageData *data, const Flags &flags);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new Image with compressed image data.
|
* Creates a new Image with compressed image data.
|
||||||
*
|
*
|
||||||
* @param cdata The compressed data from which to load the image.
|
* @param cdata The compressed data from which to load the image.
|
||||||
**/
|
**/
|
||||||
Image(love::image::CompressedData *cdata, Format format = FORMAT_NORMAL);
|
Image(love::image::CompressedData *cdata, const Flags &flags);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Destructor. Deletes the hardware texture and other resources.
|
* Destructor. Deletes the hardware texture and other resources.
|
||||||
@@ -123,12 +131,11 @@ public:
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Re-uploads the ImageData or CompressedData associated with this Image to
|
* Re-uploads the ImageData or CompressedData associated with this Image to
|
||||||
* the GPU, allowing situations where lovers modify an ImageData after image
|
* the GPU.
|
||||||
* creation from the ImageData, and apply the changes with Image:refresh().
|
|
||||||
**/
|
**/
|
||||||
bool refresh();
|
bool refresh();
|
||||||
|
|
||||||
Format getFormat() const;
|
const Flags &getFlags() const;
|
||||||
|
|
||||||
static void setDefaultMipmapSharpness(float sharpness);
|
static void setDefaultMipmapSharpness(float sharpness);
|
||||||
static float getDefaultMipmapSharpness();
|
static float getDefaultMipmapSharpness();
|
||||||
@@ -139,8 +146,8 @@ public:
|
|||||||
static bool hasCompressedTextureSupport(image::CompressedData::Format format);
|
static bool hasCompressedTextureSupport(image::CompressedData::Format format);
|
||||||
static bool hasSRGBSupport();
|
static bool hasSRGBSupport();
|
||||||
|
|
||||||
static bool getConstant(const char *in, Format &out);
|
static bool getConstant(const char *in, FlagType &out);
|
||||||
static bool getConstant(Format in, const char *&out);
|
static bool getConstant(FlagType in, const char *&out);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
@@ -162,14 +169,11 @@ private:
|
|||||||
// Mipmap texture LOD bias (sharpness) value.
|
// Mipmap texture LOD bias (sharpness) value.
|
||||||
float mipmapSharpness;
|
float mipmapSharpness;
|
||||||
|
|
||||||
// True if mipmaps have been created for this Image.
|
|
||||||
bool mipmapsCreated;
|
|
||||||
|
|
||||||
// Whether this Image is using a compressed texture.
|
// Whether this Image is using a compressed texture.
|
||||||
bool compressed;
|
bool compressed;
|
||||||
|
|
||||||
// The format to interpret the image's data as.
|
// The flags used to initialize this Image.
|
||||||
Format format;
|
Flags flags;
|
||||||
|
|
||||||
// True if the image wasn't able to be properly created and it had to fall
|
// True if the image wasn't able to be properly created and it had to fall
|
||||||
// back to a default texture.
|
// back to a default texture.
|
||||||
@@ -177,12 +181,10 @@ private:
|
|||||||
|
|
||||||
void preload();
|
void preload();
|
||||||
|
|
||||||
|
void uploadCompressedData();
|
||||||
|
void uploadImageData();
|
||||||
void uploadTexture();
|
void uploadTexture();
|
||||||
|
|
||||||
void uploadCompressedMipmaps();
|
|
||||||
void createMipmaps();
|
|
||||||
void checkMipmapsCreated();
|
|
||||||
|
|
||||||
static float maxMipmapSharpness;
|
static float maxMipmapSharpness;
|
||||||
|
|
||||||
static FilterMode defaultMipmapFilter;
|
static FilterMode defaultMipmapFilter;
|
||||||
@@ -190,8 +192,8 @@ private:
|
|||||||
|
|
||||||
GLenum getCompressedFormat(image::CompressedData::Format cformat) const;
|
GLenum getCompressedFormat(image::CompressedData::Format cformat) const;
|
||||||
|
|
||||||
static StringMap<Format, FORMAT_MAX_ENUM>::Entry formatEntries[];
|
static StringMap<FlagType, FLAG_TYPE_MAX_ENUM>::Entry flagNameEntries[];
|
||||||
static StringMap<Format, FORMAT_MAX_ENUM> formats;
|
static StringMap<FlagType, FLAG_TYPE_MAX_ENUM> flagNames;
|
||||||
|
|
||||||
}; // Image
|
}; // Image
|
||||||
|
|
||||||
|
|||||||
@@ -146,16 +146,24 @@ int w_setInvertedStencil(lua_State *L)
|
|||||||
return setStencil(L, true);
|
return setStencil(L, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const char *imageFlagName(Image::FlagType flagtype)
|
||||||
|
{
|
||||||
|
const char *name = nullptr;
|
||||||
|
Image::getConstant(flagtype, name);
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
int w_newImage(lua_State *L)
|
int w_newImage(lua_State *L)
|
||||||
{
|
{
|
||||||
love::image::ImageData *data = nullptr;
|
love::image::ImageData *data = nullptr;
|
||||||
love::image::CompressedData *cdata = nullptr;
|
love::image::CompressedData *cdata = nullptr;
|
||||||
|
|
||||||
Image::Format format = Image::FORMAT_NORMAL;
|
Image::Flags flags;
|
||||||
const char *fstr = lua_isnoneornil(L, 2) ? nullptr : luaL_checkstring(L, 2);
|
if (!lua_isnoneornil(L, 2))
|
||||||
|
{
|
||||||
if (fstr != nullptr && !Image::getConstant(fstr, format))
|
flags.mipmaps = luax_boolflag(L, 2, imageFlagName(Image::FLAG_TYPE_MIPMAPS), flags.mipmaps);
|
||||||
return luaL_error(L, "Invalid Image format: %s", fstr);
|
flags.sRGB = luax_boolflag(L, 2, imageFlagName(Image::FLAG_TYPE_SRGB), flags.sRGB);
|
||||||
|
}
|
||||||
|
|
||||||
bool releasedata = false;
|
bool releasedata = false;
|
||||||
|
|
||||||
@@ -199,9 +207,9 @@ int w_newImage(lua_State *L)
|
|||||||
luax_catchexcept(L,
|
luax_catchexcept(L,
|
||||||
[&]() {
|
[&]() {
|
||||||
if (cdata)
|
if (cdata)
|
||||||
image = instance->newImage(cdata, format);
|
image = instance->newImage(cdata, flags);
|
||||||
else if (data)
|
else if (data)
|
||||||
image = instance->newImage(data, format);
|
image = instance->newImage(data, flags);
|
||||||
},
|
},
|
||||||
[&]() {
|
[&]() {
|
||||||
if (releasedata && data)
|
if (releasedata && data)
|
||||||
|
|||||||
@@ -48,9 +48,7 @@ int w_Image_setMipmapFilter(lua_State *L)
|
|||||||
}
|
}
|
||||||
|
|
||||||
luax_catchexcept(L, [&](){ t->setFilter(f); });
|
luax_catchexcept(L, [&](){ t->setFilter(f); });
|
||||||
|
t->setMipmapSharpness((float) luaL_optnumber(L, 3, 0.0));
|
||||||
float sharpness = (float) luaL_optnumber(L, 3, 0);
|
|
||||||
t->setMipmapSharpness(sharpness);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user