Ejemplo n.º 1
0
        /// <summary>
        /// Function to create a new <see cref="GorgonRawView"/> for this buffer.
        /// </summary>
        /// <param name="elementType">The type of data to interpret elements within the buffer as.</param>
        /// <param name="startElement">[Optional] The first element to start viewing from.</param>
        /// <param name="elementCount">[Optional] The number of elements to view.</param>
        /// <returns>A <see cref="GorgonRawReadWriteView"/> used to bind the buffer to a shader.</returns>
        /// <exception cref="GorgonException">Thrown when this buffer does not have a <see cref="BufferBinding"/> of <see cref="BufferBinding.ReadWrite"/>.
        /// <para>-or-</para>
        /// <para>Thrown when this buffer has a usage of <see cref="ResourceUsage.Staging"/>.</para>
        /// </exception>
        /// <remarks>
        /// <para>
        /// This will create a shader resource view that makes a buffer accessible to shaders. This allows viewing of the buffer data in a different format, or even a subsection of the buffer from within
        /// the shader.
        /// </para>
        /// <para>
        /// The <paramref name="elementType"/> parameter is used present the raw buffer data as another type to the shader.
        /// </para>
        /// <para>
        /// The <paramref name="startElement"/> parameter defines the starting data element to allow access to within the shader. If this value falls outside of the range of available elements, then it
        /// will be clipped to the upper and lower bounds of the element range. If this value is left at 0, then first element is viewed.
        /// </para>
        /// <para>
        /// To determine how many elements are in a buffer, use the <see cref="GetTotalElementCount"/> method.
        /// </para>
        /// <para>
        /// The <paramref name="elementCount"/> parameter defines how many elements to allow access to inside of the view. If this value falls outside of the range of available elements, then it will be
        /// clipped to the upper or lower bounds of the element range. If this value is left at 0, then the entire buffer is viewed.
        /// </para>
        /// </remarks>
        public GorgonRawView GetRawView(RawBufferElementType elementType, int startElement = 0, int elementCount = 0)
        {
            if ((Usage == ResourceUsage.Staging) ||
                ((Binding & BufferBinding.Shader) != BufferBinding.Shader))
            {
                throw new GorgonException(GorgonResult.CannotCreate, string.Format(Resources.GORGFX_ERR_SRV_NOT_VALID, Name));
            }

            int totalElementCount = GetTotalRawElementCount();

            startElement = startElement.Min(totalElementCount - 1).Max(0);

            // If we didn't specify a count, then do so now.
            if (elementCount < 1)
            {
                elementCount = totalElementCount - startElement;
            }

            elementCount = elementCount.Min(totalElementCount - startElement).Max(1);

            var key = new BufferShaderViewKey(startElement, elementCount, elementType);

            if (GetView(key) is GorgonRawView view)
            {
                return(view);
            }

            view = new GorgonRawView(this, elementType, startElement, elementCount, totalElementCount);
            view.CreateNativeView();
            RegisterView(key, view);
            return(view);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Function to create a new <see cref="GorgonRawReadWriteView"/> for this buffer.
        /// </summary>
        /// <param name="elementType">The type of data to interpret elements within the buffer as.</param>
        /// <param name="startElement">[Optional] The first element to start viewing from.</param>
        /// <param name="elementCount">[Optional] The number of elements to view.</param>
        /// <returns>A <see cref="GorgonRawReadWriteView"/> used to bind the buffer to a shader.</returns>
        /// <exception cref="GorgonException">Thrown when this buffer does not have a <see cref="BufferBinding"/> of <see cref="BufferBinding.ReadWrite"/>.
        /// <para>-or-</para>
        /// <para>Thrown when this buffer has a usage of <see cref="ResourceUsage.Staging"/>.</para>
        /// </exception>
        /// <remarks>
        /// <para>
        /// This will create an unordered access view that makes a buffer accessible to shaders using unordered access to the data. This allows viewing of the buffer data in a
        /// different format, or even a subsection of the buffer from within the shader.
        /// </para>
        /// <para>
        /// The <paramref name="elementType"/> parameter is used present the raw buffer data as another type to the shader.
        /// </para>
        /// <para>
        /// The <paramref name="startElement"/> parameter defines the starting data element to allow access to within the shader. If this value falls outside of the range of available elements, then it
        /// will be clipped to the upper and lower bounds of the element range. If this value is left at 0, then first element is viewed.
        /// </para>
        /// <para>
        /// To determine how many elements are in a buffer, use the <see cref="GetTotalElementCount"/> method.
        /// </para>
        /// <para>
        /// The <paramref name="elementCount"/> parameter defines how many elements to allow access to inside of the view. If this value falls outside of the range of available elements, then it will be
        /// clipped to the upper or lower bounds of the element range. If this value is left at 0, then the entire buffer is viewed.
        /// </para>
        /// </remarks>
        public GorgonRawReadWriteView GetRawReadWriteView(RawBufferElementType elementType, int startElement = 0, int elementCount = 0)
        {
            if ((Usage == ResourceUsage.Staging) ||
                ((Binding & BufferBinding.ReadWrite) != BufferBinding.ReadWrite))
            {
                throw new GorgonException(GorgonResult.CannotCreate, string.Format(Resources.GORGFX_ERR_UAV_RESOURCE_NOT_VALID, Name));
            }

            // Ensure the size of the data type fits the requested format.
            int totalElementCount = GetTotalRawElementCount();

            startElement = startElement.Min(totalElementCount - 1).Max(0);

            if (elementCount <= 0)
            {
                elementCount = totalElementCount - startElement;
            }

            elementCount = elementCount.Min(totalElementCount - startElement).Max(1);

            var key = new BufferShaderViewKey(startElement, elementCount, elementType);
            GorgonRawReadWriteView result = GetReadWriteView <GorgonRawReadWriteView>(key);

            if (result != null)
            {
                return(result);
            }

            result = new GorgonRawReadWriteView(this, startElement, elementCount, totalElementCount, elementType);
            result.CreateNativeView();
            RegisterReadWriteView(key, result);

            return(result);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GorgonRawView"/> class.
 /// </summary>
 /// <param name="buffer">The buffer to bind to the view.</param>
 /// <param name="elementType">The type of the elements stored within the buffer.</param>
 /// <param name="startingElement">The starting element in the buffer to view.</param>
 /// <param name="elementCount">The number of elements in the buffer to view.</param>
 /// <param name="totalElementCount">The total number of elements in the buffer.</param>
 internal GorgonRawView(GorgonBuffer buffer,
                        RawBufferElementType elementType,
                        int startingElement,
                        int elementCount,
                        int totalElementCount)
     : base(buffer, startingElement, elementCount, totalElementCount) => ElementType = elementType;
Ejemplo n.º 4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="GorgonRawReadWriteView"/> class.
 /// </summary>
 /// <param name="buffer">The buffer to assign to the view.</param>
 /// <param name="elementStart">The first element in the buffer to view.</param>
 /// <param name="elementCount">The number of elements in the view.</param>
 /// <param name="totalElementCount">The total number of elements in the buffer.</param>
 /// <param name="elementType">The type of element data in the buffer.</param>
 internal GorgonRawReadWriteView(GorgonBuffer buffer, int elementStart, int elementCount, int totalElementCount, RawBufferElementType elementType)
     : base(buffer, elementStart, elementCount, totalElementCount) => ElementType = elementType;
Ejemplo n.º 5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BufferShaderViewKey"/> struct.
 /// </summary>
 /// <param name="start">The start.</param>
 /// <param name="count">The count.</param>
 /// <param name="elementType">Type of the element.</param>
 public BufferShaderViewKey(int start, int count, RawBufferElementType elementType)
     : this(start, count, (int)elementType)
 {
 }