From 6d103adf1529ded5e3d258c93a41a48648e13c80 Mon Sep 17 00:00:00 2001 From: a pirate Date: Sat, 31 Aug 2024 15:11:29 -0700 Subject: [PATCH] Mark chunk_flag as a @bitfield --- .../do_editor_ui_for_compound_type_world.h | 15 +++ generated/do_editor_ui_for_enum_chunk_flag.h | 96 +++++++++++++++++-- .../string_and_value_tables_chunk_flag.h | 43 ++++++++- src/engine/world_chunk.cpp | 4 +- src/engine/world_chunk.h | 4 +- 5 files changed, 150 insertions(+), 12 deletions(-) diff --git a/generated/do_editor_ui_for_compound_type_world.h b/generated/do_editor_ui_for_compound_type_world.h index 281e5677b..ca00fa0fc 100644 --- a/generated/do_editor_ui_for_compound_type_world.h +++ b/generated/do_editor_ui_for_compound_type_world.h @@ -58,6 +58,21 @@ DoEditorUi(renderer_2d *Ui, window_layout *Window, world *Element, cs Name, ui_r PushNewRow(Ui); + DoEditorUi(Ui, + Window, + // Cast to remove const/volatile keywords if they're there + Cast(world_chunk*, Element->ChunkHash), + CSz("ChunkHash"), + Params + ); + + + + + + + + DoEditorUi(Ui, Window, // Cast to remove const/volatile keywords if they're there diff --git a/generated/do_editor_ui_for_enum_chunk_flag.h b/generated/do_editor_ui_for_enum_chunk_flag.h index 7c6e72a78..fca301323 100644 --- a/generated/do_editor_ui_for_enum_chunk_flag.h +++ b/generated/do_editor_ui_for_enum_chunk_flag.h @@ -13,7 +13,21 @@ DoEditorUi(renderer_2d *Ui, window_layout *Window, chunk_flag *Element, cs Name, if (Name.Count) { PushColumn(Ui, CSz("|")); } // Skip the first Name column if (Button(Ui, CSz("Uninitialized"), UiId(Window, "enum Chunk_Uninitialized", Element), Params)) { - *Element = Chunk_Uninitialized; + if (Chunk_Uninitialized == chunk_flag(0)) + { + *Element = chunk_flag(0); + } + else + { + if ((*Element & Chunk_Uninitialized) == Chunk_Uninitialized) + { + *Element = chunk_flag(*Element&~Chunk_Uninitialized); + } + else + { + *Element = chunk_flag(*Element|Chunk_Uninitialized); + } + } SetToggleButton(Ui, ToggleButtonId, False); @@ -22,7 +36,21 @@ DoEditorUi(renderer_2d *Ui, window_layout *Window, chunk_flag *Element, cs Name, if (Name.Count) { PushColumn(Ui, CSz("|")); } // Skip the first Name column if (Button(Ui, CSz("Queued"), UiId(Window, "enum Chunk_Queued", Element), Params)) { - *Element = Chunk_Queued; + if (Chunk_Queued == chunk_flag(0)) + { + *Element = chunk_flag(0); + } + else + { + if ((*Element & Chunk_Queued) == Chunk_Queued) + { + *Element = chunk_flag(*Element&~Chunk_Queued); + } + else + { + *Element = chunk_flag(*Element|Chunk_Queued); + } + } SetToggleButton(Ui, ToggleButtonId, False); @@ -31,7 +59,21 @@ DoEditorUi(renderer_2d *Ui, window_layout *Window, chunk_flag *Element, cs Name, if (Name.Count) { PushColumn(Ui, CSz("|")); } // Skip the first Name column if (Button(Ui, CSz("VoxelsInitialized"), UiId(Window, "enum Chunk_VoxelsInitialized", Element), Params)) { - *Element = Chunk_VoxelsInitialized; + if (Chunk_VoxelsInitialized == chunk_flag(0)) + { + *Element = chunk_flag(0); + } + else + { + if ((*Element & Chunk_VoxelsInitialized) == Chunk_VoxelsInitialized) + { + *Element = chunk_flag(*Element&~Chunk_VoxelsInitialized); + } + else + { + *Element = chunk_flag(*Element|Chunk_VoxelsInitialized); + } + } SetToggleButton(Ui, ToggleButtonId, False); @@ -40,7 +82,21 @@ DoEditorUi(renderer_2d *Ui, window_layout *Window, chunk_flag *Element, cs Name, if (Name.Count) { PushColumn(Ui, CSz("|")); } // Skip the first Name column if (Button(Ui, CSz("Garbage"), UiId(Window, "enum Chunk_Garbage", Element), Params)) { - *Element = Chunk_Garbage; + if (Chunk_Garbage == chunk_flag(0)) + { + *Element = chunk_flag(0); + } + else + { + if ((*Element & Chunk_Garbage) == Chunk_Garbage) + { + *Element = chunk_flag(*Element&~Chunk_Garbage); + } + else + { + *Element = chunk_flag(*Element|Chunk_Garbage); + } + } SetToggleButton(Ui, ToggleButtonId, False); @@ -49,7 +105,21 @@ DoEditorUi(renderer_2d *Ui, window_layout *Window, chunk_flag *Element, cs Name, if (Name.Count) { PushColumn(Ui, CSz("|")); } // Skip the first Name column if (Button(Ui, CSz("Deallocate"), UiId(Window, "enum Chunk_Deallocate", Element), Params)) { - *Element = Chunk_Deallocate; + if (Chunk_Deallocate == chunk_flag(0)) + { + *Element = chunk_flag(0); + } + else + { + if ((*Element & Chunk_Deallocate) == Chunk_Deallocate) + { + *Element = chunk_flag(*Element&~Chunk_Deallocate); + } + else + { + *Element = chunk_flag(*Element|Chunk_Deallocate); + } + } SetToggleButton(Ui, ToggleButtonId, False); @@ -58,7 +128,21 @@ DoEditorUi(renderer_2d *Ui, window_layout *Window, chunk_flag *Element, cs Name, if (Name.Count) { PushColumn(Ui, CSz("|")); } // Skip the first Name column if (Button(Ui, CSz("Freelist"), UiId(Window, "enum Chunk_Freelist", Element), Params)) { - *Element = Chunk_Freelist; + if (Chunk_Freelist == chunk_flag(0)) + { + *Element = chunk_flag(0); + } + else + { + if ((*Element & Chunk_Freelist) == Chunk_Freelist) + { + *Element = chunk_flag(*Element&~Chunk_Freelist); + } + else + { + *Element = chunk_flag(*Element|Chunk_Freelist); + } + } SetToggleButton(Ui, ToggleButtonId, False); diff --git a/generated/string_and_value_tables_chunk_flag.h b/generated/string_and_value_tables_chunk_flag.h index 7aed96c36..eb56e1f0e 100644 --- a/generated/string_and_value_tables_chunk_flag.h +++ b/generated/string_and_value_tables_chunk_flag.h @@ -13,7 +13,30 @@ ToStringPrefixless(chunk_flag Type) case Chunk_Deallocate: { Result = CSz("Deallocate"); } break; case Chunk_Freelist: { Result = CSz("Freelist"); } break; - + // TODO(Jesse): This is pretty barf and we could do it in a single allocation, + // but the metaprogram might have to be a bit fancier.. + default: + { + u32 CurrentFlags = u32(Type); + + if (CountBitsSet_Kernighan(CurrentFlags) == 1) + { + Result = FSz("(invalid value for chunk_flag (%d))", CurrentFlags); + } + else + { + u32 FirstValue = UnsetLeastSignificantSetBit(&CurrentFlags); + Result = ToStringPrefixless(chunk_flag(FirstValue)); + + while (CurrentFlags) + { + u32 Value = UnsetLeastSignificantSetBit(&CurrentFlags); + cs Next = ToStringPrefixless(chunk_flag(Value)); + Result = FSz("%S | %S", Result, Next); + } + } + } break; + } /* if (Result.Start == 0) { Info("Could not convert value(%d) to (EnumType.name)", Type); } */ return Result; @@ -32,7 +55,23 @@ ToString(chunk_flag Type) case Chunk_Deallocate: { Result = CSz("Chunk_Deallocate"); } break; case Chunk_Freelist: { Result = CSz("Chunk_Freelist"); } break; - + // TODO(Jesse): This is pretty barf and we could do it in a single allocation, + // but the metaprogram might have to be a bit fancier.. + default: + { + u32 CurrentFlags = u32(Type); + + u32 FirstValue = UnsetLeastSignificantSetBit(&CurrentFlags); + Result = ToString(chunk_flag(FirstValue)); + + while (CurrentFlags) + { + u32 Value = UnsetLeastSignificantSetBit(&CurrentFlags); + cs Next = ToString(chunk_flag(Value)); + Result = FSz("%S | %S", Result, Next); + } + } break; + } /* if (Result.Start == 0) { Info("Could not convert value(%d) to (EnumType.name)", Type); } */ return Result; diff --git a/src/engine/world_chunk.cpp b/src/engine/world_chunk.cpp index 6152155b5..dc8a4db63 100644 --- a/src/engine/world_chunk.cpp +++ b/src/engine/world_chunk.cpp @@ -54,7 +54,7 @@ FinalizeChunkInitialization(world_chunk *Chunk) FullBarrier; /* UnSetFlag(Chunk, Chunk_Garbage); */ - /* UnSetFlag(&Chunk->Flags, Chunk_Queued); */ + UnSetFlag(&Chunk->Flags, Chunk_Queued); SetFlag(&Chunk->Flags, Chunk_VoxelsInitialized); } @@ -3455,7 +3455,7 @@ InitializeWorldChunkEmpty(world_chunk *DestChunk) #endif FinalizeChunkInitialization(DestChunk); - UnSetFlag(&DestChunk->Flags, Chunk_Queued); + /* UnSetFlag(&DestChunk->Flags, Chunk_Queued); */ return; } diff --git a/src/engine/world_chunk.h b/src/engine/world_chunk.h index bde0dd2c4..0e6a40032 100644 --- a/src/engine/world_chunk.h +++ b/src/engine/world_chunk.h @@ -62,7 +62,7 @@ enum pick_chunk_state PickedChunkState_Hover, }; -enum chunk_flag +enum chunk_flag poof(@bitfield) { Chunk_Uninitialized = 0 << 0, @@ -436,7 +436,7 @@ struct world u32 HashSlotsUsed; u32 HashSize; world_chunk **ChunkHashMemory[2]; poof(@ui_skip) - world_chunk **ChunkHash; poof(@ui_skip) + world_chunk **ChunkHash; poof(@array_length(Element->HashSize)) bonsai_futex ChunkFreelistFutex; poof(@ui_skip)