diff --git a/src/CRE/render.cpp b/src/CRE/render.cpp index b9dd7f8c..054bbe99 100644 --- a/src/CRE/render.cpp +++ b/src/CRE/render.cpp @@ -923,8 +923,10 @@ namespace rndr { dof = new renderer::DOF3(render_post_width[0], render_post_height[0]); - transparency_tex[0] = texture_load_tex_2d(texture_id(0x25, texture_counter++), - GL_RGBA16F, render_post_width[0], render_post_height[0], 0, 0, 0); + for (int32_t i = 0; i < 1; i++) + transparency_tex[i] = texture_load_tex_2d(texture_id(0x25, texture_counter++), + GL_RGBA16F, render_post_width[0], render_post_height[0], 0, 0, 0); + transparency = new renderer::Transparency(transparency_tex[0]->tex, rend_texture[0].GetDepthTex(), render_post_width[0], render_post_height[0]); } @@ -1105,15 +1107,17 @@ namespace rndr { glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzle); for (int32_t i = 0; i < 3; i++) { + texture_id id = taa_tex[i]->id; texture_free(taa_tex[i]); - taa_tex[i] = texture_load_tex_2d(texture_id(0x25, texture_counter++), - GL_RGBA8, render_post_width[0], render_post_height[0], 0, 0, 0); + taa_tex[i] = texture_load_tex_2d(id, GL_RGBA8, + render_post_width[0], render_post_height[0], 0, 0, 0); taa_buffer[i].SetColorDepthTextures(taa_tex[i]->tex, 0, rend_texture[0].GetDepthTex()); } + texture_id aet_back_tex_id = aet_back_tex->id; texture_free(aet_back_tex); - aet_back_tex = texture_load_tex_2d(texture_id(0x25, texture_counter++), - GL_RGBA8, render_post_width[0], render_post_height[0], 0, 0, 0); + aet_back_tex = texture_load_tex_2d(aet_back_tex_id, GL_RGBA8, + render_post_width[0], render_post_height[0], 0, 0, 0); aet_back_texture.SetColorDepthTextures(aet_back_tex->tex, 0, rend_texture[0].GetDepthTex()); mlaa_buffer.Init(render_post_width[0], render_post_height[0], 0, GL_RGBA8, GL_DEPTH_COMPONENT24); @@ -1132,15 +1136,13 @@ namespace rndr { dof->resize(render_post_width[0], render_post_height[0]); - for (int32_t i = 0; i < 1; i++) - if (transparency_tex[i]) { - texture_free(transparency_tex[i]); - transparency_tex[i] = 0; - texture_counter--; - } + for (int32_t i = 0; i < 1; i++) { + texture_id id = transparency_tex[i]->id; + texture_free(transparency_tex[i]); + transparency_tex[i] = texture_load_tex_2d(id, GL_RGBA16F, + render_post_width[0], render_post_height[0], 0, 0, 0); + } - transparency_tex[0] = texture_load_tex_2d(texture_id(0x25, texture_counter++), - GL_RGBA16F, render_post_width[0], render_post_height[0], 0, 0, 0); transparency->resize(transparency_tex[0]->tex, rend_texture[0].GetDepthTex(), render_post_width[0], render_post_height[0]); } diff --git a/src/CRE/renderer/fbo.cpp b/src/CRE/renderer/fbo.cpp index 11c4b9eb..33a25778 100644 --- a/src/CRE/renderer/fbo.cpp +++ b/src/CRE/renderer/fbo.cpp @@ -41,7 +41,7 @@ namespace renderer { textures[count] = depth_texture; glGenFramebuffers(1, &buffer); - glBindFramebuffer(GL_FRAMEBUFFER, buffer); + gl_state_bind_framebuffer(buffer, true); for (int32_t i = 0; i < count; i++) glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); @@ -53,6 +53,7 @@ namespace renderer { color_attachments[i] = GL_COLOR_ATTACHMENT0 + i; glDrawBuffers(count, color_attachments); glCheckFramebufferStatus(GL_FRAMEBUFFER); + gl_state_bind_framebuffer(0); } #pragma warning(pop) diff --git a/src/CRE/texture.cpp b/src/CRE/texture.cpp index f3f60f29..f5926bc0 100644 --- a/src/CRE/texture.cpp +++ b/src/CRE/texture.cpp @@ -25,7 +25,7 @@ static texture* texture_load_tex(texture_id id, GLenum target, int32_t max_mipmap_level, void** data_ptr, bool use_high_anisotropy); static GLenum texture_txp_get_gl_internal_format(txp* t); -std::vector texture_storage; +std::map texture_storage; texture::texture() : init_count(), flags(), width(), height(), tex(), target(), internal_format(), max_mipmap_level(), size() { @@ -366,72 +366,59 @@ bool texture_txp_set_load(txp_set* t, texture*** texs, texture_id* ids) { return true; } -inline void texture_storage_init() { +void texture_storage_init() { texture_storage = {}; } -inline texture* texture_storage_create_texture(texture_id id) { - for (texture*& i : texture_storage) - if (i && i->id == id) { - i->init_count++; - return i; - } +texture* texture_storage_create_texture(texture_id id) { + auto elem = texture_storage.find(id); + if (elem != texture_storage.end()) { + elem->second.init_count++; + return &elem->second; + } - texture* tex = new texture(); - tex->init_count = 1; - tex->id = id; - texture_storage.push_back(tex); - return tex; + texture tex; + tex.init_count = 1; + tex.id = id; + return &texture_storage.insert({ id, tex }).first->second; } -inline texture* texture_storage_get_texture(uint32_t id) { - for (texture*& i : texture_storage) - if (i && i->id.index == id) - return i; +texture* texture_storage_get_texture(uint32_t id) { + auto elem = texture_storage.find(texture_id(0x00, id)); + if (elem != texture_storage.end()) + return &elem->second; return 0; } -inline texture* texture_storage_get_texture(texture_id id) { - for (texture*& i : texture_storage) - if (i && i->id == id) - return i; +texture* texture_storage_get_texture(texture_id id) { + auto elem = texture_storage.find(id); + if (elem != texture_storage.end()) + return &elem->second; return 0; } -inline size_t texture_storage_get_texture_count() { +size_t texture_storage_get_texture_count() { return texture_storage.size(); } -inline texture* texture_storage_get_texture_by_index(size_t index) { - if (index >= 0 && index < texture_storage.size()) - return texture_storage[index]; - return 0; -} +void texture_storage_delete_texture(texture_id id) { + auto elem = texture_storage.find(id); + if (elem == texture_storage.end()) + return; -inline void texture_storage_delete_texture(texture_id id) { - for (std::vector::iterator i = texture_storage.begin(); i != texture_storage.end(); i++) - if (*i && (*i)->id == id) { - texture* tex = *i; - if (tex->init_count > 1) { - tex->init_count--; - break; - } - - glDeleteTextures(1, &tex->tex); - delete tex; - i = texture_storage.erase(i); - break; - } -} - -inline void texture_storage_free() { - for (texture*& i : texture_storage) { - if (i) - glDeleteTextures(1, &i->tex); - delete i; + texture* tex = &elem->second; + if (tex->init_count > 1) { + tex->init_count--; + return; } + + texture_storage.erase(elem); +} + +void texture_storage_free() { + for (auto& i : texture_storage) + glDeleteTextures(1, &i.second.tex); texture_storage.clear(); - texture_storage.shrink_to_fit(); } inline static void texture_bind(GLenum target, GLuint texture) { diff --git a/src/CRE/texture.hpp b/src/CRE/texture.hpp index a16bb7e6..1e1105fd 100644 --- a/src/CRE/texture.hpp +++ b/src/CRE/texture.hpp @@ -31,27 +31,27 @@ struct texture_id { }; inline bool operator >(const texture_id& left, const texture_id& right) { - return left.id > right.id && left.index > right.index; + return (((uint64_t)left.id << 32) | left.index) > (((uint64_t)right.id << 32) | right.index); } inline bool operator <(const texture_id& left, const texture_id& right) { - return left.id < right.id && left.index < right.index; + return (((uint64_t)left.id << 32) | left.index) < (((uint64_t)right.id << 32) | right.index); } inline bool operator >=(const texture_id& left, const texture_id& right) { - return left.id >= right.id && left.index >= right.index; + return (((uint64_t)left.id << 32) | left.index) >= (((uint64_t)right.id << 32) | right.index); } inline bool operator <=(const texture_id& left, const texture_id& right) { - return left.id <= right.id && left.index <= right.index; + return (((uint64_t)left.id << 32) | left.index) <= (((uint64_t)right.id << 32) | right.index); } inline bool operator ==(const texture_id& left, const texture_id& right) { - return left.id == right.id && left.index == right.index; + return (((uint64_t)left.id << 32) | left.index) == (((uint64_t)right.id << 32) | right.index); } inline bool operator !=(const texture_id& left, const texture_id& right) { - return left.id != right.id || left.index != right.index; + return (((uint64_t)left.id << 32) | left.index) != (((uint64_t)right.id << 32) | right.index); } struct texture { @@ -106,6 +106,5 @@ extern texture* texture_storage_create_texture(texture_id id); extern texture* texture_storage_get_texture(uint32_t id); extern texture* texture_storage_get_texture(texture_id id); extern size_t texture_storage_get_texture_count(); -extern texture* texture_storage_get_texture_by_index(size_t index); extern void texture_storage_delete_texture(texture_id id); extern void texture_storage_free(); diff --git a/src/ReDIVA/classes/data_view/texture.cpp b/src/ReDIVA/classes/data_view/texture.cpp index b27f0c79..cf0653c0 100644 --- a/src/ReDIVA/classes/data_view/texture.cpp +++ b/src/ReDIVA/classes/data_view/texture.cpp @@ -47,9 +47,9 @@ void data_view_texture_imgui(class_data* data) { return; } - size_t count = texture_storage_get_texture_count(); - for (size_t i = 0; i < count; i++) { - texture* tex = texture_storage_get_texture_by_index(i); + extern std::map texture_storage; + for (auto& i : texture_storage) { + texture* tex = &i.second; if (tex->id.id) continue;