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