Merge remote branch 'origin/master' into pipe-video
[mesa.git] / src / gallium / drivers / softpipe / sp_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_format.h"
31 #include "util/u_format_s3tc.h"
32 #include "util/u_video.h"
33 #include "pipe/p_defines.h"
34 #include "pipe/p_screen.h"
35 #include "draw/draw_context.h"
36 #include "vl/vl_mpeg12_context.h"
37
38 #include "state_tracker/sw_winsys.h"
39 #include "tgsi/tgsi_exec.h"
40
41 #include "sp_texture.h"
42 #include "sp_screen.h"
43 #include "sp_context.h"
44 #include "sp_fence.h"
45 #include "sp_public.h"
46
47
48 static const char *
49 softpipe_get_vendor(struct pipe_screen *screen)
50 {
51 return "VMware, Inc.";
52 }
53
54
55 static const char *
56 softpipe_get_name(struct pipe_screen *screen)
57 {
58 return "softpipe";
59 }
60
61
62 static int
63 softpipe_get_param(struct pipe_screen *screen, enum pipe_cap param)
64 {
65 switch (param) {
66 case PIPE_CAP_MAX_TEXTURE_IMAGE_UNITS:
67 return PIPE_MAX_SAMPLERS;
68 case PIPE_CAP_MAX_VERTEX_TEXTURE_UNITS:
69 #ifdef HAVE_LLVM
70 /* Softpipe doesn't yet know how to tell draw/llvm about textures */
71 return 0;
72 #else
73 return PIPE_MAX_VERTEX_SAMPLERS;
74 #endif
75 case PIPE_CAP_MAX_COMBINED_SAMPLERS:
76 return PIPE_MAX_SAMPLERS + PIPE_MAX_VERTEX_SAMPLERS;
77 case PIPE_CAP_NPOT_TEXTURES:
78 return 1;
79 case PIPE_CAP_TWO_SIDED_STENCIL:
80 return 1;
81 case PIPE_CAP_GLSL:
82 return 1;
83 case PIPE_CAP_SM3:
84 return 1;
85 case PIPE_CAP_ANISOTROPIC_FILTER:
86 return 0;
87 case PIPE_CAP_POINT_SPRITE:
88 return 1;
89 case PIPE_CAP_MAX_RENDER_TARGETS:
90 return PIPE_MAX_COLOR_BUFS;
91 case PIPE_CAP_OCCLUSION_QUERY:
92 return 1;
93 case PIPE_CAP_TIMER_QUERY:
94 return 1;
95 case PIPE_CAP_TEXTURE_MIRROR_CLAMP:
96 return 1;
97 case PIPE_CAP_TEXTURE_MIRROR_REPEAT:
98 return 1;
99 case PIPE_CAP_TEXTURE_SHADOW_MAP:
100 return 1;
101 case PIPE_CAP_TEXTURE_SWIZZLE:
102 return 1;
103 case PIPE_CAP_MAX_TEXTURE_2D_LEVELS:
104 return SP_MAX_TEXTURE_2D_LEVELS;
105 case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
106 return SP_MAX_TEXTURE_3D_LEVELS;
107 case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
108 return SP_MAX_TEXTURE_2D_LEVELS;
109 case PIPE_CAP_BLEND_EQUATION_SEPARATE:
110 return 1;
111 case PIPE_CAP_INDEP_BLEND_ENABLE:
112 return 1;
113 case PIPE_CAP_INDEP_BLEND_FUNC:
114 return 1;
115 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT:
116 case PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT:
117 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER:
118 case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER:
119 return 1;
120 case PIPE_CAP_STREAM_OUTPUT:
121 return 1;
122 case PIPE_CAP_PRIMITIVE_RESTART:
123 return 1;
124 case PIPE_CAP_DEPTHSTENCIL_CLEAR_SEPARATE:
125 return 0;
126 case PIPE_CAP_SHADER_STENCIL_EXPORT:
127 return 1;
128 case PIPE_CAP_TGSI_INSTANCEID:
129 case PIPE_CAP_VERTEX_ELEMENT_INSTANCE_DIVISOR:
130 return 1;
131 case PIPE_CAP_ARRAY_TEXTURES:
132 return 1;
133 default:
134 return 0;
135 }
136 }
137
138 static int
139 softpipe_get_shader_param(struct pipe_screen *screen, unsigned shader, enum pipe_shader_cap param)
140 {
141 switch(shader)
142 {
143 case PIPE_SHADER_FRAGMENT:
144 return tgsi_exec_get_shader_param(param);
145 case PIPE_SHADER_VERTEX:
146 case PIPE_SHADER_GEOMETRY:
147 return draw_get_shader_param(shader, param);
148 default:
149 return 0;
150 }
151 }
152
153 static float
154 softpipe_get_paramf(struct pipe_screen *screen, enum pipe_cap param)
155 {
156 switch (param) {
157 case PIPE_CAP_MAX_LINE_WIDTH:
158 /* fall-through */
159 case PIPE_CAP_MAX_LINE_WIDTH_AA:
160 return 255.0; /* arbitrary */
161 case PIPE_CAP_MAX_POINT_WIDTH:
162 /* fall-through */
163 case PIPE_CAP_MAX_POINT_WIDTH_AA:
164 return 255.0; /* arbitrary */
165 case PIPE_CAP_MAX_TEXTURE_ANISOTROPY:
166 return 16.0; /* not actually signficant at this time */
167 case PIPE_CAP_MAX_TEXTURE_LOD_BIAS:
168 return 16.0; /* arbitrary */
169 default:
170 return 0;
171 }
172 }
173
174
175 /**
176 * Query format support for creating a texture, drawing surface, etc.
177 * \param format the format to test
178 * \param type one of PIPE_TEXTURE, PIPE_SURFACE
179 */
180 static boolean
181 softpipe_is_format_supported( struct pipe_screen *screen,
182 enum pipe_format format,
183 enum pipe_texture_target target,
184 unsigned sample_count,
185 unsigned bind)
186 {
187 struct sw_winsys *winsys = softpipe_screen(screen)->winsys;
188 const struct util_format_description *format_desc;
189
190 assert(target == PIPE_BUFFER ||
191 target == PIPE_TEXTURE_1D ||
192 target == PIPE_TEXTURE_1D_ARRAY ||
193 target == PIPE_TEXTURE_2D ||
194 target == PIPE_TEXTURE_2D_ARRAY ||
195 target == PIPE_TEXTURE_RECT ||
196 target == PIPE_TEXTURE_3D ||
197 target == PIPE_TEXTURE_CUBE);
198
199 format_desc = util_format_description(format);
200 if (!format_desc)
201 return FALSE;
202
203 if (sample_count > 1)
204 return FALSE;
205
206 if (bind & (PIPE_BIND_DISPLAY_TARGET |
207 PIPE_BIND_SCANOUT |
208 PIPE_BIND_SHARED)) {
209 if(!winsys->is_displaytarget_format_supported(winsys, bind, format))
210 return FALSE;
211 }
212
213 if (bind & PIPE_BIND_RENDER_TARGET) {
214 if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS)
215 return FALSE;
216
217 /*
218 * Although possible, it is unnatural to render into compressed or YUV
219 * surfaces. So disable these here to avoid going into weird paths
220 * inside the state trackers.
221 */
222 if (format_desc->block.width != 1 ||
223 format_desc->block.height != 1)
224 return FALSE;
225 }
226
227 if (bind & PIPE_BIND_DEPTH_STENCIL) {
228 if (format_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS)
229 return FALSE;
230
231 /*
232 * TODO: Unfortunately we cannot render into anything more than 32 bits
233 * because we encode depth and stencil clear values into a 32bit word.
234 */
235 if (format_desc->block.bits > 32)
236 return FALSE;
237
238 /*
239 * TODO: eliminate this restriction
240 */
241 if (format == PIPE_FORMAT_Z32_FLOAT)
242 return FALSE;
243 }
244
245 /*
246 * All other operations (sampling, transfer, etc).
247 */
248
249 if (format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
250 return util_format_s3tc_enabled;
251 }
252
253 /*
254 * Everything else should be supported by u_format.
255 */
256 return TRUE;
257 }
258
259
260 static void
261 softpipe_destroy_screen( struct pipe_screen *screen )
262 {
263 struct softpipe_screen *sp_screen = softpipe_screen(screen);
264 struct sw_winsys *winsys = sp_screen->winsys;
265
266 if(winsys->destroy)
267 winsys->destroy(winsys);
268
269 FREE(screen);
270 }
271
272
273 /* This is often overriden by the co-state tracker.
274 */
275 static void
276 softpipe_flush_frontbuffer(struct pipe_screen *_screen,
277 struct pipe_resource *resource,
278 unsigned level, unsigned layer,
279 void *context_private)
280 {
281 struct softpipe_screen *screen = softpipe_screen(_screen);
282 struct sw_winsys *winsys = screen->winsys;
283 struct softpipe_resource *texture = softpipe_resource(resource);
284
285 assert(texture->dt);
286 if (texture->dt)
287 winsys->displaytarget_display(winsys, texture->dt, context_private);
288 }
289
290 static struct pipe_video_context *
291 sp_video_create(struct pipe_screen *screen, enum pipe_video_profile profile,
292 enum pipe_video_chroma_format chroma_format,
293 unsigned width, unsigned height, void *priv)
294 {
295 struct pipe_context *pipe;
296
297 assert(screen);
298 assert(width && height);
299
300 pipe = screen->context_create(screen, NULL);
301 if (!pipe)
302 return NULL;
303
304 /* TODO: Use slice buffering for softpipe when implemented, no advantage to buffering an entire picture with softpipe */
305 switch (u_reduce_video_profile(profile)) {
306 case PIPE_VIDEO_CODEC_MPEG12:
307 return vl_create_mpeg12_context(pipe, profile,
308 chroma_format,
309 width, height,
310 true,
311 PIPE_FORMAT_XYUV);
312 default:
313 return NULL;
314 }
315 }
316
317 /**
318 * Create a new pipe_screen object
319 * Note: we're not presently subclassing pipe_screen (no softpipe_screen).
320 */
321 struct pipe_screen *
322 softpipe_create_screen(struct sw_winsys *winsys)
323 {
324 struct softpipe_screen *screen = CALLOC_STRUCT(softpipe_screen);
325
326 if (!screen)
327 return NULL;
328
329 screen->winsys = winsys;
330
331 screen->base.winsys = NULL;
332 screen->base.destroy = softpipe_destroy_screen;
333
334 screen->base.get_name = softpipe_get_name;
335 screen->base.get_vendor = softpipe_get_vendor;
336 screen->base.get_param = softpipe_get_param;
337 screen->base.get_shader_param = softpipe_get_shader_param;
338 screen->base.get_paramf = softpipe_get_paramf;
339 screen->base.is_format_supported = softpipe_is_format_supported;
340 screen->base.context_create = softpipe_create_context;
341 screen->base.flush_frontbuffer = softpipe_flush_frontbuffer;
342 screen->base.video_context_create = sp_video_create;
343
344 util_format_s3tc_init();
345
346 softpipe_init_screen_texture_funcs(&screen->base);
347 softpipe_init_screen_fence_funcs(&screen->base);
348
349 return &screen->base;
350 }