Exemplo n.º 1
0
        public void SetVertexArray(int Stride, long VboKey, GalVertexAttrib[] Attribs)
        {
            if (!VboCache.TryGetValue(VboKey, out int VboHandle))
            {
                return;
            }

            if (VaoHandle == 0)
            {
                VaoHandle = GL.GenVertexArray();
            }

            GL.BindVertexArray(VaoHandle);

            foreach (GalVertexAttrib Attrib in Attribs)
            {
                GL.EnableVertexAttribArray(Attrib.Index);

                GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle);

                bool Unsigned =
                    Attrib.Type == GalVertexAttribType.Unorm ||
                    Attrib.Type == GalVertexAttribType.Uint ||
                    Attrib.Type == GalVertexAttribType.Uscaled;

                bool Normalize =
                    Attrib.Type == GalVertexAttribType.Snorm ||
                    Attrib.Type == GalVertexAttribType.Unorm;

                VertexAttribPointerType Type = 0;

                if (Attrib.Type == GalVertexAttribType.Float)
                {
                    Type = VertexAttribPointerType.Float;
                }
                else
                {
                    Type = AttribTypes[Attrib.Size] + (Unsigned ? 1 : 0);
                }

                int Size   = AttribElements[Attrib.Size];
                int Offset = Attrib.Offset;

                if (Attrib.Type == GalVertexAttribType.Sint ||
                    Attrib.Type == GalVertexAttribType.Uint)
                {
                    IntPtr Pointer = new IntPtr(Offset);

                    VertexAttribIntegerType IType = (VertexAttribIntegerType)Type;

                    GL.VertexAttribIPointer(Attrib.Index, Size, IType, Stride, Pointer);
                }
                else
                {
                    GL.VertexAttribPointer(Attrib.Index, Size, Type, Normalize, Stride, Offset);
                }
            }
        }
Exemplo n.º 2
0
        public void SetVertexAttributes(ReadOnlySpan <VertexAttribDescriptor> vertexAttribs)
        {
            int index = 0;

            for (; index < vertexAttribs.Length; index++)
            {
                VertexAttribDescriptor attrib = vertexAttribs[index];

                if (attrib.Equals(_vertexAttribs[index]))
                {
                    continue;
                }

                FormatInfo fmtInfo = FormatTable.GetFormatInfo(attrib.Format);

                if (attrib.IsZero)
                {
                    // Disabling the attribute causes the shader to read a constant value.
                    // The value is configurable, but by default is a vector of (0, 0, 0, 1).
                    DisableVertexAttrib(index);
                }
                else
                {
                    EnableVertexAttrib(index);
                }

                int offset = attrib.Offset;
                int size   = fmtInfo.Components;

                bool isFloat = fmtInfo.PixelType == PixelType.Float ||
                               fmtInfo.PixelType == PixelType.HalfFloat;

                if (isFloat || fmtInfo.Normalized || fmtInfo.Scaled)
                {
                    VertexAttribType type = (VertexAttribType)fmtInfo.PixelType;

                    GL.VertexAttribFormat(index, size, type, fmtInfo.Normalized, offset);
                }
                else
                {
                    VertexAttribIntegerType type = (VertexAttribIntegerType)fmtInfo.PixelType;

                    GL.VertexAttribIFormat(index, size, type, offset);
                }

                GL.VertexAttribBinding(index, attrib.BufferIndex);

                _vertexAttribs[index] = attrib;
            }

            _vertexAttribsCount = index;

            for (; index < Constants.MaxVertexAttribs; index++)
            {
                DisableVertexAttrib(index);
            }
        }
Exemplo n.º 3
0
 public VertexAttribute(string name, int size, VertexAttribIntegerType integerType,
                        int stride, int offset)
 {
     _name        = name;
     _size        = size;
     _integerType = integerType;
     _stride      = stride;
     _offset      = offset;
     _isInteger   = true;
 }
