default.shader 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. include = ["core/shaders/common.shader"]
  2. render_states = {
  3. debug_line = {
  4. blend_enable = true
  5. blend_src = "src_alpha"
  6. blend_dst = "inv_src_alpha"
  7. primitive_type = "pt_lines"
  8. }
  9. debug_line_noz = {
  10. inherit = "debug_line"
  11. depth_enable = false
  12. }
  13. gui = {
  14. depth_write_enable = false
  15. depth_enable = false
  16. blend_enable = true
  17. blend_src = "src_alpha"
  18. blend_dst = "inv_src_alpha"
  19. }
  20. gui_noblend = {
  21. inherit = "gui"
  22. blend_enable = false
  23. }
  24. sprite = {
  25. depth_func = "always"
  26. blend_enable = true
  27. blend_src = "src_alpha"
  28. blend_dst = "inv_src_alpha"
  29. blend_equation = "add"
  30. }
  31. mesh = {
  32. }
  33. selection = {
  34. alpha_write_enable = false
  35. }
  36. noop = {
  37. rgb_write_enable = false
  38. alpha_write_enable = false
  39. depth_write_enable = false
  40. depth_enable = false
  41. blend_enable = false
  42. }
  43. }
  44. bgfx_shaders = {
  45. debug_line = {
  46. includes = "common"
  47. varying = """
  48. vec4 v_color0 : COLOR0 = vec4(0.0, 0.0, 0.0, 0.0);
  49. vec3 a_position : POSITION;
  50. vec4 a_color0 : COLOR0;
  51. """
  52. vs_input_output = """
  53. $input a_position, a_color0
  54. $output v_color0
  55. """
  56. vs_code = """
  57. void main()
  58. {
  59. gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
  60. v_color0 = toLinearAccurate(a_color0);
  61. }
  62. """
  63. fs_input_output = """
  64. $input v_color0
  65. """
  66. fs_code = """
  67. void main()
  68. {
  69. gl_FragColor = v_color0;
  70. }
  71. """
  72. }
  73. gui = {
  74. includes = "common"
  75. samplers = {
  76. u_albedo = { sampler_state = "clamp_anisotropic" }
  77. }
  78. varying = """
  79. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  80. vec4 v_color0 : COLOR0 = vec4(0.0, 0.0, 0.0, 0.0);
  81. vec3 a_position : POSITION;
  82. vec2 a_texcoord0 : TEXCOORD0;
  83. vec4 a_color0 : COLOR0;
  84. """
  85. vs_input_output = """
  86. $input a_position, a_texcoord0, a_color0
  87. $output v_texcoord0, v_color0
  88. """
  89. vs_code = """
  90. void main()
  91. {
  92. gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
  93. #ifdef DIFFUSE_MAP
  94. v_texcoord0 = a_texcoord0;
  95. #endif // DIFFUSE_MAP
  96. v_color0 = toLinearAccurate(a_color0);
  97. }
  98. """
  99. fs_input_output = """
  100. $input v_texcoord0, v_color0
  101. """
  102. fs_code = """
  103. #ifdef DIFFUSE_MAP
  104. SAMPLER2D(u_albedo, 0);
  105. #endif // DIFFUSE_MAP
  106. void main()
  107. {
  108. #ifdef DIFFUSE_MAP
  109. gl_FragColor = texture2D(u_albedo, v_texcoord0) * v_color0;
  110. #else
  111. gl_FragColor = v_color0;
  112. #endif // DIFFUSE_MAP
  113. }
  114. """
  115. }
  116. sprite = {
  117. includes = "common"
  118. samplers = {
  119. u_albedo = { sampler_state = "clamp_point" }
  120. }
  121. varying = """
  122. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  123. vec3 a_position : POSITION;
  124. vec2 a_texcoord0 : TEXCOORD0;
  125. """
  126. vs_input_output = """
  127. $input a_position, a_texcoord0
  128. $output v_texcoord0
  129. """
  130. vs_code = """
  131. void main()
  132. {
  133. gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
  134. v_texcoord0 = a_texcoord0;
  135. }
  136. """
  137. fs_input_output = """
  138. $input v_texcoord0
  139. """
  140. fs_code = """
  141. uniform vec4 u_color;
  142. SAMPLER2D(u_albedo, 0);
  143. void main()
  144. {
  145. gl_FragColor = texture2D(u_albedo, v_texcoord0) * toLinearAccurate(u_color);
  146. }
  147. """
  148. }
  149. mesh = {
  150. includes = "common"
  151. samplers = {
  152. u_albedo = { sampler_state = "mirror_anisotropic" }
  153. }
  154. varying = """
  155. vec3 v_normal : NORMAL = vec3(0.0, 0.0, 0.0);
  156. vec4 v_view : TEXCOORD0 = vec4(0.0, 0.0, 0.0, 0.0);
  157. vec2 v_texcoord0 : TEXCOORD1 = vec2(0.0, 0.0);
  158. vec3 a_position : POSITION;
  159. vec3 a_normal : NORMAL;
  160. vec2 a_texcoord0 : TEXCOORD0;
  161. """
  162. vs_input_output = """
  163. $input a_position, a_normal, a_texcoord0
  164. $output v_normal, v_view, v_texcoord0
  165. """
  166. vs_code = """
  167. void main()
  168. {
  169. gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
  170. v_view = mul(u_modelView, vec4(a_position, 1.0));
  171. v_normal = normalize(mul(u_modelView, vec4(a_normal, 0.0)).xyz);
  172. v_texcoord0 = a_texcoord0;
  173. }
  174. """
  175. fs_input_output = """
  176. $input v_normal, v_view, v_texcoord0
  177. """
  178. fs_code = """
  179. #if !defined(NO_LIGHT)
  180. uniform vec4 u_light_position; // In world-space
  181. uniform vec4 u_light_direction; // In view-space
  182. uniform vec4 u_light_color;
  183. uniform vec4 u_light_range;
  184. uniform vec4 u_light_intensity;
  185. uniform vec4 u_ambient;
  186. uniform vec4 u_diffuse;
  187. uniform vec4 u_specular;
  188. #endif
  189. #ifdef DIFFUSE_MAP
  190. SAMPLER2D(u_albedo, 0);
  191. #endif // DIFFUSE_MAP
  192. void main()
  193. {
  194. #if !defined(NO_LIGHT)
  195. // normalize both input vectors
  196. vec3 n = normalize(v_normal);
  197. vec3 e = normalize(v_view.xyz);
  198. vec3 l = u_light_direction.xyz;
  199. float nl = max(0.0, dot(n, l));
  200. vec3 light_diffuse = nl * toLinearAccurate(u_light_color.rgb) * u_light_intensity.x;
  201. vec3 color = max(u_diffuse.rgb * light_diffuse, u_ambient.rgb);
  202. #else
  203. vec3 color = vec3(1.0f, 1.0f, 1.0f);
  204. #endif // !defined(NO_LIGHT)
  205. #ifdef DIFFUSE_MAP
  206. gl_FragColor.rgb = color * texture2D(u_albedo, v_texcoord0).rgb;
  207. #else
  208. gl_FragColor.rgb = color;
  209. #endif // DIFFUSE_MAP
  210. gl_FragColor.a = 1.0;
  211. }
  212. """
  213. }
  214. selection = {
  215. includes = "common"
  216. varying = """
  217. vec3 a_position : POSITION;
  218. """
  219. vs_input_output = """
  220. $input a_position
  221. """
  222. vs_code = """
  223. void main()
  224. {
  225. gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
  226. }
  227. """
  228. fs_input_output = """
  229. """
  230. fs_code = """
  231. uniform vec4 u_unit_id;
  232. void main()
  233. {
  234. gl_FragColor.r = u_unit_id.x;
  235. }
  236. """
  237. }
  238. outline = {
  239. includes = "common"
  240. varying = """
  241. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  242. vec3 a_position : POSITION;
  243. vec2 a_texcoord0 : TEXCOORD0;
  244. """
  245. vs_input_output = """
  246. $input a_position, a_texcoord0
  247. $output v_texcoord0
  248. """
  249. vs_code = """
  250. void main()
  251. {
  252. gl_Position = mul(u_viewProj, vec4(a_position.xy, 0.0, 1.0) );
  253. v_texcoord0 = a_texcoord0;
  254. }
  255. """
  256. fs_input_output = """
  257. $input v_texcoord0
  258. """
  259. fs_code = """
  260. #if !BX_PLATFORM_EMSCRIPTEN
  261. USAMPLER2D(s_selection, 0);
  262. SAMPLER2D(s_selection_depth, 1);
  263. SAMPLER2D(s_main_depth, 2);
  264. uniform vec4 u_outline_color;
  265. void main()
  266. {
  267. vec2 tex_size = vec2(textureSize(s_selection, 0)) - vec2(1, 1);
  268. uint id[8];
  269. id[0] = texelFetch(s_selection, ivec2(v_texcoord0 * tex_size + vec2(-1, -1)), 0).r;
  270. id[1] = texelFetch(s_selection, ivec2(v_texcoord0 * tex_size + vec2( 0, -1)), 0).r;
  271. id[2] = texelFetch(s_selection, ivec2(v_texcoord0 * tex_size + vec2( 1, -1)), 0).r;
  272. id[3] = texelFetch(s_selection, ivec2(v_texcoord0 * tex_size + vec2( 1, 0)), 0).r;
  273. id[4] = texelFetch(s_selection, ivec2(v_texcoord0 * tex_size + vec2( 1, 1)), 0).r;
  274. id[5] = texelFetch(s_selection, ivec2(v_texcoord0 * tex_size + vec2( 0, 1)), 0).r;
  275. id[6] = texelFetch(s_selection, ivec2(v_texcoord0 * tex_size + vec2(-1, 1)), 0).r;
  276. id[7] = texelFetch(s_selection, ivec2(v_texcoord0 * tex_size + vec2(-1, 0)), 0).r;
  277. uint ref_id = texelFetch(s_selection, ivec2(v_texcoord0 * tex_size), 0).r;
  278. float alpha = 0.0;
  279. for (int ii = 0; ii < 8; ++ii)
  280. {
  281. if (ref_id != id[ii])
  282. alpha += 1.0/8.0;
  283. }
  284. if (alpha == 0.0)
  285. {
  286. gl_FragColor = vec4(0, 0, 0, 0);
  287. return;
  288. }
  289. alpha = max(0.5, alpha);
  290. // Scan the depth around the center and choose the value closest
  291. // to the viewer. This is to avoid getting s_depth = 1.0.
  292. float s_depth = 1.0;
  293. s_depth = min(s_depth, texelFetch(s_selection_depth, ivec2(v_texcoord0 * tex_size + vec2(-1, -1)), 0).r);
  294. s_depth = min(s_depth, texelFetch(s_selection_depth, ivec2(v_texcoord0 * tex_size + vec2( 0, -1)), 0).r);
  295. s_depth = min(s_depth, texelFetch(s_selection_depth, ivec2(v_texcoord0 * tex_size + vec2( 1, -1)), 0).r);
  296. s_depth = min(s_depth, texelFetch(s_selection_depth, ivec2(v_texcoord0 * tex_size + vec2( 1, 0)), 0).r);
  297. s_depth = min(s_depth, texelFetch(s_selection_depth, ivec2(v_texcoord0 * tex_size + vec2( 1, 1)), 0).r);
  298. s_depth = min(s_depth, texelFetch(s_selection_depth, ivec2(v_texcoord0 * tex_size + vec2( 0, 1)), 0).r);
  299. s_depth = min(s_depth, texelFetch(s_selection_depth, ivec2(v_texcoord0 * tex_size + vec2(-1, 1)), 0).r);
  300. s_depth = min(s_depth, texelFetch(s_selection_depth, ivec2(v_texcoord0 * tex_size + vec2(-1, 0)), 0).r);
  301. s_depth = min(s_depth, texelFetch(s_selection_depth, ivec2(v_texcoord0 * tex_size + vec2( 0, 0)), 0).r);
  302. float m_depth = texelFetch(s_main_depth, ivec2(v_texcoord0 * tex_size), 0).r;
  303. // Dim alpha if selected object is behind another object.
  304. if (s_depth > m_depth)
  305. alpha *= 0.35;
  306. gl_FragColor = vec4(u_outline_color.xyz, alpha);
  307. }
  308. #else
  309. void main()
  310. {
  311. gl_FragColor = vec4_splat(0.0);
  312. }
  313. #endif
  314. """
  315. }
  316. blit = {
  317. includes = "common"
  318. varying = """
  319. vec2 v_texcoord0 : TEXCOORD0 = vec2(0.0, 0.0);
  320. vec3 a_position : POSITION;
  321. vec2 a_texcoord0 : TEXCOORD0;
  322. """
  323. vs_input_output = """
  324. $input a_position, a_texcoord0
  325. $output v_texcoord0
  326. """
  327. vs_code = """
  328. void main()
  329. {
  330. gl_Position = mul(u_viewProj, vec4(a_position.xy, 0.0, 1.0) );
  331. v_texcoord0 = a_texcoord0;
  332. }
  333. """
  334. fs_input_output = """
  335. $input v_texcoord0
  336. """
  337. fs_code = """
  338. SAMPLER2D(s_color, 0);
  339. void main()
  340. {
  341. gl_FragColor = toGammaAccurate(texture2D(s_color, v_texcoord0));
  342. }
  343. """
  344. }
  345. fallback = {
  346. includes = "common"
  347. varying = """
  348. vec3 a_position : POSITION;
  349. """
  350. vs_input_output = """
  351. $input a_position
  352. """
  353. vs_code = """
  354. void main()
  355. {
  356. gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0));
  357. }
  358. """
  359. fs_input_output = """
  360. """
  361. fs_code = """
  362. void main()
  363. {
  364. gl_FragColor = vec4(1.0, 0.0, 1.0, 1.0);
  365. }
  366. """
  367. }
  368. noop = {
  369. includes = "common"
  370. varying = """
  371. """
  372. vs_input_output = """
  373. """
  374. vs_code = """
  375. void main()
  376. {
  377. gl_Position = vec4_splat(0.0);
  378. }
  379. """
  380. fs_input_output = """
  381. """
  382. fs_code = """
  383. void main()
  384. {
  385. discard;
  386. }
  387. """
  388. }
  389. }
  390. shaders = {
  391. debug_line = {
  392. bgfx_shader = "debug_line"
  393. render_state = "debug_line"
  394. }
  395. debug_line_noz = {
  396. bgfx_shader = "debug_line"
  397. render_state = "debug_line_noz"
  398. }
  399. gui = {
  400. bgfx_shader = "gui"
  401. render_state = "gui"
  402. }
  403. sprite = {
  404. bgfx_shader = "sprite"
  405. render_state = "sprite"
  406. }
  407. mesh = {
  408. bgfx_shader = "mesh"
  409. render_state = "mesh"
  410. }
  411. selection = {
  412. bgfx_shader = "selection"
  413. render_state = "selection"
  414. }
  415. outline = {
  416. bgfx_shader = "outline"
  417. render_state = "gui"
  418. }
  419. blit = {
  420. bgfx_shader = "blit"
  421. render_state = "gui_noblend"
  422. }
  423. fallback = {
  424. bgfx_shader = "fallback"
  425. render_state = "mesh"
  426. }
  427. noop = {
  428. bgfx_shader = "noop"
  429. render_state = "noop"
  430. }
  431. }
  432. static_compile = [
  433. { shader = "debug_line" defines = [] }
  434. { shader = "debug_line_noz" defines = [] }
  435. { shader = "gui" defines = [] }
  436. { shader = "gui" defines = ["DIFFUSE_MAP"]}
  437. { shader = "sprite" defines = [] }
  438. { shader = "mesh" defines = [] }
  439. { shader = "mesh" defines = ["DIFFUSE_MAP"] }
  440. { shader = "mesh" defines = ["DIFFUSE_MAP" "NO_LIGHT"] }
  441. { shader = "selection" defines = [] }
  442. { shader = "outline" defines = [] }
  443. { shader = "blit" defines = [] }
  444. { shader = "fallback" defines = [] }
  445. { shader = "noop" defines = [] }
  446. ]