From 521e5a1636cfe68e4260ab83ce7274d9c75f38d1 Mon Sep 17 00:00:00 2001 From: Luigi Thirty Date: Sun, 30 Jul 2017 16:22:03 -0400 Subject: [PATCH] OpenGL --- FruitMachine.xcodeproj/project.pbxproj | 8 +-- FruitMachine/AppleI/AppleGLDisplay.swift | 83 +++++++++++++++++++++++ FruitMachine/AppleI/AppleI.swift | 10 ++- FruitMachine/AppleI/AppleIMetalView.swift | 24 ------- FruitMachine/MainViewController.swift | 6 +- 5 files changed, 101 insertions(+), 30 deletions(-) create mode 100644 FruitMachine/AppleI/AppleGLDisplay.swift delete mode 100644 FruitMachine/AppleI/AppleIMetalView.swift diff --git a/FruitMachine.xcodeproj/project.pbxproj b/FruitMachine.xcodeproj/project.pbxproj index 4f1f0ed..6ae92c3 100644 --- a/FruitMachine.xcodeproj/project.pbxproj +++ b/FruitMachine.xcodeproj/project.pbxproj @@ -13,10 +13,10 @@ 2A5BC51C1F29A2EB008C03BE /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2A5BC51B1F29A2EB008C03BE /* QuartzCore.framework */; }; 2A5BC51E1F29A4C3008C03BE /* AppleIBitmapDisplay.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A5BC51D1F29A4C3008C03BE /* AppleIBitmapDisplay.swift */; }; 2A60851E1F2AFAE900E05B64 /* PIA.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A60851D1F2AFAE900E05B64 /* PIA.swift */; }; + 2A6D3BF21F2E6222001CBEFB /* AppleGLDisplay.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2A6D3BF11F2E6222001CBEFB /* AppleGLDisplay.swift */; }; 2AA8B5F81F2A8889002B350F /* AppleI.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AA8B5F71F2A8889002B350F /* AppleI.swift */; }; 2AA8B5FC1F2A8EAD002B350F /* Terminal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AA8B5FB1F2A8EAD002B350F /* Terminal.swift */; }; 2AA8B5FE1F2A942C002B350F /* PIAOverrides.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AA8B5FD1F2A942C002B350F /* PIAOverrides.swift */; }; - 2AB815E71F2DA25E000BCE7D /* AppleIMetalView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AB815E61F2DA25E000BCE7D /* AppleIMetalView.swift */; }; 2AD458CE1F205EB700F05121 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AD458CD1F205EB700F05121 /* AppDelegate.swift */; }; 2AD458D01F205EB700F05121 /* DebuggerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2AD458CF1F205EB700F05121 /* DebuggerViewController.swift */; }; 2AD458D21F205EB700F05121 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 2AD458D11F205EB700F05121 /* Assets.xcassets */; }; @@ -43,10 +43,10 @@ 2A5BC51B1F29A2EB008C03BE /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; }; 2A5BC51D1F29A4C3008C03BE /* AppleIBitmapDisplay.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppleIBitmapDisplay.swift; sourceTree = ""; }; 2A60851D1F2AFAE900E05B64 /* PIA.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PIA.swift; sourceTree = ""; }; + 2A6D3BF11F2E6222001CBEFB /* AppleGLDisplay.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppleGLDisplay.swift; sourceTree = ""; }; 2AA8B5F71F2A8889002B350F /* AppleI.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppleI.swift; sourceTree = ""; }; 2AA8B5FB1F2A8EAD002B350F /* Terminal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Terminal.swift; sourceTree = ""; }; 2AA8B5FD1F2A942C002B350F /* PIAOverrides.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PIAOverrides.swift; sourceTree = ""; }; - 2AB815E61F2DA25E000BCE7D /* AppleIMetalView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppleIMetalView.swift; sourceTree = ""; }; 2AD458CA1F205EB700F05121 /* FruitMachine.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = FruitMachine.app; sourceTree = BUILT_PRODUCTS_DIR; }; 2AD458CD1F205EB700F05121 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 2AD458CF1F205EB700F05121 /* DebuggerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DebuggerViewController.swift; sourceTree = ""; }; @@ -96,8 +96,8 @@ 2A5BC5181F29A28D008C03BE /* AppleScreenView.swift */, 2A5BC51D1F29A4C3008C03BE /* AppleIBitmapDisplay.swift */, 2AA8B5F71F2A8889002B350F /* AppleI.swift */, - 2AB815E61F2DA25E000BCE7D /* AppleIMetalView.swift */, 2A60851D1F2AFAE900E05B64 /* PIA.swift */, + 2A6D3BF11F2E6222001CBEFB /* AppleGLDisplay.swift */, ); path = AppleI; sourceTree = ""; @@ -278,7 +278,6 @@ 2AE42E431F28665300C4900E /* CharacterGenerator.swift in Sources */, 2AE42E0C1F28522D00C4900E /* MemoryOverride.swift in Sources */, 2A22EBFB1F21A7A700A36A61 /* IntegerExtensions.swift in Sources */, - 2AB815E71F2DA25E000BCE7D /* AppleIMetalView.swift in Sources */, 2AD6D5841F26E6BF008F3CF5 /* DebuggerCommands.swift in Sources */, 2AE5BA041F23DE4400FAA343 /* Disassembly.swift in Sources */, 2AE42E3A1F28628300C4900E /* MainViewController.swift in Sources */, @@ -288,6 +287,7 @@ 2AE5BA061F2469EB00FAA343 /* AddressConversions.swift in Sources */, 2AE42E0A1F28521E00C4900E /* WriteOverride.swift in Sources */, 2A5BC5191F29A28D008C03BE /* AppleScreenView.swift in Sources */, + 2A6D3BF21F2E6222001CBEFB /* AppleGLDisplay.swift in Sources */, 2AD458E11F2064CB00F05121 /* MemoryInterface.swift in Sources */, 2AA8B5F81F2A8889002B350F /* AppleI.swift in Sources */, 2AD458DF1F205F4500F05121 /* CPU.swift in Sources */, diff --git a/FruitMachine/AppleI/AppleGLDisplay.swift b/FruitMachine/AppleI/AppleGLDisplay.swift new file mode 100644 index 0000000..35e2b94 --- /dev/null +++ b/FruitMachine/AppleI/AppleGLDisplay.swift @@ -0,0 +1,83 @@ +// +// AppleGLDisplay.swift +// FruitMachine +// +// Created by Christopher Rohl on 7/30/17. +// Copyright © 2017 Christopher Rohl. All rights reserved. +// + +import Cocoa +import OpenGL +import GLKit + +class AppleGLDisplay: NSOpenGLView { + + var displayLink: CVDisplayLink? + + override func draw(_ dirtyRect: NSRect) { + super.draw(dirtyRect) + + // Drawing code here. + renderFrame() + + } + + func doSetup() { + let attr = [ + NSOpenGLPixelFormatAttribute(NSOpenGLPFAOpenGLProfile), + NSOpenGLPixelFormatAttribute(NSOpenGLProfileVersion3_2Core), + NSOpenGLPixelFormatAttribute(NSOpenGLPFAColorSize), 24, + NSOpenGLPixelFormatAttribute(NSOpenGLPFAAlphaSize), 8, + NSOpenGLPixelFormatAttribute(NSOpenGLPFADoubleBuffer), + NSOpenGLPixelFormatAttribute(NSOpenGLPFADepthSize), 32, + 0 + ] + + self.pixelFormat = NSOpenGLPixelFormat(attributes: attr) + self.openGLContext = NSOpenGLContext(format: pixelFormat!, share: nil) + } + + override func prepareOpenGL() { + + func displayLinkOutputCallback(displayLink: CVDisplayLink, _ now: UnsafePointer, _ outputTime: UnsafePointer, _ flagsIn: CVOptionFlags, _ flagsOut: UnsafeMutablePointer, _ displayLinkContext: UnsafeMutableRawPointer?) -> CVReturn { + unsafeBitCast(displayLinkContext, to: AppleGLDisplay.self).renderFrame() + return kCVReturnSuccess + } + + CVDisplayLinkCreateWithActiveCGDisplays(&displayLink) + CVDisplayLinkSetOutputCallback(displayLink!, displayLinkOutputCallback, UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque())) + CVDisplayLinkStart(displayLink!) + } + + func renderFrame() { + CGLLockContext((self.openGLContext?.cglContextObj!)!) + CGLSetCurrentContext((self.openGLContext?.cglContextObj!)!) + + // Draw something... + glClearColor(0.0, 0.0, 0.0, 0.0); + glClear(GLbitfield(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)); + + let vertices = [GLfloat]([-1, -1, 0, + -1, 1, 0, + 1, 1, 0, + 1, -1, 0]) + + let indices = [GLubyte]([0, 1, 2, + 0, 2, 3]) + + glEnableVertexAttribArray(0) + + let ptr = UnsafePointer(vertices) + glVertexAttribPointer(0, 3, GLenum(GL_FLOAT), GLboolean(GL_FALSE), GLsizei(MemoryLayout.size), ptr) + + glDrawElements(GLenum(GL_TRIANGLES), 6, GLenum(GL_UNSIGNED_BYTE), indices) + + CGLFlushDrawable((self.openGLContext?.cglContextObj!)!) + CGLUnlockContext((self.openGLContext?.cglContextObj!)!) + } + + deinit { + CVDisplayLinkStop(displayLink!) + } + +} diff --git a/FruitMachine/AppleI/AppleI.swift b/FruitMachine/AppleI/AppleI.swift index 42f034a..fe87fa9 100644 --- a/FruitMachine/AppleI/AppleI.swift +++ b/FruitMachine/AppleI/AppleI.swift @@ -25,7 +25,9 @@ class AppleI: NSObject { let emulatorViewDelegate = AppleIBitmapDisplay() let emulatorView = AppleScreenView(frame: NSMakeRect(0, 0, 640, 384)) let emuScreenLayer = CALayer() - let emuMetalLayer = CAMetalLayer() + + //OpenGL + let emuGLView = AppleGLDisplay() static let CPU_FREQUENCY = 1000000 static let FRAMES_PER_SECOND = 60 @@ -43,6 +45,9 @@ class AppleI: NSObject { emuScreenLayer.setNeedsDisplay() emulatorView.layer?.addSublayer(emuScreenLayer) + //GL + emuGLView.frame = emulatorView.bounds + installOverrides() for (cellNum, character) in terminal.characters.enumerated() { @@ -78,5 +83,8 @@ class AppleI: NSObject { emulatorView.setNeedsDisplay(emulatorView.frame) emulatorView.display() + + //emuGLView.setNeedsDisplay(emuGLView.frame) + //emuGLView.display() } } diff --git a/FruitMachine/AppleI/AppleIMetalView.swift b/FruitMachine/AppleI/AppleIMetalView.swift deleted file mode 100644 index 95aa91b..0000000 --- a/FruitMachine/AppleI/AppleIMetalView.swift +++ /dev/null @@ -1,24 +0,0 @@ -// -// AppleIMetalView.swift -// FruitMachine -// -// Created by Christopher Rohl on 7/30/17. -// Copyright © 2017 Christopher Rohl. All rights reserved. -// - -import Cocoa -import Metal -import MetalKit - -class AppleIMetalView: MTKView { - var commandQueue: MTLCommandQueue? - var renderPipelineState: MTLRenderPipelineState? - var depthStencilState: MTLDepthStencilState? - - required init(coder: NSCoder) { - super.init(coder: coder) - - // Device - device = MTLCreateSystemDefaultDevice() - } -} diff --git a/FruitMachine/MainViewController.swift b/FruitMachine/MainViewController.swift index e3d056d..a686809 100644 --- a/FruitMachine/MainViewController.swift +++ b/FruitMachine/MainViewController.swift @@ -8,7 +8,8 @@ import Cocoa import CoreGraphics -import MetalKit +import OpenGL +import GLKit class MainViewController: NSViewController { @@ -28,6 +29,9 @@ class MainViewController: NSViewController { // Do view setup here. self.view.addSubview(computer.emulatorView) + //computer.emuGLView.doSetup() + //self.view.addSubview(computer.emuGLView) + self.frameTimer = Timer.scheduledTimer(timeInterval: 1/60, target: self, selector: #selector(runEmulation), userInfo: nil, repeats: true) }