X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Fmesa%2Fstate_tracker%2Fst_texture.c;h=ffe7e256a56163363605440edfae5f7d68169e16;hb=8b881ad1c3d9dd3c96afbdbb608a7240d40e9c92;hp=8a3e4cd3ac3d3807e6b67f0b83ee5e3c7d14e720;hpb=080c40ab32b2abd6d8381b4a0cc143d36a1652b2;p=mesa.git diff --git a/src/mesa/state_tracker/st_texture.c b/src/mesa/state_tracker/st_texture.c index 8a3e4cd3ac3..ffe7e256a56 100644 --- a/src/mesa/state_tracker/st_texture.c +++ b/src/mesa/state_tracker/st_texture.c @@ -25,24 +25,18 @@ * **************************************************************************/ +#include + #include "st_context.h" #include "st_format.h" -#include "st_public.h" #include "st_texture.h" #include "st_cb_fbo.h" -#include "st_inlines.h" #include "main/enums.h" -#include "main/texfetch.h" -#include "main/teximage.h" -#include "main/texobj.h" -#include "main/texstore.h" - -#undef Elements /* fix re-defined macro warning */ #include "pipe/p_state.h" #include "pipe/p_context.h" #include "pipe/p_defines.h" -#include "pipe/p_inlines.h" +#include "util/u_inlines.h" #include "util/u_format.h" #include "util/u_rect.h" #include "util/u_math.h" @@ -50,32 +44,14 @@ #define DBG if(0) printf -#if 0 -static GLenum -target_to_target(GLenum target) -{ - switch (target) { - case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB: - case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB: - case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB: - case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB: - case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB: - case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB: - return GL_TEXTURE_CUBE_MAP_ARB; - default: - return target; - } -} -#endif - /** - * Allocate a new pipe_texture object + * Allocate a new pipe_resource object * width0, height0, depth0 are the dimensions of the level 0 image * (the highest resolution). last_level indicates how many mipmap levels * to allocate storage for. For non-mipmapped textures, this will be zero. */ -struct pipe_texture * +struct pipe_resource * st_texture_create(struct st_context *st, enum pipe_texture_target target, enum pipe_format format, @@ -83,20 +59,26 @@ st_texture_create(struct st_context *st, GLuint width0, GLuint height0, GLuint depth0, - GLuint usage ) + GLuint layers, + GLuint bind ) { - struct pipe_texture pt, *newtex; + struct pipe_resource pt, *newtex; struct pipe_screen *screen = st->pipe->screen; - assert(target <= PIPE_TEXTURE_CUBE); + assert(target < PIPE_MAX_TEXTURE_TYPES); + assert(width0 > 0); + assert(height0 > 0); + assert(depth0 > 0); + if (target == PIPE_TEXTURE_CUBE) + assert(layers == 6); DBG("%s target %s format %s last_level %d\n", __FUNCTION__, _mesa_lookup_enum_by_nr(target), _mesa_lookup_enum_by_nr(format), last_level); assert(format); - assert(screen->is_format_supported(screen, format, target, - PIPE_TEXTURE_USAGE_SAMPLER, 0)); + assert(screen->is_format_supported(screen, format, target, 0, + PIPE_BIND_SAMPLER_VIEW)); memset(&pt, 0, sizeof(pt)); pt.target = target; @@ -105,9 +87,12 @@ st_texture_create(struct st_context *st, pt.width0 = width0; pt.height0 = height0; pt.depth0 = depth0; - pt.tex_usage = usage; + pt.array_size = (target == PIPE_TEXTURE_CUBE ? 6 : layers); + pt.usage = PIPE_USAGE_DEFAULT; + pt.bind = bind; + pt.flags = 0; - newtex = screen->texture_create(screen, &pt); + newtex = screen->resource_create(screen, &pt); assert(!newtex || pipe_is_referenced(&newtex->reference)); @@ -115,14 +100,82 @@ st_texture_create(struct st_context *st, } +/** + * In OpenGL the number of 1D array texture layers is the "height" and + * the number of 2D array texture layers is the "depth". In Gallium the + * number of layers in an array texture is a separate 'array_size' field. + * This function converts dimensions from the former to the later. + */ +void +st_gl_texture_dims_to_pipe_dims(GLenum texture, + GLuint widthIn, + GLuint heightIn, + GLuint depthIn, + GLuint *widthOut, + GLuint *heightOut, + GLuint *depthOut, + GLuint *layersOut) +{ + switch (texture) { + case GL_TEXTURE_1D: + assert(heightIn == 1); + assert(depthIn == 1); + *widthOut = widthIn; + *heightOut = 1; + *depthOut = 1; + *layersOut = 1; + break; + case GL_TEXTURE_1D_ARRAY: + assert(depthIn == 1); + *widthOut = widthIn; + *heightOut = 1; + *depthOut = 1; + *layersOut = heightIn; + break; + case GL_TEXTURE_2D: + case GL_TEXTURE_RECTANGLE: + assert(depthIn == 1); + *widthOut = widthIn; + *heightOut = heightIn; + *depthOut = 1; + *layersOut = 1; + break; + case GL_TEXTURE_CUBE_MAP: + assert(depthIn == 1); + *widthOut = widthIn; + *heightOut = heightIn; + *depthOut = 1; + *layersOut = 6; + break; + case GL_TEXTURE_2D_ARRAY: + *widthOut = widthIn; + *heightOut = heightIn; + *depthOut = 1; + *layersOut = depthIn; + break; + default: + assert(0 && "Unexpected texture in st_gl_texture_dims_to_pipe_dims()"); + /* fall-through */ + case GL_TEXTURE_3D: + *widthOut = widthIn; + *heightOut = heightIn; + *depthOut = depthIn; + *layersOut = 1; + break; + } +} + + /** * Check if a texture image can be pulled into a unified mipmap texture. */ GLboolean -st_texture_match_image(const struct pipe_texture *pt, +st_texture_match_image(const struct pipe_resource *pt, const struct gl_texture_image *image, GLuint face, GLuint level) { + GLuint ptWidth, ptHeight, ptDepth, ptLayers; + /* Images with borders are never pulled into mipmap textures. */ if (image->Border) @@ -133,77 +186,47 @@ st_texture_match_image(const struct pipe_texture *pt, if (st_mesa_format_to_pipe_format(image->TexFormat) != pt->format) return GL_FALSE; + st_gl_texture_dims_to_pipe_dims(image->TexObject->Target, + image->Width, image->Height, image->Depth, + &ptWidth, &ptHeight, &ptDepth, &ptLayers); + /* Test if this image's size matches what's expected in the * established texture. */ - if (image->Width != u_minify(pt->width0, level) || - image->Height != u_minify(pt->height0, level) || - image->Depth != u_minify(pt->depth0, level)) + if (ptWidth != u_minify(pt->width0, level) || + ptHeight != u_minify(pt->height0, level) || + ptDepth != u_minify(pt->depth0, level) || + ptLayers != pt->array_size) return GL_FALSE; return GL_TRUE; } -#if 000 -/* Although we use the image_offset[] array to store relative offsets - * to cube faces, Mesa doesn't know anything about this and expects - * each cube face to be treated as a separate image. - * - * These functions present that view to mesa: - */ -const GLuint * -st_texture_depth_offsets(struct pipe_texture *pt, GLuint level) -{ - static const GLuint zero = 0; - - if (pt->target != PIPE_TEXTURE_3D || pt->level[level].nr_images == 1) - return &zero; - else - return pt->level[level].image_offset; -} - - /** - * Return the offset to the given mipmap texture image within the - * texture memory buffer, in bytes. - */ -GLuint -st_texture_image_offset(const struct pipe_texture * pt, - GLuint face, GLuint level) -{ - if (pt->target == PIPE_TEXTURE_CUBE) - return (pt->level[level].level_offset + - pt->level[level].image_offset[face] * pt->cpp); - else - return pt->level[level].level_offset; -} -#endif - - -/** - * Map a teximage in a mipmap texture. - * \param row_stride returns row stride in bytes - * \param image_stride returns image stride in bytes (for 3D textures). - * \return address of mapping + * Map a texture image and return the address for a particular 2D face/slice/ + * layer. The stImage indicates the cube face and mipmap level. The slice + * of the 3D texture is passed in 'zoffset'. + * \param usage one of the PIPE_TRANSFER_x values + * \param x, y, w, h the region of interest of the 2D image. + * \return address of mapping or NULL if any error */ GLubyte * st_texture_image_map(struct st_context *st, struct st_texture_image *stImage, - GLuint zoffset, enum pipe_transfer_usage usage, + GLuint zoffset, enum pipe_transfer_usage usage, GLuint x, GLuint y, GLuint w, GLuint h) { struct pipe_context *pipe = st->pipe; - struct pipe_screen *screen = pipe->screen; - struct pipe_texture *pt = stImage->pt; + struct pipe_resource *pt = stImage->pt; DBG("%s \n", __FUNCTION__); - stImage->transfer = st_no_flush_get_tex_transfer(st, pt, stImage->face, - stImage->level, zoffset, - usage, x, y, w, h); + stImage->transfer = pipe_get_transfer(st->pipe, pt, stImage->level, + stImage->face + zoffset, + usage, x, y, w, h); if (stImage->transfer) - return screen->transfer_map(screen, stImage->transfer); + return pipe_transfer_map(pipe, stImage->transfer); else return NULL; } @@ -213,13 +236,13 @@ void st_texture_image_unmap(struct st_context *st, struct st_texture_image *stImage) { - struct pipe_screen *screen = st->pipe->screen; + struct pipe_context *pipe = st->pipe; DBG("%s\n", __FUNCTION__); - screen->transfer_unmap(screen, stImage->transfer); + pipe_transfer_unmap(pipe, stImage->transfer); - screen->tex_transfer_destroy(stImage->transfer); + pipe->transfer_destroy(pipe, stImage->transfer); } @@ -239,19 +262,18 @@ st_surface_data(struct pipe_context *pipe, const void *src, unsigned src_stride, unsigned srcx, unsigned srcy, unsigned width, unsigned height) { - struct pipe_screen *screen = pipe->screen; - void *map = screen->transfer_map(screen, dst); + void *map = pipe_transfer_map(pipe, dst); - assert(dst->texture); + assert(dst->resource); util_copy_rect(map, - dst->texture->format, + dst->resource->format, dst->stride, dstx, dsty, width, height, src, src_stride, srcx, srcy); - screen->transfer_unmap(screen, dst); + pipe_transfer_unmap(pipe, dst); } @@ -259,26 +281,31 @@ st_surface_data(struct pipe_context *pipe, */ void st_texture_image_data(struct st_context *st, - struct pipe_texture *dst, + struct pipe_resource *dst, GLuint face, GLuint level, void *src, GLuint src_row_stride, GLuint src_image_stride) { struct pipe_context *pipe = st->pipe; - struct pipe_screen *screen = pipe->screen; - GLuint depth = u_minify(dst->depth0, level); GLuint i; const GLubyte *srcUB = src; struct pipe_transfer *dst_transfer; + GLuint layers; + + if (dst->target == PIPE_TEXTURE_1D_ARRAY || + dst->target == PIPE_TEXTURE_2D_ARRAY) + layers = dst->array_size; + else + layers = u_minify(dst->depth0, level); DBG("%s\n", __FUNCTION__); - for (i = 0; i < depth; i++) { - dst_transfer = st_no_flush_get_tex_transfer(st, dst, face, level, i, - PIPE_TRANSFER_WRITE, 0, 0, - u_minify(dst->width0, level), - u_minify(dst->height0, level)); + for (i = 0; i < layers; i++) { + dst_transfer = pipe_get_transfer(st->pipe, dst, level, face + i, + PIPE_TRANSFER_WRITE, 0, 0, + u_minify(dst->width0, level), + u_minify(dst->height0, level)); st_surface_data(pipe, dst_transfer, 0, 0, /* dstx, dsty */ @@ -286,310 +313,86 @@ st_texture_image_data(struct st_context *st, src_row_stride, 0, 0, /* source x, y */ u_minify(dst->width0, level), - u_minify(dst->height0, level)); /* width, height */ + u_minify(dst->height0, level)); /* width, height */ - screen->tex_transfer_destroy(dst_transfer); + pipe->transfer_destroy(pipe, dst_transfer); srcUB += src_image_stride; } } -/* Copy mipmap image between textures - */ -void -st_texture_image_copy(struct pipe_context *pipe, - struct pipe_texture *dst, GLuint dstLevel, - struct pipe_texture *src, - GLuint face) -{ - struct pipe_screen *screen = pipe->screen; - GLuint width = u_minify(dst->width0, dstLevel); - GLuint height = u_minify(dst->height0, dstLevel); - GLuint depth = u_minify(dst->depth0, dstLevel); - struct pipe_surface *src_surface; - struct pipe_surface *dst_surface; - GLuint i; - - for (i = 0; i < depth; i++) { - GLuint srcLevel; - - /* find src texture level of needed size */ - for (srcLevel = 0; srcLevel <= src->last_level; srcLevel++) { - if (u_minify(src->width0, srcLevel) == width && - u_minify(src->height0, srcLevel) == height) { - break; - } - } - assert(u_minify(src->width0, srcLevel) == width); - assert(u_minify(src->height0, srcLevel) == height); - -#if 0 - { - src_surface = screen->get_tex_surface(screen, src, face, srcLevel, i, - PIPE_BUFFER_USAGE_CPU_READ); - ubyte *map = screen->surface_map(screen, src_surface, PIPE_BUFFER_USAGE_CPU_READ); - map += src_surface->width * src_surface->height * 4 / 2; - printf("%s center pixel: %d %d %d %d (pt %p[%d] -> %p[%d])\n", - __FUNCTION__, - map[0], map[1], map[2], map[3], - src, srcLevel, dst, dstLevel); - - screen->surface_unmap(screen, src_surface); - pipe_surface_reference(&src_surface, NULL); - } -#endif - - dst_surface = screen->get_tex_surface(screen, dst, face, dstLevel, i, - PIPE_BUFFER_USAGE_GPU_WRITE); - - src_surface = screen->get_tex_surface(screen, src, face, srcLevel, i, - PIPE_BUFFER_USAGE_GPU_READ); - - if (pipe->surface_copy) { - pipe->surface_copy(pipe, - dst_surface, - 0, 0, /* destX, Y */ - src_surface, - 0, 0, /* srcX, Y */ - width, height); - } else { - util_surface_copy(pipe, FALSE, - dst_surface, - 0, 0, /* destX, Y */ - src_surface, - 0, 0, /* srcX, Y */ - width, height); - } - - pipe_surface_reference(&src_surface, NULL); - pipe_surface_reference(&dst_surface, NULL); - } -} - - /** - * Bind a pipe surface to a texture object. After the call, - * the texture object is marked dirty and will be (re-)validated. - * - * If this is the first surface bound, the texture object is said to - * switch from normal to surface based. It will be cleared first in - * this case. - * - * \param ps pipe surface to be unbound - * \param target texture target - * \param level image level - * \param format internal format of the texture + * For debug only: get/print center pixel in the src resource. */ -int -st_bind_texture_surface(struct pipe_surface *ps, int target, int level, - enum pipe_format format) +static void +print_center_pixel(struct pipe_context *pipe, struct pipe_resource *src) { - GET_CURRENT_CONTEXT(ctx); - const GLuint unit = ctx->Texture.CurrentUnit; - struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit]; - struct gl_texture_object *texObj; - struct gl_texture_image *texImage; - struct st_texture_object *stObj; - struct st_texture_image *stImage; - GLenum internalFormat; - - switch (target) { - case ST_TEXTURE_2D: - target = GL_TEXTURE_2D; - break; - case ST_TEXTURE_RECT: - target = GL_TEXTURE_RECTANGLE_ARB; - break; - default: - return 0; - } + struct pipe_transfer *xfer; + struct pipe_box region; + ubyte *map; - /* map pipe format to base format for now */ - if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_RGB, 3) > 0) - internalFormat = GL_RGBA; - else - internalFormat = GL_RGB; - - texObj = _mesa_select_tex_object(ctx, texUnit, target); - _mesa_lock_texture(ctx, texObj); - - stObj = st_texture_object(texObj); - /* switch to surface based */ - if (!stObj->surface_based) { - _mesa_clear_texture_object(ctx, texObj); - stObj->surface_based = GL_TRUE; - } + region.x = src->width0 / 2; + region.y = src->height0 / 2; + region.z = 0; + region.width = 1; + region.height = 1; + region.depth = 1; - texImage = _mesa_get_tex_image(ctx, texObj, target, level); - stImage = st_texture_image(texImage); + xfer = pipe->get_transfer(pipe, src, 0, PIPE_TRANSFER_READ, ®ion); + map = pipe->transfer_map(pipe, xfer); - _mesa_init_teximage_fields(ctx, target, texImage, - ps->width, ps->height, 1, 0, internalFormat); - texImage->TexFormat = st_ChooseTextureFormat(ctx, internalFormat, - GL_RGBA, GL_UNSIGNED_BYTE); - _mesa_set_fetch_functions(texImage, 2); - pipe_texture_reference(&stImage->pt, ps->texture); + printf("center pixel: %d %d %d %d\n", map[0], map[1], map[2], map[3]); - _mesa_dirty_texobj(ctx, texObj, GL_TRUE); - _mesa_unlock_texture(ctx, texObj); - - return 1; + pipe->transfer_unmap(pipe, xfer); + pipe->transfer_destroy(pipe, xfer); } /** - * Unbind a pipe surface from a texture object. After the call, - * the texture object is marked dirty and will be (re-)validated. - * - * \param ps pipe surface to be unbound - * \param target texture target - * \param level image level + * Copy the image at level=0 in 'src' to the 'dst' resource at 'dstLevel'. + * This is used to copy mipmap images from one texture buffer to another. + * This typically happens when our initial guess at the total texture size + * is incorrect (see the guess_and_alloc_texture() function). */ -int -st_unbind_texture_surface(struct pipe_surface *ps, int target, int level) -{ - GET_CURRENT_CONTEXT(ctx); - const GLuint unit = ctx->Texture.CurrentUnit; - struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit]; - struct gl_texture_object *texObj; - struct gl_texture_image *texImage; - struct st_texture_object *stObj; - struct st_texture_image *stImage; - - switch (target) { - case ST_TEXTURE_2D: - target = GL_TEXTURE_2D; - break; - case ST_TEXTURE_RECT: - target = GL_TEXTURE_RECTANGLE_ARB; - break; - default: - return 0; - } - - texObj = _mesa_select_tex_object(ctx, texUnit, target); - - _mesa_lock_texture(ctx, texObj); - - texImage = _mesa_get_tex_image(ctx, texObj, target, level); - stObj = st_texture_object(texObj); - stImage = st_texture_image(texImage); - - /* Make sure the pipe surface is still bound. The texture object is still - * considered surface based even if this is the last bound surface. */ - if (stImage->pt == ps->texture) { - pipe_texture_reference(&stImage->pt, NULL); - _mesa_clear_texture_image(ctx, texImage); - - _mesa_dirty_texobj(ctx, texObj, GL_TRUE); - } - - _mesa_unlock_texture(ctx, texObj); - - return 1; -} - - -/** Redirect rendering into stfb's surface to a texture image */ -int -st_bind_teximage(struct st_framebuffer *stfb, uint surfIndex, - int target, int format, int level) -{ - GET_CURRENT_CONTEXT(ctx); - struct st_context *st = ctx->st; - struct pipe_context *pipe = st->pipe; - struct pipe_screen *screen = pipe->screen; - const GLuint unit = ctx->Texture.CurrentUnit; - struct gl_texture_unit *texUnit = &ctx->Texture.Unit[unit]; - struct gl_texture_object *texObj; - struct gl_texture_image *texImage; - struct st_texture_image *stImage; - struct st_renderbuffer *strb; - GLint face = 0, slice = 0; - - assert(surfIndex <= ST_SURFACE_DEPTH); - - strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer); - - if (strb->texture_save || strb->surface_save) { - /* Error! */ - return 0; - } - - if (target == ST_TEXTURE_2D) { - texObj = texUnit->CurrentTex[TEXTURE_2D_INDEX]; - texImage = _mesa_get_tex_image(ctx, texObj, GL_TEXTURE_2D, level); - stImage = st_texture_image(texImage); - } - else { - /* unsupported target */ - return 0; - } - - st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL); - - /* save the renderbuffer's surface/texture info */ - pipe_texture_reference(&strb->texture_save, strb->texture); - pipe_surface_reference(&strb->surface_save, strb->surface); - - /* plug in new surface/texture info */ - pipe_texture_reference(&strb->texture, stImage->pt); - strb->surface = screen->get_tex_surface(screen, strb->texture, - face, level, slice, - (PIPE_BUFFER_USAGE_GPU_READ | - PIPE_BUFFER_USAGE_GPU_WRITE)); - - st->dirty.st |= ST_NEW_FRAMEBUFFER; - - return 1; -} - - -/** Undo surface-to-texture binding */ -int -st_release_teximage(struct st_framebuffer *stfb, uint surfIndex, - int target, int format, int level) +void +st_texture_image_copy(struct pipe_context *pipe, + struct pipe_resource *dst, GLuint dstLevel, + struct pipe_resource *src, GLuint srcLevel, + GLuint face) { - GET_CURRENT_CONTEXT(ctx); - struct st_context *st = ctx->st; - struct st_renderbuffer *strb; - - assert(surfIndex <= ST_SURFACE_DEPTH); + GLuint width = u_minify(dst->width0, dstLevel); + GLuint height = u_minify(dst->height0, dstLevel); + GLuint depth = u_minify(dst->depth0, dstLevel); + struct pipe_box src_box; + GLuint i; - strb = st_renderbuffer(stfb->Base.Attachment[surfIndex].Renderbuffer); + assert(u_minify(src->width0, srcLevel) == width); + assert(u_minify(src->height0, srcLevel) == height); + assert(u_minify(src->depth0, srcLevel) == depth); + + src_box.x = 0; + src_box.y = 0; + src_box.width = width; + src_box.height = height; + src_box.depth = 1; + /* Loop over 3D image slices */ + /* could (and probably should) use "true" 3d box here - + but drivers can't quite handle it yet */ + for (i = face; i < face + depth; i++) { + src_box.z = i; + + if (0) { + print_center_pixel(pipe, src); + } - if (!strb->texture_save || !strb->surface_save) { - /* Error! */ - return 0; + pipe->resource_copy_region(pipe, + dst, + dstLevel, + 0, 0, i,/* destX, Y, Z */ + src, + srcLevel, + &src_box); } - - st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL); - - /* free tex surface, restore original */ - pipe_surface_reference(&strb->surface, strb->surface_save); - pipe_texture_reference(&strb->texture, strb->texture_save); - - pipe_surface_reference(&strb->surface_save, NULL); - pipe_texture_reference(&strb->texture_save, NULL); - - st->dirty.st |= ST_NEW_FRAMEBUFFER; - - return 1; } -void -st_teximage_flush_before_map(struct st_context *st, - struct pipe_texture *pt, - unsigned int face, - unsigned int level, - enum pipe_transfer_usage usage) -{ - struct pipe_context *pipe = st->pipe; - unsigned referenced = - pipe->is_texture_referenced(pipe, pt, face, level); - - if (referenced && ((referenced & PIPE_REFERENCED_FOR_WRITE) || - (usage & PIPE_TRANSFER_WRITE))) - st->pipe->flush(st->pipe, PIPE_FLUSH_RENDER_CACHE, NULL); -}