Exemple #1
0
        public void Update()
        {
            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, framebuffer);
            Gl.Viewport(0, 0, screenWidth, screenHeight);
            Color32 clrColor = sceneClear;

            Gl.ClearColor(clrColor.r / 255.0f, clrColor.g / 255.0f, clrColor.b / 255.0f, 255.0f);
            Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            ObjRenderer.RenderQueue();
            Debug.Label("obj render");
            drawScreen.Render();
            Debug.Label("soft render");

            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
            Gl.Viewport(0, 0, windowWidth, windowHeight);
            Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            Debug.Label("framebuffer swap");
            Gl.UseProgram(shader);

            Gl.BindBufferToShaderAttribute(vertices, shader, "pos");
            Gl.BindBufferToShaderAttribute(uvs, shader, "uv");
            Gl.BindBuffer(triangles);
            Gl.BindTexture(TextureTarget.Texture2D, texture);

            Gl.DrawElements(BeginMode.Triangles, 6, DrawElementsType.UnsignedInt, IntPtr.Zero);
            Debug.Label("framebuffer render");

            SDL.SDL_GL_SwapWindow(window);
            Debug.Label("swap window");
        }
        private void _renderOneBuffer(bool isTransplate, int frameIdx = -1)
        {
            ParticleEditModel md = MainModel.ins.particleEditModel;

            int w    = md.width;
            int h    = md.height;
            int size = w * h * pxChannel;

            try {
                initOutputBuffer();

                Gl.BindFramebuffer(FramebufferTarget.Framebuffer, glbufOutput);

                if (isTransplate)
                {
                    glClear(0);
                }
                //glClear(0x00808080);
                renderGlToBuffer(frameIdx);
                glClear(md.background, 1);

                Gl.ReadPixels(0, 0, w, h, OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, mlBufferOutput.Address);

                Gl.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
            } catch (Exception ex) {
                Debug.WriteLine(ex.ToString());
            }
        }
Exemple #3
0
        /// <summary>
        /// Bind this GraphicsSurface for drawing.
        /// </summary>
        /// <param name="ctx">
        /// A <see cref="GraphicsContext"/> to wich associate its rendering result to this GraphicsSurface.
        /// </param>
        public override void BindDraw(GraphicsContext ctx)
        {
            // Bind this framebuffer
            Gl.BindFramebuffer(Gl.DRAW_FRAMEBUFFER, ObjectName);

            List <int> drawBuffers = new List <int>();

            for (uint i = 0; i < Gl.CurrentLimits.MaxColorAttachments; i++)
            {
                // Reset dirty binding points
                if ((_ColorBuffers[i] != null) && _ColorBuffers[i].Dirty)
                {
                    // Ensure created buffer
                    _ColorBuffers[i].Create(ctx);
                    // Ensure attached buffer
                    _ColorBuffers[i].Attach(Gl.DRAW_FRAMEBUFFER, Gl.COLOR_ATTACHMENT0 + (int)i);
                    _ColorBuffers[i].Dirty = false;
                }

                // Collect draw buffers
                if (_ColorBuffers[i] != null)
                {
                    drawBuffers.Add(Gl.COLOR_ATTACHMENT0 + (int)i);
                }
            }

            // Validate completeness status
            Validate(ctx);

            // Update draw buffers
            Gl.DrawBuffers(drawBuffers.ToArray());
        }
