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