Exemple #1
0
        private void SetupNonMultisampled()
        {
            // Generate FBO
            if (this.handleMainFBO == null)
            {
                this.handleMainFBO = GraphicsBackend.GL.CreateFramebuffer();
            }
            GraphicsBackend.GL.BindFramebuffer(WebGLRenderingContextBase.FRAMEBUFFER, this.handleMainFBO);

            // Attach textures
            int oglWidth  = 0;
            int oglHeight = 0;

            for (int i = 0; i < this.targetInfos.Count; i++)
            {
                NativeTexture tex = this.targetInfos[i].Target;

                uint attachment = (WebGLRenderingContextBase.COLOR_ATTACHMENT0 + (uint)i);
                GraphicsBackend.GL.FramebufferTexture2D(
                    WebGLRenderingContextBase.FRAMEBUFFER,
                    attachment,
                    WebGLRenderingContextBase.TEXTURE_2D,
                    tex.Handle,
                    0);
                oglWidth  = tex.Width;
                oglHeight = tex.Height;
            }

            // Generate or delete depth renderbuffer
            if (this.depthBuffer)
            {
                if (this.handleDepthRBO == null)
                {
                    this.handleDepthRBO = GraphicsBackend.GL.CreateRenderbuffer();
                }
                GraphicsBackend.GL.BindRenderbuffer(WebGLRenderingContextBase.RENDERBUFFER, this.handleDepthRBO);
                GraphicsBackend.GL.RenderbufferStorage(WebGLRenderingContextBase.RENDERBUFFER, WebGL2RenderingContextBase.DEPTH_COMPONENT24, oglWidth, oglHeight);
                GraphicsBackend.GL.FramebufferRenderbuffer(WebGLRenderingContextBase.FRAMEBUFFER, WebGLRenderingContextBase.DEPTH_ATTACHMENT, WebGLRenderingContextBase.RENDERBUFFER, this.handleDepthRBO);
            }
            else
            {
                GraphicsBackend.GL.FramebufferRenderbuffer(WebGLRenderingContextBase.FRAMEBUFFER, WebGLRenderingContextBase.DEPTH_ATTACHMENT, WebGLRenderingContextBase.RENDERBUFFER, null);
                if (this.handleDepthRBO != null)
                {
                    GraphicsBackend.GL.DeleteRenderbuffer(this.handleDepthRBO);
                }
                this.handleDepthRBO = null;
            }

            // Check status
            int status = GraphicsBackend.GL.CheckFramebufferStatus(WebGLRenderingContextBase.FRAMEBUFFER);

            if (status != WebGLRenderingContextBase.FRAMEBUFFER_COMPLETE)
            {
                throw new BackendException(string.Format("Incomplete Framebuffer: {0}", status));
            }

            GraphicsBackend.GL.BindRenderbuffer(WebGLRenderingContextBase.RENDERBUFFER, null);
            GraphicsBackend.GL.BindFramebuffer(WebGLRenderingContextBase.FRAMEBUFFER, null);
        }
Exemple #2
0
        public texReader(WebGLRenderingContext webgl, WebGLTexture texRGBA, int width, int height, bool gray = true)
        {
            this.gray   = gray;
            this.width  = width;
            this.height = height;

            var fbo = webgl.CreateFramebuffer();
            WebGLFramebuffer fbold = webgl.GetParameter(webgl.FRAMEBUFFER_BINDING) as WebGLFramebuffer;

            webgl.BindFramebuffer(webgl.FRAMEBUFFER, fbo);
            webgl.FramebufferTexture2D(webgl.FRAMEBUFFER, webgl.COLOR_ATTACHMENT0, webgl.TEXTURE_2D,
                                       texRGBA, 0);

            var readData = new Uint8Array(this.width * this.height * 4);

            readData[0] = 2;
            webgl.ReadPixels(0, 0, this.width, this.height, webgl.RGBA, webgl.UNSIGNED_BYTE,
                             readData);
            webgl.DeleteFramebuffer(fbo);
            webgl.BindFramebuffer(webgl.FRAMEBUFFER, fbold);

            if (gray)
            {
                this.data = new Uint8Array(this.width * this.height);
                for (var i = 0; i < width * height; i++)
                {
                    this.data[i] = readData[i * 4];
                }
            }
            else
            {
                this.data = readData;
            }
        }
 internal void DisposeFramebuffer(WebGLFramebuffer handle)
 {
     if (!_isDisposed)
     {
         lock (_disposeActionsLock)
         {
             _disposeNextFrame.Add(ResourceHandle.Framebuffer(handle));
         }
     }
 }
