OpenCL C 8.图像寻址和过滤

8. Image Addressing and Filtering

8.图像寻址和过滤

Let wt, ht and dt be the width, height (or image array size for a 1D image array) and depth (or image array size for a 2D image array) of the image in pixels. Let coord.xy (also referred to as (s,t)) or coord.xyz (also referred to as (s,t,r)) be the coordinates specified to read_image{f|i|ui}. The sampler specified in read_image{f|i|ui} is used to determine how to sample the image and return an appropriate color.

设wt、ht和dt为图像的宽度、高度(或1D图像阵列的图像阵列大小)和深度(或2D图像阵列的信息阵列大小),单位为像素。设coord.xy(也称为(s,t))或coord.xyz(也称为“s,t,r”)为指定给read_image{f|i|ui}的坐标。read_image{f|i|ui}中指定的采样器用于确定如何对图像进行采样并返回适当的颜色。

8.1. Image Coordinates

8.1. 图像坐标

This affects the interpretation of image coordinates. If image coordinates specified to read_image{f|i|ui} are normalized (as specified in the sampler), the st, and r coordinate values are multiplied by wt, ht, and dt respectively to generate the unnormalized coordinate values. For image arrays, the image array coordinate (i.e. t if it is a 1D image array or r if it is a 2D image array) specified to read_image{f|i|ui} must always be the un-normalized image coordinate value.

这会影响图像坐标的解释。如果指定给read_image{f|i|ui}的图像坐标被归一化(如采样器中指定的),则s、t和r坐标值分别乘以wt、ht和dt以生成未归一化的坐标值。对于图像阵列,指定为read_image{f|i|ui}的图像阵列坐标(即,如果是1D图像阵列,则为t,如果是2D图像数组,则为r)必须始终是未归一化的图像坐标值。

Let (u,v,w) represent the unnormalized image coordinate values.

设(u,v,w)表示未归一化的图像坐标值。

8.2. Addressing and Filter Modes

8.2. 寻址和过滤模式

We first describe how the addressing and filter modes are applied to generate the appropriate sample locations to read from the image if the addressing mode is not CLK_ADDRESS_REPEAT nor CLK_ADDRESS_MIRRORED_REPEAT.

我们首先描述如果寻址模式既不是CLK_ADDRESS_REPEAT也不是CLK_ADDR MIRRORED_REPEAT,则如何应用寻址和滤波模式来生成从图像中读取的适当采样位置。

After generating the image coordinate (u,v,w) we apply the appropriate addressing and filter mode to generate the appropriate sample locations to read from the image.

在生成图像坐标(u,v,w)后,我们应用适当的寻址和滤波模式来生成从图像中读取的适当样本位置。

If values in (u,v,w) are INF or NaN, the behavior of read_image{f|i|ui} is undefined.

如果(u,v,w)中的值是INF或NaN,则read_image{f|i|ui}的行为未定义。

Filter Mode CLK_FILTER_NEAREST

滤波器模式CLK_FILTER_NEAREST

When filter mode is CLK_FILTER_NEAREST, the image element in the image that is nearest (in Manhattan distance) to that specified by (u,v,w) is obtained. This means the image element at location (i,j,k) becomes the image element value, where

当滤波器模式为CLK_FILTER_NEAREST时,获得图像中最接近(曼哈顿距离)由(u,v,w)指定的图像元素。这意味着位置(i,j,k)处的图像元素变为图像元素值,其中

i = address_mode((int)floor(u))
j = address_mode((int)floor(v))
k = address_mode((int)floor(w))

For a 3D image, the image element at location (i,j,k) becomes the color value. For a 2D image, the image element at location (i,j) becomes the color value.

对于3D图像,位置(i,j,k)处的图像元素成为颜色值。对于2D图像,位置(i,j)处的图像元素成为颜色值。

The following table describes the address_mode function.

下表描述了address_mode函数。

Table 70. Addressing modes to generate texel location

表70 生成纹理像素位置的寻址模式

Addressing Mode

寻址模式

Result of address_mode(coord)

address_mode(coord)的结果

CLK_ADDRESS_CLAMP_TO_EDGE

clamp (coord, 0, size - 1)

CLK_ADDRESS_CLAMP

clamp (coord, -1, size)

CLK_ADDRESS_NONE

coord

The size term in this table is wt for u, ht for v and dt for w.

本表中的size项为u的wt、v的ht和w的dt。

The clamp function used in this table is defined as:

本表中使用的clamp功能定义为:

clamp(a, b, c) = return (a < b) ? b : ((a > c) ? c : a)

If the selected texel location (i,j,k) refers to a location outside the image, the border color is used as the color value for this texel.

如果所选纹理像素位置(i,j,k)指的是图像外部的位置,则使用边框颜色作为此纹理像素的颜色值。

Filter Mode CLK_FILTER_LINEAR

滤波器模式CLK_FILTER_LINEAR

When filter mode is CLK_FILTER_LINEAR, a 2×2 square of image elements for a 2D image or a 2×2×2 cube of image elements for a 3D image is selected. This 2×2 square or 2×2×2 cube is obtained as follows.

当滤波器模式为CLK_FILTER_LINEAR时,为2D图像选择2×2平方的图像元素,为3D图像选择2 x 2×2立方体的图像元素。这个2×2正方形或2×2×2立方体的计算方法如下。

Let

