draw: add vertex shader run_linear function
[mesa.git] / src / gallium / auxiliary / draw / draw_vs_exec.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 /*
29 * Authors:
30 * Keith Whitwell <keith@tungstengraphics.com>
31 * Brian Paul
32 */
33
34 #include "pipe/p_util.h"
35 #include "pipe/p_shader_tokens.h"
36
37 #include "draw_private.h"
38 #include "draw_context.h"
39 #include "draw_vs.h"
40
41 #include "tgsi/util/tgsi_parse.h"
42
43
44 struct exec_vertex_shader {
45 struct draw_vertex_shader base;
46 struct tgsi_exec_machine *machine;
47 };
48
49 static struct exec_vertex_shader *exec_vertex_shader( struct draw_vertex_shader *vs )
50 {
51 return (struct exec_vertex_shader *)vs;
52 }
53
54
55 /* Not required for run_linear.
56 */
57 static void
58 vs_exec_prepare( struct draw_vertex_shader *shader,
59 struct draw_context *draw )
60 {
61 /* specify the vertex program to interpret/execute */
62 tgsi_exec_machine_bind_shader(&draw->machine,
63 shader->state.tokens,
64 PIPE_MAX_SAMPLERS,
65 NULL /*samplers*/ );
66
67 draw_update_vertex_fetch( draw );
68 }
69
70
71 /**
72 * Transform vertices with the current vertex program/shader
73 * Up to four vertices can be shaded at a time.
74 * \param vbuffer the input vertex data
75 * \param elts indexes of four input vertices
76 * \param count number of vertices to shade [1..4]
77 * \param vOut array of pointers to four output vertices
78 */
79 static boolean
80 vs_exec_run( struct draw_vertex_shader *shader,
81 struct draw_context *draw,
82 const unsigned *elts,
83 unsigned count,
84 void *vOut,
85 unsigned vertex_size)
86 {
87 struct tgsi_exec_machine *machine = &draw->machine;
88 unsigned int i, j;
89 unsigned int clipped = 0;
90
91 ALIGN16_DECL(struct tgsi_exec_vector, inputs, PIPE_MAX_ATTRIBS);
92 ALIGN16_DECL(struct tgsi_exec_vector, outputs, PIPE_MAX_ATTRIBS);
93 const float *scale = draw->viewport.scale;
94 const float *trans = draw->viewport.translate;
95
96 assert(shader->info.output_semantic_name[0] == TGSI_SEMANTIC_POSITION);
97
98 machine->Consts = (const float (*)[4]) draw->user.constants;
99 machine->Inputs = ALIGN16_ASSIGN(inputs);
100 if (draw->rasterizer->bypass_vs) {
101 /* outputs are just the inputs */
102 machine->Outputs = machine->Inputs;
103 }
104 else {
105 machine->Outputs = ALIGN16_ASSIGN(outputs);
106 }
107
108 for (i = 0; i < count; i += MAX_TGSI_VERTICES) {
109 unsigned int max_vertices = MIN2(MAX_TGSI_VERTICES, count - i);
110 draw->vertex_fetch.fetch_func( draw, machine, &elts[i], max_vertices );
111
112 if (!draw->rasterizer->bypass_vs) {
113 /* run interpreter */
114 tgsi_exec_machine_run( machine );
115 }
116
117 /* store machine results */
118 for (j = 0; j < max_vertices; j++) {
119 unsigned slot;
120 float x, y, z, w;
121 struct vertex_header *out =
122 draw_header_from_block(vOut, vertex_size, i + j);
123
124 /* Handle attr[0] (position) specially:
125 *
126 * XXX: Computing the clipmask should be done in the vertex
127 * program as a set of DP4 instructions appended to the
128 * user-provided code.
129 */
130 x = out->clip[0] = machine->Outputs[0].xyzw[0].f[j];
131 y = out->clip[1] = machine->Outputs[0].xyzw[1].f[j];
132 z = out->clip[2] = machine->Outputs[0].xyzw[2].f[j];
133 w = out->clip[3] = machine->Outputs[0].xyzw[3].f[j];
134
135 if (!draw->rasterizer->bypass_clipping) {
136 out->clipmask = compute_clipmask(out->clip, draw->plane,
137 draw->nr_planes);
138 clipped += out->clipmask;
139
140 /* divide by w */
141 w = 1.0f / w;
142 x *= w;
143 y *= w;
144 z *= w;
145 }
146 else {
147 out->clipmask = 0;
148 }
149 out->edgeflag = 1;
150 out->vertex_id = UNDEFINED_VERTEX_ID;
151
152 if (!draw->identity_viewport) {
153 /* Viewport mapping */
154 out->data[0][0] = x * scale[0] + trans[0];
155 out->data[0][1] = y * scale[1] + trans[1];
156 out->data[0][2] = z * scale[2] + trans[2];
157 out->data[0][3] = w;
158 }
159 else {
160 out->data[0][0] = x;
161 out->data[0][1] = y;
162 out->data[0][2] = z;
163 out->data[0][3] = w;
164 }
165
166 /* Remaining attributes are packed into sequential post-transform
167 * vertex attrib slots.
168 */
169 for (slot = 1; slot < draw->num_vs_outputs; slot++) {
170 out->data[slot][0] = machine->Outputs[slot].xyzw[0].f[j];
171 out->data[slot][1] = machine->Outputs[slot].xyzw[1].f[j];
172 out->data[slot][2] = machine->Outputs[slot].xyzw[2].f[j];
173 out->data[slot][3] = machine->Outputs[slot].xyzw[3].f[j];
174 }
175
176 #if 0 /*DEBUG*/
177 printf("%d) Post xform vert:\n", i + j);
178 for (slot = 0; slot < draw->num_vs_outputs; slot++) {
179 printf("\t%d: %f %f %f %f\n", slot,
180 out->data[slot][0],
181 out->data[slot][1],
182 out->data[slot][2],
183 out->data[slot][3]);
184 }
185 #endif
186 } /* loop over vertices */
187 }
188 return clipped != 0;
189 }
190
191
192
193 /* Simplified vertex shader interface for the pt paths. Given the
194 * complexity of code-generating all the above operations together,
195 * it's time to try doing all the other stuff separately.
196 */
197 static void
198 vs_exec_run_linear( struct draw_vertex_shader *shader,
199 const float (*input)[4],
200 float (*output)[4],
201 const float (*constants)[4],
202 unsigned count,
203 unsigned input_stride,
204 unsigned output_stride )
205 {
206 struct exec_vertex_shader *evs = exec_vertex_shader(shader);
207 struct tgsi_exec_machine *machine = evs->machine;
208 unsigned int i, j;
209 unsigned slot;
210
211 machine->Consts = constants;
212
213 for (i = 0; i < count; i += MAX_TGSI_VERTICES) {
214 unsigned int max_vertices = MIN2(MAX_TGSI_VERTICES, count - i);
215
216 /* Swizzle inputs.
217 */
218 for (j = 0; j < max_vertices; j++) {
219 for (slot = 0; slot < shader->info.num_inputs; slot++) {
220 machine->Inputs[slot].xyzw[0].f[j] = input[slot][0];
221 machine->Inputs[slot].xyzw[1].f[j] = input[slot][1];
222 machine->Inputs[slot].xyzw[2].f[j] = input[slot][2];
223 machine->Inputs[slot].xyzw[3].f[j] = input[slot][3];
224 }
225 }
226
227 /* run interpreter */
228 tgsi_exec_machine_run( machine );
229
230 /* Unswizzle all output results.
231 */
232 for (j = 0; j < max_vertices; j++) {
233 for (slot = 0; slot < shader->info.num_outputs; slot++) {
234 output[slot][0] = machine->Outputs[slot].xyzw[0].f[j];
235 output[slot][1] = machine->Outputs[slot].xyzw[1].f[j];
236 output[slot][2] = machine->Outputs[slot].xyzw[2].f[j];
237 output[slot][3] = machine->Outputs[slot].xyzw[3].f[j];
238 }
239 }
240
241 /* Advance input, output pointers:
242 */
243 input = (const float (*)[4])((const char *)input + input_stride);
244 output = (float (*)[4])((char *)output + output_stride);
245 }
246 }
247
248
249
250
251 static void
252 vs_exec_delete( struct draw_vertex_shader *dvs )
253 {
254 FREE((void*) dvs->state.tokens);
255 FREE( dvs );
256 }
257
258
259 struct draw_vertex_shader *
260 draw_create_vs_exec(struct draw_context *draw,
261 const struct pipe_shader_state *state)
262 {
263 struct exec_vertex_shader *vs = CALLOC_STRUCT( exec_vertex_shader );
264 uint nt = tgsi_num_tokens(state->tokens);
265
266 if (vs == NULL)
267 return NULL;
268
269 /* we make a private copy of the tokens */
270 vs->base.state.tokens = mem_dup(state->tokens, nt * sizeof(state->tokens[0]));
271 tgsi_scan_shader(state->tokens, &vs->base.info);
272
273
274 vs->base.prepare = vs_exec_prepare;
275 vs->base.run = vs_exec_run;
276 vs->base.run_linear = vs_exec_run_linear;
277 vs->base.delete = vs_exec_delete;
278 vs->machine = &draw->machine;
279
280
281 return &vs->base;
282 }