protected override void CreateAssets()
    {
        base.CreateAssets();

        _bundleAllocator = CreateBundleAllocator();
        _bundle          = CreateBundle();

        ID3D12GraphicsCommandList *CreateBundle()
        {
            ID3D12GraphicsCommandList *bundle;

            ThrowIfFailed(D3DDevice->CreateCommandList(nodeMask: 0, D3D12_COMMAND_LIST_TYPE_BUNDLE, _bundleAllocator, PipelineState, __uuidof <ID3D12GraphicsCommandList>(), (void **)&bundle));

            bundle->SetGraphicsRootSignature(RootSignature);
            bundle->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

            fixed(D3D12_VERTEX_BUFFER_VIEW *pVertexBufferView = &VertexBufferView)
            {
                bundle->IASetVertexBuffers(StartSlot: 0, 1, pVertexBufferView);
            }

            bundle->DrawInstanced(3, 1, 0, 0);
            ThrowIfFailed(bundle->Close());

            return(bundle);
        }

        ID3D12CommandAllocator *CreateBundleAllocator()
        {
            ID3D12CommandAllocator *bundleAllocator;

            ThrowIfFailed(D3DDevice->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_BUNDLE, __uuidof <ID3D12CommandAllocator>(), (void **)&bundleAllocator));
            return(bundleAllocator);
        }
    }
