关于粗大的纹理

2023-11-30 19:58
文章标签 纹理 粗大

本文主要是介绍关于粗大的纹理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

General-purpose programming on GPU

First steps in CUDA

Giuseppe Bilotta, Eugenio Rustico, Alexis Hérault

DMI — Università di Catania
Sezione di Catania — INGV

Vector types

CUDA has built-in support for vector types: multi-dimensional data with 1 to 4 components, addressed by .x.y.z.w. Some definitions:

struct uchar1
{unsigned char x;
};struct __align__(4) ushort2
{unsigned short x, y;
};struct uint3
{unsigned int x, y, z;
};struct __builtin_align__(16) float4
{float x, y, z, w;
};

You can make them available in CPU code by including the appropriate header:

#include <vector_types.h>

Example usages:

  • images with 8-bit color (uchar3) (RGB => x, y, z);
  • images with 32-bit color and transparency (uint4) (RGBA => x, y, z, w);
  • particle systems (float3) (x, y, z: physical coordinates);

Texture memory

3D graphics uses textures to ‘draw’ fancy stuff on 3D surfaces. Texture allocation and reading are also made available to the computing part of a GPU and can improve memory access or reduce computations in some use cases.

Kernels only have read-only access to texture memory. Reading a texel (texture element) is done with a fetch at given coordinates on atexture reference.

Before it can be used, a texture reference must be bound to a memory region. Binding is done by the CPU. Multiple texture references can be bound to the same or to overlapping memory areas.

A texture reference can have one, two or three dimensions. They can be bound either to standard linear memory addresses, or to special memory allocations called CUDA Arrays.


Texture references can only be used for integers (signed and unsigned, 8-, 16- or 32-bit wide), floats, and for the corresponding 1-, 2- and 4-component (but not 3-component) vector types.

Normalization of the values (mapping the value range to [0.0, 1.0] or [-1.0, 1.0]) can also be done automatically by texture references for 8-bit or 16-bit signed and unsigned integers. In this mode, for example, an unsigned 8-bit value of 0xcd (decimal 205) will be fetched as 0.803921569f (205/255).

Texture coordinates are floats in the range [0, N) where N is the texture size in that dimension. Example: a 64×32 texture will have coordinates [0,63]×[0,31]. Textures can be set to use normalized coordinates, mapping the actual size to [0, 1) in all dimensions.

Out-of-bounds coordinates are clamped, i.e. replaced with the closest in-bound coordinate. Example: a fetch for (-3.3, 33.3) on the previous texture would retrieve (0, 31). With normalized coordinates, textures can be set to wrap out of bounds coordinates. A fetch for (1.25, -1.25) would retrieve (0.25, 0.75).

Coordinates that do not fall exactly on a texel can return either the nearest neighbour or a value interpolated linearly from the neighbouring texels.


Some high-level examples. A 1-dimensional texture of float elements, returning the corresponding element type:

texture<float, 1, cudaReadModeElementType> posTex;

A 2-dimensional texture of char4 elements, returning a float4 with components in [-1.0, 1.0]:

texture<char4, 2, cudaReadModeNormalizedFloat> pixTex;

Texture references are always static (they have file scope) and must be global (i.e. do not declare them inside a function or structure).

The texture<> construct is a high-level interface to the structure

struct textureReference {int                          normalized;enum cudaTextureFilterMode   filterMode;enum cudaTextureAddressMode  addressMode[3];struct cudaChannelFormatDesc channelDesc;
}

that allow you to choose if you want to normalize coordinates (normalized = 1), interpolate coordinates (filterMode = cudaFilterModeLinear) or set out-of-bounds coordinates to wrap in any particular direction (addressMode[i] = cudaAddressModeWrap).

Binding textures

Examples for binding textures to linear memory:

texture<float, 1> oneTex;
texture<float, 2> twoTex;
float *dVector;cudaMalloc(&dVector, width*height*sizeof(float));
cudaBindTexture(NULL, oneTex, dVector, vecsize);
cudaBindTexture2D(NULL, twoTex, dVector, twoTex.channelDesc,width, height, pitch);
cudaUnbindTexture(twoTex);
cudaUnbindTexture(oneTex);

