/// <inheritdoc/> public void SetTexture(Int32 sampler, Texture2D texture) { Contract.EnsureRange(sampler >= 0 && sampler < maxTextureStages, nameof(sampler)); Contract.EnsureNotDisposed(this, Disposed); Ultraviolet.ValidateResource(texture); if (texture != null && texture.BoundForWriting) { throw new InvalidOperationException(OpenGLStrings.RenderBufferCannotBeUsedAsTexture); } if (texture != null && texture.WillNotBeSampled) { throw new InvalidOperationException(OpenGLStrings.RenderBufferWillNotBeSampled); } if (this.textures[sampler] != texture) { var textureName = (texture == null) ? 0 : ((IOpenGLResource)texture).OpenGLName; OpenGLState.ActiveTexture((uint)(gl.GL_TEXTURE0 + sampler)); OpenGLState.Texture2D(textureName); if (this.textures[sampler] != null) { ((IBindableResource)this.textures[sampler]).UnbindRead(); } this.textures[sampler] = texture; if (this.textures[sampler] != null) { ((IBindableResource)this.textures[sampler]).BindRead(); } if (!capabilities.SupportsIndependentSamplerState) { var samplerState = (OpenGLSamplerState)(GetSamplerState(sampler) ?? SamplerState.LinearClamp); for (int i = 0; i < samplerStates.Length; i++) { if (this.textures[i] == texture) { samplerState.Apply(sampler); } } } } }