sp_quad_fs.c

Go to the documentation of this file.
00001 /**************************************************************************
00002  * 
00003  * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
00004  * All Rights Reserved.
00005  * 
00006  * Permission is hereby granted, free of charge, to any person obtaining a
00007  * copy of this software and associated documentation files (the
00008  * "Software"), to deal in the Software without restriction, including
00009  * without limitation the rights to use, copy, modify, merge, publish,
00010  * distribute, sub license, and/or sell copies of the Software, and to
00011  * permit persons to whom the Software is furnished to do so, subject to
00012  * the following conditions:
00013  * 
00014  * The above copyright notice and this permission notice (including the
00015  * next paragraph) shall be included in all copies or substantial portions
00016  * of the Software.
00017  * 
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00019  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00020  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
00021  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
00022  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
00023  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
00024  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00025  * 
00026  **************************************************************************/
00027 
00028 /* Vertices are just an array of floats, with all the attributes
00029  * packed.  We currently assume a layout like:
00030  *
00031  * attr[0][0..3] - window position
00032  * attr[1..n][0..3] - remaining attributes.
00033  *
00034  * Attributes are assumed to be 4 floats wide but are packed so that
00035  * all the enabled attributes run contiguously.
00036  */
00037 
00038 #include "util/u_math.h"
00039 #include "util/u_memory.h"
00040 #include "pipe/p_defines.h"
00041 #include "pipe/p_shader_tokens.h"
00042 
00043 #include "sp_context.h"
00044 #include "sp_state.h"
00045 #include "sp_headers.h"
00046 #include "sp_quad.h"
00047 #include "sp_texture.h"
00048 #include "sp_tex_sample.h"
00049 
00050 
00051 struct quad_shade_stage
00052 {
00053    struct quad_stage stage;
00054    struct tgsi_sampler samplers[PIPE_MAX_SAMPLERS];
00055    struct tgsi_exec_machine machine;
00056    struct tgsi_exec_vector *inputs, *outputs;
00057 };
00058 
00059 
00061 static INLINE struct quad_shade_stage *
00062 quad_shade_stage(struct quad_stage *qs)
00063 {
00064    return (struct quad_shade_stage *) qs;
00065 }
00066 
00067 
00068 
00072 static void
00073 shade_quad(
00074    struct quad_stage *qs,
00075    struct quad_header *quad )
00076 {
00077    struct quad_shade_stage *qss = quad_shade_stage( qs );
00078    struct softpipe_context *softpipe = qs->softpipe;
00079    struct tgsi_exec_machine *machine = &qss->machine;
00080    boolean z_written;
00081    
00082    /* Consts do not require 16 byte alignment. */
00083    machine->Consts = softpipe->mapped_constants[PIPE_SHADER_FRAGMENT];
00084 
00085    machine->InterpCoefs = quad->coef;
00086 
00087    /* run shader */
00088    quad->inout.mask &= softpipe->fs->run( softpipe->fs, 
00089                                     &qss->machine,
00090                                     quad );
00091 
00092    /* store outputs */
00093    z_written = FALSE;
00094    {
00095       const ubyte *sem_name = softpipe->fs->info.output_semantic_name;
00096       const ubyte *sem_index = softpipe->fs->info.output_semantic_index;
00097       const uint n = qss->stage.softpipe->fs->info.num_outputs;
00098       uint i;
00099       for (i = 0; i < n; i++) {
00100          switch (sem_name[i]) {
00101          case TGSI_SEMANTIC_COLOR:
00102             {
00103                uint cbuf = sem_index[i];
00104                memcpy(quad->output.color[cbuf],
00105                       &machine->Outputs[i].xyzw[0].f[0],
00106                       sizeof(quad->output.color[0]) );
00107             }
00108             break;
00109          case TGSI_SEMANTIC_POSITION:
00110             {
00111                uint j;
00112                for (j = 0; j < 4; j++) {
00113                   quad->output.depth[j] = machine->Outputs[0].xyzw[2].f[j];
00114                }
00115                z_written = TRUE;
00116             }
00117             break;
00118          }
00119       }
00120    }
00121 
00122    if (!z_written) {
00123       /* compute Z values now, as in the quad earlyz stage */
00124       /* XXX we should really only do this if the earlyz stage is not used */
00125       const float fx = (float) quad->input.x0;
00126       const float fy = (float) quad->input.y0;
00127       const float dzdx = quad->posCoef->dadx[2];
00128       const float dzdy = quad->posCoef->dady[2];
00129       const float z0 = quad->posCoef->a0[2] + dzdx * fx + dzdy * fy;
00130 
00131       quad->output.depth[0] = z0;
00132       quad->output.depth[1] = z0 + dzdx;
00133       quad->output.depth[2] = z0 + dzdy;
00134       quad->output.depth[3] = z0 + dzdx + dzdy;
00135    }
00136 
00137    /* shader may cull fragments */
00138    if( quad->inout.mask ) {
00139       qs->next->run( qs->next, quad );
00140    }
00141 }
00142 
00146 static void shade_begin(struct quad_stage *qs)
00147 {
00148    struct quad_shade_stage *qss = quad_shade_stage(qs);
00149    struct softpipe_context *softpipe = qs->softpipe;
00150    unsigned i;
00151    unsigned num = MAX2(softpipe->num_textures, softpipe->num_samplers);
00152 
00153    /* set TGSI sampler state that varies */
00154    for (i = 0; i < num; i++) {
00155       qss->samplers[i].state = softpipe->sampler[i];
00156       qss->samplers[i].texture = softpipe->texture[i];
00157    }
00158 
00159    softpipe->fs->prepare( softpipe->fs, 
00160                           &qss->machine,
00161                           qss->samplers );
00162 
00163    qs->next->begin(qs->next);
00164 }
00165 
00166 
00167 static void shade_destroy(struct quad_stage *qs)
00168 {
00169    struct quad_shade_stage *qss = (struct quad_shade_stage *) qs;
00170 
00171    tgsi_exec_machine_free_data(&qss->machine);
00172    FREE( qss->inputs );
00173    FREE( qss->outputs );
00174    FREE( qs );
00175 }
00176 
00177 
00178 struct quad_stage *sp_quad_shade_stage( struct softpipe_context *softpipe )
00179 {
00180    struct quad_shade_stage *qss = CALLOC_STRUCT(quad_shade_stage);
00181    uint i;
00182 
00183    /* allocate storage for program inputs/outputs, aligned to 16 bytes */
00184    qss->inputs = MALLOC(PIPE_MAX_ATTRIBS * sizeof(*qss->inputs) + 16);
00185    qss->outputs = MALLOC(PIPE_MAX_ATTRIBS * sizeof(*qss->outputs) + 16);
00186    qss->machine.Inputs = align16(qss->inputs);
00187    qss->machine.Outputs = align16(qss->outputs);
00188 
00189    qss->stage.softpipe = softpipe;
00190    qss->stage.begin = shade_begin;
00191    qss->stage.run = shade_quad;
00192    qss->stage.destroy = shade_destroy;
00193 
00194    /* set TGSI sampler state that's constant */
00195    for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
00196       assert(softpipe->tex_cache[i]);
00197       qss->samplers[i].get_samples = sp_get_samples;
00198       qss->samplers[i].pipe = &softpipe->pipe;
00199       qss->samplers[i].cache = softpipe->tex_cache[i];
00200    }
00201 
00202    tgsi_exec_machine_init( &qss->machine );
00203 
00204    return &qss->stage;
00205 }

Generated on Tue Sep 29 06:25:17 2009 for Gallium3D by  doxygen 1.5.4