bit flag comparison helpers

This commit is contained in:
jacob 2025-11-03 19:48:45 -06:00
parent 7d6fcc8a42
commit e7410b917a
4 changed files with 38 additions and 195 deletions

View File

@ -348,6 +348,12 @@ void __asan_unpoison_memory_region(void const volatile *add, size_t);
#define DllPushFront(f,l,n) DllPushFrontNPZ(0,f,l,n,next,prev) #define DllPushFront(f,l,n) DllPushFrontNPZ(0,f,l,n,next,prev)
#define DllRemove(f,l,n) DllRemoveNPZ(0,f,l,n,next,prev) #define DllRemove(f,l,n) DllRemoveNPZ(0,f,l,n,next,prev)
////////////////////////////////////////////////////////////
//~ Bit helper macros
#define AllBits(a, b) (((a) & (b)) == (b))
#define AnyBit(a, b) (((a) & (b)) != 0)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
//~ Color helper macros //~ Color helper macros

View File

@ -1,163 +0,0 @@
////////////////////////////////////////////////////////////
//~ @hookdef File system hooks
OS_File OS_OpenFile(String path, OS_FileFlag flags, i64 timeout_ns)
{
TempArena scratch = BeginScratchNoConflict();
OS_File result = ZI;
wchar_t *path_wstr = WstrFromString(scratch.arena, path);
u32 share_mode = FILE_SHARE_READ;
u32 create_mode = OPEN_EXISTING;
u32 access_flags = 0;
if (flags & OS_FileFlag_Read) access_flags |= GENERIC_READ;
if (flags & OS_FileFlag_Write) access_flags |= GENERIC_WRITE;
if (flags & OS_FileFlag_Create) create_mode = OPEN_ALWAYS;
i32 timeout_ms = timeout_ns / 1000000;
i32 elapsed_ms = 0;
i32 delay_ms = 1;
HANDLE handle = INVALID_HANDLE_VALUE;
for (;;)
{
handle = CreateFileW(path_wstr, access_flags, share_mode, 0, create_mode, FILE_ATTRIBUTE_NORMAL, 0);
if (handle == INVALID_HANDLE_VALUE && elapsed_ms < timeout_ms && GetLastError() == ERROR_SHARING_VIOLATION)
{
Sleep(delay_ms);
elapsed_ms += delay_ms;
delay_ms = MinI32(delay_ms * 2, 1024);
}
else
{
break;
}
}
EndScratch(scratch);
result.handle = (u64)handle;
return result;
}
void OS_CloseFile(OS_File file)
{
HANDLE handle = (HANDLE)file.handle;
CloseHandle(handle);
}
String OS_ReadEntireFile(Arena *arena, OS_File file)
{
String result = ZI;
HANDLE handle = (HANDLE)file.handle;
u32 chunk_size = Kibi(64);
result.text = PushDry(arena, u8);
for (;;)
{
u8 *chunk = PushStructsNoZero(arena, u8, chunk_size);
u32 chunk_bytes_read = 0;
ReadFile(handle, chunk, chunk_size, &chunk_bytes_read, 0);
result.len += chunk_bytes_read;
if (chunk_bytes_read < chunk_size)
{
PopStructsNoCopy(arena, u8, chunk_size - chunk_bytes_read);
break;
}
}
return result;
}
void OS_ClearWriteFile(OS_File file, String data)
{
HANDLE handle = (HANDLE)file.handle;
SetFilePointer(handle, 0, 0, FILE_BEGIN);
SetEndOfFile(handle);
WriteFile(handle, data.text, data.len, 0, 0);
}
void OS_DirContentsFromFullPath(Arena *arena, StringList *list, String path)
{
TempArena scratch = BeginScratch(arena);
WIN32_FIND_DATAW find_data = ZI;
String filter = StringF(scratch.arena, "%F\\*", FmtString(path));
wchar_t *filter_wstr = WstrFromString(scratch.arena, filter);
HANDLE find_handle = FindFirstFileExW(filter_wstr, FindExInfoStandard, &find_data, FindExSearchNameMatch, 0, FIND_FIRST_EX_CASE_SENSITIVE | FIND_FIRST_EX_LARGE_FETCH);
b32 found = find_handle && find_handle != INVALID_HANDLE_VALUE;
while (found)
{
String file_name = StringFromWstrNoLimit(arena, find_data.cFileName);
if (!EqString(file_name, Lit(".")) && !EqString(file_name, Lit("..")))
{
PushStringToList(arena, list, file_name);
}
found = FindNextFileW(find_handle, &find_data);
}
EndScratch(scratch);
}
String OS_GetFullPath(Arena *arena, String path)
{
TempArena scratch = BeginScratch(arena);
wchar_t *rel_path_wstr = WstrFromString(scratch.arena, path);
wchar_t full_path_wstr_buff[4096];
GetFullPathNameW(rel_path_wstr, countof(full_path_wstr_buff), full_path_wstr_buff, 0);
String res = StringFromWstr(arena, full_path_wstr_buff, countof(full_path_wstr_buff));
EndScratch(scratch);
return res;
}
u64 OS_LastWriteTimestampFromPath(String path)
{
TempArena scratch = BeginScratchNoConflict();
u64 result = 0;
WIN32_FILE_ATTRIBUTE_DATA a = ZI;
wchar_t *path_wstr = WstrFromString(scratch.arena, path);
if (GetFileAttributesExW(path_wstr, GetFileExInfoStandard, &a))
{
result = ((ULONGLONG)a.ftLastWriteTime.dwHighDateTime << 32) |
(ULONGLONG)a.ftLastWriteTime.dwLowDateTime;
}
EndScratch(scratch);
return result;
}
////////////////////////////////////////////////////////////
//~ @hookdef Directory helper hooks
b32 OS_FileOrDirExists(String path)
{
TempArena scratch = BeginScratchNoConflict();
wchar_t *path_wstr = WstrFromString(scratch.arena, path);
DWORD attributes = GetFileAttributesW(path_wstr);
EndScratch(scratch);
return attributes != INVALID_FILE_ATTRIBUTES;
}
b32 OS_FileExists(String path)
{
TempArena scratch = BeginScratchNoConflict();
wchar_t *path_wstr = WstrFromString(scratch.arena, path);
DWORD attributes = GetFileAttributesW(path_wstr);
EndScratch(scratch);
return attributes != INVALID_FILE_ATTRIBUTES && !(attributes & FILE_ATTRIBUTE_DIRECTORY);
}
b32 OS_DirExists(String path)
{
TempArena scratch = BeginScratchNoConflict();
wchar_t *path_wstr = WstrFromString(scratch.arena, path);
DWORD attributes = GetFileAttributesW(path_wstr);
EndScratch(scratch);
return attributes != INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_DIRECTORY);
}
void OS_Mkdir(String path)
{
TempArena scratch = BeginScratchNoConflict();
wchar_t *path_wstr = WstrFromString(scratch.arena, path);
CreateDirectoryW(path_wstr, 0);
EndScratch(scratch);
}
void OS_Rm(String path)
{
TempArena scratch = BeginScratchNoConflict();
wchar_t *path_wstr = WstrFromString(scratch.arena, path);
DeleteFile(path_wstr);
EndScratch(scratch);
}