Exemple #2
0
        //public static ulong UpdateSubresources(
        //    [In] ID3D12GraphicsCommandList* pCmdList,
        //    [In] ID3D12Resource* pDestinationResource,
        //    [In] ID3D12Resource* pIntermediate,
        //    ulong IntermediateOffset,
        //    [In] uint FirstSubresource,
        //    [In] uint NumSubresources,
        //    [In] D3D12_SUBRESOURCE_DATA* pSrcData)
        //{
        //    ulong RequiredSize = 0;
        //    ulong MemToAlloc = (ulong) (sizeof(D3D12_PLACED_SUBRESOURCE_FOOTPRINT) + sizeof(uint) + sizeof(ulong)) *
        //                       NumSubresources;

        //    if (MemToAlloc > (ulong)(void*)-1) // new
        //    {
        //        return 0;
        //    }

        //    void* pMem = UnsafeNativeMethods.HeapAlloc(UnsafeNativeMethods.GetProcessHeap(), 0, (IntPtr)MemToAlloc);

        //    if (pMem == null)
        //    {
        //        return 0;
        //    }

        //    D3D12_PLACED_SUBRESOURCE_FOOTPRINT* pLayouts = (D3D12_PLACED_SUBRESOURCE_FOOTPRINT*) pMem;
        //    ulong* pRowSizesInBytes = (ulong*) (pLayouts + NumSubresources);
        //    uint* pNumRows = (uint*) (pRowSizesInBytes + NumSubresources);

        //    D3D12_RESOURCE_DESC Desc;
        //    pDestinationResource->GetDesc(&Desc);
        //    ID3D12Device* pDevice;
        //    Guid iid = D3D12.IID_ID3D12Device;
        //    pDestinationResource->GetDevice(&iid, (void**) &pDevice);
        //    pDevice->GetCopyableFootprints(&Desc, FirstSubresource, NumSubresources, IntermediateOffset, pLayouts,
        //        pNumRows, pRowSizesInBytes, &RequiredSize);
        //    pDevice->Release();

        //    ulong Result = UpdateSubresources(pCmdList, pDestinationResource, pIntermediate, FirstSubresource,
        //        NumSubresources, RequiredSize, pLayouts, pNumRows, pRowSizesInBytes, pSrcData);
        //    UnsafeNativeMethods.HeapFree(UnsafeNativeMethods.GetProcessHeap(), 0, pMem);
        //    return Result;
        //}

        //public static ulong UpdateSubresources(
        //    uint MaxSubresources,
        //    [In] ID3D12GraphicsCommandList* pCmdList,
        //    [In] ID3D12Resource* pDestinationResource,
        //    [In] ID3D12Resource* pIntermediate,
        //    ulong IntermediateOffset,
        //    [In] uint FirstSubresource,
        //    [In] uint NumSubresources,
        //    [In] D3D12_SUBRESOURCE_DATA* pSrcData)
        //{
        //    ulong RequiredSize = 0;
        //    D3D12_PLACED_SUBRESOURCE_FOOTPRINT* Layouts
        //        = stackalloc D3D12_PLACED_SUBRESOURCE_FOOTPRINT[(int) MaxSubresources];
        //    uint* NumRows = stackalloc uint[(int) MaxSubresources];
        //    ulong* RowSizesInBytes = stackalloc ulong[(int) MaxSubresources];

        //    D3D12_RESOURCE_DESC Desc;
        //    pDestinationResource->GetDesc(&Desc);
        //    ID3D12Device* pDevice;
        //    Guid iid = D3D12.IID_ID3D12Device;
        //    pDestinationResource->GetDevice(&iid, (void**) &pDevice);
        //    pDevice->GetCopyableFootprints(&Desc, FirstSubresource, NumSubresources, IntermediateOffset, Layouts,
        //        NumRows, RowSizesInBytes, &RequiredSize);
        //    pDevice->Release();

        //    return UpdateSubresources(pCmdList, pDestinationResource, pIntermediate, FirstSubresource, NumSubresources,
        //        RequiredSize, Layouts, NumRows, RowSizesInBytes, pSrcData);
        //}

        //public static ulong UpdateSubresources(
        //    [In] ID3D12GraphicsCommandList* pCmdList,
        //    [In] ID3D12Resource* pDestinationResource,
        //    [In] ID3D12Resource* pIntermediate,
        //    [In] uint FirstSubresource,
        //    [In] uint NumSubresources,
        //    ulong RequiredSize,
        //    [In] D3D12_PLACED_SUBRESOURCE_FOOTPRINT* pLayouts,
        //    [In] uint* pNumRows,
        //    [In] ulong* pRowSizesInBytes,
        //    [In] D3D12_SUBRESOURCE_DATA* pSrcData)
        //{
        //    // Minor validation
        //    D3D12_RESOURCE_DESC IntermediateDesc;
        //    pIntermediate->GetDesc(&IntermediateDesc);

        //    D3D12_RESOURCE_DESC DestinationDesc;
        //    pDestinationResource->GetDesc(&DestinationDesc);

        //    if (IntermediateDesc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER ||
        //        IntermediateDesc.Width < RequiredSize + pLayouts[0].Offset ||
        //        RequiredSize > (ulong)(void*)(-1) ||
        //        (DestinationDesc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER &&
        //         (FirstSubresource != 0 || NumSubresources != 1)))
        //    {
        //        return 0;
        //    }

        //    byte* pData;
        //    HRESULT hr = pIntermediate->Map(0, null, (void**) &pData);
        //    if (FAILED(hr))
        //    {
        //        return 0;
        //    }

        //    for (uint i = 0; i < NumSubresources; ++i)
        //    {
        //        /*
        //           void *pData;
        //           SIZE_T RowPitch;
        //           SIZE_T SlicePitch;
        //         */
        //        if (pRowSizesInBytes[i] > (ulong)(void*)-1) return 0;
        //        D3D12_MEMCPY_DEST DestData = new D3D12_MEMCPY_DEST
        //        {
        //            pData = pData + pLayouts[i].Offset,
        //            RowPitch = (UIntPtr)pLayouts[i].Footprint.RowPitch,
        //            SlicePitch = (UIntPtr)(pLayouts[i].Footprint.RowPitch * pNumRows[i])
        //        };

        //        MemcpySubresource(&DestData, &pSrcData[i], (UIntPtr) pRowSizesInBytes[i], pNumRows[i],
        //            pLayouts[i].Footprint.Depth);
        //    }

        //    pIntermediate->Unmap(0, null);

        //    if (DestinationDesc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER)
        //    {
        //        D3D12_BOX SrcBox = CD3DX12_BOX.Create((int) pLayouts[0].Offset,
        //            (int) (pLayouts[0].Offset + pLayouts[0].Footprint.Width));
        //        pCmdList->CopyBufferRegion(
        //            pDestinationResource, 0, pIntermediate, pLayouts[0].Offset, pLayouts[0].Footprint.Width);
        //    }
        //    else
        //    {
        //        for (uint i = 0; i < NumSubresources; ++i)
        //        {
        //            D3D12_TEXTURE_COPY_LOCATION Dst =
        //                CD3DX12_TEXTURE_COPY_LOCATION.Create(pDestinationResource, i + FirstSubresource);
        //            D3D12_TEXTURE_COPY_LOCATION Src = CD3DX12_TEXTURE_COPY_LOCATION.Create(pIntermediate, pLayouts[i]);
        //            pCmdList->CopyTextureRegion(&Dst, 0, 0, 0, &Src, null);
        //        }
        //    }

        //    return RequiredSize;
        //}

        //public static void MemcpySubresource(
        //    [In] D3D12_MEMCPY_DEST* pDest,
        //    [In] D3D12_SUBRESOURCE_DATA* pSrc,
        //    UIntPtr RowSizeInBytes,
        //    uint NumRows,
        //    uint NumSlices)
        //{
        //    for (uint z = 0; z < NumSlices; ++z)
        //    {
        //        byte* pDestSlice = (byte*)(pDest->pData);
        //        byte* pSrcSlice = (byte*)(pSrc->pData);
        //        if (IntPtr.Size == 4)
        //        {
        //            pDestSlice += (uint)pDest->SlicePitch * z;
        //            pSrcSlice += (uint)pSrc->SlicePitch * z;
        //        }
        //        else
        //        {
        //            pDestSlice += (ulong)pDest->SlicePitch * z;
        //            pSrcSlice += (ulong)pSrc->SlicePitch * z;
        //        }
        //        for (var y = 0u; y < NumRows; ++y)
        //        {
        //            byte* pTempDest = pDestSlice;
        //            byte* pTempSrc = pSrcSlice;

        //            if (IntPtr.Size == 4)
        //            {
        //                pDestSlice += (uint)pDest->RowPitch * y;
        //                pSrcSlice += (uint)pDest->RowPitch * y;
        //            }
        //            else
        //            {
        //                pDestSlice += (ulong)pDest->RowPitch * y;
        //                pSrcSlice += (ulong)pDest->RowPitch * y;
        //            }

        //            Buffer.MemoryCopy(pTempSrc, pTempDest, (long)RowSizeInBytes, (long)RowSizeInBytes);
        //        }
        //    }
        //}

        public static ulong UpdateSubresources(ID3D12GraphicsCommandList *pCmdList, ID3D12Resource *pDestinationResource, ID3D12Resource *pIntermediate, ulong IntermediateOffset, uint FirstSubresource, uint NumSubresources, D3D12_SUBRESOURCE_DATA *pSrcData)
        {
            ulong RequiredSize = 0;
            ulong MemToAlloc   = (ulong)(sizeof(D3D12_PLACED_SUBRESOURCE_FOOTPRINT) + sizeof(uint) + sizeof(ulong)) * NumSubresources;

            if (MemToAlloc > ((IntPtr.Size == 4) ? uint.MaxValue : ulong.MaxValue))
            {
                return(0);
            }
            void *pMem = Marshal.AllocHGlobal((IntPtr)MemToAlloc).ToPointer();

            if (pMem == null)
            {
                return(0);
            }
            D3D12_PLACED_SUBRESOURCE_FOOTPRINT *pLayouts = (D3D12_PLACED_SUBRESOURCE_FOOTPRINT *)(pMem);
            ulong *pRowSizesInBytes = (ulong *)(pLayouts + NumSubresources);
            uint * pNumRows         = (uint *)(pRowSizesInBytes + NumSubresources);

            D3D12_RESOURCE_DESC Desc;

            pDestinationResource->GetDesc(&Desc);
            ID3D12Device *pDevice;
            var           iid = IID_ID3D12Device;

            pDestinationResource->GetDevice(&iid, (void **)(&pDevice));
            pDevice->GetCopyableFootprints(&Desc, FirstSubresource, NumSubresources, IntermediateOffset, pLayouts, pNumRows, pRowSizesInBytes, &RequiredSize);
            pDevice->Release();

            ulong Result = UpdateSubresources(pCmdList, pDestinationResource, pIntermediate, FirstSubresource, NumSubresources, RequiredSize, pLayouts, pNumRows, pRowSizesInBytes, pSrcData);

            Marshal.FreeHGlobal((IntPtr)pMem);
            return(Result);
        }
 /// <summary>
 /// Returns a <see cref="ID3D12GraphicsCommandList"/> and <see cref="ID3D12CommandAllocator"/> pair.
 /// </summary>
 /// <param name="d3D12CommandList">The returned <see cref="ID3D12GraphicsCommandList"/> value.</param>
 /// <param name="d3D12CommandAllocator">The returned <see cref="ID3D12CommandAllocator"/> value.</param>
 public void Return(ID3D12GraphicsCommandList *d3D12CommandList, ID3D12CommandAllocator *d3D12CommandAllocator)
 {
     lock (this.d3D12CommandListBundleQueue)
     {
         this.d3D12CommandListBundleQueue.Enqueue(new D3D12CommandListBundle(d3D12CommandList, d3D12CommandAllocator));
     }
 }
    protected override void DestroyAssets()
    {
        DestroyBundle();
        DestroyBundleAllocator();
        base.DestroyAssets();

        void DestroyBundle()
        {
            var bundle = _bundle;

            if (bundle != null)
            {
                _bundle = null;
                _       = bundle->Release();
            }
        }

        void DestroyBundleAllocator()
        {
            var bundleAllocator = _bundleAllocator;

            if (bundleAllocator != null)
            {
                _bundleAllocator = null;
                _ = bundleAllocator->Release();
            }
        }
    }
