i965: Move buffer texture size calculation into a common helper function.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_state_upload.c
1 /*
2 Copyright (C) Intel Corp. 2006. All Rights Reserved.
3 Intel funded Tungsten Graphics to
4 develop this 3D driver.
5
6 Permission is hereby granted, free of charge, to any person obtaining
7 a 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, sublicense, 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
16 portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **********************************************************************/
27 /*
28 * Authors:
29 * Keith Whitwell <keithw@vmware.com>
30 */
31
32
33
34 #include "brw_context.h"
35 #include "brw_defines.h"
36 #include "brw_state.h"
37 #include "brw_program.h"
38 #include "drivers/common/meta.h"
39 #include "intel_batchbuffer.h"
40 #include "intel_buffers.h"
41 #include "brw_vs.h"
42 #include "brw_ff_gs.h"
43 #include "brw_gs.h"
44 #include "brw_wm.h"
45 #include "brw_cs.h"
46 #include "main/framebuffer.h"
47
48 static void
49 brw_upload_initial_gpu_state(struct brw_context *brw)
50 {
51 const struct gen_device_info *devinfo = &brw->screen->devinfo;
52 const struct brw_compiler *compiler = brw->screen->compiler;
53
54 /* On platforms with hardware contexts, we can set our initial GPU state
55 * right away rather than doing it via state atoms. This saves a small
56 * amount of overhead on every draw call.
57 */
58 if (!brw->hw_ctx)
59 return;
60
61 if (devinfo->gen == 6)
62 brw_emit_post_sync_nonzero_flush(brw);
63
64 brw_upload_invariant_state(brw);
65
66 if (devinfo->gen == 10 || devinfo->gen == 11) {
67 brw_load_register_imm32(brw, GEN10_CACHE_MODE_SS,
68 REG_MASK(GEN10_FLOAT_BLEND_OPTIMIZATION_ENABLE) |
69 GEN10_FLOAT_BLEND_OPTIMIZATION_ENABLE);
70
71 /* From gen10 workaround table in h/w specs:
72 *
73 * "On 3DSTATE_3D_MODE, driver must always program bits 31:16 of DW1
74 * a value of 0xFFFF"
75 *
76 * This means that we end up setting the entire 3D_MODE state. Bits
77 * in this register control things such as slice hashing and we want
78 * the default values of zero at the moment.
79 */
80 BEGIN_BATCH(2);
81 OUT_BATCH(_3DSTATE_3D_MODE << 16 | (2 - 2));
82 OUT_BATCH(0xFFFF << 16);
83 ADVANCE_BATCH();
84 }
85
86 if (devinfo->gen == 9) {
87 /* Recommended optimizations for Victim Cache eviction and floating
88 * point blending.
89 */
90 brw_load_register_imm32(brw, GEN7_CACHE_MODE_1,
91 REG_MASK(GEN9_FLOAT_BLEND_OPTIMIZATION_ENABLE) |
92 REG_MASK(GEN9_PARTIAL_RESOLVE_DISABLE_IN_VC) |
93 GEN9_FLOAT_BLEND_OPTIMIZATION_ENABLE |
94 GEN9_PARTIAL_RESOLVE_DISABLE_IN_VC);
95
96 if (gen_device_info_is_9lp(devinfo)) {
97 brw_load_register_imm32(brw, GEN7_GT_MODE,
98 GEN9_SUBSLICE_HASHING_MASK_BITS |
99 GEN9_SUBSLICE_HASHING_16x16);
100 }
101 }
102
103 if (devinfo->gen >= 8) {
104 gen8_emit_3dstate_sample_pattern(brw);
105
106 BEGIN_BATCH(5);
107 OUT_BATCH(_3DSTATE_WM_HZ_OP << 16 | (5 - 2));
108 OUT_BATCH(0);
109 OUT_BATCH(0);
110 OUT_BATCH(0);
111 OUT_BATCH(0);
112 ADVANCE_BATCH();
113
114 BEGIN_BATCH(2);
115 OUT_BATCH(_3DSTATE_WM_CHROMAKEY << 16 | (2 - 2));
116 OUT_BATCH(0);
117 ADVANCE_BATCH();
118 }
119
120 /* Set the "CONSTANT_BUFFER Address Offset Disable" bit, so
121 * 3DSTATE_CONSTANT_XS buffer 0 is an absolute address.
122 *
123 * This is only safe on kernels with context isolation support.
124 */
125 if (!compiler->constant_buffer_0_is_relative) {
126 if (devinfo->gen >= 9) {
127 BEGIN_BATCH(3);
128 OUT_BATCH(MI_LOAD_REGISTER_IMM | (3 - 2));
129 OUT_BATCH(CS_DEBUG_MODE2);
130 OUT_BATCH(REG_MASK(CSDBG2_CONSTANT_BUFFER_ADDRESS_OFFSET_DISABLE) |
131 CSDBG2_CONSTANT_BUFFER_ADDRESS_OFFSET_DISABLE);
132 ADVANCE_BATCH();
133 } else if (devinfo->gen == 8) {
134 BEGIN_BATCH(3);
135 OUT_BATCH(MI_LOAD_REGISTER_IMM | (3 - 2));
136 OUT_BATCH(INSTPM);
137 OUT_BATCH(REG_MASK(INSTPM_CONSTANT_BUFFER_ADDRESS_OFFSET_DISABLE) |
138 INSTPM_CONSTANT_BUFFER_ADDRESS_OFFSET_DISABLE);
139 ADVANCE_BATCH();
140 }
141 }
142 }
143
144 static inline const struct brw_tracked_state *
145 brw_get_pipeline_atoms(struct brw_context *brw,
146 enum brw_pipeline pipeline)
147 {
148 switch (pipeline) {
149 case BRW_RENDER_PIPELINE:
150 return brw->render_atoms;
151 case BRW_COMPUTE_PIPELINE:
152 return brw->compute_atoms;
153 default:
154 STATIC_ASSERT(BRW_NUM_PIPELINES == 2);
155 unreachable("Unsupported pipeline");
156 return NULL;
157 }
158 }
159
160 void
161 brw_copy_pipeline_atoms(struct brw_context *brw,
162 enum brw_pipeline pipeline,
163 const struct brw_tracked_state **atoms,
164 int num_atoms)
165 {
166 /* This is to work around brw_context::atoms being declared const. We want
167 * it to be const, but it needs to be initialized somehow!
168 */
169 struct brw_tracked_state *context_atoms =
170 (struct brw_tracked_state *) brw_get_pipeline_atoms(brw, pipeline);
171
172 for (int i = 0; i < num_atoms; i++) {
173 context_atoms[i] = *atoms[i];
174 assert(context_atoms[i].dirty.mesa | context_atoms[i].dirty.brw);
175 assert(context_atoms[i].emit);
176 }
177
178 brw->num_atoms[pipeline] = num_atoms;
179 }
180
181 void brw_init_state( struct brw_context *brw )
182 {
183 struct gl_context *ctx = &brw->ctx;
184 const struct gen_device_info *devinfo = &brw->screen->devinfo;
185
186 /* Force the first brw_select_pipeline to emit pipeline select */
187 brw->last_pipeline = BRW_NUM_PIPELINES;
188
189 brw_init_caches(brw);
190
191 if (devinfo->gen >= 11)
192 gen11_init_atoms(brw);
193 else if (devinfo->gen >= 10)
194 gen10_init_atoms(brw);
195 else if (devinfo->gen >= 9)
196 gen9_init_atoms(brw);
197 else if (devinfo->gen >= 8)
198 gen8_init_atoms(brw);
199 else if (devinfo->is_haswell)
200 gen75_init_atoms(brw);
201 else if (devinfo->gen >= 7)
202 gen7_init_atoms(brw);
203 else if (devinfo->gen >= 6)
204 gen6_init_atoms(brw);
205 else if (devinfo->gen >= 5)
206 gen5_init_atoms(brw);
207 else if (devinfo->is_g4x)
208 gen45_init_atoms(brw);
209 else
210 gen4_init_atoms(brw);
211
212 brw_upload_initial_gpu_state(brw);
213
214 brw->NewGLState = ~0;
215 brw->ctx.NewDriverState = ~0ull;
216
217 /* ~0 is a nonsensical value which won't match anything we program, so
218 * the programming will take effect on the first time around.
219 */
220 brw->pma_stall_bits = ~0;
221
222 /* Make sure that brw->ctx.NewDriverState has enough bits to hold all possible
223 * dirty flags.
224 */
225 STATIC_ASSERT(BRW_NUM_STATE_BITS <= 8 * sizeof(brw->ctx.NewDriverState));
226
227 ctx->DriverFlags.NewTransformFeedback = BRW_NEW_TRANSFORM_FEEDBACK;
228 ctx->DriverFlags.NewTransformFeedbackProg = BRW_NEW_TRANSFORM_FEEDBACK;
229 ctx->DriverFlags.NewRasterizerDiscard = BRW_NEW_RASTERIZER_DISCARD;
230 ctx->DriverFlags.NewUniformBuffer = BRW_NEW_UNIFORM_BUFFER;
231 ctx->DriverFlags.NewShaderStorageBuffer = BRW_NEW_UNIFORM_BUFFER;
232 ctx->DriverFlags.NewTextureBuffer = BRW_NEW_TEXTURE_BUFFER;
233 ctx->DriverFlags.NewAtomicBuffer = BRW_NEW_UNIFORM_BUFFER;
234 ctx->DriverFlags.NewImageUnits = BRW_NEW_IMAGE_UNITS;
235 ctx->DriverFlags.NewDefaultTessLevels = BRW_NEW_DEFAULT_TESS_LEVELS;
236 ctx->DriverFlags.NewIntelConservativeRasterization = BRW_NEW_CONSERVATIVE_RASTERIZATION;
237 }
238
239
240 void brw_destroy_state( struct brw_context *brw )
241 {
242 brw_destroy_caches(brw);
243 }
244
245 /***********************************************************************
246 */
247
248 static bool
249 check_state(const struct brw_state_flags *a, const struct brw_state_flags *b)
250 {
251 return ((a->mesa & b->mesa) | (a->brw & b->brw)) != 0;
252 }
253
254 static void accumulate_state( struct brw_state_flags *a,
255 const struct brw_state_flags *b )
256 {
257 a->mesa |= b->mesa;
258 a->brw |= b->brw;
259 }
260
261
262 static void xor_states( struct brw_state_flags *result,
263 const struct brw_state_flags *a,
264 const struct brw_state_flags *b )
265 {
266 result->mesa = a->mesa ^ b->mesa;
267 result->brw = a->brw ^ b->brw;
268 }
269
270 struct dirty_bit_map {
271 uint64_t bit;
272 char *name;
273 uint32_t count;
274 };
275
276 #define DEFINE_BIT(name) {name, #name, 0}
277
278 static struct dirty_bit_map mesa_bits[] = {
279 DEFINE_BIT(_NEW_MODELVIEW),
280 DEFINE_BIT(_NEW_PROJECTION),
281 DEFINE_BIT(_NEW_TEXTURE_MATRIX),
282 DEFINE_BIT(_NEW_COLOR),
283 DEFINE_BIT(_NEW_DEPTH),
284 DEFINE_BIT(_NEW_EVAL),
285 DEFINE_BIT(_NEW_FOG),
286 DEFINE_BIT(_NEW_HINT),
287 DEFINE_BIT(_NEW_LIGHT),
288 DEFINE_BIT(_NEW_LINE),
289 DEFINE_BIT(_NEW_PIXEL),
290 DEFINE_BIT(_NEW_POINT),
291 DEFINE_BIT(_NEW_POLYGON),
292 DEFINE_BIT(_NEW_POLYGONSTIPPLE),
293 DEFINE_BIT(_NEW_SCISSOR),
294 DEFINE_BIT(_NEW_STENCIL),
295 DEFINE_BIT(_NEW_TEXTURE_OBJECT),
296 DEFINE_BIT(_NEW_TRANSFORM),
297 DEFINE_BIT(_NEW_VIEWPORT),
298 DEFINE_BIT(_NEW_TEXTURE_STATE),
299 DEFINE_BIT(_NEW_ARRAY),
300 DEFINE_BIT(_NEW_RENDERMODE),
301 DEFINE_BIT(_NEW_BUFFERS),
302 DEFINE_BIT(_NEW_CURRENT_ATTRIB),
303 DEFINE_BIT(_NEW_MULTISAMPLE),
304 DEFINE_BIT(_NEW_TRACK_MATRIX),
305 DEFINE_BIT(_NEW_PROGRAM),
306 DEFINE_BIT(_NEW_PROGRAM_CONSTANTS),
307 DEFINE_BIT(_NEW_FRAG_CLAMP),
308 /* Avoid sign extension problems. */
309 {(unsigned) _NEW_VARYING_VP_INPUTS, "_NEW_VARYING_VP_INPUTS", 0},
310 {0, 0, 0}
311 };
312
313 static struct dirty_bit_map brw_bits[] = {
314 DEFINE_BIT(BRW_NEW_FS_PROG_DATA),
315 DEFINE_BIT(BRW_NEW_BLORP_BLIT_PROG_DATA),
316 DEFINE_BIT(BRW_NEW_SF_PROG_DATA),
317 DEFINE_BIT(BRW_NEW_VS_PROG_DATA),
318 DEFINE_BIT(BRW_NEW_FF_GS_PROG_DATA),
319 DEFINE_BIT(BRW_NEW_GS_PROG_DATA),
320 DEFINE_BIT(BRW_NEW_TCS_PROG_DATA),
321 DEFINE_BIT(BRW_NEW_TES_PROG_DATA),
322 DEFINE_BIT(BRW_NEW_CLIP_PROG_DATA),
323 DEFINE_BIT(BRW_NEW_CS_PROG_DATA),
324 DEFINE_BIT(BRW_NEW_URB_FENCE),
325 DEFINE_BIT(BRW_NEW_FRAGMENT_PROGRAM),
326 DEFINE_BIT(BRW_NEW_GEOMETRY_PROGRAM),
327 DEFINE_BIT(BRW_NEW_TESS_PROGRAMS),
328 DEFINE_BIT(BRW_NEW_VERTEX_PROGRAM),
329 DEFINE_BIT(BRW_NEW_REDUCED_PRIMITIVE),
330 DEFINE_BIT(BRW_NEW_PATCH_PRIMITIVE),
331 DEFINE_BIT(BRW_NEW_PRIMITIVE),
332 DEFINE_BIT(BRW_NEW_CONTEXT),
333 DEFINE_BIT(BRW_NEW_PSP),
334 DEFINE_BIT(BRW_NEW_SURFACES),
335 DEFINE_BIT(BRW_NEW_BINDING_TABLE_POINTERS),
336 DEFINE_BIT(BRW_NEW_INDICES),
337 DEFINE_BIT(BRW_NEW_VERTICES),
338 DEFINE_BIT(BRW_NEW_DEFAULT_TESS_LEVELS),
339 DEFINE_BIT(BRW_NEW_BATCH),
340 DEFINE_BIT(BRW_NEW_INDEX_BUFFER),
341 DEFINE_BIT(BRW_NEW_VS_CONSTBUF),
342 DEFINE_BIT(BRW_NEW_TCS_CONSTBUF),
343 DEFINE_BIT(BRW_NEW_TES_CONSTBUF),
344 DEFINE_BIT(BRW_NEW_GS_CONSTBUF),
345 DEFINE_BIT(BRW_NEW_PROGRAM_CACHE),
346 DEFINE_BIT(BRW_NEW_STATE_BASE_ADDRESS),
347 DEFINE_BIT(BRW_NEW_VUE_MAP_GEOM_OUT),
348 DEFINE_BIT(BRW_NEW_TRANSFORM_FEEDBACK),
349 DEFINE_BIT(BRW_NEW_RASTERIZER_DISCARD),
350 DEFINE_BIT(BRW_NEW_STATS_WM),
351 DEFINE_BIT(BRW_NEW_UNIFORM_BUFFER),
352 DEFINE_BIT(BRW_NEW_IMAGE_UNITS),
353 DEFINE_BIT(BRW_NEW_META_IN_PROGRESS),
354 DEFINE_BIT(BRW_NEW_PUSH_CONSTANT_ALLOCATION),
355 DEFINE_BIT(BRW_NEW_NUM_SAMPLES),
356 DEFINE_BIT(BRW_NEW_TEXTURE_BUFFER),
357 DEFINE_BIT(BRW_NEW_GEN4_UNIT_STATE),
358 DEFINE_BIT(BRW_NEW_CC_VP),
359 DEFINE_BIT(BRW_NEW_SF_VP),
360 DEFINE_BIT(BRW_NEW_CLIP_VP),
361 DEFINE_BIT(BRW_NEW_SAMPLER_STATE_TABLE),
362 DEFINE_BIT(BRW_NEW_VS_ATTRIB_WORKAROUNDS),
363 DEFINE_BIT(BRW_NEW_COMPUTE_PROGRAM),
364 DEFINE_BIT(BRW_NEW_CS_WORK_GROUPS),
365 DEFINE_BIT(BRW_NEW_URB_SIZE),
366 DEFINE_BIT(BRW_NEW_CC_STATE),
367 DEFINE_BIT(BRW_NEW_BLORP),
368 DEFINE_BIT(BRW_NEW_VIEWPORT_COUNT),
369 DEFINE_BIT(BRW_NEW_CONSERVATIVE_RASTERIZATION),
370 DEFINE_BIT(BRW_NEW_DRAW_CALL),
371 DEFINE_BIT(BRW_NEW_AUX_STATE),
372 {0, 0, 0}
373 };
374
375 static void
376 brw_update_dirty_count(struct dirty_bit_map *bit_map, uint64_t bits)
377 {
378 for (int i = 0; bit_map[i].bit != 0; i++) {
379 if (bit_map[i].bit & bits)
380 bit_map[i].count++;
381 }
382 }
383
384 static void
385 brw_print_dirty_count(struct dirty_bit_map *bit_map)
386 {
387 for (int i = 0; bit_map[i].bit != 0; i++) {
388 if (bit_map[i].count > 1) {
389 fprintf(stderr, "0x%016"PRIx64": %12d (%s)\n",
390 bit_map[i].bit, bit_map[i].count, bit_map[i].name);
391 }
392 }
393 }
394
395 static inline void
396 brw_upload_tess_programs(struct brw_context *brw)
397 {
398 if (brw->programs[MESA_SHADER_TESS_EVAL]) {
399 brw_upload_tcs_prog(brw);
400 brw_upload_tes_prog(brw);
401 } else {
402 brw->tcs.base.prog_data = NULL;
403 brw->tes.base.prog_data = NULL;
404 }
405 }
406
407 static inline void
408 brw_upload_programs(struct brw_context *brw,
409 enum brw_pipeline pipeline)
410 {
411 struct gl_context *ctx = &brw->ctx;
412 const struct gen_device_info *devinfo = &brw->screen->devinfo;
413
414 if (pipeline == BRW_RENDER_PIPELINE) {
415 brw_upload_vs_prog(brw);
416 brw_upload_tess_programs(brw);
417
418 if (brw->programs[MESA_SHADER_GEOMETRY]) {
419 brw_upload_gs_prog(brw);
420 } else {
421 brw->gs.base.prog_data = NULL;
422 if (devinfo->gen < 7)
423 brw_upload_ff_gs_prog(brw);
424 }
425
426 /* Update the VUE map for data exiting the GS stage of the pipeline.
427 * This comes from the last enabled shader stage.
428 */
429 GLbitfield64 old_slots = brw->vue_map_geom_out.slots_valid;
430 bool old_separate = brw->vue_map_geom_out.separate;
431 struct brw_vue_prog_data *vue_prog_data;
432 if (brw->programs[MESA_SHADER_GEOMETRY])
433 vue_prog_data = brw_vue_prog_data(brw->gs.base.prog_data);
434 else if (brw->programs[MESA_SHADER_TESS_EVAL])
435 vue_prog_data = brw_vue_prog_data(brw->tes.base.prog_data);
436 else
437 vue_prog_data = brw_vue_prog_data(brw->vs.base.prog_data);
438
439 brw->vue_map_geom_out = vue_prog_data->vue_map;
440
441 /* If the layout has changed, signal BRW_NEW_VUE_MAP_GEOM_OUT. */
442 if (old_slots != brw->vue_map_geom_out.slots_valid ||
443 old_separate != brw->vue_map_geom_out.separate)
444 brw->ctx.NewDriverState |= BRW_NEW_VUE_MAP_GEOM_OUT;
445
446 if ((old_slots ^ brw->vue_map_geom_out.slots_valid) &
447 VARYING_BIT_VIEWPORT) {
448 ctx->NewDriverState |= BRW_NEW_VIEWPORT_COUNT;
449 brw->clip.viewport_count =
450 (brw->vue_map_geom_out.slots_valid & VARYING_BIT_VIEWPORT) ?
451 ctx->Const.MaxViewports : 1;
452 }
453
454 brw_upload_wm_prog(brw);
455
456 if (devinfo->gen < 6) {
457 brw_upload_clip_prog(brw);
458 brw_upload_sf_prog(brw);
459 }
460
461 brw_disk_cache_write_render_programs(brw);
462 } else if (pipeline == BRW_COMPUTE_PIPELINE) {
463 brw_upload_cs_prog(brw);
464 brw_disk_cache_write_compute_program(brw);
465 }
466 }
467
468 static inline void
469 merge_ctx_state(struct brw_context *brw,
470 struct brw_state_flags *state)
471 {
472 state->mesa |= brw->NewGLState;
473 state->brw |= brw->ctx.NewDriverState;
474 }
475
476 static ALWAYS_INLINE void
477 check_and_emit_atom(struct brw_context *brw,
478 struct brw_state_flags *state,
479 const struct brw_tracked_state *atom)
480 {
481 if (check_state(state, &atom->dirty)) {
482 atom->emit(brw);
483 merge_ctx_state(brw, state);
484 }
485 }
486
487 static inline void
488 brw_upload_pipeline_state(struct brw_context *brw,
489 enum brw_pipeline pipeline)
490 {
491 const struct gen_device_info *devinfo = &brw->screen->devinfo;
492 struct gl_context *ctx = &brw->ctx;
493 int i;
494 static int dirty_count = 0;
495 struct brw_state_flags state = brw->state.pipelines[pipeline];
496 const unsigned fb_samples =
497 MAX2(_mesa_geometric_samples(ctx->DrawBuffer), 1);
498
499 brw_select_pipeline(brw, pipeline);
500
501 if (unlikely(INTEL_DEBUG & DEBUG_REEMIT)) {
502 /* Always re-emit all state. */
503 brw->NewGLState = ~0;
504 ctx->NewDriverState = ~0ull;
505 }
506
507 if (pipeline == BRW_RENDER_PIPELINE) {
508 if (brw->programs[MESA_SHADER_FRAGMENT] !=
509 ctx->FragmentProgram._Current) {
510 brw->programs[MESA_SHADER_FRAGMENT] = ctx->FragmentProgram._Current;
511 brw->ctx.NewDriverState |= BRW_NEW_FRAGMENT_PROGRAM;
512 }
513
514 if (brw->programs[MESA_SHADER_TESS_EVAL] !=
515 ctx->TessEvalProgram._Current) {
516 brw->programs[MESA_SHADER_TESS_EVAL] = ctx->TessEvalProgram._Current;
517 brw->ctx.NewDriverState |= BRW_NEW_TESS_PROGRAMS;
518 }
519
520 if (brw->programs[MESA_SHADER_TESS_CTRL] !=
521 ctx->TessCtrlProgram._Current) {
522 brw->programs[MESA_SHADER_TESS_CTRL] = ctx->TessCtrlProgram._Current;
523 brw->ctx.NewDriverState |= BRW_NEW_TESS_PROGRAMS;
524 }
525
526 if (brw->programs[MESA_SHADER_GEOMETRY] !=
527 ctx->GeometryProgram._Current) {
528 brw->programs[MESA_SHADER_GEOMETRY] = ctx->GeometryProgram._Current;
529 brw->ctx.NewDriverState |= BRW_NEW_GEOMETRY_PROGRAM;
530 }
531
532 if (brw->programs[MESA_SHADER_VERTEX] != ctx->VertexProgram._Current) {
533 brw->programs[MESA_SHADER_VERTEX] = ctx->VertexProgram._Current;
534 brw->ctx.NewDriverState |= BRW_NEW_VERTEX_PROGRAM;
535 }
536 }
537
538 if (brw->programs[MESA_SHADER_COMPUTE] != ctx->ComputeProgram._Current) {
539 brw->programs[MESA_SHADER_COMPUTE] = ctx->ComputeProgram._Current;
540 brw->ctx.NewDriverState |= BRW_NEW_COMPUTE_PROGRAM;
541 }
542
543 if (brw->meta_in_progress != _mesa_meta_in_progress(ctx)) {
544 brw->meta_in_progress = _mesa_meta_in_progress(ctx);
545 brw->ctx.NewDriverState |= BRW_NEW_META_IN_PROGRESS;
546 }
547
548 if (brw->num_samples != fb_samples) {
549 brw->num_samples = fb_samples;
550 brw->ctx.NewDriverState |= BRW_NEW_NUM_SAMPLES;
551 }
552
553 /* Exit early if no state is flagged as dirty */
554 merge_ctx_state(brw, &state);
555 if ((state.mesa | state.brw) == 0)
556 return;
557
558 /* Emit Sandybridge workaround flushes on every primitive, for safety. */
559 if (devinfo->gen == 6)
560 brw_emit_post_sync_nonzero_flush(brw);
561
562 brw_upload_programs(brw, pipeline);
563 merge_ctx_state(brw, &state);
564
565 brw_upload_state_base_address(brw);
566
567 const struct brw_tracked_state *atoms =
568 brw_get_pipeline_atoms(brw, pipeline);
569 const int num_atoms = brw->num_atoms[pipeline];
570
571 if (unlikely(INTEL_DEBUG)) {
572 /* Debug version which enforces various sanity checks on the
573 * state flags which are generated and checked to help ensure
574 * state atoms are ordered correctly in the list.
575 */
576 struct brw_state_flags examined, prev;
577 memset(&examined, 0, sizeof(examined));
578 prev = state;
579
580 for (i = 0; i < num_atoms; i++) {
581 const struct brw_tracked_state *atom = &atoms[i];
582 struct brw_state_flags generated;
583
584 check_and_emit_atom(brw, &state, atom);
585
586 accumulate_state(&examined, &atom->dirty);
587
588 /* generated = (prev ^ state)
589 * if (examined & generated)
590 * fail;
591 */
592 xor_states(&generated, &prev, &state);
593 assert(!check_state(&examined, &generated));
594 prev = state;
595 }
596 }
597 else {
598 for (i = 0; i < num_atoms; i++) {
599 const struct brw_tracked_state *atom = &atoms[i];
600
601 check_and_emit_atom(brw, &state, atom);
602 }
603 }
604
605 if (unlikely(INTEL_DEBUG & DEBUG_STATE)) {
606 STATIC_ASSERT(ARRAY_SIZE(brw_bits) == BRW_NUM_STATE_BITS + 1);
607
608 brw_update_dirty_count(mesa_bits, state.mesa);
609 brw_update_dirty_count(brw_bits, state.brw);
610 if (dirty_count++ % 1000 == 0) {
611 brw_print_dirty_count(mesa_bits);
612 brw_print_dirty_count(brw_bits);
613 fprintf(stderr, "\n");
614 }
615 }
616 }
617
618 /***********************************************************************
619 * Emit all state:
620 */
621 void brw_upload_render_state(struct brw_context *brw)
622 {
623 brw_upload_pipeline_state(brw, BRW_RENDER_PIPELINE);
624 }
625
626 static inline void
627 brw_pipeline_state_finished(struct brw_context *brw,
628 enum brw_pipeline pipeline)
629 {
630 /* Save all dirty state into the other pipelines */
631 for (unsigned i = 0; i < BRW_NUM_PIPELINES; i++) {
632 if (i != pipeline) {
633 brw->state.pipelines[i].mesa |= brw->NewGLState;
634 brw->state.pipelines[i].brw |= brw->ctx.NewDriverState;
635 } else {
636 memset(&brw->state.pipelines[i], 0, sizeof(struct brw_state_flags));
637 }
638 }
639
640 brw->NewGLState = 0;
641 brw->ctx.NewDriverState = 0ull;
642 }
643
644 /**
645 * Clear dirty bits to account for the fact that the state emitted by
646 * brw_upload_render_state() has been committed to the hardware. This is a
647 * separate call from brw_upload_render_state() because it's possible that
648 * after the call to brw_upload_render_state(), we will discover that we've
649 * run out of aperture space, and need to rewind the batch buffer to the state
650 * it had before the brw_upload_render_state() call.
651 */
652 void
653 brw_render_state_finished(struct brw_context *brw)
654 {
655 brw_pipeline_state_finished(brw, BRW_RENDER_PIPELINE);
656 }
657
658 void
659 brw_upload_compute_state(struct brw_context *brw)
660 {
661 brw_upload_pipeline_state(brw, BRW_COMPUTE_PIPELINE);
662 }
663
664 void
665 brw_compute_state_finished(struct brw_context *brw)
666 {
667 brw_pipeline_state_finished(brw, BRW_COMPUTE_PIPELINE);
668 }