OSDN Git Service

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