Exemplo n.º 1
0
        internal BufferPool(GraphicsResourceAllocator allocator, GraphicsDevice graphicsDevice, int size, CommandList clist = null)
        {
            constantBufferAlignment = graphicsDevice.ConstantBufferDataPlacementAlignment;
            if (size % constantBufferAlignment != 0)
            {
                throw new ArgumentException($"size needs to be a multiple of constant buffer alignment ({constantBufferAlignment})", nameof(size));
            }

            this.allocator = allocator;

            Size = size;
            if (!UseBufferOffsets)
            {
                Data = Marshal.AllocHGlobal(size);
            }

            this.commandList = clist;

#if XENKO_GRAPHICS_API_VULKAN
            constantBuffer = new Buffer[BUFFERS_PER_POOL];
            for (int i = 0; i < constantBuffer.Length; i++)
            {
                constantBuffer[i] = allocator.GetTemporaryBuffer(new BufferDescription(Size, BufferFlags.ConstantBuffer, GraphicsResourceUsage.Dynamic));
            }
#else
            constantBuffer = new Buffer[1];
#endif
            Reset();
        }
Exemplo n.º 2
0
        public GraphicsContext(GraphicsDevice graphicsDevice, GraphicsResourceAllocator allocator = null, CommandList commandList = null)
        {
            CommandList = commandList ?? graphicsDevice.InternalMainCommandList ?? CommandList.New(graphicsDevice).DisposeBy(graphicsDevice);
            Allocator   = allocator ?? new GraphicsResourceAllocator(graphicsDevice).DisposeBy(graphicsDevice);

            // prepare some resources now, so we don't need to do it during runtime (which can cause lag spikes or worse)
            // DirectX doesn't seem to like this very much, though
            if (GraphicsDevice.Platform == GraphicsPlatform.Vulkan && AvailableAllocators == null && PrepareVulkanAllocatorCount > 0)
            {
                AvailableAllocators = new Queue <ResourceGroupAllocator>();
                while (AvailableAllocators.Count < PrepareVulkanAllocatorCount)
                {
                    AvailableAllocators.Enqueue(new ResourceGroupAllocator(Allocator, CommandList, 2).DisposeBy(graphicsDevice));
                }
            }

            if (AvailableAllocators?.Count > 0)
            {
                ResourceGroupAllocator = AvailableAllocators.Dequeue();
            }
            else
            {
                ResourceGroupAllocator = new ResourceGroupAllocator(Allocator, CommandList, 2).DisposeBy(graphicsDevice);
            }
        }
Exemplo n.º 3
0
        public ResourceGroupAllocator(GraphicsResourceAllocator allocator, CommandList commandList)
        {
            this.allocator      = allocator;
            this.commandList    = commandList;
            this.graphicsDevice = commandList.GraphicsDevice;

            SetupNextDescriptorPool();
        }
Exemplo n.º 4
0
        public ResourceGroupAllocator(GraphicsResourceAllocator allocator, CommandList commandList)
        {
            this.allocator      = allocator;
            this.commandList    = commandList;
            this.graphicsDevice = commandList.GraphicsDevice;

            SetupNextDescriptorPool();

            bufferPools.Add(currentBufferPool = BufferPool.New(allocator, graphicsDevice, 1024 * 1024, commandList));
        }
Exemplo n.º 5
0
        public ResourceGroupAllocator(GraphicsResourceAllocator allocator, CommandList commandList)
        {
            this.allocator      = allocator;
            this.commandList    = commandList;
            this.graphicsDevice = commandList.GraphicsDevice;

            SetupNextDescriptorPool();

            // prepare some buffers now, so we don't need to worry about threading issues later
            PreallocateBuffers(PREALLOCATE_RESOURCE_COUNT);
        }
Exemplo n.º 6
0
        public ResourceGroupAllocator(GraphicsResourceAllocator allocator, CommandList commandList, int initialCount)
        {
            this.allocator      = allocator;
            this.commandList    = commandList;
            this.graphicsDevice = commandList.GraphicsDevice;

            SetupNextDescriptorPool();

            for (int i = 0; i < initialCount; i++)
            {
                bufferPools.Add(currentBufferPool = BufferPool.New(allocator, graphicsDevice, 1024 * 1024, 4, commandList));
            }
        }
