1
0

BulbRenderer.gml 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /// Constructor. Creates a Bulb renderer struct that is responsible for the final renderering of
  2. /// lights in your game.
  3. ///
  4. /// @param [camera]
  5. ///
  6. /// Full list of variables:
  7. ///
  8. /// `.ambientColor` | `c_black` | Baseline ambient light color
  9. /// `.ambientInGammaSpace` | `false` | Whether the above is in gamma space (`true`) or linear space {`false`)
  10. /// `.smooth` | `true` | Whether to use texture filtering (bilinear interpolation) where possible
  11. /// `.soft` | `true` | Whether to use soft shadows
  12. /// `.selfLighting` | `false` | Whether to allow light to enter but not escape occluders. Hard shadow mode only
  13. /// `.exposure` | `1.0` | Exposure for the entire lighting render. Should usually be left at `1.0` when not in HDR mode
  14. /// `.ldrTonemap` | `BULB_TONEMAP_NONE` | Tonemap to use when not in HDR mode. Should usually be left at `BULB_TONEMAP_NONE`
  15. /// `.hdr` | `false` | Whether to use HDR rendering or not. HDR surface is 16-bit
  16. /// `.hdrTonemap` | `BULB_TONEMAP_HBD` | Tonemap to use when in HDR mode
  17. /// `.hdrBloomIntensity` | `0` | Intensity of the bloom effect
  18. /// `.hdrBloomIterations` | `3` | Number of Kawase blur iterations to apply to the bloom
  19. /// `.hdrBloomThresholdMin` | `0.6` | Lower threshold for bloom cut-off
  20. /// `.hdrBloomThresholdMax` | `0.8` | Upper threshold for bloom cut-off
  21. /// `.normalMap` | Config macro | Whether normal mapping should be used. Defaults to `BULB_DEFAULT_USE_NORMAL_MAP`
  22. ///
  23. /// Full list of methods:
  24. ///
  25. /// `.Free()`
  26. /// `.SetCamera(camera)`
  27. /// `.GetCamera()`
  28. /// `.SetSurfaceDimensions(width, height)`
  29. /// `.GetSurfaceDimensions()`
  30. /// `.Update()`
  31. /// `.GetOutputSurface(surface)`
  32. /// `.DrawLitSurface(surface, x, y, width, height, [textureFiltering], [alphaBlend])`
  33. /// `.GetTonemap()`
  34. /// `.GetLightSurface()`
  35. /// `.GetLightValue(x, y)`
  36. /// `.GetNormalMapSurface()
  37. /// `.DrawNormalMapDebug(x, y, width, height)`
  38. /// `.RefreshStaticOccluders()`
  39. function BulbRenderer(_camera) constructor
  40. {
  41. static _system = __BulbSystem();
  42. static _vformat3DNormal = (function()
  43. {
  44. vertex_format_begin();
  45. vertex_format_add_position_3d();
  46. vertex_format_add_normal();
  47. return vertex_format_end();
  48. })();
  49. static _vformat3DNormalTex = (function()
  50. {
  51. vertex_format_begin();
  52. vertex_format_add_position_3d();
  53. vertex_format_add_normal();
  54. vertex_format_add_texcoord();
  55. return vertex_format_end();
  56. })();
  57. SetCamera = function(_camera)
  58. {
  59. if (__cameraImplicit)
  60. {
  61. camera_destroy(__camera);
  62. }
  63. if (_camera != undefined)
  64. {
  65. __camera = _camera;
  66. __cameraImplicit = false;
  67. }
  68. else
  69. {
  70. __camera = camera_create_view(0, 0, room_width, room_height, 0, noone, 0, 0, 0, 0);
  71. __cameraImplicit = true;
  72. }
  73. //Set the lighting surface dimensions from the camera
  74. var _projMatrix = camera_get_proj_mat(__camera);
  75. var _width = round(abs(2/_projMatrix[0]));
  76. var _height = round(abs(2/_projMatrix[5]));
  77. SetSurfaceDimensions(_width, _height);
  78. }
  79. GetCamera = function()
  80. {
  81. return __camera;
  82. }
  83. SetSurfaceDimensions = function(_width, _height)
  84. {
  85. __surfaceWidth = _width;
  86. __surfaceHeight = _height;
  87. GetLightSurface();
  88. }
  89. GetSurfaceDimensions = function()
  90. {
  91. static _result = {};
  92. _result.width = __surfaceWidth;
  93. _result.height = __surfaceHeight;
  94. return _result;
  95. }
  96. Update = function()
  97. {
  98. //Deploy PROPER MATHS in case the dev is using matrices
  99. var _viewMatrix = camera_get_view_mat(__camera);
  100. var _projMatrix = camera_get_proj_mat(__camera);
  101. var _cameraCos = _viewMatrix[ 0];
  102. var _cameraSin = _viewMatrix[ 1];
  103. var _matrixX = -_viewMatrix[12];
  104. var _matrixY = -_viewMatrix[13];
  105. var _cameraCX = _matrixX*_cameraCos + _matrixY*_cameraSin;
  106. var _cameraCY = -_matrixX*_cameraSin + _matrixY*_cameraCos;
  107. var _cameraW = round(abs(2/_projMatrix[0]));
  108. var _cameraH = round(abs(2/_projMatrix[5]));
  109. var _rotatedW = _cameraW*abs(_cameraCos) + _cameraH*abs(_cameraSin);
  110. var _rotatedH = _cameraW*abs(_cameraSin) + _cameraH*abs(_cameraCos);
  111. var _boundaryL = _cameraCX - _rotatedW/2;
  112. var _boundaryT = _cameraCY - _rotatedH/2;
  113. var _boundaryR = _cameraCX + _rotatedW/2;
  114. var _boundaryB = _cameraCY + _rotatedH/2;
  115. var _boundaryExpandedL = _boundaryL - BULB_DYNAMIC_OCCLUDER_RANGE;
  116. var _boundaryExpandedT = _boundaryT - BULB_DYNAMIC_OCCLUDER_RANGE;
  117. var _boundaryExpandedR = _boundaryR + BULB_DYNAMIC_OCCLUDER_RANGE;
  118. var _boundaryExpandedB = _boundaryB + BULB_DYNAMIC_OCCLUDER_RANGE;
  119. //Force a regeneration of vertex buffers if we're swapped between hard/soft lights
  120. if (soft != __oldSoft)
  121. {
  122. __oldSoft = soft;
  123. __FreeVertexBuffers();
  124. }
  125. //Determine whether we actually want HDR
  126. var _hdr = (hdr && _system.__hdrAvailable);
  127. //Force regeneration/freeing of surfaces if the HDR state has changed
  128. if (_hdr != __oldHDR)
  129. {
  130. __oldHDR = _hdr;
  131. if (__lightSurface != undefined)
  132. {
  133. surface_free(__lightSurface);
  134. __lightSurface = undefined;
  135. }
  136. if (not _hdr)
  137. {
  138. __FreeOutputSurface();
  139. }
  140. }
  141. //Manage bloom surfaces if the number of iterations has changed
  142. if ((not _hdr) || (hdrBloomIterations != __oldHDRBloomIterations))
  143. {
  144. __FreeBloomSurfaces();
  145. }
  146. //Free up memory if the normal map state has changed
  147. if ((not normalMap) && __oldNormalMap)
  148. {
  149. __FreeNormalMapSurface();
  150. }
  151. //Construct our wipe/static/dynamic vertex buffers
  152. __UpdateVertexBuffers(_boundaryExpandedL, _boundaryExpandedT, _boundaryExpandedR, _boundaryExpandedB);
  153. //Create accumulating renderer surface
  154. surface_set_target(GetLightSurface());
  155. //Set the view/projection matrices
  156. camera_apply(__camera);
  157. //Set up some GPU state
  158. var _oldNoCulling = gpu_get_cullmode();
  159. var _oldTexFilter = gpu_get_tex_filter();
  160. gpu_set_cullmode(cull_noculling);
  161. gpu_set_tex_filter(smooth);
  162. //Clear the light surface with the ambient colour
  163. draw_clear(__GetAmbientColor());
  164. //Accumulate lights and shadows onto the lighting surface
  165. __AccumulateAmbienceSprite(_boundaryL, _boundaryT, _boundaryR, _boundaryB);
  166. if (soft)
  167. {
  168. __AccumulateSoftLights(_boundaryL, _boundaryT, _boundaryR, _boundaryB, _cameraCX, _cameraCY, _cameraW, _cameraH, _cameraCos, _cameraSin, selfLighting? -1 : 1);
  169. }
  170. else
  171. {
  172. __AccumulateHardLights(_boundaryL, _boundaryT, _boundaryR, _boundaryB, _cameraCX, _cameraCY, _cameraW, _cameraH, _cameraCos, _cameraSin, selfLighting? -1 : 1);
  173. }
  174. __AccumulateShadowOverlay(_boundaryL, _boundaryT, _boundaryR, _boundaryB);
  175. __AccumulateLightOverlay(_boundaryL, _boundaryT, _boundaryR, _boundaryB);
  176. //Restore default behaviour
  177. gpu_set_colorwriteenable(true, true, true, true);
  178. gpu_set_blendmode(bm_normal);
  179. gpu_set_cullmode(_oldNoCulling);
  180. gpu_set_tex_filter(_oldTexFilter);
  181. surface_reset_target();
  182. }
  183. __GetTonemapShader = function()
  184. {
  185. var _hdrTonemap = GetTonemap();
  186. if (_hdrTonemap == BULB_TONEMAP_NONE)
  187. {
  188. return __shdBulbTonemapNone;
  189. }
  190. else if (_hdrTonemap == BULB_TONEMAP_CLAMP)
  191. {
  192. return __shdBulbTonemapClamp;
  193. }
  194. else if (_hdrTonemap == BULB_TONEMAP_REINHARD)
  195. {
  196. return __shdBulbTonemapReinhard;
  197. }
  198. else if (_hdrTonemap == BULB_TONEMAP_REINHARD_EXTENDED)
  199. {
  200. return __shdBulbTonemapReinhardExtended;
  201. }
  202. else if (_hdrTonemap == BULB_TONEMAP_ACES)
  203. {
  204. return __shdBulbTonemapACES;
  205. }
  206. else if (_hdrTonemap == BULB_TONEMAP_UNCHARTED2)
  207. {
  208. return __shdBulbTonemapUncharted2;
  209. }
  210. else if (_hdrTonemap == BULB_TONEMAP_UNREAL3)
  211. {
  212. return __shdBulbTonemapUnreal3;
  213. }
  214. else if (_hdrTonemap == BULB_TONEMAP_HBD)
  215. {
  216. return __shdBulbTonemapHBD;
  217. }
  218. else
  219. {
  220. return __shdBulbTonemapBadGamma;
  221. }
  222. }
  223. GetOutputSurface = function(_surface)
  224. {
  225. var _surfaceWidth = surface_get_width( _surface);
  226. var _surfaceHeight = surface_get_height(_surface);
  227. __GetOutputSurface(_surfaceWidth, _surfaceHeight);
  228. surface_set_target(__outputSurface);
  229. DrawLitSurface(_surface, 0, 0, _surfaceWidth, _surfaceHeight, false, false);
  230. surface_reset_target();
  231. return __outputSurface;
  232. }
  233. DrawLitSurface = function(_surface, _x, _y, _width, _height, _textureFiltering = undefined, _alphaBlend = undefined)
  234. {
  235. if (surface_get_target() == _surface)
  236. {
  237. __BulbError("Cannot call .DrawLitSurface() when the destination surface and drawn surface are the same\nIf you are drawing the application surface, use a Post-Draw event or GUI draw event");
  238. }
  239. var _oldTextureFiltering = gpu_get_tex_filter();
  240. var _oldAlphaBlend = gpu_get_blendenable();
  241. if (_textureFiltering != undefined) gpu_set_tex_filter(_textureFiltering);
  242. if (_alphaBlend != undefined) gpu_set_blendenable(_alphaBlend);
  243. if ((__lightSurface != undefined) && surface_exists(__lightSurface))
  244. {
  245. if (hdr && _system.__hdrAvailable
  246. && (hdrBloomIntensity > 0) && (hdrBloomIterations >= 1)
  247. && (__lightSurface != undefined) && surface_exists(__lightSurface))
  248. {
  249. __KawaseBloom(__lightSurface, 1/max(0.0001, exposure));
  250. }
  251. var _shader = __GetTonemapShader();
  252. var _exposureUniform = shader_get_uniform(_shader, "u_fExposure");
  253. var _sampler = shader_get_sampler_index(_shader, "u_sLightMap");
  254. shader_set(_shader);
  255. shader_set_uniform_f(_exposureUniform, exposure);
  256. texture_set_stage(_sampler, surface_get_texture(__lightSurface));
  257. gpu_set_tex_filter_ext(_sampler, smooth);
  258. draw_surface_stretched(_surface, _x, _y, _width, _height);
  259. shader_reset();
  260. }
  261. else
  262. {
  263. draw_surface_stretched(_surface, _x, _y, _width, _height);
  264. }
  265. gpu_set_tex_filter(_oldTextureFiltering);
  266. gpu_set_blendenable(_oldAlphaBlend);
  267. }
  268. __KawaseBloom = function(_surface, _thresholdCoeff)
  269. {
  270. static _u_fIntensity = shader_get_uniform(__shdBulbIntensity, "u_fIntensity");
  271. static _u_vThreshold = shader_get_uniform(__shdBulbKawaseDownWithThreshold, "u_vThreshold");
  272. var _surfaceWidth = surface_get_width( _surface);
  273. var _surfaceHeight = surface_get_height(_surface);
  274. var _bloomSurfaceArray = __bloomSurfaceArray;
  275. //Create new bloom surfaces on demand
  276. if (array_length(_bloomSurfaceArray) < hdrBloomIterations-1)
  277. {
  278. __FreeBloomSurfaces();
  279. var _bloomWidth = _surfaceWidth;
  280. var _bloomHeight = _surfaceHeight;
  281. //Work around compile error in LTS
  282. var _surface_create = surface_create;
  283. var _i = 0;
  284. repeat(hdrBloomIterations)
  285. {
  286. _bloomWidth = _bloomWidth div 2;
  287. _bloomHeight = _bloomHeight div 2;
  288. _bloomSurfaceArray[_i] = _surface_create(_bloomWidth, _bloomHeight, surface_rgba16float);
  289. ++_i;
  290. }
  291. }
  292. //Perform Kawase blur
  293. var _oldTextureFiltering = gpu_get_tex_filter();
  294. var _oldAlphaBlend = gpu_get_blendenable();
  295. gpu_set_tex_filter(true);
  296. gpu_set_blendenable(false);
  297. surface_set_target(_bloomSurfaceArray[0]);
  298. shader_set(__shdBulbKawaseDownWithThreshold);
  299. shader_set_uniform_f(_u_vThreshold, hdrBloomThresholdMin*_thresholdCoeff, hdrBloomThresholdMax*_thresholdCoeff);
  300. shader_set_uniform_f(shader_get_uniform(__shdBulbKawaseDownWithThreshold, "u_vTexel"), texture_get_texel_width(surface_get_texture(_surface)), texture_get_texel_height(surface_get_texture(_surface)));
  301. draw_surface_stretched(_surface, 0, 0, surface_get_width(_bloomSurfaceArray[0]), surface_get_height(_bloomSurfaceArray[0]));
  302. shader_reset();
  303. surface_reset_target();
  304. if (hdrBloomIterations >= 2)
  305. {
  306. var _i = 1;
  307. repeat(hdrBloomIterations-1)
  308. {
  309. surface_set_target(_bloomSurfaceArray[_i]);
  310. shader_set(__shdBulbKawaseDown);
  311. shader_set_uniform_f(shader_get_uniform(__shdBulbKawaseDown, "u_vTexel"), texture_get_texel_width(surface_get_texture(_bloomSurfaceArray[_i-1])), texture_get_texel_height(surface_get_texture(_bloomSurfaceArray[_i-1])));
  312. draw_surface_stretched(_bloomSurfaceArray[_i-1], 0, 0, surface_get_width(_bloomSurfaceArray[_i]), surface_get_height(_bloomSurfaceArray[_i]));
  313. surface_reset_target();
  314. ++_i;
  315. }
  316. var _i = hdrBloomIterations-1;
  317. repeat(hdrBloomIterations-1)
  318. {
  319. surface_set_target(_bloomSurfaceArray[_i-1]);
  320. shader_set(__shdBulbKawaseUp);
  321. shader_set_uniform_f(shader_get_uniform(__shdBulbKawaseUp, "u_vTexel"), texture_get_texel_width(surface_get_texture(_bloomSurfaceArray[_i])), texture_get_texel_height(surface_get_texture(_bloomSurfaceArray[_i])));
  322. draw_surface_stretched(_bloomSurfaceArray[_i], 0, 0, surface_get_width(_bloomSurfaceArray[_i-1]), surface_get_height(_bloomSurfaceArray[_i-1]));
  323. surface_reset_target();
  324. --_i;
  325. }
  326. }
  327. gpu_set_blendenable(true);
  328. surface_set_target(_surface);
  329. gpu_set_blendmode_ext(bm_one, bm_one);
  330. shader_set(__shdBulbIntensity);
  331. shader_set_uniform_f(_u_fIntensity, hdrBloomIntensity);
  332. draw_surface_stretched_ext(_bloomSurfaceArray[0], 0, 0, _surfaceWidth, _surfaceHeight, c_white, 1);
  333. gpu_set_blendmode(bm_normal);
  334. shader_reset();
  335. surface_reset_target();
  336. gpu_set_tex_filter(_oldTextureFiltering);
  337. gpu_set_blendenable(_oldAlphaBlend);
  338. }
  339. Free = function()
  340. {
  341. __FreeVertexBuffers();
  342. __FreeLightSurface();
  343. __FreeOutputSurface();
  344. __FreeBloomSurfaces();
  345. __FreeNormalMapSurface();
  346. var _nullFunc = function() {}
  347. //BulbRenderer()
  348. SetCamera = _nullFunc;
  349. GetCamera = _nullFunc;
  350. SetSurfaceDimensions = _nullFunc;
  351. GetSurfaceDimensions = _nullFunc;
  352. Update = _nullFunc;
  353. GetOutputSurface = _nullFunc;
  354. DrawLitSurface = _nullFunc;
  355. GetTonemap = _nullFunc;
  356. RefreshStaticOccluders = _nullFunc;
  357. __KawaseBloom = _nullFunc;
  358. //__BulbRendererDefineHDR()
  359. __GetOutputSurface = _nullFunc;
  360. __FreeOutputSurface = _nullFunc;
  361. __FreeBloomSurfaces = _nullFunc;
  362. //__BulbRendererDefineNormal()
  363. GetNormalMapSurface = _nullFunc;
  364. DrawNormalMapDebug = _nullFunc;
  365. __FreeNormalMapSurface = _nullFunc;
  366. //__BulbRendererDefineOverlayUnderlay()
  367. __AccumulateAmbienceSprite = _nullFunc;
  368. __AccumulateShadowOverlay = _nullFunc;
  369. __AccumulateLightOverlay = _nullFunc;
  370. //__BulbRendererDefineAccumulateSoft()
  371. __AccumulateSoftLights = _nullFunc;
  372. //__BulbRendererDefineAccumulateHard()
  373. __AccumulateHardLights = _nullFunc;
  374. //__BulbRendererDefineVertexBuffers()
  375. RefreshStaticOccluders = _nullFunc;
  376. __FreeVertexBuffers = _nullFunc;
  377. __UpdateVertexBuffers = _nullFunc;
  378. //__BulbRendererDefineLight()
  379. GetLightSurface = _nullFunc;
  380. __FreeLightSurface = _nullFunc;
  381. GetLightValue = _nullFunc;
  382. }
  383. GetTonemap = function()
  384. {
  385. return (hdr && _system.__hdrAvailable)? hdrTonemap : ldrTonemap;
  386. }
  387. __GetAmbientColor = function()
  388. {
  389. if (GetTonemap() == BULB_TONEMAP_BAD_GAMMA)
  390. {
  391. if (ambientInGammaSpace)
  392. {
  393. return ambientColor;
  394. }
  395. else
  396. {
  397. return make_color_rgb(255*power(color_get_red( ambientColor)/255, 1/BULB_GAMMA),
  398. 255*power(color_get_green(ambientColor)/255, 1/BULB_GAMMA),
  399. 255*power(color_get_blue( ambientColor)/255, 1/BULB_GAMMA));
  400. }
  401. }
  402. else
  403. {
  404. if (ambientInGammaSpace)
  405. {
  406. return make_color_rgb(255*power(color_get_red( ambientColor)/255, BULB_GAMMA),
  407. 255*power(color_get_green(ambientColor)/255, BULB_GAMMA),
  408. 255*power(color_get_blue( ambientColor)/255, BULB_GAMMA));
  409. }
  410. else
  411. {
  412. return ambientColor;
  413. }
  414. }
  415. }
  416. //Assign the ambient colour used for the darkest areas of the screen. This can be changed on the fly
  417. ambientColor = c_black;
  418. ambientInGammaSpace = false;
  419. //The smoothing mode controls texture filtering both when accumulating lights and when drawing the resulting surface
  420. smooth = true;
  421. selfLighting = false;
  422. soft = true;
  423. __oldSoft = undefined;
  424. exposure = 1;
  425. ldrTonemap = BULB_TONEMAP_NONE;
  426. __camera = undefined;
  427. __cameraImplicit = false;
  428. __surfaceWidth = -1;
  429. __surfaceHeight = -1;
  430. __BulbRendererDefineHDR();
  431. __BulbRendererDefineNormal();
  432. __BulbRendererDefineOverlayUnderlay();
  433. __BulbRendererDefineAccumulateSoft();
  434. if (_system.__hasStencil) __BulbRendererDefineAccumulateHard() else __BulbRendererDefineAccumulateHardNoStencil();
  435. __BulbRendererDefineVertexBuffers();
  436. __BulbRendererDefineLight();
  437. SetCamera(_camera);
  438. }