Skip to content

Commit

Permalink
Make RenderSceneData take projection correction into account
Browse files Browse the repository at this point in the history
  • Loading branch information
BastiaanOlij committed Jun 26, 2024
1 parent 95110dd commit 08473c8
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 20 deletions.
4 changes: 4 additions & 0 deletions doc/classes/RenderSceneData.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
<methods>
<method name="get_cam_projection" qualifiers="const">
<return type="Projection" />
<param index="0" name="flip_y" type="bool" default="true" />
<description>
Returns the camera projection used to render this frame.
[b]Note:[/b] If more than one view is rendered, this will return a combined projection.
[param flip_y] should be true for viewports rendered with Rendering Device based renderers, false for reflection probes and the compatibility renderer.
</description>
</method>
<method name="get_cam_transform" qualifiers="const">
Expand Down Expand Up @@ -46,9 +48,11 @@
<method name="get_view_projection" qualifiers="const">
<return type="Projection" />
<param index="0" name="view" type="int" />
<param index="1" name="flip_y" type="bool" default="true" />
<description>
Returns the view projection per view used to render this frame.
[b]Note:[/b] If a single view is rendered, this returns the camera projection. If more than one view is rendered, this will return a projection for the given view including the eye offset.
[param flip_y] should be true for viewports rendered with Rendering Device based renderers, false for reflection probes and the compatibility renderer.
</description>
</method>
</methods>
Expand Down
2 changes: 2 additions & 0 deletions doc/classes/RenderSceneDataExtension.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<methods>
<method name="_get_cam_projection" qualifiers="virtual const">
<return type="Projection" />
<param index="0" name="flip_y" type="bool" />
<description>
Implement this in GDExtension to return the camera [Projection].
</description>
Expand Down Expand Up @@ -43,6 +44,7 @@
<method name="_get_view_projection" qualifiers="virtual const">
<return type="Projection" />
<param index="0" name="view" type="int" />
<param index="1" name="flip_y" type="bool" />
<description>
Implement this in GDExtension to return the view [Projection] for the given [param view].
</description>
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 @@ -41,8 +41,12 @@ Transform3D RenderSceneDataRD::get_cam_transform() const {
return cam_transform;
}

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

return correction * cam_projection;
}

