Skip to content

Commit

Permalink
D3D: Minor workgraph stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
godlikepanos committed Jun 24, 2024
1 parent 7087b31 commit 0e27348
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 39 deletions.
4 changes: 3 additions & 1 deletion AnKi/Gr/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ enum class ShaderType : U16
kMiss,
kIntersection,
kCallable,
kWorkgraph,

kCount,
kFirst = 0,
Expand Down Expand Up @@ -583,14 +584,15 @@ enum class ShaderTypeBit : U16
kMiss = 1 << 11,
kIntersection = 1 << 12,
kCallable = 1 << 13,
kWorkgraph = 1 << 14,

kNone = 0,
kAllGraphics = kVertex | kTessellationControl | kTessellationEvaluation | kGeometry | kTask | kMesh | kFragment,
kAllLegacyGeometry = kVertex | kTessellationControl | kTessellationEvaluation | kGeometry,
kAllModernGeometry = kTask | kMesh,
kAllRayTracing = kRayGen | kAnyHit | kClosestHit | kMiss | kIntersection | kCallable,
kAllHit = kAnyHit | kClosestHit,
kAll = kAllGraphics | kCompute | kAllRayTracing,
kAll = kAllGraphics | kCompute | kAllRayTracing | kWorkgraph,
};
ANKI_ENUM_ALLOW_NUMERIC_OPERATIONS(ShaderTypeBit)

Expand Down
60 changes: 38 additions & 22 deletions AnKi/ShaderCompiler/Dxc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <AnKi/Util/Filesystem.h>
#include <AnKi/Util/File.h>
#include <AnKi/Util/HighRezTimer.h>
#include <AnKi/Util/StringList.h>

