summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Crayne <joe@jerkface.net>2019-07-31 11:24:27 -0400
committerJoe Crayne <joe@jerkface.net>2020-01-13 23:59:35 -0500
commit7cad9f6b02e2f71803047235622b9157ff988e75 (patch)
tree36c31805b96ebdc7dd915fc4ce50cfa33f3e5118
parent85fb0dd752a8911e8fa14287ab9f8673fd6ffda4 (diff)
faster file loading.
-rw-r--r--ByteStringUtil.hs29
-rw-r--r--CubeMap.hs3
-rw-r--r--LoadMesh.hs3
-rw-r--r--lambda-gtk.cabal3
4 files changed, 35 insertions, 3 deletions
diff --git a/ByteStringUtil.hs b/ByteStringUtil.hs
new file mode 100644
index 0000000..9711e0b
--- /dev/null
+++ b/ByteStringUtil.hs
@@ -0,0 +1,29 @@
1module ByteStringUtil where
2
3import Data.ByteString.Lazy.Internal
4import Data.ByteString.Lazy as L
5import qualified Data.ByteString as S
6import System.IO
7import System.IO.Unsafe
8
9oneMeg :: Int
10oneMeg = 1048576
11
12hGetContentsN :: Int -> Handle -> IO ByteString
13hGetContentsN kk h = lazyRead
14 where
15 k = kk - chunkOverhead
16 lazyRead = unsafeInterleaveIO loop
17
18 loop = do
19 c <- S.hGetSome h k -- only blocks if there is no data available
20 if S.null c
21 then hClose h >> return Empty
22 else do cs <- lazyRead
23 return (Chunk c cs)
24
25readBigFile :: FilePath -> IO ByteString
26readBigFile fname = do
27 h <- openFile fname ReadMode
28 hGetContentsN oneMeg h
29
diff --git a/CubeMap.hs b/CubeMap.hs
index e71892a..864bb57 100644
--- a/CubeMap.hs
+++ b/CubeMap.hs
@@ -4,6 +4,7 @@ module CubeMap
4 , cubeMesh 4 , cubeMesh
5 ) where 5 ) where
6 6
7import ByteStringUtil
7import LambdaCube.GL as LC 8import LambdaCube.GL as LC
8import LambdaCube.GL.Data (uploadCubeMapToGPU) 9import LambdaCube.GL.Data (uploadCubeMapToGPU)
9import LambdaCube.GL.Mesh as LC 10import LambdaCube.GL.Mesh as LC
@@ -70,7 +71,7 @@ loadSkyboxes = do
70 , skyboxLoad = \n -> do 71 , skyboxLoad = \n -> do
71 let fn = zips !! mod n len 72 let fn = zips !! mod n len
72 putStrLn $ "Loading skybox " ++ show n ++ ": " ++ fn ++ "..." 73 putStrLn $ "Loading skybox " ++ show n ++ ": " ++ fn ++ "..."
73 archive <- toArchive <$> Lazy.readFile (dir </> fn) 74 archive <- toArchive <$> readBigFile (dir </> fn)
74 let es = mapMaybe (`findEntryByPath` archive) $ filterImageNames (filesInArchive archive) 75 let es = mapMaybe (`findEntryByPath` archive) $ filterImageNames (filesInArchive archive)
75 imgs <- fmap sequence $ forM es $ \entry -> do 76 imgs <- fmap sequence $ forM es $ \entry -> do
76 return $ Juicy.decodeImage $ Lazy.toStrict $ fromEntry entry 77 return $ Juicy.decodeImage $ Lazy.toStrict $ fromEntry entry
diff --git a/LoadMesh.hs b/LoadMesh.hs
index 423630f..32e4f84 100644
--- a/LoadMesh.hs
+++ b/LoadMesh.hs
@@ -5,6 +5,7 @@
5{-# LANGUAGE TupleSections #-} 5{-# LANGUAGE TupleSections #-}
6module LoadMesh where 6module LoadMesh where
7 7
8import ByteStringUtil
8import LambdaCube.GL as LC -- renderer 9import LambdaCube.GL as LC -- renderer
9import LambdaCube.GL.Mesh as LambdaCubeGL 10import LambdaCube.GL.Mesh as LambdaCubeGL
10import LambdaCube.GL.Type as LC 11import LambdaCube.GL.Type as LC
@@ -54,7 +55,7 @@ relativeFrom path file | isAbsolute file = file
54relativeFrom path file = takeDirectory path </> file 55relativeFrom path file = takeDirectory path </> file
55 56
56loadOBJ :: String -> IO (Either String MeshData) 57loadOBJ :: String -> IO (Either String MeshData)
57loadOBJ fname = L.readFile fname >>= \bs -> do 58loadOBJ fname = readBigFile fname >>= \bs -> do
58 let obj@OBJ{..} = Wavefront.parse bs 59 let obj@OBJ{..} = Wavefront.parse bs
59 -- load materials 60 -- load materials
60 mtlLib <- if V.null objMtlLibs 61 mtlLib <- if V.null objMtlLibs
diff --git a/lambda-gtk.cabal b/lambda-gtk.cabal
index da4c751..d262740 100644
--- a/lambda-gtk.cabal
+++ b/lambda-gtk.cabal
@@ -50,7 +50,8 @@ executable meshsketch
50 other-modules: InfinitePlane LambdaCubeWidget GLWidget LambdaCube.Gtk TimeKeeper 50 other-modules: InfinitePlane LambdaCubeWidget GLWidget LambdaCube.Gtk TimeKeeper
51 LoadMesh MtlParser Matrix LambdaCube.GL.HMatrix 51 LoadMesh MtlParser Matrix LambdaCube.GL.HMatrix
52 Animator MeshSketch CubeMap AttributeData GPURing MaskableStream 52 Animator MeshSketch CubeMap AttributeData GPURing MaskableStream
53 RingBuffer SmallRing VectorRing Camera Bezier FitCurves Mask Data.List.Merge 53 RingBuffer SmallRing VectorRing Camera Bezier FitCurves Mask
54 Data.List.Merge ByteStringUtil
54 extensions: NondecreasingIndentation 55 extensions: NondecreasingIndentation
55 other-extensions: OverloadedLabels, OverloadedLists, OverloadedStrings 56 other-extensions: OverloadedLabels, OverloadedLists, OverloadedStrings
56 build-depends: base, containers >=0.5 && <0.6, bytestring >=0.10 && <0.11, 57 build-depends: base, containers >=0.5 && <0.6, bytestring >=0.10 && <0.11,