• 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    { "mem", DEBUG_MEM, NULL },
74    { "fs", DEBUG_FS, NULL },
75    { "cs", DEBUG_CS, NULL },
76    { "tgsi_ir", DEBUG_TGSI_IR, NULL },
77    { "cache_stats", DEBUG_CACHE_STATS, NULL },
78    DEBUG_NAMED_VALUE_END
79 };
80 #endif
81 
82 int LP_PERF = 0;
83 static const struct debug_named_value lp_perf_flags[] = {
84    { "texmem",         PERF_TEX_MEM, NULL },
85    { "no_mipmap",      PERF_NO_MIPMAPS, NULL },
86    { "no_linear",      PERF_NO_LINEAR, NULL },
87    { "no_mip_linear",  PERF_NO_MIP_LINEAR, NULL },
88    { "no_tex",         PERF_NO_TEX, NULL },
89    { "no_blend",       PERF_NO_BLEND, NULL },
90    { "no_depth",       PERF_NO_DEPTH, NULL },
91    { "no_alphatest",   PERF_NO_ALPHATEST, NULL },
92    DEBUG_NAMED_VALUE_END
93 };
94 
95 
96 static const char *
llvmpipe_get_vendor(struct pipe_screen * screen)97 llvmpipe_get_vendor(struct pipe_screen *screen)
98 {
99    return "Mesa/X.org";
100 }
101 
102 
103 static const char *
llvmpipe_get_name(struct pipe_screen * screen)104 llvmpipe_get_name(struct pipe_screen *screen)
105 {
106    static char buf[100];
107    snprintf(buf, sizeof(buf), "llvmpipe (LLVM " MESA_LLVM_VERSION_STRING ", %u bits)",
108             lp_native_vector_width );
109    return buf;
110 }
111 
112 
113 static int
llvmpipe_get_param(struct pipe_screen * screen,enum pipe_cap param)114 llvmpipe_get_param(struct pipe_screen *screen, enum pipe_cap param)
115 {
116    switch (param) {
117    case PIPE_CAP_NPOT_TEXTURES:
118    case PIPE_CAP_MIXED_FRAMEBUFFER_SIZES:
119    case PIPE_CAP_MIXED_COLOR_DEPTH_BITS:
120       return 1;
121    case PIPE_CAP_FRAGMENT_SHADER_TEXTURE_LOD:
122    case PIPE_CAP_FRAGMENT_SHADER_DERIVATIVES:
123    case PIPE_CAP_VERTEX_SHADER_SATURATE:
124       return 1;
125    case PIPE_CAP_MAX_DUAL_SOURCE_RENDER_TARGETS:
126       return 1;
127    case PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS:
128       return PIPE_MAX_SO_BUFFERS;
129    case PIPE_CAP_POINT_SPRITE:
130       return 1;
131    case PIPE_CAP_MAX_RENDER_TARGETS:
132       return PIPE_MAX_COLOR_BUFS;
133    case PIPE_CAP_OCCLUSION_QUERY:
134    case PIPE_CAP_QUERY_TIMESTAMP:
135       return 1;
136    case PIPE_CAP_QUERY_PIPELINE_STATISTICS:
137       return 1;
138    case PIPE_CAP_TEXTURE_MIRROR_CLAMP:
139    case PIPE_CAP_TEXTURE_MIRROR_CLAMP_TO_EDGE:
140       return 1;
141    case PIPE_CAP_TEXTURE_SWIZZLE:
142    case PIPE_CAP_TEXTURE_SHADOW_LOD:
143       return 1;
144    case PIPE_CAP_MAX_TEXTURE_2D_SIZE:
145       return 1 << (LP_MAX_TEXTURE_2D_LEVELS - 1);
146    case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
147       return LP_MAX_TEXTURE_3D_LEVELS;
148    case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
149       return LP_MAX_TEXTURE_CUBE_LEVELS;
150    case PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS:
151       return LP_MAX_TEXTURE_ARRAY_LAYERS;
152    case PIPE_CAP_BLEND_EQUATION_SEPARATE:
153       return 1;
154    case PIPE_CAP_INDEP_BLEND_ENABLE:
155       return 1;
156    case PIPE_CAP_INDEP_BLEND_FUNC:
157       return 1;
158    case PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT:
159    case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER:
160    case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER:
161       return 1;
162    case PIPE_CAP_PRIMITIVE_RESTART:
163    case PIPE_CAP_PRIMITIVE_RESTART_FIXED_INDEX:
164       return 1;
165    case PIPE_CAP_DEPTH_CLIP_DISABLE:
166       return 1;
167    case PIPE_CAP_SHADER_STENCIL_EXPORT:
168       return 1;
169    case PIPE_CAP_TGSI_INSTANCEID:
170    case PIPE_CAP_VERTEX_ELEMENT_INSTANCE_DIVISOR:
171    case PIPE_CAP_START_INSTANCE:
172    case PIPE_CAP_MIXED_COLORBUFFER_FORMATS:
173       return 1;
174    case PIPE_CAP_SEAMLESS_CUBE_MAP:
175    case PIPE_CAP_SEAMLESS_CUBE_MAP_PER_TEXTURE:
176       return 1;
177    /* this is a lie could support arbitrary large offsets */
178    case PIPE_CAP_MIN_TEXTURE_GATHER_OFFSET:
179    case PIPE_CAP_MIN_TEXEL_OFFSET:
180       return -32;
181    case PIPE_CAP_MAX_TEXTURE_GATHER_OFFSET:
182    case PIPE_CAP_MAX_TEXEL_OFFSET:
183       return 31;
184    case PIPE_CAP_CONDITIONAL_RENDER:
185    case PIPE_CAP_TEXTURE_BARRIER:
186       return 1;
187    case PIPE_CAP_MAX_STREAM_OUTPUT_SEPARATE_COMPONENTS:
188    case PIPE_CAP_MAX_STREAM_OUTPUT_INTERLEAVED_COMPONENTS:
189       return 16*4;
190    case PIPE_CAP_MAX_GEOMETRY_OUTPUT_VERTICES:
191    case PIPE_CAP_MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS:
192       return 1024;
193    case PIPE_CAP_MAX_VERTEX_STREAMS: {
194       struct llvmpipe_screen *lscreen = llvmpipe_screen(screen);
195       return lscreen->use_tgsi ? 1 : 4;
196    }
197    case PIPE_CAP_MAX_VERTEX_ATTRIB_STRIDE:
198       return 2048;
199    case PIPE_CAP_STREAM_OUTPUT_PAUSE_RESUME:
200    case PIPE_CAP_STREAM_OUTPUT_INTERLEAVE_BUFFERS:
201    case PIPE_CAP_VERTEX_COLOR_UNCLAMPED:
202    case PIPE_CAP_VERTEX_COLOR_CLAMPED:
203       return 1;
204    case PIPE_CAP_GLSL_FEATURE_LEVEL: {
205       struct llvmpipe_screen *lscreen = llvmpipe_screen(screen);
206       return lscreen->use_tgsi ? 330 : 450;
207    }
208    case PIPE_CAP_GLSL_FEATURE_LEVEL_COMPATIBILITY:
209       return 140;
210    case PIPE_CAP_COMPUTE:
211       return GALLIVM_HAVE_CORO;
212    case PIPE_CAP_USER_VERTEX_BUFFERS:
213       return 1;
214    case PIPE_CAP_TGSI_TEXCOORD:
215    case PIPE_CAP_DRAW_INDIRECT:
216       return 1;
217 
218    case PIPE_CAP_CUBE_MAP_ARRAY:
219       return 1;
220    case PIPE_CAP_CONSTANT_BUFFER_OFFSET_ALIGNMENT:
221       return 16;
222    case PIPE_CAP_MIN_MAP_BUFFER_ALIGNMENT:
223       return 64;
224    case PIPE_CAP_TEXTURE_BUFFER_OBJECTS:
225       return 1;
226    case PIPE_CAP_MAX_TEXTURE_BUFFER_SIZE:
227       return 65536;
228    case PIPE_CAP_TEXTURE_BUFFER_OFFSET_ALIGNMENT:
229       return 16;
230    case PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER:
231       return 0;
232    case PIPE_CAP_MAX_VIEWPORTS:
233       return PIPE_MAX_VIEWPORTS;
234    case PIPE_CAP_ENDIANNESS:
235       return PIPE_ENDIAN_NATIVE;
236    case PIPE_CAP_TGSI_VS_LAYER_VIEWPORT:
237       return 1;
238    case PIPE_CAP_BUFFER_MAP_PERSISTENT_COHERENT:
239       return 1;
240    case PIPE_CAP_MAX_TEXTURE_GATHER_COMPONENTS:
241       return 4;
242    case PIPE_CAP_TGSI_VS_WINDOW_SPACE_POSITION:
243       return 1;
244    case PIPE_CAP_TGSI_FS_FINE_DERIVATIVE:
245       return 1;
246    case PIPE_CAP_TGSI_TEX_TXF_LZ:
247    case PIPE_CAP_SAMPLER_VIEW_TARGET:
248       return 1;
249    case PIPE_CAP_FAKE_SW_MSAA: {
250       struct llvmpipe_screen *lscreen = llvmpipe_screen(screen);
251       return lscreen->use_tgsi ? 1 : 0;
252    }
253    case PIPE_CAP_TEXTURE_QUERY_LOD:
254    case PIPE_CAP_CONDITIONAL_RENDER_INVERTED:
255    case PIPE_CAP_TGSI_ARRAY_COMPONENTS:
256    case PIPE_CAP_DOUBLES:
257    case PIPE_CAP_INT64:
258    case PIPE_CAP_INT64_DIVMOD:
259    case PIPE_CAP_QUERY_SO_OVERFLOW:
260    case PIPE_CAP_TGSI_DIV:
261       return 1;
262 
263    case PIPE_CAP_VENDOR_ID:
264       return 0xFFFFFFFF;
265    case PIPE_CAP_DEVICE_ID:
266       return 0xFFFFFFFF;
267    case PIPE_CAP_ACCELERATED:
268       return 0;
269    case PIPE_CAP_VIDEO_MEMORY: {
270       /* XXX: Do we want to return the full amount fo system memory ? */
271       uint64_t system_memory;
272 
273       if (!os_get_total_physical_memory(&system_memory))
274          return 0;
275 
276       if (sizeof(void *) == 4)
277          /* Cap to 2 GB on 32 bits system. We do this because llvmpipe does
278           * eat application memory, which is quite limited on 32 bits. App
279           * shouldn't expect too much available memory. */
280          system_memory = MIN2(system_memory, 2048 << 20);
281 
282       return (int)(system_memory >> 20);
283    }
284    case PIPE_CAP_UMA:
285       return 0;
286    case PIPE_CAP_CLIP_HALFZ:
287       return 1;
288    case PIPE_CAP_POLYGON_OFFSET_CLAMP:
289    case PIPE_CAP_TEXTURE_FLOAT_LINEAR:
290    case PIPE_CAP_TEXTURE_HALF_FLOAT_LINEAR:
291       return 1;
292    case PIPE_CAP_CULL_DISTANCE:
293       return 1;
294    case PIPE_CAP_COPY_BETWEEN_COMPRESSED_AND_PLAIN_FORMATS:
295       return 1;
296    case PIPE_CAP_CLEAR_TEXTURE:
297       return 1;
298    case PIPE_CAP_MAX_VARYINGS:
299       return 32;
300    case PIPE_CAP_SHADER_BUFFER_OFFSET_ALIGNMENT:
301       return 16;
302    case PIPE_CAP_QUERY_BUFFER_OBJECT:
303       return 1;
304    case PIPE_CAP_DRAW_PARAMETERS:
305       return 1;
306    case PIPE_CAP_FBFETCH:
307       return 8;
308    case PIPE_CAP_FBFETCH_COHERENT:
309       return 0;
310    case PIPE_CAP_MULTI_DRAW_INDIRECT:
311    case PIPE_CAP_MULTI_DRAW_INDIRECT_PARAMS:
312       return 1;
313    case PIPE_CAP_DEVICE_RESET_STATUS_QUERY:
314    case PIPE_CAP_ROBUST_BUFFER_ACCESS_BEHAVIOR:
315       return 1;
316    case PIPE_CAP_MAX_SHADER_PATCH_VARYINGS:
317       return 32;
318    case PIPE_CAP_RASTERIZER_SUBPIXEL_BITS:
319       return 8;
320    case PIPE_CAP_PCI_GROUP:
321    case PIPE_CAP_PCI_BUS:
322    case PIPE_CAP_PCI_DEVICE:
323    case PIPE_CAP_PCI_FUNCTION:
324    case PIPE_CAP_GLSL_OPTIMIZE_CONSERVATIVELY:
325    case PIPE_CAP_ALLOW_MAPPED_BUFFERS_DURING_EXECUTION:
326       return 0;
327    case PIPE_CAP_MAX_GS_INVOCATIONS:
328       return 32;
329    case PIPE_CAP_MAX_SHADER_BUFFER_SIZE:
330       return LP_MAX_TGSI_SHADER_BUFFER_SIZE;
331    case PIPE_CAP_FRAMEBUFFER_NO_ATTACHMENT:
332    case PIPE_CAP_TGSI_TG4_COMPONENT_IN_SWIZZLE:
333    case PIPE_CAP_TGSI_FS_FACE_IS_INTEGER_SYSVAL:
334       return 1;
335    case PIPE_CAP_TGSI_TXQS:
336    case PIPE_CAP_TGSI_VOTE:
337    case PIPE_CAP_LOAD_CONSTBUF:
338    case PIPE_CAP_TEXTURE_MULTISAMPLE:
339    case PIPE_CAP_SAMPLE_SHADING:
340    case PIPE_CAP_POST_DEPTH_COVERAGE:
341    case PIPE_CAP_PACKED_UNIFORMS: {
342       struct llvmpipe_screen *lscreen = llvmpipe_screen(screen);
343       return !lscreen->use_tgsi;
344    }
345    default:
346       return u_pipe_screen_get_param_defaults(screen, param);
347    }
348 }
349 
350 static int
llvmpipe_get_shader_param(struct pipe_screen * screen,enum pipe_shader_type shader,enum pipe_shader_cap param)351 llvmpipe_get_shader_param(struct pipe_screen *screen,
352                           enum pipe_shader_type shader,
353                           enum pipe_shader_cap param)
354 {
355    struct llvmpipe_screen *lscreen = llvmpipe_screen(screen);
356    switch(shader)
357    {
358    case PIPE_SHADER_COMPUTE:
359       if ((lscreen->allow_cl) && param == PIPE_SHADER_CAP_SUPPORTED_IRS)
360          return (1 << PIPE_SHADER_IR_TGSI) | (1 << PIPE_SHADER_IR_NIR) | (1 << PIPE_SHADER_IR_NIR_SERIALIZED);
361       /* fallthrough */
362    case PIPE_SHADER_FRAGMENT:
363       if (param == PIPE_SHADER_CAP_PREFERRED_IR) {
364          if (lscreen->use_tgsi)
365             return PIPE_SHADER_IR_TGSI;
366          else
367             return PIPE_SHADER_IR_NIR;
368       }
369       switch (param) {
370       default:
371          return gallivm_get_shader_param(param);
372       }
373       /* fallthrough */
374    case PIPE_SHADER_TESS_CTRL:
375    case PIPE_SHADER_TESS_EVAL:
376       /* Tessellation shader needs llvm coroutines support */
377       if (!GALLIVM_HAVE_CORO || lscreen->use_tgsi)
378          return 0;
379       /* fallthrough */
380    case PIPE_SHADER_VERTEX:
381    case PIPE_SHADER_GEOMETRY:
382       if (param == PIPE_SHADER_CAP_PREFERRED_IR) {
383          if (lscreen->use_tgsi)
384             return PIPE_SHADER_IR_TGSI;
385          else
386             return PIPE_SHADER_IR_NIR;
387       }
388 
389       switch (param) {
390       case PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS:
391          /* At this time, the draw module and llvmpipe driver only
392           * support vertex shader texture lookups when LLVM is enabled in
393           * the draw module.
394           */
395          if (debug_get_bool_option("DRAW_USE_LLVM", TRUE))
396             return PIPE_MAX_SAMPLERS;
397          else
398             return 0;
399       case PIPE_SHADER_CAP_MAX_SAMPLER_VIEWS:
400          if (debug_get_bool_option("DRAW_USE_LLVM", TRUE))
401             return PIPE_MAX_SHADER_SAMPLER_VIEWS;
402          else
403             return 0;
404       default:
405          return draw_get_shader_param(shader, param);
406       }
407    default:
408       return 0;
409    }
410 }
411 
412 static float
llvmpipe_get_paramf(struct pipe_screen * screen,enum pipe_capf param)413 llvmpipe_get_paramf(struct pipe_screen *screen, enum pipe_capf param)
414 {
415    switch (param) {
416    case PIPE_CAPF_MAX_LINE_WIDTH:
417       /* fall-through */
418    case PIPE_CAPF_MAX_LINE_WIDTH_AA:
419       return 255.0; /* arbitrary */
420    case PIPE_CAPF_MAX_POINT_WIDTH:
421       /* fall-through */
422    case PIPE_CAPF_MAX_POINT_WIDTH_AA:
423       return LP_MAX_POINT_WIDTH; /* arbitrary */
424    case PIPE_CAPF_MAX_TEXTURE_ANISOTROPY:
425       return 16.0; /* not actually signficant at this time */
426    case PIPE_CAPF_MAX_TEXTURE_LOD_BIAS:
427       return 16.0; /* arbitrary */
428    case PIPE_CAPF_MIN_CONSERVATIVE_RASTER_DILATE:
429       return 0.0;
430    case PIPE_CAPF_MAX_CONSERVATIVE_RASTER_DILATE:
431       return 0.0;
432    case PIPE_CAPF_CONSERVATIVE_RASTER_DILATE_GRANULARITY:
433       return 0.0;
434    }
435    /* should only get here on unhandled cases */
436    debug_printf("Unexpected PIPE_CAP %d query\n", param);
437    return 0.0;
438 }
439 
440 static int
llvmpipe_get_compute_param(struct pipe_screen * _screen,enum pipe_shader_ir ir_type,enum pipe_compute_cap param,void * ret)441 llvmpipe_get_compute_param(struct pipe_screen *_screen,
442                            enum pipe_shader_ir ir_type,
443                            enum pipe_compute_cap param,
444                            void *ret)
445 {
446    switch (param) {
447    case PIPE_COMPUTE_CAP_IR_TARGET:
448       return 0;
449    case PIPE_COMPUTE_CAP_MAX_GRID_SIZE:
450       if (ret) {
451          uint64_t *grid_size = ret;
452          grid_size[0] = 65535;
453          grid_size[1] = 65535;
454          grid_size[2] = 65535;
455       }
456       return 3 * sizeof(uint64_t) ;
457    case PIPE_COMPUTE_CAP_MAX_BLOCK_SIZE:
458       if (ret) {
459          uint64_t *block_size = ret;
460          block_size[0] = 1024;
461          block_size[1] = 1024;
462          block_size[2] = 1024;
463       }
464       return 3 * sizeof(uint64_t);
465    case PIPE_COMPUTE_CAP_MAX_THREADS_PER_BLOCK:
466       if (ret) {
467          uint64_t *max_threads_per_block = ret;
468          *max_threads_per_block = 1024;
469       }
470       return sizeof(uint64_t);
471    case PIPE_COMPUTE_CAP_MAX_LOCAL_SIZE:
472       if (ret) {
473          uint64_t *max_local_size = ret;
474          *max_local_size = 32768;
475       }
476       return sizeof(uint64_t);
477    case PIPE_COMPUTE_CAP_GRID_DIMENSION:
478       if (ret) {
479          uint32_t *grid_dim = ret;
480          *grid_dim = 3;
481       }
482       return sizeof(uint32_t);
483    case PIPE_COMPUTE_CAP_MAX_GLOBAL_SIZE:
484       if (ret) {
485          uint64_t *max_global_size = ret;
486          *max_global_size = (1ULL << 31);
487       }
488       return sizeof(uint64_t);
489    case PIPE_COMPUTE_CAP_MAX_MEM_ALLOC_SIZE:
490       if (ret) {
491          uint64_t *max_mem_alloc_size = ret;
492          *max_mem_alloc_size = (1ULL << 31);
493       }
494       return sizeof(uint64_t);
495    case PIPE_COMPUTE_CAP_MAX_PRIVATE_SIZE:
496       if (ret) {
497          uint64_t *max_private = ret;
498          *max_private = (1UL << 31);
499       }
500       return sizeof(uint64_t);
501    case PIPE_COMPUTE_CAP_MAX_INPUT_SIZE:
502       if (ret) {
503          uint64_t *max_input = ret;
504          *max_input = 4096;
505       }
506       return sizeof(uint64_t);
507    case PIPE_COMPUTE_CAP_IMAGES_SUPPORTED:
508       if (ret) {
509          uint32_t *images = ret;
510          *images = LP_MAX_TGSI_SHADER_IMAGES;
511       }
512       return sizeof(uint32_t);
513    case PIPE_COMPUTE_CAP_MAX_VARIABLE_THREADS_PER_BLOCK:
514       return 0;
515    case PIPE_COMPUTE_CAP_SUBGROUP_SIZE:
516       if (ret) {
517          uint32_t *subgroup_size = ret;
518          *subgroup_size = 32;
519       }
520       return sizeof(uint32_t);
521    case PIPE_COMPUTE_CAP_MAX_COMPUTE_UNITS:
522       if (ret) {
523          uint32_t *max_compute_units = ret;
524          *max_compute_units = 8;
525       }
526       return sizeof(uint32_t);
527    case PIPE_COMPUTE_CAP_MAX_CLOCK_FREQUENCY:
528       if (ret) {
529          uint32_t *max_clock_freq = ret;
530          *max_clock_freq = 300;
531       }
532       return sizeof(uint32_t);
533    case PIPE_COMPUTE_CAP_ADDRESS_BITS:
534       if (ret) {
535          uint32_t *address_bits = ret;
536          *address_bits = 64;
537       }
538       return sizeof(uint32_t);
539    }
540    return 0;
541 }
542 
543 static const struct nir_shader_compiler_options gallivm_nir_options = {
544    .lower_scmp = true,
545    .lower_flrp32 = true,
546    .lower_flrp64 = true,
547    .lower_fsat = true,
548    .lower_bitfield_insert_to_shifts = true,
549    .lower_bitfield_extract_to_shifts = true,
550    .lower_sub = true,
551    .lower_ffma16 = true,
552    .lower_ffma32 = true,
553    .lower_ffma64 = true,
554    .lower_fmod = true,
555    .lower_hadd = true,
556    .lower_add_sat = true,
557    .lower_ldexp = true,
558    .lower_pack_snorm_2x16 = true,
559    .lower_pack_snorm_4x8 = true,
560    .lower_pack_unorm_2x16 = true,
561    .lower_pack_unorm_4x8 = true,
562    .lower_pack_half_2x16 = true,
563    .lower_pack_split = true,
564    .lower_unpack_snorm_2x16 = true,
565    .lower_unpack_snorm_4x8 = true,
566    .lower_unpack_unorm_2x16 = true,
567    .lower_unpack_unorm_4x8 = true,
568    .lower_unpack_half_2x16 = true,
569    .lower_extract_byte = true,
570    .lower_extract_word = true,
571    .lower_rotate = true,
572    .lower_uadd_carry = true,
573    .lower_usub_borrow = true,
574    .lower_mul_2x32_64 = true,
575    .lower_ifind_msb = true,
576    .max_unroll_iterations = 32,
577    .use_interpolated_input_intrinsics = true,
578    .lower_to_scalar = true,
579    .lower_cs_local_index_from_id = true,
580    .lower_uniforms_to_ubo = true,
581 };
582 
583 static void
llvmpipe_finalize_nir(struct pipe_screen * screen,void * nirptr,bool optimize)584 llvmpipe_finalize_nir(struct pipe_screen *screen,
585                       void *nirptr,
586                       bool optimize)
587 {
588    struct nir_shader *nir = (struct nir_shader *)nirptr;
589    lp_build_opt_nir(nir);
590 }
591 
592 static inline const void *
llvmpipe_get_compiler_options(struct pipe_screen * screen,enum pipe_shader_ir ir,enum pipe_shader_type shader)593 llvmpipe_get_compiler_options(struct pipe_screen *screen,
594                               enum pipe_shader_ir ir,
595                               enum pipe_shader_type shader)
596 {
597    assert(ir == PIPE_SHADER_IR_NIR);
598    return &gallivm_nir_options;
599 }
600 
601 /**
602  * Query format support for creating a texture, drawing surface, etc.
603  * \param format  the format to test
604  * \param type  one of PIPE_TEXTURE, PIPE_SURFACE
605  */
606 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)607 llvmpipe_is_format_supported( struct pipe_screen *_screen,
608                               enum pipe_format format,
609                               enum pipe_texture_target target,
610                               unsigned sample_count,
611                               unsigned storage_sample_count,
612                               unsigned bind)
613 {
614    struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
615    struct sw_winsys *winsys = screen->winsys;
616    const struct util_format_description *format_desc;
617 
618    format_desc = util_format_description(format);
619    if (!format_desc)
620       return false;
621 
622    assert(target == PIPE_BUFFER ||
623           target == PIPE_TEXTURE_1D ||
624           target == PIPE_TEXTURE_1D_ARRAY ||
625           target == PIPE_TEXTURE_2D ||
626           target == PIPE_TEXTURE_2D_ARRAY ||
627           target == PIPE_TEXTURE_RECT ||
628           target == PIPE_TEXTURE_3D ||
629           target == PIPE_TEXTURE_CUBE ||
630           target == PIPE_TEXTURE_CUBE_ARRAY);
631 
632    if (sample_count != 0 && sample_count != 1 && sample_count != 4)
633       return false;
634 
635    if (MAX2(1, sample_count) != MAX2(1, storage_sample_count))
636       return false;
637 
638    if (bind & PIPE_BIND_RENDER_TARGET) {
639       if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
640          /* this is a lie actually other formats COULD exist where we would fail */
641          if (format_desc->nr_channels < 3)
642             return false;
643       }
644       else if (format_desc->colorspace != UTIL_FORMAT_COLORSPACE_RGB)
645          return false;
646 
647       if (format_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN &&
648           format != PIPE_FORMAT_R11G11B10_FLOAT)
649          return false;
650 
651       assert(format_desc->block.width == 1);
652       assert(format_desc->block.height == 1);
653 
654       if (format_desc->is_mixed)
655          return false;
656 
657       if (!format_desc->is_array && !format_desc->is_bitmask &&
658           format != PIPE_FORMAT_R11G11B10_FLOAT)
659          return false;
660    }
661 
662    if ((bind & (PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW)) &&
663        ((bind & PIPE_BIND_DISPLAY_TARGET) == 0)) {
664       /* Disable all 3-channel formats, where channel size != 32 bits.
665        * In some cases we run into crashes (in generate_unswizzled_blend()),
666        * for 3-channel RGB16 variants, there was an apparent LLVM bug.
667        * In any case, disabling the shallower 3-channel formats avoids a
668        * number of issues with GL_ARB_copy_image support.
669        */
670       if (format_desc->is_array &&
671           format_desc->nr_channels == 3 &&
672           format_desc->block.bits != 96) {
673          return false;
674       }
675    }
676 
677    if (bind & PIPE_BIND_DISPLAY_TARGET) {
678       if(!winsys->is_displaytarget_format_supported(winsys, bind, format))
679          return false;
680    }
681 
682    if (bind & PIPE_BIND_DEPTH_STENCIL) {
683       if (format_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
684          return false;
685 
686       if (format_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS)
687          return false;
688    }
689 
690    if (format_desc->layout == UTIL_FORMAT_LAYOUT_ASTC ||
691        format_desc->layout == UTIL_FORMAT_LAYOUT_ATC ||
692        format_desc->layout == UTIL_FORMAT_LAYOUT_FXT1) {
693       /* Software decoding is not hooked up. */
694       return false;
695    }
696 
697    if (format_desc->layout == UTIL_FORMAT_LAYOUT_ETC &&
698        format != PIPE_FORMAT_ETC1_RGB8)
699       return false;
700 
701    /*
702     * Everything can be supported by u_format
703     * (those without fetch_rgba_float might be not but shouldn't hit that)
704     */
705 
706    return true;
707 }
708 
709 
710 
711 
712 static void
llvmpipe_flush_frontbuffer(struct pipe_screen * _screen,struct pipe_resource * resource,unsigned level,unsigned layer,void * context_private,struct pipe_box * sub_box)713 llvmpipe_flush_frontbuffer(struct pipe_screen *_screen,
714                            struct pipe_resource *resource,
715                            unsigned level, unsigned layer,
716                            void *context_private,
717                            struct pipe_box *sub_box)
718 {
719    struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
720    struct sw_winsys *winsys = screen->winsys;
721    struct llvmpipe_resource *texture = llvmpipe_resource(resource);
722 
723    assert(texture->dt);
724    if (texture->dt)
725       winsys->displaytarget_display(winsys, texture->dt, context_private, sub_box);
726 }
727 
728 static void
llvmpipe_destroy_screen(struct pipe_screen * _screen)729 llvmpipe_destroy_screen( struct pipe_screen *_screen )
730 {
731    struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
732    struct sw_winsys *winsys = screen->winsys;
733 
734    if (screen->cs_tpool)
735       lp_cs_tpool_destroy(screen->cs_tpool);
736 
737    if (screen->rast)
738       lp_rast_destroy(screen->rast);
739 
740    lp_jit_screen_cleanup(screen);
741 
742    if (LP_DEBUG & DEBUG_CACHE_STATS)
743       printf("disk shader cache:   hits = %u, misses = %u\n", screen->num_disk_shader_cache_hits,
744              screen->num_disk_shader_cache_misses);
745    disk_cache_destroy(screen->disk_shader_cache);
746    if(winsys->destroy)
747       winsys->destroy(winsys);
748 
749    glsl_type_singleton_decref();
750 
751    mtx_destroy(&screen->rast_mutex);
752    mtx_destroy(&screen->cs_mutex);
753    FREE(screen);
754 }
755 
756 
757 
758 
759 /**
760  * Fence reference counting.
761  */
762 static void
llvmpipe_fence_reference(struct pipe_screen * screen,struct pipe_fence_handle ** ptr,struct pipe_fence_handle * fence)763 llvmpipe_fence_reference(struct pipe_screen *screen,
764                          struct pipe_fence_handle **ptr,
765                          struct pipe_fence_handle *fence)
766 {
767    struct lp_fence **old = (struct lp_fence **) ptr;
768    struct lp_fence *f = (struct lp_fence *) fence;
769 
770    lp_fence_reference(old, f);
771 }
772 
773 
774 /**
775  * Wait for the fence to finish.
776  */
777 static bool
llvmpipe_fence_finish(struct pipe_screen * screen,struct pipe_context * ctx,struct pipe_fence_handle * fence_handle,uint64_t timeout)778 llvmpipe_fence_finish(struct pipe_screen *screen,
779                       struct pipe_context *ctx,
780                       struct pipe_fence_handle *fence_handle,
781                       uint64_t timeout)
782 {
783    struct lp_fence *f = (struct lp_fence *) fence_handle;
784 
785    if (!timeout)
786       return lp_fence_signalled(f);
787 
788    if (!lp_fence_signalled(f)) {
789       if (timeout != PIPE_TIMEOUT_INFINITE)
790          return lp_fence_timedwait(f, timeout);
791 
792       lp_fence_wait(f);
793    }
794    return true;
795 }
796 
797 static uint64_t
llvmpipe_get_timestamp(struct pipe_screen * _screen)798 llvmpipe_get_timestamp(struct pipe_screen *_screen)
799 {
800    return os_time_get_nano();
801 }
802 
lp_disk_cache_create(struct llvmpipe_screen * screen)803 static void lp_disk_cache_create(struct llvmpipe_screen *screen)
804 {
805    struct mesa_sha1 ctx;
806    unsigned gallivm_perf = gallivm_get_perf_flags();
807    unsigned char sha1[20];
808    char cache_id[20 * 2 + 1];
809    _mesa_sha1_init(&ctx);
810 
811    if (!disk_cache_get_function_identifier(lp_disk_cache_create, &ctx) ||
812        !disk_cache_get_function_identifier(LLVMLinkInMCJIT, &ctx))
813       return;
814 
815    _mesa_sha1_update(&ctx, &gallivm_perf, sizeof(gallivm_perf));
816    _mesa_sha1_final(&ctx, sha1);
817    disk_cache_format_hex_id(cache_id, sha1, 20 * 2);
818 
819    screen->disk_shader_cache = disk_cache_create("llvmpipe", cache_id, 0);
820 }
821 
lp_get_disk_shader_cache(struct pipe_screen * _screen)822 static struct disk_cache *lp_get_disk_shader_cache(struct pipe_screen *_screen)
823 {
824    struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
825 
826    return screen->disk_shader_cache;
827 }
828 
lp_disk_cache_find_shader(struct llvmpipe_screen * screen,struct lp_cached_code * cache,unsigned char ir_sha1_cache_key[20])829 void lp_disk_cache_find_shader(struct llvmpipe_screen *screen,
830                                struct lp_cached_code *cache,
831                                unsigned char ir_sha1_cache_key[20])
832 {
833    unsigned char sha1[CACHE_KEY_SIZE];
834 
835    if (!screen->disk_shader_cache)
836       return;
837    disk_cache_compute_key(screen->disk_shader_cache, ir_sha1_cache_key, 20, sha1);
838 
839    size_t binary_size;
840    uint8_t *buffer = disk_cache_get(screen->disk_shader_cache, sha1, &binary_size);
841    if (!buffer) {
842       cache->data_size = 0;
843       p_atomic_inc(&screen->num_disk_shader_cache_misses);
844       return;
845    }
846    cache->data_size = binary_size;
847    cache->data = buffer;
848    p_atomic_inc(&screen->num_disk_shader_cache_hits);
849 }
850 
lp_disk_cache_insert_shader(struct llvmpipe_screen * screen,struct lp_cached_code * cache,unsigned char ir_sha1_cache_key[20])851 void lp_disk_cache_insert_shader(struct llvmpipe_screen *screen,
852                                  struct lp_cached_code *cache,
853                                  unsigned char ir_sha1_cache_key[20])
854 {
855    unsigned char sha1[CACHE_KEY_SIZE];
856 
857    if (!screen->disk_shader_cache || !cache->data_size || cache->dont_cache)
858       return;
859    disk_cache_compute_key(screen->disk_shader_cache, ir_sha1_cache_key, 20, sha1);
860    disk_cache_put(screen->disk_shader_cache, sha1, cache->data, cache->data_size, NULL);
861 }
862 /**
863  * Create a new pipe_screen object
864  * Note: we're not presently subclassing pipe_screen (no llvmpipe_screen).
865  */
866 struct pipe_screen *
llvmpipe_create_screen(struct sw_winsys * winsys)867 llvmpipe_create_screen(struct sw_winsys *winsys)
868 {
869    struct llvmpipe_screen *screen;
870 
871    util_cpu_detect();
872 
873    glsl_type_singleton_init_or_ref();
874 
875 #ifdef DEBUG
876    LP_DEBUG = debug_get_flags_option("LP_DEBUG", lp_debug_flags, 0 );
877 #endif
878 
879    LP_PERF = debug_get_flags_option("LP_PERF", lp_perf_flags, 0 );
880 
881    screen = CALLOC_STRUCT(llvmpipe_screen);
882    if (!screen)
883       return NULL;
884 
885    if (!lp_jit_screen_init(screen)) {
886       FREE(screen);
887       return NULL;
888    }
889 
890    screen->winsys = winsys;
891 
892    screen->base.destroy = llvmpipe_destroy_screen;
893 
894    screen->base.get_name = llvmpipe_get_name;
895    screen->base.get_vendor = llvmpipe_get_vendor;
896    screen->base.get_device_vendor = llvmpipe_get_vendor; // TODO should be the CPU vendor
897    screen->base.get_param = llvmpipe_get_param;
898    screen->base.get_shader_param = llvmpipe_get_shader_param;
899    screen->base.get_compute_param = llvmpipe_get_compute_param;
900    screen->base.get_paramf = llvmpipe_get_paramf;
901    screen->base.get_compiler_options = llvmpipe_get_compiler_options;
902    screen->base.is_format_supported = llvmpipe_is_format_supported;
903 
904    screen->base.context_create = llvmpipe_create_context;
905    screen->base.flush_frontbuffer = llvmpipe_flush_frontbuffer;
906    screen->base.fence_reference = llvmpipe_fence_reference;
907    screen->base.fence_finish = llvmpipe_fence_finish;
908 
909    screen->base.get_timestamp = llvmpipe_get_timestamp;
910 
911    screen->base.finalize_nir = llvmpipe_finalize_nir;
912 
913    screen->base.get_disk_shader_cache = lp_get_disk_shader_cache;
914    llvmpipe_init_screen_resource_funcs(&screen->base);
915 
916    screen->allow_cl = !!getenv("LP_CL");
917    screen->use_tgsi = (LP_DEBUG & DEBUG_TGSI_IR);
918    screen->num_threads = util_cpu_caps.nr_cpus > 1 ? util_cpu_caps.nr_cpus : 0;
919 #ifdef EMBEDDED_DEVICE
920    screen->num_threads = 0;
921 #endif
922    screen->num_threads = debug_get_num_option("LP_NUM_THREADS", screen->num_threads);
923    screen->num_threads = MIN2(screen->num_threads, LP_MAX_THREADS);
924 
925    screen->rast = lp_rast_create(screen->num_threads);
926    if (!screen->rast) {
927       lp_jit_screen_cleanup(screen);
928       FREE(screen);
929       return NULL;
930    }
931    (void) mtx_init(&screen->rast_mutex, mtx_plain);
932 
933    screen->cs_tpool = lp_cs_tpool_create(screen->num_threads);
934    if (!screen->cs_tpool) {
935       lp_rast_destroy(screen->rast);
936       lp_jit_screen_cleanup(screen);
937       FREE(screen);
938       return NULL;
939    }
940    (void) mtx_init(&screen->cs_mutex, mtx_plain);
941 
942    lp_disk_cache_create(screen);
943    return &screen->base;
944 }
945