17ec681f3Smrg/*
27ec681f3Smrg * Copyright (C) 2018 Rob Clark <robclark@freedesktop.org>
37ec681f3Smrg * Copyright © 2018-2019 Google, Inc.
47ec681f3Smrg *
57ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
67ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
77ec681f3Smrg * to deal in the Software without restriction, including without limitation
87ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
97ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
107ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
117ec681f3Smrg *
127ec681f3Smrg * The above copyright notice and this permission notice (including the next
137ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
147ec681f3Smrg * Software.
157ec681f3Smrg *
167ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
177ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
187ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
197ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
207ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
217ec681f3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
227ec681f3Smrg * SOFTWARE.
237ec681f3Smrg *
247ec681f3Smrg * Authors:
257ec681f3Smrg *    Rob Clark <robclark@freedesktop.org>
267ec681f3Smrg */
277ec681f3Smrg
287ec681f3Smrg#include <stdio.h>
297ec681f3Smrg
307ec681f3Smrg#include "freedreno_layout.h"
317ec681f3Smrg
327ec681f3Smrgvoid
337ec681f3Smrgfdl5_layout(struct fdl_layout *layout, enum pipe_format format,
347ec681f3Smrg            uint32_t nr_samples, uint32_t width0, uint32_t height0,
357ec681f3Smrg            uint32_t depth0, uint32_t mip_levels, uint32_t array_size,
367ec681f3Smrg            bool is_3d)
377ec681f3Smrg{
387ec681f3Smrg   assert(nr_samples > 0);
397ec681f3Smrg   layout->width0 = width0;
407ec681f3Smrg   layout->height0 = height0;
417ec681f3Smrg   layout->depth0 = depth0;
427ec681f3Smrg
437ec681f3Smrg   layout->cpp = util_format_get_blocksize(format);
447ec681f3Smrg   layout->cpp *= nr_samples;
457ec681f3Smrg   layout->cpp_shift = ffs(layout->cpp) - 1;
467ec681f3Smrg
477ec681f3Smrg   layout->format = format;
487ec681f3Smrg   layout->nr_samples = nr_samples;
497ec681f3Smrg   layout->layer_first = !is_3d;
507ec681f3Smrg
517ec681f3Smrg   uint32_t heightalign = layout->cpp == 1 ? 32 : 16;
527ec681f3Smrg   /* in layer_first layout, the level (slice) contains just one
537ec681f3Smrg    * layer (since in fact the layer contains the slices)
547ec681f3Smrg    */
557ec681f3Smrg   uint32_t layers_in_level = layout->layer_first ? 1 : array_size;
567ec681f3Smrg
577ec681f3Smrg   /* use 128 pixel alignment for cpp=1 and cpp=2 */
587ec681f3Smrg   if (layout->cpp < 4 && layout->tile_mode)
597ec681f3Smrg      fdl_set_pitchalign(layout, fdl_cpp_shift(layout) + 7);
607ec681f3Smrg   else
617ec681f3Smrg      fdl_set_pitchalign(layout, fdl_cpp_shift(layout) + 6);
627ec681f3Smrg
637ec681f3Smrg   for (uint32_t level = 0; level < mip_levels; level++) {
647ec681f3Smrg      struct fdl_slice *slice = &layout->slices[level];
657ec681f3Smrg      uint32_t tile_mode = fdl_tile_mode(layout, level);
667ec681f3Smrg      uint32_t pitch = fdl_pitch(layout, level);
677ec681f3Smrg      uint32_t nblocksy =
687ec681f3Smrg         util_format_get_nblocksy(format, u_minify(height0, level));
697ec681f3Smrg
707ec681f3Smrg      if (tile_mode) {
717ec681f3Smrg         nblocksy = align(nblocksy, heightalign);
727ec681f3Smrg      } else {
737ec681f3Smrg         /* The blits used for mem<->gmem work at a granularity of
747ec681f3Smrg          * 32x32, which can cause faults due to over-fetch on the
757ec681f3Smrg          * last level.  The simple solution is to over-allocate a
767ec681f3Smrg          * bit the last level to ensure any over-fetch is harmless.
777ec681f3Smrg          * The pitch is already sufficiently aligned, but height
787ec681f3Smrg          * may not be:
797ec681f3Smrg          */
807ec681f3Smrg         if (level == mip_levels - 1)
817ec681f3Smrg            nblocksy = align(nblocksy, 32);
827ec681f3Smrg      }
837ec681f3Smrg
847ec681f3Smrg      slice->offset = layout->size;
857ec681f3Smrg
867ec681f3Smrg      const int alignment = is_3d ? 4096 : 1;
877ec681f3Smrg
887ec681f3Smrg      /* 1d array and 2d array textures must all have the same layer size
897ec681f3Smrg       * for each miplevel on a3xx. 3d textures can have different layer
907ec681f3Smrg       * sizes for high levels, but the hw auto-sizer is buggy (or at least
917ec681f3Smrg       * different than what this code does), so as soon as the layer size
927ec681f3Smrg       * range gets into range, we stop reducing it.
937ec681f3Smrg       */
947ec681f3Smrg      if (is_3d && (level == 1 ||
957ec681f3Smrg                    (level > 1 && layout->slices[level - 1].size0 > 0xf000)))
967ec681f3Smrg         slice->size0 = align(nblocksy * pitch, alignment);
977ec681f3Smrg      else if (level == 0 || layout->layer_first || alignment == 1)
987ec681f3Smrg         slice->size0 = align(nblocksy * pitch, alignment);
997ec681f3Smrg      else
1007ec681f3Smrg         slice->size0 = layout->slices[level - 1].size0;
1017ec681f3Smrg
1027ec681f3Smrg      layout->size += slice->size0 * u_minify(depth0, level) * layers_in_level;
1037ec681f3Smrg   }
1047ec681f3Smrg}
105