Exemple #5
0
        public static ulong UpdateSubresources([NativeTypeName("ID3D12GraphicsCommandList *")] ID3D12GraphicsCommandList *pCmdList, [NativeTypeName("ID3D12Resource *")] ID3D12Resource *pDestinationResource, [NativeTypeName("ID3D12Resource *")] ID3D12Resource *pIntermediate, [NativeTypeName("UINT64")] ulong IntermediateOffset, [NativeTypeName("UINT")] uint FirstSubresource, [NativeTypeName("UINT")] uint NumSubresources, [NativeTypeName("const void *")] void *pResourceData, [NativeTypeName("D3D12_SUBRESOURCE_INFO *")] D3D12_SUBRESOURCE_INFO *pSrcData)
        {
            ulong RequiredSize = 0;
            ulong MemToAlloc   = (ulong)(sizeof(D3D12_PLACED_SUBRESOURCE_FOOTPRINT) + sizeof(uint) + sizeof(ulong)) * NumSubresources;

            if (MemToAlloc > unchecked ((nuint)(-1)))
            {
                return(0);
            }

            var pMem = HeapAlloc(GetProcessHeap(), 0, (nuint)MemToAlloc);

            if (pMem == null)
            {
                return(0);
            }

            var    pLayouts         = (D3D12_PLACED_SUBRESOURCE_FOOTPRINT *)pMem;
            ulong *pRowSizesInBytes = (ulong *)(pLayouts + NumSubresources);
            uint * pNumRows         = (uint *)(pRowSizesInBytes + NumSubresources);

            var Desc = pDestinationResource->GetDesc();

            ID3D12Device *pDevice = null;
            var           iid     = IID_ID3D12Device;

            pDestinationResource->GetDevice(&iid, (void **)&pDevice);
            pDevice->GetCopyableFootprints(&Desc, FirstSubresource, NumSubresources, IntermediateOffset, pLayouts, pNumRows, pRowSizesInBytes, &RequiredSize);
            pDevice->Release();

            ulong Result = UpdateSubresources(pCmdList, pDestinationResource, pIntermediate, FirstSubresource, NumSubresources, RequiredSize, pLayouts, pNumRows, pRowSizesInBytes, pResourceData, pSrcData);

            HeapFree(GetProcessHeap(), 0, pMem);
            return(Result);
        }
 public static void NameD3D12Object(ID3D12GraphicsCommandList *pObject, string name)
 {
     fixed(char *pName = name)
     {
         pObject->SetName(pName);
     }
 }
