r300g: fix emission of some non-CSO atoms at the beginning of CS
[mesa.git] / src / gallium / drivers / r300 / r300_context.c
1 /*
2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
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 #include "draw/draw_context.h"
24
25 #include "util/u_memory.h"
26 #include "util/u_simple_list.h"
27 #include "util/u_upload_mgr.h"
28
29 #include "r300_blit.h"
30 #include "r300_context.h"
31 #include "r300_emit.h"
32 #include "r300_flush.h"
33 #include "r300_query.h"
34 #include "r300_render.h"
35 #include "r300_screen.h"
36 #include "r300_screen_buffer.h"
37 #include "r300_state_invariant.h"
38 #include "r300_winsys.h"
39
40 #include <inttypes.h>
41
42 static void r300_destroy_context(struct pipe_context* context)
43 {
44 struct r300_context* r300 = r300_context(context);
45 struct r300_query* query, * temp;
46 struct r300_atom *atom;
47
48 util_blitter_destroy(r300->blitter);
49 draw_destroy(r300->draw);
50
51 /* Print stats, if enabled. */
52 if (SCREEN_DBG_ON(r300->screen, DBG_STATS)) {
53 fprintf(stderr, "r300: Stats for context %p:\n", r300);
54 fprintf(stderr, " : Flushes: %" PRIu64 "\n", r300->flush_counter);
55 foreach(atom, &r300->atom_list) {
56 fprintf(stderr, " : %s: %" PRIu64 " emits\n",
57 atom->name, atom->counter);
58 }
59 }
60
61 /* Free the OQ BO. */
62 context->screen->resource_destroy(context->screen, r300->oqbo);
63
64 /* If there are any queries pending or not destroyed, remove them now. */
65 foreach_s(query, temp, &r300->query_list) {
66 remove_from_list(query);
67 FREE(query);
68 }
69
70 u_upload_destroy(r300->upload_vb);
71 u_upload_destroy(r300->upload_ib);
72
73 FREE(r300->blend_color_state.state);
74 FREE(r300->clip_state.state);
75 FREE(r300->fb_state.state);
76 FREE(r300->rs_block_state.state);
77 FREE(r300->scissor_state.state);
78 FREE(r300->textures_state.state);
79 FREE(r300->viewport_state.state);
80 FREE(r300->ztop_state.state);
81 FREE(r300->fs_constants.state);
82 FREE(r300->vs_constants.state);
83 FREE(r300);
84 }
85
86 static void r300_flush_cb(void *data)
87 {
88 struct r300_context* const cs_context_copy = data;
89
90 cs_context_copy->context.flush(&cs_context_copy->context, 0, NULL);
91 }
92
93 #define R300_INIT_ATOM(atomname, atomsize) \
94 r300->atomname.name = #atomname; \
95 r300->atomname.state = NULL; \
96 r300->atomname.size = atomsize; \
97 r300->atomname.emit = r300_emit_##atomname; \
98 r300->atomname.dirty = FALSE; \
99 insert_at_tail(&r300->atom_list, &r300->atomname);
100
101 static void r300_setup_atoms(struct r300_context* r300)
102 {
103 boolean is_r500 = r300->screen->caps.is_r500;
104 boolean has_tcl = r300->screen->caps.has_tcl;
105
106 /* Create the actual atom list.
107 *
108 * Each atom is examined and emitted in the order it appears here, which
109 * can affect performance and conformance if not handled with care.
110 *
111 * Some atoms never change size, others change every emit - those have
112 * the size of 0 here. */
113 make_empty_list(&r300->atom_list);
114 R300_INIT_ATOM(invariant_state, 71);
115 R300_INIT_ATOM(query_start, 4);
116 R300_INIT_ATOM(ztop_state, 2);
117 R300_INIT_ATOM(blend_state, 8);
118 R300_INIT_ATOM(blend_color_state, is_r500 ? 3 : 2);
119 R300_INIT_ATOM(clip_state, has_tcl ? 5 + (6 * 4) : 2);
120 R300_INIT_ATOM(dsa_state, is_r500 ? 8 : 6);
121 R300_INIT_ATOM(fb_state, 0);
122 R300_INIT_ATOM(rs_state, 0);
123 R300_INIT_ATOM(scissor_state, 3);
124 R300_INIT_ATOM(viewport_state, 9);
125 R300_INIT_ATOM(rs_block_state, 0);
126 R300_INIT_ATOM(vertex_stream_state, 0);
127 R300_INIT_ATOM(pvs_flush, 2);
128 R300_INIT_ATOM(vs_state, 0);
129 R300_INIT_ATOM(vs_constants, 0);
130 R300_INIT_ATOM(texture_cache_inval, 2);
131 R300_INIT_ATOM(textures_state, 0);
132 R300_INIT_ATOM(fs, 0);
133 R300_INIT_ATOM(fs_rc_constant_state, 0);
134 R300_INIT_ATOM(fs_constants, 0);
135
136 /* Replace emission functions for r500. */
137 if (r300->screen->caps.is_r500) {
138 r300->fs.emit = r500_emit_fs;
139 r300->fs_rc_constant_state.emit = r500_emit_fs_rc_constant_state;
140 r300->fs_constants.emit = r500_emit_fs_constants;
141 }
142
143 /* Some non-CSO atoms need explicit space to store the state locally. */
144 r300->blend_color_state.state = CALLOC_STRUCT(r300_blend_color_state);
145 r300->clip_state.state = CALLOC_STRUCT(pipe_clip_state);
146 r300->fb_state.state = CALLOC_STRUCT(pipe_framebuffer_state);
147 r300->rs_block_state.state = CALLOC_STRUCT(r300_rs_block);
148 r300->scissor_state.state = CALLOC_STRUCT(pipe_scissor_state);
149 r300->textures_state.state = CALLOC_STRUCT(r300_textures_state);
150 r300->viewport_state.state = CALLOC_STRUCT(r300_viewport_state);
151 r300->ztop_state.state = CALLOC_STRUCT(r300_ztop_state);
152 r300->fs_constants.state = CALLOC_STRUCT(r300_constant_buffer);
153 r300->vs_constants.state = CALLOC_STRUCT(r300_constant_buffer);
154
155 /* Some non-CSO atoms don't use the state pointer. */
156 r300->invariant_state.allow_null_state = TRUE;
157 r300->fs_rc_constant_state.allow_null_state = TRUE;
158 r300->pvs_flush.allow_null_state = TRUE;
159 r300->query_start.allow_null_state = TRUE;
160 r300->texture_cache_inval.allow_null_state = TRUE;
161 }
162
163 struct pipe_context* r300_create_context(struct pipe_screen* screen,
164 void *priv)
165 {
166 struct r300_context* r300 = CALLOC_STRUCT(r300_context);
167 struct r300_screen* r300screen = r300_screen(screen);
168 struct r300_winsys_screen *rws = r300screen->rws;
169
170 if (!r300)
171 return NULL;
172
173 r300->rws = rws;
174 r300->screen = r300screen;
175
176 r300->context.winsys = (struct pipe_winsys*)rws;
177 r300->context.screen = screen;
178 r300->context.priv = priv;
179
180 r300->context.destroy = r300_destroy_context;
181
182 r300->context.clear = r300_clear;
183 r300->context.surface_copy = r300_surface_copy;
184 r300->context.surface_fill = r300_surface_fill;
185
186 if (r300screen->caps.has_tcl) {
187 r300->context.draw_arrays = r300_draw_arrays;
188 r300->context.draw_elements = r300_draw_elements;
189 r300->context.draw_range_elements = r300_draw_range_elements;
190
191 if (r300screen->caps.is_r500) {
192 r300->emit_draw_arrays_immediate = r500_emit_draw_arrays_immediate;
193 r300->emit_draw_arrays = r500_emit_draw_arrays;
194 r300->emit_draw_elements = r500_emit_draw_elements;
195 } else {
196 r300->emit_draw_arrays_immediate = r300_emit_draw_arrays_immediate;
197 r300->emit_draw_arrays = r300_emit_draw_arrays;
198 r300->emit_draw_elements = r300_emit_draw_elements;
199 }
200 } else {
201 r300->context.draw_arrays = r300_swtcl_draw_arrays;
202 r300->context.draw_elements = r300_draw_elements;
203 r300->context.draw_range_elements = r300_swtcl_draw_range_elements;
204
205 /* Create a Draw. This is used for SW TCL. */
206 r300->draw = draw_create(&r300->context);
207 /* Enable our renderer. */
208 draw_set_rasterize_stage(r300->draw, r300_draw_stage(r300));
209 /* Enable Draw's clipping. */
210 draw_set_driver_clipping(r300->draw, FALSE);
211 }
212
213 r300_setup_atoms(r300);
214
215 /* Open up the OQ BO. */
216 r300->oqbo = pipe_buffer_create(screen,
217 R300_BIND_OQBO, 4096);
218 make_empty_list(&r300->query_list);
219
220 r300_init_flush_functions(r300);
221 r300_init_query_functions(r300);
222 r300_init_state_functions(r300);
223 r300_init_resource_functions(r300);
224
225 r300->invariant_state.dirty = TRUE;
226
227 rws->set_flush_cb(r300->rws, r300_flush_cb, r300);
228 r300->dirty_hw++;
229
230 r300->blitter = util_blitter_create(&r300->context);
231
232 r300->upload_ib = u_upload_create(&r300->context,
233 32 * 1024, 16,
234 PIPE_BIND_INDEX_BUFFER);
235
236 if (r300->upload_ib == NULL)
237 goto no_upload_ib;
238
239 r300->upload_vb = u_upload_create(&r300->context,
240 128 * 1024, 16,
241 PIPE_BIND_VERTEX_BUFFER);
242 if (r300->upload_vb == NULL)
243 goto no_upload_vb;
244
245 return &r300->context;
246
247 no_upload_ib:
248 u_upload_destroy(r300->upload_ib);
249 no_upload_vb:
250 FREE(r300);
251 return NULL;
252 }