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