default.shader 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. include = [
  2. "core/shaders/common.shader"
  3. "core/shaders/lighting.shader"
  4. ]
  5. render_states = {
  6. debug_line = {
  7. states = {
  8. blend_enable = true
  9. blend_src = "src_alpha"
  10. blend_dst = "inv_src_alpha"
  11. primitive_type = "pt_lines"
  12. "!defined(DEPTH_ENABLED)" = {
  13. depth_enable = false
  14. }
  15. }
  16. }
  17. gui = {
  18. states = {
  19. cull_mode = "none"
  20. depth_write_enable = true
  21. depth_enable = false
  22. blend_enable = true
  23. blend_src = "src_alpha"
  24. blend_dst = "inv_src_alpha"
  25. "defined(DEPTH_ENABLED)" = {
  26. depth_enable = true
  27. }
  28. }
  29. }
  30. blit = {
  31. states = {
  32. cull_mode = "none"
  33. depth_write_enable = false;
  34. depth_enable = false;
  35. blend_enable = false;
  36. }
  37. }
  38. sprite = {
  39. states = {
  40. depth_func = "always"
  41. blend_enable = true
  42. blend_src = "src_alpha"
  43. blend_dst = "inv_src_alpha"
  44. blend_equation = "add"
  45. }
  46. }
  47. mesh = {
  48. }
  49. selection = {
  50. states = {
  51. alpha_write_enable = false
  52. }
  53. }
  54. outline = {
  55. inherit = "gui"
  56. states = {
  57. depth_write_enable = false
  58. }
  59. }
  60. noop = {
  61. states = {
  62. rgb_write_enable = false
  63. alpha_write_enable = false
  64. depth_write_enable = false
  65. depth_enable = false
  66. blend_enable = false
  67. }
  68. }
  69. }
  70. bgfx_shaders = {
  71. debug_line = {
  72. includes = "common"
  73. varying = """
  74. vec4 v_color0 : COLOR0 = vec4(0.0, 0.0, 0.0, 0.0);
  75. vec3 a_position : POSITION;
  76. vec4 a_color0 : COLOR0;
  77. """
  78. vs_input_output = """
  79. $input a_position, a_color0
  80. $output v_color0
  81. """
  82. vs_code = """
  83. void main()
  84. {
  85. gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
  86. v_color0 = a_color0;
  87. }
  88. """
  89. fs_input_output = """
  90. $input v_color0
  91. """
  92. fs_code = """
  93. void main()
  94. {
  95. gl_FragColor = v_color0;
  96. }
  97. """
  98. }
  99. gui = {
  100. includes = "common"
  101. samplers = {
  102. u_albedo_map = { sampler_state = "clamp_anisotropic" }
  103. }
  104. varying = """
  105. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  106. vec4 v_color0 : COLOR0 = vec4(0.0, 0.0, 0.0, 0.0);
  107. vec3 a_position : POSITION;
  108. vec2 a_texcoord0 : TEXCOORD0;
  109. vec4 a_color0 : COLOR0;
  110. """
  111. vs_input_output = """
  112. $input a_position, a_texcoord0, a_color0
  113. $output v_texcoord0, v_color0
  114. """
  115. vs_code = """
  116. void main()
  117. {
  118. gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
  119. #if defined(DIFFUSE_MAP)
  120. v_texcoord0 = a_texcoord0;
  121. #endif // DIFFUSE_MAP
  122. v_color0 = a_color0;
  123. }
  124. """
  125. fs_input_output = """
  126. $input v_texcoord0, v_color0
  127. """
  128. fs_code = """
  129. #if defined(DIFFUSE_MAP)
  130. SAMPLER2D(u_albedo_map, 0);
  131. #endif // DIFFUSE_MAP
  132. void main()
  133. {
  134. #if defined(DIFFUSE_MAP)
  135. gl_FragColor = toGammaAccurate(texture2D(u_albedo_map, v_texcoord0) * toLinearAccurate(v_color0));
  136. #else
  137. gl_FragColor = v_color0;
  138. #endif // DIFFUSE_MAP
  139. }
  140. """
  141. }
  142. sprite = {
  143. includes = "common"
  144. samplers = {
  145. u_albedo_map = { sampler_state = "clamp_point" }
  146. }
  147. varying = """
  148. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  149. vec3 a_position : POSITION;
  150. vec2 a_texcoord0 : TEXCOORD0;
  151. """
  152. vs_input_output = """
  153. $input a_position, a_texcoord0
  154. $output v_texcoord0
  155. """
  156. vs_code = """
  157. void main()
  158. {
  159. gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
  160. v_texcoord0 = a_texcoord0;
  161. }
  162. """
  163. fs_input_output = """
  164. $input v_texcoord0
  165. """
  166. fs_code = """
  167. SAMPLER2D(u_albedo_map, 0);
  168. uniform vec4 u_color;
  169. void main()
  170. {
  171. gl_FragColor = toGammaAccurate(texture2D(u_albedo_map, v_texcoord0) * toLinearAccurate(u_color));
  172. }
  173. """
  174. }
  175. mesh = {
  176. includes = [ "common" "lighting" ]
  177. samplers = {
  178. u_albedo_map = { sampler_state = "mirror_anisotropic" }
  179. u_normal_map = { sampler_state = "mirror_anisotropic" }
  180. u_metallic_map = { sampler_state = "mirror_anisotropic" }
  181. u_roughness_map = { sampler_state = "mirror_anisotropic" }
  182. u_ao_map = { sampler_state = "mirror_anisotropic" }
  183. }
  184. varying = """
  185. vec3 v_normal : NORMAL = vec3(0.0, 0.0, 0.0);
  186. vec3 v_tangent : TANGENT = vec3(0.0, 0.0, 0.0);
  187. vec3 v_bitangent : BITANGENT = vec3(0.0, 0.0, 0.0);
  188. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  189. vec3 v_position : TEXCOORD1 = vec3(0.0, 0.0, 0.0);
  190. vec3 v_camera : TEXCOORD2 = vec3(0.0, 0.0, 0.0);
  191. vec3 a_position : POSITION;
  192. vec3 a_normal : NORMAL;
  193. vec3 a_tangent : TANGENT;
  194. vec3 a_bitangent : BITANGENT;
  195. vec4 a_indices : BLENDINDICES;
  196. vec4 a_weight : BLENDWEIGHT;
  197. vec2 a_texcoord0 : TEXCOORD0;
  198. """
  199. vs_input_output = """
  200. #if defined(SKINNING)
  201. $input a_position, a_normal, a_tangent, a_bitangent, a_texcoord0, a_indices, a_weight
  202. #else
  203. $input a_position, a_normal, a_tangent, a_bitangent, a_texcoord0
  204. #endif
  205. $output v_normal, v_tangent, v_bitangent, v_texcoord0, v_position, v_camera
  206. """
  207. vs_code = """
  208. uniform vec4 u_use_normal_map;
  209. void main()
  210. {
  211. #if defined(SKINNING)
  212. mat4 model;
  213. model = a_weight.x * u_model[int(a_indices.x)];
  214. model += a_weight.y * u_model[int(a_indices.y)];
  215. model += a_weight.z * u_model[int(a_indices.z)];
  216. model += a_weight.w * u_model[int(a_indices.w)];
  217. gl_Position = mul(mul(u_modelViewProj, model), vec4(a_position, 1.0));
  218. model = mul(u_model[0], model);
  219. #else
  220. gl_Position = mul(mul(u_viewProj, u_model[0]), vec4(a_position, 1.0));
  221. mat4 model = u_model[0];
  222. #endif
  223. v_position = mul(model, vec4(a_position, 1.0)).xyz;
  224. v_normal = normalize(mul(model, vec4(a_normal, 0.0))).xyz;
  225. v_tangent = normalize(mul(model, vec4(a_tangent, 0.0))).xyz;
  226. v_bitangent = normalize(mul(model, vec4(a_bitangent, 0.0))).xyz;
  227. mat3 tbn;
  228. if (u_use_normal_map.r == 1.0)
  229. tbn = mtxFromCols(v_tangent, v_bitangent, v_normal);
  230. else
  231. tbn = mtxFromCols(vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0));
  232. v_camera = mul(u_invView, vec4(0.0, 0.0, 0.0, 1.0)).xyz;
  233. v_camera = mul(v_camera - v_position, tbn);
  234. v_position = mul(v_position, tbn);
  235. v_texcoord0 = a_texcoord0;
  236. }
  237. """
  238. fs_input_output = """
  239. $input v_normal, v_tangent, v_bitangent, v_texcoord0, v_position, v_camera
  240. """
  241. fs_code = """
  242. SAMPLER2D(u_albedo_map, 0);
  243. SAMPLER2D(u_normal_map, 1);
  244. SAMPLER2D(u_metallic_map, 2);
  245. SAMPLER2D(u_roughness_map, 3);
  246. SAMPLER2D(u_ao_map, 4);
  247. uniform vec4 u_albedo;
  248. uniform vec4 u_metallic;
  249. uniform vec4 u_roughness;
  250. uniform vec4 u_use_albedo_map;
  251. uniform vec4 u_use_normal_map;
  252. uniform vec4 u_use_metallic_map;
  253. uniform vec4 u_use_roughness_map;
  254. uniform vec4 u_use_ao_map;
  255. void main()
  256. {
  257. vec3 albedo = u_use_albedo_map.r == 1.0 ? texture2D(u_albedo_map, v_texcoord0).rgb : u_albedo.rgb;
  258. #if defined(NO_LIGHT)
  259. vec3 radiance = albedo;
  260. #else
  261. vec3 normal = u_use_normal_map.r == 1.0 ? decodeNormalUint(texture2D(u_normal_map, v_texcoord0).rgb) : v_normal;
  262. float metallic = u_use_metallic_map.r == 1.0 ? texture2D(u_metallic_map, v_texcoord0).r : u_metallic.r;
  263. float roughness = u_use_roughness_map.r == 1.0 ? texture2D(u_roughness_map, v_texcoord0).r: u_roughness.r;
  264. float ao = u_use_ao_map.r == 1.0 ? texture2D(u_ao_map, v_texcoord0).r : 0.0;
  265. mat3 tbn;
  266. if (u_use_normal_map.r == 1.0)
  267. tbn = mtxFromCols(v_tangent, v_bitangent, v_normal);
  268. else
  269. tbn = mtxFromCols(vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0));
  270. vec3 n = normalize(normal); // Fragment normal.
  271. vec3 v = normalize(v_camera); // Versor from fragment to camera pos.
  272. vec3 f0 = mix(vec3_splat(0.04), albedo, metallic);
  273. vec3 radiance = calc_lighting(tbn, n, v, v_position, albedo, metallic, roughness, f0);
  274. radiance = radiance / (radiance + vec3_splat(1.0)); // Tone-mapping.
  275. #endif // !defined(NO_LIGHT)
  276. gl_FragColor = vec4(toGammaAccurate(radiance), 1.0);
  277. }
  278. """
  279. }
  280. selection = {
  281. includes = "common"
  282. varying = """
  283. vec3 a_position : POSITION;
  284. """
  285. vs_input_output = """
  286. $input a_position
  287. """
  288. vs_code = """
  289. void main()
  290. {
  291. gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
  292. }
  293. """
  294. fs_input_output = """
  295. """
  296. fs_code = """
  297. uniform vec4 u_unit_id;
  298. void main()
  299. {
  300. gl_FragColor.r = u_unit_id.x;
  301. }
  302. """
  303. }
  304. outline = {
  305. includes = "common"
  306. varying = """
  307. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  308. vec3 a_position : POSITION;
  309. vec2 a_texcoord0 : TEXCOORD0;
  310. """
  311. vs_input_output = """
  312. $input a_position, a_texcoord0
  313. $output v_texcoord0
  314. """
  315. vs_code = """
  316. void main()
  317. {
  318. gl_Position = mul(u_viewProj, vec4(a_position.xy, 0.0, 1.0) );
  319. v_texcoord0 = a_texcoord0;
  320. }
  321. """
  322. fs_input_output = """
  323. $input v_texcoord0
  324. """
  325. fs_code = """
  326. #if !BX_PLATFORM_EMSCRIPTEN
  327. USAMPLER2D(s_selection, 0);
  328. SAMPLER2D(s_selection_depth, 1);
  329. SAMPLER2D(s_main_depth, 2);
  330. uniform vec4 u_outline_color;
  331. void main()
  332. {
  333. vec2 tex_size = vec2(textureSize(s_selection, 0)) - vec2(1, 1);
  334. uint id[8];
  335. id[0] = texelFetch(s_selection, ivec2(v_texcoord0 * tex_size + vec2(-1, -1)), 0).r;
  336. id[1] = texelFetch(s_selection, ivec2(v_texcoord0 * tex_size + vec2( 0, -1)), 0).r;
  337. id[2] = texelFetch(s_selection, ivec2(v_texcoord0 * tex_size + vec2( 1, -1)), 0).r;
  338. id[3] = texelFetch(s_selection, ivec2(v_texcoord0 * tex_size + vec2( 1, 0)), 0).r;
  339. id[4] = texelFetch(s_selection, ivec2(v_texcoord0 * tex_size + vec2( 1, 1)), 0).r;
  340. id[5] = texelFetch(s_selection, ivec2(v_texcoord0 * tex_size + vec2( 0, 1)), 0).r;
  341. id[6] = texelFetch(s_selection, ivec2(v_texcoord0 * tex_size + vec2(-1, 1)), 0).r;
  342. id[7] = texelFetch(s_selection, ivec2(v_texcoord0 * tex_size + vec2(-1, 0)), 0).r;
  343. uint ref_id = texelFetch(s_selection, ivec2(v_texcoord0 * tex_size), 0).r;
  344. float alpha = 0.0;
  345. for (int ii = 0; ii < 8; ++ii)
  346. {
  347. if (ref_id != id[ii])
  348. alpha += 1.0/8.0;
  349. }
  350. if (alpha == 0.0)
  351. {
  352. gl_FragColor = vec4(0, 0, 0, 0);
  353. return;
  354. }
  355. alpha = max(0.5, alpha);
  356. // Scan the depth around the center and choose the value closest
  357. // to the viewer. This is to avoid getting s_depth = 1.0.
  358. float s_depth = 1.0;
  359. s_depth = min(s_depth, texelFetch(s_selection_depth, ivec2(v_texcoord0 * tex_size + vec2(-1, -1)), 0).r);
  360. s_depth = min(s_depth, texelFetch(s_selection_depth, ivec2(v_texcoord0 * tex_size + vec2( 0, -1)), 0).r);
  361. s_depth = min(s_depth, texelFetch(s_selection_depth, ivec2(v_texcoord0 * tex_size + vec2( 1, -1)), 0).r);
  362. s_depth = min(s_depth, texelFetch(s_selection_depth, ivec2(v_texcoord0 * tex_size + vec2( 1, 0)), 0).r);
  363. s_depth = min(s_depth, texelFetch(s_selection_depth, ivec2(v_texcoord0 * tex_size + vec2( 1, 1)), 0).r);
  364. s_depth = min(s_depth, texelFetch(s_selection_depth, ivec2(v_texcoord0 * tex_size + vec2( 0, 1)), 0).r);
  365. s_depth = min(s_depth, texelFetch(s_selection_depth, ivec2(v_texcoord0 * tex_size + vec2(-1, 1)), 0).r);
  366. s_depth = min(s_depth, texelFetch(s_selection_depth, ivec2(v_texcoord0 * tex_size + vec2(-1, 0)), 0).r);
  367. s_depth = min(s_depth, texelFetch(s_selection_depth, ivec2(v_texcoord0 * tex_size + vec2( 0, 0)), 0).r);
  368. float m_depth = texelFetch(s_main_depth, ivec2(v_texcoord0 * tex_size), 0).r;
  369. // Dim alpha if selected object is behind another object.
  370. if (s_depth > m_depth)
  371. alpha *= 0.35;
  372. gl_FragColor = vec4(u_outline_color.xyz, alpha);
  373. }
  374. #else
  375. void main()
  376. {
  377. gl_FragColor = vec4_splat(0.0);
  378. }
  379. #endif
  380. """
  381. }
  382. blit = {
  383. includes = "common"
  384. varying = """
  385. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  386. vec3 a_position : POSITION;
  387. vec2 a_texcoord0 : TEXCOORD0;
  388. """
  389. vs_input_output = """
  390. $input a_position, a_texcoord0
  391. $output v_texcoord0
  392. """
  393. vs_code = """
  394. void main()
  395. {
  396. gl_Position = mul(u_viewProj, vec4(a_position.xy, 0.0, 1.0) );
  397. v_texcoord0 = a_texcoord0;
  398. }
  399. """
  400. fs_input_output = """
  401. $input v_texcoord0
  402. """
  403. fs_code = """
  404. SAMPLER2D(s_color, 0);
  405. void main()
  406. {
  407. gl_FragColor = texture2D(s_color, v_texcoord0);
  408. }
  409. """
  410. }
  411. fallback = {
  412. includes = "common"
  413. varying = """
  414. vec3 a_position : POSITION;
  415. """
  416. vs_input_output = """
  417. $input a_position
  418. """
  419. vs_code = """
  420. void main()
  421. {
  422. gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
  423. }
  424. """
  425. fs_input_output = """
  426. """
  427. fs_code = """
  428. void main()
  429. {
  430. gl_FragColor = toGammaAccurate(vec4(1.0, 0.0, 1.0, 1.0));
  431. }
  432. """
  433. }
  434. noop = {
  435. includes = "common"
  436. varying = """
  437. """
  438. vs_input_output = """
  439. """
  440. vs_code = """
  441. void main()
  442. {
  443. gl_Position = vec4_splat(0.0);
  444. }
  445. """
  446. fs_input_output = """
  447. """
  448. fs_code = """
  449. void main()
  450. {
  451. discard;
  452. }
  453. """
  454. }
  455. }
  456. shaders = {
  457. debug_line = {
  458. bgfx_shader = "debug_line"
  459. render_state = "debug_line"
  460. }
  461. gui = {
  462. bgfx_shader = "gui"
  463. render_state = "gui"
  464. }
  465. sprite = {
  466. bgfx_shader = "sprite"
  467. render_state = "sprite"
  468. }
  469. mesh = {
  470. bgfx_shader = "mesh"
  471. render_state = "mesh"
  472. }
  473. selection = {
  474. bgfx_shader = "selection"
  475. render_state = "selection"
  476. }
  477. outline = {
  478. bgfx_shader = "outline"
  479. render_state = "outline"
  480. }
  481. blit = {
  482. bgfx_shader = "blit"
  483. render_state = "blit"
  484. }
  485. fallback = {
  486. bgfx_shader = "fallback"
  487. render_state = "mesh"
  488. }
  489. noop = {
  490. bgfx_shader = "noop"
  491. render_state = "noop"
  492. }
  493. }
  494. static_compile = [
  495. { shader = "debug_line" defines = [] }
  496. { shader = "debug_line" defines = ["DEPTH_ENABLED"] }
  497. { shader = "gui" defines = [] }
  498. { shader = "gui" defines = ["DIFFUSE_MAP"]}
  499. { shader = "gui" defines = ["DEPTH_ENABLED"]}
  500. { shader = "gui" defines = ["DIFFUSE_MAP" "DEPTH_ENABLED"]}
  501. { shader = "sprite" defines = [] }
  502. { shader = "mesh" defines = [] }
  503. { shader = "mesh" defines = ["DIFFUSE_MAP"] }
  504. { shader = "mesh" defines = ["SKINNING"] }
  505. { shader = "mesh" defines = ["NO_LIGHT"] }
  506. { shader = "mesh" defines = ["DIFFUSE_MAP" "SKINNING"] }
  507. { shader = "mesh" defines = ["DIFFUSE_MAP" "NO_LIGHT"] }
  508. { shader = "selection" defines = [] }
  509. { shader = "outline" defines = [] }
  510. { shader = "blit" defines = [] }
  511. { shader = "fallback" defines = [] }
  512. { shader = "noop" defines = [] }
  513. ]