summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/ilo/ilo_transfer.c
blob: 30751d32b0fdb373da052074ac8f45f0dcbe09f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/*
 * Mesa 3-D graphics library
 *
 * Copyright (C) 2012-2013 LunarG, Inc.
 *
 * 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 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 AUTHORS OR COPYRIGHT HOLDERS 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:
 *    Chia-I Wu <olv@lunarg.com>
 */

#include "util/u_surface.h"
#include "util/u_transfer.h"
#include "util/u_format_etc.h"

#include "ilo_cp.h"
#include "ilo_context.h"
#include "ilo_resource.h"
#include "ilo_transfer.h"

enum ilo_transfer_map_method {
   ILO_TRANSFER_MAP_DIRECT,
   ILO_TRANSFER_MAP_STAGING_SYS,
};

struct ilo_transfer {
   struct pipe_transfer base;

   enum ilo_transfer_map_method method;
   void *ptr;

   void *staging_sys;
};

static inline struct ilo_transfer *
ilo_transfer(struct pipe_transfer *transfer)
{
   return (struct ilo_transfer *) transfer;
}

static void
ilo_transfer_inline_write(struct pipe_context *pipe,
                          struct pipe_resource *res,
                          unsigned level,
                          unsigned usage,
                          const struct pipe_box *box,
                          const void *data,
                          unsigned stride,
                          unsigned layer_stride)
{
   struct ilo_context *ilo = ilo_context(pipe);
   struct ilo_texture *tex = ilo_texture(res);
   int offset, size;
   bool will_be_busy;

   /*
    * Fall back to map(), memcpy(), and unmap().  We use this path for
    * unsynchronized write, as the buffer is likely to be busy and pwrite()
    * will stall.
    */
   if (unlikely(tex->base.target != PIPE_BUFFER) ||
       (usage & PIPE_TRANSFER_UNSYNCHRONIZED)) {
      u_default_transfer_inline_write(pipe, res,
            level, usage, box, data, stride, layer_stride);

      return;
   }

   /*
    * XXX With hardware context support, the bo may be needed by GPU without
    * being referenced by ilo->cp->bo.  We have to flush unconditionally, and
    * that is bad.
    */
   if (ilo->cp->hw_ctx)
      ilo_cp_flush(ilo->cp);

   will_be_busy = ilo->cp->bo->references(ilo->cp->bo, tex->bo);

   /* see if we can avoid stalling */
   if (will_be_busy || intel_bo_is_busy(tex->bo)) {
      bool will_stall = true;

      if (usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
         /* old data not needed so discard the old bo to avoid stalling */
         if (ilo_texture_alloc_bo(tex))
            will_stall = false;
      }
      else {
         /*
          * We could allocate a temporary bo to hold the data and emit
          * pipelined copy blit to move them to tex->bo.  But for now, do
          * nothing.
          */
      }

      /* flush to make bo busy (so that pwrite() stalls as it should be) */
      if (will_stall && will_be_busy)
         ilo_cp_flush(ilo->cp);
   }

   /* for PIPE_BUFFERs, conversion should not be needed */
   assert(tex->bo_format == tex->base.format);

   /* they should specify just an offset and a size */
   assert(level == 0);
   assert(box->y == 0);
   assert(box->z == 0);
   assert(box->height == 1);
   assert(box->depth == 1);
   offset = box->x;
   size = box->width;

   tex->bo->pwrite(tex->bo, offset, size, data);
}

static void
transfer_unmap_sys_convert(enum pipe_format dst_fmt,
                           const struct pipe_transfer *dst_xfer,
                           void *dst,
                           enum pipe_format src_fmt,
                           const struct pipe_transfer *src_xfer,
                           const void *src)
{
   int i;

   switch (src_fmt) {
   case PIPE_FORMAT_ETC1_RGB8:
      assert(dst_fmt == PIPE_FORMAT_R8G8B8X8_UNORM);

      for (i = 0; i < dst_xfer->box.depth; i++) {
         util_format_etc1_rgb8_unpack_rgba_8unorm(dst,
               dst_xfer->stride, src, src_xfer->stride,
               dst_xfer->box.width, dst_xfer->box.height);

         dst += dst_xfer->layer_stride;
         src += src_xfer->layer_stride;
      }
      break;
   default:
      assert(!"unable to convert the staging data");
      break;
   }
}

static void
transfer_unmap_sys(struct ilo_context *ilo,
                   struct ilo_texture *tex,
                   struct ilo_transfer *xfer)
{
   const void *src = xfer->ptr;
   struct pipe_transfer *dst_xfer;
   void *dst;

