金属着色器3D纹理查找

时间:2018-05-01 11:20:47

标签: ios fragment-shader texture-mapping metal metalkit

我在iOS上的LUT文件中创建了一个3D纹理,如下所示:

    let dim = 33
    let textureDescriptor = MTLTextureDescriptor()
    textureDescriptor.textureType = .type3D
    textureDescriptor.pixelFormat = .rgba32Float
    textureDescriptor.width = dim
    textureDescriptor.height = dim
    textureDescriptor.depth = dim
    textureDescriptor.usage = .shaderRead

    let texture = device.makeTexture(descriptor: textureDescriptor)

    texture!.replace(region: MTLRegionMake3D(0, 0, 0, dim, dim, dim),
                    mipmapLevel:0,
                    slice:0,
                    withBytes:values!,
                    bytesPerRow:dim * MemoryLayout<Float>.size * 4,
                    bytesPerImage:dim * dim * MemoryLayout<Float>.size * 4)

然后我尝试在片段着色器中使用此LUT,如下所示:

 fragment half4 fragmentShader ( MappedVertex in [[ stage_in ]],
                              texture2d<float, access::sample> inputTexture [[ texture(0) ]],
                              texture3d<float, access::sample> lutTexture [[ texture(1) ]]
                              )
{
   constexpr sampler s(s_address::clamp_to_edge, t_address::clamp_to_edge, min_filter::linear, mag_filter::linear);
   float3 rgb = inputTexture.sample(s, in.textureCoordinate).rgb;

   float3 lookupColor = lutTexture.sample(s, rgb).rgb;
   return half4(half3(lookupColor), 1.h);
}

我担心我没有得到正确的结果。代码中的一切都完美吗?我正确地对3d纹理进行采样吗?

0 个答案:

没有答案