Browse Source

graphics: bind internal depth-compare textures to unbound sampler2DShadow vars.

Sasha Szpakowski 1 year ago
parent
commit
03f364e131

+ 28 - 8
src/modules/graphics/Graphics.cpp

@@ -538,9 +538,10 @@ bool Graphics::validateShader(bool gles, const std::vector<std::string> &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

+ 2 - 2
src/modules/graphics/Graphics.h

@@ -485,7 +485,7 @@ public:
 
 	bool validateShader(bool gles, const std::vector<std::string> &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;
 

+ 1 - 1
src/modules/graphics/Shader.cpp

@@ -725,7 +725,7 @@ Shader::Shader(StrongRef<ShaderStage> _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();

+ 1 - 1
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();

+ 1 - 1
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);
 		}
 

+ 1 - 1
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<love::graphics::Graphics>(Module::M_GRAPHICS);
-			tex = gfx->getDefaultTexture(info->textureType, info->dataBaseType);
+			tex = gfx->getDefaultTexture(info->textureType, info->dataBaseType, info->isDepthSampler);
 		}
 
 		tex->retain();