Site Stability Read more... ×

# HLSL - Divisions with float2

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

## Recommended Posts

Hi guys,

i would like to know if the result is correct, just be sure that i there is no concept-errors.:

float2 textureSize = (1024.0, 800.0);
float Divider = 1.0;
float2 pixel = (Divider/textureSize.xy);

the result:

pixel.x = 0.0009
pixel.y = 0.00125

Is he dividing wans x with the divider then y with the divider? Or is there something wrong, so that only one part of "pixel" variable has correct value?

Thank you very much

##### Share on other sites
The 1.0f is expanded to float2(1.0f, 1.0f). The reciprocal of 1024 is roughly 0.0009 and reciprocal of 800 is exactly 0.00125 so the calculation is correct to the expected precision.

This type of formula is often used to determine the dimensions of one source texture texel in texture space. By using the values, it is possible to find the coordinates of the neighboring texels related to the current one; just offset the current UV with the size of the texel, multiplied by the number of pixels you want to offset the coordinate by. For example, blurring requires n neighboring texels of the "current" source texel so this is useful there.

The variables in your example are poorly named, though. The "divider" is actually the dividend, and "pixel" is actually the dimensions of one pixel on the texture, given the texture size matches the actual physical texture size.

##### Share on other sites
Thank you very much for the detailled and great Answer! Did not know that 1.0 is expanded to float2(1.0f, 1.0f).

##### Share on other sites
Yeah in HLSL scalars can be expanded to vectors by duplicating. So the following are both valid:
 float2 x = 2.0f * float2(1.0f, 2.0f); // x = float2(2.0f, 4.0f) float3 y = 1.0f; // y = float2(1.0f, 1.0f, 1.0f); 

However vectors cannot be expanded to larger vectors. So if you did the following, you would get a compiler error:
 float3 x = float2(1.0f, 2.0f); 

Vectors can be truncated, but the compiler will give you a warning (since usually it's not intended). So you can do the following:
 float2 x = float3(1.0f, 2.0f, 3.0f); // x = float2(1.0f, .20f) 

Personally I really dislike implicit truncation, so I turn on "treat warnings as errors" and fix those cases by using an explicit truncation through a swizzle:
 float3 num3 = float3(1.0f, 2.0f, 3.0f); float2 num2 = num3.xy; 

##### Share on other sites
Other factoids:
You can use the type float1 if you want to forbid the ability for the value to be implicitly convertable to float2, float3, and float4.
You can use swizzle operators on constants like 3.14159.xxx to make them float3's.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 21
• 21
• 11
• 25