Path: blob/21.2-virgl/src/gallium/auxiliary/draw/draw_pipe_twoside.c
4565 views
/**************************************************************************1*2* Copyright 2007 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627/* Authors: Keith Whitwell <[email protected]>28*/2930#include "util/u_math.h"31#include "util/u_memory.h"32#include "pipe/p_defines.h"33#include "pipe/p_shader_tokens.h"34#include "draw_vs.h"35#include "draw_pipe.h"3637struct twoside_stage {38struct draw_stage stage;39float sign; /**< +1 or -1 */40int attrib_front0, attrib_back0;41int attrib_front1, attrib_back1;42};434445static inline struct twoside_stage *twoside_stage( struct draw_stage *stage )46{47return (struct twoside_stage *)stage;48}4950/**51* Copy back color(s) to front color(s).52*/53static inline struct vertex_header *54copy_bfc( struct twoside_stage *twoside,55const struct vertex_header *v,56unsigned idx )57{58struct vertex_header *tmp = dup_vert( &twoside->stage, v, idx );5960if (twoside->attrib_back0 >= 0 && twoside->attrib_front0 >= 0) {61COPY_4FV(tmp->data[twoside->attrib_front0],62tmp->data[twoside->attrib_back0]);63}64if (twoside->attrib_back1 >= 0 && twoside->attrib_front1 >= 0) {65COPY_4FV(tmp->data[twoside->attrib_front1],66tmp->data[twoside->attrib_back1]);67}6869return tmp;70}717273/* Twoside tri:74*/75static void twoside_tri( struct draw_stage *stage,76struct prim_header *header )77{78struct twoside_stage *twoside = twoside_stage(stage);7980if (header->det * twoside->sign < 0.0) {81/* this is a back-facing triangle */82struct prim_header tmp;8384tmp.det = header->det;85tmp.flags = header->flags;86tmp.pad = header->pad;87/* copy back attribs to front attribs */88tmp.v[0] = copy_bfc(twoside, header->v[0], 0);89tmp.v[1] = copy_bfc(twoside, header->v[1], 1);90tmp.v[2] = copy_bfc(twoside, header->v[2], 2);9192stage->next->tri( stage->next, &tmp );93}94else {95stage->next->tri( stage->next, header );96}97}9899100101static void twoside_first_tri( struct draw_stage *stage,102struct prim_header *header )103{104struct twoside_stage *twoside = twoside_stage(stage);105const struct draw_vertex_shader *vs = stage->draw->vs.vertex_shader;106uint i;107108twoside->attrib_front0 = -1;109twoside->attrib_front1 = -1;110twoside->attrib_back0 = -1;111twoside->attrib_back1 = -1;112113/*114* Find which vertex shader outputs are front/back colors115* (only first two can be front or back).116*/117for (i = 0; i < vs->info.num_outputs; i++) {118if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_COLOR) {119if (vs->info.output_semantic_index[i] == 0)120twoside->attrib_front0 = i;121else if (vs->info.output_semantic_index[i] == 1)122twoside->attrib_front1 = i;123}124if (vs->info.output_semantic_name[i] == TGSI_SEMANTIC_BCOLOR) {125if (vs->info.output_semantic_index[i] == 0)126twoside->attrib_back0 = i;127else if (vs->info.output_semantic_index[i] == 1)128twoside->attrib_back1 = i;129}130}131132/*133* We'll multiply the primitive's determinant by this sign to determine134* if the triangle is back-facing (negative).135* sign = -1 for CCW, +1 for CW136*/137twoside->sign = stage->draw->rasterizer->front_ccw ? -1.0f : 1.0f;138139stage->tri = twoside_tri;140stage->tri( stage, header );141}142143144static void twoside_flush( struct draw_stage *stage, unsigned flags )145{146stage->tri = twoside_first_tri;147stage->next->flush( stage->next, flags );148}149150151static void twoside_reset_stipple_counter( struct draw_stage *stage )152{153stage->next->reset_stipple_counter( stage->next );154}155156157static void twoside_destroy( struct draw_stage *stage )158{159draw_free_temp_verts( stage );160FREE( stage );161}162163164/**165* Create twoside pipeline stage.166*/167struct draw_stage *draw_twoside_stage( struct draw_context *draw )168{169struct twoside_stage *twoside = CALLOC_STRUCT(twoside_stage);170if (!twoside)171goto fail;172173twoside->stage.draw = draw;174twoside->stage.name = "twoside";175twoside->stage.next = NULL;176twoside->stage.point = draw_pipe_passthrough_point;177twoside->stage.line = draw_pipe_passthrough_line;178twoside->stage.tri = twoside_first_tri;179twoside->stage.flush = twoside_flush;180twoside->stage.reset_stipple_counter = twoside_reset_stipple_counter;181twoside->stage.destroy = twoside_destroy;182183if (!draw_alloc_temp_verts( &twoside->stage, 3 ))184goto fail;185186return &twoside->stage;187188fail:189if (twoside)190twoside->stage.destroy( &twoside->stage );191192return NULL;193}194195196