2 * Copyright (C) 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "core/html/canvas/WebGLTexture.h"
30 #include "core/html/canvas/WebGLRenderingContextBase.h"
34 PassRefPtr<WebGLTexture> WebGLTexture::create(WebGLRenderingContextBase* ctx)
36 return adoptRef(new WebGLTexture(ctx));
39 WebGLTexture::WebGLTexture(WebGLRenderingContextBase* ctx)
40 : WebGLSharedObject(ctx)
42 , m_minFilter(GL_NEAREST_MIPMAP_LINEAR)
43 , m_magFilter(GL_LINEAR)
47 , m_isCubeComplete(false)
49 , m_needToUseBlackTexture(false)
50 , m_isFloatType(false)
51 , m_isHalfFloatType(false)
53 ScriptWrappable::init(this);
54 setObject(ctx->webContext()->createTexture());
57 WebGLTexture::~WebGLTexture()
62 void WebGLTexture::setTarget(GLenum target, GLint maxLevel)
66 // Target is finalized the first time bindTexture() is called.
73 m_info[0].resize(maxLevel);
75 case GL_TEXTURE_CUBE_MAP:
78 for (int ii = 0; ii < 6; ++ii)
79 m_info[ii].resize(maxLevel);
84 void WebGLTexture::setParameteri(GLenum pname, GLint param)
86 if (!object() || !m_target)
89 case GL_TEXTURE_MIN_FILTER:
93 case GL_NEAREST_MIPMAP_NEAREST:
94 case GL_LINEAR_MIPMAP_NEAREST:
95 case GL_NEAREST_MIPMAP_LINEAR:
96 case GL_LINEAR_MIPMAP_LINEAR:
101 case GL_TEXTURE_MAG_FILTER:
109 case GL_TEXTURE_WRAP_S:
111 case GL_CLAMP_TO_EDGE:
112 case GL_MIRRORED_REPEAT:
118 case GL_TEXTURE_WRAP_T:
120 case GL_CLAMP_TO_EDGE:
121 case GL_MIRRORED_REPEAT:
133 void WebGLTexture::setParameterf(GLenum pname, GLfloat param)
135 if (!object() || !m_target)
137 GLint iparam = static_cast<GLint>(param);
138 setParameteri(pname, iparam);
141 void WebGLTexture::setLevelInfo(GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLenum type)
143 if (!object() || !m_target)
145 // We assume level, internalFormat, width, height, and type have all been
146 // validated already.
147 int index = mapTargetToIndex(target);
150 m_info[index][level].setInfo(internalFormat, width, height, type);
154 void WebGLTexture::generateMipmapLevelInfo()
156 if (!object() || !m_target)
158 if (!canGenerateMipmaps())
161 for (size_t ii = 0; ii < m_info.size(); ++ii) {
162 const LevelInfo& info0 = m_info[ii][0];
163 GLsizei width = info0.width;
164 GLsizei height = info0.height;
165 GLint levelCount = computeLevelCount(width, height);
166 for (GLint level = 1; level < levelCount; ++level) {
167 width = std::max(1, width >> 1);
168 height = std::max(1, height >> 1);
169 LevelInfo& info = m_info[ii][level];
170 info.setInfo(info0.internalFormat, width, height, info0.type);
175 m_needToUseBlackTexture = false;
178 GLenum WebGLTexture::getInternalFormat(GLenum target, GLint level) const
180 const LevelInfo* info = getLevelInfo(target, level);
183 return info->internalFormat;
186 GLenum WebGLTexture::getType(GLenum target, GLint level) const
188 const LevelInfo* info = getLevelInfo(target, level);
194 GLsizei WebGLTexture::getWidth(GLenum target, GLint level) const
196 const LevelInfo* info = getLevelInfo(target, level);
202 GLsizei WebGLTexture::getHeight(GLenum target, GLint level) const
204 const LevelInfo* info = getLevelInfo(target, level);
210 bool WebGLTexture::isValid(GLenum target, GLint level) const
212 const LevelInfo* info = getLevelInfo(target, level);
218 bool WebGLTexture::isNPOT(GLsizei width, GLsizei height)
220 ASSERT(width >= 0 && height >= 0);
221 if (!width || !height)
223 if ((width & (width - 1)) || (height & (height - 1)))
228 bool WebGLTexture::isNPOT() const
235 bool WebGLTexture::needToUseBlackTexture(TextureExtensionFlag flag) const
239 if (m_needToUseBlackTexture)
241 if ((m_isFloatType && !(flag & TextureFloatLinearExtensionEnabled)) || (m_isHalfFloatType && !(flag && TextureHalfFloatLinearExtensionEnabled))) {
242 if (m_magFilter != GL_NEAREST || (m_minFilter != GL_NEAREST && m_minFilter != GL_NEAREST_MIPMAP_NEAREST))
248 void WebGLTexture::deleteObjectImpl(blink::WebGraphicsContext3D* context3d, Platform3DObject object)
250 context3d->deleteTexture(object);
253 int WebGLTexture::mapTargetToIndex(GLenum target) const
255 if (m_target == GL_TEXTURE_2D) {
256 if (target == GL_TEXTURE_2D)
258 } else if (m_target == GL_TEXTURE_CUBE_MAP) {
260 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
262 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
264 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
266 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
268 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
270 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
277 bool WebGLTexture::canGenerateMipmaps()
281 const LevelInfo& first = m_info[0][0];
282 for (size_t ii = 0; ii < m_info.size(); ++ii) {
283 const LevelInfo& info = m_info[ii][0];
285 || info.width != first.width || info.height != first.height
286 || info.internalFormat != first.internalFormat || info.type != first.type
287 || (m_info.size() > 1 && !m_isCubeComplete))
293 GLint WebGLTexture::computeLevelCount(GLsizei width, GLsizei height)
295 // return 1 + log2Floor(std::max(width, height));
296 GLsizei n = std::max(width, height);
301 for (int ii = 4; ii >= 0; --ii) {
302 int shift = (1 << ii);
303 GLsizei x = (value >> shift);
313 void WebGLTexture::update()
316 for (size_t ii = 0; ii < m_info.size(); ++ii) {
317 if (isNPOT(m_info[ii][0].width, m_info[ii][0].height)) {
323 m_isCubeComplete = true;
324 const LevelInfo& first = m_info[0][0];
325 GLint levelCount = computeLevelCount(first.width, first.height);
327 m_isComplete = false;
329 for (size_t ii = 0; ii < m_info.size() && m_isComplete; ++ii) {
330 const LevelInfo& info0 = m_info[ii][0];
332 || info0.width != first.width || info0.height != first.height
333 || info0.internalFormat != first.internalFormat || info0.type != first.type
334 || (m_info.size() > 1 && info0.width != info0.height)) {
335 if (m_info.size() > 1)
336 m_isCubeComplete = false;
337 m_isComplete = false;
340 GLsizei width = info0.width;
341 GLsizei height = info0.height;
342 for (GLint level = 1; level < levelCount; ++level) {
343 width = std::max(1, width >> 1);
344 height = std::max(1, height >> 1);
345 const LevelInfo& info = m_info[ii][level];
347 || info.width != width || info.height != height
348 || info.internalFormat != info0.internalFormat || info.type != info0.type) {
349 m_isComplete = false;
356 m_isFloatType = m_info[0][0].type == GL_FLOAT;
357 m_isHalfFloatType = m_info[0][0].type == GL_HALF_FLOAT_OES;
359 m_needToUseBlackTexture = false;
361 if (m_isNPOT && ((m_minFilter != GL_NEAREST && m_minFilter != GL_LINEAR)
362 || m_wrapS != GL_CLAMP_TO_EDGE || m_wrapT != GL_CLAMP_TO_EDGE))
363 m_needToUseBlackTexture = true;
364 // If it is a Cube texture, check Cube Completeness first
365 if (m_info.size() > 1 && !m_isCubeComplete)
366 m_needToUseBlackTexture = true;
368 if (!m_isComplete && m_minFilter != GL_NEAREST && m_minFilter != GL_LINEAR)
369 m_needToUseBlackTexture = true;
372 const WebGLTexture::LevelInfo* WebGLTexture::getLevelInfo(GLenum target, GLint level) const
374 if (!object() || !m_target)
376 int targetIndex = mapTargetToIndex(target);
377 if (targetIndex < 0 || targetIndex >= static_cast<int>(m_info.size()))
379 if (level < 0 || level >= static_cast<GLint>(m_info[targetIndex].size()))
381 return &(m_info[targetIndex][level]);