Add more i915 state packets.
[mesa.git] / src / mesa / pipe / i915simple / i915_state_dynamic.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * 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, sub license, 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 portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "glheader.h"
29 #include "context.h"
30 #include "macros.h"
31 #include "enums.h"
32
33 #include "i915_batch.h"
34 #include "i915_state_inlines.h"
35 #include "i915_context.h"
36 #include "i915_reg.h"
37 #include "i915_state.h"
38
39 #define FILE_DEBUG_FLAG DEBUG_STATE
40
41 /* State that we have chosen to store in the DYNAMIC segment of the
42 * i915 indirect state mechanism.
43 *
44 * Can't cache these in the way we do the static state, as there is no
45 * start/size in the command packet, instead an 'end' value that gets
46 * incremented.
47 *
48 * Additionally, there seems to be a requirement to re-issue the full
49 * (active) state every time a 4kb boundary is crossed.
50 */
51
52 static inline void set_dynamic_indirect( struct i915_context *i915,
53 GLuint offset,
54 const GLuint *src,
55 GLuint dwords )
56 {
57 int i;
58
59 for (i = 0; i < dwords; i++)
60 i915->current.dynamic[offset + i] = src[i];
61
62 i915->hardware_dirty |= I915_HW_DYNAMIC;
63 }
64
65
66 /***********************************************************************
67 * Modes4: stencil masks and logicop
68 */
69 static void upload_MODES4( struct i915_context *i915 )
70 {
71 GLuint modes4 = 0;
72
73 /* I915_NEW_STENCIL */
74 {
75 GLint testmask = i915->stencil.value_mask[0] & 0xff;
76 GLint writemask = i915->stencil.write_mask[0] & 0xff;
77
78 modes4 |= (_3DSTATE_MODES_4_CMD |
79 ENABLE_STENCIL_TEST_MASK |
80 STENCIL_TEST_MASK(testmask) |
81 ENABLE_STENCIL_WRITE_MASK |
82 STENCIL_WRITE_MASK(writemask));
83 }
84
85 /* I915_NEW_BLEND */
86 {
87 modes4 |= (_3DSTATE_MODES_4_CMD |
88 ENABLE_LOGIC_OP_FUNC |
89 LOGIC_OP_FUNC(i915_translate_logic_op(i915->blend.logicop_func)));
90 }
91
92 /* Always, so that we know when state is in-active:
93 */
94 set_dynamic_indirect( i915,
95 I915_DYNAMIC_MODES4,
96 &modes4,
97 1 );
98 }
99
100 const struct i915_tracked_state i915_upload_MODES4 = {
101 .dirty = I915_NEW_BLEND | I915_NEW_STENCIL,
102 .update = upload_MODES4
103 };
104
105
106
107
108 /***********************************************************************
109 */
110
111 static void upload_BFO( struct i915_context *i915 )
112 {
113 GLuint bf[2];
114
115 memset( bf, 0, sizeof(bf) );
116
117 /* _NEW_STENCIL
118 */
119 if (i915->stencil.back_enabled) {
120 GLint test = i915_translate_compare_func(i915->stencil.back_func);
121 GLint fop = i915_translate_stencil_op(i915->stencil.back_fail_op);
122 GLint dfop = i915_translate_stencil_op(i915->stencil.back_zfail_op);
123 GLint dpop = i915_translate_stencil_op(i915->stencil.back_zpass_op);
124 GLint ref = i915->stencil.ref_value[1] & 0xff;
125 GLint tmask = i915->stencil.value_mask[1] & 0xff;
126 GLint wmask = i915->stencil.write_mask[1] & 0xff;
127
128 bf[0] = (_3DSTATE_BACKFACE_STENCIL_OPS |
129 BFO_ENABLE_STENCIL_FUNCS |
130 BFO_ENABLE_STENCIL_TWO_SIDE |
131 BFO_ENABLE_STENCIL_REF |
132 BFO_STENCIL_TWO_SIDE |
133 (ref << BFO_STENCIL_REF_SHIFT) |
134 (test << BFO_STENCIL_TEST_SHIFT) |
135 (fop << BFO_STENCIL_FAIL_SHIFT) |
136 (dfop << BFO_STENCIL_PASS_Z_FAIL_SHIFT) |
137 (dpop << BFO_STENCIL_PASS_Z_PASS_SHIFT));
138
139 bf[1] = (_3DSTATE_BACKFACE_STENCIL_MASKS |
140 BFM_ENABLE_STENCIL_TEST_MASK |
141 BFM_ENABLE_STENCIL_WRITE_MASK |
142 (tmask << BFM_STENCIL_TEST_MASK_SHIFT) |
143 (wmask << BFM_STENCIL_WRITE_MASK_SHIFT));
144 }
145 else {
146 /* This actually disables two-side stencil: The bit set is a
147 * modify-enable bit to indicate we are changing the two-side
148 * setting. Then there is a symbolic zero to show that we are
149 * setting the flag to zero/off.
150 */
151 bf[0] = (_3DSTATE_BACKFACE_STENCIL_OPS |
152 BFO_ENABLE_STENCIL_TWO_SIDE |
153 0);
154 bf[1] = 0;
155 }
156
157 set_dynamic_indirect( i915,
158 I915_DYNAMIC_BFO_0,
159 &bf[0],
160 2 );
161 }
162
163 const struct i915_tracked_state i915_upload_BFO = {
164 .dirty = I915_NEW_STENCIL,
165 .update = upload_BFO
166 };
167
168
169 /***********************************************************************
170 */
171
172
173 static void upload_BLENDCOLOR( struct i915_context *i915 )
174 {
175 GLuint bc[2];
176
177 memset( bc, 0, sizeof(bc) );
178
179 /* I915_NEW_BLEND {_COLOR}
180 */
181 {
182 const GLfloat *color = i915->blend_color.color;
183 GLubyte r, g, b, a;
184
185 UNCLAMPED_FLOAT_TO_UBYTE(r, color[RCOMP]);
186 UNCLAMPED_FLOAT_TO_UBYTE(g, color[GCOMP]);
187 UNCLAMPED_FLOAT_TO_UBYTE(b, color[BCOMP]);
188 UNCLAMPED_FLOAT_TO_UBYTE(a, color[ACOMP]);
189
190 bc[0] = (_3DSTATE_CONST_BLEND_COLOR_CMD);
191 bc[1] = (a << 24) | (r << 16) | (g << 8) | b;
192 }
193
194 set_dynamic_indirect( i915,
195 I915_DYNAMIC_BC_0,
196 bc,
197 2 );
198 }
199
200 const struct i915_tracked_state i915_upload_BLENDCOLOR = {
201 .dirty = I915_NEW_BLEND,
202 .update = upload_BLENDCOLOR
203 };
204
205 /***********************************************************************
206 */
207
208
209 static void upload_IAB( struct i915_context *i915 )
210 {
211 GLuint iab = 0;
212
213 {
214 GLuint eqRGB = i915->blend.rgb_func;
215 GLuint srcRGB = i915->blend.rgb_src_factor;
216 GLuint dstRGB = i915->blend.rgb_dst_factor;
217
218 GLuint eqA = i915->blend.alpha_func;
219 GLuint srcA = i915->blend.alpha_src_factor;
220 GLuint dstA = i915->blend.alpha_dst_factor;
221
222 if (eqA == GL_MIN || eqA == GL_MAX) {
223 srcA = dstA = GL_ONE;
224 }
225
226 if (eqRGB == GL_MIN || eqRGB == GL_MAX) {
227 srcRGB = dstRGB = GL_ONE;
228 }
229
230 if (srcA != srcRGB ||
231 dstA != dstRGB ||
232 eqA != eqRGB) {
233
234 iab = (_3DSTATE_INDEPENDENT_ALPHA_BLEND_CMD |
235 IAB_MODIFY_ENABLE |
236 IAB_ENABLE |
237 IAB_MODIFY_FUNC |
238 IAB_MODIFY_SRC_FACTOR |
239 IAB_MODIFY_DST_FACTOR |
240 SRC_ABLND_FACT(i915_translate_blend_factor(srcA)) |
241 DST_ABLND_FACT(i915_translate_blend_factor(dstA)) |
242 (i915_translate_blend_func(eqA) << IAB_FUNC_SHIFT));
243 }
244 else {
245 iab = (_3DSTATE_INDEPENDENT_ALPHA_BLEND_CMD |
246 IAB_MODIFY_ENABLE |
247 0);
248 }
249 }
250
251
252 set_dynamic_indirect( i915,
253 I915_DYNAMIC_IAB,
254 &iab,
255 1 );
256 }
257
258 const struct i915_tracked_state i915_upload_IAB = {
259 .dirty = I915_NEW_BLEND,
260 .update = upload_IAB
261 };
262
263
264 /***********************************************************************
265 */
266
267
268
269 static void upload_DEPTHSCALE( struct i915_context *i915 )
270 {
271 union { GLfloat f; GLuint u; } ds[2];
272
273 memset( ds, 0, sizeof(ds) );
274
275 /* I915_NEW_SETUP
276 */
277 ds[0].u = _3DSTATE_DEPTH_OFFSET_SCALE;
278 ds[1].f = i915->setup.offset_scale;
279
280 set_dynamic_indirect( i915,
281 I915_DYNAMIC_DEPTHSCALE_0,
282 &ds[0].u,
283 2 );
284 }
285
286 const struct i915_tracked_state i915_upload_DEPTHSCALE = {
287 .dirty = I915_NEW_SETUP,
288 .update = upload_DEPTHSCALE
289 };
290
291
292
293 /***********************************************************************
294 * Polygon stipple
295 *
296 * The i915 supports a 4x4 stipple natively, GL wants 32x32.
297 * Fortunately stipple is usually a repeating pattern.
298 *
299 * XXX: does stipple pattern need to be adjusted according to
300 * the window position?
301 *
302 * XXX: possibly need workaround for conform paths test.
303 */
304
305 static void upload_STIPPLE( struct i915_context *i915 )
306 {
307 GLuint st[2];
308
309 st[0] = _3DSTATE_STIPPLE;
310 st[1] = 0;
311
312 /* I915_NEW_SETUP
313 */
314 if (i915->setup.poly_stipple_enable) {
315 st[1] |= ST1_ENABLE;
316 }
317
318
319 /* I915_NEW_STIPPLE
320 */
321 {
322 const GLubyte *mask = (const GLubyte *)i915->poly_stipple.stipple;
323 GLubyte p[4];
324
325 p[0] = mask[12] & 0xf;
326 p[1] = mask[8] & 0xf;
327 p[2] = mask[4] & 0xf;
328 p[3] = mask[0] & 0xf;
329
330 /* Not sure what to do about fallbacks, so for now just dont:
331 */
332 st[1] |= ((p[0] << 0) |
333 (p[1] << 4) |
334 (p[2] << 8) |
335 (p[3] << 12));
336 }
337
338
339 set_dynamic_indirect( i915,
340 I915_DYNAMIC_STP_0,
341 &st[0],
342 2 );
343 }
344
345
346 const struct i915_tracked_state i915_upload_STIPPLE = {
347 .dirty = I915_NEW_SETUP | I915_NEW_STIPPLE,
348 .update = upload_STIPPLE
349 };
350
351
352
353 /***********************************************************************
354 * Scissor.
355 */
356 static void upload_SCISSOR_ENABLE( struct i915_context *i915 )
357 {
358 unsigned sc[1];
359
360 if (i915->setup.scissor)
361 sc[0] = _3DSTATE_SCISSOR_ENABLE_CMD | ENABLE_SCISSOR_RECT;
362 else
363 sc[0] = _3DSTATE_SCISSOR_ENABLE_CMD | DISABLE_SCISSOR_RECT;
364
365 set_dynamic_indirect( i915,
366 I915_DYNAMIC_SC_ENA_0,
367 &sc[0],
368 1 );
369 }
370
371 const struct i915_tracked_state i915_upload_SCISSOR_ENABLE = {
372 .dirty = I915_NEW_SETUP,
373 .update = upload_SCISSOR_ENABLE
374 };
375
376
377
378 static void upload_SCISSOR_RECT( struct i915_context *i915 )
379 {
380 unsigned x1 = i915->scissor.minx;
381 unsigned y1 = i915->scissor.miny;
382 unsigned x2 = i915->scissor.maxx;
383 unsigned y2 = i915->scissor.maxy;
384 unsigned sc[3];
385
386 sc[0] = _3DSTATE_SCISSOR_RECT_0_CMD;
387 sc[1] = (y1 << 16) | (x1 & 0xffff);
388 sc[2] = (y2 << 16) | (x2 & 0xffff);
389
390 set_dynamic_indirect( i915,
391 I915_DYNAMIC_SC_RECT_0,
392 &sc[0],
393 3 );
394 }
395
396
397 const struct i915_tracked_state i915_upload_SCISSOR_RECT = {
398 .dirty = I915_NEW_SCISSOR,
399 .update = upload_SCISSOR_RECT
400 };
401
402
403
404
405
406
407 static const struct i915_tracked_state *atoms[] = {
408 &i915_upload_MODES4,
409 &i915_upload_BFO,
410 &i915_upload_BLENDCOLOR,
411 &i915_upload_IAB,
412 &i915_upload_DEPTHSCALE,
413 &i915_upload_STIPPLE,
414 &i915_upload_SCISSOR_ENABLE,
415 &i915_upload_SCISSOR_RECT
416 };
417
418 /* These will be dynamic indirect state commands, but for now just end
419 * up on the batch buffer with everything else.
420 */
421 void i915_update_dynamic( struct i915_context *i915 )
422 {
423 int i;
424
425 for (i = 0; i < Elements(atoms); i++)
426 if (i915->dirty & atoms[i]->dirty)
427 atoms[i]->update( i915 );
428 }
429