prototype for spirv linking

This commit is contained in:
2026-04-30 22:03:31 +03:00
parent 9569555347
commit 386c2fc045
10 changed files with 120 additions and 34 deletions

View File

@@ -36,6 +36,12 @@ void CVkShaderLinker::Build()
uint32_t last = 0;
uint32_t current = 0;
CUtlVector<uint32_t> callers = {};
mspv_array(mspv_function) functions_orig;
V_memcpy(&functions_orig, &mod->functions, sizeof(functions_orig));
mod->functions = {};
for ( int i = 0; i < mod->types.count; i++ )
{
SET_LAST(mod->types.data[i].id);
@@ -50,13 +56,20 @@ void CVkShaderLinker::Build()
SET_LAST(mod->variables.data[i].result);
SET_LAST(mod->variables.data[i].resulttype);
}
for ( int i = 0; i < mod->functions.count; i++ )
for ( int i = 0; i < functions_orig.count; i++ )
{
SET_LAST(mod->functions.data[i].result);
mspv_function &f = mod->functions.data[i];
SET_LAST(functions_orig.data[i].result);
mspv_function &f = functions_orig.data[i];
bool bIsCaller = false;
for ( int u = 0; u < f.instructions.len; )
{
SpvOp op = (SpvOp)(f.instructions.data[u]&0xFFFF);
if (op == SpvOpExecuteCallableKHR)
{
bIsCaller = true;
callers.AppendTail(i);
V_printf("found caller: %i %u\n", i, f.result);
}
uint32_t uOpLen = f.instructions.data[u]>>16;
uint32_t uOpCount = SpvGetOperandCount(op);
if (uOpCount == 0)
@@ -90,13 +103,16 @@ void CVkShaderLinker::Build()
}
u+=uOpLen;
V_free(peOps);
V_free(peFlags);
}
if (!bIsCaller)
mspv_array_push(mod->functions, f);
}
current = last;
/* now we can combine shaders*/
CUtlVector<uint32_t> functions = {};
CUtlVector<mspv_function> f = {};
for (auto &shader: m_shaders)
{
mspv_module *s = mspv_read_module(shader.m_size, shader.m_data);
@@ -117,6 +133,14 @@ void CVkShaderLinker::Build()
for ( int i = 0; i < s->entry_points.count; i++ )
{
functions.AppendTail(s->entry_points.data[i].id+current);
for ( uint32_t u = 0; u < s->functions.count; u++ )
{
if (s->functions.data[u].result != s->entry_points.data[i].id)
continue;
f.AppendTail(s->functions.data[u]);
break;
}
SET_LAST(s->entry_points.data[i].id+current);
}
@@ -222,6 +246,52 @@ void CVkShaderLinker::Build()
}
current = last;
}
/* generate trace call */
V_printf("functions %i\n", functions.GetSize());
uint32_t n = 0;
for ( auto &c: callers )
{
CUtlVector<uint32_t> ops = {};
mspv_function fn = functions_orig.data[c];
mspv_data_view dv = fn.instructions;
uint32_t u;
CUtlVector<uint32_t> inputs = {};
u = 0;
while(u < fn.instructions.len)
{
SpvOp op = (SpvOp)(dv.data[u]&0xFFFF);
uint32_t uOpLen = dv.data[u]>>16;
if (op != SpvOpFunctionParameter)
{
break;
}
ops.AppendTail(&dv.data[u], uOpLen);
inputs.AppendTail(dv.data[u+2]);
u += uOpLen;
}
ops.AppendTail(mspv_make_op(SpvOpLabel, 1));
ops.AppendTail(current++);
ops.AppendTail(mspv_make_op(SpvOpFunctionCall, 5));
ops.AppendTail(f[n].resulttype);
ops.AppendTail(current++);
ops.AppendTail(f[n].result);
ops.AppendTail(inputs[0]);
ops.AppendTail(inputs[1]);
ops.AppendTail(mspv_make_op(SpvOpReturn, 0));
fn.instructions.data = (uint32_t*)V_malloc(ops.GetSize()*sizeof(uint32_t));
V_memcpy(fn.instructions.data, ops.GetData(), ops.GetSize()*sizeof(uint32_t));
fn.instructions.len = ops.GetSize();
mspv_array_push(mod->functions, fn);
n++;
}
mspv_spv spv = mspv_write_module(mod);
mspv_close_module(mod);