Exemple #4
0
        void IDisposable.Dispose()
        {
            if (DualityApp.ExecContext == DualityApp.ExecutionContext.Terminated)
            {
                return;
            }

            // If there are changes pending to be applied to the bound textures,
            // they should be executed before the render target is gone.
            this.ApplyPostRender();

            if (this.handleMainFBO != null)
            {
                //GraphicsBackend.GL.DeleteFramebuffer(this.handleMainFBO);
                try {
                    GraphicsBackend.GL.DeleteFramebuffer(this.handleMainFBO);
                } catch (Exception ex) {
                    Console.WriteLine("DeleteFramebuffer() failed: " + this.handleMainFBO + " | " + ex);
                }

                this.handleMainFBO = null;
            }
            if (this.handleDepthRBO != null)
            {
                //GraphicsBackend.GL.DeleteRenderbuffer(this.handleDepthRBO);
                try {
                    GraphicsBackend.GL.DeleteRenderbuffer(this.handleDepthRBO);
                } catch (Exception ex) {
                    Console.WriteLine("DeleteRenderbuffer() failed: " + this.handleDepthRBO + " | " + ex);
                }

                this.handleDepthRBO = null;
            }
            if (this.handleMsaaFBO != null)
            {
                //GraphicsBackend.GL.DeleteFramebuffer(this.handleMsaaFBO);
                try {
                    GraphicsBackend.GL.DeleteFramebuffer(this.handleMsaaFBO);
                } catch (Exception ex) {
                    Console.WriteLine("DeleteFramebuffer() failed: " + this.handleMsaaFBO + " | " + ex);
                }

                this.handleMsaaFBO = null;
            }
            for (int i = 0; i < this.targetInfos.Count; i++)
            {
                if (this.targetInfos.Data[i].HandleMsaaColorRBO != null)
                {
                    GraphicsBackend.GL.DeleteRenderbuffer(this.targetInfos.Data[i].HandleMsaaColorRBO);
                    this.targetInfos.Data[i].HandleMsaaColorRBO = null;
                }
            }
        }
Exemple #5
0
        public WebGL2Framebuffer(int width, int height, int colourTextures, TextureFormat textureColourFormat, TextureFormat?textureDepthFormat = null)
        {
            if (colourTextures < 1)
            {
                throw new CKGLException("Must have at least 1 colour texture.");
            }
            if (colourTextures > (int)GL.getParameter(MAX_COLOR_ATTACHMENTS_Static) || colourTextures > (int)GL.getParameter(MAX_DRAW_BUFFERS_Static))
            {
                throw new CKGLException("Too many colour textures.");
            }
            if (textureColourFormat.ToWebGL2PixelFormat() == DEPTH_COMPONENT || textureColourFormat.ToWebGL2PixelFormat() == DEPTH_STENCIL)
            {
                throw new CKGLException("textureColourFormat cannot be a depth(stencil) texture.");
            }
            if (textureDepthFormat.HasValue && !(textureDepthFormat.Value.ToWebGL2PixelFormat() == DEPTH_COMPONENT || textureDepthFormat.Value.ToWebGL2PixelFormat() == DEPTH_STENCIL))
            {
                throw new CKGLException("textureDepthFormat is not a depth(stencil) texture.");
            }

            this.width  = width;
            this.height = height;

            camera2D.Width  = width;
            camera2D.Height = height;

            id = GL.createFramebuffer();

            Bind();

            Textures = new Texture[colourTextures];
            double[] drawBuffers = new double[colourTextures];
            for (int i = 0; i < colourTextures; i++)
            {
                Textures[i]    = Texture.Create2D(Width, Height, textureColourFormat);
                drawBuffers[i] = COLOR_ATTACHMENT0 + i;
                GL.framebufferTexture2D(FRAMEBUFFER, COLOR_ATTACHMENT0 + i, (Textures[i] as WebGL2Texture).TextureTarget, (Textures[i] as WebGL2Texture).ID, 0);
                Textures[i].Unbind();
                CheckStatus();
            }
            GL.drawBuffers(drawBuffers);
            CheckStatus();

            if (textureDepthFormat.HasValue)
            {
                DepthStencilTexture = Texture.Create2D(Width, Height, textureDepthFormat.Value);
                GL.framebufferTexture2D(FRAMEBUFFER, textureDepthFormat.Value.ToWebGL2TextureAttachment(), (DepthStencilTexture as WebGL2Texture).TextureTarget, (DepthStencilTexture as WebGL2Texture).ID, 0);
                DepthStencilTexture.Unbind();
                CheckStatus();
            }
        }
        public void bindFramebuffer(int target, WebGLFramebuffer framebuffer)
        {
            var bufferId = (int)(framebuffer != null ? framebuffer.Value : 0);

#if _DEBUG
            Log.Info(string.Format("bindFramebuffer {0} {1}", target, bufferId));
#endif

#if GLEW_STATIC
            Gl.glBindFramebuffer(target, bufferId);
#else
            Gl.__glewBindFramebuffer(target, bufferId);
#endif
            this.ErrorTest();
        }
        internal void PlatformDeleteRenderTarget(IRenderTarget renderTarget)
        {
            var color   = renderTarget.GLColorBuffer;
            var depth   = renderTarget.GLDepthBuffer;
            var stencil = renderTarget.GLStencilBuffer;

            if (color != null)
            {
                this.framebufferHelper.DeleteRenderbuffer(color);

                if (stencil != null && stencil != depth)
                {
                    this.framebufferHelper.DeleteRenderbuffer(stencil);
                }
                if (depth != null)
                {
                    this.framebufferHelper.DeleteRenderbuffer(depth);
                }

                var bindingsToDelete = new List <RenderTargetBinding[]>();
                foreach (var bindings in this.glFramebuffers.Keys)
                {
                    foreach (var binding in bindings)
                    {
                        if (binding.RenderTarget == renderTarget)
                        {
                            bindingsToDelete.Add(bindings);
                            break;
                        }
                    }
                }

                foreach (var bindings in bindingsToDelete)
                {
                    WebGLFramebuffer fbo = null;
                    if (this.glFramebuffers.TryGetValue(bindings, out fbo))
                    {
                        this.framebufferHelper.DeleteFramebuffer(fbo);
                        this.glFramebuffers.Remove(bindings);
                    }
                    if (this.glResolveFramebuffers.TryGetValue(bindings, out fbo))
                    {
                        this.framebufferHelper.DeleteFramebuffer(fbo);
                        this.glResolveFramebuffers.Remove(bindings);
                    }
                }
            }
        }
