I want to access a buffer in pixel shader and write data to it( something like shader storage buffer object in opengl). Is there such buffer in directx? I havn’t used it before…thanks!
i’m trying to implement a 2d shadow shader.
So I think I can firstly draw all sprites and record all pixels drawn.
Then
I draw a full screen quad with a function in pixel shader to judge if
this pixel is part of the shadow using the data recorded in the last
step. /though there is a problem of rendering sprites on top of their
shadows…/
Thus it needs a texture buffer to store those pixels drawn in the first step(in pixel shader), which maybe a large one…
use rendertarget?..
what you want is a rendertarget.
A rendertarget is simply a texture (or a buffer if you want to call it that).
For your 2D shadows the whole thing would look like this:
private RenderTarget2D renderTarget;
//Initialize our code
public void Initialize(GraphicsDevice graphicsDevice)
{
_graphicsDevice = graphicsDevice;
width = graphicsDevice.PresentationParameters.BackBufferWidth;
height = graphicsDevice.PresentationParameters.BackBufferHeight;
//this is youc custom shader. I load it in another class, but you can do whatever
_shadowEffect= Art.PostProcess;
spriteBatch = new SpriteBatch(graphicsDevice);
if (renderTarget != null)
{
renderTarget.Dispose();
}
renderTarget = new RenderTarget2D(graphicsDevice, width, height, false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8);
}
//
public void Draw(List<BasicEntity> entityList, Vector2 Camera)
{
//Here we declare the texture which we will write our graphics stuff to
_graphicsDevice.SetRenderTarget(renderTarget);
_graphicsDevice.Clear(Color.DarkSlateGray);
//Draw all our stuff which will have shadows!
spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointWrap, null, null);
//Draw Entities
foreach (BasicEntity entity in entityList)
{
spriteBatch.Draw(entity.spriteTexture, entity.GetDestinationRectangle(Camera), entity.GetSourceRectangle(), entity.GetColor(), 0, Vector2.Zero, entity.GetOrientation(), 0);
}
spriteBatch.End();
//Set the renderTarget to null -> draw directly to the backbuffer (to the final output!)
_graphicsDevice.SetRenderTarget(null);
//Background: draw your background below
//...
//Draw the shadows below
spriteBatch.Begin(0, BlendState.Opaque, SamplerState.AnisotropicClamp, DepthStencilState.None, RasterizerState.CullNone, __shadowEffect);
//Draw the renderTarget again in your shader where you process the shadows.
//Basically everything we have drawn just agian in black and blurred and with an offset for example to create the shadow
spriteBatch.Draw(renderTarget, new Rectangle(0, 0, _graphicsDevice.PresentationParameters.BackBufferWidth, _graphicsDevice.PresentationParameters.BackBufferHeight), Color.White);
spriteBatch.End();
//Draw the foreground below
//we can redraw our original rendertarget here
//by using spritebatch.begin .... draw(renderTarget) ... end
}
1 Like
oh, thanks very much! that’s exactly what I’m looking for:grin: