d3d11_windows.go 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694
  1. // SPDX-License-Identifier: Unlicense OR MIT
  2. package d3d11
  3. import (
  4. "fmt"
  5. "math"
  6. "syscall"
  7. "unsafe"
  8. "gioui.org/internal/f32color"
  9. "golang.org/x/sys/windows"
  10. )
  11. type DXGI_SWAP_CHAIN_DESC struct {
  12. BufferDesc DXGI_MODE_DESC
  13. SampleDesc DXGI_SAMPLE_DESC
  14. BufferUsage uint32
  15. BufferCount uint32
  16. OutputWindow windows.Handle
  17. Windowed uint32
  18. SwapEffect uint32
  19. Flags uint32
  20. }
  21. type DXGI_SAMPLE_DESC struct {
  22. Count uint32
  23. Quality uint32
  24. }
  25. type DXGI_MODE_DESC struct {
  26. Width uint32
  27. Height uint32
  28. RefreshRate DXGI_RATIONAL
  29. Format uint32
  30. ScanlineOrdering uint32
  31. Scaling uint32
  32. }
  33. type DXGI_RATIONAL struct {
  34. Numerator uint32
  35. Denominator uint32
  36. }
  37. type TEXTURE2D_DESC struct {
  38. Width uint32
  39. Height uint32
  40. MipLevels uint32
  41. ArraySize uint32
  42. Format uint32
  43. SampleDesc DXGI_SAMPLE_DESC
  44. Usage uint32
  45. BindFlags uint32
  46. CPUAccessFlags uint32
  47. MiscFlags uint32
  48. }
  49. type SAMPLER_DESC struct {
  50. Filter uint32
  51. AddressU uint32
  52. AddressV uint32
  53. AddressW uint32
  54. MipLODBias float32
  55. MaxAnisotropy uint32
  56. ComparisonFunc uint32
  57. BorderColor [4]float32
  58. MinLOD float32
  59. MaxLOD float32
  60. }
  61. type SHADER_RESOURCE_VIEW_DESC_TEX2D struct {
  62. SHADER_RESOURCE_VIEW_DESC
  63. Texture2D TEX2D_SRV
  64. }
  65. type SHADER_RESOURCE_VIEW_DESC_BUFFEREX struct {
  66. SHADER_RESOURCE_VIEW_DESC
  67. Buffer BUFFEREX_SRV
  68. }
  69. type UNORDERED_ACCESS_VIEW_DESC_TEX2D struct {
  70. UNORDERED_ACCESS_VIEW_DESC
  71. Texture2D TEX2D_UAV
  72. }
  73. type UNORDERED_ACCESS_VIEW_DESC_BUFFER struct {
  74. UNORDERED_ACCESS_VIEW_DESC
  75. Buffer BUFFER_UAV
  76. }
  77. type SHADER_RESOURCE_VIEW_DESC struct {
  78. Format uint32
  79. ViewDimension uint32
  80. }
  81. type UNORDERED_ACCESS_VIEW_DESC struct {
  82. Format uint32
  83. ViewDimension uint32
  84. }
  85. type TEX2D_SRV struct {
  86. MostDetailedMip uint32
  87. MipLevels uint32
  88. }
  89. type BUFFEREX_SRV struct {
  90. FirstElement uint32
  91. NumElements uint32
  92. Flags uint32
  93. }
  94. type TEX2D_UAV struct {
  95. MipSlice uint32
  96. }
  97. type BUFFER_UAV struct {
  98. FirstElement uint32
  99. NumElements uint32
  100. Flags uint32
  101. }
  102. type INPUT_ELEMENT_DESC struct {
  103. SemanticName *byte
  104. SemanticIndex uint32
  105. Format uint32
  106. InputSlot uint32
  107. AlignedByteOffset uint32
  108. InputSlotClass uint32
  109. InstanceDataStepRate uint32
  110. }
  111. type IDXGISwapChain struct {
  112. Vtbl *struct {
  113. _IUnknownVTbl
  114. SetPrivateData uintptr
  115. SetPrivateDataInterface uintptr
  116. GetPrivateData uintptr
  117. GetParent uintptr
  118. GetDevice uintptr
  119. Present uintptr
  120. GetBuffer uintptr
  121. SetFullscreenState uintptr
  122. GetFullscreenState uintptr
  123. GetDesc uintptr
  124. ResizeBuffers uintptr
  125. ResizeTarget uintptr
  126. GetContainingOutput uintptr
  127. GetFrameStatistics uintptr
  128. GetLastPresentCount uintptr
  129. }
  130. }
  131. type Debug struct {
  132. Vtbl *struct {
  133. _IUnknownVTbl
  134. SetFeatureMask uintptr
  135. GetFeatureMask uintptr
  136. SetPresentPerRenderOpDelay uintptr
  137. GetPresentPerRenderOpDelay uintptr
  138. SetSwapChain uintptr
  139. GetSwapChain uintptr
  140. ValidateContext uintptr
  141. ReportLiveDeviceObjects uintptr
  142. ValidateContextForDispatch uintptr
  143. }
  144. }
  145. type Device struct {
  146. Vtbl *struct {
  147. _IUnknownVTbl
  148. CreateBuffer uintptr
  149. CreateTexture1D uintptr
  150. CreateTexture2D uintptr
  151. CreateTexture3D uintptr
  152. CreateShaderResourceView uintptr
  153. CreateUnorderedAccessView uintptr
  154. CreateRenderTargetView uintptr
  155. CreateDepthStencilView uintptr
  156. CreateInputLayout uintptr
  157. CreateVertexShader uintptr
  158. CreateGeometryShader uintptr
  159. CreateGeometryShaderWithStreamOutput uintptr
  160. CreatePixelShader uintptr
  161. CreateHullShader uintptr
  162. CreateDomainShader uintptr
  163. CreateComputeShader uintptr
  164. CreateClassLinkage uintptr
  165. CreateBlendState uintptr
  166. CreateDepthStencilState uintptr
  167. CreateRasterizerState uintptr
  168. CreateSamplerState uintptr
  169. CreateQuery uintptr
  170. CreatePredicate uintptr
  171. CreateCounter uintptr
  172. CreateDeferredContext uintptr
  173. OpenSharedResource uintptr
  174. CheckFormatSupport uintptr
  175. CheckMultisampleQualityLevels uintptr
  176. CheckCounterInfo uintptr
  177. CheckCounter uintptr
  178. CheckFeatureSupport uintptr
  179. GetPrivateData uintptr
  180. SetPrivateData uintptr
  181. SetPrivateDataInterface uintptr
  182. GetFeatureLevel uintptr
  183. GetCreationFlags uintptr
  184. GetDeviceRemovedReason uintptr
  185. GetImmediateContext uintptr
  186. SetExceptionMode uintptr
  187. GetExceptionMode uintptr
  188. }
  189. }
  190. type DeviceContext struct {
  191. Vtbl *struct {
  192. _IUnknownVTbl
  193. GetDevice uintptr
  194. GetPrivateData uintptr
  195. SetPrivateData uintptr
  196. SetPrivateDataInterface uintptr
  197. VSSetConstantBuffers uintptr
  198. PSSetShaderResources uintptr
  199. PSSetShader uintptr
  200. PSSetSamplers uintptr
  201. VSSetShader uintptr
  202. DrawIndexed uintptr
  203. Draw uintptr
  204. Map uintptr
  205. Unmap uintptr
  206. PSSetConstantBuffers uintptr
  207. IASetInputLayout uintptr
  208. IASetVertexBuffers uintptr
  209. IASetIndexBuffer uintptr
  210. DrawIndexedInstanced uintptr
  211. DrawInstanced uintptr
  212. GSSetConstantBuffers uintptr
  213. GSSetShader uintptr
  214. IASetPrimitiveTopology uintptr
  215. VSSetShaderResources uintptr
  216. VSSetSamplers uintptr
  217. Begin uintptr
  218. End uintptr
  219. GetData uintptr
  220. SetPredication uintptr
  221. GSSetShaderResources uintptr
  222. GSSetSamplers uintptr
  223. OMSetRenderTargets uintptr
  224. OMSetRenderTargetsAndUnorderedAccessViews uintptr
  225. OMSetBlendState uintptr
  226. OMSetDepthStencilState uintptr
  227. SOSetTargets uintptr
  228. DrawAuto uintptr
  229. DrawIndexedInstancedIndirect uintptr
  230. DrawInstancedIndirect uintptr
  231. Dispatch uintptr
  232. DispatchIndirect uintptr
  233. RSSetState uintptr
  234. RSSetViewports uintptr
  235. RSSetScissorRects uintptr
  236. CopySubresourceRegion uintptr
  237. CopyResource uintptr
  238. UpdateSubresource uintptr
  239. CopyStructureCount uintptr
  240. ClearRenderTargetView uintptr
  241. ClearUnorderedAccessViewUint uintptr
  242. ClearUnorderedAccessViewFloat uintptr
  243. ClearDepthStencilView uintptr
  244. GenerateMips uintptr
  245. SetResourceMinLOD uintptr
  246. GetResourceMinLOD uintptr
  247. ResolveSubresource uintptr
  248. ExecuteCommandList uintptr
  249. HSSetShaderResources uintptr
  250. HSSetShader uintptr
  251. HSSetSamplers uintptr
  252. HSSetConstantBuffers uintptr
  253. DSSetShaderResources uintptr
  254. DSSetShader uintptr
  255. DSSetSamplers uintptr
  256. DSSetConstantBuffers uintptr
  257. CSSetShaderResources uintptr
  258. CSSetUnorderedAccessViews uintptr
  259. CSSetShader uintptr
  260. CSSetSamplers uintptr
  261. CSSetConstantBuffers uintptr
  262. VSGetConstantBuffers uintptr
  263. PSGetShaderResources uintptr
  264. PSGetShader uintptr
  265. PSGetSamplers uintptr
  266. VSGetShader uintptr
  267. PSGetConstantBuffers uintptr
  268. IAGetInputLayout uintptr
  269. IAGetVertexBuffers uintptr
  270. IAGetIndexBuffer uintptr
  271. GSGetConstantBuffers uintptr
  272. GSGetShader uintptr
  273. IAGetPrimitiveTopology uintptr
  274. VSGetShaderResources uintptr
  275. VSGetSamplers uintptr
  276. GetPredication uintptr
  277. GSGetShaderResources uintptr
  278. GSGetSamplers uintptr
  279. OMGetRenderTargets uintptr
  280. OMGetRenderTargetsAndUnorderedAccessViews uintptr
  281. OMGetBlendState uintptr
  282. OMGetDepthStencilState uintptr
  283. SOGetTargets uintptr
  284. RSGetState uintptr
  285. RSGetViewports uintptr
  286. RSGetScissorRects uintptr
  287. HSGetShaderResources uintptr
  288. HSGetShader uintptr
  289. HSGetSamplers uintptr
  290. HSGetConstantBuffers uintptr
  291. DSGetShaderResources uintptr
  292. DSGetShader uintptr
  293. DSGetSamplers uintptr
  294. DSGetConstantBuffers uintptr
  295. CSGetShaderResources uintptr
  296. CSGetUnorderedAccessViews uintptr
  297. CSGetShader uintptr
  298. CSGetSamplers uintptr
  299. CSGetConstantBuffers uintptr
  300. ClearState uintptr
  301. Flush uintptr
  302. GetType uintptr
  303. GetContextFlags uintptr
  304. FinishCommandList uintptr
  305. }
  306. }
  307. type RenderTargetView struct {
  308. Vtbl *struct {
  309. _IUnknownVTbl
  310. }
  311. }
  312. type Resource struct {
  313. Vtbl *struct {
  314. _IUnknownVTbl
  315. }
  316. }
  317. type Texture2D struct {
  318. Vtbl *struct {
  319. _IUnknownVTbl
  320. }
  321. }
  322. type Buffer struct {
  323. Vtbl *struct {
  324. _IUnknownVTbl
  325. }
  326. }
  327. type SamplerState struct {
  328. Vtbl *struct {
  329. _IUnknownVTbl
  330. }
  331. }
  332. type PixelShader struct {
  333. Vtbl *struct {
  334. _IUnknownVTbl
  335. }
  336. }
  337. type ShaderResourceView struct {
  338. Vtbl *struct {
  339. _IUnknownVTbl
  340. }
  341. }
  342. type UnorderedAccessView struct {
  343. Vtbl *struct {
  344. _IUnknownVTbl
  345. }
  346. }
  347. type DepthStencilView struct {
  348. Vtbl *struct {
  349. _IUnknownVTbl
  350. }
  351. }
  352. type BlendState struct {
  353. Vtbl *struct {
  354. _IUnknownVTbl
  355. }
  356. }
  357. type DepthStencilState struct {
  358. Vtbl *struct {
  359. _IUnknownVTbl
  360. }
  361. }
  362. type VertexShader struct {
  363. Vtbl *struct {
  364. _IUnknownVTbl
  365. }
  366. }
  367. type ComputeShader struct {
  368. Vtbl *struct {
  369. _IUnknownVTbl
  370. }
  371. }
  372. type RasterizerState struct {
  373. Vtbl *struct {
  374. _IUnknownVTbl
  375. }
  376. }
  377. type InputLayout struct {
  378. Vtbl *struct {
  379. _IUnknownVTbl
  380. GetBufferPointer uintptr
  381. GetBufferSize uintptr
  382. }
  383. }
  384. type DEPTH_STENCIL_DESC struct {
  385. DepthEnable uint32
  386. DepthWriteMask uint32
  387. DepthFunc uint32
  388. StencilEnable uint32
  389. StencilReadMask uint8
  390. StencilWriteMask uint8
  391. FrontFace DEPTH_STENCILOP_DESC
  392. BackFace DEPTH_STENCILOP_DESC
  393. }
  394. type DEPTH_STENCILOP_DESC struct {
  395. StencilFailOp uint32
  396. StencilDepthFailOp uint32
  397. StencilPassOp uint32
  398. StencilFunc uint32
  399. }
  400. type DEPTH_STENCIL_VIEW_DESC_TEX2D struct {
  401. Format uint32
  402. ViewDimension uint32
  403. Flags uint32
  404. Texture2D TEX2D_DSV
  405. }
  406. type TEX2D_DSV struct {
  407. MipSlice uint32
  408. }
  409. type BLEND_DESC struct {
  410. AlphaToCoverageEnable uint32
  411. IndependentBlendEnable uint32
  412. RenderTarget [8]RENDER_TARGET_BLEND_DESC
  413. }
  414. type RENDER_TARGET_BLEND_DESC struct {
  415. BlendEnable uint32
  416. SrcBlend uint32
  417. DestBlend uint32
  418. BlendOp uint32
  419. SrcBlendAlpha uint32
  420. DestBlendAlpha uint32
  421. BlendOpAlpha uint32
  422. RenderTargetWriteMask uint8
  423. }
  424. type IDXGIObject struct {
  425. Vtbl *struct {
  426. _IUnknownVTbl
  427. SetPrivateData uintptr
  428. SetPrivateDataInterface uintptr
  429. GetPrivateData uintptr
  430. GetParent uintptr
  431. }
  432. }
  433. type IDXGIAdapter struct {
  434. Vtbl *struct {
  435. _IUnknownVTbl
  436. SetPrivateData uintptr
  437. SetPrivateDataInterface uintptr
  438. GetPrivateData uintptr
  439. GetParent uintptr
  440. EnumOutputs uintptr
  441. GetDesc uintptr
  442. CheckInterfaceSupport uintptr
  443. GetDesc1 uintptr
  444. }
  445. }
  446. type IDXGIFactory struct {
  447. Vtbl *struct {
  448. _IUnknownVTbl
  449. SetPrivateData uintptr
  450. SetPrivateDataInterface uintptr
  451. GetPrivateData uintptr
  452. GetParent uintptr
  453. EnumAdapters uintptr
  454. MakeWindowAssociation uintptr
  455. GetWindowAssociation uintptr
  456. CreateSwapChain uintptr
  457. CreateSoftwareAdapter uintptr
  458. }
  459. }
  460. type IDXGIDebug struct {
  461. Vtbl *struct {
  462. _IUnknownVTbl
  463. ReportLiveObjects uintptr
  464. }
  465. }
  466. type IDXGIDevice struct {
  467. Vtbl *struct {
  468. _IUnknownVTbl
  469. SetPrivateData uintptr
  470. SetPrivateDataInterface uintptr
  471. GetPrivateData uintptr
  472. GetParent uintptr
  473. GetAdapter uintptr
  474. CreateSurface uintptr
  475. QueryResourceResidency uintptr
  476. SetGPUThreadPriority uintptr
  477. GetGPUThreadPriority uintptr
  478. }
  479. }
  480. type IUnknown struct {
  481. Vtbl *struct {
  482. _IUnknownVTbl
  483. }
  484. }
  485. type _IUnknownVTbl struct {
  486. QueryInterface uintptr
  487. AddRef uintptr
  488. Release uintptr
  489. }
  490. type BUFFER_DESC struct {
  491. ByteWidth uint32
  492. Usage uint32
  493. BindFlags uint32
  494. CPUAccessFlags uint32
  495. MiscFlags uint32
  496. StructureByteStride uint32
  497. }
  498. type GUID struct {
  499. Data1 uint32
  500. Data2 uint16
  501. Data3 uint16
  502. Data4_0 uint8
  503. Data4_1 uint8
  504. Data4_2 uint8
  505. Data4_3 uint8
  506. Data4_4 uint8
  507. Data4_5 uint8
  508. Data4_6 uint8
  509. Data4_7 uint8
  510. }
  511. type VIEWPORT struct {
  512. TopLeftX float32
  513. TopLeftY float32
  514. Width float32
  515. Height float32
  516. MinDepth float32
  517. MaxDepth float32
  518. }
  519. type SUBRESOURCE_DATA struct {
  520. pSysMem *byte
  521. }
  522. type BOX struct {
  523. Left uint32
  524. Top uint32
  525. Front uint32
  526. Right uint32
  527. Bottom uint32
  528. Back uint32
  529. }
  530. type MAPPED_SUBRESOURCE struct {
  531. PData uintptr
  532. RowPitch uint32
  533. DepthPitch uint32
  534. }
  535. type ErrorCode struct {
  536. Name string
  537. Code uint32
  538. }
  539. type RASTERIZER_DESC struct {
  540. FillMode uint32
  541. CullMode uint32
  542. FrontCounterClockwise uint32
  543. DepthBias int32
  544. DepthBiasClamp float32
  545. SlopeScaledDepthBias float32
  546. DepthClipEnable uint32
  547. ScissorEnable uint32
  548. MultisampleEnable uint32
  549. AntialiasedLineEnable uint32
  550. }
  551. var (
  552. IID_Texture2D = GUID{0x6f15aaf2, 0xd208, 0x4e89, 0x9a, 0xb4, 0x48, 0x95, 0x35, 0xd3, 0x4f, 0x9c}
  553. IID_IDXGIDebug = GUID{0x119E7452, 0xDE9E, 0x40fe, 0x88, 0x06, 0x88, 0xF9, 0x0C, 0x12, 0xB4, 0x41}
  554. IID_IDXGIDevice = GUID{0x54ec77fa, 0x1377, 0x44e6, 0x8c, 0x32, 0x88, 0xfd, 0x5f, 0x44, 0xc8, 0x4c}
  555. IID_IDXGIFactory = GUID{0x7b7166ec, 0x21c7, 0x44ae, 0xb2, 0x1a, 0xc9, 0xae, 0x32, 0x1a, 0xe3, 0x69}
  556. IID_ID3D11Debug = GUID{0x79cf2233, 0x7536, 0x4948, 0x9d, 0x36, 0x1e, 0x46, 0x92, 0xdc, 0x57, 0x60}
  557. DXGI_DEBUG_ALL = GUID{0xe48ae283, 0xda80, 0x490b, 0x87, 0xe6, 0x43, 0xe9, 0xa9, 0xcf, 0xda, 0x8}
  558. )
  559. var (
  560. d3d11 = windows.NewLazySystemDLL("d3d11.dll")
  561. _D3D11CreateDevice = d3d11.NewProc("D3D11CreateDevice")
  562. _D3D11CreateDeviceAndSwapChain = d3d11.NewProc("D3D11CreateDeviceAndSwapChain")
  563. dxgi = windows.NewLazySystemDLL("dxgi.dll")
  564. _DXGIGetDebugInterface1 = dxgi.NewProc("DXGIGetDebugInterface1")
  565. )
  566. const (
  567. SDK_VERSION = 7
  568. DRIVER_TYPE_HARDWARE = 1
  569. DXGI_FORMAT_UNKNOWN = 0
  570. DXGI_FORMAT_R16_FLOAT = 54
  571. DXGI_FORMAT_R32_FLOAT = 41
  572. DXGI_FORMAT_R32_TYPELESS = 39
  573. DXGI_FORMAT_R32G32_FLOAT = 16
  574. DXGI_FORMAT_R32G32B32_FLOAT = 6
  575. DXGI_FORMAT_R32G32B32A32_FLOAT = 2
  576. DXGI_FORMAT_R8G8B8A8_UNORM = 28
  577. DXGI_FORMAT_R8G8B8A8_UNORM_SRGB = 29
  578. DXGI_FORMAT_R16_SINT = 59
  579. DXGI_FORMAT_R16G16_SINT = 38
  580. DXGI_FORMAT_R16_UINT = 57
  581. DXGI_FORMAT_D24_UNORM_S8_UINT = 45
  582. DXGI_FORMAT_R16G16_FLOAT = 34
  583. DXGI_FORMAT_R16G16B16A16_FLOAT = 10
  584. DXGI_DEBUG_RLO_SUMMARY = 0x1
  585. DXGI_DEBUG_RLO_DETAIL = 0x2
  586. DXGI_DEBUG_RLO_IGNORE_INTERNAL = 0x4
  587. FORMAT_SUPPORT_TEXTURE2D = 0x20
  588. FORMAT_SUPPORT_RENDER_TARGET = 0x4000
  589. DXGI_USAGE_RENDER_TARGET_OUTPUT = 1 << (1 + 4)
  590. CPU_ACCESS_READ = 0x20000
  591. MAP_READ = 1
  592. DXGI_SWAP_EFFECT_DISCARD = 0
  593. FEATURE_LEVEL_9_1 = 0x9100
  594. FEATURE_LEVEL_9_3 = 0x9300
  595. FEATURE_LEVEL_11_0 = 0xb000
  596. USAGE_IMMUTABLE = 1
  597. USAGE_STAGING = 3
  598. BIND_VERTEX_BUFFER = 0x1
  599. BIND_INDEX_BUFFER = 0x2
  600. BIND_CONSTANT_BUFFER = 0x4
  601. BIND_SHADER_RESOURCE = 0x8
  602. BIND_RENDER_TARGET = 0x20
  603. BIND_DEPTH_STENCIL = 0x40
  604. BIND_UNORDERED_ACCESS = 0x80
  605. PRIMITIVE_TOPOLOGY_TRIANGLELIST = 4
  606. PRIMITIVE_TOPOLOGY_TRIANGLESTRIP = 5
  607. FILTER_MIN_MAG_LINEAR_MIP_POINT = 0x14
  608. FILTER_MIN_MAG_MIP_LINEAR = 0x15
  609. FILTER_MIN_MAG_MIP_POINT = 0
  610. TEXTURE_ADDRESS_MIRROR = 2
  611. TEXTURE_ADDRESS_CLAMP = 3
  612. TEXTURE_ADDRESS_WRAP = 1
  613. SRV_DIMENSION_BUFFER = 1
  614. UAV_DIMENSION_BUFFER = 1
  615. SRV_DIMENSION_BUFFEREX = 11
  616. SRV_DIMENSION_TEXTURE2D = 4
  617. UAV_DIMENSION_TEXTURE2D = 4
  618. BUFFER_UAV_FLAG_RAW = 0x1
  619. BUFFEREX_SRV_FLAG_RAW = 0x1
  620. RESOURCE_MISC_BUFFER_ALLOW_RAW_VIEWS = 0x20
  621. RESOURCE_MISC_GENERATE_MIPS = 0x1
  622. CREATE_DEVICE_DEBUG = 0x2
  623. FILL_SOLID = 3
  624. CULL_NONE = 1
  625. CLEAR_DEPTH = 0x1
  626. CLEAR_STENCIL = 0x2
  627. DSV_DIMENSION_TEXTURE2D = 3
  628. DEPTH_WRITE_MASK_ALL = 1
  629. COMPARISON_GREATER = 5
  630. COMPARISON_GREATER_EQUAL = 7
  631. BLEND_OP_ADD = 1
  632. BLEND_ONE = 2
  633. BLEND_INV_SRC_ALPHA = 6
  634. BLEND_ZERO = 1
  635. BLEND_DEST_COLOR = 9
  636. BLEND_DEST_ALPHA = 7
  637. COLOR_WRITE_ENABLE_ALL = 1 | 2 | 4 | 8
  638. DXGI_STATUS_OCCLUDED = 0x087A0001
  639. DXGI_ERROR_DEVICE_RESET = 0x887A0007
  640. DXGI_ERROR_DEVICE_REMOVED = 0x887A0005
  641. D3DDDIERR_DEVICEREMOVED = 1<<31 | 0x876<<16 | 2160
  642. RLDO_SUMMARY = 1
  643. RLDO_DETAIL = 2
  644. RLDO_IGNORE_INTERNAL = 4
  645. )
  646. func CreateDevice(driverType uint32, flags uint32) (*Device, *DeviceContext, uint32, error) {
  647. var (
  648. dev *Device
  649. ctx *DeviceContext
  650. featLvl uint32
  651. )
  652. r, _, _ := _D3D11CreateDevice.Call(
  653. 0, // pAdapter
  654. uintptr(driverType), // driverType
  655. 0, // Software
  656. uintptr(flags), // Flags
  657. 0, // pFeatureLevels
  658. 0, // FeatureLevels
  659. SDK_VERSION, // SDKVersion
  660. uintptr(unsafe.Pointer(&dev)), // ppDevice
  661. uintptr(unsafe.Pointer(&featLvl)), // pFeatureLevel
  662. uintptr(unsafe.Pointer(&ctx)), // ppImmediateContext
  663. )
  664. if r != 0 {
  665. return nil, nil, 0, ErrorCode{Name: "D3D11CreateDevice", Code: uint32(r)}
  666. }
  667. return dev, ctx, featLvl, nil
  668. }
  669. func CreateDeviceAndSwapChain(driverType uint32, flags uint32, swapDesc *DXGI_SWAP_CHAIN_DESC) (*Device, *DeviceContext, *IDXGISwapChain, uint32, error) {
  670. var (
  671. dev *Device
  672. ctx *DeviceContext
  673. swchain *IDXGISwapChain
  674. featLvl uint32
  675. )
  676. r, _, _ := _D3D11CreateDeviceAndSwapChain.Call(
  677. 0, // pAdapter
  678. uintptr(driverType), // driverType
  679. 0, // Software
  680. uintptr(flags), // Flags
  681. 0, // pFeatureLevels
  682. 0, // FeatureLevels
  683. SDK_VERSION, // SDKVersion
  684. uintptr(unsafe.Pointer(swapDesc)), // pSwapChainDesc
  685. uintptr(unsafe.Pointer(&swchain)), // ppSwapChain
  686. uintptr(unsafe.Pointer(&dev)), // ppDevice
  687. uintptr(unsafe.Pointer(&featLvl)), // pFeatureLevel
  688. uintptr(unsafe.Pointer(&ctx)), // ppImmediateContext
  689. )
  690. if r != 0 {
  691. return nil, nil, nil, 0, ErrorCode{Name: "D3D11CreateDeviceAndSwapChain", Code: uint32(r)}
  692. }
  693. return dev, ctx, swchain, featLvl, nil
  694. }
  695. func DXGIGetDebugInterface1() (*IDXGIDebug, error) {
  696. var dbg *IDXGIDebug
  697. r, _, _ := _DXGIGetDebugInterface1.Call(
  698. 0, // Flags
  699. uintptr(unsafe.Pointer(&IID_IDXGIDebug)),
  700. uintptr(unsafe.Pointer(&dbg)),
  701. )
  702. if r != 0 {
  703. return nil, ErrorCode{Name: "DXGIGetDebugInterface1", Code: uint32(r)}
  704. }
  705. return dbg, nil
  706. }
  707. func ReportLiveObjects() error {
  708. dxgi, err := DXGIGetDebugInterface1()
  709. if err != nil {
  710. return err
  711. }
  712. defer IUnknownRelease(unsafe.Pointer(dxgi), dxgi.Vtbl.Release)
  713. dxgi.ReportLiveObjects(&DXGI_DEBUG_ALL, DXGI_DEBUG_RLO_DETAIL|DXGI_DEBUG_RLO_IGNORE_INTERNAL)
  714. return nil
  715. }
  716. func (d *IDXGIDebug) ReportLiveObjects(guid *GUID, flags uint32) {
  717. syscall.Syscall6(
  718. d.Vtbl.ReportLiveObjects,
  719. 3,
  720. uintptr(unsafe.Pointer(d)),
  721. uintptr(unsafe.Pointer(guid)),
  722. uintptr(flags),
  723. 0,
  724. 0,
  725. 0,
  726. )
  727. }
  728. func (d *Device) CheckFormatSupport(format uint32) (uint32, error) {
  729. var support uint32
  730. r, _, _ := syscall.Syscall(
  731. d.Vtbl.CheckFormatSupport,
  732. 3,
  733. uintptr(unsafe.Pointer(d)),
  734. uintptr(format),
  735. uintptr(unsafe.Pointer(&support)),
  736. )
  737. if r != 0 {
  738. return 0, ErrorCode{Name: "DeviceCheckFormatSupport", Code: uint32(r)}
  739. }
  740. return support, nil
  741. }
  742. func (d *Device) CreateBuffer(desc *BUFFER_DESC, data []byte) (*Buffer, error) {
  743. var dataDesc *SUBRESOURCE_DATA
  744. if len(data) > 0 {
  745. dataDesc = &SUBRESOURCE_DATA{
  746. pSysMem: &data[0],
  747. }
  748. }
  749. var buf *Buffer
  750. r, _, _ := syscall.Syscall6(
  751. d.Vtbl.CreateBuffer,
  752. 4,
  753. uintptr(unsafe.Pointer(d)),
  754. uintptr(unsafe.Pointer(desc)),
  755. uintptr(unsafe.Pointer(dataDesc)),
  756. uintptr(unsafe.Pointer(&buf)),
  757. 0, 0,
  758. )
  759. if r != 0 {
  760. return nil, ErrorCode{Name: "DeviceCreateBuffer", Code: uint32(r)}
  761. }
  762. return buf, nil
  763. }
  764. func (d *Device) CreateDepthStencilViewTEX2D(res *Resource, desc *DEPTH_STENCIL_VIEW_DESC_TEX2D) (*DepthStencilView, error) {
  765. var view *DepthStencilView
  766. r, _, _ := syscall.Syscall6(
  767. d.Vtbl.CreateDepthStencilView,
  768. 4,
  769. uintptr(unsafe.Pointer(d)),
  770. uintptr(unsafe.Pointer(res)),
  771. uintptr(unsafe.Pointer(desc)),
  772. uintptr(unsafe.Pointer(&view)),
  773. 0, 0,
  774. )
  775. if r != 0 {
  776. return nil, ErrorCode{Name: "DeviceCreateDepthStencilView", Code: uint32(r)}
  777. }
  778. return view, nil
  779. }
  780. func (d *Device) CreatePixelShader(bytecode []byte) (*PixelShader, error) {
  781. var shader *PixelShader
  782. r, _, _ := syscall.Syscall6(
  783. d.Vtbl.CreatePixelShader,
  784. 5,
  785. uintptr(unsafe.Pointer(d)),
  786. uintptr(unsafe.Pointer(&bytecode[0])),
  787. uintptr(len(bytecode)),
  788. 0, // pClassLinkage
  789. uintptr(unsafe.Pointer(&shader)),
  790. 0,
  791. )
  792. if r != 0 {
  793. return nil, ErrorCode{Name: "DeviceCreatePixelShader", Code: uint32(r)}
  794. }
  795. return shader, nil
  796. }
  797. func (d *Device) CreateVertexShader(bytecode []byte) (*VertexShader, error) {
  798. var shader *VertexShader
  799. r, _, _ := syscall.Syscall6(
  800. d.Vtbl.CreateVertexShader,
  801. 5,
  802. uintptr(unsafe.Pointer(d)),
  803. uintptr(unsafe.Pointer(&bytecode[0])),
  804. uintptr(len(bytecode)),
  805. 0, // pClassLinkage
  806. uintptr(unsafe.Pointer(&shader)),
  807. 0,
  808. )
  809. if r != 0 {
  810. return nil, ErrorCode{Name: "DeviceCreateVertexShader", Code: uint32(r)}
  811. }
  812. return shader, nil
  813. }
  814. func (d *Device) CreateComputeShader(bytecode []byte) (*ComputeShader, error) {
  815. var shader *ComputeShader
  816. r, _, _ := syscall.Syscall6(
  817. d.Vtbl.CreateComputeShader,
  818. 5,
  819. uintptr(unsafe.Pointer(d)),
  820. uintptr(unsafe.Pointer(&bytecode[0])),
  821. uintptr(len(bytecode)),
  822. 0, // pClassLinkage
  823. uintptr(unsafe.Pointer(&shader)),
  824. 0,
  825. )
  826. if r != 0 {
  827. return nil, ErrorCode{Name: "DeviceCreateComputeShader", Code: uint32(r)}
  828. }
  829. return shader, nil
  830. }
  831. func (d *Device) CreateShaderResourceView(res *Resource, desc unsafe.Pointer) (*ShaderResourceView, error) {
  832. var resView *ShaderResourceView
  833. r, _, _ := syscall.Syscall6(
  834. d.Vtbl.CreateShaderResourceView,
  835. 4,
  836. uintptr(unsafe.Pointer(d)),
  837. uintptr(unsafe.Pointer(res)),
  838. uintptr(desc),
  839. uintptr(unsafe.Pointer(&resView)),
  840. 0, 0,
  841. )
  842. if r != 0 {
  843. return nil, ErrorCode{Name: "DeviceCreateShaderResourceView", Code: uint32(r)}
  844. }
  845. return resView, nil
  846. }
  847. func (d *Device) CreateUnorderedAccessView(res *Resource, desc unsafe.Pointer) (*UnorderedAccessView, error) {
  848. var uaView *UnorderedAccessView
  849. r, _, _ := syscall.Syscall6(
  850. d.Vtbl.CreateUnorderedAccessView,
  851. 4,
  852. uintptr(unsafe.Pointer(d)),
  853. uintptr(unsafe.Pointer(res)),
  854. uintptr(desc),
  855. uintptr(unsafe.Pointer(&uaView)),
  856. 0, 0,
  857. )
  858. if r != 0 {
  859. return nil, ErrorCode{Name: "DeviceCreateUnorderedAccessView", Code: uint32(r)}
  860. }
  861. return uaView, nil
  862. }
  863. func (d *Device) CreateRasterizerState(desc *RASTERIZER_DESC) (*RasterizerState, error) {
  864. var state *RasterizerState
  865. r, _, _ := syscall.Syscall(
  866. d.Vtbl.CreateRasterizerState,
  867. 3,
  868. uintptr(unsafe.Pointer(d)),
  869. uintptr(unsafe.Pointer(desc)),
  870. uintptr(unsafe.Pointer(&state)),
  871. )
  872. if r != 0 {
  873. return nil, ErrorCode{Name: "DeviceCreateRasterizerState", Code: uint32(r)}
  874. }
  875. return state, nil
  876. }
  877. func (d *Device) CreateInputLayout(descs []INPUT_ELEMENT_DESC, bytecode []byte) (*InputLayout, error) {
  878. var pdesc *INPUT_ELEMENT_DESC
  879. if len(descs) > 0 {
  880. pdesc = &descs[0]
  881. }
  882. var layout *InputLayout
  883. r, _, _ := syscall.Syscall6(
  884. d.Vtbl.CreateInputLayout,
  885. 6,
  886. uintptr(unsafe.Pointer(d)),
  887. uintptr(unsafe.Pointer(pdesc)),
  888. uintptr(len(descs)),
  889. uintptr(unsafe.Pointer(&bytecode[0])),
  890. uintptr(len(bytecode)),
  891. uintptr(unsafe.Pointer(&layout)),
  892. )
  893. if r != 0 {
  894. return nil, ErrorCode{Name: "DeviceCreateInputLayout", Code: uint32(r)}
  895. }
  896. return layout, nil
  897. }
  898. func (d *Device) CreateSamplerState(desc *SAMPLER_DESC) (*SamplerState, error) {
  899. var sampler *SamplerState
  900. r, _, _ := syscall.Syscall(
  901. d.Vtbl.CreateSamplerState,
  902. 3,
  903. uintptr(unsafe.Pointer(d)),
  904. uintptr(unsafe.Pointer(desc)),
  905. uintptr(unsafe.Pointer(&sampler)),
  906. )
  907. if r != 0 {
  908. return nil, ErrorCode{Name: "DeviceCreateSamplerState", Code: uint32(r)}
  909. }
  910. return sampler, nil
  911. }
  912. func (d *Device) CreateTexture2D(desc *TEXTURE2D_DESC) (*Texture2D, error) {
  913. var tex *Texture2D
  914. r, _, _ := syscall.Syscall6(
  915. d.Vtbl.CreateTexture2D,
  916. 4,
  917. uintptr(unsafe.Pointer(d)),
  918. uintptr(unsafe.Pointer(desc)),
  919. 0, // pInitialData
  920. uintptr(unsafe.Pointer(&tex)),
  921. 0, 0,
  922. )
  923. if r != 0 {
  924. return nil, ErrorCode{Name: "CreateTexture2D", Code: uint32(r)}
  925. }
  926. return tex, nil
  927. }
  928. func (d *Device) CreateRenderTargetView(res *Resource) (*RenderTargetView, error) {
  929. var target *RenderTargetView
  930. r, _, _ := syscall.Syscall6(
  931. d.Vtbl.CreateRenderTargetView,
  932. 4,
  933. uintptr(unsafe.Pointer(d)),
  934. uintptr(unsafe.Pointer(res)),
  935. 0, // pDesc
  936. uintptr(unsafe.Pointer(&target)),
  937. 0, 0,
  938. )
  939. if r != 0 {
  940. return nil, ErrorCode{Name: "DeviceCreateRenderTargetView", Code: uint32(r)}
  941. }
  942. return target, nil
  943. }
  944. func (d *Device) CreateBlendState(desc *BLEND_DESC) (*BlendState, error) {
  945. var state *BlendState
  946. r, _, _ := syscall.Syscall(
  947. d.Vtbl.CreateBlendState,
  948. 3,
  949. uintptr(unsafe.Pointer(d)),
  950. uintptr(unsafe.Pointer(desc)),
  951. uintptr(unsafe.Pointer(&state)),
  952. )
  953. if r != 0 {
  954. return nil, ErrorCode{Name: "DeviceCreateBlendState", Code: uint32(r)}
  955. }
  956. return state, nil
  957. }
  958. func (d *Device) CreateDepthStencilState(desc *DEPTH_STENCIL_DESC) (*DepthStencilState, error) {
  959. var state *DepthStencilState
  960. r, _, _ := syscall.Syscall(
  961. d.Vtbl.CreateDepthStencilState,
  962. 3,
  963. uintptr(unsafe.Pointer(d)),
  964. uintptr(unsafe.Pointer(desc)),
  965. uintptr(unsafe.Pointer(&state)),
  966. )
  967. if r != 0 {
  968. return nil, ErrorCode{Name: "DeviceCreateDepthStencilState", Code: uint32(r)}
  969. }
  970. return state, nil
  971. }
  972. func (d *Device) GetFeatureLevel() int {
  973. lvl, _, _ := syscall.Syscall(
  974. d.Vtbl.GetFeatureLevel,
  975. 1,
  976. uintptr(unsafe.Pointer(d)),
  977. 0, 0,
  978. )
  979. return int(lvl)
  980. }
  981. func (d *Device) GetImmediateContext() *DeviceContext {
  982. var ctx *DeviceContext
  983. syscall.Syscall(
  984. d.Vtbl.GetImmediateContext,
  985. 2,
  986. uintptr(unsafe.Pointer(d)),
  987. uintptr(unsafe.Pointer(&ctx)),
  988. 0,
  989. )
  990. return ctx
  991. }
  992. func (d *Device) ReportLiveDeviceObjects() error {
  993. intf, err := IUnknownQueryInterface(unsafe.Pointer(d), d.Vtbl.QueryInterface, &IID_ID3D11Debug)
  994. if err != nil {
  995. return fmt.Errorf("ReportLiveObjects: failed to query ID3D11Debug interface: %v", err)
  996. }
  997. defer IUnknownRelease(unsafe.Pointer(intf), intf.Vtbl.Release)
  998. dbg := (*Debug)(unsafe.Pointer(intf))
  999. dbg.ReportLiveDeviceObjects(RLDO_DETAIL | RLDO_IGNORE_INTERNAL)
  1000. return nil
  1001. }
  1002. func (d *Debug) ReportLiveDeviceObjects(flags uint32) {
  1003. syscall.Syscall(
  1004. d.Vtbl.ReportLiveDeviceObjects,
  1005. 2,
  1006. uintptr(unsafe.Pointer(d)),
  1007. uintptr(flags),
  1008. 0,
  1009. )
  1010. }
  1011. func (s *IDXGISwapChain) GetDesc() (DXGI_SWAP_CHAIN_DESC, error) {
  1012. var desc DXGI_SWAP_CHAIN_DESC
  1013. r, _, _ := syscall.Syscall(
  1014. s.Vtbl.GetDesc,
  1015. 2,
  1016. uintptr(unsafe.Pointer(s)),
  1017. uintptr(unsafe.Pointer(&desc)),
  1018. 0,
  1019. )
  1020. if r != 0 {
  1021. return DXGI_SWAP_CHAIN_DESC{}, ErrorCode{Name: "IDXGISwapChainGetDesc", Code: uint32(r)}
  1022. }
  1023. return desc, nil
  1024. }
  1025. func (s *IDXGISwapChain) ResizeBuffers(buffers, width, height, newFormat, flags uint32) error {
  1026. r, _, _ := syscall.Syscall6(
  1027. s.Vtbl.ResizeBuffers,
  1028. 6,
  1029. uintptr(unsafe.Pointer(s)),
  1030. uintptr(buffers),
  1031. uintptr(width),
  1032. uintptr(height),
  1033. uintptr(newFormat),
  1034. uintptr(flags),
  1035. )
  1036. if r != 0 {
  1037. return ErrorCode{Name: "IDXGISwapChainResizeBuffers", Code: uint32(r)}
  1038. }
  1039. return nil
  1040. }
  1041. func (s *IDXGISwapChain) Present(SyncInterval int, Flags uint32) error {
  1042. r, _, _ := syscall.Syscall(
  1043. s.Vtbl.Present,
  1044. 3,
  1045. uintptr(unsafe.Pointer(s)),
  1046. uintptr(SyncInterval),
  1047. uintptr(Flags),
  1048. )
  1049. if r != 0 {
  1050. return ErrorCode{Name: "IDXGISwapChainPresent", Code: uint32(r)}
  1051. }
  1052. return nil
  1053. }
  1054. func (s *IDXGISwapChain) GetBuffer(index int, riid *GUID) (*IUnknown, error) {
  1055. var buf *IUnknown
  1056. r, _, _ := syscall.Syscall6(
  1057. s.Vtbl.GetBuffer,
  1058. 4,
  1059. uintptr(unsafe.Pointer(s)),
  1060. uintptr(index),
  1061. uintptr(unsafe.Pointer(riid)),
  1062. uintptr(unsafe.Pointer(&buf)),
  1063. 0,
  1064. 0,
  1065. )
  1066. if r != 0 {
  1067. return nil, ErrorCode{Name: "IDXGISwapChainGetBuffer", Code: uint32(r)}
  1068. }
  1069. return buf, nil
  1070. }
  1071. func (c *DeviceContext) GenerateMips(res *ShaderResourceView) {
  1072. syscall.Syscall(
  1073. c.Vtbl.GenerateMips,
  1074. 2,
  1075. uintptr(unsafe.Pointer(c)),
  1076. uintptr(unsafe.Pointer(res)),
  1077. 0,
  1078. )
  1079. }
  1080. func (c *DeviceContext) Unmap(resource *Resource, subResource uint32) {
  1081. syscall.Syscall(
  1082. c.Vtbl.Unmap,
  1083. 3,
  1084. uintptr(unsafe.Pointer(c)),
  1085. uintptr(unsafe.Pointer(resource)),
  1086. uintptr(subResource),
  1087. )
  1088. }
  1089. func (c *DeviceContext) Map(resource *Resource, subResource, mapType, mapFlags uint32) (MAPPED_SUBRESOURCE, error) {
  1090. var resMap MAPPED_SUBRESOURCE
  1091. r, _, _ := syscall.Syscall6(
  1092. c.Vtbl.Map,
  1093. 6,
  1094. uintptr(unsafe.Pointer(c)),
  1095. uintptr(unsafe.Pointer(resource)),
  1096. uintptr(subResource),
  1097. uintptr(mapType),
  1098. uintptr(mapFlags),
  1099. uintptr(unsafe.Pointer(&resMap)),
  1100. )
  1101. if r != 0 {
  1102. return resMap, ErrorCode{Name: "DeviceContextMap", Code: uint32(r)}
  1103. }
  1104. return resMap, nil
  1105. }
  1106. func (c *DeviceContext) CopySubresourceRegion(dst *Resource, dstSubresource, dstX, dstY, dstZ uint32, src *Resource, srcSubresource uint32, srcBox *BOX) {
  1107. syscall.Syscall9(
  1108. c.Vtbl.CopySubresourceRegion,
  1109. 9,
  1110. uintptr(unsafe.Pointer(c)),
  1111. uintptr(unsafe.Pointer(dst)),
  1112. uintptr(dstSubresource),
  1113. uintptr(dstX),
  1114. uintptr(dstY),
  1115. uintptr(dstZ),
  1116. uintptr(unsafe.Pointer(src)),
  1117. uintptr(srcSubresource),
  1118. uintptr(unsafe.Pointer(srcBox)),
  1119. )
  1120. }
  1121. func (c *DeviceContext) ClearDepthStencilView(target *DepthStencilView, flags uint32, depth float32, stencil uint8) {
  1122. syscall.Syscall6(
  1123. c.Vtbl.ClearDepthStencilView,
  1124. 5,
  1125. uintptr(unsafe.Pointer(c)),
  1126. uintptr(unsafe.Pointer(target)),
  1127. uintptr(flags),
  1128. uintptr(math.Float32bits(depth)),
  1129. uintptr(stencil),
  1130. 0,
  1131. )
  1132. }
  1133. func (c *DeviceContext) ClearRenderTargetView(target *RenderTargetView, color *[4]float32) {
  1134. syscall.Syscall(
  1135. c.Vtbl.ClearRenderTargetView,
  1136. 3,
  1137. uintptr(unsafe.Pointer(c)),
  1138. uintptr(unsafe.Pointer(target)),
  1139. uintptr(unsafe.Pointer(color)),
  1140. )
  1141. }
  1142. func (c *DeviceContext) CSSetShaderResources(startSlot uint32, s *ShaderResourceView) {
  1143. syscall.Syscall6(
  1144. c.Vtbl.CSSetShaderResources,
  1145. 4,
  1146. uintptr(unsafe.Pointer(c)),
  1147. uintptr(startSlot),
  1148. 1, // NumViews
  1149. uintptr(unsafe.Pointer(&s)),
  1150. 0, 0,
  1151. )
  1152. }
  1153. func (c *DeviceContext) CSSetUnorderedAccessViews(startSlot uint32, v *UnorderedAccessView) {
  1154. syscall.Syscall6(
  1155. c.Vtbl.CSSetUnorderedAccessViews,
  1156. 4,
  1157. uintptr(unsafe.Pointer(c)),
  1158. uintptr(startSlot),
  1159. 1, // NumViews
  1160. uintptr(unsafe.Pointer(&v)),
  1161. 0, 0,
  1162. )
  1163. }
  1164. func (c *DeviceContext) CSSetShader(s *ComputeShader) {
  1165. syscall.Syscall6(
  1166. c.Vtbl.CSSetShader,
  1167. 4,
  1168. uintptr(unsafe.Pointer(c)),
  1169. uintptr(unsafe.Pointer(s)),
  1170. 0, // ppClassInstances
  1171. 0, // NumClassInstances
  1172. 0, 0,
  1173. )
  1174. }
  1175. func (c *DeviceContext) RSSetViewports(viewport *VIEWPORT) {
  1176. syscall.Syscall(
  1177. c.Vtbl.RSSetViewports,
  1178. 3,
  1179. uintptr(unsafe.Pointer(c)),
  1180. 1, // NumViewports
  1181. uintptr(unsafe.Pointer(viewport)),
  1182. )
  1183. }
  1184. func (c *DeviceContext) VSSetShader(s *VertexShader) {
  1185. syscall.Syscall6(
  1186. c.Vtbl.VSSetShader,
  1187. 4,
  1188. uintptr(unsafe.Pointer(c)),
  1189. uintptr(unsafe.Pointer(s)),
  1190. 0, // ppClassInstances
  1191. 0, // NumClassInstances
  1192. 0, 0,
  1193. )
  1194. }
  1195. func (c *DeviceContext) VSSetConstantBuffers(b *Buffer) {
  1196. syscall.Syscall6(
  1197. c.Vtbl.VSSetConstantBuffers,
  1198. 4,
  1199. uintptr(unsafe.Pointer(c)),
  1200. 0, // StartSlot
  1201. 1, // NumBuffers
  1202. uintptr(unsafe.Pointer(&b)),
  1203. 0, 0,
  1204. )
  1205. }
  1206. func (c *DeviceContext) PSSetConstantBuffers(b *Buffer) {
  1207. syscall.Syscall6(
  1208. c.Vtbl.PSSetConstantBuffers,
  1209. 4,
  1210. uintptr(unsafe.Pointer(c)),
  1211. 0, // StartSlot
  1212. 1, // NumBuffers
  1213. uintptr(unsafe.Pointer(&b)),
  1214. 0, 0,
  1215. )
  1216. }
  1217. func (c *DeviceContext) PSSetShaderResources(startSlot uint32, s *ShaderResourceView) {
  1218. syscall.Syscall6(
  1219. c.Vtbl.PSSetShaderResources,
  1220. 4,
  1221. uintptr(unsafe.Pointer(c)),
  1222. uintptr(startSlot),
  1223. 1, // NumViews
  1224. uintptr(unsafe.Pointer(&s)),
  1225. 0, 0,
  1226. )
  1227. }
  1228. func (c *DeviceContext) PSSetSamplers(startSlot uint32, s *SamplerState) {
  1229. syscall.Syscall6(
  1230. c.Vtbl.PSSetSamplers,
  1231. 4,
  1232. uintptr(unsafe.Pointer(c)),
  1233. uintptr(startSlot),
  1234. 1, // NumSamplers
  1235. uintptr(unsafe.Pointer(&s)),
  1236. 0, 0,
  1237. )
  1238. }
  1239. func (c *DeviceContext) PSSetShader(s *PixelShader) {
  1240. syscall.Syscall6(
  1241. c.Vtbl.PSSetShader,
  1242. 4,
  1243. uintptr(unsafe.Pointer(c)),
  1244. uintptr(unsafe.Pointer(s)),
  1245. 0, // ppClassInstances
  1246. 0, // NumClassInstances
  1247. 0, 0,
  1248. )
  1249. }
  1250. func (c *DeviceContext) UpdateSubresource(res *Resource, dstBox *BOX, rowPitch, depthPitch uint32, data []byte) {
  1251. syscall.Syscall9(
  1252. c.Vtbl.UpdateSubresource,
  1253. 7,
  1254. uintptr(unsafe.Pointer(c)),
  1255. uintptr(unsafe.Pointer(res)),
  1256. 0, // DstSubresource
  1257. uintptr(unsafe.Pointer(dstBox)),
  1258. uintptr(unsafe.Pointer(&data[0])),
  1259. uintptr(rowPitch),
  1260. uintptr(depthPitch),
  1261. 0, 0,
  1262. )
  1263. }
  1264. func (c *DeviceContext) RSSetState(state *RasterizerState) {
  1265. syscall.Syscall(
  1266. c.Vtbl.RSSetState,
  1267. 2,
  1268. uintptr(unsafe.Pointer(c)),
  1269. uintptr(unsafe.Pointer(state)),
  1270. 0,
  1271. )
  1272. }
  1273. func (c *DeviceContext) IASetInputLayout(layout *InputLayout) {
  1274. syscall.Syscall(
  1275. c.Vtbl.IASetInputLayout,
  1276. 2,
  1277. uintptr(unsafe.Pointer(c)),
  1278. uintptr(unsafe.Pointer(layout)),
  1279. 0,
  1280. )
  1281. }
  1282. func (c *DeviceContext) IASetIndexBuffer(buf *Buffer, format, offset uint32) {
  1283. syscall.Syscall6(
  1284. c.Vtbl.IASetIndexBuffer,
  1285. 4,
  1286. uintptr(unsafe.Pointer(c)),
  1287. uintptr(unsafe.Pointer(buf)),
  1288. uintptr(format),
  1289. uintptr(offset),
  1290. 0, 0,
  1291. )
  1292. }
  1293. func (c *DeviceContext) IASetVertexBuffers(buf *Buffer, stride, offset uint32) {
  1294. syscall.Syscall6(
  1295. c.Vtbl.IASetVertexBuffers,
  1296. 6,
  1297. uintptr(unsafe.Pointer(c)),
  1298. 0, // StartSlot
  1299. 1, // NumBuffers,
  1300. uintptr(unsafe.Pointer(&buf)),
  1301. uintptr(unsafe.Pointer(&stride)),
  1302. uintptr(unsafe.Pointer(&offset)),
  1303. )
  1304. }
  1305. func (c *DeviceContext) IASetPrimitiveTopology(mode uint32) {
  1306. syscall.Syscall(
  1307. c.Vtbl.IASetPrimitiveTopology,
  1308. 2,
  1309. uintptr(unsafe.Pointer(c)),
  1310. uintptr(mode),
  1311. 0,
  1312. )
  1313. }
  1314. func (c *DeviceContext) OMGetRenderTargets() (*RenderTargetView, *DepthStencilView) {
  1315. var (
  1316. target *RenderTargetView
  1317. depthStencilView *DepthStencilView
  1318. )
  1319. syscall.Syscall6(
  1320. c.Vtbl.OMGetRenderTargets,
  1321. 4,
  1322. uintptr(unsafe.Pointer(c)),
  1323. 1, // NumViews
  1324. uintptr(unsafe.Pointer(&target)),
  1325. uintptr(unsafe.Pointer(&depthStencilView)),
  1326. 0, 0,
  1327. )
  1328. return target, depthStencilView
  1329. }
  1330. func (c *DeviceContext) OMSetRenderTargets(target *RenderTargetView, depthStencil *DepthStencilView) {
  1331. syscall.Syscall6(
  1332. c.Vtbl.OMSetRenderTargets,
  1333. 4,
  1334. uintptr(unsafe.Pointer(c)),
  1335. 1, // NumViews
  1336. uintptr(unsafe.Pointer(&target)),
  1337. uintptr(unsafe.Pointer(depthStencil)),
  1338. 0, 0,
  1339. )
  1340. }
  1341. func (c *DeviceContext) Draw(count, start uint32) {
  1342. syscall.Syscall(
  1343. c.Vtbl.Draw,
  1344. 3,
  1345. uintptr(unsafe.Pointer(c)),
  1346. uintptr(count),
  1347. uintptr(start),
  1348. )
  1349. }
  1350. func (c *DeviceContext) DrawIndexed(count, start uint32, base int32) {
  1351. syscall.Syscall6(
  1352. c.Vtbl.DrawIndexed,
  1353. 4,
  1354. uintptr(unsafe.Pointer(c)),
  1355. uintptr(count),
  1356. uintptr(start),
  1357. uintptr(base),
  1358. 0, 0,
  1359. )
  1360. }
  1361. func (c *DeviceContext) Dispatch(x, y, z uint32) {
  1362. syscall.Syscall6(
  1363. c.Vtbl.Dispatch,
  1364. 4,
  1365. uintptr(unsafe.Pointer(c)),
  1366. uintptr(x),
  1367. uintptr(y),
  1368. uintptr(z),
  1369. 0, 0,
  1370. )
  1371. }
  1372. func (c *DeviceContext) OMSetBlendState(state *BlendState, factor *f32color.RGBA, sampleMask uint32) {
  1373. syscall.Syscall6(
  1374. c.Vtbl.OMSetBlendState,
  1375. 4,
  1376. uintptr(unsafe.Pointer(c)),
  1377. uintptr(unsafe.Pointer(state)),
  1378. uintptr(unsafe.Pointer(factor)),
  1379. uintptr(sampleMask),
  1380. 0, 0,
  1381. )
  1382. }
  1383. func (c *DeviceContext) OMSetDepthStencilState(state *DepthStencilState, stencilRef uint32) {
  1384. syscall.Syscall(
  1385. c.Vtbl.OMSetDepthStencilState,
  1386. 3,
  1387. uintptr(unsafe.Pointer(c)),
  1388. uintptr(unsafe.Pointer(state)),
  1389. uintptr(stencilRef),
  1390. )
  1391. }
  1392. func (d *IDXGIObject) GetParent(guid *GUID) (*IDXGIObject, error) {
  1393. var parent *IDXGIObject
  1394. r, _, _ := syscall.Syscall(
  1395. d.Vtbl.GetParent,
  1396. 3,
  1397. uintptr(unsafe.Pointer(d)),
  1398. uintptr(unsafe.Pointer(guid)),
  1399. uintptr(unsafe.Pointer(&parent)),
  1400. )
  1401. if r != 0 {
  1402. return nil, ErrorCode{Name: "IDXGIObjectGetParent", Code: uint32(r)}
  1403. }
  1404. return parent, nil
  1405. }
  1406. func (d *IDXGIFactory) CreateSwapChain(device *IUnknown, desc *DXGI_SWAP_CHAIN_DESC) (*IDXGISwapChain, error) {
  1407. var swchain *IDXGISwapChain
  1408. r, _, _ := syscall.Syscall6(
  1409. d.Vtbl.CreateSwapChain,
  1410. 4,
  1411. uintptr(unsafe.Pointer(d)),
  1412. uintptr(unsafe.Pointer(device)),
  1413. uintptr(unsafe.Pointer(desc)),
  1414. uintptr(unsafe.Pointer(&swchain)),
  1415. 0, 0,
  1416. )
  1417. if r != 0 {
  1418. return nil, ErrorCode{Name: "IDXGIFactory", Code: uint32(r)}
  1419. }
  1420. return swchain, nil
  1421. }
  1422. func (d *IDXGIDevice) GetAdapter() (*IDXGIAdapter, error) {
  1423. var adapter *IDXGIAdapter
  1424. r, _, _ := syscall.Syscall(
  1425. d.Vtbl.GetAdapter,
  1426. 2,
  1427. uintptr(unsafe.Pointer(d)),
  1428. uintptr(unsafe.Pointer(&adapter)),
  1429. 0,
  1430. )
  1431. if r != 0 {
  1432. return nil, ErrorCode{Name: "IDXGIDeviceGetAdapter", Code: uint32(r)}
  1433. }
  1434. return adapter, nil
  1435. }
  1436. func IUnknownQueryInterface(obj unsafe.Pointer, queryInterfaceMethod uintptr, guid *GUID) (*IUnknown, error) {
  1437. var ref *IUnknown
  1438. r, _, _ := syscall.Syscall(
  1439. queryInterfaceMethod,
  1440. 3,
  1441. uintptr(obj),
  1442. uintptr(unsafe.Pointer(guid)),
  1443. uintptr(unsafe.Pointer(&ref)),
  1444. )
  1445. if r != 0 {
  1446. return nil, ErrorCode{Name: "IUnknownQueryInterface", Code: uint32(r)}
  1447. }
  1448. return ref, nil
  1449. }
  1450. func IUnknownAddRef(obj unsafe.Pointer, addRefMethod uintptr) {
  1451. syscall.Syscall(
  1452. addRefMethod,
  1453. 1,
  1454. uintptr(obj),
  1455. 0,
  1456. 0,
  1457. )
  1458. }
  1459. func IUnknownRelease(obj unsafe.Pointer, releaseMethod uintptr) {
  1460. syscall.Syscall(
  1461. releaseMethod,
  1462. 1,
  1463. uintptr(obj),
  1464. 0,
  1465. 0,
  1466. )
  1467. }
  1468. func (e ErrorCode) Error() string {
  1469. return fmt.Sprintf("%s: %#x", e.Name, e.Code)
  1470. }
  1471. func CreateSwapChain(dev *Device, hwnd windows.Handle) (*IDXGISwapChain, error) {
  1472. dxgiDev, err := IUnknownQueryInterface(unsafe.Pointer(dev), dev.Vtbl.QueryInterface, &IID_IDXGIDevice)
  1473. if err != nil {
  1474. return nil, fmt.Errorf("NewContext: %v", err)
  1475. }
  1476. adapter, err := (*IDXGIDevice)(unsafe.Pointer(dxgiDev)).GetAdapter()
  1477. IUnknownRelease(unsafe.Pointer(dxgiDev), dxgiDev.Vtbl.Release)
  1478. if err != nil {
  1479. return nil, fmt.Errorf("NewContext: %v", err)
  1480. }
  1481. dxgiFactory, err := (*IDXGIObject)(unsafe.Pointer(adapter)).GetParent(&IID_IDXGIFactory)
  1482. IUnknownRelease(unsafe.Pointer(adapter), adapter.Vtbl.Release)
  1483. if err != nil {
  1484. return nil, fmt.Errorf("NewContext: %v", err)
  1485. }
  1486. swchain, err := (*IDXGIFactory)(unsafe.Pointer(dxgiFactory)).CreateSwapChain(
  1487. (*IUnknown)(unsafe.Pointer(dev)),
  1488. &DXGI_SWAP_CHAIN_DESC{
  1489. BufferDesc: DXGI_MODE_DESC{
  1490. Format: DXGI_FORMAT_R8G8B8A8_UNORM_SRGB,
  1491. },
  1492. SampleDesc: DXGI_SAMPLE_DESC{
  1493. Count: 1,
  1494. },
  1495. BufferUsage: DXGI_USAGE_RENDER_TARGET_OUTPUT,
  1496. BufferCount: 1,
  1497. OutputWindow: hwnd,
  1498. Windowed: 1,
  1499. SwapEffect: DXGI_SWAP_EFFECT_DISCARD,
  1500. },
  1501. )
  1502. IUnknownRelease(unsafe.Pointer(dxgiFactory), dxgiFactory.Vtbl.Release)
  1503. if err != nil {
  1504. return nil, fmt.Errorf("NewContext: %v", err)
  1505. }
  1506. return swchain, nil
  1507. }
  1508. func CreateDepthView(d *Device, width, height, depthBits int) (*DepthStencilView, error) {
  1509. depthTex, err := d.CreateTexture2D(&TEXTURE2D_DESC{
  1510. Width: uint32(width),
  1511. Height: uint32(height),
  1512. MipLevels: 1,
  1513. ArraySize: 1,
  1514. Format: DXGI_FORMAT_D24_UNORM_S8_UINT,
  1515. SampleDesc: DXGI_SAMPLE_DESC{
  1516. Count: 1,
  1517. Quality: 0,
  1518. },
  1519. BindFlags: BIND_DEPTH_STENCIL,
  1520. })
  1521. if err != nil {
  1522. return nil, err
  1523. }
  1524. depthView, err := d.CreateDepthStencilViewTEX2D(
  1525. (*Resource)(unsafe.Pointer(depthTex)),
  1526. &DEPTH_STENCIL_VIEW_DESC_TEX2D{
  1527. Format: DXGI_FORMAT_D24_UNORM_S8_UINT,
  1528. ViewDimension: DSV_DIMENSION_TEXTURE2D,
  1529. },
  1530. )
  1531. IUnknownRelease(unsafe.Pointer(depthTex), depthTex.Vtbl.Release)
  1532. return depthView, err
  1533. }