Skip to content

Commit

Permalink
Correct and revise OpenGL texture pooling (#2709)
Browse files Browse the repository at this point in the history
  • Loading branch information
alasram authored Aug 29, 2024
1 parent 290bf92 commit b850c25
Show file tree
Hide file tree
Showing 79 changed files with 817 additions and 247 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ list(APPEND INCLUDE_FILES
${PROJECT_SOURCE_DIR}/include/mbgl/util/instrumentation.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/interpolate.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/logging.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/lru_cache.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/noncopyable.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/platform.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/premultiply.hpp
Expand Down Expand Up @@ -1094,6 +1095,8 @@ if(MLN_WITH_OPENGL)
${PROJECT_SOURCE_DIR}/src/mbgl/gl/renderbuffer_resource.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/renderbuffer_resource.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/renderer_backend.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/resource_pool.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/resource_pool.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/state.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/texture.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/gl/texture.hpp
Expand Down
3 changes: 3 additions & 0 deletions bazel/core.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,7 @@ MLN_CORE_HEADERS = [
"include/mbgl/util/instrumentation.hpp",
"include/mbgl/util/interpolate.hpp",
"include/mbgl/util/logging.hpp",
"include/mbgl/util/lru_cache.hpp",
"include/mbgl/util/monotonic_timer.hpp",
"include/mbgl/util/noncopyable.hpp",
"include/mbgl/util/platform.hpp",
Expand Down Expand Up @@ -886,6 +887,8 @@ MLN_OPENGL_SOURCE = [
"src/mbgl/gl/renderbuffer_resource.cpp",
"src/mbgl/gl/renderbuffer_resource.hpp",
"src/mbgl/gl/renderer_backend.cpp",
"src/mbgl/gl/resource_pool.cpp",
"src/mbgl/gl/resource_pool.hpp",
"src/mbgl/gl/state.hpp",
"src/mbgl/gl/texture.cpp",
"src/mbgl/gl/texture.hpp",
Expand Down
4 changes: 2 additions & 2 deletions include/mbgl/gl/texture2d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ class Texture2D : public gfx::Texture2D {
void unbind() noexcept;

private:
void createObject() noexcept;
void createStorage(const void* data = nullptr) noexcept;
void allocateTexture() noexcept;
void updateTextureData(const void* data = nullptr) noexcept;

private:
gl::Context& context;
Expand Down
2 changes: 1 addition & 1 deletion include/mbgl/util/instrumentation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ constexpr const char* tracyConstMemoryLabel = "Constant Buffer Memory";
#define MLN_TRACE_GL_CONTEXT() ((void)0)
#define MLN_TRACE_GL_ZONE(label) ((void)0)
#define MLN_TRACE_FUNC_GL() ((void)0)
#define MLN_END_FRAME() FrameMark;
#define MLN_END_FRAME() FrameMark

#endif // MLN_RENDER_BACKEND_OPENGL

Expand Down
60 changes: 60 additions & 0 deletions include/mbgl/util/lru_cache.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#pragma once

#include <list>
#include <stdexcept>
#include <unordered_map>

namespace mbgl {

// Simple non-thread-safe LRU cache
template <typename Item, typename Hash = std::hash<Item>>
class LRU {
public:
// Number of cached items
size_t size() const { return list.size(); }

// Check if cache is empty
bool empty() const { return list.empty(); }

// Access x
// If x is already cached, move it to the front (most recently used)
void touch(Item x) {
auto it = map.find(x);
if (it != map.end()) {
list.splice(list.begin(), list, it->second);
} else {
list.push_front(x);
map[std::move(x)] = list.begin();
}
}

// Evict the least recently used item
// If the cache is empty, throw an exception
Item evict() {
if (list.empty()) {
throw std::runtime_error("LRU::evict: empty cache");
}
Item x = std::move(list.back());
map.erase(x);
list.pop_back();
return x;
}

// Check if item exists in cache without changing its order
bool isHit(const Item& x) const { return map.find(std::move(x)) != map.end(); }

// remove item from cache if it exists
void remove(const Item& x) {
auto it = map.find(x);
if (it != map.end()) {
list.erase(it->second);
map.erase(it);
}
}

private:
std::list<Item> list;
std::unordered_map<Item, typename std::list<Item>::iterator, Hash> map;
};

} // namespace mbgl
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
15,
1,
[
147712,
147712
164096,
164096
],
[
130,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
51,
1,
[
53024,
53024
53032,
53032
],
[
43734,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
17,
1,
[
125574,
125574
191110,
191110
],
[
454,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
7,
1,
[
32768,
32768
49152,
49152
],
[
46,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
8,
1,
[
32768,
32768
49152,
49152
],
[
46,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
9,
1,
[
22784,
22784
29184,
29184
],
[
34,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
5,
1,
[
22784,
22784
25088,
25088
],
[
22,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
7,
1,
[
32768,
32768
49152,
49152
],
[
94,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
9,
1,
[
33768,
33768
50702,
50702
],
[
34,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
9,
1,
[
22080,
22080
27776,
27776
],
[
34,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
7,
1,
[
32768,
32768
49152,
49152
],
[
34,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
5,
1,
[
32768,
32768
114688,
114688
],
[
22,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
7,
1,
[
32768,
32768
49152,
49152
],
[
34,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
5,
1,
[
65536,
65536
81920,
81920
],
[
22,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
9,
1,
[
21264,
21264
42528,
42528
],
[
34,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
17,
1,
[
163840,
163840
196608,
196608
],
[
118,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
7,
1,
[
32768,
32768
65536,
65536
],
[
82,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
9,
1,
[
24576,
24576
46656,
46656
],
[
34,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
7,
1,
[
32768,
32768
49152,
49152
],
[
46,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
7,
1,
[
32768,
32768
49152,
49152
],
[
46,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
32,
4,
[
158509,
158509
321286,
321286
],
[
712,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
60,
4,
[
131780,
131780
230615,
230615
],
[
102328,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
9,
1,
[
215688,
215688
281224,
281224
],
[
58,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
36,
4,
[
143344,
143344
250852,
250852
],
[
194056,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
52,
4,
[
296872,
296872
685542,
685542
],
[
79084,
Expand Down
Loading

0 comments on commit b850c25

Please sign in to comment.