Skip to content

Commit 5aa5e52

Browse files
authored
Fix chrome rendering bug (#21202)
# Objective - Fix chrome rendering bug: <img width="1755" height="985" alt="image" src="https://github.com/user-attachments/assets/6293a960-b8fa-460e-9d71-e7f2a9fb2d20" /> ## Solution - Revert part of #20313 - I don't know why this fixes it, but #20960 also ran into it ## Testing -
1 parent 177f4c0 commit 5aa5e52

File tree

1 file changed

+70
-55
lines changed

1 file changed

+70
-55
lines changed

crates/bevy_pbr/src/render/view_transformations.wgsl

Lines changed: 70 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#import bevy_pbr::mesh_view_bindings as view_bindings
44
#import bevy_pbr::prepass_bindings
5-
#import bevy_render::view
65

76
/// World space:
87
/// +y is up
@@ -36,188 +35,204 @@
3635
// -----------------
3736

3837
/// Convert a view space position to world space
39-
/// DEPRECATED: use bevy_render::view::position_view_to_world instead
4038
fn position_view_to_world(view_pos: vec3<f32>) -> vec3<f32> {
41-
return view::position_view_to_world(view_pos, view_bindings::view.world_from_view);
39+
let world_pos = view_bindings::view.world_from_view * vec4(view_pos, 1.0);
40+
return world_pos.xyz;
4241
}
4342

4443
/// Convert a clip space position to world space
45-
/// DEPRECATED: use bevy_render::view::position_clip_to_world instead
4644
fn position_clip_to_world(clip_pos: vec4<f32>) -> vec3<f32> {
47-
return view::position_clip_to_world(clip_pos, view_bindings::view.world_from_clip);
45+
let world_pos = view_bindings::view.world_from_clip * clip_pos;
46+
return world_pos.xyz;
4847
}
4948

5049
/// Convert a ndc space position to world space
51-
/// DEPRECATED: use bevy_render::view::position_ndc_to_world instead
5250
fn position_ndc_to_world(ndc_pos: vec3<f32>) -> vec3<f32> {
53-
return view::position_ndc_to_world(ndc_pos, view_bindings::view.world_from_clip);
51+
let world_pos = view_bindings::view.world_from_clip * vec4(ndc_pos, 1.0);
52+
return world_pos.xyz / world_pos.w;
5453
}
5554

5655
/// Convert a view space direction to world space
57-
/// DEPRECATED: use bevy_render::view::direction_view_to_world instead
5856
fn direction_view_to_world(view_dir: vec3<f32>) -> vec3<f32> {
59-
return view::direction_view_to_world(view_dir, view_bindings::view.world_from_view);
57+
let world_dir = view_bindings::view.world_from_view * vec4(view_dir, 0.0);
58+
return world_dir.xyz;
6059
}
6160

6261
/// Convert a clip space direction to world space
63-
/// DEPRECATED: use bevy_render::view::direction_clip_to_world instead
6462
fn direction_clip_to_world(clip_dir: vec4<f32>) -> vec3<f32> {
65-
return view::direction_clip_to_world(clip_dir, view_bindings::view.world_from_clip);
63+
let world_dir = view_bindings::view.world_from_clip * clip_dir;
64+
return world_dir.xyz;
6665
}
6766

6867
// -----------------
6968
// TO VIEW ---------
7069
// -----------------
7170

7271
/// Convert a world space position to view space
73-
/// DEPRECATED: use bevy_render::view::position_world_to_view instead
7472
fn position_world_to_view(world_pos: vec3<f32>) -> vec3<f32> {
75-
return view::position_world_to_view(world_pos, view_bindings::view.view_from_world);
73+
let view_pos = view_bindings::view.view_from_world * vec4(world_pos, 1.0);
74+
return view_pos.xyz;
7675
}
7776

7877
/// Convert a clip space position to view space
79-
/// DEPRECATED: use bevy_render::view::position_clip_to_view instead
8078
fn position_clip_to_view(clip_pos: vec4<f32>) -> vec3<f32> {
81-
return view::position_clip_to_view(clip_pos, view_bindings::view.view_from_clip);
79+
let view_pos = view_bindings::view.view_from_clip * clip_pos;
80+
return view_pos.xyz;
8281
}
8382

8483
/// Convert a ndc space position to view space
85-
/// DEPRECATED: use bevy_render::view::position_ndc_to_view instead
8684
fn position_ndc_to_view(ndc_pos: vec3<f32>) -> vec3<f32> {
87-
return view::position_ndc_to_view(ndc_pos, view_bindings::view.view_from_clip);
85+
let view_pos = view_bindings::view.view_from_clip * vec4(ndc_pos, 1.0);
86+
return view_pos.xyz / view_pos.w;
8887
}
8988

9089
/// Convert a world space direction to view space
91-
/// DEPRECATED: use bevy_render::view::direction_world_to_view instead
9290
fn direction_world_to_view(world_dir: vec3<f32>) -> vec3<f32> {
93-
return view::direction_world_to_view(world_dir, view_bindings::view.view_from_world);
91+
let view_dir = view_bindings::view.view_from_world * vec4(world_dir, 0.0);
92+
return view_dir.xyz;
9493
}
9594

9695
/// Convert a clip space direction to view space
97-
/// DEPRECATED: use bevy_render::view::direction_clip_to_view instead
9896
fn direction_clip_to_view(clip_dir: vec4<f32>) -> vec3<f32> {
99-
return view::direction_clip_to_view(clip_dir, view_bindings::view.view_from_clip);
97+
let view_dir = view_bindings::view.view_from_clip * clip_dir;
98+
return view_dir.xyz;
10099
}
101100

102101
// -----------------
103102
// TO PREV. VIEW ---
104103
// -----------------
105104

106-
/// DEPRECATED: use bevy_render::view::position_world_to_view instead
107105
fn position_world_to_prev_view(world_pos: vec3<f32>) -> vec3<f32> {
108-
return view::position_world_to_view(world_pos, prepass_bindings::previous_view_uniforms.view_from_world);
106+
let view_pos = prepass_bindings::previous_view_uniforms.view_from_world *
107+
vec4(world_pos, 1.0);
108+
return view_pos.xyz;
109109
}
110110

111-
/// DEPRECATED: use bevy_render::view::position_world_to_ndc instead
112111
fn position_world_to_prev_ndc(world_pos: vec3<f32>) -> vec3<f32> {
113-
return view::position_world_to_ndc(world_pos, prepass_bindings::previous_view_uniforms.clip_from_world);
112+
let ndc_pos = prepass_bindings::previous_view_uniforms.clip_from_world *
113+
vec4(world_pos, 1.0);
114+
return ndc_pos.xyz / ndc_pos.w;
114115
}
115116

116117
// -----------------
117118
// TO CLIP ---------
118119
// -----------------
119120

120121
/// Convert a world space position to clip space
121-
/// DEPRECATED: use bevy_render::view::position_world_to_clip instead
122122
fn position_world_to_clip(world_pos: vec3<f32>) -> vec4<f32> {
123-
return view::position_world_to_clip(world_pos, view_bindings::view.clip_from_world);
123+
let clip_pos = view_bindings::view.clip_from_world * vec4(world_pos, 1.0);
124+
return clip_pos;
124125
}
125126

126127
/// Convert a view space position to clip space
127-
/// DEPRECATED: use bevy_render::view::position_view_to_clip instead
128128
fn position_view_to_clip(view_pos: vec3<f32>) -> vec4<f32> {
129-
return view::position_view_to_clip(view_pos, view_bindings::view.clip_from_view);
129+
let clip_pos = view_bindings::view.clip_from_view * vec4(view_pos, 1.0);
130+
return clip_pos;
130131
}
131132

132133
/// Convert a world space direction to clip space
133-
/// DEPRECATED: use bevy_render::view::direction_world_to_clip instead
134134
fn direction_world_to_clip(world_dir: vec3<f32>) -> vec4<f32> {
135-
return view::direction_world_to_clip(world_dir, view_bindings::view.clip_from_world);
135+
let clip_dir = view_bindings::view.clip_from_world * vec4(world_dir, 0.0);
136+
return clip_dir;
136137
}
137138

138139
/// Convert a view space direction to clip space
139-
/// DEPRECATED: use bevy_render::view::direction_view_to_clip instead
140140
fn direction_view_to_clip(view_dir: vec3<f32>) -> vec4<f32> {
141-
return view::direction_view_to_clip(view_dir, view_bindings::view.clip_from_view);
141+
let clip_dir = view_bindings::view.clip_from_view * vec4(view_dir, 0.0);
142+
return clip_dir;
142143
}
143144

144145
// -----------------
145146
// TO NDC ----------
146147
// -----------------
147148

148149
/// Convert a world space position to ndc space
149-
/// DEPRECATED: use bevy_render::view::position_world_to_ndc instead
150150
fn position_world_to_ndc(world_pos: vec3<f32>) -> vec3<f32> {
151-
return view::position_world_to_ndc(world_pos, view_bindings::view.clip_from_world);
151+
let ndc_pos = view_bindings::view.clip_from_world * vec4(world_pos, 1.0);
152+
return ndc_pos.xyz / ndc_pos.w;
152153
}
153154

154155
/// Convert a view space position to ndc space
155-
/// DEPRECATED: use bevy_render::view::position_view_to_ndc instead
156156
fn position_view_to_ndc(view_pos: vec3<f32>) -> vec3<f32> {
157-
return view::position_view_to_ndc(view_pos, view_bindings::view.clip_from_view);
157+
let ndc_pos = view_bindings::view.clip_from_view * vec4(view_pos, 1.0);
158+
return ndc_pos.xyz / ndc_pos.w;
158159
}
159160

160161
// -----------------
161162
// DEPTH -----------
162163
// -----------------
163164

164165
/// Retrieve the perspective camera near clipping plane
165-
/// DEPRECATED: use bevy_render::view::perspective_camera_near instead
166166
fn perspective_camera_near() -> f32 {
167-
return view::perspective_camera_near(view_bindings::view.clip_from_view);
167+
return view_bindings::view.clip_from_view[3][2];
168168
}
169169

170170
/// Convert ndc depth to linear view z.
171171
/// Note: Depth values in front of the camera will be negative as -z is forward
172-
/// DEPRECATED: use bevy_render::view::depth_ndc_to_view_z instead
173172
fn depth_ndc_to_view_z(ndc_depth: f32) -> f32 {
174-
return view::depth_ndc_to_view_z(ndc_depth, view_bindings::view.clip_from_view, view_bindings::view.view_from_clip);
173+
#ifdef VIEW_PROJECTION_PERSPECTIVE
174+
return -perspective_camera_near() / ndc_depth;
175+
#else ifdef VIEW_PROJECTION_ORTHOGRAPHIC
176+
return -(view_bindings::view.clip_from_view[3][2] - ndc_depth) / view_bindings::view.clip_from_view[2][2];
177+
#else
178+
let view_pos = view_bindings::view.view_from_clip * vec4(0.0, 0.0, ndc_depth, 1.0);
179+
return view_pos.z / view_pos.w;
180+
#endif
175181
}
176182

177183
/// Convert linear view z to ndc depth.
178184
/// Note: View z input should be negative for values in front of the camera as -z is forward
179-
/// DEPRECATED: use bevy_render::view::view_z_to_depth_ndc instead
180185
fn view_z_to_depth_ndc(view_z: f32) -> f32 {
181-
return view::view_z_to_depth_ndc(view_z, view_bindings::view.clip_from_view);
186+
#ifdef VIEW_PROJECTION_PERSPECTIVE
187+
return -perspective_camera_near() / view_z;
188+
#else ifdef VIEW_PROJECTION_ORTHOGRAPHIC
189+
return view_bindings::view.clip_from_view[3][2] + view_z * view_bindings::view.clip_from_view[2][2];
190+
#else
191+
let ndc_pos = view_bindings::view.clip_from_view * vec4(0.0, 0.0, view_z, 1.0);
192+
return ndc_pos.z / ndc_pos.w;
193+
#endif
182194
}
183195

184-
/// DEPRECATED: use bevy_render::view::prev_view_z_to_depth_ndc instead
185196
fn prev_view_z_to_depth_ndc(view_z: f32) -> f32 {
186-
return view::view_z_to_depth_ndc(view_z, prepass_bindings::previous_view_uniforms.clip_from_view);
197+
#ifdef VIEW_PROJECTION_PERSPECTIVE
198+
return -perspective_camera_near() / view_z;
199+
#else ifdef VIEW_PROJECTION_ORTHOGRAPHIC
200+
return prepass_bindings::previous_view_uniforms.clip_from_view[3][2] +
201+
view_z * prepass_bindings::previous_view_uniforms.clip_from_view[2][2];
202+
#else
203+
let ndc_pos = prepass_bindings::previous_view_uniforms.clip_from_view *
204+
vec4(0.0, 0.0, view_z, 1.0);
205+
return ndc_pos.z / ndc_pos.w;
206+
#endif
187207
}
188208

189209
// -----------------
190210
// UV --------------
191211
// -----------------
192212

193213
/// Convert ndc space xy coordinate [-1.0 .. 1.0] to uv [0.0 .. 1.0]
194-
/// DEPRECATED: use bevy_render::view::ndc_to_uv instead
195214
fn ndc_to_uv(ndc: vec2<f32>) -> vec2<f32> {
196-
return view::ndc_to_uv(ndc);
215+
return ndc * vec2(0.5, -0.5) + vec2(0.5);
197216
}
198217

199218
/// Convert uv [0.0 .. 1.0] coordinate to ndc space xy [-1.0 .. 1.0]
200-
/// DEPRECATED: use bevy_render::view::uv_to_ndc instead
201219
fn uv_to_ndc(uv: vec2<f32>) -> vec2<f32> {
202-
return view::uv_to_ndc(uv);
220+
return uv * vec2(2.0, -2.0) + vec2(-1.0, 1.0);
203221
}
204222

205223
/// returns the (0.0, 0.0) .. (1.0, 1.0) position within the viewport for the current render target
206224
/// [0 .. render target viewport size] eg. [(0.0, 0.0) .. (1280.0, 720.0)] to [(0.0, 0.0) .. (1.0, 1.0)]
207-
/// DEPRECATED: use bevy_render::view::frag_coord_to_uv instead
208225
fn frag_coord_to_uv(frag_coord: vec2<f32>) -> vec2<f32> {
209-
return view::frag_coord_to_uv(frag_coord, view_bindings::view.viewport);
226+
return (frag_coord - view_bindings::view.viewport.xy) / view_bindings::view.viewport.zw;
210227
}
211228

212229
/// Convert frag coord to ndc
213-
/// DEPRECATED: use bevy_render::view::frag_coord_to_ndc instead
214230
fn frag_coord_to_ndc(frag_coord: vec4<f32>) -> vec3<f32> {
215-
return view::frag_coord_to_ndc(frag_coord, view_bindings::view.viewport);
231+
return vec3(uv_to_ndc(frag_coord_to_uv(frag_coord.xy)), frag_coord.z);
216232
}
217233

218234
/// Convert ndc space xy coordinate [-1.0 .. 1.0] to [0 .. render target
219235
/// viewport size]
220-
/// DEPRECATED: use bevy_render::view::ndc_to_frag_coord instead
221236
fn ndc_to_frag_coord(ndc: vec2<f32>) -> vec2<f32> {
222-
return view::ndc_to_frag_coord(ndc, view_bindings::view.viewport);
237+
return ndc_to_uv(ndc) * view_bindings::view.viewport.zw;
223238
}

0 commit comments

Comments
 (0)