Exemple #4
0
        /// <summary>
        /// Set ShaderProgram state.
        /// </summary>
        /// <param name="ctx">
        /// A <see cref="GraphicsContext"/> which has defined the shader program <paramref name="sProgram"/>.
        /// </param>
        /// <param name="sProgram">
        /// The <see cref="ShaderProgram"/> which has the state set.
        /// </param>
        public override void ApplyState(GraphicsContext ctx, ShaderProgram sProgram)
        {
            if (ctx == null)
            {
                throw new ArgumentNullException("ctx");
            }

            // Framebuffer object must be existing to apply state
            if (mDrawFramebuffer != null && !mDrawFramebuffer.Exists(ctx))
            {
                throw new InvalidOperationException("RenderFramebuffer not existing");
            }

            // Setup binding
            if (mDrawFramebuffer == null)
            {
                // The draw binding is stored in 'mDrawBinding': 0 indicates the default framebuffer
                Gl.BindFramebuffer(Gl.DRAW_FRAMEBUFFER, mDrawBinding);
            }
            else
            {
                // Bind a framebuffer object instead of any framebuffer (including the default one)
                // Here a default draw buffers configuration is setup
                mDrawFramebuffer.BindDraw(ctx);
            }

            // Setup draw buffers
            if (mDrawBuffers != null)
            {
                Gl.DrawBuffers(mDrawBuffers);
            }
        }
        /// <inheritdoc />
        public override void CopyRenderTarget(RenderTarget source, RenderTarget dest, Rectangle?sourceRect = null, Rectangle?destRect = null, bool smooth = false)
        {
            GLThread.ExecuteGLThread(() =>
            {
                RenderTarget srcTarget  = source ?? Engine.Renderer.BaseTarget;
                RenderTarget destTarget = dest ?? Engine.Renderer.BaseTarget;

                Vector2 sourceSize = srcTarget.Size;
                Vector2 destSize   = destTarget.Size;

                Rectangle srcRect = sourceRect ?? new Rectangle(0, 0, sourceSize);
                Rectangle dstRect = destRect ?? new Rectangle(0, 0, destSize);

                uint srcPointer  = (uint)((GlRenderTarget)srcTarget)?.Pointer;
                uint destPointer = (uint)((GlRenderTarget)destTarget)?.Pointer;

                Gl.BindFramebuffer(FramebufferTarget.ReadFramebuffer, srcPointer);
                Gl.BindFramebuffer(FramebufferTarget.DrawFramebuffer, destPointer);
                Gl.BlitFramebuffer((int)srcRect.X, (int)srcRect.Y, (int)srcRect.Width, (int)srcRect.Height, (int)dstRect.X, (int)dstRect.Y, (int)dstRect.Width, (int)dstRect.Height,
                                   ClearBufferMask.ColorBufferBit, smooth ? BlitFramebufferFilter.Linear : BlitFramebufferFilter.Nearest);

                // Restore bindings and so on.
                Engine.Renderer.EnsureRenderTarget();
            });
        }
Exemple #6
0
        public Framebuffer()
        {
            Fbo = Gl.GenFramebuffer();
            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, Fbo);

            ColorTexture = Gl.GenTexture();
            Gl.BindTexture(TextureTarget.Texture2d, ColorTexture);
            Gl.TexImage2D(TextureTarget.Texture2d, 0, InternalFormat.Srgb, Constants.renderWidth, Constants.renderHeight, 0, PixelFormat.Rgb, 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.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
            Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);

            Gl.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2d, ColorTexture, 0);

            DepthTexture = Gl.GenTexture();
            Gl.BindTexture(TextureTarget.Texture2d, DepthTexture);
            Gl.TexImage2D(TextureTarget.Texture2d, 0, InternalFormat.DepthComponent, Constants.renderWidth, Constants.renderHeight, 0, PixelFormat.DepthComponent, 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.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
            Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);

            Gl.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, TextureTarget.Texture2d, DepthTexture, 0);

            Gl.BindTexture(TextureTarget.Texture2d, 0);
            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
        }
Exemple #7
0
        public static uint CreateBrdfLut()
        {
            var shader         = new Shader("Content/Shaders/BrdfLut/brdfLut.frag", "Content/Shaders/BrdfLut/brdfLut.vert");
            var brdfLutTexture = Gl.GenTexture();
            var plane          = new Plane();

            Gl.BindTexture(TextureTarget.Texture2d, brdfLutTexture);
            Gl.TexImage2D(TextureTarget.Texture2d, 0, InternalFormat.Rg16f, 512, 512, 0, PixelFormat.Rg, 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.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
            Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);

            var captureFbo = Gl.GenFramebuffer();

            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, captureFbo);
            Gl.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2d, brdfLutTexture, 0);

            Gl.Viewport(0, 0, 512, 512);
            shader.Use();
            // Gl.ClearColor(1.0f, 1.0f, 1.0f, 1.0f);
            Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            Gl.Disable(EnableCap.DepthTest);
            Gl.Disable(EnableCap.CullFace);
            plane.DrawRaw();
            Gl.Enable(EnableCap.DepthTest);
            Gl.Enable(EnableCap.CullFace);

            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, 0);

            return(brdfLutTexture);
        }
Exemple #8
0
 public void BindFrameBuffer()
 {
     //Gl.BindTexture(TextureTarget.Texture2d, 0); //To make sure the texture isn't bound
     Gl.Viewport(0, 0, SHADOW_WIDTH, SHADOW_HEIGHT);
     Gl.BindFramebuffer(FramebufferTarget.Framebuffer, this.frameBuffer);
     Gl.Clear(ClearBufferMask.DepthBufferBit);
 }