Exemplo n.º 4
0
        public void SetVertexAttributes(VertexAttribDescriptor[] vertexAttribs)
        {
            int attribIndex = 0;

            foreach (VertexAttribDescriptor attrib in vertexAttribs)
            {
                FormatInfo fmtInfo = FormatTable.GetFormatInfo(attrib.Format);

                if (attrib.IsZero)
                {
                    // Disabling the attribute causes the shader to read a constant value.
                    // The value is configurable, but by default is a vector of (0, 0, 0, 1).
                    GL.DisableVertexAttribArray(attribIndex);
                }
                else
                {
                    GL.EnableVertexAttribArray(attribIndex);
                }

                int offset = attrib.Offset;
                int size   = fmtInfo.Components;

                bool isFloat = fmtInfo.PixelType == PixelType.Float ||
                               fmtInfo.PixelType == PixelType.HalfFloat;

                if (isFloat || fmtInfo.Normalized || fmtInfo.Scaled)
                {
                    VertexAttribType type = (VertexAttribType)fmtInfo.PixelType;

                    GL.VertexAttribFormat(attribIndex, size, type, fmtInfo.Normalized, offset);
                }
                else
                {
                    VertexAttribIntegerType type = (VertexAttribIntegerType)fmtInfo.PixelType;

                    GL.VertexAttribIFormat(attribIndex, size, type, offset);
                }

                GL.VertexAttribBinding(attribIndex, attrib.BufferIndex);

                attribIndex++;
            }

            for (; attribIndex < 16; attribIndex++)
            {
                GL.DisableVertexAttribArray(attribIndex);
            }

            _vertexAttribs = vertexAttribs;
        }
Exemplo n.º 5
0
            private static int GetSize(VertexAttribIntegerType type)
            {
                switch (type)
                {
                case VertexAttribIntegerType.Byte:
                case VertexAttribIntegerType.UnsignedByte:
                    return(1);

                case VertexAttribIntegerType.Short:
                case VertexAttribIntegerType.UnsignedShort:
                    return(2);

                case VertexAttribIntegerType.Int:
                case VertexAttribIntegerType.UnsignedInt:
                    return(4);

                default:
                    throw new ArgumentException();
                }
            }
Exemplo n.º 6
0
        public void SetVertexAttributes(VertexAttribDescriptor[] vertexAttribs)
        {
            int attribIndex = 0;

            foreach (VertexAttribDescriptor attrib in vertexAttribs)
            {
                FormatInfo fmtInfo = FormatTable.GetFormatInfo(attrib.Format);

                GL.EnableVertexAttribArray(attribIndex);

                int offset = attrib.Offset;
                int size   = fmtInfo.Components;

                bool isFloat = fmtInfo.PixelType == PixelType.Float ||
                               fmtInfo.PixelType == PixelType.HalfFloat;

                if (isFloat || fmtInfo.Normalized || fmtInfo.Scaled)
                {
                    VertexAttribType type = (VertexAttribType)fmtInfo.PixelType;

                    GL.VertexAttribFormat(attribIndex, size, type, fmtInfo.Normalized, offset);
                }
                else
                {
                    VertexAttribIntegerType type = (VertexAttribIntegerType)fmtInfo.PixelType;

                    GL.VertexAttribIFormat(attribIndex, size, type, offset);
                }

                GL.VertexAttribBinding(attribIndex, attrib.BufferIndex);

                attribIndex++;
            }

            for (; attribIndex < 16; attribIndex++)
            {
                GL.DisableVertexAttribArray(attribIndex);
            }

            _vertexAttribs = vertexAttribs;
        }
Exemplo n.º 7
0
        public void EnableAttribI(
            VboPosition id, int attribArrayIdx = -1,
            VertexAttribIntegerType type       = VertexAttribIntegerType.Int,
            int stride = 0, int offset = 0)
        {
            if (attribArrayIdx < 0)
            {
                attribArrayIdx = (int)id;
            }


            VboBase vboBase = this[id];

            GL.BindVertexArray(Handle);
            vboBase.Bind();

            GL.EnableVertexAttribArray(attribArrayIdx);
            GL.VertexAttribIPointer(attribArrayIdx, vboBase.ElementSize, type, stride, new IntPtr(offset));

            //GL.BindVertexArray(0);
            //VboBase.Unbind();
        }
Exemplo n.º 8
0
        private void BindVertexLayout(GalPipelineState New)
        {
            foreach (GalVertexBinding Binding in New.VertexBindings)
            {
                if (!Binding.Enabled || !Rasterizer.TryGetVbo(Binding.VboKey, out int VboHandle))
                {
                    continue;
                }

                if (VaoHandle == 0)
                {
                    VaoHandle = GL.GenVertexArray();

                    //Vertex arrays shouldn't be used anywhere else in OpenGL's backend
                    //if you want to use it, move this line out of the if
                    GL.BindVertexArray(VaoHandle);
                }

                foreach (GalVertexAttrib Attrib in Binding.Attribs)
                {
                    //Skip uninitialized attributes.
                    if (Attrib.Size == 0)
                    {
                        continue;
                    }

                    GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle);

                    bool Unsigned =
                        Attrib.Type == GalVertexAttribType.Unorm ||
                        Attrib.Type == GalVertexAttribType.Uint ||
                        Attrib.Type == GalVertexAttribType.Uscaled;

                    bool Normalize =
                        Attrib.Type == GalVertexAttribType.Snorm ||
                        Attrib.Type == GalVertexAttribType.Unorm;

                    VertexAttribPointerType Type = 0;

                    if (Attrib.Type == GalVertexAttribType.Float)
                    {
                        Type = GetType(FloatAttribTypes, Attrib);
                    }
                    else
                    {
                        if (Unsigned)
                        {
                            Type = GetType(UnsignedAttribTypes, Attrib);
                        }
                        else
                        {
                            Type = GetType(SignedAttribTypes, Attrib);
                        }
                    }

                    if (!AttribElements.TryGetValue(Attrib.Size, out int Size))
                    {
                        throw new InvalidOperationException("Invalid attribute size \"" + Attrib.Size + "\"!");
                    }

                    int Offset = Attrib.Offset;

                    if (Binding.Stride != 0)
                    {
                        GL.EnableVertexAttribArray(Attrib.Index);

                        if (Attrib.Type == GalVertexAttribType.Sint ||
                            Attrib.Type == GalVertexAttribType.Uint)
                        {
                            IntPtr Pointer = new IntPtr(Offset);

                            VertexAttribIntegerType IType = (VertexAttribIntegerType)Type;

                            GL.VertexAttribIPointer(Attrib.Index, Size, IType, Binding.Stride, Pointer);
                        }
                        else
                        {
                            GL.VertexAttribPointer(Attrib.Index, Size, Type, Normalize, Binding.Stride, Offset);
                        }
                    }
                    else
                    {
                        GL.DisableVertexAttribArray(Attrib.Index);

                        SetConstAttrib(Attrib);
                    }

                    if (Binding.Instanced && Binding.Divisor != 0)
                    {
                        GL.VertexAttribDivisor(Attrib.Index, 1);
                    }
                    else
                    {
                        GL.VertexAttribDivisor(Attrib.Index, 0);
                    }
                }
            }
        }
Exemplo n.º 9
0
        private void BindVertexLayout(GalPipelineState New)
        {
            foreach (GalVertexBinding Binding in New.VertexBindings)
            {
                if (!Binding.Enabled || !Rasterizer.TryGetVbo(Binding.VboKey, out int VboHandle))
                {
                    continue;
                }

                if (VaoHandle == 0)
                {
                    VaoHandle = GL.GenVertexArray();

                    //Vertex arrays shouldn't be used anywhere else in OpenGL's backend
                    //if you want to use it, move this line out of the if
                    GL.BindVertexArray(VaoHandle);
                }

                foreach (GalVertexAttrib Attrib in Binding.Attribs)
                {
                    GL.EnableVertexAttribArray(Attrib.Index);

                    GL.BindBuffer(BufferTarget.ArrayBuffer, VboHandle);

                    bool Unsigned =
                        Attrib.Type == GalVertexAttribType.Unorm ||
                        Attrib.Type == GalVertexAttribType.Uint ||
                        Attrib.Type == GalVertexAttribType.Uscaled;

                    bool Normalize =
                        Attrib.Type == GalVertexAttribType.Snorm ||
                        Attrib.Type == GalVertexAttribType.Unorm;

                    VertexAttribPointerType Type = 0;

                    if (Attrib.Type == GalVertexAttribType.Float)
                    {
                        Type = VertexAttribPointerType.Float;
                    }
                    else
                    {
                        Type = AttribTypes[Attrib.Size] + (Unsigned ? 1 : 0);
                    }

                    int Size   = AttribElements[Attrib.Size];
                    int Offset = Attrib.Offset;

                    if (Attrib.Type == GalVertexAttribType.Sint ||
                        Attrib.Type == GalVertexAttribType.Uint)
                    {
                        IntPtr Pointer = new IntPtr(Offset);

                        VertexAttribIntegerType IType = (VertexAttribIntegerType)Type;

                        GL.VertexAttribIPointer(Attrib.Index, Size, IType, Binding.Stride, Pointer);
                    }
                    else
                    {
                        GL.VertexAttribPointer(Attrib.Index, Size, Type, Normalize, Binding.Stride, Offset);
                    }
                }
            }
        }
