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