A basic shader question , I am using a MeshInstance2D to generate a mesh.

When two triangles/primitives of a mesh overlap are the overlapping pixels processed twice ? If so can I know in the fragment shader when a pixel is processed twice , say to give the overlapping region a different colour in a single pass ?

  • xyz and Haystack replied to this.
  • druboi

    If so can I know in the fragment shader when a pixel is processed twice , say to give the overlapping region a different colour in a single pass ?

    You can't do that in a single pass. In the GPU hardware, the fragment shader can't read the contents of the destination color buffer or depth buffer. You can blend the final color of the current primitive with the contents of the color buffer, using a fixed set of blend modes, but that blending doesn't happen inside the fragment shader. In the GPU hardware, that happens at a later step.

    druboi When two triangles/primitives of a mesh overlap are the overlapping pixels processed twice ?

    If GPU depth test is not enabled (typically for 2D) then yes, the overlapping pixels will be processed twice. With depth test enabled, it will also happen twice in most cases, although in some cases it won't, depending on the hardware and shader code. Look up "early depth test" for more info on this.

    druboi If so can I know in the fragment shader when a pixel is processed twice , say to give the overlapping region a different colour in a single pass ?

    Fragment shader cannot know how many times a pixel was processed, however the shader can read what's currently in the framebuffer and use this for further processing or to make conclusions about what has been previously drawn.

      xyz

      xyz the shader can read what's currently in the framebuffer

      I think by this you mean reading the screen texture (?). I tried it but that doesn't give me any new information about the overlapping pixels.
      I am working only in 2D. I am trying to give the overlapping region a different colour by checking if it was already assigned a colour. Guess this isn't possible. Is there any other way I could achieve this ?

      • xyz replied to this.

        druboi I think by this you mean reading the screen texture (?). I tried it but that doesn't give me any new information about the overlapping pixels.

        It does if you know your initial background color. In the example image you posted, if the color of a pixel is different that white then it must mean something was drawn there previously.

          xyz That's true, so long as you don't draw anything in white. Or in gradients fading to/from white. I haven't done 2D screen reading shaders in a while, but if godot 2D has a transparency mask or such in 'alpha' channel then that might be more useful to check if something has been drawn in x,y pixel position.

          • xyz replied to this.

            Megalomaniak but if godot 2D has a transparency mask or such in 'alpha' channel then that might be more useful to check if something has been drawn in x,y pixel position.

            I think it has alpha only in custom sub viewports (if they are set to be transparent). In the main viewport, alpha is always cleared to 1.0 so it's useless in this respect. But depending on the wanted effect, something could be arrange by cleverly using rgb values and blending operations. OP should perhaps describe in more details what exact effect they're after.

            druboi

            If so can I know in the fragment shader when a pixel is processed twice , say to give the overlapping region a different colour in a single pass ?

            You can't do that in a single pass. In the GPU hardware, the fragment shader can't read the contents of the destination color buffer or depth buffer. You can blend the final color of the current primitive with the contents of the color buffer, using a fixed set of blend modes, but that blending doesn't happen inside the fragment shader. In the GPU hardware, that happens at a later step.

              Haystack

              Yes !
              To restate the problem : I want to detect the overlapping region of the same mesh and give it a different colour.
              Here using a blend_sub , I can get the overlap but the overlapping colour (green and black) is not made available to the fragment shader. This is because , as you said , the blending happens after the fragment shader execution. So this can't be done in a single pass or I have to separate the mesh. Thanks all !

              • xyz replied to this.

                druboi ...of the same mesh

                You should have clearly stated that in your original question.

                Yeah, there's no way of knowing what the same draw call put previously onto the screen. However, you can know what previous draw calls put there. So, you'll have to do a draw call per triangle, which in practice means putting each triangle into its own mesh instance.