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