Skip to content

Commit 07d3e53

Browse files
committed
Migrate sky/cloud rendering to fragment shader and introduce high precision post process option
Signed-off-by: Rye <rye@alchemyviewer.org>
1 parent 0fa5991 commit 07d3e53

File tree

13 files changed

+303
-338
lines changed

13 files changed

+303
-338
lines changed

indra/newview/app_settings/settings.xml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10118,6 +10118,17 @@
1011810118
<key>Value</key>
1011910119
<integer>1</integer>
1012010120
</map>
10121+
<key>RenderHighPrecisionPostProcess</key>
10122+
<map>
10123+
<key>Comment</key>
10124+
<string>Use 16-bit floating point buffers for post process rendering</string>
10125+
<key>Persist</key>
10126+
<integer>1</integer>
10127+
<key>Type</key>
10128+
<string>Boolean</string>
10129+
<key>Value</key>
10130+
<integer>0</integer>
10131+
</map>
1012110132
<key>ReplaySession</key>
1012210133
<map>
1012310134
<key>Comment</key>
@@ -13851,7 +13862,7 @@
1385113862
<key>WLSkyDetail</key>
1385213863
<map>
1385313864
<key>Comment</key>
13854-
<string>Controls vertex detail on the WindLight sky. Lower numbers will give better performance and uglier skies.</string>
13865+
<string>[OBSOLETE]</string>
1385513866
<key>Persist</key>
1385613867
<integer>1</integer>
1385713868
<key>Type</key>

indra/newview/app_settings/shaders/class1/deferred/cloudsF.glsl

Lines changed: 123 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@ out vec4 frag_data[4];
3030
// The fragment shader for the sky
3131
/////////////////////////////////////////////////////////////////////////
3232

33-
in vec3 vary_CloudColorSun;
34-
in vec3 vary_CloudColorAmbient;
35-
in float vary_CloudDensity;
33+
// In
34+
in vec3 pos;
35+
36+
in vec2 vary_texcoord0;
37+
in vec2 vary_texcoord1;
38+
in vec2 vary_texcoord2;
39+
in vec2 vary_texcoord3;
3640

3741
uniform sampler2D cloud_noise_texture;
3842
uniform sampler2D cloud_noise_texture_next;
@@ -42,11 +46,26 @@ uniform vec3 cloud_pos_density2;
4246
uniform float cloud_scale;
4347
uniform float cloud_variance;
4448

45-
in vec2 vary_texcoord0;
46-
in vec2 vary_texcoord1;
47-
in vec2 vary_texcoord2;
48-
in vec2 vary_texcoord3;
49-
in float altitude_blend_factor;
49+
uniform vec3 camPosLocal;
50+
51+
uniform vec3 lightnorm;
52+
uniform vec3 sunlight_color;
53+
uniform vec3 moonlight_color;
54+
uniform int sun_up_factor;
55+
uniform vec3 ambient_color;
56+
uniform vec3 blue_horizon;
57+
uniform vec3 blue_density;
58+
uniform float haze_horizon;
59+
uniform float haze_density;
60+
61+
uniform float cloud_shadow;
62+
uniform float density_multiplier;
63+
uniform float max_y;
64+
65+
uniform vec3 glow;
66+
uniform float sun_moon_glow_factor;
67+
68+
uniform vec3 cloud_color;
5069

