Shaders.metal只获取鼠标的{y值

时间:2016-09-05 11:31:04

标签: macos metal compute-shader

我想用鼠标来控制compute function计算出的相机位置。在我的view代码中:

import MetalKit

public class MetalView: MTKView, NSWindowDelegate {

    var queue: MTLCommandQueue! = nil
    var cps: MTLComputePipelineState! = nil

    var timer: Float = 0
    var timerBuffer: MTLBuffer!

    var mouseBuffer: MTLBuffer!
    var pos: NSPoint!

    required public init(coder: NSCoder) {
        super.init(coder: coder)
        self.framebufferOnly = false
        device = MTLCreateSystemDefaultDevice()
        registerShaders()
    }


    override public func drawRect(dirtyRect: NSRect) {
        super.drawRect(dirtyRect)
        if let drawable = currentDrawable {
            let command_buffer = queue.commandBuffer()
            let command_encoder = command_buffer.computeCommandEncoder()
            command_encoder.setComputePipelineState(cps)
            command_encoder.setTexture(drawable.texture, atIndex: 0)
            command_encoder.setBuffer(timerBuffer, offset: 0, atIndex: 1)
            command_encoder.setBuffer(mouseBuffer, offset: 0, atIndex: 2)
            update()
            let threadGroupCount = MTLSizeMake(8, 8, 1)
            let threadGroups = MTLSizeMake(drawable.texture.width / threadGroupCount.width, drawable.texture.height / threadGroupCount.height, 1)
            command_encoder.dispatchThreadgroups(threadGroups, threadsPerThreadgroup: threadGroupCount)
            command_encoder.endEncoding()
            command_buffer.presentDrawable(drawable)
            command_buffer.commit()
        }
    }

    func registerShaders() {
        queue = device!.newCommandQueue()
        do {
            let library = device!.newDefaultLibrary()!
            let kernel = library.newFunctionWithName("compute")!
            timerBuffer = device!.newBufferWithLength(sizeof(Float), options: [])
            mouseBuffer = device!.newBufferWithLength(sizeof(NSPoint), options: [])
            cps = try device!.newComputePipelineStateWithFunction(kernel)
        } catch let e {
            Swift.print("\(e)")
        }
    }

    func update() {
        timer += 0.01
        var bufferPointer = timerBuffer.contents()
        memcpy(bufferPointer, &timer, sizeof(Float))
        bufferPointer = mouseBuffer.contents()
        memcpy(bufferPointer, &pos, sizeof(NSPoint))
    }

    override public func mouseDragged(event: NSEvent) {
        pos = convertPointToLayer(convertPoint(event.locationInWindow, fromView: nil))
        let scale = layer!.contentsScale
        pos.x *= scale
        pos.y *= scale
        debugPrint("Hello",pos.x,pos.y)
    }
}

和我的着色器代码:

kernel void compute(texture2d<float, access::write> output [[texture(0)]],
                    constant float &timer [[buffer(1)]],
                    constant float2 &mouse [[buffer(2)]],
                    uint2 gid [[thread_position_in_grid]])
{
    int width = output.get_width();
    int height = output.get_height();
    float2 uv = float2(gid) / float2(width, height);
    uv = uv * 2.0 - 1.0;
    // scale proportionately.
    if(width > height) uv.x *= float(width)/float(height);
    if(width < height) uv.y *= float(height)/float(width);


    float2 mpos = mouse * 2.0 - 1.0;

    float3 cameraPosition = float3( mpos.x,mpos.y, -10.0 );///<-- mouse position to set camera position

...

}

但不知怎的,我只获得mpos.y值。好像mpos.x似乎没有发送到compute函数。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

CGFloat的字段为CGFloat。我认为问题在于,对于64位,Double定义为Float,而不是float。金属Float对应Swift的Double,而不是float2。据推测,Float对应两个Swift Float。您的缓冲区布局错误。将两个NSPoint而不是mouseBuffer复制到<div ng-click="show2=true; show1=false" ng-show="show1"> <center> <i class="ion-arrow-down-b flecheCacherTacheEffectuee"></i> <span class="titreTacheEffectue">TEST 1</span> </center> </div> <div ng-show="show2" ng-click="show1= true; show2=false"> <center> <i class="ion-arrow-up-b flecheCacherTacheEffectuee"></i> <span class="titreTacheEffectue">TEST 2</span> </center> </div>