Maths question. Is it possible to store multiple greyscale textures inside one greyscale texture.

I understand it is possible to combine multiple black/white mask textures into one grey scale texture, and de-combine them into separate masks in a shader.

My question: How would you combine/encode grey scale textures that contain a larger range of values than black and white and how would I de-combine/de-encode them in the shader?
Is this even possible?

Any Ideas/thoughts are welcome and very much appreciated :slight_smile:

why dont you just use packed textures?
Store each greyscale texture in the single RGB channels

RGBA channel packing would allow me to store 4 greyscale textures inn one texture. However I am looking for a way to store ALOT more than 4 textutures in one texture.

Search for bit-packing.

What is your actual use case?

Thankyou Deathrey
Unfortunately this seems too much for my small brain; I am not a programmer. All the same, you have helped me realize the complexity of what I was asking for.

My use case is rather alot to explain, but my simple brain thought “Hey, I dont need all the bit depth of an 8-bit greyscale texture. Maybe I can can store 30 low bit-depth textures inside one high-bit deth texture. Think of all the GPU memory and texture lookups I could save”

But ofcourse it is not that simple :L

Edit: After playing around with packing alpha masks, this is what I came up with. This shader encodes a binary black/white mask into a greyscale texture, and then decodes it.

It works fine and I can technically pack more than 1 mask. However I want to pack grey scale images, not binary black/white masks. Can someone confirm for me whether this is actually possible?

You could, via material shaders, do a lot of freaky things, like what you did above. Do the “encoding” when making the texture in Photoshop, then “decode” it using a material setup. You can also use the If node to only search for specific values, theoretically storing up to 256 different 1-bit “overlays” into a single uncompressed grayscale texture (though each pixel can only be used for one layer, and you’d need more than 1 bits for properly anti-aliasing round edges).

I actually did something similar to this for The Perfect Tile System: I have a dual coloring option that shifts from black to white at a user-defined midpoint, then black to white again, each side overlaid with different colors. It’s a really cool effect, and allowed me to get multiple surfaces from a single grayscale channel.

Is it possible to store multiple greyscale textures inside one greyscale texture.
Sounds like a texture atlas to me.

But in 8 bits you can only store as much as 8 black/white masks, not more. Or two 4-bit masks. Or 1 7 bit grayscale image and 1 black and white mask. Or anything really, as long as it fits.

Yes. You can use bitpacking in any way you can imagine, for example storing 16 8 bit greyscale masks in a single 32 bit RGBA texture.

Question is… should you ?

Keep in mind that any non linear encoding/decoding scheme will not work with bilinear filtering, mipmaps or compression. These all are quite important for quality and performance. So you save small amount of memory but pay extra cost of manual bilinear filtering, loss of mipmaps and bit packing decoding. I would not do this because of these severe limitations.
Instead I would store each mask as separate texture using alpha compression(BC4) and use as low texture size as possible can. This way you get compression(4bit per texel), filtering and mipmaps support. Remember that mipmapping enabled efficient streaming which is very much needed if memory is issue. Also if mask does not need all to be same size you can will save great amount of memory with just by lowering texture size for non important masks.

Thankyou so much for that useful information **Deathrey.Im sure It will help me **work things out. Indeed, Question is… should I? I dont know XD

**Kalle_H**Thanks very much for that information. You asked me earlier what my use case is; I am experimenting with creating volumetric lightingmaps and effects.

Currently, my method involves interpolating between a vertical stack of about 80 2k greyscale textures (which are all rendered in a post-process shader). Sampling so many textures is inefficient, and the GPU memory consumption of so many textures is also too high.

I would love to hear peoples thoughts on the matter.

If you are doing something like that, likely you have selected completely wrong approach to a task you are facing.

That post would be much more helpful if you’d actually mention the right approach as well. :slight_smile:

When you say vertical what do you actually mean? Are you trying to do something similar that this? Physically-based & Unified Volumetric Rendering in Frostbite - Frostbite

texture atlas + RGBA channels .

Use a 2048x2048x80 volume texture instead. Check out the docs, you can generate a volume texture asset from your separate textures and sample it directly in the materials using a 3D coordinate.