Browse Source

metal: clean up some shader resource binding code

Sasha Szpakowski 1 year ago
parent
commit
321143755f
2 changed files with 27 additions and 77 deletions
  1. 0 1
      src/modules/graphics/metal/Shader.h
  2. 27 76
      src/modules/graphics/metal/Shader.mm

+ 0 - 1
src/modules/graphics/metal/Shader.h

@@ -138,7 +138,6 @@ private:
 	};
 	};
 
 
 	void buildLocalUniforms(const spirv_cross::CompilerMSL &msl, const spirv_cross::SPIRType &type, size_t baseoffset, const std::string &basename);
 	void buildLocalUniforms(const spirv_cross::CompilerMSL &msl, const spirv_cross::SPIRType &type, size_t baseoffset, const std::string &basename);
-	void addImage(const spirv_cross::Resource &resource);
 	void compileFromGLSLang(id<MTLDevice> device, const glslang::TProgram &program);
 	void compileFromGLSLang(id<MTLDevice> device, const glslang::TProgram &program);
 
 
 	id<MTLFunction> functions[SHADERSTAGE_MAX_ENUM];
 	id<MTLFunction> functions[SHADERSTAGE_MAX_ENUM];

+ 27 - 76
src/modules/graphics/metal/Shader.mm

@@ -340,30 +340,6 @@ void Shader::buildLocalUniforms(const spirv_cross::CompilerMSL &msl, const spirv
 	}
 	}
 }
 }
 
 
-void Shader::addImage(const spirv_cross::Resource &resource)
-{
-	using namespace spirv_cross;
-
-	std::string name = canonicaliizeUniformName(resource.name);
-	auto uniformit = reflection.allUniforms.find(name);
-	if (uniformit == reflection.allUniforms.end())
-		return;
-
-	UniformInfo &u = *(uniformit->second);
-
-	if (u.dataSize == 0)
-	{
-		u.dataSize = sizeof(int) * u.count;
-		u.data = malloc(u.dataSize);
-		for (int i = 0; i < u.count; i++)
-			u.ints[i] = -1; // Initialized below, after compiling.
-	}
-
-	BuiltinUniform builtin;
-	if (getConstant(name.c_str(), builtin))
-		builtinUniformInfo[builtin] = &u;
-}
-
 void Shader::compileFromGLSLang(id<MTLDevice> device, const glslang::TProgram &program)
 void Shader::compileFromGLSLang(id<MTLDevice> device, const glslang::TProgram &program)
 {
 {
 	using namespace glslang;
 	using namespace glslang;
@@ -414,16 +390,6 @@ void Shader::compileFromGLSLang(id<MTLDevice> device, const glslang::TProgram &p
 
 
 		msl.set_enabled_interface_variables(interfacevars);
 		msl.set_enabled_interface_variables(interfacevars);
 
 
-		for (const auto &resource : resources.storage_images)
-		{
-			addImage(resource);
-		}
-
-		for (const auto &resource : resources.sampled_images)
-		{
-			addImage(resource);
-		}
-
 		for (const auto &resource : resources.uniform_buffers)
 		for (const auto &resource : resources.uniform_buffers)
 		{
 		{
 			MSLResourceBinding binding;
 			MSLResourceBinding binding;
@@ -471,25 +437,6 @@ void Shader::compileFromGLSLang(id<MTLDevice> device, const glslang::TProgram &p
 			binding.desc_set = msl.get_decoration(resource.id, spv::DecorationDescriptorSet);
 			binding.desc_set = msl.get_decoration(resource.id, spv::DecorationDescriptorSet);
 			binding.msl_buffer = metalBufferIndices[stageindex]++;
 			binding.msl_buffer = metalBufferIndices[stageindex]++;
 			msl.add_msl_resource_binding(binding);
 			msl.add_msl_resource_binding(binding);
-
-			std::string name = canonicaliizeUniformName(resource.name);
-			auto uniformit = reflection.allUniforms.find(name);
-			if (uniformit == reflection.allUniforms.end())
-			{
-				handleUnknownUniformName(name.c_str());
-				continue;
-			}
-
-			UniformInfo &u = *(uniformit->second);
-
-			if (u.dataSize > 0)
-				continue;
-
-			u.dataSize = sizeof(int) * u.count;
-			u.data = malloc(u.dataSize);
-
-			for (int i = 0; i < u.count; i++)
-				u.ints[i] = -1; // Initialized below, after compiling.
 		}
 		}
 
 
 		if (stageindex == SHADERSTAGE_VERTEX)
 		if (stageindex == SHADERSTAGE_VERTEX)
@@ -577,7 +524,10 @@ void Shader::compileFromGLSLang(id<MTLDevice> device, const glslang::TProgram &p
 			std::string name = canonicaliizeUniformName(resource.name);
 			std::string name = canonicaliizeUniformName(resource.name);
 			auto it = reflection.allUniforms.find(name);
 			auto it = reflection.allUniforms.find(name);
 			if (it == reflection.allUniforms.end())
 			if (it == reflection.allUniforms.end())
+			{
+				handleUnknownUniformName(name.c_str());
 				return;
 				return;
+			}
 
 
 			UniformInfo &u = *(it->second);
 			UniformInfo &u = *(it->second);
 
 
@@ -592,11 +542,11 @@ void Shader::compileFromGLSLang(id<MTLDevice> device, const glslang::TProgram &p
 
 
 			u.active = true;
 			u.active = true;
 
 
-			for (int i = 0; i < u.count; i++)
+			if (u.location < 0)
 			{
 			{
-				if (u.ints[i] == -1)
+				u.location = (int)textureBindings.size();
+				for (int i = 0; i < u.count; i++)
 				{
 				{
-					u.ints[i] = (int)textureBindings.size();
 					TextureBinding b = {};
 					TextureBinding b = {};
 					b.access = u.access;
 					b.access = u.access;
 
 
@@ -614,11 +564,18 @@ void Shader::compileFromGLSLang(id<MTLDevice> device, const glslang::TProgram &p
 
 
 					textureBindings.push_back(b);
 					textureBindings.push_back(b);
 				}
 				}
+			}
 
 
-				auto &b = textureBindings[u.ints[i]];
+			for (int i = 0; i < u.count; i++)
+			{
+				auto &b = textureBindings[u.location + i];
 				b.textureStages[stageindex] = (uint8) texturebinding;
 				b.textureStages[stageindex] = (uint8) texturebinding;
 				b.samplerStages[stageindex] = (uint8) samplerbinding;
 				b.samplerStages[stageindex] = (uint8) samplerbinding;
 			}
 			}
+
+			BuiltinUniform builtin;
+			if (getConstant(name.c_str(), builtin))
+				builtinUniformInfo[builtin] = &u;
 		};
 		};
 
 
 		for (const auto &resource : resources.sampled_images)
 		for (const auto &resource : resources.sampled_images)
@@ -636,7 +593,10 @@ void Shader::compileFromGLSLang(id<MTLDevice> device, const glslang::TProgram &p
 			std::string name = canonicaliizeUniformName(resource.name);
 			std::string name = canonicaliizeUniformName(resource.name);
 			auto it = reflection.storageBuffers.find(name);
 			auto it = reflection.storageBuffers.find(name);
 			if (it == reflection.storageBuffers.end())
 			if (it == reflection.storageBuffers.end())
+			{
+				handleUnknownUniformName(name.c_str());
 				continue;
 				continue;
+			}
 
 
 			UniformInfo &u = it->second;
 			UniformInfo &u = it->second;
 
 
@@ -649,11 +609,11 @@ void Shader::compileFromGLSLang(id<MTLDevice> device, const glslang::TProgram &p
 
 
 			u.active = true;
 			u.active = true;
 
 
-			for (int i = 0; i < u.count; i++)
+			if (u.location < 0)
 			{
 			{
-				if (u.ints[i] == -1)
+				u.location = (int)bufferBindings.size();
+				for (int i = 0; i < u.count; i++)
 				{
 				{
-					u.ints[i] = (int)bufferBindings.size();
 					BufferBinding b = {};
 					BufferBinding b = {};
 					b.access = u.access;
 					b.access = u.access;
 
 
@@ -662,9 +622,10 @@ void Shader::compileFromGLSLang(id<MTLDevice> device, const glslang::TProgram &p
 
 
 					bufferBindings.push_back(b);
 					bufferBindings.push_back(b);
 				}
 				}
-
-				bufferBindings[u.ints[i]].stages[stageindex] = (uint8) bufferbinding;
 			}
 			}
+
+			for (int i = 0; i < u.count; i++)
+				bufferBindings[u.location + i].stages[stageindex] = (uint8) bufferbinding;
 		}
 		}
 	}
 	}
 
 
@@ -700,15 +661,6 @@ Shader::~Shader()
 
 
 	cachedRenderPipelines.clear();
 	cachedRenderPipelines.clear();
 
 
-	for (const auto &it : reflection.allUniforms)
-	{
-		const auto &u = *(it.second);
-		if (u.baseType == UNIFORM_SAMPLER || u.baseType == UNIFORM_STORAGETEXTURE)
-			free(u.data);
-		else if (u.baseType == UNIFORM_TEXELBUFFER || u.baseType == UNIFORM_STORAGEBUFFER)
-			free(u.data);
-	}
-
 	delete[] localUniformStagingData;
 	delete[] localUniformStagingData;
 	delete[] localUniformBufferData;
 	delete[] localUniformBufferData;
 }}
 }}
@@ -816,10 +768,10 @@ void Shader::sendTextures(const UniformInfo *info, love::graphics::Texture **tex
 
 
 		activeTextures[resourceindex] = tex;
 		activeTextures[resourceindex] = tex;
 
 
-		if (info->dataSize == 0)
+		if (info->location < 0)
 			continue;
 			continue;
 
 
-		int bindingindex = info->ints[i];
+		int bindingindex = info->location + i;
 		if (bindingindex < 0)
 		if (bindingindex < 0)
 			continue;
 			continue;
 
 
@@ -850,7 +802,6 @@ void Shader::sendBuffers(const UniformInfo *info, love::graphics::Buffer **buffe
 
 
 	count = std::min(count, info->count);
 	count = std::min(count, info->count);
 
 
-	// Bind the textures to the texture units.
 	for (int i = 0; i < count; i++)
 	for (int i = 0; i < count; i++)
 	{
 	{
 		love::graphics::Buffer *buffer = buffers[i];
 		love::graphics::Buffer *buffer = buffers[i];
@@ -879,10 +830,10 @@ void Shader::sendBuffers(const UniformInfo *info, love::graphics::Buffer **buffe
 
 
 		activeBuffers[resourceindex] = buffer;
 		activeBuffers[resourceindex] = buffer;
 
 
-		if (info->dataSize == 0)
+		if (info->location < 0)
 			continue;
 			continue;
 
 
-		int bindingindex = info->ints[i];
+		int bindingindex = info->location + i;
 		if (texelbinding && bindingindex >= 0)
 		if (texelbinding && bindingindex >= 0)
 		{
 		{
 			textureBindings[bindingindex].texture = getMTLTexture(buffer);
 			textureBindings[bindingindex].texture = getMTLTexture(buffer);