Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5564 serge 1
/**************************************************************************
2
 *
3
 * Copyright 2009 VMware, Inc.  All Rights Reserved.
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * copy of this software and associated documentation files (the
7
 * "Software"), to deal in the Software without restriction, including
8
 * without limitation the rights to use, copy, modify, merge, publish,
9
 * distribute, sub license, and/or sell copies of the Software, and to
10
 * permit persons to whom the Software is furnished to do so, subject to
11
 * the following conditions:
12
 *
13
 * The above copyright notice and this permission notice (including the
14
 * next paragraph) shall be included in all copies or substantial portions
15
 * of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
20
 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
 *
25
 **************************************************************************/
26
 
27
/**
28
 * @file
29
 * Surface utility functions.
30
 *
31
 * @author Brian Paul
32
 */
33
 
34
 
35
#include "pipe/p_defines.h"
36
#include "pipe/p_screen.h"
37
#include "pipe/p_state.h"
38
 
39
#include "util/u_format.h"
40
#include "util/u_inlines.h"
41
#include "util/u_rect.h"
42
#include "util/u_surface.h"
43
#include "util/u_pack_color.h"
44
 
45
 
46
/**
47
 * Initialize a pipe_surface object.  'view' is considered to have
48
 * uninitialized contents.
49
 */
50
void
51
u_surface_default_template(struct pipe_surface *surf,
52
                           const struct pipe_resource *texture)
53
{
54
   memset(surf, 0, sizeof(*surf));
55
 
56
   surf->format = texture->format;
57
}
58
 
59
 
60
/**
61
 * Copy 2D rect from one place to another.
62
 * Position and sizes are in pixels.
63
 * src_stride may be negative to do vertical flip of pixels from source.
64
 */
65
void
66
util_copy_rect(ubyte * dst,
67
               enum pipe_format format,
68
               unsigned dst_stride,
69
               unsigned dst_x,
70
               unsigned dst_y,
71
               unsigned width,
72
               unsigned height,
73
               const ubyte * src,
74
               int src_stride,
75
               unsigned src_x,
76
               unsigned src_y)
77
{
78
   unsigned i;
79
   int src_stride_pos = src_stride < 0 ? -src_stride : src_stride;
80
   int blocksize = util_format_get_blocksize(format);
81
   int blockwidth = util_format_get_blockwidth(format);
82
   int blockheight = util_format_get_blockheight(format);
83
 
84
   assert(blocksize > 0);
85
   assert(blockwidth > 0);
86
   assert(blockheight > 0);
87
 
88
   dst_x /= blockwidth;
89
   dst_y /= blockheight;
90
   width = (width + blockwidth - 1)/blockwidth;
91
   height = (height + blockheight - 1)/blockheight;
92
   src_x /= blockwidth;
93
   src_y /= blockheight;
94
 
95
   dst += dst_x * blocksize;
96
   src += src_x * blocksize;
97
   dst += dst_y * dst_stride;
98
   src += src_y * src_stride_pos;
99
   width *= blocksize;
100
 
101
   if (width == dst_stride && width == src_stride)
102
      memcpy(dst, src, height * width);
103
   else {
104
      for (i = 0; i < height; i++) {
105
         memcpy(dst, src, width);
106
         dst += dst_stride;
107
         src += src_stride;
108
      }
109
   }
110
}
111
 
112
 
113
/**
114
 * Copy 3D box from one place to another.
115
 * Position and sizes are in pixels.
116
 */
117
void
118
util_copy_box(ubyte * dst,
119
              enum pipe_format format,
120
              unsigned dst_stride, unsigned dst_slice_stride,
121
              unsigned dst_x, unsigned dst_y, unsigned dst_z,
122
              unsigned width, unsigned height, unsigned depth,
123
              const ubyte * src,
124
              int src_stride, unsigned src_slice_stride,
125
              unsigned src_x, unsigned src_y, unsigned src_z)
