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.

ConstantBuffer.cs 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. using System.Collections.Generic;
  2. using Unity.Collections.LowLevel.Unsafe;
  3. namespace UnityEngine.Rendering
  4. {
  5. /// <summary>
  6. /// Constant Buffer management class.
  7. /// </summary>
  8. public class ConstantBuffer
  9. {
  10. static List<ConstantBufferBase> m_RegisteredConstantBuffers = new List<ConstantBufferBase>();
  11. /// <summary>
  12. /// Update the GPU data of the constant buffer and bind it globally via a command buffer.
  13. /// </summary>
  14. /// <typeparam name="CBType">The type of structure representing the constant buffer data.</typeparam>
  15. /// <param name="cmd">Command Buffer used to execute the graphic commands.</param>
  16. /// <param name="data">Input data of the constant buffer.</param>
  17. /// <param name="shaderId">Shader porperty id to bind the constant buffer to.</param>
  18. public static void PushGlobal<CBType>(CommandBuffer cmd, in CBType data, int shaderId) where CBType : struct
  19. {
  20. var cb = ConstantBufferSingleton<CBType>.instance;
  21. cb.UpdateData(cmd, data);
  22. cb.SetGlobal(cmd, shaderId);
  23. }
  24. /// <summary>
  25. /// Update the GPU data of the constant buffer and bind it globally.
  26. /// </summary>
  27. /// <typeparam name="CBType">The type of structure representing the constant buffer data.</typeparam>
  28. /// <param name="data">Input data of the constant buffer.</param>
  29. /// <param name="shaderId">Shader porperty id to bind the constant buffer to.</param>
  30. public static void PushGlobal<CBType>(in CBType data, int shaderId) where CBType : struct
  31. {
  32. var cb = ConstantBufferSingleton<CBType>.instance;
  33. cb.UpdateData(data);
  34. cb.SetGlobal(shaderId);
  35. }
  36. /// <summary>
  37. /// Update the GPU data of the constant buffer and bind it to a compute shader via a command buffer.
  38. /// </summary>
  39. /// <typeparam name="CBType">The type of structure representing the constant buffer data.</typeparam>
  40. /// <param name="cmd">Command Buffer used to execute the graphic commands.</param>
  41. /// <param name="data">Input data of the constant buffer.</param>
  42. /// <param name="cs">Compute shader to which the constant buffer should be bound.</param>
  43. /// <param name="shaderId">Shader porperty id to bind the constant buffer to.</param>
  44. public static void Push<CBType>(CommandBuffer cmd, in CBType data, ComputeShader cs, int shaderId) where CBType : struct
  45. {
  46. var cb = ConstantBufferSingleton<CBType>.instance;
  47. cb.UpdateData(cmd, data);
  48. cb.Set(cmd, cs, shaderId);
  49. }
  50. /// <summary>
  51. /// Update the GPU data of the constant buffer and bind it to a compute shader.
  52. /// </summary>
  53. /// <typeparam name="CBType">The type of structure representing the constant buffer data.</typeparam>
  54. /// <param name="data">Input data of the constant buffer.</param>
  55. /// <param name="cs">Compute shader to which the constant buffer should be bound.</param>
  56. /// <param name="shaderId">Shader porperty id to bind the constant buffer to.</param>
  57. public static void Push<CBType>(in CBType data, ComputeShader cs, int shaderId) where CBType : struct
  58. {
  59. var cb = ConstantBufferSingleton<CBType>.instance;
  60. cb.UpdateData(data);
  61. cb.Set(cs, shaderId);
  62. }
  63. /// <summary>
  64. /// Update the GPU data of the constant buffer and bind it to a material via a command buffer.
  65. /// </summary>
  66. /// <typeparam name="CBType">The type of structure representing the constant buffer data.</typeparam>
  67. /// <param name="cmd">Command Buffer used to execute the graphic commands.</param>
  68. /// <param name="data">Input data of the constant buffer.</param>
  69. /// <param name="mat">Material to which the constant buffer should be bound.</param>
  70. /// <param name="shaderId">Shader porperty id to bind the constant buffer to.</param>
  71. public static void Push<CBType>(CommandBuffer cmd, in CBType data, Material mat, int shaderId) where CBType : struct
  72. {
  73. var cb = ConstantBufferSingleton<CBType>.instance;
  74. cb.UpdateData(cmd, data);
  75. cb.Set(mat, shaderId);
  76. }
  77. /// <summary>
  78. /// Update the GPU data of the constant buffer and bind it to a material.
  79. /// </summary>
  80. /// <typeparam name="CBType">The type of structure representing the constant buffer data.</typeparam>
  81. /// <param name="data">Input data of the constant buffer.</param>
  82. /// <param name="mat">Material to which the constant buffer should be bound.</param>
  83. /// <param name="shaderId">Shader porperty id to bind the constant buffer to.</param>
  84. public static void Push<CBType>(in CBType data, Material mat, int shaderId) where CBType : struct
  85. {
  86. var cb = ConstantBufferSingleton<CBType>.instance;
  87. cb.UpdateData(data);
  88. cb.Set(mat, shaderId);
  89. }
  90. /// <summary>
  91. /// Update the GPU data of the constant buffer via a command buffer.
  92. /// </summary>
  93. /// <typeparam name="CBType">The type of structure representing the constant buffer data.</typeparam>
  94. /// <param name="cmd">Command Buffer used to execute the graphic commands.</param>
  95. /// <param name="data">Input data of the constant buffer.</param>
  96. public static void UpdateData<CBType>(CommandBuffer cmd, in CBType data) where CBType : struct
  97. {
  98. var cb = ConstantBufferSingleton<CBType>.instance;
  99. cb.UpdateData(cmd, data);
  100. }
  101. /// <summary>
  102. /// Update the GPU data of the constant buffer.
  103. /// </summary>
  104. /// <typeparam name="CBType">The type of structure representing the constant buffer data.</typeparam>
  105. /// <param name="data">Input data of the constant buffer.</param>
  106. public static void UpdateData<CBType>(in CBType data) where CBType : struct
  107. {
  108. var cb = ConstantBufferSingleton<CBType>.instance;
  109. cb.UpdateData(data);
  110. }
  111. /// <summary>
  112. /// Bind the constant buffer globally via a command buffer.
  113. /// </summary>
  114. /// <typeparam name="CBType">The type of structure representing the constant buffer data.</typeparam>
  115. /// <param name="cmd">Command Buffer used to execute the graphic commands.</param>
  116. /// <param name="shaderId">Shader porperty id to bind the constant buffer to.</param>
  117. public static void SetGlobal<CBType>(CommandBuffer cmd, int shaderId) where CBType : struct
  118. {
  119. var cb = ConstantBufferSingleton<CBType>.instance;
  120. cb.SetGlobal(cmd, shaderId);
  121. }
  122. /// <summary>
  123. /// Bind the constant buffer globally.
  124. /// </summary>
  125. /// <typeparam name="CBType">The type of structure representing the constant buffer data.</typeparam>
  126. /// <param name="shaderId">Shader porperty id to bind the constant buffer to.</param>
  127. public static void SetGlobal<CBType>(int shaderId) where CBType : struct
  128. {
  129. var cb = ConstantBufferSingleton<CBType>.instance;
  130. cb.SetGlobal(shaderId);
  131. }
  132. /// <summary>
  133. /// Bind the constant buffer to a compute shader via a command buffer.
  134. /// </summary>
  135. /// <typeparam name="CBType">The type of structure representing the constant buffer data.</typeparam>
  136. /// <param name="cmd">Command Buffer used to execute the graphic commands.</param>
  137. /// <param name="cs">Compute shader to which the constant buffer should be bound.</param>
  138. /// <param name="shaderId">Shader porperty id to bind the constant buffer to.</param>
  139. public static void Set<CBType>(CommandBuffer cmd, ComputeShader cs, int shaderId) where CBType : struct
  140. {
  141. var cb = ConstantBufferSingleton<CBType>.instance;
  142. cb.Set(cmd, cs, shaderId);
  143. }
  144. /// <summary>
  145. /// Bind the constant buffer to a compute shader.
  146. /// </summary>
  147. /// <typeparam name="CBType">The type of structure representing the constant buffer data.</typeparam>
  148. /// <param name="cs">Compute shader to which the constant buffer should be bound.</param>
  149. /// <param name="shaderId">Shader porperty id to bind the constant buffer to.</param>
  150. public static void Set<CBType>(ComputeShader cs, int shaderId) where CBType : struct
  151. {
  152. var cb = ConstantBufferSingleton<CBType>.instance;
  153. cb.Set(cs, shaderId);
  154. }
  155. /// <summary>
  156. /// Bind the constant buffer to a material.
  157. /// </summary>
  158. /// <typeparam name="CBType">The type of structure representing the constant buffer data.</typeparam>
  159. /// <param name="mat">Material to which the constant buffer should be bound.</param>
  160. /// <param name="shaderId">Shader porperty id to bind the constant buffer to.</param>
  161. public static void Set<CBType>(Material mat, int shaderId) where CBType : struct
  162. {
  163. var cb = ConstantBufferSingleton<CBType>.instance;
  164. cb.Set(mat, shaderId);
  165. }
  166. /// <summary>
  167. /// Release all currently allocated singleton constant buffers.
  168. /// This needs to be called before shutting down the application.
  169. /// </summary>
  170. public static void ReleaseAll()
  171. {
  172. foreach (var cb in m_RegisteredConstantBuffers)
  173. cb.Release();
  174. m_RegisteredConstantBuffers.Clear();
  175. }
  176. internal static void Register(ConstantBufferBase cb)
  177. {
  178. m_RegisteredConstantBuffers.Add(cb);
  179. }
  180. }
  181. /// <summary>
  182. /// The base class of Constant Buffer.
  183. /// </summary>
  184. public abstract class ConstantBufferBase
  185. {
  186. /// <summary>
  187. /// Release the constant buffer.
  188. /// </summary>
  189. public abstract void Release();
  190. }
  191. /// <summary>
  192. /// An instance of a constant buffer.
  193. /// </summary>
  194. /// <typeparam name="CBType">The type of structure representing the constant buffer data.</typeparam>
  195. public class ConstantBuffer<CBType> : ConstantBufferBase where CBType : struct
  196. {
  197. // Used to track all global bindings used by this CB type.
  198. HashSet<int> m_GlobalBindings = new HashSet<int>();
  199. // Array is required by the ComputeBuffer SetData API
  200. CBType[] m_Data = new CBType[1];
  201. ComputeBuffer m_GPUConstantBuffer = null;
  202. /// <summary>
  203. /// Constant Buffer constructor.
  204. /// </summary>
  205. public ConstantBuffer()
  206. {
  207. m_GPUConstantBuffer = new ComputeBuffer(1, UnsafeUtility.SizeOf<CBType>(), ComputeBufferType.Constant);
  208. }
  209. /// <summary>
  210. /// Update the GPU data of the constant buffer via a command buffer.
  211. /// </summary>
  212. /// <param name="cmd">Command Buffer used to execute the graphic commands.</param>
  213. /// <param name="data">Input data of the constant buffer.</param>
  214. public void UpdateData(CommandBuffer cmd, in CBType data)
  215. {
  216. m_Data[0] = data;
  217. #if UNITY_2021_1_OR_NEWER
  218. cmd.SetBufferData(m_GPUConstantBuffer, m_Data);
  219. #else
  220. cmd.SetComputeBufferData(m_GPUConstantBuffer, m_Data);
  221. #endif
  222. }
  223. /// <summary>
  224. /// Update the GPU data of the constant buffer.
  225. /// </summary>
  226. /// <param name="data">Input data of the constant buffer.</param>
  227. public void UpdateData(in CBType data)
  228. {
  229. m_Data[0] = data;
  230. m_GPUConstantBuffer.SetData(m_Data);
  231. }
  232. /// <summary>
  233. /// Bind the constant buffer globally via a command buffer.
  234. /// </summary>
  235. /// <param name="cmd">Command Buffer used to execute the graphic commands.</param>
  236. /// <param name="shaderId">Shader porperty id to bind the constant buffer to.</param>
  237. public void SetGlobal(CommandBuffer cmd, int shaderId)
  238. {
  239. m_GlobalBindings.Add(shaderId);
  240. cmd.SetGlobalConstantBuffer(m_GPUConstantBuffer, shaderId, 0, m_GPUConstantBuffer.stride);
  241. }
  242. /// <summary>
  243. /// Bind the constant buffer globally.
  244. /// </summary>
  245. /// <param name="shaderId">Shader porperty id to bind the constant buffer to.</param>
  246. public void SetGlobal(int shaderId)
  247. {
  248. m_GlobalBindings.Add(shaderId);
  249. Shader.SetGlobalConstantBuffer(shaderId, m_GPUConstantBuffer, 0, m_GPUConstantBuffer.stride);
  250. }
  251. /// <summary>
  252. /// Bind the constant buffer to a compute shader via a command buffer.
  253. /// </summary>
  254. /// <param name="cmd">Command Buffer used to execute the graphic commands.</param>
  255. /// <param name="cs">Compute shader to which the constant buffer should be bound.</param>
  256. /// <param name="shaderId">Shader porperty id to bind the constant buffer to.</param>
  257. public void Set(CommandBuffer cmd, ComputeShader cs, int shaderId)
  258. {
  259. cmd.SetComputeConstantBufferParam(cs, shaderId, m_GPUConstantBuffer, 0, m_GPUConstantBuffer.stride);
  260. }
  261. /// <summary>
  262. /// Bind the constant buffer to a compute shader.
  263. /// </summary>
  264. /// <param name="cs">Compute shader to which the constant buffer should be bound.</param>
  265. /// <param name="shaderId">Shader porperty id to bind the constant buffer to.</param>
  266. public void Set(ComputeShader cs, int shaderId)
  267. {
  268. cs.SetConstantBuffer(shaderId, m_GPUConstantBuffer, 0, m_GPUConstantBuffer.stride);
  269. }
  270. /// <summary>
  271. /// Bind the constant buffer to a material.
  272. /// </summary>
  273. /// <param name="mat">Material to which the constant buffer should be bound.</param>
  274. /// <param name="shaderId">Shader porperty id to bind the constant buffer to.</param>
  275. public void Set(Material mat, int shaderId)
  276. {
  277. // This isn't done via command buffer because as long as the buffer itself is not destroyed,
  278. // the binding stays valid. Only the commit of data needs to go through the command buffer.
  279. // We do it here anyway for now to simplify user API.
  280. mat.SetConstantBuffer(shaderId, m_GPUConstantBuffer, 0, m_GPUConstantBuffer.stride);
  281. }
  282. /// <summary>
  283. /// Bind the constant buffer to a material property block.
  284. /// </summary>
  285. /// <param name="mpb">Material property block to which the constant buffer should be bound.</param>
  286. /// <param name="shaderId">Shader porperty id to bind the constant buffer to.</param>
  287. public void Set(MaterialPropertyBlock mpb, int shaderId)
  288. {
  289. mpb.SetConstantBuffer(shaderId, m_GPUConstantBuffer, 0, m_GPUConstantBuffer.stride);
  290. }
  291. /// <summary>
  292. /// Update the GPU data of the constant buffer and bind it globally via a command buffer.
  293. /// </summary>
  294. /// <param name="cmd">Command Buffer used to execute the graphic commands.</param>
  295. /// <param name="data">Input data of the constant buffer.</param>
  296. /// <param name="shaderId">Shader porperty id to bind the constant buffer to.</param>
  297. public void PushGlobal(CommandBuffer cmd, in CBType data, int shaderId)
  298. {
  299. UpdateData(cmd, data);
  300. SetGlobal(cmd, shaderId);
  301. }
  302. /// <summary>
  303. /// Update the GPU data of the constant buffer and bind it globally.
  304. /// </summary>
  305. /// <param name="data">Input data of the constant buffer.</param>
  306. /// <param name="shaderId">Shader porperty id to bind the constant buffer to.</param>
  307. public void PushGlobal(in CBType data, int shaderId)
  308. {
  309. UpdateData(data);
  310. SetGlobal(shaderId);
  311. }
  312. /// <summary>
  313. /// Release the constant buffers.
  314. /// </summary>
  315. public override void Release()
  316. {
  317. // Depending on the device, globally bound buffers can leave stale "valid" shader ids pointing to a destroyed buffer.
  318. // In DX11 it does not cause issues but on Vulkan this will result in skipped drawcalls (even if the buffer is not actually accessed in the shader).
  319. // To avoid this kind of issues, it's good practice to "unbind" all globally bound buffers upon destruction.
  320. foreach (int shaderId in m_GlobalBindings)
  321. Shader.SetGlobalConstantBuffer(shaderId, (ComputeBuffer)null, 0, 0);
  322. m_GlobalBindings.Clear();
  323. CoreUtils.SafeRelease(m_GPUConstantBuffer);
  324. }
  325. }
  326. class ConstantBufferSingleton<CBType> : ConstantBuffer<CBType> where CBType : struct
  327. {
  328. static ConstantBufferSingleton<CBType> s_Instance = null;
  329. internal static ConstantBufferSingleton<CBType> instance
  330. {
  331. get
  332. {
  333. if (s_Instance == null)
  334. {
  335. s_Instance = new ConstantBufferSingleton<CBType>();
  336. ConstantBuffer.Register(s_Instance);
  337. }
  338. return s_Instance;
  339. }
  340. set
  341. {
  342. s_Instance = value;
  343. }
  344. }
  345. public override void Release()
  346. {
  347. base.Release();
  348. s_Instance = null;
  349. }
  350. }
  351. }