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_batchbuffer.h"
41 #include "brw_context.h"
42 #include "brw_resource.h"
46 * Subclass of pipe_transfer
50 struct pipe_transfer base
;
55 static INLINE
struct brw_transfer
*
56 brw_transfer(struct pipe_transfer
*transfer
)
58 return (struct brw_transfer
*)transfer
;
62 static GLuint
translate_tex_target( unsigned target
)
66 return BRW_SURFACE_1D
;
69 case PIPE_TEXTURE_RECT
:
70 return BRW_SURFACE_2D
;
73 return BRW_SURFACE_3D
;
75 case PIPE_TEXTURE_CUBE
:
76 return BRW_SURFACE_CUBE
;
80 return BRW_SURFACE_1D
;
85 static GLuint
translate_tex_format( enum pipe_format pf
)
88 case PIPE_FORMAT_L8_UNORM
:
89 return BRW_SURFACEFORMAT_L8_UNORM
;
91 case PIPE_FORMAT_I8_UNORM
:
92 return BRW_SURFACEFORMAT_I8_UNORM
;
94 case PIPE_FORMAT_A8_UNORM
:
95 return BRW_SURFACEFORMAT_A8_UNORM
;
97 case PIPE_FORMAT_L16_UNORM
:
98 return BRW_SURFACEFORMAT_L16_UNORM
;
100 /* XXX: Add these to gallium
101 case PIPE_FORMAT_I16_UNORM:
102 return BRW_SURFACEFORMAT_I16_UNORM;
104 case PIPE_FORMAT_A16_UNORM:
105 return BRW_SURFACEFORMAT_A16_UNORM;
108 case PIPE_FORMAT_L8A8_UNORM
:
109 return BRW_SURFACEFORMAT_L8A8_UNORM
;
111 case PIPE_FORMAT_B5G6R5_UNORM
:
112 return BRW_SURFACEFORMAT_B5G6R5_UNORM
;
114 case PIPE_FORMAT_B5G5R5A1_UNORM
:
115 return BRW_SURFACEFORMAT_B5G5R5A1_UNORM
;
117 case PIPE_FORMAT_B4G4R4A4_UNORM
:
118 return BRW_SURFACEFORMAT_B4G4R4A4_UNORM
;
120 case PIPE_FORMAT_B8G8R8X8_UNORM
:
121 return BRW_SURFACEFORMAT_R8G8B8X8_UNORM
;
123 case PIPE_FORMAT_B8G8R8A8_UNORM
:
124 return BRW_SURFACEFORMAT_B8G8R8A8_UNORM
;
130 case PIPE_FORMAT_YUYV
:
131 return BRW_SURFACEFORMAT_YCRCB_NORMAL
;
133 case PIPE_FORMAT_UYVY
:
134 return BRW_SURFACEFORMAT_YCRCB_SWAPUVY
;
137 * Compressed formats.
139 /* XXX: Add FXT to gallium?
140 case PIPE_FORMAT_FXT1_RGBA:
141 return BRW_SURFACEFORMAT_FXT1;
144 case PIPE_FORMAT_DXT1_RGB
:
145 return BRW_SURFACEFORMAT_DXT1_RGB
;
147 case PIPE_FORMAT_DXT1_RGBA
:
148 return BRW_SURFACEFORMAT_BC1_UNORM
;
150 case PIPE_FORMAT_DXT3_RGBA
:
151 return BRW_SURFACEFORMAT_BC2_UNORM
;
153 case PIPE_FORMAT_DXT5_RGBA
:
154 return BRW_SURFACEFORMAT_BC3_UNORM
;
160 case PIPE_FORMAT_A8B8G8R8_SRGB
:
161 return BRW_SURFACEFORMAT_B8G8R8A8_UNORM_SRGB
;
163 case PIPE_FORMAT_L8A8_SRGB
:
164 return BRW_SURFACEFORMAT_L8A8_UNORM_SRGB
;
166 case PIPE_FORMAT_L8_SRGB
:
167 return BRW_SURFACEFORMAT_L8_UNORM_SRGB
;
169 case PIPE_FORMAT_DXT1_SRGB
:
170 return BRW_SURFACEFORMAT_BC1_UNORM_SRGB
;
176 case PIPE_FORMAT_Z16_UNORM
:
177 return BRW_SURFACEFORMAT_I16_UNORM
;
179 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
180 case PIPE_FORMAT_Z24X8_UNORM
:
181 return BRW_SURFACEFORMAT_I24X8_UNORM
;
183 case PIPE_FORMAT_Z32_FLOAT
:
184 return BRW_SURFACEFORMAT_I32_FLOAT
;
186 /* XXX: presumably for bump mapping. Add this to mesa state
189 * XXX: Add flipped versions of these formats to Gallium.
191 case PIPE_FORMAT_R8G8_SNORM
:
192 return BRW_SURFACEFORMAT_R8G8_SNORM
;
194 case PIPE_FORMAT_R8G8B8A8_SNORM
:
195 return BRW_SURFACEFORMAT_R8G8B8A8_SNORM
;
198 return BRW_SURFACEFORMAT_INVALID
;
204 brw_texture_get_handle(struct pipe_screen
*screen
,
205 struct pipe_resource
*texture
,
206 struct winsys_handle
*whandle
)
208 struct brw_screen
*bscreen
= brw_screen(screen
);
209 struct brw_texture
*tex
= brw_texture(texture
);
212 stride
= tex
->pitch
* tex
->cpp
;
214 return bscreen
->sws
->bo_get_handle(tex
->bo
, whandle
, stride
) == PIPE_OK
;
219 static void brw_texture_destroy(struct pipe_screen
*screen
,
220 struct pipe_resource
*pt
)
222 struct brw_texture
*tex
= brw_texture(pt
);
223 bo_reference(&tex
->bo
, NULL
);
230 static unsigned brw_texture_is_referenced( struct pipe_context
*pipe
,
231 struct pipe_resource
*texture
,
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
;
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.
246 if (bscreen
->sws
->bo_references( batch_bo
, tex
->bo
))
247 return PIPE_REFERENCED_FOR_READ
| PIPE_REFERENCED_FOR_WRITE
;
249 /* Find any view on this texture for this face/level and see if it
252 for (i
= 0; i
< 2; i
++) {
253 foreach (surf
, &tex
->views
[i
]) {
254 if (surf
->bo
== tex
->bo
)
257 if (surf
->id
.bits
.face
!= face
||
258 surf
->id
.bits
.level
!= level
)
261 if (bscreen
->sws
->bo_references( batch_bo
, surf
->bo
))
262 return PIPE_REFERENCED_FOR_READ
| PIPE_REFERENCED_FOR_WRITE
;
266 return PIPE_UNREFERENCED
;
275 static struct pipe_transfer
*
276 brw_texture_get_transfer(struct pipe_context
*context
,
277 struct pipe_resource
*resource
,
278 struct pipe_subresource sr
,
280 const struct pipe_box
*box
)
282 struct brw_texture
*tex
= brw_texture(resource
);
283 struct pipe_transfer
*transfer
= CALLOC_STRUCT(pipe_transfer
);
284 if (transfer
== NULL
)
287 transfer
->resource
= resource
;
289 transfer
->usage
= usage
;
290 transfer
->box
= *box
;
291 transfer
->stride
= tex
->pitch
* tex
->cpp
;
298 brw_texture_transfer_map(struct pipe_context
*pipe
,
299 struct pipe_transfer
*transfer
)
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
;
311 if (resource
->target
== PIPE_TEXTURE_CUBE
) {
312 offset
= tex
->image_offset
[sr
.level
][sr
.face
];
314 else if (resource
->target
== PIPE_TEXTURE_3D
) {
315 offset
= tex
->image_offset
[sr
.level
][box
->z
];
318 offset
= tex
->image_offset
[sr
.level
][0];
319 assert(sr
.face
== 0);
323 map
= sws
->bo_map(tex
->bo
,
327 (usage
& PIPE_TRANSFER_WRITE
) ? TRUE
: FALSE
,
328 (usage
& 0) ? TRUE
: FALSE
,
329 (usage
& 0) ? TRUE
: FALSE
);
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
);
340 brw_texture_transfer_unmap(struct pipe_context
*pipe
,
341 struct pipe_transfer
*transfer
)
343 struct brw_texture
*tex
= brw_texture(transfer
->resource
);
344 struct brw_winsys_screen
*sws
= brw_screen(pipe
->screen
)->sws
;
346 sws
->bo_unmap(tex
->bo
);
353 struct u_resource_vtbl brw_texture_vtbl
=
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 */
370 struct pipe_resource
*
371 brw_texture_create( struct pipe_screen
*screen
,
372 const struct pipe_resource
*template )
374 struct brw_screen
*bscreen
= brw_screen(screen
);
375 struct brw_texture
*tex
;
376 enum brw_buffer_type buffer_type
;
380 tex
= CALLOC_STRUCT(brw_texture
);
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
;
389 /* XXX: compressed textures need special treatment here
391 tex
->cpp
= util_format_get_blocksize(tex
->b
.b
.format
);
392 tex
->compressed
= util_format_is_s3tc(tex
->b
.b
.format
);
394 make_empty_list(&tex
->views
[0]);
395 make_empty_list(&tex
->views
[1]);
397 /* XXX: No tiling with compressed textures??
399 if (tex
->compressed
== 0 &&
402 if (bscreen
->chipset
.is_965
&&
403 util_format_is_depth_or_stencil(template->format
))
404 tex
->tiling
= BRW_TILING_Y
;
406 tex
->tiling
= BRW_TILING_X
;
409 tex
->tiling
= BRW_TILING_NONE
;
413 if (!brw_texture_layout( bscreen
, tex
))
417 if (template->bind
& (PIPE_BIND_SCANOUT
|
419 buffer_type
= BRW_BUFFER_TYPE_SCANOUT
;
422 buffer_type
= BRW_BUFFER_TYPE_TEXTURE
;
425 ret
= bscreen
->sws
->bo_alloc( bscreen
->sws
,
427 tex
->pitch
* tex
->total_height
* tex
->cpp
,
433 tex
->ss
.ss0
.mipmap_layout_mode
= BRW_SURFACE_MIPMAPLAYOUT_BELOW
;
434 tex
->ss
.ss0
.surface_type
= translate_tex_target(tex
->b
.b
.target
);
436 format
= translate_tex_format(tex
->b
.b
.format
);
437 assert(format
!= BRW_SURFACEFORMAT_INVALID
);
438 tex
->ss
.ss0
.surface_format
= format
;
440 /* This is ok for all textures with channel width 8bit or less:
442 /* tex->ss.ss0.data_return_format = BRW_SURFACERETURNFORMAT_S1; */
445 /* XXX: what happens when tex->bo->offset changes???
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;
452 switch (tex
->tiling
) {
453 case BRW_TILING_NONE
:
454 tex
->ss
.ss3
.tiled_surface
= 0;
455 tex
->ss
.ss3
.tile_walk
= 0;
458 tex
->ss
.ss3
.tiled_surface
= 1;
459 tex
->ss
.ss3
.tile_walk
= BRW_TILEWALK_XMAJOR
;
462 tex
->ss
.ss3
.tiled_surface
= 1;
463 tex
->ss
.ss3
.tile_walk
= BRW_TILEWALK_YMAJOR
;
467 tex
->ss
.ss3
.pitch
= (tex
->pitch
* tex
->cpp
) - 1;
468 tex
->ss
.ss3
.depth
= tex
->b
.b
.depth0
- 1;
470 tex
->ss
.ss4
.min_lod
= 0;
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;
484 bo_reference(&tex
->bo
, NULL
);
490 struct pipe_resource
*
491 brw_texture_from_handle(struct pipe_screen
*screen
,
492 const struct pipe_resource
*template,
493 struct winsys_handle
*whandle
)
495 struct brw_screen
*bscreen
= brw_screen(screen
);
496 struct brw_texture
*tex
;
497 struct brw_winsys_buffer
*buffer
;
502 if ((template->target
!= PIPE_TEXTURE_2D
503 && template->target
!= PIPE_TEXTURE_RECT
) ||
504 template->last_level
!= 0 ||
505 template->depth0
!= 1)
508 if (util_format_is_s3tc(template->format
))
511 tex
= CALLOC_STRUCT(brw_texture
);
515 if (bscreen
->sws
->bo_from_handle(bscreen
->sws
, whandle
, &pitch
, &tiling
, &buffer
) != PIPE_OK
)
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
;
523 /* XXX: cpp vs. blocksize
525 tex
->cpp
= util_format_get_blocksize(tex
->b
.b
.format
);
526 tex
->tiling
= tiling
;
528 make_empty_list(&tex
->views
[0]);
529 make_empty_list(&tex
->views
[1]);
531 if (!brw_texture_layout(bscreen
, tex
))
534 /* XXX Maybe some more checks? */
535 if ((pitch
/ tex
->cpp
) < tex
->pitch
)
538 tex
->pitch
= pitch
/ tex
->cpp
;
542 /* fix this warning */
544 if (tex
->size
> buffer
->size
)
548 tex
->ss
.ss0
.mipmap_layout_mode
= BRW_SURFACE_MIPMAPLAYOUT_BELOW
;
549 tex
->ss
.ss0
.surface_type
= translate_tex_target(tex
->b
.b
.target
);
551 format
= translate_tex_format(tex
->b
.b
.format
);
552 assert(format
!= BRW_SURFACEFORMAT_INVALID
);
553 tex
->ss
.ss0
.surface_format
= format
;
555 /* This is ok for all textures with channel width 8bit or less:
557 /* tex->ss.ss0.data_return_format = BRW_SURFACERETURNFORMAT_S1; */
560 /* XXX: what happens when tex->bo->offset changes???
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;
567 switch (tex
->tiling
) {
568 case BRW_TILING_NONE
:
569 tex
->ss
.ss3
.tiled_surface
= 0;
570 tex
->ss
.ss3
.tile_walk
= 0;
573 tex
->ss
.ss3
.tiled_surface
= 1;
574 tex
->ss
.ss3
.tile_walk
= BRW_TILEWALK_XMAJOR
;
577 tex
->ss
.ss3
.tiled_surface
= 1;
578 tex
->ss
.ss3
.tile_walk
= BRW_TILEWALK_YMAJOR
;
582 tex
->ss
.ss3
.pitch
= (tex
->pitch
* tex
->cpp
) - 1;
583 tex
->ss
.ss3
.depth
= tex
->b
.b
.depth0
- 1;
585 tex
->ss
.ss4
.min_lod
= 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
,
601 unsigned geom_flags
)
603 return translate_tex_format(format
) != BRW_SURFACEFORMAT_INVALID
;