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.


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