xref: /aosp_15_r20/external/mesa3d/src/gallium/frontends/nine/nine_csmt_helper.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright 2016 Patrick Rudolph <[email protected]>
3  * SPDX-License-Identifier: MIT
4  */
5 
6 /* get number of arguments with __NARG__ */
7 #define __NARG__(...)  __NARG_I_(__VA_ARGS__,__RSEQ_N())
8 #define __NARG_I_(...) __ARG_N(__VA_ARGS__)
9 #define __ARG_N( \
10       _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
11      _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
12      _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
13      _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
14      _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
15      _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
16      _61,_62,_63,_64,_65,_66,_67,_68,_69,_70,N,...) N
17 #define __RSEQ_N() \
18      70,69,68,67,66,65,64,63,62,61,60, \
19      59,58,57,56,55,54,53,52,51,50, \
20      49,48,47,46,45,44,43,42,41,40, \
21      39,38,37,36,35,34,33,32,31,30, \
22      29,28,27,26,25,24,23,22,21,20, \
23      19,18,17,16,15,14,13,12,11,10, \
24      9,8,7,6,5,4,3,2,1,0
25 
26 
27 #define _args_for_bypass_1(a) a
28 #define _args_for_bypass_7(a, b, c, d, e, f, g) ,g
29 #define _args_for_bypass_14(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_7(__VA_ARGS__)
30 #define _args_for_bypass_21(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_14(__VA_ARGS__)
31 #define _args_for_bypass_28(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_21(__VA_ARGS__)
32 #define _args_for_bypass_35(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_28(__VA_ARGS__)
33 #define _args_for_bypass_42(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_35(__VA_ARGS__)
34 #define _args_for_bypass_49(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_42(__VA_ARGS__)
35 #define _args_for_bypass_56(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_49(__VA_ARGS__)
36 #define _args_for_bypass_63(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_56(__VA_ARGS__)
37 #define _args_for_bypass_70(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_63(__VA_ARGS__)
38 #define _args_for_bypass_77(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_70(__VA_ARGS__)
39 
40 #define _GFUNC_(n) _args_for_bypass_##n
41 #define _GFUNC(n) _GFUNC_(n)
42 
43 #define ARGS_FOR_BYPASS(...) _GFUNC(__NARG__(__VA_ARGS__)) (__VA_ARGS__)
44 
45 #define _args_for_mem_1(a) a;
46 #define _args_for_mem_7(a, b, c, d, e, f, g) f;
47 #define _args_for_mem_14(a, b, c, d, e, f, g, ...) f; _args_for_mem_7(__VA_ARGS__)
48 #define _args_for_mem_21(a, b, c, d, e, f, g, ...) f; _args_for_mem_14(__VA_ARGS__)
49 #define _args_for_mem_28(a, b, c, d, e, f, g, ...) f; _args_for_mem_21(__VA_ARGS__)
50 #define _args_for_mem_35(a, b, c, d, e, f, g, ...) f; _args_for_mem_28(__VA_ARGS__)
51 #define _args_for_mem_42(a, b, c, d, e, f, g, ...) f; _args_for_mem_35(__VA_ARGS__)
52 #define _args_for_mem_49(a, b, c, d, e, f, g, ...) f; _args_for_mem_42(__VA_ARGS__)
53 #define _args_for_mem_56(a, b, c, d, e, f, g, ...) f; _args_for_mem_49(__VA_ARGS__)
54 #define _args_for_mem_63(a, b, c, d, e, f, g, ...) f; _args_for_mem_56(__VA_ARGS__)
55 #define _args_for_mem_70(a, b, c, d, e, f, g, ...) f; _args_for_mem_63(__VA_ARGS__)
56 #define _args_for_mem_77(a, b, c, d, e, f, g, ...) f; _args_for_mem_70(__VA_ARGS__)
57 
58 #define _FFUNC_(n) _args_for_mem_##n
59 #define _FFUNC(n) _FFUNC_(n)
60 
61 #define ARGS_FOR_MEM(...) _FFUNC(__NARG__(__VA_ARGS__)) (__VA_ARGS__)
62 
63 #define _args_for_unbind_1(a) a;
64 #define _args_for_unbind_7(a, b, c, d, e, f, g) e;
65 #define _args_for_unbind_14(a, b, c, d, e, f, g, ...) e; _args_for_unbind_7(__VA_ARGS__)
66 #define _args_for_unbind_21(a, b, c, d, e, f, g, ...) e; _args_for_unbind_14(__VA_ARGS__)
67 #define _args_for_unbind_28(a, b, c, d, e, f, g, ...) e; _args_for_unbind_21(__VA_ARGS__)
68 #define _args_for_unbind_35(a, b, c, d, e, f, g, ...) e; _args_for_unbind_28(__VA_ARGS__)
69 #define _args_for_unbind_42(a, b, c, d, e, f, g, ...) e; _args_for_unbind_35(__VA_ARGS__)
70 #define _args_for_unbind_49(a, b, c, d, e, f, g, ...) e; _args_for_unbind_42(__VA_ARGS__)
71 #define _args_for_unbind_56(a, b, c, d, e, f, g, ...) e; _args_for_unbind_49(__VA_ARGS__)
72 #define _args_for_unbind_63(a, b, c, d, e, f, g, ...) e; _args_for_unbind_56(__VA_ARGS__)
73 #define _args_for_unbind_70(a, b, c, d, e, f, g, ...) e; _args_for_unbind_63(__VA_ARGS__)
74 #define _args_for_unbind_77(a, b, c, d, e, f, g, ...) e; _args_for_unbind_70(__VA_ARGS__)
75 
76 #define _EFUNC_(n) _args_for_unbind_##n
77 #define _EFUNC(n) _EFUNC_(n)
78 
79 #define ARGS_FOR_UNBIND(...) _EFUNC(__NARG__(__VA_ARGS__)) (__VA_ARGS__)
80 
81 #define _args_for_call_1(a) a
82 #define _args_for_call_7(a, b, c, d, e, f, g) ,d
83 #define _args_for_call_14(a, b, c, d, e, f, g, ...) ,d _args_for_call_7(__VA_ARGS__)
84 #define _args_for_call_21(a, b, c, d, e, f, g, ...) ,d _args_for_call_14(__VA_ARGS__)
85 #define _args_for_call_28(a, b, c, d, e, f, g, ...) ,d _args_for_call_21(__VA_ARGS__)
86 #define _args_for_call_35(a, b, c, d, e, f, g, ...) ,d _args_for_call_28(__VA_ARGS__)
87 #define _args_for_call_42(a, b, c, d, e, f, g, ...) ,d _args_for_call_35(__VA_ARGS__)
88 #define _args_for_call_49(a, b, c, d, e, f, g, ...) ,d _args_for_call_42(__VA_ARGS__)
89 #define _args_for_call_56(a, b, c, d, e, f, g, ...) ,d _args_for_call_49(__VA_ARGS__)
90 #define _args_for_call_63(a, b, c, d, e, f, g, ...) ,d _args_for_call_56(__VA_ARGS__)
91 #define _args_for_call_70(a, b, c, d, e, f, g, ...) ,d _args_for_call_63(__VA_ARGS__)
92 #define _args_for_call_77(a, b, c, d, e, f, g, ...) ,d _args_for_call_70(__VA_ARGS__)
93 
94 #define _DFUNC_(n) _args_for_call_##n
95 #define _DFUNC(n) _DFUNC_(n)
96 
97 #define ARGS_FOR_CALL(...) _DFUNC(__NARG__(__VA_ARGS__)) (__VA_ARGS__)
98 
99 #define _args_for_decl_1(a) a
100 #define _args_for_decl_7(a, b, c, d, e, f, g) ,c
101 #define _args_for_decl_14(a, b, c, d, e, f, g, ...) ,c _args_for_decl_7(__VA_ARGS__)
102 #define _args_for_decl_21(a, b, c, d, e, f, g, ...) ,c _args_for_decl_14(__VA_ARGS__)
103 #define _args_for_decl_28(a, b, c, d, e, f, g, ...) ,c _args_for_decl_21(__VA_ARGS__)
104 #define _args_for_decl_35(a, b, c, d, e, f, g, ...) ,c _args_for_decl_28(__VA_ARGS__)
105 #define _args_for_decl_42(a, b, c, d, e, f, g, ...) ,c _args_for_decl_35(__VA_ARGS__)
106 #define _args_for_decl_49(a, b, c, d, e, f, g, ...) ,c _args_for_decl_42(__VA_ARGS__)
107 #define _args_for_decl_56(a, b, c, d, e, f, g, ...) ,c _args_for_decl_49(__VA_ARGS__)
108 #define _args_for_decl_63(a, b, c, d, e, f, g, ...) ,c _args_for_decl_56(__VA_ARGS__)
109 #define _args_for_decl_70(a, b, c, d, e, f, g, ...) ,c _args_for_decl_63(__VA_ARGS__)
110 #define _args_for_decl_77(a, b, c, d, e, f, g, ...) ,c _args_for_decl_70(__VA_ARGS__)
111 
112 #define _CFUNC_(n) _args_for_decl_##n
113 #define _CFUNC(n) _CFUNC_(n)
114 
115 #define ARGS_FOR_DECLARATION(...) _CFUNC(__NARG__(__VA_ARGS__)) (__VA_ARGS__)
116 
117 #define _args_for_assign_1(a) a
118 #define _args_for_assign_7(a, b, c, d, e, f, g) b;
119 #define _args_for_assign_14(a, b, c, d, e, f, g, ...) b; _args_for_assign_7(__VA_ARGS__)
120 #define _args_for_assign_21(a, b, c, d, e, f, g, ...) b; _args_for_assign_14(__VA_ARGS__)
121 #define _args_for_assign_28(a, b, c, d, e, f, g, ...) b; _args_for_assign_21(__VA_ARGS__)
122 #define _args_for_assign_35(a, b, c, d, e, f, g, ...) b; _args_for_assign_28(__VA_ARGS__)
123 #define _args_for_assign_42(a, b, c, d, e, f, g, ...) b; _args_for_assign_35(__VA_ARGS__)
124 #define _args_for_assign_49(a, b, c, d, e, f, g, ...) b; _args_for_assign_42(__VA_ARGS__)
125 #define _args_for_assign_56(a, b, c, d, e, f, g, ...) b; _args_for_assign_49(__VA_ARGS__)
126 #define _args_for_assign_63(a, b, c, d, e, f, g, ...) b; _args_for_assign_56(__VA_ARGS__)
127 #define _args_for_assign_70(a, b, c, d, e, f, g, ...) b; _args_for_assign_63(__VA_ARGS__)
128 #define _args_for_assign_77(a, b, c, d, e, f, g, ...) b; _args_for_assign_70(__VA_ARGS__)
129 
130 #define _BFUNC_(n) _args_for_assign_##n
131 #define _BFUNC(n) _BFUNC_(n)
132 
133 #define ARGS_FOR_ASSIGN(...) _BFUNC(__NARG__(__VA_ARGS__)) (__VA_ARGS__)
134 
135 #define _args_for_struct_1(a) a;
136 #define _args_for_struct_7(a, b, c, d, e, f, g) a;
137 #define _args_for_struct_14(a, b, c, d, e, f, g, ...) a; _args_for_struct_7(__VA_ARGS__)
138 #define _args_for_struct_21(a, b, c, d, e, f, g, ...) a; _args_for_struct_14(__VA_ARGS__)
139 #define _args_for_struct_28(a, b, c, d, e, f, g, ...) a; _args_for_struct_21(__VA_ARGS__)
140 #define _args_for_struct_35(a, b, c, d, e, f, g, ...) a; _args_for_struct_28(__VA_ARGS__)
141 #define _args_for_struct_42(a, b, c, d, e, f, g, ...) a; _args_for_struct_35(__VA_ARGS__)
142 #define _args_for_struct_49(a, b, c, d, e, f, g, ...) a; _args_for_struct_42(__VA_ARGS__)
143 #define _args_for_struct_56(a, b, c, d, e, f, g, ...) a; _args_for_struct_49(__VA_ARGS__)
144 #define _args_for_struct_63(a, b, c, d, e, f, g, ...) a; _args_for_struct_56(__VA_ARGS__)
145 #define _args_for_struct_70(a, b, c, d, e, f, g, ...) a; _args_for_struct_63(__VA_ARGS__)
146 #define _args_for_struct_77(a, b, c, d, e, f, g, ...) a; _args_for_struct_70(__VA_ARGS__)
147 
148 #define _AFUNC_(n) _args_for_struct_##n
149 #define _AFUNC(n) _AFUNC_(n)
150 
151 #define ARGS_FOR_STRUCT(...) _AFUNC(__NARG__(__VA_ARGS__)) (__VA_ARGS__)
152 
153 /* Serialization and deserialization */
154 
155 #define CSMT_ITEM_NO_WAIT(name, ...) \
156 \
157 struct s_##name##_private { \
158     struct csmt_instruction instr; \
159     ARGS_FOR_STRUCT( __VA_ARGS__ ) \
160 }; \
161 \
162 static void \
163 name##_priv( struct NineDevice9 *device ARGS_FOR_DECLARATION( __VA_ARGS__ ) ); \
164 \
165 static int \
166 name##_rx( struct NineDevice9 *device, struct csmt_instruction *instr ) \
167 { \
168     struct csmt_context *ctx = device->csmt_ctx; \
169     struct s_##name##_private *args = (struct s_##name##_private *)instr; \
170     \
171     (void) args; \
172     (void) ctx; \
173     name##_priv( \
174         device ARGS_FOR_CALL( __VA_ARGS__ ) \
175     ); \
176     ARGS_FOR_UNBIND( __VA_ARGS__ ) \
177     return 0; \
178 } \
179 \
180 void \
181 name( struct NineDevice9 *device ARGS_FOR_DECLARATION( __VA_ARGS__ ) ) \
182 { \
183     struct csmt_context *ctx = device->csmt_ctx; \
184     struct s_##name##_private *args; \
185     unsigned memsize = sizeof(struct s_##name##_private); \
186     unsigned memsize2 = 0; \
187     \
188     if (!device->csmt_active) { \
189         name##_priv( \
190             device ARGS_FOR_BYPASS( __VA_ARGS__ ) \
191         ); \
192         return; \
193     } \
194     ARGS_FOR_MEM ( __VA_ARGS__ ) \
195     args = nine_queue_alloc(ctx->pool, memsize + memsize2); \
196     assert(args); \
197     args->instr.func = &name##_rx; \
198     ARGS_FOR_ASSIGN( __VA_ARGS__ ) \
199 } \
200 \
201 static void \
202 name##_priv( struct NineDevice9 *device ARGS_FOR_DECLARATION( __VA_ARGS__ ) )
203 
204 #define CSMT_ITEM_NO_WAIT_WITH_COUNTER(name, ...) \
205 \
206 struct s_##name##_private { \
207     struct csmt_instruction instr; \
208     unsigned *counter; \
209     ARGS_FOR_STRUCT( __VA_ARGS__ ) \
210 }; \
211 \
212 static void \
213 name##_priv( struct NineDevice9 *device ARGS_FOR_DECLARATION( __VA_ARGS__ ) ); \
214 \
215 static int \
216 name##_rx( struct NineDevice9 *device, struct csmt_instruction *instr ) \
217 { \
218     struct csmt_context *ctx = device->csmt_ctx; \
219     struct s_##name##_private *args = (struct s_##name##_private *)instr; \
220     \
221     (void) args; \
222     (void) ctx; \
223     name##_priv( \
224         device ARGS_FOR_CALL( __VA_ARGS__ ) \
225     ); \
226     p_atomic_dec(args->counter); \
227     ARGS_FOR_UNBIND( __VA_ARGS__ ) \
228     return 0; \
229 } \
230 \
231 void \
232 name( struct NineDevice9 *device, unsigned *counter ARGS_FOR_DECLARATION( __VA_ARGS__ ) ) \
233 { \
234     struct csmt_context *ctx = device->csmt_ctx; \
235     struct s_##name##_private *args; \
236     unsigned memsize = sizeof(struct s_##name##_private); \
237     unsigned memsize2 = 0; \
238     \
239     if (!device->csmt_active) { \
240         name##_priv( \
241             device ARGS_FOR_BYPASS( __VA_ARGS__ ) \
242         ); \
243         return; \
244     } \
245     assert(counter); \
246     p_atomic_inc(counter); \
247     ARGS_FOR_MEM ( __VA_ARGS__ ) \
248     args = nine_queue_alloc(ctx->pool, memsize + memsize2); \
249     assert(args); \
250     args->instr.func = &name##_rx; \
251     args->counter = counter; \
252     ARGS_FOR_ASSIGN( __VA_ARGS__ ) \
253 } \
254 \
255 static void \
256 name##_priv( struct NineDevice9 *device ARGS_FOR_DECLARATION( __VA_ARGS__ ) )
257 
258 #define CSMT_ITEM_DO_WAIT(name, ...) \
259 \
260 struct s_##name##_private { \
261     struct csmt_instruction instr; \
262     ARGS_FOR_STRUCT( __VA_ARGS__ ) \
263 }; \
264 static void \
265 name##_priv( struct NineDevice9 *device ARGS_FOR_DECLARATION( __VA_ARGS__ ) ); \
266 \
267 static int \
268 name##_rx( struct NineDevice9 *device, struct csmt_instruction *instr) \
269 { \
270     struct csmt_context *ctx = device->csmt_ctx; \
271     struct s_##name##_private *args = (struct s_##name##_private *)instr; \
272     \
273     (void) args; \
274     (void) ctx; \
275     name##_priv( \
276         device ARGS_FOR_CALL( __VA_ARGS__ ) \
277     ); \
278     ARGS_FOR_UNBIND( __VA_ARGS__ ) \
279     return 1; \
280 } \
281 \
282 void \
283 name( struct NineDevice9 *device ARGS_FOR_DECLARATION( __VA_ARGS__ ) ) \
284 { \
285     struct csmt_context *ctx = device->csmt_ctx; \
286     struct s_##name##_private *args; \
287     unsigned memsize = sizeof(struct s_##name##_private); \
288     unsigned memsize2 = 0; \
289     \
290     if (!device->csmt_active) { \
291         name##_priv( \
292             device ARGS_FOR_BYPASS( __VA_ARGS__ ) \
293         ); \
294         return; \
295     } \
296     ARGS_FOR_MEM ( __VA_ARGS__ ) \
297     args = nine_queue_alloc(ctx->pool, memsize + memsize2); \
298     assert(args); \
299     args->instr.func = &name##_rx; \
300     ARGS_FOR_ASSIGN( __VA_ARGS__ ) \
301     ctx->processed = false; \
302     nine_queue_flush(ctx->pool); \
303     nine_csmt_wait_processed(ctx); \
304 } \
305 \
306 static void \
307 name##_priv( struct NineDevice9 *device ARGS_FOR_DECLARATION( __VA_ARGS__ ) )
308 
309 /* ARGS_FOR_STRUCT, ARGS_FOR_ASSIGN, ARGS_FOR_DECLARATION, ARGS_FOR_CALL, ARGS_FOR_UNBIND, ARGS_FOR_MEM, ARGS_FOR_BYPASS */
310 #define ARG_VAL(x, y) \
311         x _##y ; ,\
312         args->_##y = y ; ,\
313         x y ,\
314         args->_##y ,\
315         ,\
316         ,\
317         y
318 
319 #define ARG_REF(x, y) \
320         x* _##y ; ,\
321         args->_##y = y; ,\
322         x *y ,\
323         args->_##y ,\
324         ,\
325         ,\
326         y
327 
328 #define ARG_COPY_REF(x, y) \
329         x * _##y ; x __##y ; ,\
330         if ( y ) { args->_##y = &args->__##y ; args->__##y = *y ; } else { args->_##y = NULL; } ,\
331         const x *y ,\
332         (const x *)args->_##y ,\
333         ,\
334         ,\
335         (const x *)y
336 
337 #define ARG_BIND_REF(x, y) \
338         x * _##y ,\
339         if ( y ) \
340             NineUnknown_Bind( (void *)y ); \
341         args->_##y = y ; ,\
342         x *y ,\
343         args->_##y,\
344         if (args->_##y) \
345             NineUnknown_Unbind((void *)(args->_##y)); \
346         args->_##y = NULL; ,\
347         ,\
348         y
349 
350 #define ARG_BIND_RES(x, y) \
351         x * _##y ,\
352         args->_##y = NULL; \
353         if (y) \
354             pipe_resource_reference(&args->_##y, y); ,\
355         x *y ,\
356         args->_##y ,\
357         if (args->_##y) \
358             pipe_resource_reference(&args->_##y, NULL); ,\
359         ,\
360         y
361 
362 #define ARG_MEM(x, y) \
363         x * _##y ,\
364         args->_##y = (void *)args + memsize;\
365         memcpy(args->_##y, y, memsize2); ,\
366         const x *y ,\
367         (const x *)args->_##y ,\
368         ,\
369         ,\
370         (const x *)y
371 
372 #define ARG_MEM_SIZE(x, y) \
373         x _##y ,\
374         args->_##y = y; ,\
375         x y ,\
376         args->_##y ,\
377         ,\
378         memsize2 = y, \
379         y
380 
381 #define ARG_BIND_BLIT(x, y) \
382         x _##y ,\
383         memcpy(&args->_##y , y, sizeof(x)); \
384         args->_##y.src.resource = NULL; \
385         args->_##y.dst.resource = NULL; \
386         pipe_resource_reference(&args->_##y.src.resource, y->src.resource); \
387         pipe_resource_reference(&args->_##y.dst.resource, y->dst.resource);,\
388         x *y ,\
389         &args->_##y ,\
390         pipe_resource_reference(&args->_##y.src.resource, NULL); \
391         pipe_resource_reference(&args->_##y.dst.resource, NULL);,\
392         ,\
393         y
394 
395 #define ARG_BIND_VBUF(x, y) \
396         x _##y ,\
397         memcpy(&args->_##y , y, sizeof(x)); \
398         args->_##y.buffer.resource = NULL; \
399         pipe_resource_reference(&args->_##y.buffer.resource, y->buffer.resource); ,\
400         x *y ,\
401         &args->_##y ,\
402         pipe_resource_reference(&args->_##y.buffer.resource, NULL); ,\
403         ,\
404         y
405 
406 #define ARG_BIND_IBUF(x, y) \
407         x _##y ,\
408         memcpy(&args->_##y , y, sizeof(x)); \
409         args->_##y.buffer = NULL; \
410         pipe_resource_reference(&args->_##y.buffer, y->buffer); ,\
411         x *y ,\
412         &args->_##y ,\
413         pipe_resource_reference(&args->_##y.buffer, NULL); ,\
414         ,\
415         y
416 
417 #define ARG_BIND_VIEW(x, y) \
418         x * _##y ,\
419         args->_##y = NULL; \
420         if (y) \
421             pipe_sampler_view_reference(&args->_##y, y); ,\
422         x *y ,\
423         args->_##y ,\
424         if (args->_##y) \
425             pipe_sampler_view_reference(&args->_##y, NULL); ,\
426         ,\
427         y
428 
429