Exemple #7
0
        public static void PIXSetMarker(ID3D12GraphicsCommandList *pCommandList, ulong metadata, string format)
        {
            var size = (uint)((format.Length + 1) * sizeof(char));

            fixed(char *pFormat = format)
            {
                pCommandList->SetMarker(Detail.PIX_EVENT_UNICODE_VERSION, pFormat, size);
            }
        }
Exemple #8
0
        public static void PIXBeginEvent(ID3D12GraphicsCommandList *pCommandList, ulong metadata, string format, params object[] args)
        {
            string buf   = string.Format(format, args);
            var    count = (uint)((buf.Length + 1) * sizeof(char));

            fixed(char *pBuf = buf)
            {
                pCommandList->BeginEvent(Detail.PIX_EVENT_UNICODE_VERSION, pBuf, count);
            }
        }
Exemple #9
0
    public static ulong UpdateSubresources(ID3D12GraphicsCommandList *pCmdList, ID3D12Resource *pDestinationResource, ID3D12Resource *pIntermediate, uint FirstSubresource, uint NumSubresources, [NativeTypeName("UINT64")] ulong RequiredSize, [NativeTypeName("const D3D12_PLACED_SUBRESOURCE_FOOTPRINT *")] D3D12_PLACED_SUBRESOURCE_FOOTPRINT *pLayouts, [NativeTypeName("const UINT *")] uint *pNumRows, [NativeTypeName("const UINT64 *")] ulong *pRowSizesInBytes, [NativeTypeName("const D3D12_SUBRESOURCE_DATA *")] D3D12_SUBRESOURCE_DATA *pSrcData)
    {
        D3D12_RESOURCE_DESC IntermediateDesc = pIntermediate->GetDesc();

        D3D12_RESOURCE_DESC DestinationDesc = pDestinationResource->GetDesc();

        if (unchecked (IntermediateDesc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER || IntermediateDesc.Width < RequiredSize + pLayouts[0].Offset || RequiredSize > (nuint)(-1)) || (DestinationDesc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER && (FirstSubresource != 0 || NumSubresources != 1)))
        {
            return(0);
        }

        byte *  pData;
        HRESULT hr = pIntermediate->Map(0, null, (void **)(&pData));

        if ((((HRESULT)(hr)) < 0))
        {
            return(0);
        }

        for (uint i = 0; i < NumSubresources; ++i)
        {
            if (pRowSizesInBytes[i] > unchecked ((nuint)(-1)))
            {
                return(0);
            }

            D3D12_MEMCPY_DEST DestData = new D3D12_MEMCPY_DEST
            {
                pData      = pData + pLayouts[i].Offset,
                RowPitch   = pLayouts[i].Footprint.RowPitch,
                SlicePitch = unchecked ((nuint)(pLayouts[i].Footprint.RowPitch) * (nuint)(pNumRows[i])),
            };

            MemcpySubresource(&DestData, &pSrcData[i], unchecked ((nuint)(pRowSizesInBytes[i])), pNumRows[i], pLayouts[i].Footprint.Depth);
        }

        pIntermediate->Unmap(0, null);
        if (DestinationDesc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER)
        {
            pCmdList->CopyBufferRegion(pDestinationResource, 0, pIntermediate, pLayouts[0].Offset, pLayouts[0].Footprint.Width);
        }
        else
        {
            for (uint i = 0; i < NumSubresources; ++i)
            {
                D3D12_TEXTURE_COPY_LOCATION Dst = new D3D12_TEXTURE_COPY_LOCATION(pDestinationResource, i + FirstSubresource);
                D3D12_TEXTURE_COPY_LOCATION Src = new D3D12_TEXTURE_COPY_LOCATION(pIntermediate, pLayouts[i]);

                pCmdList->CopyTextureRegion(&Dst, 0, 0, 0, &Src, null);
            }
        }

        return(RequiredSize);
    }
