VFXDataOutputEvent.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.Serialization;
  6. using UnityEngine.VFX;
  7. using System.Text;
  8. namespace UnityEditor.VFX
  9. {
  10. class VFXDataOutputEvent : VFXData
  11. {
  12. public override VFXDataType type => VFXDataType.OutputEvent;
  13. public override void CopySettings<T>(T dst)
  14. {
  15. //There is nothing serialized here
  16. }
  17. public override bool CanBeCompiled()
  18. {
  19. return m_Owners.Any(o => o.inputContexts.Any());
  20. }
  21. //Shortcut to per context value
  22. public string eventName
  23. {
  24. get
  25. {
  26. if (m_Contexts.Any())
  27. {
  28. var setting = m_Contexts.First().GetSetting("eventName");
  29. return (string)setting.value;
  30. }
  31. return null;
  32. }
  33. }
  34. public override void FillDescs(
  35. VFXCompileErrorReporter reporter,
  36. List<VFXGPUBufferDesc> outBufferDescs,
  37. List<VFXTemporaryGPUBufferDesc> outTemporaryBufferDescs,
  38. List<VFXEditorSystemDesc> outSystemDescs,
  39. VFXExpressionGraph expressionGraph,
  40. Dictionary<VFXContext, VFXContextCompiledData> contextToCompiledData,
  41. Dictionary<VFXContext, int> contextSpawnToBufferIndex,
  42. VFXDependentBuffersData dependentBuffers,
  43. Dictionary<VFXContext, List<VFXContextLink>[]> effectiveFlowInputLinks,
  44. VFXSystemNames systemNames)
  45. {
  46. if (m_Contexts.Count != 1)
  47. throw new InvalidOperationException("VFXDataOutputEvent unexpected context count : " + m_Contexts.Count);
  48. if (m_Contexts.Any(o => o.contextType != VFXContextType.OutputEvent))
  49. throw new InvalidOperationException("VFXDataOutputEvent unexpected context type");
  50. var nativeName = eventName;
  51. if (outSystemDescs.Any(o => o.name == nativeName && o.type == VFXSystemType.OutputEvent))
  52. {
  53. //Check if already processed, it already present, this VFXDataOutputEvent has been gather with previous entry.
  54. return;
  55. }
  56. var allMatchingVFXOutputEvent = contextToCompiledData.Keys.Where(context =>
  57. {
  58. if (context.contextType == VFXContextType.OutputEvent)
  59. {
  60. if (((VFXDataOutputEvent)context.GetData()).eventName == nativeName)
  61. {
  62. return true;
  63. }
  64. }
  65. return false;
  66. }).ToArray();
  67. var allMatchingVFXDataOutputEvent = allMatchingVFXOutputEvent.Select(o => o.GetData()).Cast<VFXDataOutputEvent>().ToArray();
  68. var flowInputLinks = allMatchingVFXDataOutputEvent.SelectMany(data => data.m_Contexts.SelectMany(context =>
  69. {
  70. if (effectiveFlowInputLinks.ContainsKey(context))
  71. {
  72. var r = effectiveFlowInputLinks[context];
  73. return r.SelectMany(o => o);
  74. }
  75. //A context could have been filtered out due because there isn't any flow input link
  76. return Enumerable.Empty<VFXContextLink>();
  77. }));
  78. var inputSpawnerContext = flowInputLinks.Select(l => l.context).Distinct();
  79. var systemBufferMappings = new List<VFXMapping>();
  80. foreach (var spawner in inputSpawnerContext)
  81. {
  82. if (spawner.contextType != VFXContextType.Spawner)
  83. throw new InvalidOperationException("VFXDataOutputEvent unexpected link on Output event");
  84. systemBufferMappings.Add(new VFXMapping()
  85. {
  86. name = "spawner_input",
  87. index = contextSpawnToBufferIndex[spawner]
  88. });
  89. }
  90. outSystemDescs.Add(new VFXEditorSystemDesc()
  91. {
  92. flags = VFXSystemFlag.SystemDefault,
  93. name = nativeName,
  94. buffers = systemBufferMappings.ToArray(),
  95. type = VFXSystemType.OutputEvent,
  96. layer = m_Layer
  97. });
  98. }
  99. public override void GenerateAttributeLayout(Dictionary<VFXContext, List<VFXContextLink>[]> effectiveFlowInputLinks)
  100. {
  101. }
  102. public override string GetAttributeDataDeclaration(VFXAttributeMode mode)
  103. {
  104. throw new NotImplementedException();
  105. }
  106. public override VFXDeviceTarget GetCompilationTarget(VFXContext context)
  107. {
  108. return VFXDeviceTarget.CPU;
  109. }
  110. public override string GetLoadAttributeCode(VFXAttribute attrib, VFXAttributeLocation location)
  111. {
  112. throw new NotImplementedException();
  113. }
  114. public override string GetStoreAttributeCode(VFXAttribute attrib, string value)
  115. {
  116. throw new NotImplementedException();
  117. }
  118. }
  119. }