nvc0: add state management for shader buffers
[mesa.git] / src / gallium / drivers / nouveau / nvc0 / nvc0_state_validate.c
1
2 #include "util/u_format.h"
3 #include "util/u_math.h"
4
5 #include "nvc0/nvc0_context.h"
6 #include "nv50/nv50_defs.xml.h"
7
8 #if 0
9 static void
10 nvc0_validate_zcull(struct nvc0_context *nvc0)
11 {
12 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
13 struct pipe_framebuffer_state *fb = &nvc0->framebuffer;
14 struct nv50_surface *sf = nv50_surface(fb->zsbuf);
15 struct nv50_miptree *mt = nv50_miptree(sf->base.texture);
16 struct nouveau_bo *bo = mt->base.bo;
17 uint32_t size;
18 uint32_t offset = align(mt->total_size, 1 << 17);
19 unsigned width, height;
20
21 assert(mt->base.base.depth0 == 1 && mt->base.base.array_size < 2);
22
23 size = mt->total_size * 2;
24
25 height = align(fb->height, 32);
26 width = fb->width % 224;
27 if (width)
28 width = fb->width + (224 - width);
29 else
30 width = fb->width;
31
32 BEGIN_NVC0(push, NVC0_3D(ZCULL_REGION), 1);
33 PUSH_DATA (push, 0);
34 BEGIN_NVC0(push, NVC0_3D(ZCULL_ADDRESS_HIGH), 2);
35 PUSH_DATAh(push, bo->offset + offset);
36 PUSH_DATA (push, bo->offset + offset);
37 offset += 1 << 17;
38 BEGIN_NVC0(push, NVC0_3D(ZCULL_LIMIT_HIGH), 2);
39 PUSH_DATAh(push, bo->offset + offset);
40 PUSH_DATA (push, bo->offset + offset);
41 BEGIN_NVC0(push, SUBC_3D(0x07e0), 2);
42 PUSH_DATA (push, size);
43 PUSH_DATA (push, size >> 16);
44 BEGIN_NVC0(push, SUBC_3D(0x15c8), 1); /* bits 0x3 */
45 PUSH_DATA (push, 2);
46 BEGIN_NVC0(push, NVC0_3D(ZCULL_WIDTH), 4);
47 PUSH_DATA (push, width);
48 PUSH_DATA (push, height);
49 PUSH_DATA (push, 1);
50 PUSH_DATA (push, 0);
51 BEGIN_NVC0(push, NVC0_3D(ZCULL_WINDOW_OFFSET_X), 2);
52 PUSH_DATA (push, 0);
53 PUSH_DATA (push, 0);
54 BEGIN_NVC0(push, NVC0_3D(ZCULL_INVALIDATE), 1);
55 PUSH_DATA (push, 0);
56 }
57 #endif
58
59 static inline void
60 nvc0_fb_set_null_rt(struct nouveau_pushbuf *push, unsigned i)
61 {
62 BEGIN_NVC0(push, NVC0_3D(RT_ADDRESS_HIGH(i)), 6);
63 PUSH_DATA (push, 0);
64 PUSH_DATA (push, 0);
65 PUSH_DATA (push, 64);
66 PUSH_DATA (push, 0);
67 PUSH_DATA (push, 0);
68 PUSH_DATA (push, 0);
69 }
70
71 static void
72 nvc0_validate_fb(struct nvc0_context *nvc0)
73 {
74 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
75 struct pipe_framebuffer_state *fb = &nvc0->framebuffer;
76 unsigned i, ms;
77 unsigned ms_mode = NVC0_3D_MULTISAMPLE_MODE_MS1;
78 bool serialize = false;
79
80 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_FB);
81
82 BEGIN_NVC0(push, NVC0_3D(RT_CONTROL), 1);
83 PUSH_DATA (push, (076543210 << 4) | fb->nr_cbufs);
84 BEGIN_NVC0(push, NVC0_3D(SCREEN_SCISSOR_HORIZ), 2);
85 PUSH_DATA (push, fb->width << 16);
86 PUSH_DATA (push, fb->height << 16);
87
88 for (i = 0; i < fb->nr_cbufs; ++i) {
89 struct nv50_surface *sf;
90 struct nv04_resource *res;
91 struct nouveau_bo *bo;
92
93 if (!fb->cbufs[i]) {
94 nvc0_fb_set_null_rt(push, i);
95 continue;
96 }
97
98 sf = nv50_surface(fb->cbufs[i]);
99 res = nv04_resource(sf->base.texture);
100 bo = res->bo;
101
102 BEGIN_NVC0(push, NVC0_3D(RT_ADDRESS_HIGH(i)), 9);
103 PUSH_DATAh(push, res->address + sf->offset);
104 PUSH_DATA (push, res->address + sf->offset);
105 if (likely(nouveau_bo_memtype(bo))) {
106 struct nv50_miptree *mt = nv50_miptree(sf->base.texture);
107
108 assert(sf->base.texture->target != PIPE_BUFFER);
109
110 PUSH_DATA(push, sf->width);
111 PUSH_DATA(push, sf->height);
112 PUSH_DATA(push, nvc0_format_table[sf->base.format].rt);
113 PUSH_DATA(push, (mt->layout_3d << 16) |
114 mt->level[sf->base.u.tex.level].tile_mode);
115 PUSH_DATA(push, sf->base.u.tex.first_layer + sf->depth);
116 PUSH_DATA(push, mt->layer_stride >> 2);
117 PUSH_DATA(push, sf->base.u.tex.first_layer);
118
119 ms_mode = mt->ms_mode;
120 } else {
121 if (res->base.target == PIPE_BUFFER) {
122 PUSH_DATA(push, 262144);
123 PUSH_DATA(push, 1);
124 } else {
125 PUSH_DATA(push, nv50_miptree(sf->base.texture)->level[0].pitch);
126 PUSH_DATA(push, sf->height);
127 }
128 PUSH_DATA(push, nvc0_format_table[sf->base.format].rt);
129 PUSH_DATA(push, 1 << 12);
130 PUSH_DATA(push, 1);
131 PUSH_DATA(push, 0);
132 PUSH_DATA(push, 0);
133
134 nvc0_resource_fence(res, NOUVEAU_BO_WR);
135
136 assert(!fb->zsbuf);
137 }
138
139 if (res->status & NOUVEAU_BUFFER_STATUS_GPU_READING)
140 serialize = true;
141 res->status |= NOUVEAU_BUFFER_STATUS_GPU_WRITING;
142 res->status &= ~NOUVEAU_BUFFER_STATUS_GPU_READING;
143
144 /* only register for writing, otherwise we'd always serialize here */
145 BCTX_REFN(nvc0->bufctx_3d, FB, res, WR);
146 }
147
148 if (fb->zsbuf) {
149 struct nv50_miptree *mt = nv50_miptree(fb->zsbuf->texture);
150 struct nv50_surface *sf = nv50_surface(fb->zsbuf);
151 int unk = mt->base.base.target == PIPE_TEXTURE_2D;
152
153 BEGIN_NVC0(push, NVC0_3D(ZETA_ADDRESS_HIGH), 5);
154 PUSH_DATAh(push, mt->base.address + sf->offset);
155 PUSH_DATA (push, mt->base.address + sf->offset);
156 PUSH_DATA (push, nvc0_format_table[fb->zsbuf->format].rt);
157 PUSH_DATA (push, mt->level[sf->base.u.tex.level].tile_mode);
158 PUSH_DATA (push, mt->layer_stride >> 2);
159 BEGIN_NVC0(push, NVC0_3D(ZETA_ENABLE), 1);
160 PUSH_DATA (push, 1);
161 BEGIN_NVC0(push, NVC0_3D(ZETA_HORIZ), 3);
162 PUSH_DATA (push, sf->width);
163 PUSH_DATA (push, sf->height);
164 PUSH_DATA (push, (unk << 16) |
165 (sf->base.u.tex.first_layer + sf->depth));
166 BEGIN_NVC0(push, NVC0_3D(ZETA_BASE_LAYER), 1);
167 PUSH_DATA (push, sf->base.u.tex.first_layer);
168
169 ms_mode = mt->ms_mode;
170
171 if (mt->base.status & NOUVEAU_BUFFER_STATUS_GPU_READING)
172 serialize = true;
173 mt->base.status |= NOUVEAU_BUFFER_STATUS_GPU_WRITING;
174 mt->base.status &= ~NOUVEAU_BUFFER_STATUS_GPU_READING;
175
176 BCTX_REFN(nvc0->bufctx_3d, FB, &mt->base, WR);
177 } else {
178 BEGIN_NVC0(push, NVC0_3D(ZETA_ENABLE), 1);
179 PUSH_DATA (push, 0);
180 }
181
182 IMMED_NVC0(push, NVC0_3D(MULTISAMPLE_MODE), ms_mode);
183
184 ms = 1 << ms_mode;
185 BEGIN_NVC0(push, NVC0_3D(CB_SIZE), 3);
186 PUSH_DATA (push, 1024);
187 PUSH_DATAh(push, nvc0->screen->uniform_bo->offset + (5 << 16) + (4 << 10));
188 PUSH_DATA (push, nvc0->screen->uniform_bo->offset + (5 << 16) + (4 << 10));
189 BEGIN_1IC0(push, NVC0_3D(CB_POS), 1 + 2 * ms);
190 PUSH_DATA (push, 256 + 128);
191 for (i = 0; i < ms; i++) {
192 float xy[2];
193 nvc0->base.pipe.get_sample_position(&nvc0->base.pipe, ms, i, xy);
194 PUSH_DATAf(push, xy[0]);
195 PUSH_DATAf(push, xy[1]);
196 }
197
198 if (serialize)
199 IMMED_NVC0(push, NVC0_3D(SERIALIZE), 0);
200
201 NOUVEAU_DRV_STAT(&nvc0->screen->base, gpu_serialize_count, serialize);
202 }
203
204 static void
205 nvc0_validate_blend_colour(struct nvc0_context *nvc0)
206 {
207 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
208
209 BEGIN_NVC0(push, NVC0_3D(BLEND_COLOR(0)), 4);
210 PUSH_DATAf(push, nvc0->blend_colour.color[0]);
211 PUSH_DATAf(push, nvc0->blend_colour.color[1]);
212 PUSH_DATAf(push, nvc0->blend_colour.color[2]);
213 PUSH_DATAf(push, nvc0->blend_colour.color[3]);
214 }
215
216 static void
217 nvc0_validate_stencil_ref(struct nvc0_context *nvc0)
218 {
219 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
220 const ubyte *ref = &nvc0->stencil_ref.ref_value[0];
221
222 IMMED_NVC0(push, NVC0_3D(STENCIL_FRONT_FUNC_REF), ref[0]);
223 IMMED_NVC0(push, NVC0_3D(STENCIL_BACK_FUNC_REF), ref[1]);
224 }
225
226 static void
227 nvc0_validate_stipple(struct nvc0_context *nvc0)
228 {
229 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
230 unsigned i;
231
232 BEGIN_NVC0(push, NVC0_3D(POLYGON_STIPPLE_PATTERN(0)), 32);
233 for (i = 0; i < 32; ++i)
234 PUSH_DATA(push, util_bswap32(nvc0->stipple.stipple[i]));
235 }
236
237 static void
238 nvc0_validate_scissor(struct nvc0_context *nvc0)
239 {
240 int i;
241 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
242
243 if (!(nvc0->dirty & NVC0_NEW_SCISSOR) &&
244 nvc0->rast->pipe.scissor == nvc0->state.scissor)
245 return;
246
247 if (nvc0->state.scissor != nvc0->rast->pipe.scissor)
248 nvc0->scissors_dirty = (1 << NVC0_MAX_VIEWPORTS) - 1;
249
250 nvc0->state.scissor = nvc0->rast->pipe.scissor;
251
252 for (i = 0; i < NVC0_MAX_VIEWPORTS; i++) {
253 struct pipe_scissor_state *s = &nvc0->scissors[i];
254 if (!(nvc0->scissors_dirty & (1 << i)))
255 continue;
256
257 BEGIN_NVC0(push, NVC0_3D(SCISSOR_HORIZ(i)), 2);
258 if (nvc0->rast->pipe.scissor) {
259 PUSH_DATA(push, (s->maxx << 16) | s->minx);
260 PUSH_DATA(push, (s->maxy << 16) | s->miny);
261 } else {
262 PUSH_DATA(push, (0xffff << 16) | 0);
263 PUSH_DATA(push, (0xffff << 16) | 0);
264 }
265 }
266 nvc0->scissors_dirty = 0;
267 }
268
269 static void
270 nvc0_validate_viewport(struct nvc0_context *nvc0)
271 {
272 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
273 int x, y, w, h, i;
274 float zmin, zmax;
275
276 for (i = 0; i < NVC0_MAX_VIEWPORTS; i++) {
277 struct pipe_viewport_state *vp = &nvc0->viewports[i];
278
279 if (!(nvc0->viewports_dirty & (1 << i)))
280 continue;
281
282 BEGIN_NVC0(push, NVC0_3D(VIEWPORT_TRANSLATE_X(i)), 3);
283 PUSH_DATAf(push, vp->translate[0]);
284 PUSH_DATAf(push, vp->translate[1]);
285 PUSH_DATAf(push, vp->translate[2]);
286
287 BEGIN_NVC0(push, NVC0_3D(VIEWPORT_SCALE_X(i)), 3);
288 PUSH_DATAf(push, vp->scale[0]);
289 PUSH_DATAf(push, vp->scale[1]);
290 PUSH_DATAf(push, vp->scale[2]);
291
292 /* now set the viewport rectangle to viewport dimensions for clipping */
293
294 x = util_iround(MAX2(0.0f, vp->translate[0] - fabsf(vp->scale[0])));
295 y = util_iround(MAX2(0.0f, vp->translate[1] - fabsf(vp->scale[1])));
296 w = util_iround(vp->translate[0] + fabsf(vp->scale[0])) - x;
297 h = util_iround(vp->translate[1] + fabsf(vp->scale[1])) - y;
298
299 BEGIN_NVC0(push, NVC0_3D(VIEWPORT_HORIZ(i)), 2);
300 PUSH_DATA (push, (w << 16) | x);
301 PUSH_DATA (push, (h << 16) | y);
302
303 zmin = vp->translate[2] - fabsf(vp->scale[2]);
304 zmax = vp->translate[2] + fabsf(vp->scale[2]);
305
306 BEGIN_NVC0(push, NVC0_3D(DEPTH_RANGE_NEAR(i)), 2);
307 PUSH_DATAf(push, zmin);
308 PUSH_DATAf(push, zmax);
309 }
310 nvc0->viewports_dirty = 0;
311 }
312
313 static inline void
314 nvc0_upload_uclip_planes(struct nvc0_context *nvc0, unsigned s)
315 {
316 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
317 struct nouveau_bo *bo = nvc0->screen->uniform_bo;
318
319 BEGIN_NVC0(push, NVC0_3D(CB_SIZE), 3);
320 PUSH_DATA (push, 1024);
321 PUSH_DATAh(push, bo->offset + (5 << 16) + (s << 10));
322 PUSH_DATA (push, bo->offset + (5 << 16) + (s << 10));
323 BEGIN_1IC0(push, NVC0_3D(CB_POS), PIPE_MAX_CLIP_PLANES * 4 + 1);
324 PUSH_DATA (push, 256);
325 PUSH_DATAp(push, &nvc0->clip.ucp[0][0], PIPE_MAX_CLIP_PLANES * 4);
326 }
327
328 static inline void
329 nvc0_check_program_ucps(struct nvc0_context *nvc0,
330 struct nvc0_program *vp, uint8_t mask)
331 {
332 const unsigned n = util_logbase2(mask) + 1;
333
334 if (vp->vp.num_ucps >= n)
335 return;
336 nvc0_program_destroy(nvc0, vp);
337
338 vp->vp.num_ucps = n;
339 if (likely(vp == nvc0->vertprog))
340 nvc0_vertprog_validate(nvc0);
341 else
342 if (likely(vp == nvc0->gmtyprog))
343 nvc0_gmtyprog_validate(nvc0);
344 else
345 nvc0_tevlprog_validate(nvc0);
346 }
347
348 static void
349 nvc0_validate_clip(struct nvc0_context *nvc0)
350 {
351 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
352 struct nvc0_program *vp;
353 unsigned stage;
354 uint8_t clip_enable = nvc0->rast->pipe.clip_plane_enable;
355
356 if (nvc0->gmtyprog) {
357 stage = 3;
358 vp = nvc0->gmtyprog;
359 } else
360 if (nvc0->tevlprog) {
361 stage = 2;
362 vp = nvc0->tevlprog;
363 } else {
364 stage = 0;
365 vp = nvc0->vertprog;
366 }
367
368 if (clip_enable && vp->vp.num_ucps < PIPE_MAX_CLIP_PLANES)
369 nvc0_check_program_ucps(nvc0, vp, clip_enable);
370
371 if (nvc0->dirty & (NVC0_NEW_CLIP | (NVC0_NEW_VERTPROG << stage)))
372 if (vp->vp.num_ucps > 0 && vp->vp.num_ucps <= PIPE_MAX_CLIP_PLANES)
373 nvc0_upload_uclip_planes(nvc0, stage);
374
375 clip_enable &= vp->vp.clip_enable;
376
377 if (nvc0->state.clip_enable != clip_enable) {
378 nvc0->state.clip_enable = clip_enable;
379 IMMED_NVC0(push, NVC0_3D(CLIP_DISTANCE_ENABLE), clip_enable);
380 }
381 if (nvc0->state.clip_mode != vp->vp.clip_mode) {
382 nvc0->state.clip_mode = vp->vp.clip_mode;
383 BEGIN_NVC0(push, NVC0_3D(CLIP_DISTANCE_MODE), 1);
384 PUSH_DATA (push, vp->vp.clip_mode);
385 }
386 }
387
388 static void
389 nvc0_validate_blend(struct nvc0_context *nvc0)
390 {
391 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
392
393 PUSH_SPACE(push, nvc0->blend->size);
394 PUSH_DATAp(push, nvc0->blend->state, nvc0->blend->size);
395 }
396
397 static void
398 nvc0_validate_zsa(struct nvc0_context *nvc0)
399 {
400 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
401
402 PUSH_SPACE(push, nvc0->zsa->size);
403 PUSH_DATAp(push, nvc0->zsa->state, nvc0->zsa->size);
404 }
405
406 static void
407 nvc0_validate_rasterizer(struct nvc0_context *nvc0)
408 {
409 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
410
411 PUSH_SPACE(push, nvc0->rast->size);
412 PUSH_DATAp(push, nvc0->rast->state, nvc0->rast->size);
413 }
414
415 static void
416 nvc0_constbufs_validate(struct nvc0_context *nvc0)
417 {
418 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
419 unsigned s;
420
421 for (s = 0; s < 5; ++s) {
422 while (nvc0->constbuf_dirty[s]) {
423 int i = ffs(nvc0->constbuf_dirty[s]) - 1;
424 nvc0->constbuf_dirty[s] &= ~(1 << i);
425
426 if (nvc0->constbuf[s][i].user) {
427 struct nouveau_bo *bo = nvc0->screen->uniform_bo;
428 const unsigned base = s << 16;
429 const unsigned size = nvc0->constbuf[s][0].size;
430 assert(i == 0); /* we really only want OpenGL uniforms here */
431 assert(nvc0->constbuf[s][0].u.data);
432
433 if (nvc0->state.uniform_buffer_bound[s] < size) {
434 nvc0->state.uniform_buffer_bound[s] = align(size, 0x100);
435
436 BEGIN_NVC0(push, NVC0_3D(CB_SIZE), 3);
437 PUSH_DATA (push, nvc0->state.uniform_buffer_bound[s]);
438 PUSH_DATAh(push, bo->offset + base);
439 PUSH_DATA (push, bo->offset + base);
440 BEGIN_NVC0(push, NVC0_3D(CB_BIND(s)), 1);
441 PUSH_DATA (push, (0 << 4) | 1);
442 }
443 nvc0_cb_bo_push(&nvc0->base, bo, NV_VRAM_DOMAIN(&nvc0->screen->base),
444 base, nvc0->state.uniform_buffer_bound[s],
445 0, (size + 3) / 4,
446 nvc0->constbuf[s][0].u.data);
447 } else {
448 struct nv04_resource *res =
449 nv04_resource(nvc0->constbuf[s][i].u.buf);
450 if (res) {
451 BEGIN_NVC0(push, NVC0_3D(CB_SIZE), 3);
452 PUSH_DATA (push, nvc0->constbuf[s][i].size);
453 PUSH_DATAh(push, res->address + nvc0->constbuf[s][i].offset);
454 PUSH_DATA (push, res->address + nvc0->constbuf[s][i].offset);
455 BEGIN_NVC0(push, NVC0_3D(CB_BIND(s)), 1);
456 PUSH_DATA (push, (i << 4) | 1);
457
458 BCTX_REFN(nvc0->bufctx_3d, CB(s, i), res, RD);
459
460 nvc0->cb_dirty = 1; /* Force cache flush for UBO. */
461 res->cb_bindings[s] |= 1 << i;
462 } else {
463 BEGIN_NVC0(push, NVC0_3D(CB_BIND(s)), 1);
464 PUSH_DATA (push, (i << 4) | 0);
465 }
466 if (i == 0)
467 nvc0->state.uniform_buffer_bound[s] = 0;
468 }
469 }
470 }
471 }
472
473 static void
474 nvc0_validate_buffers(struct nvc0_context *nvc0)
475 {
476 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
477 int i, s;
478
479 for (s = 0; s < 5; s++) {
480 BEGIN_NVC0(push, NVC0_3D(CB_SIZE), 3);
481 PUSH_DATA (push, 1024);
482 PUSH_DATAh(push, nvc0->screen->uniform_bo->offset + (5 << 16) + (s << 10));
483 PUSH_DATA (push, nvc0->screen->uniform_bo->offset + (5 << 16) + (s << 10));
484 BEGIN_1IC0(push, NVC0_3D(CB_POS), 1 + 4 * NVC0_MAX_BUFFERS);
485 PUSH_DATA (push, 512);
486 for (i = 0; i < NVC0_MAX_BUFFERS; i++) {
487 if (nvc0->buffers[s][i].buffer) {
488 struct nv04_resource *res =
489 nv04_resource(nvc0->buffers[s][i].buffer);
490 PUSH_DATA (push, res->address + nvc0->buffers[s][i].buffer_offset);
491 PUSH_DATAh(push, res->address + nvc0->buffers[s][i].buffer_offset);
492 PUSH_DATA (push, nvc0->buffers[s][i].buffer_size);
493 PUSH_DATA (push, 0);
494 BCTX_REFN(nvc0->bufctx_3d, BUF, res, RDWR);
495 } else {
496 PUSH_DATA (push, 0);
497 PUSH_DATA (push, 0);
498 PUSH_DATA (push, 0);
499 PUSH_DATA (push, 0);
500 }
501 }
502 }
503
504 }
505
506 static void
507 nvc0_validate_sample_mask(struct nvc0_context *nvc0)
508 {
509 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
510
511 unsigned mask[4] =
512 {
513 nvc0->sample_mask & 0xffff,
514 nvc0->sample_mask & 0xffff,
515 nvc0->sample_mask & 0xffff,
516 nvc0->sample_mask & 0xffff
517 };
518
519 BEGIN_NVC0(push, NVC0_3D(MSAA_MASK(0)), 4);
520 PUSH_DATA (push, mask[0]);
521 PUSH_DATA (push, mask[1]);
522 PUSH_DATA (push, mask[2]);
523 PUSH_DATA (push, mask[3]);
524 }
525
526 static void
527 nvc0_validate_min_samples(struct nvc0_context *nvc0)
528 {
529 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
530 int samples;
531
532 samples = util_next_power_of_two(nvc0->min_samples);
533 if (samples > 1)
534 samples |= NVC0_3D_SAMPLE_SHADING_ENABLE;
535
536 IMMED_NVC0(push, NVC0_3D(SAMPLE_SHADING), samples);
537 }
538
539 void
540 nvc0_validate_global_residents(struct nvc0_context *nvc0,
541 struct nouveau_bufctx *bctx, int bin)
542 {
543 unsigned i;
544
545 for (i = 0; i < nvc0->global_residents.size / sizeof(struct pipe_resource *);
546 ++i) {
547 struct pipe_resource *res = *util_dynarray_element(
548 &nvc0->global_residents, struct pipe_resource *, i);
549 if (res)
550 nvc0_add_resident(bctx, bin, nv04_resource(res), NOUVEAU_BO_RDWR);
551 }
552 }
553
554 static void
555 nvc0_validate_derived_1(struct nvc0_context *nvc0)
556 {
557 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
558 bool rasterizer_discard;
559
560 if (nvc0->rast && nvc0->rast->pipe.rasterizer_discard) {
561 rasterizer_discard = true;
562 } else {
563 bool zs = nvc0->zsa &&
564 (nvc0->zsa->pipe.depth.enabled || nvc0->zsa->pipe.stencil[0].enabled);
565 rasterizer_discard = !zs &&
566 (!nvc0->fragprog || !nvc0->fragprog->hdr[18]);
567 }
568
569 if (rasterizer_discard != nvc0->state.rasterizer_discard) {
570 nvc0->state.rasterizer_discard = rasterizer_discard;
571 IMMED_NVC0(push, NVC0_3D(RASTERIZE_ENABLE), !rasterizer_discard);
572 }
573 }
574
575 /* alpha test is disabled if there are no color RTs, so make sure we have at
576 * least one if alpha test is enabled. Note that this must run after
577 * nvc0_validate_fb, otherwise that will override the RT count setting.
578 */
579 static void
580 nvc0_validate_derived_2(struct nvc0_context *nvc0)
581 {
582 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
583
584 if (nvc0->zsa && nvc0->zsa->pipe.alpha.enabled &&
585 nvc0->framebuffer.nr_cbufs == 0) {
586 nvc0_fb_set_null_rt(push, 0);
587 BEGIN_NVC0(push, NVC0_3D(RT_CONTROL), 1);
588 PUSH_DATA (push, (076543210 << 4) | 1);
589 }
590 }
591
592 static void
593 nvc0_validate_derived_3(struct nvc0_context *nvc0)
594 {
595 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
596 struct pipe_framebuffer_state *fb = &nvc0->framebuffer;
597 uint32_t ms = 0;
598
599 if ((!fb->nr_cbufs || !fb->cbufs[0] ||
600 !util_format_is_pure_integer(fb->cbufs[0]->format)) && nvc0->blend) {
601 if (nvc0->blend->pipe.alpha_to_coverage)
602 ms |= NVC0_3D_MULTISAMPLE_CTRL_ALPHA_TO_COVERAGE;
603 if (nvc0->blend->pipe.alpha_to_one)
604 ms |= NVC0_3D_MULTISAMPLE_CTRL_ALPHA_TO_ONE;
605 }
606
607 BEGIN_NVC0(push, NVC0_3D(MULTISAMPLE_CTRL), 1);
608 PUSH_DATA (push, ms);
609 }
610
611 static void
612 nvc0_validate_tess_state(struct nvc0_context *nvc0)
613 {
614 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
615
616 BEGIN_NVC0(push, NVC0_3D(TESS_LEVEL_OUTER(0)), 6);
617 PUSH_DATAp(push, nvc0->default_tess_outer, 4);
618 PUSH_DATAp(push, nvc0->default_tess_inner, 2);
619 }
620
621 static void
622 nvc0_switch_pipe_context(struct nvc0_context *ctx_to)
623 {
624 struct nvc0_context *ctx_from = ctx_to->screen->cur_ctx;
625 unsigned s;
626
627 if (ctx_from)
628 ctx_to->state = ctx_from->state;
629 else
630 ctx_to->state = ctx_to->screen->save_state;
631
632 ctx_to->dirty = ~0;
633 ctx_to->viewports_dirty = ~0;
634 ctx_to->scissors_dirty = ~0;
635
636 for (s = 0; s < 5; ++s) {
637 ctx_to->samplers_dirty[s] = ~0;
638 ctx_to->textures_dirty[s] = ~0;
639 ctx_to->constbuf_dirty[s] = (1 << NVC0_MAX_PIPE_CONSTBUFS) - 1;
640 }
641
642 /* Reset tfb as the shader that owns it may have been deleted. */
643 ctx_to->state.tfb = NULL;
644
645 if (!ctx_to->vertex)
646 ctx_to->dirty &= ~(NVC0_NEW_VERTEX | NVC0_NEW_ARRAYS);
647 if (!ctx_to->idxbuf.buffer)
648 ctx_to->dirty &= ~NVC0_NEW_IDXBUF;
649
650 if (!ctx_to->vertprog)
651 ctx_to->dirty &= ~NVC0_NEW_VERTPROG;
652 if (!ctx_to->fragprog)
653 ctx_to->dirty &= ~NVC0_NEW_FRAGPROG;
654
655 if (!ctx_to->blend)
656 ctx_to->dirty &= ~NVC0_NEW_BLEND;
657 if (!ctx_to->rast)
658 ctx_to->dirty &= ~(NVC0_NEW_RASTERIZER | NVC0_NEW_SCISSOR);
659 if (!ctx_to->zsa)
660 ctx_to->dirty &= ~NVC0_NEW_ZSA;
661
662 ctx_to->screen->cur_ctx = ctx_to;
663 }
664
665 static struct state_validate {
666 void (*func)(struct nvc0_context *);
667 uint32_t states;
668 } validate_list[] = {
669 { nvc0_validate_fb, NVC0_NEW_FRAMEBUFFER },
670 { nvc0_validate_blend, NVC0_NEW_BLEND },
671 { nvc0_validate_zsa, NVC0_NEW_ZSA },
672 { nvc0_validate_sample_mask, NVC0_NEW_SAMPLE_MASK },
673 { nvc0_validate_rasterizer, NVC0_NEW_RASTERIZER },
674 { nvc0_validate_blend_colour, NVC0_NEW_BLEND_COLOUR },
675 { nvc0_validate_stencil_ref, NVC0_NEW_STENCIL_REF },
676 { nvc0_validate_stipple, NVC0_NEW_STIPPLE },
677 { nvc0_validate_scissor, NVC0_NEW_SCISSOR | NVC0_NEW_RASTERIZER },
678 { nvc0_validate_viewport, NVC0_NEW_VIEWPORT },
679 { nvc0_vertprog_validate, NVC0_NEW_VERTPROG },
680 { nvc0_tctlprog_validate, NVC0_NEW_TCTLPROG },
681 { nvc0_tevlprog_validate, NVC0_NEW_TEVLPROG },
682 { nvc0_validate_tess_state, NVC0_NEW_TESSFACTOR },
683 { nvc0_gmtyprog_validate, NVC0_NEW_GMTYPROG },
684 { nvc0_fragprog_validate, NVC0_NEW_FRAGPROG | NVC0_NEW_RASTERIZER },
685 { nvc0_validate_derived_1, NVC0_NEW_FRAGPROG | NVC0_NEW_ZSA |
686 NVC0_NEW_RASTERIZER },
687 { nvc0_validate_derived_2, NVC0_NEW_ZSA | NVC0_NEW_FRAMEBUFFER },
688 { nvc0_validate_derived_3, NVC0_NEW_BLEND | NVC0_NEW_FRAMEBUFFER },
689 { nvc0_validate_clip, NVC0_NEW_CLIP | NVC0_NEW_RASTERIZER |
690 NVC0_NEW_VERTPROG |
691 NVC0_NEW_TEVLPROG |
692 NVC0_NEW_GMTYPROG },
693 { nvc0_constbufs_validate, NVC0_NEW_CONSTBUF },
694 { nvc0_validate_textures, NVC0_NEW_TEXTURES },
695 { nvc0_validate_samplers, NVC0_NEW_SAMPLERS },
696 { nve4_set_tex_handles, NVC0_NEW_TEXTURES | NVC0_NEW_SAMPLERS },
697 { nvc0_vertex_arrays_validate, NVC0_NEW_VERTEX | NVC0_NEW_ARRAYS },
698 { nvc0_validate_surfaces, NVC0_NEW_SURFACES },
699 { nvc0_validate_buffers, NVC0_NEW_BUFFERS },
700 { nvc0_idxbuf_validate, NVC0_NEW_IDXBUF },
701 { nvc0_tfb_validate, NVC0_NEW_TFB_TARGETS | NVC0_NEW_GMTYPROG },
702 { nvc0_validate_min_samples, NVC0_NEW_MIN_SAMPLES },
703 };
704
705 bool
706 nvc0_state_validate(struct nvc0_context *nvc0, uint32_t mask, unsigned words)
707 {
708 uint32_t state_mask;
709 int ret;
710 unsigned i;
711
712 if (nvc0->screen->cur_ctx != nvc0)
713 nvc0_switch_pipe_context(nvc0);
714
715 state_mask = nvc0->dirty & mask;
716
717 if (state_mask) {
718 for (i = 0; i < ARRAY_SIZE(validate_list); ++i) {
719 struct state_validate *validate = &validate_list[i];
720
721 if (state_mask & validate->states)
722 validate->func(nvc0);
723 }
724 nvc0->dirty &= ~state_mask;
725
726 nvc0_bufctx_fence(nvc0, nvc0->bufctx_3d, false);
727 }
728
729 nouveau_pushbuf_bufctx(nvc0->base.pushbuf, nvc0->bufctx_3d);
730 ret = nouveau_pushbuf_validate(nvc0->base.pushbuf);
731
732 if (unlikely(nvc0->state.flushed)) {
733 nvc0->state.flushed = false;
734 nvc0_bufctx_fence(nvc0, nvc0->bufctx_3d, true);
735 }
736 return !ret;
737 }