Skip to content

Commit

Permalink
Take execution model into account for entry point methods.
Browse files Browse the repository at this point in the history
SPIR-V allows names to alias if they implement different stages.
Deprecate the old interface and replace it with a new one which takes
execution modes into account.
  • Loading branch information
HansKristian-Work committed Mar 1, 2018
1 parent cae1722 commit eecbeaa
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 13 deletions.
120 changes: 111 additions & 9 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,9 @@ static void print_resources(const Compiler &compiler, const ShaderResources &res
uint64_t modes = compiler.get_execution_mode_mask();

fprintf(stderr, "Entry points:\n");
auto entry_points = compiler.get_entry_points();
auto entry_points = compiler.get_entry_points_and_stages();
for (auto &e : entry_points)
fprintf(stderr, " %s (%s)\n", e.c_str(), execution_model_to_str(compiler.get_entry_point(e).model));
fprintf(stderr, " %s (%s)\n", e.name.c_str(), execution_model_to_str(e.execution_model));
fprintf(stderr, "\n");

fprintf(stderr, "Execution modes:\n");
Expand Down Expand Up @@ -467,8 +467,15 @@ struct CLIArguments
vector<InterfaceVariableRename> interface_variable_renames;
vector<HLSLVertexAttributeRemap> hlsl_attr_remap;
string entry;
string entry_stage;

vector<pair<string, string>> entry_point_rename;
struct Rename
{
string old_name;
string new_name;
ExecutionModel execution_model;
};
vector<Rename> entry_point_rename;

uint32_t iterations = 1;
bool cpp = false;
Expand All @@ -491,12 +498,12 @@ static void print_help()
"[--hlsl] [--shader-model] [--hlsl-enable-compat] "
"[--separate-shader-objects]"
"[--pls-in format input-name] [--pls-out format output-name] [--remap source_name target_name "
"components] [--extension ext] [--entry name] [--remove-unused-variables] "
"components] [--extension ext] [--entry name] [--stage <stage (vert, frag, geom, tesc, tese, comp)>] [--remove-unused-variables] "
"[--flatten-multidimensional-arrays] [--no-420pack-extension] "
"[--remap-variable-type <variable_name> <new_variable_type>] "
"[--rename-interface-variable <in|out> <location> <new_variable_name>] "
"[--set-hlsl-vertex-input-semantic <location> <semantic>] "
"[--rename-entry-point <old> <new>] "
"[--rename-entry-point <old> <new> <stage>] "
"\n");
}

Expand Down Expand Up @@ -611,6 +618,24 @@ void rename_interface_variable(Compiler &compiler, const vector<Resource> &resou
}
}

static ExecutionModel stage_to_execution_model(const std::string &stage)
{
if (stage == "vert")
return ExecutionModelVertex;
else if (stage == "frag")
return ExecutionModelFragment;
else if (stage == "comp")
return ExecutionModelGLCompute;
else if (stage == "tesc")
return ExecutionModelTessellationControl;
else if (stage == "tese")
return ExecutionModelTessellationEvaluation;
else if (stage == "geom")
return ExecutionModelGeometry;
else
SPIRV_CROSS_THROW("Invalid stage.");
}

