xref: /aosp_15_r20/external/mesa3d/src/mesa/state_tracker/tests/st_format.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright 2007 VMware, Inc.
4  * Copyright (c) 2008-2010 VMware, Inc.
5  * Copyright (c) 2019 Google, LLC
6  * All Rights Reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sub license, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the
17  * next paragraph) shall be included in all copies or substantial portions
18  * of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
24  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  *
28  **************************************************************************/
29 
30 #include "pipe/p_context.h"
31 #include "pipe/p_screen.h"
32 #include "state_tracker/st_context.h"
33 #include "state_tracker/st_format.h"
34 #include "state_tracker/st_texture.h"
35 #include "util/format/u_format.h"
36 #include <stdbool.h>
37 
38 static bool
is_format_supported(struct pipe_screen * pscreen,enum pipe_format format,enum pipe_texture_target target,unsigned sample_count,unsigned storage_sample_count,unsigned usage)39 is_format_supported(struct pipe_screen *pscreen,
40                     enum pipe_format format,
41                     enum pipe_texture_target target,
42                     unsigned sample_count,
43                     unsigned storage_sample_count,
44                     unsigned usage)
45 {
46    return true;
47 }
48 
main(int argc,char ** argv)49 int main(int argc, char **argv)
50 {
51    struct pipe_screen screen = {
52       .is_format_supported = is_format_supported,
53    };
54    struct pipe_context pctx = {
55       .screen = &screen,
56    };
57    struct st_context local_st = {
58       .pipe = &pctx,
59       .has_etc1 = true,
60       .has_etc2 = true,
61       .has_astc_2d_ldr = true,
62       .has_astc_5x5_ldr = true,
63       .has_s3tc = true,
64       .has_rgtc = true,
65       .has_latc = true,
66       .has_bptc = true,
67    };
68    struct st_context *st = &local_st;
69 
70    GLuint i;
71 
72    /* test all Mesa formats */
73    for (i = 1; i < MESA_FORMAT_COUNT; i++) {
74       if (!_mesa_get_format_name(i))
75          continue;
76 
77       enum pipe_format pf;
78 
79       assert(!st_compressed_format_fallback(st, i));
80 
81       pf = st_mesa_format_to_pipe_format(st, i);
82       if (pf != PIPE_FORMAT_NONE) {
83          mesa_format mf = st_pipe_format_to_mesa_format(pf);
84          if (mf != i) {
85             fprintf(stderr, "Round-tripping %s -> %s -> %s failed\n",
86                     _mesa_get_format_name(i), util_format_short_name(pf),
87                     _mesa_get_format_name(mf));
88             return 1;
89          }
90 
91          const struct util_format_description *desc = util_format_description(i);
92 
93          /* Make sure that gallium and Mesa agree on whether the format is an
94           * array format.
95           */
96          if (desc->nr_channels > 1) {
97             bool mesa_array = (_mesa_get_format_layout(mf) ==
98                                MESA_FORMAT_LAYOUT_ARRAY);
99             bool gallium_array = desc->is_array && !desc->is_bitmask;
100             /* We should probably be checking equality here, but we have some
101              * UINT and SINT types that are array formats in Mesa but not in
102              * gallium.
103              */
104             if (gallium_array && !mesa_array) {
105                fprintf(stderr, "%s is %sarray, %s is %sarray\n",
106                        util_format_short_name(i),
107                        gallium_array ? "" : "not ",
108                        _mesa_get_format_name(mf),
109                        mesa_array ? "" : "not ");
110                return 1;
111             }
112          }
113       }
114    }
115 
116    return 0;
117 }
118