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