/// <summary>
            /// Creates an immutable vertex buffer from the given vertex array.
            /// </summary>
            /// <typeparam name="T">Type of a vertex.</typeparam>
            /// <param name="device">Graphics device.</param>
            /// <param name="vertices">The vertex array.</param>
            public static unsafe D3D11.ID3D11Buffer CreateImmutableVertexBuffer <T>(EngineDevice device, params T[][] vertices)
                where T : unmanaged
            {
                device.EnsureNotNull(nameof(device));
                vertices.EnsureNotNull(nameof(vertices));

                var vertexCount = vertices.Sum(actArray => actArray.Length);
                var vertexSize  = sizeof(T);

                using var outStream = new DataStream(
                          vertexCount * vertexSize,
                          true, true);

                foreach (var actArray in vertices)
                {
                    outStream.WriteRange(actArray);
                }
                outStream.Position = 0;

                var bufferDescription = new D3D11.BufferDescription
                {
                    BindFlags           = D3D11.BindFlags.VertexBuffer,
                    CpuAccessFlags      = D3D11.CpuAccessFlags.None,
                    OptionFlags         = D3D11.ResourceOptionFlags.None,
                    SizeInBytes         = vertexCount * vertexSize,
                    Usage               = D3D11.ResourceUsage.Immutable,
                    StructureByteStride = vertexSize
                };

                var result = device.DeviceD3D11_1.CreateBuffer(bufferDescription, outStream.DataPointer);

                return(result);
            }
Exemple #2
0
        public D3D11Buffer(ID3D11Device device, uint sizeInBytes, BufferUsage usage, uint structureByteStride, bool rawBuffer)
        {
            _device              = device;
            SizeInBytes          = sizeInBytes;
            Usage                = usage;
            _structureByteStride = structureByteStride;
            _rawBuffer           = rawBuffer;

            Vortice.Direct3D11.BufferDescription bd = new Vortice.Direct3D11.BufferDescription(
                (int)sizeInBytes,
                D3D11Formats.VdToD3D11BindFlags(usage),
                ResourceUsage.Default);
            if ((usage & BufferUsage.StructuredBufferReadOnly) == BufferUsage.StructuredBufferReadOnly ||
                (usage & BufferUsage.StructuredBufferReadWrite) == BufferUsage.StructuredBufferReadWrite)
            {
                if (rawBuffer)
                {
                    bd.OptionFlags = ResourceOptionFlags.BufferAllowRawViews;
                }
                else
                {
                    bd.OptionFlags         = ResourceOptionFlags.BufferStructured;
                    bd.StructureByteStride = (int)structureByteStride;
                }
            }
            if ((usage & BufferUsage.IndirectBuffer) == BufferUsage.IndirectBuffer)
            {
                bd.OptionFlags = ResourceOptionFlags.DrawIndirectArguments;
            }

            if ((usage & BufferUsage.Dynamic) == BufferUsage.Dynamic)
            {
                bd.Usage          = ResourceUsage.Dynamic;
                bd.CpuAccessFlags = CpuAccessFlags.Write;
            }
            else if ((usage & BufferUsage.Staging) == BufferUsage.Staging)
            {
                bd.Usage          = ResourceUsage.Staging;
                bd.CpuAccessFlags = CpuAccessFlags.Read | CpuAccessFlags.Write;
            }

            _buffer = device.CreateBuffer(bd);
        }
            /// <summary>
            /// Creates a dynamic vertex buffer for the given vertex type and maximum capacity.
            /// </summary>
            /// <typeparam name="T">Type of the vertices.</typeparam>
            /// <param name="device">Graphics device.</param>
            /// <param name="vertexCount">Maximum count of vertices within the buffer.</param>
            public static unsafe D3D11.ID3D11Buffer CreateDynamicVertexBuffer <T>(EngineDevice device, int vertexCount)
                where T : unmanaged
            {
                device.EnsureNotNull(nameof(device));
                vertexCount.EnsurePositiveOrZero(nameof(vertexCount));

                var vertexSize        = sizeof(T);
                var bufferDescription = new D3D11.BufferDescription
                {
                    BindFlags           = D3D11.BindFlags.VertexBuffer,
                    CpuAccessFlags      = D3D11.CpuAccessFlags.Write,
                    OptionFlags         = D3D11.ResourceOptionFlags.None,
                    SizeInBytes         = vertexCount * vertexSize,
                    Usage               = D3D11.ResourceUsage.Dynamic,
                    StructureByteStride = vertexCount * vertexSize
                };

                return(device.DeviceD3D11_1.CreateBuffer(bufferDescription));
            }
            /// <summary>
            /// Creates an immutable index buffer from the given index array.
            /// </summary>
            /// <param name="device">Graphics device.</param>
            /// <param name="indices">Source index array.</param>
            public static D3D11.ID3D11Buffer CreateImmutableIndexBuffer(EngineDevice device, params int[][] indices)
            {
                device.EnsureNotNull(nameof(device));
                indices.EnsureNotNull(nameof(indices));

                const int BYTES_PER_INDEX = sizeof(uint);
                var       countIndices    = indices.Sum(actArray => actArray.Length);

                using var outStreamIndex = new DataStream(
                          countIndices *
                          BYTES_PER_INDEX, true, true);

                // Write all instance data to the target stream
                foreach (var actArray in indices)
                {
                    var actArrayLength = actArray.Length;

                    for (var loop = 0; loop < actArrayLength; loop++)
                    {
                        outStreamIndex.Write((uint)actArray[loop]);
                    }
                }

                outStreamIndex.Position = 0;

                // ConfigureLoading index buffer
                var bufferDescriptionIndex = new D3D11.BufferDescription
                {
                    BindFlags      = D3D11.BindFlags.IndexBuffer,
                    CpuAccessFlags = D3D11.CpuAccessFlags.None,
                    OptionFlags    = D3D11.ResourceOptionFlags.None,
                    SizeInBytes    = countIndices * BYTES_PER_INDEX,
                    Usage          = D3D11.ResourceUsage.Immutable
                };

                // Load the index buffer
                var result = device.DeviceD3D11_1.CreateBuffer(bufferDescriptionIndex, outStreamIndex.DataPointer);

                return(result);
            }
 public ID3D11Buffer CreateBuffer(BufferDescription description, IntPtr dataPointer)
 {
     return(CreateBuffer(description, new SubresourceData(dataPointer)));
 }