Skip to content

Commit 7d93119

Browse files
committed
Renderer: Eliminates String allocations for all labels in the renderer
Uses `Span<char>` to avoid additional allocations in the graph.
1 parent 6c9765d commit 7d93119

File tree

10 files changed

+30
-19
lines changed

10 files changed

+30
-19
lines changed

drivers/metal/rendering_context_driver_metal.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ class PixelFormats;
5656
class MDResourceCache;
5757

5858
class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) RenderingContextDriverMetal : public RenderingContextDriver {
59+
bool capture_available = false;
60+
5961
protected:
6062
#ifdef __OBJC__
6163
id<MTLDevice> metal_device = nullptr;
@@ -80,7 +82,7 @@ class API_AVAILABLE(macos(11.0), ios(14.0), tvos(14.0)) RenderingContextDriverMe
8082
void surface_set_needs_resize(SurfaceID p_surface, bool p_needs_resize) final override;
8183
bool surface_get_needs_resize(SurfaceID p_surface) const final override;
8284
void surface_destroy(SurfaceID p_surface) final override;
83-
bool is_debug_utils_enabled() const final override { return true; }
85+
bool is_debug_utils_enabled() const final override { return capture_available; }
8486

8587
#pragma mark - Metal-specific methods
8688

drivers/metal/rendering_context_driver_metal.mm

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ - (void)setShouldMaximizeConcurrentCompilation:(BOOL)v;
4545
}
4646

