Merge branch 'mesa_7_7_branch'
[mesa.git] / src / mesa / drivers / dri / intel / intel_tex_subimage.c
1
2 /**************************************************************************
3 *
4 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
17 * of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
22 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
23 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 **************************************************************************/
28
29 #include "main/mtypes.h"
30 #include "main/texobj.h"
31 #include "main/texstore.h"
32 #include "main/texcompress.h"
33 #include "main/enums.h"
34
35 #include "intel_context.h"
36 #include "intel_tex.h"
37 #include "intel_mipmap_tree.h"
38
39 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
40
41 static void
42 intelTexSubimage(GLcontext * ctx,
43 GLint dims,
44 GLenum target, GLint level,
45 GLint xoffset, GLint yoffset, GLint zoffset,
46 GLint width, GLint height, GLint depth,
47 GLsizei imageSize,
48 GLenum format, GLenum type, const void *pixels,
49 const struct gl_pixelstore_attrib *packing,
50 struct gl_texture_object *texObj,
51 struct gl_texture_image *texImage,
52 GLboolean compressed)
53 {
54 struct intel_context *intel = intel_context(ctx);
55 struct intel_texture_image *intelImage = intel_texture_image(texImage);
56 GLuint dstRowStride = 0;
57
58 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__,
59 _mesa_lookup_enum_by_nr(target),
60 level, xoffset, yoffset, width, height);
61
62 intelFlush(ctx);
63
64 if (compressed)
65 pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize,
66 pixels, packing,
67 "glCompressedTexImage");
68 else
69 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, depth,
70 format, type, pixels, packing,
71 "glTexSubImage");
72 if (!pixels)
73 return;
74
75 /* Map buffer if necessary. Need to lock to prevent other contexts
76 * from uploading the buffer under us.
77 */
78 if (intelImage->mt)
79 texImage->Data = intel_miptree_image_map(intel,
80 intelImage->mt,
81 intelImage->face,
82 intelImage->level,
83 &dstRowStride,
84 texImage->ImageOffsets);
85 else {
86 if (_mesa_is_format_compressed(texImage->TexFormat)) {
87 dstRowStride =
88 _mesa_format_row_stride(texImage->TexFormat, width);
89 assert(dims != 3);
90 }
91 else {
92 dstRowStride = texImage->RowStride * _mesa_get_format_bytes(texImage->TexFormat);
93 }
94 }
95
96 assert(dstRowStride);
97
98 if (compressed) {
99 if (intelImage->mt) {
100 struct intel_region *dst = intelImage->mt->region;
101
102 _mesa_copy_rect(texImage->Data, dst->cpp, dst->pitch,
103 xoffset, yoffset / 4,
104 (width + 3) & ~3, (height + 3) / 4,
105 pixels, (width + 3) & ~3, 0, 0);
106 }
107 else {
108 memcpy(texImage->Data, pixels, imageSize);
109 }
110 }
111 else {
112 if (!_mesa_texstore(ctx, dims, texImage->_BaseFormat,
113 texImage->TexFormat,
114 texImage->Data,
115 xoffset, yoffset, zoffset,
116 dstRowStride,
117 texImage->ImageOffsets,
118 width, height, depth,
119 format, type, pixels, packing)) {
120 _mesa_error(ctx, GL_OUT_OF_MEMORY, "intelTexSubImage");
121 }
122 }
123
124 _mesa_unmap_teximage_pbo(ctx, packing);
125
126 if (intelImage->mt) {
127 intel_miptree_image_unmap(intel, intelImage->mt);
128 texImage->Data = NULL;
129 }
130 }
131
132
133 static void
134 intelTexSubImage3D(GLcontext * ctx,
135 GLenum target,
136 GLint level,
137 GLint xoffset, GLint yoffset, GLint zoffset,
138 GLsizei width, GLsizei height, GLsizei depth,
139 GLenum format, GLenum type,
140 const GLvoid * pixels,
141 const struct gl_pixelstore_attrib *packing,
142 struct gl_texture_object *texObj,
143 struct gl_texture_image *texImage)
144 {
145 intelTexSubimage(ctx, 3,
146 target, level,
147 xoffset, yoffset, zoffset,
148 width, height, depth, 0,
149 format, type, pixels, packing, texObj, texImage, GL_FALSE);
150 }
151
152
153 static void
154 intelTexSubImage2D(GLcontext * ctx,
155 GLenum target,
156 GLint level,
157 GLint xoffset, GLint yoffset,
158 GLsizei width, GLsizei height,
159 GLenum format, GLenum type,
160 const GLvoid * pixels,
161 const struct gl_pixelstore_attrib *packing,
162 struct gl_texture_object *texObj,
163 struct gl_texture_image *texImage)
164 {
165 intelTexSubimage(ctx, 2,
166 target, level,
167 xoffset, yoffset, 0,
168 width, height, 1, 0,
169 format, type, pixels, packing, texObj, texImage, GL_FALSE);
170 }
171
172
173 static void
174 intelTexSubImage1D(GLcontext * ctx,
175 GLenum target,
176 GLint level,
177 GLint xoffset,
178 GLsizei width,
179 GLenum format, GLenum type,
180 const GLvoid * pixels,
181 const struct gl_pixelstore_attrib *packing,
182 struct gl_texture_object *texObj,
183 struct gl_texture_image *texImage)
184 {
185 intelTexSubimage(ctx, 1,
186 target, level,
187 xoffset, 0, 0,
188 width, 1, 1, 0,
189 format, type, pixels, packing, texObj, texImage, GL_FALSE);
190 }
191
192 static void
193 intelCompressedTexSubImage2D(GLcontext * ctx,
194 GLenum target,
195 GLint level,
196 GLint xoffset, GLint yoffset,
197 GLsizei width, GLsizei height,
198 GLenum format, GLsizei imageSize,
199 const GLvoid * pixels,
200 struct gl_texture_object *texObj,
201 struct gl_texture_image *texImage)
202 {
203 intelTexSubimage(ctx, 2,
204 target, level,
205 xoffset, yoffset, 0,
206 width, height, 1, imageSize,
207 format, 0, pixels, &ctx->Unpack, texObj, texImage, GL_TRUE);
208 }
209
210
211
212 void
213 intelInitTextureSubImageFuncs(struct dd_function_table *functions)
214 {
215 functions->TexSubImage1D = intelTexSubImage1D;
216 functions->TexSubImage2D = intelTexSubImage2D;
217 functions->TexSubImage3D = intelTexSubImage3D;
218 functions->CompressedTexSubImage2D = intelCompressedTexSubImage2D;
219 }