public IndexBuffer(GraphicsDevice graphicsDevice, IndexFormat format, ResourceUsage usage, int indexCount) : base(graphicsDevice, new System.Diagnostics.StackTrace(1)) { if (indexCount <= 0) { throw new ArgumentOutOfRangeException("indexCount", indexCount, "indexCount must be bigger than zero."); } if (usage == ResourceUsage.Immutable) { throw new ArgumentException("data", "Immutable buffers must be initialized with data."); } EnumConverter.Convert(Format); //Check whether format is supported this.Usage = usage; this.Format = format; SizeBytes = indexCount * graphicsDevice.GetSizeOf(format); IboID = GL.GenBuffer(); if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.None) { graphicsDevice.BindManager.IndexBuffer = this; GL.BufferData(BufferTarget.ElementArrayBuffer, new IntPtr(SizeBytes), IntPtr.Zero, EnumConverter.Convert(Usage)); } else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Extension) { OpenTK.Graphics.OpenGL.GL.Ext.NamedBufferData(IboID, new IntPtr(SizeBytes), IntPtr.Zero, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)EnumConverter.Convert(Usage)); } graphicsDevice.CheckGLError("IndexBuffer Constructor"); }
internal VertexBuffer(GraphicsDevice graphicsDevice, VertexDescription description, ResourceUsage usage, int vertexCount) : base(graphicsDevice, new System.Diagnostics.StackTrace(1)) { if (vertexCount <= 0) { throw new ArgumentException("vertexCount must not be smaller than/ equal to zero.", "vertexCount"); } if (usage == ResourceUsage.Immutable) { throw new ArgumentException("data", "Immutable buffers must be initialized with data."); } this.Description = description; this.Usage = usage; VboID = GL.GenBuffer(); if (!graphicsDevice.OpenGLCapabilities.VertexAttribBinding) { VaoID = GL.GenVertexArray(); LayoutDirty = true; } SizeBytes = vertexCount * graphicsDevice.GetSizeOf(description); if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.None) { graphicsDevice.BindManager.VertexBuffer = this; GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(SizeBytes), IntPtr.Zero, EnumConverter.Convert(Usage)); } else if (graphicsDevice.OpenGLCapabilities.DirectStateAccess == DirectStateAccess.Extension) { OpenTK.Graphics.OpenGL.GL.Ext.NamedBufferData(VboID, new IntPtr(SizeBytes), IntPtr.Zero, (OpenTK.Graphics.OpenGL.ExtDirectStateAccess)EnumConverter.Convert(Usage)); } graphicsDevice.CheckGLError("VertexBuffer Constructor"); }
internal void SetVertexBuffer(int bindingPoint, IVertexBuffer buffer) { if (!graphicsDevice.OpenGLCapabilities.VertexAttribBinding) { throw new PlatformNotSupportedException("VertexAttribBinding is not supported"); } if (buffer != currentVertexBuffers[bindingPoint]) { if (buffer == null) { GL.BindVertexBuffer(bindingPoint, 0, (IntPtr)0, 0); } else { if (bindingPoint >= graphicsDevice.OpenGLCapabilities.MaxVertexAttribBindings) { throw new PlatformNotSupportedException("bindingPoint exceeds maximum amount of VertexAttribBindings"); } if (currentVertexBuffer == buffer) { currentVertexBuffer = null; } VertexBuffer internalVertexBuffer = graphicsDevice.Cast <VertexBuffer>(buffer, "buffer"); int stride = graphicsDevice.GetSizeOf(internalVertexBuffer.Description); if (stride > graphicsDevice.OpenGLCapabilities.MaxVertexAttribStride) { throw new GraphicsException(string.Format("The vertex size exceeds the maximum of {0}.", graphicsDevice.OpenGLCapabilities.MaxVertexAttribStride)); } GL.BindVertexBuffer(0, internalVertexBuffer.VboID, (IntPtr)0, stride); } currentVertexBuffers[bindingPoint] = buffer; } }