From 03f364e1317a718cf985df8b4ed0a5cc5af6129d Mon Sep 17 00:00:00 2001 From: Sasha Szpakowski Date: Fri, 3 May 2024 20:56:13 -0300 Subject: [PATCH] graphics: bind internal depth-compare textures to unbound sampler2DShadow vars. --- src/modules/graphics/Graphics.cpp | 36 ++++++++++++++++++------ src/modules/graphics/Graphics.h | 4 +-- src/modules/graphics/Shader.cpp | 2 +- src/modules/graphics/metal/Shader.mm | 2 +- src/modules/graphics/opengl/Graphics.cpp | 2 +- src/modules/graphics/opengl/Shader.cpp | 2 +- 6 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/modules/graphics/Graphics.cpp b/src/modules/graphics/Graphics.cpp index 5f6a58af3..e585aa185 100644 --- a/src/modules/graphics/Graphics.cpp +++ b/src/modules/graphics/Graphics.cpp @@ -538,9 +538,10 @@ bool Graphics::validateShader(bool gles, const std::vector &stagess return Shader::validate(stages, err); } -Texture *Graphics::getDefaultTexture(TextureType type, DataBaseType dataType) +Texture *Graphics::getDefaultTexture(TextureType type, DataBaseType dataType, bool depthSample) { - Texture *tex = defaultTextures[type][dataType]; + uint32 depthsampleindex = depthSample ? 1 : 0; + Texture *tex = defaultTextures[type][dataType][depthsampleindex]; if (tex != nullptr) return tex; @@ -561,6 +562,18 @@ Texture *Graphics::getDefaultTexture(TextureType type, DataBaseType dataType) break; } + if (depthSample) + { + if (isPixelFormatSupported(PIXELFORMAT_DEPTH16_UNORM, PIXELFORMATUSAGE_SAMPLE)) + settings.format = PIXELFORMAT_DEPTH16_UNORM; + else if (isPixelFormatSupported(PIXELFORMAT_DEPTH24_UNORM, PIXELFORMATUSAGE_SAMPLE)) + settings.format = PIXELFORMAT_DEPTH24_UNORM; + else if (isPixelFormatSupported(PIXELFORMAT_DEPTH32_FLOAT, PIXELFORMATUSAGE_SAMPLE)) + settings.format = PIXELFORMAT_DEPTH32_FLOAT; + else // TODO? + settings.format = PIXELFORMAT_DEPTH24_UNORM; + } + std::string name = "default_"; const char *tname = "unknown"; @@ -578,6 +591,10 @@ Texture *Graphics::getDefaultTexture(TextureType type, DataBaseType dataType) SamplerState s; s.minFilter = s.magFilter = SamplerState::FILTER_NEAREST; s.wrapU = s.wrapV = s.wrapW = SamplerState::WRAP_CLAMP; + + if (depthSample) + s.depthSampleMode.set(COMPARE_ALWAYS); + tex->setSamplerState(s); uint8 pixel[] = {255, 255, 255, 255}; @@ -587,7 +604,7 @@ Texture *Graphics::getDefaultTexture(TextureType type, DataBaseType dataType) for (int slice = 0; slice < (type == TEXTURE_CUBE ? 6 : 1); slice++) tex->replacePixels(pixel, sizeof(pixel), slice, 0, {0, 0, 1, 1}, false); - defaultTextures[type][dataType] = tex; + defaultTextures[type][dataType][depthsampleindex] = tex; return tex; } @@ -647,9 +664,12 @@ void Graphics::releaseDefaultResources() { for (int dataType = 0; dataType < DATA_BASETYPE_MAX_ENUM; dataType++) { - if (defaultTextures[type][dataType]) - defaultTextures[type][dataType]->release(); - defaultTextures[type][dataType] = nullptr; + for (int depthsample = 0; depthsample < 2; depthsample++) + { + if (defaultTextures[type][dataType][depthsample]) + defaultTextures[type][dataType][depthsample]->release(); + defaultTextures[type][dataType][depthsample] = nullptr; + } } } @@ -676,10 +696,10 @@ Texture *Graphics::getTextureOrDefaultForActiveShader(Texture *tex) { auto texinfo = shader->getMainTextureInfo(); if (texinfo != nullptr && texinfo->textureType != TEXTURE_MAX_ENUM) - return getDefaultTexture(texinfo->textureType, texinfo->dataBaseType); + return getDefaultTexture(texinfo->textureType, texinfo->dataBaseType, texinfo->isDepthSampler); } - return getDefaultTexture(TEXTURE_2D, DATA_BASETYPE_FLOAT); + return getDefaultTexture(TEXTURE_2D, DATA_BASETYPE_FLOAT, false); } void Graphics::validateStencilState(const StencilState &s) const diff --git a/src/modules/graphics/Graphics.h b/src/modules/graphics/Graphics.h index bc4688350..10b674e39 100644 --- a/src/modules/graphics/Graphics.h +++ b/src/modules/graphics/Graphics.h @@ -485,7 +485,7 @@ public: bool validateShader(bool gles, const std::vector &stages, const Shader::CompileOptions &options, std::string &err); - Texture *getDefaultTexture(TextureType type, DataBaseType dataType); + Texture *getDefaultTexture(TextureType type, DataBaseType dataType, bool depthSample); Buffer *getDefaultTexelBuffer(DataBaseType dataType); Buffer *getDefaultStorageBuffer(); Texture *getTextureOrDefaultForActiveShader(Texture *tex); @@ -1100,7 +1100,7 @@ private: void checkSetDefaultFont(); int calculateEllipsePoints(float rx, float ry) const; - Texture *defaultTextures[TEXTURE_MAX_ENUM][DATA_BASETYPE_MAX_ENUM]; + Texture *defaultTextures[TEXTURE_MAX_ENUM][DATA_BASETYPE_MAX_ENUM][2]; Buffer *defaultTexelBuffers[DATA_BASETYPE_MAX_ENUM]; Buffer *defaultStorageBuffer; diff --git a/src/modules/graphics/Shader.cpp b/src/modules/graphics/Shader.cpp index 6f4bee6fe..68fc16b70 100644 --- a/src/modules/graphics/Shader.cpp +++ b/src/modules/graphics/Shader.cpp @@ -725,7 +725,7 @@ Shader::Shader(StrongRef _stages[], const CompileOptions &options) if (u.baseType == UNIFORM_SAMPLER || u.baseType == UNIFORM_STORAGETEXTURE) { - auto tex = gfx->getDefaultTexture(u.textureType, u.dataBaseType); + auto tex = gfx->getDefaultTexture(u.textureType, u.dataBaseType, u.isDepthSampler); for (int i = 0; i < u.count; i++) { tex->retain(); diff --git a/src/modules/graphics/metal/Shader.mm b/src/modules/graphics/metal/Shader.mm index 20cb2df13..5963ec414 100644 --- a/src/modules/graphics/metal/Shader.mm +++ b/src/modules/graphics/metal/Shader.mm @@ -743,7 +743,7 @@ void Shader::sendTextures(const UniformInfo *info, love::graphics::Texture **tex else { auto gfx = Graphics::getInstance(); - tex = gfx->getDefaultTexture(info->textureType, info->dataBaseType); + tex = gfx->getDefaultTexture(info->textureType, info->dataBaseType, info->isDepthSampler); } tex->retain(); diff --git a/src/modules/graphics/opengl/Graphics.cpp b/src/modules/graphics/opengl/Graphics.cpp index c26fa8c07..9a1527ba9 100644 --- a/src/modules/graphics/opengl/Graphics.cpp +++ b/src/modules/graphics/opengl/Graphics.cpp @@ -1720,7 +1720,7 @@ uint32 Graphics::computePixelFormatUsage(PixelFormat format, bool readable) // this is required on ES2 but I'm not positive. if (isPixelFormatDepthStencil(format)) { - love::graphics::Texture *tex = getDefaultTexture(TEXTURE_2D, DATA_BASETYPE_FLOAT); + love::graphics::Texture *tex = getDefaultTexture(TEXTURE_2D, DATA_BASETYPE_FLOAT, false); gl.framebufferTexture(GL_COLOR_ATTACHMENT0, TEXTURE_2D, (GLuint) tex->getHandle(), 0, 0, 0); } diff --git a/src/modules/graphics/opengl/Shader.cpp b/src/modules/graphics/opengl/Shader.cpp index 3885b7d8a..99258682e 100644 --- a/src/modules/graphics/opengl/Shader.cpp +++ b/src/modules/graphics/opengl/Shader.cpp @@ -593,7 +593,7 @@ void Shader::sendTextures(const UniformInfo *info, love::graphics::Texture **tex else { auto gfx = Module::getInstance(Module::M_GRAPHICS); - tex = gfx->getDefaultTexture(info->textureType, info->dataBaseType); + tex = gfx->getDefaultTexture(info->textureType, info->dataBaseType, info->isDepthSampler); } tex->retain();