hadronized / quaazar

Realtime 3D engine
BSD 3-Clause "New" or "Revised" License
6 stars 2 forks source link

Toward a safer OpenGL layer #96

Open hadronized opened 9 years ago

hadronized commented 9 years ago

Context

Up to now, we’re using Quaazar.Render.GL to implement a little thin OpenGL wrapper. That one is basically running (ruining?) in MonadIO m and is straightforwardly performing bare and direct OpenGL calls from the gl package. We then lose Foreign code from the interface with OpenGL, which is great. Apart that, we add nothing more.

OpenGL is a (fucking) state machine. Up to now, we don’t track any state. We just ensure that we don’t fuck up with the state, as all imperative dudes. And as all imperative dudes, we fuck the state up from time to time. That’s boring because fucking with OpenGL means only one thing. This:

Yeah, right? Fucking annoying. I’ve been looking for weeks how to prevent that from happening by lifting runtime errors to compile-time. And I found nothing.

Indexed monads

Indexed monads can be used to implement compile-time tracking of the OpenGL (fucking) state machine. The basic idea is the following:

class IxApplicative f where
  ipure :: a -> f i i a
  iap :: f i j (a -> b) -> f j k a -> f i k b

class (IxApplicative m) => IxMonad m where
  ibind :: f i j a -> (a -> f j k b) -> f i k b

And some kind of state to follow to replace i and j by.

hadronized commented 9 years ago

I guess we could use a runtime stack instead, to track all runtime values.