public UniformBuffer(int size, int stride, Type type) { if (stride % 16 != 0) { throw new Exception("Must be aligned to minimum 16"); } this.stride = stride; this.size = size; storageType = type; ID = GL.GenBuffer(); GLBind.UniformBuffer(ID); GL.BufferData(GL.GL_UNIFORM_BUFFER, new IntPtr(size * stride), IntPtr.Zero, GL.GL_STREAM_DRAW); GL.GetIntegerv(GL.GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, out int align); var lval = stride % align; if (lval == 0) { gAlignment = 0; } else { gAlignment = align; } #if DEBUG if (align < 256 && 256 % align != 0) { gAlignment = 256; //Set larger alignment on debug for testing } #endif if (align < stride && lval != 0) { throw new Exception("Platform has incompatible alignment"); } }
public UniformBuffer(int size, int stride, Type type) { this.stride = stride; this.size = size; storageType = type; ID = GL.GenBuffer(); GLBind.UniformBuffer(ID); GL.BufferData(GL.GL_UNIFORM_BUFFER, new IntPtr(size * stride), IntPtr.Zero, GL.GL_STREAM_DRAW); }
public void SetData <T>(T[] array, int start = 0, int length = -1) where T : struct { if (typeof(T) != storageType) { throw new InvalidOperationException(); } var len = length < 0 ? array.Length : length; GLBind.UniformBuffer(ID); var handle = GCHandle.Alloc(array, GCHandleType.Pinned); GL.BufferSubData(GL.GL_UNIFORM_BUFFER, (IntPtr)(start * stride), (IntPtr)(len * stride), handle.AddrOfPinnedObject()); handle.Free(); }