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