1
votes

Im trying to setup some monocolor textures. I want to use a texture composed of a single 32bit float. Then use a linear sampler to interpolate values from it. I have setup my metal texture descriptor as follows.

MTLTextureDescriptor *textureDescriptor = [[MTLTextureDescriptor alloc] init];
textureDescriptor.textureType = MTLTextureType2D;
textureDescriptor.pixelFormat = MTLPixelFormatR32Float;
textureDescriptor.width = width;
textureDescriptor.height = height;
textureDescriptor.depth = 1;
textureDescriptor.mipmapLevelCount = 1;
textureDescriptor.sampleCount = 1;
textureDescriptor.arrayLength = 1;
textureDescriptor.cpuCacheMode = MTLCPUCacheModeWriteCombined;
textureDescriptor.storageMode = MTLStorageModeManaged;
textureDescriptor.allowGPUOptimizedContents = true;
textureDescriptor.usage = MTLTextureUsageShaderRead;

However I'm getting a compiling error in my shader code.

kernel text_kernel(texture2d<float, access::sample> x [[texture(0)]])
{
    constexpr sampler linear(coord::normalized,
                             address::clamp_to_edge,
                             filter::linear);
    float x_sample = x.sample(linear, float2(0.1, 0.2));
}

It appears that sample wants to return a float4 instead of a single float. So this raises a number of questions.

1 What's being returned in the other three components?

Is it returning the neighboring pixels or just putting all the same value in the other components.

2 Is sample normalizing the pixel values?

If I initialize the texture with float values greater than 1 or less than zero is it clamping the sampled values?

1

1 Answers

3
votes

1 What's being returned in the other three components?

I believe it fills the missing components from the corresponding component of float4(0, 0, 0, 1).

If you're only interested in the red component, just apply .r to the result from sample(). I.e.:

    float x_sample = x.sample(linear, float2(0.1, 0.2)).r;

2 Is sample normalizing the pixel values?

No. For floating-point pixel formats, it doesn't clamp.