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
ViewVC logotype

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 873, Wed May 5 20:18:00 2010 UTC revision 906, Wed Jun 2 21:13:39 2010 UTC
# Line 26  Line 26 
26          size : IR.var,          (* float *)          size : IR.var,          (* float *)
27          isDead : IR.var,        (* bool *)          isDead : IR.var,        (* bool *)
28          color : IR.var,         (* vec3 (NOTE: should be vector4) *)          color : IR.var,         (* vec3 (NOTE: should be vector4) *)
29            pos2 : IR.var,          (* vec3 *)
30          dummy : IR.var          dummy : IR.var
31        }        }
32    
33    (* special PSV global variables *)    (* special PSV global variables *)
     val timeStep = PSV.new("g_timeStep", PSV.T_FLOAT)   (* physics timestep *)  
     val numDead = PSV.new("g_numDead", PSV.T_INT)       (* # of dead particles *)  
34      val epsilon = PSV.constf(0.00001)      val epsilon = PSV.constf(0.00001)
35    
36    (* constants *)    (* constants *)
# Line 52  Line 51 
51    
52    
53      fun retState s = let      fun retState s = let
54        val PS{pos, vel, size, isDead, color, dummy} = s        val PS{pos, vel, size, isDead, color, pos2, dummy} = s
55       in       in
56        IR.mkRETURN [pos, vel, size, isDead, color, dummy]        IR.mkRETURN (
57            [pos, vel, size, isDead, color, pos2, dummy],
58            [IR.POS, IR.VEL, IR.SZ, IR.ISDEAD, IR.COLOR, IR.POS2, IR.DUMMY]
59          )
60       end       end
61    
62    (* translation environment *)    (* translation environment *)
# Line 75  Line 77 
77            val isDead = IR.newParam ("ps_isDead", IR.T_BOOL)            val isDead = IR.newParam ("ps_isDead", IR.T_BOOL)
78            val color = IR.newParam ("ps_color", IR.T_VEC)            val color = IR.newParam ("ps_color", IR.T_VEC)
79            val dummy = IR.newParam ("ps_dummy", IR.T_FLOAT)            val dummy = IR.newParam ("ps_dummy", IR.T_FLOAT)
80            val state = PS{pos=pos, vel=vel, size=size, isDead=isDead, color=color, dummy=dummy}            val pos2 = IR.newParam ("ps_pos2", IR.T_VEC)
81            val blk = IR.newBlock ([pos, vel, size, isDead, color, dummy], k state)            val state = PS{pos=pos, vel=vel, size=size, isDead=isDead, color=color, pos2=pos2, dummy=dummy}
82              val blk = IR.newBlock ([pos, vel, size, isDead, color, pos2, dummy], k state)
83            in            in
84              blks := blk :: !blks;              blks := blk :: !blks;
85              blk              blk
# Line 89  Line 92 
92            val isDead = IR.newParam ("ps_isDead", IR.T_BOOL)            val isDead = IR.newParam ("ps_isDead", IR.T_BOOL)
93            val color = IR.newParam ("ps_color", IR.T_VEC)            val color = IR.newParam ("ps_color", IR.T_VEC)
94            val dummy = IR.newParam ("ps_dummy", IR.T_FLOAT)            val dummy = IR.newParam ("ps_dummy", IR.T_FLOAT)
95            val state = PS{pos=pos, vel=vel, size=size, isDead=isDead, color=color, dummy = dummy}            val pos2 = IR.newParam ("ps_pos2", IR.T_VEC)
96            val blk = IR.newBlock ([pos, vel, size, isDead, color, dummy] @ args, k state)            val state = PS{pos=pos, vel=vel, size=size, isDead=isDead, color=color, pos2=pos2, dummy = dummy}
97              val blk = IR.newBlock ([pos, vel, size, isDead, color, pos2, dummy] @ args, k state)
98            in            in
99              blks := blk :: !blks;              blks := blk :: !blks;
100              blk              blk
101            end            end
102    
103      fun goto (PS{pos, vel, size, isDead, color, dummy}, blk) =      fun goto (PS{pos, vel, size, isDead, color, pos2, dummy}, blk) =
104            IR.mkGOTO(blk, [pos, vel, size, isDead, color, dummy])            IR.mkGOTO(blk, [pos, vel, size, isDead, color, pos2, dummy])
105    
106          fun gotoWithArgs(PS{pos, vel, size, isDead, color, dummy}, args, blk) =          fun gotoWithArgs(PS{pos, vel, size, isDead, color, pos2, dummy}, args, blk) =
107            IR.mkGOTO(blk, [pos, vel, size, isDead, color, dummy] @ args)            IR.mkGOTO(blk, [pos, vel, size, isDead, color, pos2, dummy] @ args)
108    
109      fun letPRIM (x, ty, p, args, body) = let      fun letPRIM (x, ty, p, args, body) = let
110            val x' = IR.newLocal(x, ty, (p, args))            val x' = IR.newLocal(x, ty, (p, args))
# Line 291  Line 295 
295               * behind it (with respect to the normal)               * behind it (with respect to the normal)
296               *)               *)
297                | P.D_PLANE{pt, normal} =>                | P.D_PLANE{pt, normal} =>
298                    letPRIM("posToPt", IR.T_VEC, IR.SUB_VEC, [psvToIRVar(env, pt), pos], fn posToPt =>                    letPRIM("posToPt", IR.T_VEC, IR.SUB_VEC, [pos, psvToIRVar(env, pt)], fn posToPt =>
299                    letPRIM("dot", IR.T_FLOAT, IR.DOT, [posToPt, psvToIRVar(env, normal)], fn dotProd =>                    letPRIM("dot", IR.T_FLOAT, IR.DOT, [posToPt, psvToIRVar(env, normal)], fn dotProd =>
300                    letPRIM(boolVar, IR.T_BOOL, IR.GT, [dotProd, IR.newConst("zero", IR.C_FLOAT 0.0)], stmt)))                    letPRIM(boolVar, IR.T_BOOL, IR.GT, [dotProd, IR.newConst("zero", IR.C_FLOAT 0.0)], stmt)))
301    
# Line 349  Line 353 
353                     size = newSize,                     size = newSize,
354                     isDead = newIsDead,                     isDead = newIsDead,
355                     color = newCol,                     color = newCol,
356                     dummy = IR.newConst("dmy", IR.C_FLOAT 0.01)})                     pos2 = IR.newConst("ps_pos2", IR.C_VEC {x=0.0, y=0.0, z=0.0}),
357                       dummy = IR.newConst("ps_dummy", IR.C_FLOAT 0.01)})
358              )))))              )))))
359    
360      (* Find the normal at the given position of the particle for the specified      (* Find the normal at the given position of the particle for the specified
# Line 400  Line 405 
405        IR.mkIF(isDead,        IR.mkIF(isDead,
406         (* then *)         (* then *)
407         letPRIM("t1", IR.T_FLOAT, IR.ITOF, [psvToIRVar (env, maxNum)], fn t1 =>         letPRIM("t1", IR.T_FLOAT, IR.ITOF, [psvToIRVar (env, maxNum)], fn t1 =>
408         letPRIM("t2", IR.T_FLOAT, IR.ITOF, [psvToIRVar (env, numDead)], fn t2 =>         letPRIM("t2", IR.T_FLOAT, IR.ITOF, [psvToIRVar (env, PSV.numDead)], fn t2 =>
409         letPRIM("prob", IR.T_FLOAT, IR.DIV, [t1, t2], fn prob =>         letPRIM("prob", IR.T_FLOAT, IR.DIV, [t1, t2], fn prob =>
410         letPRIM("r", IR.T_FLOAT, IR.RAND, [], fn r =>         letPRIM("r", IR.T_FLOAT, IR.RAND, [], fn r =>
411         letPRIM("t3", IR.T_BOOL, IR.GT, [prob, r], fn t3 =>         letPRIM("t3", IR.T_BOOL, IR.GT, [prob, r], fn t3 =>
# Line 426  Line 431 
431           end           end
432    
433      fun trAct (action, env, state, k : particle_state -> IR.stmt) = let      fun trAct (action, env, state, k : particle_state -> IR.stmt) = let
434            val PS{pos, vel, size, isDead, color, dummy} = state            val PS{pos, vel, size, isDead, color, pos2, dummy} = state
435            in            in
436              case action              case action
437               of P.BOUNCE{friction, resilience, cutoff, d} => let               of P.BOUNCE{friction, resilience, cutoff, d} => let
438                    val blk = newBlock (env, k)                    val blk = newBlock (env, k)
439                    val negOne = IR.newConst("negOne", IR.C_FLOAT ~1.0)                    val negOne = IR.newConst("negOne", IR.C_FLOAT ~1.0)
440                    in                    in
441                      letPRIM("vs", IR.T_VEC, IR.SCALE, [psvToIRVar(env, timeStep), vel], fn velScale =>                      letPRIM("vs", IR.T_VEC, IR.SCALE, [psvToIRVar(env, PSV.timeStep), vel], fn velScale =>
442                      letPRIM("np", IR.T_VEC, IR.ADD_VEC, [pos, velScale], fn nextPos =>                      letPRIM("np", IR.T_VEC, IR.ADD_VEC, [pos, velScale], fn nextPos =>
443                      mkWithinVar("wnp", env, pos, d, fn withinNextPos =>                      mkWithinVar("wnp", env, pos, d, fn withinNextPos =>
444                      IR.mkIF(withinNextPos,                      IR.mkIF(withinNextPos,
445                        (*then*)                        (*then*)
446                          normAtPoint("n", d, env, state, fn normAtD => fn state' => let                          normAtPoint("n", d, env, state, fn normAtD => fn state' => let
447                 val PS{pos=nextPos, vel=nextVel, size=nextSize, isDead=nextIsDead, color=nextColor, dummy=nextDummy} = state'                 val PS{pos=nextPos, vel=nextVel, size=nextSize, isDead=nextIsDead, color=nextColor, pos2=nextPos2, dummy=nextDummy} = state'
448                            in                            in
449                             letPRIM("negVel", IR.T_VEC, IR.SCALE, [negOne, nextVel], fn negVel =>                             letPRIM("negVel", IR.T_VEC, IR.SCALE, [negOne, nextVel], fn negVel =>
450                             letPRIM("dnv", IR.T_FLOAT, IR.DOT, [negVel, normAtD], fn dotNegVel =>                             letPRIM("dnv", IR.T_FLOAT, IR.DOT, [negVel, normAtD], fn dotNegVel =>
# Line 458  Line 463 
463                               letPRIM("f", IR.T_FLOAT, IR.MULT, [negOne, frictInv], fn modFrict =>                               letPRIM("f", IR.T_FLOAT, IR.MULT, [negOne, frictInv], fn modFrict =>
464                               letPRIM("fTang", IR.T_VEC, IR.SCALE, [modFrict, tang], fn frictTang =>                               letPRIM("fTang", IR.T_VEC, IR.SCALE, [modFrict, tang], fn frictTang =>
465                               letPRIM("newVel", IR.T_VEC, IR.ADD_VEC, [frictTang, resNorm], fn newVel =>                               letPRIM("newVel", IR.T_VEC, IR.ADD_VEC, [frictTang, resNorm], fn newVel =>
466                                goto(PS{pos=nextPos, vel=newVel, size=nextSize, isDead=nextIsDead, color=nextColor, dummy=nextDummy}, blk)                                goto(PS{pos=nextPos, vel=newVel, size=nextSize, isDead=nextIsDead, color=nextColor, pos2=nextPos2, dummy=nextDummy}, blk)
467                              )))),                              )))),
468                               (*else*)                               (*else*)
469                               letPRIM("fTang", IR.T_VEC, IR.SCALE, [negOne, tang], fn frictTang =>                               letPRIM("fTang", IR.T_VEC, IR.SCALE, [negOne, tang], fn frictTang =>
470                               letPRIM("newVel", IR.T_VEC, IR.ADD_VEC, [frictTang, resNorm], fn newVel =>                               letPRIM("ps_vel", IR.T_VEC, IR.ADD_VEC, [frictTang, resNorm], fn newVel =>
471                                goto(PS{pos=nextPos, vel=newVel, size=nextSize, isDead=nextIsDead, color=nextColor, dummy=nextDummy}, blk)                                goto(PS{pos=nextPos, vel=newVel, size=nextSize, isDead=nextIsDead, color=nextColor, pos2=nextPos2, dummy=nextDummy}, blk)
472                               ))                               ))
473                           )))))))))                           )))))))))
474                           end                           end
# Line 473  Line 478 
478                    end                    end
479    
480                | P.GRAVITY(dir) =>                | P.GRAVITY(dir) =>
481                      letPRIM("scaledVec", IR.T_VEC, IR.SCALE, [psvToIRVar(env, timeStep), psvToIRVar(env, dir)], fn theScale =>                      letPRIM("scaledVec", IR.T_VEC, IR.SCALE, [psvToIRVar(env, PSV.timeStep), psvToIRVar(env, dir)], fn theScale =>
482                      letPRIM("nextVel", IR.T_VEC, IR.ADD_VEC, [theScale, vel], fn newVel =>                      letPRIM("ps_vel", IR.T_VEC, IR.ADD_VEC, [theScale, vel], fn newVel =>
483                        k(PS{pos = pos, vel = newVel, size = size, isDead = isDead, color = color, dummy=dummy})))                        k(PS{pos = pos, vel = newVel, size = size, isDead = isDead, color = color, pos2=pos2, dummy=dummy})))
484    
485                | P.MOVE =>                | P.MOVE =>
486                  letPRIM("scaledVec", IR.T_VEC, IR.SCALE, [psvToIRVar(env, timeStep), vel], fn theScale =>                  letPRIM("scaledVec", IR.T_VEC, IR.SCALE, [psvToIRVar(env, PSV.timeStep), vel], fn theScale =>
487                      letPRIM("nextPos", IR.T_VEC, IR.ADD_VEC, [theScale, pos], fn newPos =>                      letPRIM("ps_pos", IR.T_VEC, IR.ADD_VEC, [theScale, pos], fn newPos =>
488                        k(PS{pos = newPos, vel = vel, size = size, isDead = isDead, color = color, dummy=dummy})))                        k(PS{pos = newPos, vel = vel, size = size, isDead = isDead, color = color, pos2=pos2, dummy=dummy})))
489                (*                (*
490                | P.SINK({d, kill_inside}) =>                | P.SINK({d, kill_inside}) =>
491                      mkWithinVar("isWithin", env, state, d, fn withinVal =>                      mkWithinVar("isWithin", env, state, d, fn withinVal =>
# Line 512  Line 517 
517                    letPRIM("magRatio", IR.T_FLOAT, IR.DIV, [distToP, psvToIRVar(env, maxRad)], fn magRatio =>                    letPRIM("magRatio", IR.T_FLOAT, IR.DIV, [distToP, psvToIRVar(env, maxRad)], fn magRatio =>
518                    letPRIM("oneMinMR", IR.T_FLOAT, IR.SUB, [IR.newConst("one", IR.C_FLOAT 1.0), magRatio], fn oneMinMR =>                    letPRIM("oneMinMR", IR.T_FLOAT, IR.SUB, [IR.newConst("one", IR.C_FLOAT 1.0), magRatio], fn oneMinMR =>
519                    letPRIM("gravityMag", IR.T_FLOAT, IR.MULT, [oneMinMR, psvToIRVar(env, mag)], fn gravityMag =>                    letPRIM("gravityMag", IR.T_FLOAT, IR.MULT, [oneMinMR, psvToIRVar(env, mag)], fn gravityMag =>
520                    letPRIM("totalMag", IR.T_FLOAT, IR.MULT, [gravityMag, psvToIRVar(env, timeStep)], fn totMag =>                    letPRIM("totalMag", IR.T_FLOAT, IR.MULT, [gravityMag, psvToIRVar(env, PSV.timeStep)], fn totMag =>
521                    letPRIM("accVec", IR.T_VEC, IR.SUB_VEC, [closestP, pos], fn accVec =>                    letPRIM("accVec", IR.T_VEC, IR.SUB_VEC, [closestP, pos], fn accVec =>
522                    letPRIM("acc", IR.T_VEC, IR.SCALE, [totMag, accVec], fn acc =>                    letPRIM("acc", IR.T_VEC, IR.SCALE, [totMag, accVec], fn acc =>
523                    letPRIM("newVel", IR.T_VEC, IR.ADD_VEC, [vel, acc], fn newVel =>                    letPRIM("ps_vel", IR.T_VEC, IR.ADD_VEC, [vel, acc], fn newVel =>
524                    goto(PS{pos = pos, vel = newVel, size = size, isDead = isDead, color = color, dummy=dummy}, blk)                    goto(PS{pos = pos, vel = newVel, size = size, isDead = isDead, color = color, pos2=pos2, dummy=dummy}, blk)
525                    )))))))                    )))))))
526                  )))))))))))                  )))))))))))
527                  end                  end
# Line 536  Line 541 
541            val blks = ref[]            val blks = ref[]
542            val env = let            val env = let
543                (* add special globals to free vars *)                (* add special globals to free vars *)
544                  val vars = PSV.Set.union(emitVars, PSV.Set.addList(actionVars, [maxNum, numDead, timeStep, epsilon]))                  val vars = PSV.Set.union(emitVars, PSV.Set.addList(actionVars, [maxNum, PSV.numDead, PSV.timeStep, epsilon]))
545                  fun ins (x as PSV.V{name, ty, binding, id, ...}, map) = let                  fun ins (x as PSV.V{name, ty, binding, id, ...}, map) = let
546                        val x' = (case (ty, !binding)                        val x' = (case (ty, !binding)
547                               of (PSV.T_BOOL,  PSV.UNDEF) => IR.newGlobal(x, IR.T_BOOL)                               of (PSV.T_BOOL,  PSV.UNDEF) => IR.newGlobal(x, IR.T_BOOL)
# Line 575  Line 580 
580    
581            (* At the highest level, we want to return when we reach the end of the action list *)            (* At the highest level, we want to return when we reach the end of the action list *)
582            fun trActs [] state = let            fun trActs [] state = let
583                  val PS{pos, vel, size, isDead, color, dummy} = state                  val PS{pos, vel, size, isDead, color, pos2, dummy} = state
584                  in                  in
585                    IR.mkRETURN[ pos, vel, size, isDead, color, dummy ]                    IR.mkRETURN (
586                        [ pos, vel, size, isDead, color, pos2, dummy ],
587                        [IR.POS, IR.VEL, IR.SZ, IR.ISDEAD, IR.COLOR, IR.POS2, IR.DUMMY]
588                      )
589                  end (* trActs *)                  end (* trActs *)
590              | trActs _ _ = raise Fail "Should never reach here"              | trActs _ _ = raise Fail "Should never reach here"
591    
# Line 595  Line 603 
603            (* The entry block is the emitter, and the rest of the blocks define the physics processing. *)            (* The entry block is the emitter, and the rest of the blocks define the physics processing. *)
604            val outPgm = PSysIR.PGM {            val outPgm = PSysIR.PGM {
605              emitter = entryBlock,              emitter = entryBlock,
606              physics = List.drop(!blks, 1),              physics = List.nth(!blks, 1),
607              render = render              render = render
608            }            }
609    
610            val optimized = if (Checker.checkIR(outPgm)) then Optimize.optimizeIR(outPgm) else outPgm            val optimized = if (Checker.checkIR(outPgm)) then (printErr "Pre-optimization complete."; Optimize.optimizeIR(outPgm)) else outPgm
611    
612            in            in
613              IR.outputPgm(TextIO.stdErr, outPgm);              (* IR.outputPgm(TextIO.stdErr, outPgm); *)
614              if Checker.checkIR(optimized) then              if Checker.checkIR(optimized) then
615               printErr "Compilation succeeded." (* Note: it only succeeds if we can optimize, too *)               printErr "Compilation succeeded." (* Note: it only succeeds if we can optimize, too *)
616              else              else
617               ();               ();
             IR.outputPgm(TextIO.stdErr, optimized);  
618              optimized              optimized
619            end (* compile *)            end (* compile *)
620    

Legend:
Removed from v.873  
changed lines
  Added in v.906

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