Просмотр исходного кода

[shaderc] Clean up (#3258)

* Add Metal Shading Language version options to shaderc. Reference version options from: https://developer.apple.com/documentation/metal/mtllanguageversion?language=objc
Add configuration options for MSL compiler based on MSL version and Platform
Configure MSL->SPIR-V version configuration based on when ray tracing types become available
Set default metal compiler option to be metal 1.2, which is the default version assigned in the current SPIRV-Cross being used

* Add Metal Shading Language version options to shaderc. Reference version options from: https://developer.apple.com/documentation/metal/mtllanguageversion?language=objc
Add configuration options for MSL compiler based on MSL version and Platform
Configure MSL->SPIR-V version configuration based on when ray tracing types become available
Set default metal compiler option to be metal 1.2, which is the default version assigned in the current SPIRV-Cross being used

* Group ios and osx platform code paths
Reduce the size of temp buffer used in writing the MSL preprocessor define
Fix a compiler warning about size_t to int32_t truncation
Adjust whitespace in usage text

* Remove indented scope left-over from branch condition removal

* Remove some testing code

* Adjust whitespace for function arguments

* Replace BX_ASSERT with bx::write to message handler in spirv generation
pheonix 1 год назад
Родитель
Сommit
56ad576dcd
3 измененных файлов с 141 добавлено и 146 удалено
  1. 15 23
      tools/shaderc/shaderc.cpp
  2. 107 110
      tools/shaderc/shaderc_metal.cpp
  3. 19 13
      tools/shaderc/shaderc_spirv.cpp

+ 15 - 23
tools/shaderc/shaderc.cpp

@@ -1036,7 +1036,7 @@ namespace bgfx
 
 			  "\n"
 			  "Options:\n"
-			  "  -h, --help            	       Display this help and exit.\n"
+			  "  -h, --help                    Display this help and exit.\n"
 			  "  -v, --version                 Output version information and exit.\n"
 			  "  -f <file path>                Input's file path.\n"
 			  "  -i <include path>             Include path. (for multiple paths use -i multiple times)\n"
@@ -1213,22 +1213,6 @@ namespace bgfx
 			preprocessor.setDefine("BX_PLATFORM_EMSCRIPTEN=1");
 			preprocessor.setDefine(glslDefine);
 		}
-		else if (0 == bx::strCmpI(platform, "ios") )
-		{
-			preprocessor.setDefine("BX_PLATFORM_IOS=1");
-			if (profile->lang != ShadingLang::Metal)
-			{
-				preprocessor.setDefine(glslDefine);
-			}
-			char temp[32];
-			bx::snprintf(
-				temp
-				, sizeof(temp)
-				, "BGFX_SHADER_LANGUAGE_METAL=%d"
-				, (profile->lang == ShadingLang::Metal) ? profile->id : 0
-			);
-			preprocessor.setDefine(temp);
-		}
 		else if (0 == bx::strCmpI(platform, "linux") )
 		{
 			preprocessor.setDefine("BX_PLATFORM_LINUX=1");
@@ -1241,20 +1225,28 @@ namespace bgfx
 				preprocessor.setDefine(glslDefine);
 			}
 		}
-		else if (0 == bx::strCmpI(platform, "osx") )
+		else if (0 == bx::strCmpI(platform, "ios") || (0 == bx::strCmpI(platform, "osx")) )
 		{
-			preprocessor.setDefine("BX_PLATFORM_OSX=1");
+			if (0 == bx::strCmpI(platform, "osx"))
+			{
+				preprocessor.setDefine("BX_PLATFORM_OSX=1");
+			}
+			else
+			{
+				preprocessor.setDefine("BX_PLATFORM_IOS=1");
+			}
+
 			if (profile->lang != ShadingLang::Metal)
 			{
 				preprocessor.setDefine(glslDefine);
 			}
-			char temp[256];
+			char temp[32];
 			bx::snprintf(
-				  temp
+				temp
 				, sizeof(temp)
 				, "BGFX_SHADER_LANGUAGE_METAL=%d"
 				, (profile->lang == ShadingLang::Metal) ? profile->id : 0
-				);
+			);
 			preprocessor.setDefine(temp);
 		}
 		else if (0 == bx::strCmpI(platform, "windows") )
