2
0

scene_forward_clustered_inc.glsl 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. #define M_PI 3.14159265359
  2. #define ROUGHNESS_MAX_LOD 5
  3. #define MAX_GI_PROBES 8
  4. #if defined(has_GL_KHR_shader_subgroup_ballot) && defined(has_GL_KHR_shader_subgroup_arithmetic)
  5. #extension GL_KHR_shader_subgroup_ballot : enable
  6. #extension GL_KHR_shader_subgroup_arithmetic : enable
  7. #define USE_SUBGROUPS
  8. #endif
  9. #include "cluster_data_inc.glsl"
  10. #if !defined(MODE_RENDER_DEPTH) || defined(MODE_RENDER_MATERIAL) || defined(MODE_RENDER_SDF) || defined(MODE_RENDER_NORMAL_ROUGHNESS) || defined(MODE_RENDER_GIPROBE) || defined(TANGENT_USED) || defined(NORMAL_MAP_USED)
  11. #ifndef NORMAL_USED
  12. #define NORMAL_USED
  13. #endif
  14. #endif
  15. layout(push_constant, binding = 0, std430) uniform DrawCall {
  16. uint instance_index;
  17. uint uv_offset;
  18. uint pad0;
  19. uint pad1;
  20. }
  21. draw_call;
  22. /* Set 0 Scene data that never changes, ever */
  23. #define SAMPLER_NEAREST_CLAMP 0
  24. #define SAMPLER_LINEAR_CLAMP 1
  25. #define SAMPLER_NEAREST_WITH_MIPMAPS_CLAMP 2
  26. #define SAMPLER_LINEAR_WITH_MIPMAPS_CLAMP 3
  27. #define SAMPLER_NEAREST_WITH_MIPMAPS_ANISOTROPIC_CLAMP 4
  28. #define SAMPLER_LINEAR_WITH_MIPMAPS_ANISOTROPIC_CLAMP 5
  29. #define SAMPLER_NEAREST_REPEAT 6
  30. #define SAMPLER_LINEAR_REPEAT 7
  31. #define SAMPLER_NEAREST_WITH_MIPMAPS_REPEAT 8
  32. #define SAMPLER_LINEAR_WITH_MIPMAPS_REPEAT 9
  33. #define SAMPLER_NEAREST_WITH_MIPMAPS_ANISOTROPIC_REPEAT 10
  34. #define SAMPLER_LINEAR_WITH_MIPMAPS_ANISOTROPIC_REPEAT 11
  35. #define SDFGI_MAX_CASCADES 8
  36. /* Set 1: Base Pass (never changes) */
  37. layout(set = 0, binding = 1) uniform sampler material_samplers[12];
  38. layout(set = 0, binding = 2) uniform sampler shadow_sampler;
  39. #define INSTANCE_FLAGS_USE_GI_BUFFERS (1 << 6)
  40. #define INSTANCE_FLAGS_USE_SDFGI (1 << 7)
  41. #define INSTANCE_FLAGS_USE_LIGHTMAP_CAPTURE (1 << 8)
  42. #define INSTANCE_FLAGS_USE_LIGHTMAP (1 << 9)
  43. #define INSTANCE_FLAGS_USE_SH_LIGHTMAP (1 << 10)
  44. #define INSTANCE_FLAGS_USE_GIPROBE (1 << 11)
  45. #define INSTANCE_FLAGS_MULTIMESH (1 << 12)
  46. #define INSTANCE_FLAGS_MULTIMESH_FORMAT_2D (1 << 13)
  47. #define INSTANCE_FLAGS_MULTIMESH_HAS_COLOR (1 << 14)
  48. #define INSTANCE_FLAGS_MULTIMESH_HAS_CUSTOM_DATA (1 << 15)
  49. #define INSTANCE_FLAGS_PARTICLE_TRAIL_SHIFT 16
  50. //3 bits of stride
  51. #define INSTANCE_FLAGS_PARTICLE_TRAIL_MASK 0xFF
  52. #define INSTANCE_FLAGS_NON_UNIFORM_SCALE (1 << 24)
  53. layout(set = 0, binding = 3, std430) restrict readonly buffer OmniLights {
  54. LightData data[];
  55. }
  56. omni_lights;
  57. layout(set = 0, binding = 4, std430) restrict readonly buffer SpotLights {
  58. LightData data[];
  59. }
  60. spot_lights;
  61. layout(set = 0, binding = 5, std430) restrict readonly buffer ReflectionProbeData {
  62. ReflectionData data[];
  63. }
  64. reflections;
  65. layout(set = 0, binding = 6, std140) uniform DirectionalLights {
  66. DirectionalLightData data[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS];
  67. }
  68. directional_lights;
  69. #define LIGHTMAP_FLAG_USE_DIRECTION 1
  70. #define LIGHTMAP_FLAG_USE_SPECULAR_DIRECTION 2
  71. struct Lightmap {
  72. mat3 normal_xform;
  73. };
  74. layout(set = 0, binding = 7, std140) restrict readonly buffer Lightmaps {
  75. Lightmap data[];
  76. }
  77. lightmaps;
  78. struct LightmapCapture {
  79. vec4 sh[9];
  80. };
  81. layout(set = 0, binding = 8, std140) restrict readonly buffer LightmapCaptures {
  82. LightmapCapture data[];
  83. }
  84. lightmap_captures;
  85. layout(set = 0, binding = 9) uniform texture2D decal_atlas;
  86. layout(set = 0, binding = 10) uniform texture2D decal_atlas_srgb;
  87. layout(set = 0, binding = 11, std430) restrict readonly buffer Decals {
  88. DecalData data[];
  89. }
  90. decals;
  91. layout(set = 0, binding = 12, std430) restrict readonly buffer GlobalVariableData {
  92. vec4 data[];
  93. }
  94. global_variables;
  95. struct SDFGIProbeCascadeData {
  96. vec3 position;
  97. float to_probe;
  98. ivec3 probe_world_offset;
  99. float to_cell; // 1/bounds * grid_size
  100. };
  101. layout(set = 0, binding = 13, std140) uniform SDFGI {
  102. vec3 grid_size;
  103. uint max_cascades;
  104. bool use_occlusion;
  105. int probe_axis_size;
  106. float probe_to_uvw;
  107. float normal_bias;
  108. vec3 lightprobe_tex_pixel_size;
  109. float energy;
  110. vec3 lightprobe_uv_offset;
  111. float y_mult;
  112. vec3 occlusion_clamp;
  113. uint pad3;
  114. vec3 occlusion_renormalize;
  115. uint pad4;
  116. vec3 cascade_probe_size;
  117. uint pad5;
  118. SDFGIProbeCascadeData cascades[SDFGI_MAX_CASCADES];
  119. }
  120. sdfgi;
  121. /* Set 2: Render Pass (changes per render pass) */
  122. layout(set = 1, binding = 0, std140) uniform SceneData {
  123. mat4 projection_matrix;
  124. mat4 inv_projection_matrix;
  125. mat4 camera_matrix;
  126. mat4 inv_camera_matrix;
  127. vec2 viewport_size;
  128. vec2 screen_pixel_size;
  129. uint cluster_shift;
  130. uint cluster_width;
  131. uint cluster_type_size;
  132. uint max_cluster_element_count_div_32;
  133. //use vec4s because std140 doesnt play nice with vec2s, z and w are wasted
  134. vec4 directional_penumbra_shadow_kernel[32];
  135. vec4 directional_soft_shadow_kernel[32];
  136. vec4 penumbra_shadow_kernel[32];
  137. vec4 soft_shadow_kernel[32];
  138. uint directional_penumbra_shadow_samples;
  139. uint directional_soft_shadow_samples;
  140. uint penumbra_shadow_samples;
  141. uint soft_shadow_samples;
  142. vec4 ambient_light_color_energy;
  143. float ambient_color_sky_mix;
  144. bool use_ambient_light;
  145. bool use_ambient_cubemap;
  146. bool use_reflection_cubemap;
  147. mat3 radiance_inverse_xform;
  148. vec2 shadow_atlas_pixel_size;
  149. vec2 directional_shadow_pixel_size;
  150. uint directional_light_count;
  151. float dual_paraboloid_side;
  152. float z_far;
  153. float z_near;
  154. bool ssao_enabled;
  155. float ssao_light_affect;
  156. float ssao_ao_affect;
  157. bool roughness_limiter_enabled;
  158. float roughness_limiter_amount;
  159. float roughness_limiter_limit;
  160. uvec2 roughness_limiter_pad;
  161. vec4 ao_color;
  162. mat4 sdf_to_bounds;
  163. ivec3 sdf_offset;
  164. bool material_uv2_mode;
  165. ivec3 sdf_size;
  166. bool gi_upscale_for_msaa;
  167. bool volumetric_fog_enabled;
  168. float volumetric_fog_inv_length;
  169. float volumetric_fog_detail_spread;
  170. uint volumetric_fog_pad;
  171. bool fog_enabled;
  172. float fog_density;
  173. float fog_height;
  174. float fog_height_density;
  175. vec3 fog_light_color;
  176. float fog_sun_scatter;
  177. float fog_aerial_perspective;
  178. float time;
  179. float reflection_multiplier; // one normally, zero when rendering reflections
  180. bool pancake_shadows;
  181. }
  182. scene_data;
  183. struct InstanceData {
  184. mat4 transform;
  185. uint flags;
  186. uint instance_uniforms_ofs; //base offset in global buffer for instance variables
  187. uint gi_offset; //GI information when using lightmapping (VCT or lightmap index)
  188. uint layer_mask;
  189. vec4 lightmap_uv_scale;
  190. };
  191. layout(set = 1, binding = 1, std430) buffer restrict readonly InstanceDataBuffer {
  192. InstanceData data[];
  193. }
  194. instances;
  195. #ifdef USE_RADIANCE_CUBEMAP_ARRAY
  196. layout(set = 1, binding = 2) uniform textureCubeArray radiance_cubemap;
  197. #else
  198. layout(set = 1, binding = 2) uniform textureCube radiance_cubemap;
  199. #endif
  200. layout(set = 1, binding = 3) uniform textureCubeArray reflection_atlas;
  201. layout(set = 1, binding = 4) uniform texture2D shadow_atlas;
  202. layout(set = 1, binding = 5) uniform texture2D directional_shadow_atlas;
  203. layout(set = 1, binding = 6) uniform texture2DArray lightmap_textures[MAX_LIGHTMAP_TEXTURES];
  204. layout(set = 1, binding = 7) uniform texture3D gi_probe_textures[MAX_GI_PROBES];
  205. layout(set = 1, binding = 8, std430) buffer restrict readonly ClusterBuffer {
  206. uint data[];
  207. }
  208. cluster_buffer;
  209. #ifdef MODE_RENDER_SDF
  210. layout(r16ui, set = 1, binding = 9) uniform restrict writeonly uimage3D albedo_volume_grid;
  211. layout(r32ui, set = 1, binding = 10) uniform restrict writeonly uimage3D emission_grid;
  212. layout(r32ui, set = 1, binding = 11) uniform restrict writeonly uimage3D emission_aniso_grid;
  213. layout(r32ui, set = 1, binding = 12) uniform restrict uimage3D geom_facing_grid;
  214. //still need to be present for shaders that use it, so remap them to something
  215. #define depth_buffer shadow_atlas
  216. #define color_buffer shadow_atlas
  217. #define normal_roughness_buffer shadow_atlas
  218. #else
  219. layout(set = 1, binding = 9) uniform texture2D depth_buffer;
  220. layout(set = 1, binding = 10) uniform texture2D color_buffer;
  221. layout(set = 1, binding = 11) uniform texture2D normal_roughness_buffer;
  222. layout(set = 1, binding = 12) uniform texture2D ao_buffer;
  223. layout(set = 1, binding = 13) uniform texture2D ambient_buffer;
  224. layout(set = 1, binding = 14) uniform texture2D reflection_buffer;
  225. layout(set = 1, binding = 15) uniform texture2DArray sdfgi_lightprobe_texture;
  226. layout(set = 1, binding = 16) uniform texture3D sdfgi_occlusion_cascades;
  227. struct GIProbeData {
  228. mat4 xform;
  229. vec3 bounds;
  230. float dynamic_range;
  231. float bias;
  232. float normal_bias;
  233. bool blend_ambient;
  234. uint texture_slot;
  235. float anisotropy_strength;
  236. float ambient_occlusion;
  237. float ambient_occlusion_size;
  238. uint mipmaps;
  239. };
  240. layout(set = 1, binding = 17, std140) uniform GIProbes {
  241. GIProbeData data[MAX_GI_PROBES];
  242. }
  243. gi_probes;
  244. layout(set = 1, binding = 18) uniform texture3D volumetric_fog_texture;
  245. #endif
  246. /* Set 2 Skeleton & Instancing (can change per item) */
  247. layout(set = 2, binding = 0, std430) restrict readonly buffer Transforms {
  248. vec4 data[];
  249. }
  250. transforms;
  251. /* Set 3 User Material */