- Home /
Can I use a render target as a shader property when rendering to the same render target?
The title says it all:
I'd like to use a render target as an input texture to a shader, this shader being used to render into said render target.
What I'm mostly interested to know is if I do that, when I read pixels from the RT (tex2D(blahblah);) will those retrieved pixels be of the untouched original RT before any rendering, or will they be modified as pixels get rendered into the RT?
Or am I committing a crime against nature?
Thanks.
I'm going with 'crime against nature'. I expect your computer will dissappear into a singularity too!
seriously though, this sounds like the easiest way to answer your question would be to go ahead and just test it. be sure to post your own answer here if you find out. sounds interesting.
Hey Dock, thanks for answering. I indeed tested it. And I answered it :) Enjoy
Answer by taoa · May 21, 2010 at 03:11 PM
OK, I tested it and I can answer this ages-old question:
Yes, it's possible to do something like this:
RenderTexture oldRT = RenderTexture.active;
RenderTexture.active = RT;
GL.PushMatrix( );
Material mat = myMat;
mat.SetTexture( "_MainTex", RT );
for( int i = 0 ; i < mat.passCount ; i++ )
{
mat.SetPass( i );
DrawQuad( rect, new Rect( 0.0f, 0.0f, 1.0f, 1.0f ) ); //some rectangle, with texture coords...
}
GL.PopMatrix( );
RenderTexture.active = oldRT;
which uses the target texture as a normal texture in the shader. If it works for me, it might be because my hardware (GTX260) is capable of getting data from stuff it's rendering too, I don't know. But that definitely works.
I think I can also answer my second question, since I can see some bleeding from my blur shader: when I read from my render texture, it reads things that were already rendered for previous pixels.
In other words: I'm definitely reading what I'm writing into by doing that.
And I for one am happy of this. There are so many crazy things that can be done with this fact. Many will consider it horrible hack, I don't mind :)
But I still need to use an intermediate render texture for my blur, because of all the bleeding :p