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:

  • Addressing Mode

  • Filter Mode

  • Normalized Coordinates

These properties control how elements of an 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:

Sampler State Description

<normalized coords>

Specifies whether the x, y and z coordinates are passed in as normalized or unnormalized values. This must be a literal value and can be one of the following predefined enums:

CLK_NORMALIZED_COORDS_TRUE or
CLK_NORMALIZED_COORDS_FALSE.

The samplers used with an image in multiple calls to read_image{f|i|ui} declared in a kernel must use the same value for <normalized coords>.

<address mode>

Specifies the image addressing-mode i.e. how out-of-range image coordinates are handled. This must be a literal value and can be one of the following predefined enums:

CLK_ADDRESS_MIRRORED_REPEAT - Flip the image coordinate at every integer junction. This address mode can only be used with normalized coordinates. If normalized coordinates are not used, this addressing mode may generate image coordinates that are undefined.

CLK_ADDRESS_REPEAT - out-of-range image coordinates are wrapped to the valid range. This address mode can only be used with normalized coordinates. If normalized coordinates are not used, this addressing mode may generate image coordinates that are undefined.

CLK_ADDRESS_CLAMP_TO_EDGE - out-of-range image coordinates are clamped to the extent.

CLK_ADDRESS_CLAMP - out-of-range image coordinates will return a border color.

CLK_ADDRESS_NONE - for this address mode the programmer guarantees that the image coordinates used to sample elements of the image refer to a location inside the image; otherwise the results are undefined.

<filter mode>

Specifies the filtering mode to use. This must be a literal value and can be one of the following predefined enums: CLK_FILTER_NEAREST or CLK_FILTER_LINEAR.

Refer to section on Image Addressing and Filtering in the OpenCL specification for a description of these filtering modes.

Samplers cannot be declared as arrays, pointers, or be used as the type for local variables inside a function or as the return value of a function defined in a program. Samplers cannot be passed as arguments to functions called by a __kernel function. A sampler argument to a __kernel function cannot be modified.

Notes

The maximum number of samplers that can be declared in a kernel can be queried using the CL_DEVICE_MAX_SAMPLERS token in the table of OpenCL Device Queries for clGetDeviceInfo.

If <addressing mode> in sampler is CLK_ADDRESS_CLAMP, then out-of-range image coordinates return the border color. The border color selected depends on the image channel order and can be one of the following values:

  • If the image channel order is CL_A, CL_INTENSITY, CL_Rx, CL_RA, CL_RGx,CL_RGBx, CL_ARGB, CL_BGRA, or CL_RGBA, the border color is (0.0f, 0.0f,0.0f, 0.0f).
  • If the image channel order is CL_R, CL_RG, CL_RGB, or CL_LUMINANCE, the border color is (0.0f, 0.0f, 0.0f, 1.0f)

A kernel that uses a sampler with the CL_ADDRESS_CLAMP addressing mode with multiple images may result in additional samplers being used internally by an implementation. If the same sampler is used with multiple images called via read_imagef or read_image{i|ui} then it is possible that an implementation may need to allocate an additional sampler to handle the different border color values that may be needed depending on the image formats being used. These implementation allocated samplers will count against the maximum sampler values supported by the device and given by CL_DEVICE_MAX_SAMPLERS. Enqueuing a kernel that requires more samplers than the implementation can support will result in a CL_OUT_OF_RESOURCES error being returned.

Example

const sampler_t samplerA = CLK_NORMALIZED_COORDS_TRUE | CLK_ADDRESS_REPEAT | CLK_FILTER_NEAREST;

samplerA specifies a sampler that uses normalized coordinates, the repeat addressing mode and a nearest filter.

Specification

OpenCL Specification

Also see

Other Data Types, Image Read and Write Functions, Cardinality Diagram

Copyright © 2007-2010 The Khronos Group Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and/or associated documentation files (the "Materials"), to deal in the Materials without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Materials, and to permit persons to whom the Materials are furnished to do so, subject to the condition that this copyright notice and permission notice shall be included in all copies or substantial portions of the Materials.