Skip to content

Commit 3dbd36a

Browse files
authored
TEMP with initial value and provide default types for shader functions (#2318)
1 parent 0231ec4 commit 3dbd36a

File tree

5 files changed

+92
-94
lines changed

5 files changed

+92
-94
lines changed

korge-core/src/korlibs/graphics/shader/shaders.kt

Lines changed: 60 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

korge-core/src/korlibs/korge/render/SDFShaders.kt

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,15 @@ object SDFShaders : Program.Builder() {
4545
RETURN(clamp(d / fwidth(d) + 0.5f, 0f.lit, 1f.lit))
4646
}
4747
val opSmoothUnion by FUNC(Float1, Float1, Float1, returns = Float1) { d1, d2, k ->
48-
val h = TEMP(Float1)
49-
SET(h, clamp(.5f + .5f * (d2 - d1) / k, 0f.lit, 1f.lit))
48+
val h = TEMP(clamp(.5f + .5f * (d2 - d1) / k, 0f.lit, 1f.lit))
5049
RETURN(mix(d2, d1, h) - k * h * (1f - h))
5150
}
5251
val opSmoothSubtraction by FUNC(Float1, Float1, Float1, returns = Float1) { d1, d2, k ->
53-
val h = TEMP(Float1)
54-
SET(h, clamp(.5f - .5f * (d2 + d1) / k, 0f.lit, 1f.lit))
52+
val h = TEMP(clamp(.5f - .5f * (d2 + d1) / k, 0f.lit, 1f.lit))
5553
RETURN(mix(d2, -d1, h) + k * h * (1f - h))
5654
}
5755
val opSmoothIntersection by FUNC(Float1, Float1, Float1, returns = Float1) { d1, d2, k ->
58-
val h = TEMP(Float1)
59-
SET(h, clamp(.5f - .5f * (d2 - d1) / k, 0f.lit, 1f.lit))
56+
val h = TEMP(clamp(.5f - .5f * (d2 - d1) / k, 0f.lit, 1f.lit))
6057
RETURN(mix(d2, d1, h) + k * h * (1f - h))
6158
}
6259

@@ -65,15 +62,13 @@ object SDFShaders : Program.Builder() {
6562
RETURN(length(p) - r)
6663
}
6764
val roundedBox by FUNC(Float2, Float2, Float4, returns = Float1) { p, b, r ->
68-
val q = TEMP(Float2)
6965
SET(r["xy"], TERNARY(p.x gt 0f, r["xy"], r["zw"]))
7066
SET(r.x, TERNARY(p.y gt 0f, r.x, r.y))
71-
SET(q, abs(p) - b + r.x)
67+
val q = TEMP(abs(p) - b + r.x)
7268
RETURN(min(max(q.x, q.y), 0f.lit) + length(max(q, 0f.lit)) - r.x)
7369
}
7470
val box by FUNC(Float2, Float2, returns = Float1) { p, b ->
75-
val d = TEMP(Float2)
76-
SET(d, abs(p) - b)
71+
val d = TEMP(abs(p) - b)
7772
RETURN(length(max(d, 0f.lit)) + min(max(d.x, d.y), 0f.lit))
7873
}
7974

korge/src/korlibs/korge/render/RenderContext2DExt.kt

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,32 +70,28 @@ object MaterialRender {
7070
}
7171

7272
val PROGRAM = ShadedView.buildShader {
73-
val roundedDist = TEMP(Float1)
74-
val borderDist = TEMP(Float1)
75-
val highlightDist = TEMP(Float1)
76-
val borderAlpha = TEMP(Float1)
77-
val highlightAlpha = TEMP(Float1)
78-
7973
// The pixel space scale of the rectangle.
8074
val size = u_Size
8175

82-
SET(roundedDist, SDFShaders.roundedBox(v_Tex - (size / 2f), size / 2f, u_Radius))
76+
val roundedDist = TEMP(SDFShaders.roundedBox(v_Tex - (size / 2f), size / 2f, u_Radius))
77+
8378
SET(out, u_BackgroundColor * SDFShaders.opAA(roundedDist))
8479

8580
// Render circle highlight
8681
IF(u_HighlightRadius gt 0f) {
87-
SET(highlightDist, SDFShaders.opIntersect(roundedDist, SDFShaders.circle(v_Tex - u_HighlightPos, u_HighlightRadius)))
88-
SET(highlightAlpha, SDFShaders.opAA(highlightDist))
82+
val highlightDist = SDFShaders.opIntersect(roundedDist, SDFShaders.circle(v_Tex - u_HighlightPos, u_HighlightRadius))
83+
val highlightAlpha = TEMP(SDFShaders.opAA(highlightDist))
84+
8985
IF(highlightAlpha gt 0f) {
9086
SET(out, SDFShaders.opCombinePremultipliedColors(out, u_HighlightColor * highlightAlpha))
9187
}
9288
}
9389

9490
// Render border
9591
IF(u_BorderSizeHalf gt 0f) {
96-
SET(borderDist, SDFShaders.opBorderInner(roundedDist, u_BorderSizeHalf * 2f))
92+
val borderDist = SDFShaders.opBorderInner(roundedDist, u_BorderSizeHalf * 2f)
9793
//SET(borderDist, SDFShaders.opBorder(roundedDist, u_BorderSizeHalf))
98-
SET(borderAlpha, SDFShaders.opAA(borderDist))
94+
val borderAlpha = TEMP(SDFShaders.opAA(borderDist))
9995
IF(borderAlpha gt 0f) {
10096
SET(out, SDFShaders.opCombinePremultipliedColors(out, u_BorderColor * borderAlpha))
10197
}

korge/src/korlibs/korge/view/fast/FSprites.kt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,7 @@ open class FSprites(val maxSize: Int) {
227227
return Program(VertexShaderDefault {
228228
//SET(out, (u_ProjMat * u_ViewMat) * vec4(vec2(a_x, a_y), 0f.lit, 1f.lit))
229229
//SET(v_color, texture2D(u_Tex, vec2(vec1(id) / 4f.lit, 0f.lit)))
230-
val baseSize = TEMP(VarType.Float2)
231230
val texSize = TEMP(VarType.Float2)
232-
SET(baseSize, a_uv1 - a_uv0)
233231
SET(v_Col, vec4(a_colMul["rgb"] * a_colMul["a"], a_colMul["a"])) // Pre-multiply color here
234232
SET(v_TexId, a_texId)
235233

@@ -241,17 +239,16 @@ open class FSprites(val maxSize: Int) {
241239
mix(a_uv0.y, a_uv1.y, a_xy.y),
242240
) * texSize)
243241

244-
val cos = TEMP(VarType.Float1)
245-
val sin = TEMP(VarType.Float1)
246-
SET(cos, cos(a_angle))
247-
SET(sin, sin(a_angle))
242+
val cos = TEMP(cos(a_angle))
243+
val sin = TEMP(sin(a_angle))
248244
SET(t_TempMat2, mat2(
249245
cos, -sin,
250246
sin, cos,
251247
))
252248
val size = t_Temp0["zw"]
253249
val localPos = t_Temp0["xy"]
254250

251+
val baseSize = a_uv1 - a_uv0
255252
SET(size, baseSize * a_scale)
256253
SET(localPos, t_TempMat2 * ((a_xy - a_anchor) * size))
257254
SET(out, (u_ProjMat * u_ViewMat) * vec4(localPos + vec2(a_pos.x, a_pos.y), 0f.lit, 1f.lit))

korge/src/korlibs/korge/view/filter/DitheringFilter.kt

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,12 @@ class DitheringFilter(
3333
63, 31, 55, 23, 61, 29, 53, 21
3434
)
3535
val indexValue by FUNC(Float2, returns = Float1) { coords ->
36-
val index = TEMP(Float1)
3736
val matrix = indexMatrix8x8
3837
//val matrix = indexMatrix4x4
3938
val width = kotlin.math.sqrt(matrix.size.toDouble()).toInt()
4039
val x = int(mod(coords.x, width.toFloat().lit))
4140
val y = int(mod(coords.y, width.toFloat().lit))
42-
SET(index, float(x + y * width.lit))
41+
val index = TEMP(float(x + y * width.lit))
4342
IF_ELSE_BINARY_LOOKUP(index, 0, matrix.size - 1) {
4443
RETURN((matrix[it].toFloat() / matrix.size.toFloat()).lit)
4544
}
@@ -48,26 +47,23 @@ class DitheringFilter(
4847
}
4948

5049
override val fragment: FragmentShader = FragmentShaderDefault {
51-
val COL = TEMP(Float4)
52-
val COL1 = TEMP(Float4)
53-
val COL2 = TEMP(Float4)
54-
val DIST1 = TEMP(Float4)
55-
val DIST3 = TEMP(Float4)
56-
val INDEX1 = TEMP(Float1)
57-
val STEPS = DitherUB.u_Levels
58-
val hueDiff = TEMP(Float4)
59-
SET(COL, tex(fragmentCoords))
60-
SET(COL1, vec4(floor(COL * STEPS)) / STEPS)
61-
SET(COL2, vec4(ceil(COL * STEPS)) / STEPS)
62-
SET(DIST1, abs(COL1 - COL))
63-
SET(DIST3, abs(COL2 - COL1))
64-
SET(INDEX1, DitheringTools.indexValue(fragmentCoords))
65-
SET(hueDiff , DIST1 / DIST3)
50+
val steps = DitherUB.u_Levels
51+
52+
val col = TEMP(tex(fragmentCoords))
53+
val col1 = TEMP(vec4(floor(col * steps)) / steps)
54+
val col2 = TEMP(vec4(ceil(col * steps)) / steps)
55+
56+
val dist1 = TEMP(abs(col1 - col))
57+
val dist3 = TEMP(abs(col2 - col1))
58+
59+
val index1 = TEMP(DitheringTools.indexValue(fragmentCoords))
60+
val hueDiff = TEMP(dist1 / dist3)
61+
6662
SET(out, vec4(
67-
TERNARY(hueDiff.x lt INDEX1, COL1.x, COL2.x),
68-
TERNARY(hueDiff.y lt INDEX1, COL1.y, COL2.y),
69-
TERNARY(hueDiff.z lt INDEX1, COL1.z, COL2.z),
70-
TERNARY(hueDiff.w lt INDEX1, COL1.w, COL2.w),
63+
TERNARY(hueDiff.x lt index1, col1.x, col2.x),
64+
TERNARY(hueDiff.y lt index1, col1.y, col2.y),
65+
TERNARY(hueDiff.z lt index1, col1.z, col2.z),
66+
TERNARY(hueDiff.w lt index1, col1.w, col2.w),
7167
))
7268
}
7369
}

0 commit comments

Comments
 (0)