@@ -4,6 +4,7 @@ import ultraviolet.DebugAST
44import ultraviolet .syntax .*
55
66import scala .annotation .nowarn
7+ import ultraviolet .macros .UBOReader
78
89@ nowarn(" msg=unused" )
910class GLSLUBOTests extends munit.FunSuite {
@@ -49,4 +50,46 @@ class GLSLUBOTests extends munit.FunSuite {
4950
5051 }
5152
53+ test(" Can define a UBO struct from a case class with a companion" ) {
54+
55+ case class UBO1 (TIME : highp[Float ], val VIEWPORT_SIZE : vec2)
56+ object UBO1 :
57+ /** Because of the initialisation order of synthesized mirror instances in companion objects, this _must_ be a
58+ * lazy val or def, or use `new UBO1(..)`.
59+ *
60+ * HOWEVER, in practice, only a def will do, because the macro can't read the member values correctly if you use
61+ * lazy val or val with new.
62+ */
63+ def reference : UBO1 =
64+ UBO1 (1.0f , vec2(0.0f ))
65+
66+ // println(UBOReader.readUBO[UBO1])
67+
68+ inline def fragment =
69+ Shader [UBO1 & FragEnv , Unit ] { env =>
70+ ubo[UBO1 ]
71+ env.COLOR = vec4(env.UV , env.TIME , 1.0f )
72+ }
73+
74+ val actual =
75+ fragment.toGLSL[WebGL2 ](Version300ES , PrecisionHighPFloat ).toOutput.code
76+
77+ // DebugAST.toAST(fragment)
78+ // println(actual)
79+
80+ assertEquals(
81+ actual,
82+ s """
83+ |#version 300 es
84+ |precision highp float;
85+ |layout (std140) uniform UBO1 {
86+ | highp float TIME;
87+ | vec2 VIEWPORT_SIZE;
88+ |};
89+ |COLOR=vec4(UV,TIME,1.0);
90+ | """ .stripMargin.trim
91+ )
92+
93+ }
94+
5295}
0 commit comments