Path: blob/21.2-virgl/src/gallium/drivers/softpipe/sp_quad_fs.c
4570 views
/**************************************************************************1*2* Copyright 2007 VMware, Inc.3* All Rights Reserved.4* Copyright 2008 VMware, Inc. All rights reserved.5*6* Permission is hereby granted, free of charge, to any person obtaining a7* copy of this software and associated documentation files (the8* "Software"), to deal in the Software without restriction, including9* without limitation the rights to use, copy, modify, merge, publish,10* distribute, sub license, and/or sell copies of the Software, and to11* permit persons to whom the Software is furnished to do so, subject to12* the following conditions:13*14* The above copyright notice and this permission notice (including the15* next paragraph) shall be included in all copies or substantial portions16* of the Software.17*18* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS19* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF20* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.21* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR22* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,23* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE24* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.25*26**************************************************************************/2728/* Vertices are just an array of floats, with all the attributes29* packed. We currently assume a layout like:30*31* attr[0][0..3] - window position32* attr[1..n][0..3] - remaining attributes.33*34* Attributes are assumed to be 4 floats wide but are packed so that35* all the enabled attributes run contiguously.36*/3738#include "util/u_math.h"39#include "util/u_memory.h"40#include "pipe/p_defines.h"41#include "pipe/p_shader_tokens.h"4243#include "sp_context.h"44#include "sp_state.h"45#include "sp_quad.h"46#include "sp_quad_pipe.h"474849struct quad_shade_stage50{51struct quad_stage stage; /**< base class */5253/* no other fields at this time */54};555657/**58* Execute fragment shader for the four fragments in the quad.59* \return TRUE if quad is alive, FALSE if all four pixels are killed60*/61static inline boolean62shade_quad(struct quad_stage *qs, struct quad_header *quad)63{64struct softpipe_context *softpipe = qs->softpipe;65struct tgsi_exec_machine *machine = softpipe->fs_machine;6667if (softpipe->active_statistics_queries) {68softpipe->pipeline_statistics.ps_invocations +=69util_bitcount(quad->inout.mask);70}7172/* run shader */73machine->flatshade_color = softpipe->rasterizer->flatshade ? TRUE : FALSE;74return softpipe->fs_variant->run( softpipe->fs_variant, machine, quad, softpipe->early_depth );75}76777879static void80coverage_quad(struct quad_stage *qs, struct quad_header *quad)81{82struct softpipe_context *softpipe = qs->softpipe;83uint cbuf;8485/* loop over colorbuffer outputs */86for (cbuf = 0; cbuf < softpipe->framebuffer.nr_cbufs; cbuf++) {87float (*quadColor)[4] = quad->output.color[cbuf];88unsigned j;89for (j = 0; j < TGSI_QUAD_SIZE; j++) {90assert(quad->input.coverage[j] >= 0.0);91assert(quad->input.coverage[j] <= 1.0);92quadColor[3][j] *= quad->input.coverage[j];93}94}95}969798/**99* Shade/write an array of quads100* Called via quad_stage::run()101*/102static void103shade_quads(struct quad_stage *qs,104struct quad_header *quads[],105unsigned nr)106{107struct softpipe_context *softpipe = qs->softpipe;108struct tgsi_exec_machine *machine = softpipe->fs_machine;109unsigned i, nr_quads = 0;110111tgsi_exec_set_constant_buffers(machine, PIPE_MAX_CONSTANT_BUFFERS,112softpipe->mapped_constants[PIPE_SHADER_FRAGMENT],113softpipe->const_buffer_size[PIPE_SHADER_FRAGMENT]);114115machine->InterpCoefs = quads[0]->coef;116117for (i = 0; i < nr; i++) {118/* Only omit this quad from the output list if all the fragments119* are killed _AND_ it's not the first quad in the list.120* The first quad is special in the (optimized) depth-testing code:121* the quads' Z coordinates are step-wise interpolated with respect122* to the first quad in the list.123* For multi-pass algorithms we need to produce exactly the same124* Z values in each pass. If interpolation starts with different quads125* we can get different Z values for the same (x,y).126*/127if (!shade_quad(qs, quads[i]) && i > 0)128continue; /* quad totally culled/killed */129130if (/*do_coverage*/ 0)131coverage_quad( qs, quads[i] );132133quads[nr_quads++] = quads[i];134}135136if (nr_quads)137qs->next->run(qs->next, quads, nr_quads);138}139140141/**142* Per-primitive (or per-begin?) setup143*/144static void145shade_begin(struct quad_stage *qs)146{147qs->next->begin(qs->next);148}149150151static void152shade_destroy(struct quad_stage *qs)153{154FREE( qs );155}156157158struct quad_stage *159sp_quad_shade_stage( struct softpipe_context *softpipe )160{161struct quad_shade_stage *qss = CALLOC_STRUCT(quad_shade_stage);162if (!qss)163goto fail;164165qss->stage.softpipe = softpipe;166qss->stage.begin = shade_begin;167qss->stage.run = shade_quads;168qss->stage.destroy = shade_destroy;169170return &qss->stage;171172fail:173FREE(qss);174return NULL;175}176177178