xref: /aosp_15_r20/external/mesa3d/src/intel/isl/isl_query.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2023 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  */
24 
25 #include <stdbool.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <inttypes.h>
32 #include <assert.h>
33 #include <getopt.h>
34 
35 #include "dev/intel_debug.h"
36 #include "dev/intel_device_info.h"
37 #include "isl/isl.h"
38 
39 const struct debug_control usages_control[] = {
40    { "rt",          ISL_SURF_USAGE_RENDER_TARGET_BIT, },
41    { "depth",       ISL_SURF_USAGE_DEPTH_BIT, },
42    { "stencil",     ISL_SURF_USAGE_STENCIL_BIT, },
43    { "texture",     ISL_SURF_USAGE_TEXTURE_BIT, },
44    { "cube",        ISL_SURF_USAGE_CUBE_BIT, },
45    { "disable-aux", ISL_SURF_USAGE_DISABLE_AUX_BIT, },
46    { "display",     ISL_SURF_USAGE_DISPLAY_BIT, },
47    { "storage",     ISL_SURF_USAGE_STORAGE_BIT, },
48    { "hiz",         ISL_SURF_USAGE_HIZ_BIT, },
49    { "mcs",         ISL_SURF_USAGE_MCS_BIT, },
50    { "ccs",         ISL_SURF_USAGE_CCS_BIT, },
51    { "vertex",      ISL_SURF_USAGE_VERTEX_BUFFER_BIT, },
52    { "index",       ISL_SURF_USAGE_INDEX_BUFFER_BIT, },
53    { "constant",    ISL_SURF_USAGE_CONSTANT_BUFFER_BIT, },
54    { "cpb",         ISL_SURF_USAGE_CPB_BIT, },
55 };
56 
57 static void
print_help(const char * name)58 print_help(const char *name)
59 {
60    printf("Usage: %s [OPTION] ...\n", name);
61    printf("Prints out ISL surface parameters.\n");
62    printf("    --help                          Display this help and exit\n");
63    printf("    -p, --platform NAME             Platform to use\n");
64    printf("    -D, --dimension DIM             Dimension of the surface (1, 2, 3)\n");
65    printf("    -f, --format FORMAT             Format of the surface (--list-formats to list them)\n");
66    printf("    -w, --width WIDTH               Width of the surface\n");
67    printf("    -h, --height HEIGHT             Height of the surface\n");
68    printf("    -d, --depth DEPTH               Depth of the surface\n");
69    printf("    -a, --array ARRAY               Array length of the surface\n");
70    printf("    -s, --samples SAMPLES           Sample count of the surface\n");
71    printf("    -l, --levels LEVELS             Miplevel count of the surface\n");
72    printf("    -P, --pitch PITCH               Row pitch of the surface\n");
73    printf("    -u, --usages USAGE1,USAGE2,...  Usage list of the surface\n");
74    printf("        usages: ");
75    for (uint32_t i = 0; i < ARRAY_SIZE(usages_control); i++)
76       printf("%s%s", i == 0 ? "" : ", ", usages_control[i].string);
77    printf("\n");
78    printf("    -F, --list-formats              List format names\n");
79    printf("\n");
80 }
81 
82 int
main(int argc,char * argv[])83 main(int argc, char *argv[])
84 {
85    int c, i;
86    bool help = false;
87    const struct option isl_opts[] = {
88       { "help",         no_argument,       (int *) &help,               true },
89       { "platform",     required_argument, NULL,                        'p' },
90       { "dimension",    required_argument, NULL,                        'D' },
91       { "format",       required_argument, NULL,                        'f' },
92       { "width",        required_argument, NULL,                        'w' },
93       { "height",       required_argument, NULL,                        'h' },
94       { "depth",        required_argument, NULL,                        'd' },
95       { "array",        required_argument, NULL,                        'a' },
96       { "samples",      required_argument, NULL,                        's' },
97       { "levels",       required_argument, NULL,                        'l' },
98       { "pitch",        required_argument, NULL,                        'P' },
99       { "usages",       required_argument, NULL,                        'u' },
100       { "list-formats",       no_argument, NULL,                        'F' },
101       { NULL,         0,                 NULL,                          0 }
102    };
103 
104    const char *platform          = "tgl";
105    enum isl_surf_dim dimension   = ISL_SURF_DIM_2D;
106    enum isl_format format        = ISL_FORMAT_R8G8B8A8_UNORM;
107    uint32_t width                = 1;
108    uint32_t height               = 1;
109    uint32_t depth                = 1;
110    uint32_t array                = 1;
111    uint32_t samples              = 1;
112    uint32_t levels               = 1;
113    uint32_t row_pitch            = 0;
114    isl_surf_usage_flags_t usages = ISL_SURF_USAGE_RENDER_TARGET_BIT | ISL_SURF_USAGE_TEXTURE_BIT;
115 
116    i = 0;
117    while ((c = getopt_long(argc, argv, "p:D:f:w:h:d:a:s:l:P:u:F", isl_opts, &i)) != -1) {
118       switch (c) {
119       case 'p':
120          platform = optarg;
121          break;
122       case 'D':
123          dimension = atoi(optarg) - 1;
124          break;
125       case 'f':
126          for (uint32_t i = 0; i < ISL_NUM_FORMATS; i++) {
127             if (isl_format_get_layout(i)->bpb != 0 &&
128                 strcmp(optarg, isl_format_get_short_name(i)) == 0) {
129                format = i;
130                break;
131             }
132          }
133          break;
134       case 'w':
135          width = atoi(optarg);
136          break;
137       case 'h':
138          height = atoi(optarg);
139          break;
140       case 'd':
141          depth = atoi(optarg);
142          break;
143       case 'a':
144          array = atoi(optarg);
145          break;
146       case 's':
147          samples = atoi(optarg);
148          break;
149       case 'l':
150          levels = atoi(optarg);
151          break;
152       case 'P':
153          row_pitch = atoi(optarg);
154          break;
155       case 'u': {
156          usages = parse_debug_string(optarg, usages_control);
157          break;
158       }
159       case 'F':
160          printf("Format list:\n");
161          for (uint32_t i = 0; i < ISL_NUM_FORMATS; i++) {
162             if (isl_format_get_layout(i)->bpb != 0)
163                printf("   %s\n", isl_format_get_short_name(i));
164          }
165          exit(EXIT_SUCCESS);
166          break;
167       case '?':
168          print_help(argv[0]);
169          exit(EXIT_FAILURE);
170       default:
171          break;
172       }
173    }
174 
175    process_intel_debug_variable();
176 
177    int pci_id = intel_device_name_to_pci_device_id(platform);
178    if (pci_id == -1) {
179       printf("Unknown platform: %s\n", platform);
180       exit(EXIT_FAILURE);
181    }
182 
183    struct intel_device_info devinfo;
184    if (!intel_get_device_info_from_pci_id(pci_id, &devinfo)) {
185       printf("Unable to identify devid=0x%x\n", pci_id);
186       exit(EXIT_FAILURE);
187    }
188 
189    struct isl_device isl_dev;
190    isl_device_init(&isl_dev, &devinfo);
191 
192    struct isl_surf surf;
193    bool ok = isl_surf_init(&isl_dev, &surf,
194                            .format = format,
195                            .dim = dimension,
196                            .width = width,
197                            .height = height,
198                            .depth = depth,
199                            .levels = levels,
200                            .array_len = array,
201                            .samples = samples,
202                            .row_pitch_B = row_pitch,
203                            .usage = usages,
204                            .tiling_flags = ISL_TILING_ANY_MASK);
205    if (!ok) {
206       printf("Surface creation failed\n");
207       exit(EXIT_FAILURE);
208    }
209 
210 #define _2d_vals(name) \
211    name.w, name.h
212 #define _3d_vals(name) \
213    name.w, name.h, name.d
214 #define _4d_vals(name) \
215    name.w, name.h, name.d, name.a
216 
217    printf("Surface parameters:\n");
218    printf("  dim:                 %ud\n", surf.dim + 1);
219    printf("  dim_layout:          %u\n", surf.dim_layout);
220    printf("  msaa_layout:         %u\n", surf.msaa_layout);
221    printf("  tiling:              %s\n", isl_tiling_to_name(surf.tiling));
222    printf("  format:              %s\n", isl_format_get_short_name(surf.format));
223    printf("  img_align_el:        %ux%ux%u\n", _3d_vals(surf.image_alignment_el));
224    printf("  logical_level0_px:   %ux%ux%ux%u\n", _4d_vals(surf.logical_level0_px));
225    printf("  phys_level0_sa:      %ux%ux%ux%u\n", _4d_vals(surf.phys_level0_sa));
226    printf("  levels:              %u\n", surf.levels);
227    printf("  samples:             %ux\n", surf.samples);
228    printf("  size_B:              %"PRIu64"\n", surf.size_B);
229    printf("  alignment_B:         %u\n", surf.alignment_B);
230    printf("  row_pitch_B:         %u\n", surf.row_pitch_B);
231    printf("  array_pitch_el_rows: %u\n", surf.array_pitch_el_rows);
232 
233    struct isl_tile_info tile_info;
234    isl_surf_get_tile_info(&surf, &tile_info);
235    printf("  tile_info:\n");
236    printf("    tiling:             %s\n", isl_tiling_to_name(tile_info.tiling));
237    printf("    format_bpb:         %u\n", tile_info.format_bpb);
238    printf("    logical_extent_el:  %ux%ux%ux%u\n", _4d_vals(tile_info.logical_extent_el));
239    printf("    phys_extent_B:      %ux%u = %u\n",
240           _2d_vals(tile_info.phys_extent_B),
241           tile_info.phys_extent_B.w * tile_info.phys_extent_B.h);
242 
243    return EXIT_SUCCESS;
244 }
245