Home My Page Projects Code Snippets Project Openings 3D graphics for Standard ML
 Summary Activity SCM

# SCM Repository

[sml3d] Diff of /trunk/sml3d/src/particles/compiler/translate.sml
 [sml3d] / trunk / sml3d / src / particles / compiler / translate.sml

# Diff of /trunk/sml3d/src/particles/compiler/translate.sml

revision 1119, Mon Apr 11 17:09:46 2011 UTC revision 1120, Wed Apr 13 05:25:17 2011 UTC
# Line 337  Line 337
337     * We set the boolean to whether or not the current particle given by the particle     * We set the boolean to whether or not the current particle given by the particle
338     * state is within the domain, and then pass the continuation on.     * state is within the domain, and then pass the continuation on.
339     *)     *)
340      fun mkWithinVar (boolVar, env, var, d, stmt : IR.var -> IR.stmt) = let      fun mkVecWithinVar (boolVar, env, var, d : Vec3f.vec3 P.domain, stmt : IR.var -> IR.stmt) = let
341            val pos = var            val pos = var
342            in            in
343              case d              case d
# Line 463  Line 463
463                | P.D_BLOB {center: vec3f var, stddev: float var}                | P.D_BLOB {center: vec3f var, stddev: float var}
464                | P.D_DISC {pt: vec3f var, normal: vec3f var, irad: float var, orad: float var}                | P.D_DISC {pt: vec3f var, normal: vec3f var, irad: float var, orad: float var}
465  *)  *)
466                | _ => raise Fail "Cannot determine within-ness for specified domain."                | _ => raise Fail "Cannot determine within-ness for specified vec3 domain."
467              (* end case *)              (* end case *)
468            end (*end let *)            end (*end let *)
469
470            fun mkFloatWithinVar (boolVar, env, var, d : Float.float P.domain, stmt : IR.var -> IR.stmt) = (case d
471              of P.D_POINT(pt) => letPRIM(boolVar, IR.T_BOOL, IR.EQUALS, [psvToIRVar(env, pt), var], stmt)
472               | P.D_BOX {min, max} =>
473                 letPRIM("bigMin", IR.T_BOOL, IR.GT, [var, psvToIRVar(env, min)], fn bigMin =>
474                 letPRIM("smallMax", IR.T_BOOL, IR.GT, [psvToIRVar(env, max), var], fn smallMax =>
475                 letPRIM(boolVar, IR.T_BOOL, IR.AND, [bigMin, smallMax], stmt)))
476               | _ => raise Fail "Cannot determine within-ness for specified float domain."
477             (* end case *))
478
479          fun mkIntBool(env, p1var, p2var, d : Vec3f.vec3 P.domain, k : IR.var -> IR.stmt) = let          fun mkIntBool(env, p1var, p2var, d : Vec3f.vec3 P.domain, k : IR.var -> IR.stmt) = let
480            val _ = ()            val _ = ()
481           in           in
# Line 521  Line 530
530            of P.D_PLANE{pt, normal} => letPRIM(retNorm, IR.T_VEC, IR.COPY, [psvToIRVar(env, normal)],            of P.D_PLANE{pt, normal} => letPRIM(retNorm, IR.T_VEC, IR.COPY, [psvToIRVar(env, normal)],
531                fn newNormVar => gotoWithArgs(state, [newNormVar], nextBlk))                fn newNormVar => gotoWithArgs(state, [newNormVar], nextBlk))
533                mkWithinVar("inP", env, pos, d, fn inPlane =>                mkVecWithinVar("inP", env, pos, d, fn inPlane =>
534                    IR.mkIF(inPlane,                    IR.mkIF(inPlane,
535                      (* then *)                      (* then *)
536                      letPRIM(retNorm, IR.T_VEC, IR.COPY, [psvToIRVar(env, normal)],                      letPRIM(retNorm, IR.T_VEC, IR.COPY, [psvToIRVar(env, normal)],
# Line 732  Line 741
741       end       end
742
743  (*  (*
744  //    fun trPred(pred, env, state, thenk : particle_state -> IR.stmt, elsek : particle_state -> IR.stmt) = let  //
745  //      val PS{pos, vel, ...} = state      fun trPred(pred, env, state, thenk : particle_state -> IR.stmt, elsek : particle_state -> IR.stmt) = let
746  //      val P.PR{ifstmt, ...} = pred        val P.PR{ifstmt, ...} = pred
747  //    in      in
748  //      case ifstmt        case ifstmt
749  //       of P.WITHIN(d) => mkWithinVar("wv", env, pos, d, fn withinVar =>         of P.WITHIN(d) => mkWithinVar("wv", env, pos, d, fn withinVar =>
750  //               IR.mkIF(withinVar, thenk(state), elsek(state)))                 IR.mkIF(withinVar, thenk(state), elsek(state)))
751  //         | P.WITHINVEL(d) => mkWithinVar("wv", env, vel, d, fn withinVar =>           | P.WITHINVEL(d) => mkWithinVar("wv", env, vel, d, fn withinVar =>
752  //               IR.mkIF(withinVar, thenk(state), elsek(state)))                 IR.mkIF(withinVar, thenk(state), elsek(state)))
753  //     end            /       end
754  //  //
755  //    fun trAct (action, env, state, k : particle_state -> IR.stmt) = let/  //    fun trAct (action, env, state, k : particle_state -> IR.stmt) = let/
756  //        val PS{pos, vel, size, ttl, color, user} = state  //        val PS{pos, vel, size, ttl, color, user} = state
# Line 860  Line 869
869  //          (* end case *)  //          (* end case *)
870  //        end  //        end
871  *)  *)
872
873        (* trExpr(expr, env, state, k : IR.var -> IR.stmt) *)
874        (* mkFloatWithinVar (boolVar, env, var, d : Float.float P.domain, stmt : IR.var -> IR.stmt) *)
875        fun trPred(cond, env, state, thenk : particle_state -> IR.stmt, elsek : particle_state -> IR.stmt) = let
876          fun grabVar(cond, env, state, k : IR.var -> IR.stmt) = (case cond
877            of P.WITHINF(d, expr) =>
878                trExpr(expr, env, state, fn checkMe =>
879                mkFloatWithinVar("wv", env, checkMe, d, k))
880
881             | P.WITHIN3F(d, expr) =>
882                trExpr(expr, env, state, fn checkMe =>
883                mkVecWithinVar("wv", env, checkMe, d, k))
884
885             | P.DO_INTERSECT {p1, p2, d} =>
886               trExpr(p1, env, state, fn p1var =>
887               trExpr(p2, env, state, fn p2var =>
888               mkIntBool(env, p1var, p2var, d, k)))
889
890             | P.GTHAN (e1, e2) =>
891               trExpr(e1, env, state, fn e1var =>
892               trExpr(e2, env, state, fn e2var =>
893               letPRIM("gtVar", IR.T_BOOL, IR.GT, [e1var, e2var], k)))
894
895             | P.BOOLVAR (b) => k (psvToIRVar(env, b))
896
897             | P.AND(c1, c2) =>
898               grabVar(c1, env, state, fn c1Var =>
899               grabVar(c2, env, state, fn c2Var =>
900               letPRIM("andVar", IR.T_BOOL, IR.AND, [c1Var, c2Var], k)))
901
902             | P.OR(c1, c2) =>
903               grabVar(c1, env, state, fn c1Var =>
904               grabVar(c2, env, state, fn c2Var =>
905               letPRIM("andVar", IR.T_BOOL, IR.OR, [c1Var, c2Var], k)))
906
907             | P.XOR(c1, c2) =>
908               grabVar(c1, env, state, fn c1Var =>
909               grabVar(c2, env, state, fn c2Var =>
910               mkXOR ("xorVar", c1Var, c2Var, k)))
911
912             | P.NOT(c) =>
913               grabVar(c, env, state, fn cvar =>
914               letPRIM("notVar", IR.T_BOOL, IR.NOT, [cvar], k))
915
916            (* end case *))
917         in
918          grabVar(cond, env, state, fn result =>
919          IR.mkIF(result, thenk(state), elsek(state)))
920         end
921
922      fun compile (P.PG{      fun compile (P.PG{
923         emit as P.EMIT{freq, sv_gens}, act, render,         emit as P.EMIT{freq, sv_gens}, act, render,
924         vars, state_vars, render_vars         vars, state_vars, render_vars
925      }) = let      }) = let
926        val blks = ref[]        val blks = ref[]
927          fun convertToIR (PSV.SV{name, ty, id, ...}) = IR.newParam("ps_" ^ name, IR.psvTyToIRTy ty)
928        val env = let        val env = let
929                (* add special globals to free vars *)                (* add special globals to free vars *)
930          val pgm_vars = PSV.Set.union(PSV.Set.singleton epsilon, vars)          val pgm_vars = PSV.Set.union(PSV.Set.singleton epsilon, vars)
# Line 883  Line 943
943                     in                     in
944                          PSV.Map.insert (map, x, x')                          PSV.Map.insert (map, x, x')
945                     end (* ins *)                     end (* ins *)

fun convertToIR PSV.SV{name, ty, id, ...} = IR.newParam("ps_" ^ name, IR.psvTyToIRTy ty)
946                  in                  in
947                    TE( blks, PSV.Set.foldl insv PSV.Map.empty pgm_vars )                    TE( blks, PSV.Set.foldl insv PSV.Map.empty pgm_vars )
948                  end (* env *)                  end (* env *)
949
950        fun evalActs f [] state = f [] state        fun evalActs theAct state f = (case theAct
| evalActs f (psa :: psal) state = (case psa
951                of P.SEQ(acts) => (case acts                of P.SEQ(acts) => (case acts
952                    of [] => f state
953                     | oneAct :: rest => evalActs oneAct state (fn state' => (evalActs (P.SEQ(rest)) state' f))
954                    (* end case *))
955
956                   | P.PRED(cond, thenAct, elseAct) =>
957                     trPred(cond, env, state,
958                       fn state' => evalActs thenAct state' f,
959                       fn state' => evalActs elseAct state' f
960                     )
961
962                   | P.DIE => IR.DISCARD
963
964                   | P.ASSIGN(sv, expr) => let
965                     val PSV.SV{name, ty, ...} = sv
966                     fun replaceStateVar (var, []) = [var]
967                       | replaceStateVar (var, nv :: svars) = let
968                         val IR.V{name=nvname, ...} = nv
969                         val IR.V{name=varname, ...} = var
970                        in
971                         if nvname = varname then
972                           var :: svars
973                         else
974                           nv :: replaceStateVar(var, svars)
975                        end
976                    in
977                     trExpr(expr, env, state, fn newVar =>
978                     letPRIM("ps_" ^ name, IR.psvTyToIRTy ty, IR.COPY, [newVar],
979                       fn thisVar => f (replaceStateVar(thisVar, state))))
980                    end
981
982                  (* end case *))
983    (*
984                  (case acts
985                   of [] => raise Fail "Should never reach here."                   of [] => raise Fail "Should never reach here."
986                    | [act] => trAct(act, env, state, evalActs f psal)                    | [act] => trAct(act, env, state, evalActs f psal)
987                    | act :: rest => trAct(act, env, state, evalActs f (P.SEQ(rest) :: psal))                    | act :: rest => trAct(act, env, state, evalActs f (P.SEQ(rest) :: psal))
# Line 904  Line 994
994                     trPred(pred, env, state, evalActs trPredActs t, evalActs trPredActs e)                     trPred(pred, env, state, evalActs trPredActs t, evalActs trPredActs e)
995                       end                       end
996                (* end case *))                (* end case *))
997    *)
(* At the highest level, we want to return when we reach the end of the action list *)
fun trActs [] state = retState state
| trActs _ _ = raise Fail "Should never reach here"
998
999            (* The entry block is the first block of the program, or in other words, the emitter. *)            (* The entry block is the first block of the program, or in other words, the emitter. *)
1000            val entryBlock = newBlock (            val entryBlock = newBlock (
1001              env,              env,
1002              List.map convertToIR state_vars,              List.map convertToIR (PSV.SVSet.listItems state_vars),
1003              fn pstate => trEmitter(              fn pstate => trEmitter(
1004                emit,                emit,
1005                env,                env,
1006                pstate,                pstate,
1007                fn state => evalActs trActs root_act state                fn state => evalActs act state retState
1008              )              )
1009            )            )
1010

Legend:
 Removed from v.1119 changed lines Added in v.1120

 root@smlnj-gforge.cs.uchicago.edu ViewVC Help Powered by ViewVC 1.0.0