mirror of
https://github.com/love2d/love.git
synced 2026-08-14 10:13:46 +02:00
Merge branch '12.0' into GraphicsBuffer
This commit is contained in:
+651
-142
@@ -33,42 +33,264 @@ namespace love
|
||||
namespace graphics
|
||||
{
|
||||
|
||||
love::Type Texture::type("Texture", &Drawable::type);
|
||||
uint64 SamplerState::toKey() const
|
||||
{
|
||||
union { float f; uint32 i; } conv;
|
||||
conv.f = lodBias;
|
||||
|
||||
Texture::Filter Texture::defaultFilter;
|
||||
Texture::FilterMode Texture::defaultMipmapFilter = Texture::FILTER_LINEAR;
|
||||
float Texture::defaultMipmapSharpness = 0.0f;
|
||||
return (minFilter << 0) | (magFilter << 1) | (mipmapFilter << 2)
|
||||
| (wrapU << 4) | (wrapV << 7) | (wrapW << 10)
|
||||
| (maxAnisotropy << 12) | (minLod << 16) | (maxLod << 20)
|
||||
| (depthSampleMode.hasValue << 24) | (depthSampleMode.value << 25)
|
||||
| ((uint64)conv.i << 32);
|
||||
}
|
||||
|
||||
SamplerState SamplerState::fromKey(uint64 key)
|
||||
{
|
||||
const uint32 BITS_1 = 0x1;
|
||||
const uint32 BITS_2 = 0x3;
|
||||
const uint32 BITS_3 = 0x7;
|
||||
const uint32 BITS_4 = 0xF;
|
||||
|
||||
SamplerState s;
|
||||
|
||||
s.minFilter = (FilterMode) ((key >> 0) & BITS_1);
|
||||
s.magFilter = (FilterMode) ((key >> 1) & BITS_1);
|
||||
s.mipmapFilter = (MipmapFilterMode) ((key >> 2) & BITS_2);
|
||||
|
||||
s.wrapU = (WrapMode) ((key >> 4 ) & BITS_3);
|
||||
s.wrapV = (WrapMode) ((key >> 7 ) & BITS_3);
|
||||
s.wrapW = (WrapMode) ((key >> 10) & BITS_3);
|
||||
|
||||
s.maxAnisotropy = (key >> 12) & BITS_4;
|
||||
|
||||
s.minLod = (key >> 16) & BITS_4;
|
||||
s.maxLod = (key >> 20) & BITS_4;
|
||||
|
||||
s.depthSampleMode.hasValue = ((key >> 24) & BITS_1) != 0;
|
||||
s.depthSampleMode.value = (CompareMode) ((key >> 25) & BITS_4);
|
||||
|
||||
union { float f; uint32 i; } conv;
|
||||
conv.i = (uint32) (key >> 32);
|
||||
s.lodBias = conv.f;
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
bool SamplerState::isClampZeroOrOne(WrapMode w)
|
||||
{
|
||||
return w == WRAP_CLAMP_ONE || w == WRAP_CLAMP_ZERO;
|
||||
}
|
||||
|
||||
static StringMap<SamplerState::FilterMode, SamplerState::FILTER_MAX_ENUM>::Entry filterModeEntries[] =
|
||||
{
|
||||
{ "linear", SamplerState::FILTER_LINEAR },
|
||||
{ "nearest", SamplerState::FILTER_NEAREST },
|
||||
};
|
||||
|
||||
static StringMap<SamplerState::FilterMode, SamplerState::FILTER_MAX_ENUM> filterModes(filterModeEntries, sizeof(filterModeEntries));
|
||||
|
||||
static StringMap<SamplerState::MipmapFilterMode, SamplerState::MIPMAP_FILTER_MAX_ENUM>::Entry mipmapFilterModeEntries[] =
|
||||
{
|
||||
{ "none", SamplerState::MIPMAP_FILTER_NONE },
|
||||
{ "linear", SamplerState::MIPMAP_FILTER_LINEAR },
|
||||
{ "nearest", SamplerState::MIPMAP_FILTER_NEAREST },
|
||||
};
|
||||
|
||||
static StringMap<SamplerState::MipmapFilterMode, SamplerState::MIPMAP_FILTER_MAX_ENUM> mipmapFilterModes(mipmapFilterModeEntries, sizeof(mipmapFilterModeEntries));
|
||||
|
||||
static StringMap<SamplerState::WrapMode, SamplerState::WRAP_MAX_ENUM>::Entry wrapModeEntries[] =
|
||||
{
|
||||
{ "clamp", SamplerState::WRAP_CLAMP },
|
||||
{ "clampzero", SamplerState::WRAP_CLAMP_ZERO },
|
||||
{ "clampone", SamplerState::WRAP_CLAMP_ONE },
|
||||
{ "repeat", SamplerState::WRAP_REPEAT },
|
||||
{ "mirroredrepeat", SamplerState::WRAP_MIRRORED_REPEAT },
|
||||
};
|
||||
|
||||
static StringMap<SamplerState::WrapMode, SamplerState::WRAP_MAX_ENUM> wrapModes(wrapModeEntries, sizeof(wrapModeEntries));
|
||||
|
||||
bool SamplerState::getConstant(const char *in, FilterMode &out)
|
||||
{
|
||||
return filterModes.find(in, out);
|
||||
}
|
||||
|
||||
bool SamplerState::getConstant(FilterMode in, const char *&out)
|
||||
{
|
||||
return filterModes.find(in, out);
|
||||
}
|
||||
|
||||
std::vector<std::string> SamplerState::getConstants(FilterMode)
|
||||
{
|
||||
return filterModes.getNames();
|
||||
}
|
||||
|
||||
bool SamplerState::getConstant(const char *in, MipmapFilterMode &out)
|
||||
{
|
||||
return mipmapFilterModes.find(in, out);
|
||||
}
|
||||
|
||||
bool SamplerState::getConstant(MipmapFilterMode in, const char *&out)
|
||||
{
|
||||
return mipmapFilterModes.find(in, out);
|
||||
}
|
||||
|
||||
std::vector<std::string> SamplerState::getConstants(MipmapFilterMode)
|
||||
{
|
||||
return mipmapFilterModes.getNames();
|
||||
}
|
||||
|
||||
bool SamplerState::getConstant(const char *in, WrapMode &out)
|
||||
{
|
||||
return wrapModes.find(in, out);
|
||||
}
|
||||
|
||||
bool SamplerState::getConstant(WrapMode in, const char *&out)
|
||||
{
|
||||
return wrapModes.find(in, out);
|
||||
}
|
||||
|
||||
std::vector<std::string> SamplerState::getConstants(WrapMode)
|
||||
{
|
||||
return wrapModes.getNames();
|
||||
}
|
||||
|
||||
love::Type Texture::type("Texture", &Drawable::type);
|
||||
int Texture::textureCount = 0;
|
||||
int64 Texture::totalGraphicsMemory = 0;
|
||||
|
||||
Texture::Texture(TextureType texType)
|
||||
: texType(texType)
|
||||
, format(PIXELFORMAT_UNKNOWN)
|
||||
Texture::Texture(const Settings &settings, const Slices *slices)
|
||||
: texType(settings.type)
|
||||
, format(settings.format)
|
||||
, renderTarget(settings.renderTarget)
|
||||
, readable(true)
|
||||
, width(0)
|
||||
, height(0)
|
||||
, depth(1)
|
||||
, layers(1)
|
||||
, mipmapsMode(settings.mipmaps)
|
||||
, sRGB(isGammaCorrect() && !settings.linear)
|
||||
, width(settings.width)
|
||||
, height(settings.height)
|
||||
, depth(settings.type == TEXTURE_VOLUME ? settings.layers : 1)
|
||||
, layers(settings.type == TEXTURE_2D_ARRAY ? settings.layers : 1)
|
||||
, mipmapCount(1)
|
||||
, pixelWidth(0)
|
||||
, pixelHeight(0)
|
||||
, filter(defaultFilter)
|
||||
, wrap()
|
||||
, mipmapSharpness(defaultMipmapSharpness)
|
||||
, requestedMSAA(settings.msaa)
|
||||
, samplerState()
|
||||
, graphicsMemorySize(0)
|
||||
, usingDefaultTexture(false)
|
||||
{
|
||||
auto gfx = Module::getInstance<Graphics>(Module::M_GRAPHICS);
|
||||
|
||||
if (slices != nullptr && slices->getMipmapCount() > 0 && slices->getSliceCount() > 0)
|
||||
{
|
||||
texType = slices->getTextureType();
|
||||
|
||||
if (requestedMSAA > 1)
|
||||
throw love::Exception("MSAA textures cannot be created from image data.");
|
||||
|
||||
int dataMipmaps = 1;
|
||||
if (slices->validate() && slices->getMipmapCount() > 1)
|
||||
dataMipmaps = slices->getMipmapCount();
|
||||
|
||||
love::image::ImageDataBase *slice = slices->get(0, 0);
|
||||
|
||||
format = slice->getFormat();
|
||||
|
||||
pixelWidth = slice->getWidth();
|
||||
pixelHeight = slice->getHeight();
|
||||
|
||||
if (texType == TEXTURE_2D_ARRAY)
|
||||
layers = slices->getSliceCount();
|
||||
else if (texType == TEXTURE_VOLUME)
|
||||
depth = slices->getSliceCount();
|
||||
|
||||
width = (int) (pixelWidth / settings.dpiScale + 0.5);
|
||||
height = (int) (pixelHeight / settings.dpiScale + 0.5);
|
||||
|
||||
if (isCompressed() && dataMipmaps <= 1)
|
||||
mipmapsMode = MIPMAPS_NONE;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (isCompressed())
|
||||
throw love::Exception("Compressed textures must be created with initial data.");
|
||||
|
||||
pixelWidth = (int) ((width * settings.dpiScale) + 0.5);
|
||||
pixelHeight = (int) ((height * settings.dpiScale) + 0.5);
|
||||
}
|
||||
|
||||
if (settings.readable.hasValue)
|
||||
readable = settings.readable.value;
|
||||
else
|
||||
readable = !renderTarget || !isPixelFormatDepthStencil(format);
|
||||
|
||||
format = gfx->getSizedFormat(format, renderTarget, readable, sRGB);
|
||||
|
||||
if (mipmapsMode == MIPMAPS_AUTO && isCompressed())
|
||||
mipmapsMode = MIPMAPS_MANUAL;
|
||||
|
||||
if (mipmapsMode != MIPMAPS_NONE)
|
||||
mipmapCount = getTotalMipmapCount(pixelWidth, pixelHeight, depth);
|
||||
|
||||
if (pixelWidth <= 0 || pixelHeight <= 0 || layers <= 0 || depth <= 0)
|
||||
throw love::Exception("Texture dimensions must be greater than 0.");
|
||||
|
||||
if (texType != TEXTURE_2D && requestedMSAA > 1)
|
||||
throw love::Exception("MSAA is only supported for textures with the 2D texture type.");
|
||||
|
||||
if (!renderTarget && requestedMSAA > 1)
|
||||
throw love::Exception("MSAA is only supported with render target textures.");
|
||||
|
||||
if (readable && isPixelFormatDepthStencil(format) && settings.msaa > 1)
|
||||
throw love::Exception("Readable depth/stencil textures with MSAA are not currently supported.");
|
||||
|
||||
if ((!readable || settings.msaa > 1) && mipmapsMode != MIPMAPS_NONE)
|
||||
throw love::Exception("Non-readable and MSAA textures cannot have mipmaps.");
|
||||
|
||||
if (!readable && texType != TEXTURE_2D)
|
||||
throw love::Exception("Non-readable pixel formats are only supported for 2D texture types.");
|
||||
|
||||
if (isCompressed() && renderTarget)
|
||||
throw love::Exception("Compressed textures cannot be render targets.");
|
||||
|
||||
if (!gfx->isPixelFormatSupported(format, renderTarget, readable, sRGB))
|
||||
{
|
||||
const char *fstr = "unknown";
|
||||
love::getConstant(format, fstr);
|
||||
|
||||
const char *readablestr = "";
|
||||
if (readable != !isPixelFormatDepthStencil(format))
|
||||
readablestr = readable ? " readable" : " non-readable";
|
||||
|
||||
const char *rtstr = "";
|
||||
if (renderTarget)
|
||||
rtstr = " as a render target";
|
||||
|
||||
throw love::Exception("The %s%s pixel format is not supported%s on this system.", fstr, readablestr, rtstr);
|
||||
}
|
||||
|
||||
if (!gfx->getCapabilities().textureTypes[texType])
|
||||
{
|
||||
const char *textypestr = "unknown";
|
||||
Texture::getConstant(texType, textypestr);
|
||||
throw love::Exception("%s textures are not supported on this system.", textypestr);
|
||||
}
|
||||
|
||||
validateDimensions(renderTarget || !readable);
|
||||
|
||||
samplerState = gfx->getDefaultSamplerState();
|
||||
|
||||
Quad::Viewport v = {0, 0, (double) width, (double) height};
|
||||
quad.set(new Quad(v, width, height), Acquire::NORETAIN);
|
||||
|
||||
++textureCount;
|
||||
}
|
||||
|
||||
Texture::~Texture()
|
||||
{
|
||||
--textureCount;
|
||||
setGraphicsMemorySize(0);
|
||||
}
|
||||
|
||||
void Texture::initQuad()
|
||||
{
|
||||
Quad::Viewport v = {0, 0, (double) width, (double) height};
|
||||
quad.set(new Quad(v, width, height), Acquire::NORETAIN);
|
||||
}
|
||||
|
||||
void Texture::setGraphicsMemorySize(int64 bytes)
|
||||
{
|
||||
totalGraphicsMemory = std::max(totalGraphicsMemory - graphicsMemorySize, (int64) 0);
|
||||
@@ -78,38 +300,6 @@ void Texture::setGraphicsMemorySize(int64 bytes)
|
||||
totalGraphicsMemory += bytes;
|
||||
}
|
||||
|
||||
TextureType Texture::getTextureType() const
|
||||
{
|
||||
return texType;
|
||||
}
|
||||
|
||||
PixelFormat Texture::getPixelFormat() const
|
||||
{
|
||||
return format;
|
||||
}
|
||||
|
||||
bool Texture::isReadable() const
|
||||
{
|
||||
return readable;
|
||||
}
|
||||
|
||||
bool Texture::isValidSlice(int slice) const
|
||||
{
|
||||
if (slice < 0)
|
||||
return false;
|
||||
|
||||
if (texType == TEXTURE_CUBE)
|
||||
return slice < 6;
|
||||
else if (texType == TEXTURE_VOLUME)
|
||||
return slice < depth;
|
||||
else if (texType == TEXTURE_2D_ARRAY)
|
||||
return slice < layers;
|
||||
else if (slice > 0)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Texture::draw(Graphics *gfx, const Matrix4 &m)
|
||||
{
|
||||
draw(gfx, quad, m);
|
||||
@@ -120,6 +310,9 @@ void Texture::draw(Graphics *gfx, Quad *q, const Matrix4 &localTransform)
|
||||
if (!readable)
|
||||
throw love::Exception("Textures with non-readable formats cannot be drawn.");
|
||||
|
||||
if (renderTarget && gfx->isRenderTargetActive(this))
|
||||
throw love::Exception("Cannot render a Texture to itself.");
|
||||
|
||||
if (texType == TEXTURE_2D_ARRAY)
|
||||
{
|
||||
drawLayer(gfx, q->getLayer(), q, localTransform);
|
||||
@@ -168,8 +361,11 @@ void Texture::drawLayer(Graphics *gfx, int layer, Quad *q, const Matrix4 &m)
|
||||
if (!readable)
|
||||
throw love::Exception("Textures with non-readable formats cannot be drawn.");
|
||||
|
||||
if (renderTarget && gfx->isRenderTargetActive(this, layer))
|
||||
throw love::Exception("Cannot render a Texture to itself.");
|
||||
|
||||
if (texType != TEXTURE_2D_ARRAY)
|
||||
throw love::Exception("drawLayer can only be used with Array Textures!");
|
||||
throw love::Exception("drawLayer can only be used with Array Textures.");
|
||||
|
||||
if (layer < 0 || layer >= layers)
|
||||
throw love::Exception("Invalid layer: %d (Texture has %d layers)", layer + 1, layers);
|
||||
@@ -208,6 +404,176 @@ void Texture::drawLayer(Graphics *gfx, int layer, Quad *q, const Matrix4 &m)
|
||||
}
|
||||
}
|
||||
|
||||
void Texture::uploadImageData(love::image::ImageDataBase *d, int level, int slice, int x, int y)
|
||||
{
|
||||
love::image::ImageData *id = dynamic_cast<love::image::ImageData *>(d);
|
||||
|
||||
love::thread::EmptyLock lock;
|
||||
if (id != nullptr)
|
||||
lock.setLock(id->getMutex());
|
||||
|
||||
Rect rect = {x, y, d->getWidth(), d->getHeight()};
|
||||
uploadByteData(d->getFormat(), d->getData(), d->getSize(), level, slice, rect, d);
|
||||
}
|
||||
|
||||
void Texture::replacePixels(love::image::ImageDataBase *d, int slice, int mipmap, int x, int y, bool reloadmipmaps)
|
||||
{
|
||||
if (!isReadable())
|
||||
throw love::Exception("replacePixels can only be called on readable Textures.");
|
||||
|
||||
if (getMSAA() > 1)
|
||||
throw love::Exception("replacePixels cannot be called on a MSAA Texture.");
|
||||
|
||||
auto gfx = Module::getInstance<Graphics>(Module::M_GRAPHICS);
|
||||
if (gfx != nullptr && gfx->isRenderTargetActive(this))
|
||||
throw love::Exception("replacePixels cannot be called on this Texture while it's an active render target.");
|
||||
|
||||
// No effect if the texture hasn't been created yet.
|
||||
if (getHandle() == 0 || usingDefaultTexture)
|
||||
return;
|
||||
|
||||
if (d->getFormat() != getPixelFormat())
|
||||
throw love::Exception("Pixel formats must match.");
|
||||
|
||||
if (mipmap < 0 || mipmap >= getMipmapCount())
|
||||
throw love::Exception("Invalid texture mipmap index %d.", mipmap + 1);
|
||||
|
||||
if (slice < 0 || (texType == TEXTURE_CUBE && slice >= 6)
|
||||
|| (texType == TEXTURE_VOLUME && slice >= getDepth(mipmap))
|
||||
|| (texType == TEXTURE_2D_ARRAY && slice >= getLayerCount()))
|
||||
{
|
||||
throw love::Exception("Invalid texture slice index %d.", slice + 1);
|
||||
}
|
||||
|
||||
Rect rect = {x, y, d->getWidth(), d->getHeight()};
|
||||
|
||||
int mipw = getPixelWidth(mipmap);
|
||||
int miph = getPixelHeight(mipmap);
|
||||
|
||||
if (rect.x < 0 || rect.y < 0 || rect.w <= 0 || rect.h <= 0
|
||||
|| (rect.x + rect.w) > mipw || (rect.y + rect.h) > miph)
|
||||
{
|
||||
throw love::Exception("Invalid rectangle dimensions (x=%d, y=%d, w=%d, h=%d) for %dx%d Texture.", rect.x, rect.y, rect.w, rect.h, mipw, miph);
|
||||
}
|
||||
|
||||
// We don't currently support partial updates of compressed textures.
|
||||
if (isPixelFormatCompressed(d->getFormat()) && (rect.x != 0 || rect.y != 0 || rect.w != mipw || rect.h != miph))
|
||||
throw love::Exception("Compressed textures only support replacing the entire Texture.");
|
||||
|
||||
Graphics::flushStreamDrawsGlobal();
|
||||
|
||||
uploadImageData(d, mipmap, slice, x, y);
|
||||
|
||||
if (reloadmipmaps && mipmap == 0 && getMipmapCount() > 1)
|
||||
generateMipmaps();
|
||||
}
|
||||
|
||||
void Texture::replacePixels(const void *data, size_t size, int slice, int mipmap, const Rect &rect, bool reloadmipmaps)
|
||||
{
|
||||
if (!isReadable() || getMSAA() > 1)
|
||||
return;
|
||||
|
||||
auto gfx = Module::getInstance<Graphics>(Module::M_GRAPHICS);
|
||||
if (gfx != nullptr && gfx->isRenderTargetActive(this))
|
||||
return;
|
||||
|
||||
Graphics::flushStreamDrawsGlobal();
|
||||
|
||||
uploadByteData(format, data, size, mipmap, slice, rect, nullptr);
|
||||
|
||||
if (reloadmipmaps && mipmap == 0 && getMipmapCount() > 1)
|
||||
generateMipmaps();
|
||||
}
|
||||
|
||||
love::image::ImageData *Texture::newImageData(love::image::Image *module, int slice, int mipmap, const Rect &r)
|
||||
{
|
||||
if (!isReadable())
|
||||
throw love::Exception("Texture:newImageData cannot be called on non-readable Textures.");
|
||||
|
||||
if (!isRenderTarget())
|
||||
throw love::Exception("Texture:newImageData can only be called on render target Textures.");
|
||||
|
||||
if (isPixelFormatDepthStencil(getPixelFormat()))
|
||||
throw love::Exception("Texture:newImageData cannot be called on Textures with depth/stencil pixel formats.");
|
||||
|
||||
if (r.x < 0 || r.y < 0 || r.w <= 0 || r.h <= 0 || (r.x + r.w) > getPixelWidth(mipmap) || (r.y + r.h) > getPixelHeight(mipmap))
|
||||
throw love::Exception("Invalid rectangle dimensions.");
|
||||
|
||||
if (slice < 0 || (texType == TEXTURE_VOLUME && slice >= getDepth(mipmap))
|
||||
|| (texType == TEXTURE_2D_ARRAY && slice >= layers)
|
||||
|| (texType == TEXTURE_CUBE && slice >= 6))
|
||||
{
|
||||
throw love::Exception("Invalid slice index.");
|
||||
}
|
||||
|
||||
Graphics *gfx = Module::getInstance<Graphics>(Module::M_GRAPHICS);
|
||||
if (gfx != nullptr && gfx->isRenderTargetActive(this))
|
||||
throw love::Exception("Texture:newImageData cannot be called while that Texture is an active render target.");
|
||||
|
||||
PixelFormat dataformat = getLinearPixelFormat(getPixelFormat());
|
||||
|
||||
if (!image::ImageData::validPixelFormat(dataformat))
|
||||
{
|
||||
const char *formatname = "unknown";
|
||||
love::getConstant(dataformat, formatname);
|
||||
throw love::Exception("ImageData with the '%s' pixel format is not supported.", formatname);
|
||||
}
|
||||
|
||||
return module->newImageData(r.w, r.h, dataformat);
|
||||
}
|
||||
|
||||
TextureType Texture::getTextureType() const
|
||||
{
|
||||
return texType;
|
||||
}
|
||||
|
||||
PixelFormat Texture::getPixelFormat() const
|
||||
{
|
||||
return format;
|
||||
}
|
||||
|
||||
Texture::MipmapsMode Texture::getMipmapsMode() const
|
||||
{
|
||||
return mipmapsMode;
|
||||
}
|
||||
|
||||
bool Texture::isRenderTarget() const
|
||||
{
|
||||
return renderTarget;
|
||||
}
|
||||
|
||||
bool Texture::isReadable() const
|
||||
{
|
||||
return readable;
|
||||
}
|
||||
|
||||
bool Texture::isCompressed() const
|
||||
{
|
||||
return isPixelFormatCompressed(format);
|
||||
}
|
||||
|
||||
bool Texture::isFormatLinear() const
|
||||
{
|
||||
return isGammaCorrect() && !sRGB && format != PIXELFORMAT_sRGBA8_UNORM;
|
||||
}
|
||||
|
||||
bool Texture::isValidSlice(int slice) const
|
||||
{
|
||||
if (slice < 0)
|
||||
return false;
|
||||
|
||||
if (texType == TEXTURE_CUBE)
|
||||
return slice < 6;
|
||||
else if (texType == TEXTURE_VOLUME)
|
||||
return slice < depth;
|
||||
else if (texType == TEXTURE_2D_ARRAY)
|
||||
return slice < layers;
|
||||
else if (slice > 0)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int Texture::getWidth(int mip) const
|
||||
{
|
||||
return std::max(width >> mip, 1);
|
||||
@@ -248,45 +614,33 @@ float Texture::getDPIScale() const
|
||||
return (float) pixelHeight / (float) height;
|
||||
}
|
||||
|
||||
void Texture::setFilter(const Filter &f)
|
||||
int Texture::getRequestedMSAA() const
|
||||
{
|
||||
if (!validateFilter(f, getMipmapCount() > 1))
|
||||
{
|
||||
if (f.mipmap != FILTER_NONE && getMipmapCount() == 1)
|
||||
throw love::Exception("Non-mipmapped texture cannot have mipmap filtering.");
|
||||
else
|
||||
throw love::Exception("Invalid texture filter.");
|
||||
}
|
||||
return requestedMSAA;
|
||||
}
|
||||
|
||||
void Texture::setSamplerState(const SamplerState &s)
|
||||
{
|
||||
if (!readable)
|
||||
return;
|
||||
|
||||
if (s.depthSampleMode.hasValue && !isPixelFormatDepth(format))
|
||||
throw love::Exception("Only depth textures can have a depth sample compare mode.");
|
||||
|
||||
Graphics::flushStreamDrawsGlobal();
|
||||
|
||||
filter = f;
|
||||
samplerState = s;
|
||||
|
||||
if (samplerState.mipmapFilter != SamplerState::MIPMAP_FILTER_NONE && getMipmapCount() == 1)
|
||||
samplerState.mipmapFilter = SamplerState::MIPMAP_FILTER_NONE;
|
||||
|
||||
if (texType == TEXTURE_CUBE)
|
||||
samplerState.wrapU = samplerState.wrapV = samplerState.wrapW = SamplerState::WRAP_CLAMP;
|
||||
}
|
||||
|
||||
const Texture::Filter &Texture::getFilter() const
|
||||
const SamplerState &Texture::getSamplerState() const
|
||||
{
|
||||
return filter;
|
||||
}
|
||||
|
||||
const Texture::Wrap &Texture::getWrap() const
|
||||
{
|
||||
return wrap;
|
||||
}
|
||||
|
||||
float Texture::getMipmapSharpness() const
|
||||
{
|
||||
return mipmapSharpness;
|
||||
}
|
||||
|
||||
void Texture::setDepthSampleMode(Optional<CompareMode> mode)
|
||||
{
|
||||
if (mode.hasValue && (!readable || !isPixelFormatDepthStencil(format)))
|
||||
throw love::Exception("Only readable depth textures can have a depth sample compare mode.");
|
||||
}
|
||||
|
||||
Optional<CompareMode> Texture::getDepthSampleMode() const
|
||||
{
|
||||
return depthCompareMode;
|
||||
return samplerState;
|
||||
}
|
||||
|
||||
Quad *Texture::getQuad() const
|
||||
@@ -294,23 +648,6 @@ Quad *Texture::getQuad() const
|
||||
return quad;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int Texture::getTotalMipmapCount(int w, int h)
|
||||
{
|
||||
return (int) log2(std::max(w, h)) + 1;
|
||||
@@ -378,6 +715,201 @@ bool Texture::validateDimensions(bool throwException) const
|
||||
return success;
|
||||
}
|
||||
|
||||
Texture::Slices::Slices(TextureType textype)
|
||||
: textureType(textype)
|
||||
{
|
||||
}
|
||||
|
||||
void Texture::Slices::clear()
|
||||
{
|
||||
data.clear();
|
||||
}
|
||||
|
||||
void Texture::Slices::set(int slice, int mipmap, love::image::ImageDataBase *d)
|
||||
{
|
||||
if (textureType == TEXTURE_VOLUME)
|
||||
{
|
||||
if (mipmap >= (int) data.size())
|
||||
data.resize(mipmap + 1);
|
||||
|
||||
if (slice >= (int) data[mipmap].size())
|
||||
data[mipmap].resize(slice + 1);
|
||||
|
||||
data[mipmap][slice].set(d);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (slice >= (int) data.size())
|
||||
data.resize(slice + 1);
|
||||
|
||||
if (mipmap >= (int) data[slice].size())
|
||||
data[slice].resize(mipmap + 1);
|
||||
|
||||
data[slice][mipmap].set(d);
|
||||
}
|
||||
}
|
||||
|
||||
love::image::ImageDataBase *Texture::Slices::get(int slice, int mipmap) const
|
||||
{
|
||||
if (slice < 0 || slice >= getSliceCount(mipmap))
|
||||
return nullptr;
|
||||
|
||||
if (mipmap < 0 || mipmap >= getMipmapCount(slice))
|
||||
return nullptr;
|
||||
|
||||
if (textureType == TEXTURE_VOLUME)
|
||||
return data[mipmap][slice].get();
|
||||
else
|
||||
return data[slice][mipmap].get();
|
||||
}
|
||||
|
||||
void Texture::Slices::add(love::image::CompressedImageData *cdata, int startslice, int startmip, bool addallslices, bool addallmips)
|
||||
{
|
||||
int slicecount = addallslices ? cdata->getSliceCount() : 1;
|
||||
int mipcount = addallmips ? cdata->getMipmapCount() : 1;
|
||||
|
||||
for (int mip = 0; mip < mipcount; mip++)
|
||||
{
|
||||
for (int slice = 0; slice < slicecount; slice++)
|
||||
set(startslice + slice, startmip + mip, cdata->getSlice(slice, mip));
|
||||
}
|
||||
}
|
||||
|
||||
int Texture::Slices::getSliceCount(int mip) const
|
||||
{
|
||||
if (textureType == TEXTURE_VOLUME)
|
||||
{
|
||||
if (mip < 0 || mip >= (int) data.size())
|
||||
return 0;
|
||||
|
||||
return (int) data[mip].size();
|
||||
}
|
||||
else
|
||||
return (int) data.size();
|
||||
}
|
||||
|
||||
int Texture::Slices::getMipmapCount(int slice) const
|
||||
{
|
||||
if (textureType == TEXTURE_VOLUME)
|
||||
return (int) data.size();
|
||||
else
|
||||
{
|
||||
if (slice < 0 || slice >= (int) data.size())
|
||||
return 0;
|
||||
|
||||
return data[slice].size();
|
||||
}
|
||||
}
|
||||
|
||||
bool Texture::Slices::validate() const
|
||||
{
|
||||
int slicecount = getSliceCount();
|
||||
int mipcount = getMipmapCount(0);
|
||||
|
||||
if (slicecount == 0 || mipcount == 0)
|
||||
throw love::Exception("At least one ImageData or CompressedImageData is required.");
|
||||
|
||||
if (textureType == TEXTURE_CUBE && slicecount != 6)
|
||||
throw love::Exception("Cube textures must have exactly 6 sides.");
|
||||
|
||||
image::ImageDataBase *firstdata = get(0, 0);
|
||||
|
||||
int w = firstdata->getWidth();
|
||||
int h = firstdata->getHeight();
|
||||
int depth = textureType == TEXTURE_VOLUME ? slicecount : 1;
|
||||
PixelFormat format = firstdata->getFormat();
|
||||
|
||||
int expectedmips = Texture::getTotalMipmapCount(w, h, depth);
|
||||
|
||||
if (mipcount != expectedmips && mipcount != 1)
|
||||
throw love::Exception("Texture does not have all required mipmap levels (expected %d, got %d)", expectedmips, mipcount);
|
||||
|
||||
if (textureType == TEXTURE_CUBE && w != h)
|
||||
throw love::Exception("Cube textures must have equal widths and heights for each cube face.");
|
||||
|
||||
int mipw = w;
|
||||
int miph = h;
|
||||
int mipslices = slicecount;
|
||||
|
||||
for (int mip = 0; mip < mipcount; mip++)
|
||||
{
|
||||
if (textureType == TEXTURE_VOLUME)
|
||||
{
|
||||
slicecount = getSliceCount(mip);
|
||||
|
||||
if (slicecount != mipslices)
|
||||
throw love::Exception("Invalid number of image data layers in mipmap level %d (expected %d, got %d)", mip+1, mipslices, slicecount);
|
||||
}
|
||||
|
||||
for (int slice = 0; slice < slicecount; slice++)
|
||||
{
|
||||
auto slicedata = get(slice, mip);
|
||||
|
||||
if (slicedata == nullptr)
|
||||
throw love::Exception("Missing image data (slice %d, mipmap level %d)", slice+1, mip+1);
|
||||
|
||||
int realw = slicedata->getWidth();
|
||||
int realh = slicedata->getHeight();
|
||||
|
||||
if (getMipmapCount(slice) != mipcount)
|
||||
throw love::Exception("All texture layers must have the same mipmap count.");
|
||||
|
||||
if (mipw != realw)
|
||||
throw love::Exception("Width of image data (slice %d, mipmap level %d) is incorrect (expected %d, got %d)", slice+1, mip+1, mipw, realw);
|
||||
|
||||
if (miph != realh)
|
||||
throw love::Exception("Height of image data (slice %d, mipmap level %d) is incorrect (expected %d, got %d)", slice+1, mip+1, miph, realh);
|
||||
|
||||
if (format != slicedata->getFormat())
|
||||
throw love::Exception("All texture slices and mipmaps must have the same pixel format.");
|
||||
}
|
||||
|
||||
mipw = std::max(mipw / 2, 1);
|
||||
miph = std::max(miph / 2, 1);
|
||||
|
||||
if (textureType == TEXTURE_VOLUME)
|
||||
mipslices = std::max(mipslices / 2, 1);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static StringMap<TextureType, TEXTURE_MAX_ENUM>::Entry texTypeEntries[] =
|
||||
{
|
||||
{ "2d", TEXTURE_2D },
|
||||
{ "volume", TEXTURE_VOLUME },
|
||||
{ "array", TEXTURE_2D_ARRAY },
|
||||
{ "cube", TEXTURE_CUBE },
|
||||
};
|
||||
|
||||
static StringMap<TextureType, TEXTURE_MAX_ENUM> texTypes(texTypeEntries, sizeof(texTypeEntries));
|
||||
|
||||
static StringMap<Texture::MipmapsMode, Texture::MIPMAPS_MAX_ENUM>::Entry mipmapEntries[] =
|
||||
{
|
||||
{ "none", Texture::MIPMAPS_NONE },
|
||||
{ "manual", Texture::MIPMAPS_MANUAL },
|
||||
{ "auto", Texture::MIPMAPS_AUTO },
|
||||
};
|
||||
|
||||
static StringMap<Texture::MipmapsMode, Texture::MIPMAPS_MAX_ENUM> mipmapModes(mipmapEntries, sizeof(mipmapEntries));
|
||||
|
||||
static StringMap<Texture::SettingType, Texture::SETTING_MAX_ENUM>::Entry settingTypeEntries[] =
|
||||
{
|
||||
{ "width", Texture::SETTING_WIDTH },
|
||||
{ "height", Texture::SETTING_HEIGHT },
|
||||
{ "layers", Texture::SETTING_LAYERS },
|
||||
{ "mipmaps", Texture::SETTING_MIPMAPS },
|
||||
{ "format", Texture::SETTING_FORMAT },
|
||||
{ "linear", Texture::SETTING_LINEAR },
|
||||
{ "type", Texture::SETTING_TYPE },
|
||||
{ "dpiscale", Texture::SETTING_DPI_SCALE },
|
||||
{ "msaa", Texture::SETTING_MSAA },
|
||||
{ "canvas", Texture::SETTING_RENDER_TARGET },
|
||||
{ "readable", Texture::SETTING_READABLE },
|
||||
};
|
||||
|
||||
static StringMap<Texture::SettingType, Texture::SETTING_MAX_ENUM> settingTypes(settingTypeEntries, sizeof(settingTypeEntries));
|
||||
|
||||
bool Texture::getConstant(const char *in, TextureType &out)
|
||||
{
|
||||
return texTypes.find(in, out);
|
||||
@@ -393,65 +925,42 @@ std::vector<std::string> Texture::getConstants(TextureType)
|
||||
return texTypes.getNames();
|
||||
}
|
||||
|
||||
bool Texture::getConstant(const char *in, FilterMode &out)
|
||||
bool Texture::getConstant(const char *in, MipmapsMode &out)
|
||||
{
|
||||
return filterModes.find(in, out);
|
||||
return mipmapModes.find(in, out);
|
||||
}
|
||||
|
||||
bool Texture::getConstant(FilterMode in, const char *&out)
|
||||
bool Texture::getConstant(MipmapsMode in, const char *&out)
|
||||
{
|
||||
return filterModes.find(in, out);
|
||||
return mipmapModes.find(in, out);
|
||||
}
|
||||
|
||||
std::vector<std::string> Texture::getConstants(FilterMode)
|
||||
std::vector<std::string> Texture::getConstants(MipmapsMode)
|
||||
{
|
||||
return filterModes.getNames();
|
||||
return mipmapModes.getNames();
|
||||
}
|
||||
|
||||
bool Texture::getConstant(const char *in, WrapMode &out)
|
||||
bool Texture::getConstant(const char *in, SettingType &out)
|
||||
{
|
||||
return wrapModes.find(in, out);
|
||||
return settingTypes.find(in, out);
|
||||
}
|
||||
|
||||
bool Texture::getConstant(WrapMode in, const char *&out)
|
||||
bool Texture::getConstant(SettingType in, const char *&out)
|
||||
{
|
||||
return wrapModes.find(in, out);
|
||||
return settingTypes.find(in, out);
|
||||
}
|
||||
|
||||
std::vector<std::string> Texture::getConstants(WrapMode)
|
||||
const char *Texture::getConstant(SettingType in)
|
||||
{
|
||||
return wrapModes.getNames();
|
||||
const char *name = nullptr;
|
||||
getConstant(in, name);
|
||||
return name;
|
||||
}
|
||||
|
||||
StringMap<TextureType, TEXTURE_MAX_ENUM>::Entry Texture::texTypeEntries[] =
|
||||
std::vector<std::string> Texture::getConstants(SettingType)
|
||||
{
|
||||
{ "2d", TEXTURE_2D },
|
||||
{ "volume", TEXTURE_VOLUME },
|
||||
{ "array", TEXTURE_2D_ARRAY },
|
||||
{ "cube", TEXTURE_CUBE },
|
||||
};
|
||||
|
||||
StringMap<TextureType, TEXTURE_MAX_ENUM> Texture::texTypes(Texture::texTypeEntries, sizeof(Texture::texTypeEntries));
|
||||
|
||||
StringMap<Texture::FilterMode, Texture::FILTER_MAX_ENUM>::Entry Texture::filterModeEntries[] =
|
||||
{
|
||||
{ "linear", FILTER_LINEAR },
|
||||
{ "nearest", FILTER_NEAREST },
|
||||
{ "none", FILTER_NONE },
|
||||
};
|
||||
|
||||
StringMap<Texture::FilterMode, Texture::FILTER_MAX_ENUM> Texture::filterModes(Texture::filterModeEntries, sizeof(Texture::filterModeEntries));
|
||||
|
||||
StringMap<Texture::WrapMode, Texture::WRAP_MAX_ENUM>::Entry Texture::wrapModeEntries[] =
|
||||
{
|
||||
{ "clamp", WRAP_CLAMP },
|
||||
{ "clampzero", WRAP_CLAMP_ZERO },
|
||||
{ "clampone", WRAP_CLAMP_ONE },
|
||||
{ "repeat", WRAP_REPEAT },
|
||||
{ "mirroredrepeat", WRAP_MIRRORED_REPEAT },
|
||||
};
|
||||
|
||||
StringMap<Texture::WrapMode, Texture::WRAP_MAX_ENUM> Texture::wrapModes(Texture::wrapModeEntries, sizeof(Texture::wrapModeEntries));
|
||||
return settingTypes.getNames();
|
||||
}
|
||||
|
||||
} // graphics
|
||||
} // love
|
||||
|
||||
Reference in New Issue
Block a user