Use reserveGlobalTextureUnits for shadow maps

This commit is contained in:
AnyOldName3 2023-02-09 01:32:48 +00:00
parent 9be3d2668a
commit 7d4410d4fb
4 changed files with 20 additions and 25 deletions

View file

@ -636,31 +636,24 @@ namespace Shader
program->addShader(linkedShader);
}
int ShaderManager::reserveGlobalTextureUnits(Slot slot)
int ShaderManager::reserveGlobalTextureUnits(Slot slot, int count)
{
int unit = mReservedTextureUnitsBySlot[static_cast<int>(slot)];
if (unit >= 0)
return unit;
// TODO: Reuse units when count increase forces reallocation
// TODO: Warn if trampling on the ~8 units needed by model textures
auto unit = mReservedTextureUnitsBySlot[static_cast<int>(slot)];
if (unit.index >= 0 && unit.count >= count)
return unit.index;
{
// Texture units from `8 - numberOfShadowMaps` to `8` are used for shadows, so we skip them here.
// TODO: Maybe instead of fixed texture units use `reserveGlobalTextureUnits` for shadows as well.
static const int numberOfShadowMaps = Settings::Manager::getBool("enable shadows", "Shadows")
? std::clamp(Settings::Manager::getInt("number of shadow maps", "Shadows"), 1, 8)
: 0;
if (getAvailableTextureUnits() >= 8 && getAvailableTextureUnits() - 1 < 8)
mReservedTextureUnits = mMaxTextureUnits - (8 - numberOfShadowMaps);
}
if (getAvailableTextureUnits() < 2)
if (getAvailableTextureUnits() < count + 1)
throw std::runtime_error("Can't reserve texture unit; no available units");
mReservedTextureUnits++;
mReservedTextureUnits += count;
unit = mMaxTextureUnits - mReservedTextureUnits;
unit.index = mMaxTextureUnits - mReservedTextureUnits;
unit.count = count;
mReservedTextureUnitsBySlot[static_cast<int>(slot)] = unit;
return unit;
return unit.index;
}
void ShaderManager::update(osgViewer::Viewer& viewer)

View file

@ -76,9 +76,11 @@ namespace Shader
{
OpaqueDepthTexture,
SkyTexture,
ShadowMaps,
SLOT_COUNT
};
int reserveGlobalTextureUnits(Slot slot);
int reserveGlobalTextureUnits(Slot slot, int count = 1);
void update(osgViewer::Viewer& viewer);
void setHotReloadEnabled(bool value);
@ -116,7 +118,8 @@ namespace Shader
int mMaxTextureUnits = 0;
int mReservedTextureUnits = 0;
std::unique_ptr<HotReloadManager> mHotReloadManager;
std::array<int, 2> mReservedTextureUnitsBySlot = { -1, -1 };
struct ReservedTextureUnits { int index = -1; int count = 0; };
std::array<ReservedTextureUnits, static_cast<int>(Slot::SLOT_COUNT) > mReservedTextureUnitsBySlot = {};
};
bool parseForeachDirective(std::string& source, const std::string& templateName, size_t foundPos);