我具有金属内核功能。通常,您访问像这样的像素:
kernel void edgeDetect(texture2d<half, access::sample> inTexture [[ texture(0) ]],
texture2d<half, access::write> outTexture [[ texture(1) ]],
device const uint *roi [[ buffer(0) ]],
uint2 grid [[ thread_position_in_grid ]]) {
if (grid.x >= outTexture.get_width() || grid.y >= outTexture.get_height()) {
return;
}
half c[9];
for (int i=0; i < 3; ++i) {
for (int j=0; j < 3; ++j) {
c[3*i+j] = inTexture.read(grid + uint2(i-1,j-1)).x;
}
}
half3 Lx = 2.0*(c[7]-c[1]) + c[6] + c[8] - c[2] - c[0];
half3 Ly = 2.0*(c[3]-c[5]) + c[6] + c[0] - c[2] - c[8];
half3 G = sqrt(Lx*Lx+Ly*Ly);
outTexture.write(half4(G, 0.0), grid);
}
现在,我需要像这样访问当前网格位置附近的像素:
half4 inColor = inTexture.read(grid - uint2(-1,-1));
基本上这是可行的,但是在线程边界上,如该图所示(砖墙图案),我有“间断性”。
这很清楚,因为每个线程仅传递其子纹理进行处理。因此,超出线程边界,我无法访问像素。
我的问题是:当我需要对计算内核中当前位置以外的像素进行寻址时,这是什么概念?使用计算内核完全可以吗?
我发现了问题: