2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) 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 <keith@tungstengraphics.com>
36 #include "brw_structs.h"
37 #include "brw_defines.h"
38 #include "shader/program.h"
40 #define BRW_SWIZZLE4(a,b,c,d) (((a)<<0) | ((b)<<2) | ((c)<<4) | ((d)<<6))
41 #define BRW_GET_SWZ(swz, idx) (((swz) >> ((idx)*2)) & 0x3)
43 #define BRW_SWIZZLE_NOOP BRW_SWIZZLE4(0,1,2,3)
44 #define BRW_SWIZZLE_XYZW BRW_SWIZZLE4(0,1,2,3)
45 #define BRW_SWIZZLE_XXXX BRW_SWIZZLE4(0,0,0,0)
46 #define BRW_SWIZZLE_XYXY BRW_SWIZZLE4(0,1,0,1)
49 #define REG_SIZE (8*4)
52 /* These aren't hardware structs, just something useful for us to pass around:
54 * Align1 operation has a lot of control over input ranges. Used in
55 * WM programs to implement shaders decomposed into "channel serial"
56 * or "structure of array" form:
63 GLuint subnr
:5; /* :1 in align16 */
64 GLuint negate
:1; /* source only */
65 GLuint abs
:1; /* source only */
66 GLuint vstride
:4; /* source only */
67 GLuint width
:3; /* src only, align1 only */
68 GLuint hstride
:2; /* src only, align1 only */
69 GLuint address_mode
:1; /* relative addressing, hopefully! */
74 GLuint swizzle
:8; /* src only, align16 only */
75 GLuint writemask
:4; /* dest only, align16 only */
76 GLint indirect_offset
:10; /* relative addressing offset */
77 GLuint pad1
:10; /* two dwords total */
94 #define BRW_EU_MAX_INSN_STACK 5
95 #define BRW_EU_MAX_INSN 1200
98 struct brw_instruction store
[BRW_EU_MAX_INSN
];
101 /* Allow clients to push/pop instruction state:
103 struct brw_instruction stack
[BRW_EU_MAX_INSN_STACK
];
104 struct brw_instruction
*current
;
111 static __inline
int type_sz( GLuint type
)
114 case BRW_REGISTER_TYPE_UD
:
115 case BRW_REGISTER_TYPE_D
:
116 case BRW_REGISTER_TYPE_F
:
118 case BRW_REGISTER_TYPE_HF
:
119 case BRW_REGISTER_TYPE_UW
:
120 case BRW_REGISTER_TYPE_W
:
122 case BRW_REGISTER_TYPE_UB
:
123 case BRW_REGISTER_TYPE_B
:
130 static __inline
struct brw_reg
brw_reg( GLuint file
,
145 reg
.subnr
= subnr
* type_sz(type
);
148 reg
.vstride
= vstride
;
150 reg
.hstride
= hstride
;
151 reg
.address_mode
= BRW_ADDRESS_DIRECT
;
154 /* Could do better: If the reg is r5.3<0;1,0>, we probably want to
155 * set swizzle and writemask to W, as the lower bits of subnr will
156 * be lost when converted to align16. This is probably too much to
157 * keep track of as you'd want it adjusted by suboffset(), etc.
158 * Perhaps fix up when converting to align16?
160 reg
.dw1
.bits
.swizzle
= swizzle
;
161 reg
.dw1
.bits
.writemask
= writemask
;
162 reg
.dw1
.bits
.indirect_offset
= 0;
163 reg
.dw1
.bits
.pad1
= 0;
167 static __inline
struct brw_reg
brw_vec16_reg( GLuint file
,
175 BRW_VERTICAL_STRIDE_16
,
177 BRW_HORIZONTAL_STRIDE_1
,
182 static __inline
struct brw_reg
brw_vec8_reg( GLuint file
,
190 BRW_VERTICAL_STRIDE_8
,
192 BRW_HORIZONTAL_STRIDE_1
,
198 static __inline
struct brw_reg
brw_vec4_reg( GLuint file
,
206 BRW_VERTICAL_STRIDE_4
,
208 BRW_HORIZONTAL_STRIDE_1
,
214 static __inline
struct brw_reg
brw_vec2_reg( GLuint file
,
222 BRW_VERTICAL_STRIDE_2
,
224 BRW_HORIZONTAL_STRIDE_1
,
229 static __inline
struct brw_reg
brw_vec1_reg( GLuint file
,
237 BRW_VERTICAL_STRIDE_0
,
239 BRW_HORIZONTAL_STRIDE_0
,
245 static __inline
struct brw_reg
retype( struct brw_reg reg
,
252 static __inline
struct brw_reg
suboffset( struct brw_reg reg
,
255 reg
.subnr
+= delta
* type_sz(reg
.type
);
260 static __inline
struct brw_reg
offset( struct brw_reg reg
,
268 static __inline
struct brw_reg
byte_offset( struct brw_reg reg
,
271 GLuint newoffset
= reg
.nr
* REG_SIZE
+ reg
.subnr
+ bytes
;
272 reg
.nr
= newoffset
/ REG_SIZE
;
273 reg
.subnr
= newoffset
% REG_SIZE
;
278 static __inline
struct brw_reg
brw_uw16_reg( GLuint file
,
282 return suboffset(retype(brw_vec16_reg(file
, nr
, 0), BRW_REGISTER_TYPE_UW
), subnr
);
285 static __inline
struct brw_reg
brw_uw8_reg( GLuint file
,
289 return suboffset(retype(brw_vec8_reg(file
, nr
, 0), BRW_REGISTER_TYPE_UW
), subnr
);
292 static __inline
struct brw_reg
brw_uw1_reg( GLuint file
,
296 return suboffset(retype(brw_vec1_reg(file
, nr
, 0), BRW_REGISTER_TYPE_UW
), subnr
);
299 static __inline
struct brw_reg
brw_imm_reg( GLuint type
)
301 return brw_reg( BRW_IMMEDIATE_VALUE
,
305 BRW_VERTICAL_STRIDE_0
,
307 BRW_HORIZONTAL_STRIDE_0
,
312 static __inline
struct brw_reg
brw_imm_f( GLfloat f
)
314 struct brw_reg imm
= brw_imm_reg(BRW_REGISTER_TYPE_F
);
319 static __inline
struct brw_reg
brw_imm_d( GLint d
)
321 struct brw_reg imm
= brw_imm_reg(BRW_REGISTER_TYPE_D
);
326 static __inline
struct brw_reg
brw_imm_ud( GLuint ud
)
328 struct brw_reg imm
= brw_imm_reg(BRW_REGISTER_TYPE_UD
);
333 static __inline
struct brw_reg
brw_imm_uw( GLushort uw
)
335 struct brw_reg imm
= brw_imm_reg(BRW_REGISTER_TYPE_UW
);
340 static __inline
struct brw_reg
brw_imm_w( GLshort w
)
342 struct brw_reg imm
= brw_imm_reg(BRW_REGISTER_TYPE_W
);
347 /* brw_imm_b and brw_imm_ub aren't supported by hardware - the type
348 * numbers alias with _V and _VF below:
351 /* Vector of eight signed half-byte values:
353 static __inline
struct brw_reg
brw_imm_v( GLuint v
)
355 struct brw_reg imm
= brw_imm_reg(BRW_REGISTER_TYPE_V
);
356 imm
.vstride
= BRW_VERTICAL_STRIDE_0
;
357 imm
.width
= BRW_WIDTH_8
;
358 imm
.hstride
= BRW_HORIZONTAL_STRIDE_1
;
363 /* Vector of four 8-bit float values:
365 static __inline
struct brw_reg
brw_imm_vf( GLuint v
)
367 struct brw_reg imm
= brw_imm_reg(BRW_REGISTER_TYPE_VF
);
368 imm
.vstride
= BRW_VERTICAL_STRIDE_0
;
369 imm
.width
= BRW_WIDTH_4
;
370 imm
.hstride
= BRW_HORIZONTAL_STRIDE_1
;
377 #define VF_NEG (1<<7)
379 static __inline
struct brw_reg
brw_imm_vf4( GLuint v0
,
384 struct brw_reg imm
= brw_imm_reg(BRW_REGISTER_TYPE_VF
);
385 imm
.vstride
= BRW_VERTICAL_STRIDE_0
;
386 imm
.width
= BRW_WIDTH_4
;
387 imm
.hstride
= BRW_HORIZONTAL_STRIDE_1
;
388 imm
.dw1
.ud
= ((v0
<< 0) |
396 static __inline
struct brw_reg
brw_address( struct brw_reg reg
)
398 return brw_imm_uw(reg
.nr
* REG_SIZE
+ reg
.subnr
);
402 static __inline
struct brw_reg
brw_vec1_grf( GLuint nr
,
405 return brw_vec1_reg(BRW_GENERAL_REGISTER_FILE
, nr
, subnr
);
408 static __inline
struct brw_reg
brw_vec8_grf( GLuint nr
,
411 return brw_vec8_reg(BRW_GENERAL_REGISTER_FILE
, nr
, subnr
);
414 static __inline
struct brw_reg
brw_vec4_grf( GLuint nr
,
417 return brw_vec4_reg(BRW_GENERAL_REGISTER_FILE
, nr
, subnr
);
421 static __inline
struct brw_reg
brw_vec2_grf( GLuint nr
,
424 return brw_vec2_reg(BRW_GENERAL_REGISTER_FILE
, nr
, subnr
);
427 static __inline
struct brw_reg
brw_uw8_grf( GLuint nr
,
430 return brw_uw8_reg(BRW_GENERAL_REGISTER_FILE
, nr
, subnr
);
433 static __inline
struct brw_reg
brw_null_reg( void )
435 return brw_vec8_reg(BRW_ARCHITECTURE_REGISTER_FILE
,
440 static __inline
struct brw_reg
brw_address_reg( GLuint subnr
)
442 return brw_uw1_reg(BRW_ARCHITECTURE_REGISTER_FILE
,
447 /* If/else instructions break in align16 mode if writemask & swizzle
448 * aren't xyzw. This goes against the convention for other scalar
451 static __inline
struct brw_reg
brw_ip_reg( void )
453 return brw_reg(BRW_ARCHITECTURE_REGISTER_FILE
,
456 BRW_REGISTER_TYPE_UD
,
457 BRW_VERTICAL_STRIDE_4
, /* ? */
459 BRW_HORIZONTAL_STRIDE_0
,
460 BRW_SWIZZLE_XYZW
, /* NOTE! */
461 WRITEMASK_XYZW
); /* NOTE! */
464 static __inline
struct brw_reg
brw_acc_reg( void )
466 return brw_vec8_reg(BRW_ARCHITECTURE_REGISTER_FILE
,
472 static __inline
struct brw_reg
brw_flag_reg( void )
474 return brw_uw1_reg(BRW_ARCHITECTURE_REGISTER_FILE
,
480 static __inline
struct brw_reg
brw_mask_reg( GLuint subnr
)
482 return brw_uw1_reg(BRW_ARCHITECTURE_REGISTER_FILE
,
487 static __inline
struct brw_reg
brw_message_reg( GLuint nr
)
489 return brw_vec8_reg(BRW_MESSAGE_REGISTER_FILE
,
497 /* This is almost always called with a numeric constant argument, so
498 * make things easy to evaluate at compile time:
500 static __inline GLuint
cvt( GLuint val
)
514 static __inline
struct brw_reg
stride( struct brw_reg reg
,
520 reg
.vstride
= cvt(vstride
);
521 reg
.width
= cvt(width
) - 1;
522 reg
.hstride
= cvt(hstride
);
526 static __inline
struct brw_reg
vec16( struct brw_reg reg
)
528 return stride(reg
, 16,16,1);
531 static __inline
struct brw_reg
vec8( struct brw_reg reg
)
533 return stride(reg
, 8,8,1);
536 static __inline
struct brw_reg
vec4( struct brw_reg reg
)
538 return stride(reg
, 4,4,1);
541 static __inline
struct brw_reg
vec2( struct brw_reg reg
)
543 return stride(reg
, 2,2,1);
546 static __inline
struct brw_reg
vec1( struct brw_reg reg
)
548 return stride(reg
, 0,1,0);
551 static __inline
struct brw_reg
get_element( struct brw_reg reg
, GLuint elt
)
553 return vec1(suboffset(reg
, elt
));
556 static __inline
struct brw_reg
get_element_ud( struct brw_reg reg
, GLuint elt
)
558 return vec1(suboffset(retype(reg
, BRW_REGISTER_TYPE_UD
), elt
));
562 static __inline
struct brw_reg
brw_swizzle( struct brw_reg reg
,
568 reg
.dw1
.bits
.swizzle
= BRW_SWIZZLE4(BRW_GET_SWZ(reg
.dw1
.bits
.swizzle
, x
),
569 BRW_GET_SWZ(reg
.dw1
.bits
.swizzle
, y
),
570 BRW_GET_SWZ(reg
.dw1
.bits
.swizzle
, z
),
571 BRW_GET_SWZ(reg
.dw1
.bits
.swizzle
, w
));
576 static __inline
struct brw_reg
brw_swizzle1( struct brw_reg reg
,
579 return brw_swizzle(reg
, x
, x
, x
, x
);
582 static __inline
struct brw_reg
brw_writemask( struct brw_reg reg
,
585 reg
.dw1
.bits
.writemask
&= mask
;
589 static __inline
struct brw_reg
brw_set_writemask( struct brw_reg reg
,
592 reg
.dw1
.bits
.writemask
= mask
;
596 static __inline
struct brw_reg
negate( struct brw_reg reg
)
602 static __inline
struct brw_reg
brw_abs( struct brw_reg reg
)
608 /***********************************************************************
610 static __inline
struct brw_reg
brw_vec4_indirect( GLuint subnr
,
613 struct brw_reg reg
= brw_vec4_grf(0, 0);
615 reg
.address_mode
= BRW_ADDRESS_REGISTER_INDIRECT_REGISTER
;
616 reg
.dw1
.bits
.indirect_offset
= offset
;
620 static __inline
struct brw_reg
brw_vec1_indirect( GLuint subnr
,
623 struct brw_reg reg
= brw_vec1_grf(0, 0);
625 reg
.address_mode
= BRW_ADDRESS_REGISTER_INDIRECT_REGISTER
;
626 reg
.dw1
.bits
.indirect_offset
= offset
;
630 static __inline
struct brw_reg
deref_4f(struct brw_indirect ptr
, GLint offset
)
632 return brw_vec4_indirect(ptr
.addr_subnr
, ptr
.addr_offset
+ offset
);
635 static __inline
struct brw_reg
deref_1f(struct brw_indirect ptr
, GLint offset
)
637 return brw_vec1_indirect(ptr
.addr_subnr
, ptr
.addr_offset
+ offset
);
640 static __inline
struct brw_reg
deref_4b(struct brw_indirect ptr
, GLint offset
)
642 return retype(deref_4f(ptr
, offset
), BRW_REGISTER_TYPE_B
);
645 static __inline
struct brw_reg
deref_1uw(struct brw_indirect ptr
, GLint offset
)
647 return retype(deref_1f(ptr
, offset
), BRW_REGISTER_TYPE_UW
);
650 static __inline
struct brw_reg
get_addr_reg(struct brw_indirect ptr
)
652 return brw_address_reg(ptr
.addr_subnr
);
655 static __inline
struct brw_indirect
brw_indirect_offset( struct brw_indirect ptr
, GLint offset
)
657 ptr
.addr_offset
+= offset
;
661 static __inline
struct brw_indirect
brw_indirect( GLuint addr_subnr
, GLint offset
)
663 struct brw_indirect ptr
;
664 ptr
.addr_subnr
= addr_subnr
;
665 ptr
.addr_offset
= offset
;
672 void brw_pop_insn_state( struct brw_compile
*p
);
673 void brw_push_insn_state( struct brw_compile
*p
);
674 void brw_set_mask_control( struct brw_compile
*p
, GLuint value
);
675 void brw_set_saturate( struct brw_compile
*p
, GLuint value
);
676 void brw_set_access_mode( struct brw_compile
*p
, GLuint access_mode
);
677 void brw_set_compression_control( struct brw_compile
*p
, GLboolean control
);
678 void brw_set_predicate_control_flag_value( struct brw_compile
*p
, GLuint value
);
679 void brw_set_predicate_control( struct brw_compile
*p
, GLuint pc
);
680 void brw_set_conditionalmod( struct brw_compile
*p
, GLuint conditional
);
682 void brw_init_compile( struct brw_compile
*p
);
683 const GLuint
*brw_get_program( struct brw_compile
*p
, GLuint
*sz
);
686 /* Helpers for regular instructions:
689 struct brw_instruction *brw_##OP(struct brw_compile *p, \
690 struct brw_reg dest, \
691 struct brw_reg src0);
694 struct brw_instruction *brw_##OP(struct brw_compile *p, \
695 struct brw_reg dest, \
696 struct brw_reg src0, \
697 struct brw_reg src1);
729 /* Helpers for SEND instruction:
731 void brw_urb_WRITE(struct brw_compile
*p
,
738 GLuint response_length
,
740 GLboolean writes_complete
,
744 void brw_fb_WRITE(struct brw_compile
*p
,
748 GLuint binding_table_index
,
750 GLuint response_length
,
753 void brw_SAMPLE(struct brw_compile
*p
,
757 GLuint binding_table_index
,
761 GLuint response_length
,
765 void brw_math_16( struct brw_compile
*p
,
773 void brw_math( struct brw_compile
*p
,
782 void brw_dp_READ_16( struct brw_compile
*p
,
785 GLuint scratch_offset
);
787 void brw_dp_WRITE_16( struct brw_compile
*p
,
790 GLuint scratch_offset
);
792 /* If/else/endif. Works by manipulating the execution flags on each
795 struct brw_instruction
*brw_IF(struct brw_compile
*p
,
796 GLuint execute_size
);
798 struct brw_instruction
*brw_ELSE(struct brw_compile
*p
,
799 struct brw_instruction
*if_insn
);
801 void brw_ENDIF(struct brw_compile
*p
,
802 struct brw_instruction
*if_or_else_insn
);
807 struct brw_instruction
*brw_DO(struct brw_compile
*p
,
808 GLuint execute_size
);
810 void brw_WHILE(struct brw_compile
*p
,
811 struct brw_instruction
*patch_insn
);
815 void brw_land_fwd_jump(struct brw_compile
*p
,
816 struct brw_instruction
*jmp_insn
);
820 void brw_NOP(struct brw_compile
*p
);
822 /* Special case: there is never a destination, execution size will be
825 void brw_CMP(struct brw_compile
*p
,
829 struct brw_reg src1
);
831 void brw_print_reg( struct brw_reg reg
);
834 /***********************************************************************
838 void brw_copy_indirect_to_indirect(struct brw_compile
*p
,
839 struct brw_indirect dst_ptr
,
840 struct brw_indirect src_ptr
,
843 void brw_copy_from_indirect(struct brw_compile
*p
,
845 struct brw_indirect ptr
,
848 void brw_copy4(struct brw_compile
*p
,
853 void brw_copy8(struct brw_compile
*p
,
858 void brw_math_invert( struct brw_compile
*p
,