Refactor sampler state parameters for textures.

This commit is contained in:
Alex Szpakowski
2020-02-02 22:26:28 -04:00
parent f24b1aa377
commit f9248d478c
23 changed files with 473 additions and 585 deletions
+42 -41
View File
@@ -132,111 +132,111 @@ int w_Texture_getDPIScale(lua_State *L)
int w_Texture_setFilter(lua_State *L)
{
Texture *t = luax_checktexture(L, 1);
Texture::Filter f = t->getFilter();
SamplerState s = t->getSamplerState();
const char *minstr = luaL_checkstring(L, 2);
const char *magstr = luaL_optstring(L, 3, minstr);
if (!Texture::getConstant(minstr, f.min))
return luax_enumerror(L, "filter mode", Texture::getConstants(f.min), minstr);
if (!Texture::getConstant(magstr, f.mag))
return luax_enumerror(L, "filter mode", Texture::getConstants(f.mag), magstr);
if (!SamplerState::getConstant(minstr, s.minFilter))
return luax_enumerror(L, "filter mode", SamplerState::getConstants(s.minFilter), minstr);
if (!SamplerState::getConstant(magstr, s.magFilter))
return luax_enumerror(L, "filter mode", SamplerState::getConstants(s.magFilter), magstr);
f.anisotropy = (float) luaL_optnumber(L, 4, 1.0);
s.maxAnisotropy = std::min(std::max(1, (int) luaL_optnumber(L, 4, 1.0)), LOVE_UINT8_MAX);
luax_catchexcept(L, [&](){ t->setFilter(f); });
luax_catchexcept(L, [&](){ t->setSamplerState(s); });
return 0;
}
int w_Texture_getFilter(lua_State *L)
{
Texture *t = luax_checktexture(L, 1);
const Texture::Filter f = t->getFilter();
const SamplerState &s = t->getSamplerState();
const char *minstr = nullptr;
const char *magstr = nullptr;
if (!Texture::getConstant(f.min, minstr))
if (!SamplerState::getConstant(s.minFilter, minstr))
return luaL_error(L, "Unknown filter mode.");
if (!Texture::getConstant(f.mag, magstr))
if (!SamplerState::getConstant(s.magFilter, magstr))
return luaL_error(L, "Unknown filter mode.");
lua_pushstring(L, minstr);
lua_pushstring(L, magstr);
lua_pushnumber(L, f.anisotropy);
lua_pushnumber(L, s.maxAnisotropy);
return 3;
}
int w_Texture_setMipmapFilter(lua_State *L)
{
Texture *t = luax_checktexture(L, 1);
Texture::Filter f = t->getFilter();
SamplerState s = t->getSamplerState();
// Mipmapping is disabled if no argument is given.
if (lua_isnoneornil(L, 2))
f.mipmap = Texture::FILTER_NONE; // mipmapping is disabled if no argument is given
s.mipmapFilter = SamplerState::MIPMAP_FILTER_NONE;
else
{
const char *mipmapstr = luaL_checkstring(L, 2);
if (!Texture::getConstant(mipmapstr, f.mipmap))
return luax_enumerror(L, "filter mode", Texture::getConstants(f.mipmap), mipmapstr);
if (!SamplerState::getConstant(mipmapstr, s.mipmapFilter))
return luax_enumerror(L, "filter mode", SamplerState::getConstants(s.mipmapFilter), mipmapstr);
}
luax_catchexcept(L, [&](){ t->setFilter(f); });
t->setMipmapSharpness((float) luaL_optnumber(L, 3, 0.0));
s.lodBias = -((float) luaL_optnumber(L, 3, 0.0));
luax_catchexcept(L, [&](){ t->setSamplerState(s); });
return 0;
}
int w_Texture_getMipmapFilter(lua_State *L)
{
Texture *t = luax_checktexture(L, 1);
const Texture::Filter &f = t->getFilter();
const SamplerState &s = t->getSamplerState();
const char *mipmapstr;
if (Texture::getConstant(f.mipmap, mipmapstr))
if (SamplerState::getConstant(s.mipmapFilter, mipmapstr))
lua_pushstring(L, mipmapstr);
else
lua_pushnil(L); // only return a mipmap filter if mipmapping is enabled
lua_pushnumber(L, t->getMipmapSharpness());
lua_pushnumber(L, -s.lodBias);
return 2;
}
int w_Texture_setWrap(lua_State *L)
{
Texture *t = luax_checktexture(L, 1);
Texture::Wrap w;
SamplerState s = t->getSamplerState();
const char *sstr = luaL_checkstring(L, 2);
const char *tstr = luaL_optstring(L, 3, sstr);
const char *rstr = luaL_optstring(L, 4, sstr);
if (!Texture::getConstant(sstr, w.s))
return luax_enumerror(L, "wrap mode", Texture::getConstants(w.s), sstr);
if (!Texture::getConstant(tstr, w.t))
return luax_enumerror(L, "wrap mode", Texture::getConstants(w.t), tstr);
if (!Texture::getConstant(rstr, w.r))
return luax_enumerror(L, "wrap mode", Texture::getConstants(w.r), rstr);
if (!SamplerState::getConstant(sstr, s.wrapU))
return luax_enumerror(L, "wrap mode", SamplerState::getConstants(s.wrapU), sstr);
if (!SamplerState::getConstant(tstr, s.wrapV))
return luax_enumerror(L, "wrap mode", SamplerState::getConstants(s.wrapV), tstr);
if (!SamplerState::getConstant(rstr, s.wrapW))
return luax_enumerror(L, "wrap mode", SamplerState::getConstants(s.wrapW), rstr);
luax_pushboolean(L, t->setWrap(w));
luax_catchexcept(L, [&](){ t->setSamplerState(s); });
return 1;
}
int w_Texture_getWrap(lua_State *L)
{
Texture *t = luax_checktexture(L, 1);
const Texture::Wrap w = t->getWrap();
const SamplerState &s = t->getSamplerState();
const char *sstr = nullptr;
const char *tstr = nullptr;
const char *rstr = nullptr;
if (!Texture::getConstant(w.s, sstr))
if (!SamplerState::getConstant(s.wrapU, sstr))
return luaL_error(L, "Unknown wrap mode.");
if (!Texture::getConstant(w.t, tstr))
if (!SamplerState::getConstant(s.wrapV, tstr))
return luaL_error(L, "Unknown wrap mode.");
if (!Texture::getConstant(w.r, rstr))
if (!SamplerState::getConstant(s.wrapW, rstr))
return luaL_error(L, "Unknown wrap mode.");
lua_pushstring(L, sstr);
@@ -267,30 +267,31 @@ int w_Texture_isReadable(lua_State *L)
int w_Texture_setDepthSampleMode(lua_State *L)
{
Texture *t = luax_checktexture(L, 1);
SamplerState s = t->getSamplerState();
Optional<CompareMode> mode;
s.depthSampleMode.hasValue = false;
if (!lua_isnoneornil(L, 2))
{
const char *str = luaL_checkstring(L, 2);
mode.hasValue = true;
if (!getConstant(str, mode.value))
return luax_enumerror(L, "compare mode", getConstants(mode.value), str);
s.depthSampleMode.hasValue = true;
if (!getConstant(str, s.depthSampleMode.value))
return luax_enumerror(L, "compare mode", getConstants(s.depthSampleMode.value), str);
}
luax_catchexcept(L, [&]() { t->setDepthSampleMode(mode); });
luax_catchexcept(L, [&](){ t->setSamplerState(s); });
return 0;
}
int w_Texture_getDepthSampleMode(lua_State *L)
{
Texture *t = luax_checktexture(L, 1);
Optional<CompareMode> mode = t->getDepthSampleMode();
const SamplerState &s = t->getSamplerState();
if (mode.hasValue)
if (s.depthSampleMode.hasValue)
{
const char *str = nullptr;
if (!getConstant(mode.value, str))
if (!getConstant(s.depthSampleMode.value, str))
return luaL_error(L, "Unknown compare mode.");
lua_pushstring(L, str);
}