In the 2D case it is necessary to specify the pitch, i.e. the byte length of a row. This is typically width*sizeof(element), but may be larger is the rows are padded in memory to ensure a given alignment.

The first parameter is used to retrieve the offset that must be used to access elements, but it's only needed when the memory was not allocated with cudaMalloc (e.g. a texture pointing to a subset of an existing memory area) to comply with the memory alignment requirements of textures.


Transposing an image, with and without textures:

  • CPU code to transpose an image
  • GPU code to transpose an image (no textures)
  • GPU code to transpose an image (1D textures)
  • GPU code to transpose an image (2D textures)

Further texture examples: increasing the depth of an image, and various address modes

  • include file to read/write images in PAM format
  • example PAM image
  • base GPU code with 8-to-16 bitdepth conversion
  • further texture usage examples

PAM  is a simple but flexible uncompressed bitmap format developed within the netpbm image manipulation toolkit . ImageMagick  can be used to display and convert PAM images.

CUDA Arrays

CUDA arrays are memory areas dedicate to textures. They are read-only for the GPU (and are only accessible through texture fetches), and can be written to by the CPU using cudaMemcpyToArray. Devices with capability 2.0 or higher can write to CUDA arrays using surfaces, a read-write feature similar to textures.

Allocating a CUDA array requires the specification of a channel format description matching the one used by the texture that needs to be mapped on it.


Allocation:

texture<> someTex;
cudaArray *dArray;
cudaMallocArray(&dArray, &someTex.channelDesc, width, height);

Use (can copy at an offset wo, ho in the array):

cudaMemcpyToArray(dArray, wo, ho, source, size, cudaMemcpyHostToDevice);
cudaBindTextureToArray(someTex, dArray);

Release:

cudaFreeArray(dArray);

The channel format description

The cudaChannelFormatDesc describes the format of a texture element.

struct cudaChannelFormatDesc {int x, y, z, w;enum cudaChannelFormatKind f;
}

where x, y, z, w are set to the number of bits for each component, and f is one of cudaChannelFormatKindSigned,cudaChannelFormatKindUnsignedcudaChannelFormatKindFloat.

Example, for float texels we could create a channel with

cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat);

while for short4 texels this would be

cudaCreateChannelDesc(16, 16, 16, 16, cudaChannelFormatKindSigned);

这篇关于关于粗大的纹理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/438347

相关文章

opengl纹理操作

我们在前一课中,学习了简单的像素操作,这意味着我们可以使用各种各样的BMP文件来丰富程序的显示效果,于是我们的OpenGL图形程序也不再像以前总是只显示几个多边形那样单调了。——但是这还不够。虽然我们可以将像素数据按照矩形进行缩小和放大,但是还不足以满足我们的要求。例如要将一幅世界地图绘制到一个球体表面,只使用glPixelZoom这样的函数来进行缩放显然是不够的。OpenGL纹理映射功能支持将

试用UE4的纹理数组(UTexture2DArray)

UTexture2DArray 我发现在我目前使用的版本(4.25)中,官方已经实现了纹理数组(可能在4.24或更早版本就已经实现了)。 纹理数组,其含义不言而喻。一个重要作用是可以使用更多的纹理而不受制于sampler数目限制。 这一篇里我想对官方的纹理数组进行一下简单的试用。 试用 0. 启用纹理数组 虽然我看到了代码中有UTexture2DArray这个类,不过一开始并没有在编辑器

研究纹理采样器在像素级别的采样位置

问题 【纹理采样器】是一个基础的概念。假设有一个正方形面片,顶点的UV范围是0.0~1.0,那么在这个正方形面片上采样一张纹理时,会呈现出完整的纹理。 但我现在关注的问题是,在像素级别上,采样的位置是怎样的。具体来讲:对于UV值是(0.0,0.0)的点,它对应的采样位置是纹理最左上角像素的中心?还是纹理最左上角像素的左上角?即,下面左右哪个是正确的情况? 在宏观上,尤其是像素较多的时候,二者

