Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 11 Mar 2012
Offline Last Active Apr 19 2012 04:08 AM

Posts I've Made

In Topic: Best practices for rendering LOTS of text?

10 April 2012 - 12:24 AM

Going back to your fixed 80*50 grid mesh idea, you can do this with a static mesh buffer, a static texture, and a dynamic buffer of glyph ID's. You make a static mesh, that contains a quad (or two tris) for each grid cell. Every vert can have a static texcoord that identifies which corner of the glyph it represents (e.g. 2D values of [-1,-1],[1,-1],[1,1] or [-1,1] or alternatively simply 1D values of 0,1,2 or 3) and a secondary 1D texcoord that identifies which 'cell' the vert belongs to. If theres 80*50 cells, then this cell-ID ranges from 0-3999. You can fit the corner-ID in a single byte (for the 1D version, or two bytes for the 2D version), the cell-ID in a 16-bit int. Then your dynamic data consists of specifying which glyph should appear in which cell. Assuming you only need 256 glyphs, then this is one byte per cell, or 4000 bytes. All of your glyphs can be stored on a static texture sheet -- e.g. a 256*256 texture gives you 256 glyphs at 16*16 resolution. In the vertex shader, you read the vertex's cell-ID, use that to fetch the glyph-ID that should appear in that cell, convert the glyph-ID into the texture-coordinate at the centre of the glyph. Then once you've got this glyph-centre tex-coord, you use the vertex's corner-ID to offset it to the appropriate corner of the glyph. That sounds like it should work, and should perform fine. If you're struggling with specific implementation details, ask below ;) ---------- [edit] As an alternative implementation: You could have a static grid mesh that only contains positions for the verts and an index buffer so you're not repeating yourself (there's 6 verts per two-tri-quad, but only 4 unique values -- so you write the 4 values into a vertex buffer, and then write 6 indices into an index buffer). Then you make a dynamic vertex buffer for storing the UV coordinates. If your text isn't super-high resolution, then these can be stored as two bytes per UV. On the CPU, you determine which glyph appears in each cell, and write the 4 corner UV's for that glyph into your dynamic vertex buffer. When rendering, you bind both the static position buffer and the dynamic tex-coord buffer. The size of the dynamic data is 80*50*2, or 8KiB, which is still tiny. Most games that I've worked on have use the above implementation, except that both positions and tex-coords are dynamic and are generated by the CPU every frame.

Thank you!
Though i am having a hard time following along with your description (too technical for me, i am still a newbie at D3D, SlimDX and shaders. (Or at least i consider myself to be one)
I will read through it several times to see if i can understand it any better but the first two questions that spring to mind is:

1. How would this look in shader code, should i declare an array and use that to determine which texture coordinates i need in shader code? Please show me an example of what i need to do here since i have no idea at all.
2. An example in code would be nice too, i know there are lots of different types of resources i can update but i am not so sure about arrays. Do i even send arrays? Maybe i just send an 8 bit texture?

Like i said, i am pretty clueless and i figure stuff out as i go.


That aside, I've managed to pass a 2048x2048 texture to my GPU and am testing by rebuilding the entire vertex buffer every frame.
Currently i have a 200 x 200 grid of quads that are fed random tu and tv values each update and i am running at 77 FPS. That is beyond expectations for me, i have never been able to run so fast before.

I am using a GeForce GTX580 and an I7 at ~3.6 GHz in case anyone wants to know.
Mind you, this is the first crude testing. I don't intend to update the entire vertex buffer like this but only the texture values (something i still have to figure out how to do unless someone shows me how) and eventually optimize that down to one byte per character.

As of current with some quickie math...

201x201x6 vertices = 242,406 vertices per frame-
vertex data size is 24 so that's 5,817,744 per frame. (or ~5.5 MB per frame)
Running at 77 FPS, that's 427.21 MB/s sent to the GPU. What a waste but this is just how i work, it helps me organize and think ahead since i never actually plan ahead that much.


Oh and i think (if it's possible) i am going to have a static char buffer so if it isn't updated at all then it would reuse the old one already present on the GPU. In case it wasn't clear before.
Which brings up another question, say i want to alter a specific row and/or a specific column or region etc in my little console window. It's possible to just update a portion of the char array without having to update the whole buffer/whatever right?

I like performance and say i wanted to make some sort of ASCII animation on the virtual screen without updating the entire thing then that would be possible too right?

In Topic: Best practices for rendering LOTS of text?

10 April 2012 - 12:09 AM

Ive never needed to render the amount of text that you are, but off the top of my head, why are you trying to use textures to do it? Why not use ID3DXFont->Draw()? You could create a data type that holds your text as a string, holds the column, row ect...(Using RECT coordinates) then loop through all your text, sending the string through your ID3DXFont object. I have never done this as I said, so I am not sure of the performance you would get out of ID3DXFont, but I would imagine it would be faster than sending everything through a texture. You could simulate the text position in your 3D world onto your console.

I have used ID3DXFont in the past, for full page updates (That is, scrolling text like in a command prompt) it's simply too slow.
My idea is to have the bitmap font texture stored in video memory and only alter the texture coordinates when the window needs to be updated.
Keep in mind that ID3DXFont too sends texture resources to the hardware after you have created a batch. Thus, sending a full 80x50 grid of characters is as expensive as sending a texture. The difference is i can get rid of some overhead doing it with pre-allocated font maps. XNA does this but i am not so sure it's the optimal solution either.

So in essence, my question really is. How can i update a grid of quads that are already assigned to hardware without sending the full vertex buffer every frame. All i am interested in is setting the texture coordinates and if possible, sending only byte arrays to the shader since i only intend to use up to 255 different characters.

In Topic: The evolution of Direct3D?

14 March 2012 - 06:06 PM

Well i don't know much about the history of DirectX... And despite some rumblings about the accuracy of Wikipedia it's at least a good start to get a brief overview of DX history.
There are two articles of interest, one covering DirectX and the other covering Direct3D.

Instead of me linking them just google "DirectX" and "Direct3D" and you will find them in the top 3 results.
Beyond that you might have to google each and every version of DirectX and Direct3D separately and build a history from there.

Unless of course you can find a game developer that has been around since 1992 and is willing to share his knowledge with you.

To answer your question "why it was necessary to" it's simply because demand exceeded the capabilities of the latest version and hardware grew ever faster and more capable. OpenGL played a big part in speeding up the development of DirectX and when OpenGL slowed down (version 1.3) so did DirectX (Version 9.0 a, b, c) and furthermore, the Xbox and PS3 can only use hardware compatible with DirectX 9 and OpenGL | ES 1.0 (OpenGL 1.3) with some features of OGL | ES 2.0 (OpenGL 2.0) respectively.
Only 'recently' (with the arrival of Vista, DirectX 10) an enormous demand has arrived for MORE. As gamers and developers demand ever higher fidelity in rendering (graphics) both hardware and drivers need to match the expecations.

So, simply put... It needed to evolve because we wanted more!
And there are many many more techniques to come as hardware grows, not necessarily in speed but in width.

In Topic: [SOLVED] I am supposed to use Transpose but when i do...

13 March 2012 - 04:10 PM

In your vertex buffer binding you set the stride to 0 when it should actually be sizeof(SimpleVertex), which is 20.

It works, you are a god and all that.
Now i have a working example to go from to fix my other ones. Thanks!


Ok, this is what i have learned for all this so far.
  • For projection, a near plane of 0.0f does NOT work.
  • One does indeed need to transpose the matrices. How i managed to get anything useful out of not transposing them i will never know. (Matrix math is still beyond me...)
  • It helps to pay attention when you code, as Mike pointed out you have to set a proper stride.
  • It was initially very unclear to me how to use constant buffers and the answer from Mike on stackoverflow never mentioned that you had to set then to the shaders using SetConstantBuffers. This could have been realized had i known anything about D3D in the first place or by simply looking into the DX SDK examples.
Point is, never give up and when you get stuck make sure you don't start assuming things as i did.

Now, a quick example on how to properly update constant buffers in case anyone else finds themselves in a similar situation:

A simple shader with 3 constant buffers:
cbuffer WorldMatrixBuffer : register(b0)
matrix world;
cbuffer ViewMatrixBuffer : register(b1)
matrix view;
cbuffer ProjectionMatrixBuffer : register(b2)
matrix projection;
struct VS_IN
float4 position  : POSITION;
struct PS_IN
float4 position  : SV_POSITION;
PS_IN VShader(VS_IN input)
PS_IN output;
output.position = input.position;
output.position = mul(output.position, world);
output.position = mul(output.position, view);
output.position = mul(output.position, projection);
return output;
float4 PShader(PS_IN input) : SV_Target
return float4(1.0, 1.0, 0.0, 1.0);

This will take in a vertex buffer, multiply it's positions by the world, view and projection matrices that are defined in their own separate constant buffers.
It passes the information along down the rasterizer where each pixel is set to a solid yellow color.
One could gain a little bit of performance by multiplying the view and projection matrices before sending them to the shader and using a combined viewProjection cbuffer to reduce the number of mul() operations in the vertex shader stage. For the sake of clarity i have decided not to do this here.

The registers (b0, b1 and b2) are defined so we can assign them in code using their respective indexes (slots) and to update and assign them you need (in code):
  • A Buffer with the ConstantBuffer bind, Default resource usage and in this case, no CPU access flags.
  • A data stream to write matrix data to. A matrix is 64 bytes large (float4x4, 16 floats of 4 bytes each) thus you need to have 64 bytes of memory allocated to write to these constant buffers.
  • A context to call UpdateSubresoruce().
Sample code:
// Create the projection matrix buffer.
   projectionMatrixBuffer = new Buffer
    new BufferDescription
	 BindFlags = BindFlags.ConstantBuffer,
	 CpuAccessFlags = CpuAccessFlags.None,
	 SizeInBytes = Marshal.SizeOf(projection),
	 Usage = ResourceUsage.Default,
   // Update the projection constant buffer.
   using (DataStream data = new DataStream(Marshal.SizeOf(projection), true, true))
    data.Position = 0;
    context.UpdateSubresource(new DataBox(0, 0, data), projectionMatrixBuffer, 0);

So first we create a buffer with the proper buffer description. Marshal.SizeOf() resides in System.Runtime.InteropServices and is used to determine the size of Types (Classes) and objects (assigned variables) which is helpful if you don't want to manually calculate the size of each constant buffer.
In this case i write the matrix directly to the stream but if your cbuffer has more than one element in it it may be useful to create a structure or class to contain all elements of the constant buffer in it before writing to the buffer.

For example:
class HerpClass
  public Matrix world;
  public Matrix view;
  public Matrix projection;
struct HerpStruct
  public Matrix world;
  public Matrix view;
  public Matrix projection;
int classSize = Marshal.SizeOf(typeof(HerpClass));  // Is 192 (64 * 3)
int structSize = Marshal.SizeOf(typeof(HerpStruct)); // Is 192 (64 * 3)

Adding any private variables to these classes will still count towards the total size of the class/structure so don't do it. (No i didn't either btw if you thought so, i just want to cover this incase someone gets any ideas.)
The reason you need "[StructLayout(LayoutKind.Sequential)]" on the class is because otherwise Marshal.SizeOf will produce an ArgumentException that reads "HerpClass cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed."
Thus, using a struct is your best option and structs and classes are pretty much the same things anyways.

Yes, you can create a constructor thus enabling you to use "new HerpClass(world, view, projection);" if you so desire.

Right, moving on...
Before you render you need to set the buffers to the vertex shader (and pixel shader where needed, they are separate) and to do that you do this:

// Set the vertex and pixel shaders to the active rendering pipeline.
	 context.VertexShader.SetConstantBuffers(new Buffer[] { worldMatrixBuffer }, 0, 1);
	 context.VertexShader.SetConstantBuffers(new Buffer[] { viewMatrixBuffer }, 1, 1);
	 context.VertexShader.SetConstantBuffers(new Buffer[] { projectionMatrixBuffer }, 2, 1);
	 context.DrawIndexed(indexCount, 0, 0);

Now, i included the Vertex/PixelShader.Set() here as well as the DrawIndexed and Present calls for clarity.
You can do it any way you like, but the gist of it all is you set them BEFORE the draw calls.
Obviously setting the projection for each mesh you draw is excessive and wastes precious cycles. You only need to set the projection when you change the shader or your projection changes. Like from a form resize or if you are zooming the view or changing the view distance.
The same applies with the view matrix, that only needs to be when the camera moves or you switch shaders.

To summarize then.
  • Create buffers in code that match the buffers in the shader.
  • Write to the buffers in code when the world, view or projection matrices change using a data stream and update them in the context using UpdateSubresource.
  • Set them to the shader using SetConstantBuffers when the shader is changed. The second argument to the function call is the index as defined in the shader file.
Happy coding!

And once again, thanks for the assist!

In Topic: [SOLVED] I am supposed to use Transpose but when i do...

13 March 2012 - 04:57 AM

Bump and update.

I converted the code in the DirectX SDK for Direct3D 11 Tutorial 7 into SlimDX code line by line. (Or at least i think i did)
Only a few minor changes/additions needed to be made considering c++ is by far superior to C# when it comes to sizeof() and other such "unsafe" and dangerous things... (UGH)

Either way, here is the conversion (Attached file) if anyone is interested in looking at it. I experience the same problems as before... Nothing get's shown on screen when using UpdateSubresource and SetConstantbuffers.
I know the code works in C++ but it doesn't do it when i convert to C# and SlimDX.

I am at a total loss here.
Thanks for any assistance!


Some additional information...

Using SlimDX January 2012 version.
.Net 2.0
Tried 32 and 64 bit SlimDX dll's for .net 2.0

It works when i use the Effect class. But doing it the "right" way doesn't work at all for me.