126
{
127
   unsigned z;
128
   dst += dst_z * dst_slice_stride;
129
   src += src_z * src_slice_stride;
130
   for (z = 0; z < depth; ++z) {
131
      util_copy_rect(dst,
132
                     format,
133
                     dst_stride,
134
                     dst_x, dst_y,
135
                     width, height,
136
                     src,
137
                     src_stride,
138
                     src_x, src_y);
139
 
140
      dst += dst_slice_stride;
141
      src += src_slice_stride;
142
   }
143
}
144
 
145
 
146
void
147
util_fill_rect(ubyte * dst,
148
               enum pipe_format format,
149
               unsigned dst_stride,
150
               unsigned dst_x,
151
               unsigned dst_y,
152
               unsigned width,
153
               unsigned height,
154
               union util_color *uc)
155
{
156
   const struct util_format_description *desc = util_format_description(format);
157
   unsigned i, j;
158
   unsigned width_size;
159
   int blocksize = desc->block.bits / 8;
160
   int blockwidth = desc->block.width;
161
   int blockheight = desc->block.height;
162
 
163
   assert(blocksize > 0);
164
   assert(blockwidth > 0);
165
   assert(blockheight > 0);
166
 
167
   dst_x /= blockwidth;
168
   dst_y /= blockheight;
169
   width = (width + blockwidth - 1)/blockwidth;
170
   height = (height + blockheight - 1)/blockheight;
171
 
172
   dst += dst_x * blocksize;
173
   dst += dst_y * dst_stride;
174
   width_size = width * blocksize;
175
 
176
   switch (blocksize) {
177
   case 1:
178
      if(dst_stride == width_size)
179
         memset(dst, uc->ub, height * width_size);
180
      else {
181
         for (i = 0; i < height; i++) {
182
            memset(dst, uc->ub, width_size);
183
            dst += dst_stride;
184
         }
185
      }
186
      break;
187
   case 2:
188
      for (i = 0; i < height; i++) {
189
         uint16_t *row = (uint16_t *)dst;
190
         for (j = 0; j < width; j++)
191
            *row++ = uc->us;
192
         dst += dst_stride;
193
      }
194
      break;
195
   case 4:
196
      for (i = 0; i < height; i++) {
197
         uint32_t *row = (uint32_t *)dst;
198
         for (j = 0; j < width; j++)
199
            *row++ = uc->ui[0];
200
         dst += dst_stride;
201
      }
202
      break;
203
   default:
204
      for (i = 0; i < height; i++) {
205
         ubyte *row = dst;
206
         for (j = 0; j < width; j++) {
207
            memcpy(row, uc, blocksize);
208
            row += blocksize;
209
         }
210
         dst += dst_stride;
211
      }
212
      break;
213
   }
214
}
215
 
216
 
217
void
218
util_fill_box(ubyte * dst,
219
              enum pipe_format format,
220
              unsigned stride,
221
              unsigned layer_stride,
222
              unsigned x,
223
              unsigned y,
224
              unsigned z,
225
              unsigned width,
226
              unsigned height,
227
              unsigned depth,
228
              union util_color *uc)
229
{
230
   unsigned layer;
231
   dst += z * layer_stride;
232
   for (layer = z; layer < depth; layer++) {
233
      util_fill_rect(dst, format,
234
                     stride,
235
                     x, y, width, height, uc);
236
      dst += layer_stride;
237
   }
238
}
239
 
240
 
241
/**
242
 * Fallback function for pipe->resource_copy_region().
243
 * Note: (X,Y)=(0,0) is always the upper-left corner.
244
 */
245
void
246
util_resource_copy_region(struct pipe_context *pipe,
247
                          struct pipe_resource *dst,
248
                          unsigned dst_level,
249
                          unsigned dst_x, unsigned dst_y, unsigned dst_z,
250
                          struct pipe_resource *src,
251
                          unsigned src_level,
252
                          const struct pipe_box *src_box)
