Skip to content

Commit

Permalink
update bgfx 127
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwu committed May 12, 2024
1 parent acdde15 commit f958779
Show file tree
Hide file tree
Showing 7 changed files with 291 additions and 122 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ BGFXSRC = ../bgfx
BXSRC = ../bx
BIMGSRC = ../bimg
LUAINC = -I/usr/local/include
LUALIB = -L/usr/local/bin -llua54
LUALIB = -L/usr/local/bin -L$(MINGW)/bin -llua54
SDLINC = -I../SDL/include
SDLLIB = -Lbin -lSDL2

Expand Down
3 changes: 2 additions & 1 deletion ant/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ function ant.init(args)
reset = args.reset,
debug = args.debug,
profile = args.profile,
getlog = args.getlog,
pushlog = args.pushlog,
pushlog_context = args.pushlog_context,
numBackBuffers = args.numBackBuffers,
maxFrameLatency = args.maxFrameLatency,

Expand Down
123 changes: 41 additions & 82 deletions luabgfx.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <android/log.h>
#endif

#if BGFX_API_VERSION != 122
#if BGFX_API_VERSION != 127
# error BGFX_API_VERSION mismatch
#endif

Expand Down Expand Up @@ -101,12 +101,14 @@ struct log_cache {
char log[MAX_LOGBUFFER];
};

typedef void (*bgfx_pushlog)(void* context, const char *file, uint16_t line, const char *format, va_list ap);

struct callback {
bgfx_callback_interface_t base;
struct screenshot_queue ss;
struct log_cache lc;
uint32_t filterlog;
bool getlog;
bgfx_pushlog pushlog;
void* pushlog_context;
};

static int
Expand Down Expand Up @@ -272,6 +274,14 @@ getfield(lua_State *L, const char *key) {
return ud;
}

static int
getfield_int(lua_State *L, const char *key) {
lua_getfield(L, 1, key);
int v = lua_tointeger(L, -1);
lua_pop(L, 1);
return v;
}

