Sign in to follow this  
Majestik_666

uploading textures with only certain components using glTexImage2D

Recommended Posts

Hi, for some image processing reasons, I need to upload "special" textures into the vid card ram to apply shaders and render. Those textures are stored in the cpu memory but they can contain a limited number of components , for example R , RB, GA, RGB, etc ... all the combinations of 1 to 4 channels. I've been looking at glTexImage2D to upload those textures onto the gpu ram, for 1 and 4 components it's quite easy and doesn't cause any problem, but my concern is for 2 or 3 channels, the formats offered by glTexImage2D are pretty limited and if I choose to use GL_LUMINANCE_ALPHA for a 2 components image, the channels won't be set properly .... anyone has done this kind of things before ? Thanks !

Share this post


Link to post
Share on other sites
I would say there is quite a few formats to choose from


internalformat Specifies the number of color components in
the texture. Must be 1, 2, 3, or 4, or one
of the following symbolic constants:
GL_ALPHA, GL_ALPHA4, GL_ALPHA8, GL_ALPHA12,
GL_ALPHA16, GL_LUMINANCE, GL_LUMINANCE4,
GL_LUMINANCE8, GL_LUMINANCE12,
GL_LUMINANCE16, GL_LUMINANCE_ALPHA,
GL_LUMINANCE4_ALPHA4, GL_LUMINANCE6_ALPHA2,
GL_LUMINANCE8_ALPHA8, GL_LUMINANCE12_ALPHA4,
GL_LUMINANCE12_ALPHA12,
GL_LUMINANCE16_ALPHA16, GL_INTENSITY,
GL_INTENSITY4, GL_INTENSITY8,
GL_INTENSITY12, GL_INTENSITY16, GL_R3_G3_B2,
GL_RGB, GL_RGB4, GL_RGB5, GL_RGB8, GL_RGB10,
GL_RGB12, GL_RGB16, GL_RGBA, GL_RGBA2,
GL_RGBA4, GL_RGB5_A1, GL_RGBA8, GL_RGB10_A2,
GL_RGBA12, or GL_RGBA16.



Share this post


Link to post
Share on other sites
yes there are quite a lot of formats ...
but let's say i have an 8bits image with Red and Blue channels only
and I want to upload it, with the correct data in red and blue and
0.0 for green and alpha...
that's the sort of things i want to do

Share this post


Link to post
Share on other sites
Quote:
Original post by Majestik_666
yes there are quite a lot of formats ...
but let's say i have an 8bits image with Red and Blue channels only
and I want to upload it, with the correct data in red and blue and
0.0 for green and alpha...
that's the sort of things i want to do
Why don't you just use RGB, and fill the 0.0f in the green for yourself when acquiring the original image data?

Share this post


Link to post
Share on other sites
Quote:
Original post by Majestik_666
well for memory saving purposes i do not ...
my buffers can have only 2 channels and use only the exact required space
for it ...
Why?

Don't some of the drivers expand everything to 32 bits anyways as an optimization? I read that somewhere a long time ago, I don't remember where.

Share this post


Link to post
Share on other sites
yes that's fair enough but
I only upload the buffer to the video card ram when I need to
some processing is done on the gpu, 90% of the time the buffers
will be in CPU ram and not video card ram
and when you have 8Kx8K float textures , well removing some channels
from the buffer can make quite a big difference ...

Share this post


Link to post
Share on other sites
Quote:
Original post by Majestik_666
...I only upload the buffer to the video card ram when I need to
some processing is done on the gpu, 90% of the time the buffers
will be in CPU ram and not video card ram
...


I think you are answering your own question. Keep it in one format in system memory, and another in video memory. Just simply fill in the missing components when uploading to the card.

Share this post


Link to post
Share on other sites
well that's always been the plan ...
but my question is how to upload one buffer from main memory to gpu memory
when it only has a subset of the RGBA ?

the ideal solution would be something like that :
only R Channel : GL_RED
R and B Channels : GL_RED | GL_GREEN
R and A Channels : GL_RED | GL_ALPHA
RGB Channels : GL_RGB
etc...

it'd be nice if this glTexImage was a bit more versatile in terms
of channels ...

Share this post


Link to post
Share on other sites

Does it matter that much what you label the channels as? If you're using shaders, I thought that for most purposes you could interpret the channels any way you wanted. For example you could have RGB really be CMY or something. There's no CMY texture format, but I don't think that would matter if you fine with
Quote:
Original post by Majestik_666
R and B Channels : GL_RED | GL_GREEN

Share this post


Link to post
Share on other sites
well it does and it does not ...
let's say i have an image with Red and Blue Channel
and a fragment shader doing :
R = R + G;
B = R + B;
B = B;

with this shader I just cannot swap channels etc ...

my main concern is that i don't "choose" the shaders, they're user
provided, so my texture should reflect exactly the data provided from
the main memory ...

hope it makes sense :)

Share this post


Link to post
Share on other sites
OK, first of all:
Quote:

I think you are answering your own question. Keep it in one format in system memory, and another in video memory. Just simply fill in the missing components when uploading to the card.

Do NOT do this. Every GPU since the Voodoo has native support for single channel texture formats, and will not expand these to RGBA. And only really old cards will expand dual channel formats to RGBA (GeForce2, and similar). From the GF 7x00 series onwards, RGB24 tri-channel formats are not expanded to 32bit anymore, since those GPUs can access 24bit aligned data internally.

My suggestions:

* For single channel formats, use the luminance8 format
* For dual channel formats, the luminance8-alpha8 format is ideal
* For three component formats, RGB8 will not be expanded to RGBA8 on anything equal or above the NV44.

You can easily use simple component swizzling in a pixel shader (basically this operation is free) to get the channels back into the correct order.

For very specific purposes, Nvidia created a few new compressed high precision bi-component formats on the GF 8800 series (eg. GL_COMPRESSED_RED_GREEN_RGTC2_EXT), which are specifically designed for two-component normal map representation.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this