forked from notaz/mesa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlp_bld_sample_soa.c
4989 lines (4477 loc) · 205 KB
/
lp_bld_sample_soa.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**************************************************************************
*
* Copyright 2009 VMware, Inc.
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sub license, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
**************************************************************************/
/**
* @file
* Texture sampling -- SoA.
*
* @author Jose Fonseca <[email protected]>
* @author Brian Paul <[email protected]>
*/
#include "pipe/p_defines.h"
#include "pipe/p_state.h"
#include "pipe/p_shader_tokens.h"
#include "util/compiler.h"
#include "util/u_debug.h"
#include "util/u_dump.h"
#include "util/u_memory.h"
#include "util/u_math.h"
#include "util/format/u_format.h"
#include "util/u_cpu_detect.h"
#include "util/format_rgb9e5.h"
#include "lp_bld_debug.h"
#include "lp_bld_type.h"
#include "lp_bld_const.h"
#include "lp_bld_conv.h"
#include "lp_bld_arit.h"
#include "lp_bld_bitarit.h"
#include "lp_bld_logic.h"
#include "lp_bld_printf.h"
#include "lp_bld_swizzle.h"
#include "lp_bld_flow.h"
#include "lp_bld_gather.h"
#include "lp_bld_format.h"
#include "lp_bld_sample.h"
#include "lp_bld_sample_aos.h"
#include "lp_bld_struct.h"
#include "lp_bld_quad.h"
#include "lp_bld_pack.h"
#include "lp_bld_intr.h"
#include "lp_bld_misc.h"
/**
* Generate code to fetch a texel from a texture at int coords (x, y, z).
* The computation depends on whether the texture is 1D, 2D or 3D.
* The result, texel, will be float vectors:
* texel[0] = red values
* texel[1] = green values
* texel[2] = blue values
* texel[3] = alpha values
*/
static void
lp_build_sample_texel_soa(struct lp_build_sample_context *bld,
LLVMValueRef width,
LLVMValueRef height,
LLVMValueRef depth,
LLVMValueRef x,
LLVMValueRef y,
LLVMValueRef z,
LLVMValueRef y_stride,
LLVMValueRef z_stride,
LLVMValueRef data_ptr,
LLVMValueRef mipoffsets,
LLVMValueRef texel_out[4])
{
const struct lp_static_sampler_state *static_state = bld->static_sampler_state;
const unsigned dims = bld->dims;
struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
LLVMBuilderRef builder = bld->gallivm->builder;
LLVMValueRef offset;
LLVMValueRef i, j;
LLVMValueRef use_border = NULL;
/* use_border = x < 0 || x >= width || y < 0 || y >= height */
if (lp_sampler_wrap_mode_uses_border_color(static_state->wrap_s,
static_state->min_img_filter,
static_state->mag_img_filter)) {
LLVMValueRef b1, b2;
b1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, x, int_coord_bld->zero);
b2 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, x, width);
use_border = LLVMBuildOr(builder, b1, b2, "b1_or_b2");
}
if (dims >= 2 &&
lp_sampler_wrap_mode_uses_border_color(static_state->wrap_t,
static_state->min_img_filter,
static_state->mag_img_filter)) {
LLVMValueRef b1, b2;
b1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, y, int_coord_bld->zero);
b2 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, y, height);
if (use_border) {
use_border = LLVMBuildOr(builder, use_border, b1, "ub_or_b1");
use_border = LLVMBuildOr(builder, use_border, b2, "ub_or_b2");
}
else {
use_border = LLVMBuildOr(builder, b1, b2, "b1_or_b2");
}
}
if (dims == 3 &&
lp_sampler_wrap_mode_uses_border_color(static_state->wrap_r,
static_state->min_img_filter,
static_state->mag_img_filter)) {
LLVMValueRef b1, b2;
b1 = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, z, int_coord_bld->zero);
b2 = lp_build_cmp(int_coord_bld, PIPE_FUNC_GEQUAL, z, depth);
if (use_border) {
use_border = LLVMBuildOr(builder, use_border, b1, "ub_or_b1");
use_border = LLVMBuildOr(builder, use_border, b2, "ub_or_b2");
}
else {
use_border = LLVMBuildOr(builder, b1, b2, "b1_or_b2");
}
}
/* convert x,y,z coords to linear offset from start of texture, in bytes */
lp_build_sample_offset(&bld->int_coord_bld,
bld->format_desc,
x, y, z, y_stride, z_stride,
&offset, &i, &j);
if (mipoffsets) {
offset = lp_build_add(&bld->int_coord_bld, offset, mipoffsets);
}
if (use_border) {
/* If we can sample the border color, it means that texcoords may
* lie outside the bounds of the texture image. We need to do
* something to prevent reading out of bounds and causing a segfault.
*
* Simply AND the texture coords with !use_border. This will cause
* coords which are out of bounds to become zero. Zero's guaranteed
* to be inside the texture image.
*/
offset = lp_build_andnot(&bld->int_coord_bld, offset, use_border);
}
lp_build_fetch_rgba_soa(bld->gallivm,
bld->format_desc,
bld->texel_type, TRUE,
data_ptr, offset,
i, j,
bld->cache,
texel_out);
/*
* Note: if we find an app which frequently samples the texture border
* we might want to implement a true conditional here to avoid sampling
* the texture whenever possible (since that's quite a bit of code).
* Ex:
* if (use_border) {
* texel = border_color;
* }
* else {
* texel = sample_texture(coord);
* }
* As it is now, we always sample the texture, then selectively replace
* the texel color results with the border color.
*/
if (use_border) {
/* select texel color or border color depending on use_border. */
const struct util_format_description *format_desc = bld->format_desc;
int chan;
struct lp_type border_type = bld->texel_type;
border_type.length = 4;
/*
* Only replace channels which are actually present. The others should
* get optimized away eventually by sampler_view swizzle anyway but it's
* easier too.
*/
for (chan = 0; chan < 4; chan++) {
unsigned chan_s;
/* reverse-map channel... */
if (util_format_has_stencil(format_desc)) {
if (chan == 0)
chan_s = 0;
else
break;
}
else {
for (chan_s = 0; chan_s < 4; chan_s++) {
if (chan_s == format_desc->swizzle[chan]) {
break;
}
}
}
if (chan_s <= 3) {
/* use the already clamped color */
LLVMValueRef idx = lp_build_const_int32(bld->gallivm, chan);
LLVMValueRef border_chan;
border_chan = lp_build_extract_broadcast(bld->gallivm,
border_type,
bld->texel_type,
bld->border_color_clamped,
idx);
texel_out[chan] = lp_build_select(&bld->texel_bld, use_border,
border_chan, texel_out[chan]);
}
}
}
}
/**
* Helper to compute the mirror function for the PIPE_WRAP_MIRROR_REPEAT mode.
* (Note that with pot sizes could do this much more easily post-scale
* with some bit arithmetic.)
*/
static LLVMValueRef
lp_build_coord_mirror(struct lp_build_sample_context *bld,
LLVMValueRef coord, boolean posOnly)
{
struct lp_build_context *coord_bld = &bld->coord_bld;
LLVMValueRef fract;
LLVMValueRef half = lp_build_const_vec(bld->gallivm, coord_bld->type, 0.5);
/*
* We can just use 2*(x - round(0.5*x)) to do all the mirroring,
* it all works out. (The result is in range [-1, 1.0], negative if
* the coord is in the "odd" section, otherwise positive.)
*/
coord = lp_build_mul(coord_bld, coord, half);
fract = lp_build_round(coord_bld, coord);
fract = lp_build_sub(coord_bld, coord, fract);
coord = lp_build_add(coord_bld, fract, fract);
if (posOnly) {
/*
* Theoretically it's not quite 100% accurate because the spec says
* that ultimately a scaled coord of -x.0 should map to int coord
* -x + 1 with mirroring, not -x (this does not matter for bilinear
* filtering).
*/
coord = lp_build_abs(coord_bld, coord);
/* kill off NaNs */
/* XXX: not safe without arch rounding, fract can be anything. */
coord = lp_build_max_ext(coord_bld, coord, coord_bld->zero,
GALLIVM_NAN_RETURN_OTHER_SECOND_NONNAN);
}
return coord;
}
/**
* Helper to compute the first coord and the weight for
* linear wrap repeat npot textures
*/
void
lp_build_coord_repeat_npot_linear(struct lp_build_sample_context *bld,
LLVMValueRef coord_f,
LLVMValueRef length_i,
LLVMValueRef length_f,
LLVMValueRef *coord0_i,
LLVMValueRef *weight_f)
{
struct lp_build_context *coord_bld = &bld->coord_bld;
struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
LLVMValueRef half = lp_build_const_vec(bld->gallivm, coord_bld->type, 0.5);
LLVMValueRef length_minus_one = lp_build_sub(int_coord_bld, length_i,
int_coord_bld->one);
LLVMValueRef mask;
/* wrap with normalized floats is just fract */
coord_f = lp_build_fract(coord_bld, coord_f);
/* mul by size and subtract 0.5 */
coord_f = lp_build_mul(coord_bld, coord_f, length_f);
coord_f = lp_build_sub(coord_bld, coord_f, half);
/*
* we avoided the 0.5/length division before the repeat wrap,
* now need to fix up edge cases with selects
*/
/*
* Note we do a float (unordered) compare so we can eliminate NaNs.
* (Otherwise would need fract_safe above).
*/
mask = lp_build_compare(coord_bld->gallivm, coord_bld->type,
PIPE_FUNC_LESS, coord_f, coord_bld->zero);
/* convert to int, compute lerp weight */
lp_build_ifloor_fract(coord_bld, coord_f, coord0_i, weight_f);
*coord0_i = lp_build_select(int_coord_bld, mask, length_minus_one, *coord0_i);
}
/**
* Build LLVM code for texture wrap mode for linear filtering.
* \param x0_out returns first integer texcoord
* \param x1_out returns second integer texcoord
* \param weight_out returns linear interpolation weight
*/
static void
lp_build_sample_wrap_linear(struct lp_build_sample_context *bld,
boolean is_gather,
LLVMValueRef coord,
LLVMValueRef length,
LLVMValueRef length_f,
LLVMValueRef offset,
boolean is_pot,
unsigned wrap_mode,
LLVMValueRef *x0_out,
LLVMValueRef *x1_out,
LLVMValueRef *weight_out)
{
struct lp_build_context *coord_bld = &bld->coord_bld;
struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
LLVMBuilderRef builder = bld->gallivm->builder;
LLVMValueRef half = lp_build_const_vec(bld->gallivm, coord_bld->type, 0.5);
LLVMValueRef length_minus_one = lp_build_sub(int_coord_bld, length, int_coord_bld->one);
LLVMValueRef coord0, coord1, weight;
switch(wrap_mode) {
case PIPE_TEX_WRAP_REPEAT:
if (is_pot) {
/* mul by size and subtract 0.5 */
coord = lp_build_mul(coord_bld, coord, length_f);
coord = lp_build_sub(coord_bld, coord, half);
if (offset) {
offset = lp_build_int_to_float(coord_bld, offset);
coord = lp_build_add(coord_bld, coord, offset);
}
/* convert to int, compute lerp weight */
lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
/* repeat wrap */
coord0 = LLVMBuildAnd(builder, coord0, length_minus_one, "");
coord1 = LLVMBuildAnd(builder, coord1, length_minus_one, "");
}
else {
LLVMValueRef mask;
if (offset) {
offset = lp_build_int_to_float(coord_bld, offset);
offset = lp_build_div(coord_bld, offset, length_f);
coord = lp_build_add(coord_bld, coord, offset);
}
lp_build_coord_repeat_npot_linear(bld, coord,
length, length_f,
&coord0, &weight);
mask = lp_build_compare(int_coord_bld->gallivm, int_coord_bld->type,
PIPE_FUNC_NOTEQUAL, coord0, length_minus_one);
coord1 = LLVMBuildAnd(builder,
lp_build_add(int_coord_bld, coord0, int_coord_bld->one),
mask, "");
}
break;
case PIPE_TEX_WRAP_CLAMP:
if (bld->static_sampler_state->normalized_coords) {
/* scale coord to length */
coord = lp_build_mul(coord_bld, coord, length_f);
}
if (offset) {
offset = lp_build_int_to_float(coord_bld, offset);
coord = lp_build_add(coord_bld, coord, offset);
}
/*
* clamp to [0, length]
*
* Unlike some other wrap modes, this should be correct for gather
* too. GL_CLAMP explicitly does this clamp on the coord prior to
* actual wrapping (which is per sample).
*/
coord = lp_build_clamp(coord_bld, coord, coord_bld->zero, length_f);
coord = lp_build_sub(coord_bld, coord, half);
/* convert to int, compute lerp weight */
lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
break;
case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
{
struct lp_build_context abs_coord_bld = bld->coord_bld;
abs_coord_bld.type.sign = FALSE;
if (bld->static_sampler_state->normalized_coords) {
/* mul by tex size */
coord = lp_build_mul(coord_bld, coord, length_f);
}
if (offset) {
offset = lp_build_int_to_float(coord_bld, offset);
coord = lp_build_add(coord_bld, coord, offset);
}
/* clamp to length max */
coord = lp_build_min_ext(coord_bld, coord, length_f,
GALLIVM_NAN_RETURN_OTHER_SECOND_NONNAN);
if (!is_gather) {
/* subtract 0.5 */
coord = lp_build_sub(coord_bld, coord, half);
/* clamp to [0, length - 0.5] */
coord = lp_build_max(coord_bld, coord, coord_bld->zero);
/* convert to int, compute lerp weight */
lp_build_ifloor_fract(&abs_coord_bld, coord, &coord0, &weight);
coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
} else {
/*
* The non-gather path will end up with coords 0, 1 if coord was
* smaller than 0.5 (with corresponding weight 0.0 so it doesn't
* really matter what the second coord is). But for gather, we
* really need to end up with coords 0, 0.
*/
coord = lp_build_max(coord_bld, coord, coord_bld->zero);
coord0 = lp_build_sub(coord_bld, coord, half);
coord1 = lp_build_add(coord_bld, coord, half);
/* Values range ([-0.5, length_f - 0.5], [0.5, length_f + 0.5] */
coord0 = lp_build_itrunc(coord_bld, coord0);
coord1 = lp_build_itrunc(coord_bld, coord1);
weight = coord_bld->undef;
}
/* coord1 = min(coord1, length-1) */
coord1 = lp_build_min(int_coord_bld, coord1, length_minus_one);
break;
}
case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
if (bld->static_sampler_state->normalized_coords) {
/* scale coord to length */
coord = lp_build_mul(coord_bld, coord, length_f);
}
if (offset) {
offset = lp_build_int_to_float(coord_bld, offset);
coord = lp_build_add(coord_bld, coord, offset);
}
/*
* We don't need any clamp. Technically, for very large (pos or neg)
* (or infinite) values, clamp against [-length, length] would be
* correct, but we don't need to guarantee any specific
* result for such coords (the ifloor will be undefined, but for modes
* requiring border all resulting coords are safe).
*/
coord = lp_build_sub(coord_bld, coord, half);
/* convert to int, compute lerp weight */
lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
break;
case PIPE_TEX_WRAP_MIRROR_REPEAT:
if (offset) {
offset = lp_build_int_to_float(coord_bld, offset);
offset = lp_build_div(coord_bld, offset, length_f);
coord = lp_build_add(coord_bld, coord, offset);
}
if (!is_gather) {
/* compute mirror function */
coord = lp_build_coord_mirror(bld, coord, TRUE);
/* scale coord to length */
coord = lp_build_mul(coord_bld, coord, length_f);
coord = lp_build_sub(coord_bld, coord, half);
/* convert to int, compute lerp weight */
lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
/* coord0 = max(coord0, 0) */
coord0 = lp_build_max(int_coord_bld, coord0, int_coord_bld->zero);
/* coord1 = min(coord1, length-1) */
coord1 = lp_build_min(int_coord_bld, coord1, length_minus_one);
} else {
/*
* This is pretty reasonable in the end, all what the tests care
* about is nasty edge cases (scaled coords x.5, so the individual
* coords are actually integers, which is REALLY tricky to get right
* due to this working differently both for negative numbers as well
* as for even/odd cases). But with enough magic it's not too complex
* after all.
* Maybe should try a bit arithmetic one though for POT textures...
*/
LLVMValueRef isNeg;
/*
* Wrapping just once still works, even though it means we can
* get "wrong" sign due to performing mirror in the middle of the
* two coords (because this can only happen very near the odd/even
* edges, so both coords will actually end up as 0 or length - 1
* in the end).
* For GL4 gather with per-sample offsets we'd need to the mirroring
* per coord too.
*/
coord = lp_build_coord_mirror(bld, coord, FALSE);
coord = lp_build_mul(coord_bld, coord, length_f);
/*
* NaNs should be safe here, we'll do away with them with
* the ones' complement plus min.
*/
coord0 = lp_build_sub(coord_bld, coord, half);
coord0 = lp_build_ifloor(coord_bld, coord0);
coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
/* ones complement for neg numbers (mirror(negX) = X - 1) */
isNeg = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS,
coord0, int_coord_bld->zero);
coord0 = lp_build_xor(int_coord_bld, coord0, isNeg);
isNeg = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS,
coord1, int_coord_bld->zero);
coord1 = lp_build_xor(int_coord_bld, coord1, isNeg);
coord0 = lp_build_min(int_coord_bld, coord0, length_minus_one);
coord1 = lp_build_min(int_coord_bld, coord1, length_minus_one);
weight = coord_bld->undef;
}
break;
case PIPE_TEX_WRAP_MIRROR_CLAMP:
if (bld->static_sampler_state->normalized_coords) {
/* scale coord to length */
coord = lp_build_mul(coord_bld, coord, length_f);
}
if (offset) {
offset = lp_build_int_to_float(coord_bld, offset);
coord = lp_build_add(coord_bld, coord, offset);
}
/*
* XXX: probably not correct for gather, albeit I'm not
* entirely sure as it's poorly specified. The wrapping looks
* correct according to the spec which is against gl 1.2.1,
* however negative values will be swapped - gl re-specified
* wrapping with newer versions (no more pre-clamp except with
* GL_CLAMP).
*/
coord = lp_build_abs(coord_bld, coord);
/* clamp to [0, length] */
coord = lp_build_min_ext(coord_bld, coord, length_f,
GALLIVM_NAN_RETURN_OTHER_SECOND_NONNAN);
coord = lp_build_sub(coord_bld, coord, half);
/* convert to int, compute lerp weight */
lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
break;
case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
{
struct lp_build_context abs_coord_bld = bld->coord_bld;
abs_coord_bld.type.sign = FALSE;
if (bld->static_sampler_state->normalized_coords) {
/* scale coord to length */
coord = lp_build_mul(coord_bld, coord, length_f);
}
if (offset) {
offset = lp_build_int_to_float(coord_bld, offset);
coord = lp_build_add(coord_bld, coord, offset);
}
if (!is_gather) {
coord = lp_build_abs(coord_bld, coord);
/* clamp to length max */
coord = lp_build_min_ext(coord_bld, coord, length_f,
GALLIVM_NAN_RETURN_OTHER_SECOND_NONNAN);
/* subtract 0.5 */
coord = lp_build_sub(coord_bld, coord, half);
/* clamp to [0, length - 0.5] */
coord = lp_build_max(coord_bld, coord, coord_bld->zero);
/* convert to int, compute lerp weight */
lp_build_ifloor_fract(&abs_coord_bld, coord, &coord0, &weight);
coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
/* coord1 = min(coord1, length-1) */
coord1 = lp_build_min(int_coord_bld, coord1, length_minus_one);
} else {
/*
* The non-gather path will swap coord0/1 if coord was negative,
* which is ok for filtering since the filter weight matches
* accordingly. Also, if coord is close to zero, coord0/1 will
* be 0 and 1, instead of 0 and 0 (again ok due to filter
* weight being 0.0). Both issues need to be fixed for gather.
*/
LLVMValueRef isNeg;
/*
* Actually wanted to cheat here and use:
* coord1 = lp_build_iround(coord_bld, coord);
* but it's not good enough for some tests (even piglit
* textureGather is set up in a way so the coords area always
* .5, that is right at the crossover points).
* So do ordinary sub/floor, then do ones' complement
* for negative numbers.
* (Note can't just do sub|add/abs/itrunc per coord neither -
* because the spec demands that mirror(3.0) = 3 but
* mirror(-3.0) = 2.)
*/
coord = lp_build_sub(coord_bld, coord, half);
coord0 = lp_build_ifloor(coord_bld, coord);
coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
isNeg = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, coord0,
int_coord_bld->zero);
coord0 = lp_build_xor(int_coord_bld, isNeg, coord0);
coord0 = lp_build_min(int_coord_bld, coord0, length_minus_one);
isNeg = lp_build_cmp(int_coord_bld, PIPE_FUNC_LESS, coord1,
int_coord_bld->zero);
coord1 = lp_build_xor(int_coord_bld, isNeg, coord1);
coord1 = lp_build_min(int_coord_bld, coord1, length_minus_one);
weight = coord_bld->undef;
}
}
break;
case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
{
if (bld->static_sampler_state->normalized_coords) {
/* scale coord to length */
coord = lp_build_mul(coord_bld, coord, length_f);
}
if (offset) {
offset = lp_build_int_to_float(coord_bld, offset);
coord = lp_build_add(coord_bld, coord, offset);
}
/*
* XXX: probably not correct for gather due to swapped
* order if coord is negative (same rationale as for
* MIRROR_CLAMP).
*/
coord = lp_build_abs(coord_bld, coord);
/*
* We don't need any clamp. Technically, for very large
* (or infinite) values, clamp against length would be
* correct, but we don't need to guarantee any specific
* result for such coords (the ifloor will be undefined, but
* for modes requiring border all resulting coords are safe).
*/
coord = lp_build_sub(coord_bld, coord, half);
/* convert to int, compute lerp weight */
lp_build_ifloor_fract(coord_bld, coord, &coord0, &weight);
coord1 = lp_build_add(int_coord_bld, coord0, int_coord_bld->one);
}
break;
default:
assert(0);
coord0 = NULL;
coord1 = NULL;
weight = NULL;
}
*x0_out = coord0;
*x1_out = coord1;
*weight_out = weight;
}
/**
* Build LLVM code for texture wrap mode for nearest filtering.
* \param coord the incoming texcoord (nominally in [0,1])
* \param length the texture size along one dimension, as int vector
* \param length_f the texture size along one dimension, as float vector
* \param offset texel offset along one dimension (as int vector)
* \param is_pot if TRUE, length is a power of two
* \param wrap_mode one of PIPE_TEX_WRAP_x
*/
static LLVMValueRef
lp_build_sample_wrap_nearest(struct lp_build_sample_context *bld,
LLVMValueRef coord,
LLVMValueRef length,
LLVMValueRef length_f,
LLVMValueRef offset,
boolean is_pot,
unsigned wrap_mode)
{
struct lp_build_context *coord_bld = &bld->coord_bld;
struct lp_build_context *int_coord_bld = &bld->int_coord_bld;
LLVMBuilderRef builder = bld->gallivm->builder;
LLVMValueRef length_minus_one = lp_build_sub(int_coord_bld, length, int_coord_bld->one);
LLVMValueRef icoord;
switch(wrap_mode) {
case PIPE_TEX_WRAP_REPEAT:
if (is_pot) {
coord = lp_build_mul(coord_bld, coord, length_f);
icoord = lp_build_ifloor(coord_bld, coord);
if (offset) {
icoord = lp_build_add(int_coord_bld, icoord, offset);
}
icoord = LLVMBuildAnd(builder, icoord, length_minus_one, "");
}
else {
if (offset) {
offset = lp_build_int_to_float(coord_bld, offset);
offset = lp_build_div(coord_bld, offset, length_f);
coord = lp_build_add(coord_bld, coord, offset);
}
/* take fraction, unnormalize */
coord = lp_build_fract_safe(coord_bld, coord);
coord = lp_build_mul(coord_bld, coord, length_f);
icoord = lp_build_itrunc(coord_bld, coord);
}
break;
case PIPE_TEX_WRAP_CLAMP:
case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
if (bld->static_sampler_state->normalized_coords) {
/* scale coord to length */
coord = lp_build_mul(coord_bld, coord, length_f);
}
if (offset) {
offset = lp_build_int_to_float(coord_bld, offset);
coord = lp_build_add(coord_bld, coord, offset);
}
/* floor */
/* use itrunc instead since we clamp to 0 anyway */
icoord = lp_build_itrunc(coord_bld, coord);
/* clamp to [0, length - 1]. */
icoord = lp_build_clamp(int_coord_bld, icoord, int_coord_bld->zero,
length_minus_one);
break;
case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
if (bld->static_sampler_state->normalized_coords) {
/* scale coord to length */
coord = lp_build_mul(coord_bld, coord, length_f);
}
/* no clamp necessary, border masking will handle this */
icoord = lp_build_ifloor(coord_bld, coord);
if (offset) {
icoord = lp_build_add(int_coord_bld, icoord, offset);
}
break;
case PIPE_TEX_WRAP_MIRROR_REPEAT:
if (offset) {
offset = lp_build_int_to_float(coord_bld, offset);
offset = lp_build_div(coord_bld, offset, length_f);
coord = lp_build_add(coord_bld, coord, offset);
}
/* compute mirror function */
coord = lp_build_coord_mirror(bld, coord, TRUE);
/* scale coord to length */
assert(bld->static_sampler_state->normalized_coords);
coord = lp_build_mul(coord_bld, coord, length_f);
/* itrunc == ifloor here */
icoord = lp_build_itrunc(coord_bld, coord);
/* clamp to [0, length - 1] */
icoord = lp_build_min(int_coord_bld, icoord, length_minus_one);
break;
case PIPE_TEX_WRAP_MIRROR_CLAMP:
case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
if (bld->static_sampler_state->normalized_coords) {
/* scale coord to length */
coord = lp_build_mul(coord_bld, coord, length_f);
}
if (offset) {
offset = lp_build_int_to_float(coord_bld, offset);
coord = lp_build_add(coord_bld, coord, offset);
}
coord = lp_build_abs(coord_bld, coord);
/* itrunc == ifloor here */
icoord = lp_build_itrunc(coord_bld, coord);
/*
* Use unsigned min due to possible undef values (NaNs, overflow)
*/
{
struct lp_build_context abs_coord_bld = *int_coord_bld;
abs_coord_bld.type.sign = FALSE;
/* clamp to [0, length - 1] */
icoord = lp_build_min(&abs_coord_bld, icoord, length_minus_one);
}
break;
case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
if (bld->static_sampler_state->normalized_coords) {
/* scale coord to length */
coord = lp_build_mul(coord_bld, coord, length_f);
}
if (offset) {
offset = lp_build_int_to_float(coord_bld, offset);
coord = lp_build_add(coord_bld, coord, offset);
}
coord = lp_build_abs(coord_bld, coord);
/* itrunc == ifloor here */
icoord = lp_build_itrunc(coord_bld, coord);
break;
default:
assert(0);
icoord = NULL;
}
return icoord;
}
/**
* Do shadow test/comparison.
* \param p shadow ref value
* \param texel the texel to compare against
*/
static LLVMValueRef
lp_build_sample_comparefunc(struct lp_build_sample_context *bld,
LLVMValueRef p,
LLVMValueRef texel)
{
struct lp_build_context *texel_bld = &bld->texel_bld;
LLVMValueRef res;
if (0) {
//lp_build_print_value(bld->gallivm, "shadow cmp coord", p);
lp_build_print_value(bld->gallivm, "shadow cmp texel", texel);
}
/* result = (p FUNC texel) ? 1 : 0 */
/*
* honor d3d10 floating point rules here, which state that comparisons
* are ordered except NOT_EQUAL which is unordered.
*/
if (bld->static_sampler_state->compare_func != PIPE_FUNC_NOTEQUAL) {
res = lp_build_cmp_ordered(texel_bld, bld->static_sampler_state->compare_func,
p, texel);
}
else {
res = lp_build_cmp(texel_bld, bld->static_sampler_state->compare_func,
p, texel);
}
return res;
}
/**
* Generate code to sample a mipmap level with nearest filtering.
* If sampling a cube texture, r = cube face in [0,5].
*/
static void
lp_build_sample_image_nearest(struct lp_build_sample_context *bld,
LLVMValueRef size,
LLVMValueRef row_stride_vec,
LLVMValueRef img_stride_vec,
LLVMValueRef data_ptr,
LLVMValueRef mipoffsets,
const LLVMValueRef *coords,
const LLVMValueRef *offsets,
LLVMValueRef colors_out[4])
{
const unsigned dims = bld->dims;
LLVMValueRef width_vec;
LLVMValueRef height_vec;
LLVMValueRef depth_vec;
LLVMValueRef flt_size;
LLVMValueRef flt_width_vec;
LLVMValueRef flt_height_vec;
LLVMValueRef flt_depth_vec;
LLVMValueRef x, y = NULL, z = NULL;
lp_build_extract_image_sizes(bld,
&bld->int_size_bld,
bld->int_coord_type,
size,
&width_vec, &height_vec, &depth_vec);
flt_size = lp_build_int_to_float(&bld->float_size_bld, size);
lp_build_extract_image_sizes(bld,
&bld->float_size_bld,
bld->coord_type,
flt_size,
&flt_width_vec, &flt_height_vec, &flt_depth_vec);
/*
* Compute integer texcoords.
*/
x = lp_build_sample_wrap_nearest(bld, coords[0], width_vec,
flt_width_vec, offsets[0],
bld->static_texture_state->pot_width,
bld->static_sampler_state->wrap_s);
lp_build_name(x, "tex.x.wrapped");
if (dims >= 2) {
y = lp_build_sample_wrap_nearest(bld, coords[1], height_vec,
flt_height_vec, offsets[1],
bld->static_texture_state->pot_height,
bld->static_sampler_state->wrap_t);
lp_build_name(y, "tex.y.wrapped");
if (dims == 3) {
z = lp_build_sample_wrap_nearest(bld, coords[2], depth_vec,
flt_depth_vec, offsets[2],
bld->static_texture_state->pot_depth,
bld->static_sampler_state->wrap_r);
lp_build_name(z, "tex.z.wrapped");
}
}
if (has_layer_coord(bld->static_texture_state->target)) {
if (bld->static_texture_state->target == PIPE_TEXTURE_CUBE_ARRAY) {
/* add cube layer to face */
z = lp_build_add(&bld->int_coord_bld, coords[2], coords[3]);
}
else {
z = coords[2];
}
lp_build_name(z, "tex.z.layer");
}
/*
* Get texture colors.
*/
lp_build_sample_texel_soa(bld,
width_vec, height_vec, depth_vec,
x, y, z,
row_stride_vec, img_stride_vec,
data_ptr, mipoffsets, colors_out);
if (bld->static_sampler_state->compare_mode != PIPE_TEX_COMPARE_NONE) {
LLVMValueRef cmpval;
cmpval = lp_build_sample_comparefunc(bld, coords[4], colors_out[0]);
/* this is really just a AND 1.0, cmpval but llvm is clever enough */
colors_out[0] = lp_build_select(&bld->texel_bld, cmpval,
bld->texel_bld.one, bld->texel_bld.zero);
colors_out[1] = colors_out[2] = colors_out[3] = colors_out[0];
}
}
/**
* Like a lerp, but inputs are 0/~0 masks, so can simplify slightly.
*/
static LLVMValueRef
lp_build_masklerp(struct lp_build_context *bld,
LLVMValueRef weight,
LLVMValueRef mask0,
LLVMValueRef mask1)
{
struct gallivm_state *gallivm = bld->gallivm;
LLVMBuilderRef builder = gallivm->builder;
LLVMValueRef weight2;
weight2 = lp_build_sub(bld, bld->one, weight);
weight = LLVMBuildBitCast(builder, weight,
lp_build_int_vec_type(gallivm, bld->type), "");
weight2 = LLVMBuildBitCast(builder, weight2,
lp_build_int_vec_type(gallivm, bld->type), "");
weight = LLVMBuildAnd(builder, weight, mask1, "");
weight2 = LLVMBuildAnd(builder, weight2, mask0, "");
weight = LLVMBuildBitCast(builder, weight, bld->vec_type, "");
weight2 = LLVMBuildBitCast(builder, weight2, bld->vec_type, "");
return lp_build_add(bld, weight, weight2);
}
/**
* Like a 2d lerp, but inputs are 0/~0 masks, so can simplify slightly.
*/
static LLVMValueRef
lp_build_masklerp2d(struct lp_build_context *bld,
LLVMValueRef weight0,
LLVMValueRef weight1,
LLVMValueRef mask00,
LLVMValueRef mask01,
LLVMValueRef mask10,
LLVMValueRef mask11)
{
LLVMValueRef val0 = lp_build_masklerp(bld, weight0, mask00, mask01);
LLVMValueRef val1 = lp_build_masklerp(bld, weight0, mask10, mask11);
return lp_build_lerp(bld, weight1, val0, val1, 0);
}
/*
* this is a bit excessive code for something OpenGL just recommends
* but does not require.
*/
#define ACCURATE_CUBE_CORNERS 1