working shader printf w/ formatting & logging

This commit is contained in:
jacob 2025-12-10 22:00:04 -06:00
parent 4fe18195d1
commit 84fbaaf7cd
10 changed files with 65 additions and 59 deletions

View File

@ -537,21 +537,21 @@ String FormatString(Arena *arena, String fmt, FmtArgArray args)
result.text = ArenaNext(arena, u8);
u8 *end = fmt.text + fmt.len;
b32 no_more_args = 0;
b32 no_more_valid_args = 0;
u64 arg_idx = 0;
for (u8 *c = fmt.text; c < end; ++c)
{
u8 *next = ((c + 1) < end) ? (c + 1) : (u8 *)"\0";
/* Escape '%%' */
b32 escape = !no_more_args && *c == '%' && *next == '%';
b32 escape = !no_more_valid_args && *c == '%' && *next == '%';
if (escape)
{
/* Skip the escaped '%' char from parsing */
++c;
}
if (!no_more_args && !escape && *c == '%' && *next == 'F')
if (!no_more_valid_args && !escape && *c == '%' && *next == 'F')
{
String parsed_arg = ZI;
@ -563,7 +563,7 @@ String FormatString(Arena *arena, String fmt, FmtArgArray args)
}
else
{
no_more_args = 1;
no_more_valid_args = 1;
}
switch (arg.kind)
@ -573,7 +573,7 @@ String FormatString(Arena *arena, String fmt, FmtArgArray args)
/* Unknown format type */
Assert(0);
parsed_arg = PushString(arena, Lit("<?>"));
no_more_args = 1;
no_more_valid_args = 1;
} break;
case FmtArgKind_Char:
@ -626,12 +626,12 @@ String FormatString(Arena *arena, String fmt, FmtArgArray args)
/* Unexpected end. Not enough FMT args passed to function. */
Assert(0);
parsed_arg = PushString(arena, Lit("<?>"));
no_more_args = 1;
no_more_valid_args = 1;
} break;
}
/* Update final string len / start */
final_len += parsed_arg.len;
result.len += parsed_arg.len;
/* Skip 'F' from parsing */
++c;
@ -640,32 +640,37 @@ String FormatString(Arena *arena, String fmt, FmtArgArray args)
{
/* Parse character normally */
StringFromChar(arena, *c);
++final_len;
++result.len;
}
}
#if IsRtcEnabled
if (!no_more_args)
if (!no_more_valid_args)
{
FmtArg last_arg = ZI;
if (arg_idx < args.count)
{
last_arg = args.args[arg_idx];
}
/* End arg not reached. Too many args supplied. */
Assert(last_arg.kind == FmtArgKind_End);
}
}
#endif
return result;
}
String FormatStringV_(Arena *arena, String fmt, ...)
String StringF_(Arena *arena, String fmt, ...)
{
String result = ZI;
TempArena scratch = BeginScratch(arena);
{
va_list args;
va_start(args, fmt);
String result = FormatStringVL(arena, fmt, args);
result = FormatString(arena, fmt, FmtArgsFromVaList(scratch.arena, args));
va_end(args);
}
EndScratch(scratch);
return result;
}

View File

@ -117,24 +117,12 @@ String StringFromList(Arena *arena, StringList l, String separator);
#define FmtUid(v, ...) FMTARG(FmtArgKind_Uid, .value.uid = (v), __VA_ARGS__)
#define FmtEnd FMTARG(FmtArgKind_End) /* Denotes end of VA list */
// #define StringF(arena, lit, ...) FormatString_((arena), Lit(lit), __VA_ARGS__, FmtEnd)
// #define FormatString(arena, fmt, ...) FormatString_((arena), (fmt), __VA_ARGS__, FmtEnd)
// String FormatString_(Arena *arena, String fmt, ...);
// String FormatStringV(Arena *arena, String fmt, va_list args);
String FormatString(Arena *arena, String fmt, FmtArgArray args);
String StringF_(Arena *arena, String fmt, ...);
#define StringF(arena, lit, ...) StringF_((arena), Lit(lit), __VA_ARGS__, FmtEnd)
FmtArgArray FmtArgsFromVaList(Arena *arena, va_list vl);
////////////////////////////////////////////////////////////
//~ Unicode

View File

@ -62,15 +62,17 @@ Inline u64 HashFnv64(u64 seed, String s)
#define HashF(fmt_cstr, ...) HashF_(StringFromCstrNoLimit(fmt_cstr), __VA_ARGS__, FmtEnd)
Inline u64 HashF_(String fmt, ...)
{
TempArena scratch = BeginScratchNoConflict();
u64 result = 0;
TempArena scratch = BeginScratchNoConflict();
{
va_list args;
va_start(args, fmt);
{
String str = FormatStringV(scratch.arena, fmt, args);
String str = FormatString(scratch.arena, fmt, FmtArgsFromVaList(scratch.arena, args));
result = HashFnv64(Fnv64Basis, str);
}
va_end(args);
}
EndScratch(scratch);
return result;
}

View File

