i965: Make the constant surface interface take a normal byte size.
[mesa.git] / src / mesa / drivers / dri / intel / intel_tex_image.c
1
2 #include "main/glheader.h"
3 #include "main/macros.h"
4 #include "main/mfeatures.h"
5 #include "main/mtypes.h"
6 #include "main/enums.h"
7 #include "main/bufferobj.h"
8 #include "main/context.h"
9 #include "main/formats.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 #include "intel_span.h"
26
27 #ifndef I915
28 #include "brw_context.h"
29 #endif
30
31 #define FILE_DEBUG_FLAG DEBUG_TEXTURE
32
33 /* Work back from the specified level of the image to the baselevel and create a
34 * miptree of that size.
35 */
36 struct intel_mipmap_tree *
37 intel_miptree_create_for_teximage(struct intel_context *intel,
38 struct intel_texture_object *intelObj,
39 struct intel_texture_image *intelImage,
40 bool expect_accelerated_upload)
41 {
42 GLuint firstLevel;
43 GLuint lastLevel;
44 int width, height, depth;
45 GLuint i;
46
47 intel_miptree_get_dimensions_for_image(&intelImage->base.Base,
48 &width, &height, &depth);
49
50 DBG("%s\n", __FUNCTION__);
51
52 if (intelImage->base.Base.Level > intelObj->base.BaseLevel &&
53 (width == 1 ||
54 (intelObj->base.Target != GL_TEXTURE_1D && height == 1) ||
55 (intelObj->base.Target == GL_TEXTURE_3D && depth == 1))) {
56 /* For this combination, we're at some lower mipmap level and
57 * some important dimension is 1. We can't extrapolate up to a
58 * likely base level width/height/depth for a full mipmap stack
59 * from this info, so just allocate this one level.
60 */
61 firstLevel = intelImage->base.Base.Level;
62 lastLevel = intelImage->base.Base.Level;
63 } else {
64 /* If this image disrespects BaseLevel, allocate from level zero.
65 * Usually BaseLevel == 0, so it's unlikely to happen.
66 */
67 if (intelImage->base.Base.Level < intelObj->base.BaseLevel)
68 firstLevel = 0;
69 else
70 firstLevel = intelObj->base.BaseLevel;
71
72 /* Figure out image dimensions at start level. */
73 for (i = intelImage->base.Base.Level; i > firstLevel; i--) {
74 width <<= 1;
75 if (height != 1)
76 height <<= 1;
77 if (depth != 1)
78 depth <<= 1;
79 }
80
81 /* Guess a reasonable value for lastLevel. This is probably going
82 * to be wrong fairly often and might mean that we have to look at
83 * resizable buffers, or require that buffers implement lazy
84 * pagetable arrangements.
85 */
86 if ((intelObj->base.Sampler.MinFilter == GL_NEAREST ||
87 intelObj->base.Sampler.MinFilter == GL_LINEAR) &&
88 intelImage->base.Base.Level == firstLevel &&
89 (intel->gen < 4 || firstLevel == 0)) {
90 lastLevel = firstLevel;
91 } else {
92 lastLevel = (firstLevel +
93 _mesa_get_tex_max_num_levels(intelObj->base.Target,
94 width, height, depth) - 1);
95 }
96 }
97
98 return intel_miptree_create(intel,
99 intelObj->base.Target,
100 intelImage->base.Base.TexFormat,
101 firstLevel,
102 lastLevel,
103 width,
104 height,
105 depth,
106 expect_accelerated_upload,
107 intelImage->base.Base.NumSamples,
108 false /* force_y_tiling */);
109 }
110
111 /* XXX: Do this for TexSubImage also:
112 */
113 static bool
114 try_pbo_upload(struct gl_context *ctx,
115 struct gl_texture_image *image,
116 const struct gl_pixelstore_attrib *unpack,
117 GLenum format, GLenum type, const void *pixels)
118 {
119 struct intel_texture_image *intelImage = intel_texture_image(image);
120 struct intel_context *intel = intel_context(ctx);
121 struct intel_buffer_object *pbo = intel_buffer_object(unpack->BufferObj);
122 GLuint src_offset, src_stride;
123 GLuint dst_x, dst_y;
124 drm_intel_bo *dst_buffer, *src_buffer;
125
126 if (!_mesa_is_bufferobj(unpack->BufferObj))
127 return false;
128
129 DBG("trying pbo upload\n");
130
131 if (intel->ctx._ImageTransferState ||
132 unpack->SkipPixels || unpack->SkipRows) {
133 DBG("%s: image transfer\n", __FUNCTION__);
134 return false;
135 }
136
137 if (!_mesa_format_matches_format_and_type(image->TexFormat,
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(image->TexFormat),
141 format, type);
142 return false;
143 }
144
145 ctx->Driver.AllocTextureImageBuffer(ctx, image);
146
147 if (!intelImage->mt) {
148 DBG("%s: no miptree\n", __FUNCTION__);
149 return false;
150 }
151
152 if (image->TexObject->Target == GL_TEXTURE_1D_ARRAY ||
153 image->TexObject->Target == GL_TEXTURE_2D_ARRAY) {
154 DBG("%s: no support for array textures\n", __FUNCTION__);
155 return false;
156 }
157
158 dst_buffer = intelImage->mt->region->bo;
159 src_buffer = intel_bufferobj_source(intel, pbo, 64, &src_offset);
160 /* note: potential 64-bit ptr to 32-bit int cast */
161 src_offset += (GLuint) (unsigned long) pixels;
162
163 if (unpack->RowLength > 0)
164 src_stride = unpack->RowLength;
165 else
166 src_stride = image->Width;
167 src_stride *= intelImage->mt->region->cpp;
168
169 intel_miptree_get_image_offset(intelImage->mt, intelImage->base.Base.Level,
170 intelImage->base.Base.Face,
171 &dst_x, &dst_y);
172
173 if (!intelEmitCopyBlit(intel,
174 intelImage->mt->cpp,
175 src_stride, src_buffer,
176 src_offset, false,
177 intelImage->mt->region->pitch, dst_buffer, 0,
178 intelImage->mt->region->tiling,
179 0, 0, dst_x, dst_y, image->Width, image->Height,
180 GL_COPY)) {
181 DBG("%s: blit failed\n", __FUNCTION__);
182 return false;
183 }
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_set_texture_image_region(ctx, texImage, rb->mt->region, target,
346 internalFormat, texFormat, 0,
347 rb->mt->region->width,
348 rb->mt->region->height,
349 0, 0);
350 _mesa_unlock_texture(&intel->ctx, texObj);
351 }
352
353 void
354 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
355 {
356 /* The old interface didn't have the format argument, so copy our
357 * implementation's behavior at the time.
358 */
359 intelSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
360 }
361
362 static void
363 intel_image_target_texture_2d(struct gl_context *ctx, GLenum target,
364 struct gl_texture_object *texObj,
365 struct gl_texture_image *texImage,
366 GLeglImageOES image_handle)
367 {
368 struct intel_context *intel = intel_context(ctx);
369 __DRIscreen *screen;
370 __DRIimage *image;
371
372 screen = intel->intelScreen->driScrnPriv;
373 image = screen->dri2.image->lookupEGLImage(screen, image_handle,
374 screen->loaderPrivate);
375 if (image == NULL)
376 return;
377
378 /* Disallow depth/stencil textures: we don't have a way to pass the
379 * separate stencil miptree of a GL_DEPTH_STENCIL texture through.
380 */
381 if (image->has_depthstencil) {
382 _mesa_error(ctx, GL_INVALID_OPERATION, __func__);
383 return;
384 }
385
386 intel_set_texture_image_region(ctx, texImage, image->region,
387 target, image->internal_format,
388 image->format, image->offset,
389 image->width, image->height,
390 image->tile_x, image->tile_y);
391 }
392
393 void
394 intelInitTextureImageFuncs(struct dd_function_table *functions)
395 {
396 functions->TexImage = intelTexImage;
397 functions->EGLImageTargetTexture2D = intel_image_target_texture_2d;
398 }