3
メタルカーネルのランダムな浮動小数点数を持つバッファの平均値を計算する適切な方法は誰もが知っていますか?メタルカーネルの平均値の計算
threadsPerGroup = MTLSizeMake(1, 1, inputTexture.arrayLength);
numThreadGroups = MTLSizeMake(1, 1, inputTexture.arrayLength/threadsPerGroup.depth);
[commandEncoder dispatchThreadgroups:numThreadGroups
threadsPerThreadgroup:threadsPerGroup];
カーネルコード:
kernel void mean(texture2d_array<float, access::read> inTex [[ texture(0) ]],
device float *means [[ buffer(1) ]],
uint3 id [[ thread_position_in_grid ]]) {
if (id.x == 0 && id.y == 0) {
float mean = 0.0;
for (uint i = 0; i < inTex.get_width(); ++i) {
for (uint j = 0; j < inTex.get_height(); ++j) {
mean += inTex.read(uint2(i, j), id.z)[0];
}
}
float textureArea = inTex.get_width() * inTex.get_height();
mean /= textureArea;
out[id.z] = mean;
}
}
バッファがR32Floatピクセルフォーマットとtexture2d_arrayタイプのテクスチャーで表される計算コマンドエンコーダに作業をディスパッチ
。
フロート値は〜1E-15〜〜1E8まで変化し、負の値もあります。私はそれらをintまたはuintに許容可能な精度でキャストできません。 –