Image sampling in a shader, RGB values

Posted By: jumpman

Image sampling in a shader, RGB values - 09/05/17 19:31

Hello!

Ive made a shader that has a cubic reflection based on the Alpha of a texture thats stored in the model. If the alpha has white, the model is more reflective there, and if its black, the model isnt reflective there.

So once I was able to sample the Alpha value of a model's second entity texture, i naturally started calling for the RGB values for other shader effects. For example, the specularity strength would use the Red value of the image, another effect uses the Green value of the image.

So if my model has 1 diffuse + 3 textures, and the shader is looking up the RGBA values of each of these 3 textures to determine the strengths of each shading component, is that giving the GPU more work to do? I obviously cannot have every model have cubic mapping + normal + specular + rim + etc. But once you call a texture as a sampler, and Float4, all the information is there already right? Does going into the RGBA values each give more work to the GPU?
Posted By: WretchedSid

Re: Image sampling in a shader, RGB values - 09/05/17 21:15

Your question is a bit weirdly phrased. Do you mean that if you do a texture lookup but only use let's say the alpha component of the returned float4 it gets cheaper? if so, highly doubtful, since the function returns a float4 and you merely discard parts of the return value. Since all modern shader compilers are SSA based you could potentially argue that if the ISA supports it the compiler could optimize it to a specific single channel fetch, but I don't think any vendor does that.

In any case, a texture lookup is incredibly cheap, especially once the texture is in the GPU cache. You can totally get away with two texture fetches per fragment in order to do your effects. That's pretty much what the whole industry does anyway.
Posted By: jumpman

Re: Image sampling in a shader, RGB values - 09/05/17 22:43

So once you declare a sampler;

Texture entskin2; //uses the stored entskin image 2

Sampler reflective
{...}

...

Float 4 reflective_pixels tex2d(reflective,...)

Specular_strength = reflective_pixels.r; //looking up the red channel
specular_falloff = reflective_pixels.g; //looking up the green channel
Specular_bounce = reflective_pixels.b; //looking up the blue channel
Cubic_strength = reflective_pixels.a; //looking up the alpha channel

Does it uses more gpu processing time to go through each RGBA value? Compared to

Float reflective_pixels tex2d(reflective...). // a single float
Specular_strength = reflective_pixels;
Posted By: WretchedSid

Re: Image sampling in a shader, RGB values - 09/06/17 00:21

It's not tied to the sampler. You can declare a sampler all day long, if you don't use it, the compiler will optimize it out. But the tex2d() lookup will have the same cost no matter what you use of the returned components. Accessing them isn't the expensive part, getting the values in the first place is. But you pay that price one way or another.
Posted By: jumpman

Re: Image sampling in a shader, RGB values - 09/06/17 03:47

thank you friend!
© 2024 lite-C Forums