summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/i965/brw_resource_texture.c
blob: 07537fe44efb07f2560cdf0af9de737321df1f65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
/*
 Copyright (C) Intel Corp.  2006.  All Rights Reserved.
 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
 develop this 3D driver.
 
 Permission is hereby granted, free of charge, to any person obtaining
 a copy of this software and associated documentation files (the
 "Software"), to deal in the Software without restriction, including
 without limitation the rights to use, copy, modify, merge, publish,
 distribute, sublicense, and/or sell copies of the Software, and to
 permit persons to whom the Software is furnished to do so, subject to
 the following conditions:
 
 The above copyright notice and this permission notice (including the
 next paragraph) shall be included in all copies or substantial
 portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
 **********************************************************************/
 /*
  * Authors:
  *   Keith Whitwell <keith@tungstengraphics.com>
  */

#include "util/u_memory.h"
#include "util/u_simple_list.h"
#include "util/u_format.h"

#include "brw_screen.h"
#include "brw_defines.h"
#include "brw_structs.h"
#include "brw_winsys.h"
#include "brw_batchbuffer.h"
#include "brw_context.h"
#include "brw_resource.h"


/**
 * Subclass of pipe_transfer
 */
struct brw_transfer
{
   struct pipe_transfer base;

   unsigned offset;
};

static INLINE struct brw_transfer *
brw_transfer(struct pipe_transfer *transfer)
{
   return (struct brw_transfer *)transfer;
}


static GLuint translate_tex_target( unsigned target )
{
   switch (target) {
   case PIPE_TEXTURE_1D: 
      return BRW_SURFACE_1D;

   case PIPE_TEXTURE_2D: 
      return BRW_SURFACE_2D;

   case PIPE_TEXTURE_3D: 
      return BRW_SURFACE_3D;

   case PIPE_TEXTURE_CUBE:
      return BRW_SURFACE_CUBE;

   default: 
      assert(0); 
      return BRW_SURFACE_1D;
   }
}


static GLuint translate_tex_format( enum pipe_format pf )
{
   switch( pf ) {
   case PIPE_FORMAT_L8_UNORM:
      return BRW_SURFACEFORMAT_L8_UNORM;

   case PIPE_FORMAT_I8_UNORM:
      return BRW_SURFACEFORMAT_I8_UNORM;

   case PIPE_FORMAT_A8_UNORM:
      return BRW_SURFACEFORMAT_A8_UNORM; 

   case PIPE_FORMAT_L16_UNORM:
      return BRW_SURFACEFORMAT_L16_UNORM;

      /* XXX: Add these to gallium
   case PIPE_FORMAT_I16_UNORM:
      return BRW_SURFACEFORMAT_I16_UNORM;

   case PIPE_FORMAT_A16_UNORM:
      return BRW_SURFACEFORMAT_A16_UNORM; 
      */

   case PIPE_FORMAT_L8A8_UNORM:
      return BRW_SURFACEFORMAT_L8A8_UNORM;

   case PIPE_FORMAT_B5G6R5_UNORM:
      return BRW_SURFACEFORMAT_B5G6R5_UNORM;

   case PIPE_FORMAT_B5G5R5A1_UNORM:
      return BRW_SURFACEFORMAT_B5G5R5A1_UNORM;

   case PIPE_FORMAT_B4G4R4A4_UNORM:
      return BRW_SURFACEFORMAT_B4G4R4A4_UNORM;

   case PIPE_FORMAT_B8G8R8X8_UNORM:
      return BRW_SURFACEFORMAT_R8G8B8X8_UNORM;

   case PIPE_FORMAT_B8G8R8A8_UNORM:
      return BRW_SURFACEFORMAT_B8G8R8A8_UNORM;

   /*
    * Video formats
    */

   case PIPE_FORMAT_YUYV:
      return BRW_SURFACEFORMAT_YCRCB_NORMAL;

   case PIPE_FORMAT_UYVY:
      return BRW_SURFACEFORMAT_YCRCB_SWAPUVY;

   /*
    * Compressed formats.
    */
      /* XXX: Add FXT to gallium?
   case PIPE_FORMAT_FXT1_RGBA:
      return BRW_SURFACEFORMAT_FXT1;
      */

   case PIPE_FORMAT_DXT1_RGB:
       return BRW_SURFACEFORMAT_DXT1_RGB;

   case PIPE_FORMAT_DXT1_RGBA:
       return BRW_SURFACEFORMAT_BC1_UNORM;
       
   case PIPE_FORMAT_DXT3_RGBA:
       return BRW_SURFACEFORMAT_BC2_UNORM;
       
   case PIPE_FORMAT_DXT5_RGBA:
       return BRW_SURFACEFORMAT_BC3_UNORM;

   /*
    * sRGB formats
    */

   case PIPE_FORMAT_A8B8G8R8_SRGB:
      return BRW_SURFACEFORMAT_B8G8R8A8_UNORM_SRGB;

   case PIPE_FORMAT_L8A8_SRGB:
      return BRW_SURFACEFORMAT_L8A8_UNORM_SRGB;

   case PIPE_FORMAT_L8_SRGB:
      return BRW_SURFACEFORMAT_L8_UNORM_SRGB;

   case PIPE_FORMAT_DXT1_SRGB:
      return BRW_SURFACEFORMAT_BC1_UNORM_SRGB;

   /*
    * Depth formats
    */

   case PIPE_FORMAT_Z16_UNORM:
         return BRW_SURFACEFORMAT_I16_UNORM;

   case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
   case PIPE_FORMAT_Z24X8_UNORM:
         return BRW_SURFACEFORMAT_I24X8_UNORM;

   case PIPE_FORMAT_Z32_FLOAT:
         return BRW_SURFACEFORMAT_I32_FLOAT;

      /* XXX: presumably for bump mapping.  Add this to mesa state
       * tracker?
       *
       * XXX: Add flipped versions of these formats to Gallium.
       */
   case PIPE_FORMAT_R8G8_SNORM:
      return BRW_SURFACEFORMAT_R8G8_SNORM;

   case PIPE_FORMAT_R8G8B8A8_SNORM:
      return BRW_SURFACEFORMAT_R8G8B8A8_SNORM;

   default:
      return BRW_SURFACEFORMAT_INVALID;
   }
}


