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

Add support for BGRA textures with Texture*RD #98258

Merged
merged 1 commit into from
Oct 21, 2024

Conversation

LainAmongYou
Copy link
Contributor

@LainAmongYou LainAmongYou commented Oct 17, 2024

Description

Adds the ability to use BGRA textures created with RenderingDevice with classes such as Texture2DRD, which removes the need to unnecessarily convert from BGRA to RGBA.

Motivation and Context

I am creating a native GDExtension plugin for Godot that allows the ability to capture the screen, or specific windows, or possibly a game, as a texture, which is updated every frame. The over-arching goal is to give streamers the ability to composite their stream almost entirely in Godot if they so choose, allowing the ability for streamers to create some totally sick streamer setups.

However, after getting my plugin working, I noticed that BGRA textures created via RenderingDevice::texture_create() are not supported when you use them with Texture2DRD::set_texture_rd_rid(), even though they are supported by the device. This seems to be an unnecessary and arbitrary limitation due to the fact that Image::Format enum does not have BGRA.

This fix eliminates a completely unnecessary format conversion when one has a genuine need for BGRA texture support and needs to update textures frequently. Manually converting BGRA to RGBA every frame here would be an absolutely wasteful and pointless performance hit, especially when capturing 4K monitors which have a very large texture size, so I would preferably like to patch Godot to support BGRA so we don't have to do a conversion every frame.

var rd = RenderingServer.get_rendering_device()
	
var format = RDTextureFormat.new()
format.format = RenderingDevice.DATA_FORMAT_B8G8R8A8_UNORM
# [...]
	
var rd_rid = rd.texture_create(format, RDTextureView.new()) # Succeeds because BGRA is supported here
var tex = Texture2DRD.new()
tex.texture_rd_rid = rd_rid # Failed because BGRA was not supported here

How Has This Been Tested?

I tested this with capturing in real time and it works without issue. This should also not cause any sort of regression or affect anyone else because Godot did not originally allow this before this PR, thus no one's workflows should be affected by this.

This adds the ability to use BGRA textures created with RenderingDevice
with classes such as Texture2DRD.
@LainAmongYou LainAmongYou requested a review from a team as a code owner October 17, 2024 07:02
@bruvzg bruvzg requested review from a team and removed request for a team October 17, 2024 07:16
@Chaosus Chaosus added this to the 4.4 milestone Oct 17, 2024
Copy link
Member

@clayjohn clayjohn left a comment

Choose a reason for hiding this comment

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

Makes sense to me!

@Repiteo Repiteo merged commit 178342b into godotengine:master Oct 21, 2024
20 checks passed
@Repiteo
Copy link
Contributor

Repiteo commented Oct 21, 2024

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants