Sort variables in HLSL input/output structs

To make binding numbers the same in all
shader stages.
This commit is contained in:
Robert Konrad 2016-08-15 20:33:10 +02:00
parent da5f99bb6d
commit a896868215
2 changed files with 32 additions and 4 deletions

View File

@ -15,11 +15,26 @@
*/
#include "spirv_hlsl.hpp"
#include <algorithm>
using namespace spv;
using namespace spirv_cross;
using namespace std;
namespace
{
struct VariableComparator {
VariableComparator(const std::vector<Meta>& meta) : meta(meta) { }
bool operator () (SPIRVariable* var1, SPIRVariable* var2)
{
return meta[var1->self].decoration.alias.compare(meta[var2->self].decoration.alias) < 0;
}
const std::vector<Meta>& meta;
};
}
string CompilerHLSL::type_to_glsl(const SPIRType &type)
{
// Ignore the pointer type since GLSL doesn't have pointers.
@ -247,6 +262,7 @@ void CompilerHLSL::emit_resources()
}
begin_scope();
uint32_t binding_number = 0;
std::vector<SPIRVariable*> variables;
for (auto &id : ids)
{
if (id.get_type() == TypeVariable)
@ -257,11 +273,16 @@ void CompilerHLSL::emit_resources()
if (var.storage != StorageClassFunction && !var.remapped_variable && type.pointer &&
var.storage == StorageClassInput && interface_variable_exists_in_entry_point(var.self))
{
emit_interface_block_in_struct(var, binding_number);
emitted = true;
variables.push_back(&var);
}
}
}
sort(variables.begin(), variables.end(), VariableComparator(meta));
for (auto var : variables)
{
emit_interface_block_in_struct(*var, binding_number);
emitted = true;
}
end_scope_decl();
statement("");
@ -275,6 +296,7 @@ void CompilerHLSL::emit_resources()
}
begin_scope();
binding_number = 0;
variables.clear();
for (auto &id : ids)
{
if (id.get_type() == TypeVariable)
@ -285,11 +307,16 @@ void CompilerHLSL::emit_resources()
if (var.storage != StorageClassFunction && !var.remapped_variable && type.pointer &&
var.storage == StorageClassOutput && interface_variable_exists_in_entry_point(var.self))
{
emit_interface_block_in_struct(var, binding_number);
emitted = true;
variables.push_back(&var);
}
}
}
sort(variables.begin(), variables.end(), VariableComparator(meta));
for (auto var : variables)
{
emit_interface_block_in_struct(*var, binding_number);
emitted = true;
}
end_scope_decl();
statement("");

View File

@ -44,6 +44,7 @@ private:
void emit_instruction(const Instruction &instruction) override;
void emit_binary_func_op_transpose_first(uint32_t result_type, uint32_t result_id, uint32_t op0, uint32_t op1,
const char *op);
bool myfunction(Variant id1, Variant id2);
};
}