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