Archived

This topic is now archived and is closed to further replies.

Raloth

pixel shader troubles

Recommended Posts

I''m trying to write a pixel shader that scales down texture coordinates. It seems like I need at least ps_1_4 in order to get access to the coordinates, but anything I try ends up crashing. Can anyone give me a quick example of how I would do this? Maybe it would be easier for me to just go to ps_2_0...

Share this post


Link to post
Share on other sites
In HLSL your shader might look something like:

sampler s;

float4 main(float2 t : TEXCOORD0) : COLOR
{
return tex2D(s, t * 0.25);
}


In ps_1_4 assembly, your shader might look something like:

ps_1_4
def c0, 0.25, 0.25, 0.25, 0
texcrd r0.xyz, t0
mul r0.xyz, r0, c0
phase
texld r0, r0

xyzzy

Share this post


Link to post
Share on other sites
Thanks! I had the exact same thing before except I was missing the .xyz (using the assembly one) . Why is it needed? Also, it works in the opposite direction I wanted. I wanted the texture to repeat more often and not less, but changing the .25s to something greater than 1 has no effect.

[edit] Actually, I can use it the way it is. There's a ton I need to add though... Does ps_2_0 have that instruction flow thing where the instructions need to come in a specific order?

[edited by - Raloth on January 18, 2004 3:21:00 PM]

Share this post


Link to post
Share on other sites
This is what I have so far, but it doesn't work right .

ps_1_4

def c0, 0.125, 0.125, 0.125, 0.125

texcrd r0.xyz,t0
mul r0.xyz,r0,c0

phase

texld r0,r0
texld r1,t0
mul r0,r0,r1.w
I'm trying to make it so it uses the alpha channel of the texture like a detail texture, but the alpha channel doesn't show up or anything. I know it is there from a different one I had, but as soon as I added the scaling of the coordinates for the rgb channels something broke.

[edit] When I put "mov r0,r1" at the end, nothing ends up being rendered? Shouldn't that just make it so it is the way before the scaling?

[edited by - Raloth on January 18, 2004 7:26:08 PM]

Share this post


Link to post
Share on other sites
In any ps_1_x profiles, the constant values cannot be outside the range -1.0 to 1.0. You can use the _x2,_x4,_x8 destination modifiers to scale the output of any arithmetic instruction.

Your assembly looks right. It sounds like you are not setting up state for sampler1 correctly. Note that the two 'texld' instructions in the second phase use s0 and s1 respectively. If you want them both to sample the same texture, you'll need to set the texture to both stages.

xyzzy

[edited by - xyzzy00 on January 18, 2004 11:55:44 PM]

Share this post


Link to post
Share on other sites
Thanks a lot for your help. I'm finally starting to understand this stuff. I've gotten it now to separate different channels of a texture and do some blending so I can use it how I want. The one thing that bugs me is when I scale the coordinates like that I have to use two texture stages. This was supposed to be a way for me to get around that . Is there any way to do it with 1.4, or do I have to move up to 2.0 and access the sampler register?

[edit] I remember something buried in the MSDN about the maximum number texture stages not necessarily being related to the maximum number of textures. Either way I don't know if 12 (6 per stage) texture instructions per stage is going to be enough. I will certainly have problems sticking everything into 6 temporary registers with the instruction flow thing. Has that been removed in 2.0 or does it still apply?

[edited by - Raloth on January 19, 2004 5:24:59 PM]

Share this post


Link to post
Share on other sites
Scaling texture coordinates in the pixel shader requires some arithmentic instructions before your texture loads. I don''t see how you can get around using 2 phases, unless you do the scaling in your vertex shader.

ps_2_0 is much less restrictive than ps_1_4. In ps_2_0, you have 12 temp registers, 32 constant registers, 16 samplers, 32 texture instructions, and 64 arithmetic instructions. Also, your constants are no longer limited to -1..1, and you have alot more precision in your temps.

There is a limit to the dependent texture read depth in ps_2_0 which you have to watch out for. I find it helps to still structure your code into phases.. blocks of texture instructions followed by arithmetic instructions. You can have 4 of these phases before you start hitting dependent texture read limits.

xyzzy

Share this post


Link to post
Share on other sites
It's not the phase thing I'm worried about, it's the texture stages it takes up. Can I get around that?

[edit] Looks like the only way is ps2.0... My question still holds about the instruction flow.

[edited by - Raloth on January 19, 2004 7:45:23 PM]

Share this post


Link to post
Share on other sites
In ps_2_0, since you explicitly pass the sampler to the tex instruction, you can issue multiple texture instructions all using the same sampler. Your program above becomes this in ps_2_0:

ps_2_0
def c0, 0.125, 0, 0, 0
dcl t0.xy
dcl_2d s0
mul r0.xy, t0, c0.x
texld r0, r0, s0
texld r1, t0, s0
mul r0, r0, r1.w
mov oC0, r0

Note that both texld instructions in the ps_2_0 can use s0. In ps_1_4, since each texture instruction in a phase uses a separate sampler, they used s0 and s1.

xyzzy

Share this post


Link to post
Share on other sites