Path: blob/21.2-virgl/src/gallium/auxiliary/draw/draw_pipe_flatshade.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"3233#include "pipe/p_shader_tokens.h"34#include "draw_vs.h"35#include "draw_fs.h"36#include "draw_pipe.h"373839/** subclass of draw_stage */40struct flat_stage41{42struct draw_stage stage;4344uint num_flat_attribs;45uint flat_attribs[PIPE_MAX_SHADER_OUTPUTS]; /* flatshaded attribs */46};474849static inline struct flat_stage *50flat_stage(struct draw_stage *stage)51{52return (struct flat_stage *) stage;53}545556/** Copy all the constant attributes from 'src' vertex to 'dst' vertex */57static inline void copy_flats( struct draw_stage *stage,58struct vertex_header *dst,59const struct vertex_header *src )60{61const struct flat_stage *flat = flat_stage(stage);62uint i;6364for (i = 0; i < flat->num_flat_attribs; i++) {65const uint attr = flat->flat_attribs[i];66COPY_4FV(dst->data[attr], src->data[attr]);67}68}697071/** Copy all the color attributes from src vertex to dst0 & dst1 vertices */72static inline void copy_flats2( struct draw_stage *stage,73struct vertex_header *dst0,74struct vertex_header *dst1,75const struct vertex_header *src )76{77const struct flat_stage *flat = flat_stage(stage);78uint i;79for (i = 0; i < flat->num_flat_attribs; i++) {80const uint attr = flat->flat_attribs[i];81COPY_4FV(dst0->data[attr], src->data[attr]);82COPY_4FV(dst1->data[attr], src->data[attr]);83}84}858687/**88* Flatshade tri. Not required for clipping which handles this on its own,89* but required for unfilled tris and other primitive-changing stages90* (like widelines). If no such stages are active, handled by hardware.91*/92static void flatshade_tri_0( struct draw_stage *stage,93struct prim_header *header )94{95struct prim_header tmp;9697tmp.det = header->det;98tmp.flags = header->flags;99tmp.pad = header->pad;100tmp.v[0] = header->v[0];101tmp.v[1] = dup_vert(stage, header->v[1], 0);102tmp.v[2] = dup_vert(stage, header->v[2], 1);103104copy_flats2(stage, tmp.v[1], tmp.v[2], tmp.v[0]);105106stage->next->tri( stage->next, &tmp );107}108109110static void flatshade_tri_2( struct draw_stage *stage,111struct prim_header *header )112{113struct prim_header tmp;114115tmp.det = header->det;116tmp.flags = header->flags;117tmp.pad = header->pad;118tmp.v[0] = dup_vert(stage, header->v[0], 0);119tmp.v[1] = dup_vert(stage, header->v[1], 1);120tmp.v[2] = header->v[2];121122copy_flats2(stage, tmp.v[0], tmp.v[1], tmp.v[2]);123124stage->next->tri( stage->next, &tmp );125}126127128/**129* Flatshade line.130*/131static void flatshade_line_0( struct draw_stage *stage,132struct prim_header *header )133{134struct prim_header tmp;135136tmp.det = header->det;137tmp.flags = header->flags;138tmp.pad = header->pad;139tmp.v[0] = header->v[0];140tmp.v[1] = dup_vert(stage, header->v[1], 0);141142copy_flats(stage, tmp.v[1], tmp.v[0]);143144stage->next->line( stage->next, &tmp );145}146147148static void flatshade_line_1( struct draw_stage *stage,149struct prim_header *header )150{151struct prim_header tmp;152153tmp.det = header->det;154tmp.flags = header->flags;155tmp.pad = header->pad;156tmp.v[0] = dup_vert(stage, header->v[0], 0);157tmp.v[1] = header->v[1];158159copy_flats(stage, tmp.v[0], tmp.v[1]);160161stage->next->line( stage->next, &tmp );162}163164165static int166find_interp(const struct draw_fragment_shader *fs, int *indexed_interp,167uint semantic_name, uint semantic_index)168{169int interp;170/* If it's gl_{Front,Back}{,Secondary}Color, pick up the mode171* from the array we've filled before. */172if ((semantic_name == TGSI_SEMANTIC_COLOR ||173semantic_name == TGSI_SEMANTIC_BCOLOR) &&174semantic_index < 2) {175interp = indexed_interp[semantic_index];176} else {177/* Otherwise, search in the FS inputs, with a decent default178* if we don't find it.179*/180uint j;181interp = TGSI_INTERPOLATE_PERSPECTIVE;182if (fs) {183for (j = 0; j < fs->info.num_inputs; j++) {184if (semantic_name == fs->info.input_semantic_name[j] &&185semantic_index == fs->info.input_semantic_index[j]) {186interp = fs->info.input_interpolate[j];187break;188}189}190}191}192return interp;193}194195196static void flatshade_init_state( struct draw_stage *stage )197{198struct flat_stage *flat = flat_stage(stage);199const struct draw_context *draw = stage->draw;200const struct draw_fragment_shader *fs = draw->fs.fragment_shader;201const struct tgsi_shader_info *info = draw_get_shader_info(draw);202uint i, j;203204/* Find which vertex shader outputs need constant interpolation, make a list */205206/* XXX: this code is a near exact copy of the one in clip_init_state.207* The latter also cares about perspective though.208*/209210/* First pick up the interpolation mode for211* gl_Color/gl_SecondaryColor, with the correct default.212*/213int indexed_interp[2];214indexed_interp[0] = indexed_interp[1] = draw->rasterizer->flatshade ?215TGSI_INTERPOLATE_CONSTANT : TGSI_INTERPOLATE_PERSPECTIVE;216217if (fs) {218for (i = 0; i < fs->info.num_inputs; i++) {219if (fs->info.input_semantic_name[i] == TGSI_SEMANTIC_COLOR &&220fs->info.input_semantic_index[i] < 2) {221if (fs->info.input_interpolate[i] != TGSI_INTERPOLATE_COLOR)222indexed_interp[fs->info.input_semantic_index[i]] = fs->info.input_interpolate[i];223}224}225}226227/* Then resolve the interpolation mode for every output attribute.228*229* Given how the rest of the code, the most efficient way is to230* have a vector of flat-mode attributes.231*/232flat->num_flat_attribs = 0;233for (i = 0; i < info->num_outputs; i++) {234/* Find the interpolation mode for a specific attribute */235int interp = find_interp(fs, indexed_interp,236info->output_semantic_name[i],237info->output_semantic_index[i]);238/* If it's flat, add it to the flat vector. */239240if (interp == TGSI_INTERPOLATE_CONSTANT ||241(interp == TGSI_INTERPOLATE_COLOR && draw->rasterizer->flatshade)) {242flat->flat_attribs[flat->num_flat_attribs] = i;243flat->num_flat_attribs++;244}245}246/* Search the extra vertex attributes */247for (j = 0; j < draw->extra_shader_outputs.num; j++) {248/* Find the interpolation mode for a specific attribute */249int interp = find_interp(fs, indexed_interp,250draw->extra_shader_outputs.semantic_name[j],251draw->extra_shader_outputs.semantic_index[j]);252/* If it's flat, add it to the flat vector. */253if (interp == TGSI_INTERPOLATE_CONSTANT) {254flat->flat_attribs[flat->num_flat_attribs] = i + j;255flat->num_flat_attribs++;256}257}258259/* Choose flatshade routine according to provoking vertex:260*/261if (draw->rasterizer->flatshade_first) {262stage->line = flatshade_line_0;263stage->tri = flatshade_tri_0;264}265else {266stage->line = flatshade_line_1;267stage->tri = flatshade_tri_2;268}269}270271static void flatshade_first_tri( struct draw_stage *stage,272struct prim_header *header )273{274flatshade_init_state( stage );275stage->tri( stage, header );276}277278static void flatshade_first_line( struct draw_stage *stage,279struct prim_header *header )280{281flatshade_init_state( stage );282stage->line( stage, header );283}284285286static void flatshade_flush( struct draw_stage *stage,287unsigned flags )288{289stage->tri = flatshade_first_tri;290stage->line = flatshade_first_line;291stage->next->flush( stage->next, flags );292}293294295static void flatshade_reset_stipple_counter( struct draw_stage *stage )296{297stage->next->reset_stipple_counter( stage->next );298}299300301static void flatshade_destroy( struct draw_stage *stage )302{303draw_free_temp_verts( stage );304FREE( stage );305}306307308/**309* Create flatshading drawing stage.310*/311struct draw_stage *draw_flatshade_stage( struct draw_context *draw )312{313struct flat_stage *flatshade = CALLOC_STRUCT(flat_stage);314if (!flatshade)315goto fail;316317flatshade->stage.draw = draw;318flatshade->stage.name = "flatshade";319flatshade->stage.next = NULL;320flatshade->stage.point = draw_pipe_passthrough_point;321flatshade->stage.line = flatshade_first_line;322flatshade->stage.tri = flatshade_first_tri;323flatshade->stage.flush = flatshade_flush;324flatshade->stage.reset_stipple_counter = flatshade_reset_stipple_counter;325flatshade->stage.destroy = flatshade_destroy;326327if (!draw_alloc_temp_verts( &flatshade->stage, 2 ))328goto fail;329330return &flatshade->stage;331332fail:333if (flatshade)334flatshade->stage.destroy( &flatshade->stage );335336return NULL;337}338339340341342