1 /**************************************************************************
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "st_context.h"
29 #include "st_format.h"
30 #include "st_texture.h"
33 #include "pipe/p_state.h"
34 #include "pipe/p_context.h"
35 #include "pipe/p_defines.h"
36 #include "pipe/p_inlines.h"
37 #include "pipe/p_util.h"
38 #include "pipe/p_inlines.h"
39 #include "pipe/p_winsys.h"
42 #define DBG if(0) printf
46 target_to_target(GLenum target
)
49 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
50 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
51 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
52 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
53 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
54 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
55 return GL_TEXTURE_CUBE_MAP_ARB
;
64 * Allocate a new pipe_texture object
65 * width0, height0, depth0 are the dimensions of the level 0 image
66 * (the highest resolution). last_level indicates how many mipmap levels
67 * to allocate storage for. For non-mipmapped textures, this will be zero.
70 st_texture_create(struct st_context
*st
,
71 enum pipe_texture_target target
,
72 enum pipe_format format
,
79 struct pipe_texture pt
, *newtex
;
80 struct pipe_screen
*screen
= st
->pipe
->screen
;
82 assert(target
<= PIPE_TEXTURE_CUBE
);
84 DBG("%s target %s format %s last_level %d\n", __FUNCTION__
,
85 _mesa_lookup_enum_by_nr(target
),
86 _mesa_lookup_enum_by_nr(format
), last_level
);
90 memset(&pt
, 0, sizeof(pt
));
93 pt
.last_level
= last_level
;
95 pt
.height
[0] = height0
;
97 pt
.compressed
= compress_byte
? 1 : 0;
98 pt
.cpp
= pt
.compressed
? compress_byte
: st_sizeof_format(format
);
100 newtex
= screen
->texture_create(screen
, &pt
);
102 assert(!newtex
|| newtex
->refcount
== 1);
109 * Check if a texture image be pulled into a unified mipmap texture.
110 * This mirrors the completeness test in a lot of ways.
112 * Not sure whether I want to pass gl_texture_image here.
115 st_texture_match_image(const struct pipe_texture
*pt
,
116 const struct gl_texture_image
*image
,
117 GLuint face
, GLuint level
)
119 /* Images with borders are never pulled into mipmap textures.
124 if (st_mesa_format_to_pipe_format(image
->TexFormat
->MesaFormat
) != pt
->format
||
125 image
->IsCompressed
!= pt
->compressed
)
128 /* Test image dimensions against the base level image adjusted for
129 * minification. This will also catch images not present in the
130 * texture, changed targets, etc.
132 if (image
->Width
!= pt
->width
[level
] ||
133 image
->Height
!= pt
->height
[level
] ||
134 image
->Depth
!= pt
->depth
[level
])
142 /* Although we use the image_offset[] array to store relative offsets
143 * to cube faces, Mesa doesn't know anything about this and expects
144 * each cube face to be treated as a separate image.
146 * These functions present that view to mesa:
149 st_texture_depth_offsets(struct pipe_texture
*pt
, GLuint level
)
151 static const GLuint zero
= 0;
153 if (pt
->target
!= PIPE_TEXTURE_3D
|| pt
->level
[level
].nr_images
== 1)
156 return pt
->level
[level
].image_offset
;
161 * Return the offset to the given mipmap texture image within the
162 * texture memory buffer, in bytes.
165 st_texture_image_offset(const struct pipe_texture
* pt
,
166 GLuint face
, GLuint level
)
168 if (pt
->target
== PIPE_TEXTURE_CUBE
)
169 return (pt
->level
[level
].level_offset
+
170 pt
->level
[level
].image_offset
[face
] * pt
->cpp
);
172 return pt
->level
[level
].level_offset
;
178 * Map a teximage in a mipmap texture.
179 * \param row_stride returns row stride in bytes
180 * \param image_stride returns image stride in bytes (for 3D textures).
181 * \return address of mapping
184 st_texture_image_map(struct st_context
*st
, struct st_texture_image
*stImage
,
187 struct pipe_screen
*screen
= st
->pipe
->screen
;
188 struct pipe_texture
*pt
= stImage
->pt
;
189 DBG("%s \n", __FUNCTION__
);
191 stImage
->surface
= screen
->get_tex_surface(screen
, pt
, stImage
->face
,
192 stImage
->level
, zoffset
);
194 return pipe_surface_map(stImage
->surface
);
199 st_texture_image_unmap(struct st_texture_image
*stImage
)
201 DBG("%s\n", __FUNCTION__
);
203 pipe_surface_unmap(stImage
->surface
);
205 pipe_surface_reference(&stImage
->surface
, NULL
);
211 * Upload data to a rectangular sub-region. Lots of choices how to do this:
213 * - memcpy by span to current destination
214 * - upload data as new buffer and blit
216 * Currently always memcpy.
219 st_surface_data(struct pipe_context
*pipe
,
220 struct pipe_surface
*dst
,
221 unsigned dstx
, unsigned dsty
,
222 const void *src
, unsigned src_pitch
,
223 unsigned srcx
, unsigned srcy
, unsigned width
, unsigned height
)
225 pipe_copy_rect(pipe_surface_map(dst
),
228 dstx
, dsty
, width
, height
, src
, src_pitch
, srcx
, srcy
);
230 pipe_surface_unmap(dst
);
234 /* Upload data for a particular image.
237 st_texture_image_data(struct pipe_context
*pipe
,
238 struct pipe_texture
*dst
,
242 GLuint src_row_pitch
, GLuint src_image_pitch
)
244 struct pipe_screen
*screen
= pipe
->screen
;
245 GLuint depth
= dst
->depth
[level
];
248 const GLubyte
*srcUB
= src
;
249 struct pipe_surface
*dst_surface
;
251 DBG("%s\n", __FUNCTION__
);
252 for (i
= 0; i
< depth
; i
++) {
253 height
= dst
->height
[level
];
257 dst_surface
= screen
->get_tex_surface(screen
, dst
, face
, level
, i
);
259 st_surface_data(pipe
, dst_surface
,
260 0, 0, /* dstx, dsty */
263 0, 0, /* source x, y */
264 dst
->width
[level
], height
); /* width, height */
266 pipe_surface_reference(&dst_surface
, NULL
);
268 srcUB
+= src_image_pitch
* dst
->cpp
;
273 /* Copy mipmap image between textures
276 st_texture_image_copy(struct pipe_context
*pipe
,
277 struct pipe_texture
*dst
, GLuint dstLevel
,
278 struct pipe_texture
*src
,
281 struct pipe_screen
*screen
= pipe
->screen
;
282 GLuint width
= dst
->width
[dstLevel
];
283 GLuint height
= dst
->height
[dstLevel
];
284 GLuint depth
= dst
->depth
[dstLevel
];
285 struct pipe_surface
*src_surface
;
286 struct pipe_surface
*dst_surface
;
289 /* XXX this is a hack */
293 for (i
= 0; i
< depth
; i
++) {
296 /* find src texture level of needed size */
297 for (srcLevel
= 0; srcLevel
<= src
->last_level
; srcLevel
++) {
298 if (src
->width
[srcLevel
] == width
&&
299 src
->height
[srcLevel
] == height
) {
303 assert(src
->width
[srcLevel
] == width
);
304 assert(src
->height
[srcLevel
] == height
);
306 dst_surface
= screen
->get_tex_surface(screen
, dst
, face
, dstLevel
, i
);
307 src_surface
= screen
->get_tex_surface(screen
, src
, face
, srcLevel
, i
);
309 pipe
->surface_copy(pipe
,
317 pipe_surface_reference(&dst_surface
, NULL
);
318 pipe_surface_reference(&src_surface
, NULL
);