public StreamBuffer(string name, BufferTarget target, int size, BufferStorageFlags flags = BufferStorageFlags.MapWriteBit, BufferAccessMask mask = (BufferAccessMask)0, int bufferingLevel = 3) { Size = size; TotalBytesPerFrame += size; _bufferingLevel = bufferingLevel; _fences = new FenceSync[bufferingLevel]; _handle = GL.GenBuffer(); GL.BindBuffer(target, _handle); GL.BufferStorage(target, new IntPtr(size * bufferingLevel), IntPtr.Zero, (BufferStorageFlags)flags | BufferStorageFlags.MapPersistentBit); Glob.Utils.SetObjectLabel(ObjectLabelIdentifier.Buffer, _handle, name); var accessFlags = (BufferAccessMask)0; if (flags.HasFlag(BufferStorageFlags.MapWriteBit)) { accessFlags = accessFlags | BufferAccessMask.MapWriteBit; } if (flags.HasFlag(BufferStorageFlags.MapReadBit)) { accessFlags = accessFlags | BufferAccessMask.MapReadBit; } _bufferPointer = GL.MapBufferRange(target, IntPtr.Zero, new IntPtr(size * bufferingLevel), accessFlags | mask | BufferAccessMask.MapPersistentBit); }
public BufferObject(T[] data, BufferStorageFlags storageFlags = BufferStorageFlags.DynamicStorageBit) { GL.CreateBuffers(1, out id); _dataSize = Marshal.SizeOf <T>(); _bufferSize = _dataSize * data.Length; GL.NamedBufferStorage(id, _bufferSize, data, storageFlags); }
void CreateVBOs() { var totalLength = (sizeof(uint) * indicesVboData.Length) + (Marshal.SizeOf(typeof(Vector3)) * positionVboData.Length); var buffers = new int[1]; // ARB_direct_state_access // Allows buffer objects to be initialised without binding them GL.CreateBuffers(1, buffers); { var error = GL.GetError(); if (error != ErrorCode.NoError) { Console.WriteLine("GL.CreateBuffers " + error); } } mRender_VBO_position = buffers[0]; BufferStorageFlags flags = BufferStorageFlags.MapWriteBit | BufferStorageFlags.MapPersistentBit | BufferStorageFlags.MapCoherentBit; GL.NamedBufferStorage(mRender_VBO_position, totalLength, IntPtr.Zero, flags); { var error = GL.GetError(); if (error != ErrorCode.NoError) { Console.WriteLine("GL.NamedBufferStorage " + error); } } }
public void Create <T_DATA>() { int data_size = Marshal.SizeOf(default(T_DATA)); GL.CreateBuffers(1, out this._ppbo); BufferStorageFlags storage = BufferStorageFlags.MapReadBit | BufferStorageFlags.MapPersistentBit; GL.NamedBufferStorage(this._ppbo, data_size, IntPtr.Zero, storage); }
private void load(BufferStorageFlags storage_flags, int index, int size) { // Create Uniform Buffer GL.GenBuffers(1, out _buffer_id); GL.BindBuffer(BufferTarget.UniformBuffer, _buffer_id); GL.BufferStorage(BufferTarget.UniformBuffer, size, (IntPtr)null, storage_flags); GL.BindBuffer(BufferTarget.UniformBuffer, 0); //Bind uniform buffer to binding index since the block size is set and ubo is created GL.BindBufferBase(BufferRangeTarget.UniformBuffer, index, _buffer_id); }
public PersistentBufferObject(BufferUsageHint hint, UInt32 size) : base(BufferTarget.ArrayBuffer, hint) { mySize = size; bind(); BufferStorageFlags flags = BufferStorageFlags.MapWriteBit | BufferStorageFlags.MapPersistentBit | BufferStorageFlags.MapCoherentBit; GL.BufferStorage(BufferTarget.ArrayBuffer, new IntPtr(size), IntPtr.Zero, flags); BufferAccessMask flags2 = BufferAccessMask.MapWriteBit | BufferAccessMask.MapPersistentBit | BufferAccessMask.MapCoherentBit; myPtr = GL.MapBufferRange(BufferTarget.ArrayBuffer, IntPtr.Zero, new IntPtr(size), flags2); unbind(); }
public static BufferObject CreateBuffer(int size, IntPtr data, BufferStorageFlags flags) { BufferObject b = new BufferObject(); b.Bind(BufferTarget.CopyWriteBuffer); b.immutable |= IMMUTABLE; if ((flags & BufferStorageFlags.DynamicStorageBit) == 0) { b.immutable |= NOT_DYNAMIC; } GL.NamedBufferStorage(b.ID, size, data, flags); return(b); }
public static BufferObject CreateBuffer <T>(T[] data, BufferStorageFlags flags) where T : struct { BufferObject b = new BufferObject(); b.Bind(BufferTarget.CopyWriteBuffer); b.immutable |= IMMUTABLE; if ((flags & BufferStorageFlags.DynamicStorageBit) == 0) { b.immutable |= NOT_DYNAMIC; } GL.NamedBufferStorage(b.ID, data.Length * Marshal.SizeOf(typeof(T)), data, flags); return(b); }
/// <summary> /// Construct a new buffer /// </summary> /// <param name="handle"></param> /// <param name="name"></param> /// <param name="size"></param> /// <param name="flags"></param> /// <param name="data"></param> private Buffer( int handle, string name, IntPtr size, BufferStorageFlags flags = BufferStorageFlags.None, IntPtr data = default ) { GL.BindBuffer(BufferTarget.ArrayBuffer, handle); GL.BufferStorage(BufferTarget.ArrayBuffer, size, data, flags); if (State.VersionIsAtLeast(4, 3)) { GL.ObjectLabel(ObjectLabelIdentifier.Buffer, handle, name.Length, name); } Handle = new GLHandle(handle); Name = name; }
void CreateVBOs() { // positionVboHandle = GL.GenBuffer(); // GL.BindBuffer(BufferTarget.ArrayBuffer, positionVboHandle); var totalLength = (sizeof(uint) * indicesVboData.Length) + (Marshal.SizeOf(typeof(Vector3)) * positionVboData.Length); var buffers = new int[1]; // ARB_direct_state_access // Allows buffer objects to be initialised without binding them GL.CreateBuffers(1, buffers); { var error = GL.GetError(); //Debug.WriteLineIf (error != ErrorCode.NoError, "GL.CreateBuffers " + error); } positionVboHandle = buffers [0]; BufferStorageFlags flags = BufferStorageFlags.MapWriteBit | BufferStorageFlags.MapPersistentBit | BufferStorageFlags.MapCoherentBit; GL.NamedBufferStorage(positionVboHandle, totalLength, IntPtr.Zero, flags); { var error = GL.GetError(); //Debug.WriteLineIf (error != ErrorCode.NoError, "GL.NamedBufferStorage " + error); } // GL.BufferData<Vector3>(BufferTarget.ArrayBuffer, // new IntPtr(positionVboData.Length * Vector3.SizeInBytes), // positionVboData, BufferUsageHint.StaticDraw); // normalVboHandle = GL.GenBuffer(); // GL.BindBuffer(BufferTarget.ArrayBuffer, normalVboHandle); // GL.BufferData<Vector3>(BufferTarget.ArrayBuffer, // new IntPtr(positionVboData.Length * Vector3.SizeInBytes), // positionVboData, BufferUsageHint.StaticDraw); // eboHandle = GL.GenBuffer(); // GL.BindBuffer(BufferTarget.ElementArrayBuffer, positionVboHandle); // GL.BufferData(BufferTarget.ElementArrayBuffer, // new IntPtr(sizeof(uint) * indicesVboData.Length), // indicesVboData, BufferUsageHint.StaticDraw); // GL.BindBuffer(BufferTarget.ArrayBuffer, 0); // GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); }
public UniformBuffer(BufferStorageFlags storage_flags, int index, EngineHelper.size[] ubo_stack) { // Calculate total UBO byte size based on ubo_stack items _ubo_stack = ubo_stack; _ubo_stack_offets = new int[ubo_stack.Length]; int size = 0; for (int i = 0; i < ubo_stack.Length; i++) { _ubo_stack_offets[i] = size; size += (int)ubo_stack[i]; } load(storage_flags, index, size); }
public UniformBuffer(BufferStorageFlags storage_flags, int index, EngineHelper.size ubo_item_size, int length) { // Calculate total UBO byte size based on ubo_stack items _ubo_stack = new EngineHelper.size[length]; _ubo_stack_offets = new int[length]; int size = (int)ubo_item_size * length; int size_increment = 0; for (int i = 0; i < length; i++) { _ubo_stack[i] = ubo_item_size; _ubo_stack_offets[i] = size_increment; size_increment += (int)ubo_item_size; } load(storage_flags, index, size); }
protected unsafe void Allocate <T>(T[] data, BufferUsageHint bufferUsageHint, bool immutable = true, BufferStorageFlags extraFlags = BufferStorageFlags.None) where T : unmanaged { if (data == null) { throw new ArgumentNullException(nameof(data)); } SizeInBytes = data.Length * sizeof(T); Count = data.Length; IsImmutable = immutable; InitializeBuffer(data, bufferUsageHint, extraFlags); }
void CreateMappableBuffer() { // int localBuffer = GL.GenBuffer (); // GL.BindBuffer( BufferTarget.ElementArrayBuffer, localBuffer); // var indices = new ushort[] { 0, 1, 2, 1, 2, 3 }; // // var bufferSize = (IntPtr)(indices.Length * sizeof(ushort)); // GL.BufferData (BufferTarget.ElementArrayBuffer, bufferSize, IntPtr.Zero, BufferUsageHint.DynamicDraw); // GL.Arb.BufferStorage( // // IntPtr VideoMemoryIntPtr = GL.Ext.MapNamedBufferRange(localBuffer, IntPtr.Zero, bufferSize, BufferAccessMask.MapWriteBit); // GL.Ext.UnmapNamedBuffer(localBuffer); // // GL.DeleteBuffer (localBuffer); var buffers = new int[1]; // ARB_direct_state_access // Allows buffer objects to be initialised without binding them GL.CreateBuffers(1, buffers); var targetType = BufferTarget.ElementArrayBuffer; //GL.BindBuffer (targetType, buffers[0]); var bufferSize = (indices.Length * sizeof(ushort)); BufferStorageFlags flags = BufferStorageFlags.MapCoherentBit; GL.NamedBufferStorage(buffers[0], bufferSize, IntPtr.Zero, BufferStorageFlags.MapWriteBit | BufferStorageFlags.MapPersistentBit | BufferStorageFlags.MapCoherentBit); var error = GL.GetError(); Debug.WriteLineIf(error != ErrorCode.NoError, "NamedBufferStorage " + error); BufferAccessMask rangeFlags = BufferAccessMask.MapWriteBit | BufferAccessMask.MapPersistentBit | BufferAccessMask.MapCoherentBit; IntPtr Handle = GL.MapNamedBufferRange(buffers[0], IntPtr.Zero, bufferSize, rangeFlags); error = GL.GetError(); Debug.WriteLineIf(error != ErrorCode.NoError, "MapNamedBufferRange " + error); var result = GL.Ext.UnmapNamedBuffer(buffers[0]); error = GL.GetError(); Debug.WriteLineIf(error != ErrorCode.NoError, "UnmapNamedBuffer " + error); GL.DeleteBuffer(buffers[0]); }
public static unsafe VertexBuffer Create <T>(RenderContext renderContext, int capacity, BufferUsageHint bufferUsageHint, bool immutable, BufferStorageFlags flags = BufferStorageFlags.None) where T : unmanaged { if (renderContext == null) { throw new ArgumentNullException(nameof(renderContext)); } var vbo = new VertexBuffer(renderContext, VertexTypeInfo.Get <T>()); vbo.Allocate <T>(capacity * sizeof(T), bufferUsageHint, immutable, flags); return(vbo); }
public GraphicsBuffer( BufferTarget target, BufferStorageFlags storageFlags = BufferStorageFlags.MapReadBit | BufferStorageFlags.MapWriteBit | BufferStorageFlags.DynamicStorageBit | BufferStorageFlags.MapCoherentBit | BufferStorageFlags.MapPersistentBit, BufferAccessMask accessMask = BufferAccessMask.MapReadBit | BufferAccessMask.MapWriteBit | BufferAccessMask.MapCoherentBit | BufferAccessMask.MapPersistentBit) { Target = target; StorageFlags = storageFlags; AccessMask = accessMask; Cursor = 0; Length = DefaultLength; Allocate(); }
public GLDeviceMemory(MgMemoryAllocateInfo pAllocateInfo, IGLErrorHandler errHandler) { mErrHandler = errHandler; mBufferType = (GLMemoryBufferType)pAllocateInfo.MemoryTypeIndex; mIsHostCached = (mBufferType == GLMemoryBufferType.INDIRECT || mBufferType == GLMemoryBufferType.IMAGE); if (pAllocateInfo.AllocationSize > (ulong)int.MaxValue) { throw new ArgumentOutOfRangeException("pAllocateInfo.AllocationSize > int.MaxValue"); } mBufferSize = (int)pAllocateInfo.AllocationSize; if (mBufferType != GLMemoryBufferType.IMAGE) { mBufferTarget = GetBufferTarget(mBufferType); } if (mIsHostCached || pAllocateInfo.MemoryTypeIndex == (uint)GLMemoryBufferType.IMAGE) { mHandle = Marshal.AllocHGlobal(mBufferSize); } else { if (mBufferTarget.HasValue) { var buffers = new uint[1]; // ARB_direct_state_access // Allows buffer objects to be initialised without binding them GL.CreateBuffers(1, buffers); mErrHandler.LogGLError("GL.CreateBuffers"); mBufferId = buffers[0]; // TODO : update flags based on buffer request BufferStorageFlags flags = BufferStorageFlags.MapWriteBit | BufferStorageFlags.MapPersistentBit | BufferStorageFlags.MapCoherentBit; GL.NamedBufferStorage(mBufferId, mBufferSize, IntPtr.Zero, flags); mErrHandler.LogGLError("GL.NamedBufferStorage"); // BufferAccessMask rangeFlags = BufferAccessMask.MapWriteBit | BufferAccessMask.MapPersistentBit | BufferAccessMask.MapCoherentBit; // Handle = GL.MapNamedBufferRange (buffers[0], (IntPtr)0, BufferSize, rangeFlags); } } }
protected unsafe void Allocate <T>(int sizeInBytes, BufferUsageHint bufferUsageHint, bool immutable = true, BufferStorageFlags extraFlags = BufferStorageFlags.None) where T : unmanaged { SizeInBytes = sizeInBytes; IsImmutable = immutable; float capacity = sizeInBytes / (float)sizeof(T); if (!(Math.Abs(capacity - (int)capacity) < float.Epsilon)) { throw new ArgumentOutOfRangeException(nameof(sizeInBytes)); } Count = (int)capacity; InitializeBuffer <T>(null, bufferUsageHint, extraFlags); }
private void InitializeBuffer <T>(T[] data, BufferUsageHint bufferUsageHint, BufferStorageFlags storageFlags) where T : unmanaged { if (GLInfo.HasDirectStateAccess) { if (IsImmutable && GLInfo.HasBufferStorage) { GL.NamedBufferStorage(Handle, SizeInBytes, data, BufferUsageToStorageFlags(bufferUsageHint) | storageFlags); } else { GL.NamedBufferData(Handle, SizeInBytes, data, bufferUsageHint); } } else { Bind(ParentContext); if (IsImmutable && GLInfo.HasBufferStorage) { GL.BufferStorage(BufferTarget, SizeInBytes, data, BufferUsageToStorageFlags(bufferUsageHint) | storageFlags); } else { GL.BufferData(BufferTarget, SizeInBytes, data, bufferUsageHint); } } }
public UniformBuffer(BufferStorageFlags storage_flags, int index, EngineHelper.size[] ubo_sub_stack, int length) { // Calculate total UBO byte size based on ubo_stack items int stack_length = ubo_sub_stack.Length * length; _ubo_stack = new EngineHelper.size[stack_length]; _ubo_stack_offets = new int[stack_length]; int size = 0; for (int i = 0; i < length; i++) { for (int j = 0; j < ubo_sub_stack.Length; j++) { int stack_index = i * ubo_sub_stack.Length + j; _ubo_stack[stack_index] = ubo_sub_stack[j]; _ubo_stack_offets[stack_index] = size; size += (int)ubo_sub_stack[j]; } } load(storage_flags, index, size); }
/// <summary> /// Generate an empty buffer /// </summary> /// <param name="name"></param> /// <param name="length"></param> /// <param name="flags"></param> /// <param name="data"></param> /// <returns>The new buffer</returns> public static unsafe Buffer <T> Create( string name, long length, BufferStorageFlags flags = BufferStorageFlags.None, IntPtr data = default ) { return(new Buffer <T>(GL.GenBuffer(), name, (IntPtr)(TypeInfo <T> .TypeSize * length), flags, data)); }
//ARB_buffer_storage /// <summary> /// Allocates immutable storage for buffer bound to specified target. /// </summary> /// <param name="target">Target containging a buffer to allocate immutable storage for.s</param> /// <param name="size">Size in bytes of allocates storage.</param> /// <param name="data">Data to upload to buffer or zero to just allocate.</param> /// <param name="flags">Buffer Allocation Flags.</param> public static void BufferStorage(BufferTarget target, IntPtr size, IntPtr data, BufferStorageFlags flags) { Delegates.glBufferStorage(target, size, data, flags); }
/// <summary> /// Creates an OpenGL buffer object with immutable storage using GL.BufferStorage /// Note: changes buffer binding of the "target" BufferTarget parameter. /// </summary> /// <param name="target">BufferTarget for the new buffer</param> /// <param name="length">Size of the new buffer in bytes</param> /// <param name="contents">Data that will be copied to the new buffer</param> /// <param name="flags">BufferStorageFlags for the new buffer</param> /// <param name="name">Debug label of the new buffer</param> /// <returns>Handle of the new buffer</returns> public static int CreateBuffer <T>(BufferTarget target, IntPtr length, T[] contents, BufferStorageFlags flags, string name) where T : struct { int buffer = GL.GenBuffer(); GL.BindBuffer(target, buffer); GL.BufferStorage(target, length, contents, flags); SetObjectLabel(ObjectLabelIdentifier.Buffer, buffer, name); GL.BindBuffer(target, 0); return(buffer); }
//ARB_buffer_storage /// <summary> /// Allocates a buffer with immutable storage. /// </summary> /// <param name="buffer">Buffer id to allocate storage for.</param> /// <param name="size">Size in bytes of buffer.</param> /// <param name="data">Pointer to the data to upload or null.</param> /// <param name="flags">Buffer Allocation Flags.</param> public static void NamedBufferStorageEXT(uint buffer, long size, IntPtr data, BufferStorageFlags flags) { Delegates.glNamedBufferStorageEXT(buffer, (IntPtr)size, data, flags); }
//ARB_buffer_storage /// <summary> /// Allocates immutable storage for buffer bound to specified target. /// </summary> /// <param name="target">Target containging a buffer to allocate immutable storage for.s</param> /// <param name="size">Size in bytes of allocates storage.</param> /// <param name="data">Data to upload to buffer or zero to just allocate.</param> /// <param name="flags">Buffer Allocation Flags.</param> public static void BufferStorage(BufferTarget target, IntPtr size, IntPtr data, BufferStorageFlags flags) { Delegates.glBufferStorage(target, size, data, flags); }
public void storage(IntPtr size, IntPtr data, BufferStorageFlags flags) { GL.BufferStorage(_target, size, data, flags); }
//ARB_buffer_storage /// <summary> /// Allocates a buffer with immutable storage. /// </summary> /// <param name="buffer">Buffer id to allocate storage for.</param> /// <param name="size">Size in bytes of buffer.</param> /// <param name="data">Pointer to the data to upload or null.</param> /// <param name="flags">Buffer Allocation Flags.</param> public static void NamedBufferStorageEXT(uint buffer, long size, IntPtr data, BufferStorageFlags flags) { Delegates.glNamedBufferStorageEXT(buffer, (IntPtr)size, data, flags); }