Skip to content

Commit

Permalink
[Raytracing] Add duplication checks in effect builders
Browse files Browse the repository at this point in the history
  • Loading branch information
hyazinthh committed Aug 31, 2023
1 parent 6b7b996 commit 85d7c8d
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/Libs/FShade.Core/RaytracingEffect.fs
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,16 @@ module RaytracingBuilders =
if shader.Stage <> stage then
failwithf "[FShade] Expected %A shader but got %A shader." stage shader.Stage

let add (map : Map<Symbol, RaytracingShader>) =
if map.ContainsKey rayType then
failwithf "[FShade] Cannot set multiple %A shaders for ray type %A." stage rayType

map |> Map.add rayType shader

match stage with
| ShaderStage.AnyHit -> { group with AnyHit = group.AnyHit |> Map.add rayType shader }
| ShaderStage.ClosestHit -> { group with ClosestHit = group.ClosestHit |> Map.add rayType shader }
| _ -> { group with Intersection = group.Intersection |> Map.add rayType shader }
| ShaderStage.AnyHit -> { group with AnyHit = add group.AnyHit }
| ShaderStage.ClosestHit -> { group with ClosestHit = add group.ClosestHit }
| _ -> { group with Intersection = add group.Intersection }


[<CustomOperation("anyHit")>]
Expand Down Expand Up @@ -435,6 +441,9 @@ module RaytracingBuilders =
if shader.Stage <> stage then
failwithf "[FShade] Expected %A shader but got %A shader." stage shader.Stage

if shaders.ContainsKey slot then
failwithf "[FShade] Cannot set multiple shaders for slot %A." slot

shaders |> Map.add slot shader

[<CustomOperation("raygen")>]
Expand Down Expand Up @@ -576,6 +585,13 @@ module RaytracingBuilders =
member x.HitGroup(shaders : Map<ShaderSlot, RaytracingShader>, name : Symbol, group : HitGroup) =
let mutable shaders = shaders

for slot in Map.keys shaders do
match slot with
| ShaderSlot.AnyHit (group, _) | ShaderSlot.ClosestHit (group, _) | ShaderSlot.Intersection (group, _) when group = name ->
failwithf "[FShade] Cannot set multiple hit groups with name %A." name
| _ ->
()

for KeyValue(ray, shader) in group.AnyHit do
shaders <- shaders |> Map.add (ShaderSlot.AnyHit (name, ray)) shader

Expand Down

0 comments on commit 85d7c8d

Please sign in to comment.