Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch WebXRInterface from using external textures to using the render targets from Godot #55869

Merged
merged 1 commit into from
Dec 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions modules/webxr/godot_webxr.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ extern int godot_webxr_get_view_count();
extern int *godot_webxr_get_render_targetsize();
extern float *godot_webxr_get_transform_for_eye(int p_eye);
extern float *godot_webxr_get_projection_for_eye(int p_eye);
extern int godot_webxr_get_external_texture_for_eye(int p_eye);
extern void godot_webxr_commit_for_eye(int p_eye);
extern void godot_webxr_commit_for_eye(int p_eye, unsigned int p_texture_id);

extern void godot_webxr_sample_controller_data();
extern int godot_webxr_get_controller_count();
Expand Down
66 changes: 3 additions & 63 deletions modules/webxr/native/library_godot_webxr.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ const GodotWebXR = {
$GodotWebXR: {
gl: null,

texture_ids: [null, null],
textures: [null, null],

session: null,
space: null,
frame: null,
Expand Down Expand Up @@ -371,22 +368,6 @@ const GodotWebXR = {
.catch((e) => { });
}

// Clean-up the textures we allocated for each view.
const gl = GodotWebXR.gl;
for (let i = 0; i < GodotWebXR.textures.length; i++) {
const texture = GodotWebXR.textures[i];
if (texture !== null) {
gl.deleteTexture(texture);
}
GodotWebXR.textures[i] = null;

const texture_id = GodotWebXR.texture_ids[i];
if (texture_id !== null) {
GL.textures[texture_id] = null;
}
GodotWebXR.texture_ids[i] = null;
}

GodotWebXR.session = null;
GodotWebXR.space = null;
GodotWebXR.frame = null;
Expand Down Expand Up @@ -461,50 +442,9 @@ const GodotWebXR = {
return buf;
},

godot_webxr_get_external_texture_for_eye__proxy: 'sync',
godot_webxr_get_external_texture_for_eye__sig: 'ii',
godot_webxr_get_external_texture_for_eye: function (p_eye) {
if (!GodotWebXR.session) {
return 0;
}

const view_index = (p_eye === 2 /* ARVRInterface::EYE_RIGHT */) ? 1 : 0;
if (GodotWebXR.texture_ids[view_index]) {
return GodotWebXR.texture_ids[view_index];
}

// Check pose separately and after returning the cached texture id,
// because we won't get a pose in some cases if we lose tracking, and
// we don't want to return 0 just because tracking was lost.
if (!GodotWebXR.pose) {
return 0;
}

const glLayer = GodotWebXR.session.renderState.baseLayer;
const view = GodotWebXR.pose.views[view_index];
const viewport = glLayer.getViewport(view);
const gl = GodotWebXR.gl;

const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, viewport.width, viewport.height, 0, gl.RGBA, gl.UNSIGNED_BYTE, null);

gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.bindTexture(gl.TEXTURE_2D, null);

const texture_id = GL.getNewId(GL.textures);
GL.textures[texture_id] = texture;
GodotWebXR.textures[view_index] = texture;
GodotWebXR.texture_ids[view_index] = texture_id;
return texture_id;
},

godot_webxr_commit_for_eye__proxy: 'sync',
godot_webxr_commit_for_eye__sig: 'vi',
godot_webxr_commit_for_eye: function (p_eye) {
godot_webxr_commit_for_eye__sig: 'vii',
godot_webxr_commit_for_eye: function (p_eye, p_texture_id) {
if (!GodotWebXR.session || !GodotWebXR.pose) {
return;
}
Expand All @@ -522,7 +462,7 @@ const GodotWebXR = {
gl.bindFramebuffer(gl.FRAMEBUFFER, glLayer.framebuffer);
gl.viewport(viewport.x, viewport.y, viewport.width, viewport.height);

GodotWebXR.blitTexture(gl, GodotWebXR.textures[view_index]);
GodotWebXR.blitTexture(gl, GL.textures[p_texture_id]);

// Restore state.
gl.bindFramebuffer(gl.FRAMEBUFFER, orig_framebuffer);
Expand Down
13 changes: 5 additions & 8 deletions modules/webxr/webxr_interface_js.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,18 +349,15 @@ CameraMatrix WebXRInterfaceJS::get_projection_for_eye(ARVRInterface::Eyes p_eye,
return eye;
}

unsigned int WebXRInterfaceJS::get_external_texture_for_eye(ARVRInterface::Eyes p_eye) {
if (!initialized) {
return 0;
}
return godot_webxr_get_external_texture_for_eye(p_eye);
}

void WebXRInterfaceJS::commit_for_eye(ARVRInterface::Eyes p_eye, RID p_render_target, const Rect2 &p_screen_rect) {
if (!initialized) {
return;
}
godot_webxr_commit_for_eye(p_eye);

RID texture = VSG::storage->render_target_get_texture(p_render_target);
uint32_t texture_id = VSG::storage->texture_get_texid(texture);

godot_webxr_commit_for_eye(p_eye, texture_id);
};

void WebXRInterfaceJS::process() {
Expand Down
1 change: 0 additions & 1 deletion modules/webxr/webxr_interface_js.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ class WebXRInterfaceJS : public WebXRInterface {
virtual bool is_stereo();
virtual Transform get_transform_for_eye(ARVRInterface::Eyes p_eye, const Transform &p_cam_transform);
virtual CameraMatrix get_projection_for_eye(ARVRInterface::Eyes p_eye, real_t p_aspect, real_t p_z_near, real_t p_z_far);
virtual unsigned int get_external_texture_for_eye(ARVRInterface::Eyes p_eye);
virtual void commit_for_eye(ARVRInterface::Eyes p_eye, RID p_render_target, const Rect2 &p_screen_rect);

virtual void process();
Expand Down