1

I created a 3D texture from a LUT file on iOS as follows:

    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)

and then I try to use this LUT in fragment shader as follows:

 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);
}

I am afraid that I'm not getting the correct results. Is everything in the code perfect? Am I sampling the 3d texture correctly?

5
  • It's hard to answer this because you're leaving a lot of information out. Where does values come from and how do you know its format? If there's any padding/alignment, then you may have the bytes-per-row and bytes-per-image wrong. Why are you "afraid that [you're] not getting the correct results"? What were you expecting? What did you actually get? Commented May 1, 2018 at 14:29
  • values holds a sequence of rgba values and is 33*33*33*4*MemoryLayout<Float>.size bytes in size. It is a LUT table. My doubt is whether shader code that calculates lookupColor is correct or not. Commented May 1, 2018 at 16:00
  • Having done similar things in the past, this looks correct to me. Again, what are you expecting, and exactly what results are you getting?
    – warrenm
    Commented May 2, 2018 at 3:56
  • I believe then my LUT is not perfect or there are some interpolation issues with the sampler. Commented May 2, 2018 at 9:04
  • hi, I face the same problem with you, did you find any solution to fix interpolation issues?
    – krosshj
    Commented May 6, 2021 at 10:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.