@@ -490,85 +490,93 @@ data class Program(val vertex: VertexShader, val fragment: FragmentShader, val n
490490 val out : Output get() = Output
491491 // fun out(to: Operand) = Stm.Set(if (type == ShaderType.VERTEX) out_Position else out_FragColor, to)
492492
493- fun sin (arg : Operand ): Operand = Func (" sin" , arg)
494- fun cos (arg : Operand ): Operand = Func (" cos" , arg)
495- fun tan (arg : Operand ): Operand = Func (" tan" , arg)
496-
497- fun asin (arg : Operand ): Operand = Func (" asin" , arg)
498- fun acos (arg : Operand ): Operand = Func (" acos" , arg)
499- fun atan (y_over_x : Operand ): Operand = Func (" atan" , y_over_x)
500- fun atan (y : Operand , x : Operand ): Operand = Func (" atan" , y, x)
493+ fun sin (angle : Operand ): Operand = Func (" sin" , angle, type = angle.type)
494+ fun cos (angle : Operand ): Operand = Func (" cos" , angle, type = angle.type)
495+ fun tan (angle : Operand ): Operand = Func (" tan" , angle, type = angle.type)
496+
497+ fun asin (x : Operand ): Operand = Func (" asin" , x, type = x.type)
498+ fun acos (x : Operand ): Operand = Func (" acos" , x, type = x.type)
499+ fun atan (yOverX : Operand ): Operand = Func (" atan" , yOverX, type = yOverX.type)
500+ fun atan (y : Operand , x : Operand ): Operand = Func (" atan" , y, x, type = y.type)
501+
502+ fun sinh (x : Operand ): Operand = Func (" sinh" , x, type = x.type)
503+ fun cosh (x : Operand ): Operand = Func (" cosh" , x, type = x.type)
504+ fun tanh (x : Operand ): Operand = Func (" tanh" , x, type = x.type)
505+ fun asinh (x : Operand ): Operand = Func (" asinh" , x, type = x.type)
506+ fun acosh (x : Operand ): Operand = Func (" acosh" , x, type = x.type)
507+ fun atanh (x : Operand ): Operand = Func (" atanh" , x, type = x.type)
501508
502509 // @TODO: https://en.wikipedia.org/wiki/Atan2#Definition_and_computation (IF chain)
503510 // fun atan2(a: Operand, b: Operand): Operand = atan(a / b) * 2f.lit
504511
505- fun radians (arg : Operand ): Operand = Func (" radians" , arg )
506- fun degrees (arg : Operand ): Operand = Func (" degrees" , arg )
512+ fun radians (degrees : Operand ): Operand = Func (" radians" , degrees, type = degrees.type )
513+ fun degrees (radians : Operand ): Operand = Func (" degrees" , radians, type = radians.type )
507514
508515 // Sampling
509516 fun texture2D (sampler : Operand , coord : Operand ): Operand = Func (" texture2D" , sampler, coord, type = VarType .Float4 )
510- fun texture (sampler : Operand , P : Operand ): Operand = Func (" texture" , sampler, P )
517+ fun texture (sampler : Operand , P : Operand ): Operand = Func (" texture" , sampler, P , type = VarType . Float4 ) // @TODO: calculate correct result type based on operand types
511518
512- fun func (name : String , vararg args : Operand ): Operand = Func (name, * args.map { it }.toTypedArray())
519+ fun func (name : String , vararg args : Operand , type : VarType = VarType . Float1 ): Operand = Func (name, * args.map { it }.toTypedArray(), type = type )
513520
514521 fun TERNARY (cond : Operand , otrue : Operand , ofalse : Operand ): Operand = Ternary (cond, otrue, ofalse)
515522
516523 // CAST
517- fun int (v : Operand ): Operand = Func (" int" , v)
518- fun float (v : Operand ): Operand = Func (" float" , v)
519-
520- fun pow (b : Operand , e : Operand ): Operand = Func (" pow" , b, e)
521- fun exp (v : Operand ): Operand = Func (" exp" , v)
522- fun exp2 (v : Operand ): Operand = Func (" exp2" , v)
523- fun log (v : Operand ): Operand = Func (" log" , v)
524- fun log2 (v : Operand ): Operand = Func (" log2" , v)
525- fun sqrt (v : Operand ): Operand = Func (" sqrt" , v)
526- fun inversesqrt (v : Operand ): Operand = Func (" inversesqrt" , v)
527-
528- fun abs (v : Operand ): Operand = Func (" abs" , v)
529- fun sign (v : Operand ): Operand = Func (" sign" , v)
530- fun ceil (v : Operand ): Operand = Func (" ceil" , v)
531- fun floor (v : Operand ): Operand = Func (" floor" , v)
524+ fun int (v : Operand ): Operand = Func (" int" , v, type = VarType . Int1 )
525+ fun float (v : Operand ): Operand = Func (" float" , v, type = VarType . Float1 )
526+
527+ fun pow (b : Operand , e : Operand ): Operand = Func (" pow" , b, e, type = b.type )
528+ fun exp (v : Operand ): Operand = Func (" exp" , v, type = v.type )
529+ fun exp2 (v : Operand ): Operand = Func (" exp2" , v, type = v.type )
530+ fun log (v : Operand ): Operand = Func (" log" , v, type = v.type )
531+ fun log2 (v : Operand ): Operand = Func (" log2" , v, type = v.type )
532+ fun sqrt (v : Operand ): Operand = Func (" sqrt" , v, type = v.type )
533+ fun inversesqrt (v : Operand ): Operand = Func (" inversesqrt" , v, type = v.type )
534+
535+ fun abs (v : Operand ): Operand = Func (" abs" , v, type = v.type )
536+ fun sign (v : Operand ): Operand = Func (" sign" , v, type = v.type )
537+ fun ceil (v : Operand ): Operand = Func (" ceil" , v, type = v.type )
538+ fun floor (v : Operand ): Operand = Func (" floor" , v, type = v.type )
532539
533540 /* * The fractional part of v. This is calculated as v - floor(v). */
534- fun fract (v : Operand ): Operand = Func (" fract" , v)
541+ fun fract (v : Operand ): Operand = Func (" fract" , v, type = v.type )
535542
536543 fun clamp01 (v : Operand ): Operand = clamp(v, 0f .lit, 1f .lit)
537- fun clamp (v : Operand , min : Operand , max : Operand ): Operand = Func (" clamp" , v, min, max)
538- fun min (a : Operand , b : Operand ): Operand = Func (" min" , a, b)
539- fun max (a : Operand , b : Operand ): Operand = Func (" max" , a, b)
540- fun mod (a : Operand , b : Operand ): Operand = Func (" mod" , a, b)
544+ fun clamp (v : Operand , min : Operand , max : Operand ): Operand = Func (" clamp" , v, min, max, type = v.type )
545+ fun min (a : Operand , b : Operand ): Operand = Func (" min" , a, b, type = a.type )
546+ fun max (a : Operand , b : Operand ): Operand = Func (" max" , a, b, type = a.type )
547+ fun mod (a : Operand , b : Operand ): Operand = Func (" mod" , a, b, type = a.type )
541548
542549 // fun lerp(a: Operand, b: Operand, c: Operand): Operand = Func("lerp", a, b, c)
543550
544551 // https://learnwebgl.brown37.net/12_shader_language/documents/webgl-reference-card-1_0.pdf
545552 // #extension GL_OES_standard_derivatives : enable
546553 // https://stackoverflow.com/questions/68573364/enable-extension-and-fwidth-in-glsl
547- fun fwidth (a : Operand ): Operand = Func (" fwidth" , a)
548- fun dFdx (a : Operand ): Operand = Func (" dFdx" , a)
549- fun dFdy (a : Operand ): Operand = Func (" dFdy" , a)
554+ fun fwidth (a : Operand ): Operand = Func (" fwidth" , a, type = a.type )
555+ fun dFdx (a : Operand ): Operand = Func (" dFdx" , a, type = a.type )
556+ fun dFdy (a : Operand ): Operand = Func (" dFdy" , a, type = a.type )
550557
551558 // lessThan
552559
553560
554561 // @JvmName("modInfix") infix fun Operand.mod(that: Operand): Operand = mod(this, that)
555562
556563 fun mix (a : Operand , b : Operand , step : Operand ): Operand =
557- Func (" mix" , a, b, step)
558- fun step (a : Operand , b : Operand ): Operand = Func (" step" , a, b)
564+ Func (" mix" , a, b, step, type = a.type )
565+ fun step (a : Operand , b : Operand ): Operand = Func (" step" , a, b, type = a.type )
559566 fun smoothstep (a : Operand , b : Operand , c : Operand ): Operand =
560- Func (" smoothstep" , a, b, c)
567+ Func (" smoothstep" , a, b, c, type = a.type )
561568
562- fun length (a : Operand ): Operand = Func (" length" , a)
563- fun distance (a : Operand , b : Operand ): Operand = Func (" distance" , a, b)
564- fun dot (a : Operand , b : Operand ): Operand = Func (" dot" , a, b)
565- fun cross (a : Operand , b : Operand ): Operand = Func (" cross" , a, b)
566- fun normalize (a : Operand ): Operand = Func (" normalize" , a)
569+ fun length (a : Operand ): Operand = Func (" length" , a, type = VarType . Float1 )
570+ fun distance (a : Operand , b : Operand ): Operand = Func (" distance" , a, b, type = VarType . Float1 )
571+ fun dot (a : Operand , b : Operand ): Operand = Func (" dot" , a, b, type = VarType . Float1 )
572+ fun cross (a : Operand , b : Operand ): Operand = Func (" cross" , a, b, type = VarType . Float3 )
573+ fun normalize (a : Operand ): Operand = Func (" normalize" , a, type = a.type )
567574 fun faceforward (a : Operand , b : Operand , c : Operand ): Operand =
568- Func (" faceforward" , a, b, c)
569- fun reflect (a : Operand , b : Operand ): Operand = Func (" reflect" , a, b)
575+ Func (" faceforward" , a, b, c, type = a.type)
576+ fun reflect (a : Operand , b : Operand ): Operand =
577+ Func (" reflect" , a, b, type = a.type)
570578 fun refract (a : Operand , b : Operand , c : Operand ): Operand =
571- Func (" refract" , a, b, c)
579+ Func (" refract" , a, b, c, type = a.type )
572580
573581 val Int .lit: IntLiteral get() = IntLiteral (this )
574582 @Deprecated(" " , ReplaceWith (" this.toFloat().lit" ))
@@ -759,6 +767,12 @@ data class Program(val vertex: VertexShader, val fragment: FragmentShader, val n
759767
760768 fun TEMP (type : VarType ): Temp = Temp (context.tempLastId++ , type)
761769
770+ fun TEMP (initialValue : Operand ): Temp {
771+ val temp = TEMP (initialValue.type)
772+ SET (temp, initialValue)
773+ return temp
774+ }
775+
762776 class FuncDeclGetter <T : FuncRef >(val decl : FuncDecl ) {
763777 operator fun getValue (thisRef : Any? , property : KProperty <* >): T = decl as T
764778 }
0 commit comments