2016-04-07 00:42:27 +03:00
|
|
|
/*
|
2017-01-26 06:33:32 +03:00
|
|
|
* Copyright 2016-2017 The Brenwill Workshop Ltd.
|
2016-04-07 00:42:27 +03:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "spirv_msl.hpp"
|
2016-10-28 01:47:17 +03:00
|
|
|
#include "GLSL.std.450.h"
|
2017-01-27 03:40:56 +03:00
|
|
|
|
2016-04-07 00:42:27 +03:00
|
|
|
#include <algorithm>
|
2017-01-27 03:40:56 +03:00
|
|
|
#include <cassert>
|
2016-04-07 00:42:27 +03:00
|
|
|
#include <numeric>
|
|
|
|
|
|
|
|
using namespace spv;
|
|
|
|
using namespace spirv_cross;
|
|
|
|
using namespace std;
|
|
|
|
|
2017-01-29 21:28:20 +03:00
|
|
|
static const uint32_t k_unknown_location = ~0;
|
|
|
|
|
2016-05-05 10:33:18 +03:00
|
|
|
CompilerMSL::CompilerMSL(vector<uint32_t> spirv_)
|
|
|
|
: CompilerGLSL(move(spirv_))
|
|
|
|
{
|
|
|
|
options.vertex.fixup_clipspace = false;
|
2017-01-08 06:15:58 +03:00
|
|
|
|
2017-01-08 16:52:57 +03:00
|
|
|
populate_func_name_overrides();
|
2017-01-23 12:00:40 +03:00
|
|
|
populate_var_name_overrides();
|
2017-01-08 06:15:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Populate the collection of function names that need to be overridden
|
|
|
|
void CompilerMSL::populate_func_name_overrides()
|
|
|
|
{
|
2017-01-08 16:52:57 +03:00
|
|
|
func_name_overrides["main"] = "main0";
|
|
|
|
func_name_overrides["saturate"] = "saturate0";
|
2016-04-08 04:25:51 +03:00
|
|
|
}
|
|
|
|
|
2017-01-23 12:00:40 +03:00
|
|
|
void CompilerMSL::populate_var_name_overrides()
|
|
|
|
{
|
|
|
|
var_name_overrides["kernel"] = "kernel0";
|
|
|
|
var_name_overrides["bias"] = "bias0";
|
|
|
|
}
|
|
|
|
|
2016-05-05 10:33:18 +03:00
|
|
|
string CompilerMSL::compile(MSLConfiguration &msl_cfg, vector<MSLVertexAttr> *p_vtx_attrs,
|
|
|
|
std::vector<MSLResourceBinding> *p_res_bindings)
|
2016-04-08 04:25:51 +03:00
|
|
|
{
|
2017-01-11 17:57:05 +03:00
|
|
|
// Force a classic "C" locale, reverts when function returns
|
|
|
|
ClassicLocale classic_locale;
|
|
|
|
|
2017-01-01 22:43:20 +03:00
|
|
|
// Remember the input parameters
|
2016-05-05 10:33:18 +03:00
|
|
|
msl_config = msl_cfg;
|
|
|
|
|
2017-01-23 14:04:12 +03:00
|
|
|
// Set main function name if it was explicitly set
|
|
|
|
if (!msl_config.entry_point_name.empty())
|
|
|
|
set_name(entry_point, msl_config.entry_point_name);
|
|
|
|
|
2016-05-05 10:33:18 +03:00
|
|
|
vtx_attrs_by_location.clear();
|
|
|
|
if (p_vtx_attrs)
|
|
|
|
for (auto &va : *p_vtx_attrs)
|
|
|
|
vtx_attrs_by_location[va.location] = &va;
|
|
|
|
|
2017-01-29 21:28:20 +03:00
|
|
|
non_stage_in_input_var_ids.clear();
|
|
|
|
pad_type_ids_by_pad_len.clear();
|
|
|
|
|
2016-05-05 10:33:18 +03:00
|
|
|
resource_bindings.clear();
|
|
|
|
if (p_res_bindings)
|
|
|
|
for (auto &rb : *p_res_bindings)
|
|
|
|
resource_bindings.push_back(&rb);
|
|
|
|
|
2017-01-20 19:24:44 +03:00
|
|
|
// Preprocess OpCodes to extract the need to output additional header content
|
2016-12-19 02:48:15 +03:00
|
|
|
set_enabled_interface_variables(get_active_interface_variables());
|
2017-01-20 19:24:44 +03:00
|
|
|
preprocess_op_codes();
|
2017-01-01 22:43:20 +03:00
|
|
|
|
2017-01-15 18:14:21 +03:00
|
|
|
// Create structs to hold input, output and uniform variables
|
2017-01-01 22:43:20 +03:00
|
|
|
qual_pos_var_name = "";
|
|
|
|
stage_in_var_id = add_interface_block(StorageClassInput);
|
|
|
|
stage_out_var_id = add_interface_block(StorageClassOutput);
|
2017-01-15 18:14:21 +03:00
|
|
|
stage_uniforms_var_id = add_interface_block(StorageClassUniformConstant);
|
2017-01-01 22:43:20 +03:00
|
|
|
|
|
|
|
// Convert the use of global variables to recursively-passed function parameters
|
2016-05-05 10:33:18 +03:00
|
|
|
localize_global_variables();
|
2016-10-24 16:24:24 +03:00
|
|
|
extract_global_variables_from_functions();
|
2016-05-05 10:33:18 +03:00
|
|
|
|
2017-01-01 22:43:20 +03:00
|
|
|
// Do not deal with GLES-isms like precision, older extensions and such.
|
2016-05-05 10:33:18 +03:00
|
|
|
options.es = false;
|
2016-10-28 01:47:17 +03:00
|
|
|
options.version = 120;
|
2016-05-05 10:33:18 +03:00
|
|
|
backend.float_literal_suffix = false;
|
|
|
|
backend.uint32_t_literal_suffix = true;
|
|
|
|
backend.basic_int_type = "int";
|
|
|
|
backend.basic_uint_type = "uint";
|
2016-10-27 17:20:01 +03:00
|
|
|
backend.discard_literal = "discard_fragment()";
|
2016-05-05 10:33:18 +03:00
|
|
|
backend.swizzle_is_function = false;
|
|
|
|
backend.shared_is_implied = false;
|
2016-12-14 10:12:52 +03:00
|
|
|
backend.native_row_major_matrix = false;
|
2016-05-05 10:33:18 +03:00
|
|
|
|
|
|
|
uint32_t pass_count = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (pass_count >= 3)
|
2016-12-13 00:33:22 +03:00
|
|
|
SPIRV_CROSS_THROW("Over 3 compilation loops detected. Must be a bug!");
|
2016-05-05 10:33:18 +03:00
|
|
|
|
|
|
|
reset();
|
|
|
|
|
2016-09-17 12:30:58 +03:00
|
|
|
next_metal_resource_index = MSLResourceBinding(); // Start bindings at zero
|
2016-09-17 19:46:10 +03:00
|
|
|
|
2016-05-05 10:33:18 +03:00
|
|
|
// Move constructor for this type is broken on GCC 4.9 ...
|
|
|
|
buffer = unique_ptr<ostringstream>(new ostringstream());
|
|
|
|
|
|
|
|
emit_header();
|
|
|
|
emit_resources();
|
2016-12-19 05:42:10 +03:00
|
|
|
emit_custom_functions();
|
2016-07-28 12:16:02 +03:00
|
|
|
emit_function(get<SPIRFunction>(entry_point), 0);
|
2016-05-05 10:33:18 +03:00
|
|
|
|
|
|
|
pass_count++;
|
|
|
|
} while (force_recompile);
|
|
|
|
|
|
|
|
return buffer->str();
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
2016-04-08 04:25:51 +03:00
|
|
|
string CompilerMSL::compile()
|
2016-04-07 00:42:27 +03:00
|
|
|
{
|
2016-05-05 10:33:18 +03:00
|
|
|
MSLConfiguration default_msl_cfg;
|
|
|
|
return compile(default_msl_cfg, nullptr, nullptr);
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
2016-12-19 05:42:10 +03:00
|
|
|
// Register the need to output any custom functions.
|
2017-01-20 19:24:44 +03:00
|
|
|
void CompilerMSL::preprocess_op_codes()
|
2016-10-24 16:24:24 +03:00
|
|
|
{
|
2016-12-19 05:42:10 +03:00
|
|
|
custom_function_ops.clear();
|
2017-01-20 19:24:44 +03:00
|
|
|
|
2017-01-20 19:33:59 +03:00
|
|
|
OpCodePreprocessor preproc(*this);
|
2017-01-20 19:24:44 +03:00
|
|
|
traverse_all_reachable_opcodes(get<SPIRFunction>(entry_point), preproc);
|
|
|
|
|
2017-01-20 19:33:59 +03:00
|
|
|
if (preproc.suppress_missing_prototypes)
|
|
|
|
add_header_line("#pragma clang diagnostic ignored \"-Wmissing-prototypes\"");
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
2016-04-27 20:54:33 +03:00
|
|
|
// Move the Private global variables to the entry function.
|
|
|
|
// Non-constant variables cannot have global scope in Metal.
|
|
|
|
void CompilerMSL::localize_global_variables()
|
|
|
|
{
|
2016-07-28 12:16:02 +03:00
|
|
|
auto &entry_func = get<SPIRFunction>(entry_point);
|
2016-05-05 10:33:18 +03:00
|
|
|
auto iter = global_variables.begin();
|
|
|
|
while (iter != global_variables.end())
|
|
|
|
{
|
|
|
|
uint32_t gv_id = *iter;
|
|
|
|
auto &gbl_var = get<SPIRVariable>(gv_id);
|
|
|
|
if (gbl_var.storage == StorageClassPrivate)
|
|
|
|
{
|
|
|
|
entry_func.add_local_variable(gv_id);
|
2017-01-16 11:44:40 +03:00
|
|
|
iter = global_variables.erase(iter);
|
2016-05-05 10:33:18 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
iter++;
|
|
|
|
}
|
2016-07-06 23:55:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// For any global variable accessed directly by a function,
|
|
|
|
// extract that variable and add it as an argument to that function.
|
|
|
|
void CompilerMSL::extract_global_variables_from_functions()
|
|
|
|
{
|
|
|
|
|
2016-10-24 16:24:24 +03:00
|
|
|
// Uniforms
|
2016-12-19 02:48:15 +03:00
|
|
|
std::unordered_set<uint32_t> global_var_ids;
|
2016-10-24 16:24:24 +03:00
|
|
|
for (auto &id : ids)
|
|
|
|
{
|
|
|
|
if (id.get_type() == TypeVariable)
|
|
|
|
{
|
|
|
|
auto &var = id.get<SPIRVariable>();
|
|
|
|
if (var.storage == StorageClassInput || var.storage == StorageClassUniform ||
|
2017-01-15 18:39:03 +03:00
|
|
|
var.storage == StorageClassUniformConstant || var.storage == StorageClassPushConstant)
|
|
|
|
{
|
2016-10-24 16:24:24 +03:00
|
|
|
global_var_ids.insert(var.self);
|
2017-01-09 18:23:58 +03:00
|
|
|
}
|
2016-10-24 16:24:24 +03:00
|
|
|
}
|
|
|
|
}
|
2017-01-26 12:20:42 +03:00
|
|
|
|
2017-01-16 11:44:40 +03:00
|
|
|
// Local vars that are declared in the main function and accessed directy by a function
|
|
|
|
auto &entry_func = get<SPIRFunction>(entry_point);
|
2017-01-26 22:05:43 +03:00
|
|
|
for (auto &var : entry_func.local_variables)
|
|
|
|
global_var_ids.insert(var);
|
2016-10-24 16:24:24 +03:00
|
|
|
|
2017-01-31 19:02:44 +03:00
|
|
|
std::set<uint32_t> added_arg_ids;
|
2016-12-19 02:48:15 +03:00
|
|
|
std::unordered_set<uint32_t> processed_func_ids;
|
2016-10-24 16:24:24 +03:00
|
|
|
extract_global_variables_from_function(entry_point, added_arg_ids, global_var_ids, processed_func_ids);
|
2016-07-06 23:55:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// MSL does not support the use of global variables for shader input content.
|
|
|
|
// For any global variable accessed directly by the specified function, extract that variable,
|
|
|
|
// add it as an argument to that function, and the arg to the added_arg_ids collection.
|
2017-01-31 19:02:44 +03:00
|
|
|
void CompilerMSL::extract_global_variables_from_function(uint32_t func_id, std::set<uint32_t> &added_arg_ids,
|
2016-12-19 02:48:15 +03:00
|
|
|
std::unordered_set<uint32_t> &global_var_ids,
|
|
|
|
std::unordered_set<uint32_t> &processed_func_ids)
|
2016-07-06 23:55:45 +03:00
|
|
|
{
|
2016-10-24 16:24:24 +03:00
|
|
|
// Avoid processing a function more than once
|
2017-01-15 18:39:03 +03:00
|
|
|
if (processed_func_ids.find(func_id) != processed_func_ids.end())
|
|
|
|
{
|
2017-01-09 18:23:58 +03:00
|
|
|
// Return function global variables
|
|
|
|
added_arg_ids = function_global_vars[func_id];
|
2016-10-24 16:24:24 +03:00
|
|
|
return;
|
2017-01-09 18:23:58 +03:00
|
|
|
}
|
2016-10-24 16:24:24 +03:00
|
|
|
|
|
|
|
processed_func_ids.insert(func_id);
|
|
|
|
|
|
|
|
auto &func = get<SPIRFunction>(func_id);
|
|
|
|
|
|
|
|
// Recursively establish global args added to functions on which we depend.
|
|
|
|
for (auto block : func.blocks)
|
|
|
|
{
|
|
|
|
auto &b = get<SPIRBlock>(block);
|
|
|
|
for (auto &i : b.ops)
|
|
|
|
{
|
|
|
|
auto ops = stream(i);
|
|
|
|
auto op = static_cast<Op>(i.op);
|
|
|
|
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case OpLoad:
|
|
|
|
case OpAccessChain:
|
|
|
|
{
|
|
|
|
uint32_t base_id = ops[2];
|
|
|
|
if (global_var_ids.find(base_id) != global_var_ids.end())
|
|
|
|
added_arg_ids.insert(base_id);
|
2017-01-15 18:39:03 +03:00
|
|
|
|
2016-10-24 16:24:24 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OpFunctionCall:
|
|
|
|
{
|
|
|
|
uint32_t inner_func_id = ops[2];
|
2017-01-31 19:02:44 +03:00
|
|
|
std::set<uint32_t> inner_func_args;
|
2016-10-24 16:24:24 +03:00
|
|
|
extract_global_variables_from_function(inner_func_id, inner_func_args, global_var_ids,
|
|
|
|
processed_func_ids);
|
|
|
|
added_arg_ids.insert(inner_func_args.begin(), inner_func_args.end());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-15 18:39:03 +03:00
|
|
|
|
|
|
|
function_global_vars[func_id] = added_arg_ids;
|
2016-10-24 16:24:24 +03:00
|
|
|
|
|
|
|
// Add the global variables as arguments to the function
|
|
|
|
if (func_id != entry_point)
|
|
|
|
{
|
2016-10-27 17:20:01 +03:00
|
|
|
uint32_t next_id = increase_bound_by(uint32_t(added_arg_ids.size()));
|
2016-10-24 16:24:24 +03:00
|
|
|
for (uint32_t arg_id : added_arg_ids)
|
|
|
|
{
|
|
|
|
uint32_t type_id = get<SPIRVariable>(arg_id).basetype;
|
2017-02-05 12:50:14 +03:00
|
|
|
func.add_parameter(type_id, next_id, true);
|
2016-10-24 16:24:24 +03:00
|
|
|
set<SPIRVariable>(next_id, type_id, StorageClassFunction);
|
2016-11-27 23:00:06 +03:00
|
|
|
|
|
|
|
// Ensure both the existing and new variables have the same name, and the name is valid
|
|
|
|
string vld_name = ensure_valid_name(to_name(arg_id), "v");
|
|
|
|
set_name(arg_id, vld_name);
|
|
|
|
set_name(next_id, vld_name);
|
|
|
|
|
2016-10-24 16:24:24 +03:00
|
|
|
meta[next_id].decoration.qualified_alias = meta[arg_id].decoration.qualified_alias;
|
|
|
|
next_id++;
|
|
|
|
}
|
|
|
|
}
|
2016-04-27 20:54:33 +03:00
|
|
|
}
|
|
|
|
|
2017-01-01 22:43:20 +03:00
|
|
|
// If a vertex attribute exists at the location, it is marked as being used by this shader
|
|
|
|
void CompilerMSL::mark_location_as_used_by_shader(uint32_t location, StorageClass storage)
|
2016-04-07 00:42:27 +03:00
|
|
|
{
|
2017-01-01 22:43:20 +03:00
|
|
|
MSLVertexAttr *p_va;
|
2016-07-28 12:16:02 +03:00
|
|
|
auto &execution = get_entry_point();
|
2017-01-01 22:43:20 +03:00
|
|
|
if ((execution.model == ExecutionModelVertex) && (storage == StorageClassInput) &&
|
|
|
|
(p_va = vtx_attrs_by_location[location]))
|
|
|
|
p_va->used_by_shader = true;
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
2017-01-01 22:43:20 +03:00
|
|
|
// Add an interface structure for the type of storage, which is either StorageClassInput or StorageClassOutput.
|
2016-04-07 00:42:27 +03:00
|
|
|
// Returns the ID of the newly added variable, or zero if no variable was added.
|
2017-01-01 22:43:20 +03:00
|
|
|
uint32_t CompilerMSL::add_interface_block(StorageClass storage)
|
2016-04-07 00:42:27 +03:00
|
|
|
{
|
2016-05-05 10:33:18 +03:00
|
|
|
// Accumulate the variables that should appear in the interface struct
|
|
|
|
vector<SPIRVariable *> vars;
|
2017-01-01 22:43:20 +03:00
|
|
|
bool incl_builtins = (storage == StorageClassOutput);
|
2016-05-05 10:33:18 +03:00
|
|
|
for (auto &id : ids)
|
|
|
|
{
|
|
|
|
if (id.get_type() == TypeVariable)
|
|
|
|
{
|
|
|
|
auto &var = id.get<SPIRVariable>();
|
|
|
|
auto &type = get<SPIRType>(var.basetype);
|
2016-07-28 12:16:02 +03:00
|
|
|
if (var.storage == storage && interface_variable_exists_in_entry_point(var.self) &&
|
2017-01-01 22:43:20 +03:00
|
|
|
!is_hidden_variable(var, incl_builtins) && type.pointer)
|
2016-05-05 10:33:18 +03:00
|
|
|
{
|
|
|
|
vars.push_back(&var);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-18 01:07:53 +03:00
|
|
|
// If no variables qualify, leave
|
2016-05-05 10:33:18 +03:00
|
|
|
if (vars.empty())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Add a new typed variable for this interface structure.
|
|
|
|
// The initializer expression is allocated here, but populated when the function
|
|
|
|
// declaraion is emitted, because it is cleared after each compilation pass.
|
|
|
|
uint32_t next_id = increase_bound_by(3);
|
|
|
|
uint32_t ib_type_id = next_id++;
|
|
|
|
auto &ib_type = set<SPIRType>(ib_type_id);
|
|
|
|
ib_type.basetype = SPIRType::Struct;
|
|
|
|
ib_type.storage = storage;
|
2016-12-19 02:48:15 +03:00
|
|
|
set_decoration(ib_type_id, DecorationBlock);
|
2016-05-05 10:33:18 +03:00
|
|
|
|
|
|
|
uint32_t ib_var_id = next_id++;
|
|
|
|
auto &var = set<SPIRVariable>(ib_var_id, ib_type_id, storage, 0);
|
|
|
|
var.initializer = next_id++;
|
|
|
|
|
|
|
|
string ib_var_ref;
|
2017-01-01 22:43:20 +03:00
|
|
|
switch (storage)
|
2016-12-04 20:32:58 +03:00
|
|
|
{
|
2017-01-01 22:43:20 +03:00
|
|
|
case StorageClassInput:
|
2016-12-04 20:32:58 +03:00
|
|
|
ib_var_ref = stage_in_var_name;
|
2017-01-01 22:43:20 +03:00
|
|
|
break;
|
2016-12-04 20:32:58 +03:00
|
|
|
|
2017-01-01 22:43:20 +03:00
|
|
|
case StorageClassOutput:
|
2016-05-05 10:33:18 +03:00
|
|
|
{
|
|
|
|
ib_var_ref = stage_out_var_name;
|
|
|
|
|
|
|
|
// Add the output interface struct as a local variable to the entry function,
|
|
|
|
// and force the entry function to return the output interface struct from
|
|
|
|
// any blocks that perform a function return.
|
2016-07-28 12:16:02 +03:00
|
|
|
auto &entry_func = get<SPIRFunction>(entry_point);
|
2016-05-05 10:33:18 +03:00
|
|
|
entry_func.add_local_variable(ib_var_id);
|
|
|
|
for (auto &blk_id : entry_func.blocks)
|
|
|
|
{
|
|
|
|
auto &blk = get<SPIRBlock>(blk_id);
|
|
|
|
if (blk.terminator == SPIRBlock::Return)
|
|
|
|
blk.return_value = ib_var_id;
|
|
|
|
}
|
2017-01-01 22:43:20 +03:00
|
|
|
break;
|
|
|
|
}
|
2017-01-15 18:39:03 +03:00
|
|
|
|
2017-01-15 18:14:21 +03:00
|
|
|
case StorageClassUniformConstant:
|
|
|
|
{
|
|
|
|
ib_var_ref = stage_uniform_var_name;
|
2017-01-16 14:42:26 +03:00
|
|
|
break;
|
2017-01-15 18:14:21 +03:00
|
|
|
}
|
2017-01-01 22:43:20 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2016-05-05 10:33:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
set_name(ib_type_id, get_entry_point_name() + "_" + ib_var_ref);
|
|
|
|
set_name(ib_var_id, ib_var_ref);
|
|
|
|
|
|
|
|
for (auto p_var : vars)
|
|
|
|
{
|
2016-12-18 01:07:53 +03:00
|
|
|
uint32_t type_id = p_var->basetype;
|
|
|
|
auto &type = get<SPIRType>(type_id);
|
2016-10-24 16:24:24 +03:00
|
|
|
if (type.basetype == SPIRType::Struct)
|
2016-05-05 10:33:18 +03:00
|
|
|
{
|
|
|
|
// Flatten the struct members into the interface struct
|
2017-01-01 22:43:20 +03:00
|
|
|
uint32_t mbr_idx = 0;
|
2017-01-29 21:28:20 +03:00
|
|
|
for (auto &mbr_type_id : type.member_types)
|
2016-05-05 10:33:18 +03:00
|
|
|
{
|
2017-01-29 21:28:20 +03:00
|
|
|
auto &mbr_type = get<SPIRType>(mbr_type_id);
|
|
|
|
if (is_matrix(mbr_type))
|
|
|
|
{
|
|
|
|
exclude_member_from_stage_in(type, mbr_idx);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Add a reference to the member to the interface struct.
|
|
|
|
uint32_t ib_mbr_idx = uint32_t(ib_type.member_types.size());
|
|
|
|
ib_type.member_types.push_back(mbr_type_id); // membertype.self is different for array types
|
2016-05-05 10:33:18 +03:00
|
|
|
|
2017-01-29 21:28:20 +03:00
|
|
|
// Give the member a name
|
|
|
|
string mbr_name = ensure_valid_name(to_qualified_member_name(type, mbr_idx), "m");
|
|
|
|
set_member_name(ib_type_id, ib_mbr_idx, mbr_name);
|
2016-05-05 10:33:18 +03:00
|
|
|
|
2017-01-29 21:28:20 +03:00
|
|
|
// Update the original variable reference to include the structure reference
|
|
|
|
string qual_var_name = ib_var_ref + "." + mbr_name;
|
|
|
|
set_member_qualified_name(type_id, mbr_idx, qual_var_name);
|
2016-05-05 10:33:18 +03:00
|
|
|
|
2017-01-29 21:28:20 +03:00
|
|
|
// Copy the variable location from the original variable to the member
|
|
|
|
if (get_member_decoration_mask(type_id, mbr_idx) & (1ull << DecorationLocation))
|
|
|
|
{
|
|
|
|
uint32_t locn = get_member_decoration(type_id, mbr_idx, DecorationLocation);
|
|
|
|
set_member_decoration(ib_type_id, ib_mbr_idx, DecorationLocation, locn);
|
|
|
|
mark_location_as_used_by_shader(locn, storage);
|
|
|
|
}
|
2016-05-05 10:33:18 +03:00
|
|
|
|
2017-01-29 21:28:20 +03:00
|
|
|
// Mark the member as builtin if needed
|
|
|
|
BuiltIn builtin;
|
|
|
|
if (is_member_builtin(type, mbr_idx, &builtin))
|
|
|
|
{
|
|
|
|
set_member_decoration(ib_type_id, ib_mbr_idx, DecorationBuiltIn, builtin);
|
|
|
|
if (builtin == BuiltInPosition)
|
|
|
|
qual_pos_var_name = qual_var_name;
|
|
|
|
}
|
2016-05-05 10:33:18 +03:00
|
|
|
}
|
2017-01-01 22:43:20 +03:00
|
|
|
mbr_idx++;
|
2016-05-05 10:33:18 +03:00
|
|
|
}
|
|
|
|
}
|
2017-01-15 18:39:03 +03:00
|
|
|
else if (type.basetype == SPIRType::Boolean || type.basetype == SPIRType::Char ||
|
|
|
|
type.basetype == SPIRType::Int || type.basetype == SPIRType::UInt ||
|
|
|
|
type.basetype == SPIRType::Int64 || type.basetype == SPIRType::UInt64 ||
|
|
|
|
type.basetype == SPIRType::Float || type.basetype == SPIRType::Double ||
|
|
|
|
type.basetype == SPIRType::Boolean)
|
2016-05-05 10:33:18 +03:00
|
|
|
{
|
2017-01-29 21:28:20 +03:00
|
|
|
if (is_matrix(type))
|
2017-01-26 12:20:42 +03:00
|
|
|
{
|
2017-01-29 21:28:20 +03:00
|
|
|
exclude_from_stage_in(*p_var);
|
2017-01-15 18:14:21 +03:00
|
|
|
}
|
2017-01-29 21:28:20 +03:00
|
|
|
else
|
2016-05-05 10:33:18 +03:00
|
|
|
{
|
2017-01-29 21:28:20 +03:00
|
|
|
// Add a reference to the variable type to the interface struct.
|
|
|
|
uint32_t ib_mbr_idx = uint32_t(ib_type.member_types.size());
|
|
|
|
ib_type.member_types.push_back(type_id);
|
|
|
|
|
|
|
|
// Give the member a name
|
|
|
|
string mbr_name = ensure_valid_name(to_expression(p_var->self), "m");
|
|
|
|
set_member_name(ib_type_id, ib_mbr_idx, mbr_name);
|
|
|
|
|
|
|
|
// Update the original variable reference to include the structure reference
|
|
|
|
string qual_var_name = ib_var_ref + "." + mbr_name;
|
|
|
|
meta[p_var->self].decoration.qualified_alias = qual_var_name;
|
|
|
|
|
|
|
|
// Copy the variable location from the original variable to the member
|
|
|
|
if (get_decoration_mask(p_var->self) & (1ull << DecorationLocation))
|
|
|
|
{
|
|
|
|
uint32_t locn = get_decoration(p_var->self, DecorationLocation);
|
|
|
|
set_member_decoration(ib_type_id, ib_mbr_idx, DecorationLocation, locn);
|
|
|
|
mark_location_as_used_by_shader(locn, storage);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark the member as builtin if needed
|
|
|
|
if (is_builtin_variable(*p_var))
|
|
|
|
{
|
|
|
|
uint32_t builtin = get_decoration(p_var->self, DecorationBuiltIn);
|
|
|
|
set_member_decoration(ib_type_id, ib_mbr_idx, DecorationBuiltIn, builtin);
|
|
|
|
if (builtin == BuiltInPosition)
|
|
|
|
qual_pos_var_name = qual_var_name;
|
|
|
|
}
|
2016-05-05 10:33:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-19 02:48:15 +03:00
|
|
|
// Sort the members of the interface structure by their attribute numbers.
|
|
|
|
// Oddly, Metal handles inputs better if they are sorted in reverse order,
|
|
|
|
// particularly if the offsets are all equal.
|
|
|
|
MemberSorter::SortAspect sort_aspect =
|
|
|
|
(storage == StorageClassInput) ? MemberSorter::LocationReverse : MemberSorter::Location;
|
2017-01-28 10:58:25 +03:00
|
|
|
MemberSorter member_sorter(ib_type, meta[ib_type_id], sort_aspect);
|
|
|
|
member_sorter.sort();
|
2017-01-15 18:39:03 +03:00
|
|
|
|
|
|
|
// Sort input or output variables alphabetical
|
2017-01-15 18:14:21 +03:00
|
|
|
auto &execution = get_entry_point();
|
|
|
|
if ((execution.model == ExecutionModelFragment && storage == StorageClassInput) ||
|
2017-01-15 18:39:03 +03:00
|
|
|
(execution.model == ExecutionModelVertex && storage == StorageClassOutput))
|
|
|
|
{
|
2017-01-28 10:58:25 +03:00
|
|
|
MemberSorter member_sorter_io(ib_type, meta[ib_type.self], MemberSorter::Alphabetical);
|
|
|
|
member_sorter_io.sort();
|
2017-01-15 18:14:21 +03:00
|
|
|
}
|
2016-05-05 10:33:18 +03:00
|
|
|
|
|
|
|
return ib_var_id;
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
2017-01-29 21:28:20 +03:00
|
|
|
// Excludes the specified input variable from the stage_in block structure.
|
|
|
|
// Instead, the variable is added to a block variable corresponding to a secondary MSL buffer.
|
|
|
|
// The main use case for this is when a stage_in variable contains a matrix, which is a rare occurrence.
|
|
|
|
void CompilerMSL::exclude_from_stage_in(SPIRVariable &var)
|
|
|
|
{
|
|
|
|
uint32_t var_id = var.self;
|
|
|
|
|
|
|
|
if (!(get_decoration_mask(var_id) & (1ull << DecorationLocation)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
uint32_t mbr_type_id = var.basetype;
|
|
|
|
string mbr_name = ensure_valid_name(to_expression(var_id), "m");
|
|
|
|
uint32_t mbr_locn = get_decoration(var_id, DecorationLocation);
|
|
|
|
meta[var_id].decoration.qualified_alias = add_input_buffer_block_member(mbr_type_id, mbr_name, mbr_locn);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Excludes the specified type member from the stage_in block structure.
|
|
|
|
// Instead, the member is added to a block variable corresponding to a secondary MSL buffer.
|
|
|
|
// The main use case for this is when a stage_in variable contains a matrix, which is a rare occurrence.
|
|
|
|
void CompilerMSL::exclude_member_from_stage_in(const SPIRType &type, uint32_t index)
|
|
|
|
{
|
|
|
|
uint32_t type_id = type.self;
|
|
|
|
|
|
|
|
if (!(get_member_decoration_mask(type_id, index) & (1ull << DecorationLocation)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
uint32_t mbr_type_id = type.member_types[index];
|
|
|
|
string mbr_name = ensure_valid_name(to_qualified_member_name(type, index), "m");
|
|
|
|
uint32_t mbr_locn = get_member_decoration(type_id, index, DecorationLocation);
|
|
|
|
string qual_name = add_input_buffer_block_member(mbr_type_id, mbr_name, mbr_locn);
|
|
|
|
set_member_qualified_name(type_id, index, qual_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds a member to the input buffer block that corresponds to the MTLBuffer used by an attribute location
|
|
|
|
string CompilerMSL::add_input_buffer_block_member(uint32_t mbr_type_id, string mbr_name, uint32_t mbr_locn)
|
|
|
|
{
|
|
|
|
mark_location_as_used_by_shader(mbr_locn, StorageClassInput);
|
|
|
|
|
|
|
|
MSLVertexAttr *p_va = vtx_attrs_by_location[mbr_locn];
|
|
|
|
if (!p_va)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
if (p_va->per_instance)
|
|
|
|
needs_instance_idx_arg = true;
|
|
|
|
else
|
|
|
|
needs_vertex_idx_arg = true;
|
|
|
|
|
|
|
|
// The variable that is the block struct.
|
|
|
|
// Record the stride of this struct in its offset decoration.
|
|
|
|
uint32_t ib_var_id = get_input_buffer_block_var_id(p_va->msl_buffer);
|
|
|
|
auto &ib_var = get<SPIRVariable>(ib_var_id);
|
|
|
|
uint32_t ib_type_id = ib_var.basetype;
|
|
|
|
auto &ib_type = get<SPIRType>(ib_type_id);
|
|
|
|
set_decoration(ib_type_id, DecorationOffset, p_va->msl_stride);
|
|
|
|
|
|
|
|
// Add a reference to the variable type to the interface struct.
|
|
|
|
uint32_t ib_mbr_idx = uint32_t(ib_type.member_types.size());
|
|
|
|
ib_type.member_types.push_back(mbr_type_id);
|
|
|
|
|
|
|
|
// Give the member a name
|
|
|
|
set_member_name(ib_type_id, ib_mbr_idx, mbr_name);
|
|
|
|
|
|
|
|
// Set MSL buffer and offset decorations, and indicate no valid attribute location
|
|
|
|
set_member_decoration(ib_type_id, ib_mbr_idx, DecorationBinding, p_va->msl_buffer);
|
|
|
|
set_member_decoration(ib_type_id, ib_mbr_idx, DecorationOffset, p_va->msl_offset);
|
|
|
|
set_member_decoration(ib_type_id, ib_mbr_idx, DecorationLocation, k_unknown_location);
|
|
|
|
|
|
|
|
// Update the original variable reference to include the structure and index reference
|
|
|
|
string idx_var_name = builtin_to_glsl(p_va->per_instance ? BuiltInInstanceIndex : BuiltInVertexIndex);
|
|
|
|
return get_name(ib_var_id) + "[" + idx_var_name + "]." + mbr_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the ID of the input block that will use the specified MSL buffer index,
|
|
|
|
// lazily creating an input block variable and type if needed.
|
|
|
|
//
|
|
|
|
// The use of this block applies only to input variables that have been excluded from the stage_in
|
|
|
|
// block, which typically only occurs if an attempt to pass a matrix in the stage_in block.
|
|
|
|
uint32_t CompilerMSL::get_input_buffer_block_var_id(uint32_t msl_buffer)
|
|
|
|
{
|
|
|
|
uint32_t ib_var_id = non_stage_in_input_var_ids[msl_buffer];
|
|
|
|
if (!ib_var_id)
|
|
|
|
{
|
|
|
|
// No interface block exists yet. Create a new typed variable for this interface block.
|
|
|
|
// The initializer expression is allocated here, but populated when the function
|
|
|
|
// declaraion is emitted, because it is cleared after each compilation pass.
|
|
|
|
uint32_t next_id = increase_bound_by(3);
|
|
|
|
uint32_t ib_type_id = next_id++;
|
|
|
|
auto &ib_type = set<SPIRType>(ib_type_id);
|
|
|
|
ib_type.basetype = SPIRType::Struct;
|
|
|
|
ib_type.storage = StorageClassInput;
|
|
|
|
set_decoration(ib_type_id, DecorationBlock);
|
|
|
|
|
|
|
|
ib_var_id = next_id++;
|
|
|
|
auto &var = set<SPIRVariable>(ib_var_id, ib_type_id, StorageClassInput, 0);
|
|
|
|
var.initializer = next_id++;
|
|
|
|
|
|
|
|
string ib_var_name = stage_in_var_name + convert_to_string(msl_buffer);
|
|
|
|
set_name(ib_var_id, ib_var_name);
|
|
|
|
set_name(ib_type_id, get_entry_point_name() + "_" + ib_var_name);
|
|
|
|
|
|
|
|
// Add the variable to the map of buffer blocks, accessed by the Metal buffer index.
|
|
|
|
non_stage_in_input_var_ids[msl_buffer] = ib_var_id;
|
|
|
|
}
|
|
|
|
return ib_var_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort the members of the struct type by offset, and pad where needed.
|
|
|
|
void CompilerMSL::pad_input_buffer_block(uint32_t ib_type_id)
|
|
|
|
{
|
|
|
|
auto &ib_type = get<SPIRType>(ib_type_id);
|
|
|
|
|
|
|
|
// Sort the members of the interface structure by their offset.
|
|
|
|
MemberSorter member_sorter(ib_type, meta[ib_type_id], MemberSorter::Offset);
|
|
|
|
member_sorter.sort();
|
|
|
|
|
|
|
|
uint32_t curr_offset = 0;
|
|
|
|
for (uint32_t mbr_idx = 0; mbr_idx < ib_type.member_types.size(); mbr_idx++)
|
|
|
|
{
|
|
|
|
uint32_t mbr_offset = get_member_decoration(ib_type_id, mbr_idx, DecorationOffset);
|
|
|
|
uint32_t gap = mbr_offset - curr_offset;
|
|
|
|
if (gap > 0)
|
|
|
|
{
|
|
|
|
ib_type.member_types.insert(ib_type.member_types.begin() + mbr_idx, get_pad_type(gap).self);
|
|
|
|
set_member_name(ib_type_id, mbr_idx, ("pad" + convert_to_string(mbr_idx)));
|
|
|
|
set_member_decoration(ib_type_id, mbr_idx, DecorationOffset, curr_offset);
|
|
|
|
mbr_idx++; // Now move to the actual member
|
|
|
|
}
|
|
|
|
curr_offset = mbr_offset + uint32_t(get_declared_struct_member_size(ib_type, mbr_idx));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, check if we need to pad to the end of the struct to match its stride
|
|
|
|
uint32_t bb_size = get_decoration(ib_type_id, DecorationOffset);
|
|
|
|
uint32_t gap = bb_size - curr_offset;
|
|
|
|
if (gap > 0)
|
|
|
|
{
|
|
|
|
uint32_t mbr_idx = uint32_t(ib_type.member_types.size());
|
|
|
|
ib_type.member_types.push_back(get_pad_type(gap).self);
|
|
|
|
set_member_name(ib_type_id, mbr_idx, ("pad" + convert_to_string(mbr_idx)));
|
|
|
|
set_member_decoration(ib_type_id, mbr_idx, DecorationOffset, curr_offset);
|
|
|
|
mbr_idx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a char array type suitable for use as a padding member in a packed struct
|
|
|
|
SPIRType &CompilerMSL::get_pad_type(uint32_t pad_len)
|
|
|
|
{
|
|
|
|
uint32_t pad_type_id = pad_type_ids_by_pad_len[pad_len];
|
|
|
|
if (pad_type_id != 0)
|
|
|
|
return get<SPIRType>(pad_type_id);
|
|
|
|
|
|
|
|
pad_type_id = increase_bound_by(1);
|
2017-01-31 19:02:44 +03:00
|
|
|
auto &pad_type = set<SPIRType>(pad_type_id);
|
|
|
|
pad_type.storage = StorageClassGeneric;
|
|
|
|
pad_type.basetype = SPIRType::Char;
|
|
|
|
pad_type.width = 8;
|
|
|
|
pad_type.array.push_back(pad_len);
|
|
|
|
pad_type.array_size_literal.push_back(true);
|
|
|
|
set_decoration(pad_type.self, DecorationArrayStride, pad_len);
|
2017-01-29 21:28:20 +03:00
|
|
|
|
|
|
|
pad_type_ids_by_pad_len[pad_len] = pad_type_id;
|
2017-01-31 19:02:44 +03:00
|
|
|
return pad_type;
|
2017-01-29 21:28:20 +03:00
|
|
|
}
|
|
|
|
|
2016-04-07 00:42:27 +03:00
|
|
|
// Emits the file header info
|
|
|
|
void CompilerMSL::emit_header()
|
|
|
|
{
|
2017-01-20 19:33:59 +03:00
|
|
|
if (!header_lines.empty())
|
|
|
|
{
|
|
|
|
for (auto &header : header_lines)
|
|
|
|
statement(header);
|
2017-01-20 19:24:44 +03:00
|
|
|
|
2017-01-20 19:33:59 +03:00
|
|
|
statement("");
|
|
|
|
}
|
2016-09-17 06:21:37 +03:00
|
|
|
|
2016-05-05 10:33:18 +03:00
|
|
|
statement("#include <metal_stdlib>");
|
|
|
|
statement("#include <simd/simd.h>");
|
|
|
|
statement("");
|
|
|
|
statement("using namespace metal;");
|
|
|
|
statement("");
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
2016-12-19 05:42:10 +03:00
|
|
|
// Emits any needed custom function bodies.
|
|
|
|
void CompilerMSL::emit_custom_functions()
|
|
|
|
{
|
|
|
|
for (auto &op : custom_function_ops)
|
|
|
|
{
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case OpFMod:
|
|
|
|
statement("// Support GLSL mod(), which is slightly different than Metal fmod()");
|
|
|
|
statement("template<typename Tx, typename Ty>");
|
|
|
|
statement("Tx mod(Tx x, Ty y)");
|
|
|
|
begin_scope();
|
|
|
|
statement("return x - y * floor(x / y);");
|
|
|
|
end_scope();
|
|
|
|
statement("");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-05 10:33:18 +03:00
|
|
|
void CompilerMSL::emit_resources()
|
|
|
|
{
|
|
|
|
|
|
|
|
// Output all basic struct types which are not Block or BufferBlock as these are declared inplace
|
|
|
|
// when such variables are instantiated.
|
|
|
|
for (auto &id : ids)
|
|
|
|
{
|
|
|
|
if (id.get_type() == TypeType)
|
|
|
|
{
|
|
|
|
auto &type = id.get<SPIRType>();
|
|
|
|
if (type.basetype == SPIRType::Struct && type.array.empty() && !type.pointer &&
|
|
|
|
(meta[type.self].decoration.decoration_flags &
|
|
|
|
((1ull << DecorationBlock) | (1ull << DecorationBufferBlock))) == 0)
|
|
|
|
{
|
|
|
|
emit_struct(type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output Uniform buffers and constants
|
|
|
|
for (auto &id : ids)
|
|
|
|
{
|
|
|
|
if (id.get_type() == TypeVariable)
|
|
|
|
{
|
|
|
|
auto &var = id.get<SPIRVariable>();
|
|
|
|
auto &type = get<SPIRType>(var.basetype);
|
|
|
|
|
2016-07-05 14:21:26 +03:00
|
|
|
if (var.storage != StorageClassFunction && type.pointer &&
|
|
|
|
(type.storage == StorageClassUniform || type.storage == StorageClassUniformConstant ||
|
|
|
|
type.storage == StorageClassPushConstant) &&
|
2016-08-26 13:58:50 +03:00
|
|
|
!is_hidden_variable(var) && (meta[type.self].decoration.decoration_flags &
|
|
|
|
((1ull << DecorationBlock) | (1ull << DecorationBufferBlock))))
|
2016-05-05 10:33:18 +03:00
|
|
|
{
|
|
|
|
emit_struct(type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output interface blocks.
|
2017-01-01 22:43:20 +03:00
|
|
|
emit_interface_block(stage_in_var_id);
|
2017-01-29 21:28:20 +03:00
|
|
|
for (auto &nsi_var : non_stage_in_input_var_ids)
|
|
|
|
emit_interface_block(nsi_var.second);
|
|
|
|
|
2016-05-05 10:33:18 +03:00
|
|
|
emit_interface_block(stage_out_var_id);
|
2017-01-15 18:14:21 +03:00
|
|
|
emit_interface_block(stage_uniforms_var_id);
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
2016-10-28 01:47:17 +03:00
|
|
|
// Override for MSL-specific syntax instructions
|
|
|
|
void CompilerMSL::emit_instruction(const Instruction &instruction)
|
|
|
|
{
|
|
|
|
|
|
|
|
#define BOP(op) emit_binary_op(ops[0], ops[1], ops[2], ops[3], #op)
|
2016-11-12 12:04:50 +03:00
|
|
|
#define BOP_CAST(op, type) \
|
|
|
|
emit_binary_op_cast(ops[0], ops[1], ops[2], ops[3], #op, type, opcode_is_sign_invariant(opcode))
|
2016-10-28 01:47:17 +03:00
|
|
|
#define UOP(op) emit_unary_op(ops[0], ops[1], ops[2], #op)
|
|
|
|
#define QFOP(op) emit_quaternary_func_op(ops[0], ops[1], ops[2], ops[3], ops[4], ops[5], #op)
|
|
|
|
#define TFOP(op) emit_trinary_func_op(ops[0], ops[1], ops[2], ops[3], ops[4], #op)
|
|
|
|
#define BFOP(op) emit_binary_func_op(ops[0], ops[1], ops[2], ops[3], #op)
|
2016-11-12 12:04:50 +03:00
|
|
|
#define BFOP_CAST(op, type) \
|
|
|
|
emit_binary_func_op_cast(ops[0], ops[1], ops[2], ops[3], #op, type, opcode_is_sign_invariant(opcode))
|
2016-10-28 01:47:17 +03:00
|
|
|
#define BFOP(op) emit_binary_func_op(ops[0], ops[1], ops[2], ops[3], #op)
|
|
|
|
#define UFOP(op) emit_unary_func_op(ops[0], ops[1], ops[2], #op)
|
|
|
|
|
|
|
|
auto ops = stream(instruction);
|
|
|
|
auto opcode = static_cast<Op>(instruction.op);
|
|
|
|
|
|
|
|
switch (opcode)
|
|
|
|
{
|
2016-12-04 20:32:58 +03:00
|
|
|
|
2016-10-28 01:47:17 +03:00
|
|
|
// Comparisons
|
|
|
|
case OpIEqual:
|
|
|
|
case OpLogicalEqual:
|
|
|
|
case OpFOrdEqual:
|
|
|
|
BOP(==);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OpINotEqual:
|
|
|
|
case OpLogicalNotEqual:
|
|
|
|
case OpFOrdNotEqual:
|
|
|
|
BOP(!=);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OpUGreaterThan:
|
|
|
|
case OpSGreaterThan:
|
|
|
|
case OpFOrdGreaterThan:
|
|
|
|
BOP(>);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OpUGreaterThanEqual:
|
|
|
|
case OpSGreaterThanEqual:
|
|
|
|
case OpFOrdGreaterThanEqual:
|
|
|
|
BOP(>=);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OpULessThan:
|
|
|
|
case OpSLessThan:
|
|
|
|
case OpFOrdLessThan:
|
|
|
|
BOP(<);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OpULessThanEqual:
|
|
|
|
case OpSLessThanEqual:
|
|
|
|
case OpFOrdLessThanEqual:
|
|
|
|
BOP(<=);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Derivatives
|
|
|
|
case OpDPdx:
|
|
|
|
UFOP(dfdx);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OpDPdy:
|
|
|
|
UFOP(dfdy);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OpImageQuerySize:
|
|
|
|
{
|
|
|
|
auto &type = expression_type(ops[2]);
|
|
|
|
uint32_t result_type = ops[0];
|
|
|
|
uint32_t id = ops[1];
|
|
|
|
|
|
|
|
if (type.basetype == SPIRType::Image)
|
|
|
|
{
|
|
|
|
string img_exp = to_expression(ops[2]);
|
|
|
|
auto &img_type = type.image;
|
|
|
|
switch (img_type.dim)
|
|
|
|
{
|
|
|
|
case Dim1D:
|
|
|
|
if (img_type.arrayed)
|
2016-11-12 12:04:50 +03:00
|
|
|
emit_op(result_type, id, join("uint2(", img_exp, ".get_width(), ", img_exp, ".get_array_size())"),
|
2016-12-05 12:22:54 +03:00
|
|
|
false);
|
2016-10-28 01:47:17 +03:00
|
|
|
else
|
2016-12-05 12:22:54 +03:00
|
|
|
emit_op(result_type, id, join(img_exp, ".get_width()"), true);
|
2016-10-28 01:47:17 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Dim2D:
|
|
|
|
case DimCube:
|
|
|
|
if (img_type.arrayed)
|
2016-11-12 12:04:50 +03:00
|
|
|
emit_op(result_type, id, join("uint3(", img_exp, ".get_width(), ", img_exp, ".get_height(), ",
|
|
|
|
img_exp, ".get_array_size())"),
|
2016-12-05 12:22:54 +03:00
|
|
|
false);
|
2016-10-28 01:47:17 +03:00
|
|
|
else
|
2016-11-12 12:04:50 +03:00
|
|
|
emit_op(result_type, id, join("uint2(", img_exp, ".get_width(), ", img_exp, ".get_height())"),
|
2016-12-05 12:22:54 +03:00
|
|
|
false);
|
2016-10-28 01:47:17 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Dim3D:
|
2016-11-12 12:04:50 +03:00
|
|
|
emit_op(result_type, id,
|
|
|
|
join("uint3(", img_exp, ".get_width(), ", img_exp, ".get_height(), ", img_exp, ".get_depth())"),
|
2016-12-05 12:22:54 +03:00
|
|
|
false);
|
2016-10-28 01:47:17 +03:00
|
|
|
break;
|
2016-11-12 12:04:50 +03:00
|
|
|
|
2016-10-28 01:47:17 +03:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2016-12-13 00:33:22 +03:00
|
|
|
SPIRV_CROSS_THROW("Invalid type for OpImageQuerySize.");
|
2016-10-28 01:47:17 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
CompilerGLSL::emit_instruction(instruction);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Override for MSL-specific extension syntax instructions
|
|
|
|
void CompilerMSL::emit_glsl_op(uint32_t result_type, uint32_t id, uint32_t eop, const uint32_t *args, uint32_t count)
|
|
|
|
{
|
|
|
|
GLSLstd450 op = static_cast<GLSLstd450>(eop);
|
|
|
|
|
|
|
|
switch (op)
|
|
|
|
{
|
|
|
|
case GLSLstd450Atan2:
|
|
|
|
emit_binary_func_op(result_type, id, args[0], args[1], "atan2");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
CompilerGLSL::emit_glsl_op(result_type, id, eop, args, count);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-07 00:42:27 +03:00
|
|
|
// Emit a structure declaration for the specified interface variable.
|
|
|
|
void CompilerMSL::emit_interface_block(uint32_t ib_var_id)
|
|
|
|
{
|
2016-05-05 10:33:18 +03:00
|
|
|
if (ib_var_id)
|
|
|
|
{
|
|
|
|
auto &ib_var = get<SPIRVariable>(ib_var_id);
|
|
|
|
auto &ib_type = get<SPIRType>(ib_var.basetype);
|
2017-01-16 16:17:23 +03:00
|
|
|
auto &m = meta.at(ib_type.self);
|
2017-01-26 12:20:42 +03:00
|
|
|
if (m.members.size() > 0)
|
|
|
|
emit_struct(ib_type);
|
2016-05-05 10:33:18 +03:00
|
|
|
}
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emits the declaration signature of the specified function.
|
|
|
|
// If this is the entry point function, Metal-specific return value and function arguments are added.
|
2017-01-20 19:24:44 +03:00
|
|
|
void CompilerMSL::emit_function_prototype(SPIRFunction &func, uint64_t)
|
2016-04-07 00:42:27 +03:00
|
|
|
{
|
2016-05-23 13:25:09 +03:00
|
|
|
local_variable_names = resource_names;
|
2016-05-05 10:33:18 +03:00
|
|
|
string decl;
|
|
|
|
|
2016-07-28 12:16:02 +03:00
|
|
|
processing_entry_point = (func.self == entry_point);
|
2016-05-05 10:33:18 +03:00
|
|
|
|
|
|
|
auto &type = get<SPIRType>(func.return_type);
|
|
|
|
decl += func_type_decl(type);
|
|
|
|
decl += " ";
|
|
|
|
decl += clean_func_name(to_name(func.self));
|
|
|
|
|
|
|
|
decl += "(";
|
|
|
|
|
|
|
|
if (processing_entry_point)
|
|
|
|
{
|
|
|
|
decl += entry_point_args(!func.arguments.empty());
|
|
|
|
|
|
|
|
// If entry point function has a output interface struct, set its initializer.
|
|
|
|
// This is done at this late stage because the initialization expression is
|
|
|
|
// cleared after each compilation pass.
|
|
|
|
if (stage_out_var_id)
|
|
|
|
{
|
|
|
|
auto &so_var = get<SPIRVariable>(stage_out_var_id);
|
|
|
|
auto &so_type = get<SPIRType>(so_var.basetype);
|
|
|
|
set<SPIRExpression>(so_var.initializer, "{}", so_type.self, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &arg : func.arguments)
|
|
|
|
{
|
2016-05-23 13:25:09 +03:00
|
|
|
add_local_variable_name(arg.id);
|
2016-05-05 10:33:18 +03:00
|
|
|
|
2017-01-27 03:40:56 +03:00
|
|
|
string address_space = "thread";
|
|
|
|
|
2016-05-05 10:33:18 +03:00
|
|
|
auto *var = maybe_get<SPIRVariable>(arg.id);
|
2016-10-24 16:24:24 +03:00
|
|
|
if (var)
|
|
|
|
{
|
|
|
|
var->parameter = &arg; // Hold a pointer to the parameter so we can invalidate the readonly field if needed.
|
2017-01-27 03:40:56 +03:00
|
|
|
address_space = get_argument_address_space(*var);
|
2016-10-24 16:24:24 +03:00
|
|
|
}
|
2016-07-06 23:55:45 +03:00
|
|
|
|
2017-01-27 03:40:56 +03:00
|
|
|
decl += address_space + " ";
|
2016-10-24 16:24:24 +03:00
|
|
|
decl += argument_decl(arg);
|
2016-07-07 03:30:47 +03:00
|
|
|
|
2016-10-24 16:24:24 +03:00
|
|
|
// Manufacture automatic sampler arg for SampledImage texture
|
|
|
|
auto &arg_type = get<SPIRType>(arg.type);
|
|
|
|
if (arg_type.basetype == SPIRType::SampledImage)
|
|
|
|
decl += ", thread const sampler& " + to_sampler_expression(arg.id);
|
2016-07-07 03:30:47 +03:00
|
|
|
|
2016-10-24 16:24:24 +03:00
|
|
|
if (&arg != &func.arguments.back())
|
|
|
|
decl += ", ";
|
|
|
|
}
|
2016-05-05 10:33:18 +03:00
|
|
|
|
|
|
|
decl += ")";
|
2017-01-20 19:33:59 +03:00
|
|
|
statement(decl);
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
2016-12-29 02:36:42 +03:00
|
|
|
// Returns the texture sampling function string for the specified image and sampling characteristics.
|
2017-01-08 16:52:57 +03:00
|
|
|
string CompilerMSL::to_function_name(uint32_t img, const SPIRType &, bool is_fetch, bool is_gather, bool, bool, bool,
|
|
|
|
bool, bool, bool has_dref)
|
2016-04-07 00:42:27 +03:00
|
|
|
{
|
2016-05-05 10:33:18 +03:00
|
|
|
// Texture reference
|
2016-12-29 02:36:42 +03:00
|
|
|
string fname = to_expression(img) + ".";
|
2016-05-05 10:33:18 +03:00
|
|
|
|
|
|
|
// Texture function and sampler
|
2016-12-29 02:36:42 +03:00
|
|
|
if (is_fetch)
|
|
|
|
fname += "read";
|
|
|
|
else if (is_gather)
|
|
|
|
fname += "gather";
|
2016-05-05 10:33:18 +03:00
|
|
|
else
|
2016-12-29 02:36:42 +03:00
|
|
|
fname += "sample";
|
2016-05-05 10:33:18 +03:00
|
|
|
|
2016-12-29 02:36:42 +03:00
|
|
|
if (has_dref)
|
|
|
|
fname += "_compare";
|
2016-05-05 10:33:18 +03:00
|
|
|
|
2016-12-29 02:36:42 +03:00
|
|
|
return fname;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the function args for a texture sampling function for the specified image and sampling characteristics.
|
2017-01-07 23:55:04 +03:00
|
|
|
string CompilerMSL::to_function_args(uint32_t img, const SPIRType &imgtype, bool is_fetch, bool, bool is_proj,
|
|
|
|
uint32_t coord, uint32_t, uint32_t dref, uint32_t grad_x, uint32_t grad_y,
|
|
|
|
uint32_t lod, uint32_t coffset, uint32_t offset, uint32_t bias, uint32_t comp,
|
2017-01-21 01:30:36 +03:00
|
|
|
uint32_t sample, bool *p_forward)
|
2016-12-29 02:36:42 +03:00
|
|
|
{
|
2017-01-21 01:30:36 +03:00
|
|
|
string farg_str;
|
|
|
|
if (!is_fetch)
|
|
|
|
farg_str += to_sampler_expression(img);
|
2016-12-29 02:36:42 +03:00
|
|
|
|
|
|
|
// Texture coordinates
|
2016-05-05 10:33:18 +03:00
|
|
|
bool forward = should_forward(coord);
|
2016-12-07 01:03:35 +03:00
|
|
|
auto coord_expr = to_enclosed_expression(coord);
|
2016-05-05 10:33:18 +03:00
|
|
|
string tex_coords = coord_expr;
|
2017-01-21 01:30:36 +03:00
|
|
|
string face_expr;
|
2016-12-29 02:36:42 +03:00
|
|
|
const char *alt_coord = "";
|
2016-05-05 10:33:18 +03:00
|
|
|
|
2016-12-29 02:36:42 +03:00
|
|
|
switch (imgtype.image.dim)
|
2016-05-05 10:33:18 +03:00
|
|
|
{
|
|
|
|
case Dim1D:
|
2016-12-29 02:36:42 +03:00
|
|
|
tex_coords = coord_expr + ".x";
|
|
|
|
remove_duplicate_swizzle(tex_coords);
|
|
|
|
|
2017-01-21 01:30:36 +03:00
|
|
|
if (is_fetch)
|
|
|
|
tex_coords = "uint(" + tex_coords + ")";
|
|
|
|
|
2016-12-29 02:36:42 +03:00
|
|
|
alt_coord = ".y";
|
|
|
|
|
2016-05-05 10:33:18 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Dim2D:
|
|
|
|
if (msl_config.flip_frag_y)
|
|
|
|
{
|
|
|
|
string coord_x = coord_expr + ".x";
|
|
|
|
remove_duplicate_swizzle(coord_x);
|
|
|
|
string coord_y = coord_expr + ".y";
|
|
|
|
remove_duplicate_swizzle(coord_y);
|
2017-01-21 01:30:36 +03:00
|
|
|
if (is_fetch)
|
|
|
|
tex_coords = "uint2(" + coord_x + ", (1.0 - " + coord_y + "))";
|
|
|
|
else
|
|
|
|
tex_coords = "float2(" + coord_x + ", (1.0 - " + coord_y + "))";
|
2016-05-05 10:33:18 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tex_coords = coord_expr + ".xy";
|
|
|
|
remove_duplicate_swizzle(tex_coords);
|
2017-01-21 01:30:36 +03:00
|
|
|
|
|
|
|
if (is_fetch)
|
|
|
|
tex_coords = "uint2(" + tex_coords + ")";
|
2016-05-05 10:33:18 +03:00
|
|
|
}
|
|
|
|
|
2016-12-29 02:36:42 +03:00
|
|
|
alt_coord = ".z";
|
2016-05-05 10:33:18 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Dim3D:
|
|
|
|
if (msl_config.flip_frag_y)
|
|
|
|
{
|
|
|
|
string coord_x = coord_expr + ".x";
|
|
|
|
remove_duplicate_swizzle(coord_x);
|
|
|
|
string coord_y = coord_expr + ".y";
|
|
|
|
remove_duplicate_swizzle(coord_y);
|
|
|
|
string coord_z = coord_expr + ".z";
|
|
|
|
remove_duplicate_swizzle(coord_z);
|
2017-01-21 01:30:36 +03:00
|
|
|
if (is_fetch)
|
|
|
|
tex_coords = "uint3(" + coord_x + ", (1.0 - " + coord_y + "), " + coord_z + ")";
|
|
|
|
else
|
|
|
|
tex_coords = "float3(" + coord_x + ", (1.0 - " + coord_y + "), " + coord_z + ")";
|
2016-05-05 10:33:18 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tex_coords = coord_expr + ".xyz";
|
|
|
|
remove_duplicate_swizzle(tex_coords);
|
2017-01-21 01:30:36 +03:00
|
|
|
|
|
|
|
if (is_fetch)
|
|
|
|
tex_coords = "uint3(" + tex_coords + ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
alt_coord = ".w";
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DimCube:
|
|
|
|
if (msl_config.flip_frag_y)
|
|
|
|
{
|
|
|
|
string coord_x = coord_expr + ".x";
|
|
|
|
remove_duplicate_swizzle(coord_x);
|
|
|
|
string coord_y = coord_expr + ".y";
|
|
|
|
remove_duplicate_swizzle(coord_y);
|
|
|
|
string coord_z = coord_expr + ".z";
|
|
|
|
remove_duplicate_swizzle(coord_z);
|
|
|
|
|
|
|
|
if (is_fetch)
|
|
|
|
{
|
|
|
|
tex_coords = "uint2(" + coord_x + ", (1.0 - " + coord_y + "))";
|
|
|
|
face_expr = coord_z;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
tex_coords = "float3(" + coord_x + ", (1.0 - " + coord_y + "), " + coord_z + ")";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (is_fetch)
|
|
|
|
{
|
|
|
|
tex_coords = coord_expr + ".xy";
|
|
|
|
remove_duplicate_swizzle(tex_coords);
|
|
|
|
tex_coords = "uint2(" + tex_coords + ")";
|
|
|
|
|
|
|
|
face_expr = coord_expr + ".z";
|
|
|
|
remove_duplicate_swizzle(face_expr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tex_coords = coord_expr + ".xyz";
|
|
|
|
remove_duplicate_swizzle(tex_coords);
|
|
|
|
}
|
2016-05-05 10:33:18 +03:00
|
|
|
}
|
|
|
|
|
2016-12-29 02:36:42 +03:00
|
|
|
alt_coord = ".w";
|
2016-05-05 10:33:18 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-01-21 01:30:36 +03:00
|
|
|
// If projection, use alt coord as divisor
|
|
|
|
if (is_proj)
|
|
|
|
{
|
|
|
|
string divisor = coord_expr + alt_coord;
|
|
|
|
remove_duplicate_swizzle(divisor);
|
|
|
|
tex_coords += " / " + divisor;
|
|
|
|
}
|
2016-12-29 02:36:42 +03:00
|
|
|
|
2017-01-21 01:30:36 +03:00
|
|
|
if (!farg_str.empty())
|
|
|
|
farg_str += ", ";
|
2016-12-29 02:36:42 +03:00
|
|
|
farg_str += tex_coords;
|
|
|
|
|
2017-01-21 01:30:36 +03:00
|
|
|
// If fetch from cube, add face explicitly
|
|
|
|
if (!face_expr.empty())
|
|
|
|
farg_str += ", uint(" + face_expr + ")";
|
|
|
|
|
|
|
|
// If array, use alt coord
|
|
|
|
if (imgtype.image.arrayed)
|
|
|
|
{
|
|
|
|
string array_index = coord_expr + alt_coord;
|
|
|
|
remove_duplicate_swizzle(array_index);
|
|
|
|
farg_str += ", uint(" + array_index + ")";
|
|
|
|
}
|
|
|
|
|
2016-12-29 02:36:42 +03:00
|
|
|
// Depth compare reference value
|
|
|
|
if (dref)
|
|
|
|
{
|
|
|
|
forward = forward && should_forward(dref);
|
|
|
|
farg_str += ", ";
|
|
|
|
farg_str += to_expression(dref);
|
|
|
|
}
|
2016-05-05 10:33:18 +03:00
|
|
|
|
|
|
|
// LOD Options
|
|
|
|
if (bias)
|
|
|
|
{
|
|
|
|
forward = forward && should_forward(bias);
|
2016-12-29 02:36:42 +03:00
|
|
|
farg_str += ", bias(" + to_expression(bias) + ")";
|
2016-05-05 10:33:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lod)
|
|
|
|
{
|
|
|
|
forward = forward && should_forward(lod);
|
2016-12-29 02:36:42 +03:00
|
|
|
if (is_fetch)
|
2016-05-05 10:33:18 +03:00
|
|
|
{
|
2016-12-29 02:36:42 +03:00
|
|
|
farg_str += ", " + to_expression(lod);
|
2016-05-05 10:33:18 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-12-29 02:36:42 +03:00
|
|
|
farg_str += ", level(" + to_expression(lod) + ")";
|
2016-05-05 10:33:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (grad_x || grad_y)
|
|
|
|
{
|
|
|
|
forward = forward && should_forward(grad_x);
|
|
|
|
forward = forward && should_forward(grad_y);
|
|
|
|
string grad_opt;
|
2016-12-29 02:36:42 +03:00
|
|
|
switch (imgtype.image.dim)
|
2016-05-05 10:33:18 +03:00
|
|
|
{
|
|
|
|
case Dim2D:
|
|
|
|
grad_opt = "2d";
|
|
|
|
break;
|
|
|
|
case Dim3D:
|
|
|
|
grad_opt = "3d";
|
|
|
|
break;
|
|
|
|
case DimCube:
|
|
|
|
grad_opt = "cube";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
grad_opt = "unsupported_gradient_dimension";
|
|
|
|
break;
|
|
|
|
}
|
2016-12-29 02:36:42 +03:00
|
|
|
farg_str += ", gradient" + grad_opt + "(" + to_expression(grad_x) + ", " + to_expression(grad_y) + ")";
|
2016-05-05 10:33:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add offsets
|
|
|
|
string offset_expr;
|
|
|
|
if (coffset)
|
|
|
|
{
|
|
|
|
forward = forward && should_forward(coffset);
|
|
|
|
offset_expr = to_expression(coffset);
|
|
|
|
}
|
|
|
|
else if (offset)
|
|
|
|
{
|
|
|
|
forward = forward && should_forward(offset);
|
|
|
|
offset_expr = to_expression(offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!offset_expr.empty())
|
|
|
|
{
|
2016-12-29 02:36:42 +03:00
|
|
|
switch (imgtype.image.dim)
|
2016-05-05 10:33:18 +03:00
|
|
|
{
|
|
|
|
case Dim2D:
|
|
|
|
if (msl_config.flip_frag_y)
|
|
|
|
{
|
|
|
|
string coord_x = offset_expr + ".x";
|
|
|
|
remove_duplicate_swizzle(coord_x);
|
|
|
|
string coord_y = offset_expr + ".y";
|
|
|
|
remove_duplicate_swizzle(coord_y);
|
|
|
|
offset_expr = "float2(" + coord_x + ", (1.0 - " + coord_y + "))";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
offset_expr = offset_expr + ".xy";
|
|
|
|
remove_duplicate_swizzle(offset_expr);
|
|
|
|
}
|
|
|
|
|
2016-12-29 02:36:42 +03:00
|
|
|
farg_str += ", " + offset_expr;
|
2016-05-05 10:33:18 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Dim3D:
|
|
|
|
if (msl_config.flip_frag_y)
|
|
|
|
{
|
|
|
|
string coord_x = offset_expr + ".x";
|
|
|
|
remove_duplicate_swizzle(coord_x);
|
|
|
|
string coord_y = offset_expr + ".y";
|
|
|
|
remove_duplicate_swizzle(coord_y);
|
|
|
|
string coord_z = offset_expr + ".z";
|
|
|
|
remove_duplicate_swizzle(coord_z);
|
|
|
|
offset_expr = "float3(" + coord_x + ", (1.0 - " + coord_y + "), " + coord_z + ")";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
offset_expr = offset_expr + ".xyz";
|
|
|
|
remove_duplicate_swizzle(offset_expr);
|
|
|
|
}
|
|
|
|
|
2016-12-29 02:36:42 +03:00
|
|
|
farg_str += ", " + offset_expr;
|
2016-05-05 10:33:18 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (comp)
|
|
|
|
{
|
|
|
|
forward = forward && should_forward(comp);
|
2016-12-29 02:36:42 +03:00
|
|
|
farg_str += ", " + to_component_argument(comp);
|
2016-05-05 10:33:18 +03:00
|
|
|
}
|
|
|
|
|
2017-01-21 01:30:36 +03:00
|
|
|
if (sample)
|
|
|
|
{
|
|
|
|
farg_str += ", ";
|
|
|
|
farg_str += to_expression(sample);
|
|
|
|
}
|
|
|
|
|
2016-12-29 02:36:42 +03:00
|
|
|
*p_forward = forward;
|
2016-05-05 10:33:18 +03:00
|
|
|
|
2016-12-29 02:36:42 +03:00
|
|
|
return farg_str;
|
|
|
|
}
|
2016-05-05 10:33:18 +03:00
|
|
|
|
2016-12-29 02:36:42 +03:00
|
|
|
// Returns a string to use in an image sampling function argument.
|
|
|
|
// The ID must be a scalar constant.
|
|
|
|
string CompilerMSL::to_component_argument(uint32_t id)
|
|
|
|
{
|
|
|
|
if (ids[id].get_type() != TypeConstant)
|
|
|
|
{
|
|
|
|
SPIRV_CROSS_THROW("ID " + to_string(id) + " is not an OpConstant.");
|
|
|
|
return "component::x";
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t component_index = get<SPIRConstant>(id).scalar();
|
|
|
|
switch (component_index)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
return "component::x";
|
|
|
|
case 1:
|
|
|
|
return "component::y";
|
|
|
|
case 2:
|
|
|
|
return "component::z";
|
|
|
|
case 3:
|
|
|
|
return "component::w";
|
|
|
|
|
|
|
|
default:
|
|
|
|
SPIRV_CROSS_THROW("The value (" + to_string(component_index) + ") of OpConstant ID " + to_string(id) +
|
|
|
|
" is not a valid Component index, which must be one of 0, 1, 2, or 3.");
|
|
|
|
return "component::x";
|
|
|
|
}
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
2016-04-24 04:47:41 +03:00
|
|
|
// Establish sampled image as expression object and assign the sampler to it.
|
|
|
|
void CompilerMSL::emit_sampled_image_op(uint32_t result_type, uint32_t result_id, uint32_t image_id, uint32_t samp_id)
|
|
|
|
{
|
2016-05-05 10:33:18 +03:00
|
|
|
set<SPIRExpression>(result_id, to_expression(image_id), result_type, true);
|
|
|
|
meta[result_id].sampler = samp_id;
|
2016-04-24 04:47:41 +03:00
|
|
|
}
|
|
|
|
|
2016-07-07 03:30:47 +03:00
|
|
|
// 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)
|
|
|
|
{
|
2016-10-24 16:24:24 +03:00
|
|
|
string arg_str = CompilerGLSL::to_func_call_arg(id);
|
2016-07-07 03:30:47 +03:00
|
|
|
|
2016-11-27 20:34:04 +03:00
|
|
|
// Manufacture automatic sampler arg if the arg is a SampledImage texture.
|
|
|
|
Variant &id_v = ids[id];
|
|
|
|
if (id_v.get_type() == TypeVariable)
|
|
|
|
{
|
|
|
|
auto &var = id_v.get<SPIRVariable>();
|
|
|
|
auto &type = get<SPIRType>(var.basetype);
|
|
|
|
if (type.basetype == SPIRType::SampledImage)
|
|
|
|
arg_str += ", " + to_sampler_expression(id);
|
|
|
|
}
|
2016-07-07 03:30:47 +03:00
|
|
|
|
2016-10-24 16:24:24 +03:00
|
|
|
return arg_str;
|
2016-07-07 03:30:47 +03:00
|
|
|
}
|
|
|
|
|
2016-04-24 04:47:41 +03:00
|
|
|
// 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.
|
|
|
|
string CompilerMSL::to_sampler_expression(uint32_t id)
|
|
|
|
{
|
2016-05-05 10:33:18 +03:00
|
|
|
uint32_t samp_id = meta[id].sampler;
|
|
|
|
return samp_id ? to_expression(samp_id) : to_expression(id) + sampler_name_suffix;
|
2016-04-24 04:47:41 +03:00
|
|
|
}
|
|
|
|
|
2016-04-07 00:42:27 +03:00
|
|
|
// Called automatically at the end of the entry point function
|
|
|
|
void CompilerMSL::emit_fixup()
|
|
|
|
{
|
2016-07-28 12:16:02 +03:00
|
|
|
auto &execution = get_entry_point();
|
|
|
|
|
2016-05-05 10:33:18 +03:00
|
|
|
if ((execution.model == ExecutionModelVertex) && stage_out_var_id && !qual_pos_var_name.empty())
|
|
|
|
{
|
|
|
|
if (options.vertex.fixup_clipspace)
|
|
|
|
{
|
2017-01-15 18:39:03 +03:00
|
|
|
statement(qual_pos_var_name, ".z = (", qual_pos_var_name, ".z + ", qual_pos_var_name,
|
|
|
|
".w) * 0.5; // Adjust clip-space for Metal");
|
2016-05-05 10:33:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (msl_config.flip_vert_y)
|
|
|
|
statement(qual_pos_var_name, ".y = -(", qual_pos_var_name, ".y);", " // Invert Y-axis for Metal");
|
|
|
|
}
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a declaration for a structure member.
|
|
|
|
string CompilerMSL::member_decl(const SPIRType &type, const SPIRType &membertype, uint32_t index)
|
|
|
|
{
|
2016-12-04 20:32:58 +03:00
|
|
|
return join(type_to_glsl(membertype), " ", to_member_name(type, index), type_to_array_glsl(membertype),
|
|
|
|
member_attribute_qualifier(type, index));
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return a MSL qualifier for the specified function attribute member
|
2016-05-05 10:33:18 +03:00
|
|
|
string CompilerMSL::member_attribute_qualifier(const SPIRType &type, uint32_t index)
|
|
|
|
{
|
2016-07-28 12:16:02 +03:00
|
|
|
auto &execution = get_entry_point();
|
2016-05-05 10:33:18 +03:00
|
|
|
|
|
|
|
BuiltIn builtin;
|
|
|
|
bool is_builtin = is_member_builtin(type, index, &builtin);
|
|
|
|
|
|
|
|
// Vertex function inputs
|
|
|
|
if (execution.model == ExecutionModelVertex && type.storage == StorageClassInput)
|
|
|
|
{
|
|
|
|
if (is_builtin)
|
|
|
|
{
|
|
|
|
switch (builtin)
|
|
|
|
{
|
|
|
|
case BuiltInVertexId:
|
|
|
|
case BuiltInVertexIndex:
|
|
|
|
case BuiltInInstanceId:
|
|
|
|
case BuiltInInstanceIndex:
|
|
|
|
return string(" [[") + builtin_qualifier(builtin) + "]]";
|
|
|
|
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uint32_t locn = get_ordered_member_location(type.self, index);
|
2017-01-29 21:28:20 +03:00
|
|
|
if (locn != k_unknown_location)
|
|
|
|
return string(" [[attribute(") + convert_to_string(locn) + ")]]";
|
2016-05-05 10:33:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Vertex function outputs
|
|
|
|
if (execution.model == ExecutionModelVertex && type.storage == StorageClassOutput)
|
|
|
|
{
|
|
|
|
if (is_builtin)
|
|
|
|
{
|
|
|
|
switch (builtin)
|
|
|
|
{
|
|
|
|
case BuiltInClipDistance:
|
|
|
|
return " /* [[clip_distance]] built-in not yet supported under Metal. */";
|
|
|
|
|
|
|
|
case BuiltInPointSize: // Must output only if really rendering points
|
|
|
|
return msl_config.is_rendering_points ? (string(" [[") + builtin_qualifier(builtin) + "]]") : "";
|
|
|
|
|
|
|
|
case BuiltInPosition:
|
|
|
|
case BuiltInLayer:
|
|
|
|
return string(" [[") + builtin_qualifier(builtin) + "]]";
|
|
|
|
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uint32_t locn = get_ordered_member_location(type.self, index);
|
2017-01-29 21:28:20 +03:00
|
|
|
if (locn != k_unknown_location)
|
|
|
|
return string(" [[user(locn") + convert_to_string(locn) + ")]]";
|
2016-05-05 10:33:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fragment function inputs
|
|
|
|
if (execution.model == ExecutionModelFragment && type.storage == StorageClassInput)
|
|
|
|
{
|
|
|
|
if (is_builtin)
|
|
|
|
{
|
|
|
|
switch (builtin)
|
|
|
|
{
|
|
|
|
case BuiltInFrontFacing:
|
|
|
|
case BuiltInPointCoord:
|
2016-07-04 18:49:46 +03:00
|
|
|
case BuiltInFragCoord:
|
2016-05-05 10:33:18 +03:00
|
|
|
case BuiltInSampleId:
|
|
|
|
case BuiltInSampleMask:
|
|
|
|
case BuiltInLayer:
|
|
|
|
return string(" [[") + builtin_qualifier(builtin) + "]]";
|
|
|
|
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
uint32_t locn = get_ordered_member_location(type.self, index);
|
2017-01-29 21:28:20 +03:00
|
|
|
if (locn != k_unknown_location)
|
|
|
|
return string(" [[user(locn") + convert_to_string(locn) + ")]]";
|
2016-05-05 10:33:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fragment function outputs
|
|
|
|
if (execution.model == ExecutionModelFragment && type.storage == StorageClassOutput)
|
|
|
|
{
|
|
|
|
if (is_builtin)
|
|
|
|
{
|
|
|
|
switch (builtin)
|
|
|
|
{
|
|
|
|
case BuiltInSampleMask:
|
|
|
|
case BuiltInFragDepth:
|
|
|
|
return string(" [[") + builtin_qualifier(builtin) + "]]";
|
|
|
|
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
2016-10-01 09:46:43 +03:00
|
|
|
uint32_t locn = get_ordered_member_location(type.self, index);
|
2017-01-29 21:28:20 +03:00
|
|
|
if (locn != k_unknown_location)
|
|
|
|
return string(" [[color(") + convert_to_string(locn) + ")]]";
|
2016-05-05 10:33:18 +03:00
|
|
|
}
|
|
|
|
|
2017-01-22 21:09:04 +03:00
|
|
|
// Compute function inputs
|
|
|
|
if (execution.model == ExecutionModelGLCompute && type.storage == StorageClassInput)
|
|
|
|
{
|
|
|
|
if (is_builtin)
|
|
|
|
{
|
|
|
|
switch (builtin)
|
|
|
|
{
|
|
|
|
case BuiltInGlobalInvocationId:
|
2017-01-27 04:22:31 +03:00
|
|
|
case BuiltInLocalInvocationId:
|
|
|
|
case BuiltInLocalInvocationIndex:
|
2017-01-22 21:09:04 +03:00
|
|
|
return string(" [[") + builtin_qualifier(builtin) + "]]";
|
|
|
|
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-05 10:33:18 +03:00
|
|
|
return "";
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
2016-12-04 20:32:58 +03:00
|
|
|
// Returns the location decoration of the member with the specified index in the specified type.
|
|
|
|
// If the location of the member has been explicitly set, that location is used. If not, this
|
|
|
|
// function assumes the members are ordered in their location order, and simply returns the
|
|
|
|
// index as the location.
|
2016-04-07 00:42:27 +03:00
|
|
|
uint32_t CompilerMSL::get_ordered_member_location(uint32_t type_id, uint32_t index)
|
|
|
|
{
|
2016-12-04 20:32:58 +03:00
|
|
|
auto &m = meta.at(type_id);
|
|
|
|
if (index < m.members.size())
|
|
|
|
{
|
|
|
|
auto &dec = m.members[index];
|
|
|
|
if (dec.decoration_flags & (1ull << DecorationLocation))
|
|
|
|
return dec.location;
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
string CompilerMSL::constant_expression(const SPIRConstant &c)
|
|
|
|
{
|
2016-05-05 10:33:18 +03:00
|
|
|
if (!c.subconstants.empty())
|
|
|
|
{
|
|
|
|
// Handles Arrays and structures.
|
|
|
|
string res = "{";
|
|
|
|
for (auto &elem : c.subconstants)
|
|
|
|
{
|
|
|
|
res += constant_expression(get<SPIRConstant>(elem));
|
|
|
|
if (&elem != &c.subconstants.back())
|
|
|
|
res += ", ";
|
|
|
|
}
|
|
|
|
res += "}";
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
else if (c.columns() == 1)
|
|
|
|
{
|
|
|
|
return constant_expression_vector(c, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
string res = type_to_glsl(get<SPIRType>(c.constant_type)) + "(";
|
|
|
|
for (uint32_t col = 0; col < c.columns(); col++)
|
|
|
|
{
|
|
|
|
res += constant_expression_vector(c, col);
|
|
|
|
if (col + 1 < c.columns())
|
|
|
|
res += ", ";
|
|
|
|
}
|
|
|
|
res += ")";
|
|
|
|
return res;
|
|
|
|
}
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the type declaration for a function, including the
|
|
|
|
// entry type if the current function is the entry point function
|
2016-05-05 10:33:18 +03:00
|
|
|
string CompilerMSL::func_type_decl(SPIRType &type)
|
2016-04-07 00:42:27 +03:00
|
|
|
{
|
2016-07-28 12:16:02 +03:00
|
|
|
auto &execution = get_entry_point();
|
2016-05-05 10:33:18 +03:00
|
|
|
// The regular function return type. If not processing the entry point function, that's all we need
|
|
|
|
string return_type = type_to_glsl(type);
|
|
|
|
if (!processing_entry_point)
|
|
|
|
return return_type;
|
|
|
|
|
|
|
|
// If an outgoing interface block has been defined, override the entry point return type
|
|
|
|
if (stage_out_var_id)
|
|
|
|
{
|
|
|
|
auto &so_var = get<SPIRVariable>(stage_out_var_id);
|
|
|
|
auto &so_type = get<SPIRType>(so_var.basetype);
|
|
|
|
return_type = type_to_glsl(so_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepend a entry type, based on the execution model
|
|
|
|
string entry_type;
|
2016-10-24 16:24:24 +03:00
|
|
|
switch (execution.model)
|
|
|
|
{
|
|
|
|
case ExecutionModelVertex:
|
|
|
|
entry_type = "vertex";
|
|
|
|
break;
|
|
|
|
case ExecutionModelFragment:
|
|
|
|
entry_type = (execution.flags & (1ull << ExecutionModeEarlyFragmentTests)) ?
|
|
|
|
"fragment [[ early_fragment_tests ]]" :
|
|
|
|
"fragment";
|
|
|
|
break;
|
|
|
|
case ExecutionModelGLCompute:
|
|
|
|
case ExecutionModelKernel:
|
|
|
|
entry_type = "kernel";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
entry_type = "unknown";
|
|
|
|
break;
|
|
|
|
}
|
2016-05-05 10:33:18 +03:00
|
|
|
|
|
|
|
return entry_type + " " + return_type;
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Ensures the function name is not "main", which is illegal in MSL
|
|
|
|
string CompilerMSL::clean_func_name(string func_name)
|
|
|
|
{
|
2017-01-08 16:52:57 +03:00
|
|
|
auto iter = func_name_overrides.find(func_name);
|
|
|
|
return (iter != func_name_overrides.end()) ? iter->second : func_name;
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
2017-01-27 03:40:56 +03:00
|
|
|
// In MSL address space qualifiers are required for all pointer or reference arguments
|
|
|
|
string CompilerMSL::get_argument_address_space(const SPIRVariable &argument)
|
|
|
|
{
|
|
|
|
const auto &type = get<SPIRType>(argument.basetype);
|
|
|
|
|
|
|
|
if ((type.basetype == SPIRType::Struct) &&
|
|
|
|
(type.storage == StorageClassUniform || type.storage == StorageClassUniformConstant ||
|
|
|
|
type.storage == StorageClassPushConstant))
|
|
|
|
{
|
|
|
|
if ((meta[type.self].decoration.decoration_flags & (1ull << DecorationBufferBlock)) != 0 &&
|
|
|
|
(meta[argument.self].decoration.decoration_flags & (1ull << DecorationNonWritable)) == 0)
|
|
|
|
{
|
|
|
|
return "device";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return "constant";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "thread";
|
|
|
|
}
|
|
|
|
|
2016-04-07 00:42:27 +03:00
|
|
|
// Returns a string containing a comma-delimited list of args for the entry point function
|
|
|
|
string CompilerMSL::entry_point_args(bool append_comma)
|
|
|
|
{
|
2016-05-05 10:33:18 +03:00
|
|
|
string ep_args;
|
|
|
|
|
2017-01-01 22:43:20 +03:00
|
|
|
// Stage-in structure
|
|
|
|
if (stage_in_var_id)
|
2016-05-05 10:33:18 +03:00
|
|
|
{
|
2017-01-01 22:43:20 +03:00
|
|
|
auto &var = get<SPIRVariable>(stage_in_var_id);
|
2016-05-05 10:33:18 +03:00
|
|
|
auto &type = get<SPIRType>(var.basetype);
|
|
|
|
|
|
|
|
if (!ep_args.empty())
|
|
|
|
ep_args += ", ";
|
2017-01-01 22:43:20 +03:00
|
|
|
|
|
|
|
ep_args += type_to_glsl(type) + " " + to_name(var.self) + " [[stage_in]]";
|
2016-05-05 10:33:18 +03:00
|
|
|
}
|
|
|
|
|
2017-01-29 21:28:20 +03:00
|
|
|
// Non-stage-in vertex attribute structures
|
|
|
|
for (auto &nsi_var : non_stage_in_input_var_ids)
|
|
|
|
{
|
|
|
|
auto &var = get<SPIRVariable>(nsi_var.second);
|
|
|
|
auto &type = get<SPIRType>(var.basetype);
|
|
|
|
|
|
|
|
if (!ep_args.empty())
|
|
|
|
ep_args += ", ";
|
|
|
|
|
|
|
|
ep_args += "device " + type_to_glsl(type) + "* " + to_name(var.self) + " [[buffer(" +
|
|
|
|
convert_to_string(nsi_var.first) + ")]]";
|
|
|
|
}
|
|
|
|
|
2016-05-05 10:33:18 +03:00
|
|
|
// Uniforms
|
|
|
|
for (auto &id : ids)
|
|
|
|
{
|
|
|
|
if (id.get_type() == TypeVariable)
|
|
|
|
{
|
|
|
|
auto &var = id.get<SPIRVariable>();
|
|
|
|
auto &type = get<SPIRType>(var.basetype);
|
2017-01-15 18:39:03 +03:00
|
|
|
|
2016-12-19 02:48:15 +03:00
|
|
|
if ((var.storage == StorageClassUniform || var.storage == StorageClassUniformConstant ||
|
2017-01-15 18:14:21 +03:00
|
|
|
var.storage == StorageClassPushConstant))
|
2016-05-05 10:33:18 +03:00
|
|
|
{
|
|
|
|
switch (type.basetype)
|
|
|
|
{
|
|
|
|
case SPIRType::Struct:
|
2017-01-26 12:19:26 +03:00
|
|
|
{
|
|
|
|
auto &m = meta.at(type.self);
|
2017-01-26 12:20:42 +03:00
|
|
|
if (m.members.size() == 0)
|
|
|
|
break;
|
2016-05-05 10:33:18 +03:00
|
|
|
if (!ep_args.empty())
|
|
|
|
ep_args += ", ";
|
2017-01-27 03:40:56 +03:00
|
|
|
ep_args += get_argument_address_space(var) + " " + type_to_glsl(type) + "& " + to_name(var.self);
|
2016-05-05 10:33:18 +03:00
|
|
|
ep_args += " [[buffer(" + convert_to_string(get_metal_resource_index(var, type.basetype)) + ")]]";
|
|
|
|
break;
|
2017-01-26 12:19:26 +03:00
|
|
|
}
|
2016-05-05 10:33:18 +03:00
|
|
|
case SPIRType::Sampler:
|
|
|
|
if (!ep_args.empty())
|
|
|
|
ep_args += ", ";
|
|
|
|
ep_args += type_to_glsl(type) + " " + to_name(var.self);
|
|
|
|
ep_args += " [[sampler(" + convert_to_string(get_metal_resource_index(var, type.basetype)) + ")]]";
|
|
|
|
break;
|
|
|
|
case SPIRType::Image:
|
|
|
|
if (!ep_args.empty())
|
|
|
|
ep_args += ", ";
|
|
|
|
ep_args += type_to_glsl(type) + " " + to_name(var.self);
|
|
|
|
ep_args += " [[texture(" + convert_to_string(get_metal_resource_index(var, type.basetype)) + ")]]";
|
|
|
|
break;
|
|
|
|
case SPIRType::SampledImage:
|
|
|
|
if (!ep_args.empty())
|
|
|
|
ep_args += ", ";
|
|
|
|
ep_args += type_to_glsl(type) + " " + to_name(var.self);
|
|
|
|
ep_args +=
|
|
|
|
" [[texture(" + convert_to_string(get_metal_resource_index(var, SPIRType::Image)) + ")]]";
|
|
|
|
if (type.image.dim != DimBuffer)
|
|
|
|
{
|
|
|
|
ep_args += ", sampler " + to_sampler_expression(var.self);
|
|
|
|
ep_args +=
|
|
|
|
" [[sampler(" + convert_to_string(get_metal_resource_index(var, SPIRType::Sampler)) + ")]]";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (var.storage == StorageClassInput && is_builtin_variable(var))
|
|
|
|
{
|
|
|
|
if (!ep_args.empty())
|
|
|
|
ep_args += ", ";
|
|
|
|
BuiltIn bi_type = meta[var.self].decoration.builtin_type;
|
|
|
|
ep_args += builtin_type_decl(bi_type) + " " + to_expression(var.self);
|
|
|
|
ep_args += " [[" + builtin_qualifier(bi_type) + "]]";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-29 21:28:20 +03:00
|
|
|
// Vertex and instance index built-ins
|
|
|
|
if (needs_vertex_idx_arg)
|
|
|
|
ep_args += built_in_func_arg(BuiltInVertexIndex, !ep_args.empty());
|
|
|
|
|
|
|
|
if (needs_instance_idx_arg)
|
|
|
|
ep_args += built_in_func_arg(BuiltInInstanceIndex, !ep_args.empty());
|
|
|
|
|
2016-05-05 10:33:18 +03:00
|
|
|
if (!ep_args.empty() && append_comma)
|
|
|
|
ep_args += ", ";
|
|
|
|
|
|
|
|
return ep_args;
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the Metal index of the resource of the specified type as used by the specified variable.
|
2016-05-05 10:33:18 +03:00
|
|
|
uint32_t CompilerMSL::get_metal_resource_index(SPIRVariable &var, SPIRType::BaseType basetype)
|
|
|
|
{
|
2016-07-28 12:16:02 +03:00
|
|
|
auto &execution = get_entry_point();
|
2016-05-05 10:33:18 +03:00
|
|
|
auto &var_dec = meta[var.self].decoration;
|
|
|
|
uint32_t var_desc_set = (var.storage == StorageClassPushConstant) ? kPushConstDescSet : var_dec.set;
|
|
|
|
uint32_t var_binding = (var.storage == StorageClassPushConstant) ? kPushConstBinding : var_dec.binding;
|
|
|
|
|
|
|
|
// If a matching binding has been specified, find and use it
|
|
|
|
for (auto p_res_bind : resource_bindings)
|
|
|
|
{
|
|
|
|
if (p_res_bind->stage == execution.model && p_res_bind->desc_set == var_desc_set &&
|
|
|
|
p_res_bind->binding == var_binding)
|
|
|
|
{
|
|
|
|
|
|
|
|
p_res_bind->used_by_shader = true;
|
|
|
|
switch (basetype)
|
|
|
|
{
|
|
|
|
case SPIRType::Struct:
|
|
|
|
return p_res_bind->msl_buffer;
|
|
|
|
case SPIRType::Image:
|
|
|
|
return p_res_bind->msl_texture;
|
|
|
|
case SPIRType::Sampler:
|
|
|
|
return p_res_bind->msl_sampler;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If a binding has not been specified, revert to incrementing resource indices
|
|
|
|
switch (basetype)
|
|
|
|
{
|
|
|
|
case SPIRType::Struct:
|
|
|
|
return next_metal_resource_index.msl_buffer++;
|
|
|
|
case SPIRType::Image:
|
|
|
|
return next_metal_resource_index.msl_texture++;
|
|
|
|
case SPIRType::Sampler:
|
|
|
|
return next_metal_resource_index.msl_sampler++;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the name of the entry point of this shader
|
|
|
|
string CompilerMSL::get_entry_point_name()
|
|
|
|
{
|
2016-07-28 12:16:02 +03:00
|
|
|
return clean_func_name(to_name(entry_point));
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
string CompilerMSL::argument_decl(const SPIRFunction::Parameter &arg)
|
|
|
|
{
|
2016-05-05 10:33:18 +03:00
|
|
|
auto &type = expression_type(arg.id);
|
2017-02-05 12:50:14 +03:00
|
|
|
bool constref = !arg.alias_global_variable && (!type.pointer || arg.write_count == 0);
|
2017-01-15 18:39:03 +03:00
|
|
|
|
|
|
|
// TODO: Check if this arg is an uniform pointer
|
|
|
|
bool pointer = type.storage == StorageClassUniformConstant;
|
2016-04-07 00:42:27 +03:00
|
|
|
|
2016-05-05 10:33:18 +03:00
|
|
|
auto &var = get<SPIRVariable>(arg.id);
|
2017-01-15 18:39:03 +03:00
|
|
|
return join(constref ? "const " : "", type_to_glsl(type), pointer ? " " : "& ", to_name(var.self),
|
|
|
|
type_to_array_glsl(type));
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
2016-07-08 19:39:22 +03:00
|
|
|
// If we're currently in the entry point function, and the object
|
|
|
|
// has a qualified name, use it, otherwise use the standard name.
|
|
|
|
string CompilerMSL::to_name(uint32_t id, bool allow_alias)
|
|
|
|
{
|
2016-10-24 16:24:24 +03:00
|
|
|
if (current_function && (current_function->self == entry_point))
|
|
|
|
{
|
|
|
|
string qual_name = meta.at(id).decoration.qualified_alias;
|
|
|
|
if (!qual_name.empty())
|
|
|
|
return qual_name;
|
|
|
|
}
|
|
|
|
return Compiler::to_name(id, allow_alias);
|
2016-07-08 19:39:22 +03:00
|
|
|
}
|
|
|
|
|
2016-11-11 05:07:50 +03:00
|
|
|
// Returns a name that combines the name of the struct with the name of the member, except for Builtins
|
2016-10-24 04:42:54 +03:00
|
|
|
string CompilerMSL::to_qualified_member_name(const SPIRType &type, uint32_t index)
|
|
|
|
{
|
2016-11-11 05:07:50 +03:00
|
|
|
// Don't qualify Builtin names because they are unique and are treated as such when building expressions
|
2016-12-18 01:07:53 +03:00
|
|
|
BuiltIn builtin;
|
|
|
|
if (is_member_builtin(type, index, &builtin))
|
|
|
|
return builtin_to_glsl(builtin);
|
2016-11-11 05:07:50 +03:00
|
|
|
|
|
|
|
// Strip any underscore prefix from member name
|
2016-12-18 01:07:53 +03:00
|
|
|
string mbr_name = to_member_name(type, index);
|
2016-10-24 16:24:24 +03:00
|
|
|
size_t startPos = mbr_name.find_first_not_of("_");
|
|
|
|
mbr_name = (startPos != std::string::npos) ? mbr_name.substr(startPos) : "";
|
|
|
|
return join(to_name(type.self), "_", mbr_name);
|
2016-10-24 04:42:54 +03:00
|
|
|
}
|
|
|
|
|
2016-11-27 23:00:06 +03:00
|
|
|
// Ensures that the specified name is permanently usable by prepending a prefix
|
|
|
|
// if the first chars are _ and a digit, which indicate a transient name.
|
|
|
|
string CompilerMSL::ensure_valid_name(string name, string pfx)
|
2016-10-24 04:42:54 +03:00
|
|
|
{
|
2017-01-15 18:39:03 +03:00
|
|
|
if (name.size() >= 2 && name[0] == '_' && isdigit(name[1]))
|
|
|
|
{
|
|
|
|
return join(pfx, name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-01-23 12:00:40 +03:00
|
|
|
auto iter = var_name_overrides.find(name);
|
|
|
|
return (iter != var_name_overrides.end()) ? iter->second : name;
|
2017-01-15 18:39:03 +03:00
|
|
|
}
|
2016-10-24 04:42:54 +03:00
|
|
|
}
|
|
|
|
|
2016-04-07 00:42:27 +03:00
|
|
|
// Returns an MSL string describing the SPIR-V type
|
|
|
|
string CompilerMSL::type_to_glsl(const SPIRType &type)
|
|
|
|
{
|
2016-05-05 10:33:18 +03:00
|
|
|
// Ignore the pointer type since GLSL doesn't have pointers.
|
|
|
|
|
|
|
|
switch (type.basetype)
|
|
|
|
{
|
|
|
|
case SPIRType::Struct:
|
|
|
|
// Need OpName lookup here to get a "sensible" name for a struct.
|
|
|
|
return to_name(type.self);
|
|
|
|
|
|
|
|
case SPIRType::Image:
|
|
|
|
case SPIRType::SampledImage:
|
|
|
|
return image_type_glsl(type);
|
|
|
|
|
|
|
|
case SPIRType::Sampler:
|
|
|
|
// Not really used.
|
|
|
|
return "sampler";
|
|
|
|
|
|
|
|
case SPIRType::Void:
|
|
|
|
return "void";
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_scalar(type)) // Scalar builtin
|
|
|
|
{
|
|
|
|
switch (type.basetype)
|
|
|
|
{
|
2016-06-05 21:13:45 +03:00
|
|
|
case SPIRType::Boolean:
|
2016-05-05 10:33:18 +03:00
|
|
|
return "bool";
|
|
|
|
case SPIRType::Char:
|
|
|
|
return "char";
|
|
|
|
case SPIRType::Int:
|
|
|
|
return (type.width == 16 ? "short" : "int");
|
|
|
|
case SPIRType::UInt:
|
|
|
|
return (type.width == 16 ? "ushort" : "uint");
|
|
|
|
case SPIRType::AtomicCounter:
|
|
|
|
return "atomic_uint";
|
|
|
|
case SPIRType::Float:
|
|
|
|
return (type.width == 16 ? "half" : "float");
|
|
|
|
default:
|
|
|
|
return "unknown_type";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (is_vector(type)) // Vector builtin
|
|
|
|
{
|
|
|
|
switch (type.basetype)
|
|
|
|
{
|
2016-06-05 21:13:45 +03:00
|
|
|
case SPIRType::Boolean:
|
2016-05-05 10:33:18 +03:00
|
|
|
return join("bool", type.vecsize);
|
|
|
|
case SPIRType::Char:
|
|
|
|
return join("char", type.vecsize);
|
|
|
|
case SPIRType::Int:
|
|
|
|
return join((type.width == 16 ? "short" : "int"), type.vecsize);
|
|
|
|
case SPIRType::UInt:
|
|
|
|
return join((type.width == 16 ? "ushort" : "uint"), type.vecsize);
|
|
|
|
case SPIRType::Float:
|
|
|
|
return join((type.width == 16 ? "half" : "float"), type.vecsize);
|
|
|
|
default:
|
|
|
|
return "unknown_type";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (type.basetype)
|
|
|
|
{
|
2016-06-05 21:13:45 +03:00
|
|
|
case SPIRType::Boolean:
|
2016-05-05 10:33:18 +03:00
|
|
|
case SPIRType::Int:
|
|
|
|
case SPIRType::UInt:
|
|
|
|
case SPIRType::Float:
|
|
|
|
return join((type.width == 16 ? "half" : "float"), type.columns, "x", type.vecsize);
|
|
|
|
default:
|
|
|
|
return "unknown_type";
|
|
|
|
}
|
|
|
|
}
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns an MSL string describing the SPIR-V image type
|
|
|
|
string CompilerMSL::image_type_glsl(const SPIRType &type)
|
|
|
|
{
|
2016-05-05 10:33:18 +03:00
|
|
|
string img_type_name;
|
|
|
|
|
|
|
|
auto &img_type = type.image;
|
|
|
|
if (img_type.depth)
|
|
|
|
{
|
|
|
|
switch (img_type.dim)
|
|
|
|
{
|
|
|
|
case spv::Dim2D:
|
|
|
|
img_type_name += (img_type.ms ? "depth2d_ms" : (img_type.arrayed ? "depth2d_array" : "depth2d"));
|
|
|
|
break;
|
|
|
|
case spv::DimCube:
|
|
|
|
img_type_name += (img_type.arrayed ? "depthcube_array" : "depthcube");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
img_type_name += "unknown_depth_texture_type";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (img_type.dim)
|
|
|
|
{
|
|
|
|
case spv::Dim1D:
|
|
|
|
img_type_name += (img_type.arrayed ? "texture1d_array" : "texture1d");
|
|
|
|
break;
|
|
|
|
case spv::DimBuffer:
|
|
|
|
case spv::Dim2D:
|
|
|
|
img_type_name += (img_type.ms ? "texture2d_ms" : (img_type.arrayed ? "texture2d_array" : "texture2d"));
|
|
|
|
break;
|
|
|
|
case spv::Dim3D:
|
2016-11-27 23:00:06 +03:00
|
|
|
img_type_name += "texture3d";
|
2016-05-05 10:33:18 +03:00
|
|
|
break;
|
|
|
|
case spv::DimCube:
|
|
|
|
img_type_name += (img_type.arrayed ? "texturecube_array" : "texturecube");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
img_type_name += "unknown_texture_type";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append the pixel type
|
|
|
|
auto &img_pix_type = get<SPIRType>(img_type.type);
|
|
|
|
img_type_name += "<" + type_to_glsl(img_pix_type) + ">";
|
|
|
|
|
|
|
|
return img_type_name;
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
2016-12-18 01:07:53 +03:00
|
|
|
// Returns an MSL string identifying the name of a SPIR-V builtin.
|
|
|
|
// Output builtins are qualified with the name of the stage out structure.
|
2016-04-07 00:42:27 +03:00
|
|
|
string CompilerMSL::builtin_to_glsl(BuiltIn builtin)
|
|
|
|
{
|
2016-05-05 10:33:18 +03:00
|
|
|
switch (builtin)
|
|
|
|
{
|
2016-12-18 01:07:53 +03:00
|
|
|
|
|
|
|
// Override GLSL compiler strictness
|
2016-05-05 10:33:18 +03:00
|
|
|
case BuiltInVertexId:
|
|
|
|
return "gl_VertexID";
|
|
|
|
case BuiltInInstanceId:
|
|
|
|
return "gl_InstanceID";
|
|
|
|
case BuiltInVertexIndex:
|
|
|
|
return "gl_VertexIndex";
|
|
|
|
case BuiltInInstanceIndex:
|
|
|
|
return "gl_InstanceIndex";
|
2016-12-18 01:07:53 +03:00
|
|
|
|
|
|
|
// Output builtins qualified with output struct when used in the entry function
|
|
|
|
case BuiltInPosition:
|
|
|
|
case BuiltInPointSize:
|
|
|
|
case BuiltInClipDistance:
|
2016-05-05 10:33:18 +03:00
|
|
|
case BuiltInLayer:
|
2016-12-18 01:07:53 +03:00
|
|
|
if (current_function && (current_function->self == entry_point))
|
|
|
|
return stage_out_var_name + "." + CompilerGLSL::builtin_to_glsl(builtin);
|
|
|
|
else
|
|
|
|
return CompilerGLSL::builtin_to_glsl(builtin);
|
|
|
|
|
2016-05-05 10:33:18 +03:00
|
|
|
default:
|
2016-12-18 01:07:53 +03:00
|
|
|
return CompilerGLSL::builtin_to_glsl(builtin);
|
2016-05-05 10:33:18 +03:00
|
|
|
}
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns an MSL string attribute qualifer for a SPIR-V builtin
|
|
|
|
string CompilerMSL::builtin_qualifier(BuiltIn builtin)
|
|
|
|
{
|
2016-07-28 12:16:02 +03:00
|
|
|
auto &execution = get_entry_point();
|
|
|
|
|
2016-05-05 10:33:18 +03:00
|
|
|
switch (builtin)
|
|
|
|
{
|
|
|
|
// Vertex function in
|
|
|
|
case BuiltInVertexId:
|
|
|
|
return "vertex_id";
|
|
|
|
case BuiltInVertexIndex:
|
|
|
|
return "vertex_id";
|
|
|
|
case BuiltInInstanceId:
|
|
|
|
return "instance_id";
|
|
|
|
case BuiltInInstanceIndex:
|
|
|
|
return "instance_id";
|
|
|
|
|
|
|
|
// Vertex function out
|
|
|
|
case BuiltInClipDistance:
|
|
|
|
return "clip_distance";
|
|
|
|
case BuiltInPointSize:
|
|
|
|
return "point_size";
|
|
|
|
case BuiltInPosition:
|
|
|
|
return "position";
|
|
|
|
case BuiltInLayer:
|
|
|
|
return "render_target_array_index";
|
|
|
|
|
|
|
|
// Fragment function in
|
|
|
|
case BuiltInFrontFacing:
|
|
|
|
return "front_facing";
|
|
|
|
case BuiltInPointCoord:
|
|
|
|
return "point_coord";
|
2016-07-04 18:49:46 +03:00
|
|
|
case BuiltInFragCoord:
|
2016-05-05 10:33:18 +03:00
|
|
|
return "position";
|
|
|
|
case BuiltInSampleId:
|
|
|
|
return "sample_id";
|
|
|
|
case BuiltInSampleMask:
|
|
|
|
return "sample_mask";
|
|
|
|
|
|
|
|
// Fragment function out
|
|
|
|
case BuiltInFragDepth:
|
|
|
|
{
|
|
|
|
if (execution.flags & (1ull << ExecutionModeDepthGreater))
|
|
|
|
return "depth(greater)";
|
|
|
|
|
|
|
|
if (execution.flags & (1ull << ExecutionModeDepthLess))
|
|
|
|
return "depth(less)";
|
|
|
|
|
|
|
|
if (execution.flags & (1ull << ExecutionModeDepthUnchanged))
|
|
|
|
return "depth(any)";
|
|
|
|
}
|
|
|
|
|
2017-01-27 03:40:56 +03:00
|
|
|
// Compute function in
|
2017-01-22 21:09:04 +03:00
|
|
|
case BuiltInGlobalInvocationId:
|
|
|
|
return "thread_position_in_grid";
|
|
|
|
|
2017-01-27 04:22:31 +03:00
|
|
|
case BuiltInLocalInvocationId:
|
|
|
|
return "thread_position_in_threadgroup";
|
|
|
|
|
|
|
|
case BuiltInLocalInvocationIndex:
|
|
|
|
return "thread_index_in_threadgroup";
|
|
|
|
|
2016-05-05 10:33:18 +03:00
|
|
|
default:
|
|
|
|
return "unsupported-built-in";
|
|
|
|
}
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns an MSL string type declaration for a SPIR-V builtin
|
|
|
|
string CompilerMSL::builtin_type_decl(BuiltIn builtin)
|
|
|
|
{
|
2016-05-05 10:33:18 +03:00
|
|
|
switch (builtin)
|
|
|
|
{
|
|
|
|
// Vertex function in
|
|
|
|
case BuiltInVertexId:
|
|
|
|
return "uint";
|
|
|
|
case BuiltInVertexIndex:
|
|
|
|
return "uint";
|
|
|
|
case BuiltInInstanceId:
|
|
|
|
return "uint";
|
|
|
|
case BuiltInInstanceIndex:
|
|
|
|
return "uint";
|
|
|
|
|
|
|
|
// Vertex function out
|
|
|
|
case BuiltInClipDistance:
|
|
|
|
return "float";
|
|
|
|
case BuiltInPointSize:
|
|
|
|
return "float";
|
|
|
|
case BuiltInPosition:
|
|
|
|
return "float4";
|
|
|
|
|
|
|
|
// Fragment function in
|
|
|
|
case BuiltInFrontFacing:
|
|
|
|
return "bool";
|
|
|
|
case BuiltInPointCoord:
|
|
|
|
return "float2";
|
2016-07-04 18:49:46 +03:00
|
|
|
case BuiltInFragCoord:
|
2016-05-05 10:33:18 +03:00
|
|
|
return "float4";
|
|
|
|
case BuiltInSampleId:
|
|
|
|
return "uint";
|
|
|
|
case BuiltInSampleMask:
|
|
|
|
return "uint";
|
|
|
|
|
2017-01-22 21:09:04 +03:00
|
|
|
// Compute function in
|
|
|
|
case BuiltInGlobalInvocationId:
|
|
|
|
return "uint3";
|
2017-01-27 04:22:31 +03:00
|
|
|
case BuiltInLocalInvocationId:
|
|
|
|
return "uint3";
|
|
|
|
case BuiltInLocalInvocationIndex:
|
|
|
|
return "uint";
|
2017-01-22 21:09:04 +03:00
|
|
|
|
2016-05-05 10:33:18 +03:00
|
|
|
default:
|
|
|
|
return "unsupported-built-in-type";
|
|
|
|
}
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
2017-01-29 21:28:20 +03:00
|
|
|
// Returns the declaration of a built-in argument to a function
|
|
|
|
string CompilerMSL::built_in_func_arg(BuiltIn builtin, bool prefix_comma)
|
|
|
|
{
|
|
|
|
string bi_arg;
|
|
|
|
if (prefix_comma)
|
|
|
|
bi_arg += ", ";
|
|
|
|
bi_arg += builtin_type_decl(builtin);
|
|
|
|
bi_arg += " " + builtin_to_glsl(builtin);
|
|
|
|
bi_arg += " [[" + builtin_qualifier(builtin) + "]]";
|
|
|
|
return bi_arg;
|
|
|
|
}
|
|
|
|
|
2016-04-07 00:42:27 +03:00
|
|
|
// Returns the effective size of a buffer block struct member.
|
2016-05-05 10:33:18 +03:00
|
|
|
size_t CompilerMSL::get_declared_struct_member_size(const SPIRType &struct_type, uint32_t index) const
|
2016-04-07 00:42:27 +03:00
|
|
|
{
|
2016-12-18 01:07:53 +03:00
|
|
|
uint32_t type_id = struct_type.member_types[index];
|
2016-05-05 10:33:18 +03:00
|
|
|
auto dec_mask = get_member_decoration_mask(struct_type.self, index);
|
2016-12-18 01:07:53 +03:00
|
|
|
return get_declared_type_size(type_id, dec_mask);
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the effective size of a variable type.
|
2016-12-18 01:07:53 +03:00
|
|
|
size_t CompilerMSL::get_declared_type_size(uint32_t type_id) const
|
2016-04-07 00:42:27 +03:00
|
|
|
{
|
2016-12-18 01:07:53 +03:00
|
|
|
return get_declared_type_size(type_id, get_decoration_mask(type_id));
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the effective size of a variable type or member type,
|
|
|
|
// taking into consideration the specified mask of decorations.
|
2016-12-18 01:07:53 +03:00
|
|
|
size_t CompilerMSL::get_declared_type_size(uint32_t type_id, uint64_t dec_mask) const
|
2016-04-07 00:42:27 +03:00
|
|
|
{
|
2016-12-18 01:07:53 +03:00
|
|
|
auto &type = get<SPIRType>(type_id);
|
|
|
|
|
2016-10-24 04:42:54 +03:00
|
|
|
if (type.basetype == SPIRType::Struct)
|
2016-10-24 16:24:24 +03:00
|
|
|
return get_declared_struct_size(type);
|
|
|
|
|
|
|
|
switch (type.basetype)
|
|
|
|
{
|
|
|
|
case SPIRType::Unknown:
|
|
|
|
case SPIRType::Void:
|
|
|
|
case SPIRType::AtomicCounter:
|
|
|
|
case SPIRType::Image:
|
|
|
|
case SPIRType::SampledImage:
|
|
|
|
case SPIRType::Sampler:
|
2016-12-13 00:33:22 +03:00
|
|
|
SPIRV_CROSS_THROW("Querying size of object with opaque size.");
|
2016-10-24 16:24:24 +03:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t component_size = type.width / 8;
|
|
|
|
unsigned vecsize = type.vecsize;
|
|
|
|
unsigned columns = type.columns;
|
|
|
|
|
2016-12-18 01:07:53 +03:00
|
|
|
if (!type.array.empty())
|
2016-10-24 16:24:24 +03:00
|
|
|
{
|
2016-12-18 01:07:53 +03:00
|
|
|
// For arrays, we can use ArrayStride to get an easy check if it has been populated.
|
2016-10-24 16:24:24 +03:00
|
|
|
// ArrayStride is part of the array type not OpMemberDecorate.
|
2016-12-18 01:07:53 +03:00
|
|
|
auto &dec = meta[type_id].decoration;
|
2016-10-24 16:24:24 +03:00
|
|
|
if (dec.decoration_flags & (1ull << DecorationArrayStride))
|
2016-10-27 17:20:01 +03:00
|
|
|
return dec.array_stride * to_array_size_literal(type, uint32_t(type.array.size()) - 1);
|
2016-10-24 16:24:24 +03:00
|
|
|
}
|
2016-12-18 01:07:53 +03:00
|
|
|
|
|
|
|
// Vectors.
|
|
|
|
if (columns == 1)
|
|
|
|
return vecsize * component_size;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Per SPIR-V spec, matrices must be tightly packed and aligned up for vec3 accesses.
|
|
|
|
if ((dec_mask & (1ull << DecorationRowMajor)) && columns == 3)
|
|
|
|
columns = 4;
|
|
|
|
else if ((dec_mask & (1ull << DecorationColMajor)) && vecsize == 3)
|
|
|
|
vecsize = 4;
|
|
|
|
|
|
|
|
return vecsize * columns * component_size;
|
2016-10-24 16:24:24 +03:00
|
|
|
}
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
2017-01-20 19:24:44 +03:00
|
|
|
bool CompilerMSL::OpCodePreprocessor::handle(Op opcode, const uint32_t * /*args*/, uint32_t /*length*/)
|
2016-12-22 00:31:13 +03:00
|
|
|
{
|
|
|
|
switch (opcode)
|
|
|
|
{
|
2017-01-20 19:33:59 +03:00
|
|
|
// If an opcode requires a bespoke custom function be output, remember it.
|
2016-12-22 00:31:13 +03:00
|
|
|
case OpFMod:
|
2017-01-20 19:33:59 +03:00
|
|
|
compiler.custom_function_ops.insert(uint32_t(opcode));
|
|
|
|
break;
|
|
|
|
|
|
|
|
// Since MSL exists in a single execution scope, function prototype declarations are not
|
|
|
|
// needed, and clutter the output. If secondary functions are output (as indicated by the
|
|
|
|
// presence of OpFunctionCall, then suppress compiler warnings of missing function prototypes.
|
|
|
|
case OpFunctionCall:
|
|
|
|
suppress_missing_prototypes = true;
|
2016-12-22 00:31:13 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
2016-12-19 02:48:15 +03:00
|
|
|
// Sort both type and meta member content based on builtin status (put builtins at end),
|
|
|
|
// then by the required sorting aspect.
|
2016-12-22 00:31:13 +03:00
|
|
|
void CompilerMSL::MemberSorter::sort()
|
2016-04-07 00:42:27 +03:00
|
|
|
{
|
2017-01-31 19:02:44 +03:00
|
|
|
// Create a temporary array of consecutive member indices and sort it based on how
|
2016-12-19 02:48:15 +03:00
|
|
|
// the members should be reordered, based on builtin and sorting aspect meta info.
|
2016-05-05 10:33:18 +03:00
|
|
|
size_t mbr_cnt = type.member_types.size();
|
|
|
|
vector<uint32_t> mbr_idxs(mbr_cnt);
|
2017-01-31 19:02:44 +03:00
|
|
|
iota(mbr_idxs.begin(), mbr_idxs.end(), 0); // Fill with consecutive indices
|
2016-12-19 02:48:15 +03:00
|
|
|
std::sort(mbr_idxs.begin(), mbr_idxs.end(), *this); // Sort member indices based on sorting aspect
|
2016-05-05 10:33:18 +03:00
|
|
|
|
|
|
|
// Move type and meta member info to the order defined by the sorted member indices.
|
|
|
|
// This is done by creating temporary copies of both member types and meta, and then
|
|
|
|
// copying back to the original content at the sorted indices.
|
|
|
|
auto mbr_types_cpy = type.member_types;
|
|
|
|
auto mbr_meta_cpy = meta.members;
|
|
|
|
for (uint32_t mbr_idx = 0; mbr_idx < mbr_cnt; mbr_idx++)
|
|
|
|
{
|
|
|
|
type.member_types[mbr_idx] = mbr_types_cpy[mbr_idxs[mbr_idx]];
|
|
|
|
meta.members[mbr_idx] = mbr_meta_cpy[mbr_idxs[mbr_idx]];
|
|
|
|
}
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|
|
|
|
|
2016-12-19 02:48:15 +03:00
|
|
|
// Sort first by builtin status (put builtins at end), then by the sorting aspect.
|
2016-12-22 00:31:13 +03:00
|
|
|
bool CompilerMSL::MemberSorter::operator()(uint32_t mbr_idx1, uint32_t mbr_idx2)
|
2016-04-07 00:42:27 +03:00
|
|
|
{
|
2016-05-05 10:33:18 +03:00
|
|
|
auto &mbr_meta1 = meta.members[mbr_idx1];
|
|
|
|
auto &mbr_meta2 = meta.members[mbr_idx2];
|
|
|
|
if (mbr_meta1.builtin != mbr_meta2.builtin)
|
|
|
|
return mbr_meta2.builtin;
|
|
|
|
else
|
2016-12-04 20:32:58 +03:00
|
|
|
switch (sort_aspect)
|
|
|
|
{
|
|
|
|
case Location:
|
|
|
|
return mbr_meta1.location < mbr_meta2.location;
|
2016-12-19 02:48:15 +03:00
|
|
|
case LocationReverse:
|
|
|
|
return mbr_meta1.location > mbr_meta2.location;
|
2016-12-04 20:32:58 +03:00
|
|
|
case Offset:
|
|
|
|
return mbr_meta1.offset < mbr_meta2.offset;
|
2016-12-19 02:48:15 +03:00
|
|
|
case OffsetThenLocationReverse:
|
|
|
|
return (mbr_meta1.offset < mbr_meta2.offset) ||
|
|
|
|
((mbr_meta1.offset == mbr_meta2.offset) && (mbr_meta1.location > mbr_meta2.location));
|
2017-01-15 18:14:21 +03:00
|
|
|
case Alphabetical:
|
|
|
|
return mbr_meta1.alias > mbr_meta2.alias;
|
2016-12-04 20:32:58 +03:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-07 00:42:27 +03:00
|
|
|
}
|