Advertisement
Guest User

Untitled

a guest
Jan 20th, 2025
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.05 KB | Source Code | 0 0
  1. #version 450
  2. #extension GL_EXT_debug_printf : enable
  3.  
  4. layout (local_size_x = 256, local_size_y = 1, local_size_z = 1) in;
  5.  
  6. #define PI 3.14159
  7.  
  8. struct particle
  9. {
  10.     vec2 pos;
  11.     float height;
  12.     float angle;
  13.     float tilt;
  14.     float vel;
  15.     float opacity;
  16.     float temp;
  17. };
  18.  
  19. struct draw_indirect_cmd
  20. {
  21.     uint vertex_count;
  22.     uint instance_count;
  23.     uint first_vertex;
  24.     uint first_instance;
  25. };
  26.  
  27. layout (binding = 0) uniform frustum_info
  28. {
  29.     vec4 planes[6];
  30. }   frustum;
  31.  
  32. layout (std430, binding = 1) buffer stars
  33. {
  34.     particle particles[];
  35. }   particles;
  36.  
  37. layout (std430, binding = 2) writeonly buffer indirect_draws
  38. {
  39.     draw_indirect_cmd draws[];
  40. }   draws;
  41.  
  42. layout (std140, binding = 3) buffer count_buffer
  43. {
  44.     uint draw_count;
  45. }   count;
  46.  
  47. layout (push_constant) uniform galaxy
  48. {
  49.     uint star_count;
  50.     float max_rad, bulge_rad;
  51.     float angle, eccentricity;
  52.     float base_height, height;
  53.     float dust_temp, min_temp, max_temp;
  54.     float min_star_opacity, max_star_opacity, min_dust_opacity, max_dust_opacity;
  55.     float speed;
  56. }   galaxy_params;
  57.  
  58. // http://www.gamedev.net/topic/592001-random-number-generation-based-on-time-in-hlsl/
  59. #define RANDOM_IA 16807
  60. #define RANDOM_IM 2147483647
  61. #define RANDOM_AM 1.0 / float(RANDOM_IM)
  62. #define RANDOM_IQ 127773
  63. #define RANDOM_IR 2836
  64. #define RANDOM_MASK 123459876
  65.  
  66. int _SEED = 0;
  67.  
  68. void srand_cycle(void)
  69. {
  70.     _SEED ^= RANDOM_MASK;
  71.     int k = _SEED / RANDOM_IQ;
  72.     _SEED = RANDOM_IA * (_SEED - k * RANDOM_IQ) - RANDOM_IR * k;
  73.  
  74.     if (_SEED < 0)
  75.         _SEED += RANDOM_IM;
  76.  
  77.     _SEED ^= RANDOM_MASK;
  78. }
  79.  
  80. void srand_set(int seed)
  81. {
  82.     _SEED = seed;
  83.     srand_cycle();
  84. }
  85.  
  86. float rand(void)
  87. {
  88.     srand_cycle();
  89.     return RANDOM_AM * _SEED;
  90. }
  91.  
  92. float ease_in_exp(float x)
  93. {
  94.     return x <= 0.0 ? 0.0 : pow(2, 10.0 * x - 10.0);
  95. }
  96.  
  97. float ease_in_circ(float x)
  98. {
  99.     return x >= 1.0 ? 1.0 : 1.0 - sqrt(1.0 - x * x);
  100. }
  101.  
  102. float rand_height(void)
  103. {
  104.     float r = ease_in_circ(rand());
  105.     if (rand() < 0.5)
  106.         r *= -1;
  107.  
  108.     return galaxy_params.base_height + (galaxy_params.height * 0.5) * r;
  109. }
  110.  
  111. float rand_height_bulge(float rad)
  112. {
  113.     float r = ease_in_circ(rand());
  114.     if (rand() < 0.5)
  115.         r *= -1;
  116.  
  117.     float bound = (galaxy_params.height * 0.5) + (galaxy_params.height * 0.5) * cos(PI * rad / galaxy_params.bulge_rad);
  118.  
  119.     return galaxy_params.base_height + bound * r;
  120. }
  121.  
  122. float rand_height_bulge_dust(float rad)
  123. {
  124.     float r = ease_in_circ(rand());
  125.     if (rand() < 0.5)
  126.         r *= -1;
  127.  
  128.     float bound = (galaxy_params.height * 0.5) * cos(PI * rad / galaxy_params.bulge_rad);
  129.  
  130.     return galaxy_params.base_height + bound * r;
  131. }
  132.  
  133. bool frustum_check(vec4 pos, float radius)
  134. {
  135.     for (uint i = 0; i < 6; i++)
  136.     {
  137.         if (dot(pos, frustum.planes[i]) + radius < 0.0)
  138.         {
  139.             return false;
  140.         }
  141.     }
  142.  
  143.     return true;
  144. }
  145.  
  146. void main(void)
  147. {
  148.     srand_set(int(gl_GlobalInvocationID.x));
  149.     particle p;
  150.     float speed = galaxy_params.speed;
  151.     uint type = 0;
  152.  
  153.     if (gl_GlobalInvocationID.x < galaxy_params.star_count) // Star
  154.     {
  155.         float rad = ease_in_exp(rand()) * galaxy_params.max_rad;
  156.  
  157.         p.pos = vec2(rad, galaxy_params.eccentricity * rad);
  158.         p.angle = rand() * 2.0 * PI;
  159.         p.tilt = (rad / galaxy_params.max_rad) * galaxy_params.angle;
  160.         p.vel = -speed * sqrt(1.0 / rad);
  161.         p.opacity = galaxy_params.min_star_opacity + (galaxy_params.max_star_opacity - galaxy_params.min_star_opacity) * rand();
  162.         p.temp = galaxy_params.min_temp + (galaxy_params.max_temp - galaxy_params.min_temp) * rand();
  163.  
  164.         if (rad < galaxy_params.bulge_rad)
  165.             p.height = rand_height_bulge(rad);
  166.         else
  167.             p.height = rand_height();
  168.  
  169.         type = 0;
  170.     }
  171.     else // Dust
  172.     {
  173.         float rad;
  174.         if (gl_GlobalInvocationID.x % 2 == 0)
  175.             rad = rand() * galaxy_params.max_rad;
  176.         else
  177.             rad = ease_in_exp(rand()) * galaxy_params.max_rad;
  178.  
  179.         p.pos = vec2(rad, galaxy_params.eccentricity * rad);
  180.         p.angle = rand() * 2.0 * PI;
  181.         p.tilt = (rad / galaxy_params.max_rad) * galaxy_params.angle;
  182.         p.vel = -speed * sqrt(1.0 / rad);
  183.         p.temp = galaxy_params.dust_temp + 2.0 * rad;
  184.         p.opacity = galaxy_params.min_dust_opacity + (galaxy_params.max_dust_opacity - galaxy_params.min_dust_opacity) * rand();
  185.  
  186.         if (rad < galaxy_params.bulge_rad)
  187.             p.height = rand_height_bulge_dust(rad);
  188.         else
  189.             p.height = galaxy_params.base_height;
  190.  
  191.         type = 1;
  192.     }
  193.  
  194.     vec4 pos = {p.pos.x, p.height, p.pos.y, 1.0};
  195.  
  196.     float size = 0.0;
  197.     if (type == 0)  // Star
  198.     {
  199.         size = 10.0;
  200.     }
  201.     else    // Dust
  202.     {
  203.         size = 500.0;
  204.     }
  205.  
  206.     debugPrintfEXT("Size: %f\n", size);
  207.  
  208.     // Cull against stars
  209.     if (frustum_check(pos, size))
  210.     {
  211.         uint draw_cmd_index = atomicAdd(count.draw_count, 6);
  212.         draws.draws[gl_GlobalInvocationID.x].vertex_count = 6;
  213.  
  214.         if (gl_GlobalInvocationID.x == 0)
  215.         {
  216.             draws.draws[gl_GlobalInvocationID.x].instance_count = 1;
  217.         }
  218.         else
  219.         {
  220.             draws.draws[gl_GlobalInvocationID.x].instance_count = 0;
  221.         }
  222.  
  223.         draws.draws[gl_GlobalInvocationID.x].first_instance = 0;
  224.         draws.draws[gl_GlobalInvocationID.x].first_vertex = 0;
  225.         particles.particles[gl_GlobalInvocationID.x] = p;
  226.         debugPrintfEXT("Not culled, index: %u\n", gl_GlobalInvocationID.x);
  227.     }
  228.     else
  229.     {
  230.         debugPrintfEXT("Culled, index: %u\n", gl_GlobalInvocationID.x);
  231.     }
  232.  
  233. }
  234.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement