-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6ad80f6
commit e33ff92
Showing
14 changed files
with
3,322 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Oops, something went wrong.