56a8f55d7747b0a06edf025ee1b110fd16aff6ce
[mesa.git] / src / gallium / drivers / softpipe / sp_quad_fs.c
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 #include "sp_texture.h"
49 #include "sp_tex_sample.h"
50
51
52 struct 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 */
61 static INLINE struct quad_shade_stage *
62 quad_shade_stage(struct quad_stage *qs)
63 {
64 return (struct quad_shade_stage *) qs;
65 }
66
67
68 /**
69 * Execute fragment shader for the four fragments in the quad.
70 */
71 static boolean
72 shade_quad(struct quad_stage *qs, struct quad_header *quad)
73 {
74 struct quad_shade_stage *qss = quad_shade_stage( qs );
75 struct softpipe_context *softpipe = qs->softpipe;
76 struct tgsi_exec_machine *machine = qss->machine;
77 boolean z_written;
78
79 /* run shader */
80 quad->inout.mask &= softpipe->fs->run( softpipe->fs, machine, quad );
81 if (quad->inout.mask == 0)
82 return FALSE;
83
84 /* store outputs */
85 z_written = FALSE;
86 {
87 const ubyte *sem_name = softpipe->fs->info.output_semantic_name;
88 const ubyte *sem_index = softpipe->fs->info.output_semantic_index;
89 const uint n = qss->stage.softpipe->fs->info.num_outputs;
90 uint i;
91 for (i = 0; i < n; i++) {
92 switch (sem_name[i]) {
93 case TGSI_SEMANTIC_COLOR:
94 {
95 uint cbuf = sem_index[i];
96 memcpy(quad->output.color[cbuf],
97 &machine->Outputs[i].xyzw[0].f[0],
98 sizeof(quad->output.color[0]) );
99 }
100 break;
101 case TGSI_SEMANTIC_POSITION:
102 {
103 uint j;
104 for (j = 0; j < 4; j++) {
105 quad->output.depth[j] = machine->Outputs[0].xyzw[2].f[j];
106 }
107 z_written = TRUE;
108 }
109 break;
110 }
111 }
112 }
113
114 return TRUE;
115 }
116
117
118
119 static void
120 coverage_quad(struct quad_stage *qs, struct quad_header *quad)
121 {
122 struct softpipe_context *softpipe = qs->softpipe;
123 uint cbuf;
124
125 /* loop over colorbuffer outputs */
126 for (cbuf = 0; cbuf < softpipe->framebuffer.nr_cbufs; cbuf++) {
127 float (*quadColor)[4] = quad->output.color[cbuf];
128 unsigned j;
129 for (j = 0; j < QUAD_SIZE; j++) {
130 assert(quad->input.coverage[j] >= 0.0);
131 assert(quad->input.coverage[j] <= 1.0);
132 quadColor[3][j] *= quad->input.coverage[j];
133 }
134 }
135 }
136
137
138
139 static void
140 shade_quads(struct quad_stage *qs,
141 struct quad_header *quads[],
142 unsigned nr)
143 {
144 struct quad_shade_stage *qss = quad_shade_stage( qs );
145 struct softpipe_context *softpipe = qs->softpipe;
146 struct tgsi_exec_machine *machine = qss->machine;
147
148 unsigned i, pass = 0;
149
150 machine->Consts = softpipe->mapped_constants[PIPE_SHADER_FRAGMENT];
151 machine->InterpCoefs = quads[0]->coef;
152
153 for (i = 0; i < nr; i++) {
154 if (!shade_quad(qs, quads[i]))
155 continue;
156
157 if (/*do_coverage*/ 0)
158 coverage_quad( qs, quads[i] );
159
160 quads[pass++] = quads[i];
161 }
162
163 if (pass)
164 qs->next->run(qs->next, quads, pass);
165 }
166
167
168
169
170
171 /**
172 * Per-primitive (or per-begin?) setup
173 */
174 static void
175 shade_begin(struct quad_stage *qs)
176 {
177 struct quad_shade_stage *qss = quad_shade_stage(qs);
178 struct softpipe_context *softpipe = qs->softpipe;
179
180 softpipe->fs->prepare( softpipe->fs,
181 qss->machine,
182 (struct tgsi_sampler **)
183 softpipe->tgsi.frag_samplers_list );
184
185 qs->next->begin(qs->next);
186 }
187
188
189 static void
190 shade_destroy(struct quad_stage *qs)
191 {
192 struct quad_shade_stage *qss = (struct quad_shade_stage *) qs;
193
194 tgsi_exec_machine_destroy(qss->machine);
195
196 FREE( qs );
197 }
198
199
200 struct quad_stage *
201 sp_quad_shade_stage( struct softpipe_context *softpipe )
202 {
203 struct quad_shade_stage *qss = CALLOC_STRUCT(quad_shade_stage);
204 if (!qss)
205 goto fail;
206
207 qss->stage.softpipe = softpipe;
208 qss->stage.begin = shade_begin;
209 qss->stage.run = shade_quads;
210 qss->stage.destroy = shade_destroy;
211
212 qss->machine = tgsi_exec_machine_create();
213 if (!qss->machine)
214 goto fail;
215
216 return &qss->stage;
217
218 fail:
219 if (qss && qss->machine)
220 tgsi_exec_machine_destroy(qss->machine);
221
222 FREE(qss);
223 return NULL;
224 }