View File

@ -897,7 +897,7 @@ GPU_Resource *GPU_AcquireResource(GPU_ResourceDesc desc)
d3d_desc.MipLevels = 1; d3d_desc.MipLevels = 1;
d3d_desc.SampleDesc.Count = 1; d3d_desc.SampleDesc.Count = 1;
d3d_desc.SampleDesc.Quality = 0; d3d_desc.SampleDesc.Quality = 0;
d3d_desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS * !!(desc.flags & GPU_ResourceFlag_Writable); d3d_desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS * AnyBit(desc.flags, GPU_ResourceFlag_Writable);
r->state = desc.buffer.heap_kind == GPU_HeapKind_Download ? D3D12_RESOURCE_STATE_COPY_DEST : D3D12_RESOURCE_STATE_COMMON; r->state = desc.buffer.heap_kind == GPU_HeapKind_Download ? D3D12_RESOURCE_STATE_COPY_DEST : D3D12_RESOURCE_STATE_COMMON;
HRESULT hr = ID3D12Device_CreateCommittedResource(g->device, &heap_props, heap_flags, &d3d_desc, r->state, 0, &IID_ID3D12Resource, (void **)&r->d3d_resource); HRESULT hr = ID3D12Device_CreateCommittedResource(g->device, &heap_props, heap_flags, &d3d_desc, r->state, 0, &IID_ID3D12Resource, (void **)&r->d3d_resource);
Atomic64FetchAdd(&g->driver_resources_allocated, 1); Atomic64FetchAdd(&g->driver_resources_allocated, 1);
@ -929,8 +929,8 @@ GPU_Resource *GPU_AcquireResource(GPU_ResourceDesc desc)
d3d_desc.MipLevels = (desc.flags & GPU_ResourceFlag_MaxMipLevels) ? 0 : MaxI32(desc.texture.mip_levels, 1); d3d_desc.MipLevels = (desc.flags & GPU_ResourceFlag_MaxMipLevels) ? 0 : MaxI32(desc.texture.mip_levels, 1);
d3d_desc.SampleDesc.Count = 1; d3d_desc.SampleDesc.Count = 1;
d3d_desc.SampleDesc.Quality = 0; d3d_desc.SampleDesc.Quality = 0;
d3d_desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS * !!(desc.flags & GPU_ResourceFlag_Writable); d3d_desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS * AnyBit(desc.flags, GPU_ResourceFlag_Writable);
d3d_desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET * !!(desc.flags & GPU_ResourceFlag_Renderable); d3d_desc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET * AnyBit(desc.flags, GPU_ResourceFlag_Renderable);
r->state = D3D12_RESOURCE_STATE_COMMON; r->state = D3D12_RESOURCE_STATE_COMMON;
D3D12_CLEAR_VALUE clear_value = { .Format = d3d_desc.Format, .Color = { 0 } }; D3D12_CLEAR_VALUE clear_value = { .Format = d3d_desc.Format, .Color = { 0 } };
clear_value.Color[0] = desc.clear_color.x; clear_value.Color[0] = desc.clear_color.x;

