summaryrefslogtreecommitdiff
path: root/backendtest/EditorExamplesTest.hs
blob: a4434d2db5b4664cc2ea7ea417fac02dd50a9d8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
{-# LANGUAGE LambdaCase #-}
module EditorExamplesTest (getRenderJob) where

import Control.Monad
import qualified Data.Vector as V
import qualified Data.Map as Map
import qualified Data.ByteString as BS
import qualified Data.ByteString.Base64 as B64
import Data.ByteString.Char8 (unpack)
import System.FilePath
import System.Directory

import Data.Aeson
import Data.Vect

import TestData as TD
import LambdaCube.Linear
import LambdaCube.IR
import LambdaCube.PipelineSchema
import LambdaCube.PipelineSchemaUtil
import LambdaCube.Mesh

import LambdaCube.Compiler as LambdaCube -- compiler

{-
  ../test-data/editor-examples

  let inputSchema = 
        { slots : fromArray [ Tuple "stream4" {primitive: Triangles, attributes: fromArray [Tuple "position4" TV4F, Tuple "vertexUV" TV2F]}
                            ]
        , uniforms : fromArray
          +[ Tuple "MVP" M44F
          +, Tuple "Time" Float
          +, Tuple "Diffuse" FTexture2D
          ]
        }
-}

inputSchema = makeSchema $ do
  defObjectArray "stream4" Triangles $ do
    "position4" @: Attribute_V4F
    "vertexUV"  @: Attribute_V2F
  defUniforms $ do
    "Time"    @: Float
    "MVP"     @: M44F
    "Diffuse" @: FTexture2D

frame t m = Frame
  { renderCount   = 10
  , frameUniforms = Map.fromList [("Time",VFloat t), ("MVP",VM44F m)]
  , frameTextures = Map.fromList [("Diffuse",0)]
  }

scene wh = Scene
  { TD.objectArrays     = Map.fromList [("stream4", V.fromList [0])]
  , renderTargetWidth   = wh
  , renderTargetHeight  = wh
  , frames              = V.fromList [frame t (mvp t) | t <- [0..10]]
  }
  where
    mvp t =
      let camPos = Vec3 3.0 1.3 0.3
          camTarget = Vec3 0.0 0.0 0.0
          camUp = Vec3 0.0 1.0 0.0
          near = 0.1
          far = 100.0
          fovDeg = 30.0

          angle = pi / 24.0 * t

          cm = fromProjective $ lookat camPos camTarget camUp
          mm = fromProjective $ orthogonal $ toOrthoUnsafe $ rotMatrixY angle
          pm = perspective near far (fovDeg / 180 * pi) (fromIntegral wh / fromIntegral wh)
      in mat4ToM44F $ mm .*. cm .*. pm

getRenderJob = do
  let path = "./testdata/editor-examples"
  tests <- filter ((".lc" ==) . takeExtension) <$> getDirectoryContents path
  print tests
  ppls <- forM tests $ \name -> do
    putStrLn $ "compile: " ++ name
    LambdaCube.compileMain [path] OpenGL33 name >>= \case
      Left err  -> fail $ "compile error:\n" ++ show err
      Right ppl -> return $ PipelineInfo
        { pipelineName = path </> name
        , pipeline = ppl
        }

  img <- unpack . B64.encode <$> BS.readFile "./backend-test-data/editor/logo256x256.png"

  let job = RenderJob
        { meshes      = V.fromList [cubeMesh]
        , TD.textures = V.fromList [img]
        , schema      = inputSchema
        , scenes      = V.fromList [scene 64]
        , pipelines   = V.fromList ppls
        }
  return ("editor",job)

g_vertex_buffer_data =
    [ V4   1.0    1.0  (-1.0) 1.0
    , V4   1.0  (-1.0) (-1.0) 1.0
    , V4 (-1.0) (-1.0) (-1.0) 1.0

    , V4   1.0    1.0  (-1.0) 1.0
    , V4 (-1.0) (-1.0) (-1.0) 1.0
    , V4 (-1.0)   1.0  (-1.0) 1.0

    , V4   1.0    1.0  (-1.0) 1.0
    , V4   1.0    1.0    1.0  1.0
    , V4   1.0  (-1.0)   1.0  1.0

    , V4   1.0    1.0  (-1.0) 1.0
    , V4   1.0  (-1.0)   1.0  1.0
    , V4   1.0  (-1.0) (-1.0) 1.0

    , V4   1.0    1.0    1.0  1.0
    , V4 (-1.0) (-1.0)   1.0  1.0
    , V4   1.0  (-1.0)   1.0  1.0

    , V4   1.0    1.0    1.0  1.0
    , V4 (-1.0)   1.0    1.0  1.0
    , V4 (-1.0) (-1.0)   1.0  1.0

    , V4 (-1.0)   1.0    1.0  1.0
    , V4 (-1.0) (-1.0) (-1.0) 1.0
    , V4 (-1.0) (-1.0)   1.0  1.0

    , V4 (-1.0)   1.0    1.0  1.0
    , V4 (-1.0)   1.0  (-1.0) 1.0
    , V4 (-1.0) (-1.0) (-1.0) 1.0

    , V4   1.0    1.0  (-1.0) 1.0
    , V4 (-1.0)   1.0  (-1.0) 1.0
    , V4 (-1.0)   1.0    1.0  1.0

    , V4   1.0    1.0  (-1.0) 1.0
    , V4 (-1.0)   1.0    1.0  1.0
    , V4   1.0    1.0    1.0  1.0

    , V4   1.0    (-1.0)  (-1.0) 1.0
    , V4   1.0    (-1.0)    1.0  1.0
    , V4 (-1.0)   (-1.0)    1.0  1.0

    , V4   1.0    (-1.0)  (-1.0) 1.0
    , V4 (-1.0)   (-1.0)    1.0  1.0
    , V4 (-1.0)   (-1.0)  (-1.0) 1.0
    ]

--  Two UV coordinatesfor each vertex. They were created with Blender.
g_uv_buffer_data =
    [ V2 0.0 1.0
    , V2 0.0 0.0
    , V2 1.0 0.0
    , V2 0.0 1.0
    , V2 1.0 0.0
    , V2 1.0 1.0
    , V2 0.0 1.0
    , V2 1.0 1.0
    , V2 1.0 0.0
    , V2 0.0 1.0
    , V2 1.0 0.0
    , V2 0.0 0.0
    , V2 1.0 1.0
    , V2 0.0 0.0
    , V2 1.0 0.0
    , V2 1.0 1.0
    , V2 0.0 1.0
    , V2 0.0 0.0
    , V2 0.0 1.0
    , V2 1.0 0.0
    , V2 0.0 0.0
    , V2 0.0 1.0
    , V2 1.0 1.0
    , V2 1.0 0.0
    , V2 0.0 1.0
    , V2 1.0 1.0
    , V2 1.0 0.0
    , V2 0.0 1.0
    , V2 1.0 0.0
    , V2 0.0 0.0
    , V2 0.0 1.0
    , V2 0.0 0.0
    , V2 1.0 0.0
    , V2 0.0 1.0
    , V2 1.0 0.0
    , V2 1.0 1.0
    ]

cubeMesh = Mesh
  { mAttributes = Map.fromList
        [ ("position4", A_V4F $ V.fromList g_vertex_buffer_data)
        , ("vertexUV",  A_V2F $ V.fromList g_uv_buffer_data)
        ]
  , mPrimitive = P_Triangles
  }

vec4ToV4F (Vec4 x y z w) = V4 x y z w
mat4ToM44F (Mat4 a b c d) = V4 (vec4ToV4F a) (vec4ToV4F b) (vec4ToV4F c) (vec4ToV4F d)

-- | Camera transformation matrix.
lookat :: Vec3   -- ^ Camera position.
       -> Vec3   -- ^ Target position.
       -> Vec3   -- ^ Upward direction.
       -> Proj4
lookat pos target up = translateBefore4 (neg pos) (orthogonal $ toOrthoUnsafe r)
  where
    w = normalize $ pos &- target
    u = normalize $ up &^ w
    v = w &^ u
    r = transpose $ Mat3 u v w

-- | Perspective transformation matrix in row major order.
perspective :: Float  -- ^ Near plane clipping distance (always positive).
            -> Float  -- ^ Far plane clipping distance (always positive).
            -> Float  -- ^ Field of view of the y axis, in radians.
            -> Float  -- ^ Aspect ratio, i.e. screen's width\/height.
            -> Mat4
perspective n f fovy aspect = transpose $
    Mat4 (Vec4 (2*n/(r-l))       0       (-(r+l)/(r-l))        0)
         (Vec4     0        (2*n/(t-b))  ((t+b)/(t-b))         0)
         (Vec4     0             0       (-(f+n)/(f-n))  (-2*f*n/(f-n)))
         (Vec4     0             0            (-1)             0)
  where
    t = n*tan(fovy/2)
    b = -t
    r = aspect*t
    l = -r