115
114
return &obj->base;
118
/** called via ctx->Driver.DeleteTextureImage() */
117
/** called via ctx->Driver.DeleteTextureObject() */
120
119
st_DeleteTextureObject(GLcontext *ctx,
121
120
struct gl_texture_object *texObj)
122
struct st_context *st = st_context(ctx);
123
123
struct st_texture_object *stObj = st_texture_object(texObj);
125
pipe_texture_reference(&stObj->pt, NULL);
125
pipe_resource_reference(&stObj->pt, NULL);
126
if (stObj->sampler_view) {
127
if (stObj->sampler_view->context != st->pipe) {
128
/* Take "ownership" of this texture sampler view by setting
129
* its context pointer to this context. This avoids potential
130
* crashes when the texture object is shared among contexts
131
* and the original/owner context has already been destroyed.
133
stObj->sampler_view->context = st->pipe;
135
pipe_sampler_view_reference(&stObj->sampler_view, NULL);
127
137
_mesa_delete_texture_object(ctx, texObj);
201
* Return default texture usage bitmask for the given texture format.
211
* Return default texture resource binding bitmask for the given format.
204
default_usage(enum pipe_format fmt)
206
GLuint usage = PIPE_TEXTURE_USAGE_SAMPLER;
207
if (util_format_is_depth_or_stencil(fmt))
208
usage |= PIPE_TEXTURE_USAGE_DEPTH_STENCIL;
210
usage |= PIPE_TEXTURE_USAGE_RENDER_TARGET;
214
default_bindings(struct st_context *st, enum pipe_format format)
216
struct pipe_screen *screen = st->pipe->screen;
217
const unsigned target = PIPE_TEXTURE_2D;
218
const unsigned geom = 0x0;
221
if (util_format_is_depth_or_stencil(format))
222
bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_DEPTH_STENCIL;
224
bindings = PIPE_BIND_SAMPLER_VIEW | PIPE_BIND_RENDER_TARGET;
226
if (screen->is_format_supported(screen, format, target, 0, bindings, geom))
229
return PIPE_BIND_SAMPLER_VIEW;
233
/** Return number of image dimensions (1, 2 or 3) for a texture target. */
235
get_texture_dims(GLenum target)
239
case GL_TEXTURE_1D_ARRAY_EXT:
242
case GL_TEXTURE_CUBE_MAP_ARB:
243
case GL_TEXTURE_RECTANGLE_NV:
244
case GL_TEXTURE_2D_ARRAY_EXT:
249
assert(0 && "invalid texture target in get_texture_dims()");
216
* Allocate a pipe_texture object for the given st_texture_object using
217
* the given st_texture_image to guess the mipmap size/levels.
220
* Otherwise, store it in memory if (Border != 0) or (any dimension ==
223
* Otherwise, if max_level >= level >= min_level, create texture with
224
* space for images from min_level down to max_level.
226
* Otherwise, create texture with space for images from (level 0)..(1x1).
227
* Consider pruning this texture at a validation if the saving is worth it.
256
* Try to allocate a pipe_resource object for the given st_texture_object.
258
* We use the given st_texture_image as a clue to determine the size of the
259
* mipmap image at level=0.
261
* \return GL_TRUE for success, GL_FALSE if out of memory.
230
264
guess_and_alloc_texture(struct st_context *st,
231
265
struct st_texture_object *stObj,
232
266
const struct st_texture_image *stImage)
236
GLuint width = stImage->base.Width2; /* size w/out border */
237
GLuint height = stImage->base.Height2;
238
GLuint depth = stImage->base.Depth2;
268
const GLuint dims = get_texture_dims(stObj->base.Target);
269
GLuint level, lastLevel, width, height, depth;
240
271
enum pipe_format fmt;
242
273
DBG("%s\n", __FUNCTION__);
244
275
assert(!stObj->pt);
247
(GLint) stImage->level > stObj->base.BaseLevel &&
248
(stImage->base.Width == 1 ||
249
(stObj->base.Target != GL_TEXTURE_1D &&
250
stImage->base.Height == 1) ||
251
(stObj->base.Target == GL_TEXTURE_3D &&
252
stImage->base.Depth == 1)))
255
/* If this image disrespects BaseLevel, allocate from level zero.
256
* Usually BaseLevel == 0, so it's unlikely to happen.
258
if ((GLint) stImage->level < stObj->base.BaseLevel)
261
firstLevel = stObj->base.BaseLevel;
264
/* Figure out image dimensions at start level.
266
for (i = stImage->level; i > firstLevel; i--) {
277
level = stImage->level;
278
width = stImage->base.Width2; /* size w/out border */
279
height = stImage->base.Height2;
280
depth = stImage->base.Depth2;
286
/* Depending on the image's size, we can't always make a guess here.
289
if ( (dims >= 1 && width == 1) ||
290
(dims >= 2 && height == 1) ||
291
(dims >= 3 && depth == 1) ) {
292
/* we can't determine the image size at level=0 */
293
stObj->width0 = stObj->height0 = stObj->depth0 = 0;
294
/* this is not an out of memory error */
299
/* grow the image size until we hit level = 0 */
275
if (width == 0 || height == 0 || depth == 0) {
276
/* no texture needed */
280
/* Guess a reasonable value for lastLevel. This is probably going
281
* to be wrong fairly often and might mean that we have to look at
282
* resizable buffers, or require that buffers implement lazy
283
* pagetable arrangements.
312
/* At this point, (width x height x depth) is the expected size of
313
* the level=0 mipmap image.
316
/* Guess a reasonable value for lastLevel. With OpenGL we have no
317
* idea how many mipmap levels will be in a texture until we start
318
* to render with it. Make an educated guess here but be prepared
319
* to re-allocating a texture buffer with space for more (or fewer)
320
* mipmap levels later.
285
322
if ((stObj->base.MinFilter == GL_NEAREST ||
286
323
stObj->base.MinFilter == GL_LINEAR ||
287
324
stImage->base._BaseFormat == GL_DEPTH_COMPONENT ||
288
325
stImage->base._BaseFormat == GL_DEPTH_STENCIL_EXT) &&
289
326
!stObj->base.GenerateMipmap &&
290
stImage->level == firstLevel) {
327
stImage->level == 0) {
291
328
/* only alloc space for a single mipmap level */
292
lastLevel = firstLevel;
295
332
/* alloc space for a full mipmap */
296
333
GLuint l2width = util_logbase2(width);
297
334
GLuint l2height = util_logbase2(height);
298
335
GLuint l2depth = util_logbase2(depth);
299
lastLevel = firstLevel + MAX2(MAX2(l2width, l2height), l2depth);
336
lastLevel = MAX2(MAX2(l2width, l2height), l2depth);
339
/* Save the level=0 dimensions */
340
stObj->width0 = width;
341
stObj->height0 = height;
342
stObj->depth0 = depth;
302
344
fmt = st_mesa_format_to_pipe_format(stImage->base.TexFormat);
304
usage = default_usage(fmt);
346
bindings = default_bindings(st, fmt);
306
348
stObj->pt = st_texture_create(st,
307
349
gl_target_to_pipe(stObj->base.Target),
403
451
/* Create the temporary source texture
405
453
memset(&templ, 0, sizeof(templ));
406
templ.target = PIPE_TEXTURE_2D;
454
templ.target = st->internal_target;
407
455
templ.format = st_mesa_format_to_pipe_format(mesa_format);
408
456
templ.width0 = width;
409
457
templ.height0 = height;
410
458
templ.depth0 = 1;
411
459
templ.last_level = 0;
412
templ.tex_usage = PIPE_TEXTURE_USAGE_SAMPLER;
413
src_tex = screen->texture_create(screen, &templ);
460
templ.usage = PIPE_USAGE_DEFAULT;
461
templ.bind = PIPE_BIND_SAMPLER_VIEW;
462
src_tex = screen->resource_create(screen, &templ);
418
467
/* Put user's tex data into the temporary texture
420
tex_xfer = st_cond_flush_get_tex_transfer(st_context(ctx), src_tex,
469
tex_xfer = pipe_get_transfer(st_context(ctx)->pipe, src_tex,
421
470
0, 0, 0, /* face, level are zero */
422
471
PIPE_TRANSFER_WRITE,
423
472
0, 0, width, height); /* x, y, w, h */
424
map = screen->transfer_map(screen, tex_xfer);
473
map = pipe_transfer_map(pipe, tex_xfer);
426
475
_mesa_texstore(ctx, 2, GL_RGBA, mesa_format,
427
476
map, /* dest ptr */
433
482
pixels, /* source data */
434
483
unpack); /* source data packing */
436
screen->transfer_unmap(screen, tex_xfer);
437
screen->tex_transfer_destroy(tex_xfer);
485
pipe_transfer_unmap(pipe, tex_xfer);
486
pipe->transfer_destroy(pipe, tex_xfer);
488
/* Create temporary sampler view */
489
u_sampler_view_default_template(&view_templ,
492
src_view = pipe->create_sampler_view(pipe, src_tex, &view_templ);
439
495
/* copy / compress image */
440
util_blit_pixels_tex(ctx->st->blit,
441
src_tex, /* pipe_texture (src) */
496
util_blit_pixels_tex(st->blit,
497
src_view, /* sampler view (src) */
442
498
0, 0, /* src x0, y0 */
443
499
width, height, /* src x1, y1 */
444
500
dst_surface, /* pipe_surface (dst) */
483
539
DBG("%s target %s level %d %dx%dx%d border %d\n", __FUNCTION__,
484
540
_mesa_lookup_enum_by_nr(target), level, width, height, depth, border);
542
/* The Mesa/Gallium state tracker does not implement the imaging extensions
543
* such as convolution.
545
assert(!ctx->Extensions.ARB_imaging);
546
assert(!ctx->Extensions.EXT_convolution);
486
548
/* switch to "normal" */
487
549
if (stObj->surface_based) {
488
550
_mesa_clear_texture_object(ctx, texObj);
551
pipe_resource_reference(&stObj->pt, NULL);
553
/* oops, need to init this image again */
554
_mesa_init_teximage_fields(ctx, target, texImage,
555
width, height, depth, border, internalFormat);
556
_mesa_choose_texture_format(ctx, texObj, texImage, target, level,
557
internalFormat, format, type);
489
559
stObj->surface_based = GL_FALSE;
499
569
texImage->Border = 0;
503
postConvWidth = width;
504
postConvHeight = height;
573
assert(texImage->Width == width);
574
assert(texImage->Height == height);
575
assert(texImage->Depth == depth);
506
578
stImage->face = _mesa_tex_target_to_face(target);
507
579
stImage->level = level;
510
if (ctx->_ImageTransferState & IMAGE_CONVOLUTION_BIT) {
511
_mesa_adjust_image_for_convolution(ctx, dims, &postConvWidth,
516
581
_mesa_set_fetch_functions(texImage, dims);
518
if (_mesa_is_format_compressed(texImage->TexFormat)) {
519
/* must be a compressed format */
523
texelBytes = _mesa_get_format_bytes(texImage->TexFormat);
525
/* Minimum pitch of 32 bytes */
526
if (postConvWidth * texelBytes < 32) {
527
postConvWidth = 32 / texelBytes;
528
texImage->RowStride = postConvWidth;
531
/* we'll set RowStride elsewhere when the texture is a "mapped" state */
532
/*assert(texImage->RowStride == postConvWidth);*/
535
583
/* Release the reference to a potentially orphaned buffer.
536
584
* Release any old malloced memory.
538
586
if (stImage->pt) {
539
pipe_texture_reference(&stImage->pt, NULL);
587
pipe_resource_reference(&stImage->pt, NULL);
540
588
assert(!texImage->Data);
542
590
else if (texImage->Data) {
582
627
assert(!stImage->pt);
629
/* Check if this texture image can live inside the texture object's buffer.
630
* If so, store the image there. Otherwise the image will temporarily live
585
634
st_texture_match_image(stObj->pt, &stImage->base,
586
stImage->face, stImage->level)) {
635
stImage->face, stImage->level)) {
588
pipe_texture_reference(&stImage->pt, stObj->pt);
637
pipe_resource_reference(&stImage->pt, stObj->pt);
589
638
assert(stImage->pt);
592
641
if (!stImage->pt)
593
642
DBG("XXX: Image did not fit into texture - storing in local memory!\n");
595
/* st_CopyTexImage calls this function with pixels == NULL, with
596
* the expectation that the texture will be set up but nothing
597
* more will be done. This is where those calls return:
644
/* Pixel data may come from regular user memory or a PBO. For the later,
645
* do bounds checking and map the PBO to read pixels data from it.
647
* XXX we should try to use a GPU-accelerated path to copy the image data
648
* from the PBO to the texture.
599
650
if (compressed_src) {
600
651
pixels = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, pixels,
680
* Prepare to store the texture data. Either map the gallium texture buffer
681
* memory or malloc space for it.
632
683
if (stImage->pt) {
684
/* Store the image in the gallium texture memory buffer */
633
685
if (format == GL_DEPTH_COMPONENT &&
634
686
util_format_is_depth_and_stencil(stImage->pt->format))
635
687
transfer_usage = PIPE_TRANSFER_READ_WRITE;
637
689
transfer_usage = PIPE_TRANSFER_WRITE;
639
texImage->Data = st_texture_image_map(ctx->st, stImage, 0,
640
transfer_usage, 0, 0,
642
stImage->base.Height);
691
texImage->Data = st_texture_image_map(st, stImage, 0,
692
transfer_usage, 0, 0, width, height);
643
693
if(stImage->transfer)
644
694
dstRowStride = stImage->transfer->stride;
647
697
/* Allocate regular memory and store the image there temporarily. */
648
if (_mesa_is_format_compressed(texImage->TexFormat)) {
649
sizeInBytes = _mesa_format_image_size(texImage->TexFormat,
653
dstRowStride = _mesa_format_row_stride(texImage->TexFormat, width);
657
dstRowStride = postConvWidth * texelBytes;
658
sizeInBytes = depth * dstRowStride * postConvHeight;
698
GLuint imageSize = _mesa_format_image_size(texImage->TexFormat,
699
width, height, depth);
700
dstRowStride = _mesa_format_row_stride(texImage->TexFormat, width);
661
texImage->Data = _mesa_align_malloc(sizeInBytes, 16);
702
texImage->Data = _mesa_align_malloc(imageSize, 16);
664
705
if (!texImage->Data) {
711
/* We've allocated texture memory, but have no pixel data - all done. */
672
715
DBG("Upload image %dx%dx%d row_len %x pitch %x\n",
673
width, height, depth, width * texelBytes, dstRowStride);
716
width, height, depth, width, dstRowStride);
675
/* Copy data. Would like to know when it's ok for us to eg. use
676
* the blitter to copy. Or, use the hardware to do the format
677
* conversion and copy:
718
/* Copy user texture image into the texture buffer.
679
720
if (compressed_src) {
680
const GLuint srcImageStride = _mesa_format_row_stride(texImage->TexFormat, width);
681
if(dstRowStride == srcImageStride)
721
const GLuint srcRowStride =
722
_mesa_format_row_stride(texImage->TexFormat, width);
723
if (dstRowStride == srcRowStride) {
682
724
memcpy(texImage->Data, pixels, imageSize);
685
727
char *dst = texImage->Data;
686
728
const char *src = pixels;
687
729
GLuint i, bw, bh, lines;
688
730
_mesa_get_format_block_size(texImage->TexFormat, &bw, &bh);
689
731
lines = (height + bh - 1) / bh;
691
for(i = 0; i < lines; ++i)
693
memcpy(dst, src, srcImageStride);
733
for (i = 0; i < lines; ++i) {
734
memcpy(dst, src, srcRowStride);
694
735
dst += dstRowStride;
695
src += srcImageStride;
809
849
struct gl_texture_object *texObj,
810
850
struct gl_texture_image *texImage)
812
struct pipe_screen *screen = ctx->st->pipe->screen;
852
struct st_context *st = st_context(ctx);
853
struct pipe_context *pipe = st->pipe;
854
struct pipe_screen *screen = pipe->screen;
813
855
struct st_texture_image *stImage = st_texture_image(texImage);
856
struct st_texture_object *stObj = st_texture_object(texObj);
857
struct pipe_sampler_view *src_view =
858
st_get_texture_sampler_view(stObj, pipe);
814
859
const GLuint width = texImage->Width;
815
860
const GLuint height = texImage->Height;
816
861
struct pipe_surface *dst_surface;
817
struct pipe_texture *dst_texture;
862
struct pipe_resource *dst_texture;
818
863
struct pipe_transfer *tex_xfer;
864
unsigned bind = (PIPE_BIND_RENDER_TARGET | /* util_blit may choose to render */
865
PIPE_BIND_TRANSFER_READ);
820
867
/* create temp / dest surface */
821
if (!util_create_rgba_surface(screen, width, height,
868
if (!util_create_rgba_surface(screen, width, height, bind,
822
869
&dst_texture, &dst_surface)) {
823
870
_mesa_problem(ctx, "util_create_rgba_surface() failed "
824
871
"in decompress_with_blit()");
1463
1499
const GLenum texBaseFormat = texImage->_BaseFormat;
1464
1500
struct gl_framebuffer *fb = ctx->ReadBuffer;
1465
1501
struct st_renderbuffer *strb;
1466
struct pipe_context *pipe = ctx->st->pipe;
1502
struct st_context *st = st_context(ctx);
1503
struct pipe_context *pipe = st->pipe;
1467
1504
struct pipe_screen *screen = pipe->screen;
1468
1505
enum pipe_format dest_format, src_format;
1469
1506
GLboolean use_fallback = GL_TRUE;
1470
1507
GLboolean matching_base_formats;
1471
GLuint format_writemask;
1508
GLuint format_writemask, sample_count;
1472
1509
struct pipe_surface *dest_surface = NULL;
1473
1510
GLboolean do_flip = (st_fb_orientation(ctx->ReadBuffer) == Y_0_TOP);
1475
/* any rendering in progress must flushed before we grab the fb image */
1476
st_flush(ctx->st, PIPE_FLUSH_RENDER_CACHE, NULL);
1478
1512
/* make sure finalize_textures has been called?
1480
if (0) st_validate_state(ctx->st);
1514
if (0) st_validate_state(st);
1482
1516
/* determine if copying depth or color data */
1483
1517
if (texBaseFormat == GL_DEPTH_COMPONENT ||
1545
1585
if (ctx->_ImageTransferState == 0x0) {
1547
if (pipe->surface_copy &&
1548
matching_base_formats &&
1587
if (matching_base_formats &&
1549
1588
src_format == dest_format &&
1552
1591
/* use surface_copy() / blit */
1554
dest_surface = screen->get_tex_surface(screen, stImage->pt,
1555
stImage->face, stImage->level,
1557
PIPE_BUFFER_USAGE_GPU_WRITE);
1559
/* for surface_copy(), y=0=top, always */
1560
pipe->surface_copy(pipe,
1592
struct pipe_subresource subdst, subsrc;
1593
subdst.face = stImage->face;
1594
subdst.level = stImage->level;
1595
subsrc.face = strb->surface->face;
1596
subsrc.level = strb->surface->level;
1598
/* for resource_copy_region(), y=0=top, always */
1599
pipe->resource_copy_region(pipe,
1603
destX, destY, destZ,
1607
srcX, srcY, strb->surface->zslice,
1569
1610
use_fallback = GL_FALSE;
1571
1612
else if (format_writemask &&
1572
1613
texBaseFormat != GL_DEPTH_COMPONENT &&
1573
1614
texBaseFormat != GL_DEPTH_STENCIL &&
1574
1615
screen->is_format_supported(screen, src_format,
1576
PIPE_TEXTURE_USAGE_SAMPLER,
1616
PIPE_TEXTURE_2D, sample_count,
1617
PIPE_BIND_SAMPLER_VIEW,
1578
1619
screen->is_format_supported(screen, dest_format,
1580
PIPE_TEXTURE_USAGE_RENDER_TARGET,
1621
PIPE_BIND_RENDER_TARGET,
1582
1623
/* draw textured quad to do the copy */
1583
1624
GLint srcY0, srcY1;
1625
struct pipe_subresource subsrc;
1585
1627
dest_surface = screen->get_tex_surface(screen, stImage->pt,
1586
1628
stImage->face, stImage->level,
1588
PIPE_BUFFER_USAGE_GPU_WRITE);
1630
PIPE_BIND_RENDER_TARGET);
1591
1633
srcY1 = strb->Base.Height - srcY - height;
1761
* Copy image data from stImage into the texture object 'stObj' at level
1714
1765
copy_image_data_to_texture(struct st_context *st,
1715
1766
struct st_texture_object *stObj,
1716
1767
GLuint dstLevel,
1717
1768
struct st_texture_image *stImage)
1772
const struct gl_texture_image *dstImage =
1773
stObj->base.Image[stImage->face][stImage->level];
1775
assert(dstImage->Width == stImage->base.Width);
1776
assert(dstImage->Height == stImage->base.Height);
1777
assert(dstImage->Depth == stImage->base.Depth);
1719
1780
if (stImage->pt) {
1720
1781
/* Copy potentially with the blitter:
1722
1783
st_texture_image_copy(st->pipe,
1723
1784
stObj->pt, dstLevel, /* dest texture, level */
1724
stImage->pt, /* src texture */
1785
stImage->pt, stImage->level, /* src texture, level */
1725
1786
stImage->face);
1727
pipe_texture_reference(&stImage->pt, NULL);
1788
pipe_resource_reference(&stImage->pt, NULL);
1729
1790
else if (stImage->base.Data) {
1730
/* More straightforward upload.
1732
st_teximage_flush_before_map(st, stObj->pt, stImage->face, dstLevel,
1733
PIPE_TRANSFER_WRITE);
1735
1791
st_texture_image_data(st,
1792
1848
if (firstImage->pt &&
1793
1849
firstImage->pt != stObj->pt &&
1794
1850
firstImage->pt->last_level >= stObj->lastLevel) {
1795
pipe_texture_reference(&stObj->pt, firstImage->pt);
1851
pipe_resource_reference(&stObj->pt, firstImage->pt);
1852
pipe_sampler_view_reference(&stObj->sampler_view, NULL);
1798
/* bytes per pixel block (blocks are usually 1x1) */
1799
blockSize = _mesa_get_format_bytes(firstImage->base.TexFormat);
1855
/* Find gallium format for the Mesa texture */
1856
firstImageFormat = st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
1801
1858
/* If we already have a gallium texture, check that it matches the texture
1802
1859
* object's format, target, size, num_levels, etc.
1804
1861
if (stObj->pt) {
1805
const enum pipe_format fmt =
1806
st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
1807
1862
if (stObj->pt->target != gl_target_to_pipe(stObj->base.Target) ||
1808
stObj->pt->format != fmt ||
1863
!st_sampler_compat_formats(stObj->pt->format, firstImageFormat) ||
1809
1864
stObj->pt->last_level < stObj->lastLevel ||
1810
stObj->pt->width0 != firstImage->base.Width2 ||
1811
stObj->pt->height0 != firstImage->base.Height2 ||
1812
stObj->pt->depth0 != firstImage->base.Depth2)
1865
stObj->pt->width0 != stObj->width0 ||
1866
stObj->pt->height0 != stObj->height0 ||
1867
stObj->pt->depth0 != stObj->depth0)
1814
pipe_texture_reference(&stObj->pt, NULL);
1815
ctx->st->dirty.st |= ST_NEW_FRAMEBUFFER;
1869
/* The gallium texture does not match the Mesa texture so delete the
1870
* gallium texture now. We'll make a new one below.
1872
pipe_resource_reference(&stObj->pt, NULL);
1873
pipe_sampler_view_reference(&stObj->sampler_view, NULL);
1874
st->dirty.st |= ST_NEW_FRAMEBUFFER;
1819
1878
/* May need to create a new gallium texture:
1821
1880
if (!stObj->pt) {
1822
const enum pipe_format fmt =
1823
st_mesa_format_to_pipe_format(firstImage->base.TexFormat);
1824
GLuint usage = default_usage(fmt);
1881
GLuint bindings = default_bindings(st, firstImageFormat);
1826
stObj->pt = st_texture_create(ctx->st,
1883
stObj->pt = st_texture_create(st,
1827
1884
gl_target_to_pipe(stObj->base.Target),
1829
1886
stObj->lastLevel,
1830
firstImage->base.Width2,
1831
firstImage->base.Height2,
1832
firstImage->base.Depth2,
1835
1892
if (!stObj->pt) {
1836
1893
_mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");