Exemple #9
0
        public static uint RenderToCubemap(Shader shader, int resolution, Action preRender)
        {
            var cube       = new Cube();
            var captureFbo = Gl.GenFramebuffer();

            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, captureFbo);

            var colorTexture = Gl.GenTexture();

            Gl.BindTexture(TextureTarget.Texture2d, colorTexture);
            Gl.TexImage2D(TextureTarget.Texture2d, 0, InternalFormat.Srgb, Constants.renderWidth, Constants.renderHeight, 0, PixelFormat.Rgb, PixelType.Float, IntPtr.Zero);

            Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.NearestMipmapNearest);
            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.ColorAttachment0, TextureTarget.Texture2d, colorTexture, 0);

            var envCubemap = Gl.GenTexture();

            Gl.BindTexture(TextureTarget.TextureCubeMap, envCubemap);
            for (int i = 0; i < 6; ++i)
            {
                Gl.TexImage2D(TextureTarget.TextureCubeMapPositiveX + i, 0, InternalFormat.Srgb, resolution, resolution, 0, PixelFormat.Rgb, PixelType.Float, IntPtr.Zero);
            }

            Gl.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            Gl.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            Gl.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
            Gl.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);
            Gl.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapR, (int)TextureWrapMode.ClampToEdge);

            var projMatrix   = Matrix4x4f.Perspective(90f, 1.0f, 0.1f, 10.0f);
            var viewMatrices = new Matrix4x4f[]
            {
                Matrix4x4f.LookAt(new Vertex3f(0f, 0f, 0f), new Vertex3f(1f, 0f, 0f), new Vertex3f(0f, -1f, 0f)),
                Matrix4x4f.LookAt(new Vertex3f(0f, 0f, 0f), new Vertex3f(-1f, 0f, 0f), new Vertex3f(0f, -1f, 0f)),
                Matrix4x4f.LookAt(new Vertex3f(0f, 0f, 0f), new Vertex3f(0f, 1f, 0f), new Vertex3f(0f, 0f, 1f)),
                Matrix4x4f.LookAt(new Vertex3f(0f, 0f, 0f), new Vertex3f(0f, -1f, 0f), new Vertex3f(0f, 0f, -1f)),
                Matrix4x4f.LookAt(new Vertex3f(0f, 0f, 0f), new Vertex3f(0f, 0f, 1f), new Vertex3f(0f, -1f, 0f)),
                Matrix4x4f.LookAt(new Vertex3f(0f, 0f, 0f), new Vertex3f(0f, 0f, -1f), new Vertex3f(0f, -1f, 0f)),
            };

            shader.Use();
            preRender.Invoke();
            shader.SetMatrix("projectionMatrix", projMatrix);

            Gl.Viewport(0, 0, resolution, resolution);
            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, captureFbo);
            for (int i = 0; i < 6; ++i)
            {
                shader.SetMatrix("viewMatrix", viewMatrices[i]);
                Gl.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.TextureCubeMapPositiveX + i, envCubemap, 0);
                Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
                cube.Draw();
            }
            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
            return(envCubemap);
        }
        /// <inheritdoc />
        public override RenderTarget CreateRenderTarget(Vector2 size, bool smooth      = false, TextureInternalFormat internalFormat = TextureInternalFormat.Rgba,
                                                        TexturePixelFormat pixelFormat = TexturePixelFormat.Rgba, bool attachStencil = false)
        {
            RenderTarget resultTarget = null;

            InternalFormat intFormat = (InternalFormat)Enum.Parse(typeof(InternalFormat), internalFormat.ToString());
            PixelFormat    glFormat  = (PixelFormat)Enum.Parse(typeof(PixelFormat), pixelFormat.ToString());

            GLThread.ExecuteGLThread(() =>
            {
                // Create the FBO which rendering will be done to.
                uint newFbo = Gl.GenFramebuffer();
                Gl.BindFramebuffer(FramebufferTarget.Framebuffer, newFbo);

                // Create the texture.
                uint renderTexture = Gl.GenTexture();
                Gl.BindTexture(TextureTarget.Texture2d, renderTexture);
                Gl.TexImage2D(TextureTarget.Texture2d, 0, intFormat, (int)size.X, (int)size.Y, 0, glFormat,
                              PixelType.UnsignedByte, IntPtr.Zero);
                Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureMinFilter, smooth ? Gl.LINEAR : Gl.NEAREST);
                Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureMagFilter, smooth ? Gl.LINEAR : Gl.NEAREST);

                // Attach the texture to the frame buffer.
                Gl.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2d, renderTexture, 0);

                // Attach color components.
                int[] modes = { Gl.COLOR_ATTACHMENT0 };
                Gl.DrawBuffers(modes);

                // Create render buffer.
                uint depthBuffer = Gl.GenRenderbuffer();
                Gl.BindRenderbuffer(RenderbufferTarget.Renderbuffer, depthBuffer);
                Gl.RenderbufferStorage(RenderbufferTarget.Renderbuffer, InternalFormat.Depth24Stencil8, (int)size.X,
                                       (int)size.Y);
                Gl.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, attachStencil ? FramebufferAttachment.DepthStencilAttachment : FramebufferAttachment.DepthAttachment, RenderbufferTarget.Renderbuffer, depthBuffer);

                // Check status.
                FramebufferStatus status = Gl.CheckFramebufferStatus(FramebufferTarget.Framebuffer);
                if (status != FramebufferStatus.FramebufferComplete)
                {
                    Engine.Log.Warning($"Framebuffer creation failed. Error code {status}.", MessageSource.GL);
                }

                // Create the texture object.
                Texture targetTexture = new GLTexture(renderTexture, new Vector2(size.X, size.Y), null, $"FBO {newFbo} Texture");

                // Create the render target object.
                resultTarget = new GlRenderTarget(newFbo, size, targetTexture);

                // Clear the target.
                ClearScreen();

                CheckError("creating scale fbo");

                // Restore bindings and so on.
                Engine.Renderer?.EnsureRenderTarget();
            });

            return(resultTarget);
        }