253
{
254
   struct pipe_transfer *src_trans, *dst_trans;
255
   uint8_t *dst_map;
256
   const uint8_t *src_map;
257
   enum pipe_format src_format, dst_format;
258
   struct pipe_box dst_box;
259
 
260
   assert(src && dst);
261
   if (!src || !dst)
262
      return;
263
 
264
   assert((src->target == PIPE_BUFFER && dst->target == PIPE_BUFFER) ||
265
          (src->target != PIPE_BUFFER && dst->target != PIPE_BUFFER));
266
 
267
   src_format = src->format;
268
   dst_format = dst->format;
269
 
270
   assert(util_format_get_blocksize(dst_format) == util_format_get_blocksize(src_format));
271
   assert(util_format_get_blockwidth(dst_format) == util_format_get_blockwidth(src_format));
272
   assert(util_format_get_blockheight(dst_format) == util_format_get_blockheight(src_format));
273
 
274
   src_map = pipe->transfer_map(pipe,
275
                                src,
276
                                src_level,
277
                                PIPE_TRANSFER_READ,
278
                                src_box, &src_trans);
279
   assert(src_map);
280
   if (!src_map) {
281
      goto no_src_map;
282
   }
283
 
284
   dst_box.x = dst_x;
285
   dst_box.y = dst_y;
286
   dst_box.z = dst_z;
287
   dst_box.width  = src_box->width;
288
   dst_box.height = src_box->height;
289
   dst_box.depth  = src_box->depth;
290
 
291
   dst_map = pipe->transfer_map(pipe,
292
                                dst,
293
                                dst_level,
294
                                PIPE_TRANSFER_WRITE | PIPE_TRANSFER_DISCARD_RANGE,
295
                                &dst_box, &dst_trans);
296
   assert(dst_map);
297
   if (!dst_map) {
298
      goto no_dst_map;
299
   }
300
 
301
   if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
302
      assert(src_box->height == 1);
303
      assert(src_box->depth == 1);
304
      memcpy(dst_map, src_map, src_box->width);
305
   } else {
306
      util_copy_box(dst_map,
307
                    dst_format,
308
                    dst_trans->stride, dst_trans->layer_stride,
309
                    0, 0, 0,
310
                    src_box->width, src_box->height, src_box->depth,
311
                    src_map,
312
                    src_trans->stride, src_trans->layer_stride,
313
                    0, 0, 0);
314
   }
315
 
316
   pipe->transfer_unmap(pipe, dst_trans);
317
no_dst_map:
318
   pipe->transfer_unmap(pipe, src_trans);
319
no_src_map:
320
   ;
321
}
322
 
323
 
324
 
325
#define UBYTE_TO_USHORT(B) ((B) | ((B) << 8))
326
 
327
 
328
/**
329
 * Fallback for pipe->clear_render_target() function.
330
 * XXX this looks too hackish to be really useful.
331
 * cpp > 4 looks like a gross hack at best...
332
 * Plus can't use these transfer fallbacks when clearing
333
 * multisampled surfaces for instance.
334
 * Clears all bound layers.
335
 */
336
void
337
util_clear_render_target(struct pipe_context *pipe,
338
                         struct pipe_surface *dst,
339
                         const union pipe_color_union *color,
340
                         unsigned dstx, unsigned dsty,
341
                         unsigned width, unsigned height)
