svga_screen.c revision a508d2dddcc67d0f92cc36b9ed6f36a9bbfc579d
1/**********************************************************
2 * Copyright 2008-2009 VMware, Inc.  All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26#include "util/u_memory.h"
27#include "util/u_inlines.h"
28#include "util/u_string.h"
29#include "util/u_math.h"
30
31#include "svga_winsys.h"
32#include "svga_public.h"
33#include "svga_context.h"
34#include "svga_screen.h"
35#include "svga_resource_texture.h"
36#include "svga_resource.h"
37#include "svga_debug.h"
38#include "svga_surface.h"
39
40#include "svga3d_shaderdefs.h"
41
42
43#ifdef DEBUG
44int SVGA_DEBUG = 0;
45
46static const struct debug_named_value svga_debug_flags[] = {
47   { "dma",      DEBUG_DMA, NULL },
48   { "tgsi",     DEBUG_TGSI, NULL },
49   { "pipe",     DEBUG_PIPE, NULL },
50   { "state",    DEBUG_STATE, NULL },
51   { "screen",   DEBUG_SCREEN, NULL },
52   { "tex",      DEBUG_TEX, NULL },
53   { "swtnl",    DEBUG_SWTNL, NULL },
54   { "const",    DEBUG_CONSTS, NULL },
55   { "viewport", DEBUG_VIEWPORT, NULL },
56   { "views",    DEBUG_VIEWS, NULL },
57   { "perf",     DEBUG_PERF, NULL },
58   { "flush",    DEBUG_FLUSH, NULL },
59   { "sync",     DEBUG_SYNC, NULL },
60   { "cache",    DEBUG_CACHE, NULL },
61   DEBUG_NAMED_VALUE_END
62};
63#endif
64
65static const char *
66svga_get_vendor( struct pipe_screen *pscreen )
67{
68   return "VMware, Inc.";
69}
70
71
72static const char *
73svga_get_name( struct pipe_screen *pscreen )
74{
75#ifdef DEBUG
76   /* Only return internal details in the DEBUG version:
77    */
78   return "SVGA3D; build: DEBUG; mutex: " PIPE_ATOMIC;
79#else
80   return "SVGA3D; build: RELEASE; ";
81#endif
82}
83
84
85
86
87static float
88svga_get_paramf(struct pipe_screen *screen, enum pipe_cap param)
89{
90   struct svga_screen *svgascreen = svga_screen(screen);
91   struct svga_winsys_screen *sws = svgascreen->sws;
92   SVGA3dDevCapResult result;
93
94   switch (param) {
95   case PIPE_CAP_MAX_LINE_WIDTH:
96      /* fall-through */
97   case PIPE_CAP_MAX_LINE_WIDTH_AA:
98      return 7.0;
99
100   case PIPE_CAP_MAX_POINT_WIDTH:
101      /* fall-through */
102   case PIPE_CAP_MAX_POINT_WIDTH_AA:
103      /* Keep this to a reasonable size to avoid failures in
104       * conform/pntaa.c:
105       */
106      return SVGA_MAX_POINTSIZE;
107
108   case PIPE_CAP_MAX_TEXTURE_ANISOTROPY:
109      if(!sws->get_cap(sws, SVGA3D_DEVCAP_MAX_TEXTURE_ANISOTROPY, &result))
110         return 4.0;
111      return result.u;
112
113   case PIPE_CAP_MAX_TEXTURE_LOD_BIAS:
114      return 16.0;
115
116   case PIPE_CAP_MAX_TEXTURE_IMAGE_UNITS:
117      return 16;
118   case PIPE_CAP_MAX_COMBINED_SAMPLERS:
119      return 16;
120   case PIPE_CAP_NPOT_TEXTURES:
121      return 1;
122   case PIPE_CAP_TWO_SIDED_STENCIL:
123      return 1;
124   case PIPE_CAP_GLSL:
125      return svgascreen->use_ps30 && svgascreen->use_vs30;
126   case PIPE_CAP_ANISOTROPIC_FILTER:
127      return 1;
128   case PIPE_CAP_POINT_SPRITE:
129      return 1;
130   case PIPE_CAP_MAX_RENDER_TARGETS:
131      if(!sws->get_cap(sws, SVGA3D_DEVCAP_MAX_RENDER_TARGETS, &result))
132         return 1;
133      if(!result.u)
134         return 1;
135      return MIN2(result.u, PIPE_MAX_COLOR_BUFS);
136   case PIPE_CAP_OCCLUSION_QUERY:
137      return 1;
138   case PIPE_CAP_TIMER_QUERY:
139      return 0;
140   case PIPE_CAP_TEXTURE_SHADOW_MAP:
141      return 1;
142
143   case PIPE_CAP_MAX_TEXTURE_2D_LEVELS:
144      {
145         unsigned levels = SVGA_MAX_TEXTURE_LEVELS;
146         if (sws->get_cap(sws, SVGA3D_DEVCAP_MAX_TEXTURE_WIDTH, &result))
147            levels = MIN2(util_logbase2(result.u) + 1, levels);
148         else
149            levels = 12 /* 2048x2048 */;
150         if (sws->get_cap(sws, SVGA3D_DEVCAP_MAX_TEXTURE_HEIGHT, &result))
151            levels = MIN2(util_logbase2(result.u) + 1, levels);
152         else
153            levels = 12 /* 2048x2048 */;
154         return levels;
155      }
156
157   case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
158      if (!sws->get_cap(sws, SVGA3D_DEVCAP_MAX_VOLUME_EXTENT, &result))
159         return 8;  /* max 128x128x128 */
160      return MIN2(util_logbase2(result.u) + 1, SVGA_MAX_TEXTURE_LEVELS);
161
162   case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
163      /*
164       * No mechanism to query the host, and at least limited to 2048x2048 on
165       * certain hardware.
166       */
167      return MIN2(screen->get_paramf(screen, PIPE_CAP_MAX_TEXTURE_2D_LEVELS),
168                  12.0 /* 2048x2048 */);
169
170   case PIPE_CAP_TEXTURE_MIRROR_REPEAT: /* req. for GL 1.4 */
171      return 1;
172
173   case PIPE_CAP_BLEND_EQUATION_SEPARATE: /* req. for GL 1.5 */
174      return 1;
175
176   case PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT:
177   case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER:
178      return 1;
179   case PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT:
180   case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER:
181      return 0;
182
183   case PIPE_CAP_DEPTHSTENCIL_CLEAR_SEPARATE:
184      return 1;
185
186   default:
187      return 0;
188   }
189}
190
191
192/* This is a fairly pointless interface
193 */
194static int
195svga_get_param(struct pipe_screen *screen, enum pipe_cap param)
196{
197   return (int) svga_get_paramf( screen, param );
198}
199
200static int svga_get_shader_param(struct pipe_screen *screen, unsigned shader, enum pipe_shader_cap param)
201{
202   struct svga_screen *svgascreen = svga_screen(screen);
203   struct svga_winsys_screen *sws = svgascreen->sws;
204   SVGA3dDevCapResult result;
205
206   switch (shader)
207   {
208   case PIPE_SHADER_FRAGMENT:
209      switch (param)
210      {
211      case PIPE_SHADER_CAP_MAX_INSTRUCTIONS:
212      case PIPE_SHADER_CAP_MAX_ALU_INSTRUCTIONS:
213      case PIPE_SHADER_CAP_MAX_TEX_INSTRUCTIONS:
214      case PIPE_SHADER_CAP_MAX_TEX_INDIRECTIONS:
215         return svgascreen->use_ps30 ? 512 : 96;
216      case PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH:
217         return SVGA3D_MAX_NESTING_LEVEL;
218      case PIPE_SHADER_CAP_MAX_INPUTS:
219         return 10;
220      case PIPE_SHADER_CAP_MAX_CONSTS:
221         return svgascreen->use_ps30 ? 224 : 16;
222      case PIPE_SHADER_CAP_MAX_CONST_BUFFERS:
223         return 1;
224      case PIPE_SHADER_CAP_MAX_TEMPS:
225         if (!sws->get_cap(sws, SVGA3D_DEVCAP_MAX_FRAGMENT_SHADER_TEMPS, &result))
226            return svgascreen->use_ps30 ? 32 : 12;
227         return result.u;
228      case PIPE_SHADER_CAP_MAX_ADDRS:
229         return svgascreen->use_ps30 ? 1 : 0;
230      case PIPE_SHADER_CAP_MAX_PREDS:
231         return svgascreen->use_ps30 ? 1 : 0;
232      case PIPE_SHADER_CAP_TGSI_CONT_SUPPORTED:
233         return 1;
234      }
235      break;
236   case PIPE_SHADER_VERTEX:
237      switch (param)
238      {
239      case PIPE_SHADER_CAP_MAX_INSTRUCTIONS:
240      case PIPE_SHADER_CAP_MAX_ALU_INSTRUCTIONS:
241         if (!sws->get_cap(sws, SVGA3D_DEVCAP_MAX_VERTEX_SHADER_INSTRUCTIONS, &result))
242            return svgascreen->use_vs30 ? 512 : 256;
243         return result.u;
244      case PIPE_SHADER_CAP_MAX_TEX_INSTRUCTIONS:
245      case PIPE_SHADER_CAP_MAX_TEX_INDIRECTIONS:
246         /* XXX: until we have vertex texture support */
247         return 0;
248      case PIPE_SHADER_CAP_MAX_CONTROL_FLOW_DEPTH:
249         return SVGA3D_MAX_NESTING_LEVEL;
250      case PIPE_SHADER_CAP_MAX_INPUTS:
251         return 16;
252      case PIPE_SHADER_CAP_MAX_CONSTS:
253         return 256;
254      case PIPE_SHADER_CAP_MAX_CONST_BUFFERS:
255         return 1;
256      case PIPE_SHADER_CAP_MAX_TEMPS:
257         if (!sws->get_cap(sws, SVGA3D_DEVCAP_MAX_VERTEX_SHADER_TEMPS, &result))
258            return svgascreen->use_vs30 ? 32 : 12;
259         return result.u;
260      case PIPE_SHADER_CAP_MAX_ADDRS:
261         return svgascreen->use_vs30 ? 1 : 0;
262      case PIPE_SHADER_CAP_MAX_PREDS:
263         return svgascreen->use_vs30 ? 1 : 0;
264      case PIPE_SHADER_CAP_TGSI_CONT_SUPPORTED:
265         return 1;
266      default:
267         break;
268      }
269      break;
270   default:
271      break;
272   }
273   return 0;
274}
275
276static INLINE SVGA3dDevCapIndex
277svga_translate_format_cap(enum pipe_format format)
278{
279   switch(format) {
280
281   case PIPE_FORMAT_B8G8R8A8_UNORM:
282      return SVGA3D_DEVCAP_SURFACEFMT_A8R8G8B8;
283   case PIPE_FORMAT_B8G8R8X8_UNORM:
284      return SVGA3D_DEVCAP_SURFACEFMT_X8R8G8B8;
285
286   case PIPE_FORMAT_B5G6R5_UNORM:
287      return SVGA3D_DEVCAP_SURFACEFMT_R5G6B5;
288   case PIPE_FORMAT_B5G5R5A1_UNORM:
289      return SVGA3D_DEVCAP_SURFACEFMT_A1R5G5B5;
290   case PIPE_FORMAT_B4G4R4A4_UNORM:
291      return SVGA3D_DEVCAP_SURFACEFMT_A4R4G4B4;
292
293   case PIPE_FORMAT_Z16_UNORM:
294      return SVGA3D_DEVCAP_SURFACEFMT_Z_D16;
295   case PIPE_FORMAT_S8_USCALED_Z24_UNORM:
296      return SVGA3D_DEVCAP_SURFACEFMT_Z_D24S8;
297   case PIPE_FORMAT_X8Z24_UNORM:
298      return SVGA3D_DEVCAP_SURFACEFMT_Z_D24X8;
299
300   case PIPE_FORMAT_A8_UNORM:
301      return SVGA3D_DEVCAP_SURFACEFMT_ALPHA8;
302   case PIPE_FORMAT_L8_UNORM:
303      return SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE8;
304
305   case PIPE_FORMAT_DXT1_RGB:
306   case PIPE_FORMAT_DXT1_RGBA:
307      return SVGA3D_DEVCAP_SURFACEFMT_DXT1;
308   case PIPE_FORMAT_DXT3_RGBA:
309      return SVGA3D_DEVCAP_SURFACEFMT_DXT3;
310   case PIPE_FORMAT_DXT5_RGBA:
311      return SVGA3D_DEVCAP_SURFACEFMT_DXT5;
312
313   default:
314      return SVGA3D_DEVCAP_MAX;
315   }
316}
317
318
319static boolean
320svga_is_format_supported( struct pipe_screen *screen,
321                          enum pipe_format format,
322                          enum pipe_texture_target target,
323                          unsigned sample_count,
324                          unsigned tex_usage,
325                          unsigned geom_flags )
326{
327   struct svga_winsys_screen *sws = svga_screen(screen)->sws;
328   SVGA3dDevCapIndex index;
329   SVGA3dDevCapResult result;
330
331   assert(tex_usage);
332
333   if (sample_count > 1)
334      return FALSE;
335
336   /* Override host capabilities */
337   if (tex_usage & PIPE_BIND_RENDER_TARGET) {
338      switch(format) {
339
340      /* Often unsupported/problematic. This means we end up with the same
341       * visuals for all virtual hardware implementations.
342       */
343      case PIPE_FORMAT_B4G4R4A4_UNORM:
344      case PIPE_FORMAT_B5G5R5A1_UNORM:
345         return FALSE;
346
347      /* Simulate ability to render into compressed textures */
348      case PIPE_FORMAT_DXT1_RGB:
349      case PIPE_FORMAT_DXT1_RGBA:
350      case PIPE_FORMAT_DXT3_RGBA:
351      case PIPE_FORMAT_DXT5_RGBA:
352         return TRUE;
353
354      default:
355         break;
356      }
357   }
358
359   /* Try to query the host */
360   index = svga_translate_format_cap(format);
361   if( index < SVGA3D_DEVCAP_MAX &&
362       sws->get_cap(sws, index, &result) )
363   {
364      SVGA3dSurfaceFormatCaps mask;
365
366      mask.value = 0;
367      if (tex_usage & PIPE_BIND_RENDER_TARGET)
368         mask.offscreenRenderTarget = 1;
369      if (tex_usage & PIPE_BIND_DEPTH_STENCIL)
370         mask.zStencil = 1;
371      if (tex_usage & PIPE_BIND_SAMPLER_VIEW)
372         mask.texture = 1;
373
374      if ((result.u & mask.value) == mask.value)
375         return TRUE;
376      else
377         return FALSE;
378   }
379
380   /* Use our translate functions directly rather than relying on a
381    * duplicated list of supported formats which is prone to getting
382    * out of sync:
383    */
384   if(tex_usage & (PIPE_BIND_RENDER_TARGET | PIPE_BIND_DEPTH_STENCIL))
385      return svga_translate_format_render(format) != SVGA3D_FORMAT_INVALID;
386   else
387      return svga_translate_format(format) != SVGA3D_FORMAT_INVALID;
388}
389
390
391static void
392svga_fence_reference(struct pipe_screen *screen,
393                     struct pipe_fence_handle **ptr,
394                     struct pipe_fence_handle *fence)
395{
396   struct svga_winsys_screen *sws = svga_screen(screen)->sws;
397   sws->fence_reference(sws, ptr, fence);
398}
399
400
401static int
402svga_fence_signalled(struct pipe_screen *screen,
403                     struct pipe_fence_handle *fence,
404                     unsigned flag)
405{
406   struct svga_winsys_screen *sws = svga_screen(screen)->sws;
407   return sws->fence_signalled(sws, fence, flag);
408}
409
410
411static int
412svga_fence_finish(struct pipe_screen *screen,
413                  struct pipe_fence_handle *fence,
414                  unsigned flag)
415{
416   struct svga_winsys_screen *sws = svga_screen(screen)->sws;
417
418   SVGA_DBG(DEBUG_DMA|DEBUG_PERF, "%s fence_ptr %p\n",
419            __FUNCTION__, fence);
420
421   return sws->fence_finish(sws, fence, flag);
422}
423
424
425static void
426svga_destroy_screen( struct pipe_screen *screen )
427{
428   struct svga_screen *svgascreen = svga_screen(screen);
429
430   svga_screen_cache_cleanup(svgascreen);
431
432   pipe_mutex_destroy(svgascreen->swc_mutex);
433   pipe_mutex_destroy(svgascreen->tex_mutex);
434
435   svgascreen->sws->destroy(svgascreen->sws);
436
437   FREE(svgascreen);
438}
439
440
441/**
442 * Create a new svga_screen object
443 */
444struct pipe_screen *
445svga_screen_create(struct svga_winsys_screen *sws)
446{
447   struct svga_screen *svgascreen;
448   struct pipe_screen *screen;
449   SVGA3dDevCapResult result;
450
451#ifdef DEBUG
452   SVGA_DEBUG = debug_get_flags_option("SVGA_DEBUG", svga_debug_flags, 0 );
453#endif
454
455   svgascreen = CALLOC_STRUCT(svga_screen);
456   if (!svgascreen)
457      goto error1;
458
459   svgascreen->debug.force_level_surface_view =
460      debug_get_bool_option("SVGA_FORCE_LEVEL_SURFACE_VIEW", FALSE);
461   svgascreen->debug.force_surface_view =
462      debug_get_bool_option("SVGA_FORCE_SURFACE_VIEW", FALSE);
463   svgascreen->debug.force_sampler_view =
464      debug_get_bool_option("SVGA_FORCE_SAMPLER_VIEW", FALSE);
465   svgascreen->debug.no_surface_view =
466      debug_get_bool_option("SVGA_NO_SURFACE_VIEW", FALSE);
467   svgascreen->debug.no_sampler_view =
468      debug_get_bool_option("SVGA_NO_SAMPLER_VIEW", FALSE);
469
470   screen = &svgascreen->screen;
471
472   screen->destroy = svga_destroy_screen;
473   screen->get_name = svga_get_name;
474   screen->get_vendor = svga_get_vendor;
475   screen->get_param = svga_get_param;
476   screen->get_shader_param = svga_get_shader_param;
477   screen->get_paramf = svga_get_paramf;
478   screen->is_format_supported = svga_is_format_supported;
479   screen->context_create = svga_context_create;
480   screen->fence_reference = svga_fence_reference;
481   screen->fence_signalled = svga_fence_signalled;
482   screen->fence_finish = svga_fence_finish;
483   svgascreen->sws = sws;
484
485   svga_screen_init_surface_functions(screen);
486   svga_init_screen_resource_functions(svgascreen);
487
488   svgascreen->use_ps30 =
489      sws->get_cap(sws, SVGA3D_DEVCAP_FRAGMENT_SHADER_VERSION, &result) &&
490      result.u >= SVGA3DPSVERSION_30 ? TRUE : FALSE;
491
492   svgascreen->use_vs30 =
493      sws->get_cap(sws, SVGA3D_DEVCAP_VERTEX_SHADER_VERSION, &result) &&
494      result.u >= SVGA3DVSVERSION_30 ? TRUE : FALSE;
495
496#if 1
497   /* Shader model 2.0 is unsupported at the moment. */
498   if(!svgascreen->use_ps30 || !svgascreen->use_vs30)
499      goto error2;
500#else
501   if(debug_get_bool_option("SVGA_NO_SM30", FALSE))
502      svgascreen->use_vs30 = svgascreen->use_ps30 = FALSE;
503#endif
504
505   pipe_mutex_init(svgascreen->tex_mutex);
506   pipe_mutex_init(svgascreen->swc_mutex);
507
508   svga_screen_cache_init(svgascreen);
509
510   return screen;
511error2:
512   FREE(svgascreen);
513error1:
514   return NULL;
515}
516
517struct svga_winsys_screen *
518svga_winsys_screen(struct pipe_screen *screen)
519{
520   return svga_screen(screen)->sws;
521}
522
523#ifdef DEBUG
524struct svga_screen *
525svga_screen(struct pipe_screen *screen)
526{
527   assert(screen);
528   assert(screen->destroy == svga_destroy_screen);
529   return (struct svga_screen *)screen;
530}
531#endif
532