예제 #1
0
파일: Node.cs 프로젝트: hglei/Materia
 protected virtual void CreateBufferIfNeeded()
 {
     if (buffer == null || buffer.Id == 0)
     {
         buffer = new GLTextuer2D((GLInterfaces.PixelInternalFormat)((int)internalPixelType));
         buffer.Bind();
         buffer.SetData(IntPtr.Zero, GLInterfaces.PixelFormat.Rgba, width, height);
         buffer.Linear();
         buffer.Repeat();
         if (internalPixelType == GraphPixelType.Luminance16F || internalPixelType == GraphPixelType.Luminance32F)
         {
             buffer.SetSwizzleLuminance();
         }
         GLTextuer2D.Unbind();
     }
 }
예제 #2
0
파일: FXNode.cs 프로젝트: kergalym/Materia
        protected override void CreateBufferIfNeeded()
        {
            base.CreateBufferIfNeeded();

            if (temp == null || temp.Id == 0)
            {
                temp = new GLTextuer2D((GLInterfaces.PixelInternalFormat)((int)internalPixelType));
                temp.Bind();
                temp.SetData(IntPtr.Zero, GLInterfaces.PixelFormat.Rgba, width, height);
                temp.Linear();
                temp.Repeat();
                if (internalPixelType == GraphPixelType.Luminance16F || internalPixelType == GraphPixelType.Luminance32F)
                {
                    temp.SetSwizzleLuminance();
                }
                GLTextuer2D.Unbind();
            }
        }
예제 #3
0
        protected void CreateBuffersIfNeeded()
        {
            if (resizeProcessor == null)
            {
                resizeProcessor = new PreviewProcessor();
            }
            if (renderBuff == null)
            {
                renderBuff = new GLRenderBuffer();
                renderBuff.Bind();
                renderBuff.SetBufferStorageAsDepth(4096, 4096);
                GLRenderBuffer.Unbind();
            }
            if (colorBuff == null)
            {
                //colorbuff part of the framebuffer is always Rgba32f to support all texture formats
                //that could be rendered into it
                colorBuff = new GLTextuer2D(PixelInternalFormat.Rgba32f);
                colorBuff.Bind();
                colorBuff.SetData(IntPtr.Zero, PixelFormat.Rgba, 4096, 4096);
                colorBuff.Nearest();
                colorBuff.Repeat();
                GLTextuer2D.Unbind();
            }
            if (frameBuff == null)
            {
                frameBuff = new GLFrameBuffer();
                frameBuff.Bind();
                frameBuff.AttachColor(colorBuff);
                frameBuff.AttachDepth(renderBuff);
                IGL.Primary.DrawBuffers(new int[] { (int)DrawBufferMode.ColorAttachment0 });
                IGL.Primary.ReadBuffer((int)ReadBufferMode.ColorAttachment0);

                if (!frameBuff.IsValid)
                {
                    var status = IGL.Primary.CheckFramebufferStatus((int)FramebufferTarget.Framebuffer);
                    GLFrameBuffer.Unbind();
                    return;
                }

                GLFrameBuffer.Unbind();
            }
        }
