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 "state_tracker/sw_winsys.h"
48 #include "nir/nir_to_tgsi_info.h"
50 struct lp_cs_job_info
{
51 unsigned grid_size
[3];
52 unsigned block_size
[3];
53 unsigned req_local_mem
;
55 struct lp_cs_exec
*current
;
59 generate_compute(struct llvmpipe_context
*lp
,
60 struct lp_compute_shader
*shader
,
61 struct lp_compute_shader_variant
*variant
)
63 struct gallivm_state
*gallivm
= variant
->gallivm
;
64 const struct lp_compute_shader_variant_key
*key
= &variant
->key
;
65 char func_name
[64], func_name_coro
[64];
66 LLVMTypeRef arg_types
[14];
67 LLVMTypeRef func_type
, coro_func_type
;
68 LLVMTypeRef int32_type
= LLVMInt32TypeInContext(gallivm
->context
);
69 LLVMValueRef context_ptr
;
70 LLVMValueRef x_size_arg
, y_size_arg
, z_size_arg
;
71 LLVMValueRef grid_x_arg
, grid_y_arg
, grid_z_arg
;
72 LLVMValueRef grid_size_x_arg
, grid_size_y_arg
, grid_size_z_arg
;
73 LLVMValueRef work_dim_arg
, thread_data_ptr
;
74 LLVMBasicBlockRef block
;
75 LLVMBuilderRef builder
;
76 struct lp_build_sampler_soa
*sampler
;
77 struct lp_build_image_soa
*image
;
78 LLVMValueRef function
, coro
;
79 struct lp_type cs_type
;
83 * This function has two parts
84 * a) setup the coroutine execution environment loop.
85 * b) build the compute shader llvm for use inside the coroutine.
87 assert(lp_native_vector_width
/ 32 >= 4);
89 memset(&cs_type
, 0, sizeof cs_type
);
90 cs_type
.floating
= TRUE
; /* floating point values */
91 cs_type
.sign
= TRUE
; /* values are signed */
92 cs_type
.norm
= FALSE
; /* values are not limited to [0,1] or [-1,1] */
93 cs_type
.width
= 32; /* 32-bit float */
94 cs_type
.length
= MIN2(lp_native_vector_width
/ 32, 16); /* n*4 elements per vector */
95 snprintf(func_name
, sizeof(func_name
), "cs%u_variant%u",
96 shader
->no
, variant
->no
);
98 snprintf(func_name_coro
, sizeof(func_name
), "cs_co_%u_variant%u",
99 shader
->no
, variant
->no
);
101 arg_types
[0] = variant
->jit_cs_context_ptr_type
; /* context */
102 arg_types
[1] = int32_type
; /* block_x_size */
103 arg_types
[2] = int32_type
; /* block_y_size */
104 arg_types
[3] = int32_type
; /* block_z_size */
105 arg_types
[4] = int32_type
; /* grid_x */
106 arg_types
[5] = int32_type
; /* grid_y */
107 arg_types
[6] = int32_type
; /* grid_z */
108 arg_types
[7] = int32_type
; /* grid_size_x */
109 arg_types
[8] = int32_type
; /* grid_size_y */
110 arg_types
[9] = int32_type
; /* grid_size_z */
111 arg_types
[10] = int32_type
; /* work dim */
112 arg_types
[11] = variant
->jit_cs_thread_data_ptr_type
; /* per thread data */
113 arg_types
[12] = int32_type
; /* coro only - num X loops */
114 arg_types
[13] = int32_type
; /* coro only - partials */
115 func_type
= LLVMFunctionType(LLVMVoidTypeInContext(gallivm
->context
),
116 arg_types
, ARRAY_SIZE(arg_types
) - 5, 0);
118 coro_func_type
= LLVMFunctionType(LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0),
119 arg_types
, ARRAY_SIZE(arg_types
), 0);
121 function
= LLVMAddFunction(gallivm
->module
, func_name
, func_type
);
122 LLVMSetFunctionCallConv(function
, LLVMCCallConv
);
124 coro
= LLVMAddFunction(gallivm
->module
, func_name_coro
, coro_func_type
);
125 LLVMSetFunctionCallConv(coro
, LLVMCCallConv
);
127 variant
->function
= function
;
129 for(i
= 0; i
< ARRAY_SIZE(arg_types
); ++i
) {
130 if(LLVMGetTypeKind(arg_types
[i
]) == LLVMPointerTypeKind
) {
131 lp_add_function_attr(coro
, i
+ 1, LP_FUNC_ATTR_NOALIAS
);
132 lp_add_function_attr(function
, i
+ 1, LP_FUNC_ATTR_NOALIAS
);
136 context_ptr
= LLVMGetParam(function
, 0);
137 x_size_arg
= LLVMGetParam(function
, 1);
138 y_size_arg
= LLVMGetParam(function
, 2);
139 z_size_arg
= LLVMGetParam(function
, 3);
140 grid_x_arg
= LLVMGetParam(function
, 4);
141 grid_y_arg
= LLVMGetParam(function
, 5);
142 grid_z_arg
= LLVMGetParam(function
, 6);
143 grid_size_x_arg
= LLVMGetParam(function
, 7);
144 grid_size_y_arg
= LLVMGetParam(function
, 8);
145 grid_size_z_arg
= LLVMGetParam(function
, 9);
146 work_dim_arg
= LLVMGetParam(function
, 10);
147 thread_data_ptr
= LLVMGetParam(function
, 11);
149 lp_build_name(context_ptr
, "context");
150 lp_build_name(x_size_arg
, "x_size");
151 lp_build_name(y_size_arg
, "y_size");
152 lp_build_name(z_size_arg
, "z_size");
153 lp_build_name(grid_x_arg
, "grid_x");
154 lp_build_name(grid_y_arg
, "grid_y");
155 lp_build_name(grid_z_arg
, "grid_z");
156 lp_build_name(grid_size_x_arg
, "grid_size_x");
157 lp_build_name(grid_size_y_arg
, "grid_size_y");
158 lp_build_name(grid_size_z_arg
, "grid_size_z");
159 lp_build_name(work_dim_arg
, "work_dim");
160 lp_build_name(thread_data_ptr
, "thread_data");
162 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, function
, "entry");
163 builder
= gallivm
->builder
;
165 LLVMPositionBuilderAtEnd(builder
, block
);
166 sampler
= lp_llvm_sampler_soa_create(key
->state
);
167 image
= lp_llvm_image_soa_create(key
->image_state
);
169 struct lp_build_loop_state loop_state
[4];
170 LLVMValueRef num_x_loop
;
171 LLVMValueRef vec_length
= lp_build_const_int32(gallivm
, cs_type
.length
);
172 num_x_loop
= LLVMBuildAdd(gallivm
->builder
, x_size_arg
, vec_length
, "");
173 num_x_loop
= LLVMBuildSub(gallivm
->builder
, num_x_loop
, lp_build_const_int32(gallivm
, 1), "");
174 num_x_loop
= LLVMBuildUDiv(gallivm
->builder
, num_x_loop
, vec_length
, "");
175 LLVMValueRef partials
= LLVMBuildURem(gallivm
->builder
, x_size_arg
, vec_length
, "");
177 LLVMValueRef coro_num_hdls
= LLVMBuildMul(gallivm
->builder
, num_x_loop
, y_size_arg
, "");
178 coro_num_hdls
= LLVMBuildMul(gallivm
->builder
, coro_num_hdls
, z_size_arg
, "");
180 LLVMTypeRef hdl_ptr_type
= LLVMPointerType(LLVMInt8TypeInContext(gallivm
->context
), 0);
181 LLVMValueRef coro_hdls
= LLVMBuildArrayAlloca(gallivm
->builder
, hdl_ptr_type
, coro_num_hdls
, "coro_hdls");
183 unsigned end_coroutine
= INT_MAX
;
186 * This is the main coroutine execution loop. It iterates over the dimensions
187 * and calls the coroutine main entrypoint on the first pass, but in subsequent
188 * passes it checks if the coroutine has completed and resumes it if not.
190 /* take x_width - round up to type.length width */
191 lp_build_loop_begin(&loop_state
[3], gallivm
,
192 lp_build_const_int32(gallivm
, 0)); /* coroutine reentry loop */
193 lp_build_loop_begin(&loop_state
[2], gallivm
,
194 lp_build_const_int32(gallivm
, 0)); /* z loop */
195 lp_build_loop_begin(&loop_state
[1], gallivm
,
196 lp_build_const_int32(gallivm
, 0)); /* y loop */
197 lp_build_loop_begin(&loop_state
[0], gallivm
,
198 lp_build_const_int32(gallivm
, 0)); /* x loop */
200 LLVMValueRef args
[14];
201 args
[0] = context_ptr
;
202 args
[1] = loop_state
[0].counter
;
203 args
[2] = loop_state
[1].counter
;
204 args
[3] = loop_state
[2].counter
;
205 args
[4] = grid_x_arg
;
206 args
[5] = grid_y_arg
;
207 args
[6] = grid_z_arg
;
208 args
[7] = grid_size_x_arg
;
209 args
[8] = grid_size_y_arg
;
210 args
[9] = grid_size_z_arg
;
211 args
[10] = work_dim_arg
;
212 args
[11] = thread_data_ptr
;
213 args
[12] = num_x_loop
;
216 /* idx = (z * (size_x * size_y) + y * size_x + x */
217 LLVMValueRef coro_hdl_idx
= LLVMBuildMul(gallivm
->builder
, loop_state
[2].counter
,
218 LLVMBuildMul(gallivm
->builder
, num_x_loop
, y_size_arg
, ""), "");
219 coro_hdl_idx
= LLVMBuildAdd(gallivm
->builder
, coro_hdl_idx
,
220 LLVMBuildMul(gallivm
->builder
, loop_state
[1].counter
,
221 num_x_loop
, ""), "");
222 coro_hdl_idx
= LLVMBuildAdd(gallivm
->builder
, coro_hdl_idx
,
223 loop_state
[0].counter
, "");
225 LLVMValueRef coro_entry
= LLVMBuildGEP(gallivm
->builder
, coro_hdls
, &coro_hdl_idx
, 1, "");
227 LLVMValueRef coro_hdl
= LLVMBuildLoad(gallivm
->builder
, coro_entry
, "coro_hdl");
229 struct lp_build_if_state ifstate
;
230 LLVMValueRef cmp
= LLVMBuildICmp(gallivm
->builder
, LLVMIntEQ
, loop_state
[3].counter
,
231 lp_build_const_int32(gallivm
, 0), "");
232 /* first time here - call the coroutine function entry point */
233 lp_build_if(&ifstate
, gallivm
, cmp
);
234 LLVMValueRef coro_ret
= LLVMBuildCall(gallivm
->builder
, coro
, args
, 14, "");
235 LLVMBuildStore(gallivm
->builder
, coro_ret
, coro_entry
);
236 lp_build_else(&ifstate
);
237 /* subsequent calls for this invocation - check if done. */
238 LLVMValueRef coro_done
= lp_build_coro_done(gallivm
, coro_hdl
);
239 struct lp_build_if_state ifstate2
;
240 lp_build_if(&ifstate2
, gallivm
, coro_done
);
241 /* if done destroy and force loop exit */
242 lp_build_coro_destroy(gallivm
, coro_hdl
);
243 lp_build_loop_force_set_counter(&loop_state
[3], lp_build_const_int32(gallivm
, end_coroutine
- 1));
244 lp_build_else(&ifstate2
);
245 /* otherwise resume the coroutine */
246 lp_build_coro_resume(gallivm
, coro_hdl
);
247 lp_build_endif(&ifstate2
);
248 lp_build_endif(&ifstate
);
249 lp_build_loop_force_reload_counter(&loop_state
[3]);
251 lp_build_loop_end_cond(&loop_state
[0],
254 lp_build_loop_end_cond(&loop_state
[1],
257 lp_build_loop_end_cond(&loop_state
[2],
260 lp_build_loop_end_cond(&loop_state
[3],
261 lp_build_const_int32(gallivm
, end_coroutine
),
263 LLVMBuildRetVoid(builder
);
265 /* This is stage (b) - generate the compute shader code inside the coroutine. */
266 context_ptr
= LLVMGetParam(coro
, 0);
267 x_size_arg
= LLVMGetParam(coro
, 1);
268 y_size_arg
= LLVMGetParam(coro
, 2);
269 z_size_arg
= LLVMGetParam(coro
, 3);
270 grid_x_arg
= LLVMGetParam(coro
, 4);
271 grid_y_arg
= LLVMGetParam(coro
, 5);
272 grid_z_arg
= LLVMGetParam(coro
, 6);
273 grid_size_x_arg
= LLVMGetParam(coro
, 7);
274 grid_size_y_arg
= LLVMGetParam(coro
, 8);
275 grid_size_z_arg
= LLVMGetParam(coro
, 9);
276 work_dim_arg
= LLVMGetParam(coro
, 10);
277 thread_data_ptr
= LLVMGetParam(coro
, 11);
278 num_x_loop
= LLVMGetParam(coro
, 12);
279 partials
= LLVMGetParam(coro
, 13);
280 block
= LLVMAppendBasicBlockInContext(gallivm
->context
, coro
, "entry");
281 LLVMPositionBuilderAtEnd(builder
, block
);
283 LLVMValueRef consts_ptr
, num_consts_ptr
;
284 LLVMValueRef ssbo_ptr
, num_ssbo_ptr
;
285 LLVMValueRef shared_ptr
;
286 LLVMValueRef kernel_args_ptr
;
287 struct lp_build_mask_context mask
;
288 struct lp_bld_tgsi_system_values system_values
;
290 memset(&system_values
, 0, sizeof(system_values
));
291 consts_ptr
= lp_jit_cs_context_constants(gallivm
, context_ptr
);
292 num_consts_ptr
= lp_jit_cs_context_num_constants(gallivm
, context_ptr
);
293 ssbo_ptr
= lp_jit_cs_context_ssbos(gallivm
, context_ptr
);
294 num_ssbo_ptr
= lp_jit_cs_context_num_ssbos(gallivm
, context_ptr
);
295 kernel_args_ptr
= lp_jit_cs_context_kernel_args(gallivm
, context_ptr
);
297 shared_ptr
= lp_jit_cs_thread_data_shared(gallivm
, thread_data_ptr
);
299 /* these are coroutine entrypoint necessities */
300 LLVMValueRef coro_id
= lp_build_coro_id(gallivm
);
301 LLVMValueRef coro_hdl
= lp_build_coro_begin_alloc_mem(gallivm
, coro_id
);
303 LLVMValueRef has_partials
= LLVMBuildICmp(gallivm
->builder
, LLVMIntNE
, partials
, lp_build_const_int32(gallivm
, 0), "");
304 LLVMValueRef tid_vals
[3];
305 LLVMValueRef tids_x
[LP_MAX_VECTOR_LENGTH
], tids_y
[LP_MAX_VECTOR_LENGTH
], tids_z
[LP_MAX_VECTOR_LENGTH
];
306 LLVMValueRef base_val
= LLVMBuildMul(gallivm
->builder
, x_size_arg
, vec_length
, "");
307 for (i
= 0; i
< cs_type
.length
; i
++) {
308 tids_x
[i
] = LLVMBuildAdd(gallivm
->builder
, base_val
, lp_build_const_int32(gallivm
, i
), "");
309 tids_y
[i
] = y_size_arg
;
310 tids_z
[i
] = z_size_arg
;
312 tid_vals
[0] = lp_build_gather_values(gallivm
, tids_x
, cs_type
.length
);
313 tid_vals
[1] = lp_build_gather_values(gallivm
, tids_y
, cs_type
.length
);
314 tid_vals
[2] = lp_build_gather_values(gallivm
, tids_z
, cs_type
.length
);
315 system_values
.thread_id
= LLVMGetUndef(LLVMArrayType(LLVMVectorType(int32_type
, cs_type
.length
), 3));
316 for (i
= 0; i
< 3; i
++)
317 system_values
.thread_id
= LLVMBuildInsertValue(builder
, system_values
.thread_id
, tid_vals
[i
], i
, "");
319 LLVMValueRef gtids
[3] = { grid_x_arg
, grid_y_arg
, grid_z_arg
};
320 system_values
.block_id
= LLVMGetUndef(LLVMVectorType(int32_type
, 3));
321 for (i
= 0; i
< 3; i
++)
322 system_values
.block_id
= LLVMBuildInsertElement(builder
, system_values
.block_id
, gtids
[i
], lp_build_const_int32(gallivm
, i
), "");
324 LLVMValueRef gstids
[3] = { grid_size_x_arg
, grid_size_y_arg
, grid_size_z_arg
};
325 system_values
.grid_size
= LLVMGetUndef(LLVMVectorType(int32_type
, 3));
326 for (i
= 0; i
< 3; i
++)
327 system_values
.grid_size
= LLVMBuildInsertElement(builder
, system_values
.grid_size
, gstids
[i
], lp_build_const_int32(gallivm
, i
), "");
329 system_values
.work_dim
= work_dim_arg
;
331 LLVMValueRef last_x_loop
= LLVMBuildICmp(gallivm
->builder
, LLVMIntEQ
, x_size_arg
, LLVMBuildSub(gallivm
->builder
, num_x_loop
, lp_build_const_int32(gallivm
, 1), ""), "");
332 LLVMValueRef use_partial_mask
= LLVMBuildAnd(gallivm
->builder
, last_x_loop
, has_partials
, "");
333 struct lp_build_if_state if_state
;
334 LLVMValueRef mask_val
= lp_build_alloca(gallivm
, LLVMVectorType(int32_type
, cs_type
.length
), "mask");
335 LLVMValueRef full_mask_val
= lp_build_const_int_vec(gallivm
, cs_type
, ~0);
336 LLVMBuildStore(gallivm
->builder
, full_mask_val
, mask_val
);
338 lp_build_if(&if_state
, gallivm
, use_partial_mask
);
339 struct lp_build_loop_state mask_loop_state
;
340 lp_build_loop_begin(&mask_loop_state
, gallivm
, partials
);
341 LLVMValueRef tmask_val
= LLVMBuildLoad(gallivm
->builder
, mask_val
, "");
342 tmask_val
= LLVMBuildInsertElement(gallivm
->builder
, tmask_val
, lp_build_const_int32(gallivm
, 0), mask_loop_state
.counter
, "");
343 LLVMBuildStore(gallivm
->builder
, tmask_val
, mask_val
);
344 lp_build_loop_end_cond(&mask_loop_state
, vec_length
, NULL
, LLVMIntUGE
);
345 lp_build_endif(&if_state
);
347 mask_val
= LLVMBuildLoad(gallivm
->builder
, mask_val
, "");
348 lp_build_mask_begin(&mask
, gallivm
, cs_type
, mask_val
);
350 struct lp_build_coro_suspend_info coro_info
;
352 LLVMBasicBlockRef sus_block
= LLVMAppendBasicBlockInContext(gallivm
->context
, coro
, "suspend");
353 LLVMBasicBlockRef clean_block
= LLVMAppendBasicBlockInContext(gallivm
->context
, coro
, "cleanup");
355 coro_info
.suspend
= sus_block
;
356 coro_info
.cleanup
= clean_block
;
358 struct lp_build_tgsi_params params
;
359 memset(¶ms
, 0, sizeof(params
));
361 params
.type
= cs_type
;
363 params
.consts_ptr
= consts_ptr
;
364 params
.const_sizes_ptr
= num_consts_ptr
;
365 params
.system_values
= &system_values
;
366 params
.context_ptr
= context_ptr
;
367 params
.sampler
= sampler
;
368 params
.info
= &shader
->info
.base
;
369 params
.ssbo_ptr
= ssbo_ptr
;
370 params
.ssbo_sizes_ptr
= num_ssbo_ptr
;
371 params
.image
= image
;
372 params
.shared_ptr
= shared_ptr
;
373 params
.coro
= &coro_info
;
374 params
.kernel_args
= kernel_args_ptr
;
376 if (shader
->base
.type
== PIPE_SHADER_IR_TGSI
)
377 lp_build_tgsi_soa(gallivm
, shader
->base
.tokens
, ¶ms
, NULL
);
379 lp_build_nir_soa(gallivm
, shader
->base
.ir
.nir
, ¶ms
,
382 mask_val
= lp_build_mask_end(&mask
);
384 lp_build_coro_suspend_switch(gallivm
, &coro_info
, NULL
, true);
385 LLVMPositionBuilderAtEnd(builder
, clean_block
);
387 lp_build_coro_free_mem(gallivm
, coro_id
, coro_hdl
);
389 LLVMBuildBr(builder
, sus_block
);
390 LLVMPositionBuilderAtEnd(builder
, sus_block
);
392 lp_build_coro_end(gallivm
, coro_hdl
);
393 LLVMBuildRet(builder
, coro_hdl
);
396 sampler
->destroy(sampler
);
397 image
->destroy(image
);
399 gallivm_verify_function(gallivm
, coro
);
400 gallivm_verify_function(gallivm
, function
);
404 llvmpipe_create_compute_state(struct pipe_context
*pipe
,
405 const struct pipe_compute_state
*templ
)
407 struct lp_compute_shader
*shader
;
408 int nr_samplers
, nr_sampler_views
;
409 shader
= CALLOC_STRUCT(lp_compute_shader
);
413 shader
->base
.type
= templ
->ir_type
;
414 if (templ
->ir_type
== PIPE_SHADER_IR_TGSI
) {
415 /* get/save the summary info for this shader */
416 lp_build_tgsi_info(templ
->prog
, &shader
->info
);
418 /* we need to keep a local copy of the tokens */
419 shader
->base
.tokens
= tgsi_dup_tokens(templ
->prog
);
421 shader
->base
.ir
.nir
= (struct nir_shader
*)templ
->prog
;
422 nir_tgsi_scan_shader(templ
->prog
, &shader
->info
.base
, false);
425 shader
->req_local_mem
= templ
->req_local_mem
;
426 make_empty_list(&shader
->variants
);
428 nr_samplers
= shader
->info
.base
.file_max
[TGSI_FILE_SAMPLER
] + 1;
429 nr_sampler_views
= shader
->info
.base
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
430 shader
->variant_key_size
= Offset(struct lp_compute_shader_variant_key
,
431 state
[MAX2(nr_samplers
, nr_sampler_views
)]);
436 llvmpipe_bind_compute_state(struct pipe_context
*pipe
,
439 struct llvmpipe_context
*llvmpipe
= llvmpipe_context(pipe
);
441 if (llvmpipe
->cs
== cs
)
444 llvmpipe
->cs
= (struct lp_compute_shader
*)cs
;
445 llvmpipe
->cs_dirty
|= LP_CSNEW_CS
;
449 * Remove shader variant from two lists: the shader's variant list
450 * and the context's variant list.
453 llvmpipe_remove_cs_shader_variant(struct llvmpipe_context
*lp
,
454 struct lp_compute_shader_variant
*variant
)
456 if ((LP_DEBUG
& DEBUG_CS
) || (gallivm_debug
& GALLIVM_DEBUG_IR
)) {
457 debug_printf("llvmpipe: del cs #%u var %u v created %u v cached %u "
458 "v total cached %u inst %u total inst %u\n",
459 variant
->shader
->no
, variant
->no
,
460 variant
->shader
->variants_created
,
461 variant
->shader
->variants_cached
,
462 lp
->nr_cs_variants
, variant
->nr_instrs
, lp
->nr_cs_instrs
);
465 gallivm_destroy(variant
->gallivm
);
467 /* remove from shader's list */
468 remove_from_list(&variant
->list_item_local
);
469 variant
->shader
->variants_cached
--;
471 /* remove from context's list */
472 remove_from_list(&variant
->list_item_global
);
473 lp
->nr_fs_variants
--;
474 lp
->nr_fs_instrs
-= variant
->nr_instrs
;
480 llvmpipe_delete_compute_state(struct pipe_context
*pipe
,
483 struct llvmpipe_context
*llvmpipe
= llvmpipe_context(pipe
);
484 struct lp_compute_shader
*shader
= cs
;
485 struct lp_cs_variant_list_item
*li
;
487 /* Delete all the variants */
488 li
= first_elem(&shader
->variants
);
489 while(!at_end(&shader
->variants
, li
)) {
490 struct lp_cs_variant_list_item
*next
= next_elem(li
);
491 llvmpipe_remove_cs_shader_variant(llvmpipe
, li
->base
);
494 tgsi_free_tokens(shader
->base
.tokens
);
499 make_variant_key(struct llvmpipe_context
*lp
,
500 struct lp_compute_shader
*shader
,
501 struct lp_compute_shader_variant_key
*key
)
505 memset(key
, 0, shader
->variant_key_size
);
507 /* This value will be the same for all the variants of a given shader:
509 key
->nr_samplers
= shader
->info
.base
.file_max
[TGSI_FILE_SAMPLER
] + 1;
511 for(i
= 0; i
< key
->nr_samplers
; ++i
) {
512 if(shader
->info
.base
.file_mask
[TGSI_FILE_SAMPLER
] & (1 << i
)) {
513 lp_sampler_static_sampler_state(&key
->state
[i
].sampler_state
,
514 lp
->samplers
[PIPE_SHADER_COMPUTE
][i
]);
519 * XXX If TGSI_FILE_SAMPLER_VIEW exists assume all texture opcodes
520 * are dx10-style? Can't really have mixed opcodes, at least not
521 * if we want to skip the holes here (without rescanning tgsi).
523 if (shader
->info
.base
.file_max
[TGSI_FILE_SAMPLER_VIEW
] != -1) {
524 key
->nr_sampler_views
= shader
->info
.base
.file_max
[TGSI_FILE_SAMPLER_VIEW
] + 1;
525 for(i
= 0; i
< key
->nr_sampler_views
; ++i
) {
527 * Note sview may exceed what's representable by file_mask.
528 * This will still work, the only downside is that not actually
529 * used views may be included in the shader key.
531 if(shader
->info
.base
.file_mask
[TGSI_FILE_SAMPLER_VIEW
] & (1u << (i
& 31))) {
532 lp_sampler_static_texture_state(&key
->state
[i
].texture_state
,
533 lp
->sampler_views
[PIPE_SHADER_COMPUTE
][i
]);
538 key
->nr_sampler_views
= key
->nr_samplers
;
539 for(i
= 0; i
< key
->nr_sampler_views
; ++i
) {
540 if(shader
->info
.base
.file_mask
[TGSI_FILE_SAMPLER
] & (1 << i
)) {
541 lp_sampler_static_texture_state(&key
->state
[i
].texture_state
,
542 lp
->sampler_views
[PIPE_SHADER_COMPUTE
][i
]);
547 key
->nr_images
= shader
->info
.base
.file_max
[TGSI_FILE_IMAGE
] + 1;
548 for (i
= 0; i
< key
->nr_images
; ++i
) {
549 if (shader
->info
.base
.file_mask
[TGSI_FILE_IMAGE
] & (1 << i
)) {
550 lp_sampler_static_texture_state_image(&key
->image_state
[i
].image_state
,
551 &lp
->images
[PIPE_SHADER_COMPUTE
][i
]);
557 dump_cs_variant_key(const struct lp_compute_shader_variant_key
*key
)
560 debug_printf("cs variant %p:\n", (void *) key
);
562 for (i
= 0; i
< key
->nr_samplers
; ++i
) {
563 const struct lp_static_sampler_state
*sampler
= &key
->state
[i
].sampler_state
;
564 debug_printf("sampler[%u] = \n", i
);
565 debug_printf(" .wrap = %s %s %s\n",
566 util_str_tex_wrap(sampler
->wrap_s
, TRUE
),
567 util_str_tex_wrap(sampler
->wrap_t
, TRUE
),
568 util_str_tex_wrap(sampler
->wrap_r
, TRUE
));
569 debug_printf(" .min_img_filter = %s\n",
570 util_str_tex_filter(sampler
->min_img_filter
, TRUE
));
571 debug_printf(" .min_mip_filter = %s\n",
572 util_str_tex_mipfilter(sampler
->min_mip_filter
, TRUE
));
573 debug_printf(" .mag_img_filter = %s\n",
574 util_str_tex_filter(sampler
->mag_img_filter
, TRUE
));
575 if (sampler
->compare_mode
!= PIPE_TEX_COMPARE_NONE
)
576 debug_printf(" .compare_func = %s\n", util_str_func(sampler
->compare_func
, TRUE
));
577 debug_printf(" .normalized_coords = %u\n", sampler
->normalized_coords
);
578 debug_printf(" .min_max_lod_equal = %u\n", sampler
->min_max_lod_equal
);
579 debug_printf(" .lod_bias_non_zero = %u\n", sampler
->lod_bias_non_zero
);
580 debug_printf(" .apply_min_lod = %u\n", sampler
->apply_min_lod
);
581 debug_printf(" .apply_max_lod = %u\n", sampler
->apply_max_lod
);
583 for (i
= 0; i
< key
->nr_sampler_views
; ++i
) {
584 const struct lp_static_texture_state
*texture
= &key
->state
[i
].texture_state
;
585 debug_printf("texture[%u] = \n", i
);
586 debug_printf(" .format = %s\n",
587 util_format_name(texture
->format
));
588 debug_printf(" .target = %s\n",
589 util_str_tex_target(texture
->target
, TRUE
));
590 debug_printf(" .level_zero_only = %u\n",
591 texture
->level_zero_only
);
592 debug_printf(" .pot = %u %u %u\n",
597 for (i
= 0; i
< key
->nr_images
; ++i
) {
598 const struct lp_static_texture_state
*image
= &key
->image_state
[i
].image_state
;
599 debug_printf("image[%u] = \n", i
);
600 debug_printf(" .format = %s\n",
601 util_format_name(image
->format
));
602 debug_printf(" .target = %s\n",
603 util_str_tex_target(image
->target
, TRUE
));
604 debug_printf(" .level_zero_only = %u\n",
605 image
->level_zero_only
);
606 debug_printf(" .pot = %u %u %u\n",
614 lp_debug_cs_variant(const struct lp_compute_shader_variant
*variant
)
616 debug_printf("llvmpipe: Compute shader #%u variant #%u:\n",
617 variant
->shader
->no
, variant
->no
);
618 if (variant
->shader
->base
.type
== PIPE_SHADER_IR_TGSI
)
619 tgsi_dump(variant
->shader
->base
.tokens
, 0);
621 nir_print_shader(variant
->shader
->base
.ir
.nir
, stderr
);
622 dump_cs_variant_key(&variant
->key
);
626 static struct lp_compute_shader_variant
*
627 generate_variant(struct llvmpipe_context
*lp
,
628 struct lp_compute_shader
*shader
,
629 const struct lp_compute_shader_variant_key
*key
)
631 struct lp_compute_shader_variant
*variant
;
632 char module_name
[64];
634 variant
= CALLOC_STRUCT(lp_compute_shader_variant
);
638 snprintf(module_name
, sizeof(module_name
), "cs%u_variant%u",
639 shader
->no
, shader
->variants_created
);
641 variant
->gallivm
= gallivm_create(module_name
, lp
->context
);
642 if (!variant
->gallivm
) {
647 variant
->shader
= shader
;
648 variant
->list_item_global
.base
= variant
;
649 variant
->list_item_local
.base
= variant
;
650 variant
->no
= shader
->variants_created
++;
652 memcpy(&variant
->key
, key
, shader
->variant_key_size
);
654 if ((LP_DEBUG
& DEBUG_CS
) || (gallivm_debug
& GALLIVM_DEBUG_IR
)) {
655 lp_debug_cs_variant(variant
);
658 lp_jit_init_cs_types(variant
);
660 generate_compute(lp
, shader
, variant
);
662 gallivm_compile_module(variant
->gallivm
);
664 variant
->nr_instrs
+= lp_build_count_ir_module(variant
->gallivm
->module
);
666 variant
->jit_function
= (lp_jit_cs_func
)gallivm_jit_function(variant
->gallivm
, variant
->function
);
668 gallivm_free_ir(variant
->gallivm
);
673 lp_cs_ctx_set_cs_variant( struct lp_cs_context
*csctx
,
674 struct lp_compute_shader_variant
*variant
)
676 csctx
->cs
.current
.variant
= variant
;
680 llvmpipe_update_cs(struct llvmpipe_context
*lp
)
682 struct lp_compute_shader
*shader
= lp
->cs
;
684 struct lp_compute_shader_variant_key key
;
685 struct lp_compute_shader_variant
*variant
= NULL
;
686 struct lp_cs_variant_list_item
*li
;
688 make_variant_key(lp
, shader
, &key
);
690 /* Search the variants for one which matches the key */
691 li
= first_elem(&shader
->variants
);
692 while(!at_end(&shader
->variants
, li
)) {
693 if(memcmp(&li
->base
->key
, &key
, shader
->variant_key_size
) == 0) {
701 /* Move this variant to the head of the list to implement LRU
702 * deletion of shader's when we have too many.
704 move_to_head(&lp
->cs_variants_list
, &variant
->list_item_global
);
707 /* variant not found, create it now */
710 unsigned variants_to_cull
;
712 if (LP_DEBUG
& DEBUG_CS
) {
713 debug_printf("%u variants,\t%u instrs,\t%u instrs/variant\n",
716 lp
->nr_cs_variants
? lp
->nr_cs_instrs
/ lp
->nr_cs_variants
: 0);
719 /* First, check if we've exceeded the max number of shader variants.
720 * If so, free 6.25% of them (the least recently used ones).
722 variants_to_cull
= lp
->nr_cs_variants
>= LP_MAX_SHADER_VARIANTS
? LP_MAX_SHADER_VARIANTS
/ 16 : 0;
724 if (variants_to_cull
||
725 lp
->nr_cs_instrs
>= LP_MAX_SHADER_INSTRUCTIONS
) {
726 if (gallivm_debug
& GALLIVM_DEBUG_PERF
) {
727 debug_printf("Evicting CS: %u cs variants,\t%u total variants,"
728 "\t%u instrs,\t%u instrs/variant\n",
729 shader
->variants_cached
,
730 lp
->nr_cs_variants
, lp
->nr_cs_instrs
,
731 lp
->nr_cs_instrs
/ lp
->nr_cs_variants
);
735 * We need to re-check lp->nr_cs_variants because an arbitrarliy large
736 * number of shader variants (potentially all of them) could be
737 * pending for destruction on flush.
740 for (i
= 0; i
< variants_to_cull
|| lp
->nr_cs_instrs
>= LP_MAX_SHADER_INSTRUCTIONS
; i
++) {
741 struct lp_cs_variant_list_item
*item
;
742 if (is_empty_list(&lp
->cs_variants_list
)) {
745 item
= last_elem(&lp
->cs_variants_list
);
748 llvmpipe_remove_cs_shader_variant(lp
, item
->base
);
752 * Generate the new variant.
755 variant
= generate_variant(lp
, shader
, &key
);
758 LP_COUNT_ADD(llvm_compile_time
, dt
);
759 LP_COUNT_ADD(nr_llvm_compiles
, 2); /* emit vs. omit in/out test */
761 /* Put the new variant into the list */
763 insert_at_head(&shader
->variants
, &variant
->list_item_local
);
764 insert_at_head(&lp
->cs_variants_list
, &variant
->list_item_global
);
765 lp
->nr_cs_variants
++;
766 lp
->nr_cs_instrs
+= variant
->nr_instrs
;
767 shader
->variants_cached
++;
770 /* Bind this variant */
771 lp_cs_ctx_set_cs_variant(lp
->csctx
, variant
);
775 * Called during state validation when LP_CSNEW_SAMPLER_VIEW is set.
778 lp_csctx_set_sampler_views(struct lp_cs_context
*csctx
,
780 struct pipe_sampler_view
**views
)
782 unsigned i
, max_tex_num
;
784 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
786 assert(num
<= PIPE_MAX_SHADER_SAMPLER_VIEWS
);
788 max_tex_num
= MAX2(num
, csctx
->cs
.current_tex_num
);
790 for (i
= 0; i
< max_tex_num
; i
++) {
791 struct pipe_sampler_view
*view
= i
< num
? views
[i
] : NULL
;
794 struct pipe_resource
*res
= view
->texture
;
795 struct llvmpipe_resource
*lp_tex
= llvmpipe_resource(res
);
796 struct lp_jit_texture
*jit_tex
;
797 jit_tex
= &csctx
->cs
.current
.jit_context
.textures
[i
];
799 /* We're referencing the texture's internal data, so save a
802 pipe_resource_reference(&csctx
->cs
.current_tex
[i
], res
);
805 /* regular texture - csctx array of mipmap level offsets */
807 unsigned first_level
= 0;
808 unsigned last_level
= 0;
810 if (llvmpipe_resource_is_texture(res
)) {
811 first_level
= view
->u
.tex
.first_level
;
812 last_level
= view
->u
.tex
.last_level
;
813 assert(first_level
<= last_level
);
814 assert(last_level
<= res
->last_level
);
815 jit_tex
->base
= lp_tex
->tex_data
;
818 jit_tex
->base
= lp_tex
->data
;
820 if (LP_PERF
& PERF_TEX_MEM
) {
821 /* use dummy tile memory */
822 jit_tex
->base
= lp_dummy_tile
;
823 jit_tex
->width
= TILE_SIZE
/8;
824 jit_tex
->height
= TILE_SIZE
/8;
826 jit_tex
->first_level
= 0;
827 jit_tex
->last_level
= 0;
828 jit_tex
->mip_offsets
[0] = 0;
829 jit_tex
->row_stride
[0] = 0;
830 jit_tex
->img_stride
[0] = 0;
833 jit_tex
->width
= res
->width0
;
834 jit_tex
->height
= res
->height0
;
835 jit_tex
->depth
= res
->depth0
;
836 jit_tex
->first_level
= first_level
;
837 jit_tex
->last_level
= last_level
;
839 if (llvmpipe_resource_is_texture(res
)) {
840 for (j
= first_level
; j
<= last_level
; j
++) {
841 jit_tex
->mip_offsets
[j
] = lp_tex
->mip_offsets
[j
];
842 jit_tex
->row_stride
[j
] = lp_tex
->row_stride
[j
];
843 jit_tex
->img_stride
[j
] = lp_tex
->img_stride
[j
];
846 if (res
->target
== PIPE_TEXTURE_1D_ARRAY
||
847 res
->target
== PIPE_TEXTURE_2D_ARRAY
||
848 res
->target
== PIPE_TEXTURE_CUBE
||
849 res
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
851 * For array textures, we don't have first_layer, instead
852 * adjust last_layer (stored as depth) plus the mip level offsets
853 * (as we have mip-first layout can't just adjust base ptr).
854 * XXX For mip levels, could do something similar.
856 jit_tex
->depth
= view
->u
.tex
.last_layer
- view
->u
.tex
.first_layer
+ 1;
857 for (j
= first_level
; j
<= last_level
; j
++) {
858 jit_tex
->mip_offsets
[j
] += view
->u
.tex
.first_layer
*
859 lp_tex
->img_stride
[j
];
861 if (view
->target
== PIPE_TEXTURE_CUBE
||
862 view
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
863 assert(jit_tex
->depth
% 6 == 0);
865 assert(view
->u
.tex
.first_layer
<= view
->u
.tex
.last_layer
);
866 assert(view
->u
.tex
.last_layer
< res
->array_size
);
871 * For buffers, we don't have "offset", instead adjust
872 * the size (stored as width) plus the base pointer.
874 unsigned view_blocksize
= util_format_get_blocksize(view
->format
);
875 /* probably don't really need to fill that out */
876 jit_tex
->mip_offsets
[0] = 0;
877 jit_tex
->row_stride
[0] = 0;
878 jit_tex
->img_stride
[0] = 0;
880 /* everything specified in number of elements here. */
881 jit_tex
->width
= view
->u
.buf
.size
/ view_blocksize
;
882 jit_tex
->base
= (uint8_t *)jit_tex
->base
+ view
->u
.buf
.offset
;
883 /* XXX Unsure if we need to sanitize parameters? */
884 assert(view
->u
.buf
.offset
+ view
->u
.buf
.size
<= res
->width0
);
889 /* display target texture/surface */
891 * XXX: Where should this be unmapped?
893 struct llvmpipe_screen
*screen
= llvmpipe_screen(res
->screen
);
894 struct sw_winsys
*winsys
= screen
->winsys
;
895 jit_tex
->base
= winsys
->displaytarget_map(winsys
, lp_tex
->dt
,
897 jit_tex
->row_stride
[0] = lp_tex
->row_stride
[0];
898 jit_tex
->img_stride
[0] = lp_tex
->img_stride
[0];
899 jit_tex
->mip_offsets
[0] = 0;
900 jit_tex
->width
= res
->width0
;
901 jit_tex
->height
= res
->height0
;
902 jit_tex
->depth
= res
->depth0
;
903 jit_tex
->first_level
= jit_tex
->last_level
= 0;
904 assert(jit_tex
->base
);
908 pipe_resource_reference(&csctx
->cs
.current_tex
[i
], NULL
);
911 csctx
->cs
.current_tex_num
= num
;
916 * Called during state validation when LP_NEW_SAMPLER is set.
919 lp_csctx_set_sampler_state(struct lp_cs_context
*csctx
,
921 struct pipe_sampler_state
**samplers
)
925 LP_DBG(DEBUG_SETUP
, "%s\n", __FUNCTION__
);
927 assert(num
<= PIPE_MAX_SAMPLERS
);
929 for (i
= 0; i
< PIPE_MAX_SAMPLERS
; i
++) {
930 const struct pipe_sampler_state
*sampler
= i
< num
? samplers
[i
] : NULL
;
933 struct lp_jit_sampler
*jit_sam
;
934 jit_sam
= &csctx
->cs
.current
.jit_context
.samplers
[i
];
936 jit_sam
->min_lod
= sampler
->min_lod
;
937 jit_sam
->max_lod
= sampler
->max_lod
;
938 jit_sam
->lod_bias
= sampler
->lod_bias
;
939 COPY_4V(jit_sam
->border_color
, sampler
->border_color
.f
);
945 lp_csctx_set_cs_constants(struct lp_cs_context
*csctx
,
947 struct pipe_constant_buffer
*buffers
)
951 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) buffers
);
953 assert(num
<= ARRAY_SIZE(csctx
->constants
));
955 for (i
= 0; i
< num
; ++i
) {
956 util_copy_constant_buffer(&csctx
->constants
[i
].current
, &buffers
[i
]);
958 for (; i
< ARRAY_SIZE(csctx
->constants
); i
++) {
959 util_copy_constant_buffer(&csctx
->constants
[i
].current
, NULL
);
964 lp_csctx_set_cs_ssbos(struct lp_cs_context
*csctx
,
966 struct pipe_shader_buffer
*buffers
)
969 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *)buffers
);
971 assert (num
<= ARRAY_SIZE(csctx
->ssbos
));
973 for (i
= 0; i
< num
; ++i
) {
974 util_copy_shader_buffer(&csctx
->ssbos
[i
].current
, &buffers
[i
]);
976 for (; i
< ARRAY_SIZE(csctx
->ssbos
); i
++) {
977 util_copy_shader_buffer(&csctx
->ssbos
[i
].current
, NULL
);
982 lp_csctx_set_cs_images(struct lp_cs_context
*csctx
,
984 struct pipe_image_view
*images
)
988 LP_DBG(DEBUG_SETUP
, "%s %p\n", __FUNCTION__
, (void *) images
);
990 assert(num
<= ARRAY_SIZE(csctx
->images
));
992 for (i
= 0; i
< num
; ++i
) {
993 struct pipe_image_view
*image
= &images
[i
];
994 util_copy_image_view(&csctx
->images
[i
].current
, &images
[i
]);
996 struct pipe_resource
*res
= image
->resource
;
997 struct llvmpipe_resource
*lp_res
= llvmpipe_resource(res
);
998 struct lp_jit_image
*jit_image
;
1000 jit_image
= &csctx
->cs
.current
.jit_context
.images
[i
];
1004 /* regular texture - csctx array of mipmap level offsets */
1005 if (llvmpipe_resource_is_texture(res
)) {
1006 jit_image
->base
= lp_res
->tex_data
;
1008 jit_image
->base
= lp_res
->data
;
1010 jit_image
->width
= res
->width0
;
1011 jit_image
->height
= res
->height0
;
1012 jit_image
->depth
= res
->depth0
;
1014 if (llvmpipe_resource_is_texture(res
)) {
1015 uint32_t mip_offset
= lp_res
->mip_offsets
[image
->u
.tex
.level
];
1017 jit_image
->width
= u_minify(jit_image
->width
, image
->u
.tex
.level
);
1018 jit_image
->height
= u_minify(jit_image
->height
, image
->u
.tex
.level
);
1020 if (res
->target
== PIPE_TEXTURE_1D_ARRAY
||
1021 res
->target
== PIPE_TEXTURE_2D_ARRAY
||
1022 res
->target
== PIPE_TEXTURE_3D
||
1023 res
->target
== PIPE_TEXTURE_CUBE
||
1024 res
->target
== PIPE_TEXTURE_CUBE_ARRAY
) {
1026 * For array textures, we don't have first_layer, instead
1027 * adjust last_layer (stored as depth) plus the mip level offsets
1028 * (as we have mip-first layout can't just adjust base ptr).
1029 * XXX For mip levels, could do something similar.
1031 jit_image
->depth
= image
->u
.tex
.last_layer
- image
->u
.tex
.first_layer
+ 1;
1032 mip_offset
+= image
->u
.tex
.first_layer
* lp_res
->img_stride
[image
->u
.tex
.level
];
1034 jit_image
->depth
= u_minify(jit_image
->depth
, image
->u
.tex
.level
);
1036 jit_image
->row_stride
= lp_res
->row_stride
[image
->u
.tex
.level
];
1037 jit_image
->img_stride
= lp_res
->img_stride
[image
->u
.tex
.level
];
1038 jit_image
->base
= (uint8_t *)jit_image
->base
+ mip_offset
;
1040 unsigned view_blocksize
= util_format_get_blocksize(image
->format
);
1041 jit_image
->width
= image
->u
.buf
.size
/ view_blocksize
;
1042 jit_image
->base
= (uint8_t *)jit_image
->base
+ image
->u
.buf
.offset
;
1046 for (; i
< ARRAY_SIZE(csctx
->images
); i
++) {
1047 util_copy_image_view(&csctx
->images
[i
].current
, NULL
);
1052 update_csctx_consts(struct llvmpipe_context
*llvmpipe
)
1054 struct lp_cs_context
*csctx
= llvmpipe
->csctx
;
1057 for (i
= 0; i
< ARRAY_SIZE(csctx
->constants
); ++i
) {
1058 struct pipe_resource
*buffer
= csctx
->constants
[i
].current
.buffer
;
1059 const ubyte
*current_data
= NULL
;
1062 /* resource buffer */
1063 current_data
= (ubyte
*) llvmpipe_resource_data(buffer
);
1065 else if (csctx
->constants
[i
].current
.user_buffer
) {
1066 /* user-space buffer */
1067 current_data
= (ubyte
*) csctx
->constants
[i
].current
.user_buffer
;
1071 current_data
+= csctx
->constants
[i
].current
.buffer_offset
;
1073 csctx
->cs
.current
.jit_context
.constants
[i
] = (const float *)current_data
;
1074 csctx
->cs
.current
.jit_context
.num_constants
[i
] = csctx
->constants
[i
].current
.buffer_size
;
1076 csctx
->cs
.current
.jit_context
.constants
[i
] = NULL
;
1077 csctx
->cs
.current
.jit_context
.num_constants
[i
] = 0;
1083 update_csctx_ssbo(struct llvmpipe_context
*llvmpipe
)
1085 struct lp_cs_context
*csctx
= llvmpipe
->csctx
;
1087 for (i
= 0; i
< ARRAY_SIZE(csctx
->ssbos
); ++i
) {
1088 struct pipe_resource
*buffer
= csctx
->ssbos
[i
].current
.buffer
;
1089 const ubyte
*current_data
= NULL
;
1093 /* resource buffer */
1094 current_data
= (ubyte
*) llvmpipe_resource_data(buffer
);
1096 current_data
+= csctx
->ssbos
[i
].current
.buffer_offset
;
1098 csctx
->cs
.current
.jit_context
.ssbos
[i
] = (const uint32_t *)current_data
;
1099 csctx
->cs
.current
.jit_context
.num_ssbos
[i
] = csctx
->ssbos
[i
].current
.buffer_size
;
1101 csctx
->cs
.current
.jit_context
.ssbos
[i
] = NULL
;
1102 csctx
->cs
.current
.jit_context
.num_ssbos
[i
] = 0;
1108 llvmpipe_cs_update_derived(struct llvmpipe_context
*llvmpipe
, void *input
)
1110 if (llvmpipe
->cs_dirty
& (LP_CSNEW_CS
))
1111 llvmpipe_update_cs(llvmpipe
);
1113 if (llvmpipe
->cs_dirty
& LP_CSNEW_CONSTANTS
) {
1114 lp_csctx_set_cs_constants(llvmpipe
->csctx
,
1115 ARRAY_SIZE(llvmpipe
->constants
[PIPE_SHADER_COMPUTE
]),
1116 llvmpipe
->constants
[PIPE_SHADER_COMPUTE
]);
1117 update_csctx_consts(llvmpipe
);
1120 if (llvmpipe
->cs_dirty
& LP_CSNEW_SSBOS
) {
1121 lp_csctx_set_cs_ssbos(llvmpipe
->csctx
,
1122 ARRAY_SIZE(llvmpipe
->ssbos
[PIPE_SHADER_COMPUTE
]),
1123 llvmpipe
->ssbos
[PIPE_SHADER_COMPUTE
]);
1124 update_csctx_ssbo(llvmpipe
);
1127 if (llvmpipe
->cs_dirty
& LP_CSNEW_SAMPLER_VIEW
)
1128 lp_csctx_set_sampler_views(llvmpipe
->csctx
,
1129 llvmpipe
->num_sampler_views
[PIPE_SHADER_COMPUTE
],
1130 llvmpipe
->sampler_views
[PIPE_SHADER_COMPUTE
]);
1132 if (llvmpipe
->cs_dirty
& LP_CSNEW_SAMPLER
)
1133 lp_csctx_set_sampler_state(llvmpipe
->csctx
,
1134 llvmpipe
->num_samplers
[PIPE_SHADER_COMPUTE
],
1135 llvmpipe
->samplers
[PIPE_SHADER_COMPUTE
]);
1137 if (llvmpipe
->cs_dirty
& LP_CSNEW_IMAGES
)
1138 lp_csctx_set_cs_images(llvmpipe
->csctx
,
1139 ARRAY_SIZE(llvmpipe
->images
[PIPE_SHADER_COMPUTE
]),
1140 llvmpipe
->images
[PIPE_SHADER_COMPUTE
]);
1143 struct lp_cs_context
*csctx
= llvmpipe
->csctx
;
1144 csctx
->input
= input
;
1145 csctx
->cs
.current
.jit_context
.kernel_args
= input
;
1148 llvmpipe
->cs_dirty
= 0;
1152 cs_exec_fn(void *init_data
, int iter_idx
, struct lp_cs_local_mem
*lmem
)
1154 struct lp_cs_job_info
*job_info
= init_data
;
1155 struct lp_jit_cs_thread_data thread_data
;
1157 memset(&thread_data
, 0, sizeof(thread_data
));
1159 if (lmem
->local_size
< job_info
->req_local_mem
) {
1160 lmem
->local_mem_ptr
= REALLOC(lmem
->local_mem_ptr
, lmem
->local_size
,
1161 job_info
->req_local_mem
);
1162 lmem
->local_size
= job_info
->req_local_mem
;
1164 thread_data
.shared
= lmem
->local_mem_ptr
;
1166 unsigned grid_z
= iter_idx
/ (job_info
->grid_size
[0] * job_info
->grid_size
[1]);
1167 unsigned grid_y
= (iter_idx
- (grid_z
* (job_info
->grid_size
[0] * job_info
->grid_size
[1]))) / job_info
->grid_size
[0];
1168 unsigned grid_x
= (iter_idx
- (grid_z
* (job_info
->grid_size
[0] * job_info
->grid_size
[1])) - (grid_y
* job_info
->grid_size
[0]));
1169 struct lp_compute_shader_variant
*variant
= job_info
->current
->variant
;
1170 variant
->jit_function(&job_info
->current
->jit_context
,
1171 job_info
->block_size
[0], job_info
->block_size
[1], job_info
->block_size
[2],
1172 grid_x
, grid_y
, grid_z
,
1173 job_info
->grid_size
[0], job_info
->grid_size
[1], job_info
->grid_size
[2], job_info
->work_dim
,
1178 fill_grid_size(struct pipe_context
*pipe
,
1179 const struct pipe_grid_info
*info
,
1180 uint32_t grid_size
[3])
1182 struct pipe_transfer
*transfer
;
1184 if (!info
->indirect
) {
1185 grid_size
[0] = info
->grid
[0];
1186 grid_size
[1] = info
->grid
[1];
1187 grid_size
[2] = info
->grid
[2];
1190 params
= pipe_buffer_map_range(pipe
, info
->indirect
,
1191 info
->indirect_offset
,
1192 3 * sizeof(uint32_t),
1199 grid_size
[0] = params
[0];
1200 grid_size
[1] = params
[1];
1201 grid_size
[2] = params
[2];
1202 pipe_buffer_unmap(pipe
, transfer
);
1205 static void llvmpipe_launch_grid(struct pipe_context
*pipe
,
1206 const struct pipe_grid_info
*info
)
1208 struct llvmpipe_context
*llvmpipe
= llvmpipe_context(pipe
);
1209 struct llvmpipe_screen
*screen
= llvmpipe_screen(pipe
->screen
);
1210 struct lp_cs_job_info job_info
;
1212 memset(&job_info
, 0, sizeof(job_info
));
1214 llvmpipe_cs_update_derived(llvmpipe
, info
->input
);
1216 fill_grid_size(pipe
, info
, job_info
.grid_size
);
1218 job_info
.block_size
[0] = info
->block
[0];
1219 job_info
.block_size
[1] = info
->block
[1];
1220 job_info
.block_size
[2] = info
->block
[2];
1221 job_info
.work_dim
= info
->work_dim
;
1222 job_info
.req_local_mem
= llvmpipe
->cs
->req_local_mem
;
1223 job_info
.current
= &llvmpipe
->csctx
->cs
.current
;
1225 int num_tasks
= job_info
.grid_size
[2] * job_info
.grid_size
[1] * job_info
.grid_size
[0];
1227 struct lp_cs_tpool_task
*task
;
1228 mtx_lock(&screen
->cs_mutex
);
1229 task
= lp_cs_tpool_queue_task(screen
->cs_tpool
, cs_exec_fn
, &job_info
, num_tasks
);
1231 lp_cs_tpool_wait_for_task(screen
->cs_tpool
, &task
);
1232 mtx_unlock(&screen
->cs_mutex
);
1234 llvmpipe
->pipeline_statistics
.cs_invocations
+= num_tasks
* info
->block
[0] * info
->block
[1] * info
->block
[2];
1238 llvmpipe_init_compute_funcs(struct llvmpipe_context
*llvmpipe
)
1240 llvmpipe
->pipe
.create_compute_state
= llvmpipe_create_compute_state
;
1241 llvmpipe
->pipe
.bind_compute_state
= llvmpipe_bind_compute_state
;
1242 llvmpipe
->pipe
.delete_compute_state
= llvmpipe_delete_compute_state
;
1243 llvmpipe
->pipe
.launch_grid
= llvmpipe_launch_grid
;
1247 lp_csctx_destroy(struct lp_cs_context
*csctx
)
1250 for (i
= 0; i
< ARRAY_SIZE(csctx
->cs
.current_tex
); i
++) {
1251 pipe_resource_reference(&csctx
->cs
.current_tex
[i
], NULL
);
1253 for (i
= 0; i
< ARRAY_SIZE(csctx
->constants
); i
++) {
1254 pipe_resource_reference(&csctx
->constants
[i
].current
.buffer
, NULL
);
1256 for (i
= 0; i
< ARRAY_SIZE(csctx
->ssbos
); i
++) {
1257 pipe_resource_reference(&csctx
->ssbos
[i
].current
.buffer
, NULL
);
1262 struct lp_cs_context
*lp_csctx_create(struct pipe_context
*pipe
)
1264 struct lp_cs_context
*csctx
;
1266 csctx
= CALLOC_STRUCT(lp_cs_context
);