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
;
85 ret
= nouveau_bo_map(s
->bo
, NOUVEAU_BO_RDWR
);
88 ti
->Data
= s
->bo
->map
;
93 nouveau_teximage_unmap(GLcontext
*ctx
, struct gl_texture_image
*ti
)
95 struct nouveau_surface
*s
= &to_nouveau_teximage(ti
)->surface
;
98 nouveau_bo_unmap(s
->bo
);
103 nouveau_choose_tex_format(GLcontext
*ctx
, GLint internalFormat
,
104 GLenum srcFormat
, GLenum srcType
)
106 switch (internalFormat
) {
118 return MESA_FORMAT_ARGB8888
;
120 return MESA_FORMAT_ARGB1555
;
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 t
->Target
== GL_TEXTURE_RECTANGLE
||
181 (s
->bo
&& s
->width
== ti
->Width
&&
182 s
->height
== ti
->Height
&&
183 s
->format
== ti
->TexFormat
);
187 validate_teximage(GLcontext
*ctx
, struct gl_texture_object
*t
,
188 int level
, int x
, int y
, int z
,
189 int width
, int height
, int depth
)
191 struct gl_texture_image
*ti
= t
->Image
[0][level
];
193 if (ti
&& teximage_fits(t
, level
, ti
)) {
194 struct nouveau_surface
*ss
= to_nouveau_texture(t
)->surfaces
;
195 struct nouveau_surface
*s
= &to_nouveau_teximage(ti
)->surface
;
197 if (t
->Target
== GL_TEXTURE_RECTANGLE
)
198 nouveau_surface_ref(s
, &ss
[level
]);
200 context_drv(ctx
)->surface_copy(ctx
, &ss
[level
], s
,
211 get_last_level(struct gl_texture_object
*t
)
213 struct gl_texture_image
*base
= t
->Image
[0][t
->BaseLevel
];
215 if (t
->MinFilter
== GL_NEAREST
||
216 t
->MinFilter
== GL_LINEAR
|| !base
)
219 return MIN2(t
->BaseLevel
+ base
->MaxLog2
, t
->MaxLevel
);
223 relayout_texture(GLcontext
*ctx
, struct gl_texture_object
*t
)
225 struct gl_texture_image
*base
= t
->Image
[0][t
->BaseLevel
];
227 if (base
&& t
->Target
!= GL_TEXTURE_RECTANGLE
) {
228 struct nouveau_surface
*ss
= to_nouveau_texture(t
)->surfaces
;
229 struct nouveau_surface
*s
= &to_nouveau_teximage(base
)->surface
;
230 int i
, ret
, last
= get_last_level(t
);
231 unsigned size
, offset
= 0,
235 /* Deallocate the old storage. */
236 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++)
237 nouveau_bo_ref(NULL
, &ss
[i
].bo
);
239 /* Relayout the mipmap tree. */
240 for (i
= t
->BaseLevel
; i
<= last
; i
++) {
241 size
= width
* height
* s
->cpp
;
243 /* Images larger than 16B have to be aligned. */
245 offset
= align(offset
, 64);
247 ss
[i
] = (struct nouveau_surface
) {
254 .pitch
= width
* s
->cpp
,
258 width
= MAX2(1, width
/ 2);
259 height
= MAX2(1, height
/ 2);
262 /* Get new storage. */
263 size
= align(offset
, 64);
265 ret
= nouveau_bo_new(context_dev(ctx
), NOUVEAU_BO_MAP
|
266 NOUVEAU_BO_GART
| NOUVEAU_BO_VRAM
,
267 0, size
, &ss
[last
].bo
);
270 for (i
= t
->BaseLevel
; i
< last
; i
++)
271 nouveau_bo_ref(ss
[last
].bo
, &ss
[i
].bo
);
276 nouveau_texture_validate(GLcontext
*ctx
, struct gl_texture_object
*t
)
278 struct nouveau_texture
*nt
= to_nouveau_texture(t
);
279 int i
, last
= get_last_level(t
);
281 if (!nt
->surfaces
[last
].bo
)
285 nt
->dirty
= GL_FALSE
;
287 /* Copy the teximages to the actual miptree. */
288 for (i
= t
->BaseLevel
; i
<= last
; i
++) {
289 struct nouveau_surface
*s
= &nt
->surfaces
[i
];
291 validate_teximage(ctx
, t
, i
, 0, 0, 0,
292 s
->width
, s
->height
, 1);
300 nouveau_texture_reallocate(GLcontext
*ctx
, struct gl_texture_object
*t
)
303 relayout_texture(ctx
, t
);
304 nouveau_texture_validate(ctx
, t
);
308 get_teximage_placement(struct gl_texture_image
*ti
)
310 if (ti
->TexFormat
== MESA_FORMAT_A8
||
311 ti
->TexFormat
== MESA_FORMAT_L8
||
312 ti
->TexFormat
== MESA_FORMAT_I8
)
313 /* 1 cpp formats will have to be swizzled by the CPU,
314 * so leave them in system RAM for now. */
315 return NOUVEAU_BO_MAP
;
317 return NOUVEAU_BO_GART
| NOUVEAU_BO_MAP
;
321 nouveau_teximage(GLcontext
*ctx
, GLint dims
, GLenum target
, GLint level
,
322 GLint internalFormat
,
323 GLint width
, GLint height
, GLint depth
, GLint border
,
324 GLenum format
, GLenum type
, const GLvoid
*pixels
,
325 const struct gl_pixelstore_attrib
*packing
,
326 struct gl_texture_object
*t
,
327 struct gl_texture_image
*ti
)
329 struct nouveau_surface
*s
= &to_nouveau_teximage(ti
)->surface
;
332 /* Allocate a new bo for the image. */
333 nouveau_surface_alloc(ctx
, s
, LINEAR
, get_teximage_placement(ti
),
334 ti
->TexFormat
, width
, height
);
335 ti
->RowStride
= s
->pitch
/ s
->cpp
;
337 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
338 format
, type
, pixels
, packing
,
341 /* Store the pixel data. */
342 nouveau_teximage_map(ctx
, ti
);
344 ret
= _mesa_texstore(ctx
, dims
, ti
->_BaseFormat
,
345 ti
->TexFormat
, ti
->Data
,
348 width
, height
, depth
,
349 format
, type
, pixels
, packing
);
352 nouveau_teximage_unmap(ctx
, ti
);
353 _mesa_unmap_teximage_pbo(ctx
, packing
);
355 if (!validate_teximage(ctx
, t
, level
, 0, 0, 0,
356 width
, height
, depth
))
357 /* It doesn't fit, mark it as dirty. */
361 if (level
== t
->BaseLevel
) {
362 if (!teximage_fits(t
, level
, ti
))
363 relayout_texture(ctx
, t
);
364 nouveau_texture_validate(ctx
, t
);
367 context_dirty_i(ctx
, TEX_OBJ
, ctx
->Texture
.CurrentUnit
);
368 context_dirty_i(ctx
, TEX_ENV
, ctx
->Texture
.CurrentUnit
);
372 nouveau_teximage_1d(GLcontext
*ctx
, GLenum target
, GLint level
,
373 GLint internalFormat
,
374 GLint width
, GLint border
,
375 GLenum format
, GLenum type
, const GLvoid
*pixels
,
376 const struct gl_pixelstore_attrib
*packing
,
377 struct gl_texture_object
*t
,
378 struct gl_texture_image
*ti
)
380 nouveau_teximage(ctx
, 1, target
, level
, internalFormat
,
381 width
, 1, 1, border
, format
, type
, pixels
,
386 nouveau_teximage_2d(GLcontext
*ctx
, GLenum target
, GLint level
,
387 GLint internalFormat
,
388 GLint width
, GLint height
, GLint border
,
389 GLenum format
, GLenum type
, const GLvoid
*pixels
,
390 const struct gl_pixelstore_attrib
*packing
,
391 struct gl_texture_object
*t
,
392 struct gl_texture_image
*ti
)
394 nouveau_teximage(ctx
, 2, target
, level
, internalFormat
,
395 width
, height
, 1, border
, format
, type
, pixels
,
400 nouveau_teximage_3d(GLcontext
*ctx
, GLenum target
, GLint level
,
401 GLint internalFormat
,
402 GLint width
, GLint height
, GLint depth
, GLint border
,
403 GLenum format
, GLenum type
, const GLvoid
*pixels
,
404 const struct gl_pixelstore_attrib
*packing
,
405 struct gl_texture_object
*t
,
406 struct gl_texture_image
*ti
)
408 nouveau_teximage(ctx
, 3, target
, level
, internalFormat
,
409 width
, height
, depth
, border
, format
, type
, pixels
,
414 nouveau_texsubimage_3d(GLcontext
*ctx
, GLenum target
, GLint level
,
415 GLint xoffset
, GLint yoffset
, GLint zoffset
,
416 GLint width
, GLint height
, GLint depth
,
417 GLenum format
, GLenum type
, const void *pixels
,
418 const struct gl_pixelstore_attrib
*packing
,
419 struct gl_texture_object
*t
,
420 struct gl_texture_image
*ti
)
422 nouveau_teximage_map(ctx
, ti
);
423 _mesa_store_texsubimage3d(ctx
, target
, level
, xoffset
, yoffset
, zoffset
,
424 width
, height
, depth
, format
, type
, pixels
,
426 nouveau_teximage_unmap(ctx
, ti
);
428 if (!to_nouveau_texture(t
)->dirty
)
429 validate_teximage(ctx
, t
, level
, xoffset
, yoffset
, zoffset
,
430 width
, height
, depth
);
434 nouveau_texsubimage_2d(GLcontext
*ctx
, GLenum target
, GLint level
,
435 GLint xoffset
, GLint yoffset
,
436 GLint width
, GLint height
,
437 GLenum format
, GLenum type
, const void *pixels
,
438 const struct gl_pixelstore_attrib
*packing
,
439 struct gl_texture_object
*t
,
440 struct gl_texture_image
*ti
)
442 nouveau_teximage_map(ctx
, ti
);
443 _mesa_store_texsubimage2d(ctx
, target
, level
, xoffset
, yoffset
,
444 width
, height
, format
, type
, pixels
,
446 nouveau_teximage_unmap(ctx
, ti
);
448 if (!to_nouveau_texture(t
)->dirty
)
449 validate_teximage(ctx
, t
, level
, xoffset
, yoffset
, 0,
454 nouveau_texsubimage_1d(GLcontext
*ctx
, GLenum target
, GLint level
,
455 GLint xoffset
, GLint width
,
456 GLenum format
, GLenum type
, const void *pixels
,
457 const struct gl_pixelstore_attrib
*packing
,
458 struct gl_texture_object
*t
,
459 struct gl_texture_image
*ti
)
461 nouveau_teximage_map(ctx
, ti
);
462 _mesa_store_texsubimage1d(ctx
, target
, level
, xoffset
,
463 width
, format
, type
, pixels
,
465 nouveau_teximage_unmap(ctx
, ti
);
467 if (!to_nouveau_texture(t
)->dirty
)
468 validate_teximage(ctx
, t
, level
, xoffset
, 0, 0,
473 nouveau_get_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
474 GLenum format
, GLenum type
, GLvoid
*pixels
,
475 struct gl_texture_object
*t
,
476 struct gl_texture_image
*ti
)
478 nouveau_teximage_map(ctx
, ti
);
479 _mesa_get_teximage(ctx
, target
, level
, format
, type
, pixels
,
481 nouveau_teximage_unmap(ctx
, ti
);
485 nouveau_bind_texture(GLcontext
*ctx
, GLenum target
,
486 struct gl_texture_object
*t
)
488 context_dirty_i(ctx
, TEX_OBJ
, ctx
->Texture
.CurrentUnit
);
489 context_dirty_i(ctx
, TEX_ENV
, ctx
->Texture
.CurrentUnit
);
493 nouveau_texture_map(GLcontext
*ctx
, struct gl_texture_object
*t
)
497 for (i
= t
->BaseLevel
; i
< t
->_MaxLevel
; i
++) {
499 nouveau_teximage_map(ctx
, t
->Image
[0][i
]);
504 nouveau_texture_unmap(GLcontext
*ctx
, struct gl_texture_object
*t
)
508 for (i
= t
->BaseLevel
; i
< t
->_MaxLevel
; i
++) {
510 nouveau_teximage_unmap(ctx
, t
->Image
[0][i
]);
515 nouveau_texture_functions_init(struct dd_function_table
*functions
)
517 functions
->NewTextureObject
= nouveau_texture_new
;
518 functions
->DeleteTexture
= nouveau_texture_free
;
519 functions
->NewTextureImage
= nouveau_teximage_new
;
520 functions
->FreeTexImageData
= nouveau_teximage_free
;
521 functions
->ChooseTextureFormat
= nouveau_choose_tex_format
;
522 functions
->TexImage1D
= nouveau_teximage_1d
;
523 functions
->TexImage2D
= nouveau_teximage_2d
;
524 functions
->TexImage3D
= nouveau_teximage_3d
;
525 functions
->TexSubImage1D
= nouveau_texsubimage_1d
;
526 functions
->TexSubImage2D
= nouveau_texsubimage_2d
;
527 functions
->TexSubImage3D
= nouveau_texsubimage_3d
;
528 functions
->GetTexImage
= nouveau_get_teximage
;
529 functions
->BindTexture
= nouveau_bind_texture
;
530 functions
->MapTexture
= nouveau_texture_map
;
531 functions
->UnmapTexture
= nouveau_texture_unmap
;