7f0f17ed784682638511ee9f292674e73404b797
[mesa.git] / src / gallium / drivers / llvmpipe / lp_screen.c
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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
39 #include "lp_texture.h"
40 #include "lp_fence.h"
41 #include "lp_jit.h"
42 #include "lp_screen.h"
43 #include "lp_context.h"
44 #include "lp_debug.h"
45 #include "lp_public.h"
46 #include "lp_limits.h"
47 #include "lp_rast.h"
48
49 #include "state_tracker/sw_winsys.h"
50
51 #ifdef DEBUG
52 int LP_DEBUG = 0;
53
54 static const struct debug_named_value lp_debug_flags[] = {
55 { "pipe", DEBUG_PIPE, NULL },
56 { "tgsi", DEBUG_TGSI, NULL },
57 { "tex", DEBUG_TEX, NULL },
58 { "setup", DEBUG_SETUP, NULL },
59 { "rast", DEBUG_RAST, NULL },
60 { "query", DEBUG_QUERY, NULL },
61 { "screen", DEBUG_SCREEN, NULL },
62 { "show_tiles", DEBUG_SHOW_TILES, NULL },
63 { "show_subtiles", DEBUG_SHOW_SUBTILES, 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 0x%x)", HAVE_LLVM);
99 return buf;
100 }
101
102
103 static int
104 llvmpipe_get_param(struct pipe_screen *screen, enum pipe_cap param)
105 {
106 switch (param) {
107 case PIPE_CAP_MAX_COMBINED_SAMPLERS:
108 return PIPE_MAX_SAMPLERS + PIPE_MAX_VERTEX_SAMPLERS;
109 case PIPE_CAP_NPOT_TEXTURES:
110 return 1;
111 case PIPE_CAP_TWO_SIDED_STENCIL:
112 return 1;
113 case PIPE_CAP_SM3:
114 return 1;
115 case PIPE_CAP_ANISOTROPIC_FILTER:
116 return 0;
117 case PIPE_CAP_POINT_SPRITE:
118 return 1;
119 case PIPE_CAP_MAX_RENDER_TARGETS:
120 return PIPE_MAX_COLOR_BUFS;
121 case PIPE_CAP_OCCLUSION_QUERY:
122 return 1;
123 case PIPE_CAP_TIMER_QUERY:
124 return 0;
125 case PIPE_CAP_TEXTURE_MIRROR_CLAMP:
126 return 1;
127 case PIPE_CAP_TEXTURE_SHADOW_MAP:
128 return 1;
129 case PIPE_CAP_TEXTURE_SWIZZLE:
130 return 1;
131 case PIPE_CAP_MAX_TEXTURE_2D_LEVELS:
132 return LP_MAX_TEXTURE_2D_LEVELS;
133 case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
134 return LP_MAX_TEXTURE_3D_LEVELS;
135 case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
136 return LP_MAX_TEXTURE_2D_LEVELS;
137 case PIPE_CAP_BLEND_EQUATION_SEPARATE:
138 return 1;
139 case PIPE_CAP_INDEP_BLEND_ENABLE:
140 return 1;
141 case PIPE_CAP_INDEP_BLEND_FUNC:
142 return 1;
143 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT:
144 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER:
145 return 1;
146 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT:
147 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER:
148 return 0;
149 case PIPE_CAP_PRIMITIVE_RESTART:
150 return 1;
151 case PIPE_CAP_DEPTHSTENCIL_CLEAR_SEPARATE:
152 return 1;
153 case PIPE_CAP_DEPTH_CLIP_DISABLE:
154 return 0;
155 case PIPE_CAP_TGSI_INSTANCEID:
156 case PIPE_CAP_VERTEX_ELEMENT_INSTANCE_DIVISOR:
157 case PIPE_CAP_MIXED_COLORBUFFER_FORMATS:
158 case PIPE_CAP_CONDITIONAL_RENDER:
159 return 1;
160 case PIPE_CAP_QUADS_FOLLOW_PROVOKING_VERTEX_CONVENTION:
161 return 0;
162 default:
163 return 0;
164 }
165 }
166
167 static int
168 llvmpipe_get_shader_param(struct pipe_screen *screen, unsigned shader, enum pipe_shader_cap param)
169 {
170 switch(shader)
171 {
172 case PIPE_SHADER_FRAGMENT:
173 switch (param) {
174 case PIPE_SHADER_CAP_INTEGERS:
175 return 0;
176 default:
177 return tgsi_exec_get_shader_param(param);
178 }
179 case PIPE_SHADER_VERTEX:
180 case PIPE_SHADER_GEOMETRY:
181 switch (param) {
182 case PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS:
183 /* At this time, the draw module and llvmpipe driver only
184 * support vertex shader texture lookups when LLVM is enabled in
185 * the draw module.
186 */
187 if (debug_get_bool_option("DRAW_USE_LLVM", TRUE))
188 return PIPE_MAX_VERTEX_SAMPLERS;
189 else
190 return 0;
191 case PIPE_SHADER_CAP_INTEGERS:
192 return 0;
193 default:
194 return draw_get_shader_param(shader, param);
195 }
196 default:
197 return 0;
198 }
199 }
200
201 static float
202 llvmpipe_get_paramf(struct pipe_screen *screen, enum pipe_capf param)
203 {
204 switch (param) {
205 case PIPE_CAPF_MAX_LINE_WIDTH:
206 /* fall-through */
207 case PIPE_CAPF_MAX_LINE_WIDTH_AA:
208 return 255.0; /* arbitrary */
209 case PIPE_CAPF_MAX_POINT_WIDTH:
210 /* fall-through */
211 case PIPE_CAPF_MAX_POINT_WIDTH_AA:
212 return 255.0; /* arbitrary */
213 case PIPE_CAPF_MAX_TEXTURE_ANISOTROPY:
214 return 16.0; /* not actually signficant at this time */
215 case PIPE_CAPF_MAX_TEXTURE_LOD_BIAS:
216 return 16.0; /* arbitrary */
217 case PIPE_CAPF_GUARD_BAND_LEFT:
218 case PIPE_CAPF_GUARD_BAND_TOP:
219 case PIPE_CAPF_GUARD_BAND_RIGHT:
220 case PIPE_CAPF_GUARD_BAND_BOTTOM:
221 return 0.0;
222 default:
223 assert(0);
224 return 0;
225 }
226 }
227
228
229 /**
230 * Query format support for creating a texture, drawing surface, etc.
231 * \param format the format to test
232 * \param type one of PIPE_TEXTURE, PIPE_SURFACE
233 */
234 static boolean
235 llvmpipe_is_format_supported( struct pipe_screen *_screen,
236 enum pipe_format format,
237 enum pipe_texture_target target,
238 unsigned sample_count,
239 unsigned bind)
240 {
241 struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
242 struct sw_winsys *winsys = screen->winsys;
243 const struct util_format_description *format_desc;
244
245 format_desc = util_format_description(format);
246 if (!format_desc)
247 return FALSE;
248
249 assert(target == PIPE_BUFFER ||
250 target == PIPE_TEXTURE_1D ||
251 target == PIPE_TEXTURE_2D ||
252 target == PIPE_TEXTURE_RECT ||
253 target == PIPE_TEXTURE_3D ||
254 target == PIPE_TEXTURE_CUBE);
255
256 if (sample_count > 1)
257 return FALSE;
258
259 if (format_desc->format == PIPE_FORMAT_R11G11B10_FLOAT ||
260 format_desc->format == PIPE_FORMAT_R9G9B9E5_FLOAT)
261 return TRUE;
262
263 if (bind & PIPE_BIND_RENDER_TARGET) {
264 if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS ||
265 format_desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB)
266 return FALSE;
267
268 if (format_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
269 return FALSE;
270
271 if (format_desc->block.width != 1 ||
272 format_desc->block.height != 1)
273 return FALSE;
274 }
275
276 if (bind & PIPE_BIND_DISPLAY_TARGET) {
277 if(!winsys->is_displaytarget_format_supported(winsys, bind, format))
278 return FALSE;
279 }
280
281 if (bind & PIPE_BIND_DEPTH_STENCIL) {
282 if (format_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
283 return FALSE;
284
285 if (format_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS)
286 return FALSE;
287
288 /* FIXME: Temporary restriction. See lp_state_fs.c. */
289 if (format_desc->block.bits != 32)
290 return FALSE;
291 }
292
293 if (format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
294 return util_format_s3tc_enabled;
295 }
296
297 /*
298 * Everything can be supported by u_format.
299 */
300
301 if (format_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS &&
302 !format_desc->fetch_rgba_float) {
303 return FALSE;
304 }
305
306 return TRUE;
307 }
308
309
310
311
312 static void
313 llvmpipe_flush_frontbuffer(struct pipe_screen *_screen,
314 struct pipe_resource *resource,
315 unsigned level, unsigned layer,
316 void *context_private)
317 {
318 struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
319 struct sw_winsys *winsys = screen->winsys;
320 struct llvmpipe_resource *texture = llvmpipe_resource(resource);
321
322 assert(texture->dt);
323 if (texture->dt)
324 winsys->displaytarget_display(winsys, texture->dt, context_private);
325 }
326
327
328 static void
329 llvmpipe_destroy_screen( struct pipe_screen *_screen )
330 {
331 struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
332 struct sw_winsys *winsys = screen->winsys;
333
334 if (screen->rast)
335 lp_rast_destroy(screen->rast);
336
337 lp_jit_screen_cleanup(screen);
338
339 if(winsys->destroy)
340 winsys->destroy(winsys);
341
342 pipe_mutex_destroy(screen->rast_mutex);
343
344 FREE(screen);
345 }
346
347
348
349
350 /**
351 * Fence reference counting.
352 */
353 static void
354 llvmpipe_fence_reference(struct pipe_screen *screen,
355 struct pipe_fence_handle **ptr,
356 struct pipe_fence_handle *fence)
357 {
358 struct lp_fence **old = (struct lp_fence **) ptr;
359 struct lp_fence *f = (struct lp_fence *) fence;
360
361 lp_fence_reference(old, f);
362 }
363
364
365 /**
366 * Has the fence been executed/finished?
367 */
368 static boolean
369 llvmpipe_fence_signalled(struct pipe_screen *screen,
370 struct pipe_fence_handle *fence)
371 {
372 struct lp_fence *f = (struct lp_fence *) fence;
373 return lp_fence_signalled(f);
374 }
375
376
377 /**
378 * Wait for the fence to finish.
379 */
380 static boolean
381 llvmpipe_fence_finish(struct pipe_screen *screen,
382 struct pipe_fence_handle *fence_handle,
383 uint64_t timeout)
384 {
385 struct lp_fence *f = (struct lp_fence *) fence_handle;
386
387 lp_fence_wait(f);
388 return TRUE;
389 }
390
391
392
393 /**
394 * Create a new pipe_screen object
395 * Note: we're not presently subclassing pipe_screen (no llvmpipe_screen).
396 */
397 struct pipe_screen *
398 llvmpipe_create_screen(struct sw_winsys *winsys)
399 {
400 struct llvmpipe_screen *screen;
401
402 #ifdef PIPE_ARCH_X86
403 /* require SSE2 due to LLVM PR6960. */
404 util_cpu_detect();
405 if (!util_cpu_caps.has_sse2)
406 return NULL;
407 #endif
408
409 #ifdef DEBUG
410 LP_DEBUG = debug_get_flags_option("LP_DEBUG", lp_debug_flags, 0 );
411 #endif
412
413 LP_PERF = debug_get_flags_option("LP_PERF", lp_perf_flags, 0 );
414
415 screen = CALLOC_STRUCT(llvmpipe_screen);
416 if (!screen)
417 return NULL;
418
419 screen->winsys = winsys;
420
421 screen->base.destroy = llvmpipe_destroy_screen;
422
423 screen->base.get_name = llvmpipe_get_name;
424 screen->base.get_vendor = llvmpipe_get_vendor;
425 screen->base.get_param = llvmpipe_get_param;
426 screen->base.get_shader_param = llvmpipe_get_shader_param;
427 screen->base.get_paramf = llvmpipe_get_paramf;
428 screen->base.is_format_supported = llvmpipe_is_format_supported;
429
430 screen->base.context_create = llvmpipe_create_context;
431 screen->base.flush_frontbuffer = llvmpipe_flush_frontbuffer;
432 screen->base.fence_reference = llvmpipe_fence_reference;
433 screen->base.fence_signalled = llvmpipe_fence_signalled;
434 screen->base.fence_finish = llvmpipe_fence_finish;
435
436 llvmpipe_init_screen_resource_funcs(&screen->base);
437
438 lp_jit_screen_init(screen);
439
440 screen->num_threads = util_cpu_caps.nr_cpus > 1 ? util_cpu_caps.nr_cpus : 0;
441 #ifdef PIPE_SUBSYSTEM_EMBEDDED
442 screen->num_threads = 0;
443 #endif
444 screen->num_threads = debug_get_num_option("LP_NUM_THREADS", screen->num_threads);
445 screen->num_threads = MIN2(screen->num_threads, LP_MAX_THREADS);
446
447 screen->rast = lp_rast_create(screen->num_threads);
448 if (!screen->rast) {
449 lp_jit_screen_cleanup(screen);
450 FREE(screen);
451 return NULL;
452 }
453 pipe_mutex_init(screen->rast_mutex);
454
455 util_format_s3tc_init();
456
457 return &screen->base;
458 }