   dst = ilo->base.transfer_map(&ilo->base,
         xfer->base.resource, xfer->base.level,
         PIPE_TRANSFER_WRITE |
         PIPE_TRANSFER_MAP_DIRECTLY |
         PIPE_TRANSFER_DISCARD_RANGE,
         &xfer->base.box, &dst_xfer);
   if (!dst_xfer) {
      ilo_err("failed to map resource for moving staging data\n");
      FREE(xfer->staging_sys);
      return;
   }

   if (likely(tex->bo_format != tex->base.format)) {
      transfer_unmap_sys_convert(tex->bo_format, dst_xfer, dst,
            tex->base.format, &xfer->base, src);
   }
   else {
      util_copy_box(dst, tex->bo_format,
            dst_xfer->stride, dst_xfer->layer_stride, 0, 0, 0,
            dst_xfer->box.width, dst_xfer->box.height, dst_xfer->box.depth,
            src, xfer->base.stride, xfer->base.layer_stride, 0, 0, 0);
   }

   ilo->base.transfer_unmap(&ilo->base, dst_xfer);
   FREE(xfer->staging_sys);
}

static bool
transfer_map_sys(struct ilo_context *ilo,
                 struct ilo_texture *tex,
                 struct ilo_transfer *xfer)
{
   const struct pipe_box *box = &xfer->base.box;
   const size_t stride = util_format_get_stride(tex->base.format, box->width);
   const size_t size =
      util_format_get_2d_size(tex->base.format, stride, box->height);
   bool read_back = false;

   if (xfer->base.usage & PIPE_TRANSFER_READ) {
      read_back = true;
   }
   else if (xfer->base.usage & PIPE_TRANSFER_WRITE) {
      const unsigned discard_flags =
         (PIPE_TRANSFER_DISCARD_RANGE | PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE);

      if (!(xfer->base.usage & discard_flags))
         read_back = true;
   }

   /* TODO */
   if (read_back)
      return false;

   xfer->staging_sys = MALLOC(size * box->depth);
   if (!xfer->staging_sys)
      return false;

   xfer->base.stride = stride;
   xfer->base.layer_stride = size;
   xfer->ptr = xfer->staging_sys;

   return true;
}

static void
transfer_unmap_direct(struct ilo_context *ilo,
                      struct ilo_texture *tex,
                      struct ilo_transfer *xfer)
{
   tex->bo->unmap(tex->bo);
}

static bool
transfer_map_direct(struct ilo_context *ilo,
                    struct ilo_texture *tex,
                    struct ilo_transfer *xfer)
{
   int x, y, err;

   if (xfer->base.usage & PIPE_TRANSFER_UNSYNCHRONIZED)
      err = tex->bo->map_unsynchronized(tex->bo);
   /* prefer map() when there is the last-level cache */
   else if (tex->tiling == INTEL_TILING_NONE &&
            (ilo->dev->has_llc || (xfer->base.usage & PIPE_TRANSFER_READ)))
      err = tex->bo->map(tex->bo, (xfer->base.usage & PIPE_TRANSFER_WRITE));
   else
      err = tex->bo->map_gtt(tex->bo);

   if (err)
      return false;

   /* note that stride is for a block row, not a texel row */
   xfer->base.stride = tex->bo_stride;

   /*
    * we can walk through layers when the resource is a texture array or
    * when this is the first level of a 3D texture being mapped
    */
   if (tex->base.array_size > 1 ||
       (tex->base.target == PIPE_TEXTURE_3D && xfer->base.level == 0)) {
      const unsigned qpitch = tex->slice_offsets[xfer->base.level][1].y -
         tex->slice_offsets[xfer->base.level][0].y;

      assert(qpitch % tex->block_height == 0);
      xfer->base.layer_stride = (qpitch / tex->block_height) * xfer->base.stride;
   }
   else {
      xfer->base.layer_stride = 0;
   }

   x = tex->slice_offsets[xfer->base.level][xfer->base.box.z].x;
   y = tex->slice_offsets[xfer->base.level][xfer->base.box.z].y;

   x += xfer->base.box.x;
   y += xfer->base.box.y;

   /* in blocks */
   assert(x % tex->block_width == 0 && y % tex->block_height == 0);
   x /= tex->block_width;
   y /= tex->block_height;

   xfer->ptr = tex->bo->get_virtual(tex->bo);
   xfer->ptr += y * tex->bo_stride + x * tex->bo_cpp;

   return true;
}

/**
 * Choose the best mapping method, depending on the transfer usage and whether
 * the bo is busy.
 */
static bool
transfer_map_choose_method(struct ilo_context *ilo,
                           struct ilo_texture *tex,
                           struct ilo_transfer *xfer)
{
   bool will_be_busy, will_stall;

   /* need to convert on-the-fly */
   if (tex->bo_format != tex->base.format &&
       !(xfer->base.usage & PIPE_TRANSFER_MAP_DIRECTLY)) {
      xfer->method = ILO_TRANSFER_MAP_STAGING_SYS;

      return true;
   }

