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
67 st_texture_create(struct st_context
*st
,
68 enum pipe_texture_target target
,
69 enum pipe_format format
,
77 struct pipe_texture pt
;
79 assert(target
<= PIPE_TEXTURE_CUBE
);
81 DBG("%s target %s format %s level %d..%d\n", __FUNCTION__
,
82 _mesa_lookup_enum_by_nr(target
),
83 _mesa_lookup_enum_by_nr(format
), first_level
, last_level
);
89 pt
.first_level
= first_level
;
90 pt
.last_level
= last_level
;
92 pt
.height
[0] = height0
;
94 pt
.compressed
= compress_byte
? 1 : 0;
95 pt
.cpp
= pt
.compressed
? compress_byte
: st_sizeof_format(format
);
98 return st
->pipe
->texture_create(st
->pipe
, &pt
);
103 * Check if a texture image be pulled into a unified mipmap texture.
104 * This mirrors the completeness test in a lot of ways.
106 * Not sure whether I want to pass gl_texture_image here.
109 st_texture_match_image(const struct pipe_texture
*pt
,
110 const struct gl_texture_image
*image
,
111 GLuint face
, GLuint level
)
113 /* Images with borders are never pulled into mipmap textures.
118 if (st_mesa_format_to_pipe_format(image
->TexFormat
->MesaFormat
) != pt
->format
||
119 image
->IsCompressed
!= pt
->compressed
)
122 /* Test image dimensions against the base level image adjusted for
123 * minification. This will also catch images not present in the
124 * texture, changed targets, etc.
126 if (image
->Width
!= pt
->width
[level
] ||
127 image
->Height
!= pt
->height
[level
] ||
128 image
->Depth
!= pt
->depth
[level
])
136 /* Although we use the image_offset[] array to store relative offsets
137 * to cube faces, Mesa doesn't know anything about this and expects
138 * each cube face to be treated as a separate image.
140 * These functions present that view to mesa:
143 st_texture_depth_offsets(struct pipe_texture
*pt
, GLuint level
)
145 static const GLuint zero
= 0;
147 if (pt
->target
!= PIPE_TEXTURE_3D
|| pt
->level
[level
].nr_images
== 1)
150 return pt
->level
[level
].image_offset
;
155 * Return the offset to the given mipmap texture image within the
156 * texture memory buffer, in bytes.
159 st_texture_image_offset(const struct pipe_texture
* pt
,
160 GLuint face
, GLuint level
)
162 if (pt
->target
== PIPE_TEXTURE_CUBE
)
163 return (pt
->level
[level
].level_offset
+
164 pt
->level
[level
].image_offset
[face
] * pt
->cpp
);
166 return pt
->level
[level
].level_offset
;
172 * Map a teximage in a mipmap texture.
173 * \param row_stride returns row stride in bytes
174 * \param image_stride returns image stride in bytes (for 3D textures).
175 * \return address of mapping
178 st_texture_image_map(struct st_context
*st
, struct st_texture_image
*stImage
,
181 struct pipe_texture
*pt
= stImage
->pt
;
182 DBG("%s \n", __FUNCTION__
);
184 stImage
->surface
= st
->pipe
->get_tex_surface(st
->pipe
, pt
, stImage
->face
,
185 stImage
->level
, zoffset
);
187 return pipe_surface_map(stImage
->surface
);
192 st_texture_image_unmap(struct st_texture_image
*stImage
)
194 DBG("%s\n", __FUNCTION__
);
196 pipe_surface_unmap(stImage
->surface
);
198 pipe_surface_reference(&stImage
->surface
, NULL
);
204 * Upload data to a rectangular sub-region. Lots of choices how to do this:
206 * - memcpy by span to current destination
207 * - upload data as new buffer and blit
209 * Currently always memcpy.
212 st_surface_data(struct pipe_context
*pipe
,
213 struct pipe_surface
*dst
,
214 unsigned dstx
, unsigned dsty
,
215 const void *src
, unsigned src_pitch
,
216 unsigned srcx
, unsigned srcy
, unsigned width
, unsigned height
)
218 pipe_copy_rect(pipe_surface_map(dst
),
221 dstx
, dsty
, width
, height
, src
, src_pitch
, srcx
, srcy
);
223 pipe_surface_unmap(dst
);
227 /* Upload data for a particular image.
230 st_texture_image_data(struct pipe_context
*pipe
,
231 struct pipe_texture
*dst
,
235 GLuint src_row_pitch
, GLuint src_image_pitch
)
237 GLuint depth
= dst
->depth
[level
];
240 const GLubyte
*srcUB
= src
;
241 struct pipe_surface
*dst_surface
;
243 DBG("%s\n", __FUNCTION__
);
244 for (i
= 0; i
< depth
; i
++) {
245 height
= dst
->height
[level
];
249 dst_surface
= pipe
->get_tex_surface(pipe
, dst
, face
, level
, i
);
251 st_surface_data(pipe
, dst_surface
,
252 0, 0, /* dstx, dsty */
255 0, 0, /* source x, y */
256 dst
->width
[level
], height
); /* width, height */
258 pipe_surface_reference(&dst_surface
, NULL
);
260 srcUB
+= src_image_pitch
* dst
->cpp
;
265 /* Copy mipmap image between textures
268 st_texture_image_copy(struct pipe_context
*pipe
,
269 struct pipe_texture
*dst
,
270 GLuint face
, GLuint level
,
271 struct pipe_texture
*src
)
273 GLuint width
= src
->width
[level
];
274 GLuint height
= src
->height
[level
];
275 GLuint depth
= src
->depth
[level
];
276 struct pipe_surface
*src_surface
;
277 struct pipe_surface
*dst_surface
;
283 for (i
= 0; i
< depth
; i
++) {
284 dst_surface
= pipe
->get_tex_surface(pipe
, dst
, face
, level
, i
);
285 src_surface
= pipe
->get_tex_surface(pipe
, src
, face
, level
, i
);
287 pipe
->surface_copy(pipe
,
294 pipe_surface_reference(&dst_surface
, NULL
);
295 pipe_surface_reference(&src_surface
, NULL
);