1848b8605Smrg/*
2848b8605Smrg * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3848b8605Smrg * Copyright 2010 Marek Olšák <maraeo@gmail.com>
4848b8605Smrg *
5848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
6848b8605Smrg * copy of this software and associated documentation files (the "Software"),
7848b8605Smrg * to deal in the Software without restriction, including without limitation
8848b8605Smrg * on the rights to use, copy, modify, merge, publish, distribute, sub
9848b8605Smrg * license, and/or sell copies of the Software, and to permit persons to whom
10848b8605Smrg * the Software is furnished to do so, subject to the following conditions:
11848b8605Smrg *
12848b8605Smrg * The above copyright notice and this permission notice (including the next
13848b8605Smrg * paragraph) shall be included in all copies or substantial portions of the
14848b8605Smrg * Software.
15848b8605Smrg *
16848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17848b8605Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19848b8605Smrg * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20848b8605Smrg * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21848b8605Smrg * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22848b8605Smrg * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23848b8605Smrg
24848b8605Smrg#include "draw/draw_context.h"
25848b8605Smrg#include "draw/draw_private.h"
26848b8605Smrg
27b8e80941Smrg#include "util/simple_list.h"
28848b8605Smrg#include "util/u_upload_mgr.h"
29848b8605Smrg
30b8e80941Smrg#include "util/os_time.h"
31848b8605Smrg
32848b8605Smrg#include "r300_context.h"
33848b8605Smrg#include "r300_cs.h"
34848b8605Smrg#include "r300_emit.h"
35848b8605Smrg
36848b8605Smrg
37848b8605Smrgstatic void r300_flush_and_cleanup(struct r300_context *r300, unsigned flags,
38848b8605Smrg                                   struct pipe_fence_handle **fence)
39848b8605Smrg{
40848b8605Smrg    struct r300_atom *atom;
41848b8605Smrg
42848b8605Smrg    r300_emit_hyperz_end(r300);
43848b8605Smrg    r300_emit_query_end(r300);
44848b8605Smrg    if (r300->screen->caps.is_r500)
45848b8605Smrg        r500_emit_index_bias(r300, 0);
46848b8605Smrg
47848b8605Smrg    /* The DDX doesn't set these regs. */
48b8e80941Smrg    {
49848b8605Smrg        CS_LOCALS(r300);
50848b8605Smrg        OUT_CS_REG_SEQ(R300_GB_MSPOS0, 2);
51848b8605Smrg        OUT_CS(0x66666666);
52848b8605Smrg        OUT_CS(0x6666666);
53848b8605Smrg    }
54848b8605Smrg
55848b8605Smrg    r300->flush_counter++;
56b8e80941Smrg    r300->rws->cs_flush(r300->cs, flags, fence);
57848b8605Smrg    r300->dirty_hw = 0;
58848b8605Smrg
59848b8605Smrg    /* New kitchen sink, baby. */
60848b8605Smrg    foreach_atom(r300, atom) {
61848b8605Smrg        if (atom->state || atom->allow_null_state) {
62848b8605Smrg            r300_mark_atom_dirty(r300, atom);
63848b8605Smrg        }
64848b8605Smrg    }
65848b8605Smrg    r300->vertex_arrays_dirty = TRUE;
66848b8605Smrg
67848b8605Smrg    /* Unmark HWTCL state for SWTCL. */
68848b8605Smrg    if (!r300->screen->caps.has_tcl) {
69848b8605Smrg        r300->vs_state.dirty = FALSE;
70848b8605Smrg        r300->vs_constants.dirty = FALSE;
71848b8605Smrg        r300->clip_state.dirty = FALSE;
72848b8605Smrg    }
73848b8605Smrg}
74848b8605Smrg
75848b8605Smrgvoid r300_flush(struct pipe_context *pipe,
76848b8605Smrg                unsigned flags,
77848b8605Smrg                struct pipe_fence_handle **fence)
78848b8605Smrg{
79848b8605Smrg    struct r300_context *r300 = r300_context(pipe);
80848b8605Smrg
81848b8605Smrg    if (r300->dirty_hw) {
82848b8605Smrg        r300_flush_and_cleanup(r300, flags, fence);
83848b8605Smrg    } else {
84848b8605Smrg        if (fence) {
85848b8605Smrg            /* We have to create a fence object, but the command stream is empty
86848b8605Smrg             * and we cannot emit an empty CS. Let's write to some reg. */
87848b8605Smrg            CS_LOCALS(r300);
88848b8605Smrg            OUT_CS_REG(RB3D_COLOR_CHANNEL_MASK, 0);
89b8e80941Smrg            r300->rws->cs_flush(r300->cs, flags, fence);
90848b8605Smrg        } else {
91848b8605Smrg            /* Even if hw is not dirty, we should at least reset the CS in case
92848b8605Smrg             * the space checking failed for the first draw operation. */
93b8e80941Smrg            r300->rws->cs_flush(r300->cs, flags, NULL);
94848b8605Smrg        }
95848b8605Smrg    }
96848b8605Smrg
97848b8605Smrg    /* Update Hyper-Z status. */
98848b8605Smrg    if (r300->hyperz_enabled) {
99848b8605Smrg        /* If there was a Z clear, keep Hyper-Z access. */
100848b8605Smrg        if (r300->num_z_clears) {
101848b8605Smrg            r300->hyperz_time_of_last_flush = os_time_get();
102848b8605Smrg            r300->num_z_clears = 0;
103848b8605Smrg        } else if (r300->hyperz_time_of_last_flush - os_time_get() > 2000000) {
104848b8605Smrg            /* If there hasn't been a Z clear for 2 seconds, revoke Hyper-Z access. */
105848b8605Smrg            r300->hiz_in_use = FALSE;
106848b8605Smrg
107848b8605Smrg            /* Decompress the Z buffer. */
108848b8605Smrg            if (r300->zmask_in_use) {
109848b8605Smrg                if (r300->locked_zbuffer) {
110848b8605Smrg                    r300_decompress_zmask_locked(r300);
111848b8605Smrg                } else {
112848b8605Smrg                    r300_decompress_zmask(r300);
113848b8605Smrg                }
114848b8605Smrg
115848b8605Smrg                if (fence && *fence)
116848b8605Smrg                    r300->rws->fence_reference(fence, NULL);
117848b8605Smrg                r300_flush_and_cleanup(r300, flags, fence);
118848b8605Smrg            }
119848b8605Smrg
120848b8605Smrg            /* Revoke Hyper-Z access, so that some other process can take it. */
121848b8605Smrg            r300->rws->cs_request_feature(r300->cs, RADEON_FID_R300_HYPERZ_ACCESS,
122848b8605Smrg                                          FALSE);
123848b8605Smrg            r300->hyperz_enabled = FALSE;
124848b8605Smrg        }
125848b8605Smrg    }
126848b8605Smrg}
127848b8605Smrg
128848b8605Smrgstatic void r300_flush_wrapped(struct pipe_context *pipe,
129848b8605Smrg                               struct pipe_fence_handle **fence,
130848b8605Smrg                               unsigned flags)
131848b8605Smrg{
132b8e80941Smrg    if (flags & PIPE_FLUSH_HINT_FINISH)
133b8e80941Smrg        flags &= ~PIPE_FLUSH_ASYNC;
134b8e80941Smrg
135b8e80941Smrg    r300_flush(pipe, flags, fence);
136848b8605Smrg}
137848b8605Smrg
138848b8605Smrgvoid r300_init_flush_functions(struct r300_context* r300)
139848b8605Smrg{
140848b8605Smrg    r300->context.flush = r300_flush_wrapped;
141848b8605Smrg}
142