2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
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:
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.
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.
26 **********************************************************************/
29 * Keith Whitwell <keith@tungstengraphics.com>
32 #include "util/u_memory.h"
33 #include "util/u_simple_list.h"
34 #include "util/u_format.h"
36 #include "brw_screen.h"
37 #include "brw_defines.h"
38 #include "brw_structs.h"
39 #include "brw_winsys.h"
40 #include "brw_context.h"
45 static GLuint translate_tex_target( unsigned target )
49 return BRW_SURFACE_1D;
52 return BRW_SURFACE_2D;
55 return BRW_SURFACE_3D;
57 case PIPE_TEXTURE_CUBE:
58 return BRW_SURFACE_CUBE;
62 return BRW_SURFACE_1D;
67 static GLuint translate_tex_format( enum pipe_format pf )
70 case PIPE_FORMAT_L8_UNORM:
71 return BRW_SURFACEFORMAT_L8_UNORM;
73 case PIPE_FORMAT_I8_UNORM:
74 return BRW_SURFACEFORMAT_I8_UNORM;
76 case PIPE_FORMAT_A8_UNORM:
77 return BRW_SURFACEFORMAT_A8_UNORM;
79 case PIPE_FORMAT_L16_UNORM:
80 return BRW_SURFACEFORMAT_L16_UNORM;
82 /* XXX: Add these to gallium
83 case PIPE_FORMAT_I16_UNORM:
84 return BRW_SURFACEFORMAT_I16_UNORM;
86 case PIPE_FORMAT_A16_UNORM:
87 return BRW_SURFACEFORMAT_A16_UNORM;
90 case PIPE_FORMAT_L8A8_UNORM:
91 return BRW_SURFACEFORMAT_L8A8_UNORM;
93 case PIPE_FORMAT_B5G6R5_UNORM:
94 return BRW_SURFACEFORMAT_B5G6R5_UNORM;
96 case PIPE_FORMAT_B5G5R5A1_UNORM:
97 return BRW_SURFACEFORMAT_B5G5R5A1_UNORM;
99 case PIPE_FORMAT_B4G4R4A4_UNORM:
100 return BRW_SURFACEFORMAT_B4G4R4A4_UNORM;
102 case PIPE_FORMAT_B8G8R8X8_UNORM:
103 return BRW_SURFACEFORMAT_R8G8B8X8_UNORM;
105 case PIPE_FORMAT_B8G8R8A8_UNORM:
106 return BRW_SURFACEFORMAT_B8G8R8A8_UNORM;
112 case PIPE_FORMAT_YUYV:
113 return BRW_SURFACEFORMAT_YCRCB_NORMAL;
115 case PIPE_FORMAT_UYVY:
116 return BRW_SURFACEFORMAT_YCRCB_SWAPUVY;
119 * Compressed formats.
121 /* XXX: Add FXT to gallium?
122 case PIPE_FORMAT_FXT1_RGBA:
123 return BRW_SURFACEFORMAT_FXT1;
126 case PIPE_FORMAT_DXT1_RGB:
127 return BRW_SURFACEFORMAT_DXT1_RGB;
129 case PIPE_FORMAT_DXT1_RGBA:
130 return BRW_SURFACEFORMAT_BC1_UNORM;
132 case PIPE_FORMAT_DXT3_RGBA:
133 return BRW_SURFACEFORMAT_BC2_UNORM;
135 case PIPE_FORMAT_DXT5_RGBA:
136 return BRW_SURFACEFORMAT_BC3_UNORM;
142 case PIPE_FORMAT_A8B8G8R8_SRGB:
143 return BRW_SURFACEFORMAT_B8G8R8A8_UNORM_SRGB;
145 case PIPE_FORMAT_L8A8_SRGB:
146 return BRW_SURFACEFORMAT_L8A8_UNORM_SRGB;
148 case PIPE_FORMAT_L8_SRGB:
149 return BRW_SURFACEFORMAT_L8_UNORM_SRGB;
151 case PIPE_FORMAT_DXT1_SRGB:
152 return BRW_SURFACEFORMAT_BC1_UNORM_SRGB;
158 case PIPE_FORMAT_Z16_UNORM:
159 return BRW_SURFACEFORMAT_I16_UNORM;
161 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
162 case PIPE_FORMAT_Z24X8_UNORM:
163 return BRW_SURFACEFORMAT_I24X8_UNORM;
165 case PIPE_FORMAT_Z32_FLOAT:
166 return BRW_SURFACEFORMAT_I32_FLOAT;
168 /* XXX: presumably for bump mapping. Add this to mesa state
171 * XXX: Add flipped versions of these formats to Gallium.
173 case PIPE_FORMAT_R8G8_SNORM:
174 return BRW_SURFACEFORMAT_R8G8_SNORM;
176 case PIPE_FORMAT_R8G8B8A8_SNORM:
177 return BRW_SURFACEFORMAT_R8G8B8A8_SNORM;
180 return BRW_SURFACEFORMAT_INVALID;
188 static struct pipe_texture *brw_texture_create( struct pipe_screen *screen,
189 const struct pipe_texture *templ )
192 struct brw_screen *bscreen = brw_screen(screen);
193 struct brw_texture *tex;
194 enum brw_buffer_type buffer_type;
198 tex = CALLOC_STRUCT(brw_texture);
202 memcpy(&tex->base, templ, sizeof *templ);
203 pipe_reference_init(&tex->base.reference, 1);
204 tex->base.screen = screen;
206 /* XXX: compressed textures need special treatment here
208 tex->cpp = util_format_get_blocksize(tex->base.format);
209 tex->compressed = util_format_is_s3tc(tex->base.format);
211 make_empty_list(&tex->views[0]);
212 make_empty_list(&tex->views[1]);
214 /* XXX: No tiling with compressed textures??
216 if (tex->compressed == 0 &&
219 if (bscreen->chipset.is_965 &&
220 util_format_is_depth_or_stencil(templ->format))
221 tex->tiling = BRW_TILING_Y;
223 tex->tiling = BRW_TILING_X;
226 tex->tiling = BRW_TILING_NONE;
232 if (!brw_texture_layout( bscreen, tex ))
236 if (templ->tex_usage & (PIPE_TEXTURE_USAGE_SCANOUT |
237 PIPE_TEXTURE_USAGE_SHARED)) {
238 buffer_type = BRW_BUFFER_TYPE_SCANOUT;
241 buffer_type = BRW_BUFFER_TYPE_TEXTURE;
244 ret = bscreen->sws->bo_alloc( bscreen->sws,
246 tex->pitch * tex->total_height * tex->cpp,
252 tex->ss.ss0.mipmap_layout_mode = BRW_SURFACE_MIPMAPLAYOUT_BELOW;
253 tex->ss.ss0.surface_type = translate_tex_target(tex->base.target);
255 format = translate_tex_format(tex->base.format);
256 assert(format != BRW_SURFACEFORMAT_INVALID);
257 tex->ss.ss0.surface_format = format;
259 /* This is ok for all textures with channel width 8bit or less:
261 /* tex->ss.ss0.data_return_format = BRW_SURFACERETURNFORMAT_S1; */
264 /* XXX: what happens when tex->bo->offset changes???
266 tex->ss.ss1.base_addr = 0; /* reloc */
267 tex->ss.ss2.mip_count = tex->base.last_level;
268 tex->ss.ss2.width = tex->base.width0 - 1;
269 tex->ss.ss2.height = tex->base.height0 - 1;
271 switch (tex->tiling) {
272 case BRW_TILING_NONE:
273 tex->ss.ss3.tiled_surface = 0;
274 tex->ss.ss3.tile_walk = 0;
277 tex->ss.ss3.tiled_surface = 1;
278 tex->ss.ss3.tile_walk = BRW_TILEWALK_XMAJOR;
281 tex->ss.ss3.tiled_surface = 1;
282 tex->ss.ss3.tile_walk = BRW_TILEWALK_YMAJOR;
286 tex->ss.ss3.pitch = (tex->pitch * tex->cpp) - 1;
287 tex->ss.ss3.depth = tex->base.depth0 - 1;
289 tex->ss.ss4.min_lod = 0;
291 if (tex->base.target == PIPE_TEXTURE_CUBE) {
292 tex->ss.ss0.cube_pos_x = 1;
293 tex->ss.ss0.cube_pos_y = 1;
294 tex->ss.ss0.cube_pos_z = 1;
295 tex->ss.ss0.cube_neg_x = 1;
296 tex->ss.ss0.cube_neg_y = 1;
297 tex->ss.ss0.cube_neg_z = 1;
303 bo_reference(&tex->bo, NULL);
308 static struct pipe_texture *
309 brw_texture_from_handle(struct pipe_screen *screen,
310 const struct pipe_texture *templ,
311 struct winsys_handle *whandle)
313 struct brw_screen *bscreen = brw_screen(screen);
314 struct brw_texture *tex;
315 struct brw_winsys_buffer *buffer;
320 if (templ->target != PIPE_TEXTURE_2D ||
321 templ->last_level != 0 ||
325 if (util_format_is_s3tc(templ->format))
328 tex = CALLOC_STRUCT(brw_texture);
332 if (bscreen->sws->bo_from_handle(bscreen->sws, whandle, &pitch, &tiling, &buffer) != PIPE_OK)
335 memcpy(&tex->base, templ, sizeof *templ);
336 pipe_reference_init(&tex->base.reference, 1);
337 tex->base.screen = screen;
339 /* XXX: cpp vs. blocksize
341 tex->cpp = util_format_get_blocksize(tex->base.format);
342 tex->tiling = tiling;
344 make_empty_list(&tex->views[0]);
345 make_empty_list(&tex->views[1]);
347 if (!brw_texture_layout(bscreen, tex))
350 /* XXX Maybe some more checks? */
351 if ((pitch / tex->cpp) < tex->pitch)
354 tex->pitch = pitch / tex->cpp;
358 /* fix this warning */
360 if (tex->size > buffer->size)
364 tex->ss.ss0.mipmap_layout_mode = BRW_SURFACE_MIPMAPLAYOUT_BELOW;
365 tex->ss.ss0.surface_type = translate_tex_target(tex->base.target);
367 format = translate_tex_format(tex->base.format);
368 assert(format != BRW_SURFACEFORMAT_INVALID);
369 tex->ss.ss0.surface_format = format;
371 /* This is ok for all textures with channel width 8bit or less:
373 /* tex->ss.ss0.data_return_format = BRW_SURFACERETURNFORMAT_S1; */
376 /* XXX: what happens when tex->bo->offset changes???
378 tex->ss.ss1.base_addr = 0; /* reloc */
379 tex->ss.ss2.mip_count = tex->base.last_level;
380 tex->ss.ss2.width = tex->base.width0 - 1;
381 tex->ss.ss2.height = tex->base.height0 - 1;
383 switch (tex->tiling) {
384 case BRW_TILING_NONE:
385 tex->ss.ss3.tiled_surface = 0;
386 tex->ss.ss3.tile_walk = 0;
389 tex->ss.ss3.tiled_surface = 1;
390 tex->ss.ss3.tile_walk = BRW_TILEWALK_XMAJOR;
393 tex->ss.ss3.tiled_surface = 1;
394 tex->ss.ss3.tile_walk = BRW_TILEWALK_YMAJOR;
398 tex->ss.ss3.pitch = (tex->pitch * tex->cpp) - 1;
399 tex->ss.ss3.depth = tex->base.depth0 - 1;
401 tex->ss.ss4.min_lod = 0;
411 brw_texture_get_handle(struct pipe_screen *screen,
412 struct pipe_texture *texture,
413 struct winsys_handle *whandle)
415 struct brw_screen *bscreen = brw_screen(screen);
416 struct brw_texture *tex = brw_texture(texture);
419 stride = tex->pitch * tex->cpp;
421 return bscreen->sws->bo_get_handle(tex->bo, whandle, stride);
426 static void brw_texture_destroy(struct pipe_texture *pt)
428 struct brw_texture *tex = brw_texture(pt);
429 bo_reference(&tex->bo, NULL);
434 static boolean brw_is_format_supported( struct pipe_screen *screen,
435 enum pipe_format format,
436 enum pipe_texture_target target,
438 unsigned geom_flags )
440 return translate_tex_format(format) != BRW_SURFACEFORMAT_INVALID;
444 boolean brw_is_texture_referenced_by_bo( struct brw_screen *brw_screen,
445 struct pipe_texture *texture,
448 struct brw_winsys_buffer *bo )
450 struct brw_texture *tex = brw_texture(texture);
451 struct brw_surface *surf;
454 /* XXX: this is subject to false positives if the underlying
455 * texture BO is referenced, we can't tell whether the sub-region
456 * we care about participates in that.
458 if (brw_screen->sws->bo_references( bo, tex->bo ))
461 /* Find any view on this texture for this face/level and see if it
464 for (i = 0; i < 2; i++) {
465 foreach (surf, &tex->views[i]) {
466 if (surf->bo == tex->bo)
469 if (surf->id.bits.face != face ||
470 surf->id.bits.level != level)
473 if (brw_screen->sws->bo_references( bo, surf->bo))
486 static struct pipe_transfer*
487 brw_get_tex_transfer(struct pipe_context *pipe,
488 struct pipe_texture *texture,
489 unsigned face, unsigned level, unsigned zslice,
490 enum pipe_transfer_usage usage, unsigned x, unsigned y,
491 unsigned w, unsigned h)
493 struct brw_texture *tex = brw_texture(texture);
494 struct brw_transfer *trans;
495 unsigned offset; /* in bytes */
497 if (texture->target == PIPE_TEXTURE_CUBE) {
498 offset = tex->image_offset[level][face];
499 } else if (texture->target == PIPE_TEXTURE_3D) {
500 offset = tex->image_offset[level][zslice];
502 offset = tex->image_offset[level][0];
507 trans = CALLOC_STRUCT(brw_transfer);
509 pipe_texture_reference(&trans->base.texture, texture);
512 trans->base.width = w;
513 trans->base.height = h;
514 trans->base.stride = tex->pitch * tex->cpp;
515 trans->offset = offset;
516 trans->base.usage = usage;
522 brw_transfer_map(struct pipe_context *pipe,
523 struct pipe_transfer *transfer)
525 struct brw_texture *tex = brw_texture(transfer->texture);
526 struct brw_winsys_screen *sws = brw_screen(pipe->screen)->sws;
528 unsigned usage = transfer->usage;
530 map = sws->bo_map(tex->bo,
534 (usage & PIPE_TRANSFER_WRITE) ? TRUE : FALSE,
535 (usage & 0) ? TRUE : FALSE,
536 (usage & 0) ? TRUE : FALSE);
541 /* XXX: blocksize and compressed textures
543 return map + brw_transfer(transfer)->offset +
544 transfer->y /* / transfer->block.height */ * transfer->stride +
545 transfer->x /* / transfer->block.width */ * brw_texture(transfer->texture)->cpp;
549 brw_transfer_unmap(struct pipe_context *pipe,
550 struct pipe_transfer *transfer)
552 struct brw_texture *tex = brw_texture(transfer->texture);
553 struct brw_winsys_screen *sws = brw_screen(pipe->screen)->sws;
555 sws->bo_unmap(tex->bo);
559 brw_tex_transfer_destroy(struct pipe_context *pipe,
560 struct pipe_transfer *trans)
562 pipe_texture_reference(&trans->texture, NULL);
567 void brw_tex_init( struct brw_context *brw )
569 brw->base.get_tex_transfer = brw_get_tex_transfer;
570 brw->base.transfer_map = brw_transfer_map;
571 brw->base.transfer_unmap = brw_transfer_unmap;
572 brw->base.tex_transfer_destroy = brw_tex_transfer_destroy;
575 void brw_screen_tex_init( struct brw_screen *brw_screen )
577 brw_screen->base.is_format_supported = brw_is_format_supported;
578 brw_screen->base.texture_create = brw_texture_create;
579 brw_screen->base.texture_from_handle = brw_texture_from_handle;
580 brw_screen->base.texture_get_handle = brw_texture_get_handle;
581 brw_screen->base.texture_destroy = brw_texture_destroy;