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_winsys.h"
32 #include "i915_batch.h"
35 #include "pipe/p_context.h"
36 #include "pipe/p_defines.h"
38 static unsigned translate_format( enum pipe_format format
)
41 case PIPE_FORMAT_A8R8G8B8_UNORM
:
42 return COLOR_BUF_ARGB8888
;
43 case PIPE_FORMAT_R5G6B5_UNORM
:
44 return COLOR_BUF_RGB565
;
51 static unsigned translate_depth_format( enum pipe_format zformat
)
54 case PIPE_FORMAT_S8Z24_UNORM
:
55 return DEPTH_FRMT_24_FIXED_8_OTHER
;
56 case PIPE_FORMAT_Z16_UNORM
:
57 return DEPTH_FRMT_16_FIXED
;
66 * Examine framebuffer state to determine width, height.
69 framebuffer_size(const struct pipe_framebuffer_state
*fb
,
70 uint
*width
, uint
*height
)
73 *width
= fb
->cbufs
[0]->width
;
74 *height
= fb
->cbufs
[0]->height
;
78 *width
= fb
->zsbuf
->width
;
79 *height
= fb
->zsbuf
->height
;
89 /* Push the state into the sarea and/or texture memory.
92 i915_emit_hardware_state(struct i915_context
*i915
)
94 /* XXX: there must be an easier way */
95 const unsigned dwords
= ( 14 +
99 2 + I915_TEX_UNITS
*3 +
100 2 + I915_TEX_UNITS
*3 +
101 2 + I915_MAX_CONSTANT
*4 +
103 i915
->current
.program_len
+
105 i915
->fs
->program_len
+
108 ) * 3/2; /* plus 50% margin */
109 const unsigned relocs
= ( I915_TEX_UNITS
+
111 ) * 3/2; /* plus 50% margin */
114 debug_printf("i915_emit_hardware_state: %d dwords, %d relocs\n", dwords
, relocs
);
117 if(!BEGIN_BATCH(dwords
, relocs
)) {
119 assert(BEGIN_BATCH(dwords
, relocs
));
122 /* 14 dwords, 0 relocs */
123 if (i915
->hardware_dirty
& I915_HW_INVARIENT
)
125 OUT_BATCH(_3DSTATE_AA_CMD
|
126 AA_LINE_ECAAR_WIDTH_ENABLE
|
127 AA_LINE_ECAAR_WIDTH_1_0
|
128 AA_LINE_REGION_WIDTH_ENABLE
| AA_LINE_REGION_WIDTH_1_0
);
130 OUT_BATCH(_3DSTATE_DFLT_DIFFUSE_CMD
);
133 OUT_BATCH(_3DSTATE_DFLT_SPEC_CMD
);
136 OUT_BATCH(_3DSTATE_DFLT_Z_CMD
);
139 OUT_BATCH(_3DSTATE_COORD_SET_BINDINGS
|
149 OUT_BATCH(_3DSTATE_RASTER_RULES_CMD
|
150 ENABLE_POINT_RASTER_RULE
|
151 OGL_POINT_RASTER_RULE
|
152 ENABLE_LINE_STRIP_PROVOKE_VRTX
|
153 ENABLE_TRI_FAN_PROVOKE_VRTX
|
154 LINE_STRIP_PROVOKE_VRTX(1) |
155 TRI_FAN_PROVOKE_VRTX(2) |
156 ENABLE_TEXKILL_3D_4D
|
159 /* Need to initialize this to zero.
161 OUT_BATCH(_3DSTATE_LOAD_STATE_IMMEDIATE_1
| I1_LOAD_S(3) | (0));
164 OUT_BATCH(_3DSTATE_DEPTH_SUBRECT_DISABLE
);
166 /* disable indirect state for now
168 OUT_BATCH(_3DSTATE_LOAD_INDIRECT
| 0);
172 /* 7 dwords, 1 relocs */
173 if (i915
->hardware_dirty
& I915_HW_IMMEDIATE
)
175 OUT_BATCH(_3DSTATE_LOAD_STATE_IMMEDIATE_1
|
186 I915_BUFFER_ACCESS_READ
,
187 i915
->current
.immediate
[I915_IMMEDIATE_S0
]);
189 /* FIXME: we should not do this */
191 OUT_BATCH(i915
->current
.immediate
[I915_IMMEDIATE_S1
]);
192 OUT_BATCH(i915
->current
.immediate
[I915_IMMEDIATE_S2
]);
193 OUT_BATCH(i915
->current
.immediate
[I915_IMMEDIATE_S4
]);
194 OUT_BATCH(i915
->current
.immediate
[I915_IMMEDIATE_S5
]);
195 OUT_BATCH(i915
->current
.immediate
[I915_IMMEDIATE_S6
]);
198 /* I915_MAX_DYNAMIC dwords, 0 relocs */
199 if (i915
->hardware_dirty
& I915_HW_DYNAMIC
)
202 for (i
= 0; i
< I915_MAX_DYNAMIC
; i
++) {
203 OUT_BATCH(i915
->current
.dynamic
[i
]);
207 /* 8 dwords, 2 relocs */
208 if (i915
->hardware_dirty
& I915_HW_STATIC
)
210 struct pipe_surface
*cbuf_surface
= i915
->framebuffer
.cbufs
[0];
211 struct pipe_surface
*depth_surface
= i915
->framebuffer
.zsbuf
;
214 unsigned pitch
= (cbuf_surface
->pitch
* cbuf_surface
->cpp
);
216 OUT_BATCH(_3DSTATE_BUF_INFO_CMD
);
218 OUT_BATCH(BUF_3D_ID_COLOR_BACK
|
219 BUF_3D_PITCH(pitch
) | /* pitch in bytes */
222 OUT_RELOC(cbuf_surface
->buffer
,
223 I915_BUFFER_ACCESS_WRITE
,
227 /* What happens if no zbuf??
230 unsigned zpitch
= (depth_surface
->pitch
* depth_surface
->cpp
);
232 OUT_BATCH(_3DSTATE_BUF_INFO_CMD
);
234 OUT_BATCH(BUF_3D_ID_DEPTH
|
235 BUF_3D_PITCH(zpitch
) | /* pitch in bytes */
238 OUT_RELOC(depth_surface
->buffer
,
239 I915_BUFFER_ACCESS_WRITE
,
244 unsigned cformat
, zformat
= 0;
247 cformat
= cbuf_surface
->format
;
249 cformat
= PIPE_FORMAT_A8R8G8B8_UNORM
; /* arbitrary */
250 cformat
= translate_format(cformat
);
253 zformat
= translate_depth_format( i915
->framebuffer
.zsbuf
->format
);
255 OUT_BATCH(_3DSTATE_DST_BUF_VARS_CMD
);
256 OUT_BATCH(DSTORG_HORT_BIAS(0x8) | /* .5 */
257 DSTORG_VERT_BIAS(0x8) | /* .5 */
259 TEX_DEFAULT_COLOR_OGL
|
267 /* 2 + I915_TEX_UNITS*3 dwords, I915_TEX_UNITS relocs */
268 if (i915
->hardware_dirty
& (I915_HW_MAP
| I915_HW_SAMPLER
))
270 /* XXX: we were refering to sampler state
271 * (current.sampler_enable_nr) below, but only checking
272 * I915_HW_MAP above. Should probably calculate the enabled
273 * flags separately - but there will be further rework of
274 * state so perhaps not necessary yet.
276 const uint nr
= i915
->current
.sampler_enable_nr
;
278 const uint enabled
= i915
->current
.sampler_enable_flags
;
281 OUT_BATCH(_3DSTATE_MAP_STATE
| (3 * nr
));
283 for (unit
= 0; unit
< I915_TEX_UNITS
; unit
++) {
284 if (enabled
& (1 << unit
)) {
285 struct pipe_buffer
*buf
=
286 i915
->texture
[unit
]->buffer
;
293 I915_BUFFER_ACCESS_READ
,
295 OUT_BATCH(i915
->current
.texbuffer
[unit
][0]); /* MS3 */
296 OUT_BATCH(i915
->current
.texbuffer
[unit
][1]); /* MS4 */
306 /* 2 + I915_TEX_UNITS*3 dwords, 0 relocs */
307 if (i915
->hardware_dirty
& I915_HW_SAMPLER
)
309 if (i915
->current
.sampler_enable_nr
) {
312 OUT_BATCH( _3DSTATE_SAMPLER_STATE
|
313 (3 * i915
->current
.sampler_enable_nr
) );
315 OUT_BATCH( i915
->current
.sampler_enable_flags
);
317 for (i
= 0; i
< I915_TEX_UNITS
; i
++) {
318 if (i915
->current
.sampler_enable_flags
& (1<<i
)) {
319 OUT_BATCH( i915
->current
.sampler
[i
][0] );
320 OUT_BATCH( i915
->current
.sampler
[i
][1] );
321 OUT_BATCH( i915
->current
.sampler
[i
][2] );
329 /* 2 + I915_MAX_CONSTANT*4 dwords, 0 relocs */
330 if (i915
->hardware_dirty
& I915_HW_PROGRAM
)
332 /* Collate the user-defined constants with the fragment shader's
333 * immediates according to the constant_flags[] array.
335 const uint nr
= i915
->fs
->num_constants
;
339 OUT_BATCH( _3DSTATE_PIXEL_SHADER_CONSTANTS
| (nr
* 4) );
340 OUT_BATCH( (1 << (nr
- 1)) | ((1 << (nr
- 1)) - 1) );
342 for (i
= 0; i
< nr
; i
++) {
344 if (i915
->fs
->constant_flags
[i
] == I915_CONSTFLAG_USER
) {
345 /* grab user-defined constant */
346 c
= (uint
*) i915
->current
.constants
[PIPE_SHADER_FRAGMENT
][i
];
349 /* emit program constant */
350 c
= (uint
*) i915
->fs
->constants
[i
];
354 float *f
= (float *) c
;
355 printf("Const %2d: %f %f %f %f %s\n", i
, f
[0], f
[1], f
[2], f
[3],
356 (i915
->fs
->constant_flags
[i
] == I915_CONSTFLAG_USER
357 ? "user" : "immediate"));
368 /* Fragment program */
369 /* i915->current.program_len dwords, 0 relocs */
370 if (i915
->hardware_dirty
& I915_HW_PROGRAM
)
373 /* we should always have, at least, a pass-through program */
374 assert(i915
->fs
->program_len
> 0);
375 for (i
= 0; i
< i915
->fs
->program_len
; i
++) {
376 OUT_BATCH(i915
->fs
->program
[i
]);
380 /* drawing surface size */
381 /* 6 dwords, 0 relocs */
384 boolean k
= framebuffer_size(&i915
->framebuffer
, &w
, &h
);
387 OUT_BATCH(_3DSTATE_DRAW_RECT_CMD
);
390 OUT_BATCH(((w
- 1) & 0xffff) | ((h
- 1) << 16));
396 i915
->hardware_dirty
= 0;