nv50_surface.c revision b328949a37fee7b0f68ed3e068ffc4426c083042
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 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF 19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 20 * 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_surface.h" 31 32#include "nv50_context.h" 33#include "nv50_resource.h" 34 35#include "nv50_defs.xml.h" 36 37#define NV50_ENG2D_SUPPORTED_FORMATS 0xff0843e080608409ULL 38 39/* return TRUE for formats that can be converted among each other by NV50_2D */ 40static INLINE boolean 41nv50_2d_format_faithful(enum pipe_format format) 42{ 43 uint8_t id = nv50_format_table[format].rt; 44 45 return (id >= 0xc0) && 46 (NV50_ENG2D_SUPPORTED_FORMATS & (1ULL << (id - 0xc0))); 47} 48 49static INLINE uint8_t 50nv50_2d_format(enum pipe_format format) 51{ 52 uint8_t id = nv50_format_table[format].rt; 53 54 /* Hardware values for color formats range from 0xc0 to 0xff, 55 * but the 2D engine doesn't support all of them. 56 */ 57 if ((id >= 0xc0) && (NV50_ENG2D_SUPPORTED_FORMATS & (1ULL << (id - 0xc0)))) 58 return id; 59 60 switch (util_format_get_blocksize(format)) { 61 case 1: 62 return NV50_SURFACE_FORMAT_R8_UNORM; 63 case 2: 64 return NV50_SURFACE_FORMAT_R16_UNORM; 65 case 4: 66 return NV50_SURFACE_FORMAT_BGRA8_UNORM; 67 default: 68 return 0; 69 } 70} 71 72static int 73nv50_2d_texture_set(struct nouveau_pushbuf *push, int dst, 74 struct nv50_miptree *mt, unsigned level, unsigned layer) 75{ 76 struct nouveau_bo *bo = mt->base.bo; 77 uint32_t width, height, depth; 78 uint32_t format; 79 uint32_t mthd = dst ? NV50_2D_DST_FORMAT : NV50_2D_SRC_FORMAT; 80 uint32_t offset = mt->level[level].offset; 81 82 format = nv50_2d_format(mt->base.base.format); 83 if (!format) { 84 NOUVEAU_ERR("invalid/unsupported surface format: %s\n", 85 util_format_name(mt->base.base.format)); 86 return 1; 87 } 88 89 width = u_minify(mt->base.base.width0, level) << mt->ms_x; 90 height = u_minify(mt->base.base.height0, level) << mt->ms_y; 91 92 offset = mt->level[level].offset; 93 if (!mt->layout_3d) { 94 offset += mt->layer_stride * layer; 95 depth = 1; 96 layer = 0; 97 } else { 98 depth = u_minify(mt->base.base.depth0, level); 99 } 100 101 if (!nouveau_bo_memtype(bo)) { 102 BEGIN_NV04(push, SUBC_2D(mthd), 2); 103 PUSH_DATA (push, format); 104 PUSH_DATA (push, 1); 105 BEGIN_NV04(push, SUBC_2D(mthd + 0x14), 5); 106 PUSH_DATA (push, mt->level[level].pitch); 107 PUSH_DATA (push, width); 108 PUSH_DATA (push, height); 109 PUSH_DATAh(push, bo->offset + offset); 110 PUSH_DATA (push, bo->offset + offset); 111 } else { 112 BEGIN_NV04(push, SUBC_2D(mthd), 5); 113 PUSH_DATA (push, format); 114 PUSH_DATA (push, 0); 115 PUSH_DATA (push, mt->level[level].tile_mode); 116 PUSH_DATA (push, depth); 117 PUSH_DATA (push, layer); 118 BEGIN_NV04(push, SUBC_2D(mthd + 0x18), 4); 119 PUSH_DATA (push, width); 120 PUSH_DATA (push, height); 121 PUSH_DATAh(push, bo->offset + offset); 122 PUSH_DATA (push, bo->offset + offset); 123 } 124 125#if 0 126 if (dst) { 127 BEGIN_NV04(push, SUBC_2D(NV50_2D_CLIP_X), 4); 128 PUSH_DATA (push, 0); 129 PUSH_DATA (push, 0); 130 PUSH_DATA (push, width); 131 PUSH_DATA (push, height); 132 } 133#endif 134 return 0; 135} 136 137static int 138nv50_2d_texture_do_copy(struct nouveau_pushbuf *push, 139 struct nv50_miptree *dst, unsigned dst_level, 140 unsigned dx, unsigned dy, unsigned dz, 141 struct nv50_miptree *src, unsigned src_level, 142 unsigned sx, unsigned sy, unsigned sz, 143 unsigned w, unsigned h) 144{ 145 static const uint32_t duvdxy[5] = 146 { 147 0x40000000, 0x80000000, 0x00000001, 0x00000002, 0x00000004 148 }; 149 150 int ret; 151 uint32_t ctrl; 152#if 0 153 ret = MARK_RING(chan, 2 * 16 + 32, 4); 154 if (ret) 155 return ret; 156#endif 157 ret = nv50_2d_texture_set(push, 1, dst, dst_level, dz); 158 if (ret) 159 return ret; 160 161 ret = nv50_2d_texture_set(push, 0, src, src_level, sz); 162 if (ret) 163 return ret; 164 165 /* NOTE: 2D engine doesn't work for MS8 */ 166 if (src->ms_x) 167 ctrl = 0x11; 168 169 /* 0/1 = CENTER/CORNER, 00/10 = POINT/BILINEAR */ 170 BEGIN_NV04(push, NV50_2D(BLIT_CONTROL), 1); 171 PUSH_DATA (push, ctrl); 172 BEGIN_NV04(push, NV50_2D(BLIT_DST_X), 4); 173 PUSH_DATA (push, dx << dst->ms_x); 174 PUSH_DATA (push, dy << dst->ms_y); 175 PUSH_DATA (push, w << dst->ms_x); 176 PUSH_DATA (push, h << dst->ms_y); 177 BEGIN_NV04(push, NV50_2D(BLIT_DU_DX_FRACT), 4); 178 PUSH_DATA (push, duvdxy[2 + ((int)src->ms_x - (int)dst->ms_x)] & 0xf0000000); 179 PUSH_DATA (push, duvdxy[2 + ((int)src->ms_x - (int)dst->ms_x)] & 0x0000000f); 180 PUSH_DATA (push, duvdxy[2 + ((int)src->ms_y - (int)dst->ms_y)] & 0xf0000000); 181 PUSH_DATA (push, duvdxy[2 + ((int)src->ms_y - (int)dst->ms_y)] & 0x0000000f); 182 BEGIN_NV04(push, NV50_2D(BLIT_SRC_X_FRACT), 4); 183 PUSH_DATA (push, 0); 184 PUSH_DATA (push, sx << src->ms_x); 185 PUSH_DATA (push, 0); 186 PUSH_DATA (push, sy << src->ms_y); 187 188 return 0; 189} 190 191static void 192nv50_resource_copy_region(struct pipe_context *pipe, 193 struct pipe_resource *dst, unsigned dst_level, 194 unsigned dstx, unsigned dsty, unsigned dstz, 195 struct pipe_resource *src, unsigned src_level, 196 const struct pipe_box *src_box) 197{ 198 struct nv50_context *nv50 = nv50_context(pipe); 199 int ret; 200 boolean m2mf; 201 unsigned dst_layer = dstz, src_layer = src_box->z; 202 203 /* Fallback for buffers. */ 204 if (dst->target == PIPE_BUFFER && src->target == PIPE_BUFFER) { 205 util_resource_copy_region(pipe, dst, dst_level, dstx, dsty, dstz, 206 src, src_level, src_box); 207 return; 208 } 209 210 /* 0 and 1 are equal, only supporting 0/1, 2, 4 and 8 */ 211 assert((src->nr_samples | 1) == (dst->nr_samples | 1)); 212 213 m2mf = (src->format == dst->format) || 214 (util_format_get_blocksizebits(src->format) == 215 util_format_get_blocksizebits(dst->format)); 216 217 nv04_resource(dst)->status |= NOUVEAU_BUFFER_STATUS_GPU_WRITING; 218 219 if (m2mf) { 220 struct nv50_m2mf_rect drect, srect; 221 unsigned i; 222 unsigned nx = util_format_get_nblocksx(src->format, src_box->width); 223 unsigned ny = util_format_get_nblocksy(src->format, src_box->height); 224 225 nv50_m2mf_rect_setup(&drect, dst, dst_level, dstx, dsty, dstz); 226 nv50_m2mf_rect_setup(&srect, src, src_level, 227 src_box->x, src_box->y, src_box->z); 228 229 for (i = 0; i < src_box->depth; ++i) { 230 nv50_m2mf_transfer_rect(nv50, &drect, &srect, nx, ny); 231 232 if (nv50_miptree(dst)->layout_3d) 233 drect.z++; 234 else 235 drect.base += nv50_miptree(dst)->layer_stride; 236 237 if (nv50_miptree(src)->layout_3d) 238 srect.z++; 239 else 240 srect.base += nv50_miptree(src)->layer_stride; 241 } 242 return; 243 } 244 245 assert((src->format == dst->format) || 246 (nv50_2d_format_faithful(src->format) && 247 nv50_2d_format_faithful(dst->format))); 248 249 BCTX_REFN(nv50->bufctx, 2D, nv04_resource(src), RD); 250 BCTX_REFN(nv50->bufctx, 2D, nv04_resource(dst), WR); 251 nouveau_pushbuf_bufctx(nv50->base.pushbuf, nv50->bufctx); 252 nouveau_pushbuf_validate(nv50->base.pushbuf); 253 254 for (; dst_layer < dstz + src_box->depth; ++dst_layer, ++src_layer) { 255 ret = nv50_2d_texture_do_copy(nv50->base.pushbuf, 256 nv50_miptree(dst), dst_level, 257 dstx, dsty, dst_layer, 258 nv50_miptree(src), src_level, 259 src_box->x, src_box->y, src_layer, 260 src_box->width, src_box->height); 261 if (ret) 262 break; 263 } 264 nouveau_bufctx_reset(nv50->bufctx, NV50_BIND_2D); 265} 266 267static void 268nv50_clear_render_target(struct pipe_context *pipe, 269 struct pipe_surface *dst, 270 const union pipe_color_union *color, 271 unsigned dstx, unsigned dsty, 272 unsigned width, unsigned height) 273{ 274 struct nv50_context *nv50 = nv50_context(pipe); 275 struct nouveau_pushbuf *push = nv50->base.pushbuf; 276 struct nv50_miptree *mt = nv50_miptree(dst->texture); 277 struct nv50_surface *sf = nv50_surface(dst); 278 struct nouveau_bo *bo = mt->base.bo; 279 280 BEGIN_NV04(push, NV50_3D(CLEAR_COLOR(0)), 4); 281 PUSH_DATAf(push, color->f[0]); 282 PUSH_DATAf(push, color->f[1]); 283 PUSH_DATAf(push, color->f[2]); 284 PUSH_DATAf(push, color->f[3]); 285#if 0 286 if (MARK_RING(chan, 18, 2)) 287 return; 288#endif 289 BEGIN_NV04(push, NV50_3D(RT_CONTROL), 1); 290 PUSH_DATA (push, 1); 291 BEGIN_NV04(push, NV50_3D(RT_ADDRESS_HIGH(0)), 5); 292 PUSH_DATAh(push, bo->offset + sf->offset); 293 PUSH_DATA (push, bo->offset + sf->offset); 294 PUSH_DATA (push, nv50_format_table[dst->format].rt); 295 PUSH_DATA (push, mt->level[sf->base.u.tex.level].tile_mode); 296 PUSH_DATA (push, 0); 297 BEGIN_NV04(push, NV50_3D(RT_HORIZ(0)), 2); 298 if (nouveau_bo_memtype(bo)) 299 PUSH_DATA(push, sf->width); 300 else 301 PUSH_DATA(push, NV50_3D_RT_HORIZ_LINEAR | mt->level[0].pitch); 302 PUSH_DATA (push, sf->height); 303 BEGIN_NV04(push, NV50_3D(RT_ARRAY_MODE), 1); 304 PUSH_DATA (push, 1); 305 306 if (!nouveau_bo_memtype(bo)) { 307 BEGIN_NV04(push, NV50_3D(ZETA_ENABLE), 1); 308 PUSH_DATA (push, 0); 309 } 310 311 /* NOTE: only works with D3D clear flag (5097/0x143c bit 4) */ 312 313 BEGIN_NV04(push, NV50_3D(VIEWPORT_HORIZ(0)), 2); 314 PUSH_DATA (push, (width << 16) | dstx); 315 PUSH_DATA (push, (height << 16) | dsty); 316 317 BEGIN_NV04(push, NV50_3D(CLEAR_BUFFERS), 1); 318 PUSH_DATA (push, 0x3c); 319 320 nv50->dirty |= NV50_NEW_FRAMEBUFFER; 321} 322 323static void 324nv50_clear_depth_stencil(struct pipe_context *pipe, 325 struct pipe_surface *dst, 326 unsigned clear_flags, 327 double depth, 328 unsigned stencil, 329 unsigned dstx, unsigned dsty, 330 unsigned width, unsigned height) 331{ 332 struct nv50_context *nv50 = nv50_context(pipe); 333 struct nouveau_pushbuf *push = nv50->base.pushbuf; 334 struct nv50_miptree *mt = nv50_miptree(dst->texture); 335 struct nv50_surface *sf = nv50_surface(dst); 336 struct nouveau_bo *bo = mt->base.bo; 337 uint32_t mode = 0; 338 339 assert(nouveau_bo_memtype(bo)); /* ZETA cannot be linear */ 340 341 if (clear_flags & PIPE_CLEAR_DEPTH) { 342 BEGIN_NV04(push, NV50_3D(CLEAR_DEPTH), 1); 343 PUSH_DATAf(push, depth); 344 mode |= NV50_3D_CLEAR_BUFFERS_Z; 345 } 346 347 if (clear_flags & PIPE_CLEAR_STENCIL) { 348 BEGIN_NV04(push, NV50_3D(CLEAR_STENCIL), 1); 349 PUSH_DATA (push, stencil & 0xff); 350 mode |= NV50_3D_CLEAR_BUFFERS_S; 351 } 352#if 0 353 if (MARK_RING(chan, 17, 2)) 354 return; 355#endif 356 BEGIN_NV04(push, NV50_3D(ZETA_ADDRESS_HIGH), 5); 357 PUSH_DATAh(push, bo->offset + sf->offset); 358 PUSH_DATA (push, bo->offset + sf->offset); 359 PUSH_DATA (push, nv50_format_table[dst->format].rt); 360 PUSH_DATA (push, mt->level[sf->base.u.tex.level].tile_mode); 361 PUSH_DATA (push, 0); 362 BEGIN_NV04(push, NV50_3D(ZETA_ENABLE), 1); 363 PUSH_DATA (push, 1); 364 BEGIN_NV04(push, NV50_3D(ZETA_HORIZ), 3); 365 PUSH_DATA (push, sf->width); 366 PUSH_DATA (push, sf->height); 367 PUSH_DATA (push, (1 << 16) | 1); 368 369 BEGIN_NV04(push, NV50_3D(VIEWPORT_HORIZ(0)), 2); 370 PUSH_DATA (push, (width << 16) | dstx); 371 PUSH_DATA (push, (height << 16) | dsty); 372 373 BEGIN_NV04(push, NV50_3D(CLEAR_BUFFERS), 1); 374 PUSH_DATA (push, mode); 375 376 nv50->dirty |= NV50_NEW_FRAMEBUFFER; 377} 378 379void 380nv50_clear(struct pipe_context *pipe, unsigned buffers, 381 const union pipe_color_union *color, 382 double depth, unsigned stencil) 383{ 384 struct nv50_context *nv50 = nv50_context(pipe); 385 struct nouveau_pushbuf *push = nv50->base.pushbuf; 386 struct pipe_framebuffer_state *fb = &nv50->framebuffer; 387 unsigned i; 388 uint32_t mode = 0; 389 390 /* don't need NEW_BLEND, COLOR_MASK doesn't affect CLEAR_BUFFERS */ 391 if (!nv50_state_validate(nv50, NV50_NEW_FRAMEBUFFER, 9 + (fb->nr_cbufs * 2))) 392 return; 393 394 if (buffers & PIPE_CLEAR_COLOR && fb->nr_cbufs) { 395 BEGIN_NV04(push, NV50_3D(CLEAR_COLOR(0)), 4); 396 PUSH_DATAf(push, color->f[0]); 397 PUSH_DATAf(push, color->f[1]); 398 PUSH_DATAf(push, color->f[2]); 399 PUSH_DATAf(push, color->f[3]); 400 mode = 401 NV50_3D_CLEAR_BUFFERS_R | NV50_3D_CLEAR_BUFFERS_G | 402 NV50_3D_CLEAR_BUFFERS_B | NV50_3D_CLEAR_BUFFERS_A; 403 } 404 405 if (buffers & PIPE_CLEAR_DEPTH) { 406 BEGIN_NV04(push, NV50_3D(CLEAR_DEPTH), 1); 407 PUSH_DATA (push, fui(depth)); 408 mode |= NV50_3D_CLEAR_BUFFERS_Z; 409 } 410 411 if (buffers & PIPE_CLEAR_STENCIL) { 412 BEGIN_NV04(push, NV50_3D(CLEAR_STENCIL), 1); 413 PUSH_DATA (push, stencil & 0xff); 414 mode |= NV50_3D_CLEAR_BUFFERS_S; 415 } 416 417 BEGIN_NV04(push, NV50_3D(CLEAR_BUFFERS), 1); 418 PUSH_DATA (push, mode); 419 420 for (i = 1; i < fb->nr_cbufs; i++) { 421 BEGIN_NV04(push, NV50_3D(CLEAR_BUFFERS), 1); 422 PUSH_DATA (push, (i << 6) | 0x3c); 423 } 424} 425 426 427struct nv50_blitctx 428{ 429 struct nv50_screen *screen; 430 struct { 431 struct pipe_framebuffer_state fb; 432 struct nv50_program *vp; 433 struct nv50_program *gp; 434 struct nv50_program *fp; 435 unsigned num_textures[3]; 436 unsigned num_samplers[3]; 437 struct pipe_sampler_view *texture[2]; 438 struct nv50_tsc_entry *sampler[2]; 439 unsigned dirty; 440 } saved; 441 struct nv50_program vp; 442 struct nv50_program fp; 443 struct nv50_tsc_entry sampler[2]; /* nearest, bilinear */ 444 uint32_t fp_offset; 445 uint16_t color_mask; 446 uint8_t filter; 447}; 448 449static void 450nv50_blitctx_make_vp(struct nv50_blitctx *blit) 451{ 452 static const uint32_t code[] = 453 { 454 0x10000001, /* mov b32 o[0x00] s[0x00] */ /* HPOS.x */ 455 0x0423c788, 456 0x10000205, /* mov b32 o[0x04] s[0x04] */ /* HPOS.y */ 457 0x0423c788, 458 0x10000409, /* mov b32 o[0x08] s[0x08] */ /* TEXC.x */ 459 0x0423c788, 460 0x1000060d, /* mov b32 o[0x0c] s[0x0c] */ /* TEXC.y */ 461 0x0423c788, 462 0x10000811, /* exit mov b32 o[0x10] s[0x10] */ /* TEXC.z */ 463 0x0423c789, 464 }; 465 466 blit->vp.type = PIPE_SHADER_VERTEX; 467 blit->vp.translated = TRUE; 468 blit->vp.code = (uint32_t *)code; /* const_cast */ 469 blit->vp.code_size = sizeof(code); 470 blit->vp.max_gpr = 4; 471 blit->vp.max_out = 5; 472 blit->vp.out_nr = 2; 473 blit->vp.out[0].mask = 0x3; 474 blit->vp.out[0].sn = TGSI_SEMANTIC_POSITION; 475 blit->vp.out[1].hw = 2; 476 blit->vp.out[1].mask = 0x7; 477 blit->vp.out[1].sn = TGSI_SEMANTIC_GENERIC; 478 blit->vp.vp.attrs[0] = 0x73; 479 blit->vp.vp.psiz = 0x40; 480 blit->vp.vp.edgeflag = 0x40; 481} 482 483static void 484nv50_blitctx_make_fp(struct nv50_blitctx *blit) 485{ 486 static const uint32_t code[] = 487 { 488 /* 3 coords RGBA in, RGBA out, also for Z32_FLOAT(_S8X24_UINT) */ 489 0x80000000, /* interp $r0 v[0x0] */ 490 0x80010004, /* interp $r1 v[0x4] */ 491 0x80020009, /* interp $r2 flat v[0x8] */ 492 0x00040780, 493 0xf6800001, /* texauto live { $r0,1,2,3 } $t0 $s0 { $r0,1,2 } */ 494 0x0000c785, /* exit */ 495 496 /* 3 coords ZS in, S encoded in R, Z encoded in GBA (8_UNORM) */ 497 0x80000000, /* interp $r0 v[0x00] */ 498 0x80010004, /* interp $r1 v[0x04] */ 499 0x80020108, /* interp $r2 flat v[0x8] */ 500 0x10008010, /* mov b32 $r4 $r0 */ 501 0xf2820201, /* texauto live { $r0,#,#,# } $t1 $s1 { $r0,1,2 } */ 502 0x00000784, 503 0xa000000d, /* cvt f32 $r3 s32 $r0 */ 504 0x44014780, 505 0x10000801, /* mov b32 $r0 $r4 */ 506 0x0403c780, 507 0xf2800001, /* texauto live { $r0,#,#,# } $t0 $s0 { $r0,1,2 } */ 508 0x00000784, 509 0xc03f0009, /* mul f32 $r2 $r0 (2^24 - 1) */ 510 0x04b7ffff, 511 0xa0000409, /* cvt rni s32 $r2 f32 $r2 */ 512 0x8c004780, 513 0xc0010601, /* mul f32 $r0 $r3 1/0xff */ 514 0x03b8080b, 515 0xd03f0405, /* and b32 $r1 $r2 0x0000ff */ 516 0x0000000f, 517 0xd000040d, /* and b32 $r3 $r2 0xff0000 */ 518 0x000ff003, 519 0xd0000409, /* and b32 $r2 $r2 0x00ff00 */ 520 0x00000ff3, 521 0xa0000205, /* cvt f32 $r1 s32 $r1 */ 522 0x44014780, 523 0xa000060d, /* cvt f32 $r3 s32 $r3 */ 524 0x44014780, 525 0xa0000409, /* cvt f32 $r2 s32 $r2 */ 526 0x44014780, 527 0xc0010205, /* mul f32 $r1 $r1 1/0x0000ff */ 528 0x03b8080b, 529 0xc001060d, /* mul f32 $r3 $r3 1/0x00ff00 */ 530 0x0338080b, 531 0xc0010409, /* mul f32 $r2 $r2 1/0xff0000 */ 532 0x0378080b, 533 0xf0000001, /* exit never nop */ 534 0xe0000001, 535 536 /* 3 coords ZS in, Z encoded in RGB, S encoded in A (U8_UNORM) */ 537 0x80000000, /* interp $r0 v[0x00] */ 538 0x80010004, /* interp $r1 v[0x04] */ 539 0x80020108, /* interp $r2 flat v[0x8] */ 540 0x10008010, /* mov b32 $r4 $r0 */ 541 0xf2820201, /* texauto live { $r0,#,#,# } $t1 $s1 { $r0,1,2 } */ 542 0x00000784, 543 0xa000000d, /* cvt f32 $r3 s32 $r0 */ 544 0x44014780, 545 0x10000801, /* mov b32 $r0 $r4 */ 546 0x0403c780, 547 0xf2800001, /* texauto live { $r0,#,#,# } $t0 $s0 { $r0,1,2 } */ 548 0x00000784, 549 0xc03f0009, /* mul f32 $r2 $r0 (2^24 - 1) */ 550 0x04b7ffff, 551 0xa0000409, /* cvt rni s32 $r2 f32 $r2 */ 552 0x8c004780, 553 0xc001060d, /* mul f32 $r3 $r3 1/0xff */ 554 0x03b8080b, 555 0xd03f0401, /* and b32 $r0 $r2 0x0000ff */ 556 0x0000000f, 557 0xd0000405, /* and b32 $r1 $r2 0x00ff00 */ 558 0x00000ff3, 559 0xd0000409, /* and b32 $r2 $r2 0xff0000 */ 560 0x000ff003, 561 0xa0000001, /* cvt f32 $r0 s32 $r0 */ 562 0x44014780, 563 0xa0000205, /* cvt f32 $r1 s32 $r1 */ 564 0x44014780, 565 0xa0000409, /* cvt f32 $r2 s32 $r2 */ 566 0x44014780, 567 0xc0010001, /* mul f32 $r0 $r0 1/0x0000ff */ 568 0x03b8080b, 569 0xc0010205, /* mul f32 $r1 $r1 1/0x00ff00 */ 570 0x0378080b, 571 0xc0010409, /* mul f32 $r2 $r2 1/0xff0000 */ 572 0x0338080b, 573 0xf0000001, /* exit never nop */ 574 0xe0000001 575 }; 576 577 blit->fp.type = PIPE_SHADER_FRAGMENT; 578 blit->fp.translated = TRUE; 579 blit->fp.code = (uint32_t *)code; /* const_cast */ 580 blit->fp.code_size = sizeof(code); 581 blit->fp.max_gpr = 5; 582 blit->fp.max_out = 4; 583 blit->fp.in_nr = 1; 584 blit->fp.in[0].mask = 0x7; /* last component flat */ 585 blit->fp.in[0].linear = 1; 586 blit->fp.in[0].sn = TGSI_SEMANTIC_GENERIC; 587 blit->fp.out_nr = 1; 588 blit->fp.out[0].mask = 0xf; 589 blit->fp.out[0].sn = TGSI_SEMANTIC_COLOR; 590 blit->fp.fp.interp = 0x00020403; 591 blit->fp.gp.primid = 0x80; 592} 593 594static void 595nv50_blitctx_make_sampler(struct nv50_blitctx *blit) 596{ 597 /* clamp to edge, min/max lod = 0, nearest filtering */ 598 599 blit->sampler[0].id = -1; 600 601 blit->sampler[0].tsc[0] = 0x00000092; 602 blit->sampler[0].tsc[1] = 0x00000051; 603 604 /* clamp to edge, min/max lod = 0, bilinear filtering */ 605 606 blit->sampler[1].id = -1; 607 608 blit->sampler[1].tsc[0] = 0x00000092; 609 blit->sampler[1].tsc[1] = 0x00000062; 610} 611 612/* Since shaders cannot export stencil, we cannot copy stencil values when 613 * rendering to ZETA, so we attach the ZS surface to a colour render target. 614 */ 615static INLINE enum pipe_format 616nv50_blit_zeta_to_colour_format(enum pipe_format format) 617{ 618 switch (format) { 619 case PIPE_FORMAT_Z16_UNORM: return PIPE_FORMAT_R16_UNORM; 620 case PIPE_FORMAT_Z24_UNORM_S8_UINT: 621 case PIPE_FORMAT_S8_UINT_Z24_UNORM: 622 case PIPE_FORMAT_Z24X8_UNORM: return PIPE_FORMAT_R8G8B8A8_UNORM; 623 case PIPE_FORMAT_Z32_FLOAT: return PIPE_FORMAT_R32_FLOAT; 624 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT: return PIPE_FORMAT_R32G32_FLOAT; 625 default: 626 assert(0); 627 return PIPE_FORMAT_NONE; 628 } 629} 630 631static void 632nv50_blitctx_get_color_mask_and_fp(struct nv50_blitctx *blit, 633 enum pipe_format format, uint8_t mask) 634{ 635 blit->color_mask = 0; 636 637 switch (format) { 638 case PIPE_FORMAT_Z24X8_UNORM: 639 case PIPE_FORMAT_Z24_UNORM_S8_UINT: 640 blit->fp_offset = 160; 641 if (mask & PIPE_MASK_Z) 642 blit->color_mask |= 0x0111; 643 if (mask & PIPE_MASK_S) 644 blit->color_mask |= 0x1000; 645 break; 646 case PIPE_FORMAT_S8_UINT_Z24_UNORM: 647 blit->fp_offset = 24; 648 if (mask & PIPE_MASK_Z) 649 blit->color_mask |= 0x1110; 650 if (mask & PIPE_MASK_S) 651 blit->color_mask |= 0x0001; 652 break; 653 default: 654 blit->fp_offset = 0; 655 if (mask & (PIPE_MASK_R | PIPE_MASK_Z)) blit->color_mask |= 0x0001; 656 if (mask & (PIPE_MASK_G | PIPE_MASK_S)) blit->color_mask |= 0x0010; 657 if (mask & PIPE_MASK_B) blit->color_mask |= 0x0100; 658 if (mask & PIPE_MASK_A) blit->color_mask |= 0x1000; 659 break; 660 } 661} 662 663static void 664nv50_blit_set_dst(struct nv50_context *nv50, 665 struct pipe_resource *res, unsigned level, unsigned layer) 666{ 667 struct pipe_context *pipe = &nv50->base.pipe; 668 struct pipe_surface templ; 669 670 if (util_format_is_depth_or_stencil(res->format)) 671 templ.format = nv50_blit_zeta_to_colour_format(res->format); 672 else 673 templ.format = res->format; 674 675 templ.usage = PIPE_USAGE_STREAM; 676 templ.u.tex.level = level; 677 templ.u.tex.first_layer = templ.u.tex.last_layer = layer; 678 679 nv50->framebuffer.cbufs[0] = nv50_miptree_surface_new(pipe, res, &templ); 680 nv50->framebuffer.nr_cbufs = 1; 681 nv50->framebuffer.zsbuf = NULL; 682 nv50->framebuffer.width = nv50->framebuffer.cbufs[0]->width; 683 nv50->framebuffer.height = nv50->framebuffer.cbufs[0]->height; 684} 685 686static INLINE void 687nv50_blit_fixup_tic_entry(struct pipe_sampler_view *view) 688{ 689 struct nv50_tic_entry *ent = nv50_tic_entry(view); 690 691 ent->tic[2] &= ~(1 << 31); /* scaled coordinates, ok with 3d textures ? */ 692 693 /* magic: */ 694 695 ent->tic[3] = 0x20000000; /* affects quality of near vertical edges in MS8 */ 696} 697 698static void 699nv50_blit_set_src(struct nv50_context *nv50, 700 struct pipe_resource *res, unsigned level, unsigned layer) 701{ 702 struct pipe_context *pipe = &nv50->base.pipe; 703 struct pipe_sampler_view templ; 704 705 templ.format = res->format; 706 templ.u.tex.first_layer = templ.u.tex.last_layer = layer; 707 templ.u.tex.first_level = templ.u.tex.last_level = level; 708 templ.swizzle_r = PIPE_SWIZZLE_RED; 709 templ.swizzle_g = PIPE_SWIZZLE_GREEN; 710 templ.swizzle_b = PIPE_SWIZZLE_BLUE; 711 templ.swizzle_a = PIPE_SWIZZLE_ALPHA; 712 713 nv50->textures[2][0] = nv50_create_sampler_view(pipe, res, &templ); 714 nv50->textures[2][0] = NULL; 715 716 nv50_blit_fixup_tic_entry(nv50->textures[2][0]); 717 718 nv50->num_textures[0] = nv50->num_textures[1] = 0; 719 nv50->num_textures[2] = 1; 720 721 templ.format = nv50_zs_to_s_format(res->format); 722 if (templ.format != res->format) { 723 nv50->textures[2][1] = nv50_create_sampler_view(pipe, res, &templ); 724 nv50_blit_fixup_tic_entry(nv50->textures[2][1]); 725 nv50->num_textures[2] = 2; 726 } 727} 728 729static void 730nv50_blitctx_prepare_state(struct nv50_blitctx *blit) 731{ 732 struct nouveau_pushbuf *push = blit->screen->base.pushbuf; 733 734 /* blend state */ 735 BEGIN_NV04(push, NV50_3D(COLOR_MASK(0)), 1); 736 PUSH_DATA (push, blit->color_mask); 737 BEGIN_NV04(push, NV50_3D(BLEND_ENABLE(0)), 1); 738 PUSH_DATA (push, 0); 739 BEGIN_NV04(push, NV50_3D(LOGIC_OP_ENABLE), 1); 740 PUSH_DATA (push, 0); 741 742 /* rasterizer state */ 743#ifndef NV50_SCISSORS_CLIPPING 744 BEGIN_NV04(push, NV50_3D(SCISSOR_ENABLE(0)), 1); 745 PUSH_DATA (push, 1); 746#endif 747 BEGIN_NV04(push, NV50_3D(VERTEX_TWO_SIDE_ENABLE), 1); 748 PUSH_DATA (push, 0); 749 BEGIN_NV04(push, NV50_3D(FRAG_COLOR_CLAMP_EN), 1); 750 PUSH_DATA (push, 0); 751 BEGIN_NV04(push, NV50_3D(MULTISAMPLE_ENABLE), 1); 752 PUSH_DATA (push, 0); 753 BEGIN_NV04(push, NV50_3D(MSAA_MASK(0)), 4); 754 PUSH_DATA (push, 0xffff); 755 PUSH_DATA (push, 0xffff); 756 PUSH_DATA (push, 0xffff); 757 PUSH_DATA (push, 0xffff); 758 BEGIN_NV04(push, NV50_3D(POLYGON_MODE_FRONT), 3); 759 PUSH_DATA (push, NV50_3D_POLYGON_MODE_FRONT_FILL); 760 PUSH_DATA (push, NV50_3D_POLYGON_MODE_BACK_FILL); 761 PUSH_DATA (push, 0); 762 BEGIN_NV04(push, NV50_3D(CULL_FACE_ENABLE), 1); 763 PUSH_DATA (push, 0); 764 BEGIN_NV04(push, NV50_3D(POLYGON_STIPPLE_ENABLE), 1); 765 PUSH_DATA (push, 0); 766 BEGIN_NV04(push, NV50_3D(POLYGON_OFFSET_FILL_ENABLE), 1); 767 PUSH_DATA (push, 0); 768 769 /* zsa state */ 770 BEGIN_NV04(push, NV50_3D(DEPTH_TEST_ENABLE), 1); 771 PUSH_DATA (push, 0); 772 BEGIN_NV04(push, NV50_3D(STENCIL_ENABLE), 1); 773 PUSH_DATA (push, 0); 774 BEGIN_NV04(push, NV50_3D(ALPHA_TEST_ENABLE), 1); 775 PUSH_DATA (push, 0); 776} 777 778static void 779nv50_blitctx_pre_blit(struct nv50_blitctx *blit, struct nv50_context *nv50) 780{ 781 int s; 782 783 blit->saved.fb.width = nv50->framebuffer.width; 784 blit->saved.fb.height = nv50->framebuffer.height; 785 blit->saved.fb.nr_cbufs = nv50->framebuffer.nr_cbufs; 786 blit->saved.fb.cbufs[0] = nv50->framebuffer.cbufs[0]; 787 blit->saved.fb.zsbuf = nv50->framebuffer.zsbuf; 788 789 blit->saved.vp = nv50->vertprog; 790 blit->saved.gp = nv50->gmtyprog; 791 blit->saved.fp = nv50->fragprog; 792 793 nv50->vertprog = &blit->vp; 794 nv50->gmtyprog = NULL; 795 nv50->fragprog = &blit->fp; 796 797 for (s = 0; s < 3; ++s) { 798 blit->saved.num_textures[s] = nv50->num_textures[s]; 799 blit->saved.num_samplers[s] = nv50->num_samplers[s]; 800 } 801 blit->saved.texture[0] = nv50->textures[2][0]; 802 blit->saved.texture[1] = nv50->textures[2][1]; 803 blit->saved.sampler[0] = nv50->samplers[2][0]; 804 blit->saved.sampler[1] = nv50->samplers[2][1]; 805 806 nv50->samplers[2][0] = &blit->sampler[blit->filter]; 807 nv50->samplers[2][1] = &blit->sampler[blit->filter]; 808 809 nv50->num_samplers[0] = nv50->num_samplers[1] = 0; 810 nv50->num_samplers[2] = 2; 811 812 blit->saved.dirty = nv50->dirty; 813 814 nv50->dirty = 815 NV50_NEW_FRAMEBUFFER | 816 NV50_NEW_VERTPROG | NV50_NEW_FRAGPROG | NV50_NEW_GMTYPROG | 817 NV50_NEW_TEXTURES | NV50_NEW_SAMPLERS; 818} 819 820static void 821nv50_blitctx_post_blit(struct nv50_context *nv50, struct nv50_blitctx *blit) 822{ 823 int s; 824 825 pipe_surface_reference(&nv50->framebuffer.cbufs[0], NULL); 826 827 nv50->framebuffer.width = blit->saved.fb.width; 828 nv50->framebuffer.height = blit->saved.fb.height; 829 nv50->framebuffer.nr_cbufs = blit->saved.fb.nr_cbufs; 830 nv50->framebuffer.cbufs[0] = blit->saved.fb.cbufs[0]; 831 nv50->framebuffer.zsbuf = blit->saved.fb.zsbuf; 832 833 nv50->vertprog = blit->saved.vp; 834 nv50->gmtyprog = blit->saved.gp; 835 nv50->fragprog = blit->saved.fp; 836 837 pipe_sampler_view_reference(&nv50->textures[2][0], NULL); 838 pipe_sampler_view_reference(&nv50->textures[2][1], NULL); 839 840 for (s = 0; s < 3; ++s) { 841 nv50->num_textures[s] = blit->saved.num_textures[s]; 842 nv50->num_samplers[s] = blit->saved.num_samplers[s]; 843 } 844 nv50->textures[2][0] = blit->saved.texture[0]; 845 nv50->textures[2][1] = blit->saved.texture[1]; 846 nv50->samplers[2][0] = blit->saved.sampler[0]; 847 nv50->samplers[2][1] = blit->saved.sampler[1]; 848 849 nv50->dirty = blit->saved.dirty | 850 (NV50_NEW_FRAMEBUFFER | NV50_NEW_SCISSOR | NV50_NEW_SAMPLE_MASK | 851 NV50_NEW_RASTERIZER | NV50_NEW_ZSA | NV50_NEW_BLEND | 852 NV50_NEW_TEXTURES | NV50_NEW_SAMPLERS | 853 NV50_NEW_VERTPROG | NV50_NEW_GMTYPROG | NV50_NEW_FRAGPROG); 854} 855 856static void 857nv50_resource_resolve(struct pipe_context *pipe, 858 const struct pipe_resolve_info *info) 859{ 860 struct nv50_context *nv50 = nv50_context(pipe); 861 struct nv50_screen *screen = nv50->screen; 862 struct nv50_blitctx *blit = screen->blitctx; 863 struct nouveau_pushbuf *push = nv50->base.pushbuf; 864 struct pipe_resource *src = info->src.res; 865 struct pipe_resource *dst = info->dst.res; 866 float x0, x1, y0, y1, z; 867 float x_range, y_range; 868 869 nv50_blitctx_get_color_mask_and_fp(blit, dst->format, info->mask); 870 871 blit->filter = util_format_is_depth_or_stencil(dst->format) ? 0 : 1; 872 873 nv50_blitctx_pre_blit(blit, nv50); 874 875 nv50_blit_set_dst(nv50, dst, info->dst.level, info->dst.layer); 876 nv50_blit_set_src(nv50, src, 0, info->src.layer); 877 878 nv50_blitctx_prepare_state(blit); 879 880 nv50_state_validate(nv50, ~0, 36); 881 882 x_range = 883 (float)(info->src.x1 - info->src.x0) / 884 (float)(info->dst.x1 - info->dst.x0); 885 y_range = 886 (float)(info->src.y1 - info->src.y0) / 887 (float)(info->dst.y1 - info->dst.y0); 888 889 x0 = (float)info->src.x0 - x_range * (float)info->dst.x0; 890 y0 = (float)info->src.y0 - y_range * (float)info->dst.y0; 891 892 x1 = x0 + 16384.0f * x_range; 893 y1 = y0 + 16384.0f * y_range; 894 895 x0 *= (float)(1 << nv50_miptree(src)->ms_x); 896 x1 *= (float)(1 << nv50_miptree(src)->ms_x); 897 y0 *= (float)(1 << nv50_miptree(src)->ms_y); 898 y1 *= (float)(1 << nv50_miptree(src)->ms_y); 899 900 z = (float)info->src.layer; 901 902 BEGIN_NV04(push, NV50_3D(FP_START_ID), 1); 903 PUSH_DATA (push, 904 blit->fp.code_base + blit->fp_offset); 905 906 BEGIN_NV04(push, NV50_3D(VIEWPORT_TRANSFORM_EN), 1); 907 PUSH_DATA (push, 0); 908 909 /* Draw a large triangle in screen coordinates covering the whole 910 * render target, with scissors defining the destination region. 911 * The vertex is supplied with non-normalized texture coordinates 912 * arranged in a way to yield the desired offset and scale. 913 */ 914 915 BEGIN_NV04(push, NV50_3D(SCISSOR_HORIZ(0)), 2); 916 PUSH_DATA (push, (info->dst.x1 << 16) | info->dst.x0); 917 PUSH_DATA (push, (info->dst.y1 << 16) | info->dst.y0); 918 919 BEGIN_NV04(push, NV50_3D(VERTEX_BEGIN_GL), 1); 920 PUSH_DATA (push, NV50_3D_VERTEX_BEGIN_GL_PRIMITIVE_TRIANGLES); 921 BEGIN_NV04(push, NV50_3D(VTX_ATTR_3F_X(1)), 3); 922 PUSH_DATAf(push, x0); 923 PUSH_DATAf(push, y0); 924 PUSH_DATAf(push, z); 925 BEGIN_NV04(push, NV50_3D(VTX_ATTR_2F_X(0)), 2); 926 PUSH_DATAf(push, 0.0f); 927 PUSH_DATAf(push, 0.0f); 928 BEGIN_NV04(push, NV50_3D(VTX_ATTR_3F_X(1)), 3); 929 PUSH_DATAf(push, x1); 930 PUSH_DATAf(push, y0); 931 PUSH_DATAf(push, z); 932 BEGIN_NV04(push, NV50_3D(VTX_ATTR_2F_X(0)), 2); 933 PUSH_DATAf(push, 16384 << nv50_miptree(dst)->ms_x); 934 PUSH_DATAf(push, 0.0f); 935 BEGIN_NV04(push, NV50_3D(VTX_ATTR_3F_X(1)), 3); 936 PUSH_DATAf(push, x0); 937 PUSH_DATAf(push, y1); 938 PUSH_DATAf(push, z); 939 BEGIN_NV04(push, NV50_3D(VTX_ATTR_2F_X(0)), 2); 940 PUSH_DATAf(push, 0.0f); 941 PUSH_DATAf(push, 16384 << nv50_miptree(dst)->ms_y); 942 BEGIN_NV04(push, NV50_3D(VERTEX_END_GL), 1); 943 PUSH_DATA (push, 0); 944 945 /* re-enable normally constant state */ 946 947 BEGIN_NV04(push, NV50_3D(VIEWPORT_TRANSFORM_EN), 1); 948 PUSH_DATA (push, 1); 949 950 nv50_blitctx_post_blit(nv50, blit); 951} 952 953boolean 954nv50_blitctx_create(struct nv50_screen *screen) 955{ 956 screen->blitctx = CALLOC_STRUCT(nv50_blitctx); 957 if (!screen->blitctx) { 958 NOUVEAU_ERR("failed to allocate blit context\n"); 959 return FALSE; 960 } 961 962 screen->blitctx->screen = screen; 963 964 nv50_blitctx_make_vp(screen->blitctx); 965 nv50_blitctx_make_fp(screen->blitctx); 966 967 nv50_blitctx_make_sampler(screen->blitctx); 968 969 screen->blitctx->color_mask = 0x1111; 970 971 return TRUE; 972} 973 974void 975nv50_init_surface_functions(struct nv50_context *nv50) 976{ 977 struct pipe_context *pipe = &nv50->base.pipe; 978 979 pipe->resource_copy_region = nv50_resource_copy_region; 980 pipe->resource_resolve = nv50_resource_resolve; 981 pipe->clear_render_target = nv50_clear_render_target; 982 pipe->clear_depth_stencil = nv50_clear_depth_stencil; 983} 984 985 986