shaders.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import {FlamegraphTheme} from '../flamegraph/flamegraphTheme';
  2. export const vertex = () => `
  3. attribute vec2 a_position;
  4. attribute vec4 a_color;
  5. attribute vec4 a_bounds;
  6. attribute float a_is_search_result;
  7. uniform mat3 u_model;
  8. uniform mat3 u_projection;
  9. varying vec4 v_color;
  10. varying vec2 v_pos;
  11. varying vec4 v_bounds;
  12. varying float v_is_search_result;
  13. void main() {
  14. vec2 scaled = (u_model * vec3(a_position.xy, 1)).xy;
  15. vec2 pos = (u_projection * vec3(scaled.xy, 1)).xy;
  16. gl_Position = vec4(pos, 0.0, 1.0);
  17. v_color = a_color;
  18. v_pos = a_position.xy;
  19. v_bounds = a_bounds;
  20. v_is_search_result = a_is_search_result;
  21. }
  22. `;
  23. export const fragment = (theme: FlamegraphTheme) => `
  24. precision mediump float;
  25. uniform vec2 u_border_width;
  26. uniform bool u_draw_border;
  27. uniform bool u_grayscale;
  28. varying vec4 v_color;
  29. varying vec4 v_bounds;
  30. varying vec2 v_pos;
  31. varying float v_is_search_result;
  32. void main() {
  33. float minX = v_bounds.x + u_border_width.x;
  34. float maxX = v_bounds.z - u_border_width.x;
  35. float minY = v_bounds.y + u_border_width.y;
  36. float maxY = v_bounds.y + 1.0 - u_border_width.y;
  37. float width = maxX - minX;
  38. vec4 color = vec4(v_color);
  39. if(u_grayscale && v_is_search_result == 0.0) {
  40. color = vec4(${theme.COLORS.FRAME_GRAYSCALE_COLOR.join(',')});
  41. }
  42. if (u_draw_border) {
  43. if(width <= u_border_width.x) {
  44. if(v_pos.y > minY && v_pos.y < maxY){
  45. gl_FragColor = color;
  46. } else {
  47. gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
  48. }
  49. } else if (v_pos.x > minX && v_pos.x < maxX && v_pos.y > minY && v_pos.y < maxY) {
  50. gl_FragColor = color;
  51. } else {
  52. gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
  53. }
  54. } else {
  55. gl_FragColor = color;
  56. }
  57. }
  58. `;
  59. export const uiFramesVertext = () => `
  60. attribute vec2 a_position;
  61. attribute float a_frame_type;
  62. attribute vec4 a_bounds;
  63. uniform mat3 u_model;
  64. uniform mat3 u_projection;
  65. varying float v_frame_type;
  66. varying vec2 v_pos;
  67. varying vec4 v_bounds;
  68. void main() {
  69. vec2 scaled = (u_model * vec3(a_position.xy, 1)).xy;
  70. vec2 pos = (u_projection * vec3(scaled.xy, 1)).xy;
  71. gl_Position = vec4(pos, 0.0, 1.0);
  72. v_frame_type = a_frame_type;
  73. v_pos = a_position.xy;
  74. v_bounds = a_bounds;
  75. }
  76. `;
  77. export const uiFramesFragment = (theme: FlamegraphTheme) => `
  78. precision mediump float;
  79. uniform vec2 u_border_width;
  80. varying float v_frame_type;
  81. varying vec4 v_bounds;
  82. varying vec2 v_pos;
  83. void main() {
  84. float minX = v_bounds.x + u_border_width.x;
  85. float maxX = v_bounds.z - u_border_width.x;
  86. float minY = v_bounds.y + u_border_width.y;
  87. float maxY = v_bounds.y + 1.0 - u_border_width.y;
  88. float width = maxX - minX;
  89. vec4 color = vec4(0.0, 0.0, 0.0, 0.0);
  90. if(v_frame_type == 1.0) {
  91. color = vec4(${theme.COLORS.UI_FRAME_COLOR_FROZEN.join(',')});
  92. } else {
  93. color = vec4(${theme.COLORS.UI_FRAME_COLOR_SLOW.join(',')});
  94. }
  95. if(width <= u_border_width.x) {
  96. if(v_pos.y > minY && v_pos.y < maxY){
  97. gl_FragColor = color;
  98. } else {
  99. gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
  100. }
  101. } else if (v_pos.x > minX && v_pos.x < maxX && v_pos.y > minY && v_pos.y < maxY) {
  102. gl_FragColor = color;
  103. } else {
  104. gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0);
  105. }
  106. }
  107. `;