summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/i965/brw_screen_texture.c
blob: 666ec70d4240a37d96c42a4a422f6a8f2d967d71 (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
/*
 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 "brw_screen.h"
#include "brw_defines.h"
#include "brw_structs.h"
#include "brw_winsys.h"



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_A8L8_UNORM:
      return BRW_SURFACEFORMAT_L8A8_UNORM;

   case PIPE_FORMAT_R5G6B5_UNORM:
      return BRW_SURFACEFORMAT_B5G6R5_UNORM;

   case PIPE_FORMAT_A1R5G5B5_UNORM:
      return BRW_SURFACEFORMAT_B5G5R5A1_UNORM;

   case PIPE_FORMAT_A4R4G4B4_UNORM:
      return BRW_SURFACEFORMAT_B4G4R4A4_UNORM;

   case PIPE_FORMAT_X8R8G8B8_UNORM:
      return BRW_SURFACEFORMAT_R8G8B8X8_UNORM;

   case PIPE_FORMAT_A8R8G8B8_UNORM:
      return BRW_SURFACEFORMAT_B8G8R8A8_UNORM;

   /*
    * Video formats
    */

   case PIPE_FORMAT_YCBCR_REV:
      return BRW_SURFACEFORMAT_YCRCB_NORMAL;

   case PIPE_FORMAT_YCBCR:
      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_R8G8B8A8_SRGB:
      return BRW_SURFACEFORMAT_B8G8R8A8_UNORM_SRGB;

   case PIPE_FORMAT_A8L8_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_S8Z24_UNORM:
   case PIPE_FORMAT_X8Z24_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 struct pipe_texture *brw_texture_create( struct pipe_screen *screen,
						const struct pipe_texture *templ )

{  
   struct brw_screen *bscreen = brw_screen(screen);
   struct brw_texture *tex;
   enum brw_buffer_type buffer_type;
   enum pipe_error ret;
   
   tex = CALLOC_STRUCT(brw_texture);
   if (tex == NULL)
      return NULL;

   memcpy(&tex->base, templ, sizeof *templ);
   pipe_reference_init(&tex->base.reference, 1);
   tex->base.screen = screen;

   /* XXX: compressed textures need special treatment here
    */
   tex->cpp = pf_get_size(tex->base.format);
   tex->compressed = pf_is_compressed(tex->base.format);

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

   /* XXX: No tiling with compressed textures??
    */
   if (tex->compressed == 0 
       /* && bscreen->use_texture_tiling */
       /* && bscreen->kernel_exec_fencing */) 
   {
      if (1)
         tex->tiling = BRW_TILING_NONE;
      else if (bscreen->chipset.is_965 &&
               pf_is_depth_or_stencil(templ->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 (templ->tex_usage & (PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
                           PIPE_TEXTURE_USAGE_PRIMARY)) {
      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->base.target);
   tex->ss.ss0.surface_format = translate_tex_format(tex->base.format);
   assert(tex->ss.ss0.surface_format != BRW_SURFACEFORMAT_INVALID);

   /* 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->base.last_level;
   tex->ss.ss2.width = tex->base.width[0] - 1;
   tex->ss.ss2.height = tex->base.height[0] - 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->base.depth[0] - 1;

   tex->ss.ss4.min_lod = 0;
 
   if (tex->base.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->base;

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

static struct pipe_texture *brw_texture_blanket(struct pipe_screen *screen,
						const struct pipe_texture *templ,
						const unsigned *stride,
						struct pipe_buffer *buffer)
{
   return NULL;
}

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


static 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;
}


boolean brw_is_texture_referenced_by_bo( struct brw_screen *brw_screen,
                                      struct pipe_texture *texture,
                                      unsigned face, 
                                      unsigned level,
                                      struct brw_winsys_buffer *bo )
{
   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 (brw_screen->sws->bo_references( bo, tex->bo ))
      return TRUE;

   /* 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 (brw_screen->sws->bo_references( bo, surf->bo))
            return TRUE;
      }
   }

   return FALSE;
}


/*
 * Transfer functions
 */

static struct pipe_transfer*
brw_get_tex_transfer(struct pipe_screen *screen,
                     struct pipe_texture *texture,
                     unsigned face, unsigned level, unsigned zslice,
                     enum pipe_transfer_usage usage, unsigned x, unsigned y,
                     unsigned w, unsigned h)
{
   struct brw_texture *tex = brw_texture(texture);
   struct brw_transfer *trans;
   unsigned offset;  /* in bytes */

   if (texture->target == PIPE_TEXTURE_CUBE) {
      offset = tex->image_offset[level][face];
   } else if (texture->target == PIPE_TEXTURE_3D) {
      offset = tex->image_offset[level][zslice];
   } else {
      offset = tex->image_offset[level][0];
      assert(face == 0);
      assert(zslice == 0);
   }

   trans = CALLOC_STRUCT(brw_transfer);
   if (trans) {
      pipe_texture_reference(&trans->base.texture, texture);
      trans->base.format = trans->base.format;
      trans->base.x = x;
      trans->base.y = y;
      trans->base.width = w;
      trans->base.height = h;
      trans->base.block = texture->block;
      trans->base.nblocksx = texture->nblocksx[level];
      trans->base.nblocksy = texture->nblocksy[level];
      trans->base.stride = tex->pitch * tex->cpp;
      trans->offset = offset;
      trans->base.usage = usage;
   }
   return &trans->base;
}

static void *
brw_transfer_map(struct pipe_screen *screen,
                 struct pipe_transfer *transfer)
{
   struct brw_texture *tex = brw_texture(transfer->texture);
   struct brw_winsys_screen *sws = brw_screen(screen)->sws;
   char *map;
   unsigned usage = transfer->usage;

   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 + brw_transfer(transfer)->offset +
      transfer->y / transfer->block.height * transfer->stride +
      transfer->x / transfer->block.width * transfer->block.size;
}

static void
brw_transfer_unmap(struct pipe_screen *screen,
                   struct pipe_transfer *transfer)
{
   struct brw_texture *tex = brw_texture(transfer->texture);
   struct brw_winsys_screen *sws = brw_screen(screen)->sws;

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

static void
brw_tex_transfer_destroy(struct pipe_transfer *trans)
{
   pipe_texture_reference(&trans->texture, NULL);
   FREE(trans);
}


/*
 * Functions exported to the winsys
 */

boolean brw_texture_get_winsys_buffer(struct pipe_texture *texture,
                                      struct brw_winsys_buffer **buffer,
                                      unsigned *stride)
{
   struct brw_texture *tex = brw_texture(texture);

   *buffer = tex->bo;
   if (stride)
      *stride = tex->pitch * tex->cpp;

   return TRUE;
}

struct pipe_texture * 
brw_texture_blanket_winsys_buffer(struct pipe_screen *screen,
                                  const struct pipe_texture *templ,
                                  const unsigned pitch,
                                  struct brw_winsys_buffer *buffer)
{
   struct brw_screen *bscreen = brw_screen(screen);
   struct brw_texture *tex;

   if (templ->target != PIPE_TEXTURE_2D ||
       templ->last_level != 0 ||
       templ->depth[0] != 1)
      return NULL;

   if (pf_is_compressed(templ->format))
      return NULL;

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

   memcpy(&tex->base, templ, sizeof *templ);
   pipe_reference_init(&tex->base.reference, 1);
   tex->base.screen = screen;

   tex->cpp = pf_get_size(tex->base.format);

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

   if (1)
      tex->tiling = BRW_TILING_NONE;
   else if (bscreen->chipset.is_965 &&
            pf_is_depth_or_stencil(templ->format))
      tex->tiling = BRW_TILING_Y;
   else
      tex->tiling = BRW_TILING_X;

   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->base.target);
   tex->ss.ss0.surface_format = translate_tex_format(tex->base.format);
   assert(tex->ss.ss0.surface_format != BRW_SURFACEFORMAT_INVALID);

   /* 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->base.last_level;
   tex->ss.ss2.width = tex->base.width[0] - 1;
   tex->ss.ss2.height = tex->base.height[0] - 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->base.depth[0] - 1;

   tex->ss.ss4.min_lod = 0;

   return &tex->base;

fail:
   FREE(tex);
   return NULL;
}

void brw_screen_tex_init( struct brw_screen *brw_screen )
{
   brw_screen->base.is_format_supported = brw_is_format_supported;
   brw_screen->base.texture_create = brw_texture_create;
   brw_screen->base.texture_destroy = brw_texture_destroy;
   brw_screen->base.texture_blanket = brw_texture_blanket;
   brw_screen->base.get_tex_transfer = brw_get_tex_transfer;
   brw_screen->base.transfer_map = brw_transfer_map;
   brw_screen->base.transfer_unmap = brw_transfer_unmap;
   brw_screen->base.tex_transfer_destroy = brw_tex_transfer_destroy;
}