2 * Copyright (C) 2009 Francisco Jerez.
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:
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.
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.
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"
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 #include "drivers/common/meta.h"
43 static struct gl_texture_object
*
44 nouveau_texture_new(GLcontext
*ctx
, GLuint name
, GLenum target
)
46 struct nouveau_texture
*nt
= CALLOC_STRUCT(nouveau_texture
);
48 _mesa_initialize_texture_object(&nt
->base
, name
, target
);
54 nouveau_texture_free(GLcontext
*ctx
, struct gl_texture_object
*t
)
56 struct nouveau_texture
*nt
= to_nouveau_texture(t
);
59 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++)
60 nouveau_surface_ref(NULL
, &nt
->surfaces
[i
]);
62 _mesa_delete_texture_object(ctx
, t
);
65 static struct gl_texture_image
*
66 nouveau_teximage_new(GLcontext
*ctx
)
68 struct nouveau_teximage
*nti
= CALLOC_STRUCT(nouveau_teximage
);
74 nouveau_teximage_free(GLcontext
*ctx
, struct gl_texture_image
*ti
)
76 struct nouveau_teximage
*nti
= to_nouveau_teximage(ti
);
78 nouveau_surface_ref(NULL
, &nti
->surface
);
82 nouveau_teximage_map(GLcontext
*ctx
, struct gl_texture_image
*ti
)
84 struct nouveau_surface
*s
= &to_nouveau_teximage(ti
)->surface
;
88 ret
= nouveau_bo_map(s
->bo
, NOUVEAU_BO_RDWR
);
91 ti
->Data
= s
->bo
->map
;
96 nouveau_teximage_unmap(GLcontext
*ctx
, struct gl_texture_image
*ti
)
98 struct nouveau_surface
*s
= &to_nouveau_teximage(ti
)->surface
;
101 nouveau_bo_unmap(s
->bo
);
106 nouveau_choose_tex_format(GLcontext
*ctx
, GLint internalFormat
,
107 GLenum srcFormat
, GLenum srcType
)
109 switch (internalFormat
) {
118 return MESA_FORMAT_ARGB8888
;
120 return MESA_FORMAT_ARGB1555
;
127 return MESA_FORMAT_XRGB8888
;
132 return MESA_FORMAT_RGB565
;
135 case GL_LUMINANCE_ALPHA
:
136 case GL_LUMINANCE4_ALPHA4
:
137 case GL_LUMINANCE6_ALPHA2
:
138 case GL_LUMINANCE12_ALPHA4
:
139 case GL_LUMINANCE12_ALPHA12
:
140 case GL_LUMINANCE16_ALPHA16
:
141 case GL_LUMINANCE8_ALPHA8
:
142 return MESA_FORMAT_ARGB8888
;
150 return MESA_FORMAT_L8
;
157 return MESA_FORMAT_A8
;
164 return MESA_FORMAT_I8
;
167 case GL_COLOR_INDEX1_EXT
:
168 case GL_COLOR_INDEX2_EXT
:
169 case GL_COLOR_INDEX4_EXT
:
170 case GL_COLOR_INDEX12_EXT
:
171 case GL_COLOR_INDEX16_EXT
:
172 case GL_COLOR_INDEX8_EXT
:
173 return MESA_FORMAT_CI8
;
181 teximage_fits(struct gl_texture_object
*t
, int level
)
183 struct nouveau_surface
*s
= &to_nouveau_texture(t
)->surfaces
[level
];
184 struct gl_texture_image
*ti
= t
->Image
[0][level
];
186 if (!ti
|| !to_nouveau_teximage(ti
)->surface
.bo
)
189 if (level
== t
->BaseLevel
&& (s
->offset
& 0x7f))
192 return t
->Target
== GL_TEXTURE_RECTANGLE
||
193 (s
->bo
&& s
->format
== ti
->TexFormat
&&
194 s
->width
== ti
->Width
&& s
->height
== ti
->Height
);
198 validate_teximage(GLcontext
*ctx
, struct gl_texture_object
*t
,
199 int level
, int x
, int y
, int z
,
200 int width
, int height
, int depth
)
202 struct gl_texture_image
*ti
= t
->Image
[0][level
];
204 if (teximage_fits(t
, level
)) {
205 struct nouveau_surface
*ss
= to_nouveau_texture(t
)->surfaces
;
206 struct nouveau_surface
*s
= &to_nouveau_teximage(ti
)->surface
;
208 if (t
->Target
== GL_TEXTURE_RECTANGLE
)
209 nouveau_surface_ref(s
, &ss
[level
]);
211 context_drv(ctx
)->surface_copy(ctx
, &ss
[level
], s
,
222 get_last_level(struct gl_texture_object
*t
)
224 struct gl_texture_image
*base
= t
->Image
[0][t
->BaseLevel
];
226 if (t
->MinFilter
== GL_NEAREST
||
227 t
->MinFilter
== GL_LINEAR
|| !base
)
230 return MIN2(t
->BaseLevel
+ base
->MaxLog2
, t
->MaxLevel
);
234 relayout_texture(GLcontext
*ctx
, struct gl_texture_object
*t
)
236 struct gl_texture_image
*base
= t
->Image
[0][t
->BaseLevel
];
238 if (base
&& t
->Target
!= GL_TEXTURE_RECTANGLE
) {
239 struct nouveau_surface
*ss
= to_nouveau_texture(t
)->surfaces
;
240 struct nouveau_surface
*s
= &to_nouveau_teximage(base
)->surface
;
241 int i
, ret
, last
= get_last_level(t
);
242 unsigned size
, offset
= 0,
246 /* Deallocate the old storage. */
247 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++)
248 nouveau_bo_ref(NULL
, &ss
[i
].bo
);
250 /* Relayout the mipmap tree. */
251 for (i
= t
->BaseLevel
; i
<= last
; i
++) {
252 size
= width
* height
* s
->cpp
;
254 /* Images larger than 16B have to be aligned. */
256 offset
= align(offset
, 64);
258 ss
[i
] = (struct nouveau_surface
) {
265 .pitch
= width
* s
->cpp
,
269 width
= MAX2(1, width
/ 2);
270 height
= MAX2(1, height
/ 2);
273 /* Get new storage. */
274 size
= align(offset
, 64);
276 ret
= nouveau_bo_new(context_dev(ctx
), NOUVEAU_BO_MAP
|
277 NOUVEAU_BO_GART
| NOUVEAU_BO_VRAM
,
278 0, size
, &ss
[last
].bo
);
281 for (i
= t
->BaseLevel
; i
< last
; i
++)
282 nouveau_bo_ref(ss
[last
].bo
, &ss
[i
].bo
);
287 nouveau_texture_validate(GLcontext
*ctx
, struct gl_texture_object
*t
)
289 struct nouveau_texture
*nt
= to_nouveau_texture(t
);
290 int i
, last
= get_last_level(t
);
292 if (!teximage_fits(t
, t
->BaseLevel
) ||
293 !teximage_fits(t
, last
))
297 nt
->dirty
= GL_FALSE
;
299 /* Copy the teximages to the actual miptree. */
300 for (i
= t
->BaseLevel
; i
<= last
; i
++) {
301 struct nouveau_surface
*s
= &nt
->surfaces
[i
];
303 validate_teximage(ctx
, t
, i
, 0, 0, 0,
304 s
->width
, s
->height
, 1);
307 FIRE_RING(context_chan(ctx
));
314 nouveau_texture_reallocate(GLcontext
*ctx
, struct gl_texture_object
*t
)
316 if (!teximage_fits(t
, t
->BaseLevel
) ||
317 !teximage_fits(t
, get_last_level(t
))) {
319 relayout_texture(ctx
, t
);
320 nouveau_texture_validate(ctx
, t
);
325 get_teximage_placement(struct gl_texture_image
*ti
)
327 if (ti
->TexFormat
== MESA_FORMAT_A8
||
328 ti
->TexFormat
== MESA_FORMAT_L8
||
329 ti
->TexFormat
== MESA_FORMAT_I8
)
330 /* 1 cpp formats will have to be swizzled by the CPU,
331 * so leave them in system RAM for now. */
332 return NOUVEAU_BO_MAP
;
334 return NOUVEAU_BO_GART
| NOUVEAU_BO_MAP
;
338 nouveau_teximage(GLcontext
*ctx
, GLint dims
, GLenum target
, GLint level
,
339 GLint internalFormat
,
340 GLint width
, GLint height
, GLint depth
, GLint border
,
341 GLenum format
, GLenum type
, const GLvoid
*pixels
,
342 const struct gl_pixelstore_attrib
*packing
,
343 struct gl_texture_object
*t
,
344 struct gl_texture_image
*ti
)
346 struct nouveau_surface
*s
= &to_nouveau_teximage(ti
)->surface
;
349 /* Allocate a new bo for the image. */
350 nouveau_surface_alloc(ctx
, s
, LINEAR
, get_teximage_placement(ti
),
351 ti
->TexFormat
, width
, height
);
352 ti
->RowStride
= s
->pitch
/ s
->cpp
;
354 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
355 format
, type
, pixels
, packing
,
358 /* Store the pixel data. */
359 nouveau_teximage_map(ctx
, ti
);
361 ret
= _mesa_texstore(ctx
, dims
, ti
->_BaseFormat
,
362 ti
->TexFormat
, ti
->Data
,
365 width
, height
, depth
,
366 format
, type
, pixels
, packing
);
369 nouveau_teximage_unmap(ctx
, ti
);
370 _mesa_unmap_teximage_pbo(ctx
, packing
);
372 if (!validate_teximage(ctx
, t
, level
, 0, 0, 0,
373 width
, height
, depth
))
374 /* It doesn't fit, mark it as dirty. */
378 if (level
== t
->BaseLevel
) {
379 if (!teximage_fits(t
, level
))
380 relayout_texture(ctx
, t
);
381 nouveau_texture_validate(ctx
, t
);
384 context_dirty_i(ctx
, TEX_OBJ
, ctx
->Texture
.CurrentUnit
);
385 context_dirty_i(ctx
, TEX_ENV
, ctx
->Texture
.CurrentUnit
);
389 nouveau_teximage_1d(GLcontext
*ctx
, GLenum target
, GLint level
,
390 GLint internalFormat
,
391 GLint width
, GLint border
,
392 GLenum format
, GLenum type
, const GLvoid
*pixels
,
393 const struct gl_pixelstore_attrib
*packing
,
394 struct gl_texture_object
*t
,
395 struct gl_texture_image
*ti
)
397 nouveau_teximage(ctx
, 1, target
, level
, internalFormat
,
398 width
, 1, 1, border
, format
, type
, pixels
,
403 nouveau_teximage_2d(GLcontext
*ctx
, GLenum target
, GLint level
,
404 GLint internalFormat
,
405 GLint width
, GLint height
, GLint border
,
406 GLenum format
, GLenum type
, const GLvoid
*pixels
,
407 const struct gl_pixelstore_attrib
*packing
,
408 struct gl_texture_object
*t
,
409 struct gl_texture_image
*ti
)
411 nouveau_teximage(ctx
, 2, target
, level
, internalFormat
,
412 width
, height
, 1, border
, format
, type
, pixels
,
417 nouveau_teximage_3d(GLcontext
*ctx
, GLenum target
, GLint level
,
418 GLint internalFormat
,
419 GLint width
, GLint height
, GLint depth
, GLint border
,
420 GLenum format
, GLenum type
, const GLvoid
*pixels
,
421 const struct gl_pixelstore_attrib
*packing
,
422 struct gl_texture_object
*t
,
423 struct gl_texture_image
*ti
)
425 nouveau_teximage(ctx
, 3, target
, level
, internalFormat
,
426 width
, height
, depth
, border
, format
, type
, pixels
,
431 nouveau_texsubimage(GLcontext
*ctx
, GLint dims
, GLenum target
, GLint level
,
432 GLint xoffset
, GLint yoffset
, GLint zoffset
,
433 GLint width
, GLint height
, GLint depth
,
434 GLenum format
, GLenum type
, const void *pixels
,
435 const struct gl_pixelstore_attrib
*packing
,
436 struct gl_texture_object
*t
,
437 struct gl_texture_image
*ti
)
439 struct nouveau_surface
*s
= &to_nouveau_teximage(ti
)->surface
;
442 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
443 format
, type
, pixels
, packing
,
446 nouveau_teximage_map(ctx
, ti
);
448 ret
= _mesa_texstore(ctx
, 3, ti
->_BaseFormat
, ti
->TexFormat
,
449 ti
->Data
, xoffset
, yoffset
, zoffset
,
450 s
->pitch
, ti
->ImageOffsets
,
451 width
, height
, depth
, format
, type
,
455 nouveau_teximage_unmap(ctx
, ti
);
456 _mesa_unmap_teximage_pbo(ctx
, packing
);
459 if (!to_nouveau_texture(t
)->dirty
)
460 validate_teximage(ctx
, t
, level
, xoffset
, yoffset
, zoffset
,
461 width
, height
, depth
);
465 nouveau_texsubimage_3d(GLcontext
*ctx
, GLenum target
, GLint level
,
466 GLint xoffset
, GLint yoffset
, GLint zoffset
,
467 GLint width
, GLint height
, GLint depth
,
468 GLenum format
, GLenum type
, const void *pixels
,
469 const struct gl_pixelstore_attrib
*packing
,
470 struct gl_texture_object
*t
,
471 struct gl_texture_image
*ti
)
473 nouveau_texsubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
474 width
, height
, depth
, format
, type
, pixels
,
479 nouveau_texsubimage_2d(GLcontext
*ctx
, GLenum target
, GLint level
,
480 GLint xoffset
, GLint yoffset
,
481 GLint width
, GLint height
,
482 GLenum format
, GLenum type
, const void *pixels
,
483 const struct gl_pixelstore_attrib
*packing
,
484 struct gl_texture_object
*t
,
485 struct gl_texture_image
*ti
)
487 nouveau_texsubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
488 width
, height
, 1, format
, type
, pixels
,
493 nouveau_texsubimage_1d(GLcontext
*ctx
, GLenum target
, GLint level
,
494 GLint xoffset
, GLint width
,
495 GLenum format
, GLenum type
, const void *pixels
,
496 const struct gl_pixelstore_attrib
*packing
,
497 struct gl_texture_object
*t
,
498 struct gl_texture_image
*ti
)
500 nouveau_texsubimage(ctx
, 1, target
, level
, xoffset
, 0, 0,
501 width
, 1, 1, format
, type
, pixels
,
506 nouveau_get_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
507 GLenum format
, GLenum type
, GLvoid
*pixels
,
508 struct gl_texture_object
*t
,
509 struct gl_texture_image
*ti
)
511 nouveau_teximage_map(ctx
, ti
);
512 _mesa_get_teximage(ctx
, target
, level
, format
, type
, pixels
,
514 nouveau_teximage_unmap(ctx
, ti
);
518 nouveau_bind_texture(GLcontext
*ctx
, GLenum target
,
519 struct gl_texture_object
*t
)
521 context_dirty_i(ctx
, TEX_OBJ
, ctx
->Texture
.CurrentUnit
);
522 context_dirty_i(ctx
, TEX_ENV
, ctx
->Texture
.CurrentUnit
);
526 get_texbuffer_format(struct gl_renderbuffer
*rb
, GLint format
)
528 struct nouveau_surface
*s
= &to_nouveau_renderbuffer(rb
)->surface
;
532 else if (format
== __DRI_TEXTURE_FORMAT_RGBA
)
533 return MESA_FORMAT_ARGB8888
;
535 return MESA_FORMAT_XRGB8888
;
539 nouveau_set_texbuffer(__DRIcontext
*dri_ctx
,
540 GLint target
, GLint format
,
543 struct nouveau_context
*nctx
= dri_ctx
->driverPrivate
;
544 GLcontext
*ctx
= &nctx
->base
;
545 struct gl_framebuffer
*fb
= draw
->driverPrivate
;
546 struct gl_renderbuffer
*rb
=
547 fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
;
548 struct gl_texture_object
*t
= _mesa_get_current_tex_object(ctx
, target
);
549 struct gl_texture_image
*ti
;
550 struct nouveau_surface
*s
;
552 _mesa_lock_texture(ctx
, t
);
553 ti
= _mesa_get_tex_image(ctx
, t
, target
, 0);
554 s
= &to_nouveau_teximage(ti
)->surface
;
556 /* Update the texture surface with the given drawable. */
557 nouveau_update_renderbuffers(dri_ctx
, draw
);
558 nouveau_surface_ref(&to_nouveau_renderbuffer(rb
)->surface
, s
);
560 /* Update the image fields. */
561 _mesa_init_teximage_fields(ctx
, target
, ti
, s
->width
, s
->height
,
563 ti
->RowStride
= s
->pitch
/ s
->cpp
;
564 ti
->TexFormat
= s
->format
= get_texbuffer_format(rb
, format
);
566 /* Try to validate it. */
567 if (!validate_teximage(ctx
, t
, 0, 0, 0, 0, s
->width
, s
->height
, 1))
568 nouveau_texture_reallocate(ctx
, t
);
570 context_dirty_i(ctx
, TEX_OBJ
, ctx
->Texture
.CurrentUnit
);
571 context_dirty_i(ctx
, TEX_ENV
, ctx
->Texture
.CurrentUnit
);
573 _mesa_unlock_texture(ctx
, t
);
577 nouveau_texture_map(GLcontext
*ctx
, struct gl_texture_object
*t
)
581 for (i
= t
->BaseLevel
; i
< t
->_MaxLevel
; i
++) {
583 nouveau_teximage_map(ctx
, t
->Image
[0][i
]);
588 nouveau_texture_unmap(GLcontext
*ctx
, struct gl_texture_object
*t
)
592 for (i
= t
->BaseLevel
; i
< t
->_MaxLevel
; i
++) {
594 nouveau_teximage_unmap(ctx
, t
->Image
[0][i
]);
599 store_mipmap(GLcontext
*ctx
, GLenum target
, int first
, int last
,
600 struct gl_texture_object
*t
)
602 struct gl_pixelstore_attrib packing
= {
603 .BufferObj
= ctx
->Shared
->NullBufferObj
,
606 GLenum format
= t
->Image
[0][t
->BaseLevel
]->TexFormat
;
607 unsigned base_format
, type
, comps
;
610 base_format
= _mesa_get_format_base_format(format
);
611 _mesa_format_to_type_and_comps(format
, &type
, &comps
);
613 for (i
= first
; i
<= last
; i
++) {
614 struct gl_texture_image
*ti
= t
->Image
[0][i
];
615 void *data
= ti
->Data
;
617 nouveau_teximage(ctx
, 3, target
, i
, ti
->InternalFormat
,
618 ti
->Width
, ti
->Height
, ti
->Depth
,
619 ti
->Border
, base_format
, type
, data
,
622 _mesa_free_texmemory(data
);
627 nouveau_generate_mipmap(GLcontext
*ctx
, GLenum target
,
628 struct gl_texture_object
*t
)
630 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, t
)) {
631 struct gl_texture_image
*base
= t
->Image
[0][t
->BaseLevel
];
633 nouveau_teximage_map(ctx
, base
);
634 _mesa_generate_mipmap(ctx
, target
, t
);
635 nouveau_teximage_unmap(ctx
, base
);
637 store_mipmap(ctx
, target
, t
->BaseLevel
+ 1,
638 get_last_level(t
), t
);
641 _mesa_meta_GenerateMipmap(ctx
, target
, t
);
646 nouveau_texture_functions_init(struct dd_function_table
*functions
)
648 functions
->NewTextureObject
= nouveau_texture_new
;
649 functions
->DeleteTexture
= nouveau_texture_free
;
650 functions
->NewTextureImage
= nouveau_teximage_new
;
651 functions
->FreeTexImageData
= nouveau_teximage_free
;
652 functions
->ChooseTextureFormat
= nouveau_choose_tex_format
;
653 functions
->TexImage1D
= nouveau_teximage_1d
;
654 functions
->TexImage2D
= nouveau_teximage_2d
;
655 functions
->TexImage3D
= nouveau_teximage_3d
;
656 functions
->TexSubImage1D
= nouveau_texsubimage_1d
;
657 functions
->TexSubImage2D
= nouveau_texsubimage_2d
;
658 functions
->TexSubImage3D
= nouveau_texsubimage_3d
;
659 functions
->GetTexImage
= nouveau_get_teximage
;
660 functions
->BindTexture
= nouveau_bind_texture
;
661 functions
->MapTexture
= nouveau_texture_map
;
662 functions
->UnmapTexture
= nouveau_texture_unmap
;
663 functions
->GenerateMipmap
= nouveau_generate_mipmap
;