i0 = address_mode((int)floor(u - 0.5))
j0 = address_mode((int)floor(v - 0.5))
k0 = address_mode((int)floor(w - 0.5))
i1 = address_mode((int)floor(u - 0.5) + 1)
j1 = address_mode((int)floor(v - 0.5) + 1)
k1 = address_mode((int)floor(w - 0.5) + 1)
a = frac(u - 0.5)
b = frac(v - 0.5)
c = frac(w - 0.5)

where frac(x) denotes the fractional part of x and is computed as x - floor(x).

其中frac(x)表示x的分数部分,并计算为x-floor(x)。

For a 3D image, the image element value is found as

对于3D图像,图像元素值为

T = (1 - a) * (1 - b) * (1 - c) * T_i0j0k0
    + a * (1 - b) * (1 - c) * T_i1j0k0
    + (1 - a) * b * (1 - c) * T_i0j1k0
    + a * b * (1 - c) * T_i1j1k0
    + (1 - a) * (1 - b) * c * T_i0j0k1
    + a * (1 - b) * c * T_i1j0k1
    + (1 - a) * b * c * T_i0j1k1
    + a * b * c * T_i1j1k1

where T_ijk is the image element at location (i,j,k) in the 3D image.

其中T_ijk是3D图像中位置(i,j,k)处的图像元素。

For a 2D image, the image element value is found as

对于2D图像,图像元素值为

T = (1 - a) * (1 - b) * T_i0j0
    + a * (1 - b) * T_i1j0
    + (1 - a) * b * T_i0j1
    + a * b * T_i1j1

where T_ij is the image element at location (i,j) in the 2D image.

其中T_ij是2D图像中位置(i,j)处的图像元素。

If any of the selected T_ijk or T_ij in the above equations refers to a location outside the image, the border color is used as the color value for T_ijk or T_ij.

如果上述方程中选择的T_ijk或T_ij中的任何一个涉及图像外部的位置,则使用边框颜色作为T_ijk和T_ij的颜色值。

If the image channel type is CL_FLOAT or CL_HALF_FLOAT and any of the image elements T_ijk or T_ij is INF or NaN, the behavior of the built-in image read function is undefined.

如果图像通道类型为CL_FLOAT或CL_HALF_FLOAT,并且图像元素T_ijk或T_ij中的任何一个为INF或NaN,则内置图像读取功能的行为未定义。

We now discuss how the addressing and filter modes are applied to generate the appropriate sample locations to read from the image if the addressing mode is CLK_ADDRESS_REPEAT.

我们现在讨论如果寻址模式是CLK_ADDRESS_REPEAT,如何应用寻址和滤波模式来生成从图像中读取的适当采样位置。

If values in (s,t,r) are INF or NaN, the behavior of the built-in image read functions is undefined.

如果(s,t,r)中的值是INF或NaN,则内置图像读取函数的行为未定义。

Filter Mode CLK_FILTER_NEAREST

滤波器模式CLK_FILTER_NEAREST

When filter mode is CLK_FILTER_NEAREST, the image element at location (i,j,k) becomes the image element value, with ij, and k computed as

当滤波器模式为CLK_FILTER_NEAREST时,位置(i,j,k)处的图像元素变为图像元素值,其中i、j和k计算如下

u = (s - floor(s)) * w_t
i = (int)floor(u)
if (i > w_t - 1)
    i = i - w_t

v = (t - floor(t)) * h_t
j = (int)floor(v)
if (j > h_t - 1)
    j = j - h_t

w = (r - floor(r)) * d_t
k = (int)floor(w)
if (k > d_t - 1)
    k = k - d_t

For a 3D image, the image element at location (i,j,k) becomes the color value. For a 2D image, the image element at location (i,j) becomes the color value.

对于3D图像,位置(i,j,k)处的图像元素成为颜色值。对于2D图像,位置(i,j)处的图像元素成为颜色值。

Filter Mode CLK_FILTER_LINEAR

滤波器模式CLK_FILTER_LINEAR

When filter mode is CLK_FILTER_LINEAR, a 2×2 square of image elements for a 2D image or a 2×2×2 cube of image elements for a 3D image is selected. This 2×2 square or 2×2×2 cube is obtained as follows.

当滤波器模式为CLK_FILTER_LINEAR时,为2D图像选择2×2平方的图像元素,为3D图像选择2 x 2×2立方体的图像元素。这个2×2正方形或2×2×2立方体的计算方法如下。

Let

u = (s - floor(s)) * w_t
i0 = (int)floor(u - 0.5)
i1 = i0 + 1
if (i0 < 0)
    i0 = w_t + i0
if (i1 > w_t - 1)
    i1 = i1 - w_t

v = (t - floor(t)) * h_t
j0 = (int)floor(v - 0.5)
j1 = j0 + 1
if (j0 < 0)
    j0 = h_t + j0
if (j1 > h_t - 1)
    j1 = j1 - h_t

w = (r - floor(r)) * d_t
k0 = (int)floor(w - 0.5)
k1 = k0 + 1
if (k0 < 0)
    k0 = d_t + k0
if (k1 > d_t - 1)
    k1 = k1 - d_t

a = frac(u - 0.5)
b = frac(v - 0.5)
c = frac(w - 0.5)

where frac(x) denotes the fractional part of x and is computed as x - floor(x).

其中frac(x)表示x的分数部分,并计算为x-floor(x)。

For a 3D image, the image element value is found as

