Home | History | Annotate | Download | only in softpipe
      1 /**************************************************************************
      2  *
      3  * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
      4  * All Rights Reserved.
      5  * Copyright 2008 VMware, Inc.  All rights reserved.
      6  *
      7  * Permission is hereby granted, free of charge, to any person obtaining a
      8  * copy of this software and associated documentation files (the
      9  * "Software"), to deal in the Software without restriction, including
     10  * without limitation the rights to use, copy, modify, merge, publish,
     11  * distribute, sub license, and/or sell copies of the Software, and to
     12  * permit persons to whom the Software is furnished to do so, subject to
     13  * the following conditions:
     14  *
     15  * The above copyright notice and this permission notice (including the
     16  * next paragraph) shall be included in all copies or substantial portions
     17  * of the Software.
     18  *
     19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
     20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
     22  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
     23  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
     24  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
     25  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     26  *
     27  **************************************************************************/
     28 
     29 /* Vertices are just an array of floats, with all the attributes
     30  * packed.  We currently assume a layout like:
     31  *
     32  * attr[0][0..3] - window position
     33  * attr[1..n][0..3] - remaining attributes.
     34  *
     35  * Attributes are assumed to be 4 floats wide but are packed so that
     36  * all the enabled attributes run contiguously.
     37  */
     38 
     39 #include "util/u_math.h"
     40 #include "util/u_memory.h"
     41 #include "pipe/p_defines.h"
     42 #include "pipe/p_shader_tokens.h"
     43 
     44 #include "sp_context.h"
     45 #include "sp_state.h"
     46 #include "sp_quad.h"
     47 #include "sp_quad_pipe.h"
     48 
     49 
     50 struct quad_shade_stage
     51 {
     52    struct quad_stage stage;  /**< base class */
     53 
     54    /* no other fields at this time */
     55 };
     56 
     57 
     58 /** cast wrapper */
     59 static INLINE struct quad_shade_stage *
     60 quad_shade_stage(struct quad_stage *qs)
     61 {
     62    return (struct quad_shade_stage *) qs;
     63 }
     64 
     65 
     66 /**
     67  * Execute fragment shader for the four fragments in the quad.
     68  * \return TRUE if quad is alive, FALSE if all four pixels are killed
     69  */
     70 static INLINE boolean
     71 shade_quad(struct quad_stage *qs, struct quad_header *quad)
     72 {
     73    struct softpipe_context *softpipe = qs->softpipe;
     74    struct tgsi_exec_machine *machine = softpipe->fs_machine;
     75 
     76    /* run shader */
     77    machine->flatshade_color = softpipe->rasterizer->flatshade ? TRUE : FALSE;
     78    return softpipe->fs_variant->run( softpipe->fs_variant, machine, quad );
     79 }
     80 
     81 
     82 
     83 static void
     84 coverage_quad(struct quad_stage *qs, struct quad_header *quad)
     85 {
     86    struct softpipe_context *softpipe = qs->softpipe;
     87    uint cbuf;
     88 
     89    /* loop over colorbuffer outputs */
     90    for (cbuf = 0; cbuf < softpipe->framebuffer.nr_cbufs; cbuf++) {
     91       float (*quadColor)[4] = quad->output.color[cbuf];
     92       unsigned j;
     93       for (j = 0; j < TGSI_QUAD_SIZE; j++) {
     94          assert(quad->input.coverage[j] >= 0.0);
     95          assert(quad->input.coverage[j] <= 1.0);
     96          quadColor[3][j] *= quad->input.coverage[j];
     97       }
     98    }
     99 }
    100 
    101 
    102 /**
    103  * Shade/write an array of quads
    104  * Called via quad_stage::run()
    105  */
    106 static void
    107 shade_quads(struct quad_stage *qs,
    108             struct quad_header *quads[],
    109             unsigned nr)
    110 {
    111    struct softpipe_context *softpipe = qs->softpipe;
    112    struct tgsi_exec_machine *machine = softpipe->fs_machine;
    113    unsigned i, nr_quads = 0;
    114 
    115    tgsi_exec_set_constant_buffers(machine, PIPE_MAX_CONSTANT_BUFFERS,
    116                          softpipe->mapped_constants[PIPE_SHADER_FRAGMENT],
    117                          softpipe->const_buffer_size[PIPE_SHADER_FRAGMENT]);
    118 
    119    machine->InterpCoefs = quads[0]->coef;
    120 
    121    for (i = 0; i < nr; i++) {
    122       /* Only omit this quad from the output list if all the fragments
    123        * are killed _AND_ it's not the first quad in the list.
    124        * The first quad is special in the (optimized) depth-testing code:
    125        * the quads' Z coordinates are step-wise interpolated with respect
    126        * to the first quad in the list.
    127        * For multi-pass algorithms we need to produce exactly the same
    128        * Z values in each pass.  If interpolation starts with different quads
    129        * we can get different Z values for the same (x,y).
    130        */
    131       if (!shade_quad(qs, quads[i]) && i > 0)
    132          continue; /* quad totally culled/killed */
    133 
    134       if (/*do_coverage*/ 0)
    135          coverage_quad( qs, quads[i] );
    136 
    137       quads[nr_quads++] = quads[i];
    138    }
    139 
    140    if (nr_quads)
    141       qs->next->run(qs->next, quads, nr_quads);
    142 }
    143 
    144 
    145 /**
    146  * Per-primitive (or per-begin?) setup
    147  */
    148 static void
    149 shade_begin(struct quad_stage *qs)
    150 {
    151    qs->next->begin(qs->next);
    152 }
    153 
    154 
    155 static void
    156 shade_destroy(struct quad_stage *qs)
    157 {
    158    FREE( qs );
    159 }
    160 
    161 
    162 struct quad_stage *
    163 sp_quad_shade_stage( struct softpipe_context *softpipe )
    164 {
    165    struct quad_shade_stage *qss = CALLOC_STRUCT(quad_shade_stage);
    166    if (!qss)
    167       goto fail;
    168 
    169    qss->stage.softpipe = softpipe;
    170    qss->stage.begin = shade_begin;
    171    qss->stage.run = shade_quads;
    172    qss->stage.destroy = shade_destroy;
    173 
    174    return &qss->stage;
    175 
    176 fail:
    177    FREE(qss);
    178    return NULL;
    179 }
    180