{"id":341435,"date":"2022-11-18T15:00:53","date_gmt":"2022-11-18T15:00:53","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=341435"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=341435","title":{"rendered":"<span>\u041c\u0443\u043b\u044c\u0442\u0438-\u043f\u043e\u0442\u043e\u0447\u043d\u0430\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438 \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0430 \u0442\u0435\u043a\u0441\u0442\u0443\u0440 \u0432 OpenGL C++<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u042f \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u0443\u0433\u043e\u0434\u0430 \u0441\u043e\u0431\u0438\u0440\u0430\u043b \u043f\u043e \u043a\u0440\u0443\u043f\u0438\u0446\u0430\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0442\u043e\u043c \u043a\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0435 \u0442\u043e\u0440\u043c\u043e\u0437\u044f\u0449\u0443\u044e \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0442\u0440\u0435\u0434 \u0440\u0435\u043d\u0434\u0435\u0440\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u0442\u0435\u043a\u0441\u0442\u0443\u0440. \u0410\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0430\u044f \u0444\u0438\u0447\u0430 \u0434\u043b\u044f \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432, \u0433\u0434\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043d\u0430 \u043b\u0435\u0442\u0443 \u043f\u043e\u0434\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0438 \u0432\u044b\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0442\u0435\u043a\u0441\u0442\u0443\u0440\u044b, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0438 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0433\u043e \u043c\u0438\u0440\u0430. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0440\u0435\u0446\u0435\u043f\u0442&#8230;<\/p>\n<p>\u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u0447\u0435\u0440\u0435\u0437 <strong>stb_image<\/strong>.<\/p>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u043c\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0441 \u043c\u0443\u043b\u044c\u0442\u0438-\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c\u044e, \u043f\u0440\u0435\u0436\u0434\u0435 \u0432\u0441\u0435\u0433\u043e \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u0430\u044f (\u0434\u043b\u044f \u043c\u0443\u043b\u044c\u0442\u0438-\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438) \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u044f \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e \u0431\u0435\u0437 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0435\u0432:<\/p>\n<pre><code class=\"cpp\">#pragma once  #include &lt;queue> #include &lt;mutex> #include &lt;condition_variable> #include &lt;optional>  template&lt;class T> class SafeQueue { public: SafeQueue(void) : q(), m(), c() {}  ~SafeQueue(void) {}  void enqueue(T t) { std::lock_guard&lt;std::mutex> lock(m); q.push(t); c.notify_one(); }  T dequeue(void) { std::unique_lock&lt;std::mutex> lock(m); while (q.empty()) { c.wait(lock); } T val = q.front(); q.pop(); return val; }  std::optional&lt;T> pop(void) { std::unique_lock&lt;std::mutex> lock(m); if (q.empty()) { return {}; } T val = q.front(); q.pop(); return val; }  int size() { std::lock_guard&lt;std::mutex> lock(m); return q.size(); }  private: std::queue&lt;T> q; mutable std::mutex m; std::condition_variable c; };<\/code><\/pre>\n<p>\u0421\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 <strong>LoadFromFile<\/strong> \u043a\u043b\u0430\u0441\u0441\u0430 <strong>Texture<\/strong> \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u0437\u0430 \u0432\u044b\u0434\u0430\u0447\u0443 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432 \u0442\u0435\u043a\u0441\u0442\u0443\u0440 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432\u0438\u0434\u0435\u043e\u043a\u0430\u0440\u0442\u044b \u0432 \u043e\u0431\u043c\u0435\u043d \u043d\u0430 <strong>path<\/strong> \u043a \u0442\u0435\u043a\u0441\u0442\u0443\u0440\u0435 \u0438 \u0434\u0432\u0430 \u0444\u043b\u0430\u0433\u0430: <strong>srgb<\/strong> \u0438 <strong>force_uncompressed<\/strong>. \u0412\u044b\u0434\u0430\u043d\u043d\u044b\u0439 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0440\u0430\u0437\u0443, \u043f\u043e\u043a\u0430 \u0442\u0435\u043a\u0441\u0442\u0443\u0440\u0430 \u043d\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u0430 \u0432\u0438\u0437\u0443\u0430\u043b\u044c\u043d\u043e \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043a\u0430\u043a \u0447\u0451\u0440\u043d\u044b\u0439 \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a.<\/p>\n<pre><code class=\"cpp\">#pragma once  #include &lt;string> #include &lt;vector> #include &lt;map> #include &lt;set> #include \"SafeQueue.hpp\"  class Texture { public: struct UploadData { unsigned int gl_id{0}; std::string path{\"\"}; bool srgb{true}; bool force_uncompressed{false}; unsigned char* data; unsigned int width; unsigned int height; unsigned int nrComponents; };  unsigned int id; std::string type;  static std::recursive_mutex mutex; static SafeQueue&lt;UploadData> QueueToLoad; static std::map&lt;std::string, unsigned int> path2id; static std::map&lt;unsigned int, std::string> id2path; static std::map&lt;unsigned int, bool> loaded_state; static std::set&lt;unsigned int> need_to_unload_after_unmap;  static unsigned int LoadFromFile(const std::string &amp;path, bool srgb = true, bool force_uncompressed = false);  static unsigned int LoadCubemap(std::vector&lt;std::string> faces); [[noreturn]] static void ProcessQueueToLoad(); static void Unload(unsigned int gl_id); };<\/code><\/pre>\n<p><strong>mutex<\/strong> &#8212; \u043c\u044e\u0442\u0435\u043a\u0441 \u0434\u043b\u044f \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0442\u0435\u043a\u0441\u0442\u0443\u0440,<\/p>\n<p><strong>UploadData<\/strong> &#8212; \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0442\u0435\u043a\u0441\u0442\u0443\u0440\u044b \u0432 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438,<\/p>\n<p><strong>QueueToLoad<\/strong> &#8212; \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u044d\u0442\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, <\/p>\n<p><strong>path2id<\/strong> &#8212; \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043f\u0443\u0442\u0435\u0439 \u043a \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430\u043c \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432\u0438\u0434\u0435\u043e\u043a\u0430\u0440\u0442\u044b, <\/p>\n<p><strong>id2path<\/strong> &#8212; \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043e\u0431\u0440\u0430\u0442\u043d\u0430\u044f \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439,<\/p>\n<p><strong>loaded_state<\/strong> &#8212; \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0442\u0435\u043a\u0441\u0442\u0443\u0440 (false &#8212; \u0435\u0449\u0435 \u0433\u0440\u0443\u0437\u0438\u0442\u0441\u044f, true &#8212; \u0443\u0436\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u0430),<\/p>\n<p><strong>need_to_unload_after_unmap<\/strong> &#8212; \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432 \u0442\u0435\u043a\u0441\u0442\u0443\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u043e\u0441\u044c \u0432\u044b\u0433\u0440\u0443\u0437\u0438\u0442\u044c, \u043d\u043e \u043f\u043e\u043a\u0430 \u0447\u0442\u043e \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0442\u0430\u043a \u043a\u0430\u043a \u0442\u0435\u043a\u0441\u0442\u0443\u0440\u0430 \u0432\u0441\u0435 \u0435\u0449\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442\u0441\u044f.<\/p>\n<pre><code class=\"cpp\">#include \"Texture.hpp\" #include \"Exception.hpp\" #include &lt;glad\/glad.h> #include &lt;stb_image\/stb_image.h> #include &lt;stb_image\/stb_image_resize.h> #include &lt;iostream> #include &lt;algorithm> #include &lt;filesystem> #include &lt;thread> #include \"Engine.hpp\"  std::recursive_mutex Texture::mutex; SafeQueue&lt;Texture::UploadData> Texture::QueueToLoad; std::map&lt;std::string, unsigned int> Texture::path2id; std::map&lt;unsigned int, std::string> Texture::id2path; std::map&lt;unsigned int, bool> Texture::loaded_state; std::set&lt;unsigned int> Texture::need_to_unload_after_unmap;  bool IsPowerOfTwo(int x) { return (x != 0) &amp;&amp; ((x &amp; (x - 1)) == 0); }  int closest(std::vector&lt;int> const &amp;vec, int value) { for (auto i = vec.rbegin(); i != vec.rend(); ++i) if ((*i) &lt;= value) return (*i); return 1; } <\/code><\/pre>\n<p>\u0412 \u043d\u0430\u0447\u0430\u043b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u044e <strong>stb_image,<\/strong> \u0430 \u0442\u0430\u043a \u0436\u0435 \u0434\u0432\u0430 \u043a\u043b\u0430\u0441\u0441\u0430 Exception (\u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u044f \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e \u043d\u0430 \u0432\u0430\u0448\u0435 \u0443\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u0438\u0435) \u0438 Engine (\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432 \u0432\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u043e\u0436\u043d\u043e \u0443\u0431\u0440\u0430\u0442\u044c). \u041b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b <strong>IsPowerOfTwo<\/strong> \u0438 <strong>closest<\/strong> \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043b\u0438 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f, \u0435\u0441\u043b\u0438 \u0432 \u0432\u0430\u0448\u0435\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u0432\u0441\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u044f\u0445 \u0441\u0442\u0435\u043f\u0435\u043d\u0438 \u0434\u0432\u043e\u0439\u043a\u0438 (512*512, 1024*1024 \u0438 \u0442.\u0434.), \u0442\u043e \u044d\u0442\u0438 \u043c\u0435\u0442\u043e\u0434\u044b \u0442\u043e\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0438\u0433\u043d\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c.<\/p>\n<pre><code class=\"cpp\">\/\/ should be main thread only unsigned int Texture::LoadFromFile(const std::string &amp;path, bool srgb, bool force_uncompressed) { const std::lock_guard&lt;std::recursive_mutex> lock(mutex);<\/code><\/pre>\n<p>\u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043c\u0435\u0442\u043e\u0434\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0442\u0435\u043a\u0441\u0442\u0443\u0440\u044b \u043c\u044b \u0441\u0440\u0430\u0437\u0443 \u0436\u0435 \u043b\u043e\u043a\u0430\u0435\u043c \u0433\u043b\u0430\u0432\u043d\u044b\u0439 \u043c\u044e\u0442\u0435\u043a\u0441.<\/p>\n<pre><code class=\"cpp\">if (path2id.contains(path)) { unsigned int gl_id = path2id.at(path); if (need_to_unload_after_unmap.contains(gl_id)) { need_to_unload_after_unmap.erase(gl_id); } return gl_id; }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c\u0430\u044f \u0442\u0435\u043a\u0441\u0442\u0443\u0440\u0430 \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 <strong>path2id<\/strong> (\u0442\u043e \u0435\u0441\u0442\u044c \u0443\u0436\u0435 \u0431\u044b\u043b \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u0435\u0435 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443) &#8212; \u0442\u043e \u0441\u0440\u0430\u0437\u0443 \u0436\u0435 \u0432\u044b\u0434\u0430\u0435\u043c \u0435\u0435 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0438 \u043f\u0440\u0435\u043a\u0440\u0430\u0449\u0430\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u0430. \u0417\u0430\u043e\u0434\u043d\u043e, \u0435\u0441\u043b\u0438 \u044d\u0442\u0443 \u0442\u0435\u043a\u0441\u0442\u0443\u0440\u0443 \u0443\u0436\u0435 \u0437\u0430\u043a\u0430\u0437\u0430\u043b\u0438 \u043d\u0430 \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0443 (\u043e\u043d\u0430 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0432 \u0441\u044d\u0442\u0435 <strong>need_to_unload_after_unmap<\/strong>) &#8212; \u043e\u0442\u043c\u0435\u043d\u044f\u0435\u043c \u044d\u0442\u043e\u0442 \u0437\u0430\u043a\u0430\u0437.<\/p>\n<pre><code class=\"cpp\">if (!std::filesystem::is_regular_file(path)) { throw Exception(\"File not found: \" + path); }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0444\u0430\u0439\u043b \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d &#8212; \u043a\u0438\u0434\u0430\u0435\u043c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435.<\/p>\n<pre><code class=\"cpp\">unsigned int textureID; glGenTextures(1, &amp;textureID); QueueToLoad.enqueue({textureID, path, srgb, force_uncompressed}); path2id.insert({path, textureID}); id2path.insert({textureID, path}); loaded_state.insert({textureID, false}); return textureID; }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0434\u043b\u044f \u043d\u043e\u0432\u043e\u0439 \u0442\u0435\u043a\u0441\u0442\u0443\u0440\u044b \u0438 \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432\u0441\u0435 \u0437\u0430\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443. \u0417\u0430\u043e\u0434\u043d\u043e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u0445 <strong>path2id<\/strong>, <strong>id2path<\/strong> \u0438 <strong>loaded_state<\/strong>.<\/p>\n<pre><code class=\"cpp\">\/\/ any thread void Texture::Unload(unsigned int gl_id) { const std::lock_guard&lt;std::recursive_mutex> lock(mutex); if (!loaded_state.contains(gl_id)) return; if (!loaded_state.at(gl_id)) { need_to_unload_after_unmap.insert(gl_id); } else { path2id.erase(id2path.at(gl_id)); id2path.erase(gl_id); loaded_state.erase(gl_id); glDeleteTextures(1, &amp;gl_id); } }  <\/code><\/pre>\n<p>\u0421\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 <strong>Unload<\/strong> \u043e\u0431\u044f\u0437\u0443\u0435\u0442\u0441\u044f \u0432\u044b\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0442\u0435\u043a\u0441\u0442\u0443\u0440\u0443: \u0435\u0441\u043b\u0438 \u0442\u0435\u043a\u0441\u0442\u0443\u0440\u0430 \u0432\u0441\u0435 \u0435\u0449\u0435 \u0433\u0440\u0443\u0437\u0438\u0442\u0441\u044f, \u043e\u043d \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0435\u0435 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0432 \u0441\u044d\u0442 <strong>need_to_unload_after_unmap<\/strong>, \u0432 \u0438\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 &#8212; \u0432\u044b\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0435\u0435 \u0438\u0437 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u0442 \u0432\u0441\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u0445 <strong>path2id<\/strong>, <strong>id2path<\/strong> \u0438 <strong>loaded_state<\/strong>.<\/p>\n<pre><code class=\"cpp\"> glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); window2 = glfwCreateWindow(640, 480, \"Second Window\", NULL, window); glfwWindowHint(GLFW_VISIBLE, GLFW_TRUE); std::thread([this]() { glfwMakeContextCurrent(window2); Texture::ProcessQueueToLoad(); }).detach();<\/code><\/pre>\n<p>\u0421\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 <strong>ProcessQueueToLoad<\/strong> \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u0435\u043d \u0437\u0430 \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u0442\u0435\u043a\u0441\u0442\u0443\u0440 \u0438\u0437 \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0432 \u0432\u0438\u0434\u0435\u043e-\u043f\u0430\u043c\u044f\u0442\u044c. \u0415\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0432 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435, \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u0432 \u043a \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c\u0443 \u0441\u043a\u0440\u044b\u0442\u043e\u043c\u0443 \u043e\u043a\u043d\u0443 <strong>window2<\/strong>, \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u044e\u0449\u0435\u043c\u0443 \u0432\u0441\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b \u0441 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c \u043e\u043a\u043d\u043e\u043c <strong>window<\/strong>.<\/p>\n<pre><code class=\"cpp\">\/\/ parallel thead [[noreturn]] void Texture::ProcessQueueToLoad() { stbi_set_flip_vertically_on_load(true); while (true) { auto image = QueueToLoad.dequeue(); auto path = image.path; auto textureID = image.gl_id;<\/code><\/pre>\n<p>\u0421\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 <strong>ProcessQueueToLoad<\/strong> \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e\u043c \u0446\u0438\u043a\u043b\u0435, \u0432\u044b\u0431\u0438\u0440\u0430\u044f \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 <strong>QueueToLoad<\/strong> \u043d\u043e\u0432\u044b\u0435 \u0437\u0430\u043f\u0438\u0441\u0438. \u041c\u0435\u0442\u043e\u0434 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 <strong>dequeue <\/strong>\u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442 \u043f\u043e\u0442\u043e\u043a \u043f\u043e\u043a\u0430 \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043d\u0435\u0442.<\/p>\n<pre><code class=\"cpp\">int width, height, nrComponents; unsigned char* data = stbi_load(path.c_str(), &amp;width, &amp;height, &amp;nrComponents, 0);<\/code><\/pre>\n<p>\u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0438\u0437 \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0432 \u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c (<strong>data<\/strong>) \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <strong>stb_image<\/strong>. \u0423 \u0432\u0430\u0441, \u0440\u0430\u0437\u0443\u043c\u0435\u0435\u0442\u0441\u044f, \u0435\u0441\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0435 \u0438\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c. \u0424\u043e\u0440\u043c\u0430\u0442 \u043d\u0435 \u0441\u0436\u0430\u0442\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u0435\u043d \u043b\u044e\u0431\u043e\u043c\u0443 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u0443 \u0438 \u0443\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u0440\u0430\u0437\u043c\u0435\u0440 \u0434\u0430\u043d\u043d\u044b\u0445: \u0432\u044b\u0441\u043e\u0442\u0430 * \u0448\u0438\u0440\u0438\u043d\u0430 * \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u0432 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438. \u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u044d\u0442\u043e 1, 3 \u0438\u043b\u0438 4 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0442\u043e\u0433\u043e \u043c\u043e\u043d\u043e\u0445\u0440\u043e\u043c\u043d\u043e \u043b\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u043e\u0431\u044b\u0447\u043d\u043e\u0435 \u0438\u043b\u0438 \u043e\u0431\u044b\u0447\u043d\u043e\u0435 \u0441 \u043f\u0440\u043e\u0437\u0440\u0430\u0447\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<pre><code class=\"cpp\">if (!IsPowerOfTwo(width) || !IsPowerOfTwo(height)) { \/\/std::cout &lt;&lt; \"size is not power of two! resizing... \"; std::vector&lt;int> powers = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048}; int new_size = std::max(closest(powers, width), closest(powers, height)); \/\/std::cout &lt;&lt; \"new_size: \" &lt;&lt; new_size &lt;&lt; \"x\" &lt;&lt; new_size &lt;&lt; \" \"; unsigned char* data_resized = (unsigned char*) malloc(new_size * new_size * nrComponents); stbir_resize_uint8(data, width, height, 0, data_resized, new_size, new_size, 0, nrComponents); \/\/std::cout &lt;&lt; \"resized! \";  stbi_image_free(data); data = data_resized; width = new_size; height = new_size; resolution = new_size; }<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u0431\u043b\u043e\u043a \u043a\u043e\u0434\u0430 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u0442 \u0440\u0430\u0437\u043c\u0435\u0440 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f, \u0435\u0441\u043b\u0438 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0435\u043f\u0435\u043d\u044c\u044e \u0434\u0432\u043e\u0439\u043a\u0438. \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0431\u043b\u043e\u043a, \u0435\u0441\u043b\u0438 \u0432\u0441\u0435 \u0432\u0430\u0448\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430.<\/p>\n<pre><code class=\"cpp\">image.width = width; image.height = height; image.nrComponents = nrComponents; image.data = data;<\/code><\/pre>\n<p>^_^<\/p>\n<pre><code class=\"cpp\">bool compress = ::engine.rendererOptions.GetTextureCompression() == RendererOptions::TextureCompression::ENABLED; if (image.force_uncompressed) { compress = false; }<\/code><\/pre>\n<p>\u0412 \u043c\u043e\u0451\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u043e\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043b\u043e\u0441\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043b\u0438 \u0441\u0436\u0438\u043c\u0430\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043f\u0440\u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u0432 \u0432\u0438\u0434\u0435\u043e\u043f\u0430\u043c\u044f\u0442\u044c. \u042f \u0434\u0435\u043b\u0430\u044e \u044d\u0442\u043e \u0447\u0435\u0440\u0435\u0437 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432 \u043c\u043e\u0439 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 <strong>engine, <\/strong>\u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0441\u0442\u043e\u043a\u0443 \u043a\u043e\u0434\u0430, \u0437\u0430\u043c\u0435\u043d\u0438\u0432 \u0435\u0435 \u043f\u0440\u0430\u0432\u0443 \u0447\u0430\u0441\u0442\u044c \u043d\u0430 <strong>true<\/strong>. \u0418\u043b\u0438 \u0435\u0449\u0435 \u043f\u0440\u043e\u0449\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c: <br \/>bool compress = !image.force_uncompressed;<\/p>\n<pre><code class=\"cpp\">GLenum internalformat; GLenum format; if (image.nrComponents == 1) { internalformat = compress ? GL_COMPRESSED_RED : GL_RED; format = GL_RED; } else if (image.nrComponents == 3) { if (!image.srgb) { internalformat = compress ? GL_COMPRESSED_RGB : GL_RGB; } else { internalformat = compress ? GL_COMPRESSED_SRGB : GL_SRGB; } format = GL_RGB; } else if (image.nrComponents == 4) { if (!image.srgb) { internalformat = compress ? GL_COMPRESSED_RGBA : GL_RGBA; } else { internalformat = compress ? GL_COMPRESSED_SRGB_ALPHA : GL_SRGB_ALPHA; } format = GL_RGBA; }<\/code><\/pre>\n<p>\u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0447\u0438\u0441\u0435\u043b\u043e\u043a <strong>internalformat<\/strong> \u0438 <strong>format<\/strong> (\u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0436\u0435\u043b\u0430\u0435\u043c\u043e\u0433\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0430, \u0436\u0435\u043b\u0430\u0435\u043c\u043e\u0433\u043e \u0441\u0436\u0430\u0442\u0438\u044f \u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u0432 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0434\u0430\u043b\u044c\u0448\u0435 \u0432 \u0433\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e <strong>glTexImage2D<\/strong>.<\/p>\n<pre><code class=\"cpp\">glBindTexture(GL_TEXTURE_2D, image.gl_id); glTexImage2D(GL_TEXTURE_2D, 0, internalformat, image.width, image.height, 0, format, GL_UNSIGNED_BYTE, image.data); glGenerateMipmap(GL_TEXTURE_2D);  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);<\/code><\/pre>\n<p>\u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f! \u0421\u043b\u0435\u0434\u043e\u043c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044f \u043c\u0438\u043f\u043c\u0430\u043f\u043e\u0432 \u0438 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0442\u0435\u043a\u0441\u0442\u0443\u0440\u043d\u043e\u0439 \u043c\u0430\u0433\u0438\u0438. \u0422\u0430\u043a \u043a\u0430\u043a \u0432\u0441\u0451 \u044d\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0432 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435 \u0431\u0435\u0437 \u043b\u043e\u043a\u043e\u0432 \u043a\u0430\u043a\u0438\u0445-\u043b\u0438\u0431\u043e \u043c\u044e\u0442\u0435\u043a\u0441\u043e\u0432 \u2014 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043f\u043e\u0442\u043e\u043a \u0440\u0435\u043d\u0434\u0435\u0440\u0430 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u043e \u0431\u0435\u0437 \u0444\u0440\u0438\u0437\u043e\u0432.<\/p>\n<pre><code class=\"cpp\">stbi_image_free(image.data);<\/code><\/pre>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u043c\u043e\u043c\u0435\u043d\u0442\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432 \u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u043d\u0430\u043c \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u043d\u0443\u0436\u043d\u044b.<\/p>\n<pre><code class=\"cpp\">const std::lock_guard&lt;std::recursive_mutex> lock(mutex); loaded_state.at(image.gl_id) = true; glFinish(); \/\/std::cout &lt;&lt; \"Texture loaded: \" &lt;&lt; image.path &lt;&lt; std::endl; if (need_to_unload_after_unmap.contains(image.gl_id)) { need_to_unload_after_unmap.erase(image.gl_id); Unload(image.gl_id); }     } }<\/code><\/pre>\n<p>\u0412 \u043a\u043e\u043d\u0446\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0441\u0430\u043c\u043e\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435:<\/p>\n<ul>\n<li>\n<p>\u043b\u043e\u043a\u0430\u0435\u0442\u0441\u044f \u043c\u044e\u0442\u0435\u043a\u0441,<\/p>\n<\/li>\n<li>\n<p>\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043d\u0430 true,<\/p>\n<\/li>\n<li>\n<p>\u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <strong>glFinish,<\/strong><\/p>\n<\/li>\n<li>\n<p>\u0435\u0441\u043b\u0438 \u044d\u0442\u0443 \u0442\u0435\u043a\u0441\u0442\u0443\u0440\u0443 (\u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438) \u0437\u0430\u043a\u0430\u0437\u0430\u043b\u0438 \u0432\u044b\u0433\u0440\u0443\u0437\u0438\u0442\u044c &#8212; \u0432\u044b\u0433\u0440\u0443\u0436\u0430\u0435\u043c \u0435\u0435 \u0438 \u043e\u0442\u043c\u0435\u043d\u044f\u0435\u043c \u0437\u0430\u043a\u0430\u0437 \u043d\u0430 \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0443.<\/p>\n<\/li>\n<\/ul>\n<p>\u0412 \u0437\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0438 \u0436\u0435\u043b\u0430\u044e \u0432\u0430\u043c \u0440\u0435\u0448\u0430\u0435\u043c\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 ;3<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/700194\/\"> https:\/\/habr.com\/ru\/post\/700194\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u042f \u0432 \u0442\u0435\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u0443\u0433\u043e\u0434\u0430 \u0441\u043e\u0431\u0438\u0440\u0430\u043b \u043f\u043e \u043a\u0440\u0443\u043f\u0438\u0446\u0430\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043e \u0442\u043e\u043c \u043a\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u043d\u0435 \u0442\u043e\u0440\u043c\u043e\u0437\u044f\u0449\u0443\u044e \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u0442\u0440\u0435\u0434 \u0440\u0435\u043d\u0434\u0435\u0440\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u0442\u0435\u043a\u0441\u0442\u0443\u0440. \u0410\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u0430\u044f \u0444\u0438\u0447\u0430 \u0434\u043b\u044f \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u0432, \u0433\u0434\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043d\u0430 \u043b\u0435\u0442\u0443 \u043f\u043e\u0434\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0438 \u0432\u044b\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0442\u0435\u043a\u0441\u0442\u0443\u0440\u044b, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0438 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u043e\u0433\u043e \u043e\u0442\u043a\u0440\u044b\u0442\u043e\u0433\u043e \u043c\u0438\u0440\u0430. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0440\u0435\u0446\u0435\u043f\u0442&#8230;<\/p>\n<p>\u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u0447\u0435\u0440\u0435\u0437 <strong>stb_image<\/strong>.<\/p>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u043c\u044b \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0441 \u043c\u0443\u043b\u044c\u0442\u0438-\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u044c\u044e, \u043f\u0440\u0435\u0436\u0434\u0435 \u0432\u0441\u0435\u0433\u043e \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u0430\u044f (\u0434\u043b\u044f \u043c\u0443\u043b\u044c\u0442\u0438-\u043f\u043e\u0442\u043e\u0447\u043d\u043e\u0441\u0442\u0438) \u043e\u0447\u0435\u0440\u0435\u0434\u044c, \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u044f \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e \u0431\u0435\u0437 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0435\u0432:<\/p>\n<pre><code class=\"cpp\">#pragma once  #include &lt;queue> #include &lt;mutex> #include &lt;condition_variable> #include &lt;optional>  template&lt;class T> class SafeQueue { public: SafeQueue(void) : q(), m(), c() {}  ~SafeQueue(void) {}  void enqueue(T t) { std::lock_guard&lt;std::mutex> lock(m); q.push(t); c.notify_one(); }  T dequeue(void) { std::unique_lock&lt;std::mutex> lock(m); while (q.empty()) { c.wait(lock); } T val = q.front(); q.pop(); return val; }  std::optional&lt;T> pop(void) { std::unique_lock&lt;std::mutex> lock(m); if (q.empty()) { return {}; } T val = q.front(); q.pop(); return val; }  int size() { std::lock_guard&lt;std::mutex> lock(m); return q.size(); }  private: std::queue&lt;T> q; mutable std::mutex m; std::condition_variable c; };<\/code><\/pre>\n<p>\u0421\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 <strong>LoadFromFile<\/strong> \u043a\u043b\u0430\u0441\u0441\u0430 <strong>Texture<\/strong> \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u043c \u0437\u0430 \u0432\u044b\u0434\u0430\u0447\u0443 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432 \u0442\u0435\u043a\u0441\u0442\u0443\u0440 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432\u0438\u0434\u0435\u043e\u043a\u0430\u0440\u0442\u044b \u0432 \u043e\u0431\u043c\u0435\u043d \u043d\u0430 <strong>path<\/strong> \u043a \u0442\u0435\u043a\u0441\u0442\u0443\u0440\u0435 \u0438 \u0434\u0432\u0430 \u0444\u043b\u0430\u0433\u0430: <strong>srgb<\/strong> \u0438 <strong>force_uncompressed<\/strong>. \u0412\u044b\u0434\u0430\u043d\u043d\u044b\u0439 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0440\u0430\u0437\u0443, \u043f\u043e\u043a\u0430 \u0442\u0435\u043a\u0441\u0442\u0443\u0440\u0430 \u043d\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u0430 \u0432\u0438\u0437\u0443\u0430\u043b\u044c\u043d\u043e \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043a\u0430\u043a \u0447\u0451\u0440\u043d\u044b\u0439 \u043f\u0440\u044f\u043c\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a.<\/p>\n<pre><code class=\"cpp\">#pragma once  #include &lt;string> #include &lt;vector> #include &lt;map> #include &lt;set> #include \"SafeQueue.hpp\"  class Texture { public: struct UploadData { unsigned int gl_id{0}; std::string path{\"\"}; bool srgb{true}; bool force_uncompressed{false}; unsigned char* data; unsigned int width; unsigned int height; unsigned int nrComponents; };  unsigned int id; std::string type;  static std::recursive_mutex mutex; static SafeQueue&lt;UploadData> QueueToLoad; static std::map&lt;std::string, unsigned int> path2id; static std::map&lt;unsigned int, std::string> id2path; static std::map&lt;unsigned int, bool> loaded_state; static std::set&lt;unsigned int> need_to_unload_after_unmap;  static unsigned int LoadFromFile(const std::string &amp;path, bool srgb = true, bool force_uncompressed = false);  static unsigned int LoadCubemap(std::vector&lt;std::string> faces); [[noreturn]] static void ProcessQueueToLoad(); static void Unload(unsigned int gl_id); };<\/code><\/pre>\n<p><strong>mutex<\/strong> &#8212; \u043c\u044e\u0442\u0435\u043a\u0441 \u0434\u043b\u044f \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044f \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0442\u0435\u043a\u0441\u0442\u0443\u0440,<\/p>\n<p><strong>UploadData<\/strong> &#8212; \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0442\u0435\u043a\u0441\u0442\u0443\u0440\u044b \u0432 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438,<\/p>\n<p><strong>QueueToLoad<\/strong> &#8212; \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u044d\u0442\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, <\/p>\n<p><strong>path2id<\/strong> &#8212; \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043f\u0443\u0442\u0435\u0439 \u043a \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430\u043c \u0432 \u043f\u0430\u043c\u044f\u0442\u0438 \u0432\u0438\u0434\u0435\u043e\u043a\u0430\u0440\u0442\u044b, <\/p>\n<p><strong>id2path<\/strong> &#8212; \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u043e\u0431\u0440\u0430\u0442\u043d\u0430\u044f \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439,<\/p>\n<p><strong>loaded_state<\/strong> &#8212; \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0442\u0435\u043a\u0441\u0442\u0443\u0440 (false &#8212; \u0435\u0449\u0435 \u0433\u0440\u0443\u0437\u0438\u0442\u0441\u044f, true &#8212; \u0443\u0436\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u0430),<\/p>\n<p><strong>need_to_unload_after_unmap<\/strong> &#8212; \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u043e\u0432 \u0442\u0435\u043a\u0441\u0442\u0443\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u043b\u043e\u0441\u044c \u0432\u044b\u0433\u0440\u0443\u0437\u0438\u0442\u044c, \u043d\u043e \u043f\u043e\u043a\u0430 \u0447\u0442\u043e \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0442\u0430\u043a \u043a\u0430\u043a \u0442\u0435\u043a\u0441\u0442\u0443\u0440\u0430 \u0432\u0441\u0435 \u0435\u0449\u0435 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442\u0441\u044f.<\/p>\n<pre><code class=\"cpp\">#include \"Texture.hpp\" #include \"Exception.hpp\" #include &lt;glad\/glad.h> #include &lt;stb_image\/stb_image.h> #include &lt;stb_image\/stb_image_resize.h> #include &lt;iostream> #include &lt;algorithm> #include &lt;filesystem> #include &lt;thread> #include \"Engine.hpp\"  std::recursive_mutex Texture::mutex; SafeQueue&lt;Texture::UploadData> Texture::QueueToLoad; std::map&lt;std::string, unsigned int> Texture::path2id; std::map&lt;unsigned int, std::string> Texture::id2path; std::map&lt;unsigned int, bool> Texture::loaded_state; std::set&lt;unsigned int> Texture::need_to_unload_after_unmap;  bool IsPowerOfTwo(int x) { return (x != 0) &amp;&amp; ((x &amp; (x - 1)) == 0); }  int closest(std::vector&lt;int> const &amp;vec, int value) { for (auto i = vec.rbegin(); i != vec.rend(); ++i) if ((*i) &lt;= value) return (*i); return 1; } <\/code><\/pre>\n<p>\u0412 \u043d\u0430\u0447\u0430\u043b\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u044e <strong>stb_image,<\/strong> \u0430 \u0442\u0430\u043a \u0436\u0435 \u0434\u0432\u0430 \u043a\u043b\u0430\u0441\u0441\u0430 Exception (\u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u044f \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u044e \u043d\u0430 \u0432\u0430\u0448\u0435 \u0443\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u0438\u0435) \u0438 Engine (\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432 \u0432\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u043e\u0436\u043d\u043e \u0443\u0431\u0440\u0430\u0442\u044c). \u041b\u043e\u043a\u0430\u043b\u044c\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b <strong>IsPowerOfTwo<\/strong> \u0438 <strong>closest<\/strong> \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043b\u0438 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f, \u0435\u0441\u043b\u0438 \u0432 \u0432\u0430\u0448\u0435\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u0432\u0441\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u044f\u0445 \u0441\u0442\u0435\u043f\u0435\u043d\u0438 \u0434\u0432\u043e\u0439\u043a\u0438 (512*512, 1024*1024 \u0438 \u0442.\u0434.), \u0442\u043e \u044d\u0442\u0438 \u043c\u0435\u0442\u043e\u0434\u044b \u0442\u043e\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0438\u0433\u043d\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c.<\/p>\n<pre><code class=\"cpp\">\/\/ should be main thread only unsigned int Texture::LoadFromFile(const std::string &amp;path, bool srgb, bool force_uncompressed) { const std::lock_guard&lt;std::recursive_mutex> lock(mutex);<\/code><\/pre>\n<p>\u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u043c\u0435\u0442\u043e\u0434\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0442\u0435\u043a\u0441\u0442\u0443\u0440\u044b \u043c\u044b \u0441\u0440\u0430\u0437\u0443 \u0436\u0435 \u043b\u043e\u043a\u0430\u0435\u043c \u0433\u043b\u0430\u0432\u043d\u044b\u0439 \u043c\u044e\u0442\u0435\u043a\u0441.<\/p>\n<pre><code class=\"cpp\">if (path2id.contains(path)) { unsigned int gl_id = path2id.at(path); if (need_to_unload_after_unmap.contains(gl_id)) { need_to_unload_after_unmap.erase(gl_id); } return gl_id; }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0437\u0430\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u0435\u043c\u0430\u044f \u0442\u0435\u043a\u0441\u0442\u0443\u0440\u0430 \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 <strong>path2id<\/strong> (\u0442\u043e \u0435\u0441\u0442\u044c \u0443\u0436\u0435 \u0431\u044b\u043b \u0437\u0430\u043f\u0440\u043e\u0441 \u043d\u0430 \u0435\u0435 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443) &#8212; \u0442\u043e \u0441\u0440\u0430\u0437\u0443 \u0436\u0435 \u0432\u044b\u0434\u0430\u0435\u043c \u0435\u0435 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0438 \u043f\u0440\u0435\u043a\u0440\u0430\u0449\u0430\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043c\u0435\u0442\u043e\u0434\u0430. \u0417\u0430\u043e\u0434\u043d\u043e, \u0435\u0441\u043b\u0438 \u044d\u0442\u0443 \u0442\u0435\u043a\u0441\u0442\u0443\u0440\u0443 \u0443\u0436\u0435 \u0437\u0430\u043a\u0430\u0437\u0430\u043b\u0438 \u043d\u0430 \u0432\u044b\u0433\u0440\u0443\u0437\u043a\u0443 (\u043e\u043d\u0430 \u043f\u0440\u0438\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0432 \u0441\u044d\u0442\u0435 <strong>need_to_unload_after_unmap<\/strong>) &#8212; \u043e\u0442\u043c\u0435\u043d\u044f\u0435\u043c \u044d\u0442\u043e\u0442 \u0437\u0430\u043a\u0430\u0437.<\/p>\n<pre><code class=\"cpp\">if (!std::filesystem::is_regular_file(path)) { throw Exception(\"File not found: \" + path); }<\/code><\/pre>\n<p>\u0415\u0441\u043b\u0438 \u0444\u0430\u0439\u043b \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u043d &#8212; \u043a\u0438\u0434\u0430\u0435\u043c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435.<\/p>\n<pre><code class=\"cpp\">unsigned int textureID; glGenTextures(1, &amp;textureID); QueueToLoad.enqueue({textureID, path, srgb, force_uncompressed}); path2id.insert({path, textureID}); id2path.insert({textureID, path}); loaded_state.insert({textureID, false}); return textureID; }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0434\u043b\u044f \u043d\u043e\u0432\u043e\u0439 \u0442\u0435\u043a\u0441\u0442\u0443\u0440\u044b \u0438 \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432\u0441\u0435 \u0437\u0430\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443. \u0417\u0430\u043e\u0434\u043d\u043e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u0445 <strong>path2id<\/strong>, <strong>id2path<\/strong> \u0438 <strong>loaded_state<\/strong>.<\/p>\n<pre><code class=\"cpp\">\/\/ any thread void Texture::Unload(unsigned int gl_id) { const std::lock_guard&lt;std::recursive_mutex> lock(mutex); if (!loaded_state.contains(gl_id)) return; if (!loaded_state.at(gl_id)) { need_to_unload_after_unmap.insert(gl_id); } else { path2id.erase(id2path.at(gl_id)); id2path.erase(gl_id); loaded_state.erase(gl_id); glDeleteTextures(1, &amp;gl_id); } }  <\/code><\/pre>\n<p>\u0421\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 <strong>Unload<\/strong> \u043e\u0431\u044f\u0437\u0443\u0435\u0442\u0441\u044f \u0432\u044b\u0433\u0440\u0443\u0437\u0438\u0442\u044c \u0442\u0435\u043a\u0441\u0442\u0443\u0440\u0443: \u0435\u0441\u043b\u0438 \u0442\u0435\u043a\u0441\u0442\u0443\u0440\u0430 \u0432\u0441\u0435 \u0435\u0449\u0435 \u0433\u0440\u0443\u0437\u0438\u0442\u0441\u044f, \u043e\u043d \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0435\u0435 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0432 \u0441\u044d\u0442 <strong>need_to_unload_after_unmap<\/strong>, \u0432 \u0438\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 &#8212; \u0432\u044b\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u0435\u0435 \u0438\u0437 \u043f\u0430\u043c\u044f\u0442\u0438 \u0438 \u0443\u0434\u0430\u043b\u044f\u0435\u0442 \u0432\u0441\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u0445 <strong>path2id<\/strong>, <strong>id2path<\/strong> \u0438 <strong>loaded_state<\/strong>.<\/p>\n<pre><code class=\"cpp\"> glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); window2 = glfwCreateWindow(640, 480, \"Second Window\", NULL, window); glfwWindowHint(GLFW_VISIBLE, GLFW_TRUE); std::thread([this]() { glfwMakeContextCurrent(window2); Texture::ProcessQueueToLoad(); }).detach();<\/code><\/pre>\n<p>\u0421\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 <strong>ProcessQueueToLoad<\/strong> \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u0435\u043d \u0437\u0430 \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443 \u0442\u0435\u043a\u0441\u0442\u0443\u0440 \u0438\u0437 \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0432 \u0432\u0438\u0434\u0435\u043e-\u043f\u0430\u043c\u044f\u0442\u044c. \u0415\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0432 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435, \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u0432 \u043a \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c\u0443 \u0441\u043a\u0440\u044b\u0442\u043e\u043c\u0443 \u043e\u043a\u043d\u0443 <strong>window2<\/strong>, \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u044e\u0449\u0435\u043c\u0443 \u0432\u0441\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b \u0441 \u043e\u0441\u043d\u043e\u0432\u043d\u044b\u043c \u043e\u043a\u043d\u043e\u043c <strong>window<\/strong>.<\/p>\n<pre><code class=\"cpp\">\/\/ parallel thead [[noreturn]] void Texture::ProcessQueueToLoad() { stbi_set_flip_vertically_on_load(true); while (true) { auto image = QueueToLoad.dequeue(); auto path = image.path; auto textureID = image.gl_id;<\/code><\/pre>\n<p>\u0421\u0442\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 <strong>ProcessQueueToLoad<\/strong> \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e\u043c \u0446\u0438\u043a\u043b\u0435, \u0432\u044b\u0431\u0438\u0440\u0430\u044f \u0438\u0437 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 <strong>QueueToLoad<\/strong> \u043d\u043e\u0432\u044b\u0435 \u0437\u0430\u043f\u0438\u0441\u0438. \u041c\u0435\u0442\u043e\u0434 \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u043e\u0439 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 <strong>dequeue <\/strong>\u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442 \u043f\u043e\u0442\u043e\u043a \u043f\u043e\u043a\u0430 \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u0438 \u043d\u0435\u0442.<\/p>\n<pre><code class=\"cpp\">int width, height, nrComponents; unsigned char* data = stbi_load(path.c_str(), &amp;width, &amp;height, &amp;nrComponents, 0);<\/code><\/pre>\n<p>\u0417\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0438\u0437 \u0444\u0430\u0439\u043b\u043e\u0432\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0432 \u043e\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c (<strong>data<\/strong>) \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <strong>stb_image<\/strong>. \u0423 \u0432\u0430\u0441, \u0440\u0430\u0437\u0443\u043c\u0435\u0435\u0442\u0441\u044f, \u0435\u0441\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0435 \u0438\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c. \u0424\u043e\u0440\u043c\u0430\u0442 \u043d\u0435 \u0441\u0436\u0430\u0442\u043e\u0433\u043e \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0438\u043d\u0442\u0443\u0438\u0442\u0438\u0432\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u0435\u043d \u043b\u044e\u0431\u043e\u043c\u0443 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0441\u0442\u0443 \u0438 \u0443\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u0440\u0430\u0437\u043c\u0435\u0440 \u0434\u0430\u043d\u043d\u044b\u0445: \u0432\u044b\u0441\u043e\u0442\u0430 * \u0448\u0438\u0440\u0438\u043d\u0430 * \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u0432 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438. \u041a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u044d\u0442\u043e 1, 3 \u0438\u043b\u0438 4 \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0442\u043e\u0433\u043e \u043c\u043e\u043d\u043e\u0445\u0440\u043e\u043c\u043d\u043e \u043b\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u043e\u0431\u044b\u0447\u043d\u043e\u0435 \u0438\u043b\u0438 \u043e\u0431\u044b\u0447\u043d\u043e\u0435 \u0441 \u043f\u0440\u043e\u0437\u0440\u0430\u0447\u043d\u043e\u0441\u0442\u044c\u044e.<\/p>\n<pre><code class=\"cpp\">if (!IsPowerOfTwo(width) || !IsPowerOfTwo(height)) { \/\/std::cout &lt;&lt; \"size is not power of two! resizing... \"; std::vector&lt;int> powers = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048}; int new_size = std::max(closest(powers, width), closest(powers, height)); \/\/std::cout &lt;&lt; \"new_size: \" &lt;&lt; new_size &lt;&lt; \"x\" &lt;&lt; new_size &lt;&lt; \" \"; unsigned char* data_resized = (unsigned char*) malloc(new_size * new_size * nrComponents); stbir_resize_uint8(data, width, height, 0, data_resized, new_size, new_size, 0, nrComponents); \/\/std::cout &lt;&lt; \"resized! \";  stbi_image_free(data); data = data_resized; width = new_size; height = new_size; resolution = new_size; }<\/code><\/pre>\n<p>\u042d\u0442\u043e\u0442 \u0431\u043b\u043e\u043a \u043a\u043e\u0434\u0430 \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u0442 \u0440\u0430\u0437\u043c\u0435\u0440 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f, \u0435\u0441\u043b\u0438 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0435\u043f\u0435\u043d\u044c\u044e \u0434\u0432\u043e\u0439\u043a\u0438. \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u0431\u043b\u043e\u043a, \u0435\u0441\u043b\u0438 \u0432\u0441\u0435 \u0432\u0430\u0448\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430.<\/p>\n<pre><code class=\"cpp\">image.width = width; image.height = height; image.nrComponents = nrComponents; image.data = data;<\/code><\/pre>\n<p>^_^<\/p>\n<pre><code class=\"cpp\">bool compress = ::engine.rendererOptions.GetTextureCompression() == RendererOptions::TextureCompression::ENABLED; if (image.force_uncompressed) { compress = false; }<\/code><\/pre>\n<p>\u0412 \u043c\u043e\u0451\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u043e\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043b\u043e\u0441\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u044f\u0442\u044c \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043b\u0438 \u0441\u0436\u0438\u043c\u0430\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043f\u0440\u0438 \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0435 \u0432 \u0432\u0438\u0434\u0435\u043e\u043f\u0430\u043c\u044f\u0442\u044c. \u042f \u0434\u0435\u043b\u0430\u044e \u044d\u0442\u043e \u0447\u0435\u0440\u0435\u0437 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432 \u043c\u043e\u0439 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 <strong>engine, <\/strong>\u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u044d\u0442\u0443 \u0441\u0442\u043e\u043a\u0443 \u043a\u043e\u0434\u0430, \u0437\u0430\u043c\u0435\u043d\u0438\u0432 \u0435\u0435 \u043f\u0440\u0430\u0432\u0443 \u0447\u0430\u0441\u0442\u044c \u043d\u0430 <strong>true<\/strong>. \u0418\u043b\u0438 \u0435\u0449\u0435 \u043f\u0440\u043e\u0449\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c: <br \/>bool compress = !image.force_uncompressed;<\/p>\n<pre><code class=\"cpp\">GLenum internalformat; GLenum format; if (image.nrComponents == 1) { internalformat = compress ? GL_COMPRESSED_RED : GL_RED; format = GL_RED; } else if (image.nrComponents == 3) { if (!image.srgb) { internalformat = compress ? GL_COMPRESSED_RGB : GL_RGB; } else { internalformat = compress ? GL_COMPRESSED_SRGB : GL_SRGB; } format = GL_RGB; } else if (image.nrComponents == 4) { if (!image.srgb) { internalformat = compress ? GL_COMPRESSED_RGBA : GL_RGBA; } else { internalformat = compress ? GL_COMPRESSED_SRGB_ALPHA : GL_SRGB_ALPHA; } format = GL_RGBA; }<\/code><\/pre>\n<p>\u041f\u0440\u0435\u0434\u0432\u0430\u0440\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0447\u0438\u0441\u0435\u043b\u043e\u043a <strong>internalformat<\/strong> \u0438 <strong>format<\/strong> (\u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0436\u0435\u043b\u0430\u0435\u043c\u043e\u0433\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0430, \u0436\u0435\u043b\u0430\u0435\u043c\u043e\u0433\u043e \u0441\u0436\u0430\u0442\u0438\u044f \u0438 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 \u0432 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0438), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0434\u0430\u043b\u044c\u0448\u0435 \u0432 \u0433\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e <strong>glTexImage2D<\/strong>.<\/p>\n<pre><code class=\"cpp\">glBindTexture(GL_TEXTURE_2D, image.gl_id); glTexImage2D(GL_TEXTURE_2D, 0, internalformat, image.width, image.height, 0, format, GL_UNSIGNED_BYTE, <\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-341435","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/341435","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=341435"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/341435\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=341435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=341435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=341435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}