2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
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:
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
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. */
23 #include "draw/draw_context.h"
25 #include "util/u_memory.h"
26 #include "util/u_sampler.h"
27 #include "util/u_simple_list.h"
28 #include "util/u_upload_mgr.h"
31 #include "r300_context.h"
32 #include "r300_emit.h"
33 #include "r300_screen.h"
34 #include "r300_screen_buffer.h"
35 #include "r300_state_invariant.h"
36 #include "r300_winsys.h"
40 static void r300_destroy_context(struct pipe_context
* context
)
42 struct r300_context
* r300
= r300_context(context
);
43 struct r300_query
*query
, *temp
;
44 struct r300_atom
*atom
;
46 if (r300
->texkill_sampler
) {
47 pipe_sampler_view_reference(
48 (struct pipe_sampler_view
**)&r300
->texkill_sampler
,
52 util_blitter_destroy(r300
->blitter
);
53 draw_destroy(r300
->draw
);
55 /* Print stats, if enabled. */
56 if (SCREEN_DBG_ON(r300
->screen
, DBG_STATS
)) {
57 fprintf(stderr
, "r300: Stats for context %p:\n", r300
);
58 fprintf(stderr
, " : Flushes: %" PRIu64
"\n", r300
->flush_counter
);
59 foreach(atom
, &r300
->atom_list
) {
60 fprintf(stderr
, " : %s: %" PRIu64
" emits\n",
61 atom
->name
, atom
->counter
);
65 /* If there are any queries pending or not destroyed, remove them now. */
66 foreach_s(query
, temp
, &r300
->query_list
) {
67 remove_from_list(query
);
71 u_upload_destroy(r300
->upload_vb
);
72 u_upload_destroy(r300
->upload_ib
);
74 translate_cache_destroy(r300
->tran
.translate_cache
);
76 FREE(r300
->blend_color_state
.state
);
77 FREE(r300
->clip_state
.state
);
78 FREE(r300
->fb_state
.state
);
79 FREE(r300
->rs_block_state
.state
);
80 FREE(r300
->scissor_state
.state
);
81 FREE(r300
->textures_state
.state
);
82 FREE(r300
->viewport_state
.state
);
83 FREE(r300
->ztop_state
.state
);
84 FREE(r300
->fs_constants
.state
);
85 FREE(r300
->vs_constants
.state
);
86 if (!r300
->screen
->caps
.has_tcl
) {
87 FREE(r300
->vertex_stream_state
.state
);
92 static void r300_flush_cb(void *data
)
94 struct r300_context
* const cs_context_copy
= data
;
96 cs_context_copy
->context
.flush(&cs_context_copy
->context
, 0, NULL
);
99 #define R300_INIT_ATOM(atomname, atomsize) \
100 r300->atomname.name = #atomname; \
101 r300->atomname.state = NULL; \
102 r300->atomname.size = atomsize; \
103 r300->atomname.emit = r300_emit_##atomname; \
104 r300->atomname.dirty = FALSE; \
105 insert_at_tail(&r300->atom_list, &r300->atomname);
107 static void r300_setup_atoms(struct r300_context
* r300
)
109 boolean is_r500
= r300
->screen
->caps
.is_r500
;
110 boolean has_tcl
= r300
->screen
->caps
.has_tcl
;
112 /* Create the actual atom list.
114 * Each atom is examined and emitted in the order it appears here, which
115 * can affect performance and conformance if not handled with care.
117 * Some atoms never change size, others change every emit - those have
118 * the size of 0 here. */
119 make_empty_list(&r300
->atom_list
);
120 R300_INIT_ATOM(invariant_state
, 71);
121 R300_INIT_ATOM(ztop_state
, 2);
122 R300_INIT_ATOM(query_start
, 4);
123 R300_INIT_ATOM(blend_state
, 8);
124 R300_INIT_ATOM(blend_color_state
, is_r500
? 3 : 2);
125 R300_INIT_ATOM(clip_state
, has_tcl
? 5 + (6 * 4) : 2);
126 R300_INIT_ATOM(dsa_state
, is_r500
? 8 : 6);
127 R300_INIT_ATOM(fb_state
, 0);
128 R300_INIT_ATOM(rs_state
, 0);
129 R300_INIT_ATOM(scissor_state
, 3);
130 R300_INIT_ATOM(viewport_state
, 9);
131 R300_INIT_ATOM(rs_block_state
, 0);
132 R300_INIT_ATOM(vertex_stream_state
, 0);
133 R300_INIT_ATOM(pvs_flush
, 2);
134 R300_INIT_ATOM(vs_state
, 0);
135 R300_INIT_ATOM(vs_constants
, 0);
136 R300_INIT_ATOM(texture_cache_inval
, 2);
137 R300_INIT_ATOM(textures_state
, 0);
138 R300_INIT_ATOM(fs
, 0);
139 R300_INIT_ATOM(fs_rc_constant_state
, 0);
140 R300_INIT_ATOM(fs_constants
, 0);
142 /* Replace emission functions for r500. */
143 if (r300
->screen
->caps
.is_r500
) {
144 r300
->fs
.emit
= r500_emit_fs
;
145 r300
->fs_rc_constant_state
.emit
= r500_emit_fs_rc_constant_state
;
146 r300
->fs_constants
.emit
= r500_emit_fs_constants
;
149 /* Some non-CSO atoms need explicit space to store the state locally. */
150 r300
->blend_color_state
.state
= CALLOC_STRUCT(r300_blend_color_state
);
151 r300
->clip_state
.state
= CALLOC_STRUCT(r300_clip_state
);
152 r300
->fb_state
.state
= CALLOC_STRUCT(pipe_framebuffer_state
);
153 r300
->rs_block_state
.state
= CALLOC_STRUCT(r300_rs_block
);
154 r300
->scissor_state
.state
= CALLOC_STRUCT(pipe_scissor_state
);
155 r300
->textures_state
.state
= CALLOC_STRUCT(r300_textures_state
);
156 r300
->viewport_state
.state
= CALLOC_STRUCT(r300_viewport_state
);
157 r300
->ztop_state
.state
= CALLOC_STRUCT(r300_ztop_state
);
158 r300
->fs_constants
.state
= CALLOC_STRUCT(r300_constant_buffer
);
159 r300
->vs_constants
.state
= CALLOC_STRUCT(r300_constant_buffer
);
160 if (!r300
->screen
->caps
.has_tcl
) {
161 r300
->vertex_stream_state
.state
= CALLOC_STRUCT(r300_vertex_stream_state
);
164 /* Some non-CSO atoms don't use the state pointer. */
165 r300
->invariant_state
.allow_null_state
= TRUE
;
166 r300
->fs_rc_constant_state
.allow_null_state
= TRUE
;
167 r300
->pvs_flush
.allow_null_state
= TRUE
;
168 r300
->query_start
.allow_null_state
= TRUE
;
169 r300
->texture_cache_inval
.allow_null_state
= TRUE
;
172 /* Not every state tracker calls every driver function before the first draw
173 * call and we must initialize the command buffers somehow. */
174 static void r300_init_states(struct pipe_context
*pipe
)
176 struct pipe_blend_color bc
= {{0}};
177 struct pipe_clip_state cs
= {{{0}}};
178 struct pipe_scissor_state ss
= {0};
179 struct r300_clip_state
*clip
=
180 (struct r300_clip_state
*)r300_context(pipe
)->clip_state
.state
;
183 pipe
->set_blend_color(pipe
, &bc
);
184 pipe
->set_scissor_state(pipe
, &ss
);
186 if (r300_context(pipe
)->screen
->caps
.has_tcl
) {
187 pipe
->set_clip_state(pipe
, &cs
);
189 BEGIN_CB(clip
->cb
, 2);
190 OUT_CB_REG(R300_VAP_CLIP_CNTL
, R300_CLIP_DISABLE
);
195 struct pipe_context
* r300_create_context(struct pipe_screen
* screen
,
198 struct r300_context
* r300
= CALLOC_STRUCT(r300_context
);
199 struct r300_screen
* r300screen
= r300_screen(screen
);
200 struct r300_winsys_screen
*rws
= r300screen
->rws
;
206 r300
->screen
= r300screen
;
208 r300
->context
.winsys
= (struct pipe_winsys
*)rws
;
209 r300
->context
.screen
= screen
;
210 r300
->context
.priv
= priv
;
212 r300
->context
.destroy
= r300_destroy_context
;
214 if (!r300screen
->caps
.has_tcl
) {
215 /* Create a Draw. This is used for SW TCL. */
216 r300
->draw
= draw_create(&r300
->context
);
217 /* Enable our renderer. */
218 draw_set_rasterize_stage(r300
->draw
, r300_draw_stage(r300
));
219 /* Enable Draw's clipping. */
220 draw_set_driver_clipping(r300
->draw
, FALSE
);
221 /* Disable converting points/lines to triangles. */
222 draw_wide_line_threshold(r300
->draw
, 10000000.f
);
223 draw_wide_point_threshold(r300
->draw
, 10000000.f
);
226 r300_setup_atoms(r300
);
228 make_empty_list(&r300
->query_list
);
230 r300_init_blit_functions(r300
);
231 r300_init_flush_functions(r300
);
232 r300_init_query_functions(r300
);
233 r300_init_render_functions(r300
);
234 r300_init_state_functions(r300
);
235 r300_init_resource_functions(r300
);
237 r300
->invariant_state
.dirty
= TRUE
;
239 rws
->set_flush_cb(r300
->rws
, r300_flush_cb
, r300
);
242 r300
->blitter
= util_blitter_create(&r300
->context
);
244 r300
->upload_ib
= u_upload_create(&r300
->context
,
246 PIPE_BIND_INDEX_BUFFER
);
248 if (r300
->upload_ib
== NULL
)
251 r300
->upload_vb
= u_upload_create(&r300
->context
,
253 PIPE_BIND_VERTEX_BUFFER
);
254 if (r300
->upload_vb
== NULL
)
257 r300
->tran
.translate_cache
= translate_cache_create();
259 r300_init_states(&r300
->context
);
261 /* The KIL opcode needs the first texture unit to be enabled
262 * on r3xx-r4xx. In order to calm down the CS checker, we bind this
263 * dummy texture there. */
264 if (!r300
->screen
->caps
.is_r500
) {
265 struct pipe_resource
*tex
;
266 struct pipe_resource rtempl
= {{0}};
267 struct pipe_sampler_view vtempl
= {{0}};
269 rtempl
.target
= PIPE_TEXTURE_2D
;
270 rtempl
.format
= PIPE_FORMAT_I8_UNORM
;
271 rtempl
.bind
= PIPE_BIND_SAMPLER_VIEW
;
275 tex
= screen
->resource_create(screen
, &rtempl
);
277 u_sampler_view_default_template(&vtempl
, tex
, tex
->format
);
279 r300
->texkill_sampler
= (struct r300_sampler_view
*)
280 r300
->context
.create_sampler_view(&r300
->context
, tex
, &vtempl
);
282 pipe_resource_reference(&tex
, NULL
);
284 /* This will make sure that the dummy texture is set up
285 * from the beginning even if an application does not use
287 r300
->textures_state
.dirty
= TRUE
;
290 return &r300
->context
;
293 u_upload_destroy(r300
->upload_ib
);
299 boolean
r300_check_cs(struct r300_context
*r300
, unsigned size
)
301 return size
<= r300
->rws
->get_cs_free_dwords(r300
->rws
);
304 void r300_finish(struct r300_context
*r300
)
306 struct pipe_framebuffer_state
*fb
;
309 /* This is a preliminary implementation of glFinish.
311 * The ideal implementation should use something like EmitIrqLocked and
312 * WaitIrq, or better, real fences.
314 if (r300
->fb_state
.state
) {
315 fb
= r300
->fb_state
.state
;
317 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
318 if (fb
->cbufs
[i
]->texture
) {
319 r300
->rws
->buffer_wait(r300
->rws
,
320 r300_texture(fb
->cbufs
[i
]->texture
)->buffer
);
324 if (fb
->zsbuf
&& fb
->zsbuf
->texture
) {
325 r300
->rws
->buffer_wait(r300
->rws
,
326 r300_texture(fb
->zsbuf
->texture
)->buffer
);