Debian Codegraph @

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

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