Add helpers to create an empty SPIR-V module in an editor

This commit is contained in:
baldurk
2020-04-10 12:56:10 +01:00
parent ead9463711
commit fc452b7586
3 changed files with 31 additions and 4 deletions
@@ -117,15 +117,37 @@ void Editor::Prepare()
}
}
void Editor::CreateEmpty(uint32_t major, uint32_t minor)
{
if(!m_ExternalSPIRV.empty())
{
RDCERR("Creating empty SPIR-V module with some SPIR-V words already in place!");
m_ExternalSPIRV.clear();
}
// create an empty SPIR-V header with an upper ID bound of 1
m_ExternalSPIRV = {
MagicNumber, (major << 16) | (minor << 8),
0, // TODO maybe register a generator ID?
1, // bound
0, // instruction schema
};
// we need at least one opcode to parse properly, and we'll always need shader.
Operation shader = Operation(OpCapability(Capability::Shader));
m_ExternalSPIRV.append(&shader[0], shader.size());
Prepare();
}
Editor::~Editor()
{
for(size_t i = FirstRealWord; i < m_SPIRV.size();)
{
while(m_SPIRV[i] == OpNopWord)
{
// don't need to update anything as we're destructing!
while(i < m_SPIRV.size() && m_SPIRV[i] == OpNopWord)
m_SPIRV.erase(i);
addWords(i, -1);
}
uint32_t len = m_SPIRV[i] >> WordCountShift;
@@ -122,6 +122,7 @@ public:
~Editor();
void Prepare();
void CreateEmpty(uint32_t major, uint32_t minor);
Id MakeId();
@@ -422,6 +422,10 @@ void Processor::Parse(const rdcarray<uint32_t> &spirvWords)
// ensure we got everything right. First section should start at the beginning
RDCASSERTEQUAL(m_Sections[Section::First].startOffset, FirstRealWord);
// if the last section is empty, set its start ofset. This will cascade below
if(m_Sections[Section::Count - 1].startOffset == 0)
m_Sections[Section::Count - 1].startOffset = m_Sections[Section::Count - 1].endOffset;
// we now set the endOffset of each section to the start of the next. Any empty sections
// temporarily have startOffset set to endOffset, we'll pad them with a nop below.
for(int s = Section::Count - 1; s > 0; s--)