2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics to
4 develop this 3D driver.
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:
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.
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.
26 **********************************************************************/
29 * Keith Whitwell <keithw@vmware.com>
33 #include "main/glheader.h"
34 #include "main/macros.h"
35 #include "main/enums.h"
36 #include "program/program.h"
38 #include "intel_batchbuffer.h"
40 #include "brw_defines.h"
41 #include "brw_context.h"
48 struct brw_reg
get_tmp( struct brw_clip_compile
*c
)
50 struct brw_reg tmp
= brw_vec4_grf(c
->last_tmp
, 0);
52 if (++c
->last_tmp
> c
->prog_data
.total_grf
)
53 c
->prog_data
.total_grf
= c
->last_tmp
;
58 static void release_tmp( struct brw_clip_compile
*c
, struct brw_reg tmp
)
60 if (tmp
.nr
== c
->last_tmp
-1)
65 static struct brw_reg
make_plane_ud(GLuint x
, GLuint y
, GLuint z
, GLuint w
)
67 return brw_imm_ud((w
<<24) | (z
<<16) | (y
<<8) | x
);
71 void brw_clip_init_planes( struct brw_clip_compile
*c
)
73 struct brw_compile
*p
= &c
->func
;
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));
89 /* Project 'pos' to screen space (or back again), overwrite with results:
91 void brw_clip_project_position(struct brw_clip_compile
*c
, struct brw_reg pos
)
93 struct brw_compile
*p
= &c
->func
;
97 brw_math_invert(p
, get_element(pos
, W
), get_element(pos
, W
));
99 /* value.xyz *= value.rhw
101 brw_set_default_access_mode(p
, BRW_ALIGN_16
);
102 brw_MUL(p
, brw_writemask(pos
, WRITEMASK_XYZ
), pos
, brw_swizzle1(pos
, W
));
103 brw_set_default_access_mode(p
, BRW_ALIGN_1
);
107 static void brw_clip_project_vertex( struct brw_clip_compile
*c
,
108 struct brw_indirect vert_addr
)
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
);
116 /* Fixup position. Extract from the original vertex and re-project
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
);
129 /* Interpolate between two vertices and put the result into a0.0.
130 * Increment a0.0 accordingly.
132 * Beware that dest_ptr can be equal to v0_ptr!
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 */
141 struct brw_compile
*p
= &c
->func
;
142 struct brw_reg t_nopersp
, v0_ndc_copy
;
145 /* Just copy the vertex header:
148 * After CLIP stage, only first 256 bits of the VUE are read
149 * back on Ironlake, so needn't change it
151 brw_copy_indirect_to_indirect(p
, dest_ptr
, v0_ptr
, 1);
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.
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
));
167 /* Compute the new 3D position
169 * dest_hpos = v0_hpos * (1 - t0) + v1_hpos * t0
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
);
180 /* Recreate the projected (NDC) coordinate in the new vertex header */
181 brw_clip_project_vertex(c
, dest_ptr
);
183 /* If we have noperspective attributes,
184 * we need to compute the screen-space t
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
);
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_default_access_mode(p
, BRW_ALIGN_16
);
197 brw_writemask(t_nopersp
, WRITEMASK_ZW
),
198 brw_swizzle(tmp
, 0, 1, 0, 1));
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)));
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.
208 * After that, we have vert1 - vert0 in t_nopersp.x and
209 * vertnew - vert0 in t_nopersp.y
211 * t_nopersp = vec2(|v1.x -v0.x| + |v1.y -v0.y|,
212 * |dest.x-v0.x| + |dest.y-v0.y|)
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_default_access_mode(p
, BRW_ALIGN_1
);
220 /* If the points are in the same place, just substitute a
221 * value to avoid divide-by-zero
223 brw_CMP(p
, vec1(brw_null_reg()), BRW_CONDITIONAL_EQ
,
226 brw_IF(p
, BRW_EXECUTE_1
);
227 brw_MOV(p
, t_nopersp
, brw_imm_vf4(1, 0, 0, 0));
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_default_access_mode(p
, BRW_ALIGN_16
);
235 brw_MOV(p
, t_nopersp
, brw_swizzle(t_nopersp
, 0, 0, 0, 0));
236 brw_set_default_access_mode(p
, BRW_ALIGN_1
);
239 release_tmp(c
, v0_ndc_copy
);
242 /* Now we can iterate over each attribute
243 * (could be done in pairs?)
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
);
249 /* HPOS, NDC already handled above */
250 if (varying
== VARYING_SLOT_POS
|| varying
== BRW_VARYING_SLOT_NDC
)
254 if (varying
== VARYING_SLOT_EDGE
) {
256 brw_MOV(p
, deref_4f(dest_ptr
, delta
), brw_imm_f(1));
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
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:
267 * New = attr0 + t*attr1 - t*attr0
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)
272 GLuint interp
= c
->key
.interpolation_mode
.mode
[slot
];
274 if (interp
!= INTERP_QUALIFIER_FLAT
) {
275 struct brw_reg tmp
= get_tmp(c
);
277 interp
== INTERP_QUALIFIER_NOPERSPECTIVE
? t_nopersp
: t0
;
280 vec4(brw_null_reg()),
281 deref_4f(v1_ptr
, delta
),
286 negate(deref_4f(v0_ptr
, delta
)),
290 deref_4f(dest_ptr
, delta
),
291 deref_4f(v0_ptr
, delta
),
298 deref_4f(dest_ptr
, delta
),
299 deref_4f(v0_ptr
, delta
));
304 if (c
->vue_map
.num_slots
% 2) {
305 GLuint delta
= brw_vue_slot_to_offset(c
->vue_map
.num_slots
);
307 brw_MOV(p
, deref_4f(dest_ptr
, delta
), brw_imm_f(0));
310 if (c
->has_noperspective_shading
)
311 release_tmp(c
, t_nopersp
);
314 void brw_clip_emit_vue(struct brw_clip_compile
*c
,
315 struct brw_indirect vert
,
316 enum brw_urb_write_flags flags
,
319 struct brw_compile
*p
= &c
->func
;
320 bool allocate
= flags
& BRW_URB_WRITE_ALLOCATE
;
324 /* Any URB entry that is allocated must subsequently be used or discarded,
325 * so it doesn't make sense to mark EOT and ALLOCATE at the same time.
327 assert(!(allocate
&& (flags
& BRW_URB_WRITE_EOT
)));
329 /* Copy the vertex from vertn into m1..mN+1:
331 brw_copy_from_indirect(p
, brw_message_reg(1), vert
, c
->nr_regs
);
333 /* Overwrite PrimType and PrimStart in the message header, for
334 * each vertex in turn:
336 brw_MOV(p
, get_element_ud(c
->reg
.R0
, 2), brw_imm_ud(header
));
339 /* Send each vertex as a seperate write to the urb. This
340 * is different to the concept in brw_sf_emit.c, where
341 * subsequent writes are used to build up a single urb
342 * entry. Each of these writes instantiates a seperate
343 * urb entry - (I think... what about 'allocate'?)
346 allocate
? c
->reg
.R0
: retype(brw_null_reg(), BRW_REGISTER_TYPE_UD
),
350 c
->nr_regs
+ 1, /* msg length */
351 allocate
? 1 : 0, /* response_length */
353 BRW_URB_SWIZZLE_NONE
);
358 void brw_clip_kill_thread(struct brw_clip_compile
*c
)
360 struct brw_compile
*p
= &c
->func
;
363 /* Send an empty message to kill the thread and release any
364 * allocated urb entry:
367 retype(brw_null_reg(), BRW_REGISTER_TYPE_UD
),
370 BRW_URB_WRITE_UNUSED
| BRW_URB_WRITE_EOT_COMPLETE
,
372 0, /* response len */
374 BRW_URB_SWIZZLE_NONE
);
380 struct brw_reg
brw_clip_plane0_address( struct brw_clip_compile
*c
)
382 return brw_address(c
->reg
.fixed_planes
);
386 struct brw_reg
brw_clip_plane_stride( struct brw_clip_compile
*c
)
388 if (c
->key
.nr_userclip
) {
389 return brw_imm_uw(16);
392 return brw_imm_uw(4);
397 /* Distribute flatshaded attributes from provoking vertex prior to
400 void brw_clip_copy_flatshaded_attributes( struct brw_clip_compile
*c
,
401 GLuint to
, GLuint from
)
403 struct brw_compile
*p
= &c
->func
;
405 for (int i
= 0; i
< c
->vue_map
.num_slots
; i
++) {
406 if (c
->key
.interpolation_mode
.mode
[i
] == INTERP_QUALIFIER_FLAT
) {
408 byte_offset(c
->reg
.vertex
[to
], brw_vue_slot_to_offset(i
)),
409 byte_offset(c
->reg
.vertex
[from
], brw_vue_slot_to_offset(i
)));
416 void brw_clip_init_clipmask( struct brw_clip_compile
*c
)
418 struct brw_compile
*p
= &c
->func
;
419 struct brw_reg incoming
= get_element_ud(c
->reg
.R0
, 2);
420 struct brw_context
*brw
= p
->brw
;
422 /* Shift so that lowest outcode bit is rightmost:
424 brw_SHR(p
, c
->reg
.planemask
, incoming
, brw_imm_ud(26));
426 if (c
->key
.nr_userclip
) {
427 struct brw_reg tmp
= retype(vec1(get_tmp(c
)), BRW_REGISTER_TYPE_UD
);
429 /* Rearrange userclip outcodes so that they come directly after
430 * the fixed plane bits.
432 if (brw
->gen
== 5 || brw
->is_g4x
)
433 brw_AND(p
, tmp
, incoming
, brw_imm_ud(0xff<<14));
435 brw_AND(p
, tmp
, incoming
, brw_imm_ud(0x3f<<14));
437 brw_SHR(p
, tmp
, tmp
, brw_imm_ud(8));
438 brw_OR(p
, c
->reg
.planemask
, c
->reg
.planemask
, tmp
);
444 void brw_clip_ff_sync(struct brw_clip_compile
*c
)
446 struct brw_compile
*p
= &c
->func
;
447 struct brw_context
*brw
= p
->brw
;
450 brw_AND(p
, brw_null_reg(), c
->reg
.ff_sync
, brw_imm_ud(0x1));
451 brw_inst_set_cond_modifier(brw
, brw_last_inst
, BRW_CONDITIONAL_Z
);
452 brw_IF(p
, BRW_EXECUTE_1
);
454 brw_OR(p
, c
->reg
.ff_sync
, c
->reg
.ff_sync
, brw_imm_ud(0x1));
460 1, /* response length */
464 brw_set_default_predicate_control(p
, BRW_PREDICATE_NONE
);
468 void brw_clip_init_ff_sync(struct brw_clip_compile
*c
)
470 struct brw_context
*brw
= c
->func
.brw
;
473 struct brw_compile
*p
= &c
->func
;
475 brw_MOV(p
, c
->reg
.ff_sync
, brw_imm_ud(0));