Merge remote branch 'origin/master' into nv50-compiler
[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 face,
233 unsigned level )
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 face/level 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 (surf->id.bits.face != face ||
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 struct pipe_subresource sr,
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->sr = sr;
289 transfer->usage = usage;
290 transfer->box = *box;
291 transfer->stride = tex->pitch * tex->cpp;
292
293 return transfer;
294 }
295
296
297 static void *
298 brw_texture_transfer_map(struct pipe_context *pipe,
299 struct pipe_transfer *transfer)
300 {
301 struct pipe_resource *resource = transfer->resource;
302 struct brw_texture *tex = brw_texture(transfer->resource);
303 struct brw_winsys_screen *sws = brw_screen(pipe->screen)->sws;
304 struct pipe_subresource sr = transfer->sr;
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_CUBE) {
312 offset = tex->image_offset[sr.level][sr.face];
313 }
314 else if (resource->target == PIPE_TEXTURE_3D) {
315 offset = tex->image_offset[sr.level][box->z];
316 }
317 else {
318 offset = tex->image_offset[sr.level][0];
319 assert(sr.face == 0);
320 assert(box->z == 0);
321 }
322
323 map = sws->bo_map(tex->bo,
324 BRW_DATA_OTHER,
325 0,
326 tex->bo->size,
327 (usage & PIPE_TRANSFER_WRITE) ? TRUE : FALSE,
328 (usage & 0) ? TRUE : FALSE,
329 (usage & 0) ? TRUE : FALSE);
330
331 if (!map)
332 return NULL;
333
334 return map + offset +
335 box->y / util_format_get_blockheight(format) * transfer->stride +
336 box->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
337 }
338
339 static void
340 brw_texture_transfer_unmap(struct pipe_context *pipe,
341 struct pipe_transfer *transfer)
342 {
343 struct brw_texture *tex = brw_texture(transfer->resource);
344 struct brw_winsys_screen *sws = brw_screen(pipe->screen)->sws;
345
346 sws->bo_unmap(tex->bo);
347 }
348
349
350
351
352
353 struct u_resource_vtbl brw_texture_vtbl =
354 {
355 brw_texture_get_handle, /* get_handle */
356 brw_texture_destroy, /* resource_destroy */
357 brw_texture_is_referenced, /* is_resource_referenced */
358 brw_texture_get_transfer, /* get_transfer */
359 u_default_transfer_destroy, /* transfer_destroy */
360 brw_texture_transfer_map, /* transfer_map */
361 u_default_transfer_flush_region, /* transfer_flush_region */
362 brw_texture_transfer_unmap, /* transfer_unmap */
363 u_default_transfer_inline_write /* transfer_inline_write */
364 };
365
366
367
368
369
370 struct pipe_resource *
371 brw_texture_create( struct pipe_screen *screen,
372 const struct pipe_resource *template )
373 {
374 struct brw_screen *bscreen = brw_screen(screen);
375 struct brw_texture *tex;
376 enum brw_buffer_type buffer_type;
377 enum pipe_error ret;
378 GLuint format;
379
380 tex = CALLOC_STRUCT(brw_texture);
381 if (tex == NULL)
382 return NULL;
383
384 tex->b.b = *template;
385 tex->b.vtbl = &brw_texture_vtbl;
386 pipe_reference_init(&tex->b.b.reference, 1);
387 tex->b.b.screen = screen;
388
389 /* XXX: compressed textures need special treatment here
390 */
391 tex->cpp = util_format_get_blocksize(tex->b.b.format);
392 tex->compressed = util_format_is_s3tc(tex->b.b.format);
393
394 make_empty_list(&tex->views[0]);
395 make_empty_list(&tex->views[1]);
396
397 /* XXX: No tiling with compressed textures??
398 */
399 if (tex->compressed == 0 &&
400 !bscreen->no_tiling)
401 {
402 if (bscreen->chipset.is_965 &&
403 util_format_is_depth_or_stencil(template->format))
404 tex->tiling = BRW_TILING_Y;
405 else
406 tex->tiling = BRW_TILING_X;
407 }
408 else {
409 tex->tiling = BRW_TILING_NONE;
410 }
411
412
413 if (!brw_texture_layout( bscreen, tex ))
414 goto fail;
415
416
417 if (template->bind & (PIPE_BIND_SCANOUT |
418 PIPE_BIND_SHARED)) {
419 buffer_type = BRW_BUFFER_TYPE_SCANOUT;
420 }
421 else {
422 buffer_type = BRW_BUFFER_TYPE_TEXTURE;
423 }
424
425 ret = bscreen->sws->bo_alloc( bscreen->sws,
426 buffer_type,
427 tex->pitch * tex->total_height * tex->cpp,
428 64,
429 &tex->bo );
430 if (ret)
431 goto fail;
432
433 tex->ss.ss0.mipmap_layout_mode = BRW_SURFACE_MIPMAPLAYOUT_BELOW;
434 tex->ss.ss0.surface_type = translate_tex_target(tex->b.b.target);
435
436 format = translate_tex_format(tex->b.b.format);
437 assert(format != BRW_SURFACEFORMAT_INVALID);
438 tex->ss.ss0.surface_format = format;
439
440 /* This is ok for all textures with channel width 8bit or less:
441 */
442 /* tex->ss.ss0.data_return_format = BRW_SURFACERETURNFORMAT_S1; */
443
444
445 /* XXX: what happens when tex->bo->offset changes???
446 */
447 tex->ss.ss1.base_addr = 0; /* reloc */
448 tex->ss.ss2.mip_count = tex->b.b.last_level;
449 tex->ss.ss2.width = tex->b.b.width0 - 1;
450 tex->ss.ss2.height = tex->b.b.height0 - 1;
451
452 switch (tex->tiling) {
453 case BRW_TILING_NONE:
454 tex->ss.ss3.tiled_surface = 0;
455 tex->ss.ss3.tile_walk = 0;
456 break;
457 case BRW_TILING_X:
458 tex->ss.ss3.tiled_surface = 1;
459 tex->ss.ss3.tile_walk = BRW_TILEWALK_XMAJOR;
460 break;
461 case BRW_TILING_Y:
462 tex->ss.ss3.tiled_surface = 1;
463 tex->ss.ss3.tile_walk = BRW_TILEWALK_YMAJOR;
464 break;
465 }
466
467 tex->ss.ss3.pitch = (tex->pitch * tex->cpp) - 1;
468 tex->ss.ss3.depth = tex->b.b.depth0 - 1;
469
470 tex->ss.ss4.min_lod = 0;
471
472 if (tex->b.b.target == PIPE_TEXTURE_CUBE) {
473 tex->ss.ss0.cube_pos_x = 1;
474 tex->ss.ss0.cube_pos_y = 1;
475 tex->ss.ss0.cube_pos_z = 1;
476 tex->ss.ss0.cube_neg_x = 1;
477 tex->ss.ss0.cube_neg_y = 1;
478 tex->ss.ss0.cube_neg_z = 1;
479 }
480
481 return &tex->b.b;
482
483 fail:
484 bo_reference(&tex->bo, NULL);
485 FREE(tex);
486 return NULL;
487 }
488
489
490 struct pipe_resource *
491 brw_texture_from_handle(struct pipe_screen *screen,
492 const struct pipe_resource *template,
493 struct winsys_handle *whandle)
494 {
495 struct brw_screen *bscreen = brw_screen(screen);
496 struct brw_texture *tex;
497 struct brw_winsys_buffer *buffer;
498 unsigned tiling;
499 unsigned pitch;
500 GLuint format;
501
502 if ((template->target != PIPE_TEXTURE_2D
503 && template->target != PIPE_TEXTURE_RECT) ||
504 template->last_level != 0 ||
505 template->depth0 != 1)
506 return NULL;
507
508 if (util_format_is_s3tc(template->format))
509 return NULL;
510
511 tex = CALLOC_STRUCT(brw_texture);
512 if (!tex)
513 return NULL;
514
515 if (bscreen->sws->bo_from_handle(bscreen->sws, whandle, &pitch, &tiling, &buffer) != PIPE_OK)
516 goto fail;
517
518 tex->b.b = *template;
519 tex->b.vtbl = &brw_texture_vtbl;
520 pipe_reference_init(&tex->b.b.reference, 1);
521 tex->b.b.screen = screen;
522
523 /* XXX: cpp vs. blocksize
524 */
525 tex->cpp = util_format_get_blocksize(tex->b.b.format);
526 tex->tiling = tiling;
527
528 make_empty_list(&tex->views[0]);
529 make_empty_list(&tex->views[1]);
530
531 if (!brw_texture_layout(bscreen, tex))
532 goto fail;
533
534 /* XXX Maybe some more checks? */
535 if ((pitch / tex->cpp) < tex->pitch)
536 goto fail;
537
538 tex->pitch = pitch / tex->cpp;
539
540 tex->bo = buffer;
541
542 /* fix this warning */
543 #if 0
544 if (tex->size > buffer->size)
545 goto fail;
546 #endif
547
548 tex->ss.ss0.mipmap_layout_mode = BRW_SURFACE_MIPMAPLAYOUT_BELOW;
549 tex->ss.ss0.surface_type = translate_tex_target(tex->b.b.target);
550
551 format = translate_tex_format(tex->b.b.format);
552 assert(format != BRW_SURFACEFORMAT_INVALID);
553 tex->ss.ss0.surface_format = format;
554
555 /* This is ok for all textures with channel width 8bit or less:
556 */
557 /* tex->ss.ss0.data_return_format = BRW_SURFACERETURNFORMAT_S1; */
558
559
560 /* XXX: what happens when tex->bo->offset changes???
561 */
562 tex->ss.ss1.base_addr = 0; /* reloc */
563 tex->ss.ss2.mip_count = tex->b.b.last_level;
564 tex->ss.ss2.width = tex->b.b.width0 - 1;
565 tex->ss.ss2.height = tex->b.b.height0 - 1;
566
567 switch (tex->tiling) {
568 case BRW_TILING_NONE:
569 tex->ss.ss3.tiled_surface = 0;
570 tex->ss.ss3.tile_walk = 0;
571 break;
572 case BRW_TILING_X:
573 tex->ss.ss3.tiled_surface = 1;
574 tex->ss.ss3.tile_walk = BRW_TILEWALK_XMAJOR;
575 break;
576 case BRW_TILING_Y:
577 tex->ss.ss3.tiled_surface = 1;
578 tex->ss.ss3.tile_walk = BRW_TILEWALK_YMAJOR;
579 break;
580 }
581
582 tex->ss.ss3.pitch = (tex->pitch * tex->cpp) - 1;
583 tex->ss.ss3.depth = tex->b.b.depth0 - 1;
584
585 tex->ss.ss4.min_lod = 0;
586
587 return &tex->b.b;
588
589 fail:
590 FREE(tex);
591 return NULL;
592 }
593
594
595 #if 0
596 boolean brw_is_format_supported( struct pipe_screen *screen,
597 enum pipe_format format,
598 enum pipe_texture_target target,
599 unsigned sample_count,
600 unsigned tex_usage,
601 unsigned geom_flags )
602 {
603 return translate_tex_format(format) != BRW_SURFACEFORMAT_INVALID;
604 }
605 #endif