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