Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure GLSL variables are initialized #918

Merged
merged 1 commit into from
Aug 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions gapis/shadertools/cc/libmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ code_with_debug_info_t* convertGlsl(const char* input, size_t length, const opti
}
my_manager.renameViewIndex();
my_manager.removeLayoutLocations();
my_manager.initLocals();

std::vector<unsigned int> spirv_new = my_manager.getSpvBinary();

Expand Down
49 changes: 49 additions & 0 deletions gapis/shadertools/cc/spv_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,55 @@ void SpvManager::renameViewIndex() {
}
}

// Explicitly initialize locals. This is a work around for SPIR-V cross problem
// where it may generate code which reads locals before initializing them.
// For example, "v.x = 42.0;" becomes "v = vec4(42.0, v.y, v.z, v.w);"
void SpvManager::initLocals() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a good thing to upstream.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the proper fix would be in SPIRV-Cross. I have investigated that approach for a bit, but gave up. I just ended fixing a bug.

for (auto& function : *module) {
std::map<Instruction*, std::unique_ptr<Instruction>> replacement;
std::unordered_set<Instruction*> seen;
function.ForEachInst([this,&seen,&replacement](Instruction* inst) {
if (inst->opcode() == SpvOpLoad || inst->opcode() == SpvOpStore) {
Instruction* var = def_use_mgr->GetDef(inst->GetSingleWordInOperand(0));
if (seen.insert(var).second) {
if (inst->opcode() == SpvOpLoad &&
var->opcode() == SpvOpVariable &&
(var->GetSingleWordInOperand(0) == spv::StorageClassFunction ||
var->GetSingleWordInOperand(0) == spv::StorageClassPrivate) &&
var->NumInOperands() == 1 /* No initializer */) {
// We have found Load before Store to a local variable (function scope)
auto* vecType = getPointeeIfPointer(var->type_id())->AsVector();
if (vecType && vecType->element_count() == 4) {
// TODO: Handle more then just vec4 types, or fix the issue in SPIRV-Cross
uint32_t elem_type_id = TypeToId(vecType->element_type());
uint32_t elem_id = addConstant(elem_type_id, {0});
uint32_t init_value_id = getUnique();
module->AddGlobalValue(makeInstruction(SpvOpConstantComposite, TypeToId(vecType), init_value_id,
{{elem_id, elem_id, elem_id, elem_id}}));
replacement[var] = makeInstruction(var->opcode(), var->type_id(), var->result_id(),
{{var->GetSingleWordInOperand(0)}, {init_value_id}});
}
}
}
}
});
for (auto& basic_block : function) {
for (auto it = basic_block.begin(); it != basic_block.end(); it++) {
std::unique_ptr<Instruction> newInst(std::move(replacement[&*it]));
if (newInst != nullptr) {
it = it.Erase().InsertBefore(std::move(newInst));
}
}
}
for (auto it = module->types_values_begin(); it != module->types_values_end(); it++) {
std::unique_ptr<Instruction> newInst(std::move(replacement[&*it]));
if (newInst != nullptr) {
it = it.Erase().InsertBefore(std::move(newInst));
}
}
}
}

// Remove all layout(location = ...) qualifiers.
// For most use cases we should be binding/remapping locations regardless whether they
// are assigned explicit location or compiler generated one.
Expand Down
1 change: 1 addition & 0 deletions gapis/shadertools/cc/spv_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class SpvManager {
void mapDeclarationNames(std::string = "x");
void renameViewIndex();
void removeLayoutLocations();
void initLocals();
void makeSpvDebuggable();
std::vector<unsigned int> getSpvBinary();
debug_instructions_t* getDebugInstructions();
Expand Down