diff --git a/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.GetValue.cs b/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.GetValue.cs
index f40bf55b..411f6cb4 100644
--- a/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.GetValue.cs
+++ b/src/ComputeSharp.Shaders/Translation/Models/ReadableMember.GetValue.cs
@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Reflection.Emit;
+using System.Runtime.CompilerServices;
namespace ComputeSharp.Shaders.Translation.Models
{
@@ -19,11 +20,9 @@ namespace ComputeSharp.Shaders.Translation.Models
private Getter? _Getter;
///
- /// Returns the value of the wrapped member for the current instance
+ /// Ensures the dynamic accessor for the current instance is loaded and ready to use
///
- /// The target instance to use to read the value from
- [Pure]
- public object GetValue(object? instance)
+ public void PreloadAccessor()
{
if (_Getter == null)
{
@@ -38,10 +37,28 @@ namespace ComputeSharp.Shaders.Translation.Models
_Getter = getter = BuildDynamicGetter();
GettersMapping.Add(Id, getter);
}
-
}
+ }
- return _Getter(instance);
+ ///
+ /// Returns the value of the wrapped member for the current instance
+ ///
+ /// The target instance to use to read the value from
+ /// This method doesn't ensure that the accessor is available
+ /// The dynamic accessor is not available
+ [Pure]
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public object DangerousGetValue(object? instance) => _Getter!(instance);
+
+ ///
+ /// Returns the value of the wrapped member for the current instance
+ ///
+ /// The target instance to use to read the value from
+ [Pure]
+ public object GetValue(object? instance)
+ {
+ PreloadAccessor();
+ return _Getter!(instance);
}
///
diff --git a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs
index ed75fcee..ff2a4ae6 100644
--- a/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs
+++ b/src/ComputeSharp.Shaders/Translation/ShaderLoader.cs
@@ -88,7 +88,7 @@ namespace ComputeSharp.Shaders.Translation
int index = 1;
foreach (var item in _BufferMembers)
{
- yield return (index++, (GraphicsResource)item.GetValue(action.Target));
+ yield return (index++, (GraphicsResource)item.DangerousGetValue(action.Target));
}
}
@@ -105,7 +105,7 @@ namespace ComputeSharp.Shaders.Translation
{
foreach (var item in _VariableMembers)
{
- yield return item.GetValue(action.Target);
+ yield return item.DangerousGetValue(action.Target);
}
}
@@ -260,6 +260,14 @@ namespace ComputeSharp.Shaders.Translation
// Captured static delegates with a return type
LoadStaticMethodSource(fieldName, func.Method);
}
+
+ /* Build the dynamic IL accessor for the current member, if one is
+ * not available already. This is done during while the shader is
+ * being loaded and not just when needed because the internal cache
+ * system for the accessors is not thread safe. Preloading the accessor here
+ * leverages the external lock around the shader loading, ensuring
+ * that no race conditions occur when accessing the cached mapping */
+ memberInfo.PreloadAccessor();
}
///