mesa: remove unused _mesa_init_teximage_fields() target parameter
[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 #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 intel_context *intel,
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 (intel->gen < 4 || firstLevel == 0)) {
86 lastLevel = firstLevel;
87 } else if (intelObj->base.Target == GL_TEXTURE_EXTERNAL_OES) {
88 lastLevel = firstLevel;
89 } else {
90 lastLevel = firstLevel + _mesa_logbase2(MAX2(MAX2(width, height), depth));
91 }
92 }
93
94 return intel_miptree_create(intel,
95 intelObj->base.Target,
96 intelImage->base.Base.TexFormat,
97 firstLevel,
98 lastLevel,
99 width,
100 height,
101 depth,
102 expect_accelerated_upload);
103 }
104
105 /* There are actually quite a few combinations this will work for,
106 * more than what I've listed here.
107 */
108 static bool
109 check_pbo_format(GLenum format, GLenum type,
110 gl_format mesa_format)
111 {
112 switch (mesa_format) {
113 case MESA_FORMAT_ARGB8888:
114 return (format == GL_BGRA && (type == GL_UNSIGNED_BYTE ||
115 type == GL_UNSIGNED_INT_8_8_8_8_REV));
116 case MESA_FORMAT_RGB565:
117 return (format == GL_RGB && type == GL_UNSIGNED_SHORT_5_6_5);
118 case MESA_FORMAT_L8:
119 return (format == GL_LUMINANCE && type == GL_UNSIGNED_BYTE);
120 case MESA_FORMAT_YCBCR:
121 return (type == GL_UNSIGNED_SHORT_8_8_MESA || type == GL_UNSIGNED_BYTE);
122 default:
123 return false;
124 }
125 }
126
127
128 /* XXX: Do this for TexSubImage also:
129 */
130 static bool
131 try_pbo_upload(struct gl_context *ctx,
132 struct gl_texture_image *image,
133 const struct gl_pixelstore_attrib *unpack,
134 GLenum format, GLenum type,
135 GLint width, GLint height, const void *pixels)
136 {
137 struct intel_texture_image *intelImage = intel_texture_image(image);
138 struct intel_context *intel = intel_context(ctx);
139 struct intel_buffer_object *pbo = intel_buffer_object(unpack->BufferObj);
140 GLuint src_offset, src_stride;
141 GLuint dst_x, dst_y, dst_stride;
142 drm_intel_bo *dst_buffer, *src_buffer;
143
144 if (!_mesa_is_bufferobj(unpack->BufferObj))
145 return false;
146
147 DBG("trying pbo upload\n");
148
149 if (intel->ctx._ImageTransferState ||
150 unpack->SkipPixels || unpack->SkipRows) {
151 DBG("%s: image transfer\n", __FUNCTION__);
152 return false;
153 }
154
155 if (!check_pbo_format(format, type, intelImage->base.Base.TexFormat)) {
156 DBG("%s: format mismatch (upload to %s with format 0x%x, type 0x%x)\n",
157 __FUNCTION__, _mesa_get_format_name(intelImage->base.Base.TexFormat),
158 format, type);
159 return false;
160 }
161
162 ctx->Driver.AllocTextureImageBuffer(ctx, image, image->TexFormat,
163 width, height, 1);
164
165 if (!intelImage->mt) {
166 DBG("%s: no miptree\n", __FUNCTION__);
167 return false;
168 }
169
170 dst_buffer = intelImage->mt->region->bo;
171 src_buffer = intel_bufferobj_source(intel, pbo, 64, &src_offset);
172 /* note: potential 64-bit ptr to 32-bit int cast */
173 src_offset += (GLuint) (unsigned long) pixels;
174
175 if (unpack->RowLength > 0)
176 src_stride = unpack->RowLength;
177 else
178 src_stride = width;
179
180 intel_miptree_get_image_offset(intelImage->mt, intelImage->base.Base.Level,
181 intelImage->base.Base.Face, 0,
182 &dst_x, &dst_y);
183
184 dst_stride = intelImage->mt->region->pitch;
185
186 if (!intelEmitCopyBlit(intel,
187 intelImage->mt->cpp,
188 src_stride, src_buffer,
189 src_offset, false,
190 dst_stride, dst_buffer, 0,
191 intelImage->mt->region->tiling,
192 0, 0, dst_x, dst_y, width, height,
193 GL_COPY)) {
194 DBG("%s: blit failed\n", __FUNCTION__);
195 return false;
196 }
197
198 DBG("%s: success\n", __FUNCTION__);
199 return true;
200 }
201
202 static void
203 intelTexImage(struct gl_context * ctx,
204 GLint dims,
205 struct gl_texture_image *texImage,
206 GLint internalFormat,
207 GLint width, GLint height, GLint depth,
208 GLenum format, GLenum type, const void *pixels,
209 const struct gl_pixelstore_attrib *unpack,
210 GLsizei imageSize)
211 {
212 DBG("%s target %s level %d %dx%dx%d\n", __FUNCTION__,
213 _mesa_lookup_enum_by_nr(texImage->TexObject->Target),
214 texImage->Level, width, height, depth);
215
216 /* Attempt to use the blitter for PBO image uploads.
217 */
218 if (dims <= 2 &&
219 try_pbo_upload(ctx, texImage, unpack, format, type,
220 width, height, pixels)) {
221 return;
222 }
223
224 DBG("%s: upload image %dx%dx%d pixels %p\n",
225 __FUNCTION__, width, height, depth, pixels);
226
227 _mesa_store_teximage3d(ctx, texImage, internalFormat,
228 width, height, depth, 0,
229 format, type, pixels, unpack);
230 }
231
232
233 static void
234 intelTexImage3D(struct gl_context * ctx,
235 struct gl_texture_image *texImage,
236 GLint internalFormat,
237 GLint width, GLint height, GLint depth,
238 GLint border,
239 GLenum format, GLenum type, const void *pixels,
240 const struct gl_pixelstore_attrib *unpack)
241 {
242 intelTexImage(ctx, 3, texImage,
243 internalFormat, width, height, depth,
244 format, type, pixels, unpack, 0);
245 }
246
247
248 static void
249 intelTexImage2D(struct gl_context * ctx,
250 struct gl_texture_image *texImage,
251 GLint internalFormat,
252 GLint width, GLint height, GLint border,
253 GLenum format, GLenum type, const void *pixels,
254 const struct gl_pixelstore_attrib *unpack)
255 {
256 intelTexImage(ctx, 2, texImage,
257 internalFormat, width, height, 1,
258 format, type, pixels, unpack, 0);
259 }
260
261
262 static void
263 intelTexImage1D(struct gl_context * ctx,
264 struct gl_texture_image *texImage,
265 GLint internalFormat,
266 GLint width, GLint border,
267 GLenum format, GLenum type, const void *pixels,
268 const struct gl_pixelstore_attrib *unpack)
269 {
270 intelTexImage(ctx, 1, texImage,
271 internalFormat, width, 1, 1,
272 format, type, pixels, unpack, 0);
273 }
274
275
276 /**
277 * Binds a region to a texture image, like it was uploaded by glTexImage2D().
278 *
279 * Used for GLX_EXT_texture_from_pixmap and EGL image extensions,
280 */
281 static void
282 intel_set_texture_image_region(struct gl_context *ctx,
283 struct gl_texture_image *image,
284 struct intel_region *region,
285 GLenum target,
286 GLenum internalFormat,
287 gl_format format)
288 {
289 struct intel_context *intel = intel_context(ctx);
290 struct intel_texture_image *intel_image = intel_texture_image(image);
291 struct gl_texture_object *texobj = image->TexObject;
292 struct intel_texture_object *intel_texobj = intel_texture_object(texobj);
293
294 _mesa_init_teximage_fields(&intel->ctx, image,
295 region->width, region->height, 1,
296 0, internalFormat, format);
297
298 ctx->Driver.FreeTextureImageBuffer(ctx, image);
299
300 intel_image->mt = intel_miptree_create_for_region(intel, target,
301 image->TexFormat,
302 region);
303 if (intel_image->mt == NULL)
304 return;
305
306 intel_image->base.RowStride = region->pitch;
307
308 /* Immediately validate the image to the object. */
309 intel_miptree_reference(&intel_texobj->mt, intel_image->mt);
310 }
311
312 void
313 intelSetTexBuffer2(__DRIcontext *pDRICtx, GLint target,
314 GLint texture_format,
315 __DRIdrawable *dPriv)
316 {
317 struct gl_framebuffer *fb = dPriv->driverPrivate;
318 struct intel_context *intel = pDRICtx->driverPrivate;
319 struct gl_context *ctx = &intel->ctx;
320 struct intel_texture_object *intelObj;
321 struct intel_renderbuffer *rb;
322 struct gl_texture_object *texObj;
323 struct gl_texture_image *texImage;
324 int level = 0, internalFormat;
325 gl_format texFormat;
326
327 texObj = _mesa_get_current_tex_object(ctx, target);
328 intelObj = intel_texture_object(texObj);
329
330 if (!intelObj)
331 return;
332
333 if (dPriv->lastStamp != dPriv->dri2.stamp ||
334 !pDRICtx->driScreenPriv->dri2.useInvalidate)
335 intel_update_renderbuffers(pDRICtx, dPriv);
336
337 rb = intel_get_renderbuffer(fb, BUFFER_FRONT_LEFT);
338 /* If the region isn't set, then intel_update_renderbuffers was unable
339 * to get the buffers for the drawable.
340 */
341 if (!rb || !rb->mt)
342 return;
343
344 if (texture_format == __DRI_TEXTURE_FORMAT_RGB) {
345 internalFormat = GL_RGB;
346 texFormat = MESA_FORMAT_XRGB8888;
347 }
348 else {
349 internalFormat = GL_RGBA;
350 texFormat = MESA_FORMAT_ARGB8888;
351 }
352
353 _mesa_lock_texture(&intel->ctx, texObj);
354 texImage = _mesa_get_tex_image(ctx, texObj, target, level);
355 intel_set_texture_image_region(ctx, texImage, rb->mt->region, target,
356 internalFormat, texFormat);
357 _mesa_unlock_texture(&intel->ctx, texObj);
358 }
359
360 void
361 intelSetTexBuffer(__DRIcontext *pDRICtx, GLint target, __DRIdrawable *dPriv)
362 {
363 /* The old interface didn't have the format argument, so copy our
364 * implementation's behavior at the time.
365 */
366 intelSetTexBuffer2(pDRICtx, target, __DRI_TEXTURE_FORMAT_RGBA, dPriv);
367 }
368
369 #if FEATURE_OES_EGL_image
370 static void
371 intel_image_target_texture_2d(struct gl_context *ctx, GLenum target,
372 struct gl_texture_object *texObj,
373 struct gl_texture_image *texImage,
374 GLeglImageOES image_handle)
375 {
376 struct intel_context *intel = intel_context(ctx);
377 __DRIscreen *screen;
378 __DRIimage *image;
379
380 screen = intel->intelScreen->driScrnPriv;
381 image = screen->dri2.image->lookupEGLImage(screen, image_handle,
382 screen->loaderPrivate);
383 if (image == NULL)
384 return;
385
386 intel_set_texture_image_region(ctx, texImage, image->region,
387 target, image->internal_format, image->format);
388 }
389 #endif
390
391 void
392 intelInitTextureImageFuncs(struct dd_function_table *functions)
393 {
394 functions->TexImage1D = intelTexImage1D;
395 functions->TexImage2D = intelTexImage2D;
396 functions->TexImage3D = intelTexImage3D;
397
398 #if FEATURE_OES_EGL_image
399 functions->EGLImageTargetTexture2D = intel_image_target_texture_2d;
400 #endif
401 }