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
;
123 return MESA_FORMAT_ARGB4444
;
129 return MESA_FORMAT_RGB565
;
136 return MESA_FORMAT_A8
;
144 return MESA_FORMAT_L8
;
147 case GL_LUMINANCE_ALPHA
:
148 case GL_LUMINANCE4_ALPHA4
:
149 case GL_LUMINANCE6_ALPHA2
:
150 case GL_LUMINANCE12_ALPHA4
:
151 case GL_LUMINANCE12_ALPHA12
:
152 case GL_LUMINANCE16_ALPHA16
:
153 case GL_LUMINANCE8_ALPHA8
:
154 return MESA_FORMAT_ARGB8888
;
161 return MESA_FORMAT_ARGB8888
;
164 case GL_COLOR_INDEX1_EXT
:
165 case GL_COLOR_INDEX2_EXT
:
166 case GL_COLOR_INDEX4_EXT
:
167 case GL_COLOR_INDEX12_EXT
:
168 case GL_COLOR_INDEX16_EXT
:
169 case GL_COLOR_INDEX8_EXT
:
170 return MESA_FORMAT_CI8
;
178 teximage_fits(struct gl_texture_object
*t
, int level
,
179 struct gl_texture_image
*ti
)
181 struct nouveau_surface
*s
= &to_nouveau_texture(t
)->surfaces
[level
];
183 return s
->bo
&& s
->width
== ti
->Width
&&
184 s
->height
== ti
->Height
&&
185 s
->format
== ti
->TexFormat
;
189 validate_teximage(GLcontext
*ctx
, struct gl_texture_object
*t
,
190 int level
, int x
, int y
, int z
,
191 int width
, int height
, int depth
)
193 struct gl_texture_image
*ti
= t
->Image
[0][level
];
195 if (ti
&& teximage_fits(t
, level
, ti
)) {
196 struct nouveau_surface
*ss
= to_nouveau_texture(t
)->surfaces
;
197 struct nouveau_surface
*s
= &to_nouveau_teximage(ti
)->surface
;
199 context_drv(ctx
)->surface_copy(ctx
, &ss
[level
], s
,
210 get_last_level(struct gl_texture_object
*t
)
212 struct gl_texture_image
*base
= t
->Image
[0][t
->BaseLevel
];
214 if (t
->MinFilter
== GL_NEAREST
||
215 t
->MinFilter
== GL_LINEAR
|| !base
)
218 return MIN2(t
->BaseLevel
+ base
->MaxLog2
, t
->MaxLevel
);
222 relayout_texture(GLcontext
*ctx
, struct gl_texture_object
*t
)
224 struct gl_texture_image
*base
= t
->Image
[0][t
->BaseLevel
];
227 struct nouveau_surface
*ss
= to_nouveau_texture(t
)->surfaces
;
228 struct nouveau_surface
*s
= &to_nouveau_teximage(base
)->surface
;
229 int i
, ret
, last
= get_last_level(t
);
230 unsigned size
, offset
= 0,
234 /* Deallocate the old storage. */
235 for (i
= 0; i
< MAX_TEXTURE_LEVELS
; i
++)
236 nouveau_bo_ref(NULL
, &ss
[i
].bo
);
238 /* Relayout the mipmap tree. */
239 for (i
= t
->BaseLevel
; i
<= last
; i
++) {
240 size
= width
* height
* s
->cpp
;
242 /* Images larger than 16B have to be aligned. */
244 offset
= align(offset
, 64);
246 ss
[i
] = (struct nouveau_surface
) {
253 .pitch
= width
* s
->cpp
,
257 width
= MAX2(1, width
/ 2);
258 height
= MAX2(1, height
/ 2);
261 /* Get new storage. */
262 size
= align(offset
, 64);
264 ret
= nouveau_bo_new(context_dev(ctx
), NOUVEAU_BO_MAP
|
265 NOUVEAU_BO_GART
| NOUVEAU_BO_VRAM
,
266 0, size
, &ss
[last
].bo
);
269 for (i
= t
->BaseLevel
; i
< last
; i
++)
270 nouveau_bo_ref(ss
[last
].bo
, &ss
[i
].bo
);
275 nouveau_texture_validate(GLcontext
*ctx
, struct gl_texture_object
*t
)
277 struct nouveau_texture
*nt
= to_nouveau_texture(t
);
278 int i
, last
= get_last_level(t
);
280 if (!nt
->surfaces
[last
].bo
)
284 nt
->dirty
= GL_FALSE
;
286 /* Copy the teximages to the actual miptree. */
287 for (i
= t
->BaseLevel
; i
<= last
; i
++) {
288 struct nouveau_surface
*s
= &nt
->surfaces
[i
];
290 validate_teximage(ctx
, t
, i
, 0, 0, 0,
291 s
->width
, s
->height
, 1);
299 nouveau_texture_reallocate(GLcontext
*ctx
, struct gl_texture_object
*t
)
302 relayout_texture(ctx
, t
);
303 nouveau_texture_validate(ctx
, t
);
307 get_teximage_placement(struct gl_texture_image
*ti
)
309 if (ti
->TexFormat
== MESA_FORMAT_A8
||
310 ti
->TexFormat
== MESA_FORMAT_L8
||
311 ti
->TexFormat
== MESA_FORMAT_I8
)
312 /* 1 cpp formats will have to be swizzled by the CPU,
313 * so leave them in system RAM for now. */
314 return NOUVEAU_BO_MAP
;
316 return NOUVEAU_BO_GART
| NOUVEAU_BO_MAP
;
320 nouveau_teximage(GLcontext
*ctx
, GLint dims
, GLenum target
, GLint level
,
321 GLint internalFormat
,
322 GLint width
, GLint height
, GLint depth
, GLint border
,
323 GLenum format
, GLenum type
, const GLvoid
*pixels
,
324 const struct gl_pixelstore_attrib
*packing
,
325 struct gl_texture_object
*t
,
326 struct gl_texture_image
*ti
)
328 struct nouveau_surface
*s
= &to_nouveau_teximage(ti
)->surface
;
331 /* Allocate a new bo for the image. */
332 nouveau_surface_alloc(ctx
, s
, LINEAR
, get_teximage_placement(ti
),
333 ti
->TexFormat
, width
, height
);
334 ti
->RowStride
= s
->pitch
/ s
->cpp
;
336 pixels
= _mesa_validate_pbo_teximage(ctx
, dims
, width
, height
, depth
,
337 format
, type
, pixels
, packing
,
340 /* Store the pixel data. */
341 nouveau_teximage_map(ctx
, ti
);
343 ret
= _mesa_texstore(ctx
, dims
, ti
->_BaseFormat
,
344 ti
->TexFormat
, ti
->Data
,
347 width
, height
, depth
,
348 format
, type
, pixels
, packing
);
351 nouveau_teximage_unmap(ctx
, ti
);
352 _mesa_unmap_teximage_pbo(ctx
, packing
);
354 if (!validate_teximage(ctx
, t
, level
, 0, 0, 0,
355 width
, height
, depth
))
356 /* It doesn't fit, mark it as dirty. */
360 if (level
== t
->BaseLevel
) {
361 if (!teximage_fits(t
, level
, ti
))
362 relayout_texture(ctx
, t
);
363 nouveau_texture_validate(ctx
, t
);
366 context_dirty_i(ctx
, TEX_OBJ
, ctx
->Texture
.CurrentUnit
);
367 context_dirty_i(ctx
, TEX_ENV
, ctx
->Texture
.CurrentUnit
);
371 nouveau_teximage_1d(GLcontext
*ctx
, GLenum target
, GLint level
,
372 GLint internalFormat
,
373 GLint width
, GLint border
,
374 GLenum format
, GLenum type
, const GLvoid
*pixels
,
375 const struct gl_pixelstore_attrib
*packing
,
376 struct gl_texture_object
*t
,
377 struct gl_texture_image
*ti
)
379 nouveau_teximage(ctx
, 1, target
, level
, internalFormat
,
380 width
, 1, 1, border
, format
, type
, pixels
,
385 nouveau_teximage_2d(GLcontext
*ctx
, GLenum target
, GLint level
,
386 GLint internalFormat
,
387 GLint width
, GLint height
, GLint border
,
388 GLenum format
, GLenum type
, const GLvoid
*pixels
,
389 const struct gl_pixelstore_attrib
*packing
,
390 struct gl_texture_object
*t
,
391 struct gl_texture_image
*ti
)
393 nouveau_teximage(ctx
, 2, target
, level
, internalFormat
,
394 width
, height
, 1, border
, format
, type
, pixels
,
399 nouveau_teximage_3d(GLcontext
*ctx
, GLenum target
, GLint level
,
400 GLint internalFormat
,
401 GLint width
, GLint height
, GLint depth
, GLint border
,
402 GLenum format
, GLenum type
, const GLvoid
*pixels
,
403 const struct gl_pixelstore_attrib
*packing
,
404 struct gl_texture_object
*t
,
405 struct gl_texture_image
*ti
)
407 nouveau_teximage(ctx
, 3, target
, level
, internalFormat
,
408 width
, height
, depth
, border
, format
, type
, pixels
,
413 nouveau_texsubimage_3d(GLcontext
*ctx
, GLenum target
, GLint level
,
414 GLint xoffset
, GLint yoffset
, GLint zoffset
,
415 GLint width
, GLint height
, GLint depth
,
416 GLenum format
, GLenum type
, const void *pixels
,
417 const struct gl_pixelstore_attrib
*packing
,
418 struct gl_texture_object
*t
,
419 struct gl_texture_image
*ti
)
421 nouveau_teximage_map(ctx
, ti
);
422 _mesa_store_texsubimage3d(ctx
, target
, level
, xoffset
, yoffset
, zoffset
,
423 width
, height
, depth
, format
, type
, pixels
,
425 nouveau_teximage_unmap(ctx
, ti
);
427 if (!to_nouveau_texture(t
)->dirty
)
428 validate_teximage(ctx
, t
, level
, xoffset
, yoffset
, zoffset
,
429 width
, height
, depth
);
433 nouveau_texsubimage_2d(GLcontext
*ctx
, GLenum target
, GLint level
,
434 GLint xoffset
, GLint yoffset
,
435 GLint width
, GLint height
,
436 GLenum format
, GLenum type
, const void *pixels
,
437 const struct gl_pixelstore_attrib
*packing
,
438 struct gl_texture_object
*t
,
439 struct gl_texture_image
*ti
)
441 nouveau_teximage_map(ctx
, ti
);
442 _mesa_store_texsubimage2d(ctx
, target
, level
, xoffset
, yoffset
,
443 width
, height
, format
, type
, pixels
,
445 nouveau_teximage_unmap(ctx
, ti
);
447 if (!to_nouveau_texture(t
)->dirty
)
448 validate_teximage(ctx
, t
, level
, xoffset
, yoffset
, 0,
453 nouveau_texsubimage_1d(GLcontext
*ctx
, GLenum target
, GLint level
,
454 GLint xoffset
, GLint width
,
455 GLenum format
, GLenum type
, const void *pixels
,
456 const struct gl_pixelstore_attrib
*packing
,
457 struct gl_texture_object
*t
,
458 struct gl_texture_image
*ti
)
460 nouveau_teximage_map(ctx
, ti
);
461 _mesa_store_texsubimage1d(ctx
, target
, level
, xoffset
,
462 width
, format
, type
, pixels
,
464 nouveau_teximage_unmap(ctx
, ti
);
466 if (!to_nouveau_texture(t
)->dirty
)
467 validate_teximage(ctx
, t
, level
, xoffset
, 0, 0,
472 nouveau_get_teximage(GLcontext
*ctx
, GLenum target
, GLint level
,
473 GLenum format
, GLenum type
, GLvoid
*pixels
,
474 struct gl_texture_object
*t
,
475 struct gl_texture_image
*ti
)
477 nouveau_teximage_map(ctx
, ti
);
478 _mesa_get_teximage(ctx
, target
, level
, format
, type
, pixels
,
480 nouveau_teximage_unmap(ctx
, ti
);
484 nouveau_bind_texture(GLcontext
*ctx
, GLenum target
,
485 struct gl_texture_object
*t
)
487 context_dirty_i(ctx
, TEX_OBJ
, ctx
->Texture
.CurrentUnit
);
488 context_dirty_i(ctx
, TEX_ENV
, ctx
->Texture
.CurrentUnit
);
492 nouveau_texture_map(GLcontext
*ctx
, struct gl_texture_object
*t
)
496 for (i
= t
->BaseLevel
; i
< t
->_MaxLevel
; i
++) {
498 nouveau_teximage_map(ctx
, t
->Image
[0][i
]);
503 nouveau_texture_unmap(GLcontext
*ctx
, struct gl_texture_object
*t
)
507 for (i
= t
->BaseLevel
; i
< t
->_MaxLevel
; i
++) {
509 nouveau_teximage_unmap(ctx
, t
->Image
[0][i
]);
514 nouveau_texture_functions_init(struct dd_function_table
*functions
)
516 functions
->NewTextureObject
= nouveau_texture_new
;
517 functions
->DeleteTexture
= nouveau_texture_free
;
518 functions
->NewTextureImage
= nouveau_teximage_new
;
519 functions
->FreeTexImageData
= nouveau_teximage_free
;
520 functions
->ChooseTextureFormat
= nouveau_choose_tex_format
;
521 functions
->TexImage1D
= nouveau_teximage_1d
;
522 functions
->TexImage2D
= nouveau_teximage_2d
;
523 functions
->TexImage3D
= nouveau_teximage_3d
;
524 functions
->TexSubImage1D
= nouveau_texsubimage_1d
;
525 functions
->TexSubImage2D
= nouveau_texsubimage_2d
;
526 functions
->TexSubImage3D
= nouveau_texsubimage_3d
;
527 functions
->GetTexImage
= nouveau_get_teximage
;
528 functions
->BindTexture
= nouveau_bind_texture
;
529 functions
->MapTexture
= nouveau_texture_map
;
530 functions
->UnmapTexture
= nouveau_texture_unmap
;