Merge remote branch 'origin/7.8'
[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
31 struct brw_depth_stencil_state_key {
32 GLenum depth_func;
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];
38 };
39
40 static void
41 depth_stencil_state_populate_key(struct brw_context *brw,
42 struct brw_depth_stencil_state_key *key)
43 {
44 GLcontext *ctx = &brw->intel.ctx;
45 const unsigned back = ctx->Stencil._BackFace;
46
47 memset(key, 0, sizeof(*key));
48
49 /* _NEW_STENCIL */
50 key->stencil = ctx->Stencil._Enabled;
51 key->stencil_two_side = ctx->Stencil._TestTwoSide;
52
53 if (key->stencil) {
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];
60 }
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];
68 }
69
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;
74 }
75 }
76
77 /**
78 * Creates the state cache entry for the given DEPTH_STENCIL_STATE state key.
79 */
80 static dri_bo *
81 depth_stencil_state_create_from_key(struct brw_context *brw,
82 struct brw_depth_stencil_state_key *key)
83 {
84 struct gen6_depth_stencil_state ds;
85 dri_bo *bo;
86
87 memset(&ds, 0, sizeof(ds));
88
89 /* _NEW_STENCIL */
90 if (key->stencil) {
91 ds.ds0.stencil_enable = 1;
92 ds.ds0.stencil_func =
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];
102
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];
115 }
116
117 /* Not really sure about this:
118 */
119 if (key->stencil_write_mask[0] ||
120 (key->stencil_two_side && key->stencil_write_mask[1]))
121 ds.ds0.stencil_write_enable = 1;
122 }
123
124 /* _NEW_DEPTH */
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;
129 }
130
131 bo = brw_upload_cache(&brw->cache, BRW_DEPTH_STENCIL_STATE,
132 key, sizeof(*key),
133 NULL, 0,
134 &ds, sizeof(ds));
135
136 return bo;
137 }
138
139 static void
140 prepare_depth_stencil_state(struct brw_context *brw)
141 {
142 struct brw_depth_stencil_state_key key;
143
144 depth_stencil_state_populate_key(brw, &key);
145
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,
149 &key, sizeof(key),
150 NULL, 0,
151 NULL);
152
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);
156 }
157
158 const struct brw_tracked_state gen6_depth_stencil_state = {
159 .dirty = {
160 .mesa = _NEW_DEPTH | _NEW_STENCIL,
161 .brw = 0,
162 .cache = 0,
163 },
164 .prepare = prepare_depth_stencil_state,
165 };