Exemplo n.º 1
0
 /// <summary>
 /// Checks that the usage hint parameter is valid and throws an exception if it's not.
 /// </summary>
 private static void ValidateBufferUsage(BufferUsageARB usageHint)
 {
     if (!Enum.IsDefined(typeof(BufferUsageARB), usageHint))
     {
         throw new ArgumentException(nameof(usageHint) + " is not a valid " + nameof(BufferUsageARB) + " value", nameof(usageHint));
     }
 }
Exemplo n.º 2
0
        public static Buffer CreateFromData <T>(ReadOnlySpan <T> data,
                                                BufferTargetARB target = BufferTargetARB.ArrayBuffer,
                                                BufferUsageARB usage   = BufferUsageARB.StaticDraw)
        {
            var buffer = Gen(target);

            buffer.Bind();
            buffer.Data(data, usage);
            return(buffer);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a <see cref="BufferObject"/> with a specified length and usage hint.
        /// </summary>
        /// <param name="graphicsDevice">The <see cref="GraphicsDevice"/> this resource will use.</param>
        /// <param name="sizeInBytes">The desired size of the <see cref="BufferObject"/>'s storage measured in bytes.</param>
        /// <param name="usageHint">Used by the graphics driver to optimize performance.</param>
        public BufferObject(GraphicsDevice graphicsDevice, uint sizeInBytes, BufferUsageARB usageHint) : base(graphicsDevice)
        {
            if (usageHint == 0)
            {
                throw new ArgumentException(nameof(usageHint) + " is not a valid " + nameof(BufferUsageARB) + " value");
            }

            Handle = GL.GenBuffer();
            RecreateStorage(sizeInBytes, usageHint);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Recreate this <see cref="BufferObject"/>'s storage with a new size and usage hint.
        /// The contents of the new storage are undefined after this operation.
        /// </summary>
        /// <param name="sizeInBytes">The new size for the <see cref="BufferObject"/> measured in bytes.</param>
        /// <param name="usageHint">The new usage hint for the <see cref="BufferObject"/>, or 0 to keep the previous hint.</param>
        public unsafe void RecreateStorage(uint sizeInBytes, BufferUsageARB usageHint = 0)
        {
            // We check that the parameters are valid, then store them
            ValidateBufferSize(sizeInBytes);

            if (usageHint != 0)
            {
                ValidateBufferUsage(usageHint);
                UsageHint = usageHint;
            }

            StorageLengthInBytes = sizeInBytes;

            // We then bind this buffer and specify it's storage to OpenGL
            GraphicsDevice.BindBufferObject(this);
            GL.BufferData(GraphicsDevice.DefaultBufferTarget, sizeInBytes, (void *)0, UsageHint);
        }
 public abstract unsafe void BufferData([Flow(FlowDirection.In)] BufferTargetARB target, [Flow(FlowDirection.In)] UIntPtr size, [Count(Parameter = "size"), Flow(FlowDirection.In)] void *data, [Flow(FlowDirection.In)] BufferUsageARB usage);
 /// <summary>
 /// To be added.
 /// </summary>
 /// <param name="target">
 /// To be added.
 /// </param>
 /// <param name="size">
 /// To be added.
 /// </param>
 /// <param name="data">
 /// To be added.
 /// This parameter's element count is taken from size.
 /// </param>
 /// <param name="usage">
 /// To be added.
 /// </param>
 public unsafe void BufferData <T0>([Flow(FlowDirection.In)] BufferTargetARB target, [Flow(FlowDirection.In)] uint size, [Count(Parameter = "size"), Flow(FlowDirection.In)] Span <T0> data, [Flow(FlowDirection.In)] BufferUsageARB usage) where T0 : unmanaged
 {
     // IntPtrOverloader
     BufferData(target, new UIntPtr(size), data, usage);
 }
Exemplo n.º 7
0
 public static Buffer CreateFromData <T>(Span <T> data,
                                         BufferTargetARB target = BufferTargetARB.ArrayBuffer,
                                         BufferUsageARB usage   = BufferUsageARB.StaticDraw)
 => CreateFromData <T>((ReadOnlySpan <T>)data, target, usage);
Exemplo n.º 8
0
		public static void BufferData(BufferTargetARB target, UInt32 size, Object data, BufferUsageARB usage)
		{
			GCHandle pin_data = GCHandle.Alloc(data, GCHandleType.Pinned);
			try {
				BufferData(target, size, pin_data.AddrOfPinnedObject(), usage);
			} finally {
				pin_data.Free();
			}
		}
Exemplo n.º 9
0
 /// <summary>
 /// Creates a <see cref="VertexBuffer{T}"/> with specified length, optional index buffer,
 /// usage hint and initial vertex data.
 /// </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>
 /// <param name="data">A <see cref="ReadOnlySpan{T}"/> containing the initial vertex data.</param>
 /// <param name="dataWriteOffset">The offset into the vertex subset's storage at which to start writting the initial data.</param>
 public VertexBuffer(GraphicsDevice graphicsDevice, uint storageLength, uint indexStorageLength, DrawElementsType indexElementType, BufferUsageARB usageHint, ReadOnlySpan <T> data, uint dataWriteOffset = 0)
     : this(graphicsDevice, storageLength, indexStorageLength, indexElementType, usageHint)
 {
     DataSubset.SetData(data, dataWriteOffset);
 }
Exemplo n.º 10
0
        /// <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);
        }
Exemplo n.º 11
0
 /// <summary>
 /// Creates a <see cref="VertexBuffer{T}"/> with specified initial vertex data and
 /// same length as that data <see cref="ReadOnlySpan{T}"/>.
 /// </summary>
 /// <param name="graphicsDevice">The <see cref="GraphicsDevice"/> this <see cref="VertexBuffer{T}"/> will use.</param>
 /// <param name="data">A <see cref="ReadOnlySpan{T}"/> containing the initial vertex data.</param>
 /// <param name="usageHint">Used by the graphics driver to optimize performance.</param>
 public VertexBuffer(GraphicsDevice graphicsDevice, ReadOnlySpan <T> data, BufferUsageARB usageHint)
     : this(graphicsDevice, (uint)data.Length, 0, default, usageHint, data)
Exemplo n.º 12
0
 /// <summary>
 /// Creates a <see cref="VertexBuffer{T}"/> with specified initial vertex data and length but no index buffer.
 /// </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="usageHint">Used by the graphics driver to optimize performance.</param>
 /// <param name="data">A <see cref="ReadOnlySpan{T}"/> containing the initial vertex data.</param>
 /// <param name="dataWriteOffset">The offset into the vertex subset's storage at which to start writting the initial data.</param>
 public VertexBuffer(GraphicsDevice graphicsDevice, uint storageLength, BufferUsageARB usageHint, ReadOnlySpan <T> data, uint dataWriteOffset = 0)
     : this(graphicsDevice, storageLength, 0, default, usageHint, data, dataWriteOffset)
 {
 }
Exemplo n.º 13
0
 public static void qglBufferData(BufferTargetARB target, int size, void *data, BufferUsageARB usage) => glBufferData(target, size, data, usage);
Exemplo n.º 14
0
 public static unsafe void BufferData <T0>(this ArbVertexBufferObject thisApi, [Flow(FlowDirection.In)] BufferTargetARB target, [Flow(FlowDirection.In)] nuint size, [Count(Parameter = "size"), Flow(FlowDirection.In)] ReadOnlySpan <T0> data, [Flow(FlowDirection.In)] BufferUsageARB usage) where T0 : unmanaged
 {
     // SpanOverloader
     thisApi.BufferData(target, size, in data.GetPinnableReference(), usage);
 }
Exemplo n.º 15
0
 public abstract void BufferData <T0>([Flow(FlowDirection.In)] BufferTargetARB target, [Flow(FlowDirection.In)] UIntPtr size, [Count(Parameter = "size"), Flow(FlowDirection.In)] Span <T0> data, [Flow(FlowDirection.In)] BufferUsageARB usage) where T0 : unmanaged;
Exemplo n.º 16
0
		public static void BufferData(BufferTargetARB target, UInt32 size, IntPtr data, BufferUsageARB usage)
		{
			Debug.Assert(Delegates.pglBufferData != null, "pglBufferData not implemented");
			Delegates.pglBufferData((Int32)target, size, data, (Int32)usage);
			LogFunction("glBufferData({0}, {1}, 0x{2}, {3})", target, size, data.ToString("X8"), usage);
		}
Exemplo n.º 17
0
 /// <summary>
 /// To be added.
 /// </summary>
 /// <param name="target">
 /// To be added.
 /// </param>
 /// <param name="size">
 /// To be added.
 /// </param>
 /// <param name="data">
 /// To be added.
 /// This parameter's element count is taken from size.
 /// </param>
 /// <param name="usage">
 /// To be added.
 /// </param>
 public unsafe void BufferData([Flow(FlowDirection.In)] BufferTargetARB target, [Flow(FlowDirection.In)] uint size, [Count(Parameter = "size"), Flow(FlowDirection.In)] void *data, [Flow(FlowDirection.In)] BufferUsageARB usage)
 {
     // IntPtrOverloader
     BufferData(target, new UIntPtr(size), data, usage);
 }
Exemplo n.º 18
0
        protected void Init(IntPtr dataPointer)
        {
            switch (Description.Usage)
            {
            case GraphicsResourceUsage.Default:
            case GraphicsResourceUsage.Immutable:
                BufferUsageHint = BufferUsageARB.StaticDraw;
                break;

            case GraphicsResourceUsage.Dynamic:
            case GraphicsResourceUsage.Staging:
                BufferUsageHint = BufferUsageARB.DynamicDraw;
                break;

            default:
                throw new ArgumentOutOfRangeException("description.Usage");
            }

            using (var openglContext = GraphicsDevice.UseOpenGLCreationContext())
            {
                if ((Flags & BufferFlags.ShaderResource) != 0 && !GraphicsDevice.HasTextureBuffers)
                {
                    // Create a texture instead of a buffer on platforms where it's not supported
                    elementCount  = SizeInBytes / bufferTextureElementSize;
                    TextureTarget = TextureTarget.Texture2D;
                    GL.GenTextures(1, out TextureId);
                    GL.BindTexture(TextureTarget, TextureId);

                    GL.TexParameter(TextureTarget, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
                    GL.TexParameter(TextureTarget, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
                    GL.TexParameter(TextureTarget, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
                    GL.TexParameter(TextureTarget, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);

                    UpdateTextureSubresource(dataPointer, 0, 0, SizeInBytes);

                    GL.BindTexture(TextureTarget, 0);

                    if (openglContext.CommandList != null)
                    {
                        // If we messed up with some states of a command list, mark dirty states
                        openglContext.CommandList.boundShaderResourceViews[openglContext.CommandList.activeTexture] = null;
                    }
                }
                else
                {
                    // If we're on main context, unbind VAO before binding context.
                    // It will be bound again on next draw.
                    //if (!creationContext.UseDeviceCreationContext)
                    //    GraphicsDevice.UnbindVertexArrayObject();

                    GL.GenBuffers(1, out BufferId);
                    GL.BindBuffer(BufferTarget, BufferId);
                    GL.BufferData(BufferTarget, (UIntPtr)Description.SizeInBytes, (void *)dataPointer, BufferUsageHint);
                    GL.BindBuffer(BufferTarget, 0);

                    if ((Flags & BufferFlags.ShaderResource) != 0)
                    {
#if STRIDE_GRAPHICS_API_OPENGLES
                        Internal.Refactor.ThrowNotImplementedException();
#else
                        TextureTarget = TextureTarget.TextureBuffer;
                        GL.GenTextures(1, out TextureId);
                        GL.BindTexture(TextureTarget, TextureId);
                        // TODO: Check if this is really valid to cast PixelInternalFormat to SizedInternalFormat in all cases?
                        GL.TexBuffer(TextureTarget.TextureBuffer, (SizedInternalFormat)TextureInternalFormat, BufferId);
#endif
                    }
                }
            }
        }
Exemplo n.º 19
0
        internal uint GeneratePixelBufferObject(BufferTargetARB target, PixelStoreParameter alignment, BufferUsageARB bufferUsage, int totalSize)
        {
            uint result;

            GL.GenBuffers(1, out result);
            GL.BindBuffer(target, result);
            if (RowPitch < 4)
            {
                GL.PixelStore(alignment, 1);
            }
            GL.BufferData(target, (UIntPtr)totalSize, IntPtr.Zero, bufferUsage);
            GL.BindBuffer(target, 0);

            return(result);
        }