Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.63 KB | None | 0 0
  1. Skip to content
  2.  
  3. Search or jump to…
  4.  
  5. Pull requests
  6. Issues
  7. Marketplace
  8. Explore
  9.  
  10. @ergothvs
  11. 2,276
  12. 12,739 27,404 EpicGames/UnrealEngine Private
  13. Code Pull requests 695 Projects 0 Security Insights
  14. UnrealEngine/Engine/Source/Runtime/Windows/D3D11RHI/Private/D3D11Util.cpp
  15. Jonas Meyer RHI Thread for DirectX11(Change by Intel)
  16. 60cbab8 on Feb 19
  17. @ben-marsh @RCalocaO @UnrealBot @nick-penwarden @GilGribb @msieluzycki-pitbull @MarcusWassmerEPIC @andrewgrant @robertmanuszewski @mgriffinpitbull @MartinMittring @MarcAudy @KeefJudge @jbestimt @PaulEremeeff @daniwrig @ChrisBunner
  18. 545 lines (475 sloc) 16.4 KB
  19.  
  20. // Copyright 1998-2019 Epic Games, Inc. All Rights Reserved.
  21.  
  22. /*=============================================================================
  23. D3D11Util.h: D3D RHI utility implementation.
  24. =============================================================================*/
  25.  
  26. #include "D3D11RHIPrivate.h"
  27. #include "EngineModule.h"
  28. #include "RendererInterface.h"
  29.  
  30. #define D3DERR(x) case x: ErrorCodeText = TEXT(#x); break;
  31. #define LOCTEXT_NAMESPACE "Developer.MessageLog"
  32.  
  33. #ifndef _FACD3D
  34. #define _FACD3D 0x876
  35. #endif //_FACD3D
  36. #ifndef MAKE_D3DHRESULT
  37. #define _FACD3D 0x876
  38. #define MAKE_D3DHRESULT( code ) MAKE_HRESULT( 1, _FACD3D, code )
  39. #endif //MAKE_D3DHRESULT
  40.  
  41. #if WITH_D3DX_LIBS
  42. #ifndef D3DERR_INVALIDCALL
  43. #define D3DERR_INVALIDCALL MAKE_D3DHRESULT(2156)
  44. #endif//D3DERR_INVALIDCALL
  45. #ifndef D3DERR_WASSTILLDRAWING
  46. #define D3DERR_WASSTILLDRAWING MAKE_D3DHRESULT(540)
  47. #endif//D3DERR_WASSTILLDRAWING
  48. #endif
  49.  
  50. static FString GetD3D11DeviceHungErrorString(HRESULT ErrorCode)
  51. {
  52. FString ErrorCodeText;
  53.  
  54. switch(ErrorCode)
  55. {
  56. D3DERR(DXGI_ERROR_DEVICE_HUNG)
  57. D3DERR(DXGI_ERROR_DEVICE_REMOVED)
  58. D3DERR(DXGI_ERROR_DEVICE_RESET)
  59. D3DERR(DXGI_ERROR_DRIVER_INTERNAL_ERROR)
  60. D3DERR(DXGI_ERROR_INVALID_CALL)
  61. default: ErrorCodeText = FString::Printf(TEXT("%08X"),(int32)ErrorCode);
  62. }
  63.  
  64. return ErrorCodeText;
  65. }
  66.  
  67. static FString GetD3D11ErrorString(HRESULT ErrorCode, ID3D11Device* Device)
  68. {
  69. FString ErrorCodeText;
  70.  
  71. switch(ErrorCode)
  72. {
  73. D3DERR(S_OK);
  74. D3DERR(D3D11_ERROR_FILE_NOT_FOUND)
  75. D3DERR(D3D11_ERROR_TOO_MANY_UNIQUE_STATE_OBJECTS)
  76. #if WITH_D3DX_LIBS
  77. D3DERR(D3DERR_INVALIDCALL)
  78. D3DERR(D3DERR_WASSTILLDRAWING)
  79. #endif //WITH_D3DX_LIBS
  80. D3DERR(E_FAIL)
  81. D3DERR(E_INVALIDARG)
  82. D3DERR(E_OUTOFMEMORY)
  83. D3DERR(DXGI_ERROR_INVALID_CALL)
  84. D3DERR(E_NOINTERFACE)
  85. D3DERR(DXGI_ERROR_DEVICE_REMOVED)
  86. default: ErrorCodeText = FString::Printf(TEXT("%08X"),(int32)ErrorCode);
  87. }
  88.  
  89. if(ErrorCode == DXGI_ERROR_DEVICE_REMOVED && Device)
  90. {
  91. HRESULT hResDeviceRemoved = Device->GetDeviceRemovedReason();
  92. ErrorCodeText += FString(TEXT(" ")) + GetD3D11DeviceHungErrorString(hResDeviceRemoved);
  93. }
  94.  
  95. return ErrorCodeText;
  96. }
  97.  
  98. #undef D3DERR
  99.  
  100. const TCHAR* GetD3D11TextureFormatString(DXGI_FORMAT TextureFormat)
  101. {
  102. static const TCHAR* EmptyString = TEXT("");
  103. const TCHAR* TextureFormatText = EmptyString;
  104. #define D3DFORMATCASE(x) case x: TextureFormatText = TEXT(#x); break;
  105. switch(TextureFormat)
  106. {
  107. D3DFORMATCASE(DXGI_FORMAT_R8G8B8A8_UNORM)
  108. D3DFORMATCASE(DXGI_FORMAT_B8G8R8A8_UNORM)
  109. D3DFORMATCASE(DXGI_FORMAT_B8G8R8X8_UNORM)
  110. D3DFORMATCASE(DXGI_FORMAT_BC1_UNORM)
  111. D3DFORMATCASE(DXGI_FORMAT_BC2_UNORM)
  112. D3DFORMATCASE(DXGI_FORMAT_BC3_UNORM)
  113. D3DFORMATCASE(DXGI_FORMAT_BC4_UNORM)
  114. D3DFORMATCASE(DXGI_FORMAT_R16G16B16A16_FLOAT)
  115. D3DFORMATCASE(DXGI_FORMAT_R32G32B32A32_FLOAT)
  116. D3DFORMATCASE(DXGI_FORMAT_UNKNOWN)
  117. D3DFORMATCASE(DXGI_FORMAT_R8_UNORM)
  118. D3DFORMATCASE(DXGI_FORMAT_D32_FLOAT_S8X24_UINT)
  119. D3DFORMATCASE(DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS)
  120. D3DFORMATCASE(DXGI_FORMAT_R32G8X24_TYPELESS)
  121. D3DFORMATCASE(DXGI_FORMAT_D24_UNORM_S8_UINT)
  122. D3DFORMATCASE(DXGI_FORMAT_R24_UNORM_X8_TYPELESS)
  123. D3DFORMATCASE(DXGI_FORMAT_R32_FLOAT)
  124. D3DFORMATCASE(DXGI_FORMAT_R16G16_UINT)
  125. D3DFORMATCASE(DXGI_FORMAT_R16G16_UNORM)
  126. D3DFORMATCASE(DXGI_FORMAT_R16G16_SNORM)
  127. D3DFORMATCASE(DXGI_FORMAT_R16G16_FLOAT)
  128. D3DFORMATCASE(DXGI_FORMAT_R32G32_FLOAT)
  129. D3DFORMATCASE(DXGI_FORMAT_R10G10B10A2_UNORM)
  130. D3DFORMATCASE(DXGI_FORMAT_R16G16B16A16_UINT)
  131. D3DFORMATCASE(DXGI_FORMAT_R8G8_SNORM)
  132. D3DFORMATCASE(DXGI_FORMAT_BC5_UNORM)
  133. D3DFORMATCASE(DXGI_FORMAT_R1_UNORM)
  134. D3DFORMATCASE(DXGI_FORMAT_R8G8B8A8_TYPELESS)
  135. D3DFORMATCASE(DXGI_FORMAT_B8G8R8A8_TYPELESS)
  136. D3DFORMATCASE(DXGI_FORMAT_BC7_UNORM)
  137. D3DFORMATCASE(DXGI_FORMAT_BC6H_UF16)
  138. default: TextureFormatText = EmptyString;
  139. }
  140. #undef D3DFORMATCASE
  141. return TextureFormatText;
  142. }
  143.  
  144. static FString GetD3D11TextureFlagString(uint32 TextureFlags)
  145. {
  146. FString TextureFormatText = TEXT("");
  147.  
  148. if (TextureFlags & D3D11_BIND_RENDER_TARGET)
  149. {
  150. TextureFormatText += TEXT("D3D11_BIND_RENDER_TARGET ");
  151. }
  152.  
  153. if (TextureFlags & D3D11_BIND_DEPTH_STENCIL)
  154. {
  155. TextureFormatText += TEXT("D3D11_BIND_DEPTH_STENCIL ");
  156. }
  157.  
  158. if (TextureFlags & D3D11_BIND_SHADER_RESOURCE)
  159. {
  160. TextureFormatText += TEXT("D3D11_BIND_SHADER_RESOURCE ");
  161. }
  162.  
  163. if (TextureFlags & D3D11_BIND_UNORDERED_ACCESS)
  164. {
  165. TextureFormatText += TEXT("D3D11_BIND_UNORDERED_ACCESS ");
  166. }
  167.  
  168. return TextureFormatText;
  169. }
  170.  
  171. extern CORE_API bool GIsGPUCrashed;
  172. static void TerminateOnDeviceRemoved(HRESULT D3DResult, ID3D11Device* Direct3DDevice)
  173. {
  174. if (GDynamicRHI)
  175. {
  176. GDynamicRHI->CheckGpuHeartbeat();
  177. }
  178.  
  179. if (D3DResult == DXGI_ERROR_DEVICE_REMOVED)
  180. {
  181. #if NV_AFTERMATH
  182. uint32 Result = 0xffffffff;
  183. uint32 bDeviceActive = 0;
  184. if (GDX11NVAfterMathEnabled)
  185. {
  186. GFSDK_Aftermath_Device_Status Status;
  187. auto Res = GFSDK_Aftermath_GetDeviceStatus(&Status);
  188. Result = uint32(Res);
  189. if (Res == GFSDK_Aftermath_Result_Success)
  190. {
  191. bDeviceActive = Status == GFSDK_Aftermath_Device_Status_Active ? 1 : 0;
  192. }
  193. }
  194. UE_LOG(LogD3D11RHI, Log, TEXT("[Aftermath] GDynamicRHI=%p, GDX11NVAfterMathEnabled=%d, Result=0x%08X, bDeviceActive=%d"), GDynamicRHI, GDX11NVAfterMathEnabled, Result, bDeviceActive);
  195. #else
  196. UE_LOG(LogD3D11RHI, Log, TEXT("[Aftermath] NV_AFTERMATH is not set"));
  197. #endif
  198.  
  199. GIsGPUCrashed = true;
  200. if (Direct3DDevice)
  201. {
  202. HRESULT hRes = Direct3DDevice->GetDeviceRemovedReason();
  203.  
  204. const TCHAR* Reason = TEXT("?");
  205. switch (hRes)
  206. {
  207. case DXGI_ERROR_DEVICE_HUNG: Reason = TEXT("HUNG"); break;
  208. case DXGI_ERROR_DEVICE_REMOVED: Reason = TEXT("REMOVED"); break;
  209. case DXGI_ERROR_DEVICE_RESET: Reason = TEXT("RESET"); break;
  210. case DXGI_ERROR_DRIVER_INTERNAL_ERROR: Reason = TEXT("INTERNAL_ERROR"); break;
  211. case DXGI_ERROR_INVALID_CALL: Reason = TEXT("INVALID_CALL"); break;
  212. case S_OK: Reason = TEXT("S_OK"); break;
  213. }
  214.  
  215. // We currently don't support removed devices because FTexture2DResource can't recreate its RHI resources from scratch.
  216. // We would also need to recreate the viewport swap chains from scratch.
  217. UE_LOG(LogD3D11RHI, Fatal, TEXT("Unreal Engine is exiting due to D3D device being lost. (Error: 0x%X - '%s')"), hRes, Reason);
  218. }
  219. else
  220. {
  221. UE_LOG(LogD3D11RHI, Fatal, TEXT("Unreal Engine is exiting due to D3D device being lost. D3D device was not available to assertain DXGI cause."));
  222. }
  223.  
  224. // Workaround for the fact that in non-monolithic builds the exe gets into a weird state and exception handling fails.
  225. // @todo investigate why non-monolithic builds fail to capture the exception when graphics driver crashes.
  226. #if !IS_MONOLITHIC
  227. FPlatformMisc::RequestExit(true);
  228. #endif
  229. }
  230. }
  231.  
  232. static void TerminateOnOutOfMemory(HRESULT D3DResult, bool bCreatingTextures)
  233. {
  234. if (D3DResult == E_OUTOFMEMORY)
  235. {
  236. if (bCreatingTextures)
  237. {
  238. FPlatformMisc::MessageBoxExt(EAppMsgType::Ok, *LOCTEXT("OutOfVideoMemoryTextures", "Out of video memory trying to allocate a texture! Make sure your video card has the minimum required memory, try lowering the resolution and/or closing other applications that are running. Exiting...").ToString(), TEXT("Error"));
  239. }
  240. else
  241. {
  242. FPlatformMisc::MessageBoxExt(EAppMsgType::Ok, *NSLOCTEXT("D3D11RHI", "OutOfMemory", "Out of video memory trying to allocate a rendering resource. Make sure your video card has the minimum required memory, try lowering the resolution and/or closing other applications that are running. Exiting...").ToString(), TEXT("Error"));
  243. }
  244. #if STATS
  245. GetRendererModule().DebugLogOnCrash();
  246. #endif
  247. FPlatformMisc::RequestExit(true);
  248. }
  249. }
  250.  
  251.  
  252. #ifndef MAKE_D3DHRESULT
  253. #define _FACD3D 0x876
  254. #define MAKE_D3DHRESULT( code) MAKE_HRESULT( 1, _FACD3D, code )
  255. #endif //MAKE_D3DHRESULT
  256.  
  257. void VerifyD3D11Result(HRESULT D3DResult,const ANSICHAR* Code,const ANSICHAR* Filename,uint32 Line, ID3D11Device* Device)
  258. {
  259. check(FAILED(D3DResult));
  260.  
  261. const FString& ErrorString = GetD3D11ErrorString(D3DResult, Device);
  262.  
  263. UE_LOG(LogD3D11RHI, Error,TEXT("%s failed \n at %s:%u \n with error %s"),ANSI_TO_TCHAR(Code),ANSI_TO_TCHAR(Filename),Line,*ErrorString);
  264.  
  265. TerminateOnDeviceRemoved(D3DResult, Device);
  266. TerminateOnOutOfMemory(D3DResult, false);
  267.  
  268. UE_LOG(LogD3D11RHI, Fatal,TEXT("%s failed \n at %s:%u \n with error %s"),ANSI_TO_TCHAR(Code),ANSI_TO_TCHAR(Filename),Line,*ErrorString);
  269. }
  270.  
  271. void VerifyD3D11ShaderResult(FRHIShader* Shader, HRESULT D3DResult, const ANSICHAR* Code, const ANSICHAR* Filename, uint32 Line, ID3D11Device* Device)
  272. {
  273. check(FAILED(D3DResult));
  274.  
  275. const FString& ErrorString = GetD3D11ErrorString(D3DResult, Device);
  276.  
  277. #if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
  278. if (Shader->ShaderName.Len())
  279. {
  280. UE_LOG(LogD3D11RHI, Error, TEXT("%s failed trying to create shader %s\n at %s:%u \n with error %s"), ANSI_TO_TCHAR(Code), *Shader->ShaderName, ANSI_TO_TCHAR(Filename), Line, *ErrorString);
  281. TerminateOnDeviceRemoved(D3DResult, Device);
  282. TerminateOnOutOfMemory(D3DResult, false);
  283.  
  284. UE_LOG(LogD3D11RHI, Fatal, TEXT("%s failed trying to create shader %s \n at %s:%u \n with error %s"), ANSI_TO_TCHAR(Code), *Shader->ShaderName, ANSI_TO_TCHAR(Filename), Line, *ErrorString);
  285. }
  286. else
  287. #endif
  288. {
  289. VerifyD3D11Result(D3DResult, Code, Filename, Line, Device);
  290. }
  291. }
  292.  
  293. void VerifyD3D11CreateTextureResult(HRESULT D3DResult,const ANSICHAR* Code,const ANSICHAR* Filename,uint32 Line,uint32 SizeX,uint32 SizeY,uint32 SizeZ,uint8 Format,uint32 NumMips,uint32 Flags,
  294. D3D11_USAGE Usage, uint32 CPUAccessFlags, uint32 MiscFlags, uint32 SampleCount, uint32 SampleQuality,
  295. const void* SubResPtr, uint32 SubResPitch, uint32 SubResSlicePitch, ID3D11Device* Device)
  296. {
  297. check(FAILED(D3DResult));
  298.  
  299. const FString ErrorString = GetD3D11ErrorString(D3DResult, 0);
  300. const TCHAR* D3DFormatString = GetD3D11TextureFormatString((DXGI_FORMAT)Format);
  301.  
  302. UE_LOG(LogD3D11RHI, Error,
  303. TEXT("%s failed \n at %s:%u \n with error %s, \n Size=%ix%ix%i Format=%s(0x%08X), NumMips=%i, Flags=%s, Usage:0x%x, CPUFlags:0x%x, MiscFlags:0x%x, SampleCount:0x%x, SampleQuality:0x%x, SubresPtr:0x%p, SubresPitch:%i, SubresSlicePitch:%i"),
  304. ANSI_TO_TCHAR(Code),
  305. ANSI_TO_TCHAR(Filename),
  306. Line,
  307. *ErrorString,
  308. SizeX,
  309. SizeY,
  310. SizeZ,
  311. D3DFormatString,
  312. Format,
  313. NumMips,
  314. *GetD3D11TextureFlagString(Flags),
  315. Usage,
  316. CPUAccessFlags,
  317. MiscFlags,
  318. SampleCount,
  319. SampleQuality,
  320. SubResPtr,
  321. SubResPitch,
  322. SubResSlicePitch);
  323.  
  324. TerminateOnDeviceRemoved(D3DResult, Device);
  325. TerminateOnOutOfMemory(D3DResult, true);
  326.  
  327. UE_LOG(LogD3D11RHI, Fatal,
  328. TEXT("%s failed \n at %s:%u \n with error %s, \n Size=%ix%ix%i Format=%s(0x%08X), NumMips=%i, Flags=%s, Usage:0x%x, CPUFlags:0x%x, MiscFlags:0x%x, SampleCount:0x%x, SampleQuality:0x%x, SubresPtr:0x%p, SubresPitch:%i, SubresSlicePitch:%i"),
  329. ANSI_TO_TCHAR(Code),
  330. ANSI_TO_TCHAR(Filename),
  331. Line,
  332. *ErrorString,
  333. SizeX,
  334. SizeY,
  335. SizeZ,
  336. D3DFormatString,
  337. Format,
  338. NumMips,
  339. *GetD3D11TextureFlagString(Flags),
  340. Usage,
  341. CPUAccessFlags,
  342. MiscFlags,
  343. SampleCount,
  344. SampleQuality,
  345. SubResPtr,
  346. SubResPitch,
  347. SubResSlicePitch);
  348. }
  349.  
  350. void VerifyD3D11ResizeViewportResult(HRESULT D3DResult, const ANSICHAR* Code, const ANSICHAR* Filename, uint32 Line, uint32 SizeX, uint32 SizeY, uint8 Format, ID3D11Device* Device)
  351. {
  352. check(FAILED(D3DResult));
  353.  
  354. const FString ErrorString = GetD3D11ErrorString(D3DResult, 0);
  355. const TCHAR* D3DFormatString = GetD3D11TextureFormatString((DXGI_FORMAT)Format);
  356.  
  357. UE_LOG(LogD3D11RHI, Error,
  358. TEXT("%s failed \n at %s:%u \n with error %s, \n Size=%ix%i Format=%s(0x%08X)"),
  359. ANSI_TO_TCHAR(Code),
  360. ANSI_TO_TCHAR(Filename),
  361. Line,
  362. *ErrorString,
  363. SizeX,
  364. SizeY,
  365. D3DFormatString,
  366. Format);
  367.  
  368. TerminateOnDeviceRemoved(D3DResult, Device);
  369. TerminateOnOutOfMemory(D3DResult, true);
  370.  
  371. UE_LOG(LogD3D11RHI, Fatal,
  372. TEXT("%s failed \n at %s:%u \n with error %s, \n Size=%ix%i Format=%s(0x%08X)"),
  373. ANSI_TO_TCHAR(Code),
  374. ANSI_TO_TCHAR(Filename),
  375. Line,
  376. *ErrorString,
  377. SizeX,
  378. SizeY,
  379. D3DFormatString,
  380. Format);
  381. }
  382.  
  383. void VerifyComRefCount(IUnknown* Object,int32 ExpectedRefs,const TCHAR* Code,const TCHAR* Filename,int32 Line)
  384. {
  385. int32 NumRefs;
  386.  
  387. if (Object)
  388. {
  389. Object->AddRef();
  390. NumRefs = Object->Release();
  391.  
  392. checkSlow(NumRefs == ExpectedRefs);
  393.  
  394. if (NumRefs != ExpectedRefs)
  395. {
  396. UE_LOG(
  397. LogD3D11RHI,
  398. Error,
  399. TEXT("%s:(%d): %s has %d refs, expected %d"),
  400. Filename,
  401. Line,
  402. Code,
  403. NumRefs,
  404. ExpectedRefs
  405. );
  406. }
  407. }
  408. }
  409.  
  410. FD3D11BoundRenderTargets::FD3D11BoundRenderTargets(ID3D11DeviceContext* InDeviceContext)
  411. {
  412. FMemory::Memzero(RenderTargetViews,sizeof(RenderTargetViews));
  413. DepthStencilView = NULL;
  414. InDeviceContext->OMGetRenderTargets(
  415. MaxSimultaneousRenderTargets,
  416. &RenderTargetViews[0],
  417. &DepthStencilView
  418. );
  419.  
  420. // Find the last non-null rendertarget to determine the max
  421. // We traverse the array backwards, since they can be sparse
  422. for (NumActiveTargets = MaxSimultaneousRenderTargets; NumActiveTargets > 0; --NumActiveTargets)
  423. {
  424. if (RenderTargetViews[NumActiveTargets-1] != NULL)
  425. {
  426. break;
  427. }
  428. }
  429. }
  430.  
  431. FD3D11BoundRenderTargets::~FD3D11BoundRenderTargets()
  432. {
  433. // OMGetRenderTargets calls AddRef on each RTV/DSV it returns. We need
  434. // to make a corresponding call to Release.
  435. for (int32 TargetIndex = 0; TargetIndex < NumActiveTargets; ++TargetIndex)
  436. {
  437. if (RenderTargetViews[TargetIndex] != nullptr)
  438. {
  439. RenderTargetViews[TargetIndex]->Release();
  440. }
  441. }
  442. if (DepthStencilView)
  443. {
  444. DepthStencilView->Release();
  445. }
  446. }
  447.  
  448. FD3D11DynamicBuffer::FD3D11DynamicBuffer(FD3D11DynamicRHI* InD3DRHI, D3D11_BIND_FLAG InBindFlags, uint32* InBufferSizes)
  449. : D3DRHI(InD3DRHI)
  450. , BindFlags(InBindFlags)
  451. , LockedBufferIndex(-1)
  452. {
  453. while (BufferSizes.Num() < MAX_BUFFER_SIZES && *InBufferSizes > 0)
  454. {
  455. uint32 Size = *InBufferSizes++;
  456. BufferSizes.Add(Size);
  457. }
  458. check(*InBufferSizes == 0);
  459. InitResource();
  460. }
  461.  
  462. FD3D11DynamicBuffer::~FD3D11DynamicBuffer()
  463. {
  464. ReleaseResource();
  465. }
  466.  
  467. void FD3D11DynamicBuffer::InitRHI()
  468. {
  469. D3D11_BUFFER_DESC Desc;
  470. ZeroMemory( &Desc, sizeof( D3D11_BUFFER_DESC ) );
  471. Desc.Usage = D3D11_USAGE_DYNAMIC;
  472. Desc.BindFlags = BindFlags;
  473. Desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  474. Desc.MiscFlags = 0;
  475.  
  476. while (Buffers.Num() < BufferSizes.Num())
  477. {
  478. TRefCountPtr<ID3D11Buffer> Buffer;
  479. Desc.ByteWidth = BufferSizes[Buffers.Num()];
  480. VERIFYD3D11RESULT_EX(D3DRHI->GetDevice()->CreateBuffer(&Desc,NULL,Buffer.GetInitReference()), D3DRHI->GetDevice());
  481. UpdateBufferStats(Buffer,true);
  482. Buffers.Add(Buffer);
  483. }
  484. }
  485.  
  486. void FD3D11DynamicBuffer::ReleaseRHI()
  487. {
  488. for (int32 i = 0; i < Buffers.Num(); ++i)
  489. {
  490. UpdateBufferStats(Buffers[i],false);
  491. }
  492. Buffers.Empty();
  493. }
  494.  
  495. void* FD3D11DynamicBuffer::Lock(uint32 Size)
  496. {
  497. check(LockedBufferIndex == -1 && Buffers.Num() > 0);
  498.  
  499. int32 BufferIndex = 0;
  500. int32 NumBuffers = Buffers.Num();
  501. while (BufferIndex < NumBuffers && BufferSizes[BufferIndex] < Size)
  502. {
  503. BufferIndex++;
  504. }
  505. if (BufferIndex == NumBuffers)
  506. {
  507. BufferIndex--;
  508.  
  509. TRefCountPtr<ID3D11Buffer> Buffer;
  510. D3D11_BUFFER_DESC Desc;
  511. ZeroMemory( &Desc, sizeof( D3D11_BUFFER_DESC ) );
  512. Desc.Usage = D3D11_USAGE_DYNAMIC;
  513. Desc.BindFlags = BindFlags;
  514. Desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  515. Desc.MiscFlags = 0;
  516. Desc.ByteWidth = Size;
  517. VERIFYD3D11RESULT_EX(D3DRHI->GetDevice()->CreateBuffer(&Desc,NULL,Buffer.GetInitReference()), D3DRHI->GetDevice());
  518. UpdateBufferStats(Buffers[BufferIndex],false);
  519. UpdateBufferStats(Buffer,true);
  520. Buffers[BufferIndex] = Buffer;
  521. BufferSizes[BufferIndex] = Size;
  522. }
  523.  
  524. LockedBufferIndex = BufferIndex;
  525. D3D11_MAPPED_SUBRESOURCE MappedSubresource;
  526. VERIFYD3D11RESULT_EX(D3DRHI->GetDeviceContext()->Map(Buffers[BufferIndex],0,D3D11_MAP_WRITE_DISCARD,0,&MappedSubresource), D3DRHI->GetDevice());
  527. return MappedSubresource.pData;
  528. }
  529.  
  530. ID3D11Buffer* FD3D11DynamicBuffer::Unlock()
  531. {
  532. check(LockedBufferIndex != -1);
  533. ID3D11Buffer* LockedBuffer = Buffers[LockedBufferIndex];
  534. D3DRHI->GetDeviceContext()->Unmap(LockedBuffer,0);
  535. LockedBufferIndex = -1;
  536. return LockedBuffer;
  537. }
  538.  
  539. //
  540. // Stat declarations.
  541. //
  542.  
  543.  
  544. DEFINE_STAT(STAT_D3D11PresentTime);
  545. DEFINE_STAT(STAT_D3D11CustomPresentTime);
  546. DEFINE_STAT(STAT_D3D11TexturesAllocated);
  547. DEFINE_STAT(STAT_D3D11TexturesReleased);
  548. DEFINE_STAT(STAT_D3D11ClearShaderResourceTime);
  549. DEFINE_STAT(STAT_D3D11CreateTextureTime);
  550. DEFINE_STAT(STAT_D3D11LockTextureTime);
  551. DEFINE_STAT(STAT_D3D11UnlockTextureTime);
  552. DEFINE_STAT(STAT_D3D11CopyTextureTime);
  553. DEFINE_STAT(STAT_D3D11NewBoundShaderStateTime);
  554. DEFINE_STAT(STAT_D3D11CreateBoundShaderStateTime);
  555. DEFINE_STAT(STAT_D3D11CleanUniformBufferTime);
  556. DEFINE_STAT(STAT_D3D11UpdateUniformBufferTime);
  557. DEFINE_STAT(STAT_D3D11TexturePoolMemory);
  558. DEFINE_STAT(STAT_D3D11FreeUniformBufferMemory);
  559. DEFINE_STAT(STAT_D3D11NumFreeUniformBuffers);
  560. DEFINE_STAT(STAT_D3D11NumImmutableUniformBuffers);
  561. DEFINE_STAT(STAT_D3D11NumBoundShaderState);
  562.  
  563. #undef LOCTEXT_NAMESPACE
  564. © 2019 GitHub, Inc.
  565. Terms
  566. Privacy
  567. Security
  568. Status
  569. Help
  570. Contact GitHub
  571. Pricing
  572. API
  573. Training
  574. Blog
  575. About
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement