1 /**************************************************************************
3 * Copyright 2019 Red Hat.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 **************************************************************************/
25 #include "util/u_memory.h"
26 #include "util/simple_list.h"
27 #include "util/os_time.h"
28 #include "util/u_dump.h"
29 #include "util/u_string.h"
30 #include "tgsi/tgsi_dump.h"
31 #include "tgsi/tgsi_parse.h"
32 #include "gallivm/lp_bld_const.h"
33 #include "gallivm/lp_bld_debug.h"
34 #include "gallivm/lp_bld_intr.h"
35 #include "gallivm/lp_bld_flow.h"
36 #include "gallivm/lp_bld_gather.h"
37 #include "gallivm/lp_bld_coro.h"
38 #include "gallivm/lp_bld_nir.h"
39 #include "lp_state_cs.h"
40 #include "lp_context.h"
44 #include "lp_screen.h"
45 #include "lp_memory.h"
46 #include "lp_cs_tpool.h"
47 #include "frontend/sw_winsys.h"
48 #include "nir/nir_to_tgsi_info.h"
49 #include "util/mesa-sha1.h"
50 #include "nir_serialize.h"
52 /** Fragment shader number (for debugging) */
53 static unsigned cs_no
= 0;
55 struct lp_cs_job_info
{
56 unsigned grid_size
[3];
57 unsigned block_size
[3];
58 unsigned req_local_mem
;
60 struct lp_cs_exec
*current
;
64 generate_compute(struct llvmpipe_context
*lp
,
65 struct lp_compute_shader
*shader
,
66 struct lp_compute_shader_variant
*variant
)
68 struct gallivm_state
*gallivm
= variant
->gallivm
;
69 const struct lp_compute_shader_variant_key
*key
= &variant
->key
;
70 char func_name
[64], func_name_coro
[64];
71 LLVMTypeRef arg_types
[17];
72 LLVMTypeRef func_type
, coro_func_type
;
73 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
74 LLVMValueRef context_ptr
;
75 LLVMValueRef x_size_arg
, y_size_arg
, z_size_arg
;
76 LLVMValueRef grid_x_arg
, grid_y_arg
, grid_z_arg
;
77 LLVMValueRef grid_size_x_arg
, grid_size_y_arg
, grid_size_z_arg
;
78 LLVMValueRef work_dim_arg
, thread_data_ptr
;
79 LLVMBasicBlockRef block
;
80 LLVMBuilderRef builder
;
81 struct lp_build_sampler_soa
*sampler
;
82 struct lp_build_image_soa
*image
;
83 LLVMValueRef function
, coro
;
84 struct lp_type cs_type
;
88 * This function has two parts
89 * a) setup the coroutine execution environment loop.
90 * b) build the compute shader llvm for use inside the coroutine.
92 assert(lp_native_vector_width
/ 32 >= 4);
94 memset(&cs_type
, 0, sizeof cs_type
);
95 cs_type
.floating
= TRUE
; /* floating point values */
96 cs_type
.sign
= TRUE
; /* values are signed */
97 cs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
98 cs_type
.width
= 32; /* 32-bit float */
99 cs_type
.length
= MIN2(lp_native_vector_width
/ 32, 16); /* n*4 elements per vector */
100 snprintf(func_name
, sizeof(func_name
), "cs_variant");
102 snprintf(func_name_coro
, sizeof(func_name
), "cs_co_variant");
104 arg_types
[0] = variant
->jit_cs_context_ptr_type
; /* context */
105 arg_types
[1] = int32_type
; /* block_x_size */
106 arg_types
[2] = int32_type
; /* block_y_size */
107 arg_types
[3] = int32_type
; /* block_z_size */
108 arg_types
[4] = int32_type
; /* grid_x */
109 arg_types
[5] = int32_type
; /* grid_y */
110 arg_types
[6] = int32_type
; /* grid_z */
111 arg_types
[7] = int32_type
; /* grid_size_x */
112 arg_types
[8] = int32_type
; /* grid_size_y */
113 arg_types
[9] = int32_type
; /* grid_size_z */
114 arg_types
[10] = int32_type
; /* work dim */
115 arg_types
[11] = variant
->jit_cs_thread_data_ptr_type
; /* per thread data */
116 arg_types
[12] = int32_type
; /* coro only - num X loops */
117 arg_types
[13] = int32_type
; /* coro only - partials */
118 arg_types
[14] = int32_type
; /* coro block_x_size */
119 arg_types
[15] = int32_type
; /* coro block_y_size */
120 arg_types
[16] = int32_type
; /* coro block_z_size */
121 func_type
= LLVMFunctionType(LLVMVoidTypeInContext(gallivm
->context
),
122 arg_types
, ARRAY_SIZE(arg_types
) - 5, 0);
124 coro_func_type
= LLVMFunctionType(LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0),
125 arg_types
, ARRAY_SIZE(arg_types
), 0);
127 function
= LLVMAddFunction(gallivm
->module
, func_name
, func_type
);
128 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
130 coro
= LLVMAddFunction(gallivm
->module
, func_name_coro
, coro_func_type
);
131 LLVMSetFunctionCallConv(coro
, LLVMCCallConv
);
133 variant
->function
= function
;
135 for(i
= 0; i
< ARRAY_SIZE(arg_types
); ++i
) {
136 if(LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
) {
137 lp_add_function_attr(coro
, i
+ 1, LP_FUNC_ATTR_NOALIAS
);
138 lp_add_function_attr(function
, i
+ 1, LP_FUNC_ATTR_NOALIAS
);
142 lp_build_coro_declare_malloc_hooks(gallivm
);
144 if (variant
->gallivm
->cache
->data_size
)
147 context_ptr
= LLVMGetParam(function
, 0);
148 x_size_arg
= LLVMGetParam(function
, 1);
149 y_size_arg
= LLVMGetParam(function
, 2);
150 z_size_arg
= LLVMGetParam(function
, 3);
151 grid_x_arg
= LLVMGetParam(function
, 4);
152 grid_y_arg
= LLVMGetParam(function
, 5);
153 grid_z_arg
= LLVMGetParam(function
, 6);
154 grid_size_x_arg
= LLVMGetParam(function
, 7);
155 grid_size_y_arg
= LLVMGetParam(function
, 8);
156 grid_size_z_arg
= LLVMGetParam(function
, 9);
157 work_dim_arg
= LLVMGetParam(function
, 10);
158 thread_data_ptr
= LLVMGetParam(function
, 11);
160 lp_build_name(context_ptr
, "context");
161 lp_build_name(x_size_arg
, "x_size");
162 lp_build_name(y_size_arg
, "y_size");
163 lp_build_name(z_size_arg
, "z_size");
164 lp_build_name(grid_x_arg
, "grid_x");
165 lp_build_name(grid_y_arg
, "grid_y");
166 lp_build_name(grid_z_arg
, "grid_z");
167 lp_build_name(grid_size_x_arg
, "grid_size_x");
168 lp_build_name(grid_size_y_arg
, "grid_size_y");
169 lp_build_name(grid_size_z_arg
, "grid_size_z");
170 lp_build_name(work_dim_arg
, "work_dim");
171 lp_build_name(thread_data_ptr
, "thread_data");
173 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, function
, "entry");
174 builder
= gallivm
->builder
;
176 LLVMPositionBuilderAtEnd(builder
, block
);
177 sampler
= lp_llvm_sampler_soa_create(key
->samplers
, key
->nr_samplers
);
178 image
= lp_llvm_image_soa_create(lp_cs_variant_key_images(key
), key
->nr_images
);
180 struct lp_build_loop_state loop_state
[4];
181 LLVMValueRef num_x_loop
;
182 LLVMValueRef vec_length
= lp_build_const_int32(gallivm
, cs_type
.length
);
183 num_x_loop
= LLVMBuildAdd(gallivm
->builder
, x_size_arg
, vec_length
, "");
184 num_x_loop
= LLVMBuildSub(gallivm
->builder
, num_x_loop
, lp_build_const_int32(gallivm
, 1), "");
185 num_x_loop
= LLVMBuildUDiv(gallivm
->builder
, num_x_loop
, vec_length
, "");
186 LLVMValueRef partials
= LLVMBuildURem(gallivm
->builder
, x_size_arg
, vec_length
, "");
188 LLVMValueRef coro_num_hdls
= LLVMBuildMul(gallivm
->builder
, num_x_loop
, y_size_arg
, "");
189 coro_num_hdls
= LLVMBuildMul(gallivm
->builder
, coro_num_hdls
, z_size_arg
, "");
191 LLVMTypeRef hdl_ptr_type
= LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
192 LLVMValueRef coro_hdls
= LLVMBuildArrayAlloca(gallivm
->builder
, hdl_ptr_type
, coro_num_hdls
, "coro_hdls");
194 unsigned end_coroutine
= INT_MAX
;
197 * This is the main coroutine execution loop. It iterates over the dimensions
198 * and calls the coroutine main entrypoint on the first pass, but in subsequent
199 * passes it checks if the coroutine has completed and resumes it if not.
201 /* take x_width - round up to type.length width */
202 lp_build_loop_begin(&loop_state
[3], gallivm
,
203 lp_build_const_int32(gallivm
, 0)); /* coroutine reentry loop */
204 lp_build_loop_begin(&loop_state
[2], gallivm
,
205 lp_build_const_int32(gallivm
, 0)); /* z loop */
206 lp_build_loop_begin(&loop_state
[1], gallivm
,
207 lp_build_const_int32(gallivm
, 0)); /* y loop */
208 lp_build_loop_begin(&loop_state
[0], gallivm
,
209 lp_build_const_int32(gallivm
, 0)); /* x loop */
211 LLVMValueRef args
[17];
212 args
[0] = context_ptr
;
213 args
[1] = loop_state
[0].counter
;
214 args
[2] = loop_state
[1].counter
;
215 args
[3] = loop_state
[2].counter
;
216 args
[4] = grid_x_arg
;
217 args
[5] = grid_y_arg
;
218 args
[6] = grid_z_arg
;
219 args
[7] = grid_size_x_arg
;
220 args
[8] = grid_size_y_arg
;
221 args
[9] = grid_size_z_arg
;
222 args
[10] = work_dim_arg
;
223 args
[11] = thread_data_ptr
;
224 args
[12] = num_x_loop
;
226 args
[14] = x_size_arg
;
227 args
[15] = y_size_arg
;
228 args
[16] = z_size_arg
;
230 /* idx = (z * (size_x * size_y) + y * size_x + x */
231 LLVMValueRef coro_hdl_idx
= LLVMBuildMul(gallivm
->builder
, loop_state
[2].counter
,
232 LLVMBuildMul(gallivm
->builder
, num_x_loop
, y_size_arg
, ""), "");
233 coro_hdl_idx
= LLVMBuildAdd(gallivm
->builder
, coro_hdl_idx
,
234 LLVMBuildMul(gallivm
->builder
, loop_state
[1].counter
,
235 num_x_loop
, ""), "");
236 coro_hdl_idx
= LLVMBuildAdd(gallivm
->builder
, coro_hdl_idx
,
237 loop_state
[0].counter
, "");
239 LLVMValueRef coro_entry
= LLVMBuildGEP(gallivm
->builder
, coro_hdls
, &coro_hdl_idx
, 1, "");
241 LLVMValueRef coro_hdl
= LLVMBuildLoad(gallivm
->builder
, coro_entry
, "coro_hdl");
243 struct lp_build_if_state ifstate
;
244 LLVMValueRef cmp
= LLVMBuildICmp(gallivm
->builder
, LLVMIntEQ
, loop_state
[3].counter
,
245 lp_build_const_int32(gallivm
, 0), "");
246 /* first time here - call the coroutine function entry point */
247 lp_build_if(&ifstate
, gallivm
, cmp
);
248 LLVMValueRef coro_ret
= LLVMBuildCall(gallivm
->builder
, coro
, args
, 17, "");
249 LLVMBuildStore(gallivm
->builder
, coro_ret
, coro_entry
);
250 lp_build_else(&ifstate
);
251 /* subsequent calls for this invocation - check if done. */
252 LLVMValueRef coro_done
= lp_build_coro_done(gallivm
, coro_hdl
);
253 struct lp_build_if_state ifstate2
;
254 lp_build_if(&ifstate2
, gallivm
, coro_done
);
255 /* if done destroy and force loop exit */
256 lp_build_coro_destroy(gallivm
, coro_hdl
);
257 lp_build_loop_force_set_counter(&loop_state
[3], lp_build_const_int32(gallivm
, end_coroutine
- 1));
258 lp_build_else(&ifstate2
);
259 /* otherwise resume the coroutine */
260 lp_build_coro_resume(gallivm
, coro_hdl
);
261 lp_build_endif(&ifstate2
);
262 lp_build_endif(&ifstate
);
263 lp_build_loop_force_reload_counter(&loop_state
[3]);
265 lp_build_loop_end_cond(&loop_state
[0],
268 lp_build_loop_end_cond(&loop_state
[1],
271 lp_build_loop_end_cond(&loop_state
[2],
274 lp_build_loop_end_cond(&loop_state
[3],
275 lp_build_const_int32(gallivm
, end_coroutine
),
277 LLVMBuildRetVoid(builder
);
279 /* This is stage (b) - generate the compute shader code inside the coroutine. */
280 LLVMValueRef block_x_size_arg
, block_y_size_arg
, block_z_size_arg
;
281 context_ptr
= LLVMGetParam(coro
, 0);
282 x_size_arg
= LLVMGetParam(coro
, 1);
283 y_size_arg
= LLVMGetParam(coro
, 2);
284 z_size_arg
= LLVMGetParam(coro
, 3);
285 grid_x_arg
= LLVMGetParam(coro
, 4);
286 grid_y_arg
= LLVMGetParam(coro
, 5);
287 grid_z_arg
= LLVMGetParam(coro
, 6);
288 grid_size_x_arg
= LLVMGetParam(coro
, 7);
289 grid_size_y_arg
= LLVMGetParam(coro
, 8);
290 grid_size_z_arg
= LLVMGetParam(coro
, 9);
291 work_dim_arg
= LLVMGetParam(coro
, 10);
292 thread_data_ptr
= LLVMGetParam(coro
, 11);
293 num_x_loop
= LLVMGetParam(coro
, 12);
294 partials
= LLVMGetParam(coro
, 13);
295 block_x_size_arg
= LLVMGetParam(coro
, 14);
296 block_y_size_arg
= LLVMGetParam(coro
, 15);
297 block_z_size_arg
= LLVMGetParam(coro
, 16);
298 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, coro
, "entry");
299 LLVMPositionBuilderAtEnd(builder
, block
);
301 LLVMValueRef consts_ptr
, num_consts_ptr
;
302 LLVMValueRef ssbo_ptr
, num_ssbo_ptr
;
303 LLVMValueRef shared_ptr
;
304 LLVMValueRef kernel_args_ptr
;
305 struct lp_build_mask_context mask
;
306 struct lp_bld_tgsi_system_values system_values
;
308 memset(&system_values
, 0, sizeof(system_values
));
309 consts_ptr
= lp_jit_cs_context_constants(gallivm
, context_ptr
);
310 num_consts_ptr
= lp_jit_cs_context_num_constants(gallivm
, context_ptr
);
311 ssbo_ptr
= lp_jit_cs_context_ssbos(gallivm
, context_ptr
);
312 num_ssbo_ptr
= lp_jit_cs_context_num_ssbos(gallivm
, context_ptr
);
313 kernel_args_ptr
= lp_jit_cs_context_kernel_args(gallivm
, context_ptr
);
315 shared_ptr
= lp_jit_cs_thread_data_shared(gallivm
, thread_data_ptr
);
317 /* these are coroutine entrypoint necessities */
318 LLVMValueRef coro_id
= lp_build_coro_id(gallivm
);
319 LLVMValueRef coro_hdl
= lp_build_coro_begin_alloc_mem(gallivm
, coro_id
);
321 LLVMValueRef has_partials
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
, partials
, lp_build_const_int32(gallivm
, 0), "");
322 LLVMValueRef tid_vals
[3];
323 LLVMValueRef tids_x
[LP_MAX_VECTOR_LENGTH
], tids_y
[LP_MAX_VECTOR_LENGTH
], tids_z
[LP_MAX_VECTOR_LENGTH
];
324 LLVMValueRef base_val
= LLVMBuildMul(gallivm
->builder
, x_size_arg
, vec_length
, "");
325 for (i
= 0; i
< cs_type
.length
; i
++) {
326 tids_x
[i
] = LLVMBuildAdd(gallivm
->builder
, base_val
, lp_build_const_int32(gallivm
, i
), "");
327 tids_y
[i
] = y_size_arg
;
328 tids_z
[i
] = z_size_arg
;
330 tid_vals
[0] = lp_build_gather_values(gallivm
, tids_x
, cs_type
.length
);
331 tid_vals
[1] = lp_build_gather_values(gallivm
, tids_y
, cs_type
.length
);
332 tid_vals
[2] = lp_build_gather_values(gallivm
, tids_z
, cs_type
.length
);
333 system_values
.thread_id
= LLVMGetUndef(LLVMArrayType(LLVMVectorType(int32_type
, cs_type
.length
), 3));
334 for (i
= 0; i
< 3; i
++)
335 system_values
.thread_id
= LLVMBuildInsertValue(builder
, system_values
.thread_id
, tid_vals
[i
], i
, "");
337 LLVMValueRef gtids
[3] = { grid_x_arg
, grid_y_arg
, grid_z_arg
};
338 system_values
.block_id
= LLVMGetUndef(LLVMVectorType(int32_type
, 3));
339 for (i
= 0; i
< 3; i
++)
340 system_values
.block_id
= LLVMBuildInsertElement(builder
, system_values
.block_id
, gtids
[i
], lp_build_const_int32(gallivm
, i
), "");
342 LLVMValueRef gstids
[3] = { grid_size_x_arg
, grid_size_y_arg
, grid_size_z_arg
};
343 system_values
.grid_size
= LLVMGetUndef(LLVMVectorType(int32_type
, 3));
344 for (i
= 0; i
< 3; i
++)
345 system_values
.grid_size
= LLVMBuildInsertElement(builder
, system_values
.grid_size
, gstids
[i
], lp_build_const_int32(gallivm
, i
), "");
347 system_values
.work_dim
= work_dim_arg
;
349 LLVMValueRef bsize
[3] = { block_x_size_arg
, block_y_size_arg
, block_z_size_arg
};
350 system_values
.block_size
= LLVMGetUndef(LLVMVectorType(int32_type
, 3));
351 for (i
= 0; i
< 3; i
++)
352 system_values
.block_size
= LLVMBuildInsertElement(builder
, system_values
.block_size
, bsize
[i
], lp_build_const_int32(gallivm
, i
), "");
354 LLVMValueRef last_x_loop
= LLVMBuildICmp(gallivm
->builder
, LLVMIntEQ
, x_size_arg
, LLVMBuildSub(gallivm
->builder
, num_x_loop
, lp_build_const_int32(gallivm
, 1), ""), "");
355 LLVMValueRef use_partial_mask
= LLVMBuildAnd(gallivm
->builder
, last_x_loop
, has_partials
, "");
356 struct lp_build_if_state if_state
;
357 LLVMValueRef mask_val
= lp_build_alloca(gallivm
, LLVMVectorType(int32_type
, cs_type
.length
), "mask");
358 LLVMValueRef full_mask_val
= lp_build_const_int_vec(gallivm
, cs_type
, ~0);
359 LLVMBuildStore(gallivm
->builder
, full_mask_val
, mask_val
);
361 lp_build_if(&if_state
, gallivm
, use_partial_mask
);
362 struct lp_build_loop_state mask_loop_state
;
363 lp_build_loop_begin(&mask_loop_state
, gallivm
, partials
);
364 LLVMValueRef tmask_val
= LLVMBuildLoad(gallivm
->builder
, mask_val
, "");
365 tmask_val
= LLVMBuildInsertElement(gallivm
->builder
, tmask_val
, lp_build_const_int32(gallivm
, 0), mask_loop_state
.counter
, "");
366 LLVMBuildStore(gallivm
->builder
, tmask_val
, mask_val
);
367 lp_build_loop_end_cond(&mask_loop_state
, vec_length
, NULL
, LLVMIntUGE
);
368 lp_build_endif(&if_state
);
370 mask_val
= LLVMBuildLoad(gallivm
->builder
, mask_val
, "");
371 lp_build_mask_begin(&mask
, gallivm
, cs_type
, mask_val
);
373 struct lp_build_coro_suspend_info coro_info
;
375 LLVMBasicBlockRef sus_block
= LLVMAppendBasicBlockInContext(gallivm
->context
, coro
, "suspend");
376 LLVMBasicBlockRef clean_block
= LLVMAppendBasicBlockInContext(gallivm
->context
, coro
, "cleanup");
378 coro_info
.suspend
= sus_block
;
379 coro_info
.cleanup
= clean_block
;
381 struct lp_build_tgsi_params params
;
382 memset(¶ms
, 0, sizeof(params
));
384 params
.type
= cs_type
;
386 params
.consts_ptr
= consts_ptr
;
387 params
.const_sizes_ptr
= num_consts_ptr
;
388 params
.system_values
= &system_values
;
389 params
.context_ptr
= context_ptr
;
390 params
.sampler
= sampler
;
391 params
.info
= &shader
->info
.base
;
392 params
.ssbo_ptr
= ssbo_ptr
;
393 params
.ssbo_sizes_ptr
= num_ssbo_ptr
;
394 params
.image
= image
;
395 params
.shared_ptr
= shared_ptr
;
396 params
.coro
= &coro_info
;
397 params
.kernel_args
= kernel_args_ptr
;
399 if (shader
->base
.type
== PIPE_SHADER_IR_TGSI
)
400 lp_build_tgsi_soa(gallivm
, shader
->base
.tokens
, ¶ms
, NULL
);
402 lp_build_nir_soa(gallivm
, shader
->base
.ir
.nir
, ¶ms
,
405 mask_val
= lp_build_mask_end(&mask
);
407 lp_build_coro_suspend_switch(gallivm
, &coro_info
, NULL
, true);
408 LLVMPositionBuilderAtEnd(builder
, clean_block
);
410 lp_build_coro_free_mem(gallivm
, coro_id
, coro_hdl
);
412 LLVMBuildBr(builder
, sus_block
);
413 LLVMPositionBuilderAtEnd(builder
, sus_block
);
415 lp_build_coro_end(gallivm
, coro_hdl
);
416 LLVMBuildRet(builder
, coro_hdl
);
419 sampler
->destroy(sampler
);
420 image
->destroy(image
);
422 gallivm_verify_function(gallivm
, coro
);
423 gallivm_verify_function(gallivm
, function
);
427 llvmpipe_create_compute_state(struct pipe_context
*pipe
,
428 const struct pipe_compute_state
*templ
)
430 struct lp_compute_shader
*shader
;
431 int nr_samplers
, nr_sampler_views
;
433 shader
= CALLOC_STRUCT(lp_compute_shader
);
437 shader
->no
= cs_no
++;
439 shader
->base
.type
= templ
->ir_type
;
440 if (templ
->ir_type
== PIPE_SHADER_IR_NIR_SERIALIZED
) {
441 struct blob_reader reader
;
442 const struct pipe_binary_program_header
*hdr
= templ
->prog
;
444 blob_reader_init(&reader
, hdr
->blob
, hdr
->num_bytes
);
445 shader
->base
.ir
.nir
= nir_deserialize(NULL
, pipe
->screen
->get_compiler_options(pipe
->screen
, PIPE_SHADER_IR_NIR
, PIPE_SHADER_COMPUTE
), &reader
);
446 shader
->base
.type
= PIPE_SHADER_IR_NIR
;
448 pipe
->screen
->finalize_nir(pipe
->screen
, shader
->base
.ir
.nir
, false);
449 } else if (templ
->ir_type
== PIPE_SHADER_IR_NIR
)
450 shader
->base
.ir
.nir
= (struct nir_shader
*)templ
->prog
;
452 if (shader
->base
.type
== PIPE_SHADER_IR_TGSI
) {
453 /* get/save the summary info for this shader */
454 lp_build_tgsi_info(templ
->prog
, &shader
->info
);
456 /* we need to keep a local copy of the tokens */
457 shader
->base
.tokens
= tgsi_dup_tokens(templ
->prog
);
459 nir_tgsi_scan_shader(shader
->base
.ir
.nir
, &shader
->info
.base
, false);
462 shader
->req_local_mem
= templ
->req_local_mem
;
463 make_empty_list(&shader
->variants
);
465 nr_samplers
= shader
->info
.base
.file_max
[TGSI_FILE_SAMPLER
] + 1;
466 nr_sampler_views
= shader
->info
.base
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
467 int nr_images
= shader
->info
.base
.file_max
[TGSI_FILE_IMAGE
] + 1;
468 shader
->variant_key_size
= lp_cs_variant_key_size(MAX2(nr_samplers
, nr_sampler_views
), nr_images
);
474 llvmpipe_bind_compute_state(struct pipe_context
*pipe
,
477 struct llvmpipe_context
*llvmpipe
= llvmpipe_context(pipe
);
479 if (llvmpipe
->cs
== cs
)
482 llvmpipe
->cs
= (struct lp_compute_shader
*)cs
;
483 llvmpipe
->cs_dirty
|= LP_CSNEW_CS
;
487 * Remove shader variant from two lists: the shader's variant list
488 * and the context's variant list.
491 llvmpipe_remove_cs_shader_variant(struct llvmpipe_context
*lp
,
492 struct lp_compute_shader_variant
*variant
)
494 if ((LP_DEBUG
& DEBUG_CS
) || (gallivm_debug
& GALLIVM_DEBUG_IR
)) {
495 debug_printf("llvmpipe: del cs #%u var %u v created %u v cached %u "
496 "v total cached %u inst %u total inst %u\n",
497 variant
->shader
->no
, variant
->no
,
498 variant
->shader
->variants_created
,
499 variant
->shader
->variants_cached
,
500 lp
->nr_cs_variants
, variant
->nr_instrs
, lp
->nr_cs_instrs
);
503 gallivm_destroy(variant
->gallivm
);
505 /* remove from shader's list */
506 remove_from_list(&variant
->list_item_local
);
507 variant
->shader
->variants_cached
--;
509 /* remove from context's list */
510 remove_from_list(&variant
->list_item_global
);
511 lp
->nr_fs_variants
--;
512 lp
->nr_fs_instrs
-= variant
->nr_instrs
;
518 llvmpipe_delete_compute_state(struct pipe_context
*pipe
,
521 struct llvmpipe_context
*llvmpipe
= llvmpipe_context(pipe
);
522 struct lp_compute_shader
*shader
= cs
;
523 struct lp_cs_variant_list_item
*li
;
525 if (llvmpipe
->cs
== cs
)
527 for (unsigned i
= 0; i
< shader
->max_global_buffers
; i
++)
528 pipe_resource_reference(&shader
->global_buffers
[i
], NULL
);
529 FREE(shader
->global_buffers
);
531 /* Delete all the variants */
532 li
= first_elem(&shader
->variants
);
533 while(!at_end(&shader
->variants
, li
)) {
534 struct lp_cs_variant_list_item
*next
= next_elem(li
);
535 llvmpipe_remove_cs_shader_variant(llvmpipe
, li
->base
);
538 if (shader
->base
.ir
.nir
)
539 ralloc_free(shader
->base
.ir
.nir
);
540 tgsi_free_tokens(shader
->base
.tokens
);
544 static struct lp_compute_shader_variant_key
*
545 make_variant_key(struct llvmpipe_context
*lp
,
546 struct lp_compute_shader
*shader
,
550 struct lp_compute_shader_variant_key
*key
;
551 key
= (struct lp_compute_shader_variant_key
*)store
;
552 memset(key
, 0, offsetof(struct lp_compute_shader_variant_key
, samplers
[1]));
554 /* This value will be the same for all the variants of a given shader:
556 key
->nr_samplers
= shader
->info
.base
.file_max
[TGSI_FILE_SAMPLER
] + 1;
558 struct lp_sampler_static_state
*cs_sampler
;
560 cs_sampler
= key
->samplers
;
561 for(i
= 0; i
< key
->nr_samplers
; ++i
) {
562 if(shader
->info
.base
.file_mask
[TGSI_FILE_SAMPLER
] & (1 << i
)) {
563 lp_sampler_static_sampler_state(&cs_sampler
[i
].sampler_state
,
564 lp
->samplers
[PIPE_SHADER_COMPUTE
][i
]);
569 * XXX If TGSI_FILE_SAMPLER_VIEW exists assume all texture opcodes
570 * are dx10-style? Can't really have mixed opcodes, at least not
571 * if we want to skip the holes here (without rescanning tgsi).
573 if (shader
->info
.base
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
574 key
->nr_sampler_views
= shader
->info
.base
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
575 for(i
= 0; i
< key
->nr_sampler_views
; ++i
) {
577 * Note sview may exceed what's representable by file_mask.
578 * This will still work, the only downside is that not actually
579 * used views may be included in the shader key.
581 if(shader
->info
.base
.file_mask
[TGSI_FILE_SAMPLER_VIEW
] & (1u << (i
& 31))) {
582 lp_sampler_static_texture_state(&cs_sampler
[i
].texture_state
,
583 lp
->sampler_views
[PIPE_SHADER_COMPUTE
][i
]);
588 key
->nr_sampler_views
= key
->nr_samplers
;
589 for(i
= 0; i
< key
->nr_sampler_views
; ++i
) {
590 if(shader
->info
.base
.file_mask
[TGSI_FILE_SAMPLER
] & (1 << i
)) {
591 lp_sampler_static_texture_state(&cs_sampler
[i
].texture_state
,
592 lp
->sampler_views
[PIPE_SHADER_COMPUTE
][i
]);
597 struct lp_image_static_state
*lp_image
;
598 lp_image
= lp_cs_variant_key_images(key
);
599 key
->nr_images
= shader
->info
.base
.file_max
[TGSI_FILE_IMAGE
] + 1;
600 for (i
= 0; i
< key
->nr_images
; ++i
) {
601 if (shader
->info
.base
.file_mask
[TGSI_FILE_IMAGE
] & (1 << i
)) {
602 lp_sampler_static_texture_state_image(&lp_image
[i
].image_state
,
603 &lp
->images
[PIPE_SHADER_COMPUTE
][i
]);
610 dump_cs_variant_key(const struct lp_compute_shader_variant_key
*key
)
613 debug_printf("cs variant %p:\n", (void *) key
);
615 for (i
= 0; i
< key
->nr_samplers
; ++i
) {
616 const struct lp_static_sampler_state
*sampler
= &key
->samplers
[i
].sampler_state
;
617 debug_printf("sampler[%u] = \n", i
);
618 debug_printf(" .wrap = %s %s %s\n",
619 util_str_tex_wrap(sampler
->wrap_s
, TRUE
),
620 util_str_tex_wrap(sampler
->wrap_t
, TRUE
),
621 util_str_tex_wrap(sampler
->wrap_r
, TRUE
));
622 debug_printf(" .min_img_filter = %s\n",
623 util_str_tex_filter(sampler
->min_img_filter
, TRUE
));
624 debug_printf(" .min_mip_filter = %s\n",
625 util_str_tex_mipfilter(sampler
->min_mip_filter
, TRUE
));
626 debug_printf(" .mag_img_filter = %s\n",
627 util_str_tex_filter(sampler
->mag_img_filter
, TRUE
));
628 if (sampler
->compare_mode
!= PIPE_TEX_COMPARE_NONE
)
629 debug_printf(" .compare_func = %s\n", util_str_func(sampler
->compare_func
, TRUE
));
630 debug_printf(" .normalized_coords = %u\n", sampler
->normalized_coords
);
631 debug_printf(" .min_max_lod_equal = %u\n", sampler
->min_max_lod_equal
);
632 debug_printf(" .lod_bias_non_zero = %u\n", sampler
->lod_bias_non_zero
);
633 debug_printf(" .apply_min_lod = %u\n", sampler
->apply_min_lod
);
634 debug_printf(" .apply_max_lod = %u\n", sampler
->apply_max_lod
);
636 for (i
= 0; i
< key
->nr_sampler_views
; ++i
) {
637 const struct lp_static_texture_state
*texture
= &key
->samplers
[i
].texture_state
;
638 debug_printf("texture[%u] = \n", i
);
639 debug_printf(" .format = %s\n",
640 util_format_name(texture
->format
));
641 debug_printf(" .target = %s\n",
642 util_str_tex_target(texture
->target
, TRUE
));
643 debug_printf(" .level_zero_only = %u\n",
644 texture
->level_zero_only
);
645 debug_printf(" .pot = %u %u %u\n",
650 struct lp_image_static_state
*images
= lp_cs_variant_key_images(key
);
651 for (i
= 0; i
< key
->nr_images
; ++i
) {
652 const struct lp_static_texture_state
*image
= &images
[i
].image_state
;
653 debug_printf("image[%u] = \n", i
);
654 debug_printf(" .format = %s\n",
655 util_format_name(image
->format
));
656 debug_printf(" .target = %s\n",
657 util_str_tex_target(image
->target
, TRUE
));
658 debug_printf(" .level_zero_only = %u\n",
659 image
->level_zero_only
);
660 debug_printf(" .pot = %u %u %u\n",
668 lp_debug_cs_variant(const struct lp_compute_shader_variant
*variant
)
670 debug_printf("llvmpipe: Compute shader #%u variant #%u:\n",
671 variant
->shader
->no
, variant
->no
);
672 if (variant
->shader
->base
.type
== PIPE_SHADER_IR_TGSI
)
673 tgsi_dump(variant
->shader
->base
.tokens
, 0);
675 nir_print_shader(variant
->shader
->base
.ir
.nir
, stderr
);
676 dump_cs_variant_key(&variant
->key
);
681 lp_cs_get_ir_cache_key(struct lp_compute_shader_variant
*variant
,
682 unsigned char ir_sha1_cache_key
[20])
684 struct blob blob
= { 0 };
689 nir_serialize(&blob
, variant
->shader
->base
.ir
.nir
, true);
690 ir_binary
= blob
.data
;
693 struct mesa_sha1 ctx
;
694 _mesa_sha1_init(&ctx
);
695 _mesa_sha1_update(&ctx
, &variant
->key
, variant
->shader
->variant_key_size
);
696 _mesa_sha1_update(&ctx
, ir_binary
, ir_size
);
697 _mesa_sha1_final(&ctx
, ir_sha1_cache_key
);
702 static struct lp_compute_shader_variant
*
703 generate_variant(struct llvmpipe_context
*lp
,
704 struct lp_compute_shader
*shader
,
705 const struct lp_compute_shader_variant_key
*key
)
707 struct llvmpipe_screen
*screen
= llvmpipe_screen(lp
->pipe
.screen
);
708 struct lp_compute_shader_variant
*variant
;
709 char module_name
[64];
710 unsigned char ir_sha1_cache_key
[20];
711 struct lp_cached_code cached
= { 0 };
712 bool needs_caching
= false;
713 variant
= MALLOC(sizeof *variant
+ shader
->variant_key_size
- sizeof variant
->key
);
717 memset(variant
, 0, sizeof(*variant
));
718 snprintf(module_name
, sizeof(module_name
), "cs%u_variant%u",
719 shader
->no
, shader
->variants_created
);
721 variant
->shader
= shader
;
722 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
724 if (shader
->base
.ir
.nir
) {
725 lp_cs_get_ir_cache_key(variant
, ir_sha1_cache_key
);
727 lp_disk_cache_find_shader(screen
, &cached
, ir_sha1_cache_key
);
728 if (!cached
.data_size
)
729 needs_caching
= true;
731 variant
->gallivm
= gallivm_create(module_name
, lp
->context
, &cached
);
732 if (!variant
->gallivm
) {
737 variant
->list_item_global
.base
= variant
;
738 variant
->list_item_local
.base
= variant
;
739 variant
->no
= shader
->variants_created
++;
743 if ((LP_DEBUG
& DEBUG_CS
) || (gallivm_debug
& GALLIVM_DEBUG_IR
)) {
744 lp_debug_cs_variant(variant
);
747 lp_jit_init_cs_types(variant
);
749 generate_compute(lp
, shader
, variant
);
751 gallivm_compile_module(variant
->gallivm
);
753 lp_build_coro_add_malloc_hooks(variant
->gallivm
);
754 variant
->nr_instrs
+= lp_build_count_ir_module(variant
->gallivm
->module
);
756 variant
->jit_function
= (lp_jit_cs_func
)gallivm_jit_function(variant
->gallivm
, variant
->function
);
759 lp_disk_cache_insert_shader(screen
, &cached
, ir_sha1_cache_key
);
761 gallivm_free_ir(variant
->gallivm
);
766 lp_cs_ctx_set_cs_variant( struct lp_cs_context
*csctx
,
767 struct lp_compute_shader_variant
*variant
)
769 csctx
->cs
.current
.variant
= variant
;
773 llvmpipe_update_cs(struct llvmpipe_context
*lp
)
775 struct lp_compute_shader
*shader
= lp
->cs
;
777 struct lp_compute_shader_variant_key
*key
;
778 struct lp_compute_shader_variant
*variant
= NULL
;
779 struct lp_cs_variant_list_item
*li
;
780 char store
[LP_CS_MAX_VARIANT_KEY_SIZE
];
782 key
= make_variant_key(lp
, shader
, store
);
784 /* Search the variants for one which matches the key */
785 li
= first_elem(&shader
->variants
);
786 while(!at_end(&shader
->variants
, li
)) {
787 if(memcmp(&li
->base
->key
, key
, shader
->variant_key_size
) == 0) {
795 /* Move this variant to the head of the list to implement LRU
796 * deletion of shader's when we have too many.
798 move_to_head(&lp
->cs_variants_list
, &variant
->list_item_global
);
801 /* variant not found, create it now */
804 unsigned variants_to_cull
;
806 if (LP_DEBUG
& DEBUG_CS
) {
807 debug_printf("%u variants,\t%u instrs,\t%u instrs/variant\n",
810 lp
->nr_cs_variants
? lp
->nr_cs_instrs
/ lp
->nr_cs_variants
: 0);
813 /* First, check if we've exceeded the max number of shader variants.
814 * If so, free 6.25% of them (the least recently used ones).
816 variants_to_cull
= lp
->nr_cs_variants
>= LP_MAX_SHADER_VARIANTS
? LP_MAX_SHADER_VARIANTS
/ 16 : 0;
818 if (variants_to_cull
||
819 lp
->nr_cs_instrs
>= LP_MAX_SHADER_INSTRUCTIONS
) {
820 if (gallivm_debug
& GALLIVM_DEBUG_PERF
) {
821 debug_printf("Evicting CS: %u cs variants,\t%u total variants,"
822 "\t%u instrs,\t%u instrs/variant\n",
823 shader
->variants_cached
,
824 lp
->nr_cs_variants
, lp
->nr_cs_instrs
,
825 lp
->nr_cs_instrs
/ lp
->nr_cs_variants
);
829 * We need to re-check lp->nr_cs_variants because an arbitrarliy large
830 * number of shader variants (potentially all of them) could be
831 * pending for destruction on flush.
834 for (i
= 0; i
< variants_to_cull
|| lp
->nr_cs_instrs
>= LP_MAX_SHADER_INSTRUCTIONS
; i
++) {
835 struct lp_cs_variant_list_item
*item
;
836 if (is_empty_list(&lp
->cs_variants_list
)) {
839 item
= last_elem(&lp
->cs_variants_list
);
842 llvmpipe_remove_cs_shader_variant(lp
, item
->base
);
846 * Generate the new variant.
849 variant
= generate_variant(lp
, shader
, key
);
852 LP_COUNT_ADD(llvm_compile_time
, dt
);
853 LP_COUNT_ADD(nr_llvm_compiles
, 2); /* emit vs. omit in/out test */
855 /* Put the new variant into the list */
857 insert_at_head(&shader
->variants
, &variant
->list_item_local
);
858 insert_at_head(&lp
->cs_variants_list
, &variant
->list_item_global
);
859 lp
->nr_cs_variants
++;
860 lp
->nr_cs_instrs
+= variant
->nr_instrs
;
861 shader
->variants_cached
++;
864 /* Bind this variant */
865 lp_cs_ctx_set_cs_variant(lp
->csctx
, variant
);
869 * Called during state validation when LP_CSNEW_SAMPLER_VIEW is set.
872 lp_csctx_set_sampler_views(struct lp_cs_context
*csctx
,
874 struct pipe_sampler_view
**views
)
876 unsigned i
, max_tex_num
;
878 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
880 assert(num
<= PIPE_MAX_SHADER_SAMPLER_VIEWS
);
882 max_tex_num
= MAX2(num
, csctx
->cs
.current_tex_num
);
884 for (i
= 0; i
< max_tex_num
; i
++) {
885 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
888 struct pipe_resource
*res
= view
->texture
;
889 struct llvmpipe_resource
*lp_tex
= llvmpipe_resource(res
);
890 struct lp_jit_texture
*jit_tex
;
891 jit_tex
= &csctx
->cs
.current
.jit_context
.textures
[i
];
893 /* We're referencing the texture's internal data, so save a
896 pipe_resource_reference(&csctx
->cs
.current_tex
[i
], res
);
899 /* regular texture - csctx array of mipmap level offsets */
901 unsigned first_level
= 0;
902 unsigned last_level
= 0;
904 if (llvmpipe_resource_is_texture(res
)) {
905 first_level
= view
->u
.tex
.first_level
;
906 last_level
= view
->u
.tex
.last_level
;
907 assert(first_level
<= last_level
);
908 assert(last_level
<= res
->last_level
);
909 jit_tex
->base
= lp_tex
->tex_data
;
912 jit_tex
->base
= lp_tex
->data
;
914 if (LP_PERF
& PERF_TEX_MEM
) {
915 /* use dummy tile memory */
916 jit_tex
->base
= lp_dummy_tile
;
917 jit_tex
->width
= TILE_SIZE
/8;
918 jit_tex
->height
= TILE_SIZE
/8;
920 jit_tex
->first_level
= 0;
921 jit_tex
->last_level
= 0;
922 jit_tex
->mip_offsets
[0] = 0;
923 jit_tex
->row_stride
[0] = 0;
924 jit_tex
->img_stride
[0] = 0;
925 jit_tex
->num_samples
= 0;
926 jit_tex
->sample_stride
= 0;
929 jit_tex
->width
= res
->width0
;
930 jit_tex
->height
= res
->height0
;
931 jit_tex
->depth
= res
->depth0
;
932 jit_tex
->first_level
= first_level
;
933 jit_tex
->last_level
= last_level
;
934 jit_tex
->num_samples
= res
->nr_samples
;
935 jit_tex
->sample_stride
= 0;
937 if (llvmpipe_resource_is_texture(res
)) {
938 for (j
= first_level
; j
<= last_level
; j
++) {
939 jit_tex
->mip_offsets
[j
] = lp_tex
->mip_offsets
[j
];
940 jit_tex
->row_stride
[j
] = lp_tex
->row_stride
[j
];
941 jit_tex
->img_stride
[j
] = lp_tex
->img_stride
[j
];
943 jit_tex
->sample_stride
= lp_tex
->sample_stride
;
945 if (res
->target
== PIPE_TEXTURE_1D_ARRAY
||
946 res
->target
== PIPE_TEXTURE_2D_ARRAY
||
947 res
->target
== PIPE_TEXTURE_CUBE
||
948 res
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
950 * For array textures, we don't have first_layer, instead
951 * adjust last_layer (stored as depth) plus the mip level offsets
952 * (as we have mip-first layout can't just adjust base ptr).
953 * XXX For mip levels, could do something similar.
955 jit_tex
->depth
= view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1;
956 for (j
= first_level
; j
<= last_level
; j
++) {
957 jit_tex
->mip_offsets
[j
] += view
->u
.tex
.first_layer
*
958 lp_tex
->img_stride
[j
];
960 if (view
->target
== PIPE_TEXTURE_CUBE
||
961 view
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
962 assert(jit_tex
->depth
% 6 == 0);
964 assert(view
->u
.tex
.first_layer
<= view
->u
.tex
.last_layer
);
965 assert(view
->u
.tex
.last_layer
< res
->array_size
);
970 * For buffers, we don't have "offset", instead adjust
971 * the size (stored as width) plus the base pointer.
973 unsigned view_blocksize
= util_format_get_blocksize(view
->format
);
974 /* probably don't really need to fill that out */
975 jit_tex
->mip_offsets
[0] = 0;
976 jit_tex
->row_stride
[0] = 0;
977 jit_tex
->img_stride
[0] = 0;
979 /* everything specified in number of elements here. */
980 jit_tex
->width
= view
->u
.buf
.size
/ view_blocksize
;
981 jit_tex
->base
= (uint8_t *)jit_tex
->base
+ view
->u
.buf
.offset
;
982 /* XXX Unsure if we need to sanitize parameters? */
983 assert(view
->u
.buf
.offset
+ view
->u
.buf
.size
<= res
->width0
);
988 /* display target texture/surface */
990 * XXX: Where should this be unmapped?
992 struct llvmpipe_screen
*screen
= llvmpipe_screen(res
->screen
);
993 struct sw_winsys
*winsys
= screen
->winsys
;
994 jit_tex
->base
= winsys
->displaytarget_map(winsys
, lp_tex
->dt
,
996 jit_tex
->row_stride
[0] = lp_tex
->row_stride
[0];
997 jit_tex
->img_stride
[0] = lp_tex
->img_stride
[0];
998 jit_tex
->mip_offsets
[0] = 0;
999 jit_tex
->width
= res
->width0
;
1000 jit_tex
->height
= res
->height0
;
1001 jit_tex
->depth
= res
->depth0
;
1002 jit_tex
->first_level
= jit_tex
->last_level
= 0;
1003 jit_tex
->num_samples
= res
->nr_samples
;
1004 jit_tex
->sample_stride
= 0;
1005 assert(jit_tex
->base
);
1009 pipe_resource_reference(&csctx
->cs
.current_tex
[i
], NULL
);
1012 csctx
->cs
.current_tex_num
= num
;
1017 * Called during state validation when LP_NEW_SAMPLER is set.
1020 lp_csctx_set_sampler_state(struct lp_cs_context
*csctx
,
1022 struct pipe_sampler_state
**samplers
)
1026 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
1028 assert(num
<= PIPE_MAX_SAMPLERS
);
1030 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
1031 const struct pipe_sampler_state
*sampler
= i
< num
? samplers
[i
] : NULL
;
1034 struct lp_jit_sampler
*jit_sam
;
1035 jit_sam
= &csctx
->cs
.current
.jit_context
.samplers
[i
];
1037 jit_sam
->min_lod
= sampler
->min_lod
;
1038 jit_sam
->max_lod
= sampler
->max_lod
;
1039 jit_sam
->lod_bias
= sampler
->lod_bias
;
1040 COPY_4V(jit_sam
->border_color
, sampler
->border_color
.f
);
1046 lp_csctx_set_cs_constants(struct lp_cs_context
*csctx
,
1048 struct pipe_constant_buffer
*buffers
)
1052 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) buffers
);
1054 assert(num
<= ARRAY_SIZE(csctx
->constants
));
1056 for (i
= 0; i
< num
; ++i
) {
1057 util_copy_constant_buffer(&csctx
->constants
[i
].current
, &buffers
[i
]);
1059 for (; i
< ARRAY_SIZE(csctx
->constants
); i
++) {
1060 util_copy_constant_buffer(&csctx
->constants
[i
].current
, NULL
);
1065 lp_csctx_set_cs_ssbos(struct lp_cs_context
*csctx
,
1067 struct pipe_shader_buffer
*buffers
)
1070 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *)buffers
);
1072 assert (num
<= ARRAY_SIZE(csctx
->ssbos
));
1074 for (i
= 0; i
< num
; ++i
) {
1075 util_copy_shader_buffer(&csctx
->ssbos
[i
].current
, &buffers
[i
]);
1077 for (; i
< ARRAY_SIZE(csctx
->ssbos
); i
++) {
1078 util_copy_shader_buffer(&csctx
->ssbos
[i
].current
, NULL
);
1083 lp_csctx_set_cs_images(struct lp_cs_context
*csctx
,
1085 struct pipe_image_view
*images
)
1089 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) images
);
1091 assert(num
<= ARRAY_SIZE(csctx
->images
));
1093 for (i
= 0; i
< num
; ++i
) {
1094 struct pipe_image_view
*image
= &images
[i
];
1095 util_copy_image_view(&csctx
->images
[i
].current
, &images
[i
]);
1097 struct pipe_resource
*res
= image
->resource
;
1098 struct llvmpipe_resource
*lp_res
= llvmpipe_resource(res
);
1099 struct lp_jit_image
*jit_image
;
1101 jit_image
= &csctx
->cs
.current
.jit_context
.images
[i
];
1105 /* regular texture - csctx array of mipmap level offsets */
1106 if (llvmpipe_resource_is_texture(res
)) {
1107 jit_image
->base
= lp_res
->tex_data
;
1109 jit_image
->base
= lp_res
->data
;
1111 jit_image
->width
= res
->width0
;
1112 jit_image
->height
= res
->height0
;
1113 jit_image
->depth
= res
->depth0
;
1114 jit_image
->num_samples
= res
->nr_samples
;
1116 if (llvmpipe_resource_is_texture(res
)) {
1117 uint32_t mip_offset
= lp_res
->mip_offsets
[image
->u
.tex
.level
];
1119 jit_image
->width
= u_minify(jit_image
->width
, image
->u
.tex
.level
);
1120 jit_image
->height
= u_minify(jit_image
->height
, image
->u
.tex
.level
);
1122 if (res
->target
== PIPE_TEXTURE_1D_ARRAY
||
1123 res
->target
== PIPE_TEXTURE_2D_ARRAY
||
1124 res
->target
== PIPE_TEXTURE_3D
||
1125 res
->target
== PIPE_TEXTURE_CUBE
||
1126 res
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
1128 * For array textures, we don't have first_layer, instead
1129 * adjust last_layer (stored as depth) plus the mip level offsets
1130 * (as we have mip-first layout can't just adjust base ptr).
1131 * XXX For mip levels, could do something similar.
1133 jit_image
->depth
= image
->u
.tex
.last_layer
- image
->u
.tex
.first_layer
+ 1;
1134 mip_offset
+= image
->u
.tex
.first_layer
* lp_res
->img_stride
[image
->u
.tex
.level
];
1136 jit_image
->depth
= u_minify(jit_image
->depth
, image
->u
.tex
.level
);
1138 jit_image
->row_stride
= lp_res
->row_stride
[image
->u
.tex
.level
];
1139 jit_image
->img_stride
= lp_res
->img_stride
[image
->u
.tex
.level
];
1140 jit_image
->sample_stride
= lp_res
->sample_stride
;
1141 jit_image
->base
= (uint8_t *)jit_image
->base
+ mip_offset
;
1143 unsigned view_blocksize
= util_format_get_blocksize(image
->format
);
1144 jit_image
->width
= image
->u
.buf
.size
/ view_blocksize
;
1145 jit_image
->base
= (uint8_t *)jit_image
->base
+ image
->u
.buf
.offset
;
1149 for (; i
< ARRAY_SIZE(csctx
->images
); i
++) {
1150 util_copy_image_view(&csctx
->images
[i
].current
, NULL
);
1155 update_csctx_consts(struct llvmpipe_context
*llvmpipe
)
1157 struct lp_cs_context
*csctx
= llvmpipe
->csctx
;
1160 for (i
= 0; i
< ARRAY_SIZE(csctx
->constants
); ++i
) {
1161 struct pipe_resource
*buffer
= csctx
->constants
[i
].current
.buffer
;
1162 const ubyte
*current_data
= NULL
;
1165 /* resource buffer */
1166 current_data
= (ubyte
*) llvmpipe_resource_data(buffer
);
1168 else if (csctx
->constants
[i
].current
.user_buffer
) {
1169 /* user-space buffer */
1170 current_data
= (ubyte
*) csctx
->constants
[i
].current
.user_buffer
;
1174 current_data
+= csctx
->constants
[i
].current
.buffer_offset
;
1176 csctx
->cs
.current
.jit_context
.constants
[i
] = (const float *)current_data
;
1177 csctx
->cs
.current
.jit_context
.num_constants
[i
] = csctx
->constants
[i
].current
.buffer_size
;
1179 csctx
->cs
.current
.jit_context
.constants
[i
] = NULL
;
1180 csctx
->cs
.current
.jit_context
.num_constants
[i
] = 0;
1186 update_csctx_ssbo(struct llvmpipe_context
*llvmpipe
)
1188 struct lp_cs_context
*csctx
= llvmpipe
->csctx
;
1190 for (i
= 0; i
< ARRAY_SIZE(csctx
->ssbos
); ++i
) {
1191 struct pipe_resource
*buffer
= csctx
->ssbos
[i
].current
.buffer
;
1192 const ubyte
*current_data
= NULL
;
1196 /* resource buffer */
1197 current_data
= (ubyte
*) llvmpipe_resource_data(buffer
);
1199 current_data
+= csctx
->ssbos
[i
].current
.buffer_offset
;
1201 csctx
->cs
.current
.jit_context
.ssbos
[i
] = (const uint32_t *)current_data
;
1202 csctx
->cs
.current
.jit_context
.num_ssbos
[i
] = csctx
->ssbos
[i
].current
.buffer_size
;
1204 csctx
->cs
.current
.jit_context
.ssbos
[i
] = NULL
;
1205 csctx
->cs
.current
.jit_context
.num_ssbos
[i
] = 0;
1211 llvmpipe_cs_update_derived(struct llvmpipe_context
*llvmpipe
, void *input
)
1213 if (llvmpipe
->cs_dirty
& (LP_CSNEW_CS
))
1214 llvmpipe_update_cs(llvmpipe
);
1216 if (llvmpipe
->cs_dirty
& LP_CSNEW_CONSTANTS
) {
1217 lp_csctx_set_cs_constants(llvmpipe
->csctx
,
1218 ARRAY_SIZE(llvmpipe
->constants
[PIPE_SHADER_COMPUTE
]),
1219 llvmpipe
->constants
[PIPE_SHADER_COMPUTE
]);
1220 update_csctx_consts(llvmpipe
);
1223 if (llvmpipe
->cs_dirty
& LP_CSNEW_SSBOS
) {
1224 lp_csctx_set_cs_ssbos(llvmpipe
->csctx
,
1225 ARRAY_SIZE(llvmpipe
->ssbos
[PIPE_SHADER_COMPUTE
]),
1226 llvmpipe
->ssbos
[PIPE_SHADER_COMPUTE
]);
1227 update_csctx_ssbo(llvmpipe
);
1230 if (llvmpipe
->cs_dirty
& LP_CSNEW_SAMPLER_VIEW
)
1231 lp_csctx_set_sampler_views(llvmpipe
->csctx
,
1232 llvmpipe
->num_sampler_views
[PIPE_SHADER_COMPUTE
],
1233 llvmpipe
->sampler_views
[PIPE_SHADER_COMPUTE
]);
1235 if (llvmpipe
->cs_dirty
& LP_CSNEW_SAMPLER
)
1236 lp_csctx_set_sampler_state(llvmpipe
->csctx
,
1237 llvmpipe
->num_samplers
[PIPE_SHADER_COMPUTE
],
1238 llvmpipe
->samplers
[PIPE_SHADER_COMPUTE
]);
1240 if (llvmpipe
->cs_dirty
& LP_CSNEW_IMAGES
)
1241 lp_csctx_set_cs_images(llvmpipe
->csctx
,
1242 ARRAY_SIZE(llvmpipe
->images
[PIPE_SHADER_COMPUTE
]),
1243 llvmpipe
->images
[PIPE_SHADER_COMPUTE
]);
1246 struct lp_cs_context
*csctx
= llvmpipe
->csctx
;
1247 csctx
->input
= input
;
1248 csctx
->cs
.current
.jit_context
.kernel_args
= input
;
1251 llvmpipe
->cs_dirty
= 0;
1255 cs_exec_fn(void *init_data
, int iter_idx
, struct lp_cs_local_mem
*lmem
)
1257 struct lp_cs_job_info
*job_info
= init_data
;
1258 struct lp_jit_cs_thread_data thread_data
;
1260 memset(&thread_data
, 0, sizeof(thread_data
));
1262 if (lmem
->local_size
< job_info
->req_local_mem
) {
1263 lmem
->local_mem_ptr
= REALLOC(lmem
->local_mem_ptr
, lmem
->local_size
,
1264 job_info
->req_local_mem
);
1265 lmem
->local_size
= job_info
->req_local_mem
;
1267 thread_data
.shared
= lmem
->local_mem_ptr
;
1269 unsigned grid_z
= iter_idx
/ (job_info
->grid_size
[0] * job_info
->grid_size
[1]);
1270 unsigned grid_y
= (iter_idx
- (grid_z
* (job_info
->grid_size
[0] * job_info
->grid_size
[1]))) / job_info
->grid_size
[0];
1271 unsigned grid_x
= (iter_idx
- (grid_z
* (job_info
->grid_size
[0] * job_info
->grid_size
[1])) - (grid_y
* job_info
->grid_size
[0]));
1272 struct lp_compute_shader_variant
*variant
= job_info
->current
->variant
;
1273 variant
->jit_function(&job_info
->current
->jit_context
,
1274 job_info
->block_size
[0], job_info
->block_size
[1], job_info
->block_size
[2],
1275 grid_x
, grid_y
, grid_z
,
1276 job_info
->grid_size
[0], job_info
->grid_size
[1], job_info
->grid_size
[2], job_info
->work_dim
,
1281 fill_grid_size(struct pipe_context
*pipe
,
1282 const struct pipe_grid_info
*info
,
1283 uint32_t grid_size
[3])
1285 struct pipe_transfer
*transfer
;
1287 if (!info
->indirect
) {
1288 grid_size
[0] = info
->grid
[0];
1289 grid_size
[1] = info
->grid
[1];
1290 grid_size
[2] = info
->grid
[2];
1293 params
= pipe_buffer_map_range(pipe
, info
->indirect
,
1294 info
->indirect_offset
,
1295 3 * sizeof(uint32_t),
1302 grid_size
[0] = params
[0];
1303 grid_size
[1] = params
[1];
1304 grid_size
[2] = params
[2];
1305 pipe_buffer_unmap(pipe
, transfer
);
1308 static void llvmpipe_launch_grid(struct pipe_context
*pipe
,
1309 const struct pipe_grid_info
*info
)
1311 struct llvmpipe_context
*llvmpipe
= llvmpipe_context(pipe
);
1312 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
1313 struct lp_cs_job_info job_info
;
1315 memset(&job_info
, 0, sizeof(job_info
));
1317 llvmpipe_cs_update_derived(llvmpipe
, info
->input
);
1319 fill_grid_size(pipe
, info
, job_info
.grid_size
);
1321 job_info
.block_size
[0] = info
->block
[0];
1322 job_info
.block_size
[1] = info
->block
[1];
1323 job_info
.block_size
[2] = info
->block
[2];
1324 job_info
.work_dim
= info
->work_dim
;
1325 job_info
.req_local_mem
= llvmpipe
->cs
->req_local_mem
;
1326 job_info
.current
= &llvmpipe
->csctx
->cs
.current
;
1328 int num_tasks
= job_info
.grid_size
[2] * job_info
.grid_size
[1] * job_info
.grid_size
[0];
1330 struct lp_cs_tpool_task
*task
;
1331 mtx_lock(&screen
->cs_mutex
);
1332 task
= lp_cs_tpool_queue_task(screen
->cs_tpool
, cs_exec_fn
, &job_info
, num_tasks
);
1334 lp_cs_tpool_wait_for_task(screen
->cs_tpool
, &task
);
1335 mtx_unlock(&screen
->cs_mutex
);
1337 llvmpipe
->pipeline_statistics
.cs_invocations
+= num_tasks
* info
->block
[0] * info
->block
[1] * info
->block
[2];
1341 llvmpipe_set_compute_resources(struct pipe_context
*pipe
,
1342 unsigned start
, unsigned count
,
1343 struct pipe_surface
**resources
)
1350 llvmpipe_set_global_binding(struct pipe_context
*pipe
,
1351 unsigned first
, unsigned count
,
1352 struct pipe_resource
**resources
,
1355 struct llvmpipe_context
*llvmpipe
= llvmpipe_context(pipe
);
1356 struct lp_compute_shader
*cs
= llvmpipe
->cs
;
1359 if (first
+ count
> cs
->max_global_buffers
) {
1360 unsigned old_max
= cs
->max_global_buffers
;
1361 cs
->max_global_buffers
= first
+ count
;
1362 cs
->global_buffers
= realloc(cs
->global_buffers
,
1363 cs
->max_global_buffers
* sizeof(cs
->global_buffers
[0]));
1364 if (!cs
->global_buffers
) {
1368 memset(&cs
->global_buffers
[old_max
], 0, (cs
->max_global_buffers
- old_max
) * sizeof(cs
->global_buffers
[0]));
1372 for (i
= 0; i
< count
; i
++)
1373 pipe_resource_reference(&cs
->global_buffers
[first
+ i
], NULL
);
1377 for (i
= 0; i
< count
; i
++) {
1380 pipe_resource_reference(&cs
->global_buffers
[first
+ i
], resources
[i
]);
1381 struct llvmpipe_resource
*lp_res
= llvmpipe_resource(resources
[i
]);
1382 offset
= *handles
[i
];
1383 va
= (uintptr_t)((char *)lp_res
->data
+ offset
);
1384 memcpy(handles
[i
], &va
, sizeof(va
));
1389 llvmpipe_init_compute_funcs(struct llvmpipe_context
*llvmpipe
)
1391 llvmpipe
->pipe
.create_compute_state
= llvmpipe_create_compute_state
;
1392 llvmpipe
->pipe
.bind_compute_state
= llvmpipe_bind_compute_state
;
1393 llvmpipe
->pipe
.delete_compute_state
= llvmpipe_delete_compute_state
;
1394 llvmpipe
->pipe
.set_compute_resources
= llvmpipe_set_compute_resources
;
1395 llvmpipe
->pipe
.set_global_binding
= llvmpipe_set_global_binding
;
1396 llvmpipe
->pipe
.launch_grid
= llvmpipe_launch_grid
;
1400 lp_csctx_destroy(struct lp_cs_context
*csctx
)
1403 for (i
= 0; i
< ARRAY_SIZE(csctx
->cs
.current_tex
); i
++) {
1404 pipe_resource_reference(&csctx
->cs
.current_tex
[i
], NULL
);
1406 for (i
= 0; i
< ARRAY_SIZE(csctx
->constants
); i
++) {
1407 pipe_resource_reference(&csctx
->constants
[i
].current
.buffer
, NULL
);
1409 for (i
= 0; i
< ARRAY_SIZE(csctx
->ssbos
); i
++) {
1410 pipe_resource_reference(&csctx
->ssbos
[i
].current
.buffer
, NULL
);
1415 struct lp_cs_context
*lp_csctx_create(struct pipe_context
*pipe
)
1417 struct lp_cs_context
*csctx
;
1419 csctx
= CALLOC_STRUCT(lp_cs_context
);