1 #include "pipe/p_state.h"
2 #include "pipe/p_defines.h"
3 #include "util/u_inlines.h"
4 #include "util/u_format.h"
5 #include "util/u_math.h"
7 #include "nv30_context.h"
8 #include "../nouveau/nv04_surface_2d.h"
11 nv30_miptree_layout(struct nv30_miptree
*nv30mt
)
13 struct pipe_texture
*pt
= &nv30mt
->base
;
14 uint width
= pt
->width0
;
17 uint wide_pitch
= pt
->tex_usage
& (PIPE_TEXTURE_USAGE_SAMPLER
|
18 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
|
19 PIPE_TEXTURE_USAGE_RENDER_TARGET
|
20 PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
21 PIPE_TEXTURE_USAGE_SCANOUT
);
23 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
26 if (pt
->target
== PIPE_TEXTURE_3D
) {
27 nr_faces
= pt
->depth0
;
32 for (l
= 0; l
<= pt
->last_level
; l
++) {
33 if (wide_pitch
&& (pt
->tex_usage
& NOUVEAU_TEXTURE_USAGE_LINEAR
))
34 nv30mt
->level
[l
].pitch
= align(util_format_get_stride(pt
->format
, pt
->width0
), 64);
36 nv30mt
->level
[l
].pitch
= util_format_get_stride(pt
->format
, width
);
38 nv30mt
->level
[l
].image_offset
=
39 CALLOC(nr_faces
, sizeof(unsigned));
41 width
= u_minify(width
, 1);
44 for (f
= 0; f
< nr_faces
; f
++) {
45 for (l
= 0; l
< pt
->last_level
; l
++) {
46 nv30mt
->level
[l
].image_offset
[f
] = offset
;
48 if (!(pt
->tex_usage
& NOUVEAU_TEXTURE_USAGE_LINEAR
) &&
49 u_minify(pt
->width0
, l
+ 1) > 1 && u_minify(pt
->height0
, l
+ 1) > 1)
50 offset
+= align(nv30mt
->level
[l
].pitch
* u_minify(pt
->height0
, l
), 64);
52 offset
+= nv30mt
->level
[l
].pitch
* u_minify(pt
->height0
, l
);
55 nv30mt
->level
[l
].image_offset
[f
] = offset
;
56 offset
+= nv30mt
->level
[l
].pitch
* u_minify(pt
->height0
, l
);
59 nv30mt
->total_size
= offset
;
62 static struct pipe_texture
*
63 nv30_miptree_create(struct pipe_screen
*pscreen
, const struct pipe_texture
*pt
)
65 struct nv30_miptree
*mt
;
66 unsigned buf_usage
= PIPE_BUFFER_USAGE_PIXEL
|
67 NOUVEAU_BUFFER_USAGE_TEXTURE
;
69 mt
= MALLOC(sizeof(struct nv30_miptree
));
73 pipe_reference_init(&mt
->base
.reference
, 1);
74 mt
->base
.screen
= pscreen
;
76 /* Swizzled textures must be POT */
77 if (pt
->width0
& (pt
->width0
- 1) ||
78 pt
->height0
& (pt
->height0
- 1))
79 mt
->base
.tex_usage
|= NOUVEAU_TEXTURE_USAGE_LINEAR
;
81 if (pt
->tex_usage
& (PIPE_TEXTURE_USAGE_SCANOUT
|
82 PIPE_TEXTURE_USAGE_DISPLAY_TARGET
|
83 PIPE_TEXTURE_USAGE_DEPTH_STENCIL
))
84 mt
->base
.tex_usage
|= NOUVEAU_TEXTURE_USAGE_LINEAR
;
86 if (pt
->tex_usage
& PIPE_TEXTURE_USAGE_DYNAMIC
)
87 mt
->base
.tex_usage
|= NOUVEAU_TEXTURE_USAGE_LINEAR
;
90 /* TODO: Figure out which formats can be swizzled */
91 case PIPE_FORMAT_B8G8R8A8_UNORM
:
92 case PIPE_FORMAT_B8G8R8X8_UNORM
:
93 case PIPE_FORMAT_R16_SNORM
:
94 case PIPE_FORMAT_B5G6R5_UNORM
:
95 case PIPE_FORMAT_L8A8_UNORM
:
96 case PIPE_FORMAT_A8_UNORM
:
97 case PIPE_FORMAT_L8_UNORM
:
98 case PIPE_FORMAT_I8_UNORM
:
100 if (debug_get_bool_option("NOUVEAU_NO_SWIZZLE", FALSE
))
101 mt
->base
.tex_usage
|= NOUVEAU_TEXTURE_USAGE_LINEAR
;
105 mt
->base
.tex_usage
|= NOUVEAU_TEXTURE_USAGE_LINEAR
;
109 if (pt
->tex_usage
& PIPE_TEXTURE_USAGE_DYNAMIC
)
110 buf_usage
|= PIPE_BUFFER_USAGE_CPU_READ_WRITE
;
112 /* apparently we can't render to swizzled surfaces smaller than 64 bytes, so make them linear.
113 * If the user did not ask for a render target, they can still render to it, but it will cost them an extra copy.
114 * This also happens for small mipmaps of large textures. */
115 if (pt
->tex_usage
& PIPE_TEXTURE_USAGE_RENDER_TARGET
&& util_format_get_stride(pt
->format
, pt
->width0
) < 64)
116 mt
->base
.tex_usage
|= NOUVEAU_TEXTURE_USAGE_LINEAR
;
118 nv30_miptree_layout(mt
);
120 mt
->buffer
= pscreen
->buffer_create(pscreen
, 256, buf_usage
,
126 mt
->bo
= nouveau_bo(mt
->buffer
);
131 static struct pipe_texture
*
132 nv30_miptree_blanket(struct pipe_screen
*pscreen
, const struct pipe_texture
*pt
,
133 const unsigned *stride
, struct pipe_buffer
*pb
)
135 struct nv30_miptree
*mt
;
137 /* Only supports 2D, non-mipmapped textures for the moment */
138 if (pt
->target
!= PIPE_TEXTURE_2D
|| pt
->last_level
!= 0 ||
142 mt
= CALLOC_STRUCT(nv30_miptree
);
147 pipe_reference_init(&mt
->base
.reference
, 1);
148 mt
->base
.screen
= pscreen
;
149 mt
->level
[0].pitch
= stride
[0];
150 mt
->level
[0].image_offset
= CALLOC(1, sizeof(unsigned));
152 /* Assume whoever created this buffer expects it to be linear for now */
153 mt
->base
.tex_usage
|= NOUVEAU_TEXTURE_USAGE_LINEAR
;
155 pipe_buffer_reference(&mt
->buffer
, pb
);
156 mt
->bo
= nouveau_bo(mt
->buffer
);
161 nv30_miptree_destroy(struct pipe_texture
*pt
)
163 struct nv30_miptree
*mt
= (struct nv30_miptree
*)pt
;
166 pipe_buffer_reference(&mt
->buffer
, NULL
);
167 for (l
= 0; l
<= pt
->last_level
; l
++) {
168 if (mt
->level
[l
].image_offset
)
169 FREE(mt
->level
[l
].image_offset
);
175 static struct pipe_surface
*
176 nv30_miptree_surface_new(struct pipe_screen
*pscreen
, struct pipe_texture
*pt
,
177 unsigned face
, unsigned level
, unsigned zslice
,
180 struct nv30_miptree
*nv30mt
= (struct nv30_miptree
*)pt
;
181 struct nv04_surface
*ns
;
183 ns
= CALLOC_STRUCT(nv04_surface
);
186 pipe_texture_reference(&ns
->base
.texture
, pt
);
187 ns
->base
.format
= pt
->format
;
188 ns
->base
.width
= u_minify(pt
->width0
, level
);
189 ns
->base
.height
= u_minify(pt
->height0
, level
);
190 ns
->base
.usage
= flags
;
191 pipe_reference_init(&ns
->base
.reference
, 1);
192 ns
->base
.face
= face
;
193 ns
->base
.level
= level
;
194 ns
->base
.zslice
= zslice
;
195 ns
->pitch
= nv30mt
->level
[level
].pitch
;
197 if (pt
->target
== PIPE_TEXTURE_CUBE
) {
198 ns
->base
.offset
= nv30mt
->level
[level
].image_offset
[face
];
200 if (pt
->target
== PIPE_TEXTURE_3D
) {
201 ns
->base
.offset
= nv30mt
->level
[level
].image_offset
[zslice
];
203 ns
->base
.offset
= nv30mt
->level
[level
].image_offset
[0];
206 /* create a linear temporary that we can render into if necessary.
207 * Note that ns->pitch is always a multiple of 64 for linear surfaces and swizzled surfaces are POT, so
208 * ns->pitch & 63 is equivalent to (ns->pitch < 64 && swizzled)*/
209 if((ns
->pitch
& 63) && (ns
->base
.usage
& (PIPE_BUFFER_USAGE_GPU_WRITE
| NOUVEAU_BUFFER_USAGE_NO_RENDER
)) == PIPE_BUFFER_USAGE_GPU_WRITE
)
210 return &nv04_surface_wrap_for_render(pscreen
, ((struct nv30_screen
*)pscreen
)->eng2d
, ns
)->base
;
216 nv30_miptree_surface_del(struct pipe_surface
*ps
)
218 struct nv04_surface
* ns
= (struct nv04_surface
*)ps
;
221 struct nv30_screen
* screen
= (struct nv30_screen
*)ps
->texture
->screen
;
222 if(ns
->backing
->base
.usage
& PIPE_BUFFER_USAGE_GPU_WRITE
)
223 screen
->eng2d
->copy(screen
->eng2d
, &ns
->backing
->base
, 0, 0, ps
, 0, 0, ns
->base
.width
, ns
->base
.height
);
224 nv30_miptree_surface_del(&ns
->backing
->base
);
227 pipe_texture_reference(&ps
->texture
, NULL
);
232 nv30_screen_init_miptree_functions(struct pipe_screen
*pscreen
)
234 pscreen
->texture_create
= nv30_miptree_create
;
235 pscreen
->texture_blanket
= nv30_miptree_blanket
;
236 pscreen
->texture_destroy
= nv30_miptree_destroy
;
237 pscreen
->get_tex_surface
= nv30_miptree_surface_new
;
238 pscreen
->tex_surface_destroy
= nv30_miptree_surface_del
;