Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright (C) 2009 Francisco Jerez.
  3.  * All Rights Reserved.
  4.  *
  5.  * Permission is hereby granted, free of charge, to any person obtaining
  6.  * a copy of this software and associated documentation files (the
  7.  * "Software"), to deal in the Software without restriction, including
  8.  * without limitation the rights to use, copy, modify, merge, publish,
  9.  * distribute, sublicense, and/or sell copies of the Software, and to
  10.  * permit persons to whom the Software is furnished to do so, subject to
  11.  * the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice (including the
  14.  * next paragraph) shall be included in all copies or substantial
  15.  * portions of the Software.
  16.  *
  17.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18.  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19.  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20.  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  21.  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22.  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23.  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24.  *
  25.  */
  26.  
  27. #include "nouveau_driver.h"
  28. #include "nouveau_context.h"
  29. #include "nouveau_texture.h"
  30. #include "nouveau_fbo.h"
  31. #include "nouveau_util.h"
  32.  
  33. #include "main/pbo.h"
  34. #include "main/texobj.h"
  35. #include "main/texstore.h"
  36. #include "main/texformat.h"
  37. #include "main/texcompress.h"
  38. #include "main/texgetimage.h"
  39. #include "main/mipmap.h"
  40. #include "main/teximage.h"
  41. #include "drivers/common/meta.h"
  42. #include "swrast/s_texfetch.h"
  43.  
  44. static struct gl_texture_object *
  45. nouveau_texture_new(struct gl_context *ctx, GLuint name, GLenum target)
  46. {
  47.         struct nouveau_texture *nt = CALLOC_STRUCT(nouveau_texture);
  48.  
  49.         _mesa_initialize_texture_object(ctx, &nt->base, name, target);
  50.  
  51.         return &nt->base;
  52. }
  53.  
  54. static void
  55. nouveau_texture_free(struct gl_context *ctx, struct gl_texture_object *t)
  56. {
  57.         struct nouveau_texture *nt = to_nouveau_texture(t);
  58.         int i;
  59.  
  60.         for (i = 0; i < MAX_TEXTURE_LEVELS; i++)
  61.                 nouveau_surface_ref(NULL, &nt->surfaces[i]);
  62.  
  63.         _mesa_delete_texture_object(ctx, t);
  64. }
  65.  
  66. static struct gl_texture_image *
  67. nouveau_teximage_new(struct gl_context *ctx)
  68. {
  69.         struct nouveau_teximage *nti = CALLOC_STRUCT(nouveau_teximage);
  70.  
  71.         return &nti->base.Base;
  72. }
  73.  
  74. static void
  75. nouveau_teximage_free(struct gl_context *ctx, struct gl_texture_image *ti)
  76. {
  77.         struct nouveau_teximage *nti = to_nouveau_teximage(ti);
  78.  
  79.         nouveau_surface_ref(NULL, &nti->surface);
  80. }
  81.  
  82. static void
  83. nouveau_map_texture_image(struct gl_context *ctx,
  84.                           struct gl_texture_image *ti,
  85.                           GLuint slice,
  86.                           GLuint x, GLuint y, GLuint w, GLuint h,
  87.                           GLbitfield mode,
  88.                           GLubyte **map,
  89.                           GLint *stride)
  90. {
  91.         struct nouveau_teximage *nti = to_nouveau_teximage(ti);
  92.         struct nouveau_surface *s = &nti->surface;
  93.         struct nouveau_surface *st = &nti->transfer.surface;
  94.         struct nouveau_client *client = context_client(ctx);
  95.  
  96.         /* Nouveau has no support for 3D or cubemap textures. */
  97.         assert(slice == 0);
  98.  
  99.         if (s->bo) {
  100.                 if (!(mode & GL_MAP_READ_BIT) &&
  101.                     nouveau_pushbuf_refd(context_push(ctx), s->bo)) {
  102.                         unsigned size;
  103.                         /*
  104.                          * Heuristic: use a bounce buffer to pipeline
  105.                          * teximage transfers.
  106.                          */
  107.                         st->layout = LINEAR;
  108.                         st->format = s->format;
  109.                         st->cpp = s->cpp;
  110.                         st->width = w;
  111.                         st->height = h;
  112.                         st->pitch = s->pitch;
  113.                         nti->transfer.x = x;
  114.                         nti->transfer.y = y;
  115.  
  116.                         size = get_format_blocksy(st->format, h) * st->pitch;
  117.                         *map = nouveau_get_scratch(ctx, size,
  118.                                           &st->bo, &st->offset);
  119.                         *stride = st->pitch;
  120.                 } else {
  121.                         int ret, flags = 0;
  122.  
  123.                         if (mode & GL_MAP_READ_BIT)
  124.                                 flags |= NOUVEAU_BO_RD;
  125.                         if (mode & GL_MAP_WRITE_BIT)
  126.                                 flags |= NOUVEAU_BO_WR;
  127.  
  128.                         if (!s->bo->map) {
  129.                                 ret = nouveau_bo_map(s->bo, flags, client);
  130.                                 assert(!ret);
  131.                         }
  132.  
  133.                         *map = s->bo->map +
  134.                                 get_format_blocksy(s->format, y) * s->pitch +
  135.                                 get_format_blocksx(s->format, x) * s->cpp;
  136.                         *stride = s->pitch;
  137.                 }
  138.         } else {
  139.                 *map = nti->base.Buffer +
  140.                         get_format_blocksy(s->format, y) * s->pitch +
  141.                         get_format_blocksx(s->format, x) * s->cpp;
  142.                 *stride = s->pitch;
  143.         }
  144. }
  145.  
  146. static void
  147. nouveau_unmap_texture_image(struct gl_context *ctx, struct gl_texture_image *ti,
  148.                             GLuint slice)
  149. {
  150.         struct nouveau_teximage *nti = to_nouveau_teximage(ti);
  151.         struct nouveau_surface *s = &nti->surface;
  152.         struct nouveau_surface *st = &nti->transfer.surface;
  153.  
  154.         if (st->bo) {
  155.                 context_drv(ctx)->surface_copy(ctx, s, st, nti->transfer.x,
  156.                                                nti->transfer.y, 0, 0,
  157.                                                st->width, st->height);
  158.                 nouveau_surface_ref(NULL, st);
  159.  
  160.         }
  161. }
  162.  
  163. static gl_format
  164. nouveau_choose_tex_format(struct gl_context *ctx, GLenum target,
  165.                           GLint internalFormat,
  166.                           GLenum srcFormat, GLenum srcType)
  167. {
  168.         switch (internalFormat) {
  169.         case 4:
  170.         case GL_RGBA:
  171.         case GL_RGBA2:
  172.         case GL_RGBA4:
  173.         case GL_RGBA8:
  174.         case GL_RGBA12:
  175.         case GL_RGBA16:
  176.         case GL_RGB10_A2:
  177.         case GL_COMPRESSED_RGBA:
  178.                 return MESA_FORMAT_ARGB8888;
  179.         case GL_RGB5_A1:
  180.                 return MESA_FORMAT_ARGB1555;
  181.  
  182.         case GL_RGB:
  183.         case GL_RGB8:
  184.         case GL_RGB10:
  185.         case GL_RGB12:
  186.         case GL_RGB16:
  187.         case GL_COMPRESSED_RGB:
  188.                 return MESA_FORMAT_XRGB8888;
  189.         case 3:
  190.         case GL_R3_G3_B2:
  191.         case GL_RGB4:
  192.         case GL_RGB5:
  193.                 return MESA_FORMAT_RGB565;
  194.  
  195.         case 2:
  196.         case GL_LUMINANCE_ALPHA:
  197.         case GL_LUMINANCE4_ALPHA4:
  198.         case GL_LUMINANCE6_ALPHA2:
  199.         case GL_LUMINANCE12_ALPHA4:
  200.         case GL_LUMINANCE12_ALPHA12:
  201.         case GL_LUMINANCE16_ALPHA16:
  202.         case GL_LUMINANCE8_ALPHA8:
  203.         case GL_COMPRESSED_LUMINANCE_ALPHA:
  204.                 return MESA_FORMAT_ARGB8888;
  205.  
  206.         case 1:
  207.         case GL_LUMINANCE:
  208.         case GL_LUMINANCE4:
  209.         case GL_LUMINANCE12:
  210.         case GL_LUMINANCE16:
  211.         case GL_LUMINANCE8:
  212.         case GL_COMPRESSED_LUMINANCE:
  213.                 return MESA_FORMAT_L8;
  214.  
  215.         case GL_ALPHA:
  216.         case GL_ALPHA4:
  217.         case GL_ALPHA12:
  218.         case GL_ALPHA16:
  219.         case GL_ALPHA8:
  220.         case GL_COMPRESSED_ALPHA:
  221.                 return MESA_FORMAT_A8;
  222.  
  223.         case GL_INTENSITY:
  224.         case GL_INTENSITY4:
  225.         case GL_INTENSITY12:
  226.         case GL_INTENSITY16:
  227.         case GL_INTENSITY8:
  228.                 return MESA_FORMAT_I8;
  229.  
  230.         case GL_RGB_S3TC:
  231.         case GL_RGB4_S3TC:
  232.         case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
  233.                 return MESA_FORMAT_RGB_DXT1;
  234.  
  235.         case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
  236.                 return MESA_FORMAT_RGBA_DXT1;
  237.  
  238.         case GL_RGBA_S3TC:
  239.         case GL_RGBA4_S3TC:
  240.         case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
  241.                 return MESA_FORMAT_RGBA_DXT3;
  242.  
  243.         case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
  244.                 return MESA_FORMAT_RGBA_DXT5;
  245.  
  246.         default:
  247.                 assert(0);
  248.         }
  249. }
  250.  
  251. static GLboolean
  252. teximage_fits(struct gl_texture_object *t, int level)
  253. {
  254.         struct nouveau_surface *s = &to_nouveau_texture(t)->surfaces[level];
  255.         struct gl_texture_image *ti = t->Image[0][level];
  256.  
  257.         if (!ti || !to_nouveau_teximage(ti)->surface.bo)
  258.                 return GL_FALSE;
  259.  
  260.         if (level == t->BaseLevel && (s->offset & 0x7f))
  261.                 return GL_FALSE;
  262.  
  263.         return t->Target == GL_TEXTURE_RECTANGLE ||
  264.                 (s->bo && s->format == ti->TexFormat &&
  265.                  s->width == ti->Width && s->height == ti->Height);
  266. }
  267.  
  268. static GLboolean
  269. validate_teximage(struct gl_context *ctx, struct gl_texture_object *t,
  270.                   int level, int x, int y, int z,
  271.                   int width, int height, int depth)
  272. {
  273.         struct gl_texture_image *ti = t->Image[0][level];
  274.  
  275.         if (teximage_fits(t, level)) {
  276.                 struct nouveau_surface *ss = to_nouveau_texture(t)->surfaces;
  277.                 struct nouveau_surface *s = &to_nouveau_teximage(ti)->surface;
  278.  
  279.                 if (t->Target == GL_TEXTURE_RECTANGLE)
  280.                         nouveau_surface_ref(s, &ss[level]);
  281.                 else
  282.                         context_drv(ctx)->surface_copy(ctx, &ss[level], s,
  283.                                                        x, y, x, y,
  284.                                                        width, height);
  285.  
  286.                 return GL_TRUE;
  287.         }
  288.  
  289.         return GL_FALSE;
  290. }
  291.  
  292. static int
  293. get_last_level(struct gl_texture_object *t)
  294. {
  295.         struct gl_texture_image *base = t->Image[0][t->BaseLevel];
  296.  
  297.         if (t->Sampler.MinFilter == GL_NEAREST ||
  298.             t->Sampler.MinFilter == GL_LINEAR || !base)
  299.                 return t->BaseLevel;
  300.         else
  301.                 return MIN2(t->BaseLevel + base->MaxNumLevels - 1, t->MaxLevel);
  302. }
  303.  
  304. static void
  305. relayout_texture(struct gl_context *ctx, struct gl_texture_object *t)
  306. {
  307.         struct gl_texture_image *base = t->Image[0][t->BaseLevel];
  308.  
  309.         if (base && t->Target != GL_TEXTURE_RECTANGLE) {
  310.                 struct nouveau_surface *ss = to_nouveau_texture(t)->surfaces;
  311.                 struct nouveau_surface *s = &to_nouveau_teximage(base)->surface;
  312.                 int i, ret, last = get_last_level(t);
  313.                 enum nouveau_surface_layout layout =
  314.                         (_mesa_is_format_compressed(s->format) ? LINEAR : SWIZZLED);
  315.                 unsigned size, pitch, offset = 0,
  316.                         width = s->width,
  317.                         height = s->height;
  318.  
  319.                 /* Deallocate the old storage. */
  320.                 for (i = 0; i < MAX_TEXTURE_LEVELS; i++)
  321.                         nouveau_bo_ref(NULL, &ss[i].bo);
  322.  
  323.                 /* Relayout the mipmap tree. */
  324.                 for (i = t->BaseLevel; i <= last; i++) {
  325.                         pitch = _mesa_format_row_stride(s->format, width);
  326.                         size = get_format_blocksy(s->format, height) * pitch;
  327.  
  328.                         /* Images larger than 16B have to be aligned. */
  329.                         if (size > 16)
  330.                                 offset = align(offset, 64);
  331.  
  332.                         ss[i] = (struct nouveau_surface) {
  333.                                 .offset = offset,
  334.                                 .layout = layout,
  335.                                 .format = s->format,
  336.                                 .width = width,
  337.                                 .height = height,
  338.                                 .cpp = s->cpp,
  339.                                 .pitch = pitch,
  340.                         };
  341.  
  342.                         offset += size;
  343.                         width = minify(width, 1);
  344.                         height = minify(height, 1);
  345.                 }
  346.  
  347.                 /* Get new storage. */
  348.                 size = align(offset, 64);
  349.  
  350.                 ret = nouveau_bo_new(context_dev(ctx), NOUVEAU_BO_MAP |
  351.                                      NOUVEAU_BO_GART | NOUVEAU_BO_VRAM,
  352.                                      0, size, NULL, &ss[last].bo);
  353.                 assert(!ret);
  354.  
  355.                 for (i = t->BaseLevel; i < last; i++)
  356.                         nouveau_bo_ref(ss[last].bo, &ss[i].bo);
  357.         }
  358. }
  359.  
  360. GLboolean
  361. nouveau_texture_validate(struct gl_context *ctx, struct gl_texture_object *t)
  362. {
  363.         struct nouveau_texture *nt = to_nouveau_texture(t);
  364.         int i, last = get_last_level(t);
  365.  
  366.         if (!teximage_fits(t, t->BaseLevel) ||
  367.             !teximage_fits(t, last))
  368.                 return GL_FALSE;
  369.  
  370.         if (nt->dirty) {
  371.                 nt->dirty = GL_FALSE;
  372.  
  373.                 /* Copy the teximages to the actual miptree. */
  374.                 for (i = t->BaseLevel; i <= last; i++) {
  375.                         struct nouveau_surface *s = &nt->surfaces[i];
  376.  
  377.                         validate_teximage(ctx, t, i, 0, 0, 0,
  378.                                           s->width, s->height, 1);
  379.                 }
  380.  
  381.                 PUSH_KICK(context_push(ctx));
  382.         }
  383.  
  384.         return GL_TRUE;
  385. }
  386.  
  387. void
  388. nouveau_texture_reallocate(struct gl_context *ctx, struct gl_texture_object *t)
  389. {
  390.         if (!teximage_fits(t, t->BaseLevel) ||
  391.             !teximage_fits(t, get_last_level(t))) {
  392.                 texture_dirty(t);
  393.                 relayout_texture(ctx, t);
  394.                 nouveau_texture_validate(ctx, t);
  395.         }
  396. }
  397.  
  398. static unsigned
  399. get_teximage_placement(struct gl_texture_image *ti)
  400. {
  401.         if (ti->TexFormat == MESA_FORMAT_A8 ||
  402.             ti->TexFormat == MESA_FORMAT_L8 ||
  403.             ti->TexFormat == MESA_FORMAT_I8)
  404.                 /* 1 cpp formats will have to be swizzled by the CPU,
  405.                  * so leave them in system RAM for now. */
  406.                 return NOUVEAU_BO_MAP;
  407.         else
  408.                 return NOUVEAU_BO_GART | NOUVEAU_BO_MAP;
  409. }
  410.  
  411. static void
  412. nouveau_teximage(struct gl_context *ctx, GLint dims,
  413.                  struct gl_texture_image *ti,
  414.                  GLsizei imageSize,
  415.                  GLenum format, GLenum type, const GLvoid *pixels,
  416.                  const struct gl_pixelstore_attrib *packing,
  417.                  GLboolean compressed)
  418. {
  419.         struct gl_texture_object *t = ti->TexObject;
  420.         const GLuint level = ti->Level;
  421.         struct nouveau_surface *s = &to_nouveau_teximage(ti)->surface;
  422.         struct nouveau_teximage *nti = to_nouveau_teximage(ti);
  423.         int ret;
  424.         GLuint depth = compressed ? 1 : ti->Depth;
  425.  
  426.         /* Allocate a new bo for the image. */
  427.         nouveau_surface_alloc(ctx, s, LINEAR, get_teximage_placement(ti),
  428.                               ti->TexFormat, ti->Width, ti->Height);
  429.         nti->base.RowStride = s->pitch / s->cpp;
  430.  
  431.         if (compressed)
  432.                 pixels = _mesa_validate_pbo_compressed_teximage(ctx,
  433.                         dims, imageSize,
  434.                         pixels, packing, "glCompressedTexImage");
  435.         else
  436.                 pixels = _mesa_validate_pbo_teximage(ctx,
  437.                         dims, ti->Width, ti->Height, depth, format, type,
  438.                         pixels, packing, "glTexImage");
  439.  
  440.         if (pixels) {
  441.                 GLubyte *map;
  442.                 int row_stride;
  443.  
  444.                 /* Store the pixel data. */
  445.                 nouveau_map_texture_image(ctx, ti, 0,
  446.                                           0, 0, ti->Width, ti->Height,
  447.                                           GL_MAP_WRITE_BIT,
  448.                                           &map, &row_stride);
  449.  
  450.                 ret = _mesa_texstore(ctx, dims, ti->_BaseFormat,
  451.                                      ti->TexFormat,
  452.                                      row_stride,
  453.                                      &map,
  454.                                      ti->Width, ti->Height, depth,
  455.                                      format, type, pixels, packing);
  456.                 assert(ret);
  457.  
  458.                 nouveau_unmap_texture_image(ctx, ti, 0);
  459.                 _mesa_unmap_teximage_pbo(ctx, packing);
  460.  
  461.                 if (!validate_teximage(ctx, t, level, 0, 0, 0,
  462.                                        ti->Width, ti->Height, depth))
  463.                         /* It doesn't fit, mark it as dirty. */
  464.                         texture_dirty(t);
  465.         }
  466.  
  467.         if (level == t->BaseLevel) {
  468.                 if (!teximage_fits(t, level))
  469.                         relayout_texture(ctx, t);
  470.                 nouveau_texture_validate(ctx, t);
  471.         }
  472.  
  473.         context_dirty_i(ctx, TEX_OBJ, ctx->Texture.CurrentUnit);
  474.         context_dirty_i(ctx, TEX_ENV, ctx->Texture.CurrentUnit);
  475. }
  476.  
  477.  
  478. static void
  479. nouveau_teximage_123d(struct gl_context *ctx, GLuint dims,
  480.                       struct gl_texture_image *ti,
  481.                       GLenum format, GLenum type, const GLvoid *pixels,
  482.                       const struct gl_pixelstore_attrib *packing)
  483. {
  484.         nouveau_teximage(ctx, dims, ti, 0, format, type, pixels,
  485.                          packing, GL_FALSE);
  486. }
  487.  
  488. static void
  489. nouveau_compressed_teximage(struct gl_context *ctx, GLuint dims,
  490.                     struct gl_texture_image *ti,
  491.                     GLsizei imageSize, const GLvoid *data)
  492. {
  493.         nouveau_teximage(ctx, 2, ti, imageSize, 0, 0, data,
  494.                          &ctx->Unpack, GL_TRUE);
  495. }
  496.  
  497. static void
  498. nouveau_texsubimage(struct gl_context *ctx, GLint dims,
  499.                     struct gl_texture_image *ti,
  500.                     GLint xoffset, GLint yoffset, GLint zoffset,
  501.                     GLint width, GLint height, GLint depth,
  502.                     GLsizei imageSize,
  503.                     GLenum format, GLenum type, const void *pixels,
  504.                     const struct gl_pixelstore_attrib *packing,
  505.                     GLboolean compressed)
  506. {
  507.         int ret;
  508.  
  509.         if (compressed)
  510.                 pixels = _mesa_validate_pbo_compressed_teximage(ctx,
  511.                                 dims, imageSize,
  512.                                 pixels, packing, "glCompressedTexSubImage");
  513.         else
  514.                 pixels = _mesa_validate_pbo_teximage(ctx,
  515.                                 dims, width, height, depth, format, type,
  516.                                 pixels, packing, "glTexSubImage");
  517.  
  518.         if (pixels) {
  519.                 GLubyte *map;
  520.                 int row_stride;
  521.  
  522.                 nouveau_map_texture_image(ctx, ti, 0,
  523.                                           xoffset, yoffset, width, height,
  524.                                           GL_MAP_WRITE_BIT, &map, &row_stride);
  525.  
  526.                 ret = _mesa_texstore(ctx, dims, ti->_BaseFormat, ti->TexFormat,
  527.                                      row_stride, &map,
  528.                                      width, height, depth,
  529.                                      format, type, pixels, packing);
  530.                 assert(ret);
  531.  
  532.                 nouveau_unmap_texture_image(ctx, ti, 0);
  533.                 _mesa_unmap_teximage_pbo(ctx, packing);
  534.         }
  535.  
  536.         if (!to_nouveau_texture(ti->TexObject)->dirty)
  537.                 validate_teximage(ctx, ti->TexObject, ti->Level,
  538.                                   xoffset, yoffset, zoffset,
  539.                                   width, height, depth);
  540. }
  541.  
  542. static void
  543. nouveau_texsubimage_123d(struct gl_context *ctx, GLuint dims,
  544.                          struct gl_texture_image *ti,
  545.                          GLint xoffset, GLint yoffset, GLint zoffset,
  546.                          GLint width, GLint height, GLint depth,
  547.                          GLenum format, GLenum type, const void *pixels,
  548.                          const struct gl_pixelstore_attrib *packing)
  549. {
  550.         nouveau_texsubimage(ctx, dims, ti, xoffset, yoffset, zoffset,
  551.                             width, height, depth, 0, format, type, pixels,
  552.                             packing, GL_FALSE);
  553. }
  554.  
  555. static void
  556. nouveau_compressed_texsubimage(struct gl_context *ctx, GLuint dims,
  557.                        struct gl_texture_image *ti,
  558.                        GLint xoffset, GLint yoffset, GLint zoffset,
  559.                        GLsizei width, GLint height, GLint depth,
  560.                        GLenum format,
  561.                        GLint imageSize, const void *data)
  562. {
  563.         nouveau_texsubimage(ctx, dims, ti, xoffset, yoffset, zoffset,
  564.                           width, height, depth, imageSize, format, 0, data,
  565.                           &ctx->Unpack, GL_TRUE);
  566. }
  567.  
  568. static void
  569. nouveau_bind_texture(struct gl_context *ctx, GLenum target,
  570.                      struct gl_texture_object *t)
  571. {
  572.         context_dirty_i(ctx, TEX_OBJ, ctx->Texture.CurrentUnit);
  573.         context_dirty_i(ctx, TEX_ENV, ctx->Texture.CurrentUnit);
  574. }
  575.  
  576. static gl_format
  577. get_texbuffer_format(struct gl_renderbuffer *rb, GLint format)
  578. {
  579.         struct nouveau_surface *s = &to_nouveau_renderbuffer(rb)->surface;
  580.  
  581.         if (s->cpp < 4)
  582.                 return s->format;
  583.         else if (format == __DRI_TEXTURE_FORMAT_RGBA)
  584.                 return MESA_FORMAT_ARGB8888;
  585.         else
  586.                 return MESA_FORMAT_XRGB8888;
  587. }
  588.  
  589. void
  590. nouveau_set_texbuffer(__DRIcontext *dri_ctx,
  591.                       GLint target, GLint format,
  592.                       __DRIdrawable *draw)
  593. {
  594.         struct nouveau_context *nctx = dri_ctx->driverPrivate;
  595.         struct gl_context *ctx = &nctx->base;
  596.         struct gl_framebuffer *fb = draw->driverPrivate;
  597.         struct gl_renderbuffer *rb =
  598.                 fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer;
  599.         struct gl_texture_object *t = _mesa_get_current_tex_object(ctx, target);
  600.         struct gl_texture_image *ti;
  601.         struct nouveau_teximage *nti;
  602.         struct nouveau_surface *s;
  603.  
  604.         _mesa_lock_texture(ctx, t);
  605.         ti = _mesa_get_tex_image(ctx, t, target, 0);
  606.         nti = to_nouveau_teximage(ti);
  607.         s = &to_nouveau_teximage(ti)->surface;
  608.  
  609.         /* Update the texture surface with the given drawable. */
  610.         nouveau_update_renderbuffers(dri_ctx, draw);
  611.         nouveau_surface_ref(&to_nouveau_renderbuffer(rb)->surface, s);
  612.  
  613.         s->format = get_texbuffer_format(rb, format);
  614.  
  615.         /* Update the image fields. */
  616.         _mesa_init_teximage_fields(ctx, ti, s->width, s->height,
  617.                                    1, 0, s->cpp, s->format);
  618.         nti->base.RowStride = s->pitch / s->cpp;
  619.  
  620.         /* Try to validate it. */
  621.         if (!validate_teximage(ctx, t, 0, 0, 0, 0, s->width, s->height, 1))
  622.                 nouveau_texture_reallocate(ctx, t);
  623.  
  624.         context_dirty_i(ctx, TEX_OBJ, ctx->Texture.CurrentUnit);
  625.         context_dirty_i(ctx, TEX_ENV, ctx->Texture.CurrentUnit);
  626.  
  627.         _mesa_unlock_texture(ctx, t);
  628. }
  629.  
  630. void
  631. nouveau_texture_functions_init(struct dd_function_table *functions)
  632. {
  633.         functions->NewTextureObject = nouveau_texture_new;
  634.         functions->DeleteTexture = nouveau_texture_free;
  635.         functions->NewTextureImage = nouveau_teximage_new;
  636.         functions->FreeTextureImageBuffer = nouveau_teximage_free;
  637.         functions->ChooseTextureFormat = nouveau_choose_tex_format;
  638.         functions->TexImage = nouveau_teximage_123d;
  639.         functions->TexSubImage = nouveau_texsubimage_123d;
  640.         functions->CompressedTexImage = nouveau_compressed_teximage;
  641.         functions->CompressedTexSubImage = nouveau_compressed_texsubimage;
  642.         functions->BindTexture = nouveau_bind_texture;
  643.         functions->MapTextureImage = nouveau_map_texture_image;
  644.         functions->UnmapTextureImage = nouveau_unmap_texture_image;
  645. }
  646.