Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RenderersParameters.cs 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.Assertions;
  4. using Unity.Collections;
  5. using Unity.Collections.LowLevel.Unsafe;
  6. using Unity.Jobs;
  7. namespace UnityEngine.Rendering
  8. {
  9. [Flags]
  10. internal enum InstanceComponentGroup : uint
  11. {
  12. Default = 1 << 0,
  13. Wind = 1 << 1,
  14. LightProbe = 1 << 2,
  15. Lightmap = 1 << 3,
  16. DefaultWind = Default | Wind,
  17. DefaultLightProbe = Default | LightProbe,
  18. DefaultLightmap = Default | Lightmap,
  19. DefaultWindLightProbe = Default | Wind | LightProbe,
  20. DefaultWindLightmap = Default | Wind | Lightmap,
  21. }
  22. internal struct RenderersParameters
  23. {
  24. static private int s_uintSize = UnsafeUtility.SizeOf<uint>();
  25. [Flags]
  26. public enum Flags
  27. {
  28. None = 0,
  29. UseBoundingSphereParameter = 1 << 0
  30. }
  31. public static class ParamNames
  32. {
  33. public static readonly int _BaseColor = Shader.PropertyToID("_BaseColor");
  34. public static readonly int unity_SpecCube0_HDR = Shader.PropertyToID("unity_SpecCube0_HDR");
  35. public static readonly int unity_SHCoefficients = Shader.PropertyToID("unity_SHCoefficients");
  36. public static readonly int unity_LightmapST = Shader.PropertyToID("unity_LightmapST");
  37. public static readonly int unity_ObjectToWorld = Shader.PropertyToID("unity_ObjectToWorld");
  38. public static readonly int unity_WorldToObject = Shader.PropertyToID("unity_WorldToObject");
  39. public static readonly int unity_MatrixPreviousM = Shader.PropertyToID("unity_MatrixPreviousM");
  40. public static readonly int unity_MatrixPreviousMI = Shader.PropertyToID("unity_MatrixPreviousMI");
  41. public static readonly int unity_WorldBoundingSphere = Shader.PropertyToID("unity_WorldBoundingSphere");
  42. public static readonly int[] DOTS_ST_WindParams = new int[(int)SpeedTreeWindParamIndex.MaxWindParamsCount];
  43. public static readonly int[] DOTS_ST_WindHistoryParams = new int[(int)SpeedTreeWindParamIndex.MaxWindParamsCount];
  44. static ParamNames()
  45. {
  46. for(int i = 0; i < (int)SpeedTreeWindParamIndex.MaxWindParamsCount; ++i)
  47. {
  48. DOTS_ST_WindParams[i] = Shader.PropertyToID($"DOTS_ST_WindParam{i}");
  49. DOTS_ST_WindHistoryParams[i] = Shader.PropertyToID($"DOTS_ST_WindHistoryParam{i}");
  50. }
  51. }
  52. }
  53. public static GPUInstanceDataBuffer CreateInstanceDataBuffer(Flags flags, in InstanceNumInfo instanceNumInfo)
  54. {
  55. using (var builder = new GPUInstanceDataBufferBuilder())
  56. {
  57. builder.AddComponent<Vector4>(ParamNames._BaseColor, isOverriden: false, isPerInstance: false, InstanceType.MeshRenderer);
  58. builder.AddComponent<Vector4>(ParamNames.unity_SpecCube0_HDR, isOverriden: false, isPerInstance: false, InstanceType.MeshRenderer);
  59. builder.AddComponent<SHCoefficients>(ParamNames.unity_SHCoefficients, isOverriden: true, isPerInstance: true, InstanceType.MeshRenderer, InstanceComponentGroup.LightProbe);
  60. builder.AddComponent<Vector4>(ParamNames.unity_LightmapST, isOverriden: true, isPerInstance: true, InstanceType.MeshRenderer, InstanceComponentGroup.Lightmap);
  61. builder.AddComponent<PackedMatrix>(ParamNames.unity_ObjectToWorld, isOverriden: true, isPerInstance: true, InstanceType.MeshRenderer);
  62. builder.AddComponent<PackedMatrix>(ParamNames.unity_WorldToObject, isOverriden: true, isPerInstance: true, InstanceType.MeshRenderer);
  63. builder.AddComponent<PackedMatrix>(ParamNames.unity_MatrixPreviousM, isOverriden: true, isPerInstance: true, InstanceType.MeshRenderer);
  64. builder.AddComponent<PackedMatrix>(ParamNames.unity_MatrixPreviousMI, isOverriden: true, isPerInstance: true, InstanceType.MeshRenderer);
  65. if ((flags & Flags.UseBoundingSphereParameter) != 0)
  66. {
  67. builder.AddComponent<Vector4>(ParamNames.unity_WorldBoundingSphere, isOverriden: true, isPerInstance: true, InstanceType.MeshRenderer);
  68. }
  69. //@ Most of SpeedTree parameters could be packed in fp16. Do later.
  70. for (int i = 0; i < (int)SpeedTreeWindParamIndex.MaxWindParamsCount; ++i)
  71. builder.AddComponent<Vector4>(ParamNames.DOTS_ST_WindParams[i], isOverriden: true, isPerInstance: true, InstanceType.SpeedTree, InstanceComponentGroup.Wind);
  72. for (int i = 0; i < (int)SpeedTreeWindParamIndex.MaxWindParamsCount; ++i)
  73. builder.AddComponent<Vector4>(ParamNames.DOTS_ST_WindHistoryParams[i], isOverriden: true, isPerInstance: true, InstanceType.SpeedTree, InstanceComponentGroup.Wind);
  74. return builder.Build(instanceNumInfo);
  75. }
  76. }
  77. public struct ParamInfo
  78. {
  79. public int index;
  80. public int gpuAddress;
  81. public int uintOffset;
  82. public bool valid => index != 0;
  83. }
  84. public ParamInfo lightmapScale;
  85. public ParamInfo localToWorld;
  86. public ParamInfo worldToLocal;
  87. public ParamInfo matrixPreviousM;
  88. public ParamInfo matrixPreviousMI;
  89. public ParamInfo shCoefficients;
  90. public ParamInfo boundingSphere;
  91. public ParamInfo[] windParams;
  92. public ParamInfo[] windHistoryParams;
  93. public RenderersParameters(in GPUInstanceDataBuffer instanceDataBuffer)
  94. {
  95. ParamInfo GetParamInfo(in GPUInstanceDataBuffer instanceDataBuffer, int paramNameIdx, bool assertOnFail = true)
  96. {
  97. int gpuAddress = instanceDataBuffer.GetGpuAddress(paramNameIdx, assertOnFail);
  98. int index = instanceDataBuffer.GetPropertyIndex(paramNameIdx, assertOnFail);
  99. return new ParamInfo()
  100. {
  101. index = index,
  102. gpuAddress = gpuAddress,
  103. uintOffset = gpuAddress / s_uintSize
  104. };
  105. }
  106. lightmapScale = GetParamInfo(instanceDataBuffer, ParamNames.unity_LightmapST);
  107. localToWorld = GetParamInfo(instanceDataBuffer, ParamNames.unity_ObjectToWorld);
  108. worldToLocal = GetParamInfo(instanceDataBuffer, ParamNames.unity_WorldToObject);
  109. matrixPreviousM = GetParamInfo(instanceDataBuffer, ParamNames.unity_MatrixPreviousM);
  110. matrixPreviousMI = GetParamInfo(instanceDataBuffer, ParamNames.unity_MatrixPreviousMI);
  111. shCoefficients = GetParamInfo(instanceDataBuffer, ParamNames.unity_SHCoefficients);
  112. boundingSphere = GetParamInfo(instanceDataBuffer, ParamNames.unity_WorldBoundingSphere, assertOnFail: false);
  113. windParams = new ParamInfo[(int)SpeedTreeWindParamIndex.MaxWindParamsCount];
  114. windHistoryParams = new ParamInfo[(int)SpeedTreeWindParamIndex.MaxWindParamsCount];
  115. for (int i = 0; i < (int)SpeedTreeWindParamIndex.MaxWindParamsCount; ++i)
  116. {
  117. windParams[i] = GetParamInfo(instanceDataBuffer, ParamNames.DOTS_ST_WindParams[i]);
  118. windHistoryParams[i] = GetParamInfo(instanceDataBuffer, ParamNames.DOTS_ST_WindHistoryParams[i]);
  119. }
  120. }
  121. }
  122. }