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"
37 /* Some maths. These should probably find their way to u_math, if needed. */
39 static INLINE
int pack_float_16_6x(float f
) {
40 return ((int)(f
* 6.0) & 0xffff);
45 static INLINE
uint32_t r300_translate_blend_function(int blend_func
)
49 return R300_COMB_FCN_ADD_CLAMP
;
50 case PIPE_BLEND_SUBTRACT
:
51 return R300_COMB_FCN_SUB_CLAMP
;
52 case PIPE_BLEND_REVERSE_SUBTRACT
:
53 return R300_COMB_FCN_RSUB_CLAMP
;
55 return R300_COMB_FCN_MIN
;
57 return R300_COMB_FCN_MAX
;
59 fprintf(stderr
, "r300: Unknown blend function %d\n", blend_func
);
66 /* XXX we can also offer the D3D versions of some of these... */
67 static INLINE
uint32_t r300_translate_blend_factor(int blend_fact
)
70 case PIPE_BLENDFACTOR_ONE
:
71 return R300_BLEND_GL_ONE
;
72 case PIPE_BLENDFACTOR_SRC_COLOR
:
73 return R300_BLEND_GL_SRC_COLOR
;
74 case PIPE_BLENDFACTOR_SRC_ALPHA
:
75 return R300_BLEND_GL_SRC_ALPHA
;
76 case PIPE_BLENDFACTOR_DST_ALPHA
:
77 return R300_BLEND_GL_DST_ALPHA
;
78 case PIPE_BLENDFACTOR_DST_COLOR
:
79 return R300_BLEND_GL_DST_COLOR
;
80 case PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
:
81 return R300_BLEND_GL_SRC_ALPHA_SATURATE
;
82 case PIPE_BLENDFACTOR_CONST_COLOR
:
83 return R300_BLEND_GL_CONST_COLOR
;
84 case PIPE_BLENDFACTOR_CONST_ALPHA
:
85 return R300_BLEND_GL_CONST_ALPHA
;
86 case PIPE_BLENDFACTOR_ZERO
:
87 return R300_BLEND_GL_ZERO
;
88 case PIPE_BLENDFACTOR_INV_SRC_COLOR
:
89 return R300_BLEND_GL_ONE_MINUS_SRC_COLOR
;
90 case PIPE_BLENDFACTOR_INV_SRC_ALPHA
:
91 return R300_BLEND_GL_ONE_MINUS_SRC_ALPHA
;
92 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
93 return R300_BLEND_GL_ONE_MINUS_DST_ALPHA
;
94 case PIPE_BLENDFACTOR_INV_DST_COLOR
:
95 return R300_BLEND_GL_ONE_MINUS_DST_COLOR
;
96 case PIPE_BLENDFACTOR_INV_CONST_COLOR
:
97 return R300_BLEND_GL_ONE_MINUS_CONST_COLOR
;
98 case PIPE_BLENDFACTOR_INV_CONST_ALPHA
:
99 return R300_BLEND_GL_ONE_MINUS_CONST_ALPHA
;
101 case PIPE_BLENDFACTOR_SRC1_COLOR
:
102 case PIPE_BLENDFACTOR_SRC1_ALPHA
:
103 case PIPE_BLENDFACTOR_INV_SRC1_COLOR
:
104 case PIPE_BLENDFACTOR_INV_SRC1_ALPHA
:
105 fprintf(stderr
, "r300: Implementation error: "
106 "Bad blend factor %d not supported!\n", blend_fact
);
111 fprintf(stderr
, "r300: Unknown blend factor %d\n", blend_fact
);
120 static INLINE
uint32_t r300_translate_depth_stencil_function(int zs_func
)
123 case PIPE_FUNC_NEVER
:
124 return R300_ZS_NEVER
;
127 case PIPE_FUNC_EQUAL
:
128 return R300_ZS_EQUAL
;
129 case PIPE_FUNC_LEQUAL
:
130 return R300_ZS_LEQUAL
;
131 case PIPE_FUNC_GREATER
:
132 return R300_ZS_GREATER
;
133 case PIPE_FUNC_NOTEQUAL
:
134 return R300_ZS_NOTEQUAL
;
135 case PIPE_FUNC_GEQUAL
:
136 return R300_ZS_GEQUAL
;
137 case PIPE_FUNC_ALWAYS
:
138 return R300_ZS_ALWAYS
;
140 fprintf(stderr
, "r300: Unknown depth/stencil function %d\n",
148 static INLINE
uint32_t r300_translate_stencil_op(int s_op
)
151 case PIPE_STENCIL_OP_KEEP
:
153 case PIPE_STENCIL_OP_ZERO
:
155 case PIPE_STENCIL_OP_REPLACE
:
156 return R300_ZS_REPLACE
;
157 case PIPE_STENCIL_OP_INCR
:
159 case PIPE_STENCIL_OP_DECR
:
161 case PIPE_STENCIL_OP_INCR_WRAP
:
162 return R300_ZS_INCR_WRAP
;
163 case PIPE_STENCIL_OP_DECR_WRAP
:
164 return R300_ZS_DECR_WRAP
;
165 case PIPE_STENCIL_OP_INVERT
:
166 return R300_ZS_INVERT
;
168 fprintf(stderr
, "r300: Unknown stencil op %d", s_op
);
175 static INLINE
uint32_t r300_translate_alpha_function(int alpha_func
)
177 switch (alpha_func
) {
178 case PIPE_FUNC_NEVER
:
179 return R300_FG_ALPHA_FUNC_NEVER
;
181 return R300_FG_ALPHA_FUNC_LESS
;
182 case PIPE_FUNC_EQUAL
:
183 return R300_FG_ALPHA_FUNC_EQUAL
;
184 case PIPE_FUNC_LEQUAL
:
185 return R300_FG_ALPHA_FUNC_LE
;
186 case PIPE_FUNC_GREATER
:
187 return R300_FG_ALPHA_FUNC_GREATER
;
188 case PIPE_FUNC_NOTEQUAL
:
189 return R300_FG_ALPHA_FUNC_NOTEQUAL
;
190 case PIPE_FUNC_GEQUAL
:
191 return R300_FG_ALPHA_FUNC_GE
;
192 case PIPE_FUNC_ALWAYS
:
193 return R300_FG_ALPHA_FUNC_ALWAYS
;
195 fprintf(stderr
, "r300: Unknown alpha function %d", alpha_func
);
202 static INLINE
uint32_t
203 r300_translate_polygon_mode_front(unsigned mode
) {
206 case PIPE_POLYGON_MODE_FILL
:
207 return R300_GA_POLY_MODE_FRONT_PTYPE_TRI
;
208 case PIPE_POLYGON_MODE_LINE
:
209 return R300_GA_POLY_MODE_FRONT_PTYPE_LINE
;
210 case PIPE_POLYGON_MODE_POINT
:
211 return R300_GA_POLY_MODE_FRONT_PTYPE_POINT
;
214 fprintf(stderr
, "r300: Bad polygon mode %i in %s\n", mode
,
216 return R300_GA_POLY_MODE_FRONT_PTYPE_TRI
;
220 static INLINE
uint32_t
221 r300_translate_polygon_mode_back(unsigned mode
) {
224 case PIPE_POLYGON_MODE_FILL
:
225 return R300_GA_POLY_MODE_BACK_PTYPE_TRI
;
226 case PIPE_POLYGON_MODE_LINE
:
227 return R300_GA_POLY_MODE_BACK_PTYPE_LINE
;
228 case PIPE_POLYGON_MODE_POINT
:
229 return R300_GA_POLY_MODE_BACK_PTYPE_POINT
;
232 fprintf(stderr
, "r300: Bad polygon mode %i in %s\n", mode
,
234 return R300_GA_POLY_MODE_BACK_PTYPE_TRI
;
238 /* Texture sampler state. */
240 static INLINE
uint32_t r300_translate_wrap(int wrap
)
243 case PIPE_TEX_WRAP_REPEAT
:
244 return R300_TX_REPEAT
;
245 case PIPE_TEX_WRAP_CLAMP
:
246 return R300_TX_CLAMP
;
247 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
248 return R300_TX_CLAMP_TO_EDGE
;
249 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
250 return R300_TX_CLAMP_TO_BORDER
;
251 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
252 return R300_TX_REPEAT
| R300_TX_MIRRORED
;
253 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
254 return R300_TX_CLAMP
| R300_TX_MIRRORED
;
255 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
256 return R300_TX_CLAMP_TO_EDGE
| R300_TX_MIRRORED
;
257 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
258 return R300_TX_CLAMP_TO_BORDER
| R300_TX_MIRRORED
;
260 fprintf(stderr
, "r300: Unknown texture wrap %d", wrap
);
266 static INLINE
uint32_t r300_translate_tex_filters(int min
, int mag
, int mip
,
271 retval
|= R300_TX_MIN_FILTER_ANISO
| R300_TX_MAG_FILTER_ANISO
;
274 case PIPE_TEX_FILTER_NEAREST
:
275 retval
|= R300_TX_MIN_FILTER_NEAREST
;
277 case PIPE_TEX_FILTER_LINEAR
:
278 retval
|= R300_TX_MIN_FILTER_LINEAR
;
281 fprintf(stderr
, "r300: Unknown texture filter %d\n", min
);
286 case PIPE_TEX_FILTER_NEAREST
:
287 retval
|= R300_TX_MAG_FILTER_NEAREST
;
289 case PIPE_TEX_FILTER_LINEAR
:
290 retval
|= R300_TX_MAG_FILTER_LINEAR
;
293 fprintf(stderr
, "r300: Unknown texture filter %d\n", mag
);
299 case PIPE_TEX_MIPFILTER_NONE
:
300 retval
|= R300_TX_MIN_FILTER_MIP_NONE
;
302 case PIPE_TEX_MIPFILTER_NEAREST
:
303 retval
|= R300_TX_MIN_FILTER_MIP_NEAREST
;
305 case PIPE_TEX_MIPFILTER_LINEAR
:
306 retval
|= R300_TX_MIN_FILTER_MIP_LINEAR
;
309 fprintf(stderr
, "r300: Unknown texture filter %d\n", mip
);
317 static INLINE
uint32_t r300_anisotropy(unsigned max_aniso
)
319 if (max_aniso
>= 16) {
320 return R300_TX_MAX_ANISO_16_TO_1
;
321 } else if (max_aniso
>= 8) {
322 return R300_TX_MAX_ANISO_8_TO_1
;
323 } else if (max_aniso
>= 4) {
324 return R300_TX_MAX_ANISO_4_TO_1
;
325 } else if (max_aniso
>= 2) {
326 return R300_TX_MAX_ANISO_2_TO_1
;
328 return R300_TX_MAX_ANISO_1_TO_1
;
332 static INLINE
uint32_t r500_anisotropy(unsigned max_aniso
)
339 // Map the range [0, 15] to [0, 63].
340 return R500_TX_MAX_ANISO(MIN2((unsigned)(max_aniso
*4.2001), 63)) |
341 R500_TX_ANISO_HIGH_QUALITY
;
344 /* Non-CSO state. (For now.) */
346 static INLINE
uint32_t r300_translate_gb_pipes(int pipe_count
)
348 switch (pipe_count
) {
350 return R300_GB_TILE_PIPE_COUNT_RV300
;
352 return R300_GB_TILE_PIPE_COUNT_R300
;
354 return R300_GB_TILE_PIPE_COUNT_R420_3P
;
356 return R300_GB_TILE_PIPE_COUNT_R420
;
362 /* Translate pipe_formats into PSC vertex types. */
363 static INLINE
uint16_t
364 r300_translate_vertex_data_type(enum pipe_format format
) {
366 const struct util_format_description
*desc
;
368 desc
= util_format_description(format
);
370 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
) {
371 return R300_INVALID_FORMAT
;
374 switch (desc
->channel
[0].type
) {
375 /* Half-floats, floats, doubles */
376 case UTIL_FORMAT_TYPE_FLOAT
:
377 switch (desc
->channel
[0].size
) {
379 /* Supported only on RV350 and later. */
380 if (desc
->nr_channels
> 2) {
381 result
= R300_DATA_TYPE_FLT16_4
;
383 result
= R300_DATA_TYPE_FLT16_2
;
387 result
= R300_DATA_TYPE_FLOAT_1
+ (desc
->nr_channels
- 1);
390 return R300_INVALID_FORMAT
;
394 case UTIL_FORMAT_TYPE_UNSIGNED
:
396 case UTIL_FORMAT_TYPE_SIGNED
:
397 switch (desc
->channel
[0].size
) {
399 result
= R300_DATA_TYPE_BYTE
;
402 if (desc
->nr_channels
> 2) {
403 result
= R300_DATA_TYPE_SHORT_4
;
405 result
= R300_DATA_TYPE_SHORT_2
;
409 return R300_INVALID_FORMAT
;
413 return R300_INVALID_FORMAT
;
416 if (desc
->channel
[0].type
== UTIL_FORMAT_TYPE_SIGNED
) {
417 result
|= R300_SIGNED
;
419 if (desc
->channel
[0].normalized
) {
420 result
|= R300_NORMALIZE
;
426 static INLINE
uint16_t
427 r300_translate_vertex_data_swizzle(enum pipe_format format
) {
428 const struct util_format_description
*desc
= util_format_description(format
);
429 unsigned i
, swizzle
= 0;
433 if (desc
->layout
!= UTIL_FORMAT_LAYOUT_PLAIN
) {
434 fprintf(stderr
, "r300: Bad format %s in %s:%d\n",
435 util_format_short_name(format
), __FUNCTION__
, __LINE__
);
439 for (i
= 0; i
< desc
->nr_channels
; i
++) {
441 MIN2(desc
->swizzle
[i
], R300_SWIZZLE_SELECT_FP_ONE
) << (3*i
);
443 /* Set (0,0,0,1) in unused components. */
445 swizzle
|= R300_SWIZZLE_SELECT_FP_ZERO
<< (3*i
);
448 swizzle
|= R300_SWIZZLE_SELECT_FP_ONE
<< (3*i
);
451 return swizzle
| (0xf << R300_WRITE_ENA_SHIFT
);
454 #endif /* R300_STATE_INLINES_H */