1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3bf215546Sopenharmony_ci * Copyright 2010 Marek Olšák <maraeo@gmail.com>
4bf215546Sopenharmony_ci *
5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
6bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
7bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
8bf215546Sopenharmony_ci * on the rights to use, copy, modify, merge, publish, distribute, sub
9bf215546Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom
10bf215546Sopenharmony_ci * the Software is furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci *
12bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
13bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
14bf215546Sopenharmony_ci * Software.
15bf215546Sopenharmony_ci *
16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23bf215546Sopenharmony_ci
24bf215546Sopenharmony_ci#include "draw/draw_context.h"
25bf215546Sopenharmony_ci#include "draw/draw_private.h"
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "util/u_upload_mgr.h"
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include "util/os_time.h"
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include "r300_context.h"
32bf215546Sopenharmony_ci#include "r300_cs.h"
33bf215546Sopenharmony_ci#include "r300_emit.h"
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_cistatic void r300_flush_and_cleanup(struct r300_context *r300, unsigned flags,
37bf215546Sopenharmony_ci                                   struct pipe_fence_handle **fence)
38bf215546Sopenharmony_ci{
39bf215546Sopenharmony_ci    struct r300_atom *atom;
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci    r300_emit_hyperz_end(r300);
42bf215546Sopenharmony_ci    r300_emit_query_end(r300);
43bf215546Sopenharmony_ci    if (r300->screen->caps.is_r500)
44bf215546Sopenharmony_ci        r500_emit_index_bias(r300, 0);
45bf215546Sopenharmony_ci
46bf215546Sopenharmony_ci    /* The DDX doesn't set these regs. */
47bf215546Sopenharmony_ci    {
48bf215546Sopenharmony_ci        CS_LOCALS(r300);
49bf215546Sopenharmony_ci        OUT_CS_REG_SEQ(R300_GB_MSPOS0, 2);
50bf215546Sopenharmony_ci        OUT_CS(0x66666666);
51bf215546Sopenharmony_ci        OUT_CS(0x6666666);
52bf215546Sopenharmony_ci    }
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_ci    r300->flush_counter++;
55bf215546Sopenharmony_ci    r300->rws->cs_flush(&r300->cs, flags, fence);
56bf215546Sopenharmony_ci    r300->dirty_hw = 0;
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci    /* New kitchen sink, baby. */
59bf215546Sopenharmony_ci    foreach_atom(r300, atom) {
60bf215546Sopenharmony_ci        if (atom->state || atom->allow_null_state) {
61bf215546Sopenharmony_ci            r300_mark_atom_dirty(r300, atom);
62bf215546Sopenharmony_ci        }
63bf215546Sopenharmony_ci    }
64bf215546Sopenharmony_ci    r300->vertex_arrays_dirty = TRUE;
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci    /* Unmark HWTCL state for SWTCL. */
67bf215546Sopenharmony_ci    if (!r300->screen->caps.has_tcl) {
68bf215546Sopenharmony_ci        r300->vs_state.dirty = FALSE;
69bf215546Sopenharmony_ci        r300->vs_constants.dirty = FALSE;
70bf215546Sopenharmony_ci        r300->clip_state.dirty = FALSE;
71bf215546Sopenharmony_ci    }
72bf215546Sopenharmony_ci}
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_civoid r300_flush(struct pipe_context *pipe,
75bf215546Sopenharmony_ci                unsigned flags,
76bf215546Sopenharmony_ci                struct pipe_fence_handle **fence)
77bf215546Sopenharmony_ci{
78bf215546Sopenharmony_ci    struct r300_context *r300 = r300_context(pipe);
79bf215546Sopenharmony_ci
80bf215546Sopenharmony_ci    if (r300->dirty_hw) {
81bf215546Sopenharmony_ci        r300_flush_and_cleanup(r300, flags, fence);
82bf215546Sopenharmony_ci    } else {
83bf215546Sopenharmony_ci        if (fence) {
84bf215546Sopenharmony_ci            /* We have to create a fence object, but the command stream is empty
85bf215546Sopenharmony_ci             * and we cannot emit an empty CS. Let's write to some reg. */
86bf215546Sopenharmony_ci            CS_LOCALS(r300);
87bf215546Sopenharmony_ci            OUT_CS_REG(RB3D_COLOR_CHANNEL_MASK, 0);
88bf215546Sopenharmony_ci            r300->rws->cs_flush(&r300->cs, flags, fence);
89bf215546Sopenharmony_ci        } else {
90bf215546Sopenharmony_ci            /* Even if hw is not dirty, we should at least reset the CS in case
91bf215546Sopenharmony_ci             * the space checking failed for the first draw operation. */
92bf215546Sopenharmony_ci            r300->rws->cs_flush(&r300->cs, flags, NULL);
93bf215546Sopenharmony_ci        }
94bf215546Sopenharmony_ci    }
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci    /* Update Hyper-Z status. */
97bf215546Sopenharmony_ci    if (r300->hyperz_enabled) {
98bf215546Sopenharmony_ci        /* If there was a Z clear, keep Hyper-Z access. */
99bf215546Sopenharmony_ci        if (r300->num_z_clears) {
100bf215546Sopenharmony_ci            r300->hyperz_time_of_last_flush = os_time_get();
101bf215546Sopenharmony_ci            r300->num_z_clears = 0;
102bf215546Sopenharmony_ci        } else if (r300->hyperz_time_of_last_flush - os_time_get() > 2000000) {
103bf215546Sopenharmony_ci            /* If there hasn't been a Z clear for 2 seconds, revoke Hyper-Z access. */
104bf215546Sopenharmony_ci            r300->hiz_in_use = FALSE;
105bf215546Sopenharmony_ci
106bf215546Sopenharmony_ci            /* Decompress the Z buffer. */
107bf215546Sopenharmony_ci            if (r300->zmask_in_use) {
108bf215546Sopenharmony_ci                if (r300->locked_zbuffer) {
109bf215546Sopenharmony_ci                    r300_decompress_zmask_locked(r300);
110bf215546Sopenharmony_ci                } else {
111bf215546Sopenharmony_ci                    r300_decompress_zmask(r300);
112bf215546Sopenharmony_ci                }
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci                if (fence && *fence)
115bf215546Sopenharmony_ci                    r300->rws->fence_reference(fence, NULL);
116bf215546Sopenharmony_ci                r300_flush_and_cleanup(r300, flags, fence);
117bf215546Sopenharmony_ci            }
118bf215546Sopenharmony_ci
119bf215546Sopenharmony_ci            /* Revoke Hyper-Z access, so that some other process can take it. */
120bf215546Sopenharmony_ci            r300->rws->cs_request_feature(&r300->cs, RADEON_FID_R300_HYPERZ_ACCESS,
121bf215546Sopenharmony_ci                                          FALSE);
122bf215546Sopenharmony_ci            r300->hyperz_enabled = FALSE;
123bf215546Sopenharmony_ci        }
124bf215546Sopenharmony_ci    }
125bf215546Sopenharmony_ci}
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_cistatic void r300_flush_wrapped(struct pipe_context *pipe,
128bf215546Sopenharmony_ci                               struct pipe_fence_handle **fence,
129bf215546Sopenharmony_ci                               unsigned flags)
130bf215546Sopenharmony_ci{
131bf215546Sopenharmony_ci    if (flags & PIPE_FLUSH_HINT_FINISH)
132bf215546Sopenharmony_ci        flags &= ~PIPE_FLUSH_ASYNC;
133bf215546Sopenharmony_ci
134bf215546Sopenharmony_ci    r300_flush(pipe, flags, fence);
135bf215546Sopenharmony_ci}
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_civoid r300_init_flush_functions(struct r300_context* r300)
138bf215546Sopenharmony_ci{
139bf215546Sopenharmony_ci    r300->context.flush = r300_flush_wrapped;
140bf215546Sopenharmony_ci}
141