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"
41 * Examine framebuffer state to determine width, height.
44 framebuffer_size(const struct pipe_framebuffer_state
*fb
,
45 uint
*width
, uint
*height
)
48 *width
= fb
->cbufs
[0]->width
;
49 *height
= fb
->cbufs
[0]->height
;
53 *width
= fb
->zsbuf
->width
;
54 *height
= fb
->zsbuf
->height
;
64 /* Push the state into the sarea and/or texture memory.
67 i915_emit_hardware_state(struct i915_context
*i915
)
69 /* XXX: there must be an easier way */
70 const unsigned dwords
= ( 14 +
74 2 + I915_TEX_UNITS
*3 +
75 2 + I915_TEX_UNITS
*3 +
76 2 + I915_MAX_CONSTANT
*4 +
78 i915
->current
.program_len
+
80 i915
->fs
->program_len
+
83 ) * 3/2; /* plus 50% margin */
84 const unsigned relocs
= ( I915_TEX_UNITS
+
86 ) * 3/2; /* plus 50% margin */
91 if (I915_DBG_ON(DBG_ATOMS
))
92 i915_dump_hardware_dirty(i915
, __FUNCTION__
);
94 if(!BEGIN_BATCH(dwords
, relocs
)) {
96 assert(BEGIN_BATCH(dwords
, relocs
));
99 save_ptr
= (uintptr_t)i915
->batch
->ptr
;
100 save_relocs
= i915
->batch
->relocs
;
102 /* 14 dwords, 0 relocs */
103 if (i915
->hardware_dirty
& I915_HW_INVARIANT
)
105 OUT_BATCH(_3DSTATE_AA_CMD
|
106 AA_LINE_ECAAR_WIDTH_ENABLE
|
107 AA_LINE_ECAAR_WIDTH_1_0
|
108 AA_LINE_REGION_WIDTH_ENABLE
| AA_LINE_REGION_WIDTH_1_0
);
110 OUT_BATCH(_3DSTATE_DFLT_DIFFUSE_CMD
);
113 OUT_BATCH(_3DSTATE_DFLT_SPEC_CMD
);
116 OUT_BATCH(_3DSTATE_DFLT_Z_CMD
);
119 OUT_BATCH(_3DSTATE_COORD_SET_BINDINGS
|
129 OUT_BATCH(_3DSTATE_RASTER_RULES_CMD
|
130 ENABLE_POINT_RASTER_RULE
|
131 OGL_POINT_RASTER_RULE
|
132 ENABLE_LINE_STRIP_PROVOKE_VRTX
|
133 ENABLE_TRI_FAN_PROVOKE_VRTX
|
134 LINE_STRIP_PROVOKE_VRTX(1) |
135 TRI_FAN_PROVOKE_VRTX(2) |
136 ENABLE_TEXKILL_3D_4D
|
139 OUT_BATCH(_3DSTATE_DEPTH_SUBRECT_DISABLE
);
141 /* disable indirect state for now
143 OUT_BATCH(_3DSTATE_LOAD_INDIRECT
| 0);
147 /* 7 dwords, 1 relocs */
148 if (i915
->hardware_dirty
& I915_HW_IMMEDIATE
)
150 /* remove unwatned bits and S7 */
151 unsigned dirty
= (1 << I915_IMMEDIATE_S0
| 1 << I915_IMMEDIATE_S1
|
152 1 << I915_IMMEDIATE_S2
| 1 << I915_IMMEDIATE_S3
|
153 1 << I915_IMMEDIATE_S3
| 1 << I915_IMMEDIATE_S4
|
154 1 << I915_IMMEDIATE_S5
| 1 << I915_IMMEDIATE_S6
) &
155 i915
->immediate_dirty
;
156 int i
, num
= util_bitcount(dirty
);
157 assert(num
&& num
<= I915_MAX_IMMEDIATE
);
159 OUT_BATCH(_3DSTATE_LOAD_STATE_IMMEDIATE_1
|
160 dirty
<< 4 | (num
- 1));
162 if (i915
->immediate_dirty
& (1 << I915_IMMEDIATE_S0
)) {
164 OUT_RELOC(i915
->vbo
, I915_USAGE_VERTEX
,
165 i915
->current
.immediate
[I915_IMMEDIATE_S0
]);
170 for (i
= 1; i
< I915_MAX_IMMEDIATE
; i
++) {
171 if (dirty
& (1 << i
))
172 OUT_BATCH(i915
->current
.immediate
[i
]);
177 /* I915_MAX_DYNAMIC dwords, 0 relocs */
178 if (i915
->hardware_dirty
& I915_HW_DYNAMIC
)
181 for (i
= 0; i
< I915_MAX_DYNAMIC
; i
++) {
182 if (i915
->dynamic_dirty
& (1 << i
))
183 OUT_BATCH(i915
->current
.dynamic
[i
]);
189 /* 8 dwords, 2 relocs */
190 if (i915
->hardware_dirty
& I915_HW_STATIC
)
192 if (i915
->current
.cbuf_bo
) {
193 OUT_BATCH(_3DSTATE_BUF_INFO_CMD
);
194 OUT_BATCH(i915
->current
.cbuf_flags
);
195 OUT_RELOC(i915
->current
.cbuf_bo
,
200 /* What happens if no zbuf??
202 if (i915
->current
.depth_bo
) {
203 OUT_BATCH(_3DSTATE_BUF_INFO_CMD
);
204 OUT_BATCH(i915
->current
.depth_flags
);
205 OUT_RELOC(i915
->current
.depth_bo
,
211 OUT_BATCH(_3DSTATE_DST_BUF_VARS_CMD
);
212 OUT_BATCH(i915
->current
.dst_buf_vars
);
219 /* 2 + I915_TEX_UNITS*3 dwords, I915_TEX_UNITS relocs */
220 if (i915
->hardware_dirty
& (I915_HW_MAP
| I915_HW_SAMPLER
))
222 const uint nr
= i915
->current
.sampler_enable_nr
;
224 const uint enabled
= i915
->current
.sampler_enable_flags
;
227 OUT_BATCH(_3DSTATE_MAP_STATE
| (3 * nr
));
229 for (unit
= 0; unit
< I915_TEX_UNITS
; unit
++) {
230 if (enabled
& (1 << unit
)) {
231 struct i915_texture
*texture
= i915_texture(i915
->fragment_sampler_views
[unit
]->texture
);
232 struct i915_winsys_buffer
*buf
= texture
->buffer
;
237 OUT_RELOC(buf
, I915_USAGE_SAMPLER
, 0);
238 OUT_BATCH(i915
->current
.texbuffer
[unit
][0]); /* MS3 */
239 OUT_BATCH(i915
->current
.texbuffer
[unit
][1]); /* MS4 */
249 /* 2 + I915_TEX_UNITS*3 dwords, 0 relocs */
250 if (i915
->hardware_dirty
& I915_HW_SAMPLER
)
252 if (i915
->current
.sampler_enable_nr
) {
255 OUT_BATCH( _3DSTATE_SAMPLER_STATE
|
256 (3 * i915
->current
.sampler_enable_nr
) );
258 OUT_BATCH( i915
->current
.sampler_enable_flags
);
260 for (i
= 0; i
< I915_TEX_UNITS
; i
++) {
261 if (i915
->current
.sampler_enable_flags
& (1<<i
)) {
262 OUT_BATCH( i915
->current
.sampler
[i
][0] );
263 OUT_BATCH( i915
->current
.sampler
[i
][1] );
264 OUT_BATCH( i915
->current
.sampler
[i
][2] );
273 /* 2 + I915_MAX_CONSTANT*4 dwords, 0 relocs */
274 if (i915
->hardware_dirty
& I915_HW_CONSTANTS
)
276 /* Collate the user-defined constants with the fragment shader's
277 * immediates according to the constant_flags[] array.
279 const uint nr
= i915
->fs
->num_constants
;
283 OUT_BATCH( _3DSTATE_PIXEL_SHADER_CONSTANTS
| (nr
* 4) );
284 OUT_BATCH((1 << nr
) - 1);
286 for (i
= 0; i
< nr
; i
++) {
288 if (i915
->fs
->constant_flags
[i
] == I915_CONSTFLAG_USER
) {
289 /* grab user-defined constant */
290 c
= (uint
*) i915_buffer(i915
->constants
[PIPE_SHADER_FRAGMENT
])->data
;
294 /* emit program constant */
295 c
= (uint
*) i915
->fs
->constants
[i
];
299 float *f
= (float *) c
;
300 printf("Const %2d: %f %f %f %f %s\n", i
, f
[0], f
[1], f
[2], f
[3],
301 (i915
->fs
->constant_flags
[i
] == I915_CONSTFLAG_USER
302 ? "user" : "immediate"));
315 /* Fragment program */
316 /* i915->current.program_len dwords, 0 relocs */
317 if (i915
->hardware_dirty
& I915_HW_PROGRAM
)
320 /* we should always have, at least, a pass-through program */
321 assert(i915
->fs
->program_len
> 0);
322 for (i
= 0; i
< i915
->fs
->program_len
; i
++) {
323 OUT_BATCH(i915
->fs
->program
[i
]);
329 /* drawing surface size */
330 /* 6 dwords, 0 relocs */
331 if (i915
->hardware_dirty
& I915_HW_STATIC
)
334 struct pipe_surface
*cbuf_surface
= i915
->framebuffer
.cbufs
[0];
337 uint32_t draw_offset
;
340 ret
= framebuffer_size(&i915
->framebuffer
, &w
, &h
);
344 struct i915_texture
*tex
= i915_texture(cbuf_surface
->texture
);
345 layer
= cbuf_surface
->u
.tex
.first_layer
;
347 x
= tex
->image_offset
[cbuf_surface
->u
.tex
.level
][layer
].nblocksx
;
348 y
= tex
->image_offset
[cbuf_surface
->u
.tex
.level
][layer
].nblocksy
;
353 draw_offset
= x
| (y
<< 16);
355 /* XXX flush only required when the draw_offset changes! */
356 OUT_BATCH(MI_FLUSH
| INHIBIT_FLUSH_RENDER_CACHE
);
357 OUT_BATCH(_3DSTATE_DRAW_RECT_CMD
);
358 OUT_BATCH(DRAW_RECT_DIS_DEPTH_OFS
);
359 OUT_BATCH(draw_offset
);
360 OUT_BATCH((w
- 1 + x
) | ((h
- 1 + y
) << 16));
361 OUT_BATCH(draw_offset
);
365 I915_DBG(DBG_EMIT
, "%s: used %d dwords, %d relocs\n", __FUNCTION__
,
366 ((uintptr_t)i915
->batch
->ptr
- save_ptr
) / 4,
367 i915
->batch
->relocs
- save_relocs
);
369 i915
->hardware_dirty
= 0;
370 i915
->immediate_dirty
= 0;
371 i915
->dynamic_dirty
= 0;