Exemple #8
0
        public override void Destroy()
        {
            if (!IsDefault)
            {
                for (int i = Textures.Length; i == 0; i--)
                {
                    Textures[i]?.Destroy();
                    Textures[i] = null;
                }

                DepthStencilTexture?.Destroy();
                DepthStencilTexture = null;

                if (id != null)
                {
                    GL.deleteFramebuffer(id);
                    id = null;
                }
            }
        }
Exemple #9
0
 public bool isFramebuffer(WebGLFramebuffer framebuffer)
 {
     return(default(bool));
 }
Exemple #10
0
 public void deleteFramebuffer(WebGLFramebuffer framebuffer)
 {
 }
Exemple #11
0
 public void bindFramebuffer(int target, WebGLFramebuffer framebuffer)
 {
 }
 internal virtual void DeleteFramebuffer(WebGLFramebuffer framebuffer)
 {
     gl.deleteFramebuffer(framebuffer);
     GraphicsExtensions.CheckGLError();
 }
 /// <summary>
 /// Deletes a specific WebGLFramebuffer object. If you delete the currently bound framebuffer, the default framebuffer will be bound. Deleting a framebuffer detaches all of its attachments.
 /// If a buffer has already been deleted, this method has no effect.
 /// </summary>
 /// <param name="framebuffer">Framebuffer object to delete.</param>
 public virtual void DeleteFramebuffer(WebGLFramebuffer framebuffer) { }
 /// <summary>
 /// Associates a WebGLFramebuffer object with the gl.FRAMEBUFFER bind target.
 /// The currently bound framebuffer is the target of drawing operations. If the framebuffer is null, the context reverts to the default framebuffer.
 ///
 /// Errors:
 ///     gl.INVALID_ENUM    - If target isn't gl.FRAMEBUFFER.
 /// </summary>
 /// <param name="target">The target the WebGLFramebuffer object is bound to. Must be gl.FRAMEBUFFER.</param>
 /// <param name="framebuffer">The WebGLFramebuffer object.</param>
 public virtual void BindFramebuffer(int target, WebGLFramebuffer framebuffer) { }
 internal virtual void GenFramebuffer(out WebGLFramebuffer framebuffer)
 {
     framebuffer = gl.createFramebuffer();
     GraphicsExtensions.CheckGLError();
 }
 /// <summary>
 /// Gets state of framebuffer. Returns true if buffer is valid, false otherwise.
 /// </summary>
 /// <param name="framebuffer">The framebuffer to query. </param>
 /// <returns>True if framebuffer is valid, false otherwise. </returns>
 public virtual bool IsFramebuffer(WebGLFramebuffer framebuffer)
 {
     return false;
 }
        internal void PlatformResolveRenderTargets()
        {
            if (this._currentRenderTargetCount == 0)
            {
                return;
            }

            var renderTargetBinding = this._currentRenderTargetBindings[0];
            var renderTarget        = renderTargetBinding.RenderTarget as IRenderTarget;

            if (renderTarget.MultiSampleCount > 0 && this.framebufferHelper.SupportsBlitFramebuffer)
            {
                WebGLFramebuffer glResolveFramebuffer = null;
                if (!this.glResolveFramebuffers.TryGetValue(this._currentRenderTargetBindings, out glResolveFramebuffer))
                {
                    this.framebufferHelper.GenFramebuffer(out glResolveFramebuffer);
                    this.framebufferHelper.BindFramebuffer(glResolveFramebuffer);
                    for (var i = 0; i < this._currentRenderTargetCount; ++i)
                    {
                        var rt = this._currentRenderTargetBindings[i].RenderTarget as IRenderTarget;
                        this.framebufferHelper.FramebufferTexture2D((int)(gl.COLOR_ATTACHMENT0 + i), (int)rt.GetFramebufferTarget(renderTargetBinding), rt.GLTexture);
                    }
                    this.glResolveFramebuffers.Add((RenderTargetBinding[])this._currentRenderTargetBindings.Clone(), glResolveFramebuffer);
                }
                else
                {
                    this.framebufferHelper.BindFramebuffer(glResolveFramebuffer);
                }
                // The only fragment operations which affect the resolve are the pixel ownership test, the scissor test, and dithering.
                if (this._lastRasterizerState.ScissorTestEnable)
                {
                    gl.disable(gl.SCISSOR_TEST);
                    GraphicsExtensions.CheckGLError();
                }
                var glFramebuffer = this.glFramebuffers[this._currentRenderTargetBindings];
                this.framebufferHelper.BindReadFramebuffer(glFramebuffer);
                for (var i = 0; i < this._currentRenderTargetCount; ++i)
                {
                    renderTargetBinding = this._currentRenderTargetBindings[i];
                    renderTarget        = renderTargetBinding.RenderTarget as IRenderTarget;
                    this.framebufferHelper.BlitFramebuffer(i, renderTarget.Width, renderTarget.Height);
                }
                if (renderTarget.RenderTargetUsage == RenderTargetUsage.DiscardContents && this.framebufferHelper.SupportsInvalidateFramebuffer)
                {
                    this.framebufferHelper.InvalidateReadFramebuffer();
                }
                if (this._lastRasterizerState.ScissorTestEnable)
                {
                    gl.enable(gl.SCISSOR_TEST);
                    GraphicsExtensions.CheckGLError();
                }
            }
            for (var i = 0; i < this._currentRenderTargetCount; ++i)
            {
                renderTargetBinding = this._currentRenderTargetBindings[i];
                renderTarget        = renderTargetBinding.RenderTarget as IRenderTarget;
                if (renderTarget.LevelCount > 1)
                {
                    gl.bindTexture(renderTarget.GLTarget, renderTarget.GLTexture);
                    GraphicsExtensions.CheckGLError();
                    this.framebufferHelper.GenerateMipmap((int)renderTarget.GLTarget);
                }
            }
        }
 public void deleteFramebuffer(WebGLFramebuffer framebuffer) { }
 internal virtual void BindFramebuffer(WebGLFramebuffer framebuffer)
 {
     gl.bindFramebuffer(gl.FRAMEBUFFER, framebuffer);
     GraphicsExtensions.CheckGLError();
 }
 public void deleteFramebuffer(WebGLFramebuffer framebuffer)
 {
     throw new NotImplementedException();
 }
 public bool isFramebuffer(WebGLFramebuffer framebuffer)
 {
     throw new NotImplementedException();
 }
Exemple #22
0
 public void bindFramebuffer(int target, WebGLFramebuffer framebuffer)
 {
     this.openGl.BindFramebufferEXT((uint)target, framebuffer != null ? framebuffer.Value : 0);
     this.ErrorTest();
 }
 internal virtual void BindReadFramebuffer(WebGLFramebuffer readFramebuffer)
 {
 }
Exemple #24
0
 public void BindFramebuffer(FramebufferType target, WebGLFramebuffer framebuffer) => this.CallMethod <object>(BIND_FRAMEBUFFER, target, framebuffer);
Exemple #25
0
 public void DeleteFramebuffer(WebGLFramebuffer buffer) => this.CallMethod <object>(DELETE_FRAMEBUFFER, buffer);
 public void bindFramebuffer(int target, WebGLFramebuffer framebuffer) { }
Exemple #27
0
 public bool IsFramebuffer(WebGLFramebuffer framebuffer) => this.CallMethod <bool>(IS_FRAMEBUFFER, framebuffer);
 public bool isFramebuffer(WebGLFramebuffer framebuffer) { return default(bool); }
Exemple #29
0
 // Default WebGL2Framebuffer
 private WebGL2Framebuffer()
 {
     id        = null;
     IsDefault = true;
 }