/// <summary> /// Function to create a vertex buffer and its binding. /// </summary> /// <typeparam name="T">The type of data representing a vertex, must be an unmanaged value type.</typeparam> /// <param name="graphics">The graphics interface that will create the buffer.</param> /// <param name="vertexCount">The total number vertices that the buffer can hold.</param> /// <param name="usage">[Optional] The intended usage for the buffer.</param> /// <param name="binding">[Optional] The binding options for the buffer.</param> /// <param name="initialData">[Optional] An initial set of vertex data to send to the buffer.</param> /// <param name="bindingIndex">[Optional] The index, in vertices, inside the buffer where binding is to begin.</param> /// <param name="bufferName">[Optional] A name for the buffer.</param> /// <returns>A new <see cref="GorgonVertexBufferBinding"/>.</returns> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="graphics"/> parameter is <b>null</b>.</exception> /// <remarks> /// <para> /// Use this to quickly create a vertex buffer and its binding based on a known vertex data type. /// </para> /// <para> /// Be aware that the <see cref="VertexBuffer"/> created by this method must be disposed manually after it is no longer of any use. /// </para> /// </remarks> /// <seealso cref="GorgonVertexBuffer"/> public static GorgonVertexBufferBinding CreateVertexBuffer <T>(GorgonGraphics graphics, int vertexCount, ResourceUsage usage = ResourceUsage.Default, VertexIndexBufferBinding binding = VertexIndexBufferBinding.None, GorgonNativeBuffer <T> initialData = null, int bindingIndex = 0, string bufferName = null) where T : unmanaged { if (graphics == null) { throw new ArgumentNullException(nameof(graphics)); } int vertexSize = Unsafe.SizeOf <T>(); var buffer = new GorgonVertexBuffer(graphics, new GorgonVertexBufferInfo(bufferName) { SizeInBytes = vertexCount * vertexSize, Binding = binding, Usage = usage }, initialData?.Cast <byte>()); return(new GorgonVertexBufferBinding(buffer, vertexSize, bindingIndex * vertexSize)); }
/// <summary> /// Function to retrieve a copy of this buffer as a staging resource. /// </summary> /// <returns>The staging buffer to retrieve.</returns> public GorgonVertexBuffer GetStaging() { var buffer = new GorgonVertexBuffer(Graphics, new GorgonVertexBufferInfo(_info, $"{Name}_Staging") { Binding = VertexIndexBufferBinding.None, Usage = ResourceUsage.Staging }); CopyTo(buffer); return(buffer); }
/// <summary> /// Function to initialize the buffer data. /// </summary> /// <param name="initialData">The initial data used to populate the buffer.</param> private void Initialize <T>(GorgonNativeBuffer <T> initialData) where T : unmanaged { D3D11.CpuAccessFlags cpuFlags = GetCpuFlags(false, D3D11.BindFlags.IndexBuffer); Log.Print($"{Name} Index Buffer: Creating D3D11 buffer. Size: {SizeInBytes} bytes", LoggingLevel.Simple); GorgonVertexBuffer.ValidateBufferBindings(_info.Usage, 0); D3D11.BindFlags bindFlags = D3D11.BindFlags.IndexBuffer; if ((_info.Binding & VertexIndexBufferBinding.StreamOut) == VertexIndexBufferBinding.StreamOut) { bindFlags |= D3D11.BindFlags.StreamOutput; } if ((_info.Binding & VertexIndexBufferBinding.UnorderedAccess) == VertexIndexBufferBinding.UnorderedAccess) { bindFlags |= D3D11.BindFlags.UnorderedAccess; } var desc = new D3D11.BufferDescription { SizeInBytes = SizeInBytes, Usage = (D3D11.ResourceUsage)_info.Usage, BindFlags = bindFlags, OptionFlags = D3D11.ResourceOptionFlags.None, CpuAccessFlags = cpuFlags, StructureByteStride = 0 }; if ((initialData != null) && (initialData.Length > 0)) { unsafe { D3DResource = Native = new D3D11.Buffer(Graphics.D3DDevice, new IntPtr((void *)initialData), desc) { DebugName = Name }; } } else { D3DResource = Native = new D3D11.Buffer(Graphics.D3DDevice, desc) { DebugName = Name }; } }
/// <summary> /// Function to create a vertex buffer and its binding. /// </summary> /// <typeparam name="T">The type of data representing a vertex, must be an unmanaged value type.</typeparam> /// <param name="graphics">The graphics interface that will create the buffer.</param> /// <param name="info">Information about the buffer to create.</param> /// <param name="initialData">[Optional] An initial set of vertex data to send to the buffer.</param> /// <param name="bindingIndex">[Optional] The index, in vertices, inside the buffer where binding is to begin.</param> /// <returns>A new <see cref="GorgonVertexBufferBinding"/>.</returns> /// <exception cref="ArgumentNullException">Thrown when the <paramref name="graphics"/>, or the <paramref name="info"/> parameter is <b>null</b>.</exception> /// <remarks> /// <para> /// Use this to quickly create a vertex buffer and its binding based on a known vertex data type. /// </para> /// <para> /// Be aware that the <see cref="VertexBuffer"/> created by this method must be disposed manually after it is no longer of any use. /// </para> /// </remarks> /// <seealso cref="GorgonVertexBuffer"/> public static GorgonVertexBufferBinding CreateVertexBuffer <T>(GorgonGraphics graphics, IGorgonVertexBufferInfo info, GorgonNativeBuffer <T> initialData = null, int bindingIndex = 0) where T : unmanaged { if (graphics == null) { throw new ArgumentNullException(nameof(graphics)); } if (info == null) { throw new ArgumentNullException(nameof(info)); } var buffer = new GorgonVertexBuffer(graphics, info, initialData?.Cast <byte>()); int vertexSize = Unsafe.SizeOf <T>(); return(new GorgonVertexBufferBinding(buffer, vertexSize, bindingIndex * vertexSize)); }