gallium: remove dependencies on pipe_shader_state's semantic info
[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
42 static INLINE unsigned
43 compute_clipmask(const float *clip, /*const*/ float plane[][4], unsigned nr)
44 {
45 unsigned mask = 0;
46 unsigned i;
47
48 /* Do the hardwired planes first:
49 */
50 if (-clip[0] + clip[3] < 0) mask |= CLIP_RIGHT_BIT;
51 if ( clip[0] + clip[3] < 0) mask |= CLIP_LEFT_BIT;
52 if (-clip[1] + clip[3] < 0) mask |= CLIP_TOP_BIT;
53 if ( clip[1] + clip[3] < 0) mask |= CLIP_BOTTOM_BIT;
54 if (-clip[2] + clip[3] < 0) mask |= CLIP_FAR_BIT;
55 if ( clip[2] + clip[3] < 0) mask |= CLIP_NEAR_BIT;
56
57 /* Followed by any remaining ones:
58 */
59 for (i = 6; i < nr; i++) {
60 if (dot4(clip, plane[i]) < 0)
61 mask |= (1<<i);
62 }
63
64 return mask;
65 }
66
67
68 static void
69 vs_exec_prepare( struct draw_vertex_shader *shader,
70 struct draw_context *draw )
71 {
72 /* specify the vertex program to interpret/execute */
73 tgsi_exec_machine_bind_shader(&draw->machine,
74 shader->state->tokens,
75 PIPE_MAX_SAMPLERS,
76 NULL /*samplers*/ );
77
78 draw_update_vertex_fetch( draw );
79 }
80
81
82 /**
83 * Transform vertices with the current vertex program/shader
84 * Up to four vertices can be shaded at a time.
85 * \param vbuffer the input vertex data
86 * \param elts indexes of four input vertices
87 * \param count number of vertices to shade [1..4]
88 * \param vOut array of pointers to four output vertices
89 */
90 static void
91 vs_exec_run( struct draw_vertex_shader *shader,
92 struct draw_context *draw,
93 const unsigned *elts,
94 unsigned count,
95 struct vertex_header *vOut[] )
96 {
97 struct tgsi_exec_machine *machine = &draw->machine;
98 unsigned int j;
99
100 ALIGN16_DECL(struct tgsi_exec_vector, inputs, PIPE_ATTRIB_MAX);
101 ALIGN16_DECL(struct tgsi_exec_vector, outputs, PIPE_ATTRIB_MAX);
102 const float *scale = draw->viewport.scale;
103 const float *trans = draw->viewport.translate;
104
105 assert(count <= 4);
106 assert(draw->vertex_shader->info.output_semantic_name[0]
107 == TGSI_SEMANTIC_POSITION);
108
109 machine->Consts = (float (*)[4]) draw->user.constants;
110 machine->Inputs = ALIGN16_ASSIGN(inputs);
111 machine->Outputs = ALIGN16_ASSIGN(outputs);
112
113 draw->vertex_fetch.fetch_func( draw, machine, elts, count );
114
115 /* run interpreter */
116 tgsi_exec_machine_run( machine );
117
118
119 /* store machine results */
120 for (j = 0; j < count; j++) {
121 unsigned slot;
122 float x, y, z, w;
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 = vOut[j]->clip[0] = machine->Outputs[0].xyzw[0].f[j];
131 y = vOut[j]->clip[1] = machine->Outputs[0].xyzw[1].f[j];
132 z = vOut[j]->clip[2] = machine->Outputs[0].xyzw[2].f[j];
133 w = vOut[j]->clip[3] = machine->Outputs[0].xyzw[3].f[j];
134
135 vOut[j]->clipmask = compute_clipmask(vOut[j]->clip, draw->plane, draw->nr_planes);
136 vOut[j]->edgeflag = 1;
137
138 /* divide by w */
139 w = 1.0f / w;
140 x *= w;
141 y *= w;
142 z *= w;
143
144 /* Viewport mapping */
145 vOut[j]->data[0][0] = x * scale[0] + trans[0];
146 vOut[j]->data[0][1] = y * scale[1] + trans[1];
147 vOut[j]->data[0][2] = z * scale[2] + trans[2];
148 vOut[j]->data[0][3] = w;
149
150 /* Remaining attributes are packed into sequential post-transform
151 * vertex attrib slots.
152 */
153 for (slot = 1; slot < draw->num_vs_outputs; slot++) {
154 vOut[j]->data[slot][0] = machine->Outputs[slot].xyzw[0].f[j];
155 vOut[j]->data[slot][1] = machine->Outputs[slot].xyzw[1].f[j];
156 vOut[j]->data[slot][2] = machine->Outputs[slot].xyzw[2].f[j];
157 vOut[j]->data[slot][3] = machine->Outputs[slot].xyzw[3].f[j];
158 }
159 } /* loop over vertices */
160 }
161
162
163
164 static void
165 vs_exec_delete( struct draw_vertex_shader *dvs )
166 {
167 FREE( dvs );
168 }
169
170
171 struct draw_vertex_shader *
172 draw_create_vs_exec(struct draw_context *draw,
173 const struct pipe_shader_state *state)
174 {
175 struct draw_vertex_shader *vs = CALLOC_STRUCT( draw_vertex_shader );
176
177 if (vs == NULL)
178 return NULL;
179
180 vs->state = state;
181 vs->prepare = vs_exec_prepare;
182 vs->run = vs_exec_run;
183 vs->delete = vs_exec_delete;
184
185 return vs;
186 }