From 85d7c8d82562678fd7bad0adc44b0a1ca2da6516 Mon Sep 17 00:00:00 2001 From: Martin Date: Thu, 31 Aug 2023 11:37:05 +0200 Subject: [PATCH] [Raytracing] Add duplication checks in effect builders --- src/Libs/FShade.Core/RaytracingEffect.fs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/Libs/FShade.Core/RaytracingEffect.fs b/src/Libs/FShade.Core/RaytracingEffect.fs index 7f02947..ecebca0 100644 --- a/src/Libs/FShade.Core/RaytracingEffect.fs +++ b/src/Libs/FShade.Core/RaytracingEffect.fs @@ -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) = + 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 } [] @@ -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 [] @@ -576,6 +585,13 @@ module RaytracingBuilders = member x.HitGroup(shaders : Map, 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