1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2014-2017 Broadcom
3bf215546Sopenharmony_ci * Copyright (C) 2012 Rob Clark <robclark@freedesktop.org>
4bf215546Sopenharmony_ci *
5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
6bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
7bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
8bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
10bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci *
12bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
13bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
14bf215546Sopenharmony_ci * Software.
15bf215546Sopenharmony_ci *
16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22bf215546Sopenharmony_ci * IN THE SOFTWARE.
23bf215546Sopenharmony_ci */
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci#include <sys/sysinfo.h>
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "common/v3d_device_info.h"
28bf215546Sopenharmony_ci#include "common/v3d_limits.h"
29bf215546Sopenharmony_ci#include "util/os_misc.h"
30bf215546Sopenharmony_ci#include "pipe/p_defines.h"
31bf215546Sopenharmony_ci#include "pipe/p_screen.h"
32bf215546Sopenharmony_ci#include "pipe/p_state.h"
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci#include "util/u_debug.h"
35bf215546Sopenharmony_ci#include "util/u_memory.h"
36bf215546Sopenharmony_ci#include "util/format/u_format.h"
37bf215546Sopenharmony_ci#include "util/u_hash_table.h"
38bf215546Sopenharmony_ci#include "util/u_screen.h"
39bf215546Sopenharmony_ci#include "util/u_transfer_helper.h"
40bf215546Sopenharmony_ci#include "util/ralloc.h"
41bf215546Sopenharmony_ci#include "util/xmlconfig.h"
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci#include <xf86drm.h>
44bf215546Sopenharmony_ci#include "v3d_screen.h"
45bf215546Sopenharmony_ci#include "v3d_context.h"
46bf215546Sopenharmony_ci#include "v3d_resource.h"
47bf215546Sopenharmony_ci#include "compiler/v3d_compiler.h"
48bf215546Sopenharmony_ci#include "drm-uapi/drm_fourcc.h"
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_cistatic const char *
51bf215546Sopenharmony_civ3d_screen_get_name(struct pipe_screen *pscreen)
52bf215546Sopenharmony_ci{
53bf215546Sopenharmony_ci        struct v3d_screen *screen = v3d_screen(pscreen);
54bf215546Sopenharmony_ci
55bf215546Sopenharmony_ci        if (!screen->name) {
56bf215546Sopenharmony_ci                screen->name = ralloc_asprintf(screen,
57bf215546Sopenharmony_ci                                               "V3D %d.%d",
58bf215546Sopenharmony_ci                                               screen->devinfo.ver / 10,
59bf215546Sopenharmony_ci                                               screen->devinfo.ver % 10);
60bf215546Sopenharmony_ci        }
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci        return screen->name;
63bf215546Sopenharmony_ci}
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_cistatic const char *
66bf215546Sopenharmony_civ3d_screen_get_vendor(struct pipe_screen *pscreen)
67bf215546Sopenharmony_ci{
68bf215546Sopenharmony_ci        return "Broadcom";
69bf215546Sopenharmony_ci}
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_cistatic void
72bf215546Sopenharmony_civ3d_screen_destroy(struct pipe_screen *pscreen)
73bf215546Sopenharmony_ci{
74bf215546Sopenharmony_ci        struct v3d_screen *screen = v3d_screen(pscreen);
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci        _mesa_hash_table_destroy(screen->bo_handles, NULL);
77bf215546Sopenharmony_ci        v3d_bufmgr_destroy(pscreen);
78bf215546Sopenharmony_ci        slab_destroy_parent(&screen->transfer_pool);
79bf215546Sopenharmony_ci        if (screen->ro)
80bf215546Sopenharmony_ci                screen->ro->destroy(screen->ro);
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci        if (using_v3d_simulator)
83bf215546Sopenharmony_ci                v3d_simulator_destroy(screen->sim_file);
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci        v3d_compiler_free(screen->compiler);
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci#ifdef ENABLE_SHADER_CACHE
88bf215546Sopenharmony_ci        if (screen->disk_cache)
89bf215546Sopenharmony_ci                disk_cache_destroy(screen->disk_cache);
90bf215546Sopenharmony_ci#endif
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci        u_transfer_helper_destroy(pscreen->transfer_helper);
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci        close(screen->fd);
95bf215546Sopenharmony_ci        ralloc_free(pscreen);
96bf215546Sopenharmony_ci}
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_cistatic bool
99bf215546Sopenharmony_civ3d_has_feature(struct v3d_screen *screen, enum drm_v3d_param feature)
100bf215546Sopenharmony_ci{
101bf215546Sopenharmony_ci        struct drm_v3d_get_param p = {
102bf215546Sopenharmony_ci                .param = feature,
103bf215546Sopenharmony_ci        };
104bf215546Sopenharmony_ci        int ret = v3d_ioctl(screen->fd, DRM_IOCTL_V3D_GET_PARAM, &p);
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci        if (ret != 0)
107bf215546Sopenharmony_ci                return false;
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci        return p.value;
110bf215546Sopenharmony_ci}
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_cistatic int
113bf215546Sopenharmony_civ3d_screen_get_param(struct pipe_screen *pscreen, enum pipe_cap param)
114bf215546Sopenharmony_ci{
115bf215546Sopenharmony_ci        struct v3d_screen *screen = v3d_screen(pscreen);
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci        switch (param) {
118bf215546Sopenharmony_ci                /* Supported features (boolean caps). */
119bf215546Sopenharmony_ci        case PIPE_CAP_VERTEX_COLOR_UNCLAMPED:
120bf215546Sopenharmony_ci        case PIPE_CAP_BUFFER_MAP_PERSISTENT_COHERENT:
121bf215546Sopenharmony_ci        case PIPE_CAP_NPOT_TEXTURES:
122bf215546Sopenharmony_ci        case PIPE_CAP_BLEND_EQUATION_SEPARATE:
123bf215546Sopenharmony_ci        case PIPE_CAP_TEXTURE_MULTISAMPLE:
124bf215546Sopenharmony_ci        case PIPE_CAP_TEXTURE_SWIZZLE:
125bf215546Sopenharmony_ci        case PIPE_CAP_VERTEX_ELEMENT_INSTANCE_DIVISOR:
126bf215546Sopenharmony_ci        case PIPE_CAP_START_INSTANCE:
127bf215546Sopenharmony_ci        case PIPE_CAP_VS_INSTANCEID:
128bf215546Sopenharmony_ci        case PIPE_CAP_FRAGMENT_SHADER_TEXTURE_LOD:
129bf215546Sopenharmony_ci        case PIPE_CAP_FRAGMENT_SHADER_DERIVATIVES:
130bf215546Sopenharmony_ci        case PIPE_CAP_PRIMITIVE_RESTART_FIXED_INDEX:
131bf215546Sopenharmony_ci        case PIPE_CAP_EMULATE_NONFIXED_PRIMITIVE_RESTART:
132bf215546Sopenharmony_ci        case PIPE_CAP_PRIMITIVE_RESTART:
133bf215546Sopenharmony_ci        case PIPE_CAP_OCCLUSION_QUERY:
134bf215546Sopenharmony_ci        case PIPE_CAP_POINT_SPRITE:
135bf215546Sopenharmony_ci        case PIPE_CAP_STREAM_OUTPUT_PAUSE_RESUME:
136bf215546Sopenharmony_ci        case PIPE_CAP_DRAW_INDIRECT:
137bf215546Sopenharmony_ci        case PIPE_CAP_MULTI_DRAW_INDIRECT:
138bf215546Sopenharmony_ci        case PIPE_CAP_QUADS_FOLLOW_PROVOKING_VERTEX_CONVENTION:
139bf215546Sopenharmony_ci        case PIPE_CAP_SIGNED_VERTEX_BUFFER_OFFSET:
140bf215546Sopenharmony_ci        case PIPE_CAP_SHADER_CAN_READ_OUTPUTS:
141bf215546Sopenharmony_ci        case PIPE_CAP_SHADER_PACK_HALF_FLOAT:
142bf215546Sopenharmony_ci        case PIPE_CAP_TEXTURE_HALF_FLOAT_LINEAR:
143bf215546Sopenharmony_ci        case PIPE_CAP_FRAMEBUFFER_NO_ATTACHMENT:
144bf215546Sopenharmony_ci        case PIPE_CAP_FS_FACE_IS_INTEGER_SYSVAL:
145bf215546Sopenharmony_ci        case PIPE_CAP_TGSI_TEXCOORD:
146bf215546Sopenharmony_ci        case PIPE_CAP_TEXTURE_MIRROR_CLAMP_TO_EDGE:
147bf215546Sopenharmony_ci        case PIPE_CAP_SAMPLER_VIEW_TARGET:
148bf215546Sopenharmony_ci        case PIPE_CAP_ANISOTROPIC_FILTER:
149bf215546Sopenharmony_ci        case PIPE_CAP_COPY_BETWEEN_COMPRESSED_AND_PLAIN_FORMATS:
150bf215546Sopenharmony_ci        case PIPE_CAP_INDEP_BLEND_FUNC:
151bf215546Sopenharmony_ci                return 1;
152bf215546Sopenharmony_ci
153bf215546Sopenharmony_ci        case PIPE_CAP_POLYGON_OFFSET_CLAMP:
154bf215546Sopenharmony_ci                return screen->devinfo.ver >= 41;
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci        case PIPE_CAP_TEXTURE_QUERY_LOD:
157bf215546Sopenharmony_ci                return screen->devinfo.ver >= 42;
158bf215546Sopenharmony_ci                break;
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_ci        case PIPE_CAP_PACKED_UNIFORMS:
161bf215546Sopenharmony_ci                /* We can't enable this flag, because it results in load_ubo
162bf215546Sopenharmony_ci                 * intrinsics across a 16b boundary, but v3d's TMU general
163bf215546Sopenharmony_ci                 * memory accesses wrap on 16b boundaries.
164bf215546Sopenharmony_ci                 */
165bf215546Sopenharmony_ci                return 0;
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_ci        case PIPE_CAP_NIR_IMAGES_AS_DEREF:
168bf215546Sopenharmony_ci                return 0;
169bf215546Sopenharmony_ci
170bf215546Sopenharmony_ci        case PIPE_CAP_TEXTURE_TRANSFER_MODES:
171bf215546Sopenharmony_ci                /* XXX perf: we don't want to emit these extra blits for
172bf215546Sopenharmony_ci                 * glReadPixels(), since we still have to do an uncached read
173bf215546Sopenharmony_ci                 * from the GPU of the result after waiting for the TFU blit
174bf215546Sopenharmony_ci                 * to happen.  However, disabling this introduces instability
175bf215546Sopenharmony_ci                 * in
176bf215546Sopenharmony_ci                 * dEQP-GLES31.functional.image_load_store.early_fragment_tests.*
177bf215546Sopenharmony_ci                 * and corruption in chromium's rendering.
178bf215546Sopenharmony_ci                 */
179bf215546Sopenharmony_ci                return PIPE_TEXTURE_TRANSFER_BLIT;
180bf215546Sopenharmony_ci
181bf215546Sopenharmony_ci        case PIPE_CAP_COMPUTE:
182bf215546Sopenharmony_ci                return screen->has_csd && screen->devinfo.ver >= 41;
183bf215546Sopenharmony_ci
184bf215546Sopenharmony_ci        case PIPE_CAP_GENERATE_MIPMAP:
185bf215546Sopenharmony_ci                return v3d_has_feature(screen, DRM_V3D_PARAM_SUPPORTS_TFU);
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_ci        case PIPE_CAP_INDEP_BLEND_ENABLE:
188bf215546Sopenharmony_ci                return screen->devinfo.ver >= 40;
189bf215546Sopenharmony_ci
190bf215546Sopenharmony_ci        case PIPE_CAP_CONSTANT_BUFFER_OFFSET_ALIGNMENT:
191bf215546Sopenharmony_ci                return V3D_NON_COHERENT_ATOM_SIZE;
192bf215546Sopenharmony_ci
193bf215546Sopenharmony_ci        case PIPE_CAP_MAX_TEXTURE_GATHER_COMPONENTS:
194bf215546Sopenharmony_ci                if (screen->devinfo.ver < 40)
195bf215546Sopenharmony_ci                        return 0;
196bf215546Sopenharmony_ci                return 4;
197bf215546Sopenharmony_ci
198bf215546Sopenharmony_ci        case PIPE_CAP_SHADER_BUFFER_OFFSET_ALIGNMENT:
199bf215546Sopenharmony_ci                if (screen->has_cache_flush)
200bf215546Sopenharmony_ci                        return 4;
201bf215546Sopenharmony_ci                else
202bf215546Sopenharmony_ci                        return 0; /* Disables shader storage */
203bf215546Sopenharmony_ci
204bf215546Sopenharmony_ci        case PIPE_CAP_GLSL_FEATURE_LEVEL:
205bf215546Sopenharmony_ci                return 330;
206bf215546Sopenharmony_ci
207bf215546Sopenharmony_ci        case PIPE_CAP_ESSL_FEATURE_LEVEL:
208bf215546Sopenharmony_ci                return 310;
209bf215546Sopenharmony_ci
210bf215546Sopenharmony_ci	case PIPE_CAP_GLSL_FEATURE_LEVEL_COMPATIBILITY:
211bf215546Sopenharmony_ci		return 140;
212bf215546Sopenharmony_ci
213bf215546Sopenharmony_ci        case PIPE_CAP_FS_COORD_ORIGIN_UPPER_LEFT:
214bf215546Sopenharmony_ci                return 1;
215bf215546Sopenharmony_ci        case PIPE_CAP_FS_COORD_ORIGIN_LOWER_LEFT:
216bf215546Sopenharmony_ci                return 0;
217bf215546Sopenharmony_ci        case PIPE_CAP_FS_COORD_PIXEL_CENTER_INTEGER:
218bf215546Sopenharmony_ci                if (screen->devinfo.ver >= 40)
219bf215546Sopenharmony_ci                        return 0;
220bf215546Sopenharmony_ci                else
221bf215546Sopenharmony_ci                        return 1;
222bf215546Sopenharmony_ci        case PIPE_CAP_FS_COORD_PIXEL_CENTER_HALF_INTEGER:
223bf215546Sopenharmony_ci                if (screen->devinfo.ver >= 40)
224bf215546Sopenharmony_ci                        return 1;
225bf215546Sopenharmony_ci                else
226bf215546Sopenharmony_ci                        return 0;
227bf215546Sopenharmony_ci
228bf215546Sopenharmony_ci        case PIPE_CAP_MIXED_FRAMEBUFFER_SIZES:
229bf215546Sopenharmony_ci        case PIPE_CAP_MIXED_COLORBUFFER_FORMATS:
230bf215546Sopenharmony_ci        case PIPE_CAP_MIXED_COLOR_DEPTH_BITS:
231bf215546Sopenharmony_ci                return 1;
232bf215546Sopenharmony_ci
233bf215546Sopenharmony_ci        case PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS:
234bf215546Sopenharmony_ci                return 4;
235bf215546Sopenharmony_ci
236bf215546Sopenharmony_ci        case PIPE_CAP_MAX_VARYINGS:
237bf215546Sopenharmony_ci                return V3D_MAX_FS_INPUTS / 4;
238bf215546Sopenharmony_ci
239bf215546Sopenharmony_ci                /* Texturing. */
240bf215546Sopenharmony_ci        case PIPE_CAP_MAX_TEXTURE_2D_SIZE:
241bf215546Sopenharmony_ci                if (screen->devinfo.ver < 40)
242bf215546Sopenharmony_ci                        return 2048;
243bf215546Sopenharmony_ci                else if (screen->nonmsaa_texture_size_limit)
244bf215546Sopenharmony_ci                        return 7680;
245bf215546Sopenharmony_ci                else
246bf215546Sopenharmony_ci                        return V3D_MAX_IMAGE_DIMENSION;
247bf215546Sopenharmony_ci        case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
248bf215546Sopenharmony_ci        case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
249bf215546Sopenharmony_ci                if (screen->devinfo.ver < 40)
250bf215546Sopenharmony_ci                        return 12;
251bf215546Sopenharmony_ci                else
252bf215546Sopenharmony_ci                        return V3D_MAX_MIP_LEVELS;
253bf215546Sopenharmony_ci        case PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS:
254bf215546Sopenharmony_ci                return V3D_MAX_ARRAY_LAYERS;
255bf215546Sopenharmony_ci
256bf215546Sopenharmony_ci                /* Render targets. */
257bf215546Sopenharmony_ci        case PIPE_CAP_MAX_RENDER_TARGETS:
258bf215546Sopenharmony_ci                return 4;
259bf215546Sopenharmony_ci
260bf215546Sopenharmony_ci        case PIPE_CAP_VENDOR_ID:
261bf215546Sopenharmony_ci                return 0x14E4;
262bf215546Sopenharmony_ci        case PIPE_CAP_ACCELERATED:
263bf215546Sopenharmony_ci                return 1;
264bf215546Sopenharmony_ci        case PIPE_CAP_VIDEO_MEMORY: {
265bf215546Sopenharmony_ci                uint64_t system_memory;
266bf215546Sopenharmony_ci
267bf215546Sopenharmony_ci                if (!os_get_total_physical_memory(&system_memory))
268bf215546Sopenharmony_ci                        return 0;
269bf215546Sopenharmony_ci
270bf215546Sopenharmony_ci                return (int)(system_memory >> 20);
271bf215546Sopenharmony_ci        }
272bf215546Sopenharmony_ci        case PIPE_CAP_UMA:
273bf215546Sopenharmony_ci                return 1;
274bf215546Sopenharmony_ci
275bf215546Sopenharmony_ci        case PIPE_CAP_ALPHA_TEST:
276bf215546Sopenharmony_ci        case PIPE_CAP_FLATSHADE:
277bf215546Sopenharmony_ci        case PIPE_CAP_TWO_SIDED_COLOR:
278bf215546Sopenharmony_ci        case PIPE_CAP_VERTEX_COLOR_CLAMPED:
279bf215546Sopenharmony_ci        case PIPE_CAP_FRAGMENT_COLOR_CLAMPED:
280bf215546Sopenharmony_ci        case PIPE_CAP_GL_CLAMP:
281bf215546Sopenharmony_ci                return 0;
282bf215546Sopenharmony_ci
283bf215546Sopenharmony_ci        /* Geometry shaders */
284bf215546Sopenharmony_ci        case PIPE_CAP_MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS:
285bf215546Sopenharmony_ci                /* Minimum required by GLES 3.2 */
286bf215546Sopenharmony_ci                return 1024;
287bf215546Sopenharmony_ci        case PIPE_CAP_MAX_GEOMETRY_OUTPUT_VERTICES:
288bf215546Sopenharmony_ci                /* MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS / 4 */
289bf215546Sopenharmony_ci                return 256;
290bf215546Sopenharmony_ci        case PIPE_CAP_MAX_GS_INVOCATIONS:
291bf215546Sopenharmony_ci                return 32;
292bf215546Sopenharmony_ci
293bf215546Sopenharmony_ci        case PIPE_CAP_SUPPORTED_PRIM_MODES:
294bf215546Sopenharmony_ci        case PIPE_CAP_SUPPORTED_PRIM_MODES_WITH_RESTART:
295bf215546Sopenharmony_ci                return screen->prim_types;
296bf215546Sopenharmony_ci
297bf215546Sopenharmony_ci        case PIPE_CAP_TEXTURE_BUFFER_OBJECTS:
298bf215546Sopenharmony_ci                return true;
299bf215546Sopenharmony_ci
300bf215546Sopenharmony_ci        case PIPE_CAP_TEXTURE_BUFFER_OFFSET_ALIGNMENT:
301bf215546Sopenharmony_ci                return 256;
302bf215546Sopenharmony_ci
303bf215546Sopenharmony_ci        case PIPE_CAP_IMAGE_STORE_FORMATTED:
304bf215546Sopenharmony_ci                return false;
305bf215546Sopenharmony_ci
306bf215546Sopenharmony_ci        default:
307bf215546Sopenharmony_ci                return u_pipe_screen_get_param_defaults(pscreen, param);
308bf215546Sopenharmony_ci        }
309bf215546Sopenharmony_ci}
310bf215546Sopenharmony_ci
311bf215546Sopenharmony_cistatic float
312bf215546Sopenharmony_civ3d_screen_get_paramf(struct pipe_screen *pscreen, enum pipe_capf param)
313bf215546Sopenharmony_ci{
314bf215546Sopenharmony_ci        switch (param) {
315bf215546Sopenharmony_ci        case PIPE_CAPF_MIN_LINE_WIDTH:
316bf215546Sopenharmony_ci        case PIPE_CAPF_MIN_LINE_WIDTH_AA:
317bf215546Sopenharmony_ci        case PIPE_CAPF_MIN_POINT_SIZE:
318bf215546Sopenharmony_ci        case PIPE_CAPF_MIN_POINT_SIZE_AA:
319bf215546Sopenharmony_ci           return 1;
320bf215546Sopenharmony_ci
321bf215546Sopenharmony_ci        case PIPE_CAPF_POINT_SIZE_GRANULARITY:
322bf215546Sopenharmony_ci        case PIPE_CAPF_LINE_WIDTH_GRANULARITY:
323bf215546Sopenharmony_ci           return 0.1;
324bf215546Sopenharmony_ci
325bf215546Sopenharmony_ci        case PIPE_CAPF_MAX_LINE_WIDTH:
326bf215546Sopenharmony_ci        case PIPE_CAPF_MAX_LINE_WIDTH_AA:
327bf215546Sopenharmony_ci                return V3D_MAX_LINE_WIDTH;
328bf215546Sopenharmony_ci
329bf215546Sopenharmony_ci        case PIPE_CAPF_MAX_POINT_SIZE:
330bf215546Sopenharmony_ci        case PIPE_CAPF_MAX_POINT_SIZE_AA:
331bf215546Sopenharmony_ci                return V3D_MAX_POINT_SIZE;
332bf215546Sopenharmony_ci
333bf215546Sopenharmony_ci        case PIPE_CAPF_MAX_TEXTURE_ANISOTROPY:
334bf215546Sopenharmony_ci                return 16.0f;
335bf215546Sopenharmony_ci        case PIPE_CAPF_MAX_TEXTURE_LOD_BIAS:
336bf215546Sopenharmony_ci                return 16.0f;
337bf215546Sopenharmony_ci
338bf215546Sopenharmony_ci        case PIPE_CAPF_MIN_CONSERVATIVE_RASTER_DILATE:
339bf215546Sopenharmony_ci        case PIPE_CAPF_MAX_CONSERVATIVE_RASTER_DILATE:
340bf215546Sopenharmony_ci        case PIPE_CAPF_CONSERVATIVE_RASTER_DILATE_GRANULARITY:
341bf215546Sopenharmony_ci                return 0.0f;
342bf215546Sopenharmony_ci        default:
343bf215546Sopenharmony_ci                fprintf(stderr, "unknown paramf %d\n", param);
344bf215546Sopenharmony_ci                return 0;
345bf215546Sopenharmony_ci        }
346bf215546Sopenharmony_ci}
347bf215546Sopenharmony_ci
348bf215546Sopenharmony_cistatic int
349bf215546Sopenharmony_civ3d_screen_get_shader_param(struct pipe_screen *pscreen, unsigned shader,
350bf215546Sopenharmony_ci                           enum pipe_shader_cap param)
351bf215546Sopenharmony_ci{
352bf215546Sopenharmony_ci        struct v3d_screen *screen = v3d_screen(pscreen);
353bf215546Sopenharmony_ci
354bf215546Sopenharmony_ci        switch (shader) {
355bf215546Sopenharmony_ci        case PIPE_SHADER_VERTEX:
356bf215546Sopenharmony_ci        case PIPE_SHADER_FRAGMENT:
357bf215546Sopenharmony_ci                break;
358bf215546Sopenharmony_ci        case PIPE_SHADER_COMPUTE:
359bf215546Sopenharmony_ci                if (!screen->has_csd)
360bf215546Sopenharmony_ci                        return 0;
361bf215546Sopenharmony_ci                break;
362bf215546Sopenharmony_ci        case PIPE_SHADER_GEOMETRY:
363bf215546Sopenharmony_ci                if (screen->devinfo.ver < 41)
364bf215546Sopenharmony_ci                        return 0;
365bf215546Sopenharmony_ci                break;
366bf215546Sopenharmony_ci        default:
367bf215546Sopenharmony_ci                return 0;
368bf215546Sopenharmony_ci        }
369bf215546Sopenharmony_ci
370bf215546Sopenharmony_ci        /* this is probably not totally correct.. but it's a start: */
371bf215546Sopenharmony_ci        switch (param) {
372bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_MAX_INSTRUCTIONS:
373bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_MAX_ALU_INSTRUCTIONS:
374bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_MAX_TEX_INSTRUCTIONS:
375bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_MAX_TEX_INDIRECTIONS:
376bf215546Sopenharmony_ci                return 16384;
377bf215546Sopenharmony_ci
378bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH:
379bf215546Sopenharmony_ci                return UINT_MAX;
380bf215546Sopenharmony_ci
381bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_MAX_INPUTS:
382bf215546Sopenharmony_ci                switch (shader) {
383bf215546Sopenharmony_ci                case PIPE_SHADER_VERTEX:
384bf215546Sopenharmony_ci                        return V3D_MAX_VS_INPUTS / 4;
385bf215546Sopenharmony_ci                case PIPE_SHADER_GEOMETRY:
386bf215546Sopenharmony_ci                        return V3D_MAX_GS_INPUTS / 4;
387bf215546Sopenharmony_ci                case PIPE_SHADER_FRAGMENT:
388bf215546Sopenharmony_ci                        return V3D_MAX_FS_INPUTS / 4;
389bf215546Sopenharmony_ci                default:
390bf215546Sopenharmony_ci                        return 0;
391bf215546Sopenharmony_ci                };
392bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_MAX_OUTPUTS:
393bf215546Sopenharmony_ci                if (shader == PIPE_SHADER_FRAGMENT)
394bf215546Sopenharmony_ci                        return 4;
395bf215546Sopenharmony_ci                else
396bf215546Sopenharmony_ci                        return V3D_MAX_FS_INPUTS / 4;
397bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_MAX_TEMPS:
398bf215546Sopenharmony_ci                return 256; /* GL_MAX_PROGRAM_TEMPORARIES_ARB */
399bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_MAX_CONST_BUFFER0_SIZE:
400bf215546Sopenharmony_ci                /* Note: Limited by the offset size in
401bf215546Sopenharmony_ci                 * v3d_unit_data_create().
402bf215546Sopenharmony_ci                 */
403bf215546Sopenharmony_ci                return 16 * 1024 * sizeof(float);
404bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_MAX_CONST_BUFFERS:
405bf215546Sopenharmony_ci                return 16;
406bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_CONT_SUPPORTED:
407bf215546Sopenharmony_ci                return 0;
408bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_INDIRECT_INPUT_ADDR:
409bf215546Sopenharmony_ci                /* We don't currently support this in the backend, but that is
410bf215546Sopenharmony_ci                 * okay because our NIR compiler sets the option
411bf215546Sopenharmony_ci                 * lower_all_io_to_temps, which will eliminate indirect
412bf215546Sopenharmony_ci                 * indexing on all input/output variables by translating it to
413bf215546Sopenharmony_ci                 * indirect indexing on temporary variables instead, which we
414bf215546Sopenharmony_ci                 * will then lower to scratch. We prefer this over setting this
415bf215546Sopenharmony_ci                 * to 0, which would cause if-ladder injection to eliminate
416bf215546Sopenharmony_ci                 * indirect indexing on inputs.
417bf215546Sopenharmony_ci                 */
418bf215546Sopenharmony_ci                return 1;
419bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_INDIRECT_OUTPUT_ADDR:
420bf215546Sopenharmony_ci                return 1;
421bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_INDIRECT_TEMP_ADDR:
422bf215546Sopenharmony_ci                return 1;
423bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_INDIRECT_CONST_ADDR:
424bf215546Sopenharmony_ci                return 1;
425bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_SUBROUTINES:
426bf215546Sopenharmony_ci                return 0;
427bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_INTEGERS:
428bf215546Sopenharmony_ci                return 1;
429bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_FP16:
430bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_FP16_DERIVATIVES:
431bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_FP16_CONST_BUFFERS:
432bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_INT16:
433bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_GLSL_16BIT_CONSTS:
434bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_DROUND_SUPPORTED:
435bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_DFRACEXP_DLDEXP_SUPPORTED:
436bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_LDEXP_SUPPORTED:
437bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_TGSI_ANY_INOUT_DECL_RANGE:
438bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_TGSI_SQRT_SUPPORTED:
439bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTERS:
440bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_MAX_HW_ATOMIC_COUNTER_BUFFERS:
441bf215546Sopenharmony_ci                return 0;
442bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS:
443bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_MAX_SAMPLER_VIEWS:
444bf215546Sopenharmony_ci                return V3D_MAX_TEXTURE_SAMPLERS;
445bf215546Sopenharmony_ci
446bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_MAX_SHADER_BUFFERS:
447bf215546Sopenharmony_ci                if (screen->has_cache_flush) {
448bf215546Sopenharmony_ci                        if (shader == PIPE_SHADER_VERTEX ||
449bf215546Sopenharmony_ci                            shader == PIPE_SHADER_GEOMETRY) {
450bf215546Sopenharmony_ci                                return 0;
451bf215546Sopenharmony_ci                        }
452bf215546Sopenharmony_ci                        return PIPE_MAX_SHADER_BUFFERS;
453bf215546Sopenharmony_ci                 } else {
454bf215546Sopenharmony_ci                        return 0;
455bf215546Sopenharmony_ci                 }
456bf215546Sopenharmony_ci
457bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_MAX_SHADER_IMAGES:
458bf215546Sopenharmony_ci                if (screen->has_cache_flush) {
459bf215546Sopenharmony_ci                        if (screen->devinfo.ver < 41)
460bf215546Sopenharmony_ci                                return 0;
461bf215546Sopenharmony_ci                        else
462bf215546Sopenharmony_ci                                return PIPE_MAX_SHADER_IMAGES;
463bf215546Sopenharmony_ci                } else {
464bf215546Sopenharmony_ci                        return 0;
465bf215546Sopenharmony_ci                }
466bf215546Sopenharmony_ci
467bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_PREFERRED_IR:
468bf215546Sopenharmony_ci                return PIPE_SHADER_IR_NIR;
469bf215546Sopenharmony_ci        case PIPE_SHADER_CAP_SUPPORTED_IRS:
470bf215546Sopenharmony_ci                return 1 << PIPE_SHADER_IR_NIR;
471bf215546Sopenharmony_ci        default:
472bf215546Sopenharmony_ci                fprintf(stderr, "unknown shader param %d\n", param);
473bf215546Sopenharmony_ci                return 0;
474bf215546Sopenharmony_ci        }
475bf215546Sopenharmony_ci        return 0;
476bf215546Sopenharmony_ci}
477bf215546Sopenharmony_ci
478bf215546Sopenharmony_cistatic int
479bf215546Sopenharmony_civ3d_get_compute_param(struct pipe_screen *pscreen, enum pipe_shader_ir ir_type,
480bf215546Sopenharmony_ci                      enum pipe_compute_cap param, void *ret)
481bf215546Sopenharmony_ci{
482bf215546Sopenharmony_ci        struct v3d_screen *screen = v3d_screen(pscreen);
483bf215546Sopenharmony_ci
484bf215546Sopenharmony_ci        if (!screen->has_csd)
485bf215546Sopenharmony_ci                return 0;
486bf215546Sopenharmony_ci
487bf215546Sopenharmony_ci#define RET(x) do {                                     \
488bf215546Sopenharmony_ci                if (ret)                                \
489bf215546Sopenharmony_ci                        memcpy(ret, x, sizeof(x));      \
490bf215546Sopenharmony_ci                return sizeof(x);                       \
491bf215546Sopenharmony_ci        } while (0)
492bf215546Sopenharmony_ci
493bf215546Sopenharmony_ci        switch (param) {
494bf215546Sopenharmony_ci        case PIPE_COMPUTE_CAP_ADDRESS_BITS:
495bf215546Sopenharmony_ci                RET((uint32_t []) { 32 });
496bf215546Sopenharmony_ci                break;
497bf215546Sopenharmony_ci
498bf215546Sopenharmony_ci        case PIPE_COMPUTE_CAP_IR_TARGET:
499bf215546Sopenharmony_ci                sprintf(ret, "v3d");
500bf215546Sopenharmony_ci                return strlen(ret);
501bf215546Sopenharmony_ci
502bf215546Sopenharmony_ci        case PIPE_COMPUTE_CAP_GRID_DIMENSION:
503bf215546Sopenharmony_ci                RET((uint64_t []) { 3 });
504bf215546Sopenharmony_ci
505bf215546Sopenharmony_ci        case PIPE_COMPUTE_CAP_MAX_GRID_SIZE:
506bf215546Sopenharmony_ci                /* GL_MAX_COMPUTE_SHADER_WORK_GROUP_COUNT: The CSD has a
507bf215546Sopenharmony_ci                 * 16-bit field for the number of workgroups in each
508bf215546Sopenharmony_ci                 * dimension.
509bf215546Sopenharmony_ci                 */
510bf215546Sopenharmony_ci                RET(((uint64_t []) { 65535, 65535, 65535 }));
511bf215546Sopenharmony_ci
512bf215546Sopenharmony_ci        case PIPE_COMPUTE_CAP_MAX_BLOCK_SIZE:
513bf215546Sopenharmony_ci                /* GL_MAX_COMPUTE_WORK_GROUP_SIZE */
514bf215546Sopenharmony_ci                RET(((uint64_t []) { 256, 256, 256 }));
515bf215546Sopenharmony_ci
516bf215546Sopenharmony_ci        case PIPE_COMPUTE_CAP_MAX_THREADS_PER_BLOCK:
517bf215546Sopenharmony_ci        case PIPE_COMPUTE_CAP_MAX_VARIABLE_THREADS_PER_BLOCK:
518bf215546Sopenharmony_ci                /* GL_MAX_COMPUTE_WORK_GROUP_INVOCATIONS: This is
519bf215546Sopenharmony_ci                 * limited by WG_SIZE in the CSD.
520bf215546Sopenharmony_ci                 */
521bf215546Sopenharmony_ci                RET((uint64_t []) { 256 });
522bf215546Sopenharmony_ci
523bf215546Sopenharmony_ci        case PIPE_COMPUTE_CAP_MAX_GLOBAL_SIZE:
524bf215546Sopenharmony_ci                RET((uint64_t []) { 1024 * 1024 * 1024 });
525bf215546Sopenharmony_ci
526bf215546Sopenharmony_ci        case PIPE_COMPUTE_CAP_MAX_LOCAL_SIZE:
527bf215546Sopenharmony_ci                /* GL_MAX_COMPUTE_SHARED_MEMORY_SIZE */
528bf215546Sopenharmony_ci                RET((uint64_t []) { 32768 });
529bf215546Sopenharmony_ci
530bf215546Sopenharmony_ci        case PIPE_COMPUTE_CAP_MAX_PRIVATE_SIZE:
531bf215546Sopenharmony_ci        case PIPE_COMPUTE_CAP_MAX_INPUT_SIZE:
532bf215546Sopenharmony_ci                RET((uint64_t []) { 4096 });
533bf215546Sopenharmony_ci
534bf215546Sopenharmony_ci        case PIPE_COMPUTE_CAP_MAX_MEM_ALLOC_SIZE: {
535bf215546Sopenharmony_ci                struct sysinfo si;
536bf215546Sopenharmony_ci                sysinfo(&si);
537bf215546Sopenharmony_ci                RET((uint64_t []) { si.totalram });
538bf215546Sopenharmony_ci        }
539bf215546Sopenharmony_ci
540bf215546Sopenharmony_ci        case PIPE_COMPUTE_CAP_MAX_CLOCK_FREQUENCY:
541bf215546Sopenharmony_ci                /* OpenCL only */
542bf215546Sopenharmony_ci                RET((uint32_t []) { 0 });
543bf215546Sopenharmony_ci
544bf215546Sopenharmony_ci        case PIPE_COMPUTE_CAP_MAX_COMPUTE_UNITS:
545bf215546Sopenharmony_ci                RET((uint32_t []) { 1 });
546bf215546Sopenharmony_ci
547bf215546Sopenharmony_ci        case PIPE_COMPUTE_CAP_IMAGES_SUPPORTED:
548bf215546Sopenharmony_ci                RET((uint32_t []) { 1 });
549bf215546Sopenharmony_ci
550bf215546Sopenharmony_ci        case PIPE_COMPUTE_CAP_SUBGROUP_SIZE:
551bf215546Sopenharmony_ci                RET((uint32_t []) { 16 });
552bf215546Sopenharmony_ci
553bf215546Sopenharmony_ci        }
554bf215546Sopenharmony_ci
555bf215546Sopenharmony_ci        return 0;
556bf215546Sopenharmony_ci}
557bf215546Sopenharmony_ci
558bf215546Sopenharmony_cistatic bool
559bf215546Sopenharmony_civ3d_screen_is_format_supported(struct pipe_screen *pscreen,
560bf215546Sopenharmony_ci                               enum pipe_format format,
561bf215546Sopenharmony_ci                               enum pipe_texture_target target,
562bf215546Sopenharmony_ci                               unsigned sample_count,
563bf215546Sopenharmony_ci                               unsigned storage_sample_count,
564bf215546Sopenharmony_ci                               unsigned usage)
565bf215546Sopenharmony_ci{
566bf215546Sopenharmony_ci        struct v3d_screen *screen = v3d_screen(pscreen);
567bf215546Sopenharmony_ci
568bf215546Sopenharmony_ci        if (MAX2(1, sample_count) != MAX2(1, storage_sample_count))
569bf215546Sopenharmony_ci                return false;
570bf215546Sopenharmony_ci
571bf215546Sopenharmony_ci        if (sample_count > 1 && sample_count != V3D_MAX_SAMPLES)
572bf215546Sopenharmony_ci                return false;
573bf215546Sopenharmony_ci
574bf215546Sopenharmony_ci        if (target >= PIPE_MAX_TEXTURE_TYPES) {
575bf215546Sopenharmony_ci                return false;
576bf215546Sopenharmony_ci        }
577bf215546Sopenharmony_ci
578bf215546Sopenharmony_ci        if (usage & PIPE_BIND_VERTEX_BUFFER) {
579bf215546Sopenharmony_ci                switch (format) {
580bf215546Sopenharmony_ci                case PIPE_FORMAT_R32G32B32A32_FLOAT:
581bf215546Sopenharmony_ci                case PIPE_FORMAT_R32G32B32_FLOAT:
582bf215546Sopenharmony_ci                case PIPE_FORMAT_R32G32_FLOAT:
583bf215546Sopenharmony_ci                case PIPE_FORMAT_R32_FLOAT:
584bf215546Sopenharmony_ci                case PIPE_FORMAT_R32G32B32A32_SNORM:
585bf215546Sopenharmony_ci                case PIPE_FORMAT_R32G32B32_SNORM:
586bf215546Sopenharmony_ci                case PIPE_FORMAT_R32G32_SNORM:
587bf215546Sopenharmony_ci                case PIPE_FORMAT_R32_SNORM:
588bf215546Sopenharmony_ci                case PIPE_FORMAT_R32G32B32A32_SSCALED:
589bf215546Sopenharmony_ci                case PIPE_FORMAT_R32G32B32_SSCALED:
590bf215546Sopenharmony_ci                case PIPE_FORMAT_R32G32_SSCALED:
591bf215546Sopenharmony_ci                case PIPE_FORMAT_R32_SSCALED:
592bf215546Sopenharmony_ci                case PIPE_FORMAT_R16G16B16A16_UNORM:
593bf215546Sopenharmony_ci                case PIPE_FORMAT_R16G16B16_UNORM:
594bf215546Sopenharmony_ci                case PIPE_FORMAT_R16G16_UNORM:
595bf215546Sopenharmony_ci                case PIPE_FORMAT_R16_UNORM:
596bf215546Sopenharmony_ci                case PIPE_FORMAT_R16G16B16A16_SNORM:
597bf215546Sopenharmony_ci                case PIPE_FORMAT_R16G16B16_SNORM:
598bf215546Sopenharmony_ci                case PIPE_FORMAT_R16G16_SNORM:
599bf215546Sopenharmony_ci                case PIPE_FORMAT_R16_SNORM:
600bf215546Sopenharmony_ci                case PIPE_FORMAT_R16G16B16A16_USCALED:
601bf215546Sopenharmony_ci                case PIPE_FORMAT_R16G16B16_USCALED:
602bf215546Sopenharmony_ci                case PIPE_FORMAT_R16G16_USCALED:
603bf215546Sopenharmony_ci                case PIPE_FORMAT_R16_USCALED:
604bf215546Sopenharmony_ci                case PIPE_FORMAT_R16G16B16A16_SSCALED:
605bf215546Sopenharmony_ci                case PIPE_FORMAT_R16G16B16_SSCALED:
606bf215546Sopenharmony_ci                case PIPE_FORMAT_R16G16_SSCALED:
607bf215546Sopenharmony_ci                case PIPE_FORMAT_R16_SSCALED:
608bf215546Sopenharmony_ci                case PIPE_FORMAT_B8G8R8A8_UNORM:
609bf215546Sopenharmony_ci                case PIPE_FORMAT_R8G8B8A8_UNORM:
610bf215546Sopenharmony_ci                case PIPE_FORMAT_R8G8B8_UNORM:
611bf215546Sopenharmony_ci                case PIPE_FORMAT_R8G8_UNORM:
612bf215546Sopenharmony_ci                case PIPE_FORMAT_R8_UNORM:
613bf215546Sopenharmony_ci                case PIPE_FORMAT_R8G8B8A8_SNORM:
614bf215546Sopenharmony_ci                case PIPE_FORMAT_R8G8B8_SNORM:
615bf215546Sopenharmony_ci                case PIPE_FORMAT_R8G8_SNORM:
616bf215546Sopenharmony_ci                case PIPE_FORMAT_R8_SNORM:
617bf215546Sopenharmony_ci                case PIPE_FORMAT_R8G8B8A8_USCALED:
618bf215546Sopenharmony_ci                case PIPE_FORMAT_R8G8B8_USCALED:
619bf215546Sopenharmony_ci                case PIPE_FORMAT_R8G8_USCALED:
620bf215546Sopenharmony_ci                case PIPE_FORMAT_R8_USCALED:
621bf215546Sopenharmony_ci                case PIPE_FORMAT_R8G8B8A8_SSCALED:
622bf215546Sopenharmony_ci                case PIPE_FORMAT_R8G8B8_SSCALED:
623bf215546Sopenharmony_ci                case PIPE_FORMAT_R8G8_SSCALED:
624bf215546Sopenharmony_ci                case PIPE_FORMAT_R8_SSCALED:
625bf215546Sopenharmony_ci                case PIPE_FORMAT_R10G10B10A2_UNORM:
626bf215546Sopenharmony_ci                case PIPE_FORMAT_B10G10R10A2_UNORM:
627bf215546Sopenharmony_ci                case PIPE_FORMAT_R10G10B10A2_SNORM:
628bf215546Sopenharmony_ci                case PIPE_FORMAT_B10G10R10A2_SNORM:
629bf215546Sopenharmony_ci                case PIPE_FORMAT_R10G10B10A2_USCALED:
630bf215546Sopenharmony_ci                case PIPE_FORMAT_B10G10R10A2_USCALED:
631bf215546Sopenharmony_ci                case PIPE_FORMAT_R10G10B10A2_SSCALED:
632bf215546Sopenharmony_ci                case PIPE_FORMAT_B10G10R10A2_SSCALED:
633bf215546Sopenharmony_ci                        break;
634bf215546Sopenharmony_ci                default:
635bf215546Sopenharmony_ci                        return false;
636bf215546Sopenharmony_ci                }
637bf215546Sopenharmony_ci        }
638bf215546Sopenharmony_ci
639bf215546Sopenharmony_ci        /* FORMAT_NONE gets allowed for ARB_framebuffer_no_attachments's probe
640bf215546Sopenharmony_ci         * of FRAMEBUFFER_MAX_SAMPLES
641bf215546Sopenharmony_ci         */
642bf215546Sopenharmony_ci        if ((usage & PIPE_BIND_RENDER_TARGET) &&
643bf215546Sopenharmony_ci            format != PIPE_FORMAT_NONE &&
644bf215546Sopenharmony_ci            !v3d_rt_format_supported(&screen->devinfo, format)) {
645bf215546Sopenharmony_ci                return false;
646bf215546Sopenharmony_ci        }
647bf215546Sopenharmony_ci
648bf215546Sopenharmony_ci        if ((usage & PIPE_BIND_SAMPLER_VIEW) &&
649bf215546Sopenharmony_ci            !v3d_tex_format_supported(&screen->devinfo, format)) {
650bf215546Sopenharmony_ci                return false;
651bf215546Sopenharmony_ci        }
652bf215546Sopenharmony_ci
653bf215546Sopenharmony_ci        if ((usage & PIPE_BIND_DEPTH_STENCIL) &&
654bf215546Sopenharmony_ci            !(format == PIPE_FORMAT_S8_UINT_Z24_UNORM ||
655bf215546Sopenharmony_ci              format == PIPE_FORMAT_X8Z24_UNORM ||
656bf215546Sopenharmony_ci              format == PIPE_FORMAT_Z16_UNORM ||
657bf215546Sopenharmony_ci              format == PIPE_FORMAT_Z32_FLOAT ||
658bf215546Sopenharmony_ci              format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT)) {
659bf215546Sopenharmony_ci                return false;
660bf215546Sopenharmony_ci        }
661bf215546Sopenharmony_ci
662bf215546Sopenharmony_ci        if ((usage & PIPE_BIND_INDEX_BUFFER) &&
663bf215546Sopenharmony_ci            !(format == PIPE_FORMAT_R8_UINT ||
664bf215546Sopenharmony_ci              format == PIPE_FORMAT_R16_UINT ||
665bf215546Sopenharmony_ci              format == PIPE_FORMAT_R32_UINT)) {
666bf215546Sopenharmony_ci                return false;
667bf215546Sopenharmony_ci        }
668bf215546Sopenharmony_ci
669bf215546Sopenharmony_ci        if (usage & PIPE_BIND_SHADER_IMAGE) {
670bf215546Sopenharmony_ci                switch (format) {
671bf215546Sopenharmony_ci                /* FIXME: maybe we can implement a swizzle-on-writes to add
672bf215546Sopenharmony_ci                 * support for BGRA-alike formats.
673bf215546Sopenharmony_ci                 */
674bf215546Sopenharmony_ci                case PIPE_FORMAT_A4B4G4R4_UNORM:
675bf215546Sopenharmony_ci                case PIPE_FORMAT_A1B5G5R5_UNORM:
676bf215546Sopenharmony_ci                case PIPE_FORMAT_B5G6R5_UNORM:
677bf215546Sopenharmony_ci                case PIPE_FORMAT_B8G8R8A8_UNORM:
678bf215546Sopenharmony_ci                case PIPE_FORMAT_X8Z24_UNORM:
679bf215546Sopenharmony_ci                case PIPE_FORMAT_Z16_UNORM:
680bf215546Sopenharmony_ci                        return false;
681bf215546Sopenharmony_ci                default:
682bf215546Sopenharmony_ci                        return true;
683bf215546Sopenharmony_ci                }
684bf215546Sopenharmony_ci        }
685bf215546Sopenharmony_ci
686bf215546Sopenharmony_ci        return true;
687bf215546Sopenharmony_ci}
688bf215546Sopenharmony_ci
689bf215546Sopenharmony_cistatic const nir_shader_compiler_options v3d_nir_options = {
690bf215546Sopenharmony_ci        .lower_uadd_sat = true,
691bf215546Sopenharmony_ci        .lower_usub_sat = true,
692bf215546Sopenharmony_ci        .lower_iadd_sat = true,
693bf215546Sopenharmony_ci        .lower_all_io_to_temps = true,
694bf215546Sopenharmony_ci        .lower_extract_byte = true,
695bf215546Sopenharmony_ci        .lower_extract_word = true,
696bf215546Sopenharmony_ci        .lower_insert_byte = true,
697bf215546Sopenharmony_ci        .lower_insert_word = true,
698bf215546Sopenharmony_ci        .lower_bitfield_insert_to_shifts = true,
699bf215546Sopenharmony_ci        .lower_bitfield_extract_to_shifts = true,
700bf215546Sopenharmony_ci        .lower_bitfield_reverse = true,
701bf215546Sopenharmony_ci        .lower_bit_count = true,
702bf215546Sopenharmony_ci        .lower_cs_local_id_to_index = true,
703bf215546Sopenharmony_ci        .lower_ffract = true,
704bf215546Sopenharmony_ci        .lower_fmod = true,
705bf215546Sopenharmony_ci        .lower_pack_unorm_2x16 = true,
706bf215546Sopenharmony_ci        .lower_pack_snorm_2x16 = true,
707bf215546Sopenharmony_ci        .lower_pack_unorm_4x8 = true,
708bf215546Sopenharmony_ci        .lower_pack_snorm_4x8 = true,
709bf215546Sopenharmony_ci        .lower_unpack_unorm_4x8 = true,
710bf215546Sopenharmony_ci        .lower_unpack_snorm_4x8 = true,
711bf215546Sopenharmony_ci        .lower_pack_half_2x16 = true,
712bf215546Sopenharmony_ci        .lower_unpack_half_2x16 = true,
713bf215546Sopenharmony_ci        .lower_pack_32_2x16 = true,
714bf215546Sopenharmony_ci        .lower_pack_32_2x16_split = true,
715bf215546Sopenharmony_ci        .lower_unpack_32_2x16_split = true,
716bf215546Sopenharmony_ci        .lower_fdiv = true,
717bf215546Sopenharmony_ci        .lower_find_lsb = true,
718bf215546Sopenharmony_ci        .lower_ffma16 = true,
719bf215546Sopenharmony_ci        .lower_ffma32 = true,
720bf215546Sopenharmony_ci        .lower_ffma64 = true,
721bf215546Sopenharmony_ci        .lower_flrp32 = true,
722bf215546Sopenharmony_ci        .lower_fpow = true,
723bf215546Sopenharmony_ci        .lower_fsat = true,
724bf215546Sopenharmony_ci        .lower_fsqrt = true,
725bf215546Sopenharmony_ci        .lower_ifind_msb = true,
726bf215546Sopenharmony_ci        .lower_isign = true,
727bf215546Sopenharmony_ci        .lower_ldexp = true,
728bf215546Sopenharmony_ci        .lower_mul_high = true,
729bf215546Sopenharmony_ci        .lower_wpos_pntc = true,
730bf215546Sopenharmony_ci        .lower_rotate = true,
731bf215546Sopenharmony_ci        .lower_to_scalar = true,
732bf215546Sopenharmony_ci        .lower_int64_options = nir_lower_imul_2x32_64,
733bf215546Sopenharmony_ci        .has_fsub = true,
734bf215546Sopenharmony_ci        .has_isub = true,
735bf215546Sopenharmony_ci        .divergence_analysis_options =
736bf215546Sopenharmony_ci                nir_divergence_multiple_workgroup_per_compute_subgroup,
737bf215546Sopenharmony_ci        /* This will enable loop unrolling in the state tracker so we won't
738bf215546Sopenharmony_ci         * be able to selectively disable it in backend if it leads to
739bf215546Sopenharmony_ci         * lower thread counts or TMU spills. Choose a conservative maximum to
740bf215546Sopenharmony_ci         * limit register pressure impact.
741bf215546Sopenharmony_ci         */
742bf215546Sopenharmony_ci        .max_unroll_iterations = 16,
743bf215546Sopenharmony_ci        .force_indirect_unrolling_sampler = true,
744bf215546Sopenharmony_ci};
745bf215546Sopenharmony_ci
746bf215546Sopenharmony_cistatic const void *
747bf215546Sopenharmony_civ3d_screen_get_compiler_options(struct pipe_screen *pscreen,
748bf215546Sopenharmony_ci                                enum pipe_shader_ir ir, unsigned shader)
749bf215546Sopenharmony_ci{
750bf215546Sopenharmony_ci        return &v3d_nir_options;
751bf215546Sopenharmony_ci}
752bf215546Sopenharmony_ci
753bf215546Sopenharmony_cistatic const uint64_t v3d_available_modifiers[] = {
754bf215546Sopenharmony_ci   DRM_FORMAT_MOD_BROADCOM_UIF,
755bf215546Sopenharmony_ci   DRM_FORMAT_MOD_LINEAR,
756bf215546Sopenharmony_ci   DRM_FORMAT_MOD_BROADCOM_SAND128,
757bf215546Sopenharmony_ci};
758bf215546Sopenharmony_ci
759bf215546Sopenharmony_cistatic void
760bf215546Sopenharmony_civ3d_screen_query_dmabuf_modifiers(struct pipe_screen *pscreen,
761bf215546Sopenharmony_ci                                  enum pipe_format format, int max,
762bf215546Sopenharmony_ci                                  uint64_t *modifiers,
763bf215546Sopenharmony_ci                                  unsigned int *external_only,
764bf215546Sopenharmony_ci                                  int *count)
765bf215546Sopenharmony_ci{
766bf215546Sopenharmony_ci        int i;
767bf215546Sopenharmony_ci        int num_modifiers = ARRAY_SIZE(v3d_available_modifiers);
768bf215546Sopenharmony_ci
769bf215546Sopenharmony_ci        /* Expose DRM_FORMAT_MOD_BROADCOM_SAND128 only for PIPE_FORMAT_NV12 */
770bf215546Sopenharmony_ci        if (format != PIPE_FORMAT_NV12)
771bf215546Sopenharmony_ci                num_modifiers--;
772bf215546Sopenharmony_ci
773bf215546Sopenharmony_ci        if (!modifiers) {
774bf215546Sopenharmony_ci                *count = num_modifiers;
775bf215546Sopenharmony_ci                return;
776bf215546Sopenharmony_ci        }
777bf215546Sopenharmony_ci
778bf215546Sopenharmony_ci        *count = MIN2(max, num_modifiers);
779bf215546Sopenharmony_ci        for (i = 0; i < *count; i++) {
780bf215546Sopenharmony_ci                modifiers[i] = v3d_available_modifiers[i];
781bf215546Sopenharmony_ci                if (external_only)
782bf215546Sopenharmony_ci                        external_only[i] = util_format_is_yuv(format);
783bf215546Sopenharmony_ci        }
784bf215546Sopenharmony_ci}
785bf215546Sopenharmony_ci
786bf215546Sopenharmony_cistatic bool
787bf215546Sopenharmony_civ3d_screen_is_dmabuf_modifier_supported(struct pipe_screen *pscreen,
788bf215546Sopenharmony_ci                                        uint64_t modifier,
789bf215546Sopenharmony_ci                                        enum pipe_format format,
790bf215546Sopenharmony_ci                                        bool *external_only)
791bf215546Sopenharmony_ci{
792bf215546Sopenharmony_ci        int i;
793bf215546Sopenharmony_ci        bool is_sand_col128 = (format == PIPE_FORMAT_NV12) &&
794bf215546Sopenharmony_ci                (fourcc_mod_broadcom_mod(modifier) == DRM_FORMAT_MOD_BROADCOM_SAND128);
795bf215546Sopenharmony_ci
796bf215546Sopenharmony_ci        if (is_sand_col128) {
797bf215546Sopenharmony_ci                if (external_only)
798bf215546Sopenharmony_ci                        *external_only = true;
799bf215546Sopenharmony_ci                return true;
800bf215546Sopenharmony_ci        }
801bf215546Sopenharmony_ci
802bf215546Sopenharmony_ci        /* We don't want to generally allow DRM_FORMAT_MOD_BROADCOM_SAND128
803bf215546Sopenharmony_ci         * modifier, that is the last v3d_available_modifiers. We only accept
804bf215546Sopenharmony_ci         * it in the case of having a PIPE_FORMAT_NV12.
805bf215546Sopenharmony_ci         */
806bf215546Sopenharmony_ci        assert(v3d_available_modifiers[ARRAY_SIZE(v3d_available_modifiers) - 1] ==
807bf215546Sopenharmony_ci               DRM_FORMAT_MOD_BROADCOM_SAND128);
808bf215546Sopenharmony_ci        for (i = 0; i < ARRAY_SIZE(v3d_available_modifiers) - 1; i++) {
809bf215546Sopenharmony_ci                if (v3d_available_modifiers[i] == modifier) {
810bf215546Sopenharmony_ci                        if (external_only)
811bf215546Sopenharmony_ci                                *external_only = util_format_is_yuv(format);
812bf215546Sopenharmony_ci
813bf215546Sopenharmony_ci                        return true;
814bf215546Sopenharmony_ci                }
815bf215546Sopenharmony_ci        }
816bf215546Sopenharmony_ci
817bf215546Sopenharmony_ci        return false;
818bf215546Sopenharmony_ci}
819bf215546Sopenharmony_ci
820bf215546Sopenharmony_cistatic enum pipe_format
821bf215546Sopenharmony_civ3d_screen_get_compatible_tlb_format(struct pipe_screen *screen,
822bf215546Sopenharmony_ci                                     enum pipe_format format)
823bf215546Sopenharmony_ci{
824bf215546Sopenharmony_ci        switch (format) {
825bf215546Sopenharmony_ci        case PIPE_FORMAT_R16G16_UNORM:
826bf215546Sopenharmony_ci                return PIPE_FORMAT_R16G16_UINT;
827bf215546Sopenharmony_ci        default:
828bf215546Sopenharmony_ci                return format;
829bf215546Sopenharmony_ci        }
830bf215546Sopenharmony_ci}
831bf215546Sopenharmony_ci
832bf215546Sopenharmony_cistatic struct disk_cache *
833bf215546Sopenharmony_civ3d_screen_get_disk_shader_cache(struct pipe_screen *pscreen)
834bf215546Sopenharmony_ci{
835bf215546Sopenharmony_ci        struct v3d_screen *screen = v3d_screen(pscreen);
836bf215546Sopenharmony_ci
837bf215546Sopenharmony_ci        return screen->disk_cache;
838bf215546Sopenharmony_ci}
839bf215546Sopenharmony_ci
840bf215546Sopenharmony_cistruct pipe_screen *
841bf215546Sopenharmony_civ3d_screen_create(int fd, const struct pipe_screen_config *config,
842bf215546Sopenharmony_ci                  struct renderonly *ro)
843bf215546Sopenharmony_ci{
844bf215546Sopenharmony_ci        struct v3d_screen *screen = rzalloc(NULL, struct v3d_screen);
845bf215546Sopenharmony_ci        struct pipe_screen *pscreen;
846bf215546Sopenharmony_ci
847bf215546Sopenharmony_ci        pscreen = &screen->base;
848bf215546Sopenharmony_ci
849bf215546Sopenharmony_ci        pscreen->destroy = v3d_screen_destroy;
850bf215546Sopenharmony_ci        pscreen->get_param = v3d_screen_get_param;
851bf215546Sopenharmony_ci        pscreen->get_paramf = v3d_screen_get_paramf;
852bf215546Sopenharmony_ci        pscreen->get_shader_param = v3d_screen_get_shader_param;
853bf215546Sopenharmony_ci        pscreen->get_compute_param = v3d_get_compute_param;
854bf215546Sopenharmony_ci        pscreen->context_create = v3d_context_create;
855bf215546Sopenharmony_ci        pscreen->is_format_supported = v3d_screen_is_format_supported;
856bf215546Sopenharmony_ci        pscreen->get_canonical_format = v3d_screen_get_compatible_tlb_format;
857bf215546Sopenharmony_ci
858bf215546Sopenharmony_ci        screen->fd = fd;
859bf215546Sopenharmony_ci        screen->ro = ro;
860bf215546Sopenharmony_ci
861bf215546Sopenharmony_ci        list_inithead(&screen->bo_cache.time_list);
862bf215546Sopenharmony_ci        (void)mtx_init(&screen->bo_handles_mutex, mtx_plain);
863bf215546Sopenharmony_ci        screen->bo_handles = util_hash_table_create_ptr_keys();
864bf215546Sopenharmony_ci
865bf215546Sopenharmony_ci#if defined(USE_V3D_SIMULATOR)
866bf215546Sopenharmony_ci        screen->sim_file = v3d_simulator_init(screen->fd);
867bf215546Sopenharmony_ci#endif
868bf215546Sopenharmony_ci
869bf215546Sopenharmony_ci        if (!v3d_get_device_info(screen->fd, &screen->devinfo, &v3d_ioctl))
870bf215546Sopenharmony_ci                goto fail;
871bf215546Sopenharmony_ci
872bf215546Sopenharmony_ci        driParseConfigFiles(config->options, config->options_info, 0, "v3d",
873bf215546Sopenharmony_ci                            NULL, NULL, NULL, 0, NULL, 0);
874bf215546Sopenharmony_ci
875bf215546Sopenharmony_ci        /* We have to driCheckOption for the simulator mode to not assertion
876bf215546Sopenharmony_ci         * fail on not having our XML config.
877bf215546Sopenharmony_ci         */
878bf215546Sopenharmony_ci        const char *nonmsaa_name = "v3d_nonmsaa_texture_size_limit";
879bf215546Sopenharmony_ci        screen->nonmsaa_texture_size_limit =
880bf215546Sopenharmony_ci                driCheckOption(config->options, nonmsaa_name, DRI_BOOL) &&
881bf215546Sopenharmony_ci                driQueryOptionb(config->options, nonmsaa_name);
882bf215546Sopenharmony_ci
883bf215546Sopenharmony_ci        slab_create_parent(&screen->transfer_pool, sizeof(struct v3d_transfer), 16);
884bf215546Sopenharmony_ci
885bf215546Sopenharmony_ci        screen->has_csd = v3d_has_feature(screen, DRM_V3D_PARAM_SUPPORTS_CSD);
886bf215546Sopenharmony_ci        screen->has_cache_flush =
887bf215546Sopenharmony_ci                v3d_has_feature(screen, DRM_V3D_PARAM_SUPPORTS_CACHE_FLUSH);
888bf215546Sopenharmony_ci        screen->has_perfmon = v3d_has_feature(screen, DRM_V3D_PARAM_SUPPORTS_PERFMON);
889bf215546Sopenharmony_ci
890bf215546Sopenharmony_ci        v3d_fence_init(screen);
891bf215546Sopenharmony_ci
892bf215546Sopenharmony_ci        v3d_process_debug_variable();
893bf215546Sopenharmony_ci
894bf215546Sopenharmony_ci        v3d_resource_screen_init(pscreen);
895bf215546Sopenharmony_ci
896bf215546Sopenharmony_ci        screen->compiler = v3d_compiler_init(&screen->devinfo, 0);
897bf215546Sopenharmony_ci
898bf215546Sopenharmony_ci#ifdef ENABLE_SHADER_CACHE
899bf215546Sopenharmony_ci        v3d_disk_cache_init(screen);
900bf215546Sopenharmony_ci#endif
901bf215546Sopenharmony_ci
902bf215546Sopenharmony_ci        pscreen->get_name = v3d_screen_get_name;
903bf215546Sopenharmony_ci        pscreen->get_vendor = v3d_screen_get_vendor;
904bf215546Sopenharmony_ci        pscreen->get_device_vendor = v3d_screen_get_vendor;
905bf215546Sopenharmony_ci        pscreen->get_compiler_options = v3d_screen_get_compiler_options;
906bf215546Sopenharmony_ci        pscreen->get_disk_shader_cache = v3d_screen_get_disk_shader_cache;
907bf215546Sopenharmony_ci        pscreen->query_dmabuf_modifiers = v3d_screen_query_dmabuf_modifiers;
908bf215546Sopenharmony_ci        pscreen->is_dmabuf_modifier_supported =
909bf215546Sopenharmony_ci                v3d_screen_is_dmabuf_modifier_supported;
910bf215546Sopenharmony_ci
911bf215546Sopenharmony_ci        if (screen->has_perfmon) {
912bf215546Sopenharmony_ci                pscreen->get_driver_query_group_info = v3d_get_driver_query_group_info;
913bf215546Sopenharmony_ci                pscreen->get_driver_query_info = v3d_get_driver_query_info;
914bf215546Sopenharmony_ci        }
915bf215546Sopenharmony_ci
916bf215546Sopenharmony_ci        /* Generate the bitmask of supported draw primitives. */
917bf215546Sopenharmony_ci        screen->prim_types = BITFIELD_BIT(PIPE_PRIM_POINTS) |
918bf215546Sopenharmony_ci                             BITFIELD_BIT(PIPE_PRIM_LINES) |
919bf215546Sopenharmony_ci                             BITFIELD_BIT(PIPE_PRIM_LINE_LOOP) |
920bf215546Sopenharmony_ci                             BITFIELD_BIT(PIPE_PRIM_LINE_STRIP) |
921bf215546Sopenharmony_ci                             BITFIELD_BIT(PIPE_PRIM_TRIANGLES) |
922bf215546Sopenharmony_ci                             BITFIELD_BIT(PIPE_PRIM_TRIANGLE_STRIP) |
923bf215546Sopenharmony_ci                             BITFIELD_BIT(PIPE_PRIM_TRIANGLE_FAN) |
924bf215546Sopenharmony_ci                             BITFIELD_BIT(PIPE_PRIM_LINES_ADJACENCY) |
925bf215546Sopenharmony_ci                             BITFIELD_BIT(PIPE_PRIM_LINE_STRIP_ADJACENCY) |
926bf215546Sopenharmony_ci                             BITFIELD_BIT(PIPE_PRIM_TRIANGLES_ADJACENCY) |
927bf215546Sopenharmony_ci                             BITFIELD_BIT(PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY);
928bf215546Sopenharmony_ci
929bf215546Sopenharmony_ci        return pscreen;
930bf215546Sopenharmony_ci
931bf215546Sopenharmony_cifail:
932bf215546Sopenharmony_ci        close(fd);
933bf215546Sopenharmony_ci        ralloc_free(pscreen);
934bf215546Sopenharmony_ci        return NULL;
935bf215546Sopenharmony_ci}
936