2 #include "util/u_format.h"
3 #include "util/u_framebuffer.h"
4 #include "util/u_math.h"
6 #include "nvc0/nvc0_context.h"
10 nvc0_validate_zcull(struct nvc0_context
*nvc0
)
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
;
18 uint32_t offset
= align(mt
->total_size
, 1 << 17);
19 unsigned width
, height
;
21 assert(mt
->base
.base
.depth0
== 1 && mt
->base
.base
.array_size
< 2);
23 size
= mt
->total_size
* 2;
25 height
= align(fb
->height
, 32);
26 width
= fb
->width
% 224;
28 width
= fb
->width
+ (224 - width
);
32 BEGIN_NVC0(push
, NVC0_3D(ZCULL_REGION
), 1);
34 BEGIN_NVC0(push
, NVC0_3D(ZCULL_ADDRESS_HIGH
), 2);
35 PUSH_DATAh(push
, bo
->offset
+ offset
);
36 PUSH_DATA (push
, bo
->offset
+ offset
);
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 */
46 BEGIN_NVC0(push
, NVC0_3D(ZCULL_WIDTH
), 4);
47 PUSH_DATA (push
, width
);
48 PUSH_DATA (push
, height
);
51 BEGIN_NVC0(push
, NVC0_3D(ZCULL_WINDOW_OFFSET_X
), 2);
54 BEGIN_NVC0(push
, NVC0_3D(ZCULL_INVALIDATE
), 1);
60 nvc0_fb_set_null_rt(struct nouveau_pushbuf
*push
, unsigned i
, unsigned layers
)
62 BEGIN_NVC0(push
, NVC0_3D(RT_ADDRESS_HIGH(i
)), 9);
65 PUSH_DATA (push
, 64); // width
66 PUSH_DATA (push
, 0); // height
67 PUSH_DATA (push
, 0); // format
68 PUSH_DATA (push
, 0); // tile mode
69 PUSH_DATA (push
, layers
); // layers
70 PUSH_DATA (push
, 0); // layer stride
71 PUSH_DATA (push
, 0); // base layer
75 nvc0_validate_fb(struct nvc0_context
*nvc0
)
77 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
78 struct pipe_framebuffer_state
*fb
= &nvc0
->framebuffer
;
79 struct nvc0_screen
*screen
= nvc0
->screen
;
81 unsigned ms_mode
= NVC0_3D_MULTISAMPLE_MODE_MS1
;
82 unsigned nr_cbufs
= fb
->nr_cbufs
;
83 bool serialize
= false;
85 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_3D_FB
);
87 BEGIN_NVC0(push
, NVC0_3D(SCREEN_SCISSOR_HORIZ
), 2);
88 PUSH_DATA (push
, fb
->width
<< 16);
89 PUSH_DATA (push
, fb
->height
<< 16);
91 for (i
= 0; i
< fb
->nr_cbufs
; ++i
) {
92 struct nv50_surface
*sf
;
93 struct nv04_resource
*res
;
94 struct nouveau_bo
*bo
;
97 nvc0_fb_set_null_rt(push
, i
, 0);
101 sf
= nv50_surface(fb
->cbufs
[i
]);
102 res
= nv04_resource(sf
->base
.texture
);
105 BEGIN_NVC0(push
, NVC0_3D(RT_ADDRESS_HIGH(i
)), 9);
106 PUSH_DATAh(push
, res
->address
+ sf
->offset
);
107 PUSH_DATA (push
, res
->address
+ sf
->offset
);
108 if (likely(nouveau_bo_memtype(bo
))) {
109 struct nv50_miptree
*mt
= nv50_miptree(sf
->base
.texture
);
111 assert(sf
->base
.texture
->target
!= PIPE_BUFFER
);
113 PUSH_DATA(push
, sf
->width
);
114 PUSH_DATA(push
, sf
->height
);
115 PUSH_DATA(push
, nvc0_format_table
[sf
->base
.format
].rt
);
116 PUSH_DATA(push
, (mt
->layout_3d
<< 16) |
117 mt
->level
[sf
->base
.u
.tex
.level
].tile_mode
);
118 PUSH_DATA(push
, sf
->base
.u
.tex
.first_layer
+ sf
->depth
);
119 PUSH_DATA(push
, mt
->layer_stride
>> 2);
120 PUSH_DATA(push
, sf
->base
.u
.tex
.first_layer
);
122 ms_mode
= mt
->ms_mode
;
124 if (res
->base
.target
== PIPE_BUFFER
) {
125 PUSH_DATA(push
, 262144);
128 PUSH_DATA(push
, nv50_miptree(sf
->base
.texture
)->level
[0].pitch
);
129 PUSH_DATA(push
, sf
->height
);
131 PUSH_DATA(push
, nvc0_format_table
[sf
->base
.format
].rt
);
132 PUSH_DATA(push
, 1 << 12);
137 nvc0_resource_fence(res
, NOUVEAU_BO_WR
);
142 if (res
->status
& NOUVEAU_BUFFER_STATUS_GPU_READING
)
144 res
->status
|= NOUVEAU_BUFFER_STATUS_GPU_WRITING
;
145 res
->status
&= ~NOUVEAU_BUFFER_STATUS_GPU_READING
;
147 /* only register for writing, otherwise we'd always serialize here */
148 BCTX_REFN(nvc0
->bufctx_3d
, 3D_FB
, res
, WR
);
152 struct nv50_miptree
*mt
= nv50_miptree(fb
->zsbuf
->texture
);
153 struct nv50_surface
*sf
= nv50_surface(fb
->zsbuf
);
154 int unk
= mt
->base
.base
.target
== PIPE_TEXTURE_2D
;
156 BEGIN_NVC0(push
, NVC0_3D(ZETA_ADDRESS_HIGH
), 5);
157 PUSH_DATAh(push
, mt
->base
.address
+ sf
->offset
);
158 PUSH_DATA (push
, mt
->base
.address
+ sf
->offset
);
159 PUSH_DATA (push
, nvc0_format_table
[fb
->zsbuf
->format
].rt
);
160 PUSH_DATA (push
, mt
->level
[sf
->base
.u
.tex
.level
].tile_mode
);
161 PUSH_DATA (push
, mt
->layer_stride
>> 2);
162 BEGIN_NVC0(push
, NVC0_3D(ZETA_ENABLE
), 1);
164 BEGIN_NVC0(push
, NVC0_3D(ZETA_HORIZ
), 3);
165 PUSH_DATA (push
, sf
->width
);
166 PUSH_DATA (push
, sf
->height
);
167 PUSH_DATA (push
, (unk
<< 16) |
168 (sf
->base
.u
.tex
.first_layer
+ sf
->depth
));
169 BEGIN_NVC0(push
, NVC0_3D(ZETA_BASE_LAYER
), 1);
170 PUSH_DATA (push
, sf
->base
.u
.tex
.first_layer
);
172 ms_mode
= mt
->ms_mode
;
174 if (mt
->base
.status
& NOUVEAU_BUFFER_STATUS_GPU_READING
)
176 mt
->base
.status
|= NOUVEAU_BUFFER_STATUS_GPU_WRITING
;
177 mt
->base
.status
&= ~NOUVEAU_BUFFER_STATUS_GPU_READING
;
179 BCTX_REFN(nvc0
->bufctx_3d
, 3D_FB
, &mt
->base
, WR
);
181 BEGIN_NVC0(push
, NVC0_3D(ZETA_ENABLE
), 1);
185 if (nr_cbufs
== 0 && !fb
->zsbuf
) {
186 assert(util_is_power_of_two(fb
->samples
));
187 assert(fb
->samples
<= 8);
189 nvc0_fb_set_null_rt(push
, 0, fb
->layers
);
192 ms_mode
= ffs(fb
->samples
) - 1;
196 BEGIN_NVC0(push
, NVC0_3D(RT_CONTROL
), 1);
197 PUSH_DATA (push
, (076543210 << 4) | nr_cbufs
);
198 IMMED_NVC0(push
, NVC0_3D(MULTISAMPLE_MODE
), ms_mode
);
201 BEGIN_NVC0(push
, NVC0_3D(CB_SIZE
), 3);
202 PUSH_DATA (push
, 2048);
203 PUSH_DATAh(push
, screen
->uniform_bo
->offset
+ NVC0_CB_AUX_INFO(4));
204 PUSH_DATA (push
, screen
->uniform_bo
->offset
+ NVC0_CB_AUX_INFO(4));
205 BEGIN_1IC0(push
, NVC0_3D(CB_POS
), 1 + 2 * ms
);
206 PUSH_DATA (push
, NVC0_CB_AUX_SAMPLE_INFO
);
207 for (i
= 0; i
< ms
; i
++) {
209 nvc0
->base
.pipe
.get_sample_position(&nvc0
->base
.pipe
, ms
, i
, xy
);
210 PUSH_DATAf(push
, xy
[0]);
211 PUSH_DATAf(push
, xy
[1]);
215 IMMED_NVC0(push
, NVC0_3D(SERIALIZE
), 0);
217 NOUVEAU_DRV_STAT(&nvc0
->screen
->base
, gpu_serialize_count
, serialize
);
221 nvc0_validate_blend_colour(struct nvc0_context
*nvc0
)
223 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
225 BEGIN_NVC0(push
, NVC0_3D(BLEND_COLOR(0)), 4);
226 PUSH_DATAf(push
, nvc0
->blend_colour
.color
[0]);
227 PUSH_DATAf(push
, nvc0
->blend_colour
.color
[1]);
228 PUSH_DATAf(push
, nvc0
->blend_colour
.color
[2]);
229 PUSH_DATAf(push
, nvc0
->blend_colour
.color
[3]);
233 nvc0_validate_stencil_ref(struct nvc0_context
*nvc0
)
235 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
236 const ubyte
*ref
= &nvc0
->stencil_ref
.ref_value
[0];
238 IMMED_NVC0(push
, NVC0_3D(STENCIL_FRONT_FUNC_REF
), ref
[0]);
239 IMMED_NVC0(push
, NVC0_3D(STENCIL_BACK_FUNC_REF
), ref
[1]);
243 nvc0_validate_stipple(struct nvc0_context
*nvc0
)
245 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
248 BEGIN_NVC0(push
, NVC0_3D(POLYGON_STIPPLE_PATTERN(0)), 32);
249 for (i
= 0; i
< 32; ++i
)
250 PUSH_DATA(push
, util_bswap32(nvc0
->stipple
.stipple
[i
]));
254 nvc0_validate_scissor(struct nvc0_context
*nvc0
)
257 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
259 if (!(nvc0
->dirty_3d
& NVC0_NEW_3D_SCISSOR
) &&
260 nvc0
->rast
->pipe
.scissor
== nvc0
->state
.scissor
)
263 if (nvc0
->state
.scissor
!= nvc0
->rast
->pipe
.scissor
)
264 nvc0
->scissors_dirty
= (1 << NVC0_MAX_VIEWPORTS
) - 1;
266 nvc0
->state
.scissor
= nvc0
->rast
->pipe
.scissor
;
268 for (i
= 0; i
< NVC0_MAX_VIEWPORTS
; i
++) {
269 struct pipe_scissor_state
*s
= &nvc0
->scissors
[i
];
270 if (!(nvc0
->scissors_dirty
& (1 << i
)))
273 BEGIN_NVC0(push
, NVC0_3D(SCISSOR_HORIZ(i
)), 2);
274 if (nvc0
->rast
->pipe
.scissor
) {
275 PUSH_DATA(push
, (s
->maxx
<< 16) | s
->minx
);
276 PUSH_DATA(push
, (s
->maxy
<< 16) | s
->miny
);
278 PUSH_DATA(push
, (0xffff << 16) | 0);
279 PUSH_DATA(push
, (0xffff << 16) | 0);
282 nvc0
->scissors_dirty
= 0;
286 nvc0_validate_viewport(struct nvc0_context
*nvc0
)
288 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
292 for (i
= 0; i
< NVC0_MAX_VIEWPORTS
; i
++) {
293 struct pipe_viewport_state
*vp
= &nvc0
->viewports
[i
];
295 if (!(nvc0
->viewports_dirty
& (1 << i
)))
298 BEGIN_NVC0(push
, NVC0_3D(VIEWPORT_TRANSLATE_X(i
)), 3);
299 PUSH_DATAf(push
, vp
->translate
[0]);
300 PUSH_DATAf(push
, vp
->translate
[1]);
301 PUSH_DATAf(push
, vp
->translate
[2]);
303 BEGIN_NVC0(push
, NVC0_3D(VIEWPORT_SCALE_X(i
)), 3);
304 PUSH_DATAf(push
, vp
->scale
[0]);
305 PUSH_DATAf(push
, vp
->scale
[1]);
306 PUSH_DATAf(push
, vp
->scale
[2]);
308 /* now set the viewport rectangle to viewport dimensions for clipping */
310 x
= util_iround(MAX2(0.0f
, vp
->translate
[0] - fabsf(vp
->scale
[0])));
311 y
= util_iround(MAX2(0.0f
, vp
->translate
[1] - fabsf(vp
->scale
[1])));
312 w
= util_iround(vp
->translate
[0] + fabsf(vp
->scale
[0])) - x
;
313 h
= util_iround(vp
->translate
[1] + fabsf(vp
->scale
[1])) - y
;
315 BEGIN_NVC0(push
, NVC0_3D(VIEWPORT_HORIZ(i
)), 2);
316 PUSH_DATA (push
, (w
<< 16) | x
);
317 PUSH_DATA (push
, (h
<< 16) | y
);
319 zmin
= vp
->translate
[2] - fabsf(vp
->scale
[2]);
320 zmax
= vp
->translate
[2] + fabsf(vp
->scale
[2]);
322 BEGIN_NVC0(push
, NVC0_3D(DEPTH_RANGE_NEAR(i
)), 2);
323 PUSH_DATAf(push
, zmin
);
324 PUSH_DATAf(push
, zmax
);
326 nvc0
->viewports_dirty
= 0;
330 nvc0_validate_window_rects(struct nvc0_context
*nvc0
)
332 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
333 bool enable
= nvc0
->window_rect
.rects
> 0 || nvc0
->window_rect
.inclusive
;
336 IMMED_NVC0(push
, NVC0_3D(CLIP_RECTS_EN
), enable
);
340 IMMED_NVC0(push
, NVC0_3D(CLIP_RECTS_MODE
), !nvc0
->window_rect
.inclusive
);
341 BEGIN_NVC0(push
, NVC0_3D(CLIP_RECT_HORIZ(0)), NVC0_MAX_WINDOW_RECTANGLES
* 2);
342 for (i
= 0; i
< nvc0
->window_rect
.rects
; i
++) {
343 struct pipe_scissor_state
*s
= &nvc0
->window_rect
.rect
[i
];
344 PUSH_DATA(push
, (s
->maxx
<< 16) | s
->minx
);
345 PUSH_DATA(push
, (s
->maxy
<< 16) | s
->miny
);
347 for (; i
< NVC0_MAX_WINDOW_RECTANGLES
; i
++) {
354 nvc0_upload_uclip_planes(struct nvc0_context
*nvc0
, unsigned s
)
356 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
357 struct nvc0_screen
*screen
= nvc0
->screen
;
359 BEGIN_NVC0(push
, NVC0_3D(CB_SIZE
), 3);
360 PUSH_DATA (push
, 2048);
361 PUSH_DATAh(push
, screen
->uniform_bo
->offset
+ NVC0_CB_AUX_INFO(s
));
362 PUSH_DATA (push
, screen
->uniform_bo
->offset
+ NVC0_CB_AUX_INFO(s
));
363 BEGIN_1IC0(push
, NVC0_3D(CB_POS
), PIPE_MAX_CLIP_PLANES
* 4 + 1);
364 PUSH_DATA (push
, NVC0_CB_AUX_UCP_INFO
);
365 PUSH_DATAp(push
, &nvc0
->clip
.ucp
[0][0], PIPE_MAX_CLIP_PLANES
* 4);
369 nvc0_check_program_ucps(struct nvc0_context
*nvc0
,
370 struct nvc0_program
*vp
, uint8_t mask
)
372 const unsigned n
= util_logbase2(mask
) + 1;
374 if (vp
->vp
.num_ucps
>= n
)
376 nvc0_program_destroy(nvc0
, vp
);
379 if (likely(vp
== nvc0
->vertprog
))
380 nvc0_vertprog_validate(nvc0
);
382 if (likely(vp
== nvc0
->gmtyprog
))
383 nvc0_gmtyprog_validate(nvc0
);
385 nvc0_tevlprog_validate(nvc0
);
389 nvc0_validate_clip(struct nvc0_context
*nvc0
)
391 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
392 struct nvc0_program
*vp
;
394 uint8_t clip_enable
= nvc0
->rast
->pipe
.clip_plane_enable
;
396 if (nvc0
->gmtyprog
) {
400 if (nvc0
->tevlprog
) {
408 if (clip_enable
&& vp
->vp
.num_ucps
< PIPE_MAX_CLIP_PLANES
)
409 nvc0_check_program_ucps(nvc0
, vp
, clip_enable
);
411 if (nvc0
->dirty_3d
& (NVC0_NEW_3D_CLIP
| (NVC0_NEW_3D_VERTPROG
<< stage
)))
412 if (vp
->vp
.num_ucps
> 0 && vp
->vp
.num_ucps
<= PIPE_MAX_CLIP_PLANES
)
413 nvc0_upload_uclip_planes(nvc0
, stage
);
415 clip_enable
&= vp
->vp
.clip_enable
;
416 clip_enable
|= vp
->vp
.cull_enable
;
418 if (nvc0
->state
.clip_enable
!= clip_enable
) {
419 nvc0
->state
.clip_enable
= clip_enable
;
420 IMMED_NVC0(push
, NVC0_3D(CLIP_DISTANCE_ENABLE
), clip_enable
);
422 if (nvc0
->state
.clip_mode
!= vp
->vp
.clip_mode
) {
423 nvc0
->state
.clip_mode
= vp
->vp
.clip_mode
;
424 BEGIN_NVC0(push
, NVC0_3D(CLIP_DISTANCE_MODE
), 1);
425 PUSH_DATA (push
, vp
->vp
.clip_mode
);
430 nvc0_validate_blend(struct nvc0_context
*nvc0
)
432 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
434 PUSH_SPACE(push
, nvc0
->blend
->size
);
435 PUSH_DATAp(push
, nvc0
->blend
->state
, nvc0
->blend
->size
);
439 nvc0_validate_zsa(struct nvc0_context
*nvc0
)
441 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
443 PUSH_SPACE(push
, nvc0
->zsa
->size
);
444 PUSH_DATAp(push
, nvc0
->zsa
->state
, nvc0
->zsa
->size
);
448 nvc0_validate_rasterizer(struct nvc0_context
*nvc0
)
450 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
452 PUSH_SPACE(push
, nvc0
->rast
->size
);
453 PUSH_DATAp(push
, nvc0
->rast
->state
, nvc0
->rast
->size
);
457 nvc0_constbufs_validate(struct nvc0_context
*nvc0
)
459 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
462 for (s
= 0; s
< 5; ++s
) {
463 while (nvc0
->constbuf_dirty
[s
]) {
464 int i
= ffs(nvc0
->constbuf_dirty
[s
]) - 1;
465 nvc0
->constbuf_dirty
[s
] &= ~(1 << i
);
467 if (nvc0
->constbuf
[s
][i
].user
) {
468 struct nouveau_bo
*bo
= nvc0
->screen
->uniform_bo
;
469 const unsigned base
= NVC0_CB_USR_INFO(s
);
470 const unsigned size
= nvc0
->constbuf
[s
][0].size
;
471 assert(i
== 0); /* we really only want OpenGL uniforms here */
472 assert(nvc0
->constbuf
[s
][0].u
.data
);
474 if (nvc0
->state
.uniform_buffer_bound
[s
] < size
) {
475 nvc0
->state
.uniform_buffer_bound
[s
] = align(size
, 0x100);
477 BEGIN_NVC0(push
, NVC0_3D(CB_SIZE
), 3);
478 PUSH_DATA (push
, nvc0
->state
.uniform_buffer_bound
[s
]);
479 PUSH_DATAh(push
, bo
->offset
+ base
);
480 PUSH_DATA (push
, bo
->offset
+ base
);
481 BEGIN_NVC0(push
, NVC0_3D(CB_BIND(s
)), 1);
482 PUSH_DATA (push
, (0 << 4) | 1);
484 nvc0_cb_bo_push(&nvc0
->base
, bo
, NV_VRAM_DOMAIN(&nvc0
->screen
->base
),
485 base
, nvc0
->state
.uniform_buffer_bound
[s
],
487 nvc0
->constbuf
[s
][0].u
.data
);
489 struct nv04_resource
*res
=
490 nv04_resource(nvc0
->constbuf
[s
][i
].u
.buf
);
492 BEGIN_NVC0(push
, NVC0_3D(CB_SIZE
), 3);
493 PUSH_DATA (push
, nvc0
->constbuf
[s
][i
].size
);
494 PUSH_DATAh(push
, res
->address
+ nvc0
->constbuf
[s
][i
].offset
);
495 PUSH_DATA (push
, res
->address
+ nvc0
->constbuf
[s
][i
].offset
);
496 BEGIN_NVC0(push
, NVC0_3D(CB_BIND(s
)), 1);
497 PUSH_DATA (push
, (i
<< 4) | 1);
499 BCTX_REFN(nvc0
->bufctx_3d
, 3D_CB(s
, i
), res
, RD
);
501 nvc0
->cb_dirty
= 1; /* Force cache flush for UBO. */
502 res
->cb_bindings
[s
] |= 1 << i
;
504 BEGIN_NVC0(push
, NVC0_3D(CB_BIND(s
)), 1);
505 PUSH_DATA (push
, (i
<< 4) | 0);
508 nvc0
->state
.uniform_buffer_bound
[s
] = 0;
513 if (nvc0
->screen
->base
.class_3d
< NVE4_3D_CLASS
) {
514 /* Invalidate all COMPUTE constbufs because they are aliased with 3D. */
515 nvc0
->dirty_cp
|= NVC0_NEW_CP_CONSTBUF
;
516 nvc0
->constbuf_dirty
[5] |= nvc0
->constbuf_valid
[5];
517 nvc0
->state
.uniform_buffer_bound
[5] = 0;
522 nvc0_validate_buffers(struct nvc0_context
*nvc0
)
524 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
525 struct nvc0_screen
*screen
= nvc0
->screen
;
528 for (s
= 0; s
< 5; s
++) {
529 BEGIN_NVC0(push
, NVC0_3D(CB_SIZE
), 3);
530 PUSH_DATA (push
, 2048);
531 PUSH_DATAh(push
, screen
->uniform_bo
->offset
+ NVC0_CB_AUX_INFO(s
));
532 PUSH_DATA (push
, screen
->uniform_bo
->offset
+ NVC0_CB_AUX_INFO(s
));
533 BEGIN_1IC0(push
, NVC0_3D(CB_POS
), 1 + 4 * NVC0_MAX_BUFFERS
);
534 PUSH_DATA (push
, NVC0_CB_AUX_BUF_INFO(0));
535 for (i
= 0; i
< NVC0_MAX_BUFFERS
; i
++) {
536 if (nvc0
->buffers
[s
][i
].buffer
) {
537 struct nv04_resource
*res
=
538 nv04_resource(nvc0
->buffers
[s
][i
].buffer
);
539 PUSH_DATA (push
, res
->address
+ nvc0
->buffers
[s
][i
].buffer_offset
);
540 PUSH_DATAh(push
, res
->address
+ nvc0
->buffers
[s
][i
].buffer_offset
);
541 PUSH_DATA (push
, nvc0
->buffers
[s
][i
].buffer_size
);
543 BCTX_REFN(nvc0
->bufctx_3d
, 3D_BUF
, res
, RDWR
);
544 util_range_add(&res
->valid_buffer_range
,
545 nvc0
->buffers
[s
][i
].buffer_offset
,
546 nvc0
->buffers
[s
][i
].buffer_size
);
559 nvc0_validate_sample_mask(struct nvc0_context
*nvc0
)
561 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
565 nvc0
->sample_mask
& 0xffff,
566 nvc0
->sample_mask
& 0xffff,
567 nvc0
->sample_mask
& 0xffff,
568 nvc0
->sample_mask
& 0xffff
571 BEGIN_NVC0(push
, NVC0_3D(MSAA_MASK(0)), 4);
572 PUSH_DATA (push
, mask
[0]);
573 PUSH_DATA (push
, mask
[1]);
574 PUSH_DATA (push
, mask
[2]);
575 PUSH_DATA (push
, mask
[3]);
579 nvc0_validate_min_samples(struct nvc0_context
*nvc0
)
581 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
584 samples
= util_next_power_of_two(nvc0
->min_samples
);
586 // If we're using the incoming sample mask and doing sample shading, we
587 // have to do sample shading "to the max", otherwise there's no way to
588 // tell which sets of samples are covered by the current invocation.
589 if (nvc0
->fragprog
->fp
.sample_mask_in
)
590 samples
= util_framebuffer_get_num_samples(&nvc0
->framebuffer
);
591 samples
|= NVC0_3D_SAMPLE_SHADING_ENABLE
;
594 IMMED_NVC0(push
, NVC0_3D(SAMPLE_SHADING
), samples
);
598 nvc0_validate_driverconst(struct nvc0_context
*nvc0
)
600 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
601 struct nvc0_screen
*screen
= nvc0
->screen
;
604 for (i
= 0; i
< 5; ++i
) {
605 BEGIN_NVC0(push
, NVC0_3D(CB_SIZE
), 3);
606 PUSH_DATA (push
, 2048);
607 PUSH_DATAh(push
, screen
->uniform_bo
->offset
+ NVC0_CB_AUX_INFO(i
));
608 PUSH_DATA (push
, screen
->uniform_bo
->offset
+ NVC0_CB_AUX_INFO(i
));
609 BEGIN_NVC0(push
, NVC0_3D(CB_BIND(i
)), 1);
610 PUSH_DATA (push
, (15 << 4) | 1);
613 nvc0
->dirty_cp
|= NVC0_NEW_CP_DRIVERCONST
;
617 nvc0_validate_derived_1(struct nvc0_context
*nvc0
)
619 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
620 bool rasterizer_discard
;
622 if (nvc0
->rast
&& nvc0
->rast
->pipe
.rasterizer_discard
) {
623 rasterizer_discard
= true;
625 bool zs
= nvc0
->zsa
&&
626 (nvc0
->zsa
->pipe
.depth
.enabled
|| nvc0
->zsa
->pipe
.stencil
[0].enabled
);
627 rasterizer_discard
= !zs
&&
628 (!nvc0
->fragprog
|| !nvc0
->fragprog
->hdr
[18]);
631 if (rasterizer_discard
!= nvc0
->state
.rasterizer_discard
) {
632 nvc0
->state
.rasterizer_discard
= rasterizer_discard
;
633 IMMED_NVC0(push
, NVC0_3D(RASTERIZE_ENABLE
), !rasterizer_discard
);
637 /* alpha test is disabled if there are no color RTs, so make sure we have at
638 * least one if alpha test is enabled. Note that this must run after
639 * nvc0_validate_fb, otherwise that will override the RT count setting.
642 nvc0_validate_derived_2(struct nvc0_context
*nvc0
)
644 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
646 if (nvc0
->zsa
&& nvc0
->zsa
->pipe
.alpha
.enabled
&&
647 nvc0
->framebuffer
.zsbuf
&&
648 nvc0
->framebuffer
.nr_cbufs
== 0) {
649 nvc0_fb_set_null_rt(push
, 0, 0);
650 BEGIN_NVC0(push
, NVC0_3D(RT_CONTROL
), 1);
651 PUSH_DATA (push
, (076543210 << 4) | 1);
656 nvc0_validate_derived_3(struct nvc0_context
*nvc0
)
658 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
659 struct pipe_framebuffer_state
*fb
= &nvc0
->framebuffer
;
662 if ((!fb
->nr_cbufs
|| !fb
->cbufs
[0] ||
663 !util_format_is_pure_integer(fb
->cbufs
[0]->format
)) && nvc0
->blend
) {
664 if (nvc0
->blend
->pipe
.alpha_to_coverage
)
665 ms
|= NVC0_3D_MULTISAMPLE_CTRL_ALPHA_TO_COVERAGE
;
666 if (nvc0
->blend
->pipe
.alpha_to_one
)
667 ms
|= NVC0_3D_MULTISAMPLE_CTRL_ALPHA_TO_ONE
;
670 BEGIN_NVC0(push
, NVC0_3D(MULTISAMPLE_CTRL
), 1);
671 PUSH_DATA (push
, ms
);
675 nvc0_validate_tess_state(struct nvc0_context
*nvc0
)
677 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
679 BEGIN_NVC0(push
, NVC0_3D(TESS_LEVEL_OUTER(0)), 6);
680 PUSH_DATAp(push
, nvc0
->default_tess_outer
, 4);
681 PUSH_DATAp(push
, nvc0
->default_tess_inner
, 2);
685 nvc0_switch_pipe_context(struct nvc0_context
*ctx_to
)
687 struct nvc0_context
*ctx_from
= ctx_to
->screen
->cur_ctx
;
691 ctx_to
->state
= ctx_from
->state
;
693 ctx_to
->state
= ctx_to
->screen
->save_state
;
695 ctx_to
->dirty_3d
= ~0;
696 ctx_to
->dirty_cp
= ~0;
697 ctx_to
->viewports_dirty
= ~0;
698 ctx_to
->scissors_dirty
= ~0;
700 for (s
= 0; s
< 6; ++s
) {
701 ctx_to
->samplers_dirty
[s
] = ~0;
702 ctx_to
->textures_dirty
[s
] = ~0;
703 ctx_to
->constbuf_dirty
[s
] = (1 << NVC0_MAX_PIPE_CONSTBUFS
) - 1;
704 ctx_to
->buffers_dirty
[s
] = ~0;
705 ctx_to
->images_dirty
[s
] = ~0;
708 /* Reset tfb as the shader that owns it may have been deleted. */
709 ctx_to
->state
.tfb
= NULL
;
712 ctx_to
->dirty_3d
&= ~(NVC0_NEW_3D_VERTEX
| NVC0_NEW_3D_ARRAYS
);
713 if (!ctx_to
->idxbuf
.buffer
)
714 ctx_to
->dirty_3d
&= ~NVC0_NEW_3D_IDXBUF
;
716 if (!ctx_to
->vertprog
)
717 ctx_to
->dirty_3d
&= ~NVC0_NEW_3D_VERTPROG
;
718 if (!ctx_to
->fragprog
)
719 ctx_to
->dirty_3d
&= ~NVC0_NEW_3D_FRAGPROG
;
722 ctx_to
->dirty_3d
&= ~NVC0_NEW_3D_BLEND
;
724 ctx_to
->dirty_3d
&= ~(NVC0_NEW_3D_RASTERIZER
| NVC0_NEW_3D_SCISSOR
);
726 ctx_to
->dirty_3d
&= ~NVC0_NEW_3D_ZSA
;
728 ctx_to
->screen
->cur_ctx
= ctx_to
;
731 static struct nvc0_state_validate
732 validate_list_3d
[] = {
733 { nvc0_validate_fb
, NVC0_NEW_3D_FRAMEBUFFER
},
734 { nvc0_validate_blend
, NVC0_NEW_3D_BLEND
},
735 { nvc0_validate_zsa
, NVC0_NEW_3D_ZSA
},
736 { nvc0_validate_sample_mask
, NVC0_NEW_3D_SAMPLE_MASK
},
737 { nvc0_validate_rasterizer
, NVC0_NEW_3D_RASTERIZER
},
738 { nvc0_validate_blend_colour
, NVC0_NEW_3D_BLEND_COLOUR
},
739 { nvc0_validate_stencil_ref
, NVC0_NEW_3D_STENCIL_REF
},
740 { nvc0_validate_stipple
, NVC0_NEW_3D_STIPPLE
},
741 { nvc0_validate_scissor
, NVC0_NEW_3D_SCISSOR
| NVC0_NEW_3D_RASTERIZER
},
742 { nvc0_validate_viewport
, NVC0_NEW_3D_VIEWPORT
},
743 { nvc0_validate_window_rects
, NVC0_NEW_3D_WINDOW_RECTS
},
744 { nvc0_vertprog_validate
, NVC0_NEW_3D_VERTPROG
},
745 { nvc0_tctlprog_validate
, NVC0_NEW_3D_TCTLPROG
},
746 { nvc0_tevlprog_validate
, NVC0_NEW_3D_TEVLPROG
},
747 { nvc0_validate_tess_state
, NVC0_NEW_3D_TESSFACTOR
},
748 { nvc0_gmtyprog_validate
, NVC0_NEW_3D_GMTYPROG
},
749 { nvc0_validate_min_samples
, NVC0_NEW_3D_MIN_SAMPLES
|
750 NVC0_NEW_3D_FRAGPROG
|
751 NVC0_NEW_3D_FRAMEBUFFER
},
752 { nvc0_fragprog_validate
, NVC0_NEW_3D_FRAGPROG
| NVC0_NEW_3D_RASTERIZER
},
753 { nvc0_validate_derived_1
, NVC0_NEW_3D_FRAGPROG
| NVC0_NEW_3D_ZSA
|
754 NVC0_NEW_3D_RASTERIZER
},
755 { nvc0_validate_derived_2
, NVC0_NEW_3D_ZSA
| NVC0_NEW_3D_FRAMEBUFFER
},
756 { nvc0_validate_derived_3
, NVC0_NEW_3D_BLEND
| NVC0_NEW_3D_FRAMEBUFFER
},
757 { nvc0_validate_clip
, NVC0_NEW_3D_CLIP
| NVC0_NEW_3D_RASTERIZER
|
758 NVC0_NEW_3D_VERTPROG
|
759 NVC0_NEW_3D_TEVLPROG
|
760 NVC0_NEW_3D_GMTYPROG
},
761 { nvc0_constbufs_validate
, NVC0_NEW_3D_CONSTBUF
},
762 { nvc0_validate_textures
, NVC0_NEW_3D_TEXTURES
},
763 { nvc0_validate_samplers
, NVC0_NEW_3D_SAMPLERS
},
764 { nve4_set_tex_handles
, NVC0_NEW_3D_TEXTURES
| NVC0_NEW_3D_SAMPLERS
},
765 { nvc0_vertex_arrays_validate
, NVC0_NEW_3D_VERTEX
| NVC0_NEW_3D_ARRAYS
},
766 { nvc0_validate_surfaces
, NVC0_NEW_3D_SURFACES
},
767 { nvc0_validate_buffers
, NVC0_NEW_3D_BUFFERS
},
768 { nvc0_idxbuf_validate
, NVC0_NEW_3D_IDXBUF
},
769 { nvc0_tfb_validate
, NVC0_NEW_3D_TFB_TARGETS
| NVC0_NEW_3D_GMTYPROG
},
770 { nvc0_validate_driverconst
, NVC0_NEW_3D_DRIVERCONST
},
774 nvc0_state_validate(struct nvc0_context
*nvc0
, uint32_t mask
,
775 struct nvc0_state_validate
*validate_list
, int size
,
776 uint32_t *dirty
, struct nouveau_bufctx
*bufctx
)
782 if (nvc0
->screen
->cur_ctx
!= nvc0
)
783 nvc0_switch_pipe_context(nvc0
);
785 state_mask
= *dirty
& mask
;
788 for (i
= 0; i
< size
; ++i
) {
789 struct nvc0_state_validate
*validate
= &validate_list
[i
];
791 if (state_mask
& validate
->states
)
792 validate
->func(nvc0
);
794 *dirty
&= ~state_mask
;
796 nvc0_bufctx_fence(nvc0
, bufctx
, false);
799 nouveau_pushbuf_bufctx(nvc0
->base
.pushbuf
, bufctx
);
800 ret
= nouveau_pushbuf_validate(nvc0
->base
.pushbuf
);
806 nvc0_state_validate_3d(struct nvc0_context
*nvc0
, uint32_t mask
)
810 ret
= nvc0_state_validate(nvc0
, mask
, validate_list_3d
,
811 ARRAY_SIZE(validate_list_3d
), &nvc0
->dirty_3d
,
814 if (unlikely(nvc0
->state
.flushed
)) {
815 nvc0
->state
.flushed
= false;
816 nvc0_bufctx_fence(nvc0
, nvc0
->bufctx_3d
, true);