i965/tex_image: Use meta for instead of the blitter PBO TexImage and GetTexImage
[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
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 bool expect_accelerated_upload)
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", __FUNCTION__);
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 expect_accelerated_upload,
83 intelImage->base.Base.NumSamples,
84 INTEL_MIPTREE_TILING_ANY,
85 false);
86 }
87
88 static void
89 intelTexImage(struct gl_context * ctx,
90 GLuint dims,
91 struct gl_texture_image *texImage,
92 GLenum format, GLenum type, const void *pixels,
93 const struct gl_pixelstore_attrib *unpack)
94 {
95 struct intel_texture_image *intelImage = intel_texture_image(texImage);
96 bool ok;
97
98 bool tex_busy = intelImage->mt && drm_intel_bo_busy(intelImage->mt->bo);
99
100 DBG("%s mesa_format %s target %s format %s type %s level %d %dx%dx%d\n",
101 __FUNCTION__, _mesa_get_format_name(texImage->TexFormat),
102 _mesa_lookup_enum_by_nr(texImage->TexObject->Target),
103 _mesa_lookup_enum_by_nr(format), _mesa_lookup_enum_by_nr(type),
104 texImage->Level, texImage->Width, texImage->Height, texImage->Depth);
105
106 ok = _mesa_meta_pbo_TexSubImage(ctx, dims, texImage, 0, 0, 0,
107 texImage->Width, texImage->Height,
108 texImage->Depth,
109 format, type, pixels,
110 true, tex_busy, unpack);
111 if (ok)
112 return;
113
114 ok = intel_texsubimage_tiled_memcpy(ctx, dims, texImage,
115 0, 0, 0, /*x,y,z offsets*/
116 texImage->Width,
117 texImage->Height,
118 texImage->Depth,
119 format, type, pixels, unpack,
120 true /*for_glTexImage*/);
121 if (ok)
122 return;
123
124 DBG("%s: upload image %dx%dx%d pixels %p\n",
125 __FUNCTION__, texImage->Width, texImage->Height, texImage->Depth,
126 pixels);
127
128 _mesa_store_teximage(ctx, dims, texImage,
129 format, type, pixels, unpack);
130 }
131
132
133 /**
134 * Binds a BO to a texture image, as if it was uploaded by glTexImage2D().
135 *
136 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
137 */
138 static void
139 intel_set_texture_image_bo(struct gl_context *ctx,
140 struct gl_texture_image *image,
141 drm_intel_bo *bo,
142 GLenum target,
143 GLenum internalFormat,
144 mesa_format format,
145 uint32_t offset,
146 GLuint width, GLuint height,
147 GLuint pitch,
148 GLuint tile_x, GLuint tile_y)
149 {
150 struct brw_context *brw = brw_context(ctx);
151 struct intel_texture_image *intel_image = intel_texture_image(image);
152 struct gl_texture_object *texobj = image->TexObject;
153 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
154 uint32_t draw_x, draw_y;
155
156 _mesa_init_teximage_fields(&brw->ctx, image,
157 width, height, 1,
158 0, internalFormat, format);
159
160 ctx->Driver.FreeTextureImageBuffer(ctx, image);
161
162 intel_image->mt = intel_miptree_create_for_bo(brw, bo, image->TexFormat,
163 0, width, height, 1, pitch);
164 if (intel_image->mt == NULL)
165 return;
166 intel_image->mt->target = target;
167 intel_image->mt->total_width = width;
168 intel_image->mt->total_height = height;
169 intel_image->mt->level[0].slice[0].x_offset = tile_x;
170 intel_image->mt->level[0].slice[0].y_offset = tile_y;
171
172 intel_miptree_get_tile_offsets(intel_image->mt, 0, 0, &draw_x, &draw_y);
173
174 /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
175 * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
176 * trouble resolving back to destination image due to alignment issues.
177 */
178 if (!brw->has_surface_tile_offset &&
179 (draw_x != 0 || draw_y != 0)) {
180 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
181 intel_miptree_release(&intel_image->mt);
182 return;
183 }
184
185 intel_texobj->needs_validate = true;
186
187 intel_image->mt->offset = offset;
188 assert(pitch % intel_image->mt->cpp == 0);
189 intel_image->base.RowStride = pitch / intel_image->mt->cpp;
190
191 /* Immediately validate the image to the object. */
192 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
193 }
194
195 void
196 intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
197 GLint texture_format,
198 __DRIdrawable *dPriv)
199 {
200 struct gl_framebuffer *fb = dPriv->driverPrivate;
201 struct brw_context *brw = pDRICtx->driverPrivate;
202 struct gl_context *ctx = &brw->ctx;
203 struct intel_renderbuffer *rb;
204 struct gl_texture_object *texObj;
205 struct gl_texture_image *texImage;
206 int level = 0, internalFormat = 0;
207 mesa_format texFormat = MESA_FORMAT_NONE;
208
209 texObj = _mesa_get_current_tex_object(ctx, target);
210
211 if (!texObj)
212 return;
213
214 if (dPriv->lastStamp != dPriv->dri2.stamp ||
215 !pDRICtx->driScreenPriv->dri2.useInvalidate)
216 intel_update_renderbuffers(pDRICtx, dPriv);
217
218 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
219 /* If the miptree isn't set, then intel_update_renderbuffers was unable
220 * to get the BO for the drawable from the window system.
221 */
222 if (!rb || !rb->mt)
223 return;
224
225 if (rb->mt->cpp == 4) {
226 if (texture_format == __DRI_TEXTURE_FORMAT_RGB) {
227 internalFormat = GL_RGB;
228 texFormat = MESA_FORMAT_B8G8R8X8_UNORM;
229 }
230 else {
231 internalFormat = GL_RGBA;
232 texFormat = MESA_FORMAT_B8G8R8A8_UNORM;
233 }
234 } else if (rb->mt->cpp == 2) {
235 internalFormat = GL_RGB;
236 texFormat = MESA_FORMAT_B5G6R5_UNORM;
237 }
238
239 _mesa_lock_texture(&brw->ctx, texObj);
240 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
241 intel_miptree_make_shareable(brw, rb->mt);
242 intel_set_texture_image_bo(ctx, texImage, rb->mt->bo, target,
243 internalFormat, texFormat, 0,
244 rb->Base.Base.Width,
245 rb->Base.Base.Height,
246 rb->mt->pitch,
247 0, 0);
248 _mesa_unlock_texture(&brw->ctx, texObj);
249 }
250
251 static GLboolean
252 intel_bind_renderbuffer_tex_image(struct gl_context *ctx,
253 struct gl_renderbuffer *rb,
254 struct gl_texture_image *image)
255 {
256 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
257 struct intel_texture_image *intel_image = intel_texture_image(image);
258 struct gl_texture_object *texobj = image->TexObject;
259 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
260
261 /* We can only handle RB allocated with AllocRenderbufferStorage, or
262 * window-system renderbuffers.
263 */
264 assert(!rb->TexImage);
265
266 if (!irb->mt)
267 return false;
268
269 _mesa_lock_texture(ctx, texobj);
270 _mesa_init_teximage_fields(ctx, image,
271 rb->Width, rb->Height, 1,
272 0, rb->InternalFormat, rb->Format);
273 image->NumSamples = rb->NumSamples;
274
275 intel_miptree_reference(&intel_image->mt, irb->mt);
276
277 /* Immediately validate the image to the object. */
278 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
279
280 intel_texobj->needs_validate = true;
281 _mesa_unlock_texture(ctx, texobj);
282
283 return true;
284 }
285
286 void
287 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
288 {
289 /* The old interface didn't have the format argument, so copy our
290 * implementation's behavior at the time.
291 */
292 intelSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
293 }
294
295 static void
296 intel_image_target_texture_2d(struct gl_context *ctx, GLenum target,
297 struct gl_texture_object *texObj,
298 struct gl_texture_image *texImage,
299 GLeglImageOES image_handle)
300 {
301 struct brw_context *brw = brw_context(ctx);
302 __DRIscreen *screen;
303 __DRIimage *image;
304
305 screen = brw->intelScreen->driScrnPriv;
306 image = screen->dri2.image->lookupEGLImage(screen, image_handle,
307 screen->loaderPrivate);
308 if (image == NULL)
309 return;
310
311 /**
312 * Images originating via EGL_EXT_image_dma_buf_import can be used only
313 * with GL_OES_EGL_image_external only.
314 */
315 if (image->dma_buf_imported && target != GL_TEXTURE_EXTERNAL_OES) {
316 _mesa_error(ctx, GL_INVALID_OPERATION,
317 "glEGLImageTargetTexture2DOES(dma buffers can be used with "
318 "GL_OES_EGL_image_external only");
319 return;
320 }
321
322 if (target == GL_TEXTURE_EXTERNAL_OES && !image->dma_buf_imported) {
323 _mesa_error(ctx, GL_INVALID_OPERATION,
324 "glEGLImageTargetTexture2DOES(external target is enabled only "
325 "for images created with EGL_EXT_image_dma_buf_import");
326 return;
327 }
328
329 /* Disallow depth/stencil textures: we don't have a way to pass the
330 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
331 */
332 if (image->has_depthstencil) {
333 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
334 return;
335 }
336
337 intel_set_texture_image_bo(ctx, texImage, image->bo,
338 target, image->internal_format,
339 image->format, image->offset,
340 image->width, image->height,
341 image->pitch,
342 image->tile_x, image->tile_y);
343 }
344
345 static void
346 intel_get_tex_image(struct gl_context *ctx,
347 GLenum format, GLenum type, GLvoid *pixels,
348 struct gl_texture_image *texImage) {
349 struct brw_context *brw = brw_context(ctx);
350
351 DBG("%s\n", __FUNCTION__);
352
353 if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
354 if (_mesa_meta_pbo_GetTexSubImage(ctx, 3, texImage, 0, 0, 0,
355 texImage->Width, texImage->Height,
356 texImage->Depth, format, type,
357 pixels, &ctx->Pack))
358 return;
359
360 perf_debug("%s: fallback to CPU mapping in PBO case\n", __FUNCTION__);
361 }
362
363 _mesa_meta_GetTexImage(ctx, format, type, pixels, texImage);
364
365 DBG("%s - DONE\n", __FUNCTION__);
366 }
367
368 void
369 intelInitTextureImageFuncs(struct dd_function_table *functions)
370 {
371 functions->TexImage = intelTexImage;
372 functions->EGLImageTargetTexture2D = intel_image_target_texture_2d;
373 functions->BindRenderbufferTexImage = intel_bind_renderbuffer_tex_image;
374 functions->GetTexImage = intel_get_tex_image;
375 }