예제 #4
0
        protected void ApplyTransformNoAuto(GLTextuer2D inc, GLTextuer2D o, int owidth, int oheight, MVector translation, MVector scale, float angle, MVector pivot)
        {
            Matrix4 proj    = Matrix4.CreateOrthographic(owidth, oheight, 0.03f, 1000f);
            Matrix4 pTrans  = Matrix4.CreateTranslation(-pivot.X, -pivot.Y, 0);
            Matrix4 iPTrans = Matrix4.CreateTranslation(pivot.X, pivot.Y, 0);
            Matrix4 trans   = Matrix4.CreateTranslation(translation.X, translation.Y, 0);
            Matrix4 sm      = Matrix4.CreateScale(scale.X, scale.Y, 1);
            Matrix4 rot     = Matrix4.CreateRotationZ(angle);
            Matrix4 model   = pTrans * sm * rot * iPTrans * trans;

            Matrix4 view = Matrix4.LookAt(new Vector3(0, 0, 1), Vector3.Zero, Vector3.UnitY);

            IGL.Primary.Viewport(0, 0, owidth, oheight);

            resizeProcessor.Model      = model;
            resizeProcessor.View       = view;
            resizeProcessor.Projection = proj;
            resizeProcessor.Luminosity = Luminosity;

            resizeProcessor.Bind(inc);

            inc.ClampToEdge();

            if (renderQuad != null)
            {
                renderQuad.Draw();
            }

            inc.Repeat();
            resizeProcessor.Unbind();

            o.Bind();
            o.Repeat();
            GLTextuer2D.Unbind();

            Blit(o, owidth, oheight);

            /*o.Bind();
             * o.Repeat();
             * o.CopyFromFrameBuffer(owidth, oheight);
             * GLTextuer2D.Unbind();*/
        }
예제 #5
0
        public void Process(int width, int height, GLTextuer2D tex, GLTextuer2D warp, GLTextuer2D output)
        {
            base.Process(width, height, tex, output);

            if (shader != null)
            {
                ResizeViewTo(tex, output, tex.Width, tex.Height, width, height);
                tex = output;
                IGL.Primary.Clear((int)(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit));

                Vector2 tiling = new Vector2(TileX, TileY);

                shader.Use();
                shader.SetUniform2("tiling", ref tiling);
                shader.SetUniform("MainTex", 0);
                shader.SetUniform("intensity", Intensity);
                IGL.Primary.ActiveTexture((int)TextureUnit.Texture0);
                tex.Bind();
                tex.ClampToEdge();
                shader.SetUniform("Warp", 1);
                IGL.Primary.ActiveTexture((int)TextureUnit.Texture1);
                warp.Bind();

                if (renderQuad != null)
                {
                    renderQuad.Draw();
                }

                tex.Bind();
                tex.Repeat();

                GLTextuer2D.Unbind();
                //output.Bind();
                //output.CopyFromFrameBuffer(width, height);
                //GLTextuer2D.Unbind();
                Blit(output, width, height);
            }
        }
