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