summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/svga/svga_format.c
blob: 8c1b161e6faa91f65d0f8a5f354f4829eaa5e6f7 (plain)
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
/**********************************************************
 * Copyright 2011 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, sublicense, 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 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
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * 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.
 *
 **********************************************************/


#include "pipe/p_format.h"
#include "util/u_debug.h"
#include "util/u_memory.h"

#include "svga_winsys.h"
#include "svga_screen.h"
#include "svga_format.h"


/*
 * Translate from gallium format to SVGA3D format.
 */
SVGA3dSurfaceFormat
svga_translate_format(struct svga_screen *ss,
                      enum pipe_format format,
                      unsigned bind)
{
   switch(format) {

   case PIPE_FORMAT_B8G8R8A8_UNORM:
      return SVGA3D_A8R8G8B8;
   case PIPE_FORMAT_B8G8R8X8_UNORM:
      return SVGA3D_X8R8G8B8;

   /* sRGB required for GL2.1 */
   case PIPE_FORMAT_B8G8R8A8_SRGB:
      return SVGA3D_A8R8G8B8;
   case PIPE_FORMAT_DXT1_SRGB:
   case PIPE_FORMAT_DXT1_SRGBA:
      return SVGA3D_DXT1;
   case PIPE_FORMAT_DXT3_SRGBA:
      return SVGA3D_DXT3;
   case PIPE_FORMAT_DXT5_SRGBA:
      return SVGA3D_DXT5;

   case PIPE_FORMAT_B5G6R5_UNORM:
      return SVGA3D_R5G6B5;
   case PIPE_FORMAT_B5G5R5A1_UNORM:
      return SVGA3D_A1R5G5B5;
   case PIPE_FORMAT_B4G4R4A4_UNORM:
      return SVGA3D_A4R4G4B4;

   case PIPE_FORMAT_R16G16B16A16_UNORM:
      return SVGA3D_A16B16G16R16;

   case PIPE_FORMAT_Z16_UNORM:
      return bind & PIPE_BIND_SAMPLER_VIEW ? ss->depth.z16 : SVGA3D_Z_D16;
   case PIPE_FORMAT_S8_UINT_Z24_UNORM:
      return bind & PIPE_BIND_SAMPLER_VIEW ? ss->depth.s8z24 : SVGA3D_Z_D24S8;
   case PIPE_FORMAT_X8Z24_UNORM:
      return bind & PIPE_BIND_SAMPLER_VIEW ? ss->depth.x8z24 : SVGA3D_Z_D24X8;

   case PIPE_FORMAT_A8_UNORM:
      return SVGA3D_ALPHA8;
   case PIPE_FORMAT_L8_UNORM:
      return SVGA3D_LUMINANCE8;

   case PIPE_FORMAT_DXT1_RGB:
   case PIPE_FORMAT_DXT1_RGBA:
      return SVGA3D_DXT1;
   case PIPE_FORMAT_DXT3_RGBA:
      return SVGA3D_DXT3;
   case PIPE_FORMAT_DXT5_RGBA:
      return SVGA3D_DXT5;

   /* Float formats (only 1, 2 and 4-component formats supported) */
   case PIPE_FORMAT_R32_FLOAT:
      return SVGA3D_R_S23E8;
   case PIPE_FORMAT_R32G32_FLOAT:
      return SVGA3D_RG_S23E8;
   case PIPE_FORMAT_R32G32B32A32_FLOAT:
      return SVGA3D_ARGB_S23E8;
   case PIPE_FORMAT_R16_FLOAT:
      return SVGA3D_R_S10E5;
   case PIPE_FORMAT_R16G16_FLOAT:
      return SVGA3D_RG_S10E5;
   case PIPE_FORMAT_R16G16B16A16_FLOAT:
      return SVGA3D_ARGB_S10E5;

   case PIPE_FORMAT_Z32_UNORM:
      /* SVGA3D_Z_D32 is not yet unsupported */
      /* fall-through */
   default:
      return SVGA3D_FORMAT_INVALID;
   }
}


/*
 * Format capability description entry.
 */
