2 * Copyright 2010 Christoph Bumiller
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
23 #include "pipe/p_defines.h"
24 #include "util/u_framebuffer.h"
25 #include "util/u_helpers.h"
26 #include "util/u_inlines.h"
27 #include "util/u_transfer.h"
29 #include "tgsi/tgsi_parse.h"
31 #include "nvc0/nvc0_stateobj.h"
32 #include "nvc0/nvc0_context.h"
33 #include "nvc0/nvc0_query_hw.h"
35 #include "nvc0/nvc0_3d.xml.h"
37 #include "nouveau_gldefs.h"
39 static inline uint32_t
40 nvc0_colormask(unsigned mask)
44 if (mask & PIPE_MASK_R)
46 if (mask & PIPE_MASK_G)
48 if (mask & PIPE_MASK_B)
50 if (mask & PIPE_MASK_A)
56 #define NVC0_BLEND_FACTOR_CASE(a, b) \
57 case PIPE_BLENDFACTOR_##a: return NV50_BLEND_FACTOR_##b
59 static inline uint32_t
60 nvc0_blend_fac(unsigned factor)
63 NVC0_BLEND_FACTOR_CASE(ONE, ONE);
64 NVC0_BLEND_FACTOR_CASE(SRC_COLOR, SRC_COLOR);
65 NVC0_BLEND_FACTOR_CASE(SRC_ALPHA, SRC_ALPHA);
66 NVC0_BLEND_FACTOR_CASE(DST_ALPHA, DST_ALPHA);
67 NVC0_BLEND_FACTOR_CASE(DST_COLOR, DST_COLOR);
68 NVC0_BLEND_FACTOR_CASE(SRC_ALPHA_SATURATE, SRC_ALPHA_SATURATE);
69 NVC0_BLEND_FACTOR_CASE(CONST_COLOR, CONSTANT_COLOR);
70 NVC0_BLEND_FACTOR_CASE(CONST_ALPHA, CONSTANT_ALPHA);
71 NVC0_BLEND_FACTOR_CASE(SRC1_COLOR, SRC1_COLOR);
72 NVC0_BLEND_FACTOR_CASE(SRC1_ALPHA, SRC1_ALPHA);
73 NVC0_BLEND_FACTOR_CASE(ZERO, ZERO);
74 NVC0_BLEND_FACTOR_CASE(INV_SRC_COLOR, ONE_MINUS_SRC_COLOR);
75 NVC0_BLEND_FACTOR_CASE(INV_SRC_ALPHA, ONE_MINUS_SRC_ALPHA);
76 NVC0_BLEND_FACTOR_CASE(INV_DST_ALPHA, ONE_MINUS_DST_ALPHA);
77 NVC0_BLEND_FACTOR_CASE(INV_DST_COLOR, ONE_MINUS_DST_COLOR);
78 NVC0_BLEND_FACTOR_CASE(INV_CONST_COLOR, ONE_MINUS_CONSTANT_COLOR);
79 NVC0_BLEND_FACTOR_CASE(INV_CONST_ALPHA, ONE_MINUS_CONSTANT_ALPHA);
80 NVC0_BLEND_FACTOR_CASE(INV_SRC1_COLOR, ONE_MINUS_SRC1_COLOR);
81 NVC0_BLEND_FACTOR_CASE(INV_SRC1_ALPHA, ONE_MINUS_SRC1_ALPHA);
83 return NV50_BLEND_FACTOR_ZERO;
88 nvc0_blend_state_create(struct pipe_context *pipe,
89 const struct pipe_blend_state *cso)
91 struct nvc0_blend_stateobj *so = CALLOC_STRUCT(nvc0_blend_stateobj);
93 int r; /* reference */
95 bool indep_masks = false;
96 bool indep_funcs = false;
100 /* check which states actually have differing values */
101 if (cso->independent_blend_enable) {
102 for (r = 0; r < 8 && !cso->rt[r].blend_enable; ++r);
104 for (i = r + 1; i < 8; ++i) {
105 if (!cso->rt[i].blend_enable)
108 if (cso->rt[i].rgb_func != cso->rt[r].rgb_func ||
109 cso->rt[i].rgb_src_factor != cso->rt[r].rgb_src_factor ||
110 cso->rt[i].rgb_dst_factor != cso->rt[r].rgb_dst_factor ||
111 cso->rt[i].alpha_func != cso->rt[r].alpha_func ||
112 cso->rt[i].alpha_src_factor != cso->rt[r].alpha_src_factor ||
113 cso->rt[i].alpha_dst_factor != cso->rt[r].alpha_dst_factor) {
119 blend_en |= (cso->rt[i].blend_enable ? 1 : 0) << i;
121 for (i = 1; i < 8; ++i) {
122 if (cso->rt[i].colormask != cso->rt[0].colormask) {
129 if (cso->rt[0].blend_enable)
133 if (cso->logicop_enable) {
134 SB_BEGIN_3D(so, LOGIC_OP_ENABLE, 2);
136 SB_DATA (so, nvgl_logicop_func(cso->logicop_func));
138 SB_IMMED_3D(so, MACRO_BLEND_ENABLES, 0);
140 SB_IMMED_3D(so, LOGIC_OP_ENABLE, 0);
142 SB_IMMED_3D(so, BLEND_INDEPENDENT, indep_funcs);
143 SB_IMMED_3D(so, MACRO_BLEND_ENABLES, blend_en);
145 for (i = 0; i < 8; ++i) {
146 if (cso->rt[i].blend_enable) {
147 SB_BEGIN_3D(so, IBLEND_EQUATION_RGB(i), 6);
148 SB_DATA (so, nvgl_blend_eqn(cso->rt[i].rgb_func));
149 SB_DATA (so, nvc0_blend_fac(cso->rt[i].rgb_src_factor));
150 SB_DATA (so, nvc0_blend_fac(cso->rt[i].rgb_dst_factor));
151 SB_DATA (so, nvgl_blend_eqn(cso->rt[i].alpha_func));
152 SB_DATA (so, nvc0_blend_fac(cso->rt[i].alpha_src_factor));
153 SB_DATA (so, nvc0_blend_fac(cso->rt[i].alpha_dst_factor));
158 SB_BEGIN_3D(so, BLEND_EQUATION_RGB, 5);
159 SB_DATA (so, nvgl_blend_eqn(cso->rt[r].rgb_func));
160 SB_DATA (so, nvc0_blend_fac(cso->rt[r].rgb_src_factor));
161 SB_DATA (so, nvc0_blend_fac(cso->rt[r].rgb_dst_factor));
162 SB_DATA (so, nvgl_blend_eqn(cso->rt[r].alpha_func));
163 SB_DATA (so, nvc0_blend_fac(cso->rt[r].alpha_src_factor));
164 SB_BEGIN_3D(so, BLEND_FUNC_DST_ALPHA, 1);
165 SB_DATA (so, nvc0_blend_fac(cso->rt[r].alpha_dst_factor));
168 SB_IMMED_3D(so, COLOR_MASK_COMMON, !indep_masks);
170 SB_BEGIN_3D(so, COLOR_MASK(0), 8);
171 for (i = 0; i < 8; ++i)
172 SB_DATA(so, nvc0_colormask(cso->rt[i].colormask));
174 SB_BEGIN_3D(so, COLOR_MASK(0), 1);
175 SB_DATA (so, nvc0_colormask(cso->rt[0].colormask));
179 assert(so->size <= ARRAY_SIZE(so->state));
184 nvc0_blend_state_bind(struct pipe_context *pipe, void *hwcso)
186 struct nvc0_context *nvc0 = nvc0_context(pipe);
189 nvc0->dirty_3d |= NVC0_NEW_3D_BLEND;
193 nvc0_blend_state_delete(struct pipe_context *pipe, void *hwcso)
198 /* NOTE: ignoring line_last_pixel */
200 nvc0_rasterizer_state_create(struct pipe_context *pipe,
201 const struct pipe_rasterizer_state *cso)
203 struct nvc0_rasterizer_stateobj *so;
206 so = CALLOC_STRUCT(nvc0_rasterizer_stateobj);
211 /* Scissor enables are handled in scissor state, we will not want to
212 * always emit 16 commands, one for each scissor rectangle, here.
215 SB_IMMED_3D(so, PROVOKING_VERTEX_LAST, !cso->flatshade_first);
216 SB_IMMED_3D(so, VERTEX_TWO_SIDE_ENABLE, cso->light_twoside);
218 SB_IMMED_3D(so, VERT_COLOR_CLAMP_EN, cso->clamp_vertex_color);
219 SB_BEGIN_3D(so, FRAG_COLOR_CLAMP_EN, 1);
220 SB_DATA (so, cso->clamp_fragment_color ? 0x11111111 : 0x00000000);
222 SB_IMMED_3D(so, MULTISAMPLE_ENABLE, cso->multisample);
224 SB_IMMED_3D(so, LINE_SMOOTH_ENABLE, cso->line_smooth);
225 if (cso->line_smooth || cso->multisample)
226 SB_BEGIN_3D(so, LINE_WIDTH_SMOOTH, 1);
228 SB_BEGIN_3D(so, LINE_WIDTH_ALIASED, 1);
229 SB_DATA (so, fui(cso->line_width));
231 SB_IMMED_3D(so, LINE_STIPPLE_ENABLE, cso->line_stipple_enable);
232 if (cso->line_stipple_enable) {
233 SB_BEGIN_3D(so, LINE_STIPPLE_PATTERN, 1);
234 SB_DATA (so, (cso->line_stipple_pattern << 8) |
235 cso->line_stipple_factor);
239 SB_IMMED_3D(so, VP_POINT_SIZE, cso->point_size_per_vertex);
240 if (!cso->point_size_per_vertex) {
241 SB_BEGIN_3D(so, POINT_SIZE, 1);
242 SB_DATA (so, fui(cso->point_size));
245 reg = (cso->sprite_coord_mode == PIPE_SPRITE_COORD_UPPER_LEFT) ?
246 NVC0_3D_POINT_COORD_REPLACE_COORD_ORIGIN_UPPER_LEFT :
247 NVC0_3D_POINT_COORD_REPLACE_COORD_ORIGIN_LOWER_LEFT;
249 SB_BEGIN_3D(so, POINT_COORD_REPLACE, 1);
250 SB_DATA (so, ((cso->sprite_coord_enable & 0xff) << 3) | reg);
251 SB_IMMED_3D(so, POINT_SPRITE_ENABLE, cso->point_quad_rasterization);
252 SB_IMMED_3D(so, POINT_SMOOTH_ENABLE, cso->point_smooth);
254 SB_BEGIN_3D(so, MACRO_POLYGON_MODE_FRONT, 1);
255 SB_DATA (so, nvgl_polygon_mode(cso->fill_front));
256 SB_BEGIN_3D(so, MACRO_POLYGON_MODE_BACK, 1);
257 SB_DATA (so, nvgl_polygon_mode(cso->fill_back));
258 SB_IMMED_3D(so, POLYGON_SMOOTH_ENABLE, cso->poly_smooth);
260 SB_BEGIN_3D(so, CULL_FACE_ENABLE, 3);
261 SB_DATA (so, cso->cull_face != PIPE_FACE_NONE);
262 SB_DATA (so, cso->front_ccw ? NVC0_3D_FRONT_FACE_CCW :
263 NVC0_3D_FRONT_FACE_CW);
264 switch (cso->cull_face) {
265 case PIPE_FACE_FRONT_AND_BACK:
266 SB_DATA(so, NVC0_3D_CULL_FACE_FRONT_AND_BACK);
268 case PIPE_FACE_FRONT:
269 SB_DATA(so, NVC0_3D_CULL_FACE_FRONT);
273 SB_DATA(so, NVC0_3D_CULL_FACE_BACK);
277 SB_IMMED_3D(so, POLYGON_STIPPLE_ENABLE, cso->poly_stipple_enable);
278 SB_BEGIN_3D(so, POLYGON_OFFSET_POINT_ENABLE, 3);
279 SB_DATA (so, cso->offset_point);
280 SB_DATA (so, cso->offset_line);
281 SB_DATA (so, cso->offset_tri);
283 if (cso->offset_point || cso->offset_line || cso->offset_tri) {
284 SB_BEGIN_3D(so, POLYGON_OFFSET_FACTOR, 1);
285 SB_DATA (so, fui(cso->offset_scale));
286 SB_BEGIN_3D(so, POLYGON_OFFSET_UNITS, 1);
287 SB_DATA (so, fui(cso->offset_units * 2.0f));
288 SB_BEGIN_3D(so, POLYGON_OFFSET_CLAMP, 1);
289 SB_DATA (so, fui(cso->offset_clamp));
293 reg = NVC0_3D_VIEW_VOLUME_CLIP_CTRL_UNK1_UNK1;
296 NVC0_3D_VIEW_VOLUME_CLIP_CTRL_UNK1_UNK1 |
297 NVC0_3D_VIEW_VOLUME_CLIP_CTRL_DEPTH_CLAMP_NEAR |
298 NVC0_3D_VIEW_VOLUME_CLIP_CTRL_DEPTH_CLAMP_FAR |
299 NVC0_3D_VIEW_VOLUME_CLIP_CTRL_UNK12_UNK2;
301 SB_BEGIN_3D(so, VIEW_VOLUME_CLIP_CTRL, 1);
304 SB_IMMED_3D(so, DEPTH_CLIP_NEGATIVE_Z, cso->clip_halfz);
306 SB_IMMED_3D(so, PIXEL_CENTER_INTEGER, !cso->half_pixel_center);
308 assert(so->size <= ARRAY_SIZE(so->state));
313 nvc0_rasterizer_state_bind(struct pipe_context *pipe, void *hwcso)
315 struct nvc0_context *nvc0 = nvc0_context(pipe);
318 nvc0->dirty_3d |= NVC0_NEW_3D_RASTERIZER;
322 nvc0_rasterizer_state_delete(struct pipe_context *pipe, void *hwcso)
328 nvc0_zsa_state_create(struct pipe_context *pipe,
329 const struct pipe_depth_stencil_alpha_state *cso)
331 struct nvc0_zsa_stateobj *so = CALLOC_STRUCT(nvc0_zsa_stateobj);
335 SB_IMMED_3D(so, DEPTH_TEST_ENABLE, cso->depth.enabled);
336 if (cso->depth.enabled) {
337 SB_IMMED_3D(so, DEPTH_WRITE_ENABLE, cso->depth.writemask);
338 SB_BEGIN_3D(so, DEPTH_TEST_FUNC, 1);
339 SB_DATA (so, nvgl_comparison_op(cso->depth.func));
342 SB_IMMED_3D(so, DEPTH_BOUNDS_EN, cso->depth.bounds_test);
343 if (cso->depth.bounds_test) {
344 SB_BEGIN_3D(so, DEPTH_BOUNDS(0), 2);
345 SB_DATA (so, fui(cso->depth.bounds_min));
346 SB_DATA (so, fui(cso->depth.bounds_max));
349 if (cso->stencil[0].enabled) {
350 SB_BEGIN_3D(so, STENCIL_ENABLE, 5);
352 SB_DATA (so, nvgl_stencil_op(cso->stencil[0].fail_op));
353 SB_DATA (so, nvgl_stencil_op(cso->stencil[0].zfail_op));
354 SB_DATA (so, nvgl_stencil_op(cso->stencil[0].zpass_op));
355 SB_DATA (so, nvgl_comparison_op(cso->stencil[0].func));
356 SB_BEGIN_3D(so, STENCIL_FRONT_FUNC_MASK, 2);
357 SB_DATA (so, cso->stencil[0].valuemask);
358 SB_DATA (so, cso->stencil[0].writemask);
360 SB_IMMED_3D(so, STENCIL_ENABLE, 0);
363 if (cso->stencil[1].enabled) {
364 assert(cso->stencil[0].enabled);
365 SB_BEGIN_3D(so, STENCIL_TWO_SIDE_ENABLE, 5);
367 SB_DATA (so, nvgl_stencil_op(cso->stencil[1].fail_op));
368 SB_DATA (so, nvgl_stencil_op(cso->stencil[1].zfail_op));
369 SB_DATA (so, nvgl_stencil_op(cso->stencil[1].zpass_op));
370 SB_DATA (so, nvgl_comparison_op(cso->stencil[1].func));
371 SB_BEGIN_3D(so, STENCIL_BACK_MASK, 2);
372 SB_DATA (so, cso->stencil[1].writemask);
373 SB_DATA (so, cso->stencil[1].valuemask);
375 if (cso->stencil[0].enabled) {
376 SB_IMMED_3D(so, STENCIL_TWO_SIDE_ENABLE, 0);
379 SB_IMMED_3D(so, ALPHA_TEST_ENABLE, cso->alpha.enabled);
380 if (cso->alpha.enabled) {
381 SB_BEGIN_3D(so, ALPHA_TEST_REF, 2);
382 SB_DATA (so, fui(cso->alpha.ref_value));
383 SB_DATA (so, nvgl_comparison_op(cso->alpha.func));
386 assert(so->size <= ARRAY_SIZE(so->state));
391 nvc0_zsa_state_bind(struct pipe_context *pipe, void *hwcso)
393 struct nvc0_context *nvc0 = nvc0_context(pipe);
396 nvc0->dirty_3d |= NVC0_NEW_3D_ZSA;
400 nvc0_zsa_state_delete(struct pipe_context *pipe, void *hwcso)
405 /* ====================== SAMPLERS AND TEXTURES ================================
408 #define NV50_TSC_WRAP_CASE(n) \
409 case PIPE_TEX_WRAP_##n: return NV50_TSC_WRAP_##n
412 nvc0_sampler_state_delete(struct pipe_context *pipe, void *hwcso)
416 for (s = 0; s < 6; ++s)
417 for (i = 0; i < nvc0_context(pipe)->num_samplers[s]; ++i)
418 if (nvc0_context(pipe)->samplers[s][i] == hwcso)
419 nvc0_context(pipe)->samplers[s][i] = NULL;
421 nvc0_screen_tsc_free(nvc0_context(pipe)->screen, nv50_tsc_entry(hwcso));
427 nvc0_stage_sampler_states_bind(struct nvc0_context *nvc0, int s,
428 unsigned nr, void **hwcso)
432 for (i = 0; i < nr; ++i) {
433 struct nv50_tsc_entry *old = nvc0->samplers[s][i];
437 nvc0->samplers_dirty[s] |= 1 << i;
439 nvc0->samplers[s][i] = nv50_tsc_entry(hwcso[i]);
441 nvc0_screen_tsc_unlock(nvc0->screen, old);
443 for (; i < nvc0->num_samplers[s]; ++i) {
444 if (nvc0->samplers[s][i]) {
445 nvc0_screen_tsc_unlock(nvc0->screen, nvc0->samplers[s][i]);
446 nvc0->samplers[s][i] = NULL;
450 nvc0->num_samplers[s] = nr;
452 nvc0->dirty_3d |= NVC0_NEW_3D_SAMPLERS;
456 nvc0_stage_sampler_states_bind_range(struct nvc0_context *nvc0,
458 unsigned start, unsigned nr, void **cso)
460 const unsigned end = start + nr;
465 for (i = start; i < end; ++i) {
466 const unsigned p = i - start;
469 if (cso[p] == nvc0->samplers[s][i])
471 nvc0->samplers_dirty[s] |= 1 << i;
473 if (nvc0->samplers[s][i])
474 nvc0_screen_tsc_unlock(nvc0->screen, nvc0->samplers[s][i]);
475 nvc0->samplers[s][i] = cso[p];
478 for (i = start; i < end; ++i) {
479 if (nvc0->samplers[s][i]) {
480 nvc0_screen_tsc_unlock(nvc0->screen, nvc0->samplers[s][i]);
481 nvc0->samplers[s][i] = NULL;
482 nvc0->samplers_dirty[s] |= 1 << i;
487 if (nvc0->num_samplers[s] <= end) {
488 if (last_valid < 0) {
489 for (i = start; i && !nvc0->samplers[s][i - 1]; --i);
490 nvc0->num_samplers[s] = i;
492 nvc0->num_samplers[s] = last_valid + 1;
498 nvc0_bind_sampler_states(struct pipe_context *pipe, unsigned shader,
499 unsigned start, unsigned nr, void **s)
502 case PIPE_SHADER_VERTEX:
504 nvc0_stage_sampler_states_bind(nvc0_context(pipe), 0, nr, s);
506 case PIPE_SHADER_TESS_CTRL:
508 nvc0_stage_sampler_states_bind(nvc0_context(pipe), 1, nr, s);
510 case PIPE_SHADER_TESS_EVAL:
512 nvc0_stage_sampler_states_bind(nvc0_context(pipe), 2, nr, s);
514 case PIPE_SHADER_GEOMETRY:
516 nvc0_stage_sampler_states_bind(nvc0_context(pipe), 3, nr, s);
518 case PIPE_SHADER_FRAGMENT:
520 nvc0_stage_sampler_states_bind(nvc0_context(pipe), 4, nr, s);
522 case PIPE_SHADER_COMPUTE:
523 nvc0_stage_sampler_states_bind_range(nvc0_context(pipe), 5,
525 nvc0_context(pipe)->dirty_cp |= NVC0_NEW_CP_SAMPLERS;
531 /* NOTE: only called when not referenced anywhere, won't be bound */
533 nvc0_sampler_view_destroy(struct pipe_context *pipe,
534 struct pipe_sampler_view *view)
536 pipe_resource_reference(&view->texture, NULL);
538 nvc0_screen_tic_free(nvc0_context(pipe)->screen, nv50_tic_entry(view));
540 FREE(nv50_tic_entry(view));
544 nvc0_stage_set_sampler_views(struct nvc0_context *nvc0, int s,
546 struct pipe_sampler_view **views)
550 for (i = 0; i < nr; ++i) {
551 struct nv50_tic_entry *old = nv50_tic_entry(nvc0->textures[s][i]);
553 if (views[i] == nvc0->textures[s][i])
555 nvc0->textures_dirty[s] |= 1 << i;
557 if (views[i] && views[i]->texture) {
558 struct pipe_resource *res = views[i]->texture;
559 if (res->target == PIPE_BUFFER &&
560 (res->flags & PIPE_RESOURCE_FLAG_MAP_COHERENT))
561 nvc0->textures_coherent[s] |= 1 << i;
563 nvc0->textures_coherent[s] &= ~(1 << i);
565 nvc0->textures_coherent[s] &= ~(1 << i);
569 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_TEX(s, i));
570 nvc0_screen_tic_unlock(nvc0->screen, old);
573 pipe_sampler_view_reference(&nvc0->textures[s][i], views[i]);
576 for (i = nr; i < nvc0->num_textures[s]; ++i) {
577 struct nv50_tic_entry *old = nv50_tic_entry(nvc0->textures[s][i]);
579 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_TEX(s, i));
580 nvc0_screen_tic_unlock(nvc0->screen, old);
581 pipe_sampler_view_reference(&nvc0->textures[s][i], NULL);
585 nvc0->num_textures[s] = nr;
587 nvc0->dirty_3d |= NVC0_NEW_3D_TEXTURES;
591 nvc0_stage_set_sampler_views_range(struct nvc0_context *nvc0, const unsigned s,
592 unsigned start, unsigned nr,
593 struct pipe_sampler_view **views)
595 struct nouveau_bufctx *bctx = (s == 5) ? nvc0->bufctx_cp : nvc0->bufctx_3d;
596 const unsigned end = start + nr;
597 const unsigned bin = (s == 5) ? NVC0_BIND_CP_TEX(0) : NVC0_BIND_3D_TEX(s, 0);
602 for (i = start; i < end; ++i) {
603 const unsigned p = i - start;
606 if (views[p] == nvc0->textures[s][i])
608 nvc0->textures_dirty[s] |= 1 << i;
610 if (views[p] && views[p]->texture) {
611 struct pipe_resource *res = views[p]->texture;
612 if (res->target == PIPE_BUFFER &&
613 (res->flags & PIPE_RESOURCE_FLAG_MAP_COHERENT))
614 nvc0->textures_coherent[s] |= 1 << i;
616 nvc0->textures_coherent[s] &= ~(1 << i);
618 nvc0->textures_coherent[s] &= ~(1 << i);
621 if (nvc0->textures[s][i]) {
622 struct nv50_tic_entry *old = nv50_tic_entry(nvc0->textures[s][i]);
623 nouveau_bufctx_reset(bctx, bin + i);
624 nvc0_screen_tic_unlock(nvc0->screen, old);
626 pipe_sampler_view_reference(&nvc0->textures[s][i], views[p]);
629 for (i = start; i < end; ++i) {
630 struct nv50_tic_entry *old = nv50_tic_entry(nvc0->textures[s][i]);
633 nvc0->textures_dirty[s] |= 1 << i;
635 nvc0_screen_tic_unlock(nvc0->screen, old);
636 pipe_sampler_view_reference(&nvc0->textures[s][i], NULL);
637 nouveau_bufctx_reset(bctx, bin + i);
641 if (nvc0->num_textures[s] <= end) {
642 if (last_valid < 0) {
643 for (i = start; i && !nvc0->textures[s][i - 1]; --i);
644 nvc0->num_textures[s] = i;
646 nvc0->num_textures[s] = last_valid + 1;
652 nvc0_set_sampler_views(struct pipe_context *pipe, unsigned shader,
653 unsigned start, unsigned nr,
654 struct pipe_sampler_view **views)
658 case PIPE_SHADER_VERTEX:
659 nvc0_stage_set_sampler_views(nvc0_context(pipe), 0, nr, views);
661 case PIPE_SHADER_TESS_CTRL:
662 nvc0_stage_set_sampler_views(nvc0_context(pipe), 1, nr, views);
664 case PIPE_SHADER_TESS_EVAL:
665 nvc0_stage_set_sampler_views(nvc0_context(pipe), 2, nr, views);
667 case PIPE_SHADER_GEOMETRY:
668 nvc0_stage_set_sampler_views(nvc0_context(pipe), 3, nr, views);
670 case PIPE_SHADER_FRAGMENT:
671 nvc0_stage_set_sampler_views(nvc0_context(pipe), 4, nr, views);
673 case PIPE_SHADER_COMPUTE:
674 nvc0_stage_set_sampler_views_range(nvc0_context(pipe), 5,
676 nvc0_context(pipe)->dirty_cp |= NVC0_NEW_CP_TEXTURES;
684 /* ============================= SHADERS =======================================
688 nvc0_sp_state_create(struct pipe_context *pipe,
689 const struct pipe_shader_state *cso, unsigned type)
691 struct nvc0_program *prog;
693 prog = CALLOC_STRUCT(nvc0_program);
700 prog->pipe.tokens = tgsi_dup_tokens(cso->tokens);
702 if (cso->stream_output.num_outputs)
703 prog->pipe.stream_output = cso->stream_output;
705 prog->translated = nvc0_program_translate(
706 prog, nvc0_context(pipe)->screen->base.device->chipset,
707 &nouveau_context(pipe)->debug);
713 nvc0_sp_state_delete(struct pipe_context *pipe, void *hwcso)
715 struct nvc0_program *prog = (struct nvc0_program *)hwcso;
717 nvc0_program_destroy(nvc0_context(pipe), prog);
719 FREE((void *)prog->pipe.tokens);
724 nvc0_vp_state_create(struct pipe_context *pipe,
725 const struct pipe_shader_state *cso)
727 return nvc0_sp_state_create(pipe, cso, PIPE_SHADER_VERTEX);
731 nvc0_vp_state_bind(struct pipe_context *pipe, void *hwcso)
733 struct nvc0_context *nvc0 = nvc0_context(pipe);
735 nvc0->vertprog = hwcso;
736 nvc0->dirty_3d |= NVC0_NEW_3D_VERTPROG;
740 nvc0_fp_state_create(struct pipe_context *pipe,
741 const struct pipe_shader_state *cso)
743 return nvc0_sp_state_create(pipe, cso, PIPE_SHADER_FRAGMENT);
747 nvc0_fp_state_bind(struct pipe_context *pipe, void *hwcso)
749 struct nvc0_context *nvc0 = nvc0_context(pipe);
751 nvc0->fragprog = hwcso;
752 nvc0->dirty_3d |= NVC0_NEW_3D_FRAGPROG;
756 nvc0_gp_state_create(struct pipe_context *pipe,
757 const struct pipe_shader_state *cso)
759 return nvc0_sp_state_create(pipe, cso, PIPE_SHADER_GEOMETRY);
763 nvc0_gp_state_bind(struct pipe_context *pipe, void *hwcso)
765 struct nvc0_context *nvc0 = nvc0_context(pipe);
767 nvc0->gmtyprog = hwcso;
768 nvc0->dirty_3d |= NVC0_NEW_3D_GMTYPROG;
772 nvc0_tcp_state_create(struct pipe_context *pipe,
773 const struct pipe_shader_state *cso)
775 return nvc0_sp_state_create(pipe, cso, PIPE_SHADER_TESS_CTRL);
779 nvc0_tcp_state_bind(struct pipe_context *pipe, void *hwcso)
781 struct nvc0_context *nvc0 = nvc0_context(pipe);
783 nvc0->tctlprog = hwcso;
784 nvc0->dirty_3d |= NVC0_NEW_3D_TCTLPROG;
788 nvc0_tep_state_create(struct pipe_context *pipe,
789 const struct pipe_shader_state *cso)
791 return nvc0_sp_state_create(pipe, cso, PIPE_SHADER_TESS_EVAL);
795 nvc0_tep_state_bind(struct pipe_context *pipe, void *hwcso)
797 struct nvc0_context *nvc0 = nvc0_context(pipe);
799 nvc0->tevlprog = hwcso;
800 nvc0->dirty_3d |= NVC0_NEW_3D_TEVLPROG;
804 nvc0_cp_state_create(struct pipe_context *pipe,
805 const struct pipe_compute_state *cso)
807 struct nvc0_program *prog;
809 prog = CALLOC_STRUCT(nvc0_program);
812 prog->type = PIPE_SHADER_COMPUTE;
814 prog->cp.smem_size = cso->req_local_mem;
815 prog->cp.lmem_size = cso->req_private_mem;
816 prog->parm_size = cso->req_input_mem;
818 prog->pipe.tokens = tgsi_dup_tokens((const struct tgsi_token *)cso->prog);
824 nvc0_cp_state_bind(struct pipe_context *pipe, void *hwcso)
826 struct nvc0_context *nvc0 = nvc0_context(pipe);
828 nvc0->compprog = hwcso;
829 nvc0->dirty_cp |= NVC0_NEW_CP_PROGRAM;
833 nvc0_set_constant_buffer(struct pipe_context *pipe, uint shader, uint index,
834 struct pipe_constant_buffer *cb)
836 struct nvc0_context *nvc0 = nvc0_context(pipe);
837 struct pipe_resource *res = cb ? cb->buffer : NULL;
838 const unsigned s = nvc0_shader_stage(shader);
839 const unsigned i = index;
841 if (unlikely(shader == PIPE_SHADER_COMPUTE)) {
842 if (nvc0->constbuf[s][i].user)
843 nvc0->constbuf[s][i].u.buf = NULL;
845 if (nvc0->constbuf[s][i].u.buf)
846 nouveau_bufctx_reset(nvc0->bufctx_cp, NVC0_BIND_CP_CB(i));
848 nvc0->dirty_cp |= NVC0_NEW_CP_CONSTBUF;
850 if (nvc0->constbuf[s][i].user)
851 nvc0->constbuf[s][i].u.buf = NULL;
853 if (nvc0->constbuf[s][i].u.buf)
854 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_CB(s, i));
856 nvc0->dirty_3d |= NVC0_NEW_3D_CONSTBUF;
858 nvc0->constbuf_dirty[s] |= 1 << i;
860 if (nvc0->constbuf[s][i].u.buf)
861 nv04_resource(nvc0->constbuf[s][i].u.buf)->cb_bindings[s] &= ~(1 << i);
862 pipe_resource_reference(&nvc0->constbuf[s][i].u.buf, res);
864 nvc0->constbuf[s][i].user = (cb && cb->user_buffer) ? true : false;
865 if (nvc0->constbuf[s][i].user) {
866 nvc0->constbuf[s][i].u.data = cb->user_buffer;
867 nvc0->constbuf[s][i].size = MIN2(cb->buffer_size, 0x10000);
868 nvc0->constbuf_valid[s] |= 1 << i;
869 nvc0->constbuf_coherent[s] &= ~(1 << i);
872 nvc0->constbuf[s][i].offset = cb->buffer_offset;
873 nvc0->constbuf[s][i].size = MIN2(align(cb->buffer_size, 0x100), 0x10000);
874 nvc0->constbuf_valid[s] |= 1 << i;
875 if (res && res->flags & PIPE_RESOURCE_FLAG_MAP_COHERENT)
876 nvc0->constbuf_coherent[s] |= 1 << i;
878 nvc0->constbuf_coherent[s] &= ~(1 << i);
881 nvc0->constbuf_valid[s] &= ~(1 << i);
882 nvc0->constbuf_coherent[s] &= ~(1 << i);
886 /* =============================================================================
890 nvc0_set_blend_color(struct pipe_context *pipe,
891 const struct pipe_blend_color *bcol)
893 struct nvc0_context *nvc0 = nvc0_context(pipe);
895 nvc0->blend_colour = *bcol;
896 nvc0->dirty_3d |= NVC0_NEW_3D_BLEND_COLOUR;
900 nvc0_set_stencil_ref(struct pipe_context *pipe,
901 const struct pipe_stencil_ref *sr)
903 struct nvc0_context *nvc0 = nvc0_context(pipe);
905 nvc0->stencil_ref = *sr;
906 nvc0->dirty_3d |= NVC0_NEW_3D_STENCIL_REF;
910 nvc0_set_clip_state(struct pipe_context *pipe,
911 const struct pipe_clip_state *clip)
913 struct nvc0_context *nvc0 = nvc0_context(pipe);
915 memcpy(nvc0->clip.ucp, clip->ucp, sizeof(clip->ucp));
917 nvc0->dirty_3d |= NVC0_NEW_3D_CLIP;
921 nvc0_set_sample_mask(struct pipe_context *pipe, unsigned sample_mask)
923 struct nvc0_context *nvc0 = nvc0_context(pipe);
925 nvc0->sample_mask = sample_mask;
926 nvc0->dirty_3d |= NVC0_NEW_3D_SAMPLE_MASK;
930 nvc0_set_min_samples(struct pipe_context *pipe, unsigned min_samples)
932 struct nvc0_context *nvc0 = nvc0_context(pipe);
934 if (nvc0->min_samples != min_samples) {
935 nvc0->min_samples = min_samples;
936 nvc0->dirty_3d |= NVC0_NEW_3D_MIN_SAMPLES;
941 nvc0_set_framebuffer_state(struct pipe_context *pipe,
942 const struct pipe_framebuffer_state *fb)
944 struct nvc0_context *nvc0 = nvc0_context(pipe);
946 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_FB);
948 util_copy_framebuffer_state(&nvc0->framebuffer, fb);
950 nvc0->dirty_3d |= NVC0_NEW_3D_FRAMEBUFFER;
954 nvc0_set_polygon_stipple(struct pipe_context *pipe,
955 const struct pipe_poly_stipple *stipple)
957 struct nvc0_context *nvc0 = nvc0_context(pipe);
959 nvc0->stipple = *stipple;
960 nvc0->dirty_3d |= NVC0_NEW_3D_STIPPLE;
964 nvc0_set_scissor_states(struct pipe_context *pipe,
966 unsigned num_scissors,
967 const struct pipe_scissor_state *scissor)
969 struct nvc0_context *nvc0 = nvc0_context(pipe);
972 assert(start_slot + num_scissors <= NVC0_MAX_VIEWPORTS);
973 for (i = 0; i < num_scissors; i++) {
974 if (!memcmp(&nvc0->scissors[start_slot + i], &scissor[i], sizeof(*scissor)))
976 nvc0->scissors[start_slot + i] = scissor[i];
977 nvc0->scissors_dirty |= 1 << (start_slot + i);
978 nvc0->dirty_3d |= NVC0_NEW_3D_SCISSOR;
983 nvc0_set_viewport_states(struct pipe_context *pipe,
985 unsigned num_viewports,
986 const struct pipe_viewport_state *vpt)
988 struct nvc0_context *nvc0 = nvc0_context(pipe);
991 assert(start_slot + num_viewports <= NVC0_MAX_VIEWPORTS);
992 for (i = 0; i < num_viewports; i++) {
993 if (!memcmp(&nvc0->viewports[start_slot + i], &vpt[i], sizeof(*vpt)))
995 nvc0->viewports[start_slot + i] = vpt[i];
996 nvc0->viewports_dirty |= 1 << (start_slot + i);
997 nvc0->dirty_3d |= NVC0_NEW_3D_VIEWPORT;
1003 nvc0_set_tess_state(struct pipe_context *pipe,
1004 const float default_tess_outer[4],
1005 const float default_tess_inner[2])
1007 struct nvc0_context *nvc0 = nvc0_context(pipe);
1009 memcpy(nvc0->default_tess_outer, default_tess_outer, 4 * sizeof(float));
1010 memcpy(nvc0->default_tess_inner, default_tess_inner, 2 * sizeof(float));
1011 nvc0->dirty_3d |= NVC0_NEW_3D_TESSFACTOR;
1015 nvc0_set_vertex_buffers(struct pipe_context *pipe,
1016 unsigned start_slot, unsigned count,
1017 const struct pipe_vertex_buffer *vb)
1019 struct nvc0_context *nvc0 = nvc0_context(pipe);
1022 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_VTX);
1023 nvc0->dirty_3d |= NVC0_NEW_3D_ARRAYS;
1025 util_set_vertex_buffers_count(nvc0->vtxbuf, &nvc0->num_vtxbufs, vb,
1029 nvc0->vbo_user &= ~(((1ull << count) - 1) << start_slot);
1030 nvc0->constant_vbos &= ~(((1ull << count) - 1) << start_slot);
1031 nvc0->vtxbufs_coherent &= ~(((1ull << count) - 1) << start_slot);
1035 for (i = 0; i < count; ++i) {
1036 unsigned dst_index = start_slot + i;
1038 if (vb[i].user_buffer) {
1039 nvc0->vbo_user |= 1 << dst_index;
1040 if (!vb[i].stride && nvc0->screen->eng3d->oclass < GM107_3D_CLASS)
1041 nvc0->constant_vbos |= 1 << dst_index;
1043 nvc0->constant_vbos &= ~(1 << dst_index);
1044 nvc0->vtxbufs_coherent &= ~(1 << dst_index);
1046 nvc0->vbo_user &= ~(1 << dst_index);
1047 nvc0->constant_vbos &= ~(1 << dst_index);
1050 vb[i].buffer->flags & PIPE_RESOURCE_FLAG_MAP_COHERENT)
1051 nvc0->vtxbufs_coherent |= (1 << dst_index);
1053 nvc0->vtxbufs_coherent &= ~(1 << dst_index);
1059 nvc0_set_index_buffer(struct pipe_context *pipe,
1060 const struct pipe_index_buffer *ib)
1062 struct nvc0_context *nvc0 = nvc0_context(pipe);
1064 if (nvc0->idxbuf.buffer)
1065 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_IDX);
1068 pipe_resource_reference(&nvc0->idxbuf.buffer, ib->buffer);
1069 nvc0->idxbuf.index_size = ib->index_size;
1071 nvc0->idxbuf.offset = ib->offset;
1072 nvc0->dirty_3d |= NVC0_NEW_3D_IDXBUF;
1074 nvc0->idxbuf.user_buffer = ib->user_buffer;
1075 nvc0->dirty_3d &= ~NVC0_NEW_3D_IDXBUF;
1078 nvc0->dirty_3d &= ~NVC0_NEW_3D_IDXBUF;
1079 pipe_resource_reference(&nvc0->idxbuf.buffer, NULL);
1084 nvc0_vertex_state_bind(struct pipe_context *pipe, void *hwcso)
1086 struct nvc0_context *nvc0 = nvc0_context(pipe);
1088 nvc0->vertex = hwcso;
1089 nvc0->dirty_3d |= NVC0_NEW_3D_VERTEX;
1092 static struct pipe_stream_output_target *
1093 nvc0_so_target_create(struct pipe_context *pipe,
1094 struct pipe_resource *res,
1095 unsigned offset, unsigned size)
1097 struct nv04_resource *buf = (struct nv04_resource *)res;
1098 struct nvc0_so_target *targ = MALLOC_STRUCT(nvc0_so_target);
1102 targ->pq = pipe->create_query(pipe, NVC0_HW_QUERY_TFB_BUFFER_OFFSET, 0);
1109 targ->pipe.buffer_size = size;
1110 targ->pipe.buffer_offset = offset;
1111 targ->pipe.context = pipe;
1112 targ->pipe.buffer = NULL;
1113 pipe_resource_reference(&targ->pipe.buffer, res);
1114 pipe_reference_init(&targ->pipe.reference, 1);
1116 assert(buf->base.target == PIPE_BUFFER);
1117 util_range_add(&buf->valid_buffer_range, offset, offset + size);
1123 nvc0_so_target_save_offset(struct pipe_context *pipe,
1124 struct pipe_stream_output_target *ptarg,
1125 unsigned index, bool *serialize)
1127 struct nvc0_so_target *targ = nvc0_so_target(ptarg);
1131 PUSH_SPACE(nvc0_context(pipe)->base.pushbuf, 1);
1132 IMMED_NVC0(nvc0_context(pipe)->base.pushbuf, NVC0_3D(SERIALIZE), 0);
1134 NOUVEAU_DRV_STAT(nouveau_screen(pipe->screen), gpu_serialize_count, 1);
1137 nvc0_query(targ->pq)->index = index;
1138 pipe->end_query(pipe, targ->pq);
1142 nvc0_so_target_destroy(struct pipe_context *pipe,
1143 struct pipe_stream_output_target *ptarg)
1145 struct nvc0_so_target *targ = nvc0_so_target(ptarg);
1146 pipe->destroy_query(pipe, targ->pq);
1147 pipe_resource_reference(&targ->pipe.buffer, NULL);
1152 nvc0_set_transform_feedback_targets(struct pipe_context *pipe,
1153 unsigned num_targets,
1154 struct pipe_stream_output_target **targets,
1155 const unsigned *offsets)
1157 struct nvc0_context *nvc0 = nvc0_context(pipe);
1159 bool serialize = true;
1161 assert(num_targets <= 4);
1163 for (i = 0; i < num_targets; ++i) {
1164 const bool changed = nvc0->tfbbuf[i] != targets[i];
1165 const bool append = (offsets[i] == ((unsigned)-1));
1166 if (!changed && append)
1168 nvc0->tfbbuf_dirty |= 1 << i;
1170 if (nvc0->tfbbuf[i] && changed)
1171 nvc0_so_target_save_offset(pipe, nvc0->tfbbuf[i], i, &serialize);
1173 if (targets[i] && !append)
1174 nvc0_so_target(targets[i])->clean = true;
1176 pipe_so_target_reference(&nvc0->tfbbuf[i], targets[i]);
1178 for (; i < nvc0->num_tfbbufs; ++i) {
1179 if (nvc0->tfbbuf[i]) {
1180 nvc0->tfbbuf_dirty |= 1 << i;
1181 nvc0_so_target_save_offset(pipe, nvc0->tfbbuf[i], i, &serialize);
1182 pipe_so_target_reference(&nvc0->tfbbuf[i], NULL);
1185 nvc0->num_tfbbufs = num_targets;
1187 if (nvc0->tfbbuf_dirty) {
1188 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_TFB);
1189 nvc0->dirty_3d |= NVC0_NEW_3D_TFB_TARGETS;
1194 nvc0_bind_surfaces_range(struct nvc0_context *nvc0, const unsigned t,
1195 unsigned start, unsigned nr,
1196 struct pipe_surface **psurfaces)
1198 const unsigned end = start + nr;
1199 const unsigned mask = ((1 << nr) - 1) << start;
1203 for (i = start; i < end; ++i) {
1204 const unsigned p = i - start;
1206 nvc0->surfaces_valid[t] |= (1 << i);
1208 nvc0->surfaces_valid[t] &= ~(1 << i);
1209 pipe_surface_reference(&nvc0->surfaces[t][i], psurfaces[p]);
1212 for (i = start; i < end; ++i)
1213 pipe_surface_reference(&nvc0->surfaces[t][i], NULL);
1214 nvc0->surfaces_valid[t] &= ~mask;
1216 nvc0->surfaces_dirty[t] |= mask;
1219 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_SUF);
1221 nouveau_bufctx_reset(nvc0->bufctx_cp, NVC0_BIND_CP_SUF);
1225 nvc0_set_compute_resources(struct pipe_context *pipe,
1226 unsigned start, unsigned nr,
1227 struct pipe_surface **resources)
1229 nvc0_bind_surfaces_range(nvc0_context(pipe), 1, start, nr, resources);
1231 nvc0_context(pipe)->dirty_cp |= NVC0_NEW_CP_SURFACES;
1235 nvc0_bind_images_range(struct nvc0_context *nvc0, const unsigned s,
1236 unsigned start, unsigned nr,
1237 struct pipe_image_view *pimages)
1239 const unsigned end = start + nr;
1240 const unsigned mask = ((1 << nr) - 1) << start;
1246 for (i = start; i < end; ++i) {
1247 const unsigned p = i - start;
1248 if (pimages[p].resource)
1249 nvc0->images_valid[s] |= (1 << i);
1251 nvc0->images_valid[s] &= ~(1 << i);
1253 nvc0->images[s][i].format = pimages[p].format;
1254 nvc0->images[s][i].access = pimages[p].access;
1255 if (pimages[p].resource->target == PIPE_BUFFER)
1256 nvc0->images[s][i].u.buf = pimages[p].u.buf;
1258 nvc0->images[s][i].u.tex = pimages[p].u.tex;
1260 pipe_resource_reference(
1261 &nvc0->images[s][i].resource, pimages[p].resource);
1264 for (i = start; i < end; ++i)
1265 pipe_resource_reference(&nvc0->images[s][i].resource, NULL);
1266 nvc0->images_valid[s] &= ~mask;
1268 nvc0->images_dirty[s] |= mask;
1271 nouveau_bufctx_reset(nvc0->bufctx_cp, NVC0_BIND_CP_SUF);
1273 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_SUF);
1277 nvc0_set_shader_images(struct pipe_context *pipe, unsigned shader,
1278 unsigned start, unsigned nr,
1279 struct pipe_image_view *images)
1281 const unsigned s = nvc0_shader_stage(shader);
1282 nvc0_bind_images_range(nvc0_context(pipe), s, start, nr, images);
1285 nvc0_context(pipe)->dirty_cp |= NVC0_NEW_CP_SURFACES;
1287 nvc0_context(pipe)->dirty_3d |= NVC0_NEW_3D_SURFACES;
1291 nvc0_bind_buffers_range(struct nvc0_context *nvc0, const unsigned t,
1292 unsigned start, unsigned nr,
1293 struct pipe_shader_buffer *pbuffers)
1295 const unsigned end = start + nr;
1302 for (i = start; i < end; ++i) {
1303 struct pipe_shader_buffer *buf = &nvc0->buffers[t][i];
1304 const unsigned p = i - start;
1305 if (buf->buffer == pbuffers[p].buffer &&
1306 buf->buffer_offset == pbuffers[p].buffer_offset &&
1307 buf->buffer_size == pbuffers[p].buffer_size)
1311 if (pbuffers[p].buffer)
1312 nvc0->buffers_valid[t] |= (1 << i);
1314 nvc0->buffers_valid[t] &= ~(1 << i);
1315 buf->buffer_offset = pbuffers[p].buffer_offset;
1316 buf->buffer_size = pbuffers[p].buffer_size;
1317 pipe_resource_reference(&buf->buffer, pbuffers[p].buffer);
1322 mask = ((1 << nr) - 1) << start;
1323 if (!(nvc0->buffers_valid[t] & mask))
1325 for (i = start; i < end; ++i)
1326 pipe_resource_reference(&nvc0->buffers[t][i].buffer, NULL);
1327 nvc0->buffers_valid[t] &= ~mask;
1329 nvc0->buffers_dirty[t] |= mask;
1332 nouveau_bufctx_reset(nvc0->bufctx_cp, NVC0_BIND_CP_BUF);
1334 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_BUF);
1340 nvc0_set_shader_buffers(struct pipe_context *pipe,
1342 unsigned start, unsigned nr,
1343 struct pipe_shader_buffer *buffers)
1345 const unsigned s = nvc0_shader_stage(shader);
1346 if (!nvc0_bind_buffers_range(nvc0_context(pipe), s, start, nr, buffers))
1350 nvc0_context(pipe)->dirty_cp |= NVC0_NEW_CP_BUFFERS;
1352 nvc0_context(pipe)->dirty_3d |= NVC0_NEW_3D_BUFFERS;
1356 nvc0_set_global_handle(uint32_t *phandle, struct pipe_resource *res)
1358 struct nv04_resource *buf = nv04_resource(res);
1360 uint64_t limit = (buf->address + buf->base.width0) - 1;
1361 if (limit < (1ULL << 32)) {
1362 *phandle = (uint32_t)buf->address;
1364 NOUVEAU_ERR("Cannot map into TGSI_RESOURCE_GLOBAL: "
1365 "resource not contained within 32-bit address space !\n");
1374 nvc0_set_global_bindings(struct pipe_context *pipe,
1375 unsigned start, unsigned nr,
1376 struct pipe_resource **resources,
1379 struct nvc0_context *nvc0 = nvc0_context(pipe);
1380 struct pipe_resource **ptr;
1382 const unsigned end = start + nr;
1384 if (nvc0->global_residents.size <= (end * sizeof(struct pipe_resource *))) {
1385 const unsigned old_size = nvc0->global_residents.size;
1386 const unsigned req_size = end * sizeof(struct pipe_resource *);
1387 util_dynarray_resize(&nvc0->global_residents, req_size);
1388 memset((uint8_t *)nvc0->global_residents.data + old_size, 0,
1389 req_size - old_size);
1393 ptr = util_dynarray_element(
1394 &nvc0->global_residents, struct pipe_resource *, start);
1395 for (i = 0; i < nr; ++i) {
1396 pipe_resource_reference(&ptr[i], resources[i]);
1397 nvc0_set_global_handle(handles[i], resources[i]);
1400 ptr = util_dynarray_element(
1401 &nvc0->global_residents, struct pipe_resource *, start);
1402 for (i = 0; i < nr; ++i)
1403 pipe_resource_reference(&ptr[i], NULL);
1406 nouveau_bufctx_reset(nvc0->bufctx_cp, NVC0_BIND_CP_GLOBAL);
1408 nvc0->dirty_cp |= NVC0_NEW_CP_GLOBALS;
1412 nvc0_init_state_functions(struct nvc0_context *nvc0)
1414 struct pipe_context *pipe = &nvc0->base.pipe;
1416 pipe->create_blend_state = nvc0_blend_state_create;
1417 pipe->bind_blend_state = nvc0_blend_state_bind;
1418 pipe->delete_blend_state = nvc0_blend_state_delete;
1420 pipe->create_rasterizer_state = nvc0_rasterizer_state_create;
1421 pipe->bind_rasterizer_state = nvc0_rasterizer_state_bind;
1422 pipe->delete_rasterizer_state = nvc0_rasterizer_state_delete;
1424 pipe->create_depth_stencil_alpha_state = nvc0_zsa_state_create;
1425 pipe->bind_depth_stencil_alpha_state = nvc0_zsa_state_bind;
1426 pipe->delete_depth_stencil_alpha_state = nvc0_zsa_state_delete;
1428 pipe->create_sampler_state = nv50_sampler_state_create;
1429 pipe->delete_sampler_state = nvc0_sampler_state_delete;
1430 pipe->bind_sampler_states = nvc0_bind_sampler_states;
1432 pipe->create_sampler_view = nvc0_create_sampler_view;
1433 pipe->sampler_view_destroy = nvc0_sampler_view_destroy;
1434 pipe->set_sampler_views = nvc0_set_sampler_views;
1436 pipe->create_vs_state = nvc0_vp_state_create;
1437 pipe->create_fs_state = nvc0_fp_state_create;
1438 pipe->create_gs_state = nvc0_gp_state_create;
1439 pipe->create_tcs_state = nvc0_tcp_state_create;
1440 pipe->create_tes_state = nvc0_tep_state_create;
1441 pipe->bind_vs_state = nvc0_vp_state_bind;
1442 pipe->bind_fs_state = nvc0_fp_state_bind;
1443 pipe->bind_gs_state = nvc0_gp_state_bind;
1444 pipe->bind_tcs_state = nvc0_tcp_state_bind;
1445 pipe->bind_tes_state = nvc0_tep_state_bind;
1446 pipe->delete_vs_state = nvc0_sp_state_delete;
1447 pipe->delete_fs_state = nvc0_sp_state_delete;
1448 pipe->delete_gs_state = nvc0_sp_state_delete;
1449 pipe->delete_tcs_state = nvc0_sp_state_delete;
1450 pipe->delete_tes_state = nvc0_sp_state_delete;
1452 pipe->create_compute_state = nvc0_cp_state_create;
1453 pipe->bind_compute_state = nvc0_cp_state_bind;
1454 pipe->delete_compute_state = nvc0_sp_state_delete;
1456 pipe->set_blend_color = nvc0_set_blend_color;
1457 pipe->set_stencil_ref = nvc0_set_stencil_ref;
1458 pipe->set_clip_state = nvc0_set_clip_state;
1459 pipe->set_sample_mask = nvc0_set_sample_mask;
1460 pipe->set_min_samples = nvc0_set_min_samples;
1461 pipe->set_constant_buffer = nvc0_set_constant_buffer;
1462 pipe->set_framebuffer_state = nvc0_set_framebuffer_state;
1463 pipe->set_polygon_stipple = nvc0_set_polygon_stipple;
1464 pipe->set_scissor_states = nvc0_set_scissor_states;
1465 pipe->set_viewport_states = nvc0_set_viewport_states;
1466 pipe->set_tess_state = nvc0_set_tess_state;
1468 pipe->create_vertex_elements_state = nvc0_vertex_state_create;
1469 pipe->delete_vertex_elements_state = nvc0_vertex_state_delete;
1470 pipe->bind_vertex_elements_state = nvc0_vertex_state_bind;
1472 pipe->set_vertex_buffers = nvc0_set_vertex_buffers;
1473 pipe->set_index_buffer = nvc0_set_index_buffer;
1475 pipe->create_stream_output_target = nvc0_so_target_create;
1476 pipe->stream_output_target_destroy = nvc0_so_target_destroy;
1477 pipe->set_stream_output_targets = nvc0_set_transform_feedback_targets;
1479 pipe->set_global_binding = nvc0_set_global_bindings;
1480 pipe->set_compute_resources = nvc0_set_compute_resources;
1481 pipe->set_shader_images = nvc0_set_shader_images;
1482 pipe->set_shader_buffers = nvc0_set_shader_buffers;
1484 nvc0->sample_mask = ~0;
1485 nvc0->min_samples = 1;
1486 nvc0->default_tess_outer[0] =
1487 nvc0->default_tess_outer[1] =
1488 nvc0->default_tess_outer[2] =
1489 nvc0->default_tess_outer[3] = 1.0;
1490 nvc0->default_tess_inner[0] =
1491 nvc0->default_tess_inner[1] = 1.0;