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