Exemple #11
0
 /// <summary>
 /// フレームバッファのサイズを変更する。
 /// </summary>
 /// <param name="width">幅</param>
 /// <param name="height">高さ</param>
 public void Resize(int width, int height)
 {
     Gl.BindFramebuffer(FramebufferTarget.Framebuffer, FrameBufferId);
     Gl.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, 0, 0);
     Texture.Dispose();
     Texture = new CubismOpenGlNetTexture(width, height);
     Gl.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureId, 0);
     Gl.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
 }
        private void initOutputBuffer()
        {
            ParticleEditModel md = MainModel.ins.particleEditModel;

            //if(md == null || md.width == 0 || md.height == 0) {
            //	return;
            //}

            if (bufWidth == md.width && bufHeight == md.height)
            {
                return;
            }

            int w = md.width;
            int h = md.height;

            bufWidth  = w;
            bufHeight = h;

            bufferMVP = calcMVP(w, h);

            //Gl.Enable(EnableCap.FramebufferSrgb);

            if (mlBufferCache != null)
            {
                mlBufferCache.Dispose();
            }
            if (mlBufferOutput != null)
            {
                mlBufferOutput.Dispose();
            }

            int size = w * h * pxChannel;

            bufferCache   = new byte[size];
            mlBufferCache = new MemoryLock(bufferCache);

            bufferOutput   = new byte[size];
            mlBufferOutput = new MemoryLock(bufferOutput);

            //Gl.BufferData(BufferTarget.PixelPackBuffer, (uint)size, mlImageData.Address, BufferUsage.DynamicRead);

            Gl.BindTexture(TextureTarget.Texture2d, glbufTexCache);
            Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToBorder);
            Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToBorder);
            //Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureWrapR, (int)TextureWrapMode.Repeat);
            Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureMagFilter, (int)TextureMinFilter.Linear);
            Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            //Gl.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (int)LightEnvModeSGIX.Replace);

            Gl.TexImage2D(TextureTarget.Texture2d, 0, InternalFormat.Rgba, w, h, 0, OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, mlBufferCache.Address);
            Gl.BindTexture(TextureTarget.Texture2d, 0);

            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, glbufOutput);
            Gl.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2d, glbufTexCache, 0);
            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
        }
Exemple #13
0
        private uint CreateFrameBuffer()
        {
            uint frameBuffer = Gl.GenFramebuffer();

            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, frameBuffer);
            Gl.DrawBuffer(DrawBufferMode.None);
            Gl.ReadBuffer(0);
            return(frameBuffer);
        }
