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"
34 #include "i915_resource.h"
36 #include "pipe/p_context.h"
37 #include "pipe/p_defines.h"
39 static unsigned translate_format( enum pipe_format format
)
42 case PIPE_FORMAT_B8G8R8A8_UNORM
:
43 return COLOR_BUF_ARGB8888
;
44 case PIPE_FORMAT_B5G6R5_UNORM
:
45 return COLOR_BUF_RGB565
;
52 static unsigned translate_depth_format( enum pipe_format zformat
)
55 case PIPE_FORMAT_Z24X8_UNORM
:
56 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
57 return DEPTH_FRMT_24_FIXED_8_OTHER
;
58 case PIPE_FORMAT_Z16_UNORM
:
59 return DEPTH_FRMT_16_FIXED
;
68 * Examine framebuffer state to determine width, height.
71 framebuffer_size(const struct pipe_framebuffer_state
*fb
,
72 uint
*width
, uint
*height
)
75 *width
= fb
->cbufs
[0]->width
;
76 *height
= fb
->cbufs
[0]->height
;
80 *width
= fb
->zsbuf
->width
;
81 *height
= fb
->zsbuf
->height
;
91 /* Push the state into the sarea and/or texture memory.
94 i915_emit_hardware_state(struct i915_context
*i915
)
96 /* XXX: there must be an easier way */
97 const unsigned dwords
= ( 14 +
101 2 + I915_TEX_UNITS
*3 +
102 2 + I915_TEX_UNITS
*3 +
103 2 + I915_MAX_CONSTANT
*4 +
105 i915
->current
.program_len
+
107 i915
->fs
->program_len
+
110 ) * 3/2; /* plus 50% margin */
111 const unsigned relocs
= ( I915_TEX_UNITS
+
113 ) * 3/2; /* plus 50% margin */
118 if (I915_DBG_ON(DBG_ATOMS
))
119 i915_dump_hardware_dirty(i915
, __FUNCTION__
);
121 if(!BEGIN_BATCH(dwords
, relocs
)) {
123 assert(BEGIN_BATCH(dwords
, relocs
));
126 save_ptr
= (uintptr_t)i915
->batch
->ptr
;
127 save_relocs
= i915
->batch
->relocs
;
129 /* 14 dwords, 0 relocs */
130 if (i915
->hardware_dirty
& I915_HW_INVARIENT
)
132 OUT_BATCH(_3DSTATE_AA_CMD
|
133 AA_LINE_ECAAR_WIDTH_ENABLE
|
134 AA_LINE_ECAAR_WIDTH_1_0
|
135 AA_LINE_REGION_WIDTH_ENABLE
| AA_LINE_REGION_WIDTH_1_0
);
137 OUT_BATCH(_3DSTATE_DFLT_DIFFUSE_CMD
);
140 OUT_BATCH(_3DSTATE_DFLT_SPEC_CMD
);
143 OUT_BATCH(_3DSTATE_DFLT_Z_CMD
);
146 OUT_BATCH(_3DSTATE_COORD_SET_BINDINGS
|
156 OUT_BATCH(_3DSTATE_RASTER_RULES_CMD
|
157 ENABLE_POINT_RASTER_RULE
|
158 OGL_POINT_RASTER_RULE
|
159 ENABLE_LINE_STRIP_PROVOKE_VRTX
|
160 ENABLE_TRI_FAN_PROVOKE_VRTX
|
161 LINE_STRIP_PROVOKE_VRTX(1) |
162 TRI_FAN_PROVOKE_VRTX(2) |
163 ENABLE_TEXKILL_3D_4D
|
166 /* Need to initialize this to zero.
168 OUT_BATCH(_3DSTATE_LOAD_STATE_IMMEDIATE_1
| I1_LOAD_S(3) | (0));
171 OUT_BATCH(_3DSTATE_DEPTH_SUBRECT_DISABLE
);
173 /* disable indirect state for now
175 OUT_BATCH(_3DSTATE_LOAD_INDIRECT
| 0);
179 /* 7 dwords, 1 relocs */
180 if (i915
->hardware_dirty
& I915_HW_IMMEDIATE
)
182 OUT_BATCH(_3DSTATE_LOAD_STATE_IMMEDIATE_1
|
194 i915
->current
.immediate
[I915_IMMEDIATE_S0
]);
196 /* FIXME: we should not do this */
198 OUT_BATCH(i915
->current
.immediate
[I915_IMMEDIATE_S1
]);
199 OUT_BATCH(i915
->current
.immediate
[I915_IMMEDIATE_S2
]);
200 OUT_BATCH(i915
->current
.immediate
[I915_IMMEDIATE_S4
]);
201 OUT_BATCH(i915
->current
.immediate
[I915_IMMEDIATE_S5
]);
202 OUT_BATCH(i915
->current
.immediate
[I915_IMMEDIATE_S6
]);
205 /* I915_MAX_DYNAMIC dwords, 0 relocs */
206 if (i915
->hardware_dirty
& I915_HW_DYNAMIC
)
209 for (i
= 0; i
< I915_MAX_DYNAMIC
; i
++) {
210 OUT_BATCH(i915
->current
.dynamic
[i
]);
214 /* 8 dwords, 2 relocs */
215 if (i915
->hardware_dirty
& I915_HW_STATIC
)
217 struct pipe_surface
*cbuf_surface
= i915
->framebuffer
.cbufs
[0];
218 struct pipe_surface
*depth_surface
= i915
->framebuffer
.zsbuf
;
221 unsigned ctile
= BUF_3D_USE_FENCE
;
222 struct i915_texture
*tex
= i915_texture(cbuf_surface
->texture
);
225 if (tex
&& tex
->sw_tiled
) {
226 ctile
= BUF_3D_TILED_SURFACE
;
229 OUT_BATCH(_3DSTATE_BUF_INFO_CMD
);
231 OUT_BATCH(BUF_3D_ID_COLOR_BACK
|
232 BUF_3D_PITCH(tex
->stride
) | /* pitch in bytes */
235 OUT_RELOC(tex
->buffer
,
237 cbuf_surface
->offset
);
240 /* What happens if no zbuf??
243 unsigned ztile
= BUF_3D_USE_FENCE
;
244 struct i915_texture
*tex
= i915_texture(depth_surface
->texture
);
247 if (tex
&& tex
->sw_tiled
) {
248 ztile
= BUF_3D_TILED_SURFACE
;
251 OUT_BATCH(_3DSTATE_BUF_INFO_CMD
);
254 OUT_BATCH(BUF_3D_ID_DEPTH
|
255 BUF_3D_PITCH(tex
->stride
) | /* pitch in bytes */
258 OUT_RELOC(tex
->buffer
,
260 depth_surface
->offset
);
264 unsigned cformat
, zformat
= 0;
267 cformat
= cbuf_surface
->format
;
269 cformat
= PIPE_FORMAT_B8G8R8A8_UNORM
; /* arbitrary */
270 cformat
= translate_format(cformat
);
273 zformat
= translate_depth_format( i915
->framebuffer
.zsbuf
->format
);
275 OUT_BATCH(_3DSTATE_DST_BUF_VARS_CMD
);
276 OUT_BATCH(DSTORG_HORT_BIAS(0x8) | /* .5 */
277 DSTORG_VERT_BIAS(0x8) | /* .5 */
279 TEX_DEFAULT_COLOR_OGL
|
287 /* 2 + I915_TEX_UNITS*3 dwords, I915_TEX_UNITS relocs */
288 if (i915
->hardware_dirty
& (I915_HW_MAP
| I915_HW_SAMPLER
))
290 const uint nr
= i915
->current
.sampler_enable_nr
;
292 const uint enabled
= i915
->current
.sampler_enable_flags
;
295 OUT_BATCH(_3DSTATE_MAP_STATE
| (3 * nr
));
297 for (unit
= 0; unit
< I915_TEX_UNITS
; unit
++) {
298 if (enabled
& (1 << unit
)) {
299 struct i915_texture
*texture
= i915_texture(i915
->fragment_sampler_views
[unit
]->texture
);
300 struct i915_winsys_buffer
*buf
= texture
->buffer
;
306 OUT_RELOC(buf
, I915_USAGE_SAMPLER
, offset
);
307 OUT_BATCH(i915
->current
.texbuffer
[unit
][0]); /* MS3 */
308 OUT_BATCH(i915
->current
.texbuffer
[unit
][1]); /* MS4 */
318 /* 2 + I915_TEX_UNITS*3 dwords, 0 relocs */
319 if (i915
->hardware_dirty
& I915_HW_SAMPLER
)
321 if (i915
->current
.sampler_enable_nr
) {
324 OUT_BATCH( _3DSTATE_SAMPLER_STATE
|
325 (3 * i915
->current
.sampler_enable_nr
) );
327 OUT_BATCH( i915
->current
.sampler_enable_flags
);
329 for (i
= 0; i
< I915_TEX_UNITS
; i
++) {
330 if (i915
->current
.sampler_enable_flags
& (1<<i
)) {
331 OUT_BATCH( i915
->current
.sampler
[i
][0] );
332 OUT_BATCH( i915
->current
.sampler
[i
][1] );
333 OUT_BATCH( i915
->current
.sampler
[i
][2] );
341 /* 2 + I915_MAX_CONSTANT*4 dwords, 0 relocs */
342 if (i915
->hardware_dirty
& I915_HW_PROGRAM
)
344 /* Collate the user-defined constants with the fragment shader's
345 * immediates according to the constant_flags[] array.
347 const uint nr
= i915
->fs
->num_constants
;
351 OUT_BATCH( _3DSTATE_PIXEL_SHADER_CONSTANTS
| (nr
* 4) );
352 OUT_BATCH( (1 << (nr
- 1)) | ((1 << (nr
- 1)) - 1) );
354 for (i
= 0; i
< nr
; i
++) {
356 if (i915
->fs
->constant_flags
[i
] == I915_CONSTFLAG_USER
) {
357 /* grab user-defined constant */
358 c
= (uint
*) i915
->current
.constants
[PIPE_SHADER_FRAGMENT
][i
];
361 /* emit program constant */
362 c
= (uint
*) i915
->fs
->constants
[i
];
366 float *f
= (float *) c
;
367 printf("Const %2d: %f %f %f %f %s\n", i
, f
[0], f
[1], f
[2], f
[3],
368 (i915
->fs
->constant_flags
[i
] == I915_CONSTFLAG_USER
369 ? "user" : "immediate"));
380 /* Fragment program */
381 /* i915->current.program_len dwords, 0 relocs */
382 if (i915
->hardware_dirty
& I915_HW_PROGRAM
)
385 /* we should always have, at least, a pass-through program */
386 assert(i915
->fs
->program_len
> 0);
387 for (i
= 0; i
< i915
->fs
->program_len
; i
++) {
388 OUT_BATCH(i915
->fs
->program
[i
]);
392 /* drawing surface size */
393 /* 6 dwords, 0 relocs */
396 boolean k
= framebuffer_size(&i915
->framebuffer
, &w
, &h
);
400 OUT_BATCH(_3DSTATE_DRAW_RECT_CMD
);
403 OUT_BATCH(((w
- 1) & 0xffff) | ((h
- 1) << 16));
408 I915_DBG(DBG_EMIT
, "%s: used %d dwords, %d relocs\n", __FUNCTION__
,
409 ((uintptr_t)i915
->batch
->ptr
- save_ptr
) / 4,
410 i915
->batch
->relocs
- save_relocs
);
412 i915
->hardware_dirty
= 0;