Skip to content

Commit 376c4d0

Browse files
committed
rework: use opaque type for extapi
1 parent 6fa0613 commit 376c4d0

14 files changed

Lines changed: 133 additions & 69 deletions

extlua/extlua_sample.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ struct pquad_inst {
4343
struct material_perspective_quad {
4444
sg_pipeline pip;
4545
sg_buffer inst;
46-
void *bind;
46+
struct soluna_render_bindings bind;
4747
int base;
4848
vs_params_t *uniform;
49-
void *bank;
49+
struct soluna_sprite_bank bank;
5050
void *tmp_ptr;
5151
size_t tmp_size;
5252
};
@@ -171,7 +171,7 @@ build_quad_from_rect(const struct sprite_rect_basis *basis, const struct soluna_
171171
}
172172

173173
static void
174-
submit(void *m_, void *ctx, int n) {
174+
submit(void *m_, struct soluna_material_stream_context ctx, int n) {
175175
struct material_perspective_quad *m = (struct material_perspective_quad *)m_;
176176
struct pquad_inst *tmp = (struct pquad_inst *)m->tmp_ptr;
177177
int out_n;
@@ -328,7 +328,9 @@ lnew_material_perspective_quad(lua_State *L) {
328328
if (lua_getfield(L, 1, "bindings") != LUA_TUSERDATA) {
329329
return luaL_error(L, "Invalid key .bindings");
330330
}
331-
m->bind = luaL_checkudata(L, -1, "SOKOL_BINDINGS");
331+
m->bind = (struct soluna_render_bindings) {
332+
.ctx = luaL_checkudata(L, -1, "SOKOL_BINDINGS"),
333+
};
332334
lua_pushvalue(L, -1);
333335
lua_setiuservalue(L, material_index, 2);
334336
lua_pop(L, 1);
@@ -344,7 +346,9 @@ lnew_material_perspective_quad(lua_State *L) {
344346
if (lua_getfield(L, 1, "sprite_bank") != LUA_TLIGHTUSERDATA) {
345347
return luaL_error(L, "Invalid key .sprite_bank");
346348
}
347-
m->bank = lua_touserdata(L, -1);
349+
m->bank = (struct soluna_sprite_bank) {
350+
.ctx = lua_touserdata(L, -1),
351+
};
348352
lua_pop(L, 1);
349353

350354
if (lua_getfield(L, 1, "tmp_buffer") != LUA_TUSERDATA) {

extlua/gen_soluna.lua

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ local apis = {
1717
ret = "int",
1818
name = "soluna_material_sprite_rect",
1919
params = {
20-
{ type = "void *", name = "bank" },
20+
{ type = "struct soluna_sprite_bank ", name = "bank" },
2121
{ type = "int ", name = "sprite" },
2222
{ type = "struct soluna_sprite_rect *", name = "out" },
2323
},
@@ -26,7 +26,7 @@ local apis = {
2626
ret = "sg_bindings",
2727
name = "soluna_material_bindings",
2828
params = {
29-
{ type = "void *", name = "bindings" },
29+
{ type = "struct soluna_render_bindings ", name = "bindings" },
3030
},
3131
},
3232
{
@@ -52,7 +52,7 @@ local apis = {
5252
ret = "int",
5353
name = "soluna_material_stream_read",
5454
params = {
55-
{ type = "void *", name = "ctx" },
55+
{ type = "struct soluna_material_stream_context ", name = "ctx" },
5656
{ type = "int ", name = "index" },
5757
{ type = "size_t ", name = "payload_size" },
5858
{ type = "void *", name = "payload" },
@@ -63,15 +63,15 @@ local apis = {
6363
ret = "void",
6464
name = "soluna_material_stream_error",
6565
params = {
66-
{ type = "void *", name = "ctx" },
66+
{ type = "struct soluna_material_stream_context ", name = "ctx" },
6767
{ type = "const char *", name = "error" },
6868
},
6969
},
7070
{
7171
ret = "int",
7272
name = "soluna_material_stream_failed",
7373
params = {
74-
{ type = "void *", name = "ctx" },
74+
{ type = "struct soluna_material_stream_context ", name = "ctx" },
7575
},
7676
},
7777
}
@@ -108,7 +108,20 @@ struct soluna_material_stream {
108108
};
109109
110110
typedef const char *soluna_material_error;
111-
typedef void (*soluna_material_submit_func)(void *ud, void *ctx, int n);
111+
112+
struct soluna_material_stream_context {
113+
void *ctx;
114+
};
115+
116+
struct soluna_render_bindings {
117+
void *ctx;
118+
};
119+
120+
struct soluna_sprite_bank {
121+
void *ctx;
122+
};
123+
124+
typedef void (*soluna_material_submit_func)(void *ud, struct soluna_material_stream_context ctx, int n);
112125
typedef void (*soluna_material_stream_write_func)(void *ud, int index, struct soluna_material_stream_item *item);
113126
]]
114127

extlua/solunaapi.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ struct soluna_api {
88
int version;
99

1010
soluna_material_error (*material_submit) (const void *stream, int prim_n, int material_id, int batch_n, void *ud, soluna_material_submit_func submit);
11-
int (*material_sprite_rect) (void *bank, int sprite, struct soluna_sprite_rect *out);
12-
sg_bindings (*material_bindings) (void *bindings);
11+
int (*material_sprite_rect) (struct soluna_sprite_bank bank, int sprite, struct soluna_sprite_rect *out);
12+
sg_bindings (*material_bindings) (struct soluna_render_bindings bindings);
1313
soluna_material_error (*material_push_stream) (int material_id, int count, size_t payload_size, soluna_material_stream_write_func write, void *ud, struct soluna_material_stream *out);
1414
void (*material_stream_free) (void *ptr);
15-
int (*material_stream_read) (void *ctx, int index, size_t payload_size, void *payload, struct soluna_material_stream_data *out);
16-
void (*material_stream_error) (void *ctx, const char *error);
17-
int (*material_stream_failed) (void *ctx);
15+
int (*material_stream_read) (struct soluna_material_stream_context ctx, int index, size_t payload_size, void *payload, struct soluna_material_stream_data *out);
16+
void (*material_stream_error) (struct soluna_material_stream_context ctx, const char *error);
17+
int (*material_stream_failed) (struct soluna_material_stream_context ctx);
1818
};
1919

2020
static struct soluna_api API;
@@ -25,12 +25,12 @@ soluna_material_submit(const void *stream, int prim_n, int material_id, int batc
2525
}
2626

2727
int
28-
soluna_material_sprite_rect(void *bank, int sprite, struct soluna_sprite_rect *out) {
28+
soluna_material_sprite_rect(struct soluna_sprite_bank bank, int sprite, struct soluna_sprite_rect *out) {
2929
return API.material_sprite_rect(bank, sprite, out);
3030
}
3131

3232
sg_bindings
33-
soluna_material_bindings(void *bindings) {
33+
soluna_material_bindings(struct soluna_render_bindings bindings) {
3434
return API.material_bindings(bindings);
3535
}
3636

@@ -45,17 +45,17 @@ soluna_material_stream_free(void *ptr) {
4545
}
4646

4747
int
48-
soluna_material_stream_read(void *ctx, int index, size_t payload_size, void *payload, struct soluna_material_stream_data *out) {
48+
soluna_material_stream_read(struct soluna_material_stream_context ctx, int index, size_t payload_size, void *payload, struct soluna_material_stream_data *out) {
4949
return API.material_stream_read(ctx, index, payload_size, payload, out);
5050
}
5151

5252
void
53-
soluna_material_stream_error(void *ctx, const char *error) {
53+
soluna_material_stream_error(struct soluna_material_stream_context ctx, const char *error) {
5454
API.material_stream_error(ctx, error);
5555
}
5656

5757
int
58-
soluna_material_stream_failed(void *ctx) {
58+
soluna_material_stream_failed(struct soluna_material_stream_context ctx) {
5959
return API.material_stream_failed(ctx);
6060
}
6161

extlua/solunaapi.h

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,31 @@ struct soluna_material_stream {
4040
};
4141

4242
typedef const char *soluna_material_error;
43-
typedef void (*soluna_material_submit_func)(void *ud, void *ctx, int n);
43+
44+
struct soluna_material_stream_context {
45+
void *ctx;
46+
};
47+
48+
struct soluna_render_bindings {
49+
void *ctx;
50+
};
51+
52+
struct soluna_sprite_bank {
53+
void *ctx;
54+
};
55+
56+
typedef void (*soluna_material_submit_func)(void *ud, struct soluna_material_stream_context ctx, int n);
4457
typedef void (*soluna_material_stream_write_func)(void *ud, int index, struct soluna_material_stream_item *item);
4558

4659

4760
void solunaapi_init(lua_State *L);
4861
soluna_material_error soluna_material_submit(const void *stream, int prim_n, int material_id, int batch_n, void *ud, soluna_material_submit_func submit);
49-
int soluna_material_sprite_rect(void *bank, int sprite, struct soluna_sprite_rect *out);
50-
sg_bindings soluna_material_bindings(void *bindings);
62+
int soluna_material_sprite_rect(struct soluna_sprite_bank bank, int sprite, struct soluna_sprite_rect *out);
63+
sg_bindings soluna_material_bindings(struct soluna_render_bindings bindings);
5164
soluna_material_error soluna_material_push_stream(int material_id, int count, size_t payload_size, soluna_material_stream_write_func write, void *ud, struct soluna_material_stream *out);
5265
void soluna_material_stream_free(void *ptr);
53-
int soluna_material_stream_read(void *ctx, int index, size_t payload_size, void *payload, struct soluna_material_stream_data *out);
54-
void soluna_material_stream_error(void *ctx, const char *error);
55-
int soluna_material_stream_failed(void *ctx);
66+
int soluna_material_stream_read(struct soluna_material_stream_context ctx, int index, size_t payload_size, void *payload, struct soluna_material_stream_data *out);
67+
void soluna_material_stream_error(struct soluna_material_stream_context ctx, const char *error);
68+
int soluna_material_stream_failed(struct soluna_material_stream_context ctx);
5669

5770
#endif

extlua/solunaapi_impl.c

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,43 @@ struct soluna_material_stream {
3535
};
3636

3737
typedef const char *soluna_material_error;
38-
typedef void (*soluna_material_submit_func)(void *ud, void *ctx, int n);
38+
39+
struct soluna_material_stream_context {
40+
void *ctx;
41+
};
42+
43+
struct soluna_render_bindings {
44+
void *ctx;
45+
};
46+
47+
struct soluna_sprite_bank {
48+
void *ctx;
49+
};
50+
51+
typedef void (*soluna_material_submit_func)(void *ud, struct soluna_material_stream_context ctx, int n);
3952
typedef void (*soluna_material_stream_write_func)(void *ud, int index, struct soluna_material_stream_item *item);
4053

4154

4255
extern soluna_material_error material_submit(const void *stream, int prim_n, int material_id, int batch_n, void *ud, soluna_material_submit_func submit);
43-
extern int material_sprite_rect(void *bank, int sprite, struct soluna_sprite_rect *out);
44-
extern sg_bindings material_bindings(void *bindings);
56+
extern int material_sprite_rect(struct soluna_sprite_bank bank, int sprite, struct soluna_sprite_rect *out);
57+
extern sg_bindings material_bindings(struct soluna_render_bindings bindings);
4558
extern soluna_material_error material_push_stream(int material_id, int count, size_t payload_size, soluna_material_stream_write_func write, void *ud, struct soluna_material_stream *out);
4659
extern void material_stream_free(void *ptr);
47-
extern int material_stream_read(void *ctx, int index, size_t payload_size, void *payload, struct soluna_material_stream_data *out);
48-
extern void material_stream_error(void *ctx, const char *error);
49-
extern int material_stream_failed(void *ctx);
60+
extern int material_stream_read(struct soluna_material_stream_context ctx, int index, size_t payload_size, void *payload, struct soluna_material_stream_data *out);
61+
extern void material_stream_error(struct soluna_material_stream_context ctx, const char *error);
62+
extern int material_stream_failed(struct soluna_material_stream_context ctx);
5063

5164
struct soluna_api {
5265
int version;
5366

5467
soluna_material_error (*material_submit) (const void *stream, int prim_n, int material_id, int batch_n, void *ud, soluna_material_submit_func submit);
55-
int (*material_sprite_rect) (void *bank, int sprite, struct soluna_sprite_rect *out);
56-
sg_bindings (*material_bindings) (void *bindings);
68+
int (*material_sprite_rect) (struct soluna_sprite_bank bank, int sprite, struct soluna_sprite_rect *out);
69+
sg_bindings (*material_bindings) (struct soluna_render_bindings bindings);
5770
soluna_material_error (*material_push_stream) (int material_id, int count, size_t payload_size, soluna_material_stream_write_func write, void *ud, struct soluna_material_stream *out);
5871
void (*material_stream_free) (void *ptr);
59-
int (*material_stream_read) (void *ctx, int index, size_t payload_size, void *payload, struct soluna_material_stream_data *out);
60-
void (*material_stream_error) (void *ctx, const char *error);
61-
int (*material_stream_failed) (void *ctx);
72+
int (*material_stream_read) (struct soluna_material_stream_context ctx, int index, size_t payload_size, void *payload, struct soluna_material_stream_data *out);
73+
void (*material_stream_error) (struct soluna_material_stream_context ctx, const char *error);
74+
int (*material_stream_failed) (struct soluna_material_stream_context ctx);
6275
};
6376

6477
struct soluna_api *

src/extapi.c

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
#define SOLUNA_THREAD_LOCAL _Thread_local
2323
#endif
2424

25-
typedef void (*material_submit_stride_func)(void *ud, void *ctx, int n);
25+
typedef void (*material_submit_stride_func)(void *ud, struct soluna_material_stream_context ctx, int n);
2626

27-
struct material_stream_context {
27+
struct material_stream_context_impl {
2828
const char *data;
2929
int n;
3030
int material_id;
@@ -34,6 +34,11 @@ struct material_stream_context {
3434

3535
static SOLUNA_THREAD_LOCAL char submit_error_buffer[STREAM_ERROR_SIZE];
3636

37+
static struct material_stream_context_impl *
38+
stream_context(struct soluna_material_stream_context ctx) {
39+
return (struct material_stream_context_impl *)ctx.ctx;
40+
}
41+
3742
static soluna_material_error
3843
copy_error(char *buffer, size_t size, const char *error) {
3944
const char *message = error != NULL ? error : "Material stream error";
@@ -47,16 +52,16 @@ copy_error(char *buffer, size_t size, const char *error) {
4752
}
4853

4954
void
50-
material_stream_error(void *ctx_, const char *error) {
51-
struct material_stream_context *ctx = (struct material_stream_context *)ctx_;
55+
material_stream_error(struct soluna_material_stream_context ctx_, const char *error) {
56+
struct material_stream_context_impl *ctx = stream_context(ctx_);
5257
if (ctx != NULL && ctx->error == NULL) {
5358
ctx->error = copy_error(ctx->error_buffer, sizeof(ctx->error_buffer), error);
5459
}
5560
}
5661

5762
int
58-
material_stream_failed(void *ctx_) {
59-
struct material_stream_context *ctx = (struct material_stream_context *)ctx_;
63+
material_stream_failed(struct soluna_material_stream_context ctx_) {
64+
struct material_stream_context_impl *ctx = stream_context(ctx_);
6065
return ctx == NULL || ctx->error != NULL;
6166
}
6267

@@ -84,24 +89,27 @@ submit_material_stride(const void *data_, int prim_n, int material_id, int batch
8489
int i = 0;
8590
for (;;) {
8691
int n = prim_n - i;
87-
struct material_stream_context ctx = {
92+
struct material_stream_context_impl impl = {
8893
.data = data,
8994
.n = n > batch_n ? batch_n : n,
9095
.material_id = material_id,
9196
.error = NULL,
9297
.error_buffer = { 0 },
9398
};
99+
struct soluna_material_stream_context ctx = {
100+
.ctx = &impl,
101+
};
94102
if (n > batch_n) {
95-
submit(ud, &ctx, batch_n);
96-
if (ctx.error != NULL) {
97-
return copy_error(submit_error_buffer, sizeof(submit_error_buffer), ctx.error);
103+
submit(ud, ctx, batch_n);
104+
if (impl.error != NULL) {
105+
return copy_error(submit_error_buffer, sizeof(submit_error_buffer), impl.error);
98106
}
99107
i += batch_n;
100108
data += stride * batch_n;
101109
} else {
102-
submit(ud, &ctx, n);
103-
if (ctx.error != NULL) {
104-
return copy_error(submit_error_buffer, sizeof(submit_error_buffer), ctx.error);
110+
submit(ud, ctx, n);
111+
if (impl.error != NULL) {
112+
return copy_error(submit_error_buffer, sizeof(submit_error_buffer), impl.error);
105113
}
106114
break;
107115
}
@@ -115,8 +123,8 @@ material_submit(const void *stream, int prim_n, int material_id, int batch_n, vo
115123
}
116124

117125
int
118-
material_sprite_rect(void *bank, int sprite, struct soluna_sprite_rect *out) {
119-
struct sprite_bank *b = (struct sprite_bank *)bank;
126+
material_sprite_rect(struct soluna_sprite_bank bank, int sprite, struct soluna_sprite_rect *out) {
127+
struct sprite_bank *b = (struct sprite_bank *)bank.ctx;
120128
if (b == NULL || out == NULL || sprite < 0 || sprite >= b->n) {
121129
return 0;
122130
}
@@ -132,8 +140,8 @@ material_sprite_rect(void *bank, int sprite, struct soluna_sprite_rect *out) {
132140
}
133141

134142
sg_bindings
135-
material_bindings(void *bindings) {
136-
struct soluna_render_bindings *b = (struct soluna_render_bindings *)bindings;
143+
material_bindings(struct soluna_render_bindings bindings) {
144+
struct render_bindings *b = (struct render_bindings *)bindings.ctx;
137145
assert(b != NULL);
138146
return b->bindings;
139147
}
@@ -221,15 +229,15 @@ clear_stream_read(size_t payload_size, void *payload, struct soluna_material_str
221229
}
222230

223231
static int
224-
fail_stream_read(void *ctx, const char *error, size_t payload_size, void *payload, struct soluna_material_stream_data *out) {
232+
fail_stream_read(struct soluna_material_stream_context ctx, const char *error, size_t payload_size, void *payload, struct soluna_material_stream_data *out) {
225233
material_stream_error(ctx, error);
226234
clear_stream_read(payload_size, payload, out);
227235
return 0;
228236
}
229237

230238
int
231-
material_stream_read(void *ctx_, int index, size_t payload_size, void *payload, struct soluna_material_stream_data *out) {
232-
struct material_stream_context *ctx = (struct material_stream_context *)ctx_;
239+
material_stream_read(struct soluna_material_stream_context ctx_, int index, size_t payload_size, void *payload, struct soluna_material_stream_data *out) {
240+
struct material_stream_context_impl *ctx = stream_context(ctx_);
233241
size_t payload_max = stream_payload_max();
234242
if (ctx == NULL) {
235243
clear_stream_read(payload_size, payload, out);

0 commit comments

Comments
 (0)