mesa/soft/llvmpipe: add fake MSAA support
[mesa.git] / src / gallium / drivers / llvmpipe / lp_screen.c
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/u_format.h"
33 #include "util/u_string.h"
34 #include "util/u_format_s3tc.h"
35 #include "pipe/p_defines.h"
36 #include "pipe/p_screen.h"
37 #include "draw/draw_context.h"
38 #include "gallivm/lp_bld_type.h"
39
40 #include "os/os_time.h"
41 #include "lp_texture.h"
42 #include "lp_fence.h"
43 #include "lp_jit.h"
44 #include "lp_screen.h"
45 #include "lp_context.h"
46 #include "lp_debug.h"
47 #include "lp_public.h"
48 #include "lp_limits.h"
49 #include "lp_rast.h"
50
51 #include "state_tracker/sw_winsys.h"
52
53 #ifdef DEBUG
54 int LP_DEBUG = 0;
55
56 static const struct debug_named_value lp_debug_flags[] = {
57 { "pipe", DEBUG_PIPE, NULL },
58 { "tgsi", DEBUG_TGSI, NULL },
59 { "tex", DEBUG_TEX, NULL },
60 { "setup", DEBUG_SETUP, NULL },
61 { "rast", DEBUG_RAST, NULL },
62 { "query", DEBUG_QUERY, NULL },
63 { "screen", DEBUG_SCREEN, NULL },
64 { "counters", DEBUG_COUNTERS, NULL },
65 { "scene", DEBUG_SCENE, NULL },
66 { "fence", DEBUG_FENCE, NULL },
67 { "mem", DEBUG_MEM, NULL },
68 { "fs", DEBUG_FS, NULL },
69 DEBUG_NAMED_VALUE_END
70 };
71 #endif
72
73 int LP_PERF = 0;
74 static const struct debug_named_value lp_perf_flags[] = {
75 { "texmem", PERF_TEX_MEM, NULL },
76 { "no_mipmap", PERF_NO_MIPMAPS, NULL },
77 { "no_linear", PERF_NO_LINEAR, NULL },
78 { "no_mip_linear", PERF_NO_MIP_LINEAR, NULL },
79 { "no_tex", PERF_NO_TEX, NULL },
80 { "no_blend", PERF_NO_BLEND, NULL },
81 { "no_depth", PERF_NO_DEPTH, NULL },
82 { "no_alphatest", PERF_NO_ALPHATEST, NULL },
83 DEBUG_NAMED_VALUE_END
84 };
85
86
87 static const char *
88 llvmpipe_get_vendor(struct pipe_screen *screen)
89 {
90 return "VMware, Inc.";
91 }
92
93
94 static const char *
95 llvmpipe_get_name(struct pipe_screen *screen)
96 {
97 static char buf[100];
98 util_snprintf(buf, sizeof(buf), "llvmpipe (LLVM %u.%u, %u bits)",
99 HAVE_LLVM >> 8, HAVE_LLVM & 0xff,
100 lp_native_vector_width );
101 return buf;
102 }
103
104
105 static int
106 llvmpipe_get_param(struct pipe_screen *screen, enum pipe_cap param)
107 {
108 switch (param) {
109 case PIPE_CAP_NPOT_TEXTURES:
110 case PIPE_CAP_MIXED_FRAMEBUFFER_SIZES:
111 return 1;
112 case PIPE_CAP_TWO_SIDED_STENCIL:
113 return 1;
114 case PIPE_CAP_SM3:
115 return 1;
116 case PIPE_CAP_MAX_DUAL_SOURCE_RENDER_TARGETS:
117 return 1;
118 case PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS:
119 return PIPE_MAX_SO_BUFFERS;
120 case PIPE_CAP_ANISOTROPIC_FILTER:
121 return 0;
122 case PIPE_CAP_POINT_SPRITE:
123 return 1;
124 case PIPE_CAP_MAX_RENDER_TARGETS:
125 return PIPE_MAX_COLOR_BUFS;
126 case PIPE_CAP_OCCLUSION_QUERY:
127 return 1;
128 case PIPE_CAP_QUERY_TIME_ELAPSED:
129 return 0;
130 case PIPE_CAP_QUERY_TIMESTAMP:
131 return 1;
132 case PIPE_CAP_QUERY_PIPELINE_STATISTICS:
133 return 0;
134 case PIPE_CAP_TEXTURE_MIRROR_CLAMP:
135 return 1;
136 case PIPE_CAP_TEXTURE_SHADOW_MAP:
137 return 1;
138 case PIPE_CAP_TEXTURE_SWIZZLE:
139 return 1;
140 case PIPE_CAP_TEXTURE_BORDER_COLOR_QUIRK:
141 return 0;
142 case PIPE_CAP_MAX_TEXTURE_2D_LEVELS:
143 return LP_MAX_TEXTURE_2D_LEVELS;
144 case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
145 return LP_MAX_TEXTURE_3D_LEVELS;
146 case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
147 return LP_MAX_TEXTURE_CUBE_LEVELS;
148 case PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS:
149 return LP_MAX_TEXTURE_ARRAY_LAYERS;
150 case PIPE_CAP_BLEND_EQUATION_SEPARATE:
151 return 1;
152 case PIPE_CAP_INDEP_BLEND_ENABLE:
153 return 1;
154 case PIPE_CAP_INDEP_BLEND_FUNC:
155 return 1;
156 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT:
157 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER:
158 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER:
159 return 1;
160 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT:
161 return 0;
162 case PIPE_CAP_PRIMITIVE_RESTART:
163 return 1;
164 case PIPE_CAP_DEPTH_CLIP_DISABLE:
165 return 1;
166 case PIPE_CAP_SHADER_STENCIL_EXPORT:
167 return 0;
168 case PIPE_CAP_TGSI_INSTANCEID:
169 case PIPE_CAP_VERTEX_ELEMENT_INSTANCE_DIVISOR:
170 return 1;
171 case PIPE_CAP_FRAGMENT_COLOR_CLAMPED:
172 return 0;
173 case PIPE_CAP_MIXED_COLORBUFFER_FORMATS:
174 return 1;
175 case PIPE_CAP_SEAMLESS_CUBE_MAP:
176 case PIPE_CAP_SEAMLESS_CUBE_MAP_PER_TEXTURE:
177 return 1;
178 /* this is a lie could support arbitrary large offsets */
179 case PIPE_CAP_MIN_TEXEL_OFFSET:
180 return -8;
181 case PIPE_CAP_MAX_TEXEL_OFFSET:
182 return 7;
183 case PIPE_CAP_CONDITIONAL_RENDER:
184 return 1;
185 case PIPE_CAP_TEXTURE_BARRIER:
186 return 0;
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_STREAM_OUTPUT_PAUSE_RESUME:
194 return 1;
195 case PIPE_CAP_TGSI_CAN_COMPACT_CONSTANTS:
196 return 0;
197 case PIPE_CAP_VERTEX_COLOR_UNCLAMPED:
198 case PIPE_CAP_VERTEX_COLOR_CLAMPED:
199 return 1;
200 case PIPE_CAP_GLSL_FEATURE_LEVEL:
201 return 140;
202 case PIPE_CAP_QUADS_FOLLOW_PROVOKING_VERTEX_CONVENTION:
203 return 0;
204 case PIPE_CAP_COMPUTE:
205 return 0;
206 case PIPE_CAP_USER_VERTEX_BUFFERS:
207 case PIPE_CAP_USER_INDEX_BUFFERS:
208 return 1;
209 case PIPE_CAP_USER_CONSTANT_BUFFERS:
210 return 0;
211 case PIPE_CAP_VERTEX_BUFFER_OFFSET_4BYTE_ALIGNED_ONLY:
212 case PIPE_CAP_VERTEX_BUFFER_STRIDE_4BYTE_ALIGNED_ONLY:
213 case PIPE_CAP_VERTEX_ELEMENT_SRC_OFFSET_4BYTE_ALIGNED_ONLY:
214 case PIPE_CAP_TGSI_TEXCOORD:
215 return 0;
216
217 case PIPE_CAP_CONSTANT_BUFFER_OFFSET_ALIGNMENT:
218 return 16;
219 case PIPE_CAP_START_INSTANCE:
220 case PIPE_CAP_TEXTURE_MULTISAMPLE:
221 case PIPE_CAP_CUBE_MAP_ARRAY:
222 return 0;
223 case PIPE_CAP_MIN_MAP_BUFFER_ALIGNMENT:
224 return 64;
225 case PIPE_CAP_TEXTURE_BUFFER_OBJECTS:
226 return 1;
227 case PIPE_CAP_MAX_TEXTURE_BUFFER_SIZE:
228 return 65536;
229 case PIPE_CAP_TEXTURE_BUFFER_OFFSET_ALIGNMENT:
230 return 1;
231 case PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER:
232 return 0;
233 case PIPE_CAP_MAX_VIEWPORTS:
234 return PIPE_MAX_VIEWPORTS;
235 case PIPE_CAP_ENDIANNESS:
236 return PIPE_ENDIAN_NATIVE;
237 case PIPE_CAP_TGSI_VS_LAYER:
238 case PIPE_CAP_MAX_TEXTURE_GATHER_COMPONENTS:
239 case PIPE_CAP_TEXTURE_GATHER_SM5:
240 case PIPE_CAP_BUFFER_MAP_PERSISTENT_COHERENT:
241 return 0;
242 case PIPE_CAP_FAKE_SW_MSAA:
243 return 1;
244 }
245 /* should only get here on unhandled cases */
246 debug_printf("Unexpected PIPE_CAP %d query\n", param);
247 return 0;
248 }
249
250 static int
251 llvmpipe_get_shader_param(struct pipe_screen *screen, unsigned shader, enum pipe_shader_cap param)
252 {
253 switch(shader)
254 {
255 case PIPE_SHADER_FRAGMENT:
256 switch (param) {
257 default:
258 return gallivm_get_shader_param(param);
259 }
260 case PIPE_SHADER_VERTEX:
261 case PIPE_SHADER_GEOMETRY:
262 switch (param) {
263 case PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS:
264 /* At this time, the draw module and llvmpipe driver only
265 * support vertex shader texture lookups when LLVM is enabled in
266 * the draw module.
267 */
268 if (debug_get_bool_option("DRAW_USE_LLVM", TRUE))
269 return PIPE_MAX_SAMPLERS;
270 else
271 return 0;
272 case PIPE_SHADER_CAP_MAX_SAMPLER_VIEWS:
273 if (debug_get_bool_option("DRAW_USE_LLVM", TRUE))
274 return PIPE_MAX_SHADER_SAMPLER_VIEWS;
275 else
276 return 0;
277 default:
278 return draw_get_shader_param(shader, param);
279 }
280 default:
281 return 0;
282 }
283 }
284
285 static float
286 llvmpipe_get_paramf(struct pipe_screen *screen, enum pipe_capf param)
287 {
288 switch (param) {
289 case PIPE_CAPF_MAX_LINE_WIDTH:
290 /* fall-through */
291 case PIPE_CAPF_MAX_LINE_WIDTH_AA:
292 return 255.0; /* arbitrary */
293 case PIPE_CAPF_MAX_POINT_WIDTH:
294 /* fall-through */
295 case PIPE_CAPF_MAX_POINT_WIDTH_AA:
296 return 255.0; /* arbitrary */
297 case PIPE_CAPF_MAX_TEXTURE_ANISOTROPY:
298 return 16.0; /* not actually signficant at this time */
299 case PIPE_CAPF_MAX_TEXTURE_LOD_BIAS:
300 return 16.0; /* arbitrary */
301 case PIPE_CAPF_GUARD_BAND_LEFT:
302 case PIPE_CAPF_GUARD_BAND_TOP:
303 case PIPE_CAPF_GUARD_BAND_RIGHT:
304 case PIPE_CAPF_GUARD_BAND_BOTTOM:
305 return 0.0;
306 }
307 /* should only get here on unhandled cases */
308 debug_printf("Unexpected PIPE_CAP %d query\n", param);
309 return 0.0;
310 }
311
312
313 /**
314 * Query format support for creating a texture, drawing surface, etc.
315 * \param format the format to test
316 * \param type one of PIPE_TEXTURE, PIPE_SURFACE
317 */
318 static boolean
319 llvmpipe_is_format_supported( struct pipe_screen *_screen,
320 enum pipe_format format,
321 enum pipe_texture_target target,
322 unsigned sample_count,
323 unsigned bind)
324 {
325 struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
326 struct sw_winsys *winsys = screen->winsys;
327 const struct util_format_description *format_desc;
328
329 format_desc = util_format_description(format);
330 if (!format_desc)
331 return FALSE;
332
333 assert(target == PIPE_BUFFER ||
334 target == PIPE_TEXTURE_1D ||
335 target == PIPE_TEXTURE_1D_ARRAY ||
336 target == PIPE_TEXTURE_2D ||
337 target == PIPE_TEXTURE_2D_ARRAY ||
338 target == PIPE_TEXTURE_RECT ||
339 target == PIPE_TEXTURE_3D ||
340 target == PIPE_TEXTURE_CUBE);
341
342 if (sample_count > 1)
343 return FALSE;
344
345 if (bind & PIPE_BIND_RENDER_TARGET) {
346 if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB) {
347 /* this is a lie actually other formats COULD exist where we would fail */
348 if (format_desc->nr_channels < 3)
349 return FALSE;
350 }
351 else if (format_desc->colorspace != UTIL_FORMAT_COLORSPACE_RGB)
352 return FALSE;
353
354 if (format_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN &&
355 format != PIPE_FORMAT_R11G11B10_FLOAT)
356 return FALSE;
357
358 assert(format_desc->block.width == 1);
359 assert(format_desc->block.height == 1);
360
361 if (format_desc->is_mixed)
362 return FALSE;
363
364 if (!format_desc->is_array && !format_desc->is_bitmask &&
365 format != PIPE_FORMAT_R11G11B10_FLOAT)
366 return FALSE;
367
368 /*
369 * XXX refuse formats known to crash in generate_unswizzled_blend().
370 * These include all 3-channel 24bit RGB8 variants, plus 48bit
371 * (except those using floats) 3-channel RGB16 variants (the latter
372 * seems to be more of a llvm bug though).
373 * The mesa state tracker only seems to use these for SINT/UINT formats.
374 */
375 if (format_desc->is_array && format_desc->nr_channels == 3) {
376 if (format_desc->block.bits == 24 || (format_desc->block.bits == 48 &&
377 !util_format_is_float(format))) {
378 return FALSE;
379 }
380 }
381 }
382
383 if (bind & PIPE_BIND_DISPLAY_TARGET) {
384 if(!winsys->is_displaytarget_format_supported(winsys, bind, format))
385 return FALSE;
386 }
387
388 if (bind & PIPE_BIND_DEPTH_STENCIL) {
389 if (format_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
390 return FALSE;
391
392 if (format_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS)
393 return FALSE;
394
395 /* TODO: Support stencil-only formats */
396 if (format_desc->swizzle[0] == UTIL_FORMAT_SWIZZLE_NONE) {
397 return FALSE;
398 }
399 }
400
401 if (format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
402 return util_format_s3tc_enabled;
403 }
404
405 /*
406 * Everything can be supported by u_format
407 * (those without fetch_rgba_float might be not but shouldn't hit that)
408 */
409
410 return TRUE;
411 }
412
413
414
415
416 static void
417 llvmpipe_flush_frontbuffer(struct pipe_screen *_screen,
418 struct pipe_resource *resource,
419 unsigned level, unsigned layer,
420 void *context_private,
421 struct pipe_box *sub_box)
422 {
423 struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
424 struct sw_winsys *winsys = screen->winsys;
425 struct llvmpipe_resource *texture = llvmpipe_resource(resource);
426
427 assert(texture->dt);
428 if (texture->dt)
429 winsys->displaytarget_display(winsys, texture->dt, context_private, sub_box);
430 }
431
432 static void
433 llvmpipe_destroy_screen( struct pipe_screen *_screen )
434 {
435 struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
436 struct sw_winsys *winsys = screen->winsys;
437
438 if (screen->rast)
439 lp_rast_destroy(screen->rast);
440
441 lp_jit_screen_cleanup(screen);
442
443 if(winsys->destroy)
444 winsys->destroy(winsys);
445
446 pipe_mutex_destroy(screen->rast_mutex);
447
448 FREE(screen);
449 }
450
451
452
453
454 /**
455 * Fence reference counting.
456 */
457 static void
458 llvmpipe_fence_reference(struct pipe_screen *screen,
459 struct pipe_fence_handle **ptr,
460 struct pipe_fence_handle *fence)
461 {
462 struct lp_fence **old = (struct lp_fence **) ptr;
463 struct lp_fence *f = (struct lp_fence *) fence;
464
465 lp_fence_reference(old, f);
466 }
467
468
469 /**
470 * Has the fence been executed/finished?
471 */
472 static boolean
473 llvmpipe_fence_signalled(struct pipe_screen *screen,
474 struct pipe_fence_handle *fence)
475 {
476 struct lp_fence *f = (struct lp_fence *) fence;
477 return lp_fence_signalled(f);
478 }
479
480
481 /**
482 * Wait for the fence to finish.
483 */
484 static boolean
485 llvmpipe_fence_finish(struct pipe_screen *screen,
486 struct pipe_fence_handle *fence_handle,
487 uint64_t timeout)
488 {
489 struct lp_fence *f = (struct lp_fence *) fence_handle;
490
491 lp_fence_wait(f);
492 return TRUE;
493 }
494
495 static uint64_t
496 llvmpipe_get_timestamp(struct pipe_screen *_screen)
497 {
498 return os_time_get_nano();
499 }
500
501 /**
502 * Create a new pipe_screen object
503 * Note: we're not presently subclassing pipe_screen (no llvmpipe_screen).
504 */
505 struct pipe_screen *
506 llvmpipe_create_screen(struct sw_winsys *winsys)
507 {
508 struct llvmpipe_screen *screen;
509
510 util_cpu_detect();
511
512 #if defined(PIPE_ARCH_X86) && HAVE_LLVM < 0x0302
513 /* require SSE2 due to LLVM PR6960. */
514 if (!util_cpu_caps.has_sse2)
515 return NULL;
516 #endif
517
518 #ifdef DEBUG
519 LP_DEBUG = debug_get_flags_option("LP_DEBUG", lp_debug_flags, 0 );
520 #endif
521
522 LP_PERF = debug_get_flags_option("LP_PERF", lp_perf_flags, 0 );
523
524 screen = CALLOC_STRUCT(llvmpipe_screen);
525 if (!screen)
526 return NULL;
527
528 screen->winsys = winsys;
529
530 screen->base.destroy = llvmpipe_destroy_screen;
531
532 screen->base.get_name = llvmpipe_get_name;
533 screen->base.get_vendor = llvmpipe_get_vendor;
534 screen->base.get_param = llvmpipe_get_param;
535 screen->base.get_shader_param = llvmpipe_get_shader_param;
536 screen->base.get_paramf = llvmpipe_get_paramf;
537 screen->base.is_format_supported = llvmpipe_is_format_supported;
538
539 screen->base.context_create = llvmpipe_create_context;
540 screen->base.flush_frontbuffer = llvmpipe_flush_frontbuffer;
541 screen->base.fence_reference = llvmpipe_fence_reference;
542 screen->base.fence_signalled = llvmpipe_fence_signalled;
543 screen->base.fence_finish = llvmpipe_fence_finish;
544
545 screen->base.get_timestamp = llvmpipe_get_timestamp;
546
547 llvmpipe_init_screen_resource_funcs(&screen->base);
548
549 lp_jit_screen_init(screen);
550
551 screen->num_threads = util_cpu_caps.nr_cpus > 1 ? util_cpu_caps.nr_cpus : 0;
552 #ifdef PIPE_SUBSYSTEM_EMBEDDED
553 screen->num_threads = 0;
554 #endif
555 screen->num_threads = debug_get_num_option("LP_NUM_THREADS", screen->num_threads);
556 screen->num_threads = MIN2(screen->num_threads, LP_MAX_THREADS);
557
558 screen->rast = lp_rast_create(screen->num_threads);
559 if (!screen->rast) {
560 lp_jit_screen_cleanup(screen);
561 FREE(screen);
562 return NULL;
563 }
564 pipe_mutex_init(screen->rast_mutex);
565
566 util_format_s3tc_init();
567
568 return &screen->base;
569 }