summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKosyrev Serge <_deepfire@feelingofgreen.ru>2018-09-17 19:43:42 +0300
committerKosyrev Serge <_deepfire@feelingofgreen.ru>2018-09-17 19:43:42 +0300
commit176ac6939f635e15bb1f5b63f1b210b46f483419 (patch)
tree3ca74a9768133aeac706eab1ffa93671a981560d
parentfa9acbf78f91a639c0e76c1757564a6446445f39 (diff)
lc/Builtins: allow RenderTextures to be specified as outputs in the IR
-rw-r--r--lc/Builtins.lc1
-rw-r--r--src/LambdaCube/Compiler/CoreToIR.hs34
2 files changed, 34 insertions, 1 deletions
diff --git a/lc/Builtins.lc b/lc/Builtins.lc
index 03970dbd..15840268 100644
--- a/lc/Builtins.lc
+++ b/lc/Builtins.lc
@@ -539,6 +539,7 @@ PrjImageColor :: FrameBuffer 1 '[ 'Depth, 'Color (Vec 4 Float)] -> Image 1
539 539
540data Output where 540data Output where
541 ScreenOut :: FrameBuffer a b -> Output 541 ScreenOut :: FrameBuffer a b -> Output
542 TextureOut :: Vec 2 Int -> FrameBuffer a b -> Output
542 543
543renderFrame = ScreenOut 544renderFrame = ScreenOut
544 545
diff --git a/src/LambdaCube/Compiler/CoreToIR.hs b/src/LambdaCube/Compiler/CoreToIR.hs
index 6646d345..decd171e 100644
--- a/src/LambdaCube/Compiler/CoreToIR.hs
+++ b/src/LambdaCube/Compiler/CoreToIR.hs
@@ -22,6 +22,7 @@ import qualified Data.Map as Map
22import qualified Data.Set as Set 22import qualified Data.Set as Set
23import qualified Data.Vector as Vector 23import qualified Data.Vector as Vector
24import GHC.Stack 24import GHC.Stack
25import GHC.Word
25import Control.Arrow hiding ((<+>)) 26import Control.Arrow hiding ((<+>))
26import Control.Monad.Writer 27import Control.Monad.Writer
27import Control.Monad.State 28import Control.Monad.State
@@ -58,7 +59,12 @@ compilePipeline backend exp = IR.Pipeline
58 ((subCmds,cmds), (streams, programs, targets, slots, textures)) 59 ((subCmds,cmds), (streams, programs, targets, slots, textures))
59 = flip runState ((0, mempty), mempty, (0, mempty), mempty, (0, mempty)) $ case toExp exp of 60 = flip runState ((0, mempty), mempty, (0, mempty), mempty, (0, mempty)) $ case toExp exp of
60 A1 "ScreenOut" a -> addTarget backend a [IR.TargetItem s $ Just $ IR.Framebuffer s | s <- getSemantics a] 61 A1 "ScreenOut" a -> addTarget backend a [IR.TargetItem s $ Just $ IR.Framebuffer s | s <- getSemantics a]
61 x -> error $ "ScreenOut expected inststead of " ++ ppShow x 62 A2 "TextureOut" rtexDimE rtexE -> do
63 let rtexDim = case compValue rtexDimE of
64 IR.VV2I v -> fromIntegral <$> v
65 x -> error "Render texture dimensions should be a pair of non-negative integrals."
66 getTextureRenderTargetCommands backend rtexDim rtexE
67 x -> error $ "ScreenOut or TextureOut expected inststead of " ++ ppShow x
62 68
63type CG = State (List IR.StreamData, Map IR.Program Int, List IR.RenderTarget, Map String (Int, IR.Slot), List IR.TextureDescriptor) 69type CG = State (List IR.StreamData, Map IR.Program Int, List IR.RenderTarget, Map String (Int, IR.Slot), List IR.TextureDescriptor)
64 70
@@ -174,6 +180,7 @@ getCommands backend e = case e of
174 let (a, tf) = case img of 180 let (a, tf) = case img of
175 A1 "PrjImageColor" a -> (,) a $ \[_, x] -> x 181 A1 "PrjImageColor" a -> (,) a $ \[_, x] -> x
176 A1 "PrjImage" a -> (,) a $ \[x] -> x 182 A1 "PrjImage" a -> (,) a $ \[x] -> x
183 x -> error $ "Unexpected image: " <> ppShow x
177 tl <- forM (getSemantics a) $ \semantic -> do 184 tl <- forM (getSemantics a) $ \semantic -> do
178 texture <- addL textureLens IR.TextureDescriptor 185 texture <- addL textureLens IR.TextureDescriptor
179 { IR.textureType = IR.Texture2D (if semantic == IR.Color then IR.FloatT IR.RGBA else IR.FloatT IR.Red) 1 186 { IR.textureType = IR.Texture2D (if semantic == IR.Color then IR.FloatT IR.RGBA else IR.FloatT IR.Red) 1
@@ -200,6 +207,31 @@ getCommands backend e = case e of
200 return ([(n, tx)], subCmds ++ cmds) 207 return ([(n, tx)], subCmds ++ cmds)
201 _ -> return mempty 208 _ -> return mempty
202 209
210getTextureRenderTargetCommands :: IR.Backend -> IR.V2 Word32 -> ExpTV -> CG ([IR.Command],[IR.Command])
211getTextureRenderTargetCommands backend dim body = do
212 targetItems <- forM (getSemantics body) $ \semantic -> do
213 texture <- addL textureLens IR.TextureDescriptor
214 { IR.textureType = IR.Texture2D (if semantic == IR.Color then IR.FloatT IR.RGBA else IR.FloatT IR.Red) 1
215 , IR.textureSize = IR.VV2U dim
216 , IR.textureSemantic = semantic
217 , IR.textureSampler = IR.SamplerDescriptor
218 { IR.samplerWrapS = IR.Repeat
219 , IR.samplerWrapT = Just IR.Repeat
220 , IR.samplerWrapR = Nothing
221 , IR.samplerMinFilter = IR.Nearest
222 , IR.samplerMagFilter = IR.Nearest
223 , IR.samplerBorderColor = IR.VV4F (IR.V4 0 0 0 1)
224 , IR.samplerMinLod = Nothing
225 , IR.samplerMaxLod = Nothing
226 , IR.samplerLodBias = 0
227 , IR.samplerCompareFunc = Nothing
228 }
229 , IR.textureBaseLevel = 0
230 , IR.textureMaxLevel = 0
231 }
232 return $ IR.TargetItem semantic $ Just $ IR.TextureImage texture 0 Nothing
233 addTarget backend body targetItems
234
203type SamplerBinding = (IR.UniformName,IR.ImageRef) 235type SamplerBinding = (IR.UniformName,IR.ImageRef)
204 236
205---------------------------------------------------------------- 237----------------------------------------------------------------