struct format_cap {
   SVGA3dSurfaceFormat format;

   /*
    * Capability index corresponding to the format.
    */
   SVGA3dDevCapIndex index;

   /*
    * Mask of supported SVGA3dFormatOp operations, to be inferred when the
    * capability is not explicitly present.
    */
   uint32 defaultOperations;
};


/*
 * Format capability description table.
 *
 * Ordererd by increasing SVGA3dSurfaceFormat value, but with gaps.
 */
static const struct format_cap format_cap_table[] = {
   {
      SVGA3D_X8R8G8B8,
      SVGA3D_DEVCAP_SURFACEFMT_X8R8G8B8,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_VOLUMETEXTURE |
      SVGA3DFORMAT_OP_DISPLAYMODE |
      SVGA3DFORMAT_OP_3DACCELERATION |
      SVGA3DFORMAT_OP_CONVERT_TO_ARGB |
      SVGA3DFORMAT_OP_MEMBEROFGROUP_ARGB |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN |
      SVGA3DFORMAT_OP_SRGBREAD |
      SVGA3DFORMAT_OP_SRGBWRITE |
      SVGA3DFORMAT_OP_SAME_FORMAT_RENDERTARGET |
      SVGA3DFORMAT_OP_OFFSCREEN_RENDERTARGET
   },
   {
      SVGA3D_A8R8G8B8,
      SVGA3D_DEVCAP_SURFACEFMT_A8R8G8B8,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_VOLUMETEXTURE |
      SVGA3DFORMAT_OP_CONVERT_TO_ARGB |
      SVGA3DFORMAT_OP_MEMBEROFGROUP_ARGB |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN |
      SVGA3DFORMAT_OP_SRGBREAD |
      SVGA3DFORMAT_OP_SRGBWRITE |
      SVGA3DFORMAT_OP_SAME_FORMAT_RENDERTARGET |
      SVGA3DFORMAT_OP_SAME_FORMAT_UP_TO_ALPHA_RENDERTARGET |
      SVGA3DFORMAT_OP_OFFSCREEN_RENDERTARGET
   },
   {
      SVGA3D_R5G6B5,
      SVGA3D_DEVCAP_SURFACEFMT_R5G6B5,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_VOLUMETEXTURE |
      SVGA3DFORMAT_OP_DISPLAYMODE |
      SVGA3DFORMAT_OP_3DACCELERATION |
      SVGA3DFORMAT_OP_CONVERT_TO_ARGB |
      SVGA3DFORMAT_OP_MEMBEROFGROUP_ARGB |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN |
      SVGA3DFORMAT_OP_SRGBREAD |
      SVGA3DFORMAT_OP_SRGBWRITE |
      SVGA3DFORMAT_OP_SAME_FORMAT_RENDERTARGET |
      SVGA3DFORMAT_OP_OFFSCREEN_RENDERTARGET
   },
   {
      SVGA3D_X1R5G5B5,
      SVGA3D_DEVCAP_SURFACEFMT_X1R5G5B5,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_VOLUMETEXTURE |
      SVGA3DFORMAT_OP_CONVERT_TO_ARGB |
      SVGA3DFORMAT_OP_MEMBEROFGROUP_ARGB |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN |
      SVGA3DFORMAT_OP_SRGBREAD |
      SVGA3DFORMAT_OP_SRGBWRITE |
      SVGA3DFORMAT_OP_SAME_FORMAT_RENDERTARGET |
      SVGA3DFORMAT_OP_OFFSCREEN_RENDERTARGET
   },
   {
      SVGA3D_A1R5G5B5,
      SVGA3D_DEVCAP_SURFACEFMT_A1R5G5B5,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_VOLUMETEXTURE |
      SVGA3DFORMAT_OP_CONVERT_TO_ARGB |
      SVGA3DFORMAT_OP_MEMBEROFGROUP_ARGB |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN |
      SVGA3DFORMAT_OP_SRGBREAD |
      SVGA3DFORMAT_OP_SRGBWRITE |
      SVGA3DFORMAT_OP_SAME_FORMAT_RENDERTARGET |
      SVGA3DFORMAT_OP_SAME_FORMAT_UP_TO_ALPHA_RENDERTARGET |
      SVGA3DFORMAT_OP_OFFSCREEN_RENDERTARGET
   },
   {
      SVGA3D_A4R4G4B4,
      SVGA3D_DEVCAP_SURFACEFMT_A4R4G4B4,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_VOLUMETEXTURE |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN |
      SVGA3DFORMAT_OP_SRGBREAD |
      SVGA3DFORMAT_OP_SRGBWRITE |
      SVGA3DFORMAT_OP_SAME_FORMAT_RENDERTARGET |
      SVGA3DFORMAT_OP_SAME_FORMAT_UP_TO_ALPHA_RENDERTARGET |
      SVGA3DFORMAT_OP_OFFSCREEN_RENDERTARGET
   },
   /*
    * SVGA3D_Z_D32 is not yet supported, and has no corresponding
    * SVGA3D_DEVCAP_xxx.
    */
   {
      SVGA3D_Z_D16,
      SVGA3D_DEVCAP_SURFACEFMT_Z_D16,
      SVGA3DFORMAT_OP_ZSTENCIL |
      SVGA3DFORMAT_OP_ZSTENCIL_WITH_ARBITRARY_COLOR_DEPTH
   },
   {
      SVGA3D_Z_D24S8,
      SVGA3D_DEVCAP_SURFACEFMT_Z_D24S8,
      SVGA3DFORMAT_OP_ZSTENCIL |
      SVGA3DFORMAT_OP_ZSTENCIL_WITH_ARBITRARY_COLOR_DEPTH
   },
   {
      SVGA3D_Z_D15S1,
      SVGA3D_DEVCAP_MAX,
      SVGA3DFORMAT_OP_ZSTENCIL |
      SVGA3DFORMAT_OP_ZSTENCIL_WITH_ARBITRARY_COLOR_DEPTH
   },
   {
      SVGA3D_LUMINANCE8,
      SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE8,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_VOLUMETEXTURE |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN
   },
   {
      SVGA3D_LUMINANCE8_ALPHA8,
      SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE8_ALPHA8,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_VOLUMETEXTURE |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN
   },
   /*
    * SVGA3D_LUMINANCE4_ALPHA4 is not supported, and has no corresponding
    * SVGA3D_DEVCAP_xxx.
    */
   {
      SVGA3D_LUMINANCE16,
      SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE16,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_VOLUMETEXTURE |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN
   },
   {
      SVGA3D_DXT1,
      SVGA3D_DEVCAP_SURFACEFMT_DXT1,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_SRGBREAD |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN
   },
   {
      SVGA3D_DXT2,
      SVGA3D_DEVCAP_SURFACEFMT_DXT2,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_SRGBREAD |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN
   },
   {
      SVGA3D_DXT3,
      SVGA3D_DEVCAP_SURFACEFMT_DXT3,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_SRGBREAD |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN
   },
   {
      SVGA3D_DXT4,
      SVGA3D_DEVCAP_SURFACEFMT_DXT4,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_SRGBREAD |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN
   },
   {
      SVGA3D_DXT5,
      SVGA3D_DEVCAP_SURFACEFMT_DXT5,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_SRGBREAD |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN
   },
   {
      SVGA3D_BUMPU8V8,
      SVGA3D_DEVCAP_SURFACEFMT_BUMPU8V8,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_VOLUMETEXTURE |
      SVGA3DFORMAT_OP_BUMPMAP |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN
   },
   /*
    * SVGA3D_BUMPL6V5U5 is unsupported; it has no corresponding
    * SVGA3D_DEVCAP_xxx.
    */
   {
      SVGA3D_BUMPX8L8V8U8,
      SVGA3D_DEVCAP_SURFACEFMT_BUMPX8L8V8U8,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_BUMPMAP |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN
   },
   /*
    * SVGA3D_BUMPL8V8U8 is unsupported; it has no corresponding
    * SVGA3D_DEVCAP_xxx. SVGA3D_BUMPX8L8V8U8 should be used instead.
    */
   {
      SVGA3D_ARGB_S10E5,
      SVGA3D_DEVCAP_SURFACEFMT_ARGB_S10E5,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_VOLUMETEXTURE |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN |
      SVGA3DFORMAT_OP_SRGBREAD |
      SVGA3DFORMAT_OP_SRGBWRITE |
      SVGA3DFORMAT_OP_SAME_FORMAT_RENDERTARGET |
      SVGA3DFORMAT_OP_OFFSCREEN_RENDERTARGET
   },
   {
      SVGA3D_ARGB_S23E8,
      SVGA3D_DEVCAP_SURFACEFMT_ARGB_S23E8,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_VOLUMETEXTURE |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN |
      SVGA3DFORMAT_OP_SRGBREAD |
      SVGA3DFORMAT_OP_SRGBWRITE |
      SVGA3DFORMAT_OP_SAME_FORMAT_RENDERTARGET |
      SVGA3DFORMAT_OP_OFFSCREEN_RENDERTARGET
   },
   {
      SVGA3D_A2R10G10B10,
      SVGA3D_DEVCAP_SURFACEFMT_A2R10G10B10,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_VOLUMETEXTURE |
      SVGA3DFORMAT_OP_CONVERT_TO_ARGB |
      SVGA3DFORMAT_OP_MEMBEROFGROUP_ARGB |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN |
      SVGA3DFORMAT_OP_SRGBREAD |
      SVGA3DFORMAT_OP_SRGBWRITE |
      SVGA3DFORMAT_OP_SAME_FORMAT_RENDERTARGET |
      SVGA3DFORMAT_OP_OFFSCREEN_RENDERTARGET
   },
   /*
    * SVGA3D_V8U8 is unsupported; it has no corresponding
    * SVGA3D_DEVCAP_xxx. SVGA3D_BUMPU8V8 should be used instead.
    */
   {
      SVGA3D_Q8W8V8U8,
      SVGA3D_DEVCAP_SURFACEFMT_Q8W8V8U8,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_BUMPMAP |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN
   },
   {
      SVGA3D_CxV8U8,
      SVGA3D_DEVCAP_SURFACEFMT_CxV8U8,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_BUMPMAP |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN
   },
   /*
    * SVGA3D_X8L8V8U8 is unsupported; it has no corresponding
    * SVGA3D_DEVCAP_xxx. SVGA3D_BUMPX8L8V8U8 should be used instead.
    */
   {
      SVGA3D_A2W10V10U10,
      SVGA3D_DEVCAP_SURFACEFMT_A2W10V10U10,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_BUMPMAP |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN
   },
   {
      SVGA3D_ALPHA8,
      SVGA3D_DEVCAP_SURFACEFMT_ALPHA8,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_VOLUMETEXTURE |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN
   },
   {
      SVGA3D_R_S10E5,
      SVGA3D_DEVCAP_SURFACEFMT_R_S10E5,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_VOLUMETEXTURE |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN |
      SVGA3DFORMAT_OP_SRGBREAD |
      SVGA3DFORMAT_OP_SRGBWRITE |
      SVGA3DFORMAT_OP_SAME_FORMAT_RENDERTARGET |
      SVGA3DFORMAT_OP_OFFSCREEN_RENDERTARGET
   },
   {
      SVGA3D_R_S23E8,
      SVGA3D_DEVCAP_SURFACEFMT_R_S23E8,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_VOLUMETEXTURE |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN |
      SVGA3DFORMAT_OP_SRGBREAD |
      SVGA3DFORMAT_OP_SRGBWRITE |
      SVGA3DFORMAT_OP_SAME_FORMAT_RENDERTARGET |
      SVGA3DFORMAT_OP_OFFSCREEN_RENDERTARGET
   },
   {
      SVGA3D_RG_S10E5,
      SVGA3D_DEVCAP_SURFACEFMT_RG_S10E5,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_VOLUMETEXTURE |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN |
      SVGA3DFORMAT_OP_SRGBREAD |
      SVGA3DFORMAT_OP_SRGBWRITE |
      SVGA3DFORMAT_OP_SAME_FORMAT_RENDERTARGET |
      SVGA3DFORMAT_OP_OFFSCREEN_RENDERTARGET
   },
   {
      SVGA3D_RG_S23E8,
      SVGA3D_DEVCAP_SURFACEFMT_RG_S23E8,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_VOLUMETEXTURE |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN |
      SVGA3DFORMAT_OP_SRGBREAD |
      SVGA3DFORMAT_OP_SRGBWRITE |
      SVGA3DFORMAT_OP_SAME_FORMAT_RENDERTARGET |
      SVGA3DFORMAT_OP_OFFSCREEN_RENDERTARGET
   },
   /*
    * SVGA3D_BUFFER is a placeholder format for index/vertex buffers.
    */
   {
      SVGA3D_Z_D24X8,
      SVGA3D_DEVCAP_SURFACEFMT_Z_D24X8,
      SVGA3DFORMAT_OP_ZSTENCIL |
      SVGA3DFORMAT_OP_ZSTENCIL_WITH_ARBITRARY_COLOR_DEPTH
   },
   {
      SVGA3D_V16U16,
      SVGA3D_DEVCAP_SURFACEFMT_V16U16,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_VOLUMETEXTURE |
      SVGA3DFORMAT_OP_BUMPMAP |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN
   },
   {
      SVGA3D_G16R16,
      SVGA3D_DEVCAP_SURFACEFMT_G16R16,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_VOLUMETEXTURE |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN |
      SVGA3DFORMAT_OP_SAME_FORMAT_RENDERTARGET |
      SVGA3DFORMAT_OP_OFFSCREEN_RENDERTARGET
   },
   {
      SVGA3D_A16B16G16R16,
      SVGA3D_DEVCAP_SURFACEFMT_A16B16G16R16,
      SVGA3DFORMAT_OP_TEXTURE |
      SVGA3DFORMAT_OP_CUBETEXTURE |
      SVGA3DFORMAT_OP_VOLUMETEXTURE |
      SVGA3DFORMAT_OP_OFFSCREENPLAIN |
      SVGA3DFORMAT_OP_SAME_FORMAT_RENDERTARGET |
      SVGA3DFORMAT_OP_OFFSCREEN_RENDERTARGET
   },
   {
      SVGA3D_UYVY,
      SVGA3D_DEVCAP_SURFACEFMT_UYVY,
      0
   },
   {
      SVGA3D_YUY2,
      SVGA3D_DEVCAP_SURFACEFMT_YUY2,
      0
   },
   {
      SVGA3D_NV12,
      SVGA3D_DEVCAP_SURFACEFMT_NV12,
      0
   },
   {
      SVGA3D_AYUV,
      SVGA3D_DEVCAP_SURFACEFMT_AYUV,
      0
   },
   {
      SVGA3D_Z_DF16,
      SVGA3D_DEVCAP_SURFACEFMT_Z_DF16,
      0
   },
   {
      SVGA3D_Z_DF24,
      SVGA3D_DEVCAP_SURFACEFMT_Z_DF24,
      0
   },
   {
      SVGA3D_Z_D24S8_INT,
      SVGA3D_DEVCAP_SURFACEFMT_Z_D24S8_INT,
      0
   },
};


