Merge branch 'upstream-gallium-0.1' into nouveau-gallium-0.1
[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 "pipe/p_util.h"
39 #include "pipe/p_defines.h"
40 #include "pipe/p_shader_tokens.h"
41
42 #include "sp_context.h"
43 #include "sp_state.h"
44 #include "sp_headers.h"
45 #include "sp_quad.h"
46 #include "sp_texture.h"
47 #include "sp_tex_sample.h"
48
49
50 struct quad_shade_stage
51 {
52 struct quad_stage stage;
53 struct tgsi_sampler samplers[PIPE_MAX_SAMPLERS];
54 struct tgsi_exec_machine machine;
55 struct tgsi_exec_vector *inputs, *outputs;
56 int colorOutSlot, depthOutSlot;
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 /**
70 * Execute fragment shader for the four fragments in the quad.
71 */
72 static void
73 shade_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
81 /* Consts do not require 16 byte alignment. */
82 machine->Consts = softpipe->mapped_constants[PIPE_SHADER_FRAGMENT];
83
84 machine->InterpCoefs = quad->coef;
85
86 /* run shader */
87 quad->mask &= softpipe->fs->run( softpipe->fs,
88 &qss->machine,
89 quad );
90
91 /* store result color */
92 if (qss->colorOutSlot >= 0) {
93 /* XXX need to handle multiple color outputs someday */
94 assert(qss->stage.softpipe->fs->info.output_semantic_name[qss->colorOutSlot]
95 == TGSI_SEMANTIC_COLOR);
96 memcpy(
97 quad->outputs.color,
98 &machine->Outputs[qss->colorOutSlot].xyzw[0].f[0],
99 sizeof( quad->outputs.color ) );
100 }
101
102 /*
103 * XXX the following code for updating quad->outputs.depth
104 * isn't really needed if we did early z testing.
105 */
106
107 /* store result Z */
108 if (qss->depthOutSlot >= 0) {
109 /* output[slot] is new Z */
110 uint i;
111 for (i = 0; i < 4; i++) {
112 quad->outputs.depth[i] = machine->Outputs[0].xyzw[2].f[i];
113 }
114 }
115 else {
116 /* copy input Z (which was interpolated by the executor) to output Z */
117 uint i;
118 for (i = 0; i < 4; i++) {
119 quad->outputs.depth[i] = machine->Inputs[0].xyzw[2].f[i];
120 /* XXX not sure the above line is always correct. The following
121 * might be better:
122 quad->outputs.depth[i] = machine->QuadPos.xyzw[2].f[i];
123 */
124 }
125 }
126
127 /* shader may cull fragments */
128 if( quad->mask ) {
129 qs->next->run( qs->next, quad );
130 }
131 }
132
133 /**
134 * Per-primitive (or per-begin?) setup
135 */
136 static void shade_begin(struct quad_stage *qs)
137 {
138 struct quad_shade_stage *qss = quad_shade_stage(qs);
139 struct softpipe_context *softpipe = qs->softpipe;
140 unsigned i;
141
142 /* set TGSI sampler state that varies */
143 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
144 qss->samplers[i].state = softpipe->sampler[i];
145 qss->samplers[i].texture = softpipe->texture[i];
146 }
147
148 /* find output slots for depth, color */
149 qss->colorOutSlot = -1;
150 qss->depthOutSlot = -1;
151 for (i = 0; i < qss->stage.softpipe->fs->info.num_outputs; i++) {
152 switch (qss->stage.softpipe->fs->info.output_semantic_name[i]) {
153 case TGSI_SEMANTIC_POSITION:
154 qss->depthOutSlot = i;
155 break;
156 case TGSI_SEMANTIC_COLOR:
157 qss->colorOutSlot = i;
158 break;
159 }
160 }
161
162 softpipe->fs->prepare( softpipe->fs,
163 &qss->machine,
164 qss->samplers );
165
166 qs->next->begin(qs->next);
167 }
168
169
170 static void shade_destroy(struct quad_stage *qs)
171 {
172 struct quad_shade_stage *qss = (struct quad_shade_stage *) qs;
173
174 tgsi_exec_machine_free_data(&qss->machine);
175 FREE( qss->inputs );
176 FREE( qss->outputs );
177 FREE( qs );
178 }
179
180
181 struct quad_stage *sp_quad_shade_stage( struct softpipe_context *softpipe )
182 {
183 struct quad_shade_stage *qss = CALLOC_STRUCT(quad_shade_stage);
184 uint i;
185
186 /* allocate storage for program inputs/outputs, aligned to 16 bytes */
187 qss->inputs = MALLOC(PIPE_ATTRIB_MAX * sizeof(*qss->inputs) + 16);
188 qss->outputs = MALLOC(PIPE_ATTRIB_MAX * sizeof(*qss->outputs) + 16);
189 qss->machine.Inputs = align16(qss->inputs);
190 qss->machine.Outputs = align16(qss->outputs);
191
192 qss->stage.softpipe = softpipe;
193 qss->stage.begin = shade_begin;
194 qss->stage.run = shade_quad;
195 qss->stage.destroy = shade_destroy;
196
197 /* set TGSI sampler state that's constant */
198 for (i = 0; i < PIPE_MAX_SAMPLERS; i++) {
199 assert(softpipe->tex_cache[i]);
200 qss->samplers[i].get_samples = sp_get_samples;
201 qss->samplers[i].pipe = &softpipe->pipe;
202 qss->samplers[i].cache = softpipe->tex_cache[i];
203 }
204
205 tgsi_exec_machine_init( &qss->machine );
206
207 return &qss->stage;
208 }