2
* Copyright © 2023 Valve Corporation
4
* Permission is hereby granted, free of charge, to any person obtaining a
5
* copy of this software and associated documentation files (the "Software"),
6
* to deal in the Software without restriction, including without limitation
7
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
* and/or sell copies of the Software, and to permit persons to whom the
9
* Software is furnished to do so, subject to the following conditions:
11
* The above copyright notice and this permission notice (including the next
12
* paragraph) shall be included in all copies or substantial portions of the
15
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24
#include "lp_context.h"
25
#include "lp_texture_handle.h"
26
#include "lp_screen.h"
28
#include "gallivm/lp_bld_const.h"
29
#include "gallivm/lp_bld_debug.h"
30
#include "gallivm/lp_bld_nir.h"
33
#include "nir_builder.h"
35
#include "pipe/p_context.h"
36
#include "pipe/p_screen.h"
37
#include "util/mesa-sha1.h"
39
static const char *image_function_base_hash = "8ca89d7a4ab5830be6a1ba1140844081235b01164a8fce8316ca6a2f81f1a899";
40
static const char *sample_function_base_hash = "0789b032c4a1ddba086e07496fe2a992b1ee08f78c0884a2923564b1ed52b9cc";
41
static const char *size_function_base_hash = "6d249ab9c1106c68b87ec9fdb5ade28368171d27f221c687f32ae1544231d2fe";
44
llvmpipe_register_texture(struct llvmpipe_context *ctx, struct lp_static_texture_state *state, bool sampled);
47
llvmpipe_register_sampler(struct llvmpipe_context *ctx, struct lp_static_sampler_state *state);
50
llvmpipe_create_texture_handle(struct pipe_context *pctx, struct pipe_sampler_view *view, const struct pipe_sampler_state *sampler)
52
struct llvmpipe_context *ctx = llvmpipe_context(pctx);
53
struct lp_sampler_matrix *matrix = &ctx->sampler_matrix;
55
struct lp_texture_handle *handle = calloc(1, sizeof(struct lp_texture_handle));
58
struct lp_static_texture_state state;
59
lp_sampler_static_texture_state(&state, view);
61
/* Trade a bit of performance for potentially less sampler/texture combinations. */
62
state.pot_width = false;
63
state.pot_height = false;
64
state.pot_depth = false;
66
llvmpipe_register_texture(ctx, &state, true);
69
for (uint32_t i = 0; i < matrix->texture_count; i++) {
70
if (!memcmp(&matrix->textures[i]->state, &state, sizeof(struct lp_static_texture_state))) {
71
handle->functions = matrix->textures[i];
80
struct lp_static_sampler_state state;
81
lp_sampler_static_sampler_state(&state, sampler);
83
llvmpipe_register_sampler(ctx, &state);
86
for (uint32_t i = 0; i < matrix->sampler_count; i++) {
87
if (!memcmp(matrix->samplers + i, &state, sizeof(struct lp_static_sampler_state))) {
88
handle->sampler_index = i;
96
return (uint64_t)(uintptr_t)handle;
100
llvmpipe_delete_texture_handle(struct pipe_context *pctx, uint64_t _handle)
105
struct lp_texture_handle *handle = (void *)(uintptr_t)_handle;
107
struct lp_texture_functions *functions = handle->functions;
109
assert(functions->ref_count);
110
functions->ref_count--;
117
llvmpipe_create_image_handle(struct pipe_context *pctx, const struct pipe_image_view *view)
119
struct llvmpipe_context *ctx = llvmpipe_context(pctx);
120
struct lp_sampler_matrix *matrix = &ctx->sampler_matrix;
122
struct lp_texture_handle *handle = calloc(1, sizeof(struct lp_texture_handle));
124
struct lp_static_texture_state state;
125
lp_sampler_static_texture_state_image(&state, view);
127
/* Trade a bit of performance for potentially less sampler/texture combinations. */
128
state.pot_width = false;
129
state.pot_height = false;
130
state.pot_depth = false;
132
if (view->u.tex.first_layer == view->u.tex.last_layer) {
133
if (state.target == PIPE_TEXTURE_1D_ARRAY)
134
state.target = PIPE_TEXTURE_1D;
135
else if (state.target == PIPE_TEXTURE_2D_ARRAY || state.target == PIPE_TEXTURE_3D)
136
state.target = PIPE_TEXTURE_2D;
137
else if (state.target == PIPE_TEXTURE_CUBE_ARRAY)
138
state.target = PIPE_TEXTURE_CUBE;
141
llvmpipe_register_texture(ctx, &state, false);
144
for (uint32_t i = 0; i < matrix->texture_count; i++) {
145
if (!memcmp(&matrix->textures[i]->state, &state, sizeof(struct lp_static_texture_state))) {
146
handle->functions = matrix->textures[i];
153
return (uint64_t)(uintptr_t)handle;
157
llvmpipe_delete_image_handle(struct pipe_context *pctx, uint64_t handle)
159
free((void *)(uintptr_t)handle);
163
llvmpipe_init_sampler_matrix(struct llvmpipe_context *ctx)
165
ctx->pipe.create_texture_handle = llvmpipe_create_texture_handle;
166
ctx->pipe.delete_texture_handle = llvmpipe_delete_texture_handle;
167
ctx->pipe.create_image_handle = llvmpipe_create_image_handle;
168
ctx->pipe.delete_image_handle = llvmpipe_delete_image_handle;
170
util_dynarray_init(&ctx->sampler_matrix.gallivms, NULL);
174
llvmpipe_sampler_matrix_destroy(struct llvmpipe_context *ctx)
176
struct lp_sampler_matrix *matrix = &ctx->sampler_matrix;
178
free(matrix->samplers);
180
for (uint32_t texture_index = 0; texture_index < matrix->texture_count; texture_index++) {
181
struct lp_texture_functions *texture = matrix->textures[texture_index];
183
uint32_t sampler_count = texture->sampler_count;
184
if (texture->state.format == PIPE_FORMAT_NONE)
185
sampler_count = MIN2(sampler_count, 1);
187
for (uint32_t sampler_index = 0; sampler_index < sampler_count; sampler_index++)
188
free(texture->sample_functions[sampler_index]);
190
free(texture->sample_functions);
191
free(texture->fetch_functions);
192
free(texture->image_functions);
195
free(matrix->textures);
197
util_dynarray_foreach (&ctx->sampler_matrix.gallivms, struct gallivm_state *, gallivm)
198
gallivm_destroy(*gallivm);
200
util_dynarray_fini(&ctx->sampler_matrix.gallivms);
204
compile_function(struct llvmpipe_context *ctx, struct gallivm_state *gallivm, LLVMValueRef function,
205
uint8_t cache_key[SHA1_DIGEST_LENGTH])
207
gallivm_verify_function(gallivm, function);
208
gallivm_compile_module(gallivm);
210
void *function_ptr = func_to_pointer(gallivm_jit_function(gallivm, function));
212
if (!gallivm->cache->data_size)
213
lp_disk_cache_insert_shader(llvmpipe_screen(ctx->pipe.screen), gallivm->cache, cache_key);
215
gallivm_free_ir(gallivm);
217
util_dynarray_append(&ctx->sampler_matrix.gallivms, struct gallivm_state *, gallivm);
223
compile_image_function(struct llvmpipe_context *ctx, struct lp_static_texture_state *texture, uint32_t op)
225
const struct util_format_description *desc = util_format_description(texture->format);
226
if (desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS && !lp_storage_render_image_format_supported(texture->format))
229
bool ms = op >= LP_TOTAL_IMAGE_OP_COUNT / 2;
231
op -= LP_TOTAL_IMAGE_OP_COUNT / 2;
233
struct lp_img_params params = { 0 };
236
if (op >= LP_IMG_OP_COUNT - 1) {
237
params.img_op = LP_IMG_ATOMIC;
238
params.op = op - (LP_IMG_OP_COUNT - 1);
239
} else if (op != LP_IMG_LOAD && op != LP_IMG_STORE) {
240
params.img_op = LP_IMG_ATOMIC_CAS;
243
/* Loads need to support a wider range of formats for input attachments. */
244
if (params.img_op != LP_IMG_LOAD)
245
if (texture->format != PIPE_FORMAT_NONE && !lp_storage_image_format_supported(texture->format))
248
uint8_t cache_key[SHA1_DIGEST_LENGTH];
249
struct mesa_sha1 hash_ctx;
250
_mesa_sha1_init(&hash_ctx);
251
_mesa_sha1_update(&hash_ctx, image_function_base_hash, strlen(image_function_base_hash));
252
_mesa_sha1_update(&hash_ctx, texture, sizeof(*texture));
253
_mesa_sha1_update(&hash_ctx, &op, sizeof(op));
254
_mesa_sha1_final(&hash_ctx, cache_key);
256
struct lp_cached_code cached = { 0 };
257
lp_disk_cache_find_shader(llvmpipe_screen(ctx->pipe.screen), &cached, cache_key);
259
struct gallivm_state *gallivm = gallivm_create("sample_function", ctx->context, &cached);
261
struct lp_image_static_state state = {
262
.image_state = *texture,
264
struct lp_build_image_soa *image_soa = lp_bld_llvm_image_soa_create(&state, 1);
267
memset(&type, 0, sizeof type);
268
type.floating = true; /* floating point values */
269
type.sign = true; /* values are signed */
270
type.norm = false; /* values are not limited to [0,1] or [-1,1] */
271
type.width = 32; /* 32-bit float */
272
type.length = MIN2(lp_native_vector_width / 32, 16); /* n*4 elements per vector */
274
struct lp_compute_shader_variant cs = { .gallivm = gallivm };
275
lp_jit_init_cs_types(&cs);
278
params.target = texture->target;
279
params.resources_type = cs.jit_resources_type;
280
params.format = texture->format;
282
LLVMTypeRef function_type = lp_build_image_function_type(gallivm, ¶ms, ms);
283
if (!function_type) {
285
gallivm_destroy(gallivm);
289
LLVMValueRef function = LLVMAddFunction(gallivm->module, "image", function_type);
291
uint32_t arg_index = 0;
293
gallivm->texture_descriptor = LLVMGetParam(function, arg_index++);
295
if (params.img_op != LP_IMG_LOAD)
296
params.exec_mask = LLVMGetParam(function, arg_index++);
298
LLVMValueRef coords[3];
299
params.coords = coords;
300
for (uint32_t i = 0; i < 3; i++)
301
coords[i] = LLVMGetParam(function, arg_index++);
304
params.ms_index = LLVMGetParam(function, arg_index++);
306
if (params.img_op != LP_IMG_LOAD)
307
for (uint32_t i = 0; i < 4; i++)
308
params.indata[i] = LLVMGetParam(function, arg_index++);
310
if (params.img_op == LP_IMG_ATOMIC_CAS)
311
for (uint32_t i = 0; i < 4; i++)
312
params.indata2[i] = LLVMGetParam(function, arg_index++);
314
LLVMBuilderRef old_builder = gallivm->builder;
315
LLVMBasicBlockRef block = LLVMAppendBasicBlockInContext(gallivm->context, function, "entry");
316
gallivm->builder = LLVMCreateBuilderInContext(gallivm->context);
317
LLVMPositionBuilderAtEnd(gallivm->builder, block);
319
LLVMValueRef outdata[4] = { 0 };
320
lp_build_img_op_soa(texture, lp_build_image_soa_dynamic_state(image_soa), gallivm, ¶ms, outdata);
322
for (uint32_t i = 1; i < 4; i++)
324
outdata[i] = outdata[0];
326
if (params.img_op != LP_IMG_STORE)
327
LLVMBuildAggregateRet(gallivm->builder, outdata, 4);
329
LLVMBuildRetVoid(gallivm->builder);
331
LLVMDisposeBuilder(gallivm->builder);
332
gallivm->builder = old_builder;
336
return compile_function(ctx, gallivm, function, cache_key);
340
compile_sample_function(struct llvmpipe_context *ctx, struct lp_static_texture_state *texture,
341
struct lp_static_sampler_state *sampler, uint32_t sample_key)
343
enum lp_sampler_lod_control lod_control = (sample_key & LP_SAMPLER_LOD_CONTROL_MASK) >> LP_SAMPLER_LOD_CONTROL_SHIFT;
345
if (texture->format != PIPE_FORMAT_NONE) {
346
enum lp_sampler_op_type op_type = (sample_key & LP_SAMPLER_OP_TYPE_MASK) >> LP_SAMPLER_OP_TYPE_SHIFT;
347
if (op_type != LP_SAMPLER_OP_LODQ)
348
if ((sampler->compare_mode == PIPE_TEX_COMPARE_NONE) == !!(sample_key & LP_SAMPLER_SHADOW))
351
/* Skip integer formats which would cause a type mismatch in the compare function. */
352
const struct util_format_description *desc = util_format_description(texture->format);
353
struct lp_type texel_type = {
358
texel_type = lp_build_texel_type(texel_type, desc);
359
if ((sample_key & LP_SAMPLER_SHADOW) && !texel_type.floating)
362
if (texture_dims(texture->target) != 2 && op_type == LP_SAMPLER_OP_GATHER)
365
if (op_type != LP_SAMPLER_OP_FETCH) {
366
if (!sampler->normalized_coords) {
367
if (texture->target != PIPE_TEXTURE_1D && texture->target != PIPE_TEXTURE_2D)
370
if (!texture->level_zero_only)
375
if (util_format_is_pure_integer(texture->format) &&
376
(sampler->min_img_filter == PIPE_TEX_FILTER_LINEAR ||
377
sampler->min_mip_filter == PIPE_TEX_MIPFILTER_LINEAR ||
378
sampler->mag_img_filter == PIPE_TEX_FILTER_LINEAR))
381
if (sampler->aniso) {
382
if (texture_dims(texture->target) != 2)
385
if (util_format_is_pure_integer(texture->format))
389
uint32_t bind = op_type == LP_SAMPLER_OP_FETCH ? PIPE_BIND_CONSTANT_BUFFER : PIPE_BIND_SAMPLER_VIEW;
390
if (!ctx->pipe.screen->is_format_supported(ctx->pipe.screen, texture->format, texture->target, 0, 0, bind))
394
uint8_t cache_key[SHA1_DIGEST_LENGTH];
395
struct mesa_sha1 hash_ctx;
396
_mesa_sha1_init(&hash_ctx);
397
_mesa_sha1_update(&hash_ctx, sample_function_base_hash, strlen(sample_function_base_hash));
398
_mesa_sha1_update(&hash_ctx, texture, sizeof(*texture));
399
_mesa_sha1_update(&hash_ctx, sampler, sizeof(*sampler));
400
_mesa_sha1_update(&hash_ctx, &sample_key, sizeof(sample_key));
401
_mesa_sha1_final(&hash_ctx, cache_key);
403
struct lp_cached_code cached = { 0 };
404
lp_disk_cache_find_shader(llvmpipe_screen(ctx->pipe.screen), &cached, cache_key);
406
struct gallivm_state *gallivm = gallivm_create("sample_function", ctx->context, &cached);
408
struct lp_sampler_static_state state = {
409
.texture_state = *texture,
410
.sampler_state = *sampler,
412
struct lp_build_sampler_soa *sampler_soa = lp_llvm_sampler_soa_create(&state, 1);
415
memset(&type, 0, sizeof type);
416
type.floating = true; /* floating point values */
417
type.sign = true; /* values are signed */
418
type.norm = false; /* values are not limited to [0,1] or [-1,1] */
419
type.width = 32; /* 32-bit float */
420
type.length = MIN2(lp_native_vector_width / 32, 16); /* n*4 elements per vector */
422
struct lp_compute_shader_variant cs = { .gallivm = gallivm };
423
lp_jit_init_cs_types(&cs);
425
LLVMTypeRef function_type = lp_build_sample_function_type(gallivm, sample_key);
426
LLVMValueRef function = LLVMAddFunction(gallivm->module, "sample", function_type);
428
uint32_t arg_index = 0;
430
gallivm->texture_descriptor = LLVMGetParam(function, arg_index++);
431
gallivm->sampler_descriptor = LLVMGetParam(function, arg_index++);
433
LLVMValueRef aniso_filter_table = LLVMGetParam(function, arg_index++);
435
LLVMValueRef coords[5];
436
for (unsigned i = 0; i < 4; i++)
437
coords[i] = LLVMGetParam(function, arg_index++);
439
if (sample_key & LP_SAMPLER_SHADOW)
440
coords[4] = LLVMGetParam(function, arg_index++);
442
coords[4] = lp_build_undef(gallivm, type);
444
LLVMValueRef ms_index = NULL;
445
if (sample_key & LP_SAMPLER_FETCH_MS)
446
ms_index = LLVMGetParam(function, arg_index++);
448
LLVMValueRef offsets[3] = { 0 };
449
if (sample_key & LP_SAMPLER_OFFSETS)
450
for (unsigned i = 0; i < 3; i++)
451
offsets[i] = LLVMGetParam(function, arg_index++);
453
LLVMValueRef lod = NULL;
454
if (lod_control == LP_SAMPLER_LOD_BIAS || lod_control == LP_SAMPLER_LOD_EXPLICIT)
455
lod = LLVMGetParam(function, arg_index++);
457
LLVMBuilderRef old_builder = gallivm->builder;
458
LLVMBasicBlockRef block = LLVMAppendBasicBlockInContext(gallivm->context, function, "entry");
459
gallivm->builder = LLVMCreateBuilderInContext(gallivm->context);
460
LLVMPositionBuilderAtEnd(gallivm->builder, block);
462
LLVMValueRef texel_out[4] = { 0 };
463
lp_build_sample_soa_code(gallivm, texture, sampler, lp_build_sampler_soa_dynamic_state(sampler_soa),
464
type, sample_key, 0, 0, cs.jit_resources_type, NULL, cs.jit_cs_thread_data_type,
465
NULL, coords, offsets, NULL, lod, ms_index, aniso_filter_table, texel_out);
467
LLVMBuildAggregateRet(gallivm->builder, texel_out, 4);
469
LLVMDisposeBuilder(gallivm->builder);
470
gallivm->builder = old_builder;
474
return compile_function(ctx, gallivm, function, cache_key);
478
compile_size_function(struct llvmpipe_context *ctx, struct lp_static_texture_state *texture, bool samples)
480
uint8_t cache_key[SHA1_DIGEST_LENGTH];
481
struct mesa_sha1 hash_ctx;
482
_mesa_sha1_init(&hash_ctx);
483
_mesa_sha1_update(&hash_ctx, size_function_base_hash, strlen(size_function_base_hash));
484
_mesa_sha1_update(&hash_ctx, texture, sizeof(*texture));
485
_mesa_sha1_update(&hash_ctx, &samples, sizeof(samples));
486
_mesa_sha1_final(&hash_ctx, cache_key);
488
struct lp_cached_code cached = { 0 };
489
lp_disk_cache_find_shader(llvmpipe_screen(ctx->pipe.screen), &cached, cache_key);
491
struct gallivm_state *gallivm = gallivm_create("sample_function", ctx->context, &cached);
493
struct lp_sampler_static_state state = {
494
.texture_state = *texture,
496
struct lp_build_sampler_soa *sampler_soa = lp_llvm_sampler_soa_create(&state, 1);
499
memset(&type, 0, sizeof type);
500
type.floating = true; /* floating point values */
501
type.sign = true; /* values are signed */
502
type.norm = false; /* values are not limited to [0,1] or [-1,1] */
503
type.width = 32; /* 32-bit float */
504
type.length = MIN2(lp_native_vector_width / 32, 16); /* n*4 elements per vector */
506
struct lp_compute_shader_variant cs = { .gallivm = gallivm };
507
lp_jit_init_cs_types(&cs);
509
struct lp_sampler_size_query_params params = {
510
.int_type = lp_int_type(type),
511
.target = texture->target,
512
.resources_type = cs.jit_resources_type,
513
.is_sviewinfo = true,
514
.samples_only = samples,
517
if (params.target == PIPE_TEXTURE_1D)
518
params.target = PIPE_TEXTURE_1D_ARRAY;
519
else if (params.target == PIPE_TEXTURE_2D)
520
params.target = PIPE_TEXTURE_2D_ARRAY;
521
else if (params.target == PIPE_TEXTURE_CUBE)
522
params.target = PIPE_TEXTURE_CUBE_ARRAY;
524
LLVMTypeRef function_type = lp_build_size_function_type(gallivm, ¶ms);
525
LLVMValueRef function = LLVMAddFunction(gallivm->module, "size", function_type);
527
uint32_t arg_index = 0;
529
gallivm->texture_descriptor = LLVMGetParam(function, arg_index++);
532
params.explicit_lod = LLVMGetParam(function, arg_index++);
534
LLVMBuilderRef old_builder = gallivm->builder;
535
LLVMBasicBlockRef block = LLVMAppendBasicBlockInContext(gallivm->context, function, "entry");
536
gallivm->builder = LLVMCreateBuilderInContext(gallivm->context);
537
LLVMPositionBuilderAtEnd(gallivm->builder, block);
539
LLVMValueRef out_sizes[4] = { 0 };
540
params.sizes_out = out_sizes;
541
lp_build_size_query_soa(gallivm, texture, lp_build_sampler_soa_dynamic_state(sampler_soa), ¶ms);
543
for (uint32_t i = 0; i < 4; i++)
545
out_sizes[i] = lp_build_const_int_vec(gallivm, params.int_type, 0);
547
LLVMBuildAggregateRet(gallivm->builder, out_sizes, 4);
549
LLVMDisposeBuilder(gallivm->builder);
550
gallivm->builder = old_builder;
554
return compile_function(ctx, gallivm, function, cache_key);
558
compile_sample_functions(struct llvmpipe_context *ctx, struct lp_static_texture_state *texture,
559
struct lp_static_sampler_state *sampler, void ***dst)
565
functions = calloc(LP_SAMPLE_KEY_COUNT, sizeof(void *));
569
bool has_sampler = !!sampler;
571
struct lp_static_sampler_state dummy_sampler = { 0 };
573
sampler = &dummy_sampler;
575
struct lp_sampler_matrix *matrix = &ctx->sampler_matrix;
576
for (uint32_t sample_key = 0; sample_key < LP_SAMPLE_KEY_COUNT; sample_key++) {
577
if (!matrix->sampler_keys[sample_key])
580
enum lp_sampler_op_type op_type = (sample_key & LP_SAMPLER_OP_TYPE_MASK) >> LP_SAMPLER_OP_TYPE_SHIFT;
581
if (has_sampler && op_type == LP_SAMPLER_OP_FETCH)
584
if (!functions[sample_key])
585
functions[sample_key] = compile_sample_function(ctx, texture, sampler, sample_key);
590
llvmpipe_register_texture(struct llvmpipe_context *ctx, struct lp_static_texture_state *state, bool sampled)
592
struct lp_sampler_matrix *matrix = &ctx->sampler_matrix;
595
uint32_t dst_index = matrix->texture_count;
596
for (uint32_t i = 0; i < matrix->texture_count; i++) {
597
if (memcmp(&matrix->textures[i]->state, state, sizeof(struct lp_static_texture_state)))
600
bool has_functions = sampled ? matrix->textures[i]->sampled : matrix->textures[i]->storage;
602
uint32_t prev_ref_count = matrix->textures[i]->ref_count++;
603
if (has_functions && prev_ref_count)
611
struct lp_texture_functions *entry;
613
matrix->texture_count++;
614
matrix->textures = realloc(matrix->textures, matrix->texture_count * sizeof(struct lp_texture_functions *));
616
entry = calloc(1, sizeof(struct lp_texture_functions));
617
matrix->textures[dst_index] = entry;
619
entry->ref_count = 1;
620
entry->state = *state;
621
entry->image_functions = calloc(LP_TOTAL_IMAGE_OP_COUNT, sizeof(void **));
623
entry = matrix->textures[dst_index];
627
entry->sampled = true;
629
entry->storage = true;
631
if (entry->sampled) {
632
if (entry->sample_functions) {
633
entry->sample_functions = realloc(entry->sample_functions, matrix->sampler_count * sizeof(void **));
634
memset(entry->sample_functions + entry->sampler_count, 0, (matrix->sampler_count - entry->sampler_count) * sizeof(void **));
636
entry->sample_functions = calloc(matrix->sampler_count, sizeof(void **));
638
entry->sampler_count = matrix->sampler_count;
640
if (state->format == PIPE_FORMAT_NONE) {
641
if (matrix->sampler_count)
642
compile_sample_functions(ctx, state, NULL, entry->sample_functions);
643
for (uint32_t i = 1; i < matrix->sampler_count; i++)
644
entry->sample_functions[i] = entry->sample_functions[0];
646
for (uint32_t i = 0; i < matrix->sampler_count; i++)
647
compile_sample_functions(ctx, state, matrix->samplers + i, entry->sample_functions + i);
650
compile_sample_functions(ctx, state, NULL, &entry->fetch_functions);
652
if (!entry->size_function)
653
entry->size_function = compile_size_function(ctx, state, false);
655
if (!entry->samples_function)
656
entry->samples_function = compile_size_function(ctx, state, true);
659
if (entry->storage) {
661
BITSET_FOREACH_SET (image_op, matrix->image_ops, LP_TOTAL_IMAGE_OP_COUNT)
662
if (!entry->image_functions[image_op])
663
entry->image_functions[image_op] = compile_image_function(ctx, state, image_op);
668
llvmpipe_register_sampler(struct llvmpipe_context *ctx, struct lp_static_sampler_state *state)
670
struct lp_sampler_matrix *matrix = &ctx->sampler_matrix;
671
for (uint32_t i = 0; i < matrix->sampler_count; i++)
672
if (!memcmp(matrix->samplers + i, state, sizeof(struct lp_static_sampler_state)))
675
matrix->sampler_count++;
676
matrix->samplers = realloc(matrix->samplers, matrix->sampler_count * sizeof(struct lp_static_sampler_state));
678
matrix->samplers[matrix->sampler_count - 1] = *state;
680
for (uint32_t i = 0; i < matrix->texture_count; i++) {
681
struct lp_texture_functions *texture = matrix->textures[i];
682
if (!texture->ref_count || !texture->sampled)
685
texture->sampler_count = matrix->sampler_count;
686
texture->sample_functions = realloc(texture->sample_functions, matrix->sampler_count * sizeof(void **));
688
void ***dst = texture->sample_functions + (matrix->sampler_count - 1);
690
if (texture->state.format == PIPE_FORMAT_NONE) {
691
if (matrix->sampler_count == 1) {
693
compile_sample_functions(ctx, &texture->state, NULL, dst);
695
*dst = texture->sample_functions[0];
702
compile_sample_functions(ctx, &texture->state, state, dst);
707
register_sample_key(struct llvmpipe_context *ctx, uint32_t sample_key)
709
struct lp_sampler_matrix *matrix = &ctx->sampler_matrix;
711
uint32_t prev_ref_count = matrix->sampler_keys[sample_key]++;
715
for (uint32_t texture_index = 0; texture_index < matrix->texture_count; texture_index++) {
716
struct lp_texture_functions *texture = matrix->textures[texture_index];
717
if (!texture->ref_count || !texture->sampled)
720
enum lp_sampler_op_type op_type = (sample_key & LP_SAMPLER_OP_TYPE_MASK) >> LP_SAMPLER_OP_TYPE_SHIFT;
721
if (op_type == LP_SAMPLER_OP_FETCH) {
722
if (!texture->fetch_functions[sample_key]) {
723
struct lp_static_sampler_state dummy_sampler = { 0 };
724
texture->fetch_functions[sample_key] = compile_sample_function(ctx, &texture->state, &dummy_sampler, sample_key);
729
if (texture->state.format == PIPE_FORMAT_NONE) {
730
if (matrix->sampler_count && !texture->sample_functions[0][sample_key]) {
731
struct lp_static_sampler_state dummy_sampler = { 0 };
732
texture->sample_functions[0][sample_key] = compile_sample_function(ctx, &texture->state, &dummy_sampler, sample_key);
737
for (uint32_t sampler_index = 0; sampler_index < matrix->sampler_count; sampler_index++) {
738
if (!texture->sample_functions[sampler_index][sample_key]) {
739
texture->sample_functions[sampler_index][sample_key] = compile_sample_function(
740
ctx, &texture->state, matrix->samplers + sampler_index, sample_key);
747
unregister_sample_key(struct llvmpipe_context *ctx, uint32_t sample_key)
749
struct lp_sampler_matrix *matrix = &ctx->sampler_matrix;
751
assert(matrix->sampler_keys[sample_key]);
752
matrix->sampler_keys[sample_key]--;
756
register_image_op(struct llvmpipe_context *ctx, uint32_t op)
758
struct lp_sampler_matrix *matrix = &ctx->sampler_matrix;
759
if (BITSET_TEST(matrix->image_ops, op))
762
BITSET_SET(matrix->image_ops, op);
764
for (uint32_t texture_index = 0; texture_index < matrix->texture_count; texture_index++) {
765
struct lp_texture_functions *texture = matrix->textures[texture_index];
766
if (texture->ref_count && texture->storage)
767
texture->image_functions[op] = compile_image_function(ctx, &texture->state, op);
771
struct register_shader_state {
772
struct llvmpipe_context *ctx;
777
register_instr(nir_builder *b, nir_instr *instr, void *_state)
779
struct register_shader_state *state = _state;
781
if (instr->type == nir_instr_type_tex) {
782
nir_tex_instr *tex = nir_instr_as_tex(instr);
783
uint32_t sample_key = lp_build_nir_sample_key(b->shader->info.stage, tex);
785
if (state->unregister)
786
unregister_sample_key(state->ctx, sample_key);
788
register_sample_key(state->ctx, sample_key);
789
} else if (instr->type == nir_instr_type_intrinsic) {
790
nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
792
struct lp_img_params params;
793
lp_img_op_from_intrinsic(¶ms, intrin);
795
if (params.img_op == -1)
798
uint32_t op = params.img_op;
799
if (op == LP_IMG_ATOMIC_CAS)
801
else if (op == LP_IMG_ATOMIC)
802
op = params.op + (LP_IMG_OP_COUNT - 1);
804
if (nir_intrinsic_image_dim(intrin) == GLSL_SAMPLER_DIM_MS ||
805
nir_intrinsic_image_dim(intrin) == GLSL_SAMPLER_DIM_SUBPASS_MS)
806
op += LP_TOTAL_IMAGE_OP_COUNT / 2;
808
register_image_op(state->ctx, op);
815
llvmpipe_register_shader(struct pipe_context *ctx, const struct pipe_shader_state *shader, bool unregister)
817
if (shader->type != PIPE_SHADER_IR_NIR)
820
struct register_shader_state state = {
821
.ctx = llvmpipe_context(ctx),
822
.unregister = unregister,
824
nir_shader_instructions_pass(shader->ir.nir, register_instr, nir_metadata_all, &state);