Skip to content

Commit

Permalink
Add support for Value property on ref types
Browse files Browse the repository at this point in the history
  • Loading branch information
hyazinthh committed Jan 25, 2024
1 parent 5bda0ca commit 058227d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
40 changes: 24 additions & 16 deletions src/Libs/FShade.Imperative/Frontend.fs
Original file line number Diff line number Diff line change
Expand Up @@ -340,34 +340,42 @@ module ExpressionExtensions =
let private deref = getMethodInfo <@ (!) @>
let private setref = getMethodInfo <@ (:=) @>

let (|RefExpr|_|) (e : Expr) =
match e.Type with
| Ref inner -> Some inner
| _ -> None

let (|RefOf|_|) (e : Expr) =
match e with
| Call(None, mi, [v]) when mi.IsGenericMethod && mi.GetGenericMethodDefinition() = refof ->
Some v
| _ ->
None
| Call(None, mi, [v]) when mi.IsGenericMethod && mi.GetGenericMethodDefinition() = refof ->
Some v
| _ ->
None

let (|NewRef|_|) (e : Expr) =
match e with
| Call(None, mi, [v]) when mi.IsGenericMethod && mi.GetGenericMethodDefinition() = newref ->
Some v
| _ ->
None
| Call(None, mi, [v]) when mi.IsGenericMethod && mi.GetGenericMethodDefinition() = newref ->
Some v
| _ ->
None

let (|DeRef|_|) (e : Expr) =
match e with
| Call(None, mi, [v]) when mi.IsGenericMethod && mi.GetGenericMethodDefinition() = deref ->
Some v
| _ ->
None
| Call(None, mi, [v]) when mi.IsGenericMethod && mi.GetGenericMethodDefinition() = deref ->
Some v
| PropertyGet(Some (RefExpr _ as v), pi, []) when pi.Name = "Value" ->
Some v
| _ ->
None

let (|SetRef|_|) (e : Expr) =
match e with
| Call(None, mi, [r;v]) when mi.IsGenericMethod && mi.GetGenericMethodDefinition() = setref ->
Some(r,v)
| _ ->
None
| Call(None, mi, [r;v]) when mi.IsGenericMethod && mi.GetGenericMethodDefinition() = setref ->
Some(r, v)
| PropertySet(Some (RefExpr _ as r), pi, [], v) when pi.Name = "Value" ->
Some(r, v)
| _ ->
None

type MethodInfo with
static member WriteOutputs = ShaderIO.WriteOutputsMeth
Expand Down
11 changes: 8 additions & 3 deletions src/Tests/FShade.GLSL.Tests/SimpleTests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,10 @@ let ``Bad Helpers``() =
let util (a : ref<float>) =
a := !a + 1.0

[<ReflectedDefinition>]
let util2 (a : ref<float>) =
a.Value <- a.Value + 1.0

[<Test>]
let ``Ref translated to inout``() =
Setup.Run()
Expand All @@ -368,18 +372,19 @@ let ``Ref translated to inout``() =
fragment {
let a = ref v.pos.X
util a
util2 a
return !a * v.c
}

let rx =
let rx name =
String.concat "" [
"[ \t\r\n]*void[ \t]+(.*?)_util_(.*?)\(inout[ \t]+float[ \t]+a\)"
"[ \t\r\n]*void[ \t]+(.*?)_" + name + "_(.*?)\(inout[ \t]+float[ \t]+a\)"
"[ \t\r\n]*\{"
"[ \t\r\n]*a[ \t]+\=[ \t]+\(a[ \t]+\+[ \t]+1\.0\);"
"[ \t\r\n]*\}"
]

GLSL.shouldCompileAndContainRegex [ Effect.ofFunction frag ] [rx]
GLSL.shouldCompileAndContainRegex [ Effect.ofFunction frag ] [rx "util"; rx "util2"]


[<GLSLIntrinsic("atomicAdd({0}, {1})")>]
Expand Down

0 comments on commit 058227d

Please sign in to comment.