2 #include "util/u_memory.h"
3 #include "util/u_math.h"
5 #include "pipe/p_context.h"
6 #include "pipe/p_state.h"
7 #include "util/u_inlines.h"
9 #include "brw_context.h"
10 #include "brw_defines.h"
14 /* The brw (and related graphics cores) do not support GL_CLAMP. The
15 * Intel drivers for "other operating systems" implement GL_CLAMP as
16 * GL_CLAMP_TO_EDGE, so the same is done here.
18 static GLuint
translate_wrap_mode( unsigned wrap
)
21 case PIPE_TEX_WRAP_REPEAT
:
22 return BRW_TEXCOORDMODE_WRAP
;
24 case PIPE_TEX_WRAP_CLAMP
:
25 case PIPE_TEX_WRAP_CLAMP_TO_EDGE
:
26 return BRW_TEXCOORDMODE_CLAMP
;
28 case PIPE_TEX_WRAP_CLAMP_TO_BORDER
:
29 return BRW_TEXCOORDMODE_CLAMP_BORDER
;
31 case PIPE_TEX_WRAP_MIRROR_REPEAT
:
32 return BRW_TEXCOORDMODE_MIRROR
;
34 case PIPE_TEX_WRAP_MIRROR_CLAMP
:
35 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
:
36 case PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER
:
37 return BRW_TEXCOORDMODE_MIRROR_ONCE
;
40 return BRW_TEXCOORDMODE_WRAP
;
44 static GLuint
translate_img_filter( unsigned filter
)
47 case PIPE_TEX_FILTER_NEAREST
:
48 return BRW_MAPFILTER_NEAREST
;
49 case PIPE_TEX_FILTER_LINEAR
:
50 return BRW_MAPFILTER_LINEAR
;
53 return BRW_MAPFILTER_NEAREST
;
57 static GLuint
translate_mip_filter( unsigned filter
)
60 case PIPE_TEX_MIPFILTER_NONE
:
61 return BRW_MIPFILTER_NONE
;
62 case PIPE_TEX_MIPFILTER_NEAREST
:
63 return BRW_MIPFILTER_NEAREST
;
64 case PIPE_TEX_MIPFILTER_LINEAR
:
65 return BRW_MIPFILTER_LINEAR
;
68 return BRW_MIPFILTER_NONE
;
72 /* XXX: not sure why there are special translations for the shadow tex
73 * compare functions. In particular ALWAYS is translated to NEVER.
74 * Is this a hardware issue? Does i965 really suffer from this?
76 static GLuint
translate_shadow_compare_func( unsigned func
)
80 return BRW_COMPAREFUNCTION_ALWAYS
;
82 return BRW_COMPAREFUNCTION_LEQUAL
;
83 case PIPE_FUNC_LEQUAL
:
84 return BRW_COMPAREFUNCTION_LESS
;
85 case PIPE_FUNC_GREATER
:
86 return BRW_COMPAREFUNCTION_GEQUAL
;
87 case PIPE_FUNC_GEQUAL
:
88 return BRW_COMPAREFUNCTION_GREATER
;
89 case PIPE_FUNC_NOTEQUAL
:
90 return BRW_COMPAREFUNCTION_EQUAL
;
92 return BRW_COMPAREFUNCTION_NOTEQUAL
;
93 case PIPE_FUNC_ALWAYS
:
94 return BRW_COMPAREFUNCTION_NEVER
;
97 return BRW_COMPAREFUNCTION_NEVER
;
105 brw_create_sampler_state( struct pipe_context
*pipe
,
106 const struct pipe_sampler_state
*template )
108 struct brw_sampler
*sampler
= CALLOC_STRUCT(brw_sampler
);
110 sampler
->ss0
.min_filter
= translate_img_filter( template->min_img_filter
);
111 sampler
->ss0
.mag_filter
= translate_img_filter( template->mag_img_filter
);
112 sampler
->ss0
.mip_filter
= translate_mip_filter( template->min_mip_filter
);
115 /* XXX: anisotropy logic slightly changed:
117 if (template->max_anisotropy
> 1.0) {
118 sampler
->ss0
.min_filter
= BRW_MAPFILTER_ANISOTROPIC
;
119 sampler
->ss0
.mag_filter
= BRW_MAPFILTER_ANISOTROPIC
;
121 if (template->max_anisotropy
> 2.0) {
122 sampler
->ss3
.max_aniso
= MIN2((template->max_anisotropy
- 2) / 2,
127 sampler
->ss1
.r_wrap_mode
= translate_wrap_mode(template->wrap_r
);
128 sampler
->ss1
.s_wrap_mode
= translate_wrap_mode(template->wrap_s
);
129 sampler
->ss1
.t_wrap_mode
= translate_wrap_mode(template->wrap_t
);
133 sampler
->ss0
.lod_bias
=
134 util_signed_fixed(CLAMP(template->lod_bias
, -16, 15), 6);
137 sampler
->ss0
.lod_preclamp
= 1; /* OpenGL mode */
138 sampler
->ss0
.default_color_mode
= 0; /* OpenGL/DX10 mode */
140 /* Set shadow function:
142 if (template->compare_mode
== PIPE_TEX_COMPARE_R_TO_TEXTURE
) {
144 /* Shadowing is "enabled" by emitting a particular sampler
145 * message (sample_c). So need to recompile WM program when
146 * shadow comparison is enabled on each/any texture unit.
148 sampler
->ss0
.shadow_function
=
149 translate_shadow_compare_func(template->compare_func
);
152 /* Set BaseMipLevel, MaxLOD, MinLOD:
154 sampler
->ss0
.base_level
=
155 util_unsigned_fixed(0, 1);
157 sampler
->ss1
.max_lod
=
158 util_unsigned_fixed(CLAMP(template->max_lod
, 0, 13), 6);
160 sampler
->ss1
.min_lod
=
161 util_unsigned_fixed(CLAMP(template->min_lod
, 0, 13), 6);
163 return (void *)sampler
;
166 static void brw_bind_sampler_state(struct pipe_context
*pipe
,
167 unsigned num
, void **sampler
)
169 struct brw_context
*brw
= brw_context(pipe
);
172 for (i
= 0; i
< num
; i
++)
173 brw
->curr
.sampler
[i
] = sampler
[i
];
175 for (i
= num
; i
< brw
->curr
.num_samplers
; i
++)
176 brw
->curr
.sampler
[i
] = NULL
;
178 brw
->curr
.num_samplers
= num
;
179 brw
->state
.dirty
.mesa
|= PIPE_NEW_SAMPLERS
;
182 static void brw_delete_sampler_state(struct pipe_context
*pipe
,
188 static void brw_set_sampler_textures(struct pipe_context
*pipe
,
190 struct pipe_texture
**texture
)
192 struct brw_context
*brw
= brw_context(pipe
);
195 for (i
= 0; i
< num
; i
++)
196 pipe_texture_reference(&brw
->curr
.texture
[i
], texture
[i
]);
198 for (i
= num
; i
< brw
->curr
.num_textures
; i
++)
199 pipe_texture_reference(&brw
->curr
.texture
[i
], NULL
);
201 brw
->curr
.num_textures
= num
;
202 brw
->state
.dirty
.mesa
|= PIPE_NEW_BOUND_TEXTURES
;
205 static void brw_set_vertex_sampler_textures(struct pipe_context
*pipe
,
207 struct pipe_texture
**texture
)
211 static void brw_bind_vertex_sampler_state(struct pipe_context
*pipe
,
212 unsigned num
, void **sampler
)
217 void brw_pipe_sampler_init( struct brw_context
*brw
)
219 brw
->base
.create_sampler_state
= brw_create_sampler_state
;
220 brw
->base
.delete_sampler_state
= brw_delete_sampler_state
;
222 brw
->base
.set_fragment_sampler_textures
= brw_set_sampler_textures
;
223 brw
->base
.bind_fragment_sampler_states
= brw_bind_sampler_state
;
225 brw
->base
.set_vertex_sampler_textures
= brw_set_vertex_sampler_textures
;
226 brw
->base
.bind_vertex_sampler_states
= brw_bind_vertex_sampler_state
;
229 void brw_pipe_sampler_cleanup( struct brw_context
*brw
)