Make shaders operate on a block of memory instead of arrays of vertex_header's
[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 #define MAX_TGSI_VERTICES 4
44
45 static void
46 vs_exec_prepare( struct draw_vertex_shader *shader,
47 struct draw_context *draw )
48 {
49 /* specify the vertex program to interpret/execute */
50 tgsi_exec_machine_bind_shader(&draw->machine,
51 shader->state.tokens,
52 PIPE_MAX_SAMPLERS,
53 NULL /*samplers*/ );
54
55 draw_update_vertex_fetch( draw );
56 }
57
58
59 /**
60 * Transform vertices with the current vertex program/shader
61 * Up to four vertices can be shaded at a time.
62 * \param vbuffer the input vertex data
63 * \param elts indexes of four input vertices
64 * \param count number of vertices to shade [1..4]
65 * \param vOut array of pointers to four output vertices
66 */
67 static boolean
68 vs_exec_run( struct draw_vertex_shader *shader,
69 struct draw_context *draw,
70 const unsigned *elts,
71 unsigned count,
72 void *vOut )
73 {
74 struct tgsi_exec_machine *machine = &draw->machine;
75 unsigned int i, j;
76 unsigned int clipped = 0;
77
78 ALIGN16_DECL(struct tgsi_exec_vector, inputs, PIPE_MAX_ATTRIBS);
79 ALIGN16_DECL(struct tgsi_exec_vector, outputs, PIPE_MAX_ATTRIBS);
80 const float *scale = draw->viewport.scale;
81 const float *trans = draw->viewport.translate;
82
83 assert(draw->vertex_shader->info.output_semantic_name[0]
84 == TGSI_SEMANTIC_POSITION);
85
86 machine->Consts = (float (*)[4]) draw->user.constants;
87 machine->Inputs = ALIGN16_ASSIGN(inputs);
88 if (draw->rasterizer->bypass_vs) {
89 /* outputs are just the inputs */
90 machine->Outputs = machine->Inputs;
91 }
92 else {
93 machine->Outputs = ALIGN16_ASSIGN(outputs);
94 }
95
96 for (i = 0; i < count; i += MAX_TGSI_VERTICES) {
97 unsigned int max_vertices = MIN2(MAX_TGSI_VERTICES, count - i);
98 draw->vertex_fetch.fetch_func( draw, machine, &elts[i], max_vertices );
99
100 if (!draw->rasterizer->bypass_vs) {
101 /* run interpreter */
102 tgsi_exec_machine_run( machine );
103 }
104
105 /* store machine results */
106 for (j = 0; j < max_vertices; j++) {
107 unsigned slot;
108 float x, y, z, w;
109 struct vertex_header *out =
110 draw_header_from_block(vOut, i + j);
111
112 /* Handle attr[0] (position) specially:
113 *
114 * XXX: Computing the clipmask should be done in the vertex
115 * program as a set of DP4 instructions appended to the
116 * user-provided code.
117 */
118 x = out->clip[0] = machine->Outputs[0].xyzw[0].f[j];
119 y = out->clip[1] = machine->Outputs[0].xyzw[1].f[j];
120 z = out->clip[2] = machine->Outputs[0].xyzw[2].f[j];
121 w = out->clip[3] = machine->Outputs[0].xyzw[3].f[j];
122
123 if (!draw->rasterizer->bypass_clipping) {
124 out->clipmask = compute_clipmask(out->clip, draw->plane,
125 draw->nr_planes);
126 clipped += out->clipmask;
127
128 /* divide by w */
129 w = 1.0f / w;
130 x *= w;
131 y *= w;
132 z *= w;
133 }
134 else {
135 out->clipmask = 0;
136 }
137 out->edgeflag = 1;
138
139 if (!draw->identity_viewport) {
140 /* Viewport mapping */
141 out->data[0][0] = x * scale[0] + trans[0];
142 out->data[0][1] = y * scale[1] + trans[1];
143 out->data[0][2] = z * scale[2] + trans[2];
144 out->data[0][3] = w;
145 }
146 else {
147 out->data[0][0] = x;
148 out->data[0][1] = y;
149 out->data[0][2] = z;
150 out->data[0][3] = w;
151 }
152
153 /* Remaining attributes are packed into sequential post-transform
154 * vertex attrib slots.
155 */
156 for (slot = 1; slot < draw->num_vs_outputs; slot++) {
157 out->data[slot][0] = machine->Outputs[slot].xyzw[0].f[j];
158 out->data[slot][1] = machine->Outputs[slot].xyzw[1].f[j];
159 out->data[slot][2] = machine->Outputs[slot].xyzw[2].f[j];
160 out->data[slot][3] = machine->Outputs[slot].xyzw[3].f[j];
161 }
162
163 #if 0 /*DEBUG*/
164 printf("%d) Post xform vert:\n", i + j);
165 for (slot = 0; slot < draw->num_vs_outputs; slot++) {
166 printf("\t%d: %f %f %f %f\n", slot,
167 out->data[slot][0],
168 out->data[slot][1],
169 out->data[slot][2],
170 out->data[slot][3]);
171 }
172 #endif
173 } /* loop over vertices */
174 }
175 return clipped != 0;
176 }
177
178
179
180 static void
181 vs_exec_delete( struct draw_vertex_shader *dvs )
182 {
183 FREE((void*) dvs->state.tokens);
184 FREE( dvs );
185 }
186
187
188 struct draw_vertex_shader *
189 draw_create_vs_exec(struct draw_context *draw,
190 const struct pipe_shader_state *state)
191 {
192 struct draw_vertex_shader *vs = CALLOC_STRUCT( draw_vertex_shader );
193 uint nt = tgsi_num_tokens(state->tokens);
194
195 if (vs == NULL)
196 return NULL;
197
198 /* we make a private copy of the tokens */
199 vs->state.tokens = mem_dup(state->tokens, nt * sizeof(state->tokens[0]));
200 vs->prepare = vs_exec_prepare;
201 vs->run = vs_exec_run;
202 vs->delete = vs_exec_delete;
203
204 return vs;
205 }