hlsl shader optimization

Most of the expense in your shader is all the samples. Look up “distance field font rendering”, if every pixel in the texture has, as lets say its alpha value, its distance in pixels from the border (the shortest distance) … then you only have to do one sample to know if its within the specified border range.

However, to answer the question about how people tend to generally do this…

Lets say your game has a TextControl which can be any one style. So if you want this sentence “Deals 300 damage!”, that is built from three text controls – since they need to be different styles. “Deals 300 damage!” could be just one.

Each text control is its own spritebatch.drawstring call … each style is a separate SpriteFont. You just have to emit controls in an efficient way, and it will be efficient.

You may or may not even need your own custom shader (colorized outlines might be a uniform). Anyhow there are also cool things you can do if you -do- have your own shader.

Actually though, unless you are having trouble hitting 60 fps or whatever your target is, and you know this is the culprit – move on to other things :smiley: I kind of doubt it is, considering how few pixels on the screen have text on them in a typical game.

@Dismiss

I already discussed using “distance field font rendering” for this in another thread, but used this shader because the implementation seemed overkill for what I wanted to achieve. (see 2D outline pixel shader on a pixel font)

I actually tested the performance difference between the shader and not drawing text at all. It was almost no difference. I currently have 400 - 500 fps on a 3440 x 1440 resolution (while also drawing 33k tiles) so framerate is not a problem (yet).
But thanks for the advice :slight_smile: .

Actually, this is really good advice. It’s easy to get caught up in performance tweaks. As someone who has gotten lost in this before, trust me… just make content. You can optimize these things later :smiley:

(Plus i’m looking forward to seeing what you’re building! :P)

1 Like

Hey. Late to the party I know.

I was curious if you could post your final usage of this shader (with render targets, sprite batch begin/end). I’m trying to put these pieces together to try it out.

Thanks!

EDIT: I would also love to know how you made your pixel font and got it to render pixel perfect

I’m not sure what Jelle did (this thread is quite old haha), but I ran into problems with pixel perfect fonts. There’s an issue with the monogame text renderer I logged here:

Unfortunately, it hasn’t been addressed. To work around this in my own projects, I just switched over to using MonoGame.Extended.BitmapFonts. I found it to work great for pixel fonts.

I talk a bit about it in a video I posted back then:

I change topics a bit later, but you can see the difference.

I hope that helps! :slight_smile:

1 Like

Hi @wazawoo

I remember I did some hacky stuff to get it right/less laggy, but if it helps you, I can open-source a part of the code for you (if I can retrieve it haha).

Like @Trinith said, this thread is quite old and my Monogame knowledge has decreased over the years, but I’ll be happy to help!

EDIT: I think I made my shader pixel perfect by accident, by rendering the font and shader with a very small scale and resizing the result again later (This was initially done for performance issues).

@Trinith Thanks much, and awesome video! I actually ended up going with BitmapFonts as well and it works great.

If anyone reading is on a Mac, the only app I could find that could make the bitmap font file is bmGlyph. All the docs mention using BMFont which is windows only. I don’t know the best approach for linux :confused:

@Jelle No worries. I’d be curious to check it out but if you have to sift through the archives it’s probably not worth. Thanks for the reply after 4 years :sweat_smile:

1 Like

@wazawoo

Damn I forgot to reply, for another year…

If you want the source code of the text renderer, you can send me a DM! I found it.

Regards