图形API学习工程(11):使用纹理

工程GIT地址:https://gitee.com/yaksue/yaksue-graphics 目标 实现纹理采样。 参考教程/代码范例: OpenGL: 纹理 - LearnOpenGL CN Vulkan: Images - Vulkan Tutorial D3D11: DirectX11官方SDK范例【Tutorial 7: Texture Mapping and Constant

翻译 Albert Julian Mayer 关于虚拟纹理的论文(3. 概述)

第3章:概述 本章介绍了整篇论文中使用的术语,并简要介绍了“虚拟纹理”及其挑战。 3.1 术语 目前,在纹理缓存领域存在术语不匹配的情况。一些论文将术语 “虚拟纹理” 应用于所有 “使用部分驻留在内存中的纹理的系统”,特别是类似 Clipmapping 的系统 [TSH09]、 [EC06]、 [Wei04]、 [SLT+07]。而其他论文和资源将这个术语应用于一种更新的、截然不同的大纹理支

翻译 Albert Julian Mayer 关于虚拟纹理的论文(1. 介绍)

译者前言 在搜寻关于虚拟纹理相关资料的时候,我发现了这篇论文: 这似乎是 维也纳科技大学计算机科学系 的学生 Albert Julian Mayer 的研究生学位论文。 这篇论文也出现在了 2014 GDC Adaptive Virtual Texture Rendering in Far Cry 4 的参考文献之中。 我希望通过翻译这篇论文来学习虚拟纹理的基础概念等知识。 摘要 在实时

Three.js new THREE.TextureLoader()纹理贴图使用png图片显示为黑色

问题代码如下: const texture = new THREE.TextureLoader().load('./image.png');droneGeometry = new THREE.PlaneGeometry(1, 1);droneMaterial = new THREE.MeshBasicMaterial({ map: texture});droneMesh = new THRE

OpenGL/GLUT实践:粒子系统,并添加纹理、动态模糊、边界碰撞(电子科技大学信软图形与动画Ⅱ实验)

源码见GitHub:A-UESTCer-s-Code 文章目录 1 运行效果2 实验过程2.1 基本粒子系统2.1.1 定义粒子结构2.1.2 创建粒子并初始化2.1.2.1 创建粒子2.1.2.2 初始化 2.1.3 粒子状态更新与绘制2.1.3.1 绘制2.1.3.2 更新 2.1.4 实现效果 2.2 添加纹理2.2.1 纹理添加2.2.2 渲染粒子2.2.3 实现效果 2.3 运动

blender中获取虚拟相机渲染图片上每像素对应的纹理上的像素值

示例图: 相机渲染出图后,图片上每个像素点中对应的纹理的像素值。获取这个对应关系存到数据库 基本思路是 从相机圆心发射射线接触到物体时获取接触点(三维坐标)所在三角面,通过这个三角面的三个顶点坐标及其三个纹理坐标,通过重心坐标求出接触点所对应的纹理坐标。在发射射线时,通过相机分辨率中某一点的二维坐标(即渲染出图后图片上的坐标)转三维坐标,结合相机圆心确认射线方向。至此【图片上每个像素点中

OpenGL/GLUT实践:绘制旋转的立方体与雪人世界——添加光照与SOIL方式添加纹理(电子科技大学信软图形与动画Ⅱ实验)

源码见GitHub:A-UESTCer-s-Code 文章目录 1 运行效果2 实现过程2.1 几何转换2.1.1 窗口刷新2.1.2 绘制雪人场景2.1.2.1 绘制雪人2.1.2.2 绘制场景 2.1.3 键盘事件2.1.4 运行效果 2.2 颜色2.3 光照2.3.1 绘制正方体2.3.2 添加光源 2.4 材质2.4.1 方法一2.4.2 方法二 2.5 纹理2.5.1 SOIL环境