Skip to content

Commit

Permalink
[shaderc] Clean up (bkaradzic#3258)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
phniix authored and Michael Pekar committed Mar 2, 2024
1 parent fba6745 commit 27b2afa
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 146 deletions.
38 changes: 15 additions & 23 deletions tools/shaderc/shaderc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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");
Expand All @@ -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") )
Expand Down Expand Up @@ -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{});
Expand Down
217 changes: 107 additions & 110 deletions tools/shaderc/shaderc_metal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, ";");
Expand Down Expand Up @@ -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);

Expand Down
Loading

0 comments on commit 27b2afa

Please sign in to comment.