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