Merge remote branch 'origin/master' into pipe-video
[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_format_s3tc.h"
34 #include "pipe/p_defines.h"
35 #include "pipe/p_screen.h"
36 #include "draw/draw_context.h"
37
38 #include "lp_texture.h"
39 #include "lp_fence.h"
40 #include "lp_jit.h"
41 #include "lp_screen.h"
42 #include "lp_context.h"
43 #include "lp_debug.h"
44 #include "lp_public.h"
45 #include "lp_limits.h"
46 #include "lp_rast.h"
47
48 #include "state_tracker/sw_winsys.h"
49
50 #ifdef DEBUG
51 int LP_DEBUG = 0;
52
53 static const struct debug_named_value lp_debug_flags[] = {
54 { "pipe", DEBUG_PIPE, NULL },
55 { "tgsi", DEBUG_TGSI, NULL },
56 { "tex", DEBUG_TEX, NULL },
57 { "setup", DEBUG_SETUP, NULL },
58 { "rast", DEBUG_RAST, NULL },
59 { "query", DEBUG_QUERY, NULL },
60 { "screen", DEBUG_SCREEN, NULL },
61 { "show_tiles", DEBUG_SHOW_TILES, NULL },
62 { "show_subtiles", DEBUG_SHOW_SUBTILES, NULL },
63 { "counters", DEBUG_COUNTERS, NULL },
64 { "scene", DEBUG_SCENE, NULL },
65 { "fence", DEBUG_FENCE, NULL },
66 { "mem", DEBUG_MEM, NULL },
67 { "fs", DEBUG_FS, NULL },
68 DEBUG_NAMED_VALUE_END
69 };
70 #endif
71
72 int LP_PERF = 0;
73 static const struct debug_named_value lp_perf_flags[] = {
74 { "texmem", PERF_TEX_MEM, NULL },
75 { "no_mipmap", PERF_NO_MIPMAPS, NULL },
76 { "no_linear", PERF_NO_LINEAR, NULL },
77 { "no_mip_linear", PERF_NO_MIP_LINEAR, NULL },
78 { "no_tex", PERF_NO_TEX, NULL },
79 { "no_blend", PERF_NO_BLEND, NULL },
80 { "no_depth", PERF_NO_DEPTH, NULL },
81 { "no_alphatest", PERF_NO_ALPHATEST, NULL },
82 DEBUG_NAMED_VALUE_END
83 };
84
85
86 static const char *
87 llvmpipe_get_vendor(struct pipe_screen *screen)
88 {
89 return "VMware, Inc.";
90 }
91
92
93 static const char *
94 llvmpipe_get_name(struct pipe_screen *screen)
95 {
96 return "llvmpipe";
97 }
98
99
100 static int
101 llvmpipe_get_param(struct pipe_screen *screen, enum pipe_cap param)
102 {
103 switch (param) {
104 case PIPE_CAP_MAX_TEXTURE_IMAGE_UNITS:
105 return PIPE_MAX_SAMPLERS;
106 case PIPE_CAP_MAX_VERTEX_TEXTURE_UNITS:
107 /* At this time, the draw module and llvmpipe driver only
108 * support vertex shader texture lookups when LLVM is enabled in
109 * the draw module.
110 */
111 if (debug_get_bool_option("DRAW_USE_LLVM", TRUE))
112 return PIPE_MAX_VERTEX_SAMPLERS;
113 else
114 return 0;
115 case PIPE_CAP_MAX_COMBINED_SAMPLERS:
116 return PIPE_MAX_SAMPLERS + PIPE_MAX_VERTEX_SAMPLERS;
117 case PIPE_CAP_NPOT_TEXTURES:
118 return 1;
119 case PIPE_CAP_TWO_SIDED_STENCIL:
120 return 1;
121 case PIPE_CAP_GLSL:
122 return 1;
123 case PIPE_CAP_SM3:
124 return 1;
125 case PIPE_CAP_ANISOTROPIC_FILTER:
126 return 0;
127 case PIPE_CAP_POINT_SPRITE:
128 return 1;
129 case PIPE_CAP_MAX_RENDER_TARGETS:
130 return PIPE_MAX_COLOR_BUFS;
131 case PIPE_CAP_OCCLUSION_QUERY:
132 return 1;
133 case PIPE_CAP_TIMER_QUERY:
134 return 0;
135 case PIPE_CAP_TEXTURE_MIRROR_CLAMP:
136 return 1;
137 case PIPE_CAP_TEXTURE_MIRROR_REPEAT:
138 return 1;
139 case PIPE_CAP_TEXTURE_SHADOW_MAP:
140 return 1;
141 case PIPE_CAP_TEXTURE_SWIZZLE:
142 return 1;
143 case PIPE_CAP_MAX_TEXTURE_2D_LEVELS:
144 return LP_MAX_TEXTURE_2D_LEVELS;
145 case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
146 return LP_MAX_TEXTURE_3D_LEVELS;
147 case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
148 return LP_MAX_TEXTURE_2D_LEVELS;
149 case PIPE_CAP_BLEND_EQUATION_SEPARATE:
150 return 1;
151 case PIPE_CAP_INDEP_BLEND_ENABLE:
152 return 1;
153 case PIPE_CAP_INDEP_BLEND_FUNC:
154 return 0;
155 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT:
156 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER:
157 return 1;
158 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT:
159 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER:
160 return 0;
161 case PIPE_CAP_PRIMITIVE_RESTART:
162 return 1;
163 case PIPE_CAP_DEPTHSTENCIL_CLEAR_SEPARATE:
164 return 1;
165 case PIPE_CAP_DEPTH_CLAMP:
166 return 0;
167 default:
168 return 0;
169 }
170 }
171
172 static int
173 llvmpipe_get_shader_param(struct pipe_screen *screen, unsigned shader, enum pipe_shader_cap param)
174 {
175 switch(shader)
176 {
177 case PIPE_SHADER_FRAGMENT:
178 return tgsi_exec_get_shader_param(param);
179 case PIPE_SHADER_VERTEX:
180 case PIPE_SHADER_GEOMETRY:
181 return draw_get_shader_param(shader, param);
182 default:
183 return 0;
184 }
185 }
186
187 static float
188 llvmpipe_get_paramf(struct pipe_screen *screen, enum pipe_cap param)
189 {
190 switch (param) {
191 case PIPE_CAP_MAX_LINE_WIDTH:
192 /* fall-through */
193 case PIPE_CAP_MAX_LINE_WIDTH_AA:
194 return 255.0; /* arbitrary */
195 case PIPE_CAP_MAX_POINT_WIDTH:
196 /* fall-through */
197 case PIPE_CAP_MAX_POINT_WIDTH_AA:
198 return 255.0; /* arbitrary */
199 case PIPE_CAP_MAX_TEXTURE_ANISOTROPY:
200 return 16.0; /* not actually signficant at this time */
201 case PIPE_CAP_MAX_TEXTURE_LOD_BIAS:
202 return 16.0; /* arbitrary */
203 case PIPE_CAP_GUARD_BAND_LEFT:
204 case PIPE_CAP_GUARD_BAND_TOP:
205 case PIPE_CAP_GUARD_BAND_RIGHT:
206 case PIPE_CAP_GUARD_BAND_BOTTOM:
207 return 0.0;
208 default:
209 assert(0);
210 return 0;
211 }
212 }
213
214
215 /**
216 * Query format support for creating a texture, drawing surface, etc.
217 * \param format the format to test
218 * \param type one of PIPE_TEXTURE, PIPE_SURFACE
219 */
220 static boolean
221 llvmpipe_is_format_supported( struct pipe_screen *_screen,
222 enum pipe_format format,
223 enum pipe_texture_target target,
224 unsigned sample_count,
225 unsigned bind,
226 unsigned geom_flags )
227 {
228 struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
229 struct sw_winsys *winsys = screen->winsys;
230 const struct util_format_description *format_desc;
231
232 format_desc = util_format_description(format);
233 if (!format_desc)
234 return FALSE;
235
236 assert(target == PIPE_BUFFER ||
237 target == PIPE_TEXTURE_1D ||
238 target == PIPE_TEXTURE_2D ||
239 target == PIPE_TEXTURE_RECT ||
240 target == PIPE_TEXTURE_3D ||
241 target == PIPE_TEXTURE_CUBE);
242
243 if (sample_count > 1)
244 return FALSE;
245
246 if (bind & PIPE_BIND_RENDER_TARGET) {
247 if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS)
248 return FALSE;
249
250 if (format_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
251 return FALSE;
252
253 if (format_desc->block.width != 1 ||
254 format_desc->block.height != 1)
255 return FALSE;
256 }
257
258 if (bind & PIPE_BIND_DISPLAY_TARGET) {
259 if(!winsys->is_displaytarget_format_supported(winsys, bind, format))
260 return FALSE;
261 }
262
263 if (bind & PIPE_BIND_DEPTH_STENCIL) {
264 if (format_desc->layout != UTIL_FORMAT_LAYOUT_PLAIN)
265 return FALSE;
266
267 if (format_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS)
268 return FALSE;
269
270 /* FIXME: Temporary restriction. See lp_state_fs.c. */
271 if (format_desc->block.bits != 32)
272 return FALSE;
273 }
274
275 if (format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
276 return util_format_s3tc_enabled;
277 }
278
279 /*
280 * Everything else should be supported by u_format.
281 */
282 return TRUE;
283 }
284
285
286
287
288 static void
289 llvmpipe_flush_frontbuffer(struct pipe_screen *_screen,
290 struct pipe_resource *resource,
291 unsigned level, unsigned layer,
292 void *context_private)
293 {
294 struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
295 struct sw_winsys *winsys = screen->winsys;
296 struct llvmpipe_resource *texture = llvmpipe_resource(resource);
297
298 assert(texture->dt);
299 if (texture->dt)
300 winsys->displaytarget_display(winsys, texture->dt, context_private);
301 }
302
303
304 static void
305 llvmpipe_destroy_screen( struct pipe_screen *_screen )
306 {
307 struct llvmpipe_screen *screen = llvmpipe_screen(_screen);
308 struct sw_winsys *winsys = screen->winsys;
309
310 if (screen->rast)
311 lp_rast_destroy(screen->rast);
312
313 lp_jit_screen_cleanup(screen);
314
315 if(winsys->destroy)
316 winsys->destroy(winsys);
317
318 pipe_mutex_destroy(screen->rast_mutex);
319
320 FREE(screen);
321 }
322
323
324
325
326 /**
327 * Fence reference counting.
328 */
329 static void
330 llvmpipe_fence_reference(struct pipe_screen *screen,
331 struct pipe_fence_handle **ptr,
332 struct pipe_fence_handle *fence)
333 {
334 struct lp_fence **old = (struct lp_fence **) ptr;
335 struct lp_fence *f = (struct lp_fence *) fence;
336
337 lp_fence_reference(old, f);
338 }
339
340
341 /**
342 * Has the fence been executed/finished?
343 */
344 static int
345 llvmpipe_fence_signalled(struct pipe_screen *screen,
346 struct pipe_fence_handle *fence,
347 unsigned flag)
348 {
349 struct lp_fence *f = (struct lp_fence *) fence;
350 return lp_fence_signalled(f);
351 }
352
353
354 /**
355 * Wait for the fence to finish.
356 */
357 static int
358 llvmpipe_fence_finish(struct pipe_screen *screen,
359 struct pipe_fence_handle *fence_handle,
360 unsigned flag)
361 {
362 struct lp_fence *f = (struct lp_fence *) fence_handle;
363
364 lp_fence_wait(f);
365 return 0;
366 }
367
368
369
370 /**
371 * Create a new pipe_screen object
372 * Note: we're not presently subclassing pipe_screen (no llvmpipe_screen).
373 */
374 struct pipe_screen *
375 llvmpipe_create_screen(struct sw_winsys *winsys)
376 {
377 struct llvmpipe_screen *screen;
378
379 #ifdef PIPE_ARCH_X86
380 /* require SSE2 due to LLVM PR6960. */
381 util_cpu_detect();
382 if (!util_cpu_caps.has_sse2)
383 return NULL;
384 #endif
385
386 screen = CALLOC_STRUCT(llvmpipe_screen);
387
388 #ifdef DEBUG
389 LP_DEBUG = debug_get_flags_option("LP_DEBUG", lp_debug_flags, 0 );
390 #endif
391
392 LP_PERF = debug_get_flags_option("LP_PERF", lp_perf_flags, 0 );
393
394 if (!screen)
395 return NULL;
396
397 screen->winsys = winsys;
398
399 screen->base.destroy = llvmpipe_destroy_screen;
400
401 screen->base.get_name = llvmpipe_get_name;
402 screen->base.get_vendor = llvmpipe_get_vendor;
403 screen->base.get_param = llvmpipe_get_param;
404 screen->base.get_shader_param = llvmpipe_get_shader_param;
405 screen->base.get_paramf = llvmpipe_get_paramf;
406 screen->base.is_format_supported = llvmpipe_is_format_supported;
407
408 screen->base.context_create = llvmpipe_create_context;
409 screen->base.flush_frontbuffer = llvmpipe_flush_frontbuffer;
410 screen->base.fence_reference = llvmpipe_fence_reference;
411 screen->base.fence_signalled = llvmpipe_fence_signalled;
412 screen->base.fence_finish = llvmpipe_fence_finish;
413
414 llvmpipe_init_screen_resource_funcs(&screen->base);
415
416 lp_jit_screen_init(screen);
417
418 screen->num_threads = util_cpu_caps.nr_cpus > 1 ? util_cpu_caps.nr_cpus : 0;
419 #ifdef PIPE_OS_EMBEDDED
420 screen->num_threads = 0;
421 #endif
422 screen->num_threads = debug_get_num_option("LP_NUM_THREADS", screen->num_threads);
423 screen->num_threads = MIN2(screen->num_threads, LP_MAX_THREADS);
424
425 screen->rast = lp_rast_create(screen->num_threads);
426 if (!screen->rast) {
427 lp_jit_screen_cleanup(screen);
428 FREE(screen);
429 return NULL;
430 }
431 pipe_mutex_init(screen->rast_mutex);
432
433 util_format_s3tc_init();
434
435 return &screen->base;
436 }