1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 #include "i915_context.h"
31 #include "i915_batch.h"
32 #include "i915_debug.h"
33 #include "i915_resource.h"
35 #include "pipe/p_context.h"
36 #include "pipe/p_defines.h"
38 #include "util/u_math.h"
39 #include "util/u_memory.h"
41 struct i915_tracked_hw_state
{
43 void (*validate
)(struct i915_context
*, unsigned *batch_space
);
44 void (*emit
)(struct i915_context
*);
45 unsigned dirty
, batch_space
;
50 validate_flush(struct i915_context
*i915
, unsigned *batch_space
)
52 *batch_space
= i915
->flush_dirty
? 1 : 0;
56 emit_flush(struct i915_context
*i915
)
58 /* Cache handling is very cheap atm. State handling can request to flushes:
59 * - I915_FLUSH_CACHE which is a flush everything request and
60 * - I915_PIPELINE_FLUSH which is specifically for the draw_offset flush.
61 * Because the cache handling is so dumb, no explicit "invalidate map cache".
62 * Also, the first is a strict superset of the latter, so the following logic
64 if (i915
->flush_dirty
& I915_FLUSH_CACHE
)
65 OUT_BATCH(MI_FLUSH
| FLUSH_MAP_CACHE
);
66 else if (i915
->flush_dirty
& I915_PIPELINE_FLUSH
)
67 OUT_BATCH(MI_FLUSH
| INHIBIT_FLUSH_RENDER_CACHE
);
70 uint32_t invariant_state
[] = {
71 _3DSTATE_AA_CMD
| AA_LINE_ECAAR_WIDTH_ENABLE
| AA_LINE_ECAAR_WIDTH_1_0
|
72 AA_LINE_REGION_WIDTH_ENABLE
| AA_LINE_REGION_WIDTH_1_0
,
74 _3DSTATE_DFLT_DIFFUSE_CMD
, 0,
76 _3DSTATE_DFLT_SPEC_CMD
, 0,
78 _3DSTATE_DFLT_Z_CMD
, 0,
80 _3DSTATE_COORD_SET_BINDINGS
|
90 _3DSTATE_RASTER_RULES_CMD
|
91 ENABLE_POINT_RASTER_RULE
|
92 OGL_POINT_RASTER_RULE
|
93 ENABLE_LINE_STRIP_PROVOKE_VRTX
|
94 ENABLE_TRI_FAN_PROVOKE_VRTX
|
95 LINE_STRIP_PROVOKE_VRTX(1) |
96 TRI_FAN_PROVOKE_VRTX(2) |
97 ENABLE_TEXKILL_3D_4D
|
100 _3DSTATE_DEPTH_SUBRECT_DISABLE
,
102 /* disable indirect state for now
104 _3DSTATE_LOAD_INDIRECT
| 0, 0};
107 emit_invariant(struct i915_context
*i915
)
109 i915_winsys_batchbuffer_write(i915
->batch
, invariant_state
,
110 Elements(invariant_state
)*sizeof(uint32_t));
114 validate_immediate(struct i915_context
*i915
, unsigned *batch_space
)
116 unsigned dirty
= (1 << I915_IMMEDIATE_S0
| 1 << I915_IMMEDIATE_S1
|
117 1 << I915_IMMEDIATE_S2
| 1 << I915_IMMEDIATE_S3
|
118 1 << I915_IMMEDIATE_S3
| 1 << I915_IMMEDIATE_S4
|
119 1 << I915_IMMEDIATE_S5
| 1 << I915_IMMEDIATE_S6
) &
120 i915
->immediate_dirty
;
122 if (i915
->immediate_dirty
& (1 << I915_IMMEDIATE_S0
) && i915
->vbo
)
123 i915
->validation_buffers
[i915
->num_validation_buffers
++] = i915
->vbo
;
125 *batch_space
= 1 + util_bitcount(dirty
);
129 emit_immediate(struct i915_context
*i915
)
131 /* remove unwatned bits and S7 */
132 unsigned dirty
= (1 << I915_IMMEDIATE_S0
| 1 << I915_IMMEDIATE_S1
|
133 1 << I915_IMMEDIATE_S2
| 1 << I915_IMMEDIATE_S3
|
134 1 << I915_IMMEDIATE_S3
| 1 << I915_IMMEDIATE_S4
|
135 1 << I915_IMMEDIATE_S5
| 1 << I915_IMMEDIATE_S6
) &
136 i915
->immediate_dirty
;
137 int i
, num
= util_bitcount(dirty
);
138 assert(num
&& num
<= I915_MAX_IMMEDIATE
);
140 OUT_BATCH(_3DSTATE_LOAD_STATE_IMMEDIATE_1
|
141 dirty
<< 4 | (num
- 1));
143 if (i915
->immediate_dirty
& (1 << I915_IMMEDIATE_S0
)) {
145 OUT_RELOC(i915
->vbo
, I915_USAGE_VERTEX
,
146 i915
->current
.immediate
[I915_IMMEDIATE_S0
]);
151 for (i
= 1; i
< I915_MAX_IMMEDIATE
; i
++) {
152 if (dirty
& (1 << i
))
153 OUT_BATCH(i915
->current
.immediate
[i
]);
158 validate_dynamic(struct i915_context
*i915
, unsigned *batch_space
)
160 *batch_space
= util_bitcount(i915
->dynamic_dirty
& ((1 << I915_MAX_DYNAMIC
) - 1));
164 emit_dynamic(struct i915_context
*i915
)
167 for (i
= 0; i
< I915_MAX_DYNAMIC
; i
++) {
168 if (i915
->dynamic_dirty
& (1 << i
))
169 OUT_BATCH(i915
->current
.dynamic
[i
]);
174 validate_static(struct i915_context
*i915
, unsigned *batch_space
)
178 if (i915
->current
.cbuf_bo
&& (i915
->static_dirty
& I915_DST_BUF_COLOR
)) {
179 i915
->validation_buffers
[i915
->num_validation_buffers
++]
180 = i915
->current
.cbuf_bo
;
184 if (i915
->current
.depth_bo
&& (i915
->static_dirty
& I915_DST_BUF_DEPTH
)) {
185 i915
->validation_buffers
[i915
->num_validation_buffers
++]
186 = i915
->current
.depth_bo
;
190 if (i915
->static_dirty
& I915_DST_VARS
)
193 if (i915
->static_dirty
& I915_DST_RECT
)
198 emit_static(struct i915_context
*i915
)
200 if (i915
->current
.cbuf_bo
&& (i915
->static_dirty
& I915_DST_BUF_COLOR
)) {
201 OUT_BATCH(_3DSTATE_BUF_INFO_CMD
);
202 OUT_BATCH(i915
->current
.cbuf_flags
);
203 OUT_RELOC(i915
->current
.cbuf_bo
,
208 /* What happens if no zbuf??
210 if (i915
->current
.depth_bo
&& (i915
->static_dirty
& I915_DST_BUF_DEPTH
)) {
211 OUT_BATCH(_3DSTATE_BUF_INFO_CMD
);
212 OUT_BATCH(i915
->current
.depth_flags
);
213 OUT_RELOC(i915
->current
.depth_bo
,
218 if (i915
->static_dirty
& I915_DST_VARS
) {
219 OUT_BATCH(_3DSTATE_DST_BUF_VARS_CMD
);
220 OUT_BATCH(i915
->current
.dst_buf_vars
);
225 validate_map(struct i915_context
*i915
, unsigned *batch_space
)
227 const uint enabled
= i915
->current
.sampler_enable_flags
;
229 struct i915_texture
*tex
;
231 *batch_space
= i915
->current
.sampler_enable_nr
?
232 2 + 3*i915
->current
.sampler_enable_nr
: 0;
234 for (unit
= 0; unit
< I915_TEX_UNITS
; unit
++) {
235 if (enabled
& (1 << unit
)) {
236 tex
= i915_texture(i915
->fragment_sampler_views
[unit
]->texture
);
237 i915
->validation_buffers
[i915
->num_validation_buffers
++] = tex
->buffer
;
243 emit_map(struct i915_context
*i915
)
245 const uint nr
= i915
->current
.sampler_enable_nr
;
247 const uint enabled
= i915
->current
.sampler_enable_flags
;
250 OUT_BATCH(_3DSTATE_MAP_STATE
| (3 * nr
));
252 for (unit
= 0; unit
< I915_TEX_UNITS
; unit
++) {
253 if (enabled
& (1 << unit
)) {
254 struct i915_texture
*texture
= i915_texture(i915
->fragment_sampler_views
[unit
]->texture
);
255 struct i915_winsys_buffer
*buf
= texture
->buffer
;
260 OUT_RELOC(buf
, I915_USAGE_SAMPLER
, 0);
261 OUT_BATCH(i915
->current
.texbuffer
[unit
][0]); /* MS3 */
262 OUT_BATCH(i915
->current
.texbuffer
[unit
][1]); /* MS4 */
270 validate_sampler(struct i915_context
*i915
, unsigned *batch_space
)
272 *batch_space
= i915
->current
.sampler_enable_nr
?
273 2 + 3*i915
->current
.sampler_enable_nr
: 0;
277 emit_sampler(struct i915_context
*i915
)
279 if (i915
->current
.sampler_enable_nr
) {
282 OUT_BATCH( _3DSTATE_SAMPLER_STATE
|
283 (3 * i915
->current
.sampler_enable_nr
) );
285 OUT_BATCH( i915
->current
.sampler_enable_flags
);
287 for (i
= 0; i
< I915_TEX_UNITS
; i
++) {
288 if (i915
->current
.sampler_enable_flags
& (1<<i
)) {
289 OUT_BATCH( i915
->current
.sampler
[i
][0] );
290 OUT_BATCH( i915
->current
.sampler
[i
][1] );
291 OUT_BATCH( i915
->current
.sampler
[i
][2] );
298 validate_constants(struct i915_context
*i915
, unsigned *batch_space
)
300 *batch_space
= i915
->fs
->num_constants
?
301 2 + 4*i915
->fs
->num_constants
: 0;
305 emit_constants(struct i915_context
*i915
)
307 /* Collate the user-defined constants with the fragment shader's
308 * immediates according to the constant_flags[] array.
310 const uint nr
= i915
->fs
->num_constants
;
314 OUT_BATCH( _3DSTATE_PIXEL_SHADER_CONSTANTS
| (nr
* 4) );
315 OUT_BATCH((1 << nr
) - 1);
317 for (i
= 0; i
< nr
; i
++) {
319 if (i915
->fs
->constant_flags
[i
] == I915_CONSTFLAG_USER
) {
320 /* grab user-defined constant */
321 c
= (uint
*) i915_buffer(i915
->constants
[PIPE_SHADER_FRAGMENT
])->data
;
325 /* emit program constant */
326 c
= (uint
*) i915
->fs
->constants
[i
];
330 float *f
= (float *) c
;
331 printf("Const %2d: %f %f %f %f %s\n", i
, f
[0], f
[1], f
[2], f
[3],
332 (i915
->fs
->constant_flags
[i
] == I915_CONSTFLAG_USER
333 ? "user" : "immediate"));
345 validate_program(struct i915_context
*i915
, unsigned *batch_space
)
347 *batch_space
= i915
->fs
->program_len
;
351 emit_program(struct i915_context
*i915
)
354 /* we should always have, at least, a pass-through program */
355 assert(i915
->fs
->program_len
> 0);
356 for (i
= 0; i
< i915
->fs
->program_len
; i
++) {
357 OUT_BATCH(i915
->fs
->program
[i
]);
362 emit_draw_rect(struct i915_context
*i915
)
364 if (i915
->static_dirty
& I915_DST_RECT
) {
365 OUT_BATCH(_3DSTATE_DRAW_RECT_CMD
);
366 OUT_BATCH(DRAW_RECT_DIS_DEPTH_OFS
);
367 OUT_BATCH(i915
->current
.draw_offset
);
368 OUT_BATCH(i915
->current
.draw_size
);
369 OUT_BATCH(i915
->current
.draw_offset
);
374 i915_validate_state(struct i915_context
*i915
, unsigned *batch_space
)
378 i915
->num_validation_buffers
= 0;
379 if (i915
->hardware_dirty
& I915_HW_INVARIANT
)
380 *batch_space
= Elements(invariant_state
);
384 #define VALIDATE_ATOM(atom, hw_dirty) \
385 if (i915->hardware_dirty & hw_dirty) { \
386 validate_##atom(i915, &tmp); \
387 *batch_space += tmp; }
388 VALIDATE_ATOM(flush
, I915_HW_FLUSH
);
389 VALIDATE_ATOM(immediate
, I915_HW_IMMEDIATE
);
390 VALIDATE_ATOM(dynamic
, I915_HW_DYNAMIC
);
391 VALIDATE_ATOM(static, I915_HW_STATIC
);
392 VALIDATE_ATOM(map
, I915_HW_MAP
);
393 VALIDATE_ATOM(sampler
, I915_HW_SAMPLER
);
394 VALIDATE_ATOM(constants
, I915_HW_CONSTANTS
);
395 VALIDATE_ATOM(program
, I915_HW_PROGRAM
);
398 if (i915
->num_validation_buffers
== 0)
401 if (!i915_winsys_validate_buffers(i915
->batch
, i915
->validation_buffers
,
402 i915
->num_validation_buffers
))
408 /* Push the state into the sarea and/or texture memory.
411 i915_emit_hardware_state(struct i915_context
*i915
)
413 unsigned batch_space
;
416 assert(i915
->dirty
== 0);
418 if (I915_DBG_ON(DBG_ATOMS
))
419 i915_dump_hardware_dirty(i915
, __FUNCTION__
);
421 if (!i915_validate_state(i915
, &batch_space
)) {
423 assert(i915_validate_state(i915
, &batch_space
));
426 if(!BEGIN_BATCH(batch_space
)) {
428 assert(i915_validate_state(i915
, &batch_space
));
429 assert(BEGIN_BATCH(batch_space
));
432 save_ptr
= (uintptr_t)i915
->batch
->ptr
;
434 #define EMIT_ATOM(atom, hw_dirty) \
435 if (i915->hardware_dirty & hw_dirty) \
437 EMIT_ATOM(flush
, I915_HW_FLUSH
);
438 EMIT_ATOM(invariant
, I915_HW_INVARIANT
);
439 EMIT_ATOM(immediate
, I915_HW_IMMEDIATE
);
440 EMIT_ATOM(dynamic
, I915_HW_DYNAMIC
);
441 EMIT_ATOM(static, I915_HW_STATIC
);
442 EMIT_ATOM(map
, I915_HW_MAP
);
443 EMIT_ATOM(sampler
, I915_HW_SAMPLER
);
444 EMIT_ATOM(constants
, I915_HW_CONSTANTS
);
445 EMIT_ATOM(program
, I915_HW_PROGRAM
);
446 EMIT_ATOM(draw_rect
, I915_HW_STATIC
);
449 I915_DBG(DBG_EMIT
, "%s: used %d dwords, %d dwords reserved\n", __FUNCTION__
,
450 ((uintptr_t)i915
->batch
->ptr
- save_ptr
) / 4,
452 assert(((uintptr_t)i915
->batch
->ptr
- save_ptr
) / 4 == batch_space
);
454 i915
->hardware_dirty
= 0;
455 i915
->immediate_dirty
= 0;
456 i915
->dynamic_dirty
= 0;
457 i915
->static_dirty
= 0;
458 i915
->flush_dirty
= 0;