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
3535static 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+
3742static soluna_material_error
3843copy_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
4954void
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
5762int
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
117125int
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
134142sg_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
223231static 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
230238int
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