st/nine: Implement gallium nine CSMT
[mesa.git] / src / gallium / state_trackers / nine / nine_csmt_helper.h
1 /*
2 * Copyright 2016 Patrick Rudolph <siro@das-labor.org>
3 *
4 * Permission is hereby granted, free of charge, f, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, f, copy, modify, merge, f, publish, distribute, f, sub
8 * license, f, and/or sell copies of the Software, f, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISe, f, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23 /* get number of arguments with __NARG__ */
24 #define __NARG__(...) __NARG_I_(__VA_ARGS__,__RSEQ_N())
25 #define __NARG_I_(...) __ARG_N(__VA_ARGS__)
26 #define __ARG_N( \
27 _1, _2, _3, _4, _5, _6, _7, _8, _9,_10, \
28 _11,_12,_13,_14,_15,_16,_17,_18,_19,_20, \
29 _21,_22,_23,_24,_25,_26,_27,_28,_29,_30, \
30 _31,_32,_33,_34,_35,_36,_37,_38,_39,_40, \
31 _41,_42,_43,_44,_45,_46,_47,_48,_49,_50, \
32 _51,_52,_53,_54,_55,_56,_57,_58,_59,_60, \
33 _61,_62,_63,N,...) N
34 #define __RSEQ_N() \
35 63,62,61,60, \
36 59,58,57,56,55,54,53,52,51,50, \
37 49,48,47,46,45,44,43,42,41,40, \
38 39,38,37,36,35,34,33,32,31,30, \
39 29,28,27,26,25,24,23,22,21,20, \
40 19,18,17,16,15,14,13,12,11,10, \
41 9,8,7,6,5,4,3,2,1,0
42
43
44 #define _args_for_bypass_1(a) a
45 #define _args_for_bypass_7(a, b, c, d, e, f, g) ,g
46 #define _args_for_bypass_14(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_7(__VA_ARGS__)
47 #define _args_for_bypass_21(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_14(__VA_ARGS__)
48 #define _args_for_bypass_28(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_21(__VA_ARGS__)
49 #define _args_for_bypass_35(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_28(__VA_ARGS__)
50 #define _args_for_bypass_42(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_35(__VA_ARGS__)
51 #define _args_for_bypass_49(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_42(__VA_ARGS__)
52 #define _args_for_bypass_56(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_49(__VA_ARGS__)
53 #define _args_for_bypass_63(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_56(__VA_ARGS__)
54 #define _args_for_bypass_70(a, b, c, d, e, f, g, ...) ,g _args_for_bypass_63(__VA_ARGS__)
55
56 #define _GFUNC_(n) _args_for_bypass_##n
57 #define _GFUNC(n) _GFUNC_(n)
58
59 #define ARGS_FOR_BYPASS(...) _GFUNC(__NARG__(__VA_ARGS__)) (__VA_ARGS__)
60
61 #define _args_for_mem_1(a) a;
62 #define _args_for_mem_7(a, b, c, d, e, f, g) f;
63 #define _args_for_mem_14(a, b, c, d, e, f, g, ...) f; _args_for_mem_7(__VA_ARGS__)
64 #define _args_for_mem_21(a, b, c, d, e, f, g, ...) f; _args_for_mem_14(__VA_ARGS__)
65 #define _args_for_mem_28(a, b, c, d, e, f, g, ...) f; _args_for_mem_21(__VA_ARGS__)
66 #define _args_for_mem_35(a, b, c, d, e, f, g, ...) f; _args_for_mem_28(__VA_ARGS__)
67 #define _args_for_mem_42(a, b, c, d, e, f, g, ...) f; _args_for_mem_35(__VA_ARGS__)
68 #define _args_for_mem_49(a, b, c, d, e, f, g, ...) f; _args_for_mem_42(__VA_ARGS__)
69 #define _args_for_mem_56(a, b, c, d, e, f, g, ...) f; _args_for_mem_49(__VA_ARGS__)
70 #define _args_for_mem_63(a, b, c, d, e, f, g, ...) f; _args_for_mem_56(__VA_ARGS__)
71 #define _args_for_mem_70(a, b, c, d, e, f, g, ...) f; _args_for_mem_63(__VA_ARGS__)
72
73 #define _FFUNC_(n) _args_for_mem_##n
74 #define _FFUNC(n) _FFUNC_(n)
75
76 #define ARGS_FOR_MEM(...) _FFUNC(__NARG__(__VA_ARGS__)) (__VA_ARGS__)
77
78 #define _args_for_unbind_1(a) a;
79 #define _args_for_unbind_7(a, b, c, d, e, f, g) e;
80 #define _args_for_unbind_14(a, b, c, d, e, f, g, ...) e; _args_for_unbind_7(__VA_ARGS__)
81 #define _args_for_unbind_21(a, b, c, d, e, f, g, ...) e; _args_for_unbind_14(__VA_ARGS__)
82 #define _args_for_unbind_28(a, b, c, d, e, f, g, ...) e; _args_for_unbind_21(__VA_ARGS__)
83 #define _args_for_unbind_35(a, b, c, d, e, f, g, ...) e; _args_for_unbind_28(__VA_ARGS__)
84 #define _args_for_unbind_42(a, b, c, d, e, f, g, ...) e; _args_for_unbind_35(__VA_ARGS__)
85 #define _args_for_unbind_49(a, b, c, d, e, f, g, ...) e; _args_for_unbind_42(__VA_ARGS__)
86 #define _args_for_unbind_56(a, b, c, d, e, f, g, ...) e; _args_for_unbind_49(__VA_ARGS__)
87 #define _args_for_unbind_63(a, b, c, d, e, f, g, ...) e; _args_for_unbind_56(__VA_ARGS__)
88 #define _args_for_unbind_70(a, b, c, d, e, f, g, ...) e; _args_for_unbind_63(__VA_ARGS__)
89
90 #define _EFUNC_(n) _args_for_unbind_##n
91 #define _EFUNC(n) _EFUNC_(n)
92
93 #define ARGS_FOR_UNBIND(...) _EFUNC(__NARG__(__VA_ARGS__)) (__VA_ARGS__)
94
95 #define _args_for_call_1(a) a
96 #define _args_for_call_7(a, b, c, d, e, f, g) ,d
97 #define _args_for_call_14(a, b, c, d, e, f, g, ...) ,d _args_for_call_7(__VA_ARGS__)
98 #define _args_for_call_21(a, b, c, d, e, f, g, ...) ,d _args_for_call_14(__VA_ARGS__)
99 #define _args_for_call_28(a, b, c, d, e, f, g, ...) ,d _args_for_call_21(__VA_ARGS__)
100 #define _args_for_call_35(a, b, c, d, e, f, g, ...) ,d _args_for_call_28(__VA_ARGS__)
101 #define _args_for_call_42(a, b, c, d, e, f, g, ...) ,d _args_for_call_35(__VA_ARGS__)
102 #define _args_for_call_49(a, b, c, d, e, f, g, ...) ,d _args_for_call_42(__VA_ARGS__)
103 #define _args_for_call_56(a, b, c, d, e, f, g, ...) ,d _args_for_call_49(__VA_ARGS__)
104 #define _args_for_call_63(a, b, c, d, e, f, g, ...) ,d _args_for_call_56(__VA_ARGS__)
105 #define _args_for_call_70(a, b, c, d, e, f, g, ...) ,d _args_for_call_63(__VA_ARGS__)
106
107 #define _DFUNC_(n) _args_for_call_##n
108 #define _DFUNC(n) _DFUNC_(n)
109
110 #define ARGS_FOR_CALL(...) _DFUNC(__NARG__(__VA_ARGS__)) (__VA_ARGS__)
111
112 #define _args_for_decl_1(a) a
113 #define _args_for_decl_7(a, b, c, d, e, f, g) ,c
114 #define _args_for_decl_14(a, b, c, d, e, f, g, ...) ,c _args_for_decl_7(__VA_ARGS__)
115 #define _args_for_decl_21(a, b, c, d, e, f, g, ...) ,c _args_for_decl_14(__VA_ARGS__)
116 #define _args_for_decl_28(a, b, c, d, e, f, g, ...) ,c _args_for_decl_21(__VA_ARGS__)
117 #define _args_for_decl_35(a, b, c, d, e, f, g, ...) ,c _args_for_decl_28(__VA_ARGS__)
118 #define _args_for_decl_42(a, b, c, d, e, f, g, ...) ,c _args_for_decl_35(__VA_ARGS__)
119 #define _args_for_decl_49(a, b, c, d, e, f, g, ...) ,c _args_for_decl_42(__VA_ARGS__)
120 #define _args_for_decl_56(a, b, c, d, e, f, g, ...) ,c _args_for_decl_49(__VA_ARGS__)
121 #define _args_for_decl_63(a, b, c, d, e, f, g, ...) ,c _args_for_decl_56(__VA_ARGS__)
122 #define _args_for_decl_70(a, b, c, d, e, f, g, ...) ,c _args_for_decl_63(__VA_ARGS__)
123
124 #define _CFUNC_(n) _args_for_decl_##n
125 #define _CFUNC(n) _CFUNC_(n)
126
127 #define ARGS_FOR_DECLARATION(...) _CFUNC(__NARG__(__VA_ARGS__)) (__VA_ARGS__)
128
129 #define _args_for_assign_1(a) a
130 #define _args_for_assign_7(a, b, c, d, e, f, g) b;
131 #define _args_for_assign_14(a, b, c, d, e, f, g, ...) b; _args_for_assign_7(__VA_ARGS__)
132 #define _args_for_assign_21(a, b, c, d, e, f, g, ...) b; _args_for_assign_14(__VA_ARGS__)
133 #define _args_for_assign_28(a, b, c, d, e, f, g, ...) b; _args_for_assign_21(__VA_ARGS__)
134 #define _args_for_assign_35(a, b, c, d, e, f, g, ...) b; _args_for_assign_28(__VA_ARGS__)
135 #define _args_for_assign_42(a, b, c, d, e, f, g, ...) b; _args_for_assign_35(__VA_ARGS__)
136 #define _args_for_assign_49(a, b, c, d, e, f, g, ...) b; _args_for_assign_42(__VA_ARGS__)
137 #define _args_for_assign_56(a, b, c, d, e, f, g, ...) b; _args_for_assign_49(__VA_ARGS__)
138 #define _args_for_assign_63(a, b, c, d, e, f, g, ...) b; _args_for_assign_56(__VA_ARGS__)
139 #define _args_for_assign_70(a, b, c, d, e, f, g, ...) b; _args_for_assign_63(__VA_ARGS__)
140
141 #define _BFUNC_(n) _args_for_assign_##n
142 #define _BFUNC(n) _BFUNC_(n)
143
144 #define ARGS_FOR_ASSIGN(...) _BFUNC(__NARG__(__VA_ARGS__)) (__VA_ARGS__)
145
146 #define _args_for_struct_1(a) a;
147 #define _args_for_struct_7(a, b, c, d, e, f, g) a;
148 #define _args_for_struct_14(a, b, c, d, e, f, g, ...) a; _args_for_struct_7(__VA_ARGS__)
149 #define _args_for_struct_21(a, b, c, d, e, f, g, ...) a; _args_for_struct_14(__VA_ARGS__)
150 #define _args_for_struct_28(a, b, c, d, e, f, g, ...) a; _args_for_struct_21(__VA_ARGS__)
151 #define _args_for_struct_35(a, b, c, d, e, f, g, ...) a; _args_for_struct_28(__VA_ARGS__)
152 #define _args_for_struct_42(a, b, c, d, e, f, g, ...) a; _args_for_struct_35(__VA_ARGS__)
153 #define _args_for_struct_49(a, b, c, d, e, f, g, ...) a; _args_for_struct_42(__VA_ARGS__)
154 #define _args_for_struct_56(a, b, c, d, e, f, g, ...) a; _args_for_struct_49(__VA_ARGS__)
155 #define _args_for_struct_63(a, b, c, d, e, f, g, ...) a; _args_for_struct_56(__VA_ARGS__)
156 #define _args_for_struct_70(a, b, c, d, e, f, g, ...) a; _args_for_struct_63(__VA_ARGS__)
157
158 #define _AFUNC_(n) _args_for_struct_##n
159 #define _AFUNC(n) _AFUNC_(n)
160
161 #define ARGS_FOR_STRUCT(...) _AFUNC(__NARG__(__VA_ARGS__)) (__VA_ARGS__)
162
163 /* Serialization and deserialization */
164
165 #define CSMT_ITEM_NO_WAIT(name, ...) \
166 \
167 struct s_##name##_private { \
168 struct csmt_instruction instr; \
169 ARGS_FOR_STRUCT( __VA_ARGS__ ) \
170 }; \
171 \
172 static void \
173 name##_priv( struct NineDevice9 *device ARGS_FOR_DECLARATION( __VA_ARGS__ ) ); \
174 \
175 static int \
176 name##_rx( struct NineDevice9 *device, struct csmt_instruction *instr ) \
177 { \
178 struct csmt_context *ctx = device->csmt_ctx; \
179 struct s_##name##_private *args = (struct s_##name##_private *)instr; \
180 \
181 (void) args; \
182 (void) ctx; \
183 name##_priv( \
184 device ARGS_FOR_CALL( __VA_ARGS__ ) \
185 ); \
186 ARGS_FOR_UNBIND( __VA_ARGS__ ) \
187 return 0; \
188 } \
189 \
190 void \
191 name( struct NineDevice9 *device ARGS_FOR_DECLARATION( __VA_ARGS__ ) ) \
192 { \
193 struct csmt_context *ctx = device->csmt_ctx; \
194 struct s_##name##_private *args; \
195 unsigned memsize = sizeof(struct s_##name##_private); \
196 unsigned memsize2 = 0; \
197 \
198 if (!device->csmt_active) { \
199 name##_priv( \
200 device ARGS_FOR_BYPASS( __VA_ARGS__ ) \
201 ); \
202 return; \
203 } \
204 ARGS_FOR_MEM ( __VA_ARGS__ ) \
205 args = nine_queue_alloc(ctx->pool, memsize + memsize2); \
206 assert(args); \
207 args->instr.func = &name##_rx; \
208 ARGS_FOR_ASSIGN( __VA_ARGS__ ) \
209 } \
210 \
211 static void \
212 name##_priv( struct NineDevice9 *device ARGS_FOR_DECLARATION( __VA_ARGS__ ) )
213
214 #define CSMT_ITEM_DO_WAIT(name, ...) \
215 \
216 struct s_##name##_private { \
217 struct csmt_instruction instr; \
218 ARGS_FOR_STRUCT( __VA_ARGS__ ) \
219 }; \
220 static void \
221 name##_priv( struct NineDevice9 *device ARGS_FOR_DECLARATION( __VA_ARGS__ ) ); \
222 \
223 static int \
224 name##_rx( struct NineDevice9 *device, struct csmt_instruction *instr) \
225 { \
226 struct csmt_context *ctx = device->csmt_ctx; \
227 struct s_##name##_private *args = (struct s_##name##_private *)instr; \
228 \
229 (void) args; \
230 (void) ctx; \
231 name##_priv( \
232 device ARGS_FOR_CALL( __VA_ARGS__ ) \
233 ); \
234 ARGS_FOR_UNBIND( __VA_ARGS__ ) \
235 return 1; \
236 } \
237 \
238 void \
239 name( struct NineDevice9 *device ARGS_FOR_DECLARATION( __VA_ARGS__ ) ) \
240 { \
241 struct csmt_context *ctx = device->csmt_ctx; \
242 struct s_##name##_private *args; \
243 unsigned memsize = sizeof(struct s_##name##_private); \
244 unsigned memsize2 = 0; \
245 \
246 if (!device->csmt_active) { \
247 name##_priv( \
248 device ARGS_FOR_BYPASS( __VA_ARGS__ ) \
249 ); \
250 return; \
251 } \
252 ARGS_FOR_MEM ( __VA_ARGS__ ) \
253 args = nine_queue_alloc(ctx->pool, memsize + memsize2); \
254 assert(args); \
255 args->instr.func = &name##_rx; \
256 ARGS_FOR_ASSIGN( __VA_ARGS__ ) \
257 ctx->processed = FALSE; \
258 nine_queue_flush(ctx->pool); \
259 nine_csmt_wait_processed(ctx); \
260 } \
261 \
262 static void \
263 name##_priv( struct NineDevice9 *device ARGS_FOR_DECLARATION( __VA_ARGS__ ) )
264
265 /* ARGS_FOR_STRUCT, ARGS_FOR_ASSIGN, ARGS_FOR_DECLARATION, ARGS_FOR_CALL, ARGS_FOR_UNBIND, ARGS_FOR_MEM, ARGS_FOR_BYPASS */
266 #define ARG_VAL(x, y) \
267 x _##y ; ,\
268 args->_##y = y ; ,\
269 x y ,\
270 args->_##y ,\
271 ,\
272 ,\
273 y
274
275 #define ARG_REF(x, y) \
276 x* _##y ; ,\
277 args->_##y = y; ,\
278 x *y ,\
279 args->_##y ,\
280 ,\
281 ,\
282 y
283
284 #define ARG_COPY_REF(x, y) \
285 x * _##y ; x __##y ; ,\
286 if ( y ) { args->_##y = &args->__##y ; args->__##y = *y ; } else { args->_##y = NULL; } ,\
287 const x *y ,\
288 (const x *)args->_##y ,\
289 ,\
290 ,\
291 (const x *)y
292
293 #define ARG_BIND_REF(x, y) \
294 x * _##y ,\
295 args->_##y = NULL; \
296 if (args->_##y != y && args->_##y) \
297 NineUnknown_Unbind((void *)(args->_##y)); \
298 if ( args->_##y != y && y ) \
299 NineUnknown_Bind( (void *)y ); \
300 if ( args->_##y != y ) \
301 args->_##y = y ; ,\
302 x *y ,\
303 args->_##y,\
304 if (args->_##y != NULL && args->_##y) \
305 NineUnknown_Unbind((void *)(args->_##y)); \
306 args->_##y = NULL; ,\
307 ,\
308 y
309
310 #define ARG_BIND_RES(x, y) \
311 x * _##y ,\
312 args->_##y = NULL; \
313 if (y) \
314 pipe_resource_reference(&args->_##y, y); ,\
315 x *y ,\
316 args->_##y ,\
317 if (args->_##y) \
318 pipe_resource_reference(&args->_##y, NULL); ,\
319 ,\
320 y
321
322 #define ARG_MEM(x, y) \
323 x * _##y ,\
324 args->_##y = (void *)args + memsize;\
325 memcpy(args->_##y, y, memsize2); ,\
326 const x *y ,\
327 (const x *)args->_##y ,\
328 ,\
329 ,\
330 (const x *)y
331
332 #define ARG_MEM_SIZE(x, y) \
333 x _##y ,\
334 args->_##y = y; ,\
335 x y ,\
336 args->_##y ,\
337 ,\
338 memsize2 = y, \
339 y
340
341 #define ARG_BIND_BLIT(x, y) \
342 x _##y ,\
343 memcpy(&args->_##y , y, sizeof(x)); \
344 args->_##y.src.resource = NULL; \
345 args->_##y.dst.resource = NULL; \
346 pipe_resource_reference(&args->_##y.src.resource, y->src.resource); \
347 pipe_resource_reference(&args->_##y.dst.resource, y->dst.resource);,\
348 x *y ,\
349 &args->_##y ,\
350 pipe_resource_reference(&args->_##y.src.resource, NULL); \
351 pipe_resource_reference(&args->_##y.dst.resource, NULL);,\
352 ,\
353 y
354
355 #define ARG_BIND_BUF(x, y) \
356 x _##y ,\
357 memcpy(&args->_##y , y, sizeof(x)); \
358 args->_##y.buffer = NULL; \
359 pipe_resource_reference(&args->_##y.buffer, y->buffer); ,\
360 x *y ,\
361 &args->_##y ,\
362 pipe_resource_reference(&args->_##y.buffer, NULL); ,\
363 ,\
364 y
365
366 #define ARG_BIND_VIEW(x, y) \
367 x * _##y ,\
368 args->_##y = NULL; \
369 if (y) \
370 pipe_sampler_view_reference(&args->_##y, y); ,\
371 x *y ,\
372 args->_##y ,\
373 if (args->_##y) \
374 pipe_sampler_view_reference(&args->_##y, NULL); ,\
375 ,\
376 y
377