How can I sample the RenderTarget2D that is being drawn to in a different draw call? Alternatively, can I sample the back buffer in any way?
You draw to the render target first as it is a fake back buffer that you can then in another drawcall using a shader sample from it.
Which is basically the same as sampling from the backbuffer. You then draw something to the real back buffer using that data (the original rendertarget) plus a shader that reads it and does something with it.
While it would be somewhat inefficient / bad practice you could even take the rendertarget and cast it to a texture use GetData on the texture to place its pixel data into a array of colors.
.
.
I think what he want’s to do is to and tell me if this sounds like your intent so we are all clear.
Draw to a rendertarget.
Set the active render target to then be the actual backbuffer.
Take that original rendertarget and then pass it to a shader.
In the shader…
Read data from the rendertarget to perform some manipulation on the current drawings using that as well as possibly pass in a addition texture ect.
Draw to the back buffer (the screen) using the above output from the shader ?
Or maybe something like so were the rendertargets continuously feed off the previous render target data in a shader?
for example in the above the game1 did roughly the following.
bool firstrun = true;
int flipflop =0;
Rendertarget currentRt;
Draw(...){
flipflop ++;
if( flipflop > 1 )
flipflop =0;
if(flipflop == 0)
currentRt = rendertarget1;
else
currentRt = rendertarget2;
SetRenderTarget(currentRt);
if(firstrun)
{
// draw something on the initial render target
firstrun = false; // never enters here again
}
SetRendertarget(null); // ready to draw to screen
// spritebatch begin(... your effect ,..) end() ect set your own effect in begin ( ... FX )
// that effect of yours that uses a texture as if it were data itself in it.
// the texture in this case which is actually the rendertarget.
spritebatch.Begin(..., , ,... your effect ,..)
// though it might be easier to draw a quad see here.
// http://community.monogame.net/t/minimal-example-of-drawing-a-quad-into-2d-space/11063
// draw the whole texture to the screen (now the back buffer) the pixel shader manipulates the data.
spriteBatch.Draw( (Texture2D) currentRt, sourceRect, destRect, ...ect);
spritebatch.End();
}
the resulting image in that picture is just growing on its own by the algorithm in the pixel shader repeatedly using data from the first run and altering it over and over each frame.