Hi there,
I’m trying to access a global variable (set from C# with Shader.SetGlobal) from inside a Compute Shader.
Is there any reason this might not work, or am I doing something wrong ?
Thanks in advance !
Hi there,
I’m trying to access a global variable (set from C# with Shader.SetGlobal) from inside a Compute Shader.
Is there any reason this might not work, or am I doing something wrong ?
Thanks in advance !
Short Answer: No, it should no work with compute.
Long Answer: Global variables + some of our built-in variables in graphics are actually batched into buffers and bind across all render calls (Of course specifics differs across graphics API), so it means it will have graphics variables in it. As compute shader has nothing common in rendering pipeline there is not actual benefit binding global variables in compute. Also keep in mind that binding buffers is not performance free.
However what you want to achieve can be easily done with ComputeBuffers (https://docs.unity3d.com/ScriptReference/ComputeBuffer.html).
So basically how it would look like:
Also I recommend creating a compute shader include file that will have a signature of you global computebuffer, so you wouldn’t need to duplicate it for every compute shader.
etc.
MyGlobalVariablesSignature.cginc
struct MyGlobalVariables
{
...
};
RWStructuredData<MyGlobalVariables> _MyGlobalVeriables;
I actually had a similar question which ties to your answer @LukasCh Im using a global variable in a shader but it doesn’t seem to work on Samsung S6 but works fine on S7 and PC, etc. Both support Shader model 3.0 right so is there anything else I should check for specifics in graphics API to find the root of the issue?
Thanks @LukasCh for clearing this up !
Uh awesome to get a bit more information about global shader variables! I’m wondering if it is possbile to profile them in any way (Or if they show up in the profiler). I’m using them quite a lot and I’m wondering how to see its performance overhead. Or is there a resource you can point me at @LukasCh ?
Yea I checked so the only difference is gpu generation (Only compared GPUs), so there shouldn’t be any obvious limitations. Its a bit hard for me to guess the issue with this little information, so basically if this only s6 issue, I really recommend reporting the bug for it (Also it always healthy to check for regression - easiest way to identify bug without any technical information).
I think your best bet is to use third party GPU debug tools (As you will get all the information). However keep in mind that it will depend from your platform:
For anyone who might have been wondering - You can share global textures between shaders and compute shaders. Which means you can pass around any arbitrarily large amounts of data you want.
control1= new RenderTexture(2048, 2048, 0,
RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear);
control1.enableRandomWrite = true;
control1.Create();
control1.SetGlobalShaderProperty("_MyGlobalControlMap");
myComputeShader.SetTextureFromGlobal(_kernelHandle, "ControlMap", "_MyGlobalControlMap");
After lot’s of checking, it wasn’t the phone that wasn’t working it was the operating system. Marshmallow didn’t work ok with global variables in shaders whereas Nougat was ok
Any documents on those built-in variables and cbuffer?I think UNITY_MATRIX_M or P or VP ,those variables are all in CBUFFER.But who is who ?for example cbuffer 0 means which variable?Where can I found documents ?