1 #include "pipe/p_state.h"
2 #include "pipe/p_defines.h"
3 #include "pipe/p_inlines.h"
5 #include "nv20_context.h"
6 #include "nv20_screen.h"
9 nv20_miptree_layout(struct nv20_miptree
*nv20mt
)
11 struct pipe_texture
*pt
= &nv20mt
->base
;
12 uint width
= pt
->width
[0], height
= pt
->height
[0];
15 uint wide_pitch
= pt
->tex_usage
& (PIPE_TEXTURE_USAGE_SAMPLER
|
16 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
|
17 PIPE_TEXTURE_USAGE_RENDER_TARGET
|
18 PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
19 PIPE_TEXTURE_USAGE_PRIMARY
);
21 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
27 for (l
= 0; l
<= pt
->last_level
; l
++) {
29 pt
->height
[l
] = height
;
30 pt
->nblocksx
[l
] = pf_get_nblocksx(&pt
->block
, width
);
31 pt
->nblocksy
[l
] = pf_get_nblocksy(&pt
->block
, height
);
33 if (wide_pitch
&& (pt
->tex_usage
& NOUVEAU_TEXTURE_USAGE_LINEAR
))
34 nv20mt
->level
[l
].pitch
= align(pt
->width
[0] * pt
->block
.size
, 64);
36 nv20mt
->level
[l
].pitch
= pt
->width
[l
] * pt
->block
.size
;
38 nv20mt
->level
[l
].image_offset
=
39 CALLOC(nr_faces
, sizeof(unsigned));
41 width
= MAX2(1, width
>> 1);
42 height
= MAX2(1, height
>> 1);
45 for (f
= 0; f
< nr_faces
; f
++) {
46 for (l
= 0; l
< pt
->last_level
; l
++) {
47 nv20mt
->level
[l
].image_offset
[f
] = offset
;
49 if (!(pt
->tex_usage
& NOUVEAU_TEXTURE_USAGE_LINEAR
) &&
50 pt
->width
[l
+ 1] > 1 && pt
->height
[l
+ 1] > 1)
51 offset
+= align(nv20mt
->level
[l
].pitch
* pt
->height
[l
], 64);
53 offset
+= nv20mt
->level
[l
].pitch
* pt
->height
[l
];
56 nv20mt
->level
[l
].image_offset
[f
] = offset
;
57 offset
+= nv20mt
->level
[l
].pitch
* pt
->height
[l
];
60 nv20mt
->total_size
= offset
;
63 static struct pipe_texture
*
64 nv20_miptree_blanket(struct pipe_screen
*pscreen
, const struct pipe_texture
*pt
,
65 const unsigned *stride
, struct pipe_buffer
*pb
)
67 struct nv20_miptree
*mt
;
69 /* Only supports 2D, non-mipmapped textures for the moment */
70 if (pt
->target
!= PIPE_TEXTURE_2D
|| pt
->last_level
!= 0 ||
74 mt
= CALLOC_STRUCT(nv20_miptree
);
79 mt
->base
.refcount
= 1;
80 mt
->base
.screen
= pscreen
;
81 mt
->level
[0].pitch
= stride
[0];
82 mt
->level
[0].image_offset
= CALLOC(1, sizeof(unsigned));
84 pipe_buffer_reference(pscreen
, &mt
->buffer
, pb
);
88 static struct pipe_texture
*
89 nv20_miptree_create(struct pipe_screen
*screen
, const struct pipe_texture
*pt
)
91 struct pipe_winsys
*ws
= screen
->winsys
;
92 struct nv20_miptree
*mt
;
93 unsigned buf_usage
= PIPE_BUFFER_USAGE_PIXEL
|
94 NOUVEAU_BUFFER_USAGE_TEXTURE
;
96 mt
= MALLOC(sizeof(struct nv20_miptree
));
100 mt
->base
.refcount
= 1;
101 mt
->base
.screen
= screen
;
103 /* Swizzled textures must be POT */
104 if (pt
->width
[0] & (pt
->width
[0] - 1) ||
105 pt
->height
[0] & (pt
->height
[0] - 1))
106 mt
->base
.tex_usage
|= NOUVEAU_TEXTURE_USAGE_LINEAR
;
108 if (pt
->tex_usage
& (PIPE_TEXTURE_USAGE_PRIMARY
|
109 PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
110 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
))
111 mt
->base
.tex_usage
|= NOUVEAU_TEXTURE_USAGE_LINEAR
;
113 if (pt
->tex_usage
& PIPE_TEXTURE_USAGE_DYNAMIC
)
114 mt
->base
.tex_usage
|= NOUVEAU_TEXTURE_USAGE_LINEAR
;
116 switch (pt
->format
) {
117 /* TODO: Figure out which formats can be swizzled */
118 case PIPE_FORMAT_A8R8G8B8_UNORM
:
119 case PIPE_FORMAT_X8R8G8B8_UNORM
:
120 case PIPE_FORMAT_R16_SNORM
:
122 if (debug_get_bool_option("NOUVEAU_NO_SWIZZLE", FALSE
))
123 mt
->base
.tex_usage
|= NOUVEAU_TEXTURE_USAGE_LINEAR
;
127 mt
->base
.tex_usage
|= NOUVEAU_TEXTURE_USAGE_LINEAR
;
131 if (pt
->tex_usage
& PIPE_TEXTURE_USAGE_DYNAMIC
)
132 buf_usage
|= PIPE_BUFFER_USAGE_CPU_READ_WRITE
;
134 nv20_miptree_layout(mt
);
136 mt
->buffer
= ws
->buffer_create(ws
, 256, buf_usage
, mt
->total_size
);
146 nv20_miptree_release(struct pipe_screen
*screen
, struct pipe_texture
**pt
)
148 struct pipe_texture
*mt
= *pt
;
151 if (--mt
->refcount
<= 0) {
152 struct nv20_miptree
*nv20mt
= (struct nv20_miptree
*)mt
;
155 pipe_buffer_reference(screen
, &nv20mt
->buffer
, NULL
);
156 for (l
= 0; l
<= mt
->last_level
; l
++) {
157 if (nv20mt
->level
[l
].image_offset
)
158 FREE(nv20mt
->level
[l
].image_offset
);
164 static struct pipe_surface
*
165 nv20_miptree_surface_get(struct pipe_screen
*screen
, struct pipe_texture
*pt
,
166 unsigned face
, unsigned level
, unsigned zslice
,
169 struct nv20_miptree
*nv20mt
= (struct nv20_miptree
*)pt
;
170 struct nv04_surface
*ns
;
172 ns
= CALLOC_STRUCT(nv04_surface
);
175 pipe_texture_reference(&ns
->base
.texture
, pt
);
176 ns
->base
.format
= pt
->format
;
177 ns
->base
.width
= pt
->width
[level
];
178 ns
->base
.height
= pt
->height
[level
];
179 ns
->base
.usage
= flags
;
180 ns
->base
.status
= PIPE_SURFACE_STATUS_DEFINED
;
181 ns
->base
.refcount
= 1;
182 ns
->base
.face
= face
;
183 ns
->base
.level
= level
;
184 ns
->base
.zslice
= zslice
;
185 ns
->pitch
= nv20mt
->level
[level
].pitch
;
187 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
188 ns
->base
.offset
= nv20mt
->level
[level
].image_offset
[face
];
190 if (pt
->target
== PIPE_TEXTURE_3D
) {
191 ns
->base
.offset
= nv20mt
->level
[level
].image_offset
[zslice
];
193 ns
->base
.offset
= nv20mt
->level
[level
].image_offset
[0];
200 nv20_miptree_surface_release(struct pipe_screen
*pscreen
,
201 struct pipe_surface
**psurface
)
203 struct pipe_surface
*ps
= *psurface
;
206 if (--ps
->refcount
> 0)
209 pipe_texture_reference(&ps
->texture
, NULL
);
213 void nv20_screen_init_miptree_functions(struct pipe_screen
*pscreen
)
215 pscreen
->texture_create
= nv20_miptree_create
;
216 pscreen
->texture_blanket
= nv20_miptree_blanket
;
217 pscreen
->texture_release
= nv20_miptree_release
;
218 pscreen
->get_tex_surface
= nv20_miptree_surface_get
;
219 pscreen
->tex_surface_release
= nv20_miptree_surface_release
;