st_texture.c revision da8412ec19ad00627ae9139dc02f46f344bbb6ac
1/************************************************************************** 2 * 3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas. 4 * All Rights Reserved. 5 * 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: 13 * 14 * The above copyright notice and this permission notice (including the 15 * next paragraph) shall be included in all copies or substantial portions 16 * of the Software. 17 * 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. 25 * 26 **************************************************************************/ 27 28#include "st_context.h" 29#include "st_format.h" 30#include "st_texture.h" 31#include "st_cb_fbo.h" 32#include "st_inlines.h" 33#include "main/enums.h" 34#include "main/texfetch.h" 35#include "main/teximage.h" 36#include "main/texobj.h" 37 38#undef Elements /* fix re-defined macro warning */ 39 40#include "pipe/p_state.h" 41#include "pipe/p_context.h" 42#include "pipe/p_defines.h" 43#include "util/u_inlines.h" 44#include "util/u_format.h" 45#include "util/u_rect.h" 46#include "util/u_math.h" 47 48 49#define DBG if(0) printf 50 51#if 0 52static GLenum 53target_to_target(GLenum target) 54{ 55 switch (target) { 56 case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB: 57 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB: 58 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB: 59 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB: 60 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB: 61 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB: 62 return GL_TEXTURE_CUBE_MAP_ARB; 63 default: 64 return target; 65 } 66} 67#endif 68 69 70/** 71 * Allocate a new pipe_resource object 72 * width0, height0, depth0 are the dimensions of the level 0 image 73 * (the highest resolution). last_level indicates how many mipmap levels 74 * to allocate storage for. For non-mipmapped textures, this will be zero. 75 */ 76struct pipe_resource * 77st_texture_create(struct st_context *st, 78 enum pipe_texture_target target, 79 enum pipe_format format, 80 GLuint last_level, 81 GLuint width0, 82 GLuint height0, 83 GLuint depth0, 84 GLuint bind ) 85{ 86 struct pipe_resource pt, *newtex; 87 struct pipe_screen *screen = st->pipe->screen; 88 89 assert(target <= PIPE_TEXTURE_CUBE); 90 91 DBG("%s target %s format %s last_level %d\n", __FUNCTION__, 92 _mesa_lookup_enum_by_nr(target), 93 _mesa_lookup_enum_by_nr(format), last_level); 94 95 assert(format); 96 assert(screen->is_format_supported(screen, format, target, 97 PIPE_BIND_SAMPLER_VIEW, 0)); 98 99 memset(&pt, 0, sizeof(pt)); 100 pt.target = target; 101 pt.format = format; 102 pt.last_level = last_level; 103 pt.width0 = width0; 104 pt.height0 = height0; 105 pt.depth0 = depth0; 106 pt._usage = PIPE_USAGE_DEFAULT; 107 pt.bind = bind; 108 pt.flags = 0; 109 110 newtex = screen->resource_create(screen, &pt); 111 112 assert(!newtex || pipe_is_referenced(&newtex->reference)); 113 114 return newtex; 115} 116 117 118/** 119 * Check if a texture image can be pulled into a unified mipmap texture. 120 */ 121GLboolean 122st_texture_match_image(const struct pipe_resource *pt, 123 const struct gl_texture_image *image, 124 GLuint face, GLuint level) 125{ 126 /* Images with borders are never pulled into mipmap textures. 127 */ 128 if (image->Border) 129 return GL_FALSE; 130 131 /* Check if this image's format matches the established texture's format. 132 */ 133 if (st_mesa_format_to_pipe_format(image->TexFormat) != pt->format) 134 return GL_FALSE; 135 136 /* Test if this image's size matches what's expected in the 137 * established texture. 138 */ 139 if (image->Width != u_minify(pt->width0, level) || 140 image->Height != u_minify(pt->height0, level) || 141 image->Depth != u_minify(pt->depth0, level)) 142 return GL_FALSE; 143 144 return GL_TRUE; 145} 146 147 148#if 000 149/* Although we use the image_offset[] array to store relative offsets 150 * to cube faces, Mesa doesn't know anything about this and expects 151 * each cube face to be treated as a separate image. 152 * 153 * These functions present that view to mesa: 154 */ 155const GLuint * 156st_texture_depth_offsets(struct pipe_resource *pt, GLuint level) 157{ 158 static const GLuint zero = 0; 159 160 if (pt->target != PIPE_TEXTURE_3D || pt->level[level].nr_images == 1) 161 return &zero; 162 else 163 return pt->level[level].image_offset; 164} 165 166 167/** 168 * Return the offset to the given mipmap texture image within the 169 * texture memory buffer, in bytes. 170 */ 171GLuint 172st_texture_image_offset(const struct pipe_resource * pt, 173 GLuint face, GLuint level) 174{ 175 if (pt->target == PIPE_TEXTURE_CUBE) 176 return (pt->level[level].level_offset + 177 pt->level[level].image_offset[face] * pt->cpp); 178 else 179 return pt->level[level].level_offset; 180} 181#endif 182 183 184/** 185 * Map a teximage in a mipmap texture. 186 * \param row_stride returns row stride in bytes 187 * \param image_stride returns image stride in bytes (for 3D textures). 188 * \return address of mapping 189 */ 190GLubyte * 191st_texture_image_map(struct st_context *st, struct st_texture_image *stImage, 192 GLuint zoffset, enum pipe_transfer_usage usage, 193 GLuint x, GLuint y, GLuint w, GLuint h) 194{ 195 struct pipe_context *pipe = st->pipe; 196 struct pipe_resource *pt = stImage->pt; 197 198 DBG("%s \n", __FUNCTION__); 199 200 stImage->transfer = st_no_flush_get_tex_transfer(st, pt, stImage->face, 201 stImage->level, zoffset, 202 usage, x, y, w, h); 203 204 if (stImage->transfer) 205 return pipe_transfer_map(pipe, stImage->transfer); 206 else 207 return NULL; 208} 209 210 211void 212st_texture_image_unmap(struct st_context *st, 213 struct st_texture_image *stImage) 214{ 215 struct pipe_context *pipe = st->pipe; 216 217 DBG("%s\n", __FUNCTION__); 218 219 pipe_transfer_unmap(pipe, stImage->transfer); 220 221 pipe->transfer_destroy(pipe, stImage->transfer); 222} 223 224 225 226/** 227 * Upload data to a rectangular sub-region. Lots of choices how to do this: 228 * 229 * - memcpy by span to current destination 230 * - upload data as new buffer and blit 231 * 232 * Currently always memcpy. 233 */ 234static void 235st_surface_data(struct pipe_context *pipe, 236 struct pipe_transfer *dst, 237 unsigned dstx, unsigned dsty, 238 const void *src, unsigned src_stride, 239 unsigned srcx, unsigned srcy, unsigned width, unsigned height) 240{ 241 void *map = pipe_transfer_map(pipe, dst); 242 243 assert(dst->resource); 244 util_copy_rect(map, 245 dst->resource->format, 246 dst->stride, 247 dstx, dsty, 248 width, height, 249 src, src_stride, 250 srcx, srcy); 251 252 pipe_transfer_unmap(pipe, dst); 253} 254 255 256/* Upload data for a particular image. 257 */ 258void 259st_texture_image_data(struct st_context *st, 260 struct pipe_resource *dst, 261 GLuint face, 262 GLuint level, 263 void *src, 264 GLuint src_row_stride, GLuint src_image_stride) 265{ 266 struct pipe_context *pipe = st->pipe; 267 GLuint depth = u_minify(dst->depth0, level); 268 GLuint i; 269 const GLubyte *srcUB = src; 270 struct pipe_transfer *dst_transfer; 271 272 DBG("%s\n", __FUNCTION__); 273 274 for (i = 0; i < depth; i++) { 275 dst_transfer = st_no_flush_get_tex_transfer(st, dst, face, level, i, 276 PIPE_TRANSFER_WRITE, 0, 0, 277 u_minify(dst->width0, level), 278 u_minify(dst->height0, level)); 279 280 st_surface_data(pipe, dst_transfer, 281 0, 0, /* dstx, dsty */ 282 srcUB, 283 src_row_stride, 284 0, 0, /* source x, y */ 285 u_minify(dst->width0, level), 286 u_minify(dst->height0, level)); /* width, height */ 287 288 pipe->transfer_destroy(pipe, dst_transfer); 289 290 srcUB += src_image_stride; 291 } 292} 293 294 295/* Copy mipmap image between textures 296 */ 297void 298st_texture_image_copy(struct pipe_context *pipe, 299 struct pipe_resource *dst, GLuint dstLevel, 300 struct pipe_resource *src, 301 GLuint face) 302{ 303 struct pipe_screen *screen = pipe->screen; 304 GLuint width = u_minify(dst->width0, dstLevel); 305 GLuint height = u_minify(dst->height0, dstLevel); 306 GLuint depth = u_minify(dst->depth0, dstLevel); 307 struct pipe_surface *src_surface; 308 struct pipe_surface *dst_surface; 309 GLuint i; 310 311 for (i = 0; i < depth; i++) { 312 GLuint srcLevel; 313 314 /* find src texture level of needed size */ 315 for (srcLevel = 0; srcLevel <= src->last_level; srcLevel++) { 316 if (u_minify(src->width0, srcLevel) == width && 317 u_minify(src->height0, srcLevel) == height) { 318 break; 319 } 320 } 321 assert(u_minify(src->width0, srcLevel) == width); 322 assert(u_minify(src->height0, srcLevel) == height); 323 324#if 0 325 { 326 src_surface = screen->get_tex_surface(screen, src, face, srcLevel, i, 327 PIPE_BUFFER_USAGE_CPU_READ); 328 ubyte *map = screen->surface_map(screen, src_surface, PIPE_BUFFER_USAGE_CPU_READ); 329 map += src_surface->width * src_surface->height * 4 / 2; 330 printf("%s center pixel: %d %d %d %d (pt %p[%d] -> %p[%d])\n", 331 __FUNCTION__, 332 map[0], map[1], map[2], map[3], 333 src, srcLevel, dst, dstLevel); 334 335 screen->surface_unmap(screen, src_surface); 336 pipe_surface_reference(&src_surface, NULL); 337 } 338#endif 339 340 dst_surface = screen->get_tex_surface(screen, dst, face, dstLevel, i, 341 PIPE_BIND_BLIT_DESTINATION); 342 343 src_surface = screen->get_tex_surface(screen, src, face, srcLevel, i, 344 PIPE_BIND_BLIT_SOURCE); 345 346 pipe->surface_copy(pipe, 347 dst_surface, 348 0, 0, /* destX, Y */ 349 src_surface, 350 0, 0, /* srcX, Y */ 351 width, height); 352 353 pipe_surface_reference(&src_surface, NULL); 354 pipe_surface_reference(&dst_surface, NULL); 355 } 356} 357 358 359void 360st_teximage_flush_before_map(struct st_context *st, 361 struct pipe_resource *pt, 362 unsigned int face, 363 unsigned int level, 364 enum pipe_transfer_usage usage) 365{ 366 struct pipe_context *pipe = st->pipe; 367 unsigned referenced = 368 pipe->is_resource_referenced(pipe, pt, face, level); 369 370 if (referenced && ((referenced & PIPE_REFERENCED_FOR_WRITE) || 371 (usage & PIPE_TRANSFER_WRITE))) 372 st->pipe->flush(st->pipe, PIPE_FLUSH_RENDER_CACHE, NULL); 373} 374