2012-12-03 16:13:47 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef WEBGLTEXTURE_H_
|
|
|
|
#define WEBGLTEXTURE_H_
|
|
|
|
|
2014-05-06 20:11:18 -07:00
|
|
|
#include "WebGLBindableName.h"
|
2014-03-03 23:14:35 -08:00
|
|
|
#include "WebGLFramebufferAttachable.h"
|
2014-05-06 20:11:18 -07:00
|
|
|
#include "WebGLObjectModel.h"
|
2014-09-18 16:14:22 -07:00
|
|
|
#include "WebGLStrongTypes.h"
|
2012-12-03 16:13:47 -08:00
|
|
|
|
|
|
|
#include "nsWrapperCache.h"
|
|
|
|
|
2014-04-30 14:30:21 -07:00
|
|
|
#include "mozilla/CheckedInt.h"
|
2012-12-03 16:13:47 -08:00
|
|
|
#include "mozilla/LinkedList.h"
|
2014-10-13 16:42:20 -07:00
|
|
|
#include "mozilla/Assertions.h"
|
2013-01-15 04:22:03 -08:00
|
|
|
#include <algorithm>
|
2014-10-16 13:07:01 -07:00
|
|
|
#include "nsAlgorithm.h"
|
2012-12-03 16:13:47 -08:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
// Zero is not an integer power of two.
|
2013-09-04 05:14:43 -07:00
|
|
|
inline bool is_pot_assuming_nonnegative(GLsizei x)
|
2012-12-03 16:13:47 -08:00
|
|
|
{
|
|
|
|
return x && (x & (x-1)) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: When this class is switched to new DOM bindings, update the (then-slow)
|
|
|
|
// WrapObject calls in GetParameter and GetFramebufferAttachmentParameter.
|
|
|
|
class WebGLTexture MOZ_FINAL
|
2013-08-29 08:39:17 -07:00
|
|
|
: public nsWrapperCache
|
2014-09-18 16:14:22 -07:00
|
|
|
, public WebGLBindableName<TexTarget>
|
2012-12-03 16:13:47 -08:00
|
|
|
, public WebGLRefCountedObject<WebGLTexture>
|
|
|
|
, public LinkedListElement<WebGLTexture>
|
|
|
|
, public WebGLContextBoundObject
|
2014-03-03 23:14:35 -08:00
|
|
|
, public WebGLFramebufferAttachable
|
2012-12-03 16:13:47 -08:00
|
|
|
{
|
|
|
|
public:
|
2014-09-01 15:26:43 -07:00
|
|
|
explicit WebGLTexture(WebGLContext* aContext);
|
2012-12-03 16:13:47 -08:00
|
|
|
|
|
|
|
void Delete();
|
|
|
|
|
|
|
|
WebGLContext *GetParentObject() const {
|
|
|
|
return Context();
|
|
|
|
}
|
|
|
|
|
2014-04-08 15:27:18 -07:00
|
|
|
virtual JSObject* WrapObject(JSContext *cx) MOZ_OVERRIDE;
|
2012-12-03 16:13:47 -08:00
|
|
|
|
2013-08-29 08:39:17 -07:00
|
|
|
NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(WebGLTexture)
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(WebGLTexture)
|
2012-12-03 16:13:47 -08:00
|
|
|
|
|
|
|
protected:
|
2014-06-26 06:30:49 -07:00
|
|
|
~WebGLTexture() {
|
|
|
|
DeleteOnce();
|
|
|
|
}
|
2012-12-03 16:13:47 -08:00
|
|
|
|
|
|
|
friend class WebGLContext;
|
|
|
|
friend class WebGLFramebuffer;
|
|
|
|
|
|
|
|
// we store information about the various images that are part of
|
|
|
|
// this texture (cubemap faces, mipmap levels)
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2013-10-11 06:16:43 -07:00
|
|
|
class ImageInfo
|
|
|
|
: public WebGLRectangleObject
|
|
|
|
{
|
2012-12-03 16:13:47 -08:00
|
|
|
public:
|
|
|
|
ImageInfo()
|
2014-10-07 16:52:58 -07:00
|
|
|
: mEffectiveInternalFormat(LOCAL_GL_NONE)
|
2014-10-13 16:42:20 -07:00
|
|
|
, mDepth(0)
|
2013-10-11 06:16:43 -07:00
|
|
|
, mImageDataStatus(WebGLImageDataStatus::NoImageData)
|
2012-12-03 16:13:47 -08:00
|
|
|
{}
|
|
|
|
|
2014-05-01 20:15:58 -07:00
|
|
|
ImageInfo(GLsizei width,
|
|
|
|
GLsizei height,
|
2014-10-13 16:42:20 -07:00
|
|
|
GLsizei depth,
|
2014-10-07 16:52:58 -07:00
|
|
|
TexInternalFormat effectiveInternalFormat,
|
2014-05-01 20:15:58 -07:00
|
|
|
WebGLImageDataStatus status)
|
2012-12-03 16:13:47 -08:00
|
|
|
: WebGLRectangleObject(width, height)
|
2014-10-07 16:52:58 -07:00
|
|
|
, mEffectiveInternalFormat(effectiveInternalFormat)
|
2014-10-13 16:42:20 -07:00
|
|
|
, mDepth(depth)
|
2013-10-11 06:16:43 -07:00
|
|
|
, mImageDataStatus(status)
|
|
|
|
{
|
|
|
|
// shouldn't use this constructor to construct a null ImageInfo
|
|
|
|
MOZ_ASSERT(status != WebGLImageDataStatus::NoImageData);
|
|
|
|
}
|
2012-12-03 16:13:47 -08:00
|
|
|
|
|
|
|
bool operator==(const ImageInfo& a) const {
|
2013-10-11 06:16:43 -07:00
|
|
|
return mImageDataStatus == a.mImageDataStatus &&
|
2014-05-01 20:15:58 -07:00
|
|
|
mWidth == a.mWidth &&
|
2013-10-11 06:16:43 -07:00
|
|
|
mHeight == a.mHeight &&
|
2014-10-13 16:42:20 -07:00
|
|
|
mDepth == a.mDepth &&
|
2014-10-07 16:52:58 -07:00
|
|
|
mEffectiveInternalFormat == a.mEffectiveInternalFormat;
|
2012-12-03 16:13:47 -08:00
|
|
|
}
|
|
|
|
bool operator!=(const ImageInfo& a) const {
|
|
|
|
return !(*this == a);
|
|
|
|
}
|
|
|
|
bool IsSquare() const {
|
|
|
|
return mWidth == mHeight;
|
|
|
|
}
|
|
|
|
bool IsPositive() const {
|
2014-10-13 16:42:20 -07:00
|
|
|
return mWidth > 0 && mHeight > 0 && mDepth > 0;
|
2012-12-03 16:13:47 -08:00
|
|
|
}
|
|
|
|
bool IsPowerOfTwo() const {
|
|
|
|
return is_pot_assuming_nonnegative(mWidth) &&
|
|
|
|
is_pot_assuming_nonnegative(mHeight); // negative sizes should never happen (caught in texImage2D...)
|
|
|
|
}
|
2013-10-11 06:16:43 -07:00
|
|
|
bool HasUninitializedImageData() const {
|
|
|
|
return mImageDataStatus == WebGLImageDataStatus::UninitializedImageData;
|
|
|
|
}
|
2014-10-13 16:42:20 -07:00
|
|
|
size_t MemoryUsage() const;
|
2014-07-14 16:55:33 -07:00
|
|
|
|
2014-10-07 16:52:58 -07:00
|
|
|
TexInternalFormat EffectiveInternalFormat() const { return mEffectiveInternalFormat; }
|
2014-10-14 05:54:52 -07:00
|
|
|
GLsizei Depth() const { return mDepth; }
|
2014-05-01 20:15:58 -07:00
|
|
|
|
2012-12-03 16:13:47 -08:00
|
|
|
protected:
|
2014-10-07 16:52:58 -07:00
|
|
|
/*
|
|
|
|
* This is the "effective internal format" of the texture,
|
|
|
|
* an official OpenGL spec concept, see
|
|
|
|
* OpenGL ES 3.0.3 spec, section 3.8.3, page 126 and below.
|
|
|
|
*/
|
|
|
|
TexInternalFormat mEffectiveInternalFormat;
|
|
|
|
|
2014-10-13 16:42:20 -07:00
|
|
|
/*
|
|
|
|
* Used only for 3D textures.
|
|
|
|
* Note that mWidth and mHeight are inherited from WebGLRectangleObject.
|
|
|
|
* It's a pity to store a useless mDepth on non-3D texture images, but
|
|
|
|
* the size of GLsizei is negligible compared to the typical size of a texture image.
|
|
|
|
*/
|
|
|
|
GLsizei mDepth;
|
|
|
|
|
2013-10-11 06:16:43 -07:00
|
|
|
WebGLImageDataStatus mImageDataStatus;
|
2012-12-03 16:13:47 -08:00
|
|
|
|
|
|
|
friend class WebGLTexture;
|
|
|
|
};
|
|
|
|
|
2013-10-01 17:30:38 -07:00
|
|
|
private:
|
2014-09-18 16:14:22 -07:00
|
|
|
static size_t FaceForTarget(TexImageTarget texImageTarget) {
|
2014-10-13 16:42:20 -07:00
|
|
|
if (texImageTarget == LOCAL_GL_TEXTURE_2D ||
|
|
|
|
texImageTarget == LOCAL_GL_TEXTURE_3D)
|
|
|
|
{
|
2014-09-18 16:14:22 -07:00
|
|
|
return 0;
|
2014-10-13 16:42:20 -07:00
|
|
|
}
|
2014-09-18 16:14:22 -07:00
|
|
|
return texImageTarget.get() - LOCAL_GL_TEXTURE_CUBE_MAP_POSITIVE_X;
|
2013-10-01 17:30:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ImageInfo& ImageInfoAtFace(size_t face, GLint level) {
|
2014-10-13 16:42:20 -07:00
|
|
|
MOZ_ASSERT(face < mFacesCount, "wrong face index, must be 0 for TEXTURE_2D or TEXTURE_3D, and at most 5 for cube maps");
|
2013-10-01 17:30:38 -07:00
|
|
|
|
2012-12-03 16:13:47 -08:00
|
|
|
// no need to check level as a wrong value would be caught by ElementAt().
|
|
|
|
return mImageInfos.ElementAt(level * mFacesCount + face);
|
|
|
|
}
|
|
|
|
|
2013-10-01 17:30:38 -07:00
|
|
|
const ImageInfo& ImageInfoAtFace(size_t face, GLint level) const {
|
|
|
|
return const_cast<const ImageInfo&>(
|
|
|
|
const_cast<WebGLTexture*>(this)->ImageInfoAtFace(face, level)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2014-09-18 16:14:22 -07:00
|
|
|
ImageInfo& ImageInfoAt(TexImageTarget imageTarget, GLint level) {
|
2013-10-01 17:30:38 -07:00
|
|
|
size_t face = FaceForTarget(imageTarget);
|
|
|
|
return ImageInfoAtFace(face, level);
|
|
|
|
}
|
|
|
|
|
2014-09-18 16:14:22 -07:00
|
|
|
const ImageInfo& ImageInfoAt(TexImageTarget imageTarget, GLint level) const {
|
2013-10-01 17:30:38 -07:00
|
|
|
return const_cast<WebGLTexture*>(this)->ImageInfoAt(imageTarget, level);
|
2012-12-03 16:13:47 -08:00
|
|
|
}
|
|
|
|
|
2014-09-18 16:14:22 -07:00
|
|
|
bool HasImageInfoAt(TexImageTarget imageTarget, GLint level) const {
|
2013-10-01 17:30:38 -07:00
|
|
|
size_t face = FaceForTarget(imageTarget);
|
2012-12-03 16:13:47 -08:00
|
|
|
CheckedUint32 checked_index = CheckedUint32(level) * mFacesCount + face;
|
|
|
|
return checked_index.isValid() &&
|
|
|
|
checked_index.value() < mImageInfos.Length() &&
|
2013-10-11 06:16:43 -07:00
|
|
|
ImageInfoAt(imageTarget, level).mImageDataStatus != WebGLImageDataStatus::NoImageData;
|
2012-12-03 16:13:47 -08:00
|
|
|
}
|
|
|
|
|
2013-10-01 17:30:38 -07:00
|
|
|
ImageInfo& ImageInfoBase() {
|
|
|
|
return ImageInfoAtFace(0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ImageInfo& ImageInfoBase() const {
|
|
|
|
return ImageInfoAtFace(0, 0);
|
2012-12-03 16:13:47 -08:00
|
|
|
}
|
|
|
|
|
2014-10-13 16:42:20 -07:00
|
|
|
size_t MemoryUsage() const;
|
2012-12-03 16:13:47 -08:00
|
|
|
|
2014-09-18 16:14:22 -07:00
|
|
|
void SetImageDataStatus(TexImageTarget imageTarget, GLint level, WebGLImageDataStatus newStatus) {
|
2013-10-11 06:16:43 -07:00
|
|
|
MOZ_ASSERT(HasImageInfoAt(imageTarget, level));
|
|
|
|
ImageInfo& imageInfo = ImageInfoAt(imageTarget, level);
|
|
|
|
// there is no way to go from having image data to not having any
|
|
|
|
MOZ_ASSERT(newStatus != WebGLImageDataStatus::NoImageData ||
|
|
|
|
imageInfo.mImageDataStatus == WebGLImageDataStatus::NoImageData);
|
2013-10-11 06:16:43 -07:00
|
|
|
if (imageInfo.mImageDataStatus != newStatus) {
|
|
|
|
SetFakeBlackStatus(WebGLTextureFakeBlackStatus::Unknown);
|
|
|
|
}
|
2013-10-11 06:16:43 -07:00
|
|
|
imageInfo.mImageDataStatus = newStatus;
|
|
|
|
}
|
|
|
|
|
2014-10-14 19:10:15 -07:00
|
|
|
void EnsureNoUninitializedImageData(TexImageTarget imageTarget, GLint level);
|
2013-10-11 06:16:43 -07:00
|
|
|
|
2012-12-03 16:13:47 -08:00
|
|
|
protected:
|
|
|
|
|
2014-09-26 13:11:51 -07:00
|
|
|
TexMinFilter mMinFilter;
|
|
|
|
TexMagFilter mMagFilter;
|
|
|
|
TexWrap mWrapS, mWrapT;
|
2012-12-03 16:13:47 -08:00
|
|
|
|
|
|
|
size_t mFacesCount, mMaxLevelWithCustomImages;
|
|
|
|
nsTArray<ImageInfo> mImageInfos;
|
|
|
|
|
2014-07-14 16:55:56 -07:00
|
|
|
bool mHaveGeneratedMipmap; // set by generateMipmap
|
|
|
|
bool mImmutable; // set by texStorage*
|
|
|
|
|
2014-10-16 13:07:01 -07:00
|
|
|
size_t mBaseMipmapLevel; // set by texParameter (defaults to 0)
|
|
|
|
size_t mMaxMipmapLevel; // set by texParameter (defaults to 1000)
|
|
|
|
|
2013-10-11 06:16:43 -07:00
|
|
|
WebGLTextureFakeBlackStatus mFakeBlackStatus;
|
2012-12-03 16:13:47 -08:00
|
|
|
|
|
|
|
void EnsureMaxLevelWithCustomImagesAtLeast(size_t aMaxLevelWithCustomImages) {
|
2013-01-15 04:22:03 -08:00
|
|
|
mMaxLevelWithCustomImages = std::max(mMaxLevelWithCustomImages, aMaxLevelWithCustomImages);
|
2012-12-03 16:13:47 -08:00
|
|
|
mImageInfos.EnsureLengthAtLeast((mMaxLevelWithCustomImages + 1) * mFacesCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CheckFloatTextureFilterParams() const {
|
|
|
|
// Without OES_texture_float_linear, only NEAREST and NEAREST_MIMPAMP_NEAREST are supported
|
|
|
|
return (mMagFilter == LOCAL_GL_NEAREST) &&
|
|
|
|
(mMinFilter == LOCAL_GL_NEAREST || mMinFilter == LOCAL_GL_NEAREST_MIPMAP_NEAREST);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AreBothWrapModesClampToEdge() const {
|
|
|
|
return mWrapS == LOCAL_GL_CLAMP_TO_EDGE && mWrapT == LOCAL_GL_CLAMP_TO_EDGE;
|
|
|
|
}
|
|
|
|
|
2014-10-13 16:42:20 -07:00
|
|
|
bool DoesMipmapHaveAllLevelsConsistentlyDefined(TexImageTarget texImageTarget) const;
|
2012-12-03 16:13:47 -08:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2014-09-18 16:14:22 -07:00
|
|
|
void Bind(TexTarget aTexTarget);
|
2012-12-03 16:13:47 -08:00
|
|
|
|
2014-09-18 16:14:22 -07:00
|
|
|
void SetImageInfo(TexImageTarget aTarget, GLint aLevel,
|
2014-10-13 16:42:20 -07:00
|
|
|
GLsizei aWidth, GLsizei aHeight, GLsizei aDepth,
|
2014-10-07 16:52:58 -07:00
|
|
|
TexInternalFormat aFormat, WebGLImageDataStatus aStatus);
|
2012-12-03 16:13:47 -08:00
|
|
|
|
2014-09-26 13:11:51 -07:00
|
|
|
void SetMinFilter(TexMinFilter aMinFilter) {
|
2012-12-03 16:13:47 -08:00
|
|
|
mMinFilter = aMinFilter;
|
2013-10-11 06:16:43 -07:00
|
|
|
SetFakeBlackStatus(WebGLTextureFakeBlackStatus::Unknown);
|
2012-12-03 16:13:47 -08:00
|
|
|
}
|
2014-09-26 13:11:51 -07:00
|
|
|
void SetMagFilter(TexMagFilter aMagFilter) {
|
2012-12-03 16:13:47 -08:00
|
|
|
mMagFilter = aMagFilter;
|
2013-10-11 06:16:43 -07:00
|
|
|
SetFakeBlackStatus(WebGLTextureFakeBlackStatus::Unknown);
|
2012-12-03 16:13:47 -08:00
|
|
|
}
|
2014-09-26 13:11:51 -07:00
|
|
|
void SetWrapS(TexWrap aWrapS) {
|
2012-12-03 16:13:47 -08:00
|
|
|
mWrapS = aWrapS;
|
2013-10-11 06:16:43 -07:00
|
|
|
SetFakeBlackStatus(WebGLTextureFakeBlackStatus::Unknown);
|
2012-12-03 16:13:47 -08:00
|
|
|
}
|
2014-09-26 13:11:51 -07:00
|
|
|
void SetWrapT(TexWrap aWrapT) {
|
2012-12-03 16:13:47 -08:00
|
|
|
mWrapT = aWrapT;
|
2013-10-11 06:16:43 -07:00
|
|
|
SetFakeBlackStatus(WebGLTextureFakeBlackStatus::Unknown);
|
2012-12-03 16:13:47 -08:00
|
|
|
}
|
2014-09-26 13:11:51 -07:00
|
|
|
TexMinFilter MinFilter() const { return mMinFilter; }
|
2012-12-03 16:13:47 -08:00
|
|
|
|
|
|
|
bool DoesMinFilterRequireMipmap() const {
|
|
|
|
return !(mMinFilter == LOCAL_GL_NEAREST || mMinFilter == LOCAL_GL_LINEAR);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetGeneratedMipmap();
|
|
|
|
|
|
|
|
void SetCustomMipmap();
|
|
|
|
|
|
|
|
bool IsFirstImagePowerOfTwo() const {
|
2013-10-01 17:30:38 -07:00
|
|
|
return ImageInfoBase().IsPowerOfTwo();
|
2012-12-03 16:13:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AreAllLevel0ImageInfosEqual() const;
|
|
|
|
|
2014-10-13 16:42:20 -07:00
|
|
|
bool IsMipmapComplete() const;
|
2012-12-03 16:13:47 -08:00
|
|
|
|
|
|
|
bool IsCubeComplete() const;
|
|
|
|
|
|
|
|
bool IsMipmapCubeComplete() const;
|
|
|
|
|
2014-04-30 14:30:21 -07:00
|
|
|
void SetFakeBlackStatus(WebGLTextureFakeBlackStatus x);
|
2014-05-06 20:11:18 -07:00
|
|
|
|
2014-07-14 16:55:56 -07:00
|
|
|
bool IsImmutable() const { return mImmutable; }
|
|
|
|
void SetImmutable() { mImmutable = true; }
|
|
|
|
|
2014-10-16 13:07:01 -07:00
|
|
|
void SetBaseMipmapLevel(size_t level) { mBaseMipmapLevel = level; }
|
|
|
|
void SetMaxMipmapLevel(size_t level) { mMaxMipmapLevel = level; }
|
|
|
|
|
|
|
|
// Clamping (from ES 3.0.4, section 3.8 - Texturing). When not immutable,
|
|
|
|
// the ranges must be guarded.
|
|
|
|
size_t EffectiveBaseMipmapLevel() const {
|
|
|
|
if (IsImmutable())
|
|
|
|
return std::min(mBaseMipmapLevel, mMaxLevelWithCustomImages);
|
|
|
|
return mBaseMipmapLevel;
|
|
|
|
}
|
|
|
|
size_t EffectiveMaxMipmapLevel() const {
|
|
|
|
if (IsImmutable())
|
|
|
|
return mozilla::clamped(mMaxMipmapLevel, EffectiveBaseMipmapLevel(), mMaxLevelWithCustomImages);
|
|
|
|
return std::min(mMaxMipmapLevel, mMaxLevelWithCustomImages);
|
|
|
|
}
|
|
|
|
bool IsMipmapRangeValid() const;
|
|
|
|
|
2014-07-14 16:55:56 -07:00
|
|
|
size_t MaxLevelWithCustomImages() const { return mMaxLevelWithCustomImages; }
|
|
|
|
|
2013-10-11 06:16:43 -07:00
|
|
|
// Returns the current fake-black-status, except if it was Unknown,
|
|
|
|
// in which case this function resolves it first, so it never returns Unknown.
|
|
|
|
WebGLTextureFakeBlackStatus ResolvedFakeBlackStatus();
|
2012-12-03 16:13:47 -08:00
|
|
|
};
|
|
|
|
|
2014-07-14 16:55:56 -07:00
|
|
|
inline TexImageTarget
|
|
|
|
TexImageTargetForTargetAndFace(TexTarget target, size_t face)
|
|
|
|
{
|
2014-10-13 16:42:20 -07:00
|
|
|
switch (target.get()) {
|
|
|
|
case LOCAL_GL_TEXTURE_2D:
|
|
|
|
case LOCAL_GL_TEXTURE_3D:
|
|
|
|
MOZ_ASSERT(face == 0);
|
|
|
|
return target.get();
|
|
|
|
case LOCAL_GL_TEXTURE_CUBE_MAP:
|
|
|
|
MOZ_ASSERT(face < 6);
|
|
|
|
return LOCAL_GL_TEXTURE_CUBE_MAP_POSITIVE_X + face;
|
|
|
|
default:
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
2014-07-14 16:55:56 -07:00
|
|
|
}
|
|
|
|
|
2012-12-03 16:13:47 -08:00
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif
|