Exemplo n.º 1
0
 public static void SetupColorTextures(this FrameBufferRef fbRef, params int[] indices)
 {
     for (int i = 0; i < indices.Length; i++)
     {
         fbRef.SetupColorTexture(indices[i]);
     }
 }
        public void Dispose()
        {
            _shader?.Dispose();
            _shader = null;

            if (_frameBuffer != null)
            {
                _platform.DisposeFrameBuffer(_frameBuffer);
                _frameBuffer = null;
            }
        }
        private void SetupFramebuffers(List <FrameBufferRef> mainBuffers)
        {
            if (_frameBuffer != null)
            {
                _platform.DisposeFrameBuffer(_frameBuffer);
                _frameBuffer = null;
            }

            var ssao = ClientSettings.SSAOQuality > 0;

            if (!ssao || !_enabled)
            {
                return;
            }

            var fbPrimary = mainBuffers[(int)EnumFrameBuffer.Primary];

            var fbWidth  = (int)(_platform.window.Width * ClientSettings.SSAA);
            var fbHeight = (int)(_platform.window.Height * ClientSettings.SSAA);

            if (fbWidth == 0 || fbHeight == 0)
            {
                return;
            }

            var fb = new FrameBufferRef
            {
                FboId           = GL.GenFramebuffer(),
                Width           = fbWidth,
                Height          = fbHeight,
                ColorTextureIds = ArrayUtil.CreateFilled(2, _ => GL.GenTexture())
            };

            GL.BindFramebuffer(FramebufferTarget.Framebuffer, fb.FboId);
            GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment,
                                    TextureTarget.Texture2D, fbPrimary.DepthTextureId, 0);
            fb.SetupColorTexture(0);
            fb.SetupColorTexture(1);

            GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment2,
                                    TextureTarget.Texture2D, fbPrimary.ColorTextureIds[2], 0);
            GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment3,
                                    TextureTarget.Texture2D, fbPrimary.ColorTextureIds[3], 0);
            GL.DrawBuffers(4, new []
            {
                DrawBuffersEnum.ColorAttachment0, DrawBuffersEnum.ColorAttachment1,
                DrawBuffersEnum.ColorAttachment2, DrawBuffersEnum.ColorAttachment3
            });

            Framebuffers.CheckStatus();
            _frameBuffer = fb;

            _screenQuad = _platform.GetScreenQuad();
        }
Exemplo n.º 4
0
 public static void SetupColorTexture(this FrameBufferRef fbRef, int textureId)
 {
     GL.BindTexture(TextureTarget.Texture2D, fbRef.ColorTextureIds[textureId]);
     GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, fbRef.Width, fbRef.Height, 0, PixelFormat.Rgba, PixelType.UnsignedShort, IntPtr.Zero);
     GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter,
                     (int)TextureMinFilter.Linear);
     GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter,
                     (int)TextureMagFilter.Linear);
     GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0 + textureId,
                             TextureTarget.Texture2D, fbRef.ColorTextureIds[textureId], 0);
 }
Exemplo n.º 5
0
 public static void SetupDepthTexture(this FrameBufferRef fbRef)
 {
     GL.BindTexture(TextureTarget.Texture2D, fbRef.DepthTextureId);
     GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.DepthComponent32,
                   fbRef.Width, fbRef.Height, 0, PixelFormat.DepthComponent, PixelType.Float, IntPtr.Zero);
     GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter,
                     (int)TextureMinFilter.Nearest);
     GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter,
                     (int)TextureMagFilter.Nearest);
     GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS,
                     (int)TextureWrapMode.ClampToEdge);
     GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT,
                     (int)TextureWrapMode.ClampToEdge);
     GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment,
                             TextureTarget.Texture2D, fbRef.DepthTextureId, 0);
 }
Exemplo n.º 6
0
        public static void SetupTextures(this FrameBufferRef fbRef, int[] vIds, int[] cIds, bool depth = true)
        {
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, fbRef.FboId);

            if (depth)
            {
                fbRef.SetupDepthTexture();
            }
            fbRef.SetupVertexTextures(vIds);
            fbRef.SetupColorTextures(cIds);

            DrawBuffersEnum[] attachments = ArrayUtil.CreateFilled(vIds.Length + cIds.Length, i => DrawBuffersEnum.ColorAttachment0 + i);
            GL.DrawBuffers(attachments.Length, attachments);

            fbRef.CheckStatus();
        }
