Skip to content

Commit

Permalink
Refactor remote visualizer stuff to support non-RGB pixel formats.
Browse files Browse the repository at this point in the history
  • Loading branch information
xian committed Aug 9, 2021
1 parent fc4a4a6 commit bd3b423
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 27 deletions.
59 changes: 32 additions & 27 deletions src/commonMain/kotlin/baaahs/gl/glsl/GlslType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, GlslType>,
Expand Down Expand Up @@ -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<String, GlslType>()

Expand Down
75 changes: 75 additions & 0 deletions src/commonTest/kotlin/baaahs/gl/glsl/GlslTypeSpec.kt
Original file line number Diff line number Diff line change
@@ -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<GlslType> {
val type by value<GlslType> { 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"
)
}
}
}
}
})

0 comments on commit bd3b423

Please sign in to comment.