Merge commit 'origin/gallium-0.1' into gallium-0.2
[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 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /* Vertices are just an array of floats, with all the attributes
29 * packed. We currently assume a layout like:
30 *
31 * attr[0][0..3] - window position
32 * attr[1..n][0..3] - remaining attributes.
33 *
34 * Attributes are assumed to be 4 floats wide but are packed so that
35 * all the enabled attributes run contiguously.
36 */
37
38 #include "util/u_math.h"
39 #include "util/u_memory.h"
40 #include "pipe/p_defines.h"
41 #include "pipe/p_shader_tokens.h"
42
43 #include "sp_context.h"
44 #include "sp_state.h"
45 #include "sp_headers.h"
46 #include "sp_quad.h"
47 #include "sp_texture.h"
48 #include "sp_tex_sample.h"
49
50
51 struct quad_shade_stage
52 {
53 struct quad_stage stage; /**< base class */
54 struct sp_shader_sampler samplers[PIPE_MAX_SAMPLERS];
55 struct sp_shader_sampler *samplers_list[PIPE_MAX_SAMPLERS];
56 struct tgsi_exec_machine machine;
57 struct tgsi_exec_vector *inputs, *outputs;
58 };
59
60
61 /** cast wrapper */
62 static INLINE struct quad_shade_stage *
63 quad_shade_stage(struct quad_stage *qs)
64 {
65 return (struct quad_shade_stage *) qs;
66 }
67
68
69
70 /**
71 * Execute fragment shader for the four fragments in the quad.
72 */
73 static void
74 shade_quad(
75 struct quad_stage *qs,
76 struct quad_header *quad )
77 {
78 struct quad_shade_stage *qss = quad_shade_stage( qs );
79 struct softpipe_context *softpipe = qs->softpipe;
80 struct tgsi_exec_machine *machine = &qss->machine;
81 boolean z_written;
82
83 /* Consts do not require 16 byte alignment. */
84 machine->Consts = softpipe->mapped_constants[PIPE_SHADER_FRAGMENT];
85
86 machine->InterpCoefs = quad->coef;
87
88 /* run shader */
89 quad->inout.mask &= softpipe->fs->run( softpipe->fs,
90 &qss->machine,
91 quad );
92
93 /* store outputs */
94 z_written = FALSE;
95 {
96 const ubyte *sem_name = softpipe->fs->info.output_semantic_name;
97 const ubyte *sem_index = softpipe->fs->info.output_semantic_index;
98 const uint n = qss->stage.softpipe->fs->info.num_outputs;
99 uint i;
100 for (i = 0; i < n; i++) {
101 switch (sem_name[i]) {
102 case TGSI_SEMANTIC_COLOR:
103 {
104 uint cbuf = sem_index[i];
105 memcpy(quad->output.color[cbuf],
106 &machine->Outputs[i].xyzw[0].f[0],
107 sizeof(quad->output.color[0]) );
108 }
109 break;
110 case TGSI_SEMANTIC_POSITION:
111 {
112 uint j;
113 for (j = 0; j < 4; j++) {
114 quad->output.depth[j] = machine->Outputs[0].xyzw[2].f[j];
115 }
116 z_written = TRUE;
117 }
118 break;
119 }
120 }
121 }
122
123 if (!z_written) {
124 /* compute Z values now, as in the quad earlyz stage */
125 /* XXX we should really only do this if the earlyz stage is not used */
126 const float fx = (float) quad->input.x0;
127 const float fy = (float) quad->input.y0;
128 const float dzdx = quad->posCoef->dadx[2];
129 const float dzdy = quad->posCoef->dady[2];
130 const float z0 = quad->posCoef->a0[2] + dzdx * fx + dzdy * fy;
131
132 quad->output.depth[0] = z0;
133 quad->output.depth[1] = z0 + dzdx;
134 quad->output.depth[2] = z0 + dzdy;
135 quad->output.depth[3] = z0 + dzdx + dzdy;
136 }
137
138 /* shader may cull fragments */
139 if( quad->inout.mask ) {
140 qs->next->run( qs->next, quad );
141 }
142 }
143
144 /**
145 * Per-primitive (or per-begin?) setup
146 */
147 static void shade_begin(struct quad_stage *qs)
148 {
149 struct quad_shade_stage *qss = quad_shade_stage(qs);
150 struct softpipe_context *softpipe = qs->softpipe;
151
152 softpipe->fs->prepare( softpipe->fs,
153 &qss->machine,
154 (struct tgsi_sampler **) qss->samplers_list );
155
156 qs->next->begin(qs->next);
157 }
158
159
160 static 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
171 struct quad_stage *sp_quad_shade_stage( struct softpipe_context *softpipe )
172 {
173 struct quad_shade_stage *qss = CALLOC_STRUCT(quad_shade_stage);
174 uint i;
175
176 /* allocate storage for program inputs/outputs, aligned to 16 bytes */
177 qss->inputs = MALLOC(PIPE_MAX_ATTRIBS * sizeof(*qss->inputs) + 16);
178 qss->outputs = MALLOC(PIPE_MAX_ATTRIBS * sizeof(*qss->outputs) + 16);
179 qss->machine.Inputs = align16(qss->inputs);
180 qss->machine.Outputs = align16(qss->outputs);
181
182 qss->stage.softpipe = softpipe;
183 qss->stage.begin = shade_begin;
184 qss->stage.run = shade_quad;
185 qss->stage.destroy = shade_destroy;
186
187 /* setup TGSI sampler state */
188 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
189 assert(softpipe->tex_cache[i]);
190 qss->samplers[i].base.get_samples = sp_get_samples;
191 qss->samplers[i].unit = i;
192 qss->samplers[i].sp = softpipe;
193 qss->samplers[i].cache = softpipe->tex_cache[i];
194 qss->samplers_list[i] = &qss->samplers[i];
195 }
196
197 tgsi_exec_machine_init( &qss->machine );
198
199 return &qss->stage;
200 }