Code Monkey home page Code Monkey logo

ixshader's Introduction

ixshader

If you're interested in this project please see its spiritual successor gristle, which simplifies the types involved by removing indexed monads.

ixshader is a shallow embedding of the OpenGL Shading Language in Haskell. It aims to look as close to actual glsl shader code as possible, while providing better compile-time safety. Currently writing shader code in ixshader's IxShader monad will catch variable assignment mismatches, multiplication mismatches and some other common errors. It also builds a description of your shader at the type level to use downstream during buffering and uniform updates. Lastly, it abstracts over shader code written for opengl and webgl.

Since this is a work in progress the entire language is not yet supported, though you can easy use the nxt and acc functions to push your own glsl into the pipeline, typechecking only what you want.

The resulting source is pretty and human readable, even debuggable without sourcemapping.

You should create a separate file for your shaders as ixshader relies on the RebindableSyntax language extension and re-defines key functions such as >>=, >>, return, fail, void, fromInteger and fromRational.

example

{-# LANGUAGE AllowAmbiguousTypes   #-}
{-# LANGUAGE DataKinds             #-}
{-# LANGUAGE FlexibleContexts      #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE GADTs                 #-}
{-# LANGUAGE LambdaCase            #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds             #-}
{-# LANGUAGE RankNTypes            #-}
{-# LANGUAGE RebindableSyntax      #-}
{-# LANGUAGE ScopedTypeVariables   #-}
{-# LANGUAGE TypeFamilies          #-}
{-# LANGUAGE TypeInType            #-}
{-# LANGUAGE TypeOperators         #-}
{-# LANGUAGE UndecidableInstances  #-}

module MyShaders where

import Graphics.IxShader

myvertex
  :: forall (ctx :: GLContext). HasContext ctx
  => IxShader ctx '[] '[ In      Xvec2 "position"
                       , In      Xvec4 "color"
                       , Uniform Xmat4 "projection"
                       , Uniform Xmat4 "modelview"
                       , Out     Xvec4 "fcolor"
                       , Out     Xvec4 "gl_Position"
                       , Main
                       ] ()
myvertex = do
  pos    <- in_
  color  <- in_
  proj   <- uniform_
  modl   <- uniform_
  fcolor <- out_
  glPos  <- gl_Position
  main_ $ do
    fcolor .= color
    glPos  .= proj .* modl .* (pos .: 0.0 .: 1.0)

myfragment
  :: forall (ctx :: GLContext). IsGLContext ctx
  => IxShader ctx '[] '[ In  Xvec4 "fcolor"
                       , Out Xvec4 (GLFragName ctx)
                       , Main
                       ] ()
myfragment = do
  fcolor <- in_
  glFrag <- gl_FragColor
  main_ $ glFrag .= fcolor


main = do
  putStrLn "First OpenGL:"
  putSrcLn $ vertex @'OpenGLContext
  putStrLn "\nThen WebGL:"
  putSrcLn $ vertex @'WebGLContext
{-
First OpenGL:
in vec2 position;
in vec4 color;
uniform mat4 projection;
uniform mat4 modelview;
out vec4 fcolor;
void main ()
{ fcolor = color;
  gl_Position = projection * modelview * vec4 (position.x, position.y, 0.0, 1.0);
}

Then WebGL:
attribute vec2 position;
attribute vec4 color;
uniform mat4 projection;
uniform mat4 modelview;
varying vec4 fcolor;
void main ()
{ fcolor = color;
  gl_Position = projection * modelview * vec4 (position.x, position.y, 0.0, 1.0);
}
-}

ixshader's People

Contributors

schell avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

tsahyt

ixshader's Issues

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.