From 5edeea64a95760334b09e49cd8145305d2ff5756 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=91=D1=80=D0=B0=D0=BD=D0=B8=D0=BC=D0=B8=D1=80=20=D0=9A?= =?UTF-8?q?=D0=B0=D1=80=D0=B0=D1=9F=D0=B8=D1=9B?= Date: Thu, 15 Aug 2019 21:37:35 -0700 Subject: [PATCH] Cleanup. --- tools/shaderc/shaderc_spirv.cpp | 89 +++++++++++++++++++++++++-------- 1 file changed, 69 insertions(+), 20 deletions(-) diff --git a/tools/shaderc/shaderc_spirv.cpp b/tools/shaderc/shaderc_spirv.cpp index e37b4580e..88d0332ec 100644 --- a/tools/shaderc/shaderc_spirv.cpp +++ b/tools/shaderc/shaderc_spirv.cpp @@ -573,16 +573,23 @@ namespace bgfx { namespace spirv const char* ptr = found.getPtr() + found.getLength(); const char* start = NULL; const char* end = NULL; - while (*ptr != ')' && ptr < _strLine.c_str() + _strLine.size()) + + while (*ptr != ')' + && ptr < _strLine.c_str() + _strLine.size() ) { if (*ptr >= '0' && *ptr <= '9') { if (start == NULL) + { start = ptr; + } + end = ptr; } + ptr++; } + BX_CHECK(start != NULL && end != NULL, "cannot find register number"); bx::StringView numberString(start, end - start + 1); @@ -814,9 +821,16 @@ namespace bgfx { namespace spirv !bx::findIdentifierMatch(strLine.c_str(), "SamplerComparisonState").isEmpty()) { int32_t regNumber = extractStageNumber(strLine); + bx::StringView found = bx::findIdentifierMatch(strLine.c_str(), "SamplerState"); - if (found.isEmpty()) - found = bx::findIdentifierMatch(strLine.c_str(), "SamplerComparisonState"); + + if (found.isEmpty() ) + { + found = bx::findIdentifierMatch( + strLine.c_str() + , "SamplerComparisonState" + ); + } const char* ptr = found.getPtr() + found.getLength(); const char* start = NULL; @@ -826,49 +840,62 @@ namespace bgfx { namespace spirv if (*ptr != ' ') { if (start == NULL) + { start = ptr; + } + end = ptr; } else if (start != NULL) { break; } + ptr++; } + BX_CHECK(start != NULL && end != NULL, "sampler name cannot be found"); std::string samplerName(start, end - start + 1); stageMap[samplerName] = regNumber; } } - else if (!bx::findIdentifierMatch(strLine.c_str(), "StructuredBuffer").isEmpty() || - !bx::findIdentifierMatch(strLine.c_str(), "RWStructuredBuffer").isEmpty()) + else if (!bx::findIdentifierMatch(strLine.c_str(), "StructuredBuffer").isEmpty() + || !bx::findIdentifierMatch(strLine.c_str(), "RWStructuredBuffer").isEmpty() ) { int32_t regNumber = extractStageNumber(strLine); - const char* ptr = strLine.c_str(); + const char* ptr = strLine.c_str(); const char* start = NULL; - const char* end = NULL; + const char* end = NULL; while (ptr < strLine.c_str() + strLine.size()) { if (*ptr == '>') { start = ptr + 1; while (*start == ' ') + { start++; + } } + if (*ptr == ':') { end = ptr - 1; while (*end == ' ') + { end--; + } } + if (start != NULL && end != NULL) { break; } + ptr++; } + BX_CHECK(start != NULL && end != NULL, "sampler name cannot be found"); std::string bufferName(start, end - start + 1); @@ -898,17 +925,21 @@ namespace bgfx { namespace spirv case 0x1404: // GL_INT: un.type = UniformType::Sampler; break; + case 0x8B52: // GL_FLOAT_VEC4: un.type = UniformType::Vec4; break; + case 0x8B5B: // GL_FLOAT_MAT3: un.type = UniformType::Mat3; un.regCount *= 3; break; + case 0x8B5C: // GL_FLOAT_MAT4: un.type = UniformType::Mat4; un.regCount *= 4; break; + default: un.type = UniformType::End; break; @@ -917,6 +948,7 @@ namespace bgfx { namespace spirv uniforms.push_back(un); } } + if (g_verbose) { program->dumpReflection(); @@ -947,7 +979,8 @@ namespace bgfx { namespace spirv opt.SetMessageConsumer(print_msg_to_stderr); opt.RegisterLegalizationPasses(); - if (!opt.Run(spirv.data(), spirv.size(), &spirv)) + + if (!opt.Run(spirv.data(), spirv.size(), &spirv) ) { compiled = false; } @@ -965,7 +998,8 @@ namespace bgfx { namespace spirv for (auto &resource : resourcesrefl.separate_images) { std::string name = refl.get_name(resource.id); - if (name.size() > 7 && 0 == bx::strCmp(name.c_str() + name.length() - 7, "Texture") ) + if (name.size() > 7 + && 0 == bx::strCmp(name.c_str() + name.length() - 7, "Texture") ) { auto uniform_name = name.substr(0, name.length() - 7); @@ -975,14 +1009,16 @@ namespace bgfx { namespace spirv uint32_t texture_binding_index = refl.get_decoration(resource.id, spv::Decoration::DecorationBinding); uint32_t sampler_binding_index = 0; - std::string sampler_name; + std::string sampler_name; + for (auto& sampler_resource : resourcesrefl.separate_samplers) { sampler_name = refl.get_name(sampler_resource.id); - if (sampler_name.size() > 7 && - !bx::strFind(sampler_name.c_str(), uniform_name.c_str()).isEmpty() && - (0 == bx::strCmp(sampler_name.c_str() + name.length() - 7, "Sampler") || - 0 == bx::strCmp(sampler_name.c_str() + name.length() - 7, "SamplerComparison"))) + if (sampler_name.size() > 7 + && !bx::strFind(sampler_name.c_str(), uniform_name.c_str()).isEmpty() + && (0 == bx::strCmp(sampler_name.c_str() + name.length() - 7, "Sampler") || + 0 == bx::strCmp(sampler_name.c_str() + name.length() - 7, "SamplerComparison") + ) ) { sampler_binding_index = refl.get_decoration(sampler_resource.id, spv::Decoration::DecorationBinding); break; @@ -1001,7 +1037,9 @@ namespace bgfx { namespace spirv for (auto &resource : resourcesrefl.storage_images) { std::string name = refl.get_name(resource.id); - if (name.size() > 7 && 0 == bx::strCmp(name.c_str() + name.length() - 7, "Texture") ) + + if (name.size() > 7 + && 0 == bx::strCmp(name.c_str() + name.length() - 7, "Texture") ) { auto uniform_name = name.substr(0, name.length() - 7); uint32_t binding_index = refl.get_decoration(resource.id, spv::Decoration::DecorationBinding); @@ -1022,6 +1060,7 @@ namespace bgfx { namespace spirv for (auto& resource : resourcesrefl.storage_buffers) { std::string name = refl.get_name(resource.id); + for (auto& uniform : uniforms) { if (!bx::strFind(uniform.name.c_str(), name.c_str()).isEmpty()) @@ -1039,7 +1078,7 @@ namespace bgfx { namespace spirv uint16_t size = writeUniformArray( _writer, uniforms, _options.shaderType == 'f'); - if (_version == BX_MAKEFOURCC('M', 'T', 'L', 0)) + if (_version == BX_MAKEFOURCC('M', 'T', 'L', 0) ) { if (g_verbose) { @@ -1051,8 +1090,14 @@ namespace bgfx { namespace spirv spirv_cross::ShaderResources resources = msl.get_shader_resources(); spirv_cross::SmallVector entryPoints = msl.get_entry_points_and_stages(); - if (!entryPoints.empty()) - msl.rename_entry_point(entryPoints[0].name, "xlatMtlMain", entryPoints[0].execution_model); + if (!entryPoints.empty() ) + { + msl.rename_entry_point( + entryPoints[0].name + , "xlatMtlMain" + , entryPoints[0].execution_model + ); + } for (auto &resource : resources.uniform_buffers) { @@ -1068,9 +1113,13 @@ namespace bgfx { namespace spirv 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") ) + 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)); + } } + std::string source = msl.compile(); if ('c' == _options.shaderType) @@ -1096,7 +1145,7 @@ namespace bgfx { namespace spirv uint8_t nul = 0; bx::write(_writer, nul); } - // + const uint8_t numAttr = (uint8_t)program->getNumLiveAttributes(); bx::write(_writer, numAttr);