[RFC,v2,23/26] libcamera: shaders: bayer_1x_packed_to_blc_glr16f.frag: Sample inputs at the texel centers
diff mbox series

Message ID 20261118095735.170587-24-bryan.odonoghue@linaro.org
State New
Headers show
Series
  • gpuisp multipass + fixes
Related show

Commit Message

Bryan O'Donoghue Nov. 18, 2026, 9:57 a.m. UTC
texture2D() takes inputs in the range 0.0f..1.0f for any x/y coordinate.
This means a byte index has to be scaled into the range 0.0f..1.0f. When we
mean to sample byte where x = 5.0 we calculate 4.964. This is very much
dependent on how an individual implementation does GL_NEAREST filtering.
For example on a reference Qualcomm system I see no error however using
SwiftShader, whose textured unit works in steps of 1/65536 we can easily
elicit an invalid read.

Change the logic of where to sample by forcing the "asked" for co-ordinate
into the middle of the texel not the periphery. We do this by adding
0.5f to each x/y before multiplying by tex_step.

The outcome looks like this:

pixel 4 of row 7 in a 2410-byte, 1088-row RAW10 frame is
byte 5 of row 7:

              byte 4          byte 5
          +-------------+-------------+
  row 6   |             |             |
          |           x |             |  x  before: read (4.96, 6.99)
          +-------------X-------------+  X  before: asked (5.0, 7.0)
  row 7   |             |             |
          |             |      o      |  o  after: asked (5.5, 7.5),
          |             |             |     read (5.48, 7.49)
          +-------------+-------------+

before: byte 4 of row 6, the low bits of pixels 0-3 on the row above
after:  byte 5 of row 7

This is easy to reproduce on SwitfShader because its texture unit is
coarser than most real hardware. For any implementation we have an upper
limit on what can be supported by frame-based floating point sampling.
Later patches will interrogate shader precision and reject resolutions
we can't support.

For now implement the immediate sampling fix as given above.

Signed-off-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org>
---
 src/libcamera/shaders/bayer_1x_packed_to_glr16f.frag | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Patch
diff mbox series

diff --git a/src/libcamera/shaders/bayer_1x_packed_to_glr16f.frag b/src/libcamera/shaders/bayer_1x_packed_to_glr16f.frag
index 418a9e063..22eb74444 100644
--- a/src/libcamera/shaders/bayer_1x_packed_to_glr16f.frag
+++ b/src/libcamera/shaders/bayer_1x_packed_to_glr16f.frag
@@ -87,7 +87,7 @@  void main(void)
 	 */
 	center_bytes.x = BPP * center_pixel.x + 0.02;
 	center_bytes.x = floor(center_bytes.x);
-	center_bytes *= tex_step;
+	center_bytes = (center_bytes + 0.5) * tex_step;
 
 	float C = texture2D(tex_y, center_bytes).r;