Sometimes将宝贵的数据从CPU传递到GPU的唯一方法是将其隐藏在纹理中。
我试图欺骗SCNTechnique并简单地传递[NSData dataWithBytes:length:]或包含我精心准备的原始像素数据字节的CGDataProviderRef,但SceneKit足够聪明,能够检测到我的险恶尝试。
但我没有放弃,并找到了一个漏洞:
[_sceneView.technique setValue: UIImagePNGRepresentation(encodeInSinglePixelUIImage(pos.x, pos.y)) forKey:@"blob_pos_"];在移动设备上以60fps编码和解码单像素PNG是你可以负担得起的,在iPhone X上它只需要2毫秒,并且让你的手掌温暖一点。然而,我在11月之前不需要任何发热功能,所以我想知道是否有一个很酷的替代方法。
发布于 2018-06-26 21:38:16
我发现最有效的方法是构造浮点RGB TIFF。它仍然不是很快,在iPhone X上消耗0.7ms,但比PNG方法快得多。
具有浮点纹理还具有直接浮点传输的优点,即无需在中央处理器上对多个uint8 RGBA值进行编码,并在GPU上重建浮点。
下面是操作步骤:
NSData * tiffencode(float x, float y)
{
const uint8_t tags = 9;
const uint8_t headerlen = 8+2+tags*12+4;
const uint8_t width = 1;
const uint8_t height = 1;
const uint8_t datalen = width*height*3*4;
static uint8_t tiff[headerlen+datalen] = {
'I', 'I', 0x2a, 0, //little endian/'I'ntel
8, 0, 0, 0, //index of metadata
tags, 0,
0x00, 1, 4, 0, 1, 0, 0, 0, width, 0, 0, 0, //width
0x01, 1, 4, 0, 1, 0, 0, 0, height, 0, 0, 0, //height
0x02, 1, 3, 0, 1, 0, 0, 0, 32, 0, 0, 0, //bits per sample(s)
0x06, 1, 3, 0, 1, 0, 0, 0, 2, 0, 0, 0, //photometric interpretation: RGB
0x11, 1, 4, 0, 1, 0, 0, 0, headerlen, 0, 0, 0,//strip offset
0x15, 1, 3, 0, 1, 0, 0, 0, 3, 0, 0, 0, //samples per pixel: 3
0x16, 1, 4, 0, 1, 0, 0, 0, height, 0, 0, 0, //rows per strip: height
0x17, 1, 4, 0, 1, 0, 0, 0, datalen, 0, 0, 0, //strip byte length
0x53, 1, 3, 0, 1, 0, 0, 0, 3, 0, 0, 0, //sampleformat: float
0, 0, 0, 0, //end of metadata
//RGBRGB.. pixeldata here
};
float *rawData = tiff+headerlen;
rawData[0] = x;
rawData[1] = y;
NSData *data = [NSData dataWithBytes:&tiff length:sizeof(tiff)];
return data;
}我使用的有用的TIFF链接:
http://www.fileformat.info/format/tiff/corion.htm
http://paulbourke.net/dataformats/tiff/
https://www.fileformat.info/format/tiff/egff.htm
https://www.awaresystems.be/imaging/tiff/tifftags/sampleformat.html
https://stackoverflow.com/questions/51026751
复制相似问题