Path: blob/21.2-virgl/src/gallium/drivers/r300/r300_render_stencilref.c
4570 views
/*1* Copyright 2010 Marek Olšák <[email protected]>2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* on the rights to use, copy, modify, merge, publish, distribute, sub7* license, and/or sell copies of the Software, and to permit persons to whom8* the Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL17* THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,18* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR19* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE20* USE OR OTHER DEALINGS IN THE SOFTWARE. */2122/**23* The two-sided stencil reference value fallback for r3xx-r4xx chips.24* These chips support two-sided stencil functions but they do not support25* a two-sided reference value.26*27* The functions below split every draw call which uses the two-sided28* reference value into two draw calls -- the first one renders front faces29* and the second renders back faces with the other reference value.30*/3132#include "r300_context.h"33#include "r300_reg.h"3435struct r300_stencilref_context {36void (*draw_vbo)(struct pipe_context *pipe,37const struct pipe_draw_info *info,38unsigned drawid_offset,39const struct pipe_draw_indirect_info *indirect,40const struct pipe_draw_start_count_bias *draws,41unsigned num_draws);4243uint32_t rs_cull_mode;44uint32_t zb_stencilrefmask;45ubyte ref_value_front;46};4748static boolean r300_stencilref_needed(struct r300_context *r300)49{50struct r300_dsa_state *dsa = (struct r300_dsa_state*)r300->dsa_state.state;5152return dsa->two_sided_stencil_ref ||53(dsa->two_sided &&54r300->stencil_ref.ref_value[0] != r300->stencil_ref.ref_value[1]);55}5657/* Set drawing for front faces. */58static void r300_stencilref_begin(struct r300_context *r300)59{60struct r300_stencilref_context *sr = r300->stencilref_fallback;61struct r300_rs_state *rs = (struct r300_rs_state*)r300->rs_state.state;62struct r300_dsa_state *dsa = (struct r300_dsa_state*)r300->dsa_state.state;6364/* Save state. */65sr->rs_cull_mode = rs->cb_main[rs->cull_mode_index];66sr->zb_stencilrefmask = dsa->stencil_ref_mask;67sr->ref_value_front = r300->stencil_ref.ref_value[0];6869/* We *cull* pixels, therefore no need to mask out the bits. */70rs->cb_main[rs->cull_mode_index] |= R300_CULL_BACK;7172r300_mark_atom_dirty(r300, &r300->rs_state);73}7475/* Set drawing for back faces. */76static void r300_stencilref_switch_side(struct r300_context *r300)77{78struct r300_stencilref_context *sr = r300->stencilref_fallback;79struct r300_rs_state *rs = (struct r300_rs_state*)r300->rs_state.state;80struct r300_dsa_state *dsa = (struct r300_dsa_state*)r300->dsa_state.state;8182rs->cb_main[rs->cull_mode_index] = sr->rs_cull_mode | R300_CULL_FRONT;83dsa->stencil_ref_mask = dsa->stencil_ref_bf;84r300->stencil_ref.ref_value[0] = r300->stencil_ref.ref_value[1];8586r300_mark_atom_dirty(r300, &r300->rs_state);87r300_mark_atom_dirty(r300, &r300->dsa_state);88}8990/* Restore the original state. */91static void r300_stencilref_end(struct r300_context *r300)92{93struct r300_stencilref_context *sr = r300->stencilref_fallback;94struct r300_rs_state *rs = (struct r300_rs_state*)r300->rs_state.state;95struct r300_dsa_state *dsa = (struct r300_dsa_state*)r300->dsa_state.state;9697/* Restore state. */98rs->cb_main[rs->cull_mode_index] = sr->rs_cull_mode;99dsa->stencil_ref_mask = sr->zb_stencilrefmask;100r300->stencil_ref.ref_value[0] = sr->ref_value_front;101102r300_mark_atom_dirty(r300, &r300->rs_state);103r300_mark_atom_dirty(r300, &r300->dsa_state);104}105106static void r300_stencilref_draw_vbo(struct pipe_context *pipe,107const struct pipe_draw_info *info,108unsigned drawid_offset,109const struct pipe_draw_indirect_info *indirect,110const struct pipe_draw_start_count_bias *draws,111unsigned num_draws)112{113struct r300_context *r300 = r300_context(pipe);114struct r300_stencilref_context *sr = r300->stencilref_fallback;115116if (!r300_stencilref_needed(r300)) {117sr->draw_vbo(pipe, info, drawid_offset, NULL, draws, num_draws);118} else {119r300_stencilref_begin(r300);120sr->draw_vbo(pipe, info, drawid_offset, NULL, draws, num_draws);121r300_stencilref_switch_side(r300);122sr->draw_vbo(pipe, info, drawid_offset, NULL, draws, num_draws);123r300_stencilref_end(r300);124}125}126127void r300_plug_in_stencil_ref_fallback(struct r300_context *r300)128{129r300->stencilref_fallback = CALLOC_STRUCT(r300_stencilref_context);130131/* Save original draw function. */132r300->stencilref_fallback->draw_vbo = r300->context.draw_vbo;133134/* Override the draw function. */135r300->context.draw_vbo = r300_stencilref_draw_vbo;136}137138139