对于3D图像,图像元素值为

T = (1 - a) * (1 - b) * (1 - c) * T_i0j0k0
    + a * (1 - b) * (1 - c) * T_i1j0k0
    + (1 - a) * b * (1 - c) * T_i0j1k0
    + a * b * (1 - c) * T_i1j1k0
    + (1 - a) * (1 - b) * c * T_i0j0k1
    + a * (1 - b) * c * T_i1j0k1
    + (1 - a) * b * c * T_i0j1k1
    + a * b * c * T_i1j1k1

where T_ijk is the image element at location (i,j,k) in the 3D image.

其中T_ijk是3D图像中位置(i,j,k)处的图像元素。

For a 2D image, the image element value is found as

对于2D图像,图像元素值为

T = (1 - a) * (1 - b) * T_i0j0
    + a * (1 - b) * T_i1j0
    + (1 - a) * b * T_i0j1
    + a * b * T_i1j1

where T_ij is the image element at location (i,j) in the 2D image.

其中T_ij是2D图像中位置(i,j)处的图像元素。

If the image channel type is CL_FLOAT or CL_HALF_FLOAT and any of the image elements T_ijk or T_ij is INF or NaN, the behavior of the built-in image read function is undefined.

如果图像通道类型为CL_FLOAT或CL_HALF_FLOAT,并且图像元素T_ijk或T_ij中的任何一个为INF或NaN,则内置图像读取功能的行为未定义。

We now discuss how the addressing and filter modes are applied to generate the appropriate sample locations to read from the image if the addressing mode is CLK_ADDRESS_MIRRORED_REPEAT. The CLK_ADDRESS_MIRRORED_REPEAT addressing mode causes the image to be read as if it is tiled at every integer seam with the interpretation of the image data flipped at each integer crossing. For example, the (s,t,r) coordinates between 2 and 3 are addressed into the image as coordinates from 1 down to 0. If values in (s,t,r) are INF or NaN, the behavior of the built-in image read functions is undefined.

如果寻址模式为CLK_ADDRES_MIRRORED_REPEAT,我们现在讨论如何应用寻址和滤波模式来生成从图像中读取的适当采样位置。CLK_ADDRESS_MIRRORED_REPEAT寻址模式使图像被读取,就像它在每个整数接缝处平铺一样,在每个整数交叉处翻转图像数据的解释。例如,2和3之间的(s,t,r)坐标作为从1到0的坐标被寻址到图像中。如果(s,t,r)中的值是INF或NaN,则内置图像读取函数的行为未定义。

Filter Mode CLK_FILTER_NEAREST

滤波器模式CLK_FILTER_NEAREST

When filter mode is CLK_FILTER_NEAREST, the image element at location (i,j,k) becomes the image element value, with i,j and k computed as

当滤波器模式为CLK_FILTER_NEAREST时,位置(i,j,k)处的图像元素变为图像元素值,其中i、j和k计算如下

s' = 2.0f * rint(0.5f * s)
s' = fabs(s - s')
u = s' * w_t
i = (int)floor(u)
i = min(i, w_t - 1)

t' = 2.0f * rint(0.5f * t)
t' = fabs(t - t')
v = t' * h_t
j = (int)floor(v)
j = min(j, h_t - 1)

r' = 2.0f * rint(0.5f * r)
r' = fabs(r - r')
w = r' * d_t
k = (int)floor(w)
k = min(k, d_t - 1)

For a 3D image, the image element at location (i,j,k) becomes the color value. For a 2D image, the image element at location (i,j) becomes the color value.

对于3D图像,位置(i,j,k)处的图像元素成为颜色值。对于2D图像,位置(i,j)处的图像元素成为颜色值。

Filter Mode CLK_FILTER_LINEAR

滤波器模式CLK_FILTER_LINEAR

When filter mode is CLK_FILTER_LINEAR, a 2×2 square of image elements for a 2D image or a 2×2×2 cube of image elements for a 3D image is selected. This 2×2 square or 2×2×2 cube is obtained as follows.

当滤波器模式为CLK_FILTER_LINEAR时,为2D图像选择2×2平方的图像元素,为3D图像选择2 x 2×2立方体的图像元素。这个2×2正方形或2×2×2立方体的计算方法如下。

Let

s' = 2.0f * rint(0.5f * s)
s' = fabs(s - s')
u = s' * w_t
i0 = (int)floor(u - 0.5f)
i1 = i0 + 1
i0 = max(i0, 0)
i1 = min(i1, w_t - 1)

t' = 2.0f * rint(0.5f * t)
t' = fabs(t - t')
v = t' * h_t
j0 = (int)floor(v - 0.5f)
j1 = j0 + 1
j0 = max(j0, 0)
j1 = min(j1, h_t - 1)

r' = 2.0f * rint(0.5f * r)
r' = fabs(r - r')
w = r' * d_t
k0 = (int)floor(w - 0.5f)
k1 = k0 + 1
k0 = max(k0, 0)
k1 = min(k1, d_t - 1)

a = frac(u - 0.5)
b = frac(v - 0.5)
c = frac(w - 0.5)

where frac(x) denotes the fractional part of x and is computed as x - floor(x).

其中frac(x)表示x的分数部分,并计算为x-floor(x)。

For a 3D image, the image element value is found as

对于3D图像,图像元素值为