static boolean
brw_texture_get_handle(struct pipe_screen *screen,
                       struct pipe_resource *texture,
                       struct winsys_handle *whandle)
{
   struct brw_screen *bscreen = brw_screen(screen);
   struct brw_texture *tex = brw_texture(texture);
   unsigned stride;

   stride = tex->pitch * tex->cpp;

   return bscreen->sws->bo_get_handle(tex->bo, whandle, stride);
}



static void brw_texture_destroy(struct pipe_screen *screen,
				struct pipe_resource *pt)
{
   struct brw_texture *tex = brw_texture(pt);
   bo_reference(&tex->bo, NULL);
   FREE(pt);
}




static unsigned brw_texture_is_referenced( struct pipe_context *pipe,
					   struct pipe_resource *texture,
					   unsigned face, 
					   unsigned level )
{
   struct brw_context *brw = brw_context(pipe);
   struct brw_screen *bscreen = brw_screen(pipe->screen);
   struct brw_winsys_buffer *batch_bo = brw->batch->buf;
   struct brw_texture *tex = brw_texture(texture);
   struct brw_surface *surf;
   int i;

   /* XXX: this is subject to false positives if the underlying
    * texture BO is referenced, we can't tell whether the sub-region
    * we care about participates in that.
    */
   if (bscreen->sws->bo_references( batch_bo, tex->bo ))
      return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;

   /* Find any view on this texture for this face/level and see if it
    * is referenced:
    */
   for (i = 0; i < 2; i++) {
      foreach (surf, &tex->views[i]) {
         if (surf->bo == tex->bo)
            continue;

         if (surf->id.bits.face != face ||
             surf->id.bits.level != level)
            continue;
         
         if (bscreen->sws->bo_references( batch_bo, surf->bo))
            return PIPE_REFERENCED_FOR_READ | PIPE_REFERENCED_FOR_WRITE;
      }
   }

   return PIPE_UNREFERENCED;
}


/*
 * Transfer functions
 */


static struct pipe_transfer * 
brw_texture_get_transfer(struct pipe_context *context,
			  struct pipe_resource *resource,
			  struct pipe_subresource sr,
			  unsigned usage,
			  const struct pipe_box *box)
{
   struct brw_texture *tex = brw_texture(resource);
   struct pipe_transfer *transfer = CALLOC_STRUCT(pipe_transfer);
   if (transfer == NULL)
      return NULL;

   transfer->resource = resource;
   transfer->sr = sr;
   transfer->usage = usage;
   transfer->box = *box;
   transfer->stride = tex->pitch * tex->cpp;

   return transfer;
}


static void *
brw_texture_transfer_map(struct pipe_context *pipe,
                 struct pipe_transfer *transfer)
{
   struct pipe_resource *resource = transfer->resource;
   struct brw_texture *tex = brw_texture(transfer->resource);
   struct brw_winsys_screen *sws = brw_screen(pipe->screen)->sws;
   struct pipe_subresource sr = transfer->sr;
   struct pipe_box *box = &transfer->box;
   enum pipe_format format = resource->format;
   unsigned usage = transfer->usage;
   unsigned offset;
   char *map;

   if (resource->target == PIPE_TEXTURE_CUBE) {
      offset = tex->image_offset[sr.level][sr.face];
   }
   else if (resource->target == PIPE_TEXTURE_3D) {
      offset = tex->image_offset[sr.level][box->z];
   }
   else {
      offset = tex->image_offset[sr.level][0];
      assert(sr.face == 0);
      assert(box->z == 0);
   }

