Godot Engine – Multi-platform 2D and 3D game engine
Mimic Draws to Texture/Decal Atlas
Colin O'Rourkea182c0c41c0a
diff --git a/drivers/gles3/storage/texture_storage.cpp b/drivers/gles3/storage/texture_storage.cppindex b7999d4a574..b95f84e12e1 100644--- a/drivers/gles3/storage/texture_storage.cpp+++ b/drivers/gles3/storage/texture_storage.cpp@@ -1521,6 +1521,11 @@ void TextureStorage::texture_drawable_blit_rect(const TypedArray<RID> &p_textureglDrawArrays(GL_TRIANGLES, 0, 6);glBindVertexArray(0);+ i = 0;+ while (i < p_textures.size()) {+ texture_atlas_mark_draw_on_texture(p_textures[i]);+ i += 1;+ }// Reset to system FBOglBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);}@@ -2342,6 +2347,18 @@ void TextureStorage::texture_atlas_mark_dirty_on_texture(RID p_texture) {}}+void TextureStorage::texture_atlas_mark_draw_on_texture(RID p_texture) {+ if (texture_atlas.dirty) {+ return; // Don't mess with it while it's dirty anyway.+ }++ if (texture_atlas.textures.has(p_texture)) {+ TextureAtlas::Texture *t = texture_atlas.textures.getptr(p_texture);+ t->drawn = true;+ texture_atlas.draw_dirty = true;+ }+}+void TextureStorage::texture_atlas_remove_texture(RID p_texture) {if (texture_atlas.textures.has(p_texture)) {texture_atlas.textures.erase(p_texture);@@ -2524,6 +2541,40 @@ void TextureStorage::update_texture_atlas() {glActiveTexture(GL_TEXTURE0);glBindTexture(GL_TEXTURE_2D, src_tex->tex_id);copy_effects->copy_to_rect(t->uv_rect);+ t->drawn = false;+ }+ }+ glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);+}++void TextureStorage::texture_atlas_redraw_textures() {+ if (texture_atlas.dirty) {+ return; // Don't mess with it while it's dirty anyway.+ }++ if (!texture_atlas.draw_dirty) {+ return; // Nothing to do.+ }++ texture_atlas.draw_dirty = false;++ CopyEffects *copy_effects = CopyEffects::get_singleton();+ ERR_FAIL_NULL(copy_effects);+ ERR_FAIL_COND(texture_atlas.texture == 0);++ glBindFramebuffer(GL_FRAMEBUFFER, texture_atlas.framebuffer);+ glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture_atlas.texture, 0);+ glViewport(0, 0, texture_atlas.size.width, texture_atlas.size.height);++ glDisable(GL_BLEND);++ for (const KeyValue<RID, TextureAtlas::Texture> &E : texture_atlas.textures) {+ TextureAtlas::Texture *t = texture_atlas.textures.getptr(E.key);+ if (t->drawn) {+ Texture *src_tex = get_texture(E.key);+ glActiveTexture(GL_TEXTURE0);+ glBindTexture(GL_TEXTURE_2D, src_tex->tex_id);+ copy_effects->copy_to_rect(t->uv_rect);}}glBindFramebuffer(GL_FRAMEBUFFER, GLES3::TextureStorage::system_fbo);diff --git a/drivers/gles3/storage/texture_storage.h b/drivers/gles3/storage/texture_storage.hindex 74e58de0ee8..1e8a35c105c 100644--- a/drivers/gles3/storage/texture_storage.h+++ b/drivers/gles3/storage/texture_storage.h@@ -555,6 +555,7 @@ private:struct Texture {int users;Rect2 uv_rect;+ bool drawn = false;};struct SortItem {@@ -575,6 +576,7 @@ private:HashMap<RID, Texture> textures;bool dirty = true;+ bool draw_dirty = false;GLuint texture = 0;GLuint framebuffer = 0;@@ -732,6 +734,7 @@ public:/* TEXTURE ATLAS API */void update_texture_atlas();+ void texture_atlas_redraw_textures();GLuint texture_atlas_get_texture() const;_FORCE_INLINE_ Rect2 texture_atlas_get_texture_rect(RID p_texture) {@@ -746,6 +749,7 @@ public:void texture_add_to_texture_atlas(RID p_texture);void texture_remove_from_texture_atlas(RID p_texture);void texture_atlas_mark_dirty_on_texture(RID p_texture);+ void texture_atlas_mark_draw_on_texture(RID p_texture);void texture_atlas_remove_texture(RID p_texture);/* AREA LIGHT ATLAS API */diff --git a/drivers/gles3/storage/utilities.cpp b/drivers/gles3/storage/utilities.cppindex 29c6c390ef8..c03bc933388 100644--- a/drivers/gles3/storage/utilities.cpp+++ b/drivers/gles3/storage/utilities.cpp@@ -405,6 +405,7 @@ void Utilities::update_dirty_resources() {MaterialStorage::get_singleton()->_update_queued_materials();MeshStorage::get_singleton()->_update_dirty_skeletons();MeshStorage::get_singleton()->_update_dirty_multimeshes();+ TextureStorage::get_singleton()->texture_atlas_redraw_textures();TextureStorage::get_singleton()->update_texture_atlas();TextureStorage::get_singleton()->update_decal_atlas();}diff --git a/servers/rendering/renderer_rd/storage_rd/texture_storage.cpp b/servers/rendering/renderer_rd/storage_rd/texture_storage.cppindex da9500f386b..a7990a73841 100644--- a/servers/rendering/renderer_rd/storage_rd/texture_storage.cpp+++ b/servers/rendering/renderer_rd/storage_rd/texture_storage.cpp@@ -1873,9 +1873,15 @@ void TextureStorage::texture_drawable_blit_rect(const TypedArray<RID> &p_texture// DRAW!!RD::get_singleton()->draw_list_draw(draw_list, false, 1u, 6u);+ // Detect if any target is in Decal Atlas and also draw to Decal Atlas?RD::get_singleton()->draw_list_end();RD::get_singleton()->draw_command_end_label();+ i = 0;+ while (i < p_textures.size()) {+ decal_atlas_mark_draw_on_texture(p_textures[i]);+ i += 1;+ }}//these two APIs can be used together or in combination with the others.@@ -3859,6 +3865,20 @@ void TextureStorage::decal_atlas_mark_dirty_on_texture(RID p_texture) {}}+void TextureStorage::decal_atlas_mark_draw_on_texture(RID p_texture) {+ if (decal_atlas.dirty) {+ return; // Don't mess with it while it's dirty anyway.+ }++ if (decal_atlas.textures.has(p_texture)) {+ // Belongs to `decal_atlas`.+ DecalAtlas::Texture *t = decal_atlas.textures.getptr(p_texture);+ t->drawn = true;++ decal_atlas.draw_dirty = true;+ }+}+void TextureStorage::decal_atlas_remove_texture(RID p_texture) {if (decal_atlas.textures.has(p_texture)) {decal_atlas.textures.erase(p_texture);@@ -3866,6 +3886,48 @@ void TextureStorage::decal_atlas_remove_texture(RID p_texture) {}}+void TextureStorage::decal_atlas_redraw_textures() {+ if (decal_atlas.dirty) {+ return; // Don't mess with it while it's dirty anyway.+ }++ if (!decal_atlas.draw_dirty) {+ return; // Nothing to do.+ }++ decal_atlas.draw_dirty = false;++ CopyEffects *copy_effects = CopyEffects::get_singleton();+ ERR_FAIL_NULL(copy_effects);+ ERR_FAIL_COND(decal_atlas.texture.is_null());++ RID prev_texture;+ for (int i = 0; i < decal_atlas.texture_mipmaps.size(); i++) {+ const DecalAtlas::MipMap &mm = decal_atlas.texture_mipmaps[i];++ if (decal_atlas.textures.size()) {+ if (i == 0) {+ RD::DrawListID draw_list = RD::get_singleton()->draw_list_begin(mm.fb);+ for (const KeyValue<RID, DecalAtlas::Texture> &E : decal_atlas.textures) {+ DecalAtlas::Texture *t = decal_atlas.textures.getptr(E.key);+ if (t->drawn) {+ Texture *src_tex = get_texture(E.key);+ copy_effects->copy_to_atlas_fb(src_tex->rd_texture, mm.fb, t->uv_rect, draw_list, false, t->panorama_to_dp_users > 0);+ t->drawn = false;+ }+ }++ RD::get_singleton()->draw_list_end();++ prev_texture = mm.texture;+ } else {+ copy_effects->copy_to_fb_rect(prev_texture, mm.fb, Rect2i(Point2i(), mm.size));+ prev_texture = mm.texture;+ }+ }+ }+}+AABB TextureStorage::decal_get_aabb(RID p_decal) const {Decal *decal = decal_owner.get_or_null(p_decal);ERR_FAIL_NULL_V(decal, AABB());@@ -3896,6 +3958,7 @@ void TextureStorage::update_decal_atlas() {}decal_atlas.dirty = false;+ decal_atlas.draw_dirty = false;if (decal_atlas.texture.is_valid()) {RD::get_singleton()->free_rid(decal_atlas.texture);@@ -4068,6 +4131,7 @@ void TextureStorage::update_decal_atlas() {Texture *src_tex = get_texture(E.key);copy_effects->copy_to_atlas_fb(src_tex->rd_texture, mm.fb, t->uv_rect, draw_list, false, t->panorama_to_dp_users > 0);+ t->drawn = false;}RD::get_singleton()->draw_list_end();diff --git a/servers/rendering/renderer_rd/storage_rd/texture_storage.h b/servers/rendering/renderer_rd/storage_rd/texture_storage.hindex 0dd38d2324d..b6b74fa6698 100644--- a/servers/rendering/renderer_rd/storage_rd/texture_storage.h+++ b/servers/rendering/renderer_rd/storage_rd/texture_storage.h@@ -298,6 +298,7 @@ private:int panorama_to_dp_users;int users;Rect2 uv_rect;+ bool drawn = false;};struct SortItem {@@ -318,6 +319,7 @@ private:HashMap<RID, Texture> textures;bool dirty = true;+ bool draw_dirty = false;int mipmaps = 5;RID texture;@@ -737,7 +739,9 @@ public:virtual void decal_set_normal_fade(RID p_decal, float p_fade) override;void decal_atlas_mark_dirty_on_texture(RID p_texture);+ void decal_atlas_mark_draw_on_texture(RID p_texture);void decal_atlas_remove_texture(RID p_texture);+ void decal_atlas_redraw_textures();virtual void texture_add_to_decal_atlas(RID p_texture, bool p_panorama_to_dp = false) override;virtual void texture_remove_from_decal_atlas(RID p_texture, bool p_panorama_to_dp = false) override;diff --git a/servers/rendering/renderer_rd/storage_rd/utilities.cpp b/servers/rendering/renderer_rd/storage_rd/utilities.cppindex d6e37febaf0..217f53ffda1 100644--- a/servers/rendering/renderer_rd/storage_rd/utilities.cpp+++ b/servers/rendering/renderer_rd/storage_rd/utilities.cpp@@ -254,6 +254,7 @@ void Utilities::update_dirty_resources() {MaterialStorage::get_singleton()->_update_queued_materials();MeshStorage::get_singleton()->_update_dirty_multimeshes();MeshStorage::get_singleton()->_update_dirty_skeletons();+ TextureStorage::get_singleton()->decal_atlas_redraw_textures();TextureStorage::get_singleton()->update_decal_atlas();TextureStorage::get_singleton()->update_area_light_atlas();}