i965: Drop intel_bufferobj_source().
[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 src_buffer = intel_bufferobj_buffer(brw, pbo, INTEL_READ);
130 /* note: potential 64-bit ptr to 32-bit int cast */
131 src_offset = (GLuint) (unsigned long) pixels;
132
133 int src_stride =
134 _mesa_image_row_stride(unpack, image->Width, format, type);
135
136 struct intel_mipmap_tree *pbo_mt =
137 intel_miptree_create_for_bo(brw,
138 src_buffer,
139 intelImage->mt->format,
140 src_offset,
141 image->Width, image->Height,
142 src_stride, I915_TILING_NONE);
143 if (!pbo_mt)
144 return false;
145
146 if (!intel_miptree_blit(brw,
147 pbo_mt, 0, 0,
148 0, 0, false,
149 intelImage->mt, image->Level, image->Face,
150 0, 0, false,
151 image->Width, image->Height, GL_COPY)) {
152 DBG("%s: blit failed\n", __FUNCTION__);
153 intel_miptree_release(&pbo_mt);
154 return false;
155 }
156
157 intel_miptree_release(&pbo_mt);
158
159 DBG("%s: success\n", __FUNCTION__);
160 return true;
161 }
162
163 static void
164 intelTexImage(struct gl_context * ctx,
165 GLuint dims,
166 struct gl_texture_image *texImage,
167 GLenum format, GLenum type, const void *pixels,
168 const struct gl_pixelstore_attrib *unpack)
169 {
170 bool ok;
171
172 DBG("%s target %s level %d %dx%dx%d\n", __FUNCTION__,
173 _mesa_lookup_enum_by_nr(texImage->TexObject->Target),
174 texImage->Level, texImage->Width, texImage->Height, texImage->Depth);
175
176 ok = intel_texsubimage_tiled_memcpy(ctx, dims, texImage,
177 0, 0, 0, /*x,y,z offsets*/
178 texImage->Width,
179 texImage->Height,
180 texImage->Depth,
181 format, type, pixels, unpack,
182 true /*for_glTexImage*/);
183 if (ok)
184 return;
185
186 /* Attempt to use the blitter for PBO image uploads.
187 */
188 if (dims <= 2 &&
189 try_pbo_upload(ctx, texImage, unpack, format, type, pixels)) {
190 return;
191 }
192
193 DBG("%s: upload image %dx%dx%d pixels %p\n",
194 __FUNCTION__, texImage->Width, texImage->Height, texImage->Depth,
195 pixels);
196
197 _mesa_store_teximage(ctx, dims, texImage,
198 format, type, pixels, unpack);
199 }
200
201
202 /**
203 * Binds a region to a texture image, like it was uploaded by glTexImage2D().
204 *
205 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
206 */
207 static void
208 intel_set_texture_image_region(struct gl_context *ctx,
209 struct gl_texture_image *image,
210 struct intel_region *region,
211 GLenum target,
212 GLenum internalFormat,
213 gl_format format,
214 uint32_t offset,
215 GLuint width,
216 GLuint height,
217 GLuint tile_x,
218 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_layout(brw, target, image->TexFormat,
233 0, 0,
234 width, height, 1,
235 true, 0 /* num_samples */);
236 if (intel_image->mt == NULL)
237 return;
238 intel_region_reference(&intel_image->mt->region, region);
239 intel_image->mt->total_width = width;
240 intel_image->mt->total_height = height;
241 intel_image->mt->level[0].slice[0].x_offset = tile_x;
242 intel_image->mt->level[0].slice[0].y_offset = tile_y;
243
244 intel_miptree_get_tile_offsets(intel_image->mt, 0, 0, &draw_x, &draw_y);
245
246 /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
247 * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
248 * trouble resolving back to destination image due to alignment issues.
249 */
250 if (!brw->has_surface_tile_offset &&
251 (draw_x != 0 || draw_y != 0)) {
252 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
253 intel_miptree_release(&intel_image->mt);
254 return;
255 }
256
257 intel_texobj->needs_validate = true;
258
259 intel_image->mt->offset = offset;
260 assert(region->pitch % region->cpp == 0);
261 intel_image->base.RowStride = region->pitch / region->cpp;
262
263 /* Immediately validate the image to the object. */
264 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
265 }
266
267 void
268 intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
269 GLint texture_format,
270 __DRIdrawable *dPriv)
271 {
272 struct gl_framebuffer *fb = dPriv->driverPrivate;
273 struct brw_context *brw = pDRICtx->driverPrivate;
274 struct gl_context *ctx = &brw->ctx;
275 struct intel_texture_object *intelObj;
276 struct intel_renderbuffer *rb;
277 struct gl_texture_object *texObj;
278 struct gl_texture_image *texImage;
279 int level = 0, internalFormat = 0;
280 gl_format texFormat = MESA_FORMAT_NONE;
281
282 texObj = _mesa_get_current_tex_object(ctx, target);
283 intelObj = intel_texture_object(texObj);
284
285 if (!intelObj)
286 return;
287
288 if (dPriv->lastStamp != dPriv->dri2.stamp ||
289 !pDRICtx->driScreenPriv->dri2.useInvalidate)
290 intel_update_renderbuffers(pDRICtx, dPriv);
291
292 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
293 /* If the region isn't set, then intel_update_renderbuffers was unable
294 * to get the buffers for the drawable.
295 */
296 if (!rb || !rb->mt)
297 return;
298
299 if (rb->mt->cpp == 4) {
300 if (texture_format == __DRI_TEXTURE_FORMAT_RGB) {
301 internalFormat = GL_RGB;
302 texFormat = MESA_FORMAT_XRGB8888;
303 }
304 else {
305 internalFormat = GL_RGBA;
306 texFormat = MESA_FORMAT_ARGB8888;
307 }
308 } else if (rb->mt->cpp == 2) {
309 internalFormat = GL_RGB;
310 texFormat = MESA_FORMAT_RGB565;
311 }
312
313 _mesa_lock_texture(&brw->ctx, texObj);
314 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
315 intel_miptree_make_shareable(brw, rb->mt);
316 intel_set_texture_image_region(ctx, texImage, rb->mt->region, target,
317 internalFormat, texFormat, 0,
318 rb->mt->region->width,
319 rb->mt->region->height,
320 0, 0);
321 _mesa_unlock_texture(&brw->ctx, texObj);
322 }
323
324 void
325 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
326 {
327 /* The old interface didn't have the format argument, so copy our
328 * implementation's behavior at the time.
329 */
330 intelSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
331 }
332
333 static void
334 intel_image_target_texture_2d(struct gl_context *ctx, GLenum target,
335 struct gl_texture_object *texObj,
336 struct gl_texture_image *texImage,
337 GLeglImageOES image_handle)
338 {
339 struct brw_context *brw = brw_context(ctx);
340 __DRIscreen *screen;
341 __DRIimage *image;
342
343 screen = brw->intelScreen->driScrnPriv;
344 image = screen->dri2.image->lookupEGLImage(screen, image_handle,
345 screen->loaderPrivate);
346 if (image == NULL)
347 return;
348
349 /**
350 * Images originating via EGL_EXT_image_dma_buf_import can be used only
351 * with GL_OES_EGL_image_external only.
352 */
353 if (image->dma_buf_imported && target != GL_TEXTURE_EXTERNAL_OES) {
354 _mesa_error(ctx, GL_INVALID_OPERATION,
355 "glEGLImageTargetTexture2DOES(dma buffers can be used with "
356 "GL_OES_EGL_image_external only");
357 return;
358 }
359
360 if (target == GL_TEXTURE_EXTERNAL_OES && !image->dma_buf_imported) {
361 _mesa_error(ctx, GL_INVALID_OPERATION,
362 "glEGLImageTargetTexture2DOES(external target is enabled only "
363 "for images created with EGL_EXT_image_dma_buf_import");
364 return;
365 }
366
367 /* Disallow depth/stencil textures: we don't have a way to pass the
368 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
369 */
370 if (image->has_depthstencil) {
371 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
372 return;
373 }
374
375 intel_set_texture_image_region(ctx, texImage, image->region,
376 target, image->internal_format,
377 image->format, image->offset,
378 image->width, image->height,
379 image->tile_x, image->tile_y);
380 }
381
382 void
383 intelInitTextureImageFuncs(struct dd_function_table *functions)
384 {
385 functions->TexImage = intelTexImage;
386 functions->EGLImageTargetTexture2D = intel_image_target_texture_2d;
387 }