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"
31 static INLINE
uint32_t
32 nv50_tex_choose_tile_dims(unsigned nx
, unsigned ny
, unsigned nz
)
34 return nvc0_tex_choose_tile_dims(nx
, ny
* 2, nz
);
38 nv50_mt_choose_storage_type(struct nv50_miptree
*mt
, boolean compressed
)
40 const unsigned ms
= mt
->ms_x
+ mt
->ms_y
;
44 if (unlikely(mt
->base
.base
.flags
& NOUVEAU_RESOURCE_FLAG_LINEAR
))
46 if (unlikely(mt
->base
.base
.bind
& PIPE_BIND_CURSOR
))
49 switch (mt
->base
.base
.format
) {
50 case PIPE_FORMAT_Z16_UNORM
:
51 tile_flags
= 0x6c + ms
;
53 case PIPE_FORMAT_S8_UINT_Z24_UNORM
:
54 tile_flags
= 0x18 + ms
;
56 case PIPE_FORMAT_Z24X8_UNORM
:
57 case PIPE_FORMAT_Z24_UNORM_S8_UINT
:
58 tile_flags
= 0x128 + ms
;
60 case PIPE_FORMAT_Z32_FLOAT
:
61 tile_flags
= 0x40 + ms
;
63 case PIPE_FORMAT_Z32_FLOAT_S8X24_UINT
:
64 tile_flags
= 0x60 + ms
;
67 switch (util_format_get_blocksizebits(mt
->base
.base
.format
)) {
74 case 2: tile_flags
= 0xfc; break;
75 case 3: tile_flags
= 0xfd; break;
82 if (mt
->base
.base
.bind
& PIPE_BIND_SCANOUT
) {
87 case 2: tile_flags
= 0xf8; break;
88 case 3: tile_flags
= 0xf9; break;
102 if (mt
->base
.base
.bind
& PIPE_BIND_CURSOR
)
107 tile_flags
&= ~0x180;
113 nv50_miptree_destroy(struct pipe_screen
*pscreen
, struct pipe_resource
*pt
)
115 struct nv50_miptree
*mt
= nv50_miptree(pt
);
117 nouveau_bo_ref(NULL
, &mt
->base
.bo
);
119 nouveau_fence_ref(NULL
, &mt
->base
.fence
);
120 nouveau_fence_ref(NULL
, &mt
->base
.fence_wr
);
126 nv50_miptree_get_handle(struct pipe_screen
*pscreen
,
127 struct pipe_resource
*pt
,
128 struct winsys_handle
*whandle
)
130 struct nv50_miptree
*mt
= nv50_miptree(pt
);
133 if (!mt
|| !mt
->base
.bo
)
136 stride
= mt
->level
[0].pitch
;
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_map
, /* transfer_map */
149 u_default_transfer_flush_region
, /* transfer_flush_region */
150 nv50_miptree_transfer_unmap
, /* transfer_unmap */
151 u_default_transfer_inline_write
/* transfer_inline_write */
154 static INLINE boolean
155 nv50_miptree_init_ms_mode(struct nv50_miptree
*mt
)
157 switch (mt
->base
.base
.nr_samples
) {
159 mt
->ms_mode
= NV50_3D_MULTISAMPLE_MODE_MS8
;
164 mt
->ms_mode
= NV50_3D_MULTISAMPLE_MODE_MS4
;
169 mt
->ms_mode
= NV50_3D_MULTISAMPLE_MODE_MS2
;
174 mt
->ms_mode
= NV50_3D_MULTISAMPLE_MODE_MS1
;
177 NOUVEAU_ERR("invalid nr_samples: %u\n", mt
->base
.base
.nr_samples
);
184 nv50_miptree_init_layout_linear(struct nv50_miptree
*mt
)
186 struct pipe_resource
*pt
= &mt
->base
.base
;
187 const unsigned blocksize
= util_format_get_blocksize(pt
->format
);
189 if (util_format_is_depth_or_stencil(pt
->format
))
192 if ((pt
->last_level
> 0) || (pt
->depth0
> 1) || (pt
->array_size
> 1))
194 if (mt
->ms_x
| mt
->ms_y
)
197 mt
->level
[0].pitch
= align(pt
->width0
* blocksize
, 64);
199 mt
->total_size
= mt
->level
[0].pitch
* pt
->height0
;
205 nv50_miptree_init_layout_tiled(struct nv50_miptree
*mt
)
207 struct pipe_resource
*pt
= &mt
->base
.base
;
209 const unsigned blocksize
= util_format_get_blocksize(pt
->format
);
211 mt
->layout_3d
= pt
->target
== PIPE_TEXTURE_3D
;
213 w
= pt
->width0
<< mt
->ms_x
;
214 h
= pt
->height0
<< mt
->ms_y
;
216 /* For 3D textures, a mipmap is spanned by all the layers, for array
217 * textures and cube maps, each layer contains its own mipmaps.
219 d
= mt
->layout_3d
? pt
->depth0
: 1;
221 for (l
= 0; l
<= pt
->last_level
; ++l
) {
222 struct nv50_miptree_level
*lvl
= &mt
->level
[l
];
223 unsigned tsx
, tsy
, tsz
;
224 unsigned nbx
= util_format_get_nblocksx(pt
->format
, w
);
225 unsigned nby
= util_format_get_nblocksy(pt
->format
, h
);
227 lvl
->offset
= mt
->total_size
;
229 lvl
->tile_mode
= nv50_tex_choose_tile_dims(nbx
, nby
, d
);
231 tsx
= NV50_TILE_SIZE_X(lvl
->tile_mode
); /* x is tile row pitch in bytes */
232 tsy
= NV50_TILE_SIZE_Y(lvl
->tile_mode
);
233 tsz
= NV50_TILE_SIZE_Z(lvl
->tile_mode
);
235 lvl
->pitch
= align(nbx
* blocksize
, tsx
);
237 mt
->total_size
+= lvl
->pitch
* align(nby
, tsy
) * align(d
, tsz
);
244 if (pt
->array_size
> 1) {
245 mt
->layer_stride
= align(mt
->total_size
,
246 NV50_TILE_SIZE(mt
->level
[0].tile_mode
));
247 mt
->total_size
= mt
->layer_stride
* pt
->array_size
;
251 struct pipe_resource
*
252 nv50_miptree_create(struct pipe_screen
*pscreen
,
253 const struct pipe_resource
*templ
)
255 struct nouveau_device
*dev
= nouveau_screen(pscreen
)->device
;
256 struct nv50_miptree
*mt
= CALLOC_STRUCT(nv50_miptree
);
257 struct pipe_resource
*pt
= &mt
->base
.base
;
259 union nouveau_bo_config bo_config
;
265 mt
->base
.vtbl
= &nv50_miptree_vtbl
;
267 pipe_reference_init(&pt
->reference
, 1);
268 pt
->screen
= pscreen
;
270 bo_config
.nv50
.memtype
= nv50_mt_choose_storage_type(mt
, TRUE
);
272 if (!nv50_miptree_init_ms_mode(mt
)) {
277 if (bo_config
.nv50
.memtype
!= 0) {
278 nv50_miptree_init_layout_tiled(mt
);
280 if (!nv50_miptree_init_layout_linear(mt
)) {
284 bo_config
.nv50
.tile_mode
= mt
->level
[0].tile_mode
;
286 bo_flags
= NOUVEAU_BO_VRAM
| NOUVEAU_BO_NOSNOOP
;
287 if (mt
->base
.base
.bind
& (PIPE_BIND_CURSOR
| PIPE_BIND_DISPLAY_TARGET
))
288 bo_flags
|= NOUVEAU_BO_CONTIG
;
290 ret
= nouveau_bo_new(dev
, bo_flags
, 4096, mt
->total_size
, &bo_config
,
296 mt
->base
.domain
= NOUVEAU_BO_VRAM
;
297 mt
->base
.address
= mt
->base
.bo
->offset
;
302 struct pipe_resource
*
303 nv50_miptree_from_handle(struct pipe_screen
*pscreen
,
304 const struct pipe_resource
*templ
,
305 struct winsys_handle
*whandle
)
307 struct nv50_miptree
*mt
;
310 /* only supports 2D, non-mipmapped textures for the moment */
311 if ((templ
->target
!= PIPE_TEXTURE_2D
&&
312 templ
->target
!= PIPE_TEXTURE_RECT
) ||
313 templ
->last_level
!= 0 ||
314 templ
->depth0
!= 1 ||
315 templ
->array_size
> 1)
318 mt
= CALLOC_STRUCT(nv50_miptree
);
322 mt
->base
.bo
= nouveau_screen_bo_from_handle(pscreen
, whandle
, &stride
);
323 if (mt
->base
.bo
== NULL
) {
327 mt
->base
.domain
= NOUVEAU_BO_VRAM
;
328 mt
->base
.address
= mt
->base
.bo
->offset
;
330 mt
->base
.base
= *templ
;
331 mt
->base
.vtbl
= &nv50_miptree_vtbl
;
332 pipe_reference_init(&mt
->base
.base
.reference
, 1);
333 mt
->base
.base
.screen
= pscreen
;
334 mt
->level
[0].pitch
= stride
;
335 mt
->level
[0].offset
= 0;
336 mt
->level
[0].tile_mode
= mt
->base
.bo
->config
.nv50
.tile_mode
;
338 /* no need to adjust bo reference count */
339 return &mt
->base
.base
;
343 /* Offset of zslice @z from start of level @l. */
345 nv50_mt_zslice_offset(const struct nv50_miptree
*mt
, unsigned l
, unsigned z
)
347 const struct pipe_resource
*pt
= &mt
->base
.base
;
349 unsigned tds
= NV50_TILE_SHIFT_Z(mt
->level
[l
].tile_mode
);
350 unsigned ths
= NV50_TILE_SHIFT_Y(mt
->level
[l
].tile_mode
);
352 unsigned nby
= util_format_get_nblocksy(pt
->format
,
353 u_minify(pt
->height0
, l
));
355 /* to next 2D tile slice within a 3D tile */
356 unsigned stride_2d
= NV50_TILE_SIZE_2D(mt
->level
[l
].tile_mode
);
358 /* to slice in the next (in z direction) 3D tile */
359 unsigned stride_3d
= (align(nby
, (1 << ths
)) * mt
->level
[l
].pitch
) << tds
;
361 return (z
& ((1 << tds
) - 1)) * stride_2d
+ (z
>> tds
) * stride_3d
;
364 /* Surface functions.
367 struct nv50_surface
*
368 nv50_surface_from_miptree(struct nv50_miptree
*mt
,
369 const struct pipe_surface
*templ
)
371 struct pipe_surface
*ps
;
372 struct nv50_surface
*ns
= CALLOC_STRUCT(nv50_surface
);
377 pipe_reference_init(&ps
->reference
, 1);
378 pipe_resource_reference(&ps
->texture
, &mt
->base
.base
);
380 ps
->format
= templ
->format
;
381 ps
->usage
= templ
->usage
;
382 ps
->u
.tex
.level
= templ
->u
.tex
.level
;
383 ps
->u
.tex
.first_layer
= templ
->u
.tex
.first_layer
;
384 ps
->u
.tex
.last_layer
= templ
->u
.tex
.last_layer
;
386 ns
->width
= u_minify(mt
->base
.base
.width0
, ps
->u
.tex
.level
);
387 ns
->height
= u_minify(mt
->base
.base
.height0
, ps
->u
.tex
.level
);
388 ns
->depth
= ps
->u
.tex
.last_layer
- ps
->u
.tex
.first_layer
+ 1;
389 ns
->offset
= mt
->level
[templ
->u
.tex
.level
].offset
;
391 /* comment says there are going to be removed, but they're used by the st */
392 ps
->width
= ns
->width
;
393 ps
->height
= ns
->height
;
395 ns
->width
<<= mt
->ms_x
;
396 ns
->height
<<= mt
->ms_y
;
401 struct pipe_surface
*
402 nv50_miptree_surface_new(struct pipe_context
*pipe
,
403 struct pipe_resource
*pt
,
404 const struct pipe_surface
*templ
)
406 struct nv50_miptree
*mt
= nv50_miptree(pt
);
407 struct nv50_surface
*ns
= nv50_surface_from_miptree(mt
, templ
);
410 ns
->base
.context
= pipe
;
412 if (ns
->base
.u
.tex
.first_layer
) {
413 const unsigned l
= ns
->base
.u
.tex
.level
;
414 const unsigned z
= ns
->base
.u
.tex
.first_layer
;
417 ns
->offset
+= nv50_mt_zslice_offset(mt
, l
, z
);
419 /* TODO: switch to depth 1 tiles; but actually this shouldn't happen */
421 (z
& (NV50_TILE_SIZE_Z(mt
->level
[l
].tile_mode
) - 1)))
422 NOUVEAU_ERR("Creating unsupported 3D surface !\n");
424 ns
->offset
+= mt
->layer_stride
* z
;