texstore.c revision 485105ed182e2e997b084f047e72d5a2c3460057
18e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul/*
28e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul * Mesa 3-D graphics library
3a4bec69e7271eda0137874973aa8c7d44175fedfBrian Paul * Version:  7.5
48e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul *
5501338d70e96e0388fd5198625d856c4ec07745fBrian Paul * Copyright (C) 1999-2008  Brian Paul   All Rights Reserved.
6a4bec69e7271eda0137874973aa8c7d44175fedfBrian Paul * Copyright (c) 2008-2009  VMware, Inc.
78e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul *
88e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul * Permission is hereby granted, free of charge, to any person obtaining a
98e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul * copy of this software and associated documentation files (the "Software"),
108e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul * to deal in the Software without restriction, including without limitation
118e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul * the rights to use, copy, modify, merge, publish, distribute, sublicense,
128e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul * and/or sell copies of the Software, and to permit persons to whom the
138e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul * Software is furnished to do so, subject to the following conditions:
148e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul *
158e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul * The above copyright notice and this permission notice shall be included
168e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul * in all copies or substantial portions of the Software.
178e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul *
188e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
198e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
208e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
218e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
228e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
238e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
248e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul */
258e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
268e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul/*
278e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul * Authors:
288e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul *   Brian Paul
298e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul */
308e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
31248200737398a7d6403a23930a6c9d93db06b942Brian Paul/**
3289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * The GL texture image functions in teximage.c basically just do
3389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * error checking and data structure allocation.  They in turn call
3489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * device driver functions which actually copy/convert/store the user's
3589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * texture image data.
3689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul *
3789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * However, most device drivers will be able to use the fallback functions
3889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * in this file.  That is, most drivers will have the following bit of
3989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * code:
4089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul *   ctx->Driver.TexImage1D = _mesa_store_teximage1d;
4189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul *   ctx->Driver.TexImage2D = _mesa_store_teximage2d;
4289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul *   ctx->Driver.TexImage3D = _mesa_store_teximage3d;
4389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul *   etc...
4489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul *
4589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * Texture image processing is actually kind of complicated.  We have to do:
4689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul *    Format/type conversions
4789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul *    pixel unpacking
4889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul *    pixel transfer (scale, bais, lookup, convolution!, etc)
4989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul *
5089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * These functions can handle most everything, including processing full
5189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * images and sub-images.
5289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul */
5389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
5489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
553c63452e64df7e10aa073c6c3b9492b1d7dabbb8Brian Paul#include "glheader.h"
567a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul#include "bufferobj.h"
57e75d2424e53d6023f4414e40694cd467e5392b96Brian Paul#include "colormac.h"
588e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul#include "context.h"
598e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul#include "convolve.h"
608e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul#include "image.h"
618e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul#include "macros.h"
6224edd9015951dd41898902b6c3973fe605e5871aBrian Paul#include "mipmap.h"
633c63452e64df7e10aa073c6c3b9492b1d7dabbb8Brian Paul#include "imports.h"
6489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul#include "texcompress.h"
65371ef9c058b0d59bfb62689b64af1b29a2214d9eGareth Hughes#include "texformat.h"
668e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul#include "teximage.h"
678e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul#include "texstore.h"
682e5c686c2b6f356895f33b2815e41386946ab55aKeith Whitwell#include "enums.h"
698e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
70248200737398a7d6403a23930a6c9d93db06b942Brian Paul
71fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwellenum {
72fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   ZERO = 4,
73fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   ONE = 5
74fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell};
7571699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell
76248200737398a7d6403a23930a6c9d93db06b942Brian Paul
77248200737398a7d6403a23930a6c9d93db06b942Brian Paul/**
78248200737398a7d6403a23930a6c9d93db06b942Brian Paul * Return GL_TRUE if the given image format is one that be converted
79248200737398a7d6403a23930a6c9d93db06b942Brian Paul * to another format by swizzling.
80248200737398a7d6403a23930a6c9d93db06b942Brian Paul */
81248200737398a7d6403a23930a6c9d93db06b942Brian Paulstatic GLboolean
82248200737398a7d6403a23930a6c9d93db06b942Brian Paulcan_swizzle(GLenum logicalBaseFormat)
8371699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell{
8471699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   switch (logicalBaseFormat) {
8571699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   case GL_RGBA:
8671699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   case GL_RGB:
8771699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   case GL_LUMINANCE_ALPHA:
8871699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   case GL_INTENSITY:
8971699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   case GL_ALPHA:
9071699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   case GL_LUMINANCE:
913aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   case GL_RED:
923aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   case GL_GREEN:
933aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   case GL_BLUE:
943aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   case GL_BGR:
953aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   case GL_BGRA:
963aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   case GL_ABGR_EXT:
9771699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell      return GL_TRUE;
9871699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   default:
9971699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell      return GL_FALSE;
10071699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   }
10171699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell}
10271699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell
1033aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell
1043aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell
105fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwellenum {
106fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   IDX_LUMINANCE = 0,
107fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   IDX_ALPHA,
108fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   IDX_INTENSITY,
109fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   IDX_LUMINANCE_ALPHA,
110fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   IDX_RGB,
111fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   IDX_RGBA,
1123aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   IDX_RED,
1133aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   IDX_GREEN,
1143aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   IDX_BLUE,
1153aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   IDX_BGR,
1163aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   IDX_BGRA,
1173aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   IDX_ABGR,
118fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   MAX_IDX
119fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell};
120fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell
1213aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell#define MAP1(x)       MAP4(x, ZERO, ZERO, ZERO)
1223aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell#define MAP2(x,y)     MAP4(x, y, ZERO, ZERO)
1233aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell#define MAP3(x,y,z)   MAP4(x, y, z, ZERO)
1243aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell#define MAP4(x,y,z,w) { x, y, z, w, ZERO, ONE }
125fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell
126fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell
1272e5c686c2b6f356895f33b2815e41386946ab55aKeith Whitwellstatic const struct {
1283aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   GLubyte format_idx;
1293aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   GLubyte to_rgba[6];
1303aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   GLubyte from_rgba[6];
1313aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell} mappings[MAX_IDX] =
132fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell{
133fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   {
1343aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      IDX_LUMINANCE,
1353aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      MAP4(0,0,0,ONE),
1363aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      MAP1(0)
137fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   },
138fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell
139fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   {
1403aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      IDX_ALPHA,
1413aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      MAP4(ZERO, ZERO, ZERO, 0),
1423aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      MAP1(3)
143fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   },
144fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell
145fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   {
1463aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      IDX_INTENSITY,
1473aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      MAP4(0, 0, 0, 0),
1483aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      MAP1(0),
149fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   },
150fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell
151fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   {
1523aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      IDX_LUMINANCE_ALPHA,
1533aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      MAP4(0,0,0,1),
1543aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      MAP2(0,3)
155fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   },
156fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell
157fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   {
1583aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      IDX_RGB,
1593aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      MAP4(0,1,2,ONE),
1603aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      MAP3(0,1,2)
161fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   },
162fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell
163fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   {
1643aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      IDX_RGBA,
1653aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      MAP4(0,1,2,3),
1663aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      MAP4(0,1,2,3),
1673aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   },
1683aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell
1693aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell
1703aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   {
1713aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      IDX_RED,
1723aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      MAP4(0, ZERO, ZERO, ONE),
1733aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      MAP1(0),
1743aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   },
1753aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell
1763aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   {
1773aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      IDX_GREEN,
1783aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      MAP4(ZERO, 0, ZERO, ONE),
1793aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      MAP1(1),
1803aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   },
1813aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell
1823aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   {
1833aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      IDX_BLUE,
1843aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      MAP4(ZERO, ZERO, 0, ONE),
1853aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      MAP1(2),
1863aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   },
1873aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell
1883aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   {
1893aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      IDX_BGR,
1903aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      MAP4(2,1,0,ONE),
1913aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      MAP3(2,1,0)
1923aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   },
1933aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell
1943aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   {
1953aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      IDX_BGRA,
1963aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      MAP4(2,1,0,3),
1973aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      MAP4(2,1,0,3)
1983aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   },
1993aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell
2003aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   {
2013aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      IDX_ABGR,
2023aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      MAP4(3,2,1,0),
2033aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      MAP4(3,2,1,0)
2043aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   },
205fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell};
206fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell
207fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell
208fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell
209248200737398a7d6403a23930a6c9d93db06b942Brian Paul/**
210248200737398a7d6403a23930a6c9d93db06b942Brian Paul * Convert a GL image format enum to an IDX_* value (see above).
211248200737398a7d6403a23930a6c9d93db06b942Brian Paul */
212248200737398a7d6403a23930a6c9d93db06b942Brian Paulstatic int
213248200737398a7d6403a23930a6c9d93db06b942Brian Paulget_map_idx(GLenum value)
214fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell{
215fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   switch (value) {
216fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   case GL_LUMINANCE: return IDX_LUMINANCE;
217fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   case GL_ALPHA: return IDX_ALPHA;
218fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   case GL_INTENSITY: return IDX_INTENSITY;
219fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   case GL_LUMINANCE_ALPHA: return IDX_LUMINANCE_ALPHA;
220fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   case GL_RGB: return IDX_RGB;
221fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   case GL_RGBA: return IDX_RGBA;
2223aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   case GL_RED: return IDX_RED;
2233aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   case GL_GREEN: return IDX_GREEN;
2243aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   case GL_BLUE: return IDX_BLUE;
2253aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   case GL_BGR: return IDX_BGR;
2263aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   case GL_BGRA: return IDX_BGRA;
2273aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   case GL_ABGR_EXT: return IDX_ABGR;
228fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   default:
229fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell      _mesa_problem(NULL, "Unexpected inFormat");
230fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell      return 0;
231fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell   }
232fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell}
233fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell
234f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
235f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul/**
236f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * When promoting texture formats (see below) we need to compute the
237f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * mapping of dest components back to source components.
238f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * This function does that.
239fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell * \param inFormat  the incoming format of the texture
240fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell * \param outFormat  the final texture format
241fce0d13b4fff04de50fd91a8f4dfdc248b6262e0Keith Whitwell * \return map[6]  a full 6-component map
242f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul */
2433aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwellstatic void
2443aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwellcompute_component_mapping(GLenum inFormat, GLenum outFormat,
2453aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell			  GLubyte *map)
246f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul{
247248200737398a7d6403a23930a6c9d93db06b942Brian Paul   const int inFmt = get_map_idx(inFormat);
248248200737398a7d6403a23930a6c9d93db06b942Brian Paul   const int outFmt = get_map_idx(outFormat);
249248200737398a7d6403a23930a6c9d93db06b942Brian Paul   const GLubyte *in2rgba = mappings[inFmt].to_rgba;
250248200737398a7d6403a23930a6c9d93db06b942Brian Paul   const GLubyte *rgba2out = mappings[outFmt].from_rgba;
2513aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   int i;
2522e5c686c2b6f356895f33b2815e41386946ab55aKeith Whitwell
2533aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   for (i = 0; i < 4; i++)
2543aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      map[i] = in2rgba[rgba2out[i]];
2553aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell
2563aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   map[ZERO] = ZERO;
2573aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   map[ONE] = ONE;
2583aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell
2593aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell/*
2603aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   _mesa_printf("from %x/%s to %x/%s map %d %d %d %d %d %d\n",
2613aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell		inFormat, _mesa_lookup_enum_by_nr(inFormat),
2623aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell		outFormat, _mesa_lookup_enum_by_nr(outFormat),
2633aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell		map[0],
2643aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell		map[1],
2653aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell		map[2],
2663aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell		map[3],
2673aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell		map[4],
2683aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell		map[5]);
2693aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell*/
270f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul}
271f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
272f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
27340d1a40f294f1ed2dacfad6f5498322fc08cc2d1Brian Paul#if !FEATURE_convolve
27440d1a40f294f1ed2dacfad6f5498322fc08cc2d1Brian Paulstatic void
27540d1a40f294f1ed2dacfad6f5498322fc08cc2d1Brian Paul_mesa_adjust_image_for_convolution(GLcontext *ctx, GLuint dims,
27640d1a40f294f1ed2dacfad6f5498322fc08cc2d1Brian Paul                                   GLsizei *srcWidth, GLsizei *srcHeight)
27740d1a40f294f1ed2dacfad6f5498322fc08cc2d1Brian Paul{
27840d1a40f294f1ed2dacfad6f5498322fc08cc2d1Brian Paul   /* no-op */
27940d1a40f294f1ed2dacfad6f5498322fc08cc2d1Brian Paul}
28040d1a40f294f1ed2dacfad6f5498322fc08cc2d1Brian Paul#endif
28140d1a40f294f1ed2dacfad6f5498322fc08cc2d1Brian Paul
28240d1a40f294f1ed2dacfad6f5498322fc08cc2d1Brian Paul
283f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul/**
284f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * Make a temporary (color) texture image with GLfloat components.
285f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * Apply all needed pixel unpacking and pixel transfer operations.
286f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * Note that there are both logicalBaseFormat and textureBaseFormat parameters.
287f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * Suppose the user specifies GL_LUMINANCE as the internal texture format
288f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * but the graphics hardware doesn't support luminance textures.  So, might
289f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * use an RGB hardware format instead.
290f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * If logicalBaseFormat != textureBaseFormat we have some extra work to do.
291f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul *
292f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * \param ctx  the rendering context
293f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * \param dims  image dimensions: 1, 2 or 3
294f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * \param logicalBaseFormat  basic texture derived from the user's
295f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul *    internal texture format value
296f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * \param textureBaseFormat  the actual basic format of the texture
297f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * \param srcWidth  source image width
298f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * \param srcHeight  source image height
299f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * \param srcDepth  source image depth
300f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * \param srcFormat  source image format
301f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * \param srcType  source image type
302f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * \param srcAddr  source image address
303f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * \param srcPacking  source image pixel packing
304f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * \return resulting image with format = textureBaseFormat and type = GLfloat.
305f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul */
306f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paulstatic GLfloat *
307f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paulmake_temp_float_image(GLcontext *ctx, GLuint dims,
308f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                      GLenum logicalBaseFormat,
309f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                      GLenum textureBaseFormat,
310f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                      GLint srcWidth, GLint srcHeight, GLint srcDepth,
311f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                      GLenum srcFormat, GLenum srcType,
312f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                      const GLvoid *srcAddr,
313f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                      const struct gl_pixelstore_attrib *srcPacking)
314f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul{
315f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   GLuint transferOps = ctx->_ImageTransferState;
316f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   GLfloat *tempImage;
317f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
318f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   ASSERT(dims >= 1 && dims <= 3);
319f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
320f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   ASSERT(logicalBaseFormat == GL_RGBA ||
321f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          logicalBaseFormat == GL_RGB ||
322f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          logicalBaseFormat == GL_LUMINANCE_ALPHA ||
323f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          logicalBaseFormat == GL_LUMINANCE ||
324f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          logicalBaseFormat == GL_ALPHA ||
325f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          logicalBaseFormat == GL_INTENSITY ||
326f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          logicalBaseFormat == GL_COLOR_INDEX ||
327f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          logicalBaseFormat == GL_DEPTH_COMPONENT);
328f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
329f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   ASSERT(textureBaseFormat == GL_RGBA ||
330f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          textureBaseFormat == GL_RGB ||
331f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          textureBaseFormat == GL_LUMINANCE_ALPHA ||
332f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          textureBaseFormat == GL_LUMINANCE ||
333f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          textureBaseFormat == GL_ALPHA ||
334f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          textureBaseFormat == GL_INTENSITY ||
335f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          textureBaseFormat == GL_COLOR_INDEX ||
336f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          textureBaseFormat == GL_DEPTH_COMPONENT);
337f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
338f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   /* conventional color image */
339f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
340f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if ((dims == 1 && ctx->Pixel.Convolution1DEnabled) ||
341f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       (dims >= 2 && ctx->Pixel.Convolution2DEnabled) ||
342f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       (dims >= 2 && ctx->Pixel.Separable2DEnabled)) {
343f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* need image convolution */
344f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      const GLuint preConvTransferOps
345f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         = (transferOps & IMAGE_PRE_CONVOLUTION_BITS) | IMAGE_CLAMP_BIT;
346f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      const GLuint postConvTransferOps
347f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         = (transferOps & IMAGE_POST_CONVOLUTION_BITS) | IMAGE_CLAMP_BIT;
348f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLint img, row;
349f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLint convWidth, convHeight;
350f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLfloat *convImage;
351f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
352f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* pre-convolution image buffer (3D) */
353f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      tempImage = (GLfloat *) _mesa_malloc(srcWidth * srcHeight * srcDepth
354f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                           * 4 * sizeof(GLfloat));
355f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (!tempImage)
356f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         return NULL;
357f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
358f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* post-convolution image buffer (2D) */
359f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      convImage = (GLfloat *) _mesa_malloc(srcWidth * srcHeight
360f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                           * 4 * sizeof(GLfloat));
361f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (!convImage) {
362f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         _mesa_free(tempImage);
363f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         return NULL;
364f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
365f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
366f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* loop over 3D image slices */
367f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      for (img = 0; img < srcDepth; img++) {
368f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         GLfloat *dst = tempImage + img * (srcWidth * srcHeight * 4);
369f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
370f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         /* unpack and do transfer ops up to convolution */
371f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         for (row = 0; row < srcHeight; row++) {
37260909388ab136d849d99eab49e782a53772a618fBrian Paul            const GLvoid *src = _mesa_image_address(dims, srcPacking,
373f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                              srcAddr, srcWidth, srcHeight,
374f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                              srcFormat, srcType, img, row, 0);
375f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            _mesa_unpack_color_span_float(ctx, srcWidth, GL_RGBA, dst,
376f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                          srcFormat, srcType, src,
377f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                          srcPacking,
378f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                          preConvTransferOps);
379f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            dst += srcWidth * 4;
380f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         }
381f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
3826f1abb9c215583d11980294f7469da41ec14b7daBrian Paul         /* size after optional convolution */
3836f1abb9c215583d11980294f7469da41ec14b7daBrian Paul         convWidth = srcWidth;
3846f1abb9c215583d11980294f7469da41ec14b7daBrian Paul         convHeight = srcHeight;
3856f1abb9c215583d11980294f7469da41ec14b7daBrian Paul
38640d1a40f294f1ed2dacfad6f5498322fc08cc2d1Brian Paul#if FEATURE_convolve
387f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         /* do convolution */
388f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         {
389f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            GLfloat *src = tempImage + img * (srcWidth * srcHeight * 4);
390f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            if (dims == 1) {
391f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               ASSERT(ctx->Pixel.Convolution1DEnabled);
392f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               _mesa_convolve_1d_image(ctx, &convWidth, src, convImage);
393f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            }
394f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            else {
395f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               if (ctx->Pixel.Convolution2DEnabled) {
396f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                  _mesa_convolve_2d_image(ctx, &convWidth, &convHeight,
397f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                          src, convImage);
398f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               }
399f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               else {
400f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                  ASSERT(ctx->Pixel.Separable2DEnabled);
401f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                  _mesa_convolve_sep_image(ctx, &convWidth, &convHeight,
402f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                           src, convImage);
403f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               }
404f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            }
405f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         }
40640d1a40f294f1ed2dacfad6f5498322fc08cc2d1Brian Paul#endif
407f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         /* do post-convolution transfer and pack into tempImage */
408f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         {
4092b012578ee519561365640e23272b71898378c45Brian Paul            const GLint logComponents
4102b012578ee519561365640e23272b71898378c45Brian Paul               = _mesa_components_in_format(logicalBaseFormat);
411f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            const GLfloat *src = convImage;
412f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            GLfloat *dst = tempImage + img * (convWidth * convHeight * 4);
413f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            for (row = 0; row < convHeight; row++) {
414f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               _mesa_pack_rgba_span_float(ctx, convWidth,
415176501dfff14b5bec78af2b3487207d42c26d37aBrian Paul                                          (GLfloat (*)[4]) src,
416f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                          logicalBaseFormat, GL_FLOAT,
417f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                          dst, &ctx->DefaultPacking,
418c7eb423c49ef3e0e071deaab04dad55254f2fa30Brian Paul                                          postConvTransferOps);
419f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               src += convWidth * 4;
4202b012578ee519561365640e23272b71898378c45Brian Paul               dst += convWidth * logComponents;
421f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            }
422f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         }
423f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      } /* loop over 3D image slices */
424f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
425f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      _mesa_free(convImage);
426f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
427f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* might need these below */
428f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      srcWidth = convWidth;
429f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      srcHeight = convHeight;
430f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
431f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   else {
432f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* no convolution */
433f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      const GLint components = _mesa_components_in_format(logicalBaseFormat);
434f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      const GLint srcStride = _mesa_image_row_stride(srcPacking,
435f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcWidth, srcFormat, srcType);
436f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLfloat *dst;
437f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLint img, row;
438f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
439f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      tempImage = (GLfloat *) _mesa_malloc(srcWidth * srcHeight * srcDepth
440f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                           * components * sizeof(GLfloat));
441f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (!tempImage)
442f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         return NULL;
443f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
444f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      dst = tempImage;
445f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      for (img = 0; img < srcDepth; img++) {
446f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         const GLubyte *src
44760909388ab136d849d99eab49e782a53772a618fBrian Paul            = (const GLubyte *) _mesa_image_address(dims, srcPacking, srcAddr,
448f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                    srcWidth, srcHeight,
449f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                    srcFormat, srcType,
450f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                    img, 0, 0);
451f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         for (row = 0; row < srcHeight; row++) {
452f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            _mesa_unpack_color_span_float(ctx, srcWidth, logicalBaseFormat,
453f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                          dst, srcFormat, srcType, src,
454f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                          srcPacking, transferOps);
455f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            dst += srcWidth * components;
456f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            src += srcStride;
457f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         }
458f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
459f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
460f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
461f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (logicalBaseFormat != textureBaseFormat) {
462f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* more work */
463f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLint texComponents = _mesa_components_in_format(textureBaseFormat);
464f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLint logComponents = _mesa_components_in_format(logicalBaseFormat);
465f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLfloat *newImage;
466f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLint i, n;
4673aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      GLubyte map[6];
468f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
46913ad04719e292a2bee7e1b3155da74a97921c035Brian Paul      /* we only promote up to RGB, RGBA and LUMINANCE_ALPHA formats for now */
47013ad04719e292a2bee7e1b3155da74a97921c035Brian Paul      ASSERT(textureBaseFormat == GL_RGB || textureBaseFormat == GL_RGBA ||
47113ad04719e292a2bee7e1b3155da74a97921c035Brian Paul             textureBaseFormat == GL_LUMINANCE_ALPHA);
472f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
473f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* The actual texture format should have at least as many components
474f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       * as the logical texture format.
475f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       */
476f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      ASSERT(texComponents >= logComponents);
477f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
478f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      newImage = (GLfloat *) _mesa_malloc(srcWidth * srcHeight * srcDepth
479f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                          * texComponents * sizeof(GLfloat));
480f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (!newImage) {
481f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         _mesa_free(tempImage);
482f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         return NULL;
483f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
484f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
4853aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      compute_component_mapping(logicalBaseFormat, textureBaseFormat, map);
486f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
487f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      n = srcWidth * srcHeight * srcDepth;
488f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      for (i = 0; i < n; i++) {
489f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         GLint k;
490f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         for (k = 0; k < texComponents; k++) {
491f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            GLint j = map[k];
492f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            if (j == ZERO)
493f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               newImage[i * texComponents + k] = 0.0F;
494f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            else if (j == ONE)
495f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               newImage[i * texComponents + k] = 1.0F;
496f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            else
497f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               newImage[i * texComponents + k] = tempImage[i * logComponents + j];
498f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         }
499f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
500f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
501f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      _mesa_free(tempImage);
502f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      tempImage = newImage;
503f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
504f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
505f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   return tempImage;
506f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul}
507f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
508f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
509f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul/**
510f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * Make a temporary (color) texture image with GLchan components.
511f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * Apply all needed pixel unpacking and pixel transfer operations.
512f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * Note that there are both logicalBaseFormat and textureBaseFormat parameters.
513f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * Suppose the user specifies GL_LUMINANCE as the internal texture format
514f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * but the graphics hardware doesn't support luminance textures.  So, might
515f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * use an RGB hardware format instead.
516f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * If logicalBaseFormat != textureBaseFormat we have some extra work to do.
517f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul *
518f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * \param ctx  the rendering context
519f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * \param dims  image dimensions: 1, 2 or 3
520f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * \param logicalBaseFormat  basic texture derived from the user's
521f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul *    internal texture format value
522f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * \param textureBaseFormat  the actual basic format of the texture
523f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * \param srcWidth  source image width
524f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * \param srcHeight  source image height
525f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * \param srcDepth  source image depth
526f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * \param srcFormat  source image format
527f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * \param srcType  source image type
528f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * \param srcAddr  source image address
529f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * \param srcPacking  source image pixel packing
530f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * \return resulting image with format = textureBaseFormat and type = GLchan.
531f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul */
5328f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian PaulGLchan *
5338f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paul_mesa_make_temp_chan_image(GLcontext *ctx, GLuint dims,
5348f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paul                           GLenum logicalBaseFormat,
5358f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paul                           GLenum textureBaseFormat,
5368f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paul                           GLint srcWidth, GLint srcHeight, GLint srcDepth,
5378f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paul                           GLenum srcFormat, GLenum srcType,
5388f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paul                           const GLvoid *srcAddr,
5398f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paul                           const struct gl_pixelstore_attrib *srcPacking)
540f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul{
541f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   GLuint transferOps = ctx->_ImageTransferState;
542f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   const GLint components = _mesa_components_in_format(logicalBaseFormat);
543f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   GLboolean freeSrcImage = GL_FALSE;
544f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   GLint img, row;
545f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   GLchan *tempImage, *dst;
546f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
547f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   ASSERT(dims >= 1 && dims <= 3);
548f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
549f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   ASSERT(logicalBaseFormat == GL_RGBA ||
550f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          logicalBaseFormat == GL_RGB ||
551f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          logicalBaseFormat == GL_LUMINANCE_ALPHA ||
552f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          logicalBaseFormat == GL_LUMINANCE ||
553f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          logicalBaseFormat == GL_ALPHA ||
554f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          logicalBaseFormat == GL_INTENSITY);
555f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
556f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   ASSERT(textureBaseFormat == GL_RGBA ||
557f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          textureBaseFormat == GL_RGB ||
558f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          textureBaseFormat == GL_LUMINANCE_ALPHA ||
559f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          textureBaseFormat == GL_LUMINANCE ||
560f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          textureBaseFormat == GL_ALPHA ||
561f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          textureBaseFormat == GL_INTENSITY);
562f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
56340d1a40f294f1ed2dacfad6f5498322fc08cc2d1Brian Paul#if FEATURE_convolve
564f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if ((dims == 1 && ctx->Pixel.Convolution1DEnabled) ||
565f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       (dims >= 2 && ctx->Pixel.Convolution2DEnabled) ||
566f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       (dims >= 2 && ctx->Pixel.Separable2DEnabled)) {
567f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* get convolved image */
568f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLfloat *convImage = make_temp_float_image(ctx, dims,
569f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 logicalBaseFormat,
570f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 logicalBaseFormat,
571f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcWidth, srcHeight, srcDepth,
572f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcFormat, srcType,
573f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcAddr, srcPacking);
574f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (!convImage)
575f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         return NULL;
576f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* the convolved image is our new source image */
577f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      srcAddr = convImage;
578f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      srcFormat = logicalBaseFormat;
579f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      srcType = GL_FLOAT;
580f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      srcPacking = &ctx->DefaultPacking;
581f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
582f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      transferOps = 0;
583f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      freeSrcImage = GL_TRUE;
584f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
58540d1a40f294f1ed2dacfad6f5498322fc08cc2d1Brian Paul#endif
586f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
587f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   /* unpack and transfer the source image */
588f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   tempImage = (GLchan *) _mesa_malloc(srcWidth * srcHeight * srcDepth
589f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                       * components * sizeof(GLchan));
590f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (!tempImage)
591f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      return NULL;
592f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
593f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   dst = tempImage;
594f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   for (img = 0; img < srcDepth; img++) {
595f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      const GLint srcStride = _mesa_image_row_stride(srcPacking,
596f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                     srcWidth, srcFormat,
597f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                     srcType);
598f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      const GLubyte *src
59960909388ab136d849d99eab49e782a53772a618fBrian Paul         = (const GLubyte *) _mesa_image_address(dims, srcPacking, srcAddr,
600f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcWidth, srcHeight,
601f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcFormat, srcType,
602f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 img, 0, 0);
603f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      for (row = 0; row < srcHeight; row++) {
6049c1b13ff6a2fb873cada61271f382a912ad99631Brian Paul         _mesa_unpack_color_span_chan(ctx, srcWidth, logicalBaseFormat, dst,
6059c1b13ff6a2fb873cada61271f382a912ad99631Brian Paul                                      srcFormat, srcType, src, srcPacking,
6069c1b13ff6a2fb873cada61271f382a912ad99631Brian Paul                                      transferOps);
607f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         dst += srcWidth * components;
608f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         src += srcStride;
609f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
610f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
611f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
612f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   /* If we made a temporary image for convolution, free it here */
613f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (freeSrcImage) {
614f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      _mesa_free((void *) srcAddr);
615f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
616f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
617f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (logicalBaseFormat != textureBaseFormat) {
618f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* one more conversion step */
619f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLint texComponents = _mesa_components_in_format(textureBaseFormat);
620f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLint logComponents = _mesa_components_in_format(logicalBaseFormat);
621f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLchan *newImage;
622f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLint i, n;
6233aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      GLubyte map[6];
624f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
62513ad04719e292a2bee7e1b3155da74a97921c035Brian Paul      /* we only promote up to RGB, RGBA and LUMINANCE_ALPHA formats for now */
62613ad04719e292a2bee7e1b3155da74a97921c035Brian Paul      ASSERT(textureBaseFormat == GL_RGB || textureBaseFormat == GL_RGBA ||
62713ad04719e292a2bee7e1b3155da74a97921c035Brian Paul             textureBaseFormat == GL_LUMINANCE_ALPHA);
628f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
629f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* The actual texture format should have at least as many components
630f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       * as the logical texture format.
631f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       */
632f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      ASSERT(texComponents >= logComponents);
633f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
634f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      newImage = (GLchan *) _mesa_malloc(srcWidth * srcHeight * srcDepth
6352dbffb30f05fcf67658c64b8101e9efaf07ca388Brian Paul                                         * texComponents * sizeof(GLchan));
636f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (!newImage) {
637f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         _mesa_free(tempImage);
638f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         return NULL;
639f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
640f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
6413aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      compute_component_mapping(logicalBaseFormat, textureBaseFormat, map);
642f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
643f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      n = srcWidth * srcHeight * srcDepth;
644f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      for (i = 0; i < n; i++) {
645f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         GLint k;
646f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         for (k = 0; k < texComponents; k++) {
647f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            GLint j = map[k];
648f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            if (j == ZERO)
649f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               newImage[i * texComponents + k] = 0;
650f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            else if (j == ONE)
651f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               newImage[i * texComponents + k] = CHAN_MAX;
652f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            else
653f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               newImage[i * texComponents + k] = tempImage[i * logComponents + j];
654f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         }
655f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
656f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
657f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      _mesa_free(tempImage);
658f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      tempImage = newImage;
659f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
660f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
661f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   return tempImage;
662f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul}
663f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
664f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
665c039af165d5919008c6df599795951f85dea164dBrian Paul/**
666c039af165d5919008c6df599795951f85dea164dBrian Paul * Copy GLubyte pixels from <src> to <dst> with swizzling.
667c039af165d5919008c6df599795951f85dea164dBrian Paul * \param dst  destination pixels
668c039af165d5919008c6df599795951f85dea164dBrian Paul * \param dstComponents  number of color components in destination pixels
669c039af165d5919008c6df599795951f85dea164dBrian Paul * \param src  source pixels
670c039af165d5919008c6df599795951f85dea164dBrian Paul * \param srcComponents  number of color components in source pixels
671248200737398a7d6403a23930a6c9d93db06b942Brian Paul * \param map  the swizzle mapping.  map[X] says where to find the X component
672248200737398a7d6403a23930a6c9d93db06b942Brian Paul *             in the source image's pixels.  For example, if the source image
673248200737398a7d6403a23930a6c9d93db06b942Brian Paul *             is GL_BGRA and X = red, map[0] yields 2.
674c039af165d5919008c6df599795951f85dea164dBrian Paul * \param count  number of pixels to copy/swizzle.
675c039af165d5919008c6df599795951f85dea164dBrian Paul */
676c039af165d5919008c6df599795951f85dea164dBrian Paulstatic void
677c039af165d5919008c6df599795951f85dea164dBrian Paulswizzle_copy(GLubyte *dst, GLuint dstComponents, const GLubyte *src,
678c039af165d5919008c6df599795951f85dea164dBrian Paul             GLuint srcComponents, const GLubyte *map, GLuint count)
67971699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell{
680501338d70e96e0388fd5198625d856c4ec07745fBrian Paul#define SWZ_CPY(dst, src, count, dstComps, srcComps) \
681501338d70e96e0388fd5198625d856c4ec07745fBrian Paul   do {                                              \
682501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      GLuint i;                                      \
683501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      for (i = 0; i < count; i++) {                  \
684501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         GLuint j;                                   \
685501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         if (srcComps == 4) {                        \
686501338d70e96e0388fd5198625d856c4ec07745fBrian Paul            COPY_4UBV(tmp, src);                     \
687501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         }                                           \
688501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         else {                                      \
689501338d70e96e0388fd5198625d856c4ec07745fBrian Paul            for (j = 0; j < srcComps; j++) {         \
690501338d70e96e0388fd5198625d856c4ec07745fBrian Paul               tmp[j] = src[j];                      \
691501338d70e96e0388fd5198625d856c4ec07745fBrian Paul            }                                        \
692501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         }                                           \
693501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         src += srcComps;                            \
694501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         for (j = 0; j < dstComps; j++) {            \
695501338d70e96e0388fd5198625d856c4ec07745fBrian Paul            dst[j] = tmp[map[j]];                    \
696501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         }                                           \
697501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         dst += dstComps;                            \
698501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      }                                              \
699501338d70e96e0388fd5198625d856c4ec07745fBrian Paul   } while (0)
700501338d70e96e0388fd5198625d856c4ec07745fBrian Paul
701248200737398a7d6403a23930a6c9d93db06b942Brian Paul   GLubyte tmp[6];
70271699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell
70371699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   tmp[ZERO] = 0x0;
70471699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   tmp[ONE] = 0xff;
70571699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell
706501338d70e96e0388fd5198625d856c4ec07745fBrian Paul   ASSERT(srcComponents <= 4);
707501338d70e96e0388fd5198625d856c4ec07745fBrian Paul   ASSERT(dstComponents <= 4);
708501338d70e96e0388fd5198625d856c4ec07745fBrian Paul
70971699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   switch (dstComponents) {
71071699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   case 4:
711501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      switch (srcComponents) {
712501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      case 4:
713501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         SWZ_CPY(dst, src, count, 4, 4);
714501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         break;
715501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      case 3:
716501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         SWZ_CPY(dst, src, count, 4, 3);
717501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         break;
718501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      case 2:
719501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         SWZ_CPY(dst, src, count, 4, 2);
720501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         break;
721501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      case 1:
722501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         SWZ_CPY(dst, src, count, 4, 1);
723501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         break;
724501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      default:
725501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         ;
72671699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell      }
72771699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell      break;
72871699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   case 3:
729501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      switch (srcComponents) {
730501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      case 4:
731501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         SWZ_CPY(dst, src, count, 3, 4);
732501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         break;
733501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      case 3:
734501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         SWZ_CPY(dst, src, count, 3, 3);
735501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         break;
736501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      case 2:
737501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         SWZ_CPY(dst, src, count, 3, 2);
738501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         break;
739501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      case 1:
740501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         SWZ_CPY(dst, src, count, 3, 1);
741501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         break;
742501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      default:
743501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         ;
74471699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell      }
74571699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell      break;
74671699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   case 2:
747501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      switch (srcComponents) {
748501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      case 4:
749501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         SWZ_CPY(dst, src, count, 2, 4);
750501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         break;
751501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      case 3:
752501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         SWZ_CPY(dst, src, count, 2, 3);
753501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         break;
754501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      case 2:
755501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         SWZ_CPY(dst, src, count, 2, 2);
756501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         break;
757501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      case 1:
758501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         SWZ_CPY(dst, src, count, 2, 1);
759501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         break;
760501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      default:
761501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         ;
76271699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell      }
76371699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell      break;
7643aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   case 1:
765501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      switch (srcComponents) {
766501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      case 4:
767501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         SWZ_CPY(dst, src, count, 1, 4);
768501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         break;
769501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      case 3:
770501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         SWZ_CPY(dst, src, count, 1, 3);
771501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         break;
772501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      case 2:
773501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         SWZ_CPY(dst, src, count, 1, 2);
774501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         break;
775501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      case 1:
776501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         SWZ_CPY(dst, src, count, 1, 1);
777501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         break;
778501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      default:
779501338d70e96e0388fd5198625d856c4ec07745fBrian Paul         ;
7803aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      }
7813aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      break;
782501338d70e96e0388fd5198625d856c4ec07745fBrian Paul   default:
783501338d70e96e0388fd5198625d856c4ec07745fBrian Paul      ;
78471699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   }
785501338d70e96e0388fd5198625d856c4ec07745fBrian Paul#undef SWZ_CPY
78671699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell}
78771699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell
78846c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell
789501338d70e96e0388fd5198625d856c4ec07745fBrian Paul
790bad5cf056ac653e97d96114d42b874f15a3c2ec8Keith Whitwellstatic const GLubyte map_identity[6] = { 0, 1, 2, 3, ZERO, ONE };
791bad5cf056ac653e97d96114d42b874f15a3c2ec8Keith Whitwellstatic const GLubyte map_3210[6] = { 3, 2, 1, 0, ZERO, ONE };
79246c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell
793bad5cf056ac653e97d96114d42b874f15a3c2ec8Keith Whitwell/* Deal with the _REV input types:
794bad5cf056ac653e97d96114d42b874f15a3c2ec8Keith Whitwell */
79546c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwellstatic const GLubyte *
796bad5cf056ac653e97d96114d42b874f15a3c2ec8Keith Whitwelltype_mapping( GLenum srcType )
79746c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell{
79846c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell   switch (srcType) {
799c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger   case GL_BYTE:
80046c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell   case GL_UNSIGNED_BYTE:
80146c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell      return map_identity;
802bad5cf056ac653e97d96114d42b874f15a3c2ec8Keith Whitwell   case GL_UNSIGNED_INT_8_8_8_8:
803df8632ebd87219c809810d993f56fef1e6853a25Michel Dänzer      return _mesa_little_endian() ? map_3210 : map_identity;
804bad5cf056ac653e97d96114d42b874f15a3c2ec8Keith Whitwell   case GL_UNSIGNED_INT_8_8_8_8_REV:
805df8632ebd87219c809810d993f56fef1e6853a25Michel Dänzer      return _mesa_little_endian() ? map_identity : map_3210;
80646c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell   default:
80746c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell      return NULL;
80846c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell   }
80946c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell}
81046c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell
811bad5cf056ac653e97d96114d42b874f15a3c2ec8Keith Whitwell/* Mapping required if input type is
81246c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell */
81346c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwellstatic const GLubyte *
814bad5cf056ac653e97d96114d42b874f15a3c2ec8Keith Whitwellbyteswap_mapping( GLboolean swapBytes,
815bad5cf056ac653e97d96114d42b874f15a3c2ec8Keith Whitwell		  GLenum srcType )
81646c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell{
817bad5cf056ac653e97d96114d42b874f15a3c2ec8Keith Whitwell   if (!swapBytes)
818bad5cf056ac653e97d96114d42b874f15a3c2ec8Keith Whitwell      return map_identity;
819bad5cf056ac653e97d96114d42b874f15a3c2ec8Keith Whitwell
82046c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell   switch (srcType) {
821c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger   case GL_BYTE:
82246c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell   case GL_UNSIGNED_BYTE:
82346c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell      return map_identity;
82446c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell   case GL_UNSIGNED_INT_8_8_8_8:
82546c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell   case GL_UNSIGNED_INT_8_8_8_8_REV:
82646c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell      return map_3210;
82746c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell   default:
82846c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell      return NULL;
82946c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell   }
83046c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell}
83146c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell
83246c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell
83346c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell
834c039af165d5919008c6df599795951f85dea164dBrian Paul/**
835c039af165d5919008c6df599795951f85dea164dBrian Paul * Transfer a GLubyte texture image with component swizzling.
836c039af165d5919008c6df599795951f85dea164dBrian Paul */
83771699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwellstatic void
83871699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell_mesa_swizzle_ubyte_image(GLcontext *ctx,
83971699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell			  GLuint dimensions,
84071699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell			  GLenum srcFormat,
84146c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell			  GLenum srcType,
84246c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell
8430c9259f3b95615ceda134bd7074d871cd0186c89Keith Whitwell			  GLenum baseInternalFormat,
8440c9259f3b95615ceda134bd7074d871cd0186c89Keith Whitwell
84546c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell			  const GLubyte *rgba2dst,
8460c9259f3b95615ceda134bd7074d871cd0186c89Keith Whitwell			  GLuint dstComponents,
84771699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell
84871699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell			  GLvoid *dstAddr,
84971699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell			  GLint dstXoffset, GLint dstYoffset, GLint dstZoffset,
850b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul			  GLint dstRowStride,
851b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                          const GLuint *dstImageOffsets,
85271699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell
85371699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell			  GLint srcWidth, GLint srcHeight, GLint srcDepth,
85471699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell			  const GLvoid *srcAddr,
85571699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell			  const struct gl_pixelstore_attrib *srcPacking )
85671699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell{
85771699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   GLint srcComponents = _mesa_components_in_format(srcFormat);
858bad5cf056ac653e97d96114d42b874f15a3c2ec8Keith Whitwell   const GLubyte *srctype2ubyte, *swap;
8593aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   GLubyte map[4], src2base[6], base2rgba[6];
86071699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   GLint i;
861c039af165d5919008c6df599795951f85dea164dBrian Paul   const GLint srcRowStride =
862c039af165d5919008c6df599795951f85dea164dBrian Paul      _mesa_image_row_stride(srcPacking, srcWidth,
863c039af165d5919008c6df599795951f85dea164dBrian Paul                             srcFormat, GL_UNSIGNED_BYTE);
864c039af165d5919008c6df599795951f85dea164dBrian Paul   const GLint srcImageStride
865c039af165d5919008c6df599795951f85dea164dBrian Paul      = _mesa_image_image_stride(srcPacking, srcWidth, srcHeight, srcFormat,
866c039af165d5919008c6df599795951f85dea164dBrian Paul                                 GL_UNSIGNED_BYTE);
867c039af165d5919008c6df599795951f85dea164dBrian Paul   const GLubyte *srcImage
868c039af165d5919008c6df599795951f85dea164dBrian Paul      = (const GLubyte *) _mesa_image_address(dimensions, srcPacking, srcAddr,
869c039af165d5919008c6df599795951f85dea164dBrian Paul                                              srcWidth, srcHeight, srcFormat,
870c039af165d5919008c6df599795951f85dea164dBrian Paul                                              GL_UNSIGNED_BYTE, 0, 0, 0);
87171699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell
872edc16a5f7a89584490be0824a1d96e2f3426998bBrian Paul   (void) ctx;
873edc16a5f7a89584490be0824a1d96e2f3426998bBrian Paul
8740c9259f3b95615ceda134bd7074d871cd0186c89Keith Whitwell   /* Translate from src->baseInternal->GL_RGBA->dst.  This will
8750c9259f3b95615ceda134bd7074d871cd0186c89Keith Whitwell    * correctly deal with RGBA->RGB->RGBA conversions where the final
8760c9259f3b95615ceda134bd7074d871cd0186c89Keith Whitwell    * A value must be 0xff regardless of the incoming alpha values.
8770c9259f3b95615ceda134bd7074d871cd0186c89Keith Whitwell    */
8783aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   compute_component_mapping(srcFormat, baseInternalFormat, src2base);
8793aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   compute_component_mapping(baseInternalFormat, GL_RGBA, base2rgba);
880bad5cf056ac653e97d96114d42b874f15a3c2ec8Keith Whitwell   swap = byteswap_mapping(srcPacking->SwapBytes, srcType);
881bad5cf056ac653e97d96114d42b874f15a3c2ec8Keith Whitwell   srctype2ubyte = type_mapping(srcType);
88246c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell
88371699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell
88471699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   for (i = 0; i < 4; i++)
885bad5cf056ac653e97d96114d42b874f15a3c2ec8Keith Whitwell      map[i] = srctype2ubyte[swap[src2base[base2rgba[rgba2dst[i]]]]];
88671699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell
8872e5c686c2b6f356895f33b2815e41386946ab55aKeith Whitwell/*    _mesa_printf("map %d %d %d %d\n", map[0], map[1], map[2], map[3]);  */
8882e5c686c2b6f356895f33b2815e41386946ab55aKeith Whitwell
889b623fa9e2d6f97f9febc978c158d790b26e175a7Brian Paul   if (srcComponents == dstComponents &&
890b623fa9e2d6f97f9febc978c158d790b26e175a7Brian Paul       srcRowStride == dstRowStride &&
8919c09259b8bef8f120cc6f4bb1a44f0eae37d71b3Michel Dänzer       srcRowStride == srcWidth * srcComponents &&
892b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul       dimensions < 3) {
893b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul      /* 1 and 2D images only */
894b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul      GLubyte *dstImage = (GLubyte *) dstAddr
895b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         + dstYoffset * dstRowStride
896b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         + dstXoffset * dstComponents;
89771699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell      swizzle_copy(dstImage, dstComponents, srcImage, srcComponents, map,
898b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul		   srcWidth * srcHeight);
89971699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   }
90071699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   else {
90171699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell      GLint img, row;
90271699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell      for (img = 0; img < srcDepth; img++) {
90371699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell         const GLubyte *srcRow = srcImage;
904b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         GLubyte *dstRow = (GLubyte *) dstAddr
905b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstImageOffsets[dstZoffset + img] * dstComponents
906b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstYoffset * dstRowStride
907b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstXoffset * dstComponents;
90871699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell         for (row = 0; row < srcHeight; row++) {
90971699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell	    swizzle_copy(dstRow, dstComponents, srcRow, srcComponents, map, srcWidth);
91071699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell            dstRow += dstRowStride;
91171699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell            srcRow += srcRowStride;
91271699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell         }
91371699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell         srcImage += srcImageStride;
91471699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell      }
91571699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   }
91671699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell}
91771699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell
91871699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell
919f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul/**
920f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * Teximage storage routine for when a simple memcpy will do.
921f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * No pixel transfer operations or special texel encodings allowed.
922f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * 1D, 2D and 3D images supported.
923f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul */
924f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paulstatic void
92517bcf9f816db3098db42acd7f0672f64554dd6a0Keith Whitwellmemcpy_texture(GLcontext *ctx,
92617bcf9f816db3098db42acd7f0672f64554dd6a0Keith Whitwell	       GLuint dimensions,
92760909388ab136d849d99eab49e782a53772a618fBrian Paul               const struct gl_texture_format *dstFormat,
928f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               GLvoid *dstAddr,
929f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               GLint dstXoffset, GLint dstYoffset, GLint dstZoffset,
930b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul               GLint dstRowStride,
931b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul               const GLuint *dstImageOffsets,
932f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               GLint srcWidth, GLint srcHeight, GLint srcDepth,
933f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               GLenum srcFormat, GLenum srcType,
934f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               const GLvoid *srcAddr,
935f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               const struct gl_pixelstore_attrib *srcPacking)
936f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul{
937f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   const GLint srcRowStride = _mesa_image_row_stride(srcPacking, srcWidth,
938f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                     srcFormat, srcType);
939f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   const GLint srcImageStride = _mesa_image_image_stride(srcPacking,
940f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                      srcWidth, srcHeight, srcFormat, srcType);
94160909388ab136d849d99eab49e782a53772a618fBrian Paul   const GLubyte *srcImage = (const GLubyte *) _mesa_image_address(dimensions,
94260909388ab136d849d99eab49e782a53772a618fBrian Paul        srcPacking, srcAddr, srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
94322108bb571808542b89677752d62d3901698265fBrian Paul   const GLuint texelBytes = _mesa_get_format_bytes(dstFormat->MesaFormat);
94422108bb571808542b89677752d62d3901698265fBrian Paul   const GLint bytesPerRow = srcWidth * texelBytes;
945b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul
946b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul#if 0
947b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul   /* XXX update/re-enable for dstImageOffsets array */
948f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   const GLint bytesPerImage = srcHeight * bytesPerRow;
949f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   const GLint bytesPerTexture = srcDepth * bytesPerImage;
950f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   GLubyte *dstImage = (GLubyte *) dstAddr
951f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     + dstZoffset * dstImageStride
952f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     + dstYoffset * dstRowStride
95322108bb571808542b89677752d62d3901698265fBrian Paul                     + dstXoffset * texelBytes;
954f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
955f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (dstRowStride == srcRowStride &&
956f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       dstRowStride == bytesPerRow &&
957f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       ((dstImageStride == srcImageStride &&
958f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         dstImageStride == bytesPerImage) ||
959f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul        (srcDepth == 1))) {
960f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* one big memcpy */
96117bcf9f816db3098db42acd7f0672f64554dd6a0Keith Whitwell      ctx->Driver.TextureMemCpy(dstImage, srcImage, bytesPerTexture);
962f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
963b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul   else
964b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul   {
965f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLint img, row;
966f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      for (img = 0; img < srcDepth; img++) {
967f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         const GLubyte *srcRow = srcImage;
968f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         GLubyte *dstRow = dstImage;
969f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         for (row = 0; row < srcHeight; row++) {
97017bcf9f816db3098db42acd7f0672f64554dd6a0Keith Whitwell            ctx->Driver.TextureMemCpy(dstRow, srcRow, bytesPerRow);
971f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            dstRow += dstRowStride;
972f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            srcRow += srcRowStride;
973f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         }
974f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         srcImage += srcImageStride;
975f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         dstImage += dstImageStride;
976f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
977f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
978b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul#endif
979b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul
980b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul   GLint img, row;
981b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul   for (img = 0; img < srcDepth; img++) {
982b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul      const GLubyte *srcRow = srcImage;
983b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul      GLubyte *dstRow = (GLubyte *) dstAddr
98422108bb571808542b89677752d62d3901698265fBrian Paul         + dstImageOffsets[dstZoffset + img] * texelBytes
985b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         + dstYoffset * dstRowStride
98622108bb571808542b89677752d62d3901698265fBrian Paul         + dstXoffset * texelBytes;
987b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul      for (row = 0; row < srcHeight; row++) {
988b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         ctx->Driver.TextureMemCpy(dstRow, srcRow, bytesPerRow);
989b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         dstRow += dstRowStride;
990b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         srcRow += srcRowStride;
991b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul      }
992b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul      srcImage += srcImageStride;
993b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul   }
994f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul}
995f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
996f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
997f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
998f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul/**
999f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * Store an image in any of the formats:
1000f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul *   _mesa_texformat_rgba
1001f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul *   _mesa_texformat_rgb
1002f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul *   _mesa_texformat_alpha
1003f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul *   _mesa_texformat_luminance
1004f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul *   _mesa_texformat_luminance_alpha
1005f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul *   _mesa_texformat_intensity
1006f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul *
1007f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul */
1008f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian PaulGLboolean
1009b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul_mesa_texstore_rgba(TEXSTORE_PARAMS)
1010f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul{
1011f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   const GLint components = _mesa_components_in_format(baseInternalFormat);
101222108bb571808542b89677752d62d3901698265fBrian Paul   const GLuint texelBytes = _mesa_get_format_bytes(dstFormat->MesaFormat);
101322108bb571808542b89677752d62d3901698265fBrian Paul   const GLenum baseFormat = _mesa_get_format_base_format(dstFormat->MesaFormat);
1014f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
1015f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   ASSERT(dstFormat == &_mesa_texformat_rgba ||
1016f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          dstFormat == &_mesa_texformat_rgb ||
1017f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          dstFormat == &_mesa_texformat_alpha ||
1018f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          dstFormat == &_mesa_texformat_luminance ||
1019f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          dstFormat == &_mesa_texformat_luminance_alpha ||
1020f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          dstFormat == &_mesa_texformat_intensity);
1021f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   ASSERT(baseInternalFormat == GL_RGBA ||
1022f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          baseInternalFormat == GL_RGB ||
1023f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          baseInternalFormat == GL_ALPHA ||
1024f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          baseInternalFormat == GL_LUMINANCE ||
1025f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          baseInternalFormat == GL_LUMINANCE_ALPHA ||
1026f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          baseInternalFormat == GL_INTENSITY);
102722108bb571808542b89677752d62d3901698265fBrian Paul   ASSERT(texelBytes == components * sizeof(GLchan));
1028f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
1029f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (!ctx->_ImageTransferState &&
1030f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       !srcPacking->SwapBytes &&
1031f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       baseInternalFormat == srcFormat &&
1032f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       srcType == CHAN_TYPE) {
1033f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* simple memcpy path */
103417bcf9f816db3098db42acd7f0672f64554dd6a0Keith Whitwell      memcpy_texture(ctx, dims,
103560909388ab136d849d99eab49e782a53772a618fBrian Paul                     dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
1036b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstRowStride,
1037b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstImageOffsets,
1038f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcWidth, srcHeight, srcDepth, srcFormat, srcType,
1039f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcAddr, srcPacking);
1040f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
1041f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   else if (!ctx->_ImageTransferState &&
1042f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            !srcPacking->SwapBytes &&
1043f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            dstFormat == &_mesa_texformat_rgb &&
1044f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            srcFormat == GL_RGBA &&
1045f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            srcType == CHAN_TYPE) {
1046f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* extract RGB from RGBA */
1047b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul      GLint img, row, col;
1048f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      for (img = 0; img < srcDepth; img++) {
1049b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         GLchan *dstImage = (GLchan *)
1050b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            ((GLubyte *) dstAddr
105122108bb571808542b89677752d62d3901698265fBrian Paul             + dstImageOffsets[dstZoffset + img] * texelBytes
1052b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul             + dstYoffset * dstRowStride
105322108bb571808542b89677752d62d3901698265fBrian Paul             + dstXoffset * texelBytes);
1054b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul
1055f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         const GLint srcRowStride = _mesa_image_row_stride(srcPacking,
1056f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcWidth, srcFormat, srcType);
105760909388ab136d849d99eab49e782a53772a618fBrian Paul         GLchan *srcRow = (GLchan *) _mesa_image_address(dims, srcPacking,
105860909388ab136d849d99eab49e782a53772a618fBrian Paul                  srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, 0, 0);
1059f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         GLchan *dstRow = dstImage;
1060f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         for (row = 0; row < srcHeight; row++) {
1061f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            for (col = 0; col < srcWidth; col++) {
1062f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               dstRow[col * 3 + RCOMP] = srcRow[col * 4 + RCOMP];
1063f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               dstRow[col * 3 + GCOMP] = srcRow[col * 4 + GCOMP];
1064f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               dstRow[col * 3 + BCOMP] = srcRow[col * 4 + BCOMP];
1065f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            }
1066edc16a5f7a89584490be0824a1d96e2f3426998bBrian Paul            dstRow += dstRowStride / sizeof(GLchan);
1067f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            srcRow = (GLchan *) ((GLubyte *) srcRow + srcRowStride);
1068f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         }
1069f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
1070f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
10713aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   else if (!ctx->_ImageTransferState &&
10723aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell	    CHAN_TYPE == GL_UNSIGNED_BYTE &&
10733aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell	    (srcType == GL_UNSIGNED_BYTE ||
10743aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell	     srcType == GL_UNSIGNED_INT_8_8_8_8 ||
10753aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell	     srcType == GL_UNSIGNED_INT_8_8_8_8_REV) &&
10763aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell	    can_swizzle(baseInternalFormat) &&
10773aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell	    can_swizzle(srcFormat)) {
10783aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell
10793974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell      const GLubyte *dstmap;
10803974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell      GLuint components;
10813aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell
10823aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      /* dstmap - how to swizzle from RGBA to dst format:
10833aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell       */
10843974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell      if (dstFormat == &_mesa_texformat_rgba) {
10853974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell	 dstmap = mappings[IDX_RGBA].from_rgba;
10863974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell	 components = 4;
10873974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell      }
10883974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell      else if (dstFormat == &_mesa_texformat_rgb) {
10893974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell	 dstmap = mappings[IDX_RGB].from_rgba;
10903974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell	 components = 3;
10913974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell      }
10923974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell      else if (dstFormat == &_mesa_texformat_alpha) {
10933974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell	 dstmap = mappings[IDX_ALPHA].from_rgba;
10943974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell	 components = 1;
10953974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell      }
10963974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell      else if (dstFormat == &_mesa_texformat_luminance) {
10973974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell	 dstmap = mappings[IDX_LUMINANCE].from_rgba;
10983974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell	 components = 1;
10993974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell      }
11003974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell      else if (dstFormat == &_mesa_texformat_luminance_alpha) {
11013974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell	 dstmap = mappings[IDX_LUMINANCE_ALPHA].from_rgba;
11023974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell	 components = 2;
11033974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell      }
11043974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell      else if (dstFormat == &_mesa_texformat_intensity) {
11053974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell	 dstmap = mappings[IDX_INTENSITY].from_rgba;
11063974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell	 components = 1;
11073974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell      }
11083974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell      else {
1109248200737398a7d6403a23930a6c9d93db06b942Brian Paul         _mesa_problem(ctx, "Unexpected dstFormat in _mesa_texstore_rgba");
1110248200737398a7d6403a23930a6c9d93db06b942Brian Paul         return GL_FALSE;
11113974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell      }
11123974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell
11133aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      _mesa_swizzle_ubyte_image(ctx, dims,
11143aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				srcFormat,
11153aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				srcType,
11163aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				baseInternalFormat,
11173974cc8c09a00274f87c418cb295ed0cdd7c9d1eKeith Whitwell				dstmap, components,
11183aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				dstAddr, dstXoffset, dstYoffset, dstZoffset,
11193aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				dstRowStride, dstImageOffsets,
11203aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				srcWidth, srcHeight, srcDepth, srcAddr,
11213aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				srcPacking);
11223aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   }
1123f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   else {
1124f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* general path */
11258f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paul      const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
1126f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 baseInternalFormat,
112722108bb571808542b89677752d62d3901698265fBrian Paul                                                 baseFormat,
1128f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcWidth, srcHeight, srcDepth,
1129f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcFormat, srcType, srcAddr,
1130f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcPacking);
1131f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      const GLchan *src = tempImage;
11329c1b13ff6a2fb873cada61271f382a912ad99631Brian Paul      GLint bytesPerRow;
1133f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLint img, row;
1134f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (!tempImage)
1135f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         return GL_FALSE;
1136f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
11379c1b13ff6a2fb873cada61271f382a912ad99631Brian Paul      bytesPerRow = srcWidth * components * sizeof(GLchan);
1138f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      for (img = 0; img < srcDepth; img++) {
1139b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         GLubyte *dstRow = (GLubyte *) dstAddr
114022108bb571808542b89677752d62d3901698265fBrian Paul            + dstImageOffsets[dstZoffset + img] * texelBytes
1141b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstYoffset * dstRowStride
114222108bb571808542b89677752d62d3901698265fBrian Paul            + dstXoffset * texelBytes;
1143f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         for (row = 0; row < srcHeight; row++) {
1144f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            _mesa_memcpy(dstRow, src, bytesPerRow);
1145f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            dstRow += dstRowStride;
1146f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            src += srcWidth * components;
1147f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         }
1148f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
1149f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
1150f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      _mesa_free((void *) tempImage);
1151f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
1152f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   return GL_TRUE;
1153f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul}
1154f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
1155f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
1156f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul/**
1157a9bcf751030895494fc098f8d0ff56b2496bd993Brian Paul * Store a 32-bit integer depth component texture image.
1158f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul */
1159f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian PaulGLboolean
1160b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul_mesa_texstore_z32(TEXSTORE_PARAMS)
1161f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul{
116225cfb68f0b3baf0e74d1f6a37afab46370f6711bBrian   const GLuint depthScale = 0xffffffff;
116322108bb571808542b89677752d62d3901698265fBrian Paul   const GLuint texelBytes = _mesa_get_format_bytes(dstFormat->MesaFormat);
1164a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) dims;
1165a9bcf751030895494fc098f8d0ff56b2496bd993Brian Paul   ASSERT(dstFormat == &_mesa_texformat_z32);
116622108bb571808542b89677752d62d3901698265fBrian Paul   ASSERT(texelBytes == sizeof(GLuint));
1167f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
1168966e199e409a1b52eef88e48997442250997f45eBrian Paul   if (ctx->Pixel.DepthScale == 1.0f &&
1169966e199e409a1b52eef88e48997442250997f45eBrian Paul       ctx->Pixel.DepthBias == 0.0f &&
1170f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       !srcPacking->SwapBytes &&
1171a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul       baseInternalFormat == GL_DEPTH_COMPONENT &&
1172a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul       srcFormat == GL_DEPTH_COMPONENT &&
1173a9bcf751030895494fc098f8d0ff56b2496bd993Brian Paul       srcType == GL_UNSIGNED_INT) {
1174f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* simple memcpy path */
117517bcf9f816db3098db42acd7f0672f64554dd6a0Keith Whitwell      memcpy_texture(ctx, dims,
117660909388ab136d849d99eab49e782a53772a618fBrian Paul                     dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
1177b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstRowStride,
1178b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstImageOffsets,
1179f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcWidth, srcHeight, srcDepth, srcFormat, srcType,
1180f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcAddr, srcPacking);
1181f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
1182f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   else {
1183f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* general path */
1184f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLint img, row;
1185f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      for (img = 0; img < srcDepth; img++) {
1186b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         GLubyte *dstRow = (GLubyte *) dstAddr
118722108bb571808542b89677752d62d3901698265fBrian Paul            + dstImageOffsets[dstZoffset + img] * texelBytes
1188b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstYoffset * dstRowStride
118922108bb571808542b89677752d62d3901698265fBrian Paul            + dstXoffset * texelBytes;
1190f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         for (row = 0; row < srcHeight; row++) {
119160909388ab136d849d99eab49e782a53772a618fBrian Paul            const GLvoid *src = _mesa_image_address(dims, srcPacking,
1192f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, row, 0);
11931ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul            _mesa_unpack_depth_span(ctx, srcWidth,
1194a9bcf751030895494fc098f8d0ff56b2496bd993Brian Paul                                    GL_UNSIGNED_INT, (GLuint *) dstRow,
1195a9bcf751030895494fc098f8d0ff56b2496bd993Brian Paul                                    depthScale, srcType, src, srcPacking);
1196f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            dstRow += dstRowStride;
1197f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         }
1198f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
1199f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
1200f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   return GL_TRUE;
1201f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul}
1202f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
1203b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul#define STRIDE_3D 0
1204f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
1205f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul/**
1206a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul * Store a 16-bit integer depth component texture image.
1207f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul */
1208f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian PaulGLboolean
1209b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul_mesa_texstore_z16(TEXSTORE_PARAMS)
1210f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul{
121125cfb68f0b3baf0e74d1f6a37afab46370f6711bBrian   const GLuint depthScale = 0xffff;
121222108bb571808542b89677752d62d3901698265fBrian Paul   const GLuint texelBytes = _mesa_get_format_bytes(dstFormat->MesaFormat);
1213a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) dims;
1214a9bcf751030895494fc098f8d0ff56b2496bd993Brian Paul   ASSERT(dstFormat == &_mesa_texformat_z16);
121522108bb571808542b89677752d62d3901698265fBrian Paul   ASSERT(texelBytes == sizeof(GLushort));
1216f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
1217966e199e409a1b52eef88e48997442250997f45eBrian Paul   if (ctx->Pixel.DepthScale == 1.0f &&
1218966e199e409a1b52eef88e48997442250997f45eBrian Paul       ctx->Pixel.DepthBias == 0.0f &&
1219f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       !srcPacking->SwapBytes &&
1220f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       baseInternalFormat == GL_DEPTH_COMPONENT &&
1221f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       srcFormat == GL_DEPTH_COMPONENT &&
1222a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul       srcType == GL_UNSIGNED_SHORT) {
1223f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* simple memcpy path */
122417bcf9f816db3098db42acd7f0672f64554dd6a0Keith Whitwell      memcpy_texture(ctx, dims,
122560909388ab136d849d99eab49e782a53772a618fBrian Paul                     dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
1226b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstRowStride,
1227b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstImageOffsets,
1228f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcWidth, srcHeight, srcDepth, srcFormat, srcType,
1229f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcAddr, srcPacking);
1230f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
1231f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   else {
1232f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* general path */
12331ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul      GLint img, row;
1234f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      for (img = 0; img < srcDepth; img++) {
1235b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         GLubyte *dstRow = (GLubyte *) dstAddr
123622108bb571808542b89677752d62d3901698265fBrian Paul            + dstImageOffsets[dstZoffset + img] * texelBytes
1237b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstYoffset * dstRowStride
123822108bb571808542b89677752d62d3901698265fBrian Paul            + dstXoffset * texelBytes;
1239f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         for (row = 0; row < srcHeight; row++) {
124060909388ab136d849d99eab49e782a53772a618fBrian Paul            const GLvoid *src = _mesa_image_address(dims, srcPacking,
1241f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, row, 0);
1242a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            GLushort *dst16 = (GLushort *) dstRow;
12431ad7b99925e044f82e635f746c1ef2df77f69ac9Brian Paul            _mesa_unpack_depth_span(ctx, srcWidth,
1244a9bcf751030895494fc098f8d0ff56b2496bd993Brian Paul                                    GL_UNSIGNED_SHORT, dst16, depthScale,
1245f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                    srcType, src, srcPacking);
1246f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            dstRow += dstRowStride;
1247f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         }
1248f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
1249f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
1250f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   return GL_TRUE;
1251f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul}
1252f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
1253f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
1254f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul/**
1255defb035b6cf03c555318d9dd48864242ed036f39Brian Paul * Store an rgb565 or rgb565_rev texture image.
1256f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul */
1257f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian PaulGLboolean
1258b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul_mesa_texstore_rgb565(TEXSTORE_PARAMS)
1259f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul{
126022108bb571808542b89677752d62d3901698265fBrian Paul   const GLuint texelBytes = _mesa_get_format_bytes(dstFormat->MesaFormat);
126122108bb571808542b89677752d62d3901698265fBrian Paul   const GLenum baseFormat = _mesa_get_format_base_format(dstFormat->MesaFormat);
126222108bb571808542b89677752d62d3901698265fBrian Paul
1263defb035b6cf03c555318d9dd48864242ed036f39Brian Paul   ASSERT(dstFormat == &_mesa_texformat_rgb565 ||
1264defb035b6cf03c555318d9dd48864242ed036f39Brian Paul          dstFormat == &_mesa_texformat_rgb565_rev);
126522108bb571808542b89677752d62d3901698265fBrian Paul   ASSERT(texelBytes == 2);
1266f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
1267f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (!ctx->_ImageTransferState &&
1268f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       !srcPacking->SwapBytes &&
1269defb035b6cf03c555318d9dd48864242ed036f39Brian Paul       dstFormat == &_mesa_texformat_rgb565 &&
1270a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul       baseInternalFormat == GL_RGB &&
1271a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul       srcFormat == GL_RGB &&
1272a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul       srcType == GL_UNSIGNED_SHORT_5_6_5) {
1273f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* simple memcpy path */
127417bcf9f816db3098db42acd7f0672f64554dd6a0Keith Whitwell      memcpy_texture(ctx, dims,
127560909388ab136d849d99eab49e782a53772a618fBrian Paul                     dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
1276b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstRowStride,
1277b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstImageOffsets,
1278f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcWidth, srcHeight, srcDepth, srcFormat, srcType,
1279f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcAddr, srcPacking);
1280f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
1281a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul   else if (!ctx->_ImageTransferState &&
1282a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            !srcPacking->SwapBytes &&
1283a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            baseInternalFormat == GL_RGB &&
1284a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            srcFormat == GL_RGB &&
1285a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            srcType == GL_UNSIGNED_BYTE &&
1286a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            dims == 2) {
1287a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      /* do optimized tex store */
1288a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      const GLint srcRowStride = _mesa_image_row_stride(srcPacking, srcWidth,
1289a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul                                                        srcFormat, srcType);
1290a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      const GLubyte *src = (const GLubyte *)
129160909388ab136d849d99eab49e782a53772a618fBrian Paul         _mesa_image_address(dims, srcPacking, srcAddr, srcWidth, srcHeight,
1292a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul                             srcFormat, srcType, 0, 0, 0);
1293a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      GLubyte *dst = (GLubyte *) dstAddr
1294a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul                   + dstYoffset * dstRowStride
129522108bb571808542b89677752d62d3901698265fBrian Paul                   + dstXoffset * texelBytes;
1296a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      GLint row, col;
1297a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      for (row = 0; row < srcHeight; row++) {
1298a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         const GLubyte *srcUB = (const GLubyte *) src;
1299a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         GLushort *dstUS = (GLushort *) dst;
1300defb035b6cf03c555318d9dd48864242ed036f39Brian Paul         /* check for byteswapped format */
1301f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul         if (dstFormat == &_mesa_texformat_rgb565) {
1302f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul            for (col = 0; col < srcWidth; col++) {
1303f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               dstUS[col] = PACK_COLOR_565( srcUB[0], srcUB[1], srcUB[2] );
1304f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               srcUB += 3;
1305f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul            }
1306f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul         }
1307f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul         else {
1308f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul            for (col = 0; col < srcWidth; col++) {
1309f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               dstUS[col] = PACK_COLOR_565_REV( srcUB[0], srcUB[1], srcUB[2] );
1310f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               srcUB += 3;
1311f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul            }
1312defb035b6cf03c555318d9dd48864242ed036f39Brian Paul         }
1313a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         dst += dstRowStride;
1314a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         src += srcRowStride;
1315a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      }
1316a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul   }
1317f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   else {
1318f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* general path */
1319a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
1320a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul                                                 baseInternalFormat,
132122108bb571808542b89677752d62d3901698265fBrian Paul                                                 baseFormat,
1322a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul                                                 srcWidth, srcHeight, srcDepth,
1323a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul                                                 srcFormat, srcType, srcAddr,
1324a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul                                                 srcPacking);
1325a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      const GLchan *src = tempImage;
1326f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLint img, row, col;
1327a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      if (!tempImage)
1328a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         return GL_FALSE;
1329a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
1330f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      for (img = 0; img < srcDepth; img++) {
1331b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         GLubyte *dstRow = (GLubyte *) dstAddr
133222108bb571808542b89677752d62d3901698265fBrian Paul            + dstImageOffsets[dstZoffset + img] * texelBytes
1333b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstYoffset * dstRowStride
133422108bb571808542b89677752d62d3901698265fBrian Paul            + dstXoffset * texelBytes;
1335f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         for (row = 0; row < srcHeight; row++) {
1336a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            GLushort *dstUS = (GLushort *) dstRow;
1337defb035b6cf03c555318d9dd48864242ed036f39Brian Paul            /* check for byteswapped format */
1338f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul            if (dstFormat == &_mesa_texformat_rgb565) {
1339f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               for (col = 0; col < srcWidth; col++) {
1340f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                  dstUS[col] = PACK_COLOR_565( CHAN_TO_UBYTE(src[RCOMP]),
1341f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                               CHAN_TO_UBYTE(src[GCOMP]),
1342f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                               CHAN_TO_UBYTE(src[BCOMP]) );
1343f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                  src += 3;
1344f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               }
1345f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul            }
1346f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul            else {
1347f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               for (col = 0; col < srcWidth; col++) {
1348f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                  dstUS[col] = PACK_COLOR_565_REV( CHAN_TO_UBYTE(src[RCOMP]),
1349f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                   CHAN_TO_UBYTE(src[GCOMP]),
1350f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                   CHAN_TO_UBYTE(src[BCOMP]) );
1351f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                  src += 3;
1352f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               }
1353f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            }
1354f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            dstRow += dstRowStride;
1355f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         }
1356f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
1357f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      _mesa_free((void *) tempImage);
1358f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
1359f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   return GL_TRUE;
1360f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul}
1361f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
1362f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
1363248200737398a7d6403a23930a6c9d93db06b942Brian Paul/**
1364248200737398a7d6403a23930a6c9d93db06b942Brian Paul * Store a texture in MESA_FORMAT_RGBA8888 or MESA_FORMAT_RGBA8888_REV.
1365248200737398a7d6403a23930a6c9d93db06b942Brian Paul */
1366f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian PaulGLboolean
1367b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul_mesa_texstore_rgba8888(TEXSTORE_PARAMS)
1368f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul{
1369184b5d89380e18008d64adfe1756dca9736426f2Brian Paul   const GLboolean littleEndian = _mesa_little_endian();
137022108bb571808542b89677752d62d3901698265fBrian Paul   const GLuint texelBytes = _mesa_get_format_bytes(dstFormat->MesaFormat);
137122108bb571808542b89677752d62d3901698265fBrian Paul   const GLenum baseFormat = _mesa_get_format_base_format(dstFormat->MesaFormat);
137271699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell
1373defb035b6cf03c555318d9dd48864242ed036f39Brian Paul   ASSERT(dstFormat == &_mesa_texformat_rgba8888 ||
1374defb035b6cf03c555318d9dd48864242ed036f39Brian Paul          dstFormat == &_mesa_texformat_rgba8888_rev);
137522108bb571808542b89677752d62d3901698265fBrian Paul   ASSERT(texelBytes == 4);
1376f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
1377f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (!ctx->_ImageTransferState &&
1378f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       !srcPacking->SwapBytes &&
1379defb035b6cf03c555318d9dd48864242ed036f39Brian Paul       dstFormat == &_mesa_texformat_rgba8888 &&
1380f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       baseInternalFormat == GL_RGBA &&
1381defb035b6cf03c555318d9dd48864242ed036f39Brian Paul      ((srcFormat == GL_RGBA && srcType == GL_UNSIGNED_INT_8_8_8_8) ||
13822e2a9813355993ba79eeb8070391e45aabb84f94Roland Scheidegger       (srcFormat == GL_RGBA && srcType == GL_UNSIGNED_BYTE && !littleEndian) ||
13832e2a9813355993ba79eeb8070391e45aabb84f94Roland Scheidegger       (srcFormat == GL_ABGR_EXT && srcType == GL_UNSIGNED_INT_8_8_8_8_REV) ||
13842e2a9813355993ba79eeb8070391e45aabb84f94Roland Scheidegger       (srcFormat == GL_ABGR_EXT && srcType == GL_UNSIGNED_BYTE && littleEndian))) {
13852e2a9813355993ba79eeb8070391e45aabb84f94Roland Scheidegger       /* simple memcpy path */
13862e2a9813355993ba79eeb8070391e45aabb84f94Roland Scheidegger      memcpy_texture(ctx, dims,
13872e2a9813355993ba79eeb8070391e45aabb84f94Roland Scheidegger                     dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
13882e2a9813355993ba79eeb8070391e45aabb84f94Roland Scheidegger                     dstRowStride,
13892e2a9813355993ba79eeb8070391e45aabb84f94Roland Scheidegger                     dstImageOffsets,
13902e2a9813355993ba79eeb8070391e45aabb84f94Roland Scheidegger                     srcWidth, srcHeight, srcDepth, srcFormat, srcType,
13912e2a9813355993ba79eeb8070391e45aabb84f94Roland Scheidegger                     srcAddr, srcPacking);
13922e2a9813355993ba79eeb8070391e45aabb84f94Roland Scheidegger   }
13932e2a9813355993ba79eeb8070391e45aabb84f94Roland Scheidegger   else if (!ctx->_ImageTransferState &&
13942e2a9813355993ba79eeb8070391e45aabb84f94Roland Scheidegger       !srcPacking->SwapBytes &&
13952e2a9813355993ba79eeb8070391e45aabb84f94Roland Scheidegger       dstFormat == &_mesa_texformat_rgba8888_rev &&
13962e2a9813355993ba79eeb8070391e45aabb84f94Roland Scheidegger       baseInternalFormat == GL_RGBA &&
13972e2a9813355993ba79eeb8070391e45aabb84f94Roland Scheidegger      ((srcFormat == GL_RGBA && srcType == GL_UNSIGNED_INT_8_8_8_8_REV) ||
13982e2a9813355993ba79eeb8070391e45aabb84f94Roland Scheidegger       (srcFormat == GL_RGBA && srcType == GL_UNSIGNED_BYTE && littleEndian) ||
13992e2a9813355993ba79eeb8070391e45aabb84f94Roland Scheidegger       (srcFormat == GL_ABGR_EXT && srcType == GL_UNSIGNED_INT_8_8_8_8) ||
14002e2a9813355993ba79eeb8070391e45aabb84f94Roland Scheidegger       (srcFormat == GL_ABGR_EXT && srcType == GL_UNSIGNED_BYTE && !littleEndian))) {
1401f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* simple memcpy path */
140217bcf9f816db3098db42acd7f0672f64554dd6a0Keith Whitwell      memcpy_texture(ctx, dims,
140360909388ab136d849d99eab49e782a53772a618fBrian Paul                     dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
1404b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstRowStride,
1405b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstImageOffsets,
1406f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcWidth, srcHeight, srcDepth, srcFormat, srcType,
1407f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcAddr, srcPacking);
1408f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
140971699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   else if (!ctx->_ImageTransferState &&
141046c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell	    (srcType == GL_UNSIGNED_BYTE ||
141146c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell	     srcType == GL_UNSIGNED_INT_8_8_8_8 ||
141246c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell	     srcType == GL_UNSIGNED_INT_8_8_8_8_REV) &&
1413528de982f88bfc025425ce1188781a34f4d84f1fRoland Scheidegger	    can_swizzle(baseInternalFormat) &&
141471699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell	    can_swizzle(srcFormat)) {
141546c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell
141671699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell      GLubyte dstmap[4];
141771699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell
1418528de982f88bfc025425ce1188781a34f4d84f1fRoland Scheidegger      /* dstmap - how to swizzle from RGBA to dst format:
141971699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell       */
1420df8632ebd87219c809810d993f56fef1e6853a25Michel Dänzer      if ((littleEndian && dstFormat == &_mesa_texformat_rgba8888) ||
1421df8632ebd87219c809810d993f56fef1e6853a25Michel Dänzer	  (!littleEndian && dstFormat == &_mesa_texformat_rgba8888_rev)) {
142246c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell	 dstmap[3] = 0;
142346c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell	 dstmap[2] = 1;
142446c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell	 dstmap[1] = 2;
142546c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell	 dstmap[0] = 3;
142646c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell      }
142746c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell      else {
142846c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell	 dstmap[3] = 3;
142946c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell	 dstmap[2] = 2;
143046c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell	 dstmap[1] = 1;
143146c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell	 dstmap[0] = 0;
143246c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell      }
143371699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell
143471699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell      _mesa_swizzle_ubyte_image(ctx, dims,
143571699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell				srcFormat,
143646c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell				srcType,
1437528de982f88bfc025425ce1188781a34f4d84f1fRoland Scheidegger				baseInternalFormat,
143871699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell				dstmap, 4,
143971699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell				dstAddr, dstXoffset, dstYoffset, dstZoffset,
1440528de982f88bfc025425ce1188781a34f4d84f1fRoland Scheidegger				dstRowStride, dstImageOffsets,
144171699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell				srcWidth, srcHeight, srcDepth, srcAddr,
144271699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell				srcPacking);
144371699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   }
1444f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   else {
1445f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* general path */
14468f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paul      const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
1447f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 baseInternalFormat,
144822108bb571808542b89677752d62d3901698265fBrian Paul                                                 baseFormat,
1449f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcWidth, srcHeight, srcDepth,
1450f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcFormat, srcType, srcAddr,
1451f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcPacking);
1452f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      const GLchan *src = tempImage;
1453f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLint img, row, col;
1454f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (!tempImage)
1455f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         return GL_FALSE;
1456f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
1457f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      for (img = 0; img < srcDepth; img++) {
1458b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         GLubyte *dstRow = (GLubyte *) dstAddr
145922108bb571808542b89677752d62d3901698265fBrian Paul            + dstImageOffsets[dstZoffset + img] * texelBytes
1460b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstYoffset * dstRowStride
146122108bb571808542b89677752d62d3901698265fBrian Paul            + dstXoffset * texelBytes;
1462f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         for (row = 0; row < srcHeight; row++) {
1463f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            GLuint *dstUI = (GLuint *) dstRow;
1464f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul            if (dstFormat == &_mesa_texformat_rgba8888) {
1465f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               for (col = 0; col < srcWidth; col++) {
1466f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                  dstUI[col] = PACK_COLOR_8888( CHAN_TO_UBYTE(src[RCOMP]),
1467f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                CHAN_TO_UBYTE(src[GCOMP]),
1468f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                CHAN_TO_UBYTE(src[BCOMP]),
1469f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                CHAN_TO_UBYTE(src[ACOMP]) );
1470f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                  src += 4;
1471f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               }
1472f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            }
1473f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul            else {
1474f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               for (col = 0; col < srcWidth; col++) {
1475f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                  dstUI[col] = PACK_COLOR_8888_REV( CHAN_TO_UBYTE(src[RCOMP]),
1476f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                    CHAN_TO_UBYTE(src[GCOMP]),
1477f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                    CHAN_TO_UBYTE(src[BCOMP]),
1478f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                    CHAN_TO_UBYTE(src[ACOMP]) );
1479f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                  src += 4;
1480f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               }
1481a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            }
1482a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            dstRow += dstRowStride;
1483a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         }
1484a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      }
1485a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      _mesa_free((void *) tempImage);
1486a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul   }
1487a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul   return GL_TRUE;
1488a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul}
1489a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul
1490a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul
1491a156b49800c1419785d0709b78ef0d35e6dab5dfBrian PaulGLboolean
1492b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul_mesa_texstore_argb8888(TEXSTORE_PARAMS)
1493f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul{
1494184b5d89380e18008d64adfe1756dca9736426f2Brian Paul   const GLboolean littleEndian = _mesa_little_endian();
149522108bb571808542b89677752d62d3901698265fBrian Paul   const GLuint texelBytes = _mesa_get_format_bytes(dstFormat->MesaFormat);
149622108bb571808542b89677752d62d3901698265fBrian Paul   const GLenum baseFormat = _mesa_get_format_base_format(dstFormat->MesaFormat);
1497f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
1498defb035b6cf03c555318d9dd48864242ed036f39Brian Paul   ASSERT(dstFormat == &_mesa_texformat_argb8888 ||
1499defb035b6cf03c555318d9dd48864242ed036f39Brian Paul          dstFormat == &_mesa_texformat_argb8888_rev);
150022108bb571808542b89677752d62d3901698265fBrian Paul   ASSERT(texelBytes == 4);
1501f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
1502f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (!ctx->_ImageTransferState &&
1503f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       !srcPacking->SwapBytes &&
1504defb035b6cf03c555318d9dd48864242ed036f39Brian Paul       dstFormat == &_mesa_texformat_argb8888 &&
1505f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       baseInternalFormat == GL_RGBA &&
1506f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       srcFormat == GL_BGRA &&
1507f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       ((srcType == GL_UNSIGNED_BYTE && littleEndian) ||
1508defb035b6cf03c555318d9dd48864242ed036f39Brian Paul        srcType == GL_UNSIGNED_INT_8_8_8_8_REV)) {
1509defb035b6cf03c555318d9dd48864242ed036f39Brian Paul      /* simple memcpy path (little endian) */
151017bcf9f816db3098db42acd7f0672f64554dd6a0Keith Whitwell      memcpy_texture(ctx, dims,
151160909388ab136d849d99eab49e782a53772a618fBrian Paul                     dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
1512b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstRowStride,
1513b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstImageOffsets,
1514f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcWidth, srcHeight, srcDepth, srcFormat, srcType,
1515f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcAddr, srcPacking);
1516f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
1517defb035b6cf03c555318d9dd48864242ed036f39Brian Paul   else if (!ctx->_ImageTransferState &&
1518a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul       !srcPacking->SwapBytes &&
1519defb035b6cf03c555318d9dd48864242ed036f39Brian Paul       dstFormat == &_mesa_texformat_argb8888_rev &&
1520a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul       baseInternalFormat == GL_RGBA &&
1521a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul       srcFormat == GL_BGRA &&
1522a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul       ((srcType == GL_UNSIGNED_BYTE && !littleEndian) ||
1523defb035b6cf03c555318d9dd48864242ed036f39Brian Paul        srcType == GL_UNSIGNED_INT_8_8_8_8)) {
1524defb035b6cf03c555318d9dd48864242ed036f39Brian Paul      /* simple memcpy path (big endian) */
152517bcf9f816db3098db42acd7f0672f64554dd6a0Keith Whitwell      memcpy_texture(ctx, dims,
152660909388ab136d849d99eab49e782a53772a618fBrian Paul                     dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
1527b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstRowStride,
1528b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstImageOffsets,
1529a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul                     srcWidth, srcHeight, srcDepth, srcFormat, srcType,
1530a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul                     srcAddr, srcPacking);
1531a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul   }
153271699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   else if (!ctx->_ImageTransferState &&
153371699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell            !srcPacking->SwapBytes &&
153471699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell	    dstFormat == &_mesa_texformat_argb8888 &&
153571699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell            srcFormat == GL_RGB &&
15360c9259f3b95615ceda134bd7074d871cd0186c89Keith Whitwell	    (baseInternalFormat == GL_RGBA ||
15370c9259f3b95615ceda134bd7074d871cd0186c89Keith Whitwell	     baseInternalFormat == GL_RGB) &&
153871699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell            srcType == GL_UNSIGNED_BYTE) {
153971699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell      int img, row, col;
154071699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell      for (img = 0; img < srcDepth; img++) {
154171699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell         const GLint srcRowStride = _mesa_image_row_stride(srcPacking,
154271699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell                                                 srcWidth, srcFormat, srcType);
154371699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell         GLubyte *srcRow = (GLubyte *) _mesa_image_address(dims, srcPacking,
154471699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell                  srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, 0, 0);
1545b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         GLubyte *dstRow = (GLubyte *) dstAddr
154622108bb571808542b89677752d62d3901698265fBrian Paul            + dstImageOffsets[dstZoffset + img] * texelBytes
1547b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstYoffset * dstRowStride
154822108bb571808542b89677752d62d3901698265fBrian Paul            + dstXoffset * texelBytes;
154971699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell         for (row = 0; row < srcHeight; row++) {
1550259eacfa94a1086e4c99db83516989cc27832ef4Brian            GLuint *d4 = (GLuint *) dstRow;
155171699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell            for (col = 0; col < srcWidth; col++) {
155224748268a3ac7bedc2c9ae5bf76c4c741d539f80Brian Paul               d4[col] = PACK_COLOR_8888(0xff,
155324748268a3ac7bedc2c9ae5bf76c4c741d539f80Brian Paul                                         srcRow[col * 3 + RCOMP],
155424748268a3ac7bedc2c9ae5bf76c4c741d539f80Brian Paul                                         srcRow[col * 3 + GCOMP],
155524748268a3ac7bedc2c9ae5bf76c4c741d539f80Brian Paul                                         srcRow[col * 3 + BCOMP]);
155671699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell            }
155771699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell            dstRow += dstRowStride;
155871699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell            srcRow += srcRowStride;
155971699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell         }
156071699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell      }
156171699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   }
156271699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   else if (!ctx->_ImageTransferState &&
156371699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell            !srcPacking->SwapBytes &&
156471699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell	    dstFormat == &_mesa_texformat_argb8888 &&
156571699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell            srcFormat == GL_RGBA &&
15660c9259f3b95615ceda134bd7074d871cd0186c89Keith Whitwell	    baseInternalFormat == GL_RGBA &&
156724748268a3ac7bedc2c9ae5bf76c4c741d539f80Brian Paul            srcType == GL_UNSIGNED_BYTE) {
1568259eacfa94a1086e4c99db83516989cc27832ef4Brian      /* same as above case, but src data has alpha too */
1569b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul      GLint img, row, col;
1570ea4fe661d7f3a95d9db17e1475076f1badf8e1a6Brian Paul      /* For some reason, streaming copies to write-combined regions
1571ea4fe661d7f3a95d9db17e1475076f1badf8e1a6Brian Paul       * are extremely sensitive to the characteristics of how the
1572ea4fe661d7f3a95d9db17e1475076f1badf8e1a6Brian Paul       * source data is retrieved.  By reordering the source reads to
1573ea4fe661d7f3a95d9db17e1475076f1badf8e1a6Brian Paul       * be in-order, the speed of this operation increases by half.
1574ea4fe661d7f3a95d9db17e1475076f1badf8e1a6Brian Paul       * Strangely the same isn't required for the RGB path, above.
1575ea4fe661d7f3a95d9db17e1475076f1badf8e1a6Brian Paul       */
1576ea4fe661d7f3a95d9db17e1475076f1badf8e1a6Brian Paul      for (img = 0; img < srcDepth; img++) {
1577ea4fe661d7f3a95d9db17e1475076f1badf8e1a6Brian Paul         const GLint srcRowStride = _mesa_image_row_stride(srcPacking,
1578ea4fe661d7f3a95d9db17e1475076f1badf8e1a6Brian Paul                                                 srcWidth, srcFormat, srcType);
1579ea4fe661d7f3a95d9db17e1475076f1badf8e1a6Brian Paul         GLubyte *srcRow = (GLubyte *) _mesa_image_address(dims, srcPacking,
1580ea4fe661d7f3a95d9db17e1475076f1badf8e1a6Brian Paul                  srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, 0, 0);
1581b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         GLubyte *dstRow = (GLubyte *) dstAddr
158222108bb571808542b89677752d62d3901698265fBrian Paul            + dstImageOffsets[dstZoffset + img] * texelBytes
1583b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstYoffset * dstRowStride
158422108bb571808542b89677752d62d3901698265fBrian Paul            + dstXoffset * texelBytes;
1585ea4fe661d7f3a95d9db17e1475076f1badf8e1a6Brian Paul         for (row = 0; row < srcHeight; row++) {
1586259eacfa94a1086e4c99db83516989cc27832ef4Brian            GLuint *d4 = (GLuint *) dstRow;
1587ea4fe661d7f3a95d9db17e1475076f1badf8e1a6Brian Paul            for (col = 0; col < srcWidth; col++) {
158824748268a3ac7bedc2c9ae5bf76c4c741d539f80Brian Paul               d4[col] = PACK_COLOR_8888(srcRow[col * 4 + ACOMP],
158924748268a3ac7bedc2c9ae5bf76c4c741d539f80Brian Paul                                         srcRow[col * 4 + RCOMP],
159024748268a3ac7bedc2c9ae5bf76c4c741d539f80Brian Paul                                         srcRow[col * 4 + GCOMP],
159124748268a3ac7bedc2c9ae5bf76c4c741d539f80Brian Paul                                         srcRow[col * 4 + BCOMP]);
159271699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell            }
159371699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell            dstRow += dstRowStride;
159471699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell            srcRow += srcRowStride;
159571699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell         }
159671699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell      }
159771699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   }
159871699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   else if (!ctx->_ImageTransferState &&
159946c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell	    (srcType == GL_UNSIGNED_BYTE ||
160046c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell	     srcType == GL_UNSIGNED_INT_8_8_8_8 ||
160146c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell	     srcType == GL_UNSIGNED_INT_8_8_8_8_REV) &&
16020c9259f3b95615ceda134bd7074d871cd0186c89Keith Whitwell	    can_swizzle(baseInternalFormat) &&
160371699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell	    can_swizzle(srcFormat)) {
160471699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell
160571699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell      GLubyte dstmap[4];
160671699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell
16070c9259f3b95615ceda134bd7074d871cd0186c89Keith Whitwell      /* dstmap - how to swizzle from RGBA to dst format:
160871699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell       */
1609df8632ebd87219c809810d993f56fef1e6853a25Michel Dänzer      if ((littleEndian && dstFormat == &_mesa_texformat_argb8888) ||
1610df8632ebd87219c809810d993f56fef1e6853a25Michel Dänzer	  (!littleEndian && dstFormat == &_mesa_texformat_argb8888_rev)) {
161146c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell	 dstmap[3] = 3;		/* alpha */
161246c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell	 dstmap[2] = 0;		/* red */
161346c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell	 dstmap[1] = 1;		/* green */
161446c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell	 dstmap[0] = 2;		/* blue */
161546c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell      }
161646c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell      else {
1617df8632ebd87219c809810d993f56fef1e6853a25Michel Dänzer	 assert((littleEndian && dstFormat == &_mesa_texformat_argb8888_rev) ||
1618df8632ebd87219c809810d993f56fef1e6853a25Michel Dänzer		(!littleEndian && dstFormat == &_mesa_texformat_argb8888));
161946c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell	 dstmap[3] = 2;
162046c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell	 dstmap[2] = 1;
162146c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell	 dstmap[1] = 0;
162246c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell	 dstmap[0] = 3;
162346c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell      }
162471699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell
162571699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell      _mesa_swizzle_ubyte_image(ctx, dims,
162671699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell				srcFormat,
162746c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell				srcType,
162846c3bd29be4970a8b0c1c358aae0f1d7c05bc9f4Keith Whitwell
16290c9259f3b95615ceda134bd7074d871cd0186c89Keith Whitwell				baseInternalFormat,
163071699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell				dstmap, 4,
163171699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell				dstAddr, dstXoffset, dstYoffset, dstZoffset,
1632b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul				dstRowStride,
1633b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                                dstImageOffsets,
163471699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell				srcWidth, srcHeight, srcDepth, srcAddr,
163571699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell				srcPacking);
163671699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   }
1637a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul   else {
1638a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      /* general path */
1639a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
1640a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul                                                 baseInternalFormat,
164122108bb571808542b89677752d62d3901698265fBrian Paul                                                 baseFormat,
1642a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul                                                 srcWidth, srcHeight, srcDepth,
1643a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul                                                 srcFormat, srcType, srcAddr,
1644a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul                                                 srcPacking);
1645a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      const GLchan *src = tempImage;
1646a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      GLint img, row, col;
1647a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      if (!tempImage)
1648a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         return GL_FALSE;
1649a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
1650a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      for (img = 0; img < srcDepth; img++) {
1651b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         GLubyte *dstRow = (GLubyte *) dstAddr
165222108bb571808542b89677752d62d3901698265fBrian Paul            + dstImageOffsets[dstZoffset + img] * texelBytes
1653b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstYoffset * dstRowStride
165422108bb571808542b89677752d62d3901698265fBrian Paul            + dstXoffset * texelBytes;
1655a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         for (row = 0; row < srcHeight; row++) {
1656a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            GLuint *dstUI = (GLuint *) dstRow;
1657f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul            if (dstFormat == &_mesa_texformat_argb8888) {
1658f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               for (col = 0; col < srcWidth; col++) {
1659f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                  dstUI[col] = PACK_COLOR_8888( CHAN_TO_UBYTE(src[ACOMP]),
1660f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                CHAN_TO_UBYTE(src[RCOMP]),
1661f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                CHAN_TO_UBYTE(src[GCOMP]),
1662f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                CHAN_TO_UBYTE(src[BCOMP]) );
1663f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                  src += 4;
1664f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               }
1665a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            }
1666f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul            else {
1667f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               for (col = 0; col < srcWidth; col++) {
1668f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                  dstUI[col] = PACK_COLOR_8888_REV( CHAN_TO_UBYTE(src[ACOMP]),
1669f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                    CHAN_TO_UBYTE(src[RCOMP]),
1670f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                    CHAN_TO_UBYTE(src[GCOMP]),
1671f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                    CHAN_TO_UBYTE(src[BCOMP]) );
1672f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                  src += 4;
1673f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               }
1674defb035b6cf03c555318d9dd48864242ed036f39Brian Paul            }
1675a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            dstRow += dstRowStride;
1676a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         }
1677a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      }
1678a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      _mesa_free((void *) tempImage);
1679a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul   }
1680a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul   return GL_TRUE;
1681a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul}
1682a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul
1683f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
1684f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian PaulGLboolean
1685b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul_mesa_texstore_rgb888(TEXSTORE_PARAMS)
1686f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul{
1687184b5d89380e18008d64adfe1756dca9736426f2Brian Paul   const GLboolean littleEndian = _mesa_little_endian();
168822108bb571808542b89677752d62d3901698265fBrian Paul   const GLuint texelBytes = _mesa_get_format_bytes(dstFormat->MesaFormat);
168922108bb571808542b89677752d62d3901698265fBrian Paul   const GLenum baseFormat = _mesa_get_format_base_format(dstFormat->MesaFormat);
1690f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
1691f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   ASSERT(dstFormat == &_mesa_texformat_rgb888);
169222108bb571808542b89677752d62d3901698265fBrian Paul   ASSERT(texelBytes == 3);
1693f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
1694f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (!ctx->_ImageTransferState &&
1695f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       !srcPacking->SwapBytes &&
1696f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       baseInternalFormat == GL_RGB &&
1697f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       srcFormat == GL_BGR &&
1698f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       srcType == GL_UNSIGNED_BYTE &&
1699f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       littleEndian) {
1700f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* simple memcpy path */
170117bcf9f816db3098db42acd7f0672f64554dd6a0Keith Whitwell      memcpy_texture(ctx, dims,
170260909388ab136d849d99eab49e782a53772a618fBrian Paul                     dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
1703b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstRowStride,
1704b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstImageOffsets,
1705f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcWidth, srcHeight, srcDepth, srcFormat, srcType,
1706f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcAddr, srcPacking);
1707f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
1708f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   else if (!ctx->_ImageTransferState &&
1709f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            !srcPacking->SwapBytes &&
1710f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            srcFormat == GL_RGBA &&
1711f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            srcType == GL_UNSIGNED_BYTE) {
1712a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      /* extract RGB from RGBA */
1713b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul      GLint img, row, col;
1714f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      for (img = 0; img < srcDepth; img++) {
1715f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         const GLint srcRowStride = _mesa_image_row_stride(srcPacking,
1716f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcWidth, srcFormat, srcType);
171760909388ab136d849d99eab49e782a53772a618fBrian Paul         GLubyte *srcRow = (GLubyte *) _mesa_image_address(dims, srcPacking,
171860909388ab136d849d99eab49e782a53772a618fBrian Paul                  srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, 0, 0);
1719b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         GLubyte *dstRow = (GLubyte *) dstAddr
172022108bb571808542b89677752d62d3901698265fBrian Paul            + dstImageOffsets[dstZoffset + img] * texelBytes
1721b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstYoffset * dstRowStride
172222108bb571808542b89677752d62d3901698265fBrian Paul            + dstXoffset * texelBytes;
1723f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         for (row = 0; row < srcHeight; row++) {
1724f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            for (col = 0; col < srcWidth; col++) {
1725f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               dstRow[col * 3 + 0] = srcRow[col * 4 + BCOMP];
1726f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               dstRow[col * 3 + 1] = srcRow[col * 4 + GCOMP];
1727f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               dstRow[col * 3 + 2] = srcRow[col * 4 + RCOMP];
1728f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            }
1729f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            dstRow += dstRowStride;
1730f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            srcRow += srcRowStride;
1731f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         }
1732f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
1733f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
17343aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   else if (!ctx->_ImageTransferState &&
17353aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell	    srcType == GL_UNSIGNED_BYTE &&
17363aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell	    can_swizzle(baseInternalFormat) &&
17373aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell	    can_swizzle(srcFormat)) {
17383aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell
17393aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      GLubyte dstmap[4];
17403aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell
17413aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      /* dstmap - how to swizzle from RGBA to dst format:
17423aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell       */
1743167ca59fe893a62e23e799f51608d18847dd590aKeith Whitwell      dstmap[0] = 2;
17443aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      dstmap[1] = 1;
1745167ca59fe893a62e23e799f51608d18847dd590aKeith Whitwell      dstmap[2] = 0;
17463aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      dstmap[3] = ONE;		/* ? */
17473aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell
17483aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      _mesa_swizzle_ubyte_image(ctx, dims,
17493aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				srcFormat,
17503aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				srcType,
17513aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				baseInternalFormat,
17523aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				dstmap, 3,
17533aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				dstAddr, dstXoffset, dstYoffset, dstZoffset,
17543aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				dstRowStride, dstImageOffsets,
17553aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				srcWidth, srcHeight, srcDepth, srcAddr,
17563aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				srcPacking);
17573aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   }
1758f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   else {
1759f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* general path */
17608f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paul      const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
1761f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 baseInternalFormat,
176222108bb571808542b89677752d62d3901698265fBrian Paul                                                 baseFormat,
1763f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcWidth, srcHeight, srcDepth,
1764f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcFormat, srcType, srcAddr,
1765f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcPacking);
17667c544d36850c6e3627adbbd66df9b12bbe0f185bBrian Paul      const GLchan *src = (const GLchan *) tempImage;
1767f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLint img, row, col;
1768f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (!tempImage)
1769f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         return GL_FALSE;
1770f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
1771f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      for (img = 0; img < srcDepth; img++) {
1772b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         GLubyte *dstRow = (GLubyte *) dstAddr
177322108bb571808542b89677752d62d3901698265fBrian Paul            + dstImageOffsets[dstZoffset + img] * texelBytes
1774b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstYoffset * dstRowStride
177522108bb571808542b89677752d62d3901698265fBrian Paul            + dstXoffset * texelBytes;
1776f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         for (row = 0; row < srcHeight; row++) {
1777f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul#if 0
1778f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            if (littleEndian) {
1779f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               for (col = 0; col < srcWidth; col++) {
1780f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                  dstRow[col * 3 + 0] = CHAN_TO_UBYTE(src[RCOMP]);
1781f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                  dstRow[col * 3 + 1] = CHAN_TO_UBYTE(src[GCOMP]);
1782f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                  dstRow[col * 3 + 2] = CHAN_TO_UBYTE(src[BCOMP]);
1783f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                  srcUB += 3;
1784f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               }
1785f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            }
1786f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            else {
1787f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               for (col = 0; col < srcWidth; col++) {
1788f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                  dstRow[col * 3 + 0] = srcUB[BCOMP];
1789f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                  dstRow[col * 3 + 1] = srcUB[GCOMP];
1790f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                  dstRow[col * 3 + 2] = srcUB[RCOMP];
1791f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                  srcUB += 3;
1792f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               }
1793f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            }
1794f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul#else
1795f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            for (col = 0; col < srcWidth; col++) {
1796f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               dstRow[col * 3 + 0] = CHAN_TO_UBYTE(src[BCOMP]);
1797f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               dstRow[col * 3 + 1] = CHAN_TO_UBYTE(src[GCOMP]);
1798f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               dstRow[col * 3 + 2] = CHAN_TO_UBYTE(src[RCOMP]);
1799f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               src += 3;
1800f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            }
1801f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul#endif
1802f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            dstRow += dstRowStride;
1803f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         }
1804f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
1805f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      _mesa_free((void *) tempImage);
1806f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
1807f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   return GL_TRUE;
1808f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul}
1809f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
1810f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
1811f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian PaulGLboolean
1812b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul_mesa_texstore_bgr888(TEXSTORE_PARAMS)
1813a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul{
1814184b5d89380e18008d64adfe1756dca9736426f2Brian Paul   const GLboolean littleEndian = _mesa_little_endian();
181522108bb571808542b89677752d62d3901698265fBrian Paul   const GLuint texelBytes = _mesa_get_format_bytes(dstFormat->MesaFormat);
181622108bb571808542b89677752d62d3901698265fBrian Paul   const GLenum baseFormat = _mesa_get_format_base_format(dstFormat->MesaFormat);
1817a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul
1818a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul   ASSERT(dstFormat == &_mesa_texformat_bgr888);
181922108bb571808542b89677752d62d3901698265fBrian Paul   ASSERT(texelBytes == 3);
1820a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul
1821a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul   if (!ctx->_ImageTransferState &&
1822a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul       !srcPacking->SwapBytes &&
1823a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul       baseInternalFormat == GL_RGB &&
1824a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul       srcFormat == GL_RGB &&
1825a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul       srcType == GL_UNSIGNED_BYTE &&
1826a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul       littleEndian) {
1827a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      /* simple memcpy path */
182817bcf9f816db3098db42acd7f0672f64554dd6a0Keith Whitwell      memcpy_texture(ctx, dims,
182960909388ab136d849d99eab49e782a53772a618fBrian Paul                     dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
1830b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstRowStride,
1831b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstImageOffsets,
1832a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul                     srcWidth, srcHeight, srcDepth, srcFormat, srcType,
1833a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul                     srcAddr, srcPacking);
1834a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul   }
1835a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul   else if (!ctx->_ImageTransferState &&
1836a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            !srcPacking->SwapBytes &&
1837a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            srcFormat == GL_RGBA &&
1838a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            srcType == GL_UNSIGNED_BYTE) {
1839a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      /* extract BGR from RGBA */
1840a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      int img, row, col;
1841a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      for (img = 0; img < srcDepth; img++) {
1842a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         const GLint srcRowStride = _mesa_image_row_stride(srcPacking,
1843a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul                                                 srcWidth, srcFormat, srcType);
184460909388ab136d849d99eab49e782a53772a618fBrian Paul         GLubyte *srcRow = (GLubyte *) _mesa_image_address(dims, srcPacking,
184560909388ab136d849d99eab49e782a53772a618fBrian Paul                  srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, 0, 0);
1846b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         GLubyte *dstRow = (GLubyte *) dstAddr
184722108bb571808542b89677752d62d3901698265fBrian Paul            + dstImageOffsets[dstZoffset + img] * texelBytes
1848b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstYoffset * dstRowStride
184922108bb571808542b89677752d62d3901698265fBrian Paul            + dstXoffset * texelBytes;
1850a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         for (row = 0; row < srcHeight; row++) {
1851a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            for (col = 0; col < srcWidth; col++) {
1852a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul               dstRow[col * 3 + 0] = srcRow[col * 4 + RCOMP];
1853a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul               dstRow[col * 3 + 1] = srcRow[col * 4 + GCOMP];
1854a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul               dstRow[col * 3 + 2] = srcRow[col * 4 + BCOMP];
1855a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            }
1856a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            dstRow += dstRowStride;
1857a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            srcRow += srcRowStride;
1858a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         }
1859a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      }
1860a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul   }
18613aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   else if (!ctx->_ImageTransferState &&
18623aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell	    srcType == GL_UNSIGNED_BYTE &&
18633aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell	    can_swizzle(baseInternalFormat) &&
18643aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell	    can_swizzle(srcFormat)) {
18653aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell
18663aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      GLubyte dstmap[4];
18673aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell
18683aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      /* dstmap - how to swizzle from RGBA to dst format:
18693aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell       */
1870167ca59fe893a62e23e799f51608d18847dd590aKeith Whitwell      dstmap[0] = 0;
18713aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      dstmap[1] = 1;
1872167ca59fe893a62e23e799f51608d18847dd590aKeith Whitwell      dstmap[2] = 2;
18733aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      dstmap[3] = ONE;		/* ? */
18743aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell
18753aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      _mesa_swizzle_ubyte_image(ctx, dims,
18763aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				srcFormat,
18773aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				srcType,
18783aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				baseInternalFormat,
18793aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				dstmap, 3,
18803aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				dstAddr, dstXoffset, dstYoffset, dstZoffset,
18813aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				dstRowStride, dstImageOffsets,
18823aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				srcWidth, srcHeight, srcDepth, srcAddr,
18833aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				srcPacking);
18843aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   }
1885a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul   else {
1886a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      /* general path */
1887a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
1888a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul                                                 baseInternalFormat,
188922108bb571808542b89677752d62d3901698265fBrian Paul                                                 baseFormat,
1890a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul                                                 srcWidth, srcHeight, srcDepth,
1891a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul                                                 srcFormat, srcType, srcAddr,
1892a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul                                                 srcPacking);
18937c544d36850c6e3627adbbd66df9b12bbe0f185bBrian Paul      const GLchan *src = (const GLchan *) tempImage;
1894a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      GLint img, row, col;
1895a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      if (!tempImage)
1896a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         return GL_FALSE;
1897a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
1898a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      for (img = 0; img < srcDepth; img++) {
1899b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         GLubyte *dstRow = (GLubyte *) dstAddr
190022108bb571808542b89677752d62d3901698265fBrian Paul            + dstImageOffsets[dstZoffset + img] * texelBytes
1901b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstYoffset * dstRowStride
190222108bb571808542b89677752d62d3901698265fBrian Paul            + dstXoffset * texelBytes;
1903a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         for (row = 0; row < srcHeight; row++) {
1904a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            for (col = 0; col < srcWidth; col++) {
1905a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul               dstRow[col * 3 + 0] = CHAN_TO_UBYTE(src[RCOMP]);
1906a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul               dstRow[col * 3 + 1] = CHAN_TO_UBYTE(src[GCOMP]);
1907a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul               dstRow[col * 3 + 2] = CHAN_TO_UBYTE(src[BCOMP]);
1908a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul               src += 3;
1909a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            }
1910a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            dstRow += dstRowStride;
1911a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         }
1912a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      }
1913a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      _mesa_free((void *) tempImage);
1914a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul   }
1915a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul   return GL_TRUE;
1916a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul}
1917a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul
1918dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas HellstromGLboolean
1919dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom_mesa_texstore_rgba4444(TEXSTORE_PARAMS)
1920dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom{
192122108bb571808542b89677752d62d3901698265fBrian Paul   const GLuint texelBytes = _mesa_get_format_bytes(dstFormat->MesaFormat);
192222108bb571808542b89677752d62d3901698265fBrian Paul   const GLenum baseFormat = _mesa_get_format_base_format(dstFormat->MesaFormat);
192322108bb571808542b89677752d62d3901698265fBrian Paul
1924dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom   ASSERT(dstFormat == &_mesa_texformat_rgba4444);
192522108bb571808542b89677752d62d3901698265fBrian Paul   ASSERT(texelBytes == 2);
1926dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom
1927dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom   if (!ctx->_ImageTransferState &&
1928dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom       !srcPacking->SwapBytes &&
1929dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom       dstFormat == &_mesa_texformat_rgba4444 &&
1930dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom       baseInternalFormat == GL_RGBA &&
1931dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom       srcFormat == GL_RGBA &&
1932dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom       srcType == GL_UNSIGNED_SHORT_4_4_4_4){
1933dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom      /* simple memcpy path */
1934dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom      memcpy_texture(ctx, dims,
1935dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom                     dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
1936dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom                     dstRowStride,
1937dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom                     dstImageOffsets,
1938dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom                     srcWidth, srcHeight, srcDepth, srcFormat, srcType,
1939dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom                     srcAddr, srcPacking);
1940dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom   }
1941dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom   else {
1942dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom      /* general path */
1943dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom      const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
1944dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom                                                 baseInternalFormat,
194522108bb571808542b89677752d62d3901698265fBrian Paul                                                 baseFormat,
1946dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom                                                 srcWidth, srcHeight, srcDepth,
1947dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom                                                 srcFormat, srcType, srcAddr,
1948dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom                                                 srcPacking);
1949dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom      const GLchan *src = tempImage;
1950dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom      GLint img, row, col;
1951dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom      if (!tempImage)
1952dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom         return GL_FALSE;
1953dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom      _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
1954dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom      for (img = 0; img < srcDepth; img++) {
1955dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom         GLubyte *dstRow = (GLubyte *) dstAddr
195622108bb571808542b89677752d62d3901698265fBrian Paul            + dstImageOffsets[dstZoffset + img] * texelBytes
1957dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom            + dstYoffset * dstRowStride
195822108bb571808542b89677752d62d3901698265fBrian Paul            + dstXoffset * texelBytes;
1959dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom         for (row = 0; row < srcHeight; row++) {
1960dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom            GLushort *dstUS = (GLushort *) dstRow;
1961dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom	    for (col = 0; col < srcWidth; col++) {
1962dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom	      dstUS[col] = PACK_COLOR_4444( CHAN_TO_UBYTE(src[RCOMP]),
1963dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom					    CHAN_TO_UBYTE(src[GCOMP]),
1964dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom					    CHAN_TO_UBYTE(src[BCOMP]),
1965dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom					    CHAN_TO_UBYTE(src[ACOMP]) );
1966dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom	      src += 4;
1967dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom            }
1968dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom            dstRow += dstRowStride;
1969dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom         }
1970dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom      }
1971dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom      _mesa_free((void *) tempImage);
1972dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom   }
1973dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom   return GL_TRUE;
1974dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom}
1975a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul
1976a156b49800c1419785d0709b78ef0d35e6dab5dfBrian PaulGLboolean
1977b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul_mesa_texstore_argb4444(TEXSTORE_PARAMS)
1978f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul{
197922108bb571808542b89677752d62d3901698265fBrian Paul   const GLuint texelBytes = _mesa_get_format_bytes(dstFormat->MesaFormat);
198022108bb571808542b89677752d62d3901698265fBrian Paul   const GLenum baseFormat = _mesa_get_format_base_format(dstFormat->MesaFormat);
198122108bb571808542b89677752d62d3901698265fBrian Paul
1982defb035b6cf03c555318d9dd48864242ed036f39Brian Paul   ASSERT(dstFormat == &_mesa_texformat_argb4444 ||
1983defb035b6cf03c555318d9dd48864242ed036f39Brian Paul          dstFormat == &_mesa_texformat_argb4444_rev);
198422108bb571808542b89677752d62d3901698265fBrian Paul   ASSERT(texelBytes == 2);
1985f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
1986f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (!ctx->_ImageTransferState &&
1987f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       !srcPacking->SwapBytes &&
1988defb035b6cf03c555318d9dd48864242ed036f39Brian Paul       dstFormat == &_mesa_texformat_argb4444 &&
1989f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       baseInternalFormat == GL_RGBA &&
1990f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       srcFormat == GL_BGRA &&
1991defb035b6cf03c555318d9dd48864242ed036f39Brian Paul       srcType == GL_UNSIGNED_SHORT_4_4_4_4_REV) {
1992f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* simple memcpy path */
199317bcf9f816db3098db42acd7f0672f64554dd6a0Keith Whitwell      memcpy_texture(ctx, dims,
199460909388ab136d849d99eab49e782a53772a618fBrian Paul                     dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
1995b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstRowStride,
1996b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstImageOffsets,
1997f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcWidth, srcHeight, srcDepth, srcFormat, srcType,
1998f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcAddr, srcPacking);
1999f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
2000f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   else {
2001f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* general path */
20028f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paul      const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
2003f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 baseInternalFormat,
200422108bb571808542b89677752d62d3901698265fBrian Paul                                                 baseFormat,
2005f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcWidth, srcHeight, srcDepth,
2006f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcFormat, srcType, srcAddr,
2007f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcPacking);
2008f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      const GLchan *src = tempImage;
2009f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLint img, row, col;
2010f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (!tempImage)
2011f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         return GL_FALSE;
2012f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
2013f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      for (img = 0; img < srcDepth; img++) {
2014b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         GLubyte *dstRow = (GLubyte *) dstAddr
201522108bb571808542b89677752d62d3901698265fBrian Paul            + dstImageOffsets[dstZoffset + img] * texelBytes
2016b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstYoffset * dstRowStride
201722108bb571808542b89677752d62d3901698265fBrian Paul            + dstXoffset * texelBytes;
2018f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         for (row = 0; row < srcHeight; row++) {
2019f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            GLushort *dstUS = (GLushort *) dstRow;
2020f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul            if (dstFormat == &_mesa_texformat_argb4444) {
2021f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               for (col = 0; col < srcWidth; col++) {
2022f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                  dstUS[col] = PACK_COLOR_4444( CHAN_TO_UBYTE(src[ACOMP]),
2023f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                CHAN_TO_UBYTE(src[RCOMP]),
2024f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                CHAN_TO_UBYTE(src[GCOMP]),
2025f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                CHAN_TO_UBYTE(src[BCOMP]) );
2026f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                  src += 4;
2027f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               }
2028f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            }
2029f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul            else {
2030f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               for (col = 0; col < srcWidth; col++) {
2031f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                  dstUS[col] = PACK_COLOR_4444_REV( CHAN_TO_UBYTE(src[ACOMP]),
2032f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                    CHAN_TO_UBYTE(src[RCOMP]),
2033f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                    CHAN_TO_UBYTE(src[GCOMP]),
2034f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                    CHAN_TO_UBYTE(src[BCOMP]) );
2035f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                  src += 4;
2036f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               }
2037a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            }
2038a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            dstRow += dstRowStride;
2039a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         }
2040a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      }
2041a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      _mesa_free((void *) tempImage);
2042a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul   }
2043a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul   return GL_TRUE;
2044a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul}
2045a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul
2046dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas HellstromGLboolean
2047dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom_mesa_texstore_rgba5551(TEXSTORE_PARAMS)
2048dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom{
204922108bb571808542b89677752d62d3901698265fBrian Paul   const GLuint texelBytes = _mesa_get_format_bytes(dstFormat->MesaFormat);
205022108bb571808542b89677752d62d3901698265fBrian Paul   const GLenum baseFormat = _mesa_get_format_base_format(dstFormat->MesaFormat);
205122108bb571808542b89677752d62d3901698265fBrian Paul
2052dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom   ASSERT(dstFormat == &_mesa_texformat_rgba5551);
205322108bb571808542b89677752d62d3901698265fBrian Paul   ASSERT(texelBytes == 2);
2054a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul
2055dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom   if (!ctx->_ImageTransferState &&
2056dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom       !srcPacking->SwapBytes &&
2057dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom       dstFormat == &_mesa_texformat_rgba5551 &&
2058dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom       baseInternalFormat == GL_RGBA &&
2059dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom       srcFormat == GL_RGBA &&
2060dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom       srcType == GL_UNSIGNED_SHORT_5_5_5_1) {
2061dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom      /* simple memcpy path */
2062dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom      memcpy_texture(ctx, dims,
2063dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom                     dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
2064dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom                     dstRowStride,
2065dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom                     dstImageOffsets,
2066dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom                     srcWidth, srcHeight, srcDepth, srcFormat, srcType,
2067dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom                     srcAddr, srcPacking);
2068dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom   }
2069dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom   else {
2070dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom      /* general path */
2071dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom      const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
2072dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom                                                 baseInternalFormat,
207322108bb571808542b89677752d62d3901698265fBrian Paul                                                 baseFormat,
2074dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom                                                 srcWidth, srcHeight, srcDepth,
2075dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom                                                 srcFormat, srcType, srcAddr,
2076dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom                                                 srcPacking);
2077dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom      const GLchan *src =tempImage;
2078dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom      GLint img, row, col;
2079dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom      if (!tempImage)
2080dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom         return GL_FALSE;
2081dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom      _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
2082dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom      for (img = 0; img < srcDepth; img++) {
2083dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom         GLubyte *dstRow = (GLubyte *) dstAddr
208422108bb571808542b89677752d62d3901698265fBrian Paul            + dstImageOffsets[dstZoffset + img] * texelBytes
2085dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom            + dstYoffset * dstRowStride
208622108bb571808542b89677752d62d3901698265fBrian Paul            + dstXoffset * texelBytes;
2087dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom         for (row = 0; row < srcHeight; row++) {
2088dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom            GLushort *dstUS = (GLushort *) dstRow;
2089dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom	    for (col = 0; col < srcWidth; col++) {
2090dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom	       dstUS[col] = PACK_COLOR_5551( CHAN_TO_UBYTE(src[RCOMP]),
2091dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom					     CHAN_TO_UBYTE(src[GCOMP]),
2092dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom					     CHAN_TO_UBYTE(src[BCOMP]),
2093dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom					     CHAN_TO_UBYTE(src[ACOMP]) );
2094dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom	      src += 4;
2095dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom	    }
2096dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom            dstRow += dstRowStride;
2097dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom         }
2098dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom      }
2099dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom      _mesa_free((void *) tempImage);
2100dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom   }
2101dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom   return GL_TRUE;
2102dbda49a9e65f684ca46c7af84cc52d63b0622978Thomas Hellstrom}
2103defb035b6cf03c555318d9dd48864242ed036f39Brian Paul
2104a156b49800c1419785d0709b78ef0d35e6dab5dfBrian PaulGLboolean
2105b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul_mesa_texstore_argb1555(TEXSTORE_PARAMS)
2106f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul{
210722108bb571808542b89677752d62d3901698265fBrian Paul   const GLuint texelBytes = _mesa_get_format_bytes(dstFormat->MesaFormat);
210822108bb571808542b89677752d62d3901698265fBrian Paul   const GLenum baseFormat = _mesa_get_format_base_format(dstFormat->MesaFormat);
210922108bb571808542b89677752d62d3901698265fBrian Paul
2110defb035b6cf03c555318d9dd48864242ed036f39Brian Paul   ASSERT(dstFormat == &_mesa_texformat_argb1555 ||
2111defb035b6cf03c555318d9dd48864242ed036f39Brian Paul          dstFormat == &_mesa_texformat_argb1555_rev);
211222108bb571808542b89677752d62d3901698265fBrian Paul   ASSERT(texelBytes == 2);
2113f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2114f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (!ctx->_ImageTransferState &&
2115f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       !srcPacking->SwapBytes &&
2116defb035b6cf03c555318d9dd48864242ed036f39Brian Paul       dstFormat == &_mesa_texformat_argb1555 &&
2117f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       baseInternalFormat == GL_RGBA &&
2118f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       srcFormat == GL_BGRA &&
2119defb035b6cf03c555318d9dd48864242ed036f39Brian Paul       srcType == GL_UNSIGNED_SHORT_1_5_5_5_REV) {
2120f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* simple memcpy path */
212117bcf9f816db3098db42acd7f0672f64554dd6a0Keith Whitwell      memcpy_texture(ctx, dims,
212260909388ab136d849d99eab49e782a53772a618fBrian Paul                     dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
2123b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstRowStride,
2124b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstImageOffsets,
2125f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcWidth, srcHeight, srcDepth, srcFormat, srcType,
2126f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcAddr, srcPacking);
2127f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
2128f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   else {
2129f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* general path */
21308f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paul      const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
2131f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 baseInternalFormat,
213222108bb571808542b89677752d62d3901698265fBrian Paul                                                 baseFormat,
2133f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcWidth, srcHeight, srcDepth,
2134f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcFormat, srcType, srcAddr,
2135f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcPacking);
2136f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      const GLchan *src =tempImage;
2137f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLint img, row, col;
2138f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (!tempImage)
2139f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         return GL_FALSE;
2140f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
2141f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      for (img = 0; img < srcDepth; img++) {
2142b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         GLubyte *dstRow = (GLubyte *) dstAddr
214322108bb571808542b89677752d62d3901698265fBrian Paul            + dstImageOffsets[dstZoffset + img] * texelBytes
2144b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstYoffset * dstRowStride
214522108bb571808542b89677752d62d3901698265fBrian Paul            + dstXoffset * texelBytes;
2146f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         for (row = 0; row < srcHeight; row++) {
2147f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            GLushort *dstUS = (GLushort *) dstRow;
2148f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul            if (dstFormat == &_mesa_texformat_argb1555) {
2149f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               for (col = 0; col < srcWidth; col++) {
2150f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                  dstUS[col] = PACK_COLOR_1555( CHAN_TO_UBYTE(src[ACOMP]),
2151f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                CHAN_TO_UBYTE(src[RCOMP]),
2152f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                CHAN_TO_UBYTE(src[GCOMP]),
2153f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                CHAN_TO_UBYTE(src[BCOMP]) );
2154f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                  src += 4;
2155f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               }
2156f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            }
2157f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul            else {
2158f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               for (col = 0; col < srcWidth; col++) {
2159f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                  dstUS[col] = PACK_COLOR_1555_REV( CHAN_TO_UBYTE(src[ACOMP]),
2160f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                    CHAN_TO_UBYTE(src[RCOMP]),
2161f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                    CHAN_TO_UBYTE(src[GCOMP]),
2162f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                    CHAN_TO_UBYTE(src[BCOMP]) );
2163f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                  src += 4;
2164f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               }
2165a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            }
2166a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            dstRow += dstRowStride;
2167a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         }
2168a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      }
2169a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      _mesa_free((void *) tempImage);
2170a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul   }
2171a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul   return GL_TRUE;
2172a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul}
2173a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul
2174f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2175f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian PaulGLboolean
2176b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul_mesa_texstore_al88(TEXSTORE_PARAMS)
2177f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul{
2178184b5d89380e18008d64adfe1756dca9736426f2Brian Paul   const GLboolean littleEndian = _mesa_little_endian();
217922108bb571808542b89677752d62d3901698265fBrian Paul   const GLuint texelBytes = _mesa_get_format_bytes(dstFormat->MesaFormat);
218022108bb571808542b89677752d62d3901698265fBrian Paul   const GLenum baseFormat = _mesa_get_format_base_format(dstFormat->MesaFormat);
2181f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2182defb035b6cf03c555318d9dd48864242ed036f39Brian Paul   ASSERT(dstFormat == &_mesa_texformat_al88 ||
2183defb035b6cf03c555318d9dd48864242ed036f39Brian Paul          dstFormat == &_mesa_texformat_al88_rev);
218422108bb571808542b89677752d62d3901698265fBrian Paul   ASSERT(texelBytes == 2);
2185f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2186f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (!ctx->_ImageTransferState &&
2187f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       !srcPacking->SwapBytes &&
2188defb035b6cf03c555318d9dd48864242ed036f39Brian Paul       dstFormat == &_mesa_texformat_al88 &&
2189f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       baseInternalFormat == GL_LUMINANCE_ALPHA &&
2190f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       srcFormat == GL_LUMINANCE_ALPHA &&
2191f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       srcType == GL_UNSIGNED_BYTE &&
2192f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       littleEndian) {
2193f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* simple memcpy path */
219417bcf9f816db3098db42acd7f0672f64554dd6a0Keith Whitwell      memcpy_texture(ctx, dims,
219560909388ab136d849d99eab49e782a53772a618fBrian Paul                     dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
2196b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstRowStride,
2197b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstImageOffsets,
2198f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcWidth, srcHeight, srcDepth, srcFormat, srcType,
2199f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcAddr, srcPacking);
2200f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
22013aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   else if (!ctx->_ImageTransferState &&
2202bad5cf056ac653e97d96114d42b874f15a3c2ec8Keith Whitwell	    littleEndian &&
22033aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell	    srcType == GL_UNSIGNED_BYTE &&
22043aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell	    can_swizzle(baseInternalFormat) &&
22053aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell	    can_swizzle(srcFormat)) {
22063aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell
22073aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      GLubyte dstmap[4];
22083aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell
22093aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      /* dstmap - how to swizzle from RGBA to dst format:
22103aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell       */
2211df8632ebd87219c809810d993f56fef1e6853a25Michel Dänzer      if ((littleEndian && dstFormat == &_mesa_texformat_al88) ||
2212df8632ebd87219c809810d993f56fef1e6853a25Michel Dänzer	  (!littleEndian && dstFormat == &_mesa_texformat_al88_rev)) {
22133aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell	 dstmap[0] = 0;
22143aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell	 dstmap[1] = 3;
22153aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      }
22163aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      else {
22173aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell	 dstmap[0] = 3;
22183aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell	 dstmap[1] = 0;
22193aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      }
22203aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      dstmap[2] = ZERO;		/* ? */
22213aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      dstmap[3] = ONE;		/* ? */
22223aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell
22233aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      _mesa_swizzle_ubyte_image(ctx, dims,
22243aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				srcFormat,
22253aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				srcType,
22263aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				baseInternalFormat,
22273aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				dstmap, 2,
22283aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				dstAddr, dstXoffset, dstYoffset, dstZoffset,
22293aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				dstRowStride, dstImageOffsets,
22303aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				srcWidth, srcHeight, srcDepth, srcAddr,
22313aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				srcPacking);
22323aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   }
2233f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   else {
2234f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* general path */
22358f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paul      const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
2236f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 baseInternalFormat,
223722108bb571808542b89677752d62d3901698265fBrian Paul                                                 baseFormat,
2238f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcWidth, srcHeight, srcDepth,
2239f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcFormat, srcType, srcAddr,
2240f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcPacking);
2241f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      const GLchan *src = tempImage;
2242f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLint img, row, col;
2243f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (!tempImage)
2244f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         return GL_FALSE;
2245f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
2246f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      for (img = 0; img < srcDepth; img++) {
2247b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         GLubyte *dstRow = (GLubyte *) dstAddr
224822108bb571808542b89677752d62d3901698265fBrian Paul            + dstImageOffsets[dstZoffset + img] * texelBytes
2249b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstYoffset * dstRowStride
225022108bb571808542b89677752d62d3901698265fBrian Paul            + dstXoffset * texelBytes;
2251f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         for (row = 0; row < srcHeight; row++) {
2252f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            GLushort *dstUS = (GLushort *) dstRow;
2253f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul            if (dstFormat == &_mesa_texformat_al88) {
2254f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               for (col = 0; col < srcWidth; col++) {
2255f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                  /* src[0] is luminance, src[1] is alpha */
2256f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                 dstUS[col] = PACK_COLOR_88( CHAN_TO_UBYTE(src[1]),
2257f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                             CHAN_TO_UBYTE(src[0]) );
2258f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                 src += 2;
2259f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               }
2260f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            }
2261f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul            else {
2262f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               for (col = 0; col < srcWidth; col++) {
2263f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                  /* src[0] is luminance, src[1] is alpha */
2264f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                 dstUS[col] = PACK_COLOR_88_REV( CHAN_TO_UBYTE(src[1]),
2265f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                                                 CHAN_TO_UBYTE(src[0]) );
2266f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul                 src += 2;
2267f252f64430ccb957698fcf85e84c9d64008147ebBrian Paul               }
2268a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            }
2269a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul            dstRow += dstRowStride;
2270a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul         }
2271a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      }
2272a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul      _mesa_free((void *) tempImage);
2273a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul   }
2274a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul   return GL_TRUE;
2275a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul}
2276a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul
2277a156b49800c1419785d0709b78ef0d35e6dab5dfBrian Paul
2278a156b49800c1419785d0709b78ef0d35e6dab5dfBrian PaulGLboolean
2279b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul_mesa_texstore_rgb332(TEXSTORE_PARAMS)
2280f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul{
228122108bb571808542b89677752d62d3901698265fBrian Paul   const GLuint texelBytes = _mesa_get_format_bytes(dstFormat->MesaFormat);
228222108bb571808542b89677752d62d3901698265fBrian Paul   const GLenum baseFormat = _mesa_get_format_base_format(dstFormat->MesaFormat);
228322108bb571808542b89677752d62d3901698265fBrian Paul
2284f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   ASSERT(dstFormat == &_mesa_texformat_rgb332);
228522108bb571808542b89677752d62d3901698265fBrian Paul   ASSERT(texelBytes == 1);
2286f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2287f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (!ctx->_ImageTransferState &&
2288f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       !srcPacking->SwapBytes &&
2289f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       baseInternalFormat == GL_RGB &&
2290f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       srcFormat == GL_RGB && srcType == GL_UNSIGNED_BYTE_3_3_2) {
2291f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* simple memcpy path */
229217bcf9f816db3098db42acd7f0672f64554dd6a0Keith Whitwell      memcpy_texture(ctx, dims,
229360909388ab136d849d99eab49e782a53772a618fBrian Paul                     dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
2294b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstRowStride,
2295b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstImageOffsets,
2296f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcWidth, srcHeight, srcDepth, srcFormat, srcType,
2297f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcAddr, srcPacking);
2298f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
2299f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   else {
2300f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* general path */
23018f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paul      const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
2302f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 baseInternalFormat,
230322108bb571808542b89677752d62d3901698265fBrian Paul                                                 baseFormat,
2304f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcWidth, srcHeight, srcDepth,
2305f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcFormat, srcType, srcAddr,
2306f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcPacking);
2307f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      const GLchan *src = tempImage;
2308f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLint img, row, col;
2309f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (!tempImage)
2310f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         return GL_FALSE;
2311f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
2312f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      for (img = 0; img < srcDepth; img++) {
2313b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         GLubyte *dstRow = (GLubyte *) dstAddr
231422108bb571808542b89677752d62d3901698265fBrian Paul            + dstImageOffsets[dstZoffset + img] * texelBytes
2315b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstYoffset * dstRowStride
231622108bb571808542b89677752d62d3901698265fBrian Paul            + dstXoffset * texelBytes;
2317f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         for (row = 0; row < srcHeight; row++) {
2318f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            for (col = 0; col < srcWidth; col++) {
2319f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               dstRow[col] = PACK_COLOR_332( CHAN_TO_UBYTE(src[RCOMP]),
2320f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                             CHAN_TO_UBYTE(src[GCOMP]),
2321f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                             CHAN_TO_UBYTE(src[BCOMP]) );
2322f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               src += 3;
2323f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            }
2324f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            dstRow += dstRowStride;
2325f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         }
2326f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
2327f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      _mesa_free((void *) tempImage);
2328f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
2329f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   return GL_TRUE;
2330f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul}
2331f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2332f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2333f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul/**
2334f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * Texstore for _mesa_texformat_a8, _mesa_texformat_l8, _mesa_texformat_i8.
2335f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul */
2336f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian PaulGLboolean
2337b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul_mesa_texstore_a8(TEXSTORE_PARAMS)
2338f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul{
233922108bb571808542b89677752d62d3901698265fBrian Paul   const GLuint texelBytes = _mesa_get_format_bytes(dstFormat->MesaFormat);
234022108bb571808542b89677752d62d3901698265fBrian Paul   const GLenum baseFormat = _mesa_get_format_base_format(dstFormat->MesaFormat);
234122108bb571808542b89677752d62d3901698265fBrian Paul
2342f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   ASSERT(dstFormat == &_mesa_texformat_a8 ||
2343f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          dstFormat == &_mesa_texformat_l8 ||
2344f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          dstFormat == &_mesa_texformat_i8);
234522108bb571808542b89677752d62d3901698265fBrian Paul   ASSERT(texelBytes == 1);
2346f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2347f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (!ctx->_ImageTransferState &&
2348f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       !srcPacking->SwapBytes &&
2349f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       baseInternalFormat == srcFormat &&
2350f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       srcType == GL_UNSIGNED_BYTE) {
2351f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* simple memcpy path */
235217bcf9f816db3098db42acd7f0672f64554dd6a0Keith Whitwell      memcpy_texture(ctx, dims,
235360909388ab136d849d99eab49e782a53772a618fBrian Paul                     dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
2354b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstRowStride,
2355b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstImageOffsets,
2356f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcWidth, srcHeight, srcDepth, srcFormat, srcType,
2357f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcAddr, srcPacking);
2358f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
23593aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   else if (!ctx->_ImageTransferState &&
23603aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell	    srcType == GL_UNSIGNED_BYTE &&
23613aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell	    can_swizzle(baseInternalFormat) &&
23623aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell	    can_swizzle(srcFormat)) {
23633aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell
23643aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      GLubyte dstmap[4];
23653aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell
23663aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      /* dstmap - how to swizzle from RGBA to dst format:
23673aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell       */
23683aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      if (dstFormat == &_mesa_texformat_a8) {
23693aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell	 dstmap[0] = 3;
23703aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      }
23713aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      else {
23723aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell	 dstmap[0] = 0;
23733aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      }
23743aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      dstmap[1] = ZERO;		/* ? */
23753aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      dstmap[2] = ZERO;		/* ? */
23763aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      dstmap[3] = ONE;		/* ? */
23773aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell
23783aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell      _mesa_swizzle_ubyte_image(ctx, dims,
23793aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				srcFormat,
23803aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				srcType,
23813aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				baseInternalFormat,
23823aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				dstmap, 1,
23833aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				dstAddr, dstXoffset, dstYoffset, dstZoffset,
23843aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				dstRowStride, dstImageOffsets,
23853aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				srcWidth, srcHeight, srcDepth, srcAddr,
23863aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell				srcPacking);
23873aea82b396387bf3835f91bcc9121e02274c4c04Keith Whitwell   }
2388f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   else {
2389f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* general path */
23908f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paul      const GLchan *tempImage = _mesa_make_temp_chan_image(ctx, dims,
2391f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 baseInternalFormat,
239222108bb571808542b89677752d62d3901698265fBrian Paul                                                 baseFormat,
2393f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcWidth, srcHeight, srcDepth,
2394f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcFormat, srcType, srcAddr,
2395f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcPacking);
2396f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      const GLchan *src = tempImage;
2397f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLint img, row, col;
2398f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (!tempImage)
2399f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         return GL_FALSE;
2400f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
2401f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      for (img = 0; img < srcDepth; img++) {
2402b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         GLubyte *dstRow = (GLubyte *) dstAddr
240322108bb571808542b89677752d62d3901698265fBrian Paul            + dstImageOffsets[dstZoffset + img] * texelBytes
2404b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstYoffset * dstRowStride
240522108bb571808542b89677752d62d3901698265fBrian Paul            + dstXoffset * texelBytes;
2406f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         for (row = 0; row < srcHeight; row++) {
2407f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            for (col = 0; col < srcWidth; col++) {
2408f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               dstRow[col] = CHAN_TO_UBYTE(src[col]);
2409f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            }
2410f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            dstRow += dstRowStride;
2411f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            src += srcWidth;
2412f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         }
2413f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
2414f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      _mesa_free((void *) tempImage);
2415f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
2416f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   return GL_TRUE;
2417f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul}
2418f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2419f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2420f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2421f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian PaulGLboolean
2422b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul_mesa_texstore_ci8(TEXSTORE_PARAMS)
2423f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul{
242422108bb571808542b89677752d62d3901698265fBrian Paul   const GLuint texelBytes = _mesa_get_format_bytes(dstFormat->MesaFormat);
242522108bb571808542b89677752d62d3901698265fBrian Paul
2426a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) dims; (void) baseInternalFormat;
2427f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   ASSERT(dstFormat == &_mesa_texformat_ci8);
242822108bb571808542b89677752d62d3901698265fBrian Paul   ASSERT(texelBytes == 1);
2429f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   ASSERT(baseInternalFormat == GL_COLOR_INDEX);
2430f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2431f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (!ctx->_ImageTransferState &&
2432f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       !srcPacking->SwapBytes &&
2433f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       srcFormat == GL_COLOR_INDEX &&
2434f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       srcType == GL_UNSIGNED_BYTE) {
2435f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* simple memcpy path */
243617bcf9f816db3098db42acd7f0672f64554dd6a0Keith Whitwell      memcpy_texture(ctx, dims,
243760909388ab136d849d99eab49e782a53772a618fBrian Paul                     dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
2438b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstRowStride,
2439b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstImageOffsets,
2440f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcWidth, srcHeight, srcDepth, srcFormat, srcType,
2441f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcAddr, srcPacking);
2442f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
2443f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   else {
2444f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* general path */
2445f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLint img, row;
2446f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      for (img = 0; img < srcDepth; img++) {
2447b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         GLubyte *dstRow = (GLubyte *) dstAddr
244822108bb571808542b89677752d62d3901698265fBrian Paul            + dstImageOffsets[dstZoffset + img] * texelBytes
2449b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstYoffset * dstRowStride
245022108bb571808542b89677752d62d3901698265fBrian Paul            + dstXoffset * texelBytes;
2451f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         for (row = 0; row < srcHeight; row++) {
245260909388ab136d849d99eab49e782a53772a618fBrian Paul            const GLvoid *src = _mesa_image_address(dims, srcPacking,
2453f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, row, 0);
2454f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            _mesa_unpack_index_span(ctx, srcWidth, GL_UNSIGNED_BYTE, dstRow,
2455f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                    srcType, src, srcPacking,
2456f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                    ctx->_ImageTransferState);
2457f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            dstRow += dstRowStride;
2458f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         }
2459f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
2460f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
2461f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   return GL_TRUE;
2462f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul}
2463f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2464f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2465f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul/**
2466f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * Texstore for _mesa_texformat_ycbcr or _mesa_texformat_ycbcr_rev.
2467f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul */
2468f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian PaulGLboolean
2469b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul_mesa_texstore_ycbcr(TEXSTORE_PARAMS)
2470f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul{
2471184b5d89380e18008d64adfe1756dca9736426f2Brian Paul   const GLboolean littleEndian = _mesa_little_endian();
247222108bb571808542b89677752d62d3901698265fBrian Paul   const GLuint texelBytes = _mesa_get_format_bytes(dstFormat->MesaFormat);
247322108bb571808542b89677752d62d3901698265fBrian Paul
2474a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) ctx; (void) dims; (void) baseInternalFormat;
2475f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2476f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   ASSERT((dstFormat == &_mesa_texformat_ycbcr) ||
2477f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          (dstFormat == &_mesa_texformat_ycbcr_rev));
247822108bb571808542b89677752d62d3901698265fBrian Paul   ASSERT(texelBytes == 2);
2479f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   ASSERT(ctx->Extensions.MESA_ycbcr_texture);
2480f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   ASSERT(srcFormat == GL_YCBCR_MESA);
2481f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   ASSERT((srcType == GL_UNSIGNED_SHORT_8_8_MESA) ||
2482f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          (srcType == GL_UNSIGNED_SHORT_8_8_REV_MESA));
2483f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   ASSERT(baseInternalFormat == GL_YCBCR_MESA);
2484f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2485f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   /* always just memcpy since no pixel transfer ops apply */
248617bcf9f816db3098db42acd7f0672f64554dd6a0Keith Whitwell   memcpy_texture(ctx, dims,
248760909388ab136d849d99eab49e782a53772a618fBrian Paul                  dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
2488b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                  dstRowStride,
2489b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                  dstImageOffsets,
2490f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                  srcWidth, srcHeight, srcDepth, srcFormat, srcType,
2491f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                  srcAddr, srcPacking);
2492f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2493f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   /* Check if we need byte swapping */
2494f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   /* XXX the logic here _might_ be wrong */
2495f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (srcPacking->SwapBytes ^
2496f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       (srcType == GL_UNSIGNED_SHORT_8_8_REV_MESA) ^
2497f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       (dstFormat == &_mesa_texformat_ycbcr_rev) ^
2498f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       !littleEndian) {
2499f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLint img, row;
2500f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      for (img = 0; img < srcDepth; img++) {
2501b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         GLubyte *dstRow = (GLubyte *) dstAddr
250222108bb571808542b89677752d62d3901698265fBrian Paul            + dstImageOffsets[dstZoffset + img] * texelBytes
2503b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstYoffset * dstRowStride
250422108bb571808542b89677752d62d3901698265fBrian Paul            + dstXoffset * texelBytes;
2505f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         for (row = 0; row < srcHeight; row++) {
2506b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            _mesa_swap2((GLushort *) dstRow, srcWidth);
2507b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            dstRow += dstRowStride;
2508f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         }
2509f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
2510f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
2511f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   return GL_TRUE;
2512f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul}
2513f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2514114152e068ec919feb0a57a1259c2ada970b9f02Roland ScheideggerGLboolean
2515114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger_mesa_texstore_dudv8(TEXSTORE_PARAMS)
2516114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger{
2517114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger   const GLboolean littleEndian = _mesa_little_endian();
251822108bb571808542b89677752d62d3901698265fBrian Paul   const GLuint texelBytes = _mesa_get_format_bytes(dstFormat->MesaFormat);
2519114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger
2520114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger   ASSERT(dstFormat == &_mesa_texformat_dudv8);
252122108bb571808542b89677752d62d3901698265fBrian Paul   ASSERT(texelBytes == 2);
2522114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger   ASSERT(ctx->Extensions.ATI_envmap_bumpmap);
2523114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger   ASSERT((srcFormat == GL_DU8DV8_ATI) ||
2524114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger	  (srcFormat == GL_DUDV_ATI));
2525114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger   ASSERT(baseInternalFormat == GL_DUDV_ATI);
2526114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger
2527114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger   if (!srcPacking->SwapBytes && srcType == GL_BYTE &&
2528114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger       littleEndian) {
2529114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      /* simple memcpy path */
2530114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      memcpy_texture(ctx, dims,
2531114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger                     dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
2532114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger                     dstRowStride,
2533114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger                     dstImageOffsets,
2534114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger                     srcWidth, srcHeight, srcDepth, srcFormat, srcType,
2535114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger                     srcAddr, srcPacking);
2536114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger   }
2537114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger   else if (srcType == GL_BYTE) {
2538114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger
2539114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      GLubyte dstmap[4];
2540114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger
2541114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      /* dstmap - how to swizzle from RGBA to dst format:
2542114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger       */
2543114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      if (littleEndian) {
2544114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger	 dstmap[0] = 0;
2545114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger	 dstmap[1] = 3;
2546114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      }
2547114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      else {
2548114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger	 dstmap[0] = 3;
2549114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger	 dstmap[1] = 0;
2550114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      }
2551114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      dstmap[2] = ZERO;		/* ? */
2552114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      dstmap[3] = ONE;		/* ? */
2553114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger
2554114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      _mesa_swizzle_ubyte_image(ctx, dims,
2555114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger				GL_LUMINANCE_ALPHA, /* hack */
2556114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger				GL_UNSIGNED_BYTE, /* hack */
2557114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger				GL_LUMINANCE_ALPHA, /* hack */
2558114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger				dstmap, 2,
2559114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger				dstAddr, dstXoffset, dstYoffset, dstZoffset,
2560114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger				dstRowStride, dstImageOffsets,
2561114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger				srcWidth, srcHeight, srcDepth, srcAddr,
2562114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger				srcPacking);
2563114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger   }
2564114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger   else {
2565114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      /* general path - note this is defined for 2d textures only */
2566114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      const GLint components = _mesa_components_in_format(baseInternalFormat);
2567114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      const GLint srcStride = _mesa_image_row_stride(srcPacking,
2568114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger                                                 srcWidth, srcFormat, srcType);
2569114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      GLbyte *tempImage, *dst, *src;
2570114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      GLint row;
2571114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger
2572114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      tempImage = (GLbyte *) _mesa_malloc(srcWidth * srcHeight * srcDepth
2573114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger                                          * components * sizeof(GLbyte));
2574114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      if (!tempImage)
2575114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger         return GL_FALSE;
2576114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger
2577114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      src = (GLbyte *) _mesa_image_address(dims, srcPacking, srcAddr,
2578114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger                                           srcWidth, srcHeight,
2579114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger                                           srcFormat, srcType,
2580114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger                                           0, 0, 0);
2581114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger
2582114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      dst = tempImage;
2583114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      for (row = 0; row < srcHeight; row++) {
2584114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger         _mesa_unpack_dudv_span_byte(ctx, srcWidth, baseInternalFormat,
2585114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger                                     dst, srcFormat, srcType, src,
2586114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger                                     srcPacking, 0);
2587114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger         dst += srcWidth * components;
2588114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger         src += srcStride;
2589114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      }
2590114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger
2591114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      src = tempImage;
2592114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      dst = (GLbyte *) dstAddr
2593114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger            + dstYoffset * dstRowStride
259422108bb571808542b89677752d62d3901698265fBrian Paul            + dstXoffset * texelBytes;
2595114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      for (row = 0; row < srcHeight; row++) {
259622108bb571808542b89677752d62d3901698265fBrian Paul         memcpy(dst, src, srcWidth * texelBytes);
2597114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger         dst += dstRowStride;
259822108bb571808542b89677752d62d3901698265fBrian Paul         src += srcWidth * texelBytes;
2599114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      }
2600114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger      _mesa_free((void *) tempImage);
2601114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger   }
2602114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger   return GL_TRUE;
2603114152e068ec919feb0a57a1259c2ada970b9f02Roland Scheidegger}
2604f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2605c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger/**
2606bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger * Store a texture in MESA_FORMAT_SIGNED_RGBA8888 or MESA_FORMAT_SIGNED_RGBA8888_REV
2607c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger */
2608c6a6cc191813e8343a17b028146a34f193a6ce44Roland ScheideggerGLboolean
2609c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger_mesa_texstore_signed_rgba8888(TEXSTORE_PARAMS)
2610c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger{
2611c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger   const GLboolean littleEndian = _mesa_little_endian();
261222108bb571808542b89677752d62d3901698265fBrian Paul   const GLuint texelBytes = _mesa_get_format_bytes(dstFormat->MesaFormat);
261322108bb571808542b89677752d62d3901698265fBrian Paul   const GLenum baseFormat = _mesa_get_format_base_format(dstFormat->MesaFormat);
2614c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger
2615bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger   ASSERT(dstFormat == &_mesa_texformat_signed_rgba8888 ||
2616bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger          dstFormat == &_mesa_texformat_signed_rgba8888_rev);
261722108bb571808542b89677752d62d3901698265fBrian Paul   ASSERT(texelBytes == 4);
2618c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger
2619c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger   if (!ctx->_ImageTransferState &&
2620c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger       !srcPacking->SwapBytes &&
2621c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger       dstFormat == &_mesa_texformat_signed_rgba8888 &&
2622c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger       baseInternalFormat == GL_RGBA &&
2623c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger      ((srcFormat == GL_RGBA && srcType == GL_BYTE && !littleEndian) ||
2624c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger       (srcFormat == GL_ABGR_EXT && srcType == GL_BYTE && littleEndian))) {
2625c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger       /* simple memcpy path */
2626c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger      memcpy_texture(ctx, dims,
2627c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger                     dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
2628c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger                     dstRowStride,
2629c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger                     dstImageOffsets,
2630c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger                     srcWidth, srcHeight, srcDepth, srcFormat, srcType,
2631c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger                     srcAddr, srcPacking);
2632c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger   }
2633c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger   else if (!ctx->_ImageTransferState &&
2634bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger       !srcPacking->SwapBytes &&
2635bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger       dstFormat == &_mesa_texformat_signed_rgba8888_rev &&
2636bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger       baseInternalFormat == GL_RGBA &&
2637bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger      ((srcFormat == GL_RGBA && srcType == GL_BYTE && littleEndian) ||
2638bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger       (srcFormat == GL_ABGR_EXT && srcType == GL_BYTE && !littleEndian))) {
2639bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger      /* simple memcpy path */
2640bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger      memcpy_texture(ctx, dims,
2641bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger                     dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
2642bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger                     dstRowStride,
2643bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger                     dstImageOffsets,
2644bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger                     srcWidth, srcHeight, srcDepth, srcFormat, srcType,
2645bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger                     srcAddr, srcPacking);
2646bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger   }
2647bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger   else if (!ctx->_ImageTransferState &&
2648c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger	    (srcType == GL_BYTE) &&
2649c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger	    can_swizzle(baseInternalFormat) &&
2650c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger	    can_swizzle(srcFormat)) {
2651c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger
2652c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger      GLubyte dstmap[4];
2653c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger
2654c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger      /* dstmap - how to swizzle from RGBA to dst format:
2655c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger       */
2656bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger      if ((littleEndian && dstFormat == &_mesa_texformat_signed_rgba8888) ||
2657bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger	  (!littleEndian && dstFormat == &_mesa_texformat_signed_rgba8888_rev)) {
2658c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger	 dstmap[3] = 0;
2659c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger	 dstmap[2] = 1;
2660c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger	 dstmap[1] = 2;
2661c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger	 dstmap[0] = 3;
2662c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger      }
2663c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger      else {
2664c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger	 dstmap[3] = 3;
2665c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger	 dstmap[2] = 2;
2666c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger	 dstmap[1] = 1;
2667c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger	 dstmap[0] = 0;
2668c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger      }
2669c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger
2670c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger      _mesa_swizzle_ubyte_image(ctx, dims,
2671c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger				srcFormat,
2672c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger				srcType,
2673c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger				baseInternalFormat,
2674c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger				dstmap, 4,
2675c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger				dstAddr, dstXoffset, dstYoffset, dstZoffset,
2676c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger				dstRowStride, dstImageOffsets,
2677c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger				srcWidth, srcHeight, srcDepth, srcAddr,
2678c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger				srcPacking);
2679c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger   }
2680c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger   else {
2681c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger      /* general path */
2682c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger      const GLfloat *tempImage = make_temp_float_image(ctx, dims,
2683c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger                                                 baseInternalFormat,
268422108bb571808542b89677752d62d3901698265fBrian Paul                                                 baseFormat,
2685c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger                                                 srcWidth, srcHeight, srcDepth,
2686c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger                                                 srcFormat, srcType, srcAddr,
2687c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger                                                 srcPacking);
2688c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger      const GLfloat *srcRow = tempImage;
2689c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger      GLint img, row, col;
2690c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger      if (!tempImage)
2691c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger         return GL_FALSE;
2692c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger      _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
2693c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger      for (img = 0; img < srcDepth; img++) {
2694c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger         GLubyte *dstRow = (GLubyte *) dstAddr
269522108bb571808542b89677752d62d3901698265fBrian Paul            + dstImageOffsets[dstZoffset + img] * texelBytes
2696c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger            + dstYoffset * dstRowStride
269722108bb571808542b89677752d62d3901698265fBrian Paul            + dstXoffset * texelBytes;
2698c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger         for (row = 0; row < srcHeight; row++) {
2699c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger            GLuint *dstUI = (GLuint *) dstRow;
2700c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger            if (dstFormat == &_mesa_texformat_signed_rgba8888) {
2701c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger               for (col = 0; col < srcWidth; col++) {
2702c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger                  dstUI[col] = PACK_COLOR_8888( FLOAT_TO_BYTE_TEX(srcRow[RCOMP]),
2703c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger                                                FLOAT_TO_BYTE_TEX(srcRow[GCOMP]),
2704c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger                                                FLOAT_TO_BYTE_TEX(srcRow[BCOMP]),
2705c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger                                                FLOAT_TO_BYTE_TEX(srcRow[ACOMP]) );
2706c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger                  srcRow += 4;
2707c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger               }
2708c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger            }
2709bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger            else {
2710bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger               for (col = 0; col < srcWidth; col++) {
2711bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger                  dstUI[col] = PACK_COLOR_8888_REV( FLOAT_TO_BYTE_TEX(srcRow[RCOMP]),
2712bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger                                                    FLOAT_TO_BYTE_TEX(srcRow[GCOMP]),
2713bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger                                                    FLOAT_TO_BYTE_TEX(srcRow[BCOMP]),
2714bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger                                                    FLOAT_TO_BYTE_TEX(srcRow[ACOMP]) );
2715bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger                  srcRow += 4;
2716bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger               }
2717bb386a1ecae6d7f805af44df463b0e4d661eef85Roland Scheidegger            }
2718c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger            dstRow += dstRowStride;
2719c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger         }
2720c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger      }
2721c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger      _mesa_free((void *) tempImage);
2722c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger   }
2723c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger   return GL_TRUE;
2724c6a6cc191813e8343a17b028146a34f193a6ce44Roland Scheidegger}
2725f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2726184a9707227ab024d65d352fe7c09b3e287348e9Brian Paul/**
2727184a9707227ab024d65d352fe7c09b3e287348e9Brian Paul * Store a combined depth/stencil texture image.
2728184a9707227ab024d65d352fe7c09b3e287348e9Brian Paul */
2729184a9707227ab024d65d352fe7c09b3e287348e9Brian PaulGLboolean
2730b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul_mesa_texstore_z24_s8(TEXSTORE_PARAMS)
2731184a9707227ab024d65d352fe7c09b3e287348e9Brian Paul{
273230640695400b9b27656893753ae6b62f2082ce9bBrian Paul   const GLfloat depthScale = (GLfloat) 0xffffff;
2733c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz   const GLint srcRowStride
2734c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz      = _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType)
2735c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz      / sizeof(GLuint);
2736c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz   GLint img, row;
273725cfb68f0b3baf0e74d1f6a37afab46370f6711bBrian
2738184a9707227ab024d65d352fe7c09b3e287348e9Brian Paul   ASSERT(dstFormat == &_mesa_texformat_z24_s8);
2739c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz   ASSERT(srcFormat == GL_DEPTH_STENCIL_EXT || srcFormat == GL_DEPTH_COMPONENT);
2740c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz   ASSERT(srcFormat != GL_DEPTH_STENCIL_EXT || srcType == GL_UNSIGNED_INT_24_8_EXT);
2741184a9707227ab024d65d352fe7c09b3e287348e9Brian Paul
2742c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz   /* In case we only upload depth we need to preserve the stencil */
2743c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz   if (srcFormat == GL_DEPTH_COMPONENT) {
2744c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz      for (img = 0; img < srcDepth; img++) {
2745c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz         GLuint *dstRow = (GLuint *) dstAddr
2746c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz            + dstImageOffsets[dstZoffset + img]
2747c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz            + dstYoffset * dstRowStride / sizeof(GLuint)
2748c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz            + dstXoffset;
2749c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz         const GLuint *src
2750c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz            = (const GLuint *) _mesa_image_address(dims, srcPacking, srcAddr,
2751c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz                  srcWidth, srcHeight,
2752c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz                  srcFormat, srcType,
2753c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz                  img, 0, 0);
2754c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz         for (row = 0; row < srcHeight; row++) {
2755c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz            GLuint depth[MAX_WIDTH];
2756c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz            GLint i;
2757c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz            _mesa_unpack_depth_span(ctx, srcWidth,
2758c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz                                    GL_UNSIGNED_INT, /* dst type */
2759c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz                                    depth, /* dst addr */
2760c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz                                    depthScale,
2761c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz                                    srcType, src, srcPacking);
2762c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz
2763c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz            for (i = 0; i < srcWidth; i++)
2764c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz               dstRow[i] = depth[i] << 8 | (dstRow[i] & 0x000000FF);
2765c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz
2766c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz            src += srcRowStride;
2767c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz            dstRow += dstRowStride / sizeof(GLuint);
2768c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz         }
2769c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz      }
2770c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz   }
2771c6de08fff483911953692693c501bc200c235dceJakob Bornecrantz   else if (ctx->Pixel.DepthScale == 1.0f &&
2772966e199e409a1b52eef88e48997442250997f45eBrian Paul       ctx->Pixel.DepthBias == 0.0f &&
2773184a9707227ab024d65d352fe7c09b3e287348e9Brian Paul       !srcPacking->SwapBytes) {
2774ef8653a83800bc4b8e116e03ad52604097224378Brian Paul      /* simple path */
2775184a9707227ab024d65d352fe7c09b3e287348e9Brian Paul      memcpy_texture(ctx, dims,
2776184a9707227ab024d65d352fe7c09b3e287348e9Brian Paul                     dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
2777b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstRowStride,
2778b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstImageOffsets,
2779184a9707227ab024d65d352fe7c09b3e287348e9Brian Paul                     srcWidth, srcHeight, srcDepth, srcFormat, srcType,
2780184a9707227ab024d65d352fe7c09b3e287348e9Brian Paul                     srcAddr, srcPacking);
2781184a9707227ab024d65d352fe7c09b3e287348e9Brian Paul   }
2782184a9707227ab024d65d352fe7c09b3e287348e9Brian Paul   else {
2783ef8653a83800bc4b8e116e03ad52604097224378Brian Paul      /* general path */
2784ef8653a83800bc4b8e116e03ad52604097224378Brian Paul      const GLint srcRowStride
2785ef8653a83800bc4b8e116e03ad52604097224378Brian Paul         = _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType)
2786ef8653a83800bc4b8e116e03ad52604097224378Brian Paul         / sizeof(GLuint);
2787ef8653a83800bc4b8e116e03ad52604097224378Brian Paul      GLint img, row;
2788ef8653a83800bc4b8e116e03ad52604097224378Brian Paul
2789ef8653a83800bc4b8e116e03ad52604097224378Brian Paul      for (img = 0; img < srcDepth; img++) {
2790b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         GLuint *dstRow = (GLuint *) dstAddr
2791b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstImageOffsets[dstZoffset + img]
2792b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstYoffset * dstRowStride / sizeof(GLuint)
2793b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstXoffset;
2794ef8653a83800bc4b8e116e03ad52604097224378Brian Paul         const GLuint *src
2795ef8653a83800bc4b8e116e03ad52604097224378Brian Paul            = (const GLuint *) _mesa_image_address(dims, srcPacking, srcAddr,
2796ef8653a83800bc4b8e116e03ad52604097224378Brian Paul                                                   srcWidth, srcHeight,
2797ef8653a83800bc4b8e116e03ad52604097224378Brian Paul                                                   srcFormat, srcType,
2798ef8653a83800bc4b8e116e03ad52604097224378Brian Paul                                                   img, 0, 0);
2799ef8653a83800bc4b8e116e03ad52604097224378Brian Paul         for (row = 0; row < srcHeight; row++) {
2800ef8653a83800bc4b8e116e03ad52604097224378Brian Paul            GLubyte stencil[MAX_WIDTH];
2801ef8653a83800bc4b8e116e03ad52604097224378Brian Paul            GLint i;
2802ef8653a83800bc4b8e116e03ad52604097224378Brian Paul            /* the 24 depth bits will be in the high position: */
2803ef8653a83800bc4b8e116e03ad52604097224378Brian Paul            _mesa_unpack_depth_span(ctx, srcWidth,
280430640695400b9b27656893753ae6b62f2082ce9bBrian Paul                                    GL_UNSIGNED_INT_24_8_EXT, /* dst type */
2805b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                                    dstRow, /* dst addr */
2806090e212c0c5e54156c3c33f7eecdfe01398a7222Michal Krol                                    (GLuint) depthScale,
2807ef8653a83800bc4b8e116e03ad52604097224378Brian Paul                                    srcType, src, srcPacking);
2808ef8653a83800bc4b8e116e03ad52604097224378Brian Paul            /* get the 8-bit stencil values */
2809ef8653a83800bc4b8e116e03ad52604097224378Brian Paul            _mesa_unpack_stencil_span(ctx, srcWidth,
2810ef8653a83800bc4b8e116e03ad52604097224378Brian Paul                                      GL_UNSIGNED_BYTE, /* dst type */
2811ef8653a83800bc4b8e116e03ad52604097224378Brian Paul                                      stencil, /* dst addr */
2812ef8653a83800bc4b8e116e03ad52604097224378Brian Paul                                      srcType, src, srcPacking,
2813ef8653a83800bc4b8e116e03ad52604097224378Brian Paul                                      ctx->_ImageTransferState);
2814ef8653a83800bc4b8e116e03ad52604097224378Brian Paul            /* merge stencil values into depth values */
2815ef8653a83800bc4b8e116e03ad52604097224378Brian Paul            for (i = 0; i < srcWidth; i++)
2816b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul               dstRow[i] |= stencil[i];
2817ef8653a83800bc4b8e116e03ad52604097224378Brian Paul
2818ef8653a83800bc4b8e116e03ad52604097224378Brian Paul            src += srcRowStride;
2819b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            dstRow += dstRowStride / sizeof(GLuint);
2820ef8653a83800bc4b8e116e03ad52604097224378Brian Paul         }
2821ef8653a83800bc4b8e116e03ad52604097224378Brian Paul      }
2822184a9707227ab024d65d352fe7c09b3e287348e9Brian Paul   }
2823184a9707227ab024d65d352fe7c09b3e287348e9Brian Paul   return GL_TRUE;
2824184a9707227ab024d65d352fe7c09b3e287348e9Brian Paul}
2825184a9707227ab024d65d352fe7c09b3e287348e9Brian Paul
2826184a9707227ab024d65d352fe7c09b3e287348e9Brian Paul
2827a1524162bf838920ad965cd44ead97da29408e50Jakob Bornecrantz/**
2828a1524162bf838920ad965cd44ead97da29408e50Jakob Bornecrantz * Store a combined depth/stencil texture image.
2829a1524162bf838920ad965cd44ead97da29408e50Jakob Bornecrantz */
2830a1524162bf838920ad965cd44ead97da29408e50Jakob BornecrantzGLboolean
2831a1524162bf838920ad965cd44ead97da29408e50Jakob Bornecrantz_mesa_texstore_s8_z24(TEXSTORE_PARAMS)
2832a1524162bf838920ad965cd44ead97da29408e50Jakob Bornecrantz{
2833a1524162bf838920ad965cd44ead97da29408e50Jakob Bornecrantz   const GLuint depthScale = 0xffffff;
2834a1524162bf838920ad965cd44ead97da29408e50Jakob Bornecrantz   const GLint srcRowStride
2835a1524162bf838920ad965cd44ead97da29408e50Jakob Bornecrantz      = _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType)
2836a1524162bf838920ad965cd44ead97da29408e50Jakob Bornecrantz      / sizeof(GLuint);
2837a1524162bf838920ad965cd44ead97da29408e50Jakob Bornecrantz   GLint img, row;
2838a1524162bf838920ad965cd44ead97da29408e50Jakob Bornecrantz
2839c47248bdf8d55f985b199fc6e15b0177305cb6fdJosé Fonseca   ASSERT(dstFormat == &_mesa_texformat_s8_z24);
2840c47248bdf8d55f985b199fc6e15b0177305cb6fdJosé Fonseca   ASSERT(srcFormat == GL_DEPTH_STENCIL_EXT || srcFormat == GL_DEPTH_COMPONENT);
2841c47248bdf8d55f985b199fc6e15b0177305cb6fdJosé Fonseca   ASSERT(srcFormat != GL_DEPTH_STENCIL_EXT || srcType == GL_UNSIGNED_INT_24_8_EXT);
2842c47248bdf8d55f985b199fc6e15b0177305cb6fdJosé Fonseca
2843966e199e409a1b52eef88e48997442250997f45eBrian Paul   /* In case we only upload depth we need to preserve the stencil */
2844666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz   if (srcFormat == GL_DEPTH_COMPONENT) {
2845666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz      for (img = 0; img < srcDepth; img++) {
2846666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz         GLuint *dstRow = (GLuint *) dstAddr
2847666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz            + dstImageOffsets[dstZoffset + img]
2848666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz            + dstYoffset * dstRowStride / sizeof(GLuint)
2849666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz            + dstXoffset;
2850666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz         const GLuint *src
2851666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz            = (const GLuint *) _mesa_image_address(dims, srcPacking, srcAddr,
2852666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz                  srcWidth, srcHeight,
2853666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz                  srcFormat, srcType,
2854666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz                  img, 0, 0);
2855666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz         for (row = 0; row < srcHeight; row++) {
2856666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz            GLuint depth[MAX_WIDTH];
2857666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz            GLint i;
2858666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz            _mesa_unpack_depth_span(ctx, srcWidth,
2859666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz                                    GL_UNSIGNED_INT, /* dst type */
2860666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz                                    depth, /* dst addr */
2861666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz                                    depthScale,
2862666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz                                    srcType, src, srcPacking);
2863a1524162bf838920ad965cd44ead97da29408e50Jakob Bornecrantz
2864666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz            for (i = 0; i < srcWidth; i++)
2865666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz               dstRow[i] = depth[i] | (dstRow[i] & 0xFF000000);
2866666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz
2867666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz            src += srcRowStride;
2868666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz            dstRow += dstRowStride / sizeof(GLuint);
2869666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz         }
2870666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz      }
28718122ab2dfd0e158a4982e1bfeb1f7a6f185b69eeBrian Paul   }
28728122ab2dfd0e158a4982e1bfeb1f7a6f185b69eeBrian Paul   else {
2873666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz      for (img = 0; img < srcDepth; img++) {
2874666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz         GLuint *dstRow = (GLuint *) dstAddr
2875666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz            + dstImageOffsets[dstZoffset + img]
2876666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz            + dstYoffset * dstRowStride / sizeof(GLuint)
2877666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz            + dstXoffset;
2878666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz         const GLuint *src
2879666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz            = (const GLuint *) _mesa_image_address(dims, srcPacking, srcAddr,
2880666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz                  srcWidth, srcHeight,
2881666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz                  srcFormat, srcType,
2882666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz                  img, 0, 0);
2883666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz         for (row = 0; row < srcHeight; row++) {
2884666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz            GLubyte stencil[MAX_WIDTH];
2885666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz            GLint i;
2886218df7f9c53db90abf3a6590f77c8e9e49aeedf5Xiang, Haihao            /* the 24 depth bits will be in the low position: */
2887666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz            _mesa_unpack_depth_span(ctx, srcWidth,
2888666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz                                    GL_UNSIGNED_INT, /* dst type */
2889666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz                                    dstRow, /* dst addr */
2890666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz                                    depthScale,
2891666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz                                    srcType, src, srcPacking);
2892666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz            /* get the 8-bit stencil values */
2893666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz            _mesa_unpack_stencil_span(ctx, srcWidth,
2894666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz                                      GL_UNSIGNED_BYTE, /* dst type */
2895666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz                                      stencil, /* dst addr */
2896666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz                                      srcType, src, srcPacking,
2897666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz                                      ctx->_ImageTransferState);
2898666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz            /* merge stencil values into depth values */
2899666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz            for (i = 0; i < srcWidth; i++)
2900218df7f9c53db90abf3a6590f77c8e9e49aeedf5Xiang, Haihao               dstRow[i] |= stencil[i] << 24;
2901666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz
2902666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz            src += srcRowStride;
2903666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz            dstRow += dstRowStride / sizeof(GLuint);
2904666b771e512a7c91fa43544afec61bda63edc240Jakob Bornecrantz         }
2905a1524162bf838920ad965cd44ead97da29408e50Jakob Bornecrantz      }
2906a1524162bf838920ad965cd44ead97da29408e50Jakob Bornecrantz   }
2907a1524162bf838920ad965cd44ead97da29408e50Jakob Bornecrantz   return GL_TRUE;
2908a1524162bf838920ad965cd44ead97da29408e50Jakob Bornecrantz}
2909f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2910f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul/**
2911f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * Store an image in any of the formats:
2912f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul *   _mesa_texformat_rgba_float32
2913f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul *   _mesa_texformat_rgb_float32
2914f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul *   _mesa_texformat_alpha_float32
2915f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul *   _mesa_texformat_luminance_float32
2916f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul *   _mesa_texformat_luminance_alpha_float32
2917f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul *   _mesa_texformat_intensity_float32
2918f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul */
2919f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian PaulGLboolean
2920b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul_mesa_texstore_rgba_float32(TEXSTORE_PARAMS)
2921f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul{
292222108bb571808542b89677752d62d3901698265fBrian Paul   const GLuint texelBytes = _mesa_get_format_bytes(dstFormat->MesaFormat);
292322108bb571808542b89677752d62d3901698265fBrian Paul   const GLenum baseFormat = _mesa_get_format_base_format(dstFormat->MesaFormat);
292422108bb571808542b89677752d62d3901698265fBrian Paul   const GLint components = _mesa_components_in_format(baseFormat);
2925f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2926f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   ASSERT(dstFormat == &_mesa_texformat_rgba_float32 ||
2927f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          dstFormat == &_mesa_texformat_rgb_float32 ||
2928f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          dstFormat == &_mesa_texformat_alpha_float32 ||
2929f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          dstFormat == &_mesa_texformat_luminance_float32 ||
2930f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          dstFormat == &_mesa_texformat_luminance_alpha_float32 ||
2931f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          dstFormat == &_mesa_texformat_intensity_float32);
2932f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   ASSERT(baseInternalFormat == GL_RGBA ||
2933f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          baseInternalFormat == GL_RGB ||
2934f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          baseInternalFormat == GL_ALPHA ||
2935f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          baseInternalFormat == GL_LUMINANCE ||
2936f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          baseInternalFormat == GL_LUMINANCE_ALPHA ||
2937f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          baseInternalFormat == GL_INTENSITY);
293822108bb571808542b89677752d62d3901698265fBrian Paul   ASSERT(texelBytes == components * sizeof(GLfloat));
2939f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2940f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (!ctx->_ImageTransferState &&
2941f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       !srcPacking->SwapBytes &&
2942f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       baseInternalFormat == srcFormat &&
2943f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       srcType == GL_FLOAT) {
2944f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* simple memcpy path */
294517bcf9f816db3098db42acd7f0672f64554dd6a0Keith Whitwell      memcpy_texture(ctx, dims,
294660909388ab136d849d99eab49e782a53772a618fBrian Paul                     dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
2947b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstRowStride,
2948b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstImageOffsets,
2949f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcWidth, srcHeight, srcDepth, srcFormat, srcType,
2950f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcAddr, srcPacking);
2951f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
2952f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   else {
2953f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* general path */
2954f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      const GLfloat *tempImage = make_temp_float_image(ctx, dims,
2955f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 baseInternalFormat,
295622108bb571808542b89677752d62d3901698265fBrian Paul                                                 baseFormat,
2957f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcWidth, srcHeight, srcDepth,
2958f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcFormat, srcType, srcAddr,
2959f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcPacking);
2960b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul      const GLfloat *srcRow = tempImage;
29619c1b13ff6a2fb873cada61271f382a912ad99631Brian Paul      GLint bytesPerRow;
2962f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLint img, row;
2963f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (!tempImage)
2964f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         return GL_FALSE;
2965f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
29669c1b13ff6a2fb873cada61271f382a912ad99631Brian Paul      bytesPerRow = srcWidth * components * sizeof(GLfloat);
2967f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      for (img = 0; img < srcDepth; img++) {
2968b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         GLubyte *dstRow = (GLubyte *) dstAddr
296922108bb571808542b89677752d62d3901698265fBrian Paul            + dstImageOffsets[dstZoffset + img] * texelBytes
2970b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstYoffset * dstRowStride
297122108bb571808542b89677752d62d3901698265fBrian Paul            + dstXoffset * texelBytes;
2972f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         for (row = 0; row < srcHeight; row++) {
2973b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            _mesa_memcpy(dstRow, srcRow, bytesPerRow);
2974b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            dstRow += dstRowStride;
2975b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            srcRow += srcWidth * components;
2976f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         }
2977f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
2978f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2979f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      _mesa_free((void *) tempImage);
2980f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
2981f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   return GL_TRUE;
2982f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul}
2983f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2984f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2985f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul/**
2986f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * As above, but store 16-bit floats.
2987f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul */
2988f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian PaulGLboolean
2989b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul_mesa_texstore_rgba_float16(TEXSTORE_PARAMS)
2990f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul{
299122108bb571808542b89677752d62d3901698265fBrian Paul   const GLuint texelBytes = _mesa_get_format_bytes(dstFormat->MesaFormat);
299222108bb571808542b89677752d62d3901698265fBrian Paul   const GLenum baseFormat = _mesa_get_format_base_format(dstFormat->MesaFormat);
299322108bb571808542b89677752d62d3901698265fBrian Paul   const GLint components = _mesa_components_in_format(baseFormat);
2994f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
2995f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   ASSERT(dstFormat == &_mesa_texformat_rgba_float16 ||
2996f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          dstFormat == &_mesa_texformat_rgb_float16 ||
2997f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          dstFormat == &_mesa_texformat_alpha_float16 ||
2998f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          dstFormat == &_mesa_texformat_luminance_float16 ||
2999f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          dstFormat == &_mesa_texformat_luminance_alpha_float16 ||
3000f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          dstFormat == &_mesa_texformat_intensity_float16);
3001f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   ASSERT(baseInternalFormat == GL_RGBA ||
3002f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          baseInternalFormat == GL_RGB ||
3003f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          baseInternalFormat == GL_ALPHA ||
3004f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          baseInternalFormat == GL_LUMINANCE ||
3005f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          baseInternalFormat == GL_LUMINANCE_ALPHA ||
3006f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul          baseInternalFormat == GL_INTENSITY);
300722108bb571808542b89677752d62d3901698265fBrian Paul   ASSERT(texelBytes == components * sizeof(GLhalfARB));
3008f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
3009f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (!ctx->_ImageTransferState &&
3010f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       !srcPacking->SwapBytes &&
3011f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       baseInternalFormat == srcFormat &&
3012f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul       srcType == GL_HALF_FLOAT_ARB) {
3013f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* simple memcpy path */
301417bcf9f816db3098db42acd7f0672f64554dd6a0Keith Whitwell      memcpy_texture(ctx, dims,
301560909388ab136d849d99eab49e782a53772a618fBrian Paul                     dstFormat, dstAddr, dstXoffset, dstYoffset, dstZoffset,
3016b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstRowStride,
3017b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                     dstImageOffsets,
3018f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcWidth, srcHeight, srcDepth, srcFormat, srcType,
3019f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                     srcAddr, srcPacking);
3020f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
3021f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   else {
3022f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      /* general path */
3023f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      const GLfloat *tempImage = make_temp_float_image(ctx, dims,
3024f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 baseInternalFormat,
302522108bb571808542b89677752d62d3901698265fBrian Paul                                                 baseFormat,
3026f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcWidth, srcHeight, srcDepth,
3027f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcFormat, srcType, srcAddr,
3028f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                 srcPacking);
3029f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      const GLfloat *src = tempImage;
3030f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLint img, row;
3031f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (!tempImage)
3032f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         return GL_FALSE;
3033f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      _mesa_adjust_image_for_convolution(ctx, dims, &srcWidth, &srcHeight);
3034f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      for (img = 0; img < srcDepth; img++) {
3035b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         GLubyte *dstRow = (GLubyte *) dstAddr
303622108bb571808542b89677752d62d3901698265fBrian Paul            + dstImageOffsets[dstZoffset + img] * texelBytes
3037b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul            + dstYoffset * dstRowStride
303822108bb571808542b89677752d62d3901698265fBrian Paul            + dstXoffset * texelBytes;
3039f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         for (row = 0; row < srcHeight; row++) {
3040f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            GLhalfARB *dstTexel = (GLhalfARB *) dstRow;
3041f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            GLint i;
3042f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            for (i = 0; i < srcWidth * components; i++) {
3043f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul               dstTexel[i] = _mesa_float_to_half(src[i]);
3044f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            }
3045f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            dstRow += dstRowStride;
3046f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul            src += srcWidth * components;
3047f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         }
3048f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
3049f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
3050f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      _mesa_free((void *) tempImage);
3051f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
3052f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   return GL_TRUE;
3053f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul}
3054f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
3055f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
30568d214bc8044e5027e3fa9302b259d0c557270b00Brian Paul#if FEATURE_EXT_texture_sRGB
30578d214bc8044e5027e3fa9302b259d0c557270b00Brian PaulGLboolean
30588d214bc8044e5027e3fa9302b259d0c557270b00Brian Paul_mesa_texstore_srgb8(TEXSTORE_PARAMS)
30598d214bc8044e5027e3fa9302b259d0c557270b00Brian Paul{
306046f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul   const struct gl_texture_format *newDstFormat;
306146f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul   StoreTexImageFunc store;
306246f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul   GLboolean k;
306346f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul
306446f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul   ASSERT(dstFormat == &_mesa_texformat_srgb8);
306546f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul
306646f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul   /* reuse normal rgb texstore code */
30675bd093bd7b3711f88e1fd0fc9cdb37a18d7d24b9Roland Scheidegger   newDstFormat = &_mesa_texformat_rgb888;
30685bd093bd7b3711f88e1fd0fc9cdb37a18d7d24b9Roland Scheidegger   store = _mesa_texstore_rgb888;
306946f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul
307046f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul   k = store(ctx, dims, baseInternalFormat,
307146f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul             newDstFormat, dstAddr,
307246f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul             dstXoffset, dstYoffset, dstZoffset,
307346f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul             dstRowStride, dstImageOffsets,
307446f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul             srcWidth, srcHeight, srcDepth,
307546f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul             srcFormat, srcType,
307646f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul             srcAddr, srcPacking);
307746f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul   return k;
30788d214bc8044e5027e3fa9302b259d0c557270b00Brian Paul}
30798d214bc8044e5027e3fa9302b259d0c557270b00Brian Paul
308054e15d65858c1d1eeea7291059766686cf2e1671Brian Paul
30818d214bc8044e5027e3fa9302b259d0c557270b00Brian PaulGLboolean
30828d214bc8044e5027e3fa9302b259d0c557270b00Brian Paul_mesa_texstore_srgba8(TEXSTORE_PARAMS)
30838d214bc8044e5027e3fa9302b259d0c557270b00Brian Paul{
308446f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul   const struct gl_texture_format *newDstFormat;
308546f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul   GLboolean k;
308646f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul
308746f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul   ASSERT(dstFormat == &_mesa_texformat_srgba8);
308846f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul
308946f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul   /* reuse normal rgba texstore code */
30905bd093bd7b3711f88e1fd0fc9cdb37a18d7d24b9Roland Scheidegger   newDstFormat = &_mesa_texformat_rgba8888;
309146f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul
309246f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul   k = _mesa_texstore_rgba8888(ctx, dims, baseInternalFormat,
309346f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul                               newDstFormat, dstAddr,
309446f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul                               dstXoffset, dstYoffset, dstZoffset,
309546f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul                               dstRowStride, dstImageOffsets,
309646f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul                               srcWidth, srcHeight, srcDepth,
309746f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul                               srcFormat, srcType,
309846f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul                               srcAddr, srcPacking);
309946f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul   return k;
31008d214bc8044e5027e3fa9302b259d0c557270b00Brian Paul}
31018d214bc8044e5027e3fa9302b259d0c557270b00Brian Paul
310254e15d65858c1d1eeea7291059766686cf2e1671Brian Paul
31038d214bc8044e5027e3fa9302b259d0c557270b00Brian PaulGLboolean
31045bd093bd7b3711f88e1fd0fc9cdb37a18d7d24b9Roland Scheidegger_mesa_texstore_sargb8(TEXSTORE_PARAMS)
31055bd093bd7b3711f88e1fd0fc9cdb37a18d7d24b9Roland Scheidegger{
31065bd093bd7b3711f88e1fd0fc9cdb37a18d7d24b9Roland Scheidegger   const struct gl_texture_format *newDstFormat;
31075bd093bd7b3711f88e1fd0fc9cdb37a18d7d24b9Roland Scheidegger   GLboolean k;
31085bd093bd7b3711f88e1fd0fc9cdb37a18d7d24b9Roland Scheidegger
31095bd093bd7b3711f88e1fd0fc9cdb37a18d7d24b9Roland Scheidegger   ASSERT(dstFormat == &_mesa_texformat_sargb8);
31105bd093bd7b3711f88e1fd0fc9cdb37a18d7d24b9Roland Scheidegger
31115bd093bd7b3711f88e1fd0fc9cdb37a18d7d24b9Roland Scheidegger   /* reuse normal rgba texstore code */
31125bd093bd7b3711f88e1fd0fc9cdb37a18d7d24b9Roland Scheidegger   newDstFormat = &_mesa_texformat_argb8888;
31135bd093bd7b3711f88e1fd0fc9cdb37a18d7d24b9Roland Scheidegger
31145bd093bd7b3711f88e1fd0fc9cdb37a18d7d24b9Roland Scheidegger   k = _mesa_texstore_argb8888(ctx, dims, baseInternalFormat,
31155bd093bd7b3711f88e1fd0fc9cdb37a18d7d24b9Roland Scheidegger                               newDstFormat, dstAddr,
31165bd093bd7b3711f88e1fd0fc9cdb37a18d7d24b9Roland Scheidegger                               dstXoffset, dstYoffset, dstZoffset,
31175bd093bd7b3711f88e1fd0fc9cdb37a18d7d24b9Roland Scheidegger                               dstRowStride, dstImageOffsets,
31185bd093bd7b3711f88e1fd0fc9cdb37a18d7d24b9Roland Scheidegger                               srcWidth, srcHeight, srcDepth,
31195bd093bd7b3711f88e1fd0fc9cdb37a18d7d24b9Roland Scheidegger                               srcFormat, srcType,
31205bd093bd7b3711f88e1fd0fc9cdb37a18d7d24b9Roland Scheidegger                               srcAddr, srcPacking);
31215bd093bd7b3711f88e1fd0fc9cdb37a18d7d24b9Roland Scheidegger   return k;
31225bd093bd7b3711f88e1fd0fc9cdb37a18d7d24b9Roland Scheidegger}
31235bd093bd7b3711f88e1fd0fc9cdb37a18d7d24b9Roland Scheidegger
31245bd093bd7b3711f88e1fd0fc9cdb37a18d7d24b9Roland Scheidegger
31255bd093bd7b3711f88e1fd0fc9cdb37a18d7d24b9Roland ScheideggerGLboolean
31268d214bc8044e5027e3fa9302b259d0c557270b00Brian Paul_mesa_texstore_sl8(TEXSTORE_PARAMS)
31278d214bc8044e5027e3fa9302b259d0c557270b00Brian Paul{
312846f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul   const struct gl_texture_format *newDstFormat;
312946f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul   GLboolean k;
313046f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul
313146f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul   ASSERT(dstFormat == &_mesa_texformat_sl8);
313246f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul
313346f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul   newDstFormat = &_mesa_texformat_l8;
313446f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul
313554e15d65858c1d1eeea7291059766686cf2e1671Brian Paul   /* _mesa_textore_a8 handles luminance8 too */
313654e15d65858c1d1eeea7291059766686cf2e1671Brian Paul   k = _mesa_texstore_a8(ctx, dims, baseInternalFormat,
313754e15d65858c1d1eeea7291059766686cf2e1671Brian Paul                         newDstFormat, dstAddr,
313854e15d65858c1d1eeea7291059766686cf2e1671Brian Paul                         dstXoffset, dstYoffset, dstZoffset,
313954e15d65858c1d1eeea7291059766686cf2e1671Brian Paul                         dstRowStride, dstImageOffsets,
314054e15d65858c1d1eeea7291059766686cf2e1671Brian Paul                         srcWidth, srcHeight, srcDepth,
314154e15d65858c1d1eeea7291059766686cf2e1671Brian Paul                         srcFormat, srcType,
314254e15d65858c1d1eeea7291059766686cf2e1671Brian Paul                         srcAddr, srcPacking);
314346f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul   return k;
31448d214bc8044e5027e3fa9302b259d0c557270b00Brian Paul}
31458d214bc8044e5027e3fa9302b259d0c557270b00Brian Paul
314654e15d65858c1d1eeea7291059766686cf2e1671Brian Paul
31478d214bc8044e5027e3fa9302b259d0c557270b00Brian PaulGLboolean
31488d214bc8044e5027e3fa9302b259d0c557270b00Brian Paul_mesa_texstore_sla8(TEXSTORE_PARAMS)
31498d214bc8044e5027e3fa9302b259d0c557270b00Brian Paul{
315046f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul   const struct gl_texture_format *newDstFormat;
315146f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul   GLboolean k;
315246f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul
315346f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul   ASSERT(dstFormat == &_mesa_texformat_sla8);
315446f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul
315546f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul   /* reuse normal luminance/alpha texstore code */
31565bd093bd7b3711f88e1fd0fc9cdb37a18d7d24b9Roland Scheidegger   newDstFormat = &_mesa_texformat_al88;
315746f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul
315846f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul   k = _mesa_texstore_al88(ctx, dims, baseInternalFormat,
315946f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul                           newDstFormat, dstAddr,
316046f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul                           dstXoffset, dstYoffset, dstZoffset,
316146f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul                           dstRowStride, dstImageOffsets,
316246f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul                           srcWidth, srcHeight, srcDepth,
316346f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul                           srcFormat, srcType,
316446f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul                           srcAddr, srcPacking);
316546f20579a28ac97dd6fd9cc505301b0764eb011aBrian Paul   return k;
31668d214bc8044e5027e3fa9302b259d0c557270b00Brian Paul}
31678d214bc8044e5027e3fa9302b259d0c557270b00Brian Paul
31688d214bc8044e5027e3fa9302b259d0c557270b00Brian Paul#endif /* FEATURE_EXT_texture_sRGB */
31698d214bc8044e5027e3fa9302b259d0c557270b00Brian Paul
3170f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
3171485105ed182e2e997b084f047e72d5a2c3460057Brian Paul
3172485105ed182e2e997b084f047e72d5a2c3460057Brian Paul
3173485105ed182e2e997b084f047e72d5a2c3460057Brian Paul/**
3174485105ed182e2e997b084f047e72d5a2c3460057Brian Paul * Table mapping MESA_FORMAT_8 to _mesa_texstore_*()
3175485105ed182e2e997b084f047e72d5a2c3460057Brian Paul * XXX this is somewhat temporary.
3176485105ed182e2e997b084f047e72d5a2c3460057Brian Paul */
3177485105ed182e2e997b084f047e72d5a2c3460057Brian Paulstatic struct {
3178485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   gl_format Name;
3179485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   StoreTexImageFunc Store;
3180485105ed182e2e997b084f047e72d5a2c3460057Brian Paul}
3181485105ed182e2e997b084f047e72d5a2c3460057Brian Paultexstore_funcs[MESA_FORMAT_COUNT] =
3182485105ed182e2e997b084f047e72d5a2c3460057Brian Paul{
3183485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_RGBA, _mesa_texstore_rgba },
3184485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_RGB, _mesa_texstore_rgba },
3185485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_ALPHA, _mesa_texstore_rgba },
3186485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_LUMINANCE, _mesa_texstore_rgba },
3187485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_LUMINANCE_ALPHA, _mesa_texstore_rgba },
3188485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_INTENSITY, _mesa_texstore_rgba },
3189485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_SRGB8, _mesa_texstore_srgb8 },
3190485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_SRGBA8, _mesa_texstore_srgba8 },
3191485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_SARGB8, _mesa_texstore_sargb8 },
3192485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_SL8, _mesa_texstore_sl8 },
3193485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_SLA8, _mesa_texstore_sla8 },
3194485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_RGBA_FLOAT32, _mesa_texstore_rgba_float32 },
3195485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_RGBA_FLOAT16, _mesa_texstore_rgba_float16 },
3196485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_RGB_FLOAT32, _mesa_texstore_rgba_float32 },
3197485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_RGB_FLOAT16, _mesa_texstore_rgba_float16 },
3198485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_ALPHA_FLOAT32, _mesa_texstore_rgba_float32 },
3199485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_ALPHA_FLOAT16, _mesa_texstore_rgba_float16 },
3200485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_LUMINANCE_FLOAT32, _mesa_texstore_rgba_float32 },
3201485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_LUMINANCE_FLOAT16, _mesa_texstore_rgba_float16 },
3202485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_LUMINANCE_ALPHA_FLOAT32, _mesa_texstore_rgba_float32 },
3203485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_LUMINANCE_ALPHA_FLOAT16, _mesa_texstore_rgba_float16 },
3204485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_INTENSITY_FLOAT32, _mesa_texstore_rgba_float32 },
3205485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_INTENSITY_FLOAT16, _mesa_texstore_rgba_float16 },
3206485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_DUDV8, _mesa_texstore_dudv8 },
3207485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_SIGNED_RGBA8888, _mesa_texstore_signed_rgba8888 },
3208485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_SIGNED_RGBA8888_REV, _mesa_texstore_signed_rgba8888 },
3209485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_RGBA8888, _mesa_texstore_rgba8888 },
3210485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_RGBA8888_REV, _mesa_texstore_rgba8888 },
3211485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_ARGB8888, _mesa_texstore_argb8888 },
3212485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_ARGB8888_REV, _mesa_texstore_argb8888 },
3213485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_RGB888, _mesa_texstore_rgb888 },
3214485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_BGR888, _mesa_texstore_bgr888 },
3215485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_RGB565, _mesa_texstore_rgb565 },
3216485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_RGB565_REV, _mesa_texstore_rgb565 },
3217485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_RGBA4444, _mesa_texstore_rgba4444 },
3218485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_ARGB4444, _mesa_texstore_argb4444 },
3219485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_ARGB4444_REV, _mesa_texstore_argb4444 },
3220485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_RGBA5551, _mesa_texstore_rgba5551 },
3221485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_ARGB1555, _mesa_texstore_argb1555 },
3222485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_ARGB1555_REV, _mesa_texstore_argb1555 },
3223485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_AL88, _mesa_texstore_al88 },
3224485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_AL88_REV, _mesa_texstore_al88 },
3225485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_RGB332, _mesa_texstore_rgb332 },
3226485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_A8, _mesa_texstore_a8 },
3227485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_L8, _mesa_texstore_a8 },
3228485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_I8, _mesa_texstore_a8 },
3229485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_CI8, _mesa_texstore_ci8 },
3230485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_YCBCR, _mesa_texstore_ycbcr },
3231485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_YCBCR_REV, _mesa_texstore_ycbcr },
3232485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_Z24_S8, _mesa_texstore_z24_s8 },
3233485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_S8_Z24, _mesa_texstore_s8_z24 },
3234485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_Z16, _mesa_texstore_z16 },
3235485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_Z32, _mesa_texstore_z32 },
3236485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   { MESA_FORMAT_S8, NULL/*_mesa_texstore_s8*/ },
3237485105ed182e2e997b084f047e72d5a2c3460057Brian Paul};
3238485105ed182e2e997b084f047e72d5a2c3460057Brian Paul
3239485105ed182e2e997b084f047e72d5a2c3460057Brian Paul
3240485105ed182e2e997b084f047e72d5a2c3460057Brian Paul/**
3241485105ed182e2e997b084f047e72d5a2c3460057Brian Paul * Return the StoreTexImageFunc pointer to store an image in the given format.
3242485105ed182e2e997b084f047e72d5a2c3460057Brian Paul */
3243485105ed182e2e997b084f047e72d5a2c3460057Brian PaulStoreTexImageFunc
3244485105ed182e2e997b084f047e72d5a2c3460057Brian Paul_mesa_get_texstore_func(gl_format format)
3245485105ed182e2e997b084f047e72d5a2c3460057Brian Paul{
3246485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   GLuint i;
3247485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   for (i = 0; i < MESA_FORMAT_COUNT; i++) {
3248485105ed182e2e997b084f047e72d5a2c3460057Brian Paul      if (texstore_funcs[i].Name == format)
3249485105ed182e2e997b084f047e72d5a2c3460057Brian Paul         return texstore_funcs[i].Store;
3250485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   }
3251485105ed182e2e997b084f047e72d5a2c3460057Brian Paul   return NULL;
3252485105ed182e2e997b084f047e72d5a2c3460057Brian Paul}
3253485105ed182e2e997b084f047e72d5a2c3460057Brian Paul
3254485105ed182e2e997b084f047e72d5a2c3460057Brian Paul
3255485105ed182e2e997b084f047e72d5a2c3460057Brian Paul
32567a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul/**
3257c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul * Check if an unpack PBO is active prior to fetching a texture image.
3258c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul * If so, do bounds checking and map the buffer into main memory.
3259c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul * Any errors detected will be recorded.
326071699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell * The caller _must_ call _mesa_unmap_teximage_pbo() too!
32617a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul */
326271699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwellconst GLvoid *
326371699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell_mesa_validate_pbo_teximage(GLcontext *ctx, GLuint dimensions,
326471699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell			    GLsizei width, GLsizei height, GLsizei depth,
326571699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell			    GLenum format, GLenum type, const GLvoid *pixels,
326671699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell			    const struct gl_pixelstore_attrib *unpack,
326771699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell			    const char *funcName)
32687a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul{
3269c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul   GLubyte *buf;
3270c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul
3271434ec3ada841915a00ffc23f699401eb3e7b37eeBrian Paul   if (!_mesa_is_bufferobj(unpack->BufferObj)) {
32727a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul      /* no PBO */
32737a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul      return pixels;
32747a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul   }
327560909388ab136d849d99eab49e782a53772a618fBrian Paul   if (!_mesa_validate_pbo_access(dimensions, unpack, width, height, depth,
327660909388ab136d849d99eab49e782a53772a618fBrian Paul                                  format, type, pixels)) {
3277c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul      _mesa_error(ctx, GL_INVALID_OPERATION, funcName, "(invalid PBO access");
3278c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul      return NULL;
32797a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul   }
3280c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul
3281a760ccf6d8a1f94d505b4c211ff4c30bc1d325a8Brian Paul   buf = (GLubyte *) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
3282a760ccf6d8a1f94d505b4c211ff4c30bc1d325a8Brian Paul                                          GL_READ_ONLY_ARB, unpack->BufferObj);
3283c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul   if (!buf) {
3284c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul      _mesa_error(ctx, GL_INVALID_OPERATION, funcName, "(PBO is mapped");
3285c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul      return NULL;
3286c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul   }
3287c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul
3288c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul   return ADD_POINTERS(buf, pixels);
32897a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul}
32907a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul
32917a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul
32927a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul/**
3293c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul * Check if an unpack PBO is active prior to fetching a compressed texture
3294c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul * image.
3295c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul * If so, do bounds checking and map the buffer into main memory.
3296c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul * Any errors detected will be recorded.
329771699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell * The caller _must_ call _mesa_unmap_teximage_pbo() too!
32987a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul */
329971699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwellconst GLvoid *
330071699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell_mesa_validate_pbo_compressed_teximage(GLcontext *ctx,
3301c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul                                 GLsizei imageSize, const GLvoid *pixels,
3302c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul                                 const struct gl_pixelstore_attrib *packing,
3303c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul                                 const char *funcName)
33047a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul{
3305c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul   GLubyte *buf;
3306c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul
3307434ec3ada841915a00ffc23f699401eb3e7b37eeBrian Paul   if (!_mesa_is_bufferobj(packing->BufferObj)) {
33087a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul      /* not using a PBO - return pointer unchanged */
33097a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul      return pixels;
33107a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul   }
3311c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul   if ((const GLubyte *) pixels + imageSize >
3312f285f0d8f60adafdfba5c1f0563b81c68bd398d3Brian Paul       ((const GLubyte *) 0) + packing->BufferObj->Size) {
3313c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul      /* out of bounds read! */
3314c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul      _mesa_error(ctx, GL_INVALID_OPERATION, funcName, "(invalid PBO access");
3315c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul      return NULL;
3316c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul   }
3317c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul
3318a760ccf6d8a1f94d505b4c211ff4c30bc1d325a8Brian Paul   buf = (GLubyte*) ctx->Driver.MapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
3319a760ccf6d8a1f94d505b4c211ff4c30bc1d325a8Brian Paul                                         GL_READ_ONLY_ARB, packing->BufferObj);
3320c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul   if (!buf) {
3321c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul      _mesa_error(ctx, GL_INVALID_OPERATION, funcName, "(PBO is mapped");
3322c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul      return NULL;
33237a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul   }
3324c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul
3325c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul   return ADD_POINTERS(buf, pixels);
33267a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul}
33277a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul
33287a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul
3329c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul/**
3330c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul * This function must be called after either of the validate_pbo_*_teximage()
3331c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul * functions.  It unmaps the PBO buffer if it was mapped earlier.
3332c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul */
333371699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwellvoid
3334c039af165d5919008c6df599795951f85dea164dBrian Paul_mesa_unmap_teximage_pbo(GLcontext *ctx,
3335c039af165d5919008c6df599795951f85dea164dBrian Paul                         const struct gl_pixelstore_attrib *unpack)
3336c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul{
3337434ec3ada841915a00ffc23f699401eb3e7b37eeBrian Paul   if (_mesa_is_bufferobj(unpack->BufferObj)) {
3338c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_UNPACK_BUFFER_EXT,
3339c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul                              unpack->BufferObj);
3340c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul   }
3341c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul}
3342c0ebc4931a003b7b14e92c3b537b6ba76259507cBrian Paul
334389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
3344da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul
3345da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul/**
3346da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul * Adaptor for fetching a GLchan texel from a float-valued texture.
3347da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul */
3348da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paulstatic void
334924edd9015951dd41898902b6c3973fe605e5871aBrian Paulfetch_texel_float_to_chan(const struct gl_texture_image *texImage,
335024edd9015951dd41898902b6c3973fe605e5871aBrian Paul                          GLint i, GLint j, GLint k, GLchan *texelOut)
3351da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul{
3352da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   GLfloat temp[4];
3353da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   ASSERT(texImage->FetchTexelf);
3354da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   texImage->FetchTexelf(texImage, i, j, k, temp);
33552742c4e4db63d61f585c014103eaeadffa8e0833Brian Paul   if (texImage->TexFormat->BaseFormat == GL_DEPTH_COMPONENT ||
33562742c4e4db63d61f585c014103eaeadffa8e0833Brian Paul       texImage->TexFormat->BaseFormat == GL_DEPTH_STENCIL_EXT) {
3357da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul      /* just one channel */
3358da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul      UNCLAMPED_FLOAT_TO_CHAN(texelOut[0], temp[0]);
3359da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   }
3360da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   else {
3361da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul      /* four channels */
3362da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul      UNCLAMPED_FLOAT_TO_CHAN(texelOut[0], temp[0]);
3363da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul      UNCLAMPED_FLOAT_TO_CHAN(texelOut[1], temp[1]);
3364da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul      UNCLAMPED_FLOAT_TO_CHAN(texelOut[2], temp[2]);
3365da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul      UNCLAMPED_FLOAT_TO_CHAN(texelOut[3], temp[3]);
3366da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   }
3367da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul}
3368da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul
3369da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul
3370da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul/**
3371da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul * Adaptor for fetching a float texel from a GLchan-valued texture.
3372da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul */
3373da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paulstatic void
337424edd9015951dd41898902b6c3973fe605e5871aBrian Paulfetch_texel_chan_to_float(const struct gl_texture_image *texImage,
337524edd9015951dd41898902b6c3973fe605e5871aBrian Paul                          GLint i, GLint j, GLint k, GLfloat *texelOut)
3376da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul{
3377da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   GLchan temp[4];
3378da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   ASSERT(texImage->FetchTexelc);
3379da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   texImage->FetchTexelc(texImage, i, j, k, temp);
33802742c4e4db63d61f585c014103eaeadffa8e0833Brian Paul   if (texImage->TexFormat->BaseFormat == GL_DEPTH_COMPONENT ||
33812742c4e4db63d61f585c014103eaeadffa8e0833Brian Paul       texImage->TexFormat->BaseFormat == GL_DEPTH_STENCIL_EXT) {
3382da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul      /* just one channel */
3383da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul      texelOut[0] = CHAN_TO_FLOAT(temp[0]);
3384da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   }
3385da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   else {
3386da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul      /* four channels */
3387da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul      texelOut[0] = CHAN_TO_FLOAT(temp[0]);
3388da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul      texelOut[1] = CHAN_TO_FLOAT(temp[1]);
3389da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul      texelOut[2] = CHAN_TO_FLOAT(temp[2]);
3390da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul      texelOut[3] = CHAN_TO_FLOAT(temp[3]);
3391da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   }
3392da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul}
3393da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul
3394da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul
3395da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul/**
3396da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul * Initialize the texture image's FetchTexelc and FetchTexelf methods.
3397da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul */
3398c96974f78cf3bbb94c01d6c84201595b4028d840Michel Dänzervoid
3399c96974f78cf3bbb94c01d6c84201595b4028d840Michel Dänzer_mesa_set_fetch_functions(struct gl_texture_image *texImage, GLuint dims)
3400da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul{
3401da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   ASSERT(dims == 1 || dims == 2 || dims == 3);
3402da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   ASSERT(texImage->TexFormat);
3403da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul
3404da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   switch (dims) {
3405da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   case 1:
3406da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul      texImage->FetchTexelc = texImage->TexFormat->FetchTexel1D;
3407da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul      texImage->FetchTexelf = texImage->TexFormat->FetchTexel1Df;
3408da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul      break;
3409da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   case 2:
3410da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul      texImage->FetchTexelc = texImage->TexFormat->FetchTexel2D;
3411da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul      texImage->FetchTexelf = texImage->TexFormat->FetchTexel2Df;
3412da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul      break;
3413da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   case 3:
3414da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul      texImage->FetchTexelc = texImage->TexFormat->FetchTexel3D;
3415da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul      texImage->FetchTexelf = texImage->TexFormat->FetchTexel3Df;
3416da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul      break;
3417da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   default:
3418da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul      ;
3419da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   }
3420da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul
3421da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   /* now check if we need to use a float/chan adaptor */
3422da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   if (!texImage->FetchTexelc) {
342324edd9015951dd41898902b6c3973fe605e5871aBrian Paul      texImage->FetchTexelc = fetch_texel_float_to_chan;
3424da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   }
3425da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   else if (!texImage->FetchTexelf) {
342624edd9015951dd41898902b6c3973fe605e5871aBrian Paul      texImage->FetchTexelf = fetch_texel_chan_to_float;
3427da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   }
3428da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul
3429da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul
3430da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   ASSERT(texImage->FetchTexelc);
3431da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul   ASSERT(texImage->FetchTexelf);
3432da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul}
3433da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul
3434da9f65268db5d0468f91860d9ef9f244587c7f48Brian Paul
34355999c5b620236fb6a996cf56759aec31f01c126bBrian Paulstatic void
343621caa29fbd332a2ee05a58df91e1664fbbc4e61fBrian Paulcompute_texture_size(GLcontext *ctx, struct gl_texture_image *texImage)
34375999c5b620236fb6a996cf56759aec31f01c126bBrian Paul{
34385999c5b620236fb6a996cf56759aec31f01c126bBrian Paul   if (texImage->TexFormat->TexelBytes == 0) {
34395999c5b620236fb6a996cf56759aec31f01c126bBrian Paul      /* must be a compressed format */
34405999c5b620236fb6a996cf56759aec31f01c126bBrian Paul      texImage->IsCompressed = GL_TRUE;
34415999c5b620236fb6a996cf56759aec31f01c126bBrian Paul      texImage->CompressedSize =
34425999c5b620236fb6a996cf56759aec31f01c126bBrian Paul         ctx->Driver.CompressedTextureSize(ctx, texImage->Width,
34435999c5b620236fb6a996cf56759aec31f01c126bBrian Paul                                           texImage->Height, texImage->Depth,
34445999c5b620236fb6a996cf56759aec31f01c126bBrian Paul                                           texImage->TexFormat->MesaFormat);
34455999c5b620236fb6a996cf56759aec31f01c126bBrian Paul   }
34465999c5b620236fb6a996cf56759aec31f01c126bBrian Paul   else {
34475999c5b620236fb6a996cf56759aec31f01c126bBrian Paul      /* non-compressed format */
34485999c5b620236fb6a996cf56759aec31f01c126bBrian Paul      texImage->IsCompressed = GL_FALSE;
34495999c5b620236fb6a996cf56759aec31f01c126bBrian Paul      texImage->CompressedSize = 0;
34505999c5b620236fb6a996cf56759aec31f01c126bBrian Paul   }
34515999c5b620236fb6a996cf56759aec31f01c126bBrian Paul}
34525999c5b620236fb6a996cf56759aec31f01c126bBrian Paul
34535999c5b620236fb6a996cf56759aec31f01c126bBrian Paul
34545999c5b620236fb6a996cf56759aec31f01c126bBrian Paul
3455ea6ddcbe0e4a0049ee8b9d244d86b6981fc6438bBrian Paul/**
345689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * This is the software fallback for Driver.TexImage1D()
3457f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul * and Driver.CopyTexImage1D().
34586b6c96bdeb580052cb9fa3831f1cd574f0e85728Brian Paul * \sa _mesa_store_teximage2d()
3459ea6ddcbe0e4a0049ee8b9d244d86b6981fc6438bBrian Paul * Note that the width may not be the actual texture width since it may
3460ea6ddcbe0e4a0049ee8b9d244d86b6981fc6438bBrian Paul * be changed by convolution w/ GL_REDUCE.  The texImage->Width field will
3461ea6ddcbe0e4a0049ee8b9d244d86b6981fc6438bBrian Paul * have the actual texture size.
34628e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul */
34638e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paulvoid
34648e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul_mesa_store_teximage1d(GLcontext *ctx, GLenum target, GLint level,
34658e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                       GLint internalFormat,
34668e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                       GLint width, GLint border,
34678e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                       GLenum format, GLenum type, const GLvoid *pixels,
34688e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                       const struct gl_pixelstore_attrib *packing,
34698e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                       struct gl_texture_object *texObj,
34708e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                       struct gl_texture_image *texImage)
34718e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul{
3472f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   GLint sizeInBytes;
3473a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) border;
34748e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
347521caa29fbd332a2ee05a58df91e1664fbbc4e61fBrian Paul   texImage->TexFormat
347621caa29fbd332a2ee05a58df91e1664fbbc4e61fBrian Paul      = ctx->Driver.ChooseTextureFormat(ctx, internalFormat, format, type);
347721caa29fbd332a2ee05a58df91e1664fbbc4e61fBrian Paul   ASSERT(texImage->TexFormat);
347821caa29fbd332a2ee05a58df91e1664fbbc4e61fBrian Paul
347921caa29fbd332a2ee05a58df91e1664fbbc4e61fBrian Paul   _mesa_set_fetch_functions(texImage, 1);
348021caa29fbd332a2ee05a58df91e1664fbbc4e61fBrian Paul   compute_texture_size(ctx, texImage);
34818e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
34828e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   /* allocate memory */
348389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   if (texImage->IsCompressed)
348489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      sizeInBytes = texImage->CompressedSize;
348589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   else
3486ea6ddcbe0e4a0049ee8b9d244d86b6981fc6438bBrian Paul      sizeInBytes = texImage->Width * texImage->TexFormat->TexelBytes;
34874cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul   texImage->Data = _mesa_alloc_texmemory(sizeInBytes);
34887d58f44f73be59bd3583e6dfeedf56c43f7fbd55Brian Paul   if (!texImage->Data) {
34897d58f44f73be59bd3583e6dfeedf56c43f7fbd55Brian Paul      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage1D");
34907d58f44f73be59bd3583e6dfeedf56c43f7fbd55Brian Paul      return;
34917d58f44f73be59bd3583e6dfeedf56c43f7fbd55Brian Paul   }
34928e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
3493e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   pixels = _mesa_validate_pbo_teximage(ctx, 1, width, 1, 1, format, type,
3494e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul                                        pixels, packing, "glTexImage1D");
34956b6c96bdeb580052cb9fa3831f1cd574f0e85728Brian Paul   if (!pixels) {
34966b6c96bdeb580052cb9fa3831f1cd574f0e85728Brian Paul      /* Note: we check for a NULL image pointer here, _after_ we allocated
34976b6c96bdeb580052cb9fa3831f1cd574f0e85728Brian Paul       * memory for the texture.  That's what the GL spec calls for.
34986b6c96bdeb580052cb9fa3831f1cd574f0e85728Brian Paul       */
349989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return;
35006b6c96bdeb580052cb9fa3831f1cd574f0e85728Brian Paul   }
35016b6c96bdeb580052cb9fa3831f1cd574f0e85728Brian Paul   else {
3502b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul      const GLint dstRowStride = 0;
3503f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLboolean success;
3504f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      ASSERT(texImage->TexFormat->StoreImage);
3505a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul      success = texImage->TexFormat->StoreImage(ctx, 1, texImage->_BaseFormat,
3506f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                texImage->TexFormat,
3507f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                texImage->Data,
3508f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                0, 0, 0,  /* dstX/Y/Zoffset */
3509b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                                                dstRowStride,
3510b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                                                texImage->ImageOffsets,
3511f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                width, 1, 1,
3512f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                format, type, pixels, packing);
3513f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (!success) {
3514f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage1D");
3515f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
3516f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
3517f3b85c983f469875ac76081a61539a6c7b26777cBrian Paul
351871699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   _mesa_unmap_teximage_pbo(ctx, packing);
35198e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul}
35208e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
35218e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
35226b6c96bdeb580052cb9fa3831f1cd574f0e85728Brian Paul/**
352389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * This is the software fallback for Driver.TexImage2D()
352489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * and Driver.CopyTexImage2D().
35256b6c96bdeb580052cb9fa3831f1cd574f0e85728Brian Paul *
3526b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul * This function is oriented toward storing images in main memory, rather
3527b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul * than VRAM.  Device driver's can easily plug in their own replacement.
3528b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul *
3529b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul * Note: width and height may be pre-convolved dimensions, but
3530b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul * texImage->Width and texImage->Height will be post-convolved dimensions.
35318e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul */
35328e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paulvoid
35338e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul_mesa_store_teximage2d(GLcontext *ctx, GLenum target, GLint level,
35348e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                       GLint internalFormat,
35358e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                       GLint width, GLint height, GLint border,
35368e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                       GLenum format, GLenum type, const void *pixels,
35378e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                       const struct gl_pixelstore_attrib *packing,
35388e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                       struct gl_texture_object *texObj,
35398e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                       struct gl_texture_image *texImage)
35408e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul{
3541e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul   GLint texelBytes, sizeInBytes;
3542a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) border;
35438e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
354421caa29fbd332a2ee05a58df91e1664fbbc4e61fBrian Paul   texImage->TexFormat
354521caa29fbd332a2ee05a58df91e1664fbbc4e61fBrian Paul      = ctx->Driver.ChooseTextureFormat(ctx, internalFormat, format, type);
354621caa29fbd332a2ee05a58df91e1664fbbc4e61fBrian Paul   ASSERT(texImage->TexFormat);
354721caa29fbd332a2ee05a58df91e1664fbbc4e61fBrian Paul
354821caa29fbd332a2ee05a58df91e1664fbbc4e61fBrian Paul   _mesa_set_fetch_functions(texImage, 2);
354921caa29fbd332a2ee05a58df91e1664fbbc4e61fBrian Paul   compute_texture_size(ctx, texImage);
35502c3d34c905fa6b831a066afae83b938de05eb241Gareth Hughes
35512c3d34c905fa6b831a066afae83b938de05eb241Gareth Hughes   texelBytes = texImage->TexFormat->TexelBytes;
35528e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
35538e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   /* allocate memory */
355489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   if (texImage->IsCompressed)
355589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      sizeInBytes = texImage->CompressedSize;
355689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   else
3557ea6ddcbe0e4a0049ee8b9d244d86b6981fc6438bBrian Paul      sizeInBytes = texImage->Width * texImage->Height * texelBytes;
35584cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul   texImage->Data = _mesa_alloc_texmemory(sizeInBytes);
35597d58f44f73be59bd3583e6dfeedf56c43f7fbd55Brian Paul   if (!texImage->Data) {
35607d58f44f73be59bd3583e6dfeedf56c43f7fbd55Brian Paul      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage2D");
35617d58f44f73be59bd3583e6dfeedf56c43f7fbd55Brian Paul      return;
35627d58f44f73be59bd3583e6dfeedf56c43f7fbd55Brian Paul   }
35638e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
356471699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   pixels = _mesa_validate_pbo_teximage(ctx, 2, width, height, 1, format, type,
3565e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul                                        pixels, packing, "glTexImage2D");
35666b6c96bdeb580052cb9fa3831f1cd574f0e85728Brian Paul   if (!pixels) {
35676b6c96bdeb580052cb9fa3831f1cd574f0e85728Brian Paul      /* Note: we check for a NULL image pointer here, _after_ we allocated
35686b6c96bdeb580052cb9fa3831f1cd574f0e85728Brian Paul       * memory for the texture.  That's what the GL spec calls for.
35696b6c96bdeb580052cb9fa3831f1cd574f0e85728Brian Paul       */
357089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return;
35716b6c96bdeb580052cb9fa3831f1cd574f0e85728Brian Paul   }
35726b6c96bdeb580052cb9fa3831f1cd574f0e85728Brian Paul   else {
3573b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul      GLint dstRowStride;
3574f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLboolean success;
3575f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (texImage->IsCompressed) {
3576a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul         dstRowStride
35775999c5b620236fb6a996cf56759aec31f01c126bBrian Paul            = _mesa_compressed_row_stride(texImage->TexFormat->MesaFormat, width);
3578f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
3579f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      else {
3580b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         dstRowStride = texImage->RowStride * texImage->TexFormat->TexelBytes;
3581f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
3582f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      ASSERT(texImage->TexFormat->StoreImage);
3583a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul      success = texImage->TexFormat->StoreImage(ctx, 2, texImage->_BaseFormat,
3584f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                texImage->TexFormat,
3585f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                texImage->Data,
3586f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                0, 0, 0,  /* dstX/Y/Zoffset */
3587b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                                                dstRowStride,
3588b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                                                texImage->ImageOffsets,
3589f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                width, height, 1,
3590f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                format, type, pixels, packing);
3591f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (!success) {
3592f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage2D");
3593f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
3594f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
3595f3b85c983f469875ac76081a61539a6c7b26777cBrian Paul
359671699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   _mesa_unmap_teximage_pbo(ctx, packing);
35978e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul}
35988e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
35998e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
36008e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
36016b6c96bdeb580052cb9fa3831f1cd574f0e85728Brian Paul/**
360289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * This is the software fallback for Driver.TexImage3D()
360389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * and Driver.CopyTexImage3D().
36046b6c96bdeb580052cb9fa3831f1cd574f0e85728Brian Paul * \sa _mesa_store_teximage2d()
36058e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul */
36068e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paulvoid
36078e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul_mesa_store_teximage3d(GLcontext *ctx, GLenum target, GLint level,
36088e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                       GLint internalFormat,
36098e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                       GLint width, GLint height, GLint depth, GLint border,
36108e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                       GLenum format, GLenum type, const void *pixels,
36118e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                       const struct gl_pixelstore_attrib *packing,
36128e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                       struct gl_texture_object *texObj,
36138e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                       struct gl_texture_image *texImage)
36148e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul{
3615e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul   GLint texelBytes, sizeInBytes;
3616a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) border;
36178e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
361821caa29fbd332a2ee05a58df91e1664fbbc4e61fBrian Paul   texImage->TexFormat
361921caa29fbd332a2ee05a58df91e1664fbbc4e61fBrian Paul      = ctx->Driver.ChooseTextureFormat(ctx, internalFormat, format, type);
362021caa29fbd332a2ee05a58df91e1664fbbc4e61fBrian Paul   ASSERT(texImage->TexFormat);
362121caa29fbd332a2ee05a58df91e1664fbbc4e61fBrian Paul
362221caa29fbd332a2ee05a58df91e1664fbbc4e61fBrian Paul   _mesa_set_fetch_functions(texImage, 3);
362321caa29fbd332a2ee05a58df91e1664fbbc4e61fBrian Paul   compute_texture_size(ctx, texImage);
36242c3d34c905fa6b831a066afae83b938de05eb241Gareth Hughes
3625197c526d63e1d4ea96f29eece392cdc389770b38Brian Paul   texelBytes = texImage->TexFormat->TexelBytes;
36268e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
36278e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul   /* allocate memory */
362889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   if (texImage->IsCompressed)
362989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      sizeInBytes = texImage->CompressedSize;
363089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   else
363189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      sizeInBytes = width * height * depth * texelBytes;
36324cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul   texImage->Data = _mesa_alloc_texmemory(sizeInBytes);
36337d58f44f73be59bd3583e6dfeedf56c43f7fbd55Brian Paul   if (!texImage->Data) {
36347d58f44f73be59bd3583e6dfeedf56c43f7fbd55Brian Paul      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage3D");
36357d58f44f73be59bd3583e6dfeedf56c43f7fbd55Brian Paul      return;
36367d58f44f73be59bd3583e6dfeedf56c43f7fbd55Brian Paul   }
36378e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
3638e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   pixels = _mesa_validate_pbo_teximage(ctx, 3, width, height, depth, format,
3639e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul                                        type, pixels, packing, "glTexImage3D");
36406b6c96bdeb580052cb9fa3831f1cd574f0e85728Brian Paul   if (!pixels) {
36416b6c96bdeb580052cb9fa3831f1cd574f0e85728Brian Paul      /* Note: we check for a NULL image pointer here, _after_ we allocated
36426b6c96bdeb580052cb9fa3831f1cd574f0e85728Brian Paul       * memory for the texture.  That's what the GL spec calls for.
36436b6c96bdeb580052cb9fa3831f1cd574f0e85728Brian Paul       */
364489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return;
36456b6c96bdeb580052cb9fa3831f1cd574f0e85728Brian Paul   }
36466b6c96bdeb580052cb9fa3831f1cd574f0e85728Brian Paul   else {
3647b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul      GLint dstRowStride;
3648f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLboolean success;
3649f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (texImage->IsCompressed) {
3650a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul         dstRowStride
36515999c5b620236fb6a996cf56759aec31f01c126bBrian Paul            = _mesa_compressed_row_stride(texImage->TexFormat->MesaFormat, width);
3652f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
3653f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      else {
3654b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         dstRowStride = texImage->RowStride * texImage->TexFormat->TexelBytes;
3655f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
3656f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      ASSERT(texImage->TexFormat->StoreImage);
3657a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul      success = texImage->TexFormat->StoreImage(ctx, 3, texImage->_BaseFormat,
3658f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                texImage->TexFormat,
3659f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                texImage->Data,
3660f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                0, 0, 0,  /* dstX/Y/Zoffset */
3661b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                                                dstRowStride,
3662b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                                                texImage->ImageOffsets,
3663f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                width, height, depth,
3664f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                format, type, pixels, packing);
3665f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (!success) {
3666f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage3D");
3667f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
3668f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
3669f3b85c983f469875ac76081a61539a6c7b26777cBrian Paul
367071699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   _mesa_unmap_teximage_pbo(ctx, packing);
36718e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul}
36728e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
36738e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
36748e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
36758e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
36768e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul/*
367789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * This is the software fallback for Driver.TexSubImage1D()
367889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * and Driver.CopyTexSubImage1D().
36798e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul */
36808e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paulvoid
36818e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul_mesa_store_texsubimage1d(GLcontext *ctx, GLenum target, GLint level,
36828e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                          GLint xoffset, GLint width,
36838e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                          GLenum format, GLenum type, const void *pixels,
36848e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                          const struct gl_pixelstore_attrib *packing,
36858e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                          struct gl_texture_object *texObj,
36868e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                          struct gl_texture_image *texImage)
36878e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul{
3688b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul   /* get pointer to src pixels (may be in a pbo which we'll map here) */
3689e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   pixels = _mesa_validate_pbo_teximage(ctx, 1, width, 1, 1, format, type,
3690e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul                                        pixels, packing, "glTexSubImage1D");
36917a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul   if (!pixels)
36927a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul      return;
36937a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul
3694f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   {
3695b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul      const GLint dstRowStride = 0;
3696f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLboolean success;
3697f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      ASSERT(texImage->TexFormat->StoreImage);
3698a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul      success = texImage->TexFormat->StoreImage(ctx, 1, texImage->_BaseFormat,
3699f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                texImage->TexFormat,
3700f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                texImage->Data,
3701f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                xoffset, 0, 0,  /* offsets */
3702b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                                                dstRowStride,
3703b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                                                texImage->ImageOffsets,
3704f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                width, 1, 1,
3705f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                format, type, pixels, packing);
3706f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (!success) {
3707f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage1D");
3708f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
3709f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
37103893e638e6521b9c070e01c0b31d22754ff97a88Brian Paul
371171699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   _mesa_unmap_teximage_pbo(ctx, packing);
37128e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul}
37138e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
37148e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
371589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
3716f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul/**
371789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * This is the software fallback for Driver.TexSubImage2D()
371889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * and Driver.CopyTexSubImage2D().
37198e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul */
37208e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paulvoid
37218e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul_mesa_store_texsubimage2d(GLcontext *ctx, GLenum target, GLint level,
37228e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                          GLint xoffset, GLint yoffset,
37238e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                          GLint width, GLint height,
37248e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                          GLenum format, GLenum type, const void *pixels,
37258e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                          const struct gl_pixelstore_attrib *packing,
37268e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                          struct gl_texture_object *texObj,
37278e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                          struct gl_texture_image *texImage)
37288e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul{
3729b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul   /* get pointer to src pixels (may be in a pbo which we'll map here) */
373071699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   pixels = _mesa_validate_pbo_teximage(ctx, 2, width, height, 1, format, type,
3731e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul                                        pixels, packing, "glTexSubImage2D");
37327a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul   if (!pixels)
37337a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul      return;
37347a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul
3735f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   {
3736b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul      GLint dstRowStride = 0;
3737f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLboolean success;
3738f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (texImage->IsCompressed) {
37395999c5b620236fb6a996cf56759aec31f01c126bBrian Paul         dstRowStride = _mesa_compressed_row_stride(texImage->TexFormat->MesaFormat,
3740f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                    texImage->Width);
3741f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
3742f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      else {
3743b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         dstRowStride = texImage->RowStride * texImage->TexFormat->TexelBytes;
3744f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
3745f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      ASSERT(texImage->TexFormat->StoreImage);
3746a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul      success = texImage->TexFormat->StoreImage(ctx, 2, texImage->_BaseFormat,
3747f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                texImage->TexFormat,
3748f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                texImage->Data,
3749f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                xoffset, yoffset, 0,
3750b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                                                dstRowStride,
3751b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                                                texImage->ImageOffsets,
3752f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                width, height, 1,
3753f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                format, type, pixels, packing);
3754f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (!success) {
3755f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage2D");
3756f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
3757f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
37583893e638e6521b9c070e01c0b31d22754ff97a88Brian Paul
375971699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   _mesa_unmap_teximage_pbo(ctx, packing);
37608e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul}
37618e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
37628e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
37638e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul/*
37648e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul * This is the software fallback for Driver.TexSubImage3D().
376589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * and Driver.CopyTexSubImage3D().
37668e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul */
37678e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paulvoid
37688e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul_mesa_store_texsubimage3d(GLcontext *ctx, GLenum target, GLint level,
37698e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                          GLint xoffset, GLint yoffset, GLint zoffset,
37708e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                          GLint width, GLint height, GLint depth,
37718e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                          GLenum format, GLenum type, const void *pixels,
37728e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                          const struct gl_pixelstore_attrib *packing,
37738e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                          struct gl_texture_object *texObj,
37748e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                          struct gl_texture_image *texImage)
37758e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul{
3776b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul   /* get pointer to src pixels (may be in a pbo which we'll map here) */
3777e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   pixels = _mesa_validate_pbo_teximage(ctx, 3, width, height, depth, format,
3778e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul                                        type, pixels, packing,
3779e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul                                        "glTexSubImage3D");
3780f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   if (!pixels)
3781f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      return;
3782f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul
3783f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   {
3784b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul      GLint dstRowStride;
3785f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      GLboolean success;
3786f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (texImage->IsCompressed) {
37875999c5b620236fb6a996cf56759aec31f01c126bBrian Paul         dstRowStride = _mesa_compressed_row_stride(texImage->TexFormat->MesaFormat,
3788f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                    texImage->Width);
3789f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
3790f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      else {
3791b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul         dstRowStride = texImage->RowStride * texImage->TexFormat->TexelBytes;
3792f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
3793f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      ASSERT(texImage->TexFormat->StoreImage);
3794a9fc8ba756dd25a07dc19058fe60f65bda82a055Brian Paul      success = texImage->TexFormat->StoreImage(ctx, 3, texImage->_BaseFormat,
3795f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                texImage->TexFormat,
3796f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                texImage->Data,
3797f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                xoffset, yoffset, zoffset,
3798b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                                                dstRowStride,
3799b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul                                                texImage->ImageOffsets,
3800f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                width, height, depth,
3801f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul                                                format, type, pixels, packing);
3802f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      if (!success) {
3803f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage3D");
3804f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul      }
3805f959f6e1dc27c71fc0ccc56e09b29101b3bf3b97Brian Paul   }
38068f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paul
380771699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   _mesa_unmap_teximage_pbo(ctx, packing);
38088e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul}
38098e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
38108e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
38112aadbf41dfd4f63c6118d0ad2d8659d289cbe454Brian Paul/*
38128e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul * Fallback for Driver.CompressedTexImage1D()
38138e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul */
38148e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paulvoid
38158e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul_mesa_store_compressed_teximage1d(GLcontext *ctx, GLenum target, GLint level,
38168e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                  GLint internalFormat,
38178e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                  GLint width, GLint border,
38188e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                  GLsizei imageSize, const GLvoid *data,
38198e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                  struct gl_texture_object *texObj,
38208e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                  struct gl_texture_image *texImage)
38218e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul{
382289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   /* this space intentionally left blank */
3823a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) ctx;
3824a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) target; (void) level;
3825a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) internalFormat;
3826a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) width; (void) border;
3827a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) imageSize; (void) data;
3828a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) texObj;
3829a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) texImage;
38308e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul}
38318e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
38328e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
38338e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
3834b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul/**
38358e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul * Fallback for Driver.CompressedTexImage2D()
38368e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul */
38378e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paulvoid
38388e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul_mesa_store_compressed_teximage2d(GLcontext *ctx, GLenum target, GLint level,
38398e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                  GLint internalFormat,
38408e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                  GLint width, GLint height, GLint border,
38418e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                  GLsizei imageSize, const GLvoid *data,
38428e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                  struct gl_texture_object *texObj,
38438e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                  struct gl_texture_image *texImage)
38448e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul{
3845a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) width; (void) height; (void) border;
3846a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul
384789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   /* This is pretty simple, basically just do a memcpy without worrying
384889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul    * about the usual image unpacking or image transfer operations.
38498e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul    */
385089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   ASSERT(texObj);
385189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   ASSERT(texImage);
385289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   ASSERT(texImage->Width > 0);
385389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   ASSERT(texImage->Height > 0);
385489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   ASSERT(texImage->Depth == 1);
385589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   ASSERT(texImage->Data == NULL); /* was freed in glCompressedTexImage2DARB */
385689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
385721caa29fbd332a2ee05a58df91e1664fbbc4e61fBrian Paul   texImage->TexFormat
385821caa29fbd332a2ee05a58df91e1664fbbc4e61fBrian Paul      = ctx->Driver.ChooseTextureFormat(ctx, internalFormat, 0, 0);
385921caa29fbd332a2ee05a58df91e1664fbbc4e61fBrian Paul   ASSERT(texImage->TexFormat);
386021caa29fbd332a2ee05a58df91e1664fbbc4e61fBrian Paul
386121caa29fbd332a2ee05a58df91e1664fbbc4e61fBrian Paul   _mesa_set_fetch_functions(texImage, 2);
386221caa29fbd332a2ee05a58df91e1664fbbc4e61fBrian Paul   compute_texture_size(ctx, texImage);
386389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
386489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   /* allocate storage */
38654cf6718725c7cf3bfb728118a8b14f8cf206c701Brian Paul   texImage->Data = _mesa_alloc_texmemory(imageSize);
386689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   if (!texImage->Data) {
386789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage2DARB");
386889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      return;
386989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   }
387089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
3871e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   data = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, data,
3872e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul                                                 &ctx->Unpack,
3873e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul                                                 "glCompressedTexImage2D");
38747a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul   if (!data)
38757a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul      return;
38767a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul
387789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   /* copy the data */
38784039cb8ca82d59451a6de8902fe35e693cdca3baBrian Paul   ASSERT(texImage->CompressedSize == (GLuint) imageSize);
387989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   MEMCPY(texImage->Data, data, imageSize);
38808f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paul
388171699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   _mesa_unmap_teximage_pbo(ctx, &ctx->Unpack);
38828e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul}
38838e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
38848e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
38858e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
38868e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul/*
38878e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul * Fallback for Driver.CompressedTexImage3D()
38888e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul */
38898e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paulvoid
38908e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul_mesa_store_compressed_teximage3d(GLcontext *ctx, GLenum target, GLint level,
38918e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                  GLint internalFormat,
38928e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                  GLint width, GLint height, GLint depth,
38938e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                  GLint border,
38948e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                  GLsizei imageSize, const GLvoid *data,
38958e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                  struct gl_texture_object *texObj,
38968e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul                                  struct gl_texture_image *texImage)
38978e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul{
389889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   /* this space intentionally left blank */
3899a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) ctx;
3900a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) target; (void) level;
3901a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) internalFormat;
3902a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) width; (void) height; (void) depth;
3903a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) border;
3904a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) imageSize; (void) data;
3905a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) texObj;
3906a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) texImage;
39078e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul}
39088e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
39098e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
39108e39ad2cd67d49be40ff0822f3269affdf83d601Brian Paul
391189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul/**
391289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * Fallback for Driver.CompressedTexSubImage1D()
3913e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul */
3914e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paulvoid
391589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul_mesa_store_compressed_texsubimage1d(GLcontext *ctx, GLenum target,
391689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                     GLint level,
391789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                     GLint xoffset, GLsizei width,
391889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                     GLenum format,
391989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                     GLsizei imageSize, const GLvoid *data,
392089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                     struct gl_texture_object *texObj,
392189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                     struct gl_texture_image *texImage)
3922e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul{
39235999c5b620236fb6a996cf56759aec31f01c126bBrian Paul   /* there are no compressed 1D texture formats yet */
3924a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) ctx;
3925a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) target; (void) level;
3926a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) xoffset; (void) width;
3927a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) format;
3928a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) imageSize; (void) data;
3929a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) texObj;
3930a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) texImage;
3931e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul}
3932e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul
3933e4276667dafc8de0c6e64af8300fc7598437de6eBrian Paul
393489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul/**
393589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * Fallback for Driver.CompressedTexSubImage2D()
393689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul */
393789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paulvoid
393889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul_mesa_store_compressed_texsubimage2d(GLcontext *ctx, GLenum target,
393989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                     GLint level,
394089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                     GLint xoffset, GLint yoffset,
394189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                     GLsizei width, GLsizei height,
394289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                     GLenum format,
394389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                     GLsizei imageSize, const GLvoid *data,
394489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                     struct gl_texture_object *texObj,
394589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                     struct gl_texture_image *texImage)
394689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul{
394789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   GLint bytesPerRow, destRowStride, srcRowStride;
394889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   GLint i, rows;
394989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   GLubyte *dest;
395089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   const GLubyte *src;
39515999c5b620236fb6a996cf56759aec31f01c126bBrian Paul   const GLuint mesaFormat = texImage->TexFormat->MesaFormat;
39525999c5b620236fb6a996cf56759aec31f01c126bBrian Paul
3953a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) format;
395489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
395589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   /* these should have been caught sooner */
395689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   ASSERT((width & 3) == 0 || width == 2 || width == 1);
395789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   ASSERT((height & 3) == 0 || height == 2 || height == 1);
395889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   ASSERT((xoffset & 3) == 0);
395989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   ASSERT((yoffset & 3) == 0);
396089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
3961b0b6d1abe5c7e629baebd4bf3d3ee3b17ba6ff08Brian Paul   /* get pointer to src pixels (may be in a pbo which we'll map here) */
3962e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul   data = _mesa_validate_pbo_compressed_teximage(ctx, imageSize, data,
3963e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul                                                 &ctx->Unpack,
3964e4b2356c07d31fbeeabb13b2fb47db703b473080Brian Paul                                                 "glCompressedTexSubImage2D");
39657a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul   if (!data)
39667a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul      return;
39677a6b71ef2944bae1718e8167b2faaceb8422071cBrian Paul
39685999c5b620236fb6a996cf56759aec31f01c126bBrian Paul   srcRowStride = _mesa_compressed_row_stride(mesaFormat, width);
396989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   src = (const GLubyte *) data;
397089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
39715999c5b620236fb6a996cf56759aec31f01c126bBrian Paul   destRowStride = _mesa_compressed_row_stride(mesaFormat, texImage->Width);
397289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   dest = _mesa_compressed_image_address(xoffset, yoffset, 0,
3973a5467697336f201abee19cba1521be80e5c87d3bBrian Paul                                         texImage->TexFormat->MesaFormat,
3974f4418f4d1c6dfe06af760226c5303e653b25b879Brian Paul                                         texImage->Width,
3975a5467697336f201abee19cba1521be80e5c87d3bBrian Paul                                         (GLubyte *) texImage->Data);
397689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
397789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   bytesPerRow = srcRowStride;
397889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   rows = height / 4;
397989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
398089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   for (i = 0; i < rows; i++) {
398189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      MEMCPY(dest, src, bytesPerRow);
398289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      dest += destRowStride;
398389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul      src += srcRowStride;
398489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul   }
39858f04c12e0ad876baa7eb9ed379e2b00150b376e0Brian Paul
398671699df7de8cc732caf6d4b8adb4ab2d1f0c7c1fKeith Whitwell   _mesa_unmap_teximage_pbo(ctx, &ctx->Unpack);
398789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul}
398889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
398989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul
399089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul/**
399189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul * Fallback for Driver.CompressedTexSubImage3D()
399289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul */
399389fb06fcc11cbe3f23521312155d6c55d869f526Brian Paulvoid
399489fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul_mesa_store_compressed_texsubimage3d(GLcontext *ctx, GLenum target,
399589fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                GLint level,
399689fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                GLint xoffset, GLint yoffset, GLint zoffset,
399789fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                GLsizei width, GLsizei height, GLsizei depth,
399889fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                GLenum format,
399989fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                GLsizei imageSize, const GLvoid *data,
400089fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                struct gl_texture_object *texObj,
400189fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul                                struct gl_texture_image *texImage)
400289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul{
40035999c5b620236fb6a996cf56759aec31f01c126bBrian Paul   /* there are no compressed 3D texture formats yet */
4004a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) ctx;
4005a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) target; (void) level;
4006a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) xoffset; (void) yoffset; (void) zoffset;
4007a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) width; (void) height; (void) depth;
4008a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) format;
4009a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) imageSize; (void) data;
4010a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) texObj;
4011a6c423d95663cfd8601cf84e10e8e1b12fa6ef15Brian Paul   (void) texImage;
401289fb06fcc11cbe3f23521312155d6c55d869f526Brian Paul}
4013