Exemple #14
0
        private uint CreateFrameBuffer()
        {
            uint frameBuffer = Gl.GenFramebuffer();

            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, frameBuffer);
            Gl.DrawBuffer(DrawBufferMode.Aux0); //Gl.GL_COLOR_ATTACHMENT0
            //indicate that we will always render to color attachment 0
            return(frameBuffer);
        }
Exemple #15
0
        /// <summary>
        /// Unbind this GraphicsSurface for drawing.
        /// </summary>
        /// <param name="ctx">
        /// A <see cref="GraphicsContext"/> to wich disassociate its rendering result from this GraphicsSurface.
        /// </param>
        public override void UnbindDraw(GraphicsContext ctx)
        {
            // Switch to default framebuffer
            Gl.BindFramebuffer(FramebufferTarget.DrawFramebuffer, InvalidObjectName);
#if !MONODROID
            Gl.DrawBuffers(Gl.BACK_LEFT);
#endif
            // Gl.DrawBuffer(DrawBufferMode.BackLeft);
        }
Exemple #16
0
        private void RenderFramebufferToScreen()
        {
            Gl.Viewport(0, 0, Constants.windowWidth, Constants.windowHeight);
            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
            Gl.ClearDepth(1.0f);
            Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            framebufferRenderShader.SetFloat("exposure", Constants.exposure);
            framebufferRenderPlane.Draw(framebufferRenderShader, mainFramebuffer.ColorTexture);
        }
Exemple #17
0
        /// <summary>
        /// Actually create this GraphicsResource resources.
        /// </summary>
        /// <param name="ctx">
        /// A <see cref="GraphicsContext"/> used for allocating resources.
        /// </param>
        protected override void CreateObject(GraphicsContext ctx)
        {
            int currentBinding;

            Gl.Get(Gl.READ_FRAMEBUFFER_BINDING, out currentBinding);
            Gl.BindFramebuffer(FramebufferTarget.ReadFramebuffer, ObjectName);

            // Restore previous Framebuffer bound
            Gl.BindFramebuffer(FramebufferTarget.ReadFramebuffer, (uint)currentBinding);
        }
Exemple #18
0
        private static void RenderOsdFramebuffer(DeviceContext deviceContext)
        {
            IntPtr glContext    = IntPtr.Zero;
            uint   framebuffer  = 0;
            uint   renderbuffer = 0;

            try {
                // Create context and make current on this thread
                if ((glContext = deviceContext.CreateContext(IntPtr.Zero)) == IntPtr.Zero)
                {
                    throw new System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error());                         // XXX
                }
                deviceContext.MakeCurrent(glContext);

                // Create framebuffer resources
                int w = Math.Min(800, Gl.CurrentLimits.MaxRenderbufferSize);
                int h = Math.Min(600, Gl.CurrentLimits.MaxRenderbufferSize);

                renderbuffer = Gl.GenRenderbuffer();
                Gl.BindRenderbuffer(RenderbufferTarget.Renderbuffer, renderbuffer);
                Gl.RenderbufferStorage(RenderbufferTarget.Renderbuffer, InternalFormat.Rgb8, w, h);

                framebuffer = Gl.GenFramebuffer();
                Gl.BindFramebuffer(FramebufferTarget.ReadFramebuffer, framebuffer);
                Gl.BindFramebuffer(FramebufferTarget.Framebuffer, framebuffer);
                Gl.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, RenderbufferTarget.Renderbuffer, renderbuffer);

                FramebufferStatus framebufferStatus = Gl.CheckFramebufferStatus(FramebufferTarget.Framebuffer);
                if (framebufferStatus != FramebufferStatus.FramebufferComplete)
                {
                    throw new InvalidOperationException("framebuffer not complete");
                }

                Gl.DrawBuffers(Gl.COLOR_ATTACHMENT0);

                RenderOsd(w, h);

                Gl.ReadBuffer(ReadBufferMode.ColorAttachment0);

                SnapshotOsd(w, h);
            } finally {
                if (renderbuffer != 0)
                {
                    Gl.DeleteRenderbuffers(renderbuffer);
                }
                if (framebuffer != 0)
                {
                    Gl.DeleteFramebuffers(framebuffer);
                }
                if (glContext != IntPtr.Zero)
                {
                    deviceContext.DeleteContext(glContext);
                }
            }
        }
        /// <inheritdoc />
        public override RenderTarget CreateMSAARenderTarget(int samples, Vector2 size, TextureInternalFormat internalFormat = TextureInternalFormat.Rgba, bool attachStencil = false)
        {
            RenderTarget resultTarget = null;

            InternalFormat intFormat = (InternalFormat)Enum.Parse(typeof(InternalFormat), internalFormat.ToString());

            GLThread.ExecuteGLThread(() =>
            {
                // Create the FBO.
                uint newFbo = Gl.GenFramebuffer();
                Gl.BindFramebuffer(FramebufferTarget.Framebuffer, newFbo);

                // Create the texture.
                uint renderTexture = Gl.GenTexture();
                Gl.BindTexture(TextureTarget.Texture2dMultisample, renderTexture);
                Gl.TexImage2DMultisample(TextureTarget.Texture2dMultisample, samples, intFormat, (int)size.X, (int)size.Y, true);

                // Attach the texture to the frame buffer.
                Gl.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2dMultisample, renderTexture, 0);

                // Attach color components.
                int[] modes = { Gl.COLOR_ATTACHMENT0 };
                Gl.DrawBuffers(modes);

                // Create render buffer.
                uint depthBuffer = Gl.GenRenderbuffer();
                Gl.BindRenderbuffer(RenderbufferTarget.Renderbuffer, depthBuffer);
                Gl.RenderbufferStorageMultisample(RenderbufferTarget.Renderbuffer, samples, InternalFormat.Depth24Stencil8, (int)size.X, (int)size.Y);
                Gl.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, attachStencil ? FramebufferAttachment.DepthStencilAttachment : FramebufferAttachment.DepthAttachment, RenderbufferTarget.Renderbuffer, depthBuffer);

                // Check status.
                FramebufferStatus status = Gl.CheckFramebufferStatus(FramebufferTarget.Framebuffer);
                if (status != FramebufferStatus.FramebufferComplete)
                {
                    Engine.Log.Warning($"MSAA Framebuffer creation failed. Error code {status}.", MessageSource.GL);
                }

                // Construct a texture object for it.
                Texture targetTexture = new GLTexture(renderTexture, new Vector2(size.X, size.Y), null, $"MSAA{samples} FBO Texture");

                // Create the render target object.
                resultTarget = new GlRenderTarget(newFbo, size, targetTexture);

                // Clear the target.
                ClearScreen();

                CheckError("creating msaa fbo");

                // Restore bindings and so on.
                Engine.Renderer?.EnsureRenderTarget();
            });

            return(resultTarget);
        }
