diff --git a/src/commonMain/kotlin/baaahs/gl/glsl/GlslType.kt b/src/commonMain/kotlin/baaahs/gl/glsl/GlslType.kt index c2488e3f0a..085882ef2a 100644 --- a/src/commonMain/kotlin/baaahs/gl/glsl/GlslType.kt +++ b/src/commonMain/kotlin/baaahs/gl/glsl/GlslType.kt @@ -14,7 +14,39 @@ sealed class GlslType constructor( val mutableDefaultInitializer: MutablePort get() = MutableConstPort(defaultInitializer.s, this) + fun arrayOf(count: kotlin.Int): Array = Array(this, count) + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is GlslType) return false + + if (glslLiteral != other.glslLiteral) return false + + return true + } + + override fun hashCode(): kotlin.Int { + return glslLiteral.hashCode() + } + + override fun toString(): String { + return "GlslType($glslLiteral)" + } + + object Bool : GlslType("bool", GlslExpr("false")) + object Float : GlslType("float", GlslExpr("0.")) + object Matrix4 : GlslType("mat4") + object Vec2 : GlslType("vec2") + object Vec3 : GlslType("vec3") + object Vec4 : GlslType("vec4") + object Int : GlslType("int", GlslExpr("0")) + object Sampler2D : GlslType("sampler2D") + object Void : GlslType("void") + private class OtherGlslType(glslLiteral: String) : GlslType(glslLiteral) + + class Array(val type: GlslType, length: kotlin.Int) : GlslType("${type.glslLiteral}[$length]") + class Struct( val name: String, val fields: Map, @@ -81,33 +113,6 @@ sealed class GlslType constructor( } } - object Bool : GlslType("bool", GlslExpr("false")) - object Float : GlslType("float", GlslExpr("0.")) - object Matrix4 : GlslType("mat4") - object Vec2 : GlslType("vec2") - object Vec3 : GlslType("vec3") - object Vec4 : GlslType("vec4") - object Int : GlslType("int", GlslExpr("0")) - object Sampler2D : GlslType("sampler2D") - object Void : GlslType("void") - - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (other !is GlslType) return false - - if (glslLiteral != other.glslLiteral) return false - - return true - } - - override fun hashCode(): kotlin.Int { - return glslLiteral.hashCode() - } - - override fun toString(): String { - return "GlslType($glslLiteral)" - } - companion object { val types = mutableMapOf() diff --git a/src/commonTest/kotlin/baaahs/gl/glsl/GlslTypeSpec.kt b/src/commonTest/kotlin/baaahs/gl/glsl/GlslTypeSpec.kt new file mode 100644 index 0000000000..3213ec3278 --- /dev/null +++ b/src/commonTest/kotlin/baaahs/gl/glsl/GlslTypeSpec.kt @@ -0,0 +1,75 @@ +package baaahs.gl.glsl + +import baaahs.describe +import baaahs.gl.override +import baaahs.toBeSpecified +import baaahs.toEqual +import ch.tutteli.atrium.api.verbs.expect +import org.spekframework.spek2.Spek +import org.spekframework.spek2.dsl.Skip + +object GlslTypeSpec : Spek({ + describe { + val type by value { toBeSpecified() } + + describe("arrays") { + override(type) { GlslType.Int.arrayOf(8) } + + it("has valid a GLSL declaration") { + expect(type.glslLiteral) + .toEqual("int[8]") + } + } + + describe("structs") { + override(type) { + GlslType.Struct( + "FixtureInfo", + "origin" to GlslType.Vec3, + "heading" to GlslType.Vec3, + "matrix" to GlslType.Matrix4 + ) + } + + it("#toGlsl generates valid a GLSL declaration") { + expect((type as GlslType.Struct).toGlsl(GlslCode.Namespace("pfx"), emptySet())) + .toEqual( + """ + struct pfx_FixtureInfo { + vec3 origin; + vec3 heading; + mat4 matrix; + }; + """.trimIndent() + "\n\n" + ) + } + + context("with arrays") { + override(type) { + GlslType.Struct( + "FixtureInfo", + "origin" to GlslType.Vec3.arrayOf(3), + "heading" to GlslType.Vec3, + "matrix" to GlslType.Matrix4 + ) + } + + it( + "#toGlsl generates valid a GLSL declaration", + skip = Skip.Yes("Fix position of array marker in declaration.") + ) { + expect((type as GlslType.Struct).toGlsl(GlslCode.Namespace("pfx"), emptySet())) + .toEqual( + """ + struct pfx_FixtureInfo { + vec3 origin[3]; + vec3 heading; + mat4 matrix; + }; + """.trimIndent() + "\n\n" + ) + } + } + } + } +})