Skip to content

Commit

Permalink
Well-known uniforms used in the assignment expression of a global var…
Browse files Browse the repository at this point in the history
…iable should be identified as input ports.
  • Loading branch information
xian committed May 1, 2021
1 parent d5672a6 commit ec97780
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/commonMain/kotlin/baaahs/gl/glsl/GlslCode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import kotlinx.serialization.json.put

class GlslCode(
val src: String,
private val statements: List<GlslStatement>
val statements: List<GlslStatement>
) {
val globalVarNames = hashSetOf<String>()
val functionNames = hashSetOf<String>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ abstract class BaseShaderDialect(id: String) : ShaderDialect(id) {
open fun findWellKnownInputPorts(glslCode: GlslCode, declaredInputPorts: Set<String>): List<InputPort> {
if (wellKnownInputPorts.isEmpty()) return emptyList()

val iVars = glslCode.functions.flatMap { glslFunction ->
val iVars = glslCode.statements.flatMap { glslFunction ->
Regex("\\w+").findAll(glslFunction.fullText).map { it.value }.filter { word ->
wellKnownInputPortsById.containsKey(word)
}.toList()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package baaahs.gl.shader.dialect

import baaahs.describe
import baaahs.gl.glsl.GlslAnalyzer
import baaahs.gl.glsl.GlslError
import baaahs.gl.glsl.GlslType
import baaahs.gl.override
import baaahs.gl.patch.ContentType
import baaahs.gl.shader.InputPort
import baaahs.gl.shader.OutputPort
import baaahs.gl.testToolchain
import baaahs.show.Shader
import baaahs.toBeSpecified
import baaahs.toEqual
import ch.tutteli.atrium.api.fluent.en_GB.contains
Expand All @@ -23,11 +21,9 @@ object GenericShaderDialectSpec : Spek({
describe<GenericShaderDialect> {
val src by value<String> { toBeSpecified() }
val dialect by value { GenericShaderDialect }
val plugins by value { testToolchain.plugins }
val analyzer by value { GlslAnalyzer(plugins) }
val shaderAnalysis by value { testToolchain.analyze(Shader("Shader", src)) }
val shaderAnalysis by value { dialect.analyze(testToolchain.parse(src), testToolchain.plugins) }
val glslCode by value { shaderAnalysis.glslCode }
val openShader by value { analyzer.openShader(shaderAnalysis) }
val openShader by value { testToolchain.openShader(shaderAnalysis) }

context("a shader having a main() function") {
override(src) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package baaahs.gl.shader.dialect

import baaahs.describe
import baaahs.gl.glsl.ErrorsShaderAnalysis
import baaahs.gl.glsl.GlslAnalyzer
import baaahs.gl.glsl.GlslError
import baaahs.gl.glsl.GlslType
import baaahs.gl.override
Expand All @@ -11,7 +10,6 @@ import baaahs.gl.shader.InputPort
import baaahs.gl.shader.OutputPort
import baaahs.gl.testToolchain
import baaahs.plugin.PluginRef
import baaahs.show.Shader
import baaahs.toBeSpecified
import baaahs.toEqual
import ch.tutteli.atrium.api.fluent.en_GB.contains
Expand All @@ -28,11 +26,9 @@ object IsfShaderDialectSpec : Spek({
describe<IsfShaderDialect> {
val src by value<String> { toBeSpecified() }
val dialect by value { IsfShaderDialect }
val plugins by value { testToolchain.plugins }
val analyzer by value { GlslAnalyzer(plugins) }
val shaderAnalysis by value { testToolchain.analyze(Shader("Shader", src)) }
val shaderAnalysis by value { dialect.analyze(testToolchain.parse(src), testToolchain.plugins) }
val glslCode by value { shaderAnalysis.glslCode }
val openShader by value { analyzer.openShader(shaderAnalysis) }
val openShader by value { testToolchain.openShader(shaderAnalysis) }

context("a shader having an ISF block at the top") {
override(src) {
Expand Down Expand Up @@ -141,6 +137,22 @@ object IsfShaderDialectSpec : Spek({
InputPort("TIME", ContentType.Time, GlslType.Float, "Time", isImplicit = true)
)
}

context("from the right hand side of a global variable") {
override(src) {
"""
/*{ }*/
float theTime = TIME;
""".trimIndent()

}

it("includes it as an input port") {
expect(openShader.inputPorts).containsExactly(
InputPort("TIME", ContentType.Time, GlslType.Float, "Time", isImplicit = true)
)
}
}
}

context("with multiple out parameters") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ object ShaderToyShaderDialectSpec : Spek({
val src by value { "void mainImage(out vec4 fragColor, in vec2 fragCoord) { ... };" }
val shader by value { Shader("Title", src) }
val dialect by value { ShaderToyShaderDialect }
val shaderAnalysis by value { testToolchain.analyze(shader) }
val shaderAnalysis by value { dialect.analyze(testToolchain.parse(src), testToolchain.plugins) }
val glslCode by value { shaderAnalysis.glslCode }
val openShader by value { OpenShader.Base(shaderAnalysis, PaintShader) }
val invocationStatement by value {
Expand All @@ -34,17 +34,17 @@ object ShaderToyShaderDialectSpec : Spek({

context("shaders having a void mainImage(out vec4, in vec2) function") {
it("is an good match") {
expect(dialect.matches(glslCode)).toEqual(MatchLevel.Good)
expect(dialect.matches(shaderAnalysis.glslCode)).toEqual(MatchLevel.Good)
}

it("finds the input port") {
expect(openShader.inputPorts.str()).containsExactly(
expect(shaderAnalysis.inputPorts.str()).containsExactly(
"fragCoord uv-coordinate:vec2 (U/V Coordinates)"
)
}

it("finds the output port") {
expect(openShader.outputPort).toEqual(
expect(shaderAnalysis.outputPorts).containsExactly(
OutputPort(Color, description = "Output Color", id = "fragColor", isParam = true)
)
}
Expand Down Expand Up @@ -73,7 +73,7 @@ object ShaderToyShaderDialectSpec : Spek({
}

it("finds the input port") {
expect(openShader.inputPorts.str()).containsExactly(
expect(shaderAnalysis.inputPorts.str()).containsExactly(
"fragCoord uv-coordinate:vec2 (U/V Coordinates)",
"intensity unknown/float:float (Intensity)"
)
Expand All @@ -90,7 +90,7 @@ object ShaderToyShaderDialectSpec : Spek({
}

it("finds the input port") {
expect(openShader.inputPorts.str()).contains.inAnyOrder.only.values(
expect(shaderAnalysis.inputPorts.str()).contains.inAnyOrder.only.values(
"fragCoord uv-coordinate:vec2 (U/V Coordinates)",
"intensity unknown/float:float (Intensity)"
)
Expand All @@ -105,7 +105,7 @@ object ShaderToyShaderDialectSpec : Spek({
}

it("identifies the uniforms and maps them to the correct content types") {
expect(openShader.inputPorts.str()).contains.inAnyOrder.only.values(
expect(shaderAnalysis.inputPorts.str()).contains.inAnyOrder.only.values(
"fragCoord uv-coordinate:vec2 (U/V Coordinates)",
"iResolution resolution:vec3 (Resolution)",
"iTime time:float (Time)",
Expand Down Expand Up @@ -158,6 +158,7 @@ object ShaderToyShaderDialectSpec : Spek({
it("fails to validate") {
expect(shaderAnalysis.isValid).toBe(false)
expect(shaderAnalysis.errors).containsExactly(
GlslError("No entry point \"mainImage\" among [main]"),
GlslError("No output port found.")
)
}
Expand Down

0 comments on commit ec97780

Please sign in to comment.