Skip to content
This repository was archived by the owner on Nov 20, 2025. It is now read-only.

Commit 21eca66

Browse files
Added test case for reading UBOs with companion objects
1 parent 7fae1d5 commit 21eca66

1 file changed

Lines changed: 43 additions & 0 deletions

File tree

‎ultraviolet/test/src/ultraviolet/acceptance/GLSLUBOTests.scala‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import ultraviolet.DebugAST
44
import ultraviolet.syntax.*
55

66
import scala.annotation.nowarn
7+
import ultraviolet.macros.UBOReader
78

89
@nowarn("msg=unused")
910
class 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

Comments
 (0)