9d77cf8128e3114f0dbc0b71fac4b351c68fb533
[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_CLAMP:
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 default:
161 return 0;
162 }
163 }
164
165 static int
166 llvmpipe_get_shader_param(struct pipe_screen *screen, unsigned shader, enum pipe_shader_cap param)
167 {
168 switch(shader)
169 {
170 case PIPE_SHADER_FRAGMENT:
171 return tgsi_exec_get_shader_param(param);
172 case PIPE_SHADER_VERTEX:
173 case PIPE_SHADER_GEOMETRY:
174 switch (param) {
175 case PIPE_SHADER_CAP_MAX_TEXTURE_SAMPLERS:
176 /* At this time, the draw module and llvmpipe driver only
177 * support vertex shader texture lookups when LLVM is enabled in
178 * the draw module.
179 */
180 if (debug_get_bool_option("DRAW_USE_LLVM", TRUE))
181 return PIPE_MAX_VERTEX_SAMPLERS;
182 else
183 return 0;
184 default:
185 return draw_get_shader_param(shader, param);
186 }
187 default:
188 return 0;
189 }
190 }
191
192 static float
193 llvmpipe_get_paramf(struct pipe_screen *screen, enum pipe_cap param)
194 {
195 switch (param) {
196 case PIPE_CAP_MAX_LINE_WIDTH:
197 /* fall-through */
198 case PIPE_CAP_MAX_LINE_WIDTH_AA:
199 return 255.0; /* arbitrary */
200 case PIPE_CAP_MAX_POINT_WIDTH:
201 /* fall-through */
202 case PIPE_CAP_MAX_POINT_WIDTH_AA:
203 return 255.0; /* arbitrary */
204 case PIPE_CAP_MAX_TEXTURE_ANISOTROPY:
205 return 16.0; /* not actually signficant at this time */
206 case PIPE_CAP_MAX_TEXTURE_LOD_BIAS:
207 return 16.0; /* arbitrary */
208 case PIPE_CAP_GUARD_BAND_LEFT:
209 case PIPE_CAP_GUARD_BAND_TOP:
210 case PIPE_CAP_GUARD_BAND_RIGHT:
211 case PIPE_CAP_GUARD_BAND_BOTTOM:
212 return 0.0;
213 default:
214 assert(0);
215 return 0;
216 }
217 }
218
219
220 /**
221 * Query format support for creating a texture, drawing surface, etc.
222 * \param format the format to test
223 * \param type one of PIPE_TEXTURE, PIPE_SURFACE
224 */
225 static boolean
226 llvmpipe_is_format_supported( struct pipe_screen *_screen,
227 enum pipe_format format,
228 enum pipe_texture_target target,
229 unsigned sample_count,
230 unsigned bind)
231 {
232 struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
233 struct sw_winsys *winsys = screen->winsys;
234 const struct util_format_description *format_desc;
235
236 format_desc = util_format_description(format);
237 if (!format_desc)
238 return FALSE;
239
240 assert(target == PIPE_BUFFER ||
241 target == PIPE_TEXTURE_1D ||
242 target == PIPE_TEXTURE_2D ||
243 target == PIPE_TEXTURE_RECT ||
244 target == PIPE_TEXTURE_3D ||
245 target == PIPE_TEXTURE_CUBE);
246
247 if (sample_count > 1)
248 return FALSE;
249
250 if (format_desc->format == PIPE_FORMAT_R11G11B10_FLOAT ||
251 format_desc->format == PIPE_FORMAT_R9G9B9E5_FLOAT)
252 return TRUE;
253
254 if (bind & PIPE_BIND_RENDER_TARGET) {
255 if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS ||
256 format_desc->colorspace == UTIL_FORMAT_COLORSPACE_SRGB)
257 return FALSE;
258
259 if (format_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
260 return FALSE;
261
262 if (format_desc->block.width != 1 ||
263 format_desc->block.height != 1)
264 return FALSE;
265 }
266
267 if (bind & PIPE_BIND_DISPLAY_TARGET) {
268 if(!winsys->is_displaytarget_format_supported(winsys, bind, format))
269 return FALSE;
270 }
271
272 if (bind & PIPE_BIND_DEPTH_STENCIL) {
273 if (format_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
274 return FALSE;
275
276 if (format_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS)
277 return FALSE;
278
279 /* FIXME: Temporary restriction. See lp_state_fs.c. */
280 if (format_desc->block.bits != 32)
281 return FALSE;
282 }
283
284 if (format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
285 return util_format_s3tc_enabled;
286 }
287
288 /*
289 * Everything can be supported by u_format.
290 */
291
292 if (format_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS &&
293 !format_desc->fetch_rgba_float) {
294 return FALSE;
295 }
296
297 return TRUE;
298 }
299
300
301
302
303 static void
304 llvmpipe_flush_frontbuffer(struct pipe_screen *_screen,
305 struct pipe_resource *resource,
306 unsigned level, unsigned layer,
307 void *context_private)
308 {
309 struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
310 struct sw_winsys *winsys = screen->winsys;
311 struct llvmpipe_resource *texture = llvmpipe_resource(resource);
312
313 assert(texture->dt);
314 if (texture->dt)
315 winsys->displaytarget_display(winsys, texture->dt, context_private);
316 }
317
318
319 static void
320 llvmpipe_destroy_screen( struct pipe_screen *_screen )
321 {
322 struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
323 struct sw_winsys *winsys = screen->winsys;
324
325 if (screen->rast)
326 lp_rast_destroy(screen->rast);
327
328 lp_jit_screen_cleanup(screen);
329
330 if(winsys->destroy)
331 winsys->destroy(winsys);
332
333 pipe_mutex_destroy(screen->rast_mutex);
334
335 FREE(screen);
336 }
337
338
339
340
341 /**
342 * Fence reference counting.
343 */
344 static void
345 llvmpipe_fence_reference(struct pipe_screen *screen,
346 struct pipe_fence_handle **ptr,
347 struct pipe_fence_handle *fence)
348 {
349 struct lp_fence **old = (struct lp_fence **) ptr;
350 struct lp_fence *f = (struct lp_fence *) fence;
351
352 lp_fence_reference(old, f);
353 }
354
355
356 /**
357 * Has the fence been executed/finished?
358 */
359 static boolean
360 llvmpipe_fence_signalled(struct pipe_screen *screen,
361 struct pipe_fence_handle *fence)
362 {
363 struct lp_fence *f = (struct lp_fence *) fence;
364 return lp_fence_signalled(f);
365 }
366
367
368 /**
369 * Wait for the fence to finish.
370 */
371 static boolean
372 llvmpipe_fence_finish(struct pipe_screen *screen,
373 struct pipe_fence_handle *fence_handle,
374 uint64_t timeout)
375 {
376 struct lp_fence *f = (struct lp_fence *) fence_handle;
377
378 lp_fence_wait(f);
379 return TRUE;
380 }
381
382
383
384 /**
385 * Create a new pipe_screen object
386 * Note: we're not presently subclassing pipe_screen (no llvmpipe_screen).
387 */
388 struct pipe_screen *
389 llvmpipe_create_screen(struct sw_winsys *winsys)
390 {
391 struct llvmpipe_screen *screen;
392
393 #ifdef PIPE_ARCH_X86
394 /* require SSE2 due to LLVM PR6960. */
395 util_cpu_detect();
396 if (!util_cpu_caps.has_sse2)
397 return NULL;
398 #endif
399
400 #ifdef DEBUG
401 LP_DEBUG = debug_get_flags_option("LP_DEBUG", lp_debug_flags, 0 );
402 #endif
403
404 LP_PERF = debug_get_flags_option("LP_PERF", lp_perf_flags, 0 );
405
406 screen = CALLOC_STRUCT(llvmpipe_screen);
407 if (!screen)
408 return NULL;
409
410 screen->winsys = winsys;
411
412 screen->base.destroy = llvmpipe_destroy_screen;
413
414 screen->base.get_name = llvmpipe_get_name;
415 screen->base.get_vendor = llvmpipe_get_vendor;
416 screen->base.get_param = llvmpipe_get_param;
417 screen->base.get_shader_param = llvmpipe_get_shader_param;
418 screen->base.get_paramf = llvmpipe_get_paramf;
419 screen->base.is_format_supported = llvmpipe_is_format_supported;
420
421 screen->base.context_create = llvmpipe_create_context;
422 screen->base.flush_frontbuffer = llvmpipe_flush_frontbuffer;
423 screen->base.fence_reference = llvmpipe_fence_reference;
424 screen->base.fence_signalled = llvmpipe_fence_signalled;
425 screen->base.fence_finish = llvmpipe_fence_finish;
426
427 llvmpipe_init_screen_resource_funcs(&screen->base);
428
429 lp_jit_screen_init(screen);
430
431 screen->num_threads = util_cpu_caps.nr_cpus > 1 ? util_cpu_caps.nr_cpus : 0;
432 #ifdef PIPE_SUBSYSTEM_EMBEDDED
433 screen->num_threads = 0;
434 #endif
435 screen->num_threads = debug_get_num_option("LP_NUM_THREADS", screen->num_threads);
436 screen->num_threads = MIN2(screen->num_threads, LP_MAX_THREADS);
437
438 screen->rast = lp_rast_create(screen->num_threads);
439 if (!screen->rast) {
440 lp_jit_screen_cleanup(screen);
441 FREE(screen);
442 return NULL;
443 }
444 pipe_mutex_init(screen->rast_mutex);
445
446 util_format_s3tc_init();
447
448 return &screen->base;
449 }