1 /*
2 * Copyright 2010 Marek Olšák <[email protected]>
3 * SPDX-License-Identifier: MIT
4 */
5
6 /**
7 * The two-sided stencil reference value fallback for r3xx-r4xx chips.
8 * These chips support two-sided stencil functions but they do not support
9 * a two-sided reference value.
10 *
11 * The functions below split every draw call which uses the two-sided
12 * reference value into two draw calls -- the first one renders front faces
13 * and the second renders back faces with the other reference value.
14 */
15
16 #include "r300_context.h"
17 #include "r300_reg.h"
18
19 struct r300_stencilref_context {
20 pipe_draw_func draw_vbo;
21
22 uint32_t rs_cull_mode;
23 uint32_t zb_stencilrefmask;
24 uint8_t ref_value_front;
25 };
26
r300_stencilref_needed(struct r300_context * r300)27 static bool r300_stencilref_needed(struct r300_context *r300)
28 {
29 struct r300_dsa_state *dsa = (struct r300_dsa_state*)r300->dsa_state.state;
30
31 return dsa->two_sided_stencil_ref ||
32 (dsa->two_sided &&
33 r300->stencil_ref.ref_value[0] != r300->stencil_ref.ref_value[1]);
34 }
35
36 /* Set drawing for front faces. */
r300_stencilref_begin(struct r300_context * r300)37 static void r300_stencilref_begin(struct r300_context *r300)
38 {
39 struct r300_stencilref_context *sr = r300->stencilref_fallback;
40 struct r300_rs_state *rs = (struct r300_rs_state*)r300->rs_state.state;
41 struct r300_dsa_state *dsa = (struct r300_dsa_state*)r300->dsa_state.state;
42
43 /* Save state. */
44 sr->rs_cull_mode = rs->cb_main[rs->cull_mode_index];
45 sr->zb_stencilrefmask = dsa->stencil_ref_mask;
46 sr->ref_value_front = r300->stencil_ref.ref_value[0];
47
48 /* We *cull* pixels, therefore no need to mask out the bits. */
49 rs->cb_main[rs->cull_mode_index] |= R300_CULL_BACK;
50
51 r300_mark_atom_dirty(r300, &r300->rs_state);
52 }
53
54 /* Set drawing for back faces. */
r300_stencilref_switch_side(struct r300_context * r300)55 static void r300_stencilref_switch_side(struct r300_context *r300)
56 {
57 struct r300_stencilref_context *sr = r300->stencilref_fallback;
58 struct r300_rs_state *rs = (struct r300_rs_state*)r300->rs_state.state;
59 struct r300_dsa_state *dsa = (struct r300_dsa_state*)r300->dsa_state.state;
60
61 rs->cb_main[rs->cull_mode_index] = sr->rs_cull_mode | R300_CULL_FRONT;
62 dsa->stencil_ref_mask = dsa->stencil_ref_bf;
63 r300->stencil_ref.ref_value[0] = r300->stencil_ref.ref_value[1];
64
65 r300_mark_atom_dirty(r300, &r300->rs_state);
66 r300_mark_atom_dirty(r300, &r300->dsa_state);
67 }
68
69 /* Restore the original state. */
r300_stencilref_end(struct r300_context * r300)70 static void r300_stencilref_end(struct r300_context *r300)
71 {
72 struct r300_stencilref_context *sr = r300->stencilref_fallback;
73 struct r300_rs_state *rs = (struct r300_rs_state*)r300->rs_state.state;
74 struct r300_dsa_state *dsa = (struct r300_dsa_state*)r300->dsa_state.state;
75
76 /* Restore state. */
77 rs->cb_main[rs->cull_mode_index] = sr->rs_cull_mode;
78 dsa->stencil_ref_mask = sr->zb_stencilrefmask;
79 r300->stencil_ref.ref_value[0] = sr->ref_value_front;
80
81 r300_mark_atom_dirty(r300, &r300->rs_state);
82 r300_mark_atom_dirty(r300, &r300->dsa_state);
83 }
84
r300_stencilref_draw_vbo(struct pipe_context * pipe,const struct pipe_draw_info * info,unsigned drawid_offset,const struct pipe_draw_indirect_info * indirect,const struct pipe_draw_start_count_bias * draws,unsigned num_draws)85 static void r300_stencilref_draw_vbo(struct pipe_context *pipe,
86 const struct pipe_draw_info *info,
87 unsigned drawid_offset,
88 const struct pipe_draw_indirect_info *indirect,
89 const struct pipe_draw_start_count_bias *draws,
90 unsigned num_draws)
91 {
92 struct r300_context *r300 = r300_context(pipe);
93 struct r300_stencilref_context *sr = r300->stencilref_fallback;
94
95 if (!r300_stencilref_needed(r300)) {
96 sr->draw_vbo(pipe, info, drawid_offset, NULL, draws, num_draws);
97 } else {
98 r300_stencilref_begin(r300);
99 sr->draw_vbo(pipe, info, drawid_offset, NULL, draws, num_draws);
100 r300_stencilref_switch_side(r300);
101 sr->draw_vbo(pipe, info, drawid_offset, NULL, draws, num_draws);
102 r300_stencilref_end(r300);
103 }
104 }
105
r300_plug_in_stencil_ref_fallback(struct r300_context * r300)106 void r300_plug_in_stencil_ref_fallback(struct r300_context *r300)
107 {
108 r300->stencilref_fallback = CALLOC_STRUCT(r300_stencilref_context);
109
110 /* Save original draw function. */
111 r300->stencilref_fallback->draw_vbo = r300->context.draw_vbo;
112
113 /* Override the draw function. */
114 r300->context.draw_vbo = r300_stencilref_draw_vbo;
115 }
116