• Create Account

## HLSL If Statement

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

14 replies to this topic

### #1DJTN  Members

207
Like
0Likes
Like

Posted 13 March 2012 - 02:39 PM

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.

### #2mhagain  Members

12438
Like
0Likes
Like

Posted 13 March 2012 - 02:44 PM

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.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

### #3DJTN  Members

207
Like
0Likes
Like

Posted 13 March 2012 - 03:20 PM

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

### #4MJP  Moderators

18218
Like
0Likes
Like

Posted 13 March 2012 - 03:25 PM

Why don't you tell us what the compile error is?

### #5DJTN  Members

207
Like
0Likes
Like

Posted 13 March 2012 - 03:45 PM

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.

### #6DJTN  Members

207
Like
0Likes
Like

Posted 13 March 2012 - 04:17 PM

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.

### #7mhagain  Members

12438
Like
1Likes
Like

Posted 13 March 2012 - 05:57 PM

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.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

### #8MJP  Moderators

18218
Like
0Likes
Like

Posted 13 March 2012 - 06:06 PM

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).

### #9DJTN  Members

207
Like
0Likes
Like

Posted 13 March 2012 - 06:10 PM

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.

### #10DJTN  Members

207
Like
0Likes
Like

Posted 13 March 2012 - 06:14 PM

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).

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.

### #11Hodgman  Moderators

49426
Like
0Likes
Like

Posted 13 March 2012 - 06:29 PM

SM3 is guaranteed to give you eight general float4 interpolators (TEXCOORD0-7) and two colour interpolators (COLOR0/1) that may have 8 bit precision.
That requirement is defined by the D3D software.

### #12mhagain  Members

12438
Like
0Likes
Like

Posted 13 March 2012 - 08:08 PM

SM3 is guaranteed to give you eight general float4 interpolators (TEXCOORD0-7) and two colour interpolators (COLOR0/1) that may have 8 bit precision.
That requirement is defined by the D3D software.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

### #13kunos  Members

2258
Like
0Likes
Like

Posted 13 March 2012 - 11:44 PM

until you write what the compiler says this looks more like a tv quiz show than a thread to help.

### #14DJTN  Members

207
Like
0Likes
Like

Posted 14 March 2012 - 07:24 AM

ERROR: Invalid register number: 10. Max allowed for v# register is 9. Compilation failed

-Which is what I suspected after mhagain's comment and realizing that nothing changed in this working shader except the addition of a TEXCOORD in my VS output struc.

Thanks to mhagain for pointing me in the right direction and MJP, sorry it took so long to post the error as I had an issue that took priority yesterday afternoon.

For anyone else that finds this thread, you can only have 10 input slots with PS 3.0 and DX 9.

### #15DJTN  Members

207
Like
0Likes
Like

Posted 14 March 2012 - 07:33 AM

ERROR: Invalid register number: 10. Max allowed for v# register is 9. Compilation failed

-Which is what I suspected after mhagain's comment and realizing that nothing changed in this working shader except the addition of a TEXCOORD in my VS output struc.

Thanks to mhagain for pointing me in the right direction and MJP, sorry it took so long to post the error as I had an issue that took priority yesterday afternoon.

For anyone else that finds this thread, you can only have 10 input slots with PS 3.0 and DX 9.

Oh, and I should also note that my solution will be to pack 2 slots into 1 as I use 2 float2s that take up 2 slots. I will combine them into one leaving me an open slot to use for the blending data. I'll just have to clearly document and comment the shader as to what this new var consist of as well as be creative when naming the thing for future reference.

Thanks again,
DJTN

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.