   map = sws->bo_map(tex->bo, 
                     BRW_DATA_OTHER,
                     0,
                     tex->bo->size,
                     (usage & PIPE_TRANSFER_WRITE) ? TRUE : FALSE,
                     (usage & 0) ? TRUE : FALSE,
                     (usage & 0) ? TRUE : FALSE);

   if (!map)
      return NULL;

   return map + offset +
      box->y / util_format_get_blockheight(format) * transfer->stride +
      box->x / util_format_get_blockwidth(format) * util_format_get_blocksize(format);
}

static void
brw_texture_transfer_unmap(struct pipe_context *pipe,
                   struct pipe_transfer *transfer)
{
   struct brw_texture *tex = brw_texture(transfer->resource);
   struct brw_winsys_screen *sws = brw_screen(pipe->screen)->sws;

   sws->bo_unmap(tex->bo);
}





struct u_resource_vtbl brw_texture_vtbl = 
{
   brw_texture_get_handle,	      /* get_handle */
   brw_texture_destroy,	      /* resource_destroy */
   brw_texture_is_referenced,	      /* is_resource_referenced */
   brw_texture_get_transfer,	      /* get_transfer */
   u_default_transfer_destroy,	      /* transfer_destroy */
   brw_texture_transfer_map,	      /* transfer_map */
   u_default_transfer_flush_region,   /* transfer_flush_region */
   brw_texture_transfer_unmap,	      /* transfer_unmap */
   u_default_transfer_inline_write    /* transfer_inline_write */
};





struct pipe_resource *
brw_texture_create( struct pipe_screen *screen,
		    const struct pipe_resource *template )
{  
   struct brw_screen *bscreen = brw_screen(screen);
   struct brw_texture *tex;
   enum brw_buffer_type buffer_type;
   enum pipe_error ret;
   GLuint format;
   
   tex = CALLOC_STRUCT(brw_texture);
   if (tex == NULL)
      return NULL;

   tex->b.b = *template;
   tex->b.vtbl = &brw_texture_vtbl;
   pipe_reference_init(&tex->b.b.reference, 1);
   tex->b.b.screen = screen;

   /* XXX: compressed textures need special treatment here
    */
   tex->cpp = util_format_get_blocksize(tex->b.b.format);
   tex->compressed = util_format_is_s3tc(tex->b.b.format);

   make_empty_list(&tex->views[0]);
   make_empty_list(&tex->views[1]);

   /* XXX: No tiling with compressed textures??
    */
   if (tex->compressed == 0 &&
       !bscreen->no_tiling) 
   {
      if (bscreen->chipset.is_965 &&
	  util_format_is_depth_or_stencil(template->format))
	 tex->tiling = BRW_TILING_Y;
      else
	 tex->tiling = BRW_TILING_X;
   } 
   else {
      tex->tiling = BRW_TILING_NONE;
   }


   if (!brw_texture_layout( bscreen, tex ))
      goto fail;

   
   if (template->bind & (PIPE_BIND_SCANOUT |
                           PIPE_BIND_SHARED)) {
      buffer_type = BRW_BUFFER_TYPE_SCANOUT;
   }
   else {
      buffer_type = BRW_BUFFER_TYPE_TEXTURE;
   }

   ret = bscreen->sws->bo_alloc( bscreen->sws,
                                 buffer_type,
                                 tex->pitch * tex->total_height * tex->cpp,
                                 64,
                                 &tex->bo );
   if (ret)
      goto fail;

   tex->ss.ss0.mipmap_layout_mode = BRW_SURFACE_MIPMAPLAYOUT_BELOW;
   tex->ss.ss0.surface_type = translate_tex_target(tex->b.b.target);

   format = translate_tex_format(tex->b.b.format);
   assert(format != BRW_SURFACEFORMAT_INVALID);
   tex->ss.ss0.surface_format = format;

   /* This is ok for all textures with channel width 8bit or less:
    */
/*    tex->ss.ss0.data_return_format = BRW_SURFACERETURNFORMAT_S1; */


   /* XXX: what happens when tex->bo->offset changes???
    */
   tex->ss.ss1.base_addr = 0; /* reloc */
   tex->ss.ss2.mip_count = tex->b.b.last_level;
   tex->ss.ss2.width = tex->b.b.width0 - 1;
   tex->ss.ss2.height = tex->b.b.height0 - 1;

   switch (tex->tiling) {
   case BRW_TILING_NONE:
      tex->ss.ss3.tiled_surface = 0;
      tex->ss.ss3.tile_walk = 0;
      break;
   case BRW_TILING_X:
      tex->ss.ss3.tiled_surface = 1;
      tex->ss.ss3.tile_walk = BRW_TILEWALK_XMAJOR;
      break;
   case BRW_TILING_Y:
      tex->ss.ss3.tiled_surface = 1;
      tex->ss.ss3.tile_walk = BRW_TILEWALK_YMAJOR;
      break;
   }

   tex->ss.ss3.pitch = (tex->pitch * tex->cpp) - 1;
   tex->ss.ss3.depth = tex->b.b.depth0 - 1;

   tex->ss.ss4.min_lod = 0;
 
   if (tex->b.b.target == PIPE_TEXTURE_CUBE) {
      tex->ss.ss0.cube_pos_x = 1;
      tex->ss.ss0.cube_pos_y = 1;
      tex->ss.ss0.cube_pos_z = 1;
      tex->ss.ss0.cube_neg_x = 1;
      tex->ss.ss0.cube_neg_y = 1;
      tex->ss.ss0.cube_neg_z = 1;
   }

   return &tex->b.b;

fail:
   bo_reference(&tex->bo, NULL);
   FREE(tex);
   return NULL;
}


