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 case PIPE_CAP_ARRAY_TEXTURES:
130 return 1;
131 default:
132 return 0;
133 }
134 }
135
136 static int
137 softpipe_get_shader_param(struct pipe_screen *screen, unsigned shader, enum pipe_shader_cap param)
138 {
139 switch(shader)
140 {
141 case PIPE_SHADER_FRAGMENT:
142 return tgsi_exec_get_shader_param(param);
143 case PIPE_SHADER_VERTEX:
144 case PIPE_SHADER_GEOMETRY:
145 return draw_get_shader_param(shader, param);
146 default:
147 return 0;
148 }
149 }
150
151 static float
152 softpipe_get_paramf(struct pipe_screen *screen, enum pipe_cap param)
153 {
154 switch (param) {
155 case PIPE_CAP_MAX_LINE_WIDTH:
156 /* fall-through */
157 case PIPE_CAP_MAX_LINE_WIDTH_AA:
158 return 255.0; /* arbitrary */
159 case PIPE_CAP_MAX_POINT_WIDTH:
160 /* fall-through */
161 case PIPE_CAP_MAX_POINT_WIDTH_AA:
162 return 255.0; /* arbitrary */
163 case PIPE_CAP_MAX_TEXTURE_ANISOTROPY:
164 return 16.0; /* not actually signficant at this time */
165 case PIPE_CAP_MAX_TEXTURE_LOD_BIAS:
166 return 16.0; /* arbitrary */
167 default:
168 return 0;
169 }
170 }
171
172
173 /**
174 * Query format support for creating a texture, drawing surface, etc.
175 * \param format the format to test
176 * \param type one of PIPE_TEXTURE, PIPE_SURFACE
177 */
178 static boolean
179 softpipe_is_format_supported( struct pipe_screen *screen,
180 enum pipe_format format,
181 enum pipe_texture_target target,
182 unsigned sample_count,
183 unsigned bind,
184 unsigned geom_flags )
185 {
186 struct sw_winsys *winsys = softpipe_screen(screen)->winsys;
187 const struct util_format_description *format_desc;
188
189 assert(target == PIPE_BUFFER ||
190 target == PIPE_TEXTURE_1D ||
191 target == PIPE_TEXTURE_1D_ARRAY ||
192 target == PIPE_TEXTURE_2D ||
193 target == PIPE_TEXTURE_2D_ARRAY ||
194 target == PIPE_TEXTURE_RECT ||
195 target == PIPE_TEXTURE_3D ||
196 target == PIPE_TEXTURE_CUBE);
197
198 format_desc = util_format_description(format);
199 if (!format_desc)
200 return FALSE;
201
202 if (sample_count > 1)
203 return FALSE;
204
205 if (bind & (PIPE_BIND_DISPLAY_TARGET |
206 PIPE_BIND_SCANOUT |
207 PIPE_BIND_SHARED)) {
208 if(!winsys->is_displaytarget_format_supported(winsys, bind, format))
209 return FALSE;
210 }
211
212 if (bind & PIPE_BIND_RENDER_TARGET) {
213 if (format_desc->colorspace == UTIL_FORMAT_COLORSPACE_ZS)
214 return FALSE;
215
216 /*
217 * Although possible, it is unnatural to render into compressed or YUV
218 * surfaces. So disable these here to avoid going into weird paths
219 * inside the state trackers.
220 */
221 if (format_desc->block.width != 1 ||
222 format_desc->block.height != 1)
223 return FALSE;
224 }
225
226 if (bind & PIPE_BIND_DEPTH_STENCIL) {
227 if (format_desc->colorspace != UTIL_FORMAT_COLORSPACE_ZS)
228 return FALSE;
229
230 /*
231 * TODO: Unfortunately we cannot render into anything more than 32 bits
232 * because we encode depth and stencil clear values into a 32bit word.
233 */
234 if (format_desc->block.bits > 32)
235 return FALSE;
236
237 /*
238 * TODO: eliminate this restriction
239 */
240 if (format == PIPE_FORMAT_Z32_FLOAT)
241 return FALSE;
242 }
243
244 /*
245 * All other operations (sampling, transfer, etc).
246 */
247
248 if (format_desc->layout == UTIL_FORMAT_LAYOUT_S3TC) {
249 return util_format_s3tc_enabled;
250 }
251
252 /*
253 * Everything else should be supported by u_format.
254 */
255 return TRUE;
256 }
257
258
259 static void
260 softpipe_destroy_screen( struct pipe_screen *screen )
261 {
262 struct softpipe_screen *sp_screen = softpipe_screen(screen);
263 struct sw_winsys *winsys = sp_screen->winsys;
264
265 if(winsys->destroy)
266 winsys->destroy(winsys);
267
268 FREE(screen);
269 }
270
271
272 /* This is often overriden by the co-state tracker.
273 */
274 static void
275 softpipe_flush_frontbuffer(struct pipe_screen *_screen,
276 struct pipe_resource *resource,
277 unsigned level, unsigned layer,
278 void *context_private)
279 {
280 struct softpipe_screen *screen = softpipe_screen(_screen);
281 struct sw_winsys *winsys = screen->winsys;
282 struct softpipe_resource *texture = softpipe_resource(resource);
283
284 assert(texture->dt);
285 if (texture->dt)
286 winsys->displaytarget_display(winsys, texture->dt, context_private);
287 }
288
289 /**
290 * Create a new pipe_screen object
291 * Note: we're not presently subclassing pipe_screen (no softpipe_screen).
292 */
293 struct pipe_screen *
294 softpipe_create_screen(struct sw_winsys *winsys)
295 {
296 struct softpipe_screen *screen = CALLOC_STRUCT(softpipe_screen);
297
298 if (!screen)
299 return NULL;
300
301 screen->winsys = winsys;
302
303 screen->base.winsys = NULL;
304 screen->base.destroy = softpipe_destroy_screen;
305
306 screen->base.get_name = softpipe_get_name;
307 screen->base.get_vendor = softpipe_get_vendor;
308 screen->base.get_param = softpipe_get_param;
309 screen->base.get_shader_param = softpipe_get_shader_param;
310 screen->base.get_paramf = softpipe_get_paramf;
311 screen->base.is_format_supported = softpipe_is_format_supported;
312 screen->base.context_create = softpipe_create_context;
313 screen->base.flush_frontbuffer = softpipe_flush_frontbuffer;
314 screen->base.video_context_create = sp_video_create;
315
316 util_format_s3tc_init();
317
318 softpipe_init_screen_texture_funcs(&screen->base);
319 softpipe_init_screen_fence_funcs(&screen->base);
320
321 return &screen->base;
322 }