1 /**************************************************************************
2  *
3  * Copyright 2002 VMware, Inc.
4  * Copyright 2011 Dave Airlie (ARB_vertex_type_2_10_10_10_rev support)
5  * Copyright 2020 Advanced Micro Devices, Inc.
6  * All Rights Reserved.
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sub license, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the
17  * next paragraph) shall be included in all copies or substantial portions
18  * of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
24  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  *
28  **************************************************************************/
29 
30 #ifndef VBO_UTIL_H
31 #define VBO_UTIL_H
32 
33 #include "main/mtypes.h"
34 
conv_ui10_to_norm_float(unsigned ui10)35 static inline float conv_ui10_to_norm_float(unsigned ui10)
36 {
37    return ui10 / 1023.0f;
38 }
39 
conv_ui2_to_norm_float(unsigned ui2)40 static inline float conv_ui2_to_norm_float(unsigned ui2)
41 {
42    return ui2 / 3.0f;
43 }
44 
45 struct attr_bits_10 {signed int x:10;};
46 struct attr_bits_2 {signed int x:2;};
47 
conv_i10_to_i(int i10)48 static inline float conv_i10_to_i(int i10)
49 {
50    struct attr_bits_10 val;
51    val.x = i10;
52    return (float)val.x;
53 }
54 
conv_i2_to_i(int i2)55 static inline float conv_i2_to_i(int i2)
56 {
57    struct attr_bits_2 val;
58    val.x = i2;
59    return (float)val.x;
60 }
61 
conv_i10_to_norm_float(const struct gl_context * ctx,int i10)62 static inline float conv_i10_to_norm_float(const struct gl_context *ctx, int i10)
63 {
64    struct attr_bits_10 val;
65    val.x = i10;
66 
67    /* Traditionally, OpenGL has had two equations for converting from
68     * normalized fixed-point data to floating-point data.  In the OpenGL 3.2
69     * specification, these are equations 2.2 and 2.3, respectively:
70     *
71     *    f = (2c + 1)/(2^b - 1).                                (2.2)
72     *
73     * Comments below this equation state: "In general, this representation is
74     * used for signed normalized fixed-point parameters in GL commands, such
75     * as vertex attribute values."  Which is what we're doing here.
76     *
77     *    f = max{c/(2^(b-1) - 1), -1.0}                         (2.3)
78     *
79     * Comments below this equation state: "In general, this representation is
80     * used for signed normalized fixed-point texture or floating point values."
81     *
82     * OpenGL 4.2+ and ES 3.0 remedy this and state that equation 2.3 (above)
83     * is used in every case.  They remove equation 2.2 completely.
84     */
85    if (_mesa_is_gles3(ctx) ||
86        (_mesa_is_desktop_gl(ctx) && ctx->Version >= 42)) {
87       /* Equation 2.3 above. */
88       float f = ((float) val.x) / 511.0F;
89       return MAX2(f, -1.0f);
90    } else {
91       /* Equation 2.2 above. */
92       return (2.0F * (float)val.x + 1.0F) * (1.0F  / 1023.0F);
93    }
94 }
95 
conv_i2_to_norm_float(const struct gl_context * ctx,int i2)96 static inline float conv_i2_to_norm_float(const struct gl_context *ctx, int i2)
97 {
98    struct attr_bits_2 val;
99    val.x = i2;
100 
101    if (_mesa_is_gles3(ctx) ||
102        (_mesa_is_desktop_gl(ctx) && ctx->Version >= 42)) {
103       /* Equation 2.3 above. */
104       float f = (float) val.x;
105       return MAX2(f, -1.0f);
106    } else {
107       /* Equation 2.2 above. */
108       return (2.0F * (float)val.x + 1.0F) * (1.0F / 3.0F);
109    }
110 }
111 
112 #define ERROR_IF_NOT_PACKED_TYPE(ctx, type, func) \
113    if (type != GL_INT_2_10_10_10_REV && type != GL_UNSIGNED_INT_2_10_10_10_REV) { \
114       _mesa_error(ctx, GL_INVALID_ENUM, "%s(type)", func); \
115       return; \
116    }
117 
118 /* Extended version of ERROR_IF_NOT_PACKED_TYPE which also
119  * accepts GL_UNSIGNED_INT_10F_11F_11F_REV.
120  *
121  * Only used for VertexAttribP[123]ui[v]; VertexAttribP4* cannot use this type,
122  * and neither can legacy vertex attribs.
123  */
124 #define ERROR_IF_NOT_PACKED_TYPE_EXT(ctx, type, func) \
125    if (type != GL_INT_2_10_10_10_REV && type != GL_UNSIGNED_INT_2_10_10_10_REV && \
126        type != GL_UNSIGNED_INT_10F_11F_11F_REV) { \
127       _mesa_error(ctx, GL_INVALID_ENUM, "%s(type)", func); \
128       return; \
129    }
130 
131 #endif
132