fe4f124dd2f0f3e59b914bb015a8faad05f1475d
[mesa.git] / src / mesa / pipe / draw / draw_vertex_shader.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 "draw_private.h"
36 #include "draw_context.h"
37 #include "draw_vertex.h"
38
39 #include "pipe/tgsi/exec/tgsi_core.h"
40
41 static INLINE unsigned
42 compute_clipmask(float cx, float cy, float cz, float cw)
43 {
44 unsigned mask = 0;
45
46 if (-cx + cw < 0) mask |= CLIP_RIGHT_BIT;
47 if ( cx + cw < 0) mask |= CLIP_LEFT_BIT;
48 if (-cy + cw < 0) mask |= CLIP_TOP_BIT;
49 if ( cy + cw < 0) mask |= CLIP_BOTTOM_BIT;
50 if (-cz + cw < 0) mask |= CLIP_FAR_BIT;
51 if ( cz + cw < 0) mask |= CLIP_NEAR_BIT;
52
53 return mask;
54 }
55
56
57
58
59 #if !defined(XSTDCALL)
60 #if defined(WIN32)
61 #define XSTDCALL __stdcall
62 #else
63 #define XSTDCALL
64 #endif
65 #endif
66
67 #if defined(USE_X86_ASM) || defined(SLANG_X86)
68 typedef void (XSTDCALL *sse2_function)(
69 const struct tgsi_exec_vector *input,
70 struct tgsi_exec_vector *output,
71 float (*constant)[4],
72 struct tgsi_exec_vector *temporary );
73 #endif
74
75 /**
76 * Transform vertices with the current vertex program/shader
77 * Up to four vertices can be shaded at a time.
78 * \param vbuffer the input vertex data
79 * \param elts indexes of four input vertices
80 * \param count number of vertices to shade [1..4]
81 * \param vOut array of pointers to four output vertices
82 */
83 static void
84 run_vertex_program(struct draw_context *draw,
85 unsigned elts[4], unsigned count,
86 struct vertex_header *vOut[])
87 {
88 struct tgsi_exec_machine machine;
89 unsigned int j;
90
91 ALIGN16_DECL(struct tgsi_exec_vector, inputs, PIPE_ATTRIB_MAX);
92 ALIGN16_DECL(struct tgsi_exec_vector, outputs, PIPE_ATTRIB_MAX);
93 const float *scale = draw->viewport.scale;
94 const float *trans = draw->viewport.translate;
95
96 assert(count <= 4);
97 assert(draw->vertex_shader.output_semantic_name[0]
98 == TGSI_SEMANTIC_POSITION);
99
100 #ifdef DEBUG
101 memset( &machine, 0, sizeof( machine ) );
102 #endif
103
104 /* init machine state */
105 tgsi_exec_machine_init(&machine,
106 draw->vertex_shader.tokens,
107 PIPE_MAX_SAMPLERS,
108 NULL /*samplers*/ );
109
110 /* Consts does not require 16 byte alignment. */
111 machine.Consts = (float (*)[4]) draw->mapped_constants;
112
113 machine.Inputs = ALIGN16_ASSIGN(inputs);
114 machine.Outputs = ALIGN16_ASSIGN(outputs);
115
116 draw_vertex_fetch( draw, &machine, elts, count );
117
118 /* run shader */
119 if( draw->vertex_shader.executable != NULL ) {
120 #if defined(USE_X86_ASM) || defined(SLANG_X86)
121 sse2_function func = (sse2_function) draw->vertex_shader.executable;
122 func(
123 machine.Inputs,
124 machine.Outputs,
125 machine.Consts,
126 machine.Temps );
127 #else
128 assert( 0 );
129 #endif
130 }
131 else {
132 tgsi_exec_machine_run( &machine );
133 }
134
135
136 /* store machine results */
137 for (j = 0; j < count; j++) {
138 unsigned slot;
139 float x, y, z, w;
140
141 /* Handle attr[0] (position) specially: */
142 x = vOut[j]->clip[0] = machine.Outputs[0].xyzw[0].f[j];
143 y = vOut[j]->clip[1] = machine.Outputs[0].xyzw[1].f[j];
144 z = vOut[j]->clip[2] = machine.Outputs[0].xyzw[2].f[j];
145 w = vOut[j]->clip[3] = machine.Outputs[0].xyzw[3].f[j];
146
147 vOut[j]->clipmask = compute_clipmask(x, y, z, w) | draw->user_clipmask;
148 vOut[j]->edgeflag = 1;
149
150 /* divide by w */
151 w = 1.0f / w;
152 x *= w;
153 y *= w;
154 z *= w;
155
156 /* Viewport mapping */
157 vOut[j]->data[0][0] = x * scale[0] + trans[0];
158 vOut[j]->data[0][1] = y * scale[1] + trans[1];
159 vOut[j]->data[0][2] = z * scale[2] + trans[2];
160 vOut[j]->data[0][3] = w;
161
162 /* Remaining attributes are packed into sequential post-transform
163 * vertex attrib slots.
164 * Skip 0 since we just did it above.
165 * Subtract two because of the VERTEX_HEADER, CLIP_POS attribs.
166 */
167 for (slot = 1; slot < draw->vertex_info.num_attribs - 2; slot++) {
168 vOut[j]->data[slot][0] = machine.Outputs[slot].xyzw[0].f[j];
169 vOut[j]->data[slot][1] = machine.Outputs[slot].xyzw[1].f[j];
170 vOut[j]->data[slot][2] = machine.Outputs[slot].xyzw[2].f[j];
171 vOut[j]->data[slot][3] = machine.Outputs[slot].xyzw[3].f[j];
172 /*
173 printf("output %d: %f %f %f %f\n", slot,
174 vOut[j]->data[slot][0],
175 vOut[j]->data[slot][1],
176 vOut[j]->data[slot][2],
177 vOut[j]->data[slot][3]);
178 */
179 }
180 } /* loop over vertices */
181 }
182
183
184 /**
185 * Called by the draw module when the vertx cache needs to be flushed.
186 * This involves running the vertex shader.
187 */
188 void draw_vertex_shader_queue_flush( struct draw_context *draw )
189 {
190 unsigned i, j;
191
192 // fprintf(stderr, " q(%d) ", draw->vs.queue_nr );
193
194 /* run vertex shader on vertex cache entries, four per invokation */
195 for (i = 0; i < draw->vs.queue_nr; i += 4) {
196 struct vertex_header *dests[4];
197 unsigned elts[4];
198 int n;
199
200 for (j = 0; j < 4; j++) {
201 elts[j] = draw->vs.queue[i + j].elt;
202 dests[j] = draw->vs.queue[i + j].dest;
203 }
204
205 n = MIN2(4, draw->vs.queue_nr - i);
206 assert(n > 0);
207 assert(n <= 4);
208
209 run_vertex_program(draw, elts, n, dests);
210 }
211
212 draw->vs.queue_nr = 0;
213 }
214