i965: Rename intel_emit* to reflect their new location in brw_pipe_control
[mesa.git] / src / mesa / drivers / dri / i965 / intel_tex_image.c
1
2 #include "main/glheader.h"
3 #include "main/macros.h"
4 #include "main/mtypes.h"
5 #include "main/enums.h"
6 #include "main/bufferobj.h"
7 #include "main/context.h"
8 #include "main/formats.h"
9 #include "main/image.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 "drivers/common/meta.h"
19
20 #include "intel_mipmap_tree.h"
21 #include "intel_buffer_objects.h"
22 #include "intel_batchbuffer.h"
23 #include "intel_tex.h"
24 #include "intel_blit.h"
25 #include "intel_fbo.h"
26 #include "intel_image.h"
27 #include "intel_tiled_memcpy.h"
28 #include "brw_context.h"
29
30 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
31
32 /* Work back from the specified level of the image to the baselevel and create a
33 * miptree of that size.
34 */
35 struct intel_mipmap_tree *
36 intel_miptree_create_for_teximage(struct brw_context *brw,
37 struct intel_texture_object *intelObj,
38 struct intel_texture_image *intelImage,
39 uint32_t layout_flags)
40 {
41 GLuint lastLevel;
42 int width, height, depth;
43 GLuint i;
44
45 intel_miptree_get_dimensions_for_image(&intelImage->base.Base,
46 &width, &height, &depth);
47
48 DBG("%s\n", __func__);
49
50 /* Figure out image dimensions at start level. */
51 for (i = intelImage->base.Base.Level; i > 0; i--) {
52 width <<= 1;
53 if (height != 1)
54 height <<= 1;
55 if (depth != 1)
56 depth <<= 1;
57 }
58
59 /* Guess a reasonable value for lastLevel. This is probably going
60 * to be wrong fairly often and might mean that we have to look at
61 * resizable buffers, or require that buffers implement lazy
62 * pagetable arrangements.
63 */
64 if ((intelObj->base.Sampler.MinFilter == GL_NEAREST ||
65 intelObj->base.Sampler.MinFilter == GL_LINEAR) &&
66 intelImage->base.Base.Level == 0 &&
67 !intelObj->base.GenerateMipmap) {
68 lastLevel = 0;
69 } else {
70 lastLevel = _mesa_get_tex_max_num_levels(intelObj->base.Target,
71 width, height, depth) - 1;
72 }
73
74 return intel_miptree_create(brw,
75 intelObj->base.Target,
76 intelImage->base.Base.TexFormat,
77 0,
78 lastLevel,
79 width,
80 height,
81 depth,
82 intelImage->base.Base.NumSamples,
83 INTEL_MIPTREE_TILING_ANY,
84 layout_flags);
85 }
86
87 static void
88 intelTexImage(struct gl_context * ctx,
89 GLuint dims,
90 struct gl_texture_image *texImage,
91 GLenum format, GLenum type, const void *pixels,
92 const struct gl_pixelstore_attrib *unpack)
93 {
94 struct intel_texture_image *intelImage = intel_texture_image(texImage);
95 bool ok;
96
97 bool tex_busy = intelImage->mt && drm_intel_bo_busy(intelImage->mt->bo);
98
99 DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
100 __func__, _mesa_get_format_name(texImage->TexFormat),
101 _mesa_lookup_enum_by_nr(texImage->TexObject->Target),
102 _mesa_lookup_enum_by_nr(format), _mesa_lookup_enum_by_nr(type),
103 texImage->Level, texImage->Width, texImage->Height, texImage->Depth);
104
105 /* Allocate storage for texture data. */
106 if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
107 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
108 return;
109 }
110
111 assert(intelImage->mt);
112
113 ok = _mesa_meta_pbo_TexSubImage(ctx, dims, texImage, 0, 0, 0,
114 texImage->Width, texImage->Height,
115 texImage->Depth,
116 format, type, pixels,
117 false /*allocate_storage*/,
118 tex_busy, unpack);
119 if (ok)
120 return;
121
122 ok = intel_texsubimage_tiled_memcpy(ctx, dims, texImage,
123 0, 0, 0, /*x,y,z offsets*/
124 texImage->Width,
125 texImage->Height,
126 texImage->Depth,
127 format, type, pixels, unpack,
128 false /*allocate_storage*/);
129 if (ok)
130 return;
131
132 DBG("%s: upload image %dx%dx%d pixels %p\n",
133 __func__, texImage->Width, texImage->Height, texImage->Depth,
134 pixels);
135
136 _mesa_store_teximage(ctx, dims, texImage,
137 format, type, pixels, unpack);
138 }
139
140
141 /**
142 * Binds a BO to a texture image, as if it was uploaded by glTexImage2D().
143 *
144 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
145 */
146 static void
147 intel_set_texture_image_bo(struct gl_context *ctx,
148 struct gl_texture_image *image,
149 drm_intel_bo *bo,
150 GLenum target,
151 GLenum internalFormat,
152 mesa_format format,
153 uint32_t offset,
154 GLuint width, GLuint height,
155 GLuint pitch,
156 GLuint tile_x, GLuint tile_y,
157 uint32_t layout_flags)
158 {
159 struct brw_context *brw = brw_context(ctx);
160 struct intel_texture_image *intel_image = intel_texture_image(image);
161 struct gl_texture_object *texobj = image->TexObject;
162 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
163 uint32_t draw_x, draw_y;
164
165 _mesa_init_teximage_fields(&brw->ctx, image,
166 width, height, 1,
167 0, internalFormat, format);
168
169 ctx->Driver.FreeTextureImageBuffer(ctx, image);
170
171 intel_image->mt = intel_miptree_create_for_bo(brw, bo, image->TexFormat,
172 0, width, height, 1, pitch,
173 layout_flags);
174 if (intel_image->mt == NULL)
175 return;
176 intel_image->mt->target = target;
177 intel_image->mt->total_width = width;
178 intel_image->mt->total_height = height;
179 intel_image->mt->level[0].slice[0].x_offset = tile_x;
180 intel_image->mt->level[0].slice[0].y_offset = tile_y;
181
182 intel_miptree_get_tile_offsets(intel_image->mt, 0, 0, &draw_x, &draw_y);
183
184 /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
185 * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
186 * trouble resolving back to destination image due to alignment issues.
187 */
188 if (!brw->has_surface_tile_offset &&
189 (draw_x != 0 || draw_y != 0)) {
190 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
191 intel_miptree_release(&intel_image->mt);
192 return;
193 }
194
195 intel_texobj->needs_validate = true;
196
197 intel_image->mt->offset = offset;
198 assert(pitch % intel_image->mt->cpp == 0);
199 intel_image->base.RowStride = pitch / intel_image->mt->cpp;
200
201 /* Immediately validate the image to the object. */
202 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
203 }
204
205 void
206 intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
207 GLint texture_format,
208 __DRIdrawable *dPriv)
209 {
210 struct gl_framebuffer *fb = dPriv->driverPrivate;
211 struct brw_context *brw = pDRICtx->driverPrivate;
212 struct gl_context *ctx = &brw->ctx;
213 struct intel_renderbuffer *rb;
214 struct gl_texture_object *texObj;
215 struct gl_texture_image *texImage;
216 int level = 0, internalFormat = 0;
217 mesa_format texFormat = MESA_FORMAT_NONE;
218
219 texObj = _mesa_get_current_tex_object(ctx, target);
220
221 if (!texObj)
222 return;
223
224 if (dPriv->lastStamp != dPriv->dri2.stamp ||
225 !pDRICtx->driScreenPriv->dri2.useInvalidate)
226 intel_update_renderbuffers(pDRICtx, dPriv);
227
228 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
229 /* If the miptree isn't set, then intel_update_renderbuffers was unable
230 * to get the BO for the drawable from the window system.
231 */
232 if (!rb || !rb->mt)
233 return;
234
235 if (rb->mt->cpp == 4) {
236 if (texture_format == __DRI_TEXTURE_FORMAT_RGB) {
237 internalFormat = GL_RGB;
238 texFormat = MESA_FORMAT_B8G8R8X8_UNORM;
239 }
240 else {
241 internalFormat = GL_RGBA;
242 texFormat = MESA_FORMAT_B8G8R8A8_UNORM;
243 }
244 } else if (rb->mt->cpp == 2) {
245 internalFormat = GL_RGB;
246 texFormat = MESA_FORMAT_B5G6R5_UNORM;
247 }
248
249 _mesa_lock_texture(&brw->ctx, texObj);
250 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
251 intel_miptree_make_shareable(brw, rb->mt);
252 intel_set_texture_image_bo(ctx, texImage, rb->mt->bo, target,
253 internalFormat, texFormat, 0,
254 rb->Base.Base.Width,
255 rb->Base.Base.Height,
256 rb->mt->pitch,
257 0, 0, 0);
258 _mesa_unlock_texture(&brw->ctx, texObj);
259 }
260
261 static GLboolean
262 intel_bind_renderbuffer_tex_image(struct gl_context *ctx,
263 struct gl_renderbuffer *rb,
264 struct gl_texture_image *image)
265 {
266 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
267 struct intel_texture_image *intel_image = intel_texture_image(image);
268 struct gl_texture_object *texobj = image->TexObject;
269 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
270
271 /* We can only handle RB allocated with AllocRenderbufferStorage, or
272 * window-system renderbuffers.
273 */
274 assert(!rb->TexImage);
275
276 if (!irb->mt)
277 return false;
278
279 _mesa_lock_texture(ctx, texobj);
280 _mesa_init_teximage_fields(ctx, image,
281 rb->Width, rb->Height, 1,
282 0, rb->InternalFormat, rb->Format);
283 image->NumSamples = rb->NumSamples;
284
285 intel_miptree_reference(&intel_image->mt, irb->mt);
286
287 /* Immediately validate the image to the object. */
288 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
289
290 intel_texobj->needs_validate = true;
291 _mesa_unlock_texture(ctx, texobj);
292
293 return true;
294 }
295
296 void
297 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
298 {
299 /* The old interface didn't have the format argument, so copy our
300 * implementation's behavior at the time.
301 */
302 intelSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
303 }
304
305 static void
306 intel_image_target_texture_2d(struct gl_context *ctx, GLenum target,
307 struct gl_texture_object *texObj,
308 struct gl_texture_image *texImage,
309 GLeglImageOES image_handle)
310 {
311 struct brw_context *brw = brw_context(ctx);
312 __DRIscreen *screen;
313 __DRIimage *image;
314
315 screen = brw->intelScreen->driScrnPriv;
316 image = screen->dri2.image->lookupEGLImage(screen, image_handle,
317 screen->loaderPrivate);
318 if (image == NULL)
319 return;
320
321 /* We support external textures only for EGLImages created with
322 * EGL_EXT_image_dma_buf_import. We may lift that restriction in the future.
323 */
324 if (target == GL_TEXTURE_EXTERNAL_OES && !image->dma_buf_imported) {
325 _mesa_error(ctx, GL_INVALID_OPERATION,
326 "glEGLImageTargetTexture2DOES(external target is enabled only "
327 "for images created with EGL_EXT_image_dma_buf_import");
328 return;
329 }
330
331 /* Disallow depth/stencil textures: we don't have a way to pass the
332 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
333 */
334 if (image->has_depthstencil) {
335 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
336 return;
337 }
338
339 /* Disable creation of the texture's aux buffers because the driver exposes
340 * no EGL API to manage them. That is, there is no API for resolving the aux
341 * buffer's content to the main buffer nor for invalidating the aux buffer's
342 * content.
343 */
344 intel_set_texture_image_bo(ctx, texImage, image->bo,
345 target, image->internal_format,
346 image->format, image->offset,
347 image->width, image->height,
348 image->pitch,
349 image->tile_x, image->tile_y,
350 MIPTREE_LAYOUT_DISABLE_AUX);
351 }
352
353 /**
354 * \brief A fast path for glGetTexImage.
355 *
356 * \see intel_readpixels_tiled_memcpy()
357 */
358 bool
359 intel_gettexsubimage_tiled_memcpy(struct gl_context *ctx,
360 struct gl_texture_image *texImage,
361 GLint xoffset, GLint yoffset,
362 GLsizei width, GLsizei height,
363 GLenum format, GLenum type,
364 GLvoid *pixels,
365 const struct gl_pixelstore_attrib *packing)
366 {
367 struct brw_context *brw = brw_context(ctx);
368 struct intel_texture_image *image = intel_texture_image(texImage);
369 int dst_pitch;
370
371 /* The miptree's buffer. */
372 drm_intel_bo *bo;
373
374 int error = 0;
375
376 uint32_t cpp;
377 mem_copy_fn mem_copy = NULL;
378
379 /* This fastpath is restricted to specific texture types:
380 * a 2D BGRA, RGBA, L8 or A8 texture. It could be generalized to support
381 * more types.
382 *
383 * FINISHME: The restrictions below on packing alignment and packing row
384 * length are likely unneeded now because we calculate the destination stride
385 * with _mesa_image_row_stride. However, before removing the restrictions
386 * we need tests.
387 */
388 if (!brw->has_llc ||
389 !(type == GL_UNSIGNED_BYTE || type == GL_UNSIGNED_INT_8_8_8_8_REV) ||
390 !(texImage->TexObject->Target == GL_TEXTURE_2D ||
391 texImage->TexObject->Target == GL_TEXTURE_RECTANGLE) ||
392 pixels == NULL ||
393 _mesa_is_bufferobj(packing->BufferObj) ||
394 packing->Alignment > 4 ||
395 packing->SkipPixels > 0 ||
396 packing->SkipRows > 0 ||
397 (packing->RowLength != 0 && packing->RowLength != width) ||
398 packing->SwapBytes ||
399 packing->LsbFirst ||
400 packing->Invert)
401 return false;
402
403 /* We can't handle copying from RGBX or BGRX because the tiled_memcpy
404 * function doesn't set the last channel to 1.
405 */
406 if (texImage->TexFormat == MESA_FORMAT_B8G8R8X8_UNORM ||
407 texImage->TexFormat == MESA_FORMAT_R8G8B8X8_UNORM)
408 return false;
409
410 if (!intel_get_memcpy(texImage->TexFormat, format, type, &mem_copy, &cpp,
411 INTEL_DOWNLOAD))
412 return false;
413
414 /* If this is a nontrivial texture view, let another path handle it instead. */
415 if (texImage->TexObject->MinLayer)
416 return false;
417
418 if (!image->mt ||
419 (image->mt->tiling != I915_TILING_X &&
420 image->mt->tiling != I915_TILING_Y)) {
421 /* The algorithm is written only for X- or Y-tiled memory. */
422 return false;
423 }
424
425 /* Since we are going to write raw data to the miptree, we need to resolve
426 * any pending fast color clears before we start.
427 */
428 intel_miptree_resolve_color(brw, image->mt);
429
430 bo = image->mt->bo;
431
432 if (drm_intel_bo_references(brw->batch.bo, bo)) {
433 perf_debug("Flushing before mapping a referenced bo.\n");
434 intel_batchbuffer_flush(brw);
435 }
436
437 error = brw_bo_map(brw, bo, false /* write enable */, "miptree");
438 if (error) {
439 DBG("%s: failed to map bo\n", __func__);
440 return false;
441 }
442
443 dst_pitch = _mesa_image_row_stride(packing, width, format, type);
444
445 DBG("%s: level=%d x,y=(%d,%d) (w,h)=(%d,%d) format=0x%x type=0x%x "
446 "mesa_format=0x%x tiling=%d "
447 "packing=(alignment=%d row_length=%d skip_pixels=%d skip_rows=%d)\n",
448 __func__, texImage->Level, xoffset, yoffset, width, height,
449 format, type, texImage->TexFormat, image->mt->tiling,
450 packing->Alignment, packing->RowLength, packing->SkipPixels,
451 packing->SkipRows);
452
453 int level = texImage->Level + texImage->TexObject->MinLevel;
454
455 /* Adjust x and y offset based on miplevel */
456 xoffset += image->mt->level[level].level_x;
457 yoffset += image->mt->level[level].level_y;
458
459 tiled_to_linear(
460 xoffset * cpp, (xoffset + width) * cpp,
461 yoffset, yoffset + height,
462 pixels - (ptrdiff_t) yoffset * dst_pitch - (ptrdiff_t) xoffset * cpp,
463 bo->virtual,
464 dst_pitch, image->mt->pitch,
465 brw->has_swizzling,
466 image->mt->tiling,
467 mem_copy
468 );
469
470 drm_intel_bo_unmap(bo);
471 return true;
472 }
473
474 static void
475 intel_get_tex_image(struct gl_context *ctx,
476 GLenum format, GLenum type, GLvoid *pixels,
477 struct gl_texture_image *texImage) {
478 struct brw_context *brw = brw_context(ctx);
479 bool ok;
480
481 DBG("%s\n", __func__);
482
483 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
484 if (_mesa_meta_pbo_GetTexSubImage(ctx, 3, texImage, 0, 0, 0,
485 texImage->Width, texImage->Height,
486 texImage->Depth, format, type,
487 pixels, &ctx->Pack)) {
488 /* Flush to guarantee coherency between the render cache and other
489 * caches the PBO could potentially be bound to after this point.
490 * See the related comment in intelReadPixels() for a more detailed
491 * explanation.
492 */
493 brw_emit_mi_flush(brw);
494 return;
495 }
496
497 perf_debug("%s: fallback to CPU mapping in PBO case\n", __func__);
498 }
499
500 ok = intel_gettexsubimage_tiled_memcpy(ctx, texImage, 0, 0,
501 texImage->Width, texImage->Height,
502 format, type, pixels, &ctx->Pack);
503
504 if(ok)
505 return;
506
507 _mesa_meta_GetTexImage(ctx, format, type, pixels, texImage);
508
509 DBG("%s - DONE\n", __func__);
510 }
511
512 void
513 intelInitTextureImageFuncs(struct dd_function_table *functions)
514 {
515 functions->TexImage = intelTexImage;
516 functions->EGLImageTargetTexture2D = intel_image_target_texture_2d;
517 functions->BindRenderbufferTexImage = intel_bind_renderbuffer_tex_image;
518 functions->GetTexImage = intel_get_tex_image;
519 }