简体   繁体   English

如何在iOS上使用Metal与GPU和CPU之间的共享内存? (理想情况下是目标c)

[英]How to use shared memory between GPU and CPU on iOS with Metal? (ideally with objective c)

I created a MTLTexture like this: 我创建了一个像这样的MTLTexture:

descTex=[MTLTextureDescriptor texture2DDescriptorWithPixelFormat:MTLPixelFormatRGBA8Unorm width:texCamWidth height:texCamHeight mipmapped:NO];
descTex.usage = MTLTextureUsageShaderWrite | MTLTextureUsageShaderRead ;
[descTex setStorageMode:MTLStorageModeShared];
texOutRGB = [_device newTextureWithDescriptor:descTex];

Used a compute shader to fill the texture and render it to the screen. 使用计算着色器填充纹理并将其渲染到屏幕。 Results are as expected. 结果如预期。 Now I need to do a CPU hook to modify the texture data which can not be done with a shader. 现在我需要做一个CPU挂钩来修改纹理数据,这是使用着色器无法完成的。 I expected that the MTLTexture.buffer contents would allow me to loop over the pixels but it appears it does not work like that. 我期望MTLTexture.buffer内容允许我循环遍历像素,但看起来它不像那样工作。 I see people using the getBytes and then replaceRegion to write it back but that does not look like using shared memory since a copy of the data is made. 我看到人们使用getBytes然后使用replaceRegion将其写回来,但是由于创建了数据副本,因此看起来不像使用共享内存。 How to loop over the RGBA pixel data in the texture with the CPU? 如何使用CPU循环纹理中的RGBA像素数据?

If you created a simple 1D buffer instead, then just access the contents member as a pointer. 如果您创建了一个简单的1D缓冲区,那么只需将内容成员作为指针访问。 If you need a RGBA buffer then create a CVPixelBuffer that contains BGRA pixels, you can then access the pixels by locking and then read/write to the base pointer for the buffer (take care to respect row widths), finally you can wrap the CVPixelBuffer as a metal texture to avoid the memcpy(). 如果你需要一个RGBA缓冲区,然后创建一个包含BGRA像素的CVPixelBuffer,你可以通过锁定访问像素,然后读/写缓冲区的基指针(注意尊重行宽),最后你可以包装CVPixelBuffer作为金属纹理,以避免memcpy()。 The 2D processing is not trivial, it is a lot easier to just use a 1D buffer. 2D处理并不简单,只需使用1D缓冲区就容易多了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM