conversion to c by gemini 4 argon

This commit is contained in:
Patedam committed 2026-10-16 23:40:45 -04:00
1 parent b70e2d21d2
commit f9356f9ba7
369 files changed
+18074 -107634

No files matched your search

+133 -163
View File
@@ -1,31 +1,32 @@
#pragma once
/* Win32 Type Definitions */
#include <Core/Common/CoreTypes.h>
#include <wchar.h>
using HRESULT = int;
using LPCVOID = const void*;
using SIZE_T = size_t;
using LPCSTR = const char*;
using UINT = unsigned int;
using ULONG = unsigned long;
using LPVOID = void*;
using REFIID = void*;
/* Win32 Type Definitions */
typedef int HRESULT;
typedef const void* LPCVOID;
typedef size_t SIZE_T;
typedef const char* LPCSTR;
typedef unsigned int UINT;
typedef unsigned long ULONG;
typedef void* LPVOID;
typedef void* REFIID;
/* dxcompiler Type Definitions */
using BOOL = int;
using REFCLSID = void*;
using LPCWSTR = wchar_t*;
using IDxcBlobEncoding = void; /* hack, unused */
using IDxcBlobWide = void; /* hack, unused */
using IDxcIncludeHandler = void; /* hack, unused */
typedef int BOOL;
typedef void* REFCLSID;
typedef const wchar_t* LPCWSTR;
typedef void IDxcBlobEncoding;
typedef void IDxcBlobWide;
typedef void IDxcIncludeHandler;
/* Unlike vkd3d-utils, libdxcompiler.so does not use msabi */
#if !defined(_WIN32)
#define __stdcall
#endif
/* Compiler Interface, _technically_ unofficial but it's MS C++, come on */
using DXC_OUT_KIND = enum DXC_OUT_KIND {
typedef enum DXC_OUT_KIND
{
DXC_OUT_NONE = 0,
DXC_OUT_OBJECT = 1,
DXC_OUT_ERRORS = 2,
@@ -41,125 +42,106 @@ using DXC_OUT_KIND = enum DXC_OUT_KIND {
DXC_OUT_TIME_REPORT = 12,
DXC_OUT_TIME_TRACE = 13,
DXC_OUT_LAST = DXC_OUT_TIME_TRACE,
DXC_OUT_NUM_ENUMS,
// DXC_OUT_FORCE_DWORD = 0xFFFFFFFF
};
DXC_OUT_NUM_ENUMS
} DXC_OUT_KIND;
#define DXC_CP_UTF8 65001
#define DXC_CP_UTF8 65001
#define DXC_CP_UTF16 1200
#define DXC_CP_UTF32 12000
/* This is for binary, ANSI-text, or to tell the compiler to try autodetecting UTF using the BOM */
#define DXC_CP_ACP 0
#define DXC_CP_ACP 0
using DxcBuffer = struct DxcBuffer
typedef struct DxcBuffer DxcBuffer;
struct DxcBuffer
{
LPCVOID Ptr;
SIZE_T Size;
UINT Encoding;
};
/* *INDENT-OFF* */ // clang-format off
static uint8 IID_IDxcBlob[] = {
0x08, 0xFB, 0xA5, 0x8B,
0x95, 0x51,
0xE2, 0x40,
0xAC,
0x58,
0x0D,
0x98,
0x9C,
0x3A,
0x01,
0x02
0xAC, 0x58, 0x0D, 0x98, 0x9C, 0x3A, 0x01, 0x02
};
using IDxcBlob = struct IDxcBlob;
using IDxcBlobVtbl = struct IDxcBlobVtbl
{
HRESULT(__stdcall *QueryInterface)(IDxcBlob *This, REFIID riid, void **ppvObject);
ULONG(__stdcall *AddRef)(IDxcBlob *This);
ULONG(__stdcall *Release)(IDxcBlob *This);
LPVOID(__stdcall *GetBufferPointer)(IDxcBlob *This);
SIZE_T(__stdcall *GetBufferSize)(IDxcBlob *This);
typedef struct IDxcBlob IDxcBlob;
typedef struct IDxcBlobVtbl IDxcBlobVtbl;
struct IDxcBlobVtbl
{
HRESULT(__stdcall* QueryInterface)(IDxcBlob* This, REFIID riid, void** ppvObject);
ULONG(__stdcall* AddRef)(IDxcBlob* This);
ULONG(__stdcall* Release)(IDxcBlob* This);
LPVOID(__stdcall* GetBufferPointer)(IDxcBlob* This);
SIZE_T(__stdcall* GetBufferSize)(IDxcBlob* This);
};
struct IDxcBlob
{
IDxcBlobVtbl *lpVtbl;
IDxcBlobVtbl* lpVtbl;
};
static uint8 IID_IDxcBlobUtf8[] = {
0xC9, 0x36, 0xA6, 0x3D,
0x71, 0xBA,
0x24, 0x40,
0xA3,
0x01,
0x30,
0xCB,
0xF1,
0x25,
0x30,
0x5B
0xA3, 0x01, 0x30, 0xCB, 0xF1, 0x25, 0x30, 0x5B
};
using IDxcBlobUtf8 = struct IDxcBlobUtf8;
using IDxcBlobUtf8Vtbl = struct IDxcBlobUtf8Vtbl
typedef struct IDxcBlobUtf8 IDxcBlobUtf8;
typedef struct IDxcBlobUtf8Vtbl IDxcBlobUtf8Vtbl;
struct IDxcBlobUtf8Vtbl
{
HRESULT(__stdcall *QueryInterface)(IDxcBlobUtf8 *This, REFIID riid, void **ppvObject);
ULONG(__stdcall *AddRef)(IDxcBlobUtf8 *This);
ULONG(__stdcall *Release)(IDxcBlobUtf8 *This);
HRESULT(__stdcall* QueryInterface)(IDxcBlobUtf8* This, REFIID riid, void** ppvObject);
ULONG(__stdcall* AddRef)(IDxcBlobUtf8* This);
ULONG(__stdcall* Release)(IDxcBlobUtf8* This);
LPVOID(__stdcall *GetBufferPointer)(IDxcBlobUtf8 *This);
SIZE_T(__stdcall *GetBufferSize)(IDxcBlobUtf8 *This);
LPVOID(__stdcall* GetBufferPointer)(IDxcBlobUtf8* This);
SIZE_T(__stdcall* GetBufferSize)(IDxcBlobUtf8* This);
HRESULT(__stdcall *GetEncoding)(IDxcBlobUtf8 *This, BOOL *pKnown, uint32 *pCodePage);
HRESULT(__stdcall* GetEncoding)(IDxcBlobUtf8* This, BOOL* pKnown, uint32* pCodePage);
LPCSTR(__stdcall *GetStringPointer)(IDxcBlobUtf8 *This);
SIZE_T(__stdcall *GetStringLength)(IDxcBlobUtf8 *This);
LPCSTR(__stdcall* GetStringPointer)(IDxcBlobUtf8* This);
SIZE_T(__stdcall* GetStringLength)(IDxcBlobUtf8* This);
};
struct IDxcBlobUtf8
{
IDxcBlobUtf8Vtbl *lpVtbl;
IDxcBlobUtf8Vtbl* lpVtbl;
};
static uint8 IID_IDxcResult[] = {
0xDA, 0x6C, 0x34, 0x58,
0xE7, 0xDD,
0x97, 0x44,
0x94,
0x61,
0x6F,
0x87,
0xAF,
0x5E,
0x06,
0x59
0x94, 0x61, 0x6F, 0x87, 0xAF, 0x5E, 0x06, 0x59
};
using IDxcResult = struct IDxcResult;
using IDxcResultVtbl = struct IDxcResultVtbl
typedef struct IDxcResult IDxcResult;
typedef struct IDxcResultVtbl IDxcResultVtbl;
struct IDxcResultVtbl
{
HRESULT(__stdcall *QueryInterface)(IDxcResult *This, REFIID riid, void **ppvObject);
ULONG(__stdcall *AddRef)(IDxcResult *This);
ULONG(__stdcall *Release)(IDxcResult *This);
HRESULT(__stdcall* QueryInterface)(IDxcResult* This, REFIID riid, void** ppvObject);
ULONG(__stdcall* AddRef)(IDxcResult* This);
ULONG(__stdcall* Release)(IDxcResult* This);
HRESULT(__stdcall *GetStatus)(IDxcResult *This, HRESULT *pStatus);
HRESULT(__stdcall *GetResult)(IDxcResult *This, IDxcBlob **ppResult);
HRESULT(__stdcall *GetErrorBuffer)(IDxcResult *This, IDxcBlobEncoding **ppErrors);
HRESULT(__stdcall* GetStatus)(IDxcResult* This, HRESULT* pStatus);
HRESULT(__stdcall* GetResult)(IDxcResult* This, IDxcBlob** ppResult);
HRESULT(__stdcall* GetErrorBuffer)(IDxcResult* This, IDxcBlobEncoding** ppErrors);
BOOL(__stdcall *HasOutput)(IDxcResult *This, DXC_OUT_KIND dxcOutKind);
HRESULT(__stdcall *GetOutput)(
IDxcResult *This,
DXC_OUT_KIND dxcOutKind,
REFIID iid,
void **ppvObject,
IDxcBlobWide **ppOutputName
);
uint32(__stdcall *GetNumOutputs)(IDxcResult *This);
DXC_OUT_KIND(__stdcall *GetOutputByIndex)(IDxcResult *This, uint32 Index);
DXC_OUT_KIND(__stdcall *PrimaryOutput)(IDxcResult *This);
BOOL(__stdcall* HasOutput)(IDxcResult* This, DXC_OUT_KIND dxcOutKind);
HRESULT(__stdcall* GetOutput)(
IDxcResult* This,
DXC_OUT_KIND dxcOutKind,
REFIID iid,
void** ppvObject,
IDxcBlobWide** ppOutputName);
uint32(__stdcall* GetNumOutputs)(IDxcResult* This);
DXC_OUT_KIND(__stdcall* GetOutputByIndex)(IDxcResult* This, uint32 Index);
DXC_OUT_KIND(__stdcall* PrimaryOutput)(IDxcResult* This);
};
struct IDxcResult
{
IDxcResultVtbl *lpVtbl;
IDxcResultVtbl* lpVtbl;
};
static struct
@@ -167,117 +149,105 @@ static struct
uint32 Data1;
uint16 Data2;
uint16 Data3;
uint8 Data4[8];
uint8 Data4[8];
} CLSID_DxcCompiler = {
.Data1 = 0x73e22d93,
.Data2 = 0xe6ce,
.Data3 = 0x47f3,
.Data4 = { 0xb5, 0xbf, 0xf0, 0x66, 0x4f, 0x39, 0xc1, 0xb0 }
};
static uint8 IID_IDxcCompiler3[] = {
0x87, 0x46, 0x8B, 0x22,
0x6A, 0x5A,
0x30, 0x47,
0x90,
0x0C,
0x97,
0x02,
0xB2,
0x20,
0x3F,
0x54
0x90, 0x0C, 0x97, 0x02, 0xB2, 0x20, 0x3F, 0x54
};
using IDxcCompiler3 = struct IDxcCompiler3;
using IDxcCompiler3Vtbl = struct IDxcCompiler3Vtbl
typedef struct IDxcCompiler3 IDxcCompiler3;
typedef struct IDxcCompiler3Vtbl IDxcCompiler3Vtbl;
struct IDxcCompiler3Vtbl
{
HRESULT(__stdcall *QueryInterface)(IDxcCompiler3 *This, REFIID riid, void **ppvObject);
ULONG(__stdcall *AddRef)(IDxcCompiler3 *This);
ULONG(__stdcall *Release)(IDxcCompiler3 *This);
HRESULT(__stdcall* QueryInterface)(IDxcCompiler3* This, REFIID riid, void** ppvObject);
ULONG(__stdcall* AddRef)(IDxcCompiler3* This);
ULONG(__stdcall* Release)(IDxcCompiler3* This);
HRESULT(__stdcall *Compile)(
IDxcCompiler3 *This,
const DxcBuffer *pSource,
LPCWSTR *pArguments,
uint32 argCount,
IDxcIncludeHandler *pIncludeHandler,
REFIID riid,
LPVOID *ppResult
);
HRESULT(__stdcall* Compile)(
IDxcCompiler3* This,
const DxcBuffer* pSource,
LPCWSTR* pArguments,
uint32 argCount,
IDxcIncludeHandler* pIncludeHandler,
REFIID riid,
LPVOID* ppResult);
HRESULT(__stdcall *Disassemble)(
IDxcCompiler3 *This,
const DxcBuffer *pObject,
REFIID riid,
LPVOID *ppResult
);
HRESULT(__stdcall* Disassemble)(
IDxcCompiler3* This,
const DxcBuffer* pObject,
REFIID riid,
LPVOID* ppResult);
};
struct IDxcCompiler3
{
const IDxcCompiler3Vtbl *lpVtbl;
const IDxcCompiler3Vtbl* lpVtbl;
};
// We need all this DxcUtils garbage for DXC include dir support. Thanks Microsoft!
using IMalloc = struct IMalloc;
using IStream = struct IStream;
using DxcDefine = struct DxcDefine;
using IDxcCompilerArgs = struct IDxcCompilerArgs;
typedef struct IMalloc IMalloc;
typedef struct IStream IStream;
typedef struct DxcDefine DxcDefine;
typedef struct IDxcCompilerArgs IDxcCompilerArgs;
static struct
{
uint32 Data1;
uint16 Data2;
uint16 Data3;
uint8 Data4[8];
uint8 Data4[8];
} CLSID_DxcUtils = {
.Data1 = 0x6245d6af,
.Data2 = 0x66e0,
.Data3 = 0x48fd,
.Data4 = {0x80, 0xb4, 0x4d, 0x27, 0x17, 0x96, 0x74, 0x8c}};
.Data4 = { 0x80, 0xb4, 0x4d, 0x27, 0x17, 0x96, 0x74, 0x8c }
};
static uint8 IID_IDxcUtils[] = {
0xcb, 0xc4, 0x05, 0x46,
0x19, 0x20,
0x2a, 0x49,
0xad,
0xa4,
0x65,
0xf2,
0x0b,
0xb7,
0xd6,
0x7f
0xad, 0xa4, 0x65, 0xf2, 0x0b, 0xb7, 0xd6, 0x7f
};
using IDxcUtilsVtbl = struct IDxcUtilsVtbl
typedef struct IDxcUtils IDxcUtils;
typedef struct IDxcUtilsVtbl IDxcUtilsVtbl;
struct IDxcUtilsVtbl
{
HRESULT (__stdcall *QueryInterface)(void *pSelf, REFIID riid, void **ppvObject);
ULONG (__stdcall *AddRef)(void *pSelf);
ULONG (__stdcall *Release)(void *pSelf);
HRESULT(__stdcall* QueryInterface)(void* pSelf, REFIID riid, void** ppvObject);
ULONG(__stdcall* AddRef)(void* pSelf);
ULONG(__stdcall* Release)(void* pSelf);
HRESULT (__stdcall *CreateBlobFromBlob)(void *pSelf, IDxcBlob *pBlob, UINT offset, UINT length, IDxcBlob **ppResult);
HRESULT (__stdcall *CreateBlobFromPinned)(void *pSelf, LPCVOID pData, UINT size, UINT codePage, IDxcBlobEncoding **pBlobEncoding);
HRESULT (__stdcall *MoveToBlob)(void *pSelf, LPCVOID pData, IMalloc *pIMalloc, UINT size, UINT codePage, IDxcBlobEncoding **pBlobEncoding);
HRESULT (__stdcall *CreateBlob)(void *pSelf, LPCVOID pData, UINT size, UINT codePage, IDxcBlobEncoding **pBlobEncoding);
HRESULT (__stdcall *LoadFile)(void *pSelf, LPCWSTR pFileName, UINT *pCodePage, IDxcBlobEncoding **pBlobEncoding);
HRESULT (__stdcall *CreateReadOnlyStreamFromBlob)(void *pSelf, IDxcBlob *pBlob, IStream **ppStream);
HRESULT (__stdcall *CreateDefaultIncludeHandler)(void *pSelf, IDxcIncludeHandler **ppResult);
HRESULT (__stdcall *GetBlobAsUtf8)(void *pSelf, IDxcBlob *pBlob, IDxcBlobUtf8 **pBlobEncoding);
HRESULT (__stdcall *GetBlobAsWide)(void *pSelf, IDxcBlob *pBlob, IDxcBlobWide **pBlobEncoding);
HRESULT (__stdcall *GetDxilContainerPart)(void *pSelf, const DxcBuffer *pShader, UINT DxcPart, void **ppPartData, UINT *pPartSizeInBytes);
HRESULT (__stdcall *CreateReflection)(void *pSelf, const DxcBuffer *pData, REFIID iid, void **ppvReflection);
HRESULT (__stdcall *BuildArguments)(void *pSelf, LPCWSTR pSourceName, LPCWSTR pEntryPoint, LPCWSTR pTargetProfile, LPCWSTR *pArguments, UINT argCount, const DxcDefine *pDefines, UINT defineCount, IDxcCompilerArgs **ppArgs);
HRESULT (__stdcall *GetPDBContents)(void *pSelf, IDxcBlob *pPDBBlob, IDxcBlob **ppHash, IDxcBlob **ppContainer);
HRESULT(__stdcall* CreateBlobFromBlob)(void* pSelf, IDxcBlob* pBlob, UINT offset, UINT length, IDxcBlob** ppResult);
HRESULT(__stdcall* CreateBlobFromPinned)(void* pSelf, LPCVOID pData, UINT size, UINT codePage, IDxcBlobEncoding** pBlobEncoding);
HRESULT(__stdcall* MoveToBlob)(void* pSelf, LPCVOID pData, IMalloc* pIMalloc, UINT size, UINT codePage, IDxcBlobEncoding** pBlobEncoding);
HRESULT(__stdcall* CreateBlob)(void* pSelf, LPCVOID pData, UINT size, UINT codePage, IDxcBlobEncoding** pBlobEncoding);
HRESULT(__stdcall* LoadFile)(void* pSelf, LPCWSTR pFileName, UINT* pCodePage, IDxcBlobEncoding** pBlobEncoding);
HRESULT(__stdcall* CreateReadOnlyStreamFromBlob)(void* pSelf, IDxcBlob* pBlob, IStream** ppStream);
HRESULT(__stdcall* CreateDefaultIncludeHandler)(void* pSelf, IDxcIncludeHandler** ppResult);
HRESULT(__stdcall* GetBlobAsUtf8)(void* pSelf, IDxcBlob* pBlob, IDxcBlobUtf8** pBlobEncoding);
HRESULT(__stdcall* GetBlobAsWide)(void* pSelf, IDxcBlob* pBlob, IDxcBlobWide** pBlobEncoding);
HRESULT(__stdcall* GetDxilContainerPart)(void* pSelf, const DxcBuffer* pShader, UINT DxcPart, void** ppPartData, UINT* pPartSizeInBytes);
HRESULT(__stdcall* CreateReflection)(void* pSelf, const DxcBuffer* pData, REFIID iid, void** ppvReflection);
HRESULT(__stdcall* BuildArguments)(void* pSelf, LPCWSTR pSourceName, LPCWSTR pEntryPoint, LPCWSTR pTargetProfile, LPCWSTR* pArguments, UINT argCount, const DxcDefine* pDefines, UINT defineCount, IDxcCompilerArgs** ppArgs);
HRESULT(__stdcall* GetPDBContents)(void* pSelf, IDxcBlob* pPDBBlob, IDxcBlob** ppHash, IDxcBlob** ppContainer);
};
using IDxcUtils = struct IDxcUtils;
struct IDxcUtils
{
const IDxcUtilsVtbl *lpVtbl;
const IDxcUtilsVtbl* lpVtbl;
};
/* *INDENT-ON* */ // clang-format on
/* DXCompiler */
#if defined(SDL_PLATFORM_XBOXONE) || defined(SDL_PLATFORM_XBOXSERIES) || defined(SDL_PLATFORM_WINDOWS)
extern "C" HRESULT __stdcall DxcCreateInstance(REFCLSID rclsid, REFIID riid, LPVOID* ppv);
#else
extern "C" HRESULT DxcCreateInstance(REFCLSID rclsid, REFIID riid, LPVOID* ppv);
#ifdef __cplusplus
extern "C" {
#endif
HRESULT __stdcall DxcCreateInstance(REFCLSID rclsid, REFIID riid, LPVOID* ppv);
#ifdef __cplusplus
}
#endif
+189
View File
@@ -0,0 +1,189 @@
#include <ShaderCompiler.h>
#include <Core/Common/CoreUtils.h>
#include <Core/Logging/LogManager.h>
#include <Core/Logging/LogTypes.h>
#include <Core/Memory/Allocator.h>
#include <DXShaderCompiler/DXCompiler.h>
internal ByteBuffer CompileWithDXC(const HLSLShaderInfo* info, bool8 compileToSpirv)
{
Assert(info != NULL);
Assert(!compileToSpirv && "SPIRV is not supported yet");
ByteBuffer outBuffer = { 0 };
IDxcCompiler3* compiler = NULL;
DxcCreateInstance(&CLSID_DxcCompiler, IID_IDxcCompiler3, (void**)&compiler);
IDxcUtils* utils = NULL;
DxcCreateInstance(&CLSID_DxcUtils, IID_IDxcUtils, (void**)&utils);
IDxcResult* dxcResult = NULL;
LPCWSTR* args = NULL;
if (compiler == NULL)
{
LogError(LogCategory_Tool, "Cannot create DXCompiler instance");
}
else if (utils == NULL)
{
LogError(LogCategory_Graphics, "Cannot create IDxcUtils instance");
}
else
{
IDxcIncludeHandler* includeHandler = NULL;
utils->lpVtbl->CreateDefaultIncludeHandler(utils, &includeHandler);
if (includeHandler == NULL)
{
LogError(LogCategory_Graphics, "Cannot create Default include handler");
}
else
{
StringBuffer convertedBuffer = { 0 };
char inplaceBuffer[1024];
convertedBuffer.Str = inplaceBuffer;
convertedBuffer.Capacity = sizeof(inplaceBuffer);
const bool8 kAddNullTerminator = true;
if (!ConvertString(StringEncoding_UTF8, StringEncoding_UTF16, info->EntryPoint, &convertedBuffer, kAddNullTerminator))
{
LogError(LogCategory_Tool, "Cannot convert entry point string to utf16");
}
else
{
const size_t kExpectedArgCount = 32;
uint32 argCount = 0;
args = (LPCWSTR*)Calloc(kExpectedArgCount, sizeof(LPCWSTR));
if (args == NULL)
{
LogError(LogCategory_Tool, "Couldn't allocate Args array");
}
else
{
args[argCount++] = L"-E";
args[argCount++] = (LPCWSTR)inplaceBuffer;
DxcBuffer source;
source.Ptr = info->ByteCodeBuffer.Data;
source.Size = info->ByteCodeBuffer.Size;
source.Encoding = DXC_CP_ACP;
bool8 stageValid = true;
switch (info->Stage)
{
case ShaderStage_Vertex:
{
args[argCount++] = L"-T";
args[argCount++] = L"vs_6_6";
break;
}
case ShaderStage_Fragment:
{
args[argCount++] = L"-T";
args[argCount++] = L"ps_6_6";
break;
}
case ShaderStage_Compute:
{
args[argCount++] = L"-T";
args[argCount++] = L"cs_6_6";
break;
}
default:
{
Assert(false && "Invalid shader stage");
stageValid = false;
break;
}
}
if (stageValid)
{
if (compileToSpirv)
{
args[argCount++] = L"-spirv";
args[argCount++] = L"-fspv-flatten-resource-arrays";
}
char nameInplaceBuffer[1024];
if (IsValidString(info->Name))
{
StringBuffer nameUtf16 = { 0 };
nameUtf16.Str = nameInplaceBuffer;
nameUtf16.Capacity = sizeof(nameInplaceBuffer);
if (ConvertString(StringEncoding_UTF8, StringEncoding_UTF16, info->Name, &nameUtf16, kAddNullTerminator))
{
args[argCount++] = (LPCWSTR)nameInplaceBuffer;
}
}
HRESULT result = compiler->lpVtbl->Compile(compiler, &source, args, argCount, includeHandler,
IID_IDxcResult, (void**)&dxcResult);
if (result < 0)
{
LogError(LogCategory_Tool, "IDxcShaderCompiler3::Compile failed: %X", result);
}
else if (dxcResult == NULL)
{
LogError(LogCategory_Tool, "%s", "HLSL compilation failed with no IDxcResult");
}
else
{
IDxcBlob* blob = NULL;
IDxcBlobUtf8* errors = NULL;
result = dxcResult->lpVtbl->GetOutput(dxcResult, DXC_OUT_OBJECT, IID_IDxcBlob, (void**)&blob, NULL);
if (result < 0)
{
dxcResult->lpVtbl->GetOutput(dxcResult, DXC_OUT_ERRORS, IID_IDxcBlobUtf8, (void**)&errors, NULL);
if (errors != NULL && errors->lpVtbl->GetBufferSize(errors) != 0)
{
LogError(LogCategory_Tool, "HLSL compilation failed: %s",
(char*)errors->lpVtbl->GetBufferPointer(errors));
}
else
{
LogError(LogCategory_Tool, "Compilation failed with unknown error");
}
}
else
{
dxcResult->lpVtbl->GetOutput(dxcResult, DXC_OUT_ERRORS, IID_IDxcBlobUtf8, (void**)&errors, NULL);
if (errors != NULL && errors->lpVtbl->GetBufferSize(errors) != 0)
{
LogWarning(LogCategory_Tool, "HLSL compiled with warnings: %s",
(char*)errors->lpVtbl->GetBufferPointer(errors));
}
outBuffer.Size = blob->lpVtbl->GetBufferSize(blob);
outBuffer.Data = (Byte*)Malloc(outBuffer.Size);
MemCopy(outBuffer.Data, blob->lpVtbl->GetBufferPointer(blob), outBuffer.Size);
blob->lpVtbl->Release(blob);
}
}
}
}
}
}
}
SafeFree(args);
if (dxcResult != NULL)
{
dxcResult->lpVtbl->Release(dxcResult);
}
if (compiler != NULL)
{
compiler->lpVtbl->Release(compiler);
}
if (utils != NULL)
{
utils->lpVtbl->Release(utils);
}
return outBuffer;
}
ByteBuffer CompileDXIL(const HLSLShaderInfo* info)
{
return CompileWithDXC(info, false);
}
-198
View File
@@ -1,198 +0,0 @@
#include <ShaderCompiler.h>
#include <Core/Common/CoreUtils.h>
#include <Core/Logging/LogManager.h>
#include <Core/Logging/LogTypes.h>
#include <Core/Memory/Allocator.h>
#include <DXShaderCompiler/DXCompiler.h>
namespace
{
ByteBuffer CompileWithDXC(HLSLShaderInfo& info, bool compileToSpirv)
{
Assert(!compileToSpirv && "SPIRV is not supported yet");
IDxcCompiler3* compiler = nullptr;
DxcCreateInstance(&CLSID_DxcCompiler, IID_IDxcCompiler3, reinterpret_cast<void**>(&compiler));
IDxcUtils* utils = nullptr;
DxcCreateInstance(&CLSID_DxcUtils, &IID_IDxcUtils, reinterpret_cast<void**>(&utils));
if (compiler == nullptr)
{
LogError(LogCategory::Tool, "Cannot create DXCompiler instance");
return {};
}
IDxcResult* dxcResult = nullptr;
LPCWSTR* args = nullptr;
auto deferred = Defer(
[&]()
{
SafeFree(args);
if (dxcResult)
{
dxcResult->lpVtbl->Release(dxcResult);
}
if (compiler)
{
compiler->lpVtbl->Release(compiler);
}
if (utils)
{
utils->lpVtbl->Release(utils);
}
});
if (utils == nullptr)
{
LogError(LogCategory::Graphics, "Cannot create IDxcUtils instance");
return {};
}
IDxcIncludeHandler* includeHandler;
utils->lpVtbl->CreateDefaultIncludeHandler(utils, &includeHandler);
if (includeHandler == nullptr)
{
LogError(LogCategory::Graphics, "Cannot create Default include handler");
return {};
}
StringBuffer convertedBuffer = {};
char inplaceBuffer[1024];
convertedBuffer.Data = inplaceBuffer;
convertedBuffer.Capacity = sizeof(inplaceBuffer);
constexpr bool kAddNullTerminator = true;
if (!ConvertString(StringEncoding::UTF8, StringEncoding::UTF16, info.EntryPoint, convertedBuffer, kAddNullTerminator))
{
LogError(LogCategory::Tool, "Cannot convert entry point string to utf16");
return {};
}
// Creating args for the compiler
// Allocating enough to fit them all
static constexpr size_t kExpectedArgCount = 32;
uint32 argCount = 0;
args = static_cast<LPCWSTR*>(Calloc(kExpectedArgCount, sizeof(LPCWSTR)));
if (!args)
{
LogError(LogCategory::Tool, "Couldn't allocate Args array");
return {};
}
args[argCount++] = const_cast<LPCWSTR>(L"-E");
args[argCount++] = reinterpret_cast<LPCWSTR>(inplaceBuffer);
DxcBuffer source;
source.Ptr = info.ByteCodeBuffer.Data;
source.Size = info.ByteCodeBuffer.Size;
source.Encoding = DXC_CP_ACP;
switch (info.Stage)
{
case ShaderStage::Vertex:
{
args[argCount++] = const_cast<LPCWSTR>(L"-T");
args[argCount++] = const_cast<LPCWSTR>(L"vs_6_6");
break;
}
case ShaderStage::Fragment:
{
args[argCount++] = const_cast<LPCWSTR>(L"-T");
args[argCount++] = const_cast<LPCWSTR>(L"ps_6_6");
break;
}
case ShaderStage::Compute:
{
args[argCount++] = const_cast<LPCWSTR>(L"-T");
args[argCount++] = const_cast<LPCWSTR>(L"cs_6_6");
break;
}
case ShaderStage::Invalid:
{
Assert(false , "Invalid shader stage");
return {};
}
}
if (compileToSpirv)
{
args[argCount++] = const_cast<LPCWSTR>(L"-spirv");
args[argCount++] = const_cast<LPCWSTR>(L"-fspv-flatten-resource-arrays");
}
char nameInplaceBuffer[1024];
if (IsValid(info.Name))
{
StringBuffer nameUtf16 = {};
nameUtf16.Data = nameInplaceBuffer;
nameUtf16.Capacity = sizeof(nameInplaceBuffer);
if (ConvertString(StringEncoding::UTF8, StringEncoding::UTF16, info.Name, nameUtf16, kAddNullTerminator))
{
args[argCount++] = reinterpret_cast<LPCWSTR>(nameInplaceBuffer);
}
}
HRESULT result = compiler->lpVtbl->Compile(compiler, &source, args, argCount, includeHandler, IID_IDxcResult,
reinterpret_cast<void**>(&dxcResult));
if (result < 0)
{
LogError(LogCategory::Tool, "IDxcShaderCompiler3::Compile failed: %X", result);
return {};
}
if (dxcResult == nullptr)
{
LogError(LogCategory::Tool, "%s", "HLSL compilation failed with no IDxcResult");
return {};
}
IDxcBlob* blob = nullptr;
IDxcBlobUtf8* errors = nullptr;
result = dxcResult->lpVtbl->GetOutput(dxcResult, DXC_OUT_OBJECT, IID_IDxcBlob, reinterpret_cast<void**>(&blob), nullptr);
if (result < 0)
{
// Compilation failed, display errors
dxcResult->lpVtbl->GetOutput(dxcResult, DXC_OUT_ERRORS, IID_IDxcBlobUtf8, reinterpret_cast<void**>(&errors), nullptr);
if (errors != nullptr && errors->lpVtbl->GetBufferSize(errors) != 0)
{
LogError(LogCategory::Tool, "HLSL compilation failed: %s",
static_cast<char*>(errors->lpVtbl->GetBufferPointer(errors)));
}
else
{
LogError(LogCategory::Tool, "Compilation failed with unknown error");
}
return {};
}
// If compilation succeeded, but there are errors, those are warnings
dxcResult->lpVtbl->GetOutput(dxcResult, DXC_OUT_ERRORS, IID_IDxcBlobUtf8, reinterpret_cast<void**>(&errors), nullptr);
if (errors != nullptr && errors->lpVtbl->GetBufferSize(errors) != 0)
{
LogWarning(LogCategory::Tool, "HLSL compiled with warnings: %s",
static_cast<char*>(errors->lpVtbl->GetBufferPointer(errors)));
}
// TODO: Scratch allocator. No malloc
ByteBuffer buffer = {};
buffer.Size = blob->lpVtbl->GetBufferSize(blob);
buffer.Data = static_cast<Byte*>(Malloc(buffer.Size));
MemCopy(buffer.Data, blob->lpVtbl->GetBufferPointer(blob), buffer.Size);
blob->lpVtbl->Release(blob);
return buffer;
}
} // namespace
ByteBuffer CompileDXIL(HLSLShaderInfo& info)
{
// TODO: Use SPIRV an do hlsl -> spirv -> hlsl -> dxil.
// This is from Shadercross SDL_ShaderCross_CompileDXILFromHLSL.
// Not sure why yet.
return CompileWithDXC(info, false);
}
+25 -24
View File
@@ -1,42 +1,43 @@
#pragma once
#pragma once
#include <Core/Common/CoreTypes.h>
#include <Core/Common/String.h>
enum class SourceLanguage : uint8
typedef uint8 SourceLanguage;
enum
{
HLSL = 0,
Count = 1,
Invalid = Count
SourceLanguage_HLSL = 0,
SourceLanguage_Count = 1,
SourceLanguage_Invalid = SourceLanguage_Count
};
enum class ShaderFormat : uint8
typedef uint8 ShaderFormat;
enum
{
DXIL = 0,
Count = 1,
Invalid = Count
ShaderFormat_DXIL = 0,
ShaderFormat_Count = 1,
ShaderFormat_Invalid = ShaderFormat_Count
};
enum class ShaderStage : uint8
typedef uint8 ShaderStage;
enum
{
Vertex = 0,
Fragment = 1,
Compute = 2,
Count = 3,
Invalid = Count
ShaderStage_Vertex = 0,
ShaderStage_Fragment = 1,
ShaderStage_Compute = 2,
ShaderStage_Count = 3,
ShaderStage_Invalid = ShaderStage_Count
};
typedef struct ShaderInfo ShaderInfo;
struct ShaderInfo
{
ByteBuffer ByteCodeBuffer;
String EntryPoint;
String Name;
ShaderStage Stage;
ByteBuffer ByteCodeBuffer;
String EntryPoint;
String Name;
ShaderStage Stage;
};
struct HLSLShaderInfo : ShaderInfo
{
// TODO Defines and includes
};
typedef ShaderInfo HLSLShaderInfo;
ByteBuffer CompileDXIL(HLSLShaderInfo& info);
ByteBuffer CompileDXIL(const HLSLShaderInfo* info);
+231
View File
@@ -0,0 +1,231 @@
#include <Core/Common/String.h>
#include <Core/HAL/IO/IOStream.h>
#include <Core/Logging/LogManager.h>
#include <Core/Logging/LogTypes.h>
#include <Core/Memory/Allocator.h>
#include <Core/Memory/MemoryArena.h>
#include <ShaderCompiler.h>
internal void PrintHelp(void)
{
LogMessage(LogCategory_Tool, "Usage: JulietShaderCompiler.exe <input> [options]");
LogMessage(LogCategory_Tool, "Required Parameters:");
const int kColumnWidth = 32;
LogMessage(LogCategory_Tool, " %-*s %s", kColumnWidth, "-o | --output <value>", "Output file.");
}
int main(int argc, char* argv[])
{
int exitCode = 0;
String filename = { 0 };
ByteBuffer fileData = { 0 };
String outputFilename = { 0 };
String entryPoint = WrapString("main");
bool8 sourceLangDetected = false;
SourceLanguage srcLanguage = SourceLanguage_Invalid;
bool8 dstShaderFormatDetected = false;
ShaderFormat dstShaderFormat = ShaderFormat_Invalid;
bool8 shaderStageDetected = false;
ShaderStage shaderStage = ShaderStage_Invalid;
IOStream* outStream = NULL;
Arena* arena = ArenaAllocate((ArenaParams){ .Name = "ShaderCompilerArena" });
for (int idx = 1; idx < argc; ++idx)
{
String arg = WrapString(argv[idx]);
if (CStr(arg)[0] == '-')
{
if (StringCompareCaseInsensitive(arg, ConstString("-h")) == 0 ||
StringCompareCaseInsensitive(arg, ConstString("--help")) == 0)
{
PrintHelp();
exitCode = 1;
break;
}
if (StringCompareCaseInsensitive(arg, ConstString("-o")) == 0 ||
StringCompareCaseInsensitive(arg, ConstString("--output")) == 0)
{
if (idx + 1 >= argc)
{
LogError(LogCategory_Tool, "%s requires an argument", argv[idx]);
PrintHelp();
exitCode = 1;
break;
}
idx += 1;
outputFilename = WrapString(argv[idx]);
}
}
else if (!IsValidString(filename))
{
filename = arg;
}
else
{
LogError(LogCategory_Tool, "%s: Unknown parameter: %s", argv[0], CStr(arg));
PrintHelp();
exitCode = 1;
break;
}
}
if (exitCode == 0)
{
if (!IsValidString(filename))
{
LogError(LogCategory_Tool, "%s: missing input filename path", argv[0]);
PrintHelp();
exitCode = 1;
}
else if (!IsValidString(outputFilename))
{
LogError(LogCategory_Tool, "%s: missing output path", argv[0]);
PrintHelp();
exitCode = 1;
}
}
if (exitCode == 0)
{
fileData = LoadFile(arena, filename);
if (!IsValidByteBuffer(fileData))
{
LogError(LogCategory_Tool, "Invalid file: %s", CStr(filename));
exitCode = 1;
}
}
if (exitCode == 0)
{
if (!sourceLangDetected)
{
if (IsValidString(FindString(filename, WrapString("hlsl"))))
{
srcLanguage = SourceLanguage_HLSL;
}
StaticAssert(SourceLanguage_Count == 1, SourceLanguageCountCheck);
}
if (srcLanguage == SourceLanguage_Invalid)
{
LogError(LogCategory_Tool,
"%s: Invalid source language. Please either have a valid file extension or use -s/--src", argv[0]);
PrintHelp();
exitCode = 1;
}
}
if (exitCode == 0)
{
if (!dstShaderFormatDetected)
{
if (IsValidString(FindString(outputFilename, WrapString("dxil"))))
{
dstShaderFormat = ShaderFormat_DXIL;
}
StaticAssert(ShaderFormat_Count == 1, ShaderFormatCountCheck);
if (dstShaderFormat == ShaderFormat_Invalid)
{
LogError(LogCategory_Tool,
"%s: Invalid shader format. Please either have a valid file extension or use -d/--dest", argv[0]);
PrintHelp();
exitCode = 1;
}
}
}
if (exitCode == 0)
{
if (!shaderStageDetected)
{
if (IsValidString(FindString(filename, WrapString("vert"))))
{
shaderStage = ShaderStage_Vertex;
}
else if (IsValidString(FindString(filename, WrapString("frag"))))
{
shaderStage = ShaderStage_Fragment;
}
else if (IsValidString(FindString(filename, WrapString("comp"))))
{
shaderStage = ShaderStage_Compute;
}
StaticAssert(ShaderFormat_Count == 1, ShaderStageFormatCountCheck);
if (shaderStage == ShaderStage_Invalid)
{
LogError(LogCategory_Tool, "%s: Invalid shader stage. Please include it in the filename", argv[0]);
PrintHelp();
exitCode = 1;
}
}
}
if (exitCode == 0)
{
outStream = IOFromFile(arena, outputFilename, WrapString("wb"));
if (outStream == NULL)
{
LogError(LogCategory_Tool, "%s: Failed to open %s for writing", argv[0], CStr(outputFilename));
exitCode = 1;
}
}
if (exitCode == 0)
{
switch (srcLanguage)
{
case SourceLanguage_HLSL:
{
HLSLShaderInfo hlslInfo = { 0 };
hlslInfo.ByteCodeBuffer = fileData;
hlslInfo.EntryPoint = entryPoint;
hlslInfo.Name = filename;
hlslInfo.Stage = shaderStage;
switch (dstShaderFormat)
{
case ShaderFormat_DXIL:
{
ByteBuffer compiledBuffer = CompileDXIL(&hlslInfo);
if (!IsValidByteBuffer(compiledBuffer))
{
LogError(LogCategory_Tool, "Failed to compile DXBC from HLSL. Error: %s", "Unknown");
}
else
{
IOWrite(outStream, compiledBuffer);
FreeByteBuffer(&compiledBuffer);
}
break;
}
default:
{
Assert(false && "Shader format is invalid");
exitCode = 1;
break;
}
}
break;
}
default:
{
Assert(false && "Source Language is invalid");
exitCode = 1;
break;
}
}
}
if (outStream != NULL)
{
IOClose(outStream);
}
ArenaRelease(arena);
return exitCode;
}
-224
View File
@@ -1,224 +0,0 @@
#include <Core/Common/EnumUtils.h>
#include <Core/Common/String.h>
#include <Core/HAL/IO/IOStream.h>
#include <Core/Logging/LogManager.h>
#include <Core/Logging/LogTypes.h>
#include <Core/Memory/Allocator.h>
#include <ShaderCompiler.h>
void PrintHelp()
{
LogMessage(LogCategory::Tool, "Usage: JulietShaderCompiler.exe <input> [options]");
LogMessage(LogCategory::Tool, "Required Parameters:");
static constexpr int kColumnWdith = 32;
LogMessage(LogCategory::Tool, " %-*s %s", kColumnWdith, "-o | --output <value>", "Output file.");
// LogMessage(LogCategory::Tool, "\n");
// LogMessage(LogCategory::Tool, "Required Parameters that can be inferred from filename:");
// LogMessage(LogCategory::Tool, " %-*s %s", kColumnWdith, "-s | --src <value>", "Source Language. Values: [HLSL*] | * Default");
// LogMessage(LogCategory::Tool, " %-*s %s", kColumnWdith, "-d | --dest <value>", "Destination Format. Values: [DXIL*] | * Default");
}
int main(int argc, char* argv[])
{
String filename = {};
ByteBuffer fileData = {};
String outputFilename = {};
String entryPoint = WrapString("main");
bool sourceLangDetected = false;
auto srcLanguage = SourceLanguage::Invalid;
bool dstShaderFormatDetected = false;
auto dstShaderFormat = ShaderFormat::Invalid;
bool shaderStageDetected = false;
auto shaderStage = ShaderStage::Invalid;
IOStream* outStream = nullptr;
auto deferred = Defer(
[&]()
{
if (IsValid(fileData))
{
Free(fileData);
}
if (outStream)
{
IOClose(outStream);
}
// Pause here to not close the console window immediately on stop while debugging
// __asm int 3;
});
for (int idx = 1; idx < argc; ++idx)
{
String arg = WrapString(argv[idx]);
if (CStr(arg)[0] == '-')
{
if (StringCompareCaseInsensitive(arg, ConstString("-h")) == 0 ||
StringCompareCaseInsensitive(arg, ConstString("--help")) == 0)
{
PrintHelp();
return 1;
}
if (StringCompareCaseInsensitive(arg, ConstString("-o")) == 0 ||
StringCompareCaseInsensitive(arg, ConstString("--output")) == 0)
{
if (idx + 1 >= argc)
{
LogError(LogCategory::Tool, "%s requires an argument", argv);
PrintHelp();
return 1;
}
idx += 1;
outputFilename = WrapString(argv[idx]);
}
}
else if (IsValid(filename) == false)
{
filename = arg;
}
else
{
LogError(LogCategory::Tool, "%s: Unknown parameter: %s", argv[0], CStr(arg));
PrintHelp();
return 1;
}
}
if (!IsValid(filename))
{
LogError(LogCategory::Tool, "%s: missing input filename path", argv[0]);
PrintHelp();
return 1;
}
if (!IsValid(outputFilename))
{
LogError(LogCategory::Tool, "%s: missing output path", argv[0]);
PrintHelp();
return 1;
}
fileData = LoadFile(filename);
if (!IsValid(fileData))
{
LogError(LogCategory::Tool, "Invalid file: %s", CStr(filename));
return 1;
}
// Find the source language from filename
{
if (!sourceLangDetected)
{
if (IsValid(FindString(filename, WrapString("hlsl"))))
{
srcLanguage = SourceLanguage::HLSL;
}
static_assert(ToUnderlying(SourceLanguage::Count) == 1, "SourceLanguage must be one of: HLSL");
}
if (srcLanguage == SourceLanguage::Invalid)
{
LogError(LogCategory::Tool,
"%s: Invalid source language. Please either have a valid file extension or use -s/--src", argv[0]);
PrintHelp();
return 1;
}
}
// Find the destination format from output file name
{
if (!dstShaderFormatDetected)
{
if (IsValid(FindString(outputFilename, WrapString("dxil"))))
{
dstShaderFormat = ShaderFormat::DXIL;
}
static_assert(ToUnderlying(ShaderFormat::Count) == 1, "ShaderFormat must be one of: DXIL");
if (dstShaderFormat == ShaderFormat::Invalid)
{
LogError(LogCategory::Tool,
"%s: Invalid shader format. Please either have a valid file extension or use -d/--dest", argv[0]);
PrintHelp();
return 1;
}
}
}
// Find shader stage from filename
{
if (!shaderStageDetected)
{
// TODO Need a case insensitive FindString
if (IsValid(FindString(filename, WrapString("vert"))))
{
shaderStage = ShaderStage::Vertex;
}
else if (IsValid(FindString(filename, WrapString("frag"))))
{
shaderStage = ShaderStage::Fragment;
}
else if (IsValid(FindString(filename, WrapString("comp"))))
{
shaderStage = ShaderStage::Compute;
}
static_assert(ToUnderlying(ShaderFormat::Count) == 1, "ShaderStage must be one of: vert, frag, comp");
if (shaderStage == ShaderStage::Invalid)
{
LogError(LogCategory::Tool, "%s: Invalid shader stage. Please include it in the filename", argv[0]);
PrintHelp();
return 1;
}
}
}
outStream = IOFromFile(outputFilename, WrapString("w"));
if (!outStream)
{
LogError(LogCategory::Tool, "%s: Failed to open %s for writing", argv[0], CStr(outputFilename));
return 1;
}
switch (srcLanguage)
{
case SourceLanguage::HLSL:
{
HLSLShaderInfo hlslInfo = {};
hlslInfo.ByteCodeBuffer = fileData;
hlslInfo.EntryPoint = entryPoint;
hlslInfo.Name = filename;
hlslInfo.Stage = shaderStage;
switch (dstShaderFormat)
{
case ShaderFormat::DXIL:
{
ByteBuffer compiledBuffer = CompileDXIL(hlslInfo);
if (!IsValid(compiledBuffer))
{
LogError(LogCategory::Tool, "Failed to compile DXBC from HLSL. Error: %s", "Unknown");
}
else
{
IOWrite(outStream, compiledBuffer);
Free(compiledBuffer);
}
break;
}
case ShaderFormat::Invalid: Assert(false , "Shader format is invalid"); return 1;
}
break;
}
case SourceLanguage::Invalid: Assert(false , "Source Language is invalid"); return 1;
}
return 0;
}