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

SCM Repository

[sml3d] View of /trunk/sml3d/src/shaders/shader-ir.sml
ViewVC logotype

View of /trunk/sml3d/src/shaders/shader-ir.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1236 - (download) (annotate)
Thu Sep 22 19:30:01 2011 UTC (6 years, 5 months ago) by samquinan
File size: 10136 byte(s)
fixed naming issues
structure ShaderIR =
  	struct
  		
  		structure REP = ShaderRep
  		
  		type var_ty = REP.var_ty
  		type stage = REP.shader_ty
  		type value = REP.value
  		type data_ty = REP.ty_rep
  		type tform_ty = REP.transform
  		
  		datatype var =
  				V of {
  					id: word,
  					name: string,
  					var_type: var_ty ref,
  					data_type: data_ty,
  					scope: scope ref,
  					useCount : int ref
  				}
  		
  		and block = BLK of {
    	  id : word,
    	  params : var list,
    	  body : stmnt,
    	  stage : stage ref,
    	  visited : bool ref,
    	  refCount : int ref
    	}
  			
  		and stmnt
  		  	= PRIM of var * primitive * var list * stmnt
  		  	| CONST of var * value * stmnt
  			| COND of var * stmnt * stmnt
  			| GOTO of block * var list
  			| RETURN of var list
  			| DISCARD  		
  			
    	and scope =
    		  S_GLOBAL of (stage ref)
    		| S_LOCAL
    		 	
    	and primitive =
    		  LEN					(* length *)
    		| NORM					(* normalize *)
    		| ISO_SCALE				(* vector scale *)
		    | DOT					(* dot product *)
		    | CROSS					(* Cross product *)
		    | EXTRACT_N				(* extracts the nth-component of a color or position vector *)
		    | NEG					(* vector negation *)
    		| ADD 					(* scalar addition *)
    		| SUB					(* scalar subtraction *)
    		| MUL 					(* scalar multiplication *)
    		| DIV 					(* scalar division *)
    		| SQRT					(* square root *)
    		| POW					(* power *)
    		| ABS					(* absolute value *)
    		| COS					(* cosine *)
    		| SIN					(* sine *)
    		| FRACT					(* fraction component of float *)
    		| MAX   				(* max *)
    		| MIN    				(* min *)
    		| MIX 		   			(* lerp *)
    		| STEP 					(* returns 0.0 for each component in v1 < corresponding componnt in v0 	*)
		    | CLAMP  				(* clamp *)
		    | GEN_VEC2				(* create a vec2 from 2 floats *)
		    | GEN_VEC3				(* create a vec3 from 3 floats *)
		    | GEN_VEC4				(* create a vec4 from 4 floats *)
		    | GEN_RGB				(* create a color3 from 3 floats *)
		    | GEN_RGBA				(* create a color4 from 4 floats *)
		    | APPLY_TFORM		  	(* applies a transform to a point, vector or normal*)
		    | TFORM_NORM			(* applies a transform to a normal*)
		    | SAMPLE_2D				(* gets indexed value from sampler2D*)
		    | SAMPLE_3D				(* gets indexed value from sampler2D*)
	    	(* Boolean Primitives *)
	      	| GT		(* greater than *)
	      	| LT		(* less than *)
	      	| EQUALS	(* equality *)
	      	| AND		(* conjunction *)
	      	| OR		(* disjunction *)
	      	| NOT		(* negation *)
	      	(* Transform Primitives *)
	      	| IDENTITY
	      	| INVERSE
	      	| TRANSPOSE
	      	| CUSTOM
	      	| TRANSLATE
	      	| ROTATE_X
	      	| ROTATE_Y
	      	| ROTATE_Z
	      	| USCALE
	      	| SCALE
	      	(*** may want to expand / include: ***
	      	| RAND		* random number between 0 and 1 *
	      	| ITOF		* convert int to float *
	      	| COPY		* copy one var to another... works with all types *
	      	****************************************)
	  	
	  	datatype shaderProgram = PGM of {
	  		globals : var list,
  			begin : block
    	}
  		
    	fun varIsEqual (V{id=a, ...}, V{id=b, ...}) = (a = b)
    	
    	fun blkIsEqual (BLK{id=a, ...}, BLK{id = b, ...}) = (a = b)
    	
    	local
    		val cnt = ref 0w0
    		fun nextId () = let val n = !cnt in cnt := n + 0w1; n end
    	in
    		fun new (name, scope, varTy, dataTy) = V{
  					id = nextId(),
  					name = name,
  					var_type = ref varTy,
  					data_type = dataTy,
  					scope = ref scope,
  					useCount = ref 0
  				}
      		fun newGlobal (name, dataTy, varTy, stage) = new (name, S_GLOBAL (ref stage), varTy, dataTy)
      		fun newLocal (name, dataTy, varTy) = new (name, S_LOCAL, varTy, dataTy)
    	end (* local *)
    
    	local
    		val cnt = ref 0w0
      		fun nextId () = let val n = !cnt in cnt := n + 0w1; n end
    	in
      		fun newBlock (params, body, stage) = BLK{	
      			id = nextId(),
        	  	params = params,
        		body = body,
        		stage = stage,
        		visited = ref false,
        		refCount = ref 0
      		}
    	end (* local *)
    
    	fun mkPRIM(var, prim, args, stmt) = 
    	  	let
    	    	val V{scope, ...} = var
    	    	val s = !scope
    	  	in
    	    	(case s of 
    	      		S_LOCAL => PRIM(var, prim, args, stmt)
    	      		| _ => raise Fail ("Cannot assign value to non-local variable.")
    	    	(* end case *))
    	  	end (* mkPRIM *)
		
		fun mkCONST(var, Val, stmt) =
			let
				val V{scope, data_type,  ...} = var
				val d_ty = REP.typeOfValue(Val)
				val s = !scope
			in
				(case s of 
    	      		S_LOCAL => 
    	      			if (data_type = d_ty)
    	      			then CONST(var, Val, stmt)
    	      			else raise Fail ("Cannot assign value to variable with a different data-type.")
    	      		| _ => raise Fail ("Cannot assign value to non-local variable.")
    	    	(* end case *))
    	    end (*mkCONST *)
		
		fun mkCOND(x, s1, s2) = COND(x, s1, s2)
		fun mkRETURN(vars) = RETURN(vars)
		fun mkGOTO(blk, vars) = GOTO(blk, vars)    
		fun mkDISCARD () = DISCARD
  	
		fun valueToString (REP.NULL) = "NULL"
          | valueToString (REP.VEC4 v) = Vec4f.toString v
   		  | valueToString (REP.VEC3 v) = Vec3f.toString v
  	  	  | valueToString (REP.VEC2 v) = Vec2f.toString v
      	  | valueToString (REP.RGB c) = Color.color3fToString c
      	  | valueToString (REP.RGBA c) = Color.color4fToString c
      	  | valueToString (REP.MAT3 m) = 
      			let
      				val rows = Matrix3f.toRows m
      				val a = Vec3f.toString (#1 rows)
      				val b = Vec3f.toString (#2 rows)
      				val c = Vec3f.toString (#3 rows)
      			in 
      				concat[	"<", a, ",", b, ", ", c, ">"]
      			end
      	  | valueToString (REP.MAT4 m) = 
      			let
      				val rows = Matrix4f.toRows m
      				val a = Vec4f.toString (#1 rows)
      				val b = Vec4f.toString (#2 rows)
      				val c = Vec4f.toString (#3 rows)
      				val d = Vec4f.toString (#4 rows)
      			in 
      				concat[	"<", a, ", ", b, ", ", c, ", ", d, ">"]
      			end
      	  | valueToString (REP.FLOAT f) = Float.toString f
      	  | valueToString (REP.BOOL b) = Bool.toString b
      	  | valueToString (REP.INT i) = Int.toString i
      	
    	fun dataTyToString (REP.T_FLOAT) = "T_FLOAT"
  	      | dataTyToString (REP.T_VEC2) = "T_VEC2"
  	      | dataTyToString (REP.T_VEC3) = "T_VEC3"
  	      | dataTyToString (REP.T_VEC4) = "T_VEC4"
  	      | dataTyToString (REP.T_RGB) = "T_RGB"
  	      | dataTyToString (REP.T_RGBA) = "T_RGBA"
  	      | dataTyToString (REP.T_MAT3) = "T_MAT3"
  	      | dataTyToString (REP.T_MAT4) = "T_MAT4"
  	      | dataTyToString (REP.T_BOOL) = "T_BOOL"
  	      | dataTyToString (REP.T_INT) = "T_INT"
  	      | dataTyToString (REP.T_TFORM2D) = "T_FORM2D"
  	      | dataTyToString (REP.T_TFORM3D) = "T_FORM3D"
  	  	
  		fun varTyToString (REP.UNIFORM) = "UNIFORM"
  	      | varTyToString (REP.ATTRIBUTE) = "ATTRIBUTE"
  	      | varTyToString (REP.VARYING) = "VARYING"
  	      | varTyToString (REP.FRAG_VAR) = "FRAG_VAR"
  	      | varTyToString (REP.OUTPUT) = "OUTPUT"
  		
  		fun primToString p = (case
		   p of LEN => "LEN"
    		  | NORM => "NORM"
    		  | ISO_SCALE => "ISO_SCALE"
	    	  | DOT => "DOT"
	    	  | CROSS => "CROSS"
    		  | EXTRACT_N => "EXTRACT_N"
	    	  | NEG=> "NEG"
    		  | ADD => "ADD"
    		  | SUB => "SUB"
    		  | MUL => "MUL"
    		  | DIV => "DIV"
    		  | POW => "POW"
    		  | SQRT => "SQRT"
    		  | ABS => "ABS"
    		  | COS => "COS"
    		  | SIN => "SIN"
    		  | FRACT => "FRACT"
    		  | MAX => "MAX"
    		  | MIN => "MIN"
    		  | MIX => "MIX"
    		  | STEP => "STEP"
	    	  | CLAMP => "CLAMP"
	    	  | GEN_VEC2 => "GEN_VEC2"
	    	  | GEN_VEC3 => "GEN_VEC3"
	    	  | GEN_VEC4 => "GEN_VEC4"
	    	  | GEN_RGB	=> "GEN_RGB"
		      | GEN_RGBA => "GEN_RGBA"
	    	  | APPLY_TFORM => "APPLY_TFORM"
	    	  | TFORM_NORM => "TFORM_NORM"
	    	  | SAMPLE_2D => "SAMPLE_2D"
	    	  | SAMPLE_3D => "SAMPLE_3D"
      		  | GT => "GT"
      		  | LT => "LT"
      		  | EQUALS => "EQUALS"
      		  | AND => "AND"
      		  | OR => "OR"
      		  | NOT => "NOT"
      		  | IDENTITY => "IDENTITY"
      		  | INVERSE => "INVERSE"
      		  | TRANSPOSE => "TRANSPOSE"
      		  | CUSTOM => "CUSTOM"
      		  | TRANSLATE => "TRANSLATE"
      		  | ROTATE_X => "ROTATE_X"
      		  | ROTATE_Y => "ROTATE_Y"
      		  | ROTATE_Z => "ROTATE_Z"
      		  | USCALE => "USCALE"
      		  | SCALE => "SCALE"
      		  (*
      		   *| RAND => "RAND"
      		   *| ITOF => "ITOF"
      		   *| COPY => "COPY"
      		   *)
      		(*end case*))
		  	
  		fun varToString (V{name, id, scope, data_type, useCount, ...}) = 
  			let
  				val s = !scope
  			in
  				(case s
		   			of S_GLOBAL _ => String.concat["$", name, "_", Word.toString id, ":", dataTyToString data_type, ":",  Int.toString (!useCount)]
	    			 | S_LOCAL => String.concat[name, "_", Word.toString id, ":", dataTyToString data_type, ":", Int.toString (!useCount)]
	  			(* end case *))
	  		end
	  	
		fun varListToString (nil) = ""
		  | varListToString (V{name, id, scope, ...}::xs) = 
		  	let
  				val s = !scope
  			in
  				(case s
		    		of S_GLOBAL _ => String.concat["$", name, "_", Word.toString id, ", ", varListToString xs]
		     		 | S_LOCAL => String.concat[name, "_", Word.toString id, ", ", varListToString xs]
		      	(* end case *))
		    end
		
		fun stmntToString stmnt = (case stmnt
			 of PRIM(v1, p1, vl1, c1) => String.concat[ varToString v1, " = ", primToString p1, "(", varListToString vl1, ")/n", stmntToString c1]
			  | CONST(v1, val1, c1) => String.concat[ varToString v1, " = ", valueToString val1, "/n", stmntToString c1]
			  | COND(v1, c1, c2) => String.concat[ "IF ", varToString v1, "/nTHEN ", stmntToString c1, "/nELSE ", stmntToString c2]
			  | GOTO(b as BLK{id, ...}, vl1) => String.concat[ "GOTO BLOCK_", Word.toString id, "(", varListToString vl1, ")/n"]
			  | RETURN(vl1) => String.concat[ "RETURN: ", varListToString vl1, "/n"]
			  | DISCARD => "DISCARD"
		(* end case *))
		
  		(*finish print block etc.*)
		
	end

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