核函数

Kernel Function

我在传递给着色器的点周围绘制正方形

   override func computedraw(computeCommandEncoder: MTLComputeCommandEncoder) {

        computeCommandEncoder.setComputePipelineState(pipelineState)
        computeCommandEncoder.setTexture(self.texture, index: 0)

        if(pointsArray.count > 0){
            var count:Int = 4;
            computeCommandEncoder.setBytes(&pointsArray, length:MemoryLayout<float2>.stride, index: 0)
            computeCommandEncoder.setBytes(&count, length:MemoryLayout<Int>.stride, index: 1)
            let threadGroupCount = MTLSizeMake(2, 2, 1)
            let threadGroups = MTLSizeMake((self.texture?.width)! / threadGroupCount.width, (self.texture?.height)! / threadGroupCount.height, 1)
            computeCommandEncoder.dispatchThreadgroups(threadGroups, threadsPerThreadgroup: threadGroupCount)
        }
    }

PointsArray

var pointsArray : [float2] = [float2(0.40,-0.5),float2(0.20,-0.5),float2(0.0,0.0),float2(0.56,-0.4)]

在内核函数中

float2 touchPointF(float2 tap) {

    float deviceWidth =  2732;
    float deviceHeight = 2048;
    float2 touchPoint = (0,0);

    touchPoint.x = ((tap.x + 1) * deviceWidth) / 2;
    touchPoint.y = ((-1 * (tap.y - 1 )) * deviceHeight) / 2;

    return touchPoint;
}

kernel void computeTool(
                        constant float2 *point [[buffer(0)]],
                        constant int &pointCount [[buffer(1)]],
                        texture2d<float,access::read_write> des [[texture(0)]],
                       // texture2d<float,access::read> star [[texture(1)]],
                        uint2 gid [[thread_position_in_grid]])
{


    for (int i = 0; i < pointCount; ++i) {

        float2 x =    touchPointF(point[i])  ;


        if ((gid.x > (uint(x.x) - 40) && (gid.x < (uint(x.x) + 40) )) && (gid.y > (uint(x.y) -40) && gid.y < (uint(x.y) + 40)  )) {
            des.write(float4(float(pointCount)/10,0.0,0.0,1.0), gid);
        }
    }

}

我将 4 个点传递给着色器,但它只在屏幕上绘制了两个点。是内核函数问题还是内核线程问题

computeCommandEncoder.setBytes(&pointsArray, length:MemoryLayout<float2>.stride, index: 0)

这里的长度我需要乘以pointsArray的个数