aboutsummaryrefslogtreecommitdiff
path: root/Spear/GL.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Spear/GL.hs')
-rw-r--r--Spear/GL.hs42
1 files changed, 21 insertions, 21 deletions
diff --git a/Spear/GL.hs b/Spear/GL.hs
index f463109..3c1734b 100644
--- a/Spear/GL.hs
+++ b/Spear/GL.hs
@@ -36,7 +36,7 @@ module Spear.GL
36 Data.StateVar.get, 36 Data.StateVar.get,
37 37
38 -- * VAOs 38 -- * VAOs
39 VAO, 39 VAO(..),
40 newVAO, 40 newVAO,
41 bindVAO, 41 bindVAO,
42 unbindVAO, 42 unbindVAO,
@@ -48,7 +48,7 @@ module Spear.GL
48 drawElements, 48 drawElements,
49 49
50 -- * Buffers 50 -- * Buffers
51 GLBuffer, 51 GLBuffer(..),
52 TargetBuffer (..), 52 TargetBuffer (..),
53 BufferUsage (..), 53 BufferUsage (..),
54 newBuffer, 54 newBuffer,
@@ -122,7 +122,7 @@ import Unsafe.Coerce
122-- | A GLSL shader handle. 122-- | A GLSL shader handle.
123data GLSLShader = GLSLShader 123data GLSLShader = GLSLShader
124 { getShader :: GLuint, 124 { getShader :: GLuint,
125 getShaderKey :: Resource 125 getShaderKey :: ReleaseKey
126 } 126 }
127 127
128instance ResourceClass GLSLShader where 128instance ResourceClass GLSLShader where
@@ -131,7 +131,7 @@ instance ResourceClass GLSLShader where
131-- | A GLSL program handle. 131-- | A GLSL program handle.
132data GLSLProgram = GLSLProgram 132data GLSLProgram = GLSLProgram
133 { getProgram :: GLuint, 133 { getProgram :: GLuint,
134 getProgramKey :: Resource 134 getProgramKey :: ReleaseKey
135 } 135 }
136 136
137instance ResourceClass GLSLProgram where 137instance ResourceClass GLSLProgram where
@@ -173,11 +173,11 @@ attribLocation prog var = makeStateVar get set
173-- | Create a new program. 173-- | Create a new program.
174newProgram :: [GLSLShader] -> Game s GLSLProgram 174newProgram :: [GLSLShader] -> Game s GLSLProgram
175newProgram shaders = do 175newProgram shaders = do
176 h <- gameIO glCreateProgram 176 h <- liftIO glCreateProgram
177 when (h == 0) $ gameError "glCreateProgram failed" 177 when (h == 0) $ gameError "glCreateProgram failed"
178 rkey <- register $ deleteProgram h 178 rkey <- register $ deleteProgram h
179 let program = GLSLProgram h rkey 179 let program = GLSLProgram h rkey
180 mapM_ (gameIO . attachShader program) shaders 180 mapM_ (liftIO . attachShader program) shaders
181 linkProgram program 181 linkProgram program
182 return program 182 return program
183 183
@@ -192,7 +192,7 @@ deleteProgram prog = do
192linkProgram :: GLSLProgram -> Game s () 192linkProgram :: GLSLProgram -> Game s ()
193linkProgram prog = do 193linkProgram prog = do
194 let h = getProgram prog 194 let h = getProgram prog
195 err <- gameIO $ do 195 err <- liftIO $ do
196 glLinkProgram h 196 glLinkProgram h
197 alloca $ \statptr -> do 197 alloca $ \statptr -> do
198 glGetProgramiv h GL_LINK_STATUS statptr 198 glGetProgramiv h GL_LINK_STATUS statptr
@@ -235,7 +235,7 @@ loadShader shaderType file = do
235-- | Create a new shader. 235-- | Create a new shader.
236newShader :: ShaderType -> Game s GLSLShader 236newShader :: ShaderType -> Game s GLSLShader
237newShader shaderType = do 237newShader shaderType = do
238 h <- gameIO $ glCreateShader (toGLShader shaderType) 238 h <- liftIO $ glCreateShader (toGLShader shaderType)
239 case h of 239 case h of
240 0 -> gameError "glCreateShader failed" 240 0 -> gameError "glCreateShader failed"
241 _ -> do 241 _ -> do
@@ -253,10 +253,10 @@ deleteShader shader = do
253-- into the shader. 253-- into the shader.
254loadSource :: FilePath -> GLSLShader -> Game s () 254loadSource :: FilePath -> GLSLShader -> Game s ()
255loadSource file h = do 255loadSource file h = do
256 exists <- gameIO $ doesFileExist file 256 exists <- liftIO $ doesFileExist file
257 case exists of 257 case exists of
258 False -> gameError "the specified shader file does not exist" 258 False -> gameError "the specified shader file does not exist"
259 True -> gameIO $ do 259 True -> liftIO $ do
260 code <- readSource file 260 code <- readSource file
261 withCString code $ shaderSource h 261 withCString code $ shaderSource h
262 262
@@ -272,10 +272,10 @@ compile file shader = do
272 let h = getShader shader 272 let h = getShader shader
273 273
274 -- Compile 274 -- Compile
275 gameIO $ glCompileShader h 275 liftIO $ glCompileShader h
276 276
277 -- Verify status 277 -- Verify status
278 err <- gameIO $ 278 err <- liftIO $
279 alloca $ \statusPtr -> do 279 alloca $ \statusPtr -> do
280 glGetShaderiv h GL_COMPILE_STATUS statusPtr 280 glGetShaderiv h GL_COMPILE_STATUS statusPtr
281 result <- peek statusPtr 281 result <- peek statusPtr
@@ -438,7 +438,7 @@ instance Uniform [Int] where
438-- | A vertex array object. 438-- | A vertex array object.
439data VAO = VAO 439data VAO = VAO
440 { getVAO :: GLuint, 440 { getVAO :: GLuint,
441 vaoKey :: Resource 441 vaoKey :: ReleaseKey
442 } 442 }
443 443
444instance ResourceClass VAO where 444instance ResourceClass VAO where
@@ -454,7 +454,7 @@ instance Ord VAO where
454-- | Create a new vao. 454-- | Create a new vao.
455newVAO :: Game s VAO 455newVAO :: Game s VAO
456newVAO = do 456newVAO = do
457 h <- gameIO . alloca $ \ptr -> do 457 h <- liftIO . alloca $ \ptr -> do
458 glGenVertexArrays 1 ptr 458 glGenVertexArrays 1 ptr
459 peek ptr 459 peek ptr
460 460
@@ -533,11 +533,11 @@ drawElements mode count t idxs = glDrawElements mode (unsafeCoerce count) t idxs
533-- | An OpenGL buffer. 533-- | An OpenGL buffer.
534data GLBuffer = GLBuffer 534data GLBuffer = GLBuffer
535 { getBuffer :: GLuint, 535 { getBuffer :: GLuint,
536 rkey :: Resource 536 bufferKey :: ReleaseKey
537 } 537 }
538 538
539instance ResourceClass GLBuffer where 539instance ResourceClass GLBuffer where
540 getResource = rkey 540 getResource = bufferKey
541 541
542-- | The type of target buffer. 542-- | The type of target buffer.
543data TargetBuffer 543data TargetBuffer
@@ -580,7 +580,7 @@ fromUsage DynamicCopy = GL_DYNAMIC_COPY
580-- | Create a new buffer. 580-- | Create a new buffer.
581newBuffer :: Game s GLBuffer 581newBuffer :: Game s GLBuffer
582newBuffer = do 582newBuffer = do
583 h <- gameIO . alloca $ \ptr -> do 583 h <- liftIO . alloca $ \ptr -> do
584 glGenBuffers 1 ptr 584 glGenBuffers 1 ptr
585 peek ptr 585 peek ptr
586 586
@@ -656,7 +656,7 @@ withGLBuffer buf f = f $ getBuffer buf
656-- | Represents a texture resource. 656-- | Represents a texture resource.
657data Texture = Texture 657data Texture = Texture
658 { getTex :: GLuint, 658 { getTex :: GLuint,
659 texKey :: Resource 659 texKey :: ReleaseKey
660 } 660 }
661 661
662instance Eq Texture where 662instance Eq Texture where
@@ -672,7 +672,7 @@ instance ResourceClass Texture where
672-- | Create a new texture. 672-- | Create a new texture.
673newTexture :: Game s Texture 673newTexture :: Game s Texture
674newTexture = do 674newTexture = do
675 tex <- gameIO . alloca $ \ptr -> do 675 tex <- liftIO . alloca $ \ptr -> do
676 glGenTextures 1 ptr 676 glGenTextures 1 ptr
677 peek ptr 677 peek ptr
678 678
@@ -697,7 +697,7 @@ loadTextureImage ::
697loadTextureImage file minFilter magFilter = do 697loadTextureImage file minFilter magFilter = do
698 image <- loadImage file 698 image <- loadImage file
699 tex <- newTexture 699 tex <- newTexture
700 gameIO $ do 700 liftIO $ do
701 let w = width image 701 let w = width image
702 h = height image 702 h = height image
703 pix = pixels image 703 pix = pixels image
@@ -794,7 +794,7 @@ printGLError =
794assertGL :: Game s a -> String -> Game s a 794assertGL :: Game s a -> String -> Game s a
795assertGL action err = do 795assertGL action err = do
796 result <- action 796 result <- action
797 status <- gameIO getGLError 797 status <- liftIO getGLError
798 case status of 798 case status of
799 Just str -> gameError $ "OpenGL error raised: " ++ err ++ "; " ++ str 799 Just str -> gameError $ "OpenGL error raised: " ++ err ++ "; " ++ str
800 Nothing -> return result 800 Nothing -> return result