|
| 1 | +using System.Collections.Generic; |
| 2 | +using Unity.Collections.LowLevel.Unsafe; |
| 3 | + |
| 4 | +namespace UnityEngine.Rendering |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// Constant Buffer management class. |
| 8 | + /// </summary> |
| 9 | + public class ConstantBuffer |
| 10 | + { |
| 11 | + static List<ConstantBufferBase> m_RegisteredConstantBuffers = new List<ConstantBufferBase>(); |
| 12 | + |
| 13 | + /// <summary> |
| 14 | + /// Update the GPU data of the constant buffer and bind it globally. |
| 15 | + /// </summary> |
| 16 | + /// <typeparam name="CBType">The type of structure representing the constant buffer data.</typeparam> |
| 17 | + /// <param name="cmd">Command Buffer used to execute the graphic commands.</param> |
| 18 | + /// <param name="data">Input data of the constant buffer.</param> |
| 19 | + /// <param name="shaderId">Shader porperty id to bind the constant buffer to.</param> |
| 20 | + public static void PushGlobal<CBType>(CommandBuffer cmd, in CBType data, int shaderId) where CBType : struct |
| 21 | + { |
| 22 | + var cb = TypedConstantBuffer<CBType>.instance; |
| 23 | + |
| 24 | + cb.UpdateData(cmd, data); |
| 25 | + cb.SetGlobal(cmd, shaderId); |
| 26 | + } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Update the GPU data of the constant buffer and bind it to a compute shader. |
| 30 | + /// </summary> |
| 31 | + /// <typeparam name="CBType">The type of structure representing the constant buffer data.</typeparam> |
| 32 | + /// <param name="cmd">Command Buffer used to execute the graphic commands.</param> |
| 33 | + /// <param name="data">Input data of the constant buffer.</param> |
| 34 | + /// <param name="cs">Compute shader to which the constant buffer should be bound.</param> |
| 35 | + /// <param name="shaderId">Shader porperty id to bind the constant buffer to.</param> |
| 36 | + public static void Push<CBType>(CommandBuffer cmd, in CBType data, ComputeShader cs, int shaderId) where CBType : struct |
| 37 | + { |
| 38 | + var cb = TypedConstantBuffer<CBType>.instance; |
| 39 | + |
| 40 | + cb.UpdateData(cmd, data); |
| 41 | + cb.Set(cmd, cs, shaderId); |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Update the GPU data of the constant buffer and bind it to a material. |
| 46 | + /// </summary> |
| 47 | + /// <typeparam name="CBType">The type of structure representing the constant buffer data.</typeparam> |
| 48 | + /// <param name="cmd">Command Buffer used to execute the graphic commands.</param> |
| 49 | + /// <param name="data">Input data of the constant buffer.</param> |
| 50 | + /// <param name="mat">Material to which the constant buffer should be bound.</param> |
| 51 | + /// <param name="shaderId">Shader porperty id to bind the constant buffer to.</param> |
| 52 | + public static void Push<CBType>(CommandBuffer cmd, in CBType data, Material mat, int shaderId) where CBType : struct |
| 53 | + { |
| 54 | + var cb = TypedConstantBuffer<CBType>.instance; |
| 55 | + |
| 56 | + cb.UpdateData(cmd, data); |
| 57 | + cb.Set(mat, shaderId); |
| 58 | + } |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Update the GPU data of the constant buffer. |
| 62 | + /// </summary> |
| 63 | + /// <typeparam name="CBType">The type of structure representing the constant buffer data.</typeparam> |
| 64 | + /// <param name="cmd">Command Buffer used to execute the graphic commands.</param> |
| 65 | + /// <param name="data">Input data of the constant buffer.</param> |
| 66 | + public static void UpdateData<CBType>(CommandBuffer cmd, in CBType data) where CBType : struct |
| 67 | + { |
| 68 | + var cb = TypedConstantBuffer<CBType>.instance; |
| 69 | + |
| 70 | + cb.UpdateData(cmd, data); |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Bind the constant buffer globally. |
| 75 | + /// </summary> |
| 76 | + /// <typeparam name="CBType">The type of structure representing the constant buffer data.</typeparam> |
| 77 | + /// <param name="cmd">Command Buffer used to execute the graphic commands.</param> |
| 78 | + /// <param name="shaderId">Shader porperty id to bind the constant buffer to.</param> |
| 79 | + public static void SetGlobal<CBType>(CommandBuffer cmd, int shaderId) where CBType : struct |
| 80 | + { |
| 81 | + var cb = TypedConstantBuffer<CBType>.instance; |
| 82 | + |
| 83 | + cb.SetGlobal(cmd, shaderId); |
| 84 | + } |
| 85 | + |
| 86 | + /// <summary> |
| 87 | + /// Bind the constant buffer to a compute shader. |
| 88 | + /// </summary> |
| 89 | + /// <typeparam name="CBType">The type of structure representing the constant buffer data.</typeparam> |
| 90 | + /// <param name="cmd">Command Buffer used to execute the graphic commands.</param> |
| 91 | + /// <param name="cs">Compute shader to which the constant buffer should be bound.</param> |
| 92 | + /// <param name="shaderId">Shader porperty id to bind the constant buffer to.</param> |
| 93 | + public static void Set<CBType>(CommandBuffer cmd, ComputeShader cs, int shaderId) where CBType : struct |
| 94 | + { |
| 95 | + var cb = TypedConstantBuffer<CBType>.instance; |
| 96 | + |
| 97 | + cb.Set(cmd, cs, shaderId); |
| 98 | + } |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Bind the constant buffer to a material. |
| 102 | + /// </summary> |
| 103 | + /// <typeparam name="CBType">The type of structure representing the constant buffer data.</typeparam> |
| 104 | + /// <param name="mat">Material to which the constant buffer should be bound.</param> |
| 105 | + /// <param name="shaderId">Shader porperty id to bind the constant buffer to.</param> |
| 106 | + public static void Set<CBType>(Material mat, int shaderId) where CBType : struct |
| 107 | + { |
| 108 | + var cb = TypedConstantBuffer<CBType>.instance; |
| 109 | + |
| 110 | + cb.Set(mat, shaderId); |
| 111 | + } |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// Release all currently allocated constant buffers. |
| 115 | + /// This needs to be called before shutting down the application. |
| 116 | + /// </summary> |
| 117 | + public static void ReleaseAll() |
| 118 | + { |
| 119 | + foreach (var cb in m_RegisteredConstantBuffers) |
| 120 | + cb.Release(); |
| 121 | + |
| 122 | + m_RegisteredConstantBuffers.Clear(); |
| 123 | + } |
| 124 | + |
| 125 | + internal abstract class ConstantBufferBase |
| 126 | + { |
| 127 | + public abstract void Release(); |
| 128 | + } |
| 129 | + |
| 130 | + internal static void Register(ConstantBufferBase cb) |
| 131 | + { |
| 132 | + m_RegisteredConstantBuffers.Add(cb); |
| 133 | + } |
| 134 | + |
| 135 | + class TypedConstantBuffer<CBType> : ConstantBufferBase where CBType : struct |
| 136 | + { |
| 137 | + CBType[] m_Data = new CBType[1]; // Array is required by the ComputeBuffer SetData API |
| 138 | + static TypedConstantBuffer<CBType> s_Instance = null; |
| 139 | + internal static TypedConstantBuffer<CBType> instance |
| 140 | + { |
| 141 | + get |
| 142 | + { |
| 143 | + if (s_Instance == null) |
| 144 | + s_Instance = new TypedConstantBuffer<CBType>(); |
| 145 | + return s_Instance; |
| 146 | + } |
| 147 | + set |
| 148 | + { |
| 149 | + s_Instance = value; |
| 150 | + } |
| 151 | + } |
| 152 | + ComputeBuffer m_GPUConstantBuffer = null; |
| 153 | + |
| 154 | + TypedConstantBuffer() |
| 155 | + { |
| 156 | + m_GPUConstantBuffer = new ComputeBuffer(1, UnsafeUtility.SizeOf<CBType>(), ComputeBufferType.Constant); |
| 157 | + ConstantBuffer.Register(this); |
| 158 | + } |
| 159 | + |
| 160 | + public void UpdateData(CommandBuffer cmd, in CBType data) |
| 161 | + { |
| 162 | + m_Data[0] = data; |
| 163 | + cmd.SetComputeBufferData(m_GPUConstantBuffer, m_Data); |
| 164 | + } |
| 165 | + |
| 166 | + public void SetGlobal(CommandBuffer cmd, int shaderId) |
| 167 | + { |
| 168 | + cmd.SetGlobalConstantBuffer(m_GPUConstantBuffer, shaderId, 0, m_GPUConstantBuffer.stride); |
| 169 | + } |
| 170 | + |
| 171 | + public void Set(CommandBuffer cmd, ComputeShader cs, int shaderId) |
| 172 | + { |
| 173 | + cmd.SetComputeConstantBufferParam(cs, shaderId, m_GPUConstantBuffer, 0, m_GPUConstantBuffer.stride); |
| 174 | + } |
| 175 | + |
| 176 | + public void Set(Material mat, int shaderId) |
| 177 | + { |
| 178 | + // This isn't done via command buffer because as long as the buffer itself is not destroyed, |
| 179 | + // the binding stays valid. Only the commit of data needs to go through the command buffer. |
| 180 | + // We do it here anyway for now to simplify user API. |
| 181 | + mat.SetConstantBuffer(shaderId, m_GPUConstantBuffer, 0, m_GPUConstantBuffer.stride); |
| 182 | + } |
| 183 | + |
| 184 | + public override void Release() |
| 185 | + { |
| 186 | + CoreUtils.SafeRelease(m_GPUConstantBuffer); |
| 187 | + s_Instance = null; |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments