361443a692375abf38f4afeb435338eaa421ecb3
[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 /* Blend state. */
32
33 static INLINE uint32_t r300_translate_blend_function(int blend_func)
34 {
35 switch (blend_func) {
36 case PIPE_BLEND_ADD:
37 return R300_COMB_FCN_ADD_CLAMP;
38 case PIPE_BLEND_SUBTRACT:
39 return R300_COMB_FCN_SUB_CLAMP;
40 case PIPE_BLEND_REVERSE_SUBTRACT:
41 return R300_COMB_FCN_RSUB_CLAMP;
42 case PIPE_BLEND_MIN:
43 return R300_COMB_FCN_MIN;
44 case PIPE_BLEND_MAX:
45 return R300_COMB_FCN_MAX;
46 default:
47 debug_printf("r300: Unknown blend function %d\n", blend_func);
48 break;
49 }
50 return 0;
51 }
52
53 /* XXX we can also offer the D3D versions of some of these... */
54 static INLINE uint32_t r300_translate_blend_factor(int blend_fact)
55 {
56 switch (blend_fact) {
57 case PIPE_BLENDFACTOR_ONE:
58 return R300_BLEND_GL_ONE;
59 case PIPE_BLENDFACTOR_SRC_COLOR:
60 return R300_BLEND_GL_SRC_COLOR;
61 case PIPE_BLENDFACTOR_SRC_ALPHA:
62 return R300_BLEND_GL_SRC_ALPHA;
63 case PIPE_BLENDFACTOR_DST_ALPHA:
64 return R300_BLEND_GL_DST_ALPHA;
65 case PIPE_BLENDFACTOR_DST_COLOR:
66 return R300_BLEND_GL_DST_COLOR;
67 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
68 return R300_BLEND_GL_SRC_ALPHA_SATURATE;
69 case PIPE_BLENDFACTOR_CONST_COLOR:
70 return R300_BLEND_GL_CONST_COLOR;
71 case PIPE_BLENDFACTOR_CONST_ALPHA:
72 return R300_BLEND_GL_CONST_ALPHA;
73 /* XXX WTF are these?
74 case PIPE_BLENDFACTOR_SRC1_COLOR:
75 case PIPE_BLENDFACTOR_SRC1_ALPHA: */
76 case PIPE_BLENDFACTOR_ZERO:
77 return R300_BLEND_GL_ZERO;
78 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
79 return R300_BLEND_GL_ONE_MINUS_SRC_COLOR;
80 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
81 return R300_BLEND_GL_ONE_MINUS_SRC_ALPHA;
82 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
83 return R300_BLEND_GL_ONE_MINUS_DST_ALPHA;
84 case PIPE_BLENDFACTOR_INV_DST_COLOR:
85 return R300_BLEND_GL_ONE_MINUS_DST_COLOR;
86 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
87 return R300_BLEND_GL_ONE_MINUS_CONST_COLOR;
88 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
89 return R300_BLEND_GL_ONE_MINUS_CONST_ALPHA;
90 /* XXX see above
91 case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
92 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA: */
93 default:
94 debug_printf("r300: Unknown blend factor %d\n", blend_fact);
95 break;
96 }
97 return 0;
98 }
99
100 /* DSA state. */
101
102 static INLINE uint32_t r300_translate_depth_stencil_function(int zs_func)
103 {
104 switch (zs_func) {
105 case PIPE_FUNC_NEVER:
106 return R300_ZS_NEVER;
107 case PIPE_FUNC_LESS:
108 return R300_ZS_LESS;
109 case PIPE_FUNC_EQUAL:
110 return R300_ZS_EQUAL;
111 case PIPE_FUNC_LEQUAL:
112 return R300_ZS_LEQUAL;
113 case PIPE_FUNC_GREATER:
114 return R300_ZS_GREATER;
115 case PIPE_FUNC_NOTEQUAL:
116 return R300_ZS_NOTEQUAL;
117 case PIPE_FUNC_GEQUAL:
118 return R300_ZS_GEQUAL;
119 case PIPE_FUNC_ALWAYS:
120 return R300_ZS_ALWAYS;
121 default:
122 debug_printf("r300: Unknown depth/stencil function %d\n",
123 zs_func);
124 break;
125 }
126 return 0;
127 }
128
129 static INLINE uint32_t r300_translate_stencil_op(int s_op)
130 {
131 switch (s_op) {
132 case PIPE_STENCIL_OP_KEEP:
133 return R300_ZS_KEEP;
134 case PIPE_STENCIL_OP_ZERO:
135 return R300_ZS_ZERO;
136 case PIPE_STENCIL_OP_REPLACE:
137 return R300_ZS_REPLACE;
138 case PIPE_STENCIL_OP_INCR:
139 return R300_ZS_INCR;
140 case PIPE_STENCIL_OP_DECR:
141 return R300_ZS_DECR;
142 case PIPE_STENCIL_OP_INCR_WRAP:
143 return R300_ZS_INCR_WRAP;
144 case PIPE_STENCIL_OP_DECR_WRAP:
145 return R300_ZS_DECR_WRAP;
146 case PIPE_STENCIL_OP_INVERT:
147 return R300_ZS_INVERT;
148 default:
149 debug_printf("r300: Unknown stencil op %d", s_op);
150 break;
151 }
152 return 0;
153 }
154
155 static INLINE uint32_t r300_translate_alpha_function(int alpha_func)
156 {
157 switch (alpha_func) {
158 case PIPE_FUNC_NEVER:
159 return R300_FG_ALPHA_FUNC_NEVER;
160 case PIPE_FUNC_LESS:
161 return R300_FG_ALPHA_FUNC_LESS;
162 case PIPE_FUNC_EQUAL:
163 return R300_FG_ALPHA_FUNC_EQUAL;
164 case PIPE_FUNC_LEQUAL:
165 return R300_FG_ALPHA_FUNC_LE;
166 case PIPE_FUNC_GREATER:
167 return R300_FG_ALPHA_FUNC_GREATER;
168 case PIPE_FUNC_NOTEQUAL:
169 return R300_FG_ALPHA_FUNC_NOTEQUAL;
170 case PIPE_FUNC_GEQUAL:
171 return R300_FG_ALPHA_FUNC_GE;
172 case PIPE_FUNC_ALWAYS:
173 return R300_FG_ALPHA_FUNC_ALWAYS;
174 default:
175 debug_printf("r300: Unknown alpha function %d", alpha_func);
176 break;
177 }
178 return 0;
179 }
180
181 /* Texture sampler state. */
182
183 static INLINE uint32_t r300_translate_wrap(int wrap)
184 {
185 switch (wrap) {
186 case PIPE_TEX_WRAP_REPEAT:
187 return R300_TX_REPEAT;
188 case PIPE_TEX_WRAP_CLAMP:
189 return R300_TX_CLAMP;
190 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
191 return R300_TX_CLAMP_TO_EDGE;
192 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
193 return R300_TX_CLAMP_TO_BORDER;
194 case PIPE_TEX_WRAP_MIRROR_REPEAT:
195 return R300_TX_REPEAT | R300_TX_MIRRORED;
196 case PIPE_TEX_WRAP_MIRROR_CLAMP:
197 return R300_TX_CLAMP | R300_TX_MIRRORED;
198 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
199 return R300_TX_CLAMP_TO_EDGE | R300_TX_MIRRORED;
200 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
201 return R300_TX_CLAMP_TO_EDGE | R300_TX_MIRRORED;
202 default:
203 debug_printf("r300: Unknown texture wrap %d", wrap);
204 return 0;
205 }
206 }
207
208 static INLINE uint32_t r300_translate_tex_filters(int min, int mag, int mip)
209 {
210 uint32_t retval = 0;
211 switch (min) {
212 case PIPE_TEX_FILTER_NEAREST:
213 retval |= R300_TX_MIN_FILTER_NEAREST;
214 case PIPE_TEX_FILTER_LINEAR:
215 retval |= R300_TX_MIN_FILTER_LINEAR;
216 case PIPE_TEX_FILTER_ANISO:
217 retval |= R300_TX_MIN_FILTER_ANISO;
218 default:
219 debug_printf("r300: Unknown texture filter %d", min);
220 break;
221 }
222 switch (mag) {
223 case PIPE_TEX_FILTER_NEAREST:
224 retval |= R300_TX_MAG_FILTER_NEAREST;
225 case PIPE_TEX_FILTER_LINEAR:
226 retval |= R300_TX_MAG_FILTER_LINEAR;
227 case PIPE_TEX_FILTER_ANISO:
228 retval |= R300_TX_MAG_FILTER_ANISO;
229 default:
230 debug_printf("r300: Unknown texture filter %d", mag);
231 break;
232 }
233 switch (mip) {
234 case PIPE_TEX_MIPFILTER_NONE:
235 retval |= R300_TX_MIN_FILTER_MIP_NONE;
236 case PIPE_TEX_MIPFILTER_NEAREST:
237 retval |= R300_TX_MIN_FILTER_MIP_NEAREST;
238 case PIPE_TEX_MIPFILTER_LINEAR:
239 retval |= R300_TX_MIN_FILTER_MIP_LINEAR;
240 default:
241 debug_printf("r300: Unknown texture filter %d", mip);
242 break;
243 }
244
245 return retval;
246 }
247
248 static INLINE uint32_t r300_anisotropy(float max_aniso)
249 {
250 if (max_aniso >= 16.0f) {
251 return R300_TX_MAX_ANISO_16_TO_1;
252 } else if (max_aniso >= 8.0f) {
253 return R300_TX_MAX_ANISO_8_TO_1;
254 } else if (max_aniso >= 4.0f) {
255 return R300_TX_MAX_ANISO_4_TO_1;
256 } else if (max_aniso >= 2.0f) {
257 return R300_TX_MAX_ANISO_2_TO_1;
258 } else {
259 return R300_TX_MAX_ANISO_1_TO_1;
260 }
261 }
262
263 /* Buffer formats. */
264
265 static INLINE uint32_t r300_translate_colorformat(enum pipe_format format)
266 {
267 switch (format) {
268 /* 8-bit buffers */
269 case PIPE_FORMAT_I8_UNORM:
270 return R300_COLOR_FORMAT_I8;
271 /* 16-bit buffers */
272 case PIPE_FORMAT_R5G6B5_UNORM:
273 return R300_COLOR_FORMAT_RGB565;
274 case PIPE_FORMAT_A1R5G5B5_UNORM:
275 return R300_COLOR_FORMAT_ARGB1555;
276 case PIPE_FORMAT_A4R4G4B4_UNORM:
277 return R300_COLOR_FORMAT_ARGB4444;
278 /* 32-bit buffers */
279 case PIPE_FORMAT_A8R8G8B8_UNORM:
280 return R300_COLOR_FORMAT_ARGB8888;
281 /* XXX Not in pipe_format
282 case PIPE_FORMAT_A32R32G32B32:
283 return R300_COLOR_FORMAT_ARGB32323232;
284 case PIPE_FORMAT_A16R16G16B16:
285 return R300_COLOR_FORMAT_ARGB16161616; */
286 /* XXX Not in pipe_format
287 case PIPE_FORMAT_A10R10G10B10_UNORM:
288 return R500_COLOR_FORMAT_ARGB10101010;
289 case PIPE_FORMAT_A2R10G10B10_UNORM:
290 return R500_COLOR_FORMAT_ARGB2101010;
291 case PIPE_FORMAT_I10_UNORM:
292 return R500_COLOR_FORMAT_I10; */
293 default:
294 debug_printf("r300: Implementation error: " \
295 "Got unsupported color format %s in %s\n",
296 pf_name(format), __FUNCTION__);
297 break;
298 }
299 return 0;
300 }
301
302 static INLINE uint32_t r300_translate_zsformat(enum pipe_format format)
303 {
304 switch (format) {
305 /* 16-bit depth, no stencil */
306 case PIPE_FORMAT_Z16_UNORM:
307 return R300_DEPTHFORMAT_16BIT_INT_Z;
308 /* 24-bit depth, 8-bit stencil */
309 case PIPE_FORMAT_Z24S8_UNORM:
310 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
311 default:
312 debug_printf("r300: Implementation error: " \
313 "Got unsupported ZS format %s in %s\n",
314 pf_name(format), __FUNCTION__);
315 break;
316 }
317 return 0;
318 }
319
320 #endif /* R300_STATE_INLINES_H */