<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Dec 9, 2014 at 4:06 AM, Iago Toral Quiroga <span dir="ltr"><<a href="mailto:itoral@igalia.com" target="_blank">itoral@igalia.com</a>></span> wrote:<br><div><br></div><div>[snip]<br></div><div> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
new file mode 100644<br>
index 0000000..5f6809e<br>
--- /dev/null<br>
+++ b/src/mesa/main/format_pack.py<br>
@@ -0,0 +1,907 @@<br>
+#!/usr/bin/env python<br>
+<br>
+from mako.template import Template<br>
+from sys import argv<br>
+<br>
+string = """/*<br>
+ * Mesa 3-D graphics library<br>
+ *<br>
+ * Copyright (c) 2011 VMware, Inc.<br>
+ * Copyright (c) 2014 Intel Corporation.<br>
+ *<br>
+ * Permission is hereby granted, free of charge, to any person obtaining a<br>
+ * copy of this software and associated documentation files (the "Software"),<br>
+ * to deal in the Software without restriction, including without limitation<br>
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,<br>
+ * and/or sell copies of the Software, and to permit persons to whom the<br>
+ * Software is furnished to do so, subject to the following conditions:<br>
+ *<br>
+ * The above copyright notice and this permission notice shall be included<br>
+ * in all copies or substantial portions of the Software.<br>
+ *<br>
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS<br>
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,<br>
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL<br>
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR<br>
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,<br>
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR<br>
+ * OTHER DEALINGS IN THE SOFTWARE.<br>
+ */<br>
+<br>
+<br>
+/**<br>
+ * Color, depth, stencil packing functions.<br>
+ * Used to pack basic color, depth and stencil formats to specific<br>
+ * hardware formats.<br>
+ *<br>
+ * There are both per-pixel and per-row packing functions:<br>
+ * - The former will be used by swrast to write values to the color, depth,<br>
+ * stencil buffers when drawing points, lines and masked spans.<br>
+ * - The later will be used for image-oriented functions like glDrawPixels,<br>
+ * glAccum, and glTexImage.<br>
+ */<br>
+<br>
+#include <stdint.h><br>
+<br>
+#include "colormac.h"<br>
+#include "format_pack.h"<br>
+#include "format_utils.h"<br>
+#include "macros.h"<br>
+#include "../../gallium/auxiliary/util/u_format_rgb9e5.h"<br>
+#include "../../gallium/auxiliary/util/u_format_r11g11b10f.h"<br>
+#include "util/format_srgb.h"<br>
+<br>
+#define UNPACK(SRC, OFFSET, BITS) (((SRC) >> (OFFSET)) & MAX_UINT(BITS))<br>
+#define PACK(SRC, OFFSET, BITS) (((SRC) & MAX_UINT(BITS)) << (OFFSET))<br>
+<br>
+<%<br>
+import format_parser as parser<br>
+<br>
+formats = parser.parse(argv[1])<br>
+<br>
+rgb_formats = []<br>
+for f in formats:<br>
+ if <a href="http://f.name" target="_blank">f.name</a> == 'MESA_FORMAT_NONE':<br>
+ continue<br>
+ if f.colorspace not in ('rgb', 'srgb'):<br>
+ continue<br>
+<br>
+ rgb_formats.append(f)<br>
+%><br>
+<br>
+/* ubyte packing functions */<br>
+<br>
+%for f in rgb_formats:<br>
+ %if <a href="http://f.name" target="_blank">f.name</a> in ('MESA_FORMAT_R9G9B9E5_FLOAT', 'MESA_FORMAT_R11G11B10_FLOAT'):<br>
+ <% continue %><br>
+ %elif f.is_compressed():<br>
+ <% continue %><br>
+ %endif<br>
+<br>
+static inline void<br>
+pack_ubyte_${f.short_name()}(const GLubyte src[4], void *dst)<br>
+{<br>
+ %for (i, c) in enumerate(f.channels):<br>
+ <% i = f.swizzle.inverse()[i] %><br>
+ %if c.type == 'x':<br>
+ <% continue %><br>
+ %endif<br>
+<br>
+ ${c.datatype()} ${<a href="http://c.name" target="_blank">c.name</a>} =<br>
+ %if c.type == parser.UNSIGNED:<br>
+ %if f.colorspace == 'srgb' and <a href="http://c.name" target="_blank">c.name</a> in 'rgb':<br>
+ util_format_linear_to_srgb_8unorm(src[${i}]);<br>
+ %else:<br>
+ _mesa_unorm_to_unorm(src[${i}], 8, ${c.size});<br>
+ %endif<br>
+ %elif c.type == parser.SIGNED:<br>
+ _mesa_unorm_to_snorm(src[${i}], 8, ${c.size});<br>
+ %elif c.type == parser.FLOAT:<br>
+ %if c.size == 32:<br>
+ _mesa_unorm_to_float(src[${i}], 8);<br>
+ %elif c.size == 16:<br>
+ _mesa_unorm_to_half(src[${i}], 8);<br>
+ %else:<br>
+ <% assert False %><br>
+ %endif<br>
+ %else:<br>
+ <% assert False %><br>
+ %endif<br>
+ %endfor<br>
+<br>
+ %if f.layout == parser.ARRAY:<br>
+ ${f.datatype()} *d = (${f.datatype()} *)dst;<br>
+ %for (i, c) in enumerate(f.channels):<br>
+ %if c.type == 'x':<br>
+ <% continue %><br>
+ %endif<br>
+ d[${i}] = ${<a href="http://c.name" target="_blank">c.name</a>};<br>
+ %endfor<br>
+ %elif f.layout == parser.PACKED:<br>
+ ${f.datatype()} d = 0;<br>
+ %for (i, c) in enumerate(f.channels):<br>
+ %if c.type == 'x':<br>
+ <% continue %><br>
+ %endif<br>
+ d |= PACK(${<a href="http://c.name" target="_blank">c.name</a>}, ${c.shift}, ${c.size});<br>
+ %endfor<br>
+ (*(${f.datatype()} *)dst) = d;<br>
+ %else:<br>
+ <% assert False %><br>
+ %endif<br>
+}<br>
+%endfor<br>
+<br>
+static inline void<br>
+pack_ubyte_r9g9b9e5_float(const GLubyte src[4], void *dst)<br>
+{<br>
+ GLuint *d = (GLuint *) dst;<br>
+ GLfloat rgb[3];<br>
+ rgb[0] = _mesa_unorm_to_float(src[RCOMP], 8);<br>
+ rgb[1] = _mesa_unorm_to_float(src[GCOMP], 8);<br>
+ rgb[2] = _mesa_unorm_to_float(src[BCOMP], 8);<br>
+ *d = float3_to_rgb9e5(rgb);<br>
+}<br>
+<br>
+static inline void<br>
+pack_ubyte_r11g11b10_float(const GLubyte src[4], void *dst)<br>
+{<br>
+ GLuint *d = (GLuint *) dst;<br>
+ GLfloat rgb[3];<br>
+ rgb[0] = _mesa_unorm_to_float(src[RCOMP], 8);<br>
+ rgb[1] = _mesa_unorm_to_float(src[GCOMP], 8);<br>
+ rgb[2] = _mesa_unorm_to_float(src[BCOMP], 8);<br>
+ *d = float3_to_r11g11b10f(rgb);<br>
+}<br>
+<br>
+<br>
+/* float packing functions */<br>
+<br>
+%for f in rgb_formats:<br>
+ %if <a href="http://f.name" target="_blank">f.name</a> in ('MESA_FORMAT_R9G9B9E5_FLOAT', 'MESA_FORMAT_R11G11B10_FLOAT'):<br>
+ <% continue %><br>
+ %elif f.is_compressed():<br>
+ <% continue %><br>
+ %endif<br>
+<br>
+static inline void<br>
+pack_float_${f.short_name()}(const GLfloat src[4], void *dst)<br>
+{<br>
+ %for (i, c) in enumerate(f.channels):<br>
+ <% i = f.swizzle.inverse()[i] %><br>
+ %if c.type == 'x':<br>
+ <% continue %><br>
+ %endif<br>
+<br>
+ ${c.datatype()} ${<a href="http://c.name" target="_blank">c.name</a>} =<br>
+ %if c.type == parser.UNSIGNED:<br>
+ %if f.colorspace == 'srgb' and <a href="http://c.name" target="_blank">c.name</a> in 'rgb':<br>
+ <% assert c.size == 8 %><br>
+ util_format_linear_float_to_srgb_8unorm(src[${i}]);<br>
+ %else:<br>
+ _mesa_float_to_unorm(src[${i}], ${c.size});<br>
+ %endif<br>
+ %elif c.type == parser.SIGNED:<br>
+ _mesa_float_to_snorm(src[${i}], ${c.size});<br></blockquote><div><br>Do we need to handle non-normalized formats here? We should either handle them properly (including clamping, etc.) or not generate the functions for them at all. IIRC, I think we need them.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ %elif c.type == parser.FLOAT:<br>
+ %if c.size == 32:<br>
+ src[${i}];<br>
+ %elif c.size == 16:<br>
+ _mesa_float_to_half(src[${i}]);<br>
+ %else:<br>
+ <% assert False %><br>
+ %endif<br>
+ %else:<br>
+ <% assert False %><br>
+ %endif<br>
+ %endfor<br>
+<br>
+ %if f.layout == parser.ARRAY:<br>
+ ${f.datatype()} *d = (${f.datatype()} *)dst;<br>
+ %for (i, c) in enumerate(f.channels):<br>
+ %if c.type == 'x':<br>
+ <% continue %><br>
+ %endif<br>
+ d[${i}] = ${<a href="http://c.name" target="_blank">c.name</a>};<br>
+ %endfor<br>
+ %elif f.layout == parser.PACKED:<br>
+ ${f.datatype()} d = 0;<br>
+ %for (i, c) in enumerate(f.channels):<br>
+ %if c.type == 'x':<br>
+ <% continue %><br>
+ %endif<br>
+ d |= PACK(${<a href="http://c.name" target="_blank">c.name</a>}, ${c.shift}, ${c.size});<br>
+ %endfor<br>
+ (*(${f.datatype()} *)dst) = d;<br>
+ %else:<br>
+ <% assert False %><br>
+ %endif<br>
+}<br>
+%endfor<br>
+<br>
+static inline void<br>
+pack_float_r9g9b9e5_float(const GLfloat src[4], void *dst)<br>
+{<br>
+ GLuint *d = (GLuint *) dst;<br>
+ *d = float3_to_rgb9e5(src);<br>
+}<br>
+<br>
+static inline void<br>
+pack_float_r11g11b10_float(const GLfloat src[4], void *dst)<br>
+{<br>
+ GLuint *d = (GLuint *) dst;<br>
+ *d = float3_to_r11g11b10f(src);<br>
+}<br>
+<br>
+/**<br>
+ * Return a function that can pack a GLubyte rgba[4] color.<br>
+ */<br>
+gl_pack_ubyte_rgba_func<br>
+_mesa_get_pack_ubyte_rgba_function(mesa_format format)<br>
+{<br>
+ switch (format) {<br>
+%for f in rgb_formats:<br>
+ %if f.is_compressed():<br>
+ <% continue %><br>
+ %endif<br>
+<br>
+ case ${<a href="http://f.name" target="_blank">f.name</a>}:<br>
+ return pack_ubyte_${f.short_name()};<br>
+%endfor<br>
+ default:<br>
+ return NULL;<br>
+ }<br>
+}<br>
+<br>
+/**<br>
+ * Return a function that can pack a GLfloat rgba[4] color.<br>
+ */<br>
+gl_pack_float_rgba_func<br>
+_mesa_get_pack_float_rgba_function(mesa_format format)<br>
+{<br>
+ switch (format) {<br>
+%for f in rgb_formats:<br>
+ %if f.is_compressed():<br>
+ <% continue %><br>
+ %endif<br>
+<br>
+ case ${<a href="http://f.name" target="_blank">f.name</a>}:<br>
+ return pack_float_${f.short_name()};<br>
+%endfor<br>
+ default:<br>
+ return NULL;<br>
+ }<br>
+}<br>
+<br>
+/**<br>
+ * Pack a row of GLubyte rgba[4] values to the destination.<br>
+ */<br>
+void<br>
+_mesa_pack_ubyte_rgba_row(mesa_format format, GLuint n,<br>
+ const GLubyte src[][4], void *dst)<br>
+{<br>
+ GLuint i;<br>
+ GLubyte *d = dst;<br>
+<br>
+ switch (format) {<br>
+%for f in rgb_formats:<br>
+ %if f.is_compressed():<br>
+ <% continue %><br>
+ %endif<br>
+<br>
+ case ${<a href="http://f.name" target="_blank">f.name</a>}:<br>
+ for (i = 0; i < n; ++i) {<br>
+ pack_ubyte_${f.short_name()}(src[i], d);<br>
+ d += ${f.block_size() / 8};<br>
+ }<br>
+ break;<br>
+%endfor<br>
+ default:<br>
+ assert(!"Invalid format");<br>
+ }<br>
+}<br>
+<br>
+/**<br>
+ * Pack a row of GLfloat rgba[4] values to the destination.<br>
+ */<br>
+void<br>
+_mesa_pack_float_rgba_row(mesa_format format, GLuint n,<br>
+ const GLfloat src[][4], void *dst)<br>
+{<br>
+ GLuint i;<br>
+ GLubyte *d = dst;<br>
+<br>
+ switch (format) {<br>
+%for f in rgb_formats:<br>
+ %if f.is_compressed():<br>
+ <% continue %><br>
+ %endif<br>
+<br>
+ case ${<a href="http://f.name" target="_blank">f.name</a>}:<br>
+ for (i = 0; i < n; ++i) {<br>
+ pack_float_${f.short_name()}(src[i], d);<br>
+ d += ${f.block_size() / 8};<br>
+ }<br>
+ break;<br>
+%endfor<br>
+ default:<br>
+ assert(!"Invalid format");<br>
+ }<br>
+}<br>
+<br>
+/**<br>
+ * Pack a 2D image of ubyte RGBA pixels in the given format.<br>
+ * \param srcRowStride source image row stride in bytes<br>
+ * \param dstRowStride destination image row stride in bytes<br>
+ */<br>
+void<br>
+_mesa_pack_ubyte_rgba_rect(mesa_format format, GLuint width, GLuint height,<br>
+ const GLubyte *src, GLint srcRowStride,<br>
+ void *dst, GLint dstRowStride)<br>
+{<br>
+ GLubyte *dstUB = dst;<br>
+ GLuint i;<br>
+<br>
+ if (srcRowStride == width * 4 * sizeof(GLubyte) &&<br>
+ dstRowStride == _mesa_format_row_stride(format, width)) {<br>
+ /* do whole image at once */<br>
+ _mesa_pack_ubyte_rgba_row(format, width * height,<br>
+ (const GLubyte (*)[4]) src, dst);<br>
+ }<br>
+ else {<br>
+ /* row by row */<br>
+ for (i = 0; i < height; i++) {<br>
+ _mesa_pack_ubyte_rgba_row(format, width,<br>
+ (const GLubyte (*)[4]) src, dstUB);<br>
+ src += srcRowStride;<br>
+ dstUB += dstRowStride;<br>
+ }<br>
+ }<br>
+}<br>
+<br>
+<br>
+/** Helper struct for MESA_FORMAT_Z32_FLOAT_S8X24_UINT */<br>
+struct z32f_x24s8<br>
+{<br>
+ float z;<br>
+ uint32_t x24s8;<br>
+};<br>
+<br>
+<br>
+/**<br>
+ ** Pack float Z pixels<br>
+ **/<br>
+<br>
+static void<br>
+pack_float_S8_UINT_Z24_UNORM(const GLfloat *src, void *dst)<br>
+{<br>
+ /* don't disturb the stencil values */<br>
+ GLuint *d = ((GLuint *) dst);<br>
+ const GLdouble scale = (GLdouble) 0xffffff;<br>
+ GLuint s = *d & 0xff;<br>
+ GLuint z = (GLuint) (*src * scale);<br>
+ assert(z <= 0xffffff);<br>
+ *d = (z << 8) | s;<br>
+}<br>
+<br>
+static void<br>
+pack_float_Z24_UNORM_S8_UINT(const GLfloat *src, void *dst)<br>
+{<br>
+ /* don't disturb the stencil values */<br>
+ GLuint *d = ((GLuint *) dst);<br>
+ const GLdouble scale = (GLdouble) 0xffffff;<br>
+ GLuint s = *d & 0xff000000;<br>
+ GLuint z = (GLuint) (*src * scale);<br>
+ assert(z <= 0xffffff);<br>
+ *d = s | z;<br>
+}<br>
+<br>
+static void<br>
+pack_float_Z_UNORM16(const GLfloat *src, void *dst)<br>
+{<br>
+ GLushort *d = ((GLushort *) dst);<br>
+ const GLfloat scale = (GLfloat) 0xffff;<br>
+ *d = (GLushort) (*src * scale);<br>
+}<br>
+<br>
+static void<br>
+pack_float_Z_UNORM32(const GLfloat *src, void *dst)<br>
+{<br>
+ GLuint *d = ((GLuint *) dst);<br>
+ const GLdouble scale = (GLdouble) 0xffffffff;<br>
+ *d = (GLuint) (*src * scale);<br>
+}<br>
+<br>
+static void<br>
+pack_float_Z_FLOAT32(const GLfloat *src, void *dst)<br>
+{<br>
+ GLfloat *d = (GLfloat *) dst;<br>
+ *d = *src;<br>
+}<br>
+<br>
+gl_pack_float_z_func<br>
+_mesa_get_pack_float_z_func(mesa_format format)<br>
+{<br>
+ switch (format) {<br>
+ case MESA_FORMAT_S8_UINT_Z24_UNORM:<br>
+ case MESA_FORMAT_X8_UINT_Z24_UNORM:<br>
+ return pack_float_S8_UINT_Z24_UNORM;<br>
+ case MESA_FORMAT_Z24_UNORM_S8_UINT:<br>
+ case MESA_FORMAT_Z24_UNORM_X8_UINT:<br>
+ return pack_float_Z24_UNORM_S8_UINT;<br>
+ case MESA_FORMAT_Z_UNORM16:<br>
+ return pack_float_Z_UNORM16;<br>
+ case MESA_FORMAT_Z_UNORM32:<br>
+ return pack_float_Z_UNORM32;<br>
+ case MESA_FORMAT_Z_FLOAT32:<br>
+ case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:<br>
+ return pack_float_Z_FLOAT32;<br>
+ default:<br>
+ _mesa_problem(NULL,<br>
+ "unexpected format in _mesa_get_pack_float_z_func()");<br>
+ return NULL;<br>
+ }<br>
+}<br>
+<br>
+<br>
+<br>
+/**<br>
+ ** Pack uint Z pixels. The incoming src value is always in<br>
+ ** the range [0, 2^32-1].<br>
+ **/<br>
+<br>
+static void<br>
+pack_uint_S8_UINT_Z24_UNORM(const GLuint *src, void *dst)<br>
+{<br>
+ /* don't disturb the stencil values */<br>
+ GLuint *d = ((GLuint *) dst);<br>
+ GLuint s = *d & 0xff;<br>
+ GLuint z = *src & 0xffffff00;<br>
+ *d = z | s;<br>
+}<br>
+<br>
+static void<br>
+pack_uint_Z24_UNORM_S8_UINT(const GLuint *src, void *dst)<br>
+{<br>
+ /* don't disturb the stencil values */<br>
+ GLuint *d = ((GLuint *) dst);<br>
+ GLuint s = *d & 0xff000000;<br>
+ GLuint z = *src >> 8;<br>
+ *d = s | z;<br>
+}<br>
+<br>
+static void<br>
+pack_uint_Z_UNORM16(const GLuint *src, void *dst)<br>
+{<br>
+ GLushort *d = ((GLushort *) dst);<br>
+ *d = *src >> 16;<br>
+}<br>
+<br>
+static void<br>
+pack_uint_Z_UNORM32(const GLuint *src, void *dst)<br>
+{<br>
+ GLuint *d = ((GLuint *) dst);<br>
+ *d = *src;<br>
+}<br>
+<br>
+static void<br>
+pack_uint_Z_FLOAT32(const GLuint *src, void *dst)<br>
+{<br>
+ GLuint *d = ((GLuint *) dst);<br>
+ const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;<br>
+ *d = (GLuint) (*src * scale);<br>
+ assert(*d >= 0.0f);<br>
+ assert(*d <= 1.0f);<br>
+}<br>
+<br>
+static void<br>
+pack_uint_Z_FLOAT32_X24S8(const GLuint *src, void *dst)<br>
+{<br>
+ GLfloat *d = ((GLfloat *) dst);<br>
+ const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;<br>
+ *d = (GLfloat) (*src * scale);<br>
+ assert(*d >= 0.0f);<br>
+ assert(*d <= 1.0f);<br>
+}<br>
+<br>
+gl_pack_uint_z_func<br>
+_mesa_get_pack_uint_z_func(mesa_format format)<br>
+{<br>
+ switch (format) {<br>
+ case MESA_FORMAT_S8_UINT_Z24_UNORM:<br>
+ case MESA_FORMAT_X8_UINT_Z24_UNORM:<br>
+ return pack_uint_S8_UINT_Z24_UNORM;<br>
+ case MESA_FORMAT_Z24_UNORM_S8_UINT:<br>
+ case MESA_FORMAT_Z24_UNORM_X8_UINT:<br>
+ return pack_uint_Z24_UNORM_S8_UINT;<br>
+ case MESA_FORMAT_Z_UNORM16:<br>
+ return pack_uint_Z_UNORM16;<br>
+ case MESA_FORMAT_Z_UNORM32:<br>
+ return pack_uint_Z_UNORM32;<br>
+ case MESA_FORMAT_Z_FLOAT32:<br>
+ return pack_uint_Z_FLOAT32;<br>
+ case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:<br>
+ return pack_uint_Z_FLOAT32_X24S8;<br>
+ default:<br>
+ _mesa_problem(NULL, "unexpected format in _mesa_get_pack_uint_z_func()");<br>
+ return NULL;<br>
+ }<br>
+}<br>
+<br>
+<br>
+/**<br>
+ ** Pack ubyte stencil pixels<br>
+ **/<br>
+<br>
+static void<br>
+pack_ubyte_stencil_Z24_S8(const GLubyte *src, void *dst)<br>
+{<br>
+ /* don't disturb the Z values */<br>
+ GLuint *d = ((GLuint *) dst);<br>
+ GLuint s = *src;<br>
+ GLuint z = *d & 0xffffff00;<br>
+ *d = z | s;<br>
+}<br>
+<br>
+static void<br>
+pack_ubyte_stencil_S8_Z24(const GLubyte *src, void *dst)<br>
+{<br>
+ /* don't disturb the Z values */<br>
+ GLuint *d = ((GLuint *) dst);<br>
+ GLuint s = *src << 24;<br>
+ GLuint z = *d & 0xffffff;<br>
+ *d = s | z;<br>
+}<br>
+<br>
+static void<br>
+pack_ubyte_stencil_S8(const GLubyte *src, void *dst)<br>
+{<br>
+ GLubyte *d = (GLubyte *) dst;<br>
+ *d = *src;<br>
+}<br>
+<br>
+static void<br>
+pack_ubyte_stencil_Z32_FLOAT_X24S8(const GLubyte *src, void *dst)<br>
+{<br>
+ GLfloat *d = ((GLfloat *) dst);<br>
+ d[1] = *src;<br>
+}<br>
+<br>
+<br>
+gl_pack_ubyte_stencil_func<br>
+_mesa_get_pack_ubyte_stencil_func(mesa_format format)<br>
+{<br>
+ switch (format) {<br>
+ case MESA_FORMAT_S8_UINT_Z24_UNORM:<br>
+ return pack_ubyte_stencil_Z24_S8;<br>
+ case MESA_FORMAT_Z24_UNORM_S8_UINT:<br>
+ return pack_ubyte_stencil_S8_Z24;<br>
+ case MESA_FORMAT_S_UINT8:<br>
+ return pack_ubyte_stencil_S8;<br>
+ case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:<br>
+ return pack_ubyte_stencil_Z32_FLOAT_X24S8;<br>
+ default:<br>
+ _mesa_problem(NULL,<br>
+ "unexpected format in _mesa_pack_ubyte_stencil_func()");<br>
+ return NULL;<br>
+ }<br>
+}<br>
+<br>
+<br>
+<br>
+void<br>
+_mesa_pack_float_z_row(mesa_format format, GLuint n,<br>
+ const GLfloat *src, void *dst)<br>
+{<br>
+ switch (format) {<br>
+ case MESA_FORMAT_S8_UINT_Z24_UNORM:<br>
+ case MESA_FORMAT_X8_UINT_Z24_UNORM:<br>
+ {<br>
+ /* don't disturb the stencil values */<br>
+ GLuint *d = ((GLuint *) dst);<br>
+ const GLdouble scale = (GLdouble) 0xffffff;<br>
+ GLuint i;<br>
+ for (i = 0; i < n; i++) {<br>
+ GLuint s = d[i] & 0xff;<br>
+ GLuint z = (GLuint) (src[i] * scale);<br>
+ assert(z <= 0xffffff);<br>
+ d[i] = (z << 8) | s;<br>
+ }<br>
+ }<br>
+ break;<br>
+ case MESA_FORMAT_Z24_UNORM_S8_UINT:<br>
+ case MESA_FORMAT_Z24_UNORM_X8_UINT:<br>
+ {<br>
+ /* don't disturb the stencil values */<br>
+ GLuint *d = ((GLuint *) dst);<br>
+ const GLdouble scale = (GLdouble) 0xffffff;<br>
+ GLuint i;<br>
+ for (i = 0; i < n; i++) {<br>
+ GLuint s = d[i] & 0xff000000;<br>
+ GLuint z = (GLuint) (src[i] * scale);<br>
+ assert(z <= 0xffffff);<br>
+ d[i] = s | z;<br>
+ }<br>
+ }<br>
+ break;<br>
+ case MESA_FORMAT_Z_UNORM16:<br>
+ {<br>
+ GLushort *d = ((GLushort *) dst);<br>
+ const GLfloat scale = (GLfloat) 0xffff;<br>
+ GLuint i;<br>
+ for (i = 0; i < n; i++) {<br>
+ d[i] = (GLushort) (src[i] * scale);<br>
+ }<br>
+ }<br>
+ break;<br>
+ case MESA_FORMAT_Z_UNORM32:<br>
+ {<br>
+ GLuint *d = ((GLuint *) dst);<br>
+ const GLdouble scale = (GLdouble) 0xffffffff;<br>
+ GLuint i;<br>
+ for (i = 0; i < n; i++) {<br>
+ d[i] = (GLuint) (src[i] * scale);<br>
+ }<br>
+ }<br>
+ break;<br>
+ case MESA_FORMAT_Z_FLOAT32:<br>
+ memcpy(dst, src, n * sizeof(GLfloat));<br>
+ break;<br>
+ case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:<br>
+ {<br>
+ struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;<br>
+ GLuint i;<br>
+ for (i = 0; i < n; i++) {<br>
+ d[i].z = src[i];<br>
+ }<br>
+ }<br>
+ break;<br>
+ default:<br>
+ _mesa_problem(NULL, "unexpected format in _mesa_pack_float_z_row()");<br>
+ }<br>
+}<br>
+<br>
+<br>
+/**<br>
+ * The incoming Z values are always in the range [0, 0xffffffff].<br>
+ */<br>
+void<br>
+_mesa_pack_uint_z_row(mesa_format format, GLuint n,<br>
+ const GLuint *src, void *dst)<br>
+{<br>
+ switch (format) {<br>
+ case MESA_FORMAT_S8_UINT_Z24_UNORM:<br>
+ case MESA_FORMAT_X8_UINT_Z24_UNORM:<br>
+ {<br>
+ /* don't disturb the stencil values */<br>
+ GLuint *d = ((GLuint *) dst);<br>
+ GLuint i;<br>
+ for (i = 0; i < n; i++) {<br>
+ GLuint s = d[i] & 0xff;<br>
+ GLuint z = src[i] & 0xffffff00;<br>
+ d[i] = z | s;<br>
+ }<br>
+ }<br>
+ break;<br>
+ case MESA_FORMAT_Z24_UNORM_S8_UINT:<br>
+ case MESA_FORMAT_Z24_UNORM_X8_UINT:<br>
+ {<br>
+ /* don't disturb the stencil values */<br>
+ GLuint *d = ((GLuint *) dst);<br>
+ GLuint i;<br>
+ for (i = 0; i < n; i++) {<br>
+ GLuint s = d[i] & 0xff000000;<br>
+ GLuint z = src[i] >> 8;<br>
+ d[i] = s | z;<br>
+ }<br>
+ }<br>
+ break;<br>
+ case MESA_FORMAT_Z_UNORM16:<br>
+ {<br>
+ GLushort *d = ((GLushort *) dst);<br>
+ GLuint i;<br>
+ for (i = 0; i < n; i++) {<br>
+ d[i] = src[i] >> 16;<br>
+ }<br>
+ }<br>
+ break;<br>
+ case MESA_FORMAT_Z_UNORM32:<br>
+ memcpy(dst, src, n * sizeof(GLfloat));<br>
+ break;<br>
+ case MESA_FORMAT_Z_FLOAT32:<br>
+ {<br>
+ GLuint *d = ((GLuint *) dst);<br>
+ const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;<br>
+ GLuint i;<br>
+ for (i = 0; i < n; i++) {<br>
+ d[i] = (GLuint) (src[i] * scale);<br>
+ assert(d[i] >= 0.0f);<br>
+ assert(d[i] <= 1.0f);<br>
+ }<br>
+ }<br>
+ break;<br>
+ case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:<br>
+ {<br>
+ struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;<br>
+ const GLdouble scale = 1.0 / (GLdouble) 0xffffffff;<br>
+ GLuint i;<br>
+ for (i = 0; i < n; i++) {<br>
+ d[i].z = (GLfloat) (src[i] * scale);<br>
+ assert(d[i].z >= 0.0f);<br>
+ assert(d[i].z <= 1.0f);<br>
+ }<br>
+ }<br>
+ break;<br>
+ default:<br>
+ _mesa_problem(NULL, "unexpected format in _mesa_pack_uint_z_row()");<br>
+ }<br>
+}<br>
+<br>
+<br>
+void<br>
+_mesa_pack_ubyte_stencil_row(mesa_format format, GLuint n,<br>
+ const GLubyte *src, void *dst)<br>
+{<br>
+ switch (format) {<br>
+ case MESA_FORMAT_S8_UINT_Z24_UNORM:<br>
+ {<br>
+ /* don't disturb the Z values */<br>
+ GLuint *d = ((GLuint *) dst);<br>
+ GLuint i;<br>
+ for (i = 0; i < n; i++) {<br>
+ GLuint s = src[i];<br>
+ GLuint z = d[i] & 0xffffff00;<br>
+ d[i] = z | s;<br>
+ }<br>
+ }<br>
+ break;<br>
+ case MESA_FORMAT_Z24_UNORM_S8_UINT:<br>
+ {<br>
+ /* don't disturb the Z values */<br>
+ GLuint *d = ((GLuint *) dst);<br>
+ GLuint i;<br>
+ for (i = 0; i < n; i++) {<br>
+ GLuint s = src[i] << 24;<br>
+ GLuint z = d[i] & 0xffffff;<br>
+ d[i] = s | z;<br>
+ }<br>
+ }<br>
+ break;<br>
+ case MESA_FORMAT_S_UINT8:<br>
+ memcpy(dst, src, n * sizeof(GLubyte));<br>
+ break;<br>
+ case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:<br>
+ {<br>
+ struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;<br>
+ GLuint i;<br>
+ for (i = 0; i < n; i++) {<br>
+ d[i].x24s8 = src[i];<br>
+ }<br>
+ }<br>
+ break;<br>
+ default:<br>
+ _mesa_problem(NULL, "unexpected format in _mesa_pack_ubyte_stencil_row()");<br>
+ }<br>
+}<br>
+<br>
+<br>
+/**<br>
+ * Incoming Z/stencil values are always in uint_24_8 format.<br>
+ */<br>
+void<br>
+_mesa_pack_uint_24_8_depth_stencil_row(mesa_format format, GLuint n,<br>
+ const GLuint *src, void *dst)<br>
+{<br>
+ switch (format) {<br>
+ case MESA_FORMAT_S8_UINT_Z24_UNORM:<br>
+ memcpy(dst, src, n * sizeof(GLuint));<br>
+ break;<br>
+ case MESA_FORMAT_Z24_UNORM_S8_UINT:<br>
+ {<br>
+ GLuint *d = ((GLuint *) dst);<br>
+ GLuint i;<br>
+ for (i = 0; i < n; i++) {<br>
+ GLuint s = src[i] << 24;<br>
+ GLuint z = src[i] >> 8;<br>
+ d[i] = s | z;<br>
+ }<br>
+ }<br>
+ break;<br>
+ case MESA_FORMAT_Z32_FLOAT_S8X24_UINT:<br>
+ {<br>
+ const GLdouble scale = 1.0 / (GLdouble) 0xffffff;<br>
+ struct z32f_x24s8 *d = (struct z32f_x24s8 *) dst;<br>
+ GLuint i;<br>
+ for (i = 0; i < n; i++) {<br>
+ GLfloat z = (GLfloat) ((src[i] >> 8) * scale);<br>
+ d[i].z = z;<br>
+ d[i].x24s8 = src[i];<br>
+ }<br>
+ }<br>
+ break;<br>
+ default:<br>
+ _mesa_problem(NULL, "bad format %s in _mesa_pack_ubyte_s_row",<br>
+ _mesa_get_format_name(format));<br>
+ return;<br>
+ }<br>
+}<br>
+<br>
+<br>
+<br>
+/**<br>
+ * Convert a boolean color mask to a packed color where each channel of<br>
+ * the packed value at dst will be 0 or ~0 depending on the colorMask.<br>
+ */<br>
+void<br>
+_mesa_pack_colormask(mesa_format format, const GLubyte colorMask[4], void *dst)<br>
+{<br>
+ GLfloat maskColor[4];<br>
+<br>
+ switch (_mesa_get_format_datatype(format)) {<br>
+ case GL_UNSIGNED_NORMALIZED:<br>
+ /* simple: 1.0 will convert to ~0 in the right bit positions */<br>
+ maskColor[0] = colorMask[0] ? 1.0f : 0.0f;<br>
+ maskColor[1] = colorMask[1] ? 1.0f : 0.0f;<br>
+ maskColor[2] = colorMask[2] ? 1.0f : 0.0f;<br>
+ maskColor[3] = colorMask[3] ? 1.0f : 0.0f;<br>
+ _mesa_pack_float_rgba_row(format, 1,<br>
+ (const GLfloat (*)[4]) maskColor, dst);<br>
+ break;<br>
+ case GL_SIGNED_NORMALIZED:<br>
+ case GL_FLOAT:<br>
+ /* These formats are harder because it's hard to know the floating<br>
+ * point values that will convert to ~0 for each color channel's bits.<br>
+ * This solution just generates a non-zero value for each color channel<br>
+ * then fixes up the non-zero values to be ~0.<br>
+ * Note: we'll need to add special case code if we ever have to deal<br>
+ * with formats with unequal color channel sizes, like R11_G11_B10.<br>
+ * We issue a warning below for channel sizes other than 8,16,32.<br>
+ */<br>
+ {<br>
+ GLuint bits = _mesa_get_format_max_bits(format); /* bits per chan */<br>
+ GLuint bytes = _mesa_get_format_bytes(format);<br>
+ GLuint i;<br>
+<br>
+ /* this should put non-zero values into the channels of dst */<br>
+ maskColor[0] = colorMask[0] ? -1.0f : 0.0f;<br>
+ maskColor[1] = colorMask[1] ? -1.0f : 0.0f;<br>
+ maskColor[2] = colorMask[2] ? -1.0f : 0.0f;<br>
+ maskColor[3] = colorMask[3] ? -1.0f : 0.0f;<br>
+ _mesa_pack_float_rgba_row(format, 1,<br>
+ (const GLfloat (*)[4]) maskColor, dst);<br>
+<br>
+ /* fix-up the dst channels by converting non-zero values to ~0 */<br>
+ if (bits == 8) {<br>
+ GLubyte *d = (GLubyte *) dst;<br>
+ for (i = 0; i < bytes; i++) {<br>
+ d[i] = d[i] ? 0xff : 0x0;<br>
+ }<br>
+ }<br>
+ else if (bits == 16) {<br>
+ GLushort *d = (GLushort *) dst;<br>
+ for (i = 0; i < bytes / 2; i++) {<br>
+ d[i] = d[i] ? 0xffff : 0x0;<br>
+ }<br>
+ }<br>
+ else if (bits == 32) {<br>
+ GLuint *d = (GLuint *) dst;<br>
+ for (i = 0; i < bytes / 4; i++) {<br>
+ d[i] = d[i] ? 0xffffffffU : 0x0;<br>
+ }<br>
+ }<br>
+ else {<br>
+ _mesa_problem(NULL, "unexpected size in _mesa_pack_colormask()");<br>
+ return;<br>
+ }<br>
+ }<br>
+ break;<br>
+ default:<br>
+ _mesa_problem(NULL, "unexpected format data type in gen_color_mask()");<br>
+ return;<br>
+ }<br>
+}<br>
+"""<br>
+<br>
+template = Template(string);<br>
+<br>
+print template.render(argv = argv[0:])<br>
\ No newline at end of file<br>
diff --git a/src/mesa/main/format_parser.py b/src/mesa/main/format_parser.py<br>
index 5e45c74..522bce3 100755<br>
--- a/src/mesa/main/format_parser.py<br>
+++ b/src/mesa/main/format_parser.py<br>
@@ -102,6 +102,10 @@ class Channel:<br>
"""Returns true if the size of this channel is a power of two."""<br>
return is_power_of_two(self.size)<br>
<br>
+ def datatype(self):<br>
+ """Returns the datatype corresponding to a channel type and size"""<br>
+ return _get_datatype(self.type, self.size)<br>
+<br>
class Swizzle:<br>
"""Describes a swizzle operation.<br>
<br>
@@ -469,6 +473,49 @@ class Format:<br>
return channel<br>
return None<br>
<br>
+ def datatype(self):<br>
+ """Returns the datatype corresponding to a format's channel type and size"""<br>
+ if self.layout == PACKED:<br>
+ if self.block_size() == 8:<br>
+ return 'uint8_t'<br>
+ if self.block_size() == 16:<br>
+ return 'uint16_t'<br>
+ if self.block_size() == 32:<br>
+ return 'uint32_t'<br>
+ else:<br>
+ assert False<br>
+ else:<br>
+ return _get_datatype(self.channel_type(), self.channel_size())<br>
+<br>
+def _get_datatype(type, size):<br>
+ if type == FLOAT:<br>
+ if size == 32:<br>
+ return 'float'<br>
+ elif size == 16:<br>
+ return 'uint16_t'<br>
+ else:<br>
+ assert False<br>
+ elif type == UNSIGNED:<br>
+ if size <= 8:<br>
+ return 'uint8_t'<br>
+ elif size <= 16:<br>
+ return 'uint16_t'<br>
+ elif size <= 32:<br>
+ return 'uint32_t'<br>
+ else:<br>
+ assert False<br>
+ elif type == SIGNED:<br>
+ if size <= 8:<br>
+ return 'int8_t'<br>
+ elif size <= 16:<br>
+ return 'int16_t'<br>
+ elif size <= 32:<br>
+ return 'int32_t'<br>
+ else:<br>
+ assert False<br>
+ else:<br>
+ assert False<br>
+<br>
def _parse_channels(fields, layout, colorspace, swizzle):<br>
channels = []<br>
for field in fields:<br>
<span><font color="#888888">--<br>
1.9.1<br>
<br>
_______________________________________________<br>
mesa-dev mailing list<br>
<a href="mailto:mesa-dev@lists.freedesktop.org" target="_blank">mesa-dev@lists.freedesktop.org</a><br>
<a href="http://lists.freedesktop.org/mailman/listinfo/mesa-dev" target="_blank">http://lists.freedesktop.org/mailman/listinfo/mesa-dev</a><br>
</font></span></blockquote></div><br></div></div>