Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jeslaspravin/1b306f6b4281c6c6eb17379a91ba7a86 to your computer and use it in GitHub Desktop.
Save jeslaspravin/1b306f6b4281c6c6eb17379a91ba7a86 to your computer and use it in GitHub Desktop.
Example of chaining diverge converge with dependencies using CoPaT
ICompilerInstance::InternalRetAsync<std::vector<std::vector<DxcCompiler::DxcCompiledResults>>> DxcCompiler::dxcCompileShards(
ArrayView<std::vector<ShardPermutation>> shardPermutations, ArrayView<std::vector<CComPtr<IDxcBlobEncoding>>> shaders
)
{
auto compileShardPermutation = [&](uint32 idx, uint32 shaderIdx)
{
const ShaderSrcEntry &entry = shaderEntries[shaderIdx];
CComPtr<IDxcBlobEncoding> code = shaders[shaderIdx][idx];
const ShardPermutation &permutation = shardPermutations[shaderIdx][idx];
std::vector<std::wstring> cDefs(permutation.cDefines.size());
std::vector<const WChar *> args(permutation.cDefines.size() * 2);
for (SizeT i = 0; i < permutation.cDefines.size(); ++i)
{
cDefs[i] = TCHAR_TO_WCHAR(String(permutation.cDefines[i]).getChar());
args[i * 2 + 0] = L"-D";
args[i * 2 + 1] = cDefs[i].c_str();
}
DxcThreadLocal &dxcTl = *static_cast<DxcThreadLocal *>(js.getTlUserData());
DxcCompiledResults result = {};
for (gal::EShaderStage::Type stage = gal::EShaderStage::RasterStart; stage <= gal::EShaderStage::RasterEnd; ++stage)
{
if (!permutation.entryPoints[stage].empty())
{
result.stages[stage] = dxcCompileShaderStage(code, entry, permutation.entryPoints, stage, args, dxcTl);
}
}
return result;
};
using PerShardAwaitable = InternalRetAsync<std::vector<DxcCompiledResults>>;
auto shaderAwaitables = copat::diverge(
&js,
copat::DispatchWithReturn<PerShardAwaitable>::FuncType::createLambda(
[&](uint32 idx) -> PerShardAwaitable
{
std::vector<DxcCompiledResults> results;
const ShaderSrcEntry &entry = shaderEntries[idx];
const std::vector<ShardPermutation> &permutations = shardPermutations[idx];
switch (entry.type)
{
case EShaderSrcType::MaterialShard:
{
results = co_await copat::awaitConverge(copat::diverge(
&js, copat::DispatchWithReturn<DxcCompiledResults>::FuncType::createLambda(compileShardPermutation, idx),
static_cast<uint32>(permutations.size())
));
break;
}
default:
break;
}
co_return results;
}
),
static_cast<uint32>(shaderEntries.size())
);
/* vector need to be moved as JobSystemTasks cannot be copied */
std::vector shardAwaitables = std::move(co_await copat::awaitConverge(std::move(shaderAwaitables)));
std::vector<std::vector<DxcCompiledResults>> retVals;
retVals.resize(shaderEntries.size());
for (SizeT i = 0; i < shaderEntries.size(); ++i)
{
retVals[i] = co_await shardAwaitables[i];
}
co_return retVals;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment