Пример #1
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);
            }
        }
Пример #2
0
        private void CreateView()
        {
            TextureTarget target = Target.Convert();

            FormatInfo format = FormatTable.GetFormatInfo(_info.Format);

            PixelInternalFormat pixelInternalFormat;

            if (format.IsCompressed)
            {
                pixelInternalFormat = (PixelInternalFormat)format.PixelFormat;
            }
            else
            {
                pixelInternalFormat = format.PixelInternalFormat;
            }

            GL.TextureView(
                Handle,
                target,
                _parent.Handle,
                pixelInternalFormat,
                _firstLevel,
                _info.Levels,
                _firstLayer,
                _info.GetLayers());

            GL.ActiveTexture(TextureUnit.Texture0);

            GL.BindTexture(target, Handle);

            int[] swizzleRgba = new int[]
            {
                (int)_info.SwizzleR.Convert(),
                (int)_info.SwizzleG.Convert(),
                (int)_info.SwizzleB.Convert(),
                (int)_info.SwizzleA.Convert()
            };

            GL.TexParameter(target, TextureParameterName.TextureSwizzleRgba, swizzleRgba);

            int maxLevel = _info.Levels - 1;

            if (maxLevel < 0)
            {
                maxLevel = 0;
            }

            GL.TexParameter(target, TextureParameterName.TextureMaxLevel, maxLevel);

            // TODO: This requires ARB_stencil_texturing, we should uncomment and test this.
            // GL.TexParameter(target, TextureParameterName.DepthStencilTextureMode, (int)_info.DepthStencilMode.Convert());
        }
Пример #3
0
        public void SetImage(int unit, ITexture texture)
        {
            if (texture != null)
            {
                TextureView view = (TextureView)texture;

                FormatInfo formatInfo = FormatTable.GetFormatInfo(view.Format);

                SizedInternalFormat format = (SizedInternalFormat)formatInfo.PixelInternalFormat;

                GL.BindImageTexture(unit, view.Handle, 0, true, 0, TextureAccess.ReadWrite, format);
            }
        }
Пример #4
0
        private void CreateView()
        {
            TextureTarget target = Target.Convert();

            FormatInfo format = FormatTable.GetFormatInfo(Info.Format);

            PixelInternalFormat pixelInternalFormat;

            if (format.IsCompressed)
            {
                pixelInternalFormat = (PixelInternalFormat)format.PixelFormat;
            }
            else
            {
                pixelInternalFormat = format.PixelInternalFormat;
            }

            GL.TextureView(
                Handle,
                target,
                _parent.Handle,
                pixelInternalFormat,
                FirstLevel,
                Info.Levels,
                FirstLayer,
                Info.GetLayers());

            GL.ActiveTexture(TextureUnit.Texture0);

            GL.BindTexture(target, Handle);

            int[] swizzleRgba = new int[]
            {
                (int)Info.SwizzleR.Convert(),
                (int)Info.SwizzleG.Convert(),
                (int)Info.SwizzleB.Convert(),
                (int)Info.SwizzleA.Convert()
            };

            GL.TexParameter(target, TextureParameterName.TextureSwizzleRgba, swizzleRgba);

            int maxLevel = Info.Levels - 1;

            if (maxLevel < 0)
            {
                maxLevel = 0;
            }

            GL.TexParameter(target, TextureParameterName.TextureMaxLevel, maxLevel);
            GL.TexParameter(target, TextureParameterName.DepthStencilTextureMode, (int)Info.DepthStencilMode.Convert());
        }
Пример #5
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;
        }
Пример #6
0
        public void SetImage(int index, ShaderStage stage, ITexture texture)
        {
            int unit = _program.GetImageUnit(stage, index);

            if (unit != -1 && texture != null)
            {
                TextureView view = (TextureView)texture;

                FormatInfo formatInfo = FormatTable.GetFormatInfo(view.Format);

                SizedInternalFormat format = (SizedInternalFormat)formatInfo.PixelInternalFormat;

                GL.BindImageTexture(unit, view.Handle, 0, true, 0, TextureAccess.ReadWrite, format);
            }
        }
