   Can a V-Shader do this? (Font related Q.)
  Hey guys :).This is going to be a very naive thing, I'm sure, but I can't find my way out of noob-ness with my mind alone, and my problem is something that would allow me to break through the noob barrier once I grasped it.I'm confused by the documentations*, which is a bad thing, but I consider myself capable of understanding it, I guess I just need a more practical approach.(*plus, some sue that old directx fx editor, which doesn't seem to be in the SDK anymore, and Nvidia FX composer seems to focus on rendering, not transforming, so it left me clueless.)But yeah.I'm working on a bitmap font renderer which should also be able to render anywhere in a scene, like maybe a Nametag-billboard hovering over a character, etc.So far, I have the whole character map in a vertexbuffer, one quad per glyph.Every quad has neutral local space coordinates.So rendering the whole set would produce all letters overlapping each other.That's the background of it.About rendering:My current approach works, but I doubt it's efficiency.Steps:- Generate an indexbuffer upon text-change, containing indices pointing to respective glyph-quads.- generate a pivot matrix, as in, specify a pivot point which is then calculated in accordance to the final width and height of the word.Then, for each letter:for (UINT i = 0; i < num_letters; ++i){//move the virtual cursor, setting the origin of the next letterD3DXMatrixTranslation(&LocalMove, cursor_pos, 0.0f,0.0f);// specify an arbitrary rotation for testing purposesD3DXMatrixRotationZ(&LocalRot, fRotation * 0.75f);  // rotate around pivotLocalTransform = Pivot * LocalRot;//move letter to the proper place (redundant?)LocalTransform = LocalMove* LocalTransform;// now local transform and rotation is done   // we can move the text anywhereD3DXMatrixTranslation(&WorldTransform, x, y, z);m_dev->SetTransform(D3DTS_WORLD,&( LocalTransform * WorldTransform));m_dev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,0,4,i*6,2); cursor_pos += m_charset.Charmap[m_text].XAdvance * 0.01f;}The above snippet is pretty much worst case, I suppose, since there is no caching done (Gonna implement that, though).So yeah. There is a lot of stuff going on, per letter...which isn't too good.My motivation behind that, originally:- No vertex buffer locks, indexbuffer gen&locks only upon text change.- Maybe some sort of instancing, as soon as I get my head around it.- Each letter is it's own object and thus could have per letter transformation effects applied.But yeah, no batching...even though they all have the same texture, and everything.It's okay for short words, but thinking about larger text doesn't feel too good to me.Would a vertexshader be able to do all those matrix transforms on the GPU, then?Letter positioning depends on a variable stride, after all, but could be passed via an array, I suppose.Well, thank you for your time :)   
  I strongly recommend you to transform all the glyphs once so they can be drawn using a single draw call. Remap each glyph to a "virtual sheet" in a given range using planar coordinates on call. Then, moving/resizing that "virtual sheet" will just take to scale the various coordinates in the VS. For example, encode the glyphs as "displacement vectors from sheet's lower left corner" and then add the position.Mapping to non-planar surfaces is more complicated, it can be done and might look good as well. I've never truly had the need to do that so far but it takes to write proper VS logic for each surface parametrization, which is likely not quite nice. It will probably also need to have some extra data to not mess up glyphs too much.   ;  Hm. That means that this will always be CPU bound then?Even if I were to render a text into a texture and have a single quad use it?(Or use your method, even though I'm not too sure what you mean exactly.)Is there a sort of sheet or something that shows when to use a vertexshader and when to use CPU transformations instead?Cause that's what I'm not sure about, I'm not asking for a premade shader here, just if it makes sense to experiment into that direction.That is what keeps me back, don't want to mess around with stuff that would be no use in the end, especially in a case like this, which looks like it might work, but...might actually not.Edit:I simply have the impression that I missed the train, and nowadays everyone seems to know everything about vertexshaders(or evreything is somehow managed by XNA or something, no idea), with only the beginnings around Directx8 had examples, and yeah, it's confusing...[Edited by - Erius on May 25, 2010 10:18:18 AM];Quote:Original post by EriusHm. That means that this will always be CPU bound then?Considering text is generally not the main asset in a game, you might go for it anyway. Just don't expect it to scale to render "a page" of characters/letters.Quote:Original post by EriusEven if I were to render a text into a texture and have a single quad use it?(Or use your method, even though I'm not too sure what you mean exactly.)Obviously not, caching the result to a texture would likely be fine. Two main caveats here: 1- ensure to properly size your texture 2- As you'll be mapping/rescaling the font quads, glyph quality will decrease as all bets by grid-fitting will be screwed. Again, you can just forget about those issues unless you have very nitpicky users or you start having walls of text.It just doesn't seem likely.Quote:Original post by EriusIs there a sort of sheet or something that shows when to use a vertexshader and when to use CPU transformations instead?I don't know. You might find some guidelines around, mostly GPU-specific. Nowadays you can do everything in the VS and it will be FAST.It's mostly a conceptual issue to me. If it's static, it can be saved and reused (CPU). If it's dynamic, it's probably better on GPU. If unsure, choose the one you're more comfortable with, prototype and benchmark later.Quote:Original post by EriusCause that's what I'm not sure about, I'm not asking for a premade shader here, just if it makes sense to experiment into that direction.That is what keeps me back, don't want to mess around with stuff that would be no use in the end, especially in a case like this, which looks like it might work, but...might actually not.Sure you should try out, the technique can be adapted to various applications. It's a general tool that you should learn to use... just in case!Quote:Original post by EriusI simply have the impression that I missed the train, and nowadays everyone seems to know everything about vertexshaders(or evreything is somehow managed by XNA or something, no idea), with only the beginnings around Directx8 had examples, and yeah, it's confusing...If it helps you feel better, I think the overall documentation quality in this case is now lower than it used to be, so I can understand your feelings. Just take take a breath and you'll crack it sooner or later. Make sure you understand at least the notions of object space and eye space.;  Heya, thanks for the in-depth reply. :)I have spent the past two days learning more about HLSL etc, and I ended up deciding that I'll split logic into two parts.Static text that is generated at loading time and is basically a baked dynamic text, and dynamic text which will, as you said, probably never be a whole page long.I'm still a noob at shaders though, so no flowing transition between those two types yet.(I'm confused at the fact hat there can be arrays passed to shaders, but so far I don't know of a method to use the values in the array according to some logic. like every 4 vertices, increase the index by one, etc)But yeah, I'm a little more confident overall.Thanks again :)