示例#1
0
        /// <summary>
        /// Creates a new <see cref="Buffer{T}"/> instance with the specified parameters.
        /// </summary>
        /// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
        /// <param name="length">The number of items to store in the current buffer.</param>
        /// <param name="elementSizeInBytes">The size in bytes of each buffer item (including padding, if any).</param>
        /// <param name="resourceType">The resource type for the current buffer.</param>
        /// <param name="allocationMode">The allocation mode to use for the new resource.</param>
        private protected Buffer(GraphicsDevice device, int length, uint elementSizeInBytes, ResourceType resourceType, AllocationMode allocationMode)
        {
            device.ThrowIfDisposed();

            if (resourceType == ResourceType.Constant)
            {
                Guard.IsBetweenOrEqualTo(length, 1, FX.D3D12_REQ_CONSTANT_BUFFER_ELEMENT_COUNT, nameof(length));
            }
            else
            {
                // The maximum length is set such that the aligned buffer size can't exceed uint.MaxValue
                Guard.IsBetweenOrEqualTo(length, 1, (uint.MaxValue / elementSizeInBytes) & ~255, nameof(length));
            }

            if (TypeInfo <T> .IsDoubleOrContainsDoubles &&
                device.D3D12Device->CheckFeatureSupport <D3D12_FEATURE_DATA_D3D12_OPTIONS>(D3D12_FEATURE_D3D12_OPTIONS).DoublePrecisionFloatShaderOps == 0)
            {
                UnsupportedDoubleOperationsException.Throw <T>();
            }

            SizeInBytes    = checked ((nint)(length * elementSizeInBytes));
            GraphicsDevice = device;
            Length         = length;

            if (device.IsCacheCoherentUMA)
            {
                this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, allocationMode, (ulong)SizeInBytes, true);
            }
            else
            {
                this.allocation    = device.Allocator->CreateResource(resourceType, allocationMode, (ulong)SizeInBytes);
                this.d3D12Resource = new ComPtr <ID3D12Resource>(this.allocation.Get()->GetResource());
            }

            device.RentShaderResourceViewDescriptorHandles(out D3D12CpuDescriptorHandle, out D3D12GpuDescriptorHandle);

            switch (resourceType)
            {
            case ResourceType.Constant:
                device.D3D12Device->CreateConstantBufferView(this.d3D12Resource.Get(), SizeInBytes, D3D12CpuDescriptorHandle);
                break;

            case ResourceType.ReadOnly:
                device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, D3D12CpuDescriptorHandle);
                break;

            case ResourceType.ReadWrite:
                device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, D3D12CpuDescriptorHandle);
                break;
            }

            this.d3D12Resource.Get()->SetName(this);
        }
示例#2
0
    /// <summary>
    /// Creates a new <see cref="Buffer{T}"/> instance with the specified parameters.
    /// </summary>
    /// <param name="device">The <see cref="GraphicsDevice"/> associated with the current instance.</param>
    /// <param name="length">The number of items to store in the current buffer.</param>
    /// <param name="elementSizeInBytes">The size in bytes of each buffer item (including padding, if any).</param>
    /// <param name="resourceType">The resource type for the current buffer.</param>
    /// <param name="allocationMode">The allocation mode to use for the new resource.</param>
    private protected Buffer(GraphicsDevice device, int length, uint elementSizeInBytes, ResourceType resourceType, AllocationMode allocationMode)
    {
        device.ThrowIfDisposed();

        if (resourceType == ResourceType.Constant)
        {
            Guard.IsBetweenOrEqualTo(length, 1, D3D12.D3D12_REQ_CONSTANT_BUFFER_ELEMENT_COUNT, nameof(length));
        }
        else
        {
            // The maximum length is set such that the aligned buffer size can't exceed uint.MaxValue
            Guard.IsBetweenOrEqualTo(length, 1, (uint.MaxValue / elementSizeInBytes) & ~255, nameof(length));
        }

        if (TypeInfo <T> .IsDoubleOrContainsDoubles &&
            device.D3D12Device->CheckFeatureSupport <D3D12_FEATURE_DATA_D3D12_OPTIONS>(D3D12_FEATURE_D3D12_OPTIONS).DoublePrecisionFloatShaderOps == 0)
        {
            UnsupportedDoubleOperationsException.Throw <T>();
        }

        nint usableSizeInBytes    = checked ((nint)(length * elementSizeInBytes));
        nint effectiveSizeInBytes = resourceType == ResourceType.Constant ? AlignmentHelper.Pad(usableSizeInBytes, D3D12.D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT) : usableSizeInBytes;

        SizeInBytes    = usableSizeInBytes;
        GraphicsDevice = device;
        Length         = length;

#if NET6_0_OR_GREATER
        this.allocation    = device.Allocator->CreateResource(device.Pool, resourceType, allocationMode, (ulong)effectiveSizeInBytes);
        this.d3D12Resource = new ComPtr <ID3D12Resource>(this.allocation.Get()->GetResource());
#else
        this.d3D12Resource = device.D3D12Device->CreateCommittedResource(resourceType, (ulong)effectiveSizeInBytes, device.IsCacheCoherentUMA);
#endif

        device.RegisterAllocatedResource();
        device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandles);
        device.RentShaderResourceViewDescriptorHandles(out this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView);

        switch (resourceType)
        {
        case ResourceType.Constant:
            device.D3D12Device->CreateConstantBufferView(this.d3D12Resource.Get(), effectiveSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
            break;

        case ResourceType.ReadOnly:
            device.D3D12Device->CreateShaderResourceView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
            break;

        case ResourceType.ReadWrite:
            device.D3D12Device->CreateUnorderedAccessView(this.d3D12Resource.Get(), (uint)length, elementSizeInBytes, this.d3D12ResourceDescriptorHandles.D3D12CpuDescriptorHandle);
            device.D3D12Device->CreateUnorderedAccessViewForClear(
                this.d3D12Resource.Get(),
                DXGI_FORMAT_R32_UINT,
                (uint)(usableSizeInBytes / sizeof(uint)),
                this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12CpuDescriptorHandle,
                this.d3D12ResourceDescriptorHandlesForTypedUnorderedAccessView.D3D12CpuDescriptorHandleNonShaderVisible);
            break;
        }

        this.d3D12Resource.Get()->SetName(this);
    }