mesa: include mtypes.h less
[mesa.git] / src / mesa / main / texstore.c
1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2008 Brian Paul All Rights Reserved.
5 * Copyright (c) 2008-2009 VMware, Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 /*
27 * Authors:
28 * Brian Paul
29 */
30
31 /**
32 * The GL texture image functions in teximage.c basically just do
33 * error checking and data structure allocation. They in turn call
34 * device driver functions which actually copy/convert/store the user's
35 * texture image data.
36 *
37 * However, most device drivers will be able to use the fallback functions
38 * in this file. That is, most drivers will have the following bit of
39 * code:
40 * ctx->Driver.TexImage = _mesa_store_teximage;
41 * ctx->Driver.TexSubImage = _mesa_store_texsubimage;
42 * etc...
43 *
44 * Texture image processing is actually kind of complicated. We have to do:
45 * Format/type conversions
46 * pixel unpacking
47 * pixel transfer (scale, bais, lookup, etc)
48 *
49 * These functions can handle most everything, including processing full
50 * images and sub-images.
51 */
52
53
54 #include "errors.h"
55 #include "glheader.h"
56 #include "bufferobj.h"
57 #include "format_pack.h"
58 #include "format_utils.h"
59 #include "image.h"
60 #include "macros.h"
61 #include "mipmap.h"
62 #include "mtypes.h"
63 #include "pack.h"
64 #include "pbo.h"
65 #include "imports.h"
66 #include "texcompress.h"
67 #include "texcompress_fxt1.h"
68 #include "texcompress_rgtc.h"
69 #include "texcompress_s3tc.h"
70 #include "texcompress_etc.h"
71 #include "texcompress_bptc.h"
72 #include "teximage.h"
73 #include "texstore.h"
74 #include "enums.h"
75 #include "glformats.h"
76 #include "pixeltransfer.h"
77 #include "util/format_rgb9e5.h"
78 #include "util/format_r11g11b10f.h"
79
80
81 enum {
82 ZERO = 4,
83 ONE = 5
84 };
85
86
87 /**
88 * Texture image storage function.
89 */
90 typedef GLboolean (*StoreTexImageFunc)(TEXSTORE_PARAMS);
91
92
93 /**
94 * Teximage storage routine for when a simple memcpy will do.
95 * No pixel transfer operations or special texel encodings allowed.
96 * 1D, 2D and 3D images supported.
97 */
98 void
99 _mesa_memcpy_texture(struct gl_context *ctx,
100 GLuint dimensions,
101 mesa_format dstFormat,
102 GLint dstRowStride,
103 GLubyte **dstSlices,
104 GLint srcWidth, GLint srcHeight, GLint srcDepth,
105 GLenum srcFormat, GLenum srcType,
106 const GLvoid *srcAddr,
107 const struct gl_pixelstore_attrib *srcPacking)
108 {
109 const GLint srcRowStride = _mesa_image_row_stride(srcPacking, srcWidth,
110 srcFormat, srcType);
111 const GLint srcImageStride = _mesa_image_image_stride(srcPacking,
112 srcWidth, srcHeight, srcFormat, srcType);
113 const GLubyte *srcImage = (const GLubyte *) _mesa_image_address(dimensions,
114 srcPacking, srcAddr, srcWidth, srcHeight, srcFormat, srcType, 0, 0, 0);
115 const GLuint texelBytes = _mesa_get_format_bytes(dstFormat);
116 const GLint bytesPerRow = srcWidth * texelBytes;
117
118 if (dstRowStride == srcRowStride &&
119 dstRowStride == bytesPerRow) {
120 /* memcpy image by image */
121 GLint img;
122 for (img = 0; img < srcDepth; img++) {
123 GLubyte *dstImage = dstSlices[img];
124 memcpy(dstImage, srcImage, bytesPerRow * srcHeight);
125 srcImage += srcImageStride;
126 }
127 }
128 else {
129 /* memcpy row by row */
130 GLint img, row;
131 for (img = 0; img < srcDepth; img++) {
132 const GLubyte *srcRow = srcImage;
133 GLubyte *dstRow = dstSlices[img];
134 for (row = 0; row < srcHeight; row++) {
135 memcpy(dstRow, srcRow, bytesPerRow);
136 dstRow += dstRowStride;
137 srcRow += srcRowStride;
138 }
139 srcImage += srcImageStride;
140 }
141 }
142 }
143
144
145 /**
146 * Store a 32-bit integer or float depth component texture image.
147 */
148 static GLboolean
149 _mesa_texstore_z32(TEXSTORE_PARAMS)
150 {
151 const GLuint depthScale = 0xffffffff;
152 GLenum dstType;
153 (void) dims;
154 assert(dstFormat == MESA_FORMAT_Z_UNORM32 ||
155 dstFormat == MESA_FORMAT_Z_FLOAT32);
156 assert(_mesa_get_format_bytes(dstFormat) == sizeof(GLuint));
157
158 if (dstFormat == MESA_FORMAT_Z_UNORM32)
159 dstType = GL_UNSIGNED_INT;
160 else
161 dstType = GL_FLOAT;
162
163 {
164 /* general path */
165 GLint img, row;
166 for (img = 0; img < srcDepth; img++) {
167 GLubyte *dstRow = dstSlices[img];
168 for (row = 0; row < srcHeight; row++) {
169 const GLvoid *src = _mesa_image_address(dims, srcPacking,
170 srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, row, 0);
171 _mesa_unpack_depth_span(ctx, srcWidth,
172 dstType, dstRow,
173 depthScale, srcType, src, srcPacking);
174 dstRow += dstRowStride;
175 }
176 }
177 }
178 return GL_TRUE;
179 }
180
181
182 /**
183 * Store a 24-bit integer depth component texture image.
184 */
185 static GLboolean
186 _mesa_texstore_x8_z24(TEXSTORE_PARAMS)
187 {
188 const GLuint depthScale = 0xffffff;
189
190 (void) dims;
191 assert(dstFormat == MESA_FORMAT_Z24_UNORM_X8_UINT);
192
193 {
194 /* general path */
195 GLint img, row;
196 for (img = 0; img < srcDepth; img++) {
197 GLubyte *dstRow = dstSlices[img];
198 for (row = 0; row < srcHeight; row++) {
199 const GLvoid *src = _mesa_image_address(dims, srcPacking,
200 srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, row, 0);
201 _mesa_unpack_depth_span(ctx, srcWidth,
202 GL_UNSIGNED_INT, (GLuint *) dstRow,
203 depthScale, srcType, src, srcPacking);
204 dstRow += dstRowStride;
205 }
206 }
207 }
208 return GL_TRUE;
209 }
210
211
212 /**
213 * Store a 24-bit integer depth component texture image.
214 */
215 static GLboolean
216 _mesa_texstore_z24_x8(TEXSTORE_PARAMS)
217 {
218 const GLuint depthScale = 0xffffff;
219
220 (void) dims;
221 assert(dstFormat == MESA_FORMAT_X8_UINT_Z24_UNORM);
222
223 {
224 /* general path */
225 GLint img, row;
226 for (img = 0; img < srcDepth; img++) {
227 GLubyte *dstRow = dstSlices[img];
228 for (row = 0; row < srcHeight; row++) {
229 const GLvoid *src = _mesa_image_address(dims, srcPacking,
230 srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, row, 0);
231 GLuint *dst = (GLuint *) dstRow;
232 GLint i;
233 _mesa_unpack_depth_span(ctx, srcWidth,
234 GL_UNSIGNED_INT, dst,
235 depthScale, srcType, src, srcPacking);
236 for (i = 0; i < srcWidth; i++)
237 dst[i] <<= 8;
238 dstRow += dstRowStride;
239 }
240 }
241 }
242 return GL_TRUE;
243 }
244
245
246 /**
247 * Store a 16-bit integer depth component texture image.
248 */
249 static GLboolean
250 _mesa_texstore_z16(TEXSTORE_PARAMS)
251 {
252 const GLuint depthScale = 0xffff;
253 (void) dims;
254 assert(dstFormat == MESA_FORMAT_Z_UNORM16);
255 assert(_mesa_get_format_bytes(dstFormat) == sizeof(GLushort));
256
257 {
258 /* general path */
259 GLint img, row;
260 for (img = 0; img < srcDepth; img++) {
261 GLubyte *dstRow = dstSlices[img];
262 for (row = 0; row < srcHeight; row++) {
263 const GLvoid *src = _mesa_image_address(dims, srcPacking,
264 srcAddr, srcWidth, srcHeight, srcFormat, srcType, img, row, 0);
265 GLushort *dst16 = (GLushort *) dstRow;
266 _mesa_unpack_depth_span(ctx, srcWidth,
267 GL_UNSIGNED_SHORT, dst16, depthScale,
268 srcType, src, srcPacking);
269 dstRow += dstRowStride;
270 }
271 }
272 }
273 return GL_TRUE;
274 }
275
276
277 /**
278 * Texstore for _mesa_texformat_ycbcr or _mesa_texformat_ycbcr_REV.
279 */
280 static GLboolean
281 _mesa_texstore_ycbcr(TEXSTORE_PARAMS)
282 {
283 const GLboolean littleEndian = _mesa_little_endian();
284
285 (void) ctx; (void) dims; (void) baseInternalFormat;
286
287 assert((dstFormat == MESA_FORMAT_YCBCR) ||
288 (dstFormat == MESA_FORMAT_YCBCR_REV));
289 assert(_mesa_get_format_bytes(dstFormat) == 2);
290 assert(ctx->Extensions.MESA_ycbcr_texture);
291 assert(srcFormat == GL_YCBCR_MESA);
292 assert((srcType == GL_UNSIGNED_SHORT_8_8_MESA) ||
293 (srcType == GL_UNSIGNED_SHORT_8_8_REV_MESA));
294 assert(baseInternalFormat == GL_YCBCR_MESA);
295
296 /* always just memcpy since no pixel transfer ops apply */
297 _mesa_memcpy_texture(ctx, dims,
298 dstFormat,
299 dstRowStride, dstSlices,
300 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
301 srcAddr, srcPacking);
302
303 /* Check if we need byte swapping */
304 /* XXX the logic here _might_ be wrong */
305 if (srcPacking->SwapBytes ^
306 (srcType == GL_UNSIGNED_SHORT_8_8_REV_MESA) ^
307 (dstFormat == MESA_FORMAT_YCBCR_REV) ^
308 !littleEndian) {
309 GLint img, row;
310 for (img = 0; img < srcDepth; img++) {
311 GLubyte *dstRow = dstSlices[img];
312 for (row = 0; row < srcHeight; row++) {
313 _mesa_swap2((GLushort *) dstRow, srcWidth);
314 dstRow += dstRowStride;
315 }
316 }
317 }
318 return GL_TRUE;
319 }
320
321
322 /**
323 * Store a combined depth/stencil texture image.
324 */
325 static GLboolean
326 _mesa_texstore_z24_s8(TEXSTORE_PARAMS)
327 {
328 const GLuint depthScale = 0xffffff;
329 const GLint srcRowStride
330 = _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
331 GLint img, row;
332 GLuint *depth = malloc(srcWidth * sizeof(GLuint));
333 GLubyte *stencil = malloc(srcWidth * sizeof(GLubyte));
334
335 assert(dstFormat == MESA_FORMAT_S8_UINT_Z24_UNORM);
336 assert(srcFormat == GL_DEPTH_STENCIL_EXT ||
337 srcFormat == GL_DEPTH_COMPONENT ||
338 srcFormat == GL_STENCIL_INDEX);
339 assert(srcFormat != GL_DEPTH_STENCIL_EXT ||
340 srcType == GL_UNSIGNED_INT_24_8_EXT ||
341 srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
342
343 if (!depth || !stencil) {
344 free(depth);
345 free(stencil);
346 return GL_FALSE;
347 }
348
349 /* In case we only upload depth we need to preserve the stencil */
350 for (img = 0; img < srcDepth; img++) {
351 GLuint *dstRow = (GLuint *) dstSlices[img];
352 const GLubyte *src
353 = (const GLubyte *) _mesa_image_address(dims, srcPacking, srcAddr,
354 srcWidth, srcHeight,
355 srcFormat, srcType,
356 img, 0, 0);
357 for (row = 0; row < srcHeight; row++) {
358 GLint i;
359 GLboolean keepdepth = GL_FALSE, keepstencil = GL_FALSE;
360
361 if (srcFormat == GL_DEPTH_COMPONENT) { /* preserve stencil */
362 keepstencil = GL_TRUE;
363 }
364 else if (srcFormat == GL_STENCIL_INDEX) { /* preserve depth */
365 keepdepth = GL_TRUE;
366 }
367
368 if (keepdepth == GL_FALSE)
369 /* the 24 depth bits will be in the low position: */
370 _mesa_unpack_depth_span(ctx, srcWidth,
371 GL_UNSIGNED_INT, /* dst type */
372 keepstencil ? depth : dstRow, /* dst addr */
373 depthScale,
374 srcType, src, srcPacking);
375
376 if (keepstencil == GL_FALSE)
377 /* get the 8-bit stencil values */
378 _mesa_unpack_stencil_span(ctx, srcWidth,
379 GL_UNSIGNED_BYTE, /* dst type */
380 stencil, /* dst addr */
381 srcType, src, srcPacking,
382 ctx->_ImageTransferState);
383
384 for (i = 0; i < srcWidth; i++) {
385 if (keepstencil)
386 dstRow[i] = depth[i] << 8 | (dstRow[i] & 0x000000FF);
387 else
388 dstRow[i] = (dstRow[i] & 0xFFFFFF00) | (stencil[i] & 0xFF);
389 }
390 src += srcRowStride;
391 dstRow += dstRowStride / sizeof(GLuint);
392 }
393 }
394
395 free(depth);
396 free(stencil);
397 return GL_TRUE;
398 }
399
400
401 /**
402 * Store a combined depth/stencil texture image.
403 */
404 static GLboolean
405 _mesa_texstore_s8_z24(TEXSTORE_PARAMS)
406 {
407 const GLuint depthScale = 0xffffff;
408 const GLint srcRowStride
409 = _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
410 GLint img, row;
411 GLuint *depth;
412 GLubyte *stencil;
413
414 assert(dstFormat == MESA_FORMAT_Z24_UNORM_S8_UINT);
415 assert(srcFormat == GL_DEPTH_STENCIL_EXT ||
416 srcFormat == GL_DEPTH_COMPONENT ||
417 srcFormat == GL_STENCIL_INDEX);
418 assert(srcFormat != GL_DEPTH_STENCIL_EXT ||
419 srcType == GL_UNSIGNED_INT_24_8_EXT ||
420 srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
421
422 depth = malloc(srcWidth * sizeof(GLuint));
423 stencil = malloc(srcWidth * sizeof(GLubyte));
424
425 if (!depth || !stencil) {
426 free(depth);
427 free(stencil);
428 return GL_FALSE;
429 }
430
431 for (img = 0; img < srcDepth; img++) {
432 GLuint *dstRow = (GLuint *) dstSlices[img];
433 const GLubyte *src
434 = (const GLubyte *) _mesa_image_address(dims, srcPacking, srcAddr,
435 srcWidth, srcHeight,
436 srcFormat, srcType,
437 img, 0, 0);
438 for (row = 0; row < srcHeight; row++) {
439 GLint i;
440 GLboolean keepdepth = GL_FALSE, keepstencil = GL_FALSE;
441
442 if (srcFormat == GL_DEPTH_COMPONENT) { /* preserve stencil */
443 keepstencil = GL_TRUE;
444 }
445 else if (srcFormat == GL_STENCIL_INDEX) { /* preserve depth */
446 keepdepth = GL_TRUE;
447 }
448
449 if (keepdepth == GL_FALSE)
450 /* the 24 depth bits will be in the low position: */
451 _mesa_unpack_depth_span(ctx, srcWidth,
452 GL_UNSIGNED_INT, /* dst type */
453 keepstencil ? depth : dstRow, /* dst addr */
454 depthScale,
455 srcType, src, srcPacking);
456
457 if (keepstencil == GL_FALSE)
458 /* get the 8-bit stencil values */
459 _mesa_unpack_stencil_span(ctx, srcWidth,
460 GL_UNSIGNED_BYTE, /* dst type */
461 stencil, /* dst addr */
462 srcType, src, srcPacking,
463 ctx->_ImageTransferState);
464
465 /* merge stencil values into depth values */
466 for (i = 0; i < srcWidth; i++) {
467 if (keepstencil)
468 dstRow[i] = depth[i] | (dstRow[i] & 0xFF000000);
469 else
470 dstRow[i] = (dstRow[i] & 0xFFFFFF) | (stencil[i] << 24);
471
472 }
473 src += srcRowStride;
474 dstRow += dstRowStride / sizeof(GLuint);
475 }
476 }
477
478 free(depth);
479 free(stencil);
480
481 return GL_TRUE;
482 }
483
484
485 /**
486 * Store simple 8-bit/value stencil texture data.
487 */
488 static GLboolean
489 _mesa_texstore_s8(TEXSTORE_PARAMS)
490 {
491 assert(dstFormat == MESA_FORMAT_S_UINT8);
492 assert(srcFormat == GL_STENCIL_INDEX);
493
494 {
495 const GLint srcRowStride
496 = _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
497 GLint img, row;
498 GLubyte *stencil = malloc(srcWidth * sizeof(GLubyte));
499
500 if (!stencil)
501 return GL_FALSE;
502
503 for (img = 0; img < srcDepth; img++) {
504 GLubyte *dstRow = dstSlices[img];
505 const GLubyte *src
506 = (const GLubyte *) _mesa_image_address(dims, srcPacking, srcAddr,
507 srcWidth, srcHeight,
508 srcFormat, srcType,
509 img, 0, 0);
510 for (row = 0; row < srcHeight; row++) {
511 GLint i;
512
513 /* get the 8-bit stencil values */
514 _mesa_unpack_stencil_span(ctx, srcWidth,
515 GL_UNSIGNED_BYTE, /* dst type */
516 stencil, /* dst addr */
517 srcType, src, srcPacking,
518 ctx->_ImageTransferState);
519 /* merge stencil values into depth values */
520 for (i = 0; i < srcWidth; i++)
521 dstRow[i] = stencil[i];
522
523 src += srcRowStride;
524 dstRow += dstRowStride / sizeof(GLubyte);
525 }
526 }
527
528 free(stencil);
529 }
530
531 return GL_TRUE;
532 }
533
534
535 static GLboolean
536 _mesa_texstore_z32f_x24s8(TEXSTORE_PARAMS)
537 {
538 GLint img, row;
539 const GLint srcRowStride
540 = _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType)
541 / sizeof(uint64_t);
542
543 assert(dstFormat == MESA_FORMAT_Z32_FLOAT_S8X24_UINT);
544 assert(srcFormat == GL_DEPTH_STENCIL ||
545 srcFormat == GL_DEPTH_COMPONENT ||
546 srcFormat == GL_STENCIL_INDEX);
547 assert(srcFormat != GL_DEPTH_STENCIL ||
548 srcType == GL_UNSIGNED_INT_24_8 ||
549 srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
550
551 /* In case we only upload depth we need to preserve the stencil */
552 for (img = 0; img < srcDepth; img++) {
553 uint64_t *dstRow = (uint64_t *) dstSlices[img];
554 const uint64_t *src
555 = (const uint64_t *) _mesa_image_address(dims, srcPacking, srcAddr,
556 srcWidth, srcHeight,
557 srcFormat, srcType,
558 img, 0, 0);
559 for (row = 0; row < srcHeight; row++) {
560 /* The unpack functions with:
561 * dstType = GL_FLOAT_32_UNSIGNED_INT_24_8_REV
562 * only write their own dword, so the other dword (stencil
563 * or depth) is preserved. */
564 if (srcFormat != GL_STENCIL_INDEX)
565 _mesa_unpack_depth_span(ctx, srcWidth,
566 GL_FLOAT_32_UNSIGNED_INT_24_8_REV, /* dst type */
567 dstRow, /* dst addr */
568 ~0U, srcType, src, srcPacking);
569
570 if (srcFormat != GL_DEPTH_COMPONENT)
571 _mesa_unpack_stencil_span(ctx, srcWidth,
572 GL_FLOAT_32_UNSIGNED_INT_24_8_REV, /* dst type */
573 dstRow, /* dst addr */
574 srcType, src, srcPacking,
575 ctx->_ImageTransferState);
576
577 src += srcRowStride;
578 dstRow += dstRowStride / sizeof(uint64_t);
579 }
580 }
581 return GL_TRUE;
582 }
583
584 static GLboolean
585 texstore_depth_stencil(TEXSTORE_PARAMS)
586 {
587 static StoreTexImageFunc table[MESA_FORMAT_COUNT];
588 static GLboolean initialized = GL_FALSE;
589
590 if (!initialized) {
591 memset(table, 0, sizeof table);
592
593 table[MESA_FORMAT_S8_UINT_Z24_UNORM] = _mesa_texstore_z24_s8;
594 table[MESA_FORMAT_Z24_UNORM_S8_UINT] = _mesa_texstore_s8_z24;
595 table[MESA_FORMAT_Z_UNORM16] = _mesa_texstore_z16;
596 table[MESA_FORMAT_Z24_UNORM_X8_UINT] = _mesa_texstore_x8_z24;
597 table[MESA_FORMAT_X8_UINT_Z24_UNORM] = _mesa_texstore_z24_x8;
598 table[MESA_FORMAT_Z_UNORM32] = _mesa_texstore_z32;
599 table[MESA_FORMAT_S_UINT8] = _mesa_texstore_s8;
600 table[MESA_FORMAT_Z_FLOAT32] = _mesa_texstore_z32;
601 table[MESA_FORMAT_Z32_FLOAT_S8X24_UINT] = _mesa_texstore_z32f_x24s8;
602
603 initialized = GL_TRUE;
604 }
605
606 assert(table[dstFormat]);
607 return table[dstFormat](ctx, dims, baseInternalFormat,
608 dstFormat, dstRowStride, dstSlices,
609 srcWidth, srcHeight, srcDepth,
610 srcFormat, srcType, srcAddr, srcPacking);
611 }
612
613 static GLboolean
614 texstore_compressed(TEXSTORE_PARAMS)
615 {
616 static StoreTexImageFunc table[MESA_FORMAT_COUNT];
617 static GLboolean initialized = GL_FALSE;
618
619 if (!initialized) {
620 memset(table, 0, sizeof table);
621
622 table[MESA_FORMAT_SRGB_DXT1] = _mesa_texstore_rgb_dxt1;
623 table[MESA_FORMAT_SRGBA_DXT1] = _mesa_texstore_rgba_dxt1;
624 table[MESA_FORMAT_SRGBA_DXT3] = _mesa_texstore_rgba_dxt3;
625 table[MESA_FORMAT_SRGBA_DXT5] = _mesa_texstore_rgba_dxt5;
626 table[MESA_FORMAT_RGB_FXT1] = _mesa_texstore_rgb_fxt1;
627 table[MESA_FORMAT_RGBA_FXT1] = _mesa_texstore_rgba_fxt1;
628 table[MESA_FORMAT_RGB_DXT1] = _mesa_texstore_rgb_dxt1;
629 table[MESA_FORMAT_RGBA_DXT1] = _mesa_texstore_rgba_dxt1;
630 table[MESA_FORMAT_RGBA_DXT3] = _mesa_texstore_rgba_dxt3;
631 table[MESA_FORMAT_RGBA_DXT5] = _mesa_texstore_rgba_dxt5;
632 table[MESA_FORMAT_R_RGTC1_UNORM] = _mesa_texstore_red_rgtc1;
633 table[MESA_FORMAT_R_RGTC1_SNORM] = _mesa_texstore_signed_red_rgtc1;
634 table[MESA_FORMAT_RG_RGTC2_UNORM] = _mesa_texstore_rg_rgtc2;
635 table[MESA_FORMAT_RG_RGTC2_SNORM] = _mesa_texstore_signed_rg_rgtc2;
636 table[MESA_FORMAT_L_LATC1_UNORM] = _mesa_texstore_red_rgtc1;
637 table[MESA_FORMAT_L_LATC1_SNORM] = _mesa_texstore_signed_red_rgtc1;
638 table[MESA_FORMAT_LA_LATC2_UNORM] = _mesa_texstore_rg_rgtc2;
639 table[MESA_FORMAT_LA_LATC2_SNORM] = _mesa_texstore_signed_rg_rgtc2;
640 table[MESA_FORMAT_ETC1_RGB8] = _mesa_texstore_etc1_rgb8;
641 table[MESA_FORMAT_ETC2_RGB8] = _mesa_texstore_etc2_rgb8;
642 table[MESA_FORMAT_ETC2_SRGB8] = _mesa_texstore_etc2_srgb8;
643 table[MESA_FORMAT_ETC2_RGBA8_EAC] = _mesa_texstore_etc2_rgba8_eac;
644 table[MESA_FORMAT_ETC2_SRGB8_ALPHA8_EAC] = _mesa_texstore_etc2_srgb8_alpha8_eac;
645 table[MESA_FORMAT_ETC2_R11_EAC] = _mesa_texstore_etc2_r11_eac;
646 table[MESA_FORMAT_ETC2_RG11_EAC] = _mesa_texstore_etc2_rg11_eac;
647 table[MESA_FORMAT_ETC2_SIGNED_R11_EAC] = _mesa_texstore_etc2_signed_r11_eac;
648 table[MESA_FORMAT_ETC2_SIGNED_RG11_EAC] = _mesa_texstore_etc2_signed_rg11_eac;
649 table[MESA_FORMAT_ETC2_RGB8_PUNCHTHROUGH_ALPHA1] =
650 _mesa_texstore_etc2_rgb8_punchthrough_alpha1;
651 table[MESA_FORMAT_ETC2_SRGB8_PUNCHTHROUGH_ALPHA1] =
652 _mesa_texstore_etc2_srgb8_punchthrough_alpha1;
653
654 table[MESA_FORMAT_BPTC_RGBA_UNORM] =
655 _mesa_texstore_bptc_rgba_unorm;
656 table[MESA_FORMAT_BPTC_SRGB_ALPHA_UNORM] =
657 _mesa_texstore_bptc_rgba_unorm;
658 table[MESA_FORMAT_BPTC_RGB_SIGNED_FLOAT] =
659 _mesa_texstore_bptc_rgb_signed_float;
660 table[MESA_FORMAT_BPTC_RGB_UNSIGNED_FLOAT] =
661 _mesa_texstore_bptc_rgb_unsigned_float;
662
663 initialized = GL_TRUE;
664 }
665
666 assert(table[dstFormat]);
667 return table[dstFormat](ctx, dims, baseInternalFormat,
668 dstFormat, dstRowStride, dstSlices,
669 srcWidth, srcHeight, srcDepth,
670 srcFormat, srcType, srcAddr, srcPacking);
671 }
672
673 static GLboolean
674 texstore_rgba(TEXSTORE_PARAMS)
675 {
676 void *tempImage = NULL, *tempRGBA = NULL;
677 int srcRowStride, img;
678 GLubyte *src, *dst;
679 uint32_t srcMesaFormat;
680 uint8_t rebaseSwizzle[4];
681 bool needRebase;
682 bool transferOpsDone = false;
683
684 /* We have to handle MESA_FORMAT_YCBCR manually because it is a special case
685 * and _mesa_format_convert does not support it. In this case the we only
686 * allow conversions between YCBCR formats and it is mostly a memcpy.
687 */
688 if (dstFormat == MESA_FORMAT_YCBCR || dstFormat == MESA_FORMAT_YCBCR_REV) {
689 return _mesa_texstore_ycbcr(ctx, dims, baseInternalFormat,
690 dstFormat, dstRowStride, dstSlices,
691 srcWidth, srcHeight, srcDepth,
692 srcFormat, srcType, srcAddr,
693 srcPacking);
694 }
695
696 /* We have to deal with GL_COLOR_INDEX manually because
697 * _mesa_format_convert does not handle this format. So what we do here is
698 * convert it to RGBA ubyte first and then convert from that to dst as usual.
699 */
700 if (srcFormat == GL_COLOR_INDEX) {
701 /* Notice that this will already handle byte swapping if necessary */
702 tempImage =
703 _mesa_unpack_color_index_to_rgba_ubyte(ctx, dims,
704 srcAddr, srcFormat, srcType,
705 srcWidth, srcHeight, srcDepth,
706 srcPacking,
707 ctx->_ImageTransferState);
708 if (!tempImage)
709 return GL_FALSE;
710
711 /* _mesa_unpack_color_index_to_rgba_ubyte has handled transferops
712 * if needed.
713 */
714 transferOpsDone = true;
715
716 /* Now we only have to adjust our src info for a conversion from
717 * the RGBA ubyte and then we continue as usual.
718 */
719 srcAddr = tempImage;
720 srcFormat = GL_RGBA;
721 srcType = GL_UNSIGNED_BYTE;
722 } else if (srcPacking->SwapBytes) {
723 /* We have to handle byte-swapping scenarios before calling
724 * _mesa_format_convert
725 */
726 GLint swapSize = _mesa_sizeof_packed_type(srcType);
727 if (swapSize == 2 || swapSize == 4) {
728 int imageStride = _mesa_image_image_stride(srcPacking, srcWidth, srcHeight, srcFormat, srcType);
729 int bufferSize = imageStride * srcDepth;
730 int layer;
731 const uint8_t *src;
732 uint8_t *dst;
733
734 tempImage = malloc(bufferSize);
735 if (!tempImage)
736 return GL_FALSE;
737 src = srcAddr;
738 dst = tempImage;
739 for (layer = 0; layer < srcDepth; layer++) {
740 _mesa_swap_bytes_2d_image(srcFormat, srcType,
741 srcPacking,
742 srcWidth, srcHeight,
743 dst, src);
744 src += imageStride;
745 dst += imageStride;
746 }
747 srcAddr = tempImage;
748 }
749 }
750
751 srcRowStride =
752 _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
753
754 srcMesaFormat = _mesa_format_from_format_and_type(srcFormat, srcType);
755 dstFormat = _mesa_get_srgb_format_linear(dstFormat);
756
757 /* If we have transferOps then we need to convert to RGBA float first,
758 then apply transferOps, then do the conversion to dst
759 */
760 if (!transferOpsDone &&
761 _mesa_texstore_needs_transfer_ops(ctx, baseInternalFormat, dstFormat)) {
762 /* Allocate RGBA float image */
763 int elementCount = srcWidth * srcHeight * srcDepth;
764 tempRGBA = malloc(4 * elementCount * sizeof(float));
765 if (!tempRGBA) {
766 free(tempImage);
767 free(tempRGBA);
768 return GL_FALSE;
769 }
770
771 /* Convert from src to RGBA float */
772 src = (GLubyte *) srcAddr;
773 dst = (GLubyte *) tempRGBA;
774 for (img = 0; img < srcDepth; img++) {
775 _mesa_format_convert(dst, RGBA32_FLOAT, 4 * srcWidth * sizeof(float),
776 src, srcMesaFormat, srcRowStride,
777 srcWidth, srcHeight, NULL);
778 src += srcHeight * srcRowStride;
779 dst += srcHeight * 4 * srcWidth * sizeof(float);
780 }
781
782 /* Apply transferOps */
783 _mesa_apply_rgba_transfer_ops(ctx, ctx->_ImageTransferState, elementCount,
784 (float(*)[4]) tempRGBA);
785
786 /* Now we have to adjust our src info for a conversion from
787 * the RGBA float image and then we continue as usual.
788 */
789 srcAddr = tempRGBA;
790 srcFormat = GL_RGBA;
791 srcType = GL_FLOAT;
792 srcRowStride = srcWidth * 4 * sizeof(float);
793 srcMesaFormat = RGBA32_FLOAT;
794 srcPacking = &ctx->DefaultPacking;
795 }
796
797 src = (GLubyte *)
798 _mesa_image_address(dims, srcPacking, srcAddr, srcWidth, srcHeight,
799 srcFormat, srcType, 0, 0, 0);
800
801 if (_mesa_get_format_base_format(dstFormat) != baseInternalFormat) {
802 needRebase =
803 _mesa_compute_rgba2base2rgba_component_mapping(baseInternalFormat,
804 rebaseSwizzle);
805 } else {
806 needRebase = false;
807 }
808
809 for (img = 0; img < srcDepth; img++) {
810 _mesa_format_convert(dstSlices[img], dstFormat, dstRowStride,
811 src, srcMesaFormat, srcRowStride,
812 srcWidth, srcHeight,
813 needRebase ? rebaseSwizzle : NULL);
814 src += srcHeight * srcRowStride;
815 }
816
817 free(tempImage);
818 free(tempRGBA);
819
820 return GL_TRUE;
821 }
822
823 GLboolean
824 _mesa_texstore_needs_transfer_ops(struct gl_context *ctx,
825 GLenum baseInternalFormat,
826 mesa_format dstFormat)
827 {
828 GLenum dstType;
829
830 /* There are different rules depending on the base format. */
831 switch (baseInternalFormat) {
832 case GL_DEPTH_COMPONENT:
833 case GL_DEPTH_STENCIL:
834 return ctx->Pixel.DepthScale != 1.0f ||
835 ctx->Pixel.DepthBias != 0.0f;
836
837 case GL_STENCIL_INDEX:
838 return GL_FALSE;
839
840 default:
841 /* Color formats.
842 * Pixel transfer ops (scale, bias, table lookup) do not apply
843 * to integer formats.
844 */
845 dstType = _mesa_get_format_datatype(dstFormat);
846
847 return dstType != GL_INT && dstType != GL_UNSIGNED_INT &&
848 ctx->_ImageTransferState;
849 }
850 }
851
852
853 GLboolean
854 _mesa_texstore_can_use_memcpy(struct gl_context *ctx,
855 GLenum baseInternalFormat, mesa_format dstFormat,
856 GLenum srcFormat, GLenum srcType,
857 const struct gl_pixelstore_attrib *srcPacking)
858 {
859 if (_mesa_texstore_needs_transfer_ops(ctx, baseInternalFormat, dstFormat)) {
860 return GL_FALSE;
861 }
862
863 /* The base internal format and the base Mesa format must match. */
864 if (baseInternalFormat != _mesa_get_format_base_format(dstFormat)) {
865 return GL_FALSE;
866 }
867
868 /* The Mesa format must match the input format and type. */
869 if (!_mesa_format_matches_format_and_type(dstFormat, srcFormat, srcType,
870 srcPacking->SwapBytes, NULL)) {
871 return GL_FALSE;
872 }
873
874 /* Depth texture data needs clamping in following cases:
875 * - Floating point dstFormat with signed srcType: clamp to [0.0, 1.0].
876 * - Fixed point dstFormat with signed srcType: clamp to [0, 2^n -1].
877 *
878 * All the cases except one (float dstFormat with float srcType) are ruled
879 * out by _mesa_format_matches_format_and_type() check above. Handle the
880 * remaining case here.
881 */
882 if ((baseInternalFormat == GL_DEPTH_COMPONENT ||
883 baseInternalFormat == GL_DEPTH_STENCIL) &&
884 (srcType == GL_FLOAT ||
885 srcType == GL_FLOAT_32_UNSIGNED_INT_24_8_REV)) {
886 return GL_FALSE;
887 }
888
889 return GL_TRUE;
890 }
891
892 static GLboolean
893 _mesa_texstore_memcpy(TEXSTORE_PARAMS)
894 {
895 if (!_mesa_texstore_can_use_memcpy(ctx, baseInternalFormat, dstFormat,
896 srcFormat, srcType, srcPacking)) {
897 return GL_FALSE;
898 }
899
900 _mesa_memcpy_texture(ctx, dims,
901 dstFormat,
902 dstRowStride, dstSlices,
903 srcWidth, srcHeight, srcDepth, srcFormat, srcType,
904 srcAddr, srcPacking);
905 return GL_TRUE;
906 }
907
908
909 /**
910 * Store user data into texture memory.
911 * Called via glTex[Sub]Image1/2/3D()
912 * \return GL_TRUE for success, GL_FALSE for failure (out of memory).
913 */
914 GLboolean
915 _mesa_texstore(TEXSTORE_PARAMS)
916 {
917 if (_mesa_texstore_memcpy(ctx, dims, baseInternalFormat,
918 dstFormat,
919 dstRowStride, dstSlices,
920 srcWidth, srcHeight, srcDepth,
921 srcFormat, srcType, srcAddr, srcPacking)) {
922 return GL_TRUE;
923 }
924
925 if (_mesa_is_depth_or_stencil_format(baseInternalFormat)) {
926 return texstore_depth_stencil(ctx, dims, baseInternalFormat,
927 dstFormat, dstRowStride, dstSlices,
928 srcWidth, srcHeight, srcDepth,
929 srcFormat, srcType, srcAddr, srcPacking);
930 } else if (_mesa_is_format_compressed(dstFormat)) {
931 return texstore_compressed(ctx, dims, baseInternalFormat,
932 dstFormat, dstRowStride, dstSlices,
933 srcWidth, srcHeight, srcDepth,
934 srcFormat, srcType, srcAddr, srcPacking);
935 } else {
936 return texstore_rgba(ctx, dims, baseInternalFormat,
937 dstFormat, dstRowStride, dstSlices,
938 srcWidth, srcHeight, srcDepth,
939 srcFormat, srcType, srcAddr, srcPacking);
940 }
941 }
942
943
944 /**
945 * Normally, we'll only _write_ texel data to a texture when we map it.
946 * But if the user is providing depth or stencil values and the texture
947 * image is a combined depth/stencil format, we'll actually read from
948 * the texture buffer too (in order to insert the depth or stencil values.
949 * \param userFormat the user-provided image format
950 * \param texFormat the destination texture format
951 */
952 static GLbitfield
953 get_read_write_mode(GLenum userFormat, mesa_format texFormat)
954 {
955 if ((userFormat == GL_STENCIL_INDEX || userFormat == GL_DEPTH_COMPONENT)
956 && _mesa_get_format_base_format(texFormat) == GL_DEPTH_STENCIL)
957 return GL_MAP_READ_BIT | GL_MAP_WRITE_BIT;
958 else
959 return GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT;
960 }
961
962
963 /**
964 * Helper function for storing 1D, 2D, 3D whole and subimages into texture
965 * memory.
966 * The source of the image data may be user memory or a PBO. In the later
967 * case, we'll map the PBO, copy from it, then unmap it.
968 */
969 static void
970 store_texsubimage(struct gl_context *ctx,
971 struct gl_texture_image *texImage,
972 GLint xoffset, GLint yoffset, GLint zoffset,
973 GLint width, GLint height, GLint depth,
974 GLenum format, GLenum type, const GLvoid *pixels,
975 const struct gl_pixelstore_attrib *packing,
976 const char *caller)
977
978 {
979 const GLbitfield mapMode = get_read_write_mode(format, texImage->TexFormat);
980 const GLenum target = texImage->TexObject->Target;
981 GLboolean success = GL_FALSE;
982 GLuint dims, slice, numSlices = 1, sliceOffset = 0;
983 GLint srcImageStride = 0;
984 const GLubyte *src;
985
986 assert(xoffset + width <= texImage->Width);
987 assert(yoffset + height <= texImage->Height);
988 assert(zoffset + depth <= texImage->Depth);
989
990 switch (target) {
991 case GL_TEXTURE_1D:
992 dims = 1;
993 break;
994 case GL_TEXTURE_2D_ARRAY:
995 case GL_TEXTURE_CUBE_MAP_ARRAY:
996 case GL_TEXTURE_3D:
997 dims = 3;
998 break;
999 default:
1000 dims = 2;
1001 }
1002
1003 /* get pointer to src pixels (may be in a pbo which we'll map here) */
1004 src = (const GLubyte *)
1005 _mesa_validate_pbo_teximage(ctx, dims, width, height, depth,
1006 format, type, pixels, packing, caller);
1007 if (!src)
1008 return;
1009
1010 /* compute slice info (and do some sanity checks) */
1011 switch (target) {
1012 case GL_TEXTURE_2D:
1013 case GL_TEXTURE_2D_MULTISAMPLE:
1014 case GL_TEXTURE_RECTANGLE:
1015 case GL_TEXTURE_CUBE_MAP:
1016 case GL_TEXTURE_EXTERNAL_OES:
1017 /* one image slice, nothing special needs to be done */
1018 break;
1019 case GL_TEXTURE_1D:
1020 assert(height == 1);
1021 assert(depth == 1);
1022 assert(yoffset == 0);
1023 assert(zoffset == 0);
1024 break;
1025 case GL_TEXTURE_1D_ARRAY:
1026 assert(depth == 1);
1027 assert(zoffset == 0);
1028 numSlices = height;
1029 sliceOffset = yoffset;
1030 height = 1;
1031 yoffset = 0;
1032 srcImageStride = _mesa_image_row_stride(packing, width, format, type);
1033 break;
1034 case GL_TEXTURE_2D_ARRAY:
1035 case GL_TEXTURE_2D_MULTISAMPLE_ARRAY:
1036 numSlices = depth;
1037 sliceOffset = zoffset;
1038 depth = 1;
1039 zoffset = 0;
1040 srcImageStride = _mesa_image_image_stride(packing, width, height,
1041 format, type);
1042 break;
1043 case GL_TEXTURE_3D:
1044 /* we'll store 3D images as a series of slices */
1045 numSlices = depth;
1046 sliceOffset = zoffset;
1047 srcImageStride = _mesa_image_image_stride(packing, width, height,
1048 format, type);
1049 break;
1050 case GL_TEXTURE_CUBE_MAP_ARRAY:
1051 numSlices = depth;
1052 sliceOffset = zoffset;
1053 srcImageStride = _mesa_image_image_stride(packing, width, height,
1054 format, type);
1055 break;
1056 default:
1057 _mesa_warning(ctx, "Unexpected target 0x%x in store_texsubimage()", target);
1058 return;
1059 }
1060
1061 assert(numSlices == 1 || srcImageStride != 0);
1062
1063 for (slice = 0; slice < numSlices; slice++) {
1064 GLubyte *dstMap;
1065 GLint dstRowStride;
1066
1067 ctx->Driver.MapTextureImage(ctx, texImage,
1068 slice + sliceOffset,
1069 xoffset, yoffset, width, height,
1070 mapMode, &dstMap, &dstRowStride);
1071 if (dstMap) {
1072 /* Note: we're only storing a 2D (or 1D) slice at a time but we need
1073 * to pass the right 'dims' value so that GL_UNPACK_SKIP_IMAGES is
1074 * used for 3D images.
1075 */
1076 success = _mesa_texstore(ctx, dims, texImage->_BaseFormat,
1077 texImage->TexFormat,
1078 dstRowStride,
1079 &dstMap,
1080 width, height, 1, /* w, h, d */
1081 format, type, src, packing);
1082
1083 ctx->Driver.UnmapTextureImage(ctx, texImage, slice + sliceOffset);
1084 }
1085
1086 src += srcImageStride;
1087
1088 if (!success)
1089 break;
1090 }
1091
1092 if (!success)
1093 _mesa_error(ctx, GL_OUT_OF_MEMORY, "%s", caller);
1094
1095 _mesa_unmap_teximage_pbo(ctx, packing);
1096 }
1097
1098
1099
1100 /**
1101 * Fallback code for ctx->Driver.TexImage().
1102 * Basically, allocate storage for the texture image, then copy the
1103 * user's image into it.
1104 */
1105 void
1106 _mesa_store_teximage(struct gl_context *ctx,
1107 GLuint dims,
1108 struct gl_texture_image *texImage,
1109 GLenum format, GLenum type, const GLvoid *pixels,
1110 const struct gl_pixelstore_attrib *packing)
1111 {
1112 assert(dims == 1 || dims == 2 || dims == 3);
1113
1114 if (texImage->Width == 0 || texImage->Height == 0 || texImage->Depth == 0)
1115 return;
1116
1117 /* allocate storage for texture data */
1118 if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
1119 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage%uD", dims);
1120 return;
1121 }
1122
1123 store_texsubimage(ctx, texImage,
1124 0, 0, 0, texImage->Width, texImage->Height, texImage->Depth,
1125 format, type, pixels, packing, "glTexImage");
1126 }
1127
1128
1129 /*
1130 * Fallback for Driver.TexSubImage().
1131 */
1132 void
1133 _mesa_store_texsubimage(struct gl_context *ctx, GLuint dims,
1134 struct gl_texture_image *texImage,
1135 GLint xoffset, GLint yoffset, GLint zoffset,
1136 GLint width, GLint height, GLint depth,
1137 GLenum format, GLenum type, const void *pixels,
1138 const struct gl_pixelstore_attrib *packing)
1139 {
1140 store_texsubimage(ctx, texImage,
1141 xoffset, yoffset, zoffset, width, height, depth,
1142 format, type, pixels, packing, "glTexSubImage");
1143 }
1144
1145 static void
1146 clear_image_to_zero(GLubyte *dstMap, GLint dstRowStride,
1147 GLsizei width, GLsizei height,
1148 GLsizei clearValueSize)
1149 {
1150 GLsizei y;
1151
1152 for (y = 0; y < height; y++) {
1153 memset(dstMap, 0, clearValueSize * width);
1154 dstMap += dstRowStride;
1155 }
1156 }
1157
1158 static void
1159 clear_image_to_value(GLubyte *dstMap, GLint dstRowStride,
1160 GLsizei width, GLsizei height,
1161 const GLvoid *clearValue,
1162 GLsizei clearValueSize)
1163 {
1164 GLsizei y, x;
1165
1166 for (y = 0; y < height; y++) {
1167 for (x = 0; x < width; x++) {
1168 memcpy(dstMap, clearValue, clearValueSize);
1169 dstMap += clearValueSize;
1170 }
1171 dstMap += dstRowStride - clearValueSize * width;
1172 }
1173 }
1174
1175 /*
1176 * Fallback for Driver.ClearTexSubImage().
1177 */
1178 void
1179 _mesa_store_cleartexsubimage(struct gl_context *ctx,
1180 struct gl_texture_image *texImage,
1181 GLint xoffset, GLint yoffset, GLint zoffset,
1182 GLsizei width, GLsizei height, GLsizei depth,
1183 const GLvoid *clearValue)
1184 {
1185 GLubyte *dstMap;
1186 GLint dstRowStride;
1187 GLsizeiptr clearValueSize;
1188 GLsizei z;
1189
1190 clearValueSize = _mesa_get_format_bytes(texImage->TexFormat);
1191
1192 for (z = 0; z < depth; z++) {
1193 ctx->Driver.MapTextureImage(ctx, texImage,
1194 z + zoffset, xoffset, yoffset,
1195 width, height,
1196 GL_MAP_WRITE_BIT,
1197 &dstMap, &dstRowStride);
1198 if (dstMap == NULL) {
1199 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glClearTex*Image");
1200 return;
1201 }
1202
1203 if (clearValue) {
1204 clear_image_to_value(dstMap, dstRowStride,
1205 width, height,
1206 clearValue,
1207 clearValueSize);
1208 } else {
1209 clear_image_to_zero(dstMap, dstRowStride,
1210 width, height,
1211 clearValueSize);
1212 }
1213
1214 ctx->Driver.UnmapTextureImage(ctx, texImage, z + zoffset);
1215 }
1216 }
1217
1218 /**
1219 * Fallback for Driver.CompressedTexImage()
1220 */
1221 void
1222 _mesa_store_compressed_teximage(struct gl_context *ctx, GLuint dims,
1223 struct gl_texture_image *texImage,
1224 GLsizei imageSize, const GLvoid *data)
1225 {
1226 /* only 2D and 3D compressed images are supported at this time */
1227 if (dims == 1) {
1228 _mesa_problem(ctx, "Unexpected glCompressedTexImage1D call");
1229 return;
1230 }
1231
1232 /* This is pretty simple, because unlike the general texstore path we don't
1233 * have to worry about the usual image unpacking or image transfer
1234 * operations.
1235 */
1236 assert(texImage);
1237 assert(texImage->Width > 0);
1238 assert(texImage->Height > 0);
1239 assert(texImage->Depth > 0);
1240
1241 /* allocate storage for texture data */
1242 if (!ctx->Driver.AllocTextureImageBuffer(ctx, texImage)) {
1243 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexImage%uD", dims);
1244 return;
1245 }
1246
1247 ctx->Driver.CompressedTexSubImage(ctx, dims, texImage,
1248 0, 0, 0,
1249 texImage->Width, texImage->Height, texImage->Depth,
1250 texImage->TexFormat,
1251 imageSize, data);
1252 }
1253
1254
1255 /**
1256 * Compute compressed_pixelstore parameters for copying compressed
1257 * texture data.
1258 * \param dims number of texture image dimensions: 1, 2 or 3
1259 * \param texFormat the compressed texture format
1260 * \param width, height, depth size of image to copy
1261 * \param packing pixelstore parameters describing user-space image packing
1262 * \param store returns the compressed_pixelstore parameters
1263 */
1264 void
1265 _mesa_compute_compressed_pixelstore(GLuint dims, mesa_format texFormat,
1266 GLsizei width, GLsizei height,
1267 GLsizei depth,
1268 const struct gl_pixelstore_attrib *packing,
1269 struct compressed_pixelstore *store)
1270 {
1271 GLuint bw, bh, bd;
1272
1273 _mesa_get_format_block_size_3d(texFormat, &bw, &bh, &bd);
1274
1275 store->SkipBytes = 0;
1276 store->TotalBytesPerRow = store->CopyBytesPerRow =
1277 _mesa_format_row_stride(texFormat, width);
1278 store->TotalRowsPerSlice = store->CopyRowsPerSlice =
1279 (height + bh - 1) / bh;
1280 store->CopySlices = (depth + bd - 1) / bd;
1281
1282 if (packing->CompressedBlockWidth &&
1283 packing->CompressedBlockSize) {
1284
1285 bw = packing->CompressedBlockWidth;
1286
1287 if (packing->RowLength) {
1288 store->TotalBytesPerRow = packing->CompressedBlockSize *
1289 ((packing->RowLength + bw - 1) / bw);
1290 }
1291
1292 store->SkipBytes += packing->SkipPixels * packing->CompressedBlockSize / bw;
1293 }
1294
1295 if (dims > 1 && packing->CompressedBlockHeight &&
1296 packing->CompressedBlockSize) {
1297
1298 bh = packing->CompressedBlockHeight;
1299
1300 store->SkipBytes += packing->SkipRows * store->TotalBytesPerRow / bh;
1301 store->CopyRowsPerSlice = (height + bh - 1) / bh; /* rows in blocks */
1302
1303 if (packing->ImageHeight) {
1304 store->TotalRowsPerSlice = (packing->ImageHeight + bh - 1) / bh;
1305 }
1306 }
1307
1308 if (dims > 2 && packing->CompressedBlockDepth &&
1309 packing->CompressedBlockSize) {
1310
1311 int bd = packing->CompressedBlockDepth;
1312
1313 store->SkipBytes += packing->SkipImages * store->TotalBytesPerRow *
1314 store->TotalRowsPerSlice / bd;
1315 }
1316 }
1317
1318
1319 /**
1320 * Fallback for Driver.CompressedTexSubImage()
1321 */
1322 void
1323 _mesa_store_compressed_texsubimage(struct gl_context *ctx, GLuint dims,
1324 struct gl_texture_image *texImage,
1325 GLint xoffset, GLint yoffset, GLint zoffset,
1326 GLsizei width, GLsizei height, GLsizei depth,
1327 GLenum format,
1328 GLsizei imageSize, const GLvoid *data)
1329 {
1330 struct compressed_pixelstore store;
1331 GLint dstRowStride;
1332 GLint i, slice;
1333 GLubyte *dstMap;
1334 const GLubyte *src;
1335
1336 if (dims == 1) {
1337 _mesa_problem(ctx, "Unexpected 1D compressed texsubimage call");
1338 return;
1339 }
1340
1341 _mesa_compute_compressed_pixelstore(dims, texImage->TexFormat,
1342 width, height, depth,
1343 &ctx->Unpack, &store);
1344
1345 /* get pointer to src pixels (may be in a pbo which we'll map here) */
1346 data = _mesa_validate_pbo_compressed_teximage(ctx, dims, imageSize, data,
1347 &ctx->Unpack,
1348 "glCompressedTexSubImage");
1349 if (!data)
1350 return;
1351
1352 src = (const GLubyte *) data + store.SkipBytes;
1353
1354 for (slice = 0; slice < store.CopySlices; slice++) {
1355 /* Map dest texture buffer */
1356 ctx->Driver.MapTextureImage(ctx, texImage, slice + zoffset,
1357 xoffset, yoffset, width, height,
1358 GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_RANGE_BIT,
1359 &dstMap, &dstRowStride);
1360
1361 if (dstMap) {
1362
1363 /* copy rows of blocks */
1364 if (dstRowStride == store.TotalBytesPerRow &&
1365 dstRowStride == store.CopyBytesPerRow) {
1366 memcpy(dstMap, src, store.CopyBytesPerRow * store.CopyRowsPerSlice);
1367 src += store.CopyBytesPerRow * store.CopyRowsPerSlice;
1368 }
1369 else {
1370 for (i = 0; i < store.CopyRowsPerSlice; i++) {
1371 memcpy(dstMap, src, store.CopyBytesPerRow);
1372 dstMap += dstRowStride;
1373 src += store.TotalBytesPerRow;
1374 }
1375 }
1376
1377 ctx->Driver.UnmapTextureImage(ctx, texImage, slice + zoffset);
1378
1379 /* advance to next slice */
1380 src += store.TotalBytesPerRow * (store.TotalRowsPerSlice - store.CopyRowsPerSlice);
1381 }
1382 else {
1383 _mesa_error(ctx, GL_OUT_OF_MEMORY, "glCompressedTexSubImage%uD",
1384 dims);
1385 }
1386 }
1387
1388 _mesa_unmap_teximage_pbo(ctx, &ctx->Unpack);
1389 }