Can a shader give information to the CPU code? For example, a shader function returns some information which the shader itself can’t do anything with, but would be valuable information in CPU code. Where do I store that value to have access to it from script?

You cannot. Shaders are part of the graphics “pipeline”, which means that you input the info to the GPU and it’ll render stuff on screen.

It’s a one-way ticket.

Problem is, you cannot simply return one float (even if you could return some data). Remember, shader program is executed once per every vertex of a game object + once for every pixel of game object displayed on screen.

So if your object covers half the screen on a fullHD resolution, a pixel shader would theoretically “return” ~1 million different float values.

You can actually return some values from GPU back to main memory, but it’s special GPGPU computing area and not GPU accelerated graphics, which is what we’re dealing with in a game engine.

Note: I do not know how ComputeShader works though.

Now, if you need that info for some debugging to find out what’s going on in your shader code, best way is to actually render out the float values in some meaningful way. Suppose you want to find out if a particular pixel is too close to the camera, and you need to find out what’s the depth value.

Simply have the pixel shader program write a red pixel if the value is too close, or return normal colored pixel if value is too far.

Pixels that are too close will be red. Shader debugging is often composed of such various hacks and workarounds.

If you absolutely must analize your shader input in total detail, you can write the needed values into seperate RenderTarget texture, and then read that texture using the CPU. Note: Reading the RenderTexture values via CPU is EXTREMELY slow and will kill performance. I recommend avoiding that.