2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
4 develop this 3D driver.
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:
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.
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.
26 **********************************************************************/
29 * Keith Whitwell <keith@tungstengraphics.com>
34 #include "intel_batchbuffer.h"
35 #include "intel_regions.h"
37 #include "brw_context.h"
38 #include "brw_state.h"
39 #include "brw_defines.h"
45 /***********************************************************************
49 static void upload_blend_constant_color(struct brw_context
*brw
)
51 GLcontext
*ctx
= &brw
->intel
.ctx
;
52 struct brw_blend_constant_color bcc
;
54 memset(&bcc
, 0, sizeof(bcc
));
55 bcc
.header
.opcode
= CMD_BLEND_CONSTANT_COLOR
;
56 bcc
.header
.length
= sizeof(bcc
)/4-2;
57 bcc
.blend_constant_color
[0] = ctx
->Color
.BlendColor
[0];
58 bcc
.blend_constant_color
[1] = ctx
->Color
.BlendColor
[1];
59 bcc
.blend_constant_color
[2] = ctx
->Color
.BlendColor
[2];
60 bcc
.blend_constant_color
[3] = ctx
->Color
.BlendColor
[3];
62 BRW_CACHED_BATCH_STRUCT(brw
, &bcc
);
66 const struct brw_tracked_state brw_blend_constant_color
= {
69 .brw
= BRW_NEW_CONTEXT
,
72 .emit
= upload_blend_constant_color
75 /* Constant single cliprect for framebuffer object or DRI2 drawing */
76 static void upload_drawing_rect(struct brw_context
*brw
)
78 struct intel_context
*intel
= &brw
->intel
;
79 GLcontext
*ctx
= &intel
->ctx
;
82 OUT_BATCH(_3DSTATE_DRAWRECT_INFO_I965
);
83 OUT_BATCH(0); /* xmin, ymin */
84 OUT_BATCH(((ctx
->DrawBuffer
->Width
- 1) & 0xffff) |
85 ((ctx
->DrawBuffer
->Height
- 1) << 16));
90 const struct brw_tracked_state brw_drawing_rect
= {
93 .brw
= BRW_NEW_CONTEXT
,
96 .emit
= upload_drawing_rect
99 static void prepare_binding_table_pointers(struct brw_context
*brw
)
101 brw_add_validated_bo(brw
, brw
->vs
.bind_bo
);
102 brw_add_validated_bo(brw
, brw
->wm
.bind_bo
);
106 * Upload the binding table pointers, which point each stage's array of surface
109 * The binding table pointers are relative to the surface state base address,
112 static void upload_binding_table_pointers(struct brw_context
*brw
)
114 struct intel_context
*intel
= &brw
->intel
;
117 OUT_BATCH(CMD_BINDING_TABLE_PTRS
<< 16 | (6 - 2));
118 if (brw
->vs
.bind_bo
!= NULL
)
119 OUT_RELOC(brw
->vs
.bind_bo
, I915_GEM_DOMAIN_SAMPLER
, 0, 0); /* vs */
122 OUT_BATCH(0); /* gs */
123 OUT_BATCH(0); /* clip */
124 OUT_BATCH(0); /* sf */
125 OUT_RELOC(brw
->wm
.bind_bo
, I915_GEM_DOMAIN_SAMPLER
, 0, 0); /* wm/ps */
129 const struct brw_tracked_state brw_binding_table_pointers
= {
132 .brw
= BRW_NEW_BATCH
,
133 .cache
= CACHE_NEW_SURF_BIND
,
135 .prepare
= prepare_binding_table_pointers
,
136 .emit
= upload_binding_table_pointers
,
140 * Upload the binding table pointers, which point each stage's array of surface
143 * The binding table pointers are relative to the surface state base address,
146 static void upload_gen6_binding_table_pointers(struct brw_context
*brw
)
148 struct intel_context
*intel
= &brw
->intel
;
151 OUT_BATCH(CMD_BINDING_TABLE_PTRS
<< 16 |
152 GEN6_BINDING_TABLE_MODIFY_VS
|
153 GEN6_BINDING_TABLE_MODIFY_GS
|
154 GEN6_BINDING_TABLE_MODIFY_PS
|
156 if (brw
->vs
.bind_bo
!= NULL
)
157 OUT_RELOC(brw
->vs
.bind_bo
, I915_GEM_DOMAIN_SAMPLER
, 0, 0); /* vs */
160 OUT_BATCH(0); /* gs */
161 OUT_RELOC(brw
->wm
.bind_bo
, I915_GEM_DOMAIN_SAMPLER
, 0, 0); /* wm/ps */
165 const struct brw_tracked_state gen6_binding_table_pointers
= {
168 .brw
= BRW_NEW_BATCH
,
169 .cache
= CACHE_NEW_SURF_BIND
,
171 .prepare
= prepare_binding_table_pointers
,
172 .emit
= upload_gen6_binding_table_pointers
,
176 * Upload pointers to the per-stage state.
178 * The state pointers in this packet are all relative to the general state
179 * base address set by CMD_STATE_BASE_ADDRESS, which is 0.
181 static void upload_pipelined_state_pointers(struct brw_context
*brw
)
183 struct intel_context
*intel
= &brw
->intel
;
185 if (intel
->gen
== 5) {
186 /* Need to flush before changing clip max threads for errata. */
193 OUT_BATCH(CMD_PIPELINED_STATE_POINTERS
<< 16 | (7 - 2));
194 OUT_RELOC(brw
->vs
.state_bo
, I915_GEM_DOMAIN_INSTRUCTION
, 0, 0);
195 if (brw
->gs
.prog_active
)
196 OUT_RELOC(brw
->gs
.state_bo
, I915_GEM_DOMAIN_INSTRUCTION
, 0, 1);
199 OUT_RELOC(brw
->clip
.state_bo
, I915_GEM_DOMAIN_INSTRUCTION
, 0, 1);
200 OUT_RELOC(brw
->sf
.state_bo
, I915_GEM_DOMAIN_INSTRUCTION
, 0, 0);
201 OUT_RELOC(brw
->wm
.state_bo
, I915_GEM_DOMAIN_INSTRUCTION
, 0, 0);
202 OUT_RELOC(brw
->cc
.state_bo
, I915_GEM_DOMAIN_INSTRUCTION
, 0, 0);
205 brw
->state
.dirty
.brw
|= BRW_NEW_PSP
;
209 static void prepare_psp_urb_cbs(struct brw_context
*brw
)
211 brw_add_validated_bo(brw
, brw
->vs
.state_bo
);
212 brw_add_validated_bo(brw
, brw
->gs
.state_bo
);
213 brw_add_validated_bo(brw
, brw
->clip
.state_bo
);
214 brw_add_validated_bo(brw
, brw
->sf
.state_bo
);
215 brw_add_validated_bo(brw
, brw
->wm
.state_bo
);
216 brw_add_validated_bo(brw
, brw
->cc
.state_bo
);
219 static void upload_psp_urb_cbs(struct brw_context
*brw
)
221 upload_pipelined_state_pointers(brw
);
222 brw_upload_urb_fence(brw
);
223 brw_upload_cs_urb_state(brw
);
226 const struct brw_tracked_state brw_psp_urb_cbs
= {
229 .brw
= BRW_NEW_URB_FENCE
| BRW_NEW_BATCH
,
230 .cache
= (CACHE_NEW_VS_UNIT
|
233 CACHE_NEW_CLIP_UNIT
|
238 .prepare
= prepare_psp_urb_cbs
,
239 .emit
= upload_psp_urb_cbs
,
242 static void prepare_depthbuffer(struct brw_context
*brw
)
244 struct intel_region
*region
= brw
->state
.depth_region
;
247 brw_add_validated_bo(brw
, region
->buffer
);
250 static void emit_depthbuffer(struct brw_context
*brw
)
252 struct intel_context
*intel
= &brw
->intel
;
253 struct intel_region
*region
= brw
->state
.depth_region
;
258 else if (intel
->is_g4x
|| intel
->gen
== 5)
263 if (region
== NULL
) {
265 OUT_BATCH(CMD_DEPTH_BUFFER
<< 16 | (len
- 2));
266 OUT_BATCH((BRW_DEPTHFORMAT_D32_FLOAT
<< 18) |
267 (BRW_SURFACE_NULL
<< 29));
272 if (intel
->is_g4x
|| intel
->gen
>= 5)
282 switch (region
->cpp
) {
284 format
= BRW_DEPTHFORMAT_D16_UNORM
;
287 if (intel
->depth_buffer_is_float
)
288 format
= BRW_DEPTHFORMAT_D32_FLOAT
;
290 format
= BRW_DEPTHFORMAT_D24_UNORM_S8_UINT
;
297 assert(region
->tiling
!= I915_TILING_X
);
298 if (IS_GEN6(intel
->intelScreen
->deviceID
))
299 assert(region
->tiling
!= I915_TILING_NONE
);
302 OUT_BATCH(CMD_DEPTH_BUFFER
<< 16 | (len
- 2));
303 OUT_BATCH(((region
->pitch
* region
->cpp
) - 1) |
305 (BRW_TILEWALK_YMAJOR
<< 26) |
306 ((region
->tiling
!= I915_TILING_NONE
) << 27) |
307 (BRW_SURFACE_2D
<< 29));
308 OUT_RELOC(region
->buffer
,
309 I915_GEM_DOMAIN_RENDER
, I915_GEM_DOMAIN_RENDER
,
311 OUT_BATCH((BRW_SURFACE_MIPMAPLAYOUT_BELOW
<< 1) |
312 ((region
->pitch
- 1) << 6) |
313 ((region
->height
- 1) << 19));
316 if (intel
->is_g4x
|| intel
->gen
>= 5)
325 /* Initialize it for safety. */
326 if (intel
->gen
>= 6) {
328 OUT_BATCH(CMD_3D_CLEAR_PARAMS
<< 16 | (2 - 2));
334 const struct brw_tracked_state brw_depthbuffer
= {
337 .brw
= BRW_NEW_DEPTH_BUFFER
| BRW_NEW_BATCH
,
340 .prepare
= prepare_depthbuffer
,
341 .emit
= emit_depthbuffer
,
346 /***********************************************************************
347 * Polygon stipple packet
350 static void upload_polygon_stipple(struct brw_context
*brw
)
352 GLcontext
*ctx
= &brw
->intel
.ctx
;
353 struct brw_polygon_stipple bps
;
356 memset(&bps
, 0, sizeof(bps
));
357 bps
.header
.opcode
= CMD_POLY_STIPPLE_PATTERN
;
358 bps
.header
.length
= sizeof(bps
)/4-2;
360 /* Polygon stipple is provided in OpenGL order, i.e. bottom
361 * row first. If we're rendering to a window (i.e. the
362 * default frame buffer object, 0), then we need to invert
363 * it to match our pixel layout. But if we're rendering
364 * to a FBO (i.e. any named frame buffer object), we *don't*
365 * need to invert - we already match the layout.
367 if (ctx
->DrawBuffer
->Name
== 0) {
368 for (i
= 0; i
< 32; i
++)
369 bps
.stipple
[i
] = ctx
->PolygonStipple
[31 - i
]; /* invert */
372 for (i
= 0; i
< 32; i
++)
373 bps
.stipple
[i
] = ctx
->PolygonStipple
[i
]; /* don't invert */
376 BRW_CACHED_BATCH_STRUCT(brw
, &bps
);
379 const struct brw_tracked_state brw_polygon_stipple
= {
381 .mesa
= _NEW_POLYGONSTIPPLE
,
382 .brw
= BRW_NEW_CONTEXT
,
385 .emit
= upload_polygon_stipple
389 /***********************************************************************
390 * Polygon stipple offset packet
393 static void upload_polygon_stipple_offset(struct brw_context
*brw
)
395 GLcontext
*ctx
= &brw
->intel
.ctx
;
396 struct brw_polygon_stipple_offset bpso
;
398 memset(&bpso
, 0, sizeof(bpso
));
399 bpso
.header
.opcode
= CMD_POLY_STIPPLE_OFFSET
;
400 bpso
.header
.length
= sizeof(bpso
)/4-2;
402 /* If we're drawing to a system window (ctx->DrawBuffer->Name == 0),
403 * we have to invert the Y axis in order to match the OpenGL
404 * pixel coordinate system, and our offset must be matched
405 * to the window position. If we're drawing to a FBO
406 * (ctx->DrawBuffer->Name != 0), then our native pixel coordinate
407 * system works just fine, and there's no window system to
410 if (brw
->intel
.ctx
.DrawBuffer
->Name
== 0) {
411 bpso
.bits0
.x_offset
= 0;
412 bpso
.bits0
.y_offset
= (32 - (ctx
->DrawBuffer
->Height
& 31)) & 31;
415 bpso
.bits0
.y_offset
= 0;
416 bpso
.bits0
.x_offset
= 0;
419 BRW_CACHED_BATCH_STRUCT(brw
, &bpso
);
422 #define _NEW_WINDOW_POS 0x40000000
424 const struct brw_tracked_state brw_polygon_stipple_offset
= {
426 .mesa
= _NEW_WINDOW_POS
,
427 .brw
= BRW_NEW_CONTEXT
,
430 .emit
= upload_polygon_stipple_offset
433 /**********************************************************************
436 static void upload_aa_line_parameters(struct brw_context
*brw
)
438 struct brw_aa_line_parameters balp
;
440 if (!brw
->has_aa_line_parameters
)
443 /* use legacy aa line coverage computation */
444 memset(&balp
, 0, sizeof(balp
));
445 balp
.header
.opcode
= CMD_AA_LINE_PARAMETERS
;
446 balp
.header
.length
= sizeof(balp
) / 4 - 2;
448 BRW_CACHED_BATCH_STRUCT(brw
, &balp
);
451 const struct brw_tracked_state brw_aa_line_parameters
= {
454 .brw
= BRW_NEW_CONTEXT
,
457 .emit
= upload_aa_line_parameters
460 /***********************************************************************
461 * Line stipple packet
464 static void upload_line_stipple(struct brw_context
*brw
)
466 GLcontext
*ctx
= &brw
->intel
.ctx
;
467 struct brw_line_stipple bls
;
471 memset(&bls
, 0, sizeof(bls
));
472 bls
.header
.opcode
= CMD_LINE_STIPPLE_PATTERN
;
473 bls
.header
.length
= sizeof(bls
)/4 - 2;
475 bls
.bits0
.pattern
= ctx
->Line
.StipplePattern
;
476 bls
.bits1
.repeat_count
= ctx
->Line
.StippleFactor
;
478 tmp
= 1.0 / (GLfloat
) ctx
->Line
.StippleFactor
;
479 tmpi
= tmp
* (1<<13);
482 bls
.bits1
.inverse_repeat_count
= tmpi
;
484 BRW_CACHED_BATCH_STRUCT(brw
, &bls
);
487 const struct brw_tracked_state brw_line_stipple
= {
490 .brw
= BRW_NEW_CONTEXT
,
493 .emit
= upload_line_stipple
497 /***********************************************************************
498 * Misc invarient state packets
501 static void upload_invarient_state( struct brw_context
*brw
)
503 struct intel_context
*intel
= &brw
->intel
;
506 /* 0x61040000 Pipeline Select */
507 /* PipelineSelect : 0 */
508 struct brw_pipeline_select ps
;
510 memset(&ps
, 0, sizeof(ps
));
511 ps
.header
.opcode
= brw
->CMD_PIPELINE_SELECT
;
512 ps
.header
.pipeline_select
= 0;
513 BRW_BATCH_STRUCT(brw
, &ps
);
516 if (intel
->gen
< 6) {
517 struct brw_global_depth_offset_clamp gdo
;
518 memset(&gdo
, 0, sizeof(gdo
));
520 /* Disable depth offset clamping.
522 gdo
.header
.opcode
= CMD_GLOBAL_DEPTH_OFFSET_CLAMP
;
523 gdo
.header
.length
= sizeof(gdo
)/4 - 2;
524 gdo
.depth_offset_clamp
= 0.0;
526 BRW_BATCH_STRUCT(brw
, &gdo
);
529 if (intel
->gen
>= 6) {
532 intel_batchbuffer_emit_mi_flush(intel
->batch
);
535 OUT_BATCH(CMD_3D_MULTISAMPLE
<< 16 | (3 - 2));
536 OUT_BATCH(MS_PIXEL_LOCATION_CENTER
|
538 OUT_BATCH(0); /* positions for 4/8-sample */
542 OUT_BATCH(CMD_3D_SAMPLE_MASK
<< 16 | (2 - 2));
546 for (i
= 0; i
< 4; i
++) {
548 OUT_BATCH(CMD_GS_SVB_INDEX
<< 16 | (4 - 2));
549 OUT_BATCH(i
<< SVB_INDEX_SHIFT
);
551 OUT_BATCH(0xffffffff);
556 /* 0x61020000 State Instruction Pointer */
558 struct brw_system_instruction_pointer sip
;
559 memset(&sip
, 0, sizeof(sip
));
561 sip
.header
.opcode
= CMD_STATE_INSN_POINTER
;
562 sip
.header
.length
= 0;
564 sip
.bits0
.system_instruction_pointer
= 0;
565 BRW_BATCH_STRUCT(brw
, &sip
);
570 struct brw_vf_statistics vfs
;
571 memset(&vfs
, 0, sizeof(vfs
));
573 vfs
.opcode
= brw
->CMD_VF_STATISTICS
;
574 if (INTEL_DEBUG
& DEBUG_STATS
)
575 vfs
.statistics_enable
= 1;
577 BRW_BATCH_STRUCT(brw
, &vfs
);
581 const struct brw_tracked_state brw_invarient_state
= {
584 .brw
= BRW_NEW_CONTEXT
,
587 .emit
= upload_invarient_state
591 * Define the base addresses which some state is referenced from.
593 * This allows us to avoid having to emit relocations in many places for
594 * cached state, and instead emit pointers inside of large, mostly-static
595 * state pools. This comes at the expense of memory, and more expensive cache
598 static void upload_state_base_address( struct brw_context
*brw
)
600 struct intel_context
*intel
= &brw
->intel
;
602 /* Output the structure (brw_state_base_address) directly to the
603 * batchbuffer, so we can emit relocations inline.
605 if (intel
->gen
>= 6) {
607 OUT_BATCH(CMD_STATE_BASE_ADDRESS
<< 16 | (10 - 2));
608 OUT_BATCH(1); /* General state base address */
609 OUT_BATCH(1); /* Surface state base address */
610 OUT_BATCH(1); /* Dynamic state base address */
611 OUT_BATCH(1); /* Indirect object base address */
612 OUT_BATCH(1); /* Instruction base address */
613 OUT_BATCH(1); /* General state upper bound */
614 OUT_BATCH(1); /* Dynamic state upper bound */
615 OUT_BATCH(1); /* Indirect object upper bound */
616 OUT_BATCH(1); /* Instruction access upper bound */
618 } else if (intel
->gen
== 5) {
620 OUT_BATCH(CMD_STATE_BASE_ADDRESS
<< 16 | (8 - 2));
621 OUT_BATCH(1); /* General state base address */
622 OUT_BATCH(1); /* Surface state base address */
623 OUT_BATCH(1); /* Indirect object base address */
624 OUT_BATCH(1); /* Instruction base address */
625 OUT_BATCH(1); /* General state upper bound */
626 OUT_BATCH(1); /* Indirect object upper bound */
627 OUT_BATCH(1); /* Instruction access upper bound */
631 OUT_BATCH(CMD_STATE_BASE_ADDRESS
<< 16 | (6 - 2));
632 OUT_BATCH(1); /* General state base address */
633 OUT_BATCH(1); /* Surface state base address */
634 OUT_BATCH(1); /* Indirect object base address */
635 OUT_BATCH(1); /* General state upper bound */
636 OUT_BATCH(1); /* Indirect object upper bound */
641 const struct brw_tracked_state brw_state_base_address
= {
644 .brw
= BRW_NEW_CONTEXT
,
647 .emit
= upload_state_base_address