1/*
2 * Copyright (c) 2012-2015 Etnaviv Project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sub license,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 *    Wladimir J. van der Laan <laanwj@gmail.com>
25 */
26
27#include "etnaviv_tiling.h"
28
29#include <stdint.h>
30#include <stdio.h>
31
32#define TEX_TILE_WIDTH (4)
33#define TEX_TILE_HEIGHT (4)
34#define TEX_TILE_WORDS (TEX_TILE_WIDTH * TEX_TILE_HEIGHT)
35
36#define DO_TILE(type)                                                   \
37   src_stride /= sizeof(type);                                          \
38   dst_stride = (dst_stride * TEX_TILE_HEIGHT) / sizeof(type);          \
39   for (unsigned srcy = 0; srcy < height; ++srcy) {                     \
40      unsigned dsty = basey + srcy;                                     \
41      unsigned ty = (dsty / TEX_TILE_HEIGHT) * dst_stride +             \
42                    (dsty % TEX_TILE_HEIGHT) * TEX_TILE_WIDTH;          \
43      for (unsigned srcx = 0; srcx < width; ++srcx) {                   \
44         unsigned dstx = basex + srcx;                                  \
45         ((type *)dest)[ty + (dstx / TEX_TILE_WIDTH) * TEX_TILE_WORDS + \
46                        (dstx % TEX_TILE_WIDTH)] =                      \
47            ((type *)src)[srcy * src_stride + srcx];                    \
48      }                                                                 \
49   }
50
51#define DO_UNTILE(type)                                                   \
52   src_stride = (src_stride * TEX_TILE_HEIGHT) / sizeof(type);            \
53   dst_stride /= sizeof(type);                                            \
54   for (unsigned dsty = 0; dsty < height; ++dsty) {                       \
55      unsigned srcy = basey + dsty;                                       \
56      unsigned sy = (srcy / TEX_TILE_HEIGHT) * src_stride +               \
57                    (srcy % TEX_TILE_HEIGHT) * TEX_TILE_WIDTH;            \
58      for (unsigned dstx = 0; dstx < width; ++dstx) {                     \
59         unsigned srcx = basex + dstx;                                    \
60         ((type *)dest)[dsty * dst_stride + dstx] =                       \
61            ((type *)src)[sy + (srcx / TEX_TILE_WIDTH) * TEX_TILE_WORDS + \
62                          (srcx % TEX_TILE_WIDTH)];                       \
63      }                                                                   \
64   }
65
66void
67etna_texture_tile(void *dest, void *src, unsigned basex, unsigned basey,
68                  unsigned dst_stride, unsigned width, unsigned height,
69                  unsigned src_stride, unsigned elmtsize)
70{
71   if (elmtsize == 8) {
72      DO_TILE(uint64_t)
73   } else if (elmtsize == 4) {
74      DO_TILE(uint32_t)
75   } else if (elmtsize == 2) {
76      DO_TILE(uint16_t)
77   } else if (elmtsize == 1) {
78      DO_TILE(uint8_t)
79   } else {
80      printf("etna_texture_tile: unhandled element size %i\n", elmtsize);
81   }
82}
83
84void
85etna_texture_untile(void *dest, void *src, unsigned basex, unsigned basey,
86                    unsigned src_stride, unsigned width, unsigned height,
87                    unsigned dst_stride, unsigned elmtsize)
88{
89   if (elmtsize == 8) {
90      DO_UNTILE(uint64_t)
91   } else if (elmtsize == 4) {
92      DO_UNTILE(uint32_t);
93   } else if (elmtsize == 2) {
94      DO_UNTILE(uint16_t);
95   } else if (elmtsize == 1) {
96      DO_UNTILE(uint8_t);
97   } else {
98      printf("etna_texture_tile: unhandled element size %i\n", elmtsize);
99   }
100}
101