1 /**************************************************************************
2 *
3 * Copyright 2007 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "main/mfeatures.h"
29 #include "main/bufferobj.h"
30 #include "main/enums.h"
31 #include "main/fbobject.h"
32 #include "main/formats.h"
33 #include "main/image.h"
34 #include "main/imports.h"
35 #include "main/macros.h"
36 #include "main/mipmap.h"
37 #include "main/pack.h"
38 #include "main/pbo.h"
39 #include "main/pixeltransfer.h"
40 #include "main/texcompress.h"
41 #include "main/texgetimage.h"
42 #include "main/teximage.h"
43 #include "main/texobj.h"
44 #include "main/texstore.h"
45
46 #include "state_tracker/st_debug.h"
47 #include "state_tracker/st_context.h"
48 #include "state_tracker/st_cb_fbo.h"
49 #include "state_tracker/st_cb_flush.h"
50 #include "state_tracker/st_cb_texture.h"
51 #include "state_tracker/st_format.h"
52 #include "state_tracker/st_texture.h"
53 #include "state_tracker/st_gen_mipmap.h"
54 #include "state_tracker/st_atom.h"
55
56 #include "pipe/p_context.h"
57 #include "pipe/p_defines.h"
58 #include "util/u_inlines.h"
59 #include "pipe/p_shader_tokens.h"
60 #include "util/u_tile.h"
61 #include "util/u_blit.h"
62 #include "util/u_format.h"
63 #include "util/u_surface.h"
64 #include "util/u_sampler.h"
65 #include "util/u_math.h"
66 #include "util/u_box.h"
67
68 #define DBG if (0) printf
69
70
71 static enum pipe_texture_target
gl_target_to_pipe(GLenum target)72 gl_target_to_pipe(GLenum target)
73 {
74 switch (target) {
75 case GL_TEXTURE_1D:
76 return PIPE_TEXTURE_1D;
77 case GL_TEXTURE_2D:
78 case GL_TEXTURE_EXTERNAL_OES:
79 return PIPE_TEXTURE_2D;
80 case GL_TEXTURE_RECTANGLE_NV:
81 return PIPE_TEXTURE_RECT;
82 case GL_TEXTURE_3D:
83 return PIPE_TEXTURE_3D;
84 case GL_TEXTURE_CUBE_MAP_ARB:
85 return PIPE_TEXTURE_CUBE;
86 case GL_TEXTURE_1D_ARRAY_EXT:
87 return PIPE_TEXTURE_1D_ARRAY;
88 case GL_TEXTURE_2D_ARRAY_EXT:
89 return PIPE_TEXTURE_2D_ARRAY;
90 case GL_TEXTURE_BUFFER:
91 return PIPE_BUFFER;
92 default:
93 assert(0);
94 return 0;
95 }
96 }
97
98
99 /** called via ctx->Driver.NewTextureImage() */
100 static struct gl_texture_image *
st_NewTextureImage(struct gl_context * ctx)101 st_NewTextureImage(struct gl_context * ctx)
102 {
103 DBG("%s\n", __FUNCTION__);
104 (void) ctx;
105 return (struct gl_texture_image *) ST_CALLOC_STRUCT(st_texture_image);
106 }
107
108
109 /** called via ctx->Driver.DeleteTextureImage() */
110 static void
st_DeleteTextureImage(struct gl_context * ctx,struct gl_texture_image * img)111 st_DeleteTextureImage(struct gl_context * ctx, struct gl_texture_image *img)
112 {
113 /* nothing special (yet) for st_texture_image */
114 _mesa_delete_texture_image(ctx, img);
115 }
116
117
118 /** called via ctx->Driver.NewTextureObject() */
119 static struct gl_texture_object *
st_NewTextureObject(struct gl_context * ctx,GLuint name,GLenum target)120 st_NewTextureObject(struct gl_context * ctx, GLuint name, GLenum target)
121 {
122 struct st_texture_object *obj = ST_CALLOC_STRUCT(st_texture_object);
123
124 DBG("%s\n", __FUNCTION__);
125 _mesa_initialize_texture_object(&obj->base, name, target);
126
127 return &obj->base;
128 }
129
130 /** called via ctx->Driver.DeleteTextureObject() */
131 static void
st_DeleteTextureObject(struct gl_context * ctx,struct gl_texture_object * texObj)132 st_DeleteTextureObject(struct gl_context *ctx,
133 struct gl_texture_object *texObj)
134 {
135 struct st_context *st = st_context(ctx);
136 struct st_texture_object *stObj = st_texture_object(texObj);
137 if (stObj->pt)
138 pipe_resource_reference(&stObj->pt, NULL);
139 if (stObj->sampler_view) {
140 pipe_sampler_view_release(st->pipe, &stObj->sampler_view);
141 }
142 _mesa_delete_texture_object(ctx, texObj);
143 }
144
145
146 /** called via ctx->Driver.FreeTextureImageBuffer() */
147 static void
st_FreeTextureImageBuffer(struct gl_context * ctx,struct gl_texture_image * texImage)148 st_FreeTextureImageBuffer(struct gl_context *ctx,
149 struct gl_texture_image *texImage)
150 {
151 struct st_texture_image *stImage = st_texture_image(texImage);
152
153 DBG("%s\n", __FUNCTION__);
154
155 if (stImage->pt) {
156 pipe_resource_reference(&stImage->pt, NULL);
157 }
158
159 if (stImage->TexData) {
160 _mesa_align_free(stImage->TexData);
161 stImage->TexData = NULL;
162 }
163 }
164
165
166 /** called via ctx->Driver.MapTextureImage() */
167 static void
st_MapTextureImage(struct gl_context * ctx,struct gl_texture_image * texImage,GLuint slice,GLuint x,GLuint y,GLuint w,GLuint h,GLbitfield mode,GLubyte ** mapOut,GLint * rowStrideOut)168 st_MapTextureImage(struct gl_context *ctx,
169 struct gl_texture_image *texImage,
170 GLuint slice, GLuint x, GLuint y, GLuint w, GLuint h,
171 GLbitfield mode,
172 GLubyte **mapOut, GLint *rowStrideOut)
173 {
174 struct st_context *st = st_context(ctx);
175 struct st_texture_image *stImage = st_texture_image(texImage);
176 unsigned pipeMode;
177 GLubyte *map;
178
179 pipeMode = 0x0;
180 if (mode & GL_MAP_READ_BIT)
181 pipeMode |= PIPE_TRANSFER_READ;
182 if (mode & GL_MAP_WRITE_BIT)
183 pipeMode |= PIPE_TRANSFER_WRITE;
184 if (mode & GL_MAP_INVALIDATE_RANGE_BIT)
185 pipeMode |= PIPE_TRANSFER_DISCARD_RANGE;
186
187 map = st_texture_image_map(st, stImage, slice, pipeMode, x, y, w, h);
188 if (map) {
189 *mapOut = map;
190 *rowStrideOut = stImage->transfer->stride;
191 }
192 else {
193 *mapOut = NULL;
194 *rowStrideOut = 0;
195 }
196 }
197
198
199 /** called via ctx->Driver.UnmapTextureImage() */
200 static void
st_UnmapTextureImage(struct gl_context * ctx,struct gl_texture_image * texImage,GLuint slice)201 st_UnmapTextureImage(struct gl_context *ctx,
202 struct gl_texture_image *texImage,
203 GLuint slice)
204 {
205 struct st_context *st = st_context(ctx);
206 struct st_texture_image *stImage = st_texture_image(texImage);
207 st_texture_image_unmap(st, stImage);
208 }
209
210
211 /**
212 * Return default texture resource binding bitmask for the given format.
213 */
214 static GLuint
default_bindings(struct st_context * st,enum pipe_format format)215 default_bindings(struct st_context *st, enum pipe_format format)
216 {
217 struct pipe_screen *screen = st->pipe->screen;
218 const unsigned target = PIPE_TEXTURE_2D;
219 unsigned bindings;
220
221 if (util_format_is_depth_or_stencil(format))
222 bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DEPTH_STENCIL;
223 else
224 bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
225
226 if (screen->is_format_supported(screen, format, target, 0, bindings))
227 return bindings;
228 else {
229 /* Try non-sRGB. */
230 format = util_format_linear(format);
231
232 if (screen->is_format_supported(screen, format, target, 0, bindings))
233 return bindings;
234 else
235 return PIPE_BIND_SAMPLER_VIEW;
236 }
237 }
238
239
240 /**
241 * Given the size of a mipmap image, try to compute the size of the level=0
242 * mipmap image.
243 *
244 * Note that this isn't always accurate for odd-sized, non-POW textures.
245 * For example, if level=1 and width=40 then the level=0 width may be 80 or 81.
246 *
247 * \return GL_TRUE for success, GL_FALSE for failure
248 */
249 static GLboolean
guess_base_level_size(GLenum target,GLuint width,GLuint height,GLuint depth,GLuint level,GLuint * width0,GLuint * height0,GLuint * depth0)250 guess_base_level_size(GLenum target,
251 GLuint width, GLuint height, GLuint depth, GLuint level,
252 GLuint *width0, GLuint *height0, GLuint *depth0)
253 {
254 assert(width >= 1);
255 assert(height >= 1);
256 assert(depth >= 1);
257
258 if (level > 0) {
259 /* Guess the size of the base level.
260 * Depending on the image's size, we can't always make a guess here.
261 */
262 switch (target) {
263 case GL_TEXTURE_1D:
264 case GL_TEXTURE_1D_ARRAY:
265 width <<= level;
266 break;
267
268 case GL_TEXTURE_2D:
269 case GL_TEXTURE_2D_ARRAY:
270 /* We can't make a good guess here, because the base level dimensions
271 * can be non-square.
272 */
273 if (width == 1 || height == 1) {
274 return GL_FALSE;
275 }
276 width <<= level;
277 height <<= level;
278 break;
279
280 case GL_TEXTURE_CUBE_MAP:
281 case GL_TEXTURE_CUBE_MAP_ARRAY:
282 width <<= level;
283 height <<= level;
284 break;
285
286 case GL_TEXTURE_3D:
287 /* We can't make a good guess here, because the base level dimensions
288 * can be non-cube.
289 */
290 if (width == 1 || height == 1 || depth == 1) {
291 return GL_FALSE;
292 }
293 width <<= level;
294 height <<= level;
295 depth <<= level;
296 break;
297
298 case GL_TEXTURE_RECTANGLE:
299 break;
300
301 default:
302 assert(0);
303 }
304 }
305
306 *width0 = width;
307 *height0 = height;
308 *depth0 = depth;
309
310 return GL_TRUE;
311 }
312
313
314 /**
315 * Try to allocate a pipe_resource object for the given st_texture_object.
316 *
317 * We use the given st_texture_image as a clue to determine the size of the
318 * mipmap image at level=0.
319 *
320 * \return GL_TRUE for success, GL_FALSE if out of memory.
321 */
322 static GLboolean
guess_and_alloc_texture(struct st_context * st,struct st_texture_object * stObj,const struct st_texture_image * stImage)323 guess_and_alloc_texture(struct st_context *st,
324 struct st_texture_object *stObj,
325 const struct st_texture_image *stImage)
326 {
327 GLuint lastLevel, width, height, depth;
328 GLuint bindings;
329 GLuint ptWidth, ptHeight, ptDepth, ptLayers;
330 enum pipe_format fmt;
331
332 DBG("%s\n", __FUNCTION__);
333
334 assert(!stObj->pt);
335
336 if (!guess_base_level_size(stObj->base.Target,
337 stImage->base.Width2,
338 stImage->base.Height2,
339 stImage->base.Depth2,
340 stImage->base.Level,
341 &width, &height, &depth)) {
342 /* we can't determine the image size at level=0 */
343 stObj->width0 = stObj->height0 = stObj->depth0 = 0;
344 /* this is not an out of memory error */
345 return GL_TRUE;
346 }
347
348 /* At this point, (width x height x depth) is the expected size of
349 * the level=0 mipmap image.
350 */
351
352 /* Guess a reasonable value for lastLevel. With OpenGL we have no
353 * idea how many mipmap levels will be in a texture until we start
354 * to render with it. Make an educated guess here but be prepared
355 * to re-allocating a texture buffer with space for more (or fewer)
356 * mipmap levels later.
357 */
358 if ((stObj->base.Sampler.MinFilter == GL_NEAREST ||
359 stObj->base.Sampler.MinFilter == GL_LINEAR ||
360 stImage->base._BaseFormat == GL_DEPTH_COMPONENT ||
361 stImage->base._BaseFormat == GL_DEPTH_STENCIL_EXT) &&
362 !stObj->base.GenerateMipmap &&
363 stImage->base.Level == 0) {
364 /* only alloc space for a single mipmap level */
365 lastLevel = 0;
366 }
367 else {
368 /* alloc space for a full mipmap */
369 lastLevel = _mesa_get_tex_max_num_levels(stObj->base.Target,
370 width, height, depth) - 1;
371 }
372
373 /* Save the level=0 dimensions */
374 stObj->width0 = width;
375 stObj->height0 = height;
376 stObj->depth0 = depth;
377
378 fmt = st_mesa_format_to_pipe_format(stImage->base.TexFormat);
379
380 bindings = default_bindings(st, fmt);
381
382 st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
383 width, height, depth,
384 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
385
386 stObj->pt = st_texture_create(st,
387 gl_target_to_pipe(stObj->base.Target),
388 fmt,
389 lastLevel,
390 ptWidth,
391 ptHeight,
392 ptDepth,
393 ptLayers,
394 bindings);
395
396 stObj->lastLevel = lastLevel;
397
398 DBG("%s returning %d\n", __FUNCTION__, (stObj->pt != NULL));
399
400 return stObj->pt != NULL;
401 }
402
403
404 /**
405 * Called via ctx->Driver.AllocTextureImageBuffer().
406 * If the texture object/buffer already has space for the indicated image,
407 * we're done. Otherwise, allocate memory for the new texture image.
408 */
409 static GLboolean
st_AllocTextureImageBuffer(struct gl_context * ctx,struct gl_texture_image * texImage)410 st_AllocTextureImageBuffer(struct gl_context *ctx,
411 struct gl_texture_image *texImage)
412 {
413 struct st_context *st = st_context(ctx);
414 struct st_texture_image *stImage = st_texture_image(texImage);
415 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
416 const GLuint level = texImage->Level;
417 GLuint width = texImage->Width;
418 GLuint height = texImage->Height;
419 GLuint depth = texImage->Depth;
420
421 DBG("%s\n", __FUNCTION__);
422
423 assert(!stImage->TexData);
424 assert(!stImage->pt); /* xxx this might be wrong */
425
426 /* Look if the parent texture object has space for this image */
427 if (stObj->pt &&
428 level <= stObj->pt->last_level &&
429 st_texture_match_image(stObj->pt, texImage)) {
430 /* this image will fit in the existing texture object's memory */
431 pipe_resource_reference(&stImage->pt, stObj->pt);
432 return GL_TRUE;
433 }
434
435 /* The parent texture object does not have space for this image */
436
437 pipe_resource_reference(&stObj->pt, NULL);
438 pipe_sampler_view_release(st->pipe, &stObj->sampler_view);
439
440 if (!guess_and_alloc_texture(st, stObj, stImage)) {
441 /* Probably out of memory.
442 * Try flushing any pending rendering, then retry.
443 */
444 st_finish(st);
445 if (!guess_and_alloc_texture(st, stObj, stImage)) {
446 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
447 return GL_FALSE;
448 }
449 }
450
451 if (stObj->pt &&
452 st_texture_match_image(stObj->pt, texImage)) {
453 /* The image will live in the object's mipmap memory */
454 pipe_resource_reference(&stImage->pt, stObj->pt);
455 assert(stImage->pt);
456 return GL_TRUE;
457 }
458 else {
459 /* Create a new, temporary texture/resource/buffer to hold this
460 * one texture image. Note that when we later access this image
461 * (either for mapping or copying) we'll want to always specify
462 * mipmap level=0, even if the image represents some other mipmap
463 * level.
464 */
465 enum pipe_format format =
466 st_mesa_format_to_pipe_format(texImage->TexFormat);
467 GLuint bindings = default_bindings(st, format);
468 GLuint ptWidth, ptHeight, ptDepth, ptLayers;
469
470 st_gl_texture_dims_to_pipe_dims(stObj->base.Target,
471 width, height, depth,
472 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
473
474 stImage->pt = st_texture_create(st,
475 gl_target_to_pipe(stObj->base.Target),
476 format,
477 0, /* lastLevel */
478 ptWidth,
479 ptHeight,
480 ptDepth,
481 ptLayers,
482 bindings);
483 return stImage->pt != NULL;
484 }
485 }
486
487
488 /**
489 * Preparation prior to glTexImage. Basically check the 'surface_based'
490 * field and switch to a "normal" tex image if necessary.
491 */
492 static void
prep_teximage(struct gl_context * ctx,struct gl_texture_image * texImage,GLenum format,GLenum type)493 prep_teximage(struct gl_context *ctx, struct gl_texture_image *texImage,
494 GLenum format, GLenum type)
495 {
496 struct gl_texture_object *texObj = texImage->TexObject;
497 struct st_texture_object *stObj = st_texture_object(texObj);
498
499 /* switch to "normal" */
500 if (stObj->surface_based) {
501 const GLenum target = texObj->Target;
502 const GLuint level = texImage->Level;
503 gl_format texFormat;
504
505 _mesa_clear_texture_object(ctx, texObj);
506 pipe_resource_reference(&stObj->pt, NULL);
507
508 /* oops, need to init this image again */
509 texFormat = _mesa_choose_texture_format(ctx, texObj, target, level,
510 texImage->InternalFormat, format,
511 type);
512
513 _mesa_init_teximage_fields(ctx, texImage,
514 texImage->Width, texImage->Height,
515 texImage->Depth, texImage->Border,
516 texImage->InternalFormat, texFormat);
517
518 stObj->surface_based = GL_FALSE;
519 }
520 }
521
522
523 static void
st_TexImage(struct gl_context * ctx,GLuint dims,struct gl_texture_image * texImage,GLenum format,GLenum type,const void * pixels,const struct gl_pixelstore_attrib * unpack)524 st_TexImage(struct gl_context * ctx, GLuint dims,
525 struct gl_texture_image *texImage,
526 GLenum format, GLenum type, const void *pixels,
527 const struct gl_pixelstore_attrib *unpack)
528 {
529 prep_teximage(ctx, texImage, format, type);
530 _mesa_store_teximage(ctx, dims, texImage, format, type, pixels, unpack);
531 }
532
533
534 static void
st_CompressedTexImage(struct gl_context * ctx,GLuint dims,struct gl_texture_image * texImage,GLsizei imageSize,const GLvoid * data)535 st_CompressedTexImage(struct gl_context *ctx, GLuint dims,
536 struct gl_texture_image *texImage,
537 GLsizei imageSize, const GLvoid *data)
538 {
539 prep_teximage(ctx, texImage, GL_NONE, GL_NONE);
540 _mesa_store_compressed_teximage(ctx, dims, texImage, imageSize, data);
541 }
542
543
544
545 /**
546 * glGetTexImage() helper: decompress a compressed texture by rendering
547 * a textured quad. Store the results in the user's buffer.
548 */
549 static void
decompress_with_blit(struct gl_context * ctx,GLenum format,GLenum type,GLvoid * pixels,struct gl_texture_image * texImage)550 decompress_with_blit(struct gl_context * ctx,
551 GLenum format, GLenum type, GLvoid *pixels,
552 struct gl_texture_image *texImage)
553 {
554 struct st_context *st = st_context(ctx);
555 struct pipe_context *pipe = st->pipe;
556 struct st_texture_image *stImage = st_texture_image(texImage);
557 struct st_texture_object *stObj = st_texture_object(texImage->TexObject);
558 struct pipe_sampler_view *src_view;
559 const GLuint width = texImage->Width;
560 const GLuint height = texImage->Height;
561 struct pipe_surface *dst_surface;
562 struct pipe_resource *dst_texture;
563 struct pipe_transfer *tex_xfer;
564 unsigned bind = (PIPE_BIND_RENDER_TARGET | /* util_blit may choose to render */
565 PIPE_BIND_TRANSFER_READ);
566
567 /* create temp / dest surface */
568 if (!util_create_rgba_surface(pipe, width, height, bind,
569 &dst_texture, &dst_surface)) {
570 _mesa_problem(ctx, "util_create_rgba_surface() failed "
571 "in decompress_with_blit()");
572 return;
573 }
574
575 /* Disable conditional rendering. */
576 if (st->render_condition) {
577 pipe->render_condition(pipe, NULL, 0);
578 }
579
580 /* Create sampler view that limits fetches to the source mipmap level */
581 {
582 struct pipe_sampler_view sv_temp;
583
584 u_sampler_view_default_template(&sv_temp, stObj->pt, stObj->pt->format);
585
586 sv_temp.format = util_format_linear(sv_temp.format);
587 sv_temp.u.tex.first_level =
588 sv_temp.u.tex.last_level = texImage->Level;
589
590 src_view = pipe->create_sampler_view(pipe, stObj->pt, &sv_temp);
591 if (!src_view) {
592 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
593 return;
594 }
595 }
596
597 /* blit/render/decompress */
598 util_blit_pixels_tex(st->blit,
599 src_view, /* pipe_resource (src) */
600 0, 0, /* src x0, y0 */
601 width, height, /* src x1, y1 */
602 dst_surface, /* pipe_surface (dst) */
603 0, 0, /* dst x0, y0 */
604 width, height, /* dst x1, y1 */
605 0.0, /* z */
606 PIPE_TEX_MIPFILTER_NEAREST);
607
608 /* Restore conditional rendering state. */
609 if (st->render_condition) {
610 pipe->render_condition(pipe, st->render_condition,
611 st->condition_mode);
612 }
613
614 /* map the dst_surface so we can read from it */
615 tex_xfer = pipe_get_transfer(pipe,
616 dst_texture, 0, 0,
617 PIPE_TRANSFER_READ,
618 0, 0, width, height);
619
620 pixels = _mesa_map_pbo_dest(ctx, &ctx->Pack, pixels);
621
622 /* copy/pack data into user buffer */
623 if (_mesa_format_matches_format_and_type(stImage->base.TexFormat,
624 format, type,
625 ctx->Pack.SwapBytes)) {
626 /* memcpy */
627 const uint bytesPerRow = width * util_format_get_blocksize(stImage->pt->format);
628 ubyte *map = pipe_transfer_map(pipe, tex_xfer);
629 GLuint row;
630 for (row = 0; row < height; row++) {
631 GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
632 height, format, type, row, 0);
633 memcpy(dest, map, bytesPerRow);
634 map += tex_xfer->stride;
635 }
636 pipe_transfer_unmap(pipe, tex_xfer);
637 }
638 else {
639 /* format translation via floats */
640 GLuint row;
641 enum pipe_format pformat = util_format_linear(dst_texture->format);
642 GLfloat *rgba;
643
644 rgba = (GLfloat *) malloc(width * 4 * sizeof(GLfloat));
645 if (!rgba) {
646 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
647 goto end;
648 }
649
650 for (row = 0; row < height; row++) {
651 const GLbitfield transferOps = 0x0; /* bypassed for glGetTexImage() */
652 GLvoid *dest = _mesa_image_address2d(&ctx->Pack, pixels, width,
653 height, format, type, row, 0);
654
655 if (ST_DEBUG & DEBUG_FALLBACK)
656 debug_printf("%s: fallback format translation\n", __FUNCTION__);
657
658 /* get float[4] rgba row from surface */
659 pipe_get_tile_rgba_format(pipe, tex_xfer, 0, row, width, 1,
660 pformat, rgba);
661
662 _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba, format,
663 type, dest, &ctx->Pack, transferOps);
664 }
665
666 free(rgba);
667 }
668
669 end:
670 _mesa_unmap_pbo_dest(ctx, &ctx->Pack);
671
672 pipe->transfer_destroy(pipe, tex_xfer);
673
674 /* destroy the temp / dest surface */
675 util_destroy_rgba_surface(dst_texture, dst_surface);
676
677 pipe_sampler_view_release(pipe, &src_view);
678 }
679
680
681
682 /**
683 * Called via ctx->Driver.GetTexImage()
684 */
685 static void
st_GetTexImage(struct gl_context * ctx,GLenum format,GLenum type,GLvoid * pixels,struct gl_texture_image * texImage)686 st_GetTexImage(struct gl_context * ctx,
687 GLenum format, GLenum type, GLvoid * pixels,
688 struct gl_texture_image *texImage)
689 {
690 struct st_texture_image *stImage = st_texture_image(texImage);
691
692 if (stImage->pt && util_format_is_s3tc(stImage->pt->format)) {
693 /* Need to decompress the texture.
694 * We'll do this by rendering a textured quad (which is hopefully
695 * faster than using the fallback code in texcompress.c).
696 * Note that we only expect RGBA formats (no Z/depth formats).
697 */
698 decompress_with_blit(ctx, format, type, pixels, texImage);
699 }
700 else {
701 _mesa_get_teximage(ctx, format, type, pixels, texImage);
702 }
703 }
704
705
706 /**
707 * Do a CopyTexSubImage operation using a read transfer from the source,
708 * a write transfer to the destination and get_tile()/put_tile() to access
709 * the pixels/texels.
710 *
711 * Note: srcY=0=TOP of renderbuffer
712 */
713 static void
fallback_copy_texsubimage(struct gl_context * ctx,struct st_renderbuffer * strb,struct st_texture_image * stImage,GLenum baseFormat,GLint destX,GLint destY,GLint destZ,GLint srcX,GLint srcY,GLsizei width,GLsizei height)714 fallback_copy_texsubimage(struct gl_context *ctx,
715 struct st_renderbuffer *strb,
716 struct st_texture_image *stImage,
717 GLenum baseFormat,
718 GLint destX, GLint destY, GLint destZ,
719 GLint srcX, GLint srcY,
720 GLsizei width, GLsizei height)
721 {
722 struct st_context *st = st_context(ctx);
723 struct pipe_context *pipe = st->pipe;
724 struct pipe_transfer *src_trans;
725 GLvoid *texDest;
726 enum pipe_transfer_usage transfer_usage;
727
728 if (ST_DEBUG & DEBUG_FALLBACK)
729 debug_printf("%s: fallback processing\n", __FUNCTION__);
730
731 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
732 srcY = strb->Base.Height - srcY - height;
733 }
734
735 src_trans = pipe_get_transfer(pipe,
736 strb->texture,
737 strb->rtt_level,
738 strb->rtt_face + strb->rtt_slice,
739 PIPE_TRANSFER_READ,
740 srcX, srcY,
741 width, height);
742
743 if ((baseFormat == GL_DEPTH_COMPONENT ||
744 baseFormat == GL_DEPTH_STENCIL) &&
745 util_format_is_depth_and_stencil(stImage->pt->format))
746 transfer_usage = PIPE_TRANSFER_READ_WRITE;
747 else
748 transfer_usage = PIPE_TRANSFER_WRITE;
749
750 /* XXX this used to ignore destZ param */
751 texDest = st_texture_image_map(st, stImage, destZ, transfer_usage,
752 destX, destY, width, height);
753
754 if (baseFormat == GL_DEPTH_COMPONENT ||
755 baseFormat == GL_DEPTH_STENCIL) {
756 const GLboolean scaleOrBias = (ctx->Pixel.DepthScale != 1.0F ||
757 ctx->Pixel.DepthBias != 0.0F);
758 GLint row, yStep;
759 uint *data;
760
761 /* determine bottom-to-top vs. top-to-bottom order for src buffer */
762 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
763 srcY = height - 1;
764 yStep = -1;
765 }
766 else {
767 srcY = 0;
768 yStep = 1;
769 }
770
771 data = (uint *) malloc(width * sizeof(uint));
772
773 if (data) {
774 /* To avoid a large temp memory allocation, do copy row by row */
775 for (row = 0; row < height; row++, srcY += yStep) {
776 pipe_get_tile_z(pipe, src_trans, 0, srcY, width, 1, data);
777 if (scaleOrBias) {
778 _mesa_scale_and_bias_depth_uint(ctx, width, data);
779 }
780 pipe_put_tile_z(pipe, stImage->transfer, 0, row, width, 1, data);
781 }
782 }
783 else {
784 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCopyTexSubImage()");
785 }
786
787 free(data);
788 }
789 else {
790 /* RGBA format */
791 GLfloat *tempSrc =
792 (GLfloat *) malloc(width * height * 4 * sizeof(GLfloat));
793
794 if (tempSrc && texDest) {
795 const GLint dims = 2;
796 const GLint dstRowStride = stImage->transfer->stride;
797 struct gl_texture_image *texImage = &stImage->base;
798 struct gl_pixelstore_attrib unpack = ctx->DefaultPacking;
799
800 if (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP) {
801 unpack.Invert = GL_TRUE;
802 }
803
804 /* get float/RGBA image from framebuffer */
805 /* XXX this usually involves a lot of int/float conversion.
806 * try to avoid that someday.
807 */
808 pipe_get_tile_rgba_format(pipe, src_trans, 0, 0, width, height,
809 util_format_linear(strb->texture->format),
810 tempSrc);
811
812 /* Store into texture memory.
813 * Note that this does some special things such as pixel transfer
814 * ops and format conversion. In particular, if the dest tex format
815 * is actually RGBA but the user created the texture as GL_RGB we
816 * need to fill-in/override the alpha channel with 1.0.
817 */
818 _mesa_texstore(ctx, dims,
819 texImage->_BaseFormat,
820 texImage->TexFormat,
821 dstRowStride,
822 (GLubyte **) &texDest,
823 width, height, 1,
824 GL_RGBA, GL_FLOAT, tempSrc, /* src */
825 &unpack);
826 }
827 else {
828 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
829 }
830
831 if (tempSrc)
832 free(tempSrc);
833 }
834
835 st_texture_image_unmap(st, stImage);
836 pipe->transfer_destroy(pipe, src_trans);
837 }
838
839
840
841 /**
842 * If the format of the src renderbuffer and the format of the dest
843 * texture are compatible (in terms of blitting), return a TGSI writemask
844 * to be used during the blit.
845 * If the src/dest are incompatible, return 0.
846 */
847 static unsigned
compatible_src_dst_formats(struct gl_context * ctx,const struct gl_renderbuffer * src,const struct gl_texture_image * dst)848 compatible_src_dst_formats(struct gl_context *ctx,
849 const struct gl_renderbuffer *src,
850 const struct gl_texture_image *dst)
851 {
852 /* Get logical base formats for the src and dest.
853 * That is, use the user-requested formats and not the actual, device-
854 * chosen formats.
855 * For example, the user may have requested an A8 texture but the
856 * driver may actually be using an RGBA texture format. When we
857 * copy/blit to that texture, we only want to copy the Alpha channel
858 * and not the RGB channels.
859 *
860 * Similarly, when the src FBO was created an RGB format may have been
861 * requested but the driver actually chose an RGBA format. In that case,
862 * we don't want to copy the undefined Alpha channel to the dest texture
863 * (it should be 1.0).
864 */
865 const GLenum srcFormat = _mesa_base_fbo_format(ctx, src->InternalFormat);
866 const GLenum dstFormat = _mesa_base_tex_format(ctx, dst->InternalFormat);
867
868 /**
869 * XXX when we have red-only and red/green renderbuffers we'll need
870 * to add more cases here (or implement a general-purpose routine that
871 * queries the existance of the R,G,B,A channels in the src and dest).
872 */
873 if (srcFormat == dstFormat) {
874 /* This is the same as matching_base_formats, which should
875 * always pass, as it did previously.
876 */
877 return TGSI_WRITEMASK_XYZW;
878 }
879 else if (srcFormat == GL_RGB && dstFormat == GL_RGBA) {
880 /* Make sure that A in the dest is 1. The actual src format
881 * may be RGBA and have undefined A values.
882 */
883 return TGSI_WRITEMASK_XYZ;
884 }
885 else if (srcFormat == GL_RGBA && dstFormat == GL_RGB) {
886 /* Make sure that A in the dest is 1. The actual dst format
887 * may be RGBA and will need A=1 to provide proper alpha values
888 * when sampled later.
889 */
890 return TGSI_WRITEMASK_XYZ;
891 }
892 else {
893 if (ST_DEBUG & DEBUG_FALLBACK)
894 debug_printf("%s failed for src %s, dst %s\n",
895 __FUNCTION__,
896 _mesa_lookup_enum_by_nr(srcFormat),
897 _mesa_lookup_enum_by_nr(dstFormat));
898
899 /* Otherwise fail.
900 */
901 return 0;
902 }
903 }
904
905
906
907 /**
908 * Do a CopyTex[Sub]Image1/2/3D() using a hardware (blit) path if possible.
909 * Note that the region to copy has already been clipped so we know we
910 * won't read from outside the source renderbuffer's bounds.
911 *
912 * Note: srcY=0=Bottom of renderbuffer (GL convention)
913 */
914 static void
st_CopyTexSubImage(struct gl_context * ctx,GLuint dims,struct gl_texture_image * texImage,GLint destX,GLint destY,GLint destZ,struct gl_renderbuffer * rb,GLint srcX,GLint srcY,GLsizei width,GLsizei height)915 st_CopyTexSubImage(struct gl_context *ctx, GLuint dims,
916 struct gl_texture_image *texImage,
917 GLint destX, GLint destY, GLint destZ,
918 struct gl_renderbuffer *rb,
919 GLint srcX, GLint srcY, GLsizei width, GLsizei height)
920 {
921 struct st_texture_image *stImage = st_texture_image(texImage);
922 const GLenum texBaseFormat = texImage->_BaseFormat;
923 struct gl_framebuffer *fb = ctx->ReadBuffer;
924 struct st_renderbuffer *strb;
925 struct st_context *st = st_context(ctx);
926 struct pipe_context *pipe = st->pipe;
927 struct pipe_screen *screen = pipe->screen;
928 enum pipe_format dest_format, src_format;
929 GLboolean matching_base_formats;
930 GLuint color_writemask, zs_writemask, sample_count;
931 struct pipe_surface *dest_surface = NULL;
932 GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
933 struct pipe_surface surf_tmpl;
934 unsigned int dst_usage;
935 GLint srcY0, srcY1;
936
937 /* make sure finalize_textures has been called?
938 */
939 if (0) st_validate_state(st);
940
941 /* determine if copying depth or color data */
942 if (texBaseFormat == GL_DEPTH_COMPONENT ||
943 texBaseFormat == GL_DEPTH_STENCIL) {
944 strb = st_renderbuffer(fb->Attachment[BUFFER_DEPTH].Renderbuffer);
945 }
946 else {
947 /* texBaseFormat == GL_RGB, GL_RGBA, GL_ALPHA, etc */
948 strb = st_renderbuffer(fb->_ColorReadBuffer);
949 }
950
951 if (!strb || !strb->surface || !stImage->pt) {
952 debug_printf("%s: null strb or stImage\n", __FUNCTION__);
953 return;
954 }
955
956 sample_count = strb->surface->texture->nr_samples;
957 /* I believe this would be legal, presumably would need to do a resolve
958 for color, and for depth/stencil spec says to just use one of the
959 depth/stencil samples per pixel? Need some transfer clarifications. */
960 assert(sample_count < 2);
961
962 assert(strb);
963 assert(strb->surface);
964 assert(stImage->pt);
965
966 src_format = strb->surface->format;
967 dest_format = stImage->pt->format;
968
969 /*
970 * Determine if the src framebuffer and dest texture have the same
971 * base format. We need this to detect a case such as the framebuffer
972 * being GL_RGBA but the texture being GL_RGB. If the actual hardware
973 * texture format stores RGBA we need to set A=1 (overriding the
974 * framebuffer's alpha values). We can't do that with the blit or
975 * textured-quad paths.
976 */
977 matching_base_formats =
978 (_mesa_get_format_base_format(strb->Base.Format) ==
979 _mesa_get_format_base_format(texImage->TexFormat));
980
981 if (ctx->_ImageTransferState) {
982 goto fallback;
983 }
984
985 if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
986 /* 1D arrays might be thought of as 2D images but the actual layout
987 * might not be that way. At some points, we convert OpenGL's 1D
988 * array 'height' into gallium 'layers' and that prevents the blit
989 * utility code from doing the right thing. Simpy use the memcpy-based
990 * fallback.
991 */
992 goto fallback;
993 }
994
995 if (matching_base_formats &&
996 src_format == dest_format &&
997 !do_flip) {
998 /* use surface_copy() / blit */
999 struct pipe_box src_box;
1000 unsigned dstLevel;
1001
1002 u_box_2d_zslice(srcX, srcY, strb->surface->u.tex.first_layer,
1003 width, height, &src_box);
1004
1005 /* If stImage->pt is an independent image (not a pointer into a full
1006 * mipmap) stImage->pt.last_level will be zero and we need to use that
1007 * as the dest level.
1008 */
1009 dstLevel = MIN2(stImage->base.Level, stImage->pt->last_level);
1010
1011 /* for resource_copy_region(), y=0=top, always */
1012 pipe->resource_copy_region(pipe,
1013 /* dest */
1014 stImage->pt,
1015 dstLevel,
1016 destX, destY, destZ + stImage->base.Face,
1017 /* src */
1018 strb->texture,
1019 strb->surface->u.tex.level,
1020 &src_box);
1021 return;
1022 }
1023
1024 if (texBaseFormat == GL_DEPTH_STENCIL) {
1025 goto fallback;
1026 }
1027
1028 if (texBaseFormat == GL_DEPTH_COMPONENT) {
1029 color_writemask = 0;
1030 zs_writemask = BLIT_WRITEMASK_Z;
1031 dst_usage = PIPE_BIND_DEPTH_STENCIL;
1032 }
1033 else {
1034 color_writemask = compatible_src_dst_formats(ctx, &strb->Base, texImage);
1035 zs_writemask = 0;
1036 dst_usage = PIPE_BIND_RENDER_TARGET;
1037 }
1038
1039 if ((!color_writemask && !zs_writemask) ||
1040 !screen->is_format_supported(screen, src_format,
1041 PIPE_TEXTURE_2D, sample_count,
1042 PIPE_BIND_SAMPLER_VIEW) ||
1043 !screen->is_format_supported(screen, dest_format,
1044 PIPE_TEXTURE_2D, 0,
1045 dst_usage)) {
1046 goto fallback;
1047 }
1048
1049 if (do_flip) {
1050 srcY1 = strb->Base.Height - srcY - height;
1051 srcY0 = srcY1 + height;
1052 }
1053 else {
1054 srcY0 = srcY;
1055 srcY1 = srcY0 + height;
1056 }
1057
1058 /* Disable conditional rendering. */
1059 if (st->render_condition) {
1060 pipe->render_condition(pipe, NULL, 0);
1061 }
1062
1063 memset(&surf_tmpl, 0, sizeof(surf_tmpl));
1064 surf_tmpl.format = util_format_linear(stImage->pt->format);
1065 surf_tmpl.usage = dst_usage;
1066 surf_tmpl.u.tex.level = stImage->base.Level;
1067 surf_tmpl.u.tex.first_layer = stImage->base.Face + destZ;
1068 surf_tmpl.u.tex.last_layer = stImage->base.Face + destZ;
1069
1070 dest_surface = pipe->create_surface(pipe, stImage->pt,
1071 &surf_tmpl);
1072 util_blit_pixels(st->blit,
1073 strb->texture,
1074 strb->surface->u.tex.level,
1075 srcX, srcY0,
1076 srcX + width, srcY1,
1077 strb->surface->u.tex.first_layer,
1078 dest_surface,
1079 destX, destY,
1080 destX + width, destY + height,
1081 0.0, PIPE_TEX_MIPFILTER_NEAREST,
1082 color_writemask, zs_writemask);
1083 pipe_surface_reference(&dest_surface, NULL);
1084
1085 /* Restore conditional rendering state. */
1086 if (st->render_condition) {
1087 pipe->render_condition(pipe, st->render_condition,
1088 st->condition_mode);
1089 }
1090
1091 return;
1092
1093 fallback:
1094 /* software fallback */
1095 fallback_copy_texsubimage(ctx,
1096 strb, stImage, texBaseFormat,
1097 destX, destY, destZ,
1098 srcX, srcY, width, height);
1099 }
1100
1101
1102 /**
1103 * Copy image data from stImage into the texture object 'stObj' at level
1104 * 'dstLevel'.
1105 */
1106 static void
copy_image_data_to_texture(struct st_context * st,struct st_texture_object * stObj,GLuint dstLevel,struct st_texture_image * stImage)1107 copy_image_data_to_texture(struct st_context *st,
1108 struct st_texture_object *stObj,
1109 GLuint dstLevel,
1110 struct st_texture_image *stImage)
1111 {
1112 /* debug checks */
1113 {
1114 const struct gl_texture_image *dstImage =
1115 stObj->base.Image[stImage->base.Face][dstLevel];
1116 assert(dstImage);
1117 assert(dstImage->Width == stImage->base.Width);
1118 assert(dstImage->Height == stImage->base.Height);
1119 assert(dstImage->Depth == stImage->base.Depth);
1120 }
1121
1122 if (stImage->pt) {
1123 /* Copy potentially with the blitter:
1124 */
1125 GLuint src_level;
1126 if (stImage->pt->last_level == 0)
1127 src_level = 0;
1128 else
1129 src_level = stImage->base.Level;
1130
1131 assert(src_level <= stImage->pt->last_level);
1132 assert(u_minify(stImage->pt->width0, src_level) == stImage->base.Width);
1133 assert(stImage->pt->target == PIPE_TEXTURE_1D_ARRAY ||
1134 u_minify(stImage->pt->height0, src_level) == stImage->base.Height);
1135 assert(stImage->pt->target == PIPE_TEXTURE_2D_ARRAY ||
1136 u_minify(stImage->pt->depth0, src_level) == stImage->base.Depth);
1137
1138 st_texture_image_copy(st->pipe,
1139 stObj->pt, dstLevel, /* dest texture, level */
1140 stImage->pt, src_level, /* src texture, level */
1141 stImage->base.Face);
1142
1143 pipe_resource_reference(&stImage->pt, NULL);
1144 }
1145 else if (stImage->TexData) {
1146 /* Copy from malloc'd memory */
1147 /* XXX this should be re-examined/tested with a compressed format */
1148 GLuint blockSize = util_format_get_blocksize(stObj->pt->format);
1149 GLuint srcRowStride = stImage->base.Width * blockSize;
1150 GLuint srcSliceStride = stImage->base.Height * srcRowStride;
1151 st_texture_image_data(st,
1152 stObj->pt,
1153 stImage->base.Face,
1154 dstLevel,
1155 stImage->TexData,
1156 srcRowStride,
1157 srcSliceStride);
1158 _mesa_align_free(stImage->TexData);
1159 stImage->TexData = NULL;
1160 }
1161
1162 pipe_resource_reference(&stImage->pt, stObj->pt);
1163 }
1164
1165
1166 /**
1167 * Called during state validation. When this function is finished,
1168 * the texture object should be ready for rendering.
1169 * \return GL_TRUE for success, GL_FALSE for failure (out of mem)
1170 */
1171 GLboolean
st_finalize_texture(struct gl_context * ctx,struct pipe_context * pipe,struct gl_texture_object * tObj)1172 st_finalize_texture(struct gl_context *ctx,
1173 struct pipe_context *pipe,
1174 struct gl_texture_object *tObj)
1175 {
1176 struct st_context *st = st_context(ctx);
1177 struct st_texture_object *stObj = st_texture_object(tObj);
1178 const GLuint nr_faces = (stObj->base.Target == GL_TEXTURE_CUBE_MAP) ? 6 : 1;
1179 GLuint face;
1180 struct st_texture_image *firstImage;
1181 enum pipe_format firstImageFormat;
1182 GLuint ptWidth, ptHeight, ptDepth, ptLayers;
1183
1184 if (_mesa_is_texture_complete(tObj, &tObj->Sampler)) {
1185 /* The texture is complete and we know exactly how many mipmap levels
1186 * are present/needed. This is conditional because we may be called
1187 * from the st_generate_mipmap() function when the texture object is
1188 * incomplete. In that case, we'll have set stObj->lastLevel before
1189 * we get here.
1190 */
1191 if (stObj->base.Sampler.MinFilter == GL_LINEAR ||
1192 stObj->base.Sampler.MinFilter == GL_NEAREST)
1193 stObj->lastLevel = stObj->base.BaseLevel;
1194 else
1195 stObj->lastLevel = stObj->base._MaxLevel;
1196 }
1197
1198 firstImage = st_texture_image(stObj->base.Image[0][stObj->base.BaseLevel]);
1199 assert(firstImage);
1200
1201 /* If both firstImage and stObj point to a texture which can contain
1202 * all active images, favour firstImage. Note that because of the
1203 * completeness requirement, we know that the image dimensions
1204 * will match.
1205 */
1206 if (firstImage->pt &&
1207 firstImage->pt != stObj->pt &&
1208 (!stObj->pt || firstImage->pt->last_level >= stObj->pt->last_level)) {
1209 pipe_resource_reference(&stObj->pt, firstImage->pt);
1210 pipe_sampler_view_release(st->pipe, &stObj->sampler_view);
1211 }
1212
1213 /* Find gallium format for the Mesa texture */
1214 firstImageFormat = st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
1215
1216 /* Find size of level=0 Gallium mipmap image, plus number of texture layers */
1217 {
1218 GLuint width, height, depth;
1219 if (!guess_base_level_size(stObj->base.Target,
1220 firstImage->base.Width2,
1221 firstImage->base.Height2,
1222 firstImage->base.Depth2,
1223 firstImage->base.Level,
1224 &width, &height, &depth)) {
1225 width = stObj->width0;
1226 height = stObj->height0;
1227 depth = stObj->depth0;
1228 }
1229 /* convert GL dims to Gallium dims */
1230 st_gl_texture_dims_to_pipe_dims(stObj->base.Target, width, height, depth,
1231 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
1232 }
1233
1234 /* If we already have a gallium texture, check that it matches the texture
1235 * object's format, target, size, num_levels, etc.
1236 */
1237 if (stObj->pt) {
1238 if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
1239 !st_sampler_compat_formats(stObj->pt->format, firstImageFormat) ||
1240 stObj->pt->last_level < stObj->lastLevel ||
1241 stObj->pt->width0 != ptWidth ||
1242 stObj->pt->height0 != ptHeight ||
1243 stObj->pt->depth0 != ptDepth ||
1244 stObj->pt->array_size != ptLayers)
1245 {
1246 /* The gallium texture does not match the Mesa texture so delete the
1247 * gallium texture now. We'll make a new one below.
1248 */
1249 pipe_resource_reference(&stObj->pt, NULL);
1250 pipe_sampler_view_release(st->pipe, &stObj->sampler_view);
1251 st->dirty.st |= ST_NEW_FRAMEBUFFER;
1252 }
1253 }
1254
1255 /* May need to create a new gallium texture:
1256 */
1257 if (!stObj->pt) {
1258 GLuint bindings = default_bindings(st, firstImageFormat);
1259
1260 stObj->pt = st_texture_create(st,
1261 gl_target_to_pipe(stObj->base.Target),
1262 firstImageFormat,
1263 stObj->lastLevel,
1264 ptWidth,
1265 ptHeight,
1266 ptDepth,
1267 ptLayers,
1268 bindings);
1269
1270 if (!stObj->pt) {
1271 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
1272 return GL_FALSE;
1273 }
1274 }
1275
1276 /* Pull in any images not in the object's texture:
1277 */
1278 for (face = 0; face < nr_faces; face++) {
1279 GLuint level;
1280 for (level = stObj->base.BaseLevel; level <= stObj->lastLevel; level++) {
1281 struct st_texture_image *stImage =
1282 st_texture_image(stObj->base.Image[face][level]);
1283
1284 /* Need to import images in main memory or held in other textures.
1285 */
1286 if (stImage && stObj->pt != stImage->pt) {
1287 if (level == 0 ||
1288 (stImage->base.Width == u_minify(stObj->width0, level) &&
1289 stImage->base.Height == u_minify(stObj->height0, level) &&
1290 stImage->base.Depth == u_minify(stObj->depth0, level))) {
1291 /* src image fits expected dest mipmap level size */
1292 copy_image_data_to_texture(st, stObj, level, stImage);
1293 }
1294 }
1295 }
1296 }
1297
1298 return GL_TRUE;
1299 }
1300
1301
1302 /**
1303 * Called via ctx->Driver.AllocTextureStorage() to allocate texture memory
1304 * for a whole mipmap stack.
1305 */
1306 static GLboolean
st_AllocTextureStorage(struct gl_context * ctx,struct gl_texture_object * texObj,GLsizei levels,GLsizei width,GLsizei height,GLsizei depth)1307 st_AllocTextureStorage(struct gl_context *ctx,
1308 struct gl_texture_object *texObj,
1309 GLsizei levels, GLsizei width,
1310 GLsizei height, GLsizei depth)
1311 {
1312 const GLuint numFaces = _mesa_num_tex_faces(texObj->Target);
1313 struct st_context *st = st_context(ctx);
1314 struct st_texture_object *stObj = st_texture_object(texObj);
1315 GLuint ptWidth, ptHeight, ptDepth, ptLayers, bindings;
1316 enum pipe_format fmt;
1317 GLint level;
1318
1319 assert(levels > 0);
1320
1321 /* Save the level=0 dimensions */
1322 stObj->width0 = width;
1323 stObj->height0 = height;
1324 stObj->depth0 = depth;
1325 stObj->lastLevel = levels - 1;
1326
1327 fmt = st_mesa_format_to_pipe_format(texObj->Image[0][0]->TexFormat);
1328
1329 bindings = default_bindings(st, fmt);
1330
1331 st_gl_texture_dims_to_pipe_dims(texObj->Target,
1332 width, height, depth,
1333 &ptWidth, &ptHeight, &ptDepth, &ptLayers);
1334
1335 stObj->pt = st_texture_create(st,
1336 gl_target_to_pipe(texObj->Target),
1337 fmt,
1338 levels,
1339 ptWidth,
1340 ptHeight,
1341 ptDepth,
1342 ptLayers,
1343 bindings);
1344 if (!stObj->pt)
1345 return GL_FALSE;
1346
1347 /* Set image resource pointers */
1348 for (level = 0; level < levels; level++) {
1349 GLuint face;
1350 for (face = 0; face < numFaces; face++) {
1351 struct st_texture_image *stImage =
1352 st_texture_image(texObj->Image[face][level]);
1353 pipe_resource_reference(&stImage->pt, stObj->pt);
1354 }
1355 }
1356
1357 return GL_TRUE;
1358 }
1359
1360
1361
1362 void
st_init_texture_functions(struct dd_function_table * functions)1363 st_init_texture_functions(struct dd_function_table *functions)
1364 {
1365 functions->ChooseTextureFormat = st_ChooseTextureFormat;
1366 functions->TexImage = st_TexImage;
1367 functions->TexSubImage = _mesa_store_texsubimage;
1368 functions->CompressedTexSubImage = _mesa_store_compressed_texsubimage;
1369 functions->CopyTexSubImage = st_CopyTexSubImage;
1370 functions->GenerateMipmap = st_generate_mipmap;
1371
1372 functions->GetTexImage = st_GetTexImage;
1373
1374 /* compressed texture functions */
1375 functions->CompressedTexImage = st_CompressedTexImage;
1376 functions->GetCompressedTexImage = _mesa_get_compressed_teximage;
1377
1378 functions->NewTextureObject = st_NewTextureObject;
1379 functions->NewTextureImage = st_NewTextureImage;
1380 functions->DeleteTextureImage = st_DeleteTextureImage;
1381 functions->DeleteTexture = st_DeleteTextureObject;
1382 functions->AllocTextureImageBuffer = st_AllocTextureImageBuffer;
1383 functions->FreeTextureImageBuffer = st_FreeTextureImageBuffer;
1384 functions->MapTextureImage = st_MapTextureImage;
1385 functions->UnmapTextureImage = st_UnmapTextureImage;
1386
1387 /* XXX Temporary until we can query pipe's texture sizes */
1388 functions->TestProxyTexImage = _mesa_test_proxy_teximage;
1389
1390 functions->AllocTextureStorage = st_AllocTextureStorage;
1391 }
1392