2 * Copyright 2008 Ben Skeggs
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
23 #include "pipe/p_state.h"
24 #include "pipe/p_defines.h"
25 #include "util/u_inlines.h"
26 #include "util/u_format.h"
28 #include "nv50_context.h"
29 #include "nv50_resource.h"
30 #include "nv50_transfer.h"
32 static INLINE
uint32_t
33 nv50_tex_choose_tile_dims(unsigned nx
, unsigned ny
, unsigned nz
)
35 return nvc0_tex_choose_tile_dims(nx
, ny
* 2, nz
) >> 4;
39 nv50_mt_choose_storage_type(struct nv50_miptree
*mt
, boolean compressed
)
41 const unsigned ms
= util_logbase2(mt
->base
.base
.nr_samples
);
45 if (mt
->base
.base
.bind
& PIPE_BIND_CURSOR
)
46 return NOUVEAU_BO_TILE_SCANOUT
;
48 switch (mt
->base
.base
.format
) {
49 case PIPE_FORMAT_Z16_UNORM
:
50 tile_flags
= 0x6c00 + (ms
<< 8);
52 case PIPE_FORMAT_S8_USCALED_Z24_UNORM
:
53 tile_flags
= 0x1800 + (ms
<< 8);
55 case PIPE_FORMAT_Z24X8_UNORM
:
56 case PIPE_FORMAT_Z24_UNORM_S8_USCALED
:
57 tile_flags
= 0x22800 + (ms
<< 8);
59 case PIPE_FORMAT_Z32_FLOAT
:
60 tile_flags
= 0x4000 + (ms
<< 8);
62 case PIPE_FORMAT_Z32_FLOAT_S8X24_USCALED
:
63 tile_flags
= 0x6000 + (ms
<< 8);
66 switch (util_format_get_blocksizebits(mt
->base
.base
.format
)) {
73 case 2: tile_flags
= 0x17c00; break;
74 case 3: tile_flags
= 0x17d00; break;
81 if (mt
->base
.base
.bind
& PIPE_BIND_SCANOUT
) {
86 case 2: tile_flags
= 0x17800; break;
87 case 3: tile_flags
= 0x17900; break;
101 if (mt
->base
.base
.bind
& PIPE_BIND_CURSOR
)
105 if (mt
->base
.base
.bind
& (PIPE_BIND_SCANOUT
| PIPE_BIND_CURSOR
))
106 tile_flags
|= NOUVEAU_BO_TILE_SCANOUT
;
109 tile_flags
&= ~0x30000;
115 nv50_miptree_destroy(struct pipe_screen
*pscreen
, struct pipe_resource
*pt
)
117 struct nv50_miptree
*mt
= nv50_miptree(pt
);
119 nouveau_screen_bo_release(pscreen
, mt
->base
.bo
);
125 nv50_miptree_get_handle(struct pipe_screen
*pscreen
,
126 struct pipe_resource
*pt
,
127 struct winsys_handle
*whandle
)
129 struct nv50_miptree
*mt
= nv50_miptree(pt
);
132 if (!mt
|| !mt
->base
.bo
)
135 stride
= util_format_get_stride(mt
->base
.base
.format
,
136 mt
->base
.base
.width0
);
138 return nouveau_screen_bo_get_handle(pscreen
,
144 const struct u_resource_vtbl nv50_miptree_vtbl
=
146 nv50_miptree_get_handle
, /* get_handle */
147 nv50_miptree_destroy
, /* resource_destroy */
148 nv50_miptree_transfer_new
, /* get_transfer */
149 nv50_miptree_transfer_del
, /* transfer_destroy */
150 nv50_miptree_transfer_map
, /* transfer_map */
151 u_default_transfer_flush_region
, /* transfer_flush_region */
152 nv50_miptree_transfer_unmap
, /* transfer_unmap */
153 u_default_transfer_inline_write
/* transfer_inline_write */
156 static INLINE boolean
157 nv50_miptree_init_ms_mode(struct nv50_miptree
*mt
)
159 switch (mt
->base
.base
.nr_samples
) {
161 mt
->ms_mode
= NV50_3D_MULTISAMPLE_MODE_MS8
;
166 mt
->ms_mode
= NV50_3D_MULTISAMPLE_MODE_MS4
;
171 mt
->ms_mode
= NV50_3D_MULTISAMPLE_MODE_MS2
;
176 mt
->ms_mode
= NV50_3D_MULTISAMPLE_MODE_MS1
;
179 NOUVEAU_ERR("invalid nr_samples: %u\n", mt
->base
.base
.nr_samples
);
186 nv50_miptree_init_layout_linear(struct nv50_miptree
*mt
)
188 struct pipe_resource
*pt
= &mt
->base
.base
;
190 if (util_format_is_depth_or_stencil(pt
->format
))
193 if ((pt
->last_level
> 0) || (pt
->depth0
> 1) || (pt
->array_size
> 1))
195 if (mt
->ms_x
| mt
->ms_y
)
198 mt
->level
[0].pitch
= align(pt
->width0
, 64);
200 mt
->total_size
= mt
->level
[0].pitch
* pt
->height0
;
206 nv50_miptree_init_layout_tiled(struct nv50_miptree
*mt
)
208 struct pipe_resource
*pt
= &mt
->base
.base
;
210 const unsigned blocksize
= util_format_get_blocksize(pt
->format
);
212 mt
->layout_3d
= pt
->target
== PIPE_TEXTURE_3D
;
214 w
= pt
->width0
<< mt
->ms_x
;
215 h
= pt
->height0
<< mt
->ms_y
;
217 /* For 3D textures, a mipmap is spanned by all the layers, for array
218 * textures and cube maps, each layer contains its own mipmaps.
220 d
= mt
->layout_3d
? pt
->depth0
: 1;
222 for (l
= 0; l
<= pt
->last_level
; ++l
) {
223 struct nv50_miptree_level
*lvl
= &mt
->level
[l
];
224 unsigned tsx
, tsy
, tsz
;
225 unsigned nbx
= util_format_get_nblocksx(pt
->format
, w
);
226 unsigned nby
= util_format_get_nblocksy(pt
->format
, h
);
228 lvl
->offset
= mt
->total_size
;
230 lvl
->tile_mode
= nv50_tex_choose_tile_dims(nbx
, nby
, d
);
232 tsx
= NV50_TILE_SIZE_X(lvl
->tile_mode
); /* x is tile row pitch in bytes */
233 tsy
= NV50_TILE_SIZE_Y(lvl
->tile_mode
);
234 tsz
= NV50_TILE_SIZE_Z(lvl
->tile_mode
);
236 lvl
->pitch
= align(nbx
* blocksize
, tsx
);
238 mt
->total_size
+= lvl
->pitch
* align(nby
, tsy
) * align(d
, tsz
);
245 if (pt
->array_size
> 1) {
246 mt
->layer_stride
= align(mt
->total_size
,
247 NV50_TILE_SIZE(mt
->level
[0].tile_mode
));
248 mt
->total_size
= mt
->layer_stride
* pt
->array_size
;
252 struct pipe_resource
*
253 nv50_miptree_create(struct pipe_screen
*pscreen
,
254 const struct pipe_resource
*templ
)
256 struct nouveau_device
*dev
= nouveau_screen(pscreen
)->device
;
257 struct nv50_miptree
*mt
= CALLOC_STRUCT(nv50_miptree
);
258 struct pipe_resource
*pt
= &mt
->base
.base
;
265 mt
->base
.vtbl
= &nv50_miptree_vtbl
;
267 pipe_reference_init(&pt
->reference
, 1);
268 pt
->screen
= pscreen
;
270 tile_flags
= nv50_mt_choose_storage_type(mt
, TRUE
);
272 if (!nv50_miptree_init_ms_mode(mt
)) {
277 if (tile_flags
& NOUVEAU_BO_TILE_LAYOUT_MASK
) {
278 nv50_miptree_init_layout_tiled(mt
);
280 if (!nv50_miptree_init_layout_linear(mt
)) {
285 ret
= nouveau_bo_new_tile(dev
, NOUVEAU_BO_VRAM
, 4096,
287 mt
->level
[0].tile_mode
, tile_flags
,
293 mt
->base
.domain
= NOUVEAU_BO_VRAM
;
298 struct pipe_resource
*
299 nv50_miptree_from_handle(struct pipe_screen
*pscreen
,
300 const struct pipe_resource
*templ
,
301 struct winsys_handle
*whandle
)
303 struct nv50_miptree
*mt
;
306 /* only supports 2D, non-mipmapped textures for the moment */
307 if ((templ
->target
!= PIPE_TEXTURE_2D
&&
308 templ
->target
!= PIPE_TEXTURE_RECT
) ||
309 templ
->last_level
!= 0 ||
310 templ
->depth0
!= 1 ||
311 templ
->array_size
> 1)
314 mt
= CALLOC_STRUCT(nv50_miptree
);
318 mt
->base
.bo
= nouveau_screen_bo_from_handle(pscreen
, whandle
, &stride
);
319 if (mt
->base
.bo
== NULL
) {
324 mt
->base
.base
= *templ
;
325 mt
->base
.vtbl
= &nv50_miptree_vtbl
;
326 pipe_reference_init(&mt
->base
.base
.reference
, 1);
327 mt
->base
.base
.screen
= pscreen
;
328 mt
->level
[0].pitch
= stride
;
329 mt
->level
[0].offset
= 0;
330 mt
->level
[0].tile_mode
= mt
->base
.bo
->tile_mode
;
332 /* no need to adjust bo reference count */
333 return &mt
->base
.base
;
337 /* Offset of zslice @z from start of level @l. */
339 nv50_mt_zslice_offset(const struct nv50_miptree
*mt
, unsigned l
, unsigned z
)
341 const struct pipe_resource
*pt
= &mt
->base
.base
;
343 unsigned tds
= NV50_TILE_SHIFT_Z(mt
->level
[l
].tile_mode
);
344 unsigned ths
= NV50_TILE_SHIFT_Y(mt
->level
[l
].tile_mode
);
346 unsigned nby
= util_format_get_nblocksy(pt
->format
,
347 u_minify(pt
->height0
, l
));
349 /* to next 2D tile slice within a 3D tile */
350 unsigned stride_2d
= NV50_TILE_SIZE_2D(mt
->level
[l
].tile_mode
);
352 /* to slice in the next (in z direction) 3D tile */
353 unsigned stride_3d
= (align(nby
, (1 << ths
)) * mt
->level
[l
].pitch
) << tds
;
355 return (z
& ((1 << tds
) - 1)) * stride_2d
+ (z
>> tds
) * stride_3d
;
358 /* Surface functions.
361 struct nv50_surface
*
362 nv50_surface_from_miptree(struct nv50_miptree
*mt
,
363 const struct pipe_surface
*templ
)
365 struct pipe_surface
*ps
;
366 struct nv50_surface
*ns
= CALLOC_STRUCT(nv50_surface
);
371 pipe_reference_init(&ps
->reference
, 1);
372 pipe_resource_reference(&ps
->texture
, &mt
->base
.base
);
374 ps
->format
= templ
->format
;
375 ps
->usage
= templ
->usage
;
376 ps
->u
.tex
.level
= templ
->u
.tex
.level
;
377 ps
->u
.tex
.first_layer
= templ
->u
.tex
.first_layer
;
378 ps
->u
.tex
.last_layer
= templ
->u
.tex
.last_layer
;
380 ns
->width
= u_minify(mt
->base
.base
.width0
, ps
->u
.tex
.level
);
381 ns
->height
= u_minify(mt
->base
.base
.height0
, ps
->u
.tex
.level
);
382 ns
->depth
= ps
->u
.tex
.last_layer
- ps
->u
.tex
.first_layer
+ 1;
383 ns
->offset
= mt
->level
[templ
->u
.tex
.level
].offset
;
385 /* comment says there are going to be removed, but they're used by the st */
386 ps
->width
= ns
->width
;
387 ps
->height
= ns
->height
;
389 ns
->width
<<= mt
->ms_x
;
390 ns
->height
<<= mt
->ms_y
;
395 struct pipe_surface
*
396 nv50_miptree_surface_new(struct pipe_context
*pipe
,
397 struct pipe_resource
*pt
,
398 const struct pipe_surface
*templ
)
400 struct nv50_miptree
*mt
= nv50_miptree(pt
);
401 struct nv50_surface
*ns
= nv50_surface_from_miptree(mt
, templ
);
404 ns
->base
.context
= pipe
;
406 if (ns
->base
.u
.tex
.first_layer
) {
407 const unsigned l
= ns
->base
.u
.tex
.level
;
408 const unsigned z
= ns
->base
.u
.tex
.first_layer
;
411 ns
->offset
+= nv50_mt_zslice_offset(mt
, l
, z
);
413 if (z
& (NV50_TILE_SIZE_Z(mt
->level
[l
].tile_mode
) - 1))
414 NOUVEAU_ERR("Creating unsupported 3D surface !\n");
416 ns
->offset
+= mt
->layer_stride
* z
;
424 nv50_miptree_surface_del(struct pipe_context
*pipe
, struct pipe_surface
*ps
)
426 struct nv50_surface
*s
= nv50_surface(ps
);
428 pipe_resource_reference(&ps
->texture
, NULL
);