Example #1
0
        /// <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 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);
        }