d3dx12.h 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514
  1. //
  2. // Copyright (c) Microsoft. All rights reserved.
  3. // This code is licensed under the MIT License (MIT).
  4. // THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
  5. // ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
  6. // IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
  7. // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
  8. //
  9. #ifdef AZ_COMPILER_CLANG
  10. #pragma clang diagnostic push
  11. #pragma clang diagnostic ignored "-Weverything"
  12. #endif
  13. #ifndef __D3DX12_H__
  14. #define __D3DX12_H__
  15. #include "d3d12.h"
  16. #if defined( __cplusplus )
  17. struct CD3DX12_DEFAULT {};
  18. extern const DECLSPEC_SELECTANY CD3DX12_DEFAULT D3D12_DEFAULT;
  19. //------------------------------------------------------------------------------------------------
  20. inline bool operator==( const D3D12_VIEWPORT& l, const D3D12_VIEWPORT& r )
  21. {
  22. return l.TopLeftX == r.TopLeftX && l.TopLeftY == r.TopLeftY && l.Width == r.Width &&
  23. l.Height == r.Height && l.MinDepth == r.MinDepth && l.MaxDepth == r.MaxDepth;
  24. }
  25. //------------------------------------------------------------------------------------------------
  26. inline bool operator!=( const D3D12_VIEWPORT& l, const D3D12_VIEWPORT& r )
  27. { return !( l == r ); }
  28. //------------------------------------------------------------------------------------------------
  29. struct CD3DX12_RECT : public D3D12_RECT
  30. {
  31. CD3DX12_RECT()
  32. {}
  33. explicit CD3DX12_RECT( const D3D12_RECT& o ) :
  34. D3D12_RECT( o )
  35. {}
  36. explicit CD3DX12_RECT(
  37. LONG Left,
  38. LONG Top,
  39. LONG Right,
  40. LONG Bottom )
  41. {
  42. left = Left;
  43. top = Top;
  44. right = Right;
  45. bottom = Bottom;
  46. }
  47. ~CD3DX12_RECT() {}
  48. operator const D3D12_RECT&() const { return *this; }
  49. };
  50. //------------------------------------------------------------------------------------------------
  51. struct CD3DX12_BOX : public D3D12_BOX
  52. {
  53. CD3DX12_BOX()
  54. {}
  55. explicit CD3DX12_BOX( const D3D12_BOX& o ) :
  56. D3D12_BOX( o )
  57. {}
  58. explicit CD3DX12_BOX(
  59. LONG Left,
  60. LONG Right )
  61. {
  62. left = Left;
  63. top = 0;
  64. front = 0;
  65. right = Right;
  66. bottom = 1;
  67. back = 1;
  68. }
  69. explicit CD3DX12_BOX(
  70. LONG Left,
  71. LONG Top,
  72. LONG Right,
  73. LONG Bottom )
  74. {
  75. left = Left;
  76. top = Top;
  77. front = 0;
  78. right = Right;
  79. bottom = Bottom;
  80. back = 1;
  81. }
  82. explicit CD3DX12_BOX(
  83. LONG Left,
  84. LONG Top,
  85. LONG Front,
  86. LONG Right,
  87. LONG Bottom,
  88. LONG Back )
  89. {
  90. left = Left;
  91. top = Top;
  92. front = Front;
  93. right = Right;
  94. bottom = Bottom;
  95. back = Back;
  96. }
  97. ~CD3DX12_BOX() {}
  98. operator const D3D12_BOX&() const { return *this; }
  99. };
  100. inline bool operator==( const D3D12_BOX& l, const D3D12_BOX& r )
  101. {
  102. return l.left == r.left && l.top == r.top && l.front == r.front &&
  103. l.right == r.right && l.bottom == r.bottom && l.back == r.back;
  104. }
  105. inline bool operator!=( const D3D12_BOX& l, const D3D12_BOX& r )
  106. { return !( l == r ); }
  107. //------------------------------------------------------------------------------------------------
  108. struct CD3DX12_DEPTH_STENCIL_DESC : public D3D12_DEPTH_STENCIL_DESC
  109. {
  110. CD3DX12_DEPTH_STENCIL_DESC()
  111. {}
  112. explicit CD3DX12_DEPTH_STENCIL_DESC( const D3D12_DEPTH_STENCIL_DESC& o ) :
  113. D3D12_DEPTH_STENCIL_DESC( o )
  114. {}
  115. explicit CD3DX12_DEPTH_STENCIL_DESC( CD3DX12_DEFAULT )
  116. {
  117. DepthEnable = TRUE;
  118. DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
  119. DepthFunc = D3D12_COMPARISON_FUNC_LESS;
  120. StencilEnable = FALSE;
  121. StencilReadMask = D3D12_DEFAULT_STENCIL_READ_MASK;
  122. StencilWriteMask = D3D12_DEFAULT_STENCIL_WRITE_MASK;
  123. const D3D12_DEPTH_STENCILOP_DESC defaultStencilOp =
  124. { D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, D3D12_COMPARISON_FUNC_ALWAYS };
  125. FrontFace = defaultStencilOp;
  126. BackFace = defaultStencilOp;
  127. }
  128. explicit CD3DX12_DEPTH_STENCIL_DESC(
  129. BOOL depthEnable,
  130. D3D12_DEPTH_WRITE_MASK depthWriteMask,
  131. D3D12_COMPARISON_FUNC depthFunc,
  132. BOOL stencilEnable,
  133. UINT8 stencilReadMask,
  134. UINT8 stencilWriteMask,
  135. D3D12_STENCIL_OP frontStencilFailOp,
  136. D3D12_STENCIL_OP frontStencilDepthFailOp,
  137. D3D12_STENCIL_OP frontStencilPassOp,
  138. D3D12_COMPARISON_FUNC frontStencilFunc,
  139. D3D12_STENCIL_OP backStencilFailOp,
  140. D3D12_STENCIL_OP backStencilDepthFailOp,
  141. D3D12_STENCIL_OP backStencilPassOp,
  142. D3D12_COMPARISON_FUNC backStencilFunc )
  143. {
  144. DepthEnable = depthEnable;
  145. DepthWriteMask = depthWriteMask;
  146. DepthFunc = depthFunc;
  147. StencilEnable = stencilEnable;
  148. StencilReadMask = stencilReadMask;
  149. StencilWriteMask = stencilWriteMask;
  150. FrontFace.StencilFailOp = frontStencilFailOp;
  151. FrontFace.StencilDepthFailOp = frontStencilDepthFailOp;
  152. FrontFace.StencilPassOp = frontStencilPassOp;
  153. FrontFace.StencilFunc = frontStencilFunc;
  154. BackFace.StencilFailOp = backStencilFailOp;
  155. BackFace.StencilDepthFailOp = backStencilDepthFailOp;
  156. BackFace.StencilPassOp = backStencilPassOp;
  157. BackFace.StencilFunc = backStencilFunc;
  158. }
  159. ~CD3DX12_DEPTH_STENCIL_DESC() {}
  160. operator const D3D12_DEPTH_STENCIL_DESC&() const { return *this; }
  161. };
  162. //------------------------------------------------------------------------------------------------
  163. struct CD3DX12_BLEND_DESC : public D3D12_BLEND_DESC
  164. {
  165. CD3DX12_BLEND_DESC()
  166. {}
  167. explicit CD3DX12_BLEND_DESC( const D3D12_BLEND_DESC& o ) :
  168. D3D12_BLEND_DESC( o )
  169. {}
  170. explicit CD3DX12_BLEND_DESC( CD3DX12_DEFAULT )
  171. {
  172. AlphaToCoverageEnable = FALSE;
  173. IndependentBlendEnable = FALSE;
  174. const D3D12_RENDER_TARGET_BLEND_DESC defaultRenderTargetBlendDesc =
  175. {
  176. FALSE,FALSE,
  177. D3D12_BLEND_ONE, D3D12_BLEND_ZERO, D3D12_BLEND_OP_ADD,
  178. D3D12_BLEND_ONE, D3D12_BLEND_ZERO, D3D12_BLEND_OP_ADD,
  179. D3D12_LOGIC_OP_NOOP,
  180. D3D12_COLOR_WRITE_ENABLE_ALL,
  181. };
  182. for (UINT i = 0; i < D3D12_SIMULTANEOUS_RENDER_TARGET_COUNT; ++i)
  183. RenderTarget[ i ] = defaultRenderTargetBlendDesc;
  184. }
  185. ~CD3DX12_BLEND_DESC() {}
  186. operator const D3D12_BLEND_DESC&() const { return *this; }
  187. };
  188. //------------------------------------------------------------------------------------------------
  189. struct CD3DX12_RASTERIZER_DESC : public D3D12_RASTERIZER_DESC
  190. {
  191. CD3DX12_RASTERIZER_DESC()
  192. {}
  193. explicit CD3DX12_RASTERIZER_DESC( const D3D12_RASTERIZER_DESC& o ) :
  194. D3D12_RASTERIZER_DESC( o )
  195. {}
  196. explicit CD3DX12_RASTERIZER_DESC( CD3DX12_DEFAULT )
  197. {
  198. FillMode = D3D12_FILL_MODE_SOLID;
  199. CullMode = D3D12_CULL_MODE_BACK;
  200. FrontCounterClockwise = FALSE;
  201. DepthBias = D3D12_DEFAULT_DEPTH_BIAS;
  202. DepthBiasClamp = D3D12_DEFAULT_DEPTH_BIAS_CLAMP;
  203. SlopeScaledDepthBias = D3D12_DEFAULT_SLOPE_SCALED_DEPTH_BIAS;
  204. DepthClipEnable = TRUE;
  205. MultisampleEnable = FALSE;
  206. AntialiasedLineEnable = FALSE;
  207. ForcedSampleCount = 0;
  208. ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF;
  209. }
  210. explicit CD3DX12_RASTERIZER_DESC(
  211. D3D12_FILL_MODE fillMode,
  212. D3D12_CULL_MODE cullMode,
  213. BOOL frontCounterClockwise,
  214. INT depthBias,
  215. FLOAT depthBiasClamp,
  216. FLOAT slopeScaledDepthBias,
  217. BOOL depthClipEnable,
  218. BOOL multisampleEnable,
  219. BOOL antialiasedLineEnable,
  220. UINT forcedSampleCount,
  221. D3D12_CONSERVATIVE_RASTERIZATION_MODE conservativeRaster)
  222. {
  223. FillMode = fillMode;
  224. CullMode = cullMode;
  225. FrontCounterClockwise = frontCounterClockwise;
  226. DepthBias = depthBias;
  227. DepthBiasClamp = depthBiasClamp;
  228. SlopeScaledDepthBias = slopeScaledDepthBias;
  229. DepthClipEnable = depthClipEnable;
  230. MultisampleEnable = multisampleEnable;
  231. AntialiasedLineEnable = antialiasedLineEnable;
  232. ForcedSampleCount = forcedSampleCount;
  233. ConservativeRaster = conservativeRaster;
  234. }
  235. ~CD3DX12_RASTERIZER_DESC() {}
  236. operator const D3D12_RASTERIZER_DESC&() const { return *this; }
  237. };
  238. //------------------------------------------------------------------------------------------------
  239. struct CD3DX12_RESOURCE_ALLOCATION_INFO : public D3D12_RESOURCE_ALLOCATION_INFO
  240. {
  241. CD3DX12_RESOURCE_ALLOCATION_INFO()
  242. {}
  243. explicit CD3DX12_RESOURCE_ALLOCATION_INFO( const D3D12_RESOURCE_ALLOCATION_INFO& o ) :
  244. D3D12_RESOURCE_ALLOCATION_INFO( o )
  245. {}
  246. CD3DX12_RESOURCE_ALLOCATION_INFO(
  247. UINT64 size,
  248. UINT64 alignment )
  249. {
  250. SizeInBytes = size;
  251. Alignment = alignment;
  252. }
  253. operator const D3D12_RESOURCE_ALLOCATION_INFO&() const { return *this; }
  254. };
  255. //------------------------------------------------------------------------------------------------
  256. struct CD3DX12_HEAP_PROPERTIES : public D3D12_HEAP_PROPERTIES
  257. {
  258. CD3DX12_HEAP_PROPERTIES()
  259. {}
  260. explicit CD3DX12_HEAP_PROPERTIES(const D3D12_HEAP_PROPERTIES &o) :
  261. D3D12_HEAP_PROPERTIES(o)
  262. {}
  263. CD3DX12_HEAP_PROPERTIES(
  264. D3D12_CPU_PAGE_PROPERTY cpuPageProperty,
  265. D3D12_MEMORY_POOL memoryPoolPreference,
  266. UINT creationNodeMask = 1,
  267. UINT nodeMask = 1 )
  268. {
  269. Type = D3D12_HEAP_TYPE_CUSTOM;
  270. CPUPageProperty = cpuPageProperty;
  271. MemoryPoolPreference = memoryPoolPreference;
  272. CreationNodeMask = creationNodeMask;
  273. VisibleNodeMask = nodeMask;
  274. }
  275. explicit CD3DX12_HEAP_PROPERTIES(
  276. D3D12_HEAP_TYPE type,
  277. UINT creationNodeMask = 1,
  278. UINT nodeMask = 1 )
  279. {
  280. Type = type;
  281. CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
  282. MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
  283. CreationNodeMask = creationNodeMask;
  284. VisibleNodeMask = nodeMask;
  285. }
  286. operator const D3D12_HEAP_PROPERTIES&() const { return *this; }
  287. bool IsCPUAccessible() const
  288. {
  289. return Type == D3D12_HEAP_TYPE_UPLOAD || Type == D3D12_HEAP_TYPE_READBACK || (Type == D3D12_HEAP_TYPE_CUSTOM &&
  290. (CPUPageProperty == D3D12_CPU_PAGE_PROPERTY_WRITE_COMBINE || CPUPageProperty == D3D12_CPU_PAGE_PROPERTY_WRITE_BACK));
  291. }
  292. };
  293. inline bool operator==( const D3D12_HEAP_PROPERTIES& l, const D3D12_HEAP_PROPERTIES& r )
  294. {
  295. return l.Type == r.Type && l.CPUPageProperty == r.CPUPageProperty &&
  296. l.MemoryPoolPreference == r.MemoryPoolPreference &&
  297. l.CreationNodeMask == r.CreationNodeMask &&
  298. l.VisibleNodeMask == r.VisibleNodeMask;
  299. }
  300. inline bool operator!=( const D3D12_HEAP_PROPERTIES& l, const D3D12_HEAP_PROPERTIES& r )
  301. { return !( l == r ); }
  302. //------------------------------------------------------------------------------------------------
  303. struct CD3DX12_HEAP_DESC : public D3D12_HEAP_DESC
  304. {
  305. CD3DX12_HEAP_DESC()
  306. {}
  307. explicit CD3DX12_HEAP_DESC(const D3D12_HEAP_DESC &o) :
  308. D3D12_HEAP_DESC(o)
  309. {}
  310. CD3DX12_HEAP_DESC(
  311. UINT64 size,
  312. D3D12_HEAP_PROPERTIES properties,
  313. UINT64 alignment = 0,
  314. D3D12_HEAP_FLAGS flags = D3D12_HEAP_FLAG_NONE )
  315. {
  316. SizeInBytes = size;
  317. Properties = properties;
  318. Alignment = alignment;
  319. Flags = flags;
  320. }
  321. CD3DX12_HEAP_DESC(
  322. UINT64 size,
  323. D3D12_HEAP_TYPE type,
  324. UINT64 alignment = 0,
  325. D3D12_HEAP_FLAGS flags = D3D12_HEAP_FLAG_NONE )
  326. {
  327. SizeInBytes = size;
  328. Properties = CD3DX12_HEAP_PROPERTIES( type );
  329. Alignment = alignment;
  330. Flags = flags;
  331. }
  332. CD3DX12_HEAP_DESC(
  333. UINT64 size,
  334. D3D12_CPU_PAGE_PROPERTY cpuPageProperty,
  335. D3D12_MEMORY_POOL memoryPoolPreference,
  336. UINT64 alignment = 0,
  337. D3D12_HEAP_FLAGS flags = D3D12_HEAP_FLAG_NONE )
  338. {
  339. SizeInBytes = size;
  340. Properties = CD3DX12_HEAP_PROPERTIES( cpuPageProperty, memoryPoolPreference );
  341. Alignment = alignment;
  342. Flags = flags;
  343. }
  344. CD3DX12_HEAP_DESC(
  345. const D3D12_RESOURCE_ALLOCATION_INFO& resAllocInfo,
  346. D3D12_HEAP_PROPERTIES properties,
  347. D3D12_HEAP_FLAGS flags = D3D12_HEAP_FLAG_NONE )
  348. {
  349. SizeInBytes = resAllocInfo.SizeInBytes;
  350. Properties = properties;
  351. Alignment = resAllocInfo.Alignment;
  352. Flags = flags;
  353. }
  354. CD3DX12_HEAP_DESC(
  355. const D3D12_RESOURCE_ALLOCATION_INFO& resAllocInfo,
  356. D3D12_HEAP_TYPE type,
  357. D3D12_HEAP_FLAGS flags = D3D12_HEAP_FLAG_NONE )
  358. {
  359. SizeInBytes = resAllocInfo.SizeInBytes;
  360. Properties = CD3DX12_HEAP_PROPERTIES( type );
  361. Alignment = resAllocInfo.Alignment;
  362. Flags = flags;
  363. }
  364. CD3DX12_HEAP_DESC(
  365. const D3D12_RESOURCE_ALLOCATION_INFO& resAllocInfo,
  366. D3D12_CPU_PAGE_PROPERTY cpuPageProperty,
  367. D3D12_MEMORY_POOL memoryPoolPreference,
  368. D3D12_HEAP_FLAGS flags = D3D12_HEAP_FLAG_NONE )
  369. {
  370. SizeInBytes = resAllocInfo.SizeInBytes;
  371. Properties = CD3DX12_HEAP_PROPERTIES( cpuPageProperty, memoryPoolPreference );
  372. Alignment = resAllocInfo.Alignment;
  373. Flags = flags;
  374. }
  375. operator const D3D12_HEAP_DESC&() const { return *this; }
  376. bool IsCPUAccessible() const
  377. { return static_cast< const CD3DX12_HEAP_PROPERTIES* >( &Properties )->IsCPUAccessible(); }
  378. };
  379. inline bool operator==( const D3D12_HEAP_DESC& l, const D3D12_HEAP_DESC& r )
  380. {
  381. return l.SizeInBytes == r.SizeInBytes &&
  382. l.Properties == r.Properties &&
  383. l.Alignment == r.Alignment &&
  384. l.Flags == r.Flags;
  385. }
  386. inline bool operator!=( const D3D12_HEAP_DESC& l, const D3D12_HEAP_DESC& r )
  387. { return !( l == r ); }
  388. //------------------------------------------------------------------------------------------------
  389. struct CD3DX12_CLEAR_VALUE : public D3D12_CLEAR_VALUE
  390. {
  391. CD3DX12_CLEAR_VALUE()
  392. {}
  393. explicit CD3DX12_CLEAR_VALUE(const D3D12_CLEAR_VALUE &o) :
  394. D3D12_CLEAR_VALUE(o)
  395. {}
  396. CD3DX12_CLEAR_VALUE(
  397. DXGI_FORMAT format,
  398. const FLOAT color[4] )
  399. {
  400. Format = format;
  401. memcpy( Color, color, sizeof( Color ) );
  402. }
  403. CD3DX12_CLEAR_VALUE(
  404. DXGI_FORMAT format,
  405. FLOAT depth,
  406. UINT8 stencil )
  407. {
  408. Format = format;
  409. /* Use memcpy to preserve NAN values */
  410. memcpy( &DepthStencil.Depth, &depth, sizeof( depth ) );
  411. DepthStencil.Stencil = stencil;
  412. }
  413. operator const D3D12_CLEAR_VALUE&() const { return *this; }
  414. };
  415. //------------------------------------------------------------------------------------------------
  416. struct CD3DX12_RANGE : public D3D12_RANGE
  417. {
  418. CD3DX12_RANGE()
  419. {}
  420. explicit CD3DX12_RANGE(const D3D12_RANGE &o) :
  421. D3D12_RANGE(o)
  422. {}
  423. CD3DX12_RANGE(
  424. SIZE_T begin,
  425. SIZE_T end )
  426. {
  427. Begin = begin;
  428. End = end;
  429. }
  430. operator const D3D12_RANGE&() const { return *this; }
  431. };
  432. //------------------------------------------------------------------------------------------------
  433. struct CD3DX12_TILED_RESOURCE_COORDINATE : public D3D12_TILED_RESOURCE_COORDINATE
  434. {
  435. CD3DX12_TILED_RESOURCE_COORDINATE()
  436. {}
  437. explicit CD3DX12_TILED_RESOURCE_COORDINATE(const D3D12_TILED_RESOURCE_COORDINATE &o) :
  438. D3D12_TILED_RESOURCE_COORDINATE(o)
  439. {}
  440. CD3DX12_TILED_RESOURCE_COORDINATE(
  441. UINT x,
  442. UINT y,
  443. UINT z,
  444. UINT subresource )
  445. {
  446. X = x;
  447. Y = y;
  448. Z = z;
  449. Subresource = subresource;
  450. }
  451. operator const D3D12_TILED_RESOURCE_COORDINATE&() const { return *this; }
  452. };
  453. //------------------------------------------------------------------------------------------------
  454. struct CD3DX12_TILE_REGION_SIZE : public D3D12_TILE_REGION_SIZE
  455. {
  456. CD3DX12_TILE_REGION_SIZE()
  457. {}
  458. explicit CD3DX12_TILE_REGION_SIZE(const D3D12_TILE_REGION_SIZE &o) :
  459. D3D12_TILE_REGION_SIZE(o)
  460. {}
  461. CD3DX12_TILE_REGION_SIZE(
  462. UINT numTiles,
  463. BOOL useBox,
  464. UINT width,
  465. UINT16 height,
  466. UINT16 depth )
  467. {
  468. NumTiles = numTiles;
  469. UseBox = useBox;
  470. Width = width;
  471. Height = height;
  472. Depth = depth;
  473. }
  474. operator const D3D12_TILE_REGION_SIZE&() const { return *this; }
  475. };
  476. //------------------------------------------------------------------------------------------------
  477. struct CD3DX12_SUBRESOURCE_TILING : public D3D12_SUBRESOURCE_TILING
  478. {
  479. CD3DX12_SUBRESOURCE_TILING()
  480. {}
  481. explicit CD3DX12_SUBRESOURCE_TILING(const D3D12_SUBRESOURCE_TILING &o) :
  482. D3D12_SUBRESOURCE_TILING(o)
  483. {}
  484. CD3DX12_SUBRESOURCE_TILING(
  485. UINT widthInTiles,
  486. UINT16 heightInTiles,
  487. UINT16 depthInTiles,
  488. UINT startTileIndexInOverallResource )
  489. {
  490. WidthInTiles = widthInTiles;
  491. HeightInTiles = heightInTiles;
  492. DepthInTiles = depthInTiles;
  493. StartTileIndexInOverallResource = startTileIndexInOverallResource;
  494. }
  495. operator const D3D12_SUBRESOURCE_TILING&() const { return *this; }
  496. };
  497. //------------------------------------------------------------------------------------------------
  498. struct CD3DX12_TILE_SHAPE : public D3D12_TILE_SHAPE
  499. {
  500. CD3DX12_TILE_SHAPE()
  501. {}
  502. explicit CD3DX12_TILE_SHAPE(const D3D12_TILE_SHAPE &o) :
  503. D3D12_TILE_SHAPE(o)
  504. {}
  505. CD3DX12_TILE_SHAPE(
  506. UINT widthInTexels,
  507. UINT heightInTexels,
  508. UINT depthInTexels )
  509. {
  510. WidthInTexels = widthInTexels;
  511. HeightInTexels = heightInTexels;
  512. DepthInTexels = depthInTexels;
  513. }
  514. operator const D3D12_TILE_SHAPE&() const { return *this; }
  515. };
  516. //------------------------------------------------------------------------------------------------
  517. struct CD3DX12_RESOURCE_BARRIER : public D3D12_RESOURCE_BARRIER
  518. {
  519. CD3DX12_RESOURCE_BARRIER()
  520. {}
  521. explicit CD3DX12_RESOURCE_BARRIER(const D3D12_RESOURCE_BARRIER &o) :
  522. D3D12_RESOURCE_BARRIER(o)
  523. {}
  524. static inline CD3DX12_RESOURCE_BARRIER Transition(
  525. _In_ ID3D12Resource* pResource,
  526. D3D12_RESOURCE_STATES stateBefore,
  527. D3D12_RESOURCE_STATES stateAfter,
  528. UINT subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES,
  529. D3D12_RESOURCE_BARRIER_FLAGS flags = D3D12_RESOURCE_BARRIER_FLAG_NONE)
  530. {
  531. CD3DX12_RESOURCE_BARRIER result;
  532. ZeroMemory(&result, sizeof(result));
  533. D3D12_RESOURCE_BARRIER &barrier = result;
  534. result.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
  535. result.Flags = flags;
  536. barrier.Transition.pResource = pResource;
  537. barrier.Transition.StateBefore = stateBefore;
  538. barrier.Transition.StateAfter = stateAfter;
  539. barrier.Transition.Subresource = subresource;
  540. return result;
  541. }
  542. static inline CD3DX12_RESOURCE_BARRIER Aliasing(
  543. _In_ ID3D12Resource* pResourceBefore,
  544. _In_ ID3D12Resource* pResourceAfter)
  545. {
  546. CD3DX12_RESOURCE_BARRIER result;
  547. ZeroMemory(&result, sizeof(result));
  548. D3D12_RESOURCE_BARRIER &barrier = result;
  549. result.Type = D3D12_RESOURCE_BARRIER_TYPE_ALIASING;
  550. barrier.Aliasing.pResourceBefore = pResourceBefore;
  551. barrier.Aliasing.pResourceAfter = pResourceAfter;
  552. return result;
  553. }
  554. static inline CD3DX12_RESOURCE_BARRIER UAV(
  555. _In_ ID3D12Resource* pResource)
  556. {
  557. CD3DX12_RESOURCE_BARRIER result;
  558. ZeroMemory(&result, sizeof(result));
  559. D3D12_RESOURCE_BARRIER &barrier = result;
  560. result.Type = D3D12_RESOURCE_BARRIER_TYPE_UAV;
  561. barrier.UAV.pResource = pResource;
  562. return result;
  563. }
  564. operator const D3D12_RESOURCE_BARRIER&() const { return *this; }
  565. };
  566. //------------------------------------------------------------------------------------------------
  567. struct CD3DX12_PACKED_MIP_INFO : public D3D12_PACKED_MIP_INFO
  568. {
  569. CD3DX12_PACKED_MIP_INFO()
  570. {}
  571. explicit CD3DX12_PACKED_MIP_INFO(const D3D12_PACKED_MIP_INFO &o) :
  572. D3D12_PACKED_MIP_INFO(o)
  573. {}
  574. CD3DX12_PACKED_MIP_INFO(
  575. UINT8 numStandardMips,
  576. UINT8 numPackedMips,
  577. UINT numTilesForPackedMips,
  578. UINT startTileIndexInOverallResource )
  579. {
  580. NumStandardMips = numStandardMips;
  581. NumPackedMips = numPackedMips;
  582. NumTilesForPackedMips = numTilesForPackedMips;
  583. StartTileIndexInOverallResource = startTileIndexInOverallResource;
  584. }
  585. operator const D3D12_PACKED_MIP_INFO&() const { return *this; }
  586. };
  587. //------------------------------------------------------------------------------------------------
  588. struct CD3DX12_SUBRESOURCE_FOOTPRINT : public D3D12_SUBRESOURCE_FOOTPRINT
  589. {
  590. CD3DX12_SUBRESOURCE_FOOTPRINT()
  591. {}
  592. explicit CD3DX12_SUBRESOURCE_FOOTPRINT(const D3D12_SUBRESOURCE_FOOTPRINT &o) :
  593. D3D12_SUBRESOURCE_FOOTPRINT(o)
  594. {}
  595. CD3DX12_SUBRESOURCE_FOOTPRINT(
  596. DXGI_FORMAT format,
  597. UINT width,
  598. UINT height,
  599. UINT depth,
  600. UINT rowPitch )
  601. {
  602. Format = format;
  603. Width = width;
  604. Height = height;
  605. Depth = depth;
  606. RowPitch = rowPitch;
  607. }
  608. explicit CD3DX12_SUBRESOURCE_FOOTPRINT(
  609. const D3D12_RESOURCE_DESC& resDesc,
  610. UINT rowPitch )
  611. {
  612. Format = resDesc.Format;
  613. Width = UINT( resDesc.Width );
  614. Height = resDesc.Height;
  615. Depth = (resDesc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D ? resDesc.DepthOrArraySize : 1);
  616. RowPitch = rowPitch;
  617. }
  618. operator const D3D12_SUBRESOURCE_FOOTPRINT&() const { return *this; }
  619. };
  620. //------------------------------------------------------------------------------------------------
  621. struct CD3DX12_TEXTURE_COPY_LOCATION : public D3D12_TEXTURE_COPY_LOCATION
  622. {
  623. CD3DX12_TEXTURE_COPY_LOCATION()
  624. {}
  625. explicit CD3DX12_TEXTURE_COPY_LOCATION(const D3D12_TEXTURE_COPY_LOCATION &o) :
  626. D3D12_TEXTURE_COPY_LOCATION(o)
  627. {}
  628. CD3DX12_TEXTURE_COPY_LOCATION(ID3D12Resource* pRes) { pResource = pRes; }
  629. CD3DX12_TEXTURE_COPY_LOCATION(ID3D12Resource* pRes, D3D12_PLACED_SUBRESOURCE_FOOTPRINT const& Footprint)
  630. {
  631. pResource = pRes;
  632. Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
  633. PlacedFootprint = Footprint;
  634. }
  635. CD3DX12_TEXTURE_COPY_LOCATION(ID3D12Resource* pRes, UINT Sub)
  636. {
  637. pResource = pRes;
  638. Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
  639. SubresourceIndex = Sub;
  640. }
  641. };
  642. //------------------------------------------------------------------------------------------------
  643. struct CD3DX12_DESCRIPTOR_RANGE : public D3D12_DESCRIPTOR_RANGE
  644. {
  645. CD3DX12_DESCRIPTOR_RANGE() { }
  646. explicit CD3DX12_DESCRIPTOR_RANGE(const D3D12_DESCRIPTOR_RANGE &o) :
  647. D3D12_DESCRIPTOR_RANGE(o)
  648. {}
  649. CD3DX12_DESCRIPTOR_RANGE(
  650. D3D12_DESCRIPTOR_RANGE_TYPE rangeType,
  651. UINT numDescriptors,
  652. UINT baseShaderRegister,
  653. UINT registerSpace = 0,
  654. UINT offsetInDescriptorsFromTableStart =
  655. D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND)
  656. {
  657. Init(rangeType, numDescriptors, baseShaderRegister, registerSpace, offsetInDescriptorsFromTableStart);
  658. }
  659. inline void Init(
  660. D3D12_DESCRIPTOR_RANGE_TYPE rangeType,
  661. UINT numDescriptors,
  662. UINT baseShaderRegister,
  663. UINT registerSpace = 0,
  664. UINT offsetInDescriptorsFromTableStart =
  665. D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND)
  666. {
  667. Init(*this, rangeType, numDescriptors, baseShaderRegister, registerSpace, offsetInDescriptorsFromTableStart);
  668. }
  669. static inline void Init(
  670. _Out_ D3D12_DESCRIPTOR_RANGE &range,
  671. D3D12_DESCRIPTOR_RANGE_TYPE rangeType,
  672. UINT numDescriptors,
  673. UINT baseShaderRegister,
  674. UINT registerSpace = 0,
  675. UINT offsetInDescriptorsFromTableStart =
  676. D3D12_DESCRIPTOR_RANGE_OFFSET_APPEND)
  677. {
  678. range.RangeType = rangeType;
  679. range.NumDescriptors = numDescriptors;
  680. range.BaseShaderRegister = baseShaderRegister;
  681. range.RegisterSpace = registerSpace;
  682. range.OffsetInDescriptorsFromTableStart = offsetInDescriptorsFromTableStart;
  683. }
  684. };
  685. //------------------------------------------------------------------------------------------------
  686. struct CD3DX12_ROOT_DESCRIPTOR_TABLE : public D3D12_ROOT_DESCRIPTOR_TABLE
  687. {
  688. CD3DX12_ROOT_DESCRIPTOR_TABLE() {}
  689. explicit CD3DX12_ROOT_DESCRIPTOR_TABLE(const D3D12_ROOT_DESCRIPTOR_TABLE &o) :
  690. D3D12_ROOT_DESCRIPTOR_TABLE(o)
  691. {}
  692. CD3DX12_ROOT_DESCRIPTOR_TABLE(
  693. UINT numDescriptorRanges,
  694. _In_reads_opt_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE* _pDescriptorRanges)
  695. {
  696. Init(numDescriptorRanges, _pDescriptorRanges);
  697. }
  698. inline void Init(
  699. UINT numDescriptorRanges,
  700. _In_reads_opt_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE* _pDescriptorRanges)
  701. {
  702. Init(*this, numDescriptorRanges, _pDescriptorRanges);
  703. }
  704. static inline void Init(
  705. _Out_ D3D12_ROOT_DESCRIPTOR_TABLE &rootDescriptorTable,
  706. UINT numDescriptorRanges,
  707. _In_reads_opt_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE* _pDescriptorRanges)
  708. {
  709. rootDescriptorTable.NumDescriptorRanges = numDescriptorRanges;
  710. rootDescriptorTable.pDescriptorRanges = _pDescriptorRanges;
  711. }
  712. };
  713. //------------------------------------------------------------------------------------------------
  714. struct CD3DX12_ROOT_CONSTANTS : public D3D12_ROOT_CONSTANTS
  715. {
  716. CD3DX12_ROOT_CONSTANTS() {}
  717. explicit CD3DX12_ROOT_CONSTANTS(const D3D12_ROOT_CONSTANTS &o) :
  718. D3D12_ROOT_CONSTANTS(o)
  719. {}
  720. CD3DX12_ROOT_CONSTANTS(
  721. UINT num32BitValues,
  722. UINT shaderRegister,
  723. UINT registerSpace = 0)
  724. {
  725. Init(num32BitValues, shaderRegister, registerSpace);
  726. }
  727. inline void Init(
  728. UINT num32BitValues,
  729. UINT shaderRegister,
  730. UINT registerSpace = 0)
  731. {
  732. Init(*this, num32BitValues, shaderRegister, registerSpace);
  733. }
  734. static inline void Init(
  735. _Out_ D3D12_ROOT_CONSTANTS &rootConstants,
  736. UINT num32BitValues,
  737. UINT shaderRegister,
  738. UINT registerSpace = 0)
  739. {
  740. rootConstants.Num32BitValues = num32BitValues;
  741. rootConstants.ShaderRegister = shaderRegister;
  742. rootConstants.RegisterSpace = registerSpace;
  743. }
  744. };
  745. //------------------------------------------------------------------------------------------------
  746. struct CD3DX12_ROOT_DESCRIPTOR : public D3D12_ROOT_DESCRIPTOR
  747. {
  748. CD3DX12_ROOT_DESCRIPTOR() {}
  749. explicit CD3DX12_ROOT_DESCRIPTOR(const D3D12_ROOT_DESCRIPTOR &o) :
  750. D3D12_ROOT_DESCRIPTOR(o)
  751. {}
  752. CD3DX12_ROOT_DESCRIPTOR(
  753. UINT shaderRegister,
  754. UINT registerSpace = 0)
  755. {
  756. Init(shaderRegister, registerSpace);
  757. }
  758. inline void Init(
  759. UINT shaderRegister,
  760. UINT registerSpace = 0)
  761. {
  762. Init(*this, shaderRegister, registerSpace);
  763. }
  764. static inline void Init(_Out_ D3D12_ROOT_DESCRIPTOR &table, UINT shaderRegister, UINT registerSpace = 0)
  765. {
  766. table.ShaderRegister = shaderRegister;
  767. table.RegisterSpace = registerSpace;
  768. }
  769. };
  770. //------------------------------------------------------------------------------------------------
  771. struct CD3DX12_ROOT_PARAMETER : public D3D12_ROOT_PARAMETER
  772. {
  773. CD3DX12_ROOT_PARAMETER() {}
  774. explicit CD3DX12_ROOT_PARAMETER(const D3D12_ROOT_PARAMETER &o) :
  775. D3D12_ROOT_PARAMETER(o)
  776. {}
  777. static inline void InitAsDescriptorTable(
  778. _Out_ D3D12_ROOT_PARAMETER &rootParam,
  779. UINT numDescriptorRanges,
  780. _In_reads_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE* pDescriptorRanges,
  781. D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)
  782. {
  783. rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
  784. rootParam.ShaderVisibility = visibility;
  785. CD3DX12_ROOT_DESCRIPTOR_TABLE::Init(rootParam.DescriptorTable, numDescriptorRanges, pDescriptorRanges);
  786. }
  787. static inline void InitAsConstants(
  788. _Out_ D3D12_ROOT_PARAMETER &rootParam,
  789. UINT num32BitValues,
  790. UINT shaderRegister,
  791. UINT registerSpace = 0,
  792. D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)
  793. {
  794. rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;
  795. rootParam.ShaderVisibility = visibility;
  796. CD3DX12_ROOT_CONSTANTS::Init(rootParam.Constants, num32BitValues, shaderRegister, registerSpace);
  797. }
  798. static inline void InitAsConstantBufferView(
  799. _Out_ D3D12_ROOT_PARAMETER &rootParam,
  800. UINT shaderRegister,
  801. UINT registerSpace = 0,
  802. D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)
  803. {
  804. rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
  805. rootParam.ShaderVisibility = visibility;
  806. CD3DX12_ROOT_DESCRIPTOR::Init(rootParam.Descriptor, shaderRegister, registerSpace);
  807. }
  808. static inline void InitAsShaderResourceView(
  809. _Out_ D3D12_ROOT_PARAMETER &rootParam,
  810. UINT shaderRegister,
  811. UINT registerSpace = 0,
  812. D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)
  813. {
  814. rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_SRV;
  815. rootParam.ShaderVisibility = visibility;
  816. CD3DX12_ROOT_DESCRIPTOR::Init(rootParam.Descriptor, shaderRegister, registerSpace);
  817. }
  818. static inline void InitAsUnorderedAccessView(
  819. _Out_ D3D12_ROOT_PARAMETER &rootParam,
  820. UINT shaderRegister,
  821. UINT registerSpace = 0,
  822. D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)
  823. {
  824. rootParam.ParameterType = D3D12_ROOT_PARAMETER_TYPE_UAV;
  825. rootParam.ShaderVisibility = visibility;
  826. CD3DX12_ROOT_DESCRIPTOR::Init(rootParam.Descriptor, shaderRegister, registerSpace);
  827. }
  828. inline void InitAsDescriptorTable(
  829. UINT numDescriptorRanges,
  830. _In_reads_(numDescriptorRanges) const D3D12_DESCRIPTOR_RANGE* pDescriptorRanges,
  831. D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)
  832. {
  833. InitAsDescriptorTable(*this, numDescriptorRanges, pDescriptorRanges, visibility);
  834. }
  835. inline void InitAsConstants(
  836. UINT num32BitValues,
  837. UINT shaderRegister,
  838. UINT registerSpace = 0,
  839. D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)
  840. {
  841. InitAsConstants(*this, num32BitValues, shaderRegister, registerSpace, visibility);
  842. }
  843. inline void InitAsConstantBufferView(
  844. UINT shaderRegister,
  845. UINT registerSpace = 0,
  846. D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)
  847. {
  848. InitAsConstantBufferView(*this, shaderRegister, registerSpace, visibility);
  849. }
  850. inline void InitAsShaderResourceView(
  851. UINT shaderRegister,
  852. UINT registerSpace = 0,
  853. D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)
  854. {
  855. InitAsShaderResourceView(*this, shaderRegister, registerSpace, visibility);
  856. }
  857. inline void InitAsUnorderedAccessView(
  858. UINT shaderRegister,
  859. UINT registerSpace = 0,
  860. D3D12_SHADER_VISIBILITY visibility = D3D12_SHADER_VISIBILITY_ALL)
  861. {
  862. InitAsUnorderedAccessView(*this, shaderRegister, registerSpace, visibility);
  863. }
  864. };
  865. //------------------------------------------------------------------------------------------------
  866. struct CD3DX12_STATIC_SAMPLER_DESC : public D3D12_STATIC_SAMPLER_DESC
  867. {
  868. CD3DX12_STATIC_SAMPLER_DESC() {}
  869. explicit CD3DX12_STATIC_SAMPLER_DESC(const D3D12_STATIC_SAMPLER_DESC &o) :
  870. D3D12_STATIC_SAMPLER_DESC(o)
  871. {}
  872. CD3DX12_STATIC_SAMPLER_DESC(
  873. UINT shaderRegister,
  874. D3D12_FILTER filter = D3D12_FILTER_ANISOTROPIC,
  875. D3D12_TEXTURE_ADDRESS_MODE addressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP,
  876. D3D12_TEXTURE_ADDRESS_MODE addressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP,
  877. D3D12_TEXTURE_ADDRESS_MODE addressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP,
  878. FLOAT mipLODBias = 0,
  879. UINT maxAnisotropy = 16,
  880. D3D12_COMPARISON_FUNC comparisonFunc = D3D12_COMPARISON_FUNC_LESS_EQUAL,
  881. D3D12_STATIC_BORDER_COLOR borderColor = D3D12_STATIC_BORDER_COLOR_OPAQUE_WHITE,
  882. FLOAT minLOD = 0.f,
  883. FLOAT maxLOD = D3D12_FLOAT32_MAX,
  884. D3D12_SHADER_VISIBILITY shaderVisibility = D3D12_SHADER_VISIBILITY_ALL,
  885. UINT registerSpace = 0)
  886. {
  887. Init(
  888. shaderRegister,
  889. filter,
  890. addressU,
  891. addressV,
  892. addressW,
  893. mipLODBias,
  894. maxAnisotropy,
  895. comparisonFunc,
  896. borderColor,
  897. minLOD,
  898. maxLOD,
  899. shaderVisibility,
  900. registerSpace);
  901. }
  902. static inline void Init(
  903. _Out_ D3D12_STATIC_SAMPLER_DESC &samplerDesc,
  904. UINT shaderRegister,
  905. D3D12_FILTER filter = D3D12_FILTER_ANISOTROPIC,
  906. D3D12_TEXTURE_ADDRESS_MODE addressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP,
  907. D3D12_TEXTURE_ADDRESS_MODE addressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP,
  908. D3D12_TEXTURE_ADDRESS_MODE addressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP,
  909. FLOAT mipLODBias = 0,
  910. UINT maxAnisotropy = 16,
  911. D3D12_COMPARISON_FUNC comparisonFunc = D3D12_COMPARISON_FUNC_LESS_EQUAL,
  912. D3D12_STATIC_BORDER_COLOR borderColor = D3D12_STATIC_BORDER_COLOR_OPAQUE_WHITE,
  913. FLOAT minLOD = 0.f,
  914. FLOAT maxLOD = D3D12_FLOAT32_MAX,
  915. D3D12_SHADER_VISIBILITY shaderVisibility = D3D12_SHADER_VISIBILITY_ALL,
  916. UINT registerSpace = 0)
  917. {
  918. samplerDesc.ShaderRegister = shaderRegister;
  919. samplerDesc.Filter = filter;
  920. samplerDesc.AddressU = addressU;
  921. samplerDesc.AddressV = addressV;
  922. samplerDesc.AddressW = addressW;
  923. samplerDesc.MipLODBias = mipLODBias;
  924. samplerDesc.MaxAnisotropy = maxAnisotropy;
  925. samplerDesc.ComparisonFunc = comparisonFunc;
  926. samplerDesc.BorderColor = borderColor;
  927. samplerDesc.MinLOD = minLOD;
  928. samplerDesc.MaxLOD = maxLOD;
  929. samplerDesc.ShaderVisibility = shaderVisibility;
  930. samplerDesc.RegisterSpace = registerSpace;
  931. }
  932. inline void Init(
  933. UINT shaderRegister,
  934. D3D12_FILTER filter = D3D12_FILTER_ANISOTROPIC,
  935. D3D12_TEXTURE_ADDRESS_MODE addressU = D3D12_TEXTURE_ADDRESS_MODE_WRAP,
  936. D3D12_TEXTURE_ADDRESS_MODE addressV = D3D12_TEXTURE_ADDRESS_MODE_WRAP,
  937. D3D12_TEXTURE_ADDRESS_MODE addressW = D3D12_TEXTURE_ADDRESS_MODE_WRAP,
  938. FLOAT mipLODBias = 0,
  939. UINT maxAnisotropy = 16,
  940. D3D12_COMPARISON_FUNC comparisonFunc = D3D12_COMPARISON_FUNC_LESS_EQUAL,
  941. D3D12_STATIC_BORDER_COLOR borderColor = D3D12_STATIC_BORDER_COLOR_OPAQUE_WHITE,
  942. FLOAT minLOD = 0.f,
  943. FLOAT maxLOD = D3D12_FLOAT32_MAX,
  944. D3D12_SHADER_VISIBILITY shaderVisibility = D3D12_SHADER_VISIBILITY_ALL,
  945. UINT registerSpace = 0)
  946. {
  947. Init(
  948. *this,
  949. shaderRegister,
  950. filter,
  951. addressU,
  952. addressV,
  953. addressW,
  954. mipLODBias,
  955. maxAnisotropy,
  956. comparisonFunc,
  957. borderColor,
  958. minLOD,
  959. maxLOD,
  960. shaderVisibility,
  961. registerSpace);
  962. }
  963. };
  964. //------------------------------------------------------------------------------------------------
  965. struct CD3DX12_ROOT_SIGNATURE_DESC : public D3D12_ROOT_SIGNATURE_DESC
  966. {
  967. CD3DX12_ROOT_SIGNATURE_DESC() {}
  968. explicit CD3DX12_ROOT_SIGNATURE_DESC(const D3D12_ROOT_SIGNATURE_DESC &o) :
  969. D3D12_ROOT_SIGNATURE_DESC(o)
  970. {}
  971. CD3DX12_ROOT_SIGNATURE_DESC(
  972. UINT numParameters,
  973. _In_reads_opt_(numParameters) const D3D12_ROOT_PARAMETER* _pParameters,
  974. UINT numStaticSamplers = 0,
  975. _In_reads_opt_(numStaticSamplers) const D3D12_STATIC_SAMPLER_DESC* _pStaticSamplers = NULL,
  976. D3D12_ROOT_SIGNATURE_FLAGS flags = D3D12_ROOT_SIGNATURE_FLAG_NONE)
  977. {
  978. Init(numParameters, _pParameters, numStaticSamplers, _pStaticSamplers, flags);
  979. }
  980. CD3DX12_ROOT_SIGNATURE_DESC(CD3DX12_DEFAULT)
  981. {
  982. Init(0, NULL, 0, NULL, D3D12_ROOT_SIGNATURE_FLAG_NONE);
  983. }
  984. inline void Init(
  985. UINT numParameters,
  986. _In_reads_opt_(numParameters) const D3D12_ROOT_PARAMETER* _pParameters,
  987. UINT numStaticSamplers = 0,
  988. _In_reads_opt_(numStaticSamplers) const D3D12_STATIC_SAMPLER_DESC* _pStaticSamplers = NULL,
  989. D3D12_ROOT_SIGNATURE_FLAGS flags = D3D12_ROOT_SIGNATURE_FLAG_NONE)
  990. {
  991. Init(*this, numParameters, _pParameters, numStaticSamplers, _pStaticSamplers, flags);
  992. }
  993. static inline void Init(
  994. _Out_ D3D12_ROOT_SIGNATURE_DESC &desc,
  995. UINT numParameters,
  996. _In_reads_opt_(numParameters) const D3D12_ROOT_PARAMETER* _pParameters,
  997. UINT numStaticSamplers = 0,
  998. _In_reads_opt_(numStaticSamplers) const D3D12_STATIC_SAMPLER_DESC* _pStaticSamplers = NULL,
  999. D3D12_ROOT_SIGNATURE_FLAGS flags = D3D12_ROOT_SIGNATURE_FLAG_NONE)
  1000. {
  1001. desc.NumParameters = numParameters;
  1002. desc.pParameters = _pParameters;
  1003. desc.NumStaticSamplers = numStaticSamplers;
  1004. desc.pStaticSamplers = _pStaticSamplers;
  1005. desc.Flags = flags;
  1006. }
  1007. };
  1008. //------------------------------------------------------------------------------------------------
  1009. struct CD3DX12_CPU_DESCRIPTOR_HANDLE : public D3D12_CPU_DESCRIPTOR_HANDLE
  1010. {
  1011. CD3DX12_CPU_DESCRIPTOR_HANDLE() {}
  1012. explicit CD3DX12_CPU_DESCRIPTOR_HANDLE(const D3D12_CPU_DESCRIPTOR_HANDLE &o) :
  1013. D3D12_CPU_DESCRIPTOR_HANDLE(o)
  1014. {}
  1015. CD3DX12_CPU_DESCRIPTOR_HANDLE(CD3DX12_DEFAULT) { ptr = 0; }
  1016. CD3DX12_CPU_DESCRIPTOR_HANDLE(_In_ const D3D12_CPU_DESCRIPTOR_HANDLE &other, INT offsetScaledByIncrementSize)
  1017. {
  1018. InitOffsetted(other, offsetScaledByIncrementSize);
  1019. }
  1020. CD3DX12_CPU_DESCRIPTOR_HANDLE(_In_ const D3D12_CPU_DESCRIPTOR_HANDLE &other, INT offsetInDescriptors, UINT descriptorIncrementSize)
  1021. {
  1022. InitOffsetted(other, offsetInDescriptors, descriptorIncrementSize);
  1023. }
  1024. CD3DX12_CPU_DESCRIPTOR_HANDLE& Offset(INT offsetInDescriptors, UINT descriptorIncrementSize)
  1025. {
  1026. ptr += offsetInDescriptors * descriptorIncrementSize;
  1027. return *this;
  1028. }
  1029. CD3DX12_CPU_DESCRIPTOR_HANDLE& Offset(INT offsetScaledByIncrementSize)
  1030. {
  1031. ptr += offsetScaledByIncrementSize;
  1032. return *this;
  1033. }
  1034. bool operator==(_In_ const D3D12_CPU_DESCRIPTOR_HANDLE& other)
  1035. {
  1036. return (ptr == other.ptr);
  1037. }
  1038. bool operator!=(_In_ const D3D12_CPU_DESCRIPTOR_HANDLE& other)
  1039. {
  1040. return (ptr != other.ptr);
  1041. }
  1042. CD3DX12_CPU_DESCRIPTOR_HANDLE &operator=(const D3D12_CPU_DESCRIPTOR_HANDLE &other)
  1043. {
  1044. ptr = other.ptr;
  1045. return *this;
  1046. }
  1047. inline void InitOffsetted(_In_ const D3D12_CPU_DESCRIPTOR_HANDLE &base, INT offsetScaledByIncrementSize)
  1048. {
  1049. InitOffsetted(*this, base, offsetScaledByIncrementSize);
  1050. }
  1051. inline void InitOffsetted(_In_ const D3D12_CPU_DESCRIPTOR_HANDLE &base, INT offsetInDescriptors, UINT descriptorIncrementSize)
  1052. {
  1053. InitOffsetted(*this, base, offsetInDescriptors, descriptorIncrementSize);
  1054. }
  1055. static inline void InitOffsetted(_Out_ D3D12_CPU_DESCRIPTOR_HANDLE &handle, _In_ const D3D12_CPU_DESCRIPTOR_HANDLE &base, INT offsetScaledByIncrementSize)
  1056. {
  1057. handle.ptr = base.ptr + offsetScaledByIncrementSize;
  1058. }
  1059. static inline void InitOffsetted(_Out_ D3D12_CPU_DESCRIPTOR_HANDLE &handle, _In_ const D3D12_CPU_DESCRIPTOR_HANDLE &base, INT offsetInDescriptors, UINT descriptorIncrementSize)
  1060. {
  1061. handle.ptr = base.ptr + offsetInDescriptors * descriptorIncrementSize;
  1062. }
  1063. };
  1064. //------------------------------------------------------------------------------------------------
  1065. struct CD3DX12_GPU_DESCRIPTOR_HANDLE : public D3D12_GPU_DESCRIPTOR_HANDLE
  1066. {
  1067. CD3DX12_GPU_DESCRIPTOR_HANDLE() {}
  1068. explicit CD3DX12_GPU_DESCRIPTOR_HANDLE(const D3D12_GPU_DESCRIPTOR_HANDLE &o) :
  1069. D3D12_GPU_DESCRIPTOR_HANDLE(o)
  1070. {}
  1071. CD3DX12_GPU_DESCRIPTOR_HANDLE(CD3DX12_DEFAULT) { ptr = 0; }
  1072. CD3DX12_GPU_DESCRIPTOR_HANDLE(_In_ const D3D12_GPU_DESCRIPTOR_HANDLE &other, INT offsetScaledByIncrementSize)
  1073. {
  1074. InitOffsetted(other, offsetScaledByIncrementSize);
  1075. }
  1076. CD3DX12_GPU_DESCRIPTOR_HANDLE(_In_ const D3D12_GPU_DESCRIPTOR_HANDLE &other, INT offsetInDescriptors, UINT descriptorIncrementSize)
  1077. {
  1078. InitOffsetted(other, offsetInDescriptors, descriptorIncrementSize);
  1079. }
  1080. CD3DX12_GPU_DESCRIPTOR_HANDLE& Offset(INT offsetInDescriptors, UINT descriptorIncrementSize)
  1081. {
  1082. ptr += offsetInDescriptors * descriptorIncrementSize;
  1083. return *this;
  1084. }
  1085. CD3DX12_GPU_DESCRIPTOR_HANDLE& Offset(INT offsetScaledByIncrementSize)
  1086. {
  1087. ptr += offsetScaledByIncrementSize;
  1088. return *this;
  1089. }
  1090. inline bool operator==(_In_ const D3D12_GPU_DESCRIPTOR_HANDLE& other)
  1091. {
  1092. return (ptr == other.ptr);
  1093. }
  1094. inline bool operator!=(_In_ const D3D12_GPU_DESCRIPTOR_HANDLE& other)
  1095. {
  1096. return (ptr != other.ptr);
  1097. }
  1098. CD3DX12_GPU_DESCRIPTOR_HANDLE &operator=(const D3D12_GPU_DESCRIPTOR_HANDLE &other)
  1099. {
  1100. ptr = other.ptr;
  1101. return *this;
  1102. }
  1103. inline void InitOffsetted(_In_ const D3D12_GPU_DESCRIPTOR_HANDLE &base, INT offsetScaledByIncrementSize)
  1104. {
  1105. InitOffsetted(*this, base, offsetScaledByIncrementSize);
  1106. }
  1107. inline void InitOffsetted(_In_ const D3D12_GPU_DESCRIPTOR_HANDLE &base, INT offsetInDescriptors, UINT descriptorIncrementSize)
  1108. {
  1109. InitOffsetted(*this, base, offsetInDescriptors, descriptorIncrementSize);
  1110. }
  1111. static inline void InitOffsetted(_Out_ D3D12_GPU_DESCRIPTOR_HANDLE &handle, _In_ const D3D12_GPU_DESCRIPTOR_HANDLE &base, INT offsetScaledByIncrementSize)
  1112. {
  1113. handle.ptr = base.ptr + offsetScaledByIncrementSize;
  1114. }
  1115. static inline void InitOffsetted(_Out_ D3D12_GPU_DESCRIPTOR_HANDLE &handle, _In_ const D3D12_GPU_DESCRIPTOR_HANDLE &base, INT offsetInDescriptors, UINT descriptorIncrementSize)
  1116. {
  1117. handle.ptr = base.ptr + offsetInDescriptors * descriptorIncrementSize;
  1118. }
  1119. };
  1120. //------------------------------------------------------------------------------------------------
  1121. inline UINT D3D12CalcSubresource( UINT MipSlice, UINT ArraySlice, UINT PlaneSlice, UINT MipLevels, UINT ArraySize )
  1122. {
  1123. return MipSlice + ArraySlice * MipLevels + PlaneSlice * MipLevels * ArraySize;
  1124. }
  1125. //------------------------------------------------------------------------------------------------
  1126. template <typename T, typename U, typename V>
  1127. inline void D3D12DecomposeSubresource( UINT Subresource, UINT MipLevels, UINT ArraySize, _Out_ T& MipSlice, _Out_ U& ArraySlice, _Out_ V& PlaneSlice )
  1128. {
  1129. MipSlice = static_cast<T>(Subresource % MipLevels);
  1130. ArraySlice = static_cast<U>((Subresource / MipLevels) % ArraySize);
  1131. PlaneSlice = static_cast<V>(Subresource / (MipLevels * ArraySize));
  1132. }
  1133. //------------------------------------------------------------------------------------------------
  1134. inline UINT8 D3D12GetFormatPlaneCount(
  1135. _In_ ID3D12Device* pDevice,
  1136. DXGI_FORMAT Format
  1137. )
  1138. {
  1139. D3D12_FEATURE_DATA_FORMAT_INFO formatInfo = {Format};
  1140. if (FAILED(pDevice->CheckFeatureSupport(D3D12_FEATURE_FORMAT_INFO, &formatInfo, sizeof(formatInfo))))
  1141. {
  1142. return 0;
  1143. }
  1144. return formatInfo.PlaneCount;
  1145. }
  1146. //------------------------------------------------------------------------------------------------
  1147. struct CD3DX12_RESOURCE_DESC : public D3D12_RESOURCE_DESC
  1148. {
  1149. CD3DX12_RESOURCE_DESC()
  1150. {}
  1151. explicit CD3DX12_RESOURCE_DESC( const D3D12_RESOURCE_DESC& o ) :
  1152. D3D12_RESOURCE_DESC( o )
  1153. {}
  1154. CD3DX12_RESOURCE_DESC(
  1155. D3D12_RESOURCE_DIMENSION dimension,
  1156. UINT64 alignment,
  1157. UINT64 width,
  1158. UINT height,
  1159. UINT16 depthOrArraySize,
  1160. UINT16 mipLevels,
  1161. DXGI_FORMAT format,
  1162. UINT sampleCount,
  1163. UINT sampleQuality,
  1164. D3D12_TEXTURE_LAYOUT layout,
  1165. D3D12_RESOURCE_FLAGS flags )
  1166. {
  1167. Dimension = dimension;
  1168. Alignment = alignment;
  1169. Width = width;
  1170. Height = height;
  1171. DepthOrArraySize = depthOrArraySize;
  1172. MipLevels = mipLevels;
  1173. Format = format;
  1174. SampleDesc.Count = sampleCount;
  1175. SampleDesc.Quality = sampleQuality;
  1176. Layout = layout;
  1177. Flags = flags;
  1178. }
  1179. static inline CD3DX12_RESOURCE_DESC Buffer(
  1180. const D3D12_RESOURCE_ALLOCATION_INFO& resAllocInfo,
  1181. D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE )
  1182. {
  1183. return CD3DX12_RESOURCE_DESC( D3D12_RESOURCE_DIMENSION_BUFFER, resAllocInfo.Alignment, resAllocInfo.SizeInBytes,
  1184. 1, 1, 1, DXGI_FORMAT_UNKNOWN, 1, 0, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, flags );
  1185. }
  1186. static inline CD3DX12_RESOURCE_DESC Buffer(
  1187. UINT64 width,
  1188. D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE,
  1189. UINT64 alignment = 0 )
  1190. {
  1191. return CD3DX12_RESOURCE_DESC( D3D12_RESOURCE_DIMENSION_BUFFER, alignment, width, 1, 1, 1,
  1192. DXGI_FORMAT_UNKNOWN, 1, 0, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, flags );
  1193. }
  1194. static inline CD3DX12_RESOURCE_DESC Tex1D(
  1195. DXGI_FORMAT format,
  1196. UINT64 width,
  1197. UINT16 arraySize = 1,
  1198. UINT16 mipLevels = 0,
  1199. D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE,
  1200. D3D12_TEXTURE_LAYOUT layout = D3D12_TEXTURE_LAYOUT_UNKNOWN,
  1201. UINT64 alignment = 0 )
  1202. {
  1203. return CD3DX12_RESOURCE_DESC( D3D12_RESOURCE_DIMENSION_TEXTURE1D, alignment, width, 1, arraySize,
  1204. mipLevels, format, 1, 0, layout, flags );
  1205. }
  1206. static inline CD3DX12_RESOURCE_DESC Tex2D(
  1207. DXGI_FORMAT format,
  1208. UINT64 width,
  1209. UINT height,
  1210. UINT16 arraySize = 1,
  1211. UINT16 mipLevels = 0,
  1212. UINT sampleCount = 1,
  1213. UINT sampleQuality = 0,
  1214. D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE,
  1215. D3D12_TEXTURE_LAYOUT layout = D3D12_TEXTURE_LAYOUT_UNKNOWN,
  1216. UINT64 alignment = 0 )
  1217. {
  1218. return CD3DX12_RESOURCE_DESC( D3D12_RESOURCE_DIMENSION_TEXTURE2D, alignment, width, height, arraySize,
  1219. mipLevels, format, sampleCount, sampleQuality, layout, flags );
  1220. }
  1221. static inline CD3DX12_RESOURCE_DESC Tex3D(
  1222. DXGI_FORMAT format,
  1223. UINT64 width,
  1224. UINT height,
  1225. UINT16 depth,
  1226. UINT16 mipLevels = 0,
  1227. D3D12_RESOURCE_FLAGS flags = D3D12_RESOURCE_FLAG_NONE,
  1228. D3D12_TEXTURE_LAYOUT layout = D3D12_TEXTURE_LAYOUT_UNKNOWN,
  1229. UINT64 alignment = 0 )
  1230. {
  1231. return CD3DX12_RESOURCE_DESC( D3D12_RESOURCE_DIMENSION_TEXTURE3D, alignment, width, height, depth,
  1232. mipLevels, format, 1, 0, layout, flags );
  1233. }
  1234. inline UINT16 Depth() const
  1235. { return (Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D ? DepthOrArraySize : 1); }
  1236. inline UINT16 ArraySize() const
  1237. { return (Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE3D ? DepthOrArraySize : 1); }
  1238. inline UINT8 PlaneCount(_In_ ID3D12Device* pDevice) const
  1239. { return D3D12GetFormatPlaneCount(pDevice, Format); }
  1240. inline UINT Subresources(_In_ ID3D12Device* pDevice) const
  1241. { return MipLevels * ArraySize() * PlaneCount(pDevice); }
  1242. inline UINT CalcSubresource(UINT MipSlice, UINT ArraySlice, UINT PlaneSlice)
  1243. { return D3D12CalcSubresource(MipSlice, ArraySlice, PlaneSlice, MipLevels, ArraySize()); }
  1244. operator const D3D12_RESOURCE_DESC&() const { return *this; }
  1245. };
  1246. inline bool operator==( const D3D12_RESOURCE_DESC& l, const D3D12_RESOURCE_DESC& r )
  1247. {
  1248. return l.Dimension == r.Dimension &&
  1249. l.Alignment == r.Alignment &&
  1250. l.Width == r.Width &&
  1251. l.Height == r.Height &&
  1252. l.DepthOrArraySize == r.DepthOrArraySize &&
  1253. l.MipLevels == r.MipLevels &&
  1254. l.Format == r.Format &&
  1255. l.SampleDesc.Count == r.SampleDesc.Count &&
  1256. l.SampleDesc.Quality == r.SampleDesc.Quality &&
  1257. l.Layout == r.Layout &&
  1258. l.Flags == r.Flags;
  1259. }
  1260. inline bool operator!=( const D3D12_RESOURCE_DESC& l, const D3D12_RESOURCE_DESC& r )
  1261. { return !( l == r ); }
  1262. //------------------------------------------------------------------------------------------------
  1263. // Row-by-row memcpy
  1264. inline void MemcpySubresource(
  1265. _In_ const D3D12_MEMCPY_DEST* pDest,
  1266. _In_ const D3D12_SUBRESOURCE_DATA* pSrc,
  1267. SIZE_T RowSizeInBytes,
  1268. UINT NumRows,
  1269. UINT NumSlices)
  1270. {
  1271. for (UINT z = 0; z < NumSlices; ++z)
  1272. {
  1273. BYTE* pDestSlice = reinterpret_cast<BYTE*>(pDest->pData) + pDest->SlicePitch * z;
  1274. const BYTE* pSrcSlice = reinterpret_cast<const BYTE*>(pSrc->pData) + pSrc->SlicePitch * z;
  1275. for (UINT y = 0; y < NumRows; ++y)
  1276. {
  1277. memcpy(pDestSlice + pDest->RowPitch * y,
  1278. pSrcSlice + pSrc->RowPitch * y,
  1279. RowSizeInBytes);
  1280. }
  1281. }
  1282. }
  1283. //------------------------------------------------------------------------------------------------
  1284. // Returns required size of a buffer to be used for data upload
  1285. inline UINT64 GetRequiredIntermediateSize(
  1286. _In_ ID3D12Resource* pDestinationResource,
  1287. _In_range_(0,D3D12_REQ_SUBRESOURCES) UINT FirstSubresource,
  1288. _In_range_(0,D3D12_REQ_SUBRESOURCES-FirstSubresource) UINT NumSubresources)
  1289. {
  1290. D3D12_RESOURCE_DESC Desc = pDestinationResource->GetDesc();
  1291. UINT64 RequiredSize = 0;
  1292. ID3D12Device* pDevice;
  1293. pDestinationResource->GetDevice(__uuidof(*pDevice), reinterpret_cast<void**>(&pDevice));
  1294. pDevice->GetCopyableFootprints(&Desc, FirstSubresource, NumSubresources, 0, nullptr, nullptr, nullptr, &RequiredSize);
  1295. pDevice->Release();
  1296. return RequiredSize;
  1297. }
  1298. //------------------------------------------------------------------------------------------------
  1299. // All arrays must be populated (e.g. by calling GetCopyableFootprints)
  1300. inline UINT64 UpdateSubresources(
  1301. _In_ ID3D12GraphicsCommandList* pCmdList,
  1302. _In_ ID3D12Resource* pDestinationResource,
  1303. _In_ ID3D12Resource* pIntermediate,
  1304. _In_range_(0,D3D12_REQ_SUBRESOURCES) UINT FirstSubresource,
  1305. _In_range_(0,D3D12_REQ_SUBRESOURCES-FirstSubresource) UINT NumSubresources,
  1306. UINT64 RequiredSize,
  1307. _In_reads_(NumSubresources) const D3D12_PLACED_SUBRESOURCE_FOOTPRINT* pLayouts,
  1308. _In_reads_(NumSubresources) const UINT* pNumRows,
  1309. _In_reads_(NumSubresources) const UINT64* pRowSizesInBytes,
  1310. _In_reads_(NumSubresources) const D3D12_SUBRESOURCE_DATA* pSrcData)
  1311. {
  1312. // Minor validation
  1313. D3D12_RESOURCE_DESC IntermediateDesc = pIntermediate->GetDesc();
  1314. D3D12_RESOURCE_DESC DestinationDesc = pDestinationResource->GetDesc();
  1315. if (IntermediateDesc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER ||
  1316. IntermediateDesc.Width < RequiredSize + pLayouts[0].Offset ||
  1317. RequiredSize > (SIZE_T)-1 ||
  1318. (DestinationDesc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER &&
  1319. (FirstSubresource != 0 || NumSubresources != 1)))
  1320. {
  1321. return 0;
  1322. }
  1323. BYTE* pData;
  1324. HRESULT hr = pIntermediate->Map(0, NULL, reinterpret_cast<void**>(&pData));
  1325. if (FAILED(hr))
  1326. {
  1327. return 0;
  1328. }
  1329. for (UINT i = 0; i < NumSubresources; ++i)
  1330. {
  1331. if (pRowSizesInBytes[i] > (SIZE_T)-1) return 0;
  1332. D3D12_MEMCPY_DEST DestData = { pData + pLayouts[i].Offset, pLayouts[i].Footprint.RowPitch, pLayouts[i].Footprint.RowPitch * pNumRows[i] };
  1333. MemcpySubresource(&DestData, &pSrcData[i], (SIZE_T)pRowSizesInBytes[i], pNumRows[i], pLayouts[i].Footprint.Depth);
  1334. }
  1335. pIntermediate->Unmap(0, NULL);
  1336. if (DestinationDesc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER)
  1337. {
  1338. CD3DX12_BOX SrcBox( UINT( pLayouts[0].Offset ), UINT( pLayouts[0].Offset + pLayouts[0].Footprint.Width ) );
  1339. pCmdList->CopyBufferRegion(
  1340. pDestinationResource, 0, pIntermediate, pLayouts[0].Offset, pLayouts[0].Footprint.Width);
  1341. }
  1342. else
  1343. {
  1344. for (UINT i = 0; i < NumSubresources; ++i)
  1345. {
  1346. CD3DX12_TEXTURE_COPY_LOCATION Dst(pDestinationResource, i + FirstSubresource);
  1347. CD3DX12_TEXTURE_COPY_LOCATION Src(pIntermediate, pLayouts[i]);
  1348. pCmdList->CopyTextureRegion(&Dst, 0, 0, 0, &Src, nullptr);
  1349. }
  1350. }
  1351. return RequiredSize;
  1352. }
  1353. //------------------------------------------------------------------------------------------------
  1354. // Heap-allocating UpdateSubresources implementation
  1355. inline UINT64 UpdateSubresources(
  1356. _In_ ID3D12GraphicsCommandList* pCmdList,
  1357. _In_ ID3D12Resource* pDestinationResource,
  1358. _In_ ID3D12Resource* pIntermediate,
  1359. UINT64 IntermediateOffset,
  1360. _In_range_(0,D3D12_REQ_SUBRESOURCES) UINT FirstSubresource,
  1361. _In_range_(0,D3D12_REQ_SUBRESOURCES-FirstSubresource) UINT NumSubresources,
  1362. _In_reads_(NumSubresources) const D3D12_SUBRESOURCE_DATA* pSrcData)
  1363. {
  1364. UINT64 RequiredSize = 0;
  1365. UINT64 MemToAlloc = static_cast<UINT64>(sizeof(D3D12_PLACED_SUBRESOURCE_FOOTPRINT) + sizeof(UINT) + sizeof(UINT64)) * NumSubresources;
  1366. if (MemToAlloc > SIZE_MAX)
  1367. {
  1368. return 0;
  1369. }
  1370. void* pMem = HeapAlloc(GetProcessHeap(), 0, static_cast<SIZE_T>(MemToAlloc));
  1371. if (pMem == NULL)
  1372. {
  1373. return 0;
  1374. }
  1375. D3D12_PLACED_SUBRESOURCE_FOOTPRINT* pLayouts = reinterpret_cast<D3D12_PLACED_SUBRESOURCE_FOOTPRINT*>(pMem);
  1376. UINT64* pRowSizesInBytes = reinterpret_cast<UINT64*>(pLayouts + NumSubresources);
  1377. UINT* pNumRows = reinterpret_cast<UINT*>(pRowSizesInBytes + NumSubresources);
  1378. D3D12_RESOURCE_DESC Desc = pDestinationResource->GetDesc();
  1379. ID3D12Device* pDevice;
  1380. pDestinationResource->GetDevice(__uuidof(*pDevice), reinterpret_cast<void**>(&pDevice));
  1381. pDevice->GetCopyableFootprints(&Desc, FirstSubresource, NumSubresources, IntermediateOffset, pLayouts, pNumRows, pRowSizesInBytes, &RequiredSize);
  1382. pDevice->Release();
  1383. UINT64 Result = UpdateSubresources(pCmdList, pDestinationResource, pIntermediate, FirstSubresource, NumSubresources, RequiredSize, pLayouts, pNumRows, pRowSizesInBytes, pSrcData);
  1384. HeapFree(GetProcessHeap(), 0, pMem);
  1385. return Result;
  1386. }
  1387. //------------------------------------------------------------------------------------------------
  1388. // Stack-allocating UpdateSubresources implementation
  1389. template <UINT MaxSubresources>
  1390. inline UINT64 UpdateSubresources(
  1391. _In_ ID3D12GraphicsCommandList* pCmdList,
  1392. _In_ ID3D12Resource* pDestinationResource,
  1393. _In_ ID3D12Resource* pIntermediate,
  1394. UINT64 IntermediateOffset,
  1395. _In_range_(0, MaxSubresources) UINT FirstSubresource,
  1396. _In_range_(1, MaxSubresources - FirstSubresource) UINT NumSubresources,
  1397. _In_reads_(NumSubresources) D3D12_SUBRESOURCE_DATA* pSrcData)
  1398. {
  1399. UINT64 RequiredSize = 0;
  1400. D3D12_PLACED_SUBRESOURCE_FOOTPRINT Layouts[MaxSubresources];
  1401. UINT NumRows[MaxSubresources];
  1402. UINT64 RowSizesInBytes[MaxSubresources];
  1403. D3D12_RESOURCE_DESC Desc = pDestinationResource->GetDesc();
  1404. ID3D12Device* pDevice;
  1405. pDestinationResource->GetDevice(__uuidof(*pDevice), reinterpret_cast<void**>(&pDevice));
  1406. pDevice->GetCopyableFootprints(&Desc, FirstSubresource, NumSubresources, IntermediateOffset, Layouts, NumRows, RowSizesInBytes, &RequiredSize);
  1407. pDevice->Release();
  1408. return UpdateSubresources(pCmdList, pDestinationResource, pIntermediate, FirstSubresource, NumSubresources, RequiredSize, Layouts, NumRows, RowSizesInBytes, pSrcData);
  1409. }
  1410. //------------------------------------------------------------------------------------------------
  1411. inline bool D3D12IsLayoutOpaque( D3D12_TEXTURE_LAYOUT Layout )
  1412. { return Layout == D3D12_TEXTURE_LAYOUT_UNKNOWN || Layout == D3D12_TEXTURE_LAYOUT_64KB_UNDEFINED_SWIZZLE; }
  1413. //------------------------------------------------------------------------------------------------
  1414. inline ID3D12CommandList * const * CommandListCast(ID3D12GraphicsCommandList * const * pp)
  1415. {
  1416. // This cast is useful for passing strongly typed command list pointers into
  1417. // ExecuteCommandLists.
  1418. // This cast is valid as long as the const-ness is respected. D3D12 APIs do
  1419. // respect the const-ness of their arguments.
  1420. return reinterpret_cast<ID3D12CommandList * const *>(pp);
  1421. }
  1422. #endif // defined( __cplusplus )
  1423. #endif //__D3DX12_H__
  1424. #ifdef AZ_COMPILER_CLANG
  1425. #pragma clang diagnostic pop
  1426. #endif