欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > windows C++ 并行编程-C++ AMP 图形(二)

windows C++ 并行编程-C++ AMP 图形(二)

2024/10/24 18:24:07 来源:https://blog.csdn.net/m0_72813396/article/details/141539221  浏览:    关键词:windows C++ 并行编程-C++ AMP 图形(二)

文中的"显存"还没有找到合适的中文翻译,它的英文名称是texture, 在应用开发者来看,texture是一个名词,在物理上指的是 GPU显存中一段连续的空间。

读取显存对象

可以使用 texture::operator[]、texture::operator() 运算符或 texture::get 方法从 texture 对象中读取。 两个运算符将返回一个值,而不是引用。 因此,你不能使用 texture 写入 texture::operator\[\] 对象。

void readTexture() {std::vector<int_2> src;for (int i = 0; i <16 *32; i++) {int_2 i2(i, i);src.push_back(i2);}std::vector<int_2> dst(16* 32);array_view<int_2, 2> arr(16, 32, dst);arr.discard_data();const texture<int_2, 2> tex9(16, 32, src.begin(), src.end());parallel_for_each(tex9.extent, [=, &tex9] (index<2> idx) restrict(amp) { // Use the subscript operator.arr[idx].x += tex9[idx].x; // Use the function () operator.arr[idx].x += tex9(idx).x; // Use the get method.arr[idx].y += tex9.get(idx).y; // Use the function () operator.arr[idx].y += tex9(idx[0], idx[1]).y;});arr.synchronize();
}

 下面的代码示例演示如何在短矢量中存储显存通道,然后访问作为短矢量属性的各个标量元素。

void UseBitsPerScalarElement() { // Create the image data. // Each unsigned int (32-bit) represents four 8-bit scalar elements(r,g,b,a values).const int image_height = 16;const int image_width = 16;std::vector<unsigned int> image(image_height* image_width);extent<2> image_extent(image_height, image_width);// By using uint_4 and 8 bits per channel, each 8-bit channel in the data source is // stored in one 32-bit component of a uint_4.texture<uint_4, 2> image_texture(image_extent, image.data(), image_extent.size()* 4U,  8U);// Use can access the RGBA values of the source data by using swizzling expressions of the uint_4.parallel_for_each(image_extent,[&image_texture](index<2> idx) restrict(amp){ // 4 bytes are automatically extracted when reading.uint_4 color = image_texture[idx];unsigned int r = color.r;unsigned int g = color.g;unsigned int b = color.b;unsigned int a = color.a;});
}

 下表列出了每种短矢量类型的每通道有效位数。

 写入显存对象

使用 texture::set 方法可写入 texture 对象。 显存对象可以是只读或读/写属性。 显存对象若要可读写,必须满足以下条件:

T 只有一个标量组件。 (不允许使用短矢量。)

T 不是 double、norm 或 unorm。

texture::bits_per_scalar_element 属性为 32。

如果不符合这三个条件,则 texture 对象为只读对象。 编译期间将检查前两个条件。 如果有代码尝试写入 readonly 显存对象,将产生编译错误。 texture::bits_per_scalar_element 条件在运行时进行检查,如果尝试写入只读的 texture 对象,则运行时将产生 unsupported_feature 异常。

下面的代码示例向一个显存对象写入了多个值。

void writeTexture() {texture<int, 1> tex1(16);parallel_for_each(tex1.extent, [&tex1] (index<1> idx) restrict(amp) {tex1.set(idx, 0);});
}
 复制显存对象

如下面的代码示例所示,可以使用 copy 函数或 copy_async 函数在显存对象之间进行复制。

void copyHostArrayToTexture() { // Copy from source array to texture object by using the copy function.float floatSource[1024* 2];for (int i = 0; i <1024* 2; i++) {floatSource[i] = (float)i;}texture<float_2, 1> floatTexture(1024);copy(floatSource, (unsigned int)sizeof(floatSource), floatTexture);// Copy from source array to texture object by using the copy function.char charSource[16* 16];for (int i = 0; i <16* 16; i++) {charSource[i] = (char)i;}texture<int, 2> charTexture(16, 16, 8U);copy(charSource, (unsigned int)sizeof(charSource), charTexture);// Copy from texture object to source array by using the copy function.copy(charTexture, charSource, (unsigned int)sizeof(charSource));
}

你还可以使用 texture::copy_to 方法从一个纹理复制到另一个纹理。 这两个纹理可以位于不同的 accelerator_view 上。 当复制到 writeonly_texture_view 对象时,数据将复制到基础 texture 对象。 源和目标 texture 对象上的每标量元素位数和范围必须相同。 如果不符合这些需求,运行时将引发异常。

显存视图类

C++ AMP 介绍了 Visual Studio 2013 中的 texture_view 类。 显存视图支持与 texture 类相同的纹素类型和秩,但是与显存不同,它们允许访问其他硬件功能,如显存采样和 mipmap。 显存视图支持对基础显存数据进行只读、只写和读/写访问。

  • 只读访问由 texture_view<const T, N> 模板专用化提供,支持具有 1 个、2 个或 4 个组件的元素、显存采样以及动态访问在实例化视图时确定的一系列 mipmap 级别;
  • 写入访问由非专用模板类 texture_view<T, N> 提供,支持具有 2 个或 4 个组件的元素,并且可以访问在实例化视图时确定的一个 mipmap 级别。 它不支持采样;
  • 读写访问由非专用模板类 texture_view<T, N> 提供,与显存一样,支持仅具有 1 个组件的元素;视图可以访问在实例化视图时确定的一个 mipmap 级别。 它不支持采样;

显存视图类似于数组视图,但是不提供 array_view 类通过 array 类提供的自动数据管理和移动功能。 texture_view 只能在基础显存数据所在的快捷键视图中进行访问。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com