Merge commit 'origin/master' into gallium-0.2
[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 "util/u_math.h"
35 #include "util/u_memory.h"
36 #include "pipe/p_shader_tokens.h"
37
38 #include "draw_private.h"
39 #include "draw_context.h"
40 #include "draw_vs.h"
41
42 #include "tgsi/tgsi_parse.h"
43 #include "tgsi/tgsi_scan.h"
44
45
46 struct exec_vertex_shader {
47 struct draw_vertex_shader base;
48 struct tgsi_exec_machine *machine;
49 };
50
51 static struct exec_vertex_shader *exec_vertex_shader( struct draw_vertex_shader *vs )
52 {
53 return (struct exec_vertex_shader *)vs;
54 }
55
56
57 /* Not required for run_linear.
58 */
59 static void
60 vs_exec_prepare( struct draw_vertex_shader *shader,
61 struct draw_context *draw )
62 {
63 struct exec_vertex_shader *evs = exec_vertex_shader(shader);
64
65 /* Specify the vertex program to interpret/execute.
66 * Avoid rebinding when possible.
67 */
68 if (evs->machine->Tokens != shader->state.tokens) {
69 tgsi_exec_machine_bind_shader(evs->machine,
70 shader->state.tokens,
71 draw->vs.num_samplers,
72 draw->vs.samplers);
73 }
74 }
75
76
77
78
79 /* Simplified vertex shader interface for the pt paths. Given the
80 * complexity of code-generating all the above operations together,
81 * it's time to try doing all the other stuff separately.
82 */
83 static void
84 vs_exec_run_linear( struct draw_vertex_shader *shader,
85 const float (*input)[4],
86 float (*output)[4],
87 const float (*constants)[4],
88 unsigned count,
89 unsigned input_stride,
90 unsigned output_stride )
91 {
92 struct exec_vertex_shader *evs = exec_vertex_shader(shader);
93 struct tgsi_exec_machine *machine = evs->machine;
94 unsigned int i, j;
95 unsigned slot;
96
97 machine->Consts = constants;
98
99 for (i = 0; i < count; i += MAX_TGSI_VERTICES) {
100 unsigned int max_vertices = MIN2(MAX_TGSI_VERTICES, count - i);
101
102 /* Swizzle inputs.
103 */
104 for (j = 0; j < max_vertices; j++) {
105 #if 0
106 debug_printf("%d) Input vert:\n", i + j);
107 for (slot = 0; slot < shader->info.num_inputs; slot++) {
108 debug_printf("\t%d: %f %f %f %f\n", slot,
109 input[slot][0],
110 input[slot][1],
111 input[slot][2],
112 input[slot][3]);
113 }
114 #endif
115
116 for (slot = 0; slot < shader->info.num_inputs; slot++) {
117 machine->Inputs[slot].xyzw[0].f[j] = input[slot][0];
118 machine->Inputs[slot].xyzw[1].f[j] = input[slot][1];
119 machine->Inputs[slot].xyzw[2].f[j] = input[slot][2];
120 machine->Inputs[slot].xyzw[3].f[j] = input[slot][3];
121 }
122
123 input = (const float (*)[4])((const char *)input + input_stride);
124 }
125
126 tgsi_set_exec_mask(machine,
127 1,
128 max_vertices > 1,
129 max_vertices > 2,
130 max_vertices > 3);
131
132 /* run interpreter */
133 tgsi_exec_machine_run( machine );
134
135 /* Unswizzle all output results.
136 */
137 for (j = 0; j < max_vertices; j++) {
138 for (slot = 0; slot < shader->info.num_outputs; slot++) {
139 output[slot][0] = machine->Outputs[slot].xyzw[0].f[j];
140 output[slot][1] = machine->Outputs[slot].xyzw[1].f[j];
141 output[slot][2] = machine->Outputs[slot].xyzw[2].f[j];
142 output[slot][3] = machine->Outputs[slot].xyzw[3].f[j];
143
144 }
145
146 #if 0
147 debug_printf("%d) Post xform vert:\n", i + j);
148 for (slot = 0; slot < shader->info.num_outputs; slot++) {
149 debug_printf("\t%d: %f %f %f %f\n", slot,
150 output[slot][0],
151 output[slot][1],
152 output[slot][2],
153 output[slot][3]);
154 }
155 #endif
156
157 output = (float (*)[4])((char *)output + output_stride);
158 }
159
160 }
161 }
162
163
164
165
166 static void
167 vs_exec_delete( struct draw_vertex_shader *dvs )
168 {
169 FREE((void*) dvs->state.tokens);
170 FREE( dvs );
171 }
172
173
174 struct draw_vertex_shader *
175 draw_create_vs_exec(struct draw_context *draw,
176 const struct pipe_shader_state *state)
177 {
178 struct exec_vertex_shader *vs = CALLOC_STRUCT( exec_vertex_shader );
179
180 if (vs == NULL)
181 return NULL;
182
183 /* we make a private copy of the tokens */
184 vs->base.state.tokens = tgsi_dup_tokens(state->tokens);
185 if (!vs->base.state.tokens) {
186 FREE(vs);
187 return NULL;
188 }
189
190 tgsi_scan_shader(state->tokens, &vs->base.info);
191
192 vs->base.draw = draw;
193 vs->base.prepare = vs_exec_prepare;
194 vs->base.run_linear = vs_exec_run_linear;
195 vs->base.delete = vs_exec_delete;
196 vs->base.create_varient = draw_vs_varient_generic;
197 vs->machine = &draw->vs.machine;
198
199 return &vs->base;
200 }