1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2009 Marek Olšák <maraeo@gmail.com>
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8bf215546Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom
9bf215546Sopenharmony_ci * the 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 next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * 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 AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22bf215546Sopenharmony_ci
23bf215546Sopenharmony_ci#include "r300_context.h"
24bf215546Sopenharmony_ci#include "r300_emit.h"
25bf215546Sopenharmony_ci#include "r300_texture.h"
26bf215546Sopenharmony_ci#include "r300_reg.h"
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include "util/format/u_format.h"
29bf215546Sopenharmony_ci#include "util/half_float.h"
30bf215546Sopenharmony_ci#include "util/u_pack_color.h"
31bf215546Sopenharmony_ci#include "util/u_surface.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_cienum r300_blitter_op /* bitmask */
34bf215546Sopenharmony_ci{
35bf215546Sopenharmony_ci    R300_STOP_QUERY         = 1,
36bf215546Sopenharmony_ci    R300_SAVE_TEXTURES      = 2,
37bf215546Sopenharmony_ci    R300_SAVE_FRAMEBUFFER   = 4,
38bf215546Sopenharmony_ci    R300_IGNORE_RENDER_COND = 8,
39bf215546Sopenharmony_ci
40bf215546Sopenharmony_ci    R300_CLEAR         = R300_STOP_QUERY,
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci    R300_CLEAR_SURFACE = R300_STOP_QUERY | R300_SAVE_FRAMEBUFFER,
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci    R300_COPY          = R300_STOP_QUERY | R300_SAVE_FRAMEBUFFER |
45bf215546Sopenharmony_ci                         R300_SAVE_TEXTURES | R300_IGNORE_RENDER_COND,
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci    R300_BLIT          = R300_STOP_QUERY | R300_SAVE_FRAMEBUFFER |
48bf215546Sopenharmony_ci                         R300_SAVE_TEXTURES,
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci    R300_DECOMPRESS    = R300_STOP_QUERY | R300_IGNORE_RENDER_COND,
51bf215546Sopenharmony_ci};
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_cistatic void r300_blitter_begin(struct r300_context* r300, enum r300_blitter_op op)
54bf215546Sopenharmony_ci{
55bf215546Sopenharmony_ci    if ((op & R300_STOP_QUERY) && r300->query_current) {
56bf215546Sopenharmony_ci        r300->blitter_saved_query = r300->query_current;
57bf215546Sopenharmony_ci        r300_stop_query(r300);
58bf215546Sopenharmony_ci    }
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci    /* Yeah we have to save all those states to ensure the blitter operation
61bf215546Sopenharmony_ci     * is really transparent. The states will be restored by the blitter once
62bf215546Sopenharmony_ci     * copying is done. */
63bf215546Sopenharmony_ci    util_blitter_save_blend(r300->blitter, r300->blend_state.state);
64bf215546Sopenharmony_ci    util_blitter_save_depth_stencil_alpha(r300->blitter, r300->dsa_state.state);
65bf215546Sopenharmony_ci    util_blitter_save_stencil_ref(r300->blitter, &(r300->stencil_ref));
66bf215546Sopenharmony_ci    util_blitter_save_rasterizer(r300->blitter, r300->rs_state.state);
67bf215546Sopenharmony_ci    util_blitter_save_fragment_shader(r300->blitter, r300->fs.state);
68bf215546Sopenharmony_ci    util_blitter_save_vertex_shader(r300->blitter, r300->vs_state.state);
69bf215546Sopenharmony_ci    util_blitter_save_viewport(r300->blitter, &r300->viewport);
70bf215546Sopenharmony_ci    util_blitter_save_scissor(r300->blitter, r300->scissor_state.state);
71bf215546Sopenharmony_ci    util_blitter_save_sample_mask(r300->blitter, *(unsigned*)r300->sample_mask.state, 0);
72bf215546Sopenharmony_ci    util_blitter_save_vertex_buffer_slot(r300->blitter, r300->vertex_buffer);
73bf215546Sopenharmony_ci    util_blitter_save_vertex_elements(r300->blitter, r300->velems);
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_ci    struct pipe_constant_buffer cb = {
76bf215546Sopenharmony_ci       /* r300 doesn't use the size for FS at all. The shader determines it.
77bf215546Sopenharmony_ci        * Set something for blitter.
78bf215546Sopenharmony_ci        */
79bf215546Sopenharmony_ci       .buffer_size = 4,
80bf215546Sopenharmony_ci       .user_buffer = ((struct r300_constant_buffer*)r300->fs_constants.state)->ptr,
81bf215546Sopenharmony_ci    };
82bf215546Sopenharmony_ci    util_blitter_save_fragment_constant_buffer_slot(r300->blitter, &cb);
83bf215546Sopenharmony_ci
84bf215546Sopenharmony_ci    if (op & R300_SAVE_FRAMEBUFFER) {
85bf215546Sopenharmony_ci        util_blitter_save_framebuffer(r300->blitter, r300->fb_state.state);
86bf215546Sopenharmony_ci    }
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci    if (op & R300_SAVE_TEXTURES) {
89bf215546Sopenharmony_ci        struct r300_textures_state* state =
90bf215546Sopenharmony_ci            (struct r300_textures_state*)r300->textures_state.state;
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci        util_blitter_save_fragment_sampler_states(
93bf215546Sopenharmony_ci            r300->blitter, state->sampler_state_count,
94bf215546Sopenharmony_ci            (void**)state->sampler_states);
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci        util_blitter_save_fragment_sampler_views(
97bf215546Sopenharmony_ci            r300->blitter, state->sampler_view_count,
98bf215546Sopenharmony_ci            (struct pipe_sampler_view**)state->sampler_views);
99bf215546Sopenharmony_ci    }
100bf215546Sopenharmony_ci
101bf215546Sopenharmony_ci    if (op & R300_IGNORE_RENDER_COND) {
102bf215546Sopenharmony_ci        /* Save the flag. */
103bf215546Sopenharmony_ci        r300->blitter_saved_skip_rendering = r300->skip_rendering+1;
104bf215546Sopenharmony_ci        r300->skip_rendering = FALSE;
105bf215546Sopenharmony_ci    } else {
106bf215546Sopenharmony_ci        r300->blitter_saved_skip_rendering = 0;
107bf215546Sopenharmony_ci    }
108bf215546Sopenharmony_ci}
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_cistatic void r300_blitter_end(struct r300_context *r300)
111bf215546Sopenharmony_ci{
112bf215546Sopenharmony_ci    if (r300->blitter_saved_query) {
113bf215546Sopenharmony_ci        r300_resume_query(r300, r300->blitter_saved_query);
114bf215546Sopenharmony_ci        r300->blitter_saved_query = NULL;
115bf215546Sopenharmony_ci    }
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci    if (r300->blitter_saved_skip_rendering) {
118bf215546Sopenharmony_ci        /* Restore the flag. */
119bf215546Sopenharmony_ci        r300->skip_rendering = r300->blitter_saved_skip_rendering-1;
120bf215546Sopenharmony_ci    }
121bf215546Sopenharmony_ci}
122bf215546Sopenharmony_ci
123bf215546Sopenharmony_cistatic uint32_t r300_depth_clear_cb_value(enum pipe_format format,
124bf215546Sopenharmony_ci                                          const float* rgba)
125bf215546Sopenharmony_ci{
126bf215546Sopenharmony_ci    union util_color uc;
127bf215546Sopenharmony_ci    util_pack_color(rgba, format, &uc);
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci    if (util_format_get_blocksizebits(format) == 32)
130bf215546Sopenharmony_ci        return uc.ui[0];
131bf215546Sopenharmony_ci    else
132bf215546Sopenharmony_ci        return uc.us | (uc.us << 16);
133bf215546Sopenharmony_ci}
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_cistatic boolean r300_cbzb_clear_allowed(struct r300_context *r300,
136bf215546Sopenharmony_ci                                       unsigned clear_buffers)
137bf215546Sopenharmony_ci{
138bf215546Sopenharmony_ci    struct pipe_framebuffer_state *fb =
139bf215546Sopenharmony_ci        (struct pipe_framebuffer_state*)r300->fb_state.state;
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci    /* Only color clear allowed, and only one colorbuffer. */
142bf215546Sopenharmony_ci    if ((clear_buffers & ~PIPE_CLEAR_COLOR) != 0 || fb->nr_cbufs != 1 || !fb->cbufs[0])
143bf215546Sopenharmony_ci        return FALSE;
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_ci    return r300_surface(fb->cbufs[0])->cbzb_allowed;
146bf215546Sopenharmony_ci}
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_cistatic boolean r300_fast_zclear_allowed(struct r300_context *r300,
149bf215546Sopenharmony_ci                                        unsigned clear_buffers)
150bf215546Sopenharmony_ci{
151bf215546Sopenharmony_ci    struct pipe_framebuffer_state *fb =
152bf215546Sopenharmony_ci        (struct pipe_framebuffer_state*)r300->fb_state.state;
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_ci    return r300_resource(fb->zsbuf->texture)->tex.zmask_dwords[fb->zsbuf->u.tex.level] != 0;
155bf215546Sopenharmony_ci}
156bf215546Sopenharmony_ci
157bf215546Sopenharmony_cistatic boolean r300_hiz_clear_allowed(struct r300_context *r300)
158bf215546Sopenharmony_ci{
159bf215546Sopenharmony_ci    struct pipe_framebuffer_state *fb =
160bf215546Sopenharmony_ci        (struct pipe_framebuffer_state*)r300->fb_state.state;
161bf215546Sopenharmony_ci
162bf215546Sopenharmony_ci    return r300_resource(fb->zsbuf->texture)->tex.hiz_dwords[fb->zsbuf->u.tex.level] != 0;
163bf215546Sopenharmony_ci}
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_cistatic uint32_t r300_depth_clear_value(enum pipe_format format,
166bf215546Sopenharmony_ci                                       double depth, unsigned stencil)
167bf215546Sopenharmony_ci{
168bf215546Sopenharmony_ci    switch (format) {
169bf215546Sopenharmony_ci        case PIPE_FORMAT_Z16_UNORM:
170bf215546Sopenharmony_ci        case PIPE_FORMAT_X8Z24_UNORM:
171bf215546Sopenharmony_ci            return util_pack_z(format, depth);
172bf215546Sopenharmony_ci
173bf215546Sopenharmony_ci        case PIPE_FORMAT_S8_UINT_Z24_UNORM:
174bf215546Sopenharmony_ci            return util_pack_z_stencil(format, depth, stencil);
175bf215546Sopenharmony_ci
176bf215546Sopenharmony_ci        default:
177bf215546Sopenharmony_ci            assert(0);
178bf215546Sopenharmony_ci            return 0;
179bf215546Sopenharmony_ci    }
180bf215546Sopenharmony_ci}
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_cistatic uint32_t r300_hiz_clear_value(double depth)
183bf215546Sopenharmony_ci{
184bf215546Sopenharmony_ci    uint32_t r = (uint32_t)(CLAMP(depth, 0, 1) * 255.5);
185bf215546Sopenharmony_ci    assert(r <= 255);
186bf215546Sopenharmony_ci    return r | (r << 8) | (r << 16) | (r << 24);
187bf215546Sopenharmony_ci}
188bf215546Sopenharmony_ci
189bf215546Sopenharmony_cistatic void r300_set_clear_color(struct r300_context *r300,
190bf215546Sopenharmony_ci                                 const union pipe_color_union *color)
191bf215546Sopenharmony_ci{
192bf215546Sopenharmony_ci    struct pipe_framebuffer_state *fb =
193bf215546Sopenharmony_ci        (struct pipe_framebuffer_state*)r300->fb_state.state;
194bf215546Sopenharmony_ci    union util_color uc;
195bf215546Sopenharmony_ci
196bf215546Sopenharmony_ci    memset(&uc, 0, sizeof(uc));
197bf215546Sopenharmony_ci    util_pack_color(color->f, fb->cbufs[0]->format, &uc);
198bf215546Sopenharmony_ci
199bf215546Sopenharmony_ci    if (fb->cbufs[0]->format == PIPE_FORMAT_R16G16B16A16_FLOAT ||
200bf215546Sopenharmony_ci        fb->cbufs[0]->format == PIPE_FORMAT_R16G16B16X16_FLOAT) {
201bf215546Sopenharmony_ci        /* (0,1,2,3) maps to (B,G,R,A) */
202bf215546Sopenharmony_ci        r300->color_clear_value_gb = uc.h[0] | ((uint32_t)uc.h[1] << 16);
203bf215546Sopenharmony_ci        r300->color_clear_value_ar = uc.h[2] | ((uint32_t)uc.h[3] << 16);
204bf215546Sopenharmony_ci    } else {
205bf215546Sopenharmony_ci        r300->color_clear_value = uc.ui[0];
206bf215546Sopenharmony_ci    }
207bf215546Sopenharmony_ci}
208bf215546Sopenharmony_ci
209bf215546Sopenharmony_ciDEBUG_GET_ONCE_BOOL_OPTION(hyperz, "RADEON_HYPERZ", FALSE)
210bf215546Sopenharmony_ci
211bf215546Sopenharmony_ci/* Clear currently bound buffers. */
212bf215546Sopenharmony_cistatic void r300_clear(struct pipe_context* pipe,
213bf215546Sopenharmony_ci                       unsigned buffers,
214bf215546Sopenharmony_ci                       const struct pipe_scissor_state *scissor_state,
215bf215546Sopenharmony_ci                       const union pipe_color_union *color,
216bf215546Sopenharmony_ci                       double depth,
217bf215546Sopenharmony_ci                       unsigned stencil)
218bf215546Sopenharmony_ci{
219bf215546Sopenharmony_ci    /* My notes about Zbuffer compression:
220bf215546Sopenharmony_ci     *
221bf215546Sopenharmony_ci     * 1) The zbuffer must be micro-tiled and whole microtiles must be
222bf215546Sopenharmony_ci     *    written if compression is enabled. If microtiling is disabled,
223bf215546Sopenharmony_ci     *    it locks up.
224bf215546Sopenharmony_ci     *
225bf215546Sopenharmony_ci     * 2) There is ZMASK RAM which contains a compressed zbuffer.
226bf215546Sopenharmony_ci     *    Each dword of the Z Mask contains compression information
227bf215546Sopenharmony_ci     *    for 16 4x4 pixel tiles, that is 2 bits for each tile.
228bf215546Sopenharmony_ci     *    On chips with 2 Z pipes, every other dword maps to a different
229bf215546Sopenharmony_ci     *    pipe. On newer chipsets, there is a new compression mode
230bf215546Sopenharmony_ci     *    with 8x8 pixel tiles per 2 bits.
231bf215546Sopenharmony_ci     *
232bf215546Sopenharmony_ci     * 3) The FASTFILL bit has nothing to do with filling. It only tells hw
233bf215546Sopenharmony_ci     *    it should look in the ZMASK RAM first before fetching from a real
234bf215546Sopenharmony_ci     *    zbuffer.
235bf215546Sopenharmony_ci     *
236bf215546Sopenharmony_ci     * 4) If a pixel is in a cleared state, ZB_DEPTHCLEARVALUE is returned
237bf215546Sopenharmony_ci     *    during zbuffer reads instead of the value that is actually stored
238bf215546Sopenharmony_ci     *    in the zbuffer memory. A pixel is in a cleared state when its ZMASK
239bf215546Sopenharmony_ci     *    is equal to 0. Therefore, if you clear ZMASK with zeros, you may
240bf215546Sopenharmony_ci     *    leave the zbuffer memory uninitialized, but then you must enable
241bf215546Sopenharmony_ci     *    compression, so that the ZMASK RAM is actually used.
242bf215546Sopenharmony_ci     *
243bf215546Sopenharmony_ci     * 5) Each 4x4 (or 8x8) tile is automatically decompressed and recompressed
244bf215546Sopenharmony_ci     *    during zbuffer updates. A special decompressing operation should be
245bf215546Sopenharmony_ci     *    used to fully decompress a zbuffer, which basically just stores all
246bf215546Sopenharmony_ci     *    compressed tiles in ZMASK to the zbuffer memory.
247bf215546Sopenharmony_ci     *
248bf215546Sopenharmony_ci     * 6) For a 16-bit zbuffer, compression causes a hung with one or
249bf215546Sopenharmony_ci     *    two samples and should not be used.
250bf215546Sopenharmony_ci     *
251bf215546Sopenharmony_ci     * 7) FORCE_COMPRESSED_STENCIL_VALUE should be enabled for stencil clears
252bf215546Sopenharmony_ci     *    to avoid needless decompression.
253bf215546Sopenharmony_ci     *
254bf215546Sopenharmony_ci     * 8) Fastfill must not be used if reading of compressed Z data is disabled
255bf215546Sopenharmony_ci     *    and writing of compressed Z data is enabled (RD/WR_COMP_ENABLE),
256bf215546Sopenharmony_ci     *    i.e. it cannot be used to compress the zbuffer.
257bf215546Sopenharmony_ci     *
258bf215546Sopenharmony_ci     * 9) ZB_CB_CLEAR does not interact with zbuffer compression in any way.
259bf215546Sopenharmony_ci     *
260bf215546Sopenharmony_ci     * - Marek
261bf215546Sopenharmony_ci     */
262bf215546Sopenharmony_ci
263bf215546Sopenharmony_ci    struct r300_context* r300 = r300_context(pipe);
264bf215546Sopenharmony_ci    struct pipe_framebuffer_state *fb =
265bf215546Sopenharmony_ci        (struct pipe_framebuffer_state*)r300->fb_state.state;
266bf215546Sopenharmony_ci    struct r300_hyperz_state *hyperz =
267bf215546Sopenharmony_ci        (struct r300_hyperz_state*)r300->hyperz_state.state;
268bf215546Sopenharmony_ci    uint32_t width = fb->width;
269bf215546Sopenharmony_ci    uint32_t height = fb->height;
270bf215546Sopenharmony_ci    uint32_t hyperz_dcv = hyperz->zb_depthclearvalue;
271bf215546Sopenharmony_ci
272bf215546Sopenharmony_ci    /* Use fast Z clear.
273bf215546Sopenharmony_ci     * The zbuffer must be in micro-tiled mode, otherwise it locks up. */
274bf215546Sopenharmony_ci    if (buffers & PIPE_CLEAR_DEPTHSTENCIL) {
275bf215546Sopenharmony_ci        boolean zmask_clear, hiz_clear;
276bf215546Sopenharmony_ci
277bf215546Sopenharmony_ci        /* If both depth and stencil are present, they must be cleared together. */
278bf215546Sopenharmony_ci        if (fb->zsbuf->texture->format == PIPE_FORMAT_S8_UINT_Z24_UNORM &&
279bf215546Sopenharmony_ci            (buffers & PIPE_CLEAR_DEPTHSTENCIL) != PIPE_CLEAR_DEPTHSTENCIL) {
280bf215546Sopenharmony_ci            zmask_clear = FALSE;
281bf215546Sopenharmony_ci            hiz_clear = FALSE;
282bf215546Sopenharmony_ci        } else {
283bf215546Sopenharmony_ci            zmask_clear = r300_fast_zclear_allowed(r300, buffers);
284bf215546Sopenharmony_ci            hiz_clear = r300_hiz_clear_allowed(r300);
285bf215546Sopenharmony_ci        }
286bf215546Sopenharmony_ci
287bf215546Sopenharmony_ci        /* If we need Hyper-Z. */
288bf215546Sopenharmony_ci        if (zmask_clear || hiz_clear) {
289bf215546Sopenharmony_ci            /* Try to obtain the access to Hyper-Z buffers if we don't have one. */
290bf215546Sopenharmony_ci            if (!r300->hyperz_enabled &&
291bf215546Sopenharmony_ci                (r300->screen->caps.is_r500 || debug_get_option_hyperz())) {
292bf215546Sopenharmony_ci                r300->hyperz_enabled =
293bf215546Sopenharmony_ci                    r300->rws->cs_request_feature(&r300->cs,
294bf215546Sopenharmony_ci                                                RADEON_FID_R300_HYPERZ_ACCESS,
295bf215546Sopenharmony_ci                                                TRUE);
296bf215546Sopenharmony_ci                if (r300->hyperz_enabled) {
297bf215546Sopenharmony_ci                   /* Need to emit HyperZ buffer regs for the first time. */
298bf215546Sopenharmony_ci                   r300_mark_fb_state_dirty(r300, R300_CHANGED_HYPERZ_FLAG);
299bf215546Sopenharmony_ci                }
300bf215546Sopenharmony_ci            }
301bf215546Sopenharmony_ci
302bf215546Sopenharmony_ci            /* Setup Hyper-Z clears. */
303bf215546Sopenharmony_ci            if (r300->hyperz_enabled) {
304bf215546Sopenharmony_ci                if (zmask_clear) {
305bf215546Sopenharmony_ci                    hyperz_dcv = hyperz->zb_depthclearvalue =
306bf215546Sopenharmony_ci                        r300_depth_clear_value(fb->zsbuf->format, depth, stencil);
307bf215546Sopenharmony_ci
308bf215546Sopenharmony_ci                    r300_mark_atom_dirty(r300, &r300->zmask_clear);
309bf215546Sopenharmony_ci                    r300_mark_atom_dirty(r300, &r300->gpu_flush);
310bf215546Sopenharmony_ci                    buffers &= ~PIPE_CLEAR_DEPTHSTENCIL;
311bf215546Sopenharmony_ci                }
312bf215546Sopenharmony_ci
313bf215546Sopenharmony_ci                if (hiz_clear) {
314bf215546Sopenharmony_ci                    r300->hiz_clear_value = r300_hiz_clear_value(depth);
315bf215546Sopenharmony_ci                    r300_mark_atom_dirty(r300, &r300->hiz_clear);
316bf215546Sopenharmony_ci                    r300_mark_atom_dirty(r300, &r300->gpu_flush);
317bf215546Sopenharmony_ci                }
318bf215546Sopenharmony_ci                r300->num_z_clears++;
319bf215546Sopenharmony_ci            }
320bf215546Sopenharmony_ci        }
321bf215546Sopenharmony_ci    }
322bf215546Sopenharmony_ci
323bf215546Sopenharmony_ci    /* Use fast color clear for an AA colorbuffer.
324bf215546Sopenharmony_ci     * The CMASK is shared between all colorbuffers, so we use it
325bf215546Sopenharmony_ci     * if there is only one colorbuffer bound. */
326bf215546Sopenharmony_ci    if ((buffers & PIPE_CLEAR_COLOR) && fb->nr_cbufs == 1 && fb->cbufs[0] &&
327bf215546Sopenharmony_ci        r300_resource(fb->cbufs[0]->texture)->tex.cmask_dwords) {
328bf215546Sopenharmony_ci        /* Try to obtain the access to the CMASK if we don't have one. */
329bf215546Sopenharmony_ci        if (!r300->cmask_access) {
330bf215546Sopenharmony_ci            r300->cmask_access =
331bf215546Sopenharmony_ci                r300->rws->cs_request_feature(&r300->cs,
332bf215546Sopenharmony_ci                                              RADEON_FID_R300_CMASK_ACCESS,
333bf215546Sopenharmony_ci                                              TRUE);
334bf215546Sopenharmony_ci        }
335bf215546Sopenharmony_ci
336bf215546Sopenharmony_ci        /* Setup the clear. */
337bf215546Sopenharmony_ci        if (r300->cmask_access) {
338bf215546Sopenharmony_ci            /* Pair the resource with the CMASK to avoid other resources
339bf215546Sopenharmony_ci             * accessing it. */
340bf215546Sopenharmony_ci            if (!r300->screen->cmask_resource) {
341bf215546Sopenharmony_ci                mtx_lock(&r300->screen->cmask_mutex);
342bf215546Sopenharmony_ci                /* Double checking (first unlocked, then locked). */
343bf215546Sopenharmony_ci                if (!r300->screen->cmask_resource) {
344bf215546Sopenharmony_ci                    /* Don't reference this, so that the texture can be
345bf215546Sopenharmony_ci                     * destroyed while set in cmask_resource.
346bf215546Sopenharmony_ci                     * Then in texture_destroy, we set cmask_resource to NULL. */
347bf215546Sopenharmony_ci                    r300->screen->cmask_resource = fb->cbufs[0]->texture;
348bf215546Sopenharmony_ci                }
349bf215546Sopenharmony_ci                mtx_unlock(&r300->screen->cmask_mutex);
350bf215546Sopenharmony_ci            }
351bf215546Sopenharmony_ci
352bf215546Sopenharmony_ci            if (r300->screen->cmask_resource == fb->cbufs[0]->texture) {
353bf215546Sopenharmony_ci                r300_set_clear_color(r300, color);
354bf215546Sopenharmony_ci                r300_mark_atom_dirty(r300, &r300->cmask_clear);
355bf215546Sopenharmony_ci                r300_mark_atom_dirty(r300, &r300->gpu_flush);
356bf215546Sopenharmony_ci                buffers &= ~PIPE_CLEAR_COLOR;
357bf215546Sopenharmony_ci            }
358bf215546Sopenharmony_ci        }
359bf215546Sopenharmony_ci    }
360bf215546Sopenharmony_ci    /* Enable CBZB clear. */
361bf215546Sopenharmony_ci    else if (r300_cbzb_clear_allowed(r300, buffers)) {
362bf215546Sopenharmony_ci        struct r300_surface *surf = r300_surface(fb->cbufs[0]);
363bf215546Sopenharmony_ci
364bf215546Sopenharmony_ci        hyperz->zb_depthclearvalue =
365bf215546Sopenharmony_ci                r300_depth_clear_cb_value(surf->base.format, color->f);
366bf215546Sopenharmony_ci
367bf215546Sopenharmony_ci        width = surf->cbzb_width;
368bf215546Sopenharmony_ci        height = surf->cbzb_height;
369bf215546Sopenharmony_ci
370bf215546Sopenharmony_ci        r300->cbzb_clear = TRUE;
371bf215546Sopenharmony_ci        r300_mark_fb_state_dirty(r300, R300_CHANGED_HYPERZ_FLAG);
372bf215546Sopenharmony_ci    }
373bf215546Sopenharmony_ci
374bf215546Sopenharmony_ci    /* Clear. */
375bf215546Sopenharmony_ci    if (buffers) {
376bf215546Sopenharmony_ci        /* Clear using the blitter. */
377bf215546Sopenharmony_ci        r300_blitter_begin(r300, R300_CLEAR);
378bf215546Sopenharmony_ci        util_blitter_clear(r300->blitter, width, height, 1,
379bf215546Sopenharmony_ci                           buffers, color, depth, stencil,
380bf215546Sopenharmony_ci                           util_framebuffer_get_num_samples(fb) > 1);
381bf215546Sopenharmony_ci        r300_blitter_end(r300);
382bf215546Sopenharmony_ci    } else if (r300->zmask_clear.dirty ||
383bf215546Sopenharmony_ci               r300->hiz_clear.dirty ||
384bf215546Sopenharmony_ci               r300->cmask_clear.dirty) {
385bf215546Sopenharmony_ci        /* Just clear zmask and hiz now, this does not use the standard draw
386bf215546Sopenharmony_ci         * procedure. */
387bf215546Sopenharmony_ci        /* Calculate zmask_clear and hiz_clear atom sizes. */
388bf215546Sopenharmony_ci        unsigned dwords =
389bf215546Sopenharmony_ci            r300->gpu_flush.size +
390bf215546Sopenharmony_ci            (r300->zmask_clear.dirty ? r300->zmask_clear.size : 0) +
391bf215546Sopenharmony_ci            (r300->hiz_clear.dirty ? r300->hiz_clear.size : 0) +
392bf215546Sopenharmony_ci            (r300->cmask_clear.dirty ? r300->cmask_clear.size : 0) +
393bf215546Sopenharmony_ci            r300_get_num_cs_end_dwords(r300);
394bf215546Sopenharmony_ci
395bf215546Sopenharmony_ci        /* Reserve CS space. */
396bf215546Sopenharmony_ci        if (!r300->rws->cs_check_space(&r300->cs, dwords)) {
397bf215546Sopenharmony_ci            r300_flush(&r300->context, PIPE_FLUSH_ASYNC, NULL);
398bf215546Sopenharmony_ci        }
399bf215546Sopenharmony_ci
400bf215546Sopenharmony_ci        /* Emit clear packets. */
401bf215546Sopenharmony_ci        r300_emit_gpu_flush(r300, r300->gpu_flush.size, r300->gpu_flush.state);
402bf215546Sopenharmony_ci        r300->gpu_flush.dirty = FALSE;
403bf215546Sopenharmony_ci
404bf215546Sopenharmony_ci        if (r300->zmask_clear.dirty) {
405bf215546Sopenharmony_ci            r300_emit_zmask_clear(r300, r300->zmask_clear.size,
406bf215546Sopenharmony_ci                                  r300->zmask_clear.state);
407bf215546Sopenharmony_ci            r300->zmask_clear.dirty = FALSE;
408bf215546Sopenharmony_ci        }
409bf215546Sopenharmony_ci        if (r300->hiz_clear.dirty) {
410bf215546Sopenharmony_ci            r300_emit_hiz_clear(r300, r300->hiz_clear.size,
411bf215546Sopenharmony_ci                                r300->hiz_clear.state);
412bf215546Sopenharmony_ci            r300->hiz_clear.dirty = FALSE;
413bf215546Sopenharmony_ci        }
414bf215546Sopenharmony_ci        if (r300->cmask_clear.dirty) {
415bf215546Sopenharmony_ci            r300_emit_cmask_clear(r300, r300->cmask_clear.size,
416bf215546Sopenharmony_ci                                  r300->cmask_clear.state);
417bf215546Sopenharmony_ci            r300->cmask_clear.dirty = FALSE;
418bf215546Sopenharmony_ci        }
419bf215546Sopenharmony_ci    } else {
420bf215546Sopenharmony_ci        assert(0);
421bf215546Sopenharmony_ci    }
422bf215546Sopenharmony_ci
423bf215546Sopenharmony_ci    /* Disable CBZB clear. */
424bf215546Sopenharmony_ci    if (r300->cbzb_clear) {
425bf215546Sopenharmony_ci        r300->cbzb_clear = FALSE;
426bf215546Sopenharmony_ci        hyperz->zb_depthclearvalue = hyperz_dcv;
427bf215546Sopenharmony_ci        r300_mark_fb_state_dirty(r300, R300_CHANGED_HYPERZ_FLAG);
428bf215546Sopenharmony_ci    }
429bf215546Sopenharmony_ci
430bf215546Sopenharmony_ci    /* Enable fastfill and/or hiz.
431bf215546Sopenharmony_ci     *
432bf215546Sopenharmony_ci     * If we cleared zmask/hiz, it's in use now. The Hyper-Z state update
433bf215546Sopenharmony_ci     * looks if zmask/hiz is in use and programs hardware accordingly. */
434bf215546Sopenharmony_ci    if (r300->zmask_in_use || r300->hiz_in_use) {
435bf215546Sopenharmony_ci        r300_mark_atom_dirty(r300, &r300->hyperz_state);
436bf215546Sopenharmony_ci    }
437bf215546Sopenharmony_ci}
438bf215546Sopenharmony_ci
439bf215546Sopenharmony_ci/* Clear a region of a color surface to a constant value. */
440bf215546Sopenharmony_cistatic void r300_clear_render_target(struct pipe_context *pipe,
441bf215546Sopenharmony_ci                                     struct pipe_surface *dst,
442bf215546Sopenharmony_ci                                     const union pipe_color_union *color,
443bf215546Sopenharmony_ci                                     unsigned dstx, unsigned dsty,
444bf215546Sopenharmony_ci                                     unsigned width, unsigned height,
445bf215546Sopenharmony_ci                                     bool render_condition_enabled)
446bf215546Sopenharmony_ci{
447bf215546Sopenharmony_ci    struct r300_context *r300 = r300_context(pipe);
448bf215546Sopenharmony_ci
449bf215546Sopenharmony_ci    r300_blitter_begin(r300, R300_CLEAR_SURFACE |
450bf215546Sopenharmony_ci                       (render_condition_enabled ? 0 : R300_IGNORE_RENDER_COND));
451bf215546Sopenharmony_ci    util_blitter_clear_render_target(r300->blitter, dst, color,
452bf215546Sopenharmony_ci                                     dstx, dsty, width, height);
453bf215546Sopenharmony_ci    r300_blitter_end(r300);
454bf215546Sopenharmony_ci}
455bf215546Sopenharmony_ci
456bf215546Sopenharmony_ci/* Clear a region of a depth stencil surface. */
457bf215546Sopenharmony_cistatic void r300_clear_depth_stencil(struct pipe_context *pipe,
458bf215546Sopenharmony_ci                                     struct pipe_surface *dst,
459bf215546Sopenharmony_ci                                     unsigned clear_flags,
460bf215546Sopenharmony_ci                                     double depth,
461bf215546Sopenharmony_ci                                     unsigned stencil,
462bf215546Sopenharmony_ci                                     unsigned dstx, unsigned dsty,
463bf215546Sopenharmony_ci                                     unsigned width, unsigned height,
464bf215546Sopenharmony_ci                                     bool render_condition_enabled)
465bf215546Sopenharmony_ci{
466bf215546Sopenharmony_ci    struct r300_context *r300 = r300_context(pipe);
467bf215546Sopenharmony_ci    struct pipe_framebuffer_state *fb =
468bf215546Sopenharmony_ci        (struct pipe_framebuffer_state*)r300->fb_state.state;
469bf215546Sopenharmony_ci
470bf215546Sopenharmony_ci    if (r300->zmask_in_use && !r300->locked_zbuffer) {
471bf215546Sopenharmony_ci        if (fb->zsbuf->texture == dst->texture) {
472bf215546Sopenharmony_ci            r300_decompress_zmask(r300);
473bf215546Sopenharmony_ci        }
474bf215546Sopenharmony_ci    }
475bf215546Sopenharmony_ci
476bf215546Sopenharmony_ci    /* XXX Do not decompress ZMask of the currently-set zbuffer. */
477bf215546Sopenharmony_ci    r300_blitter_begin(r300, R300_CLEAR_SURFACE |
478bf215546Sopenharmony_ci                       (render_condition_enabled ? 0 : R300_IGNORE_RENDER_COND));
479bf215546Sopenharmony_ci    util_blitter_clear_depth_stencil(r300->blitter, dst, clear_flags, depth, stencil,
480bf215546Sopenharmony_ci                                     dstx, dsty, width, height);
481bf215546Sopenharmony_ci    r300_blitter_end(r300);
482bf215546Sopenharmony_ci}
483bf215546Sopenharmony_ci
484bf215546Sopenharmony_civoid r300_decompress_zmask(struct r300_context *r300)
485bf215546Sopenharmony_ci{
486bf215546Sopenharmony_ci    struct pipe_framebuffer_state *fb =
487bf215546Sopenharmony_ci        (struct pipe_framebuffer_state*)r300->fb_state.state;
488bf215546Sopenharmony_ci
489bf215546Sopenharmony_ci    if (!r300->zmask_in_use || r300->locked_zbuffer)
490bf215546Sopenharmony_ci        return;
491bf215546Sopenharmony_ci
492bf215546Sopenharmony_ci    r300->zmask_decompress = TRUE;
493bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->hyperz_state);
494bf215546Sopenharmony_ci
495bf215546Sopenharmony_ci    r300_blitter_begin(r300, R300_DECOMPRESS);
496bf215546Sopenharmony_ci    util_blitter_custom_clear_depth(r300->blitter, fb->width, fb->height, 0,
497bf215546Sopenharmony_ci                                    r300->dsa_decompress_zmask);
498bf215546Sopenharmony_ci    r300_blitter_end(r300);
499bf215546Sopenharmony_ci
500bf215546Sopenharmony_ci    r300->zmask_decompress = FALSE;
501bf215546Sopenharmony_ci    r300->zmask_in_use = FALSE;
502bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->hyperz_state);
503bf215546Sopenharmony_ci}
504bf215546Sopenharmony_ci
505bf215546Sopenharmony_civoid r300_decompress_zmask_locked_unsafe(struct r300_context *r300)
506bf215546Sopenharmony_ci{
507bf215546Sopenharmony_ci    struct pipe_framebuffer_state fb;
508bf215546Sopenharmony_ci
509bf215546Sopenharmony_ci    memset(&fb, 0, sizeof(fb));
510bf215546Sopenharmony_ci    fb.width = r300->locked_zbuffer->width;
511bf215546Sopenharmony_ci    fb.height = r300->locked_zbuffer->height;
512bf215546Sopenharmony_ci    fb.zsbuf = r300->locked_zbuffer;
513bf215546Sopenharmony_ci
514bf215546Sopenharmony_ci    r300->context.set_framebuffer_state(&r300->context, &fb);
515bf215546Sopenharmony_ci    r300_decompress_zmask(r300);
516bf215546Sopenharmony_ci}
517bf215546Sopenharmony_ci
518bf215546Sopenharmony_civoid r300_decompress_zmask_locked(struct r300_context *r300)
519bf215546Sopenharmony_ci{
520bf215546Sopenharmony_ci    struct pipe_framebuffer_state saved_fb;
521bf215546Sopenharmony_ci
522bf215546Sopenharmony_ci    memset(&saved_fb, 0, sizeof(saved_fb));
523bf215546Sopenharmony_ci    util_copy_framebuffer_state(&saved_fb, r300->fb_state.state);
524bf215546Sopenharmony_ci    r300_decompress_zmask_locked_unsafe(r300);
525bf215546Sopenharmony_ci    r300->context.set_framebuffer_state(&r300->context, &saved_fb);
526bf215546Sopenharmony_ci    util_unreference_framebuffer_state(&saved_fb);
527bf215546Sopenharmony_ci
528bf215546Sopenharmony_ci    pipe_surface_reference(&r300->locked_zbuffer, NULL);
529bf215546Sopenharmony_ci}
530bf215546Sopenharmony_ci
531bf215546Sopenharmony_cibool r300_is_blit_supported(enum pipe_format format)
532bf215546Sopenharmony_ci{
533bf215546Sopenharmony_ci    const struct util_format_description *desc =
534bf215546Sopenharmony_ci        util_format_description(format);
535bf215546Sopenharmony_ci
536bf215546Sopenharmony_ci    return desc->layout == UTIL_FORMAT_LAYOUT_PLAIN ||
537bf215546Sopenharmony_ci           desc->layout == UTIL_FORMAT_LAYOUT_S3TC ||
538bf215546Sopenharmony_ci           desc->layout == UTIL_FORMAT_LAYOUT_RGTC;
539bf215546Sopenharmony_ci}
540bf215546Sopenharmony_ci
541bf215546Sopenharmony_ci/* Copy a block of pixels from one surface to another. */
542bf215546Sopenharmony_cistatic void r300_resource_copy_region(struct pipe_context *pipe,
543bf215546Sopenharmony_ci                                      struct pipe_resource *dst,
544bf215546Sopenharmony_ci                                      unsigned dst_level,
545bf215546Sopenharmony_ci                                      unsigned dstx, unsigned dsty, unsigned dstz,
546bf215546Sopenharmony_ci                                      struct pipe_resource *src,
547bf215546Sopenharmony_ci                                      unsigned src_level,
548bf215546Sopenharmony_ci                                      const struct pipe_box *src_box)
549bf215546Sopenharmony_ci{
550bf215546Sopenharmony_ci    struct pipe_screen *screen = pipe->screen;
551bf215546Sopenharmony_ci    struct r300_context *r300 = r300_context(pipe);
552bf215546Sopenharmony_ci    struct pipe_framebuffer_state *fb =
553bf215546Sopenharmony_ci        (struct pipe_framebuffer_state*)r300->fb_state.state;
554bf215546Sopenharmony_ci    unsigned src_width0 = r300_resource(src)->tex.width0;
555bf215546Sopenharmony_ci    unsigned src_height0 = r300_resource(src)->tex.height0;
556bf215546Sopenharmony_ci    unsigned dst_width0 = r300_resource(dst)->tex.width0;
557bf215546Sopenharmony_ci    unsigned dst_height0 = r300_resource(dst)->tex.height0;
558bf215546Sopenharmony_ci    unsigned layout;
559bf215546Sopenharmony_ci    struct pipe_box box, dstbox;
560bf215546Sopenharmony_ci    struct pipe_sampler_view src_templ, *src_view;
561bf215546Sopenharmony_ci    struct pipe_surface dst_templ, *dst_view;
562bf215546Sopenharmony_ci
563bf215546Sopenharmony_ci    /* Fallback for buffers. */
564bf215546Sopenharmony_ci    if ((dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) ||
565bf215546Sopenharmony_ci        !r300_is_blit_supported(dst->format)) {
566bf215546Sopenharmony_ci        util_resource_copy_region(pipe, dst, dst_level, dstx, dsty, dstz,
567bf215546Sopenharmony_ci                                  src, src_level, src_box);
568bf215546Sopenharmony_ci        return;
569bf215546Sopenharmony_ci    }
570bf215546Sopenharmony_ci
571bf215546Sopenharmony_ci    /* Can't read MSAA textures. */
572bf215546Sopenharmony_ci    if (src->nr_samples > 1 || dst->nr_samples > 1) {
573bf215546Sopenharmony_ci        return;
574bf215546Sopenharmony_ci    }
575bf215546Sopenharmony_ci
576bf215546Sopenharmony_ci    /* The code below changes the texture format so that the copy can be done
577bf215546Sopenharmony_ci     * on hardware. E.g. depth-stencil surfaces are copied as RGBA
578bf215546Sopenharmony_ci     * colorbuffers. */
579bf215546Sopenharmony_ci
580bf215546Sopenharmony_ci    util_blitter_default_dst_texture(&dst_templ, dst, dst_level, dstz);
581bf215546Sopenharmony_ci    util_blitter_default_src_texture(r300->blitter, &src_templ, src, src_level);
582bf215546Sopenharmony_ci
583bf215546Sopenharmony_ci    layout = util_format_description(dst_templ.format)->layout;
584bf215546Sopenharmony_ci
585bf215546Sopenharmony_ci    /* Handle non-renderable plain formats. */
586bf215546Sopenharmony_ci    if (layout == UTIL_FORMAT_LAYOUT_PLAIN &&
587bf215546Sopenharmony_ci        (!screen->is_format_supported(screen, src_templ.format, src->target,
588bf215546Sopenharmony_ci                                      src->nr_samples, src->nr_storage_samples,
589bf215546Sopenharmony_ci                                      PIPE_BIND_SAMPLER_VIEW) ||
590bf215546Sopenharmony_ci         !screen->is_format_supported(screen, dst_templ.format, dst->target,
591bf215546Sopenharmony_ci                                      dst->nr_samples, dst->nr_storage_samples,
592bf215546Sopenharmony_ci                                      PIPE_BIND_RENDER_TARGET))) {
593bf215546Sopenharmony_ci        switch (util_format_get_blocksize(dst_templ.format)) {
594bf215546Sopenharmony_ci            case 1:
595bf215546Sopenharmony_ci                dst_templ.format = PIPE_FORMAT_I8_UNORM;
596bf215546Sopenharmony_ci                break;
597bf215546Sopenharmony_ci            case 2:
598bf215546Sopenharmony_ci                dst_templ.format = PIPE_FORMAT_B4G4R4A4_UNORM;
599bf215546Sopenharmony_ci                break;
600bf215546Sopenharmony_ci            case 4:
601bf215546Sopenharmony_ci                dst_templ.format = PIPE_FORMAT_B8G8R8A8_UNORM;
602bf215546Sopenharmony_ci                break;
603bf215546Sopenharmony_ci            case 8:
604bf215546Sopenharmony_ci                dst_templ.format = PIPE_FORMAT_R16G16B16A16_UNORM;
605bf215546Sopenharmony_ci                break;
606bf215546Sopenharmony_ci            default:
607bf215546Sopenharmony_ci                debug_printf("r300: copy_region: Unhandled format: %s. Falling back to software.\n"
608bf215546Sopenharmony_ci                             "r300: copy_region: Software fallback doesn't work for tiled textures.\n",
609bf215546Sopenharmony_ci                             util_format_short_name(dst_templ.format));
610bf215546Sopenharmony_ci        }
611bf215546Sopenharmony_ci        src_templ.format = dst_templ.format;
612bf215546Sopenharmony_ci    }
613bf215546Sopenharmony_ci
614bf215546Sopenharmony_ci    /* Handle compressed formats. */
615bf215546Sopenharmony_ci    if (layout == UTIL_FORMAT_LAYOUT_S3TC ||
616bf215546Sopenharmony_ci        layout == UTIL_FORMAT_LAYOUT_RGTC) {
617bf215546Sopenharmony_ci        assert(src_templ.format == dst_templ.format);
618bf215546Sopenharmony_ci
619bf215546Sopenharmony_ci        box = *src_box;
620bf215546Sopenharmony_ci        src_box = &box;
621bf215546Sopenharmony_ci
622bf215546Sopenharmony_ci        dst_width0 = align(dst_width0, 4);
623bf215546Sopenharmony_ci        dst_height0 = align(dst_height0, 4);
624bf215546Sopenharmony_ci        src_width0 = align(src_width0, 4);
625bf215546Sopenharmony_ci        src_height0 = align(src_height0, 4);
626bf215546Sopenharmony_ci        box.width = align(box.width, 4);
627bf215546Sopenharmony_ci        box.height = align(box.height, 4);
628bf215546Sopenharmony_ci
629bf215546Sopenharmony_ci        switch (util_format_get_blocksize(dst_templ.format)) {
630bf215546Sopenharmony_ci        case 8:
631bf215546Sopenharmony_ci            /* one 4x4 pixel block has 8 bytes.
632bf215546Sopenharmony_ci             * we set 1 pixel = 4 bytes ===> 1 block corrensponds to 2 pixels. */
633bf215546Sopenharmony_ci            dst_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
634bf215546Sopenharmony_ci            dst_width0 = dst_width0 / 2;
635bf215546Sopenharmony_ci            src_width0 = src_width0 / 2;
636bf215546Sopenharmony_ci            dstx /= 2;
637bf215546Sopenharmony_ci            box.x /= 2;
638bf215546Sopenharmony_ci            box.width /= 2;
639bf215546Sopenharmony_ci            break;
640bf215546Sopenharmony_ci        case 16:
641bf215546Sopenharmony_ci            /* one 4x4 pixel block has 16 bytes.
642bf215546Sopenharmony_ci             * we set 1 pixel = 4 bytes ===> 1 block corresponds to 4 pixels. */
643bf215546Sopenharmony_ci            dst_templ.format = PIPE_FORMAT_R8G8B8A8_UNORM;
644bf215546Sopenharmony_ci            break;
645bf215546Sopenharmony_ci        }
646bf215546Sopenharmony_ci        src_templ.format = dst_templ.format;
647bf215546Sopenharmony_ci
648bf215546Sopenharmony_ci        dst_height0 = dst_height0 / 4;
649bf215546Sopenharmony_ci        src_height0 = src_height0 / 4;
650bf215546Sopenharmony_ci        dsty /= 4;
651bf215546Sopenharmony_ci        box.y /= 4;
652bf215546Sopenharmony_ci        box.height /= 4;
653bf215546Sopenharmony_ci    }
654bf215546Sopenharmony_ci
655bf215546Sopenharmony_ci    /* Fallback for textures. */
656bf215546Sopenharmony_ci    if (!screen->is_format_supported(screen, dst_templ.format,
657bf215546Sopenharmony_ci                                     dst->target, dst->nr_samples,
658bf215546Sopenharmony_ci                                     dst->nr_storage_samples,
659bf215546Sopenharmony_ci                                     PIPE_BIND_RENDER_TARGET) ||
660bf215546Sopenharmony_ci	!screen->is_format_supported(screen, src_templ.format,
661bf215546Sopenharmony_ci                                     src->target, src->nr_samples,
662bf215546Sopenharmony_ci                                     src->nr_storage_samples,
663bf215546Sopenharmony_ci                                     PIPE_BIND_SAMPLER_VIEW)) {
664bf215546Sopenharmony_ci        assert(0 && "this shouldn't happen, update r300_is_blit_supported");
665bf215546Sopenharmony_ci        util_resource_copy_region(pipe, dst, dst_level, dstx, dsty, dstz,
666bf215546Sopenharmony_ci                                  src, src_level, src_box);
667bf215546Sopenharmony_ci        return;
668bf215546Sopenharmony_ci    }
669bf215546Sopenharmony_ci
670bf215546Sopenharmony_ci    /* Decompress ZMASK. */
671bf215546Sopenharmony_ci    if (r300->zmask_in_use && !r300->locked_zbuffer) {
672bf215546Sopenharmony_ci        if (fb->zsbuf->texture == src ||
673bf215546Sopenharmony_ci            fb->zsbuf->texture == dst) {
674bf215546Sopenharmony_ci            r300_decompress_zmask(r300);
675bf215546Sopenharmony_ci        }
676bf215546Sopenharmony_ci    }
677bf215546Sopenharmony_ci
678bf215546Sopenharmony_ci    dst_view = r300_create_surface_custom(pipe, dst, &dst_templ, dst_width0, dst_height0);
679bf215546Sopenharmony_ci    src_view = r300_create_sampler_view_custom(pipe, src, &src_templ, src_width0, src_height0);
680bf215546Sopenharmony_ci
681bf215546Sopenharmony_ci    u_box_3d(dstx, dsty, dstz, abs(src_box->width), abs(src_box->height),
682bf215546Sopenharmony_ci             abs(src_box->depth), &dstbox);
683bf215546Sopenharmony_ci
684bf215546Sopenharmony_ci    r300_blitter_begin(r300, R300_COPY);
685bf215546Sopenharmony_ci    util_blitter_blit_generic(r300->blitter, dst_view, &dstbox,
686bf215546Sopenharmony_ci                              src_view, src_box, src_width0, src_height0,
687bf215546Sopenharmony_ci                              PIPE_MASK_RGBAZS, PIPE_TEX_FILTER_NEAREST, NULL,
688bf215546Sopenharmony_ci                              FALSE, FALSE, 0);
689bf215546Sopenharmony_ci    r300_blitter_end(r300);
690bf215546Sopenharmony_ci
691bf215546Sopenharmony_ci    pipe_surface_reference(&dst_view, NULL);
692bf215546Sopenharmony_ci    pipe_sampler_view_reference(&src_view, NULL);
693bf215546Sopenharmony_ci}
694bf215546Sopenharmony_ci
695bf215546Sopenharmony_cistatic boolean r300_is_simple_msaa_resolve(const struct pipe_blit_info *info)
696bf215546Sopenharmony_ci{
697bf215546Sopenharmony_ci    unsigned dst_width = u_minify(info->dst.resource->width0, info->dst.level);
698bf215546Sopenharmony_ci    unsigned dst_height = u_minify(info->dst.resource->height0, info->dst.level);
699bf215546Sopenharmony_ci
700bf215546Sopenharmony_ci    return info->src.resource->nr_samples > 1 &&
701bf215546Sopenharmony_ci           info->dst.resource->nr_samples <= 1 &&
702bf215546Sopenharmony_ci           info->dst.resource->format == info->src.resource->format &&
703bf215546Sopenharmony_ci           info->dst.resource->format == info->dst.format &&
704bf215546Sopenharmony_ci           info->src.resource->format == info->src.format &&
705bf215546Sopenharmony_ci           !info->scissor_enable &&
706bf215546Sopenharmony_ci           info->mask == PIPE_MASK_RGBA &&
707bf215546Sopenharmony_ci           dst_width == info->src.resource->width0 &&
708bf215546Sopenharmony_ci           dst_height == info->src.resource->height0 &&
709bf215546Sopenharmony_ci           info->dst.box.x == 0 &&
710bf215546Sopenharmony_ci           info->dst.box.y == 0 &&
711bf215546Sopenharmony_ci           info->dst.box.width == dst_width &&
712bf215546Sopenharmony_ci           info->dst.box.height == dst_height &&
713bf215546Sopenharmony_ci           info->src.box.x == 0 &&
714bf215546Sopenharmony_ci           info->src.box.y == 0 &&
715bf215546Sopenharmony_ci           info->src.box.width == dst_width &&
716bf215546Sopenharmony_ci           info->src.box.height == dst_height &&
717bf215546Sopenharmony_ci           (r300_resource(info->dst.resource)->tex.microtile != RADEON_LAYOUT_LINEAR ||
718bf215546Sopenharmony_ci            r300_resource(info->dst.resource)->tex.macrotile[info->dst.level] != RADEON_LAYOUT_LINEAR);
719bf215546Sopenharmony_ci}
720bf215546Sopenharmony_ci
721bf215546Sopenharmony_cistatic void r300_simple_msaa_resolve(struct pipe_context *pipe,
722bf215546Sopenharmony_ci                                     struct pipe_resource *dst,
723bf215546Sopenharmony_ci                                     unsigned dst_level,
724bf215546Sopenharmony_ci                                     unsigned dst_layer,
725bf215546Sopenharmony_ci                                     struct pipe_resource *src,
726bf215546Sopenharmony_ci                                     enum pipe_format format)
727bf215546Sopenharmony_ci{
728bf215546Sopenharmony_ci    struct r300_context *r300 = r300_context(pipe);
729bf215546Sopenharmony_ci    struct r300_surface *srcsurf, *dstsurf;
730bf215546Sopenharmony_ci    struct pipe_surface surf_tmpl;
731bf215546Sopenharmony_ci    struct r300_aa_state *aa = (struct r300_aa_state*)r300->aa_state.state;
732bf215546Sopenharmony_ci
733bf215546Sopenharmony_ci    memset(&surf_tmpl, 0, sizeof(surf_tmpl));
734bf215546Sopenharmony_ci    surf_tmpl.format = format;
735bf215546Sopenharmony_ci    srcsurf = r300_surface(pipe->create_surface(pipe, src, &surf_tmpl));
736bf215546Sopenharmony_ci
737bf215546Sopenharmony_ci    surf_tmpl.format = format;
738bf215546Sopenharmony_ci    surf_tmpl.u.tex.level = dst_level;
739bf215546Sopenharmony_ci    surf_tmpl.u.tex.first_layer =
740bf215546Sopenharmony_ci    surf_tmpl.u.tex.last_layer = dst_layer;
741bf215546Sopenharmony_ci    dstsurf = r300_surface(pipe->create_surface(pipe, dst, &surf_tmpl));
742bf215546Sopenharmony_ci
743bf215546Sopenharmony_ci    /* COLORPITCH should contain the tiling info of the resolve buffer.
744bf215546Sopenharmony_ci     * The tiling of the AA buffer isn't programmable anyway. */
745bf215546Sopenharmony_ci    srcsurf->pitch &= ~(R300_COLOR_TILE(1) | R300_COLOR_MICROTILE(3));
746bf215546Sopenharmony_ci    srcsurf->pitch |= dstsurf->pitch & (R300_COLOR_TILE(1) | R300_COLOR_MICROTILE(3));
747bf215546Sopenharmony_ci
748bf215546Sopenharmony_ci    /* Enable AA resolve. */
749bf215546Sopenharmony_ci    aa->dest = dstsurf;
750bf215546Sopenharmony_ci    r300->aa_state.size = 8;
751bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->aa_state);
752bf215546Sopenharmony_ci
753bf215546Sopenharmony_ci    /* Resolve the surface. */
754bf215546Sopenharmony_ci    r300_blitter_begin(r300, R300_CLEAR_SURFACE);
755bf215546Sopenharmony_ci    util_blitter_custom_color(r300->blitter, &srcsurf->base, NULL);
756bf215546Sopenharmony_ci    r300_blitter_end(r300);
757bf215546Sopenharmony_ci
758bf215546Sopenharmony_ci    /* Disable AA resolve. */
759bf215546Sopenharmony_ci    aa->dest = NULL;
760bf215546Sopenharmony_ci    r300->aa_state.size = 4;
761bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->aa_state);
762bf215546Sopenharmony_ci
763bf215546Sopenharmony_ci    pipe_surface_reference((struct pipe_surface**)&srcsurf, NULL);
764bf215546Sopenharmony_ci    pipe_surface_reference((struct pipe_surface**)&dstsurf, NULL);
765bf215546Sopenharmony_ci}
766bf215546Sopenharmony_ci
767bf215546Sopenharmony_cistatic void r300_msaa_resolve(struct pipe_context *pipe,
768bf215546Sopenharmony_ci                              const struct pipe_blit_info *info)
769bf215546Sopenharmony_ci{
770bf215546Sopenharmony_ci    struct r300_context *r300 = r300_context(pipe);
771bf215546Sopenharmony_ci    struct pipe_screen *screen = pipe->screen;
772bf215546Sopenharmony_ci    struct pipe_resource *tmp, templ;
773bf215546Sopenharmony_ci    struct pipe_blit_info blit;
774bf215546Sopenharmony_ci
775bf215546Sopenharmony_ci    assert(info->src.level == 0);
776bf215546Sopenharmony_ci    assert(info->src.box.z == 0);
777bf215546Sopenharmony_ci    assert(info->src.box.depth == 1);
778bf215546Sopenharmony_ci    assert(info->dst.box.depth == 1);
779bf215546Sopenharmony_ci
780bf215546Sopenharmony_ci    if (r300_is_simple_msaa_resolve(info)) {
781bf215546Sopenharmony_ci        r300_simple_msaa_resolve(pipe, info->dst.resource, info->dst.level,
782bf215546Sopenharmony_ci                                 info->dst.box.z, info->src.resource,
783bf215546Sopenharmony_ci                                 info->src.format);
784bf215546Sopenharmony_ci        return;
785bf215546Sopenharmony_ci    }
786bf215546Sopenharmony_ci
787bf215546Sopenharmony_ci    /* resolve into a temporary texture, then blit */
788bf215546Sopenharmony_ci    memset(&templ, 0, sizeof(templ));
789bf215546Sopenharmony_ci    templ.target = PIPE_TEXTURE_2D;
790bf215546Sopenharmony_ci    templ.format = info->src.resource->format;
791bf215546Sopenharmony_ci    templ.width0 = info->src.resource->width0;
792bf215546Sopenharmony_ci    templ.height0 = info->src.resource->height0;
793bf215546Sopenharmony_ci    templ.depth0 = 1;
794bf215546Sopenharmony_ci    templ.array_size = 1;
795bf215546Sopenharmony_ci    templ.usage = PIPE_USAGE_DEFAULT;
796bf215546Sopenharmony_ci    templ.flags = R300_RESOURCE_FORCE_MICROTILING;
797bf215546Sopenharmony_ci
798bf215546Sopenharmony_ci    tmp = screen->resource_create(screen, &templ);
799bf215546Sopenharmony_ci
800bf215546Sopenharmony_ci    /* resolve */
801bf215546Sopenharmony_ci    r300_simple_msaa_resolve(pipe, tmp, 0, 0, info->src.resource,
802bf215546Sopenharmony_ci                             info->src.format);
803bf215546Sopenharmony_ci
804bf215546Sopenharmony_ci    /* blit */
805bf215546Sopenharmony_ci    blit = *info;
806bf215546Sopenharmony_ci    blit.src.resource = tmp;
807bf215546Sopenharmony_ci    blit.src.box.z = 0;
808bf215546Sopenharmony_ci
809bf215546Sopenharmony_ci    r300_blitter_begin(r300, R300_BLIT | R300_IGNORE_RENDER_COND);
810bf215546Sopenharmony_ci    util_blitter_blit(r300->blitter, &blit);
811bf215546Sopenharmony_ci    r300_blitter_end(r300);
812bf215546Sopenharmony_ci
813bf215546Sopenharmony_ci    pipe_resource_reference(&tmp, NULL);
814bf215546Sopenharmony_ci}
815bf215546Sopenharmony_ci
816bf215546Sopenharmony_cistatic void r300_blit(struct pipe_context *pipe,
817bf215546Sopenharmony_ci                      const struct pipe_blit_info *blit)
818bf215546Sopenharmony_ci{
819bf215546Sopenharmony_ci    struct r300_context *r300 = r300_context(pipe);
820bf215546Sopenharmony_ci    struct pipe_framebuffer_state *fb =
821bf215546Sopenharmony_ci        (struct pipe_framebuffer_state*)r300->fb_state.state;
822bf215546Sopenharmony_ci    struct pipe_blit_info info = *blit;
823bf215546Sopenharmony_ci
824bf215546Sopenharmony_ci    /* The driver supports sRGB textures but not framebuffers. Blitting
825bf215546Sopenharmony_ci     * from sRGB to sRGB should be the same as blitting from linear
826bf215546Sopenharmony_ci     * to linear, so use that, This avoids incorrect linearization.
827bf215546Sopenharmony_ci     */
828bf215546Sopenharmony_ci    if (util_format_is_srgb(info.src.format)) {
829bf215546Sopenharmony_ci      info.src.format = util_format_linear(info.src.format);
830bf215546Sopenharmony_ci      info.dst.format = util_format_linear(info.dst.format);
831bf215546Sopenharmony_ci    }
832bf215546Sopenharmony_ci
833bf215546Sopenharmony_ci    /* MSAA resolve. */
834bf215546Sopenharmony_ci    if (info.src.resource->nr_samples > 1 &&
835bf215546Sopenharmony_ci        !util_format_is_depth_or_stencil(info.src.resource->format)) {
836bf215546Sopenharmony_ci        r300_msaa_resolve(pipe, &info);
837bf215546Sopenharmony_ci        return;
838bf215546Sopenharmony_ci    }
839bf215546Sopenharmony_ci
840bf215546Sopenharmony_ci    /* Can't read MSAA textures. */
841bf215546Sopenharmony_ci    if (info.src.resource->nr_samples > 1) {
842bf215546Sopenharmony_ci        return;
843bf215546Sopenharmony_ci    }
844bf215546Sopenharmony_ci
845bf215546Sopenharmony_ci    /* Blit a combined depth-stencil resource as color.
846bf215546Sopenharmony_ci     * S8Z24 is the only supported stencil format. */
847bf215546Sopenharmony_ci    if ((info.mask & PIPE_MASK_S) &&
848bf215546Sopenharmony_ci        info.src.format == PIPE_FORMAT_S8_UINT_Z24_UNORM &&
849bf215546Sopenharmony_ci        info.dst.format == PIPE_FORMAT_S8_UINT_Z24_UNORM) {
850bf215546Sopenharmony_ci        if (info.dst.resource->nr_samples > 1) {
851bf215546Sopenharmony_ci            /* Cannot do that with MSAA buffers. */
852bf215546Sopenharmony_ci            info.mask &= ~PIPE_MASK_S;
853bf215546Sopenharmony_ci            if (!(info.mask & PIPE_MASK_Z)) {
854bf215546Sopenharmony_ci                return;
855bf215546Sopenharmony_ci            }
856bf215546Sopenharmony_ci        } else {
857bf215546Sopenharmony_ci            /* Single-sample buffer. */
858bf215546Sopenharmony_ci            info.src.format = PIPE_FORMAT_B8G8R8A8_UNORM;
859bf215546Sopenharmony_ci            info.dst.format = PIPE_FORMAT_B8G8R8A8_UNORM;
860bf215546Sopenharmony_ci            if (info.mask & PIPE_MASK_Z) {
861bf215546Sopenharmony_ci                info.mask = PIPE_MASK_RGBA; /* depth+stencil */
862bf215546Sopenharmony_ci            } else {
863bf215546Sopenharmony_ci                info.mask = PIPE_MASK_B; /* stencil only */
864bf215546Sopenharmony_ci            }
865bf215546Sopenharmony_ci        }
866bf215546Sopenharmony_ci    }
867bf215546Sopenharmony_ci
868bf215546Sopenharmony_ci    /* Decompress ZMASK. */
869bf215546Sopenharmony_ci    if (r300->zmask_in_use && !r300->locked_zbuffer) {
870bf215546Sopenharmony_ci        if (fb->zsbuf->texture == info.src.resource ||
871bf215546Sopenharmony_ci            fb->zsbuf->texture == info.dst.resource) {
872bf215546Sopenharmony_ci            r300_decompress_zmask(r300);
873bf215546Sopenharmony_ci        }
874bf215546Sopenharmony_ci    }
875bf215546Sopenharmony_ci
876bf215546Sopenharmony_ci    r300_blitter_begin(r300, R300_BLIT |
877bf215546Sopenharmony_ci		       (info.render_condition_enable ? 0 : R300_IGNORE_RENDER_COND));
878bf215546Sopenharmony_ci    util_blitter_blit(r300->blitter, &info);
879bf215546Sopenharmony_ci    r300_blitter_end(r300);
880bf215546Sopenharmony_ci}
881bf215546Sopenharmony_ci
882bf215546Sopenharmony_cistatic void r300_flush_resource(struct pipe_context *ctx,
883bf215546Sopenharmony_ci				struct pipe_resource *resource)
884bf215546Sopenharmony_ci{
885bf215546Sopenharmony_ci}
886bf215546Sopenharmony_ci
887bf215546Sopenharmony_civoid r300_init_blit_functions(struct r300_context *r300)
888bf215546Sopenharmony_ci{
889bf215546Sopenharmony_ci    r300->context.clear = r300_clear;
890bf215546Sopenharmony_ci    r300->context.clear_render_target = r300_clear_render_target;
891bf215546Sopenharmony_ci    r300->context.clear_depth_stencil = r300_clear_depth_stencil;
892bf215546Sopenharmony_ci    r300->context.resource_copy_region = r300_resource_copy_region;
893bf215546Sopenharmony_ci    r300->context.blit = r300_blit;
894bf215546Sopenharmony_ci    r300->context.flush_resource = r300_flush_resource;
895bf215546Sopenharmony_ci}
896