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