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_util.h"
37 #include "pipe/p_inlines.h"
38 #include "pipe/p_winsys.h"
41 #define DBG if(0) printf
45 target_to_target(GLenum target
)
48 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
:
49 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
:
50 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
:
51 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
:
52 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
:
53 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
:
54 return GL_TEXTURE_CUBE_MAP_ARB
;
62 st_texture_create(struct st_context
*st
,
65 GLenum internal_format
,
73 struct pipe_texture
*pt
= CALLOC_STRUCT(pipe_texture
);
75 assert(target
<= PIPE_TEXTURE_CUBE
);
77 DBG("%s target %s format %s level %d..%d\n", __FUNCTION__
,
78 _mesa_lookup_enum_by_nr(target
),
79 _mesa_lookup_enum_by_nr(internal_format
), first_level
, last_level
);
88 pt
->internal_format
= internal_format
;
89 pt
->first_level
= first_level
;
90 pt
->last_level
= last_level
;
91 pt
->width
[0] = width0
;
92 pt
->height
[0] = height0
;
93 pt
->depth
[0] = depth0
;
94 pt
->compressed
= compress_byte
? 1 : 0;
95 pt
->cpp
= pt
->compressed
? compress_byte
: st_sizeof_format(format
);
98 st
->pipe
->texture_create(st
->pipe
, &pt
);
106 /* Can the image be pulled into a unified mipmap texture. This mirrors
107 * the completeness test in a lot of ways.
109 * Not sure whether I want to pass gl_texture_image here.
112 st_texture_match_image(struct pipe_texture
*pt
,
113 struct gl_texture_image
*image
,
114 GLuint face
, GLuint level
)
116 /* Images with borders are never pulled into mipmap textures.
121 if (image
->InternalFormat
!= pt
->internal_format
||
122 image
->IsCompressed
!= pt
->compressed
)
125 /* Test image dimensions against the base level image adjusted for
126 * minification. This will also catch images not present in the
127 * texture, changed targets, etc.
129 if (image
->Width
!= pt
->width
[level
] ||
130 image
->Height
!= pt
->height
[level
] ||
131 image
->Depth
!= pt
->depth
[level
])
139 /* Although we use the image_offset[] array to store relative offsets
140 * to cube faces, Mesa doesn't know anything about this and expects
141 * each cube face to be treated as a separate image.
143 * These functions present that view to mesa:
146 st_texture_depth_offsets(struct pipe_texture
*pt
, GLuint level
)
148 static const GLuint zero
= 0;
150 if (pt
->target
!= PIPE_TEXTURE_3D
|| pt
->level
[level
].nr_images
== 1)
153 return pt
->level
[level
].image_offset
;
158 * Return the offset to the given mipmap texture image within the
159 * texture memory buffer, in bytes.
162 st_texture_image_offset(const struct pipe_texture
* pt
,
163 GLuint face
, GLuint level
)
165 if (pt
->target
== PIPE_TEXTURE_CUBE
)
166 return (pt
->level
[level
].level_offset
+
167 pt
->level
[level
].image_offset
[face
] * pt
->cpp
);
169 return pt
->level
[level
].level_offset
;
175 * Map a teximage in a mipmap texture.
176 * \param row_stride returns row stride in bytes
177 * \param image_stride returns image stride in bytes (for 3D textures).
178 * \return address of mapping
181 st_texture_image_map(struct st_context
*st
, struct st_texture_image
*stImage
,
184 struct pipe_texture
*pt
= stImage
->pt
;
185 DBG("%s \n", __FUNCTION__
);
187 stImage
->surface
= st
->pipe
->get_tex_surface(st
->pipe
, pt
, stImage
->face
,
188 stImage
->level
, zoffset
);
190 (void) st
->pipe
->region_map(st
->pipe
, stImage
->surface
->region
);
192 return stImage
->surface
->region
->map
+ stImage
->surface
->offset
;
196 st_texture_image_unmap(struct st_context
*st
, struct st_texture_image
*stImage
)
198 DBG("%s\n", __FUNCTION__
);
200 st
->pipe
->region_unmap(st
->pipe
, stImage
->surface
->region
);
202 pipe_surface_reference(&stImage
->surface
, NULL
);
207 /* Upload data for a particular image.
210 st_texture_image_data(struct pipe_context
*pipe
,
211 struct pipe_texture
*dst
,
215 GLuint src_row_pitch
, GLuint src_image_pitch
)
217 GLuint depth
= dst
->depth
[level
];
220 const GLubyte
*srcUB
= src
;
221 struct pipe_surface
*dst_surface
;
223 DBG("%s\n", __FUNCTION__
);
224 for (i
= 0; i
< depth
; i
++) {
225 height
= dst
->height
[level
];
229 dst_surface
= pipe
->get_tex_surface(pipe
, dst
, face
, level
, i
);
231 pipe
->surface_data(pipe
, dst_surface
,
232 0, 0, /* dstx, dsty */
235 0, 0, /* source x, y */
236 dst
->width
[level
], height
); /* width, height */
238 pipe_surface_reference(&dst_surface
, NULL
);
240 srcUB
+= src_image_pitch
* dst
->cpp
;
244 /* Copy mipmap image between textures
247 st_texture_image_copy(struct pipe_context
*pipe
,
248 struct pipe_texture
*dst
,
249 GLuint face
, GLuint level
,
250 struct pipe_texture
*src
)
252 GLuint width
= src
->width
[level
];
253 GLuint height
= src
->height
[level
];
254 GLuint depth
= src
->depth
[level
];
255 struct pipe_surface
*src_surface
;
256 struct pipe_surface
*dst_surface
;
261 for (i
= 0; i
< depth
; i
++) {
262 dst_surface
= pipe
->get_tex_surface(pipe
, dst
, face
, level
, i
);
263 src_surface
= pipe
->get_tex_surface(pipe
, src
, face
, level
, i
);
265 pipe
->surface_copy(pipe
,
272 pipe_surface_reference(&dst_surface
, NULL
);
273 pipe_surface_reference(&src_surface
, NULL
);