yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
arena.h
Go to the documentation of this file.
1#ifndef YAZE_APP_GFX_ARENA_H
2#define YAZE_APP_GFX_ARENA_H
3
4#include <array>
5#include <cstdint>
6#include <functional>
7#include <list>
8#include <memory>
9#include <mutex>
10#include <string>
11#include <tuple>
12#include <unordered_map>
13#include <vector>
14
15#include "app/gfx/core/bitmap.h"
17#include "util/sdl_deleter.h"
18
19namespace yaze {
20namespace gfx {
21
47class Arena {
48 public:
49 static Arena& Get();
50
51 void Initialize(IRenderer* renderer);
52 ~Arena();
53
54 // --- New Deferred Command System ---
58 Bitmap* bitmap; // The bitmap that needs a texture operation
59 uint32_t generation; // Generation at queue time for staleness detection
60 };
61
63 void ProcessTextureQueue(IRenderer* renderer);
64 void ClearTextureQueue();
65
70 bool HasPendingTextures() const { return !texture_command_queue_.empty(); }
71
77 bool ProcessSingleTexture(IRenderer* renderer);
78
94 bool ProcessTextureQueueWithBudget(IRenderer* renderer, float budget_ms);
95
100 size_t textures_processed = 0; // Total textures processed this session
101 size_t frames_with_work = 0; // Frames that did texture work
102 float total_time_ms = 0.0f; // Total time spent processing
103 float max_frame_time_ms = 0.0f; // Maximum time spent in a single call
104 float avg_texture_time_ms = 0.0f; // Average time per texture
105
106 void Reset() {
109 total_time_ms = 0.0f;
110 max_frame_time_ms = 0.0f;
111 avg_texture_time_ms = 0.0f;
112 }
113 };
114
122
127
128 // --- Surface Management (unchanged) ---
129 SDL_Surface* AllocateSurface(int width, int height, int depth, int format);
130 void FreeSurface(SDL_Surface* surface);
131
132 void Shutdown();
133
134 // Resource tracking for debugging
135 size_t GetTextureCount() const { return textures_.size(); }
136 size_t GetSurfaceCount() const { return surfaces_.size(); }
137 size_t GetPooledTextureCount() const {
139 }
140 size_t GetPooledSurfaceCount() const {
142 }
144 return texture_command_queue_.size();
145 }
146
147 // Graphics sheet access (223 total sheets in YAZE)
152 std::array<gfx::Bitmap, 223>& gfx_sheets() { return gfx_sheets_; }
153
159 auto gfx_sheet(int i) {
160 if (i < 0 || i >= 223) return gfx::Bitmap{};
161 return gfx_sheets_[i];
162 }
163
169 auto mutable_gfx_sheet(int i) {
170 if (i < 0 || i >= 223) return static_cast<gfx::Bitmap*>(nullptr);
171 return &gfx_sheets_[i];
172 }
173
178 auto mutable_gfx_sheets() { return &gfx_sheets_; }
179
185 void NotifySheetModified(int sheet_index);
186
187 // ========== Palette Change Notification System ==========
188
193 std::function<void(const std::string& group_name, int palette_index)>;
194
201 void NotifyPaletteModified(const std::string& group_name,
202 int palette_index = -1);
203
210
215 void UnregisterPaletteListener(int listener_id);
216
217 // Background buffer access for SNES layer rendering
222 auto& bg1() { return bg1_; }
223
228 auto& bg2() { return bg2_; }
229
230 // ========== LRU Sheet Texture Cache ==========
231
239 void TouchSheet(int sheet_index);
240
252 Bitmap* GetSheetWithCache(int sheet_index);
253
261 void SetSheetCacheSize(size_t max_size);
262
267 size_t GetSheetCacheSize() const { return sheet_cache_max_size_; }
268
273 size_t GetCachedSheetCount() const { return sheet_lru_map_.size(); }
274
282 size_t EvictLRUSheets(size_t count = 0);
283
290 void ClearSheetCache();
291
296 size_t hits = 0; // Sheet accessed and already had texture
297 size_t misses = 0; // Sheet accessed but needed texture creation
298 size_t evictions = 0; // Textures evicted due to cache pressure
299 size_t current_size = 0; // Current number of cached textures
300
301 void Reset() {
302 hits = 0;
303 misses = 0;
304 evictions = 0;
305 current_size = 0;
306 }
307
308 float HitRate() const {
309 size_t total = hits + misses;
310 return total > 0 ? static_cast<float>(hits) / total : 0.0f;
311 }
312 };
313
319 return sheet_cache_stats_;
320 }
321
326
327 private:
328 Arena();
329
332
333 static constexpr int kTilesPerRow = 64;
334 static constexpr int kTilesPerColumn = 64;
335 static constexpr int kTotalTiles = kTilesPerRow * kTilesPerColumn;
336
337 std::array<uint16_t, kTotalTiles> layer1_buffer_;
338 std::array<uint16_t, kTotalTiles> layer2_buffer_;
339
340 std::array<gfx::Bitmap, 223> gfx_sheets_;
341
342 std::unordered_map<TextureHandle,
343 std::unique_ptr<SDL_Texture, util::SDL_Texture_Deleter>>
345
346 std::unordered_map<SDL_Surface*,
347 std::unique_ptr<SDL_Surface, util::SDL_Surface_Deleter>>
349
350 // Resource pooling for efficient memory management
351 struct TexturePool {
352 std::vector<TextureHandle> available_textures_;
353 std::unordered_map<TextureHandle, std::pair<int, int>> texture_sizes_;
354 static constexpr size_t MAX_POOL_SIZE = 100;
356
357 struct SurfacePool {
358 std::vector<SDL_Surface*> available_surfaces_;
359 std::unordered_map<SDL_Surface*, std::tuple<int, int, int, int>>
361 static constexpr size_t MAX_POOL_SIZE = 100;
363
364 std::vector<TextureCommand> texture_command_queue_;
367
368 // Palette change notification system
369 std::unordered_map<int, PaletteChangeCallback> palette_listeners_;
371
372 // LRU sheet texture cache
373 // List stores sheet indices in access order (front = most recent)
374 std::list<int> sheet_lru_list_;
375 // Map from sheet index to iterator in lru_list for O(1) access
376 std::unordered_map<int, std::list<int>::iterator> sheet_lru_map_;
377 size_t sheet_cache_max_size_ = 64; // Default: cache 64 sheet textures
379};
380
381} // namespace gfx
382} // namespace yaze
383
384#endif // YAZE_APP_GFX_ARENA_H
Resource management arena for efficient graphics memory handling.
Definition arena.h:47
IRenderer * renderer_
Definition arena.h:365
size_t sheet_cache_max_size_
Definition arena.h:377
size_t GetSurfaceCount() const
Definition arena.h:136
std::unordered_map< SDL_Surface *, std::unique_ptr< SDL_Surface, util::SDL_Surface_Deleter > > surfaces_
Definition arena.h:348
void Initialize(IRenderer *renderer)
Definition arena.cc:17
struct yaze::gfx::Arena::TexturePool texture_pool_
bool ProcessTextureQueueWithBudget(IRenderer *renderer, float budget_ms)
Process texture queue with a time budget.
Definition arena.cc:261
SDL_Surface * AllocateSurface(int width, int height, int depth, int format)
Definition arena.cc:327
std::unordered_map< TextureHandle, std::unique_ptr< SDL_Texture, util::SDL_Texture_Deleter > > textures_
Definition arena.h:344
BackgroundBuffer bg2_
Definition arena.h:331
size_t GetPooledSurfaceCount() const
Definition arena.h:140
bool HasPendingTextures() const
Check if there are pending textures to process.
Definition arena.h:70
auto gfx_sheet(int i)
Get a specific graphics sheet by index.
Definition arena.h:159
static constexpr int kTotalTiles
Definition arena.h:335
void ClearTextureQueue()
Definition arena.cc:42
Bitmap * GetSheetWithCache(int sheet_index)
Get a sheet with automatic LRU tracking and texture creation.
Definition arena.cc:476
size_t GetTextureCount() const
Definition arena.h:135
void QueueTextureCommand(TextureCommandType type, Bitmap *bitmap)
Definition arena.cc:36
int RegisterPaletteListener(PaletteChangeCallback callback)
Register a callback for palette change notifications.
Definition arena.cc:441
std::array< uint16_t, kTotalTiles > layer2_buffer_
Definition arena.h:338
static constexpr int kTilesPerColumn
Definition arena.h:334
size_t GetPooledTextureCount() const
Definition arena.h:137
static constexpr int kTilesPerRow
Definition arena.h:333
auto mutable_gfx_sheets()
Get mutable reference to all graphics sheets.
Definition arena.h:178
const SheetCacheStats & GetSheetCacheStats() const
Get sheet cache statistics.
Definition arena.h:318
void ResetTextureQueueStats()
Reset texture queue statistics.
Definition arena.h:126
SheetCacheStats sheet_cache_stats_
Definition arena.h:378
std::function< void(const std::string &group_name, int palette_index)> PaletteChangeCallback
Definition arena.h:192
void FreeSurface(SDL_Surface *surface)
Definition arena.cc:357
auto mutable_gfx_sheet(int i)
Get mutable reference to a specific graphics sheet.
Definition arena.h:169
BackgroundBuffer bg1_
Definition arena.h:330
void ProcessTextureQueue(IRenderer *renderer)
Definition arena.cc:116
std::array< gfx::Bitmap, 223 > gfx_sheets_
Definition arena.h:340
bool ProcessSingleTexture(IRenderer *renderer)
Process a single texture command for frame-budget-aware loading.
Definition arena.cc:46
auto & bg2()
Get reference to background layer 2 buffer.
Definition arena.h:228
std::unordered_map< int, std::list< int >::iterator > sheet_lru_map_
Definition arena.h:376
std::array< gfx::Bitmap, 223 > & gfx_sheets()
Get reference to all graphics sheets.
Definition arena.h:152
std::unordered_map< int, PaletteChangeCallback > palette_listeners_
Definition arena.h:369
std::vector< TextureCommand > texture_command_queue_
Definition arena.h:364
std::array< uint16_t, kTotalTiles > layer1_buffer_
Definition arena.h:337
void NotifySheetModified(int sheet_index)
Notify Arena that a graphics sheet has been modified.
Definition arena.cc:393
auto & bg1()
Get reference to background layer 1 buffer.
Definition arena.h:222
struct yaze::gfx::Arena::SurfacePool surface_pool_
void ResetSheetCacheStats()
Reset sheet cache statistics.
Definition arena.h:325
void UnregisterPaletteListener(int listener_id)
Unregister a palette change listener.
Definition arena.cc:448
TextureQueueStats texture_queue_stats_
Definition arena.h:366
size_t texture_command_queue_size() const
Definition arena.h:143
void Shutdown()
Definition arena.cc:371
void SetSheetCacheSize(size_t max_size)
Set the maximum number of sheet textures to keep cached.
Definition arena.cc:509
static Arena & Get()
Definition arena.cc:21
size_t EvictLRUSheets(size_t count=0)
Evict least recently used sheet textures.
Definition arena.cc:521
size_t GetSheetCacheSize() const
Get current sheet cache size limit.
Definition arena.h:267
std::list< int > sheet_lru_list_
Definition arena.h:374
size_t GetCachedSheetCount() const
Get number of sheets currently with textures.
Definition arena.h:273
int next_palette_listener_id_
Definition arena.h:370
void NotifyPaletteModified(const std::string &group_name, int palette_index=-1)
Notify all listeners that a palette has been modified.
Definition arena.cc:423
const TextureQueueStats & GetTextureQueueStats() const
Get texture queue processing statistics.
Definition arena.h:119
void ClearSheetCache()
Clear all sheet texture cache tracking.
Definition arena.cc:558
void TouchSheet(int sheet_index)
Mark a graphics sheet as recently accessed.
Definition arena.cc:458
Represents a bitmap image optimized for SNES ROM hacking.
Definition bitmap.h:67
Defines an abstract interface for all rendering operations.
Definition irenderer.h:60
void * TextureHandle
An abstract handle representing a texture.
Definition irenderer.h:47
Statistics for sheet cache performance.
Definition arena.h:295
static constexpr size_t MAX_POOL_SIZE
Definition arena.h:361
std::vector< SDL_Surface * > available_surfaces_
Definition arena.h:358
std::unordered_map< SDL_Surface *, std::tuple< int, int, int, int > > surface_info_
Definition arena.h:360
TextureCommandType type
Definition arena.h:57
static constexpr size_t MAX_POOL_SIZE
Definition arena.h:354
std::vector< TextureHandle > available_textures_
Definition arena.h:352
std::unordered_map< TextureHandle, std::pair< int, int > > texture_sizes_
Definition arena.h:353
Statistics for texture queue processing.
Definition arena.h:99