2 * Copyright 2009 Joakim Sindholt <opensource@zhasha.com>
3 * Corbin Simpson <MostAwesomeDude@gmail.com>
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:
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
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. */
24 #ifndef R300_STATE_INLINES_H
25 #define R300_STATE_INLINES_H
27 #include "draw/draw_vertex.h"
29 #include "pipe/p_format.h"
31 #include "util/u_format.h"
35 /* Some maths. These should probably find their way to u_math, if needed. */
37 static INLINE
int pack_float_16_6x(float f
) {
38 return ((int)(f
* 6.0) & 0xffff);
43 static INLINE
uint32_t r300_translate_blend_function(int blend_func
)
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
;
53 return R300_COMB_FCN_MIN
;
55 return R300_COMB_FCN_MAX
;
57 debug_printf("r300: Unknown blend function %d\n", blend_func
);
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
)
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 case PIPE_BLENDFACTOR_ZERO
:
85 return R300_BLEND_GL_ZERO
;
86 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
87 return R300_BLEND_GL_ONE_MINUS_SRC_COLOR
;
88 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
89 return R300_BLEND_GL_ONE_MINUS_SRC_ALPHA
;
90 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
91 return R300_BLEND_GL_ONE_MINUS_DST_ALPHA
;
92 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
93 return R300_BLEND_GL_ONE_MINUS_DST_COLOR
;
94 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
95 return R300_BLEND_GL_ONE_MINUS_CONST_COLOR
;
96 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
97 return R300_BLEND_GL_ONE_MINUS_CONST_ALPHA
;
99 case PIPE_BLENDFACTOR_SRC1_COLOR
:
100 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
101 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
102 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
103 debug_printf("r300: Implementation error: "
104 "Bad blend factor %d not supported!\n", blend_fact
);
109 debug_printf("r300: Unknown blend factor %d\n", blend_fact
);
118 static INLINE
uint32_t r300_translate_depth_stencil_function(int zs_func
)
121 case PIPE_FUNC_NEVER
:
122 return R300_ZS_NEVER
;
125 case PIPE_FUNC_EQUAL
:
126 return R300_ZS_EQUAL
;
127 case PIPE_FUNC_LEQUAL
:
128 return R300_ZS_LEQUAL
;
129 case PIPE_FUNC_GREATER
:
130 return R300_ZS_GREATER
;
131 case PIPE_FUNC_NOTEQUAL
:
132 return R300_ZS_NOTEQUAL
;
133 case PIPE_FUNC_GEQUAL
:
134 return R300_ZS_GEQUAL
;
135 case PIPE_FUNC_ALWAYS
:
136 return R300_ZS_ALWAYS
;
138 debug_printf("r300: Unknown depth/stencil function %d\n",
146 static INLINE
uint32_t r300_translate_stencil_op(int s_op
)
149 case PIPE_STENCIL_OP_KEEP
:
151 case PIPE_STENCIL_OP_ZERO
:
153 case PIPE_STENCIL_OP_REPLACE
:
154 return R300_ZS_REPLACE
;
155 case PIPE_STENCIL_OP_INCR
:
157 case PIPE_STENCIL_OP_DECR
:
159 case PIPE_STENCIL_OP_INCR_WRAP
:
160 return R300_ZS_INCR_WRAP
;
161 case PIPE_STENCIL_OP_DECR_WRAP
:
162 return R300_ZS_DECR_WRAP
;
163 case PIPE_STENCIL_OP_INVERT
:
164 return R300_ZS_INVERT
;
166 debug_printf("r300: Unknown stencil op %d", s_op
);
173 static INLINE
uint32_t r300_translate_alpha_function(int alpha_func
)
175 switch (alpha_func
) {
176 case PIPE_FUNC_NEVER
:
177 return R300_FG_ALPHA_FUNC_NEVER
;
179 return R300_FG_ALPHA_FUNC_LESS
;
180 case PIPE_FUNC_EQUAL
:
181 return R300_FG_ALPHA_FUNC_EQUAL
;
182 case PIPE_FUNC_LEQUAL
:
183 return R300_FG_ALPHA_FUNC_LE
;
184 case PIPE_FUNC_GREATER
:
185 return R300_FG_ALPHA_FUNC_GREATER
;
186 case PIPE_FUNC_NOTEQUAL
:
187 return R300_FG_ALPHA_FUNC_NOTEQUAL
;
188 case PIPE_FUNC_GEQUAL
:
189 return R300_FG_ALPHA_FUNC_GE
;
190 case PIPE_FUNC_ALWAYS
:
191 return R300_FG_ALPHA_FUNC_ALWAYS
;
193 debug_printf("r300: Unknown alpha function %d", alpha_func
);
200 static INLINE
uint32_t
201 r300_translate_polygon_mode_front(unsigned mode
) {
204 case PIPE_POLYGON_MODE_FILL
:
205 return R300_GA_POLY_MODE_FRONT_PTYPE_TRI
;
206 case PIPE_POLYGON_MODE_LINE
:
207 return R300_GA_POLY_MODE_FRONT_PTYPE_LINE
;
208 case PIPE_POLYGON_MODE_POINT
:
209 return R300_GA_POLY_MODE_FRONT_PTYPE_POINT
;
212 debug_printf("r300: Bad polygon mode %i in %s\n", mode
,
214 return R300_GA_POLY_MODE_FRONT_PTYPE_TRI
;
218 static INLINE
uint32_t
219 r300_translate_polygon_mode_back(unsigned mode
) {
222 case PIPE_POLYGON_MODE_FILL
:
223 return R300_GA_POLY_MODE_BACK_PTYPE_TRI
;
224 case PIPE_POLYGON_MODE_LINE
:
225 return R300_GA_POLY_MODE_BACK_PTYPE_LINE
;
226 case PIPE_POLYGON_MODE_POINT
:
227 return R300_GA_POLY_MODE_BACK_PTYPE_POINT
;
230 debug_printf("r300: Bad polygon mode %i in %s\n", mode
,
232 return R300_GA_POLY_MODE_BACK_PTYPE_TRI
;
236 /* Texture sampler state. */
238 static INLINE
uint32_t r300_translate_wrap(int wrap
)
241 case PIPE_TEX_WRAP_REPEAT
:
242 return R300_TX_REPEAT
;
243 case PIPE_TEX_WRAP_CLAMP
:
244 return R300_TX_CLAMP
;
245 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
246 return R300_TX_CLAMP_TO_EDGE
;
247 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
248 return R300_TX_CLAMP_TO_BORDER
;
249 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
250 return R300_TX_REPEAT
| R300_TX_MIRRORED
;
251 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
252 return R300_TX_CLAMP
| R300_TX_MIRRORED
;
253 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
254 return R300_TX_CLAMP_TO_EDGE
| R300_TX_MIRRORED
;
255 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
256 return R300_TX_CLAMP_TO_EDGE
| R300_TX_MIRRORED
;
258 debug_printf("r300: Unknown texture wrap %d", wrap
);
264 static INLINE
uint32_t r300_translate_tex_filters(int min
, int mag
, int mip
,
269 retval
|= R300_TX_MIN_FILTER_ANISO
| R300_TX_MAG_FILTER_ANISO
;
272 case PIPE_TEX_FILTER_NEAREST
:
273 retval
|= R300_TX_MIN_FILTER_NEAREST
;
275 case PIPE_TEX_FILTER_LINEAR
:
276 retval
|= R300_TX_MIN_FILTER_LINEAR
;
279 debug_printf("r300: Unknown texture filter %d\n", min
);
284 case PIPE_TEX_FILTER_NEAREST
:
285 retval
|= R300_TX_MAG_FILTER_NEAREST
;
287 case PIPE_TEX_FILTER_LINEAR
:
288 retval
|= R300_TX_MAG_FILTER_LINEAR
;
291 debug_printf("r300: Unknown texture filter %d\n", mag
);
297 case PIPE_TEX_MIPFILTER_NONE
:
298 retval
|= R300_TX_MIN_FILTER_MIP_NONE
;
300 case PIPE_TEX_MIPFILTER_NEAREST
:
301 retval
|= R300_TX_MIN_FILTER_MIP_NEAREST
;
303 case PIPE_TEX_MIPFILTER_LINEAR
:
304 retval
|= R300_TX_MIN_FILTER_MIP_LINEAR
;
307 debug_printf("r300: Unknown texture filter %d\n", mip
);
315 static INLINE
uint32_t r300_anisotropy(unsigned max_aniso
)
317 if (max_aniso
>= 16) {
318 return R300_TX_MAX_ANISO_16_TO_1
;
319 } else if (max_aniso
>= 8) {
320 return R300_TX_MAX_ANISO_8_TO_1
;
321 } else if (max_aniso
>= 4) {
322 return R300_TX_MAX_ANISO_4_TO_1
;
323 } else if (max_aniso
>= 2) {
324 return R300_TX_MAX_ANISO_2_TO_1
;
326 return R300_TX_MAX_ANISO_1_TO_1
;
330 static INLINE
uint32_t r500_anisotropy(unsigned max_aniso
)
337 // Map the range [0, 15] to [0, 63].
338 return R500_TX_MAX_ANISO(MIN2((unsigned)(max_aniso
*4.2001), 63)) |
339 R500_TX_ANISO_HIGH_QUALITY
;;
342 /* Non-CSO state. (For now.) */
344 static INLINE
uint32_t r300_translate_gb_pipes(int pipe_count
)
346 switch (pipe_count
) {
348 return R300_GB_TILE_PIPE_COUNT_RV300
;
351 return R300_GB_TILE_PIPE_COUNT_R300
;
354 return R300_GB_TILE_PIPE_COUNT_R420_3P
;
357 return R300_GB_TILE_PIPE_COUNT_R420
;
363 /* Translate pipe_formats into PSC vertex types. */
364 static INLINE
uint16_t
365 r300_translate_vertex_data_type(enum pipe_format format
) {
367 const struct util_format_description
*desc
;
368 unsigned components
= util_format_get_nr_components(format
);
370 desc
= util_format_description(format
);
372 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
) {
373 debug_printf("r300: Bad format %s in %s:%d\n", util_format_name(format
),
374 __FUNCTION__
, __LINE__
);
378 switch (desc
->channel
[0].type
) {
379 /* Half-floats, floats, doubles */
380 case UTIL_FORMAT_TYPE_FLOAT
:
381 switch (util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_RGB
, 0)) {
383 /* XXX Supported only on RV350 and later. */
384 if (components
> 2) {
385 result
= R300_DATA_TYPE_FLT16_4
;
387 result
= R300_DATA_TYPE_FLT16_2
;
391 result
= R300_DATA_TYPE_FLOAT_1
+ (components
- 1);
394 debug_printf("r300: Bad format %s in %s:%d\n",
395 util_format_name(format
), __FUNCTION__
, __LINE__
);
400 case UTIL_FORMAT_TYPE_UNSIGNED
:
402 case UTIL_FORMAT_TYPE_SIGNED
:
403 switch (util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_RGB
, 0)) {
405 result
= R300_DATA_TYPE_BYTE
;
408 if (components
> 2) {
409 result
= R300_DATA_TYPE_SHORT_4
;
411 result
= R300_DATA_TYPE_SHORT_2
;
415 debug_printf("r300: Bad format %s in %s:%d\n",
416 util_format_name(format
), __FUNCTION__
, __LINE__
);
417 debug_printf("r300: util_format_get_component_bits(format, UTIL_FORMAT_COLORSPACE_RGB, 0) == %d\n",
418 util_format_get_component_bits(format
, UTIL_FORMAT_COLORSPACE_RGB
, 0));
423 debug_printf("r300: Bad format %s in %s:%d\n",
424 util_format_name(format
), __FUNCTION__
, __LINE__
);
428 if (desc
->channel
[0].type
== UTIL_FORMAT_TYPE_SIGNED
) {
429 result
|= R300_SIGNED
;
431 if (desc
->channel
[0].normalized
) {
432 result
|= R300_NORMALIZE
;
438 static INLINE
uint16_t
439 r300_translate_vertex_data_swizzle(enum pipe_format format
) {
440 const struct util_format_description
*desc
= util_format_description(format
);
444 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
) {
445 debug_printf("r300: Bad format %s in %s:%d\n",
446 util_format_name(format
), __FUNCTION__
, __LINE__
);
450 return ((desc
->swizzle
[0] << R300_SWIZZLE_SELECT_X_SHIFT
) |
451 (desc
->swizzle
[1] << R300_SWIZZLE_SELECT_Y_SHIFT
) |
452 (desc
->swizzle
[2] << R300_SWIZZLE_SELECT_Z_SHIFT
) |
453 (desc
->swizzle
[3] << R300_SWIZZLE_SELECT_W_SHIFT
) |
454 (0xf << R300_WRITE_ENA_SHIFT
));
457 #endif /* R300_STATE_INLINES_H */