public static SamplerState New(GraphicsDevice graphicsDevice, SamplerStateDescription samplerStateDescription) { // Store SamplerState in a cache (D3D seems to have quite bad concurrency when using CreateSampler while rendering) SamplerState samplerState; lock (graphicsDevice.CachedSamplerStates) { if (graphicsDevice.CachedSamplerStates.TryGetValue(samplerStateDescription, out samplerState)) { // TODO: Appropriate destroy samplerState.AddReferenceInternal(); } else { samplerState = new SamplerState(graphicsDevice, samplerStateDescription); graphicsDevice.CachedSamplerStates.Add(samplerStateDescription, samplerState); } } return samplerState; }
internal void Apply(bool hasMipmap, SamplerState oldSamplerState, TextureTarget target) { #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES // TODO: support texture array, 3d and cube if (!GraphicsDevice.IsOpenGLES2) #endif { if (Description.MinMipLevel != oldSamplerState.Description.MinMipLevel) GL.TexParameter(target, TextureParameterName.TextureMinLod, Description.MinMipLevel); if (Description.MaxMipLevel != oldSamplerState.Description.MaxMipLevel) GL.TexParameter(target, TextureParameterName.TextureMaxLod, Description.MaxMipLevel); if (textureWrapR != oldSamplerState.textureWrapR) GL.TexParameter(target, TextureParameterName.TextureWrapR, (int)textureWrapR); if (compareFunc != oldSamplerState.compareFunc) GL.TexParameter(target, TextureParameterName.TextureCompareFunc, (int)compareFunc); } #if !SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES if (borderColor != oldSamplerState.borderColor) GL.TexParameter(target, TextureParameterName.TextureBorderColor, borderColor); if (Description.MipMapLevelOfDetailBias != oldSamplerState.Description.MipMapLevelOfDetailBias) GL.TexParameter(target, TextureParameterName.TextureLodBias, Description.MipMapLevelOfDetailBias); if (minFilter != oldSamplerState.minFilter) GL.TexParameter(target, TextureParameterName.TextureMinFilter, (int)minFilter); #else // On OpenGL ES, we need to choose the appropriate min filter ourself if the texture doesn't contain mipmaps (done at PreDraw) if (minFilter != oldSamplerState.minFilter) GL.TexParameter(target, TextureParameterName.TextureMinFilter, hasMipmap ? (int)minFilter : (int)minFilterNoMipmap); #endif #if !SILICONSTUDIO_PLATFORM_IOS if (Description.MaxAnisotropy != oldSamplerState.Description.MaxAnisotropy) GL.TexParameter(target, (TextureParameterName)OpenTK.Graphics.ES20.ExtTextureFilterAnisotropic.TextureMaxAnisotropyExt, Description.MaxAnisotropy); #endif if (magFilter != oldSamplerState.magFilter) GL.TexParameter(target, TextureParameterName.TextureMagFilter, (int)magFilter); if (textureWrapS != oldSamplerState.textureWrapS) GL.TexParameter(target, TextureParameterName.TextureWrapS, (int)textureWrapS); if (textureWrapT != oldSamplerState.textureWrapT) GL.TexParameter(target, TextureParameterName.TextureWrapT, (int)textureWrapT); }
public override void Start() { base.Start(); paradoxTexture = Asset.Load<Texture>("LogoParadox"); customEffect = EffectSystem.LoadEffect("Effect").WaitForResult(); quad = new PrimitiveQuad(GraphicsDevice, customEffect); // set fixed parameters once quad.Parameters.Set(EffectKeys.Center, new Vector2(0.5f, 0.5f)); quad.Parameters.Set(EffectKeys.Frequency, 40); quad.Parameters.Set(EffectKeys.Spread, 0.5f); quad.Parameters.Set(EffectKeys.Amplitude, 0.015f); quad.Parameters.Set(EffectKeys.InvAspectRatio, GraphicsDevice.BackBuffer.Height / (float)GraphicsDevice.BackBuffer.Width); // NOTE: Linear-Wrap sampling is not available for non-square non-power-of-two textures on opengl es 2.0 samplingState = SamplerState.New(GraphicsDevice, new SamplerStateDescription(TextureFilter.Linear, TextureAddressMode.Clamp)); // Add Effect rendering to the end of the pipeline var scene = SceneSystem.SceneInstance.Scene; var compositor = ((SceneGraphicsCompositorLayers)scene.Settings.GraphicsCompositor); renderer = new SceneDelegateRenderer(RenderQuad); compositor.Master.Renderers.Add(renderer); }
/// <summary> /// Draws a quad with a texture. This Draw method is using a simple pixel shader that is sampling the texture. /// </summary> /// <param name="texture">The texture to draw.</param> /// <param name="samplerState">State of the sampler. If null, default sampler is <see cref="SamplerStateFactory.LinearClamp" />.</param> /// <param name="color">The color.</param> /// <param name="applyEffectStates">The flag to apply effect states.</param> /// <exception cref="System.ArgumentException">Expecting a Texture;texture</exception> public void Draw(Texture texture, SamplerState samplerState, Color4 color, bool applyEffectStates = false) { // Make sure that we are using our vertex shader parameters.Set(SpriteEffectKeys.Color, color); parameters.Set(TexturingKeys.Texture0, texture); parameters.Set(TexturingKeys.Sampler, samplerState ?? GraphicsDevice.SamplerStates.LinearClamp); simpleEffect.Apply(GraphicsDevice, parameterCollectionGroup, applyEffectStates); Draw(); // TODO ADD QUICK UNBIND FOR SRV //GraphicsDevice.Context.PixelShader.SetShaderResource(0, null); }
/// <summary> /// Sets a sampler state to the shader pipeline. /// </summary> /// <param name="stage">The shader stage.</param> /// <param name="slot">The binding slot.</param> /// <param name="samplerState">The sampler state to set.</param> public void SetSamplerState(ShaderStage stage, int slot, SamplerState samplerState) { throw new NotImplementedException(); }
private void InitializeFromImpl(DataBox[] dataBoxes = null) { if (ParentTexture != null) { resourceId = ParentTexture.ResourceId; // copy parameters InternalFormat = ParentTexture.InternalFormat; FormatGl = ParentTexture.FormatGl; Type = ParentTexture.Type; Target = ParentTexture.Target; DepthPitch = ParentTexture.DepthPitch; RowPitch = ParentTexture.RowPitch; IsDepthBuffer = ParentTexture.IsDepthBuffer; HasStencil = ParentTexture.HasStencil; IsRenderbuffer = ParentTexture.IsRenderbuffer; resourceIdStencil = ParentTexture.ResourceIdStencil; pixelBufferObjectId = ParentTexture.PixelBufferObjectId; } if (resourceId == 0) { switch (Dimension) { case TextureDimension.Texture1D: #if !SILICONSTUDIO_PLATFORM_MONO_MOBILE Target = TextureTarget.Texture1D; break; #endif case TextureDimension.Texture2D: Target = TextureTarget.Texture2D; break; case TextureDimension.Texture3D: Target = TextureTarget.Texture3D; break; case TextureDimension.TextureCube: Target = TextureTarget.TextureCubeMap; break; default: throw new ArgumentOutOfRangeException(); } PixelInternalFormat internalFormat; PixelFormatGl format; PixelType type; int pixelSize; bool compressed; OpenGLConvertExtensions.ConvertPixelFormat(GraphicsDevice, Description.Format, out internalFormat, out format, out type, out pixelSize, out compressed); InternalFormat = internalFormat; FormatGl = format; Type = type; DepthPitch = Description.Width * Description.Height * pixelSize; RowPitch = Description.Width * pixelSize; if ((Description.Flags & TextureFlags.DepthStencil) != 0) { IsDepthBuffer = true; HasStencil = InternalHasStencil(Format); } else { IsDepthBuffer = false; HasStencil = false; } if ((Description.Flags & TextureFlagsCustomResourceId) != 0) return; using (GraphicsDevice.UseOpenGLCreationContext()) { // Depth texture are render buffer for now // TODO: enable switch if ((Description.Flags & TextureFlags.DepthStencil) != 0 && (Description.Flags & TextureFlags.ShaderResource) == 0) { RenderbufferStorage depth, stencil; ConvertDepthFormat(GraphicsDevice, Description.Format, out depth, out stencil); GL.GenRenderbuffers(1, out resourceId); GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, resourceId); GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, depth, Width, Height); if (stencil != 0) { // separate stencil GL.GenRenderbuffers(1, out resourceIdStencil); GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, resourceIdStencil); GL.RenderbufferStorage(RenderbufferTarget.Renderbuffer, stencil, Width, Height); } else if (HasStencil) { // depth+stencil in a single texture resourceIdStencil = resourceId; } GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, 0); IsRenderbuffer = true; return; } else { GL.GenTextures(1, out resourceId); GL.BindTexture(Target, resourceId); IsRenderbuffer = false; } // No filtering on depth buffer if ((Description.Flags & (TextureFlags.RenderTarget | TextureFlags.DepthStencil)) != TextureFlags.None) { GL.TexParameter(Target, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest); GL.TexParameter(Target, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest); GL.TexParameter(Target, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge); GL.TexParameter(Target, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge); BoundSamplerState = GraphicsDevice.SamplerStates.PointClamp; } #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES else if (Description.MipLevels <= 1) { GL.TexParameter(Target, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest); GL.TexParameter(Target, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); } #endif #if SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES if (!GraphicsDevice.IsOpenGLES2) #endif { GL.TexParameter(Target, TextureParameterName.TextureBaseLevel, 0); GL.TexParameter(Target, TextureParameterName.TextureMaxLevel, Description.MipLevels - 1); } if (Description.MipLevels == 0) throw new NotImplementedException(); var setSize = TextureSetSize(Target); for (var arrayIndex = 0; arrayIndex < Description.ArraySize; ++arrayIndex) { var offsetArray = arrayIndex*Description.MipLevels; for (int i = 0; i < Description.MipLevels; ++i) { IntPtr data = IntPtr.Zero; var width = CalculateMipSize(Description.Width, i); var height = CalculateMipSize(Description.Height, i); if (dataBoxes != null && i < dataBoxes.Length) { if (setSize > 1 && !compressed && dataBoxes[i].RowPitch != width*pixelSize) throw new NotSupportedException("Can't upload texture with pitch in glTexImage2D."); // Might be possible, need to check API better. data = dataBoxes[offsetArray + i].DataPointer; } if (setSize == 2) { var dataSetTarget = GetTextureTargetForDataSet2D(Target, arrayIndex); if (compressed) { GL.CompressedTexImage2D(dataSetTarget, i, (CompressedInternalFormat2D)internalFormat, width, height, 0, dataBoxes[offsetArray + i].SlicePitch, data); } else { GL.TexImage2D(dataSetTarget, i, internalFormat, width, height, 0, format, type, data); } } else if (setSize == 3) { var dataSetTarget = GetTextureTargetForDataSet3D(Target); var depth = Target == TextureTarget.Texture2DArray ? Description.Depth : CalculateMipSize(Description.Depth, i); // no depth mipmaps in Texture2DArray if (compressed) { GL.CompressedTexImage3D(dataSetTarget, i, (CompressedInternalFormat3D)internalFormat, width, height, depth, 0, dataBoxes[offsetArray + i].SlicePitch, data); } else { GL.TexImage3D(dataSetTarget, i, (TextureComponentCount3D)internalFormat, width, height, depth, 0, format, type, data); } } #if !SILICONSTUDIO_PARADOX_GRAPHICS_API_OPENGLES else if (setSize == 1) { if (compressed) { GL.CompressedTexImage1D(TextureTarget.Texture1D, i, internalFormat, width, 0, dataBoxes[offsetArray + i].SlicePitch, data); } else { GL.TexImage1D(TextureTarget.Texture1D, i, internalFormat, width, 0, format, type, data); } } #endif } } GL.BindTexture(Target, 0); InitializePixelBufferObject(); } } }
/// <summary> /// Draws a fullscreen texture using the specified sampler /// and the texture color multiplied by a custom color. See <see cref="Draw+a+texture"/> to learn how to use it. /// </summary> /// <param name="texture">The texture. Expecting an instance of <see cref="Texture"/>.</param> /// <param name="sampler">The sampler.</param> /// <param name="color">The color.</param> /// <param name="applyEffectStates">The flag to apply effect states.</param> public void DrawTexture(Texture texture, SamplerState sampler, Color4 color, bool applyEffectStates = false) { primitiveQuad.Draw(texture, sampler, color, applyEffectStates); }
/// <summary> /// Draws a fullscreen texture using the specified sampler. See <see cref="Draw+a+texture"/> to learn how to use it. /// </summary> /// <param name="texture">The texture. Expecting an instance of <see cref="Texture"/>.</param> /// <param name="sampler">The sampler.</param> /// <param name="applyEffectStates">The flag to apply effect states.</param> public void DrawTexture(Texture texture, SamplerState sampler, bool applyEffectStates = false) { DrawTexture(texture, sampler, Color4.White, applyEffectStates); }