DRI2: report swap events correctly in direct rendered case
[mesa.git] / src / gallium / drivers / i965 / brw_screen_texture.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a 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, sublicense, 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
16 portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **********************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 */
31
32 #include "util/u_memory.h"
33 #include "util/u_simple_list.h"
34 #include "util/u_format.h"
35
36 #include "brw_screen.h"
37 #include "brw_defines.h"
38 #include "brw_structs.h"
39 #include "brw_winsys.h"
40
41
42
43 static GLuint translate_tex_target( unsigned target )
44 {
45 switch (target) {
46 case PIPE_TEXTURE_1D:
47 return BRW_SURFACE_1D;
48
49 case PIPE_TEXTURE_2D:
50 return BRW_SURFACE_2D;
51
52 case PIPE_TEXTURE_3D:
53 return BRW_SURFACE_3D;
54
55 case PIPE_TEXTURE_CUBE:
56 return BRW_SURFACE_CUBE;
57
58 default:
59 assert(0);
60 return BRW_SURFACE_1D;
61 }
62 }
63
64
65 static GLuint translate_tex_format( enum pipe_format pf )
66 {
67 switch( pf ) {
68 case PIPE_FORMAT_L8_UNORM:
69 return BRW_SURFACEFORMAT_L8_UNORM;
70
71 case PIPE_FORMAT_I8_UNORM:
72 return BRW_SURFACEFORMAT_I8_UNORM;
73
74 case PIPE_FORMAT_A8_UNORM:
75 return BRW_SURFACEFORMAT_A8_UNORM;
76
77 case PIPE_FORMAT_L16_UNORM:
78 return BRW_SURFACEFORMAT_L16_UNORM;
79
80 /* XXX: Add these to gallium
81 case PIPE_FORMAT_I16_UNORM:
82 return BRW_SURFACEFORMAT_I16_UNORM;
83
84 case PIPE_FORMAT_A16_UNORM:
85 return BRW_SURFACEFORMAT_A16_UNORM;
86 */
87
88 case PIPE_FORMAT_L8A8_UNORM:
89 return BRW_SURFACEFORMAT_L8A8_UNORM;
90
91 case PIPE_FORMAT_B5G6R5_UNORM:
92 return BRW_SURFACEFORMAT_B5G6R5_UNORM;
93
94 case PIPE_FORMAT_B5G5R5A1_UNORM:
95 return BRW_SURFACEFORMAT_B5G5R5A1_UNORM;
96
97 case PIPE_FORMAT_B4G4R4A4_UNORM:
98 return BRW_SURFACEFORMAT_B4G4R4A4_UNORM;
99
100 case PIPE_FORMAT_B8G8R8X8_UNORM:
101 return BRW_SURFACEFORMAT_R8G8B8X8_UNORM;
102
103 case PIPE_FORMAT_B8G8R8A8_UNORM:
104 return BRW_SURFACEFORMAT_B8G8R8A8_UNORM;
105
106 /*
107 * Video formats
108 */
109
110 case PIPE_FORMAT_YUYV:
111 return BRW_SURFACEFORMAT_YCRCB_NORMAL;
112
113 case PIPE_FORMAT_UYVY:
114 return BRW_SURFACEFORMAT_YCRCB_SWAPUVY;
115
116 /*
117 * Compressed formats.
118 */
119 /* XXX: Add FXT to gallium?
120 case PIPE_FORMAT_FXT1_RGBA:
121 return BRW_SURFACEFORMAT_FXT1;
122 */
123
124 case PIPE_FORMAT_DXT1_RGB:
125 return BRW_SURFACEFORMAT_DXT1_RGB;
126
127 case PIPE_FORMAT_DXT1_RGBA:
128 return BRW_SURFACEFORMAT_BC1_UNORM;
129
130 case PIPE_FORMAT_DXT3_RGBA:
131 return BRW_SURFACEFORMAT_BC2_UNORM;
132
133 case PIPE_FORMAT_DXT5_RGBA:
134 return BRW_SURFACEFORMAT_BC3_UNORM;
135
136 /*
137 * sRGB formats
138 */
139
140 case PIPE_FORMAT_A8B8G8R8_SRGB:
141 return BRW_SURFACEFORMAT_B8G8R8A8_UNORM_SRGB;
142
143 case PIPE_FORMAT_L8A8_SRGB:
144 return BRW_SURFACEFORMAT_L8A8_UNORM_SRGB;
145
146 case PIPE_FORMAT_L8_SRGB:
147 return BRW_SURFACEFORMAT_L8_UNORM_SRGB;
148
149 case PIPE_FORMAT_DXT1_SRGB:
150 return BRW_SURFACEFORMAT_BC1_UNORM_SRGB;
151
152 /*
153 * Depth formats
154 */
155
156 case PIPE_FORMAT_Z16_UNORM:
157 return BRW_SURFACEFORMAT_I16_UNORM;
158
159 case PIPE_FORMAT_Z24S8_UNORM:
160 case PIPE_FORMAT_Z24X8_UNORM:
161 return BRW_SURFACEFORMAT_I24X8_UNORM;
162
163 case PIPE_FORMAT_Z32_FLOAT:
164 return BRW_SURFACEFORMAT_I32_FLOAT;
165
166 /* XXX: presumably for bump mapping. Add this to mesa state
167 * tracker?
168 *
169 * XXX: Add flipped versions of these formats to Gallium.
170 */
171 case PIPE_FORMAT_R8G8_SNORM:
172 return BRW_SURFACEFORMAT_R8G8_SNORM;
173
174 case PIPE_FORMAT_R8G8B8A8_SNORM:
175 return BRW_SURFACEFORMAT_R8G8B8A8_SNORM;
176
177 default:
178 return BRW_SURFACEFORMAT_INVALID;
179 }
180 }
181
182
183
184
185
186 static struct pipe_texture *brw_texture_create( struct pipe_screen *screen,
187 const struct pipe_texture *templ )
188
189 {
190 struct brw_screen *bscreen = brw_screen(screen);
191 struct brw_texture *tex;
192 enum brw_buffer_type buffer_type;
193 enum pipe_error ret;
194 GLuint format;
195
196 tex = CALLOC_STRUCT(brw_texture);
197 if (tex == NULL)
198 return NULL;
199
200 memcpy(&tex->base, templ, sizeof *templ);
201 pipe_reference_init(&tex->base.reference, 1);
202 tex->base.screen = screen;
203
204 /* XXX: compressed textures need special treatment here
205 */
206 tex->cpp = util_format_get_blocksize(tex->base.format);
207 tex->compressed = util_format_is_compressed(tex->base.format);
208
209 make_empty_list(&tex->views[0]);
210 make_empty_list(&tex->views[1]);
211
212 /* XXX: No tiling with compressed textures??
213 */
214 if (tex->compressed == 0 &&
215 !bscreen->no_tiling)
216 {
217 if (bscreen->chipset.is_965 &&
218 util_format_is_depth_or_stencil(templ->format))
219 tex->tiling = BRW_TILING_Y;
220 else
221 tex->tiling = BRW_TILING_X;
222 }
223 else {
224 tex->tiling = BRW_TILING_NONE;
225 }
226
227
228
229
230 if (!brw_texture_layout( bscreen, tex ))
231 goto fail;
232
233
234 if (templ->tex_usage & (PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
235 PIPE_TEXTURE_USAGE_PRIMARY)) {
236 buffer_type = BRW_BUFFER_TYPE_SCANOUT;
237 }
238 else {
239 buffer_type = BRW_BUFFER_TYPE_TEXTURE;
240 }
241
242 ret = bscreen->sws->bo_alloc( bscreen->sws,
243 buffer_type,
244 tex->pitch * tex->total_height * tex->cpp,
245 64,
246 &tex->bo );
247 if (ret)
248 goto fail;
249
250 tex->ss.ss0.mipmap_layout_mode = BRW_SURFACE_MIPMAPLAYOUT_BELOW;
251 tex->ss.ss0.surface_type = translate_tex_target(tex->base.target);
252
253 format = translate_tex_format(tex->base.format);
254 assert(format != BRW_SURFACEFORMAT_INVALID);
255 tex->ss.ss0.surface_format = format;
256
257 /* This is ok for all textures with channel width 8bit or less:
258 */
259 /* tex->ss.ss0.data_return_format = BRW_SURFACERETURNFORMAT_S1; */
260
261
262 /* XXX: what happens when tex->bo->offset changes???
263 */
264 tex->ss.ss1.base_addr = 0; /* reloc */
265 tex->ss.ss2.mip_count = tex->base.last_level;
266 tex->ss.ss2.width = tex->base.width0 - 1;
267 tex->ss.ss2.height = tex->base.height0 - 1;
268
269 switch (tex->tiling) {
270 case BRW_TILING_NONE:
271 tex->ss.ss3.tiled_surface = 0;
272 tex->ss.ss3.tile_walk = 0;
273 break;
274 case BRW_TILING_X:
275 tex->ss.ss3.tiled_surface = 1;
276 tex->ss.ss3.tile_walk = BRW_TILEWALK_XMAJOR;
277 break;
278 case BRW_TILING_Y:
279 tex->ss.ss3.tiled_surface = 1;
280 tex->ss.ss3.tile_walk = BRW_TILEWALK_YMAJOR;
281 break;
282 }
283
284 tex->ss.ss3.pitch = (tex->pitch * tex->cpp) - 1;
285 tex->ss.ss3.depth = tex->base.depth0 - 1;
286
287 tex->ss.ss4.min_lod = 0;
288
289 if (tex->base.target == PIPE_TEXTURE_CUBE) {
290 tex->ss.ss0.cube_pos_x = 1;
291 tex->ss.ss0.cube_pos_y = 1;
292 tex->ss.ss0.cube_pos_z = 1;
293 tex->ss.ss0.cube_neg_x = 1;
294 tex->ss.ss0.cube_neg_y = 1;
295 tex->ss.ss0.cube_neg_z = 1;
296 }
297
298 return &tex->base;
299
300 fail:
301 bo_reference(&tex->bo, NULL);
302 FREE(tex);
303 return NULL;
304 }
305
306 static struct pipe_texture *brw_texture_blanket(struct pipe_screen *screen,
307 const struct pipe_texture *templ,
308 const unsigned *stride,
309 struct pipe_buffer *buffer)
310 {
311 return NULL;
312 }
313
314 static void brw_texture_destroy(struct pipe_texture *pt)
315 {
316 struct brw_texture *tex = brw_texture(pt);
317 bo_reference(&tex->bo, NULL);
318 FREE(pt);
319 }
320
321
322 static boolean brw_is_format_supported( struct pipe_screen *screen,
323 enum pipe_format format,
324 enum pipe_texture_target target,
325 unsigned tex_usage,
326 unsigned geom_flags )
327 {
328 return translate_tex_format(format) != BRW_SURFACEFORMAT_INVALID;
329 }
330
331
332 boolean brw_is_texture_referenced_by_bo( struct brw_screen *brw_screen,
333 struct pipe_texture *texture,
334 unsigned face,
335 unsigned level,
336 struct brw_winsys_buffer *bo )
337 {
338 struct brw_texture *tex = brw_texture(texture);
339 struct brw_surface *surf;
340 int i;
341
342 /* XXX: this is subject to false positives if the underlying
343 * texture BO is referenced, we can't tell whether the sub-region
344 * we care about participates in that.
345 */
346 if (brw_screen->sws->bo_references( bo, tex->bo ))
347 return TRUE;
348
349 /* Find any view on this texture for this face/level and see if it
350 * is referenced:
351 */
352 for (i = 0; i < 2; i++) {
353 foreach (surf, &tex->views[i]) {
354 if (surf->bo == tex->bo)
355 continue;
356
357 if (surf->id.bits.face != face ||
358 surf->id.bits.level != level)
359 continue;
360
361 if (brw_screen->sws->bo_references( bo, surf->bo))
362 return TRUE;
363 }
364 }
365
366 return FALSE;
367 }
368
369
370 /*
371 * Transfer functions
372 */
373
374 static struct pipe_transfer*
375 brw_get_tex_transfer(struct pipe_screen *screen,
376 struct pipe_texture *texture,
377 unsigned face, unsigned level, unsigned zslice,
378 enum pipe_transfer_usage usage, unsigned x, unsigned y,
379 unsigned w, unsigned h)
380 {
381 struct brw_texture *tex = brw_texture(texture);
382 struct brw_transfer *trans;
383 unsigned offset; /* in bytes */
384
385 if (texture->target == PIPE_TEXTURE_CUBE) {
386 offset = tex->image_offset[level][face];
387 } else if (texture->target == PIPE_TEXTURE_3D) {
388 offset = tex->image_offset[level][zslice];
389 } else {
390 offset = tex->image_offset[level][0];
391 assert(face == 0);
392 assert(zslice == 0);
393 }
394
395 trans = CALLOC_STRUCT(brw_transfer);
396 if (trans) {
397 pipe_texture_reference(&trans->base.texture, texture);
398 trans->base.x = x;
399 trans->base.y = y;
400 trans->base.width = w;
401 trans->base.height = h;
402 trans->base.stride = tex->pitch * tex->cpp;
403 trans->offset = offset;
404 trans->base.usage = usage;
405 }
406 return &trans->base;
407 }
408
409 static void *
410 brw_transfer_map(struct pipe_screen *screen,
411 struct pipe_transfer *transfer)
412 {
413 struct brw_texture *tex = brw_texture(transfer->texture);
414 struct brw_winsys_screen *sws = brw_screen(screen)->sws;
415 char *map;
416 unsigned usage = transfer->usage;
417
418 map = sws->bo_map(tex->bo,
419 BRW_DATA_OTHER,
420 0,
421 tex->bo->size,
422 (usage & PIPE_TRANSFER_WRITE) ? TRUE : FALSE,
423 (usage & 0) ? TRUE : FALSE,
424 (usage & 0) ? TRUE : FALSE);
425
426 if (!map)
427 return NULL;
428
429 /* XXX: blocksize and compressed textures
430 */
431 return map + brw_transfer(transfer)->offset +
432 transfer->y /* / transfer->block.height */ * transfer->stride +
433 transfer->x /* / transfer->block.width */ * brw_texture(transfer->texture)->cpp;
434 }
435
436 static void
437 brw_transfer_unmap(struct pipe_screen *screen,
438 struct pipe_transfer *transfer)
439 {
440 struct brw_texture *tex = brw_texture(transfer->texture);
441 struct brw_winsys_screen *sws = brw_screen(screen)->sws;
442
443 sws->bo_unmap(tex->bo);
444 }
445
446 static void
447 brw_tex_transfer_destroy(struct pipe_transfer *trans)
448 {
449 pipe_texture_reference(&trans->texture, NULL);
450 FREE(trans);
451 }
452
453
454 /*
455 * Functions exported to the winsys
456 */
457
458 boolean brw_texture_get_winsys_buffer(struct pipe_texture *texture,
459 struct brw_winsys_buffer **buffer,
460 unsigned *stride)
461 {
462 struct brw_texture *tex = brw_texture(texture);
463
464 *buffer = tex->bo;
465 if (stride)
466 *stride = tex->pitch * tex->cpp;
467
468 return TRUE;
469 }
470
471 struct pipe_texture *
472 brw_texture_blanket_winsys_buffer(struct pipe_screen *screen,
473 const struct pipe_texture *templ,
474 unsigned pitch,
475 unsigned tiling,
476 struct brw_winsys_buffer *buffer)
477 {
478 struct brw_screen *bscreen = brw_screen(screen);
479 struct brw_texture *tex;
480 GLuint format;
481
482 if (templ->target != PIPE_TEXTURE_2D ||
483 templ->last_level != 0 ||
484 templ->depth0 != 1)
485 return NULL;
486
487 if (util_format_is_compressed(templ->format))
488 return NULL;
489
490 tex = CALLOC_STRUCT(brw_texture);
491 if (!tex)
492 return NULL;
493
494 memcpy(&tex->base, templ, sizeof *templ);
495 pipe_reference_init(&tex->base.reference, 1);
496 tex->base.screen = screen;
497
498 /* XXX: cpp vs. blocksize
499 */
500 tex->cpp = util_format_get_blocksize(tex->base.format);
501 tex->tiling = tiling;
502
503 make_empty_list(&tex->views[0]);
504 make_empty_list(&tex->views[1]);
505
506 if (!brw_texture_layout(bscreen, tex))
507 goto fail;
508
509 /* XXX Maybe some more checks? */
510 if ((pitch / tex->cpp) < tex->pitch)
511 goto fail;
512
513 tex->pitch = pitch / tex->cpp;
514
515 tex->bo = buffer;
516
517 /* fix this warning */
518 #if 0
519 if (tex->size > buffer->size)
520 goto fail;
521 #endif
522
523 tex->ss.ss0.mipmap_layout_mode = BRW_SURFACE_MIPMAPLAYOUT_BELOW;
524 tex->ss.ss0.surface_type = translate_tex_target(tex->base.target);
525
526 format = translate_tex_format(tex->base.format);
527 assert(format != BRW_SURFACEFORMAT_INVALID);
528 tex->ss.ss0.surface_format = format;
529
530 /* This is ok for all textures with channel width 8bit or less:
531 */
532 /* tex->ss.ss0.data_return_format = BRW_SURFACERETURNFORMAT_S1; */
533
534
535 /* XXX: what happens when tex->bo->offset changes???
536 */
537 tex->ss.ss1.base_addr = 0; /* reloc */
538 tex->ss.ss2.mip_count = tex->base.last_level;
539 tex->ss.ss2.width = tex->base.width0 - 1;
540 tex->ss.ss2.height = tex->base.height0 - 1;
541
542 switch (tex->tiling) {
543 case BRW_TILING_NONE:
544 tex->ss.ss3.tiled_surface = 0;
545 tex->ss.ss3.tile_walk = 0;
546 break;
547 case BRW_TILING_X:
548 tex->ss.ss3.tiled_surface = 1;
549 tex->ss.ss3.tile_walk = BRW_TILEWALK_XMAJOR;
550 break;
551 case BRW_TILING_Y:
552 tex->ss.ss3.tiled_surface = 1;
553 tex->ss.ss3.tile_walk = BRW_TILEWALK_YMAJOR;
554 break;
555 }
556
557 tex->ss.ss3.pitch = (tex->pitch * tex->cpp) - 1;
558 tex->ss.ss3.depth = tex->base.depth0 - 1;
559
560 tex->ss.ss4.min_lod = 0;
561
562 return &tex->base;
563
564 fail:
565 FREE(tex);
566 return NULL;
567 }
568
569 void brw_screen_tex_init( struct brw_screen *brw_screen )
570 {
571 brw_screen->base.is_format_supported = brw_is_format_supported;
572 brw_screen->base.texture_create = brw_texture_create;
573 brw_screen->base.texture_destroy = brw_texture_destroy;
574 brw_screen->base.texture_blanket = brw_texture_blanket;
575 brw_screen->base.get_tex_transfer = brw_get_tex_transfer;
576 brw_screen->base.transfer_map = brw_transfer_map;
577 brw_screen->base.transfer_unmap = brw_transfer_unmap;
578 brw_screen->base.tex_transfer_destroy = brw_tex_transfer_destroy;
579 }