/// <summary> /// Creates a <see cref="VertexBuffer{T}"/> with specified length, optional index buffer and usage hint. /// </summary> /// <param name="graphicsDevice">The <see cref="GraphicsDevice"/> this <see cref="VertexBuffer{T}"/> will use.</param> /// <param name="storageLength">The length for the <see cref="VertexBuffer{T}"/>'s element storage measured in vertices.</param> /// <param name="indexStorageLength">The length for the <see cref="VertexBuffer{T}"/>'s index storage measured in index elements, or 0 for no index storage.</param> /// <param name="indexElementType">The type of index element to use.</param> /// <param name="usageHint">Used by the graphics driver to optimize performance.</param> public VertexBuffer(GraphicsDevice graphicsDevice, uint storageLength, uint indexStorageLength, DrawElementsType indexElementType, BufferUsageARB usageHint) { ValidateStorageLength(storageLength); ElementSize = (uint)Marshal.SizeOf <T>(); uint bufferStorageLengthBytes; uint elementSubsetLengthBytes = storageLength * ElementSize; bool hasIndexBuffer = indexStorageLength > 0; uint indexSubsetStartBytes; if (hasIndexBuffer) { uint indexElementSize = IndexBufferSubset.GetSizeInBytesOfElementType(indexElementType); indexSubsetStartBytes = (elementSubsetLengthBytes + indexElementSize - 1) / indexElementSize * indexElementSize; bufferStorageLengthBytes = indexSubsetStartBytes + indexElementSize * indexStorageLength; } else { indexSubsetStartBytes = 0; bufferStorageLengthBytes = elementSubsetLengthBytes; } Buffer = new BufferObject(graphicsDevice, bufferStorageLengthBytes, usageHint); DataSubset = new VertexDataBufferSubset <T>(Buffer, 0, storageLength); IndexBufferSubset indexSubset = hasIndexBuffer ? new IndexBufferSubset(Buffer, indexSubsetStartBytes, indexStorageLength, indexElementType) : null; VertexArray = VertexArray.CreateSingleBuffer <T>(graphicsDevice, DataSubset, indexSubset); }
/// <summary> /// Creates a <see cref="VertexArray"/> /// </summary> /// <param name="graphicsDevice">The <see cref="GraphicsDevice"/> this resource will use.</param> /// <param name="attribSources"> /// The <see cref="VertexAttribSource"/> array that will be stored by this <see cref="VertexArray"/>. /// The library user must NOT have a reference to this array! /// </param> /// <param name="indexBuffer">An index buffer to attach to the vertex array, null if none is desired.</param> /// <param name="compensateStructPadding">Whether to compensate for struct padding. Default is true.</param> /// <param name="paddingPackValue">The struct packing value for compensating for padding. Default is 4.</param> internal VertexArray(GraphicsDevice graphicsDevice, VertexAttribSource[] attribSources, IndexBufferSubset indexBuffer = null, bool compensateStructPadding = true, uint paddingPackValue = 4) : base(graphicsDevice) { EnsureAttribsValid(attribSources); this.attribSources = attribSources; Handle = GL.GenVertexArray(); IndexBuffer = indexBuffer; UpdateVertexAttributes(compensateStructPadding, paddingPackValue); //this also binds the vertex array }
/// <summary> /// Creates a <see cref="VertexArray"/> in which all the vertex attributes come interleaved from the same data buffer. /// </summary> /// <param name="graphicsDevice">The <see cref="GraphicsDevice"/> this resource will use.</param> /// <param name="bufferSubset">The data buffer that stores all the vertex attributes.</param> /// <param name="attribDescriptions">The descriptions of the vertex attributes.</param> /// <param name="indexBuffer">An index buffer to attach to the vertex array, null if none is desired.</param> /// <param name="compensateStructPadding">Whether to compensate for struct padding. Default is true.</param> /// <param name="paddingPackValue">The struct packing value for compensating for padding. Default is 4.</param> public VertexArray(GraphicsDevice graphicsDevice, DataBufferSubset bufferSubset, ReadOnlySpan <VertexAttribDescription> attribDescriptions, IndexBufferSubset indexBuffer = null, bool compensateStructPadding = true, uint paddingPackValue = 4) : this(graphicsDevice, MakeAttribList(bufferSubset, attribDescriptions), indexBuffer, compensateStructPadding, paddingPackValue) { }
/// <summary> /// Creates a <see cref="VertexArray"/> with the specified attribute sources. /// </summary> /// <param name="graphicsDevice">The <see cref="GraphicsDevice"/> this resource will use.</param> /// <param name="attribSources">The sources from which the data of the vertex attributes will come from.</param> /// <param name="indexBuffer">An index buffer to attach to the vertex array, null if none is desired.</param> /// <param name="compensateStructPadding">Whether to compensate for struct padding. Default is true.</param> /// <param name="paddingPackValue">The struct packing value for compensating for padding. Default is 4.</param> public VertexArray(GraphicsDevice graphicsDevice, ReadOnlySpan <VertexAttribSource> attribSources, IndexBufferSubset indexBuffer = null, bool compensateStructPadding = true, uint paddingPackValue = 4) : this(graphicsDevice, attribSources.ToArray(), indexBuffer, compensateStructPadding, paddingPackValue) { }
/// <summary> /// Creates a <see cref="VertexArray"/> for the specified vertex type, where all of the vertex attributes come interleaved from the same buffer subset. /// </summary> /// <typeparam name="T">The type of vertex to use.</typeparam> /// <param name="graphicsDevice">The <see cref="GraphicsDevice"/> this resource will use.</param> /// <param name="dataBuffer">The buffer from which all attributes come from.</param> /// <param name="indexBuffer">An index buffer to attach to the vertex array, null if none is desired.</param> /// <param name="compensateStructPadding">Whether to compensate for struct padding. Default is true.</param> /// <param name="paddingPackValue">The struct packing value for compensating for padding. Default is 4.</param> public static VertexArray CreateSingleBuffer <T>(GraphicsDevice graphicsDevice, DataBufferSubset dataBuffer, IndexBufferSubset indexBuffer = null, bool compensateStructPadding = true, uint paddingPackValue = 4) where T : unmanaged, IVertex { T t = default; int attribCount = t.AttribDescriptionCount; Span <VertexAttribDescription> attribDescriptions = attribCount > 32 ? new VertexAttribDescription[attribCount] : stackalloc VertexAttribDescription[attribCount]; t.WriteAttribDescriptions(attribDescriptions); return(new VertexArray(graphicsDevice, dataBuffer, attribDescriptions, indexBuffer, compensateStructPadding, paddingPackValue)); }