Exemple #10
0
        public static void EndEvent(
            ID3D12GraphicsCommandList *context,
            ulong *args,
            uint argsLength,
            PIXEventsThreadInfo *threadInfo,
            ulong time
            )
        {
            SerializeForCpuCapture(args, argsLength, threadInfo, time);

            EndGPUEventOnContext(context);
        }
Exemple #11
0
        public static void SetMarker(
            ID3D12GraphicsCommandList *context,
            ulong *args,
            uint argsLength,
            PIXEventsThreadInfo *threadInfo,
            ulong time
            )
        {
            SerializeForCpuCapture(args, argsLength, threadInfo, time);

            SetGPUMarkerOnContext(context, args, argsLength * sizeof(ulong));
        }
Exemple #12
0
        public static void SetMarkerOnCommandList(
            ID3D12GraphicsCommandList *commandList,
            Argb32 color,
            ReadOnlySpan <char> message
            )
        {
            var length = Utf8Length(message);
            var buff   = StackSentinel.SafeToStackalloc <byte>(length) ? stackalloc byte[length] : new byte[length];

            System.Text.Encoding.UTF8.GetBytes(message, buff);

            var hr = _PIXSetMarkerOnCommandList(commandList, Argb32.GetAs32BitArgb(color),
                                                (byte *)Unsafe.AsPointer(ref MemoryMarshal.GetReference(buff)));

            ThrowHelper.ThrowIfFailed(hr);
        }