Пример #7
0
        public void SetStorage(BufferRange buffer)
        {
            if (buffer.Buffer == _buffer &&
                buffer.Offset == _bufferOffset &&
                buffer.Size == _bufferSize)
            {
                return;
            }

            _buffer       = (Buffer)buffer.Buffer;
            _bufferOffset = buffer.Offset;
            _bufferSize   = buffer.Size;

            Bind(0);

            SizedInternalFormat format = (SizedInternalFormat)FormatTable.GetFormatInfo(Info.Format).PixelInternalFormat;

            GL.TexBufferRange(TextureBufferTarget.TextureBuffer, format, _buffer.Handle, (IntPtr)buffer.Offset, buffer.Size);
        }
Пример #8
0
        private void WriteTo(IntPtr ptr)
        {
            TextureTarget target = Target.Convert();

            Bind(target, 0);

            FormatInfo format = FormatTable.GetFormatInfo(_info.Format);

            int faces = 1;

            if (target == TextureTarget.TextureCubeMap)
            {
                target = TextureTarget.TextureCubeMapPositiveX;

                faces = 6;
            }

            for (int level = 0; level < _info.Levels; level++)
            {
                for (int face = 0; face < faces; face++)
                {
                    int faceOffset = face * _info.GetMipSize2D(level);

                    if (format.IsCompressed)
                    {
                        GL.GetCompressedTexImage(target + face, level, ptr + faceOffset);
                    }
                    else
                    {
                        GL.GetTexImage(
                            target + face,
                            level,
                            format.PixelFormat,
                            format.PixelType,
                            ptr + faceOffset);
                    }
                }

                ptr += _info.GetMipSize(level);
            }
        }
Пример #9
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;
        }
Пример #10
0
        private void SetData(IntPtr data, int size)
        {
            TextureTarget target = Target.Convert();

            Bind(target, 0);

            FormatInfo format = FormatTable.GetFormatInfo(_info.Format);

            int width  = _info.Width;
            int height = _info.Height;
            int depth  = _info.Depth;

            int offset = 0;

            for (int level = 0; level < _info.Levels; level++)
            {
                int mipSize = _info.GetMipSize(level);

                int endOffset = offset + mipSize;

                if ((uint)endOffset > (uint)size)
                {
                    return;
                }

                switch (_info.Target)
                {
                case Target.Texture1D:
                    if (format.IsCompressed)
                    {
                        GL.CompressedTexSubImage1D(
                            target,
                            level,
                            0,
                            width,
                            format.PixelFormat,
                            mipSize,
                            data);
                    }
                    else
                    {
                        GL.TexSubImage1D(
                            target,
                            level,
                            0,
                            width,
                            format.PixelFormat,
                            format.PixelType,
                            data);
                    }
                    break;

                case Target.Texture1DArray:
                case Target.Texture2D:
                    if (format.IsCompressed)
                    {
                        GL.CompressedTexSubImage2D(
                            target,
                            level,
                            0,
                            0,
                            width,
                            height,
                            format.PixelFormat,
                            mipSize,
                            data);
                    }
                    else
                    {
                        GL.TexSubImage2D(
                            target,
                            level,
                            0,
                            0,
                            width,
                            height,
                            format.PixelFormat,
                            format.PixelType,
                            data);
                    }
                    break;

                case Target.Texture2DArray:
                case Target.Texture3D:
                case Target.CubemapArray:
                    if (format.IsCompressed)
                    {
                        GL.CompressedTexSubImage3D(
                            target,
                            level,
                            0,
                            0,
                            0,
                            width,
                            height,
                            depth,
                            format.PixelFormat,
                            mipSize,
                            data);
                    }
                    else
                    {
                        GL.TexSubImage3D(
                            target,
                            level,
                            0,
                            0,
                            0,
                            width,
                            height,
                            depth,
                            format.PixelFormat,
                            format.PixelType,
                            data);
                    }
                    break;

                case Target.Cubemap:
                    int faceOffset = 0;

                    for (int face = 0; face < 6; face++, faceOffset += mipSize / 6)
                    {
                        if (format.IsCompressed)
                        {
                            GL.CompressedTexSubImage2D(
                                TextureTarget.TextureCubeMapPositiveX + face,
                                level,
                                0,
                                0,
                                width,
                                height,
                                format.PixelFormat,
                                mipSize / 6,
                                data + faceOffset);
                        }
                        else
                        {
                            GL.TexSubImage2D(
                                TextureTarget.TextureCubeMapPositiveX + face,
                                level,
                                0,
                                0,
                                width,
                                height,
                                format.PixelFormat,
                                format.PixelType,
                                data + faceOffset);
                        }
                    }
                    break;
                }

                data   += mipSize;
                offset += mipSize;

                width  = Math.Max(1, width >> 1);
                height = Math.Max(1, height >> 1);

                if (Target == Target.Texture3D)
                {
                    depth = Math.Max(1, depth >> 1);
                }
            }
        }
