/// <summary> /// Creates a sub-type of <see cref="GLBuffer"/> object directly in server side(GPU) without initializing its value. /// </summary> /// <param name="target"></param> /// <param name="elementType"></param> /// <param name="length"></param> /// <param name="usage"></param> /// <returns></returns> public static GLBuffer Create(IndependentBufferTarget target, Type elementType, int length, BufferUsage usage) { if (!elementType.IsValueType) { throw new ArgumentException(string.Format("{0} must be a value type!", elementType)); } uint bufferTarget = (uint)target; int byteLength = Marshal.SizeOf(elementType) * length; uint[] buffers = new uint[1]; glGenBuffers(1, buffers); glBindBuffer(bufferTarget, buffers[0]); glBufferData(bufferTarget, byteLength, IntPtr.Zero, (uint)usage); glBindBuffer(bufferTarget, 0); GLBuffer buffer; switch (target) { case IndependentBufferTarget.AtomicCounterBuffer: buffer = new AtomicCounterBuffer(buffers[0], length, byteLength); break; case IndependentBufferTarget.PixelPackBuffer: buffer = new PixelPackBuffer(buffers[0], length, byteLength); break; case IndependentBufferTarget.PixelUnpackBuffer: buffer = new PixelUnpackBuffer(buffers[0], length, byteLength); break; case IndependentBufferTarget.ShaderStorageBuffer: buffer = new ShaderStorageBuffer(buffers[0], length, byteLength); break; case IndependentBufferTarget.TextureBuffer: buffer = new TextureBuffer(buffers[0], length, byteLength); break; case IndependentBufferTarget.UniformBuffer: buffer = new UniformBuffer(buffers[0], length, byteLength); break; case IndependentBufferTarget.TransformFeedbackBuffer: buffer = new TransformFeedbackBuffer(buffers[0], length, byteLength); break; default: throw new Exception("Unexpected IndependentBufferTarget!"); } return(buffer); }
/// <summary> /// Generates an independent buffer. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="array"></param> /// <param name="target"></param> /// <param name="usage"></param> /// <returns></returns> private static GLBuffer GenIndependentBuffer <T>(this T[] array, IndependentBufferTarget target, BufferUsage usage) where T : struct { GCHandle pinned = GCHandle.Alloc(array, GCHandleType.Pinned); IntPtr header = Marshal.UnsafeAddrOfPinnedArrayElement(array, 0); UnmanagedArrayBase unmanagedArray = new TempUnmanagedArray <T>(header, array.Length);// It's not neecessary to call Dispose() for this unmanaged array. GLBuffer buffer = GenIndependentBuffer(unmanagedArray, target, usage); pinned.Free(); return(buffer); }
///// <summary> ///// Generates an atomic counter buffer. ///// </summary> ///// <param name="array"></param> ///// <param name="usage"></param> ///// <returns></returns> //public static AtomicCounterBuffer GenAtomicCounterBuffer(this UnmanagedArrayBase array, BufferUsage usage) //{ // return GenIndependentBuffer(array, IndependentBufferTarget.AtomicCounterBuffer, usage) as AtomicCounterBuffer; //} ///// <summary> ///// Generates a pixel pack buffer. ///// </summary> ///// <param name="array"></param> ///// <param name="usage"></param> ///// <returns></returns> //public static PixelPackBuffer GenPixelPackBuffer(this UnmanagedArrayBase array, BufferUsage usage) //{ // return GenIndependentBuffer(array, IndependentBufferTarget.PixelPackBuffer, usage) as PixelPackBuffer; //} ///// <summary> ///// Generates a pixel unpack buffer. ///// </summary> ///// <param name="array"></param> ///// <param name="usage"></param> ///// <returns></returns> //public static PixelUnpackBuffer GenPixelUnpackBuffer(this UnmanagedArrayBase array, BufferUsage usage) //{ // return GenIndependentBuffer(array, IndependentBufferTarget.PixelUnpackBuffer, usage) as PixelUnpackBuffer; //} ///// <summary> ///// Generates a shader storage buffer. ///// </summary> ///// <param name="array"></param> ///// <param name="usage"></param> ///// <returns></returns> //public static ShaderStorageBuffer GenShaderStorageBuffer(this UnmanagedArrayBase array, BufferUsage usage) //{ // return GenIndependentBuffer(array, IndependentBufferTarget.ShaderStorageBuffer, usage) as ShaderStorageBuffer; //} ///// <summary> ///// Generates a texture buffer. ///// </summary> ///// <param name="array"></param> ///// <param name="usage"></param> ///// <returns></returns> //public static TextureBuffer GenTextureBuffer(this UnmanagedArrayBase array, BufferUsage usage) //{ // return GenIndependentBuffer(array, IndependentBufferTarget.TextureBuffer, usage) as TextureBuffer; //} ///// <summary> ///// Generates an uniform buffer. ///// </summary> ///// <param name="array"></param> ///// <param name="usage"></param> ///// <returns></returns> //public static UniformBuffer GenUniformBuffer(this UnmanagedArrayBase array, BufferUsage usage) //{ // return GenIndependentBuffer(array, IndependentBufferTarget.UniformBuffer, usage) as UniformBuffer; //} /// <summary> /// Generates an independent buffer. /// </summary> /// <param name="array"></param> /// <param name="bufferTarget"></param> /// <param name="usage"></param> /// <returns></returns> private static GLBuffer GenIndependentBuffer(this UnmanagedArrayBase array, IndependentBufferTarget bufferTarget, BufferUsage usage) { uint[] ids = new uint[1]; { glGenBuffers(1, ids); var target = (uint)bufferTarget; glBindBuffer(target, ids[0]); glBufferData(target, array.ByteLength, array.Header, (uint)usage); glBindBuffer(target, 0); } GLBuffer buffer = null; switch (bufferTarget) { case IndependentBufferTarget.AtomicCounterBuffer: buffer = new AtomicCounterBuffer(ids[0], array.Length, array.ByteLength); break; case IndependentBufferTarget.PixelPackBuffer: buffer = new PixelPackBuffer(ids[0], array.Length, array.ByteLength); break; case IndependentBufferTarget.PixelUnpackBuffer: buffer = new PixelUnpackBuffer(ids[0], array.Length, array.ByteLength); break; case IndependentBufferTarget.ShaderStorageBuffer: buffer = new ShaderStorageBuffer(ids[0], array.Length, array.ByteLength); break; case IndependentBufferTarget.TextureBuffer: buffer = new TextureBuffer(ids[0], array.Length, array.ByteLength); break; case IndependentBufferTarget.UniformBuffer: buffer = new UniformBuffer(ids[0], array.Length, array.ByteLength); break; case IndependentBufferTarget.TransformFeedbackBuffer: buffer = new TransformFeedbackBuffer(ids[0], array.Length, array.ByteLength); break; default: throw new NotDealWithNewEnumItemException(typeof(IndependentBufferTarget)); } return(buffer); }
///// <summary> ///// Generates an atomic counter buffer. ///// </summary> ///// <param name="array"></param> ///// <param name="usage"></param> ///// <returns></returns> //public static AtomicCounterBuffer GenAtomicCounterBuffer(this UnmanagedArrayBase array, BufferUsage usage) //{ // return GenIndependentBuffer(array, IndependentBufferTarget.AtomicCounterBuffer, usage) as AtomicCounterBuffer; //} ///// <summary> ///// Generates a pixel pack buffer. ///// </summary> ///// <param name="array"></param> ///// <param name="usage"></param> ///// <returns></returns> //public static PixelPackBuffer GenPixelPackBuffer(this UnmanagedArrayBase array, BufferUsage usage) //{ // return GenIndependentBuffer(array, IndependentBufferTarget.PixelPackBuffer, usage) as PixelPackBuffer; //} ///// <summary> ///// Generates a pixel unpack buffer. ///// </summary> ///// <param name="array"></param> ///// <param name="usage"></param> ///// <returns></returns> //public static PixelUnpackBuffer GenPixelUnpackBuffer(this UnmanagedArrayBase array, BufferUsage usage) //{ // return GenIndependentBuffer(array, IndependentBufferTarget.PixelUnpackBuffer, usage) as PixelUnpackBuffer; //} ///// <summary> ///// Generates a shader storage buffer. ///// </summary> ///// <param name="array"></param> ///// <param name="usage"></param> ///// <returns></returns> //public static ShaderStorageBuffer GenShaderStorageBuffer(this UnmanagedArrayBase array, BufferUsage usage) //{ // return GenIndependentBuffer(array, IndependentBufferTarget.ShaderStorageBuffer, usage) as ShaderStorageBuffer; //} ///// <summary> ///// Generates a texture buffer. ///// </summary> ///// <param name="array"></param> ///// <param name="usage"></param> ///// <returns></returns> //public static TextureBuffer GenTextureBuffer(this UnmanagedArrayBase array, BufferUsage usage) //{ // return GenIndependentBuffer(array, IndependentBufferTarget.TextureBuffer, usage) as TextureBuffer; //} ///// <summary> ///// Generates an uniform buffer. ///// </summary> ///// <param name="array"></param> ///// <param name="usage"></param> ///// <returns></returns> //public static UniformBuffer GenUniformBuffer(this UnmanagedArrayBase array, BufferUsage usage) //{ // return GenIndependentBuffer(array, IndependentBufferTarget.UniformBuffer, usage) as UniformBuffer; //} /// <summary> /// Generates an independent buffer. /// </summary> /// <param name="array"></param> /// <param name="bufferTarget"></param> /// <param name="usage"></param> /// <returns></returns> private static GLBuffer GenIndependentBuffer(this UnmanagedArrayBase array, IndependentBufferTarget bufferTarget, BufferUsage usage) { if (glGenBuffers == null) { InitFunctions(); } uint[] buffers = new uint[1]; glGenBuffers(1, buffers); var target = (uint)bufferTarget; glBindBuffer(target, buffers[0]); glBufferData(target, array.ByteLength, array.Header, (uint)usage); glBindBuffer(target, 0); GLBuffer buffer = null; switch (bufferTarget) { case IndependentBufferTarget.AtomicCounterBuffer: buffer = new AtomicCounterBuffer(buffers[0], array.Length, array.ByteLength); break; case IndependentBufferTarget.PixelPackBuffer: buffer = new PixelPackBuffer(buffers[0], array.Length, array.ByteLength); break; case IndependentBufferTarget.PixelUnpackBuffer: buffer = new PixelUnpackBuffer(buffers[0], array.Length, array.ByteLength); break; case IndependentBufferTarget.ShaderStorageBuffer: buffer = new ShaderStorageBuffer(buffers[0], array.Length, array.ByteLength); break; case IndependentBufferTarget.TextureBuffer: buffer = new TextureBuffer(buffers[0], array.Length, array.ByteLength); break; case IndependentBufferTarget.UniformBuffer: buffer = new UniformBuffer(buffers[0], array.Length, array.ByteLength); break; default: throw new NotImplementedException(); } return(buffer); }
///// <summary> ///// Generates an atomic counter buffer. ///// </summary> ///// <param name="array"></param> ///// <param name="usage"></param> ///// <returns></returns> //public static AtomicCounterBuffer GenAtomicCounterBuffer(this UnmanagedArrayBase array, BufferUsage usage) //{ // return GenIndependentBuffer(array, IndependentBufferTarget.AtomicCounterBuffer, usage) as AtomicCounterBuffer; //} ///// <summary> ///// Generates a pixel pack buffer. ///// </summary> ///// <param name="array"></param> ///// <param name="usage"></param> ///// <returns></returns> //public static PixelPackBuffer GenPixelPackBuffer(this UnmanagedArrayBase array, BufferUsage usage) //{ // return GenIndependentBuffer(array, IndependentBufferTarget.PixelPackBuffer, usage) as PixelPackBuffer; //} ///// <summary> ///// Generates a pixel unpack buffer. ///// </summary> ///// <param name="array"></param> ///// <param name="usage"></param> ///// <returns></returns> //public static PixelUnpackBuffer GenPixelUnpackBuffer(this UnmanagedArrayBase array, BufferUsage usage) //{ // return GenIndependentBuffer(array, IndependentBufferTarget.PixelUnpackBuffer, usage) as PixelUnpackBuffer; //} ///// <summary> ///// Generates a shader storage buffer. ///// </summary> ///// <param name="array"></param> ///// <param name="usage"></param> ///// <returns></returns> //public static ShaderStorageBuffer GenShaderStorageBuffer(this UnmanagedArrayBase array, BufferUsage usage) //{ // return GenIndependentBuffer(array, IndependentBufferTarget.ShaderStorageBuffer, usage) as ShaderStorageBuffer; //} ///// <summary> ///// Generates a texture buffer. ///// </summary> ///// <param name="array"></param> ///// <param name="usage"></param> ///// <returns></returns> //public static TextureBuffer GenTextureBuffer(this UnmanagedArrayBase array, BufferUsage usage) //{ // return GenIndependentBuffer(array, IndependentBufferTarget.TextureBuffer, usage) as TextureBuffer; //} ///// <summary> ///// Generates an uniform buffer. ///// </summary> ///// <param name="array"></param> ///// <param name="usage"></param> ///// <returns></returns> //public static UniformBuffer GenUniformBuffer(this UnmanagedArrayBase array, BufferUsage usage) //{ // return GenIndependentBuffer(array, IndependentBufferTarget.UniformBuffer, usage) as UniformBuffer; //} /// <summary> /// Generates an independent buffer. /// </summary> /// <param name="array"></param> /// <param name="bufferTarget"></param> /// <param name="usage"></param> /// <returns></returns> private static Buffer GenIndependentBuffer(this UnmanagedArrayBase array, IndependentBufferTarget bufferTarget, BufferUsage usage) { if (glGenBuffers == null) { InitFunctions(); } uint[] buffers = new uint[1]; glGenBuffers(1, buffers); var target = (uint)bufferTarget; glBindBuffer(target, buffers[0]); glBufferData(target, array.ByteLength, array.Header, (uint)usage); glBindBuffer(target, 0); Buffer buffer = null; switch (bufferTarget) { case IndependentBufferTarget.AtomicCounterBuffer: buffer = new AtomicCounterBuffer(buffers[0], array.Length, array.ByteLength); break; case IndependentBufferTarget.PixelPackBuffer: buffer = new PixelPackBuffer(buffers[0], array.Length, array.ByteLength); break; case IndependentBufferTarget.PixelUnpackBuffer: buffer = new PixelUnpackBuffer(buffers[0], array.Length, array.ByteLength); break; case IndependentBufferTarget.ShaderStorageBuffer: buffer = new ShaderStorageBuffer(buffers[0], array.Length, array.ByteLength); break; case IndependentBufferTarget.TextureBuffer: buffer = new TextureBuffer(buffers[0], array.Length, array.ByteLength); break; case IndependentBufferTarget.UniformBuffer: buffer = new UniformBuffer(buffers[0], array.Length, array.ByteLength); break; default: throw new NotImplementedException(); } return buffer; }
/// <summary> /// Creates a sub-type of <see cref="Buffer"/> object directly in server side(GPU) without initializing its value. /// </summary> /// <param name="target"></param> /// <param name="elementType"></param> /// <param name="length"></param> /// <param name="usage"></param> /// <returns></returns> public static Buffer Create(IndependentBufferTarget target, Type elementType, int length, BufferUsage usage) { if (!elementType.IsValueType) { throw new ArgumentException(string.Format("{0} must be a value type!", elementType)); } if (glGenBuffers == null) { glGenBuffers = OpenGL.GetDelegateFor<OpenGL.glGenBuffers>(); } if (glBindBuffer == null) { glBindBuffer = OpenGL.GetDelegateFor<OpenGL.glBindBuffer>(); } if (glBufferData == null) { glBufferData = OpenGL.GetDelegateFor<OpenGL.glBufferData>(); } uint bufferTarget = (uint)target; int byteLength = Marshal.SizeOf(elementType) * length; uint[] buffers = new uint[1]; glGenBuffers(1, buffers); glBindBuffer(bufferTarget, buffers[0]); glBufferData(bufferTarget, byteLength, IntPtr.Zero, (uint)usage); glBindBuffer(bufferTarget, 0); Buffer buffer; switch (target) { case IndependentBufferTarget.AtomicCounterBuffer: buffer = new AtomicCounterBuffer(buffers[0], length, byteLength); break; case IndependentBufferTarget.PixelPackBuffer: buffer = new PixelPackBuffer(buffers[0], length, byteLength); break; case IndependentBufferTarget.PixelUnpackBuffer: buffer = new PixelUnpackBuffer(buffers[0], length, byteLength); break; case IndependentBufferTarget.ShaderStorageBuffer: buffer = new ShaderStorageBuffer(buffers[0], length, byteLength); break; case IndependentBufferTarget.TextureBuffer: buffer = new TextureBuffer(buffers[0], length, byteLength); break; case IndependentBufferTarget.UniformBuffer: buffer = new UniformBuffer(buffers[0], length, byteLength); break; default: throw new NotImplementedException(); } return buffer; }