This commit is contained in:
Бранимир Караџић 2019-08-15 21:37:35 -07:00
parent e501923756
commit 5edeea64a9
1 changed files with 69 additions and 20 deletions

View File

@ -573,16 +573,23 @@ namespace bgfx { namespace spirv
const char* ptr = found.getPtr() + found.getLength(); const char* ptr = found.getPtr() + found.getLength();
const char* start = NULL; const char* start = NULL;
const char* end = 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 (*ptr >= '0' && *ptr <= '9')
{ {
if (start == NULL) if (start == NULL)
{
start = ptr; start = ptr;
}
end = ptr; end = ptr;
} }
ptr++; ptr++;
} }
BX_CHECK(start != NULL && end != NULL, "cannot find register number"); BX_CHECK(start != NULL && end != NULL, "cannot find register number");
bx::StringView numberString(start, end - start + 1); bx::StringView numberString(start, end - start + 1);
@ -814,9 +821,16 @@ namespace bgfx { namespace spirv
!bx::findIdentifierMatch(strLine.c_str(), "SamplerComparisonState").isEmpty()) !bx::findIdentifierMatch(strLine.c_str(), "SamplerComparisonState").isEmpty())
{ {
int32_t regNumber = extractStageNumber(strLine); int32_t regNumber = extractStageNumber(strLine);
bx::StringView found = bx::findIdentifierMatch(strLine.c_str(), "SamplerState"); 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* ptr = found.getPtr() + found.getLength();
const char* start = NULL; const char* start = NULL;
@ -826,49 +840,62 @@ namespace bgfx { namespace spirv
if (*ptr != ' ') if (*ptr != ' ')
{ {
if (start == NULL) if (start == NULL)
{
start = ptr; start = ptr;
}
end = ptr; end = ptr;
} }
else if (start != NULL) else if (start != NULL)
{ {
break; break;
} }
ptr++; ptr++;
} }
BX_CHECK(start != NULL && end != NULL, "sampler name cannot be found"); BX_CHECK(start != NULL && end != NULL, "sampler name cannot be found");
std::string samplerName(start, end - start + 1); std::string samplerName(start, end - start + 1);
stageMap[samplerName] = regNumber; stageMap[samplerName] = regNumber;
} }
} }
else if (!bx::findIdentifierMatch(strLine.c_str(), "StructuredBuffer").isEmpty() || else if (!bx::findIdentifierMatch(strLine.c_str(), "StructuredBuffer").isEmpty()
!bx::findIdentifierMatch(strLine.c_str(), "RWStructuredBuffer").isEmpty()) || !bx::findIdentifierMatch(strLine.c_str(), "RWStructuredBuffer").isEmpty() )
{ {
int32_t regNumber = extractStageNumber(strLine); int32_t regNumber = extractStageNumber(strLine);
const char* ptr = strLine.c_str(); const char* ptr = strLine.c_str();
const char* start = NULL; const char* start = NULL;
const char* end = NULL; const char* end = NULL;
while (ptr < strLine.c_str() + strLine.size()) while (ptr < strLine.c_str() + strLine.size())
{ {
if (*ptr == '>') if (*ptr == '>')
{ {
start = ptr + 1; start = ptr + 1;
while (*start == ' ') while (*start == ' ')
{
start++; start++;
}
} }
if (*ptr == ':') if (*ptr == ':')
{ {
end = ptr - 1; end = ptr - 1;
while (*end == ' ') while (*end == ' ')
{
end--; end--;
}
} }
if (start != NULL && end != NULL) if (start != NULL && end != NULL)
{ {
break; break;
} }
ptr++; ptr++;
} }
BX_CHECK(start != NULL && end != NULL, "sampler name cannot be found"); BX_CHECK(start != NULL && end != NULL, "sampler name cannot be found");
std::string bufferName(start, end - start + 1); std::string bufferName(start, end - start + 1);
@ -898,17 +925,21 @@ namespace bgfx { namespace spirv
case 0x1404: // GL_INT: case 0x1404: // GL_INT:
un.type = UniformType::Sampler; un.type = UniformType::Sampler;
break; break;
case 0x8B52: // GL_FLOAT_VEC4: case 0x8B52: // GL_FLOAT_VEC4:
un.type = UniformType::Vec4; un.type = UniformType::Vec4;
break; break;
case 0x8B5B: // GL_FLOAT_MAT3: case 0x8B5B: // GL_FLOAT_MAT3:
un.type = UniformType::Mat3; un.type = UniformType::Mat3;
un.regCount *= 3; un.regCount *= 3;
break; break;
case 0x8B5C: // GL_FLOAT_MAT4: case 0x8B5C: // GL_FLOAT_MAT4:
un.type = UniformType::Mat4; un.type = UniformType::Mat4;
un.regCount *= 4; un.regCount *= 4;
break; break;
default: default:
un.type = UniformType::End; un.type = UniformType::End;
break; break;
@ -917,6 +948,7 @@ namespace bgfx { namespace spirv
uniforms.push_back(un); uniforms.push_back(un);
} }
} }
if (g_verbose) if (g_verbose)
{ {
program->dumpReflection(); program->dumpReflection();
@ -947,7 +979,8 @@ namespace bgfx { namespace spirv
opt.SetMessageConsumer(print_msg_to_stderr); opt.SetMessageConsumer(print_msg_to_stderr);
opt.RegisterLegalizationPasses(); opt.RegisterLegalizationPasses();
if (!opt.Run(spirv.data(), spirv.size(), &spirv))
if (!opt.Run(spirv.data(), spirv.size(), &spirv) )
{ {
compiled = false; compiled = false;
} }
@ -965,7 +998,8 @@ namespace bgfx { namespace spirv
for (auto &resource : resourcesrefl.separate_images) for (auto &resource : resourcesrefl.separate_images)
{ {
std::string name = refl.get_name(resource.id); 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); 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 texture_binding_index = refl.get_decoration(resource.id, spv::Decoration::DecorationBinding);
uint32_t sampler_binding_index = 0; uint32_t sampler_binding_index = 0;
std::string sampler_name; std::string sampler_name;
for (auto& sampler_resource : resourcesrefl.separate_samplers) for (auto& sampler_resource : resourcesrefl.separate_samplers)
{ {
sampler_name = refl.get_name(sampler_resource.id); sampler_name = refl.get_name(sampler_resource.id);
if (sampler_name.size() > 7 && if (sampler_name.size() > 7
!bx::strFind(sampler_name.c_str(), uniform_name.c_str()).isEmpty() && && !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, "Sampler") ||
0 == bx::strCmp(sampler_name.c_str() + name.length() - 7, "SamplerComparison"))) 0 == bx::strCmp(sampler_name.c_str() + name.length() - 7, "SamplerComparison")
) )
{ {
sampler_binding_index = refl.get_decoration(sampler_resource.id, spv::Decoration::DecorationBinding); sampler_binding_index = refl.get_decoration(sampler_resource.id, spv::Decoration::DecorationBinding);
break; break;
@ -1001,7 +1037,9 @@ namespace bgfx { namespace spirv
for (auto &resource : resourcesrefl.storage_images) for (auto &resource : resourcesrefl.storage_images)
{ {
std::string name = refl.get_name(resource.id); 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); auto uniform_name = name.substr(0, name.length() - 7);
uint32_t binding_index = refl.get_decoration(resource.id, spv::Decoration::DecorationBinding); 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) for (auto& resource : resourcesrefl.storage_buffers)
{ {
std::string name = refl.get_name(resource.id); std::string name = refl.get_name(resource.id);
for (auto& uniform : uniforms) for (auto& uniform : uniforms)
{ {
if (!bx::strFind(uniform.name.c_str(), name.c_str()).isEmpty()) 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'); 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) if (g_verbose)
{ {
@ -1051,8 +1090,14 @@ namespace bgfx { namespace spirv
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(); spirv_cross::SmallVector<spirv_cross::EntryPoint> entryPoints = msl.get_entry_points_and_stages();
if (!entryPoints.empty()) if (!entryPoints.empty() )
msl.rename_entry_point(entryPoints[0].name, "xlatMtlMain", entryPoints[0].execution_model); {
msl.rename_entry_point(
entryPoints[0].name
, "xlatMtlMain"
, entryPoints[0].execution_model
);
}
for (auto &resource : resources.uniform_buffers) for (auto &resource : resources.uniform_buffers)
{ {
@ -1068,9 +1113,13 @@ namespace bgfx { namespace spirv
for (auto &resource : resources.separate_images) for (auto &resource : resources.separate_images)
{ {
std::string name = msl.get_name(resource.id); 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)); msl.set_name(resource.id, name.substr(0, name.length() - 7));
}
} }
std::string source = msl.compile(); std::string source = msl.compile();
if ('c' == _options.shaderType) if ('c' == _options.shaderType)
@ -1096,7 +1145,7 @@ namespace bgfx { namespace spirv
uint8_t nul = 0; uint8_t nul = 0;
bx::write(_writer, nul); bx::write(_writer, nul);
} }
//
const uint8_t numAttr = (uint8_t)program->getNumLiveAttributes(); const uint8_t numAttr = (uint8_t)program->getNumLiveAttributes();
bx::write(_writer, numAttr); bx::write(_writer, numAttr);