nv30: Implement color resolve for msaa
[mesa.git] / src / gallium / drivers / nouveau / nv30 / nv30_miptree.c
1 /*
2 * Copyright 2012 Red Hat Inc.
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 * Authors: Ben Skeggs
23 *
24 */
25
26 #include "util/u_format.h"
27 #include "util/u_inlines.h"
28 #include "util/u_surface.h"
29
30 #include "nv_m2mf.xml.h"
31 #include "nv_object.xml.h"
32 #include "nv30/nv30_screen.h"
33 #include "nv30/nv30_context.h"
34 #include "nv30/nv30_resource.h"
35 #include "nv30/nv30_transfer.h"
36
37 static inline unsigned
38 layer_offset(struct pipe_resource *pt, unsigned level, unsigned layer)
39 {
40 struct nv30_miptree *mt = nv30_miptree(pt);
41 struct nv30_miptree_level *lvl = &mt->level[level];
42
43 if (pt->target == PIPE_TEXTURE_CUBE)
44 return (layer * mt->layer_size) + lvl->offset;
45
46 return lvl->offset + (layer * lvl->zslice_size);
47 }
48
49 static boolean
50 nv30_miptree_get_handle(struct pipe_screen *pscreen,
51 struct pipe_resource *pt,
52 struct winsys_handle *handle)
53 {
54 struct nv30_miptree *mt = nv30_miptree(pt);
55 unsigned stride;
56
57 if (!mt || !mt->base.bo)
58 return false;
59
60 stride = mt->level[0].pitch;
61
62 return nouveau_screen_bo_get_handle(pscreen, mt->base.bo, stride, handle);
63 }
64
65 static void
66 nv30_miptree_destroy(struct pipe_screen *pscreen, struct pipe_resource *pt)
67 {
68 struct nv30_miptree *mt = nv30_miptree(pt);
69
70 nouveau_bo_ref(NULL, &mt->base.bo);
71 FREE(mt);
72 }
73
74 struct nv30_transfer {
75 struct pipe_transfer base;
76 struct nv30_rect img;
77 struct nv30_rect tmp;
78 unsigned nblocksx;
79 unsigned nblocksy;
80 };
81
82 static inline struct nv30_transfer *
83 nv30_transfer(struct pipe_transfer *ptx)
84 {
85 return (struct nv30_transfer *)ptx;
86 }
87
88 static inline void
89 define_rect(struct pipe_resource *pt, unsigned level, unsigned z,
90 unsigned x, unsigned y, unsigned w, unsigned h,
91 struct nv30_rect *rect)
92 {
93 struct nv30_miptree *mt = nv30_miptree(pt);
94 struct nv30_miptree_level *lvl = &mt->level[level];
95
96 rect->w = u_minify(pt->width0, level) << mt->ms_x;
97 rect->w = util_format_get_nblocksx(pt->format, rect->w);
98 rect->h = u_minify(pt->height0, level) << mt->ms_y;
99 rect->h = util_format_get_nblocksy(pt->format, rect->h);
100 rect->d = 1;
101 rect->z = 0;
102 if (mt->swizzled) {
103 if (pt->target == PIPE_TEXTURE_3D) {
104 rect->d = u_minify(pt->depth0, level);
105 rect->z = z; z = 0;
106 }
107 rect->pitch = 0;
108 } else {
109 rect->pitch = lvl->pitch;
110 }
111
112 rect->bo = mt->base.bo;
113 rect->domain = NOUVEAU_BO_VRAM;
114 rect->offset = layer_offset(pt, level, z);
115 rect->cpp = util_format_get_blocksize(pt->format);
116
117 rect->x0 = util_format_get_nblocksx(pt->format, x) << mt->ms_x;
118 rect->y0 = util_format_get_nblocksy(pt->format, y) << mt->ms_y;
119 rect->x1 = rect->x0 + (w << mt->ms_x);
120 rect->y1 = rect->y0 + (h << mt->ms_y);
121 }
122
123 void
124 nv30_resource_copy_region(struct pipe_context *pipe,
125 struct pipe_resource *dstres, unsigned dst_level,
126 unsigned dstx, unsigned dsty, unsigned dstz,
127 struct pipe_resource *srcres, unsigned src_level,
128 const struct pipe_box *src_box)
129 {
130 struct nv30_context *nv30 = nv30_context(pipe);
131 struct nv30_rect src, dst;
132
133 if (dstres->target == PIPE_BUFFER && srcres->target == PIPE_BUFFER) {
134 nouveau_copy_buffer(&nv30->base,
135 nv04_resource(dstres), dstx,
136 nv04_resource(srcres), src_box->x, src_box->width);
137 return;
138 }
139
140 define_rect(srcres, src_level, src_box->z, src_box->x, src_box->y,
141 src_box->width, src_box->height, &src);
142 define_rect(dstres, dst_level, dstz, dstx, dsty,
143 src_box->width, src_box->height, &dst);
144
145 nv30_transfer_rect(nv30, NEAREST, &src, &dst);
146 }
147
148 static void
149 nv30_resource_resolve(struct nv30_context *nv30,
150 const struct pipe_blit_info *info)
151 {
152 struct nv30_rect src, dst;
153
154 define_rect(info->src.resource, 0, info->src.box.z, info->src.box.x,
155 info->src.box.y, info->src.box.width, info->src.box.height, &src);
156 define_rect(info->dst.resource, 0, info->dst.box.z, info->dst.box.x,
157 info->dst.box.y, info->dst.box.width, info->dst.box.height, &dst);
158
159 nv30_transfer_rect(nv30, BILINEAR, &src, &dst);
160 }
161
162 void
163 nv30_blit(struct pipe_context *pipe,
164 const struct pipe_blit_info *blit_info)
165 {
166 struct nv30_context *nv30 = nv30_context(pipe);
167 struct pipe_blit_info info = *blit_info;
168
169 if (info.src.resource->nr_samples > 1 &&
170 info.dst.resource->nr_samples <= 1 &&
171 !util_format_is_depth_or_stencil(info.src.resource->format) &&
172 !util_format_is_pure_integer(info.src.resource->format)) {
173 nv30_resource_resolve(nv30, blit_info);
174 return;
175 }
176
177 if (util_try_blit_via_copy_region(pipe, &info)) {
178 return; /* done */
179 }
180
181 if (info.mask & PIPE_MASK_S) {
182 debug_printf("nv30: cannot blit stencil, skipping\n");
183 info.mask &= ~PIPE_MASK_S;
184 }
185
186 if (!util_blitter_is_blit_supported(nv30->blitter, &info)) {
187 debug_printf("nv30: blit unsupported %s -> %s\n",
188 util_format_short_name(info.src.resource->format),
189 util_format_short_name(info.dst.resource->format));
190 return;
191 }
192
193 /* XXX turn off occlusion queries */
194
195 util_blitter_save_vertex_buffer_slot(nv30->blitter, nv30->vtxbuf);
196 util_blitter_save_vertex_elements(nv30->blitter, nv30->vertex);
197 util_blitter_save_vertex_shader(nv30->blitter, nv30->vertprog.program);
198 util_blitter_save_rasterizer(nv30->blitter, nv30->rast);
199 util_blitter_save_viewport(nv30->blitter, &nv30->viewport);
200 util_blitter_save_scissor(nv30->blitter, &nv30->scissor);
201 util_blitter_save_fragment_shader(nv30->blitter, nv30->fragprog.program);
202 util_blitter_save_blend(nv30->blitter, nv30->blend);
203 util_blitter_save_depth_stencil_alpha(nv30->blitter,
204 nv30->zsa);
205 util_blitter_save_stencil_ref(nv30->blitter, &nv30->stencil_ref);
206 util_blitter_save_sample_mask(nv30->blitter, nv30->sample_mask);
207 util_blitter_save_framebuffer(nv30->blitter, &nv30->framebuffer);
208 util_blitter_save_fragment_sampler_states(nv30->blitter,
209 nv30->fragprog.num_samplers,
210 (void**)nv30->fragprog.samplers);
211 util_blitter_save_fragment_sampler_views(nv30->blitter,
212 nv30->fragprog.num_textures, nv30->fragprog.textures);
213 util_blitter_save_render_condition(nv30->blitter, nv30->render_cond_query,
214 nv30->render_cond_cond, nv30->render_cond_mode);
215 util_blitter_blit(nv30->blitter, &info);
216 }
217
218 void
219 nv30_flush_resource(struct pipe_context *pipe,
220 struct pipe_resource *resource)
221 {
222 }
223
224 static void *
225 nv30_miptree_transfer_map(struct pipe_context *pipe, struct pipe_resource *pt,
226 unsigned level, unsigned usage,
227 const struct pipe_box *box,
228 struct pipe_transfer **ptransfer)
229 {
230 struct nv30_context *nv30 = nv30_context(pipe);
231 struct nouveau_device *dev = nv30->screen->base.device;
232 struct nv30_transfer *tx;
233 unsigned access = 0;
234 int ret;
235
236 tx = CALLOC_STRUCT(nv30_transfer);
237 if (!tx)
238 return NULL;
239 pipe_resource_reference(&tx->base.resource, pt);
240 tx->base.level = level;
241 tx->base.usage = usage;
242 tx->base.box = *box;
243 tx->base.stride = align(util_format_get_nblocksx(pt->format, box->width) *
244 util_format_get_blocksize(pt->format), 64);
245 tx->base.layer_stride = util_format_get_nblocksy(pt->format, box->height) *
246 tx->base.stride;
247
248 tx->nblocksx = util_format_get_nblocksx(pt->format, box->width);
249 tx->nblocksy = util_format_get_nblocksy(pt->format, box->height);
250
251 define_rect(pt, level, box->z, box->x, box->y,
252 tx->nblocksx, tx->nblocksy, &tx->img);
253
254 ret = nouveau_bo_new(dev, NOUVEAU_BO_GART | NOUVEAU_BO_MAP, 0,
255 tx->base.layer_stride, NULL, &tx->tmp.bo);
256 if (ret) {
257 pipe_resource_reference(&tx->base.resource, NULL);
258 FREE(tx);
259 return NULL;
260 }
261
262 tx->tmp.domain = NOUVEAU_BO_GART;
263 tx->tmp.offset = 0;
264 tx->tmp.pitch = tx->base.stride;
265 tx->tmp.cpp = tx->img.cpp;
266 tx->tmp.w = tx->nblocksx;
267 tx->tmp.h = tx->nblocksy;
268 tx->tmp.d = 1;
269 tx->tmp.x0 = 0;
270 tx->tmp.y0 = 0;
271 tx->tmp.x1 = tx->tmp.w;
272 tx->tmp.y1 = tx->tmp.h;
273 tx->tmp.z = 0;
274
275 if (usage & PIPE_TRANSFER_READ)
276 nv30_transfer_rect(nv30, NEAREST, &tx->img, &tx->tmp);
277
278 if (tx->tmp.bo->map) {
279 *ptransfer = &tx->base;
280 return tx->tmp.bo->map;
281 }
282
283 if (usage & PIPE_TRANSFER_READ)
284 access |= NOUVEAU_BO_RD;
285 if (usage & PIPE_TRANSFER_WRITE)
286 access |= NOUVEAU_BO_WR;
287
288 ret = nouveau_bo_map(tx->tmp.bo, access, nv30->base.client);
289 if (ret) {
290 pipe_resource_reference(&tx->base.resource, NULL);
291 FREE(tx);
292 return NULL;
293 }
294
295 *ptransfer = &tx->base;
296 return tx->tmp.bo->map;
297 }
298
299 static void
300 nv30_miptree_transfer_unmap(struct pipe_context *pipe,
301 struct pipe_transfer *ptx)
302 {
303 struct nv30_context *nv30 = nv30_context(pipe);
304 struct nv30_transfer *tx = nv30_transfer(ptx);
305
306 if (ptx->usage & PIPE_TRANSFER_WRITE)
307 nv30_transfer_rect(nv30, NEAREST, &tx->tmp, &tx->img);
308
309 nouveau_bo_ref(NULL, &tx->tmp.bo);
310 pipe_resource_reference(&ptx->resource, NULL);
311 FREE(tx);
312 }
313
314 const struct u_resource_vtbl nv30_miptree_vtbl = {
315 nv30_miptree_get_handle,
316 nv30_miptree_destroy,
317 nv30_miptree_transfer_map,
318 u_default_transfer_flush_region,
319 nv30_miptree_transfer_unmap,
320 u_default_transfer_inline_write
321 };
322
323 struct pipe_resource *
324 nv30_miptree_create(struct pipe_screen *pscreen,
325 const struct pipe_resource *tmpl)
326 {
327 struct nouveau_device *dev = nouveau_screen(pscreen)->device;
328 struct nv30_miptree *mt = CALLOC_STRUCT(nv30_miptree);
329 struct pipe_resource *pt = &mt->base.base;
330 unsigned blocksz, size;
331 unsigned w, h, d, l;
332 int ret;
333
334 switch (tmpl->nr_samples) {
335 case 4:
336 mt->ms_mode = 0x00004000;
337 mt->ms_x = 1;
338 mt->ms_y = 1;
339 break;
340 case 2:
341 mt->ms_mode = 0x00003000;
342 mt->ms_x = 1;
343 mt->ms_y = 0;
344 break;
345 default:
346 mt->ms_mode = 0x00000000;
347 mt->ms_x = 0;
348 mt->ms_y = 0;
349 break;
350 }
351
352 mt->base.vtbl = &nv30_miptree_vtbl;
353 *pt = *tmpl;
354 pipe_reference_init(&pt->reference, 1);
355 pt->screen = pscreen;
356
357 w = pt->width0 << mt->ms_x;
358 h = pt->height0 << mt->ms_y;
359 d = (pt->target == PIPE_TEXTURE_3D) ? pt->depth0 : 1;
360 blocksz = util_format_get_blocksize(pt->format);
361
362 if ((pt->target == PIPE_TEXTURE_RECT) ||
363 (pt->bind & PIPE_BIND_SCANOUT) ||
364 !util_is_power_of_two(pt->width0) ||
365 !util_is_power_of_two(pt->height0) ||
366 !util_is_power_of_two(pt->depth0) ||
367 util_format_is_compressed(pt->format) ||
368 util_format_is_float(pt->format) || mt->ms_mode) {
369 mt->uniform_pitch = util_format_get_nblocksx(pt->format, w) * blocksz;
370 mt->uniform_pitch = align(mt->uniform_pitch, 64);
371 if (pt->bind & PIPE_BIND_SCANOUT) {
372 struct nv30_screen *screen = nv30_screen(pscreen);
373 int pitch_align = MAX2(
374 screen->eng3d->oclass >= NV40_3D_CLASS ? 1024 : 256,
375 /* round_down_pow2(mt->uniform_pitch / 4) */
376 1 << (util_last_bit(mt->uniform_pitch / 4) - 1));
377 mt->uniform_pitch = align(mt->uniform_pitch, pitch_align);
378 }
379 }
380
381 if (!mt->uniform_pitch)
382 mt->swizzled = true;
383
384 size = 0;
385 for (l = 0; l <= pt->last_level; l++) {
386 struct nv30_miptree_level *lvl = &mt->level[l];
387 unsigned nbx = util_format_get_nblocksx(pt->format, w);
388 unsigned nby = util_format_get_nblocksx(pt->format, h);
389
390 lvl->offset = size;
391 lvl->pitch = mt->uniform_pitch;
392 if (!lvl->pitch)
393 lvl->pitch = nbx * blocksz;
394
395 lvl->zslice_size = lvl->pitch * nby;
396 size += lvl->zslice_size * d;
397
398 w = u_minify(w, 1);
399 h = u_minify(h, 1);
400 d = u_minify(d, 1);
401 }
402
403 mt->layer_size = size;
404 if (pt->target == PIPE_TEXTURE_CUBE) {
405 if (!mt->uniform_pitch)
406 mt->layer_size = align(mt->layer_size, 128);
407 size = mt->layer_size * 6;
408 }
409
410 ret = nouveau_bo_new(dev, NOUVEAU_BO_VRAM, 256, size, NULL, &mt->base.bo);
411 if (ret) {
412 FREE(mt);
413 return NULL;
414 }
415
416 mt->base.domain = NOUVEAU_BO_VRAM;
417 return &mt->base.base;
418 }
419
420 struct pipe_resource *
421 nv30_miptree_from_handle(struct pipe_screen *pscreen,
422 const struct pipe_resource *tmpl,
423 struct winsys_handle *handle)
424 {
425 struct nv30_miptree *mt;
426 unsigned stride;
427
428 /* only supports 2D, non-mipmapped textures for the moment */
429 if ((tmpl->target != PIPE_TEXTURE_2D &&
430 tmpl->target != PIPE_TEXTURE_RECT) ||
431 tmpl->last_level != 0 ||
432 tmpl->depth0 != 1 ||
433 tmpl->array_size > 1)
434 return NULL;
435
436 mt = CALLOC_STRUCT(nv30_miptree);
437 if (!mt)
438 return NULL;
439
440 mt->base.bo = nouveau_screen_bo_from_handle(pscreen, handle, &stride);
441 if (mt->base.bo == NULL) {
442 FREE(mt);
443 return NULL;
444 }
445
446 mt->base.base = *tmpl;
447 mt->base.vtbl = &nv30_miptree_vtbl;
448 pipe_reference_init(&mt->base.base.reference, 1);
449 mt->base.base.screen = pscreen;
450 mt->uniform_pitch = stride;
451 mt->level[0].pitch = mt->uniform_pitch;
452 mt->level[0].offset = 0;
453
454 /* no need to adjust bo reference count */
455 return &mt->base.base;
456 }
457
458 struct pipe_surface *
459 nv30_miptree_surface_new(struct pipe_context *pipe,
460 struct pipe_resource *pt,
461 const struct pipe_surface *tmpl)
462 {
463 struct nv30_miptree *mt = nv30_miptree(pt); /* guaranteed */
464 struct nv30_surface *ns;
465 struct pipe_surface *ps;
466 struct nv30_miptree_level *lvl = &mt->level[tmpl->u.tex.level];
467
468 ns = CALLOC_STRUCT(nv30_surface);
469 if (!ns)
470 return NULL;
471 ps = &ns->base;
472
473 pipe_reference_init(&ps->reference, 1);
474 pipe_resource_reference(&ps->texture, pt);
475 ps->context = pipe;
476 ps->format = tmpl->format;
477 ps->u.tex.level = tmpl->u.tex.level;
478 ps->u.tex.first_layer = tmpl->u.tex.first_layer;
479 ps->u.tex.last_layer = tmpl->u.tex.last_layer;
480
481 ns->width = u_minify(pt->width0, ps->u.tex.level);
482 ns->height = u_minify(pt->height0, ps->u.tex.level);
483 ns->depth = ps->u.tex.last_layer - ps->u.tex.first_layer + 1;
484 ns->offset = layer_offset(pt, ps->u.tex.level, ps->u.tex.first_layer);
485 if (mt->swizzled)
486 ns->pitch = 4096; /* random, just something the hw won't reject.. */
487 else
488 ns->pitch = lvl->pitch;
489
490 /* comment says there are going to be removed, but they're used by the st */
491 ps->width = ns->width;
492 ps->height = ns->height;
493 return ps;
494 }
495
496 void
497 nv30_miptree_surface_del(struct pipe_context *pipe, struct pipe_surface *ps)
498 {
499 struct nv30_surface *ns = nv30_surface(ps);
500
501 pipe_resource_reference(&ps->texture, NULL);
502 FREE(ns);
503 }