Skip to content

Commit

Permalink
Updated the shader converter script to Swift 3.
Browse files Browse the repository at this point in the history
  • Loading branch information
BradLarson committed Jul 27, 2016
1 parent f9fc518 commit 9152a1f
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
17 changes: 14 additions & 3 deletions framework/Source/Mac/PictureOutput.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public class PictureOutput: ImageConsumer {
self.url = url // Create an intentional short-term retain cycle to prevent deallocation before next frame is captured
encodedImageAvailableCallback = {imageData in
do {
// FIXME: Xcode 8 beta 2
try imageData.write(to: self.url, options:.atomic)
// try imageData.write(to: self.url, options:NSData.WritingOptions.dataWritingAtomic)
} catch {
// TODO: Handle this better
print("WARNING: Couldn't save image with error:\(error)")
Expand All @@ -41,8 +43,17 @@ public class PictureOutput: ImageConsumer {
let renderFramebuffer = sharedImageProcessingContext.framebufferCache.requestFramebufferWithProperties(orientation:framebuffer.orientation, size:framebuffer.size)
renderFramebuffer.lock()
renderFramebuffer.activateFramebufferForRendering()
clearFramebufferWithColor(Color.red)
clearFramebufferWithColor(Color.transparent)

// Need the blending here to enable non-1.0 alpha on output image
glBlendEquation(GLenum(GL_FUNC_ADD))
glBlendFunc(GLenum(GL_ONE), GLenum(GL_ONE))
glEnable(GLenum(GL_BLEND))

renderQuadWithShader(sharedImageProcessingContext.passthroughShader, uniformSettings:ShaderUniformSettings(), vertices:standardImageVertices, inputTextures:[framebuffer.texturePropertiesForOutputRotation(.noRotation)])

glDisable(GLenum(GL_BLEND))

framebuffer.unlock()

let imageByteSize = Int(framebuffer.size.width * framebuffer.size.height * 4)
Expand All @@ -51,8 +62,8 @@ public class PictureOutput: ImageConsumer {
renderFramebuffer.unlock()
guard let dataProvider = CGDataProvider(dataInfo: nil, data: data, size: imageByteSize, releaseData: dataProviderReleaseCallback) else {fatalError("Could not create CGDataProvider")}
let defaultRGBColorSpace = CGColorSpaceCreateDeviceRGB()
return CGImage(width: Int(framebuffer.size.width), height: Int(framebuffer.size.height), bitsPerComponent: 8, bitsPerPixel: 32, bytesPerRow: 4 * Int(framebuffer.size.width), space: defaultRGBColorSpace, bitmapInfo: CGBitmapInfo() /*| CGImageAlphaInfo.Last*/, provider: dataProvider, decode: nil, shouldInterpolate: false, intent: .defaultIntent)!


return CGImage(width: Int(framebuffer.size.width), height: Int(framebuffer.size.height), bitsPerComponent:8, bitsPerPixel:32, bytesPerRow:4 * Int(framebuffer.size.width), space:defaultRGBColorSpace, bitmapInfo:CGBitmapInfo() /*| CGImageAlphaInfo.Last*/, provider:dataProvider, decode:nil, shouldInterpolate:false, intent:.defaultIntent)!
}

public func newFramebufferAvailable(_ framebuffer:Framebuffer, fromSourceIndex:UInt) {
Expand Down
24 changes: 12 additions & 12 deletions framework/Source/Operations/Shaders/ShaderConverter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ var allConvertedGLShaders = ""
var allConvertedGLESShaders = ""

for fileName in fileNames {
let pathURL = NSURL(fileURLWithPath:fileName)
let pathURL = URL(fileURLWithPath:fileName)
guard let pathExtension = pathURL.pathExtension else {continue}
guard let baseName = pathURL.URLByDeletingPathExtension?.lastPathComponent else {continue}
guard let baseName = try! pathURL.deletingPathExtension().lastPathComponent else {continue}

guard (NSFileManager.defaultManager().fileExistsAtPath(pathURL.path!)) else {
guard (FileManager.default.fileExists(atPath:pathURL.path!)) else {
print("Error: file \"\(fileName)\" could not be found.")
continue
}

let shaderSuffix:String
if (pathExtension.lowercaseString == "vsh") {
if (pathExtension.lowercased() == "vsh") {
shaderSuffix = "VertexShader"
} else if (pathExtension.lowercaseString == "fsh") {
} else if (pathExtension.lowercased() == "fsh") {
shaderSuffix = "FragmentShader"
} else {
continue
Expand All @@ -36,20 +36,20 @@ for fileName in fileNames {
let convertedShaderName:String
let shaderPlatform:OpenGLPlatform
if baseName.hasSuffix("_GLES") {
convertedShaderName = "\(baseName.stringByReplacingOccurrencesOfString("_GLES", withString:""))\(shaderSuffix)"
convertedShaderName = "\(baseName.replacingOccurrences(of:"_GLES", with:""))\(shaderSuffix)"
shaderPlatform = .OpenGLES
} else if baseName.hasSuffix("_GL") {
convertedShaderName = "\(baseName.stringByReplacingOccurrencesOfString("_GL", withString:""))\(shaderSuffix)"
convertedShaderName = "\(baseName.replacingOccurrences(of:"_GL", with:""))\(shaderSuffix)"
shaderPlatform = .OpenGL
} else {
convertedShaderName = "\(baseName)\(shaderSuffix)"
shaderPlatform = .Both
}

var accumulatedString = "public let \(convertedShaderName) = \""
let fileContents = try String(contentsOfFile:fileName, encoding:NSASCIIStringEncoding)
let fileContents = try String(contentsOfFile:fileName, encoding:String.Encoding.ascii)
fileContents.enumerateLines {line, stop in
accumulatedString += "\(line.stringByReplacingOccurrencesOfString("\"", withString:"\\\""))\\n "
accumulatedString += "\(line.replacingOccurrences(of:"\"", with:"\\\""))\\n "
}
accumulatedString += "\"\n"

Expand All @@ -62,6 +62,6 @@ for fileName in fileNames {
}
}

let scriptURL = NSURL(fileURLWithPath:Process.arguments.first!)
try allConvertedGLShaders.writeToURL(scriptURL.URLByDeletingLastPathComponent!.URLByAppendingPathComponent("ConvertedShaders_GL.swift"), atomically:true, encoding:NSASCIIStringEncoding)
try allConvertedGLESShaders.writeToURL(scriptURL.URLByDeletingLastPathComponent!.URLByAppendingPathComponent("ConvertedShaders_GLES.swift"), atomically:true, encoding:NSASCIIStringEncoding)
let scriptURL = URL(fileURLWithPath:Process.arguments.first!)
try allConvertedGLShaders.write(to:scriptURL.deletingLastPathComponent().appendingPathComponent("ConvertedShaders_GL.swift"), atomically:true, encoding:String.Encoding.ascii)
try allConvertedGLESShaders.write(to:scriptURL.deletingLastPathComponent().appendingPathComponent("ConvertedShaders_GLES.swift"), atomically:true, encoding:String.Encoding.ascii)
4 changes: 4 additions & 0 deletions framework/Source/SerialDispatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,19 @@ public let lowProcessingQueuePriority:DispatchQueue.GlobalAttributes = {
}()

func runAsynchronouslyOnMainQueue(_ mainThreadOperation:() -> ()) {
// FIXME: Xcode 8 beta 2
if (Thread.isMainThread) {
// if (Thread.isMainThread()) {
mainThreadOperation()
} else {
DispatchQueue.main.async(execute:mainThreadOperation)
}
}

func runOnMainQueue(_ mainThreadOperation:() -> ()) {
// FIXME: Xcode 8 beta 2
if (Thread.isMainThread) {
// if (Thread.isMainThread()) {
mainThreadOperation()
} else {
DispatchQueue.main.sync(execute:mainThreadOperation)
Expand Down
3 changes: 3 additions & 0 deletions framework/Source/ShaderProgram.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,10 @@ public func crashOnShaderCompileFailure<T>(_ shaderName:String, _ operation:() t
public func shaderFromFile(_ file:URL) throws -> String {
// Note: this is a hack until Foundation's String initializers are fully functional
// let fragmentShaderString = String(contentsOfURL:fragmentShaderFile, encoding:NSASCIIStringEncoding)
// FIXME: Xcode 8 beta 2
guard (FileManager.default.fileExists(atPath: file.path!)) else { throw ShaderCompileError(compileLog:"Shader file \(file) missing")}
// guard (FileManager.default().fileExists(atPath: file.path!)) else { throw ShaderCompileError(compileLog:"Shader file \(file) missing")}

let fragmentShaderString = try NSString(contentsOfFile:file.path!, encoding:String.Encoding.ascii.rawValue)

return String(fragmentShaderString)
Expand Down

0 comments on commit 9152a1f

Please sign in to comment.