Write a Metal compute kernel to Gaussian blur an image.
- User
Write a Metal compute kernel to Gaussian blur an image.
Assistant
Here is a Metal compute kernel to perform a Gaussian blur on a 2D image:
#include <metal_stdlib> using namespace metal; kernel void gaussianBlur(texture2d<float, access::read> inTexture [[texture(0)]], texture2d<float, access::write> outTexture [[texture(1)]], uint2 gid [[thread_position_in_grid]]) { const int kernelSize = 5; const float sigma = 2.0; const float weights[kernelSize] = {0.06136, 0.24477, 0.38774, 0.24477, 0.06136}; float3 blurColor = float3(0.0); for (int i = -kernelSize/2; i <= kernelSize/2; i++) { for (int j = -kernelSize/2; j <= kernelSize/2; j++) { int2 samplePos = gid + int2(i, j); float weight = weights[i + kernelSize/2] * weights[j + kernelSize/2]; blurColor += inTexture.read(samplePos).rgb * weight; } } outTexture.write(float4(blurColor, 1.0), gid); }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.