• Create Account

## Advice for 2d in a 3d world

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1trick  Members

Posted 22 January 2012 - 04:31 PM

After being out of programming since Directx 8, I decided recently to get back into it. So, I jumped right in with Directx 11 and started working on a library of objects to ease the process once I start to actually make something. When it came to doing 2D stuff (for user interfaces, etc) I found two primary options on the various sites I went to.

The first, is to interop Direct2D with Direct3D. This seemed like a lot of work to do, and I didn't need all the functionality of Direct2D. The second, was to use Direct3D but use an ortho matrix in place of the projection matrix. I'm sure it's something on my part, but I could never get this working correctly, plus the examples I've seen would either use a non-constant buffer for the vertex data, or have to recreate it each time the sprite was to move.

So, I came across another "solution". My question is, it seems far to simple to not have problems, otherwise why wouldn't it have been done that way in place of the ortho matrix. Does anyone see any potential problems with doing 2D sprites in this way?

When I create the quad to display the 2D image, I give it pixel size of the width and height to create it. I also store a Position value, in pixels, of where the quad should be displayed. I built a seperate vertex and pixel shaders to be used with the 2D images, where the screen size and quad position are loaded into a constant buffer. Vertex Shader below, where the vertex position is added to the position (pX and pY) in the buffer, multiplied by 2 and divided by screen size (to get the vertex position in the range of 0.0 to 2.0), then reduced by 1 to get range of -1.0 to 1.0

//Sprites.vs
//--------------------------------------------------------------------------------------
// Buffers
//--------------------------------------------------------------------------------------
cbuffer ModelVCBSTRUCT : register( b0 )
{
float sWidth;
float sHeight;
float pX;
float pY;
}
//--------------------------------------------------------------------------------------
// Typedefs
//--------------------------------------------------------------------------------------
struct VertexInput{
float4 Pos : POSITION;
float2 Tex : TEXCOORD0;
};
struct PixelInput{
float4 Pos : SV_POSITION;
float2 Tex : TEXCOORD0;
};

//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
PixelInput VS( VertexInput input )
{
PixelInput output;
output.Pos = input.Pos;
output.Pos[0] = (((output.Pos[0] + pX) * 2) / sWidth) - 1;
output.Pos[1] = (((output.Pos[1] + pY) * 2) / sHeight) - 1;
output.Tex = input.Tex;

return output;
}


Again, I just want to know if this will have some problem with it, as the solution seems far too simple compared to the other options I've seen so far. Thanks in advance for feedback!

### #2phantom  Members

Posted 22 January 2012 - 04:47 PM

With this method, as it currently stands, for every sprite you want to render you have to update the cbuffer, this means you can draw ONE sprite per draw call before having to do what is quite an expensive operation on the CPU side and will likely stall the GPU too.

You would be better served by using an Ortho matrix, using instancing to draw one quad multiple times and passing the x,y location via a vertex stream. While it might seem slightly counter intuative to update a buffer just because something has moved it will still be faster once you start drawing more than one object on the screen as you'll remove a lot of CPU cost by being able to draw multiple sprites in one batch.

### #3MJP  Moderators

Posted 22 January 2012 - 06:04 PM

You would be better served by using an Ortho matrix, using instancing to draw one quad multiple times and passing the x,y location via a vertex stream. While it might seem slightly counter intuative to update a buffer just because something has moved it will still be faster once you start drawing more than one object on the screen as you'll remove a lot of CPU cost by being able to draw multiple sprites in one batch.

The SpriteRenderer class in my sample framework does exactly this, if you need some code to look at.

### #4trick  Members

Posted 22 January 2012 - 08:43 PM

Thanks for the responses, though it seems there's something about it I'm having trouble grasping. For 3D models, you'd need to pass the model matrix in each object, each frame whereas the other matrices wouldn't need changed that often. How is it such a larger hit when doing this with two floats for position to do the sprite, vs the 16floats required for a matrix to render a 3d model?

Also, if you pass the ortho matrix once, and then render all of your 2d sprites sending the position in the vertex data, doesn't that limit you to one size for all sprites (unless you also send a world matrix, or some other data in the constant buffer for it to modify each one)?

### #5MJP  Moderators

Posted 23 January 2012 - 02:05 AM

It's not about passing data, it's about batching to reduce draw calls. Draw calls and state modification can consume a lot of CPU time if you use too many of them.

### #6trick  Members

Posted 23 January 2012 - 04:26 AM

Thanks for the input guys! Looks like I need to do some more research on instancing, using ortho matrix, etc.!

### #7trick  Members

Posted 23 January 2012 - 05:00 AM

One last question on the subject. If the object here is to use instancing, to reduce the number of draw calls being made, I see two things I want to know if I'm correct on.

1) The textures need to be in one sprite-sheet for all sprites, so that the texture buffer can be updated once prior to calling draw function.

2) Couldn't the same idea be applied without the ortho matrix, like the vertex shader below?

//Sprites.vs
//--------------------------------------------------------------------------------------
// Buffers
//--------------------------------------------------------------------------------------
cbuffer OnScreenUpdate : register( b0 )
{
float sWidth;
float sHeight;
}
//--------------------------------------------------------------------------------------
// Typedefs
//--------------------------------------------------------------------------------------
struct VertexInput{
float4 Pos : POSITION;
float2 Tex : TEXCOORD0;
float2 sPos : TEXCOORD1;
};
//sPos = screen position

struct PixelInput{
float4 Pos : SV_POSITION;
float2 Tex : TEXCOORD0;
};

//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
PixelInput VS( VertexInput input )
{
PixelInput output;
output.Pos = input.Pos;
output.Pos[0] = (((output.Pos[0] + sPos[0]) * 2) / sWidth) - 1;
output.Pos[1] = (((output.Pos[1] + sPos[1]) * 2) / sHeight) - 1;
output.Tex = input.Tex;

return output;
}


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.