1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2010 Marek Olšák <maraeo@gmail.com>
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * on the rights to use, copy, modify, merge, publish, distribute, sub
8bf215546Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom
9bf215546Sopenharmony_ci * the Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE. */
22bf215546Sopenharmony_ci
23bf215546Sopenharmony_ci/**
24bf215546Sopenharmony_ci * The two-sided stencil reference value fallback for r3xx-r4xx chips.
25bf215546Sopenharmony_ci * These chips support two-sided stencil functions but they do not support
26bf215546Sopenharmony_ci * a two-sided reference value.
27bf215546Sopenharmony_ci *
28bf215546Sopenharmony_ci * The functions below split every draw call which uses the two-sided
29bf215546Sopenharmony_ci * reference value into two draw calls -- the first one renders front faces
30bf215546Sopenharmony_ci * and the second renders back faces with the other reference value.
31bf215546Sopenharmony_ci */
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci#include "r300_context.h"
34bf215546Sopenharmony_ci#include "r300_reg.h"
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_cistruct r300_stencilref_context {
37bf215546Sopenharmony_ci    void (*draw_vbo)(struct pipe_context *pipe,
38bf215546Sopenharmony_ci                     const struct pipe_draw_info *info,
39bf215546Sopenharmony_ci                     unsigned drawid_offset,
40bf215546Sopenharmony_ci                     const struct pipe_draw_indirect_info *indirect,
41bf215546Sopenharmony_ci                     const struct pipe_draw_start_count_bias *draws,
42bf215546Sopenharmony_ci                     unsigned num_draws);
43bf215546Sopenharmony_ci
44bf215546Sopenharmony_ci    uint32_t rs_cull_mode;
45bf215546Sopenharmony_ci    uint32_t zb_stencilrefmask;
46bf215546Sopenharmony_ci    ubyte ref_value_front;
47bf215546Sopenharmony_ci};
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_cistatic boolean r300_stencilref_needed(struct r300_context *r300)
50bf215546Sopenharmony_ci{
51bf215546Sopenharmony_ci    struct r300_dsa_state *dsa = (struct r300_dsa_state*)r300->dsa_state.state;
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci    return dsa->two_sided_stencil_ref ||
54bf215546Sopenharmony_ci           (dsa->two_sided &&
55bf215546Sopenharmony_ci            r300->stencil_ref.ref_value[0] != r300->stencil_ref.ref_value[1]);
56bf215546Sopenharmony_ci}
57bf215546Sopenharmony_ci
58bf215546Sopenharmony_ci/* Set drawing for front faces. */
59bf215546Sopenharmony_cistatic void r300_stencilref_begin(struct r300_context *r300)
60bf215546Sopenharmony_ci{
61bf215546Sopenharmony_ci    struct r300_stencilref_context *sr = r300->stencilref_fallback;
62bf215546Sopenharmony_ci    struct r300_rs_state *rs = (struct r300_rs_state*)r300->rs_state.state;
63bf215546Sopenharmony_ci    struct r300_dsa_state *dsa = (struct r300_dsa_state*)r300->dsa_state.state;
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci    /* Save state. */
66bf215546Sopenharmony_ci    sr->rs_cull_mode = rs->cb_main[rs->cull_mode_index];
67bf215546Sopenharmony_ci    sr->zb_stencilrefmask = dsa->stencil_ref_mask;
68bf215546Sopenharmony_ci    sr->ref_value_front = r300->stencil_ref.ref_value[0];
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci    /* We *cull* pixels, therefore no need to mask out the bits. */
71bf215546Sopenharmony_ci    rs->cb_main[rs->cull_mode_index] |= R300_CULL_BACK;
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->rs_state);
74bf215546Sopenharmony_ci}
75bf215546Sopenharmony_ci
76bf215546Sopenharmony_ci/* Set drawing for back faces. */
77bf215546Sopenharmony_cistatic void r300_stencilref_switch_side(struct r300_context *r300)
78bf215546Sopenharmony_ci{
79bf215546Sopenharmony_ci    struct r300_stencilref_context *sr = r300->stencilref_fallback;
80bf215546Sopenharmony_ci    struct r300_rs_state *rs = (struct r300_rs_state*)r300->rs_state.state;
81bf215546Sopenharmony_ci    struct r300_dsa_state *dsa = (struct r300_dsa_state*)r300->dsa_state.state;
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_ci    rs->cb_main[rs->cull_mode_index] = sr->rs_cull_mode | R300_CULL_FRONT;
84bf215546Sopenharmony_ci    dsa->stencil_ref_mask = dsa->stencil_ref_bf;
85bf215546Sopenharmony_ci    r300->stencil_ref.ref_value[0] = r300->stencil_ref.ref_value[1];
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->rs_state);
88bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->dsa_state);
89bf215546Sopenharmony_ci}
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci/* Restore the original state. */
92bf215546Sopenharmony_cistatic void r300_stencilref_end(struct r300_context *r300)
93bf215546Sopenharmony_ci{
94bf215546Sopenharmony_ci    struct r300_stencilref_context *sr = r300->stencilref_fallback;
95bf215546Sopenharmony_ci    struct r300_rs_state *rs = (struct r300_rs_state*)r300->rs_state.state;
96bf215546Sopenharmony_ci    struct r300_dsa_state *dsa = (struct r300_dsa_state*)r300->dsa_state.state;
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci    /* Restore state. */
99bf215546Sopenharmony_ci    rs->cb_main[rs->cull_mode_index] = sr->rs_cull_mode;
100bf215546Sopenharmony_ci    dsa->stencil_ref_mask = sr->zb_stencilrefmask;
101bf215546Sopenharmony_ci    r300->stencil_ref.ref_value[0] = sr->ref_value_front;
102bf215546Sopenharmony_ci
103bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->rs_state);
104bf215546Sopenharmony_ci    r300_mark_atom_dirty(r300, &r300->dsa_state);
105bf215546Sopenharmony_ci}
106bf215546Sopenharmony_ci
107bf215546Sopenharmony_cistatic void r300_stencilref_draw_vbo(struct pipe_context *pipe,
108bf215546Sopenharmony_ci                                     const struct pipe_draw_info *info,
109bf215546Sopenharmony_ci                                     unsigned drawid_offset,
110bf215546Sopenharmony_ci                                     const struct pipe_draw_indirect_info *indirect,
111bf215546Sopenharmony_ci                                     const struct pipe_draw_start_count_bias *draws,
112bf215546Sopenharmony_ci                                     unsigned num_draws)
113bf215546Sopenharmony_ci{
114bf215546Sopenharmony_ci    struct r300_context *r300 = r300_context(pipe);
115bf215546Sopenharmony_ci    struct r300_stencilref_context *sr = r300->stencilref_fallback;
116bf215546Sopenharmony_ci
117bf215546Sopenharmony_ci    if (!r300_stencilref_needed(r300)) {
118bf215546Sopenharmony_ci        sr->draw_vbo(pipe, info, drawid_offset, NULL, draws, num_draws);
119bf215546Sopenharmony_ci    } else {
120bf215546Sopenharmony_ci        r300_stencilref_begin(r300);
121bf215546Sopenharmony_ci        sr->draw_vbo(pipe, info, drawid_offset, NULL, draws, num_draws);
122bf215546Sopenharmony_ci        r300_stencilref_switch_side(r300);
123bf215546Sopenharmony_ci        sr->draw_vbo(pipe, info, drawid_offset, NULL, draws, num_draws);
124bf215546Sopenharmony_ci        r300_stencilref_end(r300);
125bf215546Sopenharmony_ci    }
126bf215546Sopenharmony_ci}
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_civoid r300_plug_in_stencil_ref_fallback(struct r300_context *r300)
129bf215546Sopenharmony_ci{
130bf215546Sopenharmony_ci    r300->stencilref_fallback = CALLOC_STRUCT(r300_stencilref_context);
131bf215546Sopenharmony_ci
132bf215546Sopenharmony_ci    /* Save original draw function. */
133bf215546Sopenharmony_ci    r300->stencilref_fallback->draw_vbo = r300->context.draw_vbo;
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci    /* Override the draw function. */
136bf215546Sopenharmony_ci    r300->context.draw_vbo = r300_stencilref_draw_vbo;
137bf215546Sopenharmony_ci}
138