i965g: driver and winsys compile
[mesa.git] / src / gallium / drivers / i965 / brw_cc.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a copy of this software and associated documentation files (the
8 "Software"), to deal in the Software without restriction, including
9 without limitation the rights to use, copy, modify, merge, publish,
10 distribute, sublicense, and/or sell copies of the Software, and to
11 permit persons to whom the Software is furnished to do so, subject to
12 the following conditions:
13
14 The above copyright notice and this permission notice (including the
15 next paragraph) shall be included in all copies or substantial
16 portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **********************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
30 */
31
32
33 #include "brw_context.h"
34 #include "brw_state.h"
35 #include "brw_defines.h"
36
37
38 struct sane_viewport {
39 float top;
40 float left;
41 float width;
42 float height;
43 float near;
44 float far;
45 };
46
47 static void calc_sane_viewport( const struct pipe_viewport_state *vp,
48 struct sane_viewport *svp )
49 {
50 /* XXX fix me, obviously.
51 */
52 svp->top = 0;
53 svp->left = 0;
54 svp->width = 250;
55 svp->height = 250;
56 svp->near = 0;
57 svp->far = 1;
58 }
59
60 static int prepare_cc_vp( struct brw_context *brw )
61 {
62 struct brw_cc_viewport ccv;
63 struct sane_viewport svp;
64
65 memset(&ccv, 0, sizeof(ccv));
66
67 /* PIPE_NEW_VIEWPORT */
68 calc_sane_viewport( &brw->curr.vp, &svp );
69
70 ccv.min_depth = svp.near;
71 ccv.max_depth = svp.far;
72
73 brw->sws->bo_unreference(brw->cc.vp_bo);
74 brw->cc.vp_bo = brw_cache_data( &brw->cache, BRW_CC_VP, &ccv, NULL, 0 );
75
76 return 0;
77 }
78
79 const struct brw_tracked_state brw_cc_vp = {
80 .dirty = {
81 .mesa = PIPE_NEW_VIEWPORT,
82 .brw = BRW_NEW_CONTEXT,
83 .cache = 0
84 },
85 .prepare = prepare_cc_vp
86 };
87
88 struct brw_cc_unit_key {
89 struct brw_cc0 cc0;
90 struct brw_cc1 cc1;
91 struct brw_cc2 cc2;
92 struct brw_cc3 cc3;
93 struct brw_cc5 cc5;
94 struct brw_cc6 cc6;
95 struct brw_cc7 cc7;
96 };
97
98 /* A long-winded way to OR two unsigned integers together:
99 */
100 static INLINE struct brw_cc3
101 combine_cc3( struct brw_cc3 a, struct brw_cc3 b )
102 {
103 union { struct brw_cc3 cc3; unsigned i; } ca, cb;
104 ca.cc3 = a;
105 cb.cc3 = b;
106 ca.i |= cb.i;
107 return ca.cc3;
108 }
109
110 static void
111 cc_unit_populate_key(const struct brw_context *brw,
112 struct brw_cc_unit_key *key)
113 {
114 key->cc0 = brw->curr.zstencil->cc0;
115 key->cc1 = brw->curr.zstencil->cc1;
116 key->cc2 = brw->curr.zstencil->cc2;
117 key->cc3 = combine_cc3( brw->curr.zstencil->cc3, brw->curr.blend->cc3 );
118 key->cc5 = brw->curr.blend->cc5;
119 key->cc6 = brw->curr.blend->cc6;
120 key->cc7 = brw->curr.zstencil->cc7;
121 }
122
123 /**
124 * Creates the state cache entry for the given CC unit key.
125 */
126 static struct brw_winsys_buffer *
127 cc_unit_create_from_key(struct brw_context *brw, struct brw_cc_unit_key *key)
128 {
129 struct brw_cc_unit_state cc;
130 struct brw_winsys_buffer *bo;
131
132 memset(&cc, 0, sizeof(cc));
133
134 cc.cc0 = key->cc0;
135 cc.cc1 = key->cc1;
136 cc.cc2 = key->cc2;
137 cc.cc3 = key->cc3;
138
139 /* CACHE_NEW_CC_VP */
140 cc.cc4.cc_viewport_state_offset = *(brw->cc.vp_bo->offset) >> 5; /* reloc */
141
142 cc.cc5 = key->cc5;
143 cc.cc6 = key->cc6;
144 cc.cc7 = key->cc7;
145
146 bo = brw_upload_cache(&brw->cache, BRW_CC_UNIT,
147 key, sizeof(*key),
148 &brw->cc.vp_bo, 1,
149 &cc, sizeof(cc),
150 NULL, NULL);
151
152 /* Emit CC viewport relocation */
153 brw->sws->bo_emit_reloc(bo,
154 I915_GEM_DOMAIN_INSTRUCTION,
155 0,
156 0,
157 offsetof(struct brw_cc_unit_state, cc4),
158 brw->cc.vp_bo);
159
160 return bo;
161 }
162
163 static int prepare_cc_unit( struct brw_context *brw )
164 {
165 struct brw_cc_unit_key key;
166
167 cc_unit_populate_key(brw, &key);
168
169 brw->sws->bo_unreference(brw->cc.state_bo);
170 brw->cc.state_bo = brw_search_cache(&brw->cache, BRW_CC_UNIT,
171 &key, sizeof(key),
172 &brw->cc.vp_bo, 1,
173 NULL);
174
175 if (brw->cc.state_bo == NULL)
176 brw->cc.state_bo = cc_unit_create_from_key(brw, &key);
177
178 return 0;
179 }
180
181 const struct brw_tracked_state brw_cc_unit = {
182 .dirty = {
183 .mesa = PIPE_NEW_DEPTH_STENCIL_ALPHA | PIPE_NEW_BLEND,
184 .brw = 0,
185 .cache = CACHE_NEW_CC_VP
186 },
187 .prepare = prepare_cc_unit,
188 };
189
190
191