diff options
author | Csaba Hruska <csaba.hruska@gmail.com> | 2016-01-03 15:56:58 +0100 |
---|---|---|
committer | Csaba Hruska <csaba.hruska@gmail.com> | 2016-01-03 15:56:58 +0100 |
commit | 65c124310c6aad1fa7a97c547292f8b90a70e991 (patch) | |
tree | 98c27515f4731f013dc1e67d58ce6f3023db9e71 /Backend | |
parent | 7dd4c572fcfff44d179ec6dcd541f727babb51e6 (diff) |
texture slot support
Diffstat (limited to 'Backend')
-rw-r--r-- | Backend/GL.hs | 1 | ||||
-rw-r--r-- | Backend/GL/Backend.hs | 39 | ||||
-rw-r--r-- | Backend/GL/Data.hs | 26 | ||||
-rw-r--r-- | Backend/GL/Input.hs | 6 | ||||
-rw-r--r-- | Backend/GL/Util.hs | 3 |
5 files changed, 60 insertions, 15 deletions
diff --git a/Backend/GL.hs b/Backend/GL.hs index 6b27cd1..3edb4fa 100644 --- a/Backend/GL.hs +++ b/Backend/GL.hs | |||
@@ -23,6 +23,7 @@ module Backend.GL ( | |||
23 | arraySize, | 23 | arraySize, |
24 | arrayType, | 24 | arrayType, |
25 | compileTexture2DRGBAF, | 25 | compileTexture2DRGBAF, |
26 | compileTexture2DRGBAF', | ||
26 | 27 | ||
27 | -- GL Pipeline Input, Object | 28 | -- GL Pipeline Input, Object |
28 | GLPipeline, | 29 | GLPipeline, |
diff --git a/Backend/GL/Backend.hs b/Backend/GL/Backend.hs index d3abfad..55ae443 100644 --- a/Backend/GL/Backend.hs +++ b/Backend/GL/Backend.hs | |||
@@ -1,4 +1,4 @@ | |||
1 | {-# LANGUAGE TupleSections, MonadComprehensions, ViewPatterns #-} | 1 | {-# LANGUAGE TupleSections, MonadComprehensions, ViewPatterns, RecordWildCards #-} |
2 | module Backend.GL.Backend where | 2 | module Backend.GL.Backend where |
3 | 3 | ||
4 | import Control.Applicative | 4 | import Control.Applicative |
@@ -9,6 +9,7 @@ import Data.ByteString.Char8 (ByteString,pack) | |||
9 | import Data.IORef | 9 | import Data.IORef |
10 | import Data.IntMap (IntMap) | 10 | import Data.IntMap (IntMap) |
11 | import Data.Maybe (isNothing,fromJust) | 11 | import Data.Maybe (isNothing,fromJust) |
12 | import Data.Map (Map) | ||
12 | import Data.Set (Set) | 13 | import Data.Set (Set) |
13 | import Data.Trie as T | 14 | import Data.Trie as T |
14 | import Data.Trie.Convenience as T | 15 | import Data.Trie.Convenience as T |
@@ -242,15 +243,29 @@ compileProgram uniTrie p = do | |||
242 | unless (check lcStreams attributesType) $ fail $ "shader program stream input mismatch! " ++ show (attributesType,lcStreams) | 243 | unless (check lcStreams attributesType) $ fail $ "shader program stream input mismatch! " ++ show (attributesType,lcStreams) |
243 | -- the public (user) pipeline and program input is encoded by the slots, therefore the programs does not distinct the render and slot textures input | 244 | -- the public (user) pipeline and program input is encoded by the slots, therefore the programs does not distinct the render and slot textures input |
244 | let inUniNames = toTrie $ programUniforms p | 245 | let inUniNames = toTrie $ programUniforms p |
245 | (inUniforms,inTextures) = L.partition (\(n,v) -> T.member n inUniNames) $ T.toList $ uniforms | 246 | inUniforms = L.filter (\(n,v) -> T.member n inUniNames) $ T.toList $ uniforms |
247 | inTextureNames = toTrie $ programInTextures p | ||
248 | inTextures = L.filter (\(n,v) -> T.member n inTextureNames) $ T.toList $ uniforms | ||
246 | texUnis = [n | (n,_) <- inTextures, T.member n uniTrie] | 249 | texUnis = [n | (n,_) <- inTextures, T.member n uniTrie] |
250 | putStrLn $ "uniTrie: " ++ show (T.keys uniTrie) | ||
251 | putStrLn $ "inUniNames: " ++ show inUniNames | ||
252 | putStrLn $ "inUniforms: " ++ show inUniforms | ||
253 | putStrLn $ "inTextureNames: " ++ show inTextureNames | ||
254 | putStrLn $ "inTextures: " ++ show inTextures | ||
255 | putStrLn $ "texUnis: " ++ show texUnis | ||
256 | let valA = T.toList $ attributes | ||
257 | valB = T.toList $ toTrie $ programStreams p | ||
258 | putStrLn "------------" | ||
259 | print $ T.toList $ attributes | ||
260 | print $ T.toList $ toTrie $ programStreams p | ||
261 | let lcStreamName = fmap name (toTrie $ programStreams p) | ||
247 | return $ GLProgram | 262 | return $ GLProgram |
248 | { shaderObjects = objs | 263 | { shaderObjects = objs |
249 | , programObject = po | 264 | , programObject = po |
250 | , inputUniforms = T.fromList inUniforms | 265 | , inputUniforms = T.fromList inUniforms |
251 | , inputTextures = T.fromList inTextures | 266 | , inputTextures = T.fromList inTextures |
252 | , inputTextureUniforms = S.fromList $ texUnis | 267 | , inputTextureUniforms = S.fromList $ texUnis |
253 | , inputStreams = T.fromList [(n,(idx, pack attrName)) | ((n,idx),(_,(Parameter attrName _))) <- zip (T.toList $ attributes) (T.toList $ toTrie $ programStreams p)] | 268 | , inputStreams = T.fromList [(n,(idx, pack attrName)) | (n,idx) <- T.toList $ attributes, let Just attrName = T.lookup n lcStreamName] |
254 | } | 269 | } |
255 | 270 | ||
256 | compileSampler :: SamplerDescriptor -> IO GLSampler | 271 | compileSampler :: SamplerDescriptor -> IO GLSampler |
@@ -493,6 +508,7 @@ allocPipeline p = do | |||
493 | trgs <- V.mapM (compileRenderTarget (textures p) texs) $ targets p | 508 | trgs <- V.mapM (compileRenderTarget (textures p) texs) $ targets p |
494 | prgs <- V.mapM (compileProgram uniTrie) $ programs p | 509 | prgs <- V.mapM (compileProgram uniTrie) $ programs p |
495 | -- texture unit mapping ioref trie | 510 | -- texture unit mapping ioref trie |
511 | -- texUnitMapRefs :: Map UniformName (IORef TextureUnit) | ||
496 | texUnitMapRefs <- T.fromList <$> mapM (\k -> (k,) <$> newIORef 0) (S.toList $ S.fromList $ concat $ V.toList $ V.map (T.keys . toTrie . programInTextures) $ programs p) | 512 | texUnitMapRefs <- T.fromList <$> mapM (\k -> (k,) <$> newIORef 0) (S.toList $ S.fromList $ concat $ V.toList $ V.map (T.keys . toTrie . programInTextures) $ programs p) |
497 | let (cmds,st) = runState (mapM (compileCommand texUnitMapRefs smps texs trgs prgs) $ V.toList $ commands p) initCGState | 513 | let (cmds,st) = runState (mapM (compileCommand texUnitMapRefs smps texs trgs prgs) $ V.toList $ commands p) initCGState |
498 | input <- newIORef Nothing | 514 | input <- newIORef Nothing |
@@ -702,11 +718,12 @@ renderSlot cmds = forM_ cmds $ \cmd -> do | |||
702 | texUnit <- readIORef tuRef | 718 | texUnit <- readIORef tuRef |
703 | glActiveTexture $ gl_TEXTURE0 + fromIntegral texUnit | 719 | glActiveTexture $ gl_TEXTURE0 + fromIntegral texUnit |
704 | glBindTexture txTarget txObj | 720 | glBindTexture txTarget txObj |
721 | putStrLn $ "to texture unit " ++ show texUnit ++ " texture object " ++ show txObj | ||
705 | GLSetVertexAttrib idx val -> do | 722 | GLSetVertexAttrib idx val -> do |
706 | glDisableVertexAttribArray idx | 723 | glDisableVertexAttribArray idx |
707 | setVertexAttrib idx val | 724 | setVertexAttrib idx val |
708 | --isOk <- checkGL | 725 | isOk <- checkGL |
709 | --putStrLn $ SB.unpack isOk ++ " - " ++ show cmd | 726 | putStrLn $ SB.unpack isOk ++ " - " ++ show cmd |
710 | 727 | ||
711 | renderPipeline :: GLPipeline -> IO () | 728 | renderPipeline :: GLPipeline -> IO () |
712 | renderPipeline glp = do | 729 | renderPipeline glp = do |
@@ -755,18 +772,20 @@ renderPipeline glp = do | |||
755 | GLSaveImage | 772 | GLSaveImage |
756 | GLLoadImage | 773 | GLLoadImage |
757 | -} | 774 | -} |
758 | --isOk <- checkGL | 775 | isOk <- checkGL |
759 | --putStrLn $ SB.unpack isOk ++ " - " ++ show cmd | 776 | putStrLn $ SB.unpack isOk ++ " - " ++ show cmd |
760 | 777 | ||
761 | data CGState | 778 | data CGState |
762 | = CGState | 779 | = CGState |
763 | { currentProgram :: ProgramName | 780 | { currentProgram :: ProgramName |
764 | , textureBinding :: IntMap GLTexture | 781 | , textureBinding :: IntMap GLTexture |
782 | , samplerUniforms :: Map UniformName TextureUnit | ||
765 | } | 783 | } |
766 | 784 | ||
767 | initCGState = CGState | 785 | initCGState = CGState |
768 | { currentProgram = error "CGState: empty currentProgram" | 786 | { currentProgram = error "CGState: empty currentProgram" |
769 | , textureBinding = IM.empty | 787 | , textureBinding = IM.empty |
788 | , samplerUniforms = mempty | ||
770 | } | 789 | } |
771 | 790 | ||
772 | type CG a = State CGState a | 791 | type CG a = State CGState a |
@@ -780,11 +799,12 @@ compileCommand texUnitMap samplers textures targets programs cmd = case cmd of | |||
780 | modify (\s -> s {currentProgram = p}) | 799 | modify (\s -> s {currentProgram = p}) |
781 | return $ GLSetProgram $ programObject $ programs ! p | 800 | return $ GLSetProgram $ programObject $ programs ! p |
782 | SetSamplerUniform n tu -> do | 801 | SetSamplerUniform n tu -> do |
802 | modify (\s@CGState{..} -> s {samplerUniforms = Map.insert n tu samplerUniforms}) | ||
783 | p <- currentProgram <$> get | 803 | p <- currentProgram <$> get |
784 | case T.lookup (pack n) (inputTextures $ programs ! p) of | 804 | case T.lookup (pack n) (inputTextures $ programs ! p) of |
785 | Nothing -> fail "internal error (SetSamplerUniform)!" | 805 | Nothing -> fail $ "internal error (SetSamplerUniform)! - " ++ show cmd |
786 | Just i -> case T.lookup (pack n) texUnitMap of | 806 | Just i -> case T.lookup (pack n) texUnitMap of |
787 | Nothing -> fail "internal error (SetSamplerUniform - IORef)!" | 807 | Nothing -> fail $ "internal error (SetSamplerUniform - IORef)! - " ++ show cmd |
788 | Just r -> return $ GLSetSamplerUniform i (fromIntegral tu) r | 808 | Just r -> return $ GLSetSamplerUniform i (fromIntegral tu) r |
789 | SetTexture tu t -> do | 809 | SetTexture tu t -> do |
790 | let tex = textures ! t | 810 | let tex = textures ! t |
@@ -795,6 +815,7 @@ compileCommand texUnitMap samplers textures targets programs cmd = case cmd of | |||
795 | glBindSampler (fromIntegral tu) (samplerObject $ glSamplers glp ! s) | 815 | glBindSampler (fromIntegral tu) (samplerObject $ glSamplers glp ! s) |
796 | -} | 816 | -} |
797 | RenderSlot slot -> do | 817 | RenderSlot slot -> do |
818 | smpUnis <- samplerUniforms <$> get | ||
798 | p <- currentProgram <$> get | 819 | p <- currentProgram <$> get |
799 | return $ GLRenderSlot slot p | 820 | return $ GLRenderSlot slot p |
800 | RenderStream stream -> do | 821 | RenderStream stream -> do |
diff --git a/Backend/GL/Data.hs b/Backend/GL/Data.hs index 4eb3fa0..2c6e596 100644 --- a/Backend/GL/Data.hs +++ b/Backend/GL/Data.hs | |||
@@ -19,7 +19,7 @@ import qualified Data.Vector.Storable as SV | |||
19 | import Graphics.Rendering.OpenGL.Raw.Core33 | 19 | import Graphics.Rendering.OpenGL.Raw.Core33 |
20 | import Data.Word | 20 | import Data.Word |
21 | import Codec.Picture | 21 | import Codec.Picture |
22 | import Codec.Picture.RGBA8 | 22 | import Codec.Picture.Types |
23 | 23 | ||
24 | import Backend.GL.Type | 24 | import Backend.GL.Type |
25 | import Backend.GL.Util | 25 | import Backend.GL.Util |
@@ -60,8 +60,26 @@ arrayType buf arrIdx = arrType $! bufArrays buf V.! arrIdx | |||
60 | 60 | ||
61 | -- FIXME: Temporary implemenation | 61 | -- FIXME: Temporary implemenation |
62 | compileTexture2DRGBAF :: Bool -> Bool -> DynamicImage -> IO TextureData | 62 | compileTexture2DRGBAF :: Bool -> Bool -> DynamicImage -> IO TextureData |
63 | compileTexture2DRGBAF isMip isClamped bitmap' = do | 63 | compileTexture2DRGBAF = compileTexture2DRGBAF' False |
64 | let bitmap = ImageRGBA8 $ fromDynamicImage bitmap' | 64 | |
65 | compileTexture2DRGBAF' :: Bool -> Bool -> Bool -> DynamicImage -> IO TextureData | ||
66 | compileTexture2DRGBAF' isSRGB isMip isClamped bitmap' = do | ||
67 | let bitmap = case bitmap' of | ||
68 | ImageRGB8 i@(Image w h _) -> bitmap' -- pixelFoldMap (\(PixelRGB8 r g b) -> [PixelRGBA8 r g b maxBound]) i | ||
69 | ImageRGBA8 i@(Image w h _) -> bitmap' -- pixelFoldMap (\(PixelRGBA8 r g b a) -> [PixelRGBA8 r g b a]) i | ||
70 | ImageYCbCr8 i@(Image w h _) -> ImageRGB8 $ convertImage i -- $ Image w h $ SV.fromList $ pixelFoldMap (\p -> let PixelRGB8 r g b = convertPixel p in [PixelRGBA8 r g b maxBound]) i | ||
71 | ImageCMYK16 _ -> error "compileTexture2DRGBAF: ImageCMYK16" | ||
72 | ImageCMYK8 _ -> error "compileTexture2DRGBAF: ImageCMYK8" | ||
73 | ImageRGBA16 _ -> error "compileTexture2DRGBAF: ImageRGBA16" | ||
74 | ImageRGBF _ -> error "compileTexture2DRGBAF: ImageRGBF" | ||
75 | ImageRGB16 _ -> error "compileTexture2DRGBAF: ImageRGB16" | ||
76 | ImageYA16 _ -> error "compileTexture2DRGBAF: ImageYA16" | ||
77 | ImageYA8 _ -> error "compileTexture2DRGBAF: ImageYA8" | ||
78 | ImageYF _ -> error "compileTexture2DRGBAF: ImageYF" | ||
79 | ImageY16 _ -> error "compileTexture2DRGBAF: ImageY16" | ||
80 | ImageY8 _ -> error "compileTexture2DRGBAF: ImageY8" | ||
81 | _ -> error "compileTexture2DRGBAF: unknown image" | ||
82 | |||
65 | glPixelStorei gl_UNPACK_ALIGNMENT 1 | 83 | glPixelStorei gl_UNPACK_ALIGNMENT 1 |
66 | to <- alloca $! \pto -> glGenTextures 1 pto >> peek pto | 84 | to <- alloca $! \pto -> glGenTextures 1 pto >> peek pto |
67 | glBindTexture gl_TEXTURE_2D to | 85 | glBindTexture gl_TEXTURE_2D to |
@@ -85,7 +103,7 @@ compileTexture2DRGBAF isMip isClamped bitmap' = do | |||
85 | glTexParameteri gl_TEXTURE_2D gl_TEXTURE_BASE_LEVEL 0 | 103 | glTexParameteri gl_TEXTURE_2D gl_TEXTURE_BASE_LEVEL 0 |
86 | glTexParameteri gl_TEXTURE_2D gl_TEXTURE_MAX_LEVEL $ fromIntegral maxLevel | 104 | glTexParameteri gl_TEXTURE_2D gl_TEXTURE_MAX_LEVEL $ fromIntegral maxLevel |
87 | withBitmap bitmap $ \(w,h) nchn 0 ptr -> do | 105 | withBitmap bitmap $ \(w,h) nchn 0 ptr -> do |
88 | let internalFormat = fromIntegral gl_RGBA8 | 106 | let internalFormat = fromIntegral $ if isSRGB then (if nchn == 3 then gl_SRGB8 else gl_SRGB8_ALPHA8) else (if nchn == 3 then gl_RGB8 else gl_RGBA8) |
89 | dataFormat = fromIntegral $ case nchn of | 107 | dataFormat = fromIntegral $ case nchn of |
90 | 3 -> gl_RGB | 108 | 3 -> gl_RGB |
91 | 4 -> gl_RGBA | 109 | 4 -> gl_RGBA |
diff --git a/Backend/GL/Input.hs b/Backend/GL/Input.hs index e1d06b7..f92a9c9 100644 --- a/Backend/GL/Input.hs +++ b/Backend/GL/Input.hs | |||
@@ -31,7 +31,11 @@ import qualified IR as IR | |||
31 | schemaFromPipeline :: IR.Pipeline -> PipelineSchema | 31 | schemaFromPipeline :: IR.Pipeline -> PipelineSchema |
32 | schemaFromPipeline a = PipelineSchema (T.fromList sl) (foldl T.unionL T.empty ul) | 32 | schemaFromPipeline a = PipelineSchema (T.fromList sl) (foldl T.unionL T.empty ul) |
33 | where | 33 | where |
34 | (sl,ul) = unzip [((pack n,SlotSchema p (fmap cvt (toTrie s))),toTrie u) | IR.Slot n u s p _ <- V.toList $ IR.slots a] | 34 | (sl,ul) = unzip [( (pack sName,SlotSchema sPrimitive (fmap cvt (toTrie sStreams))) |
35 | , toTrie sUniforms | ||
36 | ) | ||
37 | | IR.Slot sName sStreams sUniforms sPrimitive _ <- V.toList $ IR.slots a | ||
38 | ] | ||
35 | cvt a = case toStreamType a of | 39 | cvt a = case toStreamType a of |
36 | Just v -> v | 40 | Just v -> v |
37 | Nothing -> error "internal error (schemaFromPipeline)" | 41 | Nothing -> error "internal error (schemaFromPipeline)" |
diff --git a/Backend/GL/Util.hs b/Backend/GL/Util.hs index 7a1adb0..75c2e3a 100644 --- a/Backend/GL/Util.hs +++ b/Backend/GL/Util.hs | |||
@@ -133,7 +133,8 @@ setUniform i ty ref = do | |||
133 | M42F -> glUniformMatrix4x2fv i 1 false (castPtr p) | 133 | M42F -> glUniformMatrix4x2fv i 1 false (castPtr p) |
134 | M43F -> glUniformMatrix4x3fv i 1 false (castPtr p) | 134 | M43F -> glUniformMatrix4x3fv i 1 false (castPtr p) |
135 | M44F -> glUniformMatrix4fv i 1 false (castPtr p) | 135 | M44F -> glUniformMatrix4fv i 1 false (castPtr p) |
136 | _ -> fail "internal error (setUniform)!" | 136 | FTexture2D -> return () --putStrLn $ "TODO: setUniform FTexture2D" |
137 | _ -> fail $ "internal error (setUniform)! - " ++ show ty | ||
137 | 138 | ||
138 | -- attribute functions | 139 | -- attribute functions |
139 | queryStreams :: GLuint -> IO (Trie GLuint, Trie InputType) | 140 | queryStreams :: GLuint -> IO (Trie GLuint, Trie InputType) |