2
0

dx12.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. #define HL_NAME(n) dx12_##n
  2. #include <hl.h>
  3. #ifdef HL_WIN_DESKTOP
  4. #include <dxgi.h>
  5. #include <dxgi1_5.h>
  6. #include <d3d12.h>
  7. #include <dxcapi.h>
  8. #endif
  9. #ifdef HL_XBS
  10. #ifdef _GAMING_XBOX_SCARLETT
  11. #include <d3d12_xs.h>
  12. #include <d3dx12_xs.h>
  13. #include <dxcapi_xs.h>
  14. #else
  15. #include <d3d12_x.h>
  16. #include <d3dx12_x.h>
  17. #include <dxcapi_x.h>
  18. #include <vector>
  19. #endif
  20. #define IID_PPV_ARGS_OLD(ppType) __uuidof(**(ppType)), IID_PPV_ARGS_Helper(ppType)
  21. #undef IID_PPV_ARGS
  22. #define IID_PPV_ARGS IID_GRAPHICS_PPV_ARGS
  23. #endif
  24. #define DXERR(cmd) { HRESULT __ret = cmd; if( __ret == E_OUTOFMEMORY ) return NULL; if( __ret != S_OK ) ReportDxError(__ret,__LINE__); }
  25. #define CHKERR(cmd) { HRESULT __ret = cmd; if( __ret != S_OK ) ReportDxError(__ret,__LINE__); }
  26. typedef struct {
  27. HWND wnd;
  28. ID3D12CommandQueue *commandQueue;
  29. #ifndef HL_XBS
  30. IDXGIFactory4 *factory;
  31. IDXGIAdapter1 *adapter;
  32. IDXGISwapChain4 *swapchain;
  33. ID3D12Device *device;
  34. ID3D12Debug1 *debug;
  35. ID3D12DebugDevice *debugDevice;
  36. ID3D12InfoQueue *infoQueue;
  37. #else
  38. ID3D12Device2 *device;
  39. ID3D12Debug *debug;
  40. ID3D12DebugDevice *debugDevice;
  41. // SwapChain
  42. UINT swapBufferCount;
  43. UINT backBufferIndex;
  44. ID3D12Resource **swapBuffers;
  45. D3D12XBOX_FRAME_PIPELINE_TOKEN pipelineToken;
  46. // Cached values
  47. IDXGIAdapter *adapter;
  48. #endif
  49. } dx_driver;
  50. static dx_driver *static_driver = NULL;
  51. static int CURRENT_NODEMASK = 0;
  52. HL_PRIM void dx12_flush_messages();
  53. static void ReportDxError( HRESULT err, int line ) {
  54. dx12_flush_messages();
  55. hl_error("DXERROR %X line %d",(DWORD)err,line);
  56. }
  57. static void OnDebugMessage(
  58. D3D12_MESSAGE_CATEGORY Category,
  59. D3D12_MESSAGE_SEVERITY Severity,
  60. D3D12_MESSAGE_ID ID,
  61. LPCSTR pDescription,
  62. void *pContext ) {
  63. printf("%s\n", pDescription);
  64. fflush(stdout);
  65. }
  66. HL_PRIM varray *HL_NAME(list_devices)() {
  67. #ifndef HL_XBS
  68. static int MAX_DEVICES = 64;
  69. int index = 0, write = 0;
  70. IDXGIAdapter1 *adapter = NULL;
  71. IDXGIFactory4 *factory = NULL;
  72. varray *arr = hl_alloc_array(&hlt_bytes, MAX_DEVICES);
  73. if( static_driver )
  74. factory = static_driver->factory;
  75. else {
  76. CHKERR(CreateDXGIFactory2(0, IID_PPV_ARGS(&factory)));
  77. }
  78. while( write < MAX_DEVICES && factory->EnumAdapters1(index++,&adapter) != DXGI_ERROR_NOT_FOUND ) {
  79. DXGI_ADAPTER_DESC1 desc;
  80. adapter->GetDesc1(&desc);
  81. if( (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) == 0 )
  82. hl_aptr(arr,uchar*)[write++] = ustrdup(desc.Description);
  83. adapter->Release();
  84. }
  85. if( !static_driver )
  86. factory->Release();
  87. return arr;
  88. #else
  89. varray *arr = hl_alloc_array(&hlt_bytes, 1);
  90. DXGI_ADAPTER_DESC desc;
  91. static_driver->adapter->GetDesc(&desc);
  92. hl_aptr(arr, uchar*)[0] = ustrdup(desc.Description);
  93. return arr;
  94. #endif
  95. }
  96. HL_PRIM dx_driver *HL_NAME(create)( HWND window, int flags, uchar *dev_desc ) {
  97. UINT dxgiFlags = 0;
  98. dx_driver *drv = (dx_driver*)hl_gc_alloc_raw(sizeof(dx_driver));
  99. memset(drv, 0, sizeof(dx_driver));
  100. drv->wnd = window;
  101. if( flags & 1 ) {
  102. ID3D12Debug *debugController;
  103. D3D12GetDebugInterface(IID_PPV_ARGS(&debugController));
  104. debugController->EnableDebugLayer();
  105. #ifndef HL_XBS
  106. debugController->QueryInterface(&drv->debug);
  107. drv->debug->EnableDebugLayer();
  108. drv->debug->SetEnableGPUBasedValidation(true);
  109. dxgiFlags |= DXGI_CREATE_FACTORY_DEBUG;
  110. #else
  111. debugController->QueryInterface(IID_PPV_ARGS(&drv->debug));
  112. #endif
  113. debugController->Release();
  114. }
  115. #ifndef HL_XBS
  116. CHKERR(CreateDXGIFactory2(dxgiFlags, IID_PPV_ARGS(&drv->factory)));
  117. UINT index = 0;
  118. IDXGIAdapter1 *adapter = NULL;
  119. while( drv->factory->EnumAdapters1(index++,&adapter) != DXGI_ERROR_NOT_FOUND ) {
  120. DXGI_ADAPTER_DESC1 desc;
  121. adapter->GetDesc1(&desc);
  122. if( (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) || (dev_desc && !wcsstr(desc.Description,dev_desc)) ) {
  123. adapter->Release();
  124. continue;
  125. }
  126. if( SUCCEEDED(D3D12CreateDevice(adapter,D3D_FEATURE_LEVEL_12_0,IID_PPV_ARGS(&drv->device))) ) {
  127. drv->adapter = adapter;
  128. break;
  129. }
  130. adapter->Release();
  131. }
  132. if( !drv->device )
  133. return NULL;
  134. drv->device->SetName(L"HL_DX12");
  135. if( drv->debug ) {
  136. CHKERR(drv->device->QueryInterface(IID_PPV_ARGS(&drv->debugDevice)));
  137. CHKERR(drv->device->QueryInterface(IID_PPV_ARGS(&drv->infoQueue)));
  138. drv->infoQueue->ClearStoredMessages();
  139. }
  140. #else
  141. D3D12XBOX_CREATE_DEVICE_PARAMETERS params = {};
  142. params.Version = D3D12_SDK_VERSION;
  143. #if defined(_DEBUG)
  144. params.ProcessDebugFlags = D3D12_PROCESS_DEBUG_FLAG_DEBUG_LAYER_ENABLED;
  145. #endif
  146. params.ProcessDebugFlags |= D3D12XBOX_PROCESS_DEBUG_FLAG_ENABLE_COMMON_STATE_PROMOTION;
  147. params.GraphicsCommandQueueRingSizeBytes = static_cast<UINT>(D3D12XBOX_DEFAULT_SIZE_BYTES);
  148. params.GraphicsScratchMemorySizeBytes = static_cast<UINT>(D3D12XBOX_DEFAULT_SIZE_BYTES);
  149. params.ComputeScratchMemorySizeBytes = static_cast<UINT>(D3D12XBOX_DEFAULT_SIZE_BYTES);
  150. params.DisableGeometryShaderAllocations = TRUE;
  151. params.DisableTessellationShaderAllocations = TRUE;
  152. #ifdef _GAMING_XBOX_SCARLETT
  153. params.DisableDXR = TRUE;
  154. params.CreateDeviceFlags = D3D12XBOX_CREATE_DEVICE_FLAG_NONE;
  155. #endif
  156. CHKERR(D3D12XboxCreateDevice(nullptr,&params, IID_PPV_ARGS(&drv->device)))
  157. drv->device->SetName(L"HL_DX12_XBS");
  158. // Prepare for PresentX
  159. {
  160. IDXGIDevice1 *dxgiDevice;
  161. CHKERR(drv->device->QueryInterface(IID_PPV_ARGS(&dxgiDevice)));
  162. IDXGIAdapter *dxgiAdapter;
  163. CHKERR(dxgiDevice->GetAdapter(&dxgiAdapter));
  164. DXGI_ADAPTER_DESC desc;
  165. dxgiAdapter->GetDesc(&desc);
  166. if (dev_desc) {
  167. wcsstr(desc.Description, dev_desc);
  168. }
  169. drv->adapter = dxgiAdapter;
  170. }
  171. #endif
  172. {
  173. D3D12_COMMAND_QUEUE_DESC desc = {};
  174. desc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
  175. desc.Priority = D3D12_COMMAND_QUEUE_PRIORITY_NORMAL;
  176. desc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
  177. desc.NodeMask = CURRENT_NODEMASK;
  178. CHKERR(drv->device->CreateCommandQueue(&desc, IID_PPV_ARGS(&drv->commandQueue)));
  179. }
  180. static_driver = drv;
  181. return drv;
  182. }
  183. #ifdef HL_XBS
  184. void register_frame_events() {
  185. dx_driver *drv = static_driver;
  186. // Prepare PresentX
  187. IDXGIOutput *dxgiOutput;
  188. CHKERR(drv->adapter->EnumOutputs(0, &dxgiOutput));
  189. // May return S_OK, S_FALSE
  190. HRESULT hr = drv->device->SetFrameIntervalX(dxgiOutput, D3D12XBOX_FRAME_INTERVAL_60_HZ, drv->swapBufferCount - 1u /* Allow n-1 frames of latency */, D3D12XBOX_FRAME_INTERVAL_FLAG_NONE);
  191. if (hr < 0) ReportDxError(hr, __LINE__);
  192. // May return 0x10000000 on XBOXONE
  193. hr = drv->device->ScheduleFrameEventX(D3D12XBOX_FRAME_EVENT_ORIGIN, 0U, nullptr, D3D12XBOX_SCHEDULE_FRAME_EVENT_FLAG_NONE);
  194. if (hr < 0) ReportDxError(hr, __LINE__);
  195. // Prepare first pipeline token
  196. drv->pipelineToken = D3D12XBOX_FRAME_PIPELINE_TOKEN_NULL;
  197. CHKERR(drv->device->WaitFrameEventX(D3D12XBOX_FRAME_EVENT_ORIGIN, INFINITE, nullptr, D3D12XBOX_WAIT_FRAME_EVENT_FLAG_NONE, &drv->pipelineToken));
  198. }
  199. #endif
  200. HL_PRIM void HL_NAME(suspend)() {
  201. #ifdef HL_XBS
  202. // Must be called from the render thread
  203. CHKERR(static_driver->commandQueue->SuspendX(0));
  204. #endif
  205. }
  206. HL_PRIM void HL_NAME(resume)() {
  207. #ifdef HL_XBS
  208. CHKERR(static_driver->commandQueue->ResumeX());
  209. register_frame_events();
  210. #endif
  211. }
  212. HL_PRIM void HL_NAME(resize)( int width, int height, int buffer_count, DXGI_FORMAT format ) {
  213. dx_driver *drv = static_driver;
  214. #ifndef HL_XBS
  215. if( drv->swapchain ) {
  216. CHKERR(drv->swapchain->ResizeBuffers(buffer_count, width, height, format, 0));
  217. } else {
  218. DXGI_SWAP_CHAIN_DESC1 desc = {};
  219. desc.Width = width;
  220. desc.Height = height;
  221. desc.Format = format;
  222. desc.BufferCount = buffer_count;
  223. desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  224. desc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
  225. desc.SampleDesc.Count = 1;
  226. IDXGISwapChain1 *swapchain = NULL;
  227. drv->factory->CreateSwapChainForHwnd(drv->commandQueue,drv->wnd,&desc,NULL,NULL,&swapchain);
  228. if( !swapchain ) CHKERR(E_INVALIDARG);
  229. swapchain->QueryInterface(IID_PPV_ARGS(&drv->swapchain));
  230. }
  231. #else
  232. if (drv->swapBuffers) {
  233. free(drv->swapBuffers);
  234. }
  235. // Create swap buffers
  236. CD3DX12_HEAP_PROPERTIES swapChainHeapProperties(D3D12_HEAP_TYPE_DEFAULT);
  237. D3D12_RESOURCE_DESC swapChainBufferDesc = CD3DX12_RESOURCE_DESC::Tex2D(format, width, height,
  238. 1, // This resource has only one texture.
  239. 1 // Use a single mipmap level.
  240. );
  241. swapChainBufferDesc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
  242. D3D12_CLEAR_VALUE swapChainOptimizedClearValue = {};
  243. swapChainOptimizedClearValue.Format = format;
  244. drv->swapBuffers = (ID3D12Resource**) malloc(sizeof(ID3D12Resource*) * buffer_count);
  245. if (drv->swapBuffers == nullptr)
  246. hl_error("malloc drv->swapBuffers failed");
  247. drv->swapBufferCount = buffer_count;
  248. drv->backBufferIndex = 0;
  249. for (int n = 0; n < buffer_count; n++)
  250. {
  251. drv->device->CreateCommittedResource(&swapChainHeapProperties, D3D12_HEAP_FLAG_ALLOW_DISPLAY, &swapChainBufferDesc, D3D12_RESOURCE_STATE_PRESENT, &swapChainOptimizedClearValue, IID_PPV_ARGS(&drv->swapBuffers[n]));
  252. }
  253. register_frame_events();
  254. #endif
  255. }
  256. HL_PRIM void HL_NAME(present)( bool vsync ) {
  257. dx_driver *drv = static_driver;
  258. #ifndef HL_XBS
  259. UINT syncInterval = vsync ? 1 : 0;
  260. UINT presentFlags = 0;
  261. CHKERR(drv->swapchain->Present(syncInterval, presentFlags));
  262. #else
  263. D3D12XBOX_PRESENT_PLANE_PARAMETERS planeParameters = {};
  264. planeParameters.Token = drv->pipelineToken;
  265. planeParameters.ResourceCount = 1;
  266. planeParameters.ppResources = &drv->swapBuffers[drv->backBufferIndex];
  267. CHKERR(drv->commandQueue->PresentX(1, &planeParameters, nullptr));
  268. drv->backBufferIndex = (drv->backBufferIndex + 1) % drv->swapBufferCount;
  269. // Prepare next pipeline token
  270. drv->pipelineToken = D3D12XBOX_FRAME_PIPELINE_TOKEN_NULL;
  271. CHKERR(drv->device->WaitFrameEventX(D3D12XBOX_FRAME_EVENT_ORIGIN, INFINITE, nullptr, D3D12XBOX_WAIT_FRAME_EVENT_FLAG_NONE, &drv->pipelineToken));
  272. #endif
  273. }
  274. HL_PRIM int HL_NAME(get_current_back_buffer_index)() {
  275. #ifndef HL_XBS
  276. return static_driver->swapchain->GetCurrentBackBufferIndex();
  277. #else
  278. return static_driver->backBufferIndex;
  279. #endif
  280. }
  281. HL_PRIM void HL_NAME(signal)( ID3D12Fence *fence, int64 value ) {
  282. static_driver->commandQueue->Signal(fence,value);
  283. }
  284. HL_PRIM void HL_NAME(flush_messages)() {
  285. #ifndef HL_XBS
  286. dx_driver *drv = static_driver;
  287. if( !drv->infoQueue ) return;
  288. int count = (int)drv->infoQueue->GetNumStoredMessages();
  289. if( !count ) return;
  290. int i;
  291. for(i=0;i<count;i++) {
  292. SIZE_T len = 0;
  293. drv->infoQueue->GetMessage(i, NULL, &len);
  294. D3D12_MESSAGE *msg = (D3D12_MESSAGE*)malloc(len);
  295. if( msg == NULL ) break;
  296. drv->infoQueue->GetMessage(i, msg, &len);
  297. printf("%s\n",msg->pDescription);
  298. free(msg);
  299. fflush(stdout);
  300. }
  301. drv->infoQueue->ClearStoredMessages();
  302. #endif
  303. }
  304. HL_PRIM uchar *HL_NAME(get_device_name)() {
  305. DXGI_ADAPTER_DESC desc;
  306. #ifndef HL_XBS
  307. IDXGIAdapter *adapter = NULL;
  308. if( !static_driver ) {
  309. IDXGIFactory4 *factory = NULL;
  310. CreateDXGIFactory2(0, IID_PPV_ARGS(&factory));
  311. if( factory ) factory->EnumAdapters(0,&adapter);
  312. if( !adapter )
  313. return USTR("Unknown");
  314. } else
  315. adapter = static_driver->adapter;
  316. adapter->GetDesc(&desc);
  317. #else
  318. static_driver->adapter->GetDesc(&desc);
  319. #endif
  320. return (uchar*)hl_copy_bytes((vbyte*)desc.Description, (int)(ustrlen((uchar*)desc.Description) + 1) * 2);
  321. }
  322. HL_PRIM int64 HL_NAME(get_timestamp_frequency)() {
  323. UINT64 f = 0;
  324. CHKERR(static_driver->commandQueue->GetTimestampFrequency(&f))
  325. return (int64)f;
  326. }
  327. #define _DRIVER _ABSTRACT(dx_driver)
  328. #define _RES _ABSTRACT(dx_resource)
  329. DEFINE_PRIM(_ARR, list_devices, _NO_ARG);
  330. DEFINE_PRIM(_DRIVER, create, _ABSTRACT(dx_window) _I32 _BYTES);
  331. DEFINE_PRIM(_VOID, resize, _I32 _I32 _I32 _I32);
  332. DEFINE_PRIM(_VOID, present, _BOOL);
  333. DEFINE_PRIM(_VOID, suspend, _NO_ARG);
  334. DEFINE_PRIM(_VOID, resume, _NO_ARG);
  335. DEFINE_PRIM(_I32, get_current_back_buffer_index, _NO_ARG);
  336. DEFINE_PRIM(_VOID, signal, _RES _I64);
  337. DEFINE_PRIM(_VOID, flush_messages, _NO_ARG);
  338. DEFINE_PRIM(_BYTES, get_device_name, _NO_ARG);
  339. DEFINE_PRIM(_I64, get_timestamp_frequency, _NO_ARG);
  340. /// --- utilities (from d3dx12.h)
  341. #ifndef HL_XBS
  342. struct CD3DX12_TEXTURE_COPY_LOCATION : public D3D12_TEXTURE_COPY_LOCATION
  343. {
  344. CD3DX12_TEXTURE_COPY_LOCATION() = default;
  345. explicit CD3DX12_TEXTURE_COPY_LOCATION(const D3D12_TEXTURE_COPY_LOCATION &o) noexcept :
  346. D3D12_TEXTURE_COPY_LOCATION(o)
  347. {}
  348. CD3DX12_TEXTURE_COPY_LOCATION(_In_ ID3D12Resource* pRes) noexcept
  349. {
  350. pResource = pRes;
  351. Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
  352. PlacedFootprint = {};
  353. }
  354. CD3DX12_TEXTURE_COPY_LOCATION(_In_ ID3D12Resource* pRes, D3D12_PLACED_SUBRESOURCE_FOOTPRINT const& Footprint) noexcept
  355. {
  356. pResource = pRes;
  357. Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT;
  358. PlacedFootprint = Footprint;
  359. }
  360. CD3DX12_TEXTURE_COPY_LOCATION(_In_ ID3D12Resource* pRes, UINT Sub) noexcept
  361. {
  362. pResource = pRes;
  363. Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;
  364. PlacedFootprint = {};
  365. SubresourceIndex = Sub;
  366. }
  367. };
  368. inline void MemcpySubresource(
  369. _In_ const D3D12_MEMCPY_DEST* pDest,
  370. _In_ const D3D12_SUBRESOURCE_DATA* pSrc,
  371. SIZE_T RowSizeInBytes,
  372. UINT NumRows,
  373. UINT NumSlices) noexcept
  374. {
  375. for (UINT z = 0; z < NumSlices; ++z)
  376. {
  377. auto pDestSlice = static_cast<BYTE*>(pDest->pData) + pDest->SlicePitch * z;
  378. auto pSrcSlice = static_cast<const BYTE*>(pSrc->pData) + pSrc->SlicePitch * LONG_PTR(z);
  379. for (UINT y = 0; y < NumRows; ++y)
  380. {
  381. memcpy(pDestSlice + pDest->RowPitch * y,
  382. pSrcSlice + pSrc->RowPitch * LONG_PTR(y),
  383. RowSizeInBytes);
  384. }
  385. }
  386. }
  387. inline UINT64 UpdateSubresources(
  388. _In_ ID3D12GraphicsCommandList* pCmdList,
  389. _In_ ID3D12Resource* pDestinationResource,
  390. _In_ ID3D12Resource* pIntermediate,
  391. _In_range_(0,D3D12_REQ_SUBRESOURCES) UINT FirstSubresource,
  392. _In_range_(0,D3D12_REQ_SUBRESOURCES-FirstSubresource) UINT NumSubresources,
  393. UINT64 RequiredSize,
  394. _In_reads_(NumSubresources) const D3D12_PLACED_SUBRESOURCE_FOOTPRINT* pLayouts,
  395. _In_reads_(NumSubresources) const UINT* pNumRows,
  396. _In_reads_(NumSubresources) const UINT64* pRowSizesInBytes,
  397. _In_reads_(NumSubresources) const D3D12_SUBRESOURCE_DATA* pSrcData) noexcept
  398. {
  399. // Minor validation
  400. auto IntermediateDesc = pIntermediate->GetDesc();
  401. auto DestinationDesc = pDestinationResource->GetDesc();
  402. if (IntermediateDesc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER ||
  403. IntermediateDesc.Width < RequiredSize + pLayouts[0].Offset ||
  404. RequiredSize > SIZE_T(-1) ||
  405. (DestinationDesc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER &&
  406. (FirstSubresource != 0 || NumSubresources != 1)))
  407. {
  408. return 0;
  409. }
  410. BYTE* pData;
  411. HRESULT hr = pIntermediate->Map(0, nullptr, reinterpret_cast<void**>(&pData));
  412. if (FAILED(hr))
  413. {
  414. return 0;
  415. }
  416. for (UINT i = 0; i < NumSubresources; ++i)
  417. {
  418. if (pRowSizesInBytes[i] > SIZE_T(-1)) return 0;
  419. D3D12_MEMCPY_DEST DestData = { pData + pLayouts[i].Offset, pLayouts[i].Footprint.RowPitch, SIZE_T(pLayouts[i].Footprint.RowPitch) * SIZE_T(pNumRows[i]) };
  420. MemcpySubresource(&DestData, &pSrcData[i], static_cast<SIZE_T>(pRowSizesInBytes[i]), pNumRows[i], pLayouts[i].Footprint.Depth);
  421. }
  422. pIntermediate->Unmap(0, nullptr);
  423. if (DestinationDesc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER)
  424. {
  425. pCmdList->CopyBufferRegion(
  426. pDestinationResource, 0, pIntermediate, pLayouts[0].Offset, pLayouts[0].Footprint.Width);
  427. }
  428. else
  429. {
  430. for (UINT i = 0; i < NumSubresources; ++i)
  431. {
  432. CD3DX12_TEXTURE_COPY_LOCATION Dst(pDestinationResource, i + FirstSubresource);
  433. CD3DX12_TEXTURE_COPY_LOCATION Src(pIntermediate, pLayouts[i]);
  434. pCmdList->CopyTextureRegion(&Dst, 0, 0, 0, &Src, nullptr);
  435. }
  436. }
  437. return RequiredSize;
  438. }
  439. inline UINT64 UpdateSubresources(
  440. _In_ ID3D12GraphicsCommandList* pCmdList,
  441. _In_ ID3D12Resource* pDestinationResource,
  442. _In_ ID3D12Resource* pIntermediate,
  443. UINT64 IntermediateOffset,
  444. _In_range_(0,D3D12_REQ_SUBRESOURCES) UINT FirstSubresource,
  445. _In_range_(0,D3D12_REQ_SUBRESOURCES-FirstSubresource) UINT NumSubresources,
  446. _In_reads_(NumSubresources) const D3D12_SUBRESOURCE_DATA* pSrcData) noexcept
  447. {
  448. UINT64 RequiredSize = 0;
  449. auto MemToAlloc = static_cast<UINT64>(sizeof(D3D12_PLACED_SUBRESOURCE_FOOTPRINT) + sizeof(UINT) + sizeof(UINT64)) * NumSubresources;
  450. if (MemToAlloc > SIZE_MAX)
  451. {
  452. return 0;
  453. }
  454. void* pMem = HeapAlloc(GetProcessHeap(), 0, static_cast<SIZE_T>(MemToAlloc));
  455. if (pMem == nullptr)
  456. {
  457. return 0;
  458. }
  459. auto pLayouts = static_cast<D3D12_PLACED_SUBRESOURCE_FOOTPRINT*>(pMem);
  460. auto pRowSizesInBytes = reinterpret_cast<UINT64*>(pLayouts + NumSubresources);
  461. auto pNumRows = reinterpret_cast<UINT*>(pRowSizesInBytes + NumSubresources);
  462. auto Desc = pDestinationResource->GetDesc();
  463. static_driver->device->GetCopyableFootprints(&Desc, FirstSubresource, NumSubresources, IntermediateOffset, pLayouts, pNumRows, pRowSizesInBytes, &RequiredSize);
  464. UINT64 Result = UpdateSubresources(pCmdList, pDestinationResource, pIntermediate, FirstSubresource, NumSubresources, RequiredSize, pLayouts, pNumRows, pRowSizesInBytes, pSrcData);
  465. HeapFree(GetProcessHeap(), 0, pMem);
  466. return Result;
  467. }
  468. #endif
  469. // ---- RESOURCES
  470. HL_PRIM ID3D12Resource *HL_NAME(get_back_buffer)( int index ) {
  471. ID3D12Resource *buf = NULL;
  472. #ifndef HL_XBS
  473. static_driver->swapchain->GetBuffer(index, IID_PPV_ARGS(&buf));
  474. #else
  475. buf = static_driver->swapBuffers[index];
  476. #endif
  477. return buf;
  478. }
  479. HL_PRIM ID3D12Resource *HL_NAME(create_committed_resource)( D3D12_HEAP_PROPERTIES *heapProperties, D3D12_HEAP_FLAGS heapFlags, D3D12_RESOURCE_DESC *desc, D3D12_RESOURCE_STATES initialState, D3D12_CLEAR_VALUE *clearValue ) {
  480. ID3D12Resource *res = NULL;
  481. #ifdef HL_XBS
  482. // In normal dx, INDIRECT_ARGUMENT is included in GENERIC_READ (but we never use it alone) , so we remove it to obtain GENERIC_READ in xbox
  483. initialState = (D3D12_RESOURCE_STATES)(initialState & ~D3D12_RESOURCE_STATE_INDIRECT_ARGUMENT);
  484. #endif
  485. DXERR(static_driver->device->CreateCommittedResource(heapProperties, heapFlags, desc, initialState, clearValue, IID_PPV_ARGS(&res)));
  486. return res;
  487. }
  488. HL_PRIM void HL_NAME(create_render_target_view)( ID3D12Resource *res, D3D12_RENDER_TARGET_VIEW_DESC *desc, D3D12_CPU_DESCRIPTOR_HANDLE descriptor ) {
  489. static_driver->device->CreateRenderTargetView(res,desc,descriptor);
  490. }
  491. HL_PRIM void HL_NAME(create_depth_stencil_view)( ID3D12Resource *res, D3D12_DEPTH_STENCIL_VIEW_DESC *desc, D3D12_CPU_DESCRIPTOR_HANDLE descriptor ) {
  492. static_driver->device->CreateDepthStencilView(res,desc,descriptor);
  493. }
  494. HL_PRIM void HL_NAME(create_constant_buffer_view)( D3D12_CONSTANT_BUFFER_VIEW_DESC *desc, D3D12_CPU_DESCRIPTOR_HANDLE descriptor ) {
  495. static_driver->device->CreateConstantBufferView(desc,descriptor);
  496. }
  497. HL_PRIM void HL_NAME(create_unordered_access_view)( ID3D12Resource *res, ID3D12Resource *counter, D3D12_UNORDERED_ACCESS_VIEW_DESC *desc, D3D12_CPU_DESCRIPTOR_HANDLE descriptor ) {
  498. static_driver->device->CreateUnorderedAccessView(res,counter,desc,descriptor);
  499. }
  500. HL_PRIM void HL_NAME(create_sampler)( D3D12_SAMPLER_DESC *desc, D3D12_CPU_DESCRIPTOR_HANDLE descriptor ) {
  501. static_driver->device->CreateSampler(desc,descriptor);
  502. }
  503. HL_PRIM void HL_NAME(create_shader_resource_view)( ID3D12Resource *res, D3D12_SHADER_RESOURCE_VIEW_DESC *desc, D3D12_CPU_DESCRIPTOR_HANDLE descriptor ) {
  504. static_driver->device->CreateShaderResourceView(res,desc,descriptor);
  505. }
  506. HL_PRIM int64 HL_NAME(resource_get_gpu_virtual_address)( ID3D12Resource *res ) {
  507. return res->GetGPUVirtualAddress();
  508. }
  509. HL_PRIM void HL_NAME(resource_release)( IUnknown *res ) {
  510. res->Release();
  511. }
  512. HL_PRIM void HL_NAME(resource_set_name)( ID3D12Resource *res, vbyte *name ) {
  513. res->SetName((LPCWSTR)name);
  514. }
  515. HL_PRIM void *HL_NAME(resource_map)( ID3D12Resource *res, int subres, D3D12_RANGE *range ) {
  516. void *data = NULL;
  517. DXERR(res->Map(subres, range, &data));
  518. return data;
  519. }
  520. HL_PRIM void HL_NAME(resource_unmap)( ID3D12Resource *res, int subres, D3D12_RANGE *range ) {
  521. res->Unmap(subres, range);
  522. }
  523. HL_PRIM int64 HL_NAME(get_required_intermediate_size)( ID3D12Resource *res, int first, int count ) {
  524. auto desc = res->GetDesc();
  525. UINT64 size = 0;
  526. #ifndef HL_XBS
  527. static_driver->device->GetCopyableFootprints(&desc, first, count, 0, NULL, NULL, NULL, &size);
  528. #else
  529. D3D12_PLACED_SUBRESOURCE_FOOTPRINT pLayouts;
  530. static_driver->device->GetCopyableFootprints(&desc, first + count - 1, 1, 0, &pLayouts, NULL, NULL, &size);
  531. size += pLayouts.Offset;
  532. #endif
  533. return size;
  534. }
  535. HL_PRIM bool HL_NAME(update_sub_resource)( ID3D12GraphicsCommandList *cmd, ID3D12Resource *res, ID3D12Resource *tmp, int64 tmpOffs, int first, int count, D3D12_SUBRESOURCE_DATA *data ) {
  536. #ifdef HL_XBS
  537. tmpOffs = 0;
  538. #endif
  539. return UpdateSubresources(cmd,res,tmp,(UINT64)tmpOffs,(UINT)first,(UINT)count,data) != 0;
  540. }
  541. HL_PRIM void HL_NAME(get_copyable_footprints)( D3D12_RESOURCE_DESC *desc, int first, int count, int64 offset, D3D12_PLACED_SUBRESOURCE_FOOTPRINT *layouts, int *numRows, int64 *rowSizes, int64 *totalBytes ) {
  542. static_driver->device->GetCopyableFootprints(desc, first, count, offset, layouts, (UINT*)numRows, (UINT64*)rowSizes, (UINT64*)totalBytes);
  543. }
  544. DEFINE_PRIM(_VOID, create_render_target_view, _RES _STRUCT _I64);
  545. DEFINE_PRIM(_VOID, create_depth_stencil_view, _RES _STRUCT _I64);
  546. DEFINE_PRIM(_VOID, create_shader_resource_view, _RES _STRUCT _I64);
  547. DEFINE_PRIM(_VOID, create_constant_buffer_view, _STRUCT _I64);
  548. DEFINE_PRIM(_VOID, create_unordered_access_view, _RES _RES _STRUCT _I64);
  549. DEFINE_PRIM(_VOID, create_sampler, _STRUCT _I64);
  550. DEFINE_PRIM(_RES, create_committed_resource, _STRUCT _I32 _STRUCT _I32 _STRUCT);
  551. DEFINE_PRIM(_RES, get_back_buffer, _I32);
  552. DEFINE_PRIM(_VOID, resource_release, _RES);
  553. DEFINE_PRIM(_VOID, resource_set_name, _RES _BYTES);
  554. DEFINE_PRIM(_I64, resource_get_gpu_virtual_address, _RES);
  555. DEFINE_PRIM(_BYTES, resource_map, _RES _I32 _STRUCT);
  556. DEFINE_PRIM(_VOID, resource_unmap, _RES _I32 _STRUCT);
  557. DEFINE_PRIM(_I64, get_required_intermediate_size, _RES _I32 _I32);
  558. DEFINE_PRIM(_BOOL, update_sub_resource, _RES _RES _RES _I64 _I32 _I32 _STRUCT);
  559. DEFINE_PRIM(_VOID, get_copyable_footprints, _STRUCT _I32 _I32 _I64 _STRUCT _BYTES _BYTES _BYTES);
  560. // ---- SHADERS
  561. typedef struct {
  562. #ifndef HL_XBS
  563. IDxcLibrary *library;
  564. IDxcCompiler *compiler;
  565. #else
  566. IDxcUtils *utils;
  567. IDxcCompiler3 *compiler;
  568. #endif
  569. } dx_compiler;
  570. HL_PRIM dx_compiler *HL_NAME(compiler_create)() {
  571. dx_compiler *comp = (dx_compiler*)hl_gc_alloc_raw(sizeof(dx_compiler));
  572. memset(comp,0,sizeof(dx_compiler));
  573. #ifndef HL_XBS
  574. CHKERR(DxcCreateInstance(CLSID_DxcLibrary, IID_PPV_ARGS(&comp->library)));
  575. CHKERR(DxcCreateInstance(CLSID_DxcCompiler, IID_PPV_ARGS(&comp->compiler)));
  576. #else
  577. CHKERR(DxcCreateInstance(CLSID_DxcUtils, IID_PPV_ARGS_OLD(&comp->utils)));
  578. CHKERR(DxcCreateInstance(CLSID_DxcCompiler, IID_PPV_ARGS_OLD(&comp->compiler)));
  579. #endif
  580. return comp;
  581. }
  582. HL_PRIM vbyte *HL_NAME(compiler_compile)( dx_compiler *comp, uchar *source, uchar *profile, varray *args, int *dataLen ) {
  583. IDxcBlobEncoding *blob = NULL;
  584. IDxcOperationResult *result = NULL;
  585. #ifndef HL_XBS
  586. comp->library->CreateBlobWithEncodingFromPinned(source,(int)ustrlen(source)*2,1200/*DXC_CP_UTF16*/,&blob);
  587. #else
  588. comp->utils->CreateBlobFromPinned(source, (int)ustrlen(source) * 2, 1200/*DXC_CP_UTF16*/, &blob);
  589. #endif
  590. if( blob == NULL )
  591. hl_error("Could not create blob");
  592. #ifndef HL_XBS
  593. comp->compiler->Compile(blob,L"",L"main",profile,hl_aptr(args,LPCWSTR),args->size,NULL,0,NULL,&result);
  594. #else
  595. BOOL knownEncoding = FALSE;
  596. UINT32 encoding = 0U;
  597. blob->GetEncoding(&knownEncoding, &encoding);
  598. DxcBuffer dxcBuffer = { blob->GetBufferPointer(), blob->GetBufferSize(), encoding };
  599. std::vector<LPCWSTR> arguments(hl_aptr(args, LPCWSTR), hl_aptr(args, LPCWSTR) + args->size);
  600. arguments.insert(arguments.end(), { L"-E", L"main", L"-T", profile, L"-D", L"__XBOX_DISABLE_PRECOMPILE" });
  601. CHKERR(comp->compiler->Compile(&dxcBuffer, arguments.data(), arguments.size(), NULL, IID_PPV_ARGS_OLD(&result)));
  602. #endif
  603. HRESULT hr;
  604. result->GetStatus(&hr);
  605. if( !SUCCEEDED(hr) ) {
  606. IDxcBlobEncoding *error = NULL;
  607. result->GetErrorBuffer(&error);
  608. uchar *c = hl_to_utf16((char*)error->GetBufferPointer());
  609. blob->Release();
  610. result->Release();
  611. error->Release();
  612. hl_error("%s",c);
  613. }
  614. IDxcBlob *out = NULL;
  615. result->GetResult(&out);
  616. *dataLen = (int)out->GetBufferSize();
  617. vbyte *bytes = hl_copy_bytes((vbyte*)out->GetBufferPointer(), *dataLen);
  618. out->Release();
  619. blob->Release();
  620. result->Release();
  621. return bytes;
  622. }
  623. HL_PRIM vbyte *HL_NAME(serialize_root_signature)( D3D12_ROOT_SIGNATURE_DESC *signature, D3D_ROOT_SIGNATURE_VERSION version, int *dataLen ) {
  624. ID3DBlob *data = NULL;
  625. ID3DBlob *error = NULL;
  626. HRESULT r = D3D12SerializeRootSignature(signature,version, &data, &error);
  627. if( !SUCCEEDED(r) ) {
  628. uchar *c = error ? hl_to_utf16((char*)error->GetBufferPointer()) : USTR("Invalid argument");
  629. if( error ) error->Release();
  630. hl_error("%s",c);
  631. }
  632. *dataLen = (int)data->GetBufferSize();
  633. vbyte *bytes = hl_copy_bytes((vbyte*)data->GetBufferPointer(), *dataLen);
  634. data->Release();
  635. return bytes;
  636. }
  637. HL_PRIM ID3D12RootSignature *HL_NAME(rootsignature_create)( vbyte *bytes, int len ) {
  638. ID3D12RootSignature *sign = NULL;
  639. DXERR(static_driver->device->CreateRootSignature(CURRENT_NODEMASK, bytes, len, IID_PPV_ARGS(&sign)));
  640. return sign;
  641. }
  642. HL_PRIM ID3D12PipelineState *HL_NAME(create_graphics_pipeline_state)( D3D12_GRAPHICS_PIPELINE_STATE_DESC *desc ) {
  643. ID3D12PipelineState *state = NULL;
  644. // if shader is considered invalid, maybe you're missing dxil.dll
  645. DXERR(static_driver->device->CreateGraphicsPipelineState(desc,IID_PPV_ARGS(&state)));
  646. return state;
  647. }
  648. HL_PRIM ID3D12PipelineState *HL_NAME(create_compute_pipeline_state)( D3D12_COMPUTE_PIPELINE_STATE_DESC *desc ) {
  649. ID3D12PipelineState *state = NULL;
  650. // if shader is considered invalid, maybe you're missing dxil.dll
  651. DXERR(static_driver->device->CreateComputePipelineState(desc,IID_PPV_ARGS(&state)));
  652. return state;
  653. }
  654. HL_PRIM ID3D12CommandSignature *HL_NAME(create_command_signature)( D3D12_COMMAND_SIGNATURE_DESC *desc, ID3D12RootSignature *rootSign ) {
  655. ID3D12CommandSignature *sign = NULL;
  656. DXERR(static_driver->device->CreateCommandSignature(desc,rootSign,IID_PPV_ARGS(&sign)));
  657. return sign;
  658. }
  659. #define _COMPILER _ABSTRACT(dx_compiler)
  660. DEFINE_PRIM(_COMPILER, compiler_create, _NO_ARG);
  661. DEFINE_PRIM(_BYTES, compiler_compile, _COMPILER _BYTES _BYTES _ARR _REF(_I32));
  662. DEFINE_PRIM(_BYTES, serialize_root_signature, _STRUCT _I32 _REF(_I32));
  663. DEFINE_PRIM(_RES, rootsignature_create, _BYTES _I32);
  664. DEFINE_PRIM(_RES, create_graphics_pipeline_state, _STRUCT);
  665. DEFINE_PRIM(_RES, create_compute_pipeline_state, _STRUCT);
  666. DEFINE_PRIM(_RES, create_command_signature, _STRUCT _RES);
  667. // ---- HEAPS
  668. HL_PRIM ID3D12DescriptorHeap *HL_NAME(descriptor_heap_create)( D3D12_DESCRIPTOR_HEAP_DESC *desc ) {
  669. ID3D12DescriptorHeap *heap = NULL;
  670. DXERR(static_driver->device->CreateDescriptorHeap(desc,IID_PPV_ARGS(&heap)));
  671. return heap;
  672. }
  673. HL_PRIM int HL_NAME(get_descriptor_handle_increment_size)( D3D12_DESCRIPTOR_HEAP_TYPE type ) {
  674. return static_driver->device->GetDescriptorHandleIncrementSize(type);
  675. }
  676. HL_PRIM int64 HL_NAME(descriptor_heap_get_handle)( ID3D12DescriptorHeap *heap, bool gpu ) {
  677. UINT64 handle = gpu ? heap->GetGPUDescriptorHandleForHeapStart().ptr : heap->GetCPUDescriptorHandleForHeapStart().ptr;
  678. return handle;
  679. }
  680. HL_PRIM ID3D12QueryHeap *HL_NAME(create_query_heap)( D3D12_QUERY_HEAP_DESC *desc ) {
  681. ID3D12QueryHeap *heap = NULL;
  682. DXERR(static_driver->device->CreateQueryHeap(desc,IID_PPV_ARGS(&heap)));
  683. return heap;
  684. }
  685. DEFINE_PRIM(_RES, descriptor_heap_create, _STRUCT);
  686. DEFINE_PRIM(_I32, get_descriptor_handle_increment_size, _I32);
  687. DEFINE_PRIM(_I64, descriptor_heap_get_handle, _RES _BOOL);
  688. DEFINE_PRIM(_RES, create_query_heap, _STRUCT);
  689. // ---- SYNCHRO
  690. HL_PRIM ID3D12Fence *HL_NAME(fence_create)( int64 value, D3D12_FENCE_FLAGS flags ) {
  691. ID3D12Fence *f = NULL;
  692. DXERR(static_driver->device->CreateFence(value,flags, IID_PPV_ARGS(&f)));
  693. return f;
  694. }
  695. HL_PRIM int64 HL_NAME(fence_get_completed_value)( ID3D12Fence *fence ) {
  696. return (int64)fence->GetCompletedValue();
  697. }
  698. HL_PRIM void HL_NAME(fence_set_event)( ID3D12Fence *fence, int64 value, HANDLE event ) {
  699. fence->SetEventOnCompletion(value, event);
  700. }
  701. HL_PRIM HANDLE HL_NAME(waitevent_create)( bool initState ) {
  702. return CreateEvent(NULL,FALSE,initState,NULL);
  703. }
  704. HL_PRIM bool HL_NAME(waitevent_wait)( HANDLE event, int time ) {
  705. return WaitForSingleObject(event,time) == 0;
  706. }
  707. #define _EVENT _ABSTRACT(dx_event)
  708. DEFINE_PRIM(_RES, fence_create, _I64 _I32);
  709. DEFINE_PRIM(_I64, fence_get_completed_value, _RES);
  710. DEFINE_PRIM(_VOID, fence_set_event, _RES _I64 _EVENT);
  711. DEFINE_PRIM(_EVENT, waitevent_create, _BOOL);
  712. DEFINE_PRIM(_BOOL, waitevent_wait, _EVENT _I32);
  713. // ---- COMMANDS
  714. HL_PRIM ID3D12CommandAllocator *HL_NAME(command_allocator_create)( D3D12_COMMAND_LIST_TYPE type ) {
  715. ID3D12CommandAllocator *a = NULL;
  716. DXERR(static_driver->device->CreateCommandAllocator(type,IID_PPV_ARGS(&a)));
  717. return a;
  718. }
  719. HL_PRIM void HL_NAME(command_allocator_reset)( ID3D12CommandAllocator *a ) {
  720. CHKERR(a->Reset());
  721. }
  722. HL_PRIM ID3D12GraphicsCommandList *HL_NAME(command_list_create)( D3D12_COMMAND_LIST_TYPE type, ID3D12CommandAllocator *alloc, ID3D12PipelineState *initState ) {
  723. ID3D12GraphicsCommandList *l = NULL;
  724. DXERR(static_driver->device->CreateCommandList(CURRENT_NODEMASK,type,alloc,initState,IID_PPV_ARGS(&l)));
  725. return l;
  726. }
  727. HL_PRIM void HL_NAME(command_list_close)( ID3D12GraphicsCommandList *l ) {
  728. CHKERR(l->Close());
  729. }
  730. HL_PRIM void HL_NAME(command_list_reset)( ID3D12GraphicsCommandList *l, ID3D12CommandAllocator *alloc, ID3D12PipelineState *state ) {
  731. CHKERR(l->Reset(alloc,state));
  732. }
  733. HL_PRIM void HL_NAME(command_list_execute)( ID3D12GraphicsCommandList *l ) {
  734. ID3D12CommandList* const commandLists[] = { l };
  735. static_driver->commandQueue->ExecuteCommandLists(1, commandLists);
  736. }
  737. HL_PRIM void HL_NAME(command_list_resource_barrier)( ID3D12GraphicsCommandList *l, D3D12_RESOURCE_BARRIER *barrier ) {
  738. l->ResourceBarrier(1,barrier);
  739. }
  740. HL_PRIM void HL_NAME(command_list_resource_barriers)(ID3D12GraphicsCommandList* l, D3D12_RESOURCE_BARRIER* barrier, int barrierCount) {
  741. l->ResourceBarrier(barrierCount, barrier);
  742. }
  743. HL_PRIM void HL_NAME(command_list_clear_render_target_view)( ID3D12GraphicsCommandList *l, D3D12_CPU_DESCRIPTOR_HANDLE view, FLOAT *colors ) {
  744. l->ClearRenderTargetView(view,colors,0,NULL);
  745. }
  746. HL_PRIM void HL_NAME(command_list_clear_depth_stencil_view)( ID3D12GraphicsCommandList *l, D3D12_CPU_DESCRIPTOR_HANDLE view, D3D12_CLEAR_FLAGS flags, FLOAT depth, int stencil ) {
  747. l->ClearDepthStencilView(view,flags,depth,(UINT8)stencil,0,NULL);
  748. }
  749. HL_PRIM void HL_NAME(command_list_draw_instanced)( ID3D12GraphicsCommandList *l, int vertexCountPerInstance, int instanceCount, int startVertexLocation, int startInstanceLocation ) {
  750. l->DrawInstanced(vertexCountPerInstance, instanceCount, startVertexLocation, startInstanceLocation);
  751. }
  752. HL_PRIM void HL_NAME(command_list_draw_indexed_instanced)( ID3D12GraphicsCommandList *l, int indexCountPerInstance, int instanceCount, int startIndexLocation, int baseVertexLocation, int startInstanceLocation ) {
  753. l->DrawIndexedInstanced(indexCountPerInstance, instanceCount, startIndexLocation, baseVertexLocation, startInstanceLocation);
  754. }
  755. HL_PRIM void HL_NAME(command_list_set_graphics_root_signature)( ID3D12GraphicsCommandList *l, ID3D12RootSignature *sign ) {
  756. l->SetGraphicsRootSignature(sign);
  757. }
  758. HL_PRIM void HL_NAME(command_list_set_graphics_root32_bit_constants)( ID3D12GraphicsCommandList *l, int index, int numValues, void *data, int destOffset ) {
  759. l->SetGraphicsRoot32BitConstants(index, numValues, data, destOffset);
  760. }
  761. HL_PRIM void HL_NAME(command_list_set_pipeline_state)( ID3D12GraphicsCommandList *l, ID3D12PipelineState *pipe ) {
  762. l->SetPipelineState(pipe);
  763. }
  764. HL_PRIM void HL_NAME(command_list_ia_set_vertex_buffers)( ID3D12GraphicsCommandList *l, int startSlot, int numViews, D3D12_VERTEX_BUFFER_VIEW *views ) {
  765. l->IASetVertexBuffers(startSlot, numViews, views);
  766. }
  767. HL_PRIM void HL_NAME(command_list_ia_set_index_buffer)( ID3D12GraphicsCommandList *l, D3D12_INDEX_BUFFER_VIEW *view ) {
  768. l->IASetIndexBuffer(view);
  769. }
  770. HL_PRIM void HL_NAME(command_list_ia_set_primitive_topology)( ID3D12GraphicsCommandList *l, D3D12_PRIMITIVE_TOPOLOGY topo ) {
  771. l->IASetPrimitiveTopology(topo);
  772. }
  773. HL_PRIM void HL_NAME(command_list_copy_buffer_region)( ID3D12GraphicsCommandList *l, ID3D12Resource *dst, int64 dstOffset, ID3D12Resource *src, int64 srcOffset, int64 numBytes ) {
  774. l->CopyBufferRegion(dst, dstOffset, src, srcOffset, numBytes);
  775. }
  776. HL_PRIM void HL_NAME(command_list_copy_texture_region)( ID3D12GraphicsCommandList *l, D3D12_TEXTURE_COPY_LOCATION *dst, int dstX, int dstY, int dstZ, D3D12_TEXTURE_COPY_LOCATION *src, D3D12_BOX *srcBox ) {
  777. l->CopyTextureRegion(dst, dstX, dstY, dstZ, src, srcBox);
  778. }
  779. HL_PRIM void HL_NAME(command_list_om_set_render_targets)( ID3D12GraphicsCommandList *l, int count, D3D12_CPU_DESCRIPTOR_HANDLE *handles, BOOL flag, D3D12_CPU_DESCRIPTOR_HANDLE *depthStencils ) {
  780. l->OMSetRenderTargets(count,handles,flag,depthStencils);
  781. }
  782. HL_PRIM void HL_NAME(command_list_om_set_stencil_ref)( ID3D12GraphicsCommandList *l, int value ) {
  783. l->OMSetStencilRef(value);
  784. }
  785. HL_PRIM void HL_NAME(command_list_rs_set_viewports)( ID3D12GraphicsCommandList *l, int count, D3D12_VIEWPORT *viewports ) {
  786. l->RSSetViewports(count, viewports);
  787. }
  788. HL_PRIM void HL_NAME(command_list_rs_set_scissor_rects)( ID3D12GraphicsCommandList *l, int count, D3D12_RECT *rects ) {
  789. l->RSSetScissorRects(count, rects);
  790. }
  791. HL_PRIM void HL_NAME(command_list_set_descriptor_heaps)( ID3D12GraphicsCommandList *l, varray *heaps ) {
  792. l->SetDescriptorHeaps(heaps->size,hl_aptr(heaps,ID3D12DescriptorHeap*));
  793. }
  794. HL_PRIM void HL_NAME(command_list_set_graphics_root_constant_buffer_view)( ID3D12GraphicsCommandList *l, int index, D3D12_GPU_VIRTUAL_ADDRESS address ) {
  795. l->SetGraphicsRootConstantBufferView(index,address);
  796. }
  797. HL_PRIM void HL_NAME(command_list_set_graphics_root_descriptor_table)( ID3D12GraphicsCommandList *l, int index, D3D12_GPU_DESCRIPTOR_HANDLE handle ) {
  798. l->SetGraphicsRootDescriptorTable(index,handle);
  799. }
  800. HL_PRIM void HL_NAME(command_list_set_graphics_root_shader_resource_view)( ID3D12GraphicsCommandList *l, int index, D3D12_GPU_VIRTUAL_ADDRESS handle ) {
  801. l->SetGraphicsRootShaderResourceView(index,handle);
  802. }
  803. HL_PRIM void HL_NAME(command_list_set_graphics_root_unordered_access_view)( ID3D12GraphicsCommandList *l, int index, D3D12_GPU_VIRTUAL_ADDRESS handle ) {
  804. l->SetGraphicsRootUnorderedAccessView(index,handle);
  805. }
  806. HL_PRIM void HL_NAME(command_list_execute_indirect)( ID3D12GraphicsCommandList *l, ID3D12CommandSignature *sign, int maxCommandCount, ID3D12Resource *args, int64 argsOffset, ID3D12Resource *count, int64 countOffset ) {
  807. l->ExecuteIndirect(sign, maxCommandCount, args, argsOffset, count, countOffset);
  808. }
  809. HL_PRIM void HL_NAME(command_list_begin_query)( ID3D12GraphicsCommandList *l, ID3D12QueryHeap *heap, D3D12_QUERY_TYPE type, int index ) {
  810. l->BeginQuery(heap, type, index);
  811. }
  812. HL_PRIM void HL_NAME(command_list_end_query)( ID3D12GraphicsCommandList *l, ID3D12QueryHeap *heap, D3D12_QUERY_TYPE type, int index ) {
  813. l->EndQuery(heap, type, index);
  814. }
  815. HL_PRIM void HL_NAME(command_list_resolve_query_data)( ID3D12GraphicsCommandList *l, ID3D12QueryHeap *heap, D3D12_QUERY_TYPE type, int index, int count, ID3D12Resource *dest, int64 offset ) {
  816. l->ResolveQueryData(heap,type,index,count,dest,offset);
  817. }
  818. HL_PRIM void HL_NAME(command_list_set_predication)( ID3D12GraphicsCommandList *l, ID3D12Resource *res, int64 offset, D3D12_PREDICATION_OP op ) {
  819. l->SetPredication(res,offset,op);
  820. }
  821. HL_PRIM void HL_NAME(command_list_set_compute_root_signature)( ID3D12GraphicsCommandList *l, ID3D12RootSignature *sign ) {
  822. l->SetComputeRootSignature(sign);
  823. }
  824. HL_PRIM void HL_NAME(command_list_set_compute_root32_bit_constants)( ID3D12GraphicsCommandList *l, int index, int numValues, void *data, int destOffset ) {
  825. l->SetComputeRoot32BitConstants(index, numValues, data, destOffset);
  826. }
  827. HL_PRIM void HL_NAME(command_list_set_compute_root_constant_buffer_view)( ID3D12GraphicsCommandList *l, int index, D3D12_GPU_VIRTUAL_ADDRESS address ) {
  828. l->SetComputeRootConstantBufferView(index,address);
  829. }
  830. HL_PRIM void HL_NAME(command_list_set_compute_root_descriptor_table)( ID3D12GraphicsCommandList *l, int index, D3D12_GPU_DESCRIPTOR_HANDLE handle ) {
  831. l->SetComputeRootDescriptorTable(index,handle);
  832. }
  833. HL_PRIM void HL_NAME(command_list_set_compute_root_shader_resource_view)( ID3D12GraphicsCommandList *l, int index, D3D12_GPU_VIRTUAL_ADDRESS handle ) {
  834. l->SetComputeRootShaderResourceView(index,handle);
  835. }
  836. HL_PRIM void HL_NAME(command_list_set_compute_root_unordered_access_view)( ID3D12GraphicsCommandList *l, int index, D3D12_GPU_VIRTUAL_ADDRESS handle ) {
  837. l->SetComputeRootUnorderedAccessView(index,handle);
  838. }
  839. HL_PRIM void HL_NAME(command_list_dispatch)( ID3D12GraphicsCommandList *l, int x, int y, int z ) {
  840. l->Dispatch(x,y,z);
  841. }
  842. DEFINE_PRIM(_RES, command_allocator_create, _I32);
  843. DEFINE_PRIM(_VOID, command_allocator_reset, _RES);
  844. DEFINE_PRIM(_RES, command_list_create, _I32 _RES _RES);
  845. DEFINE_PRIM(_VOID, command_list_close, _RES);
  846. DEFINE_PRIM(_VOID, command_list_reset, _RES _RES _RES);
  847. DEFINE_PRIM(_VOID, command_list_resource_barrier, _RES _STRUCT);
  848. DEFINE_PRIM(_VOID, command_list_resource_barriers, _RES _ABSTRACT(hl_carray) _I32);
  849. DEFINE_PRIM(_VOID, command_list_execute, _RES);
  850. DEFINE_PRIM(_VOID, command_list_clear_render_target_view, _RES _I64 _STRUCT);
  851. DEFINE_PRIM(_VOID, command_list_clear_depth_stencil_view, _RES _I64 _I32 _F32 _I32);
  852. DEFINE_PRIM(_VOID, command_list_draw_instanced, _RES _I32 _I32 _I32 _I32);
  853. DEFINE_PRIM(_VOID, command_list_draw_indexed_instanced, _RES _I32 _I32 _I32 _I32 _I32);
  854. DEFINE_PRIM(_VOID, command_list_set_graphics_root_signature, _RES _RES);
  855. DEFINE_PRIM(_VOID, command_list_set_graphics_root32_bit_constants, _RES _I32 _I32 _BYTES _I32);
  856. DEFINE_PRIM(_VOID, command_list_set_graphics_root_constant_buffer_view, _RES _I32 _I64);
  857. DEFINE_PRIM(_VOID, command_list_set_graphics_root_descriptor_table, _RES _I32 _I64);
  858. DEFINE_PRIM(_VOID, command_list_set_graphics_root_shader_resource_view, _RES _I32 _I64);
  859. DEFINE_PRIM(_VOID, command_list_set_graphics_root_unordered_access_view, _RES _I32 _I64);
  860. DEFINE_PRIM(_VOID, command_list_set_descriptor_heaps, _RES _ARR);
  861. DEFINE_PRIM(_VOID, command_list_set_pipeline_state, _RES _RES);
  862. DEFINE_PRIM(_VOID, command_list_ia_set_vertex_buffers, _RES _I32 _I32 _STRUCT);
  863. DEFINE_PRIM(_VOID, command_list_ia_set_index_buffer, _RES _STRUCT);
  864. DEFINE_PRIM(_VOID, command_list_ia_set_primitive_topology, _RES _I32);
  865. DEFINE_PRIM(_VOID, command_list_copy_buffer_region, _RES _RES _I64 _RES _I64 _I64);
  866. DEFINE_PRIM(_VOID, command_list_copy_texture_region, _RES _STRUCT _I32 _I32 _I32 _STRUCT _STRUCT);
  867. DEFINE_PRIM(_VOID, command_list_om_set_render_targets, _RES _I32 _BYTES _I32 _BYTES);
  868. DEFINE_PRIM(_VOID, command_list_om_set_stencil_ref, _RES _I32);
  869. DEFINE_PRIM(_VOID, command_list_rs_set_viewports, _RES _I32 _STRUCT);
  870. DEFINE_PRIM(_VOID, command_list_rs_set_scissor_rects, _RES _I32 _STRUCT);
  871. DEFINE_PRIM(_VOID, command_list_execute_indirect, _RES _RES _I32 _RES _I64 _RES _I64);
  872. DEFINE_PRIM(_VOID, command_list_begin_query, _RES _RES _I32 _I32);
  873. DEFINE_PRIM(_VOID, command_list_end_query, _RES _RES _I32 _I32);
  874. DEFINE_PRIM(_VOID, command_list_resolve_query_data, _RES _RES _I32 _I32 _I32 _RES _I64);
  875. DEFINE_PRIM(_VOID, command_list_set_predication, _RES _RES _I64 _I32);
  876. DEFINE_PRIM(_VOID, command_list_set_compute_root_signature, _RES _RES);
  877. DEFINE_PRIM(_VOID, command_list_set_compute_root32_bit_constants, _RES _I32 _I32 _BYTES _I32);
  878. DEFINE_PRIM(_VOID, command_list_set_compute_root_constant_buffer_view, _RES _I32 _I64);
  879. DEFINE_PRIM(_VOID, command_list_set_compute_root_descriptor_table, _RES _I32 _I64);
  880. DEFINE_PRIM(_VOID, command_list_set_compute_root_shader_resource_view, _RES _I32 _I64);
  881. DEFINE_PRIM(_VOID, command_list_set_compute_root_unordered_access_view, _RES _I32 _I64);
  882. DEFINE_PRIM(_VOID, command_list_dispatch, _RES _I32 _I32 _I32);
  883. //command_list_clear_unordered_access_view_float,
  884. //command_list_clear_unordered_access_view_uint,