/// <summary>
    /// Creates a new <see cref="ID3D12DescriptorHandleAllocator"/> instance with the specified parameters.
    /// </summary>
    /// <param name="device">The <see cref="ID3D12Device"/> instance to use.</param>
    public ID3D12DescriptorHandleAllocator(ID3D12Device *device)
    {
        this.d3D12DescriptorHeap = device->CreateDescriptorHeap(DescriptorsPerHeap, isShaderVisible: true);
        this.d3D12DescriptorHeapNonShaderVisible = device->CreateDescriptorHeap(DescriptorsPerHeap, isShaderVisible: false);
        this.d3D12DescriptorHandlePairs          = GC.AllocateUninitializedArray <ID3D12ResourceDescriptorHandles>((int)DescriptorsPerHeap);
        this.head = 0;
        this.tail = 0;
        this.size = (int)DescriptorsPerHeap;

        D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleStart = this.d3D12DescriptorHeap.Get()->GetCPUDescriptorHandleForHeapStart();
        D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandleStart = this.d3D12DescriptorHeap.Get()->GetGPUDescriptorHandleForHeapStart();
        D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisibleStart = this.d3D12DescriptorHeapNonShaderVisible.Get()->GetCPUDescriptorHandleForHeapStart();
        uint descriptorIncrementSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);

        for (int i = 0; i < this.d3D12DescriptorHandlePairs.Length; i++)
        {
            D3D12_CPU_DESCRIPTOR_HANDLE.InitOffsetted(out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle, in d3D12CpuDescriptorHandleStart, i, descriptorIncrementSize);
            D3D12_GPU_DESCRIPTOR_HANDLE.InitOffsetted(out D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle, in d3D12GpuDescriptorHandleStart, i, descriptorIncrementSize);
            D3D12_CPU_DESCRIPTOR_HANDLE.InitOffsetted(out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible, in d3D12CpuDescriptorHandleNonShaderVisibleStart, i, descriptorIncrementSize);

            this.d3D12DescriptorHandlePairs[i] = new ID3D12ResourceDescriptorHandles(
                d3D12CpuDescriptorHandle,
                d3D12GpuDescriptorHandle,
                d3D12CpuDescriptorHandleNonShaderVisible);
        }
    }
