[GDI/DDraw7/D3D9] Dynamic palette support?

Started by
9 comments, last by Xeeynamo 12 years, 2 months ago
Hi!
I'm trying to do a porting of a 2D game from SNES/Genesis era on Windows. This game is tile-based in 16x16 images, 8 bit per pixel and a palette large 256 entries with 16 bit of depth. I develop already a level editor using GDI and I'm able to render an entire level with dynamic palette and dynamic tiles in 1680x1050 in 24 frame per second (i think that it's a good result) using various DC in 4bpp and 8bpp. After developed level editor, I started to create the real game, so I started to learn DirectDraw. With it I had a lot of problems like the surfaces in 4bpp that can be created but can't be blitted on backbuffer; after a lot of tries and combinations I was able to create a surface in 8bpp palettized and render the entire level like GDI at 26 fps but with the 90% of use of GPU (!!!) without to be able to flip/mirror the tiles. After this fail I decided to learn Direct3D. With it I'm able to set a good viewport for X0 Y0 at top-left and X640 Y480 at bottom-right and draw squares with textures using integer coordinates like GDI and DirectDraw, but not to create surfaces or textures palettized. I tried CheckDeviceType and I discovered also that my graphic card doesn't support 16bpp surfaces in fullscreen mode and now I don't know how to proceed without to use a lot of memory and without to have bad performances.
The summary of my situation is this:
I can use GDI, it can create any DC in 2bpp, 4bpp, 8bpp all paletized, 16 and 32bpp, stretch support, flip/mirroring but trasparence blit without flipping and all this via software rendering with a good use of CPU.
I can use DirectDraw, it can create surfaces in 8bpp palettized, 16 and 32bpp, stretch support and full transparence support, but no flip/mirroring and with intense use of CPU and GPU using FastBlt
I can use Direct3D, but it cannot create surfaces different from 32bpp and lacks of palette support, stretch and flip/mirroring with projection, no support for transparent textures and with intense use of CPU and a good use of GPU using DrawPrimitives with strip triangles.
Seems that new technologies lacks a lot of features and to reach my intention the easy way is to use GDI. It's possible that if an image uses less than 256 colors I'm forced to convert it in X8R8G8B8 without palette support? I studied also some consoles and also PSP and PS2 uses 8BPP textures with dynamic palettes, so I don't understand these limits from DirectX. Also with Direct3D9 to render 1200 squares (strip triangles with 4 vertices) uses 70% of a single CPU core, so really I don't understand how some games can render a lot of graphics without lags or how some porting or emulators uses dynamic palettes.
Maybe I miss some key information to go to the right way to realize this porting. I'll wait someone that can clear my ideas here.
Advertisement
You can emulate palettes in D3D - you just need a 256x1 texture to hold the palette, store the textures using say D3DFMT_A8 and use the pixel shader to combine the two.
Look at the "Texture Palettes" entry in your DirectX SDK - I believe this might be what you want.

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


Also with Direct3D9 to render 1200 squares (strip triangles with 4 vertices) uses 70% of a single CPU core, so really I don't understand how some games can render a lot of graphics without lags


There are lots of different ways of drawing things on the screen. Some are faster and some are slower. Some ways are just horribly slow and use huge amount of CPU usage.

Basically, rendering 1200 squares is a rather trivial task for any GPU. Of course there are factors such as the pixel coverage of each square and blending states (alpha blending for example) and the shader complexity. Those things put stress on the GPU.

To be CPU bound (low FPS and high CPU usage) means that your CPU is working too hard. My guess would be that you are submitting 1200 separate draw calls for your tiles. This is an absolute "no no" if you wish to have better performance.

There are lots of ways to improve your situation. Basically what you need to do is less draw calls. Draw multiple tiles sharing the same texture or material attributes with single draw call. You may use dynamic vertex buffers with all the tile data, or different forms of geometry instancing. Less draw calls is less work for the CPU.

Best regards!

You can emulate palettes in D3D - you just need a 256x1 texture to hold the palette, store the textures using say D3DFMT_A8 and use the pixel shader to combine the two.

Mmh, the shaders are completely news for me, I think that it's time to learn them :P


Look at the "Texture Palettes" entry in your DirectX SDK - I believe this might be what you want.

I haven't any sample with that name, I'm using DXSDK7a and DSDK9 June 2010. However I did other tests like create DDS or textures in photoshop with 8bpp palettized, and they won't be loaded on my D3D app, so seems that I have no chance to uses palettes in classic way.


My guess would be that you are submitting 1200 separate draw calls for your tiles. This is an absolute "no no" if you wish to have better performance.

There are lots of ways to improve your situation. Basically what you need to do is less draw calls. Draw multiple tiles sharing the same texture or material attributes with single draw call. You may use dynamic vertex buffers with all the tile data, or different forms of geometry instancing. Less draw calls is less work for the CPU.

Best regards!

Yeah, I call DrawPrimitives for every tile. So you recommend me to use dynamic vertex buffer? I need to study them, I have few days experience of D3D. My main idea was to create a big texture where all the tiles are stored and to have a square that will be rendered with different tu and tv edited in runtime that select the correct tile but I don't know how to do that... Maybe I need to use Vertex shaders?

Yeah, I call DrawPrimitives for every tile. So you recommend me to use dynamic vertex buffer? I need to study them, I have few days experience of D3D. My main idea was to create a big texture where all the tiles are stored and to have a square that will be rendered with different tu and tv edited in runtime that select the correct tile but I don't know how to do that... Maybe I need to use Vertex shaders?


Well, if you able to present the data in a static fashion, you don't need dynamic vertex buffers.

Having all textures inside a big texture (ie. having a texture atlas) helps to batch the draw calls, but it may also be a bit more difficult to edit the tile textures. Your choice.

I think that you can draw your tiles without vertex/pixel shader (especially since you have just started). For example, if you used a dynamic vertex buffer, for each tile material, you could fill the buffer with the tile position and texture coordinates of each visible tile and the draw the contents of the buffer. So, you'd have one draw call per material instead of per tile. Of course, if you used a texture atlas, you'd just provide different texture coordinates (based on the tile location in the atlas) and you could draw all tiles with a single draw call.




Best regards!

I haven't any sample with that name, I'm using DXSDK7a and DSDK9 June 2010. However I did other tests like create DDS or textures in photoshop with 8bpp palettized, and they won't be loaded on my D3D app, so seems that I have no chance to uses palettes in classic way.

It's not in the samples, it's in the help file. You also need to look at SetCurrentTexturePalette and SetPaletteEntries.

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


[quote name='Retsu90' timestamp='1328448339' post='4909808']
I haven't any sample with that name, I'm using DXSDK7a and DSDK9 June 2010. However I did other tests like create DDS or textures in photoshop with 8bpp palettized, and they won't be loaded on my D3D app, so seems that I have no chance to uses palettes in classic way.

It's not in the samples, it's in the help file. You also need to look at SetCurrentTexturePalette and SetPaletteEntries.
[/quote]That only works if the graphics card supported palletised textures, which no current cards do.
Okay, I found a good and easy solution! I'm using TGA palettized textures, I'm able to load them and also change the palette :)
Out of interest, how are you loading them? D3DXCreateTextureFromFile? Surely D3D will still be trying to create a D3DFMT_P8 format texture or similar behind the scenes (Which is very poorly supported)?

This topic is closed to new replies.

Advertisement