uint32_t RenderSceneDataRD::get_view_count() const {
Expand All @@ -55,10 +59,14 @@ Vector3 RenderSceneDataRD::get_view_eye_offset(uint32_t p_view) const {
return view_eye_offset[p_view];
}

Projection RenderSceneDataRD::get_view_projection(uint32_t p_view) const {
Projection RenderSceneDataRD::get_view_projection(uint32_t p_view, bool p_flip_y) const {
ERR_FAIL_UNSIGNED_INDEX_V(p_view, view_count, Projection());

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

return correction * view_projection[p_view];
}

RID RenderSceneDataRD::create_uniform_buffer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ class RenderSceneDataRD : public RenderSceneData {
float time_step;

virtual Transform3D get_cam_transform() const override;
virtual Projection get_cam_projection() const override;
virtual Projection get_cam_projection(bool p_flip_y = true) const override;

virtual uint32_t get_view_count() const override;
virtual Vector3 get_view_eye_offset(uint32_t p_view) const override;
virtual Projection get_view_projection(uint32_t p_view) const override;
virtual Projection get_view_projection(uint32_t p_view, bool p_flip_y = true) 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);
Expand Down
46 changes: 46 additions & 0 deletions servers/rendering/storage/render_scene_data.compat.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**************************************************************************/
/* render_scene_data.compat.inc */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef DISABLE_DEPRECATED

Projection RenderSceneData::_get_cam_projection_93630() {
return get_cam_projection(true);
}

Projection RenderSceneData::_get_view_projection_93630(uint32_t p_view) {
return get_view_projection(p_view, true);
}

void RenderSceneData::_bind_compatibility_methods() {
ClassDB::bind_compatibility_method(D_METHOD("get_cam_projection"), &RenderSceneData::_get_cam_projection_93630);
ClassDB::bind_compatibility_method(D_METHOD("get_view_projection", "view"), &RenderSceneData::_get_view_projection_93630);
}

#endif // DISABLE_DEPRECATED
17 changes: 9 additions & 8 deletions servers/rendering/storage/render_scene_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,25 @@
/**************************************************************************/

#include "render_scene_data.h"
#include "render_scene_data.compat.inc"

void RenderSceneData::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_cam_transform"), &RenderSceneData::get_cam_transform);
ClassDB::bind_method(D_METHOD("get_cam_projection"), &RenderSceneData::get_cam_projection);
ClassDB::bind_method(D_METHOD("get_cam_projection", "flip_y"), &RenderSceneData::get_cam_projection, DEFVAL(true));

ClassDB::bind_method(D_METHOD("get_view_count"), &RenderSceneData::get_view_count);
ClassDB::bind_method(D_METHOD("get_view_eye_offset", "view"), &RenderSceneData::get_view_eye_offset);
ClassDB::bind_method(D_METHOD("get_view_projection", "view"), &RenderSceneData::get_view_projection);
ClassDB::bind_method(D_METHOD("get_view_projection", "view", "flip_y"), &RenderSceneData::get_view_projection, DEFVAL(true));

ClassDB::bind_method(D_METHOD("get_uniform_buffer"), &RenderSceneData::get_uniform_buffer);
}

void RenderSceneDataExtension::_bind_methods() {
GDVIRTUAL_BIND(_get_cam_transform);
GDVIRTUAL_BIND(_get_cam_projection);
GDVIRTUAL_BIND(_get_cam_projection, "flip_y");
GDVIRTUAL_BIND(_get_view_count);
GDVIRTUAL_BIND(_get_view_eye_offset, "view");
GDVIRTUAL_BIND(_get_view_projection, "view");
GDVIRTUAL_BIND(_get_view_projection, "view", "flip_y");

GDVIRTUAL_BIND(_get_uniform_buffer);
}
Expand All @@ -57,9 +58,9 @@ Transform3D RenderSceneDataExtension::get_cam_transform() const {
return ret;
}

Projection RenderSceneDataExtension::get_cam_projection() const {
Projection RenderSceneDataExtension::get_cam_projection(bool p_flip_y) const {
Projection ret;
GDVIRTUAL_CALL(_get_cam_projection, ret);
GDVIRTUAL_CALL(_get_cam_projection, p_flip_y, ret);
return ret;
}

Expand All @@ -75,9 +76,9 @@ Vector3 RenderSceneDataExtension::get_view_eye_offset(uint32_t p_view) const {
return ret;
}

Projection RenderSceneDataExtension::get_view_projection(uint32_t p_view) const {
Projection RenderSceneDataExtension::get_view_projection(uint32_t p_view, bool p_flip_y) const {
Projection ret;
GDVIRTUAL_CALL(_get_view_projection, p_view, ret);
GDVIRTUAL_CALL(_get_view_projection, p_view, p_flip_y, ret);
return ret;
}

Expand Down
20 changes: 14 additions & 6 deletions servers/rendering/storage/render_scene_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,21 @@ class RenderSceneData : public Object {
protected:
static void _bind_methods();

#ifndef DISABLE_DEPRECATED

Projection _get_cam_projection_93630();
Projection _get_view_projection_93630(uint32_t p_view);
static void _bind_compatibility_methods();

#endif // DISABLE_DEPRECATED

public:
virtual Transform3D get_cam_transform() const = 0;
virtual Projection get_cam_projection() const = 0;
virtual Projection get_cam_projection(bool p_flip_y = true) const = 0;

virtual uint32_t get_view_count() const = 0;
virtual Vector3 get_view_eye_offset(uint32_t p_view) const = 0;
virtual Projection get_view_projection(uint32_t p_view) const = 0;
virtual Projection get_view_projection(uint32_t p_view, bool p_flip_y = true) const = 0;

virtual RID get_uniform_buffer() const = 0;
};
Expand All @@ -61,20 +69,20 @@ class RenderSceneDataExtension : public RenderSceneData {

public:
virtual Transform3D get_cam_transform() const override;
virtual Projection get_cam_projection() const override;
virtual Projection get_cam_projection(bool p_flip_y = true) const override;

virtual uint32_t get_view_count() const override;
virtual Vector3 get_view_eye_offset(uint32_t p_view) const override;
virtual Projection get_view_projection(uint32_t p_view) const override;
virtual Projection get_view_projection(uint32_t p_view, bool p_flip_y = true) const override;

virtual RID get_uniform_buffer() const override;

GDVIRTUAL0RC(Transform3D, _get_cam_transform)
GDVIRTUAL0RC(Projection, _get_cam_projection)
GDVIRTUAL1RC(Projection, _get_cam_projection, bool)

GDVIRTUAL0RC(uint32_t, _get_view_count)
GDVIRTUAL1RC(Vector3, _get_view_eye_offset, uint32_t)
GDVIRTUAL1RC(Projection, _get_view_projection, uint32_t)
GDVIRTUAL2RC(Projection, _get_view_projection, uint32_t, bool)

GDVIRTUAL0RC(RID, _get_uniform_buffer)
};
Expand Down

0 comments on commit 08473c8

Please sign in to comment.