/// <summary> /// Creates a new <see cref="GraphicsDevice"/> instance for the input <see cref="ID3D12Device"/>. /// </summary> /// <param name="d3D12Device">The <see cref="ID3D12Device"/> to use for the new <see cref="GraphicsDevice"/> instance.</param> /// <param name="dxgiAdapter">The <see cref="IDXGIAdapter"/> that <paramref name="d3D12Device"/> was created from.</param> /// <param name="dxgiDescription1">The available info for the new <see cref="GraphicsDevice"/> instance.</param> internal GraphicsDevice(ID3D12Device *d3D12Device, IDXGIAdapter *dxgiAdapter, DXGI_ADAPTER_DESC1 *dxgiDescription1) { this.d3D12Device = new ComPtr <ID3D12Device>(d3D12Device); this.d3D12ComputeCommandQueue = d3D12Device->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE_COMPUTE); this.d3D12CopyCommandQueue = d3D12Device->CreateCommandQueue(D3D12_COMMAND_LIST_TYPE_COPY); this.d3D12ComputeFence = d3D12Device->CreateFence(); this.d3D12CopyFence = d3D12Device->CreateFence(); this.shaderResourceViewDescriptorAllocator = new ID3D12DescriptorHandleAllocator(d3D12Device); this.computeCommandListPool = new ID3D12CommandListPool(D3D12_COMMAND_LIST_TYPE_COMPUTE); this.copyCommandListPool = new ID3D12CommandListPool(D3D12_COMMAND_LIST_TYPE_COPY); Luid = Luid.FromLUID(dxgiDescription1->AdapterLuid); Name = new string((char *)dxgiDescription1->Description); DedicatedMemorySize = dxgiDescription1->DedicatedVideoMemory; SharedMemorySize = dxgiDescription1->SharedSystemMemory; IsHardwareAccelerated = (dxgiDescription1->Flags & (uint)DXGI_ADAPTER_FLAG_SOFTWARE) == 0; var d3D12Options1Data = d3D12Device->CheckFeatureSupport <D3D12_FEATURE_DATA_D3D12_OPTIONS1>(D3D12_FEATURE_D3D12_OPTIONS1); ComputeUnits = d3D12Options1Data.TotalLaneCount; WavefrontSize = d3D12Options1Data.WaveLaneCountMin; var d3D12Architecture1Data = d3D12Device->CheckFeatureSupport <D3D12_FEATURE_DATA_ARCHITECTURE1>(D3D12_FEATURE_ARCHITECTURE1); IsCacheCoherentUMA = d3D12Architecture1Data.CacheCoherentUMA != 0; #if NET6_0_OR_GREATER this.allocator = d3D12Device->CreateAllocator(dxgiAdapter); if (IsCacheCoherentUMA) { this.pool = this.allocator.Get()->CreatePoolForCacheCoherentUMA(); } #endif }
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 ShaderDispatchMetadataLoader(ID3D12Device *d3D12Device) { this.d3D12Device = d3D12Device; }
public int CreateDirect3D12HardwareProtectedBackBufferResource(ID3D12Device *pDevice, D3D12_RESOURCE_DESC *pTexture2DDesc, ID3D12ProtectedResourceSession *pProtectedResourceSession, ID3D12Resource **ppCreatedTexture2DResource) { return(((delegate * unmanaged <IHolographicCameraInterop *, ID3D12Device *, D3D12_RESOURCE_DESC *, ID3D12ProtectedResourceSession *, ID3D12Resource **, int>)(lpVtbl[7]))((IHolographicCameraInterop *)Unsafe.AsPointer(ref this), pDevice, pTexture2DDesc, pProtectedResourceSession, ppCreatedTexture2DResource)); }
public void SetThreadDeviceCreationParams([NativeTypeName("ID3D12Device *")] ID3D12Device *pDevice, [NativeTypeName("ID3D12CommandQueue *")] ID3D12CommandQueue *pGraphicsQueue) { ((delegate * unmanaged <ID3D11On12On7 *, ID3D12Device *, ID3D12CommandQueue *, void>)(lpVtbl[3]))((ID3D11On12On7 *)Unsafe.AsPointer(ref this), pDevice, pGraphicsQueue); }
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..]);
public static extern int DMLCreateDevice([NativeTypeName("ID3D12Device *")] ID3D12Device *d3d12Device, DML_CREATE_DEVICE_FLAGS flags, [NativeTypeName("const IID &")] Guid *riid, [NativeTypeName("void **")] void **ppv);
public HRESULT CreateDirect3D12ContentBufferResource(ID3D12Device *pDevice, D3D12_RESOURCE_DESC *pTexture2DDesc, ID3D12Resource **ppTexture2DResource) { return(((delegate * unmanaged <IHolographicQuadLayerInterop *, ID3D12Device *, D3D12_RESOURCE_DESC *, ID3D12Resource **, int>)(lpVtbl[6]))((IHolographicQuadLayerInterop *)Unsafe.AsPointer(ref this), pDevice, pTexture2DDesc, ppTexture2DResource)); }
public abstract void Init(GraphicalConfiguration config, ID3D12Device *device);
private async void CreateDeviceDependentResources() { unsafe { ID3D12Device *d3dDevice = _deviceResources.D3DDevice; { D3D12_DESCRIPTOR_RANGE range = CD3DX12_DESCRIPTOR_RANGE.Create(D3D12_DESCRIPTOR_RANGE_TYPE.D3D12_DESCRIPTOR_RANGE_TYPE_CBV, 1, 0); CD3DX12_ROOT_PARAMETER.InitAsDescriptorTable(out D3D12_ROOT_PARAMETER parameter, 1, &range, D3D12_SHADER_VISIBILITY.D3D12_SHADER_VISIBILITY_VERTEX); D3D12_ROOT_SIGNATURE_FLAGS rootSignatureFlags = D3D12_ROOT_SIGNATURE_FLAGS .D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT | // Only the input assembler stage needs access to the constant buffer. D3D12_ROOT_SIGNATURE_FLAGS.D3D12_ROOT_SIGNATURE_FLAG_DENY_DOMAIN_SHADER_ROOT_ACCESS | D3D12_ROOT_SIGNATURE_FLAGS.D3D12_ROOT_SIGNATURE_FLAG_DENY_GEOMETRY_SHADER_ROOT_ACCESS | D3D12_ROOT_SIGNATURE_FLAGS.D3D12_ROOT_SIGNATURE_FLAG_DENY_HULL_SHADER_ROOT_ACCESS | D3D12_ROOT_SIGNATURE_FLAGS.D3D12_ROOT_SIGNATURE_FLAG_DENY_PIXEL_SHADER_ROOT_ACCESS; CD3DX12_ROOT_SIGNATURE_DESC.Init(out D3D12_ROOT_SIGNATURE_DESC descRootSignature, 1, ¶meter, 0, null, rootSignatureFlags); var pSignature = new ComPtrField <ID3DBlob>(); var pError = new ComPtrField <ID3DBlob>(); ThrowIfFailed(D3D12.D3D12SerializeRootSignature(&descRootSignature, D3D_ROOT_SIGNATURE_VERSION.D3D_ROOT_SIGNATURE_VERSION_1, pSignature.DangerousGetAddressOf(), pError.DangerousGetAddressOf())); Guid iid = D3D12.IID_ID3D12RootSignature; ID3D12RootSignature *rootSignature; ThrowIfFailed(d3dDevice->CreateRootSignature(0, pSignature.Ptr->GetBufferPointer(), pSignature.Ptr->GetBufferSize(), &iid, (void **)&rootSignature)); _rootSignature = rootSignature; DX.NameD3D12Object(_rootSignature.Ptr, nameof(_rootSignature)); } } Task vertexShaderRead = ((Func <Task>)(async() => { const string fileName = "Content/SampleVertexShader.cso"; var size = (UIntPtr) new FileInfo(fileName).Length; byte[] shader = await File.ReadAllBytesAsync(fileName); unsafe { CopyBytesToBlob(out _vertexShader.GetPinnableReference(), size, shader); } }))(); Task pixelShaderRead = ((Func <Task>)(async() => { const string fileName = "Content/SamplePixelShader.cso"; var size = (UIntPtr) new FileInfo(fileName).Length; byte[] shader = await File.ReadAllBytesAsync(fileName); unsafe { CopyBytesToBlob(out _pixelShader.GetPinnableReference(), size, shader); } }))(); await CreatePipelineState(vertexShaderRead, pixelShaderRead).ContinueWith(CreateRendererAssets); }
/// <summary> /// Record an upload copy to the GPU to execute asynchronously /// </summary> /// <param name="device">The device to create resources on</param> /// <param name="cmdList">The command list to record to</param> /// <param name="textureDescription">The texture to be uploaded</param> /// <param name="textureBuffer">A resource buffer that will contain the uploaded texture</param> /// <param name="textureBufferUploadHeap">An intermediate buffer used to copy over the texture</param> /// <param name="resourceFlags">Flags used in creation of the <paramref name="textureBuffer"/> resource</param> public static void RecordTextureUpload( ID3D12Device *device, ID3D12GraphicsCommandList *cmdList, in DdsTextureDescription textureDescription,
public int CreateDirect3D12HardwareProtectedContentBufferResource([NativeTypeName("ID3D12Device *")] ID3D12Device *pDevice, [NativeTypeName("D3D12_RESOURCE_DESC *")] D3D12_RESOURCE_DESC *pTexture2DDesc, [NativeTypeName("ID3D12ProtectedResourceSession *")] ID3D12ProtectedResourceSession *pProtectedResourceSession, [NativeTypeName("ID3D12Resource **")] ID3D12Resource **ppCreatedTexture2DResource) { return(((delegate * unmanaged <IHolographicQuadLayerInterop *, ID3D12Device *, D3D12_RESOURCE_DESC *, ID3D12ProtectedResourceSession *, ID3D12Resource **, int>)(lpVtbl[7]))((IHolographicQuadLayerInterop *)Unsafe.AsPointer(ref this), pDevice, pTexture2DDesc, pProtectedResourceSession, ppCreatedTexture2DResource)); }
public static extern HRESULT DMLCreateDevice(ID3D12Device *d3d12Device, DML_CREATE_DEVICE_FLAGS flags, [NativeTypeName("const IID &")] Guid *riid, void **ppv);
public int CreateDirect3D12BackBufferResource([NativeTypeName("ID3D12Device *")] ID3D12Device *pDevice, [NativeTypeName("D3D12_RESOURCE_DESC *")] D3D12_RESOURCE_DESC *pTexture2DDesc, [NativeTypeName("ID3D12Resource **")] ID3D12Resource **ppCreatedTexture2DResource) { return(((delegate * unmanaged <IHolographicCameraInterop *, ID3D12Device *, D3D12_RESOURCE_DESC *, ID3D12Resource **, int>)(lpVtbl[6]))((IHolographicCameraInterop *)Unsafe.AsPointer(ref this), pDevice, pTexture2DDesc, ppCreatedTexture2DResource)); }
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); }
private unsafe void CreateAssets() { ID3D12Device *d3dDevice = _deviceResources.D3DDevice; Guid iid; { iid = D3D12.IID_ID3D12GraphicsCommandList; ID3D12GraphicsCommandList *commandList; ThrowIfFailed(d3dDevice->CreateCommandList( 0, D3D12_COMMAND_LIST_TYPE.D3D12_COMMAND_LIST_TYPE_DIRECT, _deviceResources.CommandAllocator, _pipelineState.Ptr, &iid, (void **)&commandList)); _commandList = commandList; DX.NameD3D12Object(_commandList.Ptr, nameof(_commandList)); } // Cube vertices. Each vertex has a position and a color. const uint vertexPositionColorCount = 8; VertexPositionColor *cubeVertices = stackalloc VertexPositionColor[(int)vertexPositionColorCount] { new VertexPositionColor { pos = new Vector3(-0.5f, -0.5f, -0.5f), color = new Vector3(0.0f, 0.0f, 0.0f) }, new VertexPositionColor { pos = new Vector3(-0.5f, -0.5f, 0.5f), color = new Vector3(0.0f, 0.0f, 1.0f) }, new VertexPositionColor { pos = new Vector3(-0.5f, 0.5f, -0.5f), color = new Vector3(0.0f, 1.0f, 0.0f) }, new VertexPositionColor { pos = new Vector3(-0.5f, 0.5f, 0.5f), color = new Vector3(0.0f, 1.0f, 1.0f) }, new VertexPositionColor { pos = new Vector3(0.5f, -0.5f, -0.5f), color = new Vector3(1.0f, 0.0f, 0.0f) }, new VertexPositionColor { pos = new Vector3(0.5f, -0.5f, 0.5f), color = new Vector3(1.0f, 0.0f, 1.0f) }, new VertexPositionColor { pos = new Vector3(0.5f, 0.5f, -0.5f), color = new Vector3(1.0f, 1.0f, 0.0f) }, new VertexPositionColor { pos = new Vector3(0.5f, 0.5f, 0.5f), color = new Vector3(1.0f, 1.0f, 1.0f) } }; Debug.Assert(sizeof(VertexPositionColor) == Marshal.SizeOf <VertexPositionColor>()); uint vertexBufferSize = (uint)sizeof(VertexPositionColor) * vertexPositionColorCount; using ComPtr <ID3D12Resource> vertexBufferUpload = default; D3D12_HEAP_PROPERTIES defaultHeapProperties = CD3DX12_HEAP_PROPERTIES.Create(D3D12_HEAP_TYPE.D3D12_HEAP_TYPE_DEFAULT); D3D12_RESOURCE_DESC vertexBufferDesc = CD3DX12_RESOURCE_DESC.Buffer(vertexBufferSize); { iid = D3D12.IID_ID3D12Resource; ID3D12Resource *vertexBuffer; ThrowIfFailed(d3dDevice->CreateCommittedResource( &defaultHeapProperties, D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_NONE, &vertexBufferDesc, D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COPY_DEST, null, &iid, (void **)&vertexBuffer)); _vertexBuffer = vertexBuffer; } iid = D3D12.IID_ID3D12Resource; D3D12_HEAP_PROPERTIES uploadHeapProperties = CD3DX12_HEAP_PROPERTIES.Create(D3D12_HEAP_TYPE.D3D12_HEAP_TYPE_UPLOAD); ThrowIfFailed(d3dDevice->CreateCommittedResource( &uploadHeapProperties, D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_NONE, &vertexBufferDesc, D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_GENERIC_READ, null, &iid, (void **)vertexBufferUpload.GetAddressOf())); DX.NameD3D12Object(_vertexBuffer.Ptr, nameof(_vertexBuffer)); { D3D12_SUBRESOURCE_DATA vertexData; vertexData.pData = (byte *)cubeVertices; vertexData.RowPitch = (IntPtr)vertexBufferSize; vertexData.SlicePitch = vertexData.RowPitch; Functions.UpdateSubresources( _commandList.Ptr, _vertexBuffer.Ptr, vertexBufferUpload.Ptr, 0, 0, 1, &vertexData); D3D12_RESOURCE_BARRIER vertexBufferResourceBarrier = CD3DX12_RESOURCE_BARRIER.Transition(_vertexBuffer.Ptr, D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER); _commandList.Ptr->ResourceBarrier(1, &vertexBufferResourceBarrier); } const int cubeIndicesCount = 36; ushort * cubeIndices = stackalloc ushort[cubeIndicesCount] { 0, 2, 1, // -x 1, 2, 3, 4, 5, 6, // +x 5, 7, 6, 0, 1, 5, // -y 0, 5, 4, 2, 6, 7, // +y 2, 7, 3, 0, 4, 6, // -z 0, 6, 2, 1, 3, 7, // +z 1, 7, 5, }; const uint indexBufferSize = sizeof(ushort) * cubeIndicesCount; using var indexBufferUpload = new ComPtr <ID3D12Resource>(); D3D12_RESOURCE_DESC indexBufferDesc = CD3DX12_RESOURCE_DESC.Buffer(indexBufferSize); { iid = D3D12.IID_ID3D12Resource; ID3D12Resource *indexBuffer; ThrowIfFailed(d3dDevice->CreateCommittedResource( &defaultHeapProperties, D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_NONE, &indexBufferDesc, D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COPY_DEST, null, &iid, (void **)&indexBuffer)); _indexBuffer = indexBuffer; } iid = D3D12.IID_ID3D12Resource; ThrowIfFailed(d3dDevice->CreateCommittedResource( &uploadHeapProperties, D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_NONE, &indexBufferDesc, D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_GENERIC_READ, null, &iid, (void **)indexBufferUpload.GetAddressOf())); DX.NameD3D12Object(_indexBuffer.Ptr, nameof(_indexBuffer)); { D3D12_SUBRESOURCE_DATA indexData; indexData.pData = (byte *)cubeIndices; indexData.RowPitch = (IntPtr)indexBufferSize; indexData.SlicePitch = indexData.RowPitch; Functions.UpdateSubresources( _commandList.Ptr, _indexBuffer.Ptr, indexBufferUpload.Ptr, 0, 0, 1, &indexData); D3D12_RESOURCE_BARRIER indexBufferResourceBarrier = CD3DX12_RESOURCE_BARRIER.Transition(_indexBuffer.Ptr, D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_INDEX_BUFFER); _commandList.Ptr->ResourceBarrier(1, &indexBufferResourceBarrier); } { D3D12_DESCRIPTOR_HEAP_DESC heapDesc; heapDesc.NumDescriptors = DeviceResources.FrameCount; heapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE.D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV; heapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAGS.D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE; { ID3D12DescriptorHeap *cbvHeap; iid = D3D12.IID_ID3D12DescriptorHeap; ThrowIfFailed(d3dDevice->CreateDescriptorHeap(&heapDesc, &iid, (void **)&cbvHeap)); _cbvHeap = cbvHeap; DX.NameD3D12Object(_cbvHeap.Ptr, nameof(_cbvHeap)); } } D3D12_RESOURCE_DESC constantBufferDesc = CD3DX12_RESOURCE_DESC.Buffer( DeviceResources.FrameCount * AlignedConstantBufferSize); { iid = D3D12.IID_ID3D12Resource; ID3D12Resource *constantBuffer; ThrowIfFailed(d3dDevice->CreateCommittedResource( &uploadHeapProperties, D3D12_HEAP_FLAGS.D3D12_HEAP_FLAG_NONE, &constantBufferDesc, D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_GENERIC_READ, null, &iid, (void **)&constantBuffer)); _constantBuffer = constantBuffer; DX.NameD3D12Object(_constantBuffer.Ptr, nameof(_constantBuffer)); } D3D12_GPU_VIRTUAL_ADDRESS cbvGpuAddress = _constantBuffer.Ptr->GetGPUVirtualAddress(); D3D12_CPU_DESCRIPTOR_HANDLE cbvCpuHandle = _cbvHeap.Ptr->GetCPUDescriptorHandleForHeapStart(); _cbvDescriptorSize = d3dDevice->GetDescriptorHandleIncrementSize( D3D12_DESCRIPTOR_HEAP_TYPE.D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); for (var i = 0; i < DeviceResources.FrameCount; i++) { D3D12_CONSTANT_BUFFER_VIEW_DESC desc; desc.BufferLocation = cbvGpuAddress; desc.SizeInBytes = AlignedConstantBufferSize; d3dDevice->CreateConstantBufferView(&desc, cbvCpuHandle); cbvGpuAddress += desc.SizeInBytes; cbvCpuHandle.Offset((int)_cbvDescriptorSize); } D3D12_RANGE readRange = CD3DX12_RANGE.Create((UIntPtr)0, (UIntPtr)0); fixed(byte **p = &_mappedConstantBuffer) { ThrowIfFailed(_constantBuffer.Ptr->Map(0, &readRange, (void **)p)); Unsafe.InitBlockUnaligned(_mappedConstantBuffer, 0, DeviceResources.FrameCount * AlignedConstantBufferSize); } ThrowIfFailed(_commandList.Ptr->Close()); const int ppCommandListCount = 1; ID3D12CommandList **ppCommandLists = stackalloc ID3D12CommandList *[ppCommandListCount] { (ID3D12CommandList *)_commandList.Ptr }; _deviceResources.CommandQueue->ExecuteCommandLists(ppCommandListCount, ppCommandLists); _vertexBufferView.BufferLocation = _vertexBuffer.Ptr->GetGPUVirtualAddress(); _vertexBufferView.StrideInBytes = (uint)sizeof(VertexPositionColor); _vertexBufferView.SizeInBytes = vertexBufferSize; _indexBufferView.BufferLocation = _indexBuffer.Ptr->GetGPUVirtualAddress(); _indexBufferView.SizeInBytes = indexBufferSize; _indexBufferView.Format = DXGI_FORMAT.DXGI_FORMAT_R16_UINT; _deviceResources.WaitForGpu(); } #endregion } }
public static extern int DMLCreateDevice1([NativeTypeName("ID3D12Device *")] ID3D12Device *d3d12Device, DML_CREATE_DEVICE_FLAGS flags, DML_FEATURE_LEVEL minimumFeatureLevel, [NativeTypeName("const IID &")] Guid *riid, [NativeTypeName("void **")] void **ppv);
public override void Init(GraphicalConfiguration config, ID3D12Device *device) { bool z = new RgbaColor() == new RgbaColor(); _pipelineManager = new PipelineManager(ComPtr <ID3D12Device> .CopyFromPointer(device)); _allocator = new GpuAllocator(ComPtr <ID3D12Device> .CopyFromPointer(device)); var verticesDesc = new GpuResourceDesc( GpuResourceFormat.Buffer((ulong)sizeof(Vertex) * 3), GpuMemoryType.CpuWriteOptimized, D3D12_RESOURCE_STATE_GENERIC_READ, GpuAllocFlags.ForceAllocateComitted ); //var triangleVertices = stackalloc Vertex[3] { // new Vertex // { // Position = new Vector3(0.0f, 0.25f, 0.0f), // Color = new Vector4(1.0f, 0.0f, 0.0f, 1.0f) // }, // new Vertex // { // Position = new Vector3(0.25f, -0.25f, 0.0f), // Color = new Vector4(0.0f, 1.0f, 0.0f, 1.0f) // }, // new Vertex // { // Position = new Vector3(-0.25f, -0.25f, 0.0f), // Color = new Vector4(0.0f, 0.0f, 1.0f, 1.0f) // }, //}; var cubeVertices = stackalloc Vertex[8] { new Vertex(new Vector3(-0.5f, -0.5f, -0.5f), new Vector4(0.0f, 0.0f, 0.0f, 1.0f)), new Vertex(new Vector3(-0.5f, -0.5f, 0.5f), new Vector4(0.0f, 0.0f, 1.0f, 1.0f)), new Vertex(new Vector3(-0.5f, 0.5f, -0.5f), new Vector4(0.0f, 1.0f, 0.0f, 1.0f)), new Vertex(new Vector3(-0.5f, 0.5f, 0.5f), new Vector4(0.0f, 1.0f, 1.0f, 1.0f)), new Vertex(new Vector3(0.5f, -0.5f, -0.5f), new Vector4(1.0f, 0.0f, 0.0f, 1.0f)), new Vertex(new Vector3(0.5f, -0.5f, 0.5f), new Vector4(1.0f, 0.0f, 1.0f, 1.0f)), new Vertex(new Vector3(0.5f, 0.5f, -0.5f), new Vector4(1.0f, 1.0f, 0.0f, 1.0f)), new Vertex(new Vector3(0.5f, 0.5f, 0.5f), new Vector4(1.0f, 1.0f, 1.0f, 1.0f)) }; var cubeIndices = stackalloc ushort[36] { 0, 2, 1, // -x 1, 2, 3, 4, 5, 6, // +x 5, 7, 6, 0, 1, 5, // -y 0, 5, 4, 2, 6, 7, // +y 2, 7, 3, 0, 4, 6, // -z 0, 6, 2, 1, 3, 7, // +z 1, 7, 5, }; var vertices = _allocator.AllocateVertexBuffer <Vertex>(8, GpuMemoryType.CpuWriteOptimized, GpuAllocFlags.ForceAllocateComitted); var indices = _allocator.AllocateIndexBuffer <ushort>(36, GpuMemoryType.CpuWriteOptimized, GpuAllocFlags.ForceAllocateComitted); vertices.Resource.Map(0); Unsafe.CopyBlock(vertices.Resource.CpuAddress, cubeVertices, (uint)sizeof(Vertex) * 8); vertices.Resource.Unmap(0); indices.Resource.Map(0); Unsafe.CopyBlock(indices.Resource.CpuAddress, cubeIndices, (uint)sizeof(ushort) * 36); indices.Resource.Unmap(0); _vertexBuffer = vertices; _indexBuffer = indices; _rootSig = RootSignature.Create(device, default, default);