2016-03-02 17:09:16 +00:00
|
|
|
/*
|
2019-01-04 11:38:35 +00:00
|
|
|
* Copyright 2015-2019 Arm Limited
|
2016-03-02 17:09:16 +00: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.
|
|
|
|
*/
|
|
|
|
|
2016-04-04 07:36:04 +00:00
|
|
|
#include "spirv_cpp.hpp"
|
2016-03-02 17:09:16 +00:00
|
|
|
|
|
|
|
using namespace spv;
|
2019-03-29 09:29:44 +00:00
|
|
|
using namespace SPIRV_CROSS_NAMESPACE;
|
2016-03-02 17:09:16 +00:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
void CompilerCPP::emit_buffer_block(const SPIRVariable &var)
|
|
|
|
{
|
2016-05-23 10:25:09 +00:00
|
|
|
add_resource_name(var.self);
|
|
|
|
|
2016-05-05 07:33:18 +00:00
|
|
|
auto &type = get<SPIRType>(var.basetype);
|
|
|
|
auto instance_name = to_name(var.self);
|
|
|
|
|
2018-10-05 09:30:57 +00:00
|
|
|
uint32_t descriptor_set = ir.meta[var.self].decoration.set;
|
|
|
|
uint32_t binding = ir.meta[var.self].decoration.binding;
|
2016-05-05 07:33:18 +00:00
|
|
|
|
2016-05-23 12:18:00 +00:00
|
|
|
emit_block_struct(type);
|
|
|
|
auto buffer_name = to_name(type.self);
|
|
|
|
|
|
|
|
statement("internal::Resource<", buffer_name, type_to_array_glsl(type), "> ", instance_name, "__;");
|
2016-05-05 07:33:18 +00:00
|
|
|
statement_no_indent("#define ", instance_name, " __res->", instance_name, "__.get()");
|
|
|
|
resource_registrations.push_back(
|
|
|
|
join("s.register_resource(", instance_name, "__", ", ", descriptor_set, ", ", binding, ");"));
|
|
|
|
statement("");
|
2016-03-02 17:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CompilerCPP::emit_interface_block(const SPIRVariable &var)
|
|
|
|
{
|
2016-05-23 10:25:09 +00:00
|
|
|
add_resource_name(var.self);
|
|
|
|
|
2016-05-05 07:33:18 +00:00
|
|
|
auto &type = get<SPIRType>(var.basetype);
|
2016-03-02 17:09:16 +00:00
|
|
|
|
2016-05-05 07:33:18 +00:00
|
|
|
const char *qual = var.storage == StorageClassInput ? "StageInput" : "StageOutput";
|
|
|
|
const char *lowerqual = var.storage == StorageClassInput ? "stage_input" : "stage_output";
|
|
|
|
auto instance_name = to_name(var.self);
|
2018-10-05 09:30:57 +00:00
|
|
|
uint32_t location = ir.meta[var.self].decoration.location;
|
2016-03-02 17:09:16 +00:00
|
|
|
|
2016-08-26 11:43:21 +00:00
|
|
|
string buffer_name;
|
2018-10-05 09:30:57 +00:00
|
|
|
auto flags = ir.meta[type.self].decoration.decoration_flags;
|
2018-03-12 12:09:25 +00:00
|
|
|
if (flags.get(DecorationBlock))
|
2016-08-26 11:43:21 +00:00
|
|
|
{
|
2016-05-23 12:18:00 +00:00
|
|
|
emit_block_struct(type);
|
2016-08-26 11:43:21 +00:00
|
|
|
buffer_name = to_name(type.self);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
buffer_name = type_to_glsl(type);
|
2016-03-02 17:09:16 +00:00
|
|
|
|
2016-05-23 12:18:00 +00:00
|
|
|
statement("internal::", qual, "<", buffer_name, type_to_array_glsl(type), "> ", instance_name, "__;");
|
2016-05-05 07:33:18 +00:00
|
|
|
statement_no_indent("#define ", instance_name, " __res->", instance_name, "__.get()");
|
|
|
|
resource_registrations.push_back(join("s.register_", lowerqual, "(", instance_name, "__", ", ", location, ");"));
|
|
|
|
statement("");
|
2016-03-02 17:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CompilerCPP::emit_shared(const SPIRVariable &var)
|
|
|
|
{
|
2016-05-23 10:25:09 +00:00
|
|
|
add_resource_name(var.self);
|
|
|
|
|
2016-05-05 07:33:18 +00:00
|
|
|
auto instance_name = to_name(var.self);
|
2016-05-28 11:09:26 +00:00
|
|
|
statement(CompilerGLSL::variable_decl(var), ";");
|
2016-05-05 07:33:18 +00:00
|
|
|
statement_no_indent("#define ", instance_name, " __res->", instance_name);
|
2016-03-02 17:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CompilerCPP::emit_uniform(const SPIRVariable &var)
|
|
|
|
{
|
2016-05-23 10:25:09 +00:00
|
|
|
add_resource_name(var.self);
|
|
|
|
|
2016-05-05 07:33:18 +00:00
|
|
|
auto &type = get<SPIRType>(var.basetype);
|
|
|
|
auto instance_name = to_name(var.self);
|
|
|
|
|
2018-10-05 09:30:57 +00:00
|
|
|
uint32_t descriptor_set = ir.meta[var.self].decoration.set;
|
|
|
|
uint32_t binding = ir.meta[var.self].decoration.binding;
|
|
|
|
uint32_t location = ir.meta[var.self].decoration.location;
|
2016-05-05 07:33:18 +00:00
|
|
|
|
2016-09-20 08:55:09 +00:00
|
|
|
string type_name = type_to_glsl(type);
|
|
|
|
remap_variable_type_name(type, instance_name, type_name);
|
|
|
|
|
2016-05-05 07:33:18 +00:00
|
|
|
if (type.basetype == SPIRType::Image || type.basetype == SPIRType::SampledImage ||
|
|
|
|
type.basetype == SPIRType::AtomicCounter)
|
|
|
|
{
|
2016-09-20 08:55:09 +00:00
|
|
|
statement("internal::Resource<", type_name, type_to_array_glsl(type), "> ", instance_name, "__;");
|
2016-05-05 07:33:18 +00:00
|
|
|
statement_no_indent("#define ", instance_name, " __res->", instance_name, "__.get()");
|
|
|
|
resource_registrations.push_back(
|
|
|
|
join("s.register_resource(", instance_name, "__", ", ", descriptor_set, ", ", binding, ");"));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-09-21 06:20:16 +00:00
|
|
|
statement("internal::UniformConstant<", type_name, type_to_array_glsl(type), "> ", instance_name, "__;");
|
2016-05-05 07:33:18 +00:00
|
|
|
statement_no_indent("#define ", instance_name, " __res->", instance_name, "__.get()");
|
|
|
|
resource_registrations.push_back(
|
|
|
|
join("s.register_uniform_constant(", instance_name, "__", ", ", location, ");"));
|
|
|
|
}
|
|
|
|
|
|
|
|
statement("");
|
2016-03-02 17:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CompilerCPP::emit_push_constant_block(const SPIRVariable &var)
|
|
|
|
{
|
2016-05-23 10:25:09 +00:00
|
|
|
add_resource_name(var.self);
|
|
|
|
|
2016-05-05 07:33:18 +00:00
|
|
|
auto &type = get<SPIRType>(var.basetype);
|
2018-10-05 09:30:57 +00:00
|
|
|
auto &flags = ir.meta[var.self].decoration.decoration_flags;
|
2018-03-12 12:09:25 +00:00
|
|
|
if (flags.get(DecorationBinding) || flags.get(DecorationDescriptorSet))
|
2016-12-12 21:33:22 +00:00
|
|
|
SPIRV_CROSS_THROW("Push constant blocks cannot be compiled to GLSL with Binding or Set syntax. "
|
|
|
|
"Remap to location with reflection API first or disable these decorations.");
|
2016-05-05 07:33:18 +00:00
|
|
|
|
2016-05-23 12:18:00 +00:00
|
|
|
emit_block_struct(type);
|
|
|
|
auto buffer_name = to_name(type.self);
|
2016-05-05 07:33:18 +00:00
|
|
|
auto instance_name = to_name(var.self);
|
|
|
|
|
2016-05-23 12:18:00 +00:00
|
|
|
statement("internal::PushConstant<", buffer_name, type_to_array_glsl(type), "> ", instance_name, ";");
|
2016-05-05 07:33:18 +00:00
|
|
|
statement_no_indent("#define ", instance_name, " __res->", instance_name, ".get()");
|
|
|
|
resource_registrations.push_back(join("s.register_push_constant(", instance_name, "__", ");"));
|
|
|
|
statement("");
|
2016-03-02 17:09:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 12:18:00 +00:00
|
|
|
void CompilerCPP::emit_block_struct(SPIRType &type)
|
|
|
|
{
|
|
|
|
// C++ can't do interface blocks, so we fake it by emitting a separate struct.
|
|
|
|
// However, these structs are not allowed to alias anything, so remove it before
|
|
|
|
// emitting the struct.
|
|
|
|
//
|
|
|
|
// The type we have here needs to be resolved to the non-pointer type so we can remove aliases.
|
|
|
|
auto &self = get<SPIRType>(type.self);
|
|
|
|
self.type_alias = 0;
|
|
|
|
emit_struct(self);
|
|
|
|
}
|
|
|
|
|
2016-03-02 17:09:16 +00:00
|
|
|
void CompilerCPP::emit_resources()
|
|
|
|
{
|
2018-11-27 09:49:19 +00:00
|
|
|
for (auto &id : ir.ids)
|
|
|
|
{
|
|
|
|
if (id.get_type() == TypeConstant)
|
|
|
|
{
|
|
|
|
auto &c = id.get<SPIRConstant>();
|
|
|
|
|
|
|
|
bool needs_declaration = c.specialization || c.is_used_as_lut;
|
|
|
|
|
|
|
|
if (needs_declaration)
|
|
|
|
{
|
|
|
|
if (!options.vulkan_semantics && c.specialization)
|
|
|
|
{
|
|
|
|
c.specialization_constant_macro_name =
|
|
|
|
constant_value_macro_name(get_decoration(c.self, DecorationSpecId));
|
|
|
|
}
|
|
|
|
emit_constant(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (id.get_type() == TypeConstantOp)
|
|
|
|
{
|
|
|
|
emit_specialization_constant_op(id.get<SPIRConstantOp>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-05 07:33:18 +00:00
|
|
|
// Output all basic struct types which are not Block or BufferBlock as these are declared inplace
|
|
|
|
// when such variables are instantiated.
|
2018-10-05 09:30:57 +00:00
|
|
|
for (auto &id : ir.ids)
|
2016-05-05 07:33:18 +00:00
|
|
|
{
|
|
|
|
if (id.get_type() == TypeType)
|
|
|
|
{
|
|
|
|
auto &type = id.get<SPIRType>();
|
|
|
|
if (type.basetype == SPIRType::Struct && type.array.empty() && !type.pointer &&
|
2018-10-05 09:30:57 +00:00
|
|
|
(!ir.meta[type.self].decoration.decoration_flags.get(DecorationBlock) &&
|
|
|
|
!ir.meta[type.self].decoration.decoration_flags.get(DecorationBufferBlock)))
|
2016-05-05 07:33:18 +00:00
|
|
|
{
|
|
|
|
emit_struct(type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
statement("struct Resources : ", resource_type);
|
|
|
|
begin_scope();
|
|
|
|
|
|
|
|
// Output UBOs and SSBOs
|
2018-10-05 09:30:57 +00:00
|
|
|
for (auto &id : ir.ids)
|
2016-05-05 07:33:18 +00:00
|
|
|
{
|
|
|
|
if (id.get_type() == TypeVariable)
|
|
|
|
{
|
|
|
|
auto &var = id.get<SPIRVariable>();
|
|
|
|
auto &type = get<SPIRType>(var.basetype);
|
|
|
|
|
2016-07-05 11:21:26 +00:00
|
|
|
if (var.storage != StorageClassFunction && type.pointer && type.storage == StorageClassUniform &&
|
2017-11-17 12:38:29 +00:00
|
|
|
!is_hidden_variable(var) &&
|
2018-10-05 09:30:57 +00:00
|
|
|
(ir.meta[type.self].decoration.decoration_flags.get(DecorationBlock) ||
|
|
|
|
ir.meta[type.self].decoration.decoration_flags.get(DecorationBufferBlock)))
|
2016-05-05 07:33:18 +00:00
|
|
|
{
|
|
|
|
emit_buffer_block(var);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output push constant blocks
|
2018-10-05 09:30:57 +00:00
|
|
|
for (auto &id : ir.ids)
|
2016-05-05 07:33:18 +00:00
|
|
|
{
|
|
|
|
if (id.get_type() == TypeVariable)
|
|
|
|
{
|
|
|
|
auto &var = id.get<SPIRVariable>();
|
|
|
|
auto &type = get<SPIRType>(var.basetype);
|
2016-08-26 10:58:50 +00:00
|
|
|
if (!is_hidden_variable(var) && var.storage != StorageClassFunction && type.pointer &&
|
|
|
|
type.storage == StorageClassPushConstant)
|
|
|
|
{
|
2016-05-05 07:33:18 +00:00
|
|
|
emit_push_constant_block(var);
|
2016-08-26 10:58:50 +00:00
|
|
|
}
|
2016-05-05 07:33:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output in/out interfaces.
|
2018-10-05 09:30:57 +00:00
|
|
|
for (auto &id : ir.ids)
|
2016-05-05 07:33:18 +00:00
|
|
|
{
|
|
|
|
if (id.get_type() == TypeVariable)
|
|
|
|
{
|
|
|
|
auto &var = id.get<SPIRVariable>();
|
|
|
|
auto &type = get<SPIRType>(var.basetype);
|
|
|
|
|
2016-08-26 10:58:50 +00:00
|
|
|
if (var.storage != StorageClassFunction && !is_hidden_variable(var) && type.pointer &&
|
|
|
|
(var.storage == StorageClassInput || var.storage == StorageClassOutput) &&
|
2016-07-28 09:16:02 +00:00
|
|
|
interface_variable_exists_in_entry_point(var.self))
|
2016-05-05 07:33:18 +00:00
|
|
|
{
|
|
|
|
emit_interface_block(var);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Output Uniform Constants (values, samplers, images, etc).
|
2018-10-05 09:30:57 +00:00
|
|
|
for (auto &id : ir.ids)
|
2016-05-05 07:33:18 +00:00
|
|
|
{
|
|
|
|
if (id.get_type() == TypeVariable)
|
|
|
|
{
|
|
|
|
auto &var = id.get<SPIRVariable>();
|
|
|
|
auto &type = get<SPIRType>(var.basetype);
|
|
|
|
|
2016-08-26 10:58:50 +00:00
|
|
|
if (var.storage != StorageClassFunction && !is_hidden_variable(var) && type.pointer &&
|
2016-05-05 07:33:18 +00:00
|
|
|
(type.storage == StorageClassUniformConstant || type.storage == StorageClassAtomicCounter))
|
|
|
|
{
|
|
|
|
emit_uniform(var);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Global variables.
|
|
|
|
bool emitted = false;
|
|
|
|
for (auto global : global_variables)
|
|
|
|
{
|
|
|
|
auto &var = get<SPIRVariable>(global);
|
|
|
|
if (var.storage == StorageClassWorkgroup)
|
|
|
|
{
|
|
|
|
emit_shared(var);
|
|
|
|
emitted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (emitted)
|
|
|
|
statement("");
|
|
|
|
|
2017-08-02 08:33:03 +00:00
|
|
|
declare_undefined_values();
|
|
|
|
|
2016-05-05 07:33:18 +00:00
|
|
|
statement("inline void init(spirv_cross_shader& s)");
|
|
|
|
begin_scope();
|
|
|
|
statement(resource_type, "::init(s);");
|
|
|
|
for (auto ® : resource_registrations)
|
|
|
|
statement(reg);
|
|
|
|
end_scope();
|
|
|
|
resource_registrations.clear();
|
|
|
|
|
|
|
|
end_scope_decl();
|
|
|
|
|
|
|
|
statement("");
|
|
|
|
statement("Resources* __res;");
|
2016-07-28 09:16:02 +00:00
|
|
|
if (get_entry_point().model == ExecutionModelGLCompute)
|
2016-05-05 07:33:18 +00:00
|
|
|
statement("ComputePrivateResources __priv_res;");
|
|
|
|
statement("");
|
|
|
|
|
|
|
|
// Emit regular globals which are allocated per invocation.
|
|
|
|
emitted = false;
|
|
|
|
for (auto global : global_variables)
|
|
|
|
{
|
|
|
|
auto &var = get<SPIRVariable>(global);
|
|
|
|
if (var.storage == StorageClassPrivate)
|
|
|
|
{
|
|
|
|
if (var.storage == StorageClassWorkgroup)
|
|
|
|
emit_shared(var);
|
|
|
|
else
|
2016-05-28 11:09:26 +00:00
|
|
|
statement(CompilerGLSL::variable_decl(var), ";");
|
2016-05-05 07:33:18 +00:00
|
|
|
emitted = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (emitted)
|
|
|
|
statement("");
|
2016-03-02 17:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string CompilerCPP::compile()
|
|
|
|
{
|
2016-05-05 07:33:18 +00:00
|
|
|
// Do not deal with ES-isms like precision, older extensions and such.
|
|
|
|
options.es = false;
|
|
|
|
options.version = 450;
|
|
|
|
backend.float_literal_suffix = true;
|
2016-07-27 08:59:00 +00:00
|
|
|
backend.double_literal_suffix = false;
|
2016-07-27 09:27:00 +00:00
|
|
|
backend.long_long_literal_suffix = true;
|
2016-05-05 07:33:18 +00:00
|
|
|
backend.uint32_t_literal_suffix = true;
|
|
|
|
backend.basic_int_type = "int32_t";
|
|
|
|
backend.basic_uint_type = "uint32_t";
|
|
|
|
backend.swizzle_is_function = true;
|
|
|
|
backend.shared_is_implied = true;
|
2016-05-23 07:15:49 +00:00
|
|
|
backend.flexible_member_array_supported = false;
|
2016-05-23 10:25:09 +00:00
|
|
|
backend.explicit_struct_type = true;
|
2016-05-28 11:09:26 +00:00
|
|
|
backend.use_initializer_list = true;
|
2016-03-02 17:09:16 +00:00
|
|
|
|
2018-07-05 08:42:05 +00:00
|
|
|
build_function_control_flow_graphs_and_analyze();
|
2017-03-06 14:21:00 +00:00
|
|
|
update_active_builtins();
|
|
|
|
|
2016-05-05 07:33:18 +00:00
|
|
|
uint32_t pass_count = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (pass_count >= 3)
|
2016-12-12 21:33:22 +00:00
|
|
|
SPIRV_CROSS_THROW("Over 3 compilation loops detected. Must be a bug!");
|
2016-03-02 17:09:16 +00:00
|
|
|
|
2016-05-05 07:33:18 +00:00
|
|
|
resource_registrations.clear();
|
|
|
|
reset();
|
2016-03-02 17:09:16 +00:00
|
|
|
|
2016-05-05 07:33:18 +00:00
|
|
|
// Move constructor for this type is broken on GCC 4.9 ...
|
|
|
|
buffer = unique_ptr<ostringstream>(new ostringstream());
|
2016-03-02 17:09:16 +00:00
|
|
|
|
2016-05-05 07:33:18 +00:00
|
|
|
emit_header();
|
|
|
|
emit_resources();
|
2016-03-02 17:09:16 +00:00
|
|
|
|
2018-10-05 09:30:57 +00:00
|
|
|
emit_function(get<SPIRFunction>(ir.default_entry_point), Bitset());
|
2016-03-02 17:09:16 +00:00
|
|
|
|
2016-05-05 07:33:18 +00:00
|
|
|
pass_count++;
|
2019-04-05 10:06:10 +00:00
|
|
|
} while (is_forcing_recompilation());
|
2016-03-02 17:09:16 +00:00
|
|
|
|
2016-05-05 07:33:18 +00:00
|
|
|
// Match opening scope of emit_header().
|
|
|
|
end_scope_decl();
|
|
|
|
// namespace
|
|
|
|
end_scope();
|
2016-03-02 17:09:16 +00:00
|
|
|
|
2016-05-05 07:33:18 +00:00
|
|
|
// Emit C entry points
|
|
|
|
emit_c_linkage();
|
2016-03-02 17:09:16 +00:00
|
|
|
|
2017-11-13 12:49:11 +00:00
|
|
|
// Entry point in CPP is always main() for the time being.
|
|
|
|
get_entry_point().name = "main";
|
|
|
|
|
2016-05-05 07:33:18 +00:00
|
|
|
return buffer->str();
|
2016-03-02 17:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CompilerCPP::emit_c_linkage()
|
|
|
|
{
|
2016-05-05 07:33:18 +00:00
|
|
|
statement("");
|
|
|
|
|
2016-05-30 19:45:16 +00:00
|
|
|
statement("spirv_cross_shader_t *spirv_cross_construct(void)");
|
2016-05-05 07:33:18 +00:00
|
|
|
begin_scope();
|
|
|
|
statement("return new ", impl_type, "();");
|
|
|
|
end_scope();
|
|
|
|
|
|
|
|
statement("");
|
|
|
|
statement("void spirv_cross_destruct(spirv_cross_shader_t *shader)");
|
|
|
|
begin_scope();
|
|
|
|
statement("delete static_cast<", impl_type, "*>(shader);");
|
|
|
|
end_scope();
|
|
|
|
|
|
|
|
statement("");
|
|
|
|
statement("void spirv_cross_invoke(spirv_cross_shader_t *shader)");
|
|
|
|
begin_scope();
|
|
|
|
statement("static_cast<", impl_type, "*>(shader)->invoke();");
|
|
|
|
end_scope();
|
|
|
|
|
|
|
|
statement("");
|
|
|
|
statement("static const struct spirv_cross_interface vtable =");
|
|
|
|
begin_scope();
|
|
|
|
statement("spirv_cross_construct,");
|
|
|
|
statement("spirv_cross_destruct,");
|
|
|
|
statement("spirv_cross_invoke,");
|
|
|
|
end_scope_decl();
|
|
|
|
|
|
|
|
statement("");
|
2016-05-30 19:45:16 +00:00
|
|
|
statement("const struct spirv_cross_interface *",
|
|
|
|
interface_name.empty() ? string("spirv_cross_get_interface") : interface_name, "(void)");
|
2016-05-05 07:33:18 +00:00
|
|
|
begin_scope();
|
|
|
|
statement("return &vtable;");
|
|
|
|
end_scope();
|
2016-03-02 17:09:16 +00:00
|
|
|
}
|
|
|
|
|
2018-03-12 12:09:25 +00:00
|
|
|
void CompilerCPP::emit_function_prototype(SPIRFunction &func, const Bitset &)
|
2016-03-02 17:09:16 +00:00
|
|
|
{
|
2018-10-05 09:30:57 +00:00
|
|
|
if (func.self != ir.default_entry_point)
|
2018-02-23 13:13:46 +00:00
|
|
|
add_function_overload(func);
|
|
|
|
|
2016-05-23 10:25:09 +00:00
|
|
|
local_variable_names = resource_names;
|
2016-05-05 07:33:18 +00:00
|
|
|
string decl;
|
|
|
|
|
|
|
|
auto &type = get<SPIRType>(func.return_type);
|
|
|
|
decl += "inline ";
|
|
|
|
decl += type_to_glsl(type);
|
|
|
|
decl += " ";
|
|
|
|
|
2018-10-05 09:30:57 +00:00
|
|
|
if (func.self == ir.default_entry_point)
|
2016-05-05 07:33:18 +00:00
|
|
|
{
|
|
|
|
decl += "main";
|
|
|
|
processing_entry_point = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
decl += to_name(func.self);
|
|
|
|
|
|
|
|
decl += "(";
|
|
|
|
for (auto &arg : func.arguments)
|
|
|
|
{
|
2016-05-23 10:25:09 +00:00
|
|
|
add_local_variable_name(arg.id);
|
2016-05-05 07:33:18 +00:00
|
|
|
|
|
|
|
decl += argument_decl(arg);
|
|
|
|
if (&arg != &func.arguments.back())
|
|
|
|
decl += ", ";
|
|
|
|
|
|
|
|
// Hold a pointer to the parameter so we can invalidate the readonly field if needed.
|
|
|
|
auto *var = maybe_get<SPIRVariable>(arg.id);
|
|
|
|
if (var)
|
|
|
|
var->parameter = &arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
decl += ")";
|
|
|
|
statement(decl);
|
2016-03-02 17:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
string CompilerCPP::argument_decl(const SPIRFunction::Parameter &arg)
|
|
|
|
{
|
2016-05-05 07:33:18 +00:00
|
|
|
auto &type = expression_type(arg.id);
|
|
|
|
bool constref = !type.pointer || arg.write_count == 0;
|
2016-03-02 17:09:16 +00:00
|
|
|
|
2016-05-05 07:33:18 +00:00
|
|
|
auto &var = get<SPIRVariable>(arg.id);
|
2016-05-28 11:09:26 +00:00
|
|
|
|
|
|
|
string base = type_to_glsl(type);
|
2016-09-20 08:55:09 +00:00
|
|
|
string variable_name = to_name(var.self);
|
|
|
|
remap_variable_type_name(type, variable_name, base);
|
|
|
|
|
2016-10-03 15:17:11 +00:00
|
|
|
for (uint32_t i = 0; i < type.array.size(); i++)
|
|
|
|
base = join("std::array<", base, ", ", to_array_size(type, i), ">");
|
2016-05-28 11:09:26 +00:00
|
|
|
|
2016-09-20 08:55:09 +00:00
|
|
|
return join(constref ? "const " : "", base, " &", variable_name);
|
2016-05-28 11:09:26 +00:00
|
|
|
}
|
|
|
|
|
2017-06-17 08:54:59 +00:00
|
|
|
string CompilerCPP::variable_decl(const SPIRType &type, const string &name, uint32_t /* id */)
|
2016-05-28 11:09:26 +00:00
|
|
|
{
|
|
|
|
string base = type_to_glsl(type);
|
2016-09-20 08:55:09 +00:00
|
|
|
remap_variable_type_name(type, name, base);
|
2016-05-28 11:09:26 +00:00
|
|
|
bool runtime = false;
|
2016-10-03 15:17:11 +00:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < type.array.size(); i++)
|
2016-05-28 11:09:26 +00:00
|
|
|
{
|
2016-10-03 15:17:11 +00:00
|
|
|
auto &array = type.array[i];
|
|
|
|
if (!array && type.array_size_literal[i])
|
2016-05-28 11:09:26 +00:00
|
|
|
{
|
|
|
|
// Avoid using runtime arrays with std::array since this is undefined.
|
|
|
|
// Runtime arrays cannot be passed around as values, so this is fine.
|
|
|
|
runtime = true;
|
|
|
|
}
|
2016-10-03 15:17:11 +00:00
|
|
|
else
|
|
|
|
base = join("std::array<", base, ", ", to_array_size(type, i), ">");
|
2016-05-28 11:09:26 +00:00
|
|
|
}
|
|
|
|
base += ' ';
|
|
|
|
return base + name + (runtime ? "[1]" : "");
|
2016-03-02 17:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CompilerCPP::emit_header()
|
|
|
|
{
|
2016-07-28 09:16:02 +00:00
|
|
|
auto &execution = get_entry_point();
|
|
|
|
|
2016-05-05 07:33:18 +00:00
|
|
|
statement("// This C++ shader is autogenerated by spirv-cross.");
|
|
|
|
statement("#include \"spirv_cross/internal_interface.hpp\"");
|
|
|
|
statement("#include \"spirv_cross/external_interface.h\"");
|
2016-05-28 11:09:26 +00:00
|
|
|
// Needed to properly implement GLSL-style arrays.
|
|
|
|
statement("#include <array>");
|
2016-05-05 07:33:18 +00:00
|
|
|
statement("#include <stdint.h>");
|
|
|
|
statement("");
|
|
|
|
statement("using namespace spirv_cross;");
|
|
|
|
statement("using namespace glm;");
|
|
|
|
statement("");
|
|
|
|
|
|
|
|
statement("namespace Impl");
|
|
|
|
begin_scope();
|
|
|
|
|
|
|
|
switch (execution.model)
|
|
|
|
{
|
|
|
|
case ExecutionModelGeometry:
|
|
|
|
case ExecutionModelTessellationControl:
|
|
|
|
case ExecutionModelTessellationEvaluation:
|
|
|
|
case ExecutionModelGLCompute:
|
|
|
|
case ExecutionModelFragment:
|
|
|
|
case ExecutionModelVertex:
|
|
|
|
statement("struct Shader");
|
|
|
|
begin_scope();
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-12-12 21:33:22 +00:00
|
|
|
SPIRV_CROSS_THROW("Unsupported execution model.");
|
2016-05-05 07:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (execution.model)
|
|
|
|
{
|
|
|
|
case ExecutionModelGeometry:
|
|
|
|
impl_type = "GeometryShader<Impl::Shader, Impl::Shader::Resources>";
|
|
|
|
resource_type = "GeometryResources";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ExecutionModelVertex:
|
|
|
|
impl_type = "VertexShader<Impl::Shader, Impl::Shader::Resources>";
|
|
|
|
resource_type = "VertexResources";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ExecutionModelFragment:
|
|
|
|
impl_type = "FragmentShader<Impl::Shader, Impl::Shader::Resources>";
|
|
|
|
resource_type = "FragmentResources";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ExecutionModelGLCompute:
|
|
|
|
impl_type = join("ComputeShader<Impl::Shader, Impl::Shader::Resources, ", execution.workgroup_size.x, ", ",
|
|
|
|
execution.workgroup_size.y, ", ", execution.workgroup_size.z, ">");
|
|
|
|
resource_type = "ComputeResources";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ExecutionModelTessellationControl:
|
|
|
|
impl_type = "TessControlShader<Impl::Shader, Impl::Shader::Resources>";
|
|
|
|
resource_type = "TessControlResources";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ExecutionModelTessellationEvaluation:
|
|
|
|
impl_type = "TessEvaluationShader<Impl::Shader, Impl::Shader::Resources>";
|
|
|
|
resource_type = "TessEvaluationResources";
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-12-12 21:33:22 +00:00
|
|
|
SPIRV_CROSS_THROW("Unsupported execution model.");
|
2016-05-05 07:33:18 +00:00
|
|
|
}
|
2016-03-02 17:09:16 +00:00
|
|
|
}
|