glsl2: Add and use new variable mode ir_var_temporary
[mesa/nouveau-pmpeg.git] / src / gallium / drivers / i965 / brw_resource_texture.c
blobffd0f38672c7e7b32121fea8796d1571c4b72262
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.
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 **********************************************************************/
28 * Authors:
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"
45 /**
46 * Subclass of pipe_transfer
48 struct brw_transfer
50 struct pipe_transfer base;
52 unsigned offset;
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 )
64 switch (target) {
65 case PIPE_TEXTURE_1D:
66 return BRW_SURFACE_1D;
68 case PIPE_TEXTURE_2D:
69 return BRW_SURFACE_2D;
71 case PIPE_TEXTURE_3D:
72 return BRW_SURFACE_3D;
74 case PIPE_TEXTURE_CUBE:
75 return BRW_SURFACE_CUBE;
77 default:
78 assert(0);
79 return BRW_SURFACE_1D;
84 static GLuint translate_tex_format( enum pipe_format pf )
86 switch( pf ) {
87 case PIPE_FORMAT_L8_UNORM:
88 return BRW_SURFACEFORMAT_L8_UNORM;
90 case PIPE_FORMAT_I8_UNORM:
91 return BRW_SURFACEFORMAT_I8_UNORM;
93 case PIPE_FORMAT_A8_UNORM:
94 return BRW_SURFACEFORMAT_A8_UNORM;
96 case PIPE_FORMAT_L16_UNORM:
97 return BRW_SURFACEFORMAT_L16_UNORM;
99 /* XXX: Add these to gallium
100 case PIPE_FORMAT_I16_UNORM:
101 return BRW_SURFACEFORMAT_I16_UNORM;
103 case PIPE_FORMAT_A16_UNORM:
104 return BRW_SURFACEFORMAT_A16_UNORM;
107 case PIPE_FORMAT_L8A8_UNORM:
108 return BRW_SURFACEFORMAT_L8A8_UNORM;
110 case PIPE_FORMAT_B5G6R5_UNORM:
111 return BRW_SURFACEFORMAT_B5G6R5_UNORM;
113 case PIPE_FORMAT_B5G5R5A1_UNORM:
114 return BRW_SURFACEFORMAT_B5G5R5A1_UNORM;
116 case PIPE_FORMAT_B4G4R4A4_UNORM:
117 return BRW_SURFACEFORMAT_B4G4R4A4_UNORM;
119 case PIPE_FORMAT_B8G8R8X8_UNORM:
120 return BRW_SURFACEFORMAT_R8G8B8X8_UNORM;
122 case PIPE_FORMAT_B8G8R8A8_UNORM:
123 return BRW_SURFACEFORMAT_B8G8R8A8_UNORM;
126 * Video formats
129 case PIPE_FORMAT_YUYV:
130 return BRW_SURFACEFORMAT_YCRCB_NORMAL;
132 case PIPE_FORMAT_UYVY:
133 return BRW_SURFACEFORMAT_YCRCB_SWAPUVY;
136 * Compressed formats.
138 /* XXX: Add FXT to gallium?
139 case PIPE_FORMAT_FXT1_RGBA:
140 return BRW_SURFACEFORMAT_FXT1;
143 case PIPE_FORMAT_DXT1_RGB:
144 return BRW_SURFACEFORMAT_DXT1_RGB;
146 case PIPE_FORMAT_DXT1_RGBA:
147 return BRW_SURFACEFORMAT_BC1_UNORM;
149 case PIPE_FORMAT_DXT3_RGBA:
150 return BRW_SURFACEFORMAT_BC2_UNORM;
152 case PIPE_FORMAT_DXT5_RGBA:
153 return BRW_SURFACEFORMAT_BC3_UNORM;
156 * sRGB formats
159 case PIPE_FORMAT_A8B8G8R8_SRGB:
160 return BRW_SURFACEFORMAT_B8G8R8A8_UNORM_SRGB;
162 case PIPE_FORMAT_L8A8_SRGB:
163 return BRW_SURFACEFORMAT_L8A8_UNORM_SRGB;
165 case PIPE_FORMAT_L8_SRGB:
166 return BRW_SURFACEFORMAT_L8_UNORM_SRGB;
168 case PIPE_FORMAT_DXT1_SRGB:
169 return BRW_SURFACEFORMAT_BC1_UNORM_SRGB;
172 * Depth formats
175 case PIPE_FORMAT_Z16_UNORM:
176 return BRW_SURFACEFORMAT_I16_UNORM;
178 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
179 case PIPE_FORMAT_Z24X8_UNORM:
180 return BRW_SURFACEFORMAT_I24X8_UNORM;
182 case PIPE_FORMAT_Z32_FLOAT:
183 return BRW_SURFACEFORMAT_I32_FLOAT;
185 /* XXX: presumably for bump mapping. Add this to mesa state
186 * tracker?
188 * XXX: Add flipped versions of these formats to Gallium.
190 case PIPE_FORMAT_R8G8_SNORM:
191 return BRW_SURFACEFORMAT_R8G8_SNORM;
193 case PIPE_FORMAT_R8G8B8A8_SNORM:
194 return BRW_SURFACEFORMAT_R8G8B8A8_SNORM;
196 default:
197 return BRW_SURFACEFORMAT_INVALID;
202 static boolean
203 brw_texture_get_handle(struct pipe_screen *screen,
204 struct pipe_resource *texture,
205 struct winsys_handle *whandle)
207 struct brw_screen *bscreen = brw_screen(screen);
208 struct brw_texture *tex = brw_texture(texture);
209 unsigned stride;
211 stride = tex->pitch * tex->cpp;
213 return bscreen->sws->bo_get_handle(tex->bo, whandle, stride) == PIPE_OK;
218 static void brw_texture_destroy(struct pipe_screen *screen,
219 struct pipe_resource *pt)
221 struct brw_texture *tex = brw_texture(pt);
222 bo_reference(&tex->bo, NULL);
223 FREE(pt);
229 static unsigned brw_texture_is_referenced( struct pipe_context *pipe,
230 struct pipe_resource *texture,
231 unsigned face,
232 unsigned level )
234 struct brw_context *brw = brw_context(pipe);
235 struct brw_screen *bscreen = brw_screen(pipe->screen);
236 struct brw_winsys_buffer *batch_bo = brw->batch->buf;
237 struct brw_texture *tex = brw_texture(texture);
238 struct brw_surface *surf;
239 int i;
241 /* XXX: this is subject to false positives if the underlying
242 * texture BO is referenced, we can't tell whether the sub-region
243 * we care about participates in that.
245 if (bscreen->sws->bo_references( batch_bo, tex->bo ))
246 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
248 /* Find any view on this texture for this face/level and see if it
249 * is referenced:
251 for (i = 0; i < 2; i++) {
252 foreach (surf, &tex->views[i]) {
253 if (surf->bo == tex->bo)
254 continue;
256 if (surf->id.bits.face != face ||
257 surf->id.bits.level != level)
258 continue;
260 if (bscreen->sws->bo_references( batch_bo, surf->bo))
261 return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
265 return PIPE_UNREFERENCED;
270 * Transfer functions
274 static struct pipe_transfer *
275 brw_texture_get_transfer(struct pipe_context *context,
276 struct pipe_resource *resource,
277 struct pipe_subresource sr,
278 unsigned usage,
279 const struct pipe_box *box)
281 struct brw_texture *tex = brw_texture(resource);
282 struct pipe_transfer *transfer = CALLOC_STRUCT(pipe_transfer);
283 if (transfer == NULL)
284 return NULL;
286 transfer->resource = resource;
287 transfer->sr = sr;
288 transfer->usage = usage;
289 transfer->box = *box;
290 transfer->stride = tex->pitch * tex->cpp;
292 return transfer;
296 static void *
297 brw_texture_transfer_map(struct pipe_context *pipe,
298 struct pipe_transfer *transfer)
300 struct pipe_resource *resource = transfer->resource;
301 struct brw_texture *tex = brw_texture(transfer->resource);
302 struct brw_winsys_screen *sws = brw_screen(pipe->screen)->sws;
303 struct pipe_subresource sr = transfer->sr;
304 struct pipe_box *box = &transfer->box;
305 enum pipe_format format = resource->format;
306 unsigned usage = transfer->usage;
307 unsigned offset;
308 char *map;
310 if (resource->target == PIPE_TEXTURE_CUBE) {
311 offset = tex->image_offset[sr.level][sr.face];
313 else if (resource->target == PIPE_TEXTURE_3D) {
314 offset = tex->image_offset[sr.level][box->z];
316 else {
317 offset = tex->image_offset[sr.level][0];
318 assert(sr.face == 0);
319 assert(box->z == 0);
322 map = sws->bo_map(tex->bo,
323 BRW_DATA_OTHER,
325 tex->bo->size,
326 (usage & PIPE_TRANSFER_WRITE) ? TRUE : FALSE,
327 (usage & 0) ? TRUE : FALSE,
328 (usage & 0) ? TRUE : FALSE);
330 if (!map)
331 return NULL;
333 return map + offset +
334 box->y / util_format_get_blockheight(format) * transfer->stride +
335 box->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
338 static void
339 brw_texture_transfer_unmap(struct pipe_context *pipe,
340 struct pipe_transfer *transfer)
342 struct brw_texture *tex = brw_texture(transfer->resource);
343 struct brw_winsys_screen *sws = brw_screen(pipe->screen)->sws;
345 sws->bo_unmap(tex->bo);
352 struct u_resource_vtbl brw_texture_vtbl =
354 brw_texture_get_handle, /* get_handle */
355 brw_texture_destroy, /* resource_destroy */
356 brw_texture_is_referenced, /* is_resource_referenced */
357 brw_texture_get_transfer, /* get_transfer */
358 u_default_transfer_destroy, /* transfer_destroy */
359 brw_texture_transfer_map, /* transfer_map */
360 u_default_transfer_flush_region, /* transfer_flush_region */
361 brw_texture_transfer_unmap, /* transfer_unmap */
362 u_default_transfer_inline_write /* transfer_inline_write */
369 struct pipe_resource *
370 brw_texture_create( struct pipe_screen *screen,
371 const struct pipe_resource *template )
373 struct brw_screen *bscreen = brw_screen(screen);
374 struct brw_texture *tex;
375 enum brw_buffer_type buffer_type;
376 enum pipe_error ret;
377 GLuint format;
379 tex = CALLOC_STRUCT(brw_texture);
380 if (tex == NULL)
381 return NULL;
383 tex->b.b = *template;
384 tex->b.vtbl = &brw_texture_vtbl;
385 pipe_reference_init(&tex->b.b.reference, 1);
386 tex->b.b.screen = screen;
388 /* XXX: compressed textures need special treatment here
390 tex->cpp = util_format_get_blocksize(tex->b.b.format);
391 tex->compressed = util_format_is_s3tc(tex->b.b.format);
393 make_empty_list(&tex->views[0]);
394 make_empty_list(&tex->views[1]);
396 /* XXX: No tiling with compressed textures??
398 if (tex->compressed == 0 &&
399 !bscreen->no_tiling)
401 if (bscreen->chipset.is_965 &&
402 util_format_is_depth_or_stencil(template->format))
403 tex->tiling = BRW_TILING_Y;
404 else
405 tex->tiling = BRW_TILING_X;
407 else {
408 tex->tiling = BRW_TILING_NONE;
412 if (!brw_texture_layout( bscreen, tex ))
413 goto fail;
416 if (template->bind & (PIPE_BIND_SCANOUT |
417 PIPE_BIND_SHARED)) {
418 buffer_type = BRW_BUFFER_TYPE_SCANOUT;
420 else {
421 buffer_type = BRW_BUFFER_TYPE_TEXTURE;
424 ret = bscreen->sws->bo_alloc( bscreen->sws,
425 buffer_type,
426 tex->pitch * tex->total_height * tex->cpp,
428 &tex->bo );
429 if (ret)
430 goto fail;
432 tex->ss.ss0.mipmap_layout_mode = BRW_SURFACE_MIPMAPLAYOUT_BELOW;
433 tex->ss.ss0.surface_type = translate_tex_target(tex->b.b.target);
435 format = translate_tex_format(tex->b.b.format);
436 assert(format != BRW_SURFACEFORMAT_INVALID);
437 tex->ss.ss0.surface_format = format;
439 /* This is ok for all textures with channel width 8bit or less:
441 /* tex->ss.ss0.data_return_format = BRW_SURFACERETURNFORMAT_S1; */
444 /* XXX: what happens when tex->bo->offset changes???
446 tex->ss.ss1.base_addr = 0; /* reloc */
447 tex->ss.ss2.mip_count = tex->b.b.last_level;
448 tex->ss.ss2.width = tex->b.b.width0 - 1;
449 tex->ss.ss2.height = tex->b.b.height0 - 1;
451 switch (tex->tiling) {
452 case BRW_TILING_NONE:
453 tex->ss.ss3.tiled_surface = 0;
454 tex->ss.ss3.tile_walk = 0;
455 break;
456 case BRW_TILING_X:
457 tex->ss.ss3.tiled_surface = 1;
458 tex->ss.ss3.tile_walk = BRW_TILEWALK_XMAJOR;
459 break;
460 case BRW_TILING_Y:
461 tex->ss.ss3.tiled_surface = 1;
462 tex->ss.ss3.tile_walk = BRW_TILEWALK_YMAJOR;
463 break;
466 tex->ss.ss3.pitch = (tex->pitch * tex->cpp) - 1;
467 tex->ss.ss3.depth = tex->b.b.depth0 - 1;
469 tex->ss.ss4.min_lod = 0;
471 if (tex->b.b.target == PIPE_TEXTURE_CUBE) {
472 tex->ss.ss0.cube_pos_x = 1;
473 tex->ss.ss0.cube_pos_y = 1;
474 tex->ss.ss0.cube_pos_z = 1;
475 tex->ss.ss0.cube_neg_x = 1;
476 tex->ss.ss0.cube_neg_y = 1;
477 tex->ss.ss0.cube_neg_z = 1;
480 return &tex->b.b;
482 fail:
483 bo_reference(&tex->bo, NULL);
484 FREE(tex);
485 return NULL;
489 struct pipe_resource *
490 brw_texture_from_handle(struct pipe_screen *screen,
491 const struct pipe_resource *template,
492 struct winsys_handle *whandle)
494 struct brw_screen *bscreen = brw_screen(screen);
495 struct brw_texture *tex;
496 struct brw_winsys_buffer *buffer;
497 unsigned tiling;
498 unsigned pitch;
499 GLuint format;
501 if (template->target != PIPE_TEXTURE_2D ||
502 template->last_level != 0 ||
503 template->depth0 != 1)
504 return NULL;
506 if (util_format_is_s3tc(template->format))
507 return NULL;
509 tex = CALLOC_STRUCT(brw_texture);
510 if (!tex)
511 return NULL;
513 if (bscreen->sws->bo_from_handle(bscreen->sws, whandle, &pitch, &tiling, &buffer) != PIPE_OK)
514 goto fail;
516 tex->b.b = *template;
517 tex->b.vtbl = &brw_texture_vtbl;
518 pipe_reference_init(&tex->b.b.reference, 1);
519 tex->b.b.screen = screen;
521 /* XXX: cpp vs. blocksize
523 tex->cpp = util_format_get_blocksize(tex->b.b.format);
524 tex->tiling = tiling;
526 make_empty_list(&tex->views[0]);
527 make_empty_list(&tex->views[1]);
529 if (!brw_texture_layout(bscreen, tex))
530 goto fail;
532 /* XXX Maybe some more checks? */
533 if ((pitch / tex->cpp) < tex->pitch)
534 goto fail;
536 tex->pitch = pitch / tex->cpp;
538 tex->bo = buffer;
540 /* fix this warning */
541 #if 0
542 if (tex->size > buffer->size)
543 goto fail;
544 #endif
546 tex->ss.ss0.mipmap_layout_mode = BRW_SURFACE_MIPMAPLAYOUT_BELOW;
547 tex->ss.ss0.surface_type = translate_tex_target(tex->b.b.target);
549 format = translate_tex_format(tex->b.b.format);
550 assert(format != BRW_SURFACEFORMAT_INVALID);
551 tex->ss.ss0.surface_format = format;
553 /* This is ok for all textures with channel width 8bit or less:
555 /* tex->ss.ss0.data_return_format = BRW_SURFACERETURNFORMAT_S1; */
558 /* XXX: what happens when tex->bo->offset changes???
560 tex->ss.ss1.base_addr = 0; /* reloc */
561 tex->ss.ss2.mip_count = tex->b.b.last_level;
562 tex->ss.ss2.width = tex->b.b.width0 - 1;
563 tex->ss.ss2.height = tex->b.b.height0 - 1;
565 switch (tex->tiling) {
566 case BRW_TILING_NONE:
567 tex->ss.ss3.tiled_surface = 0;
568 tex->ss.ss3.tile_walk = 0;
569 break;
570 case BRW_TILING_X:
571 tex->ss.ss3.tiled_surface = 1;
572 tex->ss.ss3.tile_walk = BRW_TILEWALK_XMAJOR;
573 break;
574 case BRW_TILING_Y:
575 tex->ss.ss3.tiled_surface = 1;
576 tex->ss.ss3.tile_walk = BRW_TILEWALK_YMAJOR;
577 break;
580 tex->ss.ss3.pitch = (tex->pitch * tex->cpp) - 1;
581 tex->ss.ss3.depth = tex->b.b.depth0 - 1;
583 tex->ss.ss4.min_lod = 0;
585 return &tex->b.b;
587 fail:
588 FREE(tex);
589 return NULL;
593 #if 0
594 boolean brw_is_format_supported( struct pipe_screen *screen,
595 enum pipe_format format,
596 enum pipe_texture_target target,
597 unsigned sample_count,
598 unsigned tex_usage,
599 unsigned geom_flags )
601 return translate_tex_format(format) != BRW_SURFACEFORMAT_INVALID;
603 #endif