/*
 * Get format capabilities from the host.  It takes in consideration
 * deprecated/unsupported formats, and formats which are implicitely assumed to
 * be supported when the host does not provide an explicit capability entry.
 */
void
svga_get_format_cap(struct svga_screen *ss,
                    SVGA3dSurfaceFormat format,
                    SVGA3dSurfaceFormatCaps *caps)
{
   const struct format_cap *entry;

   for (entry = format_cap_table; entry < format_cap_table + Elements(format_cap_table); ++entry) {
      if (entry->format == format) {
         struct svga_winsys_screen *sws = ss->sws;
         SVGA3dDevCapResult result;

         if (sws->get_cap(sws, entry->index, &result)) {
            /* Explicitly advertised format */
            caps->value = result.u;
         } else {
            /* Implicitly advertised format -- use default caps */
            caps->value = entry->defaultOperations;
         }

         return;
      }
   }

   /* Unsupported format */
   caps->value = 0;
}


/**
 * Return block size and bytes per block for the given SVGA3D format.
 * block_width and block_height are one for uncompressed formats and
 * greater than one for compressed formats.
 * Note: we don't handle formats that are unsupported, according to
 * the format_cap_table above.
 */
void
svga_format_size(SVGA3dSurfaceFormat format,
                 unsigned *block_width,
                 unsigned *block_height,
                 unsigned *bytes_per_block)
{
   *block_width = *block_height = 1;

   switch (format) {
   case SVGA3D_X8R8G8B8:
   case SVGA3D_A8R8G8B8:
      *bytes_per_block = 4;
      return;

   case SVGA3D_R5G6B5:
   case SVGA3D_X1R5G5B5:
   case SVGA3D_A1R5G5B5:
   case SVGA3D_A4R4G4B4:
      *bytes_per_block = 2;
      return;

   case SVGA3D_Z_D32:
      *bytes_per_block = 4;
      return;

   case SVGA3D_Z_D16:
      *bytes_per_block = 2;
      return;

   case SVGA3D_Z_D24S8:
      *bytes_per_block = 4;
      return;

   case SVGA3D_Z_D15S1:
      *bytes_per_block = 2;
      return;

   case SVGA3D_LUMINANCE8:
   case SVGA3D_LUMINANCE4_ALPHA4:
      *bytes_per_block = 1;
      return;

   case SVGA3D_LUMINANCE16:
   case SVGA3D_LUMINANCE8_ALPHA8:
      *bytes_per_block = 2;
      return;

   case SVGA3D_DXT1:
   case SVGA3D_DXT2:
      *block_width = *block_height = 4;
      *bytes_per_block = 8;
      return;

   case SVGA3D_DXT3:
   case SVGA3D_DXT4:
   case SVGA3D_DXT5:
      *block_width = *block_height = 4;
      *bytes_per_block = 16;
      return;

   case SVGA3D_BUMPU8V8:
   case SVGA3D_BUMPL6V5U5:
      *bytes_per_block = 2;
      return;

   case SVGA3D_BUMPX8L8V8U8:
      *bytes_per_block = 4;
      return;

   case SVGA3D_ARGB_S10E5:
      *bytes_per_block = 8;
      return;

   case SVGA3D_ARGB_S23E8:
      *bytes_per_block = 16;
      return;

   case SVGA3D_A2R10G10B10:
      *bytes_per_block = 4;
      return;

   case SVGA3D_Q8W8V8U8:
      *bytes_per_block = 4;
      return;

   case SVGA3D_CxV8U8:
      *bytes_per_block = 2;
      return;

   case SVGA3D_X8L8V8U8:
   case SVGA3D_A2W10V10U10:
      *bytes_per_block = 4;
      return;

   case SVGA3D_ALPHA8:
      *bytes_per_block = 1;
      return;

   case SVGA3D_R_S10E5:
      *bytes_per_block = 2;
      return;
   case SVGA3D_R_S23E8:
      *bytes_per_block = 4;
      return;
   case SVGA3D_RG_S10E5:
      *bytes_per_block = 4;
      return;
   case SVGA3D_RG_S23E8:
      *bytes_per_block = 8;
      return;

   case SVGA3D_BUFFER:
      *bytes_per_block = 1;
      return;

   case SVGA3D_Z_D24X8:
      *bytes_per_block = 4;
      return;

   case SVGA3D_V16U16:
      *bytes_per_block = 4;
      return;

   case SVGA3D_G16R16:
      *bytes_per_block = 4;
      return;

   case SVGA3D_A16B16G16R16:
      *bytes_per_block = 8;
      return;

   case SVGA3D_Z_DF16:
      *bytes_per_block = 2;
      return;
   case SVGA3D_Z_DF24:
      *bytes_per_block = 4;
      return;
   case SVGA3D_Z_D24S8_INT:
      *bytes_per_block = 4;
      return;

   default:
      debug_printf("format %u\n", (unsigned) format);
      assert(!"unexpected format in svga_format_size()");
      *bytes_per_block = 4;
   }
}