ysh329 / OpenCL-101

Learn OpenCL step by step.
123 stars 31 forks source link

【问题排查】MacOS X86-intel gpu OpenCL计算错误但移动端GPU和Windows-NV 计算正确:cl::Image2D的sampler_t中的CLK_NORMALIZED_COORDS_<TRUE | FALSE> #37

Open ysh329 opened 3 years ago

ysh329 commented 3 years ago
  const sampler_t sampler =
      CLK_NORMALIZED_COORDS_TRUE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST;

改为

  const sampler_t sampler =
      CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP | CLK_FILTER_NEAREST;
ysh329 commented 3 years ago

sampler_t

A type used to control how elements of a 2D or 3D image object are read by read_image{f|i|ui}.

const sampler_t <sampler name> = <value>

Description

The image read functions take a sampler argument. The sampler can be passed as an argument to the kernel using clSetKernelArg, or it can be a constant variable of type sampler_t declared in the program source. Sampler variables in a program are declared to be of type sampler_t. The sampler_t type is a 32-bit unsigned int constant and is interpreted as a bit-field that specifies the following properties:

  1. Addressing Mode;
  2. Filter Mode;
  3. Normalized Coordinates.

These properties control how elements of a 2D or 3D image object are read by read_image{f|i|ui}. Samplers can also be declared as global constants in the program source using the syntax shown at the top of this page. The sampler fields are described in the table below:

<normalized coords> Specifies whether the x, y and z coordinates are passed in as normalized or unnormalized values. This must be one of the following predefined enums: CLK_NORMALIZED_COORDS_TRUE or CLK_NORMALIZED_COORDS_FALSE. In OpenCL 1.0, the samplers specified with an image in multiple read_image{f|i|ui} calls declared in a kernel must use the same value for <normalized coords>.

ref: https://www.khronos.org/registry/OpenCL/sdk/1.0/docs/man/xhtml/sampler_t.html

ysh329 commented 3 years ago

类似stackoverflow问题:https://stackoverflow.com/questions/23417927/opencl-read-image-returns-a-blank-pixel

Depending on what values you are passing for WRATIO and HRATIO, the problem is very like that you're using CLK_NORMALIZED_COORDS_TRUE and should likely be using CLK_NORMALIZED_COORDS_FALSE. The latter lets you use pixel coordinates instead of 0.0 to 1.0 "normalized" coordinates.

Also, while you're at it, be aware that with CLK_FILTER_LINEAR and float2 coordinates, integral coordinates will give filtered results. Add (float2)(0.5f, 0.5f) to get unfiltered values. In other words, for the pixel at (2,6) read (2.5, 6.5). This will matter when your ratio is near 1.0 so you don't get a fuzzy image.