forked from GPUOpen-LibrariesAndSDKs/RenderPipelineShaders
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathafx_shader_compiler.hpp
194 lines (158 loc) · 5.27 KB
/
afx_shader_compiler.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright (c) 2023 Advanced Micro Devices, Inc. All rights reserved.
//
// This file is part of the AMD Render Pipeline Shaders SDK which is
// released under the AMD INTERNAL EVALUATION LICENSE.
//
// See file LICENSE.txt for full license details.
#pragma once
#include <unknwn.h>
#include <dxcapi.h>
#include <vector>
#include <algorithm>
#include "afx_d3d_helper.hpp"
static DxcCreateInstanceProc s_DxcCreateInstance = nullptr;
static void InitDXC(bool isSpirvTarget)
{
if (s_DxcCreateInstance != nullptr)
{
return;
}
WCHAR exeDir[MAX_PATH];
// call below should throw an exception on failure.
GetExeDirPath(exeDir);
std::wstring dxcPath(exeDir);
dxcPath += (isSpirvTarget) ? L"spirv_dxc\\dxcompiler.dll" : L"dxcompiler.dll";
HMODULE hDxcDll = ::LoadLibraryW(dxcPath.c_str());
if (!hDxcDll)
{
throw "Failed to load dxcompiler.dll";
}
s_DxcCreateInstance = (DxcCreateInstanceProc)::GetProcAddress(hDxcDll, "DxcCreateInstance");
}
interface IncluderDxc : public IDxcIncludeHandler
{
IDxcLibrary* m_pLibrary;
public:
IncluderDxc(IDxcLibrary * pLibrary)
: m_pLibrary(pLibrary)
{
}
HRESULT STDMETHODCALLTYPE QueryInterface(const IID&, void**) override
{
return S_OK;
}
ULONG STDMETHODCALLTYPE AddRef() override
{
return 0;
}
ULONG STDMETHODCALLTYPE Release() override
{
return 0;
}
HRESULT STDMETHODCALLTYPE LoadSource(LPCWSTR pFilename, IDxcBlob** ppIncludeSource) override
{
IDxcBlobEncoding* pSource;
ThrowIfFailed(m_pLibrary->CreateBlobFromFile(pFilename, NULL, &pSource));
*ppIncludeSource = pSource;
return S_OK;
}
};
#if RPS_HAS_MAYBE_UNUSED
[[maybe_unused]]
#endif
static bool DxcCompile(const char* pSrcCode,
const WCHAR* pEntryPoint,
const WCHAR* pProfile,
const WCHAR* pParams,
const DxcDefine* defines,
uint32_t defineCount,
std::vector<uint8_t>& byteCode)
{
bool isSpirvTarget = false;
std::vector<std::wstring> args;
// splits params string into an array of strings
{
WCHAR params[1024];
wcscpy_s(params, pParams);
WCHAR* next_token;
WCHAR* token = wcstok_s(params, L" ", &next_token);
while (token != NULL)
{
args.push_back(token);
if (L"-spirv" == args.back())
{
isSpirvTarget = true;
}
token = wcstok_s(NULL, L" ", &next_token);
}
}
InitDXC(isSpirvTarget);
ComPtr<IDxcLibrary> pLibrary;
ThrowIfFailed(s_DxcCreateInstance(CLSID_DxcLibrary, IID_PPV_ARGS(&pLibrary)));
ComPtr<IDxcBlobEncoding> pSource;
ThrowIfFailed(pLibrary->CreateBlobWithEncodingFromPinned((LPBYTE)pSrcCode, (UINT32)strlen(pSrcCode), CP_UTF8, &pSource));
ComPtr<IDxcCompiler2> pCompiler;
ThrowIfFailed(s_DxcCreateInstance(CLSID_DxcCompiler, IID_PPV_ARGS(&pCompiler)));
IncluderDxc Includer(pLibrary.Get());
LPCWSTR pTargetProfile = L"";
if (isSpirvTarget)
{
args.push_back(L"-T");
args.push_back(pProfile);
}
else
{
pTargetProfile = pProfile;
}
std::vector<LPCWSTR> ppArgs(args.size());
std::transform(args.begin(), args.end(), ppArgs.begin(), [](auto& s) { return s.c_str(); });
ComPtr<IDxcOperationResult> pOpRes;
HRESULT res;
if (true)
{
ppArgs.push_back(L"-Zi");
ppArgs.push_back(L"-Qembed_debug");
ComPtr<IDxcBlob> pPDB;
LPWSTR pDebugBlobName[1024];
res = pCompiler->CompileWithDebug(pSource.Get(),
NULL,
pEntryPoint,
pTargetProfile,
ppArgs.data(),
(UINT32)ppArgs.size(),
defines,
defineCount,
&Includer,
&pOpRes,
pDebugBlobName,
pPDB.GetAddressOf());
}
else
{
res = pCompiler->Compile(
pSource.Get(), NULL, pEntryPoint, pTargetProfile, ppArgs.data(), (UINT32)ppArgs.size(), defines, defineCount, &Includer, &pOpRes);
}
ComPtr<IDxcBlob> pResult;
ComPtr<IDxcBlobEncoding> pError;
if (pOpRes != NULL)
{
pOpRes->GetResult(&pResult);
pOpRes->GetErrorBuffer(&pError);
}
if (pError)
{
ComPtr<IDxcBlobEncoding> pErrorUtf8;
pLibrary->GetBlobAsUtf8(pError.Get(), &pErrorUtf8);
if (pErrorUtf8 && pErrorUtf8->GetBufferSize() > 0)
{
fprintf(stderr, "%s", (const char*)pErrorUtf8->GetBufferPointer());
}
}
if (pResult != NULL && pResult->GetBufferSize() > 0)
{
byteCode.resize(pResult->GetBufferSize());
memcpy(byteCode.data(), pResult->GetBufferPointer(), pResult->GetBufferSize());
return true;
}
return false;
}