Beispiel #2
0
 internal DescriptorSpan(D3D12_CPU_DESCRIPTOR_HANDLE cpu, D3D12_GPU_DESCRIPTOR_HANDLE gpu, uint length, ushort incrementSize, byte type)
 {
     _length       = length;
     Cpu           = cpu;
     Gpu           = gpu;
     IncrementSize = incrementSize;
     Type          = type;
 }
        /// <summary>
        /// Rents a new CPU and GPU handle pair to use in a memory buffer.
        /// </summary>
        /// <param name="d3D12CpuDescriptorHandle">The resulting <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> value.</param>
        /// <param name="d3D12GpuDescriptorHandle">The resulting <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> value.</param>
        public void Rent(
            out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle,
            out D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle)
        {
            lock (this.d3D12DescriptorHandlePairs)
            {
                Guard.IsGreaterThan(this.size, 0, nameof(size));

                ref readonly D3D12DescriptorHandlePair d3D12DescriptorHandlePair = ref this.d3D12DescriptorHandlePairs[this.head++];
Beispiel #4
0
 /// <summary>
 /// Creates a new <see cref="ID3D12ResourceDescriptorHandles"/> instance with the given parameters.
 /// </summary>
 /// <param name="d3D12CpuDescriptorHandle">The <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> value to wrap.</param>
 /// <param name="d3D12GpuDescriptorHandle">The <see cref="D3D12_GPU_DESCRIPTOR_HANDLE"/> value to wrap.</param>
 /// <param name="d3D12CpuDescriptorHandleNonShaderVisible">The non shader visible <see cref="D3D12_CPU_DESCRIPTOR_HANDLE"/> value to wrap.</param>
 public ID3D12ResourceDescriptorHandles(
     D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle,
     D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle,
     D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandleNonShaderVisible)
 {
     D3D12CpuDescriptorHandle = d3D12CpuDescriptorHandle;
     D3D12GpuDescriptorHandle = d3D12GpuDescriptorHandle;
     D3D12CpuDescriptorHandleNonShaderVisible = d3D12CpuDescriptorHandleNonShaderVisible;
 }
        public static unsafe ulong ValidateAndGetGpuDescriptorHandle <TPixel>(IReadOnlyTexture2D <TPixel> texture, GraphicsDevice device)
            where TPixel : unmanaged
        {
            if (texture is IGraphicsResource resource)
            {
                D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle = resource.ValidateAndGetGpuDescriptorHandle(device);

                return(*(ulong *)&d3D12GpuDescriptorHandle);
            }

            return(ThrowHelper.ThrowArgumentException <ulong>("The input texture is not a valid instance"));
        }
        public virtual void SetGraphicsRootDescriptorTable(
            uint RootParameterIndex,
            D3D12_GPU_DESCRIPTOR_HANDLE BaseDescriptor
            )
        {
            var fp = GetFunctionPointer(32);

            if (m_SetGraphicsRootDescriptorTableFunc == null)
            {
                m_SetGraphicsRootDescriptorTableFunc = (SetGraphicsRootDescriptorTableFunc)Marshal.GetDelegateForFunctionPointer(fp, typeof(SetGraphicsRootDescriptorTableFunc));
            }

            m_SetGraphicsRootDescriptorTableFunc(m_ptr, RootParameterIndex, BaseDescriptor);
        }
        public virtual void ClearUnorderedAccessViewFloat(
            D3D12_GPU_DESCRIPTOR_HANDLE ViewGPUHandleInCurrentHeap,
            D3D12_CPU_DESCRIPTOR_HANDLE ViewCPUHandle,
            ID3D12Resource pResource,
            ref float Values,
            uint NumRects,
            ref tagRECT pRects
            )
        {
            var fp = GetFunctionPointer(50);

            if (m_ClearUnorderedAccessViewFloatFunc == null)
            {
                m_ClearUnorderedAccessViewFloatFunc = (ClearUnorderedAccessViewFloatFunc)Marshal.GetDelegateForFunctionPointer(fp, typeof(ClearUnorderedAccessViewFloatFunc));
            }

            m_ClearUnorderedAccessViewFloatFunc(m_ptr, ViewGPUHandleInCurrentHeap, ViewCPUHandle, pResource != null ? pResource.Ptr : IntPtr.Zero, ref Values, NumRects, ref pRects);
        }
        /// <summary>
        /// Creates a new <see cref="ID3D12DescriptorHandleAllocator"/> instance with the specified parameters.
        /// </summary>
        /// <param name="device">The <see cref="ID3D12Device"/> instance to use.</param>
        public ID3D12DescriptorHandleAllocator(ID3D12Device *device)
        {
            this.d3D12DescriptorHeap        = device->CreateDescriptorHeap(DescriptorsPerHeap);
            this.d3D12DescriptorHandlePairs = GC.AllocateUninitializedArray <D3D12DescriptorHandlePair>((int)DescriptorsPerHeap);
            this.head = 0;
            this.tail = 0;
            this.size = (int)DescriptorsPerHeap;

            D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle      = this.d3D12DescriptorHeap.Get()->GetCPUDescriptorHandleForHeapStart();
            D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandleStart = this.d3D12DescriptorHeap.Get()->GetGPUDescriptorHandleForHeapStart();
            uint descriptorIncrementSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);

            for (int i = 0; i < this.d3D12DescriptorHandlePairs.Length; i++)
            {
                this.d3D12DescriptorHandlePairs[i] = new D3D12DescriptorHandlePair(
                    in d3D12CpuDescriptorHandle,
                    in d3D12GpuDescriptorHandleStart,
                    i,
                    descriptorIncrementSize);
            }
        }
Beispiel #9
0
 /// <summary>
 /// Creates a new instance of <see cref="GpuHandle"/>
 /// </summary>
 /// <param name="value">The underlying handle value</param>
 public GpuHandle(D3D12_GPU_DESCRIPTOR_HANDLE value)
 {
     Value = value;
 }
Beispiel #10
0
 /// <inheritdoc cref="ID3D12DescriptorHandleAllocator.Return"/>
 internal void ReturnShaderResourceViewDescriptorHandles(
     D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle,
     D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle)
 {
     this.shaderResourceViewDescriptorAllocator.Return(d3D12CpuDescriptorHandle, d3D12GpuDescriptorHandle);
 }
Beispiel #11
0
 /// <inheritdoc cref="ID3D12DescriptorHandleAllocator.Rent"/>
 internal void RentShaderResourceViewDescriptorHandles(
     out D3D12_CPU_DESCRIPTOR_HANDLE d3D12CpuDescriptorHandle,
     out D3D12_GPU_DESCRIPTOR_HANDLE d3D12GpuDescriptorHandle)
 {
     this.shaderResourceViewDescriptorAllocator.Rent(out d3D12CpuDescriptorHandle, out d3D12GpuDescriptorHandle);
 }
Beispiel #12
0
 /// <summary>
 /// Creates a new <see cref="DescriptorHandle"/>
 /// </summary>
 internal DescriptorHandle(D3D12_CPU_DESCRIPTOR_HANDLE cpuHandle, D3D12_GPU_DESCRIPTOR_HANDLE gpuHandle)
 {
     CpuHandle = cpuHandle;
     GpuHandle = gpuHandle;
 }
Beispiel #13
0
        public unsafe bool Render()
        {
            if (!_loadingComplete)
            {
                return(false);
            }

            ThrowIfFailed(_deviceResources.CommandAllocator->Reset());

            ThrowIfFailed(_commandList.Ptr->Reset(_deviceResources.CommandAllocator, _pipelineState.Ptr));

            {
                _commandList.Ptr->SetGraphicsRootSignature(_rootSignature.Ptr);
                const uint             ppHeapsCount = 1;
                ID3D12DescriptorHeap **ppHeaps      = stackalloc ID3D12DescriptorHeap *[(int)ppHeapsCount] {
                    _cbvHeap.Ptr
                };

                _commandList.Ptr->SetDescriptorHeaps(ppHeapsCount, ppHeaps);

                D3D12_GPU_DESCRIPTOR_HANDLE gpuHandle = _cbvHeap.Ptr->GetGPUDescriptorHandleForHeapStart();
                gpuHandle.ptr += _deviceResources.CurrentFrameIndex * _cbvDescriptorSize;
                _commandList.Ptr->SetGraphicsRootDescriptorTable(0, gpuHandle);

                D3D12_VIEWPORT viewport = _deviceResources.ScreenViewport;
                _commandList.Ptr->RSSetViewports(1, &viewport);
                D3D12_RECT rect = _scissorRect;
                _commandList.Ptr->RSSetScissorRects(1, &rect);

                D3D12_RESOURCE_BARRIER renderTargetResourceBarrier =
                    CD3DX12_RESOURCE_BARRIER.Transition(
                        _deviceResources.RenderTarget,
                        D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_PRESENT,
                        D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_RENDER_TARGET
                        );
                _commandList.Ptr->ResourceBarrier(1, &renderTargetResourceBarrier);

                D3D12_CPU_DESCRIPTOR_HANDLE renderTargetView = _deviceResources.RenderTargetView;
                D3D12_CPU_DESCRIPTOR_HANDLE depthStencilView = _deviceResources.DepthStencilView;


                _commandList.Ptr->ClearRenderTargetView(renderTargetView, DirectXHelper.CornflowerBlue, 0, null);
                _commandList.Ptr->ClearDepthStencilView(depthStencilView, D3D12_CLEAR_FLAGS.D3D12_CLEAR_FLAG_DEPTH,
                                                        1, 0, 0, null);

                _commandList.Ptr->OMSetRenderTargets(1, &renderTargetView, TerraFX.Interop.Windows.FALSE, &depthStencilView);

                _commandList.Ptr->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY.D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

                D3D12_VERTEX_BUFFER_VIEW vertexBufferView = _vertexBufferView;
                D3D12_INDEX_BUFFER_VIEW  indexBufferView  = _indexBufferView;
                _commandList.Ptr->IASetVertexBuffers(0, 1, &vertexBufferView);
                _commandList.Ptr->IASetIndexBuffer(&indexBufferView);

                _commandList.Ptr->DrawIndexedInstanced(36, 1, 0, 0, 0);

                D3D12_RESOURCE_BARRIER presentResourceBarrier =
                    CD3DX12_RESOURCE_BARRIER.Transition(
                        _deviceResources.RenderTarget,
                        D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_RENDER_TARGET,
                        D3D12_RESOURCE_STATES.D3D12_RESOURCE_STATE_PRESENT);

                _commandList.Ptr->ResourceBarrier(1, &presentResourceBarrier);
            }

            ThrowIfFailed(_commandList.Ptr->Close());

            const uint          ppCommandListsCount = 1;
            ID3D12CommandList **ppCommandLists      = stackalloc ID3D12CommandList *[(int)ppCommandListsCount]
            {
                (ID3D12CommandList *)_commandList.Ptr
            };

            _deviceResources.CommandQueue->ExecuteCommandLists(ppCommandListsCount, ppCommandLists);

            return(true);
        }
 public void ClearUnorderedAccessViewFloat(D3D12_GPU_DESCRIPTOR_HANDLE ViewGPUHandleInCurrentHeap, D3D12_CPU_DESCRIPTOR_HANDLE ViewCPUHandle, ID3D12Resource *pResource, [NativeTypeName("const FLOAT [4]")] float *Values, uint NumRects, [NativeTypeName("const D3D12_RECT *")] RECT *pRects)
 {
     ((delegate * unmanaged <ID3D12GraphicsCommandList2 *, D3D12_GPU_DESCRIPTOR_HANDLE, D3D12_CPU_DESCRIPTOR_HANDLE, ID3D12Resource *, float *, uint, RECT *, void>)(lpVtbl[50]))((ID3D12GraphicsCommandList2 *)Unsafe.AsPointer(ref this), ViewGPUHandleInCurrentHeap, ViewCPUHandle, pResource, Values, NumRects, pRects);
 }
 public void SetGraphicsRootDescriptorTable(uint RootParameterIndex, D3D12_GPU_DESCRIPTOR_HANDLE BaseDescriptor)
 {
     ((delegate * unmanaged <ID3D12GraphicsCommandList2 *, uint, D3D12_GPU_DESCRIPTOR_HANDLE, void>)(lpVtbl[32]))((ID3D12GraphicsCommandList2 *)Unsafe.AsPointer(ref this), RootParameterIndex, BaseDescriptor);
 }