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 "pipe/p_inlines.h"
27 #include "nv50_context.h"
29 /* The restrictions in tile mode selection probably aren't necessary. */
30 static INLINE
uint32_t
31 get_tile_mode(unsigned ny
, unsigned d
)
33 uint32_t tile_mode
= 0x00;
35 if (ny
> 32) tile_mode
= 0x04; /* height 64 tiles */
37 if (ny
> 16) tile_mode
= 0x03; /* height 32 tiles */
39 if (ny
> 8) tile_mode
= 0x02; /* height 16 tiles */
41 if (ny
> 4) tile_mode
= 0x01; /* height 8 tiles */
49 if (d
> 16 && tile_mode
< 0x02)
50 return tile_mode
| 0x50; /* depth 32 tiles */
51 if (d
> 8) return tile_mode
| 0x40; /* depth 16 tiles */
52 if (d
> 4) return tile_mode
| 0x30; /* depth 8 tiles */
53 if (d
> 2) return tile_mode
| 0x20; /* depth 4 tiles */
55 return tile_mode
| 0x10;
58 static struct pipe_texture
*
59 nv50_miptree_create(struct pipe_screen
*pscreen
, const struct pipe_texture
*tmp
)
61 struct nouveau_device
*dev
= nouveau_screen(pscreen
)->device
;
62 struct nv50_miptree
*mt
= CALLOC_STRUCT(nv50_miptree
);
63 struct pipe_texture
*pt
= &mt
->base
.base
;
64 unsigned width
= tmp
->width0
, height
= tmp
->height0
;
65 unsigned depth
= tmp
->depth0
, image_alignment
;
70 pipe_reference_init(&pt
->reference
, 1);
74 case PIPE_FORMAT_Z32_FLOAT
:
77 case PIPE_FORMAT_Z24S8_UNORM
:
80 case PIPE_FORMAT_X8Z24_UNORM
:
81 case PIPE_FORMAT_S8Z24_UNORM
:
89 /* XXX: texture arrays */
90 mt
->image_nr
= (pt
->target
== PIPE_TEXTURE_CUBE
) ? 6 : 1;
92 for (l
= 0; l
<= pt
->last_level
; l
++) {
93 struct nv50_miptree_level
*lvl
= &mt
->level
[l
];
94 unsigned nblocksy
= pf_get_nblocksy(pt
->format
, height
);
96 lvl
->image_offset
= CALLOC(mt
->image_nr
, sizeof(int));
97 lvl
->pitch
= align(pf_get_stride(pt
->format
, width
), 64);
98 lvl
->tile_mode
= get_tile_mode(nblocksy
, depth
);
100 width
= u_minify(width
, 1);
101 height
= u_minify(height
, 1);
102 depth
= u_minify(depth
, 1);
105 image_alignment
= get_tile_height(mt
->level
[0].tile_mode
) * 64;
106 image_alignment
*= get_tile_depth(mt
->level
[0].tile_mode
);
108 /* NOTE the distinction between arrays of mip-mapped 2D textures and
109 * mip-mapped 3D textures. We can't use image_nr == depth for 3D mip.
111 for (i
= 0; i
< mt
->image_nr
; i
++) {
112 for (l
= 0; l
<= pt
->last_level
; l
++) {
113 struct nv50_miptree_level
*lvl
= &mt
->level
[l
];
115 unsigned tile_h
= get_tile_height(lvl
->tile_mode
);
116 unsigned tile_d
= get_tile_depth(lvl
->tile_mode
);
119 size
*= align(pf_get_nblocksy(pt
->format
, u_minify(pt
->height0
, l
)), tile_h
);
120 size
*= align(u_minify(pt
->depth0
, l
), tile_d
);
122 lvl
->image_offset
[i
] = mt
->total_size
;
124 mt
->total_size
+= size
;
126 mt
->total_size
= align(mt
->total_size
, image_alignment
);
129 ret
= nouveau_bo_new_tile(dev
, NOUVEAU_BO_VRAM
, 256, mt
->total_size
,
130 mt
->level
[0].tile_mode
, tile_flags
,
133 for (l
= 0; l
< pt
->last_level
; ++l
)
134 FREE(mt
->level
[l
].image_offset
);
142 static struct pipe_texture
*
143 nv50_miptree_blanket(struct pipe_screen
*pscreen
, const struct pipe_texture
*pt
,
144 const unsigned *stride
, struct pipe_buffer
*pb
)
146 struct nouveau_bo
*bo
= nouveau_bo(pb
);
147 struct nv50_miptree
*mt
;
149 /* Only supports 2D, non-mipmapped textures for the moment */
150 if (pt
->target
!= PIPE_TEXTURE_2D
|| pt
->last_level
!= 0 ||
154 mt
= CALLOC_STRUCT(nv50_miptree
);
159 pipe_reference_init(&mt
->base
.base
.reference
, 1);
160 mt
->base
.base
.screen
= pscreen
;
162 mt
->level
[0].pitch
= *stride
;
163 mt
->level
[0].image_offset
= CALLOC(1, sizeof(unsigned));
164 mt
->level
[0].tile_mode
= bo
->tile_mode
;
166 nouveau_bo_ref(bo
, &mt
->base
.bo
);
167 return &mt
->base
.base
;
171 nv50_miptree_destroy(struct pipe_texture
*pt
)
173 struct nv50_miptree
*mt
= nv50_miptree(pt
);
176 for (l
= 0; l
< pt
->last_level
; ++l
)
177 FREE(mt
->level
[l
].image_offset
);
179 nouveau_bo_ref(NULL
, &mt
->base
.bo
);
183 static struct pipe_surface
*
184 nv50_miptree_surface_new(struct pipe_screen
*pscreen
, struct pipe_texture
*pt
,
185 unsigned face
, unsigned level
, unsigned zslice
,
188 struct nv50_miptree
*mt
= nv50_miptree(pt
);
189 struct nv50_miptree_level
*lvl
= &mt
->level
[level
];
190 struct pipe_surface
*ps
;
193 if (pt
->target
== PIPE_TEXTURE_CUBE
)
196 if (pt
->target
== PIPE_TEXTURE_3D
)
201 ps
= CALLOC_STRUCT(pipe_surface
);
204 pipe_texture_reference(&ps
->texture
, pt
);
205 ps
->format
= pt
->format
;
206 ps
->width
= u_minify(pt
->width0
, level
);
207 ps
->height
= u_minify(pt
->height0
, level
);
209 pipe_reference_init(&ps
->reference
, 1);
213 ps
->offset
= lvl
->image_offset
[img
];
219 nv50_miptree_surface_del(struct pipe_surface
*ps
)
221 struct nv50_surface
*s
= nv50_surface(ps
);
223 pipe_texture_reference(&ps
->texture
, NULL
);
228 nv50_screen_init_miptree_functions(struct pipe_screen
*pscreen
)
230 pscreen
->texture_create
= nv50_miptree_create
;
231 pscreen
->texture_blanket
= nv50_miptree_blanket
;
232 pscreen
->texture_destroy
= nv50_miptree_destroy
;
233 pscreen
->get_tex_surface
= nv50_miptree_surface_new
;
234 pscreen
->tex_surface_destroy
= nv50_miptree_surface_del
;