在金属中手动设置1D纹理 [英] Manually set a 1D Texture in Metal

查看:80
本文介绍了在金属中手动设置1D纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用值手动填充 1D纹理,并将该纹理传递给计算着色器(这些是我想通过代码设置的2个像素,它们不代表任何图像).

I'm trying to fill a 1D texture with values manually and pass that texture to a compute shader (these are 2 pixels that I want to set via code, they don't represent any image).

由于当前有少量的Metal示例,我可以找到的所有示例都涉及到 2D纹理,这些2D纹理通过将已加载的UIImage转换为原始字节数据来加载纹理,但创建了虚拟UIImage对我来说就像是黑客.

Due to the current small amount of Metal examples, all examples I could find deal with 2D textures that load the texture by converting a loaded UIImage to raw bytes data, but creating a dummy UIImage felt like a hack for me.

这是我开始的天真"方式-

This is the "naive" way I started with -

...
var manualTextureData: [Float] = [ 1.0, 0.0, 0.0, 1.0,
                                   0.0, 0.0, 1.0, 1.0 ];
let region: MTLRegion = MTLRegionMake1D(0, textureDescriptor.width);
myTexture.replaceRegion(region, mipmapLevel: 0, withBytes: &manualTextureData, bytesPerRow: 0);

但是Metal无法在着色器中识别这些值(除了第一个值,它会得到一个空的纹理).

but Metal doesn't recognize those values in the shader (it gets an empty texture, except for the first value).

我很快意识到Float数组可能必须转换为字节数组(例如UInt8),但也找不到从[Float]转换为[UInt8]的方法.

I quickly realized that the Float array probably has to be converted into a bytes array (e.g UInt8), but couldn't find a way to convert from [Float] to [UInt8] either.

我考虑的另一个可能的选择是使用CVPixelBuffer对象,但这也感觉像是解决该问题的方法.

Another possible option I consider is using a CVPixelBuffer object, but that also felt like a workaround to the problem.

那么解决这个问题的正确方法是什么?

So whats the right way to tackle that?

谢谢.

  • 请注意,我对 Objective-C 不熟悉,因此我不确定使用CVPixelBuffer/UIImage是否会被夸大了. li>
  • Please note I'm not familiar with Objective-C, hence I'm not sure whether using CVPixelBuffer / UIImage is exaggerated for something which should be straight-forward.

推荐答案

请原谅简短的答复,但您可能会发现查看我对Swift和Metal的实验很有用.我已经在Swift中创建了一个粒子系统,该系统作为粒子结构的一维数组传递到Metal计算着色器.通过使用posix_memalign,我可以消除在Metal和Swift之间传递数组引起的瓶颈.

Please forgive the terse reply, but you may find it useful to take a look at my experiments with Swift and Metal. I've created a particle system in Swift which is passed to a Metal compute shader as a one dimensional array of Particle structs. By using posix_memalign, I'm able to eliminate the bottleneck caused by passing the array between Metal and Swift.

我对此进行了广泛的博客发布: http://flexmonkey.blogspot.co. uk/search/label/Metal

I've blogged extensively about this: http://flexmonkey.blogspot.co.uk/search/label/Metal

我希望这会有所帮助.

I hope this helps.

西蒙(Simon)

这篇关于在金属中手动设置1D纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