1*61046927SAndroid Build Coastguard Worker /*
2*61046927SAndroid Build Coastguard Worker * Mesa 3-D graphics library
3*61046927SAndroid Build Coastguard Worker *
4*61046927SAndroid Build Coastguard Worker * Copyright (C) 2011 VMware, Inc. All Rights Reserved.
5*61046927SAndroid Build Coastguard Worker *
6*61046927SAndroid Build Coastguard Worker * Permission is hereby granted, free of charge, to any person obtaining a
7*61046927SAndroid Build Coastguard Worker * copy of this software and associated documentation files (the "Software"),
8*61046927SAndroid Build Coastguard Worker * to deal in the Software without restriction, including without limitation
9*61046927SAndroid Build Coastguard Worker * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10*61046927SAndroid Build Coastguard Worker * and/or sell copies of the Software, and to permit persons to whom the
11*61046927SAndroid Build Coastguard Worker * Software is furnished to do so, subject to the following conditions:
12*61046927SAndroid Build Coastguard Worker *
13*61046927SAndroid Build Coastguard Worker * The above copyright notice and this permission notice shall be included
14*61046927SAndroid Build Coastguard Worker * in all copies or substantial portions of the Software.
15*61046927SAndroid Build Coastguard Worker *
16*61046927SAndroid Build Coastguard Worker * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17*61046927SAndroid Build Coastguard Worker * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*61046927SAndroid Build Coastguard Worker * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19*61046927SAndroid Build Coastguard Worker * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20*61046927SAndroid Build Coastguard Worker * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21*61046927SAndroid Build Coastguard Worker * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22*61046927SAndroid Build Coastguard Worker * OTHER DEALINGS IN THE SOFTWARE.
23*61046927SAndroid Build Coastguard Worker */
24*61046927SAndroid Build Coastguard Worker
25*61046927SAndroid Build Coastguard Worker
26*61046927SAndroid Build Coastguard Worker
27*61046927SAndroid Build Coastguard Worker #ifndef SAMPLEROBJ_H
28*61046927SAndroid Build Coastguard Worker #define SAMPLEROBJ_H
29*61046927SAndroid Build Coastguard Worker
30*61046927SAndroid Build Coastguard Worker #include "mtypes.h"
31*61046927SAndroid Build Coastguard Worker
32*61046927SAndroid Build Coastguard Worker #ifdef __cplusplus
33*61046927SAndroid Build Coastguard Worker extern "C" {
34*61046927SAndroid Build Coastguard Worker #endif
35*61046927SAndroid Build Coastguard Worker
36*61046927SAndroid Build Coastguard Worker struct dd_function_table;
37*61046927SAndroid Build Coastguard Worker
38*61046927SAndroid Build Coastguard Worker static inline struct gl_sampler_object *
_mesa_get_samplerobj(struct gl_context * ctx,GLuint unit)39*61046927SAndroid Build Coastguard Worker _mesa_get_samplerobj(struct gl_context *ctx, GLuint unit)
40*61046927SAndroid Build Coastguard Worker {
41*61046927SAndroid Build Coastguard Worker if (ctx->Texture.Unit[unit].Sampler)
42*61046927SAndroid Build Coastguard Worker return ctx->Texture.Unit[unit].Sampler;
43*61046927SAndroid Build Coastguard Worker else if (ctx->Texture.Unit[unit]._Current)
44*61046927SAndroid Build Coastguard Worker return &ctx->Texture.Unit[unit]._Current->Sampler;
45*61046927SAndroid Build Coastguard Worker else
46*61046927SAndroid Build Coastguard Worker return NULL;
47*61046927SAndroid Build Coastguard Worker }
48*61046927SAndroid Build Coastguard Worker
49*61046927SAndroid Build Coastguard Worker
50*61046927SAndroid Build Coastguard Worker /** Does the given filter state do mipmap filtering? */
51*61046927SAndroid Build Coastguard Worker static inline GLboolean
_mesa_is_mipmap_filter(const struct gl_sampler_object * samp)52*61046927SAndroid Build Coastguard Worker _mesa_is_mipmap_filter(const struct gl_sampler_object *samp)
53*61046927SAndroid Build Coastguard Worker {
54*61046927SAndroid Build Coastguard Worker return samp->Attrib.MinFilter != GL_NEAREST && samp->Attrib.MinFilter != GL_LINEAR;
55*61046927SAndroid Build Coastguard Worker }
56*61046927SAndroid Build Coastguard Worker
57*61046927SAndroid Build Coastguard Worker
58*61046927SAndroid Build Coastguard Worker extern void
59*61046927SAndroid Build Coastguard Worker _mesa_reference_sampler_object_(struct gl_context *ctx,
60*61046927SAndroid Build Coastguard Worker struct gl_sampler_object **ptr,
61*61046927SAndroid Build Coastguard Worker struct gl_sampler_object *samp);
62*61046927SAndroid Build Coastguard Worker
63*61046927SAndroid Build Coastguard Worker static inline void
_mesa_reference_sampler_object(struct gl_context * ctx,struct gl_sampler_object ** ptr,struct gl_sampler_object * samp)64*61046927SAndroid Build Coastguard Worker _mesa_reference_sampler_object(struct gl_context *ctx,
65*61046927SAndroid Build Coastguard Worker struct gl_sampler_object **ptr,
66*61046927SAndroid Build Coastguard Worker struct gl_sampler_object *samp)
67*61046927SAndroid Build Coastguard Worker {
68*61046927SAndroid Build Coastguard Worker if (*ptr != samp)
69*61046927SAndroid Build Coastguard Worker _mesa_reference_sampler_object_(ctx, ptr, samp);
70*61046927SAndroid Build Coastguard Worker }
71*61046927SAndroid Build Coastguard Worker
72*61046927SAndroid Build Coastguard Worker extern struct gl_sampler_object *
73*61046927SAndroid Build Coastguard Worker _mesa_lookup_samplerobj(struct gl_context *ctx, GLuint name);
74*61046927SAndroid Build Coastguard Worker
75*61046927SAndroid Build Coastguard Worker extern void
76*61046927SAndroid Build Coastguard Worker _mesa_bind_sampler(struct gl_context *ctx, GLuint unit,
77*61046927SAndroid Build Coastguard Worker struct gl_sampler_object *sampObj);
78*61046927SAndroid Build Coastguard Worker
79*61046927SAndroid Build Coastguard Worker extern const enum pipe_tex_wrap wrap_to_gallium_table[32];
80*61046927SAndroid Build Coastguard Worker
81*61046927SAndroid Build Coastguard Worker /**
82*61046927SAndroid Build Coastguard Worker * Convert GLenum texcoord wrap tokens to pipe tokens.
83*61046927SAndroid Build Coastguard Worker */
84*61046927SAndroid Build Coastguard Worker static inline enum pipe_tex_wrap
wrap_to_gallium(GLenum wrap)85*61046927SAndroid Build Coastguard Worker wrap_to_gallium(GLenum wrap)
86*61046927SAndroid Build Coastguard Worker {
87*61046927SAndroid Build Coastguard Worker return wrap_to_gallium_table[wrap & 0x1f];
88*61046927SAndroid Build Coastguard Worker }
89*61046927SAndroid Build Coastguard Worker
90*61046927SAndroid Build Coastguard Worker
91*61046927SAndroid Build Coastguard Worker static inline enum pipe_tex_mipfilter
mipfilter_to_gallium(GLenum filter)92*61046927SAndroid Build Coastguard Worker mipfilter_to_gallium(GLenum filter)
93*61046927SAndroid Build Coastguard Worker {
94*61046927SAndroid Build Coastguard Worker /* Take advantage of how the enums are defined. */
95*61046927SAndroid Build Coastguard Worker if (filter <= GL_LINEAR)
96*61046927SAndroid Build Coastguard Worker return PIPE_TEX_MIPFILTER_NONE;
97*61046927SAndroid Build Coastguard Worker if (filter <= GL_LINEAR_MIPMAP_NEAREST)
98*61046927SAndroid Build Coastguard Worker return PIPE_TEX_MIPFILTER_NEAREST;
99*61046927SAndroid Build Coastguard Worker
100*61046927SAndroid Build Coastguard Worker return PIPE_TEX_MIPFILTER_LINEAR;
101*61046927SAndroid Build Coastguard Worker }
102*61046927SAndroid Build Coastguard Worker
103*61046927SAndroid Build Coastguard Worker
104*61046927SAndroid Build Coastguard Worker static inline enum pipe_tex_filter
filter_to_gallium(GLenum filter)105*61046927SAndroid Build Coastguard Worker filter_to_gallium(GLenum filter)
106*61046927SAndroid Build Coastguard Worker {
107*61046927SAndroid Build Coastguard Worker /* Take advantage of how the enums are defined. */
108*61046927SAndroid Build Coastguard Worker if (filter & 1)
109*61046927SAndroid Build Coastguard Worker return PIPE_TEX_FILTER_LINEAR;
110*61046927SAndroid Build Coastguard Worker
111*61046927SAndroid Build Coastguard Worker return PIPE_TEX_FILTER_NEAREST;
112*61046927SAndroid Build Coastguard Worker }
113*61046927SAndroid Build Coastguard Worker
114*61046927SAndroid Build Coastguard Worker static inline enum pipe_tex_reduction_mode
reduction_to_gallium(GLenum reduction_mode)115*61046927SAndroid Build Coastguard Worker reduction_to_gallium(GLenum reduction_mode)
116*61046927SAndroid Build Coastguard Worker {
117*61046927SAndroid Build Coastguard Worker switch (reduction_mode) {
118*61046927SAndroid Build Coastguard Worker case GL_MIN:
119*61046927SAndroid Build Coastguard Worker return PIPE_TEX_REDUCTION_MIN;
120*61046927SAndroid Build Coastguard Worker case GL_MAX:
121*61046927SAndroid Build Coastguard Worker return PIPE_TEX_REDUCTION_MAX;
122*61046927SAndroid Build Coastguard Worker case GL_WEIGHTED_AVERAGE_EXT:
123*61046927SAndroid Build Coastguard Worker default:
124*61046927SAndroid Build Coastguard Worker return PIPE_TEX_REDUCTION_WEIGHTED_AVERAGE;
125*61046927SAndroid Build Coastguard Worker }
126*61046927SAndroid Build Coastguard Worker }
127*61046927SAndroid Build Coastguard Worker
128*61046927SAndroid Build Coastguard Worker /**
129*61046927SAndroid Build Coastguard Worker * Convert an OpenGL compare mode to a pipe tokens.
130*61046927SAndroid Build Coastguard Worker */
131*61046927SAndroid Build Coastguard Worker static inline enum pipe_compare_func
func_to_gallium(GLenum func)132*61046927SAndroid Build Coastguard Worker func_to_gallium(GLenum func)
133*61046927SAndroid Build Coastguard Worker {
134*61046927SAndroid Build Coastguard Worker /* Same values, just biased */
135*61046927SAndroid Build Coastguard Worker STATIC_ASSERT(PIPE_FUNC_NEVER == GL_NEVER - GL_NEVER);
136*61046927SAndroid Build Coastguard Worker STATIC_ASSERT(PIPE_FUNC_LESS == GL_LESS - GL_NEVER);
137*61046927SAndroid Build Coastguard Worker STATIC_ASSERT(PIPE_FUNC_EQUAL == GL_EQUAL - GL_NEVER);
138*61046927SAndroid Build Coastguard Worker STATIC_ASSERT(PIPE_FUNC_LEQUAL == GL_LEQUAL - GL_NEVER);
139*61046927SAndroid Build Coastguard Worker STATIC_ASSERT(PIPE_FUNC_GREATER == GL_GREATER - GL_NEVER);
140*61046927SAndroid Build Coastguard Worker STATIC_ASSERT(PIPE_FUNC_NOTEQUAL == GL_NOTEQUAL - GL_NEVER);
141*61046927SAndroid Build Coastguard Worker STATIC_ASSERT(PIPE_FUNC_GEQUAL == GL_GEQUAL - GL_NEVER);
142*61046927SAndroid Build Coastguard Worker STATIC_ASSERT(PIPE_FUNC_ALWAYS == GL_ALWAYS - GL_NEVER);
143*61046927SAndroid Build Coastguard Worker assert(func >= GL_NEVER);
144*61046927SAndroid Build Coastguard Worker assert(func <= GL_ALWAYS);
145*61046927SAndroid Build Coastguard Worker return (enum pipe_compare_func)(func - GL_NEVER);
146*61046927SAndroid Build Coastguard Worker }
147*61046927SAndroid Build Coastguard Worker
148*61046927SAndroid Build Coastguard Worker static inline void
_mesa_update_is_border_color_nonzero(struct gl_sampler_object * samp)149*61046927SAndroid Build Coastguard Worker _mesa_update_is_border_color_nonzero(struct gl_sampler_object *samp)
150*61046927SAndroid Build Coastguard Worker {
151*61046927SAndroid Build Coastguard Worker samp->Attrib.IsBorderColorNonZero = samp->Attrib.state.border_color.ui[0] ||
152*61046927SAndroid Build Coastguard Worker samp->Attrib.state.border_color.ui[1] ||
153*61046927SAndroid Build Coastguard Worker samp->Attrib.state.border_color.ui[2] ||
154*61046927SAndroid Build Coastguard Worker samp->Attrib.state.border_color.ui[3];
155*61046927SAndroid Build Coastguard Worker }
156*61046927SAndroid Build Coastguard Worker
157*61046927SAndroid Build Coastguard Worker static inline enum pipe_tex_wrap
lower_gl_clamp(enum pipe_tex_wrap old_wrap,GLenum wrap,bool clamp_to_border)158*61046927SAndroid Build Coastguard Worker lower_gl_clamp(enum pipe_tex_wrap old_wrap, GLenum wrap, bool clamp_to_border)
159*61046927SAndroid Build Coastguard Worker {
160*61046927SAndroid Build Coastguard Worker if (wrap == GL_CLAMP)
161*61046927SAndroid Build Coastguard Worker return clamp_to_border ? PIPE_TEX_WRAP_CLAMP_TO_BORDER :
162*61046927SAndroid Build Coastguard Worker PIPE_TEX_WRAP_CLAMP_TO_EDGE;
163*61046927SAndroid Build Coastguard Worker else if (wrap == GL_MIRROR_CLAMP_EXT)
164*61046927SAndroid Build Coastguard Worker return clamp_to_border ? PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER :
165*61046927SAndroid Build Coastguard Worker PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE;
166*61046927SAndroid Build Coastguard Worker return old_wrap;
167*61046927SAndroid Build Coastguard Worker }
168*61046927SAndroid Build Coastguard Worker
169*61046927SAndroid Build Coastguard Worker static inline void
_mesa_lower_gl_clamp(struct gl_context * ctx,struct gl_sampler_object * samp)170*61046927SAndroid Build Coastguard Worker _mesa_lower_gl_clamp(struct gl_context *ctx, struct gl_sampler_object *samp)
171*61046927SAndroid Build Coastguard Worker {
172*61046927SAndroid Build Coastguard Worker if (ctx->DriverFlags.NewSamplersWithClamp) {
173*61046927SAndroid Build Coastguard Worker struct pipe_sampler_state *s = &samp->Attrib.state;
174*61046927SAndroid Build Coastguard Worker bool clamp_to_border = s->min_img_filter != PIPE_TEX_FILTER_NEAREST &&
175*61046927SAndroid Build Coastguard Worker s->mag_img_filter != PIPE_TEX_FILTER_NEAREST;
176*61046927SAndroid Build Coastguard Worker
177*61046927SAndroid Build Coastguard Worker s->wrap_s = lower_gl_clamp((enum pipe_tex_wrap)s->wrap_s,
178*61046927SAndroid Build Coastguard Worker samp->Attrib.WrapS, clamp_to_border);
179*61046927SAndroid Build Coastguard Worker s->wrap_t = lower_gl_clamp((enum pipe_tex_wrap)s->wrap_t,
180*61046927SAndroid Build Coastguard Worker samp->Attrib.WrapT, clamp_to_border);
181*61046927SAndroid Build Coastguard Worker s->wrap_r = lower_gl_clamp((enum pipe_tex_wrap)s->wrap_r,
182*61046927SAndroid Build Coastguard Worker samp->Attrib.WrapR, clamp_to_border);
183*61046927SAndroid Build Coastguard Worker }
184*61046927SAndroid Build Coastguard Worker }
185*61046927SAndroid Build Coastguard Worker
186*61046927SAndroid Build Coastguard Worker static inline GLboolean
is_wrap_gl_clamp(GLint param)187*61046927SAndroid Build Coastguard Worker is_wrap_gl_clamp(GLint param)
188*61046927SAndroid Build Coastguard Worker {
189*61046927SAndroid Build Coastguard Worker return param == GL_CLAMP || param == GL_MIRROR_CLAMP_EXT;
190*61046927SAndroid Build Coastguard Worker }
191*61046927SAndroid Build Coastguard Worker
192*61046927SAndroid Build Coastguard Worker static inline void
update_sampler_gl_clamp(struct gl_context * ctx,struct gl_sampler_object * samp,bool cur_state,bool new_state,gl_sampler_wrap wrap)193*61046927SAndroid Build Coastguard Worker update_sampler_gl_clamp(struct gl_context *ctx, struct gl_sampler_object *samp, bool cur_state, bool new_state, gl_sampler_wrap wrap)
194*61046927SAndroid Build Coastguard Worker {
195*61046927SAndroid Build Coastguard Worker if (cur_state == new_state)
196*61046927SAndroid Build Coastguard Worker return;
197*61046927SAndroid Build Coastguard Worker ctx->NewDriverState |= ctx->DriverFlags.NewSamplersWithClamp;
198*61046927SAndroid Build Coastguard Worker uint8_t old_mask = samp->glclamp_mask;
199*61046927SAndroid Build Coastguard Worker if (new_state)
200*61046927SAndroid Build Coastguard Worker samp->glclamp_mask |= wrap;
201*61046927SAndroid Build Coastguard Worker else
202*61046927SAndroid Build Coastguard Worker samp->glclamp_mask &= ~wrap;
203*61046927SAndroid Build Coastguard Worker
204*61046927SAndroid Build Coastguard Worker if (old_mask && !samp->glclamp_mask)
205*61046927SAndroid Build Coastguard Worker ctx->Texture.NumSamplersWithClamp--;
206*61046927SAndroid Build Coastguard Worker else if (samp->glclamp_mask && !old_mask)
207*61046927SAndroid Build Coastguard Worker ctx->Texture.NumSamplersWithClamp++;
208*61046927SAndroid Build Coastguard Worker }
209*61046927SAndroid Build Coastguard Worker #ifdef __cplusplus
210*61046927SAndroid Build Coastguard Worker }
211*61046927SAndroid Build Coastguard Worker #endif
212*61046927SAndroid Build Coastguard Worker
213*61046927SAndroid Build Coastguard Worker #endif /* SAMPLEROBJ_H */
214