   xfer->method = ILO_TRANSFER_MAP_DIRECT;

   /* unsynchronized map does not stall */
   if (xfer->base.usage & PIPE_TRANSFER_UNSYNCHRONIZED)
      return true;

   will_be_busy = ilo->cp->bo->references(ilo->cp->bo, tex->bo);
   if (!will_be_busy) {
      /*
       * XXX With hardware context support, the bo may be needed by GPU
       * without being referenced by ilo->cp->bo.  We have to flush
       * unconditionally, and that is bad.
       */
      if (ilo->cp->hw_ctx)
         ilo_cp_flush(ilo->cp);

      if (!intel_bo_is_busy(tex->bo))
         return true;
   }

   /* bo is busy and mapping it will stall */
   will_stall = true;

   if (xfer->base.usage & PIPE_TRANSFER_MAP_DIRECTLY) {
      /* nothing we can do */
   }
   else if (xfer->base.usage & PIPE_TRANSFER_DISCARD_WHOLE_RESOURCE) {
      /* discard old bo and allocate a new one for mapping */
      if (ilo_texture_alloc_bo(tex))
         will_stall = false;
   }
   else if (xfer->base.usage & PIPE_TRANSFER_FLUSH_EXPLICIT) {
      /*
       * We could allocate and return a system buffer here.  When a region of
       * the buffer is explicitly flushed, we pwrite() the region to a
       * temporary bo and emit pipelined copy blit.
       *
       * For now, do nothing.
       */
   }
   else if (xfer->base.usage & PIPE_TRANSFER_DISCARD_RANGE) {
      /*
       * We could allocate a temporary bo for mapping, and emit pipelined copy
       * blit upon unmapping.
       *
       * For now, do nothing.
       */
   }

   if (will_stall) {
      if (xfer->base.usage & PIPE_TRANSFER_DONTBLOCK)
         return false;

      /* flush to make bo busy (so that map() stalls as it should be) */
      if (will_be_busy)
         ilo_cp_flush(ilo->cp);
   }

   return true;
}

static void
ilo_transfer_flush_region(struct pipe_context *pipe,
                          struct pipe_transfer *transfer,
                          const struct pipe_box *box)
{
}

static void
ilo_transfer_unmap(struct pipe_context *pipe,
                   struct pipe_transfer *transfer)
{
   struct ilo_context *ilo = ilo_context(pipe);
   struct ilo_texture *tex = ilo_texture(transfer->resource);
   struct ilo_transfer *xfer = ilo_transfer(transfer);

   switch (xfer->method) {
   case ILO_TRANSFER_MAP_DIRECT:
      transfer_unmap_direct(ilo, tex, xfer);
      break;
   case ILO_TRANSFER_MAP_STAGING_SYS:
      transfer_unmap_sys(ilo, tex, xfer);
      break;
   default:
      assert(!"unknown mapping method");
      break;
   }

   pipe_resource_reference(&xfer->base.resource, NULL);
   FREE(xfer);
}

static void *
ilo_transfer_map(struct pipe_context *pipe,
                 struct pipe_resource *res,
                 unsigned level,
                 unsigned usage,
                 const struct pipe_box *box,
                 struct pipe_transfer **transfer)
{
   struct ilo_context *ilo = ilo_context(pipe);
   struct ilo_texture *tex = ilo_texture(res);
   struct ilo_transfer *xfer;
   int ok;

   xfer = MALLOC_STRUCT(ilo_transfer);
   if (!xfer) {
      *transfer = NULL;
      return NULL;
   }

   xfer->base.resource = NULL;
   pipe_resource_reference(&xfer->base.resource, &tex->base);
   xfer->base.level = level;
   xfer->base.usage = usage;
   xfer->base.box = *box;

   ok = transfer_map_choose_method(ilo, tex, xfer);
   if (ok) {
      switch (xfer->method) {
      case ILO_TRANSFER_MAP_DIRECT:
         ok = transfer_map_direct(ilo, tex, xfer);
         break;
      case ILO_TRANSFER_MAP_STAGING_SYS:
         ok = transfer_map_sys(ilo, tex, xfer);
         break;
      default:
         assert(!"unknown mapping method");
         ok = false;
         break;
      }
   }

   if (!ok) {
      pipe_resource_reference(&xfer->base.resource, NULL);
      FREE(xfer);

      *transfer = NULL;

      return NULL;
   }

   *transfer = &xfer->base;

   return xfer->ptr;
}

/**
 * Initialize transfer-related functions.
 */
void
ilo_init_transfer_functions(struct ilo_context *ilo)
{
   ilo->base.transfer_map = ilo_transfer_map;
   ilo->base.transfer_flush_region = ilo_transfer_flush_region;
   ilo->base.transfer_unmap = ilo_transfer_unmap;
   ilo->base.transfer_inline_write = ilo_transfer_inline_write;
}