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

Fix texture copy for Forward+ renderer #41

Merged
merged 1 commit into from
Oct 10, 2023
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
5 changes: 1 addition & 4 deletions example/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ config_version=5
config/name="TiltFiveGodot4"
run/main_scene="res://main.tscn"
config/features=PackedStringArray("4.1")
run/max_fps=60
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to add this in?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really but it shouldn't do any harm. I've been told the T5 won't run over 60fps.

config/icon="res://icon.png"

[autoload]
Expand All @@ -33,10 +34,6 @@ trigger={
3d_render/layer_2="Only spectator"
3d_render/layer_3="Only glasses"

[rendering]

renderer/rendering_method="gl_compatibility"

[xr]

shaders/enabled=true
3 changes: 3 additions & 0 deletions extension/T5Integration/Glasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ class Glasses
void get_wand_stick(size_t wand_num, float& out_stick_x, float& out_stick_y);
void get_wand_buttons(size_t wand_num, WandButtons& buttons);

virtual void on_post_draw() {}

protected:

void set_swap_chain_size(int size);
void set_swap_chain_texture_pair(int swap_chain_idx, intptr_t left_eye_handle, intptr_t right_eye_handle);
void set_swap_chain_texture_array(int swap_chain_idx, intptr_t array_handle);
Expand Down
14 changes: 8 additions & 6 deletions extension/src/OpenGLGlasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ namespace GodotT5Integration {
Ref<Texture2DArray> render_tex;
};

public:

OpenGLGlasses(std::string_view id);

virtual RID get_color_texture() override;

private:

void allocate_textures();
void deallocate_textures();

virtual void on_glasses_reserved() override;
virtual void on_glasses_released() override;
virtual void on_glasses_dropped() override;

public:

OpenGLGlasses(std::string_view id);

virtual RID get_color_texture() override;

private:

std::vector<SwapChainTextures> _swap_chain_textures;
Expand Down
14 changes: 1 addition & 13 deletions extension/src/TiltFiveXRInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,22 +372,11 @@ bool TiltFiveXRInterface::_pre_draw_viewport(const RID &render_target) {
}

void TiltFiveXRInterface::_post_draw_viewport(const RID &render_target, const Rect2 &screen_rect) {
Rect2 src_rect(0.0f, 0.0f, 1.0f, 1.0f);
Rect2 dst_rect = screen_rect;

// halve our width
Vector2 size = dst_rect.get_size();
size.x = size.x * 0.5;
dst_rect.size = size;

Vector2 eye_center(0.0, 0.0);

//add_blit(render_target, src_rect, screen_rect, true, 0, false, eye_center, 0, 0, 0, 0);
_render_glasses->on_post_draw();
_render_glasses.reset();
}

void TiltFiveXRInterface::_end_frame() {

for(auto& entry : _glasses_index) {
if(entry.rendering) {
entry.glasses.lock()->send_frame();
Expand Down Expand Up @@ -418,7 +407,6 @@ PackedStringArray TiltFiveXRInterface::_get_suggested_pose_names(const StringNam
}

void TiltFiveXRInterface::_process() {

if(!t5_service) return;

t5_service->update_connection();
Expand Down
59 changes: 57 additions & 2 deletions extension/src/VulkanGlasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,23 @@ void VulkanGlasses::SwapChainTextures::allocate_textures(int width, int height)

render_tex = render_device->texture_create(texture_format_render, texture_view);

left_eye_tex = render_device->texture_create_shared_from_slice(texture_view, render_tex, 0, 0);
right_eye_tex = render_device->texture_create_shared_from_slice(texture_view, render_tex, 1, 0);
Ref<RDTextureFormat> texture_format_send;
texture_format_send.instantiate();

texture_format_send->set_texture_type(RenderingDevice::TextureType::TEXTURE_TYPE_2D);
texture_format_send->set_format(RenderingDevice::DataFormat::DATA_FORMAT_R8G8B8A8_UNORM);
texture_format_send->set_width(width);
texture_format_send->set_height(height);
texture_format_send->set_depth(1);
texture_format_send->set_array_layers(1);
texture_format_send->set_mipmaps(1);
texture_format_send->set_samples(RenderingDevice::TextureSamples::TEXTURE_SAMPLES_1);
texture_format_send->set_usage_bits(RenderingDevice::TEXTURE_USAGE_SAMPLING_BIT | RenderingDevice::TEXTURE_USAGE_CAN_COPY_TO_BIT | RenderingDevice::TEXTURE_USAGE_CAN_COPY_FROM_BIT);
texture_format_send->add_shareable_format(RenderingDevice::DataFormat::DATA_FORMAT_R8G8B8A8_UNORM);
texture_format_send->add_shareable_format(RenderingDevice::DataFormat::DATA_FORMAT_R8G8B8A8_SRGB);

left_eye_tex = render_device->texture_create(texture_format_send, texture_view);
right_eye_tex = render_device->texture_create(texture_format_send, texture_view);
}

void VulkanGlasses::SwapChainTextures::deallocate_textures() {
Expand Down Expand Up @@ -106,6 +121,46 @@ void VulkanGlasses::on_glasses_dropped() {
GodotT5Glasses::on_glasses_dropped();
deallocate_textures();
}


void VulkanGlasses::on_post_draw() {
auto service = T5Integration::ObjectRegistry::service();
if(service->get_graphics_api() == kT5_GraphicsApi_Vulkan)
{
RenderingServer *rendering_server = RenderingServer::get_singleton();
ERR_FAIL_NULL(rendering_server);
RenderingDevice *rendering_device = rendering_server->get_rendering_device();
ERR_FAIL_NULL(rendering_device);

auto& textures = _swap_chain_textures[get_current_frame_idx()];

auto size = get_render_size();

rendering_device->texture_copy(
textures.render_tex, // src
textures.left_eye_tex, // dest
Vector3(0,0,0), // src pos
Vector3(0,0,0), // dest pos
Vector3(size.x, size.y, 1), // size
0, // src mipmap
0, // dest mipmap
0, // src layer
0 // dest layer
);

rendering_device->texture_copy(
textures.render_tex, // src
textures.right_eye_tex, // dest
Vector3(0,0,0), // src pos
Vector3(0,0,0), // dest pos
Vector3(size.x, size.y, 1), // size
0, // src mipmap
0, // dest mipmap
1, // src layer
0 // dest layer
);
}
}

RID VulkanGlasses::get_color_texture()
{
Expand Down
14 changes: 9 additions & 5 deletions extension/src/VulkanGlasses.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,22 @@ namespace GodotT5Integration {
RID right_eye_tex;
};

public:

VulkanGlasses(std::string_view id);

virtual void on_post_draw() override;
virtual RID get_color_texture() override;

private:

void allocate_textures();
void deallocate_textures();

virtual void on_glasses_reserved() override;
virtual void on_glasses_released() override;
virtual void on_glasses_dropped() override;

public:

VulkanGlasses(std::string_view id);

virtual RID get_color_texture() override;

private:

Expand Down