Debian Codegraph @

src/gallium/drivers/nouveau/nvc0/nvc0_state_validate.c

raw
1 #include "util/format/u_format.h"
2 #include "util/u_framebuffer.h"
3 #include "util/u_math.h"
4 #include "util/u_viewport.h"
5
6 #include "nvc0/nvc0_context.h"
7
8 static inline void
9 nvc0_fb_set_null_rt(struct nouveau_pushbuf *push, unsigned i, unsigned layers)
10 {
11 BEGIN_NVC0(push, NVC0_3D(RT_ADDRESS_HIGH(i)), 9);
12 PUSH_DATA (push, 0);
13 PUSH_DATA (push, 0);
14 PUSH_DATA (push, 64); // width
15 PUSH_DATA (push, 0); // height
16 PUSH_DATA (push, 0); // format
17 PUSH_DATA (push, 0); // tile mode
18 PUSH_DATA (push, layers); // layers
19 PUSH_DATA (push, 0); // layer stride
20 PUSH_DATA (push, 0); // base layer
21 }
22
23 static uint32_t
24 gm200_encode_cb_sample_location(uint8_t x, uint8_t y)
25 {
26 static const uint8_t lut[] = {
27 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf,
28 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7};
29 uint32_t result = 0;
30 /* S0.12 representation for TGSI_OPCODE_INTERP_SAMPLE */
31 result |= lut[x] << 8 | lut[y] << 24;
32 /* fill in gaps with data in a representation for SV_SAMPLE_POS */
33 result |= x << 12 | y << 28;
34 return result;
35 }
36
37 static void
38 gm200_validate_sample_locations(struct nvc0_context *nvc0, unsigned ms)
39 {
40 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
41 struct nvc0_screen *screen = nvc0->screen;
42 unsigned grid_width, grid_height, hw_grid_width;
43 uint8_t sample_locations[16][2];
44 unsigned cb[64];
45 unsigned i, pixel, pixel_y, pixel_x, sample;
46 uint32_t packed_locations[4] = {};
47
48 screen->base.base.get_sample_pixel_grid(
49 &screen->base.base, ms, &grid_width, &grid_height);
50
51 hw_grid_width = grid_width;
52 if (ms == 1) /* get_sample_pixel_grid() exposes 2x4 for 1x msaa */
53 hw_grid_width = 4;
54
55 if (nvc0->sample_locations_enabled) {
56 uint8_t locations[2 * 4 * 8];
57 memcpy(locations, nvc0->sample_locations, sizeof(locations));
58 util_sample_locations_flip_y(
59 &screen->base.base, nvc0->framebuffer.height, ms, locations);
60
61 for (pixel = 0; pixel < hw_grid_width*grid_height; pixel++) {
62 for (sample = 0; sample < ms; sample++) {
63 unsigned pixel_x = pixel % hw_grid_width;
64 unsigned pixel_y = pixel / hw_grid_width;
65 unsigned wi = pixel * ms + sample;
66 unsigned ri = (pixel_y * grid_width + pixel_x % grid_width);
67 ri = ri * ms + sample;
68 sample_locations[wi][0] = locations[ri] & 0xf;
69 sample_locations[wi][1] = locations[ri] >> 4;
70 }
71 }
72 } else {
73 const uint8_t (*ptr)[2] = nvc0_get_sample_locations(ms);
74 for (i = 0; i < 16; i++) {
75 sample_locations[i][0] = ptr[i % ms][0];
76 sample_locations[i][1] = ptr[i % ms][1];
77 }
78 }
79
80 BEGIN_NVC0(push, NVC0_3D(CB_SIZE), 3);
81 PUSH_DATA (push, NVC0_CB_AUX_SIZE);
82 PUSH_DATAh(push, screen->uniform_bo->offset + NVC0_CB_AUX_INFO(4));
83 PUSH_DATA (push, screen->uniform_bo->offset + NVC0_CB_AUX_INFO(4));
84 BEGIN_1IC0(push, NVC0_3D(CB_POS), 1 + 64);
85 PUSH_DATA (push, NVC0_CB_AUX_SAMPLE_INFO);
86 for (pixel_y = 0; pixel_y < 4; pixel_y++) {
87 for (pixel_x = 0; pixel_x < 2; pixel_x++) {
88 for (sample = 0; sample < ms; sample++) {
89 unsigned write_index = (pixel_y * 2 + pixel_x) * 8 + sample;
90 unsigned read_index = pixel_y % grid_height * hw_grid_width;
91 uint8_t x, y;
92 read_index += pixel_x % grid_width;
93 read_index = read_index * ms + sample;
94 x = sample_locations[read_index][0];
95 y = sample_locations[read_index][1];
96 cb[write_index] = gm200_encode_cb_sample_location(x, y);
97 }
98 }
99 }
100 PUSH_DATAp(push, cb, 64);
101
102 for (i = 0; i < 16; i++) {
103 packed_locations[i / 4] |= sample_locations[i][0] << ((i % 4) * 8);
104 packed_locations[i / 4] |= sample_locations[i][1] << ((i % 4) * 8 + 4);
105 }
106
107 BEGIN_NVC0(push, SUBC_3D(0x11e0), 4);
108 PUSH_DATAp(push, packed_locations, 4);
109 }
110
111 static void
112 nvc0_validate_sample_locations(struct nvc0_context *nvc0, unsigned ms)
113 {
114 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
115 struct nvc0_screen *screen = nvc0->screen;
116 unsigned i;
117
118 BEGIN_NVC0(push, NVC0_3D(CB_SIZE), 3);
119 PUSH_DATA (push, NVC0_CB_AUX_SIZE);
120 PUSH_DATAh(push, screen->uniform_bo->offset + NVC0_CB_AUX_INFO(4));
121 PUSH_DATA (push, screen->uniform_bo->offset + NVC0_CB_AUX_INFO(4));
122 BEGIN_1IC0(push, NVC0_3D(CB_POS), 1 + 2 * ms);
123 PUSH_DATA (push, NVC0_CB_AUX_SAMPLE_INFO);
124 for (i = 0; i < ms; i++) {
125 float xy[2];
126 nvc0->base.pipe.get_sample_position(&nvc0->base.pipe, ms, i, xy);
127 PUSH_DATAf(push, xy[0]);
128 PUSH_DATAf(push, xy[1]);
129 }
130 }
131
132 static void
133 validate_sample_locations(struct nvc0_context *nvc0)
134 {
135 unsigned ms = util_framebuffer_get_num_samples(&nvc0->framebuffer);
136
137 if (nvc0->screen->base.class_3d >= GM200_3D_CLASS)
138 gm200_validate_sample_locations(nvc0, ms);
139 else
140 nvc0_validate_sample_locations(nvc0, ms);
141 }
142
143 static void
144 nvc0_validate_fb(struct nvc0_context *nvc0)
145 {
146 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
147 struct pipe_framebuffer_state *fb = &nvc0->framebuffer;
148 unsigned i;
149 unsigned ms_mode = NVC0_3D_MULTISAMPLE_MODE_MS1;
150 unsigned nr_cbufs = fb->nr_cbufs;
151 bool serialize = false;
152 bool cbuf_is_linear = false;
153
154 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_FB);
155
156 BEGIN_NVC0(push, NVC0_3D(SCREEN_SCISSOR_HORIZ), 2);
157 PUSH_DATA (push, fb->width << 16);
158 PUSH_DATA (push, fb->height << 16);
159
160 for (i = 0; i < fb->nr_cbufs; ++i) {
161 struct nv50_surface *sf;
162 struct nv04_resource *res;
163 struct nouveau_bo *bo;
164
165 if (!fb->cbufs[i].texture) {
166 nvc0_fb_set_null_rt(push, i, 0);
167 continue;
168 }
169
170 sf = nv50_surface(nvc0->fb_cbufs[i]);
171 res = nv04_resource(sf->base.texture);
172 bo = res->bo;
173
174 BEGIN_NVC0(push, NVC0_3D(RT_ADDRESS_HIGH(i)), 9);
175 PUSH_DATAh(push, res->address + sf->offset);
176 PUSH_DATA (push, res->address + sf->offset);
177 if (likely(nouveau_bo_memtype(bo))) {
178 struct nv50_miptree *mt = nv50_miptree(sf->base.texture);
179
180 assert(sf->base.texture->target != PIPE_BUFFER);
181
182 PUSH_DATA(push, sf->width);
183 PUSH_DATA(push, sf->height);
184 PUSH_DATA(push, nvc0_format_table[sf->base.format].rt);
185 PUSH_DATA(push, (mt->layout_3d << 16) |
186 mt->level[sf->base.level].tile_mode);
187 PUSH_DATA(push, sf->base.first_layer + sf->depth);
188 PUSH_DATA(push, mt->layer_stride >> 2);
189 PUSH_DATA(push, sf->base.first_layer);
190
191 ms_mode = mt->ms_mode;
192 } else {
193 if (res->base.target == PIPE_BUFFER) {
194 PUSH_DATA(push, 262144);
195 PUSH_DATA(push, 1);
196 } else {
197 PUSH_DATA(push, nv50_miptree(sf->base.texture)->level[0].pitch);
198 PUSH_DATA(push, sf->height);
199 }
200 PUSH_DATA(push, nvc0_format_table[sf->base.format].rt);
201 PUSH_DATA(push, 1 << 12);
202 PUSH_DATA(push, 1);
203 PUSH_DATA(push, 0);
204 PUSH_DATA(push, 0);
205
206 nvc0_resource_fence(nvc0, res, NOUVEAU_BO_WR);
207
208 cbuf_is_linear = true;
209 }
210
211 if (res->status & NOUVEAU_BUFFER_STATUS_GPU_READING)
212 serialize = true;
213 res->status |= NOUVEAU_BUFFER_STATUS_GPU_WRITING;
214 res->status &= ~NOUVEAU_BUFFER_STATUS_GPU_READING;
215
216 /* only register for writing, otherwise we'd always serialize here */
217 BCTX_REFN(nvc0->bufctx_3d, 3D_FB, res, WR);
218 }
219
220 if (fb->zsbuf.texture && !cbuf_is_linear) {
221 struct nv50_miptree *mt = nv50_miptree(fb->zsbuf.texture);
222 struct nv50_surface *sf = nv50_surface(nvc0->fb_zsbuf);
223 int unk = mt->base.base.target == PIPE_TEXTURE_2D;
224
225 BEGIN_NVC0(push, NVC0_3D(ZETA_ADDRESS_HIGH), 5);
226 PUSH_DATAh(push, mt->base.address + sf->offset);
227 PUSH_DATA (push, mt->base.address + sf->offset);
228 PUSH_DATA (push, nvc0_format_table[fb->zsbuf.format].rt);
229 PUSH_DATA (push, mt->level[sf->base.level].tile_mode);
230 PUSH_DATA (push, mt->layer_stride >> 2);
231 BEGIN_NVC0(push, NVC0_3D(ZETA_ENABLE), 1);
232 PUSH_DATA (push, 1);
233 BEGIN_NVC0(push, NVC0_3D(ZETA_HORIZ), 3);
234 PUSH_DATA (push, sf->width);
235 PUSH_DATA (push, sf->height);
236 PUSH_DATA (push, (unk << 16) |
237 (sf->base.first_layer + sf->depth));
238 BEGIN_NVC0(push, NVC0_3D(ZETA_BASE_LAYER), 1);
239 PUSH_DATA (push, sf->base.first_layer);
240
241 ms_mode = mt->ms_mode;
242
243 if (mt->base.status & NOUVEAU_BUFFER_STATUS_GPU_READING)
244 serialize = true;
245 mt->base.status |= NOUVEAU_BUFFER_STATUS_GPU_WRITING;
246 mt->base.status &= ~NOUVEAU_BUFFER_STATUS_GPU_READING;
247
248 BCTX_REFN(nvc0->bufctx_3d, 3D_FB, &mt->base, WR);
249 } else {
250 BEGIN_NVC0(push, NVC0_3D(ZETA_ENABLE), 1);
251 PUSH_DATA (push, 0);
252 }
253
254 if (nr_cbufs == 0 && !fb->zsbuf.texture) {
255 assert(util_is_power_of_two_or_zero(fb->samples));
256 assert(fb->samples <= 8);
257
258 nvc0_fb_set_null_rt(push, 0, fb->layers);
259
260 if (fb->samples > 1)
261 ms_mode = ffs(fb->samples) - 1;
262 nr_cbufs = 1;
263 }
264
265 BEGIN_NVC0(push, NVC0_3D(RT_CONTROL), 1);
266 PUSH_DATA (push, (076543210 << 4) | nr_cbufs);
267 IMMED_NVC0(push, NVC0_3D(MULTISAMPLE_MODE), ms_mode);
268
269 if (serialize)
270 IMMED_NVC0(push, NVC0_3D(SERIALIZE), 0);
271
272 NOUVEAU_DRV_STAT(&nvc0->screen->base, gpu_serialize_count, serialize);
273 }
274
275 static void
276 nvc0_validate_blend_colour(struct nvc0_context *nvc0)
277 {
278 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
279
280 BEGIN_NVC0(push, NVC0_3D(BLEND_COLOR(0)), 4);
281 PUSH_DATAf(push, nvc0->blend_colour.color[0]);
282 PUSH_DATAf(push, nvc0->blend_colour.color[1]);
283 PUSH_DATAf(push, nvc0->blend_colour.color[2]);
284 PUSH_DATAf(push, nvc0->blend_colour.color[3]);
285 }
286
287 static void
288 nvc0_validate_stencil_ref(struct nvc0_context *nvc0)
289 {
290 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
291 const uint8_t *ref = &nvc0->stencil_ref.ref_value[0];
292
293 IMMED_NVC0(push, NVC0_3D(STENCIL_FRONT_FUNC_REF), ref[0]);
294 IMMED_NVC0(push, NVC0_3D(STENCIL_BACK_FUNC_REF), ref[1]);
295 }
296
297 static void
298 nvc0_validate_stipple(struct nvc0_context *nvc0)
299 {
300 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
301 unsigned i;
302
303 BEGIN_NVC0(push, NVC0_3D(POLYGON_STIPPLE_PATTERN(0)), 32);
304 for (i = 0; i < 32; ++i)
305 PUSH_DATA(push, util_bswap32(nvc0->stipple.stipple[i]));
306 }
307
308 static void
309 nvc0_validate_scissor(struct nvc0_context *nvc0)
310 {
311 int i;
312 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
313
314 if (!(nvc0->dirty_3d & NVC0_NEW_3D_SCISSOR) &&
315 nvc0->rast->pipe.scissor == nvc0->state.scissor)
316 return;
317
318 if (nvc0->state.scissor != nvc0->rast->pipe.scissor)
319 nvc0->scissors_dirty = (1 << NVC0_MAX_VIEWPORTS) - 1;
320
321 nvc0->state.scissor = nvc0->rast->pipe.scissor;
322
323 for (i = 0; i < NVC0_MAX_VIEWPORTS; i++) {
324 struct pipe_scissor_state *s = &nvc0->scissors[i];
325 if (!(nvc0->scissors_dirty & (1 << i)))
326 continue;
327
328 BEGIN_NVC0(push, NVC0_3D(SCISSOR_HORIZ(i)), 2);
329 if (nvc0->rast->pipe.scissor) {
330 PUSH_DATA(push, (s->maxx << 16) | s->minx);
331 PUSH_DATA(push, (s->maxy << 16) | s->miny);
332 } else {
333 PUSH_DATA(push, (0xffff << 16) | 0);
334 PUSH_DATA(push, (0xffff << 16) | 0);
335 }
336 }
337 nvc0->scissors_dirty = 0;
338 }
339
340 static void
341 nvc0_validate_viewport(struct nvc0_context *nvc0)
342 {
343 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
344 uint16_t class_3d = nvc0->screen->base.class_3d;
345 int x, y, w, h, i;
346 float zmin, zmax;
347
348 for (i = 0; i < NVC0_MAX_VIEWPORTS; i++) {
349 struct pipe_viewport_state *vp = &nvc0->viewports[i];
350
351 if (!(nvc0->viewports_dirty & (1 << i)))
352 continue;
353
354 BEGIN_NVC0(push, NVC0_3D(VIEWPORT_TRANSLATE_X(i)), 3);
355 PUSH_DATAf(push, vp->translate[0]);
356 PUSH_DATAf(push, vp->translate[1]);
357 PUSH_DATAf(push, vp->translate[2]);
358
359 BEGIN_NVC0(push, NVC0_3D(VIEWPORT_SCALE_X(i)), 3);
360 PUSH_DATAf(push, vp->scale[0]);
361 PUSH_DATAf(push, vp->scale[1]);
362 PUSH_DATAf(push, vp->scale[2]);
363
364 /* now set the viewport rectangle to viewport dimensions for clipping */
365
366 x = util_iround(MAX2(0.0f, vp->translate[0] - fabsf(vp->scale[0])));
367 y = util_iround(MAX2(0.0f, vp->translate[1] - fabsf(vp->scale[1])));
368 w = util_iround(vp->translate[0] + fabsf(vp->scale[0])) - x;
369 h = util_iround(vp->translate[1] + fabsf(vp->scale[1])) - y;
370
371 BEGIN_NVC0(push, NVC0_3D(VIEWPORT_HORIZ(i)), 2);
372 PUSH_DATA (push, (w << 16) | x);
373 PUSH_DATA (push, (h << 16) | y);
374
375 /* If the halfz setting ever changes, the viewports will also get
376 * updated. The rast will get updated before the validate function has a
377 * chance to hit, so we can just use it directly without an atom
378 * dependency.
379 */
380 util_viewport_zmin_zmax(vp, nvc0->rast->pipe.clip_halfz, &zmin, &zmax);
381
382 BEGIN_NVC0(push, NVC0_3D(DEPTH_RANGE_NEAR(i)), 2);
383 PUSH_DATAf(push, zmin);
384 PUSH_DATAf(push, zmax);
385
386 if (class_3d >= GM200_3D_CLASS) {
387 BEGIN_NVC0(push, NVC0_3D(VIEWPORT_SWIZZLE(i)), 1);
388 PUSH_DATA (push, vp->swizzle_x << 0 |
389 vp->swizzle_y << 4 |
390 vp->swizzle_z << 8 |
391 vp->swizzle_w << 12);
392 }
393 }
394 nvc0->viewports_dirty = 0;
395 }
396
397 static void
398 nvc0_validate_window_rects(struct nvc0_context *nvc0)
399 {
400 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
401 bool enable = nvc0->window_rect.rects > 0 || nvc0->window_rect.inclusive;
402 int i;
403
404 IMMED_NVC0(push, NVC0_3D(CLIP_RECTS_EN), enable);
405 if (!enable)
406 return;
407
408 IMMED_NVC0(push, NVC0_3D(CLIP_RECTS_MODE), !nvc0->window_rect.inclusive);
409 BEGIN_NVC0(push, NVC0_3D(CLIP_RECT_HORIZ(0)), NVC0_MAX_WINDOW_RECTANGLES * 2);
410 for (i = 0; i < nvc0->window_rect.rects; i++) {
411 struct pipe_scissor_state *s = &nvc0->window_rect.rect[i];
412 PUSH_DATA(push, (s->maxx << 16) | s->minx);
413 PUSH_DATA(push, (s->maxy << 16) | s->miny);
414 }
415 for (; i < NVC0_MAX_WINDOW_RECTANGLES; i++) {
416 PUSH_DATA(push, 0);
417 PUSH_DATA(push, 0);
418 }
419 }
420
421 static inline void
422 nvc0_upload_uclip_planes(struct nvc0_context *nvc0, unsigned s)
423 {
424 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
425 struct nvc0_screen *screen = nvc0->screen;
426
427 BEGIN_NVC0(push, NVC0_3D(CB_SIZE), 3);
428 PUSH_DATA (push, NVC0_CB_AUX_SIZE);
429 PUSH_DATAh(push, screen->uniform_bo->offset + NVC0_CB_AUX_INFO(s));
430 PUSH_DATA (push, screen->uniform_bo->offset + NVC0_CB_AUX_INFO(s));
431 BEGIN_1IC0(push, NVC0_3D(CB_POS), PIPE_MAX_CLIP_PLANES * 4 + 1);
432 PUSH_DATA (push, NVC0_CB_AUX_UCP_INFO);
433 PUSH_DATAp(push, &nvc0->clip.ucp[0][0], PIPE_MAX_CLIP_PLANES * 4);
434 }
435
436 static inline void
437 nvc0_check_program_ucps(struct nvc0_context *nvc0,
438 struct nvc0_program *vp, uint8_t mask)
439 {
440 const unsigned n = util_logbase2(mask) + 1;
441
442 if (vp->vp.num_ucps >= n)
443 return;
444 nvc0_program_destroy(nvc0, vp);
445
446 vp->vp.num_ucps = n;
447 if (likely(vp == nvc0->vertprog))
448 nvc0_vertprog_validate(nvc0);
449 else
450 if (likely(vp == nvc0->gmtyprog))
451 nvc0_gmtyprog_validate(nvc0);
452 else
453 nvc0_tevlprog_validate(nvc0);
454 }
455
456 static void
457 nvc0_validate_clip(struct nvc0_context *nvc0)
458 {
459 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
460 struct nvc0_program *vp;
461 unsigned stage;
462 uint8_t clip_enable = nvc0->rast->pipe.clip_plane_enable;
463
464 if (nvc0->gmtyprog) {
465 stage = 3;
466 vp = nvc0->gmtyprog;
467 } else
468 if (nvc0->tevlprog) {
469 stage = 2;
470 vp = nvc0->tevlprog;
471 } else {
472 stage = 0;
473 vp = nvc0->vertprog;
474 }
475
476 if (clip_enable && vp->vp.num_ucps < PIPE_MAX_CLIP_PLANES)
477 nvc0_check_program_ucps(nvc0, vp, clip_enable);
478
479 if (nvc0->dirty_3d & (NVC0_NEW_3D_CLIP | (NVC0_NEW_3D_VERTPROG << stage)))
480 if (vp->vp.num_ucps > 0 && vp->vp.num_ucps <= PIPE_MAX_CLIP_PLANES)
481 nvc0_upload_uclip_planes(nvc0, stage);
482
483 clip_enable &= vp->vp.clip_enable;
484 clip_enable |= vp->vp.cull_enable;
485
486 if (nvc0->state.clip_enable != clip_enable) {
487 nvc0->state.clip_enable = clip_enable;
488 IMMED_NVC0(push, NVC0_3D(CLIP_DISTANCE_ENABLE), clip_enable);
489 }
490 if (nvc0->state.clip_mode != vp->vp.clip_mode) {
491 nvc0->state.clip_mode = vp->vp.clip_mode;
492 BEGIN_NVC0(push, NVC0_3D(CLIP_DISTANCE_MODE), 1);
493 PUSH_DATA (push, vp->vp.clip_mode);
494 }
495 }
496
497 static void
498 nvc0_validate_blend(struct nvc0_context *nvc0)
499 {
500 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
501
502 PUSH_SPACE(push, nvc0->blend->size);
503 PUSH_DATAp(push, nvc0->blend->state, nvc0->blend->size);
504 }
505
506 static void
507 nvc0_validate_zsa(struct nvc0_context *nvc0)
508 {
509 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
510
511 PUSH_SPACE(push, nvc0->zsa->size);
512 PUSH_DATAp(push, nvc0->zsa->state, nvc0->zsa->size);
513 }
514
515 static void
516 nvc0_validate_rasterizer(struct nvc0_context *nvc0)
517 {
518 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
519
520 PUSH_SPACE(push, nvc0->rast->size);
521 PUSH_DATAp(push, nvc0->rast->state, nvc0->rast->size);
522 }
523
524 static void
525 nvc0_constbufs_validate(struct nvc0_context *nvc0)
526 {
527 unsigned s;
528
529 bool can_serialize = true;
530 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
531
532 for (s = 0; s < 5; ++s) {
533 while (nvc0->constbuf_dirty[s]) {
534 int i = ffs(nvc0->constbuf_dirty[s]) - 1;
535 nvc0->constbuf_dirty[s] &= ~(1 << i);
536
537 if (nvc0->constbuf[s][i].user) {
538 struct nouveau_bo *bo = nvc0->screen->uniform_bo;
539 const unsigned base = NVC0_CB_USR_INFO(s);
540 const unsigned size = nvc0->constbuf[s][0].size;
541 assert(i == 0); /* we really only want OpenGL uniforms here */
542 assert(nvc0->constbuf[s][0].u.data);
543
544 if (!nvc0->state.uniform_buffer_bound[s]) {
545 nvc0->state.uniform_buffer_bound[s] = true;
546
547 nvc0_screen_bind_cb_3d(nvc0->screen, push, &can_serialize, s, i,
548 NVC0_MAX_CONSTBUF_SIZE, bo->offset + base);
549 }
550 nvc0_cb_bo_push(&nvc0->base, bo, NV_VRAM_DOMAIN(&nvc0->screen->base),
551 base, NVC0_MAX_CONSTBUF_SIZE,
552 0, (size + 3) / 4,
553 nvc0->constbuf[s][0].u.data);
554 } else {
555 struct nv04_resource *res =
556 nv04_resource(nvc0->constbuf[s][i].u.buf);
557 if (res) {
558 nvc0_screen_bind_cb_3d(nvc0->screen, push, &can_serialize, s, i,
559 nvc0->constbuf[s][i].size,
560 res->address + nvc0->constbuf[s][i].offset);
561
562 BCTX_REFN(nvc0->bufctx_3d, 3D_CB(s, i), res, RD);
563
564 nvc0->cb_dirty = 1; /* Force cache flush for UBO. */
565 res->cb_bindings[s] |= 1 << i;
566
567 if (i == 0)
568 nvc0->state.uniform_buffer_bound[s] = false;
569 } else if (i != 0) {
570 nvc0_screen_bind_cb_3d(nvc0->screen, push, &can_serialize, s, i, -1, 0);
571 }
572 }
573 }
574 }
575
576 if (nvc0->screen->base.class_3d < NVE4_3D_CLASS) {
577 /* Invalidate all COMPUTE constbufs because they are aliased with 3D. */
578 nvc0->dirty_cp |= NVC0_NEW_CP_CONSTBUF;
579 nvc0->constbuf_dirty[5] |= nvc0->constbuf_valid[5];
580 nvc0->state.uniform_buffer_bound[5] = false;
581 }
582 }
583
584 static void
585 nvc0_validate_buffers(struct nvc0_context *nvc0)
586 {
587 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
588 struct nvc0_screen *screen = nvc0->screen;
589 int i, s;
590
591 for (s = 0; s < 5; s++) {
592 BEGIN_NVC0(push, NVC0_3D(CB_SIZE), 3);
593 PUSH_DATA (push, NVC0_CB_AUX_SIZE);
594 PUSH_DATAh(push, screen->uniform_bo->offset + NVC0_CB_AUX_INFO(s));
595 PUSH_DATA (push, screen->uniform_bo->offset + NVC0_CB_AUX_INFO(s));
596 BEGIN_1IC0(push, NVC0_3D(CB_POS), 1 + 4 * NVC0_MAX_BUFFERS);
597 PUSH_DATA (push, NVC0_CB_AUX_BUF_INFO(0));
598 for (i = 0; i < NVC0_MAX_BUFFERS; i++) {
599 if (nvc0->buffers[s][i].buffer) {
600 struct nv04_resource *res =
601 nv04_resource(nvc0->buffers[s][i].buffer);
602 PUSH_DATA (push, res->address + nvc0->buffers[s][i].buffer_offset);
603 PUSH_DATAh(push, res->address + nvc0->buffers[s][i].buffer_offset);
604 PUSH_DATA (push, nvc0->buffers[s][i].buffer_size);
605 PUSH_DATA (push, 0);
606 BCTX_REFN(nvc0->bufctx_3d, 3D_BUF, res, RDWR);
607 util_range_add(&res->base, &res->valid_buffer_range,
608 nvc0->buffers[s][i].buffer_offset,
609 nvc0->buffers[s][i].buffer_offset +
610 nvc0->buffers[s][i].buffer_size);
611 } else {
612 PUSH_DATA (push, 0);
613 PUSH_DATA (push, 0);
614 PUSH_DATA (push, 0);
615 PUSH_DATA (push, 0);
616 }
617 }
618 }
619
620 }
621
622 static void
623 nvc0_validate_sample_mask(struct nvc0_context *nvc0)
624 {
625 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
626
627 unsigned mask[4] =
628 {
629 nvc0->sample_mask & 0xffff,
630 nvc0->sample_mask & 0xffff,
631 nvc0->sample_mask & 0xffff,
632 nvc0->sample_mask & 0xffff
633 };
634
635 BEGIN_NVC0(push, NVC0_3D(MSAA_MASK(0)), 4);
636 PUSH_DATA (push, mask[0]);
637 PUSH_DATA (push, mask[1]);
638 PUSH_DATA (push, mask[2]);
639 PUSH_DATA (push, mask[3]);
640 }
641
642 static void
643 nvc0_validate_min_samples(struct nvc0_context *nvc0)
644 {
645 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
646 int samples;
647
648 samples = util_next_power_of_two(nvc0->min_samples);
649 if (samples > 1) {
650 // If we're using the incoming sample mask and doing sample shading, we
651 // have to do sample shading "to the max", otherwise there's no way to
652 // tell which sets of samples are covered by the current invocation.
653 // Similarly for reading the framebuffer.
654 if (nvc0->fragprog && (
655 nvc0->fragprog->fp.sample_mask_in ||
656 nvc0->fragprog->fp.reads_framebuffer))
657 samples = util_framebuffer_get_num_samples(&nvc0->framebuffer);
658 samples |= NVC0_3D_SAMPLE_SHADING_ENABLE;
659 }
660
661 IMMED_NVC0(push, NVC0_3D(SAMPLE_SHADING), samples);
662 }
663
664 static void
665 nvc0_validate_driverconst(struct nvc0_context *nvc0)
666 {
667 struct nvc0_screen *screen = nvc0->screen;
668 int i;
669
670 for (i = 0; i < 5; ++i)
671 nvc0_screen_bind_cb_3d(screen, nvc0->base.pushbuf, NULL, i, 15, NVC0_CB_AUX_SIZE,
672 screen->uniform_bo->offset + NVC0_CB_AUX_INFO(i));
673
674 nvc0->dirty_cp |= NVC0_NEW_CP_DRIVERCONST;
675 }
676
677 static void
678 nvc0_validate_fp_zsa_rast(struct nvc0_context *nvc0)
679 {
680 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
681 bool rasterizer_discard;
682
683 if (nvc0->rast && nvc0->rast->pipe.rasterizer_discard) {
684 rasterizer_discard = true;
685 } else {
686 bool zs = nvc0->zsa &&
687 (nvc0->zsa->pipe.depth_enabled || nvc0->zsa->pipe.stencil[0].enabled);
688 rasterizer_discard = !zs &&
689 (!nvc0->fragprog || !nvc0->fragprog->hdr[18]);
690 }
691
692 if (rasterizer_discard != nvc0->state.rasterizer_discard) {
693 nvc0->state.rasterizer_discard = rasterizer_discard;
694 IMMED_NVC0(push, NVC0_3D(RASTERIZE_ENABLE), !rasterizer_discard);
695 }
696 }
697
698 /* alpha test is disabled if there are no color RTs, so make sure we have at
699 * least one if alpha test is enabled. Note that this must run after
700 * nvc0_validate_fb, otherwise that will override the RT count setting.
701 */
702 static void
703 nvc0_validate_zsa_fb(struct nvc0_context *nvc0)
704 {
705 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
706
707 if (nvc0->zsa && nvc0->zsa->pipe.alpha_enabled &&
708 nvc0->framebuffer.zsbuf.texture &&
709 nvc0->framebuffer.nr_cbufs == 0) {
710 nvc0_fb_set_null_rt(push, 0, 0);
711 BEGIN_NVC0(push, NVC0_3D(RT_CONTROL), 1);
712 PUSH_DATA (push, (076543210 << 4) | 1);
713 }
714 }
715
716 static void
717 nvc0_validate_tess_state(struct nvc0_context *nvc0)
718 {
719 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
720
721 BEGIN_NVC0(push, NVC0_3D(TESS_LEVEL_OUTER(0)), 6);
722 PUSH_DATAp(push, nvc0->default_tess_outer, 4);
723 PUSH_DATAp(push, nvc0->default_tess_inner, 2);
724 }
725
726 /* If we have a frag shader bound which tries to read from the framebuffer, we
727 * have to make sure that the fb is bound as a texture in the expected
728 * location. For Fermi, that's in the special driver slot 16, while for Kepler
729 * it's a regular binding stored in the driver constbuf.
730 */
731 static void
732 nvc0_validate_fbread(struct nvc0_context *nvc0)
733 {
734 struct nouveau_pushbuf *push = nvc0->base.pushbuf;
735 struct nvc0_screen *screen = nvc0->screen;
736 struct pipe_context *pipe = &nvc0->base.pipe;
737 struct pipe_sampler_view *old_view = nvc0->fbtexture;
738 struct pipe_sampler_view *new_view = NULL;
739
740 if (nvc0->fragprog &&
741 nvc0->fragprog->fp.reads_framebuffer &&
742 nvc0->framebuffer.nr_cbufs &&
743 nvc0->framebuffer.cbufs[0].texture) {
744 struct pipe_sampler_view tmpl = {0};
745 const struct pipe_surface *sf = &nvc0->framebuffer.cbufs[0];
746
747 tmpl.target = PIPE_TEXTURE_2D_ARRAY;
748 tmpl.format = sf->format;
749 tmpl.u.tex.first_level = tmpl.u.tex.last_level = sf->level;
750 tmpl.u.tex.first_layer = sf->first_layer;
751 tmpl.u.tex.last_layer = sf->last_layer;
752 tmpl.swizzle_r = PIPE_SWIZZLE_X;
753 tmpl.swizzle_g = PIPE_SWIZZLE_Y;
754 tmpl.swizzle_b = PIPE_SWIZZLE_Z;
755 tmpl.swizzle_a = PIPE_SWIZZLE_W;
756
757 /* Bail if it's the same parameters */
758 if (old_view && old_view->texture == sf->texture &&
759 old_view->format == sf->format &&
760 old_view->u.tex.first_level == sf->level &&
761 old_view->u.tex.first_layer == sf->first_layer &&
762 old_view->u.tex.last_layer == sf->last_layer)
763 return;
764
765 new_view = pipe->create_sampler_view(pipe, sf->texture, &tmpl);
766 } else if (old_view == NULL) {
767 return;
768 }
769
770 if (old_view)
771 pipe_sampler_view_reference(&nvc0->fbtexture, NULL);
772 nvc0->fbtexture = new_view;
773
774 if (new_view) {
775 struct nv50_tic_entry *tic = nv50_tic_entry(new_view);
776 assert(tic->id < 0);
777 tic->id = nvc0_screen_tic_alloc(screen, tic);
778 nvc0->base.push_data(&nvc0->base, screen->txc, tic->id * 32,
779 NV_VRAM_DOMAIN(&screen->base), 32, tic->tic);
780 screen->tic.lock[tic->id / 32] |= 1 << (tic->id % 32);
781
782 if (screen->base.class_3d >= NVE4_3D_CLASS) {
783 BEGIN_NVC0(push, NVC0_3D(CB_SIZE), 3);
784 PUSH_DATA (push, NVC0_CB_AUX_SIZE);
785 PUSH_DATAh(push, screen->uniform_bo->offset + NVC0_CB_AUX_INFO(4));
786 PUSH_DATA (push, screen->uniform_bo->offset + NVC0_CB_AUX_INFO(4));
787 BEGIN_1IC0(push, NVC0_3D(CB_POS), 1 + 1);
788 PUSH_DATA (push, NVC0_CB_AUX_FB_TEX_INFO);
789 PUSH_DATA (push, (0 << 20) | tic->id);
790 } else {
791 BEGIN_NVC0(push, NVC0_3D(BIND_TIC2(0)), 1);
792 PUSH_DATA (push, (tic->id << 9) | 1);
793 }
794
795 IMMED_NVC0(push, NVC0_3D(TIC_FLUSH), 0);
796 }
797 }
798
799 static void
800 nvc0_switch_pipe_context(struct nvc0_context *ctx_to)
801 {
802 struct nvc0_context *ctx_from = ctx_to->screen->cur_ctx;
803 unsigned s;
804
805 simple_mtx_assert_locked(&ctx_to->screen->state_lock);
806 if (ctx_from)
807 ctx_to->state = ctx_from->state;
808 else
809 ctx_to->state = ctx_to->screen->save_state;
810
811 ctx_to->dirty_3d = ~0;
812 ctx_to->dirty_cp = ~0;
813 ctx_to->viewports_dirty = ~0;
814 ctx_to->scissors_dirty = ~0;
815
816 for (s = 0; s < 6; ++s) {
817 ctx_to->samplers_dirty[s] = ~0;
818 ctx_to->textures_dirty[s] = ~0;
819 ctx_to->constbuf_dirty[s] = (1 << NVC0_MAX_PIPE_CONSTBUFS) - 1;
820 ctx_to->buffers_dirty[s] = ~0;
821 ctx_to->images_dirty[s] = ~0;
822 }
823
824 /* Reset tfb as the shader that owns it may have been deleted. */
825 ctx_to->state.tfb = NULL;
826
827 if (!ctx_to->vertex)
828 ctx_to->dirty_3d &= ~(NVC0_NEW_3D_VERTEX | NVC0_NEW_3D_ARRAYS);
829
830 if (!ctx_to->vertprog)
831 ctx_to->dirty_3d &= ~NVC0_NEW_3D_VERTPROG;
832 if (!ctx_to->fragprog)
833 ctx_to->dirty_3d &= ~NVC0_NEW_3D_FRAGPROG;
834
835 if (!ctx_to->blend)
836 ctx_to->dirty_3d &= ~NVC0_NEW_3D_BLEND;
837 if (!ctx_to->rast)
838 ctx_to->dirty_3d &= ~(NVC0_NEW_3D_RASTERIZER | NVC0_NEW_3D_SCISSOR);
839 if (!ctx_to->zsa)
840 ctx_to->dirty_3d &= ~NVC0_NEW_3D_ZSA;
841
842 ctx_to->screen->cur_ctx = ctx_to;
843 }
844
845 static struct nvc0_state_validate
846 validate_list_3d[] = {
847 { nvc0_validate_fb, NVC0_NEW_3D_FRAMEBUFFER },
848 { nvc0_validate_blend, NVC0_NEW_3D_BLEND },
849 { nvc0_validate_zsa, NVC0_NEW_3D_ZSA },
850 { nvc0_validate_sample_mask, NVC0_NEW_3D_SAMPLE_MASK },
851 { nvc0_validate_rasterizer, NVC0_NEW_3D_RASTERIZER },
852 { nvc0_validate_blend_colour, NVC0_NEW_3D_BLEND_COLOUR },
853 { nvc0_validate_stencil_ref, NVC0_NEW_3D_STENCIL_REF },
854 { nvc0_validate_stipple, NVC0_NEW_3D_STIPPLE },
855 { nvc0_validate_scissor, NVC0_NEW_3D_SCISSOR | NVC0_NEW_3D_RASTERIZER },
856 { nvc0_validate_viewport, NVC0_NEW_3D_VIEWPORT },
857 { nvc0_validate_window_rects, NVC0_NEW_3D_WINDOW_RECTS },
858 { nvc0_vertprog_validate, NVC0_NEW_3D_VERTPROG },
859 { nvc0_tctlprog_validate, NVC0_NEW_3D_TCTLPROG },
860 { nvc0_tevlprog_validate, NVC0_NEW_3D_TEVLPROG },
861 { nvc0_validate_tess_state, NVC0_NEW_3D_TESSFACTOR },
862 { nvc0_gmtyprog_validate, NVC0_NEW_3D_GMTYPROG },
863 { nvc0_validate_min_samples, NVC0_NEW_3D_MIN_SAMPLES |
864 NVC0_NEW_3D_FRAGPROG |
865 NVC0_NEW_3D_FRAMEBUFFER },
866 { nvc0_fragprog_validate, NVC0_NEW_3D_FRAGPROG | NVC0_NEW_3D_RASTERIZER },
867 { nvc0_validate_fp_zsa_rast, NVC0_NEW_3D_FRAGPROG | NVC0_NEW_3D_ZSA |
868 NVC0_NEW_3D_RASTERIZER },
869 { nvc0_validate_zsa_fb, NVC0_NEW_3D_ZSA | NVC0_NEW_3D_FRAMEBUFFER },
870 { nvc0_validate_clip, NVC0_NEW_3D_CLIP | NVC0_NEW_3D_RASTERIZER |
871 NVC0_NEW_3D_VERTPROG |
872 NVC0_NEW_3D_TEVLPROG |
873 NVC0_NEW_3D_GMTYPROG },
874 { nvc0_constbufs_validate, NVC0_NEW_3D_CONSTBUF },
875 { nvc0_validate_textures, NVC0_NEW_3D_TEXTURES },
876 { nvc0_validate_samplers, NVC0_NEW_3D_SAMPLERS },
877 { nve4_set_tex_handles, NVC0_NEW_3D_TEXTURES | NVC0_NEW_3D_SAMPLERS },
878 { nvc0_validate_fbread, NVC0_NEW_3D_FRAGPROG |
879 NVC0_NEW_3D_FRAMEBUFFER },
880 { nvc0_vertex_arrays_validate, NVC0_NEW_3D_VERTEX | NVC0_NEW_3D_ARRAYS },
881 { nvc0_validate_surfaces, NVC0_NEW_3D_SURFACES },
882 { nvc0_validate_buffers, NVC0_NEW_3D_BUFFERS },
883 { nvc0_tfb_validate, NVC0_NEW_3D_TFB_TARGETS | NVC0_NEW_3D_GMTYPROG },
884 { nvc0_layer_validate, NVC0_NEW_3D_VERTPROG |
885 NVC0_NEW_3D_TEVLPROG |
886 NVC0_NEW_3D_GMTYPROG },
887 { nvc0_validate_driverconst, NVC0_NEW_3D_DRIVERCONST },
888 { validate_sample_locations, NVC0_NEW_3D_SAMPLE_LOCATIONS |
889 NVC0_NEW_3D_FRAMEBUFFER},
890 };
891
892 bool
893 nvc0_state_validate(struct nvc0_context *nvc0, uint32_t mask,
894 struct nvc0_state_validate *validate_list, int size,
895 uint32_t *dirty, struct nouveau_bufctx *bufctx)
896 {
897 uint32_t state_mask;
898 int ret;
899 unsigned i;
900
901 simple_mtx_assert_locked(&nvc0->screen->state_lock);
902
903 if (nvc0->screen->cur_ctx != nvc0)
904 nvc0_switch_pipe_context(nvc0);
905
906 state_mask = *dirty & mask;
907
908 if (state_mask) {
909 for (i = 0; i < size; ++i) {
910 struct nvc0_state_validate *validate = &validate_list[i];
911
912 if (state_mask & validate->states)
913 validate->func(nvc0);
914 }
915 *dirty &= ~state_mask;
916
917 nvc0_bufctx_fence(nvc0, bufctx, false);
918 }
919
920 nouveau_pushbuf_bufctx(nvc0->base.pushbuf, bufctx);
921 ret = PUSH_VAL(nvc0->base.pushbuf);
922
923 return !ret;
924 }
925
926 bool
927 nvc0_state_validate_3d(struct nvc0_context *nvc0, uint32_t mask)
928 {
929 bool ret;
930
931 ret = nvc0_state_validate(nvc0, mask, validate_list_3d,
932 ARRAY_SIZE(validate_list_3d), &nvc0->dirty_3d,
933 nvc0->bufctx_3d);
934
935 if (unlikely(nvc0->state.flushed)) {
936 nvc0->state.flushed = false;
937 nvc0_bufctx_fence(nvc0, nvc0->bufctx_3d, true);
938 }
939 return ret;
940 }