ppemanager.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. //! Static component of PPE manager, used to hold the instance.
  2. class PPEManagerStatic
  3. {
  4. static ref PPEManager m_Manager;
  5. static void CreateManagerStatic()
  6. {
  7. if (m_Manager)
  8. {
  9. Debug.Log("PPEManagerStatic | CreateManagerStatic - PPEManager already exists");
  10. return;
  11. }
  12. m_Manager = new PPEManager;
  13. }
  14. static void DestroyManagerStatic()
  15. {
  16. if (m_Manager)
  17. {
  18. m_Manager.Cleanup();
  19. delete m_Manager;
  20. }
  21. }
  22. //! Returns the manager instance singleton
  23. static PPEManager GetPPEManager()
  24. {
  25. return m_Manager;
  26. }
  27. }
  28. /**
  29. /brief Postprocess manager, responsible for updates, receiving, and re-distributing requester data to their respective destinations.
  30. /par Basic post process flow outline:
  31. Getting a registered 'PPERequester' instance from the 'PPERequesterBank'
  32. /par
  33. Launching the requester, either through an overriden 'Start' method, or custom method with some setters (both flag it as active and to be processed)
  34. /par On render update, PPEManager:
  35. Handles queued requester changes, re-distributes individual commands to material structure
  36. /par
  37. Updates the material/parameter structure and calculates the blend values
  38. /par
  39. Sets the final values via native functions (only called once per changed parameter - optimization stonks)
  40. /note Requester serves as a centralized platform for specific effec/group of effects. Although technically the direct commands to mat/param would be feasible, this allows for easier control of effect groups,
  41. /note and clearer command hierarchy (no value setters without clear parentage).
  42. */
  43. class PPEManager extends Managed
  44. {
  45. const int CAMERA_ID = 0;
  46. protected bool m_ManagerInitialized;
  47. protected ref map<int, ref PPEClassBase> m_PPEClassMap; //contains sorted postprocess classes, IDs in 'PostProcessEffectType' // <MaterialID,<material_class>>
  48. protected ref map<int, ref array<int>> m_PPEMaterialUpdateQueueMap; //multiple levels of update queues, to allow for multiple dependent updates during same frame (greedy?)
  49. protected ref array<int> m_UpdatedMaterials;
  50. protected ref array<ref PPERequesterBase> m_ExistingPostprocessRequests; //which requests are active overall. Does not have to be updating ATM!
  51. protected ref array<ref PPERequesterBase> m_UpdatingRequests; //which requests are currently updating and processing
  52. void PPEManager()
  53. {
  54. m_ManagerInitialized = false;
  55. PPERequesterBank.Init();
  56. }
  57. void Cleanup()
  58. {
  59. PPERequesterBank.Cleanup();
  60. if (m_ManagerInitialized)
  61. {
  62. m_PPEMaterialUpdateQueueMap.Clear();
  63. m_ExistingPostprocessRequests.Clear();
  64. m_UpdatingRequests.Clear();
  65. m_PPEClassMap.Clear();
  66. }
  67. }
  68. //! Launched from 'DayZGame.DeferredInit' to make earlier access, use, and updates impossible (downside of a non-static system)
  69. void Init()
  70. {
  71. //DbgPrnt("PPEDebug | PPEManager | m_ManagerInitialized: " + m_ManagerInitialized);
  72. if (!m_ManagerInitialized)
  73. {
  74. m_PPEMaterialUpdateQueueMap = new map<int, ref array<int>>;
  75. m_UpdatedMaterials = new array<int>;
  76. m_ExistingPostprocessRequests = new array<ref PPERequesterBase>;
  77. m_UpdatingRequests = new array<ref PPERequesterBase>;
  78. InitPPEManagerClassMap();
  79. GetGame().GetUpdateQueue(CALL_CATEGORY_GUI).Insert(this.Update); //can be safely and easily 'disabled' here
  80. m_ManagerInitialized = true;
  81. }
  82. }
  83. //! Ordered by 'PostProcessEffectType' for easy access through the same enum; ID saved all the same
  84. protected void InitPPEManagerClassMap()
  85. {
  86. if (m_PPEClassMap)
  87. {
  88. delete m_PPEClassMap;
  89. }
  90. m_PPEClassMap = new map<int, ref PPEClassBase>;
  91. RegisterPPEClass(new PPENone()); //dummy
  92. RegisterPPEClass(new PPEUnderWater());
  93. RegisterPPEClass(new PPESSAO());
  94. RegisterPPEClass(new PPEDepthOfField());
  95. RegisterPPEClass(new PPEHBAO());
  96. RegisterPPEClass(new PPERotBlur());
  97. RegisterPPEClass(new PPEGodRays());
  98. RegisterPPEClass(new PPERain());
  99. RegisterPPEClass(new PPEFilmGrain());
  100. RegisterPPEClass(new PPERadialBlur());
  101. RegisterPPEClass(new PPEChromAber());
  102. RegisterPPEClass(new PPEWetDistort());
  103. RegisterPPEClass(new PPEDynamicBlur());
  104. RegisterPPEClass(new PPEColorGrading());
  105. RegisterPPEClass(new PPEColors());
  106. RegisterPPEClass(new PPEGlow());
  107. RegisterPPEClass(new PPESMAA());
  108. RegisterPPEClass(new PPEFXAA());
  109. RegisterPPEClass(new PPEMedian());
  110. RegisterPPEClass(new PPESunMask());
  111. RegisterPPEClass(new PPEGaussFilter());
  112. RegisterPPEClass(new PPEExposureNative());
  113. RegisterPPEClass(new PPEEyeAccomodationNative());
  114. RegisterPPEClass(new PPEDOF());
  115. RegisterPPEClass(new PPELightIntensityParamsNative());
  116. RegisterPPEClass(new PPEDistort());
  117. RegisterPPEClass(new PPEGhost());
  118. }
  119. //! Registeres material class and creates data structure within
  120. protected void RegisterPPEClass(PPEClassBase material_class)
  121. {
  122. m_PPEClassMap.Set(material_class.GetPostProcessEffectID(), material_class);
  123. }
  124. void SendMaterialValueData(PPERequestParamDataBase data)
  125. {
  126. //DbgPrnt("DataVerification | m_ColorTarget | SendMaterialValueData: " + PPERequestParamDataColor.Cast(data).m_ColorTarget[0] + "/" + PPERequestParamDataColor.Cast(data).m_ColorTarget[1] + "/" + PPERequestParamDataColor.Cast(data).m_ColorTarget[2] + "/" + PPERequestParamDataColor.Cast(data).m_ColorTarget[3]);
  127. PPEClassBase mat_class = m_PPEClassMap.Get(data.GetMaterialID());
  128. mat_class.InsertParamValueData(data);
  129. SetMaterialParamUpdating(data.GetMaterialID(),data.GetParameterID(),PPEConstants.DEPENDENCY_ORDER_BASE);
  130. }
  131. //! Queues material/parameter to update (once)
  132. void SetMaterialParamUpdating(int material_id, int parameter_id, int order)
  133. {
  134. if ( order > PPEConstants.DEPENDENCY_ORDER_HIGHEST )
  135. {
  136. //DbgPrnt("PPEDebug | PPEManager - SetMaterialParamUpdating | Order higher than max, ignoring! | mat/par/ord: " + material_id + "/" + parameter_id + "/" + order);
  137. return;
  138. }
  139. PPEClassBase mat_class = m_PPEClassMap.Get(material_id);
  140. //DbgPrnt("PPEDebug | PPEManager - SetMaterialParamUpdating | mat/par: " + material_id + "/" + parameter_id);
  141. //insert material into queue
  142. if ( !m_PPEMaterialUpdateQueueMap.Contains(order) )
  143. m_PPEMaterialUpdateQueueMap.Set(order,new array<int>);
  144. int found = m_PPEMaterialUpdateQueueMap.Get(order).Find(material_id);
  145. if ( found == -1 )
  146. {
  147. m_PPEMaterialUpdateQueueMap.Get(order).Insert(material_id);
  148. }
  149. mat_class.SetParameterUpdating(order,parameter_id);
  150. }
  151. //! Currently unused, requests remain in the hierarchy and are used when needed (slightly faster than constantly re-shuffilng the arrays)
  152. void RemoveMaterialUpdating(int material_id, int order = 0)
  153. {
  154. if ( m_PPEMaterialUpdateQueueMap.Contains(order) )
  155. {
  156. m_PPEMaterialUpdateQueueMap.Get(order).RemoveItem(material_id);
  157. if ( m_PPEMaterialUpdateQueueMap.Get(order).Count() == 0)
  158. m_PPEMaterialUpdateQueueMap.Remove(order);
  159. }
  160. }
  161. protected void ClearMaterialUpdating()
  162. {
  163. m_PPEMaterialUpdateQueueMap.Clear();
  164. }
  165. //! Marks requester as 'active'. Currently indistinguiishable from 'updating' requester, can potentially be used for intermittently updated requesters
  166. void SetRequestActive(PPERequesterBase request, bool active)
  167. {
  168. int found = m_ExistingPostprocessRequests.Find(request);
  169. if ( active && found == -1 )
  170. {
  171. m_ExistingPostprocessRequests.Insert(request);
  172. }
  173. else if ( !active && found > -1 ) //should always be found in this case, redundant?
  174. {
  175. //RemoveActiveRequestFromMaterials(request);
  176. m_ExistingPostprocessRequests.Remove(found);
  177. }
  178. }
  179. //! Marks requester as 'updating' and to be processed on manager update
  180. void SetRequestUpdating(PPERequesterBase request, bool active)
  181. {
  182. if (!m_UpdatingRequests)
  183. {
  184. Debug.Log("PPEManager | SetRequestUpdating | !m_UpdatingRequests");
  185. return;
  186. }
  187. int idx = m_UpdatingRequests.Find(request);
  188. if ( active && idx == -1 )
  189. {
  190. m_UpdatingRequests.Insert(request);
  191. }
  192. else if ( !active && idx > -1 )
  193. {
  194. m_UpdatingRequests.Remove(idx);
  195. }
  196. }
  197. // Just a getter
  198. bool GetExistingRequester(typename req, out PPERequesterBase ret)
  199. {
  200. int idx = m_ExistingPostprocessRequests.Find(PPERequesterBank.GetRequester(req));
  201. if (idx > -1)
  202. {
  203. ret = m_ExistingPostprocessRequests.Get(idx);
  204. return true;
  205. }
  206. return false;
  207. }
  208. bool IsAnyRequesterRunning(array<typename> requesters)
  209. {
  210. foreach (typename requesterType : requesters)
  211. {
  212. PPERequesterBase ppeRequester;
  213. GetExistingRequester(requesterType, ppeRequester);
  214. if (ppeRequester && ppeRequester.IsRequesterRunning())
  215. return true;
  216. }
  217. return false;
  218. }
  219. /**
  220. /brief Originally designed to rip the requester data from all relevant mat/params, but that proved too costly and volatile.
  221. /note Still, it is here, use at your own peril.
  222. */
  223. protected void RemoveActiveRequestFromMaterials(PPERequesterBase req)
  224. {
  225. int count = req.GetActiveRequestStructure().Count();
  226. int mat_id;
  227. for (int i = 0; i < count; i++)
  228. {
  229. mat_id = req.GetActiveRequestStructure().GetKey(i);
  230. PPEClassBase mat_class = m_PPEClassMap.Get(mat_id);
  231. mat_class.RemoveRequest(req.GetRequesterIDX());
  232. }
  233. }
  234. //! Unused cleanup method, should it be ever needed
  235. protected void RequestsCleanup()
  236. {
  237. }
  238. //! Marks material class as updated and values to be set in the course of update - 'ProcessApplyValueChanges'
  239. void InsertUpdatedMaterial(int mat_id)
  240. {
  241. if ( m_UpdatedMaterials.Find(mat_id) == -1 )
  242. m_UpdatedMaterials.Insert(mat_id);
  243. }
  244. //---------//
  245. //PROCESSING
  246. //---------//
  247. protected void ProcessRequesterUpdates(float timeslice)
  248. {
  249. PPERequesterBase req;
  250. for (int i = 0; i < m_UpdatingRequests.Count(); i++)
  251. {
  252. //DbgPrnt("PPEDebug | ProcessRequesterUpdates | m_UpdatingRequests[i]: " + m_UpdatingRequests[i]);
  253. req = m_UpdatingRequests.Get(i);
  254. if (req)
  255. req.OnUpdate(timeslice);
  256. }
  257. }
  258. protected void ProcessMaterialUpdates(float timeslice)
  259. {
  260. for (int i = 0; i < m_PPEMaterialUpdateQueueMap.Count(); i++) //orders (levels?)
  261. {
  262. //DbgPrnt("PPEDebug | ProcessMaterialUpdates | GetKey " + i + ": " + m_PPEMaterialUpdateQueueMap.GetKey(i));
  263. //DbgPrnt("PPEDebug | ProcessMaterialUpdates | GetElement - count " + i + ": " + m_PPEMaterialUpdateQueueMap.GetElement(i).Count());
  264. for (int j = 0; j < m_PPEMaterialUpdateQueueMap.GetElement(i).Count(); j++)
  265. {
  266. PPEClassBase mat_class = m_PPEClassMap.Get(m_PPEMaterialUpdateQueueMap.GetElement(i).Get(j));
  267. mat_class.OnUpdate(timeslice,i);
  268. }
  269. }
  270. }
  271. protected void ProcessApplyValueChanges()
  272. {
  273. int material_id;
  274. for (int i = 0; i < m_UpdatedMaterials.Count(); i++)
  275. {
  276. material_id = m_UpdatedMaterials.Get(i);
  277. PPEClassBase mat_class = m_PPEClassMap.Get(material_id);
  278. mat_class.ApplyValueChanges();
  279. }
  280. m_UpdatedMaterials.Clear();
  281. ClearMaterialUpdating();
  282. }
  283. void Update(float timeslice)
  284. {
  285. if (!m_ManagerInitialized)
  286. return;
  287. ProcessRequesterUpdates(timeslice);
  288. ProcessMaterialUpdates(timeslice);
  289. ProcessApplyValueChanges();
  290. RequestsCleanup(); //unused
  291. }
  292. //! Returns default values as Param. See 'PPEConstants' file for various typedefs used
  293. Param GetPostProcessDefaultValues(int material, int parameter)
  294. {
  295. PPEClassBase mat_class = m_PPEClassMap.Get(material);
  296. return mat_class.GetParameterCommandData(parameter).GetDefaultValues();
  297. }
  298. //! Returns current values as Param. See 'PPEConstants' file for various typedefs used
  299. Param GetPostProcessCurrentValues(int material, int parameter)
  300. {
  301. PPEClassBase mat_class = m_PPEClassMap.Get(material);
  302. return mat_class.GetParameterCommandData(parameter).GetCurrentValues();
  303. }
  304. //TODO - certain C++ events may change the actual material path with a graphics option changes. Reflect this on script-side!
  305. //Currently only SSAY/HBAO affected...welp.
  306. //! Changes material file associated with the script material class. Will be used very rarely, mostly set in C++ anyway.
  307. void ChangePPEMaterial(PostProcessPrioritiesCamera priority, PostProcessEffectType type, string path, bool scriptside_only)
  308. {
  309. if (m_PPEClassMap.Contains(type))
  310. {
  311. PPEClassBase mat_class = m_PPEClassMap.Get(type);
  312. typename name = mat_class.Type();
  313. PPEClassBase postprocess_capsule = PPEClassBase.Cast(name.Spawn());
  314. postprocess_capsule.ChangeMaterialPathUsed(path);
  315. if (postprocess_capsule.GetMaterial() == 0x0)
  316. {
  317. Debug.Log("PPEManager | Invalid material path " + path + " used for " + name );
  318. return;
  319. }
  320. //m_PPEClassMap.Remove(type);
  321. m_PPEClassMap.Set(type,postprocess_capsule);
  322. }
  323. //can be sent script-side only to adapt to c++ options changes
  324. if (!scriptside_only)
  325. SetCameraPostProcessEffect(CAMERA_ID,priority,type,path);
  326. }
  327. //! stops all effects of a certain kind
  328. void StopAllEffects(int mask = 0)
  329. {
  330. if (m_ExistingPostprocessRequests)
  331. {
  332. foreach (PPERequesterBase requester : m_ExistingPostprocessRequests)
  333. {
  334. if (requester.GetCategoryMask() & mask)
  335. {
  336. requester.Stop();
  337. }
  338. }
  339. }
  340. }
  341. void DbgPrnt(string text)
  342. {
  343. //Debug.Log(""+text);
  344. }
  345. };