Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos Shaders.metal仅获取鼠标的y值_Macos_Metal_Compute Shader - Fatal编程技术网

Macos Shaders.metal仅获取鼠标的y值

Macos Shaders.metal仅获取鼠标的y值,macos,metal,compute-shader,Macos,Metal,Compute Shader,我想用鼠标控制由计算功能计算的摄像机位置。在我的视图中代码: import MetalKit public class MetalView: MTKView, NSWindowDelegate { var queue: MTLCommandQueue! = nil var cps: MTLComputePipelineState! = nil var timer: Float = 0 var timerBuffer: MTLBuffer! var

我想用鼠标控制由
计算功能计算的摄像机位置。在我的
视图中
代码:

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

...

}
kernelvoid计算(texture2d输出[[texture(0)],
恒定浮动和定时器[[缓冲器(1)]],
常量浮动2和鼠标[[缓冲区(2)],
uint2 gid[[螺纹位置在网格中]]
{
int width=output.get_width();
int height=output.get_height();
浮动2 uv=浮动2(gid)/浮动2(宽度、高度);
紫外线=紫外线*2.0-1.0;
//按比例缩放。
如果(宽度>高度)uv.x*=浮动(宽度)/浮动(高度);
如果(宽度<高度)uv.y*=浮动(高度)/浮动(宽度);
float2 mpos=鼠标*2.0-1.0;

float3 cameraPosition=float3(mpos.x,mpos.y,-10.0);//
NSPoint
的字段是
CGFloat
。我认为问题在于,对于64位,
CGFloat
被定义为
Double
,而不是
Float
。金属的
Float
对应于Swift的
Float
,而不是
Double
。大概,
float2
对应于两个Swift
Float
s。缓冲区布局错误。将两个
Float
s,而不是
NSPoint
,复制到
mouseBuffer

中,你能提供更多证据证明着色器中没有mpos.x的值吗?我在
float3 cameraPosition=float3(mpos.x,mpos.y,-10.0);
,比如说
float3(mpos.x,0,-10.0)
float3(0.0,mpos.x,-10.0)
只是想看看
mpos.x
是否工作,就我而言,它不工作。我不知道如何将
nspoint
转换为
float2
。因此,我使用了2
float
s并将其放入2
鼠标缓冲区(x和y)。然后它就工作了。