342
{
343
   struct pipe_transfer *dst_trans;
344
   ubyte *dst_map;
345
   union util_color uc;
346
   unsigned max_layer;
347
 
348
   assert(dst->texture);
349
   if (!dst->texture)
350
      return;
351
 
352
   if (dst->texture->target == PIPE_BUFFER) {
353
      /*
354
       * The fill naturally works on the surface format, however
355
       * the transfer uses resource format which is just bytes for buffers.
356
       */
357
      unsigned dx, w;
358
      unsigned pixstride = util_format_get_blocksize(dst->format);
359
      dx = (dst->u.buf.first_element + dstx) * pixstride;
360
      w = width * pixstride;
361
      max_layer = 0;
362
      dst_map = pipe_transfer_map(pipe,
363
                                  dst->texture,
364
                                  0, 0,
365
                                  PIPE_TRANSFER_WRITE,
366
                                  dx, 0, w, 1,
367
                                  &dst_trans);
368
   }
369
   else {
370
      max_layer = dst->u.tex.last_layer - dst->u.tex.first_layer;
371
      dst_map = pipe_transfer_map_3d(pipe,
372
                                     dst->texture,
373
                                     dst->u.tex.level,
374
                                     PIPE_TRANSFER_WRITE,
375
                                     dstx, dsty, dst->u.tex.first_layer,
376
                                     width, height, max_layer + 1, &dst_trans);
377
   }
378
 
379
   assert(dst_map);
380
 
381
   if (dst_map) {
382
      enum pipe_format format = dst->format;
383
      assert(dst_trans->stride > 0);
384
 
385
      if (util_format_is_pure_integer(format)) {
386
         /*
387
          * We expect int/uint clear values here, though some APIs
388
          * might disagree (but in any case util_pack_color()
389
          * couldn't handle it)...
390
          */
391
         if (util_format_is_pure_sint(format)) {
392
            util_format_write_4i(format, color->i, 0, &uc, 0, 0, 0, 1, 1);
393
         }
394
         else {
395
            assert(util_format_is_pure_uint(format));
396
            util_format_write_4ui(format, color->ui, 0, &uc, 0, 0, 0, 1, 1);
397
         }
398
      }
399
      else {
400
         util_pack_color(color->f, dst->format, &uc);
401
      }
402
 
403
      util_fill_box(dst_map, dst->format,
404
                    dst_trans->stride, dst_trans->layer_stride,
405
                    0, 0, 0, width, height, max_layer + 1, &uc);
406
 
407
      pipe->transfer_unmap(pipe, dst_trans);
408
   }
409
}
410
 
411
/**
412
 * Fallback for pipe->clear_stencil() function.
413
 * sw fallback doesn't look terribly useful here.
414
 * Plus can't use these transfer fallbacks when clearing
415
 * multisampled surfaces for instance.
416
 * Clears all bound layers.
417
 */
418
void
419
util_clear_depth_stencil(struct pipe_context *pipe,
420
                         struct pipe_surface *dst,
421
                         unsigned clear_flags,
422
                         double depth,
423
                         unsigned stencil,
424
                         unsigned dstx, unsigned dsty,
425
                         unsigned width, unsigned height)
426
{
427
   enum pipe_format format = dst->format;
428
   struct pipe_transfer *dst_trans;
429
   ubyte *dst_map;
430
   boolean need_rmw = FALSE;
431
   unsigned max_layer, layer;
432
 
433
   if ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) &&
434
       ((clear_flags & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL) &&
435
       util_format_is_depth_and_stencil(format))
436
      need_rmw = TRUE;
437
 
438
   assert(dst->texture);
439
   if (!dst->texture)
440
      return;
441
 
442
   max_layer = dst->u.tex.last_layer - dst->u.tex.first_layer;
443
   dst_map = pipe_transfer_map_3d(pipe,
444
                                  dst->texture,
445
                                  dst->u.tex.level,
446
                                  (need_rmw ? PIPE_TRANSFER_READ_WRITE :
447
                                              PIPE_TRANSFER_WRITE),
448
                                  dstx, dsty, dst->u.tex.first_layer,
449
                                  width, height, max_layer + 1, &dst_trans);
450
   assert(dst_map);
451
 
452
   if (dst_map) {
453
      unsigned dst_stride = dst_trans->stride;
454
      uint64_t zstencil = util_pack64_z_stencil(format, depth, stencil);
455
      ubyte *dst_layer = dst_map;
456
      unsigned i, j;
457
      assert(dst_trans->stride > 0);
458
 
459
      for (layer = 0; layer <= max_layer; layer++) {
460
         dst_map = dst_layer;
461
 
462
         switch (util_format_get_blocksize(format)) {
463
         case 1:
464
            assert(format == PIPE_FORMAT_S8_UINT);
465
            if(dst_stride == width)
466
               memset(dst_map, (uint8_t) zstencil, height * width);
467
            else {
468
               for (i = 0; i < height; i++) {
469
                  memset(dst_map, (uint8_t) zstencil, width);
470
                  dst_map += dst_stride;
471
               }
472
            }
473
            break;
474
         case 2:
475
            assert(format == PIPE_FORMAT_Z16_UNORM);
476
            for (i = 0; i < height; i++) {
477
               uint16_t *row = (uint16_t *)dst_map;
478
               for (j = 0; j < width; j++)
479
                  *row++ = (uint16_t) zstencil;
480
               dst_map += dst_stride;
481
               }
482
            break;
483
         case 4:
484
            if (!need_rmw) {
485
               for (i = 0; i < height; i++) {
486
                  uint32_t *row = (uint32_t *)dst_map;
487
                  for (j = 0; j < width; j++)
488
                     *row++ = (uint32_t) zstencil;
489
                  dst_map += dst_stride;
490
               }
491
            }
492
            else {
493
               uint32_t dst_mask;
494
               if (format == PIPE_FORMAT_Z24_UNORM_S8_UINT)
495
                  dst_mask = 0x00ffffff;
496
               else {
497
                  assert(format == PIPE_FORMAT_S8_UINT_Z24_UNORM);
498
                  dst_mask = 0xffffff00;
499
               }
500
               if (clear_flags & PIPE_CLEAR_DEPTH)
501
                  dst_mask = ~dst_mask;
502
               for (i = 0; i < height; i++) {
503
                  uint32_t *row = (uint32_t *)dst_map;
504
                  for (j = 0; j < width; j++) {
505
                     uint32_t tmp = *row & dst_mask;
506
                     *row++ = tmp | ((uint32_t) zstencil & ~dst_mask);
507
                  }
508
                  dst_map += dst_stride;
509
               }
510
            }
511
            break;
512
         case 8:
513
            if (!need_rmw) {
514
               for (i = 0; i < height; i++) {
515
                  uint64_t *row = (uint64_t *)dst_map;
516
                  for (j = 0; j < width; j++)
517
                     *row++ = zstencil;
518
                  dst_map += dst_stride;
519
               }
520
            }
521
            else {
522
               uint64_t src_mask;
523
 
524
               if (clear_flags & PIPE_CLEAR_DEPTH)
525
                  src_mask = 0x00000000ffffffffull;
526
               else
527
                  src_mask = 0x000000ff00000000ull;
528
 
529
               for (i = 0; i < height; i++) {
530
                  uint64_t *row = (uint64_t *)dst_map;
531
                  for (j = 0; j < width; j++) {
532
                     uint64_t tmp = *row & ~src_mask;
533
                     *row++ = tmp | (zstencil & src_mask);
534
                  }
535
                  dst_map += dst_stride;
536
               }
537
            }
538
            break;
539
         default:
540
            assert(0);
541
            break;
542
         }
543
         dst_layer += dst_trans->layer_stride;
544
      }
545
 
546
      pipe->transfer_unmap(pipe, dst_trans);
547
   }
548
}
549
 
550
 
551
/* Return if the box is totally inside the resource.
552
 */
553
static boolean
554
is_box_inside_resource(const struct pipe_resource *res,
555
                       const struct pipe_box *box,
556
                       unsigned level)
