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
64 const Bitmap* bitmap) const;
65 void ProcessTextureQueue(IRenderer* renderer);
66 void ClearTextureQueue();
67
76 void RetireBitmap(Bitmap& bitmap);
77
86 size_t DrainRetiredBitmaps(IRenderer* renderer);
87
92 bool HasPendingTextures() const { return !texture_command_queue_.empty(); }
93
99 bool ProcessSingleTexture(IRenderer* renderer);
100
116 bool ProcessTextureQueueWithBudget(IRenderer* renderer, float budget_ms);
117
122 size_t textures_processed = 0; // Total textures processed this session
123 size_t frames_with_work = 0; // Frames that did texture work
124 float total_time_ms = 0.0f; // Total time spent processing
125 float max_frame_time_ms = 0.0f; // Maximum time spent in a single call
126 float avg_texture_time_ms = 0.0f; // Average time per texture
127
128 void Reset() {
131 total_time_ms = 0.0f;
132 max_frame_time_ms = 0.0f;
133 avg_texture_time_ms = 0.0f;
134 }
135 };
136
144
149
150 // --- Surface Management (unchanged) ---
151 SDL_Surface* AllocateSurface(int width, int height, int depth, int format);
152 void FreeSurface(SDL_Surface* surface);
153
154 void Shutdown();
155
156 // Resource tracking for debugging
157 size_t GetTextureCount() const { return textures_.size(); }
158 size_t GetSurfaceCount() const { return surfaces_.size(); }
159 size_t GetPooledTextureCount() const {
161 }
162 size_t GetPooledSurfaceCount() const {
164 }
166 return texture_command_queue_.size();
167 }
169 return retired_texture_handles_.size();
170 }
171
172 // Graphics sheet access (223 total sheets in YAZE)
177 std::array<gfx::Bitmap, 223>& gfx_sheets() { return gfx_sheets_; }
178
184 auto gfx_sheet(int i) {
185 if (i < 0 || i >= 223)
186 return gfx::Bitmap{};
187 return gfx_sheets_[i];
188 }
189
195 auto mutable_gfx_sheet(int i) {
196 if (i < 0 || i >= 223)
197 return static_cast<gfx::Bitmap*>(nullptr);
198 return &gfx_sheets_[i];
199 }
200
205 auto mutable_gfx_sheets() { return &gfx_sheets_; }
206
212 void NotifySheetModified(int sheet_index);
213
214 // ========== Palette Change Notification System ==========
215
220 std::function<void(const std::string& group_name, int palette_index)>;
221
228 void NotifyPaletteModified(const std::string& group_name,
229 int palette_index = -1);
230
237
242 void UnregisterPaletteListener(int listener_id);
243
244 // Background buffer access for SNES layer rendering
249 auto& bg1() { return bg1_; }
250
255 auto& bg2() { return bg2_; }
256
257 // ========== LRU Sheet Texture Cache ==========
258
266 void TouchSheet(int sheet_index);
267
279 Bitmap* GetSheetWithCache(int sheet_index);
280
288 void SetSheetCacheSize(size_t max_size);
289
294 size_t GetSheetCacheSize() const { return sheet_cache_max_size_; }
295
300 size_t GetCachedSheetCount() const { return sheet_lru_map_.size(); }
301
309 size_t EvictLRUSheets(size_t count = 0);
310
317 void ClearSheetCache();
318
323 size_t hits = 0; // Sheet accessed and already had texture
324 size_t misses = 0; // Sheet accessed but needed texture creation
325 size_t evictions = 0; // Textures evicted due to cache pressure
326 size_t current_size = 0; // Current number of cached textures
327
328 void Reset() {
329 hits = 0;
330 misses = 0;
331 evictions = 0;
332 current_size = 0;
333 }
334
335 float HitRate() const {
336 size_t total = hits + misses;
337 return total > 0 ? static_cast<float>(hits) / total : 0.0f;
338 }
339 };
340
346 return sheet_cache_stats_;
347 }
348
353
354 private:
355 Arena();
356
359
360 static constexpr int kTilesPerRow = 64;
361 static constexpr int kTilesPerColumn = 64;
362 static constexpr int kTotalTiles = kTilesPerRow * kTilesPerColumn;
363
364 std::array<uint16_t, kTotalTiles> layer1_buffer_;
365 std::array<uint16_t, kTotalTiles> layer2_buffer_;
366
367 std::array<gfx::Bitmap, 223> gfx_sheets_;
368
369 std::unordered_map<TextureHandle,
370 std::unique_ptr<SDL_Texture, util::SDL_Texture_Deleter>>
372
373 std::unordered_map<SDL_Surface*,
374 std::unique_ptr<SDL_Surface, util::SDL_Surface_Deleter>>
376
377 // Resource pooling for efficient memory management
378 struct TexturePool {
379 std::vector<TextureHandle> available_textures_;
380 std::unordered_map<TextureHandle, std::pair<int, int>> texture_sizes_;
381 static constexpr size_t MAX_POOL_SIZE = 100;
383
384 struct SurfacePool {
385 std::vector<SDL_Surface*> available_surfaces_;
386 std::unordered_map<SDL_Surface*, std::tuple<int, int, int, int>>
388 static constexpr size_t MAX_POOL_SIZE = 100;
390
391 std::vector<TextureCommand> texture_command_queue_;
392 std::vector<TextureHandle> retired_texture_handles_;
394 bool is_shutdown_ = false;
396
397 // Palette change notification system
398 std::unordered_map<int, PaletteChangeCallback> palette_listeners_;
400
401 // LRU sheet texture cache
402 // List stores sheet indices in access order (front = most recent)
403 std::list<int> sheet_lru_list_;
404 // Map from sheet index to iterator in lru_list for O(1) access
405 std::unordered_map<int, std::list<int>::iterator> sheet_lru_map_;
406 size_t sheet_cache_max_size_ = 64; // Default: cache 64 sheet textures
408};
409
410} // namespace gfx
411} // namespace yaze
412
413#endif // YAZE_APP_GFX_ARENA_H
Resource management arena for efficient graphics memory handling.
Definition arena.h:47
IRenderer * renderer_
Definition arena.h:393
size_t sheet_cache_max_size_
Definition arena.h:406
size_t GetSurfaceCount() const
Definition arena.h:158
std::unordered_map< SDL_Surface *, std::unique_ptr< SDL_Surface, util::SDL_Surface_Deleter > > surfaces_
Definition arena.h:375
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:378
size_t retired_texture_handle_count() const
Definition arena.h:168
SDL_Surface * AllocateSurface(int width, int height, int depth, int format)
Definition arena.cc:449
std::unordered_map< TextureHandle, std::unique_ptr< SDL_Texture, util::SDL_Texture_Deleter > > textures_
Definition arena.h:371
BackgroundBuffer bg2_
Definition arena.h:358
size_t GetPooledSurfaceCount() const
Definition arena.h:162
bool HasPendingTextures() const
Check if there are pending textures to process.
Definition arena.h:92
auto gfx_sheet(int i)
Get a specific graphics sheet by index.
Definition arena.h:184
static constexpr int kTotalTiles
Definition arena.h:362
void ClearTextureQueue()
Definition arena.cc:59
Bitmap * GetSheetWithCache(int sheet_index)
Get a sheet with automatic LRU tracking and texture creation.
Definition arena.cc:609
size_t GetTextureCount() const
Definition arena.h:157
void QueueTextureCommand(TextureCommandType type, Bitmap *bitmap)
Definition arena.cc:39
int RegisterPaletteListener(PaletteChangeCallback callback)
Register a callback for palette change notifications.
Definition arena.cc:574
std::array< uint16_t, kTotalTiles > layer2_buffer_
Definition arena.h:365
static constexpr int kTilesPerColumn
Definition arena.h:361
bool HasPendingTextureCommand(TextureCommandType type, const Bitmap *bitmap) const
Definition arena.cc:45
size_t GetPooledTextureCount() const
Definition arena.h:159
static constexpr int kTilesPerRow
Definition arena.h:360
void RetireBitmap(Bitmap &bitmap)
Explicitly retire resources owned by a bitmap that is being erased.
Definition arena.cc:63
auto mutable_gfx_sheets()
Get mutable reference to all graphics sheets.
Definition arena.h:205
const SheetCacheStats & GetSheetCacheStats() const
Get sheet cache statistics.
Definition arena.h:345
size_t DrainRetiredBitmaps(IRenderer *renderer)
Destroy texture handles retired earlier in the frame.
Definition arena.cc:91
void ResetTextureQueueStats()
Reset texture queue statistics.
Definition arena.h:148
SheetCacheStats sheet_cache_stats_
Definition arena.h:407
std::function< void(const std::string &group_name, int palette_index)> PaletteChangeCallback
Definition arena.h:219
void FreeSurface(SDL_Surface *surface)
Definition arena.cc:479
auto mutable_gfx_sheet(int i)
Get mutable reference to a specific graphics sheet.
Definition arena.h:195
BackgroundBuffer bg1_
Definition arena.h:357
void ProcessTextureQueue(IRenderer *renderer)
Definition arena.cc:211
std::array< gfx::Bitmap, 223 > gfx_sheets_
Definition arena.h:367
bool ProcessSingleTexture(IRenderer *renderer)
Process a single texture command for frame-budget-aware loading.
Definition arena.cc:112
auto & bg2()
Get reference to background layer 2 buffer.
Definition arena.h:255
std::unordered_map< int, std::list< int >::iterator > sheet_lru_map_
Definition arena.h:405
std::array< gfx::Bitmap, 223 > & gfx_sheets()
Get reference to all graphics sheets.
Definition arena.h:177
std::unordered_map< int, PaletteChangeCallback > palette_listeners_
Definition arena.h:398
std::vector< TextureHandle > retired_texture_handles_
Definition arena.h:392
std::vector< TextureCommand > texture_command_queue_
Definition arena.h:391
std::array< uint16_t, kTotalTiles > layer1_buffer_
Definition arena.h:364
bool is_shutdown_
Definition arena.h:394
void NotifySheetModified(int sheet_index)
Notify Arena that a graphics sheet has been modified.
Definition arena.cc:526
auto & bg1()
Get reference to background layer 1 buffer.
Definition arena.h:249
struct yaze::gfx::Arena::SurfacePool surface_pool_
void ResetSheetCacheStats()
Reset sheet cache statistics.
Definition arena.h:352
void UnregisterPaletteListener(int listener_id)
Unregister a palette change listener.
Definition arena.cc:581
TextureQueueStats texture_queue_stats_
Definition arena.h:395
size_t texture_command_queue_size() const
Definition arena.h:165
void Shutdown()
Definition arena.cc:493
void SetSheetCacheSize(size_t max_size)
Set the maximum number of sheet textures to keep cached.
Definition arena.cc:642
static Arena & Get()
Definition arena.cc:24
size_t EvictLRUSheets(size_t count=0)
Evict least recently used sheet textures.
Definition arena.cc:654
size_t GetSheetCacheSize() const
Get current sheet cache size limit.
Definition arena.h:294
std::list< int > sheet_lru_list_
Definition arena.h:403
size_t GetCachedSheetCount() const
Get number of sheets currently with textures.
Definition arena.h:300
int next_palette_listener_id_
Definition arena.h:399
void NotifyPaletteModified(const std::string &group_name, int palette_index=-1)
Notify all listeners that a palette has been modified.
Definition arena.cc:556
const TextureQueueStats & GetTextureQueueStats() const
Get texture queue processing statistics.
Definition arena.h:141
void ClearSheetCache()
Clear all sheet texture cache tracking.
Definition arena.cc:691
void TouchSheet(int sheet_index)
Mark a graphics sheet as recently accessed.
Definition arena.cc:591
Represents a bitmap image optimized for SNES ROM hacking.
Definition bitmap.h:69
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:322
static constexpr size_t MAX_POOL_SIZE
Definition arena.h:388
std::vector< SDL_Surface * > available_surfaces_
Definition arena.h:385
std::unordered_map< SDL_Surface *, std::tuple< int, int, int, int > > surface_info_
Definition arena.h:387
TextureCommandType type
Definition arena.h:57
static constexpr size_t MAX_POOL_SIZE
Definition arena.h:381
std::vector< TextureHandle > available_textures_
Definition arena.h:379
std::unordered_map< TextureHandle, std::pair< int, int > > texture_sizes_
Definition arena.h:380
Statistics for texture queue processing.
Definition arena.h:121