2 #include "util/u_math.h"
3 #include "util/u_memory.h"
5 #include "brw_context.h"
6 #include "brw_defines.h"
8 /* XXX: Fixme - include this to get IZ_ defines
12 static unsigned brw_translate_compare_func(unsigned func
)
16 return BRW_COMPAREFUNCTION_NEVER
;
18 return BRW_COMPAREFUNCTION_LESS
;
19 case PIPE_FUNC_LEQUAL
:
20 return BRW_COMPAREFUNCTION_LEQUAL
;
21 case PIPE_FUNC_GREATER
:
22 return BRW_COMPAREFUNCTION_GREATER
;
23 case PIPE_FUNC_GEQUAL
:
24 return BRW_COMPAREFUNCTION_GEQUAL
;
25 case PIPE_FUNC_NOTEQUAL
:
26 return BRW_COMPAREFUNCTION_NOTEQUAL
;
28 return BRW_COMPAREFUNCTION_EQUAL
;
29 case PIPE_FUNC_ALWAYS
:
30 return BRW_COMPAREFUNCTION_ALWAYS
;
33 return BRW_COMPAREFUNCTION_ALWAYS
;
37 static unsigned translate_stencil_op(unsigned op
)
40 case PIPE_STENCIL_OP_KEEP
:
41 return BRW_STENCILOP_KEEP
;
42 case PIPE_STENCIL_OP_ZERO
:
43 return BRW_STENCILOP_ZERO
;
44 case PIPE_STENCIL_OP_REPLACE
:
45 return BRW_STENCILOP_REPLACE
;
46 case PIPE_STENCIL_OP_INCR
:
47 return BRW_STENCILOP_INCRSAT
;
48 case PIPE_STENCIL_OP_DECR
:
49 return BRW_STENCILOP_DECRSAT
;
50 case PIPE_STENCIL_OP_INCR_WRAP
:
51 return BRW_STENCILOP_INCR
;
52 case PIPE_STENCIL_OP_DECR_WRAP
:
53 return BRW_STENCILOP_DECR
;
54 case PIPE_STENCIL_OP_INVERT
:
55 return BRW_STENCILOP_INVERT
;
58 return BRW_STENCILOP_ZERO
;
62 static void create_bcc_state( struct brw_depth_stencil_state
*zstencil
,
63 const struct pipe_depth_stencil_alpha_state
*templ
)
65 if (templ
->stencil
[0].enabled
) {
66 zstencil
->cc0
.stencil_enable
= 1;
67 zstencil
->cc0
.stencil_func
=
68 brw_translate_compare_func(templ
->stencil
[0].func
);
69 zstencil
->cc0
.stencil_fail_op
=
70 translate_stencil_op(templ
->stencil
[0].fail_op
);
71 zstencil
->cc0
.stencil_pass_depth_fail_op
=
72 translate_stencil_op(templ
->stencil
[0].zfail_op
);
73 zstencil
->cc0
.stencil_pass_depth_pass_op
=
74 translate_stencil_op(templ
->stencil
[0].zpass_op
);
75 zstencil
->cc1
.stencil_ref
= templ
->stencil
[0].ref_value
;
76 zstencil
->cc1
.stencil_write_mask
= templ
->stencil
[0].writemask
;
77 zstencil
->cc1
.stencil_test_mask
= templ
->stencil
[0].valuemask
;
79 if (templ
->stencil
[1].enabled
) {
80 zstencil
->cc0
.bf_stencil_enable
= 1;
81 zstencil
->cc0
.bf_stencil_func
=
82 brw_translate_compare_func(templ
->stencil
[1].func
);
83 zstencil
->cc0
.bf_stencil_fail_op
=
84 translate_stencil_op(templ
->stencil
[1].fail_op
);
85 zstencil
->cc0
.bf_stencil_pass_depth_fail_op
=
86 translate_stencil_op(templ
->stencil
[1].zfail_op
);
87 zstencil
->cc0
.bf_stencil_pass_depth_pass_op
=
88 translate_stencil_op(templ
->stencil
[1].zpass_op
);
89 zstencil
->cc1
.bf_stencil_ref
= templ
->stencil
[1].ref_value
;
90 zstencil
->cc2
.bf_stencil_write_mask
= templ
->stencil
[1].writemask
;
91 zstencil
->cc2
.bf_stencil_test_mask
= templ
->stencil
[1].valuemask
;
94 zstencil
->cc0
.stencil_write_enable
= (zstencil
->cc1
.stencil_write_mask
||
95 zstencil
->cc2
.bf_stencil_write_mask
);
99 if (templ
->alpha
.enabled
) {
100 zstencil
->cc3
.alpha_test
= 1;
101 zstencil
->cc3
.alpha_test_func
= brw_translate_compare_func(templ
->alpha
.func
);
102 zstencil
->cc3
.alpha_test_format
= BRW_ALPHATEST_FORMAT_UNORM8
;
103 zstencil
->cc7
.alpha_ref
.ub
[0] = float_to_ubyte(templ
->alpha
.ref_value
);
106 if (templ
->depth
.enabled
) {
107 zstencil
->cc2
.depth_test
= 1;
108 zstencil
->cc2
.depth_test_function
= brw_translate_compare_func(templ
->depth
.func
);
109 zstencil
->cc2
.depth_write_enable
= templ
->depth
.writemask
;
113 static void create_wm_iz_state( struct brw_depth_stencil_state
*zstencil
)
115 if (zstencil
->cc3
.alpha_test
)
116 zstencil
->iz_lookup
|= IZ_PS_KILL_ALPHATEST_BIT
;
118 if (zstencil
->cc2
.depth_test
)
119 zstencil
->iz_lookup
|= IZ_DEPTH_TEST_ENABLE_BIT
;
121 if (zstencil
->cc2
.depth_write_enable
)
122 zstencil
->iz_lookup
|= IZ_DEPTH_WRITE_ENABLE_BIT
;
124 if (zstencil
->cc0
.stencil_enable
)
125 zstencil
->iz_lookup
|= IZ_STENCIL_TEST_ENABLE_BIT
;
127 if (zstencil
->cc0
.stencil_write_enable
)
128 zstencil
->iz_lookup
|= IZ_STENCIL_WRITE_ENABLE_BIT
;
134 brw_create_depth_stencil_state( struct pipe_context
*pipe
,
135 const struct pipe_depth_stencil_alpha_state
*templ
)
137 struct brw_depth_stencil_state
*zstencil
= CALLOC_STRUCT(brw_depth_stencil_state
);
139 create_bcc_state( zstencil
, templ
);
140 create_wm_iz_state( zstencil
);
142 return (void *)zstencil
;
146 static void brw_bind_depth_stencil_state(struct pipe_context
*pipe
,
149 struct brw_context
*brw
= brw_context(pipe
);
150 brw
->curr
.zstencil
= (const struct brw_depth_stencil_state
*)cso
;
151 brw
->state
.dirty
.mesa
|= PIPE_NEW_DEPTH_STENCIL_ALPHA
;
154 static void brw_delete_depth_stencil_state(struct pipe_context
*pipe
,
157 struct brw_context
*brw
= brw_context(pipe
);
158 assert((const void *)cso
!= (const void *)brw
->curr
.zstencil
);
163 void brw_pipe_depth_stencil_init( struct brw_context
*brw
)
165 brw
->base
.create_depth_stencil_alpha_state
= brw_create_depth_stencil_state
;
166 brw
->base
.bind_depth_stencil_alpha_state
= brw_bind_depth_stencil_state
;
167 brw
->base
.delete_depth_stencil_alpha_state
= brw_delete_depth_stencil_state
;
170 void brw_pipe_depth_stencil_cleanup( struct brw_context
*brw
)