- Home /
Writing to RWTexture3D using a pixel shader without using Graphics.Blit
I'm almost certain that Unity 4.6.1.1f1 still doesn't support writing to RWTexture3D
or RWTexture2D
from a pixel shader without using OnRenderImage(..)
and Graphics.Blit
It seems to me like Graphics.SetRandomWriteTarget(..)
doesn't work when used from OnPostRender
with either Graphics.DrawMeshNow
or Graphics.Blit
. I can't understand why though.
Here's a code snippet of the script:
void OnPostRender()
{
Graphics.SetRenderTarget(rtCam);
Graphics.ClearRandomWriteTargets();
Graphics.SetRandomWriteTarget(1, rtVol);
Graphics.SetRandomWriteTarget(2, rtPlane);
matWriteTextureOnPS.SetFloat("_volDepth", size);
matWriteTextureOnPS.SetFloat("_time", Time.timeSinceLevelLoad);
Graphics.DrawMeshNow(quadMesh, Vector3.zero, Quaternion.identity);
// Graphics.Blit(src, src, matWriteTextureOnPS); // replace above line with this and try.
Graphics.ClearRandomWriteTargets();
}
matWriteTextureOnPS
has a pixel shader that writes to rtVol and rtPlane which are used as textures for objects in my scene. If I move this code to OnRenderImage
and use Graphics.Blit
instead of DrawMeshNow
and make the shader use vert_img
as the VS, it works.
Here're the GPA API logs for the three scenarios I tried:
For 1 and 3, Unity binds the RT and UAVs correctly using OMSetRenderTargetsAndUnorderedAccessViews, but for 1, it resets the bindings using OMSetRenderTargets. As a result, the UAVs are no longer bound. For 2, Unity never binds the UAVs. I don't know why.
Links to similar questions:
Definitely sounds like a bug but I couldn't reproduce with a quick try. So please submit a bug with a $$anonymous$$imal repro project.
For the Draw$$anonymous$$eshNow case you could try out calling SetRenderTarget after the SetRandomWriteTarget calls to force the UAVs on that point (in case the Draw$$anonymous$$eshNow does not set them due to some bug).
Thanks, I submitted the bug with a simple test scene. Apparently, Graphics.Blit works fine in OnPostRender/OnRenderObject for UAV writes. The problem ins$$anonymous$$d is: When multiple UAV writes are done in any of these functions (via, say a loop), only the first one has the UAVs bound and successfully written to. Everything else doesn't.
Your answer
Follow this Question
Related Questions
Prevent Unity from reordering DrawMeshNow calls 1 Answer
Best way to access RenderTexture immediately after camera render 0 Answers
Speeding up reading from a RenderTexture 3 Answers
Image-Based Lighting refreshing 0 Answers
OnpostRender is not called 4 Answers