Squashed commit of the following:
[profile/ivi/mesa.git] / src / gallium / drivers / r300 / r300_screen.c
1 /*
2  * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3  * Copyright 2010 Marek Olšák <maraeo@gmail.com>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * on the rights to use, copy, modify, merge, publish, distribute, sub
9  * license, and/or sell copies of the Software, and to permit persons to whom
10  * the Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22  * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23
24 #include "util/u_format.h"
25 #include "util/u_memory.h"
26
27 #include "r300_context.h"
28 #include "r300_texture.h"
29 #include "r300_screen_buffer.h"
30 #include "r300_winsys.h"
31
32 /* Return the identifier behind whom the brave coders responsible for this
33  * amalgamation of code, sweat, and duct tape, routinely obscure their names.
34  *
35  * ...I should have just put "Corbin Simpson", but I'm not that cool.
36  *
37  * (Or egotistical. Yet.) */
38 static const char* r300_get_vendor(struct pipe_screen* pscreen)
39 {
40     return "X.Org R300 Project";
41 }
42
43 static const char* chip_families[] = {
44     "R300",
45     "R350",
46     "R360",
47     "RV350",
48     "RV370",
49     "RV380",
50     "R420",
51     "R423",
52     "R430",
53     "R480",
54     "R481",
55     "RV410",
56     "RS400",
57     "RC410",
58     "RS480",
59     "RS482",
60     "RS600",
61     "RS690",
62     "RS740",
63     "RV515",
64     "R520",
65     "RV530",
66     "R580",
67     "RV560",
68     "RV570"
69 };
70
71 static const char* r300_get_name(struct pipe_screen* pscreen)
72 {
73     struct r300_screen* r300screen = r300_screen(pscreen);
74
75     return chip_families[r300screen->caps.family];
76 }
77
78 static int r300_get_param(struct pipe_screen* pscreen, int param)
79 {
80     struct r300_screen* r300screen = r300_screen(pscreen);
81
82     switch (param) {
83         case PIPE_CAP_MAX_TEXTURE_IMAGE_UNITS:
84         case PIPE_CAP_MAX_COMBINED_SAMPLERS:
85             return r300screen->caps.num_tex_units;
86         case PIPE_CAP_NPOT_TEXTURES:
87             /* XXX enable now to get GL2.1 API,
88              * figure out later how to emulate this */
89             return 1;
90         case PIPE_CAP_TWO_SIDED_STENCIL:
91             return 1;
92         case PIPE_CAP_GLSL:
93             /* I'll be frank. This is a lie.
94              *
95              * We don't truly support GLSL on any of this driver's chipsets.
96              * To be fair, no chipset supports the full GLSL specification
97              * to the best of our knowledge, but some of the less esoteric
98              * features are still missing here.
99              *
100              * Rather than cripple ourselves intentionally, I'm going to set
101              * this flag, and as Gallium's interface continues to change, I
102              * hope that this single monolithic GLSL enable can slowly get
103              * split down into many different pieces and the state tracker
104              * will handle fallbacks transparently, like it should.
105              *
106              * ~ C.
107              */
108             return 1;
109         case PIPE_CAP_DUAL_SOURCE_BLEND:
110             return 0;
111         case PIPE_CAP_ANISOTROPIC_FILTER:
112             return 1;
113         case PIPE_CAP_POINT_SPRITE:
114             return 1;
115         case PIPE_CAP_MAX_RENDER_TARGETS:
116             return 4;
117         case PIPE_CAP_OCCLUSION_QUERY:
118             return 1;
119         case PIPE_CAP_TEXTURE_SHADOW_MAP:
120             return 1;
121         case PIPE_CAP_MAX_TEXTURE_2D_LEVELS:
122         case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
123         case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
124             if (r300screen->caps.is_r500) {
125                 /* 13 == 4096 */
126                 return 13;
127             } else {
128                 /* 12 == 2048 */
129                 return 12;
130             }
131         case PIPE_CAP_TEXTURE_MIRROR_CLAMP:
132             return 1;
133         case PIPE_CAP_TEXTURE_MIRROR_REPEAT:
134             return 1;
135         case PIPE_CAP_MAX_VERTEX_TEXTURE_UNITS:
136             return 0;
137         case PIPE_CAP_TGSI_CONT_SUPPORTED:
138             return 0;
139         case PIPE_CAP_BLEND_EQUATION_SEPARATE:
140             return 1;
141         case PIPE_CAP_SM3:
142             if (r300screen->caps.is_r500) {
143                 return 1;
144             } else {
145                 return 0;
146             }
147         case PIPE_CAP_MAX_CONST_BUFFERS:
148             return 1;
149         case PIPE_CAP_MAX_CONST_BUFFER_SIZE:
150             return 256;
151         case PIPE_CAP_INDEP_BLEND_ENABLE:
152             return 0;
153         case PIPE_CAP_INDEP_BLEND_FUNC:
154             return 0;
155         case PIPE_CAP_TGSI_FS_COORD_ORIGIN_UPPER_LEFT:
156         case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_HALF_INTEGER:
157             return 1;
158         case PIPE_CAP_TGSI_FS_COORD_ORIGIN_LOWER_LEFT:
159         case PIPE_CAP_TGSI_FS_COORD_PIXEL_CENTER_INTEGER:
160             return 0;
161         default:
162             fprintf(stderr, "r300: Implementation error: Bad param %d\n",
163                 param);
164             return 0;
165     }
166 }
167
168 static float r300_get_paramf(struct pipe_screen* pscreen, int param)
169 {
170     struct r300_screen* r300screen = r300_screen(pscreen);
171
172     switch (param) {
173         case PIPE_CAP_MAX_LINE_WIDTH:
174         case PIPE_CAP_MAX_LINE_WIDTH_AA:
175         case PIPE_CAP_MAX_POINT_WIDTH:
176         case PIPE_CAP_MAX_POINT_WIDTH_AA:
177             /* The maximum dimensions of the colorbuffer are our practical
178              * rendering limits. 2048 pixels should be enough for anybody. */
179             if (r300screen->caps.is_r500) {
180                 return 4096.0f;
181             } else if (r300screen->caps.is_r400) {
182                 return 4021.0f;
183             } else {
184                 return 2560.0f;
185             }
186         case PIPE_CAP_MAX_TEXTURE_ANISOTROPY:
187             return 16.0f;
188         case PIPE_CAP_MAX_TEXTURE_LOD_BIAS:
189             return 16.0f;
190         default:
191             fprintf(stderr, "r300: Implementation error: Bad paramf %d\n",
192                 param);
193             return 0.0f;
194     }
195 }
196
197 static boolean r300_is_format_supported(struct pipe_screen* screen,
198                                         enum pipe_format format,
199                                         enum pipe_texture_target target,
200                                         unsigned usage,
201                                         unsigned geom_flags)
202 {
203     uint32_t retval = 0;
204     boolean is_r500 = r300_screen(screen)->caps.is_r500;
205     boolean is_r400 = r300_screen(screen)->caps.is_r400;
206     boolean is_z24 = format == PIPE_FORMAT_X8Z24_UNORM ||
207                      format == PIPE_FORMAT_S8_USCALED_Z24_UNORM;
208     boolean is_color2101010 = format == PIPE_FORMAT_R10G10B10A2_UNORM;
209     boolean is_ati1n = format == PIPE_FORMAT_RGTC1_UNORM ||
210                        format == PIPE_FORMAT_RGTC1_SNORM;
211     boolean is_ati2n = format == PIPE_FORMAT_RGTC2_UNORM ||
212                        format == PIPE_FORMAT_RGTC2_SNORM;
213
214     if (target >= PIPE_MAX_TEXTURE_TYPES) {
215         fprintf(stderr, "r300: Implementation error: Received bogus texture "
216             "target %d in %s\n", target, __FUNCTION__);
217         return FALSE;
218     }
219
220     /* Check sampler format support. */
221     if ((usage & PIPE_BIND_SAMPLER_VIEW) &&
222         /* Z24 cannot be sampled from on non-r5xx. */
223         (is_r500 || !is_z24) &&
224         /* ATI1N is r5xx-only. */
225         (is_r500 || !is_ati1n) &&
226         /* ATI2N is supported on r4xx-r5xx. */
227         (is_r400 || is_r500 || !is_ati2n) &&
228         r300_is_sampler_format_supported(format)) {
229         retval |= PIPE_BIND_SAMPLER_VIEW;
230     }
231
232     /* Check colorbuffer format support. */
233     if ((usage & (PIPE_BIND_RENDER_TARGET |
234                   PIPE_BIND_DISPLAY_TARGET |
235                   PIPE_BIND_SCANOUT |
236                   PIPE_BIND_SHARED)) &&
237         /* 2101010 cannot be rendered to on non-r5xx. */
238         (is_r500 || !is_color2101010) &&
239         r300_is_colorbuffer_format_supported(format)) {
240         retval |= usage &
241             (PIPE_BIND_RENDER_TARGET |
242              PIPE_BIND_DISPLAY_TARGET |
243              PIPE_BIND_SCANOUT |
244              PIPE_BIND_SHARED);
245     }
246
247     /* Check depth-stencil format support. */
248     if (usage & PIPE_BIND_DEPTH_STENCIL &&
249         r300_is_zs_format_supported(format)) {
250         retval |= PIPE_BIND_DEPTH_STENCIL;
251     }
252
253     return retval == usage;
254 }
255
256 static void r300_destroy_screen(struct pipe_screen* pscreen)
257 {
258     struct r300_screen* r300screen = r300_screen(pscreen);
259     struct r300_winsys_screen *rws = r300_winsys_screen(pscreen);
260
261     if (rws)
262       rws->destroy(rws);
263
264     FREE(r300screen);
265 }
266
267 static void r300_fence_reference(struct pipe_screen *screen,
268                                  struct pipe_fence_handle **ptr,
269                                  struct pipe_fence_handle *fence)
270 {
271 }
272
273 static int r300_fence_signalled(struct pipe_screen *screen,
274                                 struct pipe_fence_handle *fence,
275                                 unsigned flags)
276 {
277     return 0;
278 }
279
280 static int r300_fence_finish(struct pipe_screen *screen,
281                              struct pipe_fence_handle *fence,
282                              unsigned flags)
283 {
284     return 0;
285 }
286
287 struct pipe_screen* r300_create_screen(struct r300_winsys_screen *rws)
288 {
289     struct r300_screen *r300screen = CALLOC_STRUCT(r300_screen);
290
291     if (!r300screen) {
292         FREE(r300screen);
293         return NULL;
294     }
295
296     r300screen->caps.pci_id = rws->get_value(rws, R300_VID_PCI_ID);
297     r300screen->caps.num_frag_pipes = rws->get_value(rws, R300_VID_GB_PIPES);
298     r300screen->caps.num_z_pipes = rws->get_value(rws, R300_VID_Z_PIPES);
299
300     r300_init_debug(r300screen);
301     r300_parse_chipset(&r300screen->caps);
302
303     r300screen->rws = rws;
304     r300screen->screen.winsys = (struct pipe_winsys*)rws;
305     r300screen->screen.destroy = r300_destroy_screen;
306     r300screen->screen.get_name = r300_get_name;
307     r300screen->screen.get_vendor = r300_get_vendor;
308     r300screen->screen.get_param = r300_get_param;
309     r300screen->screen.get_paramf = r300_get_paramf;
310     r300screen->screen.is_format_supported = r300_is_format_supported;
311     r300screen->screen.context_create = r300_create_context;
312
313     r300screen->screen.fence_reference = r300_fence_reference;
314     r300screen->screen.fence_signalled = r300_fence_signalled;
315     r300screen->screen.fence_finish = r300_fence_finish;
316
317     r300_init_screen_resource_functions(r300screen);
318
319     return &r300screen->screen;
320 }
321
322 struct r300_winsys_screen *
323 r300_winsys_screen(struct pipe_screen *screen)
324 {
325     return r300_screen(screen)->rws;
326 }