T = (1 - a) * (1 - b) * (1 - c) * T_i0j0k0
    + a * (1 - b) * (1 - c) * T_i1j0k0
    + (1 - a) * b * (1 - c) * T_i0j1k0
    + a * b * (1 - c) * T_i1j1k0
    + (1 - a) * (1 - b) * c * T_i0j0k1
    + a * (1 - b) * c * T_i1j0k1
    + (1 - a) * b * c * T_i0j1k1
    + a * b * c * T_i1j1k1

where T_ijk is the image element at location (i,j,k) in the 3D image.

其中T_ijk是3D图像中位置(i,j,k)处的图像元素。

For a 2D image, the image element value is found as

对于2D图像,图像元素值为

T = (1 - a) * (1 - b) * T_i0j0
    + a * (1 - b) * T_i1j0
    + (1 - a) * b * T_i0j1
    + a * b * T_i1j1

where T_ij is the image element at location (i,j) in the 2D image.

其中T_ij是2D图像中位置(i,j)处的图像元素。

For a 1D image, the image element value is found as

对于1D图像,图像元素值为

T = (1 - a) * T_i0
    + a * T_i1

where T_i is the image element at location (i) in the 1D image.

其中T_i是1D图像中位置(i)处的图像元素。

If the image channel type is CL_FLOAT or CL_HALF_FLOAT and any of the image elements T_ijk or T_ij is INF or NaN, the behavior of the built-in image read function is undefined.

如果图像通道类型为CL_FLOAT或CL_HALF_FLOAT,并且图像元素T_ijk或T_ij中的任何一个为INF或NaN,则内置图像读取功能的行为未定义。

 

If the sampler is specified as using unnormalized coordinates (floating-point or integer coordinates), filter mode set to CLK_FILTER_NEAREST and addressing mode set to one of the following modes - CLK_ADDRESS_NONECLK_ADDRESS_CLAMP_TO_EDGE or CLK_ADDRESS_CLAMP, the location of the image element in the image given by (i,j,k) will be computed without any loss of precision.

 

​如果采样器被指定为使用非标准化坐标(浮点或整数坐标),滤波器模式设置为CLK_FILTER_NEAREST ,寻址模式设置为以下模式之一-CLK_ADDRESS_NONECLK_ADDRESS_CLAMP_TO_EDGE 或CLK_ADDRESS_CLAMP,则将计算由(i,j,k)给出的图像中图像元素的位置,而不会损失任何精度。

 

For all other sampler combinations of normalized or unnormalized coordinates, filter and addressing modes, the relative error or precision of the addressing mode calculations and the image filter operation are not defined by this revision of the OpenCL specification. To ensure a minimum precision of image addressing and filter calculations across any OpenCL device, for these sampler combinations, developers should unnormalize the image coordinate in the kernel and implement the linear filter in the kernel with appropriate calls to read_image{f|i|ui} with a sampler that uses unnormalized coordinates, filter mode set to CLK_FILTER_NEAREST, addressing mode set to CLK_ADDRESS_NONECLK_ADDRESS_CLAMP_TO_EDGE or CLK_ADDRESS_CLAMP, and finally performing the interpolation of color values read from the image to generate the filtered color value.

 

对于归一化或非归一化坐标、滤波器和寻址模式的所有其他采样器组合,寻址模式计算和图像滤波操作的相对误差或精度未由OpenCL规范的本次修订定义。为了确保跨任何OpenCL设备的图像寻址和滤波器计算的最小精度,对于这些采样器组合,开发人员应该在内核中对图像坐标进行非标准化处理,并在内核中实现线性滤波器,使用使用非标准化坐标的采样器对read_image{f|i|ui}进行适当的调用,滤波器模式设置为CLK_FILTER_NEAREST,寻址模式设置为CLK_ADDRESS_NONECLK_ADDRESS_CLAMP_TO_EDGE 或CLK_ADDRESS_CLAMP,最后对从图像读取的颜色值进行插值以生成滤波后的颜色值。

8.3. Conversion Rules

8.3. 转换规则

In this section we discuss conversion rules that are applied when reading and writing images in a kernel.

在本节中,我们将讨论在内核中读写图像时应用的转换规则。

8.3.1. Conversion Rules for Normalized Integer Channel Data Types

8.3.1. 归一化整数通道数据类型的转换规则

In this section we discuss converting normalized integer channel data types to floating-point values and vice-versa.

在本节中,我们将讨论将归一化整数通道数据类型转换为浮点值,反之亦然。

8.3.1.1. Converting Normalized Integer Channel Data Types to Floating-point Values

8.3.1.1. 将标准化整数通道数据类型转换为浮点值

For images created with image channel data type of CL_UNORM_INT8 and CL_UNORM_INT16read_imagef will convert the channel values from an 8-bit or 16-bit unsigned integer to normalized floating-point values in the range [0.0f1.0f].

对于使用CL_UNORM_INT8和CL_UNORM_INT16图像通道数据类型创建的图像,read_imagef将通道值从8位或16位无符号整数转换为[0.0f,1.0f]范围内的归一化浮点值。

For images created with image channel data type of CL_SNORM_INT8 and CL_SNORM_INT16read_imagef will convert the channel values from an 8-bit or 16-bit signed integer to normalized floating-point values in the range [-1.0f1.0f].

对于使用CL_SNORM_INT8和CL_SNORM_INT16图像通道数据类型创建的图像,read_imagef将通道值从8位或16位带符号整数转换为范围[-1.0f,1.0f]内的归一化浮点值。

