intel: Make intel_blit.c take pitches in bytes.
[mesa.git] / src / mesa / drivers / dri / intel / intel_tex_image.c
1
2 #include "main/glheader.h"
3 #include "main/macros.h"
4 #include "main/mfeatures.h"
5 #include "main/mtypes.h"
6 #include "main/enums.h"
7 #include "main/bufferobj.h"
8 #include "main/context.h"
9 #include "main/formats.h"
10 #include "main/pbo.h"
11 #include "main/renderbuffer.h"
12 #include "main/texcompress.h"
13 #include "main/texgetimage.h"
14 #include "main/texobj.h"
15 #include "main/teximage.h"
16 #include "main/texstore.h"
17
18 #include "intel_context.h"
19 #include "intel_mipmap_tree.h"
20 #include "intel_buffer_objects.h"
21 #include "intel_batchbuffer.h"
22 #include "intel_tex.h"
23 #include "intel_blit.h"
24 #include "intel_fbo.h"
25 #include "intel_span.h"
26
27 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
28
29 /* Work back from the specified level of the image to the baselevel and create a
30 * miptree of that size.
31 */
32 struct intel_mipmap_tree *
33 intel_miptree_create_for_teximage(struct intel_context *intel,
34 struct intel_texture_object *intelObj,
35 struct intel_texture_image *intelImage,
36 bool expect_accelerated_upload)
37 {
38 GLuint firstLevel;
39 GLuint lastLevel;
40 int width, height, depth;
41 GLuint i;
42
43 intel_miptree_get_dimensions_for_image(&intelImage->base.Base,
44 &width, &height, &depth);
45
46 DBG("%s\n", __FUNCTION__);
47
48 if (intelImage->base.Base.Level > intelObj->base.BaseLevel &&
49 (width == 1 ||
50 (intelObj->base.Target != GL_TEXTURE_1D && height == 1) ||
51 (intelObj->base.Target == GL_TEXTURE_3D && depth == 1))) {
52 /* For this combination, we're at some lower mipmap level and
53 * some important dimension is 1. We can't extrapolate up to a
54 * likely base level width/height/depth for a full mipmap stack
55 * from this info, so just allocate this one level.
56 */
57 firstLevel = intelImage->base.Base.Level;
58 lastLevel = intelImage->base.Base.Level;
59 } else {
60 /* If this image disrespects BaseLevel, allocate from level zero.
61 * Usually BaseLevel == 0, so it's unlikely to happen.
62 */
63 if (intelImage->base.Base.Level < intelObj->base.BaseLevel)
64 firstLevel = 0;
65 else
66 firstLevel = intelObj->base.BaseLevel;
67
68 /* Figure out image dimensions at start level. */
69 for (i = intelImage->base.Base.Level; i > firstLevel; i--) {
70 width <<= 1;
71 if (height != 1)
72 height <<= 1;
73 if (depth != 1)
74 depth <<= 1;
75 }
76
77 /* Guess a reasonable value for lastLevel. This is probably going
78 * to be wrong fairly often and might mean that we have to look at
79 * resizable buffers, or require that buffers implement lazy
80 * pagetable arrangements.
81 */
82 if ((intelObj->base.Sampler.MinFilter == GL_NEAREST ||
83 intelObj->base.Sampler.MinFilter == GL_LINEAR) &&
84 intelImage->base.Base.Level == firstLevel &&
85 (intel->gen < 4 || firstLevel == 0)) {
86 lastLevel = firstLevel;
87 } else {
88 lastLevel = (firstLevel +
89 _mesa_get_tex_max_num_levels(intelObj->base.Target,
90 width, height, depth) - 1);
91 }
92 }
93
94 return intel_miptree_create(intel,
95 intelObj->base.Target,
96 intelImage->base.Base.TexFormat,
97 firstLevel,
98 lastLevel,
99 width,
100 height,
101 depth,
102 expect_accelerated_upload,
103 0 /* num_samples */,
104 false /* force_y_tiling */);
105 }
106
107 /* There are actually quite a few combinations this will work for,
108 * more than what I've listed here.
109 */
110 static bool
111 check_pbo_format(GLenum format, GLenum type,
112 gl_format mesa_format)
113 {
114 switch (mesa_format) {
115 case MESA_FORMAT_ARGB8888:
116 return (format == GL_BGRA && (type == GL_UNSIGNED_BYTE ||
117 type == GL_UNSIGNED_INT_8_8_8_8_REV));
118 case MESA_FORMAT_RGB565:
119 return (format == GL_RGB && type == GL_UNSIGNED_SHORT_5_6_5);
120 case MESA_FORMAT_L8:
121 return (format == GL_LUMINANCE && type == GL_UNSIGNED_BYTE);
122 case MESA_FORMAT_YCBCR:
123 return (type == GL_UNSIGNED_SHORT_8_8_MESA || type == GL_UNSIGNED_BYTE);
124 default:
125 return false;
126 }
127 }
128
129
130 /* XXX: Do this for TexSubImage also:
131 */
132 static bool
133 try_pbo_upload(struct gl_context *ctx,
134 struct gl_texture_image *image,
135 const struct gl_pixelstore_attrib *unpack,
136 GLenum format, GLenum type, const void *pixels)
137 {
138 struct intel_texture_image *intelImage = intel_texture_image(image);
139 struct intel_context *intel = intel_context(ctx);
140 struct intel_buffer_object *pbo = intel_buffer_object(unpack->BufferObj);
141 GLuint src_offset, src_stride;
142 GLuint dst_x, dst_y, dst_stride;
143 drm_intel_bo *dst_buffer, *src_buffer;
144
145 if (!_mesa_is_bufferobj(unpack->BufferObj))
146 return false;
147
148 DBG("trying pbo upload\n");
149
150 if (intel->ctx._ImageTransferState ||
151 unpack->SkipPixels || unpack->SkipRows) {
152 DBG("%s: image transfer\n", __FUNCTION__);
153 return false;
154 }
155
156 if (!check_pbo_format(format, type, intelImage->base.Base.TexFormat)) {
157 DBG("%s: format mismatch (upload to %s with format 0x%x, type 0x%x)\n",
158 __FUNCTION__, _mesa_get_format_name(intelImage->base.Base.TexFormat),
159 format, type);
160 return false;
161 }
162
163 ctx->Driver.AllocTextureImageBuffer(ctx, image);
164
165 if (!intelImage->mt) {
166 DBG("%s: no miptree\n", __FUNCTION__);
167 return false;
168 }
169
170 if (image->TexObject->Target == GL_TEXTURE_1D_ARRAY ||
171 image->TexObject->Target == GL_TEXTURE_2D_ARRAY) {
172 DBG("%s: no support for array textures\n", __FUNCTION__);
173 return false;
174 }
175
176 dst_buffer = intelImage->mt->region->bo;
177 src_buffer = intel_bufferobj_source(intel, pbo, 64, &src_offset);
178 /* note: potential 64-bit ptr to 32-bit int cast */
179 src_offset += (GLuint) (unsigned long) pixels;
180
181 if (unpack->RowLength > 0)
182 src_stride = unpack->RowLength;
183 else
184 src_stride = image->Width;
185 src_stride *= intelImage->mt->region->cpp;
186
187 intel_miptree_get_image_offset(intelImage->mt, intelImage->base.Base.Level,
188 intelImage->base.Base.Face,
189 &dst_x, &dst_y);
190
191 dst_stride = intelImage->mt->region->pitch * intelImage->mt->region->cpp;
192
193 if (!intelEmitCopyBlit(intel,
194 intelImage->mt->cpp,
195 src_stride, src_buffer,
196 src_offset, false,
197 dst_stride, dst_buffer, 0,
198 intelImage->mt->region->tiling,
199 0, 0, dst_x, dst_y, image->Width, image->Height,
200 GL_COPY)) {
201 DBG("%s: blit failed\n", __FUNCTION__);
202 return false;
203 }
204
205 DBG("%s: success\n", __FUNCTION__);
206 return true;
207 }
208
209 static void
210 intelTexImage(struct gl_context * ctx,
211 GLuint dims,
212 struct gl_texture_image *texImage,
213 GLenum format, GLenum type, const void *pixels,
214 const struct gl_pixelstore_attrib *unpack)
215 {
216 bool ok;
217
218 DBG("%s target %s level %d %dx%dx%d\n", __FUNCTION__,
219 _mesa_lookup_enum_by_nr(texImage->TexObject->Target),
220 texImage->Level, texImage->Width, texImage->Height, texImage->Depth);
221
222 ok = intel_texsubimage_tiled_memcpy(ctx, dims, texImage,
223 0, 0, 0, /*x,y,z offsets*/
224 texImage->Width,
225 texImage->Height,
226 texImage->Depth,
227 format, type, pixels, unpack,
228 true /*for_glTexImage*/);
229 if (ok)
230 return;
231
232 /* Attempt to use the blitter for PBO image uploads.
233 */
234 if (dims <= 2 &&
235 try_pbo_upload(ctx, texImage, unpack, format, type, pixels)) {
236 return;
237 }
238
239 DBG("%s: upload image %dx%dx%d pixels %p\n",
240 __FUNCTION__, texImage->Width, texImage->Height, texImage->Depth,
241 pixels);
242
243 _mesa_store_teximage(ctx, dims, texImage,
244 format, type, pixels, unpack);
245 }
246
247
248 /**
249 * Binds a region to a texture image, like it was uploaded by glTexImage2D().
250 *
251 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
252 */
253 static void
254 intel_set_texture_image_region(struct gl_context *ctx,
255 struct gl_texture_image *image,
256 struct intel_region *region,
257 GLenum target,
258 GLenum internalFormat,
259 gl_format format,
260 uint32_t offset)
261 {
262 struct intel_context *intel = intel_context(ctx);
263 struct intel_texture_image *intel_image = intel_texture_image(image);
264 struct gl_texture_object *texobj = image->TexObject;
265 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
266
267 _mesa_init_teximage_fields(&intel->ctx, image,
268 region->width, region->height, 1,
269 0, internalFormat, format);
270
271 ctx->Driver.FreeTextureImageBuffer(ctx, image);
272
273 intel_image->mt = intel_miptree_create_for_region(intel, target,
274 image->TexFormat,
275 region);
276 if (intel_image->mt == NULL)
277 return;
278 intel_texobj->needs_validate = true;
279
280 intel_image->mt->offset = offset;
281 intel_image->base.RowStride = region->pitch;
282
283 /* Immediately validate the image to the object. */
284 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
285 }
286
287 void
288 intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
289 GLint texture_format,
290 __DRIdrawable *dPriv)
291 {
292 struct gl_framebuffer *fb = dPriv->driverPrivate;
293 struct intel_context *intel = pDRICtx->driverPrivate;
294 struct gl_context *ctx = &intel->ctx;
295 struct intel_texture_object *intelObj;
296 struct intel_renderbuffer *rb;
297 struct gl_texture_object *texObj;
298 struct gl_texture_image *texImage;
299 int level = 0, internalFormat = 0;
300 gl_format texFormat = MESA_FORMAT_NONE;
301
302 texObj = _mesa_get_current_tex_object(ctx, target);
303 intelObj = intel_texture_object(texObj);
304
305 if (!intelObj)
306 return;
307
308 if (dPriv->lastStamp != dPriv->dri2.stamp ||
309 !pDRICtx->driScreenPriv->dri2.useInvalidate)
310 intel_update_renderbuffers(pDRICtx, dPriv);
311
312 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
313 /* If the region isn't set, then intel_update_renderbuffers was unable
314 * to get the buffers for the drawable.
315 */
316 if (!rb || !rb->mt)
317 return;
318
319 if (rb->mt->cpp == 4) {
320 if (texture_format == __DRI_TEXTURE_FORMAT_RGB) {
321 internalFormat = GL_RGB;
322 texFormat = MESA_FORMAT_XRGB8888;
323 }
324 else {
325 internalFormat = GL_RGBA;
326 texFormat = MESA_FORMAT_ARGB8888;
327 }
328 } else if (rb->mt->cpp == 2) {
329 internalFormat = GL_RGB;
330 texFormat = MESA_FORMAT_RGB565;
331 }
332
333 _mesa_lock_texture(&intel->ctx, texObj);
334 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
335 intel_set_texture_image_region(ctx, texImage, rb->mt->region, target,
336 internalFormat, texFormat, 0);
337 _mesa_unlock_texture(&intel->ctx, texObj);
338 }
339
340 void
341 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
342 {
343 /* The old interface didn't have the format argument, so copy our
344 * implementation's behavior at the time.
345 */
346 intelSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
347 }
348
349 static void
350 intel_image_target_texture_2d(struct gl_context *ctx, GLenum target,
351 struct gl_texture_object *texObj,
352 struct gl_texture_image *texImage,
353 GLeglImageOES image_handle)
354 {
355 struct intel_context *intel = intel_context(ctx);
356 __DRIscreen *screen;
357 __DRIimage *image;
358
359 screen = intel->intelScreen->driScrnPriv;
360 image = screen->dri2.image->lookupEGLImage(screen, image_handle,
361 screen->loaderPrivate);
362 if (image == NULL)
363 return;
364
365 intel_set_texture_image_region(ctx, texImage, image->region,
366 target, image->internal_format,
367 image->format, image->offset);
368 }
369
370 void
371 intelInitTextureImageFuncs(struct dd_function_table *functions)
372 {
373 functions->TexImage = intelTexImage;
374 functions->EGLImageTargetTexture2D = intel_image_target_texture_2d;
375 }