4747
Error RenderingContextDriverMetal::initialize() {
48+
if (OS::get_singleton()->get_environment(U"METAL_DEVICE_WRAPPER_TYPE") == "1") {
49+
capture_available = true;
50+
}
51+
4852
metal_device = MTLCreateSystemDefaultDevice();
4953
#if TARGET_OS_OSX
5054
if (@available(macOS 13.3, *)) {

servers/rendering/renderer_rd/effects/fsr.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,11 @@
3838
namespace RendererRD {
3939

4040
class FSR : public SpatialUpscaler {
41-
String name = "FSR 1.0 Upscale";
42-
4341
public:
4442
FSR();
4543
~FSR();
4644

47-
virtual String get_label() const final { return name; }
45+
virtual const Span<char> get_label() const final { return "FSR 1.0 Upscale"; }
4846
virtual void ensure_context(Ref<RenderSceneBuffersRD> p_render_buffers) final {}
4947
virtual void process(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_source_rd_texture, RID p_destination_texture) final;
5048

servers/rendering/renderer_rd/effects/metal_fx.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,9 @@ class MFXSpatialEffect : public SpatialUpscaler {
7676

7777
PagedAllocator<CallbackArgs, true, 16> args_allocator;
7878
static void callback(RDD *p_driver, RDD::CommandBufferID p_command_buffer, CallbackArgs *p_userdata);
79-
String name = "MetalFX Spatial Upscale";
8079

8180
public:
82-
virtual String get_label() const final { return name; }
81+
virtual const Span<char> get_label() const final { return "MetalFX Spatial Upscale"; }
8382
virtual void ensure_context(Ref<RenderSceneBuffersRD> p_render_buffers) final;
8483
virtual void process(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_src, RID p_dst) final;
8584

servers/rendering/renderer_rd/effects/spatial_upscaler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class RenderSceneBuffersRD;
3636

3737
class SpatialUpscaler {
3838
public:
39-
virtual String get_label() const = 0;
39+
virtual const Span<char> get_label() const = 0;
4040
virtual void ensure_context(Ref<RenderSceneBuffersRD> p_render_buffers) = 0;
4141
virtual void process(Ref<RenderSceneBuffersRD> p_render_buffers, RID p_source_rd_texture, RID p_destination_texture) = 0;
4242

servers/rendering/renderer_rd/effects/ss_effects.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ void SSEffects::gather_ssao(RD::ComputeListID p_compute_list, const RID *p_ao_sl
985985
continue;
986986
}
987987

988-
RD::Uniform u_ao_slice(RD::UNIFORM_TYPE_IMAGE, 0, Vector<RID>({ p_ao_slices[i] }));
988+
RD::Uniform u_ao_slice(RD::UNIFORM_TYPE_IMAGE, 0, p_ao_slices[i]);
989989

990990
ssao.gather_push_constant.pass_coord_offset[0] = i % 2;
991991
ssao.gather_push_constant.pass_coord_offset[1] = i / 2;
@@ -1419,7 +1419,11 @@ void SSEffects::screen_space_reflection(Ref<RenderSceneBuffersRD> p_render_buffe
14191419
blur_radius[1] = p_render_buffers->get_texture_slice(RB_SCOPE_SSR, RB_BLUR_RADIUS, 1, 0);
14201420
}
14211421

1422-
RD::get_singleton()->draw_command_begin_label(String("SSR View ") + itos(v));
1422+
{
1423+
char label[16];
1424+
int len = snprintf(label, sizeof(label), "SSR View %d", v);
1425+
RD::get_singleton()->draw_command_begin_label(Span<char>(label, len));
1426+
}
14231427

14241428
{ //scale color and depth to half
14251429
RD::get_singleton()->draw_command_begin_label("SSR Scale");

servers/rendering/rendering_device.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6200,7 +6200,11 @@ void RenderingDevice::set_resource_name(RID p_id, const String &p_name) {
62006200
#endif
62016201
}
62026202

6203-
void RenderingDevice::draw_command_begin_label(String p_label_name, const Color &p_color) {
6203+
void RenderingDevice::_draw_command_begin_label(String p_label_name, const Color &p_color) {
6204+
draw_command_begin_label(p_label_name.utf8().span(), p_color);
6205+
}
6206+
6207+
void RenderingDevice::draw_command_begin_label(const Span<char> p_label_name, const Color &p_color) {
62046208
ERR_RENDER_THREAD_GUARD();
62056209

62066210
if (!context->is_debug_utils_enabled()) {
@@ -7450,7 +7454,7 @@ void RenderingDevice::_bind_methods() {
74507454

74517455
ClassDB::bind_method(D_METHOD("set_resource_name", "id", "name"), &RenderingDevice::set_resource_name);
74527456

7453-
ClassDB::bind_method(D_METHOD("draw_command_begin_label", "name", "color"), &RenderingDevice::draw_command_begin_label);
7457+
ClassDB::bind_method(D_METHOD("draw_command_begin_label", "name", "color"), &RenderingDevice::_draw_command_begin_label);
74547458
#ifndef DISABLE_DEPRECATED
74557459
ClassDB::bind_method(D_METHOD("draw_command_insert_label", "name", "color"), &RenderingDevice::draw_command_insert_label);
74567460
#endif

servers/rendering/rendering_device.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1643,7 +1643,8 @@ class RenderingDevice : public RenderingDeviceCommons {
16431643

16441644
void set_resource_name(RID p_id, const String &p_name);
16451645

1646-
void draw_command_begin_label(String p_label_name, const Color &p_color = Color(1, 1, 1, 1));
1646+
void _draw_command_begin_label(String p_label_name, const Color &p_color = Color(1, 1, 1, 1));
1647+
void draw_command_begin_label(const Span<char> p_label_name, const Color &p_color = Color(1, 1, 1, 1));
16471648
void draw_command_end_label();
16481649

16491650
String get_device_vendor_name() const;

servers/rendering/rendering_device_graph.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2218,13 +2218,12 @@ void RenderingDeviceGraph::add_synchronization() {
22182218
}
22192219
}
22202220

2221-
void RenderingDeviceGraph::begin_label(const String &p_label_name, const Color &p_color) {
2221+
void RenderingDeviceGraph::begin_label(const Span<char> &p_label_name, const Color &p_color) {
22222222
uint32_t command_label_offset = command_label_chars.size();
2223-
PackedByteArray command_label_utf8 = p_label_name.to_utf8_buffer();
2224-
int command_label_utf8_size = command_label_utf8.size();
2225-
command_label_chars.resize(command_label_offset + command_label_utf8_size + 1);
2226-
memcpy(&command_label_chars[command_label_offset], command_label_utf8.ptr(), command_label_utf8.size());
2227-
command_label_chars[command_label_offset + command_label_utf8_size] = '\0';
2223+
int command_label_size = p_label_name.size();
2224+
command_label_chars.resize(command_label_offset + command_label_size + 1);
2225+
memcpy(&command_label_chars[command_label_offset], p_label_name.ptr(), command_label_size);
2226+
command_label_chars[command_label_offset + command_label_size] = '\0';
22282227
command_label_colors.push_back(p_color);
22292228
command_label_offsets.push_back(command_label_offset);
22302229
command_label_index = command_label_count;

servers/rendering/rendering_device_graph.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ class RenderingDeviceGraph {
816816
void add_texture_update(RDD::TextureID p_dst, ResourceTracker *p_dst_tracker, VectorView<RecordedBufferToTextureCopy> p_buffer_copies, VectorView<ResourceTracker *> p_buffer_trackers = VectorView<ResourceTracker *>());
817817
void add_capture_timestamp(RDD::QueryPoolID p_query_pool, uint32_t p_index);
818818
void add_synchronization();
819-
void begin_label(const String &p_label_name, const Color &p_color);
819+
void begin_label(const Span<char> &p_label_name, const Color &p_color);
820820
void end_label();
821821
void end(bool p_reorder_commands, bool p_full_barriers, RDD::CommandBufferID &r_command_buffer, CommandBufferPool &r_command_buffer_pool);
822822
static ResourceTracker *resource_tracker_create();

0 commit comments

Comments
 (0)