• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright 2010-2021 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 #include <limits.h>
29 
30 #include "pipe/p_defines.h"
31 #include "util/u_inlines.h"
32 #include "util/u_memory.h"
33 #include "util/u_pointer.h"
34 #include "util/format/u_format.h"
35 #include "util/u_dump.h"
36 #include "util/u_string.h"
37 #include "util/os_time.h"
38 #include "pipe/p_shader_tokens.h"
39 #include "draw/draw_context.h"
40 #include "gallivm/lp_bld_type.h"
41 #include "gallivm/lp_bld_const.h"
42 #include "gallivm/lp_bld_conv.h"
43 #include "gallivm/lp_bld_init.h"
44 #include "gallivm/lp_bld_intr.h"
45 #include "gallivm/lp_bld_logic.h"
46 #include "gallivm/lp_bld_tgsi.h"
47 #include "gallivm/lp_bld_swizzle.h"
48 #include "gallivm/lp_bld_flow.h"
49 #include "gallivm/lp_bld_printf.h"
50 #include "gallivm/lp_bld_debug.h"
51 #include "gallivm/lp_bld_nir.h"
52 
53 #include "lp_bld_alpha.h"
54 #include "lp_bld_blend.h"
55 #include "lp_bld_depth.h"
56 #include "lp_bld_interp.h"
57 #include "lp_context.h"
58 #include "lp_debug.h"
59 #include "lp_perf.h"
60 #include "lp_screen.h"
61 #include "lp_setup.h"
62 #include "lp_state.h"
63 #include "lp_tex_sample.h"
64 #include "lp_flush.h"
65 #include "lp_state_fs.h"
66 
67 
68 /**
69  * Sampler.
70  */
71 struct linear_sampler
72 {
73    struct lp_build_sampler_aos base;
74    LLVMValueRef texels_ptrs[LP_MAX_LINEAR_TEXTURES];
75    LLVMValueRef counter;
76    unsigned instance;
77 };
78 
79 
80 /**
81  * Provide texels to the TGSI translation.
82  *
83  * We don't actually do any texture sampling here, but simply hand the
84  * precomputed row of texels.
85  */
86 static LLVMValueRef
emit_fetch_texel_linear(const struct lp_build_sampler_aos * base,struct lp_build_context * bld,enum tgsi_texture_type target,unsigned unit,LLVMValueRef coords,const struct lp_derivatives derivs,enum lp_build_tex_modifier modifier)87 emit_fetch_texel_linear(const struct lp_build_sampler_aos *base,
88                         struct lp_build_context *bld,
89                         enum tgsi_texture_type target,
90                         unsigned unit,
91                         LLVMValueRef coords,
92                         const struct lp_derivatives derivs,
93                         enum lp_build_tex_modifier modifier)
94 {
95    struct linear_sampler *sampler = (struct linear_sampler *)base;
96 
97    if (sampler->instance >= LP_MAX_LINEAR_TEXTURES) {
98       assert(false);
99       return bld->undef;
100    }
101 
102    /* Pointer to a row of texels */
103    LLVMValueRef texels_ptr = sampler->texels_ptrs[sampler->instance];
104 
105    LLVMValueRef texel = lp_build_pointer_get2(bld->gallivm->builder,
106                                               bld->vec_type,
107                                               texels_ptr, sampler->counter);
108    assert(LLVMTypeOf(texel) == bld->vec_type);
109 
110    /*
111     * We have a struct lp_linear_sampler instance per TEX instruction,
112     * _not_ per unit, as each TEX instruction will need separate storage
113     * for the texels.
114     */
115    (void)unit;
116    ++sampler->instance;
117 
118    return texel;
119 }
120 
121 
122 /**
123  * Generates the main body of the fragment shader
124  * Supports generating code for 4 pixel blocks and individual pixels
125  */
126 static LLVMValueRef
llvm_fragment_body(struct lp_build_context * bld,struct lp_fragment_shader * shader,struct lp_fragment_shader_variant * variant,struct linear_sampler * sampler,LLVMValueRef * inputs_ptrs,LLVMValueRef consts_ptr,LLVMValueRef blend_color,LLVMValueRef alpha_ref,struct lp_type fs_type,LLVMValueRef dst)127 llvm_fragment_body(struct lp_build_context *bld,
128                    struct lp_fragment_shader *shader,
129                    struct lp_fragment_shader_variant *variant,
130                    struct linear_sampler* sampler,
131                    LLVMValueRef *inputs_ptrs,
132                    LLVMValueRef consts_ptr,
133                    LLVMValueRef blend_color,
134                    LLVMValueRef alpha_ref,
135                    struct lp_type fs_type,
136                    LLVMValueRef dst)
137 {
138    static const unsigned char bgra_swizzles[4] = {2, 1, 0, 3};
139    static const unsigned char rgba_swizzles[4] = {0, 1, 2, 3};
140    LLVMValueRef inputs[PIPE_MAX_SHADER_INPUTS];
141    LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS];
142    LLVMBuilderRef builder = bld->gallivm->builder;
143    struct gallivm_state *gallivm = bld->gallivm;
144    LLVMValueRef result = NULL;
145    bool rgba_order = (variant->key.cbuf_format[0] == PIPE_FORMAT_R8G8B8A8_UNORM ||
146                       variant->key.cbuf_format[0] == PIPE_FORMAT_R8G8B8X8_UNORM);
147    struct nir_shader *nir = shader->base.ir.nir;
148    sampler->instance = 0;
149 
150    /*
151     * Advance inputs
152     */
153    unsigned i;
154    for (i = 0; i < util_bitcount64(nir->info.inputs_read); ++i) {
155       inputs[i] =
156          lp_build_pointer_get2(builder, bld->vec_type, inputs_ptrs[i], sampler->counter);
157       assert(LLVMTypeOf(inputs[i]) == bld->vec_type);
158    }
159    for ( ; i < PIPE_MAX_SHADER_INPUTS; ++i) {
160       inputs[i] = bld->undef;
161    }
162 
163    for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; ++i) {
164       outputs[i] = bld->undef;
165    }
166 
167    nir_shader *clone = nir_shader_clone(NULL, nir);
168    lp_build_nir_aos(gallivm, clone, fs_type,
169                     rgba_order ? rgba_swizzles : bgra_swizzles,
170                     consts_ptr, inputs, outputs,
171                     &sampler->base);
172    ralloc_free(clone);
173 
174    /*
175     * Blend output color
176     */
177    nir_foreach_shader_out_variable(var, nir) {
178       unsigned slots = nir_variable_count_slots(var, var->type);
179 
180       for (unsigned s = 0; s < slots; s++) {
181          unsigned idx = var->data.driver_location + s;
182          if (!outputs[idx])
183             continue;
184 
185          LLVMValueRef output = LLVMBuildLoad2(builder, bld->vec_type, outputs[idx], "");
186          lp_build_name(output, "output%u", i);
187 
188          unsigned cbuf = var->data.location - FRAG_RESULT_DATA0 + s;
189          lp_build_name(output, "cbuf%u", cbuf);
190 
191          if (var->data.location < FRAG_RESULT_DATA0 || s > 0)
192             continue;
193 
194          /* Perform alpha test if necessary */
195          LLVMValueRef mask = NULL;
196          if (variant->key.alpha.enabled) {
197             LLVMTypeRef vec_type = lp_build_vec_type(gallivm, fs_type);
198             LLVMValueRef broadcast_alpha = lp_build_broadcast(gallivm, vec_type,
199                                                               alpha_ref);
200 
201             mask = lp_build_cmp(bld, variant->key.alpha.func, output,
202                                 broadcast_alpha);
203             /* XXX is 4 correct? */
204             mask = lp_build_swizzle_scalar_aos(bld, mask, bgra_swizzles[3], 4);
205 
206             lp_build_name(mask, "alpha_test_mask");
207          }
208 
209          LLVMValueRef src1 = lp_build_zero(gallivm, fs_type);
210 
211          result = lp_build_blend_aos(gallivm,
212                                      &variant->key.blend,
213                                      variant->key.cbuf_format[idx],
214                                      fs_type,
215                                      cbuf,   /* rt */
216                                      output, /* src */
217                                      NULL,   /* src_alpha */
218                                      src1,   /* src1 */
219                                      NULL,   /* src1_alpha */
220                                      dst,
221                                      mask,
222                                      blend_color,  /* const_ */
223                                      NULL,         /* const_alpha */
224                                      rgba_order ? rgba_swizzles : bgra_swizzles,
225                                      4);
226       }
227    }
228 
229    return result;
230 }
231 
232 
233 /**
234  * Generate a function that executes the fragment shader in a linear fashion.
235  * The shader operates on unorm8[16] vectors.
236  * See lp_state_fs_analysis for the "linear" conditions.
237  */
238 void
llvmpipe_fs_variant_linear_llvm(struct llvmpipe_context * lp,struct lp_fragment_shader * shader,struct lp_fragment_shader_variant * variant)239 llvmpipe_fs_variant_linear_llvm(struct llvmpipe_context *lp,
240                                 struct lp_fragment_shader *shader,
241                                 struct lp_fragment_shader_variant *variant)
242 {
243    assert(shader->kind == LP_FS_KIND_BLIT_RGBA ||
244           shader->kind == LP_FS_KIND_BLIT_RGB1 ||
245           shader->kind == LP_FS_KIND_LLVM_LINEAR);
246 
247    struct nir_shader *nir = shader->base.ir.nir;
248    struct gallivm_state *gallivm = variant->gallivm;
249    LLVMTypeRef int8t = LLVMInt8TypeInContext(gallivm->context);
250    LLVMTypeRef int32t = LLVMInt32TypeInContext(gallivm->context);
251    LLVMTypeRef pint8t = LLVMPointerType(int8t, 0);
252    LLVMTypeRef pixelt = LLVMVectorType(int32t, 4);
253 
254    // unorm8[16] vector type
255    struct lp_type fs_type;
256    memset(&fs_type, 0, sizeof fs_type);
257    fs_type.floating = false;
258    fs_type.sign = false;
259    fs_type.norm = true;
260    fs_type.width = 8;
261    fs_type.length = 16;
262 
263    if (LP_DEBUG & DEBUG_TGSI) {
264       if (shader->base.ir.nir) {
265          nir_print_shader(shader->base.ir.nir, stderr);
266       }
267    }
268 
269    /*
270     * Generate the function prototype. Any change here must be reflected in
271     * lp_jit.h's lp_jit_frag_func function pointer type, and vice-versa.
272     */
273 
274    char func_name[256];
275    snprintf(func_name, sizeof(func_name), "fs_variant_linear2");
276 
277    LLVMTypeRef ret_type = pint8t;
278    LLVMTypeRef arg_types[4];
279    arg_types[0] = variant->jit_linear_context_ptr_type; /* context */
280    arg_types[1] = int32t;                               /* x */
281    arg_types[2] = int32t;                               /* y */
282    arg_types[3] = int32t;                               /* width */
283 
284    LLVMTypeRef func_type =
285       LLVMFunctionType(ret_type, arg_types, ARRAY_SIZE(arg_types), 0);
286 
287    LLVMValueRef function =
288       LLVMAddFunction(gallivm->module, func_name, func_type);
289    LLVMSetFunctionCallConv(function, LLVMCCallConv);
290 
291    variant->linear_function = function;
292 
293    /* XXX: need to propagate noalias down into color param now we are
294     * passing a pointer-to-pointer?
295     */
296    for (unsigned i = 0; i < ARRAY_SIZE(arg_types); ++i) {
297       if (LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind) {
298          lp_add_function_attr(function, i + 1, LP_FUNC_ATTR_NOALIAS);
299       }
300    }
301 
302    if (variant->gallivm->cache->data_size)
303       return;
304 
305    LLVMValueRef context_ptr = LLVMGetParam(function, 0);
306    LLVMValueRef x = LLVMGetParam(function, 1);
307    LLVMValueRef y = LLVMGetParam(function, 2);
308    LLVMValueRef width = LLVMGetParam(function, 3);
309 
310    lp_build_name(context_ptr, "context");
311    lp_build_name(x, "x");
312    lp_build_name(y, "y");
313    lp_build_name(width, "width");
314 
315    /*
316     * Function body
317     */
318 
319    LLVMBasicBlockRef block =
320       LLVMAppendBasicBlockInContext(gallivm->context, function, "entry");
321    LLVMBuilderRef builder = gallivm->builder;
322 
323    LLVMPositionBuilderAtEnd(builder, block);
324 
325    struct lp_build_context bld;
326    lp_build_context_init(&bld, gallivm, fs_type);
327 
328    /*
329     * Get context data
330     */
331    LLVMValueRef consts_ptr =
332       lp_jit_linear_context_constants(gallivm,
333                                       variant->jit_linear_context_type,
334                                       context_ptr);
335    LLVMValueRef interpolators_ptr =
336       lp_jit_linear_context_inputs(gallivm,
337                                    variant->jit_linear_context_type,
338                                    context_ptr);
339    LLVMValueRef samplers_ptr =
340       lp_jit_linear_context_tex(gallivm,
341                                 variant->jit_linear_context_type,
342                                 context_ptr);
343 
344    LLVMValueRef color0_ptr =
345       lp_jit_linear_context_color0(gallivm,
346                                    variant->jit_linear_context_type,
347                                    context_ptr);
348    color0_ptr = LLVMBuildLoad2(builder, LLVMPointerType(LLVMInt8TypeInContext(gallivm->context), 0),
349                                color0_ptr, "");
350    color0_ptr = LLVMBuildBitCast(builder, color0_ptr,
351                                  LLVMPointerType(bld.vec_type, 0), "");
352 
353    LLVMValueRef blend_color =
354       lp_jit_linear_context_blend_color(gallivm,
355                                         variant->jit_linear_context_type,
356                                         context_ptr);
357    blend_color = LLVMBuildLoad2(builder, LLVMInt32TypeInContext(gallivm->context),
358                                 blend_color, "");
359    blend_color = lp_build_broadcast(gallivm, LLVMVectorType(int32t, 4),
360                                     blend_color);
361    blend_color = LLVMBuildBitCast(builder, blend_color,
362                                   LLVMVectorType(int8t, 16), "");
363 
364    LLVMValueRef alpha_ref =
365       lp_jit_linear_context_alpha_ref(gallivm,
366                                       variant->jit_linear_context_type,
367                                       context_ptr);
368    alpha_ref = LLVMBuildLoad2(builder, LLVMInt8TypeInContext(gallivm->context),
369                               alpha_ref, "");
370 
371    /*
372     * Invoke the input interpolators
373     */
374    LLVMValueRef inputs_ptrs[LP_MAX_LINEAR_INPUTS];
375 
376    nir_foreach_shader_in_variable(var, nir) {
377       unsigned slots = nir_variable_count_slots(var, var->type);
378 
379       for (unsigned s = 0; s < slots; s++) {
380          unsigned attrib = var->data.driver_location + s;
381          assert(attrib < LP_MAX_LINEAR_INPUTS);
382          if (attrib >= LP_MAX_LINEAR_INPUTS) {
383             break;
384          }
385 
386          LLVMValueRef index = LLVMConstInt(int32t, attrib, 0);
387 
388          LLVMTypeRef input_type = variant->jit_linear_inputs_type;
389          LLVMValueRef elem =
390             lp_build_array_get2(bld.gallivm, input_type, interpolators_ptr, index);
391          assert(LLVMGetTypeKind(LLVMTypeOf(elem)) == LLVMPointerTypeKind);
392 
393          LLVMTypeRef fetch_type = LLVMPointerType(variant->jit_linear_func_type, 0);
394          LLVMValueRef fetch_ptr = lp_build_pointer_get2(builder, fetch_type, elem,
395                                                         LLVMConstInt(int32t, 0, 0));
396          assert(LLVMGetTypeKind(LLVMTypeOf(fetch_ptr)) == LLVMPointerTypeKind);
397 
398          /* Pointer to a row of interpolated inputs */
399          LLVMTypeRef call_type = variant->jit_linear_func_type;
400          elem = LLVMBuildBitCast(builder, elem, pint8t, "");
401          LLVMValueRef inputs_ptr = LLVMBuildCall2(builder, call_type, fetch_ptr, &elem, 1, "");
402          assert(LLVMGetTypeKind(LLVMTypeOf(inputs_ptr)) == LLVMPointerTypeKind);
403 
404          lp_add_function_attr(inputs_ptr, -1, LP_FUNC_ATTR_NOUNWIND);
405 
406          lp_build_name(inputs_ptr, "input%u_ptr", attrib);
407 
408          inputs_ptrs[attrib] = inputs_ptr;
409       }
410    }
411 
412    /*
413     * Invoke and hook up the texture samplers.
414     */
415 
416    struct linear_sampler sampler;
417    memset(&sampler, 0, sizeof sampler);
418    sampler.base.emit_fetch_texel = &emit_fetch_texel_linear;
419 
420    for (unsigned attrib = 0; attrib < shader->info.num_texs; ++attrib) {
421       assert(attrib < LP_MAX_LINEAR_TEXTURES);
422       if (attrib >= LP_MAX_LINEAR_TEXTURES) {
423          break;
424       }
425 
426       LLVMValueRef index = LLVMConstInt(int32t, attrib, 0);
427       LLVMTypeRef samp_type = variant->jit_linear_textures_type;
428       LLVMValueRef elem = lp_build_array_get2(bld.gallivm, samp_type, samplers_ptr, index);
429       assert(LLVMGetTypeKind(LLVMTypeOf(elem)) == LLVMPointerTypeKind);
430 
431       LLVMTypeRef fetch_type = LLVMPointerType(variant->jit_linear_func_type, 0);
432       LLVMValueRef fetch_ptr =
433          lp_build_pointer_get2(builder, fetch_type,
434                                elem, LLVMConstInt(int32t, 0, 0));
435       assert(LLVMGetTypeKind(LLVMTypeOf(fetch_ptr)) == LLVMPointerTypeKind);
436 
437       /* Pointer to a row of texels */
438       LLVMTypeRef call_type = variant->jit_linear_func_type;
439       elem = LLVMBuildBitCast(builder, elem, pint8t, "");
440       LLVMValueRef texels_ptr = LLVMBuildCall2(builder, call_type, fetch_ptr, &elem, 1, "");
441       assert(LLVMGetTypeKind(LLVMTypeOf(texels_ptr)) == LLVMPointerTypeKind);
442 
443       lp_add_function_attr(texels_ptr, -1, LP_FUNC_ATTR_NOUNWIND);
444 
445       lp_build_name(texels_ptr, "tex%u_ptr", attrib);
446 
447       sampler.texels_ptrs[attrib] = texels_ptr;
448    }
449 
450    /* excess = width & 0x3 */
451    LLVMValueRef excess =
452       LLVMBuildAnd(builder, width, LLVMConstInt(int32t, 3, 0), "");
453    /* width *= 4 */
454    width = LLVMBuildLShr(builder, width, LLVMConstInt(int32t, 2, 0), "");
455 
456    /* Loop over blocks of 4 pixels */
457    /* for loop.counter = 0; loop.counter < width; loop.counter++) { */
458    struct lp_build_for_loop_state loop;
459    lp_build_for_loop_begin(&loop, gallivm, LLVMConstInt(int32t, 0, 0),
460                            LLVMIntULT, width, LLVMConstInt(int32t, 1, 0));
461    {
462       LLVMValueRef value;
463       sampler.counter = loop.counter;
464 
465       /* Read 4 pixels */
466       value = lp_build_pointer_get_unaligned2(builder,
467                                               bld.vec_type,
468                                               color0_ptr,
469                                               loop.counter, 4);
470 
471       /* Perform fragment shader body */
472       value = llvm_fragment_body(&bld, shader, variant, &sampler, inputs_ptrs,
473                                  consts_ptr, blend_color, alpha_ref, fs_type,
474                                  value);
475 
476       /* Write 4 pixels */
477       lp_build_pointer_set_unaligned(builder, color0_ptr, loop.counter,
478                                      value, 4);
479    }
480    lp_build_for_loop_end(&loop);
481 
482    /* Compute the edge pixels (width % 4) */
483    struct lp_build_if_state ifstate;
484    lp_build_if(&ifstate, gallivm, LLVMBuildICmp(builder, LLVMIntNE, excess,
485                                             LLVMConstInt(int32t, 0, 0), ""));
486    {
487       struct lp_build_loop_state loop_read, loop_write;
488       LLVMValueRef buf, elem, result, pixel_ptr;
489       LLVMValueRef buf_ptr = lp_build_alloca(gallivm, pixelt, "");
490 
491       sampler.counter = width;
492 
493       /* Get the i32* pixel pointer from the <i16x8>* element pointer */
494       pixel_ptr = LLVMBuildGEP2(gallivm->builder, bld.vec_type,
495                                 color0_ptr, &width, 1, "");
496       pixel_ptr = LLVMBuildBitCast(gallivm->builder, pixel_ptr,
497                                    LLVMPointerType(int32t, 0), "");
498 
499       /* Copy individual pixels from memory to local buffer */
500       lp_build_loop_begin(&loop_read, gallivm, LLVMConstInt(int32t, 0, 0));
501       {
502          elem = lp_build_pointer_get2(gallivm->builder,
503                                       int32t,
504                                       pixel_ptr, loop_read.counter);
505 
506          buf = LLVMBuildLoad2(gallivm->builder, pixelt, buf_ptr, "");
507          buf = LLVMBuildInsertElement(builder, buf, elem,
508                                       loop_read.counter, "");
509          LLVMBuildStore(builder, buf, buf_ptr);
510       }
511       lp_build_loop_end_cond(&loop_read, excess,
512                              LLVMConstInt(int32t, 1, 0), LLVMIntUGE);
513 
514       /* Perform fragment shader body */
515       buf = LLVMBuildLoad2(gallivm->builder, pixelt, buf_ptr, "");
516       buf = LLVMBuildBitCast(builder, buf, bld.vec_type, "");
517 
518       result = llvm_fragment_body(&bld, shader, variant, &sampler,
519                                   inputs_ptrs, consts_ptr, blend_color,
520                                   alpha_ref, fs_type, buf);
521       result = LLVMBuildBitCast(builder, result, pixelt, "");
522 
523       /* Write individual pixels from local buffer to the memory */
524       lp_build_loop_begin(&loop_write, gallivm, LLVMConstInt(int32t, 0, 0));
525       {
526          elem = LLVMBuildExtractElement(builder, result,
527                                         loop_write.counter, "");
528 
529          lp_build_pointer_set(gallivm->builder, pixel_ptr,
530                               loop_write.counter, elem);
531       }
532       lp_build_loop_end_cond(&loop_write, excess,
533                              LLVMConstInt(int32t, 1, 0), LLVMIntUGE);
534    }
535    lp_build_endif(&ifstate);
536 
537    color0_ptr = LLVMBuildBitCast(builder, color0_ptr, pint8t, "");
538 
539    LLVMBuildRet(builder, color0_ptr);
540 
541    gallivm_verify_function(gallivm, function);
542 }
543