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