i915: Remove the I915 macro from the formerly shared code.
[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", __FUNCTION__);
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 (intel->gen < 4 || firstLevel == 0)) {
85 lastLevel = firstLevel;
86 } else {
87 lastLevel = (firstLevel +
88 _mesa_get_tex_max_num_levels(intelObj->base.Target,
89 width, height, depth) - 1);
90 }
91 }
92
93 return intel_miptree_create(intel,
94 intelObj->base.Target,
95 intelImage->base.Base.TexFormat,
96 firstLevel,
97 lastLevel,
98 width,
99 height,
100 depth,
101 expect_accelerated_upload,
102 INTEL_MIPTREE_TILING_ANY);
103 }
104
105 /* XXX: Do this for TexSubImage also:
106 */
107 static bool
108 try_pbo_upload(struct gl_context *ctx,
109 struct gl_texture_image *image,
110 const struct gl_pixelstore_attrib *unpack,
111 GLenum format, GLenum type, const void *pixels)
112 {
113 struct intel_texture_image *intelImage = intel_texture_image(image);
114 struct intel_context *intel = intel_context(ctx);
115 struct intel_buffer_object *pbo = intel_buffer_object(unpack->BufferObj);
116 GLuint src_offset;
117 drm_intel_bo *src_buffer;
118
119 if (!_mesa_is_bufferobj(unpack->BufferObj))
120 return false;
121
122 DBG("trying pbo upload\n");
123
124 if (intel->ctx._ImageTransferState ||
125 unpack->SkipPixels || unpack->SkipRows) {
126 DBG("%s: image transfer\n", __FUNCTION__);
127 return false;
128 }
129
130 ctx->Driver.AllocTextureImageBuffer(ctx, image);
131
132 if (!intelImage->mt) {
133 DBG("%s: no miptree\n", __FUNCTION__);
134 return false;
135 }
136
137 if (!_mesa_format_matches_format_and_type(intelImage->mt->format,
138 format, type, false)) {
139 DBG("%s: format mismatch (upload to %s with format 0x%x, type 0x%x)\n",
140 __FUNCTION__, _mesa_get_format_name(intelImage->mt->format),
141 format, type);
142 return false;
143 }
144
145 if (image->TexObject->Target == GL_TEXTURE_1D_ARRAY ||
146 image->TexObject->Target == GL_TEXTURE_2D_ARRAY) {
147 DBG("%s: no support for array textures\n", __FUNCTION__);
148 return false;
149 }
150
151 src_buffer = intel_bufferobj_source(intel, pbo, 64, &src_offset);
152 /* note: potential 64-bit ptr to 32-bit int cast */
153 src_offset += (GLuint) (unsigned long) pixels;
154
155 int src_stride =
156 _mesa_image_row_stride(unpack, image->Width, format, type);
157
158 struct intel_mipmap_tree *pbo_mt =
159 intel_miptree_create_for_bo(intel,
160 src_buffer,
161 intelImage->mt->format,
162 src_offset,
163 image->Width, image->Height,
164 src_stride, I915_TILING_NONE);
165 if (!pbo_mt)
166 return false;
167
168 if (!intel_miptree_blit(intel,
169 pbo_mt, 0, 0,
170 0, 0, false,
171 intelImage->mt, image->Level, image->Face,
172 0, 0, false,
173 image->Width, image->Height, GL_COPY)) {
174 DBG("%s: blit failed\n", __FUNCTION__);
175 return false;
176 }
177
178 intel_miptree_release(&pbo_mt);
179
180 DBG("%s: success\n", __FUNCTION__);
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 bool ok;
192
193 DBG("%s target %s level %d %dx%dx%d\n", __FUNCTION__,
194 _mesa_lookup_enum_by_nr(texImage->TexObject->Target),
195 texImage->Level, texImage->Width, texImage->Height, texImage->Depth);
196
197 ok = intel_texsubimage_tiled_memcpy(ctx, dims, texImage,
198 0, 0, 0, /*x,y,z offsets*/
199 texImage->Width,
200 texImage->Height,
201 texImage->Depth,
202 format, type, pixels, unpack,
203 true /*for_glTexImage*/);
204 if (ok)
205 return;
206
207 /* Attempt to use the blitter for PBO image uploads.
208 */
209 if (dims <= 2 &&
210 try_pbo_upload(ctx, texImage, unpack, format, type, pixels)) {
211 return;
212 }
213
214 DBG("%s: upload image %dx%dx%d pixels %p\n",
215 __FUNCTION__, texImage->Width, texImage->Height, texImage->Depth,
216 pixels);
217
218 _mesa_store_teximage(ctx, dims, texImage,
219 format, type, pixels, unpack);
220 }
221
222
223 /**
224 * Binds a region to a texture image, like it was uploaded by glTexImage2D().
225 *
226 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
227 */
228 static void
229 intel_set_texture_image_region(struct gl_context *ctx,
230 struct gl_texture_image *image,
231 struct intel_region *region,
232 GLenum target,
233 GLenum internalFormat,
234 gl_format format,
235 uint32_t offset,
236 GLuint width,
237 GLuint height,
238 GLuint tile_x,
239 GLuint tile_y)
240 {
241 struct intel_context *intel = intel_context(ctx);
242 struct intel_texture_image *intel_image = intel_texture_image(image);
243 struct gl_texture_object *texobj = image->TexObject;
244 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
245 bool has_surface_tile_offset = false;
246 uint32_t draw_x, draw_y;
247
248 _mesa_init_teximage_fields(&intel->ctx, image,
249 width, height, 1,
250 0, internalFormat, format);
251
252 ctx->Driver.FreeTextureImageBuffer(ctx, image);
253
254 intel_image->mt = intel_miptree_create_layout(intel, target, image->TexFormat,
255 0, 0,
256 width, height, 1,
257 true);
258 if (intel_image->mt == NULL)
259 return;
260 intel_region_reference(&intel_image->mt->region, region);
261 intel_image->mt->total_width = width;
262 intel_image->mt->total_height = height;
263 intel_image->mt->level[0].slice[0].x_offset = tile_x;
264 intel_image->mt->level[0].slice[0].y_offset = tile_y;
265
266 intel_miptree_get_tile_offsets(intel_image->mt, 0, 0, &draw_x, &draw_y);
267
268 /* From "OES_EGL_image" error reporting. We report GL_INVALID_OPERATION
269 * for EGL images from non-tile aligned sufaces in gen4 hw and earlier which has
270 * trouble resolving back to destination image due to alignment issues.
271 */
272 if (!has_surface_tile_offset &&
273 (draw_x != 0 || draw_y != 0)) {
274 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
275 intel_miptree_release(&intel_image->mt);
276 return;
277 }
278
279 intel_texobj->needs_validate = true;
280
281 intel_image->mt->offset = offset;
282 assert(region->pitch % region->cpp == 0);
283 intel_image->base.RowStride = region->pitch / region->cpp;
284
285 /* Immediately validate the image to the object. */
286 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
287 }
288
289 void
290 intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
291 GLint texture_format,
292 __DRIdrawable *dPriv)
293 {
294 struct gl_framebuffer *fb = dPriv->driverPrivate;
295 struct intel_context *intel = pDRICtx->driverPrivate;
296 struct gl_context *ctx = &intel->ctx;
297 struct intel_texture_object *intelObj;
298 struct intel_renderbuffer *rb;
299 struct gl_texture_object *texObj;
300 struct gl_texture_image *texImage;
301 int level = 0, internalFormat = 0;
302 gl_format texFormat = MESA_FORMAT_NONE;
303
304 texObj = _mesa_get_current_tex_object(ctx, target);
305 intelObj = intel_texture_object(texObj);
306
307 if (!intelObj)
308 return;
309
310 if (dPriv->lastStamp != dPriv->dri2.stamp ||
311 !pDRICtx->driScreenPriv->dri2.useInvalidate)
312 intel_update_renderbuffers(pDRICtx, dPriv);
313
314 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
315 /* If the region isn't set, then intel_update_renderbuffers was unable
316 * to get the buffers for the drawable.
317 */
318 if (!rb || !rb->mt)
319 return;
320
321 if (rb->mt->cpp == 4) {
322 if (texture_format == __DRI_TEXTURE_FORMAT_RGB) {
323 internalFormat = GL_RGB;
324 texFormat = MESA_FORMAT_XRGB8888;
325 }
326 else {
327 internalFormat = GL_RGBA;
328 texFormat = MESA_FORMAT_ARGB8888;
329 }
330 } else if (rb->mt->cpp == 2) {
331 internalFormat = GL_RGB;
332 texFormat = MESA_FORMAT_RGB565;
333 }
334
335 _mesa_lock_texture(&intel->ctx, texObj);
336 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
337 intel_set_texture_image_region(ctx, texImage, rb->mt->region, target,
338 internalFormat, texFormat, 0,
339 rb->mt->region->width,
340 rb->mt->region->height,
341 0, 0);
342 _mesa_unlock_texture(&intel->ctx, texObj);
343 }
344
345 void
346 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
347 {
348 /* The old interface didn't have the format argument, so copy our
349 * implementation's behavior at the time.
350 */
351 intelSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
352 }
353
354 static void
355 intel_image_target_texture_2d(struct gl_context *ctx, GLenum target,
356 struct gl_texture_object *texObj,
357 struct gl_texture_image *texImage,
358 GLeglImageOES image_handle)
359 {
360 struct intel_context *intel = intel_context(ctx);
361 __DRIscreen *screen;
362 __DRIimage *image;
363
364 screen = intel->intelScreen->driScrnPriv;
365 image = screen->dri2.image->lookupEGLImage(screen, image_handle,
366 screen->loaderPrivate);
367 if (image == NULL)
368 return;
369
370 /* Disallow depth/stencil textures: we don't have a way to pass the
371 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
372 */
373 if (image->has_depthstencil) {
374 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
375 return;
376 }
377
378 intel_set_texture_image_region(ctx, texImage, image->region,
379 target, image->internal_format,
380 image->format, image->offset,
381 image->width, image->height,
382 image->tile_x, image->tile_y);
383 }
384
385 void
386 intelInitTextureImageFuncs(struct dd_function_table *functions)
387 {
388 functions->TexImage = intelTexImage;
389 functions->EGLImageTargetTexture2D = intel_image_target_texture_2d;
390 }