struct pipe_resource * 
brw_texture_from_handle(struct pipe_screen *screen,
                        const struct pipe_resource *template,
                        struct winsys_handle *whandle)
{
   struct brw_screen *bscreen = brw_screen(screen);
   struct brw_texture *tex;
   struct brw_winsys_buffer *buffer;
   unsigned tiling;
   unsigned pitch;
   GLuint format;

   if (template->target != PIPE_TEXTURE_2D ||
       template->last_level != 0 ||
       template->depth0 != 1)
      return NULL;

   if (util_format_is_s3tc(template->format))
      return NULL;

   tex = CALLOC_STRUCT(brw_texture);
   if (!tex)
      return NULL;

   if (bscreen->sws->bo_from_handle(bscreen->sws, whandle, &pitch, &tiling, &buffer) != PIPE_OK)
      goto fail;

   tex->b.b = *template;
   tex->b.vtbl = &brw_texture_vtbl;
   pipe_reference_init(&tex->b.b.reference, 1);
   tex->b.b.screen = screen;

   /* XXX: cpp vs. blocksize
    */
   tex->cpp = util_format_get_blocksize(tex->b.b.format);
   tex->tiling = tiling;

   make_empty_list(&tex->views[0]);
   make_empty_list(&tex->views[1]);

   if (!brw_texture_layout(bscreen, tex))
      goto fail;

   /* XXX Maybe some more checks? */
   if ((pitch / tex->cpp) < tex->pitch)
      goto fail;

   tex->pitch = pitch / tex->cpp;

   tex->bo = buffer;

   /* fix this warning */
#if 0
   if (tex->size > buffer->size)
      goto fail;
#endif

   tex->ss.ss0.mipmap_layout_mode = BRW_SURFACE_MIPMAPLAYOUT_BELOW;
   tex->ss.ss0.surface_type = translate_tex_target(tex->b.b.target);

   format = translate_tex_format(tex->b.b.format);
   assert(format != BRW_SURFACEFORMAT_INVALID);
   tex->ss.ss0.surface_format = format;

   /* This is ok for all textures with channel width 8bit or less:
    */
/*    tex->ss.ss0.data_return_format = BRW_SURFACERETURNFORMAT_S1; */


   /* XXX: what happens when tex->bo->offset changes???
    */
   tex->ss.ss1.base_addr = 0; /* reloc */
   tex->ss.ss2.mip_count = tex->b.b.last_level;
   tex->ss.ss2.width = tex->b.b.width0 - 1;
   tex->ss.ss2.height = tex->b.b.height0 - 1;

   switch (tex->tiling) {
   case BRW_TILING_NONE:
      tex->ss.ss3.tiled_surface = 0;
      tex->ss.ss3.tile_walk = 0;
      break;
   case BRW_TILING_X:
      tex->ss.ss3.tiled_surface = 1;
      tex->ss.ss3.tile_walk = BRW_TILEWALK_XMAJOR;
      break;
   case BRW_TILING_Y:
      tex->ss.ss3.tiled_surface = 1;
      tex->ss.ss3.tile_walk = BRW_TILEWALK_YMAJOR;
      break;
   }

   tex->ss.ss3.pitch = (tex->pitch * tex->cpp) - 1;
   tex->ss.ss3.depth = tex->b.b.depth0 - 1;

   tex->ss.ss4.min_lod = 0;

   return &tex->b.b;

fail:
   FREE(tex);
   return NULL;
}


#if 0
boolean brw_is_format_supported( struct pipe_screen *screen,
				 enum pipe_format format,
				 enum pipe_texture_target target,
				 unsigned tex_usage, 
				 unsigned geom_flags )
{
   return translate_tex_format(format) != BRW_SURFACEFORMAT_INVALID;
}
#endif