首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于馈送Low技术的sampler2D输入的低成本图像到NSData的转换

用于馈送Low技术的sampler2D输入的低成本图像到NSData的转换
EN

Stack Overflow用户
提问于 2018-06-25 23:13:36
回答 1查看 123关注 0票数 1

Sometimes将宝贵的数据从CPU传递到GPU的唯一方法是将其隐藏在纹理中。

我试图欺骗SCNTechnique并简单地传递[NSData dataWithBytes:length:]或包含我精心准备的原始像素数据字节的CGDataProviderRef,但SceneKit足够聪明,能够检测到我的险恶尝试。

但我没有放弃,并找到了一个漏洞:

代码语言:javascript
复制
  [_sceneView.technique setValue: UIImagePNGRepresentation(encodeInSinglePixelUIImage(pos.x, pos.y)) forKey:@"blob_pos_"];

在移动设备上以60fps编码和解码单像素PNG是你可以负担得起的,在iPhone X上它只需要2毫秒,并且让你的手掌温暖一点。然而,我在11月之前不需要任何发热功能,所以我想知道是否有一个很酷的替代方法。

EN

回答 1

Stack Overflow用户

发布于 2018-06-26 21:38:16

我发现最有效的方法是构造浮点RGB TIFF。它仍然不是很快,在iPhone X上消耗0.7ms,但比PNG方法快得多。

具有浮点纹理还具有直接浮点传输的优点,即无需在中央处理器上对多个uint8 RGBA值进行编码,并在GPU上重建浮点。

下面是操作步骤:

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51026751

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档