These conversions are performed as follows:

这些转换按如下方式执行:

CL_UNORM_INT8 (8-bit unsigned integer) → float

  • normalized float value = (float)c / 255.0f

  • 归一化float = (float)c / 255.0f

CL_UNORM_INT_101010 (10-bit unsigned integer) → float

  • normalized float value = (float)c / 1023.0f

  • 归一化float = (float)c / 1023.0f

CL_UNORM_INT16 (16-bit unsigned integer) → float

  • normalized float value = (float)c / 65535.0f

  • 归一化float(float)c / 65535.0f

CL_SNORM_INT8 (8-bit signed integer) → float

  • normalized float value = max(-1.0f(float)c / 127.0f)

  • 归一化floatmax(-1.0f(float)c / 127.0f)

CL_SNORM_INT16 (16-bit signed integer) → float

  • normalized float value = max(-1.0f(float)c / 32767.0f)

  • 归一化float=max(-1.0f(float)c / 32767.0f)

The precision of the above conversions is <= 1.5 ulp except for the following cases:

除以下情况外,上述转换的精度<=1.5 ulp:

For CL_UNORM_INT8

  • 0 must convert to 0.0f and

  • 0必须转换为0.0f,并且

  • 255 must convert to 1.0f

  • 255必须转换为1.0f

For CL_UNORM_INT_101010

  • 0 must convert to 0.0f and

  • 0必须转换为0.0f,并且

  • 1023 must convert to 1.0f

  • 1023必须转换为1.0f

For CL_UNORM_INT16

  • 0 must convert to 0.0f and

  • 0必须转换为0.0f,并且

  • 65535 must convert to 1.0f

  • 65535必须转换为1.0f

For CL_SNORM_INT8

  • -128 and -127 must convert to -1.0f,

  • -128和-127必须转换为-1.0f,

  • 0 must convert to 0.0f and

  • 0必须转换为0.0f,并且

  • 127 must convert to 1.0f

  • 127必须转换为1.0f

For CL_SNORM_INT16

  • -32768 and -32767 must convert to -1.0f,

  • -32768和-32767必须转换为-1.0f,

  • 0 must convert to 0.0f and

  • 0必须转换为0.0f,并且

  • 32767 must convert to 1.0f

  • 32767必须转换为1.0f

8.3.1.2. Converting Normalized Integer Channel Data Types to Half-Precision Floating-Point Values

8.3.1.2. 将归一化整数通道数据类型转换为半精度浮点值

If the cl_khr_fp16 extension is supported, then for images created with image channel data type of CL_UNORM_INT8 and CL_UNORM_INT16read_imageh will convert the channel values from an 8-bit or 16-bit unsigned integer to normalized half-precision floating-point values in the range [0.0h1.0h].

如果支持cl_khr_fp16扩展名,那么对于使用CL_UNORM_INT8 和CL_UNORM_INT16图像通道数据类型创建的图像,read_imageh将通道值从8位或16位无符号整数转换为[0.0h,1.0h]范围内的归一化半精度浮点值。

For images created with image channel data type of CL_SNORM_INT8 and CL_SNORM_INT16read_imageh will convert the channel values from an 8-bit or 16-bit signed integer to normalized half-precision floating-point values in the range [-1.0h1.0h].

对于使用CL_SNORM_INT8和CL_SNORM_INT16图像通道数据类型创建的图像,read_imageh会将通道值从8位或16位带符号整数转换为范围[-1.0h,1.0h]内的归一化半精度浮点值。

These conversions are performed as follows:

这些转换按如下方式执行:

CL_UNORM_INT8 (8-bit unsigned integer) → half

  • normalized half value = round_to_half(c / 255)

  • 归一化half = round_to_half(c / 255)

CL_UNORM_INT_101010 (10-bit unsigned integer) → half

  • normalized half value = round_to_half(c / 1023)

  • 归一化halfround_to_half(c / 1023)

CL_UNORM_INT16 (16-bit unsigned integer) → half

  • normalized half value = round_to_half(c / 65535)

  • 归一化halfround_to_half(c / 65535)

CL_SNORM_INT8 (8-bit signed integer) → half

  • normalized half value = max(-1.0hround_to_half(c / 127))

  • 归一化halfmax(-1.0hround_to_half(c / 127))

CL_SNORM_INT16 (16-bit signed integer) → half

  • normalized half value = max(-1.0hround_to_half(c / 32767))

  • 归一化halfmax(-1.0hround_to_half(c / 32767))

The precision of the above conversions is <= 1.5 ulp except for the following cases:

除以下情况外,上述转换的精度<=1.5 ulp:

For CL_UNORM_INT8

  • 0 must convert to 0.0h and

  • 0必须转换为0.0h,并且

  • 255 must convert to 1.0h

  • 255必须转换为1.0h

For CL_UNORM_INT_101010

  • 0 must convert to 0.0h and

  • 0必须转换为0.0h,并且

  • 1023 must convert to 1.0h

  • 1023必须转换为1.0h

For CL_UNORM_INT16

  • 0 must convert to 0.0h and

  • 0必须转换为0.0h,并且

  • 65535 must convert to 1.0h

  • 65535必须转换为1.0小时

For CL_SNORM_INT8

  • -128 and -127 must convert to -1.0h,

  • -128和-127必须转换为-1.0h,

  • 0 must convert to 0.0h and

  • 0必须转换为0.0h,并且

  • 127 must convert to 1.0h

  • 127必须转换为1.0h