namespace anki {

Expand All @@ -18,36 +19,37 @@ static CString profile(ShaderType shaderType)
switch(shaderType)
{
case ShaderType::kVertex:
return "vs_6_7";
return "vs_6_8";
break;
case ShaderType::kFragment:
return "ps_6_7";
return "ps_6_8";
break;
case ShaderType::kTessellationEvaluation:
return "ds_6_7";
return "ds_6_8";
break;
case ShaderType::kTessellationControl:
return "ds_6_7";
return "ds_6_8";
break;
case ShaderType::kGeometry:
return "gs_6_7";
return "gs_6_8";
break;
case ShaderType::kTask:
return "as_6_7";
return "as_6_8";
break;
case ShaderType::kMesh:
return "ms_6_7";
return "ms_6_8";
break;
case ShaderType::kCompute:
return "cs_6_7";
return "cs_6_8";
break;
case ShaderType::kRayGen:
case ShaderType::kAnyHit:
case ShaderType::kClosestHit:
case ShaderType::kMiss:
case ShaderType::kIntersection:
case ShaderType::kCallable:
return "lib_6_7";
case ShaderType::kWorkgraph:
return "lib_6_8";
break;
default:
ANKI_ASSERT(0);
Expand All @@ -56,8 +58,8 @@ static CString profile(ShaderType shaderType)
return "";
}

static Error compileHlsl(CString src, ShaderType shaderType, Bool compileWith16bitTypes, Bool spirv, ShaderCompilerDynamicArray<U8>& bin,
ShaderCompilerString& errorMessage)
static Error compileHlsl(CString src, ShaderType shaderType, Bool compileWith16bitTypes, Bool debugInfo, Bool spirv,
ShaderCompilerDynamicArray<U8>& bin, ShaderCompilerString& errorMessage)
{
Array<U64, 3> toHash = {g_nextFileId.fetchAdd(1), getCurrentProcessId(), getRandom() & kMaxU32};
const U64 rand = computeHash(&toHash[0], sizeof(toHash));
Expand All @@ -79,7 +81,7 @@ static Error compileHlsl(CString src, ShaderType shaderType, Bool compileWith16b
ShaderCompilerString binFilename;
binFilename.sprintf("%s/%" PRIu64 ".spvdxil", tmpDir.cstr(), rand);

ShaderCompilerDynamicArray<ShaderCompilerString> dxcArgs;
ShaderCompilerStringList dxcArgs;
dxcArgs.emplaceBack("-Fo");
dxcArgs.emplaceBack(binFilename);
dxcArgs.emplaceBack("-Wall");
Expand All @@ -97,10 +99,12 @@ static Error compileHlsl(CString src, ShaderType shaderType, Bool compileWith16b
dxcArgs.emplaceBack("main");
dxcArgs.emplaceBack("-T");
dxcArgs.emplaceBack(profile(shaderType));

if(ANKI_COMPILER_MSVC)
{
dxcArgs.emplaceBack("-fdiagnostics-format=msvc"); // Make errors clickable in visual studio
}

if(spirv)
{
dxcArgs.emplaceBack("-spirv");
Expand Down Expand Up @@ -129,10 +133,15 @@ static Error compileHlsl(CString src, ShaderType shaderType, Bool compileWith16b
}
else
{
dxcArgs.emplaceBack("-Wno-ignored-attributes"); // TODO rm that eventually
dxcArgs.emplaceBack("-Wno-inline-asm"); // TODO rm that eventually
dxcArgs.emplaceBack("-Wno-ignored-attributes"); // TODO remove that at some point
dxcArgs.emplaceBack("-Wno-inline-asm"); // Workaround a DXC bug
}

if(debugInfo)
{
dxcArgs.emplaceBack("-Zi");
}
// dxcArgs.emplaceBack("-Zi"); // Debug info

dxcArgs.emplaceBack(hlslFilename);

if(compileWith16bitTypes)
Expand All @@ -141,10 +150,11 @@ static Error compileHlsl(CString src, ShaderType shaderType, Bool compileWith16b
}

ShaderCompilerDynamicArray<CString> dxcArgs2;
dxcArgs2.resize(dxcArgs.getSize());
for(U32 i = 0; i < dxcArgs.getSize(); ++i)
dxcArgs2.resize(U32(dxcArgs.getSize()));
U32 count = 0;
for(auto& it : dxcArgs)
{
dxcArgs2[i] = dxcArgs[i];
dxcArgs2[count++] = it.cstr();
}

while(true)
Expand All @@ -171,6 +181,12 @@ static Error compileHlsl(CString src, ShaderType shaderType, Bool compileWith16b
(void)err; // Shoudn't throw an error
errorMessage = errorMessageTmp;

ShaderCompilerString args;
dxcArgs.join(" ", args);
errorMessage += " (";
errorMessage += args;
errorMessage += ")";

if(!errorMessage.isEmpty() && errorMessage.find("The process cannot access the file because") != CString::kNpos)
{
// DXC might fail to read the HLSL because the antivirus might be having a lock on it. Try again
Expand Down Expand Up @@ -206,16 +222,16 @@ static Error compileHlsl(CString src, ShaderType shaderType, Bool compileWith16b
return Error::kNone;
}

Error compileHlslToSpirv(CString src, ShaderType shaderType, Bool compileWith16bitTypes, ShaderCompilerDynamicArray<U8>& spirv,
Error compileHlslToSpirv(CString src, ShaderType shaderType, Bool compileWith16bitTypes, Bool debugInfo, ShaderCompilerDynamicArray<U8>& spirv,
ShaderCompilerString& errorMessage)
{
return compileHlsl(src, shaderType, compileWith16bitTypes, true, spirv, errorMessage);
return compileHlsl(src, shaderType, compileWith16bitTypes, debugInfo, true, spirv, errorMessage);
}

Error compileHlslToDxil(CString src, ShaderType shaderType, Bool compileWith16bitTypes, ShaderCompilerDynamicArray<U8>& dxil,
Error compileHlslToDxil(CString src, ShaderType shaderType, Bool compileWith16bitTypes, Bool debugInfo, ShaderCompilerDynamicArray<U8>& dxil,
ShaderCompilerString& errorMessage)
{
return compileHlsl(src, shaderType, compileWith16bitTypes, false, dxil, errorMessage);
return compileHlsl(src, shaderType, compileWith16bitTypes, debugInfo, false, dxil, errorMessage);
}

} // end namespace anki
4 changes: 2 additions & 2 deletions AnKi/ShaderCompiler/Dxc.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ inline constexpr Array2d<U32, kMaxDescriptorSets, U32(HlslResourceType::kCount)>
inline constexpr U32 kDxcVkBindlessRegisterSpace = 1000000;

/// Compile HLSL to SPIR-V.
Error compileHlslToSpirv(CString src, ShaderType shaderType, Bool compileWith16bitTypes, ShaderCompilerDynamicArray<U8>& spirv,
Error compileHlslToSpirv(CString src, ShaderType shaderType, Bool compileWith16bitTypes, Bool debugInfo, ShaderCompilerDynamicArray<U8>& spirv,
ShaderCompilerString& errorMessage);

/// Compile HLSL to DXIL.
Error compileHlslToDxil(CString src, ShaderType shaderType, Bool compileWith16bitTypes, ShaderCompilerDynamicArray<U8>& dxil,
Error compileHlslToDxil(CString src, ShaderType shaderType, Bool compileWith16bitTypes, Bool debugInfo, ShaderCompilerDynamicArray<U8>& dxil,
ShaderCompilerString& errorMessage);
/// @}

Expand Down
21 changes: 12 additions & 9 deletions AnKi/ShaderCompiler/ShaderCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ Error doReflectionDxil(ConstWeakArray<U8> dxil, ShaderType type, ShaderReflectio
}
#endif // #if ANKI_DIXL_REFLECTION

static void compileVariantAsync(const ShaderParser& parser, Bool spirv, ShaderBinaryMutation& mutation,
static void compileVariantAsync(const ShaderParser& parser, Bool spirv, Bool debugInfo, ShaderBinaryMutation& mutation,
ShaderCompilerDynamicArray<ShaderBinaryVariant>& variants,
ShaderCompilerDynamicArray<ShaderBinaryCodeBlock>& codeBlocks, ShaderCompilerDynamicArray<U64>& sourceCodeHashes,
ShaderCompilerAsyncTaskInterface& taskManager, Mutex& mtx, Atomic<I32>& error)
Expand All @@ -655,6 +655,7 @@ static void compileVariantAsync(const ShaderParser& parser, Bool spirv, ShaderBi
Mutex* m_mtx;
Atomic<I32>* m_err;
Bool m_spirv;
Bool m_debugInfo;
};

Ctx* ctx = newInstance<Ctx>(ShaderCompilerMemoryPool::getSingleton());
Expand All @@ -666,6 +667,7 @@ static void compileVariantAsync(const ShaderParser& parser, Bool spirv, ShaderBi
ctx->m_mtx = &mtx;
ctx->m_err = &error;
ctx->m_spirv = spirv;
ctx->m_debugInfo = debugInfo;

auto callback = [](void* userData) {
Ctx& ctx = *static_cast<Ctx*>(userData);
Expand Down Expand Up @@ -736,11 +738,11 @@ static void compileVariantAsync(const ShaderParser& parser, Bool spirv, ShaderBi
ShaderCompilerDynamicArray<U8> il;
if(ctx.m_spirv)
{
err = compileHlslToSpirv(source, shaderType, ctx.m_parser->compileWith16bitTypes(), il, compilerErrorLog);
err = compileHlslToSpirv(source, shaderType, ctx.m_parser->compileWith16bitTypes(), ctx.m_debugInfo, il, compilerErrorLog);
}
else
{
err = compileHlslToDxil(source, shaderType, ctx.m_parser->compileWith16bitTypes(), il, compilerErrorLog);
err = compileHlslToDxil(source, shaderType, ctx.m_parser->compileWith16bitTypes(), ctx.m_debugInfo, il, compilerErrorLog);
}

if(err)
Expand Down Expand Up @@ -864,7 +866,7 @@ static void compileVariantAsync(const ShaderParser& parser, Bool spirv, ShaderBi
taskManager.enqueueTask(callback, ctx);
}

static Error compileShaderProgramInternal(CString fname, Bool spirv, ShaderCompilerFilesystemInterface& fsystem,
static Error compileShaderProgramInternal(CString fname, Bool spirv, Bool debugInfo, ShaderCompilerFilesystemInterface& fsystem,
ShaderCompilerPostParseInterface* postParseCallback, ShaderCompilerAsyncTaskInterface* taskManager_,
ConstWeakArray<ShaderCompilerDefine> defines_, ShaderBinary*& binary)
{
Expand Down Expand Up @@ -980,7 +982,7 @@ static Error compileShaderProgramInternal(CString fname, Bool spirv, ShaderCompi
{
// New and unique mutation and thus variant, add it

compileVariantAsync(parser, spirv, mutation, variants, codeBlocks, sourceCodeHashes, taskManager, mtx, errorAtomic);
compileVariantAsync(parser, spirv, debugInfo, mutation, variants, codeBlocks, sourceCodeHashes, taskManager, mtx, errorAtomic);

ANKI_ASSERT(mutationHashToIdx.find(mutation.m_hash) == mutationHashToIdx.getEnd());
mutationHashToIdx.emplace(mutation.m_hash, mutationCount - 1);
Expand All @@ -1007,7 +1009,7 @@ static Error compileShaderProgramInternal(CString fname, Bool spirv, ShaderCompi
ShaderCompilerDynamicArray<ShaderBinaryCodeBlock> codeBlocks;
ShaderCompilerDynamicArray<U64> sourceCodeHashes;

compileVariantAsync(parser, spirv, binary->m_mutations[0], variants, codeBlocks, sourceCodeHashes, taskManager, mtx, errorAtomic);
compileVariantAsync(parser, spirv, debugInfo, binary->m_mutations[0], variants, codeBlocks, sourceCodeHashes, taskManager, mtx, errorAtomic);

ANKI_CHECK(taskManager.joinTasks());
ANKI_CHECK(Error(errorAtomic.getNonAtomically()));
Expand Down Expand Up @@ -1072,10 +1074,11 @@ static Error compileShaderProgramInternal(CString fname, Bool spirv, ShaderCompi
return Error::kNone;
}

Error compileShaderProgram(CString fname, Bool spirv, ShaderCompilerFilesystemInterface& fsystem, ShaderCompilerPostParseInterface* postParseCallback,
ShaderCompilerAsyncTaskInterface* taskManager, ConstWeakArray<ShaderCompilerDefine> defines, ShaderBinary*& binary)
Error compileShaderProgram(CString fname, Bool spirv, Bool debugInfo, ShaderCompilerFilesystemInterface& fsystem,
ShaderCompilerPostParseInterface* postParseCallback, ShaderCompilerAsyncTaskInterface* taskManager,
ConstWeakArray<ShaderCompilerDefine> defines, ShaderBinary*& binary)
{
const Error err = compileShaderProgramInternal(fname, spirv, fsystem, postParseCallback, taskManager, defines, binary);
const Error err = compileShaderProgramInternal(fname, spirv, debugInfo, fsystem, postParseCallback, taskManager, defines, binary);
if(err)
{
ANKI_SHADER_COMPILER_LOGE("Failed to compile: %s", fname.cstr());
Expand Down
5 changes: 3 additions & 2 deletions AnKi/ShaderCompiler/ShaderCompiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ inline Error deserializeShaderBinaryFromFile(CString fname, ShaderBinary*& binar
}

/// Takes an AnKi special shader program and spits a binary.
Error compileShaderProgram(CString fname, Bool spirv, ShaderCompilerFilesystemInterface& fsystem, ShaderCompilerPostParseInterface* postParseCallback,
ShaderCompilerAsyncTaskInterface* taskManager, ConstWeakArray<ShaderCompilerDefine> defines, ShaderBinary*& binary);
Error compileShaderProgram(CString fname, Bool spirv, Bool debugInfo, ShaderCompilerFilesystemInterface& fsystem,
ShaderCompilerPostParseInterface* postParseCallback, ShaderCompilerAsyncTaskInterface* taskManager,
ConstWeakArray<ShaderCompilerDefine> defines, ShaderBinary*& binary);

/// Free the binary created ONLY by compileShaderProgram.
void freeShaderBinary(ShaderBinary*& binary);
Expand Down
2 changes: 1 addition & 1 deletion AnKi/Util/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ class CString
if(len > 0)
{
PtrSize outSize;
[[maybe_unused]] const errno_t err = mbstowcs_s(&outSize, arr, arrSize, m_ptr, len);
[[maybe_unused]] const auto err = mbstowcs_s(&outSize, arr, arrSize, m_ptr, len);
ANKI_ASSERT(err == 0 && outSize == len + 1);
}
else
Expand Down
12 changes: 12 additions & 0 deletions Tests/Gr/GrWorkGraphs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
// All rights reserved.
// Code licensed under the BSD License.
// http://www.anki3d.org/LICENSE

#include <Tests/Framework/Framework.h>
#include <Tests/Gr/GrCommon.h>
#include <AnKi/Gr.h>

ANKI_TEST(Gr, WorkGraphsHelloWorld)
{
}
10 changes: 8 additions & 2 deletions Tools/Shader/ShaderProgramCompilerMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Usage: %s [options] input_shader_program_file
-D<define_name:val> : Extra defines to pass to the compiler
-spirv : Compile SPIR-V
-dxil : Compile DXIL
-g : Include debug info
)";

class CmdLineArgs
Expand All @@ -29,6 +30,7 @@ class CmdLineArgs
DynamicArray<ShaderCompilerDefine> m_defines;
Bool m_spirv = false;
Bool m_dxil = false;
Bool m_debugInfo = false;
};

static Error parseCommandLineArgs(int argc, char** argv, CmdLineArgs& info)
Expand Down Expand Up @@ -130,6 +132,10 @@ static Error parseCommandLineArgs(int argc, char** argv, CmdLineArgs& info)
{
info.m_dxil = true;
}
else if(strcmp(argv[i], "-g") == 0)
{
info.m_debugInfo = true;
}
else
{
return Error::kUserData;
Expand Down Expand Up @@ -210,8 +216,8 @@ static Error work(const CmdLineArgs& info)

// Compile
ShaderBinary* binary = nullptr;
ANKI_CHECK(compileShaderProgram(info.m_inputFname, info.m_spirv, fsystem, nullptr, (info.m_threadCount) ? &taskManager : nullptr, info.m_defines,
binary));
ANKI_CHECK(compileShaderProgram(info.m_inputFname, info.m_spirv, info.m_debugInfo, fsystem, nullptr,
(info.m_threadCount) ? &taskManager : nullptr, info.m_defines, binary));

class Dummy
{
Expand Down

0 comments on commit 0e27348

Please sign in to comment.