microsoft / DirectXTK

The DirectX Tool Kit (aka DirectXTK) is a collection of helper classes for writing DirectX 11.x code in C++
https://walbourn.github.io/directxtk/
MIT License
2.55k stars 506 forks source link

Look up tables with DirectXTK #114

Closed LepelTsmok closed 6 years ago

LepelTsmok commented 6 years ago

Hello DirectXTK-Team,

does DirectXTK provide a way to render textures with a look up table for the color? Or change a specific color of a texture at runtime? Or do I have to write my own shader? I'm not used to graphiccards at all so I wouldn't like to go this way...

walbourn commented 6 years ago

DXGI does not define paletted texture formats, so Direct3D 10.x/11.x/12 do not natively support paletted textures. Generally the recommended way to deal with these is to use a build-time or load-time conversion. If using WICTextureLoader, all paletted formats are converted to RGBA formats at load time. For simplicity and to keep the code size down, the DDSTextureLoader will fail if you attempt to load a legacy paletted DDS. The DirectXTex library contains code for doing all kinds of legacy expansions including DX9-era paletted DDS files. Therefore you can use texconv to do all the required conversions offline.

Really the only time you'd need to emulate true palette formats is if you using content that relies on classic palette cycling. In this case, you'd need a custom shader to do the palette look-up for each pixel. The images themselves would consist of a Texture2D (probably in DXGI_FORMAT_R8_UNORM format), and then a Texture1D to contain the palette (probably in DXGI_FORMAT_R8G8B8A8_UNORM format). You'd then do a dependent texture read in the pixel shader to do the lookup. I don't have a paletted workflow built-in to the DirectX Tool Kit because it's really a highly-specialized scenario that most likely would only be used if you were writing a legacy game system emulator.

Replacing a specific color of a texture is referred to as color keying. This is also a very old-school approach, and most cases would be done offline or at loadtime to create an alpha channel. texconv has a -c option for doing this in software. You can certainly do this in a custom shader as well as is implemented in Direct2D effects.