read depth buffer [solved]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
yal2du
Prole
Posts: 42
Joined: Wed Oct 13, 2021 5:41 pm

read depth buffer [solved]

Post by yal2du »

tldr: how do i save/access the depth buffer contents from a render?

if i modify groverburger's simplest 3d example to render the spinning cube to a special canvas:

Code: Select all

    local lg = love.graphics
    
    canvas_settings   = {
         ["type"]      = "2d",
         ["format"]    = "depth32f",
         ["readable"]  = true,
         ["msaa"]      = 0,
         ["dpiscale"]  = lg.getDPIScale(),
         ["mipmaps"]   = "none",
      }
    temp_canvas  = lg.newCanvas(
      lg.getPixelWidth(),
      lg.getPixelHeight()
    )
    depth_canvas = lg.newCanvas(
      lg.getPixelWidth(),
      lg.getPixelHeight(),
      canvas_settings
    )
and then draw temp_canvas to the default screen canvas:

Code: Select all

function love.draw()

    love.graphics.clear()
    
    -- draw the mesh using the shader to the canvas pair
    
    lg.setCanvas({temp_canvas, nil, ["depthstencil"]=depth_canvas, ["depth"]=true}})   -- <<<
    lg.clear()
    lg.setDepthMode("lequal", true)
    Shader:send("modelMatrix", GetTransformationMatrix({0,0,4}, {Timer,Timer,Timer}, {1,1,1}))
    lg.setShader(Shader)
    lg.draw(Mesh)
    
    -- set canvas back to the default
    
    lg.setCanvas()
    lg.setShader()
    lg.setDepthMode()
    
    if love.keyboard.isDown("space") then
       --shader2:send("depthmap",depth_canvas)
       --lg.setShader(shader2)
       lg.draw(depth_canvas)
       --lg.setShader()
    else
       lg.draw(temp_canvas)
    end
end
that works fine. however, when trying to draw the resultant depth_canvas to the screen canvas (or access it as a uniform in a shader), all of its values appear to be {1.0,1.0,1.0,1.0}.

source of demo: https://github.com/groverburger/simplest_3d

edit: solved, see below
Last edited by yal2du on Wed Jun 07, 2023 1:28 pm, edited 1 time in total.
User avatar
UnixRoot
Party member
Posts: 100
Joined: Mon Nov 08, 2021 8:10 am

Re: read depth buffer

Post by UnixRoot »

That's probably because you're trying to draw an 32 bit floating point image to the screen. You need to turn its values into a range that can be drawn with 8 bits.
yal2du
Prole
Posts: 42
Joined: Wed Oct 13, 2021 5:41 pm

Re: read depth buffer

Post by yal2du »

UnixRoot wrote: Wed Jun 07, 2023 1:18 pm That's probably because you're trying to draw an 32 bit floating point image to the screen. You need to turn its values into a range that can be drawn with 8 bits.
thanks, had checked this earlier and the depth (at least on my hardware win10/radeon) is written to every color channel in a vec4 float range [0-1].

realized early a.m. that all of written depth values under the rendered object are very near, but not exactly 1.0. this is depth in the projection space and everything is 'closer' to infinity (represented by 1.0) than the screen (represented by 0.0). tweaked shader2 to confirm this just now.

can be demonstrated by uncommenting the following line from love.draw above

Code: Select all

--lg.setShader(shader2)
and adding the definition for shader2

Code: Select all

local shader2 = love.graphics.newShader [[

    //uniform Image depthmap;

    #ifdef PIXEL
        vec4 effect(vec4 color, Image tex, vec2 texcoord, vec2 pixcoord)
        {
                 vec4 texcolor = Texel(tex,     vec2(texcoord.x, texcoord.y));
            //   vec4 texcolor = Texel(depthmap,vec2(texcoord.x, texcoord.y));
            
            float outcolor = texcolor.b;
            vec4  retcolor;
            
            if (texcoord.x > 0.33)
               outcolor = texcolor.g;
               
            if (texcoord.x > 0.66)
               outcolor = texcolor.r;
               
            if (outcolor < 1.0)
               outcolor = 0.3 + (100*(1.0-outcolor));
           
            retcolor = vec4(outcolor,outcolor,outcolor,1.0);
            return retcolor;
        }
    #endif
    
]]
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 1 guest