Code Monkey home page Code Monkey logo

shaderview's Introduction

ShaderView

A library for simple shader programming.

A ShaderView example with Playground

About ShaderView

ShaderView wraps the MTKView setup.

ShaderView is designed to extend MTKView which enables to do shader view programming simpler and easier, like GLSL Sandbox or Shadertoy. This library wraps the configuration process of MTLLibrary, MTLFunction, MTLComputePipelineState, MTLCommandQueue, MTLCommandBuffer and MTLCommandEncoder, and takes a simple kernel function with the arguments of output texture, current time, touch events and the position in the grid. Create ShderViewRenderer, set it on MTLView and add the MTLView to other UIKit view.

Test with Playground.

The screen recorded above is a playground attacked to this workspace MSLPlayground on which you can write/test simple MSLs. The shader function in the screenshot is converted from the GLSL on Shadertoy.

Usage

ShaderView is consisted mainly from ShaderViewRenderer and MTKView's extension.

import MetalKit
import ShaderView

// Create MTLDevice. MTLDevice is encouranged to create right after the app launch and retain throughout the life time of the app.
guard let device = MTLCreateSystemDefaultDevice else {
    return
}

// Optional: Create MTLLibrary. This is optional and if you don't set your own MTLLibrary, ShaderViewRenderer will generate a default library from the device.
var library: MTLLibrary?
do {
    let path = Bundle.main.path(forResource: "MSLPlayground", ofType: "metal")
    let source = try String(contentsOfFile: path!, encoding: .utf8)
    library = try device.makeLibrary(source: source, options: nil)
} catch let error as NSError {
    print("library error: " + error.description)
}

// Create MTLView
let shaderView = MTKView(frame: NSRect(x: 0, y: 0, width: 400, height: 400), device: device)

// Create ShaderViewRenderer with the device you created.
let renderer = ShaderViewRenderer(device: device)

// Optional: Set MTLLibrary to ShaderViewRenderer
renderer.library = library

// Set the kernel function name to ShaderViewRenderer. The function you specified must have the same arguments as "MSLPlayground.metal" in the example Playground.
renderer.functionName = "playgroundSample"

// Set the renderer to the metal view.
shaderView.sv.set(renderer: renderer)

Requirements

  • Xcode 9.1
  • Swift 4.0.2

shaderview's People

Contributors

hagmas avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.