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_winsys.h"
39 static void r300_release_referenced_objects(struct r300_context
*r300
)
41 struct pipe_framebuffer_state
*fb
=
42 (struct pipe_framebuffer_state
*)r300
->fb_state
.state
;
43 struct r300_textures_state
*textures
=
44 (struct r300_textures_state
*)r300
->textures_state
.state
;
45 struct r300_query
*query
, *temp
;
48 /* Framebuffer state. */
49 util_assign_framebuffer_state(fb
, NULL
);
52 for (i
= 0; i
< textures
->sampler_view_count
; i
++)
53 pipe_sampler_view_reference(
54 (struct pipe_sampler_view
**)&textures
->sampler_views
[i
], NULL
);
56 /* The special dummy texture for texkill. */
57 if (r300
->texkill_sampler
) {
58 pipe_sampler_view_reference(
59 (struct pipe_sampler_view
**)&r300
->texkill_sampler
,
64 pipe_resource_reference(&r300
->vbo
, NULL
);
67 for (i
= 0; i
< r300
->vertex_buffer_count
; i
++) {
68 pipe_resource_reference(&r300
->vertex_buffer
[i
].buffer
, NULL
);
71 /* If there are any queries pending or not destroyed, remove them now. */
72 foreach_s(query
, temp
, &r300
->query_list
) {
73 remove_from_list(query
);
78 static void r300_destroy_context(struct pipe_context
* context
)
80 struct r300_context
* r300
= r300_context(context
);
81 struct r300_atom
*atom
;
83 util_blitter_destroy(r300
->blitter
);
84 draw_destroy(r300
->draw
);
86 /* Print stats, if enabled. */
87 if (SCREEN_DBG_ON(r300
->screen
, DBG_STATS
)) {
88 fprintf(stderr
, "r300: Stats for context %p:\n", r300
);
89 fprintf(stderr
, " : Flushes: %" PRIu64
"\n", r300
->flush_counter
);
90 foreach(atom
, &r300
->atom_list
) {
91 fprintf(stderr
, " : %s: %" PRIu64
" emits\n",
92 atom
->name
, atom
->counter
);
96 u_upload_destroy(r300
->upload_vb
);
97 u_upload_destroy(r300
->upload_ib
);
99 translate_cache_destroy(r300
->tran
.translate_cache
);
101 r300_release_referenced_objects(r300
);
103 r300
->rws
->cs_destroy(r300
->cs
);
105 FREE(r300
->aa_state
.state
);
106 FREE(r300
->blend_color_state
.state
);
107 FREE(r300
->clip_state
.state
);
108 FREE(r300
->fb_state
.state
);
109 FREE(r300
->gpu_flush
.state
);
110 FREE(r300
->hyperz_state
.state
);
111 FREE(r300
->invariant_state
.state
);
112 FREE(r300
->rs_block_state
.state
);
113 FREE(r300
->scissor_state
.state
);
114 FREE(r300
->textures_state
.state
);
115 FREE(r300
->vap_invariant_state
.state
);
116 FREE(r300
->viewport_state
.state
);
117 FREE(r300
->ztop_state
.state
);
118 FREE(r300
->fs_constants
.state
);
119 FREE(r300
->vs_constants
.state
);
120 if (!r300
->screen
->caps
.has_tcl
) {
121 FREE(r300
->vertex_stream_state
.state
);
126 void r300_flush_cb(void *data
)
128 struct r300_context
* const cs_context_copy
= data
;
130 cs_context_copy
->context
.flush(&cs_context_copy
->context
, 0, NULL
);
133 #define R300_INIT_ATOM(atomname, atomsize) \
134 r300->atomname.name = #atomname; \
135 r300->atomname.state = NULL; \
136 r300->atomname.size = atomsize; \
137 r300->atomname.emit = r300_emit_##atomname; \
138 r300->atomname.dirty = FALSE; \
139 insert_at_tail(&r300->atom_list, &r300->atomname);
141 static void r300_setup_atoms(struct r300_context
* r300
)
143 boolean is_rv350
= r300
->screen
->caps
.is_rv350
;
144 boolean is_r500
= r300
->screen
->caps
.is_r500
;
145 boolean has_tcl
= r300
->screen
->caps
.has_tcl
;
146 boolean drm_2_3_0
= r300
->rws
->get_value(r300
->rws
, R300_VID_DRM_2_3_0
);
148 /* Create the actual atom list.
150 * Each atom is examined and emitted in the order it appears here, which
151 * can affect performance and conformance if not handled with care.
153 * Some atoms never change size, others change every emit - those have
154 * the size of 0 here.
156 * NOTE: The framebuffer state is split into these atoms:
157 * - gpu_flush (unpipelined regs)
158 * - aa_state (unpipelined regs)
159 * - fb_state (unpipelined regs)
160 * - hyperz_state (unpipelined regs followed by pipelined ones)
161 * - fb_state_pipelined (pipelined regs)
162 * The motivation behind this is to be able to emit a strict
163 * subset of the regs, and to have reasonable register ordering. */
164 make_empty_list(&r300
->atom_list
);
165 /* SC, GB (unpipelined), RB3D (unpipelined), ZB (unpipelined). */
166 R300_INIT_ATOM(gpu_flush
, 9);
167 R300_INIT_ATOM(aa_state
, 4);
168 R300_INIT_ATOM(fb_state
, 0);
169 /* ZB (unpipelined), SC. */
170 R300_INIT_ATOM(hyperz_state
, 6);
171 R300_INIT_ATOM(ztop_state
, 2);
173 R300_INIT_ATOM(dsa_state
, is_r500
? 8 : 6);
175 R300_INIT_ATOM(blend_state
, 8);
176 R300_INIT_ATOM(blend_color_state
, is_r500
? 3 : 2);
178 R300_INIT_ATOM(scissor_state
, 3);
179 /* GB, FG, GA, SU, SC, RB3D. */
180 R300_INIT_ATOM(invariant_state
, 16 + (is_rv350
? 4 : 0));
182 R300_INIT_ATOM(viewport_state
, 9);
183 R300_INIT_ATOM(pvs_flush
, 2);
184 R300_INIT_ATOM(vap_invariant_state
, 9);
185 R300_INIT_ATOM(vertex_stream_state
, 0);
186 R300_INIT_ATOM(vs_state
, 0);
187 R300_INIT_ATOM(vs_constants
, 0);
188 R300_INIT_ATOM(clip_state
, has_tcl
? 5 + (6 * 4) : 2);
189 /* VAP, RS, GA, GB, SU, SC. */
190 R300_INIT_ATOM(rs_block_state
, 0);
191 R300_INIT_ATOM(rs_state
, 0);
193 R300_INIT_ATOM(fb_state_pipelined
, 5 + (drm_2_3_0
? 3 : 0));
195 R300_INIT_ATOM(fs
, 0);
196 R300_INIT_ATOM(fs_rc_constant_state
, 0);
197 R300_INIT_ATOM(fs_constants
, 0);
199 R300_INIT_ATOM(texture_cache_inval
, 2);
200 R300_INIT_ATOM(textures_state
, 0);
201 /* ZB (unpipelined), SU. */
202 R300_INIT_ATOM(query_start
, 4);
204 /* Replace emission functions for r500. */
206 r300
->fs
.emit
= r500_emit_fs
;
207 r300
->fs_rc_constant_state
.emit
= r500_emit_fs_rc_constant_state
;
208 r300
->fs_constants
.emit
= r500_emit_fs_constants
;
211 /* Some non-CSO atoms need explicit space to store the state locally. */
212 r300
->aa_state
.state
= CALLOC_STRUCT(r300_aa_state
);
213 r300
->blend_color_state
.state
= CALLOC_STRUCT(r300_blend_color_state
);
214 r300
->clip_state
.state
= CALLOC_STRUCT(r300_clip_state
);
215 r300
->fb_state
.state
= CALLOC_STRUCT(pipe_framebuffer_state
);
216 r300
->gpu_flush
.state
= CALLOC_STRUCT(pipe_framebuffer_state
);
217 r300
->hyperz_state
.state
= CALLOC_STRUCT(r300_hyperz_state
);
218 r300
->invariant_state
.state
= CALLOC_STRUCT(r300_invariant_state
);
219 r300
->rs_block_state
.state
= CALLOC_STRUCT(r300_rs_block
);
220 r300
->scissor_state
.state
= CALLOC_STRUCT(pipe_scissor_state
);
221 r300
->textures_state
.state
= CALLOC_STRUCT(r300_textures_state
);
222 r300
->vap_invariant_state
.state
= CALLOC_STRUCT(r300_vap_invariant_state
);
223 r300
->viewport_state
.state
= CALLOC_STRUCT(r300_viewport_state
);
224 r300
->ztop_state
.state
= CALLOC_STRUCT(r300_ztop_state
);
225 r300
->fs_constants
.state
= CALLOC_STRUCT(r300_constant_buffer
);
226 r300
->vs_constants
.state
= CALLOC_STRUCT(r300_constant_buffer
);
227 if (!r300
->screen
->caps
.has_tcl
) {
228 r300
->vertex_stream_state
.state
= CALLOC_STRUCT(r300_vertex_stream_state
);
231 /* Some non-CSO atoms don't use the state pointer. */
232 r300
->fb_state_pipelined
.allow_null_state
= TRUE
;
233 r300
->fs_rc_constant_state
.allow_null_state
= TRUE
;
234 r300
->pvs_flush
.allow_null_state
= TRUE
;
235 r300
->query_start
.allow_null_state
= TRUE
;
236 r300
->texture_cache_inval
.allow_null_state
= TRUE
;
238 /* Some states must be marked as dirty here to properly set up
239 * hardware in the first command stream. */
240 r300
->invariant_state
.dirty
= TRUE
;
241 r300
->pvs_flush
.dirty
= TRUE
;
242 r300
->vap_invariant_state
.dirty
= TRUE
;
243 r300
->texture_cache_inval
.dirty
= TRUE
;
244 r300
->textures_state
.dirty
= TRUE
;
247 /* Not every state tracker calls every driver function before the first draw
248 * call and we must initialize the command buffers somehow. */
249 static void r300_init_states(struct pipe_context
*pipe
)
251 struct r300_context
*r300
= r300_context(pipe
);
252 struct pipe_blend_color bc
= {{0}};
253 struct pipe_clip_state cs
= {{{0}}};
254 struct pipe_scissor_state ss
= {0};
255 struct r300_clip_state
*clip
=
256 (struct r300_clip_state
*)r300
->clip_state
.state
;
257 struct r300_gpu_flush
*gpuflush
=
258 (struct r300_gpu_flush
*)r300
->gpu_flush
.state
;
259 struct r300_vap_invariant_state
*vap_invariant
=
260 (struct r300_vap_invariant_state
*)r300
->vap_invariant_state
.state
;
261 struct r300_invariant_state
*invariant
=
262 (struct r300_invariant_state
*)r300
->invariant_state
.state
;
263 struct r300_hyperz_state
*hyperz
=
264 (struct r300_hyperz_state
*)r300
->hyperz_state
.state
;
267 pipe
->set_blend_color(pipe
, &bc
);
268 pipe
->set_scissor_state(pipe
, &ss
);
270 /* Initialize the clip state. */
271 if (r300_context(pipe
)->screen
->caps
.has_tcl
) {
272 pipe
->set_clip_state(pipe
, &cs
);
274 BEGIN_CB(clip
->cb
, 2);
275 OUT_CB_REG(R300_VAP_CLIP_CNTL
, R300_CLIP_DISABLE
);
279 /* Initialize the GPU flush. */
281 BEGIN_CB(gpuflush
->cb_flush_clean
, 6);
283 /* Flush and free renderbuffer caches. */
284 OUT_CB_REG(R300_RB3D_DSTCACHE_CTLSTAT
,
285 R300_RB3D_DSTCACHE_CTLSTAT_DC_FREE_FREE_3D_TAGS
|
286 R300_RB3D_DSTCACHE_CTLSTAT_DC_FLUSH_FLUSH_DIRTY_3D
);
287 OUT_CB_REG(R300_ZB_ZCACHE_CTLSTAT
,
288 R300_ZB_ZCACHE_CTLSTAT_ZC_FLUSH_FLUSH_AND_FREE
|
289 R300_ZB_ZCACHE_CTLSTAT_ZC_FREE_FREE
);
291 /* Wait until the GPU is idle.
292 * This fixes random pixels sometimes appearing probably caused
293 * by incomplete rendering. */
294 OUT_CB_REG(RADEON_WAIT_UNTIL
, RADEON_WAIT_3D_IDLECLEAN
);
298 /* Initialize the VAP invariant state. */
300 BEGIN_CB(vap_invariant
->cb
, 9);
301 OUT_CB_REG(VAP_PVS_VTX_TIMEOUT_REG
, 0xffff);
302 OUT_CB_REG_SEQ(R300_VAP_GB_VERT_CLIP_ADJ
, 4);
307 OUT_CB_REG(R300_VAP_PSC_SGN_NORM_CNTL
, R300_SGN_NORM_NO_ZERO
);
311 /* Initialize the invariant state. */
313 BEGIN_CB(invariant
->cb
, r300
->invariant_state
.size
);
314 OUT_CB_REG(R300_GB_SELECT
, 0);
315 OUT_CB_REG(R300_FG_FOG_BLEND
, 0);
316 OUT_CB_REG(R300_GA_ROUND_MODE
, 1);
317 OUT_CB_REG(R300_GA_OFFSET
, 0);
318 OUT_CB_REG(R300_SU_TEX_WRAP
, 0);
319 OUT_CB_REG(R300_SU_DEPTH_SCALE
, 0x4B7FFFFF);
320 OUT_CB_REG(R300_SU_DEPTH_OFFSET
, 0);
321 OUT_CB_REG(R300_SC_EDGERULE
, 0x2DA49525);
323 if (r300
->screen
->caps
.is_rv350
) {
324 OUT_CB_REG(R500_RB3D_DISCARD_SRC_PIXEL_LTE_THRESHOLD
, 0x01010101);
325 OUT_CB_REG(R500_RB3D_DISCARD_SRC_PIXEL_GTE_THRESHOLD
, 0xFEFEFEFE);
330 /* Initialize the hyperz state. */
332 BEGIN_CB(&hyperz
->cb_begin
, r300
->hyperz_state
.size
);
333 OUT_CB_REG(R300_ZB_BW_CNTL
, 0);
334 OUT_CB_REG(R300_ZB_DEPTHCLEARVALUE
, 0);
335 OUT_CB_REG(R300_SC_HYPERZ
, R300_SC_HYPERZ_ADJ_2
);
340 struct pipe_context
* r300_create_context(struct pipe_screen
* screen
,
343 struct r300_context
* r300
= CALLOC_STRUCT(r300_context
);
344 struct r300_screen
* r300screen
= r300_screen(screen
);
345 struct r300_winsys_screen
*rws
= r300screen
->rws
;
351 r300
->screen
= r300screen
;
353 r300
->context
.winsys
= (struct pipe_winsys
*)rws
;
354 r300
->context
.screen
= screen
;
355 r300
->context
.priv
= priv
;
357 r300
->context
.destroy
= r300_destroy_context
;
359 r300
->cs
= rws
->cs_create(rws
);
361 if (!r300screen
->caps
.has_tcl
) {
362 /* Create a Draw. This is used for SW TCL. */
363 r300
->draw
= draw_create(&r300
->context
);
364 /* Enable our renderer. */
365 draw_set_rasterize_stage(r300
->draw
, r300_draw_stage(r300
));
366 /* Enable Draw's clipping. */
367 draw_set_driver_clipping(r300
->draw
, FALSE
);
368 /* Disable converting points/lines to triangles. */
369 draw_wide_line_threshold(r300
->draw
, 10000000.f
);
370 draw_wide_point_threshold(r300
->draw
, 10000000.f
);
373 r300_setup_atoms(r300
);
375 make_empty_list(&r300
->query_list
);
377 r300_init_blit_functions(r300
);
378 r300_init_flush_functions(r300
);
379 r300_init_query_functions(r300
);
380 r300_init_state_functions(r300
);
381 r300_init_resource_functions(r300
);
383 r300
->blitter
= util_blitter_create(&r300
->context
);
385 /* Render functions must be initialized after blitter. */
386 r300_init_render_functions(r300
);
388 rws
->cs_set_flush(r300
->cs
, r300_flush_cb
, r300
);
390 r300
->upload_ib
= u_upload_create(&r300
->context
,
392 PIPE_BIND_INDEX_BUFFER
);
394 if (r300
->upload_ib
== NULL
)
397 r300
->upload_vb
= u_upload_create(&r300
->context
,
399 PIPE_BIND_VERTEX_BUFFER
);
400 if (r300
->upload_vb
== NULL
)
403 r300
->tran
.translate_cache
= translate_cache_create();
405 r300_init_states(&r300
->context
);
407 /* The KIL opcode needs the first texture unit to be enabled
408 * on r3xx-r4xx. In order to calm down the CS checker, we bind this
409 * dummy texture there. */
410 if (!r300
->screen
->caps
.is_r500
) {
411 struct pipe_resource
*tex
;
412 struct pipe_resource rtempl
= {{0}};
413 struct pipe_sampler_view vtempl
= {{0}};
415 rtempl
.target
= PIPE_TEXTURE_2D
;
416 rtempl
.format
= PIPE_FORMAT_I8_UNORM
;
417 rtempl
.bind
= PIPE_BIND_SAMPLER_VIEW
;
421 tex
= screen
->resource_create(screen
, &rtempl
);
423 u_sampler_view_default_template(&vtempl
, tex
, tex
->format
);
425 r300
->texkill_sampler
= (struct r300_sampler_view
*)
426 r300
->context
.create_sampler_view(&r300
->context
, tex
, &vtempl
);
428 pipe_resource_reference(&tex
, NULL
);
431 return &r300
->context
;
434 u_upload_destroy(r300
->upload_ib
);
440 void r300_finish(struct r300_context
*r300
)
442 struct pipe_framebuffer_state
*fb
;
445 /* This is a preliminary implementation of glFinish.
447 * The ideal implementation should use something like EmitIrqLocked and
448 * WaitIrq, or better, real fences.
450 if (r300
->fb_state
.state
) {
451 fb
= r300
->fb_state
.state
;
453 for (i
= 0; i
< fb
->nr_cbufs
; i
++) {
454 if (fb
->cbufs
[i
]->texture
) {
455 r300
->rws
->buffer_wait(r300
->rws
,
456 r300_texture(fb
->cbufs
[i
]->texture
)->buffer
);
460 if (fb
->zsbuf
&& fb
->zsbuf
->texture
) {
461 r300
->rws
->buffer_wait(r300
->rws
,
462 r300_texture(fb
->zsbuf
->texture
)->buffer
);