From a41a9513ca4d1526f9e211d975fce43450cfa96b Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Sun, 5 Sep 2021 11:29:23 +1000 Subject: [PATCH 01/29] working lua --- Makefile | 16 +++++++++++++--- src/Makefile | 14 +++++++++----- src/main.c | 14 +++++++++++++- 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index b7fd655..d9146d5 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,8 @@ DOCKER_IMG=ps2build DOCKERFLAGS=--user "$(shell id -u):$(shell id -g)" DOCKER?=sudo docker +LUA_BRANCH=ee-v5.4.4 + include .lintvars dist: $(BIN) assets @@ -24,7 +26,7 @@ $(BIN): src/test.elf .PHONY: src/test.elf src/test.elf: - $(MAKE) PLATFORM=ps2 -C src test.elf + $(MAKE) platform=PS2 -C src test.elf # TODO(phy1um): update ISO building to include everything in dist/ @@ -33,11 +35,11 @@ $(ISO_TGT): $(EE_BIN) .PHONY: docker-elf docker-elf: - $(DOCKER) run -v $(shell pwd):/src $(DOCKER_IMG) make $(BIN) + $(DOCKER) run $(DOCKERFLAGS) -v $(shell pwd):/src $(DOCKER_IMG) make $(BIN) .PHONY: clean -clean: +clean: $(MAKE) -C src clean $(MAKE) -C asset clean rm -rf dist/ @@ -63,4 +65,12 @@ lint: format: $(DOCKER) run $(DOCKERFLAGS) -v $(shell pwd):/workdir unibeautify/clang-format -i -sort-includes **/*.c **/*.h +deps: + git clone https://github.com/ps2dev/lua --depth 1 --branch $(LUA_BRANCH) --single-branch src/lua + +lua-samples: + $(DOCKER) run $(DOCKERFLAGS) -v $(shell pwd):/src $(DOCKER_IMG) make -C src/lua/sample + +docker-image: + $(DOCKER) build -t $(DOCKER_IMG) . diff --git a/src/Makefile b/src/Makefile index 0aab0d6..3b963f9 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,12 +1,14 @@ PS2SDK=/usr/local/ps2dev/ps2sdk EE_BIN=test.elf -EE_OBJS=main.o gs.o mesh.o draw.o math.o pad.o -EE_LIBS=-ldma -lgraph -ldraw -lkernel -ldebug -lmath3d -lm -lpad -EE_CFLAGS += -Wall --std=c99 -EE_LDFLAGS = -L$(PSDSDK)/ee/common/lib -L$(PS2SDK)/ee/lib +EE_OBJS=main.o gs.o mesh.o draw.o math.o pad.o testlua.o +EE_LIBS=-ldma -lgraph -ldraw -lkernel -ldebug -lmath3d -lm -lpad -llua +EE_CFLAGS+=-Wall --std=c99 -Wno-sign-compare -fno-strict-aliasing -fno-exceptions -DLUA_USE_PS2 -ifeq ($(PLATFORM), ps2) +EE_INCS+=-I$(PS2SDK)/ports/include +EE_LDFLAGS=-L$(PSDSDK)/ee/common/lib -L$(PS2SDK)/ee/lib -L$(PS2SDK)/ports/lib + +ifeq ($(platform), PS2) include $(PS2SDK)/samples/Makefile.eeglobal include $(PS2SDK)/samples/Makefile.pref endif @@ -14,5 +16,7 @@ endif .PHONY: clean clean: rm -rf $(EE_BIN) $(EE_OBJS) + make -C lua -f makefile clean + diff --git a/src/main.c b/src/main.c index 880f8df..abd7b7b 100644 --- a/src/main.c +++ b/src/main.c @@ -43,7 +43,11 @@ int print_buffer(qword_t *b, int len) { return 0; } +int script_init(); + + int main() { + printf("Hello\n"); qword_t *buf = malloc(20000 * 16); char *file_load_buffer = malloc(310 * 1024); @@ -54,7 +58,7 @@ int main() { st.gmode = GRAPH_MODE_INTERLACED, // init DMAC - dma_channel_initialize(DMA_CHANNEL_GIF, 0, 0); + dma_channel_initialize(DMA_CHANNEL_GIF, 0, 0); dma_channel_fast_waits(DMA_CHANNEL_GIF); // initialize graphics mode @@ -98,6 +102,8 @@ int main() { pad_init(); graph_wait_vsync(); + + while (1) { pad_frame_start(); pad_poll(); @@ -143,6 +149,12 @@ int main() { r.camera_pos[2] += 0.2f * dz; // r.camera_pos[1] += 0.1f * dy; r.camera_rotate_y += 0.01f * dy; + + if ( button_held(BUTTON_1) ) { + int sr = script_init(); + info("SCRIPT LOADING: %d", sr); + } + } } From 2299b399cf0ce3b442dc4e72addff21b6158f374 Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Sun, 5 Sep 2021 16:15:20 +1000 Subject: [PATCH 02/29] add simple lua GS interface --- src/Makefile | 2 +- src/drawstate.c | 50 ++++++++++++++++++++++++++++++++++ src/drawstate.h | 18 ++++++++++++ src/luadrawstate.c | 33 ++++++++++++++++++++++ src/main.c | 68 +++++++++++++++++----------------------------- src/ps2draw.h | 2 ++ src/script.c | 47 ++++++++++++++++++++++++++++++++ src/script.h | 18 ++++++++++++ 8 files changed, 194 insertions(+), 44 deletions(-) create mode 100644 src/drawstate.c create mode 100644 src/drawstate.h create mode 100644 src/luadrawstate.c create mode 100644 src/script.c create mode 100644 src/script.h diff --git a/src/Makefile b/src/Makefile index 3b963f9..dab9cd9 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,7 +1,7 @@ PS2SDK=/usr/local/ps2dev/ps2sdk EE_BIN=test.elf -EE_OBJS=main.o gs.o mesh.o draw.o math.o pad.o testlua.o +EE_OBJS=main.o gs.o mesh.o draw.o math.o pad.o drawstate.o script.o luadrawstate.o EE_LIBS=-ldma -lgraph -ldraw -lkernel -ldebug -lmath3d -lm -lpad -llua EE_CFLAGS+=-Wall --std=c99 -Wno-sign-compare -fno-strict-aliasing -fno-exceptions -DLUA_USE_PS2 diff --git a/src/drawstate.c b/src/drawstate.c new file mode 100644 index 0000000..fe52012 --- /dev/null +++ b/src/drawstate.c @@ -0,0 +1,50 @@ + +#include +#include + +#include "drawstate.h" +#include "ps2draw.h" +#include "gs.h" + +static struct draw_state st = {0}; +static struct render_state r = {0}; + +int drawstate_init(int w, int h, int i) { + st.width = w; + st.height = h; + st.vmode = graph_get_region(); + st.gmode = i; + return gs_init(&st, GS_PSM_32, GS_PSMZ_24); +} + +int drawstate_camera_step(float x, float y, float z) { + r.camera_pos[0] += x; + r.camera_pos[1] += y; + r.camera_pos[2] += z; +} + +int drawstate_camera_jump(float x, float y, float z) { + r.camera_pos[0] = x; + r.camera_pos[1] = y; + r.camera_pos[2] = z; +} + +int drawstate_camera_rotate(float x, float y, float z) { + r.camera_rotate_y += y; +} + +qword_t * drawstate_ztest(qword_t *q, int enable) { + if ( enable ) { + return draw_enable_tests(q, 0, &st.zb); + } else { + return draw_disable_tests(q, 0, &st.zb); + } +} + +struct render_state *drawstate_get() { + return &r; +} + +struct draw_state *drawstate_gs_state() { + return &st; +} diff --git a/src/drawstate.h b/src/drawstate.h new file mode 100644 index 0000000..91cb257 --- /dev/null +++ b/src/drawstate.h @@ -0,0 +1,18 @@ + +#ifndef SRC_DRAWSTATE_H +#define SRC_DRAWSTATE_H + +#include "ps2draw.h" +#include "gs.h" + +#include + +int drawstate_init(int w, int h, int i); +int drawstate_camera_step(float x, float y, float z); +int drawstate_camera_jump(float x, float y, float z); +int drawstate_camera_rotate(float x, float y, float z); +qword_t * drawstate_ztest(qword_t *q, int enable); +struct render_state *drawstate_get(); +struct draw_state *drawstate_gs_state(); + +#endif diff --git a/src/luadrawstate.c b/src/luadrawstate.c new file mode 100644 index 0000000..f2d39d2 --- /dev/null +++ b/src/luadrawstate.c @@ -0,0 +1,33 @@ +#include +#include "script.h" +#include "ps2draw.h" +#include "drawstate.h" + +static int lua_gs_init(lua_State *L) { + int width = lua_tointeger(L, 1); + int height = lua_tointeger(L, 2); + int interlace = lua_tointeger(L, 3); + drawstate_init(width, height, interlace); +} + +static int lua_camera_step(lua_State *L) { + double x = lua_tonumber(L, 1); + double y = lua_tonumber(L, 2); + double z = lua_tonumber(L, 3); + drawstate_camera_step(x,y,z); +} + +static int lua_camera_jump(lua_State *L) { + double x = lua_tonumber(L, 1); + double y = lua_tonumber(L, 2); + double z = lua_tonumber(L, 3); + drawstate_camera_jump(x,y,z); +} + +int lua_drawstate_init(lua_State *L) { + LUA_DEF(gs_init); + LUA_DEF(camera_step); + LUA_DEF(camera_jump); +} + + diff --git a/src/main.c b/src/main.c index abd7b7b..e1cc181 100644 --- a/src/main.c +++ b/src/main.c @@ -8,8 +8,6 @@ #include #include #include -#include -#include #include @@ -18,6 +16,8 @@ #include "mesh.h" #include "pad.h" #include "ps2draw.h" +#include "drawstate.h" +#include "script.h" #define OFFSET_X 2048 #define OFFSET_Y 2048 @@ -25,7 +25,7 @@ #define VID_W 640 #define VID_H 448 -#define TGT_FILE "host:cube.bin" +#define TGT_FILE "host:MIT_teapot.bin" #define fatalerror(st, msg, ...) \ printf("FATAL: " msg "\n", ##__VA_ARGS__); \ @@ -43,9 +43,6 @@ int print_buffer(qword_t *b, int len) { return 0; } -int script_init(); - - int main() { printf("Hello\n"); @@ -53,43 +50,37 @@ int main() { char *file_load_buffer = malloc(310 * 1024); int file_load_buffer_len = 310 * 1024; - struct draw_state st = {0}; - st.width = VID_W, st.height = VID_H, st.vmode = graph_get_region(), - st.gmode = GRAPH_MODE_INTERLACED, - // init DMAC dma_channel_initialize(DMA_CHANNEL_GIF, 0, 0); dma_channel_fast_waits(DMA_CHANNEL_GIF); - // initialize graphics mode - gs_init(&st, GS_PSM_32, GS_PSMZ_24); + // TODO: make constant in lua out of this + // st.gmode = GRAPH_MODE_INTERLACED; + void *lua = script_load("host:entry.lua"); + script_simple_call(lua, "ps2_init"); struct model m = {0}; m.r = 0xff; int bytes_read = load_file(TGT_FILE, file_load_buffer, file_load_buffer_len); if (bytes_read <= 0) { - fatalerror(&st, "failed to load file %s", TGT_FILE); + fatalerror(drawstate_gs_state(), "failed to load file %s", TGT_FILE); } if (bytes_read % 16 != 0) { - fatalerror(&st, "lengt of model file %s was not 0 %% 16", TGT_FILE); + fatalerror(drawstate_gs_state(), "lengt of model file %s was not 0 %% 16", TGT_FILE); } if (!model_load(&m, file_load_buffer, bytes_read)) { - fatalerror(&st, "failed to process model"); + fatalerror(drawstate_gs_state(), "failed to process model"); } - struct render_state r = {0}; - - r.camera_pos[0] = 0.0f; - r.camera_pos[2] = 1.0f; - r.camera_pos[3] = 1.0f; + struct render_state *r = drawstate_get(); - r.clear_col[0] = 0xb1; - r.clear_col[1] = 0xce; - r.clear_col[2] = 0xcb; + r->clear_col[0] = 0xb1; + r->clear_col[1] = 0xce; + r->clear_col[2] = 0xcb; - r.offset_x = OFFSET_X; - r.offset_y = OFFSET_Y; + r->offset_x = OFFSET_X; + r->offset_y = OFFSET_Y; struct model_instance inst = {0}; inst.m = &m; @@ -103,7 +94,6 @@ int main() { graph_wait_vsync(); - while (1) { pad_frame_start(); pad_poll(); @@ -111,21 +101,18 @@ int main() { dma_wait_fast(); qword_t *q = buf; memset(buf, 0, 20000 * 16); - q = draw_disable_tests(q, 0, &st.zb); + q = drawstate_ztest(q, 0); q = draw_clear(q, 0, 2048.0f - 320, 2048.0f - 244, VID_W, VID_H, - r.clear_col[0], r.clear_col[1], r.clear_col[2]); - q = draw_enable_tests(q, 0, &st.zb); - if (mesh_is_visible(&inst, &r)) { + r->clear_col[0], r->clear_col[1], r->clear_col[2]); + q = drawstate_ztest(q, 1); + if (mesh_is_visible(&inst, r)) { qword_t *model_verts_start = q; memcpy(q, m.buffer, m.buffer_len); - // info("copied mesh buffer with len=%d", m.buffer_len); - mesh_transform((char *)(model_verts_start + MESH_HEADER_SIZE), &inst, &r); + mesh_transform((char *)(model_verts_start + MESH_HEADER_SIZE), &inst, r); q += (m.buffer_len / 16); } q = draw_finish(q); dma_channel_send_normal(DMA_CHANNEL_GIF, buf, q - buf, 0, 0); - // print_buffer(buf, q-buf); - // info("draw from buffer with length %d", q-buf); draw_wait_finish(); graph_wait_vsync(); @@ -145,15 +132,10 @@ int main() { int dy = button_held(BUTTON_L1) - button_held(BUTTON_L2); #endif - r.camera_pos[0] += 0.2f * dx; - r.camera_pos[2] += 0.2f * dz; - // r.camera_pos[1] += 0.1f * dy; - r.camera_rotate_y += 0.01f * dy; - - if ( button_held(BUTTON_1) ) { - int sr = script_init(); - info("SCRIPT LOADING: %d", sr); - } + r->camera_pos[0] += 0.2f * dx; + r->camera_pos[2] += 0.2f * dz; + // r->camera_pos[1] += 0.1f * dy; + r->camera_rotate_y += 0.01f * dy; } } diff --git a/src/ps2draw.h b/src/ps2draw.h index c4ee26d..a26b299 100644 --- a/src/ps2draw.h +++ b/src/ps2draw.h @@ -5,6 +5,8 @@ #define MESH_HEADER_SIZE 3 #define myftoi4(x) (((uint64_t)(x)) << 4) +#include + struct render_state { float offset_x; float offset_y; diff --git a/src/script.c b/src/script.c new file mode 100644 index 0000000..2d07b85 --- /dev/null +++ b/src/script.c @@ -0,0 +1,47 @@ +#include +#include +#include + +#include "script.h" +#include "log.h" + +struct lua_State * script_load(const char *file) { + info("init lua with entrypoint %s", file); + struct lua_State *L; + L = luaL_newstate(); + if ( !L ) { + logerr("failed to start lua state"); + return 0; + } + luaL_openlibs(L); + lua_drawstate_init(L); + + int rc = luaL_loadfile(L, file); + if ( rc ) { + logerr("failed to execute lua file %s", file); + return 0; + } + rc = lua_pcall(L, 0, 0, 0); + if ( rc ) { + const char *err = lua_tostring(L, -1); + logerr("lua execution error -- %s", err); + return 0; + } + return L; +} + +int script_simple_call(struct lua_State *L, const char *fn) { + lua_getglobal(L, fn); + if ( lua_pcall(L, 0, 0, 0) != 0 ) { + const char *err = lua_tostring(L, -1); + logerr("lua error calling %s -- %s", fn, err); + return 1; + } + return 0; +} + +int script_end(struct lua_State *L) { + lua_close(L); + return 0; +} + diff --git a/src/script.h b/src/script.h new file mode 100644 index 0000000..1a2a185 --- /dev/null +++ b/src/script.h @@ -0,0 +1,18 @@ + +#ifndef SRC_LUA_H +#define SRC_LUA_H + +#include + +#define LUA_DEF(fn) \ + do { \ + lua_pushcfunction(L, lua_ ## fn); \ + lua_setglobal(L, #fn); } while(0) + +struct lua_State * script_load(const char *file); +int script_simple_call(struct lua_State *L, const char *fn); +int script_end(struct lua_State *L); + +int lua_drawstate_init(lua_State *L); + +#endif From 5b321054e04d6d206564713a50d8129299f24c5a Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Thu, 7 Oct 2021 10:42:55 +1100 Subject: [PATCH 03/29] add gs/lua abstraction --- src/gslua.c | 154 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 src/gslua.c diff --git a/src/gslua.c b/src/gslua.c new file mode 100644 index 0000000..14f9d1a --- /dev/null +++ b/src/gslua.c @@ -0,0 +1,154 @@ + +#include + +#include "gs.h" + +#define gs_state draw_state + +struct gs_state { + framebuffer_t fb; + zbuffer_t zb; + char clear_r; + char clear_g; + char clear_b; +}; + +static int gslua_vram_alloc(lua_State *l) { + int width = lua_tointeger(l, 2); + int height = lua_tointeger(l, 3); + int format = lua_tointeger(l, 4); + int addr = graph_vram_allocate(width, height, format, GRAPH_ALIGN_PAGE); + lua_createtable(l, 0, 4); + lua_pushinteger(l, addr); + lua_setfield(l, -2, "address"); + lua_pushinteger(l, width); + lua_setfield(l, -2, "width"); + lua_pushinteger(l, height); + lua_setfield(l, -2, "height"); + lua_pushinteger(l, format); + lua_setfield(l, -2, "format"); + return 1; +} + +static int gslua_set_buffers(lua_State *l) { + // get fields from SELF argument (#1) + lua_pushstring(l, "state"); + lua_gettable(l, 1); + struct gs_state *st = (struct gs_state *) lua_touserdata(l, -1); + lua_pop(l, 1); + // get fields from FB argument (#2) + lua_pushstring(l, "width"); + lua_gettable(l, 2); + int fb_width = lua_tointeger(l, -1); + lua_pop(l, 1); + lua_pushstring(l, "height"); + lua_gettable(l, 2); + int fb_height = lua_tointeger(l, -1); + lua_pop(l, 1); + lua_pushstring(l, "address"); + lua_gettable(l, 2); + int fb_addr = lua_tointeger(l, -1); + lua_pop(l, 1); + lua_pushstring(l, "format"); + lua_gettable(l, 2); + int fb_fmt = lua_tointeger(l, -1); + lua_pop(l, 1); + // get fields from ZB argument (#3) + lua_pushstring(l, "width"); + lua_gettable(l, 3); + int zb_width = lua_tointeger(l, -1); + lua_pop(l, 1); + lua_pushstring(l, "height"); + lua_gettable(l, 3); + int zb_height = lua_tointeger(l, -1); + lua_pop(l, 1); + lua_pushstring(l, "address"); + lua_gettable(l, 3); + int zb_addr = lua_tointeger(l, -1); + lua_pop(l, 1); + lua_pushstring(l, "format"); + lua_gettable(l, 3); + int zb_fmt = lua_tointeger(l, -1); + lua_pop(l, 1); + + if (!st) { + // TODO: error + } + + st->fb.address = fb_addr; + st->fb.width = fb_width; + st->fb.height = fb_height; + st->fb.psm = fb_fmt; + st->fb.mask = 0; + st->zb.address = zb_addr; + st->zb.width = zb_width; + st->zb.height = zb_height; + st->zb.zsm = zb_fmt; + st->zb.method = ZTEST_METHOD_GREATER; + st->zb.mask = 0; + graph_set_framebuffer_filtered(st->fb.address, fb_width, fb_fmt, 0, 0); + graph_enable_output(); + +} + +static int gslua_new_state(lua_State *l) { + int width = lua_tointeger(l, 1); + int height = lua_tointeger(l, 2); + int interlace = lua_tointeger(l, 3); + int mode = lua_tointeger(l, 4); + // create table with GS state + lua_createtable(l, 0, 5); + struct gs_state *st = + (struct gs_state *) lua_newuserdata(l, sizeof(struct gs_state)); + lua_setfield(l, -2, "state"); + lua_pushinteger(l, width); + lua_setfield(l, -2, "width"); + lua_pushinteger(l, height); + lua_setfield(l, -2, "height"); + lua_pushinteger(l, interlace); + lua_setfield(l, -2, "interlace"); + lua_pushinteger(l, mode); + lua_setfield(l, -2, "mode"); + + lua_pushcfunction(l, gslua_vram_alloc); + lua_setfield(l, -2, "alloc"); + lua_pushcfunction(l, gslua_set_buffers); + lua_setfield(l, -2, "setBuffers"); + // initialize GS with no output + graph_disable_output(); + gs_set_mode(interlace, mode, GFAPH_MODE_FIELD, GRAPH_DISABLE); + gs_set_screen(0, 0, width, height); + gs_set_bgcolor(0, 0, 0); + return 1; +} + +#define bind(n, b) \ + lua_pushinteger(l, b);\ + lua_setfield(l, -2, n) +int gslua_init(lua_State *l) { + lua_createtable(l, 0, 16); + lua_pushcfunction(l, gslua_new_state); + lua_setfield(l, -2, "newState"); + + bind("PSM4", GS_PSM_4); + bind("PSM4HL", GS_PSM_4HL); + bind("PSM4HH", GS_PSM_4HH); + bind("PSM8", GS_PSM_8); + bind("PSM8H", GS_PSM_8H); + bind("PSM16", GS_PSM_16); + bind("PSM16S", GS_PSM_16S); + bind("PSM24", GS_PSM_24); + bind("PSMPS24", GS_PSM_PS24); + bind("PSM32", GS_PSM_32); + + bind("PSMZ16", GS_PSMZ_16); + bind("PSMZ16S", GS_PSMZ_16S); + bind("PSMZ24", GS_PSMZ_24); + bind("PSMZ32", GS_PSMZ_32); + + // bind this table to global name GS, nothing left on stack + lua_setglobal("GS"); + return 0; +} + + From 6466d672e333a341ebf2e39df2bdd89998c557c9 Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Sat, 9 Oct 2021 10:54:22 +1100 Subject: [PATCH 04/29] simple lua example of hardware iface --- src/Makefile | 3 +- src/dmalua.c | 44 ++++++++++++++ src/drawlua.c | 160 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/ps2lua.c | 103 ++++++++++++++++++++++++++++++++ 4 files changed, 309 insertions(+), 1 deletion(-) create mode 100644 src/dmalua.c create mode 100644 src/drawlua.c create mode 100644 src/ps2lua.c diff --git a/src/Makefile b/src/Makefile index dab9cd9..5479e19 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,7 +1,8 @@ PS2SDK=/usr/local/ps2dev/ps2sdk EE_BIN=test.elf -EE_OBJS=main.o gs.o mesh.o draw.o math.o pad.o drawstate.o script.o luadrawstate.o +#EE_OBJS=main.o gs.o mesh.o draw.o math.o pad.o drawstate.o script.o luadrawstate.o +EE_OBJS=ps2lua.o gslua.o dmalua.o drawlua.o EE_LIBS=-ldma -lgraph -ldraw -lkernel -ldebug -lmath3d -lm -lpad -llua EE_CFLAGS+=-Wall --std=c99 -Wno-sign-compare -fno-strict-aliasing -fno-exceptions -DLUA_USE_PS2 diff --git a/src/dmalua.c b/src/dmalua.c new file mode 100644 index 0000000..df78a8d --- /dev/null +++ b/src/dmalua.c @@ -0,0 +1,44 @@ +#include + +#include +#include + +static int dma_init(lua_State *l) { + int channel = lua_tointeger(l, 1); + dma_channel_initialize(channel, 0, 0); + dma_channel_fast_waits(channel); + return 0; +} + +static int dma_send_buffer(lua_State *l) { + // buffer is arg 1 + lua_pushstring(l, "ptr"); + lua_gettable(l, 1); + void *ptr = lua_touserdata(l, -1); + lua_pushstring(l, "head"); + lua_gettable(l, 1); + int head = lua_tointeger(l, -1); + + // channel is arg 2 + int channel = lua_tointeger(l, 1); + + dma_channel_send_normal(channel, ptr, head/16, 0, 0); + return 0; +} + +static int dma_wait(lua_State *l) { + dma_wait_fast(); + return 0; +} + +int dma_lua_init(lua_State *l) { + lua_createtable(l, 0, 5); + lua_pushcfunction(l, dma_wait); + lua_setfield(l, -2, "waitFast"); + lua_pushcfunction(l, dma_send_buffer); + lua_setfield(l, -2, "send"); + lua_pushcfunction(l, "dma_init"); + lua_setfield(l, -2, "init"); + lua_setglobal(l, "DMA"); + return 0; +} diff --git a/src/drawlua.c b/src/drawlua.c new file mode 100644 index 0000000..1d09808 --- /dev/null +++ b/src/drawlua.c @@ -0,0 +1,160 @@ +#include +#include + +#include +#include + +#include + +#include "log.h" +#include "script.h" + + +static int drawlua_new_drawbuffer(lua_State *l); + +static const unsigned int DRAW_BUFFER_MAX_SIZE = 2 * 1024; +char *static_draw_buffer; + +static int buffer_pushint(lua_State *l) { + int value = lua_tointeger(l, 2); + lua_pushstring(l, "ptr"); + lua_gettable(l, 1); + int *ptr = (int *) lua_touserdata(l, -1); + lua_pushstring(l, "head"); + lua_gettable(l, 1); + int head = lua_tointeger(l, -1); + // TODO: check size + // ASSUME 4byte int + if (head%4 != 0) { + // TODO: manually bitmask etc + logerr("cannot write int to buffer, head%%4 != 0"); + return 0; + } + ptr[head/4] = value; + lua_pushinteger(l, head+1); + lua_setfield(l, 1, "head"); + return 0; +} + +static int buffer_copy(lua_State *l) { + info("UNIMPLEMENTED"); + return 0; +} + +int drawlua_init(lua_State *l) { + luaL_newmetatable(l, "ps2.buffer"); + lua_createtable(l, 0, 5); + lua_pushcfunction(l, buffer_pushint); + lua_setfield(l, -2, "pushint"); + lua_pushcfunction(l, buffer_copy); + lua_setfield(l, -2, "copy"); + lua_setfield(l, -2, "__index"); + lua_pop(l, 1); + + lua_createtable(l, 0, 8); + lua_pushcfunction(l, drawlua_new_drawbuffer); + lua_setfield(l, -2, "getDrawBuffer"); + lua_setglobal(l, "RM"); + + info("allocating static draw buffer"); + static_draw_buffer = malloc(DRAW_BUFFER_MAX_SIZE); + + return 0; +} + + +static int drawlua_start_frame(lua_State *l) { + // drawbuffer is arg #1 + lua_pushstring(l, "head"); + lua_gettable(l, 1); + int head = lua_tointeger(l, -1); + lua_pushstring(l, "size"); + lua_gettable(l, 1); + int size = lua_tointeger(l, -1); + lua_pushstring(l, "ptr"); + lua_gettable(l, 1); + char *ptr = (char *) lua_touserdata(l, -1); + + // gs is arg #2 + lua_pushstring(l, "state"); + lua_gettable(l, 2); + struct gs_state *st = (struct gs_state *) lua_touserdata(l, -1); + lua_pushstring(l, "width"); + lua_gettable(l, 2); + int width = lua_tointeger(l, -1); + lua_pushstring(l, "height"); + lua_gettable(l, 2); + int height = lua_tointeger(l, -1); + + float halfw = st->fb.width / 2.f; + float halfh = st->fb.height / 2.f; + + qword_t *q = (qword_t *) (ptr + head); + q = draw_disable_tests(q, 0, &st->zb); + q = draw_clear(q, 0, 2048.0f - halfw, 2048.0f - halfh, width, height, + st->clear_r, st->clear_g, st->clear_b); + q = draw_enable_tests(q, 0, &st->zb); + + head = (char*)q - ptr; + lua_pushinteger(l, head); + lua_setfield(l, 1, "head"); + return 0; +} + +static int drawlua_end_frame(lua_State *l) { + // drawbuffer is arg #1 + lua_pushstring(l, "head"); + lua_gettable(l, 1); + int head = lua_tointeger(l, -1); + /* + lua_pushstring("size"); + lua_gettable(l, 1); + int size = lua_tointeger(l, -1); + */ + lua_pushstring(l, "ptr"); + lua_gettable(l, 1); + char *ptr = (char *) lua_touserdata(l, -1); + + // gs is arg #2 + lua_pushstring(l, "state"); + lua_gettable(l, 2); + + struct gs_state *st = (struct gs_state *) lua_touserdata(l, -1); + + qword_t *q = (qword_t *) (ptr + head); + q = draw_finish(q); + + head = (char*)q - ptr; + lua_pushinteger(l, head); + lua_setfield(l, 1, "head"); + return 0; +} + +static int drawbuffer_free(lua_State *l) { + return 0; +} + +// TODO: document this can only be called ONCE +static int drawlua_new_drawbuffer(lua_State *l) { + int size = lua_tointeger(l, 1); + if ( size >= DRAW_BUFFER_MAX_SIZE ) { + // TODO: error + } + lua_createtable(l, 0, 5); + lua_pushinteger(l, size); + lua_setfield(l, -2, "size"); + lua_pushinteger(l, 0); + lua_setfield(l, -2, "head"); + char *buf = static_draw_buffer; + lua_pushlightuserdata(l, buf); + lua_setfield(l, -2, "ptr"); + lua_pushcfunction(l, drawlua_start_frame); + lua_setfield(l, -2, "frameStart"); + lua_pushcfunction(l, drawlua_end_frame); + lua_setfield(l, -2, "frameEnd"); + luaL_getmetatable(l, "ps2.buffer"); + lua_pushcfunction(l, drawbuffer_free); + lua_setfield(l, -2, "free"); + lua_setmetatable(l, -2); + return 1; +} diff --git a/src/ps2lua.c b/src/ps2lua.c new file mode 100644 index 0000000..4e1a4ef --- /dev/null +++ b/src/ps2lua.c @@ -0,0 +1,103 @@ +#include +#include +#include + +#include +#include + +#include "log.h" + +#include "script.h" + +static int ps2luaprog_start_nil(lua_State *l) { + return 0; +} + +static int ps2luaprog_frame_nil(lua_State *l) { + return 0; +} + +int ps2luaprog_init(lua_State *l) { + lua_createtable(l, 0, 2); + lua_pushcfunction(l, ps2luaprog_start_nil); + lua_setfield(l, -2, "start"); + lua_pushcfunction(l, ps2luaprog_frame_nil); + lua_setfield(l, -2, "frame"); + lua_setglobal(l, "PS2PROG"); + return 0; +} + +int ps2luaprog_onstart(lua_State *l) { + lua_getglobal(l, "PS2PROG"); + lua_pushstring(l, "start"); + lua_gettable(l, -2); + lua_call(l, 0, 0); + /* + if ( rc ) { + const char *err = lua_tostring(L, -1); + logerr("lua execution error -- %s", err); + } + */ + return 0; +} + +int ps2luaprog_onframe(lua_State *l) { + lua_getglobal(l, "PS2PROG"); + lua_pushstring(l, "frame"); + lua_gettable(l, -2); + lua_call(l, 0, 0); + /* + if ( rc ) { + const char *err = lua_tostring(l, -1); + logerr("lua execution error -- %s", err); + } + */ + return 0; +} + +int ps2luaprog_is_running(lua_State *l) { + return 1; +} + +int main(int argc, char *argv[]) { + info("startup"); + struct lua_State *L; + L = luaL_newstate(); + if ( !L ) { + logerr("failed to start lua state"); + return -1; + } + luaL_openlibs(L); + + ps2luaprog_init(L); + dma_lua_init(L); + gs_lua_init(L); + + + //TODO: better abstraction for drawlua_* + drawlua_init(L); + + info("finished lua state setup"); + + info("loading file script/init.lua"); + + int rc = luaL_loadfile(L, "host:script/min.lua"); + if ( rc ) { + logerr("failed to execute lua file %s", "host:script/min.lua"); + return -1; + } + + rc = lua_pcall(L, 0, 0, 0); + if ( rc ) { + const char *err = lua_tostring(L, -1); + logerr("lua execution error -- %s", err); + return -1; + } + + ps2luaprog_onstart(L); + while( ps2luaprog_is_running(L) ) { + ps2luaprog_onframe(L); + draw_wait_finish(); + graph_wait_vsync(); + } +} From be2065df2c8286096c4135523c21c1a6e7202b72 Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Sun, 10 Oct 2021 00:44:09 +1100 Subject: [PATCH 05/29] add entire wip state --- Makefile | 7 ++++++- indirect.py | 31 +++++++++++++++++++++++++++++++ script/example.lua | 46 ++++++++++++++++++++++++++++++++++++++++++++++ script/min.lua | 20 ++++++++++++++++++++ src/gs.c | 20 ++++++++++++++++++++ src/gs.h | 1 + src/gslua.c | 42 ++++++++++++------------------------------ src/luadrawstate.c | 28 ++++++++++++++++++++++++++++ src/main.c | 22 ++++++++++++---------- src/script.c | 6 ++++++ src/script.h | 19 +++++++++++-------- 11 files changed, 193 insertions(+), 49 deletions(-) create mode 100644 indirect.py create mode 100644 script/example.lua create mode 100644 script/min.lua diff --git a/Makefile b/Makefile index d9146d5..8d30ee3 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ PS2HOST?=192.168.20.99 DOCKER_IMG=ps2build DOCKERFLAGS=--user "$(shell id -u):$(shell id -g)" -DOCKER?=sudo docker +DOCKER?=docker LUA_BRANCH=ee-v5.4.4 @@ -29,6 +29,11 @@ src/test.elf: $(MAKE) platform=PS2 -C src test.elf +.PHONY: scripts +scripts: + if ! [ -d dist/script ]; then mkdir -p dist/script; fi + cp script/* dist/script + # TODO(phy1um): update ISO building to include everything in dist/ $(ISO_TGT): $(EE_BIN) mkisofs -l -o $(ISO_TGT) $(BIN) dist/SYSTEM.CNF diff --git a/indirect.py b/indirect.py new file mode 100644 index 0000000..2c20bc8 --- /dev/null +++ b/indirect.py @@ -0,0 +1,31 @@ + +import socket +import subprocess +import sys + +SERVER_IP = "0.0.0.0" +SERVER_PORT = int(sys.argv[1]) +is_running = True + +def run_command(msg): + parts = list(map(lambda x: x.decode("utf-8").strip(), msg.split(b" "))) + print("got cmd " + parts[0]) + if parts[0] == "make": + subprocess.run(parts, stdout=cl, stderr=cl) + else: + print("no matching action for cmd") + +server = socket.socket (socket.AF_INET, socket.SOCK_STREAM) +server.bind((SERVER_IP, SERVER_PORT)) +server.listen(5) + +try: + while is_running == True: + cl, addr = server.accept() + print("got connection from client") + msg = cl.recv(1024) + print("running message") + output = run_command(msg) + cl.close() +finally: + server.close() diff --git a/script/example.lua b/script/example.lua new file mode 100644 index 0000000..532341b --- /dev/null +++ b/script/example.lua @@ -0,0 +1,46 @@ + +local scene = {} +local cubeMesh = {} + +function PS2PROG.entrypoint() + DMA.initGif() + local gs = GS.newState(640, 448, GS.INTERLACED, GS.NTSC) + local fb = gs:alloc(640, 448, GS.PSM24) + local zb = gs:alloc(640, 448, GS.PSMZ24) + gs:setBuffers(fb, zb) + scene = DRAW.newScene() + cubeMesh = RM.loadMesh("host:cube.bin", RM.MESH_FORMAT_GIF1) + for i=0,i<10;i++ do + local mi = mesh:instance() + mi:translate(0, 0, -100 + 10 * i) + mi:scale(0.4, 0.4, 0.4) + scene:addInstance(mi) + end + scene:cameraAt(0, 0, -5) + scene:lookAt(0, 0, -1) + + PS2PROG.bindGsState(gs) +end + +function PS2PROG.onframe(gs) + local db = RM.getDrawBuffer(10000) + db:frameStart(gs) + for i,v in ipairs(scene:meshes) do + local giftag = db:pushGifTag() + local ih = db:head() + giftag:fromMesh(v) + db:pushVertexData(v) + scene:transformVerts(db, ih + v:headerSize(), v:vertexCount()) + end + db:frameEnd(gs) + DMA.sendBuffer(db, DMA.TO_GIF) + db:free() +end + +function PS2PROG.exit() + scene:destroy() + cubeMesh:free() + gs:done() + PS2.exit_program() +end + diff --git a/script/min.lua b/script/min.lua new file mode 100644 index 0000000..db3ce8b --- /dev/null +++ b/script/min.lua @@ -0,0 +1,20 @@ + +local gs = nil + +function PS2PROG.start() + DMA.init(DMA.GIF) + gs = GS.newState(640, 448, GS.INTERLACED, GS.NTSC) + local fb = gs:alloc(640, 448, GS.PSM24) + local zb = gs:alloc(640, 448, GS.PSMZ24) + gs:setBuffers(fb, zb) +end + +function PS2PROG.onframe() + local db = RM.getDrawBuffer(10000) + db:frameStart(gs) + db:frameEnd(gs) + DMA.sendBufferNormal(db, DMA.GIF) + db:free() +end + + diff --git a/src/gs.c b/src/gs.c index 454a5b2..bef668a 100644 --- a/src/gs.c +++ b/src/gs.c @@ -55,3 +55,23 @@ int gs_init(struct draw_state *ds, int psm, int psmz) { return 0; } + +qword_t * gs_frame_start(struct draw_state *ds, qword_t *q) { + if (!ds) { + logerror("cannot run frame_start on NULL draw state"); + return q; + } + + float halfw = ds->width / 2.f; + float halfh = ds->height / 2.f; + + q = draw_disable_tests(q, 0, &ds->zb); + q = draw_clear(q, 0, 2048.0f - halfw, 2048.0f - half, ds->width, ds->height, + ds->clear_col[0], ds->clear_col[1], ds->clear_col[2]); + return draw_enable_tests(q, 0, &ds->zb); + +} + +qword_t * gs_frame_end(struct draw_state *ds, qword_t *q) { + return draw_finish(q); +} diff --git a/src/gs.h b/src/gs.h index 26afd2e..4886f16 100644 --- a/src/gs.h +++ b/src/gs.h @@ -11,6 +11,7 @@ struct draw_state { int gmode; framebuffer_t fb; zbuffer_t zb; + char clear_col[3]; }; int gs_init(struct draw_state *ds, int psm, int psmz); diff --git a/src/gslua.c b/src/gslua.c index 14f9d1a..1bc99e9 100644 --- a/src/gslua.c +++ b/src/gslua.c @@ -1,17 +1,11 @@ - #include -#include "gs.h" - -#define gs_state draw_state +#include +#include +#include +#include -struct gs_state { - framebuffer_t fb; - zbuffer_t zb; - char clear_r; - char clear_g; - char clear_b; -}; +#include "script.h" static int gslua_vram_alloc(lua_State *l) { int width = lua_tointeger(l, 2); @@ -54,18 +48,9 @@ static int gslua_set_buffers(lua_State *l) { int fb_fmt = lua_tointeger(l, -1); lua_pop(l, 1); // get fields from ZB argument (#3) - lua_pushstring(l, "width"); - lua_gettable(l, 3); - int zb_width = lua_tointeger(l, -1); - lua_pop(l, 1); - lua_pushstring(l, "height"); - lua_gettable(l, 3); - int zb_height = lua_tointeger(l, -1); - lua_pop(l, 1); lua_pushstring(l, "address"); lua_gettable(l, 3); int zb_addr = lua_tointeger(l, -1); - lua_pop(l, 1); lua_pushstring(l, "format"); lua_gettable(l, 3); int zb_fmt = lua_tointeger(l, -1); @@ -81,14 +66,12 @@ static int gslua_set_buffers(lua_State *l) { st->fb.psm = fb_fmt; st->fb.mask = 0; st->zb.address = zb_addr; - st->zb.width = zb_width; - st->zb.height = zb_height; st->zb.zsm = zb_fmt; st->zb.method = ZTEST_METHOD_GREATER; st->zb.mask = 0; graph_set_framebuffer_filtered(st->fb.address, fb_width, fb_fmt, 0, 0); graph_enable_output(); - + return 0; } static int gslua_new_state(lua_State *l) { @@ -98,8 +81,7 @@ static int gslua_new_state(lua_State *l) { int mode = lua_tointeger(l, 4); // create table with GS state lua_createtable(l, 0, 5); - struct gs_state *st = - (struct gs_state *) lua_newuserdata(l, sizeof(struct gs_state)); + lua_newuserdata(l, sizeof(struct gs_state)); lua_setfield(l, -2, "state"); lua_pushinteger(l, width); lua_setfield(l, -2, "width"); @@ -116,16 +98,16 @@ static int gslua_new_state(lua_State *l) { lua_setfield(l, -2, "setBuffers"); // initialize GS with no output graph_disable_output(); - gs_set_mode(interlace, mode, GFAPH_MODE_FIELD, GRAPH_DISABLE); - gs_set_screen(0, 0, width, height); - gs_set_bgcolor(0, 0, 0); + graph_set_mode(interlace, mode, GRAPH_MODE_FIELD, GRAPH_DISABLE); + graph_set_screen(0, 0, width, height); + graph_set_bgcolor(0, 0, 0); return 1; } #define bind(n, b) \ lua_pushinteger(l, b);\ lua_setfield(l, -2, n) -int gslua_init(lua_State *l) { +int gs_lua_init(lua_State *l) { lua_createtable(l, 0, 16); lua_pushcfunction(l, gslua_new_state); lua_setfield(l, -2, "newState"); @@ -147,7 +129,7 @@ int gslua_init(lua_State *l) { bind("PSMZ32", GS_PSMZ_32); // bind this table to global name GS, nothing left on stack - lua_setglobal("GS"); + lua_setglobal(l, "GS"); return 0; } diff --git a/src/luadrawstate.c b/src/luadrawstate.c index f2d39d2..f392703 100644 --- a/src/luadrawstate.c +++ b/src/luadrawstate.c @@ -3,6 +3,9 @@ #include "ps2draw.h" #include "drawstate.h" +#include +#include + static int lua_gs_init(lua_State *L) { int width = lua_tointeger(L, 1); int height = lua_tointeger(L, 2); @@ -28,6 +31,31 @@ int lua_drawstate_init(lua_State *L) { LUA_DEF(gs_init); LUA_DEF(camera_step); LUA_DEF(camera_jump); + return 0; +} + +int lua_drawstate_constant_table(lua_State *L) { + lua_createtable(L, 0, 4); + FIELD_INT("INTERLACED", GRAPH_MODE_INTERLACED); + FIELD_INT("NON_INTERLACED", GRAPH_MODE_NONINTERLACED); + FIELD_INT("PSM32", GS_PSM_32); + FIELD_INT("PSMZ24", GS_PSMZ_24); + lua_setglobal(L, "GS"); + return 1; } +// GS.interlaced / nonInterlaced +// +int lua_drawstate_new(lua_State *L, int w, int h, int i) { + lua_createtable(L, 0, 4); + FIELD_INT("interlace", i); + FIELD_INT("width", w); + FIELD_INT("height", h); + FIELD_INT("ztest", 1); + FIELD_CFUNC("init", lua_gs_init); + return 1; +} + + + diff --git a/src/main.c b/src/main.c index e1cc181..f4faedb 100644 --- a/src/main.c +++ b/src/main.c @@ -54,8 +54,6 @@ int main() { dma_channel_initialize(DMA_CHANNEL_GIF, 0, 0); dma_channel_fast_waits(DMA_CHANNEL_GIF); - // TODO: make constant in lua out of this - // st.gmode = GRAPH_MODE_INTERLACED; void *lua = script_load("host:entry.lua"); script_simple_call(lua, "ps2_init"); @@ -73,11 +71,12 @@ int main() { fatalerror(drawstate_gs_state(), "failed to process model"); } + struct draw_state *draw_state = drawstate_gs_state(); struct render_state *r = drawstate_get(); - r->clear_col[0] = 0xb1; - r->clear_col[1] = 0xce; - r->clear_col[2] = 0xcb; + draw_state->clear_col[0] = 0xb1; + draw_state->clear_col[1] = 0xce; + draw_state->clear_col[2] = 0xcb; r->offset_x = OFFSET_X; r->offset_y = OFFSET_Y; @@ -95,23 +94,26 @@ int main() { graph_wait_vsync(); while (1) { + pad_frame_start(); pad_poll(); update_draw_matrix(&r); dma_wait_fast(); + qword_t *q = buf; memset(buf, 0, 20000 * 16); - q = drawstate_ztest(q, 0); - q = draw_clear(q, 0, 2048.0f - 320, 2048.0f - 244, VID_W, VID_H, - r->clear_col[0], r->clear_col[1], r->clear_col[2]); - q = drawstate_ztest(q, 1); + + q = gs_frame_start(draw_state, q); + if (mesh_is_visible(&inst, r)) { qword_t *model_verts_start = q; memcpy(q, m.buffer, m.buffer_len); mesh_transform((char *)(model_verts_start + MESH_HEADER_SIZE), &inst, r); q += (m.buffer_len / 16); } - q = draw_finish(q); + + q = gs_frame_end(draw_state, q); + dma_channel_send_normal(DMA_CHANNEL_GIF, buf, q - buf, 0, 0); draw_wait_finish(); diff --git a/src/script.c b/src/script.c index 2d07b85..08b988f 100644 --- a/src/script.c +++ b/src/script.c @@ -45,3 +45,9 @@ int script_end(struct lua_State *L) { return 0; } +void script_define_constants(struct lua_State *L) { + lua_pushinteger(L, 0); + lua_setglobal(L, "GS_NONINTERLACED"); + lua_pushinteger(L, 1); + lua_setglobal(L, "GS_INTERLACED"); +} diff --git a/src/script.h b/src/script.h index 1a2a185..3ba25ed 100644 --- a/src/script.h +++ b/src/script.h @@ -4,15 +4,18 @@ #include -#define LUA_DEF(fn) \ - do { \ - lua_pushcfunction(L, lua_ ## fn); \ - lua_setglobal(L, #fn); } while(0) +struct gs_state { + framebuffer_t fb; + zbuffer_t zb; + char clear_r; + char clear_g; + char clear_b; +}; -struct lua_State * script_load(const char *file); -int script_simple_call(struct lua_State *L, const char *fn); -int script_end(struct lua_State *L); -int lua_drawstate_init(lua_State *L); + +int gs_lua_init(lua_State *l); +int dma_lua_init(lua_State *l); +int drawlua_init(lua_State *l); #endif From 798e57084d9a18c5b7f813f41d267500782d29ca Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Sun, 10 Oct 2021 06:40:01 +1100 Subject: [PATCH 06/29] working base draw example --- script/min.lua | 5 +++-- src/dmalua.c | 32 ++++++++++++++++++++++++++++++-- src/drawlua.c | 6 ++++-- src/gslua.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/ps2lua.c | 28 +++++++++++++++++++--------- src/script.h | 6 +++--- 6 files changed, 108 insertions(+), 18 deletions(-) diff --git a/script/min.lua b/script/min.lua index db3ce8b..c4c0279 100644 --- a/script/min.lua +++ b/script/min.lua @@ -7,13 +7,14 @@ function PS2PROG.start() local fb = gs:alloc(640, 448, GS.PSM24) local zb = gs:alloc(640, 448, GS.PSMZ24) gs:setBuffers(fb, zb) + gs:clearColour(255, 0, 0) end -function PS2PROG.onframe() +function PS2PROG.frame() local db = RM.getDrawBuffer(10000) db:frameStart(gs) db:frameEnd(gs) - DMA.sendBufferNormal(db, DMA.GIF) + DMA.send(db, DMA.GIF) db:free() end diff --git a/src/dmalua.c b/src/dmalua.c index df78a8d..a9e1f78 100644 --- a/src/dmalua.c +++ b/src/dmalua.c @@ -2,14 +2,29 @@ #include #include +#include + +#include "log.h" static int dma_init(lua_State *l) { int channel = lua_tointeger(l, 1); + info("doing dma init, channel = %d", channel); dma_channel_initialize(channel, 0, 0); dma_channel_fast_waits(channel); return 0; } +int print_buffer(qword_t *b, int len) { + info("-- buffer\n"); + for (int i = 0; i < len; i++) { + printf("%016llx %016llx\n", b->dw[0], b->dw[1]); + b++; + } + info("-- /buffer\n"); + return 0; +} + + static int dma_send_buffer(lua_State *l) { // buffer is arg 1 lua_pushstring(l, "ptr"); @@ -20,14 +35,19 @@ static int dma_send_buffer(lua_State *l) { int head = lua_tointeger(l, -1); // channel is arg 2 - int channel = lua_tointeger(l, 1); + int channel = lua_tointeger(l, 2); + // print buffer for debugging + print_buffer(ptr, head/16); + + info("DMA send :: sending %d qwords on channel %d", head/16, channel); dma_channel_send_normal(channel, ptr, head/16, 0, 0); return 0; } static int dma_wait(lua_State *l) { dma_wait_fast(); + return 0; } @@ -37,8 +57,16 @@ int dma_lua_init(lua_State *l) { lua_setfield(l, -2, "waitFast"); lua_pushcfunction(l, dma_send_buffer); lua_setfield(l, -2, "send"); - lua_pushcfunction(l, "dma_init"); + lua_pushcfunction(l, dma_init); lua_setfield(l, -2, "init"); + + lua_pushinteger(l, DMA_CHANNEL_GIF); + lua_setfield(l, -2, "GIF"); + lua_pushinteger(l, DMA_CHANNEL_VIF0); + lua_setfield(l, -2, "VIF0"); + lua_pushinteger(l, DMA_CHANNEL_VIF1); + lua_setfield(l, -2, "VIF1"); + lua_setglobal(l, "DMA"); return 0; } diff --git a/src/drawlua.c b/src/drawlua.c index 1d09808..deeeb82 100644 --- a/src/drawlua.c +++ b/src/drawlua.c @@ -89,6 +89,8 @@ static int drawlua_start_frame(lua_State *l) { float halfw = st->fb.width / 2.f; float halfh = st->fb.height / 2.f; + info("clear screen :: (%d, %d, %d)", st->clear_r, st->clear_g, st->clear_b); + qword_t *q = (qword_t *) (ptr + head); q = draw_disable_tests(q, 0, &st->zb); q = draw_clear(q, 0, 2048.0f - halfw, 2048.0f - halfh, width, height, @@ -152,9 +154,9 @@ static int drawlua_new_drawbuffer(lua_State *l) { lua_setfield(l, -2, "frameStart"); lua_pushcfunction(l, drawlua_end_frame); lua_setfield(l, -2, "frameEnd"); - luaL_getmetatable(l, "ps2.buffer"); lua_pushcfunction(l, drawbuffer_free); lua_setfield(l, -2, "free"); - lua_setmetatable(l, -2); + luaL_getmetatable(l, "ps2.buffer"); + lua_setmetatable(l, -2); return 1; } diff --git a/src/gslua.c b/src/gslua.c index 1bc99e9..8153898 100644 --- a/src/gslua.c +++ b/src/gslua.c @@ -4,8 +4,31 @@ #include #include #include +#include + +#include +#include #include "script.h" +#include "log.h" + +static int gslua_clear_col(lua_State *l) { + lua_pushstring(l, "state"); + lua_gettable(l, 1); + struct gs_state *st = (struct gs_state *) lua_touserdata(l, -1); + lua_pop(l, 1); + if ( !st ) { + logerr("GS state was NULL"); + return 0; + } + int r = lua_tointeger(l, 2); + int g = lua_tointeger(l, 3); + int b = lua_tointeger(l, 4); + st->clear_r = r; + st->clear_g = g; + st->clear_b = b; + return 0; +} static int gslua_vram_alloc(lua_State *l) { int width = lua_tointeger(l, 2); @@ -71,6 +94,20 @@ static int gslua_set_buffers(lua_State *l) { st->zb.mask = 0; graph_set_framebuffer_filtered(st->fb.address, fb_width, fb_fmt, 0, 0); graph_enable_output(); + + // init draw state + qword_t *head = malloc(20 * 16); + memset(head, 0, 20*16); + qword_t *q = head; + q = draw_setup_environment(q, 0, &st->fb, &st->zb); + q = draw_primitive_xyoffset(q, 0, 2048 - (st->fb.width / 2), + 2048 - (st->fb.height / 2)); + q = draw_finish(q); + dma_channel_send_normal(DMA_CHANNEL_GIF, head, q - head, 0, + 0); + draw_wait_finish(); + free(head); + return 0; } @@ -96,6 +133,8 @@ static int gslua_new_state(lua_State *l) { lua_setfield(l, -2, "alloc"); lua_pushcfunction(l, gslua_set_buffers); lua_setfield(l, -2, "setBuffers"); + lua_pushcfunction(l, gslua_clear_col); + lua_setfield(l, -2, "clearColour"); // initialize GS with no output graph_disable_output(); graph_set_mode(interlace, mode, GRAPH_MODE_FIELD, GRAPH_DISABLE); @@ -128,6 +167,16 @@ int gs_lua_init(lua_State *l) { bind("PSMZ24", GS_PSMZ_24); bind("PSMZ32", GS_PSMZ_32); + bind("NONINTERLACED", 1); + bind("INTERLACED", 1); + + bind("AUTO", GRAPH_MODE_AUTO); + bind("NTSC", GRAPH_MODE_NTSC); + bind("PAL", GRAPH_MODE_PAL); + bind("HDTV_480P", GRAPH_MODE_HDTV_480P); + bind("HDTV_576P", GRAPH_MODE_HDTV_576P); + bind("HDTV_720P", GRAPH_MODE_HDTV_720P); + // bind this table to global name GS, nothing left on stack lua_setglobal(l, "GS"); return 0; diff --git a/src/ps2lua.c b/src/ps2lua.c index 4e1a4ef..1fefee9 100644 --- a/src/ps2lua.c +++ b/src/ps2lua.c @@ -3,6 +3,7 @@ #include #include +#include #include #include "log.h" @@ -10,6 +11,7 @@ #include "script.h" static int ps2luaprog_start_nil(lua_State *l) { + info("default start..."); return 0; } @@ -31,13 +33,14 @@ int ps2luaprog_onstart(lua_State *l) { lua_getglobal(l, "PS2PROG"); lua_pushstring(l, "start"); lua_gettable(l, -2); - lua_call(l, 0, 0); - /* + int type = lua_type(l, -1); + info("start fn has type :: %s (%d)", lua_typename(l, type), type); + int rc = lua_pcall(l, 0, 0, 0); if ( rc ) { - const char *err = lua_tostring(L, -1); - logerr("lua execution error -- %s", err); + const char *err = lua_tostring(l, -1); + logerr("lua execution error (start event) -- %s", err); } - */ + return 0; } @@ -45,13 +48,15 @@ int ps2luaprog_onframe(lua_State *l) { lua_getglobal(l, "PS2PROG"); lua_pushstring(l, "frame"); lua_gettable(l, -2); - lua_call(l, 0, 0); - /* + int type = lua_type(l, -1); + info("frame fn has type :: %s (%d)", lua_typename(l, type), type); + int rc = lua_pcall(l, 0, 0, 0); + if ( rc ) { const char *err = lua_tostring(l, -1); - logerr("lua execution error -- %s", err); + logerr("lua execution error (frame event) -- %s", err); } - */ + return 0; } @@ -96,8 +101,13 @@ int main(int argc, char *argv[]) { ps2luaprog_onstart(L); while( ps2luaprog_is_running(L) ) { + dma_wait_fast(); + info("ON FRAME"); ps2luaprog_onframe(L); + info("WAIT DRAW"); draw_wait_finish(); + info("WAIT VSYNC"); graph_wait_vsync(); } + info("main loop ended"); } diff --git a/src/script.h b/src/script.h index 3ba25ed..505ca82 100644 --- a/src/script.h +++ b/src/script.h @@ -7,9 +7,9 @@ struct gs_state { framebuffer_t fb; zbuffer_t zb; - char clear_r; - char clear_g; - char clear_b; + unsigned char clear_r; + unsigned char clear_g; + unsigned char clear_b; }; From 4b84527c09b08bbe6f7a0e44f5fcf89447b8173b Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Mon, 11 Oct 2021 03:10:55 +1100 Subject: [PATCH 07/29] getting ready for drawing --- .gitignore | 2 ++ Makefile | 2 +- script/min.lua | 8 ++++++++ src/drawlua.c | 10 +++++++--- src/gslua.c | 3 ++- src/ps2lua.c | 24 ++++++++++++++++++++---- 6 files changed, 40 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 312c732..be1ac7a 100644 --- a/.gitignore +++ b/.gitignore @@ -55,3 +55,5 @@ dkms.conf # python stuff **/__pycache__/ **/*.bin + +dist/* diff --git a/Makefile b/Makefile index 8d30ee3..ccf8915 100644 --- a/Makefile +++ b/Makefile @@ -50,7 +50,7 @@ clean: rm -rf dist/ .PHONY: run -run: +run: scripts PCSX2 --elf=$(PWD)/$(BIN) # TODO(phy1um): this could be improved, hard-coded ELF name is bad diff --git a/script/min.lua b/script/min.lua index c4c0279..1fa32c4 100644 --- a/script/min.lua +++ b/script/min.lua @@ -1,6 +1,13 @@ local gs = nil +function writeToBuffer(b, ints) + print("writing " .. #ints .. " ints") + for i=1,#ints,1 do + b:pushint(ints[i]) + end +end + function PS2PROG.start() DMA.init(DMA.GIF) gs = GS.newState(640, 448, GS.INTERLACED, GS.NTSC) @@ -13,6 +20,7 @@ end function PS2PROG.frame() local db = RM.getDrawBuffer(10000) db:frameStart(gs) + -- writeToBuffer(db, {0x8001, 0x10000000, 0xe, 0x0, 0x1, 0x0, 0x61, 0x0}) db:frameEnd(gs) DMA.send(db, DMA.GIF) db:free() diff --git a/src/drawlua.c b/src/drawlua.c index deeeb82..b282c16 100644 --- a/src/drawlua.c +++ b/src/drawlua.c @@ -27,11 +27,13 @@ static int buffer_pushint(lua_State *l) { // ASSUME 4byte int if (head%4 != 0) { // TODO: manually bitmask etc - logerr("cannot write int to buffer, head%%4 != 0"); + logerr("cannot write int to buffer, head%%4 != 0 (%d|%d)", head, head%4); return 0; } + info("db write int %d @ %d", value, head); ptr[head/4] = value; - lua_pushinteger(l, head+1); + info("db head -> %d", head+4); + lua_pushinteger(l, head+4); lua_setfield(l, 1, "head"); return 0; } @@ -95,9 +97,10 @@ static int drawlua_start_frame(lua_State *l) { q = draw_disable_tests(q, 0, &st->zb); q = draw_clear(q, 0, 2048.0f - halfw, 2048.0f - halfh, width, height, st->clear_r, st->clear_g, st->clear_b); - q = draw_enable_tests(q, 0, &st->zb); + //q = draw_enable_tests(q, 0, &st->zb); head = (char*)q - ptr; + info("db head -> %d", head); lua_pushinteger(l, head); lua_setfield(l, 1, "head"); return 0; @@ -127,6 +130,7 @@ static int drawlua_end_frame(lua_State *l) { q = draw_finish(q); head = (char*)q - ptr; + info("db head -> %d", head); lua_pushinteger(l, head); lua_setfield(l, 1, "head"); return 0; diff --git a/src/gslua.c b/src/gslua.c index 8153898..d963158 100644 --- a/src/gslua.c +++ b/src/gslua.c @@ -90,7 +90,8 @@ static int gslua_set_buffers(lua_State *l) { st->fb.mask = 0; st->zb.address = zb_addr; st->zb.zsm = zb_fmt; - st->zb.method = ZTEST_METHOD_GREATER; + //st->zb.method = ZTEST_METHOD_GREATER_EQUAL; + st->zb.method = ZTEST_METHOD_ALLPASS; st->zb.mask = 0; graph_set_framebuffer_filtered(st->fb.address, fb_width, fb_fmt, 0, 0); graph_enable_output(); diff --git a/src/ps2lua.c b/src/ps2lua.c index 1fefee9..d3baaa1 100644 --- a/src/ps2lua.c +++ b/src/ps2lua.c @@ -10,6 +10,8 @@ #include "script.h" +#define INIT_SCRIPT "host:script/draw.lua" + static int ps2luaprog_start_nil(lua_State *l) { info("default start..."); return 0; @@ -84,11 +86,25 @@ int main(int argc, char *argv[]) { info("finished lua state setup"); - info("loading file script/init.lua"); + info("loading file " INIT_SCRIPT); - int rc = luaL_loadfile(L, "host:script/min.lua"); - if ( rc ) { - logerr("failed to execute lua file %s", "host:script/min.lua"); + int rc = luaL_loadfile(L, INIT_SCRIPT); + if ( rc == LUA_ERRSYNTAX ) { + logerr("failed to load " INIT_SCRIPT ": syntax error"); + const char *err = lua_tostring(L, -1); + logerr("err: %s", err); + return -1; + } + else if ( rc == LUA_ERRMEM ) { + logerr("faild to allocate memory for " INIT_SCRIPT); + return -1; + } + else if ( rc == LUA_ERRFILE ) { + logerr("could not open/read file " INIT_SCRIPT); + return -1; + } + else if ( rc ) { + logerr("unknown error loading " INIT_SCRIPT); return -1; } From 9b6f58a5016d934065fbdbe850a8f7362444f993 Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Mon, 11 Oct 2021 11:04:28 +1100 Subject: [PATCH 08/29] remove prints, increase drawbuffer size, catch error when drawbuffer too big --- script/min.lua | 3 ++- src/dmalua.c | 4 ++-- src/drawlua.c | 17 ++++++++++------- src/ps2lua.c | 4 ++-- 4 files changed, 16 insertions(+), 12 deletions(-) diff --git a/script/min.lua b/script/min.lua index 1fa32c4..ae86e55 100644 --- a/script/min.lua +++ b/script/min.lua @@ -1,3 +1,4 @@ +local gif = dofile("host:script/gif.lua") local gs = nil @@ -14,7 +15,7 @@ function PS2PROG.start() local fb = gs:alloc(640, 448, GS.PSM24) local zb = gs:alloc(640, 448, GS.PSMZ24) gs:setBuffers(fb, zb) - gs:clearColour(255, 0, 0) + gs:clearColour(0, 255, 0) end function PS2PROG.frame() diff --git a/src/dmalua.c b/src/dmalua.c index a9e1f78..bf7bc6c 100644 --- a/src/dmalua.c +++ b/src/dmalua.c @@ -38,9 +38,9 @@ static int dma_send_buffer(lua_State *l) { int channel = lua_tointeger(l, 2); // print buffer for debugging - print_buffer(ptr, head/16); + // print_buffer(ptr, head/16); - info("DMA send :: sending %d qwords on channel %d", head/16, channel); + // info("DMA send :: sending %d qwords on channel %d", head/16, channel); dma_channel_send_normal(channel, ptr, head/16, 0, 0); return 0; } diff --git a/src/drawlua.c b/src/drawlua.c index b282c16..0fa5963 100644 --- a/src/drawlua.c +++ b/src/drawlua.c @@ -12,7 +12,7 @@ static int drawlua_new_drawbuffer(lua_State *l); -static const unsigned int DRAW_BUFFER_MAX_SIZE = 2 * 1024; +static const unsigned int DRAW_BUFFER_MAX_SIZE = 5 * 1024; char *static_draw_buffer; static int buffer_pushint(lua_State *l) { @@ -30,9 +30,9 @@ static int buffer_pushint(lua_State *l) { logerr("cannot write int to buffer, head%%4 != 0 (%d|%d)", head, head%4); return 0; } - info("db write int %d @ %d", value, head); + // info("db write int %d @ %d", value, head); ptr[head/4] = value; - info("db head -> %d", head+4); + // info("db head -> %d", head+4); lua_pushinteger(l, head+4); lua_setfield(l, 1, "head"); return 0; @@ -100,7 +100,7 @@ static int drawlua_start_frame(lua_State *l) { //q = draw_enable_tests(q, 0, &st->zb); head = (char*)q - ptr; - info("db head -> %d", head); + // info("db head -> %d", head); lua_pushinteger(l, head); lua_setfield(l, 1, "head"); return 0; @@ -130,7 +130,7 @@ static int drawlua_end_frame(lua_State *l) { q = draw_finish(q); head = (char*)q - ptr; - info("db head -> %d", head); + // info("db head -> %d", head); lua_pushinteger(l, head); lua_setfield(l, 1, "head"); return 0; @@ -144,7 +144,10 @@ static int drawbuffer_free(lua_State *l) { static int drawlua_new_drawbuffer(lua_State *l) { int size = lua_tointeger(l, 1); if ( size >= DRAW_BUFFER_MAX_SIZE ) { - // TODO: error + logerr("invalid drawbuffer size: %d must be smaller than %d", size, DRAW_BUFFER_MAX_SIZE); + lua_pushstring(l, "drawbuffer size is too big"); + lua_error(l); + return 0; } lua_createtable(l, 0, 5); lua_pushinteger(l, size); @@ -161,6 +164,6 @@ static int drawlua_new_drawbuffer(lua_State *l) { lua_pushcfunction(l, drawbuffer_free); lua_setfield(l, -2, "free"); luaL_getmetatable(l, "ps2.buffer"); - lua_setmetatable(l, -2); + lua_setmetatable(l, -2); return 1; } diff --git a/src/ps2lua.c b/src/ps2lua.c index d3baaa1..3b7d7a1 100644 --- a/src/ps2lua.c +++ b/src/ps2lua.c @@ -120,9 +120,9 @@ int main(int argc, char *argv[]) { dma_wait_fast(); info("ON FRAME"); ps2luaprog_onframe(L); - info("WAIT DRAW"); + // info("WAIT DRAW"); draw_wait_finish(); - info("WAIT VSYNC"); + // info("WAIT VSYNC"); graph_wait_vsync(); } info("main loop ended"); From 7ee29f119c824c696ba91a224b0a880770265808 Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Mon, 11 Oct 2021 11:04:46 +1100 Subject: [PATCH 09/29] add utility scripts --- script/gif.lua | 75 +++++++++++++++++++++++++++++++++++++++++++++ script/ps2const.lua | 14 +++++++++ 2 files changed, 89 insertions(+) create mode 100644 script/gif.lua create mode 100644 script/ps2const.lua diff --git a/script/gif.lua b/script/gif.lua new file mode 100644 index 0000000..7197c4e --- /dev/null +++ b/script/gif.lua @@ -0,0 +1,75 @@ + +local gif = { + PACKED = 0x0, + REGLIST = 0x01 * 2^26, + IMAGE = 0x10 * 2^26, +} + +function gif.tag(b, flg, nloop, eop, regs) + if #regs == 0 then return end + -- print("GIFTAG: pushing " .. string.format("0x%x", nloop)) + if eop then + b:pushint(nloop + 0x8000) + else + b:pushint(nloop) + end + local nreg = #regs + if nreg > 16 then error("invalid gif tag: nreg = " .. #regs) end + if nreg == 16 then nreg = 0 end + local w2 = (nreg * 2^28) + flg + -- print("GIFTAG: pushing " .. string.format("0x%x", w2)) + b:pushint(w2) + local reg = 0 + local regc = 0 + local max = 1 + + -- write the registers + for i=1,#regs,1 do + reg = reg + regs[i] * 2^(4*regc) + regc = regc + 1 + if regc >=8 then + b:pushint(reg) + -- print("GIFTAG: pushing regword " .. string.format("0x%x", reg)) + regc = 0 + max = max - 1 + end + end + + -- pad out the rest + for i=max,0,-1 do + b:pushint(reg) + -- print("GIFTAG: pushing regword " .. string.format("0x%x", reg)) + reg = 0 + end +end + +function gif.setAd(b, reg, v1, v2) + b:pushint(v1) + b:pushint(v2) + b:pushint(reg) + b:pushint(0) +end + +function gif.primAd(b, primType, shaded, textured, aa) + local bits = primType + if shaded then bits = bits + 0x4 end + if textured then bits = bits + 0x8 end + if aa then bits = bits + 0x40 end + gif.setAd(b, 0x00, bits, 0) +end + +function gif.packedRGBAQ(bu, r, g, b, a) + bu:pushint(r) + bu:pushint(g) + bu:pushint(b) + bu:pushint(a) +end + +function gif.packedXYZ2(b, x, y, z) + b:pushint(x) + b:pushint(y) + b:pushint(z) + b:pushint(0) +end + +return gif diff --git a/script/ps2const.lua b/script/ps2const.lua new file mode 100644 index 0000000..d32d416 --- /dev/null +++ b/script/ps2const.lua @@ -0,0 +1,14 @@ + +return { + PRIM = { + POINT=0x0, + LINE=0x1, + LINE_STRIP=0x2, + TRI=0x3, + TRI_FAN=0x4, + TRI_STRIP=0x5, + SPRITE=0x6 + }, + ENABLE=0x1, + DISABLE=0x0, +} From 94f62c0949fe257b7625b80acf49f578c8ba3e4c Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Tue, 12 Oct 2021 04:32:54 +1100 Subject: [PATCH 10/29] better buffer interface, better init interface --- script/gif.lua | 3 +++ src/drawlua.c | 34 +++++++++++++++++++++++++++ src/ps2lua.c | 62 +++++++++++++++++++++++++++----------------------- 3 files changed, 71 insertions(+), 28 deletions(-) diff --git a/script/gif.lua b/script/gif.lua index 7197c4e..c52106f 100644 --- a/script/gif.lua +++ b/script/gif.lua @@ -6,6 +6,7 @@ local gif = { } function gif.tag(b, flg, nloop, eop, regs) + local lpp = b.head if #regs == 0 then return end -- print("GIFTAG: pushing " .. string.format("0x%x", nloop)) if eop then @@ -41,6 +42,8 @@ function gif.tag(b, flg, nloop, eop, regs) -- print("GIFTAG: pushing regword " .. string.format("0x%x", reg)) reg = 0 end + + return lpp end function gif.setAd(b, reg, v1, v2) diff --git a/src/drawlua.c b/src/drawlua.c index 0fa5963..032d7b4 100644 --- a/src/drawlua.c +++ b/src/drawlua.c @@ -43,6 +43,36 @@ static int buffer_copy(lua_State *l) { return 0; } +static int buffer_read(lua_State *l) { + int index = lua_tointeger(l, 2); + if (index % 4 != 0) { + lua_pushstring(l, "invalid read index, not ==0 %%4"); + lua_error(l); + return 1; + } + lua_pushstring(l, "ptr"); + lua_gettable(l, 1); + int *ptr = (int *) lua_touserdata(l, -1); + int res = ptr[index/4]; + lua_pushinteger(l,,res); + return 1; +} + +static int buffer_write(lua_State *l) { + int index = lua_tointeger(l, 2); + if (index % 4 != 0) { + lua_pushstring(l, "invalid read index, not ==0 %%4"); + lua_error(l); + return 1; + } + int value = lua_tointeger(l, 3); + lua_pushstring(l, "ptr"); + lua_gettable(l, 1); + int *ptr = (int *) lua_touserdata(l, -1); + ptr[index/4] = value; + return 0; +} + int drawlua_init(lua_State *l) { luaL_newmetatable(l, "ps2.buffer"); lua_createtable(l, 0, 5); @@ -50,6 +80,10 @@ int drawlua_init(lua_State *l) { lua_setfield(l, -2, "pushint"); lua_pushcfunction(l, buffer_copy); lua_setfield(l, -2, "copy"); + lua_pushcfunction(l, buffer_read); + lua_setfield(l, -2, "read"); + lua_pushcfunction(l, buffer_write); + lua_setfield(l, -2, "write"); lua_setfield(l, -2, "__index"); lua_pop(l, 1); diff --git a/src/ps2lua.c b/src/ps2lua.c index 3b7d7a1..60ff68e 100644 --- a/src/ps2lua.c +++ b/src/ps2lua.c @@ -66,6 +66,38 @@ int ps2luaprog_is_running(lua_State *l) { return 1; } +static int runfile(lua_State *l, const char *fname) { + info("running lua file %s", fname); + int rc = luaL_loadfile(l, fname); + if ( rc == LUA_ERRSYNTAX ) { + logerr("failed to load %s: syntax error", fname); + const char *err = lua_tostring(l, -1); + logerr("err: %s", err); + return -1; + } + else if ( rc == LUA_ERRMEM ) { + logerr("faild to allocate memory for %s", fname); + return -1; + } + else if ( rc == LUA_ERRFILE ) { + logerr("could not open/read file %s", fname); + return -1; + } + else if ( rc ) { + logerr("unknown error loading %s", fname); + return -1; + } + + rc = lua_pcall(l, 0, 0, 0); + if ( rc ) { + const char *err = lua_tostring(l, -1); + logerr("lua execution error -- %s", err); + return -1; + } + + return 0; +} + int main(int argc, char *argv[]) { info("startup"); struct lua_State *L; @@ -86,34 +118,8 @@ int main(int argc, char *argv[]) { info("finished lua state setup"); - info("loading file " INIT_SCRIPT); - - int rc = luaL_loadfile(L, INIT_SCRIPT); - if ( rc == LUA_ERRSYNTAX ) { - logerr("failed to load " INIT_SCRIPT ": syntax error"); - const char *err = lua_tostring(L, -1); - logerr("err: %s", err); - return -1; - } - else if ( rc == LUA_ERRMEM ) { - logerr("faild to allocate memory for " INIT_SCRIPT); - return -1; - } - else if ( rc == LUA_ERRFILE ) { - logerr("could not open/read file " INIT_SCRIPT); - return -1; - } - else if ( rc ) { - logerr("unknown error loading " INIT_SCRIPT); - return -1; - } - - rc = lua_pcall(L, 0, 0, 0); - if ( rc ) { - const char *err = lua_tostring(L, -1); - logerr("lua execution error -- %s", err); - return -1; - } + runfile(L, "host:script/ps2init.lua"); + runfile(L, INIT_SCRIPT); ps2luaprog_onstart(L); while( ps2luaprog_is_running(L) ) { From ecb3beff9811ff007cc66618dd716e4241f19fc1 Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Tue, 12 Oct 2021 04:33:44 +1100 Subject: [PATCH 11/29] add more scripts --- script/draw.lua | 77 ++++++++++++++++++++++++++++++++++++++++++++ script/draw2d.lua | 80 ++++++++++++++++++++++++++++++++++++++++++++++ script/ps2init.lua | 16 ++++++++++ 3 files changed, 173 insertions(+) create mode 100644 script/draw.lua create mode 100644 script/draw2d.lua create mode 100644 script/ps2init.lua diff --git a/script/draw.lua b/script/draw.lua new file mode 100644 index 0000000..3996ac1 --- /dev/null +++ b/script/draw.lua @@ -0,0 +1,77 @@ +--local GIF = dofile("host:script/gif.lua") +local GIF = require("gif") +local P = require("ps2const") +local D2D = require("draw2d") + + +local gs = nil + + +local triMetaTable = { + x=0,y=0,w=1,h=1, +} +function triMetaTable:draw() +-- D2D:triangle(self.x, self.y, self.x+self.w, self.y, +-- self.x, self.y+self.h) + D2D:rect(self.x, self.y, self.w, self.h) +end + +function triMetaTable.new(x,y,w,h) + return setmetatable({x=x,y=y,w=w,h=h}, {__index = triMetaTable}) +end + +local scene = {} + +function PS2PROG.start() + DMA.init(DMA.GIF) + gs = GS.newState(640, 448, GS.INTERLACED, GS.NTSC) + local fb = gs:alloc(640, 448, GS.PSM24) + local zb = gs:alloc(640, 448, GS.PSMZ24) + gs:setBuffers(fb, zb) + gs:clearColour(0x2b, 0x2b, 0x2b) + + local dd = 10 + local dx = math.floor(640/dd) + local dy = math.floor(448/dd) + for x=-320,320,dx do + for y=-224,224,dy do + tt = triMetaTable.new(x,y,dx,dy) + table.insert(scene, tt) + end + end +end + +function tri(db, x1, y1, x2, y2, x3, y3) + GIF.packedRGBAQ(db, 0xff, 0, 0, 0x80) + GIF.packedXYZ2(db, 0x8000 + (x1*16), 0x8000 + (y1*16), 0) + GIF.packedRGBAQ(db, 0xff, 0, 0, 0x80) + GIF.packedXYZ2(db, 0x8000 + (x2*16), 0x8000 + (y2*16), 0) + GIF.packedRGBAQ(db, 0xff, 0, 0, 0x80) + GIF.packedXYZ2(db, 0x8000 + (x3*16), 0x8000 + (y3*16), 0) +end + +function rect(db, x, y, w, h) + tri(db, x, y, x+w, y, x, y+h) + tri(db, x, y+h, x+w, y+h, x+w, y) +end + + + +function PS2PROG.frame() + D2D:newBuffer() + local db = D2D:getBuffer() + db:frameStart(gs) + D2D:setColour(255,0,0,0x80) + for i,s in ipairs(scene) do + s:draw() + end + db = D2D:getBuffer() + db:frameEnd(gs) + D2D:kick() + print("tris/frame = " .. D2D.rawtri .. ", KC=" .. D2D.kc) + D2D.rawtri = 0 + D2D.kc = 0 + --db:free() +end + + diff --git a/script/draw2d.lua b/script/draw2d.lua new file mode 100644 index 0000000..7363b51 --- /dev/null +++ b/script/draw2d.lua @@ -0,0 +1,80 @@ +local GIF = require("gif") +local P = require("ps2const") + +local DRAW_NONE = 0 +local DRAW_GEOM = 1 +local DRAW_SPRITE = 2 + +local DRAW_FMT_GEOM = {1,5,5,5} +local DB_SIZE = 5000 + +local draw = { + col = {r=255, g=255, b=255, a=0x80}, + state = DRAW_NONE, + loopCount = 0, + tagLoopPtr = 0, + buf = nil, + kc = 0, + rawtri = 0, + +} + + +function draw:newBuffer() + self.state = DRAW_NONE + self.loopCount = 0 + self.tagLoopPtr = 0 + self.buf = RM.getDrawBuffer(DB_SIZE) + end + +function draw:getBuffer() + return self.buf +end + +function draw:setColour(r,g,b,a) + self.col.r = r + self.col.g = g + self.col.b = b + self.col.a = a +end + +function draw:rect(x, y, w, h) + draw:triangle(x, y, x+w, y, x, y+h) + draw:triangle(x, y+h, x+w, y+h, x+w, y) +end + +function draw:triangle(x1, y1, x2, y2, x3, y3) + if self.loopCount > 10000 then self:kick() end + if self.buf.size - self.buf.head < 80 then self:kick() end + if self.state ~= DRAW_GEOM then + if self.state ~= DRAW_NONE then self:kick() end + GIF.tag(self.buf, 0, 1, false, {0xe}) + GIF.primAd(self.buf, P.PRIM.TRI, false, false, false) + self.tagLoopPtr = GIF.tag(self.buf, 0, 1, false, DRAW_FMT_GEOM) + self.loopCount = 0 + self.state = DRAW_GEOM + end + GIF.packedRGBAQ(self.buf, self.col.r, self.col.g, self.col.b, self.col.a) + GIF.packedXYZ2(self.buf, 0x8000 + (x1*16), 0x8000 + (y1*16), 0) + GIF.packedXYZ2(self.buf, 0x8000 + (x2*16), 0x8000 + (y2*16), 0) + GIF.packedXYZ2(self.buf, 0x8000 + (x3*16), 0x8000 + (y3*16), 0) + self.loopCount = self.loopCount + 1 + self.rawtri = self.rawtri + 1 +end + +function draw:kick() + local nloop = self.buf:read(self.tagLoopPtr) + if nloop - 0x8000 > 0 then + print("kick EOP") + self.buf:write(self.tagLoopPtr, 0x8000 + self.loopCount) + else + self.buf:write(self.tagLoopPtr, self.loopCount) + end + DMA.send(self.buf, DMA.GIF) + self:newBuffer() + self.kc = self.kc + 1 +end + +return draw + + diff --git a/script/ps2init.lua b/script/ps2init.lua new file mode 100644 index 0000000..b03c5ef --- /dev/null +++ b/script/ps2init.lua @@ -0,0 +1,16 @@ + +local libs = {} +local SEARCH_PATH = "host:script/" + +function require(name) + local l = libs[name] + if l ~= nil then + return l + else + l = dofile(SEARCH_PATH .. name .. ".lua") + libs[name] = l + return l + end +end + +return function() end From 9208c3afee5761aaaca62e417e0ea55673a64798 Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Wed, 13 Oct 2021 05:40:21 +1100 Subject: [PATCH 12/29] improve lua apis, rename test files --- Makefile | 2 +- script/main.lua | 2 ++ script/pre.lua | 9 ++++++ script/{draw.lua => stress.lua} | 34 +++++--------------- script/texture.lua | 57 +++++++++++++++++++++++++++++++++ src/Makefile | 2 +- src/{drawlua.c => bufferlua.c} | 10 ++---- src/ps2lua.c | 15 +++++++-- 8 files changed, 93 insertions(+), 38 deletions(-) create mode 100644 script/main.lua create mode 100644 script/pre.lua rename script/{draw.lua => stress.lua} (54%) create mode 100644 script/texture.lua rename src/{drawlua.c => bufferlua.c} (96%) diff --git a/Makefile b/Makefile index ccf8915..a56b6e6 100644 --- a/Makefile +++ b/Makefile @@ -51,7 +51,7 @@ clean: .PHONY: run run: scripts - PCSX2 --elf=$(PWD)/$(BIN) + PCSX2 --elf=$(PWD)/$(BIN) # TODO(phy1um): this could be improved, hard-coded ELF name is bad .PHONY: runps2 diff --git a/script/main.lua b/script/main.lua new file mode 100644 index 0000000..88dd945 --- /dev/null +++ b/script/main.lua @@ -0,0 +1,2 @@ + +dofile("host:script/rect.lua") diff --git a/script/pre.lua b/script/pre.lua new file mode 100644 index 0000000..b311323 --- /dev/null +++ b/script/pre.lua @@ -0,0 +1,9 @@ + +function writeToBuffer(b, ints) + for i=1,#ints,1 do + b:pushint(ints[i]) + end +end + + + diff --git a/script/draw.lua b/script/stress.lua similarity index 54% rename from script/draw.lua rename to script/stress.lua index 3996ac1..49bae36 100644 --- a/script/draw.lua +++ b/script/stress.lua @@ -6,18 +6,16 @@ local D2D = require("draw2d") local gs = nil - -local triMetaTable = { +local emt = { x=0,y=0,w=1,h=1, } -function triMetaTable:draw() --- D2D:triangle(self.x, self.y, self.x+self.w, self.y, --- self.x, self.y+self.h) - D2D:rect(self.x, self.y, self.w, self.h) +function emt:draw() + --D2D:rect(self.x, self.y, self.w, self.h) + D2D:triangle(self.x, self.y, self.x+self.w, self.y, self.x, self.y+self.h) end -function triMetaTable.new(x,y,w,h) - return setmetatable({x=x,y=y,w=w,h=h}, {__index = triMetaTable}) +function emt.new(x,y,w,h) + return setmetatable({x=x,y=y,w=w,h=h}, {__index = emt}) end local scene = {} @@ -30,33 +28,17 @@ function PS2PROG.start() gs:setBuffers(fb, zb) gs:clearColour(0x2b, 0x2b, 0x2b) - local dd = 10 + local dd = 100 local dx = math.floor(640/dd) local dy = math.floor(448/dd) for x=-320,320,dx do for y=-224,224,dy do - tt = triMetaTable.new(x,y,dx,dy) + tt = emt.new(x,y,dx,dy) table.insert(scene, tt) end end end -function tri(db, x1, y1, x2, y2, x3, y3) - GIF.packedRGBAQ(db, 0xff, 0, 0, 0x80) - GIF.packedXYZ2(db, 0x8000 + (x1*16), 0x8000 + (y1*16), 0) - GIF.packedRGBAQ(db, 0xff, 0, 0, 0x80) - GIF.packedXYZ2(db, 0x8000 + (x2*16), 0x8000 + (y2*16), 0) - GIF.packedRGBAQ(db, 0xff, 0, 0, 0x80) - GIF.packedXYZ2(db, 0x8000 + (x3*16), 0x8000 + (y3*16), 0) -end - -function rect(db, x, y, w, h) - tri(db, x, y, x+w, y, x, y+h) - tri(db, x, y+h, x+w, y+h, x+w, y) -end - - - function PS2PROG.frame() D2D:newBuffer() local db = D2D:getBuffer() diff --git a/script/texture.lua b/script/texture.lua new file mode 100644 index 0000000..5af10fb --- /dev/null +++ b/script/texture.lua @@ -0,0 +1,57 @@ + +local GIF = require("gif") +local P = require("ps2const") +local D2D = require("draw2d") + + +function makeTex(w, h, fill) + local t = {} + for x=0,w,1 do + for y=0,h,1 do + t[y*w + x] = fill + end + end +end + + +local gs = nil +local testTexBuf = nil +local tex = makeTex(50, 50, 0xff) + +function PS2PROG.start() + DMA.init(DMA.GIF) + gs = GS.newState(640, 448, GS.INTERLACED, GS.NTSC) + local fb = gs:alloc(640, 448, GS.PSM24) + local zb = gs:alloc(640, 448, GS.PSMZ24) + gs:setBuffers(fb, zb) + gs:clearColour(0x2b, 0x2b, 0x2b) + + testTexBuf = gs:alloc(50, 50, GS.PSM24) + for i=0,50,1 do + for j=0,50,1 do + if (i+j)%2 == 0 then + tex[j*50 + i] = 0xff00 + end + end + end + + ib = RM:tmpBuffer(1000) + GIF.tag(ib, GIF.IMAGE, x, 1, 1) +end + +function PS2PROG.frame() + D2D:newBuffer() + local db = D2D:getBuffer() + db:frameStart(gs) + D2D:setColour(255,0,0,0x80) + D2D:rect(-200, -200, 200, 200) + db = D2D:getBuffer() + db:frameEnd(gs) + D2D:kick() + print("tris/frame = " .. D2D.rawtri .. ", KC=" .. D2D.kc) + D2D.rawtri = 0 + D2D.kc = 0 + --db:free() +end + + diff --git a/src/Makefile b/src/Makefile index 5479e19..66e39b9 100644 --- a/src/Makefile +++ b/src/Makefile @@ -2,7 +2,7 @@ PS2SDK=/usr/local/ps2dev/ps2sdk EE_BIN=test.elf #EE_OBJS=main.o gs.o mesh.o draw.o math.o pad.o drawstate.o script.o luadrawstate.o -EE_OBJS=ps2lua.o gslua.o dmalua.o drawlua.o +EE_OBJS=ps2lua.o gslua.o dmalua.o bufferlua.o EE_LIBS=-ldma -lgraph -ldraw -lkernel -ldebug -lmath3d -lm -lpad -llua EE_CFLAGS+=-Wall --std=c99 -Wno-sign-compare -fno-strict-aliasing -fno-exceptions -DLUA_USE_PS2 diff --git a/src/drawlua.c b/src/bufferlua.c similarity index 96% rename from src/drawlua.c rename to src/bufferlua.c index 032d7b4..edd3ca9 100644 --- a/src/drawlua.c +++ b/src/bufferlua.c @@ -54,7 +54,7 @@ static int buffer_read(lua_State *l) { lua_gettable(l, 1); int *ptr = (int *) lua_touserdata(l, -1); int res = ptr[index/4]; - lua_pushinteger(l,,res); + lua_pushinteger(l,res); return 1; } @@ -104,9 +104,11 @@ static int drawlua_start_frame(lua_State *l) { lua_pushstring(l, "head"); lua_gettable(l, 1); int head = lua_tointeger(l, -1); + /* lua_pushstring(l, "size"); lua_gettable(l, 1); int size = lua_tointeger(l, -1); + */ lua_pushstring(l, "ptr"); lua_gettable(l, 1); char *ptr = (char *) lua_touserdata(l, -1); @@ -154,12 +156,6 @@ static int drawlua_end_frame(lua_State *l) { lua_gettable(l, 1); char *ptr = (char *) lua_touserdata(l, -1); - // gs is arg #2 - lua_pushstring(l, "state"); - lua_gettable(l, 2); - - struct gs_state *st = (struct gs_state *) lua_touserdata(l, -1); - qword_t *q = (qword_t *) (ptr + head); q = draw_finish(q); diff --git a/src/ps2lua.c b/src/ps2lua.c index 60ff68e..ea19572 100644 --- a/src/ps2lua.c +++ b/src/ps2lua.c @@ -10,7 +10,7 @@ #include "script.h" -#define INIT_SCRIPT "host:script/draw.lua" +#define INIT_SCRIPT "host:script/ps2init.lua" static int ps2luaprog_start_nil(lua_State *l) { info("default start..."); @@ -99,7 +99,16 @@ static int runfile(lua_State *l, const char *fname) { } int main(int argc, char *argv[]) { - info("startup"); + info("startup - argc = %d", argc); + for (int i = 0; i < argc; i++) { + info("arg %d) %s", i, argv[i]); + } + char *startup = "host:script/main.lua"; + if (argc > 1) { + info("setting entrypoint to %s", argv[1]); + startup = argv[1]; + } + struct lua_State *L; L = luaL_newstate(); if ( !L ) { @@ -118,8 +127,8 @@ int main(int argc, char *argv[]) { info("finished lua state setup"); - runfile(L, "host:script/ps2init.lua"); runfile(L, INIT_SCRIPT); + runfile(L, startup); ps2luaprog_onstart(L); while( ps2luaprog_is_running(L) ) { From 8f932440fb33c44b7218011b3ee059f2521a90d6 Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Wed, 13 Oct 2021 12:47:52 +1100 Subject: [PATCH 13/29] added texture support, not working --- script/rect.lua | 32 ++++++++++++++++++++++++++++++++ script/vram.lua | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 script/rect.lua create mode 100644 script/vram.lua diff --git a/script/rect.lua b/script/rect.lua new file mode 100644 index 0000000..48a621d --- /dev/null +++ b/script/rect.lua @@ -0,0 +1,32 @@ + +local GIF = require("gif") +local P = require("ps2const") +local D2D = require("draw2d") + +local gs = nil + +function PS2PROG.start() + DMA.init(DMA.GIF) + gs = GS.newState(640, 448, GS.INTERLACED, GS.NTSC) + local fb = gs:alloc(640, 448, GS.PSM24) + local zb = gs:alloc(640, 448, GS.PSMZ24) + gs:setBuffers(fb, zb) + gs:clearColour(0x2b, 0x2b, 0x2b) +end + +function PS2PROG.frame() + D2D:newBuffer() + local db = D2D:getBuffer() + db:frameStart(gs) + D2D:setColour(255,0,0,0x80) + D2D:rect(-200, -200, 200, 200) + db = D2D:getBuffer() + db:frameEnd(gs) + D2D:kick() + print("tris/frame = " .. D2D.rawtri .. ", KC=" .. D2D.kc) + D2D.rawtri = 0 + D2D.kc = 0 + --db:free() +end + + diff --git a/script/vram.lua b/script/vram.lua new file mode 100644 index 0000000..973ade2 --- /dev/null +++ b/script/vram.lua @@ -0,0 +1,43 @@ + +local vram = {} +local basePtr = 0 + +function vpa(v, a) + return v + a - (v%a) +end + +function vram.alloc(b, align) + local out = basePtr + out = out + align - (out%align) + basePtr = out + b + print("vram alloc: " .. out .. " base -> " .. basePtr) + return out +end + +function vram.size(w, h, psm, align) + w = vpa(w, 64) + local size = w*h + if psm == GS.PSM16 or psm == GS.PSM16S or psm == GS.PSMZ16 or psm == GS.PSMZ16S then + math.floor(width*height*0.5) + elseif psm == GS.PSM8 then size = math.floor(width*height*2^-2) + elseif psm == GS.PSM4 then size = math.floor(width*height*2^-3) + end + + return vpa(size, align) +end + +function vram.buffer(w, h, psm, align) + local sz = vram.size(w, h, psm, align) + return { + address = vram.alloc(sz, align), + width = w, + height = h, + format = psm, + } +end + +function vram.textureSize(w,h,psm) + error("not implemented") +end + +return vram From 9b9e82d749aecaf8a6da03a9076c01bfe81ef820 Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Wed, 13 Oct 2021 12:49:22 +1100 Subject: [PATCH 14/29] added texture support, not working --- Makefile | 4 ++ script/draw2d.lua | 23 ++++++++++++ script/example.lua | 46 ----------------------- script/gif.lua | 39 ++++++++++++++++++-- script/main.lua | 2 +- script/ps2const.lua | 12 +++++- script/texture.lua | 90 +++++++++++++++++++++++++++++++++++++++------ src/bufferlua.c | 52 ++++++++++++++++++++++++++ src/dmalua.c | 2 +- src/ps2lua.c | 4 +- 10 files changed, 207 insertions(+), 67 deletions(-) delete mode 100644 script/example.lua diff --git a/Makefile b/Makefile index a56b6e6..d7be636 100644 --- a/Makefile +++ b/Makefile @@ -66,6 +66,10 @@ resetps2: lint: cpplint --filter=$(CPPLINT_FILTERS) --counting=total --linelength=$(CPPLINT_LINE_LENGTH) --extensions=c,h --recursive . +.PHONY: lualint +lualint: + luac5.1 -p script/*.lua + .PHONY: format format: $(DOCKER) run $(DOCKERFLAGS) -v $(shell pwd):/workdir unibeautify/clang-format -i -sort-includes **/*.c **/*.h diff --git a/script/draw2d.lua b/script/draw2d.lua index 7363b51..2eae5a0 100644 --- a/script/draw2d.lua +++ b/script/draw2d.lua @@ -6,6 +6,7 @@ local DRAW_GEOM = 1 local DRAW_SPRITE = 2 local DRAW_FMT_GEOM = {1,5,5,5} +local DRAW_FMT_SPRITE = {1,3,5,3,5} local DB_SIZE = 5000 local draw = { @@ -43,6 +44,28 @@ function draw:rect(x, y, w, h) draw:triangle(x, y+h, x+w, y+h, x+w, y) end +function draw:rectuv(tex, x, y, w, h, u1, v1, u2, v2) + if self.loopCount > 10000 then self:kick() end + if self.buf.size - self.buf.head < 80 then self:kick() end + if self.state ~= DRAW_SPRITE then + if self.state ~= DRAW_NONE then self:kick() end + GIF.tag(self.buf, GIF.PACKED, 2, false, {0xe}) + print("HEAD BEFORE = " .. self.buf.head) + self.buf:settex(0, tex.basePtr, math.floor(tex.width/64), GS.PSM24, 6, 6, 0, 0, 0, 0, 0) + print("HEAD AFTER = " .. self.buf.head) + GIF.primAd(self.buf, P.PRIM.SPRITE, false, true, false) + self.tagLoopPtr = GIF.tag(self.buf, 0, 1, false, DRAW_FMT_SPRITE) + self.loopCount = 0 + self.state = DRAW_SPRITE + end + GIF.packedRGBAQ(self.buf, self.col.r, self.col.g, self.col.b, self.col.a) + GIF.packedUV(self.buf, u1, v1) + GIF.packedXYZ2(self.buf, 0x8000 + (x*16), 0x8000 + (y*16), 0) + GIF.packedUV(self.buf, u2, v2) + GIF.packedXYZ2(self.buf, 0x8000 + ((x+w)*16), 0x8000 + ((y+h)*16), 0) + self.loopCount = self.loopCount + 1 +end + function draw:triangle(x1, y1, x2, y2, x3, y3) if self.loopCount > 10000 then self:kick() end if self.buf.size - self.buf.head < 80 then self:kick() end diff --git a/script/example.lua b/script/example.lua deleted file mode 100644 index 532341b..0000000 --- a/script/example.lua +++ /dev/null @@ -1,46 +0,0 @@ - -local scene = {} -local cubeMesh = {} - -function PS2PROG.entrypoint() - DMA.initGif() - local gs = GS.newState(640, 448, GS.INTERLACED, GS.NTSC) - local fb = gs:alloc(640, 448, GS.PSM24) - local zb = gs:alloc(640, 448, GS.PSMZ24) - gs:setBuffers(fb, zb) - scene = DRAW.newScene() - cubeMesh = RM.loadMesh("host:cube.bin", RM.MESH_FORMAT_GIF1) - for i=0,i<10;i++ do - local mi = mesh:instance() - mi:translate(0, 0, -100 + 10 * i) - mi:scale(0.4, 0.4, 0.4) - scene:addInstance(mi) - end - scene:cameraAt(0, 0, -5) - scene:lookAt(0, 0, -1) - - PS2PROG.bindGsState(gs) -end - -function PS2PROG.onframe(gs) - local db = RM.getDrawBuffer(10000) - db:frameStart(gs) - for i,v in ipairs(scene:meshes) do - local giftag = db:pushGifTag() - local ih = db:head() - giftag:fromMesh(v) - db:pushVertexData(v) - scene:transformVerts(db, ih + v:headerSize(), v:vertexCount()) - end - db:frameEnd(gs) - DMA.sendBuffer(db, DMA.TO_GIF) - db:free() -end - -function PS2PROG.exit() - scene:destroy() - cubeMesh:free() - gs:done() - PS2.exit_program() -end - diff --git a/script/gif.lua b/script/gif.lua index c52106f..9d0b57c 100644 --- a/script/gif.lua +++ b/script/gif.lua @@ -1,8 +1,10 @@ +local P = require("ps2const") local gif = { PACKED = 0x0, REGLIST = 0x01 * 2^26, - IMAGE = 0x10 * 2^26, + IMAGE = math.floor(2^27), + BLOCKSIZE = 0x7FF, } function gif.tag(b, flg, nloop, eop, regs) @@ -17,8 +19,9 @@ function gif.tag(b, flg, nloop, eop, regs) local nreg = #regs if nreg > 16 then error("invalid gif tag: nreg = " .. #regs) end if nreg == 16 then nreg = 0 end - local w2 = (nreg * 2^28) + flg - -- print("GIFTAG: pushing " .. string.format("0x%x", w2)) + local w2 = (math.floor(nreg * 2^28) + flg) + print("GIFTAG: pushing " .. string.format("0x%x", w2) .. " :: " .. w2) + print("GIFTAG: flag = " .. flg) b:pushint(w2) local reg = 0 local regc = 0 @@ -53,12 +56,28 @@ function gif.setAd(b, reg, v1, v2) b:pushint(0) end +function gif.bitBltBuf(b, dba, dbw, psm) + gif.setAd(b, P.REG.BITBLTBUF, 0, dba + (dbw*2^16) + (psm*2^24)) +end + +function gif.trxPos(b, sx, sy, dx, dy, dir) + gif.setAd(b, P.REG.TRXPOS, sx + (sy*2^16), dx + (dy*2^16) + (dir*2^27)) +end + +function gif.trxReg(b, w, h) + gif.setAd(b, P.REG.TRXREG, w, h) +end + +function gif.trxDir(b, dir) + gif.setAd(b, P.REG.TRXDIR, dir, 0) +end + function gif.primAd(b, primType, shaded, textured, aa) local bits = primType if shaded then bits = bits + 0x4 end if textured then bits = bits + 0x8 end if aa then bits = bits + 0x40 end - gif.setAd(b, 0x00, bits, 0) + gif.setAd(b, P.REG.PRIM, bits, 0) end function gif.packedRGBAQ(bu, r, g, b, a) @@ -75,4 +94,16 @@ function gif.packedXYZ2(b, x, y, z) b:pushint(0) end +function gif.packedUV(b, u, v) + b:pushint(u) + b:pushint(v) + b:pushint(0) + b:pushint(0) +end + +function gif.texflush(b) + gif.tag(b, gif.PACKED, 1, true, {0xe}) + gif.setAd(b, P.REG.TEXFLUSH, 0, 0) +end + return gif diff --git a/script/main.lua b/script/main.lua index 88dd945..7d348e0 100644 --- a/script/main.lua +++ b/script/main.lua @@ -1,2 +1,2 @@ -dofile("host:script/rect.lua") +dofile("host:script/texture.lua") diff --git a/script/ps2const.lua b/script/ps2const.lua index d32d416..5280ef8 100644 --- a/script/ps2const.lua +++ b/script/ps2const.lua @@ -7,7 +7,17 @@ return { TRI=0x3, TRI_FAN=0x4, TRI_STRIP=0x5, - SPRITE=0x6 + SPRITE=0x6, + }, + REG = { + PRIM=0x0, + BITBLTBUF=0x50, + TRXPOS=0x51, + TRXREG=0x52, + TRXDIR=0x53, + TEXFLUSH=0x3F, + TEX0 = 0x6, + TEX1 = 0x7, }, ENABLE=0x1, DISABLE=0x0, diff --git a/script/texture.lua b/script/texture.lua index 5af10fb..a208443 100644 --- a/script/texture.lua +++ b/script/texture.lua @@ -2,7 +2,7 @@ local GIF = require("gif") local P = require("ps2const") local D2D = require("draw2d") - +local VRAM = require("vram") function makeTex(w, h, fill) local t = {} @@ -11,40 +11,106 @@ function makeTex(w, h, fill) t[y*w + x] = fill end end + return t end local gs = nil local testTexBuf = nil -local tex = makeTex(50, 50, 0xff) +local tex = makeTex(64, 64, 0xff) +local testTexBuf = 0 +local tt = { + basePtr = 0, + width = 64, + height = 64, + data = tex, +} function PS2PROG.start() DMA.init(DMA.GIF) gs = GS.newState(640, 448, GS.INTERLACED, GS.NTSC) - local fb = gs:alloc(640, 448, GS.PSM24) - local zb = gs:alloc(640, 448, GS.PSMZ24) + --local fb = gs:alloc(640, 448, GS.PSM24) + --local zb = gs:alloc(640, 448, GS.PSMZ24) + local fb = VRAM.buffer(640, 440, GS.PSM24, 256) + local zb = VRAM.buffer(640, 440, GS.PSMZ24, 256) + print("setting new buffers") gs:setBuffers(fb, zb) gs:clearColour(0x2b, 0x2b, 0x2b) - testTexBuf = gs:alloc(50, 50, GS.PSM24) - for i=0,50,1 do - for j=0,50,1 do + print("detailing texture") + local width = 64 + local height = 64 + for i=0,width,1 do + for j=0,height,1 do if (i+j)%2 == 0 then - tex[j*50 + i] = 0xff00 + tex[j*width + i] = 0xff00 end end end + local texVramSize = 1024 + testTexBuf = VRAM.alloc(texVramSize, 256) + tt.basePtr = math.floor(testTexBuf/64) + print("got texture VRAM addr = " .. testTexBuf) + + -- ib = RM.tmpBuffer(1000) + local taddr = math.floor(testTexBuf/256) + ib = RM.getDrawBuffer(5000) + + GIF.tag(ib, GIF.PACKED, 4, false, {0xe}) + GIF.bitBltBuf(ib, math.floor(testTexBuf/64), math.floor(width/64), GS.PSM24) + GIF.trxPos(ib,0,0,0,0,0) + GIF.trxReg(ib,width,height) + GIF.trxDir(ib, 0) + + local eeSize = width*height*4 + local qwc = math.floor(eeSize / 16) + print("image sent in " .. qwc .. " qwords") + if qwc % 16 ~= 0 then qwc = qwc + 1 end + local blocksize = math.floor(4496/16) + local packets = math.floor(qwc / blocksize) + local remain = qwc % blocksize + -- print("transmitting") + print("transmitting in " .. packets .. " packets with " .. remain .. " lefte") + + local tb = 0 + while packets > 0 do + GIF.tag(ib, GIF.IMAGE, blocksize, false, {0}) + for i=1,blocksize,1 do + ib:pushint(tex[tb*blocksize*4 + i]) + ib:pushint(tex[tb*blocksize*4 + i+1]) + ib:pushint(tex[tb*blocksize*4 + i+2]) + ib:pushint(tex[tb*blocksize*4 + i+3]) + end + DMA.send(ib, DMA.GIF) + ib = RM.getDrawBuffer(5000) + packets = packets - 1 + end + + if remain > 0 then + local base = math.floor(qwc/blocksize)*blocksize*4 + GIF.tag(ib, GIF.IMAGE, remain, false, {1}) + for i=1,remain,1 do + ib:pushint(tex[base + i]) + ib:pushint(tex[base + i + 1]) + ib:pushint(tex[base + i + 2]) + ib:pushint(tex[base + i + 3]) + end + end + + print("adding texflush") + GIF.texflush(ib) + + print("dma sending") + DMA.send(ib, DMA.GIF) - ib = RM:tmpBuffer(1000) - GIF.tag(ib, GIF.IMAGE, x, 1, 1) end function PS2PROG.frame() D2D:newBuffer() local db = D2D:getBuffer() db:frameStart(gs) - D2D:setColour(255,0,0,0x80) - D2D:rect(-200, -200, 200, 200) + D2D:setColour(255,255,255,0x80) + D2D:rectuv(tt, -200, -200, 200, 200, 0, 0, 2^14-1, 2^14 - 1) db = D2D:getBuffer() db:frameEnd(gs) D2D:kick() diff --git a/src/bufferlua.c b/src/bufferlua.c index edd3ca9..57a4c40 100644 --- a/src/bufferlua.c +++ b/src/bufferlua.c @@ -38,6 +38,52 @@ static int buffer_pushint(lua_State *l) { return 0; } +static int buffer_settex(lua_State *l) { + int reg= lua_tointeger(l, 2); + int tbp= lua_tointeger(l, 3); + int tbw= lua_tointeger(l, 4); + int psm= lua_tointeger(l, 5); + int tw = lua_tointeger(l, 6); + int th = lua_tointeger(l, 7); + int tcc= lua_tointeger(l, 8); + int tfx= lua_tointeger(l, 9); + + lua_pushstring(l, "ptr"); + lua_gettable(l, 1); + int *ptr = (int *) lua_touserdata(l, -1); + lua_pushstring(l, "head"); + lua_gettable(l, 1); + int head = lua_tointeger(l, -1); + // TODO: check size + // ASSUME 4byte int + if (head%4 != 0) { + // TODO: manually bitmask etc + logerr("cannot write int to buffer, head%%4 != 0 (%d|%d)", head, head%4); + return 0; + } + + int *base = ptr + (head/4); + + int v1 = tbp | (tbw<<14) | (psm<<20) | (tw<<26) | ((th&0x3) << 30); + int v2 = ((th&0x5)>>2) | (tcc<<2) | (tfx<<3); + int v3 = 0x6+reg; + int v4 = 0; + *(base) = v1; + *(base+1) = v2; + *(base+2) = v3; + *(base+3) = v4; + + info("write tex0 :: %08x %08x %08x %08x", v1, v2, v3, v4); + info("head -> %d", head+16); + + lua_pushinteger(l, head+16); + lua_setfield(l, 1, "head"); + + info("backtrack tex0 :: %08x %08x %08x %08x", base[0], base[1], base[2], base[3]); + return 0; + +} + static int buffer_copy(lua_State *l) { info("UNIMPLEMENTED"); return 0; @@ -76,10 +122,16 @@ static int buffer_write(lua_State *l) { int drawlua_init(lua_State *l) { luaL_newmetatable(l, "ps2.buffer"); lua_createtable(l, 0, 5); + lua_pushcfunction(l, buffer_pushint); lua_setfield(l, -2, "pushint"); + + lua_pushcfunction(l, buffer_settex); + lua_setfield(l, -2, "settex"); + lua_pushcfunction(l, buffer_copy); lua_setfield(l, -2, "copy"); + lua_pushcfunction(l, buffer_read); lua_setfield(l, -2, "read"); lua_pushcfunction(l, buffer_write); diff --git a/src/dmalua.c b/src/dmalua.c index bf7bc6c..2bed363 100644 --- a/src/dmalua.c +++ b/src/dmalua.c @@ -38,7 +38,7 @@ static int dma_send_buffer(lua_State *l) { int channel = lua_tointeger(l, 2); // print buffer for debugging - // print_buffer(ptr, head/16); + print_buffer(ptr, head/16); // info("DMA send :: sending %d qwords on channel %d", head/16, channel); dma_channel_send_normal(channel, ptr, head/16, 0, 0); diff --git a/src/ps2lua.c b/src/ps2lua.c index ea19572..2e49eaf 100644 --- a/src/ps2lua.c +++ b/src/ps2lua.c @@ -135,9 +135,9 @@ int main(int argc, char *argv[]) { dma_wait_fast(); info("ON FRAME"); ps2luaprog_onframe(L); - // info("WAIT DRAW"); + info("WAIT DRAW"); draw_wait_finish(); - // info("WAIT VSYNC"); + info("WAIT VSYNC"); graph_wait_vsync(); } info("main loop ended"); From d4922850e6eb6184a065dc694c808a8f075c2d6b Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Thu, 14 Oct 2021 12:32:20 +1100 Subject: [PATCH 15/29] working texture basics --- Makefile | 4 +-- script/draw2d.lua | 23 ++++++++++------ script/gif.lua | 47 ++++++++++++++++++++++++++++---- script/ps2const.lua | 7 +++-- script/texture.lua | 31 ++++++++++----------- script/vram.lua | 4 ++- src/bufferlua.c | 66 ++++++++++++++++++++++++++++++++++++++++++++- 7 files changed, 146 insertions(+), 36 deletions(-) diff --git a/Makefile b/Makefile index d7be636..554bc94 100644 --- a/Makefile +++ b/Makefile @@ -55,8 +55,8 @@ run: scripts # TODO(phy1um): this could be improved, hard-coded ELF name is bad .PHONY: runps2 -runps2: - cp dist && ps2client -h $(PS2HOST) -t 10 execee host:test.elf +runps2: scripts + ps2client -h $(PS2HOST) -t 10 execee host:test.elf .PHONY: resetps2 resetps2: diff --git a/script/draw2d.lua b/script/draw2d.lua index 2eae5a0..2a42f1e 100644 --- a/script/draw2d.lua +++ b/script/draw2d.lua @@ -6,7 +6,7 @@ local DRAW_GEOM = 1 local DRAW_SPRITE = 2 local DRAW_FMT_GEOM = {1,5,5,5} -local DRAW_FMT_SPRITE = {1,3,5,3,5} +local DRAW_FMT_SPRITE = {2,1,5,2,1,5} local DB_SIZE = 5000 local draw = { @@ -14,6 +14,7 @@ local draw = { state = DRAW_NONE, loopCount = 0, tagLoopPtr = 0, + currentTexPtr = 0, buf = nil, kc = 0, rawtri = 0, @@ -47,21 +48,27 @@ end function draw:rectuv(tex, x, y, w, h, u1, v1, u2, v2) if self.loopCount > 10000 then self:kick() end if self.buf.size - self.buf.head < 80 then self:kick() end - if self.state ~= DRAW_SPRITE then + if self.state ~= DRAW_SPRITE or self.currentTexPtr ~= tex.basePtr then if self.state ~= DRAW_NONE then self:kick() end - GIF.tag(self.buf, GIF.PACKED, 2, false, {0xe}) - print("HEAD BEFORE = " .. self.buf.head) - self.buf:settex(0, tex.basePtr, math.floor(tex.width/64), GS.PSM24, 6, 6, 0, 0, 0, 0, 0) - print("HEAD AFTER = " .. self.buf.head) + local pb = math.floor(tex.basePtr/64) + local pw = math.floor(tex.width/64) + GIF.tag(self.buf, GIF.PACKED, 4, false, {0xe}) + GIF.texA(self.buf, 0x80, 0x80) + GIF.tex1(self.buf, true, 0, true, 0, 0) + self.buf:settex(0, pb, pw, tex.format, 6, 6, 0, 1, 0, 0, 0) + -- GIF.mipTbp1(self.buf, 0, pb, pw, pb, pw, pb, pw) + -- GIF.mipTbp2(self.buf, 0, pb, pw, pb, pw, pb, pw) GIF.primAd(self.buf, P.PRIM.SPRITE, false, true, false) + --GIF.packedRGBAQ(self.buf, self.col.r, self.col.g, self.col.b, self.col.a) self.tagLoopPtr = GIF.tag(self.buf, 0, 1, false, DRAW_FMT_SPRITE) self.loopCount = 0 self.state = DRAW_SPRITE end + GIF.packedST(self.buf, u1, v1) GIF.packedRGBAQ(self.buf, self.col.r, self.col.g, self.col.b, self.col.a) - GIF.packedUV(self.buf, u1, v1) GIF.packedXYZ2(self.buf, 0x8000 + (x*16), 0x8000 + (y*16), 0) - GIF.packedUV(self.buf, u2, v2) + GIF.packedST(self.buf, u2, v2) + GIF.packedRGBAQ(self.buf, self.col.r, self.col.g, self.col.b, self.col.a) GIF.packedXYZ2(self.buf, 0x8000 + ((x+w)*16), 0x8000 + ((y+h)*16), 0) self.loopCount = self.loopCount + 1 end diff --git a/script/gif.lua b/script/gif.lua index 9d0b57c..0d4cd3a 100644 --- a/script/gif.lua +++ b/script/gif.lua @@ -72,11 +72,39 @@ function gif.trxDir(b, dir) gif.setAd(b, P.REG.TRXDIR, dir, 0) end +function gif.tex1(b, lcm, mxl, mtba, l, k) + local v1 = mxl * 4 + if lcm then v1 = v1 + 1 end + if mtba then v1 = v1 + 2^8 end + v1 = v1 + math.floor(l * (2^18)) + gif.setAd(b, P.REG.TEX1, v1, k) +end + +function gif.texA(b, a0, a1) + gif.setAd(b, P.REG.TEXA, a0, a1) +end + +function gif.tex2(b, v) + gif.setAd(b, P.REG.TEX2, v, 0) +end + +function gif.mipTbp1(b, ctx, p1, w1, p2, w2, p3, w3) + b:setMipTbp(p1, w1, p2, w2, p3, w3) + b:pushint(P.REG.MIPTBP1 + ctx) + b:pushint(0) +end + +function gif.mipTbp2(b, ctx, p1, w1, p2, w2, p3, w3) + b:setMipTbp(p1, w1, p2, w2, p3, w3) + b:pushint(P.REG.MIPTBP2 + ctx) + b:pushint(0) +end + function gif.primAd(b, primType, shaded, textured, aa) local bits = primType - if shaded then bits = bits + 0x4 end - if textured then bits = bits + 0x8 end - if aa then bits = bits + 0x40 end + if shaded then bits = bits + 0x8 end + if textured then bits = bits + 0x10 end + if aa then bits = bits + 0x80 end gif.setAd(b, P.REG.PRIM, bits, 0) end @@ -95,10 +123,19 @@ function gif.packedXYZ2(b, x, y, z) end function gif.packedUV(b, u, v) - b:pushint(u) - b:pushint(v) + local vv = u + math.floor(v * 2^16) + print("u,v -> " .. vv) + b:pushint(vv) b:pushint(0) b:pushint(0) + b:pushint(0) +end + +function gif.packedST(b, s, t) + b:pushfloat(s) + b:pushfloat(t) + b:pushfloat(0.0) + b:pushint(0) end function gif.texflush(b) diff --git a/script/ps2const.lua b/script/ps2const.lua index 5280ef8..b6aa291 100644 --- a/script/ps2const.lua +++ b/script/ps2const.lua @@ -16,8 +16,11 @@ return { TRXREG=0x52, TRXDIR=0x53, TEXFLUSH=0x3F, - TEX0 = 0x6, - TEX1 = 0x7, + TEX0 = 0x06, + TEX1 = 0x14, + TEXA = 0x3B, + MIPTBP1 = 0x34, + MIPTBP2 = 0x36, }, ENABLE=0x1, DISABLE=0x0, diff --git a/script/texture.lua b/script/texture.lua index a208443..8706a7c 100644 --- a/script/texture.lua +++ b/script/texture.lua @@ -16,21 +16,18 @@ end local gs = nil -local testTexBuf = nil -local tex = makeTex(64, 64, 0xff) -local testTexBuf = 0 +local tex = makeTex(64, 64, 0x800000ff) local tt = { basePtr = 0, width = 64, height = 64, data = tex, + format = GS.PSM32 } function PS2PROG.start() DMA.init(DMA.GIF) gs = GS.newState(640, 448, GS.INTERLACED, GS.NTSC) - --local fb = gs:alloc(640, 448, GS.PSM24) - --local zb = gs:alloc(640, 448, GS.PSMZ24) local fb = VRAM.buffer(640, 440, GS.PSM24, 256) local zb = VRAM.buffer(640, 440, GS.PSMZ24, 256) print("setting new buffers") @@ -42,27 +39,26 @@ function PS2PROG.start() local height = 64 for i=0,width,1 do for j=0,height,1 do - if (i+j)%2 == 0 then - tex[j*width + i] = 0xff00 - end + local r = 0x0f + local g = 50 + math.floor(200 * j/height) + tex[j*width + i] = 0x80000000 + math.floor(r + (g*2^8)) end end + local texVramSize = 1024 - testTexBuf = VRAM.alloc(texVramSize, 256) - tt.basePtr = math.floor(testTexBuf/64) - print("got texture VRAM addr = " .. testTexBuf) + tt.basePtr = VRAM.alloc(texVramSize, 256) + print("got texture VRAM addr = " .. tt.basePtr) -- ib = RM.tmpBuffer(1000) - local taddr = math.floor(testTexBuf/256) ib = RM.getDrawBuffer(5000) GIF.tag(ib, GIF.PACKED, 4, false, {0xe}) - GIF.bitBltBuf(ib, math.floor(testTexBuf/64), math.floor(width/64), GS.PSM24) + GIF.bitBltBuf(ib, math.floor(tt.basePtr/64), math.floor(tt.width/64), tt.format) GIF.trxPos(ib,0,0,0,0,0) - GIF.trxReg(ib,width,height) + GIF.trxReg(ib,tt.width,tt.height) GIF.trxDir(ib, 0) - local eeSize = width*height*4 + local eeSize = tt.width*tt.height*4 local qwc = math.floor(eeSize / 16) print("image sent in " .. qwc .. " qwords") if qwc % 16 ~= 0 then qwc = qwc + 1 end @@ -84,10 +80,11 @@ function PS2PROG.start() DMA.send(ib, DMA.GIF) ib = RM.getDrawBuffer(5000) packets = packets - 1 + tb = tb + 1 end if remain > 0 then - local base = math.floor(qwc/blocksize)*blocksize*4 + local base = tb*blocksize*4 GIF.tag(ib, GIF.IMAGE, remain, false, {1}) for i=1,remain,1 do ib:pushint(tex[base + i]) @@ -110,7 +107,7 @@ function PS2PROG.frame() local db = D2D:getBuffer() db:frameStart(gs) D2D:setColour(255,255,255,0x80) - D2D:rectuv(tt, -200, -200, 200, 200, 0, 0, 2^14-1, 2^14 - 1) + D2D:rectuv(tt, -200, -200, 200, 200, 0, 0, 1, 1) db = D2D:getBuffer() db:frameEnd(gs) D2D:kick() diff --git a/script/vram.lua b/script/vram.lua index 973ade2..1290a1c 100644 --- a/script/vram.lua +++ b/script/vram.lua @@ -15,7 +15,9 @@ function vram.alloc(b, align) end function vram.size(w, h, psm, align) - w = vpa(w, 64) + if w%align ~= 0 then + w = vpa(w, 64) + end local size = w*h if psm == GS.PSM16 or psm == GS.PSM16S or psm == GS.PSMZ16 or psm == GS.PSMZ16S then math.floor(width*height*0.5) diff --git a/src/bufferlua.c b/src/bufferlua.c index 57a4c40..d5d3e41 100644 --- a/src/bufferlua.c +++ b/src/bufferlua.c @@ -38,6 +38,30 @@ static int buffer_pushint(lua_State *l) { return 0; } +static int buffer_pushfloat(lua_State *l) { + float value = (float) lua_tointeger(l, 2); + lua_pushstring(l, "ptr"); + lua_gettable(l, 1); + float *ptr = (float *) lua_touserdata(l, -1); + lua_pushstring(l, "head"); + lua_gettable(l, 1); + int head = lua_tointeger(l, -1); + // TODO: check size + // ASSUME 4byte int + if (head%4 != 0) { + // TODO: manually bitmask etc + logerr("cannot write int to buffer, head%%4 != 0 (%d|%d)", head, head%4); + return 0; + } + // info("db write int %d @ %d", value, head); + ptr[head/4] = value; + // info("db head -> %d", head+4); + lua_pushinteger(l, head+4); + lua_setfield(l, 1, "head"); + return 0; +} + + static int buffer_settex(lua_State *l) { int reg= lua_tointeger(l, 2); int tbp= lua_tointeger(l, 3); @@ -65,7 +89,7 @@ static int buffer_settex(lua_State *l) { int *base = ptr + (head/4); int v1 = tbp | (tbw<<14) | (psm<<20) | (tw<<26) | ((th&0x3) << 30); - int v2 = ((th&0x5)>>2) | (tcc<<2) | (tfx<<3); + int v2 = ((th&0x5)>>2) | (tcc<<1) | (tfx<<2); int v3 = 0x6+reg; int v4 = 0; *(base) = v1; @@ -81,7 +105,40 @@ static int buffer_settex(lua_State *l) { info("backtrack tex0 :: %08x %08x %08x %08x", base[0], base[1], base[2], base[3]); return 0; +} + +static int buffer_pushmiptbp(lua_State *l) { + int p1 = lua_tointeger(l, 2); + int w1 = lua_tointeger(l, 3); + int p2 = lua_tointeger(l, 4); + int w2 = lua_tointeger(l, 5); + int p3 = lua_tointeger(l, 6); + int w3 = lua_tointeger(l, 7); + + lua_pushstring(l, "ptr"); + lua_gettable(l, 1); + int *ptr = (int *) lua_touserdata(l, -1); + lua_pushstring(l, "head"); + lua_gettable(l, 1); + int head = lua_tointeger(l, -1); + + if (head%4 != 0) { + logerr("buffer head must be =0%%4, got %d", head%4); + lua_pushstring(l, "buffer write failed"); + lua_error(l); + return 1; + } + + int v1 = p1 | (w1 << 14) | (p2 << 20); + int overflow = (p2&0x1000) >> 12; + int v2 = overflow | (w2 << 2) | (p3 << 8) | (w3 << 22); + + (ptr+head)[0] = v1; + (ptr+head)[1] = v2; + lua_pushinteger(l, head+8); + lua_setfield(l, 1, "head"); + return 0; } static int buffer_copy(lua_State *l) { @@ -126,9 +183,16 @@ int drawlua_init(lua_State *l) { lua_pushcfunction(l, buffer_pushint); lua_setfield(l, -2, "pushint"); + lua_pushcfunction(l, buffer_pushfloat); + lua_setfield(l, -2, "pushfloat"); + + lua_pushcfunction(l, buffer_settex); lua_setfield(l, -2, "settex"); + lua_pushcfunction(l, buffer_pushmiptbp); + lua_setfield(l, -2, "setMipTbp"); + lua_pushcfunction(l, buffer_copy); lua_setfield(l, -2, "copy"); From a2edb8395c5be5a1f9f18874b3c35c2281f5d104 Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Thu, 14 Oct 2021 13:14:37 +1100 Subject: [PATCH 16/29] texture loading so close i can taste it --- script/texture.lua | 29 +++++++++--------- src/Makefile | 2 +- src/bufferlua.c | 20 ++++++++++++- src/ps2lua.c | 4 ++- src/tga.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 111 insertions(+), 18 deletions(-) create mode 100644 src/tga.c diff --git a/script/texture.lua b/script/texture.lua index 8706a7c..d96c60c 100644 --- a/script/texture.lua +++ b/script/texture.lua @@ -16,16 +16,19 @@ end local gs = nil -local tex = makeTex(64, 64, 0x800000ff) +--local tex = makeTex(64, 64, 0x800000ff) local tt = { basePtr = 0, width = 64, height = 64, - data = tex, + data = nil, format = GS.PSM32 } function PS2PROG.start() + tt.data = TGA.load("host:test.tga", 64, 64) + print("loaded tga: size = " .. tt.data.size .. " head = " .. tt.data.head) + DMA.init(DMA.GIF) gs = GS.newState(640, 448, GS.INTERLACED, GS.NTSC) local fb = VRAM.buffer(640, 440, GS.PSM24, 256) @@ -34,6 +37,7 @@ function PS2PROG.start() gs:setBuffers(fb, zb) gs:clearColour(0x2b, 0x2b, 0x2b) + --[[ print("detailing texture") local width = 64 local height = 64 @@ -44,6 +48,7 @@ function PS2PROG.start() tex[j*width + i] = 0x80000000 + math.floor(r + (g*2^8)) end end + ]] local texVramSize = 1024 tt.basePtr = VRAM.alloc(texVramSize, 256) @@ -66,17 +71,14 @@ function PS2PROG.start() local packets = math.floor(qwc / blocksize) local remain = qwc % blocksize -- print("transmitting") - print("transmitting in " .. packets .. " packets with " .. remain .. " lefte") + print("transmitting in " .. packets .. " packets with " .. remain .. " left") local tb = 0 while packets > 0 do GIF.tag(ib, GIF.IMAGE, blocksize, false, {0}) - for i=1,blocksize,1 do - ib:pushint(tex[tb*blocksize*4 + i]) - ib:pushint(tex[tb*blocksize*4 + i+1]) - ib:pushint(tex[tb*blocksize*4 + i+2]) - ib:pushint(tex[tb*blocksize*4 + i+3]) - end + print("copy from TT " .. tb*blocksize*4 .. " to IB " .. ib.head .. " -- " .. blocksize*16) + tt.data:copy(ib, ib.head, tb*blocksize*4, blocksize*16) + ib.head = ib.head + blocksize*16 DMA.send(ib, DMA.GIF) ib = RM.getDrawBuffer(5000) packets = packets - 1 @@ -86,12 +88,9 @@ function PS2PROG.start() if remain > 0 then local base = tb*blocksize*4 GIF.tag(ib, GIF.IMAGE, remain, false, {1}) - for i=1,remain,1 do - ib:pushint(tex[base + i]) - ib:pushint(tex[base + i + 1]) - ib:pushint(tex[base + i + 2]) - ib:pushint(tex[base + i + 3]) - end + print("copy from TT " .. base .. " to IB " .. ib.head .. " -- " .. remain*16) + tt.data:copy(ib, ib.head, base, remain*16) + ib.head = ib.head + remain*16 end print("adding texflush") diff --git a/src/Makefile b/src/Makefile index 66e39b9..c8f0a0a 100644 --- a/src/Makefile +++ b/src/Makefile @@ -2,7 +2,7 @@ PS2SDK=/usr/local/ps2dev/ps2sdk EE_BIN=test.elf #EE_OBJS=main.o gs.o mesh.o draw.o math.o pad.o drawstate.o script.o luadrawstate.o -EE_OBJS=ps2lua.o gslua.o dmalua.o bufferlua.o +EE_OBJS=ps2lua.o gslua.o dmalua.o bufferlua.o tga.o EE_LIBS=-ldma -lgraph -ldraw -lkernel -ldebug -lmath3d -lm -lpad -llua EE_CFLAGS+=-Wall --std=c99 -Wno-sign-compare -fno-strict-aliasing -fno-exceptions -DLUA_USE_PS2 diff --git a/src/bufferlua.c b/src/bufferlua.c index d5d3e41..7d98aa3 100644 --- a/src/bufferlua.c +++ b/src/bufferlua.c @@ -5,6 +5,7 @@ #include #include +#include #include "log.h" #include "script.h" @@ -142,7 +143,24 @@ static int buffer_pushmiptbp(lua_State *l) { } static int buffer_copy(lua_State *l) { - info("UNIMPLEMENTED"); + // arg 1 = buffer from + // arg 2 = buffer TO + // arg 3 = buffer TO offset + // arg 4 = buffer FROM offset + // arg 5 = n bytes + lua_pushstring(l, "ptr"); + lua_gettable(l, 1); + char *ptr_from = lua_touserdata(l, -1); + lua_pushstring(l, "ptr"); + lua_gettable(l, 2); + char *ptr_to = lua_touserdata(l, -1); + + int to_offset = lua_tointeger(l, 3); + int from_offset = lua_tointeger(l, 4); + int n = lua_tointeger(l, 5); + + memcpy(ptr_to + to_offset, ptr_from + from_offset, n); + return 0; } diff --git a/src/ps2lua.c b/src/ps2lua.c index 2e49eaf..936096f 100644 --- a/src/ps2lua.c +++ b/src/ps2lua.c @@ -12,6 +12,8 @@ #define INIT_SCRIPT "host:script/ps2init.lua" +int lua_tga_init(lua_State *l); + static int ps2luaprog_start_nil(lua_State *l) { info("default start..."); return 0; @@ -120,7 +122,7 @@ int main(int argc, char *argv[]) { ps2luaprog_init(L); dma_lua_init(L); gs_lua_init(L); - + lua_tga_init(L); //TODO: better abstraction for drawlua_* drawlua_init(L); diff --git a/src/tga.c b/src/tga.c new file mode 100644 index 0000000..b154530 --- /dev/null +++ b/src/tga.c @@ -0,0 +1,74 @@ +#include +#include +#include + +#include +#include +#include + +#include "log.h" + +struct __attribute__((__packed__)) tga_header { + uint8_t idlen; + uint8_t colMapType; + uint8_t imgType; + + uint16_t firstColEntryIndex; + uint16_t colMapLength; + uint8_t colMapBps; + + uint16_t xorigin; + uint16_t yorigin; + uint16_t width; + uint16_t height; + uint8_t bps; + uint8_t descriptor; +}; + +int load_tga_to_raw(const char *fname, void *buffer) { + info("loading TGA %s", fname); + FILE *f = fopen(fname, "rb"); + struct tga_header header = {0}; + size_t rc = fread(&header, 1, 18, f); + + info("reading ID data - %d bytes", header.idlen); + char idData[255]; + rc = fread(idData, 1, header.idlen, f); + + // bytes per pixel from bits per pixel + int bpp = header.bps/8; + info("reading image data - %d bytes", header.width*header.height*bpp); + rc = fread(buffer, bpp, header.width*header.height*bpp, f); + fclose(f); + return 1; +} + +int load_tga_lua(lua_State *l) { + const char *fname = lua_tostring(l, 1); + int width = lua_tointeger(l, 2); + int height = lua_tointeger(l, 3); + uint32_t *b = malloc(width*height*4); + load_tga_to_raw(fname, b); + + lua_createtable(l, 0, 5); + lua_pushinteger(l, 0); + lua_setfield(l, -2, "head"); + lua_pushinteger(l, width*height*4); + lua_setfield(l, -2, "size"); + lua_pushlightuserdata(l, b); + lua_setfield(l, -2, "ptr"); + + luaL_getmetatable(l, "ps2.buffer"); + lua_setmetatable(l, -2); + + // return new buffer + return 1; +} + +int lua_tga_init(lua_State *l) { + lua_createtable(l, 0, 1); + lua_pushcfunction(l, load_tga_lua); + lua_setfield(l, -2, "load"); + lua_setglobal(l, "TGA"); + return 0; +} From 9e402d52d2aec352d7c604127880c0f8c4b77c13 Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Fri, 15 Oct 2021 05:51:34 +1100 Subject: [PATCH 17/29] working texture + tga for 32bit (with alpha) --- script/draw2d.lua | 3 +-- script/gif.lua | 2 +- script/texture.lua | 8 ++++---- src/bufferlua.c | 2 +- src/ps2lua.c | 10 ++++++++++ src/tga.c | 12 +++++++++++- 6 files changed, 28 insertions(+), 9 deletions(-) diff --git a/script/draw2d.lua b/script/draw2d.lua index 2a42f1e..997297d 100644 --- a/script/draw2d.lua +++ b/script/draw2d.lua @@ -21,7 +21,6 @@ local draw = { } - function draw:newBuffer() self.state = DRAW_NONE self.loopCount = 0 @@ -55,7 +54,7 @@ function draw:rectuv(tex, x, y, w, h, u1, v1, u2, v2) GIF.tag(self.buf, GIF.PACKED, 4, false, {0xe}) GIF.texA(self.buf, 0x80, 0x80) GIF.tex1(self.buf, true, 0, true, 0, 0) - self.buf:settex(0, pb, pw, tex.format, 6, 6, 0, 1, 0, 0, 0) + self.buf:settex(0, pb, pw, tex.format, math.floor(log2(tex.width)), math.floor(log2(tex.height)), 0, 1, 0, 0, 0) -- GIF.mipTbp1(self.buf, 0, pb, pw, pb, pw, pb, pw) -- GIF.mipTbp2(self.buf, 0, pb, pw, pb, pw, pb, pw) GIF.primAd(self.buf, P.PRIM.SPRITE, false, true, false) diff --git a/script/gif.lua b/script/gif.lua index 0d4cd3a..cfb4406 100644 --- a/script/gif.lua +++ b/script/gif.lua @@ -134,7 +134,7 @@ end function gif.packedST(b, s, t) b:pushfloat(s) b:pushfloat(t) - b:pushfloat(0.0) + b:pushfloat(1.0) b:pushint(0) end diff --git a/script/texture.lua b/script/texture.lua index d96c60c..8e57658 100644 --- a/script/texture.lua +++ b/script/texture.lua @@ -26,7 +26,7 @@ local tt = { } function PS2PROG.start() - tt.data = TGA.load("host:test.tga", 64, 64) + tt.data = TGA.load("host:font.tga", 64, 64) print("loaded tga: size = " .. tt.data.size .. " head = " .. tt.data.head) DMA.init(DMA.GIF) @@ -76,8 +76,8 @@ function PS2PROG.start() local tb = 0 while packets > 0 do GIF.tag(ib, GIF.IMAGE, blocksize, false, {0}) - print("copy from TT " .. tb*blocksize*4 .. " to IB " .. ib.head .. " -- " .. blocksize*16) - tt.data:copy(ib, ib.head, tb*blocksize*4, blocksize*16) + print("copy from TT " .. tb*blocksize*16 .. " to IB " .. ib.head .. " -- " .. blocksize*16) + tt.data:copy(ib, ib.head, tb*blocksize*16, blocksize*16) ib.head = ib.head + blocksize*16 DMA.send(ib, DMA.GIF) ib = RM.getDrawBuffer(5000) @@ -86,7 +86,7 @@ function PS2PROG.start() end if remain > 0 then - local base = tb*blocksize*4 + local base = tb*blocksize*16 GIF.tag(ib, GIF.IMAGE, remain, false, {1}) print("copy from TT " .. base .. " to IB " .. ib.head .. " -- " .. remain*16) tt.data:copy(ib, ib.head, base, remain*16) diff --git a/src/bufferlua.c b/src/bufferlua.c index 7d98aa3..42c2a4d 100644 --- a/src/bufferlua.c +++ b/src/bufferlua.c @@ -40,7 +40,7 @@ static int buffer_pushint(lua_State *l) { } static int buffer_pushfloat(lua_State *l) { - float value = (float) lua_tointeger(l, 2); + float value = (float) lua_tonumber(l, 2); lua_pushstring(l, "ptr"); lua_gettable(l, 1); float *ptr = (float *) lua_touserdata(l, -1); diff --git a/src/ps2lua.c b/src/ps2lua.c index 936096f..19824d8 100644 --- a/src/ps2lua.c +++ b/src/ps2lua.c @@ -5,6 +5,7 @@ #include #include #include +#include #include "log.h" @@ -23,6 +24,13 @@ static int ps2luaprog_frame_nil(lua_State *l) { return 0; } +static int ps2lua_log2(lua_State *l) { + int n = lua_tointeger(l, 1); + float res = log2f(n); + lua_pushnumber(l, res); + return 1; +} + int ps2luaprog_init(lua_State *l) { lua_createtable(l, 0, 2); lua_pushcfunction(l, ps2luaprog_start_nil); @@ -30,6 +38,8 @@ int ps2luaprog_init(lua_State *l) { lua_pushcfunction(l, ps2luaprog_frame_nil); lua_setfield(l, -2, "frame"); lua_setglobal(l, "PS2PROG"); + lua_pushcfunction(l, ps2lua_log2); + lua_setglobal(l, "log2"); return 0; } diff --git a/src/tga.c b/src/tga.c index b154530..d3008e5 100644 --- a/src/tga.c +++ b/src/tga.c @@ -25,6 +25,7 @@ struct __attribute__((__packed__)) tga_header { uint8_t descriptor; }; +char tmp_buffer[256*256*4]; int load_tga_to_raw(const char *fname, void *buffer) { info("loading TGA %s", fname); FILE *f = fopen(fname, "rb"); @@ -38,7 +39,16 @@ int load_tga_to_raw(const char *fname, void *buffer) { // bytes per pixel from bits per pixel int bpp = header.bps/8; info("reading image data - %d bytes", header.width*header.height*bpp); - rc = fread(buffer, bpp, header.width*header.height*bpp, f); + rc = fread(tmp_buffer, bpp, header.width*header.height*bpp, f); + char *to = (char *) buffer; + for ( int i = 0; i < header.width; i++ ) { + for ( int j = 0; j < header.height; j++ ) { + to[ (j*header.width + i) *4 + 3 ] = tmp_buffer[ (j*header.width + i) *4 + 3 ]; + to[ (j*header.width + i) *4 + 2 ] = tmp_buffer[ (j*header.width + i) *4 + 0 ]; + to[ (j*header.width + i) *4 + 1 ] = tmp_buffer[ (j*header.width + i) *4 + 1 ]; + to[ (j*header.width + i) *4 + 0 ] = tmp_buffer[ (j*header.width + i) *4 + 2 ]; + } + } fclose(f); return 1; } From 7f344bcc233bad3294a1db29a7cd495410dcda15 Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Fri, 15 Oct 2021 09:09:06 +1100 Subject: [PATCH 18/29] make texture loading not part of PS2PROG.start, cleanup logging --- script/draw2d.lua | 4 +-- script/gif.lua | 4 +-- script/texture.lua | 75 ++++++++++++++++++++++------------------------ script/vram.lua | 2 +- 4 files changed, 41 insertions(+), 44 deletions(-) diff --git a/script/draw2d.lua b/script/draw2d.lua index 997297d..9e8fff7 100644 --- a/script/draw2d.lua +++ b/script/draw2d.lua @@ -44,7 +44,7 @@ function draw:rect(x, y, w, h) draw:triangle(x, y+h, x+w, y+h, x+w, y) end -function draw:rectuv(tex, x, y, w, h, u1, v1, u2, v2) +function draw:sprite(tex, x, y, w, h, u1, v1, u2, v2) if self.loopCount > 10000 then self:kick() end if self.buf.size - self.buf.head < 80 then self:kick() end if self.state ~= DRAW_SPRITE or self.currentTexPtr ~= tex.basePtr then @@ -54,7 +54,7 @@ function draw:rectuv(tex, x, y, w, h, u1, v1, u2, v2) GIF.tag(self.buf, GIF.PACKED, 4, false, {0xe}) GIF.texA(self.buf, 0x80, 0x80) GIF.tex1(self.buf, true, 0, true, 0, 0) - self.buf:settex(0, pb, pw, tex.format, math.floor(log2(tex.width)), math.floor(log2(tex.height)), 0, 1, 0, 0, 0) + self.buf:settex(0, pb, pw, tex.format, math.floor(log2(tex.width)), math.floor(log2(tex.height)), 0, 3, 0, 0, 0) -- GIF.mipTbp1(self.buf, 0, pb, pw, pb, pw, pb, pw) -- GIF.mipTbp2(self.buf, 0, pb, pw, pb, pw, pb, pw) GIF.primAd(self.buf, P.PRIM.SPRITE, false, true, false) diff --git a/script/gif.lua b/script/gif.lua index cfb4406..39e6fb0 100644 --- a/script/gif.lua +++ b/script/gif.lua @@ -20,8 +20,8 @@ function gif.tag(b, flg, nloop, eop, regs) if nreg > 16 then error("invalid gif tag: nreg = " .. #regs) end if nreg == 16 then nreg = 0 end local w2 = (math.floor(nreg * 2^28) + flg) - print("GIFTAG: pushing " .. string.format("0x%x", w2) .. " :: " .. w2) - print("GIFTAG: flag = " .. flg) + --print("GIFTAG: pushing " .. string.format("0x%x", w2) .. " :: " .. w2) + --print("GIFTAG: flag = " .. flg) b:pushint(w2) local reg = 0 local regc = 0 diff --git a/script/texture.lua b/script/texture.lua index 8e57658..8eacb5f 100644 --- a/script/texture.lua +++ b/script/texture.lua @@ -17,42 +17,27 @@ end local gs = nil --local tex = makeTex(64, 64, 0x800000ff) -local tt = { - basePtr = 0, - width = 64, - height = 64, - data = nil, - format = GS.PSM32 -} +local testTex = {} +local fnt = nil -function PS2PROG.start() - tt.data = TGA.load("host:font.tga", 64, 64) - print("loaded tga: size = " .. tt.data.size .. " head = " .. tt.data.head) - DMA.init(DMA.GIF) - gs = GS.newState(640, 448, GS.INTERLACED, GS.NTSC) - local fb = VRAM.buffer(640, 440, GS.PSM24, 256) - local zb = VRAM.buffer(640, 440, GS.PSMZ24, 256) - print("setting new buffers") - gs:setBuffers(fb, zb) - gs:clearColour(0x2b, 0x2b, 0x2b) +-- CURRENT TEXTURE LOADER CANNOT BE USED MID-FRAME!!!!! +function loadTexture(fname, w, h) + print("LOAD TEX: " .. fname .. " @ PSM32") + local tt = { + basePtr = 0, + width = w, + height = h, + data = nil, + format = GS.PSM32 + } - --[[ - print("detailing texture") - local width = 64 - local height = 64 - for i=0,width,1 do - for j=0,height,1 do - local r = 0x0f - local g = 50 + math.floor(200 * j/height) - tex[j*width + i] = 0x80000000 + math.floor(r + (g*2^8)) - end - end - ]] + tt.data = TGA.load(fname, w, h) - local texVramSize = 1024 + -- only works for power of 2 textures @ psm32!!!!! + local texVramSize = w*h*4 tt.basePtr = VRAM.alloc(texVramSize, 256) - print("got texture VRAM addr = " .. tt.basePtr) + print("LOAD TEX: got texture VRAM addr = " .. tt.basePtr) -- ib = RM.tmpBuffer(1000) ib = RM.getDrawBuffer(5000) @@ -63,20 +48,19 @@ function PS2PROG.start() GIF.trxReg(ib,tt.width,tt.height) GIF.trxDir(ib, 0) + -- ASSUMPTION about format! local eeSize = tt.width*tt.height*4 local qwc = math.floor(eeSize / 16) - print("image sent in " .. qwc .. " qwords") if qwc % 16 ~= 0 then qwc = qwc + 1 end local blocksize = math.floor(4496/16) local packets = math.floor(qwc / blocksize) local remain = qwc % blocksize - -- print("transmitting") - print("transmitting in " .. packets .. " packets with " .. remain .. " left") + print("LOAD TEX: transmitting in " .. packets .. " packets with " .. remain .. " left") local tb = 0 while packets > 0 do GIF.tag(ib, GIF.IMAGE, blocksize, false, {0}) - print("copy from TT " .. tb*blocksize*16 .. " to IB " .. ib.head .. " -- " .. blocksize*16) + print("LOAD TEX: copy from TT " .. tb*blocksize*16 .. " to IB " .. ib.head .. " -- " .. blocksize*16) tt.data:copy(ib, ib.head, tb*blocksize*16, blocksize*16) ib.head = ib.head + blocksize*16 DMA.send(ib, DMA.GIF) @@ -93,11 +77,23 @@ function PS2PROG.start() ib.head = ib.head + remain*16 end - print("adding texflush") GIF.texflush(ib) - - print("dma sending") DMA.send(ib, DMA.GIF) + print("LOAD TEX: DMA send") + + return tt +end + +function PS2PROG.start() + testTex = loadTexture("host:test.tga", 64, 64) + fnt = loadTexture("host:bigfont.tga", 256, 64) + DMA.init(DMA.GIF) + gs = GS.newState(640, 448, GS.INTERLACED, GS.NTSC) + local fb = VRAM.buffer(640, 440, GS.PSM24, 256) + local zb = VRAM.buffer(640, 440, GS.PSMZ24, 256) + print("setting new buffers") + gs:setBuffers(fb, zb) + gs:clearColour(0x2b, 0x2b, 0x2b) end @@ -106,7 +102,8 @@ function PS2PROG.frame() local db = D2D:getBuffer() db:frameStart(gs) D2D:setColour(255,255,255,0x80) - D2D:rectuv(tt, -200, -200, 200, 200, 0, 0, 1, 1) + D2D:sprite(testTex, -200, -200, 200, 200, 0, 0, 1, 1) + D2D:sprite(fnt, 50, 100, 256, 64, 0, 0, 1, 1) db = D2D:getBuffer() db:frameEnd(gs) D2D:kick() diff --git a/script/vram.lua b/script/vram.lua index 1290a1c..8594d0a 100644 --- a/script/vram.lua +++ b/script/vram.lua @@ -18,7 +18,7 @@ function vram.size(w, h, psm, align) if w%align ~= 0 then w = vpa(w, 64) end - local size = w*h + local size = w*h*4 if psm == GS.PSM16 or psm == GS.PSM16S or psm == GS.PSMZ16 or psm == GS.PSMZ16S then math.floor(width*height*0.5) elseif psm == GS.PSM8 then size = math.floor(width*height*2^-2) From 62face01c4e78548c083c4079d0a2f6f1d7491b0 Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Fri, 15 Oct 2021 09:28:23 +1100 Subject: [PATCH 19/29] allow colour blending on textures --- script/draw2d.lua | 2 +- script/texture.lua | 2 +- src/dmalua.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/script/draw2d.lua b/script/draw2d.lua index 9e8fff7..1383c97 100644 --- a/script/draw2d.lua +++ b/script/draw2d.lua @@ -54,7 +54,7 @@ function draw:sprite(tex, x, y, w, h, u1, v1, u2, v2) GIF.tag(self.buf, GIF.PACKED, 4, false, {0xe}) GIF.texA(self.buf, 0x80, 0x80) GIF.tex1(self.buf, true, 0, true, 0, 0) - self.buf:settex(0, pb, pw, tex.format, math.floor(log2(tex.width)), math.floor(log2(tex.height)), 0, 3, 0, 0, 0) + self.buf:settex(0, pb, pw, tex.format, math.floor(log2(tex.width)), math.floor(log2(tex.height)), 0, 1, 0, 0, 0) -- GIF.mipTbp1(self.buf, 0, pb, pw, pb, pw, pb, pw) -- GIF.mipTbp2(self.buf, 0, pb, pw, pb, pw, pb, pw) GIF.primAd(self.buf, P.PRIM.SPRITE, false, true, false) diff --git a/script/texture.lua b/script/texture.lua index 8eacb5f..8f2be52 100644 --- a/script/texture.lua +++ b/script/texture.lua @@ -101,7 +101,7 @@ function PS2PROG.frame() D2D:newBuffer() local db = D2D:getBuffer() db:frameStart(gs) - D2D:setColour(255,255,255,0x80) + D2D:setColour(0x80,0x80,0x80,0x80) D2D:sprite(testTex, -200, -200, 200, 200, 0, 0, 1, 1) D2D:sprite(fnt, 50, 100, 256, 64, 0, 0, 1, 1) db = D2D:getBuffer() diff --git a/src/dmalua.c b/src/dmalua.c index 2bed363..02dfb24 100644 --- a/src/dmalua.c +++ b/src/dmalua.c @@ -38,7 +38,7 @@ static int dma_send_buffer(lua_State *l) { int channel = lua_tointeger(l, 2); // print buffer for debugging - print_buffer(ptr, head/16); + //t,print_buffer(ptr, head/16); // info("DMA send :: sending %d qwords on channel %d", head/16, channel); dma_channel_send_normal(channel, ptr, head/16, 0, 0); From 8741cbc013dfb04b5878901a3f45b696613446fc Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Fri, 15 Oct 2021 11:13:15 +1100 Subject: [PATCH 20/29] working pad abstraction in Lua --- script/draw2d.lua | 8 ++++++-- script/texture.lua | 7 ++++++- src/Makefile | 2 +- src/bufferlua.c | 9 +++++---- src/dmalua.c | 2 +- src/pad.h | 28 +++++++++++++++++----------- src/ps2lua.c | 9 +++++++-- 7 files changed, 43 insertions(+), 22 deletions(-) diff --git a/script/draw2d.lua b/script/draw2d.lua index 1383c97..41fc7bf 100644 --- a/script/draw2d.lua +++ b/script/draw2d.lua @@ -63,12 +63,16 @@ function draw:sprite(tex, x, y, w, h, u1, v1, u2, v2) self.loopCount = 0 self.state = DRAW_SPRITE end + local ix = math.floor(x) + local iy = math.floor(y) + local ix2 = math.floor(x+w) + local iy2 = math.floor(y+h) GIF.packedST(self.buf, u1, v1) GIF.packedRGBAQ(self.buf, self.col.r, self.col.g, self.col.b, self.col.a) - GIF.packedXYZ2(self.buf, 0x8000 + (x*16), 0x8000 + (y*16), 0) + GIF.packedXYZ2(self.buf, 0x8000 + (ix*16), 0x8000 + (iy*16), 0) GIF.packedST(self.buf, u2, v2) GIF.packedRGBAQ(self.buf, self.col.r, self.col.g, self.col.b, self.col.a) - GIF.packedXYZ2(self.buf, 0x8000 + ((x+w)*16), 0x8000 + ((y+h)*16), 0) + GIF.packedXYZ2(self.buf, 0x8000 + (ix2*16), 0x8000 + (iy2*16), 0) self.loopCount = self.loopCount + 1 end diff --git a/script/texture.lua b/script/texture.lua index 8f2be52..17d7f6d 100644 --- a/script/texture.lua +++ b/script/texture.lua @@ -97,12 +97,14 @@ function PS2PROG.start() end +xx = -200 +local dt = 1/60 function PS2PROG.frame() D2D:newBuffer() local db = D2D:getBuffer() db:frameStart(gs) D2D:setColour(0x80,0x80,0x80,0x80) - D2D:sprite(testTex, -200, -200, 200, 200, 0, 0, 1, 1) + D2D:sprite(testTex, xx, -200, 200, 200, 0, 0, 1, 1) D2D:sprite(fnt, 50, 100, 256, 64, 0, 0, 1, 1) db = D2D:getBuffer() db:frameEnd(gs) @@ -110,6 +112,9 @@ function PS2PROG.frame() print("tris/frame = " .. D2D.rawtri .. ", KC=" .. D2D.kc) D2D.rawtri = 0 D2D.kc = 0 + + if PAD.held(PAD.LEFT) then xx = xx - 50*dt end + if PAD.held(PAD.RIGHT) then xx = xx + 50*dt end --db:free() end diff --git a/src/Makefile b/src/Makefile index c8f0a0a..8739376 100644 --- a/src/Makefile +++ b/src/Makefile @@ -2,7 +2,7 @@ PS2SDK=/usr/local/ps2dev/ps2sdk EE_BIN=test.elf #EE_OBJS=main.o gs.o mesh.o draw.o math.o pad.o drawstate.o script.o luadrawstate.o -EE_OBJS=ps2lua.o gslua.o dmalua.o bufferlua.o tga.o +EE_OBJS=ps2lua.o gslua.o dmalua.o bufferlua.o tga.o padlua.o EE_LIBS=-ldma -lgraph -ldraw -lkernel -ldebug -lmath3d -lm -lpad -llua EE_CFLAGS+=-Wall --std=c99 -Wno-sign-compare -fno-strict-aliasing -fno-exceptions -DLUA_USE_PS2 diff --git a/src/bufferlua.c b/src/bufferlua.c index 42c2a4d..e60fe18 100644 --- a/src/bufferlua.c +++ b/src/bufferlua.c @@ -90,6 +90,7 @@ static int buffer_settex(lua_State *l) { int *base = ptr + (head/4); int v1 = tbp | (tbw<<14) | (psm<<20) | (tw<<26) | ((th&0x3) << 30); + // TODO: 0x5??? i must mean 0x4 int v2 = ((th&0x5)>>2) | (tcc<<1) | (tfx<<2); int v3 = 0x6+reg; int v4 = 0; @@ -98,13 +99,13 @@ static int buffer_settex(lua_State *l) { *(base+2) = v3; *(base+3) = v4; - info("write tex0 :: %08x %08x %08x %08x", v1, v2, v3, v4); - info("head -> %d", head+16); + // info("write tex0 :: %08x %08x %08x %08x", v1, v2, v3, v4); + // info("head -> %d", head+16); lua_pushinteger(l, head+16); lua_setfield(l, 1, "head"); - info("backtrack tex0 :: %08x %08x %08x %08x", base[0], base[1], base[2], base[3]); + // info("backtrack tex0 :: %08x %08x %08x %08x", base[0], base[1], base[2], base[3]); return 0; } @@ -261,7 +262,7 @@ static int drawlua_start_frame(lua_State *l) { float halfw = st->fb.width / 2.f; float halfh = st->fb.height / 2.f; - info("clear screen :: (%d, %d, %d)", st->clear_r, st->clear_g, st->clear_b); + // info("clear screen :: (%d, %d, %d)", st->clear_r, st->clear_g, st->clear_b); qword_t *q = (qword_t *) (ptr + head); q = draw_disable_tests(q, 0, &st->zb); diff --git a/src/dmalua.c b/src/dmalua.c index 02dfb24..d656c0f 100644 --- a/src/dmalua.c +++ b/src/dmalua.c @@ -38,7 +38,7 @@ static int dma_send_buffer(lua_State *l) { int channel = lua_tointeger(l, 2); // print buffer for debugging - //t,print_buffer(ptr, head/16); + //print_buffer(ptr, head/16); // info("DMA send :: sending %d qwords on channel %d", head/16, channel); dma_channel_send_normal(channel, ptr, head/16, 0, 0); diff --git a/src/pad.h b/src/pad.h index b9d3ab6..c1683d2 100644 --- a/src/pad.h +++ b/src/pad.h @@ -2,17 +2,21 @@ #ifndef PAD_H #define PAD_H -#define BUTTON_1 0 -#define BUTTON_2 1 -#define BUTTON_L1 2 -#define BUTTON_R1 3 -#define BUTTON_L2 4 -#define BUTTON_R2 5 -#define DPAD_DOWN 6 -#define DPAD_LEFT 7 -#define DPAD_RIGHT 8 -#define DPAD_UP 9 -#define BTN_MAX 10 +#include + +#define BUTTON_X 0 +#define BUTTON_SQUARE 1 +#define BUTTON_TRIANGLE 2 +#define BUTTON_CIRCLE 3 +#define BUTTON_L1 4 +#define BUTTON_R1 5 +#define BUTTON_L2 6 +#define BUTTON_R2 7 +#define DPAD_DOWN 8 +#define DPAD_LEFT 9 +#define DPAD_RIGHT 10 +#define DPAD_UP 11 +#define BTN_MAX 12 #define AXIS_LEFT_X 0 #define AXIS_LEFT_Y 1 @@ -32,4 +36,6 @@ int pad_init(); void pad_poll(); void pad_frame_start(); +int pad_lua_init(lua_State *l); + #endif diff --git a/src/ps2lua.c b/src/ps2lua.c index 19824d8..ab12355 100644 --- a/src/ps2lua.c +++ b/src/ps2lua.c @@ -10,6 +10,7 @@ #include "log.h" #include "script.h" +#include "pad.h" #define INIT_SCRIPT "host:script/ps2init.lua" @@ -48,7 +49,7 @@ int ps2luaprog_onstart(lua_State *l) { lua_pushstring(l, "start"); lua_gettable(l, -2); int type = lua_type(l, -1); - info("start fn has type :: %s (%d)", lua_typename(l, type), type); + // info("start fn has type :: %s (%d)", lua_typename(l, type), type); int rc = lua_pcall(l, 0, 0, 0); if ( rc ) { const char *err = lua_tostring(l, -1); @@ -63,7 +64,8 @@ int ps2luaprog_onframe(lua_State *l) { lua_pushstring(l, "frame"); lua_gettable(l, -2); int type = lua_type(l, -1); - info("frame fn has type :: %s (%d)", lua_typename(l, type), type); + // info("frame fn has type :: %s (%d)", lua_typename(l, type), type); + // int rc = lua_pcall(l, 0, 0, 0); if ( rc ) { @@ -133,6 +135,7 @@ int main(int argc, char *argv[]) { dma_lua_init(L); gs_lua_init(L); lua_tga_init(L); + pad_lua_init(L); //TODO: better abstraction for drawlua_* drawlua_init(L); @@ -144,6 +147,8 @@ int main(int argc, char *argv[]) { ps2luaprog_onstart(L); while( ps2luaprog_is_running(L) ) { + pad_frame_start(); + pad_poll(); dma_wait_fast(); info("ON FRAME"); ps2luaprog_onframe(L); From 1a38ae7835a2aaa07d6794dd149b2f05565c2653 Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Fri, 15 Oct 2021 23:52:27 +1100 Subject: [PATCH 21/29] tweaking logging and numbers --- script/min.lua | 2 +- src/dmalua.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/script/min.lua b/script/min.lua index ae86e55..d99de0f 100644 --- a/script/min.lua +++ b/script/min.lua @@ -19,7 +19,7 @@ function PS2PROG.start() end function PS2PROG.frame() - local db = RM.getDrawBuffer(10000) + local db = RM.getDrawBuffer(5000) db:frameStart(gs) -- writeToBuffer(db, {0x8001, 0x10000000, 0xe, 0x0, 0x1, 0x0, 0x61, 0x0}) db:frameEnd(gs) diff --git a/src/dmalua.c b/src/dmalua.c index d656c0f..2bed363 100644 --- a/src/dmalua.c +++ b/src/dmalua.c @@ -38,7 +38,7 @@ static int dma_send_buffer(lua_State *l) { int channel = lua_tointeger(l, 2); // print buffer for debugging - //print_buffer(ptr, head/16); + print_buffer(ptr, head/16); // info("DMA send :: sending %d qwords on channel %d", head/16, channel); dma_channel_send_normal(channel, ptr, head/16, 0, 0); From d4829e2660d9d143a356678555224e55094692f4 Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Sat, 16 Oct 2021 00:42:29 +1100 Subject: [PATCH 22/29] remove unused files in new system --- Makefile | 2 +- src/Makefile | 3 +- src/draw.c | 131 ----------------------- src/drawstate.c | 50 --------- src/drawstate.h | 18 ---- src/gs.c | 77 -------------- src/gs.h | 19 ---- src/luadrawstate.c | 61 ----------- src/main.c | 254 ++++++++++++++++++++++----------------------- src/math.c | 89 ---------------- src/mesh.c | 58 ----------- src/mesh.h | 23 ---- src/pad.c | 95 ----------------- src/ps2lua.c | 161 ---------------------------- src/ps2math.h | 16 --- src/script.c | 53 ---------- 16 files changed, 126 insertions(+), 984 deletions(-) delete mode 100644 src/draw.c delete mode 100644 src/drawstate.c delete mode 100644 src/drawstate.h delete mode 100644 src/gs.c delete mode 100644 src/gs.h delete mode 100644 src/luadrawstate.c delete mode 100644 src/math.c delete mode 100644 src/mesh.c delete mode 100644 src/mesh.h delete mode 100644 src/pad.c delete mode 100644 src/ps2lua.c delete mode 100644 src/ps2math.h delete mode 100644 src/script.c diff --git a/Makefile b/Makefile index 554bc94..49bed41 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ BIN=dist/test.elf PS2HOST?=192.168.20.99 DOCKER_IMG=ps2build -DOCKERFLAGS=--user "$(shell id -u):$(shell id -g)" +DOCKERFLAGS=--user "$(1000):$(1000)" DOCKER?=docker LUA_BRANCH=ee-v5.4.4 diff --git a/src/Makefile b/src/Makefile index 8739376..2d82dda 100644 --- a/src/Makefile +++ b/src/Makefile @@ -2,7 +2,7 @@ PS2SDK=/usr/local/ps2dev/ps2sdk EE_BIN=test.elf #EE_OBJS=main.o gs.o mesh.o draw.o math.o pad.o drawstate.o script.o luadrawstate.o -EE_OBJS=ps2lua.o gslua.o dmalua.o bufferlua.o tga.o padlua.o +EE_OBJS=main.o gslua.o dmalua.o bufferlua.o tga.o padlua.o EE_LIBS=-ldma -lgraph -ldraw -lkernel -ldebug -lmath3d -lm -lpad -llua EE_CFLAGS+=-Wall --std=c99 -Wno-sign-compare -fno-strict-aliasing -fno-exceptions -DLUA_USE_PS2 @@ -17,7 +17,6 @@ endif .PHONY: clean clean: rm -rf $(EE_BIN) $(EE_OBJS) - make -C lua -f makefile clean diff --git a/src/draw.c b/src/draw.c deleted file mode 100644 index 01c18c6..0000000 --- a/src/draw.c +++ /dev/null @@ -1,131 +0,0 @@ - -#include -#include - -#include - -#include "log.h" -#include "mesh.h" -#include "ps2draw.h" -#include "ps2math.h" - -#define ZMAX (1024 * 1024) - -static int cc = 0; - -void log_matrix(MATRIX m) { - printf("Matrix = \n"); - printf("%.2f %.2f %.2f %.2f\n", m[0], m[4], m[8], m[12]); - printf("%.2f %.2f %.2f %.2f\n", m[1], m[5], m[9], m[13]); - printf("%.2f %.2f %.2f %.2f\n", m[2], m[6], m[10], m[14]); - printf("%.2f %.2f %.2f %.2f\n", m[3], m[7], m[11], m[15]); -} - -int mesh_is_visible(struct model_instance *inst, struct render_state *d) { - VECTOR v; - vector_sub(v, d->camera_pos, inst->translate); - float dot = v[0] * d->fwd[0] + v[1] * d->fwd[1] + v[2] * d->fwd[2]; - return (dot > 0); -} - -void mesh_transform(char *b, struct model_instance *inst, - struct render_state *d) { - MATRIX tmp; - MATRIX model; - matrix_unit(model); - create_model_matrix(model, inst->translate, inst->scale, inst->rotate); - matrix_unit(tmp); - - matrix_multiply(tmp, tmp, model); - matrix_multiply(tmp, tmp, d->v); - matrix_multiply(tmp, tmp, d->p); - - if (cc == 0) { - info("PROJECTION == "); - log_matrix(d->p); - } - - if (cc % 200 == 0) { - info("###### Matrix info ######"); - info("rotate=%f", d->camera_rotate_y); - info(" view="); - log_matrix(d->v); - info(" mvp="); - log_matrix(tmp); - } - int stride = inst->m->vertex_size * 16; - float d_avg = 0; - for (int i = 0; i < inst->m->vertex_count; i++) { - // get address of current vertex data - float *pos = - (float *)(b + (stride * i) + (inst->m->vertex_position_offset * 16)); - float *v = pos; - pos[3] = 1.f; - - vector_apply(v, v, tmp); - pos[0] = pos[0] / pos[3]; - pos[1] = pos[1] / pos[3]; - pos[2] = pos[2]; - d_avg += pos[2]; - - pos[0] = (pos[0] * 200); - pos[1] = (pos[1] * 200); - - *((uint32_t *)pos) = ftoi4(pos[0] + d->offset_x); - *((uint32_t *)(pos + 1)) = ftoi4(pos[1] + d->offset_y); - uint32_t zv = (uint32_t)(ZMAX * (pos[2] / 100.f)); - *((uint32_t *)(pos + 2)) = zv; - - uint32_t *col = - (uint32_t *)(b + (stride * i) + (inst->m->vertex_colour_offset * 16)); - col[1] = 0x0f; - col[2] = 0x0f; - col[0] = (int)(((zv) / (ZMAX * 1.0f)) * 255.0f); - col[3] = 0x80; - /* - *((uint32_t*)pos) = (short)((pos[0]+1.0f)*d->offset_x); - *((uint32_t*)(pos+1)) = (short)((pos[1]+1.0f)*d->offset_y); - *((uint32_t*)(pos+2)) = (unsigned int)((pos[2]+1.0f)*20); - */ - - pos[3] = 0; - } - if (cc % 100 == 0) { - info("avg depth = %f", d_avg / (1.0f * inst->m->vertex_count)); - } - cc++; -} - -void create_model_matrix(MATRIX tgt, VECTOR translate, VECTOR scale, - VECTOR rotate) { - matrix_unit(tgt); - matrix_rotate(tgt, tgt, rotate); - matrix_scale(tgt, tgt, scale); - matrix_translate(tgt, tgt, translate); -} - -void update_draw_matrix(struct render_state *d) { - d->up[0] = 0; - d->up[1] = 1.0f; - d->up[2] = 0; - d->up[3] = 0; - - d->fwd[0] = 0; - d->fwd[1] = 0; - d->fwd[2] = -1.f; - d->fwd[3] = 1.f; - vector_rotate_y(d->fwd, d->camera_rotate_y); - - d->camera_tgt[0] = d->camera_pos[0] + d->fwd[0]; - d->camera_tgt[1] = d->camera_pos[1] + d->fwd[1]; - d->camera_tgt[2] = d->camera_pos[2] + d->fwd[2]; - /* - d->camera_tgt[0] = 0; - d->camera_tgt[1] = 0; - d->camera_tgt[2] = 0; - */ - d->camera_tgt[3] = 1; - - matrix_proj(d->p, 1.2f, 0.7f, .1f, 100.f); - matrix_lookat(d->v, d->camera_pos, d->camera_tgt, d->up); -} diff --git a/src/drawstate.c b/src/drawstate.c deleted file mode 100644 index fe52012..0000000 --- a/src/drawstate.c +++ /dev/null @@ -1,50 +0,0 @@ - -#include -#include - -#include "drawstate.h" -#include "ps2draw.h" -#include "gs.h" - -static struct draw_state st = {0}; -static struct render_state r = {0}; - -int drawstate_init(int w, int h, int i) { - st.width = w; - st.height = h; - st.vmode = graph_get_region(); - st.gmode = i; - return gs_init(&st, GS_PSM_32, GS_PSMZ_24); -} - -int drawstate_camera_step(float x, float y, float z) { - r.camera_pos[0] += x; - r.camera_pos[1] += y; - r.camera_pos[2] += z; -} - -int drawstate_camera_jump(float x, float y, float z) { - r.camera_pos[0] = x; - r.camera_pos[1] = y; - r.camera_pos[2] = z; -} - -int drawstate_camera_rotate(float x, float y, float z) { - r.camera_rotate_y += y; -} - -qword_t * drawstate_ztest(qword_t *q, int enable) { - if ( enable ) { - return draw_enable_tests(q, 0, &st.zb); - } else { - return draw_disable_tests(q, 0, &st.zb); - } -} - -struct render_state *drawstate_get() { - return &r; -} - -struct draw_state *drawstate_gs_state() { - return &st; -} diff --git a/src/drawstate.h b/src/drawstate.h deleted file mode 100644 index 91cb257..0000000 --- a/src/drawstate.h +++ /dev/null @@ -1,18 +0,0 @@ - -#ifndef SRC_DRAWSTATE_H -#define SRC_DRAWSTATE_H - -#include "ps2draw.h" -#include "gs.h" - -#include - -int drawstate_init(int w, int h, int i); -int drawstate_camera_step(float x, float y, float z); -int drawstate_camera_jump(float x, float y, float z); -int drawstate_camera_rotate(float x, float y, float z); -qword_t * drawstate_ztest(qword_t *q, int enable); -struct render_state *drawstate_get(); -struct draw_state *drawstate_gs_state(); - -#endif diff --git a/src/gs.c b/src/gs.c deleted file mode 100644 index bef668a..0000000 --- a/src/gs.c +++ /dev/null @@ -1,77 +0,0 @@ - -#include "gs.h" - -#include - -#include -#include - -#include -#include - -#include -#include - -#define GS_OFFSET_X 2048 -#define GS_OFFSET_Y 2048 - -#define GS_CMD_BUFFER_LEN (40 * 16) -static qword_t *gs_cmd_buffer; - -int gs_init(struct draw_state *ds, int psm, int psmz) { - if (!gs_cmd_buffer) { - gs_cmd_buffer = malloc(GS_CMD_BUFFER_LEN); - } - - ds->fb.address = - graph_vram_allocate(ds->width, ds->height, psm, GRAPH_ALIGN_PAGE); - ds->fb.width = ds->width; - ds->fb.height = ds->height; - ds->fb.psm = psm; - ds->fb.mask = 0; - - ds->zb.address = - graph_vram_allocate(ds->width, ds->height, psmz, GRAPH_ALIGN_PAGE); - ds->zb.enable = 1; - ds->zb.method = ZTEST_METHOD_GREATER; - ds->zb.zsm = psmz; - ds->zb.mask = 0; - - graph_set_mode(ds->gmode, ds->vmode, GRAPH_MODE_FIELD, GRAPH_DISABLE); - graph_set_screen(0, 0, ds->width, ds->height); - graph_set_bgcolor(0, 0, 0); - graph_set_framebuffer_filtered(ds->fb.address, ds->width, psm, 0, 0); - graph_enable_output(); - - qword_t *q = gs_cmd_buffer; - memset(gs_cmd_buffer, 0, GS_CMD_BUFFER_LEN); - q = draw_setup_environment(q, 0, &ds->fb, &ds->zb); - q = draw_primitive_xyoffset(q, 0, GS_OFFSET_X - (ds->width / 2), - GS_OFFSET_Y - (ds->height / 2)); - q = draw_finish(q); - dma_channel_send_normal(DMA_CHANNEL_GIF, gs_cmd_buffer, q - gs_cmd_buffer, 0, - 0); - draw_wait_finish(); - - return 0; -} - -qword_t * gs_frame_start(struct draw_state *ds, qword_t *q) { - if (!ds) { - logerror("cannot run frame_start on NULL draw state"); - return q; - } - - float halfw = ds->width / 2.f; - float halfh = ds->height / 2.f; - - q = draw_disable_tests(q, 0, &ds->zb); - q = draw_clear(q, 0, 2048.0f - halfw, 2048.0f - half, ds->width, ds->height, - ds->clear_col[0], ds->clear_col[1], ds->clear_col[2]); - return draw_enable_tests(q, 0, &ds->zb); - -} - -qword_t * gs_frame_end(struct draw_state *ds, qword_t *q) { - return draw_finish(q); -} diff --git a/src/gs.h b/src/gs.h deleted file mode 100644 index 4886f16..0000000 --- a/src/gs.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef GS_H -#define GS_H - -#include -#include - -struct draw_state { - int width; - int height; - int vmode; - int gmode; - framebuffer_t fb; - zbuffer_t zb; - char clear_col[3]; -}; - -int gs_init(struct draw_state *ds, int psm, int psmz); - -#endif diff --git a/src/luadrawstate.c b/src/luadrawstate.c deleted file mode 100644 index f392703..0000000 --- a/src/luadrawstate.c +++ /dev/null @@ -1,61 +0,0 @@ -#include -#include "script.h" -#include "ps2draw.h" -#include "drawstate.h" - -#include -#include - -static int lua_gs_init(lua_State *L) { - int width = lua_tointeger(L, 1); - int height = lua_tointeger(L, 2); - int interlace = lua_tointeger(L, 3); - drawstate_init(width, height, interlace); -} - -static int lua_camera_step(lua_State *L) { - double x = lua_tonumber(L, 1); - double y = lua_tonumber(L, 2); - double z = lua_tonumber(L, 3); - drawstate_camera_step(x,y,z); -} - -static int lua_camera_jump(lua_State *L) { - double x = lua_tonumber(L, 1); - double y = lua_tonumber(L, 2); - double z = lua_tonumber(L, 3); - drawstate_camera_jump(x,y,z); -} - -int lua_drawstate_init(lua_State *L) { - LUA_DEF(gs_init); - LUA_DEF(camera_step); - LUA_DEF(camera_jump); - return 0; -} - -int lua_drawstate_constant_table(lua_State *L) { - lua_createtable(L, 0, 4); - FIELD_INT("INTERLACED", GRAPH_MODE_INTERLACED); - FIELD_INT("NON_INTERLACED", GRAPH_MODE_NONINTERLACED); - FIELD_INT("PSM32", GS_PSM_32); - FIELD_INT("PSMZ24", GS_PSMZ_24); - lua_setglobal(L, "GS"); - return 1; -} - -// GS.interlaced / nonInterlaced -// -int lua_drawstate_new(lua_State *L, int w, int h, int i) { - lua_createtable(L, 0, 4); - FIELD_INT("interlace", i); - FIELD_INT("width", w); - FIELD_INT("height", h); - FIELD_INT("ztest", 1); - FIELD_CFUNC("init", lua_gs_init); - return 1; -} - - - - diff --git a/src/main.c b/src/main.c index f4faedb..ab12355 100644 --- a/src/main.c +++ b/src/main.c @@ -1,167 +1,161 @@ -#include -#include -#include -#include -#include +#include +#include +#include -#include -#include #include +#include #include +#include -#include - -#include "gs.h" #include "log.h" -#include "mesh.h" -#include "pad.h" -#include "ps2draw.h" -#include "drawstate.h" + #include "script.h" +#include "pad.h" -#define OFFSET_X 2048 -#define OFFSET_Y 2048 +#define INIT_SCRIPT "host:script/ps2init.lua" -#define VID_W 640 -#define VID_H 448 +int lua_tga_init(lua_State *l); -#define TGT_FILE "host:MIT_teapot.bin" +static int ps2luaprog_start_nil(lua_State *l) { + info("default start..."); + return 0; +} -#define fatalerror(st, msg, ...) \ - printf("FATAL: " msg "\n", ##__VA_ARGS__); \ - error_forever(st); \ - ((void)0) -void error_forever(struct draw_state *st); +static int ps2luaprog_frame_nil(lua_State *l) { + return 0; +} -int print_buffer(qword_t *b, int len) { - printf("-- buffer\n"); - for (int i = 0; i < len; i++) { - printf("%016llx %016llx\n", b->dw[0], b->dw[1]); - b++; - } - printf("-- /buffer\n"); +static int ps2lua_log2(lua_State *l) { + int n = lua_tointeger(l, 1); + float res = log2f(n); + lua_pushnumber(l, res); + return 1; +} + +int ps2luaprog_init(lua_State *l) { + lua_createtable(l, 0, 2); + lua_pushcfunction(l, ps2luaprog_start_nil); + lua_setfield(l, -2, "start"); + lua_pushcfunction(l, ps2luaprog_frame_nil); + lua_setfield(l, -2, "frame"); + lua_setglobal(l, "PS2PROG"); + lua_pushcfunction(l, ps2lua_log2); + lua_setglobal(l, "log2"); return 0; } -int main() { +int ps2luaprog_onstart(lua_State *l) { + lua_getglobal(l, "PS2PROG"); + lua_pushstring(l, "start"); + lua_gettable(l, -2); + int type = lua_type(l, -1); + // info("start fn has type :: %s (%d)", lua_typename(l, type), type); + int rc = lua_pcall(l, 0, 0, 0); + if ( rc ) { + const char *err = lua_tostring(l, -1); + logerr("lua execution error (start event) -- %s", err); + } - printf("Hello\n"); - qword_t *buf = malloc(20000 * 16); - char *file_load_buffer = malloc(310 * 1024); - int file_load_buffer_len = 310 * 1024; + return 0; +} - // init DMAC - dma_channel_initialize(DMA_CHANNEL_GIF, 0, 0); - dma_channel_fast_waits(DMA_CHANNEL_GIF); +int ps2luaprog_onframe(lua_State *l) { + lua_getglobal(l, "PS2PROG"); + lua_pushstring(l, "frame"); + lua_gettable(l, -2); + int type = lua_type(l, -1); + // info("frame fn has type :: %s (%d)", lua_typename(l, type), type); + // + int rc = lua_pcall(l, 0, 0, 0); + + if ( rc ) { + const char *err = lua_tostring(l, -1); + logerr("lua execution error (frame event) -- %s", err); + } - void *lua = script_load("host:entry.lua"); - script_simple_call(lua, "ps2_init"); + return 0; +} + +int ps2luaprog_is_running(lua_State *l) { + return 1; +} - struct model m = {0}; - m.r = 0xff; - int bytes_read = load_file(TGT_FILE, file_load_buffer, file_load_buffer_len); - if (bytes_read <= 0) { - fatalerror(drawstate_gs_state(), "failed to load file %s", TGT_FILE); +static int runfile(lua_State *l, const char *fname) { + info("running lua file %s", fname); + int rc = luaL_loadfile(l, fname); + if ( rc == LUA_ERRSYNTAX ) { + logerr("failed to load %s: syntax error", fname); + const char *err = lua_tostring(l, -1); + logerr("err: %s", err); + return -1; } - if (bytes_read % 16 != 0) { - fatalerror(drawstate_gs_state(), "lengt of model file %s was not 0 %% 16", TGT_FILE); + else if ( rc == LUA_ERRMEM ) { + logerr("faild to allocate memory for %s", fname); + return -1; + } + else if ( rc == LUA_ERRFILE ) { + logerr("could not open/read file %s", fname); + return -1; + } + else if ( rc ) { + logerr("unknown error loading %s", fname); + return -1; } - if (!model_load(&m, file_load_buffer, bytes_read)) { - fatalerror(drawstate_gs_state(), "failed to process model"); + rc = lua_pcall(l, 0, 0, 0); + if ( rc ) { + const char *err = lua_tostring(l, -1); + logerr("lua execution error -- %s", err); + return -1; } - struct draw_state *draw_state = drawstate_gs_state(); - struct render_state *r = drawstate_get(); + return 0; +} - draw_state->clear_col[0] = 0xb1; - draw_state->clear_col[1] = 0xce; - draw_state->clear_col[2] = 0xcb; +int main(int argc, char *argv[]) { + info("startup - argc = %d", argc); + for (int i = 0; i < argc; i++) { + info("arg %d) %s", i, argv[i]); + } + char *startup = "host:script/main.lua"; + if (argc > 1) { + info("setting entrypoint to %s", argv[1]); + startup = argv[1]; + } - r->offset_x = OFFSET_X; - r->offset_y = OFFSET_Y; + struct lua_State *L; + L = luaL_newstate(); + if ( !L ) { + logerr("failed to start lua state"); + return -1; + } + luaL_openlibs(L); - struct model_instance inst = {0}; - inst.m = &m; - inst.scale[0] = 1.f; - inst.scale[1] = 1.f; - inst.scale[2] = 1.f; - inst.scale[3] = 1.0f; - inst.translate[2] = 10.f; + ps2luaprog_init(L); + dma_lua_init(L); + gs_lua_init(L); + lua_tga_init(L); + pad_lua_init(L); - pad_init(); + //TODO: better abstraction for drawlua_* + drawlua_init(L); - graph_wait_vsync(); + info("finished lua state setup"); - while (1) { + runfile(L, INIT_SCRIPT); + runfile(L, startup); + ps2luaprog_onstart(L); + while( ps2luaprog_is_running(L) ) { pad_frame_start(); pad_poll(); - update_draw_matrix(&r); - dma_wait_fast(); - - qword_t *q = buf; - memset(buf, 0, 20000 * 16); - - q = gs_frame_start(draw_state, q); - - if (mesh_is_visible(&inst, r)) { - qword_t *model_verts_start = q; - memcpy(q, m.buffer, m.buffer_len); - mesh_transform((char *)(model_verts_start + MESH_HEADER_SIZE), &inst, r); - q += (m.buffer_len / 16); - } - - q = gs_frame_end(draw_state, q); - - dma_channel_send_normal(DMA_CHANNEL_GIF, buf, q - buf, 0, 0); - - draw_wait_finish(); - graph_wait_vsync(); - -#if 0 - unsigned char joyx = joy_axis_value(AXIS_LEFT_X); - float dx = (joy_axis_value(AXIS_LEFT_X) - 128) / 128.0f; - if ( fabs(dx) < 0.2f ) { dx = 0; } - float dz = (joy_axis_value(AXIS_LEFT_Y) - 128) / 128.0f; - if ( fabs(dz) < 0.2f ) { dz = 0; } - int dy = button_held(DPAD_DOWN) - button_held(DPAD_UP); - - info("joy %f,%f", dx, dz); -#else - int dx = button_held(DPAD_RIGHT) - button_held(DPAD_LEFT); - int dz = button_held(DPAD_DOWN) - button_held(DPAD_UP); - int dy = button_held(BUTTON_L1) - button_held(BUTTON_L2); -#endif - - r->camera_pos[0] += 0.2f * dx; - r->camera_pos[2] += 0.2f * dz; - // r->camera_pos[1] += 0.1f * dy; - r->camera_rotate_y += 0.01f * dy; - - } -} - -void error_forever(struct draw_state *st) { - qword_t *buf = malloc(1200); - while (1) { dma_wait_fast(); - qword_t *q = buf; - memset(buf, 0, 1200); - q = draw_disable_tests(q, 0, &st->zb); - q = draw_clear(q, 0, 2048.0f - 320, 2048.0f - 244, VID_W, VID_H, 0xff, 0, - 0); - q = draw_finish(q); - dma_channel_send_normal(DMA_CHANNEL_GIF, buf, q - buf, 0, 0); + info("ON FRAME"); + ps2luaprog_onframe(L); + info("WAIT DRAW"); draw_wait_finish(); + info("WAIT VSYNC"); graph_wait_vsync(); - sleep(2); } + info("main loop ended"); } - -/* - * - * xxxx xxxx xxxx . yyyy - * 0100 0000 0000 . 0000 - * -2 */ diff --git a/src/math.c b/src/math.c deleted file mode 100644 index 80d1920..0000000 --- a/src/math.c +++ /dev/null @@ -1,89 +0,0 @@ -#include -#include - -#define SWAP(i, j) \ - do { \ - t = a[i]; \ - a[i] = a[j]; \ - a[j] = t; \ - } while (0) -void matrix_tsp(MATRIX a) { - float t; - SWAP(1, 4); - SWAP(2, 8); - SWAP(3, 12); - SWAP(6, 9); - SWAP(7, 13); - SWAP(11, 14); -} - -void vector_cross(VECTOR out, VECTOR a, VECTOR b) { - out[0] = a[1] * b[2] - a[2] * b[1]; - out[1] = a[2] * b[0] - a[0] * b[2]; - out[2] = a[0] * b[1] - a[1] * b[0]; -} - -void vector_sub(VECTOR out, VECTOR a, VECTOR b) { - out[0] = a[0] - b[0]; - out[1] = a[1] - b[1]; - out[2] = a[2] - b[2]; - // out[3] = a[3] - b[3]; -} - -void matrix_lookat(MATRIX out, VECTOR eye, VECTOR c, VECTOR up) { - VECTOR z, x, y, nd; - vector_sub(z, c, eye); - vector_normalize(z, z); - - vector_cross(x, up, z); - vector_normalize(x, x); - - vector_cross(y, z, x); - vector_normalize(y, y); - - matrix_unit(out); - - nd[0] = -1 * (x[0] * eye[0] + x[1] * eye[1] + x[2] * eye[2]); - nd[1] = -1 * (y[0] * eye[0] + y[1] * eye[1] + y[2] * eye[2]); - nd[2] = -1 * (z[0] * eye[0] + z[1] * eye[1] + z[2] * eye[2]); - - for (int i = 0; i < 3; i++) { - out[4 * i] = x[i]; - out[4 * i + 1] = y[i]; - out[4 * i + 2] = z[i]; - out[4 * i + 3] = nd[i]; - } - - matrix_tsp(out); -} - -void matrix_zero(MATRIX out) { - for (int i = 0; i < 16; i++) { - out[i] = 0; - } -} - -void matrix_proj(MATRIX out, float fov, float ar, float near, float far) { - matrix_zero(out); - float angle = 1.f / (tanf(fov * 0.5f)); - float fn = 1.f / (near - far); - out[0] = angle / ar; - out[5] = angle; - out[10] = -1 * far * fn; - out[14] = near * far * fn; - out[11] = 1.0f; -} - -void matrix_viewport(MATRIX out, float w, float h) { - matrix_unit(out); - out[0] = 320; - out[5] = 224; - out[3] = 320; - out[7] = 224; - matrix_tsp(out); -} - -void vector_rotate_y(VECTOR out, float r) { - out[0] = (out[0] * cos(r)) + (out[2] * sin(r)); - out[2] = (-1.f * out[0] * sin(r)) + (out[2] * cos(r)); -} diff --git a/src/mesh.c b/src/mesh.c deleted file mode 100644 index 33c2c29..0000000 --- a/src/mesh.c +++ /dev/null @@ -1,58 +0,0 @@ - -#include -#include -#include -#include -#include - -#include "log.h" -#include "mesh.h" - -#define myftoi4(x) ((x) << 4) - -int load_file(const char *fname, char *b, int b_len) { - FILE *f = fopen(fname, "rb"); - fseek(f, 0, SEEK_END); - int len = ftell(f); - fseek(f, 0, SEEK_SET); - if (len >= b_len) { - return 0; - } - int byte_read = fread(b, 1, len, f); - fclose(f); - return byte_read; -} - -#define MODEL_BUFFER_PRE (4 * 16) - -// b_len MUST be a multiple of 16 -int model_load(struct model *m, char *b, int b_len) { - if (!m || !b || b_len <= 0) { - return 0; - } - m->buffer = malloc(b_len + MODEL_BUFFER_PRE); - m->buffer_len = b_len + MODEL_BUFFER_PRE; - qword_t *q = m->buffer; - m->vertex_size = 2; - m->vertex_count = b_len / (16 * m->vertex_size); - m->vertex_position_offset = 1; - m->vertex_colour_offset = 0; - m->face_count = m->vertex_count / 3; - info("initializing model: verts=%d, faces=%d, bytes in buf=%d", - m->vertex_count, m->face_count, m->buffer_len); - - // Create giftag, set regs via A+D - q->dw[0] = 0x1000000000000001; - q->dw[1] = 0x000000000000000e; - q++; - // set PRIM = triangle - q->dw[0] = GS_SET_PRIM(GS_PRIM_TRIANGLE, 1, 0, 0, 0, 0, 0, 0, 0); - q->dw[1] = GS_REG_PRIM; - q++; - // start vertex data GIFTAG - q->dw[0] = 0x6000000000000000 | (m->face_count & 0x3fff); - q->dw[1] = 0x0000000000515151; - q++; - memcpy(q, b, b_len); - return 1; -} diff --git a/src/mesh.h b/src/mesh.h deleted file mode 100644 index d6fa7a6..0000000 --- a/src/mesh.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef MESH_H -#define MESH_H - -#include - -struct model { - qword_t *buffer; - char r; - char g; - char b; - int face_count; - int vertex_count; - int vertex_size; - int vertex_position_offset; - int vertex_colour_offset; - int buffer_len; -}; - -int load_file(const char *fname, char *b, int b_len); - -int model_load(struct model *m, char *b, int b_len); - -#endif diff --git a/src/pad.c b/src/pad.c deleted file mode 100644 index 8c280c3..0000000 --- a/src/pad.c +++ /dev/null @@ -1,95 +0,0 @@ - -#include -#include -#include - -#include "log.h" -#include "pad.h" - -#define pad_test(b, v) \ - do { \ - if ((c & b)) { \ - btn_held[v] = 1; \ - } \ - } while (0) - -static char pad_buffer[256]; -static struct padButtonStatus pad_read_space = {0}; - -static int btn_held[BTN_MAX]; - -static unsigned char joysticks[JOY_AXIS_COUNT]; - -static int prev_inputs; - -int button_held(int b) { return btn_held[b]; } - -unsigned char joy_axis_value(int a) { return joysticks[a]; } - -int pad_init() { - info("loading SIF modules for PAD"); - int rc = SifLoadModule(R_SIO2MAN, 0, 0); - if (!rc) { - logerr("failed to load SIF %s", R_SIO2MAN); - return 0; - } - rc = SifLoadModule(R_PADMAN, 0, 0); - if (!rc) { - logerr("failed to load SIF %s", R_PADMAN); - return 0; - } - padInit(0); - padPortOpen(0, 0, &pad_buffer); - // TODO(phy1um): check for dualshock controller - padSetMainMode(0, 0, 1, 3); - return 1; -} - -static int pad_wait(int port, int slot, int tries) { - int now; - int prev = -1; - now = padGetState(port, slot); - if (now == PAD_STATE_DISCONN) { - return -1; - } - while ((now != PAD_STATE_STABLE) && (now != PAD_STATE_FINDCTP1)) { - prev = now; - now = padGetState(port, slot); - tries--; - if (tries == 0) { - break; - } - } - return 0; -} - -void pad_poll() { - if (pad_wait(0, 0, 10) < 0) { - return; - } - - if (padRead(0, 0, &pad_read_space) != 0) { - int pad = 0xffff ^ pad_read_space.btns; - int c = pad ^ prev_inputs; - pad_test(PAD_LEFT, DPAD_LEFT); - pad_test(PAD_RIGHT, DPAD_RIGHT); - pad_test(PAD_UP, DPAD_UP); - pad_test(PAD_DOWN, DPAD_DOWN); - pad_test(PAD_CROSS, BUTTON_1); - pad_test(PAD_SQUARE, BUTTON_2); - pad_test(PAD_L1, BUTTON_L1); - pad_test(PAD_L2, BUTTON_L2); - pad_test(PAD_R1, BUTTON_R1); - pad_test(PAD_R2, BUTTON_R2); - joysticks[AXIS_LEFT_X] = pad_read_space.ljoy_h; - joysticks[AXIS_LEFT_Y] = pad_read_space.ljoy_v; - joysticks[AXIS_RIGHT_X] = pad_read_space.rjoy_h; - joysticks[AXIS_RIGHT_Y] = pad_read_space.rjoy_v; - } -} - -void pad_frame_start() { - for (int i = 0; i < BTN_MAX; i++) { - btn_held[i] = 0; - } -} diff --git a/src/ps2lua.c b/src/ps2lua.c deleted file mode 100644 index ab12355..0000000 --- a/src/ps2lua.c +++ /dev/null @@ -1,161 +0,0 @@ -#include -#include -#include - -#include -#include -#include -#include - -#include "log.h" - -#include "script.h" -#include "pad.h" - -#define INIT_SCRIPT "host:script/ps2init.lua" - -int lua_tga_init(lua_State *l); - -static int ps2luaprog_start_nil(lua_State *l) { - info("default start..."); - return 0; -} - -static int ps2luaprog_frame_nil(lua_State *l) { - return 0; -} - -static int ps2lua_log2(lua_State *l) { - int n = lua_tointeger(l, 1); - float res = log2f(n); - lua_pushnumber(l, res); - return 1; -} - -int ps2luaprog_init(lua_State *l) { - lua_createtable(l, 0, 2); - lua_pushcfunction(l, ps2luaprog_start_nil); - lua_setfield(l, -2, "start"); - lua_pushcfunction(l, ps2luaprog_frame_nil); - lua_setfield(l, -2, "frame"); - lua_setglobal(l, "PS2PROG"); - lua_pushcfunction(l, ps2lua_log2); - lua_setglobal(l, "log2"); - return 0; -} - -int ps2luaprog_onstart(lua_State *l) { - lua_getglobal(l, "PS2PROG"); - lua_pushstring(l, "start"); - lua_gettable(l, -2); - int type = lua_type(l, -1); - // info("start fn has type :: %s (%d)", lua_typename(l, type), type); - int rc = lua_pcall(l, 0, 0, 0); - if ( rc ) { - const char *err = lua_tostring(l, -1); - logerr("lua execution error (start event) -- %s", err); - } - - return 0; -} - -int ps2luaprog_onframe(lua_State *l) { - lua_getglobal(l, "PS2PROG"); - lua_pushstring(l, "frame"); - lua_gettable(l, -2); - int type = lua_type(l, -1); - // info("frame fn has type :: %s (%d)", lua_typename(l, type), type); - // - int rc = lua_pcall(l, 0, 0, 0); - - if ( rc ) { - const char *err = lua_tostring(l, -1); - logerr("lua execution error (frame event) -- %s", err); - } - - return 0; -} - -int ps2luaprog_is_running(lua_State *l) { - return 1; -} - -static int runfile(lua_State *l, const char *fname) { - info("running lua file %s", fname); - int rc = luaL_loadfile(l, fname); - if ( rc == LUA_ERRSYNTAX ) { - logerr("failed to load %s: syntax error", fname); - const char *err = lua_tostring(l, -1); - logerr("err: %s", err); - return -1; - } - else if ( rc == LUA_ERRMEM ) { - logerr("faild to allocate memory for %s", fname); - return -1; - } - else if ( rc == LUA_ERRFILE ) { - logerr("could not open/read file %s", fname); - return -1; - } - else if ( rc ) { - logerr("unknown error loading %s", fname); - return -1; - } - - rc = lua_pcall(l, 0, 0, 0); - if ( rc ) { - const char *err = lua_tostring(l, -1); - logerr("lua execution error -- %s", err); - return -1; - } - - return 0; -} - -int main(int argc, char *argv[]) { - info("startup - argc = %d", argc); - for (int i = 0; i < argc; i++) { - info("arg %d) %s", i, argv[i]); - } - char *startup = "host:script/main.lua"; - if (argc > 1) { - info("setting entrypoint to %s", argv[1]); - startup = argv[1]; - } - - struct lua_State *L; - L = luaL_newstate(); - if ( !L ) { - logerr("failed to start lua state"); - return -1; - } - luaL_openlibs(L); - - ps2luaprog_init(L); - dma_lua_init(L); - gs_lua_init(L); - lua_tga_init(L); - pad_lua_init(L); - - //TODO: better abstraction for drawlua_* - drawlua_init(L); - - info("finished lua state setup"); - - runfile(L, INIT_SCRIPT); - runfile(L, startup); - - ps2luaprog_onstart(L); - while( ps2luaprog_is_running(L) ) { - pad_frame_start(); - pad_poll(); - dma_wait_fast(); - info("ON FRAME"); - ps2luaprog_onframe(L); - info("WAIT DRAW"); - draw_wait_finish(); - info("WAIT VSYNC"); - graph_wait_vsync(); - } - info("main loop ended"); -} diff --git a/src/ps2math.h b/src/ps2math.h deleted file mode 100644 index bac9911..0000000 --- a/src/ps2math.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef PS2_MATH_H -#define PS2_MATH_H - -#include - -void matrix_lookat(MATRIX out, VECTOR eye, VECTOR c, VECTOR up); - -void matrix_viewport(MATRIX out, int w, int h); -void matrix_proj(MATRIX out, float fov, float ar, float near, float far); - -void matrix_tsp(MATRIX t); - -void vector_rotate_y(VECTOR out, float r); -void vector_sub(VECTOR out, VECTOR a, VECTOR b); - -#endif diff --git a/src/script.c b/src/script.c deleted file mode 100644 index 08b988f..0000000 --- a/src/script.c +++ /dev/null @@ -1,53 +0,0 @@ -#include -#include -#include - -#include "script.h" -#include "log.h" - -struct lua_State * script_load(const char *file) { - info("init lua with entrypoint %s", file); - struct lua_State *L; - L = luaL_newstate(); - if ( !L ) { - logerr("failed to start lua state"); - return 0; - } - luaL_openlibs(L); - lua_drawstate_init(L); - - int rc = luaL_loadfile(L, file); - if ( rc ) { - logerr("failed to execute lua file %s", file); - return 0; - } - rc = lua_pcall(L, 0, 0, 0); - if ( rc ) { - const char *err = lua_tostring(L, -1); - logerr("lua execution error -- %s", err); - return 0; - } - return L; -} - -int script_simple_call(struct lua_State *L, const char *fn) { - lua_getglobal(L, fn); - if ( lua_pcall(L, 0, 0, 0) != 0 ) { - const char *err = lua_tostring(L, -1); - logerr("lua error calling %s -- %s", fn, err); - return 1; - } - return 0; -} - -int script_end(struct lua_State *L) { - lua_close(L); - return 0; -} - -void script_define_constants(struct lua_State *L) { - lua_pushinteger(L, 0); - lua_setglobal(L, "GS_NONINTERLACED"); - lua_pushinteger(L, 1); - lua_setglobal(L, "GS_INTERLACED"); -} From a44b794cfff2aa71227dca9a51d988852f6cdfcb Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Sat, 16 Oct 2021 00:43:23 +1100 Subject: [PATCH 23/29] formatter --- src/bufferlua.c | 125 +++++++++++++++++++++++------------------------- src/dmalua.c | 13 +++-- src/gslua.c | 33 ++++++------- src/main.c | 41 +++++++--------- src/script.h | 2 - src/tga.c | 36 +++++++------- 6 files changed, 119 insertions(+), 131 deletions(-) diff --git a/src/bufferlua.c b/src/bufferlua.c index e60fe18..73e7f8f 100644 --- a/src/bufferlua.c +++ b/src/bufferlua.c @@ -1,5 +1,5 @@ -#include #include +#include #include #include @@ -10,7 +10,6 @@ #include "log.h" #include "script.h" - static int drawlua_new_drawbuffer(lua_State *l); static const unsigned int DRAW_BUFFER_MAX_SIZE = 5 * 1024; @@ -20,92 +19,92 @@ static int buffer_pushint(lua_State *l) { int value = lua_tointeger(l, 2); lua_pushstring(l, "ptr"); lua_gettable(l, 1); - int *ptr = (int *) lua_touserdata(l, -1); + int *ptr = (int *)lua_touserdata(l, -1); lua_pushstring(l, "head"); lua_gettable(l, 1); int head = lua_tointeger(l, -1); // TODO: check size // ASSUME 4byte int - if (head%4 != 0) { + if (head % 4 != 0) { // TODO: manually bitmask etc - logerr("cannot write int to buffer, head%%4 != 0 (%d|%d)", head, head%4); + logerr("cannot write int to buffer, head%%4 != 0 (%d|%d)", head, head % 4); return 0; } // info("db write int %d @ %d", value, head); - ptr[head/4] = value; + ptr[head / 4] = value; // info("db head -> %d", head+4); - lua_pushinteger(l, head+4); + lua_pushinteger(l, head + 4); lua_setfield(l, 1, "head"); return 0; } static int buffer_pushfloat(lua_State *l) { - float value = (float) lua_tonumber(l, 2); + float value = (float)lua_tonumber(l, 2); lua_pushstring(l, "ptr"); lua_gettable(l, 1); - float *ptr = (float *) lua_touserdata(l, -1); + float *ptr = (float *)lua_touserdata(l, -1); lua_pushstring(l, "head"); lua_gettable(l, 1); int head = lua_tointeger(l, -1); // TODO: check size // ASSUME 4byte int - if (head%4 != 0) { + if (head % 4 != 0) { // TODO: manually bitmask etc - logerr("cannot write int to buffer, head%%4 != 0 (%d|%d)", head, head%4); + logerr("cannot write int to buffer, head%%4 != 0 (%d|%d)", head, head % 4); return 0; } // info("db write int %d @ %d", value, head); - ptr[head/4] = value; + ptr[head / 4] = value; // info("db head -> %d", head+4); - lua_pushinteger(l, head+4); + lua_pushinteger(l, head + 4); lua_setfield(l, 1, "head"); return 0; } - static int buffer_settex(lua_State *l) { - int reg= lua_tointeger(l, 2); - int tbp= lua_tointeger(l, 3); - int tbw= lua_tointeger(l, 4); - int psm= lua_tointeger(l, 5); + int reg = lua_tointeger(l, 2); + int tbp = lua_tointeger(l, 3); + int tbw = lua_tointeger(l, 4); + int psm = lua_tointeger(l, 5); int tw = lua_tointeger(l, 6); int th = lua_tointeger(l, 7); - int tcc= lua_tointeger(l, 8); - int tfx= lua_tointeger(l, 9); + int tcc = lua_tointeger(l, 8); + int tfx = lua_tointeger(l, 9); lua_pushstring(l, "ptr"); lua_gettable(l, 1); - int *ptr = (int *) lua_touserdata(l, -1); + int *ptr = (int *)lua_touserdata(l, -1); lua_pushstring(l, "head"); lua_gettable(l, 1); int head = lua_tointeger(l, -1); // TODO: check size // ASSUME 4byte int - if (head%4 != 0) { + if (head % 4 != 0) { // TODO: manually bitmask etc - logerr("cannot write int to buffer, head%%4 != 0 (%d|%d)", head, head%4); + logerr("cannot write int to buffer, head%%4 != 0 (%d|%d)", head, head % 4); return 0; } - int *base = ptr + (head/4); + int *base = ptr + (head / 4); - int v1 = tbp | (tbw<<14) | (psm<<20) | (tw<<26) | ((th&0x3) << 30); + int v1 = tbp | (tbw << 14) | (psm << 20) | (tw << 26) | ((th & 0x3) << 30); // TODO: 0x5??? i must mean 0x4 - int v2 = ((th&0x5)>>2) | (tcc<<1) | (tfx<<2); - int v3 = 0x6+reg; + int v2 = ((th & 0x5) >> 2) | (tcc << 1) | (tfx << 2); + int v3 = 0x6 + reg; int v4 = 0; *(base) = v1; - *(base+1) = v2; - *(base+2) = v3; - *(base+3) = v4; + *(base + 1) = v2; + *(base + 2) = v3; + *(base + 3) = v4; // info("write tex0 :: %08x %08x %08x %08x", v1, v2, v3, v4); // info("head -> %d", head+16); - lua_pushinteger(l, head+16); + lua_pushinteger(l, head + 16); lua_setfield(l, 1, "head"); - // info("backtrack tex0 :: %08x %08x %08x %08x", base[0], base[1], base[2], base[3]); + // info("backtrack tex0 :: %08x %08x %08x %08x", base[0], base[1], base[2], + // base[3]); return 0; } @@ -119,26 +118,26 @@ static int buffer_pushmiptbp(lua_State *l) { lua_pushstring(l, "ptr"); lua_gettable(l, 1); - int *ptr = (int *) lua_touserdata(l, -1); + int *ptr = (int *)lua_touserdata(l, -1); lua_pushstring(l, "head"); lua_gettable(l, 1); int head = lua_tointeger(l, -1); - - if (head%4 != 0) { - logerr("buffer head must be =0%%4, got %d", head%4); + + if (head % 4 != 0) { + logerr("buffer head must be =0%%4, got %d", head % 4); lua_pushstring(l, "buffer write failed"); lua_error(l); return 1; } int v1 = p1 | (w1 << 14) | (p2 << 20); - int overflow = (p2&0x1000) >> 12; + int overflow = (p2 & 0x1000) >> 12; int v2 = overflow | (w2 << 2) | (p3 << 8) | (w3 << 22); - (ptr+head)[0] = v1; - (ptr+head)[1] = v2; + (ptr + head)[0] = v1; + (ptr + head)[1] = v2; - lua_pushinteger(l, head+8); + lua_pushinteger(l, head + 8); lua_setfield(l, 1, "head"); return 0; } @@ -174,9 +173,9 @@ static int buffer_read(lua_State *l) { } lua_pushstring(l, "ptr"); lua_gettable(l, 1); - int *ptr = (int *) lua_touserdata(l, -1); - int res = ptr[index/4]; - lua_pushinteger(l,res); + int *ptr = (int *)lua_touserdata(l, -1); + int res = ptr[index / 4]; + lua_pushinteger(l, res); return 1; } @@ -190,13 +189,13 @@ static int buffer_write(lua_State *l) { int value = lua_tointeger(l, 3); lua_pushstring(l, "ptr"); lua_gettable(l, 1); - int *ptr = (int *) lua_touserdata(l, -1); - ptr[index/4] = value; + int *ptr = (int *)lua_touserdata(l, -1); + ptr[index / 4] = value; return 0; } int drawlua_init(lua_State *l) { - luaL_newmetatable(l, "ps2.buffer"); + luaL_newmetatable(l, "ps2.buffer"); lua_createtable(l, 0, 5); lua_pushcfunction(l, buffer_pushint); @@ -205,7 +204,6 @@ int drawlua_init(lua_State *l) { lua_pushcfunction(l, buffer_pushfloat); lua_setfield(l, -2, "pushfloat"); - lua_pushcfunction(l, buffer_settex); lua_setfield(l, -2, "settex"); @@ -216,7 +214,7 @@ int drawlua_init(lua_State *l) { lua_setfield(l, -2, "copy"); lua_pushcfunction(l, buffer_read); - lua_setfield(l, -2, "read"); + lua_setfield(l, -2, "read"); lua_pushcfunction(l, buffer_write); lua_setfield(l, -2, "write"); lua_setfield(l, -2, "__index"); @@ -233,7 +231,6 @@ int drawlua_init(lua_State *l) { return 0; } - static int drawlua_start_frame(lua_State *l) { // drawbuffer is arg #1 lua_pushstring(l, "head"); @@ -246,12 +243,12 @@ static int drawlua_start_frame(lua_State *l) { */ lua_pushstring(l, "ptr"); lua_gettable(l, 1); - char *ptr = (char *) lua_touserdata(l, -1); + char *ptr = (char *)lua_touserdata(l, -1); // gs is arg #2 lua_pushstring(l, "state"); lua_gettable(l, 2); - struct gs_state *st = (struct gs_state *) lua_touserdata(l, -1); + struct gs_state *st = (struct gs_state *)lua_touserdata(l, -1); lua_pushstring(l, "width"); lua_gettable(l, 2); int width = lua_tointeger(l, -1); @@ -262,15 +259,16 @@ static int drawlua_start_frame(lua_State *l) { float halfw = st->fb.width / 2.f; float halfh = st->fb.height / 2.f; - // info("clear screen :: (%d, %d, %d)", st->clear_r, st->clear_g, st->clear_b); + // info("clear screen :: (%d, %d, %d)", st->clear_r, st->clear_g, + // st->clear_b); - qword_t *q = (qword_t *) (ptr + head); + qword_t *q = (qword_t *)(ptr + head); q = draw_disable_tests(q, 0, &st->zb); q = draw_clear(q, 0, 2048.0f - halfw, 2048.0f - halfh, width, height, - st->clear_r, st->clear_g, st->clear_b); - //q = draw_enable_tests(q, 0, &st->zb); + st->clear_r, st->clear_g, st->clear_b); + // q = draw_enable_tests(q, 0, &st->zb); - head = (char*)q - ptr; + head = (char *)q - ptr; // info("db head -> %d", head); lua_pushinteger(l, head); lua_setfield(l, 1, "head"); @@ -289,27 +287,26 @@ static int drawlua_end_frame(lua_State *l) { */ lua_pushstring(l, "ptr"); lua_gettable(l, 1); - char *ptr = (char *) lua_touserdata(l, -1); + char *ptr = (char *)lua_touserdata(l, -1); - qword_t *q = (qword_t *) (ptr + head); + qword_t *q = (qword_t *)(ptr + head); q = draw_finish(q); - head = (char*)q - ptr; + head = (char *)q - ptr; // info("db head -> %d", head); lua_pushinteger(l, head); lua_setfield(l, 1, "head"); return 0; } -static int drawbuffer_free(lua_State *l) { - return 0; -} +static int drawbuffer_free(lua_State *l) { return 0; } // TODO: document this can only be called ONCE static int drawlua_new_drawbuffer(lua_State *l) { int size = lua_tointeger(l, 1); - if ( size >= DRAW_BUFFER_MAX_SIZE ) { - logerr("invalid drawbuffer size: %d must be smaller than %d", size, DRAW_BUFFER_MAX_SIZE); + if (size >= DRAW_BUFFER_MAX_SIZE) { + logerr("invalid drawbuffer size: %d must be smaller than %d", size, + DRAW_BUFFER_MAX_SIZE); lua_pushstring(l, "drawbuffer size is too big"); lua_error(l); return 0; diff --git a/src/dmalua.c b/src/dmalua.c index 2bed363..0d216d6 100644 --- a/src/dmalua.c +++ b/src/dmalua.c @@ -7,7 +7,7 @@ #include "log.h" static int dma_init(lua_State *l) { - int channel = lua_tointeger(l, 1); + int channel = lua_tointeger(l, 1); info("doing dma init, channel = %d", channel); dma_channel_initialize(channel, 0, 0); dma_channel_fast_waits(channel); @@ -24,7 +24,6 @@ int print_buffer(qword_t *b, int len) { return 0; } - static int dma_send_buffer(lua_State *l) { // buffer is arg 1 lua_pushstring(l, "ptr"); @@ -33,15 +32,15 @@ static int dma_send_buffer(lua_State *l) { lua_pushstring(l, "head"); lua_gettable(l, 1); int head = lua_tointeger(l, -1); - + // channel is arg 2 int channel = lua_tointeger(l, 2); // print buffer for debugging - print_buffer(ptr, head/16); + print_buffer(ptr, head / 16); // info("DMA send :: sending %d qwords on channel %d", head/16, channel); - dma_channel_send_normal(channel, ptr, head/16, 0, 0); + dma_channel_send_normal(channel, ptr, head / 16, 0, 0); return 0; } @@ -52,7 +51,7 @@ static int dma_wait(lua_State *l) { } int dma_lua_init(lua_State *l) { - lua_createtable(l, 0, 5); + lua_createtable(l, 0, 5); lua_pushcfunction(l, dma_wait); lua_setfield(l, -2, "waitFast"); lua_pushcfunction(l, dma_send_buffer); @@ -63,7 +62,7 @@ int dma_lua_init(lua_State *l) { lua_pushinteger(l, DMA_CHANNEL_GIF); lua_setfield(l, -2, "GIF"); lua_pushinteger(l, DMA_CHANNEL_VIF0); - lua_setfield(l, -2, "VIF0"); + lua_setfield(l, -2, "VIF0"); lua_pushinteger(l, DMA_CHANNEL_VIF1); lua_setfield(l, -2, "VIF1"); diff --git a/src/gslua.c b/src/gslua.c index d963158..2018171 100644 --- a/src/gslua.c +++ b/src/gslua.c @@ -1,29 +1,29 @@ #include +#include #include #include -#include #include -#include +#include #include #include -#include "script.h" #include "log.h" +#include "script.h" static int gslua_clear_col(lua_State *l) { lua_pushstring(l, "state"); lua_gettable(l, 1); - struct gs_state *st = (struct gs_state *) lua_touserdata(l, -1); + struct gs_state *st = (struct gs_state *)lua_touserdata(l, -1); lua_pop(l, 1); - if ( !st ) { - logerr("GS state was NULL"); + if (!st) { + logerr("GS state was NULL"); return 0; } int r = lua_tointeger(l, 2); int g = lua_tointeger(l, 3); - int b = lua_tointeger(l, 4); + int b = lua_tointeger(l, 4); st->clear_r = r; st->clear_g = g; st->clear_b = b; @@ -51,7 +51,7 @@ static int gslua_set_buffers(lua_State *l) { // get fields from SELF argument (#1) lua_pushstring(l, "state"); lua_gettable(l, 1); - struct gs_state *st = (struct gs_state *) lua_touserdata(l, -1); + struct gs_state *st = (struct gs_state *)lua_touserdata(l, -1); lua_pop(l, 1); // get fields from FB argument (#2) lua_pushstring(l, "width"); @@ -90,7 +90,7 @@ static int gslua_set_buffers(lua_State *l) { st->fb.mask = 0; st->zb.address = zb_addr; st->zb.zsm = zb_fmt; - //st->zb.method = ZTEST_METHOD_GREATER_EQUAL; + // st->zb.method = ZTEST_METHOD_GREATER_EQUAL; st->zb.method = ZTEST_METHOD_ALLPASS; st->zb.mask = 0; graph_set_framebuffer_filtered(st->fb.address, fb_width, fb_fmt, 0, 0); @@ -98,14 +98,13 @@ static int gslua_set_buffers(lua_State *l) { // init draw state qword_t *head = malloc(20 * 16); - memset(head, 0, 20*16); + memset(head, 0, 20 * 16); qword_t *q = head; q = draw_setup_environment(q, 0, &st->fb, &st->zb); q = draw_primitive_xyoffset(q, 0, 2048 - (st->fb.width / 2), 2048 - (st->fb.height / 2)); q = draw_finish(q); - dma_channel_send_normal(DMA_CHANNEL_GIF, head, q - head, 0, - 0); + dma_channel_send_normal(DMA_CHANNEL_GIF, head, q - head, 0, 0); draw_wait_finish(); free(head); @@ -113,8 +112,8 @@ static int gslua_set_buffers(lua_State *l) { } static int gslua_new_state(lua_State *l) { - int width = lua_tointeger(l, 1); - int height = lua_tointeger(l, 2); + int width = lua_tointeger(l, 1); + int height = lua_tointeger(l, 2); int interlace = lua_tointeger(l, 3); int mode = lua_tointeger(l, 4); // create table with GS state @@ -144,8 +143,8 @@ static int gslua_new_state(lua_State *l) { return 1; } -#define bind(n, b) \ - lua_pushinteger(l, b);\ +#define bind(n, b) \ + lua_pushinteger(l, b); \ lua_setfield(l, -2, n) int gs_lua_init(lua_State *l) { lua_createtable(l, 0, 16); @@ -182,5 +181,3 @@ int gs_lua_init(lua_State *l) { lua_setglobal(l, "GS"); return 0; } - - diff --git a/src/main.c b/src/main.c index ab12355..c0d7ae1 100644 --- a/src/main.c +++ b/src/main.c @@ -1,16 +1,16 @@ +#include #include #include -#include -#include #include +#include #include #include #include "log.h" -#include "script.h" #include "pad.h" +#include "script.h" #define INIT_SCRIPT "host:script/ps2init.lua" @@ -21,9 +21,7 @@ static int ps2luaprog_start_nil(lua_State *l) { return 0; } -static int ps2luaprog_frame_nil(lua_State *l) { - return 0; -} +static int ps2luaprog_frame_nil(lua_State *l) { return 0; } static int ps2lua_log2(lua_State *l) { int n = lua_tointeger(l, 1); @@ -45,13 +43,13 @@ int ps2luaprog_init(lua_State *l) { } int ps2luaprog_onstart(lua_State *l) { - lua_getglobal(l, "PS2PROG"); + lua_getglobal(l, "PS2PROG"); lua_pushstring(l, "start"); lua_gettable(l, -2); int type = lua_type(l, -1); // info("start fn has type :: %s (%d)", lua_typename(l, type), type); int rc = lua_pcall(l, 0, 0, 0); - if ( rc ) { + if (rc) { const char *err = lua_tostring(l, -1); logerr("lua execution error (start event) -- %s", err); } @@ -60,7 +58,7 @@ int ps2luaprog_onstart(lua_State *l) { } int ps2luaprog_onframe(lua_State *l) { - lua_getglobal(l, "PS2PROG"); + lua_getglobal(l, "PS2PROG"); lua_pushstring(l, "frame"); lua_gettable(l, -2); int type = lua_type(l, -1); @@ -68,7 +66,7 @@ int ps2luaprog_onframe(lua_State *l) { // int rc = lua_pcall(l, 0, 0, 0); - if ( rc ) { + if (rc) { const char *err = lua_tostring(l, -1); logerr("lua execution error (frame event) -- %s", err); } @@ -76,34 +74,29 @@ int ps2luaprog_onframe(lua_State *l) { return 0; } -int ps2luaprog_is_running(lua_State *l) { - return 1; -} +int ps2luaprog_is_running(lua_State *l) { return 1; } static int runfile(lua_State *l, const char *fname) { info("running lua file %s", fname); int rc = luaL_loadfile(l, fname); - if ( rc == LUA_ERRSYNTAX ) { + if (rc == LUA_ERRSYNTAX) { logerr("failed to load %s: syntax error", fname); const char *err = lua_tostring(l, -1); logerr("err: %s", err); return -1; - } - else if ( rc == LUA_ERRMEM ) { + } else if (rc == LUA_ERRMEM) { logerr("faild to allocate memory for %s", fname); return -1; - } - else if ( rc == LUA_ERRFILE ) { + } else if (rc == LUA_ERRFILE) { logerr("could not open/read file %s", fname); return -1; - } - else if ( rc ) { + } else if (rc) { logerr("unknown error loading %s", fname); return -1; } rc = lua_pcall(l, 0, 0, 0); - if ( rc ) { + if (rc) { const char *err = lua_tostring(l, -1); logerr("lua execution error -- %s", err); return -1; @@ -125,7 +118,7 @@ int main(int argc, char *argv[]) { struct lua_State *L; L = luaL_newstate(); - if ( !L ) { + if (!L) { logerr("failed to start lua state"); return -1; } @@ -137,7 +130,7 @@ int main(int argc, char *argv[]) { lua_tga_init(L); pad_lua_init(L); - //TODO: better abstraction for drawlua_* + // TODO: better abstraction for drawlua_* drawlua_init(L); info("finished lua state setup"); @@ -146,7 +139,7 @@ int main(int argc, char *argv[]) { runfile(L, startup); ps2luaprog_onstart(L); - while( ps2luaprog_is_running(L) ) { + while (ps2luaprog_is_running(L)) { pad_frame_start(); pad_poll(); dma_wait_fast(); diff --git a/src/script.h b/src/script.h index 505ca82..03f5f8a 100644 --- a/src/script.h +++ b/src/script.h @@ -12,8 +12,6 @@ struct gs_state { unsigned char clear_b; }; - - int gs_lua_init(lua_State *l); int dma_lua_init(lua_State *l); int drawlua_init(lua_State *l); diff --git a/src/tga.c b/src/tga.c index d3008e5..8db88fa 100644 --- a/src/tga.c +++ b/src/tga.c @@ -1,10 +1,10 @@ +#include #include #include -#include -#include #include #include +#include #include "log.h" @@ -25,7 +25,7 @@ struct __attribute__((__packed__)) tga_header { uint8_t descriptor; }; -char tmp_buffer[256*256*4]; +char tmp_buffer[256 * 256 * 4]; int load_tga_to_raw(const char *fname, void *buffer) { info("loading TGA %s", fname); FILE *f = fopen(fname, "rb"); @@ -37,16 +37,20 @@ int load_tga_to_raw(const char *fname, void *buffer) { rc = fread(idData, 1, header.idlen, f); // bytes per pixel from bits per pixel - int bpp = header.bps/8; - info("reading image data - %d bytes", header.width*header.height*bpp); - rc = fread(tmp_buffer, bpp, header.width*header.height*bpp, f); - char *to = (char *) buffer; - for ( int i = 0; i < header.width; i++ ) { - for ( int j = 0; j < header.height; j++ ) { - to[ (j*header.width + i) *4 + 3 ] = tmp_buffer[ (j*header.width + i) *4 + 3 ]; - to[ (j*header.width + i) *4 + 2 ] = tmp_buffer[ (j*header.width + i) *4 + 0 ]; - to[ (j*header.width + i) *4 + 1 ] = tmp_buffer[ (j*header.width + i) *4 + 1 ]; - to[ (j*header.width + i) *4 + 0 ] = tmp_buffer[ (j*header.width + i) *4 + 2 ]; + int bpp = header.bps / 8; + info("reading image data - %d bytes", header.width * header.height * bpp); + rc = fread(tmp_buffer, bpp, header.width * header.height * bpp, f); + char *to = (char *)buffer; + for (int i = 0; i < header.width; i++) { + for (int j = 0; j < header.height; j++) { + to[(j * header.width + i) * 4 + 3] = + tmp_buffer[(j * header.width + i) * 4 + 3]; + to[(j * header.width + i) * 4 + 2] = + tmp_buffer[(j * header.width + i) * 4 + 0]; + to[(j * header.width + i) * 4 + 1] = + tmp_buffer[(j * header.width + i) * 4 + 1]; + to[(j * header.width + i) * 4 + 0] = + tmp_buffer[(j * header.width + i) * 4 + 2]; } } fclose(f); @@ -54,16 +58,16 @@ int load_tga_to_raw(const char *fname, void *buffer) { } int load_tga_lua(lua_State *l) { - const char *fname = lua_tostring(l, 1); + const char *fname = lua_tostring(l, 1); int width = lua_tointeger(l, 2); int height = lua_tointeger(l, 3); - uint32_t *b = malloc(width*height*4); + uint32_t *b = malloc(width * height * 4); load_tga_to_raw(fname, b); lua_createtable(l, 0, 5); lua_pushinteger(l, 0); lua_setfield(l, -2, "head"); - lua_pushinteger(l, width*height*4); + lua_pushinteger(l, width * height * 4); lua_setfield(l, -2, "size"); lua_pushlightuserdata(l, b); lua_setfield(l, -2, "ptr"); From 3945ba42b1ea39723cb5a578f0b3a73f5568325b Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Sat, 16 Oct 2021 00:49:25 +1100 Subject: [PATCH 24/29] update TODOs --- Makefile | 4 ++-- src/bufferlua.c | 16 ++++++++-------- src/gslua.c | 2 +- src/main.c | 2 +- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 49bed41..9b1c9a1 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ scripts: if ! [ -d dist/script ]; then mkdir -p dist/script; fi cp script/* dist/script -# TODO(phy1um): update ISO building to include everything in dist/ +# TODO(Tom Marks): update ISO building to include everything in dist/ $(ISO_TGT): $(EE_BIN) mkisofs -l -o $(ISO_TGT) $(BIN) dist/SYSTEM.CNF @@ -53,7 +53,7 @@ clean: run: scripts PCSX2 --elf=$(PWD)/$(BIN) -# TODO(phy1um): this could be improved, hard-coded ELF name is bad +# TODO(Tom Marks): this could be improved, hard-coded ELF name is bad .PHONY: runps2 runps2: scripts ps2client -h $(PS2HOST) -t 10 execee host:test.elf diff --git a/src/bufferlua.c b/src/bufferlua.c index 73e7f8f..60f8ab5 100644 --- a/src/bufferlua.c +++ b/src/bufferlua.c @@ -23,10 +23,10 @@ static int buffer_pushint(lua_State *l) { lua_pushstring(l, "head"); lua_gettable(l, 1); int head = lua_tointeger(l, -1); - // TODO: check size + // TODO(Tom Marks): check size // ASSUME 4byte int if (head % 4 != 0) { - // TODO: manually bitmask etc + // TODO(Tom Marks): manually bitmask etc logerr("cannot write int to buffer, head%%4 != 0 (%d|%d)", head, head % 4); return 0; } @@ -46,10 +46,10 @@ static int buffer_pushfloat(lua_State *l) { lua_pushstring(l, "head"); lua_gettable(l, 1); int head = lua_tointeger(l, -1); - // TODO: check size + // TODO(Tom Marks): check size // ASSUME 4byte int if (head % 4 != 0) { - // TODO: manually bitmask etc + // TODO(Tom Marks): manually bitmask etc logerr("cannot write int to buffer, head%%4 != 0 (%d|%d)", head, head % 4); return 0; } @@ -77,10 +77,10 @@ static int buffer_settex(lua_State *l) { lua_pushstring(l, "head"); lua_gettable(l, 1); int head = lua_tointeger(l, -1); - // TODO: check size + // TODO(Tom Marks): check size // ASSUME 4byte int if (head % 4 != 0) { - // TODO: manually bitmask etc + // TODO(Tom Marks): manually bitmask etc logerr("cannot write int to buffer, head%%4 != 0 (%d|%d)", head, head % 4); return 0; } @@ -88,7 +88,7 @@ static int buffer_settex(lua_State *l) { int *base = ptr + (head / 4); int v1 = tbp | (tbw << 14) | (psm << 20) | (tw << 26) | ((th & 0x3) << 30); - // TODO: 0x5??? i must mean 0x4 + // TODO(Tom Marks): 0x5??? i must mean 0x4 int v2 = ((th & 0x5) >> 2) | (tcc << 1) | (tfx << 2); int v3 = 0x6 + reg; int v4 = 0; @@ -301,7 +301,7 @@ static int drawlua_end_frame(lua_State *l) { static int drawbuffer_free(lua_State *l) { return 0; } -// TODO: document this can only be called ONCE +// TODO(Tom Marks): document this can only be called ONCE static int drawlua_new_drawbuffer(lua_State *l) { int size = lua_tointeger(l, 1); if (size >= DRAW_BUFFER_MAX_SIZE) { diff --git a/src/gslua.c b/src/gslua.c index 2018171..2026b67 100644 --- a/src/gslua.c +++ b/src/gslua.c @@ -80,7 +80,7 @@ static int gslua_set_buffers(lua_State *l) { lua_pop(l, 1); if (!st) { - // TODO: error + // TODO(Tom Marks): error } st->fb.address = fb_addr; diff --git a/src/main.c b/src/main.c index c0d7ae1..d0c8435 100644 --- a/src/main.c +++ b/src/main.c @@ -130,7 +130,7 @@ int main(int argc, char *argv[]) { lua_tga_init(L); pad_lua_init(L); - // TODO: better abstraction for drawlua_* + // TODO(Tom Marks): better abstraction for drawlua_* drawlua_init(L); info("finished lua state setup"); From 28274b4750fd7d0913fc7be3e82e2f7ffc74b639 Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Sat, 16 Oct 2021 00:50:38 +1100 Subject: [PATCH 25/29] add texture assets and font license --- Makefile | 1 + asset/bigfont.tga | Bin 0 -> 65580 bytes asset/font.txt | 2 ++ asset/test.tga | Bin 0 -> 16428 bytes 4 files changed, 3 insertions(+) create mode 100644 asset/bigfont.tga create mode 100644 asset/font.txt create mode 100644 asset/test.tga diff --git a/Makefile b/Makefile index 9b1c9a1..c1b6655 100644 --- a/Makefile +++ b/Makefile @@ -18,6 +18,7 @@ assets: if ! [ -d dist ]; then mkdir dist; fi $(MAKE) -C asset cp asset/*.bin dist/ + cp asset/*.tga dist/ cp distfiles/* dist/ $(BIN): src/test.elf diff --git a/asset/bigfont.tga b/asset/bigfont.tga new file mode 100644 index 0000000000000000000000000000000000000000..3b99e297beaebc929555bea2a586ed1c05d3ddb9 GIT binary patch literal 65580 zcmeI1O^zi=5rhlY9D*fk308f8*$h%kh?e*h3znP&vEe)%f`hO};sk?ym{0n~+dU#O z^SxKEy5dPSW^QKg5$RRgH9hn4^3#|9f4+Hn`N=mgKmWxK<%Ye1y@9=fQycj1hY!uU zMxSBy%`#W~m3xu#T`KL$Jzl$)=XvdW?DOB&2D+b{tdHvK#RkwacH1FmA1%r_ zSAEZVW}csu+F0EmKYQp#XRO2VbBzw-S2)UyJZ6nDc!A`N_HWHOXLar@k8fO^?Hv8q zf@ghH&%8YQ>U_`3XRmr^a_)I1o_CPzxa&v@&(AZ;&SrJ~z32PR=A3KIyzqGSU?I-XTPf5V@}qZdF7)8R@qg5)QgwVv*Bc@ zGtT^+cYU_kedf$YGvXQXt-Y(tNGcSnZJ7e*<5wLH8_~h>2&A13_3{9YE2Hz z{pcWm#>z)C(uYPJSZO1j-m`u5uj0MV>E_PvZ#^cTGcz! z&dzY3c|I$=(5g(O<*YNo^Zc%MUzf4o*qx0Y$hm0LV5f&a^5_Shg;z*E zBiYc+$*pQf`kCGJ*gwOoJ2M9_BfgQ0wcwqebGNQJx59i@WI`ir-&)7JUeD>cXLgy< zeEPv^#>lJkx6(%ER$is8Jp1Z>yZNItJlp-|WLCBAysDqsU61{^S@)aI?pd8N>!10vGiUuX zpZV3@?XCE$nLKx;uX>JZ&PY2u!+qwyH#+EhR9fXb7u=Z@?*1x2^Q)dC+SU10f2HL+ zkuf@B-8?d`GiOHDqsGff);gN;h#y&c-3P}r;ssiV+lP-l&mrI8Y)+uHm_hG!B?d}ci4eSl<4eSl<4eSl<4eSl< z4eSl<4eSl<4eSl<4eSlP9yIDeYvyFEnd2KP-#mnVmkmOVwk|oH<7Pz|gz8 z>T&*=eBZ^KXFHx zBj1&*bLMCKd`6y^>zwCIu*%PRR{Ys~{G;gmX6}388Am?%AWxmK@~y#*o?#tc`Jv&1 zm4+ASInHLCnqF%6;N`jK?lG@?Ynl7Z%$b{esEt*&(%fTCHqbiUKD_c~+EqWF9rMsY z&d7)l;!_((UgTyzvsQEDXXtsEN3S(zSu%m-KIkX=E~%RKt6G0&R$ zs!uNP9{pG6S9j{}dgLs!o^OtCtbA+c(MKKVx|>PfzIo+aGpDc8@B%x((r4NcKeGA! zS-s#ek=lJXqpUta$Yv!C| z%^crY`PR(c2S4Mx$9y)k%F+{9`7^EZkIbz4t(h~|nmNAFe(>zL3^nu~SL3*eIt(lXt2G5wY z*^in`AilMZkFq1Lo4577fm>`~|LmRLBD*to)5zdc8}T;o4LouK{0u(w%zf70z}~># zz}~>|4e;l6g~zAuj=h1sfv<4`{Q2-TKHGk#y@6-h0Pk5~z7t-jS3Nm6PbB>-hn_KynR^4kgSM`1k+Ix8Z*)v}CZ}D?5yF)(XXg>E>^_iJxdgAOO zeY0%kSK6(fxtjA;w`Pvt^+%SB`^>NSxtBVSGcqUh8a`(lxetg(o$;!l=iRIJJW{R}cYW6VPHTx;gez$@RH`Dzcjz*+yypUs;2 z%m9dx3WUS?$@X%&i=ioteG>@nmN9)@~xS> zFY_)}>DJ7dS!v;Q`l!!6=JbG_XFYi2vxZ*Ha`sU^=X5x`llxY(&X~s>*TJ)vYv;2U zb$oKB)4mVuA61|1a_$k|*<11I&eqI1)0#QHvGT2%yD#%DSLxQwnOSM!b^53;_vCui z+nwnd=GCm%@VQ5avpczOHA^nA>&GJ-Idl4~nOA-{gRHTdbxYr~ZppcK#yBHu)IJ|A zBblsIulkY8J@ncm6NryS?YuSX?ssN&R+a1KS6OT3+$C$&#+Pl2`mdk;=$yws=|1f(Y@mAoEA1^j%Uif3 z?=SB>==VF`f8OCh>Uh5BkN4g_HGX)!`(S8qVL$thkKI6i*Qr5n6LoO(t|q^+-T?1^ zypMkOc|XJFeGe_ZsAJ;F`|o!?`l;(1ZS_5GfcH6eV4iEd8NTOTt~@*MKkq)c-~W~E z?aZw=!229?&2xn#*$9qp5UHtx!-}(OXpAW(LyFdT#Z$EzD=P&&J zX4`p>8{oZ;XE)FF$jjNN_ovK zW?Y+Ruc|}apS^*-f#=>p_g;58Pc!q$ZQL8!8+hIgmpMLZ0uU`HK%N=Y9 literal 0 HcmV?d00001 diff --git a/asset/font.txt b/asset/font.txt new file mode 100644 index 0000000..ce2037b --- /dev/null +++ b/asset/font.txt @@ -0,0 +1,2 @@ +SPLEEN +https://github.com/fcambus/spleen diff --git a/asset/test.tga b/asset/test.tga new file mode 100644 index 0000000000000000000000000000000000000000..f46ba4b9f753f77b731f1fa795b8163859024d9d GIT binary patch literal 16428 zcmeI3$%|b@6viL!-Dc%NTm%v9MOKjz2tq536Ao!yCR}x*d^9_&7D3={t4q?Yw%O$>g-jKYw;i z_SzS+z9kNo#a z=UHKE7ti{Z_}C-7AY9Vvuoiv5cQx#-XM3x3Zx9|9qV7YQKQ25cYy|)J6z5gpZsAAa z48i$Wa;|R*w+ZO%65#)ZaFMWGI4XQ7aOS|Z=(GJ*ItPTC1;-(mH-vkHpM{$IKa;3DJN8SU5P?^u?^ z;vIEDSP8gJW9Nh1B77~_4tczrbM9XW^uZYiOMz(*C0l z*P1@_I=#=>`j+wh@6`U`U;~fO!+cd^YGR*#wmJSk1iRNX9{c>s@lkxTgDjy)W}Q zduL`Xt!s%+YICaQd&K8$;h=D$8?Q)`PrQS1e%!x$+$T$P_80g`{m)~Q`skbU9T0bu zaUcG72rmls346D95`EUWpN}>rI=f}_yTJVwccNzr^cweD*e`cr%szMTsZAPl=sd^& z55?vF`aZ$;l-&7oANG^LeLpogBEFkpJZvwoqqs|WsPYB!%ZWD+H_XEOXLTVG^ z)fnga!_mI|EWnLBIQIMi59q z`WXertS!G^^GXNC`#X#oGv^zgj!i!A2+oV~M*{iPs)u6`lg~l;#lOM6_j36TT{D-+ z)3I|L$&r2Xvh9!@axtPm*TZ#+dqc6txz0x`Myi;m$ zcTDHB5zm72M>>9qBsXk$XU}R|;vb(p3-p#h3sB=RVv&!z5RY*Rznj>HGxM=>iS;pO zVmUYBb1%>TYlylv#+)nlPWgLYv(Ja*d%bczZv_5Ba%#5q`|*ESQ Date: Sat, 16 Oct 2021 01:03:51 +1100 Subject: [PATCH 26/29] add some license info --- LICENSE | 22 ++++++++++++++++++++++ distfiles/spleen.font.LICENSE | 25 +++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 LICENSE create mode 100644 distfiles/spleen.font.LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..bce361a --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +MIT License + +Copyright (c) [year] [fullname] + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/distfiles/spleen.font.LICENSE b/distfiles/spleen.font.LICENSE new file mode 100644 index 0000000..2e1411a --- /dev/null +++ b/distfiles/spleen.font.LICENSE @@ -0,0 +1,25 @@ +Copyright (c) 2018-2021, Frederic Cambus +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS +BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + From ec7407418e0a855cbd3c6a1fd7117a89516ecec2 Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Sat, 16 Oct 2021 01:04:01 +1100 Subject: [PATCH 27/29] add missing padlua source file --- src/padlua.c | 142 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 src/padlua.c diff --git a/src/padlua.c b/src/padlua.c new file mode 100644 index 0000000..ead16a1 --- /dev/null +++ b/src/padlua.c @@ -0,0 +1,142 @@ +#include + +#include +#include +#include + +#include + +#include "log.h" +#include "pad.h" + +#define pad_test(b, v) \ + do { \ + if ((c & b)) { \ + info("set button %d", v); \ + btn_held[v] = 1; \ + } \ + } while (0) + +char *pad_buffer; +struct padButtonStatus *pad_read_space; + +int *btn_held; + +unsigned char joysticks[JOY_AXIS_COUNT]; + +int prev_inputs; + +int button_held(int b) { return btn_held[b]; } + +unsigned char joy_axis_value(int a) { return joysticks[a]; } + +int pad_init() { + btn_held = memalign(128, 12 * sizeof(int)); + pad_buffer = memalign(256, 256); + if ((u32)pad_buffer & 0xf) { + info("pad buffer was not 16byte aligned: %x", (int)pad_buffer); + return -1; + } + pad_read_space = memalign(128, sizeof(struct padButtonStatus)); + + info("loading SIF modules for PAD"); + int rc = SifLoadModule(R_SIO2MAN, 0, 0); + if (!rc) { + logerr("failed to load SIF %s", R_SIO2MAN); + return 0; + } + rc = SifLoadModule(R_PADMAN, 0, 0); + if (!rc) { + logerr("failed to load SIF %s", R_PADMAN); + return 0; + } + padInit(0); + padPortOpen(0, 0, pad_buffer); + // TODO(phy1um): check for dualshock controller + padSetMainMode(0, 0, 1, 3); + return 1; +} + +static int pad_wait(int port, int slot, int tries) { + int now; + now = padGetState(port, slot); + if (now == PAD_STATE_DISCONN) { + return -1; + } + while ((now != PAD_STATE_STABLE) && (now != PAD_STATE_FINDCTP1)) { + now = padGetState(port, slot); + tries--; + if (tries == 0) { + break; + } + } + return 0; +} + +void pad_poll() { + if (pad_wait(0, 0, 10) < 0) { + return; + } + + if (padRead(0, 0, pad_read_space) != 0) { + int pad = 0xffff ^ pad_read_space->btns; + int c = pad ^ prev_inputs; + pad_test(PAD_LEFT, DPAD_LEFT); + pad_test(PAD_RIGHT, DPAD_RIGHT); + pad_test(PAD_UP, DPAD_UP); + pad_test(PAD_DOWN, DPAD_DOWN); + pad_test(PAD_CROSS, BUTTON_X); + pad_test(PAD_SQUARE, BUTTON_SQUARE); + pad_test(PAD_TRIANGLE, BUTTON_TRIANGLE); + pad_test(PAD_CIRCLE, BUTTON_CIRCLE); + pad_test(PAD_L1, BUTTON_L1); + pad_test(PAD_L2, BUTTON_L2); + pad_test(PAD_R1, BUTTON_R1); + pad_test(PAD_R2, BUTTON_R2); + /* + joysticks[AXIS_LEFT_X] = pad_read_space->ljoy_h; + joysticks[AXIS_LEFT_Y] = pad_read_space->ljoy_v; + joysticks[AXIS_RIGHT_X] = pad_read_space->rjoy_h; + joysticks[AXIS_RIGHT_Y] = pad_read_space->rjoy_v; + */ + } +} + +void pad_frame_start() { + for (int i = 0; i < BTN_MAX; i++) { + btn_held[i] = 0; + } +} + +static int pad_lua_button_held(lua_State *l) { + int button_id = lua_tointeger(l, 1); + // TODO(Tom Marks): bounds check + int v = btn_held[button_id]; + info("TEST BTN %d = %d", button_id, v); + lua_pushboolean(l, v); + return 1; +} + +#define bind_int(v, name) \ + lua_pushinteger(l, v); \ + lua_setfield(l, -2, name) +int pad_lua_init(lua_State *l) { + if (pad_init() == -1) { + lua_pushstring(l, "failed to init pad"); + lua_error(l); + return 1; + } + lua_createtable(l, 0, 5); + lua_pushcfunction(l, pad_lua_button_held); + lua_setfield(l, -2, "held"); + bind_int(BUTTON_X, "X"); + bind_int(BUTTON_SQUARE, "TRIANGLE"); + bind_int(BUTTON_TRIANGLE, "SQUARE"); + bind_int(BUTTON_CIRCLE, "CIRCLE"); + bind_int(DPAD_LEFT, "LEFT"); + bind_int(DPAD_RIGHT, "RIGHT"); + bind_int(DPAD_UP, "UP"); + bind_int(DPAD_DOWN, "DOWN"); + lua_setglobal(l, "PAD"); + return 0; +} From 09f26f5df1ef1b2f376154fb2483ec6d22c565a5 Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Sat, 16 Oct 2021 01:04:59 +1100 Subject: [PATCH 28/29] add missing name and year to license --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index bce361a..39059e0 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) [year] [fullname] +Copyright (c) 2021 Tom Marks Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From 9e7f9f082e03e31d7a7c926fc47ccf4605d5b338 Mon Sep 17 00:00:00 2001 From: Tom Marks Date: Sat, 16 Oct 2021 01:12:38 +1100 Subject: [PATCH 29/29] add scripts to asset build --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index c1b6655..24de624 100644 --- a/Makefile +++ b/Makefile @@ -14,12 +14,13 @@ include .lintvars dist: $(BIN) assets .PHONY: assets -assets: +assets: scripts if ! [ -d dist ]; then mkdir dist; fi $(MAKE) -C asset cp asset/*.bin dist/ cp asset/*.tga dist/ cp distfiles/* dist/ + cp LICENSE dist/ $(BIN): src/test.elf if ! [ -d dist ]; then mkdir dist; fi