r300-gallium: Fix a handful of compiler warnings.
[mesa.git] / src / gallium / drivers / r300 / r300_state_inlines.h
1 /*
2 * Copyright 2009 Joakim Sindholt <opensource@zhasha.com>
3 * Corbin Simpson <MostAwesomeDude@gmail.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23
24 #ifndef R300_STATE_INLINES_H
25 #define R300_STATE_INLINES_H
26
27 #include "pipe/p_format.h"
28
29 #include "r300_reg.h"
30
31 /* Some maths. These should probably find their way to u_math, if needed. */
32
33 static INLINE int pack_float_16_6x(float f) {
34 return ((int)(f * 6.0) & 0xffff);
35 }
36
37 /* Blend state. */
38
39 static INLINE uint32_t r300_translate_blend_function(int blend_func)
40 {
41 switch (blend_func) {
42 case PIPE_BLEND_ADD:
43 return R300_COMB_FCN_ADD_CLAMP;
44 case PIPE_BLEND_SUBTRACT:
45 return R300_COMB_FCN_SUB_CLAMP;
46 case PIPE_BLEND_REVERSE_SUBTRACT:
47 return R300_COMB_FCN_RSUB_CLAMP;
48 case PIPE_BLEND_MIN:
49 return R300_COMB_FCN_MIN;
50 case PIPE_BLEND_MAX:
51 return R300_COMB_FCN_MAX;
52 default:
53 debug_printf("r300: Unknown blend function %d\n", blend_func);
54 break;
55 }
56 return 0;
57 }
58
59 /* XXX we can also offer the D3D versions of some of these... */
60 static INLINE uint32_t r300_translate_blend_factor(int blend_fact)
61 {
62 switch (blend_fact) {
63 case PIPE_BLENDFACTOR_ONE:
64 return R300_BLEND_GL_ONE;
65 case PIPE_BLENDFACTOR_SRC_COLOR:
66 return R300_BLEND_GL_SRC_COLOR;
67 case PIPE_BLENDFACTOR_SRC_ALPHA:
68 return R300_BLEND_GL_SRC_ALPHA;
69 case PIPE_BLENDFACTOR_DST_ALPHA:
70 return R300_BLEND_GL_DST_ALPHA;
71 case PIPE_BLENDFACTOR_DST_COLOR:
72 return R300_BLEND_GL_DST_COLOR;
73 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
74 return R300_BLEND_GL_SRC_ALPHA_SATURATE;
75 case PIPE_BLENDFACTOR_CONST_COLOR:
76 return R300_BLEND_GL_CONST_COLOR;
77 case PIPE_BLENDFACTOR_CONST_ALPHA:
78 return R300_BLEND_GL_CONST_ALPHA;
79 /* XXX WTF are these?
80 case PIPE_BLENDFACTOR_SRC1_COLOR:
81 case PIPE_BLENDFACTOR_SRC1_ALPHA: */
82 case PIPE_BLENDFACTOR_ZERO:
83 return R300_BLEND_GL_ZERO;
84 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
85 return R300_BLEND_GL_ONE_MINUS_SRC_COLOR;
86 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
87 return R300_BLEND_GL_ONE_MINUS_SRC_ALPHA;
88 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
89 return R300_BLEND_GL_ONE_MINUS_DST_ALPHA;
90 case PIPE_BLENDFACTOR_INV_DST_COLOR:
91 return R300_BLEND_GL_ONE_MINUS_DST_COLOR;
92 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
93 return R300_BLEND_GL_ONE_MINUS_CONST_COLOR;
94 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
95 return R300_BLEND_GL_ONE_MINUS_CONST_ALPHA;
96 /* XXX see above
97 case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
98 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA: */
99 default:
100 debug_printf("r300: Unknown blend factor %d\n", blend_fact);
101 break;
102 }
103 return 0;
104 }
105
106 /* DSA state. */
107
108 static INLINE uint32_t r300_translate_depth_stencil_function(int zs_func)
109 {
110 switch (zs_func) {
111 case PIPE_FUNC_NEVER:
112 return R300_ZS_NEVER;
113 case PIPE_FUNC_LESS:
114 return R300_ZS_LESS;
115 case PIPE_FUNC_EQUAL:
116 return R300_ZS_EQUAL;
117 case PIPE_FUNC_LEQUAL:
118 return R300_ZS_LEQUAL;
119 case PIPE_FUNC_GREATER:
120 return R300_ZS_GREATER;
121 case PIPE_FUNC_NOTEQUAL:
122 return R300_ZS_NOTEQUAL;
123 case PIPE_FUNC_GEQUAL:
124 return R300_ZS_GEQUAL;
125 case PIPE_FUNC_ALWAYS:
126 return R300_ZS_ALWAYS;
127 default:
128 debug_printf("r300: Unknown depth/stencil function %d\n",
129 zs_func);
130 break;
131 }
132 return 0;
133 }
134
135 static INLINE uint32_t r300_translate_stencil_op(int s_op)
136 {
137 switch (s_op) {
138 case PIPE_STENCIL_OP_KEEP:
139 return R300_ZS_KEEP;
140 case PIPE_STENCIL_OP_ZERO:
141 return R300_ZS_ZERO;
142 case PIPE_STENCIL_OP_REPLACE:
143 return R300_ZS_REPLACE;
144 case PIPE_STENCIL_OP_INCR:
145 return R300_ZS_INCR;
146 case PIPE_STENCIL_OP_DECR:
147 return R300_ZS_DECR;
148 case PIPE_STENCIL_OP_INCR_WRAP:
149 return R300_ZS_INCR_WRAP;
150 case PIPE_STENCIL_OP_DECR_WRAP:
151 return R300_ZS_DECR_WRAP;
152 case PIPE_STENCIL_OP_INVERT:
153 return R300_ZS_INVERT;
154 default:
155 debug_printf("r300: Unknown stencil op %d", s_op);
156 break;
157 }
158 return 0;
159 }
160
161 static INLINE uint32_t r300_translate_alpha_function(int alpha_func)
162 {
163 switch (alpha_func) {
164 case PIPE_FUNC_NEVER:
165 return R300_FG_ALPHA_FUNC_NEVER;
166 case PIPE_FUNC_LESS:
167 return R300_FG_ALPHA_FUNC_LESS;
168 case PIPE_FUNC_EQUAL:
169 return R300_FG_ALPHA_FUNC_EQUAL;
170 case PIPE_FUNC_LEQUAL:
171 return R300_FG_ALPHA_FUNC_LE;
172 case PIPE_FUNC_GREATER:
173 return R300_FG_ALPHA_FUNC_GREATER;
174 case PIPE_FUNC_NOTEQUAL:
175 return R300_FG_ALPHA_FUNC_NOTEQUAL;
176 case PIPE_FUNC_GEQUAL:
177 return R300_FG_ALPHA_FUNC_GE;
178 case PIPE_FUNC_ALWAYS:
179 return R300_FG_ALPHA_FUNC_ALWAYS;
180 default:
181 debug_printf("r300: Unknown alpha function %d", alpha_func);
182 break;
183 }
184 return 0;
185 }
186
187 /* Texture sampler state. */
188
189 static INLINE uint32_t r300_translate_wrap(int wrap)
190 {
191 switch (wrap) {
192 case PIPE_TEX_WRAP_REPEAT:
193 return R300_TX_REPEAT;
194 case PIPE_TEX_WRAP_CLAMP:
195 return R300_TX_CLAMP;
196 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
197 return R300_TX_CLAMP_TO_EDGE;
198 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
199 return R300_TX_CLAMP_TO_BORDER;
200 case PIPE_TEX_WRAP_MIRROR_REPEAT:
201 return R300_TX_REPEAT | R300_TX_MIRRORED;
202 case PIPE_TEX_WRAP_MIRROR_CLAMP:
203 return R300_TX_CLAMP | R300_TX_MIRRORED;
204 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
205 return R300_TX_CLAMP_TO_EDGE | R300_TX_MIRRORED;
206 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
207 return R300_TX_CLAMP_TO_EDGE | R300_TX_MIRRORED;
208 default:
209 debug_printf("r300: Unknown texture wrap %d", wrap);
210 return 0;
211 }
212 }
213
214 static INLINE uint32_t r300_translate_tex_filters(int min, int mag, int mip)
215 {
216 uint32_t retval = 0;
217 switch (min) {
218 case PIPE_TEX_FILTER_NEAREST:
219 retval |= R300_TX_MIN_FILTER_NEAREST;
220 case PIPE_TEX_FILTER_LINEAR:
221 retval |= R300_TX_MIN_FILTER_LINEAR;
222 case PIPE_TEX_FILTER_ANISO:
223 retval |= R300_TX_MIN_FILTER_ANISO;
224 default:
225 debug_printf("r300: Unknown texture filter %d", min);
226 break;
227 }
228 switch (mag) {
229 case PIPE_TEX_FILTER_NEAREST:
230 retval |= R300_TX_MAG_FILTER_NEAREST;
231 case PIPE_TEX_FILTER_LINEAR:
232 retval |= R300_TX_MAG_FILTER_LINEAR;
233 case PIPE_TEX_FILTER_ANISO:
234 retval |= R300_TX_MAG_FILTER_ANISO;
235 default:
236 debug_printf("r300: Unknown texture filter %d", mag);
237 break;
238 }
239 switch (mip) {
240 case PIPE_TEX_MIPFILTER_NONE:
241 retval |= R300_TX_MIN_FILTER_MIP_NONE;
242 case PIPE_TEX_MIPFILTER_NEAREST:
243 retval |= R300_TX_MIN_FILTER_MIP_NEAREST;
244 case PIPE_TEX_MIPFILTER_LINEAR:
245 retval |= R300_TX_MIN_FILTER_MIP_LINEAR;
246 default:
247 debug_printf("r300: Unknown texture filter %d", mip);
248 break;
249 }
250
251 return retval;
252 }
253
254 static INLINE uint32_t r300_anisotropy(float max_aniso)
255 {
256 if (max_aniso >= 16.0f) {
257 return R300_TX_MAX_ANISO_16_TO_1;
258 } else if (max_aniso >= 8.0f) {
259 return R300_TX_MAX_ANISO_8_TO_1;
260 } else if (max_aniso >= 4.0f) {
261 return R300_TX_MAX_ANISO_4_TO_1;
262 } else if (max_aniso >= 2.0f) {
263 return R300_TX_MAX_ANISO_2_TO_1;
264 } else {
265 return R300_TX_MAX_ANISO_1_TO_1;
266 }
267 }
268
269 /* Buffer formats. */
270
271 static INLINE uint32_t r300_translate_colorformat(enum pipe_format format)
272 {
273 switch (format) {
274 /* 8-bit buffers */
275 case PIPE_FORMAT_I8_UNORM:
276 return R300_COLOR_FORMAT_I8;
277 /* 16-bit buffers */
278 case PIPE_FORMAT_R5G6B5_UNORM:
279 return R300_COLOR_FORMAT_RGB565;
280 case PIPE_FORMAT_A1R5G5B5_UNORM:
281 return R300_COLOR_FORMAT_ARGB1555;
282 case PIPE_FORMAT_A4R4G4B4_UNORM:
283 return R300_COLOR_FORMAT_ARGB4444;
284 /* 32-bit buffers */
285 case PIPE_FORMAT_A8R8G8B8_UNORM:
286 return R300_COLOR_FORMAT_ARGB8888;
287 /* XXX Not in pipe_format
288 case PIPE_FORMAT_A32R32G32B32:
289 return R300_COLOR_FORMAT_ARGB32323232;
290 case PIPE_FORMAT_A16R16G16B16:
291 return R300_COLOR_FORMAT_ARGB16161616; */
292 /* XXX Not in pipe_format
293 case PIPE_FORMAT_A10R10G10B10_UNORM:
294 return R500_COLOR_FORMAT_ARGB10101010;
295 case PIPE_FORMAT_A2R10G10B10_UNORM:
296 return R500_COLOR_FORMAT_ARGB2101010;
297 case PIPE_FORMAT_I10_UNORM:
298 return R500_COLOR_FORMAT_I10; */
299 default:
300 debug_printf("r300: Implementation error: " \
301 "Got unsupported color format %s in %s\n",
302 pf_name(format), __FUNCTION__);
303 break;
304 }
305 return 0;
306 }
307
308 static INLINE uint32_t r300_translate_zsformat(enum pipe_format format)
309 {
310 switch (format) {
311 /* 16-bit depth, no stencil */
312 case PIPE_FORMAT_Z16_UNORM:
313 return R300_DEPTHFORMAT_16BIT_INT_Z;
314 /* 24-bit depth, 8-bit stencil */
315 case PIPE_FORMAT_Z24S8_UNORM:
316 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
317 default:
318 debug_printf("r300: Implementation error: " \
319 "Got unsupported ZS format %s in %s\n",
320 pf_name(format), __FUNCTION__);
321 break;
322 }
323 return 0;
324 }
325
326 /* Non-CSO state. (For now.) */
327
328 static INLINE uint32_t r300_translate_gb_pipes(int pipe_count)
329 {
330 switch (pipe_count) {
331 case 1:
332 return R300_GB_TILE_PIPE_COUNT_RV300;
333 break;
334 case 2:
335 return R300_GB_TILE_PIPE_COUNT_R300;
336 break;
337 case 3:
338 return R300_GB_TILE_PIPE_COUNT_R420_3P;
339 break;
340 case 4:
341 return R300_GB_TILE_PIPE_COUNT_R420;
342 break;
343 }
344 return 0;
345 }
346
347 static INLINE uint32_t translate_vertex_data_type(int type) {
348 switch (type) {
349 case EMIT_1F:
350 case EMIT_1F_PSIZE:
351 return R300_DATA_TYPE_FLOAT_1;
352 break;
353 case EMIT_2F:
354 return R300_DATA_TYPE_FLOAT_2;
355 break;
356 case EMIT_3F:
357 return R300_DATA_TYPE_FLOAT_3;
358 break;
359 case EMIT_4F:
360 return R300_DATA_TYPE_FLOAT_4;
361 break;
362 case EMIT_4UB:
363 return R300_DATA_TYPE_BYTE;
364 break;
365 default:
366 debug_printf("r300: Implementation error: "
367 "Bad vertex data type!\n");
368 assert(0);
369 break;
370 }
371
372 return 0;
373 }
374
375 #endif /* R300_STATE_INLINES_H */