@@ -2846,7 +2838,7 @@ namespace bgfx
 				}
 			}
 
-			int32_t size = bx::getSize(&reader);
+			int32_t size = (int32_t)bx::getSize(&reader);
 			const int32_t total = size + 16384;
 			char* data = new char[total];
 			size = bx::read(&reader, data, size, bx::ErrorAssert{});

+ 107 - 110
tools/shaderc/shaderc_metal.cpp

@@ -468,7 +468,8 @@ namespace bgfx { namespace metal
 							// If the line declares a uniform, merge all next
 							// lines until we encounter a semicolon.
 							bx::StringView lineEnd = strFind(strLine, ";");
-							while (lineEnd.isEmpty() && !reader.isDone()) {
+							while (lineEnd.isEmpty() && !reader.isDone())
+							{
 								bx::StringView nextLine = reader.next();
 								strLine.set(strLine.getPtr(), nextLine.getTerm());
 								lineEnd = strFind(nextLine, ";");
@@ -651,138 +652,134 @@ namespace bgfx { namespace metal
 
 					bx::Error err;
 
-					{
-						spirv_cross::CompilerMSL msl(std::move(spirv) );
+					spirv_cross::CompilerMSL msl(std::move(spirv) );
 
-						// Configure MSL cross compiler
-						spirv_cross::CompilerMSL::Options mslOptions = msl.get_msl_options();
-						{
-							// - Platform
-							mslOptions.platform = getMslPlatform(_options.platform);
+					// Configure MSL cross compiler
+					spirv_cross::CompilerMSL::Options mslOptions = msl.get_msl_options();
+					{
+						// - Platform
+						mslOptions.platform = getMslPlatform(_options.platform);
 
-							// - MSL Version
-							uint32_t major, minor;
-							getMSLVersion(_version, major, minor, _messageWriter);
-							mslOptions.set_msl_version(major, minor);
+						// - MSL Version
+						uint32_t major, minor;
+						getMSLVersion(_version, major, minor, _messageWriter);
+						mslOptions.set_msl_version(major, minor);
+					}
+					msl.set_msl_options(mslOptions);
 
-						}
-						msl.set_msl_options(mslOptions);
+					auto executionModel = msl.get_execution_model();
+					spirv_cross::MSLResourceBinding newBinding;
+					newBinding.stage = executionModel;
 
-						auto executionModel = msl.get_execution_model();
-						spirv_cross::MSLResourceBinding newBinding;
-						newBinding.stage = executionModel;
+					spirv_cross::ShaderResources resources = msl.get_shader_resources();
 
-						spirv_cross::ShaderResources resources = msl.get_shader_resources();
+					spirv_cross::SmallVector<spirv_cross::EntryPoint> entryPoints = msl.get_entry_points_and_stages();
+					if (!entryPoints.empty() )
+					{
+						msl.rename_entry_point(
+							entryPoints[0].name
+							, "xlatMtlMain"
+							, entryPoints[0].execution_model
+							);
+					}
 
-						spirv_cross::SmallVector<spirv_cross::EntryPoint> entryPoints = msl.get_entry_points_and_stages();
-						if (!entryPoints.empty() )
-						{
-							msl.rename_entry_point(
-								  entryPoints[0].name
-								, "xlatMtlMain"
-								, entryPoints[0].execution_model
-								);
-						}
+					for (auto& resource : resources.uniform_buffers)
+					{
+						unsigned set     = msl.get_decoration(resource.id, spv::DecorationDescriptorSet);
+						unsigned binding = msl.get_decoration(resource.id, spv::DecorationBinding);
+						newBinding.desc_set   = set;
+						newBinding.binding    = binding;
+						newBinding.msl_buffer = 0;
+						msl.add_msl_resource_binding(newBinding);
+
+						msl.set_name(resource.id, "_mtl_u");
+					}
 
-						for (auto& resource : resources.uniform_buffers)
-						{
-							unsigned set     = msl.get_decoration(resource.id, spv::DecorationDescriptorSet);
-							unsigned binding = msl.get_decoration(resource.id, spv::DecorationBinding);
-							newBinding.desc_set   = set;
-							newBinding.binding    = binding;
-							newBinding.msl_buffer = 0;
-							msl.add_msl_resource_binding(newBinding);
-
-							msl.set_name(resource.id, "_mtl_u");
-						}
+					for (auto& resource : resources.storage_buffers)
+					{
+						unsigned set     = msl.get_decoration(resource.id, spv::DecorationDescriptorSet);
+						unsigned binding = msl.get_decoration(resource.id, spv::DecorationBinding);
+						newBinding.desc_set   = set;
+						newBinding.binding    = binding;
+						newBinding.msl_buffer = binding + 1;
+						msl.add_msl_resource_binding(newBinding);
+					}
 
-						for (auto& resource : resources.storage_buffers)
-						{
-							unsigned set     = msl.get_decoration(resource.id, spv::DecorationDescriptorSet);
-							unsigned binding = msl.get_decoration(resource.id, spv::DecorationBinding);
-							newBinding.desc_set   = set;
-							newBinding.binding    = binding;
-							newBinding.msl_buffer = binding + 1;
-							msl.add_msl_resource_binding(newBinding);
-						}
+					for (auto& resource : resources.separate_samplers)
+					{
+						unsigned set     = msl.get_decoration(resource.id, spv::DecorationDescriptorSet);
+						unsigned binding = msl.get_decoration(resource.id, spv::DecorationBinding);
+						newBinding.desc_set    = set;
+						newBinding.binding     = binding;
+						newBinding.msl_texture = binding - textureBindingOffset;
+						newBinding.msl_sampler = binding - textureBindingOffset;
+						msl.add_msl_resource_binding(newBinding);
+					}
 
-						for (auto& resource : resources.separate_samplers)
+					for (auto& resource : resources.separate_images)
+					{
+						std::string name = msl.get_name(resource.id);
+						if (name.size() > 7 && 0 == bx::strCmp(name.c_str() + name.length() - 7, "Texture") )
 						{
-							unsigned set     = msl.get_decoration(resource.id, spv::DecorationDescriptorSet);
-							unsigned binding = msl.get_decoration(resource.id, spv::DecorationBinding);
-							newBinding.desc_set    = set;
-							newBinding.binding     = binding;
-							newBinding.msl_texture = binding - textureBindingOffset;
-							newBinding.msl_sampler = binding - textureBindingOffset;
-							msl.add_msl_resource_binding(newBinding);
+							msl.set_name(resource.id, name.substr(0, name.length() - 7) );
 						}
 
-						for (auto& resource : resources.separate_images)
-						{
-							std::string name = msl.get_name(resource.id);
-							if (name.size() > 7 && 0 == bx::strCmp(name.c_str() + name.length() - 7, "Texture") )
-							{
-								msl.set_name(resource.id, name.substr(0, name.length() - 7) );
-							}
+						unsigned set     = msl.get_decoration(resource.id, spv::DecorationDescriptorSet);
+						unsigned binding = msl.get_decoration(resource.id, spv::DecorationBinding);
+						newBinding.desc_set    = set;
+						newBinding.binding     = binding;
+						newBinding.msl_texture = binding - textureBindingOffset;
+						newBinding.msl_sampler = binding - textureBindingOffset;
+						msl.add_msl_resource_binding(newBinding);
+					}
 
-							unsigned set     = msl.get_decoration(resource.id, spv::DecorationDescriptorSet);
-							unsigned binding = msl.get_decoration(resource.id, spv::DecorationBinding);
-							newBinding.desc_set    = set;
-							newBinding.binding     = binding;
-							newBinding.msl_texture = binding - textureBindingOffset;
-							newBinding.msl_sampler = binding - textureBindingOffset;
-							msl.add_msl_resource_binding(newBinding);
-						}
+					for (auto& resource : resources.storage_images)
+					{
+						std::string name = msl.get_name(resource.id);
+
+						unsigned set     = msl.get_decoration(resource.id, spv::DecorationDescriptorSet);
+						unsigned binding = msl.get_decoration(resource.id, spv::DecorationBinding);
+						newBinding.desc_set    = set;
+						newBinding.binding     = binding;
+						newBinding.msl_texture = binding - textureBindingOffset;
+						newBinding.msl_sampler = binding - textureBindingOffset;
+						msl.add_msl_resource_binding(newBinding);
+					}
 
-						for (auto& resource : resources.storage_images)
-						{
-							std::string name = msl.get_name(resource.id);
-
-							unsigned set     = msl.get_decoration(resource.id, spv::DecorationDescriptorSet);
-							unsigned binding = msl.get_decoration(resource.id, spv::DecorationBinding);
-							newBinding.desc_set    = set;
-							newBinding.binding     = binding;
-							newBinding.msl_texture = binding - textureBindingOffset;
-							newBinding.msl_sampler = binding - textureBindingOffset;
-							msl.add_msl_resource_binding(newBinding);
-						}
+					std::string source = msl.compile();
 
-						std::string source = msl.compile();
+					// fix https://github.com/bkaradzic/bgfx/issues/2822
+					// insert struct member which declares point size, defaulted to 1
+					if ('v' == _options.shaderType)
+					{
+						const bx::StringView xlatMtlMainOut("xlatMtlMain_out\n{");
+						size_t pos = source.find(xlatMtlMainOut.getPtr() );
 
-						// fix https://github.com/bkaradzic/bgfx/issues/2822
-						// insert struct member which declares point size, defaulted to 1
-						if ('v' == _options.shaderType)
+						if (pos != std::string::npos)
 						{
-							const bx::StringView xlatMtlMainOut("xlatMtlMain_out\n{");
-							size_t pos = source.find(xlatMtlMainOut.getPtr() );
-
-							if (pos != std::string::npos)
-							{
-								pos += xlatMtlMainOut.getLength();
-								source.insert(pos, "\n\tfloat bgfx_metal_pointSize [[point_size]] = 1;");
-							}
+							pos += xlatMtlMainOut.getLength();
+							source.insert(pos, "\n\tfloat bgfx_metal_pointSize [[point_size]] = 1;");
 						}
+					}
 
-						if ('c' == _options.shaderType)
+					if ('c' == _options.shaderType)
+					{
+						for (int i = 0; i < 3; ++i)
 						{
-							for (int i = 0; i < 3; ++i)
-							{
-								uint16_t dim = (uint16_t)msl.get_execution_mode_argument(
-									  spv::ExecutionMode::ExecutionModeLocalSize
-									, i
-									);
-								bx::write(_shaderWriter, dim, &err);
-							}
+							uint16_t dim = (uint16_t)msl.get_execution_mode_argument(
+								spv::ExecutionMode::ExecutionModeLocalSize
+								, i
+								);
+							bx::write(_shaderWriter, dim, &err);
 						}
-
-						uint32_t shaderSize = (uint32_t)source.size();
-						bx::write(_shaderWriter, shaderSize, &err);
-						bx::write(_shaderWriter, source.c_str(), shaderSize, &err);
-						uint8_t nul = 0;
-						bx::write(_shaderWriter, nul, &err);
 					}
 
-					//
+					const uint32_t shaderSize = (uint32_t)source.size();
+					bx::write(_shaderWriter, shaderSize, &err);
+					bx::write(_shaderWriter, source.c_str(), shaderSize, &err);
+					const uint8_t nul = 0;
+					bx::write(_shaderWriter, nul, &err);
+
 					const uint8_t numAttr = (uint8_t)program->getNumLiveAttributes();
 					bx::write(_shaderWriter, numAttr, &err);
 

+ 19 - 13
tools/shaderc/shaderc_spirv.cpp

@@ -375,9 +375,11 @@ namespace bgfx { namespace spirv
 		return size;
 	}
 
-	static spv_target_env getSpirvTargetVersion(uint32_t version)
+	static spv_target_env getSpirvTargetVersion(uint32_t _version, bx::WriterI* _messageWriter)
 	{
-		switch (version)
+		bx::ErrorAssert err;
+
+		switch (_version)
 		{
 			case 1010:
 				return SPV_ENV_VULKAN_1_0;
@@ -390,14 +392,16 @@ namespace bgfx { namespace spirv
 			case 1613:
 				return SPV_ENV_VULKAN_1_3;
 			default:
-				BX_ASSERT(0, "Unknown SPIR-V version requested. Returning SPV_ENV_VULKAN_1_0 as default.");
+				bx::write(_messageWriter, &err, "Warning: Unknown SPIR-V version requested. Returning SPV_ENV_VULKAN_1_0 as default.\n");
 				return SPV_ENV_VULKAN_1_0;
 		}
 	}
 
-	static glslang::EShTargetClientVersion getGlslangTargetVulkanVersion(uint32_t version)
+	static glslang::EShTargetClientVersion getGlslangTargetVulkanVersion(uint32_t _version, bx::WriterI* _messageWriter)
 	{
-		switch (version)
+		bx::ErrorAssert err;
+
+		switch (_version)
 		{
 			case 1010:
 				return glslang::EShTargetVulkan_1_0;
@@ -409,14 +413,16 @@ namespace bgfx { namespace spirv
 			case 1613:
 				return glslang::EShTargetVulkan_1_3;
 			default:
-				BX_ASSERT(0, "Unknown SPIR-V version requested. Returning EShTargetVulkan_1_0 as default.");
+				bx::write(_messageWriter, &err, "Warning: Unknown SPIR-V version requested. Returning EShTargetVulkan_1_0 as default.\n");
 				return glslang::EShTargetVulkan_1_0;
 		}
 	}
 
-	static glslang::EShTargetLanguageVersion getGlslangTargetSpirvVersion(uint32_t version)
+	static glslang::EShTargetLanguageVersion getGlslangTargetSpirvVersion(uint32_t _version, bx::WriterI* _messageWriter)
 	{
-		switch (version)
+		bx::ErrorAssert err;
+
+		switch (_version)
 		{
 			case 1010:
 				return glslang::EShTargetSpv_1_0;
@@ -429,7 +435,7 @@ namespace bgfx { namespace spirv
 			case 1613:
 				return glslang::EShTargetSpv_1_6;
 			default:
-				BX_ASSERT(0, "Unknown SPIR-V version requested. Returning EShTargetSpv_1_0 as default.");
+				bx::write(_messageWriter, &err, "Warning: Unknown SPIR-V version requested. Returning EShTargetSpv_1_0 as default.\n");
 				return glslang::EShTargetSpv_1_0;
 		}
 	}
@@ -469,8 +475,8 @@ namespace bgfx { namespace spirv
 		shader->setEntryPoint("main");
 		shader->setAutoMapBindings(true);
 		shader->setEnvInput(glslang::EShSourceHlsl, stage, glslang::EShClientVulkan, s_GLSL_VULKAN_CLIENT_VERSION);
-		shader->setEnvClient(glslang::EShClientVulkan, getGlslangTargetVulkanVersion(_version));
-		shader->setEnvTarget(glslang::EShTargetSpv, getGlslangTargetSpirvVersion(_version));
+		shader->setEnvClient(glslang::EShClientVulkan, getGlslangTargetVulkanVersion(_version, _messageWriter));
+		shader->setEnvTarget(glslang::EShTargetSpv, getGlslangTargetSpirvVersion(_version, _messageWriter));
 
 		// Reserve two spots for the stage UBOs
 		shader->setShiftBinding(glslang::EResUbo, (stage == EShLanguage::EShLangFragment ? kSpirvFragmentBinding : kSpirvVertexBinding));
@@ -712,7 +718,7 @@ namespace bgfx { namespace spirv
 
 				glslang::GlslangToSpv(*intermediate, spirv, &options);
 
-				spvtools::Optimizer opt(getSpirvTargetVersion(_version));
+				spvtools::Optimizer opt(getSpirvTargetVersion(_version, _messageWriter));
 
 				auto print_msg_to_stderr = [_messageWriter, &messageErr](
 					  spv_message_level_t
@@ -745,7 +751,7 @@ namespace bgfx { namespace spirv
 				{
 					if (g_verbose)
 					{
-						glslang::SpirvToolsDisassemble(std::cout, spirv, getSpirvTargetVersion(_version));
+						glslang::SpirvToolsDisassemble(std::cout, spirv, getSpirvTargetVersion(_version, _messageWriter));
 					}
 
 					spirv_cross::CompilerReflection refl(spirv);