gallium: split depth_clip into depth_clip_near & depth_clip_far
[mesa.git] / src / gallium / drivers / nouveau / nvc0 / nvc0_state.c
1 /*
2 * Copyright 2010 Christoph Bumiller
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
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.
21 */
22
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"
28
29 #include "tgsi/tgsi_parse.h"
30
31 #include "nvc0/nvc0_stateobj.h"
32 #include "nvc0/nvc0_context.h"
33 #include "nvc0/nvc0_query_hw.h"
34
35 #include "nvc0/nvc0_3d.xml.h"
36
37 #include "nouveau_gldefs.h"
38
39 static inline uint32_t
40 nvc0_colormask(unsigned mask)
41 {
42 uint32_t ret = 0;
43
44 if (mask & PIPE_MASK_R)
45 ret |= 0x0001;
46 if (mask & PIPE_MASK_G)
47 ret |= 0x0010;
48 if (mask & PIPE_MASK_B)
49 ret |= 0x0100;
50 if (mask & PIPE_MASK_A)
51 ret |= 0x1000;
52
53 return ret;
54 }
55
56 #define NVC0_BLEND_FACTOR_CASE(a, b) \
57 case PIPE_BLENDFACTOR_##a: return NV50_BLEND_FACTOR_##b
58
59 static inline uint32_t
60 nvc0_blend_fac(unsigned factor)
61 {
62 switch (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);
82 default:
83 return NV50_BLEND_FACTOR_ZERO;
84 }
85 }
86
87 static void *
88 nvc0_blend_state_create(struct pipe_context *pipe,
89 const struct pipe_blend_state *cso)
90 {
91 struct nvc0_blend_stateobj *so = CALLOC_STRUCT(nvc0_blend_stateobj);
92 int i;
93 int r; /* reference */
94 uint32_t ms;
95 uint8_t blend_en = 0;
96 bool indep_masks = false;
97 bool indep_funcs = false;
98
99 so->pipe = *cso;
100
101 /* check which states actually have differing values */
102 if (cso->independent_blend_enable) {
103 for (r = 0; r < 8 && !cso->rt[r].blend_enable; ++r);
104 blend_en |= 1 << r;
105 for (i = r + 1; i < 8; ++i) {
106 if (!cso->rt[i].blend_enable)
107 continue;
108 blend_en |= 1 << i;
109 if (cso->rt[i].rgb_func != cso->rt[r].rgb_func ||
110 cso->rt[i].rgb_src_factor != cso->rt[r].rgb_src_factor ||
111 cso->rt[i].rgb_dst_factor != cso->rt[r].rgb_dst_factor ||
112 cso->rt[i].alpha_func != cso->rt[r].alpha_func ||
113 cso->rt[i].alpha_src_factor != cso->rt[r].alpha_src_factor ||
114 cso->rt[i].alpha_dst_factor != cso->rt[r].alpha_dst_factor) {
115 indep_funcs = true;
116 break;
117 }
118 }
119 for (; i < 8; ++i)
120 blend_en |= (cso->rt[i].blend_enable ? 1 : 0) << i;
121
122 for (i = 1; i < 8; ++i) {
123 if (cso->rt[i].colormask != cso->rt[0].colormask) {
124 indep_masks = true;
125 break;
126 }
127 }
128 } else {
129 r = 0;
130 if (cso->rt[0].blend_enable)
131 blend_en = 0xff;
132 }
133
134 if (cso->logicop_enable) {
135 SB_BEGIN_3D(so, LOGIC_OP_ENABLE, 2);
136 SB_DATA (so, 1);
137 SB_DATA (so, nvgl_logicop_func(cso->logicop_func));
138
139 SB_IMMED_3D(so, MACRO_BLEND_ENABLES, 0);
140 } else {
141 SB_IMMED_3D(so, LOGIC_OP_ENABLE, 0);
142
143 SB_IMMED_3D(so, BLEND_INDEPENDENT, indep_funcs);
144 SB_IMMED_3D(so, MACRO_BLEND_ENABLES, blend_en);
145 if (indep_funcs) {
146 for (i = 0; i < 8; ++i) {
147 if (cso->rt[i].blend_enable) {
148 SB_BEGIN_3D(so, IBLEND_EQUATION_RGB(i), 6);
149 SB_DATA (so, nvgl_blend_eqn(cso->rt[i].rgb_func));
150 SB_DATA (so, nvc0_blend_fac(cso->rt[i].rgb_src_factor));
151 SB_DATA (so, nvc0_blend_fac(cso->rt[i].rgb_dst_factor));
152 SB_DATA (so, nvgl_blend_eqn(cso->rt[i].alpha_func));
153 SB_DATA (so, nvc0_blend_fac(cso->rt[i].alpha_src_factor));
154 SB_DATA (so, nvc0_blend_fac(cso->rt[i].alpha_dst_factor));
155 }
156 }
157 } else
158 if (blend_en) {
159 SB_BEGIN_3D(so, BLEND_EQUATION_RGB, 5);
160 SB_DATA (so, nvgl_blend_eqn(cso->rt[r].rgb_func));
161 SB_DATA (so, nvc0_blend_fac(cso->rt[r].rgb_src_factor));
162 SB_DATA (so, nvc0_blend_fac(cso->rt[r].rgb_dst_factor));
163 SB_DATA (so, nvgl_blend_eqn(cso->rt[r].alpha_func));
164 SB_DATA (so, nvc0_blend_fac(cso->rt[r].alpha_src_factor));
165 SB_BEGIN_3D(so, BLEND_FUNC_DST_ALPHA, 1);
166 SB_DATA (so, nvc0_blend_fac(cso->rt[r].alpha_dst_factor));
167 }
168
169 SB_IMMED_3D(so, COLOR_MASK_COMMON, !indep_masks);
170 if (indep_masks) {
171 SB_BEGIN_3D(so, COLOR_MASK(0), 8);
172 for (i = 0; i < 8; ++i)
173 SB_DATA(so, nvc0_colormask(cso->rt[i].colormask));
174 } else {
175 SB_BEGIN_3D(so, COLOR_MASK(0), 1);
176 SB_DATA (so, nvc0_colormask(cso->rt[0].colormask));
177 }
178 }
179
180 ms = 0;
181 if (cso->alpha_to_coverage)
182 ms |= NVC0_3D_MULTISAMPLE_CTRL_ALPHA_TO_COVERAGE;
183 if (cso->alpha_to_one)
184 ms |= NVC0_3D_MULTISAMPLE_CTRL_ALPHA_TO_ONE;
185
186 SB_BEGIN_3D(so, MULTISAMPLE_CTRL, 1);
187 SB_DATA (so, ms);
188
189 assert(so->size <= ARRAY_SIZE(so->state));
190 return so;
191 }
192
193 static void
194 nvc0_blend_state_bind(struct pipe_context *pipe, void *hwcso)
195 {
196 struct nvc0_context *nvc0 = nvc0_context(pipe);
197
198 nvc0->blend = hwcso;
199 nvc0->dirty_3d |= NVC0_NEW_3D_BLEND;
200 }
201
202 static void
203 nvc0_blend_state_delete(struct pipe_context *pipe, void *hwcso)
204 {
205 FREE(hwcso);
206 }
207
208 /* NOTE: ignoring line_last_pixel */
209 static void *
210 nvc0_rasterizer_state_create(struct pipe_context *pipe,
211 const struct pipe_rasterizer_state *cso)
212 {
213 struct nvc0_rasterizer_stateobj *so;
214 uint16_t class_3d = nouveau_screen(pipe->screen)->class_3d;
215 uint32_t reg;
216
217 so = CALLOC_STRUCT(nvc0_rasterizer_stateobj);
218 if (!so)
219 return NULL;
220 so->pipe = *cso;
221
222 /* Scissor enables are handled in scissor state, we will not want to
223 * always emit 16 commands, one for each scissor rectangle, here.
224 */
225
226 SB_IMMED_3D(so, PROVOKING_VERTEX_LAST, !cso->flatshade_first);
227 SB_IMMED_3D(so, VERTEX_TWO_SIDE_ENABLE, cso->light_twoside);
228
229 SB_IMMED_3D(so, VERT_COLOR_CLAMP_EN, cso->clamp_vertex_color);
230 SB_BEGIN_3D(so, FRAG_COLOR_CLAMP_EN, 1);
231 SB_DATA (so, cso->clamp_fragment_color ? 0x11111111 : 0x00000000);
232
233 SB_IMMED_3D(so, MULTISAMPLE_ENABLE, cso->multisample);
234
235 SB_IMMED_3D(so, LINE_SMOOTH_ENABLE, cso->line_smooth);
236 /* On GM20x+, LINE_WIDTH_SMOOTH controls both aliased and smooth
237 * rendering and LINE_WIDTH_ALIASED seems to be ignored
238 */
239 if (cso->line_smooth || cso->multisample || class_3d >= GM200_3D_CLASS)
240 SB_BEGIN_3D(so, LINE_WIDTH_SMOOTH, 1);
241 else
242 SB_BEGIN_3D(so, LINE_WIDTH_ALIASED, 1);
243 SB_DATA (so, fui(cso->line_width));
244
245 SB_IMMED_3D(so, LINE_STIPPLE_ENABLE, cso->line_stipple_enable);
246 if (cso->line_stipple_enable) {
247 SB_BEGIN_3D(so, LINE_STIPPLE_PATTERN, 1);
248 SB_DATA (so, (cso->line_stipple_pattern << 8) |
249 cso->line_stipple_factor);
250
251 }
252
253 SB_IMMED_3D(so, VP_POINT_SIZE, cso->point_size_per_vertex);
254 if (!cso->point_size_per_vertex) {
255 SB_BEGIN_3D(so, POINT_SIZE, 1);
256 SB_DATA (so, fui(cso->point_size));
257 }
258
259 reg = (cso->sprite_coord_mode == PIPE_SPRITE_COORD_UPPER_LEFT) ?
260 NVC0_3D_POINT_COORD_REPLACE_COORD_ORIGIN_UPPER_LEFT :
261 NVC0_3D_POINT_COORD_REPLACE_COORD_ORIGIN_LOWER_LEFT;
262
263 SB_BEGIN_3D(so, POINT_COORD_REPLACE, 1);
264 SB_DATA (so, ((cso->sprite_coord_enable & 0xff) << 3) | reg);
265 SB_IMMED_3D(so, POINT_SPRITE_ENABLE, cso->point_quad_rasterization);
266 SB_IMMED_3D(so, POINT_SMOOTH_ENABLE, cso->point_smooth);
267
268 if (class_3d >= GM200_3D_CLASS) {
269 SB_IMMED_3D(so, FILL_RECTANGLE,
270 cso->fill_front == PIPE_POLYGON_MODE_FILL_RECTANGLE ?
271 NVC0_3D_FILL_RECTANGLE_ENABLE : 0);
272 }
273
274 SB_BEGIN_3D(so, MACRO_POLYGON_MODE_FRONT, 1);
275 SB_DATA (so, nvgl_polygon_mode(cso->fill_front));
276 SB_BEGIN_3D(so, MACRO_POLYGON_MODE_BACK, 1);
277 SB_DATA (so, nvgl_polygon_mode(cso->fill_back));
278 SB_IMMED_3D(so, POLYGON_SMOOTH_ENABLE, cso->poly_smooth);
279
280 SB_BEGIN_3D(so, CULL_FACE_ENABLE, 3);
281 SB_DATA (so, cso->cull_face != PIPE_FACE_NONE);
282 SB_DATA (so, cso->front_ccw ? NVC0_3D_FRONT_FACE_CCW :
283 NVC0_3D_FRONT_FACE_CW);
284 switch (cso->cull_face) {
285 case PIPE_FACE_FRONT_AND_BACK:
286 SB_DATA(so, NVC0_3D_CULL_FACE_FRONT_AND_BACK);
287 break;
288 case PIPE_FACE_FRONT:
289 SB_DATA(so, NVC0_3D_CULL_FACE_FRONT);
290 break;
291 case PIPE_FACE_BACK:
292 default:
293 SB_DATA(so, NVC0_3D_CULL_FACE_BACK);
294 break;
295 }
296
297 SB_IMMED_3D(so, POLYGON_STIPPLE_ENABLE, cso->poly_stipple_enable);
298 SB_BEGIN_3D(so, POLYGON_OFFSET_POINT_ENABLE, 3);
299 SB_DATA (so, cso->offset_point);
300 SB_DATA (so, cso->offset_line);
301 SB_DATA (so, cso->offset_tri);
302
303 if (cso->offset_point || cso->offset_line || cso->offset_tri) {
304 SB_BEGIN_3D(so, POLYGON_OFFSET_FACTOR, 1);
305 SB_DATA (so, fui(cso->offset_scale));
306 if (!cso->offset_units_unscaled) {
307 SB_BEGIN_3D(so, POLYGON_OFFSET_UNITS, 1);
308 SB_DATA (so, fui(cso->offset_units * 2.0f));
309 }
310 SB_BEGIN_3D(so, POLYGON_OFFSET_CLAMP, 1);
311 SB_DATA (so, fui(cso->offset_clamp));
312 }
313
314 if (cso->depth_clip_near)
315 reg = NVC0_3D_VIEW_VOLUME_CLIP_CTRL_UNK1_UNK1;
316 else
317 reg =
318 NVC0_3D_VIEW_VOLUME_CLIP_CTRL_UNK1_UNK1 |
319 NVC0_3D_VIEW_VOLUME_CLIP_CTRL_DEPTH_CLAMP_NEAR |
320 NVC0_3D_VIEW_VOLUME_CLIP_CTRL_DEPTH_CLAMP_FAR |
321 NVC0_3D_VIEW_VOLUME_CLIP_CTRL_UNK12_UNK2;
322
323 SB_BEGIN_3D(so, VIEW_VOLUME_CLIP_CTRL, 1);
324 SB_DATA (so, reg);
325
326 SB_IMMED_3D(so, DEPTH_CLIP_NEGATIVE_Z, cso->clip_halfz);
327
328 SB_IMMED_3D(so, PIXEL_CENTER_INTEGER, !cso->half_pixel_center);
329
330 if (class_3d >= GM200_3D_CLASS) {
331 if (cso->conservative_raster_mode != PIPE_CONSERVATIVE_RASTER_OFF) {
332 bool post_snap = cso->conservative_raster_mode ==
333 PIPE_CONSERVATIVE_RASTER_POST_SNAP;
334 uint32_t state = cso->subpixel_precision_x;
335 state |= cso->subpixel_precision_y << 4;
336 state |= (uint32_t)(cso->conservative_raster_dilate * 4) << 8;
337 state |= (post_snap || class_3d < GP100_3D_CLASS) ? 1 << 10 : 0;
338 SB_IMMED_3D(so, MACRO_CONSERVATIVE_RASTER_STATE, state);
339 } else {
340 SB_IMMED_3D(so, CONSERVATIVE_RASTER, 0);
341 }
342 }
343
344 assert(so->size <= ARRAY_SIZE(so->state));
345 return (void *)so;
346 }
347
348 static void
349 nvc0_rasterizer_state_bind(struct pipe_context *pipe, void *hwcso)
350 {
351 struct nvc0_context *nvc0 = nvc0_context(pipe);
352
353 nvc0->rast = hwcso;
354 nvc0->dirty_3d |= NVC0_NEW_3D_RASTERIZER;
355 }
356
357 static void
358 nvc0_rasterizer_state_delete(struct pipe_context *pipe, void *hwcso)
359 {
360 FREE(hwcso);
361 }
362
363 static void *
364 nvc0_zsa_state_create(struct pipe_context *pipe,
365 const struct pipe_depth_stencil_alpha_state *cso)
366 {
367 struct nvc0_zsa_stateobj *so = CALLOC_STRUCT(nvc0_zsa_stateobj);
368
369 so->pipe = *cso;
370
371 SB_IMMED_3D(so, DEPTH_TEST_ENABLE, cso->depth.enabled);
372 if (cso->depth.enabled) {
373 SB_IMMED_3D(so, DEPTH_WRITE_ENABLE, cso->depth.writemask);
374 SB_BEGIN_3D(so, DEPTH_TEST_FUNC, 1);
375 SB_DATA (so, nvgl_comparison_op(cso->depth.func));
376 }
377
378 SB_IMMED_3D(so, DEPTH_BOUNDS_EN, cso->depth.bounds_test);
379 if (cso->depth.bounds_test) {
380 SB_BEGIN_3D(so, DEPTH_BOUNDS(0), 2);
381 SB_DATA (so, fui(cso->depth.bounds_min));
382 SB_DATA (so, fui(cso->depth.bounds_max));
383 }
384
385 if (cso->stencil[0].enabled) {
386 SB_BEGIN_3D(so, STENCIL_ENABLE, 5);
387 SB_DATA (so, 1);
388 SB_DATA (so, nvgl_stencil_op(cso->stencil[0].fail_op));
389 SB_DATA (so, nvgl_stencil_op(cso->stencil[0].zfail_op));
390 SB_DATA (so, nvgl_stencil_op(cso->stencil[0].zpass_op));
391 SB_DATA (so, nvgl_comparison_op(cso->stencil[0].func));
392 SB_BEGIN_3D(so, STENCIL_FRONT_FUNC_MASK, 2);
393 SB_DATA (so, cso->stencil[0].valuemask);
394 SB_DATA (so, cso->stencil[0].writemask);
395 } else {
396 SB_IMMED_3D(so, STENCIL_ENABLE, 0);
397 }
398
399 if (cso->stencil[1].enabled) {
400 assert(cso->stencil[0].enabled);
401 SB_BEGIN_3D(so, STENCIL_TWO_SIDE_ENABLE, 5);
402 SB_DATA (so, 1);
403 SB_DATA (so, nvgl_stencil_op(cso->stencil[1].fail_op));
404 SB_DATA (so, nvgl_stencil_op(cso->stencil[1].zfail_op));
405 SB_DATA (so, nvgl_stencil_op(cso->stencil[1].zpass_op));
406 SB_DATA (so, nvgl_comparison_op(cso->stencil[1].func));
407 SB_BEGIN_3D(so, STENCIL_BACK_MASK, 2);
408 SB_DATA (so, cso->stencil[1].writemask);
409 SB_DATA (so, cso->stencil[1].valuemask);
410 } else
411 if (cso->stencil[0].enabled) {
412 SB_IMMED_3D(so, STENCIL_TWO_SIDE_ENABLE, 0);
413 }
414
415 SB_IMMED_3D(so, ALPHA_TEST_ENABLE, cso->alpha.enabled);
416 if (cso->alpha.enabled) {
417 SB_BEGIN_3D(so, ALPHA_TEST_REF, 2);
418 SB_DATA (so, fui(cso->alpha.ref_value));
419 SB_DATA (so, nvgl_comparison_op(cso->alpha.func));
420 }
421
422 assert(so->size <= ARRAY_SIZE(so->state));
423 return (void *)so;
424 }
425
426 static void
427 nvc0_zsa_state_bind(struct pipe_context *pipe, void *hwcso)
428 {
429 struct nvc0_context *nvc0 = nvc0_context(pipe);
430
431 nvc0->zsa = hwcso;
432 nvc0->dirty_3d |= NVC0_NEW_3D_ZSA;
433 }
434
435 static void
436 nvc0_zsa_state_delete(struct pipe_context *pipe, void *hwcso)
437 {
438 FREE(hwcso);
439 }
440
441 /* ====================== SAMPLERS AND TEXTURES ================================
442 */
443
444 #define NV50_TSC_WRAP_CASE(n) \
445 case PIPE_TEX_WRAP_##n: return NV50_TSC_WRAP_##n
446
447 static void
448 nvc0_sampler_state_delete(struct pipe_context *pipe, void *hwcso)
449 {
450 unsigned s, i;
451
452 for (s = 0; s < 6; ++s)
453 for (i = 0; i < nvc0_context(pipe)->num_samplers[s]; ++i)
454 if (nvc0_context(pipe)->samplers[s][i] == hwcso)
455 nvc0_context(pipe)->samplers[s][i] = NULL;
456
457 nvc0_screen_tsc_free(nvc0_context(pipe)->screen, nv50_tsc_entry(hwcso));
458
459 FREE(hwcso);
460 }
461
462 static inline void
463 nvc0_stage_sampler_states_bind(struct nvc0_context *nvc0,
464 unsigned s,
465 unsigned nr, void **hwcso)
466 {
467 unsigned i;
468
469 for (i = 0; i < nr; ++i) {
470 struct nv50_tsc_entry *old = nvc0->samplers[s][i];
471
472 if (hwcso[i] == old)
473 continue;
474 nvc0->samplers_dirty[s] |= 1 << i;
475
476 nvc0->samplers[s][i] = nv50_tsc_entry(hwcso[i]);
477 if (old)
478 nvc0_screen_tsc_unlock(nvc0->screen, old);
479 }
480 for (; i < nvc0->num_samplers[s]; ++i) {
481 if (nvc0->samplers[s][i]) {
482 nvc0_screen_tsc_unlock(nvc0->screen, nvc0->samplers[s][i]);
483 nvc0->samplers[s][i] = NULL;
484 }
485 }
486
487 nvc0->num_samplers[s] = nr;
488 }
489
490 static void
491 nvc0_bind_sampler_states(struct pipe_context *pipe,
492 enum pipe_shader_type shader,
493 unsigned start, unsigned nr, void **samplers)
494 {
495 const unsigned s = nvc0_shader_stage(shader);
496
497 assert(start == 0);
498 nvc0_stage_sampler_states_bind(nvc0_context(pipe), s, nr, samplers);
499
500 if (s == 5)
501 nvc0_context(pipe)->dirty_cp |= NVC0_NEW_CP_SAMPLERS;
502 else
503 nvc0_context(pipe)->dirty_3d |= NVC0_NEW_3D_SAMPLERS;
504 }
505
506
507 /* NOTE: only called when not referenced anywhere, won't be bound */
508 static void
509 nvc0_sampler_view_destroy(struct pipe_context *pipe,
510 struct pipe_sampler_view *view)
511 {
512 pipe_resource_reference(&view->texture, NULL);
513
514 nvc0_screen_tic_free(nvc0_context(pipe)->screen, nv50_tic_entry(view));
515
516 FREE(nv50_tic_entry(view));
517 }
518
519 static inline void
520 nvc0_stage_set_sampler_views(struct nvc0_context *nvc0, int s,
521 unsigned nr,
522 struct pipe_sampler_view **views)
523 {
524 unsigned i;
525
526 for (i = 0; i < nr; ++i) {
527 struct nv50_tic_entry *old = nv50_tic_entry(nvc0->textures[s][i]);
528
529 if (views[i] == nvc0->textures[s][i])
530 continue;
531 nvc0->textures_dirty[s] |= 1 << i;
532
533 if (views[i] && views[i]->texture) {
534 struct pipe_resource *res = views[i]->texture;
535 if (res->target == PIPE_BUFFER &&
536 (res->flags & PIPE_RESOURCE_FLAG_MAP_COHERENT))
537 nvc0->textures_coherent[s] |= 1 << i;
538 else
539 nvc0->textures_coherent[s] &= ~(1 << i);
540 } else {
541 nvc0->textures_coherent[s] &= ~(1 << i);
542 }
543
544 if (old) {
545 if (s == 5)
546 nouveau_bufctx_reset(nvc0->bufctx_cp, NVC0_BIND_CP_TEX(i));
547 else
548 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_TEX(s, i));
549 nvc0_screen_tic_unlock(nvc0->screen, old);
550 }
551
552 pipe_sampler_view_reference(&nvc0->textures[s][i], views[i]);
553 }
554
555 for (i = nr; i < nvc0->num_textures[s]; ++i) {
556 struct nv50_tic_entry *old = nv50_tic_entry(nvc0->textures[s][i]);
557 if (old) {
558 if (s == 5)
559 nouveau_bufctx_reset(nvc0->bufctx_cp, NVC0_BIND_CP_TEX(i));
560 else
561 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_TEX(s, i));
562 nvc0_screen_tic_unlock(nvc0->screen, old);
563 pipe_sampler_view_reference(&nvc0->textures[s][i], NULL);
564 }
565 }
566
567 nvc0->num_textures[s] = nr;
568 }
569
570 static void
571 nvc0_set_sampler_views(struct pipe_context *pipe, enum pipe_shader_type shader,
572 unsigned start, unsigned nr,
573 struct pipe_sampler_view **views)
574 {
575 const unsigned s = nvc0_shader_stage(shader);
576
577 assert(start == 0);
578 nvc0_stage_set_sampler_views(nvc0_context(pipe), s, nr, views);
579
580 if (s == 5)
581 nvc0_context(pipe)->dirty_cp |= NVC0_NEW_CP_TEXTURES;
582 else
583 nvc0_context(pipe)->dirty_3d |= NVC0_NEW_3D_TEXTURES;
584 }
585
586 /* ============================= SHADERS =======================================
587 */
588
589 static void *
590 nvc0_sp_state_create(struct pipe_context *pipe,
591 const struct pipe_shader_state *cso, unsigned type)
592 {
593 struct nvc0_program *prog;
594
595 prog = CALLOC_STRUCT(nvc0_program);
596 if (!prog)
597 return NULL;
598
599 prog->type = type;
600
601 if (cso->tokens)
602 prog->pipe.tokens = tgsi_dup_tokens(cso->tokens);
603
604 if (cso->stream_output.num_outputs)
605 prog->pipe.stream_output = cso->stream_output;
606
607 prog->translated = nvc0_program_translate(
608 prog, nvc0_context(pipe)->screen->base.device->chipset,
609 &nouveau_context(pipe)->debug);
610
611 return (void *)prog;
612 }
613
614 static void
615 nvc0_sp_state_delete(struct pipe_context *pipe, void *hwcso)
616 {
617 struct nvc0_program *prog = (struct nvc0_program *)hwcso;
618
619 nvc0_program_destroy(nvc0_context(pipe), prog);
620
621 FREE((void *)prog->pipe.tokens);
622 FREE(prog);
623 }
624
625 static void *
626 nvc0_vp_state_create(struct pipe_context *pipe,
627 const struct pipe_shader_state *cso)
628 {
629 return nvc0_sp_state_create(pipe, cso, PIPE_SHADER_VERTEX);
630 }
631
632 static void
633 nvc0_vp_state_bind(struct pipe_context *pipe, void *hwcso)
634 {
635 struct nvc0_context *nvc0 = nvc0_context(pipe);
636
637 nvc0->vertprog = hwcso;
638 nvc0->dirty_3d |= NVC0_NEW_3D_VERTPROG;
639 }
640
641 static void *
642 nvc0_fp_state_create(struct pipe_context *pipe,
643 const struct pipe_shader_state *cso)
644 {
645 return nvc0_sp_state_create(pipe, cso, PIPE_SHADER_FRAGMENT);
646 }
647
648 static void
649 nvc0_fp_state_bind(struct pipe_context *pipe, void *hwcso)
650 {
651 struct nvc0_context *nvc0 = nvc0_context(pipe);
652
653 nvc0->fragprog = hwcso;
654 nvc0->dirty_3d |= NVC0_NEW_3D_FRAGPROG;
655 }
656
657 static void *
658 nvc0_gp_state_create(struct pipe_context *pipe,
659 const struct pipe_shader_state *cso)
660 {
661 return nvc0_sp_state_create(pipe, cso, PIPE_SHADER_GEOMETRY);
662 }
663
664 static void
665 nvc0_gp_state_bind(struct pipe_context *pipe, void *hwcso)
666 {
667 struct nvc0_context *nvc0 = nvc0_context(pipe);
668
669 nvc0->gmtyprog = hwcso;
670 nvc0->dirty_3d |= NVC0_NEW_3D_GMTYPROG;
671 }
672
673 static void *
674 nvc0_tcp_state_create(struct pipe_context *pipe,
675 const struct pipe_shader_state *cso)
676 {
677 return nvc0_sp_state_create(pipe, cso, PIPE_SHADER_TESS_CTRL);
678 }
679
680 static void
681 nvc0_tcp_state_bind(struct pipe_context *pipe, void *hwcso)
682 {
683 struct nvc0_context *nvc0 = nvc0_context(pipe);
684
685 nvc0->tctlprog = hwcso;
686 nvc0->dirty_3d |= NVC0_NEW_3D_TCTLPROG;
687 }
688
689 static void *
690 nvc0_tep_state_create(struct pipe_context *pipe,
691 const struct pipe_shader_state *cso)
692 {
693 return nvc0_sp_state_create(pipe, cso, PIPE_SHADER_TESS_EVAL);
694 }
695
696 static void
697 nvc0_tep_state_bind(struct pipe_context *pipe, void *hwcso)
698 {
699 struct nvc0_context *nvc0 = nvc0_context(pipe);
700
701 nvc0->tevlprog = hwcso;
702 nvc0->dirty_3d |= NVC0_NEW_3D_TEVLPROG;
703 }
704
705 static void *
706 nvc0_cp_state_create(struct pipe_context *pipe,
707 const struct pipe_compute_state *cso)
708 {
709 struct nvc0_program *prog;
710
711 prog = CALLOC_STRUCT(nvc0_program);
712 if (!prog)
713 return NULL;
714 prog->type = PIPE_SHADER_COMPUTE;
715
716 prog->cp.smem_size = cso->req_local_mem;
717 prog->cp.lmem_size = cso->req_private_mem;
718 prog->parm_size = cso->req_input_mem;
719
720 prog->pipe.tokens = tgsi_dup_tokens((const struct tgsi_token *)cso->prog);
721
722 prog->translated = nvc0_program_translate(
723 prog, nvc0_context(pipe)->screen->base.device->chipset,
724 &nouveau_context(pipe)->debug);
725
726 return (void *)prog;
727 }
728
729 static void
730 nvc0_cp_state_bind(struct pipe_context *pipe, void *hwcso)
731 {
732 struct nvc0_context *nvc0 = nvc0_context(pipe);
733
734 nvc0->compprog = hwcso;
735 nvc0->dirty_cp |= NVC0_NEW_CP_PROGRAM;
736 }
737
738 static void
739 nvc0_set_constant_buffer(struct pipe_context *pipe,
740 enum pipe_shader_type shader, uint index,
741 const struct pipe_constant_buffer *cb)
742 {
743 struct nvc0_context *nvc0 = nvc0_context(pipe);
744 struct pipe_resource *res = cb ? cb->buffer : NULL;
745 const unsigned s = nvc0_shader_stage(shader);
746 const unsigned i = index;
747
748 if (unlikely(shader == PIPE_SHADER_COMPUTE)) {
749 if (nvc0->constbuf[s][i].user)
750 nvc0->constbuf[s][i].u.buf = NULL;
751 else
752 if (nvc0->constbuf[s][i].u.buf)
753 nouveau_bufctx_reset(nvc0->bufctx_cp, NVC0_BIND_CP_CB(i));
754
755 nvc0->dirty_cp |= NVC0_NEW_CP_CONSTBUF;
756 } else {
757 if (nvc0->constbuf[s][i].user)
758 nvc0->constbuf[s][i].u.buf = NULL;
759 else
760 if (nvc0->constbuf[s][i].u.buf)
761 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_CB(s, i));
762
763 nvc0->dirty_3d |= NVC0_NEW_3D_CONSTBUF;
764 }
765 nvc0->constbuf_dirty[s] |= 1 << i;
766
767 if (nvc0->constbuf[s][i].u.buf)
768 nv04_resource(nvc0->constbuf[s][i].u.buf)->cb_bindings[s] &= ~(1 << i);
769 pipe_resource_reference(&nvc0->constbuf[s][i].u.buf, res);
770
771 nvc0->constbuf[s][i].user = (cb && cb->user_buffer) ? true : false;
772 if (nvc0->constbuf[s][i].user) {
773 nvc0->constbuf[s][i].u.data = cb->user_buffer;
774 nvc0->constbuf[s][i].size = MIN2(cb->buffer_size, 0x10000);
775 nvc0->constbuf_valid[s] |= 1 << i;
776 nvc0->constbuf_coherent[s] &= ~(1 << i);
777 } else
778 if (cb) {
779 nvc0->constbuf[s][i].offset = cb->buffer_offset;
780 nvc0->constbuf[s][i].size = MIN2(align(cb->buffer_size, 0x100), 0x10000);
781 nvc0->constbuf_valid[s] |= 1 << i;
782 if (res && res->flags & PIPE_RESOURCE_FLAG_MAP_COHERENT)
783 nvc0->constbuf_coherent[s] |= 1 << i;
784 else
785 nvc0->constbuf_coherent[s] &= ~(1 << i);
786 }
787 else {
788 nvc0->constbuf_valid[s] &= ~(1 << i);
789 nvc0->constbuf_coherent[s] &= ~(1 << i);
790 }
791 }
792
793 /* =============================================================================
794 */
795
796 static void
797 nvc0_set_blend_color(struct pipe_context *pipe,
798 const struct pipe_blend_color *bcol)
799 {
800 struct nvc0_context *nvc0 = nvc0_context(pipe);
801
802 nvc0->blend_colour = *bcol;
803 nvc0->dirty_3d |= NVC0_NEW_3D_BLEND_COLOUR;
804 }
805
806 static void
807 nvc0_set_stencil_ref(struct pipe_context *pipe,
808 const struct pipe_stencil_ref *sr)
809 {
810 struct nvc0_context *nvc0 = nvc0_context(pipe);
811
812 nvc0->stencil_ref = *sr;
813 nvc0->dirty_3d |= NVC0_NEW_3D_STENCIL_REF;
814 }
815
816 static void
817 nvc0_set_clip_state(struct pipe_context *pipe,
818 const struct pipe_clip_state *clip)
819 {
820 struct nvc0_context *nvc0 = nvc0_context(pipe);
821
822 memcpy(nvc0->clip.ucp, clip->ucp, sizeof(clip->ucp));
823
824 nvc0->dirty_3d |= NVC0_NEW_3D_CLIP;
825 }
826
827 static void
828 nvc0_set_sample_mask(struct pipe_context *pipe, unsigned sample_mask)
829 {
830 struct nvc0_context *nvc0 = nvc0_context(pipe);
831
832 nvc0->sample_mask = sample_mask;
833 nvc0->dirty_3d |= NVC0_NEW_3D_SAMPLE_MASK;
834 }
835
836 static void
837 nvc0_set_min_samples(struct pipe_context *pipe, unsigned min_samples)
838 {
839 struct nvc0_context *nvc0 = nvc0_context(pipe);
840
841 if (nvc0->min_samples != min_samples) {
842 nvc0->min_samples = min_samples;
843 nvc0->dirty_3d |= NVC0_NEW_3D_MIN_SAMPLES;
844 }
845 }
846
847 static void
848 nvc0_set_framebuffer_state(struct pipe_context *pipe,
849 const struct pipe_framebuffer_state *fb)
850 {
851 struct nvc0_context *nvc0 = nvc0_context(pipe);
852
853 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_FB);
854
855 util_copy_framebuffer_state(&nvc0->framebuffer, fb);
856
857 nvc0->dirty_3d |= NVC0_NEW_3D_FRAMEBUFFER | NVC0_NEW_3D_SAMPLE_LOCATIONS;
858 }
859
860 static void
861 nvc0_set_sample_locations(struct pipe_context *pipe,
862 size_t size, const uint8_t *locations)
863 {
864 struct nvc0_context *nvc0 = nvc0_context(pipe);
865
866 nvc0->sample_locations_enabled = size && locations;
867 if (size > sizeof(nvc0->sample_locations))
868 size = sizeof(nvc0->sample_locations);
869 memcpy(nvc0->sample_locations, locations, size);
870
871 nvc0->dirty_3d |= NVC0_NEW_3D_SAMPLE_LOCATIONS;
872 }
873
874 static void
875 nvc0_set_polygon_stipple(struct pipe_context *pipe,
876 const struct pipe_poly_stipple *stipple)
877 {
878 struct nvc0_context *nvc0 = nvc0_context(pipe);
879
880 nvc0->stipple = *stipple;
881 nvc0->dirty_3d |= NVC0_NEW_3D_STIPPLE;
882 }
883
884 static void
885 nvc0_set_scissor_states(struct pipe_context *pipe,
886 unsigned start_slot,
887 unsigned num_scissors,
888 const struct pipe_scissor_state *scissor)
889 {
890 struct nvc0_context *nvc0 = nvc0_context(pipe);
891 int i;
892
893 assert(start_slot + num_scissors <= NVC0_MAX_VIEWPORTS);
894 for (i = 0; i < num_scissors; i++) {
895 if (!memcmp(&nvc0->scissors[start_slot + i], &scissor[i], sizeof(*scissor)))
896 continue;
897 nvc0->scissors[start_slot + i] = scissor[i];
898 nvc0->scissors_dirty |= 1 << (start_slot + i);
899 nvc0->dirty_3d |= NVC0_NEW_3D_SCISSOR;
900 }
901 }
902
903 static void
904 nvc0_set_viewport_states(struct pipe_context *pipe,
905 unsigned start_slot,
906 unsigned num_viewports,
907 const struct pipe_viewport_state *vpt)
908 {
909 struct nvc0_context *nvc0 = nvc0_context(pipe);
910 int i;
911
912 assert(start_slot + num_viewports <= NVC0_MAX_VIEWPORTS);
913 for (i = 0; i < num_viewports; i++) {
914 if (!memcmp(&nvc0->viewports[start_slot + i], &vpt[i], sizeof(*vpt)))
915 continue;
916 nvc0->viewports[start_slot + i] = vpt[i];
917 nvc0->viewports_dirty |= 1 << (start_slot + i);
918 nvc0->dirty_3d |= NVC0_NEW_3D_VIEWPORT;
919 }
920
921 }
922
923 static void
924 nvc0_set_window_rectangles(struct pipe_context *pipe,
925 boolean include,
926 unsigned num_rectangles,
927 const struct pipe_scissor_state *rectangles)
928 {
929 struct nvc0_context *nvc0 = nvc0_context(pipe);
930
931 nvc0->window_rect.inclusive = include;
932 nvc0->window_rect.rects = MIN2(num_rectangles, NVC0_MAX_WINDOW_RECTANGLES);
933 memcpy(nvc0->window_rect.rect, rectangles,
934 sizeof(struct pipe_scissor_state) * nvc0->window_rect.rects);
935
936 nvc0->dirty_3d |= NVC0_NEW_3D_WINDOW_RECTS;
937 }
938
939 static void
940 nvc0_set_tess_state(struct pipe_context *pipe,
941 const float default_tess_outer[4],
942 const float default_tess_inner[2])
943 {
944 struct nvc0_context *nvc0 = nvc0_context(pipe);
945
946 memcpy(nvc0->default_tess_outer, default_tess_outer, 4 * sizeof(float));
947 memcpy(nvc0->default_tess_inner, default_tess_inner, 2 * sizeof(float));
948 nvc0->dirty_3d |= NVC0_NEW_3D_TESSFACTOR;
949 }
950
951 static void
952 nvc0_set_vertex_buffers(struct pipe_context *pipe,
953 unsigned start_slot, unsigned count,
954 const struct pipe_vertex_buffer *vb)
955 {
956 struct nvc0_context *nvc0 = nvc0_context(pipe);
957 unsigned i;
958
959 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_VTX);
960 nvc0->dirty_3d |= NVC0_NEW_3D_ARRAYS;
961
962 util_set_vertex_buffers_count(nvc0->vtxbuf, &nvc0->num_vtxbufs, vb,
963 start_slot, count);
964
965 if (!vb) {
966 nvc0->vbo_user &= ~(((1ull << count) - 1) << start_slot);
967 nvc0->constant_vbos &= ~(((1ull << count) - 1) << start_slot);
968 nvc0->vtxbufs_coherent &= ~(((1ull << count) - 1) << start_slot);
969 return;
970 }
971
972 for (i = 0; i < count; ++i) {
973 unsigned dst_index = start_slot + i;
974
975 if (vb[i].is_user_buffer) {
976 nvc0->vbo_user |= 1 << dst_index;
977 if (!vb[i].stride && nvc0->screen->eng3d->oclass < GM107_3D_CLASS)
978 nvc0->constant_vbos |= 1 << dst_index;
979 else
980 nvc0->constant_vbos &= ~(1 << dst_index);
981 nvc0->vtxbufs_coherent &= ~(1 << dst_index);
982 } else {
983 nvc0->vbo_user &= ~(1 << dst_index);
984 nvc0->constant_vbos &= ~(1 << dst_index);
985
986 if (vb[i].buffer.resource &&
987 vb[i].buffer.resource->flags & PIPE_RESOURCE_FLAG_MAP_COHERENT)
988 nvc0->vtxbufs_coherent |= (1 << dst_index);
989 else
990 nvc0->vtxbufs_coherent &= ~(1 << dst_index);
991 }
992 }
993 }
994
995 static void
996 nvc0_vertex_state_bind(struct pipe_context *pipe, void *hwcso)
997 {
998 struct nvc0_context *nvc0 = nvc0_context(pipe);
999
1000 nvc0->vertex = hwcso;
1001 nvc0->dirty_3d |= NVC0_NEW_3D_VERTEX;
1002 }
1003
1004 static struct pipe_stream_output_target *
1005 nvc0_so_target_create(struct pipe_context *pipe,
1006 struct pipe_resource *res,
1007 unsigned offset, unsigned size)
1008 {
1009 struct nv04_resource *buf = (struct nv04_resource *)res;
1010 struct nvc0_so_target *targ = MALLOC_STRUCT(nvc0_so_target);
1011 if (!targ)
1012 return NULL;
1013
1014 targ->pq = pipe->create_query(pipe, NVC0_HW_QUERY_TFB_BUFFER_OFFSET, 0);
1015 if (!targ->pq) {
1016 FREE(targ);
1017 return NULL;
1018 }
1019 targ->clean = true;
1020
1021 targ->pipe.buffer_size = size;
1022 targ->pipe.buffer_offset = offset;
1023 targ->pipe.context = pipe;
1024 targ->pipe.buffer = NULL;
1025 pipe_resource_reference(&targ->pipe.buffer, res);
1026 pipe_reference_init(&targ->pipe.reference, 1);
1027
1028 assert(buf->base.target == PIPE_BUFFER);
1029 util_range_add(&buf->valid_buffer_range, offset, offset + size);
1030
1031 return &targ->pipe;
1032 }
1033
1034 static void
1035 nvc0_so_target_save_offset(struct pipe_context *pipe,
1036 struct pipe_stream_output_target *ptarg,
1037 unsigned index, bool *serialize)
1038 {
1039 struct nvc0_so_target *targ = nvc0_so_target(ptarg);
1040
1041 if (*serialize) {
1042 *serialize = false;
1043 PUSH_SPACE(nvc0_context(pipe)->base.pushbuf, 1);
1044 IMMED_NVC0(nvc0_context(pipe)->base.pushbuf, NVC0_3D(SERIALIZE), 0);
1045
1046 NOUVEAU_DRV_STAT(nouveau_screen(pipe->screen), gpu_serialize_count, 1);
1047 }
1048
1049 nvc0_query(targ->pq)->index = index;
1050 pipe->end_query(pipe, targ->pq);
1051 }
1052
1053 static void
1054 nvc0_so_target_destroy(struct pipe_context *pipe,
1055 struct pipe_stream_output_target *ptarg)
1056 {
1057 struct nvc0_so_target *targ = nvc0_so_target(ptarg);
1058 pipe->destroy_query(pipe, targ->pq);
1059 pipe_resource_reference(&targ->pipe.buffer, NULL);
1060 FREE(targ);
1061 }
1062
1063 static void
1064 nvc0_set_transform_feedback_targets(struct pipe_context *pipe,
1065 unsigned num_targets,
1066 struct pipe_stream_output_target **targets,
1067 const unsigned *offsets)
1068 {
1069 struct nvc0_context *nvc0 = nvc0_context(pipe);
1070 unsigned i;
1071 bool serialize = true;
1072
1073 assert(num_targets <= 4);
1074
1075 for (i = 0; i < num_targets; ++i) {
1076 const bool changed = nvc0->tfbbuf[i] != targets[i];
1077 const bool append = (offsets[i] == ((unsigned)-1));
1078 if (!changed && append)
1079 continue;
1080 nvc0->tfbbuf_dirty |= 1 << i;
1081
1082 if (nvc0->tfbbuf[i] && changed)
1083 nvc0_so_target_save_offset(pipe, nvc0->tfbbuf[i], i, &serialize);
1084
1085 if (targets[i] && !append)
1086 nvc0_so_target(targets[i])->clean = true;
1087
1088 pipe_so_target_reference(&nvc0->tfbbuf[i], targets[i]);
1089 }
1090 for (; i < nvc0->num_tfbbufs; ++i) {
1091 if (nvc0->tfbbuf[i]) {
1092 nvc0->tfbbuf_dirty |= 1 << i;
1093 nvc0_so_target_save_offset(pipe, nvc0->tfbbuf[i], i, &serialize);
1094 pipe_so_target_reference(&nvc0->tfbbuf[i], NULL);
1095 }
1096 }
1097 nvc0->num_tfbbufs = num_targets;
1098
1099 if (nvc0->tfbbuf_dirty) {
1100 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_TFB);
1101 nvc0->dirty_3d |= NVC0_NEW_3D_TFB_TARGETS;
1102 }
1103 }
1104
1105 static void
1106 nvc0_bind_surfaces_range(struct nvc0_context *nvc0, const unsigned t,
1107 unsigned start, unsigned nr,
1108 struct pipe_surface **psurfaces)
1109 {
1110 const unsigned end = start + nr;
1111 const unsigned mask = ((1 << nr) - 1) << start;
1112 unsigned i;
1113
1114 if (psurfaces) {
1115 for (i = start; i < end; ++i) {
1116 const unsigned p = i - start;
1117 if (psurfaces[p])
1118 nvc0->surfaces_valid[t] |= (1 << i);
1119 else
1120 nvc0->surfaces_valid[t] &= ~(1 << i);
1121 pipe_surface_reference(&nvc0->surfaces[t][i], psurfaces[p]);
1122 }
1123 } else {
1124 for (i = start; i < end; ++i)
1125 pipe_surface_reference(&nvc0->surfaces[t][i], NULL);
1126 nvc0->surfaces_valid[t] &= ~mask;
1127 }
1128 nvc0->surfaces_dirty[t] |= mask;
1129
1130 if (t == 0)
1131 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_SUF);
1132 else
1133 nouveau_bufctx_reset(nvc0->bufctx_cp, NVC0_BIND_CP_SUF);
1134 }
1135
1136 static void
1137 nvc0_set_compute_resources(struct pipe_context *pipe,
1138 unsigned start, unsigned nr,
1139 struct pipe_surface **resources)
1140 {
1141 nvc0_bind_surfaces_range(nvc0_context(pipe), 1, start, nr, resources);
1142
1143 nvc0_context(pipe)->dirty_cp |= NVC0_NEW_CP_SURFACES;
1144 }
1145
1146 static bool
1147 nvc0_bind_images_range(struct nvc0_context *nvc0, const unsigned s,
1148 unsigned start, unsigned nr,
1149 const struct pipe_image_view *pimages)
1150 {
1151 const unsigned end = start + nr;
1152 unsigned mask = 0;
1153 unsigned i;
1154
1155 assert(s < 6);
1156
1157 if (pimages) {
1158 for (i = start; i < end; ++i) {
1159 struct pipe_image_view *img = &nvc0->images[s][i];
1160 const unsigned p = i - start;
1161
1162 if (img->resource == pimages[p].resource &&
1163 img->format == pimages[p].format &&
1164 img->access == pimages[p].access) {
1165 if (img->resource == NULL)
1166 continue;
1167 if (img->resource->target == PIPE_BUFFER &&
1168 img->u.buf.offset == pimages[p].u.buf.offset &&
1169 img->u.buf.size == pimages[p].u.buf.size)
1170 continue;
1171 if (img->resource->target != PIPE_BUFFER &&
1172 img->u.tex.first_layer == pimages[p].u.tex.first_layer &&
1173 img->u.tex.last_layer == pimages[p].u.tex.last_layer &&
1174 img->u.tex.level == pimages[p].u.tex.level)
1175 continue;
1176 }
1177
1178 mask |= (1 << i);
1179 if (pimages[p].resource)
1180 nvc0->images_valid[s] |= (1 << i);
1181 else
1182 nvc0->images_valid[s] &= ~(1 << i);
1183
1184 img->format = pimages[p].format;
1185 img->access = pimages[p].access;
1186 if (pimages[p].resource && pimages[p].resource->target == PIPE_BUFFER)
1187 img->u.buf = pimages[p].u.buf;
1188 else
1189 img->u.tex = pimages[p].u.tex;
1190
1191 pipe_resource_reference(
1192 &img->resource, pimages[p].resource);
1193
1194 if (nvc0->screen->base.class_3d >= GM107_3D_CLASS) {
1195 if (nvc0->images_tic[s][i]) {
1196 struct nv50_tic_entry *old =
1197 nv50_tic_entry(nvc0->images_tic[s][i]);
1198 nvc0_screen_tic_unlock(nvc0->screen, old);
1199 pipe_sampler_view_reference(&nvc0->images_tic[s][i], NULL);
1200 }
1201
1202 nvc0->images_tic[s][i] =
1203 gm107_create_texture_view_from_image(&nvc0->base.pipe,
1204 &pimages[p]);
1205 }
1206 }
1207 if (!mask)
1208 return false;
1209 } else {
1210 mask = ((1 << nr) - 1) << start;
1211 if (!(nvc0->images_valid[s] & mask))
1212 return false;
1213 for (i = start; i < end; ++i) {
1214 pipe_resource_reference(&nvc0->images[s][i].resource, NULL);
1215 if (nvc0->screen->base.class_3d >= GM107_3D_CLASS) {
1216 struct nv50_tic_entry *old = nv50_tic_entry(nvc0->images_tic[s][i]);
1217 if (old) {
1218 nvc0_screen_tic_unlock(nvc0->screen, old);
1219 pipe_sampler_view_reference(&nvc0->images_tic[s][i], NULL);
1220 }
1221 }
1222 }
1223 nvc0->images_valid[s] &= ~mask;
1224 }
1225 nvc0->images_dirty[s] |= mask;
1226
1227 if (s == 5)
1228 nouveau_bufctx_reset(nvc0->bufctx_cp, NVC0_BIND_CP_SUF);
1229 else
1230 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_SUF);
1231
1232 return true;
1233 }
1234
1235 static void
1236 nvc0_set_shader_images(struct pipe_context *pipe,
1237 enum pipe_shader_type shader,
1238 unsigned start, unsigned nr,
1239 const struct pipe_image_view *images)
1240 {
1241 const unsigned s = nvc0_shader_stage(shader);
1242 if (!nvc0_bind_images_range(nvc0_context(pipe), s, start, nr, images))
1243 return;
1244
1245 if (s == 5)
1246 nvc0_context(pipe)->dirty_cp |= NVC0_NEW_CP_SURFACES;
1247 else
1248 nvc0_context(pipe)->dirty_3d |= NVC0_NEW_3D_SURFACES;
1249 }
1250
1251 static bool
1252 nvc0_bind_buffers_range(struct nvc0_context *nvc0, const unsigned t,
1253 unsigned start, unsigned nr,
1254 const struct pipe_shader_buffer *pbuffers)
1255 {
1256 const unsigned end = start + nr;
1257 unsigned mask = 0;
1258 unsigned i;
1259
1260 assert(t < 6);
1261
1262 if (pbuffers) {
1263 for (i = start; i < end; ++i) {
1264 struct pipe_shader_buffer *buf = &nvc0->buffers[t][i];
1265 const unsigned p = i - start;
1266 if (buf->buffer == pbuffers[p].buffer &&
1267 buf->buffer_offset == pbuffers[p].buffer_offset &&
1268 buf->buffer_size == pbuffers[p].buffer_size)
1269 continue;
1270
1271 mask |= (1 << i);
1272 if (pbuffers[p].buffer)
1273 nvc0->buffers_valid[t] |= (1 << i);
1274 else
1275 nvc0->buffers_valid[t] &= ~(1 << i);
1276 buf->buffer_offset = pbuffers[p].buffer_offset;
1277 buf->buffer_size = pbuffers[p].buffer_size;
1278 pipe_resource_reference(&buf->buffer, pbuffers[p].buffer);
1279 }
1280 if (!mask)
1281 return false;
1282 } else {
1283 mask = ((1 << nr) - 1) << start;
1284 if (!(nvc0->buffers_valid[t] & mask))
1285 return false;
1286 for (i = start; i < end; ++i)
1287 pipe_resource_reference(&nvc0->buffers[t][i].buffer, NULL);
1288 nvc0->buffers_valid[t] &= ~mask;
1289 }
1290 nvc0->buffers_dirty[t] |= mask;
1291
1292 if (t == 5)
1293 nouveau_bufctx_reset(nvc0->bufctx_cp, NVC0_BIND_CP_BUF);
1294 else
1295 nouveau_bufctx_reset(nvc0->bufctx_3d, NVC0_BIND_3D_BUF);
1296
1297 return true;
1298 }
1299
1300 static void
1301 nvc0_set_shader_buffers(struct pipe_context *pipe,
1302 enum pipe_shader_type shader,
1303 unsigned start, unsigned nr,
1304 const struct pipe_shader_buffer *buffers)
1305 {
1306 const unsigned s = nvc0_shader_stage(shader);
1307 if (!nvc0_bind_buffers_range(nvc0_context(pipe), s, start, nr, buffers))
1308 return;
1309
1310 if (s == 5)
1311 nvc0_context(pipe)->dirty_cp |= NVC0_NEW_CP_BUFFERS;
1312 else
1313 nvc0_context(pipe)->dirty_3d |= NVC0_NEW_3D_BUFFERS;
1314 }
1315
1316 static inline void
1317 nvc0_set_global_handle(uint32_t *phandle, struct pipe_resource *res)
1318 {
1319 struct nv04_resource *buf = nv04_resource(res);
1320 if (buf) {
1321 uint64_t limit = (buf->address + buf->base.width0) - 1;
1322 if (limit < (1ULL << 32)) {
1323 *phandle = (uint32_t)buf->address;
1324 } else {
1325 NOUVEAU_ERR("Cannot map into TGSI_RESOURCE_GLOBAL: "
1326 "resource not contained within 32-bit address space !\n");
1327 *phandle = 0;
1328 }
1329 } else {
1330 *phandle = 0;
1331 }
1332 }
1333
1334 static void
1335 nvc0_set_global_bindings(struct pipe_context *pipe,
1336 unsigned start, unsigned nr,
1337 struct pipe_resource **resources,
1338 uint32_t **handles)
1339 {
1340 struct nvc0_context *nvc0 = nvc0_context(pipe);
1341 struct pipe_resource **ptr;
1342 unsigned i;
1343 const unsigned end = start + nr;
1344
1345 if (nvc0->global_residents.size <= (end * sizeof(struct pipe_resource *))) {
1346 const unsigned old_size = nvc0->global_residents.size;
1347 const unsigned req_size = end * sizeof(struct pipe_resource *);
1348 util_dynarray_resize(&nvc0->global_residents, req_size);
1349 memset((uint8_t *)nvc0->global_residents.data + old_size, 0,
1350 req_size - old_size);
1351 }
1352
1353 if (resources) {
1354 ptr = util_dynarray_element(
1355 &nvc0->global_residents, struct pipe_resource *, start);
1356 for (i = 0; i < nr; ++i) {
1357 pipe_resource_reference(&ptr[i], resources[i]);
1358 nvc0_set_global_handle(handles[i], resources[i]);
1359 }
1360 } else {
1361 ptr = util_dynarray_element(
1362 &nvc0->global_residents, struct pipe_resource *, start);
1363 for (i = 0; i < nr; ++i)
1364 pipe_resource_reference(&ptr[i], NULL);
1365 }
1366
1367 nouveau_bufctx_reset(nvc0->bufctx_cp, NVC0_BIND_CP_GLOBAL);
1368
1369 nvc0->dirty_cp |= NVC0_NEW_CP_GLOBALS;
1370 }
1371
1372 void
1373 nvc0_init_state_functions(struct nvc0_context *nvc0)
1374 {
1375 struct pipe_context *pipe = &nvc0->base.pipe;
1376
1377 pipe->create_blend_state = nvc0_blend_state_create;
1378 pipe->bind_blend_state = nvc0_blend_state_bind;
1379 pipe->delete_blend_state = nvc0_blend_state_delete;
1380
1381 pipe->create_rasterizer_state = nvc0_rasterizer_state_create;
1382 pipe->bind_rasterizer_state = nvc0_rasterizer_state_bind;
1383 pipe->delete_rasterizer_state = nvc0_rasterizer_state_delete;
1384
1385 pipe->create_depth_stencil_alpha_state = nvc0_zsa_state_create;
1386 pipe->bind_depth_stencil_alpha_state = nvc0_zsa_state_bind;
1387 pipe->delete_depth_stencil_alpha_state = nvc0_zsa_state_delete;
1388
1389 pipe->create_sampler_state = nv50_sampler_state_create;
1390 pipe->delete_sampler_state = nvc0_sampler_state_delete;
1391 pipe->bind_sampler_states = nvc0_bind_sampler_states;
1392
1393 pipe->create_sampler_view = nvc0_create_sampler_view;
1394 pipe->sampler_view_destroy = nvc0_sampler_view_destroy;
1395 pipe->set_sampler_views = nvc0_set_sampler_views;
1396
1397 pipe->create_vs_state = nvc0_vp_state_create;
1398 pipe->create_fs_state = nvc0_fp_state_create;
1399 pipe->create_gs_state = nvc0_gp_state_create;
1400 pipe->create_tcs_state = nvc0_tcp_state_create;
1401 pipe->create_tes_state = nvc0_tep_state_create;
1402 pipe->bind_vs_state = nvc0_vp_state_bind;
1403 pipe->bind_fs_state = nvc0_fp_state_bind;
1404 pipe->bind_gs_state = nvc0_gp_state_bind;
1405 pipe->bind_tcs_state = nvc0_tcp_state_bind;
1406 pipe->bind_tes_state = nvc0_tep_state_bind;
1407 pipe->delete_vs_state = nvc0_sp_state_delete;
1408 pipe->delete_fs_state = nvc0_sp_state_delete;
1409 pipe->delete_gs_state = nvc0_sp_state_delete;
1410 pipe->delete_tcs_state = nvc0_sp_state_delete;
1411 pipe->delete_tes_state = nvc0_sp_state_delete;
1412
1413 pipe->create_compute_state = nvc0_cp_state_create;
1414 pipe->bind_compute_state = nvc0_cp_state_bind;
1415 pipe->delete_compute_state = nvc0_sp_state_delete;
1416
1417 pipe->set_blend_color = nvc0_set_blend_color;
1418 pipe->set_stencil_ref = nvc0_set_stencil_ref;
1419 pipe->set_clip_state = nvc0_set_clip_state;
1420 pipe->set_sample_mask = nvc0_set_sample_mask;
1421 pipe->set_min_samples = nvc0_set_min_samples;
1422 pipe->set_constant_buffer = nvc0_set_constant_buffer;
1423 pipe->set_framebuffer_state = nvc0_set_framebuffer_state;
1424 pipe->set_sample_locations = nvc0_set_sample_locations;
1425 pipe->set_polygon_stipple = nvc0_set_polygon_stipple;
1426 pipe->set_scissor_states = nvc0_set_scissor_states;
1427 pipe->set_viewport_states = nvc0_set_viewport_states;
1428 pipe->set_window_rectangles = nvc0_set_window_rectangles;
1429 pipe->set_tess_state = nvc0_set_tess_state;
1430
1431 pipe->create_vertex_elements_state = nvc0_vertex_state_create;
1432 pipe->delete_vertex_elements_state = nvc0_vertex_state_delete;
1433 pipe->bind_vertex_elements_state = nvc0_vertex_state_bind;
1434
1435 pipe->set_vertex_buffers = nvc0_set_vertex_buffers;
1436
1437 pipe->create_stream_output_target = nvc0_so_target_create;
1438 pipe->stream_output_target_destroy = nvc0_so_target_destroy;
1439 pipe->set_stream_output_targets = nvc0_set_transform_feedback_targets;
1440
1441 pipe->set_global_binding = nvc0_set_global_bindings;
1442 pipe->set_compute_resources = nvc0_set_compute_resources;
1443 pipe->set_shader_images = nvc0_set_shader_images;
1444 pipe->set_shader_buffers = nvc0_set_shader_buffers;
1445
1446 nvc0->sample_mask = ~0;
1447 nvc0->min_samples = 1;
1448 nvc0->default_tess_outer[0] =
1449 nvc0->default_tess_outer[1] =
1450 nvc0->default_tess_outer[2] =
1451 nvc0->default_tess_outer[3] = 1.0;
1452 nvc0->default_tess_inner[0] =
1453 nvc0->default_tess_inner[1] = 1.0;
1454 }