1f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**************************************************************************
2f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
3f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * All Rights Reserved.
5f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
6f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Permission is hereby granted, free of charge, to any person obtaining a
7f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * copy of this software and associated documentation files (the
8f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * "Software"), to deal in the Software without restriction, including
9f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * without limitation the rights to use, copy, modify, merge, publish,
10f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * distribute, sub license, and/or sell copies of the Software, and to
11f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * permit persons to whom the Software is furnished to do so, subject to
12f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * the following conditions:
13f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
14f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * The above copyright notice and this permission notice (including the
15f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * next paragraph) shall be included in all copies or substantial portions
16f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * of the Software.
17f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
18f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
26f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org **************************************************************************/
27f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
28f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "main/mfeatures.h"
29f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "main/bufferobj.h"
30f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "main/enums.h"
31f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "main/fbobject.h"
32f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "main/formats.h"
33f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "main/image.h"
34f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "main/imports.h"
35f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "main/macros.h"
36f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "main/mipmap.h"
37f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "main/pack.h"
38f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "main/pbo.h"
39f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "main/pixeltransfer.h"
40f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "main/texcompress.h"
41f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "main/texgetimage.h"
42f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "main/teximage.h"
43f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "main/texobj.h"
44f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "main/texstore.h"
45f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
46f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "state_tracker/st_debug.h"
47f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "state_tracker/st_context.h"
48f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "state_tracker/st_cb_fbo.h"
49f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "state_tracker/st_cb_flush.h"
50f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "state_tracker/st_cb_texture.h"
51f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "state_tracker/st_format.h"
52f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "state_tracker/st_texture.h"
53f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "state_tracker/st_gen_mipmap.h"
54f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "state_tracker/st_atom.h"
55f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
56f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "pipe/p_context.h"
57f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "pipe/p_defines.h"
58f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "util/u_inlines.h"
59f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "pipe/p_shader_tokens.h"
60f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "util/u_tile.h"
61f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "util/u_blit.h"
62f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "util/u_format.h"
63f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "util/u_surface.h"
64f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "util/u_sampler.h"
65f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "util/u_math.h"
66f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#include "util/u_box.h"
67f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
68f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org#define DBG if (0) printf
69f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
70f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
71f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic enum pipe_texture_target
72f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orggl_target_to_pipe(GLenum target)
73f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
74f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   switch (target) {
75f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   case GL_TEXTURE_1D:
76f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return PIPE_TEXTURE_1D;
77f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   case GL_TEXTURE_2D:
78f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   case GL_TEXTURE_EXTERNAL_OES:
79f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return PIPE_TEXTURE_2D;
80f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   case GL_TEXTURE_RECTANGLE_NV:
81f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return PIPE_TEXTURE_RECT;
82f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   case GL_TEXTURE_3D:
83f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return PIPE_TEXTURE_3D;
84f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   case GL_TEXTURE_CUBE_MAP_ARB:
85f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return PIPE_TEXTURE_CUBE;
86f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   case GL_TEXTURE_1D_ARRAY_EXT:
87f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return PIPE_TEXTURE_1D_ARRAY;
88f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   case GL_TEXTURE_2D_ARRAY_EXT:
89f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return PIPE_TEXTURE_2D_ARRAY;
90f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   case GL_TEXTURE_BUFFER:
91f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return PIPE_BUFFER;
92f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   default:
93f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      assert(0);
94f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return 0;
95f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
96f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
97f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
98f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
99f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/** called via ctx->Driver.NewTextureImage() */
100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic struct gl_texture_image *
101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgst_NewTextureImage(struct gl_context * ctx)
102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   DBG("%s\n", __FUNCTION__);
104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   (void) ctx;
105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return (struct gl_texture_image *) ST_CALLOC_STRUCT(st_texture_image);
106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/** called via ctx->Driver.DeleteTextureImage() */
110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void
111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgst_DeleteTextureImage(struct gl_context * ctx, struct gl_texture_image *img)
112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* nothing special (yet) for st_texture_image */
114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   _mesa_delete_texture_image(ctx, img);
115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/** called via ctx->Driver.NewTextureObject() */
119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic struct gl_texture_object *
120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgst_NewTextureObject(struct gl_context * ctx, GLuint name, GLenum target)
121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_texture_object *obj = ST_CALLOC_STRUCT(st_texture_object);
123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   DBG("%s\n", __FUNCTION__);
125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   _mesa_initialize_texture_object(&obj->base, name, target);
126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return &obj->base;
128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
129f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
130f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/** called via ctx->Driver.DeleteTextureObject() */
131f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void
132f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgst_DeleteTextureObject(struct gl_context *ctx,
133f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                       struct gl_texture_object *texObj)
134f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
135f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_context *st = st_context(ctx);
136f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_texture_object *stObj = st_texture_object(texObj);
137f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (stObj->pt)
138f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      pipe_resource_reference(&stObj->pt, NULL);
139f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (stObj->sampler_view) {
140f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      pipe_sampler_view_release(st->pipe, &stObj->sampler_view);
141f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
142f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   _mesa_delete_texture_object(ctx, texObj);
143f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
144f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
145f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
146f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/** called via ctx->Driver.FreeTextureImageBuffer() */
147f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void
148f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgst_FreeTextureImageBuffer(struct gl_context *ctx,
149f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                          struct gl_texture_image *texImage)
150f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
151f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_texture_image *stImage = st_texture_image(texImage);
152f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
153f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   DBG("%s\n", __FUNCTION__);
154f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
155f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (stImage->pt) {
156f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      pipe_resource_reference(&stImage->pt, NULL);
157f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
158f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
159f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (stImage->TexData) {
160f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      _mesa_align_free(stImage->TexData);
161f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      stImage->TexData = NULL;
162f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
163f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
164f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
165f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
166f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/** called via ctx->Driver.MapTextureImage() */
167f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void
168f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgst_MapTextureImage(struct gl_context *ctx,
169f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                   struct gl_texture_image *texImage,
170f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                   GLuint slice, GLuint x, GLuint y, GLuint w, GLuint h,
171f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                   GLbitfield mode,
172f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                   GLubyte **mapOut, GLint *rowStrideOut)
173f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
174f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_context *st = st_context(ctx);
175f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_texture_image *stImage = st_texture_image(texImage);
176f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   unsigned pipeMode;
177f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   GLubyte *map;
178f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
179f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   pipeMode = 0x0;
180f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (mode & GL_MAP_READ_BIT)
181f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      pipeMode |= PIPE_TRANSFER_READ;
182f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (mode & GL_MAP_WRITE_BIT)
183f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      pipeMode |= PIPE_TRANSFER_WRITE;
184f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (mode & GL_MAP_INVALIDATE_RANGE_BIT)
185f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      pipeMode |= PIPE_TRANSFER_DISCARD_RANGE;
186f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
187f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   map = st_texture_image_map(st, stImage, slice, pipeMode, x, y, w, h);
188f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (map) {
189f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      *mapOut = map;
190f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      *rowStrideOut = stImage->transfer->stride;
191f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
192f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   else {
193f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      *mapOut = NULL;
194f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      *rowStrideOut = 0;
195f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
196f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
197f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
198f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
199f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/** called via ctx->Driver.UnmapTextureImage() */
200f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void
201f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgst_UnmapTextureImage(struct gl_context *ctx,
202f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                     struct gl_texture_image *texImage,
203f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                     GLuint slice)
204f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
205f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_context *st = st_context(ctx);
206f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_texture_image *stImage  = st_texture_image(texImage);
207f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   st_texture_image_unmap(st, stImage);
208f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
209f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
210f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
211f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
212f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Return default texture resource binding bitmask for the given format.
213f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
214f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic GLuint
215f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgdefault_bindings(struct st_context *st, enum pipe_format format)
216f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
217f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct pipe_screen *screen = st->pipe->screen;
218f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   const unsigned target = PIPE_TEXTURE_2D;
219f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   unsigned bindings;
220f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
221f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (util_format_is_depth_or_stencil(format))
222f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DEPTH_STENCIL;
223f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   else
224f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
225f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
226f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (screen->is_format_supported(screen, format, target, 0, bindings))
227f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return bindings;
228f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   else {
229f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* Try non-sRGB. */
230f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      format = util_format_linear(format);
231f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
232f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (screen->is_format_supported(screen, format, target, 0, bindings))
233f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         return bindings;
234f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      else
235f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         return PIPE_BIND_SAMPLER_VIEW;
236f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
237f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
238f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
239f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
240f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
241f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Given the size of a mipmap image, try to compute the size of the level=0
242f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * mipmap image.
243f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
244f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Note that this isn't always accurate for odd-sized, non-POW textures.
245f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
246f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
247f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * \return GL_TRUE for success, GL_FALSE for failure
248f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
249f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic GLboolean
250f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgguess_base_level_size(GLenum target,
251f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                      GLuint width, GLuint height, GLuint depth, GLuint level,
252f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                      GLuint *width0, GLuint *height0, GLuint *depth0)
253f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
254f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   assert(width >= 1);
255f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   assert(height >= 1);
256f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   assert(depth >= 1);
257f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
258f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (level > 0) {
259f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* Guess the size of the base level.
260f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * Depending on the image's size, we can't always make a guess here.
261f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       */
262f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      switch (target) {
263f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      case GL_TEXTURE_1D:
264f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      case GL_TEXTURE_1D_ARRAY:
265f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         width <<= level;
266f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         break;
267f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
268f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      case GL_TEXTURE_2D:
269f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      case GL_TEXTURE_2D_ARRAY:
270f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         /* We can't make a good guess here, because the base level dimensions
271f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          * can be non-square.
272f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          */
273f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         if (width == 1 || height == 1) {
274f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            return GL_FALSE;
275f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         }
276f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         width <<= level;
277f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         height <<= level;
278f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         break;
279f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
280f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      case GL_TEXTURE_CUBE_MAP:
281f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      case GL_TEXTURE_CUBE_MAP_ARRAY:
282f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         width <<= level;
283f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         height <<= level;
284f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         break;
285f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
286f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      case GL_TEXTURE_3D:
287f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         /* We can't make a good guess here, because the base level dimensions
288f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          * can be non-cube.
289f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          */
290f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         if (width == 1 || height == 1 || depth == 1) {
291f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            return GL_FALSE;
292f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         }
293f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         width <<= level;
294f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         height <<= level;
295f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         depth <<= level;
296f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         break;
297f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
298f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      case GL_TEXTURE_RECTANGLE:
299f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         break;
300f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
301f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      default:
302f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         assert(0);
303f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
304f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
305f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
306f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   *width0 = width;
307f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   *height0 = height;
308f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   *depth0 = depth;
309f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
310f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return GL_TRUE;
311f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
312f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
313f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
314f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
315f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Try to allocate a pipe_resource object for the given st_texture_object.
316f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
317f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * We use the given st_texture_image as a clue to determine the size of the
318f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * mipmap image at level=0.
319f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
320f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * \return GL_TRUE for success, GL_FALSE if out of memory.
321f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
322f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic GLboolean
323f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgguess_and_alloc_texture(struct st_context *st,
324f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org			struct st_texture_object *stObj,
325f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org			const struct st_texture_image *stImage)
326f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
327f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   GLuint lastLevel, width, height, depth;
328f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   GLuint bindings;
329f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   GLuint ptWidth, ptHeight, ptDepth, ptLayers;
330f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   enum pipe_format fmt;
331f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
332f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   DBG("%s\n", __FUNCTION__);
333f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
334f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   assert(!stObj->pt);
335f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
336f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (!guess_base_level_size(stObj->base.Target,
337f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                              stImage->base.Width2,
338f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                              stImage->base.Height2,
339f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                              stImage->base.Depth2,
340f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                              stImage->base.Level,
341f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                              &width, &height, &depth)) {
342f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* we can't determine the image size at level=0 */
343f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      stObj->width0 = stObj->height0 = stObj->depth0 = 0;
344f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* this is not an out of memory error */
345f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return GL_TRUE;
346f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
347f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
348f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* At this point, (width x height x depth) is the expected size of
349f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * the level=0 mipmap image.
350f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
351f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
352f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Guess a reasonable value for lastLevel.  With OpenGL we have no
353f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * idea how many mipmap levels will be in a texture until we start
354f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * to render with it.  Make an educated guess here but be prepared
355f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * to re-allocating a texture buffer with space for more (or fewer)
356f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * mipmap levels later.
357f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
358f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if ((stObj->base.Sampler.MinFilter == GL_NEAREST ||
359f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org        stObj->base.Sampler.MinFilter == GL_LINEAR ||
360f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org        stImage->base._BaseFormat == GL_DEPTH_COMPONENT ||
361f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org        stImage->base._BaseFormat == GL_DEPTH_STENCIL_EXT) &&
362f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       !stObj->base.GenerateMipmap &&
363f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       stImage->base.Level == 0) {
364f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* only alloc space for a single mipmap level */
365f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      lastLevel = 0;
366f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
367f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   else {
368f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* alloc space for a full mipmap */
369f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      lastLevel = _mesa_get_tex_max_num_levels(stObj->base.Target,
370f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                               width, height, depth) - 1;
371f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
372f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
373f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Save the level=0 dimensions */
374f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   stObj->width0 = width;
375f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   stObj->height0 = height;
376f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   stObj->depth0 = depth;
377f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
378f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   fmt = st_mesa_format_to_pipe_format(stImage->base.TexFormat);
379f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
380f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   bindings = default_bindings(st, fmt);
381f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
382f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
383f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                   width, height, depth,
384f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                   &ptWidth, &ptHeight, &ptDepth, &ptLayers);
385f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
386f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   stObj->pt = st_texture_create(st,
387f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 gl_target_to_pipe(stObj->base.Target),
388f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 fmt,
389f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 lastLevel,
390f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 ptWidth,
391f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 ptHeight,
392f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 ptDepth,
393f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 ptLayers,
394f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 bindings);
395f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
396f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   stObj->lastLevel = lastLevel;
397f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
398f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   DBG("%s returning %d\n", __FUNCTION__, (stObj->pt != NULL));
399f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
400f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return stObj->pt != NULL;
401f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
402f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
403f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
404f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
405f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Called via ctx->Driver.AllocTextureImageBuffer().
406f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * If the texture object/buffer already has space for the indicated image,
407f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * we're done.  Otherwise, allocate memory for the new texture image.
408f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
409f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic GLboolean
410f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgst_AllocTextureImageBuffer(struct gl_context *ctx,
411f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                           struct gl_texture_image *texImage)
412f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
413f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_context *st = st_context(ctx);
414f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_texture_image *stImage = st_texture_image(texImage);
415f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
416f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   const GLuint level = texImage->Level;
417f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   GLuint width = texImage->Width;
418f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   GLuint height = texImage->Height;
419f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   GLuint depth = texImage->Depth;
420f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
421f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   DBG("%s\n", __FUNCTION__);
422f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
423f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   assert(!stImage->TexData);
424f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   assert(!stImage->pt); /* xxx this might be wrong */
425f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
426f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Look if the parent texture object has space for this image */
427f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (stObj->pt &&
428f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       level <= stObj->pt->last_level &&
429f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       st_texture_match_image(stObj->pt, texImage)) {
430f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* this image will fit in the existing texture object's memory */
431f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      pipe_resource_reference(&stImage->pt, stObj->pt);
432f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return GL_TRUE;
433f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
434f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
435f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* The parent texture object does not have space for this image */
436f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
437f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   pipe_resource_reference(&stObj->pt, NULL);
438f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   pipe_sampler_view_release(st->pipe, &stObj->sampler_view);
439f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
440f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (!guess_and_alloc_texture(st, stObj, stImage)) {
441f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* Probably out of memory.
442f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * Try flushing any pending rendering, then retry.
443f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       */
444f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      st_finish(st);
445f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (!guess_and_alloc_texture(st, stObj, stImage)) {
446f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
447f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         return GL_FALSE;
448f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
449f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
450f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
451f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (stObj->pt &&
452f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       st_texture_match_image(stObj->pt, texImage)) {
453f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* The image will live in the object's mipmap memory */
454f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      pipe_resource_reference(&stImage->pt, stObj->pt);
455f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      assert(stImage->pt);
456f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return GL_TRUE;
457f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
458f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   else {
459f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* Create a new, temporary texture/resource/buffer to hold this
460f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * one texture image.  Note that when we later access this image
461f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * (either for mapping or copying) we'll want to always specify
462f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * mipmap level=0, even if the image represents some other mipmap
463f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * level.
464f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       */
465f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      enum pipe_format format =
466f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         st_mesa_format_to_pipe_format(texImage->TexFormat);
467f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      GLuint bindings = default_bindings(st, format);
468f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      GLuint ptWidth, ptHeight, ptDepth, ptLayers;
469f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
470f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
471f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                      width, height, depth,
472f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                      &ptWidth, &ptHeight, &ptDepth, &ptLayers);
473f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
474f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      stImage->pt = st_texture_create(st,
475f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                      gl_target_to_pipe(stObj->base.Target),
476f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                      format,
477f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                      0, /* lastLevel */
478f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                      ptWidth,
479f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                      ptHeight,
480f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                      ptDepth,
481f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                      ptLayers,
482f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                      bindings);
483f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return stImage->pt != NULL;
484f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
485f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
486f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
487f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
488f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
489f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Preparation prior to glTexImage.  Basically check the 'surface_based'
490f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * field and switch to a "normal" tex image if necessary.
491f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
492f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void
493f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgprep_teximage(struct gl_context *ctx, struct gl_texture_image *texImage,
494f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org              GLenum format, GLenum type)
495f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
496f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct gl_texture_object *texObj = texImage->TexObject;
497f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_texture_object *stObj = st_texture_object(texObj);
498f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
499f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* switch to "normal" */
500f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (stObj->surface_based) {
501f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      const GLenum target = texObj->Target;
502f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      const GLuint level = texImage->Level;
503f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      gl_format texFormat;
504f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
505f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      _mesa_clear_texture_object(ctx, texObj);
506f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      pipe_resource_reference(&stObj->pt, NULL);
507f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
508f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* oops, need to init this image again */
509f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
510f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                              texImage->InternalFormat, format,
511f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                              type);
512f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
513f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      _mesa_init_teximage_fields(ctx, texImage,
514f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 texImage->Width, texImage->Height,
515f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 texImage->Depth, texImage->Border,
516f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 texImage->InternalFormat, texFormat);
517f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
518f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      stObj->surface_based = GL_FALSE;
519f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
520f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
521f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
522f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
523f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void
524f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgst_TexImage(struct gl_context * ctx, GLuint dims,
525f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            struct gl_texture_image *texImage,
526f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            GLenum format, GLenum type, const void *pixels,
527f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            const struct gl_pixelstore_attrib *unpack)
528f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
529f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   prep_teximage(ctx, texImage, format, type);
530f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   _mesa_store_teximage(ctx, dims, texImage, format, type, pixels, unpack);
531f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
532f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
533f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
534f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void
535f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgst_CompressedTexImage(struct gl_context *ctx, GLuint dims,
536f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                      struct gl_texture_image *texImage,
537f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                      GLsizei imageSize, const GLvoid *data)
538f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
539f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   prep_teximage(ctx, texImage, GL_NONE, GL_NONE);
540f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   _mesa_store_compressed_teximage(ctx, dims, texImage, imageSize, data);
541f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
542f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
543f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
544f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
545f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
546f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * glGetTexImage() helper: decompress a compressed texture by rendering
547f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * a textured quad.  Store the results in the user's buffer.
548f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
549f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void
550f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgdecompress_with_blit(struct gl_context * ctx,
551f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                     GLenum format, GLenum type, GLvoid *pixels,
552f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                     struct gl_texture_image *texImage)
553f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
554f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_context *st = st_context(ctx);
555f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct pipe_context *pipe = st->pipe;
556f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_texture_image *stImage = st_texture_image(texImage);
557f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
558f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct pipe_sampler_view *src_view;
559f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   const GLuint width = texImage->Width;
560f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   const GLuint height = texImage->Height;
561f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct pipe_surface *dst_surface;
562f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct pipe_resource *dst_texture;
563f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct pipe_transfer *tex_xfer;
564f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   unsigned bind = (PIPE_BIND_RENDER_TARGET | /* util_blit may choose to render */
565f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		    PIPE_BIND_TRANSFER_READ);
566f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
567f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* create temp / dest surface */
568f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (!util_create_rgba_surface(pipe, width, height, bind,
569f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 &dst_texture, &dst_surface)) {
570f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      _mesa_problem(ctx, "util_create_rgba_surface() failed "
571f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                    "in decompress_with_blit()");
572f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return;
573f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
574f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
575f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Disable conditional rendering. */
576f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (st->render_condition) {
577f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      pipe->render_condition(pipe, NULL, 0);
578f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
579f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
580f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Create sampler view that limits fetches to the source mipmap level */
581f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   {
582f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      struct pipe_sampler_view sv_temp;
583f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
584f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      u_sampler_view_default_template(&sv_temp, stObj->pt, stObj->pt->format);
585f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
586f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      sv_temp.format = util_format_linear(sv_temp.format);
587f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      sv_temp.u.tex.first_level =
588f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      sv_temp.u.tex.last_level = texImage->Level;
589f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
590f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      src_view = pipe->create_sampler_view(pipe, stObj->pt, &sv_temp);
591f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (!src_view) {
592f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
593f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         return;
594f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
595f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
596f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
597f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* blit/render/decompress */
598f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   util_blit_pixels_tex(st->blit,
599f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                        src_view,      /* pipe_resource (src) */
600f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                        0, 0,             /* src x0, y0 */
601f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                        width, height,    /* src x1, y1 */
602f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                        dst_surface,      /* pipe_surface (dst) */
603f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                        0, 0,             /* dst x0, y0 */
604f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                        width, height,    /* dst x1, y1 */
605f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                        0.0,              /* z */
606f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                        PIPE_TEX_MIPFILTER_NEAREST);
607f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
608f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Restore conditional rendering state. */
609f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (st->render_condition) {
610f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      pipe->render_condition(pipe, st->render_condition,
611f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                             st->condition_mode);
612f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
613f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
614f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* map the dst_surface so we can read from it */
615f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   tex_xfer = pipe_get_transfer(pipe,
616f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                dst_texture, 0, 0,
617f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                PIPE_TRANSFER_READ,
618f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                0, 0, width, height);
619f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
620f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
621f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
622f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* copy/pack data into user buffer */
623f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (_mesa_format_matches_format_and_type(stImage->base.TexFormat,
624f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                            format, type,
625f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                            ctx->Pack.SwapBytes)) {
626f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* memcpy */
627f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      const uint bytesPerRow = width * util_format_get_blocksize(stImage->pt->format);
628f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      ubyte *map = pipe_transfer_map(pipe, tex_xfer);
629f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      GLuint row;
630f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      for (row = 0; row < height; row++) {
631f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
632f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                              height, format, type, row, 0);
633f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         memcpy(dest, map, bytesPerRow);
634f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         map += tex_xfer->stride;
635f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
636f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      pipe_transfer_unmap(pipe, tex_xfer);
637f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
638f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   else {
639f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* format translation via floats */
640f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      GLuint row;
641f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      enum pipe_format pformat = util_format_linear(dst_texture->format);
642f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      GLfloat *rgba;
643f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
644f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      rgba = (GLfloat *) malloc(width * 4 * sizeof(GLfloat));
645f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (!rgba) {
646f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
647f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         goto end;
648f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
649f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
650f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      for (row = 0; row < height; row++) {
651f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         const GLbitfield transferOps = 0x0; /* bypassed for glGetTexImage() */
652f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
653f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                              height, format, type, row, 0);
654f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
655f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         if (ST_DEBUG & DEBUG_FALLBACK)
656f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            debug_printf("%s: fallback format translation\n", __FUNCTION__);
657f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
658f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         /* get float[4] rgba row from surface */
659f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         pipe_get_tile_rgba_format(pipe, tex_xfer, 0, row, width, 1,
660f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                   pformat, rgba);
661f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
662f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba, format,
663f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                    type, dest, &ctx->Pack, transferOps);
664f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
665f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
666f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      free(rgba);
667f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
668f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
669f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgend:
670f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
671f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
672f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   pipe->transfer_destroy(pipe, tex_xfer);
673f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
674f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* destroy the temp / dest surface */
675f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   util_destroy_rgba_surface(dst_texture, dst_surface);
676f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
677f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   pipe_sampler_view_release(pipe, &src_view);
678f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
679f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
680f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
681f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
682f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
683f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Called via ctx->Driver.GetTexImage()
684f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
685f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void
686f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgst_GetTexImage(struct gl_context * ctx,
687f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org               GLenum format, GLenum type, GLvoid * pixels,
688f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org               struct gl_texture_image *texImage)
689f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
690f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_texture_image *stImage = st_texture_image(texImage);
691f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
692f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (stImage->pt && util_format_is_s3tc(stImage->pt->format)) {
693f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* Need to decompress the texture.
694f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * We'll do this by rendering a textured quad (which is hopefully
695f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * faster than using the fallback code in texcompress.c).
696f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * Note that we only expect RGBA formats (no Z/depth formats).
697f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       */
698f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      decompress_with_blit(ctx, format, type, pixels, texImage);
699f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
700f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   else {
701f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      _mesa_get_teximage(ctx, format, type, pixels, texImage);
702f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
703f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
704f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
705f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
706f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
707f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Do a CopyTexSubImage operation using a read transfer from the source,
708f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * a write transfer to the destination and get_tile()/put_tile() to access
709f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * the pixels/texels.
710f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
711f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Note: srcY=0=TOP of renderbuffer
712f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
713f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void
714f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgfallback_copy_texsubimage(struct gl_context *ctx,
715f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                          struct st_renderbuffer *strb,
716f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                          struct st_texture_image *stImage,
717f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                          GLenum baseFormat,
718f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                          GLint destX, GLint destY, GLint destZ,
719f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                          GLint srcX, GLint srcY,
720f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                          GLsizei width, GLsizei height)
721f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
722f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_context *st = st_context(ctx);
723f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct pipe_context *pipe = st->pipe;
724f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct pipe_transfer *src_trans;
725f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   GLvoid *texDest;
726f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   enum pipe_transfer_usage transfer_usage;
727f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
728f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (ST_DEBUG & DEBUG_FALLBACK)
729f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      debug_printf("%s: fallback processing\n", __FUNCTION__);
730f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
731f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
732f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      srcY = strb->Base.Height - srcY - height;
733f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
734f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
735f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   src_trans = pipe_get_transfer(pipe,
736f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 strb->texture,
737f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 strb->rtt_level,
738f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 strb->rtt_face + strb->rtt_slice,
739f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 PIPE_TRANSFER_READ,
740f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 srcX, srcY,
741f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 width, height);
742f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
743f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if ((baseFormat == GL_DEPTH_COMPONENT ||
744f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org        baseFormat == GL_DEPTH_STENCIL) &&
745f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       util_format_is_depth_and_stencil(stImage->pt->format))
746f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      transfer_usage = PIPE_TRANSFER_READ_WRITE;
747f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   else
748f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      transfer_usage = PIPE_TRANSFER_WRITE;
749f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
750f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* XXX this used to ignore destZ param */
751f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   texDest = st_texture_image_map(st, stImage, destZ, transfer_usage,
752f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                  destX, destY, width, height);
753f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
754f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (baseFormat == GL_DEPTH_COMPONENT ||
755f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       baseFormat == GL_DEPTH_STENCIL) {
756f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
757f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                     ctx->Pixel.DepthBias != 0.0F);
758f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      GLint row, yStep;
759f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      uint *data;
760f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
761f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* determine bottom-to-top vs. top-to-bottom order for src buffer */
762f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
763f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         srcY = height - 1;
764f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         yStep = -1;
765f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
766f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      else {
767f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         srcY = 0;
768f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         yStep = 1;
769f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
770f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
771f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      data = (uint *) malloc(width * sizeof(uint));
772f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
773f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (data) {
774f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         /* To avoid a large temp memory allocation, do copy row by row */
775f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         for (row = 0; row < height; row++, srcY += yStep) {
776f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            pipe_get_tile_z(pipe, src_trans, 0, srcY, width, 1, data);
777f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            if (scaleOrBias) {
778f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org               _mesa_scale_and_bias_depth_uint(ctx, width, data);
779f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            }
780f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            pipe_put_tile_z(pipe, stImage->transfer, 0, row, width, 1, data);
781f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         }
782f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
783f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      else {
784f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage()");
785f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
786f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
787f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      free(data);
788f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
789f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   else {
790f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* RGBA format */
791f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      GLfloat *tempSrc =
792f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
793f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
794f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (tempSrc && texDest) {
795f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         const GLint dims = 2;
796f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         const GLint dstRowStride = stImage->transfer->stride;
797f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         struct gl_texture_image *texImage = &stImage->base;
798f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
799f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
800f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
801f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            unpack.Invert = GL_TRUE;
802f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         }
803f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
804f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         /* get float/RGBA image from framebuffer */
805f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         /* XXX this usually involves a lot of int/float conversion.
806f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          * try to avoid that someday.
807f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          */
808f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         pipe_get_tile_rgba_format(pipe, src_trans, 0, 0, width, height,
809f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                   util_format_linear(strb->texture->format),
810f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                   tempSrc);
811f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
812f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         /* Store into texture memory.
813f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          * Note that this does some special things such as pixel transfer
814f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          * ops and format conversion.  In particular, if the dest tex format
815f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          * is actually RGBA but the user created the texture as GL_RGB we
816f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          * need to fill-in/override the alpha channel with 1.0.
817f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          */
818f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         _mesa_texstore(ctx, dims,
819f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                        texImage->_BaseFormat,
820f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                        texImage->TexFormat,
821f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                        dstRowStride,
822f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                        (GLubyte **) &texDest,
823f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                        width, height, 1,
824f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                        GL_RGBA, GL_FLOAT, tempSrc, /* src */
825f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                        &unpack);
826f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
827f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      else {
828f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
829f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
830f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
831f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (tempSrc)
832f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         free(tempSrc);
833f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
834f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
835f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   st_texture_image_unmap(st, stImage);
836f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   pipe->transfer_destroy(pipe, src_trans);
837f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
838f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
839f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
840f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
841f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
842f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * If the format of the src renderbuffer and the format of the dest
843f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * texture are compatible (in terms of blitting), return a TGSI writemask
844f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * to be used during the blit.
845f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * If the src/dest are incompatible, return 0.
846f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
847f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic unsigned
848f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgcompatible_src_dst_formats(struct gl_context *ctx,
849f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                           const struct gl_renderbuffer *src,
850f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                           const struct gl_texture_image *dst)
851f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
852f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Get logical base formats for the src and dest.
853f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * That is, use the user-requested formats and not the actual, device-
854f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * chosen formats.
855f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * For example, the user may have requested an A8 texture but the
856f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * driver may actually be using an RGBA texture format.  When we
857f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * copy/blit to that texture, we only want to copy the Alpha channel
858f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * and not the RGB channels.
859f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    *
860f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * Similarly, when the src FBO was created an RGB format may have been
861f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * requested but the driver actually chose an RGBA format.  In that case,
862f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * we don't want to copy the undefined Alpha channel to the dest texture
863f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * (it should be 1.0).
864f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
865f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   const GLenum srcFormat = _mesa_base_fbo_format(ctx, src->InternalFormat);
866f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   const GLenum dstFormat = _mesa_base_tex_format(ctx, dst->InternalFormat);
867f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
868f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /**
869f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * XXX when we have red-only and red/green renderbuffers we'll need
870f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * to add more cases here (or implement a general-purpose routine that
871f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * queries the existance of the R,G,B,A channels in the src and dest).
872f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
873f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (srcFormat == dstFormat) {
874f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* This is the same as matching_base_formats, which should
875f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * always pass, as it did previously.
876f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       */
877f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return TGSI_WRITEMASK_XYZW;
878f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
879f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   else if (srcFormat == GL_RGB && dstFormat == GL_RGBA) {
880f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* Make sure that A in the dest is 1.  The actual src format
881f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * may be RGBA and have undefined A values.
882f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       */
883f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return TGSI_WRITEMASK_XYZ;
884f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
885f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   else if (srcFormat == GL_RGBA && dstFormat == GL_RGB) {
886f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* Make sure that A in the dest is 1.  The actual dst format
887f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * may be RGBA and will need A=1 to provide proper alpha values
888f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * when sampled later.
889f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       */
890f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return TGSI_WRITEMASK_XYZ;
891f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
892f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   else {
893f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (ST_DEBUG & DEBUG_FALLBACK)
894f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         debug_printf("%s failed for src %s, dst %s\n",
895f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                      __FUNCTION__,
896f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                      _mesa_lookup_enum_by_nr(srcFormat),
897f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                      _mesa_lookup_enum_by_nr(dstFormat));
898f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
899f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* Otherwise fail.
900f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       */
901f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return 0;
902f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
903f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
904f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
905f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
906f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
907f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
908f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
909f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Note that the region to copy has already been clipped so we know we
910f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * won't read from outside the source renderbuffer's bounds.
911f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org *
912f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Note: srcY=0=Bottom of renderbuffer (GL convention)
913f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
914f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void
915f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgst_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
916f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                   struct gl_texture_image *texImage,
917f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                   GLint destX, GLint destY, GLint destZ,
918f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                   struct gl_renderbuffer *rb,
919f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                   GLint srcX, GLint srcY, GLsizei width, GLsizei height)
920f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
921f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_texture_image *stImage = st_texture_image(texImage);
922f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   const GLenum texBaseFormat = texImage->_BaseFormat;
923f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct gl_framebuffer *fb = ctx->ReadBuffer;
924f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_renderbuffer *strb;
925f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_context *st = st_context(ctx);
926f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct pipe_context *pipe = st->pipe;
927f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct pipe_screen *screen = pipe->screen;
928f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   enum pipe_format dest_format, src_format;
929f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   GLboolean matching_base_formats;
930f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   GLuint color_writemask, zs_writemask, sample_count;
931f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct pipe_surface *dest_surface = NULL;
932f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
933f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct pipe_surface surf_tmpl;
934f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   unsigned int dst_usage;
935f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   GLint srcY0, srcY1;
936f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
937f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* make sure finalize_textures has been called?
938f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
939f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (0) st_validate_state(st);
940f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
941f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* determine if copying depth or color data */
942f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (texBaseFormat == GL_DEPTH_COMPONENT ||
943f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       texBaseFormat == GL_DEPTH_STENCIL) {
944f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      strb = st_renderbuffer(fb->Attachment[BUFFER_DEPTH].Renderbuffer);
945f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
946f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   else {
947f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
948f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      strb = st_renderbuffer(fb->_ColorReadBuffer);
949f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
950f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
951f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (!strb || !strb->surface || !stImage->pt) {
952f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      debug_printf("%s: null strb or stImage\n", __FUNCTION__);
953f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return;
954f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
955f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
956f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   sample_count = strb->surface->texture->nr_samples;
957f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* I believe this would be legal, presumably would need to do a resolve
958f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      for color, and for depth/stencil spec says to just use one of the
959f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      depth/stencil samples per pixel? Need some transfer clarifications. */
960f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   assert(sample_count < 2);
961f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
962f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   assert(strb);
963f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   assert(strb->surface);
964f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   assert(stImage->pt);
965f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
966f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   src_format = strb->surface->format;
967f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   dest_format = stImage->pt->format;
968f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
969f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /*
970f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * Determine if the src framebuffer and dest texture have the same
971f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * base format.  We need this to detect a case such as the framebuffer
972f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * being GL_RGBA but the texture being GL_RGB.  If the actual hardware
973f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * texture format stores RGBA we need to set A=1 (overriding the
974f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * framebuffer's alpha values).  We can't do that with the blit or
975f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * textured-quad paths.
976f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
977f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   matching_base_formats =
978f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      (_mesa_get_format_base_format(strb->Base.Format) ==
979f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       _mesa_get_format_base_format(texImage->TexFormat));
980f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
981f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (ctx->_ImageTransferState) {
982f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      goto fallback;
983f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
984f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
985f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
986f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* 1D arrays might be thought of as 2D images but the actual layout
987f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * might not be that way.  At some points, we convert OpenGL's 1D
988f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * array 'height' into gallium 'layers' and that prevents the blit
989f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * utility code from doing the right thing.  Simpy use the memcpy-based
990f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * fallback.
991f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       */
992f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      goto fallback;
993f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
994f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
995f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (matching_base_formats &&
996f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       src_format == dest_format &&
997f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       !do_flip) {
998f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* use surface_copy() / blit */
999f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      struct pipe_box src_box;
1000f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      unsigned dstLevel;
1001f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1002f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      u_box_2d_zslice(srcX, srcY, strb->surface->u.tex.first_layer,
1003f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                      width, height, &src_box);
1004f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1005f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* If stImage->pt is an independent image (not a pointer into a full
1006f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * mipmap) stImage->pt.last_level will be zero and we need to use that
1007f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * as the dest level.
1008f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       */
1009f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      dstLevel = MIN2(stImage->base.Level, stImage->pt->last_level);
1010f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1011f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* for resource_copy_region(), y=0=top, always */
1012f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      pipe->resource_copy_region(pipe,
1013f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 /* dest */
1014f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 stImage->pt,
1015f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 dstLevel,
1016f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 destX, destY, destZ + stImage->base.Face,
1017f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 /* src */
1018f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 strb->texture,
1019f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 strb->surface->u.tex.level,
1020f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 &src_box);
1021f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return;
1022f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
1023f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1024f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (texBaseFormat == GL_DEPTH_STENCIL) {
1025f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      goto fallback;
1026f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
1027f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1028f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (texBaseFormat == GL_DEPTH_COMPONENT) {
1029f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      color_writemask = 0;
1030f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      zs_writemask = BLIT_WRITEMASK_Z;
1031f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      dst_usage = PIPE_BIND_DEPTH_STENCIL;
1032f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
1033f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   else {
1034f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      color_writemask = compatible_src_dst_formats(ctx, &strb->Base, texImage);
1035f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      zs_writemask = 0;
1036f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      dst_usage = PIPE_BIND_RENDER_TARGET;
1037f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
1038f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1039f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if ((!color_writemask && !zs_writemask) ||
1040f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       !screen->is_format_supported(screen, src_format,
1041f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                    PIPE_TEXTURE_2D, sample_count,
1042f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                    PIPE_BIND_SAMPLER_VIEW) ||
1043f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       !screen->is_format_supported(screen, dest_format,
1044f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                    PIPE_TEXTURE_2D, 0,
1045f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                    dst_usage)) {
1046f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      goto fallback;
1047f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
1048f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1049f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (do_flip) {
1050f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      srcY1 = strb->Base.Height - srcY - height;
1051f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      srcY0 = srcY1 + height;
1052f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
1053f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   else {
1054f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      srcY0 = srcY;
1055f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      srcY1 = srcY0 + height;
1056f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
1057f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1058f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Disable conditional rendering. */
1059f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (st->render_condition) {
1060f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      pipe->render_condition(pipe, NULL, 0);
1061f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
1062f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1063f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   memset(&surf_tmpl, 0, sizeof(surf_tmpl));
1064f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   surf_tmpl.format = util_format_linear(stImage->pt->format);
1065f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   surf_tmpl.usage = dst_usage;
1066f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   surf_tmpl.u.tex.level = stImage->base.Level;
1067f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   surf_tmpl.u.tex.first_layer = stImage->base.Face + destZ;
1068f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   surf_tmpl.u.tex.last_layer = stImage->base.Face + destZ;
1069f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1070f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   dest_surface = pipe->create_surface(pipe, stImage->pt,
1071f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                       &surf_tmpl);
1072f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   util_blit_pixels(st->blit,
1073f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                    strb->texture,
1074f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                    strb->surface->u.tex.level,
1075f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                    srcX, srcY0,
1076f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                    srcX + width, srcY1,
1077f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                    strb->surface->u.tex.first_layer,
1078f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                    dest_surface,
1079f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                    destX, destY,
1080f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                    destX + width, destY + height,
1081f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                    0.0, PIPE_TEX_MIPFILTER_NEAREST,
1082f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                    color_writemask, zs_writemask);
1083f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   pipe_surface_reference(&dest_surface, NULL);
1084f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1085f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Restore conditional rendering state. */
1086f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (st->render_condition) {
1087f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      pipe->render_condition(pipe, st->render_condition,
1088f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                             st->condition_mode);
1089f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
1090f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1091f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return;
1092f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1093f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgfallback:
1094f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* software fallback */
1095f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   fallback_copy_texsubimage(ctx,
1096f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                             strb, stImage, texBaseFormat,
1097f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                             destX, destY, destZ,
1098f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                             srcX, srcY, width, height);
1099f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
1100f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1101f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1102f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
1103f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Copy image data from stImage into the texture object 'stObj' at level
1104f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * 'dstLevel'.
1105f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
1106f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic void
1107f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgcopy_image_data_to_texture(struct st_context *st,
1108f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org			   struct st_texture_object *stObj,
1109f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                           GLuint dstLevel,
1110f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org			   struct st_texture_image *stImage)
1111f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
1112f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* debug checks */
1113f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   {
1114f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      const struct gl_texture_image *dstImage =
1115f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         stObj->base.Image[stImage->base.Face][dstLevel];
1116f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      assert(dstImage);
1117f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      assert(dstImage->Width == stImage->base.Width);
1118f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      assert(dstImage->Height == stImage->base.Height);
1119f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      assert(dstImage->Depth == stImage->base.Depth);
1120f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
1121f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1122f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (stImage->pt) {
1123f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* Copy potentially with the blitter:
1124f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       */
1125f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      GLuint src_level;
1126f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (stImage->pt->last_level == 0)
1127f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         src_level = 0;
1128f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      else
1129f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         src_level = stImage->base.Level;
1130f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1131f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      assert(src_level <= stImage->pt->last_level);
1132f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      assert(u_minify(stImage->pt->width0, src_level) == stImage->base.Width);
1133f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      assert(stImage->pt->target == PIPE_TEXTURE_1D_ARRAY ||
1134f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org             u_minify(stImage->pt->height0, src_level) == stImage->base.Height);
1135f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      assert(stImage->pt->target == PIPE_TEXTURE_2D_ARRAY ||
1136f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org             u_minify(stImage->pt->depth0, src_level) == stImage->base.Depth);
1137f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1138f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      st_texture_image_copy(st->pipe,
1139f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                            stObj->pt, dstLevel,  /* dest texture, level */
1140f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                            stImage->pt, src_level, /* src texture, level */
1141f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                            stImage->base.Face);
1142f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1143f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      pipe_resource_reference(&stImage->pt, NULL);
1144f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
1145f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   else if (stImage->TexData) {
1146f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* Copy from malloc'd memory */
1147f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* XXX this should be re-examined/tested with a compressed format */
1148f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      GLuint blockSize = util_format_get_blocksize(stObj->pt->format);
1149f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      GLuint srcRowStride = stImage->base.Width * blockSize;
1150f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      GLuint srcSliceStride = stImage->base.Height * srcRowStride;
1151f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      st_texture_image_data(st,
1152f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                            stObj->pt,
1153f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                            stImage->base.Face,
1154f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                            dstLevel,
1155f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                            stImage->TexData,
1156f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                            srcRowStride,
1157f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                            srcSliceStride);
1158f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      _mesa_align_free(stImage->TexData);
1159f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      stImage->TexData = NULL;
1160f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
1161f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1162f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   pipe_resource_reference(&stImage->pt, stObj->pt);
1163f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
1164f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1165f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1166f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
1167f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Called during state validation.  When this function is finished,
1168f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * the texture object should be ready for rendering.
1169f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1170f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
1171f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgGLboolean
1172f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgst_finalize_texture(struct gl_context *ctx,
1173f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		    struct pipe_context *pipe,
1174f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org		    struct gl_texture_object *tObj)
1175f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
1176f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_context *st = st_context(ctx);
1177f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_texture_object *stObj = st_texture_object(tObj);
1178f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1179f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   GLuint face;
1180f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_texture_image *firstImage;
1181f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   enum pipe_format firstImageFormat;
1182f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   GLuint ptWidth, ptHeight, ptDepth, ptLayers;
1183f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1184f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (_mesa_is_texture_complete(tObj, &tObj->Sampler)) {
1185f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* The texture is complete and we know exactly how many mipmap levels
1186f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * are present/needed.  This is conditional because we may be called
1187f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * from the st_generate_mipmap() function when the texture object is
1188f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * incomplete.  In that case, we'll have set stObj->lastLevel before
1189f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       * we get here.
1190f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       */
1191f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (stObj->base.Sampler.MinFilter == GL_LINEAR ||
1192f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          stObj->base.Sampler.MinFilter == GL_NEAREST)
1193f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         stObj->lastLevel = stObj->base.BaseLevel;
1194f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      else
1195f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         stObj->lastLevel = stObj->base._MaxLevel;
1196f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
1197f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1198f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
1199f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   assert(firstImage);
1200f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1201f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* If both firstImage and stObj point to a texture which can contain
1202f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * all active images, favour firstImage.  Note that because of the
1203f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * completeness requirement, we know that the image dimensions
1204f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * will match.
1205f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
1206f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (firstImage->pt &&
1207f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       firstImage->pt != stObj->pt &&
1208f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org       (!stObj->pt || firstImage->pt->last_level >= stObj->pt->last_level)) {
1209f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      pipe_resource_reference(&stObj->pt, firstImage->pt);
1210f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      pipe_sampler_view_release(st->pipe, &stObj->sampler_view);
1211f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
1212f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1213f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Find gallium format for the Mesa texture */
1214f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   firstImageFormat = st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
1215f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1216f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1217f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   {
1218f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      GLuint width, height, depth;
1219f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (!guess_base_level_size(stObj->base.Target,
1220f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 firstImage->base.Width2,
1221f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 firstImage->base.Height2,
1222f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 firstImage->base.Depth2,
1223f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 firstImage->base.Level,
1224f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 &width, &height, &depth)) {
1225f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         width = stObj->width0;
1226f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         height = stObj->height0;
1227f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         depth = stObj->depth0;
1228f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
1229f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      /* convert GL dims to Gallium dims */
1230f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      st_gl_texture_dims_to_pipe_dims(stObj->base.Target, width, height, depth,
1231f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                      &ptWidth, &ptHeight, &ptDepth, &ptLayers);
1232f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
1233f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1234f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* If we already have a gallium texture, check that it matches the texture
1235f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    * object's format, target, size, num_levels, etc.
1236f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
1237f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (stObj->pt) {
1238f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
1239f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          !st_sampler_compat_formats(stObj->pt->format, firstImageFormat) ||
1240f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          stObj->pt->last_level < stObj->lastLevel ||
1241f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          stObj->pt->width0 != ptWidth ||
1242f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          stObj->pt->height0 != ptHeight ||
1243f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          stObj->pt->depth0 != ptDepth ||
1244f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          stObj->pt->array_size != ptLayers)
1245f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      {
1246f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         /* The gallium texture does not match the Mesa texture so delete the
1247f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          * gallium texture now.  We'll make a new one below.
1248f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          */
1249f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         pipe_resource_reference(&stObj->pt, NULL);
1250f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         pipe_sampler_view_release(st->pipe, &stObj->sampler_view);
1251f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         st->dirty.st |= ST_NEW_FRAMEBUFFER;
1252f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
1253f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
1254f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1255f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* May need to create a new gallium texture:
1256f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
1257f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (!stObj->pt) {
1258f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      GLuint bindings = default_bindings(st, firstImageFormat);
1259f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1260f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      stObj->pt = st_texture_create(st,
1261f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                    gl_target_to_pipe(stObj->base.Target),
1262f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                    firstImageFormat,
1263f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                    stObj->lastLevel,
1264f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                    ptWidth,
1265f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                    ptHeight,
1266f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                    ptDepth,
1267f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                    ptLayers,
1268f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                    bindings);
1269f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1270f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      if (!stObj->pt) {
1271f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
1272f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         return GL_FALSE;
1273f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
1274f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
1275f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1276f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Pull in any images not in the object's texture:
1277f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org    */
1278f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   for (face = 0; face < nr_faces; face++) {
1279f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      GLuint level;
1280f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) {
1281f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         struct st_texture_image *stImage =
1282f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            st_texture_image(stObj->base.Image[face][level]);
1283f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1284f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         /* Need to import images in main memory or held in other textures.
1285f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org          */
1286f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         if (stImage && stObj->pt != stImage->pt) {
1287f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            if (level == 0 ||
1288f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                (stImage->base.Width == u_minify(stObj->width0, level) &&
1289f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                 stImage->base.Height == u_minify(stObj->height0, level) &&
1290f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                 stImage->base.Depth == u_minify(stObj->depth0, level))) {
1291f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org               /* src image fits expected dest mipmap level size */
1292f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org               copy_image_data_to_texture(st, stObj, level, stImage);
1293f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            }
1294f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         }
1295f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
1296f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
1297f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1298f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return GL_TRUE;
1299f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
1300f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1301f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1302f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org/**
1303f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
1304f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org * for a whole mipmap stack.
1305f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org */
1306f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgstatic GLboolean
1307f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgst_AllocTextureStorage(struct gl_context *ctx,
1308f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                       struct gl_texture_object *texObj,
1309f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                       GLsizei levels, GLsizei width,
1310f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                       GLsizei height, GLsizei depth)
1311f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
1312f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   const GLuint numFaces = _mesa_num_tex_faces(texObj->Target);
1313f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_context *st = st_context(ctx);
1314f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   struct st_texture_object *stObj = st_texture_object(texObj);
1315f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   GLuint ptWidth, ptHeight, ptDepth, ptLayers, bindings;
1316f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   enum pipe_format fmt;
1317f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   GLint level;
1318f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1319f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   assert(levels > 0);
1320f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1321f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Save the level=0 dimensions */
1322f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   stObj->width0 = width;
1323f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   stObj->height0 = height;
1324f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   stObj->depth0 = depth;
1325f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   stObj->lastLevel = levels - 1;
1326f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1327f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   fmt = st_mesa_format_to_pipe_format(texObj->Image[0][0]->TexFormat);
1328f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1329f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   bindings = default_bindings(st, fmt);
1330f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1331f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   st_gl_texture_dims_to_pipe_dims(texObj->Target,
1332f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                   width, height, depth,
1333f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                   &ptWidth, &ptHeight, &ptDepth, &ptLayers);
1334f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1335f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   stObj->pt = st_texture_create(st,
1336f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 gl_target_to_pipe(texObj->Target),
1337f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 fmt,
1338f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 levels,
1339f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 ptWidth,
1340f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 ptHeight,
1341f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 ptDepth,
1342f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 ptLayers,
1343f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org                                 bindings);
1344f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   if (!stObj->pt)
1345f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      return GL_FALSE;
1346f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1347f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* Set image resource pointers */
1348f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   for (level = 0; level < levels; level++) {
1349f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      GLuint face;
1350f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      for (face = 0; face < numFaces; face++) {
1351f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         struct st_texture_image *stImage =
1352f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org            st_texture_image(texObj->Image[face][level]);
1353f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org         pipe_resource_reference(&stImage->pt, stObj->pt);
1354f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org      }
1355f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   }
1356f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1357f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   return GL_TRUE;
1358f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
1359f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1360f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1361f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1362f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgvoid
1363f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.orgst_init_texture_functions(struct dd_function_table *functions)
1364f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org{
1365f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   functions->ChooseTextureFormat = st_ChooseTextureFormat;
1366f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   functions->TexImage = st_TexImage;
1367f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   functions->TexSubImage = _mesa_store_texsubimage;
1368f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   functions->CompressedTexSubImage = _mesa_store_compressed_texsubimage;
1369f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   functions->CopyTexSubImage = st_CopyTexSubImage;
1370f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   functions->GenerateMipmap = st_generate_mipmap;
1371f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1372f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   functions->GetTexImage = st_GetTexImage;
1373f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1374f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* compressed texture functions */
1375f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   functions->CompressedTexImage = st_CompressedTexImage;
1376f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   functions->GetCompressedTexImage = _mesa_get_compressed_teximage;
1377f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1378f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   functions->NewTextureObject = st_NewTextureObject;
1379f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   functions->NewTextureImage = st_NewTextureImage;
1380f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   functions->DeleteTextureImage = st_DeleteTextureImage;
1381f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   functions->DeleteTexture = st_DeleteTextureObject;
1382f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   functions->AllocTextureImageBuffer = st_AllocTextureImageBuffer;
1383f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   functions->FreeTextureImageBuffer = st_FreeTextureImageBuffer;
1384f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   functions->MapTextureImage = st_MapTextureImage;
1385f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   functions->UnmapTextureImage = st_UnmapTextureImage;
1386f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1387f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   /* XXX Temporary until we can query pipe's texture sizes */
1388f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   functions->TestProxyTexImage = _mesa_test_proxy_teximage;
1389f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org
1390f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org   functions->AllocTextureStorage = st_AllocTextureStorage;
1391f2ba7591b1407a7ee9209f842c50696914dc2dedkbr@chromium.org}
1392