Buffer object that not work as input variable in shader.
Inheritance: Buffer
Ejemplo n.º 1
0
        /// <summary>
        /// Initialize and setup uniform block's value.
        /// </summary>
        /// <param name="program"></param>
        /// <returns></returns>
        private UniformBuffer Initialize(ShaderProgram program)
        {
            if (glGetUniformBlockIndex == null)
            {
                glGetUniformBlockIndex    = GL.Instance.GetDelegateFor("glGetUniformBlockIndex", GLDelegates.typeof_uint_uint_string) as GLDelegates.uint_uint_string;
                glGetActiveUniformBlockiv = GL.Instance.GetDelegateFor("glGetActiveUniformBlockiv", GLDelegates.typeof_uint_uint_uint_uint_uintN) as GLDelegates.uint_uint_uint_uint_uintN;
            }

            uint uboIndex = glGetUniformBlockIndex(program.ProgramId, this.VarName);
            var  uboSize  = new uint[1];

            glGetActiveUniformBlockiv(program.ProgramId, uboIndex, GL.GL_UNIFORM_BLOCK_DATA_SIZE, uboSize);
            byte[]        bytes  = this.value.ToBytes();
            UniformBuffer result = bytes.GenUniformBuffer(BufferUsage.StaticDraw);

            // another way to do this:
            //UniformBuffer result = UniformBuffer.Create(typeof(byte), bytes.Length, BufferUsage.StaticDraw);
            //unsafe
            //{
            //    var array = (byte*)result.MapBuffer(MapBufferAccess.WriteOnly);
            //    for (int i = 0; i < bytes.Length; i++)
            //    {
            //        array[i] = bytes[i];
            //    }
            //    result.UnmapBuffer();
            //}

            //glBindBufferBase(GL.GL_UNIFORM_BUFFER, uboIndex, result.BufferId);
            //glBindBufferBase(GL.GL_UNIFORM_BUFFER, 0, result.BufferId);
            //glUniformBlockBinding(program.ProgramId, uboIndex, 0);
            result.Binding(program, uboIndex, 0);
            result.Unbind();

            return(result);
        }
Ejemplo n.º 2
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);
        }
        ///// <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);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Initialize and setup uniform block's value.
        /// </summary>
        /// <param name="program"></param>
        /// <returns></returns>
        private UniformBufferPtr Initialize(ShaderProgram program)
        {
            if (glGetUniformBlockIndex == null)
            {
                glGetUniformBlockIndex    = OpenGL.GetDelegateFor <OpenGL.glGetUniformBlockIndex>();
                glGetActiveUniformBlockiv = OpenGL.GetDelegateFor <OpenGL.glGetActiveUniformBlockiv>();
            }

            uint uboIndex = glGetUniformBlockIndex(program.ProgramId, this.VarName);
            var  uboSize  = new uint[1];

            glGetActiveUniformBlockiv(program.ProgramId, uboIndex, OpenGL.GL_UNIFORM_BLOCK_DATA_SIZE, uboSize);
            UniformBufferPtr result = null;

            using (var buffer = new UniformBuffer <byte>(BufferUsage.StaticDraw, noDataCopyed: false))
            {
                byte[] bytes = this.value.ToBytes();
                buffer.Create(bytes.Length);
                unsafe
                {
                    var array = (byte *)buffer.Header.ToPointer();
                    for (int i = 0; i < bytes.Length; i++)
                    {
                        array[i] = bytes[i];
                    }
                }

                result = buffer.GetBufferPtr() as UniformBufferPtr;
            }

            //glBindBufferBase(OpenGL.GL_UNIFORM_BUFFER, uboIndex, result.BufferId);
            //glBindBufferBase(OpenGL.GL_UNIFORM_BUFFER, 0, result.BufferId);
            //glUniformBlockBinding(program.ProgramId, uboIndex, 0);
            result.Binding(program, uboIndex, 0);
            result.Unbind();

            return(result);
        }
        ///// <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;
        }
Ejemplo n.º 7
0
        /// <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;
        }