VFXExpressionBuffer.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using UnityEngine;
  4. using UnityEngine.VFX;
  5. namespace UnityEditor.VFX
  6. {
  7. class VFXExpressionBufferCount : VFXExpression
  8. {
  9. public VFXExpressionBufferCount() : this(VFXValue<GraphicsBuffer>.Default)
  10. {
  11. }
  12. public VFXExpressionBufferCount(VFXExpression graphicsBuffer) : base(Flags.InvalidOnGPU, graphicsBuffer)
  13. {
  14. }
  15. public override VFXExpressionOperation operation => VFXExpressionOperation.BufferCount;
  16. protected override VFXExpression Evaluate(VFXExpression[] constParents)
  17. {
  18. var graphicsBuffer = constParents[0].Get<GraphicsBuffer>();
  19. return VFXValue.Constant<uint>(graphicsBuffer != null ? (uint)graphicsBuffer.count : 0u);
  20. }
  21. }
  22. class VFXExpressionBufferStride : VFXExpression
  23. {
  24. public VFXExpressionBufferStride() : this(VFXValue<GraphicsBuffer>.Default)
  25. {
  26. }
  27. public VFXExpressionBufferStride(VFXExpression graphicsBuffer) : base(Flags.InvalidOnGPU, graphicsBuffer)
  28. {
  29. }
  30. public override VFXExpressionOperation operation => VFXExpressionOperation.BufferStride;
  31. protected override VFXExpression Evaluate(VFXExpression[] constParents)
  32. {
  33. var graphicsBuffer = constParents[0].Get<GraphicsBuffer>();
  34. return VFXValue.Constant<uint>(graphicsBuffer != null ? (uint)graphicsBuffer.stride : 0u);
  35. }
  36. }
  37. #pragma warning disable 0659
  38. class VFXExpressionSampleBuffer : VFXExpression
  39. {
  40. public VFXExpressionSampleBuffer() : this(null, VFXValueType.None, string.Empty, VFXValue<GraphicsBuffer>.Default, VFXValue<uint>.Default, VFXValue<uint>.Default, VFXValue<uint>.Default)
  41. {
  42. }
  43. private Type m_SampledType;
  44. private VFXValueType m_FieldType;
  45. private string m_FieldPath;
  46. public Type GetSampledType()
  47. {
  48. return m_SampledType;
  49. }
  50. public VFXExpressionSampleBuffer(Type sampledType, VFXValueType fieldType, string path, VFXExpression graphicsBuffer, VFXExpression index, VFXExpression stride, VFXExpression count) : base(Flags.InvalidOnCPU, graphicsBuffer, index, stride, count)
  51. {
  52. m_SampledType = sampledType;
  53. m_FieldType = fieldType;
  54. m_FieldPath = path;
  55. }
  56. public override bool Equals(object obj)
  57. {
  58. if (!base.Equals(obj))
  59. return false;
  60. var other = obj as VFXExpressionSampleBuffer;
  61. if (other == null)
  62. return false;
  63. return m_SampledType.Equals(other.m_SampledType) && m_FieldPath.Equals(other.m_FieldPath) && m_FieldType.Equals(other.m_FieldType);
  64. }
  65. protected override int GetInnerHashCode()
  66. {
  67. int hash = base.GetInnerHashCode();
  68. hash = (hash * 397) ^ m_SampledType.GetHashCode();
  69. hash = (hash * 397) ^ m_FieldPath.GetHashCode();
  70. hash = (hash * 397) ^ m_FieldType.GetHashCode();
  71. return hash;
  72. }
  73. protected override VFXExpression Reduce(VFXExpression[] reducedParents)
  74. {
  75. var newExpression = (VFXExpressionSampleBuffer)base.Reduce(reducedParents);
  76. newExpression.m_SampledType = m_SampledType;
  77. newExpression.m_FieldPath = m_FieldPath;
  78. newExpression.m_FieldType = m_FieldType;
  79. return newExpression;
  80. }
  81. sealed public override VFXValueType valueType { get { return m_FieldType; } }
  82. sealed public override VFXExpressionOperation operation { get { return VFXExpressionOperation.None; } }
  83. public sealed override string GetCodeString(string[] parents)
  84. {
  85. var buffer = parents[0];
  86. var index = parents[1];
  87. var stride = parents[2];
  88. var count = parents[3];
  89. return string.Format("SampleStructuredBuffer({0}, {1}, {2}, {3}){4}", buffer, index, stride, count, string.IsNullOrEmpty(m_FieldPath) ? "" : "." + m_FieldPath);
  90. }
  91. }
  92. }