From b321b83c8f998e17fb86a71eac492ebf5666e155 Mon Sep 17 00:00:00 2001 From: Bill Hollings Date: Wed, 6 Jul 2016 20:30:47 -0400 Subject: [PATCH] MSL support textures and samplers as function args. Add automatic sampler func arg when passing SampledImage type. Pass texture and sampler in thread address space. --- spirv_glsl.cpp | 10 +++++++++- spirv_glsl.hpp | 1 + spirv_msl.cpp | 32 +++++++++++++++++++++++++++----- spirv_msl.hpp | 1 + 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/spirv_glsl.cpp b/spirv_glsl.cpp index 34308a1..e666a72 100644 --- a/spirv_glsl.cpp +++ b/spirv_glsl.cpp @@ -1109,6 +1109,14 @@ void CompilerGLSL::emit_resources() statement(""); } +// Returns a string representation of the ID, usable as a function arg. +// Default is to simply return the expression representation fo the arg ID. +// Subclasses may override to modify the return value. +string CompilerGLSL::to_func_call_arg(uint32_t id) +{ + return to_expression(id); +} + string CompilerGLSL::to_expression(uint32_t id) { auto itr = invalid_expressions.find(id); @@ -2691,7 +2699,7 @@ void CompilerGLSL::emit_instruction(const Instruction &instruction) funexpr += to_name(func) + "("; for (uint32_t i = 0; i < length; i++) { - funexpr += to_expression(arg[i]); + funexpr += to_func_call_arg(arg[i]); if (i + 1 < length) funexpr += ", "; } diff --git a/spirv_glsl.hpp b/spirv_glsl.hpp index 226df67..fc06b96 100644 --- a/spirv_glsl.hpp +++ b/spirv_glsl.hpp @@ -147,6 +147,7 @@ protected: virtual std::string constant_expression_vector(const SPIRConstant &c, uint32_t vector); virtual void emit_fixup(); virtual std::string variable_decl(const SPIRType &type, const std::string &name); + virtual std::string to_func_call_arg(uint32_t id); std::unique_ptr buffer; diff --git a/spirv_msl.cpp b/spirv_msl.cpp index 7f5fa2e..ba67bd3 100644 --- a/spirv_msl.cpp +++ b/spirv_msl.cpp @@ -618,7 +618,7 @@ void CompilerMSL::emit_function_prototype(SPIRFunction &func, bool is_decl) { add_local_variable_name(arg.id); - bool is_uniform = false; + bool is_uniform_struct = false; auto *var = maybe_get(arg.id); if (var) { var->parameter = &arg; // Hold a pointer to the parameter so we can invalidate the readonly field if needed. @@ -626,13 +626,20 @@ void CompilerMSL::emit_function_prototype(SPIRFunction &func, bool is_decl) // Check if this arg is one of the synthetic uniform args // created to handle uniform access inside the function auto &var_type = get(var->basetype); - is_uniform = (var_type.storage == StorageClassUniform || - var_type.storage == StorageClassUniformConstant || - var_type.storage == StorageClassPushConstant); + is_uniform_struct = ((var_type.basetype == SPIRType::Struct) && + (var_type.storage == StorageClassUniform || + var_type.storage == StorageClassUniformConstant || + var_type.storage == StorageClassPushConstant)); } - decl += (is_uniform ? "constant " : "thread "); + decl += (is_uniform_struct ? "constant " : "thread "); decl += argument_decl(arg); + + // Manufacture automatic sampler arg for SampledImage texture + auto &arg_type = get(arg.type); + if (arg_type.basetype == SPIRType::SampledImage) + decl += ", thread const sampler& " + to_sampler_expression(arg.id); + if (&arg != &func.arguments.back()) decl += ", "; } @@ -955,6 +962,21 @@ void CompilerMSL::emit_sampled_image_op(uint32_t result_type, uint32_t result_id meta[result_id].sampler = samp_id; } +// Returns a string representation of the ID, usable as a function arg. +// Manufacture automatic sampler arg for SampledImage texture. +string CompilerMSL::to_func_call_arg(uint32_t id) +{ + string arg_str = CompilerGLSL::to_func_call_arg(id); + + // Manufacture automatic sampler arg for SampledImage texture. + auto &var = get(id); + auto &type = get(var.basetype); + if (type.basetype == SPIRType::SampledImage) + arg_str += ", " + to_sampler_expression(id); + + return arg_str; +} + // If the ID represents a sampled image that has been assigned a sampler already, // generate an expression for the sampler, otherwise generate a fake sampler name // by appending a suffix to the expression constructed from the ID. diff --git a/spirv_msl.hpp b/spirv_msl.hpp index 66c2119..bc2e574 100644 --- a/spirv_msl.hpp +++ b/spirv_msl.hpp @@ -107,6 +107,7 @@ protected: std::string member_decl(const SPIRType &type, const SPIRType &member_type, uint32_t member) override; std::string constant_expression(const SPIRConstant &c) override; size_t get_declared_struct_member_size(const SPIRType &struct_type, uint32_t index) const override; + std::string to_func_call_arg(uint32_t id) override; void extract_builtins(); void add_builtin(spv::BuiltIn builtin_type);