public static void NameD3D12Object(ID3D12Fence *pObject, string name) { fixed(char *pName = name) { pObject->SetName(pName); } }
private static void WaitForFence(ID3D12Fence *fence, IntPtr fenceEvent, ulong fenceValue) { if (fence->GetCompletedValue() < fenceValue) { _ = fence->SetEventOnCompletion(fenceValue, fenceEvent); _ = WaitForSingleObject(fenceEvent, INFINITE); } }
private ID3D12Fence *[] CreateFences() { var fences = new ID3D12Fence *[_graphicsSurface.BufferCount]; var iid = IID_ID3D12Fence; for (var i = 0; i < fences.Length; i++) { ID3D12Fence *fence; ThrowExternalExceptionIfFailed(nameof(ID3D12Device.CreateFence), Device->CreateFence(InitialValue: 0, D3D12_FENCE_FLAG_NONE, &iid, (void **)&fence)); fences[i] = fence; } return(fences); }
public static ID3D12Fence *GetLatestD3D12Fence(ID3D12Fence *d3d12Fence, out uint d3d12FenceVersion) { ID3D12Fence *result; if (d3d12Fence->QueryInterface(__uuidof <ID3D12Fence1>(), (void **)&result).SUCCEEDED) { d3d12FenceVersion = 1; _ = d3d12Fence->Release(); } else { d3d12FenceVersion = 0; result = d3d12Fence; } return(result); }
protected override void Dispose(bool isDisposing) { var swapChain = _swapChain; if (swapChain != null) { _swapChain = null; _ = swapChain->Release(); } var device = _device; if (device != null) { _device = null; _ = device->Release(); } for (var index = 0; index < FrameCount; index++) { var renderTarget = _renderTargets[index]; if (renderTarget != null) { _renderTargets[index] = null; _ = renderTarget->Release(); } } var commandAllocator = _commandAllocator; if (commandAllocator != null) { _commandAllocator = null; _ = commandAllocator->Release(); } var bundleAllocator = _bundleAllocator; if (bundleAllocator != null) { _bundleAllocator = null; _ = bundleAllocator->Release(); } var commandQueue = _commandQueue; if (commandQueue != null) { _commandQueue = null; _ = commandQueue->Release(); } var rootSignature = _rootSignature; if (rootSignature != null) { _rootSignature = null; _ = rootSignature->Release(); } var rtvHeap = _rtvHeap; if (rtvHeap != null) { _rtvHeap = null; _ = rtvHeap->Release(); } var pipelineState = _pipelineState; if (pipelineState != null) { _pipelineState = null; _ = pipelineState->Release(); } var commandList = _commandList; if (commandList != null) { _commandList = null; _ = commandList->Release(); } var bundle = _bundle; if (bundle != null) { _bundle = null; _ = bundle->Release(); } var vertexBuffer = _vertexBuffer; if (vertexBuffer != null) { _vertexBuffer = null; _ = vertexBuffer->Release(); } var fence = _fence; if (fence != null) { _fence = null; _ = fence->Release(); } base.Dispose(isDisposing); }
protected override void DestroyDeviceDependentResources() { DestroyAssets(); DestroyGraphicsCommandLists(); DestroyPipelineState(); DestroyRootSignature(); DestroyFenceEvent(); DestroyFence(); DestroyCommandAllocators(); DestroyDescriptorHeaps(); DestroyCommandQueue(); DestroyD3DDevice(); DestroyDxgiAdapter(); DestroyDxgiFactory(); void DestroyCommandAllocators() { for (var i = 0; i < _commandAllocators.Length; i++) { var commandAllocator = _commandAllocators[i]; if (commandAllocator != null) { _commandAllocators[i] = null; _ = commandAllocator->Release(); } } } void DestroyCommandQueue() { var commandQueue = _commandQueue; if (commandQueue != null) { _commandQueue = null; _ = commandQueue->Release(); } } void DestroyD3DDevice() { var d3dDevice = _d3dDevice; if (d3dDevice != null) { _d3dDevice = null; _ = d3dDevice->Release(); } } void DestroyDxgiAdapter() { var dxgiAdapter = _dxgiAdapter; if (dxgiAdapter != null) { _dxgiAdapter = null; _ = dxgiAdapter->Release(); } } void DestroyDxgiFactory() { var dxgiFactory = _dxgiFactory; if (dxgiFactory != null) { _dxgiFactory = null; _ = dxgiFactory->Release(); } } void DestroyFence() { var fence = _fence; if (fence != null) { _fence = null; _ = fence->Release(); } } void DestroyFenceEvent() { var fenceEvent = _fenceEvent; if (fenceEvent != IntPtr.Zero) { _fenceEvent = IntPtr.Zero; _ = CloseHandle(_fenceEvent); } } void DestroyGraphicsCommandLists() { for (var i = 0; i < _graphicsCommandLists.Length; i++) { var graphicsCommandList = _graphicsCommandLists[i]; if (graphicsCommandList != null) { _graphicsCommandLists[i] = null; _ = graphicsCommandList->Release(); } } } void DestroyPipelineState() { var pipelineState = _pipelineState; if (pipelineState != null) { _pipelineState = null; _ = pipelineState->Release(); } } void DestroyRootSignature() { var rootSignature = _rootSignature; if (rootSignature != null) { _rootSignature = null; _ = rootSignature->Release(); } } }
protected override void CreateDeviceDependentResources() { _dxgiFactory = CreateDxgiFactory(); _dxgiAdapter = GetDxgiAdapter(); _d3dDevice = CreateD3DDevice(); _commandQueue = CreateCommandQueue(); CreateDescriptorHeaps(); for (int i = 0; i < FrameCount; i++) { _commandAllocators[i] = CreateCommandAllocator(); } _fence = CreateFence(); _fenceValues = CreateFenceValues(); _fenceEvent = CreateFenceEvent(); _rootSignature = CreateRootSignature(); _pipelineState = CreatePipelineState(); _graphicsCommandLists = CreateGraphicsCommandLists(); ThrowIfFailed(nameof(ID3D12CommandAllocator.Reset), CommandAllocator->Reset()); ThrowIfFailed(nameof(ID3D12GraphicsCommandList.Reset), GraphicsCommandList->Reset(CommandAllocator, PipelineState)); CreateAssets(); ID3D12CommandAllocator *CreateCommandAllocator() { ID3D12CommandAllocator *commandAllocator; var iid = IID_ID3D12CommandAllocator; ThrowIfFailed(nameof(ID3D12Device.CreateCommandAllocator), D3DDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, &iid, (void **)&commandAllocator)); return(commandAllocator); } ID3D12CommandQueue *CreateCommandQueue() { var queueDesc = new D3D12_COMMAND_QUEUE_DESC(); ID3D12CommandQueue *commandQueue; var iid = IID_ID3D12CommandQueue; ThrowIfFailed(nameof(ID3D12Device.CreateCommandQueue), D3DDevice->CreateCommandQueue(&queueDesc, &iid, (void **)&commandQueue)); return(commandQueue); } ID3D12Device *CreateD3DDevice() { ID3D12Device *d3dDevice; var iid = IID_ID3D12Device; ThrowIfFailed(nameof(D3D12CreateDevice), D3D12CreateDevice((IUnknown *)_dxgiAdapter, D3D_FEATURE_LEVEL_11_0, &iid, (void **)&d3dDevice)); return(d3dDevice); } IDXGIFactory4 *CreateDxgiFactory() { var dxgiFactoryFlags = TryEnableDebugLayer() ? DXGI_CREATE_FACTORY_DEBUG : 0u; IDXGIFactory4 *dxgiFactory; var iid = IID_IDXGIFactory4; ThrowIfFailed(nameof(CreateDXGIFactory2), CreateDXGIFactory2(dxgiFactoryFlags, &iid, (void **)&dxgiFactory)); return(dxgiFactory); } ID3D12Fence *CreateFence() { ID3D12Fence *fence; var iid = IID_ID3D12Fence; ThrowIfFailed(nameof(ID3D12Device.CreateFence), D3DDevice->CreateFence(InitialValue: 0, D3D12_FENCE_FLAG_NONE, &iid, (void **)&fence)); return(fence); } IntPtr CreateFenceEvent() { var fenceEvent = CreateEventW(lpEventAttributes: null, bManualReset: FALSE, bInitialState: FALSE, lpName: null); if (fenceEvent == IntPtr.Zero) { var hr = Marshal.GetHRForLastWin32Error(); Marshal.ThrowExceptionForHR(hr); } return(fenceEvent); } ulong[] CreateFenceValues() { var fenceValues = new ulong[FrameCount]; fenceValues[0] = 1; return(fenceValues); } ID3D12GraphicsCommandList *[] CreateGraphicsCommandLists() { var graphicsCommandLists = new ID3D12GraphicsCommandList *[FrameCount]; for (uint i = 0u; i < FrameCount; i++) { ID3D12GraphicsCommandList *graphicsCommandList; var iid = IID_ID3D12GraphicsCommandList; ThrowIfFailed(nameof(ID3D12Device.CreateCommandList), D3DDevice->CreateCommandList(nodeMask: 0, D3D12_COMMAND_LIST_TYPE_DIRECT, _commandAllocators[i], PipelineState, &iid, (void **)&graphicsCommandList)); ThrowIfFailed(nameof(ID3D12GraphicsCommandList.Close), graphicsCommandList->Close()); graphicsCommandLists[i] = graphicsCommandList; } return(graphicsCommandLists); } IDXGIAdapter1 *GetDxgiAdapter() { if (UseWarpDevice) { IDXGIAdapter1 *adapter; var iid = IID_IDXGIAdapter; ThrowIfFailed(nameof(IDXGIFactory4.EnumWarpAdapter), _dxgiFactory->EnumWarpAdapter(&iid, (void **)&adapter)); return(adapter); } else { return(GetHardwareAdapter((IDXGIFactory1 *)_dxgiFactory)); } } bool TryEnableDebugLayer() { #if DEBUG // Enable the debug layer (requires the Graphics Tools "optional feature"). // NOTE: Enabling the debug layer after device creation will invalidate the active device. using ComPtr <ID3D12Debug> debugController = null; var iid = IID_ID3D12Debug; if (SUCCEEDED(D3D12GetDebugInterface(&iid, (void **)&debugController))) { debugController.Get()->EnableDebugLayer(); return(true); } #endif return(false); } }
internal void Signal(ID3D12Fence *fence, FenceMarker marker) { _queue.Get()->Signal(fence, marker.FenceValue); }
public HRESULT Signal(ID3D12Fence *pFence, [NativeTypeName("UINT64")] ulong Value) { return(((delegate * unmanaged[Stdcall] < ID3D12CommandQueue *, ID3D12Fence *, ulong, int >)(lpVtbl[14]))((ID3D12CommandQueue *)Unsafe.AsPointer(ref this), pFence, Value)); }
public int EnqueueMakeResident(D3D12_RESIDENCY_FLAGS Flags, [NativeTypeName("UINT")] uint NumObjects, [NativeTypeName("ID3D12Pageable *const *")] ID3D12Pageable **ppObjects, ID3D12Fence *pFenceToSignal, [NativeTypeName("UINT64")] ulong FenceValueToSignal) { return(((delegate * unmanaged <ID3D12Device8 *, D3D12_RESIDENCY_FLAGS, uint, ID3D12Pageable **, ID3D12Fence *, ulong, int>)(lpVtbl[50]))((ID3D12Device8 *)Unsafe.AsPointer(ref this), Flags, NumObjects, ppObjects, pFenceToSignal, FenceValueToSignal)); }
public HRESULT CommitDirect3D12ResourceWithDepthData(ID3D12Resource *pColorResourceToCommit, ID3D12Fence *pColorResourceFence, [NativeTypeName("UINT64")] ulong colorResourceFenceSignalValue, ID3D12Resource *pDepthResourceToCommit, ID3D12Fence *pDepthResourceFence, [NativeTypeName("UINT64")] ulong depthResourceFenceSignalValue) { return(((delegate * unmanaged <IHolographicCameraRenderingParametersInterop *, ID3D12Resource *, ID3D12Fence *, ulong, ID3D12Resource *, ID3D12Fence *, ulong, int>)(lpVtbl[7]))((IHolographicCameraRenderingParametersInterop *)Unsafe.AsPointer(ref this), pColorResourceToCommit, pColorResourceFence, colorResourceFenceSignalValue, pDepthResourceToCommit, pDepthResourceFence, depthResourceFenceSignalValue)); }
/// <summary> /// Gets a new or reused <see cref="ID3D12CommandAllocator"/> item to use to issue GPU commands. /// </summary> /// <returns>The rented <see cref="ID3D12CommandAllocator"/> object to use.</returns> public ComPtr <ID3D12CommandAllocator> GetCommandAllocator(ID3D12Device *d3D12Device, ID3D12Fence *d3D12Fence) { using ComPtr <ID3D12CommandAllocator> d3D12CommandAllocator = default; lock (this.d3D12CommandAllocatorQueue) { if (this.d3D12CommandAllocatorQueue.TryDequeue(out * & d3D12CommandAllocator)) { d3D12CommandAllocator.Get()->Reset().Assert(); return(d3D12CommandAllocator.Move()); } return(d3D12Device->CreateCommandAllocator(this.d3D12CommandListType)); } }
public void SharedFenceSignal(ID3D12Fence *pFence, [NativeTypeName("UINT64")] ulong FenceValue) { ((delegate * unmanaged <ID3D12SharingContract *, ID3D12Fence *, ulong, void>)(lpVtbl[4]))((ID3D12SharingContract *)Unsafe.AsPointer(ref this), pFence, FenceValue); }
public int CommitDirect3D12Resource([NativeTypeName("ID3D12Resource *")] ID3D12Resource *pColorResourceToCommit, [NativeTypeName("ID3D12Fence *")] ID3D12Fence *pColorResourceFence, [NativeTypeName("UINT64")] ulong colorResourceFenceSignalValue) { return(((delegate * unmanaged <IHolographicCameraRenderingParametersInterop *, ID3D12Resource *, ID3D12Fence *, ulong, int>)(lpVtbl[6]))((IHolographicCameraRenderingParametersInterop *)Unsafe.AsPointer(ref this), pColorResourceToCommit, pColorResourceFence, colorResourceFenceSignalValue)); }
/// <summary>To be documented.</summary> public readonly unsafe void SharedFenceSignal(ID3D12Fence *pFence, ulong FenceValue) { var @this = (ID3D12SharingContract *)Unsafe.AsPointer(ref Unsafe.AsRef(in this)); ((delegate * unmanaged[Cdecl] < ID3D12SharingContract *, ID3D12Fence *, ulong, void >)LpVtbl[4])(@this, pFence, FenceValue); }
public HRESULT CommitDirect3D12Resource(ID3D12Resource *pColorResourceToCommit, ID3D12Fence *pColorResourceFence, [NativeTypeName("UINT64")] ulong colorResourceFenceSignalValue) { return(((delegate * unmanaged <IHolographicQuadLayerUpdateParametersInterop *, ID3D12Resource *, ID3D12Fence *, ulong, int>)(lpVtbl[6]))((IHolographicQuadLayerUpdateParametersInterop *)Unsafe.AsPointer(ref this), pColorResourceToCommit, pColorResourceFence, colorResourceFenceSignalValue)); }
public int Signal([NativeTypeName("ID3D12Fence *")] ID3D12Fence *pFence, [NativeTypeName("UINT64")] ulong Value) { return(((delegate * stdcall <ID3D12CommandQueue *, ID3D12Fence *, ulong, int>)(lpVtbl[14]))((ID3D12CommandQueue *)Unsafe.AsPointer(ref this), pFence, Value)); }
public int Wait([NativeTypeName("ID3D12Fence *")] ID3D12Fence *pFence, [NativeTypeName("UINT64")] ulong Value) { return(((delegate * unmanaged <ID3D12CommandQueue *, ID3D12Fence *, ulong, int>)(lpVtbl[15]))((ID3D12CommandQueue *)Unsafe.AsPointer(ref this), pFence, Value)); }
protected override void CreateDeviceDependentResources() { _dxgiFactory = CreateDxgiFactory(); _dxgiAdapter = GetDxgiAdapter(); _d3dDevice = CreateD3DDevice(); StartInfoPump(); _commandQueue = CreateCommandQueue(); CreateDescriptorHeaps(); for (int i = 0; i < FrameCount; i++) { _commandAllocators[i] = CreateCommandAllocator(); } _fence = CreateFence(); _fenceValues = CreateFenceValues(); _fenceEvent = CreateFenceEvent(); _rootSignature = CreateRootSignature(); _pipelineState = CreatePipelineState(); _graphicsCommandLists = CreateGraphicsCommandLists(); SilkMarshal.ThrowHResult(CommandAllocator->Reset()); SilkMarshal.ThrowHResult(GraphicsCommandList->Reset(CommandAllocator, PipelineState)); CreateAssets(); ID3D12CommandAllocator *CreateCommandAllocator() { ID3D12CommandAllocator *commandAllocator; var iid = ID3D12CommandAllocator.Guid; SilkMarshal.ThrowHResult ( D3DDevice->CreateCommandAllocator (CommandListType.CommandListTypeDirect, &iid, (void **)&commandAllocator) ); return(commandAllocator); } ID3D12CommandQueue *CreateCommandQueue() { var queueDesc = new CommandQueueDesc(); ID3D12CommandQueue *commandQueue; var iid = ID3D12CommandQueue.Guid; SilkMarshal.ThrowHResult(D3DDevice->CreateCommandQueue(&queueDesc, &iid, (void **)&commandQueue)); return(commandQueue); } ID3D12Device *CreateD3DDevice() { ID3D12Device *d3dDevice; var iid = ID3D12Device.Guid; SilkMarshal.ThrowHResult ( D3D12.CreateDevice ((IUnknown *)_dxgiAdapter, D3DFeatureLevel.D3DFeatureLevel110, &iid, (void **)&d3dDevice) ); return(d3dDevice); } IDXGIFactory4 *CreateDxgiFactory() { var dxgiFactoryFlags = TryEnableDebugLayer() ? 0x01 : 0u; IDXGIFactory4 *dxgiFactory; var iid = IDXGIFactory4.Guid; SilkMarshal.ThrowHResult(Dxgi.CreateDXGIFactory2(dxgiFactoryFlags, &iid, (void **)&dxgiFactory)); return(dxgiFactory); } ID3D12Fence *CreateFence() { ID3D12Fence *fence; var iid = ID3D12Fence.Guid; SilkMarshal.ThrowHResult (D3DDevice->CreateFence(InitialValue: 0, FenceFlags.FenceFlagNone, &iid, (void **)&fence)); return(fence); } IntPtr CreateFenceEvent() { var fenceEvent = SilkMarshal.CreateWindowsEvent(null, false, false, null); if (fenceEvent == IntPtr.Zero) { var hr = Marshal.GetHRForLastWin32Error(); Marshal.ThrowExceptionForHR(hr); } return(fenceEvent); } ulong[] CreateFenceValues() { var fenceValues = new ulong[FrameCount]; fenceValues[0] = 1; return(fenceValues); } ID3D12GraphicsCommandList *[] CreateGraphicsCommandLists() { var graphicsCommandLists = new ID3D12GraphicsCommandList *[FrameCount]; for (uint i = 0u; i < FrameCount; i++) { ID3D12GraphicsCommandList *graphicsCommandList; var iid = ID3D12GraphicsCommandList.Guid; SilkMarshal.ThrowHResult ( D3DDevice->CreateCommandList ( nodeMask: 0, CommandListType.CommandListTypeDirect, _commandAllocators[i], PipelineState, &iid, (void **)&graphicsCommandList ) ); SilkMarshal.ThrowHResult(graphicsCommandList->Close()); graphicsCommandLists[i] = graphicsCommandList; } return(graphicsCommandLists); } IDXGIAdapter1 *GetDxgiAdapter() { if (UseWarpDevice) { IDXGIAdapter1 *adapter; var iid = IDXGIAdapter.Guid; SilkMarshal.ThrowHResult(_dxgiFactory->EnumWarpAdapter(&iid, (void **)&adapter)); return(adapter); } else { return(GetHardwareAdapter((IDXGIFactory1 *)_dxgiFactory)); } } bool TryEnableDebugLayer() { #if DEBUG // Enable the debug layer (requires the Graphics Tools "optional feature"). // NOTE: Enabling the debug layer after device creation will invalidate the active device. using ComPtr <ID3D12Debug> debugController = null; var iid = ID3D12Debug.Guid; var hr = D3D12.GetDebugInterface(&iid, (void **)&debugController); if (HResult.IndicatesSuccess(hr)) { debugController.Get().EnableDebugLayer(); Log.LogInformation("Debug layer enabled"); return(_debug = true); } else { Log.LogWarning ( Marshal.GetExceptionForHR(hr), $"Failed to enable debug layer, failed with result {hr} (0x{hr:x8})" ); } #endif return(false); } void StartInfoPump() { #if DEBUG if (!_debug) { Log.LogInformation("Skipped creation of info pump due to the debug layer not being enabled."); return; } var iid = ID3D12InfoQueue.Guid; fixed(ID3D12InfoQueue ** @out = &_infoQueue) { SilkMarshal.ThrowHResult(D3DDevice->QueryInterface(&iid, (void **)@out)); } _infoPumpCancellationToken = new(); _infoPump = Task.Run ( () => { Log.LogInformation("Info queue pump started"); while (!_infoPumpCancellationToken.Token.IsCancellationRequested) { var numMessages = _infoQueue->GetNumStoredMessages(); if (numMessages == 0) { continue; } for (var i = 0ul; i < numMessages; i++) { nuint msgByteLength; SilkMarshal.ThrowHResult(_infoQueue->GetMessageA(i, null, &msgByteLength)); using var memory = GlobalMemory.Allocate((int)msgByteLength); SilkMarshal.ThrowHResult ( _infoQueue->GetMessageA(i, memory.AsPtr <Message>(), &msgByteLength) ); ref var msg = ref memory.AsRef <Message>(); var descBytes = new Span <byte>(msg.PDescription, (int)msg.DescriptionByteLength); var desc = Encoding.UTF8.GetString(descBytes[..^ 1]); var eid = new EventId((int)msg.ID, msg.ID.ToString()["MessageID".Length..]);