~ubuntu-branches/ubuntu/natty/mesa/natty-proposed

« back to all changes in this revision

Viewing changes to src/mesa/state_tracker/st_cb_texture.c

  • Committer: Bazaar Package Importer
  • Author(s): Robert Hooker, Robert Hooker, Christopher James Halse Rogers
  • Date: 2010-09-14 08:55:40 UTC
  • mfrom: (1.2.28 upstream)
  • Revision ID: james.westby@ubuntu.com-20100914085540-m4fpl0hdjlfd4jgz
Tags: 7.9~git20100909-0ubuntu1
[ Robert Hooker ]
* New upstream git snapshot up to commit 94118fe2d4b1e5 (LP: #631413)
* New features include ATI HD5xxx series support in r600, and a vastly
  improved glsl compiler.
* Remove pre-generated .pc's, use the ones generated at build time
  instead.
* Remove all references to mesa-utils now that its no longer shipped
  with the mesa source.
* Disable the experimental ARB_fragment_shader option by default on
  i915, it exposes incomplete functionality that breaks KDE compositing
  among other things. It can be enabled via driconf still. (LP: #628930).

[ Christopher James Halse Rogers ]
* debian/patches/04_osmesa_version.diff:
  - Refresh for new upstream
* Bugs fixed in this release:
  - Fixes severe rendering corruption in Unity on radeon (LP: #628727,
    LP: #596292, LP: #599741, LP: #630315, LP: #613694, LP: #599741).
  - Also fixes rendering in gnome-shell (LP: #578619).
  - Flickering in OpenGL apps on radeon (LP: #626943, LP: #610541).
  - Provides preliminary support for new intel chips (LP: #601052).
* debian/rules:
  - Update configure flags to match upstream reshuffling.
  - Explicitly remove gallium DRI drivers that we don't want to ship.
* Update debian/gbp.conf for this Maverick-specific packaging
* libegl1-mesa-dri-x11,kms: There are no longer separate kms or x11 drivers
  for EGL, libegl1-mesa-drivers now contains a single driver that provides
  both backends.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 
28
28
#include "main/mfeatures.h"
29
29
#include "main/bufferobj.h"
30
 
#if FEATURE_convolve
31
 
#include "main/convolve.h"
32
 
#endif
33
30
#include "main/enums.h"
34
31
#include "main/fbobject.h"
35
32
#include "main/formats.h"
47
44
#include "state_tracker/st_debug.h"
48
45
#include "state_tracker/st_context.h"
49
46
#include "state_tracker/st_cb_fbo.h"
 
47
#include "state_tracker/st_cb_flush.h"
50
48
#include "state_tracker/st_cb_texture.h"
51
49
#include "state_tracker/st_format.h"
52
 
#include "state_tracker/st_public.h"
53
50
#include "state_tracker/st_texture.h"
54
51
#include "state_tracker/st_gen_mipmap.h"
55
 
#include "state_tracker/st_inlines.h"
56
52
#include "state_tracker/st_atom.h"
57
53
 
58
54
#include "pipe/p_context.h"
63
59
#include "util/u_blit.h"
64
60
#include "util/u_format.h"
65
61
#include "util/u_surface.h"
 
62
#include "util/u_sampler.h"
66
63
#include "util/u_math.h"
67
64
 
68
65
 
77
74
      return PIPE_TEXTURE_1D;
78
75
 
79
76
   case GL_TEXTURE_2D:
80
 
   case GL_TEXTURE_RECTANGLE_NV:
81
77
      return PIPE_TEXTURE_2D;
82
78
 
 
79
   case GL_TEXTURE_RECTANGLE_NV:
 
80
      return PIPE_TEXTURE_RECT;
 
81
 
83
82
   case GL_TEXTURE_3D:
84
83
      return PIPE_TEXTURE_3D;
85
84
 
115
114
   return &obj->base;
116
115
}
117
116
 
118
 
/** called via ctx->Driver.DeleteTextureImage() */
 
117
/** called via ctx->Driver.DeleteTextureObject() */
119
118
static void 
120
119
st_DeleteTextureObject(GLcontext *ctx,
121
120
                       struct gl_texture_object *texObj)
122
121
{
 
122
   struct st_context *st = st_context(ctx);
123
123
   struct st_texture_object *stObj = st_texture_object(texObj);
124
124
   if (stObj->pt)
125
 
      pipe_texture_reference(&stObj->pt, NULL);
126
 
 
 
125
      pipe_resource_reference(&stObj->pt, NULL);
 
126
   if (stObj->sampler_view) {
 
127
      if (stObj->sampler_view->context != st->pipe) {
 
128
         /* Take "ownership" of this texture sampler view by setting
 
129
          * its context pointer to this context.  This avoids potential
 
130
          * crashes when the texture object is shared among contexts
 
131
          * and the original/owner context has already been destroyed.
 
132
          */
 
133
         stObj->sampler_view->context = st->pipe;
 
134
      }
 
135
      pipe_sampler_view_reference(&stObj->sampler_view, NULL);
 
136
   }
127
137
   _mesa_delete_texture_object(ctx, texObj);
128
138
}
129
139
 
137
147
   DBG("%s\n", __FUNCTION__);
138
148
 
139
149
   if (stImage->pt) {
140
 
      pipe_texture_reference(&stImage->pt, NULL);
 
150
      pipe_resource_reference(&stImage->pt, NULL);
141
151
   }
142
152
 
143
153
   if (texImage->Data) {
198
208
 
199
209
 
200
210
/**
201
 
 * Return default texture usage bitmask for the given texture format.
 
211
 * Return default texture resource binding bitmask for the given format.
202
212
 */
203
213
static GLuint
204
 
default_usage(enum pipe_format fmt)
205
 
{
206
 
   GLuint usage = PIPE_TEXTURE_USAGE_SAMPLER;
207
 
   if (util_format_is_depth_or_stencil(fmt))
208
 
      usage |= PIPE_TEXTURE_USAGE_DEPTH_STENCIL;
209
 
   else
210
 
      usage |= PIPE_TEXTURE_USAGE_RENDER_TARGET;
211
 
   return usage;
 
214
default_bindings(struct st_context *st, enum pipe_format format)
 
215
{
 
216
   struct pipe_screen *screen = st->pipe->screen;
 
217
   const unsigned target = PIPE_TEXTURE_2D;
 
218
   const unsigned geom = 0x0;
 
219
   unsigned bindings;
 
220
 
 
221
   if (util_format_is_depth_or_stencil(format))
 
222
      bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DEPTH_STENCIL;
 
223
   else
 
224
      bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
 
225
 
 
226
   if (screen->is_format_supported(screen, format, target, 0, bindings, geom))
 
227
      return bindings;
 
228
   else
 
229
      return PIPE_BIND_SAMPLER_VIEW;
 
230
}
 
231
 
 
232
 
 
233
/** Return number of image dimensions (1, 2 or 3) for a texture target. */
 
234
static GLuint
 
235
get_texture_dims(GLenum target)
 
236
{
 
237
   switch (target) {
 
238
   case GL_TEXTURE_1D:
 
239
   case GL_TEXTURE_1D_ARRAY_EXT:
 
240
      return 1;
 
241
   case GL_TEXTURE_2D:
 
242
   case GL_TEXTURE_CUBE_MAP_ARB:
 
243
   case GL_TEXTURE_RECTANGLE_NV:
 
244
   case GL_TEXTURE_2D_ARRAY_EXT:
 
245
      return 2;
 
246
   case GL_TEXTURE_3D:
 
247
      return 3;
 
248
   default:
 
249
      assert(0 && "invalid texture target in get_texture_dims()");
 
250
      return 1;
 
251
   }
212
252
}
213
253
 
214
254
 
215
255
/**
216
 
 * Allocate a pipe_texture object for the given st_texture_object using
217
 
 * the given st_texture_image to guess the mipmap size/levels.
218
 
 *
219
 
 * [comments...]
220
 
 * Otherwise, store it in memory if (Border != 0) or (any dimension ==
221
 
 * 1).
222
 
 *    
223
 
 * Otherwise, if max_level >= level >= min_level, create texture with
224
 
 * space for images from min_level down to max_level.
225
 
 *
226
 
 * Otherwise, create texture with space for images from (level 0)..(1x1).
227
 
 * Consider pruning this texture at a validation if the saving is worth it.
 
256
 * Try to allocate a pipe_resource object for the given st_texture_object.
 
257
 *
 
258
 * We use the given st_texture_image as a clue to determine the size of the
 
259
 * mipmap image at level=0.
 
260
 *
 
261
 * \return GL_TRUE for success, GL_FALSE if out of memory.
228
262
 */
229
 
static void
 
263
static GLboolean
230
264
guess_and_alloc_texture(struct st_context *st,
231
265
                        struct st_texture_object *stObj,
232
266
                        const struct st_texture_image *stImage)
233
267
{
234
 
   GLuint firstLevel;
235
 
   GLuint lastLevel;
236
 
   GLuint width = stImage->base.Width2;  /* size w/out border */
237
 
   GLuint height = stImage->base.Height2;
238
 
   GLuint depth = stImage->base.Depth2;
239
 
   GLuint i, usage;
 
268
   const GLuint dims = get_texture_dims(stObj->base.Target);
 
269
   GLuint level, lastLevel, width, height, depth;
 
270
   GLuint bindings;
240
271
   enum pipe_format fmt;
241
272
 
242
273
   DBG("%s\n", __FUNCTION__);
243
274
 
244
275
   assert(!stObj->pt);
245
276
 
246
 
   if (stObj->pt &&
247
 
       (GLint) stImage->level > stObj->base.BaseLevel &&
248
 
       (stImage->base.Width == 1 ||
249
 
        (stObj->base.Target != GL_TEXTURE_1D &&
250
 
         stImage->base.Height == 1) ||
251
 
        (stObj->base.Target == GL_TEXTURE_3D &&
252
 
         stImage->base.Depth == 1)))
253
 
      return;
254
 
 
255
 
   /* If this image disrespects BaseLevel, allocate from level zero.
256
 
    * Usually BaseLevel == 0, so it's unlikely to happen.
257
 
    */
258
 
   if ((GLint) stImage->level < stObj->base.BaseLevel)
259
 
      firstLevel = 0;
260
 
   else
261
 
      firstLevel = stObj->base.BaseLevel;
262
 
 
263
 
 
264
 
   /* Figure out image dimensions at start level. 
265
 
    */
266
 
   for (i = stImage->level; i > firstLevel; i--) {
 
277
   level = stImage->level;
 
278
   width = stImage->base.Width2;  /* size w/out border */
 
279
   height = stImage->base.Height2;
 
280
   depth = stImage->base.Depth2;
 
281
 
 
282
   assert(width > 0);
 
283
   assert(height > 0);
 
284
   assert(depth > 0);
 
285
 
 
286
   /* Depending on the image's size, we can't always make a guess here.
 
287
    */
 
288
   if (level > 0) {
 
289
      if ( (dims >= 1 && width == 1) ||
 
290
           (dims >= 2 && height == 1) ||
 
291
           (dims >= 3 && depth == 1) ) {
 
292
         /* we can't determine the image size at level=0 */
 
293
         stObj->width0 = stObj->height0 = stObj->depth0 = 0;
 
294
         /* this is not an out of memory error */
 
295
         return GL_TRUE;
 
296
      }
 
297
   }
 
298
 
 
299
   /* grow the image size until we hit level = 0 */
 
300
   while (level > 0) {
267
301
      if (width != 1)
268
302
         width <<= 1;
269
303
      if (height != 1)
270
304
         height <<= 1;
271
305
      if (depth != 1)
272
306
         depth <<= 1;
273
 
   }
274
 
 
275
 
   if (width == 0 || height == 0 || depth == 0) {
276
 
      /* no texture needed */
277
 
      return;
278
 
   }
279
 
 
280
 
   /* Guess a reasonable value for lastLevel.  This is probably going
281
 
    * to be wrong fairly often and might mean that we have to look at
282
 
    * resizable buffers, or require that buffers implement lazy
283
 
    * pagetable arrangements.
 
307
      level--;
 
308
   }      
 
309
 
 
310
   assert(level == 0);
 
311
 
 
312
   /* At this point, (width x height x depth) is the expected size of
 
313
    * the level=0 mipmap image.
 
314
    */
 
315
 
 
316
   /* Guess a reasonable value for lastLevel.  With OpenGL we have no
 
317
    * idea how many mipmap levels will be in a texture until we start
 
318
    * to render with it.  Make an educated guess here but be prepared
 
319
    * to re-allocating a texture buffer with space for more (or fewer)
 
320
    * mipmap levels later.
284
321
    */
285
322
   if ((stObj->base.MinFilter == GL_NEAREST ||
286
323
        stObj->base.MinFilter == GL_LINEAR ||
287
324
        stImage->base._BaseFormat == GL_DEPTH_COMPONENT ||
288
325
        stImage->base._BaseFormat == GL_DEPTH_STENCIL_EXT) &&
289
326
       !stObj->base.GenerateMipmap &&
290
 
       stImage->level == firstLevel) {
 
327
       stImage->level == 0) {
291
328
      /* only alloc space for a single mipmap level */
292
 
      lastLevel = firstLevel;
 
329
      lastLevel = 0;
293
330
   }
294
331
   else {
295
332
      /* alloc space for a full mipmap */
296
333
      GLuint l2width = util_logbase2(width);
297
334
      GLuint l2height = util_logbase2(height);
298
335
      GLuint l2depth = util_logbase2(depth);
299
 
      lastLevel = firstLevel + MAX2(MAX2(l2width, l2height), l2depth);
 
336
      lastLevel = MAX2(MAX2(l2width, l2height), l2depth);
300
337
   }
301
338
 
 
339
   /* Save the level=0 dimensions */
 
340
   stObj->width0 = width;
 
341
   stObj->height0 = height;
 
342
   stObj->depth0 = depth;
 
343
 
302
344
   fmt = st_mesa_format_to_pipe_format(stImage->base.TexFormat);
303
345
 
304
 
   usage = default_usage(fmt);
 
346
   bindings = default_bindings(st, fmt);
305
347
 
306
348
   stObj->pt = st_texture_create(st,
307
349
                                 gl_target_to_pipe(stObj->base.Target),
310
352
                                 width,
311
353
                                 height,
312
354
                                 depth,
313
 
                                 usage);
314
 
 
315
 
   DBG("%s - success\n", __FUNCTION__);
 
355
                                 bindings);
 
356
 
 
357
   DBG("%s returning %d\n", __FUNCTION__, (stObj->pt != NULL));
 
358
 
 
359
   return stObj->pt != NULL;
316
360
}
317
361
 
318
362
 
371
415
{
372
416
   const GLuint dstImageOffsets[1] = {0};
373
417
   struct st_texture_image *stImage = st_texture_image(texImage);
374
 
   struct pipe_screen *screen = ctx->st->pipe->screen;
 
418
   struct st_context *st = st_context(ctx);
 
419
   struct pipe_context *pipe = st->pipe;
 
420
   struct pipe_screen *screen = pipe->screen;
375
421
   gl_format mesa_format;
376
 
   struct pipe_texture templ;
377
 
   struct pipe_texture *src_tex;
 
422
   struct pipe_resource templ;
 
423
   struct pipe_resource *src_tex;
 
424
   struct pipe_sampler_view view_templ;
 
425
   struct pipe_sampler_view *src_view;
378
426
   struct pipe_surface *dst_surface;
379
427
   struct pipe_transfer *tex_xfer;
380
428
   void *map;
387
435
   /* get destination surface (in the compressed texture) */
388
436
   dst_surface = screen->get_tex_surface(screen, stImage->pt,
389
437
                                         stImage->face, stImage->level, 0,
390
 
                                         PIPE_BUFFER_USAGE_GPU_WRITE);
 
438
                                         0 /* flags */);
391
439
   if (!dst_surface) {
392
440
      /* can't render into this format (or other problem) */
393
441
      return GL_FALSE;
403
451
   /* Create the temporary source texture
404
452
    */
405
453
   memset(&templ, 0, sizeof(templ));
406
 
   templ.target = PIPE_TEXTURE_2D;
 
454
   templ.target = st->internal_target;
407
455
   templ.format = st_mesa_format_to_pipe_format(mesa_format);
408
456
   templ.width0 = width;
409
457
   templ.height0 = height;
410
458
   templ.depth0 = 1;
411
459
   templ.last_level = 0;
412
 
   templ.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER;
413
 
   src_tex = screen->texture_create(screen, &templ);
 
460
   templ.usage = PIPE_USAGE_DEFAULT;
 
461
   templ.bind = PIPE_BIND_SAMPLER_VIEW;
 
462
   src_tex = screen->resource_create(screen, &templ);
414
463
 
415
464
   if (!src_tex)
416
465
      return GL_FALSE;
417
466
 
418
467
   /* Put user's tex data into the temporary texture
419
468
    */
420
 
   tex_xfer = st_cond_flush_get_tex_transfer(st_context(ctx), src_tex,
 
469
   tex_xfer = pipe_get_transfer(st_context(ctx)->pipe, src_tex,
421
470
                                             0, 0, 0, /* face, level are zero */
422
471
                                             PIPE_TRANSFER_WRITE,
423
472
                                             0, 0, width, height); /* x, y, w, h */
424
 
   map = screen->transfer_map(screen, tex_xfer);
 
473
   map = pipe_transfer_map(pipe, tex_xfer);
425
474
 
426
475
   _mesa_texstore(ctx, 2, GL_RGBA, mesa_format,
427
476
                  map,              /* dest ptr */
433
482
                  pixels,           /* source data */
434
483
                  unpack);          /* source data packing */
435
484
 
436
 
   screen->transfer_unmap(screen, tex_xfer);
437
 
   screen->tex_transfer_destroy(tex_xfer);
 
485
   pipe_transfer_unmap(pipe, tex_xfer);
 
486
   pipe->transfer_destroy(pipe, tex_xfer);
 
487
 
 
488
   /* Create temporary sampler view */
 
489
   u_sampler_view_default_template(&view_templ,
 
490
                                   src_tex,
 
491
                                   src_tex->format);
 
492
   src_view = pipe->create_sampler_view(pipe, src_tex, &view_templ);
 
493
 
438
494
 
439
495
   /* copy / compress image */
440
 
   util_blit_pixels_tex(ctx->st->blit,
441
 
                        src_tex,          /* pipe_texture (src) */
 
496
   util_blit_pixels_tex(st->blit,
 
497
                        src_view,         /* sampler view (src) */
442
498
                        0, 0,             /* src x0, y0 */
443
499
                        width, height,    /* src x1, y1 */
444
500
                        dst_surface,      /* pipe_surface (dst) */
449
505
                        PIPE_TEX_MIPFILTER_NEAREST);
450
506
 
451
507
   pipe_surface_reference(&dst_surface, NULL);
452
 
   pipe_texture_reference(&src_tex, NULL);
 
508
   pipe_resource_reference(&src_tex, NULL);
 
509
   pipe_sampler_view_reference(&src_view, NULL);
453
510
 
454
511
   return GL_TRUE;
455
512
}
471
528
            struct gl_texture_image *texImage,
472
529
            GLsizei imageSize, GLboolean compressed_src)
473
530
{
474
 
   struct pipe_screen *screen = ctx->st->pipe->screen;
 
531
   struct st_context *st = st_context(ctx);
 
532
   struct pipe_screen *screen = st->pipe->screen;
475
533
   struct st_texture_object *stObj = st_texture_object(texObj);
476
534
   struct st_texture_image *stImage = st_texture_image(texImage);
477
 
   GLint postConvWidth, postConvHeight;
478
 
   GLint texelBytes, sizeInBytes;
479
535
   GLuint dstRowStride = 0;
480
536
   struct gl_pixelstore_attrib unpackNB;
481
537
   enum pipe_transfer_usage transfer_usage = 0;
483
539
   DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__,
484
540
       _mesa_lookup_enum_by_nr(target), level, width, height, depth, border);
485
541
 
 
542
   /* The Mesa/Gallium state tracker does not implement the imaging extensions
 
543
    * such as convolution.
 
544
    */
 
545
   assert(!ctx->Extensions.ARB_imaging);
 
546
   assert(!ctx->Extensions.EXT_convolution);
 
547
 
486
548
   /* switch to "normal" */
487
549
   if (stObj->surface_based) {
488
550
      _mesa_clear_texture_object(ctx, texObj);
 
551
      pipe_resource_reference(&stObj->pt, NULL);
 
552
 
 
553
      /* oops, need to init this image again */
 
554
      _mesa_init_teximage_fields(ctx, target, texImage,
 
555
            width, height, depth, border, internalFormat);
 
556
      _mesa_choose_texture_format(ctx, texObj, texImage, target, level,
 
557
            internalFormat, format, type);
 
558
 
489
559
      stObj->surface_based = GL_FALSE;
490
560
   }
491
561
 
499
569
      texImage->Border = 0;
500
570
      border = 0;
501
571
   }
502
 
 
503
 
   postConvWidth = width;
504
 
   postConvHeight = height;
 
572
   else {
 
573
      assert(texImage->Width == width);
 
574
      assert(texImage->Height == height);
 
575
      assert(texImage->Depth == depth);
 
576
   }
505
577
 
506
578
   stImage->face = _mesa_tex_target_to_face(target);
507
579
   stImage->level = level;
508
580
 
509
 
#if FEATURE_convolve
510
 
   if (ctx->_ImageTransferState & IMAGE_CONVOLUTION_BIT) {
511
 
      _mesa_adjust_image_for_convolution(ctx, dims, &postConvWidth,
512
 
                                         &postConvHeight);
513
 
   }
514
 
#endif
515
 
 
516
581
   _mesa_set_fetch_functions(texImage, dims);
517
582
 
518
 
   if (_mesa_is_format_compressed(texImage->TexFormat)) {
519
 
      /* must be a compressed format */
520
 
      texelBytes = 0;
521
 
   }
522
 
   else {
523
 
      texelBytes = _mesa_get_format_bytes(texImage->TexFormat);
524
 
      
525
 
      /* Minimum pitch of 32 bytes */
526
 
      if (postConvWidth * texelBytes < 32) {
527
 
         postConvWidth = 32 / texelBytes;
528
 
         texImage->RowStride = postConvWidth;
529
 
      }
530
 
      
531
 
      /* we'll set RowStride elsewhere when the texture is a "mapped" state */
532
 
      /*assert(texImage->RowStride == postConvWidth);*/
533
 
   }
534
 
 
535
583
   /* Release the reference to a potentially orphaned buffer.   
536
584
    * Release any old malloced memory.
537
585
    */
538
586
   if (stImage->pt) {
539
 
      pipe_texture_reference(&stImage->pt, NULL);
 
587
      pipe_resource_reference(&stImage->pt, NULL);
540
588
      assert(!texImage->Data);
541
589
   }
542
590
   else if (texImage->Data) {
548
596
    * mipmap.  If so, free the old mipmap.
549
597
    */
550
598
   if (stObj->pt) {
551
 
      if (stObj->teximage_realloc ||
552
 
          level > (GLint) stObj->pt->last_level ||
 
599
      if (level > (GLint) stObj->pt->last_level ||
553
600
          !st_texture_match_image(stObj->pt, &stImage->base,
554
601
                                  stImage->face, stImage->level)) {
555
602
         DBG("release it\n");
556
 
         pipe_texture_reference(&stObj->pt, NULL);
 
603
         pipe_resource_reference(&stObj->pt, NULL);
557
604
         assert(!stObj->pt);
558
 
         stObj->teximage_realloc = FALSE;
 
605
         pipe_sampler_view_reference(&stObj->sampler_view, NULL);
559
606
      }
560
607
   }
561
608
 
565
612
   }
566
613
 
567
614
   if (!stObj->pt) {
568
 
      guess_and_alloc_texture(ctx->st, stObj, stImage);
569
 
      if (!stObj->pt) {
 
615
      if (!guess_and_alloc_texture(st, stObj, stImage)) {
570
616
         /* Probably out of memory.
571
617
          * Try flushing any pending rendering, then retry.
572
618
          */
573
 
         st_finish(ctx->st);
574
 
         guess_and_alloc_texture(ctx->st, stObj, stImage);
575
 
         if (!stObj->pt) {
 
619
         st_finish(st);
 
620
         if (!guess_and_alloc_texture(st, stObj, stImage)) {
576
621
            _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
577
622
            return;
578
623
         }
581
626
 
582
627
   assert(!stImage->pt);
583
628
 
 
629
   /* Check if this texture image can live inside the texture object's buffer.
 
630
    * If so, store the image there.  Otherwise the image will temporarily live
 
631
    * in its own buffer.
 
632
    */
584
633
   if (stObj->pt &&
585
634
       st_texture_match_image(stObj->pt, &stImage->base,
586
 
                                 stImage->face, stImage->level)) {
 
635
                              stImage->face, stImage->level)) {
587
636
 
588
 
      pipe_texture_reference(&stImage->pt, stObj->pt);
 
637
      pipe_resource_reference(&stImage->pt, stObj->pt);
589
638
      assert(stImage->pt);
590
639
   }
591
640
 
592
641
   if (!stImage->pt)
593
642
      DBG("XXX: Image did not fit into texture - storing in local memory!\n");
594
643
 
595
 
   /* st_CopyTexImage calls this function with pixels == NULL, with
596
 
    * the expectation that the texture will be set up but nothing
597
 
    * more will be done.  This is where those calls return:
 
644
   /* Pixel data may come from regular user memory or a PBO.  For the later,
 
645
    * do bounds checking and map the PBO to read pixels data from it.
 
646
    *
 
647
    * XXX we should try to use a GPU-accelerated path to copy the image data
 
648
    * from the PBO to the texture.
598
649
    */
599
650
   if (compressed_src) {
600
651
      pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels,
607
658
                                           pixels, unpack, "glTexImage");
608
659
   }
609
660
 
610
 
   /* Note: we can't check for pixels==NULL until after we've allocated
611
 
    * memory for the texture.
612
 
    */
613
 
 
614
661
   /* See if we can do texture compression with a blit/render.
615
662
    */
616
663
   if (!compressed_src &&
618
665
       _mesa_is_format_compressed(texImage->TexFormat) &&
619
666
       screen->is_format_supported(screen,
620
667
                                   stImage->pt->format,
621
 
                                   stImage->pt->target,
622
 
                                   PIPE_TEXTURE_USAGE_RENDER_TARGET, 0)) {
 
668
                                   stImage->pt->target, 0,
 
669
                                   PIPE_BIND_RENDER_TARGET, 0)) {
623
670
      if (!pixels)
624
671
         goto done;
625
672
 
629
676
      }
630
677
   }
631
678
 
 
679
   /*
 
680
    * Prepare to store the texture data.  Either map the gallium texture buffer
 
681
    * memory or malloc space for it.
 
682
    */
632
683
   if (stImage->pt) {
 
684
      /* Store the image in the gallium texture memory buffer */
633
685
      if (format == GL_DEPTH_COMPONENT &&
634
686
          util_format_is_depth_and_stencil(stImage->pt->format))
635
687
         transfer_usage = PIPE_TRANSFER_READ_WRITE;
636
688
      else
637
689
         transfer_usage = PIPE_TRANSFER_WRITE;
638
690
 
639
 
      texImage->Data = st_texture_image_map(ctx->st, stImage, 0,
640
 
                                            transfer_usage, 0, 0,
641
 
                                            stImage->base.Width,
642
 
                                            stImage->base.Height);
 
691
      texImage->Data = st_texture_image_map(st, stImage, 0,
 
692
                                            transfer_usage, 0, 0, width, height);
643
693
      if(stImage->transfer)
644
694
         dstRowStride = stImage->transfer->stride;
645
695
   }
646
696
   else {
647
697
      /* Allocate regular memory and store the image there temporarily.   */
648
 
      if (_mesa_is_format_compressed(texImage->TexFormat)) {
649
 
         sizeInBytes = _mesa_format_image_size(texImage->TexFormat,
650
 
                                               texImage->Width,
651
 
                                               texImage->Height,
652
 
                                               texImage->Depth);
653
 
         dstRowStride = _mesa_format_row_stride(texImage->TexFormat, width);
654
 
         assert(dims != 3);
655
 
      }
656
 
      else {
657
 
         dstRowStride = postConvWidth * texelBytes;
658
 
         sizeInBytes = depth * dstRowStride * postConvHeight;
659
 
      }
 
698
      GLuint imageSize = _mesa_format_image_size(texImage->TexFormat,
 
699
                                                 width, height, depth);
 
700
      dstRowStride = _mesa_format_row_stride(texImage->TexFormat, width);
660
701
 
661
 
      texImage->Data = _mesa_align_malloc(sizeInBytes, 16);
 
702
      texImage->Data = _mesa_align_malloc(imageSize, 16);
662
703
   }
663
704
 
664
705
   if (!texImage->Data) {
666
707
      return;
667
708
   }
668
709
 
669
 
   if (!pixels)
 
710
   if (!pixels) {
 
711
      /* We've allocated texture memory, but have no pixel data - all done. */
670
712
      goto done;
 
713
   }
671
714
 
672
715
   DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
673
 
       width, height, depth, width * texelBytes, dstRowStride);
 
716
       width, height, depth, width, dstRowStride);
674
717
 
675
 
   /* Copy data.  Would like to know when it's ok for us to eg. use
676
 
    * the blitter to copy.  Or, use the hardware to do the format
677
 
    * conversion and copy:
 
718
   /* Copy user texture image into the texture buffer.
678
719
    */
679
720
   if (compressed_src) {
680
 
      const GLuint srcImageStride = _mesa_format_row_stride(texImage->TexFormat, width);
681
 
      if(dstRowStride == srcImageStride)
 
721
      const GLuint srcRowStride =
 
722
         _mesa_format_row_stride(texImage->TexFormat, width);
 
723
      if (dstRowStride == srcRowStride) {
682
724
         memcpy(texImage->Data, pixels, imageSize);
683
 
      else
684
 
      {
 
725
      }
 
726
      else {
685
727
         char *dst = texImage->Data;
686
728
         const char *src = pixels;
687
729
         GLuint i, bw, bh, lines;
688
730
         _mesa_get_format_block_size(texImage->TexFormat, &bw, &bh);
689
731
         lines = (height + bh - 1) / bh;
690
732
 
691
 
         for(i = 0; i < lines; ++i)
692
 
         {
693
 
            memcpy(dst, src, srcImageStride);
 
733
         for (i = 0; i < lines; ++i) {
 
734
            memcpy(dst, src, srcRowStride);
694
735
            dst += dstRowStride;
695
 
            src += srcImageStride;
 
736
            src += srcRowStride;
696
737
         }
697
738
      }
698
739
   }
717
758
 
718
759
         if (stImage->pt && i + 1 < depth) {
719
760
            /* unmap this slice */
720
 
            st_texture_image_unmap(ctx->st, stImage);
 
761
            st_texture_image_unmap(st, stImage);
721
762
            /* map next slice of 3D texture */
722
 
            texImage->Data = st_texture_image_map(ctx->st, stImage, i + 1,
 
763
            texImage->Data = st_texture_image_map(st, stImage, i + 1,
723
764
                                                  transfer_usage, 0, 0,
724
 
                                                  stImage->base.Width,
725
 
                                                  stImage->base.Height);
 
765
                                                  width, height);
726
766
            src += srcImageStride;
727
767
         }
728
768
      }
732
772
   _mesa_unmap_teximage_pbo(ctx, unpack);
733
773
 
734
774
   if (stImage->pt && texImage->Data) {
735
 
      st_texture_image_unmap(ctx->st, stImage);
 
775
      st_texture_image_unmap(st, stImage);
736
776
      texImage->Data = NULL;
737
777
   }
738
778
}
809
849
                     struct gl_texture_object *texObj,
810
850
                     struct gl_texture_image *texImage)
811
851
{
812
 
   struct pipe_screen *screen = ctx->st->pipe->screen;
 
852
   struct st_context *st = st_context(ctx);
 
853
   struct pipe_context *pipe = st->pipe;
 
854
   struct pipe_screen *screen = pipe->screen;
813
855
   struct st_texture_image *stImage = st_texture_image(texImage);
 
856
   struct st_texture_object *stObj = st_texture_object(texObj);
 
857
   struct pipe_sampler_view *src_view =
 
858
      st_get_texture_sampler_view(stObj, pipe);
814
859
   const GLuint width = texImage->Width;
815
860
   const GLuint height = texImage->Height;
816
861
   struct pipe_surface *dst_surface;
817
 
   struct pipe_texture *dst_texture;
 
862
   struct pipe_resource *dst_texture;
818
863
   struct pipe_transfer *tex_xfer;
 
864
   unsigned bind = (PIPE_BIND_RENDER_TARGET | /* util_blit may choose to render */
 
865
                    PIPE_BIND_TRANSFER_READ);
819
866
 
820
867
   /* create temp / dest surface */
821
 
   if (!util_create_rgba_surface(screen, width, height,
 
868
   if (!util_create_rgba_surface(screen, width, height, bind,
822
869
                                 &dst_texture, &dst_surface)) {
823
870
      _mesa_problem(ctx, "util_create_rgba_surface() failed "
824
871
                    "in decompress_with_blit()");
826
873
   }
827
874
 
828
875
   /* blit/render/decompress */
829
 
   util_blit_pixels_tex(ctx->st->blit,
830
 
                        stImage->pt,      /* pipe_texture (src) */
 
876
   util_blit_pixels_tex(st->blit,
 
877
                        src_view,      /* pipe_resource (src) */
831
878
                        0, 0,             /* src x0, y0 */
832
879
                        width, height,    /* src x1, y1 */
833
880
                        dst_surface,      /* pipe_surface (dst) */
837
884
                        PIPE_TEX_MIPFILTER_NEAREST);
838
885
 
839
886
   /* map the dst_surface so we can read from it */
840
 
   tex_xfer = st_cond_flush_get_tex_transfer(st_context(ctx),
 
887
   tex_xfer = pipe_get_transfer(st_context(ctx)->pipe,
841
888
                                             dst_texture, 0, 0, 0,
842
889
                                             PIPE_TRANSFER_READ,
843
890
                                             0, 0, width, height);
848
895
   if (st_equal_formats(stImage->pt->format, format, type)) {
849
896
      /* memcpy */
850
897
      const uint bytesPerRow = width * util_format_get_blocksize(stImage->pt->format);
851
 
      ubyte *map = screen->transfer_map(screen, tex_xfer);
 
898
      ubyte *map = pipe_transfer_map(pipe, tex_xfer);
852
899
      GLuint row;
853
900
      for (row = 0; row < height; row++) {
854
901
         GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
856
903
         memcpy(dest, map, bytesPerRow);
857
904
         map += tex_xfer->stride;
858
905
      }
859
 
      screen->transfer_unmap(screen, tex_xfer);
 
906
      pipe_transfer_unmap(pipe, tex_xfer);
860
907
   }
861
908
   else {
862
909
      /* format translation via floats */
871
918
            debug_printf("%s: fallback format translation\n", __FUNCTION__);
872
919
 
873
920
         /* get float[4] rgba row from surface */
874
 
         pipe_get_tile_rgba(tex_xfer, 0, row, width, 1, rgba);
 
921
         pipe_get_tile_rgba(pipe, tex_xfer, 0, row, width, 1, rgba);
875
922
 
876
923
         _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba, format,
877
924
                                    type, dest, &ctx->Pack, transferOps);
880
927
 
881
928
   _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
882
929
 
883
 
   screen->tex_transfer_destroy(tex_xfer);
 
930
   pipe->transfer_destroy(pipe, tex_xfer);
884
931
 
885
932
   /* destroy the temp / dest surface */
886
933
   util_destroy_rgba_surface(dst_texture, dst_surface);
898
945
                 struct gl_texture_object *texObj,
899
946
                 struct gl_texture_image *texImage, GLboolean compressed_dst)
900
947
{
 
948
   struct st_context *st = st_context(ctx);
901
949
   struct st_texture_image *stImage = st_texture_image(texImage);
902
950
   const GLuint dstImageStride =
903
951
      _mesa_image_image_stride(&ctx->Pack, texImage->Width, texImage->Height,
906
954
   GLubyte *dest;
907
955
 
908
956
   if (stImage->pt &&
909
 
       util_format_is_compressed(stImage->pt->format) &&
 
957
       util_format_is_s3tc(stImage->pt->format) &&
910
958
       !compressed_dst) {
911
959
      /* Need to decompress the texture.
912
960
       * We'll do this by rendering a textured quad.
922
970
      /* Image is stored in hardware format in a buffer managed by the
923
971
       * kernel.  Need to explicitly map and unmap it.
924
972
       */
925
 
      unsigned face = _mesa_tex_target_to_face(target);
926
 
 
927
 
      st_teximage_flush_before_map(ctx->st, stImage->pt, face, level,
928
 
                                   PIPE_TRANSFER_READ);
929
 
 
930
 
      texImage->Data = st_texture_image_map(ctx->st, stImage, 0,
 
973
      texImage->Data = st_texture_image_map(st, stImage, 0,
931
974
                                            PIPE_TRANSFER_READ, 0, 0,
932
975
                                            stImage->base.Width,
933
976
                                            stImage->base.Height);
953
996
 
954
997
   dest = (GLubyte *) pixels;
955
998
 
 
999
   _mesa_set_fetch_functions(texImage, get_texture_dims(target));
 
1000
 
956
1001
   for (i = 0; i < depth; i++) {
957
1002
      if (compressed_dst) {
958
1003
         _mesa_get_compressed_teximage(ctx, target, level, dest,
965
1010
 
966
1011
      if (stImage->pt && i + 1 < depth) {
967
1012
         /* unmap this slice */
968
 
         st_texture_image_unmap(ctx->st, stImage);
 
1013
         st_texture_image_unmap(st, stImage);
969
1014
         /* map next slice of 3D texture */
970
 
         texImage->Data = st_texture_image_map(ctx->st, stImage, i + 1,
 
1015
         texImage->Data = st_texture_image_map(st, stImage, i + 1,
971
1016
                                               PIPE_TRANSFER_READ, 0, 0,
972
1017
                                               stImage->base.Width,
973
1018
                                               stImage->base.Height);
979
1024
 
980
1025
   /* Unmap */
981
1026
   if (stImage->pt) {
982
 
      st_texture_image_unmap(ctx->st, stImage);
 
1027
      st_texture_image_unmap(st, stImage);
983
1028
      texImage->Data = NULL;
984
1029
   }
985
1030
}
1017
1062
               struct gl_texture_object *texObj,
1018
1063
               struct gl_texture_image *texImage)
1019
1064
{
1020
 
   struct pipe_screen *screen = ctx->st->pipe->screen;
 
1065
   struct st_context *st = st_context(ctx);
 
1066
   struct pipe_screen *screen = st->pipe->screen;
1021
1067
   struct st_texture_image *stImage = st_texture_image(texImage);
1022
1068
   GLuint dstRowStride;
1023
1069
   const GLuint srcImageStride =
1043
1089
       _mesa_is_format_compressed(texImage->TexFormat) &&
1044
1090
       screen->is_format_supported(screen,
1045
1091
                                   stImage->pt->format,
1046
 
                                   stImage->pt->target,
1047
 
                                   PIPE_TEXTURE_USAGE_RENDER_TARGET, 0)) {
 
1092
                                   stImage->pt->target, 0,
 
1093
                                   PIPE_BIND_RENDER_TARGET, 0)) {
1048
1094
      if (compress_with_blit(ctx, target, level,
1049
1095
                             xoffset, yoffset, zoffset,
1050
1096
                             width, height, depth,
1057
1103
    * from uploading the buffer under us.
1058
1104
    */
1059
1105
   if (stImage->pt) {
1060
 
      unsigned face = _mesa_tex_target_to_face(target);
1061
 
 
1062
1106
      if (format == GL_DEPTH_COMPONENT &&
1063
1107
          util_format_is_depth_and_stencil(stImage->pt->format))
1064
1108
         transfer_usage = PIPE_TRANSFER_READ_WRITE;
1065
1109
      else
1066
1110
         transfer_usage = PIPE_TRANSFER_WRITE;
1067
1111
 
1068
 
      st_teximage_flush_before_map(ctx->st, stImage->pt, face, level,
1069
 
                                   transfer_usage);
1070
 
      texImage->Data = st_texture_image_map(ctx->st, stImage, zoffset, 
 
1112
      texImage->Data = st_texture_image_map(st, stImage, zoffset, 
1071
1113
                                            transfer_usage,
1072
1114
                                            xoffset, yoffset,
1073
1115
                                            width, height);
1095
1137
 
1096
1138
      if (stImage->pt && i + 1 < depth) {
1097
1139
         /* unmap this slice */
1098
 
         st_texture_image_unmap(ctx->st, stImage);
 
1140
         st_texture_image_unmap(st, stImage);
1099
1141
         /* map next slice of 3D texture */
1100
 
         texImage->Data = st_texture_image_map(ctx->st, stImage,
 
1142
         texImage->Data = st_texture_image_map(st, stImage,
1101
1143
                                               zoffset + i + 1,
1102
1144
                                               transfer_usage,
1103
1145
                                               xoffset, yoffset,
1110
1152
   _mesa_unmap_teximage_pbo(ctx, packing);
1111
1153
 
1112
1154
   if (stImage->pt && texImage->Data) {
1113
 
      st_texture_image_unmap(ctx->st, stImage);
 
1155
      st_texture_image_unmap(st, stImage);
1114
1156
      texImage->Data = NULL;
1115
1157
   }
1116
1158
}
1181
1223
                           struct gl_texture_object *texObj,
1182
1224
                           struct gl_texture_image *texImage)
1183
1225
{
 
1226
   struct st_context *st = st_context(ctx);
1184
1227
   struct st_texture_image *stImage = st_texture_image(texImage);
1185
1228
   int srcBlockStride;
1186
1229
   int dstBlockStride;
1187
1230
   int y;
1188
 
   enum pipe_format pformat= stImage->pt->format;
 
1231
   enum pipe_format pformat;
1189
1232
 
1190
1233
   if (stImage->pt) {
1191
 
      unsigned face = _mesa_tex_target_to_face(target);
 
1234
      pformat = stImage->pt->format;
1192
1235
 
1193
 
      st_teximage_flush_before_map(ctx->st, stImage->pt, face, level,
1194
 
                                   PIPE_TRANSFER_WRITE);
1195
 
      texImage->Data = st_texture_image_map(ctx->st, stImage, 0, 
 
1236
      texImage->Data = st_texture_image_map(st, stImage, 0, 
1196
1237
                                            PIPE_TRANSFER_WRITE,
1197
1238
                                            xoffset, yoffset,
1198
1239
                                            width, height);
1213
1254
 
1214
1255
   assert(xoffset % util_format_get_blockwidth(pformat) == 0);
1215
1256
   assert(yoffset % util_format_get_blockheight(pformat) == 0);
1216
 
   assert(width % util_format_get_blockwidth(pformat) == 0);
1217
 
   assert(height % util_format_get_blockheight(pformat) == 0);
1218
1257
 
1219
1258
   for (y = 0; y < height; y += util_format_get_blockheight(pformat)) {
1220
1259
      /* don't need to adjust for xoffset and yoffset as st_texture_image_map does that */
1224
1263
   }
1225
1264
 
1226
1265
   if (stImage->pt) {
1227
 
      st_texture_image_unmap(ctx->st, stImage);
 
1266
      st_texture_image_unmap(st, stImage);
1228
1267
      texImage->Data = NULL;
1229
1268
   }
1230
1269
}
1260
1299
                          GLint srcX, GLint srcY,
1261
1300
                          GLsizei width, GLsizei height)
1262
1301
{
1263
 
   struct pipe_context *pipe = ctx->st->pipe;
1264
 
   struct pipe_screen *screen = pipe->screen;
 
1302
   struct st_context *st = st_context(ctx);
 
1303
   struct pipe_context *pipe = st->pipe;
1265
1304
   struct pipe_transfer *src_trans;
1266
1305
   GLvoid *texDest;
1267
1306
   enum pipe_transfer_usage transfer_usage;
1275
1314
      srcY = strb->Base.Height - srcY - height;
1276
1315
   }
1277
1316
 
1278
 
   src_trans = st_cond_flush_get_tex_transfer( st_context(ctx),
 
1317
   src_trans = pipe_get_transfer(st_context(ctx)->pipe,
1279
1318
                                               strb->texture,
1280
1319
                                               0, 0, 0,
1281
1320
                                               PIPE_TRANSFER_READ,
1289
1328
   else
1290
1329
      transfer_usage = PIPE_TRANSFER_WRITE;
1291
1330
 
1292
 
   st_teximage_flush_before_map(ctx->st, stImage->pt, 0, 0,
1293
 
                                transfer_usage);
1294
 
 
1295
 
   texDest = st_texture_image_map(ctx->st, stImage, 0, transfer_usage,
 
1331
   texDest = st_texture_image_map(st, stImage, 0, transfer_usage,
1296
1332
                                  destX, destY, width, height);
1297
1333
 
1298
1334
   if (baseFormat == GL_DEPTH_COMPONENT ||
1314
1350
      /* To avoid a large temp memory allocation, do copy row by row */
1315
1351
      for (row = 0; row < height; row++, srcY += yStep) {
1316
1352
         uint data[MAX_WIDTH];
1317
 
         pipe_get_tile_z(src_trans, 0, srcY, width, 1, data);
 
1353
         pipe_get_tile_z(pipe, src_trans, 0, srcY, width, 1, data);
1318
1354
         if (scaleOrBias) {
1319
1355
            _mesa_scale_and_bias_depth_uint(ctx, width, data);
1320
1356
         }
1321
 
         pipe_put_tile_z(stImage->transfer, 0, row, width, 1, data);
 
1357
         pipe_put_tile_z(pipe, stImage->transfer, 0, row, width, 1, data);
1322
1358
      }
1323
1359
   }
1324
1360
   else {
1340
1376
         /* XXX this usually involves a lot of int/float conversion.
1341
1377
          * try to avoid that someday.
1342
1378
          */
1343
 
         pipe_get_tile_rgba(src_trans, 0, 0, width, height, tempSrc);
 
1379
         pipe_get_tile_rgba(pipe, src_trans, 0, 0, width, height, tempSrc);
1344
1380
 
1345
1381
         /* Store into texture memory.
1346
1382
          * Note that this does some special things such as pixel transfer
1367
1403
         free(tempSrc);
1368
1404
   }
1369
1405
 
1370
 
   st_texture_image_unmap(ctx->st, stImage);
1371
 
   screen->tex_transfer_destroy(src_trans);
 
1406
   st_texture_image_unmap(st, stImage);
 
1407
   pipe->transfer_destroy(pipe, src_trans);
1372
1408
}
1373
1409
 
1374
1410
 
1463
1499
   const GLenum texBaseFormat = texImage->_BaseFormat;
1464
1500
   struct gl_framebuffer *fb = ctx->ReadBuffer;
1465
1501
   struct st_renderbuffer *strb;
1466
 
   struct pipe_context *pipe = ctx->st->pipe;
 
1502
   struct st_context *st = st_context(ctx);
 
1503
   struct pipe_context *pipe = st->pipe;
1467
1504
   struct pipe_screen *screen = pipe->screen;
1468
1505
   enum pipe_format dest_format, src_format;
1469
1506
   GLboolean use_fallback = GL_TRUE;
1470
1507
   GLboolean matching_base_formats;
1471
 
   GLuint format_writemask;
 
1508
   GLuint format_writemask, sample_count;
1472
1509
   struct pipe_surface *dest_surface = NULL;
1473
1510
   GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
1474
1511
 
1475
 
   /* any rendering in progress must flushed before we grab the fb image */
1476
 
   st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
1477
 
 
1478
1512
   /* make sure finalize_textures has been called? 
1479
1513
    */
1480
 
   if (0) st_validate_state(ctx->st);
 
1514
   if (0) st_validate_state(st);
1481
1515
 
1482
1516
   /* determine if copying depth or color data */
1483
1517
   if (texBaseFormat == GL_DEPTH_COMPONENT ||
1494
1528
      return;
1495
1529
   }
1496
1530
 
 
1531
   sample_count = strb->surface->texture->nr_samples;
 
1532
   /* I believe this would be legal, presumably would need to do a resolve
 
1533
      for color, and for depth/stencil spec says to just use one of the
 
1534
      depth/stencil samples per pixel? Need some transfer clarifications. */
 
1535
   assert(sample_count < 2);
 
1536
 
1497
1537
   if (srcX < 0) {
1498
1538
      width -= -srcX;
1499
1539
      destX += -srcX;
1544
1584
 
1545
1585
   if (ctx->_ImageTransferState == 0x0) {
1546
1586
 
1547
 
      if (pipe->surface_copy &&
1548
 
          matching_base_formats &&
 
1587
      if (matching_base_formats &&
1549
1588
          src_format == dest_format &&
1550
1589
          !do_flip) 
1551
1590
      {
1552
1591
         /* use surface_copy() / blit */
1553
 
 
1554
 
         dest_surface = screen->get_tex_surface(screen, stImage->pt,
1555
 
                                                stImage->face, stImage->level,
1556
 
                                                destZ,
1557
 
                                                PIPE_BUFFER_USAGE_GPU_WRITE);
1558
 
 
1559
 
         /* for surface_copy(), y=0=top, always */
1560
 
         pipe->surface_copy(pipe,
1561
 
                            /* dest */
1562
 
                            dest_surface,
1563
 
                            destX, destY,
1564
 
                            /* src */
1565
 
                            strb->surface,
1566
 
                            srcX, srcY,
1567
 
                            /* size */
1568
 
                            width, height);
 
1592
         struct pipe_subresource subdst, subsrc;
 
1593
         subdst.face = stImage->face;
 
1594
         subdst.level = stImage->level;
 
1595
         subsrc.face = strb->surface->face;
 
1596
         subsrc.level = strb->surface->level;
 
1597
 
 
1598
         /* for resource_copy_region(), y=0=top, always */
 
1599
         pipe->resource_copy_region(pipe,
 
1600
                                    /* dest */
 
1601
                                    stImage->pt,
 
1602
                                    subdst,
 
1603
                                    destX, destY, destZ,
 
1604
                                    /* src */
 
1605
                                    strb->texture,
 
1606
                                    subsrc,
 
1607
                                    srcX, srcY, strb->surface->zslice,
 
1608
                                    /* size */
 
1609
                                    width, height);
1569
1610
         use_fallback = GL_FALSE;
1570
1611
      }
1571
1612
      else if (format_writemask &&
1572
1613
               texBaseFormat != GL_DEPTH_COMPONENT &&
1573
1614
               texBaseFormat != GL_DEPTH_STENCIL &&
1574
1615
               screen->is_format_supported(screen, src_format,
1575
 
                                           PIPE_TEXTURE_2D, 
1576
 
                                           PIPE_TEXTURE_USAGE_SAMPLER,
 
1616
                                           PIPE_TEXTURE_2D, sample_count,
 
1617
                                           PIPE_BIND_SAMPLER_VIEW,
1577
1618
                                           0) &&
1578
1619
               screen->is_format_supported(screen, dest_format,
1579
 
                                           PIPE_TEXTURE_2D, 
1580
 
                                           PIPE_TEXTURE_USAGE_RENDER_TARGET,
 
1620
                                           PIPE_TEXTURE_2D, 0,
 
1621
                                           PIPE_BIND_RENDER_TARGET,
1581
1622
                                           0)) {
1582
1623
         /* draw textured quad to do the copy */
1583
1624
         GLint srcY0, srcY1;
 
1625
         struct pipe_subresource subsrc;
1584
1626
 
1585
1627
         dest_surface = screen->get_tex_surface(screen, stImage->pt,
1586
1628
                                                stImage->face, stImage->level,
1587
1629
                                                destZ,
1588
 
                                                PIPE_BUFFER_USAGE_GPU_WRITE);
 
1630
                                                PIPE_BIND_RENDER_TARGET);
1589
1631
 
1590
1632
         if (do_flip) {
1591
1633
            srcY1 = strb->Base.Height - srcY - height;
1595
1637
            srcY0 = srcY;
1596
1638
            srcY1 = srcY0 + height;
1597
1639
         }
1598
 
         util_blit_pixels_writemask(ctx->st->blit,
1599
 
                                    strb->surface,
 
1640
         subsrc.face = strb->surface->face;
 
1641
         subsrc.level = strb->surface->level;
 
1642
 
 
1643
         util_blit_pixels_writemask(st->blit,
 
1644
                                    strb->texture,
 
1645
                                    subsrc,
1600
1646
                                    srcX, srcY0,
1601
1647
                                    srcX + width, srcY1,
 
1648
                                    strb->surface->zslice,
1602
1649
                                    dest_surface,
1603
1650
                                    destX, destY,
1604
1651
                                    destX + width, destY + height,
1710
1757
}
1711
1758
 
1712
1759
 
 
1760
/**
 
1761
 * Copy image data from stImage into the texture object 'stObj' at level
 
1762
 * 'dstLevel'.
 
1763
 */
1713
1764
static void
1714
1765
copy_image_data_to_texture(struct st_context *st,
1715
1766
                           struct st_texture_object *stObj,
1716
1767
                           GLuint dstLevel,
1717
1768
                           struct st_texture_image *stImage)
1718
1769
{
 
1770
   /* debug checks */
 
1771
   {
 
1772
      const struct gl_texture_image *dstImage =
 
1773
         stObj->base.Image[stImage->face][stImage->level];
 
1774
      assert(dstImage);
 
1775
      assert(dstImage->Width == stImage->base.Width);
 
1776
      assert(dstImage->Height == stImage->base.Height);
 
1777
      assert(dstImage->Depth == stImage->base.Depth);
 
1778
   }
 
1779
 
1719
1780
   if (stImage->pt) {
1720
1781
      /* Copy potentially with the blitter:
1721
1782
       */
1722
1783
      st_texture_image_copy(st->pipe,
1723
1784
                            stObj->pt, dstLevel,  /* dest texture, level */
1724
 
                            stImage->pt, /* src texture */
 
1785
                            stImage->pt, stImage->level, /* src texture, level */
1725
1786
                            stImage->face);
1726
1787
 
1727
 
      pipe_texture_reference(&stImage->pt, NULL);
 
1788
      pipe_resource_reference(&stImage->pt, NULL);
1728
1789
   }
1729
1790
   else if (stImage->base.Data) {
1730
 
      /* More straightforward upload.  
1731
 
       */
1732
 
      st_teximage_flush_before_map(st, stObj->pt, stImage->face, dstLevel,
1733
 
                                   PIPE_TRANSFER_WRITE);
1734
 
 
1735
1791
      st_texture_image_data(st,
1736
1792
                            stObj->pt,
1737
1793
                            stImage->face,
1746
1802
      stImage->base.Data = NULL;
1747
1803
   }
1748
1804
 
1749
 
   pipe_texture_reference(&stImage->pt, stObj->pt);
 
1805
   pipe_resource_reference(&stImage->pt, stObj->pt);
1750
1806
}
1751
1807
 
1752
1808
 
1758
1814
GLboolean
1759
1815
st_finalize_texture(GLcontext *ctx,
1760
1816
                    struct pipe_context *pipe,
1761
 
                    struct gl_texture_object *tObj,
1762
 
                    GLboolean *needFlush)
 
1817
                    struct gl_texture_object *tObj)
1763
1818
{
 
1819
   struct st_context *st = st_context(ctx);
1764
1820
   struct st_texture_object *stObj = st_texture_object(tObj);
1765
1821
   const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1766
 
   GLuint blockSize, face;
 
1822
   GLuint face;
1767
1823
   struct st_texture_image *firstImage;
1768
 
 
1769
 
   *needFlush = GL_FALSE;
 
1824
   enum pipe_format firstImageFormat;
1770
1825
 
1771
1826
   if (stObj->base._Complete) {
1772
1827
      /* The texture is complete and we know exactly how many mipmap levels
1779
1834
          stObj->base.MinFilter == GL_NEAREST)
1780
1835
         stObj->lastLevel = stObj->base.BaseLevel;
1781
1836
      else
1782
 
         stObj->lastLevel = stObj->base._MaxLevel - stObj->base.BaseLevel;
 
1837
         stObj->lastLevel = stObj->base._MaxLevel;
1783
1838
   }
1784
1839
 
1785
1840
   firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
 
1841
   assert(firstImage);
1786
1842
 
1787
1843
   /* If both firstImage and stObj point to a texture which can contain
1788
1844
    * all active images, favour firstImage.  Note that because of the
1792
1848
   if (firstImage->pt &&
1793
1849
       firstImage->pt != stObj->pt &&
1794
1850
       firstImage->pt->last_level >= stObj->lastLevel) {
1795
 
      pipe_texture_reference(&stObj->pt, firstImage->pt);
 
1851
      pipe_resource_reference(&stObj->pt, firstImage->pt);
 
1852
      pipe_sampler_view_reference(&stObj->sampler_view, NULL);
1796
1853
   }
1797
1854
 
1798
 
   /* bytes per pixel block (blocks are usually 1x1) */
1799
 
   blockSize = _mesa_get_format_bytes(firstImage->base.TexFormat);
 
1855
   /* Find gallium format for the Mesa texture */
 
1856
   firstImageFormat = st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
1800
1857
 
1801
1858
   /* If we already have a gallium texture, check that it matches the texture
1802
1859
    * object's format, target, size, num_levels, etc.
1803
1860
    */
1804
1861
   if (stObj->pt) {
1805
 
      const enum pipe_format fmt =
1806
 
         st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
1807
1862
      if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
1808
 
          stObj->pt->format != fmt ||
 
1863
          !st_sampler_compat_formats(stObj->pt->format, firstImageFormat) ||
1809
1864
          stObj->pt->last_level < stObj->lastLevel ||
1810
 
          stObj->pt->width0 != firstImage->base.Width2 ||
1811
 
          stObj->pt->height0 != firstImage->base.Height2 ||
1812
 
          stObj->pt->depth0 != firstImage->base.Depth2)
 
1865
          stObj->pt->width0 != stObj->width0 ||
 
1866
          stObj->pt->height0 != stObj->height0 ||
 
1867
          stObj->pt->depth0 != stObj->depth0)
1813
1868
      {
1814
 
         pipe_texture_reference(&stObj->pt, NULL);
1815
 
         ctx->st->dirty.st |= ST_NEW_FRAMEBUFFER;
 
1869
         /* The gallium texture does not match the Mesa texture so delete the
 
1870
          * gallium texture now.  We'll make a new one below.
 
1871
          */
 
1872
         pipe_resource_reference(&stObj->pt, NULL);
 
1873
         pipe_sampler_view_reference(&stObj->sampler_view, NULL);
 
1874
         st->dirty.st |= ST_NEW_FRAMEBUFFER;
1816
1875
      }
1817
1876
   }
1818
1877
 
1819
1878
   /* May need to create a new gallium texture:
1820
1879
    */
1821
1880
   if (!stObj->pt) {
1822
 
      const enum pipe_format fmt =
1823
 
         st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
1824
 
      GLuint usage = default_usage(fmt);
 
1881
      GLuint bindings = default_bindings(st, firstImageFormat);
1825
1882
 
1826
 
      stObj->pt = st_texture_create(ctx->st,
 
1883
      stObj->pt = st_texture_create(st,
1827
1884
                                    gl_target_to_pipe(stObj->base.Target),
1828
 
                                    fmt,
 
1885
                                    firstImageFormat,
1829
1886
                                    stObj->lastLevel,
1830
 
                                    firstImage->base.Width2,
1831
 
                                    firstImage->base.Height2,
1832
 
                                    firstImage->base.Depth2,
1833
 
                                    usage);
 
1887
                                    stObj->width0,
 
1888
                                    stObj->height0,
 
1889
                                    stObj->depth0,
 
1890
                                    bindings);
1834
1891
 
1835
1892
      if (!stObj->pt) {
1836
1893
         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
1842
1899
    */
1843
1900
   for (face = 0; face < nr_faces; face++) {
1844
1901
      GLuint level;
1845
 
      for (level = 0; level <= stObj->lastLevel; level++) {
 
1902
      for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) {
1846
1903
         struct st_texture_image *stImage =
1847
 
            st_texture_image(stObj->base.Image[face][stObj->base.BaseLevel + level]);
 
1904
            st_texture_image(stObj->base.Image[face][level]);
1848
1905
 
1849
1906
         /* Need to import images in main memory or held in other textures.
1850
1907
          */
1851
1908
         if (stImage && stObj->pt != stImage->pt) {
1852
 
            copy_image_data_to_texture(ctx->st, stObj, level, stImage);
1853
 
            *needFlush = GL_TRUE;
 
1909
            copy_image_data_to_texture(st, stObj, level, stImage);
1854
1910
         }
1855
1911
      }
1856
1912
   }