0
\$\begingroup\$

I want to do palette swapping in my game, in a fast way and without shaders, so I couldn't replace pixels in a texture, then I tried to use the SetPaletteEntries and SetCurrentTexturePalette method, but it's not working.

static int counter = 0;

// Palette swapping experiment
PALETTEENTRY entries[256];

if (counter == 0)
{
    // Fill the palette entries with 0xFFFFFFFF
    FillMemory(&entries, sizeof(entries), 0xFF);

    // Set the palette entries in the palette index.
    if (FAILED(window.device->SetPaletteEntries(0, (PALETTEENTRY*) &entries)))
        throw std::runtime_error("SetPaletteEntries()");

    // Set the current texture palette
    if (FAILED(window.device->SetCurrentTexturePalette(0)))
        throw std::runtime_error("SetCurrentTexturePalette()");

    ++counter;
}
///////////////////////////////////////////
else if (counter == 1)
{
    // Fill the palette entries with 0x8080FFFF
    for (int i = 0; i < sizeof(entries) / sizeof(PALETTEENTRY); ++i)
        entries[i] = {0x80, 0x80, 0x80, 0xFF};

    // Set the palette entries in the palette index.
    if (FAILED(window.device->SetPaletteEntries(0, (PALETTEENTRY*) &entries)))
        throw std::runtime_error("SetPaletteEntries()");

    // Set the current texture palette
    if (FAILED(window.device->SetCurrentTexturePalette(0)))
        throw std::runtime_error("SetCurrentTexturePalette()");

    ++counter;
}

I'm loading the textures with DDSTextureLoader, and no exceptions are being thrown. Thanks for reading.

\$\endgroup\$
2
\$\begingroup\$

To use palettes, your texture must be in a "color index" format; that means D3DFMT_P8 or D3DFMT_A8P8. Make sure your textures are being loaded or created in that format.

I recall that this wasn't an overly well-supported format, although perhaps that's changed since I used D3D9 (although I'd be doubtful, because as Chuck Walbourn notes in the comments, later versions of D3D have abandoned them entirely). You should probably be verifying that the format is supported through CheckDeviceFormat() regardless.

If the format isn't supported by the hardware you're running on, you'll have to fall back to another approach or emulate the feature somehow.

\$\endgroup\$
3
  • 3
    \$\begingroup\$ Direct3D 10 and later (i.e. all DXGI-based versions of Direct3D) do not support palette formats. You have to emulate them in a programmable shader. \$\endgroup\$ Feb 16 '18 at 17:18
  • \$\begingroup\$ I found that my hardware don't support that format at all. \$\endgroup\$
    – GamerH2
    Feb 17 '18 at 14:31
  • \$\begingroup\$ Most hardware doesn't support paletted formats anymore... Shaders are the way to go with emulating palette either as 1D textures (so you bind two textures at a time) or a 'global/shared' palette could be in a Constant Buffer. You just render 60 fps to emulate palette-based animation. \$\endgroup\$ Jun 12 '18 at 6:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.