Skip to content

Commit

Permalink
Merge pull request godotengine#100282 from devloglogan/motion-vectors…
Browse files Browse the repository at this point in the history
…-openxr

Add renderer motion vectors API for use with OpenXR
  • Loading branch information
Repiteo committed Dec 12, 2024
2 parents 91dc363 + 3deb588 commit bfc66f0
Show file tree
Hide file tree
Showing 25 changed files with 429 additions and 12 deletions.
2 changes: 1 addition & 1 deletion drivers/gles3/storage/texture_storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2491,7 +2491,7 @@ Size2i TextureStorage::render_target_get_size(RID p_render_target) const {
return rt->size;
}

void TextureStorage::render_target_set_override(RID p_render_target, RID p_color_texture, RID p_depth_texture, RID p_velocity_texture) {
void TextureStorage::render_target_set_override(RID p_render_target, RID p_color_texture, RID p_depth_texture, RID p_velocity_texture, RID p_velocity_depth_texture) {
RenderTarget *rt = render_target_owner.get_or_null(p_render_target);
ERR_FAIL_NULL(rt);
ERR_FAIL_COND(rt->direct_to_screen);
Expand Down
6 changes: 5 additions & 1 deletion drivers/gles3/storage/texture_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -685,13 +685,17 @@ class TextureStorage : public RendererTextureStorage {
virtual void render_target_set_vrs_texture(RID p_render_target, RID p_texture) override {}
virtual RID render_target_get_vrs_texture(RID p_render_target) const override { return RID(); }

virtual void render_target_set_override(RID p_render_target, RID p_color_texture, RID p_depth_texture, RID p_velocity_texture) override;
virtual void render_target_set_override(RID p_render_target, RID p_color_texture, RID p_depth_texture, RID p_velocity_texture, RID p_velocity_depth_texture) override;
virtual RID render_target_get_override_color(RID p_render_target) const override;
virtual RID render_target_get_override_depth(RID p_render_target) const override;
virtual RID render_target_get_override_velocity(RID p_render_target) const override;
virtual RID render_target_get_override_velocity_depth(RID p_render_target) const override { return RID(); }

virtual RID render_target_get_texture(RID p_render_target) override;

virtual void render_target_set_velocity_target_size(RID p_render_target, const Size2i &p_target_size) override {}
virtual Size2i render_target_get_velocity_target_size(RID p_render_target) const override { return Size2i(); }

void bind_framebuffer(GLuint framebuffer) {
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
}
Expand Down
103 changes: 103 additions & 0 deletions modules/openxr/doc_classes/OpenXRAPIExtension.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,32 @@
Returns the predicted display timing for the current frame.
</description>
</method>
<method name="get_render_state_z_far">
<return type="float" />
<description>
Returns the far boundary value of the camera frustum.
[b]Note:[/b] This is only accessible in the render thread.
</description>
</method>
<method name="get_render_state_z_near">
<return type="float" />
<description>
Returns the near boundary value of the camera frustum.
[b]Note:[/b] This is only accessible in the render thread.
</description>
</method>
<method name="get_session">
<return type="int" />
<description>
Returns the OpenXR session, which is an [url=https://registry.khronos.org/OpenXR/specs/1.0/man/html/XrSession.html]XrSession[/url] cast to an integer.
</description>
</method>
<method name="get_supported_swapchain_formats">
<return type="PackedInt64Array" />
<description>
Returns an array of supported swapchain formats.
</description>
</method>
<method name="get_swapchain_format_name">
<return type="String" />
<param index="0" name="swapchain_format" type="int" />
Expand Down Expand Up @@ -133,13 +153,68 @@
Returns [code]true[/code] if OpenXR is enabled.
</description>
</method>
<method name="openxr_swapchain_acquire">
<return type="void" />
<param index="0" name="swapchain" type="int" />
<description>
Acquires the image of the provided swapchain.
</description>
</method>
<method name="openxr_swapchain_create">
<return type="int" />
<param index="0" name="create_flags" type="int" />
<param index="1" name="usage_flags" type="int" />
<param index="2" name="swapchain_format" type="int" />
<param index="3" name="width" type="int" />
<param index="4" name="height" type="int" />
<param index="5" name="sample_count" type="int" />
<param index="6" name="array_size" type="int" />
<description>
Returns a pointer to a new swapchain created using the provided parameters.
</description>
</method>
<method name="openxr_swapchain_free">
<return type="void" />
<param index="0" name="swapchain" type="int" />
<description>
Destroys the provided swapchain and frees it from memory.
</description>
</method>
<method name="openxr_swapchain_get_image">
<return type="RID" />
<param index="0" name="swapchain" type="int" />
<description>
Returns the RID of the provided swapchain's image.
</description>
</method>
<method name="openxr_swapchain_get_swapchain">
<return type="int" />
<param index="0" name="swapchain" type="int" />
<description>
Returns the [code]XrSwapchain[/code] handle of the provided swapchain.
</description>
</method>
<method name="openxr_swapchain_release">
<return type="void" />
<param index="0" name="swapchain" type="int" />
<description>
Releases the image of the provided swapchain.
</description>
</method>
<method name="register_composition_layer_provider">
<return type="void" />
<param index="0" name="extension" type="OpenXRExtensionWrapperExtension" />
<description>
Registers the given extension as a composition layer provider.
</description>
</method>
<method name="register_projection_views_extension">
<return type="void" />
<param index="0" name="extension" type="OpenXRExtensionWrapperExtension" />
<description>
Registers the given extension as a provider of additional data structures to projections views.
</description>
</method>
<method name="set_emulate_environment_blend_mode_alpha_blend">
<return type="void" />
<param index="0" name="enabled" type="bool" />
Expand All @@ -156,6 +231,27 @@
Set the object name of an OpenXR object, used for debug output. [param object_type] must be a valid OpenXR [code]XrObjectType[/code] enum and [param object_handle] must be a valid OpenXR object handle.
</description>
</method>
<method name="set_velocity_depth_texture">
<return type="void" />
<param index="0" name="render_target" type="RID" />
<description>
Sets the render target of the velocity depth texture.
</description>
</method>
<method name="set_velocity_target_size">
<return type="void" />
<param index="0" name="target_size" type="Vector2i" />
<description>
Sets the target size of the velocity and velocity depth textures.
</description>
</method>
<method name="set_velocity_texture">
<return type="void" />
<param index="0" name="render_target" type="RID" />
<description>
Sets the render target of the velocity texture.
</description>
</method>
<method name="transform_from_pose">
<return type="Transform3D" />
<param index="0" name="pose" type="const void*" />
Expand All @@ -170,6 +266,13 @@
Unregisters the given extension as a composition layer provider.
</description>
</method>
<method name="unregister_projection_views_extension">
<return type="void" />
<param index="0" name="extension" type="OpenXRExtensionWrapperExtension" />
<description>
Unregisters the given extension as a provider of additional data structures to projections views.
</description>
</method>
<method name="xr_result">
<return type="bool" />
<param index="0" name="result" type="int" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,14 @@
Adds additional data structures when the OpenXR instance is created.
</description>
</method>
<method name="_set_projection_views_and_get_next_pointer" qualifiers="virtual">
<return type="int" />
<param index="0" name="view_index" type="int" />
<param index="1" name="next_pointer" type="void*" />
<description>
Adds additional data structures to the projection view of the given [param view_index].
</description>
</method>
<method name="_set_session_create_and_get_next_pointer" qualifiers="virtual">
<return type="int" />
<param index="0" name="next_pointer" type="void*" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ void OpenXRViewportCompositionLayerProvider::set_viewport(RID p_viewport, Size2i
if (subviewport.viewport != p_viewport) {
if (subviewport.viewport.is_valid()) {
RID rt = rs->viewport_get_render_target(subviewport.viewport);
RSG::texture_storage->render_target_set_override(rt, RID(), RID(), RID());
RSG::texture_storage->render_target_set_override(rt, RID(), RID(), RID(), RID());
}

subviewport.viewport = p_viewport;
Expand Down Expand Up @@ -323,7 +323,7 @@ void OpenXRViewportCompositionLayerProvider::on_pre_render() {
if (update_and_acquire_swapchain(update_mode == RS::VIEWPORT_UPDATE_ONCE)) {
// Render to our XR swapchain image.
RID rt = rs->viewport_get_render_target(subviewport.viewport);
RSG::texture_storage->render_target_set_override(rt, get_current_swapchain_texture(), RID(), RID());
RSG::texture_storage->render_target_set_override(rt, get_current_swapchain_texture(), RID(), RID(), RID());
}
}
}
Expand Down
1 change: 1 addition & 0 deletions modules/openxr/extensions/openxr_extension_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class OpenXRExtensionWrapper {
virtual void *set_session_create_and_get_next_pointer(void *p_next_pointer) { return p_next_pointer; } // Add additional data structures when we create our OpenXR session.
virtual void *set_swapchain_create_info_and_get_next_pointer(void *p_next_pointer) { return p_next_pointer; } // Add additional data structures when creating OpenXR swap chains.
virtual void *set_hand_joint_locations_and_get_next_pointer(int p_hand_index, void *p_next_pointer) { return p_next_pointer; }
virtual void *set_projection_views_and_get_next_pointer(int p_view_index, void *p_next_pointer) { return p_next_pointer; }

virtual PackedStringArray get_suggested_tracker_names() { return PackedStringArray(); }

Expand Down
11 changes: 11 additions & 0 deletions modules/openxr/extensions/openxr_extension_wrapper_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ void OpenXRExtensionWrapperExtension::_bind_methods() {
GDVIRTUAL_BIND(_set_session_create_and_get_next_pointer, "next_pointer");
GDVIRTUAL_BIND(_set_swapchain_create_info_and_get_next_pointer, "next_pointer");
GDVIRTUAL_BIND(_set_hand_joint_locations_and_get_next_pointer, "hand_index", "next_pointer");
GDVIRTUAL_BIND(_set_projection_views_and_get_next_pointer, "view_index", "next_pointer");
GDVIRTUAL_BIND(_get_composition_layer_count);
GDVIRTUAL_BIND(_get_composition_layer, "index");
GDVIRTUAL_BIND(_get_composition_layer_order, "index");
Expand Down Expand Up @@ -140,6 +141,16 @@ void *OpenXRExtensionWrapperExtension::set_hand_joint_locations_and_get_next_poi
return nullptr;
}

void *OpenXRExtensionWrapperExtension::set_projection_views_and_get_next_pointer(int p_view_index, void *p_next_pointer) {
uint64_t pointer = 0;

if (GDVIRTUAL_CALL(_set_projection_views_and_get_next_pointer, p_view_index, GDExtensionPtr<void>(p_next_pointer), pointer)) {
return reinterpret_cast<void *>(pointer);
}

return nullptr;
}

PackedStringArray OpenXRExtensionWrapperExtension::get_suggested_tracker_names() {
PackedStringArray ret;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class OpenXRExtensionWrapperExtension : public Object, public OpenXRExtensionWra
virtual void *set_session_create_and_get_next_pointer(void *p_next_pointer) override;
virtual void *set_swapchain_create_info_and_get_next_pointer(void *p_next_pointer) override;
virtual void *set_hand_joint_locations_and_get_next_pointer(int p_hand_index, void *p_next_pointer) override;
virtual void *set_projection_views_and_get_next_pointer(int p_view_index, void *p_next_pointer) override;

virtual int get_composition_layer_count() override;
virtual XrCompositionLayerBaseHeader *get_composition_layer(int p_index) override;
Expand All @@ -71,6 +72,7 @@ class OpenXRExtensionWrapperExtension : public Object, public OpenXRExtensionWra
GDVIRTUAL1R(uint64_t, _set_session_create_and_get_next_pointer, GDExtensionPtr<void>);
GDVIRTUAL1R(uint64_t, _set_swapchain_create_info_and_get_next_pointer, GDExtensionPtr<void>);
GDVIRTUAL2R(uint64_t, _set_hand_joint_locations_and_get_next_pointer, int, GDExtensionPtr<void>);
GDVIRTUAL2R(uint64_t, _set_projection_views_and_get_next_pointer, int, GDExtensionPtr<void>);
GDVIRTUAL0R(int, _get_composition_layer_count);
GDVIRTUAL1R(uint64_t, _get_composition_layer, int);
GDVIRTUAL1R(int, _get_composition_layer_order, int);
Expand Down
57 changes: 57 additions & 0 deletions modules/openxr/openxr_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1942,6 +1942,9 @@ bool OpenXRAPI::get_view_projection(uint32_t p_view, double p_z_near, double p_z
}
}

render_state.z_near = p_z_near;
render_state.z_far = p_z_far;

// now update our projection
return graphics_extension->create_projection_fov(render_state.views[p_view].fov, p_z_near, p_z_far, p_camera_matrix);
}
Expand Down Expand Up @@ -2369,6 +2372,30 @@ RID OpenXRAPI::get_depth_texture() {
}
}

void OpenXRAPI::set_velocity_texture(RID p_render_target) {
velocity_texture = p_render_target;
}

RID OpenXRAPI::get_velocity_texture() {
return velocity_texture;
}

void OpenXRAPI::set_velocity_depth_texture(RID p_render_target) {
velocity_depth_texture = p_render_target;
}

RID OpenXRAPI::get_velocity_depth_texture() {
return velocity_depth_texture;
}

void OpenXRAPI::set_velocity_target_size(const Size2i &p_target_size) {
velocity_target_size = p_target_size;
}

Size2i OpenXRAPI::get_velocity_target_size() {
return velocity_target_size;
}

void OpenXRAPI::post_draw_viewport(RID p_render_target) {
// Must be called from rendering thread!
ERR_NOT_ON_RENDER_THREAD;
Expand Down Expand Up @@ -2474,6 +2501,20 @@ void OpenXRAPI::end_frame() {
render_state.view_count, // viewCount
render_state.projection_views, // views
};

if (projection_views_extensions.size() > 0) {
for (uint32_t v = 0; v < render_state.view_count; v++) {
void *next_pointer = nullptr;
for (OpenXRExtensionWrapper *wrapper : projection_views_extensions) {
void *np = wrapper->set_projection_views_and_get_next_pointer(v, next_pointer);
if (np != nullptr) {
next_pointer = np;
}
}
render_state.projection_views[v].next = next_pointer;
}
}

ordered_layers_list.push_back({ (const XrCompositionLayerBaseHeader *)&projection_layer, 0 });

// Sort our layers.
Expand Down Expand Up @@ -2604,6 +2645,14 @@ Size2 OpenXRAPI::get_play_space_bounds() const {
return ret;
}

PackedInt64Array OpenXRAPI::get_supported_swapchain_formats() {
PackedInt64Array supported_swapchain_list;
for (uint32_t i = 0; i < num_swapchain_formats; i++) {
supported_swapchain_list.push_back(supported_swapchain_formats[i]);
}
return supported_swapchain_list;
}

OpenXRAPI::OpenXRAPI() {
// OpenXRAPI is only constructed if OpenXR is enabled.
singleton = this;
Expand Down Expand Up @@ -3607,6 +3656,14 @@ void OpenXRAPI::unregister_composition_layer_provider(OpenXRCompositionLayerProv
composition_layer_providers.erase(provider);
}

void OpenXRAPI::register_projection_views_extension(OpenXRExtensionWrapper *p_extension) {
projection_views_extensions.append(p_extension);
}

void OpenXRAPI::unregister_projection_views_extension(OpenXRExtensionWrapper *p_extension) {
projection_views_extensions.erase(p_extension);
}

const XrEnvironmentBlendMode *OpenXRAPI::get_supported_environment_blend_modes(uint32_t &count) {
count = num_supported_environment_blend_modes;
return supported_environment_blend_modes;
Expand Down
Loading

0 comments on commit bfc66f0

Please sign in to comment.