Skip to content

Commit

Permalink
Use DXC for D3D12
Browse files Browse the repository at this point in the history
  • Loading branch information
RobDangerous committed Jun 23, 2024
1 parent 6ad80f6 commit e33ff92
Show file tree
Hide file tree
Showing 14 changed files with 3,322 additions and 2 deletions.
131 changes: 131 additions & 0 deletions Sources/backends/d3d12.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#include "d3d12.h"

#include "../errors.h"

#ifdef _WIN32

#include "../log.h"

#ifdef noreturn
#undef noreturn
#endif

#include <atlbase.h>
#include <dxcapi.h>

#endif

static const wchar_t *shader_string(shader_stage stage) {
switch (stage) {
case SHADER_STAGE_VERTEX:
return L"vs_6_0";
case SHADER_STAGE_FRAGMENT:
return L"ps_6_0";
/*case EShLangGeometry:
return L"gs_6_0";
case EShLangTessControl:
return L"hs_6_0";
case EShLangTessEvaluation:
return L"ds_6_0";
case EShLangCompute:
return L"cs_6_0";*/
default: {
debug_context context = {0};
error(context, "Unsupported shader stage/version combination");
return L"unsupported";
}
}
}

int compile_hlsl_to_d3d12(const char *source, uint8_t **output, size_t *outputlength, shader_stage stage, bool debug) {
#ifdef _WIN32
CComPtr<IDxcCompiler3> compiler;
DxcCreateInstance(CLSID_DxcCompiler, IID_PPV_ARGS(&compiler));

LPCWSTR compiler_args[] = {
// L"myshader.hlsl", // optional shader source file name for error reporting and for PIX shader source view
L"-E", L"main", // entry point
L"-T", shader_string(stage), // target
L"-Zi", // enable debug info
// L"-D", L"MYDEFINE=1", // a single define
// L"-Fo", L"myshader.bin", // optional. stored in the pdb.
// L"-Fd", L"myshader.pdb", // the file name of the pdb. This must either be supplied or the auto generated file name must be used
// L"-D", L"__XBOX_STRIP_DXIL", // strip DXIL
// L"-Qstrip_reflect", // strip reflection into a seperate blob
};

DxcBuffer source_buffer;
source_buffer.Ptr = source;
source_buffer.Size = strlen(source);
source_buffer.Encoding = DXC_CP_ACP; // assume BOM says UTF8 or UTF16 or this is ANSI text

CComPtr<IDxcResult> compiler_result;
compiler->Compile(&source_buffer, // source buffer
compiler_args, // Array of pointers to arguments
_countof(compiler_args), // Number of arguments
NULL, // user-provided interface to handle #include directives (optional)
IID_PPV_ARGS(&compiler_result) // Compiler output status, buffer, and errors
);

CComPtr<IDxcBlobUtf8> errors = nullptr;
compiler_result->GetOutput(DXC_OUT_ERRORS, IID_PPV_ARGS(&errors), nullptr);
// note that d3dcompiler would return null if no errors or warnings are present.
// IDxcCompiler3::Compile will always return an error buffer but it's length will be zero if there are no warnings or errors
if (errors != nullptr && errors->GetStringLength() != 0) {
kong_log(LOG_LEVEL_INFO, "Warnings and Errors:\n%s", errors->GetStringPointer());
}

HRESULT result;
compiler_result->GetStatus(&result);

if (result == S_OK) {
CComPtr<IDxcBlob> shader_buffer = nullptr;
CComPtr<IDxcBlobUtf16> shader_name = nullptr;
compiler_result->GetOutput(DXC_OUT_OBJECT, IID_PPV_ARGS(&shader_buffer), &shader_name);
if (shader_buffer == nullptr) {
return 1;
}
else {
*outputlength = shader_buffer->GetBufferSize();
*output = (uint8_t *)malloc(*outputlength);
memcpy(*output, shader_buffer->GetBufferPointer(), shader_buffer->GetBufferSize());
}

/*
//
// save pdb
//
CComPtr<IDxcBlob> pPDB = nullptr;
CComPtr<IDxcBlobUtf16> pPDBName = nullptr;
pResults->GetOutput(DXC_OUT_PDB, IID_PPV_ARGS(&pPDB), &pPDBName);
{
FILE* fp = NULL;
// note that if you do not specifiy -Fd a pdb name will be automatically generated. Use this filename to save the pdb so that PIX can find
it quickly _wfopen_s(&fp, pPDBName->GetStringPointer(), L"wb"); fwrite(pPDB->GetBufferPointer(), pPDB->GetBufferSize(), 1, fp); fclose(fp);
}
//
// print hash
//
CComPtr<IDxcBlob> pHash = nullptr;
pResults->GetOutput(DXC_OUT_SHADER_HASH, IID_PPV_ARGS(&pHash), nullptr);
if (pHash != nullptr)
{
wprintf(L"Hash: ");
DxcShaderHash* pHashBuf = (DxcShaderHash*)pHash->GetBufferPointer();
for (int i = 0; i < _countof(pHashBuf->HashDigest); i++)
wprintf(L"%x", pHashBuf->HashDigest[i]);
wprintf(L"\n");
}
*/

return 0;
}
else {
return 1;
}
#else
return 1;
#endif
}
17 changes: 17 additions & 0 deletions Sources/backends/d3d12.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "../shader_stage.h"

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

