radeonsi: prevent recursion in si_context_gfx_flush
[mesa.git] / src / gallium / drivers / radeonsi / si_hw_context.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Jerome Glisse
25 */
26
27 #include "si_pipe.h"
28
29 /* initialize */
30 void si_need_cs_space(struct si_context *ctx)
31 {
32 struct radeon_winsys_cs *cs = ctx->b.rings.gfx.cs;
33 struct radeon_winsys_cs *dma = ctx->b.rings.dma.cs;
34
35 /* Flush the DMA IB if it's not empty. */
36 if (dma && dma->cdw)
37 ctx->b.rings.dma.flush(ctx, RADEON_FLUSH_ASYNC, NULL);
38
39 /* There are two memory usage counters in the winsys for all buffers
40 * that have been added (cs_add_buffer) and two counters in the pipe
41 * driver for those that haven't been added yet.
42 */
43 if (unlikely(!ctx->b.ws->cs_memory_below_limit(ctx->b.rings.gfx.cs,
44 ctx->b.vram, ctx->b.gtt))) {
45 ctx->b.gtt = 0;
46 ctx->b.vram = 0;
47 ctx->b.rings.gfx.flush(ctx, RADEON_FLUSH_ASYNC, NULL);
48 return;
49 }
50 ctx->b.gtt = 0;
51 ctx->b.vram = 0;
52
53 /* If the CS is sufficiently large, don't count the space needed
54 * and just flush if there is not enough space left.
55 */
56 if (unlikely(cs->cdw > cs->max_dw - 2048))
57 ctx->b.rings.gfx.flush(ctx, RADEON_FLUSH_ASYNC, NULL);
58 }
59
60 void si_context_gfx_flush(void *context, unsigned flags,
61 struct pipe_fence_handle **fence)
62 {
63 struct si_context *ctx = context;
64 struct radeon_winsys_cs *cs = ctx->b.rings.gfx.cs;
65 struct radeon_winsys *ws = ctx->b.ws;
66
67 if (ctx->gfx_flush_in_progress)
68 return;
69
70 ctx->gfx_flush_in_progress = true;
71
72 if (cs->cdw == ctx->b.initial_gfx_cs_size &&
73 (!fence || ctx->last_gfx_fence)) {
74 if (fence)
75 ws->fence_reference(fence, ctx->last_gfx_fence);
76 if (!(flags & RADEON_FLUSH_ASYNC))
77 ws->cs_sync_flush(cs);
78 ctx->gfx_flush_in_progress = false;
79 return;
80 }
81
82 r600_preflush_suspend_features(&ctx->b);
83
84 ctx->b.flags |= SI_CONTEXT_FLUSH_AND_INV_FRAMEBUFFER |
85 SI_CONTEXT_INV_VMEM_L1 |
86 SI_CONTEXT_INV_GLOBAL_L2 |
87 /* this is probably not needed anymore */
88 SI_CONTEXT_PS_PARTIAL_FLUSH;
89 si_emit_cache_flush(ctx, NULL);
90
91 /* force to keep tiling flags */
92 flags |= RADEON_FLUSH_KEEP_TILING_FLAGS;
93
94 if (ctx->trace_buf)
95 si_trace_emit(ctx);
96
97 if (ctx->is_debug) {
98 unsigned i;
99
100 /* Save the IB for debug contexts. */
101 free(ctx->last_ib);
102 ctx->last_ib_dw_size = cs->cdw;
103 ctx->last_ib = malloc(cs->cdw * 4);
104 memcpy(ctx->last_ib, cs->buf, cs->cdw * 4);
105 r600_resource_reference(&ctx->last_trace_buf, ctx->trace_buf);
106 r600_resource_reference(&ctx->trace_buf, NULL);
107
108 /* Save the buffer list. */
109 if (ctx->last_bo_list) {
110 for (i = 0; i < ctx->last_bo_count; i++)
111 pb_reference(&ctx->last_bo_list[i].buf, NULL);
112 free(ctx->last_bo_list);
113 }
114 ctx->last_bo_count = ws->cs_get_buffer_list(cs, NULL);
115 ctx->last_bo_list = calloc(ctx->last_bo_count,
116 sizeof(ctx->last_bo_list[0]));
117 ws->cs_get_buffer_list(cs, ctx->last_bo_list);
118 }
119
120 /* Flush the CS. */
121 ws->cs_flush(cs, flags, &ctx->last_gfx_fence,
122 ctx->screen->b.cs_count++);
123
124 if (fence)
125 ws->fence_reference(fence, ctx->last_gfx_fence);
126
127 /* Check VM faults if needed. */
128 if (ctx->screen->b.debug_flags & DBG_CHECK_VM)
129 si_check_vm_faults(ctx);
130
131 si_begin_new_cs(ctx);
132 ctx->gfx_flush_in_progress = false;
133 }
134
135 void si_begin_new_cs(struct si_context *ctx)
136 {
137 if (ctx->is_debug) {
138 uint32_t zero = 0;
139
140 /* Create a buffer used for writing trace IDs and initialize it to 0. */
141 assert(!ctx->trace_buf);
142 ctx->trace_buf = (struct r600_resource*)
143 pipe_buffer_create(ctx->b.b.screen, PIPE_BIND_CUSTOM,
144 PIPE_USAGE_STAGING, 4);
145 if (ctx->trace_buf)
146 pipe_buffer_write_nooverlap(&ctx->b.b, &ctx->trace_buf->b.b,
147 0, sizeof(zero), &zero);
148 ctx->trace_id = 0;
149 }
150
151 if (ctx->trace_buf)
152 si_trace_emit(ctx);
153
154 /* Flush read caches at the beginning of CS. */
155 ctx->b.flags |= SI_CONTEXT_FLUSH_AND_INV_FRAMEBUFFER |
156 SI_CONTEXT_INV_VMEM_L1 |
157 SI_CONTEXT_INV_GLOBAL_L2 |
158 SI_CONTEXT_INV_SMEM_L1 |
159 SI_CONTEXT_INV_ICACHE;
160
161 /* set all valid group as dirty so they get reemited on
162 * next draw command
163 */
164 si_pm4_reset_emitted(ctx);
165
166 /* The CS initialization should be emitted before everything else. */
167 si_pm4_emit(ctx, ctx->init_config);
168
169 ctx->framebuffer.dirty_cbufs = (1 << 8) - 1;
170 ctx->framebuffer.dirty_zsbuf = true;
171 si_mark_atom_dirty(ctx, &ctx->framebuffer.atom);
172
173 si_mark_atom_dirty(ctx, &ctx->clip_regs);
174 si_mark_atom_dirty(ctx, &ctx->clip_state.atom);
175 si_mark_atom_dirty(ctx, &ctx->msaa_sample_locs);
176 si_mark_atom_dirty(ctx, &ctx->msaa_config);
177 si_mark_atom_dirty(ctx, &ctx->sample_mask.atom);
178 si_mark_atom_dirty(ctx, &ctx->cb_target_mask);
179 si_mark_atom_dirty(ctx, &ctx->blend_color.atom);
180 si_mark_atom_dirty(ctx, &ctx->db_render_state);
181 si_mark_atom_dirty(ctx, &ctx->stencil_ref.atom);
182 si_mark_atom_dirty(ctx, &ctx->spi_map);
183 si_mark_atom_dirty(ctx, &ctx->spi_ps_input);
184 si_mark_atom_dirty(ctx, &ctx->b.streamout.enable_atom);
185 si_all_descriptors_begin_new_cs(ctx);
186
187 ctx->scissors.dirty_mask = (1 << SI_MAX_VIEWPORTS) - 1;
188 ctx->viewports.dirty_mask = (1 << SI_MAX_VIEWPORTS) - 1;
189 si_mark_atom_dirty(ctx, &ctx->scissors.atom);
190 si_mark_atom_dirty(ctx, &ctx->viewports.atom);
191
192 r600_postflush_resume_features(&ctx->b);
193
194 ctx->b.initial_gfx_cs_size = ctx->b.rings.gfx.cs->cdw;
195
196 /* Invalidate various draw states so that they are emitted before
197 * the first draw call. */
198 si_invalidate_draw_sh_constants(ctx);
199 ctx->last_primitive_restart_en = -1;
200 ctx->last_restart_index = SI_RESTART_INDEX_UNKNOWN;
201 ctx->last_gs_out_prim = -1;
202 ctx->last_prim = -1;
203 ctx->last_multi_vgt_param = -1;
204 ctx->last_ls_hs_config = -1;
205 ctx->last_rast_prim = -1;
206 ctx->last_sc_line_stipple = ~0;
207 ctx->emit_scratch_reloc = true;
208 ctx->last_ls = NULL;
209 ctx->last_tcs = NULL;
210 ctx->last_tes_sh_base = -1;
211 ctx->last_num_tcs_input_cp = -1;
212 }