예제 #6
0
        public void Process(int width, int height, GLTextuer2D tex, GLTextuer2D source, GLTextuer2D output)
        {
            base.Process(width, height, tex, output);

            if (Shader != null && PreShader != null)
            {
                GLTextuer2D temp = new GLTextuer2D(output.InternalFormat);
                temp.Bind();
                temp.SetData(IntPtr.Zero, PixelFormat.Rgba, width, height);
                temp.Linear();
                temp.Repeat();
                GLTextuer2D.Unbind();
                GLTextuer2D temp2 = new GLTextuer2D(output.InternalFormat);
                temp2.Bind();
                temp2.SetData(IntPtr.Zero, PixelFormat.Rgba, width, height);
                temp2.Linear();
                temp2.Repeat();
                GLTextuer2D.Unbind();
                GLTextuer2D temp3 = new GLTextuer2D(PixelInternalFormat.Rgba32f);
                temp3.Bind();
                temp3.SetData(IntPtr.Zero, PixelFormat.Rgba, width + 1, height + 1);
                temp3.Nearest();
                temp3.ClampToEdge();
                GLTextuer2D.Unbind();

                ResizeViewTo(tex, temp, tex.Width, tex.Height, width, height);

                IGL.Primary.Clear((int)(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit));

                if (source != null)
                {
                    ResizeViewTo(source, temp2, source.Width, source.Height, width, height);
                }

                IGL.Primary.Clear((int)(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit));


                tex = temp;
                PreShader.Use();
                output.Bind();
                output.BindAsImage(0, true, true);
                tex.Bind();
                tex.BindAsImage(1, true, true);
                temp2.Bind();
                temp2.BindAsImage(2, true, true);
                temp3.Bind();
                temp3.BindAsImage(3, true, true);

                PreShader.SetUniform("width", (float)width);
                PreShader.SetUniform("height", (float)height);

                PreShader.SetUniform("sourceOnly", SourceOnly);
                PreShader.SetUniform("maxDistance", Distance);

                //stage 1 convert temp input range
                //into proper format
                //for dt
                PreShader.SetUniform("stage", (int)0);
                IGL.Primary.DispatchCompute(width / 8, height / 8, 1);

                Shader.Use();
                output.Bind();
                output.BindAsImage(0, true, true);
                tex.Bind();
                tex.BindAsImage(1, true, true);
                temp2.Bind();
                temp2.BindAsImage(2, true, true);
                temp3.Bind();
                temp3.BindAsImage(3, true, true);

                Shader.SetUniform("width", (float)width);
                Shader.SetUniform("height", (float)height);

                Shader.SetUniform("sourceOnly", SourceOnly);
                Shader.SetUniform("maxDistance", Distance);

                //stage 2 run column transform
                Shader.SetUniform("stage", (int)0);
                IGL.Primary.DispatchCompute(width / 8, 1, 1);

                //stage 3 run row transform
                Shader.SetUniform("stage", (int)1);
                IGL.Primary.DispatchCompute(height / 8, 1, 1);


                PreShader.Use();
                output.Bind();
                output.BindAsImage(0, true, true);
                tex.Bind();
                tex.BindAsImage(1, true, true);
                temp2.Bind();
                temp2.BindAsImage(2, true, true);
                temp3.Bind();
                temp3.BindAsImage(3, true, true);

                PreShader.SetUniform("width", (float)width);
                PreShader.SetUniform("height", (float)height);

                PreShader.SetUniform("sourceOnly", SourceOnly);
                PreShader.SetUniform("maxDistance", Distance);

                //stage 4 finalize with sqrt() etc
                PreShader.SetUniform("stage", (int)1);
                IGL.Primary.DispatchCompute(width / 8, height / 8, 1);

                GLTextuer2D.UnbindAsImage(0);
                GLTextuer2D.UnbindAsImage(1);
                GLTextuer2D.UnbindAsImage(2);
                GLTextuer2D.UnbindAsImage(3);
                GLTextuer2D.Unbind();

                temp.Release();
                temp2.Release();
                temp3.Release();
            }
        }
