Jump to content
  • Advertisement
Sign in to follow this  
Spa8nky

[ASM] Can someone explain how MUL works in this case?

This topic is 2777 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm in the process of converting the following shader from ASM to HLSL:



ps_2_x

//dcl_2d s0 // Declare Sampler 0 // NOT USED
dcl_2d s1 // Declare Sampler 1

//dcl t0.xyzw // Declare input (float4) // NOT USED
dcl t1.xyzw // Declare input (float4) // [struct] input.TextureCoordinates : TEXCOORD0?

// c0 contains: left, top, width, height
// c1 contains: 1/width, 1/height, 0.5, 1
// c2 contains: kMinU, kMinV, kMaxU, kMaxV // Used only for clamping texture (is this CLAMP?)
// r0 contains: Texture Coordinates // output.TextureCoordinates?


sub r0.xy, t1, c0

mul r0.xy, r0, c1 // bring coordinates into normalized local texture coord [0..1]

frc r0.xy, r0 // if texture repeats then coords are > 1, use frc to bring
// these coords back into [0, 1) interval.
mad r0.xy, r0, c0.zw, c0.xy // transform coords back to texture atlas coords

// clamp to inside texture (to avoid bi-linear filter pulling in foreign texels)
max r0.xy, r0.xy, c2.xy
min r0.xy, r0.xy, c2.zw

dsx r1, t1 // use the original coords for mip-map calculation
dsy r2, t1
texldd_pp r0, r0, s1, r1, r2

mov_pp oC0, r0 // return r0 as Colour0


I've placed comments throughout the code to help my progress but I am stuck on the implementation of the following line:


mul r0.xy, r0, c1 // bring coordinates into normalized local texture coord [0..1]


If 'c1' contains: 1/width, 1/height, 0.5, 1 then I read that as 'c1' is float4(1 / AtlasDim.x, 1 / AtlasDim.y, 0.5, 1).

How do the 0.5 and 1 factor in the mul? I have tried the following:



float4 c1 = float4(1 / AtlasDim.x, 1 / AtlasDim.y, 0.5, 1);
r0.xy *= c1;


in my direct conversion but it doesn't work:



float4 PixelShaderFunctionA(VertexShaderOutput input) : COLOR0
{
float2 r0 = input.TextureCoordinates0.xy - input.SourceRectangle0.xy;

float4 c1 = float4(1 / AtlasDim.x, 1 / AtlasDim.y, 0.5, 1);

r0.xy *= c1.xy;

r0.xy = frac(r0.xy);

r0.xy *= input.SourceRectangle0.zw; // Scale uv
r0.xy += input.SourceRectangle0.xy; // Shift uv (rectangle offset from left of atlas)

r0.xy = max(r0.xy, c2.xy);
r0.xy = min(r0.xy, c2.zw);

float r1 = ddx(input.OriginalCoordinates.x);
float r2 = ddy(input.OriginalCoordinates.x);
float4 colour = tex2D(TextureSampler, r0, r1, r2);

float4 colour = tex2D(TextureSampler, r0);

return colour;
}


Also what does c2 hold?


// c2 contains: kMinU, kMinV, kMaxU, kMaxV // Used only for clamping texture


How can this be calculated?

Share this post


Link to post
Share on other sites
Advertisement

If 'c1' contains: 1/width, 1/height, 0.5, 1 then I read that as 'c1' is float4(1 / AtlasDim.x, 1 / AtlasDim.y, 0.5, 1).

How do the 0.5 and 1 factor in the mul? I have tried the following:



They don't. The assembly is only doing the multiplication for the XY components, and so is your HLSL code. In fact if you output your warnings, you'll find that the compiler is giving you a warning for implicit truncation of a vector (since you're truncating a float4 to a float2). The assembly later uses the zw components of c1 for something else, so the author probably just packed those two values into zw of c1 so that he could avoid using another constant register.

Share this post


Link to post
Share on other sites

[color="#1C2837"]The assembly later uses the zw components of c1 for something else,
[color="#1C2837"][/quote]
[color="#1C2837"]
[color="#1C2837"]No it doesn't? c2.zw is used but c1.zw values are never used, hence the confusion. Can you please point out where you suspect they might be used?
[color="#1C2837"]
[color="#1C2837"]Also, what do you suspect the c2 values are?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!