i965: Set miptree target field when creating from a BO.
[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 "intel_mipmap_tree.h"
19 #include "intel_buffer_objects.h"
20 #include "intel_batchbuffer.h"
21 #include "intel_tex.h"
22 #include "intel_blit.h"
23 #include "intel_fbo.h"
24 #include "intel_image.h"
25
26 #include "brw_context.h"
27
28 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
29
30 /* Work back from the specified level of the image to the baselevel and create a
31 * miptree of that size.
32 */
33 struct intel_mipmap_tree *
34 intel_miptree_create_for_teximage(struct brw_context *brw,
35 struct intel_texture_object *intelObj,
36 struct intel_texture_image *intelImage,
37 bool expect_accelerated_upload)
38 {
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 /* Figure out image dimensions at start level. */
49 for (i = intelImage->base.Base.Level; i > 0; i--) {
50 width <<= 1;
51 if (height != 1)
52 height <<= 1;
53 if (depth != 1)
54 depth <<= 1;
55 }
56
57 /* Guess a reasonable value for lastLevel. This is probably going
58 * to be wrong fairly often and might mean that we have to look at
59 * resizable buffers, or require that buffers implement lazy
60 * pagetable arrangements.
61 */
62 if ((intelObj->base.Sampler.MinFilter == GL_NEAREST ||
63 intelObj->base.Sampler.MinFilter == GL_LINEAR) &&
64 intelImage->base.Base.Level == 0 &&
65 !intelObj->base.GenerateMipmap) {
66 lastLevel = 0;
67 } else {
68 lastLevel = _mesa_get_tex_max_num_levels(intelObj->base.Target,
69 width, height, depth) - 1;
70 }
71
72 return intel_miptree_create(brw,
73 intelObj->base.Target,
74 intelImage->base.Base.TexFormat,
75 0,
76 lastLevel,
77 width,
78 height,
79 depth,
80 expect_accelerated_upload,
81 intelImage->base.Base.NumSamples,
82 INTEL_MIPTREE_TILING_ANY);
83 }
84
85 /* XXX: Do this for TexSubImage also:
86 */
87 static bool
88 try_pbo_upload(struct gl_context *ctx,
89 struct gl_texture_image *image,
90 const struct gl_pixelstore_attrib *unpack,
91 GLenum format, GLenum type, const void *pixels)
92 {
93 struct intel_texture_image *intelImage = intel_texture_image(image);
94 struct brw_context *brw = brw_context(ctx);
95 struct intel_buffer_object *pbo = intel_buffer_object(unpack->BufferObj);
96 GLuint src_offset;
97 drm_intel_bo *src_buffer;
98
99 if (!_mesa_is_bufferobj(unpack->BufferObj))
100 return false;
101
102 DBG("trying pbo upload\n");
103
104 if (ctx->_ImageTransferState || unpack->SkipPixels || unpack->SkipRows) {
105 DBG("%s: image transfer\n", __FUNCTION__);
106 return false;
107 }
108
109 ctx->Driver.AllocTextureImageBuffer(ctx, image);
110
111 if (!intelImage->mt) {
112 DBG("%s: no miptree\n", __FUNCTION__);
113 return false;
114 }
115
116 if (!_mesa_format_matches_format_and_type(intelImage->mt->format,
117 format, type, false)) {
118 DBG("%s: format mismatch (upload to %s with format 0x%x, type 0x%x)\n",
119 __FUNCTION__, _mesa_get_format_name(intelImage->mt->format),
120 format, type);
121 return false;
122 }
123
124 if (image->TexObject->Target == GL_TEXTURE_1D_ARRAY ||
125 image->TexObject->Target == GL_TEXTURE_2D_ARRAY) {
126 DBG("%s: no support for array textures\n", __FUNCTION__);
127 return false;
128 }
129
130 int src_stride =
131 _mesa_image_row_stride(unpack, image->Width, format, type);
132
133 /* note: potential 64-bit ptr to 32-bit int cast */
134 src_offset = (GLuint) (unsigned long) pixels;
135 src_buffer = intel_bufferobj_buffer(brw, pbo,
136 src_offset, src_stride * image->Height);
137
138 struct intel_mipmap_tree *pbo_mt =
139 intel_miptree_create_for_bo(brw,
140 src_buffer,
141 intelImage->mt->format,
142 src_offset,
143 image->Width, image->Height,
144 src_stride);
145 if (!pbo_mt)
146 return false;
147
148 if (!intel_miptree_blit(brw,
149 pbo_mt, 0, 0,
150 0, 0, false,
151 intelImage->mt, image->Level, image->Face,
152 0, 0, false,
153 image->Width, image->Height, GL_COPY)) {
154 DBG("%s: blit failed\n", __FUNCTION__);
155 intel_miptree_release(&pbo_mt);
156 return false;
157 }
158
159 intel_miptree_release(&pbo_mt);
160
161 DBG("%s: success\n", __FUNCTION__);
162 return true;
163 }
164
165 static void
166 intelTexImage(struct gl_context * ctx,
167 GLuint dims,
168 struct gl_texture_image *texImage,
169 GLenum format, GLenum type, const void *pixels,
170 const struct gl_pixelstore_attrib *unpack)
171 {
172 bool ok;
173
174 DBG("%s target %s level %d %dx%dx%d\n", __FUNCTION__,
175 _mesa_lookup_enum_by_nr(texImage->TexObject->Target),
176 texImage->Level, texImage->Width, texImage->Height, texImage->Depth);
177
178 ok = intel_texsubimage_tiled_memcpy(ctx, dims, texImage,
179 0, 0, 0, /*x,y,z offsets*/
180 texImage->Width,
181 texImage->Height,
182 texImage->Depth,
183 format, type, pixels, unpack,
184 true /*for_glTexImage*/);
185 if (ok)
186 return;
187
188 /* Attempt to use the blitter for PBO image uploads.
189 */
190 if (dims <= 2 &&
191 try_pbo_upload(ctx, texImage, unpack, format, type, pixels)) {
192 return;
193 }
194
195 DBG("%s: upload image %dx%dx%d pixels %p\n",
196 __FUNCTION__, texImage->Width, texImage->Height, texImage->Depth,
197 pixels);
198
199 _mesa_store_teximage(ctx, dims, texImage,
200 format, type, pixels, unpack);
201 }
202
203
204 /**
205 * Binds a BO to a texture image, as if it was uploaded by glTexImage2D().
206 *
207 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
208 */
209 static void
210 intel_set_texture_image_bo(struct gl_context *ctx,
211 struct gl_texture_image *image,
212 drm_intel_bo *bo,
213 GLenum target,
214 GLenum internalFormat,
215 mesa_format format,
216 uint32_t offset,
217 GLuint width, GLuint height,
218 GLuint pitch,
219 GLuint tile_x, GLuint tile_y)
220 {
221 struct brw_context *brw = brw_context(ctx);
222 struct intel_texture_image *intel_image = intel_texture_image(image);
223 struct gl_texture_object *texobj = image->TexObject;
224 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
225 uint32_t draw_x, draw_y;
226
227 _mesa_init_teximage_fields(&brw->ctx, image,
228 width, height, 1,
229 0, internalFormat, format);
230
231 ctx->Driver.FreeTextureImageBuffer(ctx, image);
232
233 intel_image->mt = intel_miptree_create_for_bo(brw, bo, image->TexFormat,
234 0, width, height, pitch);
235 if (intel_image->mt == NULL)
236 return;
237 intel_image->mt->target = target;
238 intel_image->mt->total_width = width;
239 intel_image->mt->total_height = height;
240 intel_image->mt->level[0].slice[0].x_offset = tile_x;
241 intel_image->mt->level[0].slice[0].y_offset = tile_y;
242
243 intel_miptree_get_tile_offsets(intel_image->mt, 0, 0, &draw_x, &draw_y);
244
245 /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
246 * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
247 * trouble resolving back to destination image due to alignment issues.
248 */
249 if (!brw->has_surface_tile_offset &&
250 (draw_x != 0 || draw_y != 0)) {
251 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
252 intel_miptree_release(&intel_image->mt);
253 return;
254 }
255
256 intel_texobj->needs_validate = true;
257
258 intel_image->mt->offset = offset;
259 assert(pitch % intel_image->mt->cpp == 0);
260 intel_image->base.RowStride = pitch / intel_image->mt->cpp;
261
262 /* Immediately validate the image to the object. */
263 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
264 }
265
266 void
267 intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
268 GLint texture_format,
269 __DRIdrawable *dPriv)
270 {
271 struct gl_framebuffer *fb = dPriv->driverPrivate;
272 struct brw_context *brw = pDRICtx->driverPrivate;
273 struct gl_context *ctx = &brw->ctx;
274 struct intel_renderbuffer *rb;
275 struct gl_texture_object *texObj;
276 struct gl_texture_image *texImage;
277 int level = 0, internalFormat = 0;
278 mesa_format texFormat = MESA_FORMAT_NONE;
279
280 texObj = _mesa_get_current_tex_object(ctx, target);
281
282 if (!texObj)
283 return;
284
285 if (dPriv->lastStamp != dPriv->dri2.stamp ||
286 !pDRICtx->driScreenPriv->dri2.useInvalidate)
287 intel_update_renderbuffers(pDRICtx, dPriv);
288
289 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
290 /* If the miptree isn't set, then intel_update_renderbuffers was unable
291 * to get the BO for the drawable from the window system.
292 */
293 if (!rb || !rb->mt)
294 return;
295
296 if (rb->mt->cpp == 4) {
297 if (texture_format == __DRI_TEXTURE_FORMAT_RGB) {
298 internalFormat = GL_RGB;
299 texFormat = MESA_FORMAT_B8G8R8X8_UNORM;
300 }
301 else {
302 internalFormat = GL_RGBA;
303 texFormat = MESA_FORMAT_B8G8R8A8_UNORM;
304 }
305 } else if (rb->mt->cpp == 2) {
306 internalFormat = GL_RGB;
307 texFormat = MESA_FORMAT_B5G6R5_UNORM;
308 }
309
310 _mesa_lock_texture(&brw->ctx, texObj);
311 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
312 intel_miptree_make_shareable(brw, rb->mt);
313 intel_set_texture_image_bo(ctx, texImage, rb->mt->bo, target,
314 internalFormat, texFormat, 0,
315 rb->Base.Base.Width,
316 rb->Base.Base.Height,
317 rb->mt->pitch,
318 0, 0);
319 _mesa_unlock_texture(&brw->ctx, texObj);
320 }
321
322 static GLboolean
323 intel_bind_renderbuffer_tex_image(struct gl_context *ctx,
324 struct gl_renderbuffer *rb,
325 struct gl_texture_image *image)
326 {
327 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
328 struct intel_texture_image *intel_image = intel_texture_image(image);
329 struct gl_texture_object *texobj = image->TexObject;
330 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
331
332 /* We can only handle RB allocated with AllocRenderbufferStorage, or
333 * window-system renderbuffers.
334 */
335 assert(!rb->TexImage);
336
337 if (!irb->mt)
338 return false;
339
340 _mesa_lock_texture(ctx, texobj);
341 _mesa_init_teximage_fields(ctx, image,
342 rb->Width, rb->Height, 1,
343 0, rb->InternalFormat, rb->Format);
344 image->NumSamples = rb->NumSamples;
345
346 intel_miptree_reference(&intel_image->mt, irb->mt);
347
348 /* Immediately validate the image to the object. */
349 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
350
351 intel_texobj->needs_validate = true;
352 _mesa_unlock_texture(ctx, texobj);
353
354 return true;
355 }
356
357 void
358 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
359 {
360 /* The old interface didn't have the format argument, so copy our
361 * implementation's behavior at the time.
362 */
363 intelSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
364 }
365
366 static void
367 intel_image_target_texture_2d(struct gl_context *ctx, GLenum target,
368 struct gl_texture_object *texObj,
369 struct gl_texture_image *texImage,
370 GLeglImageOES image_handle)
371 {
372 struct brw_context *brw = brw_context(ctx);
373 __DRIscreen *screen;
374 __DRIimage *image;
375
376 screen = brw->intelScreen->driScrnPriv;
377 image = screen->dri2.image->lookupEGLImage(screen, image_handle,
378 screen->loaderPrivate);
379 if (image == NULL)
380 return;
381
382 /**
383 * Images originating via EGL_EXT_image_dma_buf_import can be used only
384 * with GL_OES_EGL_image_external only.
385 */
386 if (image->dma_buf_imported && target != GL_TEXTURE_EXTERNAL_OES) {
387 _mesa_error(ctx, GL_INVALID_OPERATION,
388 "glEGLImageTargetTexture2DOES(dma buffers can be used with "
389 "GL_OES_EGL_image_external only");
390 return;
391 }
392
393 if (target == GL_TEXTURE_EXTERNAL_OES && !image->dma_buf_imported) {
394 _mesa_error(ctx, GL_INVALID_OPERATION,
395 "glEGLImageTargetTexture2DOES(external target is enabled only "
396 "for images created with EGL_EXT_image_dma_buf_import");
397 return;
398 }
399
400 /* Disallow depth/stencil textures: we don't have a way to pass the
401 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
402 */
403 if (image->has_depthstencil) {
404 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
405 return;
406 }
407
408 intel_set_texture_image_bo(ctx, texImage, image->bo,
409 target, image->internal_format,
410 image->format, image->offset,
411 image->width, image->height,
412 image->pitch,
413 image->tile_x, image->tile_y);
414 }
415
416 void
417 intelInitTextureImageFuncs(struct dd_function_table *functions)
418 {
419 functions->TexImage = intelTexImage;
420 functions->EGLImageTargetTexture2D = intel_image_target_texture_2d;
421 functions->BindRenderbufferTexImage = intel_bind_renderbuffer_tex_image;
422 }