default.shader 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. include = [
  2. "core/shaders/common.shader"
  3. "core/shaders/lighting.shader"
  4. ]
  5. render_states = {
  6. debug_line = {
  7. inherit = "default"
  8. states = {
  9. blend_enable = true
  10. blend_src = "src_alpha"
  11. blend_dst = "inv_src_alpha"
  12. primitive_type = "pt_lines"
  13. "!defined(DEPTH_ENABLED)" = {
  14. depth_enable = false
  15. }
  16. }
  17. }
  18. gui = {
  19. inherit = "opacity"
  20. }
  21. blit = {
  22. inherit = "default"
  23. states = {
  24. cull_mode = "none"
  25. depth_write_enable = false;
  26. depth_enable = false;
  27. blend_enable = false;
  28. }
  29. }
  30. sprite = {
  31. inherit = "default"
  32. states = {
  33. depth_func = "always"
  34. blend_enable = true
  35. blend_src = "src_alpha"
  36. blend_dst = "inv_src_alpha"
  37. blend_equation = "add"
  38. }
  39. }
  40. mesh = {
  41. inherit = "default"
  42. }
  43. noop = {
  44. inherit = "default"
  45. states = {
  46. rgb_write_enable = false
  47. alpha_write_enable = false
  48. depth_write_enable = false
  49. depth_enable = false
  50. blend_enable = false
  51. }
  52. }
  53. }
  54. bgfx_shaders = {
  55. debug_line = {
  56. includes = [ "common" ]
  57. varying = """
  58. vec4 v_color0 : COLOR0 = vec4(0.0, 0.0, 0.0, 0.0);
  59. vec3 a_position : POSITION;
  60. vec4 a_color0 : COLOR0;
  61. """
  62. vs_input_output = """
  63. $input a_position, a_color0
  64. $output v_color0
  65. """
  66. vs_code = """
  67. void main()
  68. {
  69. gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
  70. v_color0 = a_color0;
  71. }
  72. """
  73. fs_input_output = """
  74. $input v_color0
  75. """
  76. fs_code = """
  77. void main()
  78. {
  79. gl_FragColor = v_color0;
  80. }
  81. """
  82. }
  83. gui = {
  84. includes = [ "common" ]
  85. samplers = {
  86. u_albedo_map = { sampler_state = "clamp_anisotropic" }
  87. }
  88. varying = """
  89. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  90. vec4 v_color0 : COLOR0 = vec4(0.0, 0.0, 0.0, 0.0);
  91. vec3 a_position : POSITION;
  92. vec2 a_texcoord0 : TEXCOORD0;
  93. vec4 a_color0 : COLOR0;
  94. """
  95. vs_input_output = """
  96. $input a_position, a_texcoord0, a_color0
  97. $output v_texcoord0, v_color0
  98. """
  99. vs_code = """
  100. void main()
  101. {
  102. gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
  103. #if defined(DIFFUSE_MAP)
  104. v_texcoord0 = a_texcoord0;
  105. #endif // DIFFUSE_MAP
  106. v_color0 = a_color0;
  107. }
  108. """
  109. fs_input_output = """
  110. $input v_texcoord0, v_color0
  111. """
  112. fs_code = """
  113. #if defined(DIFFUSE_MAP)
  114. SAMPLER2D(u_albedo_map, 0);
  115. #endif // DIFFUSE_MAP
  116. void main()
  117. {
  118. #if defined(DIFFUSE_MAP)
  119. gl_FragColor = toGammaAccurate(texture2D(u_albedo_map, v_texcoord0) * toLinearAccurate(v_color0));
  120. #else
  121. gl_FragColor = v_color0;
  122. #endif // DIFFUSE_MAP
  123. }
  124. """
  125. }
  126. sprite = {
  127. includes = [ "common" ]
  128. samplers = {
  129. u_albedo_map = { sampler_state = "clamp_point" }
  130. }
  131. varying = """
  132. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  133. vec3 a_position : POSITION;
  134. vec2 a_texcoord0 : TEXCOORD0;
  135. """
  136. vs_input_output = """
  137. $input a_position, a_texcoord0
  138. $output v_texcoord0
  139. """
  140. vs_code = """
  141. void main()
  142. {
  143. gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
  144. v_texcoord0 = a_texcoord0;
  145. }
  146. """
  147. fs_input_output = """
  148. $input v_texcoord0
  149. """
  150. fs_code = """
  151. SAMPLER2D(u_albedo_map, 0);
  152. uniform vec4 u_color;
  153. void main()
  154. {
  155. gl_FragColor = toGammaAccurate(texture2D(u_albedo_map, v_texcoord0) * toLinearAccurate(u_color));
  156. }
  157. """
  158. }
  159. mesh = {
  160. includes = [ "common" "lighting" ]
  161. samplers = {
  162. u_albedo_map = { sampler_state = "mirror_anisotropic" }
  163. u_normal_map = { sampler_state = "mirror_anisotropic" }
  164. u_metallic_map = { sampler_state = "mirror_anisotropic" }
  165. u_roughness_map = { sampler_state = "mirror_anisotropic" }
  166. u_ao_map = { sampler_state = "mirror_anisotropic" }
  167. }
  168. varying = """
  169. vec3 v_normal : NORMAL = vec3(0.0, 0.0, 0.0);
  170. vec3 v_tangent : TANGENT = vec3(0.0, 0.0, 0.0);
  171. vec3 v_bitangent : BITANGENT = vec3(0.0, 0.0, 0.0);
  172. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  173. vec3 v_position : TEXCOORD1 = vec3(0.0, 0.0, 0.0);
  174. vec3 v_camera : TEXCOORD2 = vec3(0.0, 0.0, 0.0);
  175. vec4 v_shadow0 : TEXCOORD3 = vec4(0.0, 0.0, 0.0, 0.0);
  176. vec4 v_shadow1 : TEXCOORD4 = vec4(0.0, 0.0, 0.0, 0.0);
  177. vec4 v_shadow2 : TEXCOORD5 = vec4(0.0, 0.0, 0.0, 0.0);
  178. vec4 v_shadow3 : TEXCOORD6 = vec4(0.0, 0.0, 0.0, 0.0);
  179. vec3 a_position : POSITION;
  180. vec3 a_normal : NORMAL;
  181. vec3 a_tangent : TANGENT;
  182. vec3 a_bitangent : BITANGENT;
  183. vec4 a_indices : BLENDINDICES;
  184. vec4 a_weight : BLENDWEIGHT;
  185. vec2 a_texcoord0 : TEXCOORD0;
  186. """
  187. vs_input_output = """
  188. #if defined(SKINNING)
  189. $input a_position, a_normal, a_tangent, a_bitangent, a_texcoord0, a_indices, a_weight
  190. #else
  191. $input a_position, a_normal, a_tangent, a_bitangent, a_texcoord0
  192. #endif
  193. $output v_normal, v_tangent, v_bitangent, v_texcoord0, v_position, v_camera, v_shadow0, v_shadow1, v_shadow2, v_shadow3
  194. """
  195. vs_code = """
  196. uniform vec4 u_use_normal_map;
  197. void main()
  198. {
  199. #if defined(SKINNING)
  200. mat4 model;
  201. model = a_weight.x * u_model[int(a_indices.x)];
  202. model += a_weight.y * u_model[int(a_indices.y)];
  203. model += a_weight.z * u_model[int(a_indices.z)];
  204. model += a_weight.w * u_model[int(a_indices.w)];
  205. gl_Position = mul(mul(u_modelViewProj, model), vec4(a_position, 1.0));
  206. model = mul(u_model[0], model);
  207. #else
  208. gl_Position = mul(mul(u_viewProj, u_model[0]), vec4(a_position, 1.0));
  209. mat4 model = u_model[0];
  210. #endif
  211. v_position = mul(model, vec4(a_position, 1.0)).xyz;
  212. v_normal = normalize(mul(model, vec4(a_normal, 0.0))).xyz;
  213. v_tangent = normalize(mul(model, vec4(a_tangent, 0.0))).xyz;
  214. v_bitangent = normalize(mul(model, vec4(a_bitangent, 0.0))).xyz;
  215. mat3 tbn;
  216. if (u_use_normal_map.r == 1.0)
  217. tbn = mtxFromCols(v_tangent, v_bitangent, v_normal);
  218. else
  219. tbn = mtxFromCols(vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0));
  220. v_camera = mul(u_invView, vec4(0.0, 0.0, 0.0, 1.0)).xyz;
  221. v_camera = mul(v_camera - v_position, tbn);
  222. v_position = mul(v_position, tbn);
  223. v_texcoord0 = a_texcoord0;
  224. #if !defined(NO_LIGHT)
  225. vec3 pos_offset = a_position + a_normal * 0.01;
  226. v_shadow0 = mul(mul(u_cascaded_lights[0], model), vec4(pos_offset, 1.0));
  227. v_shadow1 = mul(mul(u_cascaded_lights[1], model), vec4(pos_offset, 1.0));
  228. v_shadow2 = mul(mul(u_cascaded_lights[2], model), vec4(pos_offset, 1.0));
  229. v_shadow3 = mul(mul(u_cascaded_lights[3], model), vec4(pos_offset, 1.0));
  230. #endif
  231. }
  232. """
  233. fs_input_output = """
  234. $input v_normal, v_tangent, v_bitangent, v_texcoord0, v_position, v_camera, v_shadow0, v_shadow1, v_shadow2, v_shadow3
  235. """
  236. code = """
  237. """
  238. fs_code = """
  239. SAMPLER2D(u_albedo_map, 0);
  240. SAMPLER2D(u_normal_map, 1);
  241. SAMPLER2D(u_metallic_map, 2);
  242. SAMPLER2D(u_roughness_map, 3);
  243. SAMPLER2D(u_ao_map, 4);
  244. uniform vec4 u_albedo;
  245. uniform vec4 u_metallic;
  246. uniform vec4 u_roughness;
  247. uniform vec4 u_use_albedo_map;
  248. uniform vec4 u_use_normal_map;
  249. uniform vec4 u_use_metallic_map;
  250. uniform vec4 u_use_roughness_map;
  251. uniform vec4 u_use_ao_map;
  252. void main()
  253. {
  254. vec3 albedo = u_use_albedo_map.r == 1.0 ? texture2D(u_albedo_map, v_texcoord0).rgb : u_albedo.rgb;
  255. #if defined(NO_LIGHT)
  256. vec3 radiance = albedo;
  257. #else
  258. vec3 normal = u_use_normal_map.r == 1.0 ? decodeNormalUint(texture2D(u_normal_map, v_texcoord0).rgb) : v_normal;
  259. float metallic = u_use_metallic_map.r == 1.0 ? texture2D(u_metallic_map, v_texcoord0).r : u_metallic.r;
  260. float roughness = u_use_roughness_map.r == 1.0 ? texture2D(u_roughness_map, v_texcoord0).r: u_roughness.r;
  261. float ao = u_use_ao_map.r == 1.0 ? texture2D(u_ao_map, v_texcoord0).r : 0.0;
  262. mat3 tbn;
  263. if (u_use_normal_map.r == 1.0)
  264. tbn = mtxFromCols(v_tangent, v_bitangent, v_normal);
  265. else
  266. tbn = mtxFromCols(vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0));
  267. vec3 n = normalize(normal); // Fragment normal.
  268. vec3 v = normalize(v_camera); // Versor from fragment to camera pos.
  269. vec3 f0 = mix(vec3_splat(0.04), albedo, metallic);
  270. vec3 radiance = calc_lighting(tbn, n, v, v_position, v_shadow0, v_shadow1, v_shadow2, v_shadow3, albedo, metallic, roughness, f0);
  271. radiance = radiance / (radiance + vec3_splat(1.0)); // Tone-mapping.
  272. #endif // !defined(NO_LIGHT)
  273. gl_FragColor = vec4(toGammaAccurate(radiance), 1.0);
  274. }
  275. """
  276. }
  277. blit = {
  278. includes = [ "common" ]
  279. varying = """
  280. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  281. vec3 a_position : POSITION;
  282. vec2 a_texcoord0 : TEXCOORD0;
  283. """
  284. vs_input_output = """
  285. $input a_position, a_texcoord0
  286. $output v_texcoord0
  287. """
  288. vs_code = """
  289. void main()
  290. {
  291. gl_Position = mul(u_viewProj, vec4(a_position.xy, 0.0, 1.0) );
  292. v_texcoord0 = a_texcoord0;
  293. }
  294. """
  295. fs_input_output = """
  296. $input v_texcoord0
  297. """
  298. fs_code = """
  299. SAMPLER2D(s_color, 0);
  300. void main()
  301. {
  302. gl_FragColor = texture2D(s_color, v_texcoord0);
  303. }
  304. """
  305. }
  306. fallback = {
  307. includes = [ "common" ]
  308. varying = """
  309. vec3 a_position : POSITION;
  310. """
  311. vs_input_output = """
  312. $input a_position
  313. """
  314. vs_code = """
  315. void main()
  316. {
  317. gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
  318. }
  319. """
  320. fs_input_output = """
  321. """
  322. fs_code = """
  323. void main()
  324. {
  325. gl_FragColor = toGammaAccurate(vec4(1.0, 0.0, 1.0, 1.0));
  326. }
  327. """
  328. }
  329. noop = {
  330. includes = [ "common" ]
  331. varying = """
  332. """
  333. vs_input_output = """
  334. """
  335. vs_code = """
  336. void main()
  337. {
  338. gl_Position = vec4_splat(0.0);
  339. }
  340. """
  341. fs_input_output = """
  342. """
  343. fs_code = """
  344. void main()
  345. {
  346. discard;
  347. }
  348. """
  349. }
  350. }
  351. shaders = {
  352. debug_line = {
  353. bgfx_shader = "debug_line"
  354. render_state = "debug_line"
  355. }
  356. gui = {
  357. bgfx_shader = "gui"
  358. render_state = "gui"
  359. }
  360. sprite = {
  361. bgfx_shader = "sprite"
  362. render_state = "sprite"
  363. }
  364. mesh = {
  365. bgfx_shader = "mesh"
  366. render_state = "mesh"
  367. }
  368. blit = {
  369. bgfx_shader = "blit"
  370. render_state = "blit"
  371. }
  372. fallback = {
  373. bgfx_shader = "fallback"
  374. render_state = "mesh"
  375. }
  376. noop = {
  377. bgfx_shader = "noop"
  378. render_state = "noop"
  379. }
  380. }
  381. static_compile = [
  382. { shader = "debug_line" defines = [] }
  383. { shader = "debug_line" defines = ["DEPTH_ENABLED"] }
  384. { shader = "gui" defines = [] }
  385. { shader = "gui" defines = ["DIFFUSE_MAP"]}
  386. { shader = "gui" defines = ["DEPTH_ENABLED"]}
  387. { shader = "gui" defines = ["DIFFUSE_MAP" "DEPTH_ENABLED"]}
  388. { shader = "sprite" defines = [] }
  389. { shader = "mesh" defines = [] }
  390. { shader = "mesh" defines = ["DIFFUSE_MAP"] }
  391. { shader = "mesh" defines = ["SKINNING"] }
  392. { shader = "mesh" defines = ["NO_LIGHT"] }
  393. { shader = "mesh" defines = ["DIFFUSE_MAP" "SKINNING"] }
  394. { shader = "mesh" defines = ["DIFFUSE_MAP" "NO_LIGHT"] }
  395. { shader = "blit" defines = [] }
  396. { shader = "fallback" defines = [] }
  397. { shader = "noop" defines = [] }
  398. ]