-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicCompute11.cpp
522 lines (434 loc) · 21.2 KB
/
BasicCompute11.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
//--------------------------------------------------------------------------------------
// File: BasicCompute11.cpp
//
// Demonstrates the basics to get DirectX 11 Compute Shader (aka DirectCompute) up and
// running by implementing Array A + Array B
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include <stdio.h>
#include <crtdbg.h>
#include <d3dcommon.h>
#include <d3d11.h>
#include <d3dcompiler.h>
#include <time.h>
#include <DirectXMath.h>
#if D3D_COMPILER_VERSION < 46
#include <d3dx11.h>
#endif
#ifndef SAFE_RELEASE
#define SAFE_RELEASE(p) { if (p) { (p)->Release(); (p)=nullptr; } }
#endif
// The number of elements in a buffer to be tested
const UINT NUM_ELEMENTS = 1024;
#if defined(_MSC_VER) && (_MSC_VER<1610) && !defined(_In_reads_)
#define _Outptr_
#define _Outptr_opt_
#define _In_reads_(exp)
#define _In_reads_opt_(exp)
#define _Out_writes_(exp)
#endif
#ifndef _Use_decl_annotations_
#define _Use_decl_annotations_
#endif
//--------------------------------------------------------------------------------------
// Forward declarations
//--------------------------------------------------------------------------------------
HRESULT CreateComputeDevice( _Outptr_ ID3D11Device** ppDeviceOut, _Outptr_ ID3D11DeviceContext** ppContextOut, _In_ bool bForceRef );
HRESULT CreateComputeShader( _In_z_ LPCWSTR pSrcFile, _In_z_ LPCSTR pFunctionName,
_In_ ID3D11Device* pDevice, _Outptr_ ID3D11ComputeShader** ppShaderOut );
HRESULT CreateStructuredBuffer( _In_ ID3D11Device* pDevice, _In_ UINT uElementSize, _In_ UINT uCount,
_In_reads_(uElementSize*uCount) void* pInitData,
_Outptr_ ID3D11Buffer** ppBufOut );
HRESULT CreateRawBuffer( _In_ ID3D11Device* pDevice, _In_ UINT uSize, _In_reads_(uSize) void* pInitData, _Outptr_ ID3D11Buffer** ppBufOut );
HRESULT CreateBufferSRV( _In_ ID3D11Device* pDevice, _In_ ID3D11Buffer* pBuffer, _Outptr_ ID3D11ShaderResourceView** ppSRVOut );
HRESULT CreateBufferUAV( _In_ ID3D11Device* pDevice, _In_ ID3D11Buffer* pBuffer, _Outptr_ ID3D11UnorderedAccessView** pUAVOut );
ID3D11Buffer* CreateAndCopyToDebugBuf( _In_ ID3D11Device* pDevice, _In_ ID3D11DeviceContext* pd3dImmediateContext, _In_ ID3D11Buffer* pBuffer );
void RunComputeShader( _In_ ID3D11DeviceContext* pd3dImmediateContext,
_In_ ID3D11ComputeShader* pComputeShader,
_In_ UINT nNumViews, _In_reads_(nNumViews) ID3D11ShaderResourceView** pShaderResourceViews,
_In_opt_ ID3D11Buffer* pCBCS, _In_reads_opt_(dwNumDataBytes) void* pCSData, _In_ DWORD dwNumDataBytes,
_In_ ID3D11UnorderedAccessView* pUnorderedAccessView,
_In_ UINT X, _In_ UINT Y, _In_ UINT Z );
HRESULT FindDXSDKShaderFileCch( _Out_writes_(cchDest) WCHAR* strDestPath,
_In_ int cchDest,
_In_z_ LPCWSTR strFilename );
//--------------------------------------------------------------------------------------
// Global variables
//--------------------------------------------------------------------------------------
ID3D11Device* g_pDevice = nullptr;
ID3D11DeviceContext* g_pContext = nullptr;
ID3D11ComputeShader* g_pCS = nullptr;
ID3D11Buffer* g_pBuf0 = nullptr;
ID3D11Buffer* g_pBuf1 = nullptr;
ID3D11Buffer* g_pBufResult = nullptr;
ID3D11ShaderResourceView* g_pBuf0SRV = nullptr;
ID3D11ShaderResourceView* g_pBuf1SRV = nullptr;
ID3D11UnorderedAccessView* g_pBufResultUAV = nullptr;
clock_t tStart = 0;
#define NUMBER_MATRIX 10
struct BufType
{
DirectX::XMFLOAT4X4 f[NUMBER_MATRIX];
};
BufType g_vBuf0[NUM_ELEMENTS];
BufType g_vBuf1[NUM_ELEMENTS];
//--------------------------------------------------------------------------------------
// Entry point to the program
//--------------------------------------------------------------------------------------
int __cdecl main()
{
printf( "Creating device..." );
if ( FAILED( CreateComputeDevice( &g_pDevice, &g_pContext, false ) ) )
return 1;
printf( "done\n" );
printf( "Creating Compute Shader..." );
if ( FAILED( CreateComputeShader( L"BasicCompute11.hlsl", "CSMain", g_pDevice, &g_pCS ) ) )
return 1;
printf( "done\n" );
printf( "Creating buffers and filling them with initial data..." );
for ( int i = 0; i < NUM_ELEMENTS; ++i )
{
for (int j = 0; j < NUMBER_MATRIX; ++j)
{
g_vBuf0[i].f[j] = DirectX::XMFLOAT4X4();
g_vBuf1[i].f[j] = DirectX::XMFLOAT4X4();
for (int m = 0; m < 4; m++) {
for (int n = 0; n < 4; n++) {
g_vBuf0[i].f[j].m[m][n] = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
g_vBuf1[i].f[j].m[m][n] = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
}
}
}
}
CreateStructuredBuffer( g_pDevice, sizeof(BufType), NUM_ELEMENTS, &g_vBuf0[0], &g_pBuf0 );
CreateStructuredBuffer( g_pDevice, sizeof(BufType), NUM_ELEMENTS, &g_vBuf1[0], &g_pBuf1 );
CreateStructuredBuffer( g_pDevice, sizeof(BufType), NUM_ELEMENTS, nullptr, &g_pBufResult );
printf( "done\n" );
printf( "Creating buffer views..." );
CreateBufferSRV( g_pDevice, g_pBuf0, &g_pBuf0SRV );
CreateBufferSRV( g_pDevice, g_pBuf1, &g_pBuf1SRV );
CreateBufferUAV( g_pDevice, g_pBufResult, &g_pBufResultUAV );
printf( "done\n" );
printf( "Running Compute Shader..." );
tStart = clock();
ID3D11ShaderResourceView* aRViews[2] = { g_pBuf0SRV, g_pBuf1SRV };
RunComputeShader( g_pContext, g_pCS, 2, aRViews, nullptr, nullptr, 0, g_pBufResultUAV, NUM_ELEMENTS, 1, 1 );
printf( "done\n" );
// Read back the result from GPU, verify its correctness against result computed by CPU
{
ID3D11Buffer* debugbuf = CreateAndCopyToDebugBuf( g_pDevice, g_pContext, g_pBufResult );
D3D11_MAPPED_SUBRESOURCE MappedResource;
BufType *p;
g_pContext->Map( debugbuf, 0, D3D11_MAP_READ, 0, &MappedResource );
// Set a break point here and put down the expression "p, 1024" in your watch window to see what has been written out by our CS
// This is also a common trick to debug CS programs.
p = (BufType*)MappedResource.pData;
p[0].f->m[0][0] = 0.0;
g_pContext->Unmap( debugbuf, 0 );
SAFE_RELEASE( debugbuf );
}
printf("Time taken: %.2fms\n", ((double)(clock() - tStart)) * 1000 / CLOCKS_PER_SEC);
printf( "Cleaning up...\n" );
SAFE_RELEASE( g_pBuf0SRV );
SAFE_RELEASE( g_pBuf1SRV );
SAFE_RELEASE( g_pBufResultUAV );
SAFE_RELEASE( g_pBuf0 );
SAFE_RELEASE( g_pBuf1 );
SAFE_RELEASE( g_pBufResult );
SAFE_RELEASE( g_pCS );
SAFE_RELEASE( g_pContext );
SAFE_RELEASE( g_pDevice );
printf("After release resouces, time taken: %.2fms\n", ((double)(clock() - tStart)) * 1000 / CLOCKS_PER_SEC);
return 0;
}
//--------------------------------------------------------------------------------------
// Create the D3D device and device context suitable for running Compute Shaders(CS)
//--------------------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT CreateComputeDevice( ID3D11Device** ppDeviceOut, ID3D11DeviceContext** ppContextOut, bool bForceRef )
{
*ppDeviceOut = nullptr;
*ppContextOut = nullptr;
HRESULT hr = S_OK;
UINT uCreationFlags = D3D11_CREATE_DEVICE_SINGLETHREADED;
#ifdef _DEBUG
uCreationFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
D3D_FEATURE_LEVEL flOut;
static const D3D_FEATURE_LEVEL flvl[] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, D3D_FEATURE_LEVEL_10_0 };
bool bNeedRefDevice = false;
if ( !bForceRef )
{
hr = D3D11CreateDevice( nullptr, // Use default graphics card
D3D_DRIVER_TYPE_HARDWARE, // Try to create a hardware accelerated device
nullptr, // Do not use external software rasterizer module
uCreationFlags, // Device creation flags
flvl,
sizeof(flvl) / sizeof(D3D_FEATURE_LEVEL),
D3D11_SDK_VERSION, // SDK version
ppDeviceOut, // Device out
&flOut, // Actual feature level created
ppContextOut ); // Context out
if ( SUCCEEDED( hr ) )
{
// A hardware accelerated device has been created, so check for Compute Shader support
// If we have a device >= D3D_FEATURE_LEVEL_11_0 created, full CS5.0 support is guaranteed, no need for further checks
if ( flOut < D3D_FEATURE_LEVEL_11_0 )
{
// Otherwise, we need further check whether this device support CS4.x (Compute on 10)
D3D11_FEATURE_DATA_D3D10_X_HARDWARE_OPTIONS hwopts;
(*ppDeviceOut)->CheckFeatureSupport( D3D11_FEATURE_D3D10_X_HARDWARE_OPTIONS, &hwopts, sizeof(hwopts) );
if ( !hwopts.ComputeShaders_Plus_RawAndStructuredBuffers_Via_Shader_4_x )
{
bNeedRefDevice = true;
printf( "No hardware Compute Shader capable device found, trying to create ref device.\n" );
}
}
}
}
if ( bForceRef || FAILED(hr) || bNeedRefDevice )
{
// Either because of failure on creating a hardware device or hardware lacking CS capability, we create a ref device here
SAFE_RELEASE( *ppDeviceOut );
SAFE_RELEASE( *ppContextOut );
hr = D3D11CreateDevice( nullptr, // Use default graphics card
D3D_DRIVER_TYPE_REFERENCE, // Try to create a hardware accelerated device
nullptr, // Do not use external software rasterizer module
uCreationFlags, // Device creation flags
flvl,
sizeof(flvl) / sizeof(D3D_FEATURE_LEVEL),
D3D11_SDK_VERSION, // SDK version
ppDeviceOut, // Device out
&flOut, // Actual feature level created
ppContextOut ); // Context out
if ( FAILED(hr) )
{
printf( "Reference rasterizer device create failure\n" );
return hr;
}
}
return hr;
}
//--------------------------------------------------------------------------------------
// Compile and create the CS
//--------------------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT CreateComputeShader( LPCWSTR pSrcFile, LPCSTR pFunctionName,
ID3D11Device* pDevice, ID3D11ComputeShader** ppShaderOut )
{
if ( !pDevice || !ppShaderOut )
return E_INVALIDARG;
// Finds the correct path for the shader file.
// This is only required for this sample to be run correctly from within the Sample Browser,
// in your own projects, these lines could be removed safely
WCHAR str[MAX_PATH];
HRESULT hr = FindDXSDKShaderFileCch( str, MAX_PATH, pSrcFile );
if ( FAILED(hr) )
return hr;
DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
// We generally prefer to use the higher CS shader profile when possible as CS 5.0 is better performance on 11-class hardware
LPCSTR pProfile = ( pDevice->GetFeatureLevel() >= D3D_FEATURE_LEVEL_11_0 ) ? "cs_5_0" : "cs_4_0";
ID3DBlob* pErrorBlob = nullptr;
ID3DBlob* pBlob = nullptr;
hr = D3DCompileFromFile( str, nullptr, D3D_COMPILE_STANDARD_FILE_INCLUDE, pFunctionName, pProfile,
dwShaderFlags, 0, &pBlob, &pErrorBlob );
if ( FAILED(hr) )
{
if ( pErrorBlob )
OutputDebugStringA( (char*)pErrorBlob->GetBufferPointer() );
SAFE_RELEASE( pErrorBlob );
SAFE_RELEASE( pBlob );
return hr;
}
hr = pDevice->CreateComputeShader( pBlob->GetBufferPointer(), pBlob->GetBufferSize(), nullptr, ppShaderOut );
SAFE_RELEASE( pErrorBlob );
SAFE_RELEASE( pBlob );
return hr;
}
//--------------------------------------------------------------------------------------
// Create Structured Buffer
//--------------------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT CreateStructuredBuffer( ID3D11Device* pDevice, UINT uElementSize, UINT uCount, void* pInitData, ID3D11Buffer** ppBufOut )
{
*ppBufOut = nullptr;
D3D11_BUFFER_DESC desc;
ZeroMemory( &desc, sizeof(desc) );
desc.BindFlags = D3D11_BIND_UNORDERED_ACCESS | D3D11_BIND_SHADER_RESOURCE;
desc.ByteWidth = uElementSize * uCount;
desc.MiscFlags = D3D11_RESOURCE_MISC_BUFFER_STRUCTURED;
desc.StructureByteStride = uElementSize;
if ( pInitData )
{
D3D11_SUBRESOURCE_DATA InitData;
InitData.pSysMem = pInitData;
return pDevice->CreateBuffer( &desc, &InitData, ppBufOut );
} else
return pDevice->CreateBuffer( &desc, nullptr, ppBufOut );
}
//--------------------------------------------------------------------------------------
// Create Shader Resource View for Structured or Raw Buffers
//--------------------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT CreateBufferSRV( ID3D11Device* pDevice, ID3D11Buffer* pBuffer, ID3D11ShaderResourceView** ppSRVOut )
{
D3D11_BUFFER_DESC descBuf;
ZeroMemory( &descBuf, sizeof(descBuf) );
pBuffer->GetDesc( &descBuf );
D3D11_SHADER_RESOURCE_VIEW_DESC desc;
ZeroMemory( &desc, sizeof(desc) );
desc.ViewDimension = D3D11_SRV_DIMENSION_BUFFEREX;
desc.BufferEx.FirstElement = 0;
if ( descBuf.MiscFlags & D3D11_RESOURCE_MISC_BUFFER_ALLOW_RAW_VIEWS )
{
// This is a Raw Buffer
desc.Format = DXGI_FORMAT_R32_TYPELESS;
desc.BufferEx.Flags = D3D11_BUFFEREX_SRV_FLAG_RAW;
desc.BufferEx.NumElements = descBuf.ByteWidth / 4;
} else
if ( descBuf.MiscFlags & D3D11_RESOURCE_MISC_BUFFER_STRUCTURED )
{
// This is a Structured Buffer
desc.Format = DXGI_FORMAT_UNKNOWN;
desc.BufferEx.NumElements = descBuf.ByteWidth / descBuf.StructureByteStride;
} else
{
return E_INVALIDARG;
}
return pDevice->CreateShaderResourceView( pBuffer, &desc, ppSRVOut );
}
//--------------------------------------------------------------------------------------
// Create Unordered Access View for Structured or Raw Buffers
//--------------------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT CreateBufferUAV( ID3D11Device* pDevice, ID3D11Buffer* pBuffer, ID3D11UnorderedAccessView** ppUAVOut )
{
D3D11_BUFFER_DESC descBuf;
ZeroMemory( &descBuf, sizeof(descBuf) );
pBuffer->GetDesc( &descBuf );
D3D11_UNORDERED_ACCESS_VIEW_DESC desc;
ZeroMemory( &desc, sizeof(desc) );
desc.ViewDimension = D3D11_UAV_DIMENSION_BUFFER;
desc.Buffer.FirstElement = 0;
if ( descBuf.MiscFlags & D3D11_RESOURCE_MISC_BUFFER_ALLOW_RAW_VIEWS )
{
// This is a Raw Buffer
desc.Format = DXGI_FORMAT_R32_TYPELESS; // Format must be DXGI_FORMAT_R32_TYPELESS, when creating Raw Unordered Access View
desc.Buffer.Flags = D3D11_BUFFER_UAV_FLAG_RAW;
desc.Buffer.NumElements = descBuf.ByteWidth / 4;
} else
if ( descBuf.MiscFlags & D3D11_RESOURCE_MISC_BUFFER_STRUCTURED )
{
// This is a Structured Buffer
desc.Format = DXGI_FORMAT_UNKNOWN; // Format must be must be DXGI_FORMAT_UNKNOWN, when creating a View of a Structured Buffer
desc.Buffer.NumElements = descBuf.ByteWidth / descBuf.StructureByteStride;
} else
{
return E_INVALIDARG;
}
return pDevice->CreateUnorderedAccessView( pBuffer, &desc, ppUAVOut );
}
//--------------------------------------------------------------------------------------
// Create a CPU accessible buffer and download the content of a GPU buffer into it
// This function is very useful for debugging CS programs
//--------------------------------------------------------------------------------------
_Use_decl_annotations_
ID3D11Buffer* CreateAndCopyToDebugBuf( ID3D11Device* pDevice, ID3D11DeviceContext* pd3dImmediateContext, ID3D11Buffer* pBuffer )
{
ID3D11Buffer* debugbuf = nullptr;
D3D11_BUFFER_DESC desc;
ZeroMemory( &desc, sizeof(desc) );
pBuffer->GetDesc( &desc );
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
desc.Usage = D3D11_USAGE_STAGING;
desc.BindFlags = 0;
desc.MiscFlags = 0;
if ( SUCCEEDED(pDevice->CreateBuffer(&desc, nullptr, &debugbuf)) )
{
#if defined(_DEBUG) || defined(PROFILE)
debugbuf->SetPrivateData( WKPDID_D3DDebugObjectName, sizeof( "Debug" ) - 1, "Debug" );
#endif
pd3dImmediateContext->CopyResource( debugbuf, pBuffer );
}
return debugbuf;
}
//--------------------------------------------------------------------------------------
// Run CS
//--------------------------------------------------------------------------------------
_Use_decl_annotations_
void RunComputeShader( ID3D11DeviceContext* pd3dImmediateContext,
ID3D11ComputeShader* pComputeShader,
UINT nNumViews, ID3D11ShaderResourceView** pShaderResourceViews,
ID3D11Buffer* pCBCS, void* pCSData, DWORD dwNumDataBytes,
ID3D11UnorderedAccessView* pUnorderedAccessView,
UINT X, UINT Y, UINT Z )
{
pd3dImmediateContext->CSSetShader( pComputeShader, nullptr, 0 );
pd3dImmediateContext->CSSetShaderResources( 0, nNumViews, pShaderResourceViews );
pd3dImmediateContext->CSSetUnorderedAccessViews( 0, 1, &pUnorderedAccessView, nullptr );
if ( pCBCS && pCSData )
{
D3D11_MAPPED_SUBRESOURCE MappedResource;
pd3dImmediateContext->Map( pCBCS, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource );
memcpy( MappedResource.pData, pCSData, dwNumDataBytes );
pd3dImmediateContext->Unmap( pCBCS, 0 );
ID3D11Buffer* ppCB[1] = { pCBCS };
pd3dImmediateContext->CSSetConstantBuffers( 0, 1, ppCB );
}
pd3dImmediateContext->Dispatch( X / 256, Y, Z );
pd3dImmediateContext->CSSetShader( nullptr, nullptr, 0 );
ID3D11UnorderedAccessView* ppUAViewnullptr[1] = { nullptr };
pd3dImmediateContext->CSSetUnorderedAccessViews( 0, 1, ppUAViewnullptr, nullptr );
ID3D11ShaderResourceView* ppSRVnullptr[2] = { nullptr, nullptr };
pd3dImmediateContext->CSSetShaderResources( 0, 2, ppSRVnullptr );
ID3D11Buffer* ppCBnullptr[1] = { nullptr };
pd3dImmediateContext->CSSetConstantBuffers( 0, 1, ppCBnullptr );
}
//--------------------------------------------------------------------------------------
// Tries to find the location of the shader file
// This is a trimmed down version of DXUTFindDXSDKMediaFileCch.
//--------------------------------------------------------------------------------------
_Use_decl_annotations_
HRESULT FindDXSDKShaderFileCch( WCHAR* strDestPath,
int cchDest,
LPCWSTR strFilename )
{
if( !strFilename || strFilename[0] == 0 || !strDestPath || cchDest < 10 )
return E_INVALIDARG;
// Get the exe name, and exe path
WCHAR strExePath[MAX_PATH] =
{
0
};
WCHAR strExeName[MAX_PATH] =
{
0
};
WCHAR* strLastSlash = nullptr;
GetModuleFileName( nullptr, strExePath, MAX_PATH );
strExePath[MAX_PATH - 1] = 0;
strLastSlash = wcsrchr( strExePath, TEXT( '\\' ) );
if( strLastSlash )
{
wcscpy_s( strExeName, MAX_PATH, &strLastSlash[1] );
// Chop the exe name from the exe path
*strLastSlash = 0;
// Chop the .exe from the exe name
strLastSlash = wcsrchr( strExeName, TEXT( '.' ) );
if( strLastSlash )
*strLastSlash = 0;
}
// Search in directories:
// .\
// %EXE_DIR%\..\..\%EXE_NAME%
wcscpy_s( strDestPath, cchDest, strFilename );
if( GetFileAttributes( strDestPath ) != 0xFFFFFFFF )
return S_OK;
swprintf_s( strDestPath, cchDest, L"%s\\..\\..\\%s\\%s", strExePath, strExeName, strFilename );
if( GetFileAttributes( strDestPath ) != 0xFFFFFFFF )
return S_OK;
// On failure, return the file as the path but also return an error code
wcscpy_s( strDestPath, cchDest, strFilename );
return E_FAIL;
}