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