on master help sign in
paranoid save
commitc54cd5972eaa2867a0954010e7bd05882cf42223 authorJohn P Mayer Jr <john.p.mayer.jr@gmail.com> dateTue 3 Dec 2013, 00:19 (13 years ago) parent7aa4a5a
6 files changed+78−80
compiler/Generate/JavaScript.hs+6 compiler/Parse/Helpers.hs+12−3 compiler/Type/Constrain/Expression.hs+15−7 compiler/Type/Environment.hs+2−2 libraries/Graphics/WebGL.elm+9−14 libraries/Native/Graphics/WebGL.js+34−54
compiler/Generate/JavaScript.hs+6
@@ -1,3 +1,5 @@
1+{-# OPTIONS -Wall #-}
2+
13module Generate.JavaScript where
24
35import Control.Arrow (first,(***))
@@ -195,6 +197,10 @@ expression (L span expr) =
195197 pad = "<div style=\"height:0;width:0;\">&nbsp;</div>"
196198 md = pad ++ MD.toHtml doc ++ pad
197199
200+ GLShader uid src ->
201+ return . literal $ Str src
202+
203+
198204definition :: Def () () -> State Int [Statement ()]
199205definition def =
200206 case def of
compiler/Parse/Helpers.hs+12−3
@@ -329,10 +329,15 @@ glShader = try (string "[glShader|") >> closeShader "" where
329329 ]
330330
331331data GLTipe = V3 | M4
332+
333+glTipeName :: GLTipe -> String
334+glTipeName V3 = "MJS.V3"
335+glTipeName M4 = "MJS.M4x4"
336+
332337data GLShaderDecls = GLShaderDecls
333338 { attribute :: Map String GLTipe
334339 , uniform :: Map String GLTipe
335− , variying :: Map String GLTipe
340+ , varying :: Map String GLTipe
336341 }
337342
338343--glSource :: String -> Maybe [(StorageQualifier, GLTipe, String)]
@@ -340,11 +345,15 @@ glSource :: String -> Maybe GLShaderDecls
340345glSource src =
341346 case GLP.parse src of
342347 Right (TranslationUnit decls) ->
343− Just . foldl addGLinput emptyDecls . join . map extractGLinputs $ decls
348+ Just . foldr addGLinput emptyDecls . join . map extractGLinputs $ decls
344349 Left e -> Nothing
345350 where
346351 emptyDecls = GLShaderDecls Map.empty Map.empty Map.empty
347− addGLinput = error "TODO"
352+ addGLinput (qual,tipe,name) glDecls = case qual of
353+ Attribute -> glDecls { attribute = Map.insert name tipe $ attribute glDecls }
354+ Uniform -> glDecls { uniform = Map.insert name tipe $ uniform glDecls }
355+ Varying -> glDecls { varying = Map.insert name tipe $ varying glDecls }
356+ _ -> error "Should never happen due to below filter"
348357 extractGLinputs decl = case decl of
349358 Declaration (InitDeclaration (TypeDeclarator (FullType (Just (TypeQualSto qual)) (TypeSpec _prec (TypeSpecNoPrecision tipe _mexpr1)))) [InitDecl name _mexpr2 _mexpr3]) ->
350359 if elem qual [Attribute, Varying, Uniform]
compiler/Type/Constrain/Expression.hs+15−7
@@ -13,7 +13,6 @@ import Control.Monad.Error
1313import Control.Monad.State
1414import Data.Traversable (traverse)
1515import qualified Text.PrettyPrint as PP
16−
1716import qualified Parse.Helpers as PH
1817import SourceSyntax.Location as Loc
1918import SourceSyntax.Pattern (Pattern(PVar))
@@ -39,12 +38,21 @@ constrain env (L span expr) tipe =
3938 case expr of
4039 Literal lit -> liftIO $ Literal.constrain env span lit tipe
4140
42− GLShader _ src ->
43− return . L span $ CEqual tipe (glTipe sourceTipe) where
44− glTipe t = Env.get env Env.types "GLShader" <| t
45− sourceTipe = declsRecord $ PH.glSource src
46− declsRecord :: Maybe PH.GLShaderDecls -> TermN Variable
47− declsRecord = error "TODO"
41+ GLShader _ src -> case declsRecord <$> PH.glSource src of
42+ Nothing -> throwError [PP.text "Some sort of GLSL parse error"]
43+ Just sourceTipe -> return . L span $ CEqual tipe (shaderTipe sourceTipe)
44+ where
45+ shaderTipe t = Env.get env Env.types "Graphics.WebGL.GLShader" <| t
46+ glTipe = Env.get env Env.types . PH.glTipeName
47+ declsRecord :: PH.GLShaderDecls -> TermN Variable
48+ declsRecord extractedTipes = record (fields' extractedTipes) (TermN EmptyRecord1)
49+ fields' :: PH.GLShaderDecls -> Map.Map String [Type]
50+ fields' extractedTipes = Map.fromList
51+ [ ("attribute", [record (Map.map (\t -> [glTipe t]) $ PH.attribute extractedTipes) (TermN EmptyRecord1) ] )
52+ , ("uniform", [record (Map.map (\t -> [glTipe t]) $ PH.uniform extractedTipes) (TermN EmptyRecord1) ] )
53+ , ("varying", [record (Map.map (\t -> [glTipe t]) $ PH.varying extractedTipes) (TermN EmptyRecord1) ] )
54+ ]
55+
4856
4957 Var name | name == saveEnvName -> return (L span CSaveEnv)
5058 | otherwise -> return (name <? tipe)
compiler/Type/Environment.hs+2−2
@@ -54,8 +54,8 @@ makeTypes datatypes =
5454
5555 builtins :: [(String,Int)]
5656 builtins = concat [ map tuple [0..9]
57− , kind 1 ["_List","GLShader"]
58− , kind 0 ["Int","Float","Char","String","Bool","M4x4","V3"]
57+ , kind 1 ["_List"]
58+ , kind 0 ["Int","Float","Char","String","Bool"]
5959 ]
6060
6161
libraries/Graphics/WebGL.elm+9−14
@@ -1,4 +1,4 @@
1−module Graphics.WebGL (makeGL, Vertex, bufferMesh, transform, mesh, webgl) where
1+module Graphics.WebGL (makeGL, linkProgram, linkModel, webgl) where
22
33{-| WebGL
44
@@ -16,22 +16,17 @@ data GLContext = Fake_Context
1616makeGL : Int -> Int -> Signal GLContext
1717makeGL = Native.Graphics.WebGL.makeGL
1818
19−type Vertex = { pos: V3, color: Color }
20−type Triangle = (Vertex,Vertex,Vertex)
19+data GLShader declarations = Fake_GLShader
20+data GLProgram attributes = Fake_Program
2121
22−data Mesh = Fake_Mesh
22+linkProgram : GLShader { attribute : a, uniform : u, varying : v } -> GLShader { attribute : {}, uniform : {}, varying : v } -> GLContext -> GLProgram { attribute : a, uniform : u }
23+linkProgram = Native.Graphics.WebGL.linkProgram
2324
24−bufferMesh : [Triangle] -> GLContext -> Mesh
25−bufferMesh = Native.Graphics.WebGL.bufferMesh
25+data GLModel = Fake_Model
2626
27−data Scene = SceneNode M4x4 [Scene] | SceneLeaf Mesh
27+linkModel : (GLProgram { attribute : a, uniform : u }) -> u -> [a] -> GLModel
28+linkModel = Native.Graphics.WebGL.linkModel
2829
29−transform : M4x4 -> [Scene] -> Scene
30−transform = SceneNode
31−
32−mesh : Mesh -> Scene
33−mesh = SceneLeaf
34−
35−webgl : GLContext -> Scene -> Element
30+webgl : GLContext -> [GLModel] -> Element
3631webgl = Native.Graphics.WebGL.webgl
3732
libraries/Native/Graphics/WebGL.js+34−54
@@ -27,51 +27,19 @@ Elm.Native.Graphics.WebGL.make = function(elm) {
2727 return shader;
2828 };
2929
30− function linkProgram(gl, program) {
31− var vshader = createShader(gl, program.vshaderSource, gl.VERTEX_SHADER);
32− var fshader = createShader(gl, program.fshaderSource, gl.FRAGMENT_SHADER);
33− gl.attachShader(program, vshader);
34− gl.attachShader(program, fshader);
35− gl.linkProgram(program);
36− if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
37− throw gl.getProgramInfoLog(program);
38− }
39− }
40−
41− function createProgram(gl, vstr, fstr) {
42− var program = gl.createProgram();
43− var vshader = createShader(gl, vstr, gl.VERTEX_SHADER);
44− var fshader = createShader(gl, fstr, gl.FRAGMENT_SHADER);
30+ function linkProgram(vSrc, fSrc, context) {
31+ var gl = context.gl;
32+ var vshader = createShader(gl, vSrc, gl.VERTEX_SHADER);
33+ var fshader = createShader(gl, fSrc, gl.FRAGMENT_SHADER);
34+ var program = gl.createProgram()
4535 gl.attachShader(program, vshader);
4636 gl.attachShader(program, fshader);
4737 gl.linkProgram(program);
48−
4938 if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
5039 throw gl.getProgramInfoLog(program);
5140 }
52−
5341 return program;
54− };
55−
56− var geoVert = "attribute vec3 aVertexPosition;\
57− attribute vec4 aVertexColor;\
58− \
59− uniform mat4 uSceneMatrix;\
60− \
61− varying vec4 vVertexColor;\
62− \
63− void main() {\
64− gl_Position = uSceneMatrix * vec4(aVertexPosition, 1.0);\
65− vVertexColor = aVertexColor;\
66− }"
67−
68− var geoFrag = "precision mediump float;\
69− \
70− varying vec4 vVertexColor;\
71− \
72− void main() {\
73− gl_FragColor = vVertexColor;\
74− }"
42+ }
7543
7644 function drawGL(model) {
7745 var aspect = model.aspect;
@@ -117,8 +85,21 @@ Elm.Native.Graphics.WebGL.make = function(elm) {
11785 }
11886 }
11987
120− var scene = model.scene;
121− drawScene(scene,0);
88+ function drawModel(glModel) {
89+ var program = glModel.program;
90+ var uniforms = glModel.uniforms;
91+ va:q
92+ for (var uName in uniforms) {
93+ var uLoc = gl.getUniformLocation(program, uName);
94+ var active = gl.getActiveUniform(program, uName);
95+ }
96+
97+
98+ var attributes = glModel.attributes;
99+
100+ }
101+
102+ List.each(drawModel, model.glModels);
122103
123104 }
124105
@@ -140,17 +121,7 @@ Elm.Native.Graphics.WebGL.make = function(elm) {
140121 function makeGL(w,h) {
141122 var node = newNode('canvas');
142123 var gl = node.getContext('webgl');
143− var programPtr = createProgram(gl, geoVert, geoFrag);
144− var vertexPositionAttribute = gl.getAttribLocation(programPtr,'aVertexPosition');
145− var vertexColorAttribute = gl.getAttribLocation(programPtr,'aVertexColor');
146− var sceneMatrixUniform = gl.getUniformLocation(programPtr, 'uSceneMatrix');
147− var program = {
148− programPtr: programPtr,
149− vertexPositionAttribute: vertexPositionAttribute,
150− vertexColorAttribute: vertexColorAttribute,
151− sceneMatrixUniform: sceneMatrixUniform,
152− };
153− return Signal.constant({w:w,h:h,gl:gl,node:node,program:program});
124+ return Signal.constant({w:w,h:h,gl:gl,node:node});
154125 }
155126
156127 function bufferMesh(triangles, context) {
@@ -200,7 +171,7 @@ Elm.Native.Graphics.WebGL.make = function(elm) {
200171
201172 }
202173
203− function webgl(context, scene) {
174+ function webgl(context, glModels) {
204175
205176 return A3(newElement, context.w, context.h, {
206177 ctor: 'Custom',
@@ -211,17 +182,26 @@ Elm.Native.Graphics.WebGL.make = function(elm) {
211182 aspect: context.w/context.h,
212183 node: context.node,
213184 gl: context.gl,
214− program: context.program,
215− scene: scene,
185+ glModels: glModels,
216186 },
217187 });
218188
219189 }
220190
191+ function linkModel(program, uniforms, attributes) {
192+ return {
193+ program: program,
194+ uniforms: uniforms,
195+ attributes: attributes,
196+ };
197+ }
198+
221199 return elm.Native.Graphics.WebGL.values = {
222200 makeGL:F2(makeGL),
223201 bufferMesh:F2(bufferMesh),
224202 webgl:F2(webgl),
203+ linkProgram:F3(linkProgram),
204+ linkModel:F3(linkModel),
225205 };
226206
227207};