Blender V5.3
workbench_world_light.bsl.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2018-2022 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
8
10
11namespace workbench {
12
13/* [Drobot2014a] Low Level Optimizations for GCN */
15{
16 return intBitsToFloat(0x7eef370b - floatBitsToInt(v));
17}
18
19float3 brdf_approx(float3 spec_color, float roughness, float NV)
20{
21 /* Very rough approximation. We don't need it to be correct, just fast.
22 * Just simulate fresnel effect with roughness attenuation. */
23 float fresnel = exp2(-8.35f * NV) * (1.0f - roughness);
24 return mix(spec_color, float3(1.0f), fresnel);
25}
26
27/* Normalized Blinn shading */
28float4 blinn_specular(float4 shininess, float4 spec_angle, float4 NL)
29{
30 /* Pi is already divided in the light power.
31 * normalization_factor = (shininess + 8.0f) / (8.0f * M_PI) */
32 float4 normalization_factor = shininess * 0.125f + 1.0f;
33 float4 spec_light = pow(spec_angle, shininess) * NL * normalization_factor;
34
35 return spec_light;
36}
37
38/* NL need to be unclamped. w in [0..1] range. */
40{
41 float4 w_1 = w + 1.0f;
42 float4 denom = fast_rcp(w_1 * w_1);
43 return clamp((NL + w) * denom, 0.0f, 1.0f);
44}
45
47 float3 base_color,
48 float roughness,
49 float metallic,
50 float3 N,
51 float3 I)
52{
53 const WorldData &world_data = world.world_data;
54
55 float3 specular_color, diffuse_color;
56
57 if (world_data.use_specular) {
58 diffuse_color = mix(base_color, float3(0.0f), metallic);
59 specular_color = mix(float3(0.05f), base_color, metallic);
60 }
61 else {
62 diffuse_color = base_color;
63 specular_color = float3(0.0f);
64 }
65
66 float3 specular_light = world_data.ambient_color.rgb;
67 float3 diffuse_light = world_data.ambient_color.rgb;
68 float4 wrap = float4(world_data.lights[0].diffuse_color_wrap.a,
69 world_data.lights[1].diffuse_color_wrap.a,
70 world_data.lights[2].diffuse_color_wrap.a,
71 world_data.lights[3].diffuse_color_wrap.a);
72
73 if (world_data.use_specular) {
74 /* Prepare Specular computation. Eval 4 lights at once. */
75 float3 R = -reflect(I, N);
76
77 float4 spec_angle, spec_NL, wrapped_NL;
78 for (int i = 0; i < 4; i++) [[unroll]] {
79 float3 L = world_data.lights[i].direction.xyz;
80 float3 half_dir = normalize(L + I);
81 wrapped_NL[i] = dot(L, R);
82 spec_angle[i] = saturate(dot(half_dir, N));
83 spec_NL[i] = saturate(dot(L, N));
84 }
85
86 float4 gloss = float4(1.0f - roughness);
87 /* Reduce gloss for smooth light. (simulate bigger light) */
88 gloss *= 1.0f - wrap;
89 float4 shininess = exp2(10.0f * gloss + 1.0f);
90
91 float4 spec_light = blinn_specular(shininess, spec_angle, spec_NL);
92
93 /* Simulate Env. light. */
94 float4 w = mix(wrap, float4(1.0f), roughness);
95 float4 spec_env = wrapped_lighting(wrapped_NL, w);
96
97 spec_light = mix(spec_light, spec_env, wrap * wrap);
98
99 /* Multiply result by lights specular colors. */
100 for (int i = 0; i < 4; i++) [[unroll]] {
101 specular_light += spec_light[i] * world_data.lights[i].specular_color.rgb;
102 }
103
104 float NV = saturate(dot(N, I));
105 specular_color = brdf_approx(specular_color, roughness, NV);
106 }
107 specular_light *= specular_color;
108
109 /* Prepare diffuse computation. Eval 4 lights at once. */
110 float4 diff_NL;
111 for (int i = 0; i < 4; i++) [[unroll]] {
112 diff_NL[i] = dot(world_data.lights[i].direction.xyz, N);
113 }
114
115 float4 diff_light = wrapped_lighting(diff_NL, wrap);
116
117 /* Multiply result by lights diffuse colors. */
118 for (int i = 0; i < 4; i++) [[unroll]] {
119 diffuse_light += diff_light[i] * world_data.lights[i].diffuse_color_wrap.rgb;
120 }
121
122 /* Energy conservation with colored specular look strange.
123 * Limit this strangeness by using mono-chromatic specular intensity. */
124 float spec_energy = dot(specular_color, float3(0.33333f));
125
126 diffuse_light *= diffuse_color * (1.0f - spec_energy);
127
128 return diffuse_light + specular_light;
129}
130
131float get_shadow([[resource_table]] const workbench::World &world, float3 N, bool force_shadow)
132{
133 const WorldData &world_data = world.world_data;
134
135 float light_factor = -dot(N, world_data.shadow_direction_vs.xyz);
136 float shadow_mix = smoothstep(world_data.shadow_shift, world_data.shadow_focus, light_factor);
137 shadow_mix *= force_shadow ? 0.0f : world_data.shadow_mul;
138 return shadow_mix + world_data.shadow_add;
139}
140
141} // namespace workbench
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define NL
#define resource_table
#define unroll
T smoothstep(T, T, T) RET
VecBase< int, D > floatBitsToInt(VecOp< float, D >) RET
#define exp2
VecBase< float, D > intBitsToFloat(VecOp< int, D >) RET
#define pow
VecBase< T, D > reflect(VecOp< T, D >, VecOp< T, D >) RET
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
constexpr T clamp(T, U, U) RET
float wrap(float a, float b, float c)
#define N
#define R
#define L
float4 blinn_specular(float4 shininess, float4 spec_angle, float4 NL)
float get_shadow(const workbench::World &world, float3 N, bool force_shadow)
float4 wrapped_lighting(float4 NL, float4 w)
float4 fast_rcp(float4 v)
float3 brdf_approx(float3 spec_color, float roughness, float NV)
float3 get_world_lighting(const workbench::World &world, float3 base_color, float roughness, float metallic, float3 N, float3 I)
#define I
#define mix
#define saturate(a)
Definition smaa.cc:319
struct SolidLightData lights[4]