static int
lsetPlatformData(lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
Expand All @@ -283,6 +293,7 @@ lsetPlatformData(lua_State *L) {
bpdt.context = getfield(L, "context");
bpdt.backBuffer = getfield(L, "backBuffer");
bpdt.backBufferDS = getfield(L, "backBufferDS");
bpdt.type = (bgfx_native_window_handle_type_t)getfield_int(L, "type");

BGFX(set_platform_data)(&bpdt);

Expand All @@ -296,40 +307,15 @@ renderer_type_id(lua_State *L, int index) {
#define RENDERER_TYPE_ID(x) else if (strcmp(type, #x) == 0) id = BGFX_RENDERER_TYPE_##x
if (0) ;
RENDERER_TYPE_ID(NOOP);
RENDERER_TYPE_ID(DIRECT3D9);
RENDERER_TYPE_ID(DIRECT3D11);
RENDERER_TYPE_ID(DIRECT3D12);
RENDERER_TYPE_ID(GNM);
RENDERER_TYPE_ID(METAL);
RENDERER_TYPE_ID(NVN);
RENDERER_TYPE_ID(OPENGLES);
RENDERER_TYPE_ID(OPENGL);
RENDERER_TYPE_ID(VULKAN);
else return luaL_error(L, "Invalid renderer type %s", type);

return id;
}

static void
append_log(struct log_cache *lc, const char * buffer, int n) {
spin_lock(lc);
int sz = (int)(lc->tail - lc->head); // sz must less than MAX_LOGBUFFER
if (sz + n > MAX_LOGBUFFER)
n = MAX_LOGBUFFER - sz;
int offset = lc->tail % MAX_LOGBUFFER;
int part = MAX_LOGBUFFER - offset;
if (part >= n) {
// only one part
memcpy(lc->log + offset, buffer, n);
} else {
// ring buffer rewind
memcpy(lc->log + offset, buffer, part);
memcpy(lc->log, buffer + part, n - part);
}
lc->tail += n;
spin_unlock(lc);
}

static const char*
fatal_code_str(bgfx_fatal_t code) {
switch (code) {
Expand Down Expand Up @@ -385,19 +371,20 @@ trace_filter(const char *format, int level) {

static void
cb_trace_vargs(bgfx_callback_interface_t *self, const char *file, uint16_t line, const char *format, va_list ap) {
char tmp[MAX_LOGBUFFER];
int n = sprintf(tmp, "%s (%d): ", file, line);

n += vsnprintf(tmp+n, sizeof(tmp)-n, format, ap);
if (n > MAX_LOGBUFFER) {
// truncated
n = MAX_LOGBUFFER;
}
struct callback * cb = (struct callback *)self;
if (cb->getlog) {
append_log(&(cb->lc), tmp, n);
}
if (cb->filterlog > 0 && trace_filter(format, cb->filterlog)) {
if (cb->pushlog) {
cb->pushlog(cb->pushlog_context, file, line, format, ap);
return;
}
char tmp[MAX_LOGBUFFER];
int n = sprintf(tmp, "%s (%d): ", file, line);

n += vsnprintf(tmp+n, sizeof(tmp)-n, format, ap);
if (n > MAX_LOGBUFFER) {
// truncated
n = MAX_LOGBUFFER;
}
#if BX_PLATFORM_ANDROID
__android_log_write(ANDROID_LOG_INFO, "bgfx", tmp);
#else
Expand Down Expand Up @@ -634,6 +621,7 @@ linit(lua_State *L) {
init.platformData.context = NULL;
init.platformData.backBuffer = NULL;
init.platformData.backBufferDS = NULL;
init.platformData.type = BGFX_NATIVE_WINDOW_HANDLE_TYPE_DEFAULT;

if (!lua_isnoneornil(L, 1)) {
luaL_checktype(L, 1, LUA_TTABLE);
Expand Down Expand Up @@ -673,19 +661,17 @@ linit(lua_State *L) {
read_uint32(L, 1, "transientIbSize", &init.limits.transientIbSize);
read_boolean(L, 1, "debug", &init.debug);
read_boolean(L, 1, "profile", &init.profile);
read_boolean(L, 1, "getlog", &cb->getlog);
if (cb->getlog) {
cb->filterlog = 0; // log none
} else {
cb->filterlog = 255; // log all
}

read_uint32(L, 1, "loglevel", &cb->filterlog);
cb->pushlog = getfield(L, "pushlog");
cb->pushlog_context = getfield(L, "pushlog_context");

init.platformData.ndt = getfield(L, "ndt");
init.platformData.nwh = getfield(L, "nwh");
init.platformData.context = getfield(L, "context");
init.platformData.backBuffer = getfield(L, "backBuffer");
init.platformData.backBufferDS = getfield(L, "backBufferDS");
init.platformData.type = (bgfx_native_window_handle_type_t)getfield_int(L, "type");

//if (init.debug) {
luabgfx_getalloc(&init.allocator);
Expand All @@ -704,13 +690,9 @@ push_renderer_type(lua_State *L, bgfx_renderer_type_t t) {
#define RENDERER_TYPE(x) case BGFX_RENDERER_TYPE_##x : st = #x; break
switch(t) {
RENDERER_TYPE(NOOP);
RENDERER_TYPE(DIRECT3D9);
RENDERER_TYPE(DIRECT3D11);
RENDERER_TYPE(DIRECT3D12);
RENDERER_TYPE(GNM);
RENDERER_TYPE(METAL);
RENDERER_TYPE(OPENGLES);
RENDERER_TYPE(OPENGL);
RENDERER_TYPE(VULKAN);
default: {
luaL_error(L, "Unknown renderer type %d", t);
Expand Down Expand Up @@ -1112,7 +1094,7 @@ lgetStats(lua_State *L) {
break;
}
default:
return luaL_error(L, "Unkown stat format %c", what[i]);
return luaL_error(L, "Unknown stat format %c", what[i]);
}}
return 1;
}
Expand Down Expand Up @@ -3271,7 +3253,12 @@ ldbgTextImage(lua_State *L) {
int y = luaL_checkinteger(L, 2);
int w = luaL_checkinteger(L, 3);
int h = luaL_checkinteger(L, 4);
const char * image = luaL_checkstring(L, 5);
const char * image;
if (lua_isuserdata(L, 5)) {
image = (const char *)lua_touserdata(L, 5);
} else {
image = luaL_checkstring(L, 5);
}
int pitch = luaL_optinteger(L, 6, 2 * w);
BGFX(dbg_text_image)(x,y,w,h,image, pitch);
return 0;
Expand Down Expand Up @@ -4505,8 +4492,9 @@ lsetViewRect(lua_State *L) {
static int
lsetViewName(lua_State *L) {
bgfx_view_id_t viewid = luaL_checkinteger(L, 1);
const char *name = luaL_checkstring(L, 2);
BGFX(set_view_name)(viewid, name);
size_t sz;
const char *name = luaL_checklstring(L, 2, &sz);
BGFX(set_view_name)(viewid, name, sz);
return 0;
}

Expand Down Expand Up @@ -5061,34 +5049,6 @@ lgetScreenshot(lua_State *L) {
return memptr ? 6 : 5;
}

static int
lgetLog(lua_State *L) {
if (lua_getfield(L, LUA_REGISTRYINDEX, "bgfx_cb") != LUA_TUSERDATA) {
return luaL_error(L, "get_log failed!");
}
struct callback *cb = lua_touserdata(L, -1);
struct log_cache *lc = &cb->lc;
spin_lock(lc);
int offset = lc->head % MAX_LOGBUFFER;
int sz = (int)(lc->tail - lc->head);

int part = MAX_LOGBUFFER - offset;

if (part >= sz) {
// only one part
lua_pushlstring(L, lc->log + offset, sz);
} else {
char tmp[MAX_LOGBUFFER];
memcpy(tmp, lc->log + offset, part);
memcpy(tmp + part, lc->log, sz - part);
lua_pushlstring(L, tmp, sz);
}
lc->head = lc->tail;

spin_unlock(lc);
return 1;
}

#define SET_UNIFORM 0
#define SET_TEXTURE 1
#define SET_BUFFER 2
Expand Down Expand Up @@ -5481,7 +5441,6 @@ luaopen_bgfx(lua_State *L) {
{ "init", linit },
{ "shutdown", lshutdown },

{ "get_log", lgetLog },
{ "get_screenshot", lgetScreenshot },
{ "request_screenshot", lrequestScreenshot },

Expand Down
52 changes: 17 additions & 35 deletions luabgfximgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1294,34 +1294,7 @@ wEndListBox(lua_State *L) {
return 0;
}

static int
get_listitem_func(lua_State *L) {
int n = lua_tointeger(L, 2);
lua_geti(L, 1, n);
return 1;
}

static bool
get_listitem(void* data, int idx, const char **out_text) {
struct lua_args *args = (struct lua_args *)data;
lua_State *L = args->L;
if (args->err)
return 0;
lua_pushcfunction(L, get_listitem_func);
lua_pushvalue(L, INDEX_ARGS);
lua_pushinteger(L, idx+1);
if (lua_pcall(L, 2, 1, 0) != LUA_OK) {
args->err = true;
return 0;
}
if (lua_type(L, -1) == LUA_TSTRING) {
*out_text = lua_tostring(L, -1);
return true;
}
lua_pop(L, 1);
*out_text = NULL;
return false;
}
#define MAX_ITEM 4096

static int
wListBox(lua_State *L) {
Expand All @@ -1331,9 +1304,19 @@ wListBox(lua_State *L) {
int n = lua_tointeger(L, -1);
lua_pop(L, 1);
int height_in_items = read_field_int(L, "height", -1);
struct lua_args args = { L, false };
int current = read_field_int(L, "current", 0) - 1;
bool change = ImGui::ListBox(label, &current, get_listitem, &args, n, height_in_items);
const char * items[MAX_ITEM];
if (n > MAX_ITEM)
n = MAX_ITEM;
int i;
for (i=1;i<=n;i++) {
if (lua_geti(L, INDEX_ARGS, i) != LUA_TSTRING)
return luaL_error(L, "Invalid item at %d", i);
items[i-1] = lua_tostring(L, -1);
lua_pop(L, 1);
}

bool change = ImGui::ListBox(label, &current, items, n, height_in_items);
if (change) {
lua_pushinteger(L, current+1);
lua_setfield(L, INDEX_ARGS, "current");
Expand Down Expand Up @@ -1958,8 +1941,8 @@ uGetItemRectSize(lua_State *L) {
}

static int
uSetItemAllowOverlap(lua_State *L) {
ImGui::SetItemAllowOverlap();
uSetNextItemAllowOverlap(lua_State *L) {
ImGui::SetNextItemAllowOverlap();
return 0;
}

Expand Down Expand Up @@ -2066,7 +2049,7 @@ static struct enum_pair eSelectableFlags[] = {
static struct enum_pair eTreeNodeFlags[] = {
ENUM(ImGuiTreeNodeFlags, Selected),
ENUM(ImGuiTreeNodeFlags, Framed),
ENUM(ImGuiTreeNodeFlags, AllowItemOverlap),
ENUM(ImGuiTreeNodeFlags, AllowOverlap),
ENUM(ImGuiTreeNodeFlags, NoTreePushOnOpen),
ENUM(ImGuiTreeNodeFlags, NoAutoOpenOnLog),
ENUM(ImGuiTreeNodeFlags, DefaultOpen),
Expand Down Expand Up @@ -2097,7 +2080,6 @@ static struct enum_pair eWindowFlags[] = {
ENUM(ImGuiWindowFlags, NoBringToFrontOnFocus),
ENUM(ImGuiWindowFlags, AlwaysVerticalScrollbar),
ENUM(ImGuiWindowFlags, AlwaysHorizontalScrollbar),
ENUM(ImGuiWindowFlags, AlwaysUseWindowPadding),
ENUM(ImGuiWindowFlags, NoNavInputs),
ENUM(ImGuiWindowFlags, NoNavFocus),
ENUM(ImGuiWindowFlags, UnsavedDocument),
Expand Down Expand Up @@ -2305,7 +2287,7 @@ luaopen_bgfx_imgui(lua_State *L) {
{ "GetItemRectMin", uGetItemRectMin },
{ "GetItemRectMax", uGetItemRectMax },
{ "GetItemRectSize", uGetItemRectSize },
{ "SetItemAllowOverlap", uSetItemAllowOverlap },
{ "SetNextItemAllowOverlap", uSetNextItemAllowOverlap },
{ "LoadIniSettings", uLoadIniSettings },
{ "SaveIniSettings", uSaveIniSettings },
{ NULL, NULL },
Expand Down
2 changes: 1 addition & 1 deletion math3d
Loading

0 comments on commit f958779

Please sign in to comment.