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