summaryrefslogtreecommitdiffstats
path: root/o3d/core/cross/gles2/sampler_gles2.cc
blob: 502389cf27f1f139ecec6d07896795a64ed89f0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
 * Copyright 2009, Google Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


// This file contains the implementation for SamplerGLES2.

#include "core/cross/gles2/gles2_headers.h"
#include "core/cross/error.h"
#include "core/cross/gles2/renderer_gles2.h"
#include "core/cross/gles2/sampler_gles2.h"

namespace o3d {

SamplerGLES2::SamplerGLES2(ServiceLocator* service_locator)
    : Sampler(service_locator),
      renderer_(static_cast<RendererGLES2*>(
          service_locator->GetService<Renderer>())) {
}

SamplerGLES2::~SamplerGLES2() {
}

namespace {

unsigned int GLAddressMode(Sampler::AddressMode o3d_mode,
                           unsigned int default_mode) {
  unsigned int gl_mode = default_mode;
  switch (o3d_mode) {
    case Sampler::WRAP:
      gl_mode = GL_REPEAT;
      break;
    case Sampler::MIRROR:
      gl_mode = GL_MIRRORED_REPEAT;
      break;
    case Sampler::CLAMP:
      gl_mode = GL_CLAMP_TO_EDGE;
      break;
    case Sampler::BORDER:
      gl_mode = GL_CLAMP_TO_BORDER;
      break;
    default:
      DLOG(ERROR) << "Unknown Address mode " << static_cast<int>(o3d_mode);
      break;
  }
  return gl_mode;
}

unsigned int GLMinFilter(Sampler::FilterType o3d_filter,
                         Sampler::FilterType mip_filter) {
  switch (o3d_filter) {
    case Sampler::NONE:
      return GL_NEAREST;
    case Sampler::POINT:
      if (mip_filter == Sampler::NONE)
        return GL_NEAREST;
      else if (mip_filter == Sampler::POINT)
        return GL_NEAREST_MIPMAP_NEAREST;
      else if (mip_filter == Sampler::LINEAR)
        return GL_NEAREST_MIPMAP_LINEAR;
      else if (mip_filter == Sampler::ANISOTROPIC)
        return GL_NEAREST_MIPMAP_LINEAR;
    case Sampler::ANISOTROPIC:  // Anisotropy is handled in SetTextureAndStates
    case Sampler::LINEAR:
      if (mip_filter == Sampler::NONE)
        return GL_LINEAR;
      else if (mip_filter == Sampler::POINT)
        return GL_LINEAR_MIPMAP_NEAREST;
      else if (mip_filter == Sampler::LINEAR)
        return GL_LINEAR_MIPMAP_LINEAR;
      else if (mip_filter == Sampler::ANISOTROPIC)
        return GL_LINEAR_MIPMAP_LINEAR;
  }
  // fall through
  DLOG(ERROR) << "Unknown filter " << static_cast<int>(o3d_filter);
  DCHECK(false);
  return GL_NONE;
}

unsigned int GLMagFilter(Sampler::FilterType o3d_filter) {
  switch (o3d_filter) {
    case Sampler::NONE:
    case Sampler::POINT:
      return GL_NEAREST;
    case Sampler::LINEAR:
    case Sampler::ANISOTROPIC:
      return GL_LINEAR;
    default:
      DLOG(ERROR) << "Unknown filter " << static_cast<int>(o3d_filter);
      return GL_LINEAR;
  }
}

GLenum GLTextureTarget(Texture* texture) {
  if (texture->IsA(Texture2D::GetApparentClass())) {
    return GL_TEXTURE_2D;
  } else if (texture->IsA(TextureCUBE::GetApparentClass())) {
    return GL_TEXTURE_CUBE_MAP;
  } else {
    DLOG(ERROR) << "Unknown texture target";
    return 0;
  }
}

}  // namespace

void SamplerGLES2::SetTextureAndStates(CGparameter cg_param) {
  // Get the texture object associated with this sampler.
  Texture* texture_object = texture();

  if (!texture_object) {
    texture_object = renderer_->error_texture();
    if (!texture_object) {
      O3D_ERROR(service_locator())
          << "Missing texture for sampler " << name();
      texture_object = renderer_->fallback_error_texture();
    }
  }

  if (!renderer_->SafeToBindTexture(texture_object)) {
    O3D_ERROR(renderer_->service_locator())
        << "Attempt to bind texture, " << texture_object->name()
        << " when drawing to same texture as a RenderSurface";
    texture_object = renderer_->error_texture();
  }

  GLuint handle = static_cast<GLuint>(reinterpret_cast<intptr_t>(
      texture_object->GetTextureHandle()));
  if (handle) {
    cgGLSetTextureParameter(cg_param, handle);
    cgGLEnableTextureParameter(cg_param);
  } else {
    cgGLSetTextureParameter(cg_param, 0);
    cgGLDisableTextureParameter(cg_param);
    return;
  }

  // TODO(o3d): this is a slow check and needs to be moved to initialization
  //     time.
  GLenum target = GLTextureTarget(texture_object);

  if (target) {
    GLenum texture_unit = cgGLGetTextureEnum(cg_param);
    ::glActiveTextureARB(texture_unit);
    glBindTexture(target, handle);
    glTexParameteri(target,
                    GL_TEXTURE_WRAP_S,
                    GLAddressMode(address_mode_u(), GL_REPEAT));
    glTexParameteri(target,
                    GL_TEXTURE_WRAP_T,
                    GLAddressMode(address_mode_v(), GL_REPEAT));
    if (texture_object->IsA(TextureCUBE::GetApparentClass())) {
      glTexParameteri(target,
                      GL_TEXTURE_WRAP_R,
                      GLAddressMode(address_mode_w(), GL_REPEAT));
    }
    glTexParameteri(target,
                    GL_TEXTURE_MIN_FILTER,
                    GLMinFilter(min_filter(), mip_filter()));
    glTexParameteri(target,
                    GL_TEXTURE_MAG_FILTER,
                    GLMagFilter(mag_filter()));

    Float4 color = border_color();
    GLfloat gl_color[4] = {color[0], color[1], color[2], color[3]};
    glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, gl_color);

    // Check for anisotropic texture filtering.
    if (GLEW_EXT_texture_filter_anisotropic) {
      int gl_max_anisotropy =
          (min_filter() == ANISOTROPIC) ? max_anisotropy() : 1;
      glTexParameteri(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, gl_max_anisotropy);
    }
  }
}

void SamplerGLES2::ResetTexture(CGparameter cg_param) {
  Texture* the_texture = texture();
  if (the_texture) {
    // TODO(o3d): this is a slow check and needs to be moved to initialization
    //     time.
    GLenum target = GLTextureTarget(the_texture);
    if (target) {
      GLenum texture_unit = cgGLGetTextureEnum(cg_param);
      glActiveTextureARB(texture_unit);
      glBindTexture(target, 0);
    }
  }
}
}  // namespace o3d