• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright 2008 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 
29 #include "util/u_memory.h"
30 #include "util/u_math.h"
31 #include "util/u_cpu_detect.h"
32 #include "util/format/u_format.h"
33 #include "util/u_screen.h"
34 #include "util/u_string.h"
35 #include "util/format/u_format_s3tc.h"
36 #include "pipe/p_defines.h"
37 #include "pipe/p_screen.h"
38 #include "draw/draw_context.h"
39 #include "gallivm/lp_bld_type.h"
40 #include "gallivm/lp_bld_nir.h"
41 #include "util/disk_cache.h"
42 #include "util/os_misc.h"
43 #include "util/os_time.h"
44 #include "lp_texture.h"
45 #include "lp_fence.h"
46 #include "lp_jit.h"
47 #include "lp_screen.h"
48 #include "lp_context.h"
49 #include "lp_debug.h"
50 #include "lp_public.h"
51 #include "lp_limits.h"
52 #include "lp_rast.h"
53 #include "lp_cs_tpool.h"
54 
55 #include "frontend/sw_winsys.h"
56 
57 #include "nir.h"
58 
59 #ifdef DEBUG
60 int LP_DEBUG = 0;
61 
62 static const struct debug_named_value lp_debug_flags[] = {
63    { "pipe",   DEBUG_PIPE, NULL },
64    { "tgsi",   DEBUG_TGSI, NULL },
65    { "tex",    DEBUG_TEX, NULL },
66    { "setup",  DEBUG_SETUP, NULL },
67    { "rast",   DEBUG_RAST, NULL },
68    { "query",  DEBUG_QUERY, NULL },
69    { "screen", DEBUG_SCREEN, NULL },
70    { "counters", DEBUG_COUNTERS, NULL },
71    { "scene", DEBUG_SCENE, NULL },
72    { "fence", DEBUG_FENCE, NULL },
73    { "no_fastpath", DEBUG_NO_FASTPATH, NULL },
74    { "linear", DEBUG_LINEAR, NULL },
75    { "linear2", DEBUG_LINEAR2, NULL },
76    { "mem", DEBUG_MEM, NULL },
77    { "fs", DEBUG_FS, NULL },
78    { "cs", DEBUG_CS, NULL },
79    { "tgsi_ir", DEBUG_TGSI_IR, NULL },
80    { "cache_stats", DEBUG_CACHE_STATS, NULL },
81    { "accurate_a0", DEBUG_ACCURATE_A0 },
82    DEBUG_NAMED_VALUE_END
83 };
84 #endif
85 
86 int LP_PERF = 0;
87 static const struct debug_named_value lp_perf_flags[] = {
88    { "texmem",         PERF_TEX_MEM, NULL },
89    { "no_mipmap",      PERF_NO_MIPMAPS, NULL },
90    { "no_linear",      PERF_NO_LINEAR, NULL },
91    { "no_mip_linear",  PERF_NO_MIP_LINEAR, NULL },
92    { "no_tex",         PERF_NO_TEX, NULL },
93    { "no_blend",       PERF_NO_BLEND, NULL },
94    { "no_depth",       PERF_NO_DEPTH, NULL },
95    { "no_alphatest",   PERF_NO_ALPHATEST, NULL },
96    { "no_rast_linear", PERF_NO_RAST_LINEAR, NULL },
97    { "no_shade",       PERF_NO_SHADE, NULL },
98    DEBUG_NAMED_VALUE_END
99 };
100 
101 
102 static const char *
llvmpipe_get_vendor(struct pipe_screen * screen)103 llvmpipe_get_vendor(struct pipe_screen *screen)
104 {
105    return "Mesa/X.org";
106 }
107 
108 
109 static const char *
llvmpipe_get_name(struct pipe_screen * screen)110 llvmpipe_get_name(struct pipe_screen *screen)
111 {
112    struct llvmpipe_screen *lscreen = llvmpipe_screen(screen);
113    return lscreen->renderer_string;
114 }
115 
116 
117 static int
llvmpipe_get_param(struct pipe_screen * screen,enum pipe_cap param)118 llvmpipe_get_param(struct pipe_screen *screen, enum pipe_cap param)
119 {
120    switch (param) {
121    case PIPE_CAP_NPOT_TEXTURES:
122    case PIPE_CAP_MIXED_FRAMEBUFFER_SIZES:
123    case PIPE_CAP_MIXED_COLOR_DEPTH_BITS:
124    case PIPE_CAP_ANISOTROPIC_FILTER:
125       return 1;
126    case PIPE_CAP_FRAGMENT_SHADER_TEXTURE_LOD:
127    case PIPE_CAP_FRAGMENT_SHADER_DERIVATIVES:
128    case PIPE_CAP_VERTEX_SHADER_SATURATE:
129       return 1;
130    case PIPE_CAP_MAX_DUAL_SOURCE_RENDER_TARGETS:
131       return 1;
132    case PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS:
133       return PIPE_MAX_SO_BUFFERS;
134    case PIPE_CAP_POINT_SPRITE:
135       return 1;
136    case PIPE_CAP_MAX_RENDER_TARGETS:
137       return PIPE_MAX_COLOR_BUFS;
138    case PIPE_CAP_OCCLUSION_QUERY:
139    case PIPE_CAP_QUERY_TIMESTAMP:
140    case PIPE_CAP_QUERY_TIME_ELAPSED:
141       return 1;
142    case PIPE_CAP_QUERY_PIPELINE_STATISTICS:
143       return 1;
144    case PIPE_CAP_TEXTURE_MIRROR_CLAMP:
145    case PIPE_CAP_TEXTURE_MIRROR_CLAMP_TO_EDGE:
146       return 1;
147    case PIPE_CAP_TEXTURE_SWIZZLE:
148    case PIPE_CAP_TEXTURE_SHADOW_LOD:
149       return 1;
150    case PIPE_CAP_MAX_TEXTURE_2D_SIZE:
151       return 1 << (LP_MAX_TEXTURE_2D_LEVELS - 1);
152    case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
153       return LP_MAX_TEXTURE_3D_LEVELS;
154    case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
155       return LP_MAX_TEXTURE_CUBE_LEVELS;
156    case PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS:
157       return LP_MAX_TEXTURE_ARRAY_LAYERS;
158    case PIPE_CAP_BLEND_EQUATION_SEPARATE:
159       return 1;
160    case PIPE_CAP_INDEP_BLEND_ENABLE:
161       return 1;
162    case PIPE_CAP_INDEP_BLEND_FUNC:
163       return 1;
164    case PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT:
165    case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER:
166    case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER:
167       return 1;
168    case PIPE_CAP_PRIMITIVE_RESTART:
169    case PIPE_CAP_PRIMITIVE_RESTART_FIXED_INDEX:
170       return 1;
171    case PIPE_CAP_DEPTH_CLIP_DISABLE:
172       return 1;
173    case PIPE_CAP_DEPTH_CLAMP_ENABLE:
174       return 1;
175    case PIPE_CAP_SHADER_STENCIL_EXPORT:
176       return 1;
177    case PIPE_CAP_TGSI_INSTANCEID:
178    case PIPE_CAP_VERTEX_ELEMENT_INSTANCE_DIVISOR:
179    case PIPE_CAP_START_INSTANCE:
180    case PIPE_CAP_MIXED_COLORBUFFER_FORMATS:
181       return 1;
182    case PIPE_CAP_SEAMLESS_CUBE_MAP:
183    case PIPE_CAP_SEAMLESS_CUBE_MAP_PER_TEXTURE:
184       return 1;
185    /* this is a lie could support arbitrary large offsets */
186    case PIPE_CAP_MIN_TEXTURE_GATHER_OFFSET:
187    case PIPE_CAP_MIN_TEXEL_OFFSET:
188       return -32;
189    case PIPE_CAP_MAX_TEXTURE_GATHER_OFFSET:
190    case PIPE_CAP_MAX_TEXEL_OFFSET:
191       return 31;
192    case PIPE_CAP_CONDITIONAL_RENDER:
193    case PIPE_CAP_TEXTURE_BARRIER:
194       return 1;
195    case PIPE_CAP_MAX_STREAM_OUTPUT_SEPARATE_COMPONENTS:
196    case PIPE_CAP_MAX_STREAM_OUTPUT_INTERLEAVED_COMPONENTS:
197       return 16*4;
198    case PIPE_CAP_MAX_GEOMETRY_OUTPUT_VERTICES:
199    case PIPE_CAP_MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS:
200       return 1024;
201    case PIPE_CAP_MAX_VERTEX_STREAMS: {
202       struct llvmpipe_screen *lscreen = llvmpipe_screen(screen);
203       return lscreen->use_tgsi ? 1 : 4;
204    }
205    case PIPE_CAP_MAX_VERTEX_ATTRIB_STRIDE:
206       return 2048;
207    case PIPE_CAP_STREAM_OUTPUT_PAUSE_RESUME:
208    case PIPE_CAP_STREAM_OUTPUT_INTERLEAVE_BUFFERS:
209    case PIPE_CAP_VERTEX_COLOR_UNCLAMPED:
210    case PIPE_CAP_VERTEX_COLOR_CLAMPED:
211       return 1;
212    case PIPE_CAP_GLSL_FEATURE_LEVEL_COMPATIBILITY:
213    case PIPE_CAP_GLSL_FEATURE_LEVEL: {
214       struct llvmpipe_screen *lscreen = llvmpipe_screen(screen);
215       return lscreen->use_tgsi ? 330 : 450;
216    }
217    case PIPE_CAP_COMPUTE:
218       return GALLIVM_HAVE_CORO;
219    case PIPE_CAP_USER_VERTEX_BUFFERS:
220       return 1;
221    case PIPE_CAP_TGSI_TEXCOORD:
222    case PIPE_CAP_DRAW_INDIRECT:
223       return 1;
224 
225    case PIPE_CAP_CUBE_MAP_ARRAY:
226       return 1;
227    case PIPE_CAP_CONSTANT_BUFFER_OFFSET_ALIGNMENT:
228       return 16;
229    case PIPE_CAP_MIN_MAP_BUFFER_ALIGNMENT:
230       return 64;
231    case PIPE_CAP_TEXTURE_BUFFER_OBJECTS:
232       return 1;
233    /* Adressing that many 64bpp texels fits in an i32 so this is a reasonable value */
234    case PIPE_CAP_MAX_TEXTURE_BUFFER_SIZE:
235       return 134217728;
236    case PIPE_CAP_TEXTURE_BUFFER_OFFSET_ALIGNMENT:
237       return 16;
238    case PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER:
239       return 0;
240    case PIPE_CAP_MAX_VIEWPORTS:
241       return PIPE_MAX_VIEWPORTS;
242    case PIPE_CAP_ENDIANNESS:
243       return PIPE_ENDIAN_NATIVE;
244    case PIPE_CAP_TGSI_TES_LAYER_VIEWPORT:
245    case PIPE_CAP_TGSI_VS_LAYER_VIEWPORT:
246       return 1;
247    case PIPE_CAP_BUFFER_MAP_PERSISTENT_COHERENT:
248       return 1;
249    case PIPE_CAP_MAX_TEXTURE_GATHER_COMPONENTS:
250       return 4;
251    case PIPE_CAP_TGSI_VS_WINDOW_SPACE_POSITION:
252       return 1;
253    case PIPE_CAP_TGSI_FS_FINE_DERIVATIVE:
254       return 1;
255    case PIPE_CAP_TGSI_TEX_TXF_LZ:
256    case PIPE_CAP_SAMPLER_VIEW_TARGET:
257       return 1;
258    case PIPE_CAP_FAKE_SW_MSAA: {
259       struct llvmpipe_screen *lscreen = llvmpipe_screen(screen);
260       return lscreen->use_tgsi ? 1 : 0;
261    }
262    case PIPE_CAP_TEXTURE_QUERY_LOD:
263    case PIPE_CAP_CONDITIONAL_RENDER_INVERTED:
264    case PIPE_CAP_TGSI_ARRAY_COMPONENTS:
265    case PIPE_CAP_DOUBLES:
266    case PIPE_CAP_INT64:
267    case PIPE_CAP_INT64_DIVMOD:
268    case PIPE_CAP_QUERY_SO_OVERFLOW:
269    case PIPE_CAP_TGSI_DIV:
270       return 1;
271 
272    case PIPE_CAP_VENDOR_ID:
273       return 0xFFFFFFFF;
274    case PIPE_CAP_DEVICE_ID:
275       return 0xFFFFFFFF;
276    case PIPE_CAP_ACCELERATED:
277       return 0;
278    case PIPE_CAP_VIDEO_MEMORY: {
279       /* XXX: Do we want to return the full amount fo system memory ? */
280       uint64_t system_memory;
281 
282       if (!os_get_total_physical_memory(&system_memory))
283          return 0;
284 
285       if (sizeof(void *) == 4)
286          /* Cap to 2 GB on 32 bits system. We do this because llvmpipe does
287           * eat application memory, which is quite limited on 32 bits. App
288           * shouldn't expect too much available memory. */
289          system_memory = MIN2(system_memory, 2048 << 20);
290 
291       return (int)(system_memory >> 20);
292    }
293    case PIPE_CAP_UMA:
294       return 0;
295    case PIPE_CAP_CLIP_HALFZ:
296       return 1;
297    case PIPE_CAP_POLYGON_OFFSET_CLAMP:
298    case PIPE_CAP_TEXTURE_FLOAT_LINEAR:
299    case PIPE_CAP_TEXTURE_HALF_FLOAT_LINEAR:
300       return 1;
301    case PIPE_CAP_CULL_DISTANCE:
302       return 1;
303    case PIPE_CAP_COPY_BETWEEN_COMPRESSED_AND_PLAIN_FORMATS:
304       return 1;
305    case PIPE_CAP_CLEAR_TEXTURE:
306       return 1;
307    case PIPE_CAP_MAX_VARYINGS:
308       return 32;
309    case PIPE_CAP_SHADER_BUFFER_OFFSET_ALIGNMENT:
310       return 16;
311    case PIPE_CAP_QUERY_BUFFER_OBJECT:
312       return 1;
313    case PIPE_CAP_DRAW_PARAMETERS:
314       return 1;
315    case PIPE_CAP_FBFETCH:
316       return 8;
317    case PIPE_CAP_FBFETCH_COHERENT:
318       return 0;
319    case PIPE_CAP_MULTI_DRAW_INDIRECT:
320    case PIPE_CAP_MULTI_DRAW_INDIRECT_PARAMS:
321       return 1;
322    case PIPE_CAP_DEVICE_RESET_STATUS_QUERY:
323    case PIPE_CAP_ROBUST_BUFFER_ACCESS_BEHAVIOR:
324       return 1;
325    case PIPE_CAP_MAX_SHADER_PATCH_VARYINGS:
326       return 32;
327    case PIPE_CAP_RASTERIZER_SUBPIXEL_BITS:
328       return 8;
329    case PIPE_CAP_PCI_GROUP:
330    case PIPE_CAP_PCI_BUS:
331    case PIPE_CAP_PCI_DEVICE:
332    case PIPE_CAP_PCI_FUNCTION:
333    case PIPE_CAP_GLSL_OPTIMIZE_CONSERVATIVELY:
334    case PIPE_CAP_ALLOW_MAPPED_BUFFERS_DURING_EXECUTION:
335       return 0;
336 
337    case PIPE_CAP_SHAREABLE_SHADERS:
338       /* Can't expose shareable shaders because the draw shaders reference the
339        * draw module's state, which is per-context.
340        */
341       return 0;
342 
343    case PIPE_CAP_MAX_GS_INVOCATIONS:
344       return 32;
345    case PIPE_CAP_MAX_SHADER_BUFFER_SIZE:
346       return LP_MAX_TGSI_SHADER_BUFFER_SIZE;
347    case PIPE_CAP_FRAMEBUFFER_NO_ATTACHMENT:
348    case PIPE_CAP_TGSI_TG4_COMPONENT_IN_SWIZZLE:
349    case PIPE_CAP_TGSI_FS_FACE_IS_INTEGER_SYSVAL:
350    case PIPE_CAP_RESOURCE_FROM_USER_MEMORY:
351       return 1;
352 #ifdef PIPE_MEMORY_FD
353    case PIPE_CAP_MEMOBJ:
354       return 1;
355 #endif
356    case PIPE_CAP_SAMPLER_REDUCTION_MINMAX:
357    case PIPE_CAP_TGSI_TXQS:
358    case PIPE_CAP_TGSI_VOTE:
359    case PIPE_CAP_LOAD_CONSTBUF:
360    case PIPE_CAP_TEXTURE_MULTISAMPLE:
361    case PIPE_CAP_SAMPLE_SHADING:
362    case PIPE_CAP_GL_SPIRV:
363    case PIPE_CAP_POST_DEPTH_COVERAGE:
364    case PIPE_CAP_PACKED_UNIFORMS: {
365       struct llvmpipe_screen *lscreen = llvmpipe_screen(screen);
366       return !lscreen->use_tgsi;
367    }
368    default:
369       return u_pipe_screen_get_param_defaults(screen, param);
370    }
371 }
372 
373 static int
llvmpipe_get_shader_param(struct pipe_screen * screen,enum pipe_shader_type shader,enum pipe_shader_cap param)374 llvmpipe_get_shader_param(struct pipe_screen *screen,
375                           enum pipe_shader_type shader,
376                           enum pipe_shader_cap param)
377 {
378    struct llvmpipe_screen *lscreen = llvmpipe_screen(screen);
379    switch(shader)
380    {
381    case PIPE_SHADER_COMPUTE:
382       if ((lscreen->allow_cl) && param == PIPE_SHADER_CAP_SUPPORTED_IRS)
383          return (1 << PIPE_SHADER_IR_TGSI) | (1 << PIPE_SHADER_IR_NIR) | (1 << PIPE_SHADER_IR_NIR_SERIALIZED);
384       FALLTHROUGH;
385    case PIPE_SHADER_FRAGMENT:
386       if (param == PIPE_SHADER_CAP_PREFERRED_IR) {
387          if (lscreen->use_tgsi)
388             return PIPE_SHADER_IR_TGSI;
389          else
390             return PIPE_SHADER_IR_NIR;
391       }
392 
393       return gallivm_get_shader_param(param);
394    case PIPE_SHADER_TESS_CTRL:
395    case PIPE_SHADER_TESS_EVAL:
396       /* Tessellation shader needs llvm coroutines support */
397       if (!GALLIVM_HAVE_CORO || lscreen->use_tgsi)
398          return 0;
399       FALLTHROUGH;
400    case PIPE_SHADER_VERTEX:
401    case PIPE_SHADER_GEOMETRY:
402       if (param == PIPE_SHADER_CAP_PREFERRED_IR) {
403          if (lscreen->use_tgsi)
404             return PIPE_SHADER_IR_TGSI;
405          else
406             return PIPE_SHADER_IR_NIR;
407       }
408 
409       switch (param) {
410       case PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS:
411          /* At this time, the draw module and llvmpipe driver only
412           * support vertex shader texture lookups when LLVM is enabled in
413           * the draw module.
414           */
415          if (debug_get_bool_option("DRAW_USE_LLVM", TRUE))
416             return PIPE_MAX_SAMPLERS;
417          else
418             return 0;
419       case PIPE_SHADER_CAP_MAX_SAMPLER_VIEWS:
420          if (debug_get_bool_option("DRAW_USE_LLVM", TRUE))
421             return PIPE_MAX_SHADER_SAMPLER_VIEWS;
422          else
423             return 0;
424       default:
425          return draw_get_shader_param(shader, param);
426       }
427    default:
428       return 0;
429    }
430 }
431 
432 static float
llvmpipe_get_paramf(struct pipe_screen * screen,enum pipe_capf param)433 llvmpipe_get_paramf(struct pipe_screen *screen, enum pipe_capf param)
434 {
435    switch (param) {
436    case PIPE_CAPF_MAX_LINE_WIDTH:
437       FALLTHROUGH;
438    case PIPE_CAPF_MAX_LINE_WIDTH_AA:
439       return 255.0; /* arbitrary */
440    case PIPE_CAPF_MAX_POINT_WIDTH:
441       FALLTHROUGH;
442    case PIPE_CAPF_MAX_POINT_WIDTH_AA:
443       return LP_MAX_POINT_WIDTH; /* arbitrary */
444    case PIPE_CAPF_MAX_TEXTURE_ANISOTROPY:
445       return 16.0; /* not actually signficant at this time */
446    case PIPE_CAPF_MAX_TEXTURE_LOD_BIAS:
447       return 16.0; /* arbitrary */
448    case PIPE_CAPF_MIN_CONSERVATIVE_RASTER_DILATE:
449       return 0.0;
450    case PIPE_CAPF_MAX_CONSERVATIVE_RASTER_DILATE:
451       return 0.0;
452    case PIPE_CAPF_CONSERVATIVE_RASTER_DILATE_GRANULARITY:
453       return 0.0;
454    }
455    /* should only get here on unhandled cases */
456    debug_printf("Unexpected PIPE_CAP %d query\n", param);
457    return 0.0;
458 }
459 
460 static int
llvmpipe_get_compute_param(struct pipe_screen * _screen,enum pipe_shader_ir ir_type,enum pipe_compute_cap param,void * ret)461 llvmpipe_get_compute_param(struct pipe_screen *_screen,
462                            enum pipe_shader_ir ir_type,
463                            enum pipe_compute_cap param,
464                            void *ret)
465 {
466    switch (param) {
467    case PIPE_COMPUTE_CAP_IR_TARGET:
468       return 0;
469    case PIPE_COMPUTE_CAP_MAX_GRID_SIZE:
470       if (ret) {
471          uint64_t *grid_size = ret;
472          grid_size[0] = 65535;
473          grid_size[1] = 65535;
474          grid_size[2] = 65535;
475       }
476       return 3 * sizeof(uint64_t) ;
477    case PIPE_COMPUTE_CAP_MAX_BLOCK_SIZE:
478       if (ret) {
479          uint64_t *block_size = ret;
480          block_size[0] = 1024;
481          block_size[1] = 1024;
482          block_size[2] = 1024;
483       }
484       return 3 * sizeof(uint64_t);
485    case PIPE_COMPUTE_CAP_MAX_THREADS_PER_BLOCK:
486       if (ret) {
487          uint64_t *max_threads_per_block = ret;
488          *max_threads_per_block = 1024;
489       }
490       return sizeof(uint64_t);
491    case PIPE_COMPUTE_CAP_MAX_LOCAL_SIZE:
492       if (ret) {
493          uint64_t *max_local_size = ret;
494          *max_local_size = 32768;
495       }
496       return sizeof(uint64_t);
497    case PIPE_COMPUTE_CAP_GRID_DIMENSION:
498       if (ret) {
499          uint32_t *grid_dim = ret;
500          *grid_dim = 3;
501       }
502       return sizeof(uint32_t);
503    case PIPE_COMPUTE_CAP_MAX_GLOBAL_SIZE:
504       if (ret) {
505          uint64_t *max_global_size = ret;
506          *max_global_size = (1ULL << 31);
507       }
508       return sizeof(uint64_t);
509    case PIPE_COMPUTE_CAP_MAX_MEM_ALLOC_SIZE:
510       if (ret) {
511          uint64_t *max_mem_alloc_size = ret;
512          *max_mem_alloc_size = (1ULL << 31);
513       }
514       return sizeof(uint64_t);
515    case PIPE_COMPUTE_CAP_MAX_PRIVATE_SIZE:
516       if (ret) {
517          uint64_t *max_private = ret;
518          *max_private = (1UL << 31);
519       }
520       return sizeof(uint64_t);
521    case PIPE_COMPUTE_CAP_MAX_INPUT_SIZE:
522       if (ret) {
523          uint64_t *max_input = ret;
524          *max_input = 1576;
525       }
526       return sizeof(uint64_t);
527    case PIPE_COMPUTE_CAP_IMAGES_SUPPORTED:
528       if (ret) {
529          uint32_t *images = ret;
530          *images = LP_MAX_TGSI_SHADER_IMAGES;
531       }
532       return sizeof(uint32_t);
533    case PIPE_COMPUTE_CAP_MAX_VARIABLE_THREADS_PER_BLOCK:
534       return 0;
535    case PIPE_COMPUTE_CAP_SUBGROUP_SIZE:
536       if (ret) {
537          uint32_t *subgroup_size = ret;
538          *subgroup_size = 32;
539       }
540       return sizeof(uint32_t);
541    case PIPE_COMPUTE_CAP_MAX_COMPUTE_UNITS:
542       if (ret) {
543          uint32_t *max_compute_units = ret;
544          *max_compute_units = 8;
545       }
546       return sizeof(uint32_t);
547    case PIPE_COMPUTE_CAP_MAX_CLOCK_FREQUENCY:
548       if (ret) {
549          uint32_t *max_clock_freq = ret;
550          *max_clock_freq = 300;
551       }
552       return sizeof(uint32_t);
553    case PIPE_COMPUTE_CAP_ADDRESS_BITS:
554       if (ret) {
555          uint32_t *address_bits = ret;
556          *address_bits = 64;
557       }
558       return sizeof(uint32_t);
559    }
560    return 0;
561 }
562 
563 static const struct nir_shader_compiler_options gallivm_nir_options = {
564    .lower_scmp = true,
565    .lower_flrp32 = true,
566    .lower_flrp64 = true,
567    .lower_fsat = true,
568    .lower_bitfield_insert_to_shifts = true,
569    .lower_bitfield_extract_to_shifts = true,
570    .lower_fdot = true,
571    .lower_fdph = true,
572    .lower_ffma16 = true,
573    .lower_ffma32 = true,
574    .lower_ffma64 = true,
575    .lower_flrp16 = true,
576    .lower_fmod = true,
577    .lower_hadd = true,
578    .lower_uadd_sat = true,
579    .lower_iadd_sat = true,
580    .lower_ldexp = true,
581    .lower_pack_snorm_2x16 = true,
582    .lower_pack_snorm_4x8 = true,
583    .lower_pack_unorm_2x16 = true,
584    .lower_pack_unorm_4x8 = true,
585    .lower_pack_half_2x16 = true,
586    .lower_pack_split = true,
587    .lower_unpack_snorm_2x16 = true,
588    .lower_unpack_snorm_4x8 = true,
589    .lower_unpack_unorm_2x16 = true,
590    .lower_unpack_unorm_4x8 = true,
591    .lower_unpack_half_2x16 = true,
592    .lower_extract_byte = true,
593    .lower_extract_word = true,
594    .lower_insert_byte = true,
595    .lower_insert_word = true,
596    .lower_rotate = true,
597    .lower_uadd_carry = true,
598    .lower_usub_borrow = true,
599    .lower_mul_2x32_64 = true,
600    .lower_ifind_msb = true,
601    .max_unroll_iterations = 32,
602    .use_interpolated_input_intrinsics = true,
603    .lower_to_scalar = true,
604    .lower_uniforms_to_ubo = true,
605    .lower_vector_cmp = true,
606    .lower_device_index_to_zero = true,
607    .support_16bit_alu = true,
608    .lower_fisnormal = true,
609 };
610 
611 static char *
llvmpipe_finalize_nir(struct pipe_screen * screen,void * nirptr)612 llvmpipe_finalize_nir(struct pipe_screen *screen,
613                       void *nirptr)
614 {
615    struct nir_shader *nir = (struct nir_shader *)nirptr;
616    lp_build_opt_nir(nir);
617    return NULL;
618 }
619 
620 static inline const void *
llvmpipe_get_compiler_options(struct pipe_screen * screen,enum pipe_shader_ir ir,enum pipe_shader_type shader)621 llvmpipe_get_compiler_options(struct pipe_screen *screen,
622                               enum pipe_shader_ir ir,
623                               enum pipe_shader_type shader)
624 {
625    assert(ir == PIPE_SHADER_IR_NIR);
626    return &gallivm_nir_options;
627 }
628 
629 /**
630  * Query format support for creating a texture, drawing surface, etc.
631  * \param format  the format to test
632  * \param type  one of PIPE_TEXTURE, PIPE_SURFACE
633  */
634 static bool
llvmpipe_is_format_supported(struct pipe_screen * _screen,enum pipe_format format,enum pipe_texture_target target,unsigned sample_count,unsigned storage_sample_count,unsigned bind)635 llvmpipe_is_format_supported( struct pipe_screen *_screen,
636                               enum pipe_format format,
637                               enum pipe_texture_target target,
638                               unsigned sample_count,
639                               unsigned storage_sample_count,
640                               unsigned bind)
641 {
642    struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
643    struct sw_winsys *winsys = screen->winsys;
644    const struct util_format_description *format_desc;
645 
646    format_desc = util_format_description(format);
647    if (!format_desc)
648       return false;
649 
650    assert(target == PIPE_BUFFER ||
651           target == PIPE_TEXTURE_1D ||
652           target == PIPE_TEXTURE_1D_ARRAY ||
653           target == PIPE_TEXTURE_2D ||
654           target == PIPE_TEXTURE_2D_ARRAY ||
655           target == PIPE_TEXTURE_RECT ||
656           target == PIPE_TEXTURE_3D ||
657           target == PIPE_TEXTURE_CUBE ||
658           target == PIPE_TEXTURE_CUBE_ARRAY);
659 
660    if (sample_count != 0 && sample_count != 1 && sample_count != 4)
661       return false;
662 
663    if (MAX2(1, sample_count) != MAX2(1, storage_sample_count))
664       return false;
665 
666    if (bind & (PIPE_BIND_RENDER_TARGET | PIPE_BIND_SHADER_IMAGE)) {
667       if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
668          /* this is a lie actually other formats COULD exist where we would fail */
669          if (format_desc->nr_channels < 3)
670             return false;
671       }
672       else if (format_desc->colorspace != UTIL_FORMAT_COLORSPACE_RGB)
673          return false;
674 
675       if (format_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN &&
676           format != PIPE_FORMAT_R11G11B10_FLOAT)
677          return false;
678 
679       assert(format_desc->block.width == 1);
680       assert(format_desc->block.height == 1);
681 
682       if (format_desc->is_mixed)
683          return false;
684 
685       if (!format_desc->is_array && !format_desc->is_bitmask &&
686           format != PIPE_FORMAT_R11G11B10_FLOAT)
687          return false;
688    }
689 
690    if (bind & PIPE_BIND_SHADER_IMAGE) {
691       switch (format) {
692          case PIPE_FORMAT_R32G32B32A32_FLOAT:
693          case PIPE_FORMAT_R16G16B16A16_FLOAT:
694          case PIPE_FORMAT_R32G32_FLOAT:
695          case PIPE_FORMAT_R16G16_FLOAT:
696          case PIPE_FORMAT_R11G11B10_FLOAT:
697          case PIPE_FORMAT_R32_FLOAT:
698          case PIPE_FORMAT_R16_FLOAT:
699          case PIPE_FORMAT_R32G32B32A32_UINT:
700          case PIPE_FORMAT_R16G16B16A16_UINT:
701          case PIPE_FORMAT_R10G10B10A2_UINT:
702          case PIPE_FORMAT_R8G8B8A8_UINT:
703          case PIPE_FORMAT_R32G32_UINT:
704          case PIPE_FORMAT_R16G16_UINT:
705          case PIPE_FORMAT_R8G8_UINT:
706          case PIPE_FORMAT_R32_UINT:
707          case PIPE_FORMAT_R16_UINT:
708          case PIPE_FORMAT_R8_UINT:
709          case PIPE_FORMAT_R32G32B32A32_SINT:
710          case PIPE_FORMAT_R16G16B16A16_SINT:
711          case PIPE_FORMAT_R8G8B8A8_SINT:
712          case PIPE_FORMAT_R32G32_SINT:
713          case PIPE_FORMAT_R16G16_SINT:
714          case PIPE_FORMAT_R8G8_SINT:
715          case PIPE_FORMAT_R32_SINT:
716          case PIPE_FORMAT_R16_SINT:
717          case PIPE_FORMAT_R8_SINT:
718          case PIPE_FORMAT_R16G16B16A16_UNORM:
719          case PIPE_FORMAT_R10G10B10A2_UNORM:
720          case PIPE_FORMAT_R8G8B8A8_UNORM:
721          case PIPE_FORMAT_R16G16_UNORM:
722          case PIPE_FORMAT_R8G8_UNORM:
723          case PIPE_FORMAT_R16_UNORM:
724          case PIPE_FORMAT_R8_UNORM:
725          case PIPE_FORMAT_R16G16B16A16_SNORM:
726          case PIPE_FORMAT_R8G8B8A8_SNORM:
727          case PIPE_FORMAT_R16G16_SNORM:
728          case PIPE_FORMAT_R8G8_SNORM:
729          case PIPE_FORMAT_R16_SNORM:
730          case PIPE_FORMAT_R8_SNORM:
731             break;
732 
733          default:
734             return false;
735       }
736    }
737 
738    if ((bind & (PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW)) &&
739        ((bind & PIPE_BIND_DISPLAY_TARGET) == 0)) {
740       /* Disable all 3-channel formats, where channel size != 32 bits.
741        * In some cases we run into crashes (in generate_unswizzled_blend()),
742        * for 3-channel RGB16 variants, there was an apparent LLVM bug.
743        * In any case, disabling the shallower 3-channel formats avoids a
744        * number of issues with GL_ARB_copy_image support.
745        */
746       if (format_desc->is_array &&
747           format_desc->nr_channels == 3 &&
748           format_desc->block.bits != 96) {
749          return false;
750       }
751 
752       /* Disable 64-bit integer formats for RT/samplers.
753        * VK CTS crashes with these and they don't make much sense.
754        */
755       int c = util_format_get_first_non_void_channel(format_desc->format);
756       if (c >= 0) {
757          if (format_desc->channel[c].pure_integer && format_desc->channel[c].size == 64)
758             return false;
759       }
760 
761    }
762 
763    if (!(bind & PIPE_BIND_VERTEX_BUFFER) &&
764        util_format_is_scaled(format))
765       return false;
766 
767    if (bind & PIPE_BIND_DISPLAY_TARGET) {
768       if(!winsys->is_displaytarget_format_supported(winsys, bind, format))
769          return false;
770    }
771 
772    if (bind & PIPE_BIND_DEPTH_STENCIL) {
773       if (format_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
774          return false;
775 
776       if (format_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS)
777          return false;
778    }
779 
780    if (format_desc->layout == UTIL_FORMAT_LAYOUT_ASTC ||
781        format_desc->layout == UTIL_FORMAT_LAYOUT_ATC) {
782       /* Software decoding is not hooked up. */
783       return false;
784    }
785 
786    if (format_desc->layout == UTIL_FORMAT_LAYOUT_ETC &&
787        format != PIPE_FORMAT_ETC1_RGB8)
788       return false;
789 
790    /*
791     * Everything can be supported by u_format
792     * (those without fetch_rgba_float might be not but shouldn't hit that)
793     */
794 
795    return true;
796 }
797 
798 
799 
800 
801 static void
llvmpipe_flush_frontbuffer(struct pipe_screen * _screen,struct pipe_context * _pipe,struct pipe_resource * resource,unsigned level,unsigned layer,void * context_private,struct pipe_box * sub_box)802 llvmpipe_flush_frontbuffer(struct pipe_screen *_screen,
803                            struct pipe_context *_pipe,
804                            struct pipe_resource *resource,
805                            unsigned level, unsigned layer,
806                            void *context_private,
807                            struct pipe_box *sub_box)
808 {
809    struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
810    struct sw_winsys *winsys = screen->winsys;
811    struct llvmpipe_resource *texture = llvmpipe_resource(resource);
812 
813    assert(texture->dt);
814    if (texture->dt)
815       winsys->displaytarget_display(winsys, texture->dt, context_private, sub_box);
816 }
817 
818 static void
llvmpipe_destroy_screen(struct pipe_screen * _screen)819 llvmpipe_destroy_screen( struct pipe_screen *_screen )
820 {
821    struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
822    struct sw_winsys *winsys = screen->winsys;
823 
824    if (screen->cs_tpool)
825       lp_cs_tpool_destroy(screen->cs_tpool);
826 
827    if (screen->rast)
828       lp_rast_destroy(screen->rast);
829 
830    lp_jit_screen_cleanup(screen);
831 
832    if (LP_DEBUG & DEBUG_CACHE_STATS)
833       printf("disk shader cache:   hits = %u, misses = %u\n", screen->num_disk_shader_cache_hits,
834              screen->num_disk_shader_cache_misses);
835    disk_cache_destroy(screen->disk_shader_cache);
836    if(winsys->destroy)
837       winsys->destroy(winsys);
838 
839    glsl_type_singleton_decref();
840 
841    mtx_destroy(&screen->rast_mutex);
842    mtx_destroy(&screen->cs_mutex);
843    FREE(screen);
844 }
845 
846 
847 
848 
849 /**
850  * Fence reference counting.
851  */
852 static void
llvmpipe_fence_reference(struct pipe_screen * screen,struct pipe_fence_handle ** ptr,struct pipe_fence_handle * fence)853 llvmpipe_fence_reference(struct pipe_screen *screen,
854                          struct pipe_fence_handle **ptr,
855                          struct pipe_fence_handle *fence)
856 {
857    struct lp_fence **old = (struct lp_fence **) ptr;
858    struct lp_fence *f = (struct lp_fence *) fence;
859 
860    lp_fence_reference(old, f);
861 }
862 
863 
864 /**
865  * Wait for the fence to finish.
866  */
867 static bool
llvmpipe_fence_finish(struct pipe_screen * screen,struct pipe_context * ctx,struct pipe_fence_handle * fence_handle,uint64_t timeout)868 llvmpipe_fence_finish(struct pipe_screen *screen,
869                       struct pipe_context *ctx,
870                       struct pipe_fence_handle *fence_handle,
871                       uint64_t timeout)
872 {
873    struct lp_fence *f = (struct lp_fence *) fence_handle;
874 
875    if (!timeout)
876       return lp_fence_signalled(f);
877 
878    if (!lp_fence_signalled(f)) {
879       if (timeout != PIPE_TIMEOUT_INFINITE)
880          return lp_fence_timedwait(f, timeout);
881 
882       lp_fence_wait(f);
883    }
884    return true;
885 }
886 
887 static uint64_t
llvmpipe_get_timestamp(struct pipe_screen * _screen)888 llvmpipe_get_timestamp(struct pipe_screen *_screen)
889 {
890    return os_time_get_nano();
891 }
892 
update_cache_sha1_cpu(struct mesa_sha1 * ctx)893 static void update_cache_sha1_cpu(struct mesa_sha1 *ctx)
894 {
895    const struct util_cpu_caps_t *cpu_caps = util_get_cpu_caps();
896    /*
897     * Don't need the cpu cache affinity stuff. The rest
898     * is contained in first 5 dwords.
899     */
900    STATIC_ASSERT(offsetof(struct util_cpu_caps_t, num_L3_caches) == 5 * sizeof(uint32_t));
901    _mesa_sha1_update(ctx, cpu_caps, 5 * sizeof(uint32_t));
902 }
903 
lp_disk_cache_create(struct llvmpipe_screen * screen)904 static void lp_disk_cache_create(struct llvmpipe_screen *screen)
905 {
906    struct mesa_sha1 ctx;
907    unsigned gallivm_perf = gallivm_get_perf_flags();
908    unsigned char sha1[20];
909    char cache_id[20 * 2 + 1];
910    _mesa_sha1_init(&ctx);
911 
912    if (!disk_cache_get_function_identifier(lp_disk_cache_create, &ctx) ||
913        !disk_cache_get_function_identifier(LLVMLinkInMCJIT, &ctx))
914       return;
915 
916    _mesa_sha1_update(&ctx, &gallivm_perf, sizeof(gallivm_perf));
917    update_cache_sha1_cpu(&ctx);
918    _mesa_sha1_final(&ctx, sha1);
919    disk_cache_format_hex_id(cache_id, sha1, 20 * 2);
920 
921    screen->disk_shader_cache = disk_cache_create("llvmpipe", cache_id, 0);
922 }
923 
lp_get_disk_shader_cache(struct pipe_screen * _screen)924 static struct disk_cache *lp_get_disk_shader_cache(struct pipe_screen *_screen)
925 {
926    struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
927 
928    return screen->disk_shader_cache;
929 }
930 
lp_disk_cache_find_shader(struct llvmpipe_screen * screen,struct lp_cached_code * cache,unsigned char ir_sha1_cache_key[20])931 void lp_disk_cache_find_shader(struct llvmpipe_screen *screen,
932                                struct lp_cached_code *cache,
933                                unsigned char ir_sha1_cache_key[20])
934 {
935    unsigned char sha1[CACHE_KEY_SIZE];
936 
937    if (!screen->disk_shader_cache)
938       return;
939    disk_cache_compute_key(screen->disk_shader_cache, ir_sha1_cache_key, 20, sha1);
940 
941    size_t binary_size;
942    uint8_t *buffer = disk_cache_get(screen->disk_shader_cache, sha1, &binary_size);
943    if (!buffer) {
944       cache->data_size = 0;
945       p_atomic_inc(&screen->num_disk_shader_cache_misses);
946       return;
947    }
948    cache->data_size = binary_size;
949    cache->data = buffer;
950    p_atomic_inc(&screen->num_disk_shader_cache_hits);
951 }
952 
lp_disk_cache_insert_shader(struct llvmpipe_screen * screen,struct lp_cached_code * cache,unsigned char ir_sha1_cache_key[20])953 void lp_disk_cache_insert_shader(struct llvmpipe_screen *screen,
954                                  struct lp_cached_code *cache,
955                                  unsigned char ir_sha1_cache_key[20])
956 {
957    unsigned char sha1[CACHE_KEY_SIZE];
958 
959    if (!screen->disk_shader_cache || !cache->data_size || cache->dont_cache)
960       return;
961    disk_cache_compute_key(screen->disk_shader_cache, ir_sha1_cache_key, 20, sha1);
962    disk_cache_put(screen->disk_shader_cache, sha1, cache->data, cache->data_size, NULL);
963 }
964 
965 bool
llvmpipe_screen_late_init(struct llvmpipe_screen * screen)966 llvmpipe_screen_late_init(struct llvmpipe_screen *screen)
967 {
968    bool ret = true;
969    mtx_lock(&screen->late_mutex);
970 
971    if (screen->late_init_done)
972       goto out;
973 
974    screen->rast = lp_rast_create(screen->num_threads);
975    if (!screen->rast) {
976       ret = false;
977       goto out;
978    }
979 
980    screen->cs_tpool = lp_cs_tpool_create(screen->num_threads);
981    if (!screen->cs_tpool) {
982       lp_rast_destroy(screen->rast);
983       ret = false;
984       goto out;
985    }
986 
987    lp_disk_cache_create(screen);
988    screen->late_init_done = true;
989 out:
990    mtx_unlock(&screen->late_mutex);
991    return ret;
992 }
993 
994 /**
995  * Create a new pipe_screen object
996  * Note: we're not presently subclassing pipe_screen (no llvmpipe_screen).
997  */
998 struct pipe_screen *
llvmpipe_create_screen(struct sw_winsys * winsys)999 llvmpipe_create_screen(struct sw_winsys *winsys)
1000 {
1001    struct llvmpipe_screen *screen;
1002 
1003    util_cpu_detect();
1004 
1005    glsl_type_singleton_init_or_ref();
1006 
1007 #ifdef DEBUG
1008    LP_DEBUG = debug_get_flags_option("LP_DEBUG", lp_debug_flags, 0 );
1009 #endif
1010 
1011    LP_PERF = debug_get_flags_option("LP_PERF", lp_perf_flags, 0 );
1012 
1013    screen = CALLOC_STRUCT(llvmpipe_screen);
1014    if (!screen)
1015       return NULL;
1016 
1017    if (!lp_jit_screen_init(screen)) {
1018       FREE(screen);
1019       return NULL;
1020    }
1021 
1022    screen->winsys = winsys;
1023 
1024    screen->base.destroy = llvmpipe_destroy_screen;
1025 
1026    screen->base.get_name = llvmpipe_get_name;
1027    screen->base.get_vendor = llvmpipe_get_vendor;
1028    screen->base.get_device_vendor = llvmpipe_get_vendor; // TODO should be the CPU vendor
1029    screen->base.get_param = llvmpipe_get_param;
1030    screen->base.get_shader_param = llvmpipe_get_shader_param;
1031    screen->base.get_compute_param = llvmpipe_get_compute_param;
1032    screen->base.get_paramf = llvmpipe_get_paramf;
1033    screen->base.get_compiler_options = llvmpipe_get_compiler_options;
1034    screen->base.is_format_supported = llvmpipe_is_format_supported;
1035 
1036    screen->base.context_create = llvmpipe_create_context;
1037    screen->base.flush_frontbuffer = llvmpipe_flush_frontbuffer;
1038    screen->base.fence_reference = llvmpipe_fence_reference;
1039    screen->base.fence_finish = llvmpipe_fence_finish;
1040 
1041    screen->base.get_timestamp = llvmpipe_get_timestamp;
1042 
1043    screen->base.finalize_nir = llvmpipe_finalize_nir;
1044 
1045    screen->base.get_disk_shader_cache = lp_get_disk_shader_cache;
1046    llvmpipe_init_screen_resource_funcs(&screen->base);
1047 
1048    screen->allow_cl = !!getenv("LP_CL");
1049    screen->use_tgsi = (LP_DEBUG & DEBUG_TGSI_IR);
1050    screen->num_threads = util_get_cpu_caps()->nr_cpus > 1 ? util_get_cpu_caps()->nr_cpus : 0;
1051 #ifdef EMBEDDED_DEVICE
1052    screen->num_threads = MIN2(screen->num_threads, 2);
1053 #endif
1054    screen->num_threads = debug_get_num_option("LP_NUM_THREADS", screen->num_threads);
1055    screen->num_threads = MIN2(screen->num_threads, LP_MAX_THREADS);
1056 
1057    lp_build_init(); /* get lp_native_vector_width initialised */
1058 
1059    snprintf(screen->renderer_string, sizeof(screen->renderer_string), "llvmpipe (LLVM " MESA_LLVM_VERSION_STRING ", %u bits)", lp_native_vector_width );
1060 
1061    (void) mtx_init(&screen->cs_mutex, mtx_plain);
1062    (void) mtx_init(&screen->rast_mutex, mtx_plain);
1063 
1064    (void) mtx_init(&screen->late_mutex, mtx_plain);
1065 
1066    return &screen->base;
1067 }
1068