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 #include "util/simple_list.h"
30 
31 #include "pipe/p_defines.h"
32 #include "util/u_inlines.h"
33 #include "util/u_memory.h"
34 #include "util/u_pointer.h"
35 #include "util/format/u_format.h"
36 #include "util/u_dump.h"
37 #include "util/u_string.h"
38 #include "util/os_time.h"
39 #include "pipe/p_shader_tokens.h"
40 #include "draw/draw_context.h"
41 #include "tgsi/tgsi_dump.h"
42 #include "tgsi/tgsi_scan.h"
43 #include "tgsi/tgsi_parse.h"
44 #include "gallivm/lp_bld_type.h"
45 #include "gallivm/lp_bld_const.h"
46 #include "gallivm/lp_bld_conv.h"
47 #include "gallivm/lp_bld_init.h"
48 #include "gallivm/lp_bld_intr.h"
49 #include "gallivm/lp_bld_logic.h"
50 #include "gallivm/lp_bld_tgsi.h"
51 #include "gallivm/lp_bld_swizzle.h"
52 #include "gallivm/lp_bld_flow.h"
53 #include "gallivm/lp_bld_printf.h"
54 #include "gallivm/lp_bld_debug.h"
55 
56 #include "lp_bld_alpha.h"
57 #include "lp_bld_blend.h"
58 #include "lp_bld_depth.h"
59 #include "lp_bld_interp.h"
60 #include "lp_context.h"
61 #include "lp_debug.h"
62 #include "lp_perf.h"
63 #include "lp_screen.h"
64 #include "lp_setup.h"
65 #include "lp_state.h"
66 #include "lp_tex_sample.h"
67 #include "lp_flush.h"
68 #include "lp_state_fs.h"
69 
70 
71 /**
72  * Sampler.
73  */
74 struct linear_sampler
75 {
76    struct lp_build_sampler_aos base;
77 
78    LLVMValueRef texels_ptrs[LP_MAX_LINEAR_TEXTURES];
79 
80    LLVMValueRef counter;
81 
82    unsigned instance;
83 };
84 
85 
86 /**
87  * Provide texels to the TGSI translation.
88  *
89  * We don't actually do any texture sampling here, but simply hand the
90  * precomputed row of texels.
91  */
92 static LLVMValueRef
emit_fetch_texel_linear(const struct lp_build_sampler_aos * base,struct lp_build_context * bld,unsigned target,unsigned unit,LLVMValueRef coords,const struct lp_derivatives derivs,enum lp_build_tex_modifier modifier)93 emit_fetch_texel_linear(const struct lp_build_sampler_aos *base,
94                         struct lp_build_context *bld,
95                         unsigned target, /* TGSI_TEXTURE_* */
96                         unsigned unit,
97                         LLVMValueRef coords,
98                         const struct lp_derivatives derivs,
99                         enum lp_build_tex_modifier modifier)
100 {
101    struct linear_sampler *sampler = (struct linear_sampler *)base;
102    LLVMValueRef texels_ptr;
103    LLVMValueRef texel;
104 
105    if (sampler->instance >= LP_MAX_LINEAR_TEXTURES) {
106       assert(FALSE);
107       return bld->undef;
108    }
109 
110    /* Pointer to a row of texels */
111    texels_ptr = sampler->texels_ptrs[sampler->instance];
112 
113    texel = lp_build_pointer_get(bld->gallivm->builder, texels_ptr,
114                                 sampler->counter);
115    assert(LLVMTypeOf(texel) == bld->vec_type);
116 
117    /*
118     * We have a struct lp_linear_sampler instance per TEX instruction,
119     * _not_ per unit, as each TEX intruction will need separate storage for the
120     * texels.
121     */
122    (void)unit;
123    ++sampler->instance;
124 
125    return texel;
126 }
127 
128 /**
129  * Generates the main body of the fragment shader
130  * Supports generating code for 4 pixel blocks and individual pixels
131  */
132 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)133 llvm_fragment_body(struct lp_build_context *bld,
134                    struct lp_fragment_shader *shader,
135                    struct lp_fragment_shader_variant *variant,
136                    struct linear_sampler* sampler,
137                    LLVMValueRef *inputs_ptrs,
138                    LLVMValueRef consts_ptr,
139                    LLVMValueRef blend_color,
140                    LLVMValueRef alpha_ref,
141                    struct lp_type fs_type,
142                    LLVMValueRef dst)
143 {
144    const unsigned char bgra_swizzles[4] = {2, 1, 0, 3};
145 
146    LLVMValueRef inputs[PIPE_MAX_SHADER_INPUTS];
147    LLVMValueRef outputs[PIPE_MAX_SHADER_OUTPUTS];
148 
149    LLVMBuilderRef builder = bld->gallivm->builder;
150    struct gallivm_state *gallivm = bld->gallivm;
151 
152    LLVMValueRef src1 = lp_build_zero(gallivm, fs_type);
153 
154    LLVMValueRef result = NULL;
155    unsigned i;
156 
157    sampler->instance = 0;
158 
159 
160    /*
161     * Advance inputs
162     */
163 
164    for (i = 0; i < shader->info.base.num_inputs; ++i) {
165       LLVMValueRef inputs_ptr;
166       LLVMValueRef input;
167 
168       inputs_ptr = inputs_ptrs[i];
169 
170       input = lp_build_pointer_get(builder, inputs_ptr, sampler->counter);
171       assert(LLVMTypeOf(input) == bld->vec_type);
172 
173       inputs[i] = input;
174    }
175 
176    for ( ; i < PIPE_MAX_SHADER_INPUTS; ++i) {
177       inputs[i] = bld->undef;
178    }
179 
180 
181    /*
182     * Translate the TGSI
183     */
184 
185    for (i = 0; i < PIPE_MAX_SHADER_OUTPUTS; ++i) {
186       outputs[i] = bld->undef;
187    }
188 
189    lp_build_tgsi_aos(gallivm, shader->base.tokens, fs_type,
190                      bgra_swizzles,
191                      consts_ptr, inputs, outputs,
192                      &sampler->base,
193                      &shader->info.base);
194 
195    /*
196     * Blend output color
197     */
198 
199    for (i = 0; i < shader->info.base.num_outputs; ++i) {
200       LLVMValueRef mask = NULL;
201       LLVMValueRef output;
202       unsigned cbuf;
203 
204       if (!outputs[i])
205          continue;
206 
207       output = LLVMBuildLoad(builder, outputs[i], "");
208       lp_build_name(output, "output%u", i);
209 
210       cbuf = shader->info.base.output_semantic_index[i];
211       lp_build_name(output, "cbuf%u", cbuf);
212 
213       if (shader->info.base.output_semantic_name[i] != TGSI_SEMANTIC_COLOR || cbuf != 0)
214          continue;
215 
216       /* Perform alpha test if necessary */
217       if (variant->key.alpha.enabled) {
218          LLVMTypeRef vec_type = lp_build_vec_type(gallivm, fs_type);
219          LLVMValueRef broadcast_alpha = lp_build_broadcast(gallivm, vec_type, alpha_ref);
220 
221          mask = lp_build_cmp(bld, variant->key.alpha.func, output, broadcast_alpha);
222          /* XXX is 4 correct? */
223          mask = lp_build_swizzle_scalar_aos(bld, mask, bgra_swizzles[3], 4);
224 
225          lp_build_name(mask, "alpha_test_mask");
226       }
227 
228       result = lp_build_blend_aos(gallivm,
229                                   &variant->key.blend,
230                                   variant->key.cbuf_format[i],
231                                   fs_type,
232                                   cbuf,   /* rt */
233                                   output, /* src */
234                                   NULL,   /* src_alpha */
235                                   src1,   /* src1 */
236                                   NULL,   /* src1_alpha */
237                                   dst,
238                                   mask,
239                                   blend_color,  /* const_ */
240                                   NULL,         /* const_alpha */
241                                   bgra_swizzles,
242                                   4);
243    }
244 
245    return result;
246 }
247 
248 /**
249  * Generate a function that executes the fragment shader in a linear fashion.
250  */
251 void
llvmpipe_fs_variant_linear_llvm(struct llvmpipe_context * lp,struct lp_fragment_shader * shader,struct lp_fragment_shader_variant * variant)252 llvmpipe_fs_variant_linear_llvm(struct llvmpipe_context *lp,
253                                 struct lp_fragment_shader *shader,
254                                 struct lp_fragment_shader_variant *variant)
255 {
256    struct gallivm_state *gallivm = variant->gallivm;
257    char func_name[256];
258    struct lp_type fs_type;
259    LLVMTypeRef ret_type;
260    LLVMTypeRef arg_types[4];
261    LLVMTypeRef func_type;
262    LLVMValueRef context_ptr;
263    LLVMValueRef x;
264    LLVMValueRef y;
265    LLVMValueRef width;
266    LLVMValueRef excess;
267    LLVMValueRef function;
268    LLVMBasicBlockRef block;
269    LLVMBuilderRef builder;
270    struct lp_build_context bld;
271    struct linear_sampler sampler;
272    struct lp_build_if_state ifstate;
273    struct lp_build_for_loop_state loop;
274 
275    LLVMValueRef consts_ptr;
276    LLVMValueRef interpolators_ptr;
277    LLVMValueRef samplers_ptr;
278    LLVMValueRef color0_ptr;
279    LLVMValueRef blend_color;
280    LLVMValueRef alpha_ref;
281 
282    LLVMValueRef inputs_ptrs[LP_MAX_LINEAR_INPUTS];
283 
284    LLVMTypeRef int8t = LLVMInt8TypeInContext(gallivm->context);
285    LLVMTypeRef int32t = LLVMInt32TypeInContext(gallivm->context);
286    LLVMTypeRef pint8t = LLVMPointerType(int8t, 0);
287    LLVMTypeRef pixelt = LLVMVectorType(int32t, 4);
288 
289    unsigned attrib;
290    unsigned i;
291 
292    memset(&fs_type, 0, sizeof fs_type);
293    fs_type.floating = FALSE;
294    fs_type.sign = FALSE;
295    fs_type.norm = TRUE;
296    fs_type.width = 8;
297    fs_type.length = 16;
298 
299    if (LP_DEBUG & DEBUG_TGSI) {
300       tgsi_dump(shader->base.tokens, 0);
301    }
302 
303    /*
304     * Generate the function prototype. Any change here must be reflected in
305     * lp_jit.h's lp_jit_frag_func function pointer type, and vice-versa.
306     */
307 
308    snprintf(func_name, sizeof(func_name), "fs%u_variant%u_linear",
309             shader->no, variant->no);
310 
311    ret_type = pint8t;
312    arg_types[0] = variant->jit_linear_context_ptr_type; /* context */
313    arg_types[1] = int32t;                              /* x */
314    arg_types[2] = int32t;                              /* y */
315    arg_types[3] = int32t;                              /* width */
316 
317    func_type = LLVMFunctionType(ret_type, arg_types, ARRAY_SIZE(arg_types), 0);
318 
319    function = LLVMAddFunction(gallivm->module, func_name, func_type);
320    LLVMSetFunctionCallConv(function, LLVMCCallConv);
321 
322    variant->linear_function = function;
323 
324    /* XXX: need to propagate noalias down into color param now we are
325     * passing a pointer-to-pointer?
326     */
327    for (i = 0; i < ARRAY_SIZE(arg_types); ++i) {
328       if (LLVMGetTypeKind(arg_types[i]) == LLVMPointerTypeKind) {
329          lp_add_function_attr(function, i + 1, LP_FUNC_ATTR_NOALIAS);
330       }
331    }
332 
333    context_ptr  = LLVMGetParam(function, 0);
334    x            = LLVMGetParam(function, 1);
335    y            = LLVMGetParam(function, 2);
336    width        = LLVMGetParam(function, 3);
337 
338    lp_build_name(context_ptr, "context");
339    lp_build_name(x, "x");
340    lp_build_name(y, "y");
341    lp_build_name(width, "width");
342 
343    /*
344     * Function body
345     */
346 
347    block = LLVMAppendBasicBlockInContext(gallivm->context, function, "entry");
348    builder = gallivm->builder;
349 
350    LLVMPositionBuilderAtEnd(builder, block);
351 
352    lp_build_context_init(&bld, gallivm, fs_type);
353 
354    /*
355     * Get context data
356     */
357 
358    consts_ptr = lp_jit_linear_context_constants(gallivm, context_ptr);
359    interpolators_ptr = lp_jit_linear_context_inputs(gallivm, context_ptr);
360    samplers_ptr = lp_jit_linear_context_tex(gallivm, context_ptr);
361 
362    color0_ptr = lp_jit_linear_context_color0(gallivm, context_ptr);
363    color0_ptr = LLVMBuildLoad(builder, color0_ptr, "");
364    color0_ptr = LLVMBuildBitCast(builder, color0_ptr, LLVMPointerType(bld.vec_type, 0), "");
365 
366    blend_color = lp_jit_linear_context_blend_color(gallivm, context_ptr);
367    blend_color = LLVMBuildLoad(builder, blend_color, "");
368    blend_color = lp_build_broadcast(gallivm, LLVMVectorType(int32t, 4), blend_color);
369    blend_color = LLVMBuildBitCast(builder, blend_color, LLVMVectorType(int8t, 16), "");
370 
371    alpha_ref = lp_jit_linear_context_alpha_ref(gallivm, context_ptr);
372    alpha_ref = LLVMBuildLoad(builder, alpha_ref, "");
373 
374    /*
375     * Invoke the input interpolators
376     */
377 
378    for (attrib = 0; attrib < shader->info.base.num_inputs; ++attrib) {
379       LLVMValueRef index;
380       LLVMValueRef elem;
381       LLVMValueRef fetch_ptr;
382       LLVMValueRef inputs_ptr;
383 
384       assert(attrib < LP_MAX_LINEAR_INPUTS);
385       if (attrib >= LP_MAX_LINEAR_INPUTS) {
386          break;
387       }
388 
389       index = LLVMConstInt(int32t, attrib, 0);
390 
391       elem = lp_build_array_get(bld.gallivm, interpolators_ptr, index);
392       assert(LLVMGetTypeKind(LLVMTypeOf(elem)) == LLVMPointerTypeKind);
393 
394       fetch_ptr = lp_build_pointer_get(builder, elem,
395                                        LLVMConstInt(int32t, 0, 0));
396       assert(LLVMGetTypeKind(LLVMTypeOf(fetch_ptr)) == LLVMPointerTypeKind);
397 
398       /* Pointer to a row of interpolated inputs */
399       elem = LLVMBuildBitCast(builder, elem, pint8t, "");
400       inputs_ptr = LLVMBuildCall(builder, fetch_ptr, &elem, 1, "");
401       assert(LLVMGetTypeKind(LLVMTypeOf(inputs_ptr)) == LLVMPointerTypeKind);
402 
403       /* Mark the function read-only so that LLVM can optimize it away */
404       lp_add_function_attr(inputs_ptr, -1, LP_FUNC_ATTR_READONLY);
405       lp_add_function_attr(inputs_ptr, -1, LP_FUNC_ATTR_NOUNWIND);
406 
407       lp_build_name(inputs_ptr, "input%u_ptr", attrib);
408 
409       inputs_ptrs[attrib] = inputs_ptr;
410    }
411 
412    /*
413     * Invoke and hook up the texture samplers.
414     */
415 
416    memset(&sampler, 0, sizeof sampler);
417    sampler.base.emit_fetch_texel = &emit_fetch_texel_linear;
418 
419    for (attrib = 0; attrib < shader->info.num_texs; ++attrib) {
420       LLVMValueRef index;
421       LLVMValueRef elem;
422       LLVMValueRef fetch_ptr;
423       LLVMValueRef texels_ptr;
424 
425       assert(attrib < LP_MAX_LINEAR_TEXTURES);
426       if (attrib >= LP_MAX_LINEAR_TEXTURES) {
427          break;
428       }
429 
430       index = LLVMConstInt(int32t, attrib, 0);
431 
432       elem = lp_build_array_get(bld.gallivm, samplers_ptr, index);
433       assert(LLVMGetTypeKind(LLVMTypeOf(elem)) == LLVMPointerTypeKind);
434 
435       fetch_ptr = lp_build_pointer_get(builder, elem, LLVMConstInt(int32t, 0, 0));
436       assert(LLVMGetTypeKind(LLVMTypeOf(fetch_ptr)) == LLVMPointerTypeKind);
437 
438       /* Pointer to a row of texels */
439       elem = LLVMBuildBitCast(builder, elem, pint8t, "");
440       texels_ptr = LLVMBuildCall(builder, fetch_ptr, &elem, 1, "");
441       assert(LLVMGetTypeKind(LLVMTypeOf(texels_ptr)) == LLVMPointerTypeKind);
442 
443       /* Mark the function read-only so that LLVM can optimize it away */
444       lp_add_function_attr(texels_ptr, -1, LP_FUNC_ATTR_READONLY);
445       lp_add_function_attr(texels_ptr, -1, LP_FUNC_ATTR_NOUNWIND);
446 
447       lp_build_name(texels_ptr, "tex%u_ptr", attrib);
448 
449       sampler.texels_ptrs[attrib] = texels_ptr;
450    }
451 
452    excess = LLVMBuildAnd(builder, width, LLVMConstInt(int32t, 3, 0), "");
453    width = LLVMBuildLShr(builder, width, LLVMConstInt(int32t, 2, 0), "");
454 
455    /* Loop over blocks of 4 pixels */
456    lp_build_for_loop_begin(&loop, gallivm, LLVMConstInt(int32t, 0, 0), LLVMIntULT, width, LLVMConstInt(int32t, 1, 0));
457    {
458       LLVMValueRef value;
459       sampler.counter = loop.counter;
460 
461       /* Read 4 pixels */
462       value = lp_build_pointer_get_unaligned(builder, color0_ptr, loop.counter, 4);
463 
464       /* Perform fragment shader body */
465       value = llvm_fragment_body(&bld, shader, variant, &sampler, inputs_ptrs, consts_ptr, blend_color, alpha_ref, fs_type, value);
466 
467       /* Write 4 pixels */
468       lp_build_pointer_set_unaligned(builder, color0_ptr, loop.counter, value, 4);
469    }
470    lp_build_for_loop_end(&loop);
471 
472    /* Compute the edge pixels (width % 4) */
473    lp_build_if(&ifstate, gallivm, LLVMBuildICmp(builder, LLVMIntNE, excess, LLVMConstInt(int32t, 0, 0), ""));
474    {
475       struct lp_build_loop_state loop_read, loop_write;
476       LLVMValueRef buf, elem, result, pixel_ptr;
477       LLVMValueRef buf_ptr = lp_build_alloca(gallivm, pixelt, "");
478 
479       sampler.counter = width;
480 
481       /* Get the i32* pixel pointer from the <i16x8>* element pointer */
482       pixel_ptr = LLVMBuildGEP(gallivm->builder, color0_ptr, &width, 1, "");
483       pixel_ptr = LLVMBuildBitCast(gallivm->builder, pixel_ptr, LLVMPointerType(int32t, 0), "");
484 
485       /* Copy individual pixels from memory to local buffer */
486       lp_build_loop_begin(&loop_read, gallivm, LLVMConstInt(int32t, 0, 0));
487       {
488          elem = lp_build_pointer_get(gallivm->builder, pixel_ptr, loop_read.counter);
489 
490          buf = LLVMBuildLoad(gallivm->builder, buf_ptr, "");
491          buf = LLVMBuildInsertElement(builder, buf, elem, loop_read.counter, "");
492          LLVMBuildStore(builder, buf, buf_ptr);
493       }
494       lp_build_loop_end_cond(&loop_read, excess, LLVMConstInt(int32t, 1, 0), LLVMIntUGE);
495 
496       /* Perform fragment shader body */
497       buf = LLVMBuildLoad(gallivm->builder, buf_ptr, "");
498       buf = LLVMBuildBitCast(builder, buf, bld.vec_type, "");
499 
500       result = llvm_fragment_body(&bld, shader, variant, &sampler, inputs_ptrs, consts_ptr, blend_color, alpha_ref, fs_type, buf);
501       result = LLVMBuildBitCast(builder, result, pixelt, "");
502 
503       /* Write individual pixels from local buffer to the memory */
504       lp_build_loop_begin(&loop_write, gallivm, LLVMConstInt(int32t, 0, 0));
505       {
506          elem = LLVMBuildExtractElement(builder, result, loop_write.counter, "");
507 
508          lp_build_pointer_set(gallivm->builder, pixel_ptr, loop_write.counter, elem);
509       }
510       lp_build_loop_end_cond(&loop_write, excess, LLVMConstInt(int32t, 1, 0), LLVMIntUGE);
511    }
512    lp_build_endif(&ifstate);
513 
514 
515    color0_ptr = LLVMBuildBitCast(builder, color0_ptr, pint8t, "");
516 
517    LLVMBuildRet(builder, color0_ptr);
518 
519    gallivm_verify_function(gallivm, function);
520 }
521 
522 
523