首页
学习
活动
专区
圈层
工具
发布

核函数
EN

Stack Overflow用户
提问于 2018-12-07 06:39:54
回答 1查看 81关注 0票数 1

我正在画正方形,围绕着我传递给阴影的点

代码语言:javascript
复制
   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)
        }
    }

The PointsArray

代码语言:javascript
复制
var pointsArray : [float2] = [float2(0.40,-0.5),float2(0.20,-0.5),float2(0.0,0.0),float2(0.56,-0.4)]

核函数

代码语言:javascript
复制
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分传给着色器,但它只在屏幕上画了两个点。它是核函数问题还是内核线程问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-07 11:20:19

代码语言:javascript
复制
computeCommandEncoder.setBytes(&pointsArray, length:MemoryLayout<float2>.stride, index: 0)

这里的长度需要乘以pointsArray的计数

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53664488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档