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
60int LP_DEBUG = 0;
61
62static 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
86int LP_PERF = 0;
87static 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
102static const char *
103llvmpipe_get_vendor(struct pipe_screen *screen)
104{
105   return "Mesa/X.org";
106}
107
108
109static const char *
110llvmpipe_get_name(struct pipe_screen *screen)
111{
112   struct llvmpipe_screen *lscreen = llvmpipe_screen(screen);
113   return lscreen->renderer_string;
114}
115
116
117static int
118llvmpipe_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
373static int
374llvmpipe_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
432static float
433llvmpipe_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
460static int
461llvmpipe_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
563static void
564llvmpipe_get_driver_uuid(struct pipe_screen *pscreen, char *uuid)
565{
566   memset(uuid, 0, PIPE_UUID_SIZE);
567}
568
569static void
570llvmpipe_get_device_uuid(struct pipe_screen *pscreen, char *uuid)
571{
572   memset(uuid, 0, PIPE_UUID_SIZE);
573}
574
575static const struct nir_shader_compiler_options gallivm_nir_options = {
576   .lower_scmp = true,
577   .lower_flrp32 = true,
578   .lower_flrp64 = true,
579   .lower_fsat = true,
580   .lower_bitfield_insert_to_shifts = true,
581   .lower_bitfield_extract_to_shifts = true,
582   .lower_fdot = true,
583   .lower_fdph = true,
584   .lower_ffma16 = true,
585   .lower_ffma32 = true,
586   .lower_ffma64 = true,
587   .lower_flrp16 = true,
588   .lower_fmod = true,
589   .lower_hadd = true,
590   .lower_uadd_sat = true,
591   .lower_iadd_sat = true,
592   .lower_ldexp = true,
593   .lower_pack_snorm_2x16 = true,
594   .lower_pack_snorm_4x8 = true,
595   .lower_pack_unorm_2x16 = true,
596   .lower_pack_unorm_4x8 = true,
597   .lower_pack_half_2x16 = true,
598   .lower_pack_split = true,
599   .lower_unpack_snorm_2x16 = true,
600   .lower_unpack_snorm_4x8 = true,
601   .lower_unpack_unorm_2x16 = true,
602   .lower_unpack_unorm_4x8 = true,
603   .lower_unpack_half_2x16 = true,
604   .lower_extract_byte = true,
605   .lower_extract_word = true,
606   .lower_insert_byte = true,
607   .lower_insert_word = true,
608   .lower_rotate = true,
609   .lower_uadd_carry = true,
610   .lower_usub_borrow = true,
611   .lower_mul_2x32_64 = true,
612   .lower_ifind_msb = true,
613   .max_unroll_iterations = 32,
614   .use_interpolated_input_intrinsics = true,
615   .lower_to_scalar = true,
616   .lower_uniforms_to_ubo = true,
617   .lower_vector_cmp = true,
618   .lower_device_index_to_zero = true,
619   .support_16bit_alu = true,
620   .lower_fisnormal = true,
621};
622
623static char *
624llvmpipe_finalize_nir(struct pipe_screen *screen,
625                      void *nirptr)
626{
627   struct nir_shader *nir = (struct nir_shader *)nirptr;
628   lp_build_opt_nir(nir);
629   return NULL;
630}
631
632static inline const void *
633llvmpipe_get_compiler_options(struct pipe_screen *screen,
634                              enum pipe_shader_ir ir,
635                              enum pipe_shader_type shader)
636{
637   assert(ir == PIPE_SHADER_IR_NIR);
638   return &gallivm_nir_options;
639}
640
641/**
642 * Query format support for creating a texture, drawing surface, etc.
643 * \param format  the format to test
644 * \param type  one of PIPE_TEXTURE, PIPE_SURFACE
645 */
646static bool
647llvmpipe_is_format_supported( struct pipe_screen *_screen,
648                              enum pipe_format format,
649                              enum pipe_texture_target target,
650                              unsigned sample_count,
651                              unsigned storage_sample_count,
652                              unsigned bind)
653{
654   struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
655   struct sw_winsys *winsys = screen->winsys;
656   const struct util_format_description *format_desc;
657
658   format_desc = util_format_description(format);
659   if (!format_desc)
660      return false;
661
662   assert(target == PIPE_BUFFER ||
663          target == PIPE_TEXTURE_1D ||
664          target == PIPE_TEXTURE_1D_ARRAY ||
665          target == PIPE_TEXTURE_2D ||
666          target == PIPE_TEXTURE_2D_ARRAY ||
667          target == PIPE_TEXTURE_RECT ||
668          target == PIPE_TEXTURE_3D ||
669          target == PIPE_TEXTURE_CUBE ||
670          target == PIPE_TEXTURE_CUBE_ARRAY);
671
672   if (sample_count != 0 && sample_count != 1 && sample_count != 4)
673      return false;
674
675   if (MAX2(1, sample_count) != MAX2(1, storage_sample_count))
676      return false;
677
678   if (bind & (PIPE_BIND_RENDER_TARGET | PIPE_BIND_SHADER_IMAGE)) {
679      if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
680         /* this is a lie actually other formats COULD exist where we would fail */
681         if (format_desc->nr_channels < 3)
682            return false;
683      }
684      else if (format_desc->colorspace != UTIL_FORMAT_COLORSPACE_RGB)
685         return false;
686
687      if (format_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN &&
688          format != PIPE_FORMAT_R11G11B10_FLOAT)
689         return false;
690
691      assert(format_desc->block.width == 1);
692      assert(format_desc->block.height == 1);
693
694      if (format_desc->is_mixed)
695         return false;
696
697      if (!format_desc->is_array && !format_desc->is_bitmask &&
698          format != PIPE_FORMAT_R11G11B10_FLOAT)
699         return false;
700   }
701
702   if (bind & PIPE_BIND_SHADER_IMAGE) {
703      switch (format) {
704         case PIPE_FORMAT_R32G32B32A32_FLOAT:
705         case PIPE_FORMAT_R16G16B16A16_FLOAT:
706         case PIPE_FORMAT_R32G32_FLOAT:
707         case PIPE_FORMAT_R16G16_FLOAT:
708         case PIPE_FORMAT_R11G11B10_FLOAT:
709         case PIPE_FORMAT_R32_FLOAT:
710         case PIPE_FORMAT_R16_FLOAT:
711         case PIPE_FORMAT_R32G32B32A32_UINT:
712         case PIPE_FORMAT_R16G16B16A16_UINT:
713         case PIPE_FORMAT_R10G10B10A2_UINT:
714         case PIPE_FORMAT_R8G8B8A8_UINT:
715         case PIPE_FORMAT_R32G32_UINT:
716         case PIPE_FORMAT_R16G16_UINT:
717         case PIPE_FORMAT_R8G8_UINT:
718         case PIPE_FORMAT_R32_UINT:
719         case PIPE_FORMAT_R16_UINT:
720         case PIPE_FORMAT_R8_UINT:
721         case PIPE_FORMAT_R32G32B32A32_SINT:
722         case PIPE_FORMAT_R16G16B16A16_SINT:
723         case PIPE_FORMAT_R8G8B8A8_SINT:
724         case PIPE_FORMAT_R32G32_SINT:
725         case PIPE_FORMAT_R16G16_SINT:
726         case PIPE_FORMAT_R8G8_SINT:
727         case PIPE_FORMAT_R32_SINT:
728         case PIPE_FORMAT_R16_SINT:
729         case PIPE_FORMAT_R8_SINT:
730         case PIPE_FORMAT_R16G16B16A16_UNORM:
731         case PIPE_FORMAT_R10G10B10A2_UNORM:
732         case PIPE_FORMAT_R8G8B8A8_UNORM:
733         case PIPE_FORMAT_R16G16_UNORM:
734         case PIPE_FORMAT_R8G8_UNORM:
735         case PIPE_FORMAT_R16_UNORM:
736         case PIPE_FORMAT_R8_UNORM:
737         case PIPE_FORMAT_R16G16B16A16_SNORM:
738         case PIPE_FORMAT_R8G8B8A8_SNORM:
739         case PIPE_FORMAT_R16G16_SNORM:
740         case PIPE_FORMAT_R8G8_SNORM:
741         case PIPE_FORMAT_R16_SNORM:
742         case PIPE_FORMAT_R8_SNORM:
743            break;
744
745         default:
746            return false;
747      }
748   }
749
750   if ((bind & (PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW)) &&
751       ((bind & PIPE_BIND_DISPLAY_TARGET) == 0)) {
752      /* Disable all 3-channel formats, where channel size != 32 bits.
753       * In some cases we run into crashes (in generate_unswizzled_blend()),
754       * for 3-channel RGB16 variants, there was an apparent LLVM bug.
755       * In any case, disabling the shallower 3-channel formats avoids a
756       * number of issues with GL_ARB_copy_image support.
757       */
758      if (format_desc->is_array &&
759          format_desc->nr_channels == 3 &&
760          format_desc->block.bits != 96) {
761         return false;
762      }
763
764      /* Disable 64-bit integer formats for RT/samplers.
765       * VK CTS crashes with these and they don't make much sense.
766       */
767      int c = util_format_get_first_non_void_channel(format_desc->format);
768      if (c >= 0) {
769         if (format_desc->channel[c].pure_integer && format_desc->channel[c].size == 64)
770            return false;
771      }
772
773   }
774
775   if (!(bind & PIPE_BIND_VERTEX_BUFFER) &&
776       util_format_is_scaled(format))
777      return false;
778
779   if (bind & PIPE_BIND_DISPLAY_TARGET) {
780      if(!winsys->is_displaytarget_format_supported(winsys, bind, format))
781         return false;
782   }
783
784   if (bind & PIPE_BIND_DEPTH_STENCIL) {
785      if (format_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
786         return false;
787
788      if (format_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS)
789         return false;
790   }
791
792   if (format_desc->layout == UTIL_FORMAT_LAYOUT_ASTC ||
793       format_desc->layout == UTIL_FORMAT_LAYOUT_ATC) {
794      /* Software decoding is not hooked up. */
795      return false;
796   }
797
798   if (format_desc->layout == UTIL_FORMAT_LAYOUT_ETC &&
799       format != PIPE_FORMAT_ETC1_RGB8)
800      return false;
801
802   /*
803    * Everything can be supported by u_format
804    * (those without fetch_rgba_float might be not but shouldn't hit that)
805    */
806
807   return true;
808}
809
810
811
812
813static void
814llvmpipe_flush_frontbuffer(struct pipe_screen *_screen,
815                           struct pipe_context *_pipe,
816                           struct pipe_resource *resource,
817                           unsigned level, unsigned layer,
818                           void *context_private,
819                           struct pipe_box *sub_box)
820{
821   struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
822   struct sw_winsys *winsys = screen->winsys;
823   struct llvmpipe_resource *texture = llvmpipe_resource(resource);
824
825   assert(texture->dt);
826   if (texture->dt)
827      winsys->displaytarget_display(winsys, texture->dt, context_private, sub_box);
828}
829
830static void
831llvmpipe_destroy_screen( struct pipe_screen *_screen )
832{
833   struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
834   struct sw_winsys *winsys = screen->winsys;
835
836   if (screen->cs_tpool)
837      lp_cs_tpool_destroy(screen->cs_tpool);
838
839   if (screen->rast)
840      lp_rast_destroy(screen->rast);
841
842   lp_jit_screen_cleanup(screen);
843
844   if (LP_DEBUG & DEBUG_CACHE_STATS)
845      printf("disk shader cache:   hits = %u, misses = %u\n", screen->num_disk_shader_cache_hits,
846             screen->num_disk_shader_cache_misses);
847   disk_cache_destroy(screen->disk_shader_cache);
848   if(winsys->destroy)
849      winsys->destroy(winsys);
850
851   glsl_type_singleton_decref();
852
853   mtx_destroy(&screen->rast_mutex);
854   mtx_destroy(&screen->cs_mutex);
855   FREE(screen);
856}
857
858
859
860
861/**
862 * Fence reference counting.
863 */
864static void
865llvmpipe_fence_reference(struct pipe_screen *screen,
866                         struct pipe_fence_handle **ptr,
867                         struct pipe_fence_handle *fence)
868{
869   struct lp_fence **old = (struct lp_fence **) ptr;
870   struct lp_fence *f = (struct lp_fence *) fence;
871
872   lp_fence_reference(old, f);
873}
874
875
876/**
877 * Wait for the fence to finish.
878 */
879static bool
880llvmpipe_fence_finish(struct pipe_screen *screen,
881                      struct pipe_context *ctx,
882                      struct pipe_fence_handle *fence_handle,
883                      uint64_t timeout)
884{
885   struct lp_fence *f = (struct lp_fence *) fence_handle;
886
887   if (!timeout)
888      return lp_fence_signalled(f);
889
890   if (!lp_fence_signalled(f)) {
891      if (timeout != PIPE_TIMEOUT_INFINITE)
892         return lp_fence_timedwait(f, timeout);
893
894      lp_fence_wait(f);
895   }
896   return true;
897}
898
899static uint64_t
900llvmpipe_get_timestamp(struct pipe_screen *_screen)
901{
902   return os_time_get_nano();
903}
904
905static void update_cache_sha1_cpu(struct mesa_sha1 *ctx)
906{
907   const struct util_cpu_caps_t *cpu_caps = util_get_cpu_caps();
908   /*
909    * Don't need the cpu cache affinity stuff. The rest
910    * is contained in first 5 dwords.
911    */
912   STATIC_ASSERT(offsetof(struct util_cpu_caps_t, num_L3_caches) == 5 * sizeof(uint32_t));
913   _mesa_sha1_update(ctx, cpu_caps, 5 * sizeof(uint32_t));
914}
915
916static void lp_disk_cache_create(struct llvmpipe_screen *screen)
917{
918   struct mesa_sha1 ctx;
919   unsigned gallivm_perf = gallivm_get_perf_flags();
920   unsigned char sha1[20];
921   char cache_id[20 * 2 + 1];
922   _mesa_sha1_init(&ctx);
923
924   if (!disk_cache_get_function_identifier(lp_disk_cache_create, &ctx) ||
925       !disk_cache_get_function_identifier(LLVMLinkInMCJIT, &ctx))
926      return;
927
928   _mesa_sha1_update(&ctx, &gallivm_perf, sizeof(gallivm_perf));
929   update_cache_sha1_cpu(&ctx);
930   _mesa_sha1_final(&ctx, sha1);
931   disk_cache_format_hex_id(cache_id, sha1, 20 * 2);
932
933   screen->disk_shader_cache = disk_cache_create("llvmpipe", cache_id, 0);
934}
935
936static struct disk_cache *lp_get_disk_shader_cache(struct pipe_screen *_screen)
937{
938   struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
939
940   return screen->disk_shader_cache;
941}
942
943void lp_disk_cache_find_shader(struct llvmpipe_screen *screen,
944                               struct lp_cached_code *cache,
945                               unsigned char ir_sha1_cache_key[20])
946{
947   unsigned char sha1[CACHE_KEY_SIZE];
948
949   if (!screen->disk_shader_cache)
950      return;
951   disk_cache_compute_key(screen->disk_shader_cache, ir_sha1_cache_key, 20, sha1);
952
953   size_t binary_size;
954   uint8_t *buffer = disk_cache_get(screen->disk_shader_cache, sha1, &binary_size);
955   if (!buffer) {
956      cache->data_size = 0;
957      p_atomic_inc(&screen->num_disk_shader_cache_misses);
958      return;
959   }
960   cache->data_size = binary_size;
961   cache->data = buffer;
962   p_atomic_inc(&screen->num_disk_shader_cache_hits);
963}
964
965void lp_disk_cache_insert_shader(struct llvmpipe_screen *screen,
966                                 struct lp_cached_code *cache,
967                                 unsigned char ir_sha1_cache_key[20])
968{
969   unsigned char sha1[CACHE_KEY_SIZE];
970
971   if (!screen->disk_shader_cache || !cache->data_size || cache->dont_cache)
972      return;
973   disk_cache_compute_key(screen->disk_shader_cache, ir_sha1_cache_key, 20, sha1);
974   disk_cache_put(screen->disk_shader_cache, sha1, cache->data, cache->data_size, NULL);
975}
976
977bool
978llvmpipe_screen_late_init(struct llvmpipe_screen *screen)
979{
980   bool ret = true;
981   mtx_lock(&screen->late_mutex);
982
983   if (screen->late_init_done)
984      goto out;
985
986   screen->rast = lp_rast_create(screen->num_threads);
987   if (!screen->rast) {
988      ret = false;
989      goto out;
990   }
991
992   screen->cs_tpool = lp_cs_tpool_create(screen->num_threads);
993   if (!screen->cs_tpool) {
994      lp_rast_destroy(screen->rast);
995      ret = false;
996      goto out;
997   }
998
999   lp_disk_cache_create(screen);
1000   screen->late_init_done = true;
1001out:
1002   mtx_unlock(&screen->late_mutex);
1003   return ret;
1004}
1005
1006/**
1007 * Create a new pipe_screen object
1008 * Note: we're not presently subclassing pipe_screen (no llvmpipe_screen).
1009 */
1010struct pipe_screen *
1011llvmpipe_create_screen(struct sw_winsys *winsys)
1012{
1013   struct llvmpipe_screen *screen;
1014
1015   util_cpu_detect();
1016
1017   glsl_type_singleton_init_or_ref();
1018
1019#ifdef DEBUG
1020   LP_DEBUG = debug_get_flags_option("LP_DEBUG", lp_debug_flags, 0 );
1021#endif
1022
1023   LP_PERF = debug_get_flags_option("LP_PERF", lp_perf_flags, 0 );
1024
1025   screen = CALLOC_STRUCT(llvmpipe_screen);
1026   if (!screen)
1027      return NULL;
1028
1029   if (!lp_jit_screen_init(screen)) {
1030      FREE(screen);
1031      return NULL;
1032   }
1033
1034   screen->winsys = winsys;
1035
1036   screen->base.destroy = llvmpipe_destroy_screen;
1037
1038   screen->base.get_name = llvmpipe_get_name;
1039   screen->base.get_vendor = llvmpipe_get_vendor;
1040   screen->base.get_device_vendor = llvmpipe_get_vendor; // TODO should be the CPU vendor
1041   screen->base.get_param = llvmpipe_get_param;
1042   screen->base.get_shader_param = llvmpipe_get_shader_param;
1043   screen->base.get_compute_param = llvmpipe_get_compute_param;
1044   screen->base.get_paramf = llvmpipe_get_paramf;
1045   screen->base.get_compiler_options = llvmpipe_get_compiler_options;
1046   screen->base.is_format_supported = llvmpipe_is_format_supported;
1047
1048   screen->base.context_create = llvmpipe_create_context;
1049   screen->base.flush_frontbuffer = llvmpipe_flush_frontbuffer;
1050   screen->base.fence_reference = llvmpipe_fence_reference;
1051   screen->base.fence_finish = llvmpipe_fence_finish;
1052
1053   screen->base.get_timestamp = llvmpipe_get_timestamp;
1054
1055   screen->base.get_driver_uuid = llvmpipe_get_driver_uuid;
1056   screen->base.get_device_uuid = llvmpipe_get_device_uuid;
1057
1058   screen->base.finalize_nir = llvmpipe_finalize_nir;
1059
1060   screen->base.get_disk_shader_cache = lp_get_disk_shader_cache;
1061   llvmpipe_init_screen_resource_funcs(&screen->base);
1062
1063   screen->allow_cl = !!getenv("LP_CL");
1064   screen->use_tgsi = (LP_DEBUG & DEBUG_TGSI_IR);
1065   screen->num_threads = util_get_cpu_caps()->nr_cpus > 1 ? util_get_cpu_caps()->nr_cpus : 0;
1066#ifdef EMBEDDED_DEVICE
1067   screen->num_threads = MIN2(screen->num_threads, 2);
1068#endif
1069   screen->num_threads = debug_get_num_option("LP_NUM_THREADS", screen->num_threads);
1070   screen->num_threads = MIN2(screen->num_threads, LP_MAX_THREADS);
1071
1072   lp_build_init(); /* get lp_native_vector_width initialised */
1073
1074   snprintf(screen->renderer_string, sizeof(screen->renderer_string), "llvmpipe (LLVM " MESA_LLVM_VERSION_STRING ", %u bits)", lp_native_vector_width );
1075
1076   (void) mtx_init(&screen->cs_mutex, mtx_plain);
1077   (void) mtx_init(&screen->rast_mutex, mtx_plain);
1078
1079   (void) mtx_init(&screen->late_mutex, mtx_plain);
1080
1081   return &screen->base;
1082}
1083