Merge branch 'gallium-polygon-stipple'
[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 #include "pipe/p_format.h"
38
39 #include "util/u_format.h"
40 #include "util/u_math.h"
41 #include "util/u_memory.h"
42
43 struct i915_tracked_hw_state {
44 const char *name;
45 void (*validate)(struct i915_context *, unsigned *batch_space);
46 void (*emit)(struct i915_context *);
47 unsigned dirty, batch_space;
48 };
49
50
51 static void
52 validate_flush(struct i915_context *i915, unsigned *batch_space)
53 {
54 *batch_space = i915->flush_dirty ? 1 : 0;
55 }
56
57 static void
58 emit_flush(struct i915_context *i915)
59 {
60 /* Cache handling is very cheap atm. State handling can request to flushes:
61 * - I915_FLUSH_CACHE which is a flush everything request and
62 * - I915_PIPELINE_FLUSH which is specifically for the draw_offset flush.
63 * Because the cache handling is so dumb, no explicit "invalidate map cache".
64 * Also, the first is a strict superset of the latter, so the following logic
65 * works. */
66 if (i915->flush_dirty & I915_FLUSH_CACHE)
67 OUT_BATCH(MI_FLUSH | FLUSH_MAP_CACHE);
68 else if (i915->flush_dirty & I915_PIPELINE_FLUSH)
69 OUT_BATCH(MI_FLUSH | INHIBIT_FLUSH_RENDER_CACHE);
70 }
71
72 uint32_t invariant_state[] = {
73 _3DSTATE_AA_CMD | AA_LINE_ECAAR_WIDTH_ENABLE | AA_LINE_ECAAR_WIDTH_1_0 |
74 AA_LINE_REGION_WIDTH_ENABLE | AA_LINE_REGION_WIDTH_1_0,
75
76 _3DSTATE_DFLT_DIFFUSE_CMD, 0,
77
78 _3DSTATE_DFLT_SPEC_CMD, 0,
79
80 _3DSTATE_DFLT_Z_CMD, 0,
81
82 _3DSTATE_COORD_SET_BINDINGS |
83 CSB_TCB(0, 0) |
84 CSB_TCB(1, 1) |
85 CSB_TCB(2, 2) |
86 CSB_TCB(3, 3) |
87 CSB_TCB(4, 4) |
88 CSB_TCB(5, 5) |
89 CSB_TCB(6, 6) |
90 CSB_TCB(7, 7),
91
92 _3DSTATE_RASTER_RULES_CMD |
93 ENABLE_POINT_RASTER_RULE |
94 OGL_POINT_RASTER_RULE |
95 ENABLE_LINE_STRIP_PROVOKE_VRTX |
96 ENABLE_TRI_FAN_PROVOKE_VRTX |
97 LINE_STRIP_PROVOKE_VRTX(1) |
98 TRI_FAN_PROVOKE_VRTX(2) |
99 ENABLE_TEXKILL_3D_4D |
100 TEXKILL_4D,
101
102 _3DSTATE_DEPTH_SUBRECT_DISABLE,
103
104 /* disable indirect state for now
105 */
106 _3DSTATE_LOAD_INDIRECT | 0, 0};
107
108 static void
109 emit_invariant(struct i915_context *i915)
110 {
111 i915_winsys_batchbuffer_write(i915->batch, invariant_state,
112 Elements(invariant_state)*sizeof(uint32_t));
113 }
114
115 static void
116 validate_immediate(struct i915_context *i915, unsigned *batch_space)
117 {
118 unsigned dirty = (1 << I915_IMMEDIATE_S0 | 1 << I915_IMMEDIATE_S1 |
119 1 << I915_IMMEDIATE_S2 | 1 << I915_IMMEDIATE_S3 |
120 1 << I915_IMMEDIATE_S3 | 1 << I915_IMMEDIATE_S4 |
121 1 << I915_IMMEDIATE_S5 | 1 << I915_IMMEDIATE_S6) &
122 i915->immediate_dirty;
123
124 if (i915->immediate_dirty & (1 << I915_IMMEDIATE_S0) && i915->vbo)
125 i915->validation_buffers[i915->num_validation_buffers++] = i915->vbo;
126
127 *batch_space = 1 + util_bitcount(dirty);
128 }
129
130 static void
131 emit_immediate(struct i915_context *i915)
132 {
133 /* remove unwanted bits and S7 */
134 unsigned dirty = (1 << I915_IMMEDIATE_S0 | 1 << I915_IMMEDIATE_S1 |
135 1 << I915_IMMEDIATE_S2 | 1 << I915_IMMEDIATE_S3 |
136 1 << I915_IMMEDIATE_S3 | 1 << I915_IMMEDIATE_S4 |
137 1 << I915_IMMEDIATE_S5 | 1 << I915_IMMEDIATE_S6) &
138 i915->immediate_dirty;
139 int i, num = util_bitcount(dirty);
140 assert(num && num <= I915_MAX_IMMEDIATE);
141
142 OUT_BATCH(_3DSTATE_LOAD_STATE_IMMEDIATE_1 |
143 dirty << 4 | (num - 1));
144
145 if (i915->immediate_dirty & (1 << I915_IMMEDIATE_S0)) {
146 if (i915->vbo)
147 OUT_RELOC(i915->vbo, I915_USAGE_VERTEX,
148 i915->current.immediate[I915_IMMEDIATE_S0]);
149 else
150 OUT_BATCH(0);
151 }
152
153 for (i = 1; i < I915_MAX_IMMEDIATE; i++) {
154 if (dirty & (1 << i))
155 OUT_BATCH(i915->current.immediate[i]);
156 }
157 }
158
159 static void
160 validate_dynamic(struct i915_context *i915, unsigned *batch_space)
161 {
162 *batch_space = util_bitcount(i915->dynamic_dirty & ((1 << I915_MAX_DYNAMIC) - 1));
163 }
164
165 static void
166 emit_dynamic(struct i915_context *i915)
167 {
168 int i;
169 for (i = 0; i < I915_MAX_DYNAMIC; i++) {
170 if (i915->dynamic_dirty & (1 << i))
171 OUT_BATCH(i915->current.dynamic[i]);
172 }
173 }
174
175 static void
176 validate_static(struct i915_context *i915, unsigned *batch_space)
177 {
178 *batch_space = 0;
179
180 if (i915->current.cbuf_bo && (i915->static_dirty & I915_DST_BUF_COLOR)) {
181 i915->validation_buffers[i915->num_validation_buffers++]
182 = i915->current.cbuf_bo;
183 *batch_space += 3;
184 }
185
186 if (i915->current.depth_bo && (i915->static_dirty & I915_DST_BUF_DEPTH)) {
187 i915->validation_buffers[i915->num_validation_buffers++]
188 = i915->current.depth_bo;
189 *batch_space += 3;
190 }
191
192 if (i915->static_dirty & I915_DST_VARS)
193 *batch_space += 2;
194
195 if (i915->static_dirty & I915_DST_RECT)
196 *batch_space += 5;
197 }
198
199 static void
200 emit_static(struct i915_context *i915)
201 {
202 if (i915->current.cbuf_bo && (i915->static_dirty & I915_DST_BUF_COLOR)) {
203 OUT_BATCH(_3DSTATE_BUF_INFO_CMD);
204 OUT_BATCH(i915->current.cbuf_flags);
205 OUT_RELOC(i915->current.cbuf_bo,
206 I915_USAGE_RENDER,
207 0);
208 }
209
210 /* What happens if no zbuf??
211 */
212 if (i915->current.depth_bo && (i915->static_dirty & I915_DST_BUF_DEPTH)) {
213 OUT_BATCH(_3DSTATE_BUF_INFO_CMD);
214 OUT_BATCH(i915->current.depth_flags);
215 OUT_RELOC(i915->current.depth_bo,
216 I915_USAGE_RENDER,
217 0);
218 }
219
220 if (i915->static_dirty & I915_DST_VARS) {
221 OUT_BATCH(_3DSTATE_DST_BUF_VARS_CMD);
222 OUT_BATCH(i915->current.dst_buf_vars);
223 }
224 }
225
226 static void
227 validate_map(struct i915_context *i915, unsigned *batch_space)
228 {
229 const uint enabled = i915->current.sampler_enable_flags;
230 uint unit;
231 struct i915_texture *tex;
232
233 *batch_space = i915->current.sampler_enable_nr ?
234 2 + 3*i915->current.sampler_enable_nr : 0;
235
236 for (unit = 0; unit < I915_TEX_UNITS; unit++) {
237 if (enabled & (1 << unit)) {
238 tex = i915_texture(i915->fragment_sampler_views[unit]->texture);
239 i915->validation_buffers[i915->num_validation_buffers++] = tex->buffer;
240 }
241 }
242 }
243
244 static void
245 emit_map(struct i915_context *i915)
246 {
247 const uint nr = i915->current.sampler_enable_nr;
248 if (nr) {
249 const uint enabled = i915->current.sampler_enable_flags;
250 uint unit;
251 uint count = 0;
252 OUT_BATCH(_3DSTATE_MAP_STATE | (3 * nr));
253 OUT_BATCH(enabled);
254 for (unit = 0; unit < I915_TEX_UNITS; unit++) {
255 if (enabled & (1 << unit)) {
256 struct i915_texture *texture = i915_texture(i915->fragment_sampler_views[unit]->texture);
257 struct i915_winsys_buffer *buf = texture->buffer;
258 assert(buf);
259
260 count++;
261
262 OUT_RELOC(buf, I915_USAGE_SAMPLER, 0);
263 OUT_BATCH(i915->current.texbuffer[unit][0]); /* MS3 */
264 OUT_BATCH(i915->current.texbuffer[unit][1]); /* MS4 */
265 }
266 }
267 assert(count == nr);
268 }
269 }
270
271 static void
272 validate_sampler(struct i915_context *i915, unsigned *batch_space)
273 {
274 *batch_space = i915->current.sampler_enable_nr ?
275 2 + 3*i915->current.sampler_enable_nr : 0;
276 }
277
278 static void
279 emit_sampler(struct i915_context *i915)
280 {
281 if (i915->current.sampler_enable_nr) {
282 int i;
283
284 OUT_BATCH( _3DSTATE_SAMPLER_STATE |
285 (3 * i915->current.sampler_enable_nr) );
286
287 OUT_BATCH( i915->current.sampler_enable_flags );
288
289 for (i = 0; i < I915_TEX_UNITS; i++) {
290 if (i915->current.sampler_enable_flags & (1<<i)) {
291 OUT_BATCH( i915->current.sampler[i][0] );
292 OUT_BATCH( i915->current.sampler[i][1] );
293 OUT_BATCH( i915->current.sampler[i][2] );
294 }
295 }
296 }
297 }
298
299 static void
300 validate_constants(struct i915_context *i915, unsigned *batch_space)
301 {
302 *batch_space = i915->fs->num_constants ?
303 2 + 4*i915->fs->num_constants : 0;
304 }
305
306 static void
307 emit_constants(struct i915_context *i915)
308 {
309 /* Collate the user-defined constants with the fragment shader's
310 * immediates according to the constant_flags[] array.
311 */
312 const uint nr = i915->fs->num_constants;
313 if (nr) {
314 uint i;
315
316 OUT_BATCH( _3DSTATE_PIXEL_SHADER_CONSTANTS | (nr * 4) );
317 OUT_BATCH((1 << nr) - 1);
318
319 for (i = 0; i < nr; i++) {
320 const uint *c;
321 if (i915->fs->constant_flags[i] == I915_CONSTFLAG_USER) {
322 /* grab user-defined constant */
323 c = (uint *) i915_buffer(i915->constants[PIPE_SHADER_FRAGMENT])->data;
324 c += 4 * i;
325 }
326 else {
327 /* emit program constant */
328 c = (uint *) i915->fs->constants[i];
329 }
330 #if 0 /* debug */
331 {
332 float *f = (float *) c;
333 printf("Const %2d: %f %f %f %f %s\n", i, f[0], f[1], f[2], f[3],
334 (i915->fs->constant_flags[i] == I915_CONSTFLAG_USER
335 ? "user" : "immediate"));
336 }
337 #endif
338 OUT_BATCH(*c++);
339 OUT_BATCH(*c++);
340 OUT_BATCH(*c++);
341 OUT_BATCH(*c++);
342 }
343 }
344 }
345
346 static const struct
347 {
348 enum pipe_format format;
349 uint hw_swizzle;
350 } fixup_formats[] = {
351 { PIPE_FORMAT_R8G8B8A8_UNORM, 0x21030000 /* BGRA */},
352 { PIPE_FORMAT_L8_UNORM, 0x00030000 /* RRRA */},
353 { PIPE_FORMAT_I8_UNORM, 0x00030000 /* RRRA */},
354 { PIPE_FORMAT_A8_UNORM, 0x33330000 /* AAAA */},
355 { PIPE_FORMAT_NONE, 0x00000000},
356 };
357
358 static uint need_target_fixup(struct pipe_surface* p)
359 {
360 enum pipe_format f;
361 /* if we don't have a surface bound yet, we don't need to fixup the shader */
362 if (!p)
363 return 0;
364
365 f = p->format;
366 for(int i=0; fixup_formats[i].format != PIPE_FORMAT_NONE; i++)
367 if (fixup_formats[i].format == f)
368 return 1;
369
370 return 0;
371 }
372
373 static uint fixup_swizzle(enum pipe_format f)
374 {
375 for(int i=0; fixup_formats[i].format != PIPE_FORMAT_NONE; i++)
376 if (fixup_formats[i].format == f)
377 return fixup_formats[i].hw_swizzle;
378
379 return 0;
380 }
381
382 static void
383 validate_program(struct i915_context *i915, unsigned *batch_space)
384 {
385 struct pipe_surface *cbuf_surface = i915->framebuffer.cbufs[0];
386 uint additional_size = need_target_fixup(cbuf_surface);
387
388 /* we need more batch space if we want to emulate rgba framebuffers */
389 *batch_space = i915->fs->program_len + 3 * additional_size;
390 }
391
392 static void
393 emit_program(struct i915_context *i915)
394 {
395 struct pipe_surface *cbuf_surface = i915->framebuffer.cbufs[0];
396 uint target_fixup = need_target_fixup(cbuf_surface);
397 uint i;
398
399 /* we should always have, at least, a pass-through program */
400 assert(i915->fs->program_len > 0);
401
402 {
403 /* first word has the size, we have to adjust that */
404 uint size = (i915->fs->program[0]);
405 size += target_fixup * 3;
406 OUT_BATCH(size);
407 }
408
409 /* output the declarations of the program */
410 for (i=1 ; i < i915->fs->program_len; i++)
411 OUT_BATCH(i915->fs->program[i]);
412
413 /* we emit an additional mov with swizzle to fake RGBA framebuffers */
414 if (target_fixup) {
415 /* mov out_color, out_color.zyxw */
416 OUT_BATCH(A0_MOV |
417 (REG_TYPE_OC << A0_DEST_TYPE_SHIFT) |
418 A0_DEST_CHANNEL_ALL |
419 (REG_TYPE_OC << A0_SRC0_TYPE_SHIFT) |
420 (T_DIFFUSE << A0_SRC0_NR_SHIFT));
421 OUT_BATCH(fixup_swizzle(cbuf_surface->format));
422 OUT_BATCH(0);
423 }
424 }
425
426 static void
427 emit_draw_rect(struct i915_context *i915)
428 {
429 if (i915->static_dirty & I915_DST_RECT) {
430 OUT_BATCH(_3DSTATE_DRAW_RECT_CMD);
431 OUT_BATCH(DRAW_RECT_DIS_DEPTH_OFS);
432 OUT_BATCH(i915->current.draw_offset);
433 OUT_BATCH(i915->current.draw_size);
434 OUT_BATCH(i915->current.draw_offset);
435 }
436 }
437
438 static boolean
439 i915_validate_state(struct i915_context *i915, unsigned *batch_space)
440 {
441 unsigned tmp;
442
443 i915->num_validation_buffers = 0;
444 if (i915->hardware_dirty & I915_HW_INVARIANT)
445 *batch_space = Elements(invariant_state);
446 else
447 *batch_space = 0;
448
449 #define VALIDATE_ATOM(atom, hw_dirty) \
450 if (i915->hardware_dirty & hw_dirty) { \
451 validate_##atom(i915, &tmp); \
452 *batch_space += tmp; }
453 VALIDATE_ATOM(flush, I915_HW_FLUSH);
454 VALIDATE_ATOM(immediate, I915_HW_IMMEDIATE);
455 VALIDATE_ATOM(dynamic, I915_HW_DYNAMIC);
456 VALIDATE_ATOM(static, I915_HW_STATIC);
457 VALIDATE_ATOM(map, I915_HW_MAP);
458 VALIDATE_ATOM(sampler, I915_HW_SAMPLER);
459 VALIDATE_ATOM(constants, I915_HW_CONSTANTS);
460 VALIDATE_ATOM(program, I915_HW_PROGRAM);
461 #undef VALIDATE_ATOM
462
463 if (i915->num_validation_buffers == 0)
464 return TRUE;
465
466 if (!i915_winsys_validate_buffers(i915->batch, i915->validation_buffers,
467 i915->num_validation_buffers))
468 return FALSE;
469
470 return TRUE;
471 }
472
473 /* Push the state into the sarea and/or texture memory.
474 */
475 void
476 i915_emit_hardware_state(struct i915_context *i915 )
477 {
478 unsigned batch_space;
479 uintptr_t save_ptr;
480
481 assert(i915->dirty == 0);
482
483 if (I915_DBG_ON(DBG_ATOMS))
484 i915_dump_hardware_dirty(i915, __FUNCTION__);
485
486 if (!i915_validate_state(i915, &batch_space)) {
487 FLUSH_BATCH(NULL);
488 assert(i915_validate_state(i915, &batch_space));
489 }
490
491 if(!BEGIN_BATCH(batch_space)) {
492 FLUSH_BATCH(NULL);
493 assert(i915_validate_state(i915, &batch_space));
494 assert(BEGIN_BATCH(batch_space));
495 }
496
497 save_ptr = (uintptr_t)i915->batch->ptr;
498
499 #define EMIT_ATOM(atom, hw_dirty) \
500 if (i915->hardware_dirty & hw_dirty) \
501 emit_##atom(i915);
502 EMIT_ATOM(flush, I915_HW_FLUSH);
503 EMIT_ATOM(invariant, I915_HW_INVARIANT);
504 EMIT_ATOM(immediate, I915_HW_IMMEDIATE);
505 EMIT_ATOM(dynamic, I915_HW_DYNAMIC);
506 EMIT_ATOM(static, I915_HW_STATIC);
507 EMIT_ATOM(map, I915_HW_MAP);
508 EMIT_ATOM(sampler, I915_HW_SAMPLER);
509 EMIT_ATOM(constants, I915_HW_CONSTANTS);
510 EMIT_ATOM(program, I915_HW_PROGRAM);
511 EMIT_ATOM(draw_rect, I915_HW_STATIC);
512 #undef EMIT_ATOM
513
514 I915_DBG(DBG_EMIT, "%s: used %d dwords, %d dwords reserved\n", __FUNCTION__,
515 ((uintptr_t)i915->batch->ptr - save_ptr) / 4,
516 batch_space);
517 assert(((uintptr_t)i915->batch->ptr - save_ptr) / 4 == batch_space);
518
519 i915->hardware_dirty = 0;
520 i915->immediate_dirty = 0;
521 i915->dynamic_dirty = 0;
522 i915->static_dirty = 0;
523 i915->flush_dirty = 0;
524 }