dri/nouveau: Support rectangle textures.
[mesa.git] / src / mesa / drivers / dri / nouveau / nouveau_texture.c
1 /*
2 * Copyright (C) 2009 Francisco Jerez.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 */
26
27 #include "nouveau_driver.h"
28 #include "nouveau_context.h"
29 #include "nouveau_texture.h"
30 #include "nouveau_util.h"
31
32 #include "main/texobj.h"
33 #include "main/texstore.h"
34 #include "main/texformat.h"
35 #include "main/texcompress.h"
36 #include "main/texgetimage.h"
37 #include "main/mipmap.h"
38 #include "main/texfetch.h"
39
40 static struct gl_texture_object *
41 nouveau_texture_new(GLcontext *ctx, GLuint name, GLenum target)
42 {
43 struct nouveau_texture *nt = CALLOC_STRUCT(nouveau_texture);
44
45 _mesa_initialize_texture_object(&nt->base, name, target);
46
47 return &nt->base;
48 }
49
50 static void
51 nouveau_texture_free(GLcontext *ctx, struct gl_texture_object *t)
52 {
53 struct nouveau_texture *nt = to_nouveau_texture(t);
54 int i;
55
56 for (i = 0; i < MAX_TEXTURE_LEVELS; i++)
57 nouveau_surface_ref(NULL, &nt->surfaces[i]);
58
59 _mesa_delete_texture_object(ctx, t);
60 }
61
62 static struct gl_texture_image *
63 nouveau_teximage_new(GLcontext *ctx)
64 {
65 struct nouveau_teximage *nti = CALLOC_STRUCT(nouveau_teximage);
66
67 return &nti->base;
68 }
69
70 static void
71 nouveau_teximage_free(GLcontext *ctx, struct gl_texture_image *ti)
72 {
73 struct nouveau_teximage *nti = to_nouveau_teximage(ti);
74
75 nouveau_surface_ref(NULL, &nti->surface);
76 }
77
78 static void
79 nouveau_teximage_map(GLcontext *ctx, struct gl_texture_image *ti)
80 {
81 struct nouveau_surface *s = &to_nouveau_teximage(ti)->surface;
82 int ret;
83
84 if (s->bo) {
85 ret = nouveau_bo_map(s->bo, NOUVEAU_BO_RDWR);
86 assert(!ret);
87
88 ti->Data = s->bo->map;
89 }
90 }
91
92 static void
93 nouveau_teximage_unmap(GLcontext *ctx, struct gl_texture_image *ti)
94 {
95 struct nouveau_surface *s = &to_nouveau_teximage(ti)->surface;
96
97 if (s->bo)
98 nouveau_bo_unmap(s->bo);
99 ti->Data = NULL;
100 }
101
102 static gl_format
103 nouveau_choose_tex_format(GLcontext *ctx, GLint internalFormat,
104 GLenum srcFormat, GLenum srcType)
105 {
106 switch (internalFormat) {
107 case 4:
108 case GL_RGBA:
109 case GL_RGB10_A2:
110 case GL_RGBA12:
111 case GL_RGBA16:
112 case GL_RGBA8:
113 case GL_RGB:
114 case GL_RGB8:
115 case GL_RGB10:
116 case GL_RGB12:
117 case GL_RGB16:
118 return MESA_FORMAT_ARGB8888;
119 case GL_RGB5_A1:
120 return MESA_FORMAT_ARGB1555;
121
122 case 3:
123 case GL_R3_G3_B2:
124 case GL_RGB4:
125 case GL_RGB5:
126 return MESA_FORMAT_RGB565;
127
128 case GL_ALPHA:
129 case GL_ALPHA4:
130 case GL_ALPHA12:
131 case GL_ALPHA16:
132 case GL_ALPHA8:
133 return MESA_FORMAT_A8;
134
135 case 1:
136 case GL_LUMINANCE:
137 case GL_LUMINANCE4:
138 case GL_LUMINANCE12:
139 case GL_LUMINANCE16:
140 case GL_LUMINANCE8:
141 return MESA_FORMAT_L8;
142
143 case 2:
144 case GL_LUMINANCE_ALPHA:
145 case GL_LUMINANCE4_ALPHA4:
146 case GL_LUMINANCE6_ALPHA2:
147 case GL_LUMINANCE12_ALPHA4:
148 case GL_LUMINANCE12_ALPHA12:
149 case GL_LUMINANCE16_ALPHA16:
150 case GL_LUMINANCE8_ALPHA8:
151 return MESA_FORMAT_ARGB8888;
152
153 case GL_INTENSITY:
154 case GL_INTENSITY4:
155 case GL_INTENSITY12:
156 case GL_INTENSITY16:
157 case GL_INTENSITY8:
158 return MESA_FORMAT_ARGB8888;
159
160 case GL_COLOR_INDEX:
161 case GL_COLOR_INDEX1_EXT:
162 case GL_COLOR_INDEX2_EXT:
163 case GL_COLOR_INDEX4_EXT:
164 case GL_COLOR_INDEX12_EXT:
165 case GL_COLOR_INDEX16_EXT:
166 case GL_COLOR_INDEX8_EXT:
167 return MESA_FORMAT_CI8;
168
169 default:
170 assert(0);
171 }
172 }
173
174 static GLboolean
175 teximage_fits(struct gl_texture_object *t, int level,
176 struct gl_texture_image *ti)
177 {
178 struct nouveau_surface *s = &to_nouveau_texture(t)->surfaces[level];
179
180 return t->Target == GL_TEXTURE_RECTANGLE ||
181 (s->bo && s->width == ti->Width &&
182 s->height == ti->Height &&
183 s->format == ti->TexFormat);
184 }
185
186 static GLboolean
187 validate_teximage(GLcontext *ctx, struct gl_texture_object *t,
188 int level, int x, int y, int z,
189 int width, int height, int depth)
190 {
191 struct gl_texture_image *ti = t->Image[0][level];
192
193 if (ti && teximage_fits(t, level, ti)) {
194 struct nouveau_surface *ss = to_nouveau_texture(t)->surfaces;
195 struct nouveau_surface *s = &to_nouveau_teximage(ti)->surface;
196
197 if (t->Target == GL_TEXTURE_RECTANGLE)
198 nouveau_surface_ref(s, &ss[level]);
199 else
200 context_drv(ctx)->surface_copy(ctx, &ss[level], s,
201 x, y, x, y,
202 width, height);
203
204 return GL_TRUE;
205 }
206
207 return GL_FALSE;
208 }
209
210 static int
211 get_last_level(struct gl_texture_object *t)
212 {
213 struct gl_texture_image *base = t->Image[0][t->BaseLevel];
214
215 if (t->MinFilter == GL_NEAREST ||
216 t->MinFilter == GL_LINEAR || !base)
217 return t->BaseLevel;
218 else
219 return MIN2(t->BaseLevel + base->MaxLog2, t->MaxLevel);
220 }
221
222 static void
223 relayout_texture(GLcontext *ctx, struct gl_texture_object *t)
224 {
225 struct gl_texture_image *base = t->Image[0][t->BaseLevel];
226
227 if (base && t->Target != GL_TEXTURE_RECTANGLE) {
228 struct nouveau_surface *ss = to_nouveau_texture(t)->surfaces;
229 struct nouveau_surface *s = &to_nouveau_teximage(base)->surface;
230 int i, ret, last = get_last_level(t);
231 unsigned size, offset = 0,
232 width = s->width,
233 height = s->height;
234
235 /* Deallocate the old storage. */
236 for (i = 0; i < MAX_TEXTURE_LEVELS; i++)
237 nouveau_bo_ref(NULL, &ss[i].bo);
238
239 /* Relayout the mipmap tree. */
240 for (i = t->BaseLevel; i <= last; i++) {
241 size = width * height * s->cpp;
242
243 /* Images larger than 16B have to be aligned. */
244 if (size > 16)
245 offset = align(offset, 64);
246
247 ss[i] = (struct nouveau_surface) {
248 .offset = offset,
249 .layout = SWIZZLED,
250 .format = s->format,
251 .width = width,
252 .height = height,
253 .cpp = s->cpp,
254 .pitch = width * s->cpp,
255 };
256
257 offset += size;
258 width = MAX2(1, width / 2);
259 height = MAX2(1, height / 2);
260 }
261
262 /* Get new storage. */
263 size = align(offset, 64);
264
265 ret = nouveau_bo_new(context_dev(ctx), NOUVEAU_BO_MAP |
266 NOUVEAU_BO_GART | NOUVEAU_BO_VRAM,
267 0, size, &ss[last].bo);
268 assert(!ret);
269
270 for (i = t->BaseLevel; i < last; i++)
271 nouveau_bo_ref(ss[last].bo, &ss[i].bo);
272 }
273 }
274
275 GLboolean
276 nouveau_texture_validate(GLcontext *ctx, struct gl_texture_object *t)
277 {
278 struct nouveau_texture *nt = to_nouveau_texture(t);
279 int i, last = get_last_level(t);
280
281 if (!nt->surfaces[last].bo)
282 return GL_FALSE;
283
284 if (nt->dirty) {
285 nt->dirty = GL_FALSE;
286
287 /* Copy the teximages to the actual miptree. */
288 for (i = t->BaseLevel; i <= last; i++) {
289 struct nouveau_surface *s = &nt->surfaces[i];
290
291 validate_teximage(ctx, t, i, 0, 0, 0,
292 s->width, s->height, 1);
293 }
294 }
295
296 return GL_TRUE;
297 }
298
299 void
300 nouveau_texture_reallocate(GLcontext *ctx, struct gl_texture_object *t)
301 {
302 texture_dirty(t);
303 relayout_texture(ctx, t);
304 nouveau_texture_validate(ctx, t);
305 }
306
307 static unsigned
308 get_teximage_placement(struct gl_texture_image *ti)
309 {
310 if (ti->TexFormat == MESA_FORMAT_A8 ||
311 ti->TexFormat == MESA_FORMAT_L8 ||
312 ti->TexFormat == MESA_FORMAT_I8)
313 /* 1 cpp formats will have to be swizzled by the CPU,
314 * so leave them in system RAM for now. */
315 return NOUVEAU_BO_MAP;
316 else
317 return NOUVEAU_BO_GART | NOUVEAU_BO_MAP;
318 }
319
320 static void
321 nouveau_teximage(GLcontext *ctx, GLint dims, GLenum target, GLint level,
322 GLint internalFormat,
323 GLint width, GLint height, GLint depth, GLint border,
324 GLenum format, GLenum type, const GLvoid *pixels,
325 const struct gl_pixelstore_attrib *packing,
326 struct gl_texture_object *t,
327 struct gl_texture_image *ti)
328 {
329 struct nouveau_surface *s = &to_nouveau_teximage(ti)->surface;
330 int ret;
331
332 /* Allocate a new bo for the image. */
333 nouveau_surface_alloc(ctx, s, LINEAR, get_teximage_placement(ti),
334 ti->TexFormat, width, height);
335 ti->RowStride = s->pitch / s->cpp;
336
337 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, depth,
338 format, type, pixels, packing,
339 "glTexImage");
340 if (pixels) {
341 /* Store the pixel data. */
342 nouveau_teximage_map(ctx, ti);
343
344 ret = _mesa_texstore(ctx, dims, ti->_BaseFormat,
345 ti->TexFormat, ti->Data,
346 0, 0, 0, s->pitch,
347 ti->ImageOffsets,
348 width, height, depth,
349 format, type, pixels, packing);
350 assert(ret);
351
352 nouveau_teximage_unmap(ctx, ti);
353 _mesa_unmap_teximage_pbo(ctx, packing);
354
355 if (!validate_teximage(ctx, t, level, 0, 0, 0,
356 width, height, depth))
357 /* It doesn't fit, mark it as dirty. */
358 texture_dirty(t);
359 }
360
361 if (level == t->BaseLevel) {
362 if (!teximage_fits(t, level, ti))
363 relayout_texture(ctx, t);
364 nouveau_texture_validate(ctx, t);
365 }
366
367 context_dirty_i(ctx, TEX_OBJ, ctx->Texture.CurrentUnit);
368 context_dirty_i(ctx, TEX_ENV, ctx->Texture.CurrentUnit);
369 }
370
371 static void
372 nouveau_teximage_1d(GLcontext *ctx, GLenum target, GLint level,
373 GLint internalFormat,
374 GLint width, GLint border,
375 GLenum format, GLenum type, const GLvoid *pixels,
376 const struct gl_pixelstore_attrib *packing,
377 struct gl_texture_object *t,
378 struct gl_texture_image *ti)
379 {
380 nouveau_teximage(ctx, 1, target, level, internalFormat,
381 width, 1, 1, border, format, type, pixels,
382 packing, t, ti);
383 }
384
385 static void
386 nouveau_teximage_2d(GLcontext *ctx, GLenum target, GLint level,
387 GLint internalFormat,
388 GLint width, GLint height, GLint border,
389 GLenum format, GLenum type, const GLvoid *pixels,
390 const struct gl_pixelstore_attrib *packing,
391 struct gl_texture_object *t,
392 struct gl_texture_image *ti)
393 {
394 nouveau_teximage(ctx, 2, target, level, internalFormat,
395 width, height, 1, border, format, type, pixels,
396 packing, t, ti);
397 }
398
399 static void
400 nouveau_teximage_3d(GLcontext *ctx, GLenum target, GLint level,
401 GLint internalFormat,
402 GLint width, GLint height, GLint depth, GLint border,
403 GLenum format, GLenum type, const GLvoid *pixels,
404 const struct gl_pixelstore_attrib *packing,
405 struct gl_texture_object *t,
406 struct gl_texture_image *ti)
407 {
408 nouveau_teximage(ctx, 3, target, level, internalFormat,
409 width, height, depth, border, format, type, pixels,
410 packing, t, ti);
411 }
412
413 static void
414 nouveau_texsubimage_3d(GLcontext *ctx, GLenum target, GLint level,
415 GLint xoffset, GLint yoffset, GLint zoffset,
416 GLint width, GLint height, GLint depth,
417 GLenum format, GLenum type, const void *pixels,
418 const struct gl_pixelstore_attrib *packing,
419 struct gl_texture_object *t,
420 struct gl_texture_image *ti)
421 {
422 nouveau_teximage_map(ctx, ti);
423 _mesa_store_texsubimage3d(ctx, target, level, xoffset, yoffset, zoffset,
424 width, height, depth, format, type, pixels,
425 packing, t, ti);
426 nouveau_teximage_unmap(ctx, ti);
427
428 if (!to_nouveau_texture(t)->dirty)
429 validate_teximage(ctx, t, level, xoffset, yoffset, zoffset,
430 width, height, depth);
431 }
432
433 static void
434 nouveau_texsubimage_2d(GLcontext *ctx, GLenum target, GLint level,
435 GLint xoffset, GLint yoffset,
436 GLint width, GLint height,
437 GLenum format, GLenum type, const void *pixels,
438 const struct gl_pixelstore_attrib *packing,
439 struct gl_texture_object *t,
440 struct gl_texture_image *ti)
441 {
442 nouveau_teximage_map(ctx, ti);
443 _mesa_store_texsubimage2d(ctx, target, level, xoffset, yoffset,
444 width, height, format, type, pixels,
445 packing, t, ti);
446 nouveau_teximage_unmap(ctx, ti);
447
448 if (!to_nouveau_texture(t)->dirty)
449 validate_teximage(ctx, t, level, xoffset, yoffset, 0,
450 width, height, 1);
451 }
452
453 static void
454 nouveau_texsubimage_1d(GLcontext *ctx, GLenum target, GLint level,
455 GLint xoffset, GLint width,
456 GLenum format, GLenum type, const void *pixels,
457 const struct gl_pixelstore_attrib *packing,
458 struct gl_texture_object *t,
459 struct gl_texture_image *ti)
460 {
461 nouveau_teximage_map(ctx, ti);
462 _mesa_store_texsubimage1d(ctx, target, level, xoffset,
463 width, format, type, pixels,
464 packing, t, ti);
465 nouveau_teximage_unmap(ctx, ti);
466
467 if (!to_nouveau_texture(t)->dirty)
468 validate_teximage(ctx, t, level, xoffset, 0, 0,
469 width, 1, 1);
470 }
471
472 static void
473 nouveau_get_teximage(GLcontext *ctx, GLenum target, GLint level,
474 GLenum format, GLenum type, GLvoid *pixels,
475 struct gl_texture_object *t,
476 struct gl_texture_image *ti)
477 {
478 nouveau_teximage_map(ctx, ti);
479 _mesa_get_teximage(ctx, target, level, format, type, pixels,
480 t, ti);
481 nouveau_teximage_unmap(ctx, ti);
482 }
483
484 static void
485 nouveau_bind_texture(GLcontext *ctx, GLenum target,
486 struct gl_texture_object *t)
487 {
488 context_dirty_i(ctx, TEX_OBJ, ctx->Texture.CurrentUnit);
489 context_dirty_i(ctx, TEX_ENV, ctx->Texture.CurrentUnit);
490 }
491
492 static void
493 nouveau_texture_map(GLcontext *ctx, struct gl_texture_object *t)
494 {
495 int i;
496
497 for (i = t->BaseLevel; i < t->_MaxLevel; i++) {
498 if (t->Image[0][i])
499 nouveau_teximage_map(ctx, t->Image[0][i]);
500 }
501 }
502
503 static void
504 nouveau_texture_unmap(GLcontext *ctx, struct gl_texture_object *t)
505 {
506 int i;
507
508 for (i = t->BaseLevel; i < t->_MaxLevel; i++) {
509 if (t->Image[0][i])
510 nouveau_teximage_unmap(ctx, t->Image[0][i]);
511 }
512 }
513
514 void
515 nouveau_texture_functions_init(struct dd_function_table *functions)
516 {
517 functions->NewTextureObject = nouveau_texture_new;
518 functions->DeleteTexture = nouveau_texture_free;
519 functions->NewTextureImage = nouveau_teximage_new;
520 functions->FreeTexImageData = nouveau_teximage_free;
521 functions->ChooseTextureFormat = nouveau_choose_tex_format;
522 functions->TexImage1D = nouveau_teximage_1d;
523 functions->TexImage2D = nouveau_teximage_2d;
524 functions->TexImage3D = nouveau_teximage_3d;
525 functions->TexSubImage1D = nouveau_texsubimage_1d;
526 functions->TexSubImage2D = nouveau_texsubimage_2d;
527 functions->TexSubImage3D = nouveau_texsubimage_3d;
528 functions->GetTexImage = nouveau_get_teximage;
529 functions->BindTexture = nouveau_bind_texture;
530 functions->MapTexture = nouveau_texture_map;
531 functions->UnmapTexture = nouveau_texture_unmap;
532 }