draw: associate rhw divide with clipping not viewport flag
[mesa.git] / src / gallium / auxiliary / draw / draw_vs_llvm.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 * Zack Rusin
31 * Keith Whitwell <keith@tungstengraphics.com>
32 * Brian Paul
33 */
34
35 #include "pipe/p_util.h"
36 #include "pipe/p_shader_tokens.h"
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 #ifdef MESA_LLVM
44
45 #include "gallivm/gallivm.h"
46
47 struct draw_llvm_vertex_shader {
48 struct draw_vertex_shader base;
49 struct gallivm_prog *llvm_prog;
50 };
51
52
53 static INLINE unsigned
54 compute_clipmask(const float *clip, /*const*/ float plane[][4], unsigned nr)
55 {
56 unsigned mask = 0;
57 unsigned i;
58
59 /* Do the hardwired planes first:
60 */
61 if (-clip[0] + clip[3] < 0) mask |= CLIP_RIGHT_BIT;
62 if ( clip[0] + clip[3] < 0) mask |= CLIP_LEFT_BIT;
63 if (-clip[1] + clip[3] < 0) mask |= CLIP_TOP_BIT;
64 if ( clip[1] + clip[3] < 0) mask |= CLIP_BOTTOM_BIT;
65 if (-clip[2] + clip[3] < 0) mask |= CLIP_FAR_BIT;
66 if ( clip[2] + clip[3] < 0) mask |= CLIP_NEAR_BIT;
67
68 /* Followed by any remaining ones:
69 */
70 for (i = 6; i < nr; i++) {
71 if (dot4(clip, plane[i]) < 0)
72 mask |= (1<<i);
73 }
74
75 return mask;
76 }
77
78
79
80 static void
81 vs_llvm_prepare( struct draw_vertex_shader *base,
82 struct draw_context *draw )
83 {
84 draw_update_vertex_fetch( draw );
85 }
86
87
88
89 /**
90 * Transform vertices with the current vertex program/shader
91 * Up to four vertices can be shaded at a time.
92 * \param vbuffer the input vertex data
93 * \param elts indexes of four input vertices
94 * \param count number of vertices to shade [1..4]
95 * \param vOut array of pointers to four output vertices
96 */
97 static void
98 vs_llvm_run( struct draw_vertex_shader *base,
99 struct draw_context *draw,
100 const unsigned *elts,
101 unsigned count,
102 struct vertex_header *vOut[] )
103 {
104 struct draw_llvm_vertex_shader *shader =
105 (struct draw_llvm_vertex_shader *)base;
106
107 struct tgsi_exec_machine *machine = &draw->machine;
108 unsigned int j;
109
110 ALIGN16_DECL(struct tgsi_exec_vector, inputs, PIPE_MAX_ATTRIBS);
111 ALIGN16_DECL(struct tgsi_exec_vector, outputs, PIPE_MAX_ATTRIBS);
112 const float *scale = draw->viewport.scale;
113 const float *trans = draw->viewport.translate;
114
115
116 assert(count <= 4);
117 assert(draw->vertex_shader->state->output_semantic_name[0]
118 == TGSI_SEMANTIC_POSITION);
119
120 /* Consts does not require 16 byte alignment. */
121 machine->Consts = (float (*)[4]) draw->user.constants;
122
123 machine->Inputs = ALIGN16_ASSIGN(inputs);
124 if (draw->rasterizer->bypass_vs) {
125 /* outputs are just the inputs */
126 machine->Outputs = machine->Inputs;
127 }
128 else {
129 machine->Outputs = ALIGN16_ASSIGN(outputs);
130 }
131
132
133 draw->vertex_fetch.fetch_func( draw, machine, elts, count );
134
135 if (!draw->rasterizer->bypass_vs) {
136 /* run shader */
137 gallivm_cpu_vs_exec(shader->llvm_prog,
138 machine->Inputs,
139 machine->Outputs,
140 machine->Consts,
141 machine->Temps);
142 }
143
144 /* store machine results */
145 for (j = 0; j < count; j++) {
146 unsigned slot;
147 float x, y, z, w;
148
149 x = vOut[j]->clip[0] = machine->Outputs[0].xyzw[0].f[j];
150 y = vOut[j]->clip[1] = machine->Outputs[0].xyzw[1].f[j];
151 z = vOut[j]->clip[2] = machine->Outputs[0].xyzw[2].f[j];
152 w = vOut[j]->clip[3] = machine->Outputs[0].xyzw[3].f[j];
153
154 if (!draw->rasterizer->bypass_clipping) {
155 vOut[j]->clipmask = compute_clipmask(vOut[j]->clip, draw->plane, draw->nr_planes);
156
157 /* divide by w */
158 w = 1.0f / w;
159 x *= w;
160 y *= w;
161 z *= w;
162 }
163 else {
164 vOut[j]->clipmask = 0;
165 }
166 vOut[j]->edgeflag = 1;
167
168 if (!draw->identity_viewport) {
169 /* Viewport mapping */
170 vOut[j]->data[0][0] = x * scale[0] + trans[0];
171 vOut[j]->data[0][1] = y * scale[1] + trans[1];
172 vOut[j]->data[0][2] = z * scale[2] + trans[2];
173 vOut[j]->data[0][3] = w;
174 }
175 else {
176 vOut[j]->data[0][0] = x;
177 vOut[j]->data[0][1] = y;
178 vOut[j]->data[0][2] = z;
179 vOut[j]->data[0][3] = w;
180 }
181
182 /* Remaining attributes are packed into sequential post-transform
183 * vertex attrib slots.
184 */
185 for (slot = 1; slot < draw->num_vs_outputs; slot++) {
186 vOut[j]->data[slot][0] = machine->Outputs[slot].xyzw[0].f[j];
187 vOut[j]->data[slot][1] = machine->Outputs[slot].xyzw[1].f[j];
188 vOut[j]->data[slot][2] = machine->Outputs[slot].xyzw[2].f[j];
189 vOut[j]->data[slot][3] = machine->Outputs[slot].xyzw[3].f[j];
190 }
191 } /* loop over vertices */
192 }
193
194 static void
195 vs_llvm_delete( struct draw_vertex_shader *base )
196 {
197 struct draw_llvm_vertex_shader *shader =
198 (struct draw_llvm_vertex_shader *)base;
199
200 /* Do something to free compiled shader:
201 */
202
203 FREE( (void*) shader->base.state.tokens );
204 FREE( shader );
205 }
206
207
208
209
210 struct draw_vertex_shader *
211 draw_create_vs_llvm(struct draw_context *draw,
212 const struct pipe_shader_state *templ)
213 {
214 struct draw_llvm_vertex_shader *vs;
215 uint nt = tgsi_num_tokens(templ->tokens);
216
217 vs = CALLOC_STRUCT( draw_llvm_vertex_shader );
218 if (vs == NULL)
219 return NULL;
220
221 /* we make a private copy of the tokens */
222 vs->base.state.tokens = mem_dup(templ->tokens, nt * sizeof(templ->tokens[0]));
223 vs->base.prepare = vs_llvm_prepare;
224 vs->base.run = vs_llvm_run;
225 vs->base.delete = vs_llvm_delete;
226
227 {
228 struct gallivm_ir *ir = gallivm_ir_new(GALLIVM_VS);
229 gallivm_ir_set_layout(ir, GALLIVM_SOA);
230 gallivm_ir_set_components(ir, 4);
231 gallivm_ir_fill_from_tgsi(ir, vs->base.state->tokens);
232 vs->llvm_prog = gallivm_ir_compile(ir);
233 gallivm_ir_delete(ir);
234 }
235
236 draw->engine = gallivm_global_cpu_engine();
237
238 /* XXX: Why are there two versions of this? Shouldn't creating the
239 * engine be a separate operation to compiling a shader?
240 */
241 if (!draw->engine) {
242 draw->engine = gallivm_cpu_engine_create(vs->llvm_prog);
243 }
244 else {
245 gallivm_cpu_jit_compile(draw->engine, vs->llvm_prog);
246 }
247
248 return &vs->base;
249 }
250
251
252
253
254
255 #else
256
257 struct draw_vertex_shader *
258 draw_create_vs_llvm(struct draw_context *draw,
259 const struct pipe_shader_state *shader)
260 {
261 return NULL;
262 }
263
264 #endif