i965: Delete the intel_regions.c code.
[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_regions.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->total_width = width;
238 intel_image->mt->total_height = height;
239 intel_image->mt->level[0].slice[0].x_offset = tile_x;
240 intel_image->mt->level[0].slice[0].y_offset = tile_y;
241
242 intel_miptree_get_tile_offsets(intel_image->mt, 0, 0, &draw_x, &draw_y);
243
244 /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
245 * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
246 * trouble resolving back to destination image due to alignment issues.
247 */
248 if (!brw->has_surface_tile_offset &&
249 (draw_x != 0 || draw_y != 0)) {
250 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
251 intel_miptree_release(&intel_image->mt);
252 return;
253 }
254
255 intel_texobj->needs_validate = true;
256
257 intel_image->mt->offset = offset;
258 assert(pitch % intel_image->mt->cpp == 0);
259 intel_image->base.RowStride = pitch / intel_image->mt->cpp;
260
261 /* Immediately validate the image to the object. */
262 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
263 }
264
265 void
266 intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
267 GLint texture_format,
268 __DRIdrawable *dPriv)
269 {
270 struct gl_framebuffer *fb = dPriv->driverPrivate;
271 struct brw_context *brw = pDRICtx->driverPrivate;
272 struct gl_context *ctx = &brw->ctx;
273 struct intel_renderbuffer *rb;
274 struct gl_texture_object *texObj;
275 struct gl_texture_image *texImage;
276 int level = 0, internalFormat = 0;
277 mesa_format texFormat = MESA_FORMAT_NONE;
278
279 texObj = _mesa_get_current_tex_object(ctx, target);
280
281 if (!texObj)
282 return;
283
284 if (dPriv->lastStamp != dPriv->dri2.stamp ||
285 !pDRICtx->driScreenPriv->dri2.useInvalidate)
286 intel_update_renderbuffers(pDRICtx, dPriv);
287
288 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
289 /* If the miptree isn't set, then intel_update_renderbuffers was unable
290 * to get the BO for the drawable from the window system.
291 */
292 if (!rb || !rb->mt)
293 return;
294
295 if (rb->mt->cpp == 4) {
296 if (texture_format == __DRI_TEXTURE_FORMAT_RGB) {
297 internalFormat = GL_RGB;
298 texFormat = MESA_FORMAT_B8G8R8X8_UNORM;
299 }
300 else {
301 internalFormat = GL_RGBA;
302 texFormat = MESA_FORMAT_B8G8R8A8_UNORM;
303 }
304 } else if (rb->mt->cpp == 2) {
305 internalFormat = GL_RGB;
306 texFormat = MESA_FORMAT_B5G6R5_UNORM;
307 }
308
309 _mesa_lock_texture(&brw->ctx, texObj);
310 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
311 intel_miptree_make_shareable(brw, rb->mt);
312 intel_set_texture_image_bo(ctx, texImage, rb->mt->bo, target,
313 internalFormat, texFormat, 0,
314 rb->Base.Base.Width,
315 rb->Base.Base.Height,
316 rb->mt->pitch,
317 0, 0);
318 _mesa_unlock_texture(&brw->ctx, texObj);
319 }
320
321 static GLboolean
322 intel_bind_renderbuffer_tex_image(struct gl_context *ctx,
323 struct gl_renderbuffer *rb,
324 struct gl_texture_image *image)
325 {
326 struct intel_renderbuffer *irb = intel_renderbuffer(rb);
327 struct intel_texture_image *intel_image = intel_texture_image(image);
328 struct gl_texture_object *texobj = image->TexObject;
329 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
330
331 /* We can only handle RB allocated with AllocRenderbufferStorage, or
332 * window-system renderbuffers.
333 */
334 assert(!rb->TexImage);
335
336 if (!irb->mt)
337 return false;
338
339 _mesa_lock_texture(ctx, texobj);
340 _mesa_init_teximage_fields(ctx, image,
341 rb->Width, rb->Height, 1,
342 0, rb->InternalFormat, rb->Format);
343 image->NumSamples = rb->NumSamples;
344
345 intel_miptree_reference(&intel_image->mt, irb->mt);
346
347 /* Immediately validate the image to the object. */
348 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
349
350 intel_texobj->needs_validate = true;
351 _mesa_unlock_texture(ctx, texobj);
352
353 return true;
354 }
355
356 void
357 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
358 {
359 /* The old interface didn't have the format argument, so copy our
360 * implementation's behavior at the time.
361 */
362 intelSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
363 }
364
365 static void
366 intel_image_target_texture_2d(struct gl_context *ctx, GLenum target,
367 struct gl_texture_object *texObj,
368 struct gl_texture_image *texImage,
369 GLeglImageOES image_handle)
370 {
371 struct brw_context *brw = brw_context(ctx);
372 __DRIscreen *screen;
373 __DRIimage *image;
374
375 screen = brw->intelScreen->driScrnPriv;
376 image = screen->dri2.image->lookupEGLImage(screen, image_handle,
377 screen->loaderPrivate);
378 if (image == NULL)
379 return;
380
381 /**
382 * Images originating via EGL_EXT_image_dma_buf_import can be used only
383 * with GL_OES_EGL_image_external only.
384 */
385 if (image->dma_buf_imported && target != GL_TEXTURE_EXTERNAL_OES) {
386 _mesa_error(ctx, GL_INVALID_OPERATION,
387 "glEGLImageTargetTexture2DOES(dma buffers can be used with "
388 "GL_OES_EGL_image_external only");
389 return;
390 }
391
392 if (target == GL_TEXTURE_EXTERNAL_OES && !image->dma_buf_imported) {
393 _mesa_error(ctx, GL_INVALID_OPERATION,
394 "glEGLImageTargetTexture2DOES(external target is enabled only "
395 "for images created with EGL_EXT_image_dma_buf_import");
396 return;
397 }
398
399 /* Disallow depth/stencil textures: we don't have a way to pass the
400 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
401 */
402 if (image->has_depthstencil) {
403 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
404 return;
405 }
406
407 intel_set_texture_image_bo(ctx, texImage, image->bo,
408 target, image->internal_format,
409 image->format, image->offset,
410 image->width, image->height,
411 image->pitch,
412 image->tile_x, image->tile_y);
413 }
414
415 void
416 intelInitTextureImageFuncs(struct dd_function_table *functions)
417 {
418 functions->TexImage = intelTexImage;
419 functions->EGLImageTargetTexture2D = intel_image_target_texture_2d;
420 functions->BindRenderbufferTexImage = intel_bind_renderbuffer_tex_image;
421 }