Skip to content

Commit

Permalink
Merge pull request #93630 from BastiaanOlij/scene_data_projection_cor…
Browse files Browse the repository at this point in the history
…rection

Make RenderSceneData take projection correction into account
  • Loading branch information
akien-mga committed Jul 8, 2024
2 parents 8bf4a15 + 6ed6212 commit 6f9c0aa
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ void RenderForwardClustered::_render_list_with_draw_list(RenderListParameters *p
RD::get_singleton()->draw_list_end();
}

void RenderForwardClustered::_setup_environment(const RenderDataRD *p_render_data, bool p_no_fog, const Size2i &p_screen_size, bool p_flip_y, const Color &p_default_bg_color, bool p_opaque_render_buffers, bool p_apply_alpha_multiplier, bool p_pancake_shadows, int p_index) {
void RenderForwardClustered::_setup_environment(const RenderDataRD *p_render_data, bool p_no_fog, const Size2i &p_screen_size, const Color &p_default_bg_color, bool p_opaque_render_buffers, bool p_apply_alpha_multiplier, bool p_pancake_shadows, int p_index) {
RendererRD::LightStorage *light_storage = RendererRD::LightStorage::get_singleton();

Ref<RenderSceneBuffersRD> rd = p_render_data->render_buffers;
Expand All @@ -603,7 +603,7 @@ void RenderForwardClustered::_setup_environment(const RenderDataRD *p_render_dat
}
}

p_render_data->scene_data->update_ubo(scene_state.uniform_buffers[p_index], get_debug_draw_mode(), env, reflection_probe_instance, p_render_data->camera_attributes, p_flip_y, p_pancake_shadows, p_screen_size, p_default_bg_color, _render_buffers_get_luminance_multiplier(), p_opaque_render_buffers, p_apply_alpha_multiplier);
p_render_data->scene_data->update_ubo(scene_state.uniform_buffers[p_index], get_debug_draw_mode(), env, reflection_probe_instance, p_render_data->camera_attributes, p_pancake_shadows, p_screen_size, p_default_bg_color, _render_buffers_get_luminance_multiplier(), p_opaque_render_buffers, p_apply_alpha_multiplier);

// now do implementation UBO

Expand Down Expand Up @@ -1732,7 +1732,7 @@ void RenderForwardClustered::_render_scene(RenderDataRD *p_render_data, const Co

_setup_lightmaps(p_render_data, *p_render_data->lightmaps, p_render_data->scene_data->cam_transform);
_setup_voxelgis(*p_render_data->voxel_gi_instances);
_setup_environment(p_render_data, is_reflection_probe, screen_size, !is_reflection_probe, p_default_bg_color, false);
_setup_environment(p_render_data, is_reflection_probe, screen_size, p_default_bg_color, false);

// May have changed due to the above (light buffer enlarged, as an example).
_update_render_base_uniform_set();
Expand Down Expand Up @@ -1995,7 +1995,7 @@ void RenderForwardClustered::_render_scene(RenderDataRD *p_render_data, const Co
// Shadow pass can change the base uniform set samplers.
_update_render_base_uniform_set();

_setup_environment(p_render_data, is_reflection_probe, screen_size, !is_reflection_probe, p_default_bg_color, true, using_motion_pass);
_setup_environment(p_render_data, is_reflection_probe, screen_size, p_default_bg_color, true, using_motion_pass);

RID rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_OPAQUE, p_render_data, radiance_texture, samplers, true);

Expand Down Expand Up @@ -2209,7 +2209,7 @@ void RenderForwardClustered::_render_scene(RenderDataRD *p_render_data, const Co

rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_ALPHA, p_render_data, radiance_texture, samplers, true);

_setup_environment(p_render_data, is_reflection_probe, screen_size, !is_reflection_probe, p_default_bg_color, false);
_setup_environment(p_render_data, is_reflection_probe, screen_size, p_default_bg_color, false);

{
uint32_t transparent_color_pass_flags = (color_pass_flags | COLOR_PASS_FLAG_TRANSPARENT) & ~(COLOR_PASS_FLAG_SEPARATE_SPECULAR);
Expand Down Expand Up @@ -2562,6 +2562,7 @@ void RenderForwardClustered::_render_shadow_append(RID p_framebuffer, const Page
SceneState::ShadowPass shadow_pass;

RenderSceneDataRD scene_data;
scene_data.flip_y = !p_flip_y; // Q: Why is this inverted? Do we assume flip in shadow logic?
scene_data.cam_projection = p_projection;
scene_data.cam_transform = p_transform;
scene_data.view_projection[0] = p_projection;
Expand All @@ -2581,7 +2582,7 @@ void RenderForwardClustered::_render_shadow_append(RID p_framebuffer, const Page
render_data.instances = &p_instances;
render_data.render_info = p_render_info;

_setup_environment(&render_data, true, p_viewport_size, !p_flip_y, Color(), false, false, p_use_pancake, shadow_pass_index);
_setup_environment(&render_data, true, p_viewport_size, Color(), false, false, p_use_pancake, shadow_pass_index);

if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_DISABLE_LOD) {
scene_data.screen_mesh_lod_threshold = 0.0;
Expand Down Expand Up @@ -2654,6 +2655,7 @@ void RenderForwardClustered::_render_particle_collider_heightfield(RID p_fb, con
RD::get_singleton()->draw_command_begin_label("Render Collider Heightfield");

RenderSceneDataRD scene_data;
scene_data.flip_y = true;
scene_data.cam_projection = p_cam_projection;
scene_data.cam_transform = p_cam_transform;
scene_data.view_projection[0] = p_cam_projection;
Expand All @@ -2673,7 +2675,7 @@ void RenderForwardClustered::_render_particle_collider_heightfield(RID p_fb, con

_update_render_base_uniform_set();

_setup_environment(&render_data, true, Vector2(1, 1), true, Color(), false, false, false);
_setup_environment(&render_data, true, Vector2(1, 1), Color(), false, false, false);

PassMode pass_mode = PASS_MODE_SHADOW;

Expand Down Expand Up @@ -2720,7 +2722,7 @@ void RenderForwardClustered::_render_material(const Transform3D &p_cam_transform

_update_render_base_uniform_set();

_setup_environment(&render_data, true, Vector2(1, 1), false, Color());
_setup_environment(&render_data, true, Vector2(1, 1), Color());

PassMode pass_mode = PASS_MODE_DEPTH_MATERIAL;
_fill_render_list(RENDER_LIST_SECONDARY, &render_data, pass_mode);
Expand Down Expand Up @@ -2771,7 +2773,7 @@ void RenderForwardClustered::_render_uv2(const PagedArray<RenderGeometryInstance

_update_render_base_uniform_set();

_setup_environment(&render_data, true, Vector2(1, 1), false, Color());
_setup_environment(&render_data, true, Vector2(1, 1), Color());

PassMode pass_mode = PASS_MODE_DEPTH_MATERIAL;
_fill_render_list(RENDER_LIST_SECONDARY, &render_data, pass_mode);
Expand Down Expand Up @@ -2887,7 +2889,7 @@ void RenderForwardClustered::_render_sdfgi(Ref<RenderSceneBuffersRD> p_render_bu
RendererRD::MaterialStorage::store_transform(to_bounds.affine_inverse() * scene_data.cam_transform, scene_state.ubo.sdf_to_bounds);

scene_data.emissive_exposure_normalization = p_exposure_normalization;
_setup_environment(&render_data, true, Vector2(1, 1), false, Color());
_setup_environment(&render_data, true, Vector2(1, 1), Color());

RID rp_uniform_set = _setup_sdfgi_render_pass_uniform_set(p_albedo_texture, p_emission_texture, p_emission_aniso_texture, p_geom_facing_texture, RendererRD::MaterialStorage::get_singleton()->samplers_rd_get_default());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ class RenderForwardClustered : public RendererSceneRenderRD {

static RenderForwardClustered *singleton;

void _setup_environment(const RenderDataRD *p_render_data, bool p_no_fog, const Size2i &p_screen_size, bool p_flip_y, const Color &p_default_bg_color, bool p_opaque_render_buffers = false, bool p_apply_alpha_multiplier = false, bool p_pancake_shadows = false, int p_index = 0);
void _setup_environment(const RenderDataRD *p_render_data, bool p_no_fog, const Size2i &p_screen_size, const Color &p_default_bg_color, bool p_opaque_render_buffers = false, bool p_apply_alpha_multiplier = false, bool p_pancake_shadows = false, int p_index = 0);
void _setup_voxelgis(const PagedArray<RID> &p_voxelgis);
void _setup_lightmaps(const RenderDataRD *p_render_data, const PagedArray<RID> &p_lightmaps, const Transform3D &p_cam_transform);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ void RenderForwardMobile::_render_scene(RenderDataRD *p_render_data, const Color
RD::get_singleton()->draw_command_begin_label("Render Setup");

_setup_lightmaps(p_render_data, *p_render_data->lightmaps, p_render_data->scene_data->cam_transform);
_setup_environment(p_render_data, is_reflection_probe, screen_size, !is_reflection_probe, p_default_bg_color, false);
_setup_environment(p_render_data, is_reflection_probe, screen_size, p_default_bg_color, false);

// May have changed due to the above (light buffer enlarged, as an example).
_update_render_base_uniform_set();
Expand Down Expand Up @@ -953,7 +953,7 @@ void RenderForwardMobile::_render_scene(RenderDataRD *p_render_data, const Color
// Shadow pass can change the base uniform set samplers.
_update_render_base_uniform_set();

_setup_environment(p_render_data, is_reflection_probe, screen_size, !is_reflection_probe, p_default_bg_color, p_render_data->render_buffers.is_valid());
_setup_environment(p_render_data, is_reflection_probe, screen_size, p_default_bg_color, p_render_data->render_buffers.is_valid());

if (merge_transparent_pass && using_subpass_post_process) {
RENDER_TIMESTAMP("Render Opaque + Transparent + Tonemap");
Expand Down Expand Up @@ -1075,7 +1075,7 @@ void RenderForwardMobile::_render_scene(RenderDataRD *p_render_data, const Color
rp_uniform_set = _setup_render_pass_uniform_set(RENDER_LIST_ALPHA, p_render_data, radiance_texture, samplers, true);

// this may be needed if we re-introduced steps that change info, not sure which do so in the previous implementation
//_setup_environment(p_render_data, is_reflection_probe, screen_size, !is_reflection_probe, p_default_bg_color, false);
//_setup_environment(p_render_data, is_reflection_probe, screen_size, p_default_bg_color, false);

RenderListParameters render_list_params(render_list[RENDER_LIST_ALPHA].elements.ptr(), render_list[RENDER_LIST_ALPHA].element_info.ptr(), render_list[RENDER_LIST_ALPHA].elements.size(), reverse_cull, PASS_MODE_COLOR, rp_uniform_set, spec_constant_base_flags, get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_WIREFRAME, Vector2(), p_render_data->scene_data->lod_distance_multiplier, p_render_data->scene_data->screen_mesh_lod_threshold, p_render_data->scene_data->view_count);
render_list_params.framebuffer_format = fb_format;
Expand Down Expand Up @@ -1310,6 +1310,7 @@ void RenderForwardMobile::_render_shadow_append(RID p_framebuffer, const PagedAr
}

RenderSceneDataRD scene_data;
scene_data.flip_y = !p_flip_y; // Q: Why is this inverted? Do we assume flip in shadow logic?
scene_data.cam_projection = p_projection;
scene_data.cam_transform = p_transform;
scene_data.view_projection[0] = p_projection;
Expand All @@ -1327,7 +1328,7 @@ void RenderForwardMobile::_render_shadow_append(RID p_framebuffer, const PagedAr
render_data.instances = &p_instances;
render_data.render_info = p_render_info;

_setup_environment(&render_data, true, Vector2(1, 1), !p_flip_y, Color(), false, p_use_pancake, shadow_pass_index);
_setup_environment(&render_data, true, Vector2(1, 1), Color(), false, p_use_pancake, shadow_pass_index);

if (get_debug_draw_mode() == RS::VIEWPORT_DEBUG_DRAW_DISABLE_LOD) {
scene_data.screen_mesh_lod_threshold = 0.0;
Expand Down Expand Up @@ -1415,7 +1416,7 @@ void RenderForwardMobile::_render_material(const Transform3D &p_cam_transform, c
render_data.scene_data = &scene_data;
render_data.instances = &p_instances;

_setup_environment(&render_data, true, Vector2(1, 1), false, Color());
_setup_environment(&render_data, true, Vector2(1, 1), Color());

PassMode pass_mode = PASS_MODE_DEPTH_MATERIAL;
_fill_render_list(RENDER_LIST_SECONDARY, &render_data, pass_mode);
Expand Down Expand Up @@ -1460,7 +1461,7 @@ void RenderForwardMobile::_render_uv2(const PagedArray<RenderGeometryInstance *>
render_data.scene_data = &scene_data;
render_data.instances = &p_instances;

_setup_environment(&render_data, true, Vector2(1, 1), false, Color());
_setup_environment(&render_data, true, Vector2(1, 1), Color());

PassMode pass_mode = PASS_MODE_DEPTH_MATERIAL;
_fill_render_list(RENDER_LIST_SECONDARY, &render_data, pass_mode);
Expand Down Expand Up @@ -1526,6 +1527,7 @@ void RenderForwardMobile::_render_particle_collider_heightfield(RID p_fb, const
_update_render_base_uniform_set();

RenderSceneDataRD scene_data;
scene_data.flip_y = true;
scene_data.cam_projection = p_cam_projection;
scene_data.cam_transform = p_cam_transform;
scene_data.view_projection[0] = p_cam_projection;
Expand All @@ -1541,7 +1543,7 @@ void RenderForwardMobile::_render_particle_collider_heightfield(RID p_fb, const
render_data.scene_data = &scene_data;
render_data.instances = &p_instances;

_setup_environment(&render_data, true, Vector2(1, 1), true, Color(), false, false);
_setup_environment(&render_data, true, Vector2(1, 1), Color(), false, false);

PassMode pass_mode = PASS_MODE_SHADOW;

Expand Down Expand Up @@ -1974,7 +1976,7 @@ void RenderForwardMobile::_fill_render_list(RenderListType p_render_list, const
}
}

void RenderForwardMobile::_setup_environment(const RenderDataRD *p_render_data, bool p_no_fog, const Size2i &p_screen_size, bool p_flip_y, const Color &p_default_bg_color, bool p_opaque_render_buffers, bool p_pancake_shadows, int p_index) {
void RenderForwardMobile::_setup_environment(const RenderDataRD *p_render_data, bool p_no_fog, const Size2i &p_screen_size, const Color &p_default_bg_color, bool p_opaque_render_buffers, bool p_pancake_shadows, int p_index) {
RID env = is_environment(p_render_data->environment) ? p_render_data->environment : RID();
RID reflection_probe_instance = p_render_data->reflection_probe.is_valid() ? RendererRD::LightStorage::get_singleton()->reflection_probe_instance_get_probe(p_render_data->reflection_probe) : RID();

Expand All @@ -1987,7 +1989,7 @@ void RenderForwardMobile::_setup_environment(const RenderDataRD *p_render_data,
}
}

p_render_data->scene_data->update_ubo(scene_state.uniform_buffers[p_index], get_debug_draw_mode(), env, reflection_probe_instance, p_render_data->camera_attributes, p_flip_y, p_pancake_shadows, p_screen_size, p_default_bg_color, _render_buffers_get_luminance_multiplier(), p_opaque_render_buffers, false);
p_render_data->scene_data->update_ubo(scene_state.uniform_buffers[p_index], get_debug_draw_mode(), env, reflection_probe_instance, p_render_data->camera_attributes, p_pancake_shadows, p_screen_size, p_default_bg_color, _render_buffers_get_luminance_multiplier(), p_opaque_render_buffers, false);
}

/// RENDERING ///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class RenderForwardMobile : public RendererSceneRenderRD {
void _fill_instance_data(RenderListType p_render_list, uint32_t p_offset = 0, int32_t p_max_elements = -1, bool p_update_buffer = true);
void _fill_render_list(RenderListType p_render_list, const RenderDataRD *p_render_data, PassMode p_pass_mode, bool p_append = false);

void _setup_environment(const RenderDataRD *p_render_data, bool p_no_fog, const Size2i &p_screen_size, bool p_flip_y, const Color &p_default_bg_color, bool p_opaque_render_buffers = false, bool p_pancake_shadows = false, int p_index = 0);
void _setup_environment(const RenderDataRD *p_render_data, bool p_no_fog, const Size2i &p_screen_size, const Color &p_default_bg_color, bool p_opaque_render_buffers = false, bool p_pancake_shadows = false, int p_index = 0);
void _setup_lightmaps(const RenderDataRD *p_render_data, const PagedArray<RID> &p_lightmaps, const Transform3D &p_cam_transform);

RID render_base_uniform_set;
Expand Down
1 change: 1 addition & 0 deletions servers/rendering/renderer_rd/renderer_scene_render_rd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,7 @@ void RendererSceneRenderRD::render_scene(const Ref<RenderSceneBuffers> &p_render
scene_data.camera_visible_layers = p_camera_data->visible_layers;
scene_data.taa_jitter = p_camera_data->taa_jitter;
scene_data.main_cam_transform = p_camera_data->main_transform;
scene_data.flip_y = !p_reflection_probe.is_valid();

scene_data.view_count = p_camera_data->view_count;
for (uint32_t v = 0; v < p_camera_data->view_count; v++) {
Expand Down
16 changes: 12 additions & 4 deletions servers/rendering/renderer_rd/storage_rd/render_scene_data_rd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ Transform3D RenderSceneDataRD::get_cam_transform() const {
}

Projection RenderSceneDataRD::get_cam_projection() const {
return cam_projection;
Projection correction;
correction.set_depth_correction(flip_y);
correction.add_jitter_offset(taa_jitter);

return correction * cam_projection;
}

uint32_t RenderSceneDataRD::get_view_count() const {
Expand All @@ -58,14 +62,18 @@ Vector3 RenderSceneDataRD::get_view_eye_offset(uint32_t p_view) const {
Projection RenderSceneDataRD::get_view_projection(uint32_t p_view) const {
ERR_FAIL_UNSIGNED_INDEX_V(p_view, view_count, Projection());

return view_projection[p_view];
Projection correction;
correction.set_depth_correction(flip_y);
correction.add_jitter_offset(taa_jitter);

return correction * view_projection[p_view];
}

RID RenderSceneDataRD::create_uniform_buffer() {
return RD::get_singleton()->uniform_buffer_create(sizeof(UBODATA));
}

void RenderSceneDataRD::update_ubo(RID p_uniform_buffer, RS::ViewportDebugDraw p_debug_mode, RID p_env, RID p_reflection_probe_instance, RID p_camera_attributes, bool p_flip_y, bool p_pancake_shadows, const Size2i &p_screen_size, const Color &p_default_bg_color, float p_luminance_multiplier, bool p_opaque_render_buffers, bool p_apply_alpha_multiplier) {
void RenderSceneDataRD::update_ubo(RID p_uniform_buffer, RS::ViewportDebugDraw p_debug_mode, RID p_env, RID p_reflection_probe_instance, RID p_camera_attributes, bool p_pancake_shadows, const Size2i &p_screen_size, const Color &p_default_bg_color, float p_luminance_multiplier, bool p_opaque_render_buffers, bool p_apply_alpha_multiplier) {
RendererSceneRenderRD *render_scene_render = RendererSceneRenderRD::get_singleton();

UBODATA ubo_data;
Expand All @@ -76,7 +84,7 @@ void RenderSceneDataRD::update_ubo(RID p_uniform_buffer, RS::ViewportDebugDraw p
UBO &prev_ubo = ubo_data.prev_ubo;

Projection correction;
correction.set_depth_correction(p_flip_y);
correction.set_depth_correction(flip_y);
correction.add_jitter_offset(taa_jitter);
Projection projection = correction * cam_projection;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class RenderSceneDataRD : public RenderSceneData {
Vector2 taa_jitter;
uint32_t camera_visible_layers;
bool cam_orthogonal = false;
bool flip_y = false;

// For billboards to cast correct shadows.
Transform3D main_cam_transform;
Expand Down Expand Up @@ -90,7 +91,7 @@ class RenderSceneDataRD : public RenderSceneData {
virtual Projection get_view_projection(uint32_t p_view) const override;

RID create_uniform_buffer();
void update_ubo(RID p_uniform_buffer, RS::ViewportDebugDraw p_debug_mode, RID p_env, RID p_reflection_probe_instance, RID p_camera_attributes, bool p_flip_y, bool p_pancake_shadows, const Size2i &p_screen_size, const Color &p_default_bg_color, float p_luminance_multiplier, bool p_opaque_render_buffers, bool p_apply_alpha_multiplier);
void update_ubo(RID p_uniform_buffer, RS::ViewportDebugDraw p_debug_mode, RID p_env, RID p_reflection_probe_instance, RID p_camera_attributes, bool p_pancake_shadows, const Size2i &p_screen_size, const Color &p_default_bg_color, float p_luminance_multiplier, bool p_opaque_render_buffers, bool p_apply_alpha_multiplier);
virtual RID get_uniform_buffer() const override;

private:
Expand Down

0 comments on commit 6f9c0aa

Please sign in to comment.