Pack RGBA texture into RGB rendertexture then unpacking it back

Hi!
I have a case where I need to pack RGBA texture to RGB render texture, read it in another shader and unpack it back to RGBA.
I’m ready to sacrifice a lot of info of the A channel of the original texture and leave RGB channels as intact as possible. I’ve tried this so far:

Let’s say I have a pixel with following values:

R: 0.123
G: 0.123
B: 0.123
A: 0.678

I’ve tried dividing R, G and B channels by 10, then I wrote second (6) third (7) and fourth (8) digit of the A channel as the second digit of each of RGB channels, like this:

R: 0.6123
G: 0.7123
B: 0.8123

Then I’ve unpacked it back. It works perfectly fine if I pack and unpack it inside the same shader, but when I write it to a render texture, read it and try to restore the original information from it I run into artifacts. Only using the “point” filtering yields more or less the expected result. And even then I have some artifacts remaining. If I use trilinear or linear it’s worse and there’s way more artifacts.

How can I attack this problem?

Thanks!

I haven’t figured out a way to sacrifice more of the Alpha channel, but I did make a function to convert 4 floats into 3 floats such that 2 of the values are 11 bit and one is 10 bit. If that’s something that would interest you let me know and I’ll dig it up and paste it here.

Yep, that would be very helpful! :slight_smile:

It’s probably easiest if I just point you at my gitlab project…

This is the shader code (it has unpacking functions only):

https://gitlab.com/lclemens/animationcooker/-/blob/master/Assets/AnimationCooker/Resources/UnpackingFunctions.hlsl

And this is my C# script… it has the packing functions. It would be trivial to convert the packing functions to a shader because I use Unity.Mathematics and it’s similar to hlsl. I think the .cs file is self contained, but if not and it needs another file you should be able to go up a directory and find the other files.

https://gitlab.com/lclemens/animationcooker/-/blob/master/Assets/AnimationCooker/Scripts/PackingUtils.cs

There is some extra stuff in there that you probably won’t need. The functions you’d be most interested in are PackThree10BitFloatsToARGB() and UnpackRGBAToThree10BitFloats(). It may work for what you’re doing, but if not and you really need to sacrifice more A to get more precision in the RGB slots, at least maybe the code will give you some ideas. I think maybe you could tweak some of those number in there to shift the precisions around.

Good luck!

Than

Thank you very much!