static int main_inner(int argc, char *argv[])
{
CLIArguments args;
Expand Down Expand Up @@ -652,9 +677,11 @@ static int main_inner(int argc, char *argv[])
cbs.add("--rename-entry-point", [&args](CLIParser &parser) {
auto old_name = parser.next_string();
auto new_name = parser.next_string();
args.entry_point_rename.push_back({ old_name, new_name });
auto model = stage_to_execution_model(parser.next_string());
args.entry_point_rename.push_back({ old_name, new_name, move(model) });
});
cbs.add("--entry", [&args](CLIParser &parser) { args.entry = parser.next_string(); });
cbs.add("--stage", [&args](CLIParser &parser) { args.entry_stage = parser.next_string(); });
cbs.add("--separate-shader-objects", [&args](CLIParser &) { args.sso = true; });
cbs.add("--set-hlsl-vertex-input-semantic", [&args](CLIParser &parser) {
HLSLVertexAttributeRemap remap;
Expand Down Expand Up @@ -772,10 +799,85 @@ static int main_inner(int argc, char *argv[])
}

for (auto &rename : args.entry_point_rename)
compiler->rename_entry_point(rename.first, rename.second);
compiler->rename_entry_point(rename.old_name, rename.new_name, rename.execution_model);

auto entry_points = compiler->get_entry_points_and_stages();
auto entry_point = args.entry;
ExecutionModel model = ExecutionModelMax;

if (!args.entry_stage.empty())
{
model = stage_to_execution_model(args.entry_stage);
if (entry_point.empty())
{
// Just use the first entry point with this stage.
for (auto &e : entry_points)
{
if (e.execution_model == model)
{
entry_point = e.name;
break;
}
}

if (entry_point.empty())
{
fprintf(stderr, "Could not find an entry point with stage: %s\n",
args.entry_stage.c_str());
return EXIT_FAILURE;
}
}
else
{
// Make sure both stage and name exists.
bool exists = false;
for (auto &e : entry_points)
{
if (e.execution_model == model && e.name == entry_point)
{
exists = true;
break;
}
}

if (!exists)
{
fprintf(stderr, "Could not find an entry point %s with stage: %s\n",
entry_point.c_str(), args.entry_stage.c_str());
return EXIT_FAILURE;
}
}
}
else if (!entry_point.empty())
{
// Make sure there is just one entry point with this name, or the stage
// is ambiguous.
uint32_t stage_count = 0;
for (auto &e : entry_points)
{
if (e.name == entry_point)
{
stage_count++;
model = e.execution_model;
}
}

if (stage_count == 0)
{
fprintf(stderr, "There is no entry point with name: %s\n",
entry_point.c_str());
return EXIT_FAILURE;
}
else if (stage_count > 1)
{
fprintf(stderr, "There is more than one entry point with name: %s. Use --stage.\n",
entry_point.c_str());
return EXIT_FAILURE;
}
}

if (!args.entry.empty())
compiler->set_entry_point(args.entry);
if (!entry_point.empty())
compiler->set_entry_point(entry_point, model);

if (!args.set_version && !compiler->get_options().version)
{
Expand Down
4 changes: 4 additions & 0 deletions shaders-other/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
These shaders are not actually run yet as part of any test suite,
but are kept here because they have been used to manually test various aspects of SPIRV-Cross in the past.

These would ideally be part of the test suite in some way.
60 changes: 60 additions & 0 deletions shaders-other/aliased-entry-point-names.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
; SPIR-V
; Version: 1.0
; Generator: Khronos Glslang Reference Front End; 3
; Bound: 20
; Schema: 0
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Vertex %main "main" %_
OpEntryPoint Vertex %main2 "main2" %_
OpEntryPoint Fragment %main3 "main" %FragColor
OpEntryPoint Fragment %main4 "main2" %FragColor
OpSource GLSL 450
OpMemberDecorate %gl_PerVertex 0 BuiltIn Position
OpMemberDecorate %gl_PerVertex 1 BuiltIn PointSize
OpMemberDecorate %gl_PerVertex 2 BuiltIn ClipDistance
OpMemberDecorate %gl_PerVertex 3 BuiltIn CullDistance
OpDecorate %FragColor Location 0
OpDecorate %gl_PerVertex Block
%void = OpTypeVoid
%3 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%v4floatptr = OpTypePointer Output %v4float
%uint = OpTypeInt 32 0
%uint_1 = OpConstant %uint 1
%_arr_float_uint_1 = OpTypeArray %float %uint_1
%gl_PerVertex = OpTypeStruct %v4float %float %_arr_float_uint_1 %_arr_float_uint_1
%_ptr_Output_gl_PerVertex = OpTypePointer Output %gl_PerVertex
%_ = OpVariable %_ptr_Output_gl_PerVertex Output
%FragColor = OpVariable %v4floatptr Output
%int = OpTypeInt 32 1
%int_0 = OpConstant %int 0
%float_1 = OpConstant %float 1
%17 = OpConstantComposite %v4float %float_1 %float_1 %float_1 %float_1
%float_2 = OpConstant %float 2
%18 = OpConstantComposite %v4float %float_2 %float_2 %float_2 %float_2
%_ptr_Output_v4float = OpTypePointer Output %v4float
%main = OpFunction %void None %3
%5 = OpLabel
%19 = OpAccessChain %_ptr_Output_v4float %_ %int_0
OpStore %19 %17
OpReturn
OpFunctionEnd
%main2 = OpFunction %void None %3
%6 = OpLabel
%20 = OpAccessChain %_ptr_Output_v4float %_ %int_0
OpStore %20 %18
OpReturn
OpFunctionEnd
%main3 = OpFunction %void None %3
%7 = OpLabel
OpStore %FragColor %17
OpReturn
OpFunctionEnd
%main4 = OpFunction %void None %3
%8 = OpLabel
OpStore %FragColor %18
OpReturn
OpFunctionEnd
70 changes: 66 additions & 4 deletions spirv_cross.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2589,20 +2589,51 @@ vector<string> Compiler::get_entry_points() const
return entries;
}

vector<EntryPoint> Compiler::get_entry_points_and_stages() const
{
vector<EntryPoint> entries;
for (auto &entry : entry_points)
entries.push_back({ entry.second.orig_name, entry.second.model });
return entries;
}

void Compiler::rename_entry_point(const std::string &old_name, const std::string &new_name)
{
auto &entry = get_entry_point(old_name);
auto &entry = get_first_entry_point(old_name);
entry.orig_name = new_name;
entry.name = new_name;
}

void Compiler::rename_entry_point(const std::string &old_name, const std::string &new_name, spv::ExecutionModel model)
{
auto &entry = get_entry_point(old_name, model);
entry.orig_name = new_name;
entry.name = new_name;
}

void Compiler::set_entry_point(const std::string &name)
{
auto &entry = get_entry_point(name);
auto &entry = get_first_entry_point(name);
entry_point = entry.self;
}

void Compiler::set_entry_point(const std::string &name, spv::ExecutionModel model)
{
auto &entry = get_entry_point(name, model);
entry_point = entry.self;
}

SPIREntryPoint &Compiler::get_entry_point(const std::string &name)
{
return get_first_entry_point(name);
}

const SPIREntryPoint &Compiler::get_entry_point(const std::string &name) const
{
return get_first_entry_point(name);
}

SPIREntryPoint &Compiler::get_first_entry_point(const std::string &name)
{
auto itr =
find_if(begin(entry_points), end(entry_points), [&](const std::pair<uint32_t, SPIREntryPoint> &entry) -> bool {
Expand All @@ -2615,7 +2646,7 @@ SPIREntryPoint &Compiler::get_entry_point(const std::string &name)
return itr->second;
}

const SPIREntryPoint &Compiler::get_entry_point(const std::string &name) const
const SPIREntryPoint &Compiler::get_first_entry_point(const std::string &name) const
{
auto itr =
find_if(begin(entry_points), end(entry_points), [&](const std::pair<uint32_t, SPIREntryPoint> &entry) -> bool {
Expand All @@ -2628,9 +2659,40 @@ const SPIREntryPoint &Compiler::get_entry_point(const std::string &name) const
return itr->second;
}

SPIREntryPoint &Compiler::get_entry_point(const std::string &name, ExecutionModel model)
{
auto itr =
find_if(begin(entry_points), end(entry_points), [&](const std::pair<uint32_t, SPIREntryPoint> &entry) -> bool {
return entry.second.orig_name == name && entry.second.model == model;
});

if (itr == end(entry_points))
SPIRV_CROSS_THROW("Entry point does not exist.");

return itr->second;
}

const SPIREntryPoint &Compiler::get_entry_point(const std::string &name, ExecutionModel model) const
{
auto itr =
find_if(begin(entry_points), end(entry_points), [&](const std::pair<uint32_t, SPIREntryPoint> &entry) -> bool {
return entry.second.orig_name == name && entry.second.model == model;
});

if (itr == end(entry_points))
SPIRV_CROSS_THROW("Entry point does not exist.");

return itr->second;
}

const string &Compiler::get_cleansed_entry_point_name(const std::string &name) const
{
return get_entry_point(name).name;
return get_first_entry_point(name).name;
}

const string &Compiler::get_cleansed_entry_point_name(const std::string &name, ExecutionModel model) const
{
return get_entry_point(name, model).name;
}

const SPIREntryPoint &Compiler::get_entry_point() const
Expand Down
Loading

0 comments on commit eecbeaa

Please sign in to comment.