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