2 * Copyright © 2009 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Eric Anholt <eric@anholt.net>
28 #include "brw_context.h"
29 #include "brw_state.h"
31 struct brw_depth_stencil_state_key
{
33 GLboolean depth_test
, depth_write
;
34 GLboolean stencil
, stencil_two_side
;
35 GLenum stencil_func
[2], stencil_fail_op
[2];
36 GLenum stencil_pass_depth_fail_op
[2], stencil_pass_depth_pass_op
[2];
37 GLubyte stencil_write_mask
[2], stencil_test_mask
[2];
41 depth_stencil_state_populate_key(struct brw_context
*brw
,
42 struct brw_depth_stencil_state_key
*key
)
44 GLcontext
*ctx
= &brw
->intel
.ctx
;
45 const unsigned back
= ctx
->Stencil
._BackFace
;
47 memset(key
, 0, sizeof(*key
));
50 key
->stencil
= ctx
->Stencil
._Enabled
;
51 key
->stencil_two_side
= ctx
->Stencil
._TestTwoSide
;
54 key
->stencil_func
[0] = ctx
->Stencil
.Function
[0];
55 key
->stencil_fail_op
[0] = ctx
->Stencil
.FailFunc
[0];
56 key
->stencil_pass_depth_fail_op
[0] = ctx
->Stencil
.ZFailFunc
[0];
57 key
->stencil_pass_depth_pass_op
[0] = ctx
->Stencil
.ZPassFunc
[0];
58 key
->stencil_write_mask
[0] = ctx
->Stencil
.WriteMask
[0];
59 key
->stencil_test_mask
[0] = ctx
->Stencil
.ValueMask
[0];
61 if (key
->stencil_two_side
) {
62 key
->stencil_func
[1] = ctx
->Stencil
.Function
[back
];
63 key
->stencil_fail_op
[1] = ctx
->Stencil
.FailFunc
[back
];
64 key
->stencil_pass_depth_fail_op
[1] = ctx
->Stencil
.ZFailFunc
[back
];
65 key
->stencil_pass_depth_pass_op
[1] = ctx
->Stencil
.ZPassFunc
[back
];
66 key
->stencil_write_mask
[1] = ctx
->Stencil
.WriteMask
[back
];
67 key
->stencil_test_mask
[1] = ctx
->Stencil
.ValueMask
[back
];
70 key
->depth_test
= ctx
->Depth
.Test
;
71 if (key
->depth_test
) {
72 key
->depth_func
= ctx
->Depth
.Func
;
73 key
->depth_write
= ctx
->Depth
.Mask
;
78 * Creates the state cache entry for the given DEPTH_STENCIL_STATE state key.
81 depth_stencil_state_create_from_key(struct brw_context
*brw
,
82 struct brw_depth_stencil_state_key
*key
)
84 struct gen6_depth_stencil_state ds
;
87 memset(&ds
, 0, sizeof(ds
));
91 ds
.ds0
.stencil_enable
= 1;
93 intel_translate_compare_func(key
->stencil_func
[0]);
94 ds
.ds0
.stencil_fail_op
=
95 intel_translate_stencil_op(key
->stencil_fail_op
[0]);
96 ds
.ds0
.stencil_pass_depth_fail_op
=
97 intel_translate_stencil_op(key
->stencil_pass_depth_fail_op
[0]);
98 ds
.ds0
.stencil_pass_depth_pass_op
=
99 intel_translate_stencil_op(key
->stencil_pass_depth_pass_op
[0]);
100 ds
.ds1
.stencil_write_mask
= key
->stencil_write_mask
[0];
101 ds
.ds1
.stencil_test_mask
= key
->stencil_test_mask
[0];
103 if (key
->stencil_two_side
) {
104 ds
.ds0
.bf_stencil_enable
= 1;
105 ds
.ds0
.bf_stencil_func
=
106 intel_translate_compare_func(key
->stencil_func
[1]);
107 ds
.ds0
.bf_stencil_fail_op
=
108 intel_translate_stencil_op(key
->stencil_fail_op
[1]);
109 ds
.ds0
.bf_stencil_pass_depth_fail_op
=
110 intel_translate_stencil_op(key
->stencil_pass_depth_fail_op
[1]);
111 ds
.ds0
.bf_stencil_pass_depth_pass_op
=
112 intel_translate_stencil_op(key
->stencil_pass_depth_pass_op
[1]);
113 ds
.ds1
.bf_stencil_write_mask
= key
->stencil_write_mask
[1];
114 ds
.ds1
.bf_stencil_test_mask
= key
->stencil_test_mask
[1];
117 /* Not really sure about this:
119 if (key
->stencil_write_mask
[0] ||
120 (key
->stencil_two_side
&& key
->stencil_write_mask
[1]))
121 ds
.ds0
.stencil_write_enable
= 1;
125 if (key
->depth_test
) {
126 ds
.ds2
.depth_test_enable
= 1;
127 ds
.ds2
.depth_test_func
= intel_translate_compare_func(key
->depth_func
);
128 ds
.ds2
.depth_write_enable
= key
->depth_write
;
131 bo
= brw_upload_cache(&brw
->cache
, BRW_DEPTH_STENCIL_STATE
,
140 prepare_depth_stencil_state(struct brw_context
*brw
)
142 struct brw_depth_stencil_state_key key
;
144 depth_stencil_state_populate_key(brw
, &key
);
146 dri_bo_unreference(brw
->cc
.depth_stencil_state_bo
);
147 brw
->cc
.depth_stencil_state_bo
= brw_search_cache(&brw
->cache
,
148 BRW_DEPTH_STENCIL_STATE
,
153 if (brw
->cc
.depth_stencil_state_bo
== NULL
)
154 brw
->cc
.depth_stencil_state_bo
=
155 depth_stencil_state_create_from_key(brw
, &key
);
158 const struct brw_tracked_state gen6_depth_stencil_state
= {
160 .mesa
= _NEW_DEPTH
| _NEW_STENCIL
,
164 .prepare
= prepare_depth_stencil_state
,