Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / mesa / drivers / dri / i965 / brw_misc_state.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
34 #include "intel_batchbuffer.h"
35 #include "intel_fbo.h"
36 #include "intel_regions.h"
37
38 #include "brw_context.h"
39 #include "brw_state.h"
40 #include "brw_defines.h"
41
42 /* Constant single cliprect for framebuffer object or DRI2 drawing */
43 static void upload_drawing_rect(struct brw_context *brw)
44 {
45    struct intel_context *intel = &brw->intel;
46    struct gl_context *ctx = &intel->ctx;
47
48    BEGIN_BATCH(4);
49    OUT_BATCH(_3DSTATE_DRAWRECT_INFO_I965);
50    OUT_BATCH(0); /* xmin, ymin */
51    OUT_BATCH(((ctx->DrawBuffer->Width - 1) & 0xffff) |
52             ((ctx->DrawBuffer->Height - 1) << 16));
53    OUT_BATCH(0);
54    ADVANCE_BATCH();
55 }
56
57 const struct brw_tracked_state brw_drawing_rect = {
58    .dirty = {
59       .mesa = _NEW_BUFFERS,
60       .brw = BRW_NEW_CONTEXT,
61       .cache = 0
62    },
63    .emit = upload_drawing_rect
64 };
65
66 /**
67  * Upload the binding table pointers, which point each stage's array of surface
68  * state pointers.
69  *
70  * The binding table pointers are relative to the surface state base address,
71  * which points at the batchbuffer containing the streamed batch state.
72  */
73 static void upload_binding_table_pointers(struct brw_context *brw)
74 {
75    struct intel_context *intel = &brw->intel;
76
77    BEGIN_BATCH(6);
78    OUT_BATCH(_3DSTATE_BINDING_TABLE_POINTERS << 16 | (6 - 2));
79    OUT_BATCH(brw->vs.bind_bo_offset);
80    OUT_BATCH(0); /* gs */
81    OUT_BATCH(0); /* clip */
82    OUT_BATCH(0); /* sf */
83    OUT_BATCH(brw->wm.bind_bo_offset);
84    ADVANCE_BATCH();
85 }
86
87 const struct brw_tracked_state brw_binding_table_pointers = {
88    .dirty = {
89       .mesa = 0,
90       .brw = (BRW_NEW_BATCH |
91               BRW_NEW_STATE_BASE_ADDRESS |
92               BRW_NEW_VS_BINDING_TABLE |
93               BRW_NEW_GS_BINDING_TABLE |
94               BRW_NEW_PS_BINDING_TABLE),
95       .cache = 0,
96    },
97    .emit = upload_binding_table_pointers,
98 };
99
100 /**
101  * Upload the binding table pointers, which point each stage's array of surface
102  * state pointers.
103  *
104  * The binding table pointers are relative to the surface state base address,
105  * which points at the batchbuffer containing the streamed batch state.
106  */
107 static void upload_gen6_binding_table_pointers(struct brw_context *brw)
108 {
109    struct intel_context *intel = &brw->intel;
110
111    BEGIN_BATCH(4);
112    OUT_BATCH(_3DSTATE_BINDING_TABLE_POINTERS << 16 |
113              GEN6_BINDING_TABLE_MODIFY_VS |
114              GEN6_BINDING_TABLE_MODIFY_GS |
115              GEN6_BINDING_TABLE_MODIFY_PS |
116              (4 - 2));
117    OUT_BATCH(brw->vs.bind_bo_offset); /* vs */
118    OUT_BATCH(0); /* gs */
119    OUT_BATCH(brw->wm.bind_bo_offset); /* wm/ps */
120    ADVANCE_BATCH();
121 }
122
123 const struct brw_tracked_state gen6_binding_table_pointers = {
124    .dirty = {
125       .mesa = 0,
126       .brw = (BRW_NEW_BATCH |
127               BRW_NEW_STATE_BASE_ADDRESS |
128               BRW_NEW_VS_BINDING_TABLE |
129               BRW_NEW_GS_BINDING_TABLE |
130               BRW_NEW_PS_BINDING_TABLE),
131       .cache = 0,
132    },
133    .emit = upload_gen6_binding_table_pointers,
134 };
135
136 /**
137  * Upload pointers to the per-stage state.
138  *
139  * The state pointers in this packet are all relative to the general state
140  * base address set by CMD_STATE_BASE_ADDRESS, which is 0.
141  */
142 static void upload_pipelined_state_pointers(struct brw_context *brw )
143 {
144    struct intel_context *intel = &brw->intel;
145
146    if (intel->gen == 5) {
147       /* Need to flush before changing clip max threads for errata. */
148       BEGIN_BATCH(1);
149       OUT_BATCH(MI_FLUSH);
150       ADVANCE_BATCH();
151    }
152
153    BEGIN_BATCH(7);
154    OUT_BATCH(_3DSTATE_PIPELINED_POINTERS << 16 | (7 - 2));
155    OUT_RELOC(intel->batch.bo, I915_GEM_DOMAIN_INSTRUCTION, 0,
156              brw->vs.state_offset);
157    if (brw->gs.prog_active)
158       OUT_RELOC(brw->intel.batch.bo, I915_GEM_DOMAIN_INSTRUCTION, 0,
159                 brw->gs.state_offset | 1);
160    else
161       OUT_BATCH(0);
162    OUT_RELOC(brw->intel.batch.bo, I915_GEM_DOMAIN_INSTRUCTION, 0,
163              brw->clip.state_offset | 1);
164    OUT_RELOC(brw->intel.batch.bo, I915_GEM_DOMAIN_INSTRUCTION, 0,
165              brw->sf.state_offset);
166    OUT_RELOC(brw->intel.batch.bo, I915_GEM_DOMAIN_INSTRUCTION, 0,
167              brw->wm.state_offset);
168    OUT_RELOC(brw->intel.batch.bo, I915_GEM_DOMAIN_INSTRUCTION, 0,
169              brw->cc.state_offset);
170    ADVANCE_BATCH();
171
172    brw->state.dirty.brw |= BRW_NEW_PSP;
173 }
174
175 static void upload_psp_urb_cbs(struct brw_context *brw )
176 {
177    upload_pipelined_state_pointers(brw);
178    brw_upload_urb_fence(brw);
179    brw_upload_cs_urb_state(brw);
180 }
181
182 const struct brw_tracked_state brw_psp_urb_cbs = {
183    .dirty = {
184       .mesa = 0,
185       .brw = (BRW_NEW_URB_FENCE |
186               BRW_NEW_BATCH |
187               BRW_NEW_STATE_BASE_ADDRESS),
188       .cache = (CACHE_NEW_VS_UNIT | 
189                 CACHE_NEW_GS_UNIT | 
190                 CACHE_NEW_GS_PROG | 
191                 CACHE_NEW_CLIP_UNIT | 
192                 CACHE_NEW_SF_UNIT | 
193                 CACHE_NEW_WM_UNIT | 
194                 CACHE_NEW_CC_UNIT)
195    },
196    .emit = upload_psp_urb_cbs,
197 };
198
199 static void prepare_depthbuffer(struct brw_context *brw)
200 {
201    struct intel_context *intel = &brw->intel;
202    struct gl_context *ctx = &intel->ctx;
203    struct gl_framebuffer *fb = ctx->DrawBuffer;
204    struct intel_renderbuffer *drb = intel_get_renderbuffer(fb, BUFFER_DEPTH);
205    struct intel_renderbuffer *srb = intel_get_renderbuffer(fb, BUFFER_STENCIL);
206
207    if (drb)
208       brw_add_validated_bo(brw, drb->region->buffer);
209    if (drb && drb->hiz_region)
210       brw_add_validated_bo(brw, drb->hiz_region->buffer);
211    if (srb)
212       brw_add_validated_bo(brw, srb->region->buffer);
213 }
214
215 static void emit_depthbuffer(struct brw_context *brw)
216 {
217    struct intel_context *intel = &brw->intel;
218    struct gl_context *ctx = &intel->ctx;
219    struct gl_framebuffer *fb = ctx->DrawBuffer;
220    /* _NEW_BUFFERS */
221    struct intel_renderbuffer *depth_irb = intel_get_renderbuffer(fb, BUFFER_DEPTH);
222    struct intel_renderbuffer *stencil_irb = intel_get_renderbuffer(fb, BUFFER_STENCIL);
223    struct intel_region *hiz_region = depth_irb ? depth_irb->hiz_region : NULL;
224    unsigned int len;
225
226    /* 3DSTATE_DEPTH_BUFFER, 3DSTATE_STENCIL_BUFFER are both
227     * non-pipelined state that will need the PIPE_CONTROL workaround.
228     */
229    if (intel->gen == 6) {
230       intel_emit_post_sync_nonzero_flush(intel);
231       intel_emit_depth_stall_flushes(intel);
232    }
233
234    /*
235     * If either depth or stencil buffer has packed depth/stencil format,
236     * then don't use separate stencil. Emit only a depth buffer.
237     */
238    if (depth_irb && depth_irb->Base.Format == MESA_FORMAT_S8_Z24) {
239       stencil_irb = NULL;
240    } else if (!depth_irb && stencil_irb
241               && stencil_irb->Base.Format == MESA_FORMAT_S8_Z24) {
242       depth_irb = stencil_irb;
243       stencil_irb = NULL;
244    }
245
246    if (intel->gen >= 6)
247       len = 7;
248    else if (intel->is_g4x || intel->gen == 5)
249       len = 6;
250    else
251       len = 5;
252
253    if (!depth_irb && !stencil_irb) {
254       BEGIN_BATCH(len);
255       OUT_BATCH(_3DSTATE_DEPTH_BUFFER << 16 | (len - 2));
256       OUT_BATCH((BRW_DEPTHFORMAT_D32_FLOAT << 18) |
257                 (BRW_SURFACE_NULL << 29));
258       OUT_BATCH(0);
259       OUT_BATCH(0);
260       OUT_BATCH(0);
261
262       if (intel->is_g4x || intel->gen >= 5)
263          OUT_BATCH(0);
264
265       if (intel->gen >= 6)
266          OUT_BATCH(0);
267
268       ADVANCE_BATCH();
269
270    } else if (!depth_irb && stencil_irb) {
271       /*
272        * There exists a separate stencil buffer but no depth buffer.
273        *
274        * The stencil buffer inherits most of its fields from
275        * 3DSTATE_DEPTH_BUFFER: namely the tile walk, surface type, width, and
276        * height.
277        *
278        * Since the stencil buffer has quirky pitch requirements, its region
279        * was allocated with half height and double cpp. So we need
280        * a multiplier of 2 to obtain the surface's real height.
281        *
282        * Enable the hiz bit because it and the separate stencil bit must have
283        * the same value. From Section 2.11.5.6.1.1 3DSTATE_DEPTH_BUFFER, Bit
284        * 1.21 "Separate Stencil Enable":
285        *     [DevIL]: If this field is enabled, Hierarchical Depth Buffer
286        *     Enable must also be enabled.
287        *
288        *     [DevGT]: This field must be set to the same value (enabled or
289        *     disabled) as Hierarchical Depth Buffer Enable
290        */
291       assert(intel->has_separate_stencil);
292       assert(stencil_irb->Base.Format == MESA_FORMAT_S8);
293
294       BEGIN_BATCH(len);
295       OUT_BATCH(_3DSTATE_DEPTH_BUFFER << 16 | (len - 2));
296       OUT_BATCH((BRW_DEPTHFORMAT_D32_FLOAT << 18) |
297                 (1 << 21) | /* separate stencil enable */
298                 (1 << 22) | /* hiz enable */
299                 (BRW_TILEWALK_YMAJOR << 26) |
300                 (BRW_SURFACE_2D << 29));
301       OUT_BATCH(0);
302       OUT_BATCH(((stencil_irb->region->width - 1) << 6) |
303                  (2 * stencil_irb->region->height - 1) << 19);
304       OUT_BATCH(0);
305       OUT_BATCH(0);
306
307       if (intel->gen >= 6)
308          OUT_BATCH(0);
309
310       ADVANCE_BATCH();
311
312    } else {
313       struct intel_region *region = depth_irb->region;
314       unsigned int format;
315       uint32_t tile_x, tile_y, offset;
316
317       /* If using separate stencil, hiz must be enabled. */
318       assert(!stencil_irb || hiz_region);
319
320       switch (region->cpp) {
321       case 2:
322          format = BRW_DEPTHFORMAT_D16_UNORM;
323          break;
324       case 4:
325          if (intel->depth_buffer_is_float)
326             format = BRW_DEPTHFORMAT_D32_FLOAT;
327          else if (hiz_region)
328             format = BRW_DEPTHFORMAT_D24_UNORM_X8_UINT;
329          else
330             format = BRW_DEPTHFORMAT_D24_UNORM_S8_UINT;
331          break;
332       default:
333          assert(0);
334          return;
335       }
336
337       offset = intel_renderbuffer_tile_offsets(depth_irb, &tile_x, &tile_y);
338
339       assert(intel->gen < 6 || region->tiling == I915_TILING_Y);
340       assert(!hiz_region || region->tiling == I915_TILING_Y);
341
342       BEGIN_BATCH(len);
343       OUT_BATCH(_3DSTATE_DEPTH_BUFFER << 16 | (len - 2));
344       OUT_BATCH(((region->pitch * region->cpp) - 1) |
345                 (format << 18) |
346                 ((hiz_region ? 1 : 0) << 21) | /* separate stencil enable */
347                 ((hiz_region ? 1 : 0) << 22) | /* hiz enable */
348                 (BRW_TILEWALK_YMAJOR << 26) |
349                 ((region->tiling != I915_TILING_NONE) << 27) |
350                 (BRW_SURFACE_2D << 29));
351       OUT_RELOC(region->buffer,
352                 I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
353                 offset);
354       OUT_BATCH((BRW_SURFACE_MIPMAPLAYOUT_BELOW << 1) |
355                 ((region->width - 1) << 6) |
356                 ((region->height - 1) << 19));
357       OUT_BATCH(0);
358
359       if (intel->is_g4x || intel->gen >= 5)
360          OUT_BATCH(tile_x | (tile_y << 16));
361       else
362          assert(tile_x == 0 && tile_y == 0);
363
364       if (intel->gen >= 6)
365          OUT_BATCH(0);
366
367       ADVANCE_BATCH();
368    }
369
370    if (hiz_region || stencil_irb) {
371       /*
372        * In the 3DSTATE_DEPTH_BUFFER batch emitted above, the 'separate
373        * stencil enable' and 'hiz enable' bits were set. Therefore we must
374        * emit 3DSTATE_HIER_DEPTH_BUFFER and 3DSTATE_STENCIL_BUFFER. Even if
375        * there is no stencil buffer, 3DSTATE_STENCIL_BUFFER must be emitted;
376        * failure to do so causes hangs on gen5 and a stall on gen6.
377        */
378
379       /* Emit hiz buffer. */
380       if (hiz_region) {
381          BEGIN_BATCH(3);
382          OUT_BATCH((_3DSTATE_HIER_DEPTH_BUFFER << 16) | (3 - 2));
383          OUT_BATCH(hiz_region->pitch * hiz_region->cpp - 1);
384          OUT_RELOC(hiz_region->buffer,
385                    I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
386                    0);
387          ADVANCE_BATCH();
388       } else {
389          BEGIN_BATCH(3);
390          OUT_BATCH((_3DSTATE_HIER_DEPTH_BUFFER << 16) | (3 - 2));
391          OUT_BATCH(0);
392          OUT_BATCH(0);
393          ADVANCE_BATCH();
394       }
395
396       /* Emit stencil buffer. */
397       if (stencil_irb) {
398          BEGIN_BATCH(3);
399          OUT_BATCH((_3DSTATE_STENCIL_BUFFER << 16) | (3 - 2));
400          OUT_BATCH(stencil_irb->region->pitch * stencil_irb->region->cpp - 1);
401          OUT_RELOC(stencil_irb->region->buffer,
402                    I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER,
403                    0);
404          ADVANCE_BATCH();
405       } else {
406          BEGIN_BATCH(3);
407          OUT_BATCH((_3DSTATE_STENCIL_BUFFER << 16) | (3 - 2));
408          OUT_BATCH(0);
409          OUT_BATCH(0);
410          ADVANCE_BATCH();
411       }
412    }
413
414    /*
415     * On Gen >= 6, emit clear params for safety. If using hiz, then clear
416     * params must be emitted.
417     *
418     * From Section 2.11.5.6.4.1 3DSTATE_CLEAR_PARAMS:
419     *     3DSTATE_CLEAR_PARAMS packet must follow the DEPTH_BUFFER_STATE packet
420     *     when HiZ is enabled and the DEPTH_BUFFER_STATE changes.
421     */
422    if (intel->gen >= 6 || hiz_region) {
423       if (intel->gen == 6)
424          intel_emit_post_sync_nonzero_flush(intel);
425
426       BEGIN_BATCH(2);
427       OUT_BATCH(_3DSTATE_CLEAR_PARAMS << 16 | (2 - 2));
428       OUT_BATCH(0);
429       ADVANCE_BATCH();
430    }
431 }
432
433 const struct brw_tracked_state brw_depthbuffer = {
434    .dirty = {
435       .mesa = _NEW_BUFFERS,
436       .brw = BRW_NEW_BATCH,
437       .cache = 0,
438    },
439    .prepare = prepare_depthbuffer,
440    .emit = emit_depthbuffer,
441 };
442
443
444
445 /***********************************************************************
446  * Polygon stipple packet
447  */
448
449 static void upload_polygon_stipple(struct brw_context *brw)
450 {
451    struct intel_context *intel = &brw->intel;
452    struct gl_context *ctx = &brw->intel.ctx;
453    GLuint i;
454
455    /* _NEW_POLYGON */
456    if (!ctx->Polygon.StippleFlag)
457       return;
458
459    if (intel->gen == 6)
460       intel_emit_post_sync_nonzero_flush(intel);
461
462    BEGIN_BATCH(33);
463    OUT_BATCH(_3DSTATE_POLY_STIPPLE_PATTERN << 16 | (33 - 2));
464
465    /* Polygon stipple is provided in OpenGL order, i.e. bottom
466     * row first.  If we're rendering to a window (i.e. the
467     * default frame buffer object, 0), then we need to invert
468     * it to match our pixel layout.  But if we're rendering
469     * to a FBO (i.e. any named frame buffer object), we *don't*
470     * need to invert - we already match the layout.
471     */
472    if (ctx->DrawBuffer->Name == 0) {
473       for (i = 0; i < 32; i++)
474           OUT_BATCH(ctx->PolygonStipple[31 - i]); /* invert */
475    }
476    else {
477       for (i = 0; i < 32; i++)
478          OUT_BATCH(ctx->PolygonStipple[i]);
479    }
480    CACHED_BATCH();
481 }
482
483 const struct brw_tracked_state brw_polygon_stipple = {
484    .dirty = {
485       .mesa = (_NEW_POLYGONSTIPPLE |
486                _NEW_POLYGON),
487       .brw = BRW_NEW_CONTEXT,
488       .cache = 0
489    },
490    .emit = upload_polygon_stipple
491 };
492
493
494 /***********************************************************************
495  * Polygon stipple offset packet
496  */
497
498 static void upload_polygon_stipple_offset(struct brw_context *brw)
499 {
500    struct intel_context *intel = &brw->intel;
501    struct gl_context *ctx = &brw->intel.ctx;
502
503    /* _NEW_POLYGON */
504    if (!ctx->Polygon.StippleFlag)
505       return;
506
507    if (intel->gen == 6)
508       intel_emit_post_sync_nonzero_flush(intel);
509
510    BEGIN_BATCH(2);
511    OUT_BATCH(_3DSTATE_POLY_STIPPLE_OFFSET << 16 | (2-2));
512
513    /* _NEW_BUFFERS
514     *
515     * If we're drawing to a system window (ctx->DrawBuffer->Name == 0),
516     * we have to invert the Y axis in order to match the OpenGL
517     * pixel coordinate system, and our offset must be matched
518     * to the window position.  If we're drawing to a FBO
519     * (ctx->DrawBuffer->Name != 0), then our native pixel coordinate
520     * system works just fine, and there's no window system to
521     * worry about.
522     */
523    if (brw->intel.ctx.DrawBuffer->Name == 0)
524       OUT_BATCH((32 - (ctx->DrawBuffer->Height & 31)) & 31);
525    else
526       OUT_BATCH(0);
527    CACHED_BATCH();
528 }
529
530 const struct brw_tracked_state brw_polygon_stipple_offset = {
531    .dirty = {
532       .mesa = (_NEW_BUFFERS |
533                _NEW_POLYGON),
534       .brw = BRW_NEW_CONTEXT,
535       .cache = 0
536    },
537    .emit = upload_polygon_stipple_offset
538 };
539
540 /**********************************************************************
541  * AA Line parameters
542  */
543 static void upload_aa_line_parameters(struct brw_context *brw)
544 {
545    struct intel_context *intel = &brw->intel;
546    struct gl_context *ctx = &brw->intel.ctx;
547
548    if (!ctx->Line.SmoothFlag || !brw->has_aa_line_parameters)
549       return;
550
551    if (intel->gen == 6)
552       intel_emit_post_sync_nonzero_flush(intel);
553
554    OUT_BATCH(_3DSTATE_AA_LINE_PARAMETERS << 16 | (3 - 2));
555    /* use legacy aa line coverage computation */
556    OUT_BATCH(0);
557    OUT_BATCH(0);
558    CACHED_BATCH();
559 }
560
561 const struct brw_tracked_state brw_aa_line_parameters = {
562    .dirty = {
563       .mesa = _NEW_LINE,
564       .brw = BRW_NEW_CONTEXT,
565       .cache = 0
566    },
567    .emit = upload_aa_line_parameters
568 };
569
570 /***********************************************************************
571  * Line stipple packet
572  */
573
574 static void upload_line_stipple(struct brw_context *brw)
575 {
576    struct intel_context *intel = &brw->intel;
577    struct gl_context *ctx = &brw->intel.ctx;
578    GLfloat tmp;
579    GLint tmpi;
580
581    if (!ctx->Line.StippleFlag)
582       return;
583
584    if (intel->gen == 6)
585       intel_emit_post_sync_nonzero_flush(intel);
586
587    BEGIN_BATCH(3);
588    OUT_BATCH(_3DSTATE_LINE_STIPPLE_PATTERN << 16 | (3 - 2));
589    OUT_BATCH(ctx->Line.StipplePattern);
590    tmp = 1.0 / (GLfloat) ctx->Line.StippleFactor;
591    tmpi = tmp * (1<<13);
592    OUT_BATCH(tmpi << 16 | ctx->Line.StippleFactor);
593    CACHED_BATCH();
594 }
595
596 const struct brw_tracked_state brw_line_stipple = {
597    .dirty = {
598       .mesa = _NEW_LINE,
599       .brw = BRW_NEW_CONTEXT,
600       .cache = 0
601    },
602    .emit = upload_line_stipple
603 };
604
605
606 /***********************************************************************
607  * Misc invarient state packets
608  */
609
610 static void upload_invarient_state( struct brw_context *brw )
611 {
612    struct intel_context *intel = &brw->intel;
613
614    /* 3DSTATE_SIP, 3DSTATE_MULTISAMPLE, etc. are nonpipelined. */
615    if (intel->gen == 6)
616       intel_emit_post_sync_nonzero_flush(intel);
617
618    {
619       /* 0x61040000  Pipeline Select */
620       /*     PipelineSelect            : 0 */
621       struct brw_pipeline_select ps;
622
623       memset(&ps, 0, sizeof(ps));
624       ps.header.opcode = brw->CMD_PIPELINE_SELECT;
625       ps.header.pipeline_select = 0;
626       BRW_BATCH_STRUCT(brw, &ps);
627    }
628
629    if (intel->gen < 6) {
630       struct brw_global_depth_offset_clamp gdo;
631       memset(&gdo, 0, sizeof(gdo));
632
633       /* Disable depth offset clamping. 
634        */
635       gdo.header.opcode = _3DSTATE_GLOBAL_DEPTH_OFFSET_CLAMP;
636       gdo.header.length = sizeof(gdo)/4 - 2;
637       gdo.depth_offset_clamp = 0.0;
638
639       BRW_BATCH_STRUCT(brw, &gdo);
640    }
641
642    if (intel->gen >= 6) {
643       int i;
644       int len = intel->gen >= 7 ? 4 : 3;
645
646       BEGIN_BATCH(len);
647       OUT_BATCH(_3DSTATE_MULTISAMPLE << 16 | (len - 2));
648       OUT_BATCH(MS_PIXEL_LOCATION_CENTER |
649                 MS_NUMSAMPLES_1);
650       OUT_BATCH(0); /* positions for 4/8-sample */
651       if (intel->gen >= 7)
652          OUT_BATCH(0);
653       ADVANCE_BATCH();
654
655       BEGIN_BATCH(2);
656       OUT_BATCH(_3DSTATE_SAMPLE_MASK << 16 | (2 - 2));
657       OUT_BATCH(1);
658       ADVANCE_BATCH();
659
660       if (intel->gen < 7) {
661          for (i = 0; i < 4; i++) {
662             BEGIN_BATCH(4);
663             OUT_BATCH(_3DSTATE_GS_SVB_INDEX << 16 | (4 - 2));
664             OUT_BATCH(i << SVB_INDEX_SHIFT);
665             OUT_BATCH(0);
666             OUT_BATCH(0xffffffff);
667             ADVANCE_BATCH();
668          }
669       }
670    }
671
672    /* 0x61020000  State Instruction Pointer */
673    {
674       struct brw_system_instruction_pointer sip;
675       memset(&sip, 0, sizeof(sip));
676
677       sip.header.opcode = CMD_STATE_INSN_POINTER;
678       sip.header.length = 0;
679       sip.bits0.pad = 0;
680       sip.bits0.system_instruction_pointer = 0;
681
682       BRW_BATCH_STRUCT(brw, &sip);
683    }
684
685
686    {
687       struct brw_vf_statistics vfs;
688       memset(&vfs, 0, sizeof(vfs));
689
690       vfs.opcode = brw->CMD_VF_STATISTICS;
691       if (unlikely(INTEL_DEBUG & DEBUG_STATS))
692          vfs.statistics_enable = 1; 
693
694       BRW_BATCH_STRUCT(brw, &vfs);
695    }
696 }
697
698 const struct brw_tracked_state brw_invarient_state = {
699    .dirty = {
700       .mesa = 0,
701       .brw = BRW_NEW_CONTEXT,
702       .cache = 0
703    },
704    .emit = upload_invarient_state
705 };
706
707 /**
708  * Define the base addresses which some state is referenced from.
709  *
710  * This allows us to avoid having to emit relocations for the objects,
711  * and is actually required for binding table pointers on gen6.
712  *
713  * Surface state base address covers binding table pointers and
714  * surface state objects, but not the surfaces that the surface state
715  * objects point to.
716  */
717 static void upload_state_base_address( struct brw_context *brw )
718 {
719    struct intel_context *intel = &brw->intel;
720
721    /* FINISHME: According to section 3.6.1 "STATE_BASE_ADDRESS" of
722     * vol1a of the G45 PRM, MI_FLUSH with the ISC invalidate should be
723     * programmed prior to STATE_BASE_ADDRESS.
724     *
725     * However, given that the instruction SBA (general state base
726     * address) on this chipset is always set to 0 across X and GL,
727     * maybe this isn't required for us in particular.
728     */
729
730    if (intel->gen >= 6) {
731       if (intel->gen == 6)
732          intel_emit_post_sync_nonzero_flush(intel);
733
734        BEGIN_BATCH(10);
735        OUT_BATCH(CMD_STATE_BASE_ADDRESS << 16 | (10 - 2));
736        /* General state base address: stateless DP read/write requests */
737        OUT_BATCH(1);
738        /* Surface state base address:
739         * BINDING_TABLE_STATE
740         * SURFACE_STATE
741         */
742        OUT_RELOC(intel->batch.bo, I915_GEM_DOMAIN_SAMPLER, 0, 1);
743         /* Dynamic state base address:
744          * SAMPLER_STATE
745          * SAMPLER_BORDER_COLOR_STATE
746          * CLIP, SF, WM/CC viewport state
747          * COLOR_CALC_STATE
748          * DEPTH_STENCIL_STATE
749          * BLEND_STATE
750          * Push constants (when INSTPM: CONSTANT_BUFFER Address Offset
751          * Disable is clear, which we rely on)
752          */
753        OUT_RELOC(intel->batch.bo, (I915_GEM_DOMAIN_RENDER |
754                                    I915_GEM_DOMAIN_INSTRUCTION), 0, 1);
755
756        OUT_BATCH(1); /* Indirect object base address: MEDIA_OBJECT data */
757        OUT_RELOC(brw->cache.bo, I915_GEM_DOMAIN_INSTRUCTION, 0,
758                  1); /* Instruction base address: shader kernels (incl. SIP) */
759
760        OUT_BATCH(1); /* General state upper bound */
761        OUT_BATCH(1); /* Dynamic state upper bound */
762        OUT_BATCH(1); /* Indirect object upper bound */
763        OUT_BATCH(1); /* Instruction access upper bound */
764        ADVANCE_BATCH();
765    } else if (intel->gen == 5) {
766        BEGIN_BATCH(8);
767        OUT_BATCH(CMD_STATE_BASE_ADDRESS << 16 | (8 - 2));
768        OUT_BATCH(1); /* General state base address */
769        OUT_RELOC(intel->batch.bo, I915_GEM_DOMAIN_SAMPLER, 0,
770                  1); /* Surface state base address */
771        OUT_BATCH(1); /* Indirect object base address */
772        OUT_RELOC(brw->cache.bo, I915_GEM_DOMAIN_INSTRUCTION, 0,
773                  1); /* Instruction base address */
774        OUT_BATCH(1); /* General state upper bound */
775        OUT_BATCH(1); /* Indirect object upper bound */
776        OUT_BATCH(1); /* Instruction access upper bound */
777        ADVANCE_BATCH();
778    } else {
779        BEGIN_BATCH(6);
780        OUT_BATCH(CMD_STATE_BASE_ADDRESS << 16 | (6 - 2));
781        OUT_BATCH(1); /* General state base address */
782        OUT_RELOC(intel->batch.bo, I915_GEM_DOMAIN_SAMPLER, 0,
783                  1); /* Surface state base address */
784        OUT_BATCH(1); /* Indirect object base address */
785        OUT_BATCH(1); /* General state upper bound */
786        OUT_BATCH(1); /* Indirect object upper bound */
787        ADVANCE_BATCH();
788    }
789
790    /* According to section 3.6.1 of VOL1 of the 965 PRM,
791     * STATE_BASE_ADDRESS updates require a reissue of:
792     *
793     * 3DSTATE_PIPELINE_POINTERS
794     * 3DSTATE_BINDING_TABLE_POINTERS
795     * MEDIA_STATE_POINTERS
796     *
797     * and this continues through Ironlake.  The Sandy Bridge PRM, vol
798     * 1 part 1 says that the folowing packets must be reissued:
799     *
800     * 3DSTATE_CC_POINTERS
801     * 3DSTATE_BINDING_TABLE_POINTERS
802     * 3DSTATE_SAMPLER_STATE_POINTERS
803     * 3DSTATE_VIEWPORT_STATE_POINTERS
804     * MEDIA_STATE_POINTERS
805     *
806     * Those are always reissued following SBA updates anyway (new
807     * batch time), except in the case of the program cache BO
808     * changing.  Having a separate state flag makes the sequence more
809     * obvious.
810     */
811
812    brw->state.dirty.brw |= BRW_NEW_STATE_BASE_ADDRESS;
813 }
814
815 const struct brw_tracked_state brw_state_base_address = {
816    .dirty = {
817       .mesa = 0,
818       .brw = (BRW_NEW_BATCH |
819               BRW_NEW_PROGRAM_CACHE),
820       .cache = 0,
821    },
822    .emit = upload_state_base_address
823 };