Exemple #13
0
    public static ulong UpdateSubresources(uint MaxSubresources, ID3D12GraphicsCommandList *pCmdList, ID3D12Resource *pDestinationResource, ID3D12Resource *pIntermediate, [NativeTypeName("UINT64")] ulong IntermediateOffset, uint FirstSubresource, uint NumSubresources, [NativeTypeName("const void *")] void *pResourceData, [NativeTypeName("D3D12_SUBRESOURCE_INFO *")] D3D12_SUBRESOURCE_INFO *pSrcData)
    {
        ulong RequiredSize = 0;
        D3D12_PLACED_SUBRESOURCE_FOOTPRINT *Layouts = stackalloc D3D12_PLACED_SUBRESOURCE_FOOTPRINT[(int)MaxSubresources];
        uint * NumRows         = stackalloc uint[(int)MaxSubresources];
        ulong *RowSizesInBytes = stackalloc ulong[(int)MaxSubresources];

        var Desc = pDestinationResource->GetDesc();

        ID3D12Device *pDevice = null;

        _ = pDestinationResource->GetDevice(__uuidof <ID3D12Device>(), (void **)&pDevice);

        pDevice->GetCopyableFootprints(&Desc, FirstSubresource, NumSubresources, IntermediateOffset, Layouts, NumRows, RowSizesInBytes, &RequiredSize);
        _ = pDevice->Release();

        return(UpdateSubresources(pCmdList, pDestinationResource, pIntermediate, FirstSubresource, NumSubresources, RequiredSize, Layouts, NumRows, RowSizesInBytes, pResourceData, pSrcData));
    }
        private ID3D12GraphicsCommandList *[] CreateGraphicsCommandLists()
        {
            var graphicsCommandLists = new ID3D12GraphicsCommandList *[_graphicsSurface.BufferCount];
            var iid = IID_ID3D12GraphicsCommandList;

            for (var i = 0; i < graphicsCommandLists.Length; i++)
            {
                ID3D12GraphicsCommandList *graphicsCommandList;
                ThrowExternalExceptionIfFailed(nameof(ID3D12Device.CreateCommandList), Device->CreateCommandList(NodeMask: 0, D3D12_COMMAND_LIST_TYPE_DIRECT, CommandAllocators[i], pInitialState: null, &iid, (void **)&graphicsCommandList));

                // Command lists are created in the recording state, but there is nothing
                // to record yet. The main loop expects it to be closed, so close it now.
                ThrowExternalExceptionIfFailed(nameof(ID3D12GraphicsCommandList.Close), graphicsCommandList->Close());

                graphicsCommandLists[i] = graphicsCommandList;
            }

            return(graphicsCommandLists);
        }
    public static ID3D12GraphicsCommandList *GetLatestD3D12GraphicsCommandList(ID3D12GraphicsCommandList *d3d12GraphicsCommandList, out uint d3d12GraphicsCommandListVersion)
    {
        ID3D12GraphicsCommandList *result;

        if (d3d12GraphicsCommandList->QueryInterface(__uuidof <ID3D12GraphicsCommandList6>(), (void **)&result).SUCCEEDED)
        {
            d3d12GraphicsCommandListVersion = 6;
            _ = d3d12GraphicsCommandList->Release();
        }
        else if (d3d12GraphicsCommandList->QueryInterface(__uuidof <ID3D12GraphicsCommandList5>(), (void **)&result).SUCCEEDED)
        {
            d3d12GraphicsCommandListVersion = 5;
            _ = d3d12GraphicsCommandList->Release();
        }
        else if (d3d12GraphicsCommandList->QueryInterface(__uuidof <ID3D12GraphicsCommandList4>(), (void **)&result).SUCCEEDED)
        {
            d3d12GraphicsCommandListVersion = 4;
            _ = d3d12GraphicsCommandList->Release();
        }
        else if (d3d12GraphicsCommandList->QueryInterface(__uuidof <ID3D12GraphicsCommandList3>(), (void **)&result).SUCCEEDED)
        {
            d3d12GraphicsCommandListVersion = 3;
            _ = d3d12GraphicsCommandList->Release();
        }
        else if (d3d12GraphicsCommandList->QueryInterface(__uuidof <ID3D12GraphicsCommandList2>(), (void **)&result).SUCCEEDED)
        {
            d3d12GraphicsCommandListVersion = 2;
            _ = d3d12GraphicsCommandList->Release();
        }
        else if (d3d12GraphicsCommandList->QueryInterface(__uuidof <ID3D12GraphicsCommandList1>(), (void **)&result).SUCCEEDED)
        {
            d3d12GraphicsCommandListVersion = 1;
            _ = d3d12GraphicsCommandList->Release();
        }
        else
        {
            d3d12GraphicsCommandListVersion = 0;
            result = d3d12GraphicsCommandList;
        }

        return(result);
    }
