Question about input coordinates in fragment shader

I am working along Chapter 12: Render Passes. Towards the end, when we pass the ID Texture that is the result of rendering to a render pass with an r32Uint texture as color attachment 0, I see this fragment shader snippet that determines wether an object has been clicked by checking the ID against the .r value encoded in the texture:

if (!is_null_texture(idTexture)) {
  uint2 coord = uint2(params.touchX * 2, params.touchY * 2);
  uint objectID = idTexture.read(coord).r;
  if (params.objectId != 0 && objectID == params.objectId) {
    material.baseColor = float3(0.9, 0.5, 0);
  }
}

Can you please explain why do we need to upscale touchX and touchY by a factor of two? In fact, when running this in Xcode simulator on iPhone 14 Pro, these coordinates are wrong and I have to click a bit to the right of the object to actually select it:

Screenshot 2022-10-11 at 14.26.36

Thanks as usual!

Looks like I took the easy way out :woman_facepalming:.

x2 is the scale factor of the screen, and the Pro Max has a scale factor of 3.

This is the fixed final sample. The changes are:

Common.h - add this to Params:

uint scaleFactor;

Add this to Renderer.init(metalView:options:):

#if os(macOS)
  params.scaleFactor = uint(NSScreen.main?.backingScaleFactor ?? 1)
#elseif os(iOS)
  params.scaleFactor = uint(UIScreen.main.scale)
#else
   params.scaleFactor = 1
#endif

In PBR.metal, change coord:

uint2 coord = uint2(params.touchX * params.scaleFactor,
                       params.touchY * params.scaleFactor);

final.zip (142.6 KB)

Resolution by iOS device:

1 Like