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_util.h"
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"
40 static struct gl_texture_object
*
41 nouveau_texture_new(GLcontext
*ctx
, GLuint name
, GLenum target
)
43 struct nouveau_texture
*nt
= CALLOC_STRUCT(nouveau_texture
);
45 _mesa_initialize_texture_object(&nt
->base
, name
, target
);
51 nouveau_texture_free(GLcontext
*ctx
, struct gl_texture_object
*t
)
53 struct nouveau_texture
*nt
= to_nouveau_texture(t
);
56 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++)
57 nouveau_surface_ref(NULL
, &nt
->surfaces
[i
]);
59 _mesa_delete_texture_object(ctx
, t
);
62 static struct gl_texture_image
*
63 nouveau_teximage_new(GLcontext
*ctx
)
65 struct nouveau_teximage
*nti
= CALLOC_STRUCT(nouveau_teximage
);
71 nouveau_teximage_free(GLcontext
*ctx
, struct gl_texture_image
*ti
)
73 struct nouveau_teximage
*nti
= to_nouveau_teximage(ti
);
75 nouveau_surface_ref(NULL
, &nti
->surface
);
79 nouveau_teximage_map(GLcontext
*ctx
, struct gl_texture_image
*ti
)
81 struct nouveau_surface
*s
= &to_nouveau_teximage(ti
)->surface
;
84 ret
= nouveau_bo_map(s
->bo
, NOUVEAU_BO_RDWR
);
87 ti
->Data
= s
->bo
->map
;
91 nouveau_teximage_unmap(GLcontext
*ctx
, struct gl_texture_image
*ti
)
93 struct nouveau_surface
*s
= &to_nouveau_teximage(ti
)->surface
;
95 nouveau_bo_unmap(s
->bo
);
100 nouveau_choose_tex_format(GLcontext
*ctx
, GLint internalFormat
,
101 GLenum srcFormat
, GLenum srcType
)
103 switch (internalFormat
) {
115 return MESA_FORMAT_ARGB8888
;
117 return MESA_FORMAT_ARGB1555
;
120 return MESA_FORMAT_ARGB4444
;
126 return MESA_FORMAT_RGB565
;
133 return MESA_FORMAT_A8
;
141 return MESA_FORMAT_L8
;
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
;
158 return MESA_FORMAT_ARGB8888
;
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
;
175 teximage_fits(struct gl_texture_object
*t
, int level
,
176 struct gl_texture_image
*ti
)
178 struct nouveau_surface
*s
= &to_nouveau_texture(t
)->surfaces
[level
];
180 return s
->bo
&& s
->width
== ti
->Width
&&
181 s
->height
== ti
->Height
&&
182 s
->format
== ti
->TexFormat
;
186 validate_teximage(GLcontext
*ctx
, struct gl_texture_object
*t
,
187 int level
, int x
, int y
, int z
,
188 int width
, int height
, int depth
)
190 struct gl_texture_image
*ti
= t
->Image
[0][level
];
192 if (ti
&& teximage_fits(t
, level
, ti
)) {
193 struct nouveau_surface
*ss
= to_nouveau_texture(t
)->surfaces
;
194 struct nouveau_surface
*s
= &to_nouveau_teximage(ti
)->surface
;
196 context_drv(ctx
)->surface_copy(ctx
, &ss
[level
], s
,
207 get_last_level(struct gl_texture_object
*t
)
209 struct gl_texture_image
*base
= t
->Image
[0][t
->BaseLevel
];
211 if (t
->MinFilter
== GL_NEAREST
||
212 t
->MinFilter
== GL_LINEAR
|| !base
)
215 return MIN2(t
->BaseLevel
+ base
->MaxLog2
, t
->MaxLevel
);
219 relayout_texture(GLcontext
*ctx
, struct gl_texture_object
*t
)
221 struct gl_texture_image
*base
= t
->Image
[0][t
->BaseLevel
];
224 struct nouveau_surface
*ss
= to_nouveau_texture(t
)->surfaces
;
225 struct nouveau_surface
*s
= &to_nouveau_teximage(base
)->surface
;
226 int i
, ret
, last
= get_last_level(t
);
227 unsigned size
, offset
= 0,
231 /* Deallocate the old storage. */
232 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++)
233 nouveau_bo_ref(NULL
, &ss
[i
].bo
);
235 /* Relayout the mipmap tree. */
236 for (i
= t
->BaseLevel
; i
<= last
; i
++) {
237 size
= width
* height
* s
->cpp
;
239 /* Images larger than 16B have to be aligned. */
241 offset
= align(offset
, 64);
243 ss
[i
] = (struct nouveau_surface
) {
250 .pitch
= width
* s
->cpp
,
254 width
= MAX2(1, width
/ 2);
255 height
= MAX2(1, height
/ 2);
258 /* Get new storage. */
259 size
= align(offset
, 64);
261 ret
= nouveau_bo_new(context_dev(ctx
), NOUVEAU_BO_MAP
|
262 NOUVEAU_BO_GART
| NOUVEAU_BO_VRAM
,
263 0, size
, &ss
[last
].bo
);
266 for (i
= t
->BaseLevel
; i
< last
; i
++)
267 nouveau_bo_ref(ss
[last
].bo
, &ss
[i
].bo
);
272 nouveau_texture_validate(GLcontext
*ctx
, struct gl_texture_object
*t
)
274 struct nouveau_texture
*nt
= to_nouveau_texture(t
);
275 int i
, last
= get_last_level(t
);
277 if (!nt
->surfaces
[last
].bo
)
281 nt
->dirty
= GL_FALSE
;
283 /* Copy the teximages to the actual miptree. */
284 for (i
= t
->BaseLevel
; i
<= last
; i
++) {
285 struct nouveau_surface
*s
= &nt
->surfaces
[i
];
287 validate_teximage(ctx
, t
, i
, 0, 0, 0,
288 s
->width
, s
->height
, 1);
296 nouveau_texture_reallocate(GLcontext
*ctx
, struct gl_texture_object
*t
)
299 relayout_texture(ctx
, t
);
300 nouveau_texture_validate(ctx
, t
);
304 get_teximage_placement(struct gl_texture_image
*ti
)
306 if (ti
->TexFormat
== MESA_FORMAT_A8
||
307 ti
->TexFormat
== MESA_FORMAT_L8
||
308 ti
->TexFormat
== MESA_FORMAT_I8
)
309 /* 1 cpp formats will have to be swizzled by the CPU,
310 * so leave them in system RAM for now. */
311 return NOUVEAU_BO_MAP
;
313 return NOUVEAU_BO_GART
| NOUVEAU_BO_MAP
;
317 nouveau_teximage(GLcontext
*ctx
, GLint dims
, GLenum target
, GLint level
,
318 GLint internalFormat
,
319 GLint width
, GLint height
, GLint depth
, GLint border
,
320 GLenum format
, GLenum type
, const GLvoid
*pixels
,
321 const struct gl_pixelstore_attrib
*packing
,
322 struct gl_texture_object
*t
,
323 struct gl_texture_image
*ti
)
325 struct nouveau_surface
*s
= &to_nouveau_teximage(ti
)->surface
;
328 /* Allocate a new bo for the image. */
329 nouveau_surface_alloc(ctx
, s
, LINEAR
, get_teximage_placement(ti
),
330 ti
->TexFormat
, width
, height
);
331 ti
->RowStride
= s
->pitch
/ s
->cpp
;
333 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
334 format
, type
, pixels
, packing
,
337 /* Store the pixel data. */
338 nouveau_teximage_map(ctx
, ti
);
340 ret
= _mesa_texstore(ctx
, dims
, ti
->_BaseFormat
,
341 ti
->TexFormat
, ti
->Data
,
344 width
, height
, depth
,
345 format
, type
, pixels
, packing
);
348 nouveau_teximage_unmap(ctx
, ti
);
349 _mesa_unmap_teximage_pbo(ctx
, packing
);
351 if (!validate_teximage(ctx
, t
, level
, 0, 0, 0,
352 width
, height
, depth
))
353 /* It doesn't fit, mark it as dirty. */
357 if (level
== t
->BaseLevel
) {
358 if (!teximage_fits(t
, level
, ti
))
359 relayout_texture(ctx
, t
);
360 nouveau_texture_validate(ctx
, t
);
363 context_dirty_i(ctx
, TEX_OBJ
, ctx
->Texture
.CurrentUnit
);
364 context_dirty_i(ctx
, TEX_ENV
, ctx
->Texture
.CurrentUnit
);
368 nouveau_teximage_1d(GLcontext
*ctx
, GLenum target
, GLint level
,
369 GLint internalFormat
,
370 GLint width
, GLint border
,
371 GLenum format
, GLenum type
, const GLvoid
*pixels
,
372 const struct gl_pixelstore_attrib
*packing
,
373 struct gl_texture_object
*t
,
374 struct gl_texture_image
*ti
)
376 nouveau_teximage(ctx
, 1, target
, level
, internalFormat
,
377 width
, 1, 1, border
, format
, type
, pixels
,
382 nouveau_teximage_2d(GLcontext
*ctx
, GLenum target
, GLint level
,
383 GLint internalFormat
,
384 GLint width
, GLint height
, 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
)
390 nouveau_teximage(ctx
, 2, target
, level
, internalFormat
,
391 width
, height
, 1, border
, format
, type
, pixels
,
396 nouveau_teximage_3d(GLcontext
*ctx
, GLenum target
, GLint level
,
397 GLint internalFormat
,
398 GLint width
, GLint height
, GLint depth
, 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
)
404 nouveau_teximage(ctx
, 3, target
, level
, internalFormat
,
405 width
, height
, depth
, border
, format
, type
, pixels
,
410 nouveau_texsubimage_3d(GLcontext
*ctx
, GLenum target
, GLint level
,
411 GLint xoffset
, GLint yoffset
, GLint zoffset
,
412 GLint width
, GLint height
, GLint depth
,
413 GLenum format
, GLenum type
, const void *pixels
,
414 const struct gl_pixelstore_attrib
*packing
,
415 struct gl_texture_object
*t
,
416 struct gl_texture_image
*ti
)
418 nouveau_teximage_map(ctx
, ti
);
419 _mesa_store_texsubimage3d(ctx
, target
, level
, xoffset
, yoffset
, zoffset
,
420 width
, height
, depth
, format
, type
, pixels
,
422 nouveau_teximage_unmap(ctx
, ti
);
424 if (!to_nouveau_texture(t
)->dirty
)
425 validate_teximage(ctx
, t
, level
, xoffset
, yoffset
, zoffset
,
426 width
, height
, depth
);
430 nouveau_texsubimage_2d(GLcontext
*ctx
, GLenum target
, GLint level
,
431 GLint xoffset
, GLint yoffset
,
432 GLint width
, GLint height
,
433 GLenum format
, GLenum type
, const void *pixels
,
434 const struct gl_pixelstore_attrib
*packing
,
435 struct gl_texture_object
*t
,
436 struct gl_texture_image
*ti
)
438 nouveau_teximage_map(ctx
, ti
);
439 _mesa_store_texsubimage2d(ctx
, target
, level
, xoffset
, yoffset
,
440 width
, height
, format
, type
, pixels
,
442 nouveau_teximage_unmap(ctx
, ti
);
444 if (!to_nouveau_texture(t
)->dirty
)
445 validate_teximage(ctx
, t
, level
, xoffset
, yoffset
, 0,
450 nouveau_texsubimage_1d(GLcontext
*ctx
, GLenum target
, GLint level
,
451 GLint xoffset
, GLint width
,
452 GLenum format
, GLenum type
, const void *pixels
,
453 const struct gl_pixelstore_attrib
*packing
,
454 struct gl_texture_object
*t
,
455 struct gl_texture_image
*ti
)
457 nouveau_teximage_map(ctx
, ti
);
458 _mesa_store_texsubimage1d(ctx
, target
, level
, xoffset
,
459 width
, format
, type
, pixels
,
461 nouveau_teximage_unmap(ctx
, ti
);
463 if (!to_nouveau_texture(t
)->dirty
)
464 validate_teximage(ctx
, t
, level
, xoffset
, 0, 0,
469 nouveau_get_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
470 GLenum format
, GLenum type
, GLvoid
*pixels
,
471 struct gl_texture_object
*t
,
472 struct gl_texture_image
*ti
)
474 nouveau_teximage_map(ctx
, ti
);
475 _mesa_get_teximage(ctx
, target
, level
, format
, type
, pixels
,
477 nouveau_teximage_unmap(ctx
, ti
);
481 nouveau_bind_texture(GLcontext
*ctx
, GLenum target
,
482 struct gl_texture_object
*t
)
484 context_dirty_i(ctx
, TEX_OBJ
, ctx
->Texture
.CurrentUnit
);
485 context_dirty_i(ctx
, TEX_ENV
, ctx
->Texture
.CurrentUnit
);
489 nouveau_texture_map(GLcontext
*ctx
, struct gl_texture_object
*t
)
493 for (i
= t
->BaseLevel
; i
< t
->_MaxLevel
; i
++) {
495 nouveau_teximage_map(ctx
, t
->Image
[0][i
]);
500 nouveau_texture_unmap(GLcontext
*ctx
, struct gl_texture_object
*t
)
504 for (i
= t
->BaseLevel
; i
< t
->_MaxLevel
; i
++) {
506 nouveau_teximage_unmap(ctx
, t
->Image
[0][i
]);
511 nouveau_texture_functions_init(struct dd_function_table
*functions
)
513 functions
->NewTextureObject
= nouveau_texture_new
;
514 functions
->DeleteTexture
= nouveau_texture_free
;
515 functions
->NewTextureImage
= nouveau_teximage_new
;
516 functions
->FreeTexImageData
= nouveau_teximage_free
;
517 functions
->ChooseTextureFormat
= nouveau_choose_tex_format
;
518 functions
->TexImage1D
= nouveau_teximage_1d
;
519 functions
->TexImage2D
= nouveau_teximage_2d
;
520 functions
->TexImage3D
= nouveau_teximage_3d
;
521 functions
->TexSubImage1D
= nouveau_texsubimage_1d
;
522 functions
->TexSubImage2D
= nouveau_texsubimage_2d
;
523 functions
->TexSubImage3D
= nouveau_texsubimage_3d
;
524 functions
->GetTexImage
= nouveau_get_teximage
;
525 functions
->BindTexture
= nouveau_bind_texture
;
526 functions
->MapTexture
= nouveau_texture_map
;
527 functions
->UnmapTexture
= nouveau_texture_unmap
;