Skip to content

Commit

Permalink
Merge pull request #70 from luboslenco/master
Browse files Browse the repository at this point in the history
Fix returning output size for SPIRV
  • Loading branch information
RobDangerous authored Aug 4, 2020
2 parents a3c3223 + 70506b0 commit ec46a76
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
14 changes: 12 additions & 2 deletions Sources/SpirVTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ namespace {
}
}

void SpirVTranslator::writeInstructions(const char* filename, char* output, std::vector<Instruction>& instructions) {
int SpirVTranslator::writeInstructions(const char* filename, char* output, std::vector<Instruction>& instructions) {
std::ofstream fileout;
std::ostrstream arrayout(output, 1024 * 1024);
std::ostream* out;
Expand All @@ -87,23 +87,33 @@ void SpirVTranslator::writeInstructions(const char* filename, char* output, std:
out = &fileout;
}

int length = 0;
writeInstruction(out, magicNumber);
length += 4;
writeInstruction(out, version);
length += 4;
writeInstruction(out, generator);
length += 4;
writeInstruction(out, bound);
length += 4;
writeInstruction(out, schema);
length += 4;

for (unsigned i = 0; i < instructions.size(); ++i) {
Instruction& inst = instructions[i];
writeInstruction(out, ((inst.length + 1) << 16) | (unsigned)inst.opcode);
length += 4;
for (unsigned i2 = 0; i2 < inst.length; ++i2) {
writeInstruction(out, inst.operands[i2]);
length += 4;
}
}

if (!output) {
fileout.close();
}

return length;
}

namespace {
Expand Down Expand Up @@ -752,5 +762,5 @@ void SpirVTranslator::outputCode(const Target& target, const char* sourcefilenam
}

bound = currentId + 1;
writeInstructions(filename, output, newinstructions);
outputLength = writeInstructions(filename, output, newinstructions);
}
3 changes: 2 additions & 1 deletion Sources/SpirVTranslator.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ namespace krafix {
public:
SpirVTranslator(std::vector<unsigned>& spirv, ShaderStage stage) : Translator(spirv, stage) {}
void outputCode(const Target& target, const char* sourcefilename, const char* filename, char* output, std::map<std::string, int>& attributes) override;
int outputLength;
private:
void writeInstructions(const char* filename, char* output, std::vector<Instruction>& instructions);
int writeInstructions(const char* filename, char* output, std::vector<Instruction>& instructions);
};
}
6 changes: 6 additions & 0 deletions Sources/krafix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,12 @@ void CompileAndLinkShaderUnits(std::vector<ShaderCompUnit> compUnits, krafix::Ta
if (returnCode != 0) CompileFailed = true;
delete[] tempoutput;
}
else if (target.lang == krafix::SpirV) {
translator->outputCode(target, sourcefilename, filename, output, attributes);
if (output != nullptr) {
*length = dynamic_cast<krafix::SpirVTranslator*>(translator)->outputLength;
}
}
else {
translator->outputCode(target, sourcefilename, filename, output, attributes);
if (output != nullptr) {
Expand Down

0 comments on commit ec46a76

Please sign in to comment.