557
{
558
   unsigned width = 1, height = 1, depth = 1;
559
 
560
   switch (res->target) {
561
   case PIPE_BUFFER:
562
      width = res->width0;
563
      height = 1;
564
      depth = 1;
565
      break;
566
   case PIPE_TEXTURE_1D:
567
      width = u_minify(res->width0, level);
568
      height = 1;
569
      depth = 1;
570
      break;
571
   case PIPE_TEXTURE_2D:
572
   case PIPE_TEXTURE_RECT:
573
      width = u_minify(res->width0, level);
574
      height = u_minify(res->height0, level);
575
      depth = 1;
576
      break;
577
   case PIPE_TEXTURE_3D:
578
      width = u_minify(res->width0, level);
579
      height = u_minify(res->height0, level);
580
      depth = u_minify(res->depth0, level);
581
      break;
582
   case PIPE_TEXTURE_CUBE:
583
      width = u_minify(res->width0, level);
584
      height = u_minify(res->height0, level);
585
      depth = 6;
586
      break;
587
   case PIPE_TEXTURE_1D_ARRAY:
588
      width = u_minify(res->width0, level);
589
      height = 1;
590
      depth = res->array_size;
591
      break;
592
   case PIPE_TEXTURE_2D_ARRAY:
593
      width = u_minify(res->width0, level);
594
      height = u_minify(res->height0, level);
595
      depth = res->array_size;
596
      break;
597
   case PIPE_TEXTURE_CUBE_ARRAY:
598
      width = u_minify(res->width0, level);
599
      height = u_minify(res->height0, level);
600
      depth = res->array_size;
601
      assert(res->array_size % 6 == 0);
602
      break;
603
   case PIPE_MAX_TEXTURE_TYPES:;
604
   }
605
 
606
   return box->x >= 0 &&
607
          box->x + box->width <= (int) width &&
608
          box->y >= 0 &&
609
          box->y + box->height <= (int) height &&
610
          box->z >= 0 &&
611
          box->z + box->depth <= (int) depth;
612
}
613
 
614
static unsigned
615
get_sample_count(const struct pipe_resource *res)
616
{
617
   return res->nr_samples ? res->nr_samples : 1;
618
}
619
 
620
/**
621
 * Try to do a blit using resource_copy_region. The function calls
622
 * resource_copy_region if the blit description is compatible with it.
623
 *
624
 * It returns TRUE if the blit was done using resource_copy_region.
625
 *
626
 * It returns FALSE otherwise and the caller must fall back to a more generic
627
 * codepath for the blit operation. (e.g. by using u_blitter)
628
 */
629
boolean
630
util_try_blit_via_copy_region(struct pipe_context *ctx,
631
                              const struct pipe_blit_info *blit)
632
{
633
   unsigned mask = util_format_get_mask(blit->dst.format);
634
 
635
   /* No format conversions. */
636
   if (blit->src.resource->format != blit->src.format ||
637
       blit->dst.resource->format != blit->dst.format ||
638
       !util_is_format_compatible(
639
          util_format_description(blit->src.resource->format),
640
          util_format_description(blit->dst.resource->format))) {
641
      return FALSE;
642
   }
643
 
644
   /* No masks, no filtering, no scissor. */
645
   if ((blit->mask & mask) != mask ||
646
       blit->filter != PIPE_TEX_FILTER_NEAREST ||
647
       blit->scissor_enable) {
648
      return FALSE;
649
   }
650
 
651
   /* No flipping. */
652
   if (blit->src.box.width < 0 ||
653
       blit->src.box.height < 0 ||
654
       blit->src.box.depth < 0) {
655
      return FALSE;
656
   }
657
 
658
   /* No scaling. */
659
   if (blit->src.box.width != blit->dst.box.width ||
660
       blit->src.box.height != blit->dst.box.height ||
661
       blit->src.box.depth != blit->dst.box.depth) {
662
      return FALSE;
663
   }
664
 
665
   /* No out-of-bounds access. */
666
   if (!is_box_inside_resource(blit->src.resource, &blit->src.box,
667
                               blit->src.level) ||
668
       !is_box_inside_resource(blit->dst.resource, &blit->dst.box,
669
                               blit->dst.level)) {
670
      return FALSE;
671
   }
672
 
673
   /* Sample counts must match. */
674
   if (get_sample_count(blit->src.resource) !=
675
       get_sample_count(blit->dst.resource)) {
676
      return FALSE;
677
   }
678
 
679
   ctx->resource_copy_region(ctx, blit->dst.resource, blit->dst.level,
680
                             blit->dst.box.x, blit->dst.box.y, blit->dst.box.z,
681
                             blit->src.resource, blit->src.level,
682
                             &blit->src.box);
683
   return TRUE;
684
}