Beispiel #1
0
        private GpuResource AllocateCommitted(GpuResourceDesc desc, D3D12_RESOURCE_ALLOCATION_INFO allocInfo)
        {
            using var device = _device.Copy();

            var heapProperties = GetHeapProperties(desc);

            var clearVal = desc.ClearValue.GetValueOrDefault();

            using ComPtr <ID3D12Resource> resource = default;

            Guard.ThrowIfFailed(device.Get()->CreateCommittedResource(
                                    &heapProperties,
                                    desc.HeapFlags,
                                    &desc.ResourceFormat.D3D12ResourceDesc,
                                    desc.InitialState,
                                    desc.ClearValue is null ? null : &clearVal,
                                    resource.Guid,
                                    ComPtr.GetVoidAddressOf(&resource)
                                    ));

            return(new GpuResource(
                       resource.Move(),
                       desc,
                       allocInfo.SizeInBytes,
                       0,
Beispiel #2
0
        private void VerifyDesc(GpuResourceDesc desc)
        {
            var flags = desc.AllocFlags;

            if (flags.HasFlag(GpuAllocFlags.ForceAllocateComitted))
            {
                Debug.Assert(!flags.HasFlag(GpuAllocFlags.ForceAllocateNotComitted));
            }
            else if (flags.HasFlag(GpuAllocFlags.ForceAllocateNotComitted))
            {
                Debug.Assert(!flags.HasFlag(GpuAllocFlags.ForceAllocateComitted));
            }
        }
Beispiel #3
0
        /// <summary>
        /// Allocates a new region of GPU memory
        /// </summary>
        /// <param name="desc">The description for the resource to allocate</param>
        /// <returns>A new <see cref="GpuResource"/> which encapsulates the allocated region</returns>
        public GpuResource Allocate(
            GpuResourceDesc desc
            )
        {
            VerifyDesc(desc);

            var info = GetAllocationInfo(desc);

            if (desc.AllocFlags.HasFlag(GpuAllocFlags.ForceAllocateComitted))
            {
                return(AllocateCommitted(desc, info));
            }

            return(AllocatePlacedFromHeap(desc, info));
        }
Beispiel #4
0
        /// <summary>
        /// Allocates a new <see cref="IndexBuffer{TIndex}"/>
        /// </summary>
        /// <typeparam name="TIndex">The type of each index</typeparam>
        /// <param name="indexCount">The number of indices</param>
        /// <param name="type">The type of GPU memory to allocate in</param>
        /// <param name="flags">Any additional allocation flags passed to the allocator</param>
        /// <returns>A new <see cref="IndexBuffer{TIndex}"/></returns>
        public IndexBuffer <TIndex> AllocateIndexBuffer <TIndex>(
            uint indexCount,
            GpuMemoryType type,
            GpuAllocFlags flags = GpuAllocFlags.None
            ) where TIndex : unmanaged
        {
            Debug.Assert(type != GpuMemoryType.CpuReadOptimized);

            var desc = new GpuResourceDesc(
                GpuResourceFormat.Buffer((uint)sizeof(TIndex) * indexCount),
                type,
                type == GpuMemoryType.CpuWriteOptimized ? D3D12_RESOURCE_STATE_GENERIC_READ : D3D12_RESOURCE_STATE_INDEX_BUFFER,
                flags
                );

            return(new IndexBuffer <TIndex>(Allocate(desc)));
        }
Beispiel #5
0
        internal GpuResource(
            ComPtr <ID3D12Resource> resource,
            GpuResourceDesc desc,
            ulong size,
            ulong offset,
            AllocatorHeap heap,
            GpuAllocator allocator
            )
        {
            _value         = resource.Move();
            State          = desc.InitialState;
            ResourceFormat = desc.Format;
            Type           = desc.GpuMemoryType;
            _size          = size;
            _offset        = offset;
            _heap          = heap;
            _allocator     = allocator;

            GpuAddress = UnderlyingResource->GetGPUVirtualAddress();
        }
Beispiel #6
0
 private D3D12_RESOURCE_ALLOCATION_INFO GetAllocationInfo(GpuResourceDesc desc)
 {
     // TODO use ID3D12Device4::GetResourceAllocationInfo1 for doing all our computations in one go
     return(_device.Get()->GetResourceAllocationInfo(0 /* TODO: MULTI-GPU */, 1, &desc.ResourceFormat.D3D12ResourceDesc));
 }