Merge branch 'mesa_7_7_branch'
[mesa.git] / src / gallium / drivers / r300 / r300_screen.c
1 /*
2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22
23 #include "pipe/p_inlines.h"
24 #include "util/u_format.h"
25 #include "util/u_memory.h"
26 #include "util/u_simple_screen.h"
27
28 #include "r300_context.h"
29 #include "r300_screen.h"
30 #include "r300_texture.h"
31
32 #include "radeon_winsys.h"
33
34 /* Return the identifier behind whom the brave coders responsible for this
35 * amalgamation of code, sweat, and duct tape, routinely obscure their names.
36 *
37 * ...I should have just put "Corbin Simpson", but I'm not that cool.
38 *
39 * (Or egotistical. Yet.) */
40 static const char* r300_get_vendor(struct pipe_screen* pscreen)
41 {
42 return "X.Org R300 Project";
43 }
44
45 static const char* chip_families[] = {
46 "R300",
47 "R350",
48 "R360",
49 "RV350",
50 "RV370",
51 "RV380",
52 "R420",
53 "R423",
54 "R430",
55 "R480",
56 "R481",
57 "RV410",
58 "RS400",
59 "RC410",
60 "RS480",
61 "RS482",
62 "RS600",
63 "RS690",
64 "RS740",
65 "RV515",
66 "R520",
67 "RV530",
68 "R580",
69 "RV560",
70 "RV570"
71 };
72
73 static const char* r300_get_name(struct pipe_screen* pscreen)
74 {
75 struct r300_screen* r300screen = r300_screen(pscreen);
76
77 return chip_families[r300screen->caps->family];
78 }
79
80 static int r300_get_param(struct pipe_screen* pscreen, int param)
81 {
82 struct r300_screen* r300screen = r300_screen(pscreen);
83
84 switch (param) {
85 case PIPE_CAP_MAX_TEXTURE_IMAGE_UNITS:
86 case PIPE_CAP_MAX_COMBINED_SAMPLERS:
87 /* XXX I'm told this goes up to 16 */
88 return 8;
89 case PIPE_CAP_NPOT_TEXTURES:
90 /* XXX enable now to get GL2.1 API,
91 * figure out later how to emulate this */
92 return 1;
93 case PIPE_CAP_TWO_SIDED_STENCIL:
94 if (r300screen->caps->is_r500) {
95 return 1;
96 } else {
97 return 0;
98 }
99 case PIPE_CAP_GLSL:
100 /* I'll be frank. This is a lie.
101 *
102 * We don't truly support GLSL on any of this driver's chipsets.
103 * To be fair, no chipset supports the full GLSL specification
104 * to the best of our knowledge, but some of the less esoteric
105 * features are still missing here.
106 *
107 * Rather than cripple ourselves intentionally, I'm going to set
108 * this flag, and as Gallium's interface continues to change, I
109 * hope that this single monolithic GLSL enable can slowly get
110 * split down into many different pieces and the state tracker
111 * will handle fallbacks transparently, like it should.
112 *
113 * ~ C.
114 */
115 return 1;
116 case PIPE_CAP_ANISOTROPIC_FILTER:
117 return 1;
118 case PIPE_CAP_POINT_SPRITE:
119 return 1;
120 case PIPE_CAP_MAX_RENDER_TARGETS:
121 return 4;
122 case PIPE_CAP_OCCLUSION_QUERY:
123 return 1;
124 case PIPE_CAP_TEXTURE_SHADOW_MAP:
125 return 1;
126 case PIPE_CAP_MAX_TEXTURE_2D_LEVELS:
127 case PIPE_CAP_MAX_TEXTURE_3D_LEVELS:
128 case PIPE_CAP_MAX_TEXTURE_CUBE_LEVELS:
129 if (r300screen->caps->is_r500) {
130 /* 13 == 4096 */
131 return 13;
132 } else {
133 /* 12 == 2048 */
134 return 12;
135 }
136 case PIPE_CAP_TEXTURE_MIRROR_CLAMP:
137 return 1;
138 case PIPE_CAP_TEXTURE_MIRROR_REPEAT:
139 return 1;
140 case PIPE_CAP_MAX_VERTEX_TEXTURE_UNITS:
141 return 0;
142 case PIPE_CAP_TGSI_CONT_SUPPORTED:
143 return 0;
144 case PIPE_CAP_BLEND_EQUATION_SEPARATE:
145 return 1;
146 case PIPE_CAP_SM3:
147 if (r300screen->caps->is_r500) {
148 return 1;
149 } else {
150 return 0;
151 }
152 default:
153 debug_printf("r300: Implementation error: Bad param %d\n",
154 param);
155 return 0;
156 }
157 }
158
159 static float r300_get_paramf(struct pipe_screen* pscreen, int param)
160 {
161 struct r300_screen* r300screen = r300_screen(pscreen);
162
163 switch (param) {
164 case PIPE_CAP_MAX_LINE_WIDTH:
165 case PIPE_CAP_MAX_LINE_WIDTH_AA:
166 case PIPE_CAP_MAX_POINT_WIDTH:
167 case PIPE_CAP_MAX_POINT_WIDTH_AA:
168 /* The maximum dimensions of the colorbuffer are our practical
169 * rendering limits. 2048 pixels should be enough for anybody. */
170 if (r300screen->caps->is_r500) {
171 return 4096.0f;
172 } else {
173 return 2048.0f;
174 }
175 case PIPE_CAP_MAX_TEXTURE_ANISOTROPY:
176 return 16.0f;
177 case PIPE_CAP_MAX_TEXTURE_LOD_BIAS:
178 return 16.0f;
179 default:
180 debug_printf("r300: Implementation error: Bad paramf %d\n",
181 param);
182 return 0.0f;
183 }
184 }
185
186 static boolean r300_is_format_supported(struct pipe_screen* screen,
187 enum pipe_format format,
188 enum pipe_texture_target target,
189 unsigned usage,
190 unsigned geom_flags)
191 {
192 uint32_t retval = 0;
193 boolean is_r500 = r300_screen(screen)->caps->is_r500;
194
195 if (target >= PIPE_MAX_TEXTURE_TYPES) {
196 debug_printf("r300: Implementation error: Received bogus texture "
197 "target %d in %s\n", target, __FUNCTION__);
198 return FALSE;
199 }
200
201 switch (format) {
202 /* Supported formats. */
203 /* Colorbuffer */
204 case PIPE_FORMAT_A4R4G4B4_UNORM:
205 case PIPE_FORMAT_R5G6B5_UNORM:
206 case PIPE_FORMAT_A1R5G5B5_UNORM:
207 retval = usage &
208 (PIPE_TEXTURE_USAGE_RENDER_TARGET |
209 PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
210 PIPE_TEXTURE_USAGE_PRIMARY);
211 break;
212
213 /* Texture */
214 case PIPE_FORMAT_A8R8G8B8_SRGB:
215 case PIPE_FORMAT_R8G8B8A8_SRGB:
216 case PIPE_FORMAT_DXT1_RGB:
217 case PIPE_FORMAT_DXT1_RGBA:
218 case PIPE_FORMAT_DXT3_RGBA:
219 case PIPE_FORMAT_DXT5_RGBA:
220 case PIPE_FORMAT_YCBCR:
221 case PIPE_FORMAT_L8_UNORM:
222 case PIPE_FORMAT_A8L8_UNORM:
223 retval = usage & PIPE_TEXTURE_USAGE_SAMPLER;
224 break;
225
226 /* Colorbuffer or texture */
227 case PIPE_FORMAT_A8R8G8B8_UNORM:
228 case PIPE_FORMAT_X8R8G8B8_UNORM:
229 case PIPE_FORMAT_R8G8B8A8_UNORM:
230 case PIPE_FORMAT_R8G8B8X8_UNORM:
231 case PIPE_FORMAT_I8_UNORM:
232 retval = usage &
233 (PIPE_TEXTURE_USAGE_RENDER_TARGET |
234 PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
235 PIPE_TEXTURE_USAGE_PRIMARY |
236 PIPE_TEXTURE_USAGE_SAMPLER);
237 break;
238
239 /* Z buffer or texture */
240 case PIPE_FORMAT_Z16_UNORM:
241 retval = usage &
242 (PIPE_TEXTURE_USAGE_DEPTH_STENCIL |
243 PIPE_TEXTURE_USAGE_SAMPLER);
244 break;
245
246 /* 24bit Z buffer can only be used as a texture on R500. */
247 case PIPE_FORMAT_Z24X8_UNORM:
248 /* Z buffer with stencil or texture */
249 case PIPE_FORMAT_Z24S8_UNORM:
250 retval = usage &
251 (PIPE_TEXTURE_USAGE_DEPTH_STENCIL |
252 (is_r500 ? PIPE_TEXTURE_USAGE_SAMPLER : 0));
253 break;
254
255 /* Definitely unsupported formats. */
256 /* Non-usable Z buffer/stencil formats. */
257 case PIPE_FORMAT_Z32_UNORM:
258 case PIPE_FORMAT_S8Z24_UNORM:
259 case PIPE_FORMAT_X8Z24_UNORM:
260 SCREEN_DBG(r300_screen(screen), DBG_TEX,
261 "r300: Note: Got unsupported format: %s in %s\n",
262 pf_name(format), __FUNCTION__);
263 return FALSE;
264
265 /* XXX Add all remaining gallium-supported formats,
266 * see util/u_format.csv. */
267
268 default:
269 /* Unknown format... */
270 debug_printf("r300: Warning: Got unknown format: %s in %s\n",
271 pf_name(format), __FUNCTION__);
272 break;
273 }
274
275 /* If usage was a mask that contained multiple bits, and not all of them
276 * are supported, this will catch that and return FALSE.
277 * e.g. usage = 2 | 4; retval = 4; (retval >= usage) == FALSE
278 *
279 * This also returns FALSE for any unknown formats.
280 */
281 return (retval >= usage);
282 }
283
284 static struct pipe_transfer*
285 r300_get_tex_transfer(struct pipe_screen *screen,
286 struct pipe_texture *texture,
287 unsigned face, unsigned level, unsigned zslice,
288 enum pipe_transfer_usage usage, unsigned x, unsigned y,
289 unsigned w, unsigned h)
290 {
291 struct r300_texture *tex = (struct r300_texture *)texture;
292 struct r300_transfer *trans;
293 struct r300_screen *rscreen = r300_screen(screen);
294 unsigned offset;
295
296 offset = r300_texture_get_offset(tex, level, zslice, face); /* in bytes */
297
298 trans = CALLOC_STRUCT(r300_transfer);
299 if (trans) {
300 pipe_texture_reference(&trans->transfer.texture, texture);
301 trans->transfer.x = x;
302 trans->transfer.y = y;
303 trans->transfer.width = w;
304 trans->transfer.height = h;
305 trans->transfer.stride = r300_texture_get_stride(rscreen, tex, level);
306 trans->transfer.usage = usage;
307 trans->transfer.zslice = zslice;
308 trans->transfer.face = face;
309
310 trans->offset = offset;
311 }
312 return &trans->transfer;
313 }
314
315 static void
316 r300_tex_transfer_destroy(struct pipe_transfer *trans)
317 {
318 pipe_texture_reference(&trans->texture, NULL);
319 FREE(trans);
320 }
321
322 static void* r300_transfer_map(struct pipe_screen* screen,
323 struct pipe_transfer* transfer)
324 {
325 struct r300_texture* tex = (struct r300_texture*)transfer->texture;
326 char* map;
327 enum pipe_format format = tex->tex.format;
328
329 map = pipe_buffer_map(screen, tex->buffer,
330 pipe_transfer_buffer_flags(transfer));
331
332 if (!map) {
333 return NULL;
334 }
335
336 return map + r300_transfer(transfer)->offset +
337 transfer->y / util_format_get_blockheight(format) * transfer->stride +
338 transfer->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
339 }
340
341 static void r300_transfer_unmap(struct pipe_screen* screen,
342 struct pipe_transfer* transfer)
343 {
344 struct r300_texture* tex = (struct r300_texture*)transfer->texture;
345 pipe_buffer_unmap(screen, tex->buffer);
346 }
347
348 static void r300_destroy_screen(struct pipe_screen* pscreen)
349 {
350 struct r300_screen* r300screen = r300_screen(pscreen);
351
352 FREE(r300screen->caps);
353 FREE(r300screen);
354 }
355
356 struct pipe_screen* r300_create_screen(struct radeon_winsys* radeon_winsys)
357 {
358 struct r300_screen* r300screen = CALLOC_STRUCT(r300_screen);
359 struct r300_capabilities* caps = CALLOC_STRUCT(r300_capabilities);
360
361 if (!r300screen || !caps)
362 return NULL;
363
364 caps->pci_id = radeon_winsys->pci_id;
365 caps->num_frag_pipes = radeon_winsys->gb_pipes;
366 caps->num_z_pipes = radeon_winsys->z_pipes;
367
368 r300_init_debug(r300screen);
369 r300_parse_chipset(caps);
370
371 r300screen->caps = caps;
372 r300screen->screen.winsys = (struct pipe_winsys*)radeon_winsys;
373 r300screen->screen.destroy = r300_destroy_screen;
374 r300screen->screen.get_name = r300_get_name;
375 r300screen->screen.get_vendor = r300_get_vendor;
376 r300screen->screen.get_param = r300_get_param;
377 r300screen->screen.get_paramf = r300_get_paramf;
378 r300screen->screen.is_format_supported = r300_is_format_supported;
379 r300screen->screen.get_tex_transfer = r300_get_tex_transfer;
380 r300screen->screen.tex_transfer_destroy = r300_tex_transfer_destroy;
381 r300screen->screen.transfer_map = r300_transfer_map;
382 r300screen->screen.transfer_unmap = r300_transfer_unmap;
383
384 r300_init_screen_texture_functions(&r300screen->screen);
385 u_simple_screen_init(&r300screen->screen);
386
387 return &r300screen->screen;
388 }