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