Merge branch '7.8'
[mesa.git] / src / mesa / drivers / dri / nouveau / nouveau_util.h
1 /*
2 * Copyright (C) 2009 Francisco Jerez.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 */
26
27 #ifndef __NOUVEAU_UTIL_H__
28 #define __NOUVEAU_UTIL_H__
29
30 #include "main/formats.h"
31 #include "main/colormac.h"
32
33 static inline unsigned
34 pack_rgba_i(gl_format f, uint8_t c[])
35 {
36 switch (f) {
37 case MESA_FORMAT_ARGB8888:
38 return PACK_COLOR_8888(c[ACOMP], c[RCOMP], c[GCOMP], c[BCOMP]);
39 case MESA_FORMAT_ARGB8888_REV:
40 return PACK_COLOR_8888(c[BCOMP], c[GCOMP], c[RCOMP], c[ACOMP]);
41 case MESA_FORMAT_XRGB8888:
42 return PACK_COLOR_8888(0, c[RCOMP], c[GCOMP], c[BCOMP]);
43 case MESA_FORMAT_XRGB8888_REV:
44 return PACK_COLOR_8888(c[BCOMP], c[GCOMP], c[RCOMP], 0);
45 case MESA_FORMAT_RGBA8888:
46 return PACK_COLOR_8888(c[RCOMP], c[GCOMP], c[BCOMP], c[ACOMP]);
47 case MESA_FORMAT_RGBA8888_REV:
48 return PACK_COLOR_8888(c[ACOMP], c[BCOMP], c[GCOMP], c[RCOMP]);
49 case MESA_FORMAT_RGB565:
50 return PACK_COLOR_565(c[RCOMP], c[GCOMP], c[BCOMP]);
51 default:
52 assert(0);
53 }
54 }
55
56 static inline unsigned
57 pack_zs_i(gl_format f, uint32_t z, uint8_t s)
58 {
59 switch (f) {
60 case MESA_FORMAT_Z24_S8:
61 return (z & 0xffffff00) | (s & 0xff);
62 case MESA_FORMAT_Z24_X8:
63 return (z & 0xffffff00);
64 case MESA_FORMAT_Z16:
65 return (z & 0xffff0000) >> 16;
66 default:
67 assert(0);
68 }
69 }
70
71 static inline unsigned
72 pack_rgba_f(gl_format f, float c[])
73 {
74 return pack_rgba_i(f, (uint8_t []) {
75 FLOAT_TO_UBYTE(c[RCOMP]),
76 FLOAT_TO_UBYTE(c[GCOMP]),
77 FLOAT_TO_UBYTE(c[BCOMP]),
78 FLOAT_TO_UBYTE(c[ACOMP]) });
79 }
80
81 static inline unsigned
82 pack_zs_f(gl_format f, float z, uint8_t s)
83 {
84 return pack_zs_i(f, FLOAT_TO_UINT(z), s);
85 }
86
87 /* Integer base-2 logarithm, rounded towards zero. */
88 static inline unsigned
89 log2i(unsigned i)
90 {
91 unsigned r = 0;
92
93 if (i & 0xffff0000) {
94 i >>= 16;
95 r += 16;
96 }
97 if (i & 0x0000ff00) {
98 i >>= 8;
99 r += 8;
100 }
101 if (i & 0x000000f0) {
102 i >>= 4;
103 r += 4;
104 }
105 if (i & 0x0000000c) {
106 i >>= 2;
107 r += 2;
108 }
109 if (i & 0x00000002) {
110 r += 1;
111 }
112 return r;
113 }
114
115 static inline unsigned
116 align(unsigned x, unsigned m)
117 {
118 return (x + m - 1) & ~(m - 1);
119 }
120
121 static inline void
122 get_scissors(struct gl_framebuffer *fb, int *x, int *y, int *w, int *h)
123 {
124 *w = fb->_Xmax - fb->_Xmin;
125 *h = fb->_Ymax - fb->_Ymin;
126 *x = fb->_Xmin;
127 *y = (fb->Name ? fb->_Ymin :
128 /* Window system FBO: Flip the Y coordinate. */
129 fb->Height - fb->_Ymax);
130 }
131
132 static inline void
133 get_viewport_scale(GLcontext *ctx, float a[16])
134 {
135 struct gl_viewport_attrib *vp = &ctx->Viewport;
136 struct gl_framebuffer *fb = ctx->DrawBuffer;
137
138 a[MAT_SX] = (float)vp->Width / 2;
139
140 if (fb->Name)
141 a[MAT_SY] = (float)vp->Height / 2;
142 else
143 /* Window system FBO: Flip the Y coordinate. */
144 a[MAT_SY] = - (float)vp->Height / 2;
145
146 a[MAT_SZ] = fb->_DepthMaxF * (vp->Far - vp->Near) / 2;
147 }
148
149 static inline void
150 get_viewport_translate(GLcontext *ctx, float a[4])
151 {
152 struct gl_viewport_attrib *vp = &ctx->Viewport;
153 struct gl_framebuffer *fb = ctx->DrawBuffer;
154
155 a[0] = (float)vp->Width / 2 + vp->X;
156
157 if (fb->Name)
158 a[1] = (float)vp->Height / 2 + vp->Y;
159 else
160 /* Window system FBO: Flip the Y coordinate. */
161 a[1] = fb->Height - (float)vp->Height / 2 - vp->Y;
162
163 a[2] = fb->_DepthMaxF * (vp->Far + vp->Near) / 2;
164 }
165
166 static inline void
167 OUT_RINGm(struct nouveau_channel *chan, float m[16])
168 {
169 int i, j;
170
171 for (i = 0; i < 4; i++)
172 for (j = 0; j < 4; j++)
173 OUT_RINGf(chan, m[4*j + i]);
174 }
175
176 static inline GLboolean
177 is_color_operand(int op)
178 {
179 return op == GL_SRC_COLOR || op == GL_ONE_MINUS_SRC_COLOR;
180 }
181
182 static inline GLboolean
183 is_negative_operand(int op)
184 {
185 return op == GL_ONE_MINUS_SRC_COLOR || op == GL_ONE_MINUS_SRC_ALPHA;
186 }
187
188 static inline GLboolean
189 is_texture_source(int s)
190 {
191 return s == GL_TEXTURE || (s >= GL_TEXTURE0 && s <= GL_TEXTURE31);
192 }
193
194 static inline struct gl_texgen *
195 get_texgen_coord(struct gl_texture_unit *u, int i)
196 {
197 return ((struct gl_texgen *[])
198 { &u->GenS, &u->GenT, &u->GenR, &u->GenQ }) [i];
199 }
200
201 static inline float *
202 get_texgen_coeff(struct gl_texgen *c)
203 {
204 if (c->Mode == GL_OBJECT_LINEAR)
205 return c->ObjectPlane;
206 else if (c->Mode == GL_EYE_LINEAR)
207 return c->EyePlane;
208 else
209 return NULL;
210 }
211
212 #endif