diff options
author | Joe Crayne <joe@jerkface.net> | 2019-07-31 11:24:27 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2020-01-13 23:59:35 -0500 |
commit | 7cad9f6b02e2f71803047235622b9157ff988e75 (patch) | |
tree | 36c31805b96ebdc7dd915fc4ce50cfa33f3e5118 | |
parent | 85fb0dd752a8911e8fa14287ab9f8673fd6ffda4 (diff) |
faster file loading.
-rw-r--r-- | ByteStringUtil.hs | 29 | ||||
-rw-r--r-- | CubeMap.hs | 3 | ||||
-rw-r--r-- | LoadMesh.hs | 3 | ||||
-rw-r--r-- | lambda-gtk.cabal | 3 |
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 @@ | |||
1 | module ByteStringUtil where | ||
2 | |||
3 | import Data.ByteString.Lazy.Internal | ||
4 | import Data.ByteString.Lazy as L | ||
5 | import qualified Data.ByteString as S | ||
6 | import System.IO | ||
7 | import System.IO.Unsafe | ||
8 | |||
9 | oneMeg :: Int | ||
10 | oneMeg = 1048576 | ||
11 | |||
12 | hGetContentsN :: Int -> Handle -> IO ByteString | ||
13 | hGetContentsN 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 | |||
25 | readBigFile :: FilePath -> IO ByteString | ||
26 | readBigFile fname = do | ||
27 | h <- openFile fname ReadMode | ||
28 | hGetContentsN oneMeg h | ||
29 | |||
@@ -4,6 +4,7 @@ module CubeMap | |||
4 | , cubeMesh | 4 | , cubeMesh |
5 | ) where | 5 | ) where |
6 | 6 | ||
7 | import ByteStringUtil | ||
7 | import LambdaCube.GL as LC | 8 | import LambdaCube.GL as LC |
8 | import LambdaCube.GL.Data (uploadCubeMapToGPU) | 9 | import LambdaCube.GL.Data (uploadCubeMapToGPU) |
9 | import LambdaCube.GL.Mesh as LC | 10 | import 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 #-} |
6 | module LoadMesh where | 6 | module LoadMesh where |
7 | 7 | ||
8 | import ByteStringUtil | ||
8 | import LambdaCube.GL as LC -- renderer | 9 | import LambdaCube.GL as LC -- renderer |
9 | import LambdaCube.GL.Mesh as LambdaCubeGL | 10 | import LambdaCube.GL.Mesh as LambdaCubeGL |
10 | import LambdaCube.GL.Type as LC | 11 | import LambdaCube.GL.Type as LC |
@@ -54,7 +55,7 @@ relativeFrom path file | isAbsolute file = file | |||
54 | relativeFrom path file = takeDirectory path </> file | 55 | relativeFrom path file = takeDirectory path </> file |
55 | 56 | ||
56 | loadOBJ :: String -> IO (Either String MeshData) | 57 | loadOBJ :: String -> IO (Either String MeshData) |
57 | loadOBJ fname = L.readFile fname >>= \bs -> do | 58 | loadOBJ 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, |