Shaders.metal只获取鼠标的y值
Shaders.metal only get the y value of the mouse
我想用鼠标控制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
函数。我该如何解决这个问题?
NSPoint
的字段是 CGFloat
。我认为问题在于,对于 64 位,CGFloat
被定义为 Double
,而不是 Float
。金属的 float
对应于 Swift 的 Float
,而不是 Double
。据推测,float2
对应两个SwiftFloat
。您的缓冲区布局错误。将两个 Float
复制到 mouseBuffer
.
中,而不是 NSPoint
我想用鼠标控制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
函数。我该如何解决这个问题?
NSPoint
的字段是 CGFloat
。我认为问题在于,对于 64 位,CGFloat
被定义为 Double
,而不是 Float
。金属的 float
对应于 Swift 的 Float
,而不是 Double
。据推测,float2
对应两个SwiftFloat
。您的缓冲区布局错误。将两个 Float
复制到 mouseBuffer
.
NSPoint