forked from microsoft/FX11
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EffectAPI.cpp
335 lines (274 loc) · 10.5 KB
/
EffectAPI.cpp
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//--------------------------------------------------------------------------------------
// File: EffectAPI.cpp
//
// Effect API entry point
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// http://go.microsoft.com/fwlink/p/?LinkId=271568
//--------------------------------------------------------------------------------------
#include "pchfx.h"
#include <memory>
using namespace D3DX11Effects;
//-------------------------------------------------------------------------------------
struct handle_closer { void operator()(HANDLE h) { if (h) CloseHandle(h); } };
typedef public std::unique_ptr<void, handle_closer> ScopedHandle;
inline HANDLE safe_handle( HANDLE h ) { return (h == INVALID_HANDLE_VALUE) ? 0 : h; }
//-------------------------------------------------------------------------------------
static HRESULT LoadBinaryFromFile( _In_z_ LPCWSTR pFileName, _Inout_ std::unique_ptr<uint8_t[]>& data, _Out_ uint32_t& size )
{
// open the file
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
ScopedHandle hFile( safe_handle( CreateFile2( pFileName,
GENERIC_READ,
FILE_SHARE_READ,
OPEN_EXISTING,
nullptr ) ) );
#else
ScopedHandle hFile( safe_handle( CreateFileW( pFileName,
GENERIC_READ,
FILE_SHARE_READ,
nullptr,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
nullptr ) ) );
#endif
if ( !hFile )
{
return HRESULT_FROM_WIN32( GetLastError() );
}
// Get the file size
LARGE_INTEGER FileSize = { 0 };
#if (_WIN32_WINNT >= _WIN32_WINNT_VISTA)
FILE_STANDARD_INFO fileInfo;
if ( !GetFileInformationByHandleEx( hFile.get(), FileStandardInfo, &fileInfo, sizeof(fileInfo) ) )
{
return HRESULT_FROM_WIN32( GetLastError() );
}
FileSize = fileInfo.EndOfFile;
#else
GetFileSizeEx( hFile.get(), &FileSize );
#endif
// File is too big for 32-bit allocation or contains no data, so reject read
if ( !FileSize.LowPart || FileSize.HighPart > 0)
{
return E_FAIL;
}
// create enough space for the file data
data.reset( new uint8_t[ FileSize.LowPart ] );
if (!data)
{
return E_OUTOFMEMORY;
}
// read the data in
DWORD BytesRead = 0;
if (!ReadFile( hFile.get(),
data.get(),
FileSize.LowPart,
&BytesRead,
nullptr
))
{
return HRESULT_FROM_WIN32( GetLastError() );
}
if (BytesRead < FileSize.LowPart)
{
return E_FAIL;
}
size = BytesRead;
return S_OK;
}
//--------------------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT WINAPI D3DX11CreateEffectFromMemory(LPCVOID pData, SIZE_T DataLength, UINT FXFlags,
ID3D11Device *pDevice, ID3DX11Effect **ppEffect, LPCSTR srcName )
{
if ( !pData || !DataLength || !pDevice || !ppEffect )
return E_INVALIDARG;
#ifdef _M_X64
if ( DataLength > 0xFFFFFFFF )
return E_INVALIDARG;
#endif
HRESULT hr = S_OK;
// Note that pData must point to a compiled effect, not HLSL
VN( *ppEffect = new CEffect( FXFlags & D3DX11_EFFECT_RUNTIME_VALID_FLAGS) );
VH( ((CEffect*)(*ppEffect))->LoadEffect(pData, static_cast<uint32_t>(DataLength) ) );
VH( ((CEffect*)(*ppEffect))->BindToDevice(pDevice, (srcName) ? srcName : "D3DX11Effect" ) );
lExit:
if (FAILED(hr))
{
SAFE_RELEASE(*ppEffect);
}
return hr;
}
//--------------------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT WINAPI D3DX11CreateEffectFromFile( LPCWSTR pFileName, UINT FXFlags, ID3D11Device *pDevice, ID3DX11Effect **ppEffect )
{
if ( !pFileName || !pDevice || !ppEffect )
return E_INVALIDARG;
std::unique_ptr<uint8_t[]> fileData;
uint32_t size;
HRESULT hr = LoadBinaryFromFile( pFileName, fileData, size );
if ( FAILED(hr) )
return hr;
hr = S_OK;
// Note that pData must point to a compiled effect, not HLSL
VN( *ppEffect = new CEffect( FXFlags & D3DX11_EFFECT_RUNTIME_VALID_FLAGS) );
VH( ((CEffect*)(*ppEffect))->LoadEffect( fileData.get(), size ) );
// Create debug object name from input filename
CHAR strFileA[MAX_PATH];
int result = WideCharToMultiByte( CP_ACP, WC_NO_BEST_FIT_CHARS, pFileName, -1, strFileA, MAX_PATH, nullptr, FALSE );
if ( !result )
{
DPF(0, "Failed to load effect file due to WC to MB conversion failure: %ls", pFileName);
hr = E_FAIL;
goto lExit;
}
const CHAR* pstrName = strrchr( strFileA, '\\' );
if (!pstrName)
{
pstrName = strFileA;
}
else
{
pstrName++;
}
VH( ((CEffect*)(*ppEffect))->BindToDevice(pDevice, pstrName) );
lExit:
if (FAILED(hr))
{
SAFE_RELEASE(*ppEffect);
}
return hr;
}
//--------------------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT D3DX11CompileEffectFromMemory( LPCVOID pData, SIZE_T DataLength, LPCSTR srcName,
const D3D_SHADER_MACRO *pDefines, ID3DInclude *pInclude, UINT HLSLFlags, UINT FXFlags,
ID3D11Device *pDevice, ID3DX11Effect **ppEffect, ID3DBlob **ppErrors )
{
if ( !pData || !DataLength || !pDevice || !ppEffect )
return E_INVALIDARG;
if ( FXFlags & D3DCOMPILE_EFFECT_CHILD_EFFECT )
{
DPF(0, "Effect pools (i.e. D3DCOMPILE_EFFECT_CHILD_EFFECT) not supported" );
return E_NOTIMPL;
}
ID3DBlob *blob = nullptr;
HRESULT hr = D3DCompile( pData, DataLength, srcName, pDefines, pInclude, "", "fx_5_0", HLSLFlags, FXFlags, &blob, ppErrors );
if ( FAILED(hr) )
{
DPF(0, "D3DCompile of fx_5_0 profile failed: %08X", hr );
return hr;
}
hr = S_OK;
VN( *ppEffect = new CEffect( FXFlags & D3DX11_EFFECT_RUNTIME_VALID_FLAGS ) );
VH( ((CEffect*)(*ppEffect))->LoadEffect(blob->GetBufferPointer(), static_cast<uint32_t>( blob->GetBufferSize() ) ) );
SAFE_RELEASE( blob );
VH( ((CEffect*)(*ppEffect))->BindToDevice(pDevice, (srcName) ? srcName : "D3DX11Effect" ) );
lExit:
if (FAILED(hr))
{
SAFE_RELEASE(*ppEffect);
}
return hr;
}
//--------------------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT D3DX11CompileEffectFromFile( LPCWSTR pFileName,
const D3D_SHADER_MACRO *pDefines, ID3DInclude *pInclude, UINT HLSLFlags, UINT FXFlags,
ID3D11Device *pDevice, ID3DX11Effect **ppEffect, ID3DBlob **ppErrors )
{
if ( !pFileName || !pDevice || !ppEffect )
return E_INVALIDARG;
if ( FXFlags & D3DCOMPILE_EFFECT_CHILD_EFFECT )
{
DPF(0, "Effect pools (i.e. D3DCOMPILE_EFFECT_CHILD_EFFECT) not supported" );
return E_NOTIMPL;
}
ID3DBlob *blob = nullptr;
#if (D3D_COMPILER_VERSION >= 46) && ( !defined(WINAPI_FAMILY) || ( (WINAPI_FAMILY != WINAPI_FAMILY_APP) && (WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP) ) )
HRESULT hr = D3DCompileFromFile( pFileName, pDefines, pInclude, "", "fx_5_0", HLSLFlags, FXFlags, &blob, ppErrors );
if ( FAILED(hr) )
{
DPF(0, "D3DCompileFromFile of fx_5_0 profile failed %08X: %ls", hr, pFileName );
return hr;
}
#else // D3D_COMPILER_VERSION < 46
std::unique_ptr<uint8_t[]> fileData;
uint32_t size;
HRESULT hr = LoadBinaryFromFile( pFileName, fileData, size );
if ( FAILED(hr) )
{
DPF(0, "Failed to load effect file %08X: %ls", hr, pFileName);
return hr;
}
// Create debug object name from input filename
CHAR strFileA[MAX_PATH];
int result = WideCharToMultiByte( CP_ACP, WC_NO_BEST_FIT_CHARS, pFileName, -1, strFileA, MAX_PATH, nullptr, FALSE );
if ( !result )
{
DPF(0, "Failed to load effect file due to WC to MB conversion failure: %ls", pFileName);
return E_FAIL;
}
const CHAR* pstrName = strrchr( strFileA, '\\' );
if (!pstrName)
{
pstrName = strFileA;
}
else
{
pstrName++;
}
hr = D3DCompile( fileData.get(), size, pstrName, pDefines, pInclude, "", "fx_5_0", HLSLFlags, FXFlags, &blob, ppErrors );
if ( FAILED(hr) )
{
DPF(0, "D3DCompile of fx_5_0 profile failed: %08X", hr );
return hr;
}
#endif // D3D_COMPILER_VERSION
#ifdef _M_X64
if ( blob->GetBufferSize() > 0xFFFFFFFF )
{
SAFE_RELEASE( blob );
return E_FAIL;
}
#endif // _M_X64
hr = S_OK;
VN( *ppEffect = new CEffect( FXFlags & D3DX11_EFFECT_RUNTIME_VALID_FLAGS ) );
VH( ((CEffect*)(*ppEffect))->LoadEffect(blob->GetBufferPointer(), static_cast<uint32_t>( blob->GetBufferSize() ) ) );
SAFE_RELEASE( blob );
#if (D3D_COMPILER_VERSION >= 46) && ( !defined(WINAPI_FAMILY) || ( (WINAPI_FAMILY != WINAPI_FAMILY_APP) && (WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP) ) )
// Create debug object name from input filename
CHAR strFileA[MAX_PATH];
int result = WideCharToMultiByte( CP_ACP, WC_NO_BEST_FIT_CHARS, pFileName, -1, strFileA, MAX_PATH, nullptr, FALSE );
if ( !result )
{
DPF(0, "Failed to load effect file due to WC to MB conversion failure: %ls", pFileName);
hr = E_FAIL;
goto lExit;
}
const CHAR* pstrName = strrchr( strFileA, '\\' );
if (!pstrName)
{
pstrName = strFileA;
}
else
{
pstrName++;
}
#endif
VH( ((CEffect*)(*ppEffect))->BindToDevice(pDevice, pstrName) );
lExit:
if (FAILED(hr))
{
SAFE_RELEASE(*ppEffect);
}
return hr;
}