Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mesa
Path: blob/21.2-virgl/src/gallium/auxiliary/tgsi/tgsi_build.c
4565 views
1
/**************************************************************************
2
*
3
* Copyright 2007 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 "util/u_debug.h"
29
#include "pipe/p_format.h"
30
#include "pipe/p_shader_tokens.h"
31
#include "tgsi_build.h"
32
#include "tgsi_parse.h"
33
34
35
/*
36
* header
37
*/
38
39
struct tgsi_header
40
tgsi_build_header( void )
41
{
42
struct tgsi_header header;
43
44
header.HeaderSize = 1;
45
header.BodySize = 0;
46
47
return header;
48
}
49
50
static void
51
header_headersize_grow( struct tgsi_header *header )
52
{
53
assert( header->HeaderSize < 0xFF );
54
assert( header->BodySize == 0 );
55
56
header->HeaderSize++;
57
}
58
59
static void
60
header_bodysize_grow( struct tgsi_header *header )
61
{
62
assert( header->BodySize < 0xFFFFFF );
63
64
header->BodySize++;
65
}
66
67
struct tgsi_processor
68
tgsi_build_processor(
69
unsigned type,
70
struct tgsi_header *header )
71
{
72
struct tgsi_processor processor;
73
74
processor.Processor = type;
75
processor.Padding = 0;
76
77
header_headersize_grow( header );
78
79
return processor;
80
}
81
82
/*
83
* declaration
84
*/
85
86
static void
87
declaration_grow(
88
struct tgsi_declaration *declaration,
89
struct tgsi_header *header )
90
{
91
assert( declaration->NrTokens < 0xFF );
92
93
declaration->NrTokens++;
94
95
header_bodysize_grow( header );
96
}
97
98
static struct tgsi_declaration
99
tgsi_default_declaration( void )
100
{
101
struct tgsi_declaration declaration;
102
103
declaration.Type = TGSI_TOKEN_TYPE_DECLARATION;
104
declaration.NrTokens = 1;
105
declaration.File = TGSI_FILE_NULL;
106
declaration.UsageMask = TGSI_WRITEMASK_XYZW;
107
declaration.Interpolate = 0;
108
declaration.Dimension = 0;
109
declaration.Semantic = 0;
110
declaration.Invariant = 0;
111
declaration.Local = 0;
112
declaration.Array = 0;
113
declaration.Atomic = 0;
114
declaration.MemType = TGSI_MEMORY_TYPE_GLOBAL;
115
declaration.Padding = 0;
116
117
return declaration;
118
}
119
120
static struct tgsi_declaration
121
tgsi_build_declaration(
122
unsigned file,
123
unsigned usage_mask,
124
unsigned interpolate,
125
unsigned dimension,
126
unsigned semantic,
127
unsigned invariant,
128
unsigned local,
129
unsigned array,
130
unsigned atomic,
131
unsigned mem_type,
132
struct tgsi_header *header )
133
{
134
struct tgsi_declaration declaration;
135
136
assert( file < TGSI_FILE_COUNT );
137
assert( interpolate < TGSI_INTERPOLATE_COUNT );
138
139
declaration = tgsi_default_declaration();
140
declaration.File = file;
141
declaration.UsageMask = usage_mask;
142
declaration.Interpolate = interpolate;
143
declaration.Dimension = dimension;
144
declaration.Semantic = semantic;
145
declaration.Invariant = invariant;
146
declaration.Local = local;
147
declaration.Array = array;
148
declaration.Atomic = atomic;
149
declaration.MemType = mem_type;
150
header_bodysize_grow( header );
151
152
return declaration;
153
}
154
155
static struct tgsi_declaration_range
156
tgsi_default_declaration_range( void )
157
{
158
struct tgsi_declaration_range dr;
159
160
dr.First = 0;
161
dr.Last = 0;
162
163
return dr;
164
}
165
166
static struct tgsi_declaration_dimension
167
tgsi_default_declaration_dimension()
168
{
169
struct tgsi_declaration_dimension dim;
170
171
dim.Index2D = 0;
172
dim.Padding = 0;
173
174
return dim;
175
}
176
177
static struct tgsi_declaration_range
178
tgsi_build_declaration_range(
179
unsigned first,
180
unsigned last,
181
struct tgsi_declaration *declaration,
182
struct tgsi_header *header )
183
{
184
struct tgsi_declaration_range declaration_range;
185
186
assert( last >= first );
187
assert( last <= 0xFFFF );
188
189
declaration_range.First = first;
190
declaration_range.Last = last;
191
192
declaration_grow( declaration, header );
193
194
return declaration_range;
195
}
196
197
static struct tgsi_declaration_dimension
198
tgsi_build_declaration_dimension(unsigned index_2d,
199
struct tgsi_declaration *declaration,
200
struct tgsi_header *header)
201
{
202
struct tgsi_declaration_dimension dd;
203
204
assert(index_2d <= 0xFFFF);
205
206
dd.Index2D = index_2d;
207
dd.Padding = 0;
208
209
declaration_grow(declaration, header);
210
211
return dd;
212
}
213
214
static struct tgsi_declaration_interp
215
tgsi_default_declaration_interp( void )
216
{
217
struct tgsi_declaration_interp di;
218
219
di.Interpolate = TGSI_INTERPOLATE_CONSTANT;
220
di.Location = TGSI_INTERPOLATE_LOC_CENTER;
221
di.CylindricalWrap = 0;
222
di.Padding = 0;
223
224
return di;
225
}
226
227
static struct tgsi_declaration_interp
228
tgsi_build_declaration_interp(unsigned interpolate,
229
unsigned interpolate_location,
230
unsigned cylindrical_wrap,
231
struct tgsi_declaration *declaration,
232
struct tgsi_header *header)
233
{
234
struct tgsi_declaration_interp di;
235
236
di.Interpolate = interpolate;
237
di.Location = interpolate_location;
238
di.CylindricalWrap = cylindrical_wrap;
239
di.Padding = 0;
240
241
declaration_grow(declaration, header);
242
243
return di;
244
}
245
246
static struct tgsi_declaration_semantic
247
tgsi_default_declaration_semantic( void )
248
{
249
struct tgsi_declaration_semantic ds;
250
251
ds.Name = TGSI_SEMANTIC_POSITION;
252
ds.Index = 0;
253
ds.StreamX = 0;
254
ds.StreamY = 0;
255
ds.StreamZ = 0;
256
ds.StreamW = 0;
257
258
return ds;
259
}
260
261
static struct tgsi_declaration_semantic
262
tgsi_build_declaration_semantic(
263
unsigned semantic_name,
264
unsigned semantic_index,
265
unsigned streamx,
266
unsigned streamy,
267
unsigned streamz,
268
unsigned streamw,
269
struct tgsi_declaration *declaration,
270
struct tgsi_header *header )
271
{
272
struct tgsi_declaration_semantic ds;
273
274
assert( semantic_name <= TGSI_SEMANTIC_COUNT );
275
assert( semantic_index <= 0xFFFF );
276
277
ds.Name = semantic_name;
278
ds.Index = semantic_index;
279
ds.StreamX = streamx;
280
ds.StreamY = streamy;
281
ds.StreamZ = streamz;
282
ds.StreamW = streamw;
283
284
declaration_grow( declaration, header );
285
286
return ds;
287
}
288
289
static struct tgsi_declaration_image
290
tgsi_default_declaration_image(void)
291
{
292
struct tgsi_declaration_image di;
293
294
di.Resource = TGSI_TEXTURE_BUFFER;
295
di.Raw = 0;
296
di.Writable = 0;
297
di.Format = 0;
298
di.Padding = 0;
299
300
return di;
301
}
302
303
static struct tgsi_declaration_image
304
tgsi_build_declaration_image(unsigned texture,
305
unsigned format,
306
unsigned raw,
307
unsigned writable,
308
struct tgsi_declaration *declaration,
309
struct tgsi_header *header)
310
{
311
struct tgsi_declaration_image di;
312
313
di = tgsi_default_declaration_image();
314
di.Resource = texture;
315
di.Format = format;
316
di.Raw = raw;
317
di.Writable = writable;
318
319
declaration_grow(declaration, header);
320
321
return di;
322
}
323
324
static struct tgsi_declaration_sampler_view
325
tgsi_default_declaration_sampler_view(void)
326
{
327
struct tgsi_declaration_sampler_view dsv;
328
329
dsv.Resource = TGSI_TEXTURE_BUFFER;
330
dsv.ReturnTypeX = TGSI_RETURN_TYPE_UNORM;
331
dsv.ReturnTypeY = TGSI_RETURN_TYPE_UNORM;
332
dsv.ReturnTypeZ = TGSI_RETURN_TYPE_UNORM;
333
dsv.ReturnTypeW = TGSI_RETURN_TYPE_UNORM;
334
335
return dsv;
336
}
337
338
static struct tgsi_declaration_sampler_view
339
tgsi_build_declaration_sampler_view(unsigned texture,
340
unsigned return_type_x,
341
unsigned return_type_y,
342
unsigned return_type_z,
343
unsigned return_type_w,
344
struct tgsi_declaration *declaration,
345
struct tgsi_header *header)
346
{
347
struct tgsi_declaration_sampler_view dsv;
348
349
dsv = tgsi_default_declaration_sampler_view();
350
dsv.Resource = texture;
351
dsv.ReturnTypeX = return_type_x;
352
dsv.ReturnTypeY = return_type_y;
353
dsv.ReturnTypeZ = return_type_z;
354
dsv.ReturnTypeW = return_type_w;
355
356
declaration_grow(declaration, header);
357
358
return dsv;
359
}
360
361
362
static struct tgsi_declaration_array
363
tgsi_default_declaration_array( void )
364
{
365
struct tgsi_declaration_array a;
366
367
a.ArrayID = 0;
368
a.Padding = 0;
369
370
return a;
371
}
372
373
static struct tgsi_declaration_array
374
tgsi_build_declaration_array(unsigned arrayid,
375
struct tgsi_declaration *declaration,
376
struct tgsi_header *header)
377
{
378
struct tgsi_declaration_array da;
379
380
da = tgsi_default_declaration_array();
381
da.ArrayID = arrayid;
382
383
declaration_grow(declaration, header);
384
385
return da;
386
}
387
388
struct tgsi_full_declaration
389
tgsi_default_full_declaration( void )
390
{
391
struct tgsi_full_declaration full_declaration;
392
393
full_declaration.Declaration = tgsi_default_declaration();
394
full_declaration.Range = tgsi_default_declaration_range();
395
full_declaration.Dim = tgsi_default_declaration_dimension();
396
full_declaration.Semantic = tgsi_default_declaration_semantic();
397
full_declaration.Interp = tgsi_default_declaration_interp();
398
full_declaration.Image = tgsi_default_declaration_image();
399
full_declaration.SamplerView = tgsi_default_declaration_sampler_view();
400
full_declaration.Array = tgsi_default_declaration_array();
401
402
return full_declaration;
403
}
404
405
unsigned
406
tgsi_build_full_declaration(
407
const struct tgsi_full_declaration *full_decl,
408
struct tgsi_token *tokens,
409
struct tgsi_header *header,
410
unsigned maxsize )
411
{
412
unsigned size = 0;
413
struct tgsi_declaration *declaration;
414
struct tgsi_declaration_range *dr;
415
416
if( maxsize <= size )
417
return 0;
418
declaration = (struct tgsi_declaration *) &tokens[size];
419
size++;
420
421
*declaration = tgsi_build_declaration(
422
full_decl->Declaration.File,
423
full_decl->Declaration.UsageMask,
424
full_decl->Declaration.Interpolate,
425
full_decl->Declaration.Dimension,
426
full_decl->Declaration.Semantic,
427
full_decl->Declaration.Invariant,
428
full_decl->Declaration.Local,
429
full_decl->Declaration.Array,
430
full_decl->Declaration.Atomic,
431
full_decl->Declaration.MemType,
432
header );
433
434
if (maxsize <= size)
435
return 0;
436
dr = (struct tgsi_declaration_range *) &tokens[size];
437
size++;
438
439
*dr = tgsi_build_declaration_range(
440
full_decl->Range.First,
441
full_decl->Range.Last,
442
declaration,
443
header );
444
445
if (full_decl->Declaration.Dimension) {
446
struct tgsi_declaration_dimension *dd;
447
448
if (maxsize <= size) {
449
return 0;
450
}
451
dd = (struct tgsi_declaration_dimension *)&tokens[size];
452
size++;
453
454
*dd = tgsi_build_declaration_dimension(full_decl->Dim.Index2D,
455
declaration,
456
header);
457
}
458
459
if (full_decl->Declaration.Interpolate) {
460
struct tgsi_declaration_interp *di;
461
462
if (maxsize <= size) {
463
return 0;
464
}
465
di = (struct tgsi_declaration_interp *)&tokens[size];
466
size++;
467
468
*di = tgsi_build_declaration_interp(full_decl->Interp.Interpolate,
469
full_decl->Interp.Location,
470
full_decl->Interp.CylindricalWrap,
471
declaration,
472
header);
473
}
474
475
if( full_decl->Declaration.Semantic ) {
476
struct tgsi_declaration_semantic *ds;
477
478
if( maxsize <= size )
479
return 0;
480
ds = (struct tgsi_declaration_semantic *) &tokens[size];
481
size++;
482
483
*ds = tgsi_build_declaration_semantic(
484
full_decl->Semantic.Name,
485
full_decl->Semantic.Index,
486
full_decl->Semantic.StreamX,
487
full_decl->Semantic.StreamY,
488
full_decl->Semantic.StreamZ,
489
full_decl->Semantic.StreamW,
490
declaration,
491
header );
492
}
493
494
if (full_decl->Declaration.File == TGSI_FILE_IMAGE) {
495
struct tgsi_declaration_image *di;
496
497
if (maxsize <= size) {
498
return 0;
499
}
500
di = (struct tgsi_declaration_image *)&tokens[size];
501
size++;
502
503
*di = tgsi_build_declaration_image(full_decl->Image.Resource,
504
full_decl->Image.Format,
505
full_decl->Image.Raw,
506
full_decl->Image.Writable,
507
declaration,
508
header);
509
}
510
511
if (full_decl->Declaration.File == TGSI_FILE_SAMPLER_VIEW) {
512
struct tgsi_declaration_sampler_view *dsv;
513
514
if (maxsize <= size) {
515
return 0;
516
}
517
dsv = (struct tgsi_declaration_sampler_view *)&tokens[size];
518
size++;
519
520
*dsv = tgsi_build_declaration_sampler_view(
521
full_decl->SamplerView.Resource,
522
full_decl->SamplerView.ReturnTypeX,
523
full_decl->SamplerView.ReturnTypeY,
524
full_decl->SamplerView.ReturnTypeZ,
525
full_decl->SamplerView.ReturnTypeW,
526
declaration,
527
header);
528
}
529
530
if (full_decl->Declaration.Array) {
531
struct tgsi_declaration_array *da;
532
533
if (maxsize <= size) {
534
return 0;
535
}
536
da = (struct tgsi_declaration_array *)&tokens[size];
537
size++;
538
*da = tgsi_build_declaration_array(
539
full_decl->Array.ArrayID,
540
declaration,
541
header);
542
}
543
return size;
544
}
545
546
/*
547
* immediate
548
*/
549
550
static struct tgsi_immediate
551
tgsi_default_immediate( void )
552
{
553
struct tgsi_immediate immediate;
554
555
immediate.Type = TGSI_TOKEN_TYPE_IMMEDIATE;
556
immediate.NrTokens = 1;
557
immediate.DataType = TGSI_IMM_FLOAT32;
558
immediate.Padding = 0;
559
560
return immediate;
561
}
562
563
static struct tgsi_immediate
564
tgsi_build_immediate(
565
struct tgsi_header *header,
566
unsigned type )
567
{
568
struct tgsi_immediate immediate;
569
570
immediate = tgsi_default_immediate();
571
immediate.DataType = type;
572
573
header_bodysize_grow( header );
574
575
return immediate;
576
}
577
578
struct tgsi_full_immediate
579
tgsi_default_full_immediate( void )
580
{
581
struct tgsi_full_immediate fullimm;
582
583
fullimm.Immediate = tgsi_default_immediate();
584
fullimm.u[0].Float = 0.0f;
585
fullimm.u[1].Float = 0.0f;
586
fullimm.u[2].Float = 0.0f;
587
fullimm.u[3].Float = 0.0f;
588
589
return fullimm;
590
}
591
592
static void
593
immediate_grow(
594
struct tgsi_immediate *immediate,
595
struct tgsi_header *header )
596
{
597
assert( immediate->NrTokens < 0xFF );
598
599
immediate->NrTokens++;
600
601
header_bodysize_grow( header );
602
}
603
604
unsigned
605
tgsi_build_full_immediate(
606
const struct tgsi_full_immediate *full_imm,
607
struct tgsi_token *tokens,
608
struct tgsi_header *header,
609
unsigned maxsize )
610
{
611
unsigned size = 0;
612
int i;
613
struct tgsi_immediate *immediate;
614
615
if( maxsize <= size )
616
return 0;
617
immediate = (struct tgsi_immediate *) &tokens[size];
618
size++;
619
620
*immediate = tgsi_build_immediate( header, full_imm->Immediate.DataType );
621
622
assert( full_imm->Immediate.NrTokens <= 4 + 1 );
623
624
for( i = 0; i < full_imm->Immediate.NrTokens - 1; i++ ) {
625
union tgsi_immediate_data *data;
626
627
if( maxsize <= size )
628
return 0;
629
630
data = (union tgsi_immediate_data *) &tokens[size];
631
*data = full_imm->u[i];
632
633
immediate_grow( immediate, header );
634
size++;
635
}
636
637
return size;
638
}
639
640
/*
641
* instruction
642
*/
643
644
struct tgsi_instruction
645
tgsi_default_instruction( void )
646
{
647
struct tgsi_instruction instruction;
648
649
instruction.Type = TGSI_TOKEN_TYPE_INSTRUCTION;
650
instruction.NrTokens = 0;
651
instruction.Opcode = TGSI_OPCODE_MOV;
652
instruction.Saturate = 0;
653
instruction.NumDstRegs = 1;
654
instruction.NumSrcRegs = 1;
655
instruction.Label = 0;
656
instruction.Texture = 0;
657
instruction.Memory = 0;
658
instruction.Precise = 0;
659
instruction.Padding = 0;
660
661
return instruction;
662
}
663
664
static struct tgsi_instruction
665
tgsi_build_instruction(enum tgsi_opcode opcode,
666
unsigned saturate,
667
unsigned precise,
668
unsigned num_dst_regs,
669
unsigned num_src_regs,
670
struct tgsi_header *header)
671
{
672
struct tgsi_instruction instruction;
673
674
assert (opcode <= TGSI_OPCODE_LAST);
675
assert (saturate <= 1);
676
assert (num_dst_regs <= 3);
677
assert (num_src_regs <= 15);
678
679
instruction = tgsi_default_instruction();
680
instruction.Opcode = opcode;
681
instruction.Saturate = saturate;
682
instruction.Precise = precise;
683
instruction.NumDstRegs = num_dst_regs;
684
instruction.NumSrcRegs = num_src_regs;
685
686
header_bodysize_grow( header );
687
688
return instruction;
689
}
690
691
static void
692
instruction_grow(
693
struct tgsi_instruction *instruction,
694
struct tgsi_header *header )
695
{
696
assert (instruction->NrTokens < 0xFF);
697
698
instruction->NrTokens++;
699
700
header_bodysize_grow( header );
701
}
702
703
static struct tgsi_instruction_label
704
tgsi_default_instruction_label( void )
705
{
706
struct tgsi_instruction_label instruction_label;
707
708
instruction_label.Label = 0;
709
instruction_label.Padding = 0;
710
711
return instruction_label;
712
}
713
714
static struct tgsi_instruction_label
715
tgsi_build_instruction_label(
716
unsigned label,
717
struct tgsi_instruction *instruction,
718
struct tgsi_header *header )
719
{
720
struct tgsi_instruction_label instruction_label;
721
722
instruction_label.Label = label;
723
instruction_label.Padding = 0;
724
instruction->Label = 1;
725
726
instruction_grow( instruction, header );
727
728
return instruction_label;
729
}
730
731
static struct tgsi_instruction_texture
732
tgsi_default_instruction_texture( void )
733
{
734
struct tgsi_instruction_texture instruction_texture;
735
736
instruction_texture.Texture = TGSI_TEXTURE_UNKNOWN;
737
instruction_texture.NumOffsets = 0;
738
instruction_texture.ReturnType = TGSI_RETURN_TYPE_UNKNOWN;
739
instruction_texture.Padding = 0;
740
741
return instruction_texture;
742
}
743
744
static struct tgsi_instruction_texture
745
tgsi_build_instruction_texture(
746
unsigned texture,
747
unsigned num_offsets,
748
unsigned return_type,
749
struct tgsi_instruction *instruction,
750
struct tgsi_header *header )
751
{
752
struct tgsi_instruction_texture instruction_texture;
753
754
instruction_texture.Texture = texture;
755
instruction_texture.NumOffsets = num_offsets;
756
instruction_texture.ReturnType = return_type;
757
instruction_texture.Padding = 0;
758
instruction->Texture = 1;
759
760
instruction_grow( instruction, header );
761
762
return instruction_texture;
763
}
764
765
static struct tgsi_instruction_memory
766
tgsi_default_instruction_memory( void )
767
{
768
struct tgsi_instruction_memory instruction_memory;
769
770
instruction_memory.Qualifier = 0;
771
instruction_memory.Texture = 0;
772
instruction_memory.Format = 0;
773
instruction_memory.Padding = 0;
774
775
return instruction_memory;
776
}
777
778
static struct tgsi_instruction_memory
779
tgsi_build_instruction_memory(
780
unsigned qualifier,
781
unsigned texture,
782
unsigned format,
783
struct tgsi_instruction *instruction,
784
struct tgsi_header *header )
785
{
786
struct tgsi_instruction_memory instruction_memory;
787
788
instruction_memory.Qualifier = qualifier;
789
instruction_memory.Texture = texture;
790
instruction_memory.Format = format;
791
instruction_memory.Padding = 0;
792
instruction->Memory = 1;
793
794
instruction_grow( instruction, header );
795
796
return instruction_memory;
797
}
798
799
static struct tgsi_texture_offset
800
tgsi_default_texture_offset( void )
801
{
802
struct tgsi_texture_offset texture_offset;
803
804
texture_offset.Index = 0;
805
texture_offset.File = 0;
806
texture_offset.SwizzleX = 0;
807
texture_offset.SwizzleY = 0;
808
texture_offset.SwizzleZ = 0;
809
texture_offset.Padding = 0;
810
811
return texture_offset;
812
}
813
814
static struct tgsi_texture_offset
815
tgsi_build_texture_offset(
816
int index, int file, int swizzle_x, int swizzle_y, int swizzle_z,
817
struct tgsi_instruction *instruction,
818
struct tgsi_header *header )
819
{
820
struct tgsi_texture_offset texture_offset;
821
822
texture_offset.Index = index;
823
texture_offset.File = file;
824
texture_offset.SwizzleX = swizzle_x;
825
texture_offset.SwizzleY = swizzle_y;
826
texture_offset.SwizzleZ = swizzle_z;
827
texture_offset.Padding = 0;
828
829
instruction_grow( instruction, header );
830
831
return texture_offset;
832
}
833
834
static struct tgsi_src_register
835
tgsi_default_src_register( void )
836
{
837
struct tgsi_src_register src_register;
838
839
src_register.File = TGSI_FILE_NULL;
840
src_register.SwizzleX = TGSI_SWIZZLE_X;
841
src_register.SwizzleY = TGSI_SWIZZLE_Y;
842
src_register.SwizzleZ = TGSI_SWIZZLE_Z;
843
src_register.SwizzleW = TGSI_SWIZZLE_W;
844
src_register.Negate = 0;
845
src_register.Absolute = 0;
846
src_register.Indirect = 0;
847
src_register.Dimension = 0;
848
src_register.Index = 0;
849
850
return src_register;
851
}
852
853
static struct tgsi_src_register
854
tgsi_build_src_register(
855
unsigned file,
856
unsigned swizzle_x,
857
unsigned swizzle_y,
858
unsigned swizzle_z,
859
unsigned swizzle_w,
860
unsigned negate,
861
unsigned absolute,
862
unsigned indirect,
863
unsigned dimension,
864
int index,
865
struct tgsi_instruction *instruction,
866
struct tgsi_header *header )
867
{
868
struct tgsi_src_register src_register;
869
870
assert( file < TGSI_FILE_COUNT );
871
assert( swizzle_x <= TGSI_SWIZZLE_W );
872
assert( swizzle_y <= TGSI_SWIZZLE_W );
873
assert( swizzle_z <= TGSI_SWIZZLE_W );
874
assert( swizzle_w <= TGSI_SWIZZLE_W );
875
assert( negate <= 1 );
876
assert( index >= -0x8000 && index <= 0x7FFF );
877
878
src_register.File = file;
879
src_register.SwizzleX = swizzle_x;
880
src_register.SwizzleY = swizzle_y;
881
src_register.SwizzleZ = swizzle_z;
882
src_register.SwizzleW = swizzle_w;
883
src_register.Negate = negate;
884
src_register.Absolute = absolute;
885
src_register.Indirect = indirect;
886
src_register.Dimension = dimension;
887
src_register.Index = index;
888
889
instruction_grow( instruction, header );
890
891
return src_register;
892
}
893
894
static struct tgsi_ind_register
895
tgsi_default_ind_register( void )
896
{
897
struct tgsi_ind_register ind_register;
898
899
ind_register.File = TGSI_FILE_NULL;
900
ind_register.Index = 0;
901
ind_register.Swizzle = TGSI_SWIZZLE_X;
902
ind_register.ArrayID = 0;
903
904
return ind_register;
905
}
906
907
static struct tgsi_ind_register
908
tgsi_build_ind_register(
909
unsigned file,
910
unsigned swizzle,
911
int index,
912
unsigned arrayid,
913
struct tgsi_instruction *instruction,
914
struct tgsi_header *header )
915
{
916
struct tgsi_ind_register ind_register;
917
918
assert( file < TGSI_FILE_COUNT );
919
assert( swizzle <= TGSI_SWIZZLE_W );
920
assert( index >= -0x8000 && index <= 0x7FFF );
921
922
ind_register.File = file;
923
ind_register.Swizzle = swizzle;
924
ind_register.Index = index;
925
ind_register.ArrayID = arrayid;
926
927
instruction_grow( instruction, header );
928
929
return ind_register;
930
}
931
932
static struct tgsi_dimension
933
tgsi_default_dimension( void )
934
{
935
struct tgsi_dimension dimension;
936
937
dimension.Indirect = 0;
938
dimension.Dimension = 0;
939
dimension.Padding = 0;
940
dimension.Index = 0;
941
942
return dimension;
943
}
944
945
static struct tgsi_full_src_register
946
tgsi_default_full_src_register( void )
947
{
948
struct tgsi_full_src_register full_src_register;
949
950
full_src_register.Register = tgsi_default_src_register();
951
full_src_register.Indirect = tgsi_default_ind_register();
952
full_src_register.Dimension = tgsi_default_dimension();
953
full_src_register.DimIndirect = tgsi_default_ind_register();
954
955
return full_src_register;
956
}
957
958
static struct tgsi_dimension
959
tgsi_build_dimension(
960
unsigned indirect,
961
unsigned index,
962
struct tgsi_instruction *instruction,
963
struct tgsi_header *header )
964
{
965
struct tgsi_dimension dimension;
966
967
dimension.Indirect = indirect;
968
dimension.Dimension = 0;
969
dimension.Padding = 0;
970
dimension.Index = index;
971
972
instruction_grow( instruction, header );
973
974
return dimension;
975
}
976
977
static struct tgsi_dst_register
978
tgsi_default_dst_register( void )
979
{
980
struct tgsi_dst_register dst_register;
981
982
dst_register.File = TGSI_FILE_NULL;
983
dst_register.WriteMask = TGSI_WRITEMASK_XYZW;
984
dst_register.Indirect = 0;
985
dst_register.Dimension = 0;
986
dst_register.Index = 0;
987
dst_register.Padding = 0;
988
989
return dst_register;
990
}
991
992
static struct tgsi_dst_register
993
tgsi_build_dst_register(
994
unsigned file,
995
unsigned mask,
996
unsigned indirect,
997
unsigned dimension,
998
int index,
999
struct tgsi_instruction *instruction,
1000
struct tgsi_header *header )
1001
{
1002
struct tgsi_dst_register dst_register;
1003
1004
assert( file < TGSI_FILE_COUNT );
1005
assert( mask <= TGSI_WRITEMASK_XYZW );
1006
assert( index >= -32768 && index <= 32767 );
1007
1008
dst_register.File = file;
1009
dst_register.WriteMask = mask;
1010
dst_register.Indirect = indirect;
1011
dst_register.Dimension = dimension;
1012
dst_register.Index = index;
1013
dst_register.Padding = 0;
1014
1015
instruction_grow( instruction, header );
1016
1017
return dst_register;
1018
}
1019
1020
static struct tgsi_full_dst_register
1021
tgsi_default_full_dst_register( void )
1022
{
1023
struct tgsi_full_dst_register full_dst_register;
1024
1025
full_dst_register.Register = tgsi_default_dst_register();
1026
full_dst_register.Indirect = tgsi_default_ind_register();
1027
full_dst_register.Dimension = tgsi_default_dimension();
1028
full_dst_register.DimIndirect = tgsi_default_ind_register();
1029
1030
return full_dst_register;
1031
}
1032
1033
struct tgsi_full_instruction
1034
tgsi_default_full_instruction( void )
1035
{
1036
struct tgsi_full_instruction full_instruction;
1037
unsigned i;
1038
1039
full_instruction.Instruction = tgsi_default_instruction();
1040
full_instruction.Label = tgsi_default_instruction_label();
1041
full_instruction.Texture = tgsi_default_instruction_texture();
1042
full_instruction.Memory = tgsi_default_instruction_memory();
1043
for( i = 0; i < TGSI_FULL_MAX_TEX_OFFSETS; i++ ) {
1044
full_instruction.TexOffsets[i] = tgsi_default_texture_offset();
1045
}
1046
for( i = 0; i < TGSI_FULL_MAX_DST_REGISTERS; i++ ) {
1047
full_instruction.Dst[i] = tgsi_default_full_dst_register();
1048
}
1049
for( i = 0; i < TGSI_FULL_MAX_SRC_REGISTERS; i++ ) {
1050
full_instruction.Src[i] = tgsi_default_full_src_register();
1051
}
1052
1053
return full_instruction;
1054
}
1055
1056
unsigned
1057
tgsi_build_full_instruction(
1058
const struct tgsi_full_instruction *full_inst,
1059
struct tgsi_token *tokens,
1060
struct tgsi_header *header,
1061
unsigned maxsize )
1062
{
1063
unsigned size = 0;
1064
unsigned i;
1065
struct tgsi_instruction *instruction;
1066
1067
if( maxsize <= size )
1068
return 0;
1069
instruction = (struct tgsi_instruction *) &tokens[size];
1070
size++;
1071
1072
*instruction = tgsi_build_instruction(full_inst->Instruction.Opcode,
1073
full_inst->Instruction.Saturate,
1074
full_inst->Instruction.Precise,
1075
full_inst->Instruction.NumDstRegs,
1076
full_inst->Instruction.NumSrcRegs,
1077
header);
1078
1079
if (full_inst->Instruction.Label) {
1080
struct tgsi_instruction_label *instruction_label;
1081
1082
if( maxsize <= size )
1083
return 0;
1084
instruction_label =
1085
(struct tgsi_instruction_label *) &tokens[size];
1086
size++;
1087
1088
*instruction_label = tgsi_build_instruction_label(
1089
full_inst->Label.Label,
1090
instruction,
1091
header );
1092
}
1093
1094
if (full_inst->Instruction.Texture) {
1095
struct tgsi_instruction_texture *instruction_texture;
1096
1097
if( maxsize <= size )
1098
return 0;
1099
instruction_texture =
1100
(struct tgsi_instruction_texture *) &tokens[size];
1101
size++;
1102
1103
*instruction_texture = tgsi_build_instruction_texture(
1104
full_inst->Texture.Texture,
1105
full_inst->Texture.NumOffsets,
1106
full_inst->Texture.ReturnType,
1107
instruction,
1108
header );
1109
1110
for (i = 0; i < full_inst->Texture.NumOffsets; i++) {
1111
struct tgsi_texture_offset *texture_offset;
1112
1113
if ( maxsize <= size )
1114
return 0;
1115
texture_offset = (struct tgsi_texture_offset *)&tokens[size];
1116
size++;
1117
*texture_offset = tgsi_build_texture_offset(
1118
full_inst->TexOffsets[i].Index,
1119
full_inst->TexOffsets[i].File,
1120
full_inst->TexOffsets[i].SwizzleX,
1121
full_inst->TexOffsets[i].SwizzleY,
1122
full_inst->TexOffsets[i].SwizzleZ,
1123
instruction,
1124
header);
1125
}
1126
}
1127
1128
if (full_inst->Instruction.Memory) {
1129
struct tgsi_instruction_memory *instruction_memory;
1130
1131
if( maxsize <= size )
1132
return 0;
1133
instruction_memory =
1134
(struct tgsi_instruction_memory *) &tokens[size];
1135
size++;
1136
1137
*instruction_memory = tgsi_build_instruction_memory(
1138
full_inst->Memory.Qualifier,
1139
full_inst->Memory.Texture,
1140
full_inst->Memory.Format,
1141
instruction,
1142
header );
1143
}
1144
1145
for( i = 0; i < full_inst->Instruction.NumDstRegs; i++ ) {
1146
const struct tgsi_full_dst_register *reg = &full_inst->Dst[i];
1147
struct tgsi_dst_register *dst_register;
1148
1149
if( maxsize <= size )
1150
return 0;
1151
dst_register = (struct tgsi_dst_register *) &tokens[size];
1152
size++;
1153
1154
*dst_register = tgsi_build_dst_register(
1155
reg->Register.File,
1156
reg->Register.WriteMask,
1157
reg->Register.Indirect,
1158
reg->Register.Dimension,
1159
reg->Register.Index,
1160
instruction,
1161
header );
1162
1163
if( reg->Register.Indirect ) {
1164
struct tgsi_ind_register *ind;
1165
1166
if( maxsize <= size )
1167
return 0;
1168
ind = (struct tgsi_ind_register *) &tokens[size];
1169
size++;
1170
1171
*ind = tgsi_build_ind_register(
1172
reg->Indirect.File,
1173
reg->Indirect.Swizzle,
1174
reg->Indirect.Index,
1175
reg->Indirect.ArrayID,
1176
instruction,
1177
header );
1178
}
1179
1180
if( reg->Register.Dimension ) {
1181
struct tgsi_dimension *dim;
1182
1183
assert( !reg->Dimension.Dimension );
1184
1185
if( maxsize <= size )
1186
return 0;
1187
dim = (struct tgsi_dimension *) &tokens[size];
1188
size++;
1189
1190
*dim = tgsi_build_dimension(
1191
reg->Dimension.Indirect,
1192
reg->Dimension.Index,
1193
instruction,
1194
header );
1195
1196
if( reg->Dimension.Indirect ) {
1197
struct tgsi_ind_register *ind;
1198
1199
if( maxsize <= size )
1200
return 0;
1201
ind = (struct tgsi_ind_register *) &tokens[size];
1202
size++;
1203
1204
*ind = tgsi_build_ind_register(
1205
reg->DimIndirect.File,
1206
reg->DimIndirect.Swizzle,
1207
reg->DimIndirect.Index,
1208
reg->DimIndirect.ArrayID,
1209
instruction,
1210
header );
1211
}
1212
}
1213
}
1214
1215
for( i = 0; i < full_inst->Instruction.NumSrcRegs; i++ ) {
1216
const struct tgsi_full_src_register *reg = &full_inst->Src[i];
1217
struct tgsi_src_register *src_register;
1218
1219
if( maxsize <= size )
1220
return 0;
1221
src_register = (struct tgsi_src_register *) &tokens[size];
1222
size++;
1223
1224
*src_register = tgsi_build_src_register(
1225
reg->Register.File,
1226
reg->Register.SwizzleX,
1227
reg->Register.SwizzleY,
1228
reg->Register.SwizzleZ,
1229
reg->Register.SwizzleW,
1230
reg->Register.Negate,
1231
reg->Register.Absolute,
1232
reg->Register.Indirect,
1233
reg->Register.Dimension,
1234
reg->Register.Index,
1235
instruction,
1236
header );
1237
1238
if( reg->Register.Indirect ) {
1239
struct tgsi_ind_register *ind;
1240
1241
if( maxsize <= size )
1242
return 0;
1243
ind = (struct tgsi_ind_register *) &tokens[size];
1244
size++;
1245
1246
*ind = tgsi_build_ind_register(
1247
reg->Indirect.File,
1248
reg->Indirect.Swizzle,
1249
reg->Indirect.Index,
1250
reg->Indirect.ArrayID,
1251
instruction,
1252
header );
1253
}
1254
1255
if( reg->Register.Dimension ) {
1256
struct tgsi_dimension *dim;
1257
1258
assert( !reg->Dimension.Dimension );
1259
1260
if( maxsize <= size )
1261
return 0;
1262
dim = (struct tgsi_dimension *) &tokens[size];
1263
size++;
1264
1265
*dim = tgsi_build_dimension(
1266
reg->Dimension.Indirect,
1267
reg->Dimension.Index,
1268
instruction,
1269
header );
1270
1271
if( reg->Dimension.Indirect ) {
1272
struct tgsi_ind_register *ind;
1273
1274
if( maxsize <= size )
1275
return 0;
1276
ind = (struct tgsi_ind_register *) &tokens[size];
1277
size++;
1278
1279
*ind = tgsi_build_ind_register(
1280
reg->DimIndirect.File,
1281
reg->DimIndirect.Swizzle,
1282
reg->DimIndirect.Index,
1283
reg->DimIndirect.ArrayID,
1284
instruction,
1285
header );
1286
}
1287
}
1288
}
1289
1290
return size;
1291
}
1292
1293
static struct tgsi_property
1294
tgsi_default_property( void )
1295
{
1296
struct tgsi_property property;
1297
1298
property.Type = TGSI_TOKEN_TYPE_PROPERTY;
1299
property.NrTokens = 1;
1300
property.PropertyName = TGSI_PROPERTY_GS_INPUT_PRIM;
1301
property.Padding = 0;
1302
1303
return property;
1304
}
1305
1306
static struct tgsi_property
1307
tgsi_build_property(unsigned property_name,
1308
struct tgsi_header *header)
1309
{
1310
struct tgsi_property property;
1311
1312
property = tgsi_default_property();
1313
property.PropertyName = property_name;
1314
1315
header_bodysize_grow( header );
1316
1317
return property;
1318
}
1319
1320
1321
struct tgsi_full_property
1322
tgsi_default_full_property( void )
1323
{
1324
struct tgsi_full_property full_property;
1325
1326
full_property.Property = tgsi_default_property();
1327
memset(full_property.u, 0,
1328
sizeof(struct tgsi_property_data) * 8);
1329
1330
return full_property;
1331
}
1332
1333
static void
1334
property_grow(
1335
struct tgsi_property *property,
1336
struct tgsi_header *header )
1337
{
1338
assert( property->NrTokens < 0xFF );
1339
1340
property->NrTokens++;
1341
1342
header_bodysize_grow( header );
1343
}
1344
1345
static struct tgsi_property_data
1346
tgsi_build_property_data(
1347
unsigned value,
1348
struct tgsi_property *property,
1349
struct tgsi_header *header )
1350
{
1351
struct tgsi_property_data property_data;
1352
1353
property_data.Data = value;
1354
1355
property_grow( property, header );
1356
1357
return property_data;
1358
}
1359
1360
unsigned
1361
tgsi_build_full_property(
1362
const struct tgsi_full_property *full_prop,
1363
struct tgsi_token *tokens,
1364
struct tgsi_header *header,
1365
unsigned maxsize )
1366
{
1367
unsigned size = 0;
1368
int i;
1369
struct tgsi_property *property;
1370
1371
if( maxsize <= size )
1372
return 0;
1373
property = (struct tgsi_property *) &tokens[size];
1374
size++;
1375
1376
*property = tgsi_build_property(
1377
full_prop->Property.PropertyName,
1378
header );
1379
1380
assert( full_prop->Property.NrTokens <= 8 + 1 );
1381
1382
for( i = 0; i < full_prop->Property.NrTokens - 1; i++ ) {
1383
struct tgsi_property_data *data;
1384
1385
if( maxsize <= size )
1386
return 0;
1387
data = (struct tgsi_property_data *) &tokens[size];
1388
size++;
1389
1390
*data = tgsi_build_property_data(
1391
full_prop->u[i].Data,
1392
property,
1393
header );
1394
}
1395
1396
return size;
1397
}
1398
1399
struct tgsi_full_src_register
1400
tgsi_full_src_register_from_dst(const struct tgsi_full_dst_register *dst)
1401
{
1402
struct tgsi_full_src_register src;
1403
src.Register = tgsi_default_src_register();
1404
src.Register.File = dst->Register.File;
1405
src.Register.Indirect = dst->Register.Indirect;
1406
src.Register.Dimension = dst->Register.Dimension;
1407
src.Register.Index = dst->Register.Index;
1408
src.Indirect = dst->Indirect;
1409
src.Dimension = dst->Dimension;
1410
src.DimIndirect = dst->DimIndirect;
1411
return src;
1412
}
1413
1414