@ -124,7 +124,7 @@ void LogF_(i32 level, String fmt, ...)
va_list args;
va_start(args, fmt);
{
String msg = FormatStringV(scratch.arena, fmt, args);
String msg = FormatString(scratch.arena, fmt, FmtArgsFromVaList(scratch.arena, args));
W32_Log(level, msg);
}
va_end(args);

View File

@ -2908,7 +2908,7 @@ void G_D12_CollectionWorkerEntry(WaveLaneCtx *lane)
if (args_count > 0)
{
args.args = PushStructs(scratch.arena, FmtArg, args_count);
for (u32 arg_idx = 0; arg_idx <= args_count; ++arg_idx)
for (u32 arg_idx = 0; arg_idx < args_count; ++arg_idx)
{
G_FmtArgKind gpu_kind = (G_FmtArgKind)(*at);
at += 1;
@ -2933,6 +2933,7 @@ void G_D12_CollectionWorkerEntry(WaveLaneCtx *lane)
{
dst->kind = FmtArgKind_Float;
dst->value.f = *(f32 *)&gpu_value;
dst->p = 6;
} break;
}
}
@ -2940,6 +2941,10 @@ void G_D12_CollectionWorkerEntry(WaveLaneCtx *lane)
}
String final_str = FormatString(scratch.arena, fmt, args);
if (GPU_SHADER_PRINT_LOG)
{
LogDebug(final_str);
}
at = (u8 *)AlignU64((u64)at, 4);
}

View File

@ -167,13 +167,14 @@ Struct(G_FmtArg)
u32 base;
rw.InterlockedAdd(0, alloc_size, base);
u32 pos = base;
pos += 4; /* Offset for base counter */
pos += 4; /* Offset for success counter */
pos += 4; /* Offset for overflow counter */
base += 4; /* Offset for allocation counter */
base += 4; /* Offset for success counter */
base += 4; /* Offset for overflow counter */
if ((pos + alloc_size) < countof(rw))
if ((base + alloc_size) < countof(rw))
{
u32 pos = 0;
/* Increment success counter */
rw.InterlockedAdd(4, 1);

View File

@ -28,15 +28,15 @@ UI_Key UI_KeyFromString(String str)
UI_Key UI_KeyF_(String fmt, ...)
{
TempArena scratch = BeginScratchNoConflict();
UI_Key key = ZI;
TempArena scratch = BeginScratchNoConflict();
{
va_list args;
va_start(args, fmt);
{
String name = FormatStringV(scratch.arena, fmt, args);
String name = FormatString(scratch.arena, fmt, FmtArgsFromVaList(scratch.arena, args));
key = UI_KeyFromString(name);
}
va_end(args);
}
EndScratch(scratch);
return key;
}
@ -75,11 +75,16 @@ UI_Box *UI_BoxFromKey(UI_Key key)
String UI_StringF_(String fmt, ...)
{
UI_Frame *frame = UI_CurrentFrame();
String result = ZI;
TempArena scratch = BeginScratchNoConflict();
{
va_list args;
va_start(args, fmt);
String str = FormatStringV(frame->arena, fmt, args);
String str = FormatString(frame->arena, fmt, FmtArgsFromVaList(scratch.arena, args));
va_end(args);
return str;
}
EndScratch(scratch);
return result;
}
////////////////////////////////////////////////////////////

View File

@ -28,17 +28,17 @@ UI_Key UI_BuildLabel(String text)
return key;
}
UI_Key UI_BuildLabelF_(char *fmt_cstr, ...)
UI_Key UI_BuildLabelF_(String fmt, ...)
{
UI_Key key = ZI;
TempArena scratch = BeginScratchNoConflict();
String str = ZI;
{
va_list va;
va_start(va, fmt_cstr);
str = FormatStringV(scratch.arena, StringFromCstrNoLimit(fmt_cstr), va);
va_start(va, fmt);
String str = FormatString(scratch.arena, fmt, FmtArgsFromVaList(scratch.arena, va));
key = UI_BuildLabel(str);
va_end(va);
}
UI_Key key = UI_BuildLabel(str);
EndScratch(scratch);
return key;
}

View File

@ -2,8 +2,8 @@
//~ Label helpers
UI_Key UI_BuildLabel(String text);
#define UI_BuildLabelF(fmt_cstr, ...) UI_BuildLabelF_(fmt_cstr, __VA_ARGS__, FmtEnd)
UI_Key UI_BuildLabelF_(char *fmt_cstr, ...);
#define UI_BuildLabelF(fmt_cstr, ...) UI_BuildLabelF_(Lit(fmt_cstr), __VA_ARGS__, FmtEnd)
UI_Key UI_BuildLabelF_(String fmt, ...);
////////////////////////////////////////////////////////////
//~ Spacing helpers

View File

@ -146,7 +146,7 @@ PixelShader(UI_BlitPS, UI_BlitPSOutput, UI_BlitPSInput input)
Vec4 result = tex.Sample(sampler, uv);
// G_Print("Hello there!");
G_PrintF("Hello there: \"%F\"", G_Fmt(3.123));
G_PrintF("Bla: (%F, %F)", G_Fmt(uv.x), G_Fmt(uv.y));