Exemplo n.º 7
0
 public static void SetupVertexTexture(this FrameBufferRef fbRef, int textureId)
 {
     GL.BindTexture(TextureTarget.Texture2D, fbRef.ColorTextureIds[textureId]);
     GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba16f, fbRef.Width, fbRef.Height, 0,
                   PixelFormat.Rgba, PixelType.Float, IntPtr.Zero);
     GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter,
                     (int)TextureMinFilter.Linear);
     GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter,
                     (int)TextureMagFilter.Linear);
     GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureBorderColor,
                     new[] { 1f, 1f, 1f, 1f });
     GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS,
                     (int)TextureWrapMode.ClampToBorder);
     GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT,
                     (int)TextureWrapMode.ClampToBorder);
     GL.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0 + textureId,
                             TextureTarget.Texture2D, fbRef.ColorTextureIds[textureId], 0);
 }
        public void SetupFramebuffers(List <FrameBufferRef> mainBuffers)
        {
            _mod.Mod.Logger.Event("Recreating framebuffers");

            for (var i = 0; i < _framebuffers.Length; i++)
            {
                if (_framebuffers[i] == null)
                {
                    continue;
                }

                _platform.DisposeFrameBuffer(_framebuffers[i]);
                _framebuffers[i] = null;
            }

            // create new framebuffer
            _fbWidth  = (int)(_platform.window.Width * ClientSettings.SSAA);
            _fbHeight = (int)(_platform.window.Height * ClientSettings.SSAA);
            if (_fbWidth == 0 || _fbHeight == 0)
            {
                return;
            }

            var framebuffer = new FrameBufferRef
            {
                FboId = GL.GenFramebuffer(), Width = _fbWidth, Height = _fbHeight, DepthTextureId = GL.GenTexture()
            };

            GL.BindFramebuffer(FramebufferTarget.Framebuffer, framebuffer.FboId);
            framebuffer.SetupDepthTexture();

            // create our normal and position textures
            framebuffer.ColorTextureIds = ArrayUtil.CreateFilled(_refractionsEnabled ? 4 : 3, _ => GL.GenTexture());

            // bind and setup textures
            framebuffer.SetupVertexTexture(0);
            framebuffer.SetupVertexTexture(1);
            framebuffer.SetupColorTexture(2);
            if (_refractionsEnabled)
            {
                framebuffer.SetupVertexTexture(3);
            }

            if (_refractionsEnabled)
            {
                GL.DrawBuffers(4,
                               new[]
                {
                    DrawBuffersEnum.ColorAttachment0, DrawBuffersEnum.ColorAttachment1, DrawBuffersEnum.ColorAttachment2,
                    DrawBuffersEnum.ColorAttachment3
                });
            }
            else
            {
                GL.DrawBuffers(3,
                               new[]
                {
                    DrawBuffersEnum.ColorAttachment0, DrawBuffersEnum.ColorAttachment1,
                    DrawBuffersEnum.ColorAttachment2
                });
            }

            Framebuffers.CheckStatus();
            _framebuffers[(int)EnumSSRFB.SSR] = framebuffer;

            // setup output framebuffer
            framebuffer = new FrameBufferRef
            {
                FboId = GL.GenFramebuffer(), Width = _fbWidth, Height = _fbHeight
            };
            GL.BindFramebuffer(FramebufferTarget.Framebuffer, framebuffer.FboId);
            framebuffer.ColorTextureIds = new[] { GL.GenTexture() };

            framebuffer.SetupColorTexture(0);

            GL.DrawBuffer(DrawBufferMode.ColorAttachment0);
            Framebuffers.CheckStatus();
            _framebuffers[(int)EnumSSRFB.Out] = framebuffer;

            if (_causticsEnabled)
            {
                framebuffer = new FrameBufferRef
                {
                    FboId = GL.GenFramebuffer(), Width = _fbWidth, Height = _fbHeight
                };
                GL.BindFramebuffer(FramebufferTarget.Framebuffer, framebuffer.FboId);
                framebuffer.ColorTextureIds = new[] { GL.GenTexture() };

                framebuffer.SetupSingleColorTexture(0);

                GL.DrawBuffer(DrawBufferMode.ColorAttachment0);
                Framebuffers.CheckStatus();
                _framebuffers[(int)EnumSSRFB.Caustics] = framebuffer;
            }

            _screenQuad = _platform.GetScreenQuad();
        }
Exemplo n.º 9
0
 public static void CheckStatus(this FrameBufferRef fbRef) => CheckStatus();