View File

@ -527,7 +527,7 @@ i64 UI_EndBuild(GPU_Resource *render_target, Xform ui_to_screen_xf)
/* Push floating children to dfs stack */ /* Push floating children to dfs stack */
for (UI_Box *child = box->last; child; child = child->prev) for (UI_Box *child = box->last; child; child = child->prev)
{ {
if ((child->flags & UI_BoxFlag_Floating) != 0) if (AnyBit(child->flags, UI_BoxFlag_Floating))
{ {
BoxNode *child_n = PushStruct(scratch.arena, BoxNode); BoxNode *child_n = PushStruct(scratch.arena, BoxNode);
child_n->box = child; child_n->box = child;
@ -537,7 +537,7 @@ i64 UI_EndBuild(GPU_Resource *render_target, Xform ui_to_screen_xf)
/* Push non-floating children to dfs stack */ /* Push non-floating children to dfs stack */
for (UI_Box *child = box->last; child; child = child->prev) for (UI_Box *child = box->last; child; child = child->prev)
{ {
if ((child->flags & UI_BoxFlag_Floating) == 0) if (!AnyBit(child->flags, UI_BoxFlag_Floating))
{ {
BoxNode *child_n = PushStruct(scratch.arena, BoxNode); BoxNode *child_n = PushStruct(scratch.arena, BoxNode);
child_n->box = child; child_n->box = child;
@ -570,7 +570,7 @@ i64 UI_EndBuild(GPU_Resource *render_target, Xform ui_to_screen_xf)
{ {
box->solved_dims[axis] = pref_size.v; box->solved_dims[axis] = pref_size.v;
} }
else if (pref_size.kind == UI_SizeKind_Fit && (box->flags & UI_BoxFlag_DrawText)) else if (pref_size.kind == UI_SizeKind_Fit && AnyBit(box->flags, UI_BoxFlag_DrawText))
{ {
/* TODO: Distinguish between baseline alignment & visual alignment */ /* TODO: Distinguish between baseline alignment & visual alignment */
f32 text_size = 0; f32 text_size = 0;
@ -608,7 +608,7 @@ i64 UI_EndBuild(GPU_Resource *render_target, Xform ui_to_screen_xf)
for (UI_Box *ancestor = box->parent; ancestor; ancestor = ancestor->parent) for (UI_Box *ancestor = box->parent; ancestor; ancestor = ancestor->parent)
{ {
UI_Size tmp = ancestor->pref_size[axis]; UI_Size tmp = ancestor->pref_size[axis];
if (tmp.kind == UI_SizeKind_Pixel || (tmp.kind == UI_SizeKind_Fit && (box->flags & UI_BoxFlag_DrawText))) if (tmp.kind == UI_SizeKind_Pixel || (tmp.kind == UI_SizeKind_Fit && AnyBit(box->flags, UI_BoxFlag_DrawText)))
{ {
ancestor_size = ancestor->solved_dims[axis]; ancestor_size = ancestor->solved_dims[axis];
break; break;
@ -626,12 +626,12 @@ i64 UI_EndBuild(GPU_Resource *render_target, Xform ui_to_screen_xf)
for (Axis axis = 0; axis < Axis_CountXY; ++axis) for (Axis axis = 0; axis < Axis_CountXY; ++axis)
{ {
UI_Size pref_size = box->pref_size[axis]; UI_Size pref_size = box->pref_size[axis];
if (pref_size.kind == UI_SizeKind_Fit && !(box->flags & UI_BoxFlag_DrawText)) if (pref_size.kind == UI_SizeKind_Fit && !AnyBit(box->flags, UI_BoxFlag_DrawText))
{ {
f32 accum = 0; f32 accum = 0;
for (UI_Box *child = box->first; child; child = child->next) for (UI_Box *child = box->first; child; child = child->next)
{ {
if (!(child->flags & UI_BoxFlag_Floating)) if (!AnyBit(child->flags, UI_BoxFlag_Floating))
{ {
if (axis == box->layout_axis) if (axis == box->layout_axis)
{ {
@ -661,7 +661,7 @@ i64 UI_EndBuild(GPU_Resource *render_target, Xform ui_to_screen_xf)
f32 flex_accum = 0; f32 flex_accum = 0;
for (UI_Box *child = box->first; child; child = child->next) for (UI_Box *child = box->first; child; child = child->next)
{ {
if (!(child->flags & UI_BoxFlag_Floating)) if (!AnyBit(child->flags, UI_BoxFlag_Floating))
{ {
f32 size = child->solved_dims[axis]; f32 size = child->solved_dims[axis];
f32 strictness = child->pref_size[axis].strictness; f32 strictness = child->pref_size[axis].strictness;
@ -683,7 +683,7 @@ i64 UI_EndBuild(GPU_Resource *render_target, Xform ui_to_screen_xf)
{ {
for (UI_Box *child = box->first; child; child = child->next) for (UI_Box *child = box->first; child; child = child->next)
{ {
if (!(child->flags & UI_BoxFlag_Floating)) if (!AnyBit(child->flags, UI_BoxFlag_Floating))
{ {
f32 size = child->solved_dims[axis]; f32 size = child->solved_dims[axis];
f32 strictness = child->pref_size[axis].strictness; f32 strictness = child->pref_size[axis].strictness;
@ -706,7 +706,7 @@ i64 UI_EndBuild(GPU_Resource *render_target, Xform ui_to_screen_xf)
/* Solve floating violations */ /* Solve floating violations */
for (UI_Box *child = box->first; child; child = child->next) for (UI_Box *child = box->first; child; child = child->next)
{ {
if ((child->flags & UI_BoxFlag_Floating) && !(child->flags & UI_BoxFlag_NoFloatingClamp)) if (AnyBit(child->flags, UI_BoxFlag_Floating) && !AnyBit(child->flags, UI_BoxFlag_NoFloatingClamp))
{ {
f32 size = child->solved_dims[axis]; f32 size = child->solved_dims[axis];
if (size > box_size) if (size > box_size)
@ -732,11 +732,11 @@ i64 UI_EndBuild(GPU_Resource *render_target, Xform ui_to_screen_xf)
Vec2 final_pos = ZI; Vec2 final_pos = ZI;
/* Floating box position */ /* Floating box position */
if (box->flags & UI_BoxFlag_Floating) if (AnyBit(box->flags, UI_BoxFlag_Floating))
{ {
Vec2 offset = box->floating_pos; Vec2 offset = box->floating_pos;
final_pos = AddVec2(parent->p0, offset); final_pos = AddVec2(parent->p0, offset);
if (!(box->flags & UI_BoxFlag_NoFloatingClamp)) if (!AnyBit(box->flags, UI_BoxFlag_NoFloatingClamp))
{ {
{ {
f32 overshoot = MaxF32(0, (final_pos.x + dims.x) - parent->p1.x); f32 overshoot = MaxF32(0, (final_pos.x + dims.x) - parent->p1.x);
@ -792,7 +792,7 @@ i64 UI_EndBuild(GPU_Resource *render_target, Xform ui_to_screen_xf)
} break; } break;
} }
if (parent && !((box->flags & UI_BoxFlag_Floating) && (box->flags & UI_BoxFlag_NoFloatingClamp))) if (parent && !AllBits(box->flags, UI_BoxFlag_Floating | UI_BoxFlag_NoFloatingClamp))
{ {
Vec2 vtl = SubVec2(VEC2(parent->p0.x, parent->p0.y), VEC2(box->p0.x, box->p0.y)); Vec2 vtl = SubVec2(VEC2(parent->p0.x, parent->p0.y), VEC2(box->p0.x, box->p0.y));
Vec2 vtr = SubVec2(VEC2(parent->p1.x, parent->p0.y), VEC2(box->p1.x, box->p0.y)); Vec2 vtr = SubVec2(VEC2(parent->p1.x, parent->p0.y), VEC2(box->p1.x, box->p0.y));
@ -891,7 +891,7 @@ i64 UI_EndBuild(GPU_Resource *render_target, Xform ui_to_screen_xf)
} }
/* Text rects */ /* Text rects */
if ((box->flags & UI_BoxFlag_DrawText) && box->glyph_run.count > 0 && box->font) if (AnyBit(box->flags, UI_BoxFlag_DrawText) && box->glyph_run.count > 0 && box->font)
{ {
Texture2DRid tex_rid = GPU_Texture2DRidFromResource(box->font->texture); Texture2DRid tex_rid = GPU_Texture2DRidFromResource(box->font->texture);
Vec2 inv_font_image_size = VEC2(1.0f / (f32)box->font->image_width, 1.0f / (f32)box->font->image_height); Vec2 inv_font_image_size = VEC2(1.0f / (f32)box->font->image_width, 1.0f / (f32)box->font->image_height);
@ -908,7 +908,7 @@ i64 UI_EndBuild(GPU_Resource *render_target, Xform ui_to_screen_xf)
b32 should_truncate = run.count > 0 && (run.rects[run.count - 1].pos + run.rects[run.count - 1].advance) > max_baseline; b32 should_truncate = run.count > 0 && (run.rects[run.count - 1].pos + run.rects[run.count - 1].advance) > max_baseline;
/* Truncate run */ /* Truncate run */
if (should_truncate && !(box->flags & UI_BoxFlag_NoTextTruncation)) if (should_truncate && !AnyBit(box->flags, UI_BoxFlag_NoTextTruncation))
{ {
/* Get elipses run */ /* Get elipses run */
F_Run trunc_run = F_RunFromString(g->build_arena, box->font, Lit("...")); F_Run trunc_run = F_RunFromString(g->build_arena, box->font, Lit("..."));