github.com/Seikaijyu/gio@v0.0.1/internal/d3d11/d3d11_windows.go (about)

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