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"
34 #include "main/texobj.h"
35 #include "main/texstore.h"
36 #include "main/texformat.h"
37 #include "main/texcompress.h"
38 #include "main/texgetimage.h"
39 #include "main/mipmap.h"
40 #include "main/teximage.h"
41 #include "drivers/common/meta.h"
42 #include "swrast/s_texfetch.h"
44 static struct gl_texture_object
*
45 nouveau_texture_new(struct gl_context
*ctx
, GLuint name
, GLenum target
)
47 struct nouveau_texture
*nt
= CALLOC_STRUCT(nouveau_texture
);
49 _mesa_initialize_texture_object(ctx
, &nt
->base
, name
, target
);
55 nouveau_texture_free(struct gl_context
*ctx
, struct gl_texture_object
*t
)
57 struct nouveau_texture
*nt
= to_nouveau_texture(t
);
60 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++)
61 nouveau_surface_ref(NULL
, &nt
->surfaces
[i
]);
63 _mesa_delete_texture_object(ctx
, t
);
66 static struct gl_texture_image
*
67 nouveau_teximage_new(struct gl_context
*ctx
)
69 struct nouveau_teximage
*nti
= CALLOC_STRUCT(nouveau_teximage
);
71 return &nti
->base
.Base
;
75 nouveau_teximage_free(struct gl_context
*ctx
, struct gl_texture_image
*ti
)
77 struct nouveau_teximage
*nti
= to_nouveau_teximage(ti
);
79 nouveau_surface_ref(NULL
, &nti
->surface
);
83 nouveau_map_texture_image(struct gl_context
*ctx
,
84 struct gl_texture_image
*ti
,
86 GLuint x
, GLuint y
, GLuint w
, GLuint h
,
91 struct nouveau_teximage
*nti
= to_nouveau_teximage(ti
);
92 struct nouveau_surface
*s
= &nti
->surface
;
93 struct nouveau_surface
*st
= &nti
->transfer
.surface
;
94 struct nouveau_client
*client
= context_client(ctx
);
96 /* Nouveau has no support for 3D or cubemap textures. */
100 if (!(mode
& GL_MAP_READ_BIT
) &&
101 nouveau_pushbuf_refd(context_push(ctx
), s
->bo
)) {
104 * Heuristic: use a bounce buffer to pipeline
105 * teximage transfers.
108 st
->format
= s
->format
;
112 st
->pitch
= s
->pitch
;
116 size
= get_format_blocksy(st
->format
, h
) * st
->pitch
;
117 *map
= nouveau_get_scratch(ctx
, size
,
118 &st
->bo
, &st
->offset
);
123 if (mode
& GL_MAP_READ_BIT
)
124 flags
|= NOUVEAU_BO_RD
;
125 if (mode
& GL_MAP_WRITE_BIT
)
126 flags
|= NOUVEAU_BO_WR
;
129 ret
= nouveau_bo_map(s
->bo
, flags
, client
);
134 get_format_blocksy(s
->format
, y
) * s
->pitch
+
135 get_format_blocksx(s
->format
, x
) * s
->cpp
;
139 *map
= nti
->base
.Buffer
+
140 get_format_blocksy(s
->format
, y
) * s
->pitch
+
141 get_format_blocksx(s
->format
, x
) * s
->cpp
;
147 nouveau_unmap_texture_image(struct gl_context
*ctx
, struct gl_texture_image
*ti
,
150 struct nouveau_teximage
*nti
= to_nouveau_teximage(ti
);
151 struct nouveau_surface
*s
= &nti
->surface
;
152 struct nouveau_surface
*st
= &nti
->transfer
.surface
;
155 context_drv(ctx
)->surface_copy(ctx
, s
, st
, nti
->transfer
.x
,
156 nti
->transfer
.y
, 0, 0,
157 st
->width
, st
->height
);
158 nouveau_surface_ref(NULL
, st
);
164 nouveau_choose_tex_format(struct gl_context
*ctx
, GLenum target
,
165 GLint internalFormat
,
166 GLenum srcFormat
, GLenum srcType
)
168 switch (internalFormat
) {
177 case GL_COMPRESSED_RGBA
:
178 return MESA_FORMAT_B8G8R8A8_UNORM
;
180 return MESA_FORMAT_B5G5R5A1_UNORM
;
187 case GL_COMPRESSED_RGB
:
188 return MESA_FORMAT_B8G8R8X8_UNORM
;
193 return MESA_FORMAT_B5G6R5_UNORM
;
196 case GL_LUMINANCE_ALPHA
:
197 case GL_LUMINANCE4_ALPHA4
:
198 case GL_LUMINANCE6_ALPHA2
:
199 case GL_LUMINANCE12_ALPHA4
:
200 case GL_LUMINANCE12_ALPHA12
:
201 case GL_LUMINANCE16_ALPHA16
:
202 case GL_LUMINANCE8_ALPHA8
:
203 case GL_COMPRESSED_LUMINANCE_ALPHA
:
204 return MESA_FORMAT_B8G8R8A8_UNORM
;
212 case GL_COMPRESSED_LUMINANCE
:
213 return MESA_FORMAT_L_UNORM8
;
220 case GL_COMPRESSED_ALPHA
:
221 return MESA_FORMAT_A_UNORM8
;
228 return MESA_FORMAT_I_UNORM8
;
232 case GL_COMPRESSED_RGB_S3TC_DXT1_EXT
:
233 return MESA_FORMAT_RGB_DXT1
;
235 case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT
:
236 return MESA_FORMAT_RGBA_DXT1
;
240 case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT
:
241 return MESA_FORMAT_RGBA_DXT3
;
243 case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT
:
244 return MESA_FORMAT_RGBA_DXT5
;
252 teximage_fits(struct gl_texture_object
*t
, int level
)
254 struct nouveau_surface
*s
= &to_nouveau_texture(t
)->surfaces
[level
];
255 struct gl_texture_image
*ti
= t
->Image
[0][level
];
257 if (!ti
|| !to_nouveau_teximage(ti
)->surface
.bo
)
260 if (level
== t
->BaseLevel
&& (s
->offset
& 0x7f))
263 return t
->Target
== GL_TEXTURE_RECTANGLE
||
264 (s
->bo
&& s
->format
== ti
->TexFormat
&&
265 s
->width
== ti
->Width
&& s
->height
== ti
->Height
);
269 validate_teximage(struct gl_context
*ctx
, struct gl_texture_object
*t
,
270 int level
, int x
, int y
, int z
,
271 int width
, int height
, int depth
)
273 struct gl_texture_image
*ti
= t
->Image
[0][level
];
275 if (teximage_fits(t
, level
)) {
276 struct nouveau_surface
*ss
= to_nouveau_texture(t
)->surfaces
;
277 struct nouveau_surface
*s
= &to_nouveau_teximage(ti
)->surface
;
279 if (t
->Target
== GL_TEXTURE_RECTANGLE
)
280 nouveau_surface_ref(s
, &ss
[level
]);
282 context_drv(ctx
)->surface_copy(ctx
, &ss
[level
], s
,
293 get_last_level(struct gl_texture_object
*t
)
295 struct gl_texture_image
*base
= t
->Image
[0][t
->BaseLevel
];
297 if (t
->Sampler
.MinFilter
== GL_NEAREST
||
298 t
->Sampler
.MinFilter
== GL_LINEAR
|| !base
)
301 return MIN2(t
->BaseLevel
+ base
->MaxNumLevels
- 1, t
->MaxLevel
);
305 relayout_texture(struct gl_context
*ctx
, struct gl_texture_object
*t
)
307 struct gl_texture_image
*base
= t
->Image
[0][t
->BaseLevel
];
309 if (base
&& t
->Target
!= GL_TEXTURE_RECTANGLE
) {
310 struct nouveau_surface
*ss
= to_nouveau_texture(t
)->surfaces
;
311 struct nouveau_surface
*s
= &to_nouveau_teximage(base
)->surface
;
312 int i
, ret
, last
= get_last_level(t
);
313 enum nouveau_surface_layout layout
=
314 (_mesa_is_format_compressed(s
->format
) ? LINEAR
: SWIZZLED
);
315 unsigned size
, pitch
, offset
= 0,
319 /* Deallocate the old storage. */
320 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++)
321 nouveau_bo_ref(NULL
, &ss
[i
].bo
);
323 /* Relayout the mipmap tree. */
324 for (i
= t
->BaseLevel
; i
<= last
; i
++) {
325 pitch
= _mesa_format_row_stride(s
->format
, width
);
326 size
= get_format_blocksy(s
->format
, height
) * pitch
;
328 /* Images larger than 16B have to be aligned. */
330 offset
= align(offset
, 64);
332 ss
[i
] = (struct nouveau_surface
) {
343 width
= minify(width
, 1);
344 height
= minify(height
, 1);
347 /* Get new storage. */
348 size
= align(offset
, 64);
350 ret
= nouveau_bo_new(context_dev(ctx
), NOUVEAU_BO_MAP
|
351 NOUVEAU_BO_GART
| NOUVEAU_BO_VRAM
,
352 0, size
, NULL
, &ss
[last
].bo
);
355 for (i
= t
->BaseLevel
; i
< last
; i
++)
356 nouveau_bo_ref(ss
[last
].bo
, &ss
[i
].bo
);
361 nouveau_texture_validate(struct gl_context
*ctx
, struct gl_texture_object
*t
)
363 struct nouveau_texture
*nt
= to_nouveau_texture(t
);
364 int i
, last
= get_last_level(t
);
366 if (!teximage_fits(t
, t
->BaseLevel
) ||
367 !teximage_fits(t
, last
))
371 nt
->dirty
= GL_FALSE
;
373 /* Copy the teximages to the actual miptree. */
374 for (i
= t
->BaseLevel
; i
<= last
; i
++) {
375 struct nouveau_surface
*s
= &nt
->surfaces
[i
];
377 validate_teximage(ctx
, t
, i
, 0, 0, 0,
378 s
->width
, s
->height
, 1);
381 PUSH_KICK(context_push(ctx
));
388 nouveau_texture_reallocate(struct gl_context
*ctx
, struct gl_texture_object
*t
)
390 if (!teximage_fits(t
, t
->BaseLevel
) ||
391 !teximage_fits(t
, get_last_level(t
))) {
393 relayout_texture(ctx
, t
);
394 nouveau_texture_validate(ctx
, t
);
399 get_teximage_placement(struct gl_texture_image
*ti
)
401 if (ti
->TexFormat
== MESA_FORMAT_A_UNORM8
||
402 ti
->TexFormat
== MESA_FORMAT_L_UNORM8
||
403 ti
->TexFormat
== MESA_FORMAT_I_UNORM8
)
404 /* 1 cpp formats will have to be swizzled by the CPU,
405 * so leave them in system RAM for now. */
406 return NOUVEAU_BO_MAP
;
408 return NOUVEAU_BO_GART
| NOUVEAU_BO_MAP
;
412 nouveau_teximage(struct gl_context
*ctx
, GLint dims
,
413 struct gl_texture_image
*ti
,
415 GLenum format
, GLenum type
, const GLvoid
*pixels
,
416 const struct gl_pixelstore_attrib
*packing
,
417 GLboolean compressed
)
419 struct gl_texture_object
*t
= ti
->TexObject
;
420 const GLuint level
= ti
->Level
;
421 struct nouveau_surface
*s
= &to_nouveau_teximage(ti
)->surface
;
422 struct nouveau_teximage
*nti
= to_nouveau_teximage(ti
);
424 GLuint depth
= compressed
? 1 : ti
->Depth
;
426 /* Allocate a new bo for the image. */
427 nouveau_surface_alloc(ctx
, s
, LINEAR
, get_teximage_placement(ti
),
428 ti
->TexFormat
, ti
->Width
, ti
->Height
);
429 nti
->base
.RowStride
= s
->pitch
/ s
->cpp
;
432 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
,
434 pixels
, packing
, "glCompressedTexImage");
436 pixels
= _mesa_validate_pbo_teximage(ctx
,
437 dims
, ti
->Width
, ti
->Height
, depth
, format
, type
,
438 pixels
, packing
, "glTexImage");
444 /* Store the pixel data. */
445 nouveau_map_texture_image(ctx
, ti
, 0,
446 0, 0, ti
->Width
, ti
->Height
,
450 ret
= _mesa_texstore(ctx
, dims
, ti
->_BaseFormat
,
454 ti
->Width
, ti
->Height
, depth
,
455 format
, type
, pixels
, packing
);
458 nouveau_unmap_texture_image(ctx
, ti
, 0);
459 _mesa_unmap_teximage_pbo(ctx
, packing
);
461 if (!validate_teximage(ctx
, t
, level
, 0, 0, 0,
462 ti
->Width
, ti
->Height
, depth
))
463 /* It doesn't fit, mark it as dirty. */
467 if (level
== t
->BaseLevel
) {
468 if (!teximage_fits(t
, level
))
469 relayout_texture(ctx
, t
);
470 nouveau_texture_validate(ctx
, t
);
473 context_dirty_i(ctx
, TEX_OBJ
, ctx
->Texture
.CurrentUnit
);
474 context_dirty_i(ctx
, TEX_ENV
, ctx
->Texture
.CurrentUnit
);
479 nouveau_teximage_123d(struct gl_context
*ctx
, GLuint dims
,
480 struct gl_texture_image
*ti
,
481 GLenum format
, GLenum type
, const GLvoid
*pixels
,
482 const struct gl_pixelstore_attrib
*packing
)
484 nouveau_teximage(ctx
, dims
, ti
, 0, format
, type
, pixels
,
489 nouveau_compressed_teximage(struct gl_context
*ctx
, GLuint dims
,
490 struct gl_texture_image
*ti
,
491 GLsizei imageSize
, const GLvoid
*data
)
493 nouveau_teximage(ctx
, 2, ti
, imageSize
, 0, 0, data
,
494 &ctx
->Unpack
, GL_TRUE
);
498 nouveau_teximage_alloc(struct gl_context
*ctx
, struct gl_texture_image
*ti
)
500 nouveau_teximage(ctx
, 3, ti
, 0, 0, 0, NULL
,
501 &ctx
->DefaultPacking
, GL_FALSE
);
506 nouveau_texsubimage(struct gl_context
*ctx
, GLint dims
,
507 struct gl_texture_image
*ti
,
508 GLint xoffset
, GLint yoffset
, GLint zoffset
,
509 GLint width
, GLint height
, GLint depth
,
511 GLenum format
, GLenum type
, const void *pixels
,
512 const struct gl_pixelstore_attrib
*packing
,
513 GLboolean compressed
)
518 pixels
= _mesa_validate_pbo_compressed_teximage(ctx
,
520 pixels
, packing
, "glCompressedTexSubImage");
522 pixels
= _mesa_validate_pbo_teximage(ctx
,
523 dims
, width
, height
, depth
, format
, type
,
524 pixels
, packing
, "glTexSubImage");
530 nouveau_map_texture_image(ctx
, ti
, 0,
531 xoffset
, yoffset
, width
, height
,
532 GL_MAP_WRITE_BIT
, &map
, &row_stride
);
534 ret
= _mesa_texstore(ctx
, dims
, ti
->_BaseFormat
, ti
->TexFormat
,
536 width
, height
, depth
,
537 format
, type
, pixels
, packing
);
540 nouveau_unmap_texture_image(ctx
, ti
, 0);
541 _mesa_unmap_teximage_pbo(ctx
, packing
);
544 if (!to_nouveau_texture(ti
->TexObject
)->dirty
)
545 validate_teximage(ctx
, ti
->TexObject
, ti
->Level
,
546 xoffset
, yoffset
, zoffset
,
547 width
, height
, depth
);
551 nouveau_texsubimage_123d(struct gl_context
*ctx
, GLuint dims
,
552 struct gl_texture_image
*ti
,
553 GLint xoffset
, GLint yoffset
, GLint zoffset
,
554 GLint width
, GLint height
, GLint depth
,
555 GLenum format
, GLenum type
, const void *pixels
,
556 const struct gl_pixelstore_attrib
*packing
)
558 nouveau_texsubimage(ctx
, dims
, ti
, xoffset
, yoffset
, zoffset
,
559 width
, height
, depth
, 0, format
, type
, pixels
,
564 nouveau_compressed_texsubimage(struct gl_context
*ctx
, GLuint dims
,
565 struct gl_texture_image
*ti
,
566 GLint xoffset
, GLint yoffset
, GLint zoffset
,
567 GLsizei width
, GLint height
, GLint depth
,
569 GLint imageSize
, const void *data
)
571 nouveau_texsubimage(ctx
, dims
, ti
, xoffset
, yoffset
, zoffset
,
572 width
, height
, depth
, imageSize
, format
, 0, data
,
573 &ctx
->Unpack
, GL_TRUE
);
577 nouveau_bind_texture(struct gl_context
*ctx
, GLenum target
,
578 struct gl_texture_object
*t
)
580 context_dirty_i(ctx
, TEX_OBJ
, ctx
->Texture
.CurrentUnit
);
581 context_dirty_i(ctx
, TEX_ENV
, ctx
->Texture
.CurrentUnit
);
585 get_texbuffer_format(struct gl_renderbuffer
*rb
, GLint format
)
587 struct nouveau_surface
*s
= &to_nouveau_renderbuffer(rb
)->surface
;
591 else if (format
== __DRI_TEXTURE_FORMAT_RGBA
)
592 return MESA_FORMAT_B8G8R8A8_UNORM
;
594 return MESA_FORMAT_B8G8R8X8_UNORM
;
598 nouveau_set_texbuffer(__DRIcontext
*dri_ctx
,
599 GLint target
, GLint format
,
602 struct nouveau_context
*nctx
= dri_ctx
->driverPrivate
;
603 struct gl_context
*ctx
= &nctx
->base
;
604 struct gl_framebuffer
*fb
= draw
->driverPrivate
;
605 struct gl_renderbuffer
*rb
=
606 fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
;
607 struct gl_texture_object
*t
= _mesa_get_current_tex_object(ctx
, target
);
608 struct gl_texture_image
*ti
;
609 struct nouveau_teximage
*nti
;
610 struct nouveau_surface
*s
;
612 _mesa_lock_texture(ctx
, t
);
613 ti
= _mesa_get_tex_image(ctx
, t
, target
, 0);
614 nti
= to_nouveau_teximage(ti
);
615 s
= &to_nouveau_teximage(ti
)->surface
;
617 /* Update the texture surface with the given drawable. */
618 nouveau_update_renderbuffers(dri_ctx
, draw
);
619 nouveau_surface_ref(&to_nouveau_renderbuffer(rb
)->surface
, s
);
621 s
->format
= get_texbuffer_format(rb
, format
);
623 /* Update the image fields. */
624 _mesa_init_teximage_fields(ctx
, ti
, s
->width
, s
->height
,
625 1, 0, s
->cpp
, s
->format
);
626 nti
->base
.RowStride
= s
->pitch
/ s
->cpp
;
628 /* Try to validate it. */
629 if (!validate_teximage(ctx
, t
, 0, 0, 0, 0, s
->width
, s
->height
, 1))
630 nouveau_texture_reallocate(ctx
, t
);
632 context_dirty_i(ctx
, TEX_OBJ
, ctx
->Texture
.CurrentUnit
);
633 context_dirty_i(ctx
, TEX_ENV
, ctx
->Texture
.CurrentUnit
);
635 _mesa_unlock_texture(ctx
, t
);
639 nouveau_texture_functions_init(struct dd_function_table
*functions
)
641 functions
->NewTextureObject
= nouveau_texture_new
;
642 functions
->DeleteTexture
= nouveau_texture_free
;
643 functions
->NewTextureImage
= nouveau_teximage_new
;
644 functions
->FreeTextureImageBuffer
= nouveau_teximage_free
;
645 functions
->AllocTextureImageBuffer
= nouveau_teximage_alloc
;
646 functions
->ChooseTextureFormat
= nouveau_choose_tex_format
;
647 functions
->TexImage
= nouveau_teximage_123d
;
648 functions
->TexSubImage
= nouveau_texsubimage_123d
;
649 functions
->CompressedTexImage
= nouveau_compressed_teximage
;
650 functions
->CompressedTexSubImage
= nouveau_compressed_texsubimage
;
651 functions
->BindTexture
= nouveau_bind_texture
;
652 functions
->MapTextureImage
= nouveau_map_texture_image
;
653 functions
->UnmapTextureImage
= nouveau_unmap_texture_image
;