Merge remote branch 'origin/master' into nvc0-new
[mesa.git] / src / gallium / drivers / i965 / brw_resource_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 #include "brw_batchbuffer.h"
41 #include "brw_context.h"
42 #include "brw_resource.h"
43
44
45 /**
46 * Subclass of pipe_transfer
47 */
48 struct brw_transfer
49 {
50 struct pipe_transfer base;
51
52 unsigned offset;
53 };
54
55 static INLINE struct brw_transfer *
56 brw_transfer(struct pipe_transfer *transfer)
57 {
58 return (struct brw_transfer *)transfer;
59 }
60
61
62 static GLuint translate_tex_target( unsigned target )
63 {
64 switch (target) {
65 case PIPE_TEXTURE_1D:
66 return BRW_SURFACE_1D;
67
68 case PIPE_TEXTURE_2D:
69 case PIPE_TEXTURE_RECT:
70 return BRW_SURFACE_2D;
71
72 case PIPE_TEXTURE_3D:
73 return BRW_SURFACE_3D;
74
75 case PIPE_TEXTURE_CUBE:
76 return BRW_SURFACE_CUBE;
77
78 default:
79 assert(0);
80 return BRW_SURFACE_1D;
81 }
82 }
83
84
85 static GLuint translate_tex_format( enum pipe_format pf )
86 {
87 switch( pf ) {
88 case PIPE_FORMAT_L8_UNORM:
89 return BRW_SURFACEFORMAT_L8_UNORM;
90
91 case PIPE_FORMAT_I8_UNORM:
92 return BRW_SURFACEFORMAT_I8_UNORM;
93
94 case PIPE_FORMAT_A8_UNORM:
95 return BRW_SURFACEFORMAT_A8_UNORM;
96
97 case PIPE_FORMAT_L16_UNORM:
98 return BRW_SURFACEFORMAT_L16_UNORM;
99
100 /* XXX: Add these to gallium
101 case PIPE_FORMAT_I16_UNORM:
102 return BRW_SURFACEFORMAT_I16_UNORM;
103
104 case PIPE_FORMAT_A16_UNORM:
105 return BRW_SURFACEFORMAT_A16_UNORM;
106 */
107
108 case PIPE_FORMAT_L8A8_UNORM:
109 return BRW_SURFACEFORMAT_L8A8_UNORM;
110
111 case PIPE_FORMAT_B5G6R5_UNORM:
112 return BRW_SURFACEFORMAT_B5G6R5_UNORM;
113
114 case PIPE_FORMAT_B5G5R5A1_UNORM:
115 return BRW_SURFACEFORMAT_B5G5R5A1_UNORM;
116
117 case PIPE_FORMAT_B4G4R4A4_UNORM:
118 return BRW_SURFACEFORMAT_B4G4R4A4_UNORM;
119
120 case PIPE_FORMAT_B8G8R8X8_UNORM:
121 return BRW_SURFACEFORMAT_R8G8B8X8_UNORM;
122
123 case PIPE_FORMAT_B8G8R8A8_UNORM:
124 return BRW_SURFACEFORMAT_B8G8R8A8_UNORM;
125
126 /*
127 * Video formats
128 */
129
130 case PIPE_FORMAT_YUYV:
131 return BRW_SURFACEFORMAT_YCRCB_NORMAL;
132
133 case PIPE_FORMAT_UYVY:
134 return BRW_SURFACEFORMAT_YCRCB_SWAPUVY;
135
136 /*
137 * Compressed formats.
138 */
139 /* XXX: Add FXT to gallium?
140 case PIPE_FORMAT_FXT1_RGBA:
141 return BRW_SURFACEFORMAT_FXT1;
142 */
143
144 case PIPE_FORMAT_DXT1_RGB:
145 return BRW_SURFACEFORMAT_DXT1_RGB;
146
147 case PIPE_FORMAT_DXT1_RGBA:
148 return BRW_SURFACEFORMAT_BC1_UNORM;
149
150 case PIPE_FORMAT_DXT3_RGBA:
151 return BRW_SURFACEFORMAT_BC2_UNORM;
152
153 case PIPE_FORMAT_DXT5_RGBA:
154 return BRW_SURFACEFORMAT_BC3_UNORM;
155
156 /*
157 * sRGB formats
158 */
159
160 case PIPE_FORMAT_A8B8G8R8_SRGB:
161 return BRW_SURFACEFORMAT_B8G8R8A8_UNORM_SRGB;
162
163 case PIPE_FORMAT_L8A8_SRGB:
164 return BRW_SURFACEFORMAT_L8A8_UNORM_SRGB;
165
166 case PIPE_FORMAT_L8_SRGB:
167 return BRW_SURFACEFORMAT_L8_UNORM_SRGB;
168
169 case PIPE_FORMAT_DXT1_SRGB:
170 return BRW_SURFACEFORMAT_BC1_UNORM_SRGB;
171
172 /*
173 * Depth formats
174 */
175
176 case PIPE_FORMAT_Z16_UNORM:
177 return BRW_SURFACEFORMAT_I16_UNORM;
178
179 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
180 case PIPE_FORMAT_Z24X8_UNORM:
181 return BRW_SURFACEFORMAT_I24X8_UNORM;
182
183 case PIPE_FORMAT_Z32_FLOAT:
184 return BRW_SURFACEFORMAT_I32_FLOAT;
185
186 /* XXX: presumably for bump mapping. Add this to mesa state
187 * tracker?
188 *
189 * XXX: Add flipped versions of these formats to Gallium.
190 */
191 case PIPE_FORMAT_R8G8_SNORM:
192 return BRW_SURFACEFORMAT_R8G8_SNORM;
193
194 case PIPE_FORMAT_R8G8B8A8_SNORM:
195 return BRW_SURFACEFORMAT_R8G8B8A8_SNORM;
196
197 default:
198 return BRW_SURFACEFORMAT_INVALID;
199 }
200 }
201
202
203 static boolean
204 brw_texture_get_handle(struct pipe_screen *screen,
205 struct pipe_resource *texture,
206 struct winsys_handle *whandle)
207 {
208 struct brw_screen *bscreen = brw_screen(screen);
209 struct brw_texture *tex = brw_texture(texture);
210 unsigned stride;
211
212 stride = tex->pitch * tex->cpp;
213
214 return bscreen->sws->bo_get_handle(tex->bo, whandle, stride) == PIPE_OK;
215 }
216
217
218
219 static void brw_texture_destroy(struct pipe_screen *screen,
220 struct pipe_resource *pt)
221 {
222 struct brw_texture *tex = brw_texture(pt);
223 bo_reference(&tex->bo, NULL);
224 FREE(pt);
225 }
226
227
228
229
230 static unsigned brw_texture_is_referenced( struct pipe_context *pipe,
231 struct pipe_resource *texture,
232 unsigned level,
233 int layer )
234 {
235 struct brw_context *brw = brw_context(pipe);
236 struct brw_screen *bscreen = brw_screen(pipe->screen);
237 struct brw_winsys_buffer *batch_bo = brw->batch->buf;
238 struct brw_texture *tex = brw_texture(texture);
239 struct brw_surface *surf;
240 int i;
241
242 /* XXX: this is subject to false positives if the underlying
243 * texture BO is referenced, we can't tell whether the sub-region
244 * we care about participates in that.
245 */
246 if (bscreen->sws->bo_references( batch_bo, tex->bo ))
247 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
248
249 /* Find any view on this texture for this level/layer and see if it
250 * is referenced:
251 */
252 for (i = 0; i < 2; i++) {
253 foreach (surf, &tex->views[i]) {
254 if (surf->bo == tex->bo)
255 continue;
256
257 if (!(layer == -1 || surf->id.bits.layer == layer) ||
258 surf->id.bits.level != level)
259 continue;
260
261 if (bscreen->sws->bo_references( batch_bo, surf->bo))
262 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
263 }
264 }
265
266 return PIPE_UNREFERENCED;
267 }
268
269
270 /*
271 * Transfer functions
272 */
273
274
275 static struct pipe_transfer *
276 brw_texture_get_transfer(struct pipe_context *context,
277 struct pipe_resource *resource,
278 unsigned level,
279 unsigned usage,
280 const struct pipe_box *box)
281 {
282 struct brw_texture *tex = brw_texture(resource);
283 struct pipe_transfer *transfer = CALLOC_STRUCT(pipe_transfer);
284 if (transfer == NULL)
285 return NULL;
286
287 transfer->resource = resource;
288 transfer->level = level;
289 transfer->usage = usage;
290 transfer->box = *box;
291 transfer->stride = tex->pitch * tex->cpp;
292 /* FIXME: layer_stride */
293
294 return transfer;
295 }
296
297
298 static void *
299 brw_texture_transfer_map(struct pipe_context *pipe,
300 struct pipe_transfer *transfer)
301 {
302 struct pipe_resource *resource = transfer->resource;
303 struct brw_texture *tex = brw_texture(transfer->resource);
304 struct brw_winsys_screen *sws = brw_screen(pipe->screen)->sws;
305 struct pipe_box *box = &transfer->box;
306 enum pipe_format format = resource->format;
307 unsigned usage = transfer->usage;
308 unsigned offset;
309 char *map;
310
311 if (resource->target != PIPE_TEXTURE_3D &&
312 resource->target != PIPE_TEXTURE_CUBE)
313 assert(box->z == 0);
314 offset = tex->image_offset[transfer->level][box->z];
315
316 map = sws->bo_map(tex->bo,
317 BRW_DATA_OTHER,
318 0,
319 tex->bo->size,
320 (usage & PIPE_TRANSFER_WRITE) ? TRUE : FALSE,
321 (usage & 0) ? TRUE : FALSE,
322 (usage & 0) ? TRUE : FALSE);
323
324 if (!map)
325 return NULL;
326
327 return map + offset +
328 box->y / util_format_get_blockheight(format) * transfer->stride +
329 box->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
330 }
331
332 static void
333 brw_texture_transfer_unmap(struct pipe_context *pipe,
334 struct pipe_transfer *transfer)
335 {
336 struct brw_texture *tex = brw_texture(transfer->resource);
337 struct brw_winsys_screen *sws = brw_screen(pipe->screen)->sws;
338
339 sws->bo_unmap(tex->bo);
340 }
341
342
343
344
345
346 struct u_resource_vtbl brw_texture_vtbl =
347 {
348 brw_texture_get_handle, /* get_handle */
349 brw_texture_destroy, /* resource_destroy */
350 brw_texture_is_referenced, /* is_resource_referenced */
351 brw_texture_get_transfer, /* get_transfer */
352 u_default_transfer_destroy, /* transfer_destroy */
353 brw_texture_transfer_map, /* transfer_map */
354 u_default_transfer_flush_region, /* transfer_flush_region */
355 brw_texture_transfer_unmap, /* transfer_unmap */
356 u_default_transfer_inline_write /* transfer_inline_write */
357 };
358
359
360
361
362
363 struct pipe_resource *
364 brw_texture_create( struct pipe_screen *screen,
365 const struct pipe_resource *template )
366 {
367 struct brw_screen *bscreen = brw_screen(screen);
368 struct brw_texture *tex;
369 enum brw_buffer_type buffer_type;
370 enum pipe_error ret;
371 GLuint format;
372
373 tex = CALLOC_STRUCT(brw_texture);
374 if (tex == NULL)
375 return NULL;
376
377 tex->b.b = *template;
378 tex->b.vtbl = &brw_texture_vtbl;
379 pipe_reference_init(&tex->b.b.reference, 1);
380 tex->b.b.screen = screen;
381
382 /* XXX: compressed textures need special treatment here
383 */
384 tex->cpp = util_format_get_blocksize(tex->b.b.format);
385 tex->compressed = util_format_is_s3tc(tex->b.b.format);
386
387 make_empty_list(&tex->views[0]);
388 make_empty_list(&tex->views[1]);
389
390 /* XXX: No tiling with compressed textures??
391 */
392 if (tex->compressed == 0 &&
393 !bscreen->no_tiling)
394 {
395 if (bscreen->chipset.is_965 &&
396 util_format_is_depth_or_stencil(template->format))
397 tex->tiling = BRW_TILING_Y;
398 else
399 tex->tiling = BRW_TILING_X;
400 }
401 else {
402 tex->tiling = BRW_TILING_NONE;
403 }
404
405
406 if (!brw_texture_layout( bscreen, tex ))
407 goto fail;
408
409
410 if (template->bind & (PIPE_BIND_SCANOUT |
411 PIPE_BIND_SHARED)) {
412 buffer_type = BRW_BUFFER_TYPE_SCANOUT;
413 }
414 else {
415 buffer_type = BRW_BUFFER_TYPE_TEXTURE;
416 }
417
418 ret = bscreen->sws->bo_alloc( bscreen->sws,
419 buffer_type,
420 tex->pitch * tex->total_height * tex->cpp,
421 64,
422 &tex->bo );
423 if (ret)
424 goto fail;
425
426 tex->ss.ss0.mipmap_layout_mode = BRW_SURFACE_MIPMAPLAYOUT_BELOW;
427 tex->ss.ss0.surface_type = translate_tex_target(tex->b.b.target);
428
429 format = translate_tex_format(tex->b.b.format);
430 assert(format != BRW_SURFACEFORMAT_INVALID);
431 tex->ss.ss0.surface_format = format;
432
433 /* This is ok for all textures with channel width 8bit or less:
434 */
435 /* tex->ss.ss0.data_return_format = BRW_SURFACERETURNFORMAT_S1; */
436
437
438 /* XXX: what happens when tex->bo->offset changes???
439 */
440 tex->ss.ss1.base_addr = 0; /* reloc */
441 tex->ss.ss2.mip_count = tex->b.b.last_level;
442 tex->ss.ss2.width = tex->b.b.width0 - 1;
443 tex->ss.ss2.height = tex->b.b.height0 - 1;
444
445 switch (tex->tiling) {
446 case BRW_TILING_NONE:
447 tex->ss.ss3.tiled_surface = 0;
448 tex->ss.ss3.tile_walk = 0;
449 break;
450 case BRW_TILING_X:
451 tex->ss.ss3.tiled_surface = 1;
452 tex->ss.ss3.tile_walk = BRW_TILEWALK_XMAJOR;
453 break;
454 case BRW_TILING_Y:
455 tex->ss.ss3.tiled_surface = 1;
456 tex->ss.ss3.tile_walk = BRW_TILEWALK_YMAJOR;
457 break;
458 }
459
460 tex->ss.ss3.pitch = (tex->pitch * tex->cpp) - 1;
461 tex->ss.ss3.depth = tex->b.b.depth0 - 1;
462
463 tex->ss.ss4.min_lod = 0;
464
465 if (tex->b.b.target == PIPE_TEXTURE_CUBE) {
466 tex->ss.ss0.cube_pos_x = 1;
467 tex->ss.ss0.cube_pos_y = 1;
468 tex->ss.ss0.cube_pos_z = 1;
469 tex->ss.ss0.cube_neg_x = 1;
470 tex->ss.ss0.cube_neg_y = 1;
471 tex->ss.ss0.cube_neg_z = 1;
472 }
473
474 return &tex->b.b;
475
476 fail:
477 bo_reference(&tex->bo, NULL);
478 FREE(tex);
479 return NULL;
480 }
481
482
483 struct pipe_resource *
484 brw_texture_from_handle(struct pipe_screen *screen,
485 const struct pipe_resource *template,
486 struct winsys_handle *whandle)
487 {
488 struct brw_screen *bscreen = brw_screen(screen);
489 struct brw_texture *tex;
490 struct brw_winsys_buffer *buffer;
491 unsigned tiling;
492 unsigned pitch;
493 GLuint format;
494
495 if ((template->target != PIPE_TEXTURE_2D
496 && template->target != PIPE_TEXTURE_RECT) ||
497 template->last_level != 0 ||
498 template->depth0 != 1)
499 return NULL;
500
501 if (util_format_is_s3tc(template->format))
502 return NULL;
503
504 tex = CALLOC_STRUCT(brw_texture);
505 if (!tex)
506 return NULL;
507
508 if (bscreen->sws->bo_from_handle(bscreen->sws, whandle, &pitch, &tiling, &buffer) != PIPE_OK)
509 goto fail;
510
511 tex->b.b = *template;
512 tex->b.vtbl = &brw_texture_vtbl;
513 pipe_reference_init(&tex->b.b.reference, 1);
514 tex->b.b.screen = screen;
515
516 /* XXX: cpp vs. blocksize
517 */
518 tex->cpp = util_format_get_blocksize(tex->b.b.format);
519 tex->tiling = tiling;
520
521 make_empty_list(&tex->views[0]);
522 make_empty_list(&tex->views[1]);
523
524 if (!brw_texture_layout(bscreen, tex))
525 goto fail;
526
527 /* XXX Maybe some more checks? */
528 if ((pitch / tex->cpp) < tex->pitch)
529 goto fail;
530
531 tex->pitch = pitch / tex->cpp;
532
533 tex->bo = buffer;
534
535 /* fix this warning */
536 #if 0
537 if (tex->size > buffer->size)
538 goto fail;
539 #endif
540
541 tex->ss.ss0.mipmap_layout_mode = BRW_SURFACE_MIPMAPLAYOUT_BELOW;
542 tex->ss.ss0.surface_type = translate_tex_target(tex->b.b.target);
543
544 format = translate_tex_format(tex->b.b.format);
545 assert(format != BRW_SURFACEFORMAT_INVALID);
546 tex->ss.ss0.surface_format = format;
547
548 /* This is ok for all textures with channel width 8bit or less:
549 */
550 /* tex->ss.ss0.data_return_format = BRW_SURFACERETURNFORMAT_S1; */
551
552
553 /* XXX: what happens when tex->bo->offset changes???
554 */
555 tex->ss.ss1.base_addr = 0; /* reloc */
556 tex->ss.ss2.mip_count = tex->b.b.last_level;
557 tex->ss.ss2.width = tex->b.b.width0 - 1;
558 tex->ss.ss2.height = tex->b.b.height0 - 1;
559
560 switch (tex->tiling) {
561 case BRW_TILING_NONE:
562 tex->ss.ss3.tiled_surface = 0;
563 tex->ss.ss3.tile_walk = 0;
564 break;
565 case BRW_TILING_X:
566 tex->ss.ss3.tiled_surface = 1;
567 tex->ss.ss3.tile_walk = BRW_TILEWALK_XMAJOR;
568 break;
569 case BRW_TILING_Y:
570 tex->ss.ss3.tiled_surface = 1;
571 tex->ss.ss3.tile_walk = BRW_TILEWALK_YMAJOR;
572 break;
573 }
574
575 tex->ss.ss3.pitch = (tex->pitch * tex->cpp) - 1;
576 tex->ss.ss3.depth = tex->b.b.depth0 - 1;
577
578 tex->ss.ss4.min_lod = 0;
579
580 return &tex->b.b;
581
582 fail:
583 FREE(tex);
584 return NULL;
585 }
586
587
588 #if 0
589 boolean brw_is_format_supported( struct pipe_screen *screen,
590 enum pipe_format format,
591 enum pipe_texture_target target,
592 unsigned sample_count,
593 unsigned tex_usage,
594 unsigned geom_flags )
595 {
596 return translate_tex_format(format) != BRW_SURFACEFORMAT_INVALID;
597 }
598 #endif