Debian Codegraph @

src/gallium/drivers/nouveau/nv50/nv50_surface.c

raw
1 /*
2 * Copyright 2008 Ben Skeggs
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 <stdint.h>
24
25 #include "compiler/nir/nir.h"
26 #include "compiler/nir/nir_builder.h"
27
28 #include "nir/pipe_nir.h"
29 #include "pipe/p_defines.h"
30 #include "pipe/p_shader_tokens.h"
31
32 #include "util/u_inlines.h"
33 #include "util/u_pack_color.h"
34 #include "util/format/u_format.h"
35 #include "util/u_math.h"
36 #include "util/u_surface.h"
37 #include "util/u_thread.h"
38
39 #include "nv50_ir_driver.h"
40
41 #include "nv50/nv50_context.h"
42 #include "nv50/nv50_resource.h"
43
44 #include "nv50/g80_defs.xml.h"
45 #include "nv50/g80_texture.xml.h"
46
47 /* these are used in nv50_blit.h */
48 #define NV50_ENG2D_SUPPORTED_FORMATS 0xff0843e080608409ULL
49 #define NV50_ENG2D_NOCONVERT_FORMATS 0x0008402000000000ULL
50 #define NV50_ENG2D_LUMINANCE_FORMATS 0x0008402000000000ULL
51 #define NV50_ENG2D_INTENSITY_FORMATS 0x0000000000000000ULL
52 #define NV50_ENG2D_OPERATION_FORMATS 0x060001c000608000ULL
53
54 #define NOUVEAU_DRIVER 0x50
55 #include "nv50/nv50_blit.h"
56
57 static inline uint8_t
58 nv50_2d_format(enum pipe_format format, bool dst, bool dst_src_equal)
59 {
60 uint8_t id = nv50_format_table[format].rt;
61
62 /* Hardware values for color formats range from 0xc0 to 0xff,
63 * but the 2D engine doesn't support all of them.
64 */
65 if ((id >= 0xc0) && (NV50_ENG2D_SUPPORTED_FORMATS & (1ULL << (id - 0xc0))))
66 return id;
67 assert(dst_src_equal);
68
69 switch (util_format_get_blocksize(format)) {
70 case 1:
71 return G80_SURFACE_FORMAT_R8_UNORM;
72 case 2:
73 return G80_SURFACE_FORMAT_R16_UNORM;
74 case 4:
75 return G80_SURFACE_FORMAT_BGRA8_UNORM;
76 case 8:
77 return G80_SURFACE_FORMAT_RGBA16_FLOAT;
78 case 16:
79 return G80_SURFACE_FORMAT_RGBA32_FLOAT;
80 default:
81 return 0;
82 }
83 }
84
85 static int
86 nv50_2d_texture_set(struct nouveau_pushbuf *push, int dst,
87 struct nv50_miptree *mt, unsigned level, unsigned layer,
88 enum pipe_format pformat, bool dst_src_pformat_equal)
89 {
90 struct nouveau_bo *bo = mt->base.bo;
91 uint32_t width, height, depth;
92 uint32_t format;
93 uint32_t mthd = dst ? NV50_2D_DST_FORMAT : NV50_2D_SRC_FORMAT;
94 uint32_t offset = mt->level[level].offset;
95
96 format = nv50_2d_format(pformat, dst, dst_src_pformat_equal);
97 if (!format) {
98 NOUVEAU_ERR("invalid/unsupported surface format: %s\n",
99 util_format_name(pformat));
100 return 1;
101 }
102
103 width = u_minify(mt->base.base.width0, level) << mt->ms_x;
104 height = u_minify(mt->base.base.height0, level) << mt->ms_y;
105 depth = u_minify(mt->base.base.depth0, level);
106
107 offset = mt->level[level].offset;
108 if (!mt->layout_3d) {
109 offset += mt->layer_stride * layer;
110 depth = 1;
111 layer = 0;
112 } else
113 if (!dst) {
114 offset += nv50_mt_zslice_offset(mt, level, layer);
115 layer = 0;
116 }
117
118 if (!nouveau_bo_memtype(bo)) {
119 BEGIN_NV04(push, SUBC_2D(mthd), 2);
120 PUSH_DATA (push, format);
121 PUSH_DATA (push, 1);
122 BEGIN_NV04(push, SUBC_2D(mthd + 0x14), 5);
123 PUSH_DATA (push, mt->level[level].pitch);
124 PUSH_DATA (push, width);
125 PUSH_DATA (push, height);
126 PUSH_DATAh(push, mt->base.address + offset);
127 PUSH_DATA (push, mt->base.address + offset);
128 } else {
129 BEGIN_NV04(push, SUBC_2D(mthd), 5);
130 PUSH_DATA (push, format);
131 PUSH_DATA (push, 0);
132 PUSH_DATA (push, mt->level[level].tile_mode);
133 PUSH_DATA (push, depth);
134 PUSH_DATA (push, layer);
135 BEGIN_NV04(push, SUBC_2D(mthd + 0x18), 4);
136 PUSH_DATA (push, width);
137 PUSH_DATA (push, height);
138 PUSH_DATAh(push, mt->base.address + offset);
139 PUSH_DATA (push, mt->base.address + offset);
140 }
141
142 #if 0
143 if (dst) {
144 BEGIN_NV04(push, SUBC_2D(NV50_2D_CLIP_X), 4);
145 PUSH_DATA (push, 0);
146 PUSH_DATA (push, 0);
147 PUSH_DATA (push, width);
148 PUSH_DATA (push, height);
149 }
150 #endif
151 return 0;
152 }
153
154 static int
155 nv50_2d_texture_do_copy(struct nouveau_pushbuf *push,
156 struct nv50_miptree *dst, unsigned dst_level,
157 unsigned dx, unsigned dy, unsigned dz,
158 struct nv50_miptree *src, unsigned src_level,
159 unsigned sx, unsigned sy, unsigned sz,
160 unsigned w, unsigned h)
161 {
162 const enum pipe_format dfmt = dst->base.base.format;
163 const enum pipe_format sfmt = src->base.base.format;
164 int ret;
165 bool eqfmt = dfmt == sfmt;
166
167 if (!PUSH_SPACE(push, 2 * 16 + 32))
168 return PIPE_ERROR;
169
170 ret = nv50_2d_texture_set(push, 1, dst, dst_level, dz, dfmt, eqfmt);
171 if (ret)
172 return ret;
173
174 ret = nv50_2d_texture_set(push, 0, src, src_level, sz, sfmt, eqfmt);
175 if (ret)
176 return ret;
177
178 BEGIN_NV04(push, NV50_2D(BLIT_CONTROL), 1);
179 PUSH_DATA (push, NV50_2D_BLIT_CONTROL_FILTER_POINT_SAMPLE);
180 BEGIN_NV04(push, NV50_2D(BLIT_DST_X), 4);
181 PUSH_DATA (push, dx << dst->ms_x);
182 PUSH_DATA (push, dy << dst->ms_y);
183 PUSH_DATA (push, w << dst->ms_x);
184 PUSH_DATA (push, h << dst->ms_y);
185 BEGIN_NV04(push, NV50_2D(BLIT_DU_DX_FRACT), 4);
186 PUSH_DATA (push, 0);
187 PUSH_DATA (push, 1);
188 PUSH_DATA (push, 0);
189 PUSH_DATA (push, 1);
190 BEGIN_NV04(push, NV50_2D(BLIT_SRC_X_FRACT), 4);
191 PUSH_DATA (push, 0);
192 PUSH_DATA (push, sx << src->ms_x);
193 PUSH_DATA (push, 0);
194 PUSH_DATA (push, sy << src->ms_y);
195
196 return 0;
197 }
198
199 static void
200 nv50_resource_copy_region(struct pipe_context *pipe,
201 struct pipe_resource *dst, unsigned dst_level,
202 unsigned dstx, unsigned dsty, unsigned dstz,
203 struct pipe_resource *src, unsigned src_level,
204 const struct pipe_box *src_box)
205 {
206 struct nv50_context *nv50 = nv50_context(pipe);
207 int ret;
208 bool m2mf;
209 unsigned dst_layer = dstz, src_layer = src_box->z;
210
211 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) {
212 nouveau_copy_buffer(&nv50->base,
213 nv04_resource(dst), dstx,
214 nv04_resource(src), src_box->x, src_box->width);
215 return;
216 }
217
218 /* 0 and 1 are equal, only supporting 0/1, 2, 4 and 8 */
219 assert((src->nr_samples | 1) == (dst->nr_samples | 1));
220
221 m2mf = (src->format == dst->format) ||
222 (util_format_get_blocksizebits(src->format) ==
223 util_format_get_blocksizebits(dst->format));
224
225 nv04_resource(dst)->status |= NOUVEAU_BUFFER_STATUS_GPU_WRITING;
226
227 if (m2mf) {
228 struct nv50_miptree *src_mt = nv50_miptree(src);
229 struct nv50_miptree *dst_mt = nv50_miptree(dst);
230 struct nv50_m2mf_rect drect, srect;
231 unsigned i;
232 unsigned nx = util_format_get_nblocksx(src->format, src_box->width)
233 << src_mt->ms_x;
234 unsigned ny = util_format_get_nblocksy(src->format, src_box->height)
235 << src_mt->ms_y;
236
237 nv50_m2mf_rect_setup(&drect, dst, dst_level, dstx, dsty, dstz);
238 nv50_m2mf_rect_setup(&srect, src, src_level,
239 src_box->x, src_box->y, src_box->z);
240
241 for (i = 0; i < src_box->depth; ++i) {
242 nv50_m2mf_transfer_rect(nv50, &drect, &srect, nx, ny);
243
244 if (dst_mt->layout_3d)
245 drect.z++;
246 else
247 drect.base += dst_mt->layer_stride;
248
249 if (src_mt->layout_3d)
250 srect.z++;
251 else
252 srect.base += src_mt->layer_stride;
253 }
254 return;
255 }
256
257 assert((src->format == dst->format) ||
258 (nv50_2d_src_format_faithful(src->format) &&
259 nv50_2d_dst_format_faithful(dst->format)));
260
261 BCTX_REFN(nv50->bufctx, 2D, nv04_resource(src), RD);
262 BCTX_REFN(nv50->bufctx, 2D, nv04_resource(dst), WR);
263 nouveau_pushbuf_bufctx(nv50->base.pushbuf, nv50->bufctx);
264 PUSH_VAL(nv50->base.pushbuf);
265
266 for (; dst_layer < dstz + src_box->depth; ++dst_layer, ++src_layer) {
267 ret = nv50_2d_texture_do_copy(nv50->base.pushbuf,
268 nv50_miptree(dst), dst_level,
269 dstx, dsty, dst_layer,
270 nv50_miptree(src), src_level,
271 src_box->x, src_box->y, src_layer,
272 src_box->width, src_box->height);
273 if (ret)
274 break;
275 }
276 nouveau_bufctx_reset(nv50->bufctx, NV50_BIND_2D);
277 }
278
279 static void
280 nv50_clear_render_target(struct pipe_context *pipe,
281 struct pipe_surface *dst,
282 const union pipe_color_union *color,
283 unsigned dstx, unsigned dsty,
284 unsigned width, unsigned height,
285 bool render_condition_enabled)
286 {
287 struct nv50_context *nv50 = nv50_context(pipe);
288 struct nouveau_pushbuf *push = nv50->base.pushbuf;
289 struct nv50_miptree *mt = nv50_miptree(dst->texture);
290 struct nv50_surface *sf = nv50_surface(dst);
291 struct nouveau_bo *bo = mt->base.bo;
292 unsigned z;
293
294 assert(dst->texture->target != PIPE_BUFFER);
295
296 BEGIN_NV04(push, NV50_3D(CLEAR_COLOR(0)), 4);
297 PUSH_DATAf(push, color->f[0]);
298 PUSH_DATAf(push, color->f[1]);
299 PUSH_DATAf(push, color->f[2]);
300 PUSH_DATAf(push, color->f[3]);
301
302 if (!PUSH_SPACE_EX(push, 64 + sf->depth, 1, 0))
303 return;
304
305 PUSH_REF1(push, bo, mt->base.domain | NOUVEAU_BO_WR);
306
307 BEGIN_NV04(push, NV50_3D(SCREEN_SCISSOR_HORIZ), 2);
308 PUSH_DATA (push, ( width << 16) | dstx);
309 PUSH_DATA (push, (height << 16) | dsty);
310 BEGIN_NV04(push, NV50_3D(SCISSOR_HORIZ(0)), 2);
311 PUSH_DATA (push, 8192 << 16);
312 PUSH_DATA (push, 8192 << 16);
313 nv50->scissors_dirty |= 1;
314
315 BEGIN_NV04(push, NV50_3D(RT_CONTROL), 1);
316 PUSH_DATA (push, 1);
317 BEGIN_NV04(push, NV50_3D(RT_ADDRESS_HIGH(0)), 5);
318 PUSH_DATAh(push, mt->base.address + sf->offset);
319 PUSH_DATA (push, mt->base.address + sf->offset);
320 PUSH_DATA (push, nv50_format_table[dst->format].rt);
321 PUSH_DATA (push, mt->level[sf->base.level].tile_mode);
322 PUSH_DATA (push, mt->layer_stride >> 2);
323 BEGIN_NV04(push, NV50_3D(RT_HORIZ(0)), 2);
324 if (nouveau_bo_memtype(bo))
325 PUSH_DATA(push, sf->width);
326 else
327 PUSH_DATA(push, NV50_3D_RT_HORIZ_LINEAR | mt->level[0].pitch);
328 PUSH_DATA (push, sf->height);
329 BEGIN_NV04(push, NV50_3D(RT_ARRAY_MODE), 1);
330 if (mt->layout_3d)
331 PUSH_DATA(push, NV50_3D_RT_ARRAY_MODE_MODE_3D | 512);
332 else
333 PUSH_DATA(push, 512);
334
335 BEGIN_NV04(push, NV50_3D(MULTISAMPLE_MODE), 1);
336 PUSH_DATA (push, mt->ms_mode);
337
338 if (!nouveau_bo_memtype(bo)) {
339 BEGIN_NV04(push, NV50_3D(ZETA_ENABLE), 1);
340 PUSH_DATA (push, 0);
341 }
342
343 /* NOTE: only works with D3D clear flag (5097/0x143c bit 4) */
344
345 BEGIN_NV04(push, NV50_3D(VIEWPORT_HORIZ(0)), 2);
346 PUSH_DATA (push, (width << 16) | dstx);
347 PUSH_DATA (push, (height << 16) | dsty);
348
349 if (!render_condition_enabled) {
350 BEGIN_NV04(push, NV50_3D(COND_MODE), 1);
351 PUSH_DATA (push, NV50_3D_COND_MODE_ALWAYS);
352 }
353
354 BEGIN_NI04(push, NV50_3D(CLEAR_BUFFERS), sf->depth);
355 for (z = 0; z < sf->depth; ++z) {
356 PUSH_DATA (push, 0x3c |
357 (z << NV50_3D_CLEAR_BUFFERS_LAYER__SHIFT));
358 }
359
360 if (!render_condition_enabled) {
361 BEGIN_NV04(push, NV50_3D(COND_MODE), 1);
362 PUSH_DATA (push, nv50->cond_condmode);
363 }
364
365 nv50->dirty_3d |= NV50_NEW_3D_FRAMEBUFFER | NV50_NEW_3D_SCISSOR;
366 }
367
368 static void
369 nv50_clear_depth_stencil(struct pipe_context *pipe,
370 struct pipe_surface *dst,
371 unsigned clear_flags,
372 double depth,
373 unsigned stencil,
374 unsigned dstx, unsigned dsty,
375 unsigned width, unsigned height,
376 bool render_condition_enabled)
377 {
378 struct nv50_context *nv50 = nv50_context(pipe);
379 struct nouveau_pushbuf *push = nv50->base.pushbuf;
380 struct nv50_miptree *mt = nv50_miptree(dst->texture);
381 struct nv50_surface *sf = nv50_surface(dst);
382 struct nouveau_bo *bo = mt->base.bo;
383 uint32_t mode = 0;
384 unsigned z;
385
386 assert(dst->texture->target != PIPE_BUFFER);
387 assert(nouveau_bo_memtype(bo)); /* ZETA cannot be linear */
388
389 if (clear_flags & PIPE_CLEAR_DEPTH) {
390 BEGIN_NV04(push, NV50_3D(CLEAR_DEPTH), 1);
391 PUSH_DATAf(push, depth);
392 mode |= NV50_3D_CLEAR_BUFFERS_Z;
393 }
394
395 if (clear_flags & PIPE_CLEAR_STENCIL) {
396 BEGIN_NV04(push, NV50_3D(CLEAR_STENCIL), 1);
397 PUSH_DATA (push, stencil & 0xff);
398 mode |= NV50_3D_CLEAR_BUFFERS_S;
399 }
400
401 if (!PUSH_SPACE_EX(push, 64 + sf->depth, 1, 0))
402 return;
403
404 PUSH_REF1(push, bo, mt->base.domain | NOUVEAU_BO_WR);
405
406 BEGIN_NV04(push, NV50_3D(SCREEN_SCISSOR_HORIZ), 2);
407 PUSH_DATA (push, ( width << 16) | dstx);
408 PUSH_DATA (push, (height << 16) | dsty);
409 BEGIN_NV04(push, NV50_3D(SCISSOR_HORIZ(0)), 2);
410 PUSH_DATA (push, 8192 << 16);
411 PUSH_DATA (push, 8192 << 16);
412 nv50->scissors_dirty |= 1;
413
414 BEGIN_NV04(push, NV50_3D(ZETA_ADDRESS_HIGH), 5);
415 PUSH_DATAh(push, mt->base.address + sf->offset);
416 PUSH_DATA (push, mt->base.address + sf->offset);
417 PUSH_DATA (push, nv50_format_table[dst->format].rt);
418 PUSH_DATA (push, mt->level[sf->base.level].tile_mode);
419 PUSH_DATA (push, mt->layer_stride >> 2);
420 BEGIN_NV04(push, NV50_3D(ZETA_ENABLE), 1);
421 PUSH_DATA (push, 1);
422 BEGIN_NV04(push, NV50_3D(ZETA_HORIZ), 3);
423 PUSH_DATA (push, sf->width);
424 PUSH_DATA (push, sf->height);
425 PUSH_DATA (push, (1 << 16) | 1);
426
427 BEGIN_NV04(push, NV50_3D(RT_ARRAY_MODE), 1);
428 PUSH_DATA (push, 512);
429
430 BEGIN_NV04(push, NV50_3D(MULTISAMPLE_MODE), 1);
431 PUSH_DATA (push, mt->ms_mode);
432
433 BEGIN_NV04(push, NV50_3D(VIEWPORT_HORIZ(0)), 2);
434 PUSH_DATA (push, (width << 16) | dstx);
435 PUSH_DATA (push, (height << 16) | dsty);
436
437 if (!render_condition_enabled) {
438 BEGIN_NV04(push, NV50_3D(COND_MODE), 1);
439 PUSH_DATA (push, NV50_3D_COND_MODE_ALWAYS);
440 }
441
442 BEGIN_NI04(push, NV50_3D(CLEAR_BUFFERS), sf->depth);
443 for (z = 0; z < sf->depth; ++z) {
444 PUSH_DATA (push, mode |
445 (z << NV50_3D_CLEAR_BUFFERS_LAYER__SHIFT));
446 }
447
448 if (!render_condition_enabled) {
449 BEGIN_NV04(push, NV50_3D(COND_MODE), 1);
450 PUSH_DATA (push, nv50->cond_condmode);
451 }
452
453 nv50->dirty_3d |= NV50_NEW_3D_FRAMEBUFFER | NV50_NEW_3D_SCISSOR;
454 }
455
456 void
457 nv50_clear(struct pipe_context *pipe, unsigned buffers, const struct pipe_scissor_state *scissor_state,
458 const union pipe_color_union *color,
459 double depth, unsigned stencil)
460 {
461 struct nv50_context *nv50 = nv50_context(pipe);
462 struct nouveau_pushbuf *push = nv50->base.pushbuf;
463 struct pipe_framebuffer_state *fb = &nv50->framebuffer;
464 unsigned i, j, k;
465 uint32_t mode = 0;
466
467 simple_mtx_lock(&nv50->screen->state_lock);
468
469 /* don't need NEW_BLEND, COLOR_MASK doesn't affect CLEAR_BUFFERS */
470 if (!nv50_state_validate_3d(nv50, NV50_NEW_3D_FRAMEBUFFER))
471 goto out;
472
473 if (scissor_state) {
474 uint32_t minx = scissor_state->minx;
475 uint32_t maxx = MIN2(fb->width, scissor_state->maxx);
476 uint32_t miny = scissor_state->miny;
477 uint32_t maxy = MIN2(fb->height, scissor_state->maxy);
478 if (maxx <= minx || maxy <= miny)
479 goto out;
480
481 BEGIN_NV04(push, NV50_3D(SCREEN_SCISSOR_HORIZ), 2);
482 PUSH_DATA (push, minx | (maxx - minx) << 16);
483 PUSH_DATA (push, miny | (maxy - miny) << 16);
484 }
485
486 /* We have to clear ALL of the layers, not up to the min number of layers
487 * of any attachment. */
488 BEGIN_NV04(push, NV50_3D(RT_ARRAY_MODE), 1);
489 PUSH_DATA (push, (nv50->rt_array_mode & NV50_3D_RT_ARRAY_MODE_MODE_3D) | 512);
490
491 if (buffers & PIPE_CLEAR_COLOR && fb->nr_cbufs) {
492 BEGIN_NV04(push, NV50_3D(CLEAR_COLOR(0)), 4);
493 PUSH_DATAf(push, color->f[0]);
494 PUSH_DATAf(push, color->f[1]);
495 PUSH_DATAf(push, color->f[2]);
496 PUSH_DATAf(push, color->f[3]);
497 if (buffers & PIPE_CLEAR_COLOR0)
498 mode =
499 NV50_3D_CLEAR_BUFFERS_R | NV50_3D_CLEAR_BUFFERS_G |
500 NV50_3D_CLEAR_BUFFERS_B | NV50_3D_CLEAR_BUFFERS_A;
501 }
502
503 if (buffers & PIPE_CLEAR_DEPTH) {
504 BEGIN_NV04(push, NV50_3D(CLEAR_DEPTH), 1);
505 PUSH_DATA (push, fui(depth));
506 mode |= NV50_3D_CLEAR_BUFFERS_Z;
507 }
508
509 if (buffers & PIPE_CLEAR_STENCIL) {
510 BEGIN_NV04(push, NV50_3D(CLEAR_STENCIL), 1);
511 PUSH_DATA (push, stencil & 0xff);
512 mode |= NV50_3D_CLEAR_BUFFERS_S;
513 }
514
515 if (mode) {
516 int zs_layers = 0, color0_layers = 0;
517 if (nv50->fb_cbufs[0] && (mode & 0x3c))
518 color0_layers = nv50_surface(nv50->fb_cbufs[0])->depth;
519 if (nv50->fb_zsbuf && (mode & ~0x3c))
520 zs_layers = nv50_surface(nv50->fb_zsbuf)->depth;
521
522 for (j = 0; j < MIN2(zs_layers, color0_layers); j++) {
523 BEGIN_NV04(push, NV50_3D(CLEAR_BUFFERS), 1);
524 PUSH_DATA(push, mode | (j << NV50_3D_CLEAR_BUFFERS_LAYER__SHIFT));
525 }
526 for (k = j; k < zs_layers; k++) {
527 BEGIN_NV04(push, NV50_3D(CLEAR_BUFFERS), 1);
528 PUSH_DATA(push, (mode & ~0x3c) | (k << NV50_3D_CLEAR_BUFFERS_LAYER__SHIFT));
529 }
530 for (k = j; k < color0_layers; k++) {
531 BEGIN_NV04(push, NV50_3D(CLEAR_BUFFERS), 1);
532 PUSH_DATA(push, (mode & 0x3c) | (k << NV50_3D_CLEAR_BUFFERS_LAYER__SHIFT));
533 }
534 }
535
536 for (i = 1; i < fb->nr_cbufs; i++) {
537 struct pipe_surface *sf = nv50->fb_cbufs[i];
538 if (!sf || !(buffers & (PIPE_CLEAR_COLOR0 << i)))
539 continue;
540 for (j = 0; j < nv50_surface(sf)->depth; j++) {
541 BEGIN_NV04(push, NV50_3D(CLEAR_BUFFERS), 1);
542 PUSH_DATA (push, (i << 6) | 0x3c |
543 (j << NV50_3D_CLEAR_BUFFERS_LAYER__SHIFT));
544 }
545 }
546
547 /* restore the array mode */
548 BEGIN_NV04(push, NV50_3D(RT_ARRAY_MODE), 1);
549 PUSH_DATA (push, nv50->rt_array_mode);
550
551 /* restore screen scissor */
552 if (scissor_state) {
553 BEGIN_NV04(push, NV50_3D(SCREEN_SCISSOR_HORIZ), 2);
554 PUSH_DATA (push, fb->width << 16);
555 PUSH_DATA (push, fb->height << 16);
556 }
557
558 out:
559 PUSH_KICK(push);
560 simple_mtx_unlock(&nv50->screen->state_lock);
561 }
562
563 static void
564 nv50_clear_buffer_push(struct pipe_context *pipe,
565 struct pipe_resource *res,
566 unsigned offset, unsigned size,
567 const void *data, int data_size)
568 {
569 struct nv50_context *nv50 = nv50_context(pipe);
570 struct nouveau_pushbuf *push = nv50->base.pushbuf;
571 struct nv04_resource *buf = nv04_resource(res);
572 unsigned count = (size + 3) / 4;
573 unsigned xcoord = offset & 0xff;
574 unsigned tmp, i;
575
576 if (data_size == 1) {
577 tmp = *(unsigned char *)data;
578 tmp = (tmp << 24) | (tmp << 16) | (tmp << 8) | tmp;
579 data = &tmp;
580 data_size = 4;
581 } else if (data_size == 2) {
582 tmp = *(unsigned short *)data;
583 tmp = (tmp << 16) | tmp;
584 data = &tmp;
585 data_size = 4;
586 }
587
588 unsigned data_words = data_size / 4;
589
590 nouveau_bufctx_refn(nv50->bufctx, 0, buf->bo, buf->domain | NOUVEAU_BO_WR);
591 nouveau_pushbuf_bufctx(push, nv50->bufctx);
592 PUSH_VAL(push);
593
594 offset &= ~0xff;
595
596 BEGIN_NV04(push, NV50_2D(DST_FORMAT), 2);
597 PUSH_DATA (push, G80_SURFACE_FORMAT_R8_UNORM);
598 PUSH_DATA (push, 1);
599 BEGIN_NV04(push, NV50_2D(DST_PITCH), 5);
600 PUSH_DATA (push, 262144);
601 PUSH_DATA (push, 65536);
602 PUSH_DATA (push, 1);
603 PUSH_DATAh(push, buf->address + offset);
604 PUSH_DATA (push, buf->address + offset);
605 BEGIN_NV04(push, NV50_2D(SIFC_BITMAP_ENABLE), 2);
606 PUSH_DATA (push, 0);
607 PUSH_DATA (push, G80_SURFACE_FORMAT_R8_UNORM);
608 BEGIN_NV04(push, NV50_2D(SIFC_WIDTH), 10);
609 PUSH_DATA (push, size);
610 PUSH_DATA (push, 1);
611 PUSH_DATA (push, 0);
612 PUSH_DATA (push, 1);
613 PUSH_DATA (push, 0);
614 PUSH_DATA (push, 1);
615 PUSH_DATA (push, 0);
616 PUSH_DATA (push, xcoord);
617 PUSH_DATA (push, 0);
618 PUSH_DATA (push, 0);
619
620 while (count) {
621 unsigned nr_data = MIN2(count, NV04_PFIFO_MAX_PACKET_LEN) / data_words;
622 unsigned nr = nr_data * data_words;
623
624 BEGIN_NI04(push, NV50_2D(SIFC_DATA), nr);
625 for (i = 0; i < nr_data; i++)
626 PUSH_DATAp(push, data, data_words);
627
628 count -= nr;
629 }
630
631 nv50_resource_validate(nv50, buf, NOUVEAU_BO_WR);
632
633 nouveau_bufctx_reset(nv50->bufctx, 0);
634 }
635
636 static void
637 nv50_clear_buffer(struct pipe_context *pipe,
638 struct pipe_resource *res,
639 unsigned offset, unsigned size,
640 const void *data, int data_size)
641 {
642 struct nv50_context *nv50 = nv50_context(pipe);
643 struct nouveau_pushbuf *push = nv50->base.pushbuf;
644 struct nv04_resource *buf = (struct nv04_resource *)res;
645 union pipe_color_union color;
646 enum pipe_format dst_fmt;
647 unsigned width, height, elements;
648
649 assert(res->target == PIPE_BUFFER);
650 assert(nouveau_bo_memtype(buf->bo) == 0);
651
652 switch (data_size) {
653 case 16:
654 dst_fmt = PIPE_FORMAT_R32G32B32A32_UINT;
655 memcpy(&color.ui, data, 16);
656 break;
657 case 8:
658 dst_fmt = PIPE_FORMAT_R32G32_UINT;
659 memcpy(&color.ui, data, 8);
660 memset(&color.ui[2], 0, 8);
661 break;
662 case 4:
663 dst_fmt = PIPE_FORMAT_R32_UINT;
664 memcpy(&color.ui, data, 4);
665 memset(&color.ui[1], 0, 12);
666 break;
667 case 2:
668 dst_fmt = PIPE_FORMAT_R16_UINT;
669 color.ui[0] = util_cpu_to_le32(
670 util_le16_to_cpu(*(unsigned short *)data));
671 memset(&color.ui[1], 0, 12);
672 break;
673 case 1:
674 dst_fmt = PIPE_FORMAT_R8_UINT;
675 color.ui[0] = util_cpu_to_le32(*(unsigned char *)data);
676 memset(&color.ui[1], 0, 12);
677 break;
678 default:
679 assert(!"Unsupported element size");
680 return;
681 }
682
683 util_range_add(&buf->base, &buf->valid_buffer_range, offset, offset + size);
684
685 assert(size % data_size == 0);
686
687 if (offset & 0xff) {
688 unsigned fixup_size = MIN2(size, align(offset, 0x100) - offset);
689 assert(fixup_size % data_size == 0);
690 nv50_clear_buffer_push(pipe, res, offset, fixup_size, data, data_size);
691 offset += fixup_size;
692 size -= fixup_size;
693 if (!size)
694 return;
695 }
696
697 elements = size / data_size;
698 height = (elements + 8191) / 8192;
699 width = elements / height;
700 if (height > 1)
701 width &= ~0xff;
702 assert(width > 0);
703
704 BEGIN_NV04(push, NV50_3D(CLEAR_COLOR(0)), 4);
705 PUSH_DATA (push, color.ui[0]);
706 PUSH_DATA (push, color.ui[1]);
707 PUSH_DATA (push, color.ui[2]);
708 PUSH_DATA (push, color.ui[3]);
709
710 if (!PUSH_SPACE_EX(push, 64, 1, 0))
711 return;
712
713 PUSH_REF1(push, buf->bo, buf->domain | NOUVEAU_BO_WR);
714
715 BEGIN_NV04(push, NV50_3D(SCREEN_SCISSOR_HORIZ), 2);
716 PUSH_DATA (push, width << 16);
717 PUSH_DATA (push, height << 16);
718 BEGIN_NV04(push, NV50_3D(SCISSOR_HORIZ(0)), 2);
719 PUSH_DATA (push, 8192 << 16);
720 PUSH_DATA (push, 8192 << 16);
721 nv50->scissors_dirty |= 1;
722
723 BEGIN_NV04(push, NV50_3D(RT_CONTROL), 1);
724 PUSH_DATA (push, 1);
725 BEGIN_NV04(push, NV50_3D(RT_ADDRESS_HIGH(0)), 5);
726 PUSH_DATAh(push, buf->address + offset);
727 PUSH_DATA (push, buf->address + offset);
728 PUSH_DATA (push, nv50_format_table[dst_fmt].rt);
729 PUSH_DATA (push, 0);
730 PUSH_DATA (push, 0);
731 BEGIN_NV04(push, NV50_3D(RT_HORIZ(0)), 2);
732 PUSH_DATA (push, NV50_3D_RT_HORIZ_LINEAR | align(width * data_size, 0x100));
733 PUSH_DATA (push, height);
734 BEGIN_NV04(push, NV50_3D(ZETA_ENABLE), 1);
735 PUSH_DATA (push, 0);
736 BEGIN_NV04(push, NV50_3D(MULTISAMPLE_MODE), 1);
737 PUSH_DATA (push, 0);
738
739 /* NOTE: only works with D3D clear flag (5097/0x143c bit 4) */
740
741 BEGIN_NV04(push, NV50_3D(VIEWPORT_HORIZ(0)), 2);
742 PUSH_DATA (push, (width << 16));
743 PUSH_DATA (push, (height << 16));
744
745 BEGIN_NV04(push, NV50_3D(COND_MODE), 1);
746 PUSH_DATA (push, NV50_3D_COND_MODE_ALWAYS);
747
748 BEGIN_NI04(push, NV50_3D(CLEAR_BUFFERS), 1);
749 PUSH_DATA (push, 0x3c);
750
751 BEGIN_NV04(push, NV50_3D(COND_MODE), 1);
752 PUSH_DATA (push, nv50->cond_condmode);
753
754 nv50_resource_validate(nv50, buf, NOUVEAU_BO_WR);
755
756 if (width * height != elements) {
757 offset += width * height * data_size;
758 width = elements - width * height;
759 nv50_clear_buffer_push(pipe, res, offset, width * data_size,
760 data, data_size);
761 }
762
763 nv50->dirty_3d |= NV50_NEW_3D_FRAMEBUFFER | NV50_NEW_3D_SCISSOR;
764 }
765
766 /* =============================== BLIT CODE ===================================
767 */
768
769 struct nv50_blitter
770 {
771 struct nv50_program *fp[NV50_BLIT_MAX_TEXTURE_TYPES][NV50_BLIT_MODES];
772 struct nv50_program vp;
773
774 struct nv50_tsc_entry sampler[2]; /* nearest, bilinear */
775
776 mtx_t mutex;
777 };
778
779 struct nv50_blitctx
780 {
781 struct nv50_context *nv50;
782 struct nv50_program *fp;
783 uint8_t mode;
784 uint16_t color_mask;
785 uint8_t filter;
786 uint8_t render_condition_enable;
787 enum pipe_texture_target target;
788 struct {
789 struct pipe_framebuffer_state fb;
790 struct pipe_surface *cbuf0, *zsbuf;
791 struct nv50_window_rect_stateobj window_rect;
792 struct nv50_rasterizer_stateobj *rast;
793 struct nv50_program *vp;
794 struct nv50_program *gp;
795 struct nv50_program *fp;
796 unsigned num_textures[NV50_MAX_3D_SHADER_STAGES];
797 unsigned num_samplers[NV50_MAX_3D_SHADER_STAGES];
798 struct pipe_sampler_view *texture[2];
799 struct nv50_tsc_entry *sampler[2];
800 unsigned min_samples;
801 uint32_t dirty_3d;
802 } saved;
803 struct nv50_rasterizer_stateobj rast;
804 };
805
806 static void
807 nv50_blitter_make_vp(struct nv50_blitter *blit)
808 {
809 static const uint32_t code[] =
810 {
811 0x10000001, 0x0423c788, /* mov b32 o[0x00] s[0x00] */ /* HPOS.x */
812 0x10000205, 0x0423c788, /* mov b32 o[0x04] s[0x04] */ /* HPOS.y */
813 0x10000409, 0x0423c788, /* mov b32 o[0x08] s[0x08] */ /* TEXC.x */
814 0x1000060d, 0x0423c788, /* mov b32 o[0x0c] s[0x0c] */ /* TEXC.y */
815 0x10000811, 0x0423c789, /* mov b32 o[0x10] s[0x10] */ /* TEXC.z */
816 };
817
818 blit->vp.type = MESA_SHADER_VERTEX;
819 blit->vp.translated = true;
820 blit->vp.code = (uint32_t *)code; /* const_cast */
821 blit->vp.code_size = sizeof(code);
822 blit->vp.max_gpr = 4;
823 blit->vp.max_out = 5;
824 blit->vp.out_nr = 2;
825 blit->vp.out[0].mask = 0x3;
826 blit->vp.out[0].sn = TGSI_SEMANTIC_POSITION;
827 blit->vp.out[1].hw = 2;
828 blit->vp.out[1].mask = 0x7;
829 blit->vp.out[1].sn = TGSI_SEMANTIC_GENERIC;
830 blit->vp.out[1].si = 0;
831 blit->vp.vp.attrs[0] = 0x73;
832 blit->vp.vp.psiz = 0x40;
833 blit->vp.vp.edgeflag = 0x40;
834 }
835
836 void *
837 nv50_blitter_make_fp(struct pipe_context *pipe,
838 unsigned mode,
839 enum pipe_texture_target ptarg)
840 {
841 enum glsl_sampler_dim sampler_dim
842 = nv50_blit_get_glsl_sampler_dim(ptarg);
843 bool is_array = nv50_blit_is_array(ptarg);
844
845 bool tex_rgbaz = false;
846 bool tex_s = false;
847 bool cvt_un8 = false;
848
849 bool int_clamp = mode == NV50_BLIT_MODE_INT_CLAMP;
850 if (int_clamp)
851 mode = NV50_BLIT_MODE_PASS;
852
853 if (mode != NV50_BLIT_MODE_PASS &&
854 mode != NV50_BLIT_MODE_Z24X8 &&
855 mode != NV50_BLIT_MODE_X8Z24)
856 tex_s = true;
857
858 if (mode != NV50_BLIT_MODE_X24S8 &&
859 mode != NV50_BLIT_MODE_S8X24 &&
860 mode != NV50_BLIT_MODE_XS)
861 tex_rgbaz = true;
862
863 if (mode != NV50_BLIT_MODE_PASS &&
864 mode != NV50_BLIT_MODE_ZS &&
865 mode != NV50_BLIT_MODE_XS)
866 cvt_un8 = true;
867
868 const int chipset = nouveau_screen(pipe->screen)->device->chipset;
869 const nir_shader_compiler_options *options =
870 nv50_ir_nir_shader_compiler_options(chipset, MESA_SHADER_FRAGMENT);
871
872 struct nir_builder b =
873 nir_builder_init_simple_shader(MESA_SHADER_FRAGMENT, options,
874 "blitter_fp");
875
876 /* load coordinates */
877 const struct glsl_type* float3 = glsl_vector_type(GLSL_TYPE_FLOAT, 3);
878 nir_variable *coord_var =
879 nir_variable_create(b.shader, nir_var_shader_in, float3, "coord");
880 coord_var->data.location = VARYING_SLOT_VAR0;
881 coord_var->data.interpolation = INTERP_MODE_NOPERSPECTIVE;
882
883 nir_def *coord = nir_load_var(&b, coord_var);
884 if (ptarg == PIPE_TEXTURE_1D_ARRAY) {
885 /* Adjust coordinates. Depth is in z, but TEX expects it to be in y. */
886 coord = nir_channels(&b, coord, TGSI_WRITEMASK_XZ);
887 } else {
888 int size = glsl_get_sampler_dim_coordinate_components(sampler_dim);
889 if (is_array) size += 1;
890 coord = nir_trim_vector(&b, coord, size);
891 }
892
893 /* sample textures */
894 const struct glsl_type *sampler_type =
895 glsl_sampler_type(sampler_dim, false, is_array, GLSL_TYPE_FLOAT);
896
897 nir_def *s = NULL;
898 if (tex_s) {
899 nir_variable *sampler =
900 nir_variable_create(b.shader, nir_var_uniform,
901 sampler_type, "sampler_s");
902 sampler->data.binding = 1;
903
904 nir_deref_instr *tex_deref = nir_build_deref_var(&b, sampler);
905
906 s = nir_tex(&b, coord, .texture_deref = tex_deref, .sampler_deref = tex_deref);
907 s = nir_channel(&b, s, 0);
908 }
909
910 nir_def *rgba = NULL, *z = NULL;
911 if (tex_rgbaz) {
912 nir_variable *sampler =
913 nir_variable_create(b.shader, nir_var_uniform,
914 sampler_type, "sampler_rgbaz");
915 sampler->data.binding = 0;
916
917 nir_deref_instr *tex_deref = nir_build_deref_var(&b, sampler);
918
919 rgba = nir_tex(&b, coord, .texture_deref = tex_deref, .sampler_deref = tex_deref);
920 z = nir_channel(&b, rgba, 0);
921 }
922
923 /* handle signed to unsigned integer conversions */
924 if (int_clamp) {
925 rgba = nir_umin(&b, rgba, nir_imm_int(&b, 0x7fffffff));
926 }
927
928 /* handle conversions */
929 nir_def *out_ssa;
930 nir_component_mask_t out_mask = 0;
931 if (cvt_un8) {
932 if (tex_s) {
933 s = nir_i2f32(&b, s);
934 s = nir_fmul_imm(&b, s, 1.0f / 0xff);
935 } else {
936 s = nir_undef(&b, 1, 32);
937 }
938
939 if (tex_rgbaz) {
940 z = nir_fmul_imm(&b, z, (1 << 24) - 1);
941 z = nir_f2i32(&b, z);
942 z = nir_iand(&b, z, nir_imm_ivec3(&b, 0x0000ff,
943 0x00ff00,
944 0xff0000));
945 z = nir_i2f32(&b, z);
946 z = nir_fmul(&b, z, nir_imm_vec3(&b, 1.0f / 0x0000ff,
947 1.0f / 0x00ff00,
948 1.0f / 0xff0000));
949 } else {
950 z = nir_undef(&b, 3, 32);
951 }
952
953 if (mode == NV50_BLIT_MODE_Z24S8 ||
954 mode == NV50_BLIT_MODE_X24S8 ||
955 mode == NV50_BLIT_MODE_Z24X8) {
956 out_ssa = nir_vec4(&b,
957 nir_channel(&b, z, 0),
958 nir_channel(&b, z, 1),
959 nir_channel(&b, z, 2),
960 s);
961
962 if (tex_rgbaz) out_mask |= TGSI_WRITEMASK_XYZ;
963 if (tex_s) out_mask |= TGSI_WRITEMASK_W;
964 } else {
965 out_ssa = nir_vec4(&b,
966 s,
967 nir_channel(&b, z, 0),
968 nir_channel(&b, z, 1),
969 nir_channel(&b, z, 2));
970
971 if (tex_rgbaz) out_mask |= TGSI_WRITEMASK_YZW;
972 if (tex_s) out_mask |= TGSI_WRITEMASK_X;
973 }
974 } else {
975 if (mode == NV50_BLIT_MODE_PASS) {
976 out_ssa = rgba;
977 out_mask |= TGSI_WRITEMASK_XYZW;
978 } else {
979 out_ssa = nir_vec2(&b, z ? z : nir_undef(&b, 1, 32),
980 s ? s : nir_undef(&b, 1, 32));
981 if (tex_rgbaz) out_mask |= TGSI_WRITEMASK_X;
982 if (tex_s) out_mask |= TGSI_WRITEMASK_Y;
983 }
984 }
985
986 /* write output */
987 const struct glsl_type* out_type =
988 glsl_vector_type(GLSL_TYPE_FLOAT, out_ssa->num_components);
989 nir_variable *out_var =
990 nir_variable_create(b.shader, nir_var_shader_out, out_type, "out");
991 out_var->data.location = FRAG_RESULT_DATA0;
992
993 nir_store_var(&b, out_var, out_ssa, out_mask);
994
995 /* return shader */
996 NIR_PASS(_, b.shader, nir_lower_samplers);
997
998 return pipe_shader_from_nir(pipe, b.shader);
999 }
1000
1001 static void
1002 nv50_blitter_make_sampler(struct nv50_blitter *blit)
1003 {
1004 /* clamp to edge, min/max lod = 0, nearest filtering */
1005
1006 blit->sampler[0].id = -1;
1007
1008 blit->sampler[0].tsc[0] = G80_TSC_0_SRGB_CONVERSION |
1009 (G80_TSC_WRAP_CLAMP_TO_EDGE << G80_TSC_0_ADDRESS_U__SHIFT) |
1010 (G80_TSC_WRAP_CLAMP_TO_EDGE << G80_TSC_0_ADDRESS_V__SHIFT) |
1011 (G80_TSC_WRAP_CLAMP_TO_EDGE << G80_TSC_0_ADDRESS_P__SHIFT);
1012 blit->sampler[0].tsc[1] =
1013 G80_TSC_1_MAG_FILTER_NEAREST |
1014 G80_TSC_1_MIN_FILTER_NEAREST |
1015 G80_TSC_1_MIP_FILTER_NONE;
1016
1017 /* clamp to edge, min/max lod = 0, bilinear filtering */
1018
1019 blit->sampler[1].id = -1;
1020
1021 blit->sampler[1].tsc[0] = blit->sampler[0].tsc[0];
1022 blit->sampler[1].tsc[1] =
1023 G80_TSC_1_MAG_FILTER_LINEAR |
1024 G80_TSC_1_MIN_FILTER_LINEAR |
1025 G80_TSC_1_MIP_FILTER_NONE;
1026 }
1027
1028 unsigned
1029 nv50_blit_select_mode(const struct pipe_blit_info *info)
1030 {
1031 const unsigned mask = info->mask;
1032
1033 switch (info->dst.resource->format) {
1034 case PIPE_FORMAT_Z24_UNORM_S8_UINT:
1035 case PIPE_FORMAT_Z24X8_UNORM:
1036 case PIPE_FORMAT_X24S8_UINT:
1037 switch (mask & PIPE_MASK_ZS) {
1038 case PIPE_MASK_ZS: return NV50_BLIT_MODE_Z24S8;
1039 case PIPE_MASK_Z: return NV50_BLIT_MODE_Z24X8;
1040 default:
1041 return NV50_BLIT_MODE_X24S8;
1042 }
1043 case PIPE_FORMAT_S8_UINT_Z24_UNORM:
1044 case PIPE_FORMAT_X8Z24_UNORM:
1045 case PIPE_FORMAT_S8X24_UINT:
1046 switch (mask & PIPE_MASK_ZS) {
1047 case PIPE_MASK_ZS: return NV50_BLIT_MODE_S8Z24;
1048 case PIPE_MASK_Z: return NV50_BLIT_MODE_X8Z24;
1049 default:
1050 return NV50_BLIT_MODE_S8X24;
1051 }
1052 case PIPE_FORMAT_Z32_FLOAT:
1053 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT:
1054 case PIPE_FORMAT_X32_S8X24_UINT:
1055 switch (mask & PIPE_MASK_ZS) {
1056 case PIPE_MASK_ZS: return NV50_BLIT_MODE_ZS;
1057 case PIPE_MASK_Z: return NV50_BLIT_MODE_PASS;
1058 default:
1059 return NV50_BLIT_MODE_XS;
1060 }
1061 default:
1062 if (util_format_is_pure_uint(info->src.format) &&
1063 util_format_is_pure_sint(info->dst.format))
1064 return NV50_BLIT_MODE_INT_CLAMP;
1065 return NV50_BLIT_MODE_PASS;
1066 }
1067 }
1068
1069 static void
1070 nv50_blit_select_fp(struct nv50_blitctx *ctx, const struct pipe_blit_info *info)
1071 {
1072 struct nv50_blitter *blitter = ctx->nv50->screen->blitter;
1073
1074 const enum pipe_texture_target ptarg =
1075 nv50_blit_reinterpret_pipe_texture_target(info->src.resource->target);
1076
1077 const unsigned targ = nv50_blit_texture_type(ptarg);
1078 const unsigned mode = ctx->mode;
1079
1080 if (!blitter->fp[targ][mode]) {
1081 mtx_lock(&blitter->mutex);
1082 if (!blitter->fp[targ][mode])
1083 blitter->fp[targ][mode] =
1084 nv50_blitter_make_fp(&ctx->nv50->base.pipe, mode, ptarg);
1085 mtx_unlock(&blitter->mutex);
1086 }
1087 ctx->fp = blitter->fp[targ][mode];
1088 }
1089
1090 static void
1091 nv50_blit_set_dst(struct nv50_blitctx *ctx,
1092 struct pipe_resource *res, unsigned level, unsigned layer,
1093 enum pipe_format format)
1094 {
1095 struct nv50_context *nv50 = ctx->nv50;
1096 struct pipe_context *pipe = &nv50->base.pipe;
1097
1098 if (util_format_is_depth_or_stencil(format))
1099 format = nv50_blit_zeta_to_colour_format(format);
1100
1101 /* this will be overwritten (not released) at the end of the blit */
1102 nv50->framebuffer.cbufs[0] = (struct pipe_surface) {
1103 .texture = res,
1104 .format = format,
1105 .level = level,
1106 .first_layer = layer,
1107 .last_layer = layer,
1108 };
1109
1110 if (layer == -1) {
1111 nv50->framebuffer.cbufs[0].first_layer = 0;
1112 nv50->framebuffer.cbufs[0].last_layer =
1113 (res->target == PIPE_TEXTURE_3D ? res->depth0 : res->array_size) - 1;
1114 }
1115
1116 nv50->fb_cbufs[0] = nv50_miptree_surface_new(pipe, res, &nv50->framebuffer.cbufs[0]);
1117 nv50->framebuffer.nr_cbufs = 1;
1118 memset(&nv50->framebuffer.zsbuf, 0, sizeof(nv50->framebuffer.zsbuf));
1119 nv50->fb_zsbuf = NULL;
1120 pipe_surface_size(&nv50->framebuffer.cbufs[0], &nv50->framebuffer.width, &nv50->framebuffer.height);
1121 }
1122
1123 static void
1124 nv50_blit_set_src(struct nv50_blitctx *blit,
1125 struct pipe_resource *res, unsigned level, unsigned layer,
1126 enum pipe_format format, const uint8_t filter)
1127 {
1128 struct nv50_context *nv50 = blit->nv50;
1129 struct pipe_context *pipe = &nv50->base.pipe;
1130 struct pipe_sampler_view templ = {0};
1131 uint32_t flags;
1132 enum pipe_texture_target target;
1133
1134 target = nv50_blit_reinterpret_pipe_texture_target(res->target);
1135
1136 templ.target = target;
1137 templ.format = format;
1138 templ.u.tex.first_level = templ.u.tex.last_level = level;
1139 templ.u.tex.first_layer = templ.u.tex.last_layer = layer;
1140 templ.swizzle_r = PIPE_SWIZZLE_X;
1141 templ.swizzle_g = PIPE_SWIZZLE_Y;
1142 templ.swizzle_b = PIPE_SWIZZLE_Z;
1143 templ.swizzle_a = PIPE_SWIZZLE_W;
1144
1145 if (layer == -1) {
1146 templ.u.tex.first_layer = 0;
1147 templ.u.tex.last_layer =
1148 (res->target == PIPE_TEXTURE_3D ? res->depth0 : res->array_size) - 1;
1149 }
1150
1151 flags = res->last_level ? 0 : NV50_TEXVIEW_SCALED_COORDS;
1152 flags |= NV50_TEXVIEW_ACCESS_RESOLVE;
1153 if (filter && res->nr_samples == 8)
1154 flags |= NV50_TEXVIEW_FILTER_MSAA8;
1155
1156 nv50->textures[NV50_SHADER_STAGE_FRAGMENT][0] = nv50_create_texture_view(
1157 pipe, res, &templ, flags);
1158 nv50->textures[NV50_SHADER_STAGE_FRAGMENT][1] = NULL;
1159
1160 nv50->num_textures[NV50_SHADER_STAGE_VERTEX] = 0;
1161 nv50->num_textures[NV50_SHADER_STAGE_GEOMETRY] = 0;
1162 nv50->num_textures[NV50_SHADER_STAGE_FRAGMENT] = 1;
1163
1164 templ.format = nv50_zs_to_s_format(format);
1165 if (templ.format != res->format) {
1166 nv50->textures[NV50_SHADER_STAGE_FRAGMENT][1] = nv50_create_texture_view(
1167 pipe, res, &templ, flags);
1168 nv50->num_textures[NV50_SHADER_STAGE_FRAGMENT] = 2;
1169 }
1170 }
1171
1172 static void
1173 nv50_blitctx_prepare_state(struct nv50_blitctx *blit)
1174 {
1175 struct nouveau_pushbuf *push = blit->nv50->base.pushbuf;
1176
1177 if (blit->nv50->cond_query && !blit->render_condition_enable) {
1178 BEGIN_NV04(push, NV50_3D(COND_MODE), 1);
1179 PUSH_DATA (push, NV50_3D_COND_MODE_ALWAYS);
1180 }
1181
1182 /* blend state */
1183 BEGIN_NV04(push, NV50_3D(COLOR_MASK(0)), 1);
1184 PUSH_DATA (push, blit->color_mask);
1185 BEGIN_NV04(push, NV50_3D(BLEND_ENABLE(0)), 1);
1186 PUSH_DATA (push, 0);
1187 BEGIN_NV04(push, NV50_3D(LOGIC_OP_ENABLE), 1);
1188 PUSH_DATA (push, 0);
1189
1190 /* rasterizer state */
1191 #ifndef NV50_SCISSORS_CLIPPING
1192 BEGIN_NV04(push, NV50_3D(SCISSOR_ENABLE(0)), 1);
1193 PUSH_DATA (push, 1);
1194 #endif
1195 BEGIN_NV04(push, NV50_3D(VERTEX_TWO_SIDE_ENABLE), 1);
1196 PUSH_DATA (push, 0);
1197 BEGIN_NV04(push, NV50_3D(FRAG_COLOR_CLAMP_EN), 1);
1198 PUSH_DATA (push, 0);
1199 BEGIN_NV04(push, NV50_3D(MULTISAMPLE_ENABLE), 1);
1200 PUSH_DATA (push, 0);
1201 BEGIN_NV04(push, NV50_3D(MSAA_MASK(0)), 4);
1202 PUSH_DATA (push, 0xffff);
1203 PUSH_DATA (push, 0xffff);
1204 PUSH_DATA (push, 0xffff);
1205 PUSH_DATA (push, 0xffff);
1206 BEGIN_NV04(push, NV50_3D(POLYGON_MODE_FRONT), 3);
1207 PUSH_DATA (push, NV50_3D_POLYGON_MODE_FRONT_FILL);
1208 PUSH_DATA (push, NV50_3D_POLYGON_MODE_BACK_FILL);
1209 PUSH_DATA (push, 0);
1210 BEGIN_NV04(push, NV50_3D(CULL_FACE_ENABLE), 1);
1211 PUSH_DATA (push, 0);
1212 BEGIN_NV04(push, NV50_3D(POLYGON_STIPPLE_ENABLE), 1);
1213 PUSH_DATA (push, 0);
1214 BEGIN_NV04(push, NV50_3D(POLYGON_OFFSET_FILL_ENABLE), 1);
1215 PUSH_DATA (push, 0);
1216
1217 /* zsa state */
1218 BEGIN_NV04(push, NV50_3D(DEPTH_TEST_ENABLE), 1);
1219 PUSH_DATA (push, 0);
1220 BEGIN_NV04(push, NV50_3D(DEPTH_BOUNDS_EN), 1);
1221 PUSH_DATA (push, 0);
1222 BEGIN_NV04(push, NV50_3D(STENCIL_ENABLE), 1);
1223 PUSH_DATA (push, 0);
1224 BEGIN_NV04(push, NV50_3D(ALPHA_TEST_ENABLE), 1);
1225 PUSH_DATA (push, 0);
1226 }
1227
1228 static void
1229 nv50_blitctx_pre_blit(struct nv50_blitctx *ctx,
1230 const struct pipe_blit_info *info)
1231 {
1232 struct nv50_context *nv50 = ctx->nv50;
1233 struct nv50_blitter *blitter = nv50->screen->blitter;
1234 int s;
1235
1236 ctx->saved.fb.width = nv50->framebuffer.width;
1237 ctx->saved.fb.height = nv50->framebuffer.height;
1238 ctx->saved.fb.nr_cbufs = nv50->framebuffer.nr_cbufs;
1239 ctx->saved.fb.cbufs[0] = nv50->framebuffer.cbufs[0];
1240 ctx->saved.fb.zsbuf = nv50->framebuffer.zsbuf;
1241 ctx->saved.cbuf0 = nv50->fb_cbufs[0];
1242 ctx->saved.zsbuf = nv50->fb_zsbuf;
1243
1244 ctx->saved.rast = nv50->rast;
1245
1246 ctx->saved.vp = nv50->vertprog;
1247 ctx->saved.gp = nv50->gmtyprog;
1248 ctx->saved.fp = nv50->fragprog;
1249
1250 ctx->saved.min_samples = nv50->min_samples;
1251 ctx->saved.window_rect = nv50->window_rect;
1252
1253 nv50->rast = &ctx->rast;
1254
1255 nv50->vertprog = &blitter->vp;
1256 nv50->gmtyprog = NULL;
1257 nv50->fragprog = ctx->fp;
1258
1259 nv50->window_rect.rects =
1260 MIN2(info->num_window_rectangles, NV50_MAX_WINDOW_RECTANGLES);
1261 nv50->window_rect.inclusive = info->window_rectangle_include;
1262 if (nv50->window_rect.rects)
1263 memcpy(nv50->window_rect.rect, info->window_rectangles,
1264 sizeof(struct pipe_scissor_state) * nv50->window_rect.rects);
1265
1266 for (s = 0; s < NV50_MAX_3D_SHADER_STAGES; ++s) {
1267 ctx->saved.num_textures[s] = nv50->num_textures[s];
1268 ctx->saved.num_samplers[s] = nv50->num_samplers[s];
1269 }
1270 ctx->saved.texture[0] = nv50->textures[NV50_SHADER_STAGE_FRAGMENT][0];
1271 ctx->saved.texture[1] = nv50->textures[NV50_SHADER_STAGE_FRAGMENT][1];
1272 ctx->saved.sampler[0] = nv50->samplers[NV50_SHADER_STAGE_FRAGMENT][0];
1273 ctx->saved.sampler[1] = nv50->samplers[NV50_SHADER_STAGE_FRAGMENT][1];
1274
1275 nv50->samplers[NV50_SHADER_STAGE_FRAGMENT][0] = &blitter->sampler[ctx->filter];
1276 nv50->samplers[NV50_SHADER_STAGE_FRAGMENT][1] = &blitter->sampler[ctx->filter];
1277
1278 nv50->num_samplers[NV50_SHADER_STAGE_VERTEX] = 0;
1279 nv50->num_samplers[NV50_SHADER_STAGE_GEOMETRY] = 0;
1280 nv50->num_samplers[NV50_SHADER_STAGE_FRAGMENT] = 2;
1281
1282 nv50->min_samples = 1;
1283
1284 ctx->saved.dirty_3d = nv50->dirty_3d;
1285
1286 nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_3D_FB);
1287 nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_3D_TEXTURES);
1288
1289 nv50->dirty_3d =
1290 NV50_NEW_3D_FRAMEBUFFER | NV50_NEW_3D_MIN_SAMPLES |
1291 NV50_NEW_3D_VERTPROG | NV50_NEW_3D_FRAGPROG | NV50_NEW_3D_GMTYPROG |
1292 NV50_NEW_3D_TEXTURES | NV50_NEW_3D_SAMPLERS | NV50_NEW_3D_WINDOW_RECTS;
1293 }
1294
1295 static void
1296 nv50_blitctx_post_blit(struct nv50_blitctx *blit)
1297 {
1298 struct nv50_context *nv50 = blit->nv50;
1299 int s;
1300
1301 nv50_surface_destroy(&nv50->base.pipe, nv50->fb_cbufs[0]);
1302
1303 nv50->framebuffer.width = blit->saved.fb.width;
1304 nv50->framebuffer.height = blit->saved.fb.height;
1305 nv50->framebuffer.nr_cbufs = blit->saved.fb.nr_cbufs;
1306 nv50->framebuffer.cbufs[0] = blit->saved.fb.cbufs[0];
1307 nv50->framebuffer.zsbuf = blit->saved.fb.zsbuf;
1308 nv50->fb_cbufs[0] = blit->saved.cbuf0;
1309 nv50->fb_zsbuf = blit->saved.zsbuf;
1310
1311 nv50->rast = blit->saved.rast;
1312
1313 nv50->vertprog = blit->saved.vp;
1314 nv50->gmtyprog = blit->saved.gp;
1315 nv50->fragprog = blit->saved.fp;
1316
1317 nv50->min_samples = blit->saved.min_samples;
1318 nv50->window_rect = blit->saved.window_rect;
1319
1320 pipe_sampler_view_reference(&nv50->textures[NV50_SHADER_STAGE_FRAGMENT][0], NULL);
1321 pipe_sampler_view_reference(&nv50->textures[NV50_SHADER_STAGE_FRAGMENT][1], NULL);
1322
1323 for (s = 0; s < NV50_MAX_3D_SHADER_STAGES; ++s) {
1324 nv50->num_textures[s] = blit->saved.num_textures[s];
1325 nv50->num_samplers[s] = blit->saved.num_samplers[s];
1326 }
1327 nv50->textures[NV50_SHADER_STAGE_FRAGMENT][0] = blit->saved.texture[0];
1328 nv50->textures[NV50_SHADER_STAGE_FRAGMENT][1] = blit->saved.texture[1];
1329 nv50->samplers[NV50_SHADER_STAGE_FRAGMENT][0] = blit->saved.sampler[0];
1330 nv50->samplers[NV50_SHADER_STAGE_FRAGMENT][1] = blit->saved.sampler[1];
1331
1332 if (nv50->cond_query && !blit->render_condition_enable)
1333 nv50->base.pipe.render_condition(&nv50->base.pipe, nv50->cond_query,
1334 nv50->cond_cond, nv50->cond_mode);
1335
1336 nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_3D_FB);
1337 nouveau_bufctx_reset(nv50->bufctx_3d, NV50_BIND_3D_TEXTURES);
1338
1339 nv50->dirty_3d = blit->saved.dirty_3d |
1340 (NV50_NEW_3D_FRAMEBUFFER | NV50_NEW_3D_SCISSOR | NV50_NEW_3D_SAMPLE_MASK |
1341 NV50_NEW_3D_RASTERIZER | NV50_NEW_3D_ZSA | NV50_NEW_3D_BLEND |
1342 NV50_NEW_3D_TEXTURES | NV50_NEW_3D_SAMPLERS | NV50_NEW_3D_WINDOW_RECTS |
1343 NV50_NEW_3D_VERTPROG | NV50_NEW_3D_GMTYPROG | NV50_NEW_3D_FRAGPROG);
1344 nv50->scissors_dirty |= 1;
1345
1346 nv50->base.pipe.set_min_samples(&nv50->base.pipe, blit->saved.min_samples);
1347 }
1348
1349
1350 static void
1351 nv50_blit_3d(struct nv50_context *nv50, const struct pipe_blit_info *info)
1352 {
1353 struct nv50_blitctx *blit = nv50->blit;
1354 struct nouveau_pushbuf *push = nv50->base.pushbuf;
1355 struct pipe_resource *src = info->src.resource;
1356 struct pipe_resource *dst = info->dst.resource;
1357 int32_t minx, maxx, miny, maxy;
1358 int32_t i;
1359 float x0, x1, y0, y1, z;
1360 float dz;
1361 float x_range, y_range;
1362
1363 blit->mode = nv50_blit_select_mode(info);
1364 blit->color_mask = nv50_blit_derive_color_mask(info);
1365 blit->filter = nv50_blit_get_filter(info);
1366 blit->render_condition_enable = info->render_condition_enable;
1367
1368 nv50_blit_select_fp(blit, info);
1369 nv50_blitctx_pre_blit(blit, info);
1370
1371 nv50_blit_set_dst(blit, dst, info->dst.level, -1, info->dst.format);
1372 nv50_blit_set_src(blit, src, info->src.level, -1, info->src.format,
1373 blit->filter);
1374
1375 nv50_blitctx_prepare_state(blit);
1376
1377 nv50_state_validate_3d(nv50, ~0);
1378
1379 /* When flipping a surface from zeta <-> color "mode", we have to wait for
1380 * the GPU to flush its current draws.
1381 */
1382 struct nv50_miptree *mt = nv50_miptree(dst);
1383 bool serialize = util_format_is_depth_or_stencil(info->dst.format);
1384 if (serialize && mt->base.status & NOUVEAU_BUFFER_STATUS_GPU_WRITING) {
1385 BEGIN_NV04(push, SUBC_3D(NV50_GRAPH_SERIALIZE), 1);
1386 PUSH_DATA (push, 0);
1387 }
1388
1389 x_range = (float)info->src.box.width / (float)info->dst.box.width;
1390 y_range = (float)info->src.box.height / (float)info->dst.box.height;
1391
1392 x0 = (float)info->src.box.x - x_range * (float)info->dst.box.x;
1393 y0 = (float)info->src.box.y - y_range * (float)info->dst.box.y;
1394
1395 x1 = x0 + 16384.0f * x_range;
1396 y1 = y0 + 16384.0f * y_range;
1397
1398 x0 *= (float)(1 << nv50_miptree(src)->ms_x);
1399 x1 *= (float)(1 << nv50_miptree(src)->ms_x);
1400 y0 *= (float)(1 << nv50_miptree(src)->ms_y);
1401 y1 *= (float)(1 << nv50_miptree(src)->ms_y);
1402
1403 /* XXX: multiply by 6 for cube arrays ? */
1404 dz = (float)info->src.box.depth / (float)info->dst.box.depth;
1405 z = (float)info->src.box.z;
1406 if (nv50_miptree(src)->layout_3d)
1407 z += 0.5f * dz;
1408
1409 if (src->last_level > 0) {
1410 /* If there are mip maps, GPU always assumes normalized coordinates. */
1411 const unsigned l = info->src.level;
1412 const float fh = u_minify(src->width0 << nv50_miptree(src)->ms_x, l);
1413 const float fv = u_minify(src->height0 << nv50_miptree(src)->ms_y, l);
1414 x0 /= fh;
1415 x1 /= fh;
1416 y0 /= fv;
1417 y1 /= fv;
1418 if (nv50_miptree(src)->layout_3d) {
1419 z /= u_minify(src->depth0, l);
1420 dz /= u_minify(src->depth0, l);
1421 }
1422 }
1423
1424 BEGIN_NV04(push, NV50_3D(VIEWPORT_TRANSFORM_EN), 1);
1425 PUSH_DATA (push, 0);
1426 BEGIN_NV04(push, NV50_3D(VIEW_VOLUME_CLIP_CTRL), 1);
1427 PUSH_DATA (push, 0x1);
1428
1429 /* Draw a large triangle in screen coordinates covering the whole
1430 * render target, with scissors defining the destination region.
1431 * The vertex is supplied with non-normalized texture coordinates
1432 * arranged in a way to yield the desired offset and scale.
1433 */
1434
1435 minx = info->dst.box.x;
1436 maxx = info->dst.box.x + info->dst.box.width;
1437 miny = info->dst.box.y;
1438 maxy = info->dst.box.y + info->dst.box.height;
1439 if (info->scissor_enable) {
1440 minx = MAX2(minx, info->scissor.minx);
1441 maxx = MIN2(maxx, info->scissor.maxx);
1442 miny = MAX2(miny, info->scissor.miny);
1443 maxy = MIN2(maxy, info->scissor.maxy);
1444 }
1445 BEGIN_NV04(push, NV50_3D(SCISSOR_HORIZ(0)), 2);
1446 PUSH_DATA (push, (maxx << 16) | minx);
1447 PUSH_DATA (push, (maxy << 16) | miny);
1448
1449 for (i = 0; i < info->dst.box.depth; ++i, z += dz) {
1450 if (info->dst.box.z + i) {
1451 BEGIN_NV04(push, NV50_3D(LAYER), 1);
1452 PUSH_DATA (push, info->dst.box.z + i);
1453 }
1454 PUSH_SPACE(push, 32);
1455 BEGIN_NV04(push, NV50_3D(VERTEX_BEGIN_GL), 1);
1456 PUSH_DATA (push, NV50_3D_VERTEX_BEGIN_GL_PRIMITIVE_TRIANGLES);
1457 BEGIN_NV04(push, NV50_3D(VTX_ATTR_3F_X(1)), 3);
1458 PUSH_DATAf(push, x0);
1459 PUSH_DATAf(push, y0);
1460 PUSH_DATAf(push, z);
1461 BEGIN_NV04(push, NV50_3D(VTX_ATTR_2F_X(0)), 2);
1462 PUSH_DATAf(push, 0.0f);
1463 PUSH_DATAf(push, 0.0f);
1464 BEGIN_NV04(push, NV50_3D(VTX_ATTR_3F_X(1)), 3);
1465 PUSH_DATAf(push, x1);
1466 PUSH_DATAf(push, y0);
1467 PUSH_DATAf(push, z);
1468 BEGIN_NV04(push, NV50_3D(VTX_ATTR_2F_X(0)), 2);
1469 PUSH_DATAf(push, 16384.0f);
1470 PUSH_DATAf(push, 0.0f);
1471 BEGIN_NV04(push, NV50_3D(VTX_ATTR_3F_X(1)), 3);
1472 PUSH_DATAf(push, x0);
1473 PUSH_DATAf(push, y1);
1474 PUSH_DATAf(push, z);
1475 BEGIN_NV04(push, NV50_3D(VTX_ATTR_2F_X(0)), 2);
1476 PUSH_DATAf(push, 0.0f);
1477 PUSH_DATAf(push, 16384.0f);
1478 BEGIN_NV04(push, NV50_3D(VERTEX_END_GL), 1);
1479 PUSH_DATA (push, 0);
1480 }
1481 if (info->dst.box.z + info->dst.box.depth - 1) {
1482 BEGIN_NV04(push, NV50_3D(LAYER), 1);
1483 PUSH_DATA (push, 0);
1484 }
1485
1486 /* re-enable normally constant state */
1487
1488 BEGIN_NV04(push, NV50_3D(VIEWPORT_TRANSFORM_EN), 1);
1489 PUSH_DATA (push, 1);
1490
1491 /* mark the surface as reading, which will force a serialize next time it's
1492 * used for writing.
1493 */
1494 if (serialize)
1495 mt->base.status |= NOUVEAU_BUFFER_STATUS_GPU_READING;
1496
1497 nv50_blitctx_post_blit(blit);
1498 }
1499
1500 static void
1501 nv50_blit_eng2d(struct nv50_context *nv50, const struct pipe_blit_info *info)
1502 {
1503 struct nouveau_pushbuf *push = nv50->base.pushbuf;
1504 struct nv50_miptree *dst = nv50_miptree(info->dst.resource);
1505 struct nv50_miptree *src = nv50_miptree(info->src.resource);
1506 const int32_t srcx_adj = info->src.box.width < 0 ? -1 : 0;
1507 const int32_t srcy_adj = info->src.box.height < 0 ? -1 : 0;
1508 const int32_t dz = info->dst.box.z;
1509 const int32_t sz = info->src.box.z;
1510 uint32_t dstw, dsth;
1511 int32_t dstx, dsty;
1512 int64_t srcx, srcy;
1513 int64_t du_dx, dv_dy;
1514 int i;
1515 uint32_t mode;
1516 uint32_t mask = nv50_blit_eng2d_get_mask(info);
1517 bool b;
1518
1519 mode = nv50_blit_get_filter(info) ?
1520 NV50_2D_BLIT_CONTROL_FILTER_BILINEAR :
1521 NV50_2D_BLIT_CONTROL_FILTER_POINT_SAMPLE;
1522 mode |= (src->base.base.nr_samples > dst->base.base.nr_samples) ?
1523 NV50_2D_BLIT_CONTROL_ORIGIN_CORNER : NV50_2D_BLIT_CONTROL_ORIGIN_CENTER;
1524
1525 du_dx = ((int64_t)info->src.box.width << 32) / info->dst.box.width;
1526 dv_dy = ((int64_t)info->src.box.height << 32) / info->dst.box.height;
1527
1528 b = info->dst.format == info->src.format;
1529 nv50_2d_texture_set(push, 1, dst, info->dst.level, dz, info->dst.format, b);
1530 nv50_2d_texture_set(push, 0, src, info->src.level, sz, info->src.format, b);
1531
1532 if (info->scissor_enable) {
1533 BEGIN_NV04(push, NV50_2D(CLIP_X), 5);
1534 PUSH_DATA (push, info->scissor.minx << dst->ms_x);
1535 PUSH_DATA (push, info->scissor.miny << dst->ms_y);
1536 PUSH_DATA (push, (info->scissor.maxx - info->scissor.minx) << dst->ms_x);
1537 PUSH_DATA (push, (info->scissor.maxy - info->scissor.miny) << dst->ms_y);
1538 PUSH_DATA (push, 1); /* enable */
1539 }
1540
1541 if (nv50->cond_query && info->render_condition_enable) {
1542 BEGIN_NV04(push, NV50_2D(COND_MODE), 1);
1543 PUSH_DATA (push, nv50->cond_condmode);
1544 }
1545
1546 if (mask != 0xffffffff) {
1547 BEGIN_NV04(push, NV50_2D(ROP), 1);
1548 PUSH_DATA (push, 0xca); /* DPSDxax */
1549 BEGIN_NV04(push, NV50_2D(PATTERN_COLOR_FORMAT), 1);
1550 PUSH_DATA (push, NV50_2D_PATTERN_COLOR_FORMAT_A8R8G8B8);
1551 BEGIN_NV04(push, NV50_2D(PATTERN_BITMAP_COLOR(0)), 4);
1552 PUSH_DATA (push, 0x00000000);
1553 PUSH_DATA (push, mask);
1554 PUSH_DATA (push, 0xffffffff);
1555 PUSH_DATA (push, 0xffffffff);
1556 BEGIN_NV04(push, NV50_2D(OPERATION), 1);
1557 PUSH_DATA (push, NV50_2D_OPERATION_ROP);
1558 } else
1559 if (info->src.format != info->dst.format) {
1560 if (info->src.format == PIPE_FORMAT_R8_UNORM ||
1561 info->src.format == PIPE_FORMAT_R16_UNORM ||
1562 info->src.format == PIPE_FORMAT_R16_FLOAT ||
1563 info->src.format == PIPE_FORMAT_R32_FLOAT) {
1564 mask = 0xffff0000; /* also makes condition for OPERATION reset true */
1565 BEGIN_NV04(push, NV50_2D(BETA4), 2);
1566 PUSH_DATA (push, mask);
1567 PUSH_DATA (push, NV50_2D_OPERATION_SRCCOPY_PREMULT);
1568 }
1569 }
1570
1571 if (src->ms_x > dst->ms_x || src->ms_y > dst->ms_y) {
1572 /* ms_x is always >= ms_y */
1573 du_dx <<= src->ms_x - dst->ms_x;
1574 dv_dy <<= src->ms_y - dst->ms_y;
1575 } else {
1576 du_dx >>= dst->ms_x - src->ms_x;
1577 dv_dy >>= dst->ms_y - src->ms_y;
1578 }
1579
1580 srcx = (int64_t)(info->src.box.x + srcx_adj) << (src->ms_x + 32);
1581 srcy = (int64_t)(info->src.box.y + srcy_adj) << (src->ms_y + 32);
1582
1583 if (src->base.base.nr_samples > dst->base.base.nr_samples) {
1584 /* center src coorinates for proper MS resolve filtering */
1585 srcx += (int64_t)1 << (src->ms_x + 31);
1586 srcy += (int64_t)1 << (src->ms_y + 31);
1587 }
1588
1589 dstx = info->dst.box.x << dst->ms_x;
1590 dsty = info->dst.box.y << dst->ms_y;
1591
1592 dstw = info->dst.box.width << dst->ms_x;
1593 dsth = info->dst.box.height << dst->ms_y;
1594
1595 if (dstx < 0) {
1596 dstw += dstx;
1597 srcx -= du_dx * dstx;
1598 dstx = 0;
1599 }
1600 if (dsty < 0) {
1601 dsth += dsty;
1602 srcy -= dv_dy * dsty;
1603 dsty = 0;
1604 }
1605
1606 BEGIN_NV04(push, NV50_2D(BLIT_CONTROL), 1);
1607 PUSH_DATA (push, mode);
1608 BEGIN_NV04(push, NV50_2D(BLIT_DST_X), 4);
1609 PUSH_DATA (push, dstx);
1610 PUSH_DATA (push, dsty);
1611 PUSH_DATA (push, dstw);
1612 PUSH_DATA (push, dsth);
1613 BEGIN_NV04(push, NV50_2D(BLIT_DU_DX_FRACT), 4);
1614 PUSH_DATA (push, du_dx);
1615 PUSH_DATA (push, du_dx >> 32);
1616 PUSH_DATA (push, dv_dy);
1617 PUSH_DATA (push, dv_dy >> 32);
1618
1619 BCTX_REFN(nv50->bufctx, 2D, &dst->base, WR);
1620 BCTX_REFN(nv50->bufctx, 2D, &src->base, RD);
1621 nouveau_pushbuf_bufctx(nv50->base.pushbuf, nv50->bufctx);
1622 if (PUSH_VAL(nv50->base.pushbuf))
1623 return;
1624
1625 for (i = 0; i < info->dst.box.depth; ++i) {
1626 if (i > 0) {
1627 /* no scaling in z-direction possible for eng2d blits */
1628 if (dst->layout_3d) {
1629 BEGIN_NV04(push, NV50_2D(DST_LAYER), 1);
1630 PUSH_DATA (push, info->dst.box.z + i);
1631 } else {
1632 const unsigned z = info->dst.box.z + i;
1633 const uint64_t address = dst->base.address +
1634 dst->level[info->dst.level].offset +
1635 z * dst->layer_stride;
1636 BEGIN_NV04(push, NV50_2D(DST_ADDRESS_HIGH), 2);
1637 PUSH_DATAh(push, address);
1638 PUSH_DATA (push, address);
1639 }
1640 if (src->layout_3d) {
1641 /* not possible because of depth tiling */
1642 assert(0);
1643 } else {
1644 const unsigned z = info->src.box.z + i;
1645 const uint64_t address = src->base.address +
1646 src->level[info->src.level].offset +
1647 z * src->layer_stride;
1648 BEGIN_NV04(push, NV50_2D(SRC_ADDRESS_HIGH), 2);
1649 PUSH_DATAh(push, address);
1650 PUSH_DATA (push, address);
1651 }
1652 BEGIN_NV04(push, NV50_2D(BLIT_SRC_Y_INT), 1); /* trigger */
1653 PUSH_DATA (push, srcy >> 32);
1654 } else {
1655 BEGIN_NV04(push, NV50_2D(BLIT_SRC_X_FRACT), 4);
1656 PUSH_DATA (push, srcx);
1657 PUSH_DATA (push, srcx >> 32);
1658 PUSH_DATA (push, srcy);
1659 PUSH_DATA (push, srcy >> 32);
1660 }
1661 }
1662 nv50_bufctx_fence(nv50, nv50->bufctx, false);
1663
1664 nouveau_bufctx_reset(nv50->bufctx, NV50_BIND_2D);
1665
1666 if (info->scissor_enable) {
1667 BEGIN_NV04(push, NV50_2D(CLIP_ENABLE), 1);
1668 PUSH_DATA (push, 0);
1669 }
1670 if (mask != 0xffffffff) {
1671 BEGIN_NV04(push, NV50_2D(OPERATION), 1);
1672 PUSH_DATA (push, NV50_2D_OPERATION_SRCCOPY);
1673 }
1674 if (nv50->cond_query && info->render_condition_enable) {
1675 BEGIN_NV04(push, NV50_2D(COND_MODE), 1);
1676 PUSH_DATA (push, NV50_2D_COND_MODE_ALWAYS);
1677 }
1678 }
1679
1680 static void
1681 nv50_blit(struct pipe_context *pipe, const struct pipe_blit_info *info)
1682 {
1683 struct nv50_context *nv50 = nv50_context(pipe);
1684 struct nouveau_pushbuf *push = nv50->base.pushbuf;
1685 bool eng3d = false;
1686
1687 if (info->src.box.width == 0 || info->src.box.height == 0 ||
1688 info->dst.box.width == 0 || info->dst.box.height == 0) {
1689 util_debug_message(&nv50->base.debug, ERROR,
1690 "Blit with zero-size src or dst box");
1691 return;
1692 }
1693
1694 if (util_format_is_depth_or_stencil(info->dst.resource->format)) {
1695 if (!(info->mask & PIPE_MASK_ZS))
1696 return;
1697 if (info->dst.resource->format == PIPE_FORMAT_Z32_FLOAT ||
1698 info->dst.resource->format == PIPE_FORMAT_Z32_FLOAT_S8X24_UINT)
1699 eng3d = true;
1700 if (info->filter != PIPE_TEX_FILTER_NEAREST)
1701 eng3d = true;
1702 } else {
1703 if (!(info->mask & PIPE_MASK_RGBA))
1704 return;
1705 if (info->mask != PIPE_MASK_RGBA)
1706 eng3d = true;
1707 }
1708
1709 if (nv50_miptree(info->src.resource)->layout_3d) {
1710 eng3d = true;
1711 } else
1712 if (info->src.box.depth != info->dst.box.depth) {
1713 eng3d = true;
1714 debug_printf("blit: cannot filter array or cube textures in z direction");
1715 }
1716
1717 if (!eng3d && info->dst.format != info->src.format) {
1718 if (!nv50_2d_dst_format_faithful(info->dst.format) ||
1719 !nv50_2d_src_format_faithful(info->src.format)) {
1720 eng3d = true;
1721 } else
1722 if (!nv50_2d_src_format_faithful(info->src.format)) {
1723 if (!util_format_is_luminance(info->src.format)) {
1724 if (util_format_is_intensity(info->src.format))
1725 eng3d = true;
1726 else
1727 if (!nv50_2d_dst_format_ops_supported(info->dst.format))
1728 eng3d = true;
1729 else
1730 eng3d = !nv50_2d_format_supported(info->src.format);
1731 }
1732 } else
1733 if (util_format_is_luminance_alpha(info->src.format))
1734 eng3d = true;
1735 }
1736
1737 if (info->src.resource->nr_samples == 8 &&
1738 info->dst.resource->nr_samples <= 1)
1739 eng3d = true;
1740
1741 if (info->num_window_rectangles > 0 || info->window_rectangle_include)
1742 eng3d = true;
1743
1744 /* FIXME: can't make this work with eng2d anymore */
1745 if ((info->src.resource->nr_samples | 1) !=
1746 (info->dst.resource->nr_samples | 1))
1747 eng3d = true;
1748
1749 /* FIXME: find correct src coordinate adjustments */
1750 if ((info->src.box.width != info->dst.box.width &&
1751 info->src.box.width != -info->dst.box.width) ||
1752 (info->src.box.height != info->dst.box.height &&
1753 info->src.box.height != -info->dst.box.height))
1754 eng3d = true;
1755
1756 simple_mtx_lock(&nv50->screen->state_lock);
1757 if (nv50->screen->num_occlusion_queries_active) {
1758 BEGIN_NV04(push, NV50_3D(SAMPLECNT_ENABLE), 1);
1759 PUSH_DATA (push, 0);
1760 }
1761
1762 if (!eng3d)
1763 nv50_blit_eng2d(nv50, info);
1764 else
1765 nv50_blit_3d(nv50, info);
1766
1767 if (nv50->screen->num_occlusion_queries_active) {
1768 BEGIN_NV04(push, NV50_3D(SAMPLECNT_ENABLE), 1);
1769 PUSH_DATA (push, 1);
1770 }
1771 PUSH_KICK(push);
1772 simple_mtx_unlock(&nv50->screen->state_lock);
1773 }
1774
1775 static void
1776 nv50_flush_resource(struct pipe_context *ctx,
1777 struct pipe_resource *resource)
1778 {
1779 }
1780
1781 bool
1782 nv50_blitter_create(struct nv50_screen *screen)
1783 {
1784 screen->blitter = CALLOC_STRUCT(nv50_blitter);
1785 if (!screen->blitter) {
1786 NOUVEAU_ERR("failed to allocate blitter struct\n");
1787 return false;
1788 }
1789
1790 (void) mtx_init(&screen->blitter->mutex, mtx_plain);
1791
1792 nv50_blitter_make_vp(screen->blitter);
1793 nv50_blitter_make_sampler(screen->blitter);
1794
1795 return true;
1796 }
1797
1798 void
1799 nv50_blitter_destroy(struct nv50_screen *screen)
1800 {
1801 struct nv50_blitter *blitter = screen->blitter;
1802 unsigned i, m;
1803
1804 for (i = 0; i < NV50_BLIT_MAX_TEXTURE_TYPES; ++i) {
1805 for (m = 0; m < NV50_BLIT_MODES; ++m) {
1806 struct nv50_program *prog = blitter->fp[i][m];
1807 if (prog) {
1808 nv50_program_destroy(NULL, prog);
1809 ralloc_free((void *)prog->nir);
1810 FREE(prog);
1811 }
1812 }
1813 }
1814
1815 mtx_destroy(&blitter->mutex);
1816 FREE(blitter);
1817 }
1818
1819 bool
1820 nv50_blitctx_create(struct nv50_context *nv50)
1821 {
1822 nv50->blit = CALLOC_STRUCT(nv50_blitctx);
1823 if (!nv50->blit) {
1824 NOUVEAU_ERR("failed to allocate blit context\n");
1825 return false;
1826 }
1827
1828 nv50->blit->nv50 = nv50;
1829
1830 nv50->blit->rast.pipe.half_pixel_center = 1;
1831
1832 return true;
1833 }
1834
1835 void
1836 nv50_init_surface_functions(struct nv50_context *nv50)
1837 {
1838 struct pipe_context *pipe = &nv50->base.pipe;
1839
1840 pipe->resource_copy_region = nv50_resource_copy_region;
1841 pipe->blit = nv50_blit;
1842 pipe->flush_resource = nv50_flush_resource;
1843 pipe->clear_texture = u_default_clear_texture;
1844 pipe->clear_render_target = nv50_clear_render_target;
1845 pipe->clear_depth_stencil = nv50_clear_depth_stencil;
1846 pipe->clear_buffer = nv50_clear_buffer;
1847 }