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"
30 #include "brw_defines.h"
32 #include "main/macros.h"
33 #include "main/enums.h"
35 struct brw_depth_stencil_state_key
{
37 GLboolean depth_test
, depth_write
;
38 GLboolean stencil
, stencil_two_side
;
39 GLenum stencil_func
[2], stencil_fail_op
[2];
40 GLenum stencil_pass_depth_fail_op
[2], stencil_pass_depth_pass_op
[2];
41 GLubyte stencil_write_mask
[2], stencil_test_mask
[2];
45 depth_stencil_state_populate_key(struct brw_context
*brw
,
46 struct brw_depth_stencil_state_key
*key
)
48 GLcontext
*ctx
= &brw
->intel
.ctx
;
49 const unsigned back
= ctx
->Stencil
._BackFace
;
51 memset(key
, 0, sizeof(*key
));
54 key
->stencil
= ctx
->Stencil
._Enabled
;
55 key
->stencil_two_side
= ctx
->Stencil
._TestTwoSide
;
58 key
->stencil_func
[0] = ctx
->Stencil
.Function
[0];
59 key
->stencil_fail_op
[0] = ctx
->Stencil
.FailFunc
[0];
60 key
->stencil_pass_depth_fail_op
[0] = ctx
->Stencil
.ZFailFunc
[0];
61 key
->stencil_pass_depth_pass_op
[0] = ctx
->Stencil
.ZPassFunc
[0];
62 key
->stencil_write_mask
[0] = ctx
->Stencil
.WriteMask
[0];
63 key
->stencil_test_mask
[0] = ctx
->Stencil
.ValueMask
[0];
65 if (key
->stencil_two_side
) {
66 key
->stencil_func
[1] = ctx
->Stencil
.Function
[back
];
67 key
->stencil_fail_op
[1] = ctx
->Stencil
.FailFunc
[back
];
68 key
->stencil_pass_depth_fail_op
[1] = ctx
->Stencil
.ZFailFunc
[back
];
69 key
->stencil_pass_depth_pass_op
[1] = ctx
->Stencil
.ZPassFunc
[back
];
70 key
->stencil_write_mask
[1] = ctx
->Stencil
.WriteMask
[back
];
71 key
->stencil_test_mask
[1] = ctx
->Stencil
.ValueMask
[back
];
74 key
->depth_test
= ctx
->Depth
.Test
;
75 if (key
->depth_test
) {
76 key
->depth_func
= ctx
->Depth
.Func
;
77 key
->depth_write
= ctx
->Depth
.Mask
;
82 * Creates the state cache entry for the given DEPTH_STENCIL_STATE state key.
85 depth_stencil_state_create_from_key(struct brw_context
*brw
,
86 struct brw_depth_stencil_state_key
*key
)
88 struct gen6_depth_stencil_state ds
;
91 memset(&ds
, 0, sizeof(ds
));
95 ds
.ds0
.stencil_enable
= 1;
97 intel_translate_compare_func(key
->stencil_func
[0]);
98 ds
.ds0
.stencil_fail_op
=
99 intel_translate_stencil_op(key
->stencil_fail_op
[0]);
100 ds
.ds0
.stencil_pass_depth_fail_op
=
101 intel_translate_stencil_op(key
->stencil_pass_depth_fail_op
[0]);
102 ds
.ds0
.stencil_pass_depth_pass_op
=
103 intel_translate_stencil_op(key
->stencil_pass_depth_pass_op
[0]);
104 ds
.ds1
.stencil_write_mask
= key
->stencil_write_mask
[0];
105 ds
.ds1
.stencil_test_mask
= key
->stencil_test_mask
[0];
107 if (key
->stencil_two_side
) {
108 ds
.ds0
.bf_stencil_enable
= 1;
109 ds
.ds0
.bf_stencil_func
=
110 intel_translate_compare_func(key
->stencil_func
[1]);
111 ds
.ds0
.bf_stencil_fail_op
=
112 intel_translate_stencil_op(key
->stencil_fail_op
[1]);
113 ds
.ds0
.bf_stencil_pass_depth_fail_op
=
114 intel_translate_stencil_op(key
->stencil_pass_depth_fail_op
[1]);
115 ds
.ds0
.bf_stencil_pass_depth_pass_op
=
116 intel_translate_stencil_op(key
->stencil_pass_depth_pass_op
[1]);
117 ds
.ds1
.bf_stencil_write_mask
= key
->stencil_write_mask
[1];
118 ds
.ds1
.bf_stencil_test_mask
= key
->stencil_test_mask
[1];
121 /* Not really sure about this:
123 if (key
->stencil_write_mask
[0] ||
124 (key
->stencil_two_side
&& key
->stencil_write_mask
[1]))
125 ds
.ds0
.stencil_write_enable
= 1;
129 if (key
->depth_test
) {
130 ds
.ds2
.depth_test_enable
= 1;
131 ds
.ds2
.depth_test_func
= intel_translate_compare_func(key
->depth_func
);
132 ds
.ds2
.depth_write_enable
= key
->depth_write
;
135 bo
= brw_upload_cache(&brw
->cache
, BRW_DEPTH_STENCIL_STATE
,
144 prepare_depth_stencil_state(struct brw_context
*brw
)
146 struct brw_depth_stencil_state_key key
;
148 depth_stencil_state_populate_key(brw
, &key
);
150 dri_bo_unreference(brw
->cc
.depth_stencil_state_bo
);
151 brw
->cc
.depth_stencil_state_bo
= brw_search_cache(&brw
->cache
,
152 BRW_DEPTH_STENCIL_STATE
,
157 if (brw
->cc
.depth_stencil_state_bo
== NULL
)
158 brw
->cc
.depth_stencil_state_bo
=
159 depth_stencil_state_create_from_key(brw
, &key
);
162 const struct brw_tracked_state gen6_depth_stencil_state
= {
164 .mesa
= _NEW_DEPTH
| _NEW_STENCIL
,
168 .prepare
= prepare_depth_stencil_state
,