예제 #7
0
        /// <summary>
        /// This would be too slow for real time performance at 60fps
        /// but since we are not doing 60fps it is fine
        /// it is a fast box gaussian blur
        /// produces results better than adobes gaussian blur
        /// and faster than theirs!
        /// can easily blur 4kx4k textures in low ms on a 1080 gtx gpu with 64+ intensity
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="tex"></param>
        /// <param name="output"></param>
        public override void Process(int width, int height, GLTextuer2D tex, GLTextuer2D output)
        {
            base.Process(width, height, tex, output);

            colorBuff.Bind();
            colorBuff.ClampToEdge();
            GLTextuer2D.Unbind();

            if (shader != null)
            {
                ResizeViewTo(tex, output, tex.Width, tex.Height, width, height);
                tex = output;
                IGL.Primary.Clear((int)(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit));

                Vector2 tiling = new Vector2(TileX, TileY);
                float[] boxes  = Materia.Nodes.Helpers.Blur.BoxesForGaussian(Intensity, 3);

                shader.Use();
                shader.SetUniform2("tiling", ref tiling);
                shader.SetUniform("MainTex", 0);
                shader.SetUniform("horizontal", true);
                shader.SetUniform("intensity", (boxes[0] - 1.0f) / 2.0f);
                IGL.Primary.ActiveTexture((int)TextureUnit.Texture0);
                tex.Bind();

                //clamp to prevent blur wrap
                tex.ClampToEdge();

                if (renderQuad != null)
                {
                    renderQuad.Draw();
                }

                GLTextuer2D.Unbind();

                /*output.Bind();
                 * output.CopyFromFrameBuffer(width, height);
                 * GLTextuer2D.Unbind();*/
                Blit(output, width, height);

                tex.Bind();
                tex.Repeat();
                GLTextuer2D.Unbind();

                shader.SetUniform("horizontal", false);
                IGL.Primary.ActiveTexture((int)TextureUnit.Texture0);
                output.Bind();

                //clamp to prevent blur wrap
                output.ClampToEdge();

                IGL.Primary.Clear((int)ClearBufferMask.ColorBufferBit);
                IGL.Primary.Clear((int)ClearBufferMask.DepthBufferBit);

                if (renderQuad != null)
                {
                    renderQuad.Draw();
                }

                GLTextuer2D.Unbind();

                /*output.Bind();
                 * output.CopyFromFrameBuffer(width, height);
                 * GLTextuer2D.Unbind();*/
                Blit(output, width, height);

                //begin second box
                shader.SetUniform("horizontal", true);
                shader.SetUniform("intensity", (boxes[1] - 1.0f) / 2.0f);
                IGL.Primary.ActiveTexture((int)TextureUnit.Texture0);
                output.Bind();

                IGL.Primary.Clear((int)ClearBufferMask.ColorBufferBit);
                IGL.Primary.Clear((int)ClearBufferMask.DepthBufferBit);

                if (renderQuad != null)
                {
                    renderQuad.Draw();
                }

                GLTextuer2D.Unbind();
                output.Bind();
                output.CopyFromFrameBuffer(width, height);
                GLTextuer2D.Unbind();

                shader.SetUniform("horizontal", false);
                shader.SetUniform("intensity", (boxes[1] - 1.0f) / 2.0f);
                IGL.Primary.ActiveTexture((int)TextureUnit.Texture0);
                output.Bind();

                IGL.Primary.Clear((int)ClearBufferMask.ColorBufferBit);
                IGL.Primary.Clear((int)ClearBufferMask.DepthBufferBit);

                if (renderQuad != null)
                {
                    renderQuad.Draw();
                }

                GLTextuer2D.Unbind();

                /*output.Bind();
                 * output.CopyFromFrameBuffer(width, height);
                 * GLTextuer2D.Unbind();
                 */

                Blit(output, width, height);

                //begin third box
                shader.SetUniform("horizontal", true);
                shader.SetUniform("intensity", (boxes[2] - 1.0f) / 2.0f);
                IGL.Primary.ActiveTexture((int)TextureUnit.Texture0);
                output.Bind();

                IGL.Primary.Clear((int)ClearBufferMask.ColorBufferBit);
                IGL.Primary.Clear((int)ClearBufferMask.DepthBufferBit);

                if (renderQuad != null)
                {
                    renderQuad.Draw();
                }

                GLTextuer2D.Unbind();

                /*output.Bind();
                 * output.CopyFromFrameBuffer(width, height);
                 * GLTextuer2D.Unbind();
                 */

                Blit(output, width, height);

                shader.SetUniform("horizontal", false);
                shader.SetUniform("intensity", (boxes[2] - 1.0f) / 2.0f);
                IGL.Primary.ActiveTexture((int)TextureUnit.Texture0);
                output.Bind();

                IGL.Primary.Clear((int)ClearBufferMask.ColorBufferBit);
                IGL.Primary.Clear((int)ClearBufferMask.DepthBufferBit);

                if (renderQuad != null)
                {
                    renderQuad.Draw();
                }

                GLTextuer2D.Unbind();

                /*output.Bind();
                 * output.CopyFromFrameBuffer(width, height);
                 * GLTextuer2D.Unbind();*/
                Blit(output, width, height);
            }

            output.Bind();
            output.Repeat();
            GLTextuer2D.Unbind();

            colorBuff.Bind();
            colorBuff.Repeat();
            GLTextuer2D.Unbind();
        }