sp_quad_fs.c revision ed6f41e2f467f5b9338320a96202c7dfd181422f
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_headers.h"
47#include "sp_quad_pipe.h"
48#include "sp_texture.h"
49#include "sp_tex_sample.h"
50
51
52struct quad_shade_stage
53{
54   struct quad_stage stage;  /**< base class */
55   struct tgsi_exec_machine machine;
56   struct tgsi_exec_vector *inputs, *outputs;
57};
58
59
60/** cast wrapper */
61static INLINE struct quad_shade_stage *
62quad_shade_stage(struct quad_stage *qs)
63{
64   return (struct quad_shade_stage *) qs;
65}
66
67
68
69/**
70 * Execute fragment shader for the four fragments in the quad.
71 */
72static void
73shade_quad(
74   struct quad_stage *qs,
75   struct quad_header *quad )
76{
77   struct quad_shade_stage *qss = quad_shade_stage( qs );
78   struct softpipe_context *softpipe = qs->softpipe;
79   struct tgsi_exec_machine *machine = &qss->machine;
80   boolean z_written;
81
82   /* Consts do not require 16 byte alignment. */
83   machine->Consts = softpipe->mapped_constants[PIPE_SHADER_FRAGMENT];
84
85   machine->InterpCoefs = quad->coef;
86
87   /* run shader */
88   quad->inout.mask &= softpipe->fs->run( softpipe->fs,
89				    &qss->machine,
90				    quad );
91
92   /* store outputs */
93   z_written = FALSE;
94   {
95      const ubyte *sem_name = softpipe->fs->info.output_semantic_name;
96      const ubyte *sem_index = softpipe->fs->info.output_semantic_index;
97      const uint n = qss->stage.softpipe->fs->info.num_outputs;
98      uint i;
99      for (i = 0; i < n; i++) {
100         switch (sem_name[i]) {
101         case TGSI_SEMANTIC_COLOR:
102            {
103               uint cbuf = sem_index[i];
104               memcpy(quad->output.color[cbuf],
105                      &machine->Outputs[i].xyzw[0].f[0],
106                      sizeof(quad->output.color[0]) );
107            }
108            break;
109         case TGSI_SEMANTIC_POSITION:
110            {
111               uint j;
112               for (j = 0; j < 4; j++) {
113                  quad->output.depth[j] = machine->Outputs[0].xyzw[2].f[j];
114               }
115               z_written = TRUE;
116            }
117            break;
118         }
119      }
120   }
121
122   if (!z_written) {
123      /* compute Z values now, as in the quad earlyz stage */
124      /* XXX we should really only do this if the earlyz stage is not used */
125      const float fx = (float) quad->input.x0;
126      const float fy = (float) quad->input.y0;
127      const float dzdx = quad->posCoef->dadx[2];
128      const float dzdy = quad->posCoef->dady[2];
129      const float z0 = quad->posCoef->a0[2] + dzdx * fx + dzdy * fy;
130
131      quad->output.depth[0] = z0;
132      quad->output.depth[1] = z0 + dzdx;
133      quad->output.depth[2] = z0 + dzdy;
134      quad->output.depth[3] = z0 + dzdx + dzdy;
135   }
136
137   /* shader may cull fragments */
138   if( quad->inout.mask ) {
139      qs->next->run( qs->next, quad );
140   }
141}
142
143/**
144 * Per-primitive (or per-begin?) setup
145 */
146static void shade_begin(struct quad_stage *qs)
147{
148   struct quad_shade_stage *qss = quad_shade_stage(qs);
149   struct softpipe_context *softpipe = qs->softpipe;
150
151   softpipe->fs->prepare( softpipe->fs,
152			  &qss->machine,
153			  (struct tgsi_sampler **)
154                             softpipe->tgsi.frag_samplers_list );
155
156   qs->next->begin(qs->next);
157}
158
159
160static void shade_destroy(struct quad_stage *qs)
161{
162   struct quad_shade_stage *qss = (struct quad_shade_stage *) qs;
163
164   tgsi_exec_machine_free_data(&qss->machine);
165   FREE( qss->inputs );
166   FREE( qss->outputs );
167   FREE( qs );
168}
169
170
171struct quad_stage *sp_quad_shade_stage( struct softpipe_context *softpipe )
172{
173   struct quad_shade_stage *qss = CALLOC_STRUCT(quad_shade_stage);
174
175   /* allocate storage for program inputs/outputs, aligned to 16 bytes */
176   qss->inputs = MALLOC(PIPE_MAX_ATTRIBS * sizeof(*qss->inputs) + 16);
177   qss->outputs = MALLOC(PIPE_MAX_ATTRIBS * sizeof(*qss->outputs) + 16);
178   qss->machine.Inputs = align16(qss->inputs);
179   qss->machine.Outputs = align16(qss->outputs);
180
181   qss->stage.softpipe = softpipe;
182   qss->stage.begin = shade_begin;
183   qss->stage.run = shade_quad;
184   qss->stage.destroy = shade_destroy;
185
186   tgsi_exec_machine_init( &qss->machine );
187
188   return &qss->stage;
189}
190