Beispiel #1
0
        /// <summary>
        /// Applies the specified sampler state to this sampler.
        /// </summary>
        /// <param name="state">The sampler state to apply.</param>
        public void ApplySamplerState(SamplerState state)
        {
            Contract.Require(state, nameof(state));

            var textureWrapS = OpenGLSamplerState.GetTextureAddressModeGL(state.AddressU);

            if (textureWrapS != cachedTextureWrapS)
            {
                cachedTextureWrapS = textureWrapS;
                gl.SamplerParameteri(sampler, gl.GL_TEXTURE_WRAP_S, textureWrapS);
                gl.ThrowIfError();
            }

            var textureWrapT = OpenGLSamplerState.GetTextureAddressModeGL(state.AddressV);

            if (textureWrapT != cachedTextureWrapT)
            {
                cachedTextureWrapT = textureWrapT;
                gl.SamplerParameteri(sampler, gl.GL_TEXTURE_WRAP_T, textureWrapT);
                gl.ThrowIfError();
            }

            if (gl.IsGLES)
            {
                if (state.MipMapLevelOfDetailBias != 0)
                {
                    throw new NotSupportedException(OpenGLStrings.UnsupportedLODBiasGLES);
                }
            }
            else
            {
                if (cachedMipMapLODBias != state.MipMapLevelOfDetailBias)
                {
                    cachedMipMapLODBias = state.MipMapLevelOfDetailBias;
                    gl.SamplerParameterf(sampler, gl.GL_TEXTURE_LOD_BIAS, state.MipMapLevelOfDetailBias);
                    gl.ThrowIfError();
                }
            }

            switch (state.Filter)
            {
            case TextureFilter.Point:
                if (cachedMaxAnisotropy != 1f)
                {
                    cachedMaxAnisotropy = 1f;
                    gl.SamplerParameterf(sampler, gl.GL_TEXTURE_MAX_ANISOTROPY_EXT, 1f);
                    gl.ThrowIfError();
                }

                if (cachedMinFilter != gl.GL_NEAREST)
                {
                    cachedMinFilter = gl.GL_NEAREST;
                    gl.SamplerParameterf(sampler, gl.GL_TEXTURE_MIN_FILTER, (int)gl.GL_NEAREST);
                    gl.ThrowIfError();
                }

                if (cachedMagFilter != gl.GL_NEAREST)
                {
                    cachedMagFilter = gl.GL_NEAREST;
                    gl.SamplerParameterf(sampler, gl.GL_TEXTURE_MAG_FILTER, (int)gl.GL_NEAREST);
                    gl.ThrowIfError();
                }
                break;

            case TextureFilter.Linear:
                if (gl.IsAnisotropicFilteringAvailable)
                {
                    if (cachedMaxAnisotropy != 1f)
                    {
                        cachedMaxAnisotropy = 1f;
                        gl.SamplerParameterf(sampler, gl.GL_TEXTURE_MAX_ANISOTROPY_EXT, 1f);
                        gl.ThrowIfError();
                    }
                }

                if (cachedMinFilter != gl.GL_LINEAR)
                {
                    cachedMinFilter = gl.GL_LINEAR;
                    gl.SamplerParameteri(sampler, gl.GL_TEXTURE_MIN_FILTER, (int)gl.GL_LINEAR);
                    gl.ThrowIfError();
                }

                if (cachedMagFilter != gl.GL_LINEAR)
                {
                    cachedMagFilter = gl.GL_LINEAR;
                    gl.SamplerParameteri(sampler, gl.GL_TEXTURE_MAG_FILTER, (int)gl.GL_LINEAR);
                    gl.ThrowIfError();
                }
                break;

            case TextureFilter.Anisotropic:
                if (gl.IsAnisotropicFilteringAvailable)
                {
                    var maxAnisotropy = Math.Min(1f, state.MaxAnisotropy);
                    if (maxAnisotropy != cachedMaxAnisotropy)
                    {
                        cachedMaxAnisotropy = maxAnisotropy;
                        gl.SamplerParameterf(sampler, gl.GL_TEXTURE_MAX_ANISOTROPY_EXT, maxAnisotropy);
                        gl.ThrowIfError();
                    }
                }

                if (cachedMinFilter != gl.GL_LINEAR)
                {
                    cachedMinFilter = gl.GL_LINEAR;
                    gl.SamplerParameteri(sampler, gl.GL_TEXTURE_MIN_FILTER, (int)gl.GL_LINEAR);
                    gl.ThrowIfError();
                }

                if (cachedMagFilter != gl.GL_LINEAR)
                {
                    cachedMagFilter = gl.GL_LINEAR;
                    gl.SamplerParameteri(sampler, gl.GL_TEXTURE_MAG_FILTER, (int)gl.GL_LINEAR);
                    gl.ThrowIfError();
                }
                break;

            default:
                throw new NotSupportedException();
            }
        }
        /// <summary>
        /// Applies the specified sampler state to this sampler.
        /// </summary>
        /// <param name="state">The sampler state to apply.</param>
        public void ApplySamplerState(SamplerState state)
        {
            Contract.Require(state, nameof(state));

            gl.SamplerParameteri(sampler, gl.GL_TEXTURE_WRAP_S, OpenGLSamplerState.GetTextureAddressModeGL(state.AddressU));
            gl.ThrowIfError();

            gl.SamplerParameteri(sampler, gl.GL_TEXTURE_WRAP_T, OpenGLSamplerState.GetTextureAddressModeGL(state.AddressV));
            gl.ThrowIfError();

            if (gl.IsGLES)
            {
                if (state.MipMapLevelOfDetailBias != 0)
                {
                    throw new NotSupportedException(OpenGLStrings.UnsupportedLODBiasGLES);
                }
            }
            else
            {
                gl.SamplerParameterf(sampler, gl.GL_TEXTURE_LOD_BIAS, state.MipMapLevelOfDetailBias);
                gl.ThrowIfError();
            }

            switch (state.Filter)
            {
            case TextureFilter.Point:
                gl.SamplerParameterf(sampler, gl.GL_TEXTURE_MAX_ANISOTROPY_EXT, 1f);
                gl.ThrowIfError();

                gl.SamplerParameterf(sampler, gl.GL_TEXTURE_MIN_FILTER, (int)gl.GL_NEAREST);
                gl.ThrowIfError();

                gl.SamplerParameterf(sampler, gl.GL_TEXTURE_MAG_FILTER, (int)gl.GL_NEAREST);
                gl.ThrowIfError();
                break;

            case TextureFilter.Linear:
                if (gl.IsAnisotropicFilteringAvailable)
                {
                    gl.SamplerParameterf(sampler, gl.GL_TEXTURE_MAX_ANISOTROPY_EXT, 1f);
                    gl.ThrowIfError();
                }

                gl.SamplerParameteri(sampler, gl.GL_TEXTURE_MIN_FILTER, (int)gl.GL_LINEAR);
                gl.ThrowIfError();

                gl.SamplerParameteri(sampler, gl.GL_TEXTURE_MAG_FILTER, (int)gl.GL_LINEAR);
                gl.ThrowIfError();
                break;

            case TextureFilter.Anisotropic:
                if (gl.IsAnisotropicFilteringAvailable)
                {
                    gl.SamplerParameterf(sampler, gl.GL_TEXTURE_MAX_ANISOTROPY_EXT, Math.Min(1f, state.MaxAnisotropy));
                    gl.ThrowIfError();
                }

                gl.SamplerParameteri(sampler, gl.GL_TEXTURE_MIN_FILTER, (int)gl.GL_LINEAR);
                gl.ThrowIfError();

                gl.SamplerParameteri(sampler, gl.GL_TEXTURE_MAG_FILTER, (int)gl.GL_LINEAR);
                gl.ThrowIfError();
                break;

            default:
                throw new NotSupportedException();
            }
        }