Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / How to pack .rgb color in one 16-bit chanel?

Author
Message
Kuper
17
Years of Service
User Offline
Joined: 25th Feb 2008
Playing: Planescape:Torment
Posted: 25th Apr 2011 08:38
Hi!So I want to make shader which will pack color .rgb only in one chanel 16-bit.Is it possible to make?
Phaelax
DBPro Master
22
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 25th Apr 2011 11:32 Edited at: 25th Apr 2011 12:00
To some extent, yes, but you will lose quality and the color may be a different shade from the original base color.


For 32-bit colors, a component ranges between 0 and 255. For the 16-bit colors, a component ranges from 0 to 31. So first you need to convert the 32bit color to the smaller range.



You would do that conversion for each channel; red, green, blue.

Assuming the 16-bit color is stored into a Word in this manner: ARRRRRGGGGGBBBBB

Apply some bitwise math:

color16 = 32768 + b1 + (g1 << 15) + (r1 << 10)

I'll have to reboot back into XP to double check my math in DB, hopefully I'm not too far off. The reason for adding 32768 is to set the 16th bit to 1, which is the alpha channel.

Ok, I'm back. Hey my math worked! Here's an example:


Diggsey
19
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 25th Apr 2011 12:59
Are there any limits on what the colour can be? You can improve the quality a lot if you know that the brightness is always going to be the same, or two of the channels are going to be similar, etc.

[b]
Phaelax
DBPro Master
22
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 25th Apr 2011 13:18
So you think it'd be better to convert to HSL first?

Duke E
16
Years of Service
User Offline
Joined: 10th Mar 2009
Location:
Posted: 25th Apr 2011 13:56
Remember, bitwise operations do not exist below SM 4.0
http://en.wikipedia.org/wiki/High_Level_Shader_Language

Regards
Kuper
17
Years of Service
User Offline
Joined: 25th Feb 2008
Playing: Planescape:Torment
Posted: 25th Apr 2011 14:37
Thanks but can you convert this to hlsl?
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Diggsey
19
Years of Service
User Offline
Joined: 24th Apr 2006
Location: On this web page.
Posted: 25th Apr 2011 17:33 Edited at: 25th Apr 2011 17:33
Quote: "So you think it'd be better to convert to HSL first?"


No, I think it'd be better if we knew where these colour value are coming from. If they are evenly distributed over the range of colours, then RGB is fine. If two of the channels are usually similar, you could store one of them as an offset relative to the other, saving space, etc.

[b]
Duke E
16
Years of Service
User Offline
Joined: 10th Mar 2009
Location:
Posted: 25th Apr 2011 19:01 Edited at: 25th Apr 2011 19:03
I can see only one need for this, to pass more colour sets from the VS to the PS when all (2) semantic indexes are used. Maybe it can be done with extra textures storing some data? Or store colours in extra UV sets.

Though i use packed data heavily in my particle project it is very instruction costly even with just two values per pack. I would use other means if it where possible.

Regards

Login to post a reply

Server time is: 2025-05-12 22:24:40
Your offset time is: 2025-05-12 22:24:40