Merge branch 'pipe-format-simplify'
[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 "draw/draw_vertex.h"
28
29 #include "pipe/p_format.h"
30
31 #include "util/u_format.h"
32
33 #include "r300_reg.h"
34
35 /* Some maths. These should probably find their way to u_math, if needed. */
36
37 static INLINE int pack_float_16_6x(float f) {
38 return ((int)(f * 6.0) & 0xffff);
39 }
40
41 /* Blend state. */
42
43 static INLINE uint32_t r300_translate_blend_function(int blend_func)
44 {
45 switch (blend_func) {
46 case PIPE_BLEND_ADD:
47 return R300_COMB_FCN_ADD_CLAMP;
48 case PIPE_BLEND_SUBTRACT:
49 return R300_COMB_FCN_SUB_CLAMP;
50 case PIPE_BLEND_REVERSE_SUBTRACT:
51 return R300_COMB_FCN_RSUB_CLAMP;
52 case PIPE_BLEND_MIN:
53 return R300_COMB_FCN_MIN;
54 case PIPE_BLEND_MAX:
55 return R300_COMB_FCN_MAX;
56 default:
57 debug_printf("r300: Unknown blend function %d\n", blend_func);
58 assert(0);
59 break;
60 }
61 return 0;
62 }
63
64 /* XXX we can also offer the D3D versions of some of these... */
65 static INLINE uint32_t r300_translate_blend_factor(int blend_fact)
66 {
67 switch (blend_fact) {
68 case PIPE_BLENDFACTOR_ONE:
69 return R300_BLEND_GL_ONE;
70 case PIPE_BLENDFACTOR_SRC_COLOR:
71 return R300_BLEND_GL_SRC_COLOR;
72 case PIPE_BLENDFACTOR_SRC_ALPHA:
73 return R300_BLEND_GL_SRC_ALPHA;
74 case PIPE_BLENDFACTOR_DST_ALPHA:
75 return R300_BLEND_GL_DST_ALPHA;
76 case PIPE_BLENDFACTOR_DST_COLOR:
77 return R300_BLEND_GL_DST_COLOR;
78 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE:
79 return R300_BLEND_GL_SRC_ALPHA_SATURATE;
80 case PIPE_BLENDFACTOR_CONST_COLOR:
81 return R300_BLEND_GL_CONST_COLOR;
82 case PIPE_BLENDFACTOR_CONST_ALPHA:
83 return R300_BLEND_GL_CONST_ALPHA;
84 /* XXX WTF are these?
85 case PIPE_BLENDFACTOR_SRC1_COLOR:
86 case PIPE_BLENDFACTOR_SRC1_ALPHA: */
87 case PIPE_BLENDFACTOR_ZERO:
88 return R300_BLEND_GL_ZERO;
89 case PIPE_BLENDFACTOR_INV_SRC_COLOR:
90 return R300_BLEND_GL_ONE_MINUS_SRC_COLOR;
91 case PIPE_BLENDFACTOR_INV_SRC_ALPHA:
92 return R300_BLEND_GL_ONE_MINUS_SRC_ALPHA;
93 case PIPE_BLENDFACTOR_INV_DST_ALPHA:
94 return R300_BLEND_GL_ONE_MINUS_DST_ALPHA;
95 case PIPE_BLENDFACTOR_INV_DST_COLOR:
96 return R300_BLEND_GL_ONE_MINUS_DST_COLOR;
97 case PIPE_BLENDFACTOR_INV_CONST_COLOR:
98 return R300_BLEND_GL_ONE_MINUS_CONST_COLOR;
99 case PIPE_BLENDFACTOR_INV_CONST_ALPHA:
100 return R300_BLEND_GL_ONE_MINUS_CONST_ALPHA;
101 /* XXX see above
102 case PIPE_BLENDFACTOR_INV_SRC1_COLOR:
103 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA: */
104 default:
105 debug_printf("r300: Unknown blend factor %d\n", blend_fact);
106 assert(0);
107 break;
108 }
109 return 0;
110 }
111
112 /* DSA state. */
113
114 static INLINE uint32_t r300_translate_depth_stencil_function(int zs_func)
115 {
116 switch (zs_func) {
117 case PIPE_FUNC_NEVER:
118 return R300_ZS_NEVER;
119 case PIPE_FUNC_LESS:
120 return R300_ZS_LESS;
121 case PIPE_FUNC_EQUAL:
122 return R300_ZS_EQUAL;
123 case PIPE_FUNC_LEQUAL:
124 return R300_ZS_LEQUAL;
125 case PIPE_FUNC_GREATER:
126 return R300_ZS_GREATER;
127 case PIPE_FUNC_NOTEQUAL:
128 return R300_ZS_NOTEQUAL;
129 case PIPE_FUNC_GEQUAL:
130 return R300_ZS_GEQUAL;
131 case PIPE_FUNC_ALWAYS:
132 return R300_ZS_ALWAYS;
133 default:
134 debug_printf("r300: Unknown depth/stencil function %d\n",
135 zs_func);
136 assert(0);
137 break;
138 }
139 return 0;
140 }
141
142 static INLINE uint32_t r300_translate_stencil_op(int s_op)
143 {
144 switch (s_op) {
145 case PIPE_STENCIL_OP_KEEP:
146 return R300_ZS_KEEP;
147 case PIPE_STENCIL_OP_ZERO:
148 return R300_ZS_ZERO;
149 case PIPE_STENCIL_OP_REPLACE:
150 return R300_ZS_REPLACE;
151 case PIPE_STENCIL_OP_INCR:
152 return R300_ZS_INCR;
153 case PIPE_STENCIL_OP_DECR:
154 return R300_ZS_DECR;
155 case PIPE_STENCIL_OP_INCR_WRAP:
156 return R300_ZS_INCR_WRAP;
157 case PIPE_STENCIL_OP_DECR_WRAP:
158 return R300_ZS_DECR_WRAP;
159 case PIPE_STENCIL_OP_INVERT:
160 return R300_ZS_INVERT;
161 default:
162 debug_printf("r300: Unknown stencil op %d", s_op);
163 assert(0);
164 break;
165 }
166 return 0;
167 }
168
169 static INLINE uint32_t r300_translate_alpha_function(int alpha_func)
170 {
171 switch (alpha_func) {
172 case PIPE_FUNC_NEVER:
173 return R300_FG_ALPHA_FUNC_NEVER;
174 case PIPE_FUNC_LESS:
175 return R300_FG_ALPHA_FUNC_LESS;
176 case PIPE_FUNC_EQUAL:
177 return R300_FG_ALPHA_FUNC_EQUAL;
178 case PIPE_FUNC_LEQUAL:
179 return R300_FG_ALPHA_FUNC_LE;
180 case PIPE_FUNC_GREATER:
181 return R300_FG_ALPHA_FUNC_GREATER;
182 case PIPE_FUNC_NOTEQUAL:
183 return R300_FG_ALPHA_FUNC_NOTEQUAL;
184 case PIPE_FUNC_GEQUAL:
185 return R300_FG_ALPHA_FUNC_GE;
186 case PIPE_FUNC_ALWAYS:
187 return R300_FG_ALPHA_FUNC_ALWAYS;
188 default:
189 debug_printf("r300: Unknown alpha function %d", alpha_func);
190 assert(0);
191 break;
192 }
193 return 0;
194 }
195
196 static INLINE uint32_t
197 r300_translate_polygon_mode_front(unsigned mode) {
198 switch (mode)
199 {
200 case PIPE_POLYGON_MODE_FILL:
201 return R300_GA_POLY_MODE_FRONT_PTYPE_TRI;
202 case PIPE_POLYGON_MODE_LINE:
203 return R300_GA_POLY_MODE_FRONT_PTYPE_LINE;
204 case PIPE_POLYGON_MODE_POINT:
205 return R300_GA_POLY_MODE_FRONT_PTYPE_POINT;
206
207 default:
208 debug_printf("r300: Bad polygon mode %i in %s\n", mode,
209 __FUNCTION__);
210 return R300_GA_POLY_MODE_FRONT_PTYPE_TRI;
211 }
212 }
213
214 static INLINE uint32_t
215 r300_translate_polygon_mode_back(unsigned mode) {
216 switch (mode)
217 {
218 case PIPE_POLYGON_MODE_FILL:
219 return R300_GA_POLY_MODE_BACK_PTYPE_TRI;
220 case PIPE_POLYGON_MODE_LINE:
221 return R300_GA_POLY_MODE_BACK_PTYPE_LINE;
222 case PIPE_POLYGON_MODE_POINT:
223 return R300_GA_POLY_MODE_BACK_PTYPE_POINT;
224
225 default:
226 debug_printf("r300: Bad polygon mode %i in %s\n", mode,
227 __FUNCTION__);
228 return R300_GA_POLY_MODE_BACK_PTYPE_TRI;
229 }
230 }
231
232 /* Texture sampler state. */
233
234 static INLINE uint32_t r300_translate_wrap(int wrap)
235 {
236 switch (wrap) {
237 case PIPE_TEX_WRAP_REPEAT:
238 return R300_TX_REPEAT;
239 case PIPE_TEX_WRAP_CLAMP:
240 return R300_TX_CLAMP;
241 case PIPE_TEX_WRAP_CLAMP_TO_EDGE:
242 return R300_TX_CLAMP_TO_EDGE;
243 case PIPE_TEX_WRAP_CLAMP_TO_BORDER:
244 return R300_TX_CLAMP_TO_BORDER;
245 case PIPE_TEX_WRAP_MIRROR_REPEAT:
246 return R300_TX_REPEAT | R300_TX_MIRRORED;
247 case PIPE_TEX_WRAP_MIRROR_CLAMP:
248 return R300_TX_CLAMP | R300_TX_MIRRORED;
249 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE:
250 return R300_TX_CLAMP_TO_EDGE | R300_TX_MIRRORED;
251 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER:
252 return R300_TX_CLAMP_TO_EDGE | R300_TX_MIRRORED;
253 default:
254 debug_printf("r300: Unknown texture wrap %d", wrap);
255 assert(0);
256 return 0;
257 }
258 }
259
260 static INLINE uint32_t r300_translate_tex_filters(int min, int mag, int mip)
261 {
262 uint32_t retval = 0;
263 switch (min) {
264 case PIPE_TEX_FILTER_NEAREST:
265 retval |= R300_TX_MIN_FILTER_NEAREST;
266 break;
267 case PIPE_TEX_FILTER_LINEAR:
268 retval |= R300_TX_MIN_FILTER_LINEAR;
269 break;
270 case PIPE_TEX_FILTER_ANISO:
271 retval |= R300_TX_MIN_FILTER_ANISO;
272 break;
273 default:
274 debug_printf("r300: Unknown texture filter %d\n", min);
275 assert(0);
276 break;
277 }
278 switch (mag) {
279 case PIPE_TEX_FILTER_NEAREST:
280 retval |= R300_TX_MAG_FILTER_NEAREST;
281 break;
282 case PIPE_TEX_FILTER_LINEAR:
283 retval |= R300_TX_MAG_FILTER_LINEAR;
284 break;
285 case PIPE_TEX_FILTER_ANISO:
286 retval |= R300_TX_MAG_FILTER_ANISO;
287 break;
288 default:
289 debug_printf("r300: Unknown texture filter %d\n", mag);
290 assert(0);
291 break;
292 }
293 switch (mip) {
294 case PIPE_TEX_MIPFILTER_NONE:
295 retval |= R300_TX_MIN_FILTER_MIP_NONE;
296 break;
297 case PIPE_TEX_MIPFILTER_NEAREST:
298 retval |= R300_TX_MIN_FILTER_MIP_NEAREST;
299 break;
300 case PIPE_TEX_MIPFILTER_LINEAR:
301 retval |= R300_TX_MIN_FILTER_MIP_LINEAR;
302 break;
303 default:
304 debug_printf("r300: Unknown texture filter %d\n", mip);
305 assert(0);
306 break;
307 }
308
309 return retval;
310 }
311
312 static INLINE uint32_t r300_anisotropy(float max_aniso)
313 {
314 if (max_aniso >= 16.0f) {
315 return R300_TX_MAX_ANISO_16_TO_1;
316 } else if (max_aniso >= 8.0f) {
317 return R300_TX_MAX_ANISO_8_TO_1;
318 } else if (max_aniso >= 4.0f) {
319 return R300_TX_MAX_ANISO_4_TO_1;
320 } else if (max_aniso >= 2.0f) {
321 return R300_TX_MAX_ANISO_2_TO_1;
322 } else {
323 return R300_TX_MAX_ANISO_1_TO_1;
324 }
325 }
326
327 /* Buffer formats. */
328
329 /* Colorbuffer formats. This is the unswizzled format of the RB3D block's
330 * output. For the swizzling of the targets, check the shader's format. */
331 static INLINE uint32_t r300_translate_colorformat(enum pipe_format format)
332 {
333 switch (format) {
334 /* 8-bit buffers */
335 case PIPE_FORMAT_I8_UNORM:
336 return R300_COLOR_FORMAT_I8;
337 /* 16-bit buffers */
338 case PIPE_FORMAT_R5G6B5_UNORM:
339 return R300_COLOR_FORMAT_RGB565;
340 case PIPE_FORMAT_A1R5G5B5_UNORM:
341 return R300_COLOR_FORMAT_ARGB1555;
342 case PIPE_FORMAT_A4R4G4B4_UNORM:
343 return R300_COLOR_FORMAT_ARGB4444;
344 /* 32-bit buffers */
345 case PIPE_FORMAT_A8R8G8B8_UNORM:
346 case PIPE_FORMAT_X8R8G8B8_UNORM:
347 case PIPE_FORMAT_R8G8B8A8_UNORM:
348 case PIPE_FORMAT_R8G8B8X8_UNORM:
349 return R300_COLOR_FORMAT_ARGB8888;
350 /* XXX Not in pipe_format
351 case PIPE_FORMAT_A32R32G32B32:
352 return R300_COLOR_FORMAT_ARGB32323232;
353 case PIPE_FORMAT_A16R16G16B16:
354 return R300_COLOR_FORMAT_ARGB16161616;
355 case PIPE_FORMAT_A10R10G10B10_UNORM:
356 return R500_COLOR_FORMAT_ARGB10101010;
357 case PIPE_FORMAT_A2R10G10B10_UNORM:
358 return R500_COLOR_FORMAT_ARGB2101010;
359 case PIPE_FORMAT_I10_UNORM:
360 return R500_COLOR_FORMAT_I10; */
361 default:
362 debug_printf("r300: Implementation error: "
363 "Got unsupported color format %s in %s\n",
364 pf_name(format), __FUNCTION__);
365 assert(0);
366 break;
367 }
368 return 0;
369 }
370
371 /* Depthbuffer and stencilbuffer. Thankfully, we only support two flavors. */
372 static INLINE uint32_t r300_translate_zsformat(enum pipe_format format)
373 {
374 switch (format) {
375 /* 16-bit depth, no stencil */
376 case PIPE_FORMAT_Z16_UNORM:
377 return R300_DEPTHFORMAT_16BIT_INT_Z;
378 /* 24-bit depth, ignored stencil */
379 case PIPE_FORMAT_Z24X8_UNORM:
380 /* 24-bit depth, 8-bit stencil */
381 case PIPE_FORMAT_Z24S8_UNORM:
382 return R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
383 default:
384 debug_printf("r300: Implementation error: "
385 "Got unsupported ZS format %s in %s\n",
386 pf_name(format), __FUNCTION__);
387 assert(0);
388 break;
389 }
390 return 0;
391 }
392
393 /* Shader output formats. This is essentially the swizzle from the shader
394 * to the RB3D block.
395 *
396 * Note that formats are stored from C3 to C0. */
397 static INLINE uint32_t r300_translate_out_fmt(enum pipe_format format)
398 {
399 switch (format) {
400 case PIPE_FORMAT_A8R8G8B8_UNORM:
401 case PIPE_FORMAT_X8R8G8B8_UNORM:
402 /* XXX */
403 case PIPE_FORMAT_Z24S8_UNORM:
404 return R300_US_OUT_FMT_C4_8 |
405 R300_C0_SEL_B | R300_C1_SEL_G |
406 R300_C2_SEL_R | R300_C3_SEL_A;
407 case PIPE_FORMAT_R8G8B8A8_UNORM:
408 case PIPE_FORMAT_R8G8B8X8_UNORM:
409 return R300_US_OUT_FMT_C4_8 |
410 R300_C0_SEL_A | R300_C1_SEL_B |
411 R300_C2_SEL_G | R300_C3_SEL_R;
412 default:
413 debug_printf("r300: Implementation error: "
414 "Got unsupported output format %s in %s\n",
415 pf_name(format), __FUNCTION__);
416 assert(0);
417 return R300_US_OUT_FMT_UNUSED;
418 }
419 return 0;
420 }
421
422 /* Non-CSO state. (For now.) */
423
424 static INLINE uint32_t r300_translate_gb_pipes(int pipe_count)
425 {
426 switch (pipe_count) {
427 case 1:
428 return R300_GB_TILE_PIPE_COUNT_RV300;
429 break;
430 case 2:
431 return R300_GB_TILE_PIPE_COUNT_R300;
432 break;
433 case 3:
434 return R300_GB_TILE_PIPE_COUNT_R420_3P;
435 break;
436 case 4:
437 return R300_GB_TILE_PIPE_COUNT_R420;
438 break;
439 }
440 return 0;
441 }
442
443 /* Utility function to count the number of components in RGBAZS formats.
444 * XXX should go to util or p_format.h */
445 static INLINE unsigned pf_component_count(enum pipe_format format) {
446 unsigned count = 0;
447
448 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_RGB, 0)) {
449 count++;
450 }
451 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_RGB, 1)) {
452 count++;
453 }
454 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_RGB, 2)) {
455 count++;
456 }
457 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_RGB, 3)) {
458 count++;
459 }
460 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 0)) {
461 count++;
462 }
463 if (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_ZS, 1)) {
464 count++;
465 }
466
467 return count;
468 }
469
470 /* Translate pipe_formats into PSC vertex types. */
471 static INLINE uint16_t
472 r300_translate_vertex_data_type(enum pipe_format format) {
473 uint32_t result = 0;
474 const struct util_format_description *desc;
475 unsigned components = pf_component_count(format);
476
477 desc = util_format_description(format);
478
479 if (desc->layout != UTIL_FORMAT_LAYOUT_ARITH &&
480 desc->layout != UTIL_FORMAT_LAYOUT_ARRAY) {
481 debug_printf("r300: Bad format %s in %s:%d\n", pf_name(format),
482 __FUNCTION__, __LINE__);
483 assert(0);
484 }
485
486 switch (desc->channel[0].type) {
487 /* Half-floats, floats, doubles */
488 case UTIL_FORMAT_TYPE_FLOAT:
489 switch (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_RGB, 0)) {
490 case 32:
491 result = R300_DATA_TYPE_FLOAT_1 + (components - 1);
492 break;
493 default:
494 debug_printf("r300: Bad format %s in %s:%d\n",
495 pf_name(format), __FUNCTION__, __LINE__);
496 assert(0);
497 }
498 break;
499 /* Unsigned ints */
500 case UTIL_FORMAT_TYPE_UNSIGNED:
501 /* Signed ints */
502 case UTIL_FORMAT_TYPE_SIGNED:
503 switch (util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_RGB, 0)) {
504 case 8:
505 result = R300_DATA_TYPE_BYTE;
506 break;
507 case 16:
508 if (components > 2) {
509 result = R300_DATA_TYPE_SHORT_4;
510 } else {
511 result = R300_DATA_TYPE_SHORT_2;
512 }
513 break;
514 default:
515 debug_printf("r300: Bad format %s in %s:%d\n",
516 pf_name(format), __FUNCTION__, __LINE__);
517 debug_printf("r300: util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_RGB, 0) == %d\n",
518 util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_RGB, 0));
519 assert(0);
520 }
521 break;
522 default:
523 debug_printf("r300: Bad format %s in %s:%d\n",
524 pf_name(format), __FUNCTION__, __LINE__);
525 assert(0);
526 }
527
528 if (desc->channel[0].type == UTIL_FORMAT_TYPE_SIGNED) {
529 result |= R300_SIGNED;
530 }
531 if (desc->channel[0].normalized) {
532 result |= R300_NORMALIZE;
533 }
534
535 return result;
536 }
537
538 static INLINE uint16_t
539 r300_translate_vertex_data_swizzle(enum pipe_format format) {
540 const struct util_format_description *desc = util_format_description(format);
541
542 assert(format);
543
544 if (desc->layout != UTIL_FORMAT_LAYOUT_ARITH &&
545 desc->layout != UTIL_FORMAT_LAYOUT_ARRAY) {
546 debug_printf("r300: Bad format %s in %s:%d\n",
547 pf_name(format), __FUNCTION__, __LINE__);
548 return 0;
549 }
550
551 return ((desc->swizzle[0] << R300_SWIZZLE_SELECT_X_SHIFT) |
552 (desc->swizzle[1] << R300_SWIZZLE_SELECT_Y_SHIFT) |
553 (desc->swizzle[2] << R300_SWIZZLE_SELECT_Z_SHIFT) |
554 (desc->swizzle[3] << R300_SWIZZLE_SELECT_W_SHIFT) |
555 (0xf << R300_WRITE_ENA_SHIFT));
556 }
557
558 #endif /* R300_STATE_INLINES_H */