Exemplo n.º 7
0
        internal BufferPool(GraphicsResourceAllocator allocator, GraphicsDevice graphicsDevice, int size)
        {
            constantBufferAlignment = graphicsDevice.ConstantBufferDataPlacementAlignment;
            if (size % constantBufferAlignment != 0)
            {
                throw new ArgumentException($"size needs to be a multiple of constant buffer alignment ({constantBufferAlignment})", nameof(size));
            }

            this.allocator = allocator;

            Size = size;
            if (!UseBufferOffsets)
            {
                Data = Marshal.AllocHGlobal(size);
            }

            Reset();
        }
Exemplo n.º 8
0
        internal BufferPool(GraphicsResourceAllocator allocator, GraphicsDevice graphicsDevice, int size, CommandList clist = null)
        {
            constantBufferAlignment = graphicsDevice.ConstantBufferDataPlacementAlignment;
            if (size % constantBufferAlignment != 0)
            {
                throw new ArgumentException($"size needs to be a multiple of constant buffer alignment ({constantBufferAlignment})", nameof(size));
            }

            this.allocator = allocator;

            Size = size;
            if (!UseBufferOffsets)
            {
                Data = Marshal.AllocHGlobal(size);
            }

            this.commandList = clist;

#if XENKO_GRAPHICS_API_VULKAN
            // make buffers now, so we don't have to during gameplay which can cause threading / performance problems
            // eats some memory up, but memory is cheap compared to CPU / stability
            if (PreallocatedBuffers == null)
            {
                PreallocatedBuffers = new Stack <Buffer>();
                for (int i = 0; i < BUFFERS_PER_POOL * POOL_PREALLOCATION; i++)
                {
                    PreallocatedBuffers.Push(allocator.GetTemporaryBuffer(new BufferDescription(Size, BufferFlags.ConstantBuffer, GraphicsResourceUsage.Dynamic)));
                }
            }

            // pick our buffers, hopefully from preallocated pool
            constantBuffer = new Buffer[BUFFERS_PER_POOL];
            for (int i = 0; i < constantBuffer.Length; i++)
            {
                constantBuffer[i] = PreallocatedBuffers.Count > 0 ? PreallocatedBuffers.Pop() :
                                    allocator.GetTemporaryBuffer(new BufferDescription(Size, BufferFlags.ConstantBuffer, GraphicsResourceUsage.Dynamic));
            }
#else
            constantBuffer = new Buffer[1];
#endif
            Reset();
        }
Exemplo n.º 9
0
        internal BufferPool(GraphicsResourceAllocator allocator, GraphicsDevice graphicsDevice, int size, int initialCount, CommandList clist = null)
        {
            constantBufferAlignment = graphicsDevice.ConstantBufferDataPlacementAlignment;
            if (size % constantBufferAlignment != 0)
            {
                throw new ArgumentException($"size needs to be a multiple of constant buffer alignment ({constantBufferAlignment})", nameof(size));
            }

            this.allocator = allocator;

            Size = size;
            if (!UseBufferOffsets)
            {
                Data = Marshal.AllocHGlobal(size);
            }

            this.commandList = clist;

            defaultDescription = new BufferDescription(Size, BufferFlags.ConstantBuffer, GraphicsResourceUsage.Dynamic);

            PrepareBuffers(initialCount);

            Reset();
        }
Exemplo n.º 10
0
 public static BufferPool New(GraphicsResourceAllocator allocator, GraphicsDevice graphicsDevice, int size)
 {
     return(new BufferPool(allocator, graphicsDevice, size));
 }
Exemplo n.º 11
0
 public static BufferPool New(GraphicsResourceAllocator allocator, GraphicsDevice graphicsDevice, int size, int initialCount, CommandList clist = null)
 {
     return(new BufferPool(allocator, graphicsDevice, size, initialCount, clist));
 }
Exemplo n.º 12
0
 public GraphicsContext(GraphicsDevice graphicsDevice, GraphicsResourceAllocator allocator = null, CommandList commandList = null)
 {
     CommandList            = commandList ?? graphicsDevice.InternalMainCommandList ?? CommandList.New(graphicsDevice).DisposeBy(graphicsDevice);
     Allocator              = allocator ?? new GraphicsResourceAllocator(graphicsDevice).DisposeBy(graphicsDevice);
     ResourceGroupAllocator = new ResourceGroupAllocator(Allocator, CommandList).DisposeBy(graphicsDevice);
 }