Пример #11
0
        private void CreateImmutableStorage()
        {
            TextureTarget target = Info.Target.Convert();

            GL.ActiveTexture(TextureUnit.Texture0);

            GL.BindTexture(target, Handle);

            FormatInfo format = FormatTable.GetFormatInfo(Info.Format);

            SizedInternalFormat internalFormat;

            if (format.IsCompressed)
            {
                internalFormat = (SizedInternalFormat)format.PixelFormat;
            }
            else
            {
                internalFormat = (SizedInternalFormat)format.PixelInternalFormat;
            }

            switch (Info.Target)
            {
            case Target.Texture1D:
                GL.TexStorage1D(
                    TextureTarget1d.Texture1D,
                    Info.Levels,
                    internalFormat,
                    Info.Width);
                break;

            case Target.Texture1DArray:
                GL.TexStorage2D(
                    TextureTarget2d.Texture1DArray,
                    Info.Levels,
                    internalFormat,
                    Info.Width,
                    Info.Height);
                break;

            case Target.Texture2D:
                GL.TexStorage2D(
                    TextureTarget2d.Texture2D,
                    Info.Levels,
                    internalFormat,
                    Info.Width,
                    Info.Height);
                break;

            case Target.Texture2DArray:
                GL.TexStorage3D(
                    TextureTarget3d.Texture2DArray,
                    Info.Levels,
                    internalFormat,
                    Info.Width,
                    Info.Height,
                    Info.Depth);
                break;

            case Target.Texture2DMultisample:
                GL.TexStorage2DMultisample(
                    TextureTargetMultisample2d.Texture2DMultisample,
                    Info.Samples,
                    internalFormat,
                    Info.Width,
                    Info.Height,
                    true);
                break;

            case Target.Texture2DMultisampleArray:
                GL.TexStorage3DMultisample(
                    TextureTargetMultisample3d.Texture2DMultisampleArray,
                    Info.Samples,
                    internalFormat,
                    Info.Width,
                    Info.Height,
                    Info.Depth,
                    true);
                break;

            case Target.Texture3D:
                GL.TexStorage3D(
                    TextureTarget3d.Texture3D,
                    Info.Levels,
                    internalFormat,
                    Info.Width,
                    Info.Height,
                    Info.Depth);
                break;

            case Target.Cubemap:
                GL.TexStorage2D(
                    TextureTarget2d.TextureCubeMap,
                    Info.Levels,
                    internalFormat,
                    Info.Width,
                    Info.Height);
                break;

            case Target.CubemapArray:
                GL.TexStorage3D(
                    (TextureTarget3d)All.TextureCubeMapArray,
                    Info.Levels,
                    internalFormat,
                    Info.Width,
                    Info.Height,
                    Info.Depth);
                break;

            default:
                Logger.PrintDebug(LogClass.Gpu, $"Invalid or unsupported texture target: {target}.");
                break;
            }
        }