summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/i965/brw_screen_texture.c
blob: 8e684aa07601277093bed381d210d1d7ab0b0f3c (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
/*
 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_A8L8_UNORM:
      return BRW_SURFACEFORMAT_L8A8_UNORM;

   case PIPE_FORMAT_A8R8G8B8_UNORM: /* XXX */
   case PIPE_FORMAT_B8G8R8A8_UNORM: /* XXX */
   case PIPE_FORMAT_R8G8B8A8_UNORM: /* XXX */
      return BRW_SURFACEFORMAT_B8G8R8A8_UNORM;

   case PIPE_FORMAT_R8G8B8X8_UNORM:
      return BRW_SURFACEFORMAT_R8G8B8X8_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_L16_UNORM:
      return BRW_SURFACEFORMAT_L16_UNORM;

      /* XXX: Z texturing: 
   case PIPE_FORMAT_I16_UNORM:
      return BRW_SURFACEFORMAT_I16_UNORM;
       */

      /* XXX: Z texturing:
   case PIPE_FORMAT_A16_UNORM:
      return BRW_SURFACEFORMAT_A16_UNORM; 
      */

   case PIPE_FORMAT_YCBCR_REV:
      return BRW_SURFACEFORMAT_YCRCB_NORMAL;

   case PIPE_FORMAT_YCBCR:
      return BRW_SURFACEFORMAT_YCRCB_SWAPUVY;

      /* 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;

   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;

      /* XXX: which pipe depth formats does i965 suppport
       */
   case PIPE_FORMAT_S8Z24_UNORM:
   case PIPE_FORMAT_X8Z24_UNORM:
   case PIPE_FORMAT_Z24S8_UNORM:
   case PIPE_FORMAT_Z24X8_UNORM:
         return BRW_SURFACEFORMAT_I24X8_UNORM;

#if 0
      /* XXX: these different surface formats don't seem to
       * make any difference for shadow sampler/compares.
       */
      if (depth_mode == GL_INTENSITY) 
         return BRW_SURFACEFORMAT_I24X8_UNORM;
      else if (depth_mode == GL_ALPHA)
         return BRW_SURFACEFORMAT_A24X8_UNORM;
      else
         return BRW_SURFACEFORMAT_L24X8_UNORM;
#endif

      /* XXX: presumably for bump mapping.  Add this to mesa state
       * tracker?
       */
   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;
}



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