public void RestoreRenderTarget() { Renderer.Flush(); uint prevFramebuffer = framebufferStack.Pop(); PlatformRenderer.BindFramebuffer(prevFramebuffer); }
public void SetAsRenderTarget() { Renderer.Flush(); GetHandle(); uint currentFramebuffer = (uint)PlatformRenderer.CurrentFramebuffer; PlatformRenderer.BindFramebuffer(framebuffer); framebufferStack.Push(currentFramebuffer); }
private void CreateTexture() { if (IsDisposed) { throw new ObjectDisposedException(GetType().Name); } var t = new uint[1]; GL.GenFramebuffers(1, t); framebuffer = t[0]; GL.GenTextures(1, t); handle = t[0]; GL.ActiveTexture(TextureUnit.Texture0); GL.BindTexture(TextureTarget.Texture2D, handle); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, textureParams.MinFilter.ToInt()); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, textureParams.MagFilter.ToInt()); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, textureParams.WrapModeU.ToInt()); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, textureParams.WrapModeV.ToInt()); int bpp; if (Format == RenderTextureFormat.RGBA8) { GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, size.Width, size.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, (IntPtr)null); bpp = 4; } else { GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, size.Width, size.Height, 0, PixelFormat.Rgb, PixelType.UnsignedShort565, (IntPtr)null); bpp = 2; } MemoryUsed = SurfaceSize.Width * SurfaceSize.Height * bpp; uint oldFramebuffer = PlatformRenderer.CurrentFramebuffer; PlatformRenderer.BindFramebuffer(framebuffer); PlatformRenderer.CheckErrors(); GL.FramebufferTexture2D( FramebufferTarget.Framebuffer, FramebufferSlot.ColorAttachment0, TextureTarget.Texture2D, handle, level: 0); if ((int)GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer) != (int)FramebufferErrorCode.FramebufferComplete) { throw new Exception("Failed to create render texture. Framebuffer is incomplete."); } AttachRenderBuffer(FramebufferSlot.DepthAttachment, RenderbufferInternalFormat.DepthComponent16, out depthBuffer); Renderer.Clear(Color4.Black); PlatformRenderer.MarkTextureSlotAsDirty(0); PlatformRenderer.BindFramebuffer(oldFramebuffer); PlatformRenderer.CheckErrors(); }
private void CreateTexture() { if (!Application.CurrentThread.IsMain()) { throw new Lime.Exception("Attempt to create a RenderTexture not from the main thread"); } var t = new uint[1]; GL.GenFramebuffers(1, t); framebuffer = t[0]; GL.GenTextures(1, t); handle = t[0]; PlatformRenderer.PushTexture(handle, 0); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, textureParams.MinFilter.ToInt()); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, textureParams.MagFilter.ToInt()); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, textureParams.WrapModeU.ToInt()); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, textureParams.WrapModeV.ToInt()); int bpp; if (Format == RenderTextureFormat.RGBA8) { GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, size.Width, size.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, (IntPtr)null); bpp = 4; } else { GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgb, size.Width, size.Height, 0, PixelFormat.Rgb, PixelType.UnsignedShort565, (IntPtr)null); bpp = 2; } MemoryUsed = SurfaceSize.Width * SurfaceSize.Height * bpp; uint oldFramebuffer = PlatformRenderer.CurrentFramebuffer; PlatformRenderer.BindFramebuffer(framebuffer); PlatformRenderer.CheckErrors(); GL.FramebufferTexture2D( FramebufferTarget.Framebuffer, FramebufferSlot.ColorAttachment0, TextureTarget.Texture2D, handle, level: 0); if ((int)GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer) != (int)FramebufferErrorCode.FramebufferComplete) { throw new Exception("Failed to create render texture. Framebuffer is incomplete."); } AttachRenderBuffer(FramebufferSlot.DepthAttachment, RenderbufferInternalFormat.DepthComponent16, out depthBuffer); Renderer.Clear(0, 0, 0, 0); PlatformRenderer.PopTexture(0); PlatformRenderer.BindFramebuffer(oldFramebuffer); PlatformRenderer.CheckErrors(); }