2
0

post_processing.shader 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. include = [
  2. "core/shaders/common.shader"
  3. "core/shaders/default.shader"
  4. ]
  5. render_states = {
  6. bloom_upsample = {
  7. inherit = "default"
  8. states = {
  9. blend_enable = true
  10. blend_src = "one"
  11. blend_dst = "one"
  12. }
  13. }
  14. bloom_combine = {
  15. inherit = "default"
  16. states = {
  17. depth_write_enable = false
  18. }
  19. }
  20. }
  21. bgfx_shaders = {
  22. bloom_params = {
  23. includes = [ "common" ]
  24. code = """
  25. uniform vec4 u_bloom_params;
  26. #define bloom_weight u_bloom_params.x
  27. #define bloom_intensity u_bloom_params.y
  28. #define bloom_threshold u_bloom_params.z
  29. """
  30. }
  31. bloom_downsample = {
  32. includes = [ "common" ]
  33. varying = """
  34. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  35. vec3 a_position : POSITION;
  36. vec2 a_texcoord0 : TEXCOORD0;
  37. """
  38. vs_input_output = """
  39. $input a_position, a_texcoord0
  40. $output v_texcoord0
  41. """
  42. vs_code = """
  43. void main()
  44. {
  45. gl_Position = mul(u_viewProj, vec4(a_position.xy, 0.0, 1.0) );
  46. v_texcoord0 = a_texcoord0;
  47. }
  48. """
  49. fs_input_output = """
  50. $input v_texcoord0
  51. """
  52. fs_code = """
  53. /*
  54. * Copyright 2018 Eric Arnebäck. All rights reserved.
  55. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  56. */
  57. SAMPLER2D(s_color_map, 0);
  58. uniform vec4 u_map_pixel_size; // pixel size of the target texture.
  59. void main()
  60. {
  61. vec2 halfpixel = 0.5 * vec2(u_map_pixel_size.x, u_map_pixel_size.y);
  62. vec2 oneepixel = 1.0 * vec2(u_map_pixel_size.x, u_map_pixel_size.y);
  63. vec2 uv = v_texcoord0.xy;
  64. vec4 sum = vec4(0.0, 0.0, 0.0, 0.0);
  65. sum += (4.0/32.0) * texture2D(s_color_map, uv).rgba;
  66. sum += (4.0/32.0) * texture2D(s_color_map, uv + vec2(-halfpixel.x, -halfpixel.y) );
  67. sum += (4.0/32.0) * texture2D(s_color_map, uv + vec2(+halfpixel.x, +halfpixel.y) );
  68. sum += (4.0/32.0) * texture2D(s_color_map, uv + vec2(+halfpixel.x, -halfpixel.y) );
  69. sum += (4.0/32.0) * texture2D(s_color_map, uv + vec2(-halfpixel.x, +halfpixel.y) );
  70. sum += (2.0/32.0) * texture2D(s_color_map, uv + vec2(+oneepixel.x, 0.0) );
  71. sum += (2.0/32.0) * texture2D(s_color_map, uv + vec2(-oneepixel.x, 0.0) );
  72. sum += (2.0/32.0) * texture2D(s_color_map, uv + vec2(0.0, +oneepixel.y) );
  73. sum += (2.0/32.0) * texture2D(s_color_map, uv + vec2(0.0, -oneepixel.y) );
  74. sum += (1.0/32.0) * texture2D(s_color_map, uv + vec2(+oneepixel.x, +oneepixel.y) );
  75. sum += (1.0/32.0) * texture2D(s_color_map, uv + vec2(-oneepixel.x, +oneepixel.y) );
  76. sum += (1.0/32.0) * texture2D(s_color_map, uv + vec2(+oneepixel.x, -oneepixel.y) );
  77. sum += (1.0/32.0) * texture2D(s_color_map, uv + vec2(-oneepixel.x, -oneepixel.y) );
  78. gl_FragColor.xyzw = sum;
  79. }
  80. """
  81. }
  82. bloom_upsample = {
  83. includes = [ "common" "bloom_params" ]
  84. varying = """
  85. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  86. vec3 a_position : POSITION;
  87. vec2 a_texcoord0 : TEXCOORD0;
  88. """
  89. vs_input_output = """
  90. $input a_position, a_texcoord0
  91. $output v_texcoord0
  92. """
  93. vs_code = """
  94. void main()
  95. {
  96. gl_Position = mul(u_viewProj, vec4(a_position.xy, 0.0, 1.0) );
  97. v_texcoord0 = a_texcoord0;
  98. }
  99. """
  100. fs_input_output = """
  101. $input v_texcoord0
  102. """
  103. fs_code = """
  104. /*
  105. * Copyright 2018 Eric Arnebäck. All rights reserved.
  106. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  107. */
  108. SAMPLER2D(s_color_map, 0);
  109. uniform vec4 u_map_pixel_size;
  110. void main()
  111. {
  112. vec2 halfpixel = u_map_pixel_size.xy;
  113. vec2 uv = v_texcoord0.xy;
  114. vec4 sum = vec4_splat(0.0);
  115. sum += (2.0 / 16.0) * texture2D(s_color_map, uv + vec2(-halfpixel.x, 0.0) );
  116. sum += (2.0 / 16.0) * texture2D(s_color_map, uv + vec2( 0.0, halfpixel.y) );
  117. sum += (2.0 / 16.0) * texture2D(s_color_map, uv + vec2( halfpixel.x, 0.0) );
  118. sum += (2.0 / 16.0) * texture2D(s_color_map, uv + vec2( 0.0, -halfpixel.y) );
  119. sum += (1.0 / 16.0) * texture2D(s_color_map, uv + vec2(-halfpixel.x, -halfpixel.y) );
  120. sum += (1.0 / 16.0) * texture2D(s_color_map, uv + vec2(-halfpixel.x, halfpixel.y) );
  121. sum += (1.0 / 16.0) * texture2D(s_color_map, uv + vec2( halfpixel.x, -halfpixel.y) );
  122. sum += (1.0 / 16.0) * texture2D(s_color_map, uv + vec2( halfpixel.x, halfpixel.y) );
  123. sum += (4.0 / 16.0) * texture2D(s_color_map, uv);
  124. gl_FragColor = bloom_intensity * sum;
  125. }
  126. """
  127. }
  128. bloom_combine = {
  129. includes = [ "common" "bloom_params" ]
  130. varying = """
  131. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  132. vec3 a_position : POSITION;
  133. vec2 a_texcoord0 : TEXCOORD0;
  134. """
  135. vs_input_output = """
  136. $input a_position, a_texcoord0
  137. $output v_texcoord0
  138. """
  139. vs_code = """
  140. void main()
  141. {
  142. gl_Position = mul(u_viewProj, vec4(a_position.xy, 0.0, 1.0) );
  143. v_texcoord0 = a_texcoord0;
  144. }
  145. """
  146. fs_input_output = """
  147. $input v_texcoord0
  148. """
  149. fs_code = """
  150. SAMPLER2D(s_color_map, 0);
  151. SAMPLER2D(s_bloom_map, 1);
  152. void main()
  153. {
  154. vec3 color = texture2D(s_color_map, v_texcoord0).rgb;
  155. vec3 bloom = texture2D(s_bloom_map, v_texcoord0).rgb;
  156. gl_FragColor = vec4(mix(color, bloom, bloom_weight), 1.0);
  157. }
  158. """
  159. }
  160. tonemap = {
  161. includes = [ "common" ]
  162. varying = """
  163. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  164. vec3 a_position : POSITION;
  165. vec2 a_texcoord0 : TEXCOORD0;
  166. """
  167. vs_input_output = """
  168. $input a_position, a_texcoord0
  169. $output v_texcoord0
  170. """
  171. vs_code = """
  172. void main()
  173. {
  174. gl_Position = mul(u_viewProj, vec4(a_position.xy, 0.0, 1.0) );
  175. v_texcoord0 = a_texcoord0;
  176. }
  177. """
  178. fs_input_output = """
  179. $input v_texcoord0
  180. """
  181. fs_code = """
  182. SAMPLER2D(s_color_map, 0);
  183. uniform vec4 u_tonemap_type;
  184. void main()
  185. {
  186. vec4 color = vec4(texture2D(s_color_map, v_texcoord0).rgb, 1.0);
  187. if (u_tonemap_type.x == 0.0)
  188. gl_FragColor = toGammaAccurate(color);
  189. else if (u_tonemap_type.x == 1.0)
  190. gl_FragColor = toReinhard(color);
  191. else if (u_tonemap_type.x == 2.0)
  192. gl_FragColor = toFilmic(color);
  193. else if (u_tonemap_type.x == 3.0)
  194. gl_FragColor = toAcesFilmic(color);
  195. else
  196. gl_FragColor = color;
  197. }
  198. """
  199. }
  200. }
  201. shaders = {
  202. bloom_downsample = {
  203. bgfx_shader = "bloom_downsample"
  204. render_state = "default"
  205. }
  206. bloom_upsample = {
  207. bgfx_shader = "bloom_upsample"
  208. render_state = "bloom_upsample"
  209. }
  210. bloom_combine = {
  211. bgfx_shader = "bloom_combine"
  212. render_state = "bloom_combine"
  213. }
  214. tonemap = {
  215. bgfx_shader = "tonemap"
  216. render_state = "blit"
  217. }
  218. }
  219. static_compile = [
  220. { shader = "bloom_downsample" defines = [] }
  221. { shader = "bloom_upsample" defines = [] }
  222. { shader = "bloom_combine" defines = [] }
  223. { shader = "tonemap" defines = [] }
  224. ]