Exemple #20
0
        /// <summary>
        /// クリッピングマスクの描画のためのフレームバッファを作成する。
        /// </summary>
        /// <param name="width">幅</param>
        /// <param name="height">高さ</param>
        public CubismOpenGlNetClippingMask(int width, int height)
        {
            // テクスチャを生成する
            Texture = new CubismOpenGlNetTexture(width, height);

            // フレームバッファを生成し、テクスチャを割り当てる
            uint[] fbos = new uint[1];
            Gl.GenFramebuffers(fbos);
            FrameBufferId = fbos[0];
            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, FrameBufferId);
            Gl.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureId, 0);
            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
        }
Exemple #21
0
        public void RenderFramebuffer()
        {
            var cameraComponent = MainCamera.GetComponent <CameraComponent>();

            Gl.Viewport(0, 0, GameSettings.GameResolutionX, GameSettings.GameResolutionY);
            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
            Gl.ClearDepth(1.0f);
            Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);

            framebufferRenderShader.SetFloat("exposure", RenderManager.Instance.exposure);
            framebufferRenderShader.SetInt("tonemapOperator", (int)RenderManager.Instance.tonemapOperator);
            framebufferRenderPlane.Draw(framebufferRenderShader, cameraComponent.Framebuffer.ColorTexture);
        }
Exemple #22
0
        void BindFramebuffer(FramebufferTarget Target)
        {
#if DEBUG
            FramebufferStatus S = Gl.CheckNamedFramebufferStatus(ID, Target);
            if (S != FramebufferStatus.FramebufferComplete)
            {
                throw new InvalidOperationException("Incomplete framebuffer");
            }
#endif

            this.Target = Target;
            Gl.BindFramebuffer(Target, ID);
        }
Exemple #23
0
        public void RenderShadows()
        {
            Gl.Disable(EnableCap.CullFace);

            Gl.Viewport(0, 0, (int)light.ShadowMap.Resolution.x, (int)light.ShadowMap.Resolution.y);
            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, light.ShadowMap.DepthMapFbo);
            Gl.Clear(ClearBufferMask.DepthBufferBit);

            light.Render();
            testModel.DrawShadows(light, depthShader);

            Gl.Enable(EnableCap.CullFace);

            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
        }
Exemple #24
0
        public override void BeginScene(Camera camera)
        {
            currentCamera = camera;
            MakeCurrent();
            var renderTexture = camera.targetTexture;

            if (renderTexture is null)
            {
                Gl.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
            }
            else
            {
                Gl.BindFramebuffer(FramebufferTarget.Framebuffer, frameBufferCache.TryRegister(new FrameBuffer(renderTexture)).fbo);
            }
        }
Exemple #25
0
        private void RenderSceneToFramebuffer()
        {
            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, mainFramebuffer.Fbo);
            Gl.Viewport(0, 0, Constants.renderWidth, Constants.renderHeight);
            Gl.ClearDepth(0.0f);
            Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            Gl.ClipControl(ClipControlOrigin.LowerLeft, ClipControlDepth.ZeroToOne);
            Gl.DepthFunc(DepthFunction.Greater);

            camera.Render();
            DrawSkybox();
            testModel.Draw(camera, shader, light, (skybox, convolutedSkybox, prefilteredSkybox), brdfLut);

            Gl.DepthFunc(DepthFunction.Less);
            Gl.ClipControl(ClipControlOrigin.LowerLeft, ClipControlDepth.NegativeOneToOne);
        }
Exemple #26
0
        protected override FrameBufferData OnRegister(FrameBuffer key)
        {
            uint fbo = Gl.CreateFramebuffer();

            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, fbo);
            Gl.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2d, (device as OpenGLDevice).textureCache.TryRegister(key.renderTexture), 0);
            uint depthTexture = Gl.GenTexture();

            Gl.BindTexture(TextureTarget.Texture2d, depthTexture);
            Gl.TexImage2D(TextureTarget.Texture2d, 0, InternalFormat.DepthComponent32, (int)key.renderTexture.width, (int)key.renderTexture.height, 0, PixelFormat.DepthComponent, PixelType.Float, IntPtr.Zero);
            Gl.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, TextureTarget.Texture2d, depthTexture, 0);
            return(new FrameBufferData()
            {
                fbo = fbo, depthTexture = depthTexture
            });
        }
Exemple #27
0
        private void ApplyRenderTarget()
        {
            uint target = _state.RenderTargetBase;

            if (target != _actualRenderTarget)
            {
                if (target != 0)
                {
                    // TODO set this uint antiAlias  = _state.RenderTargetAntiAlias;
                    if (!_framebufferCache.TryGetValue(target, out var framebuffer))
                    {
                        uint[] fb = new uint[1];
                        Gl.GenFramebuffers(fb);
                        framebuffer = fb[0];
                        Gl.BindFramebuffer(FramebufferTarget.Framebuffer, framebuffer);

                        Gl.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2d, target, 0);

                        if (Gl.CheckFramebufferStatus(FramebufferTarget.Framebuffer) != FramebufferStatus.FramebufferComplete)
                        {
                            Debug.WriteLine("Failed to create framebuffer for render texture");
                        }
                        _framebufferCache.Add(target, framebuffer);
                    }
                    else
                    {
                        if (framebuffer == 0)
                        {
                            throw new InvalidOperationException("Attempted to bind framebuffer ID 0");
                        }
                        Gl.BindFramebuffer(FramebufferTarget.Framebuffer, framebuffer);
                    }
                    Gl.Viewport(0, 0, (int)_state.RenderTarget.NativeWidth, (int)_state.RenderTarget.NativeHeight);
                }
                else
                {
                    // TODO: double check these on a device, the ifdef seems to be unneeded
#if __IOS__
                    Gl.BindFramebuffer(FramebufferTarget.Framebuffer, 1);
#else
                    Gl.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
#endif
                    Gl.Viewport(0, 0, (int)_backBufferWidth, (int)_backBufferHeight);
                }
                _actualRenderTarget = target;
            }
        }
