コード例 #1
0
        /// <summary>
        /// Setups all the GPU beheviors.
        /// </summary>
        public void Setup()
        {
            uint GetSuitableMemory(PhysicalDeviceMemoryProperties props, MemoryRequirements reqs, MemoryPropertyFlags flags)
            {
                int memIndex = -1;

                for (int i = 0; i < props.MemoryTypes.Length; ++i)
                {
                    if ((reqs.MemoryTypeBits & (1 << i)) != 0 && (props.MemoryTypes[i].PropertyFlags & flags) == flags)
                    {
                        memIndex = i;
                    }
                }

                if (memIndex < 0)
                {
                    throw new NotSupportedException("Failed to allocate vertex buffer memory.");
                }

                return((uint)memIndex);
            }

            // - Gets the physical memory properties
            var memory = RenderDevice.Physical.Handle.GetMemoryProperties();

            try
            {
                // - Create the vertex buffer visible from the CPU
                StagingBuffer = RenderDevice.Handle.CreateBuffer
                                (
                    HeapSize,
                    BufferUsageFlags.TransferSource,
                    SharingMode.Exclusive,
                    null
                                );

                // - Create the vertex buffer inside the GPU private memory
                IndexBuffer = RenderDevice.Handle.CreateBuffer
                              (
                    HeapSize,
                    BufferUsageFlags.IndexBuffer | BufferUsageFlags.TransferDestination,
                    SharingMode.Exclusive,
                    null
                              );

                // - Gets all requirements
                var vertReq = IndexBuffer.GetMemoryRequirements();
                var stagReq = StagingBuffer.GetMemoryRequirements();

                var vertMemoryIndex = GetSuitableMemory(memory, vertReq, MemoryPropertyFlags.DeviceLocal);
                var stagMemoryIndex = GetSuitableMemory(memory, stagReq, MemoryPropertyFlags.HostVisible | MemoryPropertyFlags.HostCoherent);

                // - Allocate the required memory on the VRAM
                VertexVRAM  = RenderDevice.Handle.AllocateMemory(vertReq.Size, vertMemoryIndex);
                StagingVRAM = RenderDevice.Handle.AllocateMemory(stagReq.Size, stagMemoryIndex);

                // - Bind memory to the buffers
                IndexBuffer.BindMemory(VertexVRAM, 0);
                StagingBuffer.BindMemory(StagingVRAM, 0);
            }
            catch
            {
                // TODO: Add exception management
            }
        }