i965: Add dumping of the sampler default color.
[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 #include "intel_blit.h"
39
40 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
41
42 static void
43 intelTexSubimage(struct gl_context * ctx,
44 GLint dims,
45 GLenum target, GLint level,
46 GLint xoffset, GLint yoffset, GLint zoffset,
47 GLint width, GLint height, GLint depth,
48 GLsizei imageSize,
49 GLenum format, GLenum type, const void *pixels,
50 const struct gl_pixelstore_attrib *packing,
51 struct gl_texture_object *texObj,
52 struct gl_texture_image *texImage,
53 GLboolean compressed)
54 {
55 struct intel_context *intel = intel_context(ctx);
56 struct intel_texture_image *intelImage = intel_texture_image(texImage);
57 GLuint dstRowStride = 0;
58 drm_intel_bo *temp_bo = NULL, *dst_bo = NULL;
59 unsigned int blit_x = 0, blit_y = 0;
60
61 DBG("%s target %s level %d offset %d,%d %dx%d\n", __FUNCTION__,
62 _mesa_lookup_enum_by_nr(target),
63 level, xoffset, yoffset, width, height);
64
65 intel_flush(ctx);
66
67 if (compressed)
68 pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize,
69 pixels, packing,
70 "glCompressedTexImage");
71 else
72 pixels = _mesa_validate_pbo_teximage(ctx, dims, width, height, depth,
73 format, type, pixels, packing,
74 "glTexSubImage");
75 if (!pixels)
76 return;
77
78 intel_prepare_render(intel);
79
80 /* Map buffer if necessary. Need to lock to prevent other contexts
81 * from uploading the buffer under us.
82 */
83 if (intelImage->mt) {
84 dst_bo = intel_region_buffer(intel, intelImage->mt->region,
85 INTEL_WRITE_PART);
86
87 if (!compressed &&
88 intelImage->mt->region->tiling != I915_TILING_Y &&
89 intel->gen < 6 && target == GL_TEXTURE_2D &&
90 drm_intel_bo_busy(dst_bo))
91 {
92 unsigned long pitch;
93 uint32_t tiling_mode = I915_TILING_NONE;
94 temp_bo = drm_intel_bo_alloc_tiled(intel->bufmgr,
95 "subimage blit bo",
96 width, height,
97 intelImage->mt->cpp,
98 &tiling_mode,
99 &pitch,
100 0);
101 drm_intel_gem_bo_map_gtt(temp_bo);
102 texImage->Data = temp_bo->virtual;
103 texImage->ImageOffsets[0] = 0;
104 dstRowStride = pitch;
105
106 intel_miptree_get_image_offset(intelImage->mt, level,
107 intelImage->face, 0,
108 &blit_x, &blit_y);
109 blit_x += xoffset;
110 blit_y += yoffset;
111 xoffset = 0;
112 yoffset = 0;
113 } else {
114 texImage->Data = intel_miptree_image_map(intel,
115 intelImage->mt,
116 intelImage->face,
117 intelImage->level,
118 &dstRowStride,
119 texImage->ImageOffsets);
120 }
121 } else {
122 if (_mesa_is_format_compressed(texImage->TexFormat)) {
123 dstRowStride =
124 _mesa_format_row_stride(texImage->TexFormat, width);
125 assert(dims != 3);
126 }
127 else {
128 dstRowStride = texImage->RowStride * _mesa_get_format_bytes(texImage->TexFormat);
129 }
130 }
131
132 assert(dstRowStride);
133
134 if (compressed) {
135 if (intelImage->mt) {
136 struct intel_region *dst = intelImage->mt->region;
137
138 _mesa_copy_rect(texImage->Data, dst->cpp, dst->pitch,
139 xoffset, yoffset / 4,
140 (width + 3) & ~3, (height + 3) / 4,
141 pixels, (width + 3) & ~3, 0, 0);
142 }
143 else {
144 memcpy(texImage->Data, pixels, imageSize);
145 }
146 }
147 else {
148 if (!_mesa_texstore(ctx, dims, texImage->_BaseFormat,
149 texImage->TexFormat,
150 texImage->Data,
151 xoffset, yoffset, zoffset,
152 dstRowStride,
153 texImage->ImageOffsets,
154 width, height, depth,
155 format, type, pixels, packing)) {
156 _mesa_error(ctx, GL_OUT_OF_MEMORY, "intelTexSubImage");
157 }
158
159 if (temp_bo) {
160 GLboolean ret;
161 unsigned int dst_pitch = intelImage->mt->region->pitch *
162 intelImage->mt->cpp;
163
164 drm_intel_gem_bo_unmap_gtt(temp_bo);
165 texImage->Data = NULL;
166
167 ret = intelEmitCopyBlit(intel,
168 intelImage->mt->cpp,
169 dstRowStride / intelImage->mt->cpp,
170 temp_bo, 0, GL_FALSE,
171 dst_pitch / intelImage->mt->cpp, dst_bo, 0,
172 intelImage->mt->region->tiling,
173 0, 0, blit_x, blit_y, width, height,
174 GL_COPY);
175 assert(ret);
176 }
177 }
178
179 _mesa_unmap_teximage_pbo(ctx, packing);
180
181 if (temp_bo) {
182 drm_intel_bo_unreference(temp_bo);
183 temp_bo = NULL;
184 } else if (intelImage->mt) {
185 intel_miptree_image_unmap(intel, intelImage->mt);
186 texImage->Data = NULL;
187 }
188 }
189
190
191 static void
192 intelTexSubImage3D(struct gl_context * ctx,
193 GLenum target,
194 GLint level,
195 GLint xoffset, GLint yoffset, GLint zoffset,
196 GLsizei width, GLsizei height, GLsizei depth,
197 GLenum format, GLenum type,
198 const GLvoid * pixels,
199 const struct gl_pixelstore_attrib *packing,
200 struct gl_texture_object *texObj,
201 struct gl_texture_image *texImage)
202 {
203 intelTexSubimage(ctx, 3,
204 target, level,
205 xoffset, yoffset, zoffset,
206 width, height, depth, 0,
207 format, type, pixels, packing, texObj, texImage, GL_FALSE);
208 }
209
210
211 static void
212 intelTexSubImage2D(struct gl_context * ctx,
213 GLenum target,
214 GLint level,
215 GLint xoffset, GLint yoffset,
216 GLsizei width, GLsizei height,
217 GLenum format, GLenum type,
218 const GLvoid * pixels,
219 const struct gl_pixelstore_attrib *packing,
220 struct gl_texture_object *texObj,
221 struct gl_texture_image *texImage)
222 {
223 intelTexSubimage(ctx, 2,
224 target, level,
225 xoffset, yoffset, 0,
226 width, height, 1, 0,
227 format, type, pixels, packing, texObj, texImage, GL_FALSE);
228 }
229
230
231 static void
232 intelTexSubImage1D(struct gl_context * ctx,
233 GLenum target,
234 GLint level,
235 GLint xoffset,
236 GLsizei width,
237 GLenum format, GLenum type,
238 const GLvoid * pixels,
239 const struct gl_pixelstore_attrib *packing,
240 struct gl_texture_object *texObj,
241 struct gl_texture_image *texImage)
242 {
243 intelTexSubimage(ctx, 1,
244 target, level,
245 xoffset, 0, 0,
246 width, 1, 1, 0,
247 format, type, pixels, packing, texObj, texImage, GL_FALSE);
248 }
249
250 static void
251 intelCompressedTexSubImage2D(struct gl_context * ctx,
252 GLenum target,
253 GLint level,
254 GLint xoffset, GLint yoffset,
255 GLsizei width, GLsizei height,
256 GLenum format, GLsizei imageSize,
257 const GLvoid * pixels,
258 struct gl_texture_object *texObj,
259 struct gl_texture_image *texImage)
260 {
261 intelTexSubimage(ctx, 2,
262 target, level,
263 xoffset, yoffset, 0,
264 width, height, 1, imageSize,
265 format, 0, pixels, &ctx->Unpack, texObj, texImage, GL_TRUE);
266 }
267
268
269
270 void
271 intelInitTextureSubImageFuncs(struct dd_function_table *functions)
272 {
273 functions->TexSubImage1D = intelTexSubImage1D;
274 functions->TexSubImage2D = intelTexSubImage2D;
275 functions->TexSubImage3D = intelTexSubImage3D;
276 functions->CompressedTexSubImage2D = intelCompressedTexSubImage2D;
277 }