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