Skip to content

Commit

Permalink
Merge pull request godotengine#79874 from clayjohn/PlaceholderTexture…
Browse files Browse the repository at this point in the history
…-rid

Lazily allocate RIDs for PlaceholderTextures to avoid allocating GPU resources unless used
  • Loading branch information
YuriSizov committed Aug 1, 2023
2 parents 77785c3 + d61d498 commit 8965e24
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
32 changes: 26 additions & 6 deletions scene/resources/placeholder_textures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ Ref<Image> PlaceholderTexture2D::get_image() const {
}

RID PlaceholderTexture2D::get_rid() const {
if (rid.is_null()) {
rid = RenderingServer::get_singleton()->texture_2d_placeholder_create();
}
return rid;
}

Expand All @@ -61,12 +64,13 @@ void PlaceholderTexture2D::_bind_methods() {
}

PlaceholderTexture2D::PlaceholderTexture2D() {
rid = RS::get_singleton()->texture_2d_placeholder_create();
}

PlaceholderTexture2D::~PlaceholderTexture2D() {
ERR_FAIL_NULL(RenderingServer::get_singleton());
RS::get_singleton()->free(rid);
if (rid.is_valid()) {
RS::get_singleton()->free(rid);
}
}

///////////////////////////////////////////////
Expand Down Expand Up @@ -103,18 +107,26 @@ Vector<Ref<Image>> PlaceholderTexture3D::get_data() const {
return Vector<Ref<Image>>();
}

RID PlaceholderTexture3D::get_rid() const {
if (rid.is_null()) {
rid = RenderingServer::get_singleton()->texture_3d_placeholder_create();
}
return rid;
}

void PlaceholderTexture3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_size", "size"), &PlaceholderTexture3D::set_size);
ClassDB::bind_method(D_METHOD("get_size"), &PlaceholderTexture3D::get_size);
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3I, "size", PROPERTY_HINT_NONE, "suffix:px"), "set_size", "get_size");
}

PlaceholderTexture3D::PlaceholderTexture3D() {
rid = RS::get_singleton()->texture_3d_placeholder_create();
}
PlaceholderTexture3D::~PlaceholderTexture3D() {
ERR_FAIL_NULL(RenderingServer::get_singleton());
RS::get_singleton()->free(rid);
if (rid.is_valid()) {
RS::get_singleton()->free(rid);
}
}

/////////////////////////////////////////////////
Expand Down Expand Up @@ -159,6 +171,13 @@ Ref<Image> PlaceholderTextureLayered::get_layer_data(int p_layer) const {
return Ref<Image>();
}

RID PlaceholderTextureLayered::get_rid() const {
if (rid.is_null()) {
rid = RS::get_singleton()->texture_2d_layered_placeholder_create(RS::TextureLayeredType(layered_type));
}
return rid;
}

void PlaceholderTextureLayered::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_size", "size"), &PlaceholderTextureLayered::set_size);
ClassDB::bind_method(D_METHOD("get_size"), &PlaceholderTextureLayered::get_size);
Expand All @@ -169,9 +188,10 @@ void PlaceholderTextureLayered::_bind_methods() {

PlaceholderTextureLayered::PlaceholderTextureLayered(LayeredType p_type) {
layered_type = p_type;
rid = RS::get_singleton()->texture_2d_layered_placeholder_create(RS::TextureLayeredType(layered_type));
}
PlaceholderTextureLayered::~PlaceholderTextureLayered() {
ERR_FAIL_NULL(RenderingServer::get_singleton());
RS::get_singleton()->free(rid);
if (rid.is_valid()) {
RS::get_singleton()->free(rid);
}
}
8 changes: 5 additions & 3 deletions scene/resources/placeholder_textures.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
class PlaceholderTexture2D : public Texture2D {
GDCLASS(PlaceholderTexture2D, Texture2D)

RID rid;
mutable RID rid;
Size2 size = Size2(1, 1);

protected:
Expand All @@ -59,7 +59,7 @@ class PlaceholderTexture2D : public Texture2D {
class PlaceholderTexture3D : public Texture3D {
GDCLASS(PlaceholderTexture3D, Texture3D)

RID rid;
mutable RID rid;
Vector3i size = Vector3i(1, 1, 1);

protected:
Expand All @@ -74,6 +74,7 @@ class PlaceholderTexture3D : public Texture3D {
virtual int get_depth() const override;
virtual bool has_mipmaps() const override;
virtual Vector<Ref<Image>> get_data() const override;
virtual RID get_rid() const override;

PlaceholderTexture3D();
~PlaceholderTexture3D();
Expand All @@ -82,7 +83,7 @@ class PlaceholderTexture3D : public Texture3D {
class PlaceholderTextureLayered : public TextureLayered {
GDCLASS(PlaceholderTextureLayered, TextureLayered)

RID rid;
mutable RID rid;
Size2i size = Size2i(1, 1);
int layers = 1;
LayeredType layered_type = LAYERED_TYPE_2D_ARRAY;
Expand All @@ -101,6 +102,7 @@ class PlaceholderTextureLayered : public TextureLayered {
virtual int get_layers() const override;
virtual bool has_mipmaps() const override;
virtual Ref<Image> get_layer_data(int p_layer) const override;
virtual RID get_rid() const override;

PlaceholderTextureLayered(LayeredType p_type);
~PlaceholderTextureLayered();
Expand Down

0 comments on commit 8965e24

Please sign in to comment.