i965/gs: Add a case to brwNewProgram() for geometry shaders.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_clip_util.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a 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, sublicense, 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
16 portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **********************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 */
31
32
33 #include "main/glheader.h"
34 #include "main/macros.h"
35 #include "main/enums.h"
36 #include "program/program.h"
37
38 #include "intel_batchbuffer.h"
39
40 #include "brw_defines.h"
41 #include "brw_context.h"
42 #include "brw_eu.h"
43 #include "brw_clip.h"
44
45
46
47
48 struct brw_reg get_tmp( struct brw_clip_compile *c )
49 {
50 struct brw_reg tmp = brw_vec4_grf(c->last_tmp, 0);
51
52 if (++c->last_tmp > c->prog_data.total_grf)
53 c->prog_data.total_grf = c->last_tmp;
54
55 return tmp;
56 }
57
58 static void release_tmp( struct brw_clip_compile *c, struct brw_reg tmp )
59 {
60 if (tmp.nr == c->last_tmp-1)
61 c->last_tmp--;
62 }
63
64
65 static struct brw_reg make_plane_ud(GLuint x, GLuint y, GLuint z, GLuint w)
66 {
67 return brw_imm_ud((w<<24) | (z<<16) | (y<<8) | x);
68 }
69
70
71 void brw_clip_init_planes( struct brw_clip_compile *c )
72 {
73 struct brw_compile *p = &c->func;
74
75 if (!c->key.nr_userclip) {
76 brw_MOV(p, get_element_ud(c->reg.fixed_planes, 0), make_plane_ud( 0, 0, 0xff, 1));
77 brw_MOV(p, get_element_ud(c->reg.fixed_planes, 1), make_plane_ud( 0, 0, 1, 1));
78 brw_MOV(p, get_element_ud(c->reg.fixed_planes, 2), make_plane_ud( 0, 0xff, 0, 1));
79 brw_MOV(p, get_element_ud(c->reg.fixed_planes, 3), make_plane_ud( 0, 1, 0, 1));
80 brw_MOV(p, get_element_ud(c->reg.fixed_planes, 4), make_plane_ud(0xff, 0, 0, 1));
81 brw_MOV(p, get_element_ud(c->reg.fixed_planes, 5), make_plane_ud( 1, 0, 0, 1));
82 }
83 }
84
85
86
87 #define W 3
88
89 /* Project 'pos' to screen space (or back again), overwrite with results:
90 */
91 void brw_clip_project_position(struct brw_clip_compile *c, struct brw_reg pos )
92 {
93 struct brw_compile *p = &c->func;
94
95 /* calc rhw
96 */
97 brw_math_invert(p, get_element(pos, W), get_element(pos, W));
98
99 /* value.xyz *= value.rhw
100 */
101 brw_set_access_mode(p, BRW_ALIGN_16);
102 brw_MUL(p, brw_writemask(pos, WRITEMASK_XYZ), pos, brw_swizzle1(pos, W));
103 brw_set_access_mode(p, BRW_ALIGN_1);
104 }
105
106
107 static void brw_clip_project_vertex( struct brw_clip_compile *c,
108 struct brw_indirect vert_addr )
109 {
110 struct brw_compile *p = &c->func;
111 struct brw_reg tmp = get_tmp(c);
112 GLuint hpos_offset = brw_varying_to_offset(&c->vue_map, VARYING_SLOT_POS);
113 GLuint ndc_offset = brw_varying_to_offset(&c->vue_map,
114 BRW_VARYING_SLOT_NDC);
115
116 /* Fixup position. Extract from the original vertex and re-project
117 * to screen space:
118 */
119 brw_MOV(p, tmp, deref_4f(vert_addr, hpos_offset));
120 brw_clip_project_position(c, tmp);
121 brw_MOV(p, deref_4f(vert_addr, ndc_offset), tmp);
122
123 release_tmp(c, tmp);
124 }
125
126
127
128
129 /* Interpolate between two vertices and put the result into a0.0.
130 * Increment a0.0 accordingly.
131 *
132 * Beware that dest_ptr can be equal to v0_ptr!
133 */
134 void brw_clip_interp_vertex( struct brw_clip_compile *c,
135 struct brw_indirect dest_ptr,
136 struct brw_indirect v0_ptr, /* from */
137 struct brw_indirect v1_ptr, /* to */
138 struct brw_reg t0,
139 bool force_edgeflag)
140 {
141 struct brw_compile *p = &c->func;
142 struct brw_reg t_nopersp, v0_ndc_copy;
143 GLuint slot;
144
145 /* Just copy the vertex header:
146 */
147 /*
148 * After CLIP stage, only first 256 bits of the VUE are read
149 * back on Ironlake, so needn't change it
150 */
151 brw_copy_indirect_to_indirect(p, dest_ptr, v0_ptr, 1);
152
153
154 /* First handle the 3D and NDC interpolation, in case we
155 * need noperspective interpolation. Doing it early has no
156 * performance impact in any case.
157 */
158
159 /* Take a copy of the v0 NDC coordinates, in case dest == v0. */
160 if (c->has_noperspective_shading) {
161 GLuint offset = brw_varying_to_offset(&c->vue_map,
162 BRW_VARYING_SLOT_NDC);
163 v0_ndc_copy = get_tmp(c);
164 brw_MOV(p, v0_ndc_copy, deref_4f(v0_ptr, offset));
165 }
166
167 /* Compute the new 3D position
168 *
169 * dest_hpos = v0_hpos * (1 - t0) + v1_hpos * t0
170 */
171 {
172 GLuint delta = brw_varying_to_offset(&c->vue_map, VARYING_SLOT_POS);
173 struct brw_reg tmp = get_tmp(c);
174 brw_MUL(p, vec4(brw_null_reg()), deref_4f(v1_ptr, delta), t0);
175 brw_MAC(p, tmp, negate(deref_4f(v0_ptr, delta)), t0);
176 brw_ADD(p, deref_4f(dest_ptr, delta), deref_4f(v0_ptr, delta), tmp);
177 release_tmp(c, tmp);
178 }
179
180 /* Recreate the projected (NDC) coordinate in the new vertex header */
181 brw_clip_project_vertex(c, dest_ptr);
182
183 /* If we have noperspective attributes,
184 * we need to compute the screen-space t
185 */
186 if (c->has_noperspective_shading) {
187 GLuint delta = brw_varying_to_offset(&c->vue_map,
188 BRW_VARYING_SLOT_NDC);
189 struct brw_reg tmp = get_tmp(c);
190 t_nopersp = get_tmp(c);
191
192 /* t_nopersp = vec4(v1.xy, dest.xy) */
193 brw_MOV(p, t_nopersp, deref_4f(v1_ptr, delta));
194 brw_MOV(p, tmp, deref_4f(dest_ptr, delta));
195 brw_set_access_mode(p, BRW_ALIGN_16);
196 brw_MOV(p,
197 brw_writemask(t_nopersp, WRITEMASK_ZW),
198 brw_swizzle(tmp, 0, 1, 0, 1));
199
200 /* t_nopersp = vec4(v1.xy, dest.xy) - v0.xyxy */
201 brw_ADD(p, t_nopersp, t_nopersp,
202 negate(brw_swizzle(v0_ndc_copy, 0, 1, 0, 1)));
203
204 /* Add the absolute values of the X and Y deltas so that if
205 * the points aren't in the same place on the screen we get
206 * nonzero values to divide.
207 *
208 * After that, we have vert1 - vert0 in t_nopersp.x and
209 * vertnew - vert0 in t_nopersp.y
210 *
211 * t_nopersp = vec2(|v1.x -v0.x| + |v1.y -v0.y|,
212 * |dest.x-v0.x| + |dest.y-v0.y|)
213 */
214 brw_ADD(p,
215 brw_writemask(t_nopersp, WRITEMASK_XY),
216 brw_abs(brw_swizzle(t_nopersp, 0, 2, 0, 0)),
217 brw_abs(brw_swizzle(t_nopersp, 1, 3, 0, 0)));
218 brw_set_access_mode(p, BRW_ALIGN_1);
219
220 /* If the points are in the same place, just substitute a
221 * value to avoid divide-by-zero
222 */
223 brw_CMP(p, vec1(brw_null_reg()), BRW_CONDITIONAL_EQ,
224 vec1(t_nopersp),
225 brw_imm_f(0));
226 brw_IF(p, BRW_EXECUTE_1);
227 brw_MOV(p, t_nopersp, brw_imm_vf4(VF_ONE, VF_ZERO, VF_ZERO, VF_ZERO));
228 brw_ENDIF(p);
229
230 /* Now compute t_nopersp = t_nopersp.y/t_nopersp.x and broadcast it. */
231 brw_math_invert(p, get_element(t_nopersp, 0), get_element(t_nopersp, 0));
232 brw_MUL(p, vec1(t_nopersp), vec1(t_nopersp),
233 vec1(suboffset(t_nopersp, 1)));
234 brw_set_access_mode(p, BRW_ALIGN_16);
235 brw_MOV(p, t_nopersp, brw_swizzle(t_nopersp, 0, 0, 0, 0));
236 brw_set_access_mode(p, BRW_ALIGN_1);
237
238 release_tmp(c, tmp);
239 release_tmp(c, v0_ndc_copy);
240 }
241
242 /* Now we can iterate over each attribute
243 * (could be done in pairs?)
244 */
245 for (slot = 0; slot < c->vue_map.num_slots; slot++) {
246 int varying = c->vue_map.slot_to_varying[slot];
247 GLuint delta = brw_vue_slot_to_offset(slot);
248
249 /* HPOS, NDC already handled above */
250 if (varying == VARYING_SLOT_POS || varying == BRW_VARYING_SLOT_NDC)
251 continue;
252
253
254 if (varying == VARYING_SLOT_EDGE) {
255 if (force_edgeflag)
256 brw_MOV(p, deref_4f(dest_ptr, delta), brw_imm_f(1));
257 else
258 brw_MOV(p, deref_4f(dest_ptr, delta), deref_4f(v0_ptr, delta));
259 } else if (varying == VARYING_SLOT_PSIZ) {
260 /* PSIZ doesn't need interpolation because it isn't used by the
261 * fragment shader.
262 */
263 } else if (varying < VARYING_SLOT_MAX) {
264 /* This is a true vertex result (and not a special value for the VUE
265 * header), so interpolate:
266 *
267 * New = attr0 + t*attr1 - t*attr0
268 *
269 * Unless the attribute is flat shaded -- in which case just copy
270 * from one of the sources (doesn't matter which; already copied from pv)
271 */
272 GLuint interp = c->key.interpolation_mode.mode[slot];
273
274 if (interp != INTERP_QUALIFIER_FLAT) {
275 struct brw_reg tmp = get_tmp(c);
276 struct brw_reg t =
277 interp == INTERP_QUALIFIER_NOPERSPECTIVE ? t_nopersp : t0;
278
279 brw_MUL(p,
280 vec4(brw_null_reg()),
281 deref_4f(v1_ptr, delta),
282 t);
283
284 brw_MAC(p,
285 tmp,
286 negate(deref_4f(v0_ptr, delta)),
287 t);
288
289 brw_ADD(p,
290 deref_4f(dest_ptr, delta),
291 deref_4f(v0_ptr, delta),
292 tmp);
293
294 release_tmp(c, tmp);
295 }
296 else {
297 brw_MOV(p,
298 deref_4f(dest_ptr, delta),
299 deref_4f(v0_ptr, delta));
300 }
301 }
302 }
303
304 if (c->vue_map.num_slots % 2) {
305 GLuint delta = brw_vue_slot_to_offset(c->vue_map.num_slots);
306
307 brw_MOV(p, deref_4f(dest_ptr, delta), brw_imm_f(0));
308 }
309
310 if (c->has_noperspective_shading)
311 release_tmp(c, t_nopersp);
312 }
313
314 void brw_clip_emit_vue(struct brw_clip_compile *c,
315 struct brw_indirect vert,
316 bool allocate,
317 bool eot,
318 GLuint header)
319 {
320 struct brw_compile *p = &c->func;
321
322 brw_clip_ff_sync(c);
323
324 assert(!(allocate && eot));
325
326 /* Copy the vertex from vertn into m1..mN+1:
327 */
328 brw_copy_from_indirect(p, brw_message_reg(1), vert, c->nr_regs);
329
330 /* Overwrite PrimType and PrimStart in the message header, for
331 * each vertex in turn:
332 */
333 brw_MOV(p, get_element_ud(c->reg.R0, 2), brw_imm_ud(header));
334
335
336 /* Send each vertex as a seperate write to the urb. This
337 * is different to the concept in brw_sf_emit.c, where
338 * subsequent writes are used to build up a single urb
339 * entry. Each of these writes instantiates a seperate
340 * urb entry - (I think... what about 'allocate'?)
341 */
342 brw_urb_WRITE(p,
343 allocate ? c->reg.R0 : retype(brw_null_reg(), BRW_REGISTER_TYPE_UD),
344 0,
345 c->reg.R0,
346 allocate,
347 1, /* used */
348 c->nr_regs + 1, /* msg length */
349 allocate ? 1 : 0, /* response_length */
350 eot, /* eot */
351 1, /* writes_complete */
352 0, /* urb offset */
353 BRW_URB_SWIZZLE_NONE);
354 }
355
356
357
358 void brw_clip_kill_thread(struct brw_clip_compile *c)
359 {
360 struct brw_compile *p = &c->func;
361
362 brw_clip_ff_sync(c);
363 /* Send an empty message to kill the thread and release any
364 * allocated urb entry:
365 */
366 brw_urb_WRITE(p,
367 retype(brw_null_reg(), BRW_REGISTER_TYPE_UD),
368 0,
369 c->reg.R0,
370 0, /* allocate */
371 0, /* used */
372 1, /* msg len */
373 0, /* response len */
374 1, /* eot */
375 1, /* writes complete */
376 0,
377 BRW_URB_SWIZZLE_NONE);
378 }
379
380
381
382
383 struct brw_reg brw_clip_plane0_address( struct brw_clip_compile *c )
384 {
385 return brw_address(c->reg.fixed_planes);
386 }
387
388
389 struct brw_reg brw_clip_plane_stride( struct brw_clip_compile *c )
390 {
391 if (c->key.nr_userclip) {
392 return brw_imm_uw(16);
393 }
394 else {
395 return brw_imm_uw(4);
396 }
397 }
398
399
400 /* Distribute flatshaded attributes from provoking vertex prior to
401 * clipping.
402 */
403 void brw_clip_copy_flatshaded_attributes( struct brw_clip_compile *c,
404 GLuint to, GLuint from )
405 {
406 struct brw_compile *p = &c->func;
407
408 for (int i = 0; i < c->vue_map.num_slots; i++) {
409 if (c->key.interpolation_mode.mode[i] == INTERP_QUALIFIER_FLAT) {
410 brw_MOV(p,
411 byte_offset(c->reg.vertex[to], brw_vue_slot_to_offset(i)),
412 byte_offset(c->reg.vertex[from], brw_vue_slot_to_offset(i)));
413 }
414 }
415 }
416
417
418
419 void brw_clip_init_clipmask( struct brw_clip_compile *c )
420 {
421 struct brw_compile *p = &c->func;
422 struct brw_reg incoming = get_element_ud(c->reg.R0, 2);
423 struct brw_context *brw = p->brw;
424
425 /* Shift so that lowest outcode bit is rightmost:
426 */
427 brw_SHR(p, c->reg.planemask, incoming, brw_imm_ud(26));
428
429 if (c->key.nr_userclip) {
430 struct brw_reg tmp = retype(vec1(get_tmp(c)), BRW_REGISTER_TYPE_UD);
431
432 /* Rearrange userclip outcodes so that they come directly after
433 * the fixed plane bits.
434 */
435 if (brw->gen == 5 || brw->is_g4x)
436 brw_AND(p, tmp, incoming, brw_imm_ud(0xff<<14));
437 else
438 brw_AND(p, tmp, incoming, brw_imm_ud(0x3f<<14));
439
440 brw_SHR(p, tmp, tmp, brw_imm_ud(8));
441 brw_OR(p, c->reg.planemask, c->reg.planemask, tmp);
442
443 release_tmp(c, tmp);
444 }
445 }
446
447 void brw_clip_ff_sync(struct brw_clip_compile *c)
448 {
449 struct brw_compile *p = &c->func;
450 struct brw_context *brw = p->brw;
451
452 if (brw->gen == 5) {
453 brw_set_conditionalmod(p, BRW_CONDITIONAL_Z);
454 brw_AND(p, brw_null_reg(), c->reg.ff_sync, brw_imm_ud(0x1));
455 brw_IF(p, BRW_EXECUTE_1);
456 {
457 brw_OR(p, c->reg.ff_sync, c->reg.ff_sync, brw_imm_ud(0x1));
458 brw_ff_sync(p,
459 c->reg.R0,
460 0,
461 c->reg.R0,
462 1, /* allocate */
463 1, /* response length */
464 0 /* eot */);
465 }
466 brw_ENDIF(p);
467 brw_set_predicate_control(p, BRW_PREDICATE_NONE);
468 }
469 }
470
471 void brw_clip_init_ff_sync(struct brw_clip_compile *c)
472 {
473 struct brw_context *brw = c->func.brw;
474
475 if (brw->gen == 5) {
476 struct brw_compile *p = &c->func;
477
478 brw_MOV(p, c->reg.ff_sync, brw_imm_ud(0));
479 }
480 }