示例#1
0
        /// <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));
        }
示例#2
0
        /// <summary>
        /// Function to validate the bindings for a given buffer.
        /// </summary>
        /// <param name="usage">The usage flags for the buffer.</param>
        /// <param name="binding">The bindings to apply to the buffer, pass <b>null</b> to skip usage and binding check.</param>
        internal static void ValidateBufferBindings(ResourceUsage usage, VertexIndexBufferBinding binding)
        {
            switch (usage)
            {
            case ResourceUsage.Dynamic:
            case ResourceUsage.Immutable:
                if ((binding & VertexIndexBufferBinding.StreamOut) == VertexIndexBufferBinding.StreamOut)
                {
                    throw new GorgonException(GorgonResult.CannotCreate, string.Format(Resources.GORGFX_ERR_BUFFER_NO_SO, usage));
                }

                if ((binding & VertexIndexBufferBinding.UnorderedAccess) == VertexIndexBufferBinding.UnorderedAccess)
                {
                    throw new GorgonException(GorgonResult.CannotCreate, string.Format(Resources.GORGFX_ERR_BUFFER_NO_UAV, usage));
                }
                break;

            case ResourceUsage.Staging:
                if (binding != VertexIndexBufferBinding.None)
                {
                    throw new GorgonException(GorgonResult.CannotCreate, string.Format(Resources.GORGFX_ERR_BUFFER_STAGING_CANNOT_BE_BOUND_TO_GPU, binding));
                }
                break;
            }
        }