5170
vec4 cloudNoise(vec2 uv)
5271
{
@@ -58,21 +77,111 @@ vec4 cloudNoise(vec2 uv)
5877

5978
void main()
6079
{
80+
if (cloud_scale < 0.001)
81+
{
82+
discard;
83+
}
84+
6185
// Set variables
6286
vec2 uv1 = vary_texcoord0.xy;
6387
vec2 uv2 = vary_texcoord1.xy;
64-
65-
vec3 cloudColorSun = vary_CloudColorSun;
66-
vec3 cloudColorAmbient = vary_CloudColorAmbient;
67-
float cloudDensity = vary_CloudDensity;
6888
vec2 uv3 = vary_texcoord2.xy;
6989
vec2 uv4 = vary_texcoord3.xy;
7090

71-
if (cloud_scale < 0.001)
91+
// Get relative position
92+
vec3 rel_pos = pos.xyz - camPosLocal.xyz + vec3(0, 50, 0);
93+
94+
float altitude_blend_factor = clamp((rel_pos.y + 512.0) / max_y, 0.0, 1.0);
95+
96+
// Set altitude
97+
if (rel_pos.y > 0)
7298
{
73-
discard;
99+
rel_pos *= (max_y / rel_pos.y);
100+
}
101+
if (rel_pos.y < 0)
102+
{
103+
altitude_blend_factor = 0; // SL-11589 Fix clouds drooping below horizon
104+
rel_pos *= (-32000. / rel_pos.y);
74105
}
75106

107+
// Can normalize then
108+
vec3 rel_pos_norm = normalize(rel_pos);
109+
float rel_pos_len = length(rel_pos);
110+
111+
// Initialize temp variables
112+
vec3 sunlight = sunlight_color;
113+
vec3 light_atten;
114+
115+
// Sunlight attenuation effect (hue and brightness) due to atmosphere
116+
// this is used later for sunlight modulation at various altitudes
117+
light_atten = (blue_density + vec3(haze_density * 0.25)) * (density_multiplier * max_y);
118+
119+
// Calculate relative weights
120+
vec3 combined_haze = abs(blue_density) + vec3(abs(haze_density));
121+
vec3 blue_weight = blue_density / combined_haze;
122+
vec3 haze_weight = haze_density / combined_haze;
123+
124+
// Compute sunlight from rel_pos & lightnorm (for long rays like sky)
125+
float off_axis = 1.0 / max(1e-6, max(0., rel_pos_norm.y) + lightnorm.y);
126+
sunlight *= exp(-light_atten * off_axis);
127+
128+
// Distance
129+
float density_dist = rel_pos_len * density_multiplier;
130+
131+
// Transparency (-> combined_haze)
132+
// ATI Bugfix -- can't store combined_haze*density_dist in a variable because the ati
133+
// compiler gets confused.
134+
combined_haze = exp(-combined_haze * density_dist);
135+
136+
// Compute haze glow
137+
float haze_glow = 1.0 - dot(rel_pos_norm, lightnorm.xyz);
138+
// haze_glow is 0 at the sun and increases away from sun
139+
haze_glow = max(haze_glow, .001);
140+
// Set a minimum "angle" (smaller glow.y allows tighter, brighter hotspot)
141+
haze_glow *= glow.x;
142+
// Higher glow.x gives dimmer glow (because next step is 1 / "angle")
143+
haze_glow = pow(haze_glow, glow.z);
144+
// glow.z should be negative, so we're doing a sort of (1 / "angle") function
145+
146+
haze_glow *= sun_moon_glow_factor;
147+
148+
// Add "minimum anti-solar illumination"
149+
// For sun, add to glow. For moon, remove glow entirely. SL-13768
150+
haze_glow = (sun_moon_glow_factor < 1.0) ? 0.0 : (haze_glow + 0.25);
151+
152+
// Increase ambient when there are more clouds
153+
vec3 tmpAmbient = ambient_color;
154+
tmpAmbient += (1. - tmpAmbient) * cloud_shadow * 0.5;
155+
156+
// Dim sunlight by cloud shadow percentage
157+
sunlight *= (1. - cloud_shadow);
158+
159+
// Haze color below cloud
160+
vec3 additiveColorBelowCloud =
161+
(blue_horizon * blue_weight * (sunlight + tmpAmbient) + (haze_horizon * haze_weight) * (sunlight * haze_glow + tmpAmbient));
162+
163+
// CLOUDS
164+
sunlight = sunlight_color;
165+
off_axis = 1.0 / max(1e-6, lightnorm.y * 2.);
166+
sunlight *= exp(-light_atten * off_axis);
167+
168+
// Cloud color out
169+
vec3 cloudColorSun = (sunlight * haze_glow) * cloud_color;
170+
vec3 cloudColorAmbient = tmpAmbient * cloud_color;
171+
172+
// Attenuate cloud color by atmosphere
173+
combined_haze = sqrt(combined_haze); // less atmos opacity (more transparency) below clouds
174+
cloudColorSun *= combined_haze;
175+
cloudColorAmbient *= combined_haze;
176+
vec3 oHazeColorBelowCloud = additiveColorBelowCloud * (1. - combined_haze);
177+
178+
// Make a nice cloud density based on the cloud_shadow value that was passed in.
179+
float cloudDensity = 2. * (cloud_shadow - 0.25);
180+
181+
// Combine these to minimize register use
182+
cloudColorAmbient += oHazeColorBelowCloud;
183+
184+
// Cloud Fragment
76185
vec2 disturbance = vec2(cloudNoise(uv1 / 8.0f).x, cloudNoise((uv3 + uv1) / 16.0f).x) * cloud_variance * (1.0f - cloud_scale * 0.25f);
77186
vec2 disturbance2 = vec2(cloudNoise((uv1 + uv3) / 4.0f).x, cloudNoise((uv4 + uv2) / 8.0f).x) * cloud_variance * (1.0f - cloud_scale * 0.25f);
78187

Lines changed: 3 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @file WLCloudsV.glsl
2+
* @file class1/deferred/cloudsV.glsl
33
*
44
* $LicenseInfo:firstyear=2005&license=viewerlgpl$
55
* Second Life Viewer Source Code
@@ -33,38 +33,15 @@ in vec2 texcoord0;
3333
///////////////////////////////////////////////////////////////////////////////
3434

3535
// Output parameters
36-
out vec3 vary_CloudColorSun;
37-
out vec3 vary_CloudColorAmbient;
38-
out float vary_CloudDensity;
36+
out vec3 pos;
3937

4038
out vec2 vary_texcoord0;
4139
out vec2 vary_texcoord1;
4240
out vec2 vary_texcoord2;
4341
out vec2 vary_texcoord3;
44-
out float altitude_blend_factor;
4542

4643
// Inputs
47-
uniform vec3 camPosLocal;
48-
4944
uniform vec3 lightnorm;
50-
uniform vec3 sunlight_color;
51-
uniform vec3 moonlight_color;
52-
uniform int sun_up_factor;
53-
uniform vec3 ambient_color;
54-
uniform vec3 blue_horizon;
55-
uniform vec3 blue_density;
56-
uniform float haze_horizon;
57-
uniform float haze_density;
58-
59-
uniform float cloud_shadow;
60-
uniform float density_multiplier;
61-
uniform float max_y;
62-
63-
uniform vec3 glow;
64-
uniform float sun_moon_glow_factor;
65-
66-
uniform vec3 cloud_color;
67-
6845
uniform float cloud_scale;
6946

7047
// NOTE: Keep these in sync!
@@ -76,6 +53,7 @@ uniform float cloud_scale;
7653
void main()
7754
{
7855
// World / view / projection
56+
pos = position.xyz;
7957
gl_Position = modelview_projection_matrix * vec4(position.xyz, 1.0);
8058

8159
// Texture coords
@@ -92,102 +70,4 @@ void main()
9270

9371
vary_texcoord2 = vary_texcoord0 * 16.;
9472
vary_texcoord3 = vary_texcoord1 * 16.;
95-
96-
// Get relative position
97-
vec3 rel_pos = position.xyz - camPosLocal.xyz + vec3(0, 50, 0);
98-
99-
altitude_blend_factor = clamp((rel_pos.y + 512.0) / max_y, 0.0, 1.0);
100-
101-
// Set altitude
102-
if (rel_pos.y > 0)
103-
{
104-
rel_pos *= (max_y / rel_pos.y);
105-
}
106-
if (rel_pos.y < 0)
107-
{
108-
altitude_blend_factor = 0; // SL-11589 Fix clouds drooping below horizon
109-
rel_pos *= (-32000. / rel_pos.y);
110-
}
111-
112-
// Can normalize then
113-
vec3 rel_pos_norm = normalize(rel_pos);
114-
float rel_pos_len = length(rel_pos);
115-
116-
// Initialize temp variables
117-
vec3 sunlight = sunlight_color;
118-
vec3 light_atten;
119-
120-
// Sunlight attenuation effect (hue and brightness) due to atmosphere
121-
// this is used later for sunlight modulation at various altitudes
122-
light_atten = (blue_density + vec3(haze_density * 0.25)) * (density_multiplier * max_y);
123-
124-
// Calculate relative weights
125-
vec3 combined_haze = abs(blue_density) + vec3(abs(haze_density));
126-
vec3 blue_weight = blue_density / combined_haze;
127-
vec3 haze_weight = haze_density / combined_haze;
128-
129-
// Compute sunlight from rel_pos & lightnorm (for long rays like sky)
130-
float off_axis = 1.0 / max(1e-6, max(0., rel_pos_norm.y) + lightnorm.y);
131-
sunlight *= exp(-light_atten * off_axis);
132-
133-
// Distance
134-
float density_dist = rel_pos_len * density_multiplier;
135-
136-
// Transparency (-> combined_haze)
137-
// ATI Bugfix -- can't store combined_haze*density_dist in a variable because the ati
138-
// compiler gets confused.
139-
combined_haze = exp(-combined_haze * density_dist);
140-
141-
// Compute haze glow
142-
float haze_glow = 1.0 - dot(rel_pos_norm, lightnorm.xyz);
143-
// haze_glow is 0 at the sun and increases away from sun
144-
haze_glow = max(haze_glow, .001);
145-
// Set a minimum "angle" (smaller glow.y allows tighter, brighter hotspot)
146-
haze_glow *= glow.x;
147-
// Higher glow.x gives dimmer glow (because next step is 1 / "angle")
148-
haze_glow = pow(haze_glow, glow.z);
149-
// glow.z should be negative, so we're doing a sort of (1 / "angle") function
150-
151-
haze_glow *= sun_moon_glow_factor;
152-
153-
// Add "minimum anti-solar illumination"
154-
// For sun, add to glow. For moon, remove glow entirely. SL-13768
155-
haze_glow = (sun_moon_glow_factor < 1.0) ? 0.0 : (haze_glow + 0.25);
156-
157-
// Increase ambient when there are more clouds
158-
vec3 tmpAmbient = ambient_color;
159-
tmpAmbient += (1. - tmpAmbient) * cloud_shadow * 0.5;
160-
161-
// Dim sunlight by cloud shadow percentage
162-
sunlight *= (1. - cloud_shadow);
163-
164-
// Haze color below cloud
165-
vec3 additiveColorBelowCloud =
166-
(blue_horizon * blue_weight * (sunlight + tmpAmbient) + (haze_horizon * haze_weight) * (sunlight * haze_glow + tmpAmbient));
167-
168-
// CLOUDS
169-
sunlight = sunlight_color;
170-
off_axis = 1.0 / max(1e-6, lightnorm.y * 2.);
171-
sunlight *= exp(-light_atten * off_axis);
172-
173-
// Cloud color out
174-
vary_CloudColorSun = (sunlight * haze_glow) * cloud_color;
175-
vary_CloudColorAmbient = tmpAmbient * cloud_color;
176-
177-
// Attenuate cloud color by atmosphere
178-
combined_haze = sqrt(combined_haze); // less atmos opacity (more transparency) below clouds
179-
vary_CloudColorSun *= combined_haze;
180-
vary_CloudColorAmbient *= combined_haze;
181-
vec3 oHazeColorBelowCloud = additiveColorBelowCloud * (1. - combined_haze);
182-
183-
// Make a nice cloud density based on the cloud_shadow value that was passed in.
184-
vary_CloudDensity = 2. * (cloud_shadow - 0.25);
185-
186-
// Combine these to minimize register use
187-
vary_CloudColorAmbient += oHazeColorBelowCloud;
188-
189-
// needs this to compile on mac
190-
//vary_AtmosAttenuation = vec3(0.0,0.0,0.0);
191-
192-
// END CLOUDS
19373
}

0 commit comments

Comments
 (0)