canvas_uniforms_inc.glsl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #define MAX_LIGHTS_PER_ITEM 16
  2. #define M_PI 3.14159265359
  3. #define SDF_MAX_LENGTH 16384.0
  4. #define INSTANCE_FLAGS_LIGHT_COUNT_SHIFT 0 // 4 bits.
  5. #define INSTANCE_FLAGS_CLIP_RECT_UV (1 << 4)
  6. #define INSTANCE_FLAGS_TRANSPOSE_RECT (1 << 5)
  7. #define INSTANCE_FLAGS_USE_MSDF (1 << 6)
  8. #define INSTANCE_FLAGS_USE_LCD (1 << 7)
  9. #define INSTANCE_FLAGS_NINEPATCH_DRAW_CENTER_SHIFT 8
  10. #define INSTANCE_FLAGS_NINEPATCH_H_MODE_SHIFT 9
  11. #define INSTANCE_FLAGS_NINEPATCH_V_MODE_SHIFT 11
  12. #define INSTANCE_FLAGS_SHADOW_MASKED_SHIFT 13 // 16 bits.
  13. #define INSTANCE_FLAGS_SHADOW_MASKED (1 << INSTANCE_FLAGS_SHADOW_MASKED_SHIFT)
  14. struct InstanceData {
  15. vec2 world_x;
  16. vec2 world_y;
  17. vec2 world_ofs;
  18. uint flags;
  19. uint pad2;
  20. #ifdef USE_PRIMITIVE
  21. vec2 points[3];
  22. vec2 uvs[3];
  23. uint colors[6];
  24. #else
  25. vec4 modulation;
  26. vec4 ninepatch_margins;
  27. vec4 dst_rect; //for built-in rect and UV
  28. vec4 src_rect;
  29. vec2 pad;
  30. #endif
  31. vec2 color_texture_pixel_size;
  32. uvec4 lights;
  33. };
  34. //1 means enabled, 2+ means trails in use
  35. #define BATCH_FLAGS_INSTANCING_MASK 0x7F
  36. #define BATCH_FLAGS_INSTANCING_HAS_COLORS_SHIFT 7
  37. #define BATCH_FLAGS_INSTANCING_HAS_COLORS (1 << BATCH_FLAGS_INSTANCING_HAS_COLORS_SHIFT)
  38. #define BATCH_FLAGS_INSTANCING_HAS_CUSTOM_DATA_SHIFT 8
  39. #define BATCH_FLAGS_INSTANCING_HAS_CUSTOM_DATA (1 << BATCH_FLAGS_INSTANCING_HAS_CUSTOM_DATA_SHIFT)
  40. #define BATCH_FLAGS_DEFAULT_NORMAL_MAP_USED (1 << 9)
  41. #define BATCH_FLAGS_DEFAULT_SPECULAR_MAP_USED (1 << 10)
  42. layout(push_constant, std430) uniform Params {
  43. uint base_instance_index; // base index to instance data
  44. uint sc_packed_0;
  45. uint specular_shininess;
  46. uint batch_flags;
  47. }
  48. params;
  49. // Specialization constants.
  50. #ifdef UBERSHADER
  51. // Pull the constants from the draw call's push constants.
  52. uint sc_packed_0() {
  53. return params.sc_packed_0;
  54. }
  55. #else
  56. // Pull the constants from the pipeline's specialization constants.
  57. layout(constant_id = 0) const uint pso_sc_packed_0 = 0;
  58. uint sc_packed_0() {
  59. return pso_sc_packed_0;
  60. }
  61. #endif
  62. bool sc_use_lighting() {
  63. return ((sc_packed_0() >> 0) & 1U) != 0;
  64. }
  65. // In vulkan, sets should always be ordered using the following logic:
  66. // Lower Sets: Sets that change format and layout less often
  67. // Higher sets: Sets that change format and layout very often
  68. // This is because changing a set for another with a different layout or format,
  69. // invalidates all the upper ones (as likely internal base offset changes)
  70. /* SET0: Globals */
  71. #define CANVAS_FLAGS_CONVERT_ATTRIBUTES_TO_LINEAR (1 << 0)
  72. // The values passed per draw primitives are cached within it
  73. layout(set = 0, binding = 1, std140) uniform CanvasData {
  74. mat4 canvas_transform;
  75. mat4 screen_transform;
  76. mat4 canvas_normal_transform;
  77. vec4 canvas_modulation;
  78. vec2 screen_pixel_size;
  79. float time;
  80. bool use_pixel_snap;
  81. vec4 sdf_to_tex;
  82. vec2 screen_to_sdf;
  83. vec2 sdf_to_screen;
  84. uint directional_light_count;
  85. float tex_to_sdf;
  86. uint flags;
  87. uint pad2;
  88. }
  89. canvas_data;
  90. #define LIGHT_FLAGS_BLEND_MASK (3 << 16)
  91. #define LIGHT_FLAGS_BLEND_MODE_ADD (0 << 16)
  92. #define LIGHT_FLAGS_BLEND_MODE_SUB (1 << 16)
  93. #define LIGHT_FLAGS_BLEND_MODE_MIX (2 << 16)
  94. #define LIGHT_FLAGS_BLEND_MODE_MASK (3 << 16)
  95. #define LIGHT_FLAGS_HAS_SHADOW (1 << 20)
  96. #define LIGHT_FLAGS_FILTER_SHIFT 22
  97. #define LIGHT_FLAGS_FILTER_MASK (3 << 22)
  98. #define LIGHT_FLAGS_SHADOW_NEAREST (0 << 22)
  99. #define LIGHT_FLAGS_SHADOW_PCF5 (1 << 22)
  100. #define LIGHT_FLAGS_SHADOW_PCF13 (2 << 22)
  101. struct Light {
  102. mat2x4 texture_matrix; //light to texture coordinate matrix (transposed)
  103. mat2x4 shadow_matrix; //light to shadow coordinate matrix (transposed)
  104. vec4 color;
  105. uint shadow_color; // packed
  106. uint flags; //index to light texture
  107. float shadow_pixel_size;
  108. float height;
  109. vec2 position;
  110. float shadow_zfar_inv;
  111. float shadow_y_ofs;
  112. vec4 atlas_rect;
  113. };
  114. layout(set = 0, binding = 2, std140) uniform LightData {
  115. Light data[MAX_LIGHTS];
  116. }
  117. light_array;
  118. layout(set = 0, binding = 3) uniform texture2D atlas_texture;
  119. layout(set = 0, binding = 4) uniform texture2D shadow_atlas_texture;
  120. layout(set = 0, binding = 5) uniform sampler shadow_sampler;
  121. layout(set = 0, binding = 6) uniform texture2D color_buffer;
  122. layout(set = 0, binding = 7) uniform texture2D sdf_texture;
  123. #include "samplers_inc.glsl"
  124. layout(set = 0, binding = 9, std430) restrict readonly buffer GlobalShaderUniformData {
  125. vec4 data[];
  126. }
  127. global_shader_uniforms;
  128. /* SET1: Is reserved for the material */
  129. //
  130. /* SET2: Instancing and Skeleton */
  131. layout(set = 2, binding = 0, std430) restrict readonly buffer Transforms {
  132. vec4 data[];
  133. }
  134. transforms;
  135. /* SET3: Texture */
  136. layout(set = 3, binding = 0) uniform texture2D color_texture;
  137. layout(set = 3, binding = 1) uniform texture2D normal_texture;
  138. layout(set = 3, binding = 2) uniform texture2D specular_texture;
  139. layout(set = 3, binding = 3) uniform sampler texture_sampler;
  140. layout(set = 3, binding = 4, std430) restrict readonly buffer DrawData {
  141. InstanceData data[];
  142. }
  143. instances;