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