i965: Start adding support for the Sandybridge CC unit.
[mesa.git] / src / mesa / drivers / dri / i965 / gen6_depthstencil.c
1 /*
2 * Copyright © 2009 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28 #include "brw_context.h"
29 #include "brw_state.h"
30 #include "brw_defines.h"
31 #include "brw_util.h"
32 #include "main/macros.h"
33 #include "main/enums.h"
34
35 struct brw_depth_stencil_state_key {
36 GLenum depth_func;
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];
42 };
43
44 static void
45 depth_stencil_state_populate_key(struct brw_context *brw,
46 struct brw_depth_stencil_state_key *key)
47 {
48 GLcontext *ctx = &brw->intel.ctx;
49 const unsigned back = ctx->Stencil._BackFace;
50
51 memset(key, 0, sizeof(*key));
52
53 /* _NEW_STENCIL */
54 key->stencil = ctx->Stencil._Enabled;
55 key->stencil_two_side = ctx->Stencil._TestTwoSide;
56
57 if (key->stencil) {
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];
64 }
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];
72 }
73
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;
78 }
79 }
80
81 /**
82 * Creates the state cache entry for the given DEPTH_STENCIL_STATE state key.
83 */
84 static dri_bo *
85 depth_stencil_state_create_from_key(struct brw_context *brw,
86 struct brw_depth_stencil_state_key *key)
87 {
88 struct gen6_depth_stencil_state ds;
89 dri_bo *bo;
90
91 memset(&ds, 0, sizeof(ds));
92
93 /* _NEW_STENCIL */
94 if (key->stencil) {
95 ds.ds0.stencil_enable = 1;
96 ds.ds0.stencil_func =
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];
106
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];
119 }
120
121 /* Not really sure about this:
122 */
123 if (key->stencil_write_mask[0] ||
124 (key->stencil_two_side && key->stencil_write_mask[1]))
125 ds.ds0.stencil_write_enable = 1;
126 }
127
128 /* _NEW_DEPTH */
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;
133 }
134
135 bo = brw_upload_cache(&brw->cache, BRW_DEPTH_STENCIL_STATE,
136 key, sizeof(*key),
137 NULL, 0,
138 &ds, sizeof(ds));
139
140 return bo;
141 }
142
143 static void
144 prepare_depth_stencil_state(struct brw_context *brw)
145 {
146 struct brw_depth_stencil_state_key key;
147
148 depth_stencil_state_populate_key(brw, &key);
149
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,
153 &key, sizeof(key),
154 NULL, 0,
155 NULL);
156
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);
160 }
161
162 const struct brw_tracked_state gen6_depth_stencil_state = {
163 .dirty = {
164 .mesa = _NEW_DEPTH | _NEW_STENCIL,
165 .brw = 0,
166 .cache = 0,
167 },
168 .prepare = prepare_depth_stencil_state,
169 };