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