Browse Source

metal: vertex attribute reflection is slightly better

Alex Szpakowski 4 years ago
parent
commit
4d6cfb1f90
2 changed files with 37 additions and 13 deletions
  1. 3 3
      src/modules/graphics/Shader.cpp
  2. 34 10
      src/modules/graphics/metal/Shader.mm

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

@@ -272,9 +272,9 @@ void setPointSize() {
 )";
 
 static const char vertex_main[] = R"(
-LOVE_IO_LOCATION(0) attribute vec4 VertexPosition;
-LOVE_IO_LOCATION(1) attribute vec4 VertexTexCoord;
-LOVE_IO_LOCATION(2) attribute vec4 VertexColor;
+attribute vec4 VertexPosition;
+attribute vec4 VertexTexCoord;
+attribute vec4 VertexColor;
 
 varying vec4 VaryingTexCoord;
 varying vec4 VaryingColor;

+ 34 - 10
src/modules/graphics/metal/Shader.mm

@@ -322,8 +322,42 @@ Shader::Shader(id<MTLDevice> device, love::graphics::ShaderStage *vertex, love::
 				}
 			}
 
+			for (const auto &resource : resources.storage_buffers)
+			{
+				auto it = uniforms.find(resource.name);
+				if (it != uniforms.end())
+				{
+					continue;
+				}
+
+				// TODO
+			}
+
 			if (i == ShaderStage::STAGE_VERTEX)
 			{
+				int nextattributeindex = ATTRIB_MAX_ENUM;
+
+				for (const auto &var : interfacevars)
+				{
+					spv::StorageClass storage = msl.get_storage_class(var);
+					const std::string &name = msl.get_name(var);
+
+					if (storage == spv::StorageClassInput)
+					{
+						int index = 0;
+
+						BuiltinVertexAttribute builtinattribute;
+						if (graphics::getConstant(name.c_str(), builtinattribute))
+							index = (int) builtinattribute;
+						else
+							index = nextattributeindex++;
+
+						msl.set_decoration(var, spv::DecorationLocation, index);
+
+						attributes[name] = msl.get_decoration(var, spv::DecorationLocation);
+					}
+				}
+
 				for (const auto &varying : resources.stage_outputs)
 				{
 //					printf("vertex shader output %s: %d\n", inp.name.c_str(), msl.get_decoration(inp.id, spv::DecorationLocation));
@@ -373,16 +407,6 @@ Shader::Shader(id<MTLDevice> device, love::graphics::ShaderStage *vertex, love::
 			}
 
 			functions[i] = [library newFunctionWithName:library.functionNames[0]];
-
-			for (const auto &var : interfacevars)
-			{
-				spv::StorageClass storage = msl.get_storage_class(var);
-				const std::string &name = msl.get_name(var);
-//				printf("var: %s\n", name.c_str());
-
-				if (i == ShaderStage::STAGE_VERTEX && storage == spv::StorageClassInput)
-					attributes[name] = msl.get_decoration(var, spv::DecorationLocation);
-			}
 		}
 		catch (std::exception &e)
 		{