# Code Understanding Help

Alright I’m trying to wrap my mind around this simple piece of code. But I’m really having a little hard time in understanding some specifics, while I totally understand what it does contextually.

First off the computation inv_screen. I dont understand 1.0f/make_float2(screen) how it works dividing 1.0f by a vec2 kinda format. Similarly computation of pixel. I am also not sure how the variables x,y,jitter,d, ray_direction fall in place with respect to one another.

``````size_t2 screen = output_buffer.size();

float2 inv_screen = 1.0f/make_float2(screen) * 2.f;
float2 pixel = (make_float2(launch_index)) * inv_screen - 1.f;
float2 jitter_scale = inv_screen / sqrt_num_samples;

unsigned int samples_per_pixel = sqrt_num_samples*sqrt_num_samples;
float3 result = make_float3(0.0f);

unsigned int seed = tea<16>(screen.x*launch_index.y+launch_index.x, frame_number);
do
{
//
// Sample pixel using jittering
//
unsigned int x = samples_per_pixel%sqrt_num_samples;
unsigned int y = samples_per_pixel/sqrt_num_samples;
float2 jitter = make_float2(x-rnd(seed), y-rnd(seed));
float2 d = pixel + jitter*jitter_scale;
float3 ray_origin = eye;
float3 ray_direction = normalize(d.x*U + d.y*V + W);
``````

“First off the computation inv_screen. I dont understand 1.0f/make_float2(screen) how it works dividing 1.0f by a vec2 kinda format.”

Many standard operators are overloaded for the vector types.
screen is a size_t2 and needs to be converted to float2 to get an operator/() overload working which implements division of a float by a float2.

All these operators work component-wise.
Means 1.0f / make_float2(screen) <==> make_float2(1.0f) / make_float2(screen) <==> make_float2(1.0f / float(screen.x), 1.0f / float(screen.y))

Same for the operator*() in line 3 and operator-() in line 4.
Similarly for float3 and float4 or other types.

The jitter calculation computes a number of ray directions which are used for antialiasing. It’s basically a random sample point stratified to a sub-pixel grid in the cited implementation.