HLSL If Statement

Started by
13 comments, last by DJTN 12 years, 1 month ago
I'm trying to blend two textures together in a pixel shader. To get started I have 2 texture samplers and a position (float4) passed into the pixel shader from the vertex shader. I want to use this position to determin which sampler to sample from. So I do the following:



float4 texel;

if ( input.Position.y < 50 )
{
texel = tex2D(texsampler1, input.TexCoords);
}
else
{
texel = tex2D(texsampler2, input.TexCoords);
}

return texel;




This code fails and will not compile...


If I hard code the input.position.y to a number 1, I can compile but that is useless for obvious reasons.

I've also tried to implement a situation where I didnt need an "if statement" at all by adding the two pixels together after I multiplied them by a 1 or 0 passed in from the vertex shader but again, it fails.
Advertisement
It's not the if that's the problem, it's your use of position. This isn't actually readable from a pixel shader under normal circumstances, so you should pass it in another slot as well, e.g. a spare texcoord slot.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.


It's not the if that's the problem, it's your use of position. This isn't actually readable from a pixel shader under normal circumstances, so you should pass it in another slot as well, e.g. a spare texcoord slot.


I changed the input from the position to a new var I created in the vertex shader output strucure:


float height : COLOR4;



still fails to compile...

It doesnt matter if I hard code the value in the VS, change it to a float, float2, float4, or change the slot to TEXCOORD0, POSITION0, COLOR0.
It still fails.

I have no more hair to pull out sad.png
Why don't you tell us what the compile error is?
Error: Compilation failed.

I think I've run out of slots. POSITION0, TEXCOORD0, TEXCOORD1, TEXCOORD2, TEXCOORD3, TEXCOORD4, COLOR0, COLOR1, COLOR2, and COLOR3.

COLOR4 is what I used for the height. But it also failed with TEXCOORD5. If I use another float in the passed in structure it works.
Ok I can confirm I've run out of slots. I'm using PS 3.0 and I thought the units/slots were software (dx) dependant but it appears that they're hardware/device dependant.

Anyone have any ideas how I can work around this? Is it possible to set a global var and change it in the VS then read it in PS?

Also

What is the most common specs for devices in the last couple of years, in regards to texturecoords and color slot counts? My Nvidia GeForce 8500 GT only has 4 slots. Its not new but by no means was it a buget device.
10 ps input slots is max in SM 3 (you should be actually guaranteed 10, so I don't know why your 8400 only has 4, unless NV are pulling a GeForce 4 MX with it again). Also - beware of the color slots; they're not supposed to be any different with SM 3 class hardware, but some - particularly older/cheaper stuff - may interpolate at lower precision (on the assumption that they'll always contain color data and therefore you only need a 0 to 1 range).

If you're not using all 4 floats in any of your texcoord slots you could pack 2 into 1; e.g. if you've got 2 that are float2, pack them into a single float4 to free up a slot. Your code will look a little uglier but hey! You get an extra slot. Alternatively if some of your slots are used for passing the result of a calculation you might be able to get away with moving the calculation to the PS (which may even improve performance slightly if it gives better workload balancing between VS and PS - but then again it may not).

You can't set a global in the VS then read it in the PS; shaders don't work that way.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.


Error: Compilation failed.


That's not the compile error. That's an error telling you that compilation failed. The compile errors will be in the ID3DXBuffer that you get from the ppCompilationErrors parameters of D3DXCreateEffectFromFile or D3DXCompileShaderFromFile (assuming that you're compiling at runtime).
When I said 4 I meant 4 textcoord but after your post it's clear I've went past the 10 ps input slots. My vert output struc has 10 slots being used. When I add the last one ( making it the 11th) for the texure blending is when it fails but only when I do a calculation with that 11th slot's data.

Thanks Mhagain, I got some decisions to make.

[quote name='DJTN' timestamp='1331675144' post='4921779']
Error: Compilation failed.


That's not the compile error. That's an error telling you that compilation failed. The compile errors will be in the ID3DXBuffer that you get from the ppCompilationErrors parameters of D3DXCreateEffectFromFile or D3DXCompileShaderFromFile (assuming that you're compiling at runtime).
[/quote]


Thanks MJP. I'll look into that too but I'm pretty sure my issue is that I've reached the PS input slot limit for PS 3.0.

This topic is closed to new replies.

Advertisement