For CL_SNORM_INT16

  • -32768 and -32767 must convert to -1.0h,

  • -32768和-32767必须转换为-1.0h,

  • 0 must convert to 0.0h and

  • 0必须转换为0.0h,并且

  • 32767 must convert to 1.0h

  • 32767必须转换为1.0h

8.3.1.3. Converting Floating-Point Values to Normalized Integer Channel Data Types

8.3.1.3. 将浮点值转换为归一化整数通道数据类型

For images created with image channel data type of CL_UNORM_INT8 and CL_UNORM_INT16write_imagef will convert the floating-point color value to an 8-bit or 16-bit unsigned integer.

对于使用CL_UNORM_INT8和CL_UNORM_INT16图像通道数据类型创建的图像,write_imagef会将浮点颜色值转换为8位或16位无符号整数。

For images created with image channel data type of CL_SNORM_INT8 and CL_SNORM_INT16write_imagef will convert the floating-point color value to an 8-bit or 16-bit signed integer.

对于使用CL_SNORM_INT8和CL_SNORM_INT16图像通道数据类型创建的图像,write_imagef会将浮点颜色值转换为8位或16位带符号整数。

The preferred method for how conversions from floating-point values to normalized integer values are performed is as follows:

如何执行从浮点值到归一化整数值的转换的首选方法如下:

float → CL_UNORM_INT8 (8-bit unsigned integer)

  • convert_uchar_sat_rte(f * 255.0f)

float → CL_UNORM_INT_101010 (10-bit unsigned integer)

  • min(convert_ushort_sat_rte(f * 1023.0f), 0x3ff)

float → CL_UNORM_INT16 (16-bit unsigned integer)

  • convert_ushort_sat_rte(f * 65535.0f)

float → CL_SNORM_INT8 (8-bit signed integer)

  • convert_char_sat_rte(f * 127.0f)

float → CL_SNORM_INT16 (16-bit signed integer)

  • convert_short_sat_rte(f * 32767.0f)

Please refer to the out-of-range behavior and saturated conversion rules.

​请参考超出范围行为和饱和转换规则。

OpenCL implementations may choose to approximate the rounding mode used in the conversions described above. If a rounding mode other than round to nearest even (_rte) is used, the absolute error of the implementation dependant rounding mode vs. the result produced by the round to nearest even rounding mode must be ≤ 0.6.

OpenCL实现可以选择近似上述转换中使用的舍入模式。如果使用四舍五入(_rte)以外的舍入模式,则依赖于实现的舍入模式与四舍五进模式产生的结果的绝对误差必须≤0.6。

float → CL_UNORM_INT8 (8-bit unsigned integer)

  • Let fpreferred = convert_uchar_sat_rte(f * 255.0f)

  • Let fapprox = convert_uchar_sat_<impl-rounding-mode>(f * 255.0f)

  • fabs(fpreferred - fapprox) must be <= 0.6

float → CL_UNORM_INT_101010 (10-bit unsigned integer)

  • Let fpreferred = convert_ushort_sat_rte(f * 1023.0f)

  • Let fapprox = convert_ushort_sat_<impl-rounding-mode>(f * 1023.0f)

  • fabs(fpreferred - fapprox) must be <= 0.6

float → CL_UNORM_INT16 (16-bit unsigned integer)

  • Let fpreferred = convert_ushort_sat_rte(f * 65535.0f)

  • Let fapprox = convert_ushort_sat_<impl-rounding-mode>(f * 65535.0f)

  • fabs(fpreferred - fapprox) must be <= 0.6

float → CL_SNORM_INT8 (8-bit signed integer)

  • Let fpreferred = convert_char_sat_rte(f * 127.0f)

  • Let fapprox = convert_char_sat_<impl_rounding_mode>(f * 127.0f)

  • fabs(fpreferred - fapprox) must be <= 0.6

float → CL_SNORM_INT16 (16-bit signed integer)

  • Let fpreferred = convert_short_sat_rte(f * 32767.0f)

  • Let fapprox = convert_short_sat_<impl-rounding-mode>(f * 32767.0f)

  • fabs(fpreferred - fapprox) must be <= 0.6

8.3.1.4. Converting Half-Precision Floating-point Values to Normalized Integer Channel Data Types

8.3.1.4. 将半精度浮点值转换为归一化整数通道数据类型

If the cl_khr_fp16 extension is supported, then for images created with image channel data type of CL_UNORM_INT8 and CL_UNORM_INT16write_imageh will convert the floating-point color value to an 8-bit or 16-bit unsigned integer.

如果支持cl_khr_fp16扩展名,那么对于使用CL_UNORM_INT8 和CL_UNORM_INT16图像通道数据类型创建的图像,write_imageh会将浮点颜色值转换为8位或16位无符号整数。

For images created with image channel data type of CL_SNORM_INT8 and CL_SNORM_INT16write_imageh will convert the floating-point color value to an 8-bit or 16-bit signed integer.

对于使用CL_SNORM_INT8和CL_SNORM_INT16图像通道数据类型创建的图像,write_imageh会将浮点颜色值转换为8位或16位带符号整数。

The preferred conversion uses the round to nearest even (_rte) rounding mode, but OpenCL implementations may choose to approximate the rounding mode used in the conversions described below. When approximate rounding is used instead of the preferred rounding, the result of the conversion must satisfy the bound given below.

首选的转换使用四舍五入到最近偶数(_rte)舍入模式,但OpenCL实现可能会选择近似下面描述的转换中使用的舍入模式。当使用近似舍入而不是首选舍入时,转换结果必须满足下面给出的界限。

half → CL_UNORM_INT8 (8-bit unsigned integer)

  • Let fexact = max(0min(f * 255255))

  • Let fpreferred = convert_uchar_sat_rte(f * 255.0f)

  • Let fapprox = convert_uchar_sat_<impl-rounding-mode>(f * 255.0f)

  • fabs(fexact - fapprox) must be <= 0.6

half → CL_UNORM_INT_101010 (10-bit unsigned integer)

  • Let fexact = max(0min(f * 10231023))

  • Let fpreferred = min(convert_ushort_sat_rte(f * 1023.0f), 1023)

  • Let fapprox = convert_ushort_sat_<impl-rounding-mode>(f * 1023.0f)

  • fabs(fexact - fapprox) must be <= 0.6

half → CL_UNORM_INT16 (16-bit unsigned integer)

  • Let fexact = max(0min(f * 6553565535))

  • Let fpreferred = convert_ushort_sat_rte(f * 65535.0f)

  • Let fapprox = convert_ushort_sat_<impl-rounding-mode>(f * 65535.0f)

  • fabs(fexact - fapprox) must be <= 0.6

half → CL_SNORM_INT8 (8-bit signed integer)

  • Let fexact = max(-128min(f * 127127))

  • Let fpreferred = convert_char_sat_rte(f * 127.0f)

  • Let fapprox = convert_char_sat_<impl_rounding_mode>(f * 127.0f)

  • fabs(fexact - fapprox) must be <= 0.6

half → CL_SNORM_INT16 (16-bit signed integer)

  • Let fexact = max(-32768min(f * 3276732767))

  • Let fpreferred = convert_short_sat_rte(f * 32767.0f)

  • Let fapprox = convert_short_sat_<impl-rounding-mode>(f * 32767.0f)

  • fabs(fexact - fapprox) must be <= 0.6

8.3.2. Conversion Rules for Half-Precision Floating-Point Channel Data Type

8.3.2. 半精度浮点通道数据类型的转换规则

For images created with a channel data type of CL_HALF_FLOAT, the conversions from half to float are lossless (as described in "The half data type"). Conversions from float to half round the mantissa using the round to nearest even or round to zero rounding mode. Denormalized numbers for the half data type which may be generated when converting a float to a half may be flushed to zero. A float NaN must be converted to an appropriate NaN in the half type. A float INF must be converted to an appropriate INF in the half type.

​对于使用CL_HALF_FLOAT通道数据类型创建的图像,从half 值到float 值的转换是无损的(如“半值数据类型”中所述)。使用舍入到最接近偶数或舍入到零的舍入模式将尾数从float 数转换为half 数。将float 数转换为half值时可能生成的half值数据类型的非规范化数可能会被刷新为零。float 数NaN必须转换为half 类型中的适当NaN。浮点INF必须转换为half 类型的适当INF。

8.3.3. Conversion Rules for Floating-Point Channel Data Type

8.3.3. 浮点通道数据类型转换规则

The following rules apply for reading and writing images created with channel data type of CL_FLOAT.

以下规则适用于读取和写入使用CL_FLOAT通道数据类型创建的图像。

  • NaNs may be converted to a NaN value(s) supported by the device.

  • NaN可以转换为设备支持的NaN值。

  • Denorms can be flushed to zero.

  • 面额可以清零。

  • All other values must be preserved.

  • 必须保留所有其他值。

8.3.4. Conversion Rules for Signed and Unsigned 8-Bit, 16-Bit and 32-Bit Integer Channel Data Types

8.3.4. 有符号和无符号8位、16位和32位整数信道数据类型的转换规则

Calls to read_imagei with channel data type values of CL_SIGNED_INT8CL_SIGNED_INT16 and CL_SIGNED_INT32 return the unmodified integer values stored in the image at specified location.

调用通道数据类型值为CL_SIGNED_INT8、CL_SIGNED_INT16和CL_SIGNE_INT32的read_imagei将返回存储在图像中指定位置的未修改整数值。

Calls to read_imageui with channel data type values of CL_UNSIGNED_INT8CL_UNSIGNED_INT16 and CL_UNSIGNED_INT32 return the unmodified integer values stored in the image at specified location.

调用通道数据类型值为CL_UNSIGNED_INT8、CL_UNSIGNED_INT16和CL_UNSINGED_INT32的read_imageui将返回存储在图像中指定位置的未修改整数值。

Calls to write_imagei will perform one of the following conversions:

调用write_imagei将执行以下转换之一:

32 bit signed integer → 8-bit signed integer

  • convert_char_sat(i)

32 bit signed integer → 16-bit signed integer

  • convert_short_sat(i)

32 bit signed integer → 32-bit signed integer

  • no conversion is performed

Calls to write_imageui will perform one of the following conversions:

调用write_imageui将执行以下转换之一:

32 bit unsigned integer → 8-bit unsigned integer

  • convert_uchar_sat(i)

32 bit unsigned integer → 16-bit unsigned integer

  • convert_ushort_sat(i)

32 bit unsigned integer → 32-bit unsigned integer

  • no conversion is performed

  • 不执行转换