Exemple #16
0
        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);
            }
        }
    private void CreateCommandListAndAllocator(ID3D12Device *d3D12Device, ID3D12PipelineState *d3D12PipelineState, out ID3D12GraphicsCommandList *d3D12CommandList, out ID3D12CommandAllocator *d3D12CommandAllocator)
    {
        using ComPtr <ID3D12CommandAllocator> d3D12CommandAllocatorComPtr = d3D12Device->CreateCommandAllocator(this.d3D12CommandListType);
        using ComPtr <ID3D12GraphicsCommandList> d3D12CommandListComPtr   = d3D12Device->CreateCommandList(this.d3D12CommandListType, d3D12CommandAllocatorComPtr.Get(), d3D12PipelineState);

        fixed(ID3D12GraphicsCommandList **d3D12CommandListPtr = &d3D12CommandList)
        fixed(ID3D12CommandAllocator **d3D12CommandAllocatorPtr = &d3D12CommandAllocator)
        {
            d3D12CommandListComPtr.CopyTo(d3D12CommandListPtr);
            d3D12CommandAllocatorComPtr.CopyTo(d3D12CommandAllocatorPtr);
        }
    }
 /// <summary>
 /// Creates a new <see cref="D3D12CommandListBundle"/> instance with the given parameters.
 /// </summary>
 /// <param name="d3D12CommandList">The <see cref="ID3D12GraphicsCommandList"/> value to wrap.</param>
 /// <param name="d3D12CommandAllocator">The <see cref="ID3D12CommandAllocator"/> value to wrap.</param>
 public D3D12CommandListBundle(ID3D12GraphicsCommandList *d3D12CommandList, ID3D12CommandAllocator *d3D12CommandAllocator)
 {
     D3D12CommandList      = d3D12CommandList;
     D3D12CommandAllocator = d3D12CommandAllocator;
 }
    /// <summary>
    /// Rents a <see cref="ID3D12GraphicsCommandList"/> and <see cref="ID3D12CommandAllocator"/> pair.
    /// </summary>
    /// <param name="d3D12Device">The <see cref="ID3D12Device"/> renting the command list.</param>
    /// <param name="d3D12PipelineState">The <see cref="ID3D12PipelineState"/> instance to use for the new command list.</param>
    /// <param name="d3D12CommandList">The resulting <see cref="ID3D12GraphicsCommandList"/> value.</param>
    /// <param name="d3D12CommandAllocator">The resulting <see cref="ID3D12CommandAllocator"/> value.</param>
    public void Rent(ID3D12Device *d3D12Device, ID3D12PipelineState *d3D12PipelineState, out ID3D12GraphicsCommandList *d3D12CommandList, out ID3D12CommandAllocator *d3D12CommandAllocator)
    {
        lock (this.d3D12CommandListBundleQueue)
        {
            if (this.d3D12CommandListBundleQueue.TryDequeue(out D3D12CommandListBundle d3D12CommandListBundle))
            {
                d3D12CommandList      = d3D12CommandListBundle.D3D12CommandList;
                d3D12CommandAllocator = d3D12CommandListBundle.D3D12CommandAllocator;
            }
            else
            {
                d3D12CommandAllocator = null;
                d3D12CommandList      = null;
            }
        }

        // Reset the command allocator and command list outside of the lock, or create a new pair if one to be reused
        // wasn't available. These operations are relatively expensive, so doing so here reduces thread contention
        // when multiple shader executions are being dispatched in parallel on the same device.
        if (d3D12CommandAllocator is not null)
        {
            d3D12CommandAllocator->Reset().Assert();
            d3D12CommandList->Reset(d3D12CommandAllocator, d3D12PipelineState).Assert();
        }
        else
        {
            CreateCommandListAndAllocator(d3D12Device, d3D12PipelineState, out d3D12CommandList, out d3D12CommandAllocator);
        }
    }
Exemple #20
0
        public static ulong UpdateSubresources(ID3D12GraphicsCommandList *pCmdList, ID3D12Resource *pDestinationResource, ID3D12Resource *pIntermediate, uint FirstSubresource, uint NumSubresources, ulong RequiredSize, D3D12_PLACED_SUBRESOURCE_FOOTPRINT *pLayouts, uint *pNumRows, ulong *pRowSizesInBytes, D3D12_SUBRESOURCE_DATA *pSrcData)
        {
            // Minor validation
            D3D12_RESOURCE_DESC IntermediateDesc;

            pIntermediate->GetDesc(&IntermediateDesc);
            D3D12_RESOURCE_DESC DestinationDesc;

            pDestinationResource->GetDesc(&DestinationDesc);
            if (IntermediateDesc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER ||
                IntermediateDesc.Width < RequiredSize + pLayouts[0].Offset ||
                RequiredSize > ((IntPtr.Size == 4) ? uint.MaxValue : ulong.MaxValue) ||
                (DestinationDesc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER &&
                 (FirstSubresource != 0 || NumSubresources != 1)))
            {
                return(0);
            }

            byte *pData;
            int   hr = pIntermediate->Map(0, null, (void **)(&pData));

            if (FAILED(hr))
            {
                return(0);
            }

            for (var i = 0u; i < NumSubresources; ++i)
            {
                if (pRowSizesInBytes[i] > ((IntPtr.Size == 4) ? uint.MaxValue : ulong.MaxValue))
                {
                    return(0);
                }
                var DestData = new D3D12_MEMCPY_DEST
                {
                    pData      = pData + pLayouts[i].Offset,
                    RowPitch   = (UIntPtr)(pLayouts[i].Footprint.RowPitch),
                    SlicePitch = (UIntPtr)(pLayouts[i].Footprint.RowPitch * pNumRows[i])
                };
                MemcpySubresource(&DestData, &pSrcData[i], (UIntPtr)pRowSizesInBytes[i], pNumRows[i], pLayouts[i].Footprint.Depth);
            }
            pIntermediate->Unmap(0, null);

            if (DestinationDesc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER)
            {
                var SrcBox = new D3D12_BOX
                {
                    left   = (uint)pLayouts[0].Offset,
                    top    = 0,
                    front  = 0,
                    right  = (uint)pLayouts[0].Offset + pLayouts[0].Footprint.Width,
                    bottom = 1,
                    back   = 1
                };
                pCmdList->CopyBufferRegion(
                    pDestinationResource, 0, pIntermediate, pLayouts[0].Offset, pLayouts[0].Footprint.Width);
            }
            else
            {
                for (var i = 0u; i < NumSubresources; ++i)
                {
                    var Dst = new D3D12_TEXTURE_COPY_LOCATION
                    {
                        pResource = pDestinationResource,
                        Type      = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX,
                        Anonymous = new D3D12_TEXTURE_COPY_LOCATION._Anonymous_e__Union
                        {
                            SubresourceIndex = i + FirstSubresource
                        }
                    };
                    var Src = new D3D12_TEXTURE_COPY_LOCATION
                    {
                        pResource = pIntermediate,
                        Type      = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT,
                        Anonymous = new D3D12_TEXTURE_COPY_LOCATION._Anonymous_e__Union
                        {
                            PlacedFootprint = pLayouts[i]
                        }
                    };
                    pCmdList->CopyTextureRegion(&Dst, 0, 0, 0, &Src, null);
                }
            }
            return(RequiredSize);
        }
