49d571ac185cd62de75677ae26365de335da6c22
[mesa.git] / src / gallium / drivers / softpipe / sp_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/format/u_format.h"
31 #include "util/format/u_format_s3tc.h"
32 #include "util/u_screen.h"
33 #include "util/u_video.h"
34 #include "util/os_misc.h"
35 #include "util/os_time.h"
36 #include "pipe/p_defines.h"
37 #include "pipe/p_screen.h"
38 #include "draw/draw_context.h"
39
40 #include "frontend/sw_winsys.h"
41 #include "tgsi/tgsi_exec.h"
42
43 #include "sp_texture.h"
44 #include "sp_screen.h"
45 #include "sp_context.h"
46 #include "sp_fence.h"
47 #include "sp_public.h"
48
49 DEBUG_GET_ONCE_BOOL_OPTION(use_llvm, "SOFTPIPE_USE_LLVM", FALSE)
50
51 static const char *
52 softpipe_get_vendor(struct pipe_screen *screen)
53 {
54 return "Mesa/X.org";
55 }
56
57
58 static const char *
59 softpipe_get_name(struct pipe_screen *screen)
60 {
61 return "softpipe";
62 }
63
64
65 static int
66 softpipe_get_param(struct pipe_screen *screen, enum pipe_cap param)
67 {
68 struct softpipe_screen *sp_screen = softpipe_screen(screen);
69 switch (param) {
70 case PIPE_CAP_NPOT_TEXTURES:
71 case PIPE_CAP_MIXED_FRAMEBUFFER_SIZES:
72 case PIPE_CAP_MIXED_COLOR_DEPTH_BITS:
73 return 1;
74 case PIPE_CAP_FRAGMENT_SHADER_TEXTURE_LOD:
75 case PIPE_CAP_FRAGMENT_SHADER_DERIVATIVES:
76 case PIPE_CAP_VERTEX_SHADER_SATURATE:
77 return 1;
78 case PIPE_CAP_ANISOTROPIC_FILTER:
79 return 1;
80 case PIPE_CAP_POINT_SPRITE:
81 return 1;
82 case PIPE_CAP_MAX_RENDER_TARGETS:
83 return PIPE_MAX_COLOR_BUFS;
84 case PIPE_CAP_MAX_DUAL_SOURCE_RENDER_TARGETS:
85 return 1;
86 case PIPE_CAP_OCCLUSION_QUERY:
87 return 1;
88 case PIPE_CAP_QUERY_TIME_ELAPSED:
89 return 1;
90 case PIPE_CAP_QUERY_PIPELINE_STATISTICS:
91 return 1;
92 case PIPE_CAP_TEXTURE_MIRROR_CLAMP:
93 case PIPE_CAP_TEXTURE_MIRROR_CLAMP_TO_EDGE:
94 return 1;
95 case PIPE_CAP_TEXTURE_SWIZZLE:
96 return 1;
97 case PIPE_CAP_MAX_TEXTURE_2D_SIZE:
98 return 1 << (SP_MAX_TEXTURE_2D_LEVELS - 1);
99 case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
100 return SP_MAX_TEXTURE_3D_LEVELS;
101 case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
102 return SP_MAX_TEXTURE_CUBE_LEVELS;
103 case PIPE_CAP_BLEND_EQUATION_SEPARATE:
104 return 1;
105 case PIPE_CAP_INDEP_BLEND_ENABLE:
106 return 1;
107 case PIPE_CAP_INDEP_BLEND_FUNC:
108 return 1;
109 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT:
110 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT:
111 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER:
112 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER:
113 return 1;
114 case PIPE_CAP_DEPTH_CLIP_DISABLE:
115 return 1;
116 case PIPE_CAP_MAX_STREAM_OUTPUT_BUFFERS:
117 return PIPE_MAX_SO_BUFFERS;
118 case PIPE_CAP_MAX_STREAM_OUTPUT_SEPARATE_COMPONENTS:
119 case PIPE_CAP_MAX_STREAM_OUTPUT_INTERLEAVED_COMPONENTS:
120 return 16*4;
121 case PIPE_CAP_MAX_GEOMETRY_OUTPUT_VERTICES:
122 case PIPE_CAP_MAX_GEOMETRY_TOTAL_OUTPUT_COMPONENTS:
123 return 1024;
124 case PIPE_CAP_MAX_VERTEX_STREAMS:
125 if (sp_screen->use_llvm)
126 return 1;
127 else
128 return PIPE_MAX_VERTEX_STREAMS;
129 case PIPE_CAP_MAX_VERTEX_ATTRIB_STRIDE:
130 return 2048;
131 case PIPE_CAP_PRIMITIVE_RESTART:
132 case PIPE_CAP_PRIMITIVE_RESTART_FIXED_INDEX:
133 return 1;
134 case PIPE_CAP_SHADER_STENCIL_EXPORT:
135 return 1;
136 case PIPE_CAP_TGSI_ATOMFADD:
137 case PIPE_CAP_TGSI_INSTANCEID:
138 case PIPE_CAP_VERTEX_ELEMENT_INSTANCE_DIVISOR:
139 case PIPE_CAP_START_INSTANCE:
140 return 1;
141 case PIPE_CAP_SEAMLESS_CUBE_MAP:
142 case PIPE_CAP_SEAMLESS_CUBE_MAP_PER_TEXTURE:
143 return 1;
144 case PIPE_CAP_MAX_TEXTURE_ARRAY_LAYERS:
145 return 256; /* for GL3 */
146 case PIPE_CAP_MIN_TEXEL_OFFSET:
147 return -8;
148 case PIPE_CAP_MAX_TEXEL_OFFSET:
149 return 7;
150 case PIPE_CAP_CONDITIONAL_RENDER:
151 case PIPE_CAP_FRAGMENT_COLOR_CLAMPED:
152 case PIPE_CAP_VERTEX_COLOR_UNCLAMPED: /* draw module */
153 case PIPE_CAP_VERTEX_COLOR_CLAMPED: /* draw module */
154 return 1;
155 case PIPE_CAP_MIXED_COLORBUFFER_FORMATS:
156 return 1;
157 case PIPE_CAP_GLSL_FEATURE_LEVEL:
158 return 400;
159 case PIPE_CAP_GLSL_FEATURE_LEVEL_COMPATIBILITY:
160 return 140;
161 case PIPE_CAP_COMPUTE:
162 return 1;
163 case PIPE_CAP_USER_VERTEX_BUFFERS:
164 case PIPE_CAP_STREAM_OUTPUT_PAUSE_RESUME:
165 case PIPE_CAP_STREAM_OUTPUT_INTERLEAVE_BUFFERS:
166 case PIPE_CAP_TGSI_VS_LAYER_VIEWPORT:
167 case PIPE_CAP_DOUBLES:
168 case PIPE_CAP_INT64:
169 case PIPE_CAP_INT64_DIVMOD:
170 case PIPE_CAP_TGSI_DIV:
171 return 1;
172 case PIPE_CAP_CONSTANT_BUFFER_OFFSET_ALIGNMENT:
173 return 16;
174 case PIPE_CAP_MIN_MAP_BUFFER_ALIGNMENT:
175 return 64;
176 case PIPE_CAP_QUERY_TIMESTAMP:
177 case PIPE_CAP_CUBE_MAP_ARRAY:
178 return 1;
179 case PIPE_CAP_TEXTURE_BUFFER_OBJECTS:
180 return 1;
181 case PIPE_CAP_MAX_TEXTURE_BUFFER_SIZE:
182 return 65536;
183 case PIPE_CAP_TEXTURE_BUFFER_OFFSET_ALIGNMENT:
184 return 16;
185 case PIPE_CAP_PREFER_BLIT_BASED_TEXTURE_TRANSFER:
186 return 0;
187 case PIPE_CAP_MAX_VIEWPORTS:
188 return PIPE_MAX_VIEWPORTS;
189 case PIPE_CAP_ENDIANNESS:
190 return PIPE_ENDIAN_NATIVE;
191 case PIPE_CAP_MAX_TEXTURE_GATHER_COMPONENTS:
192 return 4;
193 case PIPE_CAP_TEXTURE_GATHER_SM5:
194 case PIPE_CAP_TEXTURE_QUERY_LOD:
195 return 1;
196 case PIPE_CAP_TGSI_VS_WINDOW_SPACE_POSITION:
197 return 1;
198 case PIPE_CAP_TGSI_FS_FINE_DERIVATIVE:
199 return 1;
200 case PIPE_CAP_SAMPLER_VIEW_TARGET:
201 return 1;
202 case PIPE_CAP_FAKE_SW_MSAA:
203 return 1;
204 case PIPE_CAP_MIN_TEXTURE_GATHER_OFFSET:
205 return -32;
206 case PIPE_CAP_MAX_TEXTURE_GATHER_OFFSET:
207 return 31;
208 case PIPE_CAP_DRAW_INDIRECT:
209 return 1;
210 case PIPE_CAP_QUERY_SO_OVERFLOW:
211 return 1;
212
213 case PIPE_CAP_VENDOR_ID:
214 return 0xFFFFFFFF;
215 case PIPE_CAP_DEVICE_ID:
216 return 0xFFFFFFFF;
217 case PIPE_CAP_ACCELERATED:
218 return 0;
219 case PIPE_CAP_VIDEO_MEMORY: {
220 /* XXX: Do we want to return the full amount fo system memory ? */
221 uint64_t system_memory;
222
223 if (!os_get_total_physical_memory(&system_memory))
224 return 0;
225
226 if (sizeof(void *) == 4)
227 /* Cap to 2 GB on 32 bits system. We do this because softpipe does
228 * eat application memory, which is quite limited on 32 bits. App
229 * shouldn't expect too much available memory. */
230 system_memory = MIN2(system_memory, 2048 << 20);
231
232 return (int)(system_memory >> 20);
233 }
234 case PIPE_CAP_UMA:
235 return 0;
236 case PIPE_CAP_CONDITIONAL_RENDER_INVERTED:
237 return 1;
238 case PIPE_CAP_CLIP_HALFZ:
239 case PIPE_CAP_TEXTURE_FLOAT_LINEAR:
240 case PIPE_CAP_TEXTURE_HALF_FLOAT_LINEAR:
241 return 1;
242 case PIPE_CAP_FRAMEBUFFER_NO_ATTACHMENT:
243 case PIPE_CAP_CULL_DISTANCE:
244 case PIPE_CAP_COPY_BETWEEN_COMPRESSED_AND_PLAIN_FORMATS:
245 case PIPE_CAP_TGSI_ARRAY_COMPONENTS:
246 return 1;
247 case PIPE_CAP_CLEAR_TEXTURE:
248 return 1;
249 case PIPE_CAP_MAX_VARYINGS:
250 return TGSI_EXEC_MAX_INPUT_ATTRIBS;
251 case PIPE_CAP_PCI_GROUP:
252 case PIPE_CAP_PCI_BUS:
253 case PIPE_CAP_PCI_DEVICE:
254 case PIPE_CAP_PCI_FUNCTION:
255 case PIPE_CAP_GLSL_OPTIMIZE_CONSERVATIVELY:
256 return 0;
257 case PIPE_CAP_MAX_GS_INVOCATIONS:
258 return 32;
259 case PIPE_CAP_MAX_SHADER_BUFFER_SIZE:
260 return 1 << 27;
261 case PIPE_CAP_SHADER_BUFFER_OFFSET_ALIGNMENT:
262 return 4;
263 default:
264 return u_pipe_screen_get_param_defaults(screen, param);
265 }
266 }
267
268 static int
269 softpipe_get_shader_param(struct pipe_screen *screen,
270 enum pipe_shader_type shader,
271 enum pipe_shader_cap param)
272 {
273 struct softpipe_screen *sp_screen = softpipe_screen(screen);
274 switch(shader)
275 {
276 case PIPE_SHADER_FRAGMENT:
277 return tgsi_exec_get_shader_param(param);
278 case PIPE_SHADER_COMPUTE:
279 return tgsi_exec_get_shader_param(param);
280 case PIPE_SHADER_VERTEX:
281 case PIPE_SHADER_GEOMETRY:
282 if (sp_screen->use_llvm)
283 return draw_get_shader_param(shader, param);
284 else
285 return draw_get_shader_param_no_llvm(shader, param);
286 default:
287 return 0;
288 }
289 }
290
291 static float
292 softpipe_get_paramf(struct pipe_screen *screen, enum pipe_capf param)
293 {
294 switch (param) {
295 case PIPE_CAPF_MAX_LINE_WIDTH:
296 /* fall-through */
297 case PIPE_CAPF_MAX_LINE_WIDTH_AA:
298 return 255.0; /* arbitrary */
299 case PIPE_CAPF_MAX_POINT_WIDTH:
300 /* fall-through */
301 case PIPE_CAPF_MAX_POINT_WIDTH_AA:
302 return 255.0; /* arbitrary */
303 case PIPE_CAPF_MAX_TEXTURE_ANISOTROPY:
304 return 16.0;
305 case PIPE_CAPF_MAX_TEXTURE_LOD_BIAS:
306 return 16.0; /* arbitrary */
307 case PIPE_CAPF_MIN_CONSERVATIVE_RASTER_DILATE:
308 return 0.0;
309 case PIPE_CAPF_MAX_CONSERVATIVE_RASTER_DILATE:
310 return 0.0;
311 case PIPE_CAPF_CONSERVATIVE_RASTER_DILATE_GRANULARITY:
312 return 0.0;
313 }
314 /* should only get here on unhandled cases */
315 debug_printf("Unexpected PIPE_CAPF %d query\n", param);
316 return 0.0;
317 }
318
319 /**
320 * Query format support for creating a texture, drawing surface, etc.
321 * \param format the format to test
322 * \param type one of PIPE_TEXTURE, PIPE_SURFACE
323 */
324 static bool
325 softpipe_is_format_supported( struct pipe_screen *screen,
326 enum pipe_format format,
327 enum pipe_texture_target target,
328 unsigned sample_count,
329 unsigned storage_sample_count,
330 unsigned bind)
331 {
332 struct sw_winsys *winsys = softpipe_screen(screen)->winsys;
333 const struct util_format_description *format_desc;
334
335 assert(target == PIPE_BUFFER ||
336 target == PIPE_TEXTURE_1D ||
337 target == PIPE_TEXTURE_1D_ARRAY ||
338 target == PIPE_TEXTURE_2D ||
339 target == PIPE_TEXTURE_2D_ARRAY ||
340 target == PIPE_TEXTURE_RECT ||
341 target == PIPE_TEXTURE_3D ||
342 target == PIPE_TEXTURE_CUBE ||
343 target == PIPE_TEXTURE_CUBE_ARRAY);
344
345 if (MAX2(1, sample_count) != MAX2(1, storage_sample_count))
346 return false;
347
348 format_desc = util_format_description(format);
349 if (!format_desc)
350 return false;
351
352 if (sample_count > 1)
353 return false;
354
355 if (bind & (PIPE_BIND_DISPLAY_TARGET |
356 PIPE_BIND_SCANOUT |
357 PIPE_BIND_SHARED)) {
358 if(!winsys->is_displaytarget_format_supported(winsys, bind, format))
359 return false;
360 }
361
362 if (bind & PIPE_BIND_RENDER_TARGET) {
363 if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS)
364 return false;
365
366 /*
367 * Although possible, it is unnatural to render into compressed or YUV
368 * surfaces. So disable these here to avoid going into weird paths
369 * inside gallium frontends.
370 */
371 if (format_desc->block.width != 1 ||
372 format_desc->block.height != 1)
373 return false;
374 }
375
376 if (bind & PIPE_BIND_DEPTH_STENCIL) {
377 if (format_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS)
378 return false;
379 }
380
381 if (format_desc->layout == UTIL_FORMAT_LAYOUT_ASTC ||
382 format_desc->layout == UTIL_FORMAT_LAYOUT_ATC) {
383 /* Software decoding is not hooked up. */
384 return false;
385 }
386
387 if ((bind & (PIPE_BIND_RENDER_TARGET | PIPE_BIND_SAMPLER_VIEW)) &&
388 ((bind & PIPE_BIND_DISPLAY_TARGET) == 0) &&
389 target != PIPE_BUFFER) {
390 const struct util_format_description *desc =
391 util_format_description(format);
392 if (desc->nr_channels == 3 && desc->is_array) {
393 /* Don't support any 3-component formats for rendering/texturing
394 * since we don't support the corresponding 8-bit 3 channel UNORM
395 * formats. This allows us to support GL_ARB_copy_image between
396 * GL_RGB8 and GL_RGB8UI, for example. Otherwise, we may be asked to
397 * do a resource copy between PIPE_FORMAT_R8G8B8_UINT and
398 * PIPE_FORMAT_R8G8B8X8_UNORM, for example, which will not work
399 * (different bpp).
400 */
401 return false;
402 }
403 }
404
405 if (format_desc->layout == UTIL_FORMAT_LAYOUT_ETC &&
406 format != PIPE_FORMAT_ETC1_RGB8)
407 return false;
408
409 /*
410 * All other operations (sampling, transfer, etc).
411 */
412
413 /*
414 * Everything else should be supported by u_format.
415 */
416 return true;
417 }
418
419
420 static void
421 softpipe_destroy_screen( struct pipe_screen *screen )
422 {
423 struct softpipe_screen *sp_screen = softpipe_screen(screen);
424 struct sw_winsys *winsys = sp_screen->winsys;
425
426 if(winsys->destroy)
427 winsys->destroy(winsys);
428
429 FREE(screen);
430 }
431
432
433 /* This is often overriden by the co-state tracker.
434 */
435 static void
436 softpipe_flush_frontbuffer(struct pipe_screen *_screen,
437 struct pipe_resource *resource,
438 unsigned level, unsigned layer,
439 void *context_private,
440 struct pipe_box *sub_box)
441 {
442 struct softpipe_screen *screen = softpipe_screen(_screen);
443 struct sw_winsys *winsys = screen->winsys;
444 struct softpipe_resource *texture = softpipe_resource(resource);
445
446 assert(texture->dt);
447 if (texture->dt)
448 winsys->displaytarget_display(winsys, texture->dt, context_private, sub_box);
449 }
450
451 static uint64_t
452 softpipe_get_timestamp(struct pipe_screen *_screen)
453 {
454 return os_time_get_nano();
455 }
456
457 static int
458 softpipe_get_compute_param(struct pipe_screen *_screen,
459 enum pipe_shader_ir ir_type,
460 enum pipe_compute_cap param,
461 void *ret)
462 {
463 switch (param) {
464 case PIPE_COMPUTE_CAP_IR_TARGET:
465 return 0;
466 case PIPE_COMPUTE_CAP_MAX_GRID_SIZE:
467 if (ret) {
468 uint64_t *grid_size = ret;
469 grid_size[0] = 65535;
470 grid_size[1] = 65535;
471 grid_size[2] = 65535;
472 }
473 return 3 * sizeof(uint64_t) ;
474 case PIPE_COMPUTE_CAP_MAX_BLOCK_SIZE:
475 if (ret) {
476 uint64_t *block_size = ret;
477 block_size[0] = 1024;
478 block_size[1] = 1024;
479 block_size[2] = 1024;
480 }
481 return 3 * sizeof(uint64_t);
482 case PIPE_COMPUTE_CAP_MAX_THREADS_PER_BLOCK:
483 if (ret) {
484 uint64_t *max_threads_per_block = ret;
485 *max_threads_per_block = 1024;
486 }
487 return sizeof(uint64_t);
488 case PIPE_COMPUTE_CAP_MAX_LOCAL_SIZE:
489 if (ret) {
490 uint64_t *max_local_size = ret;
491 *max_local_size = 32768;
492 }
493 return sizeof(uint64_t);
494 case PIPE_COMPUTE_CAP_GRID_DIMENSION:
495 case PIPE_COMPUTE_CAP_MAX_GLOBAL_SIZE:
496 case PIPE_COMPUTE_CAP_MAX_PRIVATE_SIZE:
497 case PIPE_COMPUTE_CAP_MAX_INPUT_SIZE:
498 case PIPE_COMPUTE_CAP_MAX_MEM_ALLOC_SIZE:
499 case PIPE_COMPUTE_CAP_MAX_CLOCK_FREQUENCY:
500 case PIPE_COMPUTE_CAP_MAX_COMPUTE_UNITS:
501 case PIPE_COMPUTE_CAP_IMAGES_SUPPORTED:
502 case PIPE_COMPUTE_CAP_SUBGROUP_SIZE:
503 case PIPE_COMPUTE_CAP_ADDRESS_BITS:
504 case PIPE_COMPUTE_CAP_MAX_VARIABLE_THREADS_PER_BLOCK:
505 break;
506 }
507 return 0;
508 }
509
510 /**
511 * Create a new pipe_screen object
512 * Note: we're not presently subclassing pipe_screen (no softpipe_screen).
513 */
514 struct pipe_screen *
515 softpipe_create_screen(struct sw_winsys *winsys)
516 {
517 struct softpipe_screen *screen = CALLOC_STRUCT(softpipe_screen);
518
519 if (!screen)
520 return NULL;
521
522 screen->winsys = winsys;
523
524 screen->base.destroy = softpipe_destroy_screen;
525
526 screen->base.get_name = softpipe_get_name;
527 screen->base.get_vendor = softpipe_get_vendor;
528 screen->base.get_device_vendor = softpipe_get_vendor; // TODO should be the CPU vendor
529 screen->base.get_param = softpipe_get_param;
530 screen->base.get_shader_param = softpipe_get_shader_param;
531 screen->base.get_paramf = softpipe_get_paramf;
532 screen->base.get_timestamp = softpipe_get_timestamp;
533 screen->base.is_format_supported = softpipe_is_format_supported;
534 screen->base.context_create = softpipe_create_context;
535 screen->base.flush_frontbuffer = softpipe_flush_frontbuffer;
536 screen->base.get_compute_param = softpipe_get_compute_param;
537 screen->use_llvm = debug_get_option_use_llvm();
538
539 softpipe_init_screen_texture_funcs(&screen->base);
540 softpipe_init_screen_fence_funcs(&screen->base);
541
542 return &screen->base;
543 }