scene_forward_clustered_inc.glsl 8.5 KB

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