0da348c8fb85c63f6d7d86a4d2011dac2073994b
[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/pbo.h"
31 #include "main/texobj.h"
32 #include "main/texstore.h"
33 #include "main/texcompress.h"
34 #include "main/enums.h"
35
36 #include "intel_context.h"
37 #include "intel_tex.h"
38 #include "intel_mipmap_tree.h"
39 #include "intel_blit.h"
40
41 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
42
43 static bool
44 intel_blit_texsubimage(struct gl_context * ctx,
45 GLenum target, GLint level,
46 GLint xoffset, GLint yoffset,
47 GLint width, GLint height,
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 {
53 struct intel_context *intel = intel_context(ctx);
54 struct intel_texture_image *intelImage = intel_texture_image(texImage);
55 GLuint dstRowStride = 0;
56 drm_intel_bo *temp_bo = NULL, *dst_bo = NULL;
57 unsigned int blit_x = 0, blit_y = 0;
58 unsigned long pitch;
59 uint32_t tiling_mode = I915_TILING_NONE;
60
61 /* Try to do a blit upload of the subimage if the texture is
62 * currently busy.
63 */
64 if (!intelImage->mt)
65 return false;
66
67 /* The blitter can't handle Y tiling */
68 if (intelImage->mt->region->tiling == I915_TILING_Y)
69 return false;
70
71 if (target != GL_TEXTURE_2D)
72 return false;
73
74 /* On gen6, it's probably not worth swapping to the blit ring to do
75 * this because of all the overhead involved.
76 */
77 if (intel->gen >= 6)
78 return false;
79
80 dst_bo = intel_region_buffer(intel, intelImage->mt->region,
81 INTEL_WRITE_PART);
82
83 if (!drm_intel_bo_busy(dst_bo))
84 return false;
85
86 DBG("BLT subimage %s target %s level %d offset %d,%d %dx%d\n",
87 __FUNCTION__,
88 _mesa_lookup_enum_by_nr(target),
89 level, xoffset, yoffset, width, height);
90
91 pixels = _mesa_validate_pbo_teximage(ctx, 2, width, height, 1,
92 format, type, pixels, packing,
93 "glTexSubImage");
94 if (!pixels)
95 return false;
96
97 temp_bo = drm_intel_bo_alloc_tiled(intel->bufmgr,
98 "subimage blit bo",
99 width, height,
100 intelImage->mt->cpp,
101 &tiling_mode,
102 &pitch,
103 0);
104 if (temp_bo == NULL) {
105 _mesa_error(ctx, GL_OUT_OF_MEMORY, "intelTexSubImage");
106 return false;
107 }
108
109 if (drm_intel_gem_bo_map_gtt(temp_bo)) {
110 _mesa_error(ctx, GL_OUT_OF_MEMORY, "intelTexSubImage");
111 return false;
112 }
113
114 texImage->Data = temp_bo->virtual;
115 texImage->ImageOffsets[0] = 0;
116 dstRowStride = pitch;
117
118 intel_miptree_get_image_offset(intelImage->mt, level,
119 intelImage->base.Base.Face, 0,
120 &blit_x, &blit_y);
121 blit_x += xoffset;
122 blit_y += yoffset;
123 xoffset = 0;
124 yoffset = 0;
125
126 if (!_mesa_texstore(ctx, 2, texImage->_BaseFormat,
127 texImage->TexFormat,
128 texImage->Data,
129 xoffset, yoffset, 0,
130 dstRowStride,
131 texImage->ImageOffsets,
132 width, height, 1,
133 format, type, pixels, packing)) {
134 _mesa_error(ctx, GL_OUT_OF_MEMORY, "intelTexSubImage");
135 }
136
137 GLboolean ret;
138 unsigned int dst_pitch = intelImage->mt->region->pitch *
139 intelImage->mt->cpp;
140
141 drm_intel_gem_bo_unmap_gtt(temp_bo);
142 texImage->Data = NULL;
143
144 ret = intelEmitCopyBlit(intel,
145 intelImage->mt->cpp,
146 dstRowStride / intelImage->mt->cpp,
147 temp_bo, 0, GL_FALSE,
148 dst_pitch / intelImage->mt->cpp, dst_bo, 0,
149 intelImage->mt->region->tiling,
150 0, 0, blit_x, blit_y, width, height,
151 GL_COPY);
152 assert(ret);
153
154 drm_intel_bo_unreference(temp_bo);
155 _mesa_unmap_teximage_pbo(ctx, packing);
156
157 return true;
158 }
159
160 static void
161 intelTexSubImage2D(struct gl_context * ctx,
162 GLenum target,
163 GLint level,
164 GLint xoffset, GLint yoffset,
165 GLsizei width, GLsizei height,
166 GLenum format, GLenum type,
167 const GLvoid * pixels,
168 const struct gl_pixelstore_attrib *packing,
169 struct gl_texture_object *texObj,
170 struct gl_texture_image *texImage)
171 {
172 if (!intel_blit_texsubimage(ctx, target, level,
173 xoffset, yoffset,
174 width, height,
175 format, type, pixels, packing,
176 texObj, texImage)) {
177 _mesa_store_texsubimage2d(ctx, target, level,
178 xoffset, yoffset,
179 width, height,
180 format, type, pixels,
181 packing, texObj, texImage);
182 }
183 }
184
185 void
186 intelInitTextureSubImageFuncs(struct dd_function_table *functions)
187 {
188 functions->TexSubImage2D = intelTexSubImage2D;
189 }