r300_texture_desc.c revision 28a336dc38c478b809544e7404c4d1fddd873333
1d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák/*
2d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák * Copyright 2010 Marek Olšák <maraeo@gmail.com>
4d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák *
5d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák * Permission is hereby granted, free of charge, to any person obtaining a
6d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák * copy of this software and associated documentation files (the "Software"),
7d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák * to deal in the Software without restriction, including without limitation
8d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák * on the rights to use, copy, modify, merge, publish, distribute, sub
9d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák * license, and/or sell copies of the Software, and to permit persons to whom
10d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák * the Software is furnished to do so, subject to the following conditions:
11d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák *
12d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák * The above copyright notice and this permission notice (including the next
13d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák * paragraph) shall be included in all copies or substantial portions of the
14d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák * Software.
15d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák *
16d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
24d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák#include "r300_texture_desc.h"
25d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák#include "r300_context.h"
26d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
27d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák#include "util/u_format.h"
28d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
29d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák/* Returns the number of pixels that the texture should be aligned to
30d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák * in the given dimension. */
31d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšákunsigned r300_get_pixel_alignment(enum pipe_format format,
32d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák                                  unsigned num_samples,
33d35aeff4bb0b03450b2c3c08bd7f84db5bf43283Marek Olšák                                  enum radeon_bo_layout microtile,
34d35aeff4bb0b03450b2c3c08bd7f84db5bf43283Marek Olšák                                  enum radeon_bo_layout macrotile,
35d19b5cbd317620f3977e68fffb7a74793436b7e2Dave Airlie                                  enum r300_dim dim, boolean is_rs690)
36d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák{
37d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    static const unsigned table[2][5][3][2] =
38d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    {
39d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        {
40d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    /* Macro: linear    linear    linear
41d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák       Micro: linear    tiled  square-tiled */
42d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák            {{ 32, 1}, { 8,  4}, { 0,  0}}, /*   8 bits per pixel */
43d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák            {{ 16, 1}, { 8,  2}, { 4,  4}}, /*  16 bits per pixel */
44d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák            {{  8, 1}, { 4,  2}, { 0,  0}}, /*  32 bits per pixel */
45d0408cf55d9e8d1d376bd844386ef5c9789a3597Marek Olšák            {{  4, 1}, { 2,  2}, { 0,  0}}, /*  64 bits per pixel */
46d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák            {{  2, 1}, { 0,  0}, { 0,  0}}  /* 128 bits per pixel */
47d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        },
48d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        {
49d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    /* Macro: tiled     tiled     tiled
50d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák       Micro: linear    tiled  square-tiled */
51d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák            {{256, 8}, {64, 32}, { 0,  0}}, /*   8 bits per pixel */
52d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák            {{128, 8}, {64, 16}, {32, 32}}, /*  16 bits per pixel */
53d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák            {{ 64, 8}, {32, 16}, { 0,  0}}, /*  32 bits per pixel */
54d0408cf55d9e8d1d376bd844386ef5c9789a3597Marek Olšák            {{ 32, 8}, {16, 16}, { 0,  0}}, /*  64 bits per pixel */
55d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák            {{ 16, 8}, { 0,  0}, { 0,  0}}  /* 128 bits per pixel */
56d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        }
57d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    };
58d19b5cbd317620f3977e68fffb7a74793436b7e2Dave Airlie
59d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    static const unsigned aa_block[2] = {4, 8};
60d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    unsigned tile = 0;
61d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    unsigned pixsize = util_format_get_blocksize(format);
62d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
63d35aeff4bb0b03450b2c3c08bd7f84db5bf43283Marek Olšák    assert(macrotile <= RADEON_LAYOUT_TILED);
64d35aeff4bb0b03450b2c3c08bd7f84db5bf43283Marek Olšák    assert(microtile <= RADEON_LAYOUT_SQUARETILED);
65d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    assert(pixsize <= 16);
66d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    assert(dim <= DIM_HEIGHT);
67d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
68d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    if (num_samples > 1) {
69d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        /* Multisampled textures have their own alignment scheme. */
70d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        if (pixsize == 4)
71d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák            tile = aa_block[dim];
72d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        /* XXX FP16 AA. */
73d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    } else {
74d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        /* Standard alignment. */
75d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        tile = table[macrotile][util_logbase2(pixsize)][microtile][dim];
76d19b5cbd317620f3977e68fffb7a74793436b7e2Dave Airlie        if (macrotile == 0 && is_rs690 && dim == DIM_WIDTH) {
77d19b5cbd317620f3977e68fffb7a74793436b7e2Dave Airlie            int align;
78d19b5cbd317620f3977e68fffb7a74793436b7e2Dave Airlie            int h_tile;
79d19b5cbd317620f3977e68fffb7a74793436b7e2Dave Airlie            h_tile = table[macrotile][util_logbase2(pixsize)][microtile][DIM_HEIGHT];
80d19b5cbd317620f3977e68fffb7a74793436b7e2Dave Airlie            align = 64 / (pixsize * h_tile);
81d19b5cbd317620f3977e68fffb7a74793436b7e2Dave Airlie            if (tile < align)
82d19b5cbd317620f3977e68fffb7a74793436b7e2Dave Airlie                tile = align;
83d19b5cbd317620f3977e68fffb7a74793436b7e2Dave Airlie        }
84d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    }
85d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
86d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    assert(tile);
87d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    return tile;
88d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák}
89d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
90d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák/* Return true if macrotiling should be enabled on the miplevel. */
9156ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšákstatic boolean r300_texture_macro_switch(struct r300_resource *tex,
92d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák                                         unsigned level,
93d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák                                         boolean rv350_mode,
94d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák                                         enum r300_dim dim)
95d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák{
96d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    unsigned tile, texdim;
97d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
9856ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    tile = r300_get_pixel_alignment(tex->b.b.b.format, tex->b.b.b.nr_samples,
99d35aeff4bb0b03450b2c3c08bd7f84db5bf43283Marek Olšák                                    tex->tex.microtile, RADEON_LAYOUT_TILED, dim, 0);
100d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    if (dim == DIM_WIDTH) {
10156ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        texdim = u_minify(tex->tex.width0, level);
102d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    } else {
10356ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        texdim = u_minify(tex->tex.height0, level);
104d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    }
105d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
106d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    /* See TX_FILTER1_n.MACRO_SWITCH. */
107d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    if (rv350_mode) {
108d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        return texdim >= tile;
109d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    } else {
110d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        return texdim > tile;
111d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    }
112d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák}
113d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
114d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák/**
115d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák * Return the stride, in bytes, of the texture image of the given texture
116d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák * at the given level.
117d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák */
118d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšákstatic unsigned r300_texture_get_stride(struct r300_screen *screen,
11956ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák                                        struct r300_resource *tex,
120d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák                                        unsigned level)
121d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák{
122d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    unsigned tile_width, width, stride;
123d19b5cbd317620f3977e68fffb7a74793436b7e2Dave Airlie    boolean is_rs690 = (screen->caps.family == CHIP_FAMILY_RS600 ||
124d19b5cbd317620f3977e68fffb7a74793436b7e2Dave Airlie                        screen->caps.family == CHIP_FAMILY_RS690 ||
125d19b5cbd317620f3977e68fffb7a74793436b7e2Dave Airlie                        screen->caps.family == CHIP_FAMILY_RS740);
126d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
12756ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    if (tex->tex.stride_in_bytes_override)
12856ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        return tex->tex.stride_in_bytes_override;
129d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
130d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    /* Check the level. */
13156ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    if (level > tex->b.b.b.last_level) {
132d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        SCREEN_DBG(screen, DBG_TEX, "%s: level (%u) > last_level (%u)\n",
13356ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák                   __FUNCTION__, level, tex->b.b.b.last_level);
134d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        return 0;
135d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    }
136d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
13756ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    width = u_minify(tex->tex.width0, level);
138d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
13956ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    if (util_format_is_plain(tex->b.b.b.format)) {
14056ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        tile_width = r300_get_pixel_alignment(tex->b.b.b.format,
14156ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák                                              tex->b.b.b.nr_samples,
14256ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák                                              tex->tex.microtile,
14356ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák                                              tex->tex.macrotile[level],
144d19b5cbd317620f3977e68fffb7a74793436b7e2Dave Airlie                                              DIM_WIDTH, is_rs690);
145d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        width = align(width, tile_width);
146d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
14756ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        stride = util_format_get_stride(tex->b.b.b.format, width);
148d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        /* The alignment to 32 bytes is sort of implied by the layout... */
149d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        return stride;
150d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    } else {
15156ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        return align(util_format_get_stride(tex->b.b.b.format, width), is_rs690 ? 64 : 32);
152d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    }
153d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák}
154d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
15556ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšákstatic unsigned r300_texture_get_nblocksy(struct r300_resource *tex,
156d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák                                          unsigned level,
157451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák                                          boolean *out_aligned_for_cbzb)
158d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák{
159d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    unsigned height, tile_height;
160d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
16156ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    height = u_minify(tex->tex.height0, level);
162d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
1632d1cc27729bd1808a39b226ae3eda5663328ba74Marek Olšák    /* Mipmapped and 3D textures must have their height aligned to POT. */
1642d1cc27729bd1808a39b226ae3eda5663328ba74Marek Olšák    if ((tex->b.b.b.target != PIPE_TEXTURE_1D &&
1652d1cc27729bd1808a39b226ae3eda5663328ba74Marek Olšák         tex->b.b.b.target != PIPE_TEXTURE_2D &&
1662d1cc27729bd1808a39b226ae3eda5663328ba74Marek Olšák         tex->b.b.b.target != PIPE_TEXTURE_RECT) ||
1672d1cc27729bd1808a39b226ae3eda5663328ba74Marek Olšák        tex->b.b.b.last_level != 0) {
1682d1cc27729bd1808a39b226ae3eda5663328ba74Marek Olšák        height = util_next_power_of_two(height);
1692d1cc27729bd1808a39b226ae3eda5663328ba74Marek Olšák    }
1702d1cc27729bd1808a39b226ae3eda5663328ba74Marek Olšák
17156ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    if (util_format_is_plain(tex->b.b.b.format)) {
17256ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        tile_height = r300_get_pixel_alignment(tex->b.b.b.format,
17356ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák                                               tex->b.b.b.nr_samples,
17456ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák                                               tex->tex.microtile,
17556ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák                                               tex->tex.macrotile[level],
176d19b5cbd317620f3977e68fffb7a74793436b7e2Dave Airlie                                               DIM_HEIGHT, 0);
177d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        height = align(height, tile_height);
178d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
179451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák        /* See if the CBZB clear can be used on the buffer,
180451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák         * taking the texture size into account. */
181451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák        if (out_aligned_for_cbzb) {
18256ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák            if (tex->tex.macrotile[level]) {
183451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák                /* When clearing, the layer (width*height) is horizontally split
184451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák                 * into two, and the upper and lower halves are cleared by the CB
185451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák                 * and ZB units, respectively. Therefore, the number of macrotiles
186451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák                 * in the Y direction must be even. */
187451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák
188451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák                /* Align the height so that there is an even number of macrotiles.
189451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák                 * Do so for 3 or more macrotiles in the Y direction. */
19056ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák                if (level == 0 && tex->b.b.b.last_level == 0 &&
19156ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák                    (tex->b.b.b.target == PIPE_TEXTURE_1D ||
19256ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák                     tex->b.b.b.target == PIPE_TEXTURE_2D ||
19356ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák                     tex->b.b.b.target == PIPE_TEXTURE_RECT) &&
194451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák                    height >= tile_height * 3) {
195451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák                    height = align(height, tile_height * 2);
196451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák                }
197451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák
198451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák                *out_aligned_for_cbzb = height % (tile_height * 2) == 0;
199451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák            } else {
200451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák                *out_aligned_for_cbzb = FALSE;
201451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák            }
202d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        }
203d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    }
204d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
20556ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    return util_format_get_nblocksy(tex->b.b.b.format, height);
206d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák}
207d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
208c92d232061c1aef6f5f56cbd815625778db2fd8cMarek Olšák/* Get a width in pixels from a stride in bytes. */
209c92d232061c1aef6f5f56cbd815625778db2fd8cMarek Olšákstatic unsigned stride_to_width(enum pipe_format format,
210c92d232061c1aef6f5f56cbd815625778db2fd8cMarek Olšák                                unsigned stride_in_bytes)
211c92d232061c1aef6f5f56cbd815625778db2fd8cMarek Olšák{
212c92d232061c1aef6f5f56cbd815625778db2fd8cMarek Olšák    return (stride_in_bytes / util_format_get_blocksize(format)) *
213c92d232061c1aef6f5f56cbd815625778db2fd8cMarek Olšák            util_format_get_blockwidth(format);
214c92d232061c1aef6f5f56cbd815625778db2fd8cMarek Olšák}
215c92d232061c1aef6f5f56cbd815625778db2fd8cMarek Olšák
216d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšákstatic void r300_setup_miptree(struct r300_screen *screen,
21756ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák                               struct r300_resource *tex,
218451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák                               boolean align_for_cbzb)
219d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák{
22056ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    struct pipe_resource *base = &tex->b.b.b;
221d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    unsigned stride, size, layer_size, nblocksy, i;
222311ab3d468ea5291c10bd5cada9b77a528fb7b7fMarek Olšák    boolean rv350_mode = screen->caps.family >= CHIP_FAMILY_R350;
223451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák    boolean aligned_for_cbzb;
224451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák
22556ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    tex->tex.size_in_bytes = 0;
226d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
227d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    SCREEN_DBG(screen, DBG_TEXALLOC,
228d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        "r300: Making miptree for texture, format %s\n",
229d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        util_format_short_name(base->format));
230d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
231d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    for (i = 0; i <= base->last_level; i++) {
232d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        /* Let's see if this miplevel can be macrotiled. */
23356ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        tex->tex.macrotile[i] =
234d35aeff4bb0b03450b2c3c08bd7f84db5bf43283Marek Olšák            (tex->tex.macrotile[0] == RADEON_LAYOUT_TILED &&
23556ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák             r300_texture_macro_switch(tex, i, rv350_mode, DIM_WIDTH) &&
23656ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák             r300_texture_macro_switch(tex, i, rv350_mode, DIM_HEIGHT)) ?
237d35aeff4bb0b03450b2c3c08bd7f84db5bf43283Marek Olšák             RADEON_LAYOUT_TILED : RADEON_LAYOUT_LINEAR;
238d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
23956ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        stride = r300_texture_get_stride(screen, tex, i);
240451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák
241451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák        /* Compute the number of blocks in Y, see if the CBZB clear can be
242451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák         * used on the texture. */
243451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák        aligned_for_cbzb = FALSE;
24456ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        if (align_for_cbzb && tex->tex.cbzb_allowed[i])
24556ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák            nblocksy = r300_texture_get_nblocksy(tex, i, &aligned_for_cbzb);
246451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák        else
24756ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák            nblocksy = r300_texture_get_nblocksy(tex, i, NULL);
248451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák
249d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        layer_size = stride * nblocksy;
250d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
251d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        if (base->nr_samples) {
252d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák            layer_size *= base->nr_samples;
253d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        }
254d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
255d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        if (base->target == PIPE_TEXTURE_CUBE)
256d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák            size = layer_size * 6;
257d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        else
25856ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák            size = layer_size * u_minify(tex->tex.depth0, i);
259d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
26056ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        tex->tex.offset_in_bytes[i] = tex->tex.size_in_bytes;
26156ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        tex->tex.size_in_bytes = tex->tex.offset_in_bytes[i] + size;
26256ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        tex->tex.layer_size_in_bytes[i] = layer_size;
26356ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        tex->tex.stride_in_bytes[i] = stride;
26456ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        tex->tex.stride_in_pixels[i] = stride_to_width(tex->b.b.b.format, stride);
26556ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        tex->tex.cbzb_allowed[i] = tex->tex.cbzb_allowed[i] && aligned_for_cbzb;
266d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
267d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        SCREEN_DBG(screen, DBG_TEXALLOC, "r300: Texture miptree: Level %d "
268d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák                "(%dx%dx%d px, pitch %d bytes) %d bytes total, macrotiled %s\n",
26956ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák                i, u_minify(tex->tex.width0, i), u_minify(tex->tex.height0, i),
27056ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák                u_minify(tex->tex.depth0, i), stride, tex->tex.size_in_bytes,
27156ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák                tex->tex.macrotile[i] ? "TRUE" : "FALSE");
272d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    }
273d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák}
274d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
27556ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšákstatic void r300_setup_flags(struct r300_resource *tex)
276d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák{
27756ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    tex->tex.uses_stride_addressing =
27856ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        !util_is_power_of_two(tex->b.b.b.width0) ||
27956ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        (tex->tex.stride_in_bytes_override &&
28056ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák         stride_to_width(tex->b.b.b.format,
28156ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák                         tex->tex.stride_in_bytes_override) != tex->b.b.b.width0);
28256ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák
28356ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    tex->tex.is_npot =
28456ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        tex->tex.uses_stride_addressing ||
28556ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        !util_is_power_of_two(tex->b.b.b.height0) ||
28656ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        !util_is_power_of_two(tex->b.b.b.depth0);
287d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák}
288d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
289d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšákstatic void r300_setup_cbzb_flags(struct r300_screen *rscreen,
29056ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák                                  struct r300_resource *tex)
291d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák{
292d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    unsigned i, bpp;
293d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    boolean first_level_valid;
294d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
29556ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    bpp = util_format_get_blocksizebits(tex->b.b.b.format);
296d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
297d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    /* 1) The texture must be point-sampled,
298d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák     * 2) The depth must be 16 or 32 bits.
299d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák     * 3) If the midpoint ZB offset is not aligned to 2048, it returns garbage
300d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák     *    with certain texture sizes. Macrotiling ensures the alignment. */
30156ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    first_level_valid = tex->b.b.b.nr_samples <= 1 &&
302d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák                       (bpp == 16 || bpp == 32) &&
30356ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák                       tex->tex.macrotile[0];
304d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
305ab7cc445801b99a4482ea50429ceea1d0601a221Marek Olšák    if (SCREEN_DBG_ON(rscreen, DBG_NO_CBZB))
306ab7cc445801b99a4482ea50429ceea1d0601a221Marek Olšák        first_level_valid = FALSE;
307ab7cc445801b99a4482ea50429ceea1d0601a221Marek Olšák
30856ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    for (i = 0; i <= tex->b.b.b.last_level; i++)
30956ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        tex->tex.cbzb_allowed[i] = first_level_valid && tex->tex.macrotile[i];
310d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák}
311d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
312d99ec708afbb785ce05031661222b38c9447059fMarek Olšákstatic unsigned r300_pixels_to_dwords(unsigned stride,
313d99ec708afbb785ce05031661222b38c9447059fMarek Olšák                                      unsigned height,
314d99ec708afbb785ce05031661222b38c9447059fMarek Olšák                                      unsigned xblock, unsigned yblock)
315d99ec708afbb785ce05031661222b38c9447059fMarek Olšák{
316b6c9c78bffe35d0ca9af100a1595412a1e06bd33Nicolas Peninguy    return (util_align_npot(stride, xblock) * align(height, yblock)) / (xblock * yblock);
317d99ec708afbb785ce05031661222b38c9447059fMarek Olšák}
3182050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák
319d99ec708afbb785ce05031661222b38c9447059fMarek Olšákstatic void r300_setup_hyperz_properties(struct r300_screen *screen,
320d99ec708afbb785ce05031661222b38c9447059fMarek Olšák                                         struct r300_resource *tex)
3212050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák{
322d99ec708afbb785ce05031661222b38c9447059fMarek Olšák    /* The tile size of 1 DWORD in ZMASK RAM is:
3232050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák     *
3242050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák     * GPU    Pipes    4x4 mode   8x8 mode
3252050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák     * ------------------------------------------
3262050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák     * R580   4P/1Z    32x32      64x64
3272050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák     * RV570  3P/1Z    48x16      96x32
3282050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák     * RV530  1P/2Z    32x16      64x32
3292050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák     *        1P/1Z    16x16      32x32
3302050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák     */
331d99ec708afbb785ce05031661222b38c9447059fMarek Olšák    static unsigned zmask_blocks_x_per_dw[4] = {4, 8, 12, 8};
332d99ec708afbb785ce05031661222b38c9447059fMarek Olšák    static unsigned zmask_blocks_y_per_dw[4] = {4, 4,  4, 8};
333d99ec708afbb785ce05031661222b38c9447059fMarek Olšák
334d99ec708afbb785ce05031661222b38c9447059fMarek Olšák    /* In HIZ RAM, one dword is always 8x8 pixels (each byte is 4x4 pixels),
335d99ec708afbb785ce05031661222b38c9447059fMarek Olšák     * but the blocks have very weird ordering.
336d99ec708afbb785ce05031661222b38c9447059fMarek Olšák     *
337d99ec708afbb785ce05031661222b38c9447059fMarek Olšák     * With 2 pipes and an image of size 8xY, where Y >= 1,
338d99ec708afbb785ce05031661222b38c9447059fMarek Olšák     * clearing 4 dwords clears blocks like this:
339d99ec708afbb785ce05031661222b38c9447059fMarek Olšák     *
340d99ec708afbb785ce05031661222b38c9447059fMarek Olšák     *    01012323
341d99ec708afbb785ce05031661222b38c9447059fMarek Olšák     *
342d99ec708afbb785ce05031661222b38c9447059fMarek Olšák     * where numbers correspond to dword indices. The blocks are interleaved
343d99ec708afbb785ce05031661222b38c9447059fMarek Olšák     * in the X direction, so the alignment must be 4x1 blocks (32x8 pixels).
344d99ec708afbb785ce05031661222b38c9447059fMarek Olšák     *
345d99ec708afbb785ce05031661222b38c9447059fMarek Olšák     * With 4 pipes and an image of size 8xY, where Y >= 4,
346d99ec708afbb785ce05031661222b38c9447059fMarek Olšák     * clearing 8 dwords clears blocks like this:
347d99ec708afbb785ce05031661222b38c9447059fMarek Olšák     *    01012323
348d99ec708afbb785ce05031661222b38c9447059fMarek Olšák     *    45456767
349d99ec708afbb785ce05031661222b38c9447059fMarek Olšák     *    01012323
350d99ec708afbb785ce05031661222b38c9447059fMarek Olšák     *    45456767
351d99ec708afbb785ce05031661222b38c9447059fMarek Olšák     * where numbers correspond to dword indices. The blocks are interleaved
352d99ec708afbb785ce05031661222b38c9447059fMarek Olšák     * in both directions, so the alignment must be 4x4 blocks (32x32 pixels)
353d99ec708afbb785ce05031661222b38c9447059fMarek Olšák     */
354d99ec708afbb785ce05031661222b38c9447059fMarek Olšák    static unsigned hiz_align_x[4] = {8, 32, 48, 32};
355d99ec708afbb785ce05031661222b38c9447059fMarek Olšák    static unsigned hiz_align_y[4] = {8, 8, 8, 32};
3562050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák
35756ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    if (util_format_is_depth_or_stencil(tex->b.b.b.format) &&
35856ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        util_format_get_blocksizebits(tex->b.b.b.format) == 32 &&
35956ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        tex->tex.microtile) {
3602050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák        unsigned i, pipes;
3612050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák
3622050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák        if (screen->caps.family == CHIP_FAMILY_RV530) {
36328a336dc38c478b809544e7404c4d1fddd873333Marek Olšák            pipes = screen->info.r300_num_z_pipes;
3642050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák        } else {
36528a336dc38c478b809544e7404c4d1fddd873333Marek Olšák            pipes = screen->info.r300_num_gb_pipes;
3662050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák        }
3672050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák
36856ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        for (i = 0; i <= tex->b.b.b.last_level; i++) {
369d99ec708afbb785ce05031661222b38c9447059fMarek Olšák            unsigned zcomp_numdw, zcompsize, hiz_numdw, stride, height;
370d99ec708afbb785ce05031661222b38c9447059fMarek Olšák
371d99ec708afbb785ce05031661222b38c9447059fMarek Olšák            stride = align(tex->tex.stride_in_pixels[i], 16);
372d99ec708afbb785ce05031661222b38c9447059fMarek Olšák            height = u_minify(tex->b.b.b.height0, i);
3732050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák
3742050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák            /* The 8x8 compression mode needs macrotiling. */
375d99ec708afbb785ce05031661222b38c9447059fMarek Olšák            zcompsize = screen->caps.z_compress == R300_ZCOMP_8X8 &&
37656ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák                       tex->tex.macrotile[i] &&
37756ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák                       tex->b.b.b.nr_samples <= 1 ? 8 : 4;
3782050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák
379d99ec708afbb785ce05031661222b38c9447059fMarek Olšák            /* Get the ZMASK buffer size in dwords. */
380d99ec708afbb785ce05031661222b38c9447059fMarek Olšák            zcomp_numdw = r300_pixels_to_dwords(stride, height,
381d99ec708afbb785ce05031661222b38c9447059fMarek Olšák                                zmask_blocks_x_per_dw[pipes-1] * zcompsize,
382d99ec708afbb785ce05031661222b38c9447059fMarek Olšák                                zmask_blocks_y_per_dw[pipes-1] * zcompsize);
3832050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák
384d99ec708afbb785ce05031661222b38c9447059fMarek Olšák            /* Check whether we have enough ZMASK memory. */
385d99ec708afbb785ce05031661222b38c9447059fMarek Olšák            if (util_format_get_blocksizebits(tex->b.b.b.format) == 32 &&
386d99ec708afbb785ce05031661222b38c9447059fMarek Olšák                zcomp_numdw <= screen->caps.zmask_ram * pipes) {
387d99ec708afbb785ce05031661222b38c9447059fMarek Olšák                tex->tex.zmask_dwords[i] = zcomp_numdw;
388d99ec708afbb785ce05031661222b38c9447059fMarek Olšák                tex->tex.zcomp8x8[i] = zcompsize == 8;
3892050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák
390d99ec708afbb785ce05031661222b38c9447059fMarek Olšák                tex->tex.zmask_stride_in_pixels[i] =
391b6c9c78bffe35d0ca9af100a1595412a1e06bd33Nicolas Peninguy                    util_align_npot(stride, zmask_blocks_x_per_dw[pipes-1] * zcompsize);
3922050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák            } else {
39356ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák                tex->tex.zmask_dwords[i] = 0;
39456ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák                tex->tex.zcomp8x8[i] = FALSE;
395d99ec708afbb785ce05031661222b38c9447059fMarek Olšák                tex->tex.zmask_stride_in_pixels[i] = 0;
396d99ec708afbb785ce05031661222b38c9447059fMarek Olšák            }
397d99ec708afbb785ce05031661222b38c9447059fMarek Olšák
398d99ec708afbb785ce05031661222b38c9447059fMarek Olšák            /* Now setup HIZ. */
399b6c9c78bffe35d0ca9af100a1595412a1e06bd33Nicolas Peninguy            stride = util_align_npot(stride, hiz_align_x[pipes-1]);
400d99ec708afbb785ce05031661222b38c9447059fMarek Olšák            height = align(height, hiz_align_y[pipes-1]);
401d99ec708afbb785ce05031661222b38c9447059fMarek Olšák
402d99ec708afbb785ce05031661222b38c9447059fMarek Olšák            /* Get the HIZ buffer size in dwords. */
403d99ec708afbb785ce05031661222b38c9447059fMarek Olšák            hiz_numdw = (stride * height) / (8*8 * pipes);
404d99ec708afbb785ce05031661222b38c9447059fMarek Olšák
405d99ec708afbb785ce05031661222b38c9447059fMarek Olšák            /* Check whether we have enough HIZ memory. */
406d99ec708afbb785ce05031661222b38c9447059fMarek Olšák            if (hiz_numdw <= screen->caps.hiz_ram * pipes) {
407d99ec708afbb785ce05031661222b38c9447059fMarek Olšák                tex->tex.hiz_dwords[i] = hiz_numdw;
408d99ec708afbb785ce05031661222b38c9447059fMarek Olšák                tex->tex.hiz_stride_in_pixels[i] = stride;
409d99ec708afbb785ce05031661222b38c9447059fMarek Olšák            } else {
410d99ec708afbb785ce05031661222b38c9447059fMarek Olšák                tex->tex.hiz_dwords[i] = 0;
411d99ec708afbb785ce05031661222b38c9447059fMarek Olšák                tex->tex.hiz_stride_in_pixels[i] = 0;
4122050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák            }
4132050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák        }
4142050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák    }
4152050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák}
4162050f2ab96f923112d3475a655b31c8f5145a800Marek Olšák
417d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšákstatic void r300_setup_tiling(struct r300_screen *screen,
41856ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák                              struct r300_resource *tex)
419d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák{
42056ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    enum pipe_format format = tex->b.b.b.format;
421311ab3d468ea5291c10bd5cada9b77a528fb7b7fMarek Olšák    boolean rv350_mode = screen->caps.family >= CHIP_FAMILY_R350;
422d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    boolean is_zb = util_format_is_depth_or_stencil(format);
423d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    boolean dbg_no_tiling = SCREEN_DBG_ON(screen, DBG_NO_TILING);
424d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
425d35aeff4bb0b03450b2c3c08bd7f84db5bf43283Marek Olšák    tex->tex.microtile = RADEON_LAYOUT_LINEAR;
426d35aeff4bb0b03450b2c3c08bd7f84db5bf43283Marek Olšák    tex->tex.macrotile[0] = RADEON_LAYOUT_LINEAR;
4277c24a4c6a86402be1f68d23f4d52d4d071957801Marek Olšák
428d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    if (!util_format_is_plain(format)) {
429d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        return;
430d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    }
431d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
432d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    /* If height == 1, disable microtiling except for zbuffer. */
43356ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    if (!is_zb && (tex->b.b.b.height0 == 1 || dbg_no_tiling)) {
434d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        return;
435d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    }
436d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
437d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    /* Set microtiling. */
438d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    switch (util_format_get_blocksize(format)) {
439d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        case 1:
440d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        case 4:
441d0408cf55d9e8d1d376bd844386ef5c9789a3597Marek Olšák        case 8:
442d35aeff4bb0b03450b2c3c08bd7f84db5bf43283Marek Olšák            tex->tex.microtile = RADEON_LAYOUT_TILED;
443d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák            break;
444d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
445d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        case 2:
446d35aeff4bb0b03450b2c3c08bd7f84db5bf43283Marek Olšák            tex->tex.microtile = RADEON_LAYOUT_SQUARETILED;
447d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák            break;
448d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    }
449d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
450d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    if (dbg_no_tiling) {
451d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        return;
452d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    }
453d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
454d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    /* Set macrotiling. */
45556ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    if (r300_texture_macro_switch(tex, 0, rv350_mode, DIM_WIDTH) &&
45656ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        r300_texture_macro_switch(tex, 0, rv350_mode, DIM_HEIGHT)) {
457d35aeff4bb0b03450b2c3c08bd7f84db5bf43283Marek Olšák        tex->tex.macrotile[0] = RADEON_LAYOUT_TILED;
458d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    }
459d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák}
460d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
46156ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšákstatic void r300_tex_print_info(struct r300_resource *tex,
462d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák                                const char *func)
463d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák{
464d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    fprintf(stderr,
465d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák            "r300: %s: Macro: %s, Micro: %s, Pitch: %i, Dim: %ix%ix%i, "
466d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák            "LastLevel: %i, Size: %i, Format: %s\n",
467d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák            func,
46856ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák            tex->tex.macrotile[0] ? "YES" : " NO",
46956ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák            tex->tex.microtile ? "YES" : " NO",
47056ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák            tex->tex.stride_in_pixels[0],
47156ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák            tex->b.b.b.width0, tex->b.b.b.height0, tex->b.b.b.depth0,
47256ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák            tex->b.b.b.last_level, tex->tex.size_in_bytes,
47356ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák            util_format_short_name(tex->b.b.b.format));
474d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák}
475d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
476d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšákboolean r300_texture_desc_init(struct r300_screen *rscreen,
47756ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák                               struct r300_resource *tex,
4787c24a4c6a86402be1f68d23f4d52d4d071957801Marek Olšák                               const struct pipe_resource *base)
479d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák{
4807c24a4c6a86402be1f68d23f4d52d4d071957801Marek Olšák    tex->b.b.b.target = base->target;
4817c24a4c6a86402be1f68d23f4d52d4d071957801Marek Olšák    tex->b.b.b.format = base->format;
4827c24a4c6a86402be1f68d23f4d52d4d071957801Marek Olšák    tex->b.b.b.width0 = base->width0;
4837c24a4c6a86402be1f68d23f4d52d4d071957801Marek Olšák    tex->b.b.b.height0 = base->height0;
4847c24a4c6a86402be1f68d23f4d52d4d071957801Marek Olšák    tex->b.b.b.depth0 = base->depth0;
4857c24a4c6a86402be1f68d23f4d52d4d071957801Marek Olšák    tex->b.b.b.array_size = base->array_size;
4867c24a4c6a86402be1f68d23f4d52d4d071957801Marek Olšák    tex->b.b.b.last_level = base->last_level;
4877c24a4c6a86402be1f68d23f4d52d4d071957801Marek Olšák    tex->b.b.b.nr_samples = base->nr_samples;
48856ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    tex->tex.width0 = base->width0;
48956ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    tex->tex.height0 = base->height0;
49056ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    tex->tex.depth0 = base->depth0;
49113359e6a4b732335cdd8da48276960d0b176ffe3Marek Olšák
49256ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    r300_setup_flags(tex);
49313359e6a4b732335cdd8da48276960d0b176ffe3Marek Olšák
49413359e6a4b732335cdd8da48276960d0b176ffe3Marek Olšák    /* Align a 3D NPOT texture to POT. */
49556ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    if (base->target == PIPE_TEXTURE_3D && tex->tex.is_npot) {
49656ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        tex->tex.width0 = util_next_power_of_two(tex->tex.width0);
49756ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        tex->tex.height0 = util_next_power_of_two(tex->tex.height0);
49856ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        tex->tex.depth0 = util_next_power_of_two(tex->tex.depth0);
49913359e6a4b732335cdd8da48276960d0b176ffe3Marek Olšák    }
500d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
50113359e6a4b732335cdd8da48276960d0b176ffe3Marek Olšák    /* Setup tiling. */
502d35aeff4bb0b03450b2c3c08bd7f84db5bf43283Marek Olšák    if (tex->tex.microtile == RADEON_LAYOUT_UNKNOWN) {
50356ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        r300_setup_tiling(rscreen, tex);
504d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    }
505d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
50656ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    r300_setup_cbzb_flags(rscreen, tex);
507d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
508451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák    /* Setup the miptree description. */
50956ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    r300_setup_miptree(rscreen, tex, TRUE);
510451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák    /* If the required buffer size is larger the given max size,
511451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák     * try again without the alignment for the CBZB clear. */
5127c24a4c6a86402be1f68d23f4d52d4d071957801Marek Olšák    if (tex->buf_size && tex->tex.size_in_bytes > tex->buf_size) {
51356ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        r300_setup_miptree(rscreen, tex, FALSE);
514451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák    }
515451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák
516d99ec708afbb785ce05031661222b38c9447059fMarek Olšák    r300_setup_hyperz_properties(rscreen, tex);
517451a0ddb190e5185372fed9ec57d24a822442eccMarek Olšák
5187c24a4c6a86402be1f68d23f4d52d4d071957801Marek Olšák    if (tex->buf_size) {
519d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        /* Make sure the buffer we got is large enough. */
5207c24a4c6a86402be1f68d23f4d52d4d071957801Marek Olšák        if (tex->tex.size_in_bytes > tex->buf_size) {
5217d28ec8500fc08fafbdb3ae5443b32dd756eddd7Marek Olšák            fprintf(stderr, "r300: texture_desc_init: The buffer is not "
522d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák                            "large enough. Got: %i, Need: %i, Info:\n",
5237c24a4c6a86402be1f68d23f4d52d4d071957801Marek Olšák                            tex->buf_size, tex->tex.size_in_bytes);
52456ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák            r300_tex_print_info(tex, "texture_desc_init");
525d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák            return FALSE;
526d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        }
527d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
5287c24a4c6a86402be1f68d23f4d52d4d071957801Marek Olšák        tex->tex.buffer_size_in_bytes = tex->buf_size;
529d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    } else {
53056ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        tex->tex.buffer_size_in_bytes = tex->tex.size_in_bytes;
531d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    }
532d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
533d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    if (SCREEN_DBG_ON(rscreen, DBG_TEX))
53456ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák        r300_tex_print_info(tex, "texture_desc_init");
535d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
536d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    return TRUE;
537d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák}
538d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
53956ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšákunsigned r300_texture_get_offset(struct r300_resource *tex,
5404c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger                                 unsigned level, unsigned layer)
541d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák{
54256ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    unsigned offset = tex->tex.offset_in_bytes[level];
543d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
54456ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák    switch (tex->b.b.b.target) {
545d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        case PIPE_TEXTURE_3D:
546f3021c688fc7a9c7d1eb5c207b6edebfcc9bd6fbMarek Olšák        case PIPE_TEXTURE_CUBE:
54756ba7e913fef0ea2b1bead582108f9ab3ab8263dMarek Olšák            return offset + layer * tex->tex.layer_size_in_bytes[level];
548d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák
549d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák        default:
5504c7001462607e6e99e474d6271dd481d3f8f201cRoland Scheidegger            assert(layer == 0);
551d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák            return offset;
552d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák    }
553d779a5d16ae6a17b3fc0c097f4eb477a80e54566Marek Olšák}
554