Merge branch 'master' into pipe-video
[mesa.git] / src / gallium / drivers / i915 / i915_state_emit.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28
29 #include "i915_reg.h"
30 #include "i915_context.h"
31 #include "i915_batch.h"
32 #include "i915_debug.h"
33 #include "i915_resource.h"
34
35 #include "pipe/p_context.h"
36 #include "pipe/p_defines.h"
37
38 static unsigned translate_format( enum pipe_format format )
39 {
40 switch (format) {
41 case PIPE_FORMAT_B8G8R8A8_UNORM:
42 return COLOR_BUF_ARGB8888;
43 case PIPE_FORMAT_B5G6R5_UNORM:
44 return COLOR_BUF_RGB565;
45 default:
46 assert(0);
47 return 0;
48 }
49 }
50
51 static unsigned translate_depth_format( enum pipe_format zformat )
52 {
53 switch (zformat) {
54 case PIPE_FORMAT_Z24X8_UNORM:
55 case PIPE_FORMAT_Z24_UNORM_S8_USCALED:
56 return DEPTH_FRMT_24_FIXED_8_OTHER;
57 case PIPE_FORMAT_Z16_UNORM:
58 return DEPTH_FRMT_16_FIXED;
59 default:
60 assert(0);
61 return 0;
62 }
63 }
64
65
66 /**
67 * Examine framebuffer state to determine width, height.
68 */
69 static boolean
70 framebuffer_size(const struct pipe_framebuffer_state *fb,
71 uint *width, uint *height)
72 {
73 if (fb->cbufs[0]) {
74 *width = fb->cbufs[0]->width;
75 *height = fb->cbufs[0]->height;
76 return TRUE;
77 }
78 else if (fb->zsbuf) {
79 *width = fb->zsbuf->width;
80 *height = fb->zsbuf->height;
81 return TRUE;
82 }
83 else {
84 *width = *height = 0;
85 return FALSE;
86 }
87 }
88
89
90 /* Push the state into the sarea and/or texture memory.
91 */
92 void
93 i915_emit_hardware_state(struct i915_context *i915 )
94 {
95 /* XXX: there must be an easier way */
96 const unsigned dwords = ( 14 +
97 7 +
98 I915_MAX_DYNAMIC +
99 8 +
100 2 + I915_TEX_UNITS*3 +
101 2 + I915_TEX_UNITS*3 +
102 2 + I915_MAX_CONSTANT*4 +
103 #if 0
104 i915->current.program_len +
105 #else
106 i915->fs->program_len +
107 #endif
108 6
109 ) * 3/2; /* plus 50% margin */
110 const unsigned relocs = ( I915_TEX_UNITS +
111 3
112 ) * 3/2; /* plus 50% margin */
113
114 uintptr_t save_ptr;
115 size_t save_relocs;
116
117 if (I915_DBG_ON(DBG_ATOMS))
118 i915_dump_hardware_dirty(i915, __FUNCTION__);
119
120 if(!BEGIN_BATCH(dwords, relocs)) {
121 FLUSH_BATCH(NULL);
122 assert(BEGIN_BATCH(dwords, relocs));
123 }
124
125 save_ptr = (uintptr_t)i915->batch->ptr;
126 save_relocs = i915->batch->relocs;
127
128 /* 14 dwords, 0 relocs */
129 if (i915->hardware_dirty & I915_HW_INVARIENT)
130 {
131 OUT_BATCH(_3DSTATE_AA_CMD |
132 AA_LINE_ECAAR_WIDTH_ENABLE |
133 AA_LINE_ECAAR_WIDTH_1_0 |
134 AA_LINE_REGION_WIDTH_ENABLE | AA_LINE_REGION_WIDTH_1_0);
135
136 OUT_BATCH(_3DSTATE_DFLT_DIFFUSE_CMD);
137 OUT_BATCH(0);
138
139 OUT_BATCH(_3DSTATE_DFLT_SPEC_CMD);
140 OUT_BATCH(0);
141
142 OUT_BATCH(_3DSTATE_DFLT_Z_CMD);
143 OUT_BATCH(0);
144
145 OUT_BATCH(_3DSTATE_COORD_SET_BINDINGS |
146 CSB_TCB(0, 0) |
147 CSB_TCB(1, 1) |
148 CSB_TCB(2, 2) |
149 CSB_TCB(3, 3) |
150 CSB_TCB(4, 4) |
151 CSB_TCB(5, 5) |
152 CSB_TCB(6, 6) |
153 CSB_TCB(7, 7));
154
155 OUT_BATCH(_3DSTATE_RASTER_RULES_CMD |
156 ENABLE_POINT_RASTER_RULE |
157 OGL_POINT_RASTER_RULE |
158 ENABLE_LINE_STRIP_PROVOKE_VRTX |
159 ENABLE_TRI_FAN_PROVOKE_VRTX |
160 LINE_STRIP_PROVOKE_VRTX(1) |
161 TRI_FAN_PROVOKE_VRTX(2) |
162 ENABLE_TEXKILL_3D_4D |
163 TEXKILL_4D);
164
165 /* Need to initialize this to zero.
166 */
167 OUT_BATCH(_3DSTATE_LOAD_STATE_IMMEDIATE_1 | I1_LOAD_S(3) | (0));
168 OUT_BATCH(0);
169
170 OUT_BATCH(_3DSTATE_DEPTH_SUBRECT_DISABLE);
171
172 /* disable indirect state for now
173 */
174 OUT_BATCH(_3DSTATE_LOAD_INDIRECT | 0);
175 OUT_BATCH(0);
176 }
177
178 /* 7 dwords, 1 relocs */
179 if (i915->hardware_dirty & I915_HW_IMMEDIATE)
180 {
181 OUT_BATCH(_3DSTATE_LOAD_STATE_IMMEDIATE_1 |
182 I1_LOAD_S(0) |
183 I1_LOAD_S(1) |
184 I1_LOAD_S(2) |
185 I1_LOAD_S(4) |
186 I1_LOAD_S(5) |
187 I1_LOAD_S(6) |
188 (5));
189
190 if(i915->vbo)
191 OUT_RELOC(i915->vbo,
192 I915_USAGE_VERTEX,
193 i915->current.immediate[I915_IMMEDIATE_S0]);
194 else
195 /* FIXME: we should not do this */
196 OUT_BATCH(0);
197 OUT_BATCH(i915->current.immediate[I915_IMMEDIATE_S1]);
198 OUT_BATCH(i915->current.immediate[I915_IMMEDIATE_S2]);
199 OUT_BATCH(i915->current.immediate[I915_IMMEDIATE_S4]);
200 OUT_BATCH(i915->current.immediate[I915_IMMEDIATE_S5]);
201 OUT_BATCH(i915->current.immediate[I915_IMMEDIATE_S6]);
202 }
203
204 #if 01
205 /* I915_MAX_DYNAMIC dwords, 0 relocs */
206 if (i915->hardware_dirty & I915_HW_DYNAMIC)
207 {
208 int i;
209 for (i = 0; i < I915_MAX_DYNAMIC; i++) {
210 OUT_BATCH(i915->current.dynamic[i]);
211 }
212 }
213 #endif
214
215 #if 01
216 /* 8 dwords, 2 relocs */
217 if (i915->hardware_dirty & I915_HW_STATIC)
218 {
219 struct pipe_surface *cbuf_surface = i915->framebuffer.cbufs[0];
220 struct pipe_surface *depth_surface = i915->framebuffer.zsbuf;
221
222 if (cbuf_surface) {
223 unsigned ctile = BUF_3D_USE_FENCE;
224 struct i915_texture *tex = i915_texture(cbuf_surface->texture);
225 assert(tex);
226
227 if (tex && tex->sw_tiled) {
228 ctile = BUF_3D_TILED_SURFACE;
229 }
230
231 OUT_BATCH(_3DSTATE_BUF_INFO_CMD);
232
233 OUT_BATCH(BUF_3D_ID_COLOR_BACK |
234 BUF_3D_PITCH(tex->stride) | /* pitch in bytes */
235 ctile);
236
237 OUT_RELOC(tex->buffer,
238 I915_USAGE_RENDER,
239 cbuf_surface->offset);
240 }
241
242 /* What happens if no zbuf??
243 */
244 if (depth_surface) {
245 unsigned ztile = BUF_3D_USE_FENCE;
246 struct i915_texture *tex = i915_texture(depth_surface->texture);
247 assert(tex);
248
249 if (tex && tex->sw_tiled) {
250 ztile = BUF_3D_TILED_SURFACE;
251 }
252
253 OUT_BATCH(_3DSTATE_BUF_INFO_CMD);
254
255 assert(tex);
256 OUT_BATCH(BUF_3D_ID_DEPTH |
257 BUF_3D_PITCH(tex->stride) | /* pitch in bytes */
258 ztile);
259
260 OUT_RELOC(tex->buffer,
261 I915_USAGE_RENDER,
262 depth_surface->offset);
263 }
264
265 {
266 unsigned cformat, zformat = 0;
267
268 if (cbuf_surface)
269 cformat = cbuf_surface->format;
270 else
271 cformat = PIPE_FORMAT_B8G8R8A8_UNORM; /* arbitrary */
272 cformat = translate_format(cformat);
273
274 if (depth_surface)
275 zformat = translate_depth_format( i915->framebuffer.zsbuf->format );
276
277 OUT_BATCH(_3DSTATE_DST_BUF_VARS_CMD);
278 OUT_BATCH(DSTORG_HORT_BIAS(0x8) | /* .5 */
279 DSTORG_VERT_BIAS(0x8) | /* .5 */
280 LOD_PRECLAMP_OGL |
281 TEX_DEFAULT_COLOR_OGL |
282 cformat |
283 zformat );
284 }
285 }
286 #endif
287
288 #if 01
289 /* texture images */
290 /* 2 + I915_TEX_UNITS*3 dwords, I915_TEX_UNITS relocs */
291 if (i915->hardware_dirty & (I915_HW_MAP | I915_HW_SAMPLER))
292 {
293 const uint nr = i915->current.sampler_enable_nr;
294 if (nr) {
295 const uint enabled = i915->current.sampler_enable_flags;
296 uint unit;
297 uint count = 0;
298 OUT_BATCH(_3DSTATE_MAP_STATE | (3 * nr));
299 OUT_BATCH(enabled);
300 for (unit = 0; unit < I915_TEX_UNITS; unit++) {
301 if (enabled & (1 << unit)) {
302 struct i915_texture *texture = i915_texture(i915->fragment_sampler_views[unit]->texture);
303 struct i915_winsys_buffer *buf = texture->buffer;
304 uint offset = 0;
305 assert(buf);
306
307 count++;
308
309 OUT_RELOC(buf, I915_USAGE_SAMPLER, offset);
310 OUT_BATCH(i915->current.texbuffer[unit][0]); /* MS3 */
311 OUT_BATCH(i915->current.texbuffer[unit][1]); /* MS4 */
312 }
313 }
314 assert(count == nr);
315 }
316 }
317 #endif
318
319 #if 01
320 /* samplers */
321 /* 2 + I915_TEX_UNITS*3 dwords, 0 relocs */
322 if (i915->hardware_dirty & I915_HW_SAMPLER)
323 {
324 if (i915->current.sampler_enable_nr) {
325 int i;
326
327 OUT_BATCH( _3DSTATE_SAMPLER_STATE |
328 (3 * i915->current.sampler_enable_nr) );
329
330 OUT_BATCH( i915->current.sampler_enable_flags );
331
332 for (i = 0; i < I915_TEX_UNITS; i++) {
333 if (i915->current.sampler_enable_flags & (1<<i)) {
334 OUT_BATCH( i915->current.sampler[i][0] );
335 OUT_BATCH( i915->current.sampler[i][1] );
336 OUT_BATCH( i915->current.sampler[i][2] );
337 }
338 }
339 }
340 }
341 #endif
342
343 #if 01
344 /* constants */
345 /* 2 + I915_MAX_CONSTANT*4 dwords, 0 relocs */
346 if (i915->hardware_dirty & I915_HW_CONSTANTS)
347 {
348 /* Collate the user-defined constants with the fragment shader's
349 * immediates according to the constant_flags[] array.
350 */
351 const uint nr = i915->fs->num_constants;
352 if (nr) {
353 uint i;
354
355 OUT_BATCH( _3DSTATE_PIXEL_SHADER_CONSTANTS | (nr * 4) );
356 OUT_BATCH( (1 << (nr - 1)) | ((1 << (nr - 1)) - 1) );
357
358 for (i = 0; i < nr; i++) {
359 const uint *c;
360 if (i915->fs->constant_flags[i] == I915_CONSTFLAG_USER) {
361 /* grab user-defined constant */
362 c = (uint *) i915->current.constants[PIPE_SHADER_FRAGMENT][i];
363 }
364 else {
365 /* emit program constant */
366 c = (uint *) i915->fs->constants[i];
367 }
368 #if 0 /* debug */
369 {
370 float *f = (float *) c;
371 printf("Const %2d: %f %f %f %f %s\n", i, f[0], f[1], f[2], f[3],
372 (i915->fs->constant_flags[i] == I915_CONSTFLAG_USER
373 ? "user" : "immediate"));
374 }
375 #endif
376 OUT_BATCH(*c++);
377 OUT_BATCH(*c++);
378 OUT_BATCH(*c++);
379 OUT_BATCH(*c++);
380 }
381 }
382 }
383 #endif
384
385 #if 01
386 /* Fragment program */
387 /* i915->current.program_len dwords, 0 relocs */
388 if (i915->hardware_dirty & I915_HW_PROGRAM)
389 {
390 uint i;
391 /* we should always have, at least, a pass-through program */
392 assert(i915->fs->program_len > 0);
393 for (i = 0; i < i915->fs->program_len; i++) {
394 OUT_BATCH(i915->fs->program[i]);
395 }
396 }
397 #endif
398
399 #if 01
400 /* drawing surface size */
401 /* 6 dwords, 0 relocs */
402 {
403 uint w, h;
404 boolean k = framebuffer_size(&i915->framebuffer, &w, &h);
405 (void)k;
406 assert(k);
407
408 OUT_BATCH(_3DSTATE_DRAW_RECT_CMD);
409 OUT_BATCH(0);
410 OUT_BATCH(0);
411 OUT_BATCH(((w - 1) & 0xffff) | ((h - 1) << 16));
412 OUT_BATCH(0);
413 OUT_BATCH(0);
414 }
415 #endif
416
417 I915_DBG(DBG_EMIT, "%s: used %d dwords, %d relocs\n", __FUNCTION__,
418 ((uintptr_t)i915->batch->ptr - save_ptr) / 4,
419 i915->batch->relocs - save_relocs);
420
421 i915->hardware_dirty = 0;
422 }