byte color[4] vs float color[4]

Started by
7 comments, last by Jiia 18 years, 11 months ago
I have a problem - and it is all those "optimization" docs out there. Recently I decided to switch from multiple streamsources (that is, one streamsource for vertex data, one for normals, one for vertex coloring.. you get the point) and group all my vertex data into one vertexbuffer. According to most of the optimization-docs out there, the optimal size of a vertexbuffer is around 2-4Mb to remove redundant vertexbuffer switches. However, another doc claims that there is unnessescary to send more data than needed: do NOT send color as a float component when a byte is enough. Alright - that seems clever enough... but hey.. how am I supposed to keep the colordata - stored as bytes - in the same buffer that's containing floats? well, perhaps I can use a separate vertexbuffer for that.. yes that'll work. NO IT WONT. at least not in a efficient way. If you are supposed to store the colordata in a separate vertexbuffer you need to keep the colordata aligned to the vertexdata because you can not use several indexbuffers on one stream. So either ju keep using your colors as a float and keep then together with the rest of the vertex data, or you'll create a vertexbuffer big enough to align the colordata with the rest of the vertexdata - which is pure madness - because you'll waste tons of valueable video memory. Let's say you got a vertexformat containing position (xyz), normal (xyz) and two sets of texture coordinates (uv * 2) reaching and astounding 12 + 12 + 8 * 2 = 40 bytes. If you are to use a separate vertexdata for the colorbuffer, you're wasting 40b of data per vertex (just because the colordata needs to be offseted by 40b). BUT if you did use floats as you color component you'd only waste sizeof(float) - sizeof(byte) bytes per color component and vertex. However, the only way to use floats as color components is throught the programmable pipeline. Is there anyone around with some more information about this?
Ethereal
Advertisement
I'm not very experienced in this part, but for every stream source you can set the stride. So although the indexing remains the same, the size of each individual data component does not need to be the same among different buffers. So you can actually have a buffer with byte-colors and another with vertices of different size and use them with one indexing scheme in one call to DrawIndexedPrimitive. So as long as the layout in terms of indices is the same, it is supposed to work.

Greetz,

Illco
Hmm.. I thouht I had that in mind, but that was obviously wrong. :)
Ethereal
How was it obviously wrong?
Quote:Original post by Illco
How was it obviously wrong?


I meant that I thought I had an explanation to why that wouldn't work :)
Ethereal
I don't have much experience with vertex declarations - why can't you just set the declaration type to byte instead of float?
You might want to have a read of Superpig's Journal - he has a nice entry on vertex data compression and decompression...

An obvious one is to reduce normals to 2D vectors and deduce the 3rd component in a shader, which should trim a float out of your vertex declaration.

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

a byte is basically a char, it holds a value up to 256, a float has 4 bytes in it so it is more precise and you can store larger numbers in it / decimals its around 1.2x10^-38 to 3.4x10^38 roughly since this isn't true on all machines, it depends on the computer
Hmm, I'm not sure I understand the problem. A D3DCOLOR component is 4 bytes, which is the exact size as a single float. I just store my vertex format into a structure. Create an array of the structure and stuff it into a buffer.

By the way, in a vertex shader, DirectX (or the graphics card, not sure which) converts D3DCOLOR values into float[4] arrays. So if you use a shader, your shader-code won't change much.

Sorry if I'm way off, I didn't really take my time to read through.

This topic is closed to new replies.

Advertisement