r300g: align the height of NPOT textures to POT
[profile/ivi/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       return BRW_SURFACE_2D;
70
71    case PIPE_TEXTURE_3D: 
72       return BRW_SURFACE_3D;
73
74    case PIPE_TEXTURE_CUBE:
75       return BRW_SURFACE_CUBE;
76
77    default: 
78       assert(0); 
79       return BRW_SURFACE_1D;
80    }
81 }
82
83
84 static GLuint translate_tex_format( enum pipe_format pf )
85 {
86    switch( pf ) {
87    case PIPE_FORMAT_L8_UNORM:
88       return BRW_SURFACEFORMAT_L8_UNORM;
89
90    case PIPE_FORMAT_I8_UNORM:
91       return BRW_SURFACEFORMAT_I8_UNORM;
92
93    case PIPE_FORMAT_A8_UNORM:
94       return BRW_SURFACEFORMAT_A8_UNORM; 
95
96    case PIPE_FORMAT_L16_UNORM:
97       return BRW_SURFACEFORMAT_L16_UNORM;
98
99       /* XXX: Add these to gallium
100    case PIPE_FORMAT_I16_UNORM:
101       return BRW_SURFACEFORMAT_I16_UNORM;
102
103    case PIPE_FORMAT_A16_UNORM:
104       return BRW_SURFACEFORMAT_A16_UNORM; 
105       */
106
107    case PIPE_FORMAT_L8A8_UNORM:
108       return BRW_SURFACEFORMAT_L8A8_UNORM;
109
110    case PIPE_FORMAT_B5G6R5_UNORM:
111       return BRW_SURFACEFORMAT_B5G6R5_UNORM;
112
113    case PIPE_FORMAT_B5G5R5A1_UNORM:
114       return BRW_SURFACEFORMAT_B5G5R5A1_UNORM;
115
116    case PIPE_FORMAT_B4G4R4A4_UNORM:
117       return BRW_SURFACEFORMAT_B4G4R4A4_UNORM;
118
119    case PIPE_FORMAT_B8G8R8X8_UNORM:
120       return BRW_SURFACEFORMAT_R8G8B8X8_UNORM;
121
122    case PIPE_FORMAT_B8G8R8A8_UNORM:
123       return BRW_SURFACEFORMAT_B8G8R8A8_UNORM;
124
125    /*
126     * Video formats
127     */
128
129    case PIPE_FORMAT_YUYV:
130       return BRW_SURFACEFORMAT_YCRCB_NORMAL;
131
132    case PIPE_FORMAT_UYVY:
133       return BRW_SURFACEFORMAT_YCRCB_SWAPUVY;
134
135    /*
136     * Compressed formats.
137     */
138       /* XXX: Add FXT to gallium?
139    case PIPE_FORMAT_FXT1_RGBA:
140       return BRW_SURFACEFORMAT_FXT1;
141       */
142
143    case PIPE_FORMAT_DXT1_RGB:
144        return BRW_SURFACEFORMAT_DXT1_RGB;
145
146    case PIPE_FORMAT_DXT1_RGBA:
147        return BRW_SURFACEFORMAT_BC1_UNORM;
148        
149    case PIPE_FORMAT_DXT3_RGBA:
150        return BRW_SURFACEFORMAT_BC2_UNORM;
151        
152    case PIPE_FORMAT_DXT5_RGBA:
153        return BRW_SURFACEFORMAT_BC3_UNORM;
154
155    /*
156     * sRGB formats
157     */
158
159    case PIPE_FORMAT_A8B8G8R8_SRGB:
160       return BRW_SURFACEFORMAT_B8G8R8A8_UNORM_SRGB;
161
162    case PIPE_FORMAT_L8A8_SRGB:
163       return BRW_SURFACEFORMAT_L8A8_UNORM_SRGB;
164
165    case PIPE_FORMAT_L8_SRGB:
166       return BRW_SURFACEFORMAT_L8_UNORM_SRGB;
167
168    case PIPE_FORMAT_DXT1_SRGB:
169       return BRW_SURFACEFORMAT_BC1_UNORM_SRGB;
170
171    /*
172     * Depth formats
173     */
174
175    case PIPE_FORMAT_Z16_UNORM:
176          return BRW_SURFACEFORMAT_I16_UNORM;
177
178    case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
179    case PIPE_FORMAT_Z24X8_UNORM:
180          return BRW_SURFACEFORMAT_I24X8_UNORM;
181
182    case PIPE_FORMAT_Z32_FLOAT:
183          return BRW_SURFACEFORMAT_I32_FLOAT;
184
185       /* XXX: presumably for bump mapping.  Add this to mesa state
186        * tracker?
187        *
188        * XXX: Add flipped versions of these formats to Gallium.
189        */
190    case PIPE_FORMAT_R8G8_SNORM:
191       return BRW_SURFACEFORMAT_R8G8_SNORM;
192
193    case PIPE_FORMAT_R8G8B8A8_SNORM:
194       return BRW_SURFACEFORMAT_R8G8B8A8_SNORM;
195
196    default:
197       return BRW_SURFACEFORMAT_INVALID;
198    }
199 }
200
201
202 static boolean
203 brw_texture_get_handle(struct pipe_screen *screen,
204                        struct pipe_resource *texture,
205                        struct winsys_handle *whandle)
206 {
207    struct brw_screen *bscreen = brw_screen(screen);
208    struct brw_texture *tex = brw_texture(texture);
209    unsigned stride;
210
211    stride = tex->pitch * tex->cpp;
212
213    return bscreen->sws->bo_get_handle(tex->bo, whandle, stride);
214 }
215
216
217
218 static void brw_texture_destroy(struct pipe_screen *screen,
219                                 struct pipe_resource *pt)
220 {
221    struct brw_texture *tex = brw_texture(pt);
222    bo_reference(&tex->bo, NULL);
223    FREE(pt);
224 }
225
226
227
228
229 static unsigned brw_texture_is_referenced( struct pipe_context *pipe,
230                                            struct pipe_resource *texture,
231                                            unsigned face, 
232                                            unsigned level )
233 {
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;
240
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.
244     */
245    if (bscreen->sws->bo_references( batch_bo, tex->bo ))
246       return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
247
248    /* Find any view on this texture for this face/level and see if it
249     * is referenced:
250     */
251    for (i = 0; i < 2; i++) {
252       foreach (surf, &tex->views[i]) {
253          if (surf->bo == tex->bo)
254             continue;
255
256          if (surf->id.bits.face != face ||
257              surf->id.bits.level != level)
258             continue;
259          
260          if (bscreen->sws->bo_references( batch_bo, surf->bo))
261             return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
262       }
263    }
264
265    return PIPE_UNREFERENCED;
266 }
267
268
269 /*
270  * Transfer functions
271  */
272
273
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)
280 {
281    struct brw_texture *tex = brw_texture(resource);
282    struct pipe_transfer *transfer = CALLOC_STRUCT(pipe_transfer);
283    if (transfer == NULL)
284       return NULL;
285
286    transfer->resource = resource;
287    transfer->sr = sr;
288    transfer->usage = usage;
289    transfer->box = *box;
290    transfer->stride = tex->pitch * tex->cpp;
291
292    return transfer;
293 }
294
295
296 static void *
297 brw_texture_transfer_map(struct pipe_context *pipe,
298                  struct pipe_transfer *transfer)
299 {
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;
309
310    if (resource->target == PIPE_TEXTURE_CUBE) {
311       offset = tex->image_offset[sr.level][sr.face];
312    }
313    else if (resource->target == PIPE_TEXTURE_3D) {
314       offset = tex->image_offset[sr.level][box->z];
315    }
316    else {
317       offset = tex->image_offset[sr.level][0];
318       assert(sr.face == 0);
319       assert(box->z == 0);
320    }
321
322    map = sws->bo_map(tex->bo, 
323                      BRW_DATA_OTHER,
324                      0,
325                      tex->bo->size,
326                      (usage & PIPE_TRANSFER_WRITE) ? TRUE : FALSE,
327                      (usage & 0) ? TRUE : FALSE,
328                      (usage & 0) ? TRUE : FALSE);
329
330    if (!map)
331       return NULL;
332
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);
336 }
337
338 static void
339 brw_texture_transfer_unmap(struct pipe_context *pipe,
340                    struct pipe_transfer *transfer)
341 {
342    struct brw_texture *tex = brw_texture(transfer->resource);
343    struct brw_winsys_screen *sws = brw_screen(pipe->screen)->sws;
344
345    sws->bo_unmap(tex->bo);
346 }
347
348
349
350
351
352 struct u_resource_vtbl brw_texture_vtbl = 
353 {
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 */
363 };
364
365
366
367
368
369 struct pipe_resource *
370 brw_texture_create( struct pipe_screen *screen,
371                     const struct pipe_resource *template )
372 {  
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;
378    
379    tex = CALLOC_STRUCT(brw_texture);
380    if (tex == NULL)
381       return NULL;
382
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;
387
388    /* XXX: compressed textures need special treatment here
389     */
390    tex->cpp = util_format_get_blocksize(tex->b.b.format);
391    tex->compressed = util_format_is_s3tc(tex->b.b.format);
392
393    make_empty_list(&tex->views[0]);
394    make_empty_list(&tex->views[1]);
395
396    /* XXX: No tiling with compressed textures??
397     */
398    if (tex->compressed == 0 &&
399        !bscreen->no_tiling) 
400    {
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;
406    } 
407    else {
408       tex->tiling = BRW_TILING_NONE;
409    }
410
411
412    if (!brw_texture_layout( bscreen, tex ))
413       goto fail;
414
415    
416    if (template->bind & (PIPE_BIND_SCANOUT |
417                            PIPE_BIND_SHARED)) {
418       buffer_type = BRW_BUFFER_TYPE_SCANOUT;
419    }
420    else {
421       buffer_type = BRW_BUFFER_TYPE_TEXTURE;
422    }
423
424    ret = bscreen->sws->bo_alloc( bscreen->sws,
425                                  buffer_type,
426                                  tex->pitch * tex->total_height * tex->cpp,
427                                  64,
428                                  &tex->bo );
429    if (ret)
430       goto fail;
431
432    tex->ss.ss0.mipmap_layout_mode = BRW_SURFACE_MIPMAPLAYOUT_BELOW;
433    tex->ss.ss0.surface_type = translate_tex_target(tex->b.b.target);
434
435    format = translate_tex_format(tex->b.b.format);
436    assert(format != BRW_SURFACEFORMAT_INVALID);
437    tex->ss.ss0.surface_format = format;
438
439    /* This is ok for all textures with channel width 8bit or less:
440     */
441 /*    tex->ss.ss0.data_return_format = BRW_SURFACERETURNFORMAT_S1; */
442
443
444    /* XXX: what happens when tex->bo->offset changes???
445     */
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;
450
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;
464    }
465
466    tex->ss.ss3.pitch = (tex->pitch * tex->cpp) - 1;
467    tex->ss.ss3.depth = tex->b.b.depth0 - 1;
468
469    tex->ss.ss4.min_lod = 0;
470  
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;
478    }
479
480    return &tex->b.b;
481
482 fail:
483    bo_reference(&tex->bo, NULL);
484    FREE(tex);
485    return NULL;
486 }
487
488
489 struct pipe_resource * 
490 brw_texture_from_handle(struct pipe_screen *screen,
491                         const struct pipe_resource *template,
492                         struct winsys_handle *whandle)
493 {
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;
500
501    if (template->target != PIPE_TEXTURE_2D ||
502        template->last_level != 0 ||
503        template->depth0 != 1)
504       return NULL;
505
506    if (util_format_is_s3tc(template->format))
507       return NULL;
508
509    tex = CALLOC_STRUCT(brw_texture);
510    if (!tex)
511       return NULL;
512
513    if (bscreen->sws->bo_from_handle(bscreen->sws, whandle, &pitch, &tiling, &buffer) != PIPE_OK)
514       goto fail;
515
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;
520
521    /* XXX: cpp vs. blocksize
522     */
523    tex->cpp = util_format_get_blocksize(tex->b.b.format);
524    tex->tiling = tiling;
525
526    make_empty_list(&tex->views[0]);
527    make_empty_list(&tex->views[1]);
528
529    if (!brw_texture_layout(bscreen, tex))
530       goto fail;
531
532    /* XXX Maybe some more checks? */
533    if ((pitch / tex->cpp) < tex->pitch)
534       goto fail;
535
536    tex->pitch = pitch / tex->cpp;
537
538    tex->bo = buffer;
539
540    /* fix this warning */
541 #if 0
542    if (tex->size > buffer->size)
543       goto fail;
544 #endif
545
546    tex->ss.ss0.mipmap_layout_mode = BRW_SURFACE_MIPMAPLAYOUT_BELOW;
547    tex->ss.ss0.surface_type = translate_tex_target(tex->b.b.target);
548
549    format = translate_tex_format(tex->b.b.format);
550    assert(format != BRW_SURFACEFORMAT_INVALID);
551    tex->ss.ss0.surface_format = format;
552    assert(tex->ss.ss0.surface_format != BRW_SURFACEFORMAT_INVALID);
553
554    /* This is ok for all textures with channel width 8bit or less:
555     */
556 /*    tex->ss.ss0.data_return_format = BRW_SURFACERETURNFORMAT_S1; */
557
558
559    /* XXX: what happens when tex->bo->offset changes???
560     */
561    tex->ss.ss1.base_addr = 0; /* reloc */
562    tex->ss.ss2.mip_count = tex->b.b.last_level;
563    tex->ss.ss2.width = tex->b.b.width0 - 1;
564    tex->ss.ss2.height = tex->b.b.height0 - 1;
565
566    switch (tex->tiling) {
567    case BRW_TILING_NONE:
568       tex->ss.ss3.tiled_surface = 0;
569       tex->ss.ss3.tile_walk = 0;
570       break;
571    case BRW_TILING_X:
572       tex->ss.ss3.tiled_surface = 1;
573       tex->ss.ss3.tile_walk = BRW_TILEWALK_XMAJOR;
574       break;
575    case BRW_TILING_Y:
576       tex->ss.ss3.tiled_surface = 1;
577       tex->ss.ss3.tile_walk = BRW_TILEWALK_YMAJOR;
578       break;
579    }
580
581    tex->ss.ss3.pitch = (tex->pitch * tex->cpp) - 1;
582    tex->ss.ss3.depth = tex->b.b.depth0 - 1;
583
584    tex->ss.ss4.min_lod = 0;
585
586    return &tex->b.b;
587
588 fail:
589    FREE(tex);
590    return NULL;
591 }
592
593
594 #if 0
595 boolean brw_is_format_supported( struct pipe_screen *screen,
596                                  enum pipe_format format,
597                                  enum pipe_texture_target target,
598                                  unsigned tex_usage, 
599                                  unsigned geom_flags )
600 {
601    return translate_tex_format(format) != BRW_SURFACEFORMAT_INVALID;
602 }
603 #endif