Skip to content

Commit 4c6d5c5

Browse files
PaulDemeulenaereEvergreen
authored andcommitted
[VFX] Collect CustomHLSL Per Context
Fix specific failure scenario (initially identified by @ludovic-theobald during GDC) which is a consequence of bad fetching of customhlsl expression: Instead of per system, it should be done per context. It has two effects: - Fix attribute generation: modifying non stored variable was leading to compilation failure. - Allow diverging Buffer usage: Same buffer used as RWBuffer & Buffer in two separate context. 🎁 Fix untracked issue about multiple include within CustomHLSL (see 813e3a7dff57e3192c35f0dd88a248a48de37572)
1 parent 03f80d5 commit 4c6d5c5

File tree

8 files changed

+494
-151
lines changed

8 files changed

+494
-151
lines changed

Packages/com.unity.visualeffectgraph/Editor/Compiler/VFXExpressionGraph.cs

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ private void CompileExpressionContext(IEnumerable<VFXContext> contexts,
5656
if (mapper != null)
5757
{
5858
foreach (var exp in mapper.expressions)
59-
expressionContext.RegisterExpression(exp);
59+
expressionContext.RegisterExpression(exp, context);
6060
contextsToExpressions.Add(context, mapper);
6161
}
6262
}
@@ -82,22 +82,18 @@ private void CompileExpressionContext(IEnumerable<VFXContext> contexts,
8282
foreach (var exp in expressionsToReduced.Values)
8383
AddExpressionDataRecursively(m_ExpressionsData, exp);
8484

85-
var bufferUsage = m_BufferUsage
86-
.Concat(expressionContext.GraphicsBufferUsageType)
87-
.GroupBy(o => o.Key).ToArray();
88-
89-
m_BufferUsage.Clear();
90-
foreach (var expression in bufferUsage)
85+
foreach (var bufferTypeUsage in expressionContext.GraphicsBufferTypeUsagePerContext)
9186
{
92-
var types = expression.Select(o => o.Value);
93-
if (types.Count() != 1)
94-
throw new InvalidOperationException("Diverging type usage for GraphicsBuffer : " + types.Select(o => o.ToString()).Aggregate((a, b) => a + b));
95-
m_BufferUsage.Add(expression.Key, types.First());
87+
m_BufferTypeUsagePerContext.TryAdd(bufferTypeUsage.Key, bufferTypeUsage.Value);
9688
}
9789

90+
9891
if (target == VFXDeviceTarget.GPU)
9992
{
100-
m_CustomHLSLExpressions = expressionContext.hlslCodeHolders;
93+
foreach (var hlslCodeHolder in expressionContext.hlslCodeHoldersPerContext)
94+
{
95+
m_CustomHLSLExpressionsPerContext.Add(hlslCodeHolder.Key, hlslCodeHolder.Value);
96+
}
10197
}
10298
}
10399

@@ -294,12 +290,27 @@ private VFXExpressionMapper BuildMapper(VFXContext context, Dictionary<VFXContex
294290

295291
public IEnumerable<VFXLayoutElementDesc> GlobalEventAttributes => m_GlobalEventAttributes;
296292

297-
public ReadOnlyDictionary<VFXExpression, BufferUsage> BufferUsage => new ReadOnlyDictionary<VFXExpression, BufferUsage>(m_BufferUsage);
293+
public ReadOnlyDictionary<VFXExpression, BufferUsage> GetBufferTypeUsage(VFXContext context)
294+
{
295+
if (m_BufferTypeUsagePerContext.TryGetValue(context, out var bufferTypeUsage))
296+
{
297+
return new ReadOnlyDictionary<VFXExpression, BufferUsage>(bufferTypeUsage);
298+
}
298299

299-
public IHLSLCodeHolder[] customHLSLExpressions => m_CustomHLSLExpressions;
300+
return new ReadOnlyDictionary<VFXExpression, BufferUsage>(new Dictionary<VFXExpression, BufferUsage>());
301+
}
302+
303+
public IHLSLCodeHolder[] GetCustomHLSLExpressions(VFXContext context)
304+
{
305+
if (m_CustomHLSLExpressionsPerContext.TryGetValue(context, out var hlslCodeHolders))
306+
{
307+
return hlslCodeHolders.ToArray();
308+
}
309+
return Array.Empty<IHLSLCodeHolder>();
310+
}
300311

301-
private IHLSLCodeHolder[] m_CustomHLSLExpressions;
302-
private Dictionary<VFXExpression, BufferUsage> m_BufferUsage = new Dictionary<VFXExpression, BufferUsage>();
312+
private Dictionary<VFXContext, List<IHLSLCodeHolder>> m_CustomHLSLExpressionsPerContext = new();
313+
private Dictionary<VFXContext, Dictionary<VFXExpression, BufferUsage>> m_BufferTypeUsagePerContext = new();
303314
private HashSet<VFXExpression> m_Expressions = new HashSet<VFXExpression>();
304315
private Dictionary<VFXExpression, VFXExpression> m_CPUExpressionsToReduced = new Dictionary<VFXExpression, VFXExpression>();
305316
private Dictionary<VFXExpression, VFXExpression> m_GPUExpressionsToReduced = new Dictionary<VFXExpression, VFXExpression>();

Packages/com.unity.visualeffectgraph/Editor/Compiler/VFXGraphCompiledData.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -842,7 +842,7 @@ private void GenerateShaders(List<GeneratedCodeData> outGeneratedCodeData, VFXEx
842842
var contextData = compiledData.taskToCompiledData[task];
843843
contextData.gpuMapper = gpuMapper;
844844
contextData.uniformMapper = uniformMapper;
845-
contextData.bufferUsage = graph.BufferUsage;
845+
contextData.bufferUsage = graph.GetBufferTypeUsage(context);
846846

847847
if (task.doesGenerateShader)
848848
{
@@ -1183,7 +1183,7 @@ public void Compile(VFXCompilationMode compilationMode, bool forceShaderValidati
11831183
foreach (var task in contextCompiledData.tasks)
11841184
{
11851185
var contextData = new VFXTaskCompiledData() { indexInShaderSource = -1 };
1186-
contextData.hlslCodeHolders = m_ExpressionGraph.customHLSLExpressions;
1186+
contextData.hlslCodeHolders = m_ExpressionGraph.GetCustomHLSLExpressions(context);
11871187
contextData.cpuMapper = cpuMapper;
11881188
contextData.parameters = context.additionalMappings.ToArray();
11891189
contextData.linkedEventOut = ComputeEventListFromSlot(context.allLinkedOutputSlot).ToArray();

0 commit comments

Comments
 (0)