Search Unity

Question How does Camera.RenderToCubemap work internally?

Discussion in 'Shaders' started by LazloBonin, May 31, 2023.

  1. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    812
    Hi!

    I'm debugging a complex rendering stack that works perfectly fine when rendering to a 2D texture, but has some quirks when rendering to a cubemap.

    I'd like to know what RenderToCubemap does behind the scenes to help me understand where the issues may be coming from, but I can't find any documentation on this. Additionally, I don't know how to debug those using either the Frame Debugger or RenderDoc.

    Some questions:
    1. Does RenderToCubemap render 6 different textures then combine them, or does it render all of them at once somehow?

    2. Probably corollary to #1, but do CommandBuffers get executed in order for each face (all events, 6 times), or in order for all faces at once (event on all 6 faces, then next event, etc.)

    3. What is the status of view matrices like unity_MatrixV and unity_MatrixInvV during this rendering? Do these matrices get updated between each face? What about the camera-bound matrices like unity_CameraToWorld and unity_WorldToCamera?

    4. Do grab passes grab each face individually, or the entire flattened texture? Does grab pass sampling code need to be adjusted when rendering to a cubemap?

    5. Bonus: Is there any way I can debug a RenderToCubemap call using either the Frame Debugger or RenderDoc?

    Thanks in advance to anyone who can share some insight! :)
     
  2. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    812
  3. Rukhanka

    Rukhanka

    Joined:
    Dec 14, 2022
    Posts:
    204
    1. RenderToCubemap renders scene 6 times (if all faces needed by specified flags). No combining needed because internally in GPU cubemap is just 6 equivalent textures that treated as one entity.

    2. Not 100% sure, but according to the logic, every face (camera view) need to be correctly rendered with respect to all data associated with camera. So I assume that camera command buffers are executed for every face.

    3. All matrices will be correctly set up for every face.

    4. Grab Pass for each face.

    5. Any rendering can be observed by RenderDoc or any other GPU debugger/profiler, so yes.
     
  4. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    812
    Thanks for the reply! I'd love to be able to validate that myself, some of these don't seem to correlate with what I've observed...

    How can I use RenderDoc to observe a call to Camera.RenderToCubemap?
     
  5. Rukhanka

    Rukhanka

    Joined:
    Dec 14, 2022
    Posts:
    204
    Make RenderToCubemap to be executed every frame, then capture frame in RenderDoc. Then search 6 camera render sections that corresponds to cubemap update