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