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

Enforce calling RenderingDevice code from rendering thread in TextureRD classes #87721

Merged
merged 1 commit into from
Feb 13, 2024
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
186 changes: 99 additions & 87 deletions scene/resources/texture_rd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,7 @@ void Texture2DRD::set_texture_rd_rid(RID p_texture_rd_rid) {
ERR_FAIL_NULL(RS::get_singleton());

if (p_texture_rd_rid.is_valid()) {
ERR_FAIL_NULL(RD::get_singleton());
ERR_FAIL_COND(!RD::get_singleton()->texture_is_valid(p_texture_rd_rid));

RD::TextureFormat tf = RD::get_singleton()->texture_get_format(p_texture_rd_rid);
ERR_FAIL_COND(tf.texture_type != RD::TEXTURE_TYPE_2D);
ERR_FAIL_COND(tf.depth > 1);
ERR_FAIL_COND(tf.array_layers > 1);

size.width = tf.width;
size.height = tf.height;

texture_rd_rid = p_texture_rd_rid;

if (texture_rid.is_valid()) {
RS::get_singleton()->texture_replace(texture_rid, RS::get_singleton()->texture_rd_create(p_texture_rd_rid));
} else {
texture_rid = RS::get_singleton()->texture_rd_create(p_texture_rd_rid);
}

notify_property_list_changed();
emit_changed();
RS::get_singleton()->call_on_render_thread(callable_mp(this, &Texture2DRD::_set_texture_rd_rid).bind(p_texture_rd_rid));
} else if (texture_rid.is_valid()) {
RS::get_singleton()->free(texture_rid);
texture_rid = RID();
Expand All @@ -105,6 +85,30 @@ void Texture2DRD::set_texture_rd_rid(RID p_texture_rd_rid) {
}
}

void Texture2DRD::_set_texture_rd_rid(RID p_texture_rd_rid) {
ERR_FAIL_NULL(RD::get_singleton());
ERR_FAIL_COND(!RD::get_singleton()->texture_is_valid(p_texture_rd_rid));

RD::TextureFormat tf = RD::get_singleton()->texture_get_format(p_texture_rd_rid);
ERR_FAIL_COND(tf.texture_type != RD::TEXTURE_TYPE_2D);
ERR_FAIL_COND(tf.depth > 1);
ERR_FAIL_COND(tf.array_layers > 1);

size.width = tf.width;
size.height = tf.height;

texture_rd_rid = p_texture_rd_rid;

if (texture_rid.is_valid()) {
RS::get_singleton()->texture_replace(texture_rid, RS::get_singleton()->texture_rd_create(p_texture_rd_rid));
} else {
texture_rid = RS::get_singleton()->texture_rd_create(p_texture_rd_rid);
}

notify_property_list_changed();
emit_changed();
}

RID Texture2DRD::get_texture_rd_rid() const {
return texture_rd_rid;
}
Expand Down Expand Up @@ -173,48 +177,7 @@ void TextureLayeredRD::set_texture_rd_rid(RID p_texture_rd_rid) {
ERR_FAIL_NULL(RS::get_singleton());

if (p_texture_rd_rid.is_valid()) {
ERR_FAIL_NULL(RD::get_singleton());
ERR_FAIL_COND(!RD::get_singleton()->texture_is_valid(p_texture_rd_rid));

RS::TextureLayeredType rs_layer_type;
RD::TextureFormat tf = RD::get_singleton()->texture_get_format(p_texture_rd_rid);
ERR_FAIL_COND(tf.texture_type != RD::TEXTURE_TYPE_2D_ARRAY);
ERR_FAIL_COND(tf.depth > 1);
switch (layer_type) {
case LAYERED_TYPE_2D_ARRAY: {
ERR_FAIL_COND(tf.array_layers <= 1);
rs_layer_type = RS::TEXTURE_LAYERED_2D_ARRAY;
} break;
case LAYERED_TYPE_CUBEMAP: {
ERR_FAIL_COND(tf.array_layers != 6);
rs_layer_type = RS::TEXTURE_LAYERED_CUBEMAP;
} break;
case LAYERED_TYPE_CUBEMAP_ARRAY: {
ERR_FAIL_COND((tf.array_layers == 0) || ((tf.array_layers % 6) != 0));
rs_layer_type = RS::TEXTURE_LAYERED_CUBEMAP_ARRAY;
} break;
default: {
ERR_FAIL_MSG("Unknown layer type selected");
} break;
}

size.width = tf.width;
size.height = tf.height;
layers = tf.array_layers;
mipmaps = tf.mipmaps;

texture_rd_rid = p_texture_rd_rid;

if (texture_rid.is_valid()) {
RS::get_singleton()->texture_replace(texture_rid, RS::get_singleton()->texture_rd_create(p_texture_rd_rid, rs_layer_type));
} else {
texture_rid = RS::get_singleton()->texture_rd_create(p_texture_rd_rid, rs_layer_type);
}

image_format = RS::get_singleton()->texture_get_format(texture_rid);

notify_property_list_changed();
emit_changed();
RS::get_singleton()->call_on_render_thread(callable_mp(this, &TextureLayeredRD::_set_texture_rd_rid).bind(p_texture_rd_rid));
} else if (texture_rid.is_valid()) {
RS::get_singleton()->free(texture_rid);
texture_rid = RID();
Expand All @@ -228,6 +191,51 @@ void TextureLayeredRD::set_texture_rd_rid(RID p_texture_rd_rid) {
}
}

void TextureLayeredRD::_set_texture_rd_rid(RID p_texture_rd_rid) {
ERR_FAIL_NULL(RD::get_singleton());
ERR_FAIL_COND(!RD::get_singleton()->texture_is_valid(p_texture_rd_rid));

RS::TextureLayeredType rs_layer_type;
RD::TextureFormat tf = RD::get_singleton()->texture_get_format(p_texture_rd_rid);
ERR_FAIL_COND(tf.texture_type != RD::TEXTURE_TYPE_2D_ARRAY);
ERR_FAIL_COND(tf.depth > 1);
switch (layer_type) {
case LAYERED_TYPE_2D_ARRAY: {
ERR_FAIL_COND(tf.array_layers <= 1);
rs_layer_type = RS::TEXTURE_LAYERED_2D_ARRAY;
} break;
case LAYERED_TYPE_CUBEMAP: {
ERR_FAIL_COND(tf.array_layers != 6);
rs_layer_type = RS::TEXTURE_LAYERED_CUBEMAP;
} break;
case LAYERED_TYPE_CUBEMAP_ARRAY: {
ERR_FAIL_COND((tf.array_layers == 0) || ((tf.array_layers % 6) != 0));
rs_layer_type = RS::TEXTURE_LAYERED_CUBEMAP_ARRAY;
} break;
default: {
ERR_FAIL_MSG("Unknown layer type selected");
} break;
}

size.width = tf.width;
size.height = tf.height;
layers = tf.array_layers;
mipmaps = tf.mipmaps;

texture_rd_rid = p_texture_rd_rid;

if (texture_rid.is_valid()) {
RS::get_singleton()->texture_replace(texture_rid, RS::get_singleton()->texture_rd_create(p_texture_rd_rid, rs_layer_type));
} else {
texture_rid = RS::get_singleton()->texture_rd_create(p_texture_rd_rid, rs_layer_type);
}

image_format = RS::get_singleton()->texture_get_format(texture_rid);

notify_property_list_changed();
emit_changed();
}

RID TextureLayeredRD::get_texture_rd_rid() const {
return texture_rd_rid;
}
Expand Down Expand Up @@ -291,30 +299,7 @@ void Texture3DRD::set_texture_rd_rid(RID p_texture_rd_rid) {
ERR_FAIL_NULL(RS::get_singleton());

if (p_texture_rd_rid.is_valid()) {
ERR_FAIL_NULL(RD::get_singleton());
ERR_FAIL_COND(!RD::get_singleton()->texture_is_valid(p_texture_rd_rid));

RD::TextureFormat tf = RD::get_singleton()->texture_get_format(p_texture_rd_rid);
ERR_FAIL_COND(tf.texture_type != RD::TEXTURE_TYPE_3D);
ERR_FAIL_COND(tf.array_layers > 1);

size.x = tf.width;
size.y = tf.height;
size.z = tf.depth;
mipmaps = tf.mipmaps;

texture_rd_rid = p_texture_rd_rid;

if (texture_rid.is_valid()) {
RS::get_singleton()->texture_replace(texture_rid, RS::get_singleton()->texture_rd_create(p_texture_rd_rid));
} else {
texture_rid = RS::get_singleton()->texture_rd_create(p_texture_rd_rid);
}

image_format = RS::get_singleton()->texture_get_format(texture_rid);

notify_property_list_changed();
emit_changed();
RS::get_singleton()->call_on_render_thread(callable_mp(this, &Texture3DRD::_set_texture_rd_rid).bind(p_texture_rd_rid));
} else if (texture_rid.is_valid()) {
RS::get_singleton()->free(texture_rid);
texture_rid = RID();
Expand All @@ -327,6 +312,33 @@ void Texture3DRD::set_texture_rd_rid(RID p_texture_rd_rid) {
}
}

void Texture3DRD::_set_texture_rd_rid(RID p_texture_rd_rid) {
ERR_FAIL_NULL(RD::get_singleton());
ERR_FAIL_COND(!RD::get_singleton()->texture_is_valid(p_texture_rd_rid));

RD::TextureFormat tf = RD::get_singleton()->texture_get_format(p_texture_rd_rid);
ERR_FAIL_COND(tf.texture_type != RD::TEXTURE_TYPE_3D);
ERR_FAIL_COND(tf.array_layers > 1);

size.x = tf.width;
size.y = tf.height;
size.z = tf.depth;
mipmaps = tf.mipmaps;

texture_rd_rid = p_texture_rd_rid;

if (texture_rid.is_valid()) {
RS::get_singleton()->texture_replace(texture_rid, RS::get_singleton()->texture_rd_create(p_texture_rd_rid));
} else {
texture_rid = RS::get_singleton()->texture_rd_create(p_texture_rd_rid);
}

image_format = RS::get_singleton()->texture_get_format(texture_rid);

notify_property_list_changed();
emit_changed();
}

RID Texture3DRD::get_texture_rd_rid() const {
return texture_rd_rid;
}
Expand Down
9 changes: 9 additions & 0 deletions scene/resources/texture_rd.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class Texture2DRD : public Texture2D {
void set_texture_rd_rid(RID p_texture_rd_rid);
RID get_texture_rd_rid() const;

// Internal function that should only be called from the rendering thread.
void _set_texture_rd_rid(RID p_texture_rd_rid);

Texture2DRD();
~Texture2DRD();
};
Expand Down Expand Up @@ -94,6 +97,9 @@ class TextureLayeredRD : public TextureLayered {
void set_texture_rd_rid(RID p_texture_rd_rid);
RID get_texture_rd_rid() const;

// Internal function that should only be called from the rendering thread.
void _set_texture_rd_rid(RID p_texture_rd_rid);

TextureLayeredRD(LayeredType p_layer_type);
~TextureLayeredRD();
};
Expand Down Expand Up @@ -146,6 +152,9 @@ class Texture3DRD : public Texture3D {
void set_texture_rd_rid(RID p_texture_rd_rid);
RID get_texture_rd_rid() const;

// Internal function that should only be called from the rendering thread.
void _set_texture_rd_rid(RID p_texture_rd_rid);

Texture3DRD();
~Texture3DRD();
};
Expand Down
Loading