13464ebd5Sriastradh/*
23464ebd5Sriastradh * Mesa 3-D graphics library
33464ebd5Sriastradh *
43464ebd5Sriastradh * Copyright (C) 2011  VMware, Inc.  All Rights Reserved.
53464ebd5Sriastradh *
63464ebd5Sriastradh * Permission is hereby granted, free of charge, to any person obtaining a
73464ebd5Sriastradh * copy of this software and associated documentation files (the "Software"),
83464ebd5Sriastradh * to deal in the Software without restriction, including without limitation
93464ebd5Sriastradh * the rights to use, copy, modify, merge, publish, distribute, sublicense,
103464ebd5Sriastradh * and/or sell copies of the Software, and to permit persons to whom the
113464ebd5Sriastradh * Software is furnished to do so, subject to the following conditions:
123464ebd5Sriastradh *
133464ebd5Sriastradh * The above copyright notice and this permission notice shall be included
143464ebd5Sriastradh * in all copies or substantial portions of the Software.
153464ebd5Sriastradh *
163464ebd5Sriastradh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
173464ebd5Sriastradh * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
183464ebd5Sriastradh * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19af69d88dSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20af69d88dSmrg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21af69d88dSmrg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22af69d88dSmrg * OTHER DEALINGS IN THE SOFTWARE.
233464ebd5Sriastradh */
243464ebd5Sriastradh
253464ebd5Sriastradh
263464ebd5Sriastradh
273464ebd5Sriastradh#ifndef SAMPLEROBJ_H
283464ebd5Sriastradh#define SAMPLEROBJ_H
293464ebd5Sriastradh
3001e04c3fSmrg#include "mtypes.h"
3101e04c3fSmrg
3201e04c3fSmrg#ifdef __cplusplus
3301e04c3fSmrgextern "C" {
3401e04c3fSmrg#endif
3501e04c3fSmrg
363464ebd5Sriastradhstruct dd_function_table;
373464ebd5Sriastradh
38af69d88dSmrgstatic inline struct gl_sampler_object *
393464ebd5Sriastradh_mesa_get_samplerobj(struct gl_context *ctx, GLuint unit)
403464ebd5Sriastradh{
413464ebd5Sriastradh   if (ctx->Texture.Unit[unit].Sampler)
423464ebd5Sriastradh      return ctx->Texture.Unit[unit].Sampler;
43af69d88dSmrg   else if (ctx->Texture.Unit[unit]._Current)
443464ebd5Sriastradh      return &ctx->Texture.Unit[unit]._Current->Sampler;
45af69d88dSmrg   else
46af69d88dSmrg      return NULL;
47af69d88dSmrg}
48af69d88dSmrg
49af69d88dSmrg
50af69d88dSmrg/** Does the given filter state do mipmap filtering? */
51af69d88dSmrgstatic inline GLboolean
52af69d88dSmrg_mesa_is_mipmap_filter(const struct gl_sampler_object *samp)
53af69d88dSmrg{
547ec681f3Smrg   return samp->Attrib.MinFilter != GL_NEAREST && samp->Attrib.MinFilter != GL_LINEAR;
553464ebd5Sriastradh}
563464ebd5Sriastradh
57af69d88dSmrg
583464ebd5Sriastradhextern void
59af69d88dSmrg_mesa_reference_sampler_object_(struct gl_context *ctx,
60af69d88dSmrg                                struct gl_sampler_object **ptr,
61af69d88dSmrg                                struct gl_sampler_object *samp);
62af69d88dSmrg
63af69d88dSmrgstatic inline void
643464ebd5Sriastradh_mesa_reference_sampler_object(struct gl_context *ctx,
653464ebd5Sriastradh                               struct gl_sampler_object **ptr,
66af69d88dSmrg                               struct gl_sampler_object *samp)
67af69d88dSmrg{
68af69d88dSmrg   if (*ptr != samp)
69af69d88dSmrg      _mesa_reference_sampler_object_(ctx, ptr, samp);
70af69d88dSmrg}
713464ebd5Sriastradh
72af69d88dSmrgextern struct gl_sampler_object *
73af69d88dSmrg_mesa_lookup_samplerobj(struct gl_context *ctx, GLuint name);
743464ebd5Sriastradh
753464ebd5Sriastradhextern struct gl_sampler_object *
763464ebd5Sriastradh_mesa_new_sampler_object(struct gl_context *ctx, GLuint name);
773464ebd5Sriastradh
783464ebd5Sriastradhextern void
793464ebd5Sriastradh_mesa_init_sampler_object_functions(struct dd_function_table *driver);
803464ebd5Sriastradh
8101e04c3fSmrgextern void
8201e04c3fSmrg_mesa_set_sampler_wrap(struct gl_context *ctx, struct gl_sampler_object *samp,
8301e04c3fSmrg                       GLenum s, GLenum t, GLenum r);
8401e04c3fSmrg
8501e04c3fSmrgextern void
8601e04c3fSmrg_mesa_set_sampler_filters(struct gl_context *ctx,
8701e04c3fSmrg                          struct gl_sampler_object *samp,
8801e04c3fSmrg                          GLenum min_filter, GLenum mag_filter);
8901e04c3fSmrg
9001e04c3fSmrgextern void
9101e04c3fSmrg_mesa_set_sampler_srgb_decode(struct gl_context *ctx,
9201e04c3fSmrg                              struct gl_sampler_object *samp, GLenum param);
9301e04c3fSmrg
9401e04c3fSmrgextern void
9501e04c3fSmrg_mesa_bind_sampler(struct gl_context *ctx, GLuint unit,
9601e04c3fSmrg                   struct gl_sampler_object *sampObj);
9701e04c3fSmrg
9801e04c3fSmrgvoid GLAPIENTRY
9901e04c3fSmrg_mesa_GenSamplers_no_error(GLsizei count, GLuint *samplers);
10001e04c3fSmrg
101af69d88dSmrgvoid GLAPIENTRY
102af69d88dSmrg_mesa_GenSamplers(GLsizei count, GLuint *samplers);
10301e04c3fSmrg
10401e04c3fSmrgvoid GLAPIENTRY
10501e04c3fSmrg_mesa_CreateSamplers_no_error(GLsizei count, GLuint *samplers);
10601e04c3fSmrg
10701e04c3fSmrgvoid GLAPIENTRY
10801e04c3fSmrg_mesa_CreateSamplers(GLsizei count, GLuint *samplers);
10901e04c3fSmrg
11001e04c3fSmrgvoid GLAPIENTRY
11101e04c3fSmrg_mesa_DeleteSamplers_no_error(GLsizei count, const GLuint *samplers);
11201e04c3fSmrg
113af69d88dSmrgvoid GLAPIENTRY
114af69d88dSmrg_mesa_DeleteSamplers(GLsizei count, const GLuint *samplers);
11501e04c3fSmrg
116af69d88dSmrgGLboolean GLAPIENTRY
117af69d88dSmrg_mesa_IsSampler(GLuint sampler);
11801e04c3fSmrg
11901e04c3fSmrgvoid GLAPIENTRY
12001e04c3fSmrg_mesa_BindSampler_no_error(GLuint unit, GLuint sampler);
12101e04c3fSmrg
122af69d88dSmrgvoid GLAPIENTRY
123af69d88dSmrg_mesa_BindSampler(GLuint unit, GLuint sampler);
12401e04c3fSmrg
12501e04c3fSmrgvoid GLAPIENTRY
12601e04c3fSmrg_mesa_BindSamplers_no_error(GLuint first, GLsizei count, const GLuint *samplers);
12701e04c3fSmrg
128af69d88dSmrgvoid GLAPIENTRY
129af69d88dSmrg_mesa_BindSamplers(GLuint first, GLsizei count, const GLuint *samplers);
13001e04c3fSmrg
131af69d88dSmrgvoid GLAPIENTRY
132af69d88dSmrg_mesa_SamplerParameteri(GLuint sampler, GLenum pname, GLint param);
133af69d88dSmrgvoid GLAPIENTRY
134af69d88dSmrg_mesa_SamplerParameterf(GLuint sampler, GLenum pname, GLfloat param);
135af69d88dSmrgvoid GLAPIENTRY
136af69d88dSmrg_mesa_SamplerParameteriv(GLuint sampler, GLenum pname, const GLint *params);
137af69d88dSmrgvoid GLAPIENTRY
138af69d88dSmrg_mesa_SamplerParameterfv(GLuint sampler, GLenum pname, const GLfloat *params);
139af69d88dSmrgvoid GLAPIENTRY
140af69d88dSmrg_mesa_SamplerParameterIiv(GLuint sampler, GLenum pname, const GLint *params);
141af69d88dSmrgvoid GLAPIENTRY
142af69d88dSmrg_mesa_SamplerParameterIuiv(GLuint sampler, GLenum pname, const GLuint *params);
143af69d88dSmrgvoid GLAPIENTRY
144af69d88dSmrg_mesa_GetSamplerParameteriv(GLuint sampler, GLenum pname, GLint *params);
145af69d88dSmrgvoid GLAPIENTRY
146af69d88dSmrg_mesa_GetSamplerParameterfv(GLuint sampler, GLenum pname, GLfloat *params);
147af69d88dSmrgvoid GLAPIENTRY
148af69d88dSmrg_mesa_GetSamplerParameterIiv(GLuint sampler, GLenum pname, GLint *params);
149af69d88dSmrgvoid GLAPIENTRY
150af69d88dSmrg_mesa_GetSamplerParameterIuiv(GLuint sampler, GLenum pname, GLuint *params);
1513464ebd5Sriastradh
1527ec681f3Smrgextern const enum pipe_tex_wrap wrap_to_gallium_table[32];
1537ec681f3Smrg
1547ec681f3Smrg/**
1557ec681f3Smrg * Convert GLenum texcoord wrap tokens to pipe tokens.
1567ec681f3Smrg */
1577ec681f3Smrgstatic inline enum pipe_tex_wrap
1587ec681f3Smrgwrap_to_gallium(GLenum wrap)
1597ec681f3Smrg{
1607ec681f3Smrg   return wrap_to_gallium_table[wrap & 0x1f];
1617ec681f3Smrg}
1627ec681f3Smrg
1637ec681f3Smrg
1647ec681f3Smrgstatic inline enum pipe_tex_mipfilter
1657ec681f3Smrgmipfilter_to_gallium(GLenum filter)
1667ec681f3Smrg{
1677ec681f3Smrg   /* Take advantage of how the enums are defined. */
1687ec681f3Smrg   if (filter <= GL_LINEAR)
1697ec681f3Smrg      return PIPE_TEX_MIPFILTER_NONE;
1707ec681f3Smrg   if (filter <= GL_LINEAR_MIPMAP_NEAREST)
1717ec681f3Smrg      return PIPE_TEX_MIPFILTER_NEAREST;
1727ec681f3Smrg
1737ec681f3Smrg   return PIPE_TEX_MIPFILTER_LINEAR;
1747ec681f3Smrg}
1757ec681f3Smrg
1767ec681f3Smrg
1777ec681f3Smrgstatic inline enum pipe_tex_filter
1787ec681f3Smrgfilter_to_gallium(GLenum filter)
1797ec681f3Smrg{
1807ec681f3Smrg   /* Take advantage of how the enums are defined. */
1817ec681f3Smrg   if (filter & 1)
1827ec681f3Smrg      return PIPE_TEX_FILTER_LINEAR;
1837ec681f3Smrg
1847ec681f3Smrg   return PIPE_TEX_FILTER_NEAREST;
1857ec681f3Smrg}
1867ec681f3Smrg
1877ec681f3Smrgstatic inline enum pipe_tex_reduction_mode
1887ec681f3Smrgreduction_to_gallium(GLenum reduction_mode)
1897ec681f3Smrg{
1907ec681f3Smrg   switch (reduction_mode) {
1917ec681f3Smrg   case GL_MIN:
1927ec681f3Smrg      return PIPE_TEX_REDUCTION_MIN;
1937ec681f3Smrg   case GL_MAX:
1947ec681f3Smrg      return PIPE_TEX_REDUCTION_MAX;
1957ec681f3Smrg   case GL_WEIGHTED_AVERAGE_EXT:
1967ec681f3Smrg   default:
1977ec681f3Smrg      return PIPE_TEX_REDUCTION_WEIGHTED_AVERAGE;
1987ec681f3Smrg   }
1997ec681f3Smrg}
2007ec681f3Smrg
2017ec681f3Smrg/**
2027ec681f3Smrg * Convert an OpenGL compare mode to a pipe tokens.
2037ec681f3Smrg */
2047ec681f3Smrgstatic inline enum pipe_compare_func
2057ec681f3Smrgfunc_to_gallium(GLenum func)
2067ec681f3Smrg{
2077ec681f3Smrg   /* Same values, just biased */
2087ec681f3Smrg   STATIC_ASSERT(PIPE_FUNC_NEVER == GL_NEVER - GL_NEVER);
2097ec681f3Smrg   STATIC_ASSERT(PIPE_FUNC_LESS == GL_LESS - GL_NEVER);
2107ec681f3Smrg   STATIC_ASSERT(PIPE_FUNC_EQUAL == GL_EQUAL - GL_NEVER);
2117ec681f3Smrg   STATIC_ASSERT(PIPE_FUNC_LEQUAL == GL_LEQUAL - GL_NEVER);
2127ec681f3Smrg   STATIC_ASSERT(PIPE_FUNC_GREATER == GL_GREATER - GL_NEVER);
2137ec681f3Smrg   STATIC_ASSERT(PIPE_FUNC_NOTEQUAL == GL_NOTEQUAL - GL_NEVER);
2147ec681f3Smrg   STATIC_ASSERT(PIPE_FUNC_GEQUAL == GL_GEQUAL - GL_NEVER);
2157ec681f3Smrg   STATIC_ASSERT(PIPE_FUNC_ALWAYS == GL_ALWAYS - GL_NEVER);
2167ec681f3Smrg   assert(func >= GL_NEVER);
2177ec681f3Smrg   assert(func <= GL_ALWAYS);
2187ec681f3Smrg   return (enum pipe_compare_func)(func - GL_NEVER);
2197ec681f3Smrg}
2207ec681f3Smrg
2217ec681f3Smrgstatic inline void
2227ec681f3Smrg_mesa_update_is_border_color_nonzero(struct gl_sampler_object *samp)
2237ec681f3Smrg{
2247ec681f3Smrg   samp->Attrib.IsBorderColorNonZero = samp->Attrib.state.border_color.ui[0] ||
2257ec681f3Smrg                                       samp->Attrib.state.border_color.ui[1] ||
2267ec681f3Smrg                                       samp->Attrib.state.border_color.ui[2] ||
2277ec681f3Smrg                                       samp->Attrib.state.border_color.ui[3];
2287ec681f3Smrg}
2297ec681f3Smrg
2307ec681f3Smrgstatic inline enum pipe_tex_wrap
2317ec681f3Smrglower_gl_clamp(enum pipe_tex_wrap old_wrap, GLenum wrap, bool clamp_to_border)
2327ec681f3Smrg{
2337ec681f3Smrg   if (wrap == GL_CLAMP)
2347ec681f3Smrg      return clamp_to_border ? PIPE_TEX_WRAP_CLAMP_TO_BORDER :
2357ec681f3Smrg                               PIPE_TEX_WRAP_CLAMP_TO_EDGE;
2367ec681f3Smrg   else if (wrap == GL_MIRROR_CLAMP_EXT)
2377ec681f3Smrg      return clamp_to_border ? PIPE_TEX_WRAP_MIRROR_CLAMP_TO_BORDER :
2387ec681f3Smrg                               PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE;
2397ec681f3Smrg   return old_wrap;
2407ec681f3Smrg}
2417ec681f3Smrg
2427ec681f3Smrgstatic inline void
2437ec681f3Smrg_mesa_lower_gl_clamp(struct gl_context *ctx, struct gl_sampler_object *samp)
2447ec681f3Smrg{
2457ec681f3Smrg   if (ctx->DriverFlags.NewSamplersWithClamp) {
2467ec681f3Smrg      struct pipe_sampler_state *s = &samp->Attrib.state;
2477ec681f3Smrg      bool clamp_to_border = s->min_img_filter != PIPE_TEX_FILTER_NEAREST &&
2487ec681f3Smrg                             s->mag_img_filter != PIPE_TEX_FILTER_NEAREST;
2497ec681f3Smrg
2507ec681f3Smrg      s->wrap_s = lower_gl_clamp((enum pipe_tex_wrap)s->wrap_s,
2517ec681f3Smrg                                 samp->Attrib.WrapS, clamp_to_border);
2527ec681f3Smrg      s->wrap_t = lower_gl_clamp((enum pipe_tex_wrap)s->wrap_t,
2537ec681f3Smrg                                 samp->Attrib.WrapT, clamp_to_border);
2547ec681f3Smrg      s->wrap_r = lower_gl_clamp((enum pipe_tex_wrap)s->wrap_r,
2557ec681f3Smrg                                 samp->Attrib.WrapR, clamp_to_border);
2567ec681f3Smrg   }
2577ec681f3Smrg}
2587ec681f3Smrg
25901e04c3fSmrg#ifdef __cplusplus
26001e04c3fSmrg}
26101e04c3fSmrg#endif
26201e04c3fSmrg
2633464ebd5Sriastradh#endif /* SAMPLEROBJ_H */
264