st/mesa: fix incorrect RowStride computation
[mesa.git] / src / gallium / drivers / nv40 / nv40_miptree.c
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"
6
7 #include "nv40_context.h"
8 #include "../nouveau/nv04_surface_2d.h"
9
10
11
12 static void
13 nv40_miptree_layout(struct nv40_miptree *mt)
14 {
15 struct pipe_texture *pt = &mt->base;
16 uint width = pt->width0;
17 uint offset = 0;
18 int nr_faces, l, f;
19 uint wide_pitch = pt->tex_usage & (PIPE_TEXTURE_USAGE_SAMPLER |
20 PIPE_TEXTURE_USAGE_DEPTH_STENCIL |
21 PIPE_TEXTURE_USAGE_RENDER_TARGET |
22 PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
23 PIPE_TEXTURE_USAGE_PRIMARY);
24
25 if (pt->target == PIPE_TEXTURE_CUBE) {
26 nr_faces = 6;
27 } else
28 if (pt->target == PIPE_TEXTURE_3D) {
29 nr_faces = pt->depth0;
30 } else {
31 nr_faces = 1;
32 }
33
34 for (l = 0; l <= pt->last_level; l++) {
35 if (wide_pitch && (pt->tex_usage & NOUVEAU_TEXTURE_USAGE_LINEAR))
36 mt->level[l].pitch = align(util_format_get_stride(pt->format, pt->width0), 64);
37 else
38 mt->level[l].pitch = util_format_get_stride(pt->format, width);
39
40 mt->level[l].image_offset =
41 CALLOC(nr_faces, sizeof(unsigned));
42
43 width = u_minify(width, 1);
44 }
45
46 for (f = 0; f < nr_faces; f++) {
47 for (l = 0; l < pt->last_level; l++) {
48 mt->level[l].image_offset[f] = offset;
49
50 if (!(pt->tex_usage & NOUVEAU_TEXTURE_USAGE_LINEAR) &&
51 u_minify(pt->width0, l + 1) > 1 && u_minify(pt->height0, l + 1) > 1)
52 offset += align(mt->level[l].pitch * u_minify(pt->height0, l), 64);
53 else
54 offset += mt->level[l].pitch * u_minify(pt->height0, l);
55 }
56
57 mt->level[l].image_offset[f] = offset;
58 offset += mt->level[l].pitch * u_minify(pt->height0, l);
59 }
60
61 mt->total_size = offset;
62 }
63
64 static struct pipe_texture *
65 nv40_miptree_create(struct pipe_screen *pscreen, const struct pipe_texture *pt)
66 {
67 struct nv40_miptree *mt;
68 unsigned buf_usage = PIPE_BUFFER_USAGE_PIXEL |
69 NOUVEAU_BUFFER_USAGE_TEXTURE;
70
71 mt = MALLOC(sizeof(struct nv40_miptree));
72 if (!mt)
73 return NULL;
74 mt->base = *pt;
75 pipe_reference_init(&mt->base.reference, 1);
76 mt->base.screen = pscreen;
77
78 /* Swizzled textures must be POT */
79 if (pt->width0 & (pt->width0 - 1) ||
80 pt->height0 & (pt->height0 - 1))
81 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
82 else
83 if (pt->tex_usage & (PIPE_TEXTURE_USAGE_PRIMARY |
84 PIPE_TEXTURE_USAGE_DISPLAY_TARGET |
85 PIPE_TEXTURE_USAGE_DEPTH_STENCIL))
86 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
87 else
88 if (pt->tex_usage & PIPE_TEXTURE_USAGE_DYNAMIC)
89 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
90 else {
91 switch (pt->format) {
92 /* TODO: Figure out which formats can be swizzled */
93 case PIPE_FORMAT_B8G8R8A8_UNORM:
94 case PIPE_FORMAT_B8G8R8X8_UNORM:
95 case PIPE_FORMAT_R16_SNORM:
96 {
97 if (debug_get_bool_option("NOUVEAU_NO_SWIZZLE", FALSE))
98 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
99 break;
100 }
101 default:
102 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
103 }
104 }
105
106 if (pt->tex_usage & PIPE_TEXTURE_USAGE_DYNAMIC)
107 buf_usage |= PIPE_BUFFER_USAGE_CPU_READ_WRITE;
108
109 /* apparently we can't render to swizzled surfaces smaller than 64 bytes, so make them linear.
110 * If the user did not ask for a render target, they can still render to it, but it will cost them an extra copy.
111 * This also happens for small mipmaps of large textures. */
112 if (pt->tex_usage & PIPE_TEXTURE_USAGE_RENDER_TARGET && util_format_get_stride(pt->format, pt->width0) < 64)
113 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
114
115 nv40_miptree_layout(mt);
116
117 mt->buffer = pscreen->buffer_create(pscreen, 256, buf_usage, mt->total_size);
118 if (!mt->buffer) {
119 FREE(mt);
120 return NULL;
121 }
122 mt->bo = nouveau_bo(mt->buffer);
123 return &mt->base;
124 }
125
126 static struct pipe_texture *
127 nv40_miptree_blanket(struct pipe_screen *pscreen, const struct pipe_texture *pt,
128 const unsigned *stride, struct pipe_buffer *pb)
129 {
130 struct nv40_miptree *mt;
131
132 /* Only supports 2D, non-mipmapped textures for the moment */
133 if (pt->target != PIPE_TEXTURE_2D || pt->last_level != 0 ||
134 pt->depth0 != 1)
135 return NULL;
136
137 mt = CALLOC_STRUCT(nv40_miptree);
138 if (!mt)
139 return NULL;
140
141 mt->base = *pt;
142 pipe_reference_init(&mt->base.reference, 1);
143 mt->base.screen = pscreen;
144 mt->level[0].pitch = stride[0];
145 mt->level[0].image_offset = CALLOC(1, sizeof(unsigned));
146
147 /* Assume whoever created this buffer expects it to be linear for now */
148 mt->base.tex_usage |= NOUVEAU_TEXTURE_USAGE_LINEAR;
149
150 pipe_buffer_reference(&mt->buffer, pb);
151 mt->bo = nouveau_bo(mt->buffer);
152 return &mt->base;
153 }
154
155 static void
156 nv40_miptree_destroy(struct pipe_texture *pt)
157 {
158 struct nv40_miptree *mt = (struct nv40_miptree *)pt;
159 int l;
160
161 pipe_buffer_reference(&mt->buffer, NULL);
162 for (l = 0; l <= pt->last_level; l++) {
163 if (mt->level[l].image_offset)
164 FREE(mt->level[l].image_offset);
165 }
166
167 FREE(mt);
168 }
169
170 static struct pipe_surface *
171 nv40_miptree_surface_new(struct pipe_screen *pscreen, struct pipe_texture *pt,
172 unsigned face, unsigned level, unsigned zslice,
173 unsigned flags)
174 {
175 struct nv40_miptree *mt = (struct nv40_miptree *)pt;
176 struct nv04_surface *ns;
177
178 ns = CALLOC_STRUCT(nv04_surface);
179 if (!ns)
180 return NULL;
181 pipe_texture_reference(&ns->base.texture, pt);
182 ns->base.format = pt->format;
183 ns->base.width = u_minify(pt->width0, level);
184 ns->base.height = u_minify(pt->height0, level);
185 ns->base.usage = flags;
186 pipe_reference_init(&ns->base.reference, 1);
187 ns->base.face = face;
188 ns->base.level = level;
189 ns->base.zslice = zslice;
190 ns->pitch = mt->level[level].pitch;
191
192 if (pt->target == PIPE_TEXTURE_CUBE) {
193 ns->base.offset = mt->level[level].image_offset[face];
194 } else
195 if (pt->target == PIPE_TEXTURE_3D) {
196 ns->base.offset = mt->level[level].image_offset[zslice];
197 } else {
198 ns->base.offset = mt->level[level].image_offset[0];
199 }
200
201 /* create a linear temporary that we can render into if necessary.
202 * Note that ns->pitch is always a multiple of 64 for linear surfaces and swizzled surfaces are POT, so
203 * ns->pitch & 63 is equivalent to (ns->pitch < 64 && swizzled)*/
204 if((ns->pitch & 63) && (ns->base.usage & (PIPE_BUFFER_USAGE_GPU_WRITE | NOUVEAU_BUFFER_USAGE_NO_RENDER)) == PIPE_BUFFER_USAGE_GPU_WRITE)
205 return &nv04_surface_wrap_for_render(pscreen, ((struct nv40_screen*)pscreen)->eng2d, ns)->base;
206
207 return &ns->base;
208 }
209
210 static void
211 nv40_miptree_surface_del(struct pipe_surface *ps)
212 {
213 struct nv04_surface* ns = (struct nv04_surface*)ps;
214 if(ns->backing)
215 {
216 struct nv40_screen* screen = (struct nv40_screen*)ps->texture->screen;
217 if(ns->backing->base.usage & PIPE_BUFFER_USAGE_GPU_WRITE)
218 screen->eng2d->copy(screen->eng2d, &ns->backing->base, 0, 0, ps, 0, 0, ns->base.width, ns->base.height);
219 nv40_miptree_surface_del(&ns->backing->base);
220 }
221
222 pipe_texture_reference(&ps->texture, NULL);
223 FREE(ps);
224 }
225
226 void
227 nv40_screen_init_miptree_functions(struct pipe_screen *pscreen)
228 {
229 pscreen->texture_create = nv40_miptree_create;
230 pscreen->texture_blanket = nv40_miptree_blanket;
231 pscreen->texture_destroy = nv40_miptree_destroy;
232 pscreen->get_tex_surface = nv40_miptree_surface_new;
233 pscreen->tex_surface_destroy = nv40_miptree_surface_del;
234 }
235