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 return ti
&& (t
->Target
== GL_TEXTURE_RECTANGLE
||
187 (s
->bo
&& s
->width
== ti
->Width
&&
188 s
->height
== ti
->Height
&&
189 s
->format
== ti
->TexFormat
));
193 validate_teximage(GLcontext
*ctx
, struct gl_texture_object
*t
,
194 int level
, int x
, int y
, int z
,
195 int width
, int height
, int depth
)
197 struct gl_texture_image
*ti
= t
->Image
[0][level
];
199 if (teximage_fits(t
, level
)) {
200 struct nouveau_surface
*ss
= to_nouveau_texture(t
)->surfaces
;
201 struct nouveau_surface
*s
= &to_nouveau_teximage(ti
)->surface
;
203 if (t
->Target
== GL_TEXTURE_RECTANGLE
)
204 nouveau_surface_ref(s
, &ss
[level
]);
206 context_drv(ctx
)->surface_copy(ctx
, &ss
[level
], s
,
217 get_last_level(struct gl_texture_object
*t
)
219 struct gl_texture_image
*base
= t
->Image
[0][t
->BaseLevel
];
221 if (t
->MinFilter
== GL_NEAREST
||
222 t
->MinFilter
== GL_LINEAR
|| !base
)
225 return MIN2(t
->BaseLevel
+ base
->MaxLog2
, t
->MaxLevel
);
229 relayout_texture(GLcontext
*ctx
, struct gl_texture_object
*t
)
231 struct gl_texture_image
*base
= t
->Image
[0][t
->BaseLevel
];
233 if (base
&& t
->Target
!= GL_TEXTURE_RECTANGLE
) {
234 struct nouveau_surface
*ss
= to_nouveau_texture(t
)->surfaces
;
235 struct nouveau_surface
*s
= &to_nouveau_teximage(base
)->surface
;
236 int i
, ret
, last
= get_last_level(t
);
237 unsigned size
, offset
= 0,
241 /* Deallocate the old storage. */
242 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++)
243 nouveau_bo_ref(NULL
, &ss
[i
].bo
);
245 /* Relayout the mipmap tree. */
246 for (i
= t
->BaseLevel
; i
<= last
; i
++) {
247 size
= width
* height
* s
->cpp
;
249 /* Images larger than 16B have to be aligned. */
251 offset
= align(offset
, 64);
253 ss
[i
] = (struct nouveau_surface
) {
260 .pitch
= width
* s
->cpp
,
264 width
= MAX2(1, width
/ 2);
265 height
= MAX2(1, height
/ 2);
268 /* Get new storage. */
269 size
= align(offset
, 64);
271 ret
= nouveau_bo_new(context_dev(ctx
), NOUVEAU_BO_MAP
|
272 NOUVEAU_BO_GART
| NOUVEAU_BO_VRAM
,
273 0, size
, &ss
[last
].bo
);
276 for (i
= t
->BaseLevel
; i
< last
; i
++)
277 nouveau_bo_ref(ss
[last
].bo
, &ss
[i
].bo
);
282 nouveau_texture_validate(GLcontext
*ctx
, struct gl_texture_object
*t
)
284 struct nouveau_texture
*nt
= to_nouveau_texture(t
);
285 int i
, last
= get_last_level(t
);
287 if (!teximage_fits(t
, t
->BaseLevel
) ||
288 !teximage_fits(t
, last
))
292 nt
->dirty
= GL_FALSE
;
294 /* Copy the teximages to the actual miptree. */
295 for (i
= t
->BaseLevel
; i
<= last
; i
++) {
296 struct nouveau_surface
*s
= &nt
->surfaces
[i
];
298 validate_teximage(ctx
, t
, i
, 0, 0, 0,
299 s
->width
, s
->height
, 1);
302 FIRE_RING(context_chan(ctx
));
309 nouveau_texture_reallocate(GLcontext
*ctx
, struct gl_texture_object
*t
)
311 if (!teximage_fits(t
, t
->BaseLevel
) ||
312 !teximage_fits(t
, get_last_level(t
))) {
314 relayout_texture(ctx
, t
);
315 nouveau_texture_validate(ctx
, t
);
320 get_teximage_placement(struct gl_texture_image
*ti
)
322 if (ti
->TexFormat
== MESA_FORMAT_A8
||
323 ti
->TexFormat
== MESA_FORMAT_L8
||
324 ti
->TexFormat
== MESA_FORMAT_I8
)
325 /* 1 cpp formats will have to be swizzled by the CPU,
326 * so leave them in system RAM for now. */
327 return NOUVEAU_BO_MAP
;
329 return NOUVEAU_BO_GART
| NOUVEAU_BO_MAP
;
333 nouveau_teximage(GLcontext
*ctx
, GLint dims
, GLenum target
, GLint level
,
334 GLint internalFormat
,
335 GLint width
, GLint height
, GLint depth
, GLint border
,
336 GLenum format
, GLenum type
, const GLvoid
*pixels
,
337 const struct gl_pixelstore_attrib
*packing
,
338 struct gl_texture_object
*t
,
339 struct gl_texture_image
*ti
)
341 struct nouveau_surface
*s
= &to_nouveau_teximage(ti
)->surface
;
344 /* Allocate a new bo for the image. */
345 nouveau_surface_alloc(ctx
, s
, LINEAR
, get_teximage_placement(ti
),
346 ti
->TexFormat
, width
, height
);
347 ti
->RowStride
= s
->pitch
/ s
->cpp
;
349 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
350 format
, type
, pixels
, packing
,
353 /* Store the pixel data. */
354 nouveau_teximage_map(ctx
, ti
);
356 ret
= _mesa_texstore(ctx
, dims
, ti
->_BaseFormat
,
357 ti
->TexFormat
, ti
->Data
,
360 width
, height
, depth
,
361 format
, type
, pixels
, packing
);
364 nouveau_teximage_unmap(ctx
, ti
);
365 _mesa_unmap_teximage_pbo(ctx
, packing
);
367 if (!validate_teximage(ctx
, t
, level
, 0, 0, 0,
368 width
, height
, depth
))
369 /* It doesn't fit, mark it as dirty. */
373 if (level
== t
->BaseLevel
) {
374 if (!teximage_fits(t
, level
))
375 relayout_texture(ctx
, t
);
376 nouveau_texture_validate(ctx
, t
);
379 context_dirty_i(ctx
, TEX_OBJ
, ctx
->Texture
.CurrentUnit
);
380 context_dirty_i(ctx
, TEX_ENV
, ctx
->Texture
.CurrentUnit
);
384 nouveau_teximage_1d(GLcontext
*ctx
, GLenum target
, GLint level
,
385 GLint internalFormat
,
386 GLint width
, GLint border
,
387 GLenum format
, GLenum type
, const GLvoid
*pixels
,
388 const struct gl_pixelstore_attrib
*packing
,
389 struct gl_texture_object
*t
,
390 struct gl_texture_image
*ti
)
392 nouveau_teximage(ctx
, 1, target
, level
, internalFormat
,
393 width
, 1, 1, border
, format
, type
, pixels
,
398 nouveau_teximage_2d(GLcontext
*ctx
, GLenum target
, GLint level
,
399 GLint internalFormat
,
400 GLint width
, GLint height
, GLint border
,
401 GLenum format
, GLenum type
, const GLvoid
*pixels
,
402 const struct gl_pixelstore_attrib
*packing
,
403 struct gl_texture_object
*t
,
404 struct gl_texture_image
*ti
)
406 nouveau_teximage(ctx
, 2, target
, level
, internalFormat
,
407 width
, height
, 1, border
, format
, type
, pixels
,
412 nouveau_teximage_3d(GLcontext
*ctx
, GLenum target
, GLint level
,
413 GLint internalFormat
,
414 GLint width
, GLint height
, GLint depth
, GLint border
,
415 GLenum format
, GLenum type
, const GLvoid
*pixels
,
416 const struct gl_pixelstore_attrib
*packing
,
417 struct gl_texture_object
*t
,
418 struct gl_texture_image
*ti
)
420 nouveau_teximage(ctx
, 3, target
, level
, internalFormat
,
421 width
, height
, depth
, border
, format
, type
, pixels
,
426 nouveau_texsubimage(GLcontext
*ctx
, GLint dims
, GLenum target
, GLint level
,
427 GLint xoffset
, GLint yoffset
, GLint zoffset
,
428 GLint width
, GLint height
, GLint depth
,
429 GLenum format
, GLenum type
, const void *pixels
,
430 const struct gl_pixelstore_attrib
*packing
,
431 struct gl_texture_object
*t
,
432 struct gl_texture_image
*ti
)
434 struct nouveau_surface
*s
= &to_nouveau_teximage(ti
)->surface
;
437 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
438 format
, type
, pixels
, packing
,
441 nouveau_teximage_map(ctx
, ti
);
443 ret
= _mesa_texstore(ctx
, 3, ti
->_BaseFormat
, ti
->TexFormat
,
444 ti
->Data
, xoffset
, yoffset
, zoffset
,
445 s
->pitch
, ti
->ImageOffsets
,
446 width
, height
, depth
, format
, type
,
450 nouveau_teximage_unmap(ctx
, ti
);
451 _mesa_unmap_teximage_pbo(ctx
, packing
);
454 if (!to_nouveau_texture(t
)->dirty
)
455 validate_teximage(ctx
, t
, level
, xoffset
, yoffset
, zoffset
,
456 width
, height
, depth
);
460 nouveau_texsubimage_3d(GLcontext
*ctx
, GLenum target
, GLint level
,
461 GLint xoffset
, GLint yoffset
, GLint zoffset
,
462 GLint width
, GLint height
, GLint depth
,
463 GLenum format
, GLenum type
, const void *pixels
,
464 const struct gl_pixelstore_attrib
*packing
,
465 struct gl_texture_object
*t
,
466 struct gl_texture_image
*ti
)
468 nouveau_texsubimage(ctx
, 3, target
, level
, xoffset
, yoffset
, zoffset
,
469 width
, height
, depth
, format
, type
, pixels
,
474 nouveau_texsubimage_2d(GLcontext
*ctx
, GLenum target
, GLint level
,
475 GLint xoffset
, GLint yoffset
,
476 GLint width
, GLint height
,
477 GLenum format
, GLenum type
, const void *pixels
,
478 const struct gl_pixelstore_attrib
*packing
,
479 struct gl_texture_object
*t
,
480 struct gl_texture_image
*ti
)
482 nouveau_texsubimage(ctx
, 2, target
, level
, xoffset
, yoffset
, 0,
483 width
, height
, 1, format
, type
, pixels
,
488 nouveau_texsubimage_1d(GLcontext
*ctx
, GLenum target
, GLint level
,
489 GLint xoffset
, GLint width
,
490 GLenum format
, GLenum type
, const void *pixels
,
491 const struct gl_pixelstore_attrib
*packing
,
492 struct gl_texture_object
*t
,
493 struct gl_texture_image
*ti
)
495 nouveau_texsubimage(ctx
, 1, target
, level
, xoffset
, 0, 0,
496 width
, 1, 1, format
, type
, pixels
,
501 nouveau_get_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
502 GLenum format
, GLenum type
, GLvoid
*pixels
,
503 struct gl_texture_object
*t
,
504 struct gl_texture_image
*ti
)
506 nouveau_teximage_map(ctx
, ti
);
507 _mesa_get_teximage(ctx
, target
, level
, format
, type
, pixels
,
509 nouveau_teximage_unmap(ctx
, ti
);
513 nouveau_bind_texture(GLcontext
*ctx
, GLenum target
,
514 struct gl_texture_object
*t
)
516 context_dirty_i(ctx
, TEX_OBJ
, ctx
->Texture
.CurrentUnit
);
517 context_dirty_i(ctx
, TEX_ENV
, ctx
->Texture
.CurrentUnit
);
521 get_texbuffer_format(struct gl_renderbuffer
*rb
, GLint format
)
523 struct nouveau_surface
*s
= &to_nouveau_renderbuffer(rb
)->surface
;
527 else if (format
== __DRI_TEXTURE_FORMAT_RGBA
)
528 return MESA_FORMAT_ARGB8888
;
530 return MESA_FORMAT_XRGB8888
;
534 nouveau_set_texbuffer(__DRIcontext
*dri_ctx
,
535 GLint target
, GLint format
,
538 struct nouveau_context
*nctx
= dri_ctx
->driverPrivate
;
539 GLcontext
*ctx
= &nctx
->base
;
540 struct gl_framebuffer
*fb
= draw
->driverPrivate
;
541 struct gl_renderbuffer
*rb
=
542 fb
->Attachment
[BUFFER_FRONT_LEFT
].Renderbuffer
;
543 struct gl_texture_object
*t
= _mesa_get_current_tex_object(ctx
, target
);
544 struct gl_texture_image
*ti
;
545 struct nouveau_surface
*s
;
547 _mesa_lock_texture(ctx
, t
);
548 ti
= _mesa_get_tex_image(ctx
, t
, target
, 0);
549 s
= &to_nouveau_teximage(ti
)->surface
;
551 /* Update the texture surface with the given drawable. */
552 nouveau_update_renderbuffers(dri_ctx
, draw
);
553 nouveau_surface_ref(&to_nouveau_renderbuffer(rb
)->surface
, s
);
555 /* Update the image fields. */
556 _mesa_init_teximage_fields(ctx
, target
, ti
, s
->width
, s
->height
,
558 ti
->RowStride
= s
->pitch
/ s
->cpp
;
559 ti
->TexFormat
= s
->format
= get_texbuffer_format(rb
, format
);
561 /* Try to validate it. */
562 if (!validate_teximage(ctx
, t
, 0, 0, 0, 0, s
->width
, s
->height
, 1))
563 nouveau_texture_reallocate(ctx
, t
);
565 context_dirty_i(ctx
, TEX_OBJ
, ctx
->Texture
.CurrentUnit
);
566 context_dirty_i(ctx
, TEX_ENV
, ctx
->Texture
.CurrentUnit
);
568 _mesa_unlock_texture(ctx
, t
);
572 nouveau_texture_map(GLcontext
*ctx
, struct gl_texture_object
*t
)
576 for (i
= t
->BaseLevel
; i
< t
->_MaxLevel
; i
++) {
578 nouveau_teximage_map(ctx
, t
->Image
[0][i
]);
583 nouveau_texture_unmap(GLcontext
*ctx
, struct gl_texture_object
*t
)
587 for (i
= t
->BaseLevel
; i
< t
->_MaxLevel
; i
++) {
589 nouveau_teximage_unmap(ctx
, t
->Image
[0][i
]);
594 store_mipmap(GLcontext
*ctx
, GLenum target
, int first
, int last
,
595 struct gl_texture_object
*t
)
597 struct gl_pixelstore_attrib packing
= {
598 .BufferObj
= ctx
->Shared
->NullBufferObj
,
601 GLenum format
= t
->Image
[0][first
]->TexFormat
;
602 unsigned base_format
, type
, comps
;
605 base_format
= _mesa_get_format_base_format(format
);
606 _mesa_format_to_type_and_comps(format
, &type
, &comps
);
608 for (i
= first
; i
<= last
; i
++) {
609 struct gl_texture_image
*ti
= t
->Image
[0][i
];
610 void *data
= ti
->Data
;
612 nouveau_teximage(ctx
, 3, target
, i
, ti
->InternalFormat
,
613 ti
->Width
, ti
->Height
, ti
->Depth
,
614 ti
->Border
, base_format
, type
, data
,
617 _mesa_free_texmemory(data
);
622 nouveau_generate_mipmap(GLcontext
*ctx
, GLenum target
,
623 struct gl_texture_object
*t
)
625 if (_mesa_meta_check_generate_mipmap_fallback(ctx
, target
, t
)) {
626 struct gl_texture_image
*base
= t
->Image
[0][t
->BaseLevel
];
628 nouveau_teximage_map(ctx
, base
);
629 _mesa_generate_mipmap(ctx
, target
, t
);
630 nouveau_teximage_unmap(ctx
, base
);
632 store_mipmap(ctx
, target
, t
->BaseLevel
+ 1,
633 get_last_level(t
), t
);
636 _mesa_meta_GenerateMipmap(ctx
, target
, t
);
641 nouveau_texture_functions_init(struct dd_function_table
*functions
)
643 functions
->NewTextureObject
= nouveau_texture_new
;
644 functions
->DeleteTexture
= nouveau_texture_free
;
645 functions
->NewTextureImage
= nouveau_teximage_new
;
646 functions
->FreeTexImageData
= nouveau_teximage_free
;
647 functions
->ChooseTextureFormat
= nouveau_choose_tex_format
;
648 functions
->TexImage1D
= nouveau_teximage_1d
;
649 functions
->TexImage2D
= nouveau_teximage_2d
;
650 functions
->TexImage3D
= nouveau_teximage_3d
;
651 functions
->TexSubImage1D
= nouveau_texsubimage_1d
;
652 functions
->TexSubImage2D
= nouveau_texsubimage_2d
;
653 functions
->TexSubImage3D
= nouveau_texsubimage_3d
;
654 functions
->GetTexImage
= nouveau_get_teximage
;
655 functions
->BindTexture
= nouveau_bind_texture
;
656 functions
->MapTexture
= nouveau_texture_map
;
657 functions
->UnmapTexture
= nouveau_texture_unmap
;
658 functions
->GenerateMipmap
= nouveau_generate_mipmap
;