Hi,
I’ve encountered a rather weird problem and I wondered if anyone else had seen anything similar.
Essentially I’ve got a bit of code which is using a compute shader to generate a texture, and then running a vertex shader that reads data from that texture. So the code is something like this (distilled to just the bits that matter):
// Initialisation
dataTex = new RenderTexture(dataTexWidth, dataTexHeight, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear);
dataTex.enableRandomWrite = true;
dataTex.filterMode = FilterMode.Point;
dataTex.useMipMap = false;
dataTex.Create();
// Render process
// Fill in dataTex using compute shader
computeShader.SetTexture(kernel, "dataTex", dataTex);
computeShader.Dispatch(kernel, xThreads, yThreads, 1);
// Draw geometry using data in dataTex
renderMaterial.SetTexture("_DataTex", dataTex);
Graphics.DrawMesh(mesh, Matrix4x4.identity, renderMaterial, layer);
…what happens with this code, though, is that the vertex shader texture fetches always return all zeros (except for the very first frame, which looks like it might be correct?). Sampling dataTex in the fragment shader, however, works fine, returning the correct results.
Now, what’s weird is that if I add the following in between the compute shader step and the geometry drawing step:
RenderTexture.active = dataTex;
RenderTexture.active = null;
…then everything starts working perfectly. That code isn’t abbreviated in any way - I’m literally just setting RenderTexture.active and then clearing it again immediately.
This is all on Unity 4.3 with the DirectX 11 renderer on a GF670.
Unless I’ve done something incredibly stupid here, I can only assume that either some piece of internal state isn’t getting set up or flushed correctly, and the “fake” rendertarget swap forces it into sorting itself out. Obviously with this workaround I can get things working, but I’m somewhat nervous that this is the tip of a bigger iceberg… have I missed some critical synchronisation here? Has anyone else seen this or have any ideas what it might be?