Exemplo n.º 10
0
        private void BindVertexLayout(GalPipelineState New)
        {
            foreach (GalVertexBinding binding in New.VertexBindings)
            {
                if (!binding.Enabled || !_rasterizer.TryGetVbo(binding.VboKey, out int vboHandle))
                {
                    continue;
                }

                if (_vaoHandle == 0)
                {
                    _vaoHandle = GL.GenVertexArray();

                    // Vertex arrays shouldn't be used anywhere else in OpenGL's backend
                    // if you want to use it, move this line out of the if
                    GL.BindVertexArray(_vaoHandle);
                }

                foreach (GalVertexAttrib attrib in binding.Attribs)
                {
                    // Skip uninitialized attributes.
                    if (attrib.Size == 0)
                    {
                        continue;
                    }

                    GL.BindBuffer(BufferTarget.ArrayBuffer, vboHandle);

                    bool unsigned =
                        attrib.Type == GalVertexAttribType.Unorm ||
                        attrib.Type == GalVertexAttribType.Uint ||
                        attrib.Type == GalVertexAttribType.Uscaled;

                    bool normalize =
                        attrib.Type == GalVertexAttribType.Snorm ||
                        attrib.Type == GalVertexAttribType.Unorm;

                    VertexAttribPointerType type = 0;

                    if (attrib.Type == GalVertexAttribType.Float)
                    {
                        type = GetType(_floatAttribTypes, attrib);
                    }
                    else
                    {
                        if (unsigned)
                        {
                            type = GetType(_unsignedAttribTypes, attrib);
                        }
                        else
                        {
                            type = GetType(_signedAttribTypes, attrib);
                        }
                    }

                    if (!_attribElements.TryGetValue(attrib.Size, out int size))
                    {
                        throw new InvalidOperationException("Invalid attribute size \"" + attrib.Size + "\"!");
                    }

                    int offset = attrib.Offset;

                    if (binding.Stride != 0)
                    {
                        GL.EnableVertexAttribArray(attrib.Index);

                        if (attrib.Type == GalVertexAttribType.Sint ||
                            attrib.Type == GalVertexAttribType.Uint)
                        {
                            IntPtr pointer = new IntPtr(offset);

                            VertexAttribIntegerType iType = (VertexAttribIntegerType)type;

                            GL.VertexAttribIPointer(attrib.Index, size, iType, binding.Stride, pointer);
                        }
                        else
                        {
                            GL.VertexAttribPointer(attrib.Index, size, type, normalize, binding.Stride, offset);
                        }
                    }
                    else
                    {
                        GL.DisableVertexAttribArray(attrib.Index);

                        SetConstAttrib(attrib);
                    }

                    if (binding.Instanced && binding.Divisor != 0)
                    {
                        GL.VertexAttribDivisor(attrib.Index, 1);
                    }
                    else
                    {
                        GL.VertexAttribDivisor(attrib.Index, 0);
                    }
                }
            }
        }
Exemplo n.º 11
0
        public void EnableAttribI(
            VboPosition id, int attribArrayIdx = -1,
            VertexAttribIntegerType type = VertexAttribIntegerType.Int,
            int stride = 0, int offset = 0)
        {
            if (attribArrayIdx < 0)
                attribArrayIdx = (int)id;


            VboBase vboBase = this[id];

            GL.BindVertexArray(Handle);
            vboBase.Bind();

            GL.EnableVertexAttribArray(attribArrayIdx);
            GL.VertexAttribIPointer(attribArrayIdx, vboBase.ElementSize, type, stride, new IntPtr(offset));

            //GL.BindVertexArray(0);
            //VboBase.Unbind();
        }
Exemplo n.º 12
0
 /// <summary>
 /// Creates a new vertex attribute.
 /// </summary>
 /// <param name="name">The name of the attribute in the shader</param>
 /// <param name="valueCount">The number of components for the value</param>
 /// <param name="type">The data type of the value</param>
 /// <exception cref="System.NotSupportedException"><paramref name="type"/> is not
 /// a supported attribute type.</exception>
 public VertexIntAttribute(string name, ValueCount valueCount, VertexAttribIntegerType type)
     : base(name, valueCount, (VertexAttribPointerType)type)
 {
     // The default attribute pointer type enum contains all the integer values.
     SizeInBytes = (int)valueCount * AttribPointerUtils.GetSizeInBytes(type);
 }
Exemplo n.º 13
0
 public void VertexAttribArray(int index, int count, VertexAttribIntegerType type, int stride = 0,
                               int offset = 0)
 {
     GL.VertexAttribIPointer(index, count, type, stride, (IntPtr)offset);
     GL.EnableVertexAttribArray(index);
 }
Exemplo n.º 14
0
 /// <summary>
 /// Returns the size in bytes of the C# equivalent for a specified OpenGL attribute pointer type.
 /// </summary>
 /// <returns>The size of <paramref name="type"/> in bytes</returns>
 /// <exception cref="System.NotImplementedException">The size of <paramref name="type"/> is not implemented</exception>
 public static int GetSizeInBytes(VertexAttribIntegerType type)
 {
     return(GetSizeInBytes((VertexAttribPointerType)type));
 }
Exemplo n.º 15
0
 public void LayoutAddInt(int count, VertexAttribIntegerType type)
 => _attribs.Add(new AttribEntry()
 {
     entryType = AttribEntry.AttribEntryType.Int, count = count, intType = type
 });