dri/nouveau: Flush after texture validation.
[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_fbo.h"
31 #include "nouveau_util.h"
32
33 #include "main/texobj.h"
34 #include "main/texstore.h"
35 #include "main/texformat.h"
36 #include "main/texcompress.h"
37 #include "main/texgetimage.h"
38 #include "main/mipmap.h"
39 #include "main/texfetch.h"
40 #include "main/teximage.h"
41
42 static struct gl_texture_object *
43 nouveau_texture_new(GLcontext *ctx, GLuint name, GLenum target)
44 {
45 struct nouveau_texture *nt = CALLOC_STRUCT(nouveau_texture);
46
47 _mesa_initialize_texture_object(&nt->base, name, target);
48
49 return &nt->base;
50 }
51
52 static void
53 nouveau_texture_free(GLcontext *ctx, struct gl_texture_object *t)
54 {
55 struct nouveau_texture *nt = to_nouveau_texture(t);
56 int i;
57
58 for (i = 0; i < MAX_TEXTURE_LEVELS; i++)
59 nouveau_surface_ref(NULL, &nt->surfaces[i]);
60
61 _mesa_delete_texture_object(ctx, t);
62 }
63
64 static struct gl_texture_image *
65 nouveau_teximage_new(GLcontext *ctx)
66 {
67 struct nouveau_teximage *nti = CALLOC_STRUCT(nouveau_teximage);
68
69 return &nti->base;
70 }
71
72 static void
73 nouveau_teximage_free(GLcontext *ctx, struct gl_texture_image *ti)
74 {
75 struct nouveau_teximage *nti = to_nouveau_teximage(ti);
76
77 nouveau_surface_ref(NULL, &nti->surface);
78 }
79
80 static void
81 nouveau_teximage_map(GLcontext *ctx, struct gl_texture_image *ti)
82 {
83 struct nouveau_surface *s = &to_nouveau_teximage(ti)->surface;
84 int ret;
85
86 if (s->bo) {
87 ret = nouveau_bo_map(s->bo, NOUVEAU_BO_RDWR);
88 assert(!ret);
89
90 ti->Data = s->bo->map;
91 }
92 }
93
94 static void
95 nouveau_teximage_unmap(GLcontext *ctx, struct gl_texture_image *ti)
96 {
97 struct nouveau_surface *s = &to_nouveau_teximage(ti)->surface;
98
99 if (s->bo)
100 nouveau_bo_unmap(s->bo);
101 ti->Data = NULL;
102 }
103
104 static gl_format
105 nouveau_choose_tex_format(GLcontext *ctx, GLint internalFormat,
106 GLenum srcFormat, GLenum srcType)
107 {
108 switch (internalFormat) {
109 case 4:
110 case GL_RGBA:
111 case GL_RGBA2:
112 case GL_RGBA4:
113 case GL_RGBA8:
114 case GL_RGBA12:
115 case GL_RGBA16:
116 case GL_RGB10_A2:
117 return MESA_FORMAT_ARGB8888;
118 case GL_RGB5_A1:
119 return MESA_FORMAT_ARGB1555;
120
121 case GL_RGB:
122 case GL_RGB8:
123 case GL_RGB10:
124 case GL_RGB12:
125 case GL_RGB16:
126 return MESA_FORMAT_XRGB8888;
127 case 3:
128 case GL_R3_G3_B2:
129 case GL_RGB4:
130 case GL_RGB5:
131 return MESA_FORMAT_RGB565;
132
133 case 2:
134 case GL_LUMINANCE_ALPHA:
135 case GL_LUMINANCE4_ALPHA4:
136 case GL_LUMINANCE6_ALPHA2:
137 case GL_LUMINANCE12_ALPHA4:
138 case GL_LUMINANCE12_ALPHA12:
139 case GL_LUMINANCE16_ALPHA16:
140 case GL_LUMINANCE8_ALPHA8:
141 return MESA_FORMAT_ARGB8888;
142
143 case 1:
144 case GL_LUMINANCE:
145 case GL_LUMINANCE4:
146 case GL_LUMINANCE12:
147 case GL_LUMINANCE16:
148 case GL_LUMINANCE8:
149 return MESA_FORMAT_L8;
150
151 case GL_ALPHA:
152 case GL_ALPHA4:
153 case GL_ALPHA12:
154 case GL_ALPHA16:
155 case GL_ALPHA8:
156 return MESA_FORMAT_A8;
157
158 case GL_INTENSITY:
159 case GL_INTENSITY4:
160 case GL_INTENSITY12:
161 case GL_INTENSITY16:
162 case GL_INTENSITY8:
163 return MESA_FORMAT_I8;
164
165 case GL_COLOR_INDEX:
166 case GL_COLOR_INDEX1_EXT:
167 case GL_COLOR_INDEX2_EXT:
168 case GL_COLOR_INDEX4_EXT:
169 case GL_COLOR_INDEX12_EXT:
170 case GL_COLOR_INDEX16_EXT:
171 case GL_COLOR_INDEX8_EXT:
172 return MESA_FORMAT_CI8;
173
174 default:
175 assert(0);
176 }
177 }
178
179 static GLboolean
180 teximage_fits(struct gl_texture_object *t, int level)
181 {
182 struct nouveau_surface *s = &to_nouveau_texture(t)->surfaces[level];
183 struct gl_texture_image *ti = t->Image[0][level];
184
185 return ti && (t->Target == GL_TEXTURE_RECTANGLE ||
186 (s->bo && s->width == ti->Width &&
187 s->height == ti->Height &&
188 s->format == ti->TexFormat));
189 }
190
191 static GLboolean
192 validate_teximage(GLcontext *ctx, struct gl_texture_object *t,
193 int level, int x, int y, int z,
194 int width, int height, int depth)
195 {
196 struct gl_texture_image *ti = t->Image[0][level];
197
198 if (teximage_fits(t, level)) {
199 struct nouveau_surface *ss = to_nouveau_texture(t)->surfaces;
200 struct nouveau_surface *s = &to_nouveau_teximage(ti)->surface;
201
202 if (t->Target == GL_TEXTURE_RECTANGLE)
203 nouveau_surface_ref(s, &ss[level]);
204 else
205 context_drv(ctx)->surface_copy(ctx, &ss[level], s,
206 x, y, x, y,
207 width, height);
208
209 return GL_TRUE;
210 }
211
212 return GL_FALSE;
213 }
214
215 static int
216 get_last_level(struct gl_texture_object *t)
217 {
218 struct gl_texture_image *base = t->Image[0][t->BaseLevel];
219
220 if (t->MinFilter == GL_NEAREST ||
221 t->MinFilter == GL_LINEAR || !base)
222 return t->BaseLevel;
223 else
224 return MIN2(t->BaseLevel + base->MaxLog2, t->MaxLevel);
225 }
226
227 static void
228 relayout_texture(GLcontext *ctx, struct gl_texture_object *t)
229 {
230 struct gl_texture_image *base = t->Image[0][t->BaseLevel];
231
232 if (base && t->Target != GL_TEXTURE_RECTANGLE) {
233 struct nouveau_surface *ss = to_nouveau_texture(t)->surfaces;
234 struct nouveau_surface *s = &to_nouveau_teximage(base)->surface;
235 int i, ret, last = get_last_level(t);
236 unsigned size, offset = 0,
237 width = s->width,
238 height = s->height;
239
240 /* Deallocate the old storage. */
241 for (i = 0; i < MAX_TEXTURE_LEVELS; i++)
242 nouveau_bo_ref(NULL, &ss[i].bo);
243
244 /* Relayout the mipmap tree. */
245 for (i = t->BaseLevel; i <= last; i++) {
246 size = width * height * s->cpp;
247
248 /* Images larger than 16B have to be aligned. */
249 if (size > 16)
250 offset = align(offset, 64);
251
252 ss[i] = (struct nouveau_surface) {
253 .offset = offset,
254 .layout = SWIZZLED,
255 .format = s->format,
256 .width = width,
257 .height = height,
258 .cpp = s->cpp,
259 .pitch = width * s->cpp,
260 };
261
262 offset += size;
263 width = MAX2(1, width / 2);
264 height = MAX2(1, height / 2);
265 }
266
267 /* Get new storage. */
268 size = align(offset, 64);
269
270 ret = nouveau_bo_new(context_dev(ctx), NOUVEAU_BO_MAP |
271 NOUVEAU_BO_GART | NOUVEAU_BO_VRAM,
272 0, size, &ss[last].bo);
273 assert(!ret);
274
275 for (i = t->BaseLevel; i < last; i++)
276 nouveau_bo_ref(ss[last].bo, &ss[i].bo);
277 }
278 }
279
280 GLboolean
281 nouveau_texture_validate(GLcontext *ctx, struct gl_texture_object *t)
282 {
283 struct nouveau_texture *nt = to_nouveau_texture(t);
284 int i, last = get_last_level(t);
285
286 if (!nt->surfaces[last].bo)
287 return GL_FALSE;
288
289 if (nt->dirty) {
290 nt->dirty = GL_FALSE;
291
292 /* Copy the teximages to the actual miptree. */
293 for (i = t->BaseLevel; i <= last; i++) {
294 struct nouveau_surface *s = &nt->surfaces[i];
295
296 validate_teximage(ctx, t, i, 0, 0, 0,
297 s->width, s->height, 1);
298 }
299
300 FIRE_RING(context_chan(ctx));
301 }
302
303 return GL_TRUE;
304 }
305
306 void
307 nouveau_texture_reallocate(GLcontext *ctx, struct gl_texture_object *t)
308 {
309 if (!teximage_fits(t, t->BaseLevel) ||
310 !teximage_fits(t, get_last_level(t))) {
311 texture_dirty(t);
312 relayout_texture(ctx, t);
313 nouveau_texture_validate(ctx, t);
314 }
315 }
316
317 static unsigned
318 get_teximage_placement(struct gl_texture_image *ti)
319 {
320 if (ti->TexFormat == MESA_FORMAT_A8 ||
321 ti->TexFormat == MESA_FORMAT_L8 ||
322 ti->TexFormat == MESA_FORMAT_I8)
323 /* 1 cpp formats will have to be swizzled by the CPU,
324 * so leave them in system RAM for now. */
325 return NOUVEAU_BO_MAP;
326 else
327 return NOUVEAU_BO_GART | NOUVEAU_BO_MAP;
328 }
329
330 static void
331 nouveau_teximage(GLcontext *ctx, GLint dims, GLenum target, GLint level,
332 GLint internalFormat,
333 GLint width, GLint height, GLint depth, GLint border,
334 GLenum format, GLenum type, const GLvoid *pixels,
335 const struct gl_pixelstore_attrib *packing,
336 struct gl_texture_object *t,
337 struct gl_texture_image *ti)
338 {
339 struct nouveau_surface *s = &to_nouveau_teximage(ti)->surface;
340 int ret;
341
342 /* Allocate a new bo for the image. */
343 nouveau_surface_alloc(ctx, s, LINEAR, get_teximage_placement(ti),
344 ti->TexFormat, width, height);
345 ti->RowStride = s->pitch / s->cpp;
346
347 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, depth,
348 format, type, pixels, packing,
349 "glTexImage");
350 if (pixels) {
351 /* Store the pixel data. */
352 nouveau_teximage_map(ctx, ti);
353
354 ret = _mesa_texstore(ctx, dims, ti->_BaseFormat,
355 ti->TexFormat, ti->Data,
356 0, 0, 0, s->pitch,
357 ti->ImageOffsets,
358 width, height, depth,
359 format, type, pixels, packing);
360 assert(ret);
361
362 nouveau_teximage_unmap(ctx, ti);
363 _mesa_unmap_teximage_pbo(ctx, packing);
364
365 if (!validate_teximage(ctx, t, level, 0, 0, 0,
366 width, height, depth))
367 /* It doesn't fit, mark it as dirty. */
368 texture_dirty(t);
369 }
370
371 if (level == t->BaseLevel) {
372 if (!teximage_fits(t, level))
373 relayout_texture(ctx, t);
374 nouveau_texture_validate(ctx, t);
375 }
376
377 context_dirty_i(ctx, TEX_OBJ, ctx->Texture.CurrentUnit);
378 context_dirty_i(ctx, TEX_ENV, ctx->Texture.CurrentUnit);
379 }
380
381 static void
382 nouveau_teximage_1d(GLcontext *ctx, GLenum target, GLint level,
383 GLint internalFormat,
384 GLint width, GLint border,
385 GLenum format, GLenum type, const GLvoid *pixels,
386 const struct gl_pixelstore_attrib *packing,
387 struct gl_texture_object *t,
388 struct gl_texture_image *ti)
389 {
390 nouveau_teximage(ctx, 1, target, level, internalFormat,
391 width, 1, 1, border, format, type, pixels,
392 packing, t, ti);
393 }
394
395 static void
396 nouveau_teximage_2d(GLcontext *ctx, GLenum target, GLint level,
397 GLint internalFormat,
398 GLint width, GLint height, GLint border,
399 GLenum format, GLenum type, const GLvoid *pixels,
400 const struct gl_pixelstore_attrib *packing,
401 struct gl_texture_object *t,
402 struct gl_texture_image *ti)
403 {
404 nouveau_teximage(ctx, 2, target, level, internalFormat,
405 width, height, 1, border, format, type, pixels,
406 packing, t, ti);
407 }
408
409 static void
410 nouveau_teximage_3d(GLcontext *ctx, GLenum target, GLint level,
411 GLint internalFormat,
412 GLint width, GLint height, GLint depth, GLint border,
413 GLenum format, GLenum type, const GLvoid *pixels,
414 const struct gl_pixelstore_attrib *packing,
415 struct gl_texture_object *t,
416 struct gl_texture_image *ti)
417 {
418 nouveau_teximage(ctx, 3, target, level, internalFormat,
419 width, height, depth, border, format, type, pixels,
420 packing, t, ti);
421 }
422
423 static void
424 nouveau_texsubimage_3d(GLcontext *ctx, GLenum target, GLint level,
425 GLint xoffset, GLint yoffset, GLint zoffset,
426 GLint width, GLint height, GLint depth,
427 GLenum format, GLenum type, const void *pixels,
428 const struct gl_pixelstore_attrib *packing,
429 struct gl_texture_object *t,
430 struct gl_texture_image *ti)
431 {
432 nouveau_teximage_map(ctx, ti);
433 _mesa_store_texsubimage3d(ctx, target, level, xoffset, yoffset, zoffset,
434 width, height, depth, format, type, pixels,
435 packing, t, ti);
436 nouveau_teximage_unmap(ctx, ti);
437
438 if (!to_nouveau_texture(t)->dirty)
439 validate_teximage(ctx, t, level, xoffset, yoffset, zoffset,
440 width, height, depth);
441 }
442
443 static void
444 nouveau_texsubimage_2d(GLcontext *ctx, GLenum target, GLint level,
445 GLint xoffset, GLint yoffset,
446 GLint width, GLint height,
447 GLenum format, GLenum type, const void *pixels,
448 const struct gl_pixelstore_attrib *packing,
449 struct gl_texture_object *t,
450 struct gl_texture_image *ti)
451 {
452 nouveau_teximage_map(ctx, ti);
453 _mesa_store_texsubimage2d(ctx, target, level, xoffset, yoffset,
454 width, height, format, type, pixels,
455 packing, t, ti);
456 nouveau_teximage_unmap(ctx, ti);
457
458 if (!to_nouveau_texture(t)->dirty)
459 validate_teximage(ctx, t, level, xoffset, yoffset, 0,
460 width, height, 1);
461 }
462
463 static void
464 nouveau_texsubimage_1d(GLcontext *ctx, GLenum target, GLint level,
465 GLint xoffset, GLint width,
466 GLenum format, GLenum type, const void *pixels,
467 const struct gl_pixelstore_attrib *packing,
468 struct gl_texture_object *t,
469 struct gl_texture_image *ti)
470 {
471 nouveau_teximage_map(ctx, ti);
472 _mesa_store_texsubimage1d(ctx, target, level, xoffset,
473 width, format, type, pixels,
474 packing, t, ti);
475 nouveau_teximage_unmap(ctx, ti);
476
477 if (!to_nouveau_texture(t)->dirty)
478 validate_teximage(ctx, t, level, xoffset, 0, 0,
479 width, 1, 1);
480 }
481
482 static void
483 nouveau_get_teximage(GLcontext *ctx, GLenum target, GLint level,
484 GLenum format, GLenum type, GLvoid *pixels,
485 struct gl_texture_object *t,
486 struct gl_texture_image *ti)
487 {
488 nouveau_teximage_map(ctx, ti);
489 _mesa_get_teximage(ctx, target, level, format, type, pixels,
490 t, ti);
491 nouveau_teximage_unmap(ctx, ti);
492 }
493
494 static void
495 nouveau_bind_texture(GLcontext *ctx, GLenum target,
496 struct gl_texture_object *t)
497 {
498 context_dirty_i(ctx, TEX_OBJ, ctx->Texture.CurrentUnit);
499 context_dirty_i(ctx, TEX_ENV, ctx->Texture.CurrentUnit);
500 }
501
502 static gl_format
503 get_texbuffer_format(struct gl_renderbuffer *rb, GLint format)
504 {
505 struct nouveau_surface *s = &to_nouveau_renderbuffer(rb)->surface;
506
507 if (s->cpp < 4)
508 return s->format;
509 else if (format == __DRI_TEXTURE_FORMAT_RGBA)
510 return MESA_FORMAT_ARGB8888;
511 else
512 return MESA_FORMAT_XRGB8888;
513 }
514
515 void
516 nouveau_set_texbuffer(__DRIcontext *dri_ctx,
517 GLint target, GLint format,
518 __DRIdrawable *draw)
519 {
520 struct nouveau_context *nctx = dri_ctx->driverPrivate;
521 GLcontext *ctx = &nctx->base;
522 struct gl_framebuffer *fb = draw->driverPrivate;
523 struct gl_renderbuffer *rb =
524 fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer;
525 struct gl_texture_object *t = _mesa_get_current_tex_object(ctx, target);
526 struct gl_texture_image *ti;
527 struct nouveau_surface *s;
528
529 _mesa_lock_texture(ctx, t);
530 ti = _mesa_get_tex_image(ctx, t, target, 0);
531 s = &to_nouveau_teximage(ti)->surface;
532
533 /* Update the texture surface with the given drawable. */
534 nouveau_update_renderbuffers(dri_ctx, draw);
535 nouveau_surface_ref(&to_nouveau_renderbuffer(rb)->surface, s);
536
537 /* Update the image fields. */
538 _mesa_init_teximage_fields(ctx, target, ti, s->width, s->height,
539 1, 0, s->cpp);
540 ti->RowStride = s->pitch / s->cpp;
541 ti->TexFormat = s->format = get_texbuffer_format(rb, format);
542
543 /* Try to validate it. */
544 if (!validate_teximage(ctx, t, 0, 0, 0, 0, s->width, s->height, 1))
545 nouveau_texture_reallocate(ctx, t);
546
547 context_dirty_i(ctx, TEX_OBJ, ctx->Texture.CurrentUnit);
548 context_dirty_i(ctx, TEX_ENV, ctx->Texture.CurrentUnit);
549
550 _mesa_unlock_texture(ctx, t);
551 }
552
553 static void
554 nouveau_texture_map(GLcontext *ctx, struct gl_texture_object *t)
555 {
556 int i;
557
558 for (i = t->BaseLevel; i < t->_MaxLevel; i++) {
559 if (t->Image[0][i])
560 nouveau_teximage_map(ctx, t->Image[0][i]);
561 }
562 }
563
564 static void
565 nouveau_texture_unmap(GLcontext *ctx, struct gl_texture_object *t)
566 {
567 int i;
568
569 for (i = t->BaseLevel; i < t->_MaxLevel; i++) {
570 if (t->Image[0][i])
571 nouveau_teximage_unmap(ctx, t->Image[0][i]);
572 }
573 }
574
575 void
576 nouveau_texture_functions_init(struct dd_function_table *functions)
577 {
578 functions->NewTextureObject = nouveau_texture_new;
579 functions->DeleteTexture = nouveau_texture_free;
580 functions->NewTextureImage = nouveau_teximage_new;
581 functions->FreeTexImageData = nouveau_teximage_free;
582 functions->ChooseTextureFormat = nouveau_choose_tex_format;
583 functions->TexImage1D = nouveau_teximage_1d;
584 functions->TexImage2D = nouveau_teximage_2d;
585 functions->TexImage3D = nouveau_teximage_3d;
586 functions->TexSubImage1D = nouveau_texsubimage_1d;
587 functions->TexSubImage2D = nouveau_texsubimage_2d;
588 functions->TexSubImage3D = nouveau_texsubimage_3d;
589 functions->GetTexImage = nouveau_get_teximage;
590 functions->BindTexture = nouveau_bind_texture;
591 functions->MapTexture = nouveau_texture_map;
592 functions->UnmapTexture = nouveau_texture_unmap;
593 }