glx-multithread-texture: Avoid front-buffer rendering.
[piglit.git] / tests / util / rgb9e5.c
blob66c4532260e7fe0a436c901b05942c2592c05906
1 /*
2 * Copyright (C) 2011 Marek Olšák <maraeo@gmail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
24 /* Copied from EXT_texture_shared_exponent */
26 #include "rgb9e5.h"
27 #include <math.h>
28 #include <assert.h>
30 #define MAX2( A, B ) ( (A)>(B) ? (A) : (B) )
32 /* XXX assuming little endian */
34 #define RGB9E5_EXPONENT_BITS 5
35 #define RGB9E5_MANTISSA_BITS 9
36 #define RGB9E5_EXP_BIAS 15
37 #define RGB9E5_MAX_VALID_BIASED_EXP 31
39 #define MAX_RGB9E5_EXP (RGB9E5_MAX_VALID_BIASED_EXP - RGB9E5_EXP_BIAS)
40 #define RGB9E5_MANTISSA_VALUES (1<<RGB9E5_MANTISSA_BITS)
41 #define MAX_RGB9E5_MANTISSA (RGB9E5_MANTISSA_VALUES-1)
42 #define MAX_RGB9E5 (((float)MAX_RGB9E5_MANTISSA)/RGB9E5_MANTISSA_VALUES * (1<<MAX_RGB9E5_EXP))
43 #define EPSILON_RGB9E5 ((1.0/RGB9E5_MANTISSA_VALUES) / (1<<RGB9E5_EXP_BIAS))
45 typedef struct {
46 #ifdef __BIG_ENDIAN__
47 unsigned int negative:1;
48 unsigned int biasedexponent:8;
49 unsigned int mantissa:23;
50 #else
51 unsigned int mantissa:23;
52 unsigned int biasedexponent:8;
53 unsigned int negative:1;
54 #endif
55 } BitsOfIEEE754;
57 typedef union {
58 unsigned int raw;
59 float value;
60 BitsOfIEEE754 field;
61 } float754;
63 typedef struct {
64 #ifdef __BIG_ENDIAN__
65 unsigned int biasedexponent:RGB9E5_EXPONENT_BITS;
66 unsigned int b:RGB9E5_MANTISSA_BITS;
67 unsigned int g:RGB9E5_MANTISSA_BITS;
68 unsigned int r:RGB9E5_MANTISSA_BITS;
69 #else
70 unsigned int r:RGB9E5_MANTISSA_BITS;
71 unsigned int g:RGB9E5_MANTISSA_BITS;
72 unsigned int b:RGB9E5_MANTISSA_BITS;
73 unsigned int biasedexponent:RGB9E5_EXPONENT_BITS;
74 #endif
75 } BitsOfRGB9E5;
77 typedef union {
78 unsigned int raw;
79 BitsOfRGB9E5 field;
80 } rgb9e5;
82 static float ClampRange_for_rgb9e5(float x)
84 if (x > 0.0) {
85 if (x >= MAX_RGB9E5) {
86 return MAX_RGB9E5;
87 } else {
88 return x;
90 } else {
91 /* NaN gets here too since comparisons with NaN always fail! */
92 return 0.0;
96 static float MaxOf3(float x, float y, float z)
98 if (x > y) {
99 return MAX2(x, z);
100 } else {
101 return MAX2(y, z);
105 /* Ok, FloorLog2 is not correct for the denorm and zero values, but we
106 are going to do a max of this value with the minimum rgb9e5 exponent
107 that will hide these problem cases. */
108 static int FloorLog2(float x)
110 float754 f;
112 f.value = x;
113 return (f.field.biasedexponent - 127);
116 unsigned float3_to_rgb9e5(const float rgb[3])
118 rgb9e5 retval;
119 float maxrgb;
120 int rm, gm, bm;
121 float rc, gc, bc;
122 int exp_shared, maxm;
123 double denom;
125 rc = ClampRange_for_rgb9e5(rgb[0]);
126 gc = ClampRange_for_rgb9e5(rgb[1]);
127 bc = ClampRange_for_rgb9e5(rgb[2]);
129 maxrgb = MaxOf3(rc, gc, bc);
130 exp_shared = MAX2(-RGB9E5_EXP_BIAS-1, FloorLog2(maxrgb)) + 1 + RGB9E5_EXP_BIAS;
131 assert(exp_shared <= RGB9E5_MAX_VALID_BIASED_EXP);
132 assert(exp_shared >= 0);
133 /* This pow function could be replaced by a table. */
134 denom = pow(2, exp_shared - RGB9E5_EXP_BIAS - RGB9E5_MANTISSA_BITS);
136 maxm = (int) floor(maxrgb / denom + 0.5);
137 if (maxm == MAX_RGB9E5_MANTISSA+1) {
138 denom *= 2;
139 exp_shared += 1;
140 assert(exp_shared <= RGB9E5_MAX_VALID_BIASED_EXP);
141 } else {
142 assert(maxm <= MAX_RGB9E5_MANTISSA);
145 rm = (int) floor(rc / denom + 0.5);
146 gm = (int) floor(gc / denom + 0.5);
147 bm = (int) floor(bc / denom + 0.5);
149 assert(rm <= MAX_RGB9E5_MANTISSA);
150 assert(gm <= MAX_RGB9E5_MANTISSA);
151 assert(bm <= MAX_RGB9E5_MANTISSA);
152 assert(rm >= 0);
153 assert(gm >= 0);
154 assert(bm >= 0);
156 retval.field.r = rm;
157 retval.field.g = gm;
158 retval.field.b = bm;
159 retval.field.biasedexponent = exp_shared;
161 return retval.raw;
164 void rgb9e5_to_float3(unsigned rgb, float retval[3])
166 rgb9e5 v;
167 int exponent;
168 float scale;
170 v.raw = rgb;
171 exponent = v.field.biasedexponent - RGB9E5_EXP_BIAS - RGB9E5_MANTISSA_BITS;
172 scale = (float) pow(2, exponent);
174 retval[0] = v.field.r * scale;
175 retval[1] = v.field.g * scale;
176 retval[2] = v.field.b * scale;