int compile_hlsl_to_d3d12(const char *source, uint8_t **output, size_t *outputlength, shader_stage stage, bool debug);

#ifdef __cplusplus
}
#endif
9 changes: 7 additions & 2 deletions Sources/backends/hlsl.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "../types.h"
#include "cstyle.h"
#include "d3d11.h"
#include "d3d12.h"
#include "d3d9.h"

#include <assert.h>
Expand Down Expand Up @@ -498,9 +499,11 @@ static void hlsl_export_vertex(char *directory, api_kind d3d, function *main) {
result = compile_hlsl_to_d3d9(hlsl, &output, &output_size, SHADER_STAGE_VERTEX, false);
break;
case API_DIRECT3D11:
case API_DIRECT3D12:
result = compile_hlsl_to_d3d11(hlsl, &output, &output_size, SHADER_STAGE_VERTEX, false);
break;
case API_DIRECT3D12:
result = compile_hlsl_to_d3d12(hlsl, &output, &output_size, SHADER_STAGE_VERTEX, false);
break;
default:
error(context, "Unsupported API for HLSL");
}
Expand Down Expand Up @@ -540,9 +543,11 @@ static void hlsl_export_fragment(char *directory, api_kind d3d, function *main)
result = compile_hlsl_to_d3d9(hlsl, &output, &output_size, SHADER_STAGE_FRAGMENT, false);
break;
case API_DIRECT3D11:
case API_DIRECT3D12:
result = compile_hlsl_to_d3d11(hlsl, &output, &output_size, SHADER_STAGE_FRAGMENT, false);
break;
case API_DIRECT3D12:
result = compile_hlsl_to_d3d12(hlsl, &output, &output_size, SHADER_STAGE_FRAGMENT, false);
break;
default:
error(context, "Unsupported API for HLSL");
}
Expand Down
8 changes: 8 additions & 0 deletions Sources/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#include <stdint.h>
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

#if (__STDC_VERSION__ >= 201112L)
#define noreturn _Noreturn
#else
Expand All @@ -26,3 +30,7 @@ void check_function(bool test, debug_context context, const char *message, ...);
void check_args(bool test, debug_context context, const char *message, va_list args);

// V_ASSERT_CONTRACT, assertMacro:check

#ifdef __cplusplus
}
#endif
43 changes: 43 additions & 0 deletions Sources/libs/dxc/LICENSE-LLVM.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
==============================================================================
LLVM Release License
==============================================================================
University of Illinois/NCSA
Open Source License

Copyright (c) 2003-2015 University of Illinois at Urbana-Champaign.
All rights reserved.

Developed by:

LLVM Team

University of Illinois at Urbana-Champaign

http://llvm.org

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal with
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimers.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimers in the
documentation and/or other materials provided with the distribution.

* Neither the names of the LLVM Team, University of Illinois at
Urbana-Champaign, nor the names of its contributors may be used to
endorse or promote products derived from this Software without specific
prior written permission.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE
SOFTWARE.
21 changes: 21 additions & 0 deletions Sources/libs/dxc/LICENSE-MIT.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Copyright (c) Microsoft Corporation.

MIT License

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Loading

0 comments on commit e33ff92

Please sign in to comment.