该MVP Metal main.swift显示为空白

时间:2019-03-17 07:17:47

标签: swift macos metal

因此,在项目的private void btn_Insert_Click(object sender, EventArgs e) { if (txt_StudentFirstName.Text != "" && txt_StudentMobileNo.Text != "" && cmb_Teacher.Text != "" && img_Box.Image != null) { byte[] img = null; FileStream fs = new FileStream(imgLoc, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); img = br.ReadBytes((int)fs.Length); cmd = new OleDbCommand("INSERT INTO Student (FirstName, LastName, FatherName, FatherID, DOB, DOR, Class, Course, Gender, Teacher, Address, EmailID, MobileNo, TotalFee, FeePaid, Balance, Image) VALUES (@FirstName, @LastName, @FatherName, @FatherID, @DOB, @DOR, @Class, @Course, @Gender, @Teacher, @Address, @EmailID, @MobileNo, @TotalFee, @FeePaid, @Balance, @Img)", con); con.Open(); cmd.Parameters.AddWithValue("@FirstName", txt_StudentFirstName.Text); cmd.Parameters.AddWithValue("@LastName", txt_StudentLastName.Text); cmd.Parameters.AddWithValue("@FatherName", txt_StudentFatherName.Text); cmd.Parameters.AddWithValue("@FatherID", txt_StudentFatherID.Text); cmd.Parameters.AddWithValue("@DOB", Student_DOB.Text); cmd.Parameters.AddWithValue("@DOR", Student_DOR.Text); cmd.Parameters.AddWithValue("@Class", cmb_Class.Text); cmd.Parameters.AddWithValue("@Course", cmb_Course.Text); cmd.Parameters.AddWithValue("@Gender", cmb_Gender.Text); cmd.Parameters.AddWithValue("@Teacher", cmb_Teacher.Text); cmd.Parameters.AddWithValue("@Address", txt_StudentAddress.Text); cmd.Parameters.AddWithValue("@EmailID", txt_StudentEmailID.Text); cmd.Parameters.AddWithValue("@MobileNo", txt_StudentMobileNo.Text); cmd.Parameters.AddWithValue("@TotalFee", cmb_CourseFee.Text); cmd.Parameters.AddWithValue("@FeePaid", txt_FeePaid.Text); cmd.Parameters.AddWithValue("@Balance", txt_Balance.Text); // byte[] yourPhoto = (); cmd.Parameters.AddWithValue("@Img", img); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("Record inserted successfully"); DisplayData(); ClearData(); } else { MessageBox.Show("Please provide complete details with image !"); } } 文件中,您可以像这样创建一个窗口(然后从那里开始):

main.swift

我想知道如何用金属三角形做同样的事情。我一直在浏览github.com/topics/metalkit,但到目前为止,我所发现的最接近的东西不在gist中。

let nsapp = NSApplication.shared
let window = NSWindow(
  contentRect: NSMakeRect(0, 0, 200, 200),
  styleMask: .fullSizeContentView,
  backing: NSWindow.BackingStoreType.buffered,
  defer: false
)
window.cascadeTopLeft(from:NSMakePoint(20,20))
nsapp.run()

它至少从头开始构建import Cocoa import MetalKit @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate, MTKViewDelegate { weak var window: NSWindow! weak var metalView: MTKView! let device = MTLCreateSystemDefaultDevice()! var commandQueue: MTLCommandQueue! var pipelineState: MTLRenderPipelineState! func applicationDidFinishLaunching(_ aNotification: Notification) { metalView = MTKView(frame: NSRect(origin: CGPoint.zero, size: window.frame.size), device: device) metalView.delegate = self window.contentView = metalView commandQueue = device.makeCommandQueue() let shaders = """ #include <metal_stdlib> using namespace metal; struct VertexIn { packed_float3 position; packed_float3 color; }; struct VertexOut { float4 position [[position]]; float4 color; }; vertex VertexOut vertex_main(device const VertexIn *vertices [[buffer(0)]], uint vertexId [[vertex_id]]) { VertexOut out; out.position = float4(vertices[vertexId].position, 1); out.color = float4(vertices[vertexId].color, 1); return out; } fragment float4 fragment_main(VertexOut in [[stage_in]]) { return in.color; } """ do { let library = try device.makeLibrary(source: shaders, options: nil) let pipelineDescriptor = MTLRenderPipelineDescriptor() pipelineDescriptor.colorAttachments[0].pixelFormat = metalView.colorPixelFormat pipelineDescriptor.vertexFunction = library.makeFunction(name: "vertex_main") pipelineDescriptor.fragmentFunction = library.makeFunction(name: "fragment_main") pipelineState = try device.makeRenderPipelineState(descriptor: pipelineDescriptor) } catch {} } func mtkView(_ view: MTKView, drawableSizeWillChange size: CGSize) { } func draw(in view: MTKView) { guard let commandBuffer = commandQueue.makeCommandBuffer() else { return } guard let passDescriptor = view.currentRenderPassDescriptor else { return } guard let encoder = commandBuffer.makeRenderCommandEncoder(descriptor: passDescriptor) else { return } let vertexData: [Float] = [ -0.5, -0.5, 0, 1, 0, 0, 0.5, -0.5, 0, 0, 1, 0, 0, 0.5, 0, 0, 0, 1 ] encoder.setVertexBytes(vertexData, length: vertexData.count * MemoryLayout<Float>.stride, index: 0) encoder.setRenderPipelineState(pipelineState) encoder.drawPrimitives(type: .triangle, vertexStart: 0, vertexCount: 3) encoder.endEncoding() commandBuffer.present(view.currentDrawable!) commandBuffer.commit() } } 。但是,我不确定没有任何控制器,代表,应用程序的情况下,使金属事物正常工作的最低可行产品是什么,我将开始进行反复试验以使其正常工作,但是可能要花几天时间,并认为如果有人已经知道了这一点,对其他人可能会有帮助。

我将两者结合了起来,但我无法说清。

MTKView

对我来说是空白。我也尝试遵循this

This越来越近了。

1 个答案:

答案 0 :(得分:1)

这里的主要问题是NSApplication的{​​{1}}方法在应用终止之前不会返回,因此您的渲染命令编码永远不会发生。您可以继承run的子类并覆盖其MTKView的方法来绘制图形:

draw