Skip to content

Commit cb164a3

Browse files
committed
Merge pull request #107384 from Kaleb-Reid/compat-directional-cull-mask
Implement DirectionalLight3D cull masks in Compatibility
2 parents 9c63619 + 79b1a6d commit cb164a3

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
@@ -1715,6 +1715,8 @@ void RasterizerSceneGLES3::_setup_lights(const RenderDataGLES3 *p_render_data, b
17151715

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

1718+
light_data.mask = light_storage->light_get_cull_mask(base);
1719+
17181720
light_data.shadow_opacity = (p_using_shadows && light_storage->light_has_shadow(base))
17191721
? light_storage->light_get_param(base, RS::LIGHT_PARAM_SHADOW_OPACITY)
17201722
: 0.0;
@@ -3421,6 +3423,10 @@ void RasterizerSceneGLES3::_render_list_template(RenderListParameters *p_params,
34213423
// Render directional lights.
34223424

34233425
uint32_t shadow_id = MAX_DIRECTIONAL_LIGHTS - 1 - (pass - int32_t(inst->light_passes.size()));
3426+
if (!(scene_state.directional_lights[shadow_id].mask & inst->layer_mask)) {
3427+
// Disable additive lighting when masks are not overlapping.
3428+
spec_constants &= ~SceneShaderGLES3::USE_ADDITIVE_LIGHTING;
3429+
}
34243430
if (pass == 0 && inst->lightmap_instance.is_valid() && scene_state.directional_lights[shadow_id].bake_mode == RenderingServer::LIGHT_BAKE_STATIC) {
34253431
// Disable additive lighting with a static light and a lightmap.
34263432
spec_constants &= ~SceneShaderGLES3::USE_ADDITIVE_LIGHTING;
@@ -3667,6 +3673,8 @@ void RasterizerSceneGLES3::_render_list_template(RenderListParameters *p_params,
36673673

36683674
if (p_pass_mode == PASS_MODE_MATERIAL) {
36693675
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::UV_OFFSET, p_params->uv_offset, shader->version, instance_variant, spec_constants);
3676+
} else if (p_pass_mode == PASS_MODE_COLOR || p_pass_mode == PASS_MODE_COLOR_TRANSPARENT) {
3677+
material_storage->shaders.scene_shader.version_set_uniform(SceneShaderGLES3::LAYER_MASK, inst->layer_mask, shader->version, instance_variant, spec_constants);
36703678
}
36713679

36723680
// 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
@@ -291,19 +291,19 @@ struct DirectionalLightData {
291291
mediump float energy;
292292
mediump vec3 color;
293293
mediump float size;
294-
lowp uint unused;
295-
lowp uint bake_mode;
294+
lowp uint enabled_bake_mode;
296295
mediump float shadow_opacity;
297296
mediump float specular;
297+
highp uint mask;
298298
};
299299

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

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

309309
// Omni and spot light data.
@@ -466,6 +466,7 @@ uniform highp vec3 compressed_aabb_position;
466466
uniform highp vec3 compressed_aabb_size;
467467
uniform highp vec4 uv_scale;
468468
uniform highp uint instance_offset;
469+
uniform highp uint layer_mask;
469470

470471
#if defined(RENDER_MOTION_VECTORS)
471472
uniform highp mat4 prev_world_transform;
@@ -815,8 +816,11 @@ void vertex_shader(vec4 vertex_angle_attrib_input,
815816
#ifdef BASE_PASS
816817
#ifndef DISABLE_LIGHT_DIRECTIONAL
817818
for (uint i = uint(0); i < scene_data_input.directional_light_count; i++) {
819+
if (!bool(directional_lights[i].mask & layer_mask)) {
820+
continue;
821+
}
818822
#if defined(USE_LIGHTMAP) && !defined(DISABLE_LIGHTMAP)
819-
if (directional_lights[i].bake_mode == LIGHT_BAKE_STATIC) {
823+
if (bool(directional_lights[i].enabled_bake_mode & DIRECTIONAL_LIGHT_BAKE_STATIC)) {
820824
continue;
821825
}
822826
#endif
@@ -847,9 +851,11 @@ void vertex_shader(vec4 vertex_angle_attrib_input,
847851
additive_specular_light_interp = vec3(0.0);
848852
#if !defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT)
849853

850-
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,
851-
additive_diffuse_light_interp.rgb,
852-
additive_specular_light_interp.rgb);
854+
if (bool(directional_lights[directional_shadow_index].mask & layer_mask)) {
855+
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,
856+
additive_diffuse_light_interp.rgb,
857+
additive_specular_light_interp.rgb);
858+
}
853859
#endif // !defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT)
854860

855861
#ifdef ADDITIVE_OMNI
@@ -1188,11 +1194,12 @@ multiview_data_block;
11881194

11891195
uniform highp mat4 world_transform;
11901196
uniform highp uint instance_offset;
1197+
uniform highp uint layer_mask;
11911198
uniform highp uint model_flags;
11921199

1193-
#define LIGHT_BAKE_DISABLED 0u
1194-
#define LIGHT_BAKE_STATIC 1u
1195-
#define LIGHT_BAKE_DYNAMIC 2u
1200+
#define DIRECTIONAL_LIGHT_ENABLED uint(1 << 0)
1201+
#define DIRECTIONAL_LIGHT_BAKE_STATIC uint(1 << 1)
1202+
#define DIRECTIONAL_LIGHT_BAKE_DYNAMIC uint(1 << 2)
11961203

11971204
#ifndef MODE_RENDER_DEPTH
11981205
#ifdef USE_VERTEX_LIGHTING
@@ -1213,10 +1220,10 @@ struct DirectionalLightData {
12131220
mediump float energy;
12141221
mediump vec3 color;
12151222
mediump float size;
1216-
lowp uint unused;
1217-
lowp uint bake_mode;
1223+
lowp uint enabled_bake_mode;
12181224
mediump float shadow_opacity;
12191225
mediump float specular;
1226+
highp uint mask;
12201227
};
12211228

12221229
layout(std140) uniform DirectionalLights { // ubo:7
@@ -2373,8 +2380,11 @@ void main() {
23732380

23742381
#ifndef DISABLE_LIGHT_DIRECTIONAL
23752382
for (uint i = uint(0); i < scene_data_block.data.directional_light_count; i++) {
2383+
if (!bool(directional_lights[i].mask & layer_mask)) {
2384+
continue;
2385+
}
23762386
#if defined(USE_LIGHTMAP) && !defined(DISABLE_LIGHTMAP)
2377-
if (directional_lights[i].bake_mode == LIGHT_BAKE_STATIC) {
2387+
if (bool(directional_lights[i].enabled_bake_mode & DIRECTIONAL_LIGHT_BAKE_STATIC)) {
23782388
continue;
23792389
}
23802390
#endif
@@ -2692,26 +2702,30 @@ void main() {
26922702
#endif // SHADOWS_DISABLED
26932703

26942704
#ifndef USE_VERTEX_LIGHTING
2695-
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,
2705+
if (bool(directional_lights[directional_shadow_index].mask & layer_mask)) {
2706+
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,
26962707
#ifdef LIGHT_BACKLIGHT_USED
2697-
backlight,
2708+
backlight,
26982709
#endif
26992710
#ifdef LIGHT_RIM_USED
2700-
rim, rim_tint,
2711+
rim, rim_tint,
27012712
#endif
27022713
#ifdef LIGHT_CLEARCOAT_USED
2703-
clearcoat, clearcoat_roughness, geo_normal,
2714+
clearcoat, clearcoat_roughness, geo_normal,
27042715
#endif // LIGHT_CLEARCOAT_USED
27052716
#ifdef LIGHT_ANISOTROPY_USED
2706-
binormal,
2707-
tangent, anisotropy,
2717+
binormal,
2718+
tangent, anisotropy,
27082719
#endif
2709-
diffuse_light,
2710-
specular_light);
2711-
#else
2712-
// Just apply shadows to vertex lighting.
2713-
diffuse_light *= directional_shadow;
2714-
specular_light *= directional_shadow;
2720+
diffuse_light,
2721+
specular_light);
2722+
} else {
2723+
#endif // !USE_VERTEX_LIGHTING
2724+
// Just apply shadows to vertex lighting.
2725+
diffuse_light *= directional_shadow;
2726+
specular_light *= directional_shadow;
2727+
#ifndef USE_VERTEX_LIGHTING
2728+
}
27152729
#endif // !USE_VERTEX_LIGHTING
27162730
#endif // !defined(ADDITIVE_OMNI) && !defined(ADDITIVE_SPOT)
27172731

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
@@ -1513,22 +1513,22 @@ MaterialStorage::MaterialStorage() {
15131513
actions.renames["QUARTER_RES_COLOR"] = "quarter_res_color";
15141514
actions.renames["RADIANCE"] = "radiance";
15151515
actions.renames["FOG"] = "custom_fog";
1516-
actions.renames["LIGHT0_ENABLED"] = "directional_lights.data[0].enabled";
1516+
actions.renames["LIGHT0_ENABLED"] = "bool(directional_lights.data[0].enabled_bake_mode & DIRECTIONAL_LIGHT_ENABLED)";
15171517
actions.renames["LIGHT0_DIRECTION"] = "directional_lights.data[0].direction_energy.xyz";
15181518
actions.renames["LIGHT0_ENERGY"] = "directional_lights.data[0].direction_energy.w";
15191519
actions.renames["LIGHT0_COLOR"] = "directional_lights.data[0].color_size.xyz";
15201520
actions.renames["LIGHT0_SIZE"] = "directional_lights.data[0].color_size.w";
1521-
actions.renames["LIGHT1_ENABLED"] = "directional_lights.data[1].enabled";
1521+
actions.renames["LIGHT1_ENABLED"] = "bool(directional_lights.data[1].enabled_bake_mode & DIRECTIONAL_LIGHT_ENABLED)";
15221522
actions.renames["LIGHT1_DIRECTION"] = "directional_lights.data[1].direction_energy.xyz";
15231523
actions.renames["LIGHT1_ENERGY"] = "directional_lights.data[1].direction_energy.w";
15241524
actions.renames["LIGHT1_COLOR"] = "directional_lights.data[1].color_size.xyz";
15251525
actions.renames["LIGHT1_SIZE"] = "directional_lights.data[1].color_size.w";
1526-
actions.renames["LIGHT2_ENABLED"] = "directional_lights.data[2].enabled";
1526+
actions.renames["LIGHT2_ENABLED"] = "bool(directional_lights.data[2].enabled_bake_mode & DIRECTIONAL_LIGHT_ENABLED)";
15271527
actions.renames["LIGHT2_DIRECTION"] = "directional_lights.data[2].direction_energy.xyz";
15281528
actions.renames["LIGHT2_ENERGY"] = "directional_lights.data[2].direction_energy.w";
15291529
actions.renames["LIGHT2_COLOR"] = "directional_lights.data[2].color_size.xyz";
15301530
actions.renames["LIGHT2_SIZE"] = "directional_lights.data[2].color_size.w";
1531-
actions.renames["LIGHT3_ENABLED"] = "directional_lights.data[3].enabled";
1531+
actions.renames["LIGHT3_ENABLED"] = "bool(directional_lights.data[3].enabled_bake_mode & DIRECTIONAL_LIGHT_ENABLED)";
15321532
actions.renames["LIGHT3_DIRECTION"] = "directional_lights.data[3].direction_energy.xyz";
15331533
actions.renames["LIGHT3_ENERGY"] = "directional_lights.data[3].direction_energy.w";
15341534
actions.renames["LIGHT3_COLOR"] = "directional_lights.data[3].color_size.xyz";

0 commit comments

Comments
 (0)