Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions impeller/compiler/compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,24 @@
namespace impeller {
namespace compiler {

static std::optional<spv::ExecutionModel> SourceTypeToExecutionModel(
const SourceType& type) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: SourceType is just an enum, so we'd usually pass it by value.

Suggested change
const SourceType& type) {
SourceType type) {

switch (type) {
case SourceType::kUnknown:
return std::nullopt;
case SourceType::kVertexShader:
return spv::ExecutionModel::ExecutionModelVertex;
case SourceType::kFragmentShader:
return spv::ExecutionModel::ExecutionModelFragment;
case SourceType::kTessellationControlShader:
return spv::ExecutionModel::ExecutionModelTessellationControl;
case SourceType::kTessellationEvaluationShader:
return spv::ExecutionModel::ExecutionModelTessellationEvaluation;
case SourceType::kComputeShader:
return spv::ExecutionModel::ExecutionModelGLCompute;
}
}

const uint32_t kFragBindingBase = 128;
const size_t kNumUniformKinds =
static_cast<int>(shaderc_uniform_kind::shaderc_uniform_kind_buffer) + 1;
Expand All @@ -34,7 +52,43 @@ static CompilerBackend CreateMSLCompiler(const spirv_cross::ParsedIR& ir,
// Metal to AIR must be updated as well.
sl_options.msl_version =
spirv_cross::CompilerMSL::Options::make_msl_version(1, 2);
sl_options.enable_decoration_binding = true;
sl_compiler->set_msl_options(sl_options);

// Set metal resource mappings to be consistent with location based mapping
// used on other backends when creating shaders.
std::vector<uint32_t> sampler_offsets;
ir.for_each_typed_id<spirv_cross::SPIRVariable>(
[&](uint32_t, const spirv_cross::SPIRVariable& var) {
if (var.storage != spv::StorageClassUniformConstant) {
return;
}
const auto spir_type = sl_compiler->get_type(var.basetype);
if (spir_type.basetype !=
spirv_cross::SPIRType::BaseType::SampledImage) {
return;
}
auto location = sl_compiler->get_decoration(
var.self, spv::Decoration::DecorationLocation);
sampler_offsets.push_back(location);
});
auto stage = SourceTypeToExecutionModel(source_options.type);
if (stage.has_value() && sampler_offsets.size() > 0) {
auto start_offset =
*std::min_element(sampler_offsets.begin(), sampler_offsets.end());
for (auto offset : sampler_offsets) {
sl_compiler->add_msl_resource_binding({
.stage = stage.value(),
.basetype = spirv_cross::SPIRType::BaseType::SampledImage,
.binding = offset,
.count = 1u,
.msl_buffer = offset - start_offset,
.msl_texture = offset - start_offset,
.msl_sampler = offset - start_offset,
});
}
}

return CompilerBackend(sl_compiler);
}

Expand Down