Color vs 1px Texture

Started by
3 comments, last by L. Spiro 7 years ago

When drawing an object in a solid color, is it more advisable to make a shader with a color input or use a shader with a texture input and use a 1px texture?

Advertisement

Whatever makes your life easier. All your other objects are textured and the color never changes/ou only need a very limited set of colors? A texture may make sense.

The color of objects frequently changes through all RGB values? A color input will be best.

In principle, avoiding the texture would be better. In practice it will not make a difference, because all shaders have a certain minimum execution time, based on the length of the execution pipeline of the GPU.

I personally don't use a texture for drawing solid color objects.

I favour the texture here as well, because in general a texture change (which that option would require) will be cheaper than a shader change (which the other option would require, in addition to potential input format and buffer changes too), but a lot of this will also depend on the reason why you're drawing a solid object. I'm hesitant to give general advice without knowing more about that.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

I would never (mis)use textures this way.

A texture is a tightly controlled resource which will incur a stall if not updated properly and which sends the GPU off on a tangent during a fetch which again needs to be properly configured for filtering and color-space (non-)conversions. Performance could vary wildly on different hardware/platforms. Some hardware will fetch the texel in advance while the pixel shader is preparing for execution so that the value is already cached, but only in certain circumstances. Others may not prefetch at all and only begin the fetch once the shader instructs it to do so, which will then always be more costly than reading a register.

Updates to the color mean updating the texture, which means a stall in a naive case or extra textures and their management in the best case (or extra data to tell the shader where to read the texture which then defeats the whole point of not uploading data to constant buffers).

Additionally, each texture you create means extra book-keeping memory for each. You can't allocate a single pixel of memory—you will always have memory allocated to store the texture size, filter modes, read flags, etc.

In contrast, a constant buffer is more freely manipulated by the GPU. Copies will be made as necessary when you update them, and you will likely already have a buffer you update for every draw call, meaning you are already paying this cost anyway. No extra effort is required to change the color, no extra memory is allocated besides for the color vector itself, reads of it in the shader will never be slower than a texture fetch, and you don't need a separate section of the pipeline to set filtering flags, color format, sRGB toggles, etc. (not to mention an algorithm or tool to create the texture).


As for the cost of swapping a shader rather than a texture, this is not as straightforward as it first seems. If you really are able to use the same shader as you use for all the textured objects, that means you have a whole set of UV's on the model all set to [0,0] or [0.5,0.5]. This is wasteful memorywise and has to be specifically prepared by an artist, and because the UV coordinates are not hardcoded, depending on the complexity of the shader there is extra likelihood that the GPU will not be able to cache the texture reads during the shader load (and again your mileage may vary depending on the GPU).

If you have one object that uses solid colors, you likely have several, so the cost of a single shader swap will be amortized over several draw calls as it gets reused, meanwhile you don't have useless UV coordinates and wasted memory.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

This topic is closed to new replies.

Advertisement