Search Unity

How to read camera position in compute shader?

Discussion in 'Shaders' started by DrummerB, Aug 20, 2019.

  1. DrummerB

    DrummerB

    Joined:
    Dec 19, 2013
    Posts:
    42
    I have a full screen image effect for VR that is implemented as a compute shader. This effect needs access to the current camera position. It is dispatched as part of a CommandBuffer.

    It all works quite well in Multi Pass mode. The compute shader is dispatched twice, once per eye, and the _WorldSpaceCameraPos is set to the correct left/right eye positions.

    Now, I'm trying to convert it to Single Pass Instanced mode. I'm not sure what is the correct setup for this case and how to access the camera position.

    In Stereo mode, _WorldSpaceCameraPos is defined as

    Code (CSharp):
    1. #define _WorldSpaceCameraPos unity_StereoWorldSpaceCameraPos[unity_StereoEyeIndex]
    For this to work, you need to use some macros that are explained in the documentation. These macros are clearly only intended for vertex/fragment shaders.

    Figuring out the eye index would be quite simple in a compute shader (e.g. thread id.z). However, the unity_StereoWorldSpaceCameraPos don't seem to be set or even accessible. I think Unity ignores the stereo mode, when compiling compute shaders, e.g. UNITY_STEREO_INSTANCING_ENABLED doesn't seem to be defined.

    Is this working as intended? Am I missing something? Am I supposed to just send the camera position myself?