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