d4dcacb85069fe14d6d9b084d6510ea8736e830a
[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_buffer_slot(nv30->blitter, nv30->vtxbuf);
197 util_blitter_save_vertex_elements(nv30->blitter, nv30->vertex);
198 util_blitter_save_vertex_shader(nv30->blitter, nv30->vertprog.program);
199 util_blitter_save_rasterizer(nv30->blitter, nv30->rast);
200 util_blitter_save_viewport(nv30->blitter, &nv30->viewport);
201 util_blitter_save_scissor(nv30->blitter, &nv30->scissor);
202 util_blitter_save_fragment_shader(nv30->blitter, nv30->fragprog.program);
203 util_blitter_save_blend(nv30->blitter, nv30->blend);
204 util_blitter_save_depth_stencil_alpha(nv30->blitter,
205 nv30->zsa);
206 util_blitter_save_stencil_ref(nv30->blitter, &nv30->stencil_ref);
207 util_blitter_save_sample_mask(nv30->blitter, nv30->sample_mask);
208 util_blitter_save_framebuffer(nv30->blitter, &nv30->framebuffer);
209 util_blitter_save_fragment_sampler_states(nv30->blitter,
210 nv30->fragprog.num_samplers,
211 (void**)nv30->fragprog.samplers);
212 util_blitter_save_fragment_sampler_views(nv30->blitter,
213 nv30->fragprog.num_textures, nv30->fragprog.textures);
214 util_blitter_save_render_condition(nv30->blitter, nv30->render_cond_query,
215 nv30->render_cond_mode);
216 util_blitter_blit(nv30->blitter, &info);
217 }
218
219 static void *
220 nv30_miptree_transfer_map(struct pipe_context *pipe, struct pipe_resource *pt,
221 unsigned level, unsigned usage,
222 const struct pipe_box *box,
223 struct pipe_transfer **ptransfer)
224 {
225 struct nv30_context *nv30 = nv30_context(pipe);
226 struct nouveau_device *dev = nv30->screen->base.device;
227 struct nv30_transfer *tx;
228 unsigned access = 0;
229 int ret;
230
231 tx = CALLOC_STRUCT(nv30_transfer);
232 if (!tx)
233 return NULL;
234 pipe_resource_reference(&tx->base.resource, pt);
235 tx->base.level = level;
236 tx->base.usage = usage;
237 tx->base.box = *box;
238 tx->base.stride = util_format_get_nblocksx(pt->format, box->width) *
239 util_format_get_blocksize(pt->format);
240 tx->base.layer_stride = util_format_get_nblocksy(pt->format, box->height) *
241 tx->base.stride;
242
243 tx->nblocksx = util_format_get_nblocksx(pt->format, box->width);
244 tx->nblocksy = util_format_get_nblocksy(pt->format, box->height);
245
246 define_rect(pt, level, box->z, box->x, box->y,
247 tx->nblocksx, tx->nblocksy, &tx->img);
248
249 ret = nouveau_bo_new(dev, NOUVEAU_BO_GART | NOUVEAU_BO_MAP, 0,
250 tx->base.layer_stride, NULL, &tx->tmp.bo);
251 if (ret) {
252 pipe_resource_reference(&tx->base.resource, NULL);
253 FREE(tx);
254 return NULL;
255 }
256
257 tx->tmp.domain = NOUVEAU_BO_GART;
258 tx->tmp.offset = 0;
259 tx->tmp.pitch = tx->base.stride;
260 tx->tmp.cpp = tx->img.cpp;
261 tx->tmp.w = tx->nblocksx;
262 tx->tmp.h = tx->nblocksy;
263 tx->tmp.d = 1;
264 tx->tmp.x0 = 0;
265 tx->tmp.y0 = 0;
266 tx->tmp.x1 = tx->tmp.w;
267 tx->tmp.y1 = tx->tmp.h;
268 tx->tmp.z = 0;
269
270 if (usage & PIPE_TRANSFER_READ)
271 nv30_transfer_rect(nv30, NEAREST, &tx->img, &tx->tmp);
272
273 if (tx->tmp.bo->map) {
274 *ptransfer = &tx->base;
275 return tx->tmp.bo->map;
276 }
277
278 if (usage & PIPE_TRANSFER_READ)
279 access |= NOUVEAU_BO_RD;
280 if (usage & PIPE_TRANSFER_WRITE)
281 access |= NOUVEAU_BO_WR;
282
283 ret = nouveau_bo_map(tx->tmp.bo, access, nv30->base.client);
284 if (ret) {
285 pipe_resource_reference(&tx->base.resource, NULL);
286 FREE(tx);
287 return NULL;
288 }
289
290 *ptransfer = &tx->base;
291 return tx->tmp.bo->map;
292 }
293
294 static void
295 nv30_miptree_transfer_unmap(struct pipe_context *pipe,
296 struct pipe_transfer *ptx)
297 {
298 struct nv30_context *nv30 = nv30_context(pipe);
299 struct nv30_transfer *tx = nv30_transfer(ptx);
300
301 if (ptx->usage & PIPE_TRANSFER_WRITE)
302 nv30_transfer_rect(nv30, NEAREST, &tx->tmp, &tx->img);
303
304 nouveau_bo_ref(NULL, &tx->tmp.bo);
305 pipe_resource_reference(&ptx->resource, NULL);
306 FREE(tx);
307 }
308
309 const struct u_resource_vtbl nv30_miptree_vtbl = {
310 nv30_miptree_get_handle,
311 nv30_miptree_destroy,
312 nv30_miptree_transfer_map,
313 u_default_transfer_flush_region,
314 nv30_miptree_transfer_unmap,
315 u_default_transfer_inline_write
316 };
317
318 struct pipe_resource *
319 nv30_miptree_create(struct pipe_screen *pscreen,
320 const struct pipe_resource *tmpl)
321 {
322 struct nouveau_device *dev = nouveau_screen(pscreen)->device;
323 struct nv30_miptree *mt = CALLOC_STRUCT(nv30_miptree);
324 struct pipe_resource *pt = &mt->base.base;
325 unsigned blocksz, size;
326 unsigned w, h, d, l;
327 int ret;
328
329 switch (tmpl->nr_samples) {
330 case 4:
331 mt->ms_mode = 0x00004000;
332 mt->ms_x = 1;
333 mt->ms_y = 1;
334 break;
335 case 2:
336 mt->ms_mode = 0x00003000;
337 mt->ms_x = 1;
338 mt->ms_y = 0;
339 break;
340 default:
341 mt->ms_mode = 0x00000000;
342 mt->ms_x = 0;
343 mt->ms_y = 0;
344 break;
345 }
346
347 mt->base.vtbl = &nv30_miptree_vtbl;
348 *pt = *tmpl;
349 pipe_reference_init(&pt->reference, 1);
350 pt->screen = pscreen;
351
352 w = pt->width0 << mt->ms_x;
353 h = pt->height0 << mt->ms_y;
354 d = (pt->target == PIPE_TEXTURE_3D) ? pt->depth0 : 1;
355 blocksz = util_format_get_blocksize(pt->format);
356
357 if ((pt->target == PIPE_TEXTURE_RECT) ||
358 !util_is_power_of_two(pt->width0) ||
359 !util_is_power_of_two(pt->height0) ||
360 !util_is_power_of_two(pt->depth0) ||
361 util_format_is_compressed(pt->format) ||
362 util_format_is_float(pt->format) || mt->ms_mode) {
363 mt->uniform_pitch = util_format_get_nblocksx(pt->format, w) * blocksz;
364 mt->uniform_pitch = align(mt->uniform_pitch, 64);
365 }
366
367 if (!mt->uniform_pitch)
368 mt->swizzled = TRUE;
369
370 size = 0;
371 for (l = 0; l <= pt->last_level; l++) {
372 struct nv30_miptree_level *lvl = &mt->level[l];
373 unsigned nbx = util_format_get_nblocksx(pt->format, w);
374 unsigned nby = util_format_get_nblocksx(pt->format, h);
375
376 lvl->offset = size;
377 lvl->pitch = mt->uniform_pitch;
378 if (!lvl->pitch)
379 lvl->pitch = nbx * blocksz;
380
381 lvl->zslice_size = lvl->pitch * nby;
382 size += lvl->zslice_size * d;
383
384 w = u_minify(w, 1);
385 h = u_minify(h, 1);
386 d = u_minify(d, 1);
387 }
388
389 mt->layer_size = size;
390 if (pt->target == PIPE_TEXTURE_CUBE) {
391 if (!mt->uniform_pitch)
392 mt->layer_size = align(mt->layer_size, 128);
393 size = mt->layer_size * 6;
394 }
395
396 ret = nouveau_bo_new(dev, NOUVEAU_BO_VRAM, 256, size, NULL, &mt->base.bo);
397 if (ret) {
398 FREE(mt);
399 return NULL;
400 }
401
402 mt->base.domain = NOUVEAU_BO_VRAM;
403 return &mt->base.base;
404 }
405
406 struct pipe_resource *
407 nv30_miptree_from_handle(struct pipe_screen *pscreen,
408 const struct pipe_resource *tmpl,
409 struct winsys_handle *handle)
410 {
411 struct nv30_miptree *mt;
412 unsigned stride;
413
414 /* only supports 2D, non-mipmapped textures for the moment */
415 if ((tmpl->target != PIPE_TEXTURE_2D &&
416 tmpl->target != PIPE_TEXTURE_RECT) ||
417 tmpl->last_level != 0 ||
418 tmpl->depth0 != 1 ||
419 tmpl->array_size > 1)
420 return NULL;
421
422 mt = CALLOC_STRUCT(nv30_miptree);
423 if (!mt)
424 return NULL;
425
426 mt->base.bo = nouveau_screen_bo_from_handle(pscreen, handle, &stride);
427 if (mt->base.bo == NULL) {
428 FREE(mt);
429 return NULL;
430 }
431
432 mt->base.base = *tmpl;
433 mt->base.vtbl = &nv30_miptree_vtbl;
434 pipe_reference_init(&mt->base.base.reference, 1);
435 mt->base.base.screen = pscreen;
436 mt->uniform_pitch = stride;
437 mt->level[0].pitch = mt->uniform_pitch;
438 mt->level[0].offset = 0;
439
440 /* no need to adjust bo reference count */
441 return &mt->base.base;
442 }
443
444 struct pipe_surface *
445 nv30_miptree_surface_new(struct pipe_context *pipe,
446 struct pipe_resource *pt,
447 const struct pipe_surface *tmpl)
448 {
449 struct nv30_miptree *mt = nv30_miptree(pt); /* guaranteed */
450 struct nv30_surface *ns;
451 struct pipe_surface *ps;
452 struct nv30_miptree_level *lvl = &mt->level[tmpl->u.tex.level];
453
454 ns = CALLOC_STRUCT(nv30_surface);
455 if (!ns)
456 return NULL;
457 ps = &ns->base;
458
459 pipe_reference_init(&ps->reference, 1);
460 pipe_resource_reference(&ps->texture, pt);
461 ps->context = pipe;
462 ps->format = tmpl->format;
463 ps->u.tex.level = tmpl->u.tex.level;
464 ps->u.tex.first_layer = tmpl->u.tex.first_layer;
465 ps->u.tex.last_layer = tmpl->u.tex.last_layer;
466
467 ns->width = u_minify(pt->width0, ps->u.tex.level);
468 ns->height = u_minify(pt->height0, ps->u.tex.level);
469 ns->depth = ps->u.tex.last_layer - ps->u.tex.first_layer + 1;
470 ns->offset = layer_offset(pt, ps->u.tex.level, ps->u.tex.first_layer);
471 if (mt->swizzled)
472 ns->pitch = 4096; /* random, just something the hw won't reject.. */
473 else
474 ns->pitch = lvl->pitch;
475
476 /* comment says there are going to be removed, but they're used by the st */
477 ps->width = ns->width;
478 ps->height = ns->height;
479 return ps;
480 }
481
482 void
483 nv30_miptree_surface_del(struct pipe_context *pipe, struct pipe_surface *ps)
484 {
485 struct nv30_surface *ns = nv30_surface(ps);
486
487 pipe_resource_reference(&ps->texture, NULL);
488 FREE(ns);
489 }