The conversions described in this section must be correctly saturated.

本节中描述的转换必须正确饱和。

8.3.5. Conversion Rules for sRGBA and sBGRA Images

8.3.5. sRGBA和sBGRA图像的转换规则

Standard RGB data, which roughly displays colors in a linear ramp of luminosity levels such that an average observer, under average viewing conditions, can view them as perceptually equal steps on an average display. All 0’s maps to 0.0f, and all 1’s maps to 1.0f. The sequence of unsigned integer encodings between all 0’s and all 1’s represent a nonlinear progression in the floating-point interpretation of the numbers between 0.0f to 1.0f. For more detail, see the SRGB color standard.

​标准RGB数据,大致以亮度水平的线性斜坡显示颜色,以便在平均观看条件下,普通观察者可以将它们视为平均显示器上感知上相等的步骤。所有0映射到0.0f,所有1映射到1.0f。所有0和所有1之间的无符号整数编码序列表示0.0f到1.0f之间数字的浮点解释中的非线性级数。有关更多详细信息,请参阅SRGB颜色标准。

Conversion from sRGB space is automatically done by read_imagef built-in functions if the image channel order is one of the sRGB values described above. When reading from an sRGB image, the conversion from sRGB to linear RGB is performed before the filter specified in the sampler specified to read_imagef is applied. If the format has an alpha channel, the alpha data is stored in linear color space. Conversion to sRGB space is automatically done by write_imagef built-in functions if the image channel order is one of the sRGB values described above and the device supports writing to sRGB images.

如果图像通道顺序是上述sRGB值之一,则通过read_imagef内置函数自动完成sRGB空间的转换。当从sRGB图像读取时,在应用指定为read_imagef的采样器中指定的滤波器之前,执行从sRGB到线性RGB的转换。如果格式有阿尔法通道,阿尔法数据将存储在线性颜色空间中。如果图像通道顺序是上述sRGB值之一,并且设备支持写入sRGB图像,则write_imagef内置函数会自动转换为sRGB空间。

If the format has an alpha channel, the alpha data is stored in linear color space.

如果格式有阿尔法通道,阿尔法数据将存储在线性颜色空间中。

The following is the conversion rule for converting a normalized 8-bit unsigned integer sRGB color value to a floating-point linear RGB color value using read_imagef.

以下是使用read_imagef将归一化的8位无符号整数sRGB颜色值转换为浮点线性RGB颜色值的转换规则。

// Convert the normalized 8-bit unsigned integer R, G and B channel values
// to a floating-point value (call it c) as per rules described in section
// 8.3.1.1.

if (c <= 0.04045),
    result = c / 12.92;
else
    result = powr((c + 0.055) / 1.055, 2.4);

The resulting floating-point value, if converted back to an sRGB value without rounding to a 8-bit unsigned integer value, must be within 0.5 ulp of the original sRGB value.

如果转换回sRGB值而不四舍五入为8位无符号整数值,则生成的浮点值必须在原始sRGB值的0.5 ulp以内。

The following are the conversion rules for converting a linear RGB floating-point color value (call it c) to a normalized 8-bit unsigned integer sRGB value using write_imagef.

以下是使用write_imagef将线性RGB浮点颜色值(称为c)转换为归一化的8位无符号整数sRGB值的转换规则。

if (c is NaN)
    c = 0.0;
if (c > 1.0)
    c = 1.0;
else if (c < 0.0)
    c = 0.0;
else if (c < 0.0031308)
    c = 12.92 * c;
else
    c = 1.055 * powr(c, 1.0/2.4) - 0.055;

scaled_reference_result = c * 255
channel_component = floor(scaled_reference_result + 0.5);

The precision of the above conversion should be such that

上述转换的精度应达到

  • |generated_channel_component - scaled_reference_result| ≤ 0.6

where generated_channel_component is the actual value that the implementation produces and being checked for conformance.

其中generated_channel_component是实现生成并检查一致性的实际值。

8.4. Selecting an Image From an Image Array

8.4. 从图像阵列中选择图像

Let (u,v,w) represent the unnormalized image coordinate values for reading from and/or writing to a 2D image in a 2D image array.

设(u,v,w)表示用于从2D图像阵列中的2D图像读取或向2D图像写入的非归一化图像坐标值。

When read using a sampler, the 2D image layer selected is computed as:

当使用采样器读取时,所选的2D图像层计算如下:

  • layer = clamp(rint(w), 0, dt - 1)

otherwise the layer selected is computed as:

否则,所选层计算如下:

  • layer = w

(since w is already an integer) and the result is undefined if w is not one of the integers 0, 1, …​ dt - 1.

(因为w已经是整数),如果w不是整数0, 1, …​ dt - 1之一。

Let (u,v) represent the unnormalized image coordinate values for reading from and/or writing to a 1D image in a 1D image array.

设(u,v)表示用于从1D图像阵列中的1D图像读取或向1D图像写入的非归一化图像坐标值。

When read using a sampler, the 1D image layer selected is computed as:

当使用采样器读取时,所选的1D图像层计算如下:

  • layer = clamp(rint(v), 0, ht - 1)

otherwise the layer selected is computed as:

否则,所选层计算如下:

  • layer = v

(since v is already an integer) and the result is undefined if v is not one of the integers 0, 1, …​ ht - 1.

(因为v已经是整数),如果v不是整数0, 1, …​ ht - 1之一。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值