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