Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #103: Missing != operator #104

Merged
merged 1 commit into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,23 @@ class CreateShaderAST[Q <: Quotes](using val qq: Q) extends ShaderMacroUtils:

case Apply(Select(term, op), xs) =>
op match
case "+" | "-" | "*" | "/" | "<" | ">" | "==" | "<=" | ">=" | "&&" | "||" =>
case "<" | "<=" | ">" | ">=" | "==" | "!=" =>
// Vector Relational Functions, according to the spec TL;DR: the left and right side types must be the same.
// However, we don't have a nice way to do that check yet.
val lhs = walkTerm(term, envVarName)
val rhs = xs.headOption.map(tt => walkTerm(tt, envVarName)).getOrElse(ShaderAST.Empty())
val rt = findReturnType(lhs)
ShaderAST.Infix(op, lhs, rhs, rt)

case "+" | "-" | "*" | "/" =>
// Math operators.
val lhs = walkTerm(term, envVarName)
val rhs = xs.headOption.map(tt => walkTerm(tt, envVarName)).getOrElse(ShaderAST.Empty())
val rt = findReturnType(lhs)
ShaderAST.Infix(op, lhs, rhs, rt)

case "&&" | "||" =>
// Logical operators.
val lhs = walkTerm(term, envVarName)
val rhs = xs.headOption.map(tt => walkTerm(tt, envVarName)).getOrElse(ShaderAST.Empty())
val rt = findReturnType(lhs)
Expand All @@ -1210,7 +1226,23 @@ class CreateShaderAST[Q <: Quotes](using val qq: Q) extends ShaderMacroUtils:

case Apply(Apply(Ident(op), List(l)), List(r)) =>
op match
case "+" | "-" | "*" | "/" | "<" | ">" | "==" | "<=" | ">=" | "&&" | "||" =>
case "<" | "<=" | ">" | ">=" | "==" | "!=" =>
// Vector Relational Functions, according to the spec. TL;DR: the left and right side types must be the same.
// However, we don't have a nice way to do that check yet.
val lhs = walkTerm(l, envVarName)
val rhs = walkTerm(r, envVarName)
val rt = findReturnType(lhs)
ShaderAST.Infix(op, lhs, rhs, rt)

case "+" | "-" | "*" | "/" =>
// Math operators.
val lhs = walkTerm(l, envVarName)
val rhs = walkTerm(r, envVarName)
val rt = findReturnType(lhs)
ShaderAST.Infix(op, lhs, rhs, rt)

case "&&" | "||" =>
// Logical operators.
val lhs = walkTerm(l, envVarName)
val rhs = walkTerm(r, envVarName)
val rt = findReturnType(lhs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,4 +371,30 @@ class GLSLIfStatementTests extends munit.FunSuite {
)
}

test("if statement with not operator comparison") {
inline def fragment: Shader[FragEnv, vec4] =
Shader { _ =>
val x = 1.0f
if x != 2.0f then vec4(10.0f) else vec4(20.0f)
}

val actual =
fragment.toGLSL[WebGL2].toOutput.code

// DebugAST.toAST(fragment)
// println(actual)

assertEquals(
actual,
s"""
|float x=1.0;
|if(x!=2.0){
| vec4(10.0);
|}else{
| vec4(20.0);
|}
|""".stripMargin.trim
)
}

}
Loading