Squashed commit of gallium-no-texture-blanket
[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_SCANOUT |
235 PIPE_TEXTURE_USAGE_SHARED)) {
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 *
307 brw_texture_from_handle(struct pipe_screen *screen,
308 const struct pipe_texture *templ,
309 struct winsys_handle *whandle)
310 {
311 struct brw_screen *bscreen = brw_screen(screen);
312 struct brw_texture *tex;
313 struct brw_winsys_buffer *buffer;
314 unsigned tiling;
315 unsigned pitch;
316
317 if (templ->target != PIPE_TEXTURE_2D ||
318 templ->last_level != 0 ||
319 templ->depth0 != 1)
320 return NULL;
321
322 if (util_format_is_compressed(templ->format))
323 return NULL;
324
325 tex = CALLOC_STRUCT(brw_texture);
326 if (!tex)
327 return NULL;
328
329 if (bscreen->sws->bo_from_handle(bscreen->sws, whandle, &pitch, &tiling, &buffer) != PIPE_OK)
330 goto fail;
331
332 memcpy(&tex->base, templ, sizeof *templ);
333 pipe_reference_init(&tex->base.reference, 1);
334 tex->base.screen = screen;
335
336 /* XXX: cpp vs. blocksize
337 */
338 tex->cpp = util_format_get_blocksize(tex->base.format);
339 tex->tiling = tiling;
340
341 make_empty_list(&tex->views[0]);
342 make_empty_list(&tex->views[1]);
343
344 if (!brw_texture_layout(bscreen, tex))
345 goto fail;
346
347 /* XXX Maybe some more checks? */
348 if ((pitch / tex->cpp) < tex->pitch)
349 goto fail;
350
351 tex->pitch = pitch / tex->cpp;
352
353 tex->bo = buffer;
354
355 /* fix this warning */
356 #if 0
357 if (tex->size > buffer->size)
358 goto fail;
359 #endif
360
361 tex->ss.ss0.mipmap_layout_mode = BRW_SURFACE_MIPMAPLAYOUT_BELOW;
362 tex->ss.ss0.surface_type = translate_tex_target(tex->base.target);
363 tex->ss.ss0.surface_format = translate_tex_format(tex->base.format);
364 assert(tex->ss.ss0.surface_format != BRW_SURFACEFORMAT_INVALID);
365
366 /* This is ok for all textures with channel width 8bit or less:
367 */
368 /* tex->ss.ss0.data_return_format = BRW_SURFACERETURNFORMAT_S1; */
369
370
371 /* XXX: what happens when tex->bo->offset changes???
372 */
373 tex->ss.ss1.base_addr = 0; /* reloc */
374 tex->ss.ss2.mip_count = tex->base.last_level;
375 tex->ss.ss2.width = tex->base.width0 - 1;
376 tex->ss.ss2.height = tex->base.height0 - 1;
377
378 switch (tex->tiling) {
379 case BRW_TILING_NONE:
380 tex->ss.ss3.tiled_surface = 0;
381 tex->ss.ss3.tile_walk = 0;
382 break;
383 case BRW_TILING_X:
384 tex->ss.ss3.tiled_surface = 1;
385 tex->ss.ss3.tile_walk = BRW_TILEWALK_XMAJOR;
386 break;
387 case BRW_TILING_Y:
388 tex->ss.ss3.tiled_surface = 1;
389 tex->ss.ss3.tile_walk = BRW_TILEWALK_YMAJOR;
390 break;
391 }
392
393 tex->ss.ss3.pitch = (tex->pitch * tex->cpp) - 1;
394 tex->ss.ss3.depth = tex->base.depth0 - 1;
395
396 tex->ss.ss4.min_lod = 0;
397
398 return &tex->base;
399
400 fail:
401 FREE(tex);
402 return NULL;
403 }
404
405 static boolean
406 brw_texture_get_handle(struct pipe_screen *screen,
407 struct pipe_texture *texture,
408 struct winsys_handle *whandle)
409 {
410 struct brw_screen *bscreen = brw_screen(screen);
411 struct brw_texture *tex = brw_texture(texture);
412 unsigned stride;
413
414 stride = tex->pitch * tex->cpp;
415
416 return bscreen->sws->bo_get_handle(tex->bo, whandle, stride);
417 }
418
419
420
421 static void brw_texture_destroy(struct pipe_texture *pt)
422 {
423 struct brw_texture *tex = brw_texture(pt);
424 bo_reference(&tex->bo, NULL);
425 FREE(pt);
426 }
427
428
429 static boolean brw_is_format_supported( struct pipe_screen *screen,
430 enum pipe_format format,
431 enum pipe_texture_target target,
432 unsigned tex_usage,
433 unsigned geom_flags )
434 {
435 return translate_tex_format(format) != BRW_SURFACEFORMAT_INVALID;
436 }
437
438
439 boolean brw_is_texture_referenced_by_bo( struct brw_screen *brw_screen,
440 struct pipe_texture *texture,
441 unsigned face,
442 unsigned level,
443 struct brw_winsys_buffer *bo )
444 {
445 struct brw_texture *tex = brw_texture(texture);
446 struct brw_surface *surf;
447 int i;
448
449 /* XXX: this is subject to false positives if the underlying
450 * texture BO is referenced, we can't tell whether the sub-region
451 * we care about participates in that.
452 */
453 if (brw_screen->sws->bo_references( bo, tex->bo ))
454 return TRUE;
455
456 /* Find any view on this texture for this face/level and see if it
457 * is referenced:
458 */
459 for (i = 0; i < 2; i++) {
460 foreach (surf, &tex->views[i]) {
461 if (surf->bo == tex->bo)
462 continue;
463
464 if (surf->id.bits.face != face ||
465 surf->id.bits.level != level)
466 continue;
467
468 if (brw_screen->sws->bo_references( bo, surf->bo))
469 return TRUE;
470 }
471 }
472
473 return FALSE;
474 }
475
476
477 /*
478 * Transfer functions
479 */
480
481 static struct pipe_transfer*
482 brw_get_tex_transfer(struct pipe_screen *screen,
483 struct pipe_texture *texture,
484 unsigned face, unsigned level, unsigned zslice,
485 enum pipe_transfer_usage usage, unsigned x, unsigned y,
486 unsigned w, unsigned h)
487 {
488 struct brw_texture *tex = brw_texture(texture);
489 struct brw_transfer *trans;
490 unsigned offset; /* in bytes */
491
492 if (texture->target == PIPE_TEXTURE_CUBE) {
493 offset = tex->image_offset[level][face];
494 } else if (texture->target == PIPE_TEXTURE_3D) {
495 offset = tex->image_offset[level][zslice];
496 } else {
497 offset = tex->image_offset[level][0];
498 assert(face == 0);
499 assert(zslice == 0);
500 }
501
502 trans = CALLOC_STRUCT(brw_transfer);
503 if (trans) {
504 pipe_texture_reference(&trans->base.texture, texture);
505 trans->base.x = x;
506 trans->base.y = y;
507 trans->base.width = w;
508 trans->base.height = h;
509 trans->base.stride = tex->pitch * tex->cpp;
510 trans->offset = offset;
511 trans->base.usage = usage;
512 }
513 return &trans->base;
514 }
515
516 static void *
517 brw_transfer_map(struct pipe_screen *screen,
518 struct pipe_transfer *transfer)
519 {
520 struct brw_texture *tex = brw_texture(transfer->texture);
521 struct brw_winsys_screen *sws = brw_screen(screen)->sws;
522 char *map;
523 unsigned usage = transfer->usage;
524
525 map = sws->bo_map(tex->bo,
526 BRW_DATA_OTHER,
527 0,
528 tex->bo->size,
529 (usage & PIPE_TRANSFER_WRITE) ? TRUE : FALSE,
530 (usage & 0) ? TRUE : FALSE,
531 (usage & 0) ? TRUE : FALSE);
532
533 if (!map)
534 return NULL;
535
536 /* XXX: blocksize and compressed textures
537 */
538 return map + brw_transfer(transfer)->offset +
539 transfer->y /* / transfer->block.height */ * transfer->stride +
540 transfer->x /* / transfer->block.width */ * brw_texture(transfer->texture)->cpp;
541 }
542
543 static void
544 brw_transfer_unmap(struct pipe_screen *screen,
545 struct pipe_transfer *transfer)
546 {
547 struct brw_texture *tex = brw_texture(transfer->texture);
548 struct brw_winsys_screen *sws = brw_screen(screen)->sws;
549
550 sws->bo_unmap(tex->bo);
551 }
552
553 static void
554 brw_tex_transfer_destroy(struct pipe_transfer *trans)
555 {
556 pipe_texture_reference(&trans->texture, NULL);
557 FREE(trans);
558 }
559
560
561 void brw_screen_tex_init( struct brw_screen *brw_screen )
562 {
563 brw_screen->base.is_format_supported = brw_is_format_supported;
564 brw_screen->base.texture_create = brw_texture_create;
565 brw_screen->base.texture_from_handle = brw_texture_from_handle;
566 brw_screen->base.texture_get_handle = brw_texture_get_handle;
567 brw_screen->base.texture_destroy = brw_texture_destroy;
568 brw_screen->base.get_tex_transfer = brw_get_tex_transfer;
569 brw_screen->base.transfer_map = brw_transfer_map;
570 brw_screen->base.transfer_unmap = brw_transfer_unmap;
571 brw_screen->base.tex_transfer_destroy = brw_tex_transfer_destroy;
572 }