dfc7eeaa7e661021bf04cfa378e0a7408a535e5c
[mesa.git] / src / gallium / drivers / llvmpipe / lp_quad_fs.c
1 /**************************************************************************
2 *
3 * Copyright 2008-2009 VMware, Inc.
4 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * 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 "lp_context.h"
45 #include "lp_state.h"
46 #include "lp_quad.h"
47 #include "lp_quad_pipe.h"
48 #include "lp_texture.h"
49 #include "lp_tex_sample.h"
50
51
52 struct quad_shade_stage
53 {
54 struct quad_stage stage; /**< base class */
55
56 union tgsi_exec_channel ALIGN16_ATTRIB pos[NUM_CHANNELS];
57 float ALIGN16_ATTRIB a0[PIPE_MAX_SHADER_INPUTS][NUM_CHANNELS];
58 float ALIGN16_ATTRIB dadx[PIPE_MAX_SHADER_INPUTS][NUM_CHANNELS];
59 float ALIGN16_ATTRIB dady[PIPE_MAX_SHADER_INPUTS][NUM_CHANNELS];
60
61 struct tgsi_exec_vector ALIGN16_ATTRIB outputs[PIPE_MAX_ATTRIBS];
62 };
63
64
65 /** cast wrapper */
66 static INLINE struct quad_shade_stage *
67 quad_shade_stage(struct quad_stage *qs)
68 {
69 return (struct quad_shade_stage *) qs;
70 }
71
72
73 static void
74 setup_pos_vector(struct quad_shade_stage *qss,
75 const struct tgsi_interp_coef *coef,
76 float x, float y)
77 {
78 uint chan;
79
80 /* do X */
81 qss->pos[0].f[0] = x;
82 qss->pos[0].f[1] = x + 1;
83 qss->pos[0].f[2] = x;
84 qss->pos[0].f[3] = x + 1;
85
86 /* do Y */
87 qss->pos[1].f[0] = y;
88 qss->pos[1].f[1] = y;
89 qss->pos[1].f[2] = y + 1;
90 qss->pos[1].f[3] = y + 1;
91
92 /* do Z and W for all fragments in the quad */
93 for (chan = 2; chan < 4; chan++) {
94 const float dadx = coef->dadx[chan];
95 const float dady = coef->dady[chan];
96 const float a0 = coef->a0[chan] + dadx * x + dady * y;
97 qss->pos[chan].f[0] = a0;
98 qss->pos[chan].f[1] = a0 + dadx;
99 qss->pos[chan].f[2] = a0 + dady;
100 qss->pos[chan].f[3] = a0 + dadx + dady;
101 }
102 }
103
104
105 static void
106 setup_coef_vector(struct quad_shade_stage *qss,
107 const struct tgsi_interp_coef *coef)
108 {
109 unsigned num_inputs = qss->stage.llvmpipe->fs->info.num_inputs;
110 unsigned attrib, chan, i;
111
112 for (attrib = 0; attrib < num_inputs; ++attrib) {
113 for (chan = 0; chan < NUM_CHANNELS; ++chan) {
114 qss->a0[attrib][chan] = coef[attrib].a0[chan];
115 qss->dadx[attrib][chan] = coef[attrib].dadx[chan];
116 qss->dady[attrib][chan] = coef[attrib].dady[chan];
117 }
118 }
119 }
120
121
122 /**
123 * Execute fragment shader for the four fragments in the quad.
124 */
125 static boolean
126 shade_quad(struct quad_stage *qs, struct quad_header *quad)
127 {
128 struct quad_shade_stage *qss = quad_shade_stage( qs );
129 struct llvmpipe_context *llvmpipe = qs->llvmpipe;
130 void *constants;
131 struct tgsi_sampler **samplers;
132 boolean z_written;
133
134 /* Compute X, Y, Z, W vals for this quad */
135 setup_pos_vector(qss,
136 quad->posCoef,
137 (float)quad->input.x0, (float)quad->input.y0);
138
139
140 constants = llvmpipe->mapped_constants[PIPE_SHADER_FRAGMENT];
141 samplers = (struct tgsi_sampler **)llvmpipe->tgsi.frag_samplers_list;
142
143 /* run shader */
144 llvmpipe->fs->jit_function( qss->pos,
145 qss->a0, qss->dadx, qss->dady,
146 constants,
147 qss->outputs,
148 samplers);
149
150 /* FIXME */
151 #if 0
152 quad->inout.mask &= ... ;
153 if (quad->inout.mask == 0)
154 return FALSE;
155 #endif
156
157 /* store outputs */
158 z_written = FALSE;
159 {
160 const ubyte *sem_name = llvmpipe->fs->info.output_semantic_name;
161 const ubyte *sem_index = llvmpipe->fs->info.output_semantic_index;
162 const uint n = qss->stage.llvmpipe->fs->info.num_outputs;
163 uint i;
164 for (i = 0; i < n; i++) {
165 switch (sem_name[i]) {
166 case TGSI_SEMANTIC_COLOR:
167 {
168 uint cbuf = sem_index[i];
169 memcpy(quad->output.color[cbuf],
170 &qss->outputs[i].xyzw[0].f[0],
171 sizeof(quad->output.color[0]) );
172 }
173 break;
174 case TGSI_SEMANTIC_POSITION:
175 {
176 uint j;
177 for (j = 0; j < 4; j++) {
178 quad->output.depth[j] = qss->outputs[0].xyzw[2].f[j];
179 }
180 z_written = TRUE;
181 }
182 break;
183 }
184 }
185 }
186
187 return TRUE;
188 }
189
190
191
192 static void
193 coverage_quad(struct quad_stage *qs, struct quad_header *quad)
194 {
195 struct llvmpipe_context *llvmpipe = qs->llvmpipe;
196 uint cbuf;
197
198 /* loop over colorbuffer outputs */
199 for (cbuf = 0; cbuf < llvmpipe->framebuffer.nr_cbufs; cbuf++) {
200 float (*quadColor)[4] = quad->output.color[cbuf];
201 unsigned j;
202 for (j = 0; j < QUAD_SIZE; j++) {
203 assert(quad->input.coverage[j] >= 0.0);
204 assert(quad->input.coverage[j] <= 1.0);
205 quadColor[3][j] *= quad->input.coverage[j];
206 }
207 }
208 }
209
210
211
212 static void
213 shade_quads(struct quad_stage *qs,
214 struct quad_header *quads[],
215 unsigned nr)
216 {
217 struct quad_shade_stage *qss = quad_shade_stage( qs );
218 unsigned i, pass = 0;
219
220 setup_coef_vector(qss,
221 quads[0]->coef);
222
223 for (i = 0; i < nr; i++) {
224 if (!shade_quad(qs, quads[i]))
225 continue;
226
227 if (/*do_coverage*/ 0)
228 coverage_quad( qs, quads[i] );
229
230 quads[pass++] = quads[i];
231 }
232
233 if (pass)
234 qs->next->run(qs->next, quads, pass);
235 }
236
237
238
239
240
241 /**
242 * Per-primitive (or per-begin?) setup
243 */
244 static void
245 shade_begin(struct quad_stage *qs)
246 {
247 qs->next->begin(qs->next);
248 }
249
250
251 static void
252 shade_destroy(struct quad_stage *qs)
253 {
254 FREE( qs );
255 }
256
257
258 struct quad_stage *
259 lp_quad_shade_stage( struct llvmpipe_context *llvmpipe )
260 {
261 struct quad_shade_stage *qss;
262
263 qss = CALLOC_STRUCT(quad_shade_stage);
264 if (!qss)
265 return NULL;
266
267 qss->stage.llvmpipe = llvmpipe;
268 qss->stage.begin = shade_begin;
269 qss->stage.run = shade_quads;
270 qss->stage.destroy = shade_destroy;
271
272 return &qss->stage;
273 }