Skip to content

Commit 79b1a6d

Browse files
committed
Implement DirectionalLight3D cull masks in Compatibility
1 parent 06827c9 commit 79b1a6d

File tree

5 files changed

+62
-34
lines changed

5 files changed

+62
-34
lines changed

drivers/gles3/rasterizer_scene_gles3.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,6 +1712,8 @@ void RasterizerSceneGLES3::_setup_lights(const RenderDataGLES3 *p_render_data, b
17121712

17131713
light_data.specular = light_storage->light_get_param(base, RS::LIGHT_PARAM_SPECULAR);
17141714

1715+
light_data.mask = light_storage->light_get_cull_mask(base);
1716+
17151717
light_data.shadow_opacity = (p_using_shadows && light_storage->light_has_shadow(base))
17161718
? light_storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_OPACITY)
17171719
: 0.0;
@@ -3428,6 +3430,10 @@ void RasterizerSceneGLES3::_render_list_template(RenderListParameters *p_params,
34283430
// Render directional lights.
34293431

34303432
uint32_t shadow_id = MAX_DIRECTIONAL_LIGHTS - 1 - (pass - int32_t(inst->light_passes.size()));
3433+
if (!(scene_state.directional_lights[shadow_id].mask & inst->layer_mask)) {
3434+
// Disable additive lighting when masks are not overlapping.
3435+
spec_constants &= ~SceneShaderGLES3::USE_ADDITIVE_LIGHTING;
3436+
}
34313437
if (pass == 0 && inst->lightmap_instance.is_valid() && scene_state.directional_lights[shadow_id].bake_mode == RenderingServer::LIGHT_BAKE_STATIC) {
34323438
// Disable additive lighting with a static light and a lightmap.
34333439
spec_constants &= ~SceneShaderGLES3::USE_ADDITIVE_LIGHTING;
@@ -3674,6 +3680,8 @@ void RasterizerSceneGLES3::_render_list_template(RenderListParameters *p_params,
36743680

36753681
if (p_pass_mode == PASS_MODE_MATERIAL) {
36763682
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::UV_OFFSET, p_params->uv_offset, shader->version, instance_variant, spec_constants);
3683+
} else if (p_pass_mode == PASS_MODE_COLOR || p_pass_mode == PASS_MODE_COLOR_TRANSPARENT) {
3684+
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::LAYER_MASK, inst->layer_mask, shader->version, instance_variant, spec_constants);
36773685
}
36783686

36793687
// Can be index count or vertex count

drivers/gles3/rasterizer_scene_gles3.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,11 @@ class RasterizerSceneGLES3 : public RendererSceneRender {
203203
float color[3];
204204
float size;
205205

206-
uint32_t enabled; // For use by SkyShaders
207-
uint32_t bake_mode;
206+
uint32_t enabled : 1; // For use by SkyShaders
207+
uint32_t bake_mode : 2;
208208
float shadow_opacity;
209209
float specular;
210+
uint32_t mask;
210211
};
211212
static_assert(sizeof(DirectionalLightData) % 16 == 0, "DirectionalLightData size must be a multiple of 16 bytes");
212213

drivers/gles3/shaders/scene.glsl

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -290,19 +290,19 @@ struct DirectionalLightData {
290290
mediump float energy;
291291
mediump vec3 color;
292292
mediump float size;
293-
lowp uint unused;
294-
lowp uint bake_mode;
293+
lowp uint enabled_bake_mode;
295294
mediump float shadow_opacity;
296295
mediump float specular;
296+
highp uint mask;
297297
};
298298

299299
layout(std140) uniform DirectionalLights { // ubo:7
300300
DirectionalLightData directional_lights[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS];
301301
};
302302

303-
#define LIGHT_BAKE_DISABLED 0u
304-
#define LIGHT_BAKE_STATIC 1u
305-
#define LIGHT_BAKE_DYNAMIC 2u
303+
#define DIRECTIONAL_LIGHT_ENABLED uint(1 << 0)
304+
#define DIRECTIONAL_LIGHT_BAKE_STATIC uint(1 << 1)
305+
#define DIRECTIONAL_LIGHT_BAKE_DYNAMIC uint(1 << 2)
306306
#endif // !DISABLE_LIGHT_DIRECTIONAL
307307

308308
// Omni and spot light data.
@@ -465,6 +465,7 @@ uniform highp vec3 compressed_aabb_position;
465465
uniform highp vec3 compressed_aabb_size;
466466
uniform highp vec4 uv_scale;
467467
uniform highp uint instance_offset;
468+
uniform highp uint layer_mask;
468469

469470
#if defined(RENDER_MOTION_VECTORS)
470471
uniform highp mat4 prev_world_transform;
@@ -814,8 +815,11 @@ void vertex_shader(vec4 vertex_angle_attrib_input,
814815
#ifdef BASE_PASS
815816
#ifndef DISABLE_LIGHT_DIRECTIONAL
816817
for (uint i = uint(0); i < scene_data_input.directional_light_count; i++) {
818+
if (!bool(directional_lights[i].mask & layer_mask)) {
819+
continue;
820+
}
817821
#if defined(USE_LIGHTMAP) && !defined(DISABLE_LIGHTMAP)
818-
if (directional_lights[i].bake_mode == LIGHT_BAKE_STATIC) {
822+
if (bool(directional_lights[i].enabled_bake_mode & DIRECTIONAL_LIGHT_BAKE_STATIC)) {
819823
continue;
820824
}
821825
#endif
@@ -846,9 +850,11 @@ void vertex_shader(vec4 vertex_angle_attrib_input,
846850
additive_specular_light_interp = vec3(0.0);
847851
#if !defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT)
848852

849-
light_compute(normal_interp, normalize(directional_lights[directional_shadow_index].direction), normalize(view), directional_lights[directional_shadow_index].color * directional_lights[directional_shadow_index].energy, true, roughness,
850-
additive_diffuse_light_interp.rgb,
851-
additive_specular_light_interp.rgb);
853+
if (bool(directional_lights[directional_shadow_index].mask & layer_mask)) {
854+
light_compute(normal_interp, normalize(directional_lights[directional_shadow_index].direction), normalize(view), directional_lights[directional_shadow_index].color * directional_lights[directional_shadow_index].energy, true, roughness,
855+
additive_diffuse_light_interp.rgb,
856+
additive_specular_light_interp.rgb);
857+
}
852858
#endif // !defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT)
853859

854860
#ifdef ADDITIVE_OMNI
@@ -1187,11 +1193,12 @@ multiview_data_block;
11871193

11881194
uniform highp mat4 world_transform;
11891195
uniform highp uint instance_offset;
1196+
uniform highp uint layer_mask;
11901197
uniform highp uint model_flags;
11911198

1192-
#define LIGHT_BAKE_DISABLED 0u
1193-
#define LIGHT_BAKE_STATIC 1u
1194-
#define LIGHT_BAKE_DYNAMIC 2u
1199+
#define DIRECTIONAL_LIGHT_ENABLED uint(1 << 0)
1200+
#define DIRECTIONAL_LIGHT_BAKE_STATIC uint(1 << 1)
1201+
#define DIRECTIONAL_LIGHT_BAKE_DYNAMIC uint(1 << 2)
11951202

11961203
#ifndef MODE_RENDER_DEPTH
11971204
#ifdef USE_VERTEX_LIGHTING
@@ -1212,10 +1219,10 @@ struct DirectionalLightData {
12121219
mediump float energy;
12131220
mediump vec3 color;
12141221
mediump float size;
1215-
lowp uint unused;
1216-
lowp uint bake_mode;
1222+
lowp uint enabled_bake_mode;
12171223
mediump float shadow_opacity;
12181224
mediump float specular;
1225+
highp uint mask;
12191226
};
12201227

12211228
layout(std140) uniform DirectionalLights { // ubo:7
@@ -2369,8 +2376,11 @@ void main() {
23692376

23702377
#ifndef DISABLE_LIGHT_DIRECTIONAL
23712378
for (uint i = uint(0); i < scene_data_block.data.directional_light_count; i++) {
2379+
if (!bool(directional_lights[i].mask & layer_mask)) {
2380+
continue;
2381+
}
23722382
#if defined(USE_LIGHTMAP) && !defined(DISABLE_LIGHTMAP)
2373-
if (directional_lights[i].bake_mode == LIGHT_BAKE_STATIC) {
2383+
if (bool(directional_lights[i].enabled_bake_mode & DIRECTIONAL_LIGHT_BAKE_STATIC)) {
23742384
continue;
23752385
}
23762386
#endif
@@ -2688,26 +2698,30 @@ void main() {
26882698
#endif // SHADOWS_DISABLED
26892699

26902700
#ifndef USE_VERTEX_LIGHTING
2691-
light_compute(normal, normalize(directional_lights[directional_shadow_index].direction), normalize(view), directional_lights[directional_shadow_index].size, directional_lights[directional_shadow_index].color * directional_lights[directional_shadow_index].energy, true, directional_shadow, f0, roughness, metallic, directional_lights[directional_shadow_index].specular, albedo, alpha, screen_uv,
2701+
if (bool(directional_lights[directional_shadow_index].mask & layer_mask)) {
2702+
light_compute(normal, normalize(directional_lights[directional_shadow_index].direction), normalize(view), directional_lights[directional_shadow_index].size, directional_lights[directional_shadow_index].color * directional_lights[directional_shadow_index].energy, true, directional_shadow, f0, roughness, metallic, directional_lights[directional_shadow_index].specular, albedo, alpha, screen_uv,
26922703
#ifdef LIGHT_BACKLIGHT_USED
2693-
backlight,
2704+
backlight,
26942705
#endif
26952706
#ifdef LIGHT_RIM_USED
2696-
rim, rim_tint,
2707+
rim, rim_tint,
26972708
#endif
26982709
#ifdef LIGHT_CLEARCOAT_USED
2699-
clearcoat, clearcoat_roughness, geo_normal,
2710+
clearcoat, clearcoat_roughness, geo_normal,
27002711
#endif // LIGHT_CLEARCOAT_USED
27012712
#ifdef LIGHT_ANISOTROPY_USED
2702-
binormal,
2703-
tangent, anisotropy,
2713+
binormal,
2714+
tangent, anisotropy,
27042715
#endif
2705-
diffuse_light,
2706-
specular_light);
2707-
#else
2708-
// Just apply shadows to vertex lighting.
2709-
diffuse_light *= directional_shadow;
2710-
specular_light *= directional_shadow;
2716+
diffuse_light,
2717+
specular_light);
2718+
} else {
2719+
#endif // !USE_VERTEX_LIGHTING
2720+
// Just apply shadows to vertex lighting.
2721+
diffuse_light *= directional_shadow;
2722+
specular_light *= directional_shadow;
2723+
#ifndef USE_VERTEX_LIGHTING
2724+
}
27112725
#endif // !USE_VERTEX_LIGHTING
27122726
#endif // !defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT)
27132727

drivers/gles3/shaders/sky.glsl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,19 @@ layout(std140) uniform GlobalShaderUniformData { //ubo:1
5959
struct DirectionalLightData {
6060
vec4 direction_energy;
6161
vec4 color_size;
62-
bool enabled;
62+
uint enabled_bake_mode;
63+
float shadow_opacity;
64+
float specular;
65+
uint mask;
6366
};
6467

6568
layout(std140) uniform DirectionalLights { //ubo:4
6669
DirectionalLightData data[MAX_DIRECTIONAL_LIGHT_DATA_STRUCTS];
6770
}
6871
directional_lights;
6972

73+
#define DIRECTIONAL_LIGHT_ENABLED uint(1 << 0)
74+
7075
/* clang-format off */
7176

7277
#ifdef MATERIAL_UNIFORMS_USED

drivers/gles3/storage/material_storage.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,22 +1512,22 @@ MaterialStorage::MaterialStorage() {
15121512
actions.renames["QUARTER_RES_COLOR"] = "quarter_res_color";
15131513
actions.renames["RADIANCE"] = "radiance";
15141514
actions.renames["FOG"] = "custom_fog";
1515-
actions.renames["LIGHT0_ENABLED"] = "directional_lights.data[0].enabled";
1515+
actions.renames["LIGHT0_ENABLED"] = "bool(directional_lights.data[0].enabled_bake_mode & DIRECTIONAL_LIGHT_ENABLED)";
15161516
actions.renames["LIGHT0_DIRECTION"] = "directional_lights.data[0].direction_energy.xyz";
15171517
actions.renames["LIGHT0_ENERGY"] = "directional_lights.data[0].direction_energy.w";
15181518
actions.renames["LIGHT0_COLOR"] = "directional_lights.data[0].color_size.xyz";
15191519
actions.renames["LIGHT0_SIZE"] = "directional_lights.data[0].color_size.w";
1520-
actions.renames["LIGHT1_ENABLED"] = "directional_lights.data[1].enabled";
1520+
actions.renames["LIGHT1_ENABLED"] = "bool(directional_lights.data[1].enabled_bake_mode & DIRECTIONAL_LIGHT_ENABLED)";
15211521
actions.renames["LIGHT1_DIRECTION"] = "directional_lights.data[1].direction_energy.xyz";
15221522
actions.renames["LIGHT1_ENERGY"] = "directional_lights.data[1].direction_energy.w";
15231523
actions.renames["LIGHT1_COLOR"] = "directional_lights.data[1].color_size.xyz";
15241524
actions.renames["LIGHT1_SIZE"] = "directional_lights.data[1].color_size.w";
1525-
actions.renames["LIGHT2_ENABLED"] = "directional_lights.data[2].enabled";
1525+
actions.renames["LIGHT2_ENABLED"] = "bool(directional_lights.data[2].enabled_bake_mode & DIRECTIONAL_LIGHT_ENABLED)";
15261526
actions.renames["LIGHT2_DIRECTION"] = "directional_lights.data[2].direction_energy.xyz";
15271527
actions.renames["LIGHT2_ENERGY"] = "directional_lights.data[2].direction_energy.w";
15281528
actions.renames["LIGHT2_COLOR"] = "directional_lights.data[2].color_size.xyz";
15291529
actions.renames["LIGHT2_SIZE"] = "directional_lights.data[2].color_size.w";
1530-
actions.renames["LIGHT3_ENABLED"] = "directional_lights.data[3].enabled";
1530+
actions.renames["LIGHT3_ENABLED"] = "bool(directional_lights.data[3].enabled_bake_mode & DIRECTIONAL_LIGHT_ENABLED)";
15311531
actions.renames["LIGHT3_DIRECTION"] = "directional_lights.data[3].direction_energy.xyz";
15321532
actions.renames["LIGHT3_ENERGY"] = "directional_lights.data[3].direction_energy.w";
15331533
actions.renames["LIGHT3_COLOR"] = "directional_lights.data[3].color_size.xyz";

0 commit comments

Comments
 (0)