Compute Shader call breaks following Blit call in build, but not in editor

40 views Asked by At

using unity 2022.3.10f1 on an m1 Mac.

I was working on a bloom effect and ran into a weird issue.

private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        Graphics.Blit(source, tempRt);
        Graphics.Blit(source, tempRt2);

        //Compute Shader to get bloomy parts of image
        ExtractBloomLayer.SetTexture(0, "LayerOut", tempRt);
        ExtractBloomLayer.Dispatch(0, tempRt.width / 32, tempRt.height / 32, 1);
        //tempRt now contains the information that I'll blur to make the bloom

        //apply the blur (only one iteration for testing)
        Graphics.Blit(tempRt, tempRt2, blurMat); //SHOULD MAKE WHOLE SCREEN RED

        //sending raw blur to screen (for testing)
        Graphics.Blit(tempRt2, destination);
}

this code should make my entire screen red, because of the blurMat Graphics.Blit, and it works as expected in editor, but not in build. If I comment out the two ExtractBloomLayer lines, the build works as expected as well (but I need those lines, and can't just delete them.) In the build, the ExtractBloomLayer compute shader works great, but the blurMat blit doesn't do anything at all. Does anybody know why this is?

I've tried a bunch of stuff with the blurMat shader, but it always fails to show up in the built version of the game (but again, only breaks when the compute shader lines are active)

Modifying shader stripping options https://forum.unity.com/threads/some-shaders-not-rendering-in-build.1412457/ Putting the shader in Assets/Resources https://forum.unity.com/threads/why-this-shader-works-in-editor-but-not-when-i-build-the-game.255756/ Removing Shadergraph from packages (im in built-in so N/A) https://forum.unity.com/threads/standard-cutout-shader-working-in-editor-not-in-build.1223088/ Putting the shader in the "Always Include" list in graphics settings

Nothing has worked so far.

1

There are 1 answers

0
spacemann On

I couldn't find a real solution. Instead I rewrote my isolate bloom compute shader as a fragment shader and replaced the relevant lines. Build now works as expected.

I know compute shaders are probably not a great idea to rely on so heavily but they're so much more intuitive to work with that I'm usually fine with their downsides.

working code:

private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
    Graphics.Blit(source, tempRt, isolateBloomMat);

    if (blurIterations > 50) blurIterations = 50;
    for (int i = 0; i < blurIterations; i++)
    {
        Graphics.Blit(tempRt, tempRt2, blurMat);
        Graphics.Blit(tempRt2, tempRt, blurMat);
    }

    Graphics.Blit(source, tempRt);
    BlurAndAdd.SetTexture(0, "CamIn", tempRt);
    BlurAndAdd.SetTexture(0, "BLayer", tempRt2);
    BlurAndAdd.Dispatch(0, tempRt.width / 32, tempRt.height / 32, 1);
    Graphics.Blit(tempRt, destination);
}