Exemple #21
0
 public static void PIXEndEvent(ID3D12GraphicsCommandList *pCommandList)
 {
     pCommandList->EndEvent();
 }
Exemple #22
0
 internal PixScopedEvent(ID3D12GraphicsCommandList *context)
 {
     _context = context;
     _type    = ContextType.List;
 }
Exemple #23
0
 private static extern HRESULT _PIXEndEventOnCommandList(ID3D12GraphicsCommandList *commandList);
Exemple #24
0
 public void ExecuteBundle([NativeTypeName("ID3D12GraphicsCommandList *")] ID3D12GraphicsCommandList *pCommandList)
 {
     ((delegate * stdcall <ID3D12GraphicsCommandList4 *, ID3D12GraphicsCommandList *, void>)(lpVtbl[27]))((ID3D12GraphicsCommandList4 *)Unsafe.AsPointer(ref this), pCommandList);
 }
        public AutoBreadcrumbNode
        (
            byte *pCommandListDebugNameA            = null,
            char *pCommandListDebugNameW            = null,
            byte *pCommandQueueDebugNameA           = null,
            char *pCommandQueueDebugNameW           = null,
            ID3D12GraphicsCommandList *pCommandList = null,
            ID3D12CommandQueue *pCommandQueue       = null,
            uint?breadcrumbCount              = null,
            uint *pLastBreadcrumbValue        = null,
            AutoBreadcrumbOp *pCommandHistory = null,
            AutoBreadcrumbNode *pNext         = null
        ) : this()
        {
            if (pCommandListDebugNameA is not null)
            {
                PCommandListDebugNameA = pCommandListDebugNameA;
            }

            if (pCommandListDebugNameW is not null)
            {
                PCommandListDebugNameW = pCommandListDebugNameW;
            }

            if (pCommandQueueDebugNameA is not null)
            {
                PCommandQueueDebugNameA = pCommandQueueDebugNameA;
            }

            if (pCommandQueueDebugNameW is not null)
            {
                PCommandQueueDebugNameW = pCommandQueueDebugNameW;
            }

            if (pCommandList is not null)
            {
                PCommandList = pCommandList;
            }

            if (pCommandQueue is not null)
            {
                PCommandQueue = pCommandQueue;
            }

            if (breadcrumbCount is not null)
            {
                BreadcrumbCount = breadcrumbCount.Value;
            }

            if (pLastBreadcrumbValue is not null)
            {
                PLastBreadcrumbValue = pLastBreadcrumbValue;
            }

            if (pCommandHistory is not null)
            {
                PCommandHistory = pCommandHistory;
            }

            if (pNext is not null)
            {
                PNext = pNext;
            }
        }
Exemple #26
0
        public static void EndEventOnCommandList(ID3D12GraphicsCommandList *commandList)
        {
            var hr = _PIXEndEventOnCommandList(commandList);

            ThrowHelper.ThrowIfFailed(hr);
        }
Exemple #27
0
        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);
        }
Exemple #28
0
 private static extern HRESULT _PIXBeginEventOnCommandList(
     ID3D12GraphicsCommandList *commandList,
     ulong color,
     byte *formatString
     );
Exemple #29
0
 public void ExecuteBundle(ID3D12GraphicsCommandList *pCommandList)
 {
     ((delegate * unmanaged <ID3D12GraphicsCommandList2 *, ID3D12GraphicsCommandList *, void>)(lpVtbl[27]))((ID3D12GraphicsCommandList2 *)Unsafe.AsPointer(ref this), pCommandList);
 }
Exemple #30
0
 private static extern HRESULT _PIXSetMarkerOnCommandList(
     ID3D12GraphicsCommandList *commandList,
     ulong color,
     byte *formatString
     );