Exemple #28
0
		private static void RenderOsdFramebuffer(DeviceContext deviceContext)
		{
			IntPtr glContext = IntPtr.Zero;
			uint framebuffer = 0;
			uint renderbuffer = 0;

			try {
				// Create context and make current on this thread
				if ((glContext = deviceContext.CreateContext(IntPtr.Zero)) == IntPtr.Zero)
					throw new System.ComponentModel.Win32Exception(System.Runtime.InteropServices.Marshal.GetLastWin32Error());	// XXX
				deviceContext.MakeCurrent(glContext);

				// Create framebuffer resources
				int w = Math.Min(800, Gl.CurrentLimits.MaxRenderBufferSize);
				int h = Math.Min(600, Gl.CurrentLimits.MaxRenderBufferSize);

				renderbuffer = Gl.GenRenderbuffer();
				Gl.BindRenderbuffer(Gl.RENDERBUFFER, renderbuffer);
				Gl.RenderbufferStorage(Gl.RENDERBUFFER, Gl.RGB8, w, h);

				framebuffer = Gl.GenFramebuffer();
				Gl.BindFramebuffer(Gl.READ_FRAMEBUFFER, framebuffer);
				Gl.BindFramebuffer(Gl.FRAMEBUFFER, framebuffer);
				Gl.FramebufferRenderbuffer(Gl.FRAMEBUFFER, Gl.COLOR_ATTACHMENT0, Gl.RENDERBUFFER, renderbuffer);

				int framebufferStatus = Gl.CheckFramebufferStatus(Gl.FRAMEBUFFER);
				if (framebufferStatus != Gl.FRAMEBUFFER_COMPLETE)
					throw new InvalidOperationException("framebuffer not complete");

				Gl.DrawBuffers(Gl.COLOR_ATTACHMENT0);

				RenderOsd(w, h);

				Gl.ReadBuffer(ReadBufferMode.ColorAttachment0);

				SnapshotOsd(w, h);

			} finally {
				if (renderbuffer != 0)
					Gl.DeleteRenderbuffers(renderbuffer);
				if (framebuffer != 0)
					Gl.DeleteFramebuffers(framebuffer);
				if (glContext != IntPtr.Zero)
					deviceContext.DeleteContext(glContext);
			}
		}
        /// <inheritdoc />
        public override void BindRenderTarget(RenderTarget t)
        {
            GLThread.ExecuteGLThread(() =>
            {
                // Check if restoring the window target.
                if (t == null)
                {
                    Gl.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
                    Gl.Viewport((int)Viewport.X, (int)Viewport.Y, (int)Viewport.Width, (int)Viewport.Height);
                    return;
                }

                GlRenderTarget target = (GlRenderTarget)t;
                Gl.BindFramebuffer(FramebufferTarget.Framebuffer, target.Pointer);
                Gl.Viewport((int)target.Viewport.X, (int)target.Viewport.Y, (int)target.Viewport.Width, (int)target.Viewport.Height);
            });
        }
Exemple #30
0
        private void RenderSceneToFramebuffer()
        {
            var cameraComponent = MainCamera.GetComponent <CameraComponent>();

            Gl.BindFramebuffer(FramebufferTarget.Framebuffer, cameraComponent.Framebuffer.Fbo);
            Gl.Viewport(0, 0, (int)cameraComponent.Resolution.X, (int)cameraComponent.Resolution.Y);
            Gl.ClearDepth(0.0f);
            Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            Gl.ClipControl(ClipControlOrigin.LowerLeft, ClipControlDepth.ZeroToOne);
            Gl.DepthFunc(DepthFunction.Greater);

            MainCamera.Render();
            var skyboxComponent = skyboxEntity.GetComponent <SkyboxComponent>();

            skyboxComponent.DrawSkybox(MainCamera);

            var templateDrawInfo = new Mesh.DrawInfo()
            {
                Camera      = MainCamera,
                Light       = Lights[0],
                PbrCubemaps = (skyboxComponent.skybox, skyboxComponent.convolutedSkybox, skyboxComponent.prefilteredSkybox),
                BrdfLut     = brdfLut,
                HoloTexture = holoTexture,
                ProjMatrix  = MainCamera.GetComponent <CameraComponent>().ProjMatrix,
                ViewMatrix  = MainCamera.GetComponent <CameraComponent>().ViewMatrix
            };

            foreach (var entity in Entities)
            {
                if (!entity.HasComponent <ModelComponent>())
                {
                    continue;
                }

                var modelComponent = entity.GetComponent <ModelComponent>();
                var newDrawInfo    = new Mesh.DrawInfo(templateDrawInfo);
                newDrawInfo.Shader      = entity.GetComponent <ShaderComponent>();
                newDrawInfo.ModelMatrix = entity.GetComponent <TransformComponent>().Matrix;

                modelComponent.Draw(newDrawInfo);
            }

            Gl.DepthFunc(DepthFunction.Less);
            Gl.ClipControl(ClipControlOrigin.LowerLeft, ClipControlDepth.NegativeOneToOne);
        }