2009-09-02 17:47:49 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Mozilla Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2007
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Vladimir Vukicevic <vladimir@pobox.com> (original author)
|
2009-09-17 23:01:12 -07:00
|
|
|
* Mark Steele <mwsteele@gmail.com>
|
2009-09-02 17:47:49 -07:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#ifndef WEBGLCONTEXT_H_
|
|
|
|
#define WEBGLCONTEXT_H_
|
|
|
|
|
|
|
|
#include <stdarg.h>
|
2010-06-10 10:45:00 -07:00
|
|
|
#include <vector>
|
2009-09-02 17:47:49 -07:00
|
|
|
|
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsDataHashtable.h"
|
|
|
|
#include "nsRefPtrHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
|
|
|
|
|
|
|
#include "nsIDocShell.h"
|
|
|
|
|
2010-11-16 20:33:04 -08:00
|
|
|
#include "nsIDOMWebGLRenderingContext.h"
|
2009-09-02 17:47:49 -07:00
|
|
|
#include "nsICanvasRenderingContextInternal.h"
|
2010-05-17 21:04:22 -07:00
|
|
|
#include "nsHTMLCanvasElement.h"
|
2009-09-02 17:47:49 -07:00
|
|
|
#include "nsWeakReference.h"
|
|
|
|
#include "nsIDOMHTMLElement.h"
|
2009-09-17 23:01:07 -07:00
|
|
|
#include "nsIJSNativeInitializer.h"
|
2009-09-02 17:47:49 -07:00
|
|
|
|
2010-07-18 22:01:14 -07:00
|
|
|
#include "GLContextProvider.h"
|
2010-05-17 21:04:22 -07:00
|
|
|
#include "Layers.h"
|
2010-01-22 18:29:49 -08:00
|
|
|
|
2010-07-03 15:32:19 -07:00
|
|
|
#include "CheckedInt.h"
|
|
|
|
|
2009-09-02 17:47:49 -07:00
|
|
|
class nsIDocShell;
|
2010-11-16 20:33:03 -08:00
|
|
|
class nsIPropertyBag;
|
2009-09-02 17:47:49 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
class WebGLTexture;
|
|
|
|
class WebGLBuffer;
|
|
|
|
class WebGLProgram;
|
|
|
|
class WebGLShader;
|
|
|
|
class WebGLFramebuffer;
|
|
|
|
class WebGLRenderbuffer;
|
2010-06-01 23:09:19 -07:00
|
|
|
class WebGLUniformLocation;
|
2009-09-02 17:47:49 -07:00
|
|
|
|
|
|
|
class WebGLZeroingObject;
|
2010-06-10 10:45:00 -07:00
|
|
|
class WebGLContextBoundObject;
|
2009-09-02 17:47:49 -07:00
|
|
|
|
2010-08-23 14:03:53 -07:00
|
|
|
enum FakeBlackStatus { DoNotNeedFakeBlack, DoNeedFakeBlack, DontKnowIfNeedFakeBlack };
|
|
|
|
|
2011-02-24 14:17:34 -08:00
|
|
|
struct VertexAttrib0Status {
|
|
|
|
enum { Default, EmulatedUninitializedArray, EmulatedInitializedArray };
|
|
|
|
};
|
|
|
|
|
2010-10-15 14:50:15 -07:00
|
|
|
struct WebGLTexelFormat {
|
2011-05-20 12:53:53 -07:00
|
|
|
enum { Generic, Auto, RGBA8, RGB8, RGBX8, BGRA8, BGR8, BGRX8, RGBA5551, RGBA4444, RGB565, R8, RA8, A8,
|
|
|
|
RGBA32F, RGB32F, A32F, R32F, RA32F };
|
2010-10-15 14:50:15 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
struct WebGLTexelPremultiplicationOp {
|
|
|
|
enum { Generic, None, Premultiply, Unmultiply };
|
|
|
|
};
|
|
|
|
|
|
|
|
int GetWebGLTexelFormat(GLenum format, GLenum type);
|
|
|
|
|
2010-08-23 14:03:53 -07:00
|
|
|
inline PRBool is_pot_assuming_nonnegative(WebGLsizei x)
|
|
|
|
{
|
|
|
|
return (x & (x-1)) == 0;
|
|
|
|
}
|
|
|
|
|
2009-09-02 17:47:49 -07:00
|
|
|
class WebGLObjectBaseRefPtr
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
friend class WebGLZeroingObject;
|
|
|
|
|
|
|
|
WebGLObjectBaseRefPtr()
|
|
|
|
: mRawPtr(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
WebGLObjectBaseRefPtr(nsISupports *rawPtr)
|
|
|
|
: mRawPtr(rawPtr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Zero() {
|
|
|
|
if (mRawPtr) {
|
|
|
|
// Note: RemoveRefOwner isn't called here, because
|
|
|
|
// the entire owner array will be cleared.
|
|
|
|
mRawPtr->Release();
|
|
|
|
mRawPtr = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsISupports *mRawPtr;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class WebGLObjectRefPtr
|
|
|
|
: public WebGLObjectBaseRefPtr
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef T element_type;
|
|
|
|
|
|
|
|
WebGLObjectRefPtr()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
WebGLObjectRefPtr(const WebGLObjectRefPtr<T>& aSmartPtr)
|
|
|
|
: WebGLObjectBaseRefPtr(aSmartPtr.mRawPtr)
|
|
|
|
{
|
|
|
|
if (mRawPtr) {
|
|
|
|
RawPtr()->AddRef();
|
|
|
|
RawPtr()->AddRefOwner(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WebGLObjectRefPtr(T *aRawPtr)
|
|
|
|
: WebGLObjectBaseRefPtr(aRawPtr)
|
|
|
|
{
|
|
|
|
if (mRawPtr) {
|
|
|
|
RawPtr()->AddRef();
|
|
|
|
RawPtr()->AddRefOwner(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WebGLObjectRefPtr(const already_AddRefed<T>& aSmartPtr)
|
|
|
|
: WebGLObjectBaseRefPtr(aSmartPtr.mRawPtr)
|
|
|
|
// construct from |dont_AddRef(expr)|
|
|
|
|
{
|
|
|
|
if (mRawPtr) {
|
|
|
|
RawPtr()->AddRef();
|
|
|
|
RawPtr()->AddRefOwner(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~WebGLObjectRefPtr() {
|
|
|
|
if (mRawPtr) {
|
|
|
|
RawPtr()->RemoveRefOwner(this);
|
|
|
|
RawPtr()->Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WebGLObjectRefPtr<T>&
|
|
|
|
operator=(const WebGLObjectRefPtr<T>& rhs)
|
|
|
|
{
|
|
|
|
assign_with_AddRef(static_cast<T*>(rhs.mRawPtr));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
WebGLObjectRefPtr<T>&
|
|
|
|
operator=(T* rhs)
|
|
|
|
{
|
|
|
|
assign_with_AddRef(rhs);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
WebGLObjectRefPtr<T>&
|
|
|
|
operator=(const already_AddRefed<T>& rhs)
|
|
|
|
{
|
|
|
|
assign_assuming_AddRef(static_cast<T*>(rhs.mRawPtr));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
T* get() const {
|
|
|
|
return const_cast<T*>(static_cast<T*>(mRawPtr));
|
|
|
|
}
|
|
|
|
|
|
|
|
operator T*() const {
|
|
|
|
return get();
|
|
|
|
}
|
|
|
|
|
|
|
|
T* operator->() const {
|
|
|
|
NS_PRECONDITION(mRawPtr != 0, "You can't dereference a NULL WebGLObjectRefPtr with operator->()!");
|
|
|
|
return get();
|
|
|
|
}
|
|
|
|
|
|
|
|
T& operator*() const {
|
|
|
|
NS_PRECONDITION(mRawPtr != 0, "You can't dereference a NULL WebGLObjectRefPtr with operator*()!");
|
|
|
|
return *get();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
T* RawPtr() { return static_cast<T*>(mRawPtr); }
|
|
|
|
|
|
|
|
void assign_with_AddRef(T* rawPtr) {
|
|
|
|
if (rawPtr) {
|
|
|
|
rawPtr->AddRef();
|
|
|
|
rawPtr->AddRefOwner(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
assign_assuming_AddRef(rawPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void assign_assuming_AddRef(T* newPtr) {
|
|
|
|
T* oldPtr = RawPtr();
|
|
|
|
mRawPtr = newPtr;
|
|
|
|
if (oldPtr) {
|
|
|
|
oldPtr->RemoveRefOwner(this);
|
|
|
|
oldPtr->Release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class WebGLBuffer;
|
|
|
|
|
|
|
|
struct WebGLVertexAttribData {
|
2010-11-16 20:33:03 -08:00
|
|
|
// note that these initial values are what GL initializes vertex attribs to
|
2009-09-02 17:47:49 -07:00
|
|
|
WebGLVertexAttribData()
|
2010-11-16 20:33:03 -08:00
|
|
|
: buf(0), stride(0), size(4), byteOffset(0),
|
|
|
|
type(LOCAL_GL_FLOAT), enabled(PR_FALSE), normalized(PR_FALSE)
|
2009-09-02 17:47:49 -07:00
|
|
|
{ }
|
|
|
|
|
|
|
|
WebGLObjectRefPtr<WebGLBuffer> buf;
|
2010-05-28 15:52:39 -07:00
|
|
|
WebGLuint stride;
|
|
|
|
WebGLuint size;
|
2010-06-01 23:09:18 -07:00
|
|
|
GLuint byteOffset;
|
|
|
|
GLenum type;
|
2009-09-02 17:47:49 -07:00
|
|
|
PRBool enabled;
|
2010-09-02 07:34:08 -07:00
|
|
|
PRBool normalized;
|
2010-06-01 23:09:18 -07:00
|
|
|
|
2010-06-08 15:14:43 -07:00
|
|
|
GLuint componentSize() const {
|
2010-06-01 23:09:18 -07:00
|
|
|
switch(type) {
|
|
|
|
case LOCAL_GL_BYTE:
|
2010-06-08 15:14:43 -07:00
|
|
|
return sizeof(GLbyte);
|
2010-06-01 23:09:18 -07:00
|
|
|
break;
|
|
|
|
case LOCAL_GL_UNSIGNED_BYTE:
|
2010-06-08 15:14:43 -07:00
|
|
|
return sizeof(GLubyte);
|
2010-06-01 23:09:18 -07:00
|
|
|
break;
|
|
|
|
case LOCAL_GL_SHORT:
|
2010-06-08 15:14:43 -07:00
|
|
|
return sizeof(GLshort);
|
2010-06-01 23:09:18 -07:00
|
|
|
break;
|
|
|
|
case LOCAL_GL_UNSIGNED_SHORT:
|
2010-06-08 15:14:43 -07:00
|
|
|
return sizeof(GLushort);
|
2010-06-01 23:09:18 -07:00
|
|
|
break;
|
|
|
|
// XXX case LOCAL_GL_FIXED:
|
|
|
|
case LOCAL_GL_FLOAT:
|
2010-06-08 15:14:43 -07:00
|
|
|
return sizeof(GLfloat);
|
2010-06-01 23:09:18 -07:00
|
|
|
break;
|
2010-06-08 15:14:43 -07:00
|
|
|
default:
|
|
|
|
NS_ERROR("Should never get here!");
|
|
|
|
return 0;
|
2010-06-01 23:09:18 -07:00
|
|
|
}
|
2010-06-08 15:14:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
GLuint actualStride() const {
|
|
|
|
if (stride) return stride;
|
|
|
|
return size * componentSize();
|
2010-06-01 23:09:18 -07:00
|
|
|
}
|
2009-09-02 17:47:49 -07:00
|
|
|
};
|
|
|
|
|
2010-11-16 20:33:03 -08:00
|
|
|
struct WebGLContextOptions {
|
|
|
|
// these are defaults
|
|
|
|
WebGLContextOptions()
|
|
|
|
: alpha(true), depth(true), stencil(false),
|
|
|
|
premultipliedAlpha(true), antialiasHint(false)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool operator==(const WebGLContextOptions& other) const {
|
|
|
|
return
|
|
|
|
alpha == other.alpha &&
|
|
|
|
depth == other.depth &&
|
|
|
|
stencil == other.stencil &&
|
|
|
|
premultipliedAlpha == other.premultipliedAlpha &&
|
|
|
|
antialiasHint == other.antialiasHint;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const WebGLContextOptions& other) const {
|
|
|
|
return
|
|
|
|
alpha != other.alpha ||
|
|
|
|
depth != other.depth ||
|
|
|
|
stencil != other.stencil ||
|
|
|
|
premultipliedAlpha != other.premultipliedAlpha ||
|
|
|
|
antialiasHint != other.antialiasHint;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool alpha;
|
|
|
|
bool depth;
|
|
|
|
bool stencil;
|
|
|
|
|
|
|
|
bool premultipliedAlpha;
|
|
|
|
bool antialiasHint;
|
|
|
|
};
|
|
|
|
|
2009-09-02 17:47:49 -07:00
|
|
|
class WebGLContext :
|
2010-11-16 20:33:04 -08:00
|
|
|
public nsIDOMWebGLRenderingContext,
|
2009-09-02 17:47:49 -07:00
|
|
|
public nsICanvasRenderingContextInternal,
|
|
|
|
public nsSupportsWeakReference
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
WebGLContext();
|
|
|
|
virtual ~WebGLContext();
|
|
|
|
|
2010-06-15 14:38:05 -07:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
|
2010-11-16 20:33:04 -08:00
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(WebGLContext, nsIDOMWebGLRenderingContext)
|
2010-06-15 14:38:05 -07:00
|
|
|
|
2010-11-16 20:33:04 -08:00
|
|
|
NS_DECL_NSIDOMWEBGLRENDERINGCONTEXT
|
2009-09-02 17:47:49 -07:00
|
|
|
|
|
|
|
// nsICanvasRenderingContextInternal
|
2010-05-17 21:04:22 -07:00
|
|
|
NS_IMETHOD SetCanvasElement(nsHTMLCanvasElement* aParentCanvas);
|
2009-09-02 17:47:49 -07:00
|
|
|
NS_IMETHOD SetDimensions(PRInt32 width, PRInt32 height);
|
|
|
|
NS_IMETHOD InitializeWithSurface(nsIDocShell *docShell, gfxASurface *surface, PRInt32 width, PRInt32 height)
|
|
|
|
{ return NS_ERROR_NOT_IMPLEMENTED; }
|
2010-08-19 08:22:46 -07:00
|
|
|
NS_IMETHOD Reset()
|
|
|
|
{ /* (InitializeWithSurface) */ return NS_ERROR_NOT_IMPLEMENTED; }
|
2009-09-02 17:47:49 -07:00
|
|
|
NS_IMETHOD Render(gfxContext *ctx, gfxPattern::GraphicsFilter f);
|
|
|
|
NS_IMETHOD GetInputStream(const char* aMimeType,
|
|
|
|
const PRUnichar* aEncoderOptions,
|
|
|
|
nsIInputStream **aStream);
|
|
|
|
NS_IMETHOD GetThebesSurface(gfxASurface **surface);
|
|
|
|
NS_IMETHOD SetIsOpaque(PRBool b) { return NS_OK; };
|
2010-11-16 20:33:03 -08:00
|
|
|
NS_IMETHOD SetContextOptions(nsIPropertyBag *aOptions);
|
|
|
|
|
2010-06-08 10:27:24 -07:00
|
|
|
NS_IMETHOD SetIsIPC(PRBool b) { return NS_ERROR_NOT_IMPLEMENTED; }
|
2009-10-29 10:58:31 -07:00
|
|
|
NS_IMETHOD Redraw(const gfxRect&) { return NS_ERROR_NOT_IMPLEMENTED; }
|
2010-03-24 03:47:18 -07:00
|
|
|
NS_IMETHOD Swap(mozilla::ipc::Shmem& aBack,
|
|
|
|
PRInt32 x, PRInt32 y, PRInt32 w, PRInt32 h)
|
|
|
|
{ return NS_ERROR_NOT_IMPLEMENTED; }
|
2010-06-04 06:58:22 -07:00
|
|
|
NS_IMETHOD Swap(PRUint32 nativeID,
|
|
|
|
PRInt32 x, PRInt32 y, PRInt32 w, PRInt32 h)
|
|
|
|
{ return NS_ERROR_NOT_IMPLEMENTED; }
|
2009-09-02 17:47:49 -07:00
|
|
|
|
2010-05-28 15:52:39 -07:00
|
|
|
nsresult SynthesizeGLError(WebGLenum err);
|
|
|
|
nsresult SynthesizeGLError(WebGLenum err, const char *fmt, ...);
|
2010-05-15 06:55:45 -07:00
|
|
|
|
2010-06-04 12:03:33 -07:00
|
|
|
nsresult ErrorInvalidEnum(const char *fmt = 0, ...);
|
|
|
|
nsresult ErrorInvalidOperation(const char *fmt = 0, ...);
|
|
|
|
nsresult ErrorInvalidValue(const char *fmt = 0, ...);
|
2010-07-16 07:31:48 -07:00
|
|
|
nsresult ErrorInvalidEnumInfo(const char *info, PRUint32 enumvalue) {
|
|
|
|
return ErrorInvalidEnum("%s: invalid enum value 0x%x", info, enumvalue);
|
2010-06-30 08:49:59 -07:00
|
|
|
}
|
2011-02-24 14:17:34 -08:00
|
|
|
nsresult ErrorOutOfMemory(const char *fmt = 0, ...);
|
2010-05-15 06:55:45 -07:00
|
|
|
|
2010-08-23 14:03:35 -07:00
|
|
|
WebGLTexture *activeBoundTextureForTarget(WebGLenum target) {
|
|
|
|
return target == LOCAL_GL_TEXTURE_2D ? mBound2DTextures[mActiveTexture]
|
|
|
|
: mBoundCubeMapTextures[mActiveTexture];
|
|
|
|
}
|
|
|
|
|
2011-03-27 16:59:47 -07:00
|
|
|
already_AddRefed<CanvasLayer> GetCanvasLayer(nsDisplayListBuilder* aBuilder,
|
|
|
|
CanvasLayer *aOldLayer,
|
2010-07-15 14:07:46 -07:00
|
|
|
LayerManager *aManager);
|
2011-03-27 16:59:47 -07:00
|
|
|
void MarkContextClean() { mInvalidated = PR_FALSE; }
|
2010-05-17 21:04:22 -07:00
|
|
|
|
2010-06-04 12:03:37 -07:00
|
|
|
// a number that increments every time we have an event that causes
|
|
|
|
// all context resources to be lost.
|
2010-07-03 15:32:19 -07:00
|
|
|
PRUint32 Generation() { return mGeneration.value(); }
|
2010-08-23 14:03:53 -07:00
|
|
|
|
2011-02-24 14:17:34 -08:00
|
|
|
protected:
|
2010-08-23 14:03:53 -07:00
|
|
|
void SetDontKnowIfNeedFakeBlack() {
|
|
|
|
mFakeBlackStatus = DontKnowIfNeedFakeBlack;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool NeedFakeBlack();
|
|
|
|
void BindFakeBlackTextures();
|
|
|
|
void UnbindFakeBlackTextures();
|
|
|
|
|
2011-02-24 14:17:34 -08:00
|
|
|
int WhatDoesVertexAttrib0Need();
|
2010-09-02 07:34:08 -07:00
|
|
|
void DoFakeVertexAttrib0(WebGLuint vertexCount);
|
|
|
|
void UndoFakeVertexAttrib0();
|
2011-02-24 14:17:34 -08:00
|
|
|
void InvalidateFakeVertexAttrib0();
|
2010-09-02 07:34:08 -07:00
|
|
|
|
2010-06-15 14:38:05 -07:00
|
|
|
nsCOMPtr<nsIDOMHTMLCanvasElement> mCanvasElement;
|
|
|
|
nsHTMLCanvasElement *HTMLCanvasElement() {
|
|
|
|
return static_cast<nsHTMLCanvasElement*>(mCanvasElement.get());
|
|
|
|
}
|
2009-09-02 17:47:49 -07:00
|
|
|
|
2010-05-17 21:04:22 -07:00
|
|
|
nsRefPtr<gl::GLContext> gl;
|
2009-09-02 17:47:49 -07:00
|
|
|
|
|
|
|
PRInt32 mWidth, mHeight;
|
2010-07-03 15:32:19 -07:00
|
|
|
CheckedUint32 mGeneration;
|
2009-09-02 17:47:49 -07:00
|
|
|
|
2010-11-16 20:33:03 -08:00
|
|
|
WebGLContextOptions mOptions;
|
|
|
|
|
2010-07-15 14:07:46 -07:00
|
|
|
PRPackedBool mInvalidated;
|
|
|
|
PRPackedBool mResetLayer;
|
2010-09-13 08:40:01 -07:00
|
|
|
PRPackedBool mVerbose;
|
2010-11-16 20:33:03 -08:00
|
|
|
PRPackedBool mOptionsFrozen;
|
2009-09-02 17:47:49 -07:00
|
|
|
|
2010-05-28 15:52:39 -07:00
|
|
|
WebGLuint mActiveTexture;
|
|
|
|
WebGLenum mSynthesizedGLError;
|
2010-05-15 06:55:45 -07:00
|
|
|
|
2010-07-14 20:52:34 -07:00
|
|
|
// whether shader validation is supported
|
|
|
|
PRBool mShaderValidation;
|
|
|
|
|
|
|
|
// some GL constants
|
2010-08-23 14:03:35 -07:00
|
|
|
PRInt32 mGLMaxVertexAttribs;
|
|
|
|
PRInt32 mGLMaxTextureUnits;
|
|
|
|
PRInt32 mGLMaxTextureSize;
|
|
|
|
PRInt32 mGLMaxCubeMapTextureSize;
|
|
|
|
PRInt32 mGLMaxTextureImageUnits;
|
|
|
|
PRInt32 mGLMaxVertexTextureImageUnits;
|
|
|
|
PRInt32 mGLMaxVaryingVectors;
|
|
|
|
PRInt32 mGLMaxFragmentUniformVectors;
|
|
|
|
PRInt32 mGLMaxVertexUniformVectors;
|
2010-07-14 20:52:34 -07:00
|
|
|
|
2011-05-20 12:53:53 -07:00
|
|
|
// extensions
|
|
|
|
enum WebGLExtensionID {
|
|
|
|
WebGL_OES_texture_float,
|
|
|
|
WebGLExtensionID_Max
|
|
|
|
};
|
2011-05-20 12:53:53 -07:00
|
|
|
nsCOMPtr<nsIWebGLExtension> mEnabledExtensions[WebGLExtensionID_Max];
|
|
|
|
PRBool IsExtensionEnabled(WebGLExtensionID ext) const {
|
|
|
|
NS_ABORT_IF_FALSE(ext >= 0 && ext < WebGLExtensionID_Max, "bogus index!");
|
2011-05-20 12:53:53 -07:00
|
|
|
return mEnabledExtensions[ext] != nsnull;
|
|
|
|
}
|
|
|
|
|
2010-05-17 21:04:22 -07:00
|
|
|
PRBool SafeToCreateCanvas3DContext(nsHTMLCanvasElement *canvasElement);
|
2010-06-14 11:44:12 -07:00
|
|
|
PRBool InitAndValidateGL();
|
2011-01-25 19:19:46 -08:00
|
|
|
PRBool ValidateBuffers(PRInt32* maxAllowedCount, const char *info);
|
2010-06-30 08:49:59 -07:00
|
|
|
PRBool ValidateCapabilityEnum(WebGLenum cap, const char *info);
|
2010-12-06 03:34:35 -08:00
|
|
|
PRBool ValidateBlendEquationEnum(WebGLenum cap, const char *info);
|
|
|
|
PRBool ValidateBlendFuncDstEnum(WebGLenum mode, const char *info);
|
|
|
|
PRBool ValidateBlendFuncSrcEnum(WebGLenum mode, const char *info);
|
|
|
|
PRBool ValidateBlendFuncEnumsCompatibility(WebGLenum sfactor, WebGLenum dfactor, const char *info);
|
2010-06-30 08:49:59 -07:00
|
|
|
PRBool ValidateTextureTargetEnum(WebGLenum target, const char *info);
|
|
|
|
PRBool ValidateComparisonEnum(WebGLenum target, const char *info);
|
|
|
|
PRBool ValidateStencilOpEnum(WebGLenum action, const char *info);
|
2010-07-16 07:31:48 -07:00
|
|
|
PRBool ValidateFaceEnum(WebGLenum face, const char *info);
|
2010-07-03 15:34:07 -07:00
|
|
|
PRBool ValidateBufferUsageEnum(WebGLenum target, const char *info);
|
2011-05-20 12:53:53 -07:00
|
|
|
PRBool ValidateTexFormatAndType(WebGLenum format, WebGLenum type, int jsArrayType,
|
2010-06-30 08:49:59 -07:00
|
|
|
PRUint32 *texelSize, const char *info);
|
2010-07-16 07:31:48 -07:00
|
|
|
PRBool ValidateDrawModeEnum(WebGLenum mode, const char *info);
|
2011-02-24 14:17:34 -08:00
|
|
|
PRBool ValidateAttribIndex(WebGLuint index, const char *info);
|
2010-06-30 08:48:30 -07:00
|
|
|
|
2009-09-02 17:47:49 -07:00
|
|
|
void Invalidate();
|
2010-07-18 22:01:14 -07:00
|
|
|
void DestroyResourcesAndContext();
|
2009-09-02 17:47:49 -07:00
|
|
|
|
2010-05-17 21:04:22 -07:00
|
|
|
void MakeContextCurrent() { gl->MakeCurrent(); }
|
2009-09-02 17:47:49 -07:00
|
|
|
|
2010-01-22 13:34:25 -08:00
|
|
|
// helpers
|
2010-05-28 15:52:39 -07:00
|
|
|
nsresult TexImage2D_base(WebGLenum target, WebGLint level, WebGLenum internalformat,
|
2010-10-15 14:50:15 -07:00
|
|
|
WebGLsizei width, WebGLsizei height, WebGLsizei srcStrideOrZero, WebGLint border,
|
2010-05-28 15:52:39 -07:00
|
|
|
WebGLenum format, WebGLenum type,
|
2010-10-15 14:50:15 -07:00
|
|
|
void *data, PRUint32 byteLength,
|
2011-05-20 12:53:53 -07:00
|
|
|
int jsArrayType,
|
2010-10-15 14:50:15 -07:00
|
|
|
int srcFormat, PRBool srcPremultiplied);
|
2010-05-28 15:52:39 -07:00
|
|
|
nsresult TexSubImage2D_base(WebGLenum target, WebGLint level,
|
|
|
|
WebGLint xoffset, WebGLint yoffset,
|
2010-10-15 14:50:15 -07:00
|
|
|
WebGLsizei width, WebGLsizei height, WebGLsizei srcStrideOrZero,
|
2010-05-28 15:52:39 -07:00
|
|
|
WebGLenum format, WebGLenum type,
|
2010-10-15 14:50:15 -07:00
|
|
|
void *pixels, PRUint32 byteLength,
|
2011-05-20 12:53:53 -07:00
|
|
|
int jsArrayType,
|
2010-10-15 14:50:15 -07:00
|
|
|
int srcFormat, PRBool srcPremultiplied);
|
2010-06-19 07:46:12 -07:00
|
|
|
nsresult ReadPixels_base(WebGLint x, WebGLint y, WebGLsizei width, WebGLsizei height,
|
|
|
|
WebGLenum format, WebGLenum type, void *data, PRUint32 byteLength);
|
2010-08-23 14:03:44 -07:00
|
|
|
nsresult TexParameter_base(WebGLenum target, WebGLenum pname,
|
|
|
|
WebGLint *intParamPtr, WebGLfloat *floatParamPtr);
|
2010-01-22 13:34:25 -08:00
|
|
|
|
2010-10-15 14:50:15 -07:00
|
|
|
void ConvertImage(size_t width, size_t height, size_t srcStride, size_t dstStride,
|
2010-10-15 14:50:15 -07:00
|
|
|
const PRUint8*src, PRUint8 *dst,
|
|
|
|
int srcFormat, PRBool srcPremultiplied,
|
|
|
|
int dstFormat, PRBool dstPremultiplied,
|
|
|
|
size_t dstTexelSize);
|
|
|
|
|
2010-01-22 13:34:25 -08:00
|
|
|
nsresult DOMElementToImageSurface(nsIDOMElement *imageOrCanvas,
|
|
|
|
gfxImageSurface **imageOut,
|
2010-10-15 14:50:15 -07:00
|
|
|
int *format);
|
2009-09-02 17:47:49 -07:00
|
|
|
|
2011-01-06 14:07:13 -08:00
|
|
|
nsresult CopyTexSubImage2D_base(WebGLenum target,
|
|
|
|
WebGLint level,
|
|
|
|
WebGLenum internalformat,
|
|
|
|
WebGLint xoffset,
|
|
|
|
WebGLint yoffset,
|
|
|
|
WebGLint x,
|
|
|
|
WebGLint y,
|
|
|
|
WebGLsizei width,
|
|
|
|
WebGLsizei height,
|
|
|
|
bool sub
|
|
|
|
);
|
|
|
|
|
2010-06-04 12:03:37 -07:00
|
|
|
// Conversion from public nsI* interfaces to concrete objects
|
|
|
|
template<class ConcreteObjectType, class BaseInterfaceType>
|
2010-07-16 07:30:32 -07:00
|
|
|
PRBool GetConcreteObject(const char *info,
|
|
|
|
BaseInterfaceType *aInterface,
|
2010-06-04 12:03:37 -07:00
|
|
|
ConcreteObjectType **aConcreteObject,
|
|
|
|
PRBool *isNull = 0,
|
2010-07-16 07:30:32 -07:00
|
|
|
PRBool *isDeleted = 0,
|
|
|
|
PRBool generateErrors = PR_TRUE);
|
2010-06-04 12:03:37 -07:00
|
|
|
|
|
|
|
template<class ConcreteObjectType, class BaseInterfaceType>
|
2010-07-16 07:30:32 -07:00
|
|
|
PRBool GetConcreteObjectAndGLName(const char *info,
|
|
|
|
BaseInterfaceType *aInterface,
|
2010-06-04 12:03:37 -07:00
|
|
|
ConcreteObjectType **aConcreteObject,
|
|
|
|
WebGLuint *aGLObjectName,
|
|
|
|
PRBool *isNull = 0,
|
|
|
|
PRBool *isDeleted = 0);
|
|
|
|
|
|
|
|
template<class ConcreteObjectType, class BaseInterfaceType>
|
2010-07-16 07:30:32 -07:00
|
|
|
PRBool GetGLName(const char *info,
|
|
|
|
BaseInterfaceType *aInterface,
|
2010-06-04 12:03:37 -07:00
|
|
|
WebGLuint *aGLObjectName,
|
|
|
|
PRBool *isNull = 0,
|
|
|
|
PRBool *isDeleted = 0);
|
|
|
|
|
|
|
|
template<class ConcreteObjectType, class BaseInterfaceType>
|
2010-07-16 07:30:32 -07:00
|
|
|
PRBool CanGetConcreteObject(const char *info,
|
|
|
|
BaseInterfaceType *aInterface,
|
|
|
|
PRBool *isNull = 0,
|
|
|
|
PRBool *isDeleted = 0);
|
2010-06-04 12:03:37 -07:00
|
|
|
|
2011-02-24 14:17:34 -08:00
|
|
|
PRInt32 MaxTextureSizeForTarget(WebGLenum target) const {
|
|
|
|
return target == LOCAL_GL_TEXTURE_2D ? mGLMaxTextureSize : mGLMaxCubeMapTextureSize;
|
|
|
|
}
|
2010-06-04 12:03:37 -07:00
|
|
|
|
2009-09-02 17:47:49 -07:00
|
|
|
// the buffers bound to the current program's attribs
|
|
|
|
nsTArray<WebGLVertexAttribData> mAttribBuffers;
|
|
|
|
|
|
|
|
// the textures bound to any sampler uniforms
|
|
|
|
nsTArray<WebGLObjectRefPtr<WebGLTexture> > mUniformTextures;
|
|
|
|
|
|
|
|
// textures bound to
|
|
|
|
nsTArray<WebGLObjectRefPtr<WebGLTexture> > mBound2DTextures;
|
|
|
|
nsTArray<WebGLObjectRefPtr<WebGLTexture> > mBoundCubeMapTextures;
|
|
|
|
|
|
|
|
WebGLObjectRefPtr<WebGLBuffer> mBoundArrayBuffer;
|
|
|
|
WebGLObjectRefPtr<WebGLBuffer> mBoundElementArrayBuffer;
|
2011-01-05 13:08:53 -08:00
|
|
|
// note nsRefPtr -- this stays alive even after being deleted,
|
|
|
|
// and is only explicitly removed from the current state via
|
|
|
|
// a call to UseProgram.
|
|
|
|
nsRefPtr<WebGLProgram> mCurrentProgram;
|
2009-09-02 17:47:49 -07:00
|
|
|
|
2010-11-05 12:57:58 -07:00
|
|
|
PRUint32 mMaxFramebufferColorAttachments;
|
2009-09-02 17:47:49 -07:00
|
|
|
|
2009-12-07 15:10:04 -08:00
|
|
|
nsRefPtr<WebGLFramebuffer> mBoundFramebuffer;
|
2009-09-02 17:47:49 -07:00
|
|
|
nsRefPtr<WebGLRenderbuffer> mBoundRenderbuffer;
|
|
|
|
|
|
|
|
// lookup tables for GL name -> object wrapper
|
|
|
|
nsRefPtrHashtable<nsUint32HashKey, WebGLTexture> mMapTextures;
|
|
|
|
nsRefPtrHashtable<nsUint32HashKey, WebGLBuffer> mMapBuffers;
|
|
|
|
nsRefPtrHashtable<nsUint32HashKey, WebGLProgram> mMapPrograms;
|
|
|
|
nsRefPtrHashtable<nsUint32HashKey, WebGLShader> mMapShaders;
|
|
|
|
nsRefPtrHashtable<nsUint32HashKey, WebGLFramebuffer> mMapFramebuffers;
|
|
|
|
nsRefPtrHashtable<nsUint32HashKey, WebGLRenderbuffer> mMapRenderbuffers;
|
|
|
|
|
2010-10-15 14:50:15 -07:00
|
|
|
// PixelStore parameters
|
2010-12-06 03:34:35 -08:00
|
|
|
PRUint32 mPixelStorePackAlignment, mPixelStoreUnpackAlignment, mPixelStoreColorspaceConversion;
|
2010-06-15 08:59:39 -07:00
|
|
|
PRBool mPixelStoreFlipY, mPixelStorePremultiplyAlpha;
|
|
|
|
|
2010-08-23 14:03:53 -07:00
|
|
|
FakeBlackStatus mFakeBlackStatus;
|
|
|
|
|
|
|
|
WebGLuint mBlackTexture2D, mBlackTextureCubeMap;
|
|
|
|
PRBool mBlackTexturesAreInitialized;
|
|
|
|
|
2010-09-02 07:34:08 -07:00
|
|
|
WebGLfloat mVertexAttrib0Vector[4];
|
2011-02-24 14:17:34 -08:00
|
|
|
WebGLfloat mFakeVertexAttrib0BufferObjectVector[4];
|
|
|
|
size_t mFakeVertexAttrib0BufferObjectSize;
|
|
|
|
GLuint mFakeVertexAttrib0BufferObject;
|
|
|
|
int mFakeVertexAttrib0BufferStatus;
|
2010-09-02 07:34:08 -07:00
|
|
|
|
2010-12-06 03:34:35 -08:00
|
|
|
WebGLint mStencilRef;
|
|
|
|
WebGLuint mStencilValueMask, mStencilWriteMask;
|
|
|
|
|
2010-05-19 13:46:08 -07:00
|
|
|
public:
|
2009-09-02 17:47:49 -07:00
|
|
|
// console logging helpers
|
2010-09-13 08:40:01 -07:00
|
|
|
static void LogMessage(const char *fmt, ...);
|
2010-05-15 06:55:45 -07:00
|
|
|
static void LogMessage(const char *fmt, va_list ap);
|
2010-09-13 08:40:01 -07:00
|
|
|
void LogMessageIfVerbose(const char *fmt, ...);
|
2010-10-15 14:50:15 -07:00
|
|
|
void LogMessageIfVerbose(const char *fmt, va_list ap);
|
2010-08-23 14:03:53 -07:00
|
|
|
|
|
|
|
friend class WebGLTexture;
|
2010-11-05 12:57:58 -07:00
|
|
|
friend class WebGLFramebuffer;
|
2009-09-02 17:47:49 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// this class is a mixin for the named type wrappers, and is used
|
|
|
|
// by WebGLObjectRefPtr to tell the object who holds references, so that
|
|
|
|
// we can zero them out appropriately when the object is deleted, because
|
|
|
|
// it will be unbound in the GL.
|
|
|
|
class WebGLZeroingObject
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
WebGLZeroingObject()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
void AddRefOwner(WebGLObjectBaseRefPtr *owner) {
|
|
|
|
mRefOwners.AppendElement(owner);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveRefOwner(WebGLObjectBaseRefPtr *owner) {
|
|
|
|
mRefOwners.RemoveElement(owner);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ZeroOwners() {
|
|
|
|
WebGLObjectBaseRefPtr **owners = mRefOwners.Elements();
|
|
|
|
|
|
|
|
for (PRUint32 i = 0; i < mRefOwners.Length(); i++) {
|
|
|
|
owners[i]->Zero();
|
|
|
|
}
|
|
|
|
|
|
|
|
mRefOwners.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsTArray<WebGLObjectBaseRefPtr *> mRefOwners;
|
|
|
|
};
|
|
|
|
|
2010-06-04 12:03:37 -07:00
|
|
|
// this class is a mixin for GL objects that have dimensions
|
|
|
|
// that we need to track.
|
2010-05-15 05:07:30 -07:00
|
|
|
class WebGLRectangleObject
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
WebGLRectangleObject()
|
|
|
|
: mWidth(0), mHeight(0) { }
|
|
|
|
|
|
|
|
public:
|
2010-05-28 15:52:39 -07:00
|
|
|
WebGLsizei width() { return mWidth; }
|
|
|
|
void width(WebGLsizei value) { mWidth = value; }
|
2010-05-15 05:07:30 -07:00
|
|
|
|
2010-05-28 15:52:39 -07:00
|
|
|
WebGLsizei height() { return mHeight; }
|
|
|
|
void height(WebGLsizei value) { mHeight = value; }
|
2010-05-15 05:07:30 -07:00
|
|
|
|
2010-05-28 15:52:39 -07:00
|
|
|
void setDimensions(WebGLsizei width, WebGLsizei height) {
|
2010-05-15 05:07:30 -07:00
|
|
|
mWidth = width;
|
|
|
|
mHeight = height;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setDimensions(WebGLRectangleObject *rect) {
|
|
|
|
if (rect) {
|
|
|
|
mWidth = rect->width();
|
|
|
|
mHeight = rect->height();
|
|
|
|
} else {
|
|
|
|
mWidth = 0;
|
|
|
|
mHeight = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2010-05-28 15:52:39 -07:00
|
|
|
WebGLsizei mWidth;
|
|
|
|
WebGLsizei mHeight;
|
2010-05-15 05:07:30 -07:00
|
|
|
};
|
|
|
|
|
2010-06-04 12:03:37 -07:00
|
|
|
// This class is a mixin for objects that are tied to a specific
|
|
|
|
// context (which is to say, all of them). They provide initialization
|
|
|
|
// as well as comparison with the current context.
|
|
|
|
class WebGLContextBoundObject
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
WebGLContextBoundObject(WebGLContext *context) {
|
|
|
|
mContext = context;
|
|
|
|
mContextGeneration = context->Generation();
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool IsCompatibleWithContext(WebGLContext *other) {
|
|
|
|
return mContext == other &&
|
|
|
|
mContextGeneration == other->Generation();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
WebGLContext *mContext;
|
|
|
|
PRUint32 mContextGeneration;
|
2010-05-15 05:07:30 -07:00
|
|
|
};
|
|
|
|
|
2010-05-19 13:46:08 -07:00
|
|
|
#define WEBGLBUFFER_PRIVATE_IID \
|
|
|
|
{0xd69f22e9, 0x6f98, 0x48bd, {0xb6, 0x94, 0x34, 0x17, 0xed, 0x06, 0x11, 0xab}}
|
2009-09-02 17:47:49 -07:00
|
|
|
class WebGLBuffer :
|
|
|
|
public nsIWebGLBuffer,
|
2010-06-04 12:03:37 -07:00
|
|
|
public WebGLZeroingObject,
|
|
|
|
public WebGLContextBoundObject
|
2009-09-02 17:47:49 -07:00
|
|
|
{
|
|
|
|
public:
|
2010-05-19 13:46:08 -07:00
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(WEBGLBUFFER_PRIVATE_IID)
|
|
|
|
|
2010-06-04 12:03:37 -07:00
|
|
|
WebGLBuffer(WebGLContext *context, WebGLuint name) :
|
|
|
|
WebGLContextBoundObject(context),
|
2011-02-11 15:11:30 -08:00
|
|
|
mName(name), mDeleted(PR_FALSE), mHasEverBeenBound(PR_FALSE),
|
2010-06-04 12:03:37 -07:00
|
|
|
mByteLength(0), mTarget(LOCAL_GL_NONE), mData(nsnull)
|
2009-09-02 17:47:49 -07:00
|
|
|
{ }
|
|
|
|
|
2010-06-01 23:09:18 -07:00
|
|
|
~WebGLBuffer() {
|
|
|
|
Delete();
|
|
|
|
}
|
|
|
|
|
2009-09-02 17:47:49 -07:00
|
|
|
void Delete() {
|
|
|
|
if (mDeleted)
|
|
|
|
return;
|
|
|
|
ZeroOwners();
|
2010-01-22 13:34:25 -08:00
|
|
|
|
2010-06-01 23:09:18 -07:00
|
|
|
free(mData);
|
|
|
|
mData = nsnull;
|
|
|
|
|
2009-09-02 17:47:49 -07:00
|
|
|
mDeleted = PR_TRUE;
|
2010-01-22 13:34:25 -08:00
|
|
|
mByteLength = 0;
|
2009-09-02 17:47:49 -07:00
|
|
|
}
|
2010-01-22 13:34:25 -08:00
|
|
|
|
2010-06-01 23:09:18 -07:00
|
|
|
PRBool Deleted() const { return mDeleted; }
|
2011-02-11 15:11:30 -08:00
|
|
|
PRBool HasEverBeenBound() { return mHasEverBeenBound; }
|
|
|
|
void SetHasEverBeenBound(PRBool x) { mHasEverBeenBound = x; }
|
2010-06-01 23:09:18 -07:00
|
|
|
GLuint GLName() const { return mName; }
|
|
|
|
GLuint ByteLength() const { return mByteLength; }
|
|
|
|
GLenum Target() const { return mTarget; }
|
|
|
|
const void *Data() const { return mData; }
|
|
|
|
|
|
|
|
void SetByteLength(GLuint byteLength) { mByteLength = byteLength; }
|
|
|
|
void SetTarget(GLenum target) { mTarget = target; }
|
|
|
|
|
|
|
|
// element array buffers are the only buffers for which we need to keep a copy of the data.
|
|
|
|
// this method assumes that the byte length has previously been set by calling SetByteLength.
|
2011-02-24 14:17:34 -08:00
|
|
|
PRBool CopyDataIfElementArray(const void* data) {
|
2010-06-01 23:09:18 -07:00
|
|
|
if (mTarget == LOCAL_GL_ELEMENT_ARRAY_BUFFER) {
|
|
|
|
mData = realloc(mData, mByteLength);
|
2011-02-24 14:17:34 -08:00
|
|
|
if (!mData)
|
|
|
|
return PR_FALSE;
|
2010-06-01 23:09:18 -07:00
|
|
|
memcpy(mData, data, mByteLength);
|
|
|
|
}
|
2011-02-24 14:17:34 -08:00
|
|
|
return PR_TRUE;
|
2010-06-01 23:09:18 -07:00
|
|
|
}
|
2009-09-02 17:47:49 -07:00
|
|
|
|
2010-06-01 23:09:18 -07:00
|
|
|
// same comments as for CopyElementArrayData
|
2011-02-24 14:17:34 -08:00
|
|
|
PRBool ZeroDataIfElementArray() {
|
2010-06-01 23:09:18 -07:00
|
|
|
if (mTarget == LOCAL_GL_ELEMENT_ARRAY_BUFFER) {
|
|
|
|
mData = realloc(mData, mByteLength);
|
2011-02-24 14:17:34 -08:00
|
|
|
if (!mData)
|
|
|
|
return PR_FALSE;
|
2010-06-01 23:09:18 -07:00
|
|
|
memset(mData, 0, mByteLength);
|
|
|
|
}
|
2011-02-24 14:17:34 -08:00
|
|
|
return PR_TRUE;
|
2010-06-01 23:09:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// same comments as for CopyElementArrayData
|
|
|
|
void CopySubDataIfElementArray(GLuint byteOffset, GLuint byteLength, const void* data) {
|
|
|
|
if (mTarget == LOCAL_GL_ELEMENT_ARRAY_BUFFER) {
|
|
|
|
memcpy((void*) (size_t(mData)+byteOffset), data, byteLength);
|
|
|
|
}
|
|
|
|
}
|
2009-09-02 17:47:49 -07:00
|
|
|
|
2010-06-01 23:09:18 -07:00
|
|
|
// this method too is only for element array buffers. It returns the maximum value in the part of
|
|
|
|
// the buffer starting at given offset, consisting of given count of elements. The type T is the type
|
|
|
|
// to interprete the array elements as, must be GLushort or GLubyte.
|
|
|
|
template<typename T>
|
2011-01-25 19:19:46 -08:00
|
|
|
PRInt32 FindMaxElementInSubArray(GLuint count, GLuint byteOffset)
|
2010-06-01 23:09:18 -07:00
|
|
|
{
|
|
|
|
const T* start = reinterpret_cast<T*>(reinterpret_cast<size_t>(mData) + byteOffset);
|
|
|
|
const T* stop = start + count;
|
|
|
|
T result = 0;
|
|
|
|
for(const T* ptr = start; ptr != stop; ++ptr) {
|
|
|
|
if (*ptr > result) result = *ptr;
|
|
|
|
}
|
|
|
|
return result;
|
2009-09-02 17:47:49 -07:00
|
|
|
}
|
|
|
|
|
2011-01-25 19:19:46 -08:00
|
|
|
void InvalidateCachedMaxElements() {
|
|
|
|
mHasCachedMaxUbyteElement = PR_FALSE;
|
|
|
|
mHasCachedMaxUshortElement = PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32 FindMaxUbyteElement() {
|
|
|
|
if (mHasCachedMaxUbyteElement) {
|
|
|
|
return mCachedMaxUbyteElement;
|
|
|
|
} else {
|
|
|
|
mHasCachedMaxUbyteElement = PR_TRUE;
|
|
|
|
mCachedMaxUbyteElement = FindMaxElementInSubArray<GLubyte>(mByteLength, 0);
|
|
|
|
return mCachedMaxUbyteElement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32 FindMaxUshortElement() {
|
|
|
|
if (mHasCachedMaxUshortElement) {
|
|
|
|
return mCachedMaxUshortElement;
|
|
|
|
} else {
|
|
|
|
mHasCachedMaxUshortElement = PR_TRUE;
|
|
|
|
mCachedMaxUshortElement = FindMaxElementInSubArray<GLshort>(mByteLength>>1, 0);
|
|
|
|
return mCachedMaxUshortElement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-09-02 17:47:49 -07:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIWEBGLBUFFER
|
|
|
|
protected:
|
2010-05-28 15:52:39 -07:00
|
|
|
WebGLuint mName;
|
2009-09-02 17:47:49 -07:00
|
|
|
PRBool mDeleted;
|
2011-02-11 15:11:30 -08:00
|
|
|
PRBool mHasEverBeenBound;
|
2010-06-01 23:09:18 -07:00
|
|
|
GLuint mByteLength;
|
|
|
|
GLenum mTarget;
|
2011-01-25 19:19:46 -08:00
|
|
|
|
|
|
|
PRUint8 mCachedMaxUbyteElement;
|
|
|
|
PRBool mHasCachedMaxUbyteElement;
|
|
|
|
PRUint16 mCachedMaxUshortElement;
|
|
|
|
PRBool mHasCachedMaxUshortElement;
|
2011-02-11 15:11:30 -08:00
|
|
|
|
2010-06-01 23:09:18 -07:00
|
|
|
void* mData; // in the case of an Element Array Buffer, we keep a copy.
|
2009-09-02 17:47:49 -07:00
|
|
|
};
|
|
|
|
|
2010-05-19 13:46:08 -07:00
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(WebGLBuffer, WEBGLBUFFER_PRIVATE_IID)
|
|
|
|
|
|
|
|
#define WEBGLTEXTURE_PRIVATE_IID \
|
|
|
|
{0x4c19f189, 0x1f86, 0x4e61, {0x96, 0x21, 0x0a, 0x11, 0xda, 0x28, 0x10, 0xdd}}
|
2009-09-02 17:47:49 -07:00
|
|
|
class WebGLTexture :
|
|
|
|
public nsIWebGLTexture,
|
2010-05-15 05:07:30 -07:00
|
|
|
public WebGLZeroingObject,
|
2010-06-04 12:03:37 -07:00
|
|
|
public WebGLRectangleObject,
|
|
|
|
public WebGLContextBoundObject
|
2009-09-02 17:47:49 -07:00
|
|
|
{
|
|
|
|
public:
|
2010-05-19 13:46:08 -07:00
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(WEBGLTEXTURE_PRIVATE_IID)
|
|
|
|
|
2010-06-04 12:03:37 -07:00
|
|
|
WebGLTexture(WebGLContext *context, WebGLuint name) :
|
|
|
|
WebGLContextBoundObject(context),
|
2011-02-11 15:11:30 -08:00
|
|
|
mDeleted(PR_FALSE), mHasEverBeenBound(PR_FALSE), mName(name),
|
2010-08-23 14:03:53 -07:00
|
|
|
mTarget(0),
|
|
|
|
mMinFilter(LOCAL_GL_NEAREST_MIPMAP_LINEAR),
|
|
|
|
mMagFilter(LOCAL_GL_LINEAR),
|
|
|
|
mWrapS(LOCAL_GL_REPEAT),
|
|
|
|
mWrapT(LOCAL_GL_REPEAT),
|
|
|
|
mFacesCount(0),
|
|
|
|
mMaxLevelWithCustomImages(0),
|
|
|
|
mHaveGeneratedMipmap(PR_FALSE),
|
2010-09-02 07:29:41 -07:00
|
|
|
mFakeBlackStatus(DoNotNeedFakeBlack)
|
2010-12-06 03:34:35 -08:00
|
|
|
{
|
|
|
|
}
|
2009-09-02 17:47:49 -07:00
|
|
|
|
|
|
|
void Delete() {
|
|
|
|
if (mDeleted)
|
|
|
|
return;
|
|
|
|
ZeroOwners();
|
|
|
|
mDeleted = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool Deleted() { return mDeleted; }
|
2011-02-11 15:11:30 -08:00
|
|
|
PRBool HasEverBeenBound() { return mHasEverBeenBound; }
|
|
|
|
void SetHasEverBeenBound(PRBool x) { mHasEverBeenBound = x; }
|
2010-05-28 15:52:39 -07:00
|
|
|
WebGLuint GLName() { return mName; }
|
2009-09-02 17:47:49 -07:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIWEBGLTEXTURE
|
2010-08-23 14:03:53 -07:00
|
|
|
|
2009-09-02 17:47:49 -07:00
|
|
|
protected:
|
2010-11-16 20:33:03 -08:00
|
|
|
friend class WebGLContext;
|
|
|
|
friend class WebGLFramebuffer;
|
|
|
|
|
2009-09-02 17:47:49 -07:00
|
|
|
PRBool mDeleted;
|
2011-02-11 15:11:30 -08:00
|
|
|
PRBool mHasEverBeenBound;
|
2010-08-23 14:03:53 -07:00
|
|
|
WebGLuint mName;
|
|
|
|
|
2010-11-16 20:33:03 -08:00
|
|
|
// we store information about the various images that are part of
|
|
|
|
// this texture (cubemap faces, mipmap levels)
|
2010-08-23 14:03:53 -07:00
|
|
|
|
|
|
|
struct ImageInfo {
|
|
|
|
ImageInfo() : mWidth(0), mHeight(0), mFormat(0), mType(0), mIsDefined(PR_FALSE) {}
|
|
|
|
PRBool operator==(const ImageInfo& a) const {
|
|
|
|
return mWidth == a.mWidth && mHeight == a.mHeight &&
|
|
|
|
mFormat == a.mFormat && mType == a.mType;
|
|
|
|
}
|
|
|
|
PRBool operator!=(const ImageInfo& a) const {
|
|
|
|
return !(*this == a);
|
|
|
|
}
|
|
|
|
PRBool IsSquare() const {
|
|
|
|
return mWidth == mHeight;
|
|
|
|
}
|
|
|
|
PRBool IsPositive() const {
|
|
|
|
return mWidth > 0 && mHeight > 0;
|
|
|
|
}
|
|
|
|
PRBool IsPowerOfTwo() const {
|
|
|
|
return is_pot_assuming_nonnegative(mWidth) &&
|
|
|
|
is_pot_assuming_nonnegative(mHeight); // negative sizes should never happen (caught in texImage2D...)
|
|
|
|
}
|
|
|
|
WebGLsizei mWidth, mHeight;
|
|
|
|
WebGLenum mFormat, mType;
|
|
|
|
PRBool mIsDefined;
|
|
|
|
};
|
|
|
|
|
2010-12-06 03:34:35 -08:00
|
|
|
public:
|
|
|
|
|
2011-05-20 12:53:53 -07:00
|
|
|
ImageInfo& ImageInfoAt(size_t level, size_t face = 0) {
|
2010-08-23 14:03:53 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (face >= mFacesCount)
|
|
|
|
NS_ERROR("wrong face index, must be 0 for TEXTURE_2D and at most 5 for cube maps");
|
|
|
|
#endif
|
|
|
|
// no need to check level as a wrong value would be caught by ElementAt().
|
|
|
|
return mImageInfos.ElementAt(level * mFacesCount + face);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ImageInfo& ImageInfoAt(size_t level, size_t face) const {
|
|
|
|
return const_cast<WebGLTexture*>(this)->ImageInfoAt(level, face);
|
|
|
|
}
|
|
|
|
|
2011-02-24 14:17:34 -08:00
|
|
|
PRBool HasImageInfoAt(size_t level, size_t face) const {
|
|
|
|
return level <= mMaxLevelWithCustomImages &&
|
|
|
|
face < mFacesCount &&
|
|
|
|
ImageInfoAt(level, 0).mIsDefined;
|
|
|
|
}
|
|
|
|
|
2011-02-24 14:17:34 -08:00
|
|
|
static size_t FaceForTarget(WebGLenum target) {
|
|
|
|
return target == LOCAL_GL_TEXTURE_2D ? 0 : target - LOCAL_GL_TEXTURE_CUBE_MAP_POSITIVE_X;
|
|
|
|
}
|
|
|
|
|
2010-12-06 03:34:35 -08:00
|
|
|
protected:
|
|
|
|
|
2010-08-23 14:03:53 -07:00
|
|
|
WebGLenum mTarget;
|
|
|
|
WebGLenum mMinFilter, mMagFilter, mWrapS, mWrapT;
|
|
|
|
|
|
|
|
size_t mFacesCount, mMaxLevelWithCustomImages;
|
|
|
|
nsTArray<ImageInfo> mImageInfos;
|
|
|
|
|
|
|
|
PRBool mHaveGeneratedMipmap;
|
|
|
|
FakeBlackStatus mFakeBlackStatus;
|
|
|
|
|
|
|
|
void EnsureMaxLevelWithCustomImagesAtLeast(size_t aMaxLevelWithCustomImages) {
|
|
|
|
mMaxLevelWithCustomImages = PR_MAX(mMaxLevelWithCustomImages, aMaxLevelWithCustomImages);
|
|
|
|
mImageInfos.EnsureLengthAtLeast((mMaxLevelWithCustomImages + 1) * mFacesCount);
|
|
|
|
}
|
|
|
|
|
2011-05-20 12:53:53 -07:00
|
|
|
PRBool 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);
|
|
|
|
}
|
|
|
|
|
2010-08-23 14:03:53 -07:00
|
|
|
PRBool DoesMinFilterRequireMipmap() const {
|
|
|
|
return !(mMinFilter == LOCAL_GL_NEAREST || mMinFilter == LOCAL_GL_LINEAR);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool AreBothWrapModesClampToEdge() const {
|
|
|
|
return mWrapS == LOCAL_GL_CLAMP_TO_EDGE && mWrapT == LOCAL_GL_CLAMP_TO_EDGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool DoesTexture2DMipmapHaveAllLevelsConsistentlyDefined(size_t face) const {
|
|
|
|
if (mHaveGeneratedMipmap)
|
|
|
|
return PR_TRUE;
|
|
|
|
|
|
|
|
ImageInfo expected = ImageInfoAt(0, face);
|
|
|
|
|
|
|
|
// checks if custom level>0 images are all defined up to the highest level defined
|
|
|
|
// and have the expected dimensions
|
|
|
|
for (size_t level = 0; level <= mMaxLevelWithCustomImages; ++level) {
|
|
|
|
const ImageInfo& actual = ImageInfoAt(level, face);
|
|
|
|
if (actual != expected)
|
|
|
|
return PR_FALSE;
|
|
|
|
expected.mWidth = PR_MAX(1, expected.mWidth >> 1);
|
|
|
|
expected.mHeight = PR_MAX(1, expected.mHeight >> 1);
|
|
|
|
|
|
|
|
// if the current level has size 1x1, we can stop here: the spec doesn't seem to forbid the existence
|
|
|
|
// of extra useless levels.
|
|
|
|
if (actual.mWidth == 1 && actual.mHeight == 1)
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we're here, we've exhausted all levels without finding a 1x1 image
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
void SetDontKnowIfNeedFakeBlack() {
|
|
|
|
mFakeBlackStatus = DontKnowIfNeedFakeBlack;
|
|
|
|
mContext->SetDontKnowIfNeedFakeBlack();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Bind(WebGLenum aTarget) {
|
|
|
|
// this function should only be called by bindTexture().
|
|
|
|
// it assumes that the GL context is already current.
|
|
|
|
|
2011-02-11 15:11:30 -08:00
|
|
|
PRBool firstTimeThisTextureIsBound = !mHasEverBeenBound;
|
2010-08-23 14:03:53 -07:00
|
|
|
|
|
|
|
if (!firstTimeThisTextureIsBound && aTarget != mTarget) {
|
|
|
|
mContext->ErrorInvalidOperation("bindTexture: this texture has already been bound to a different target");
|
|
|
|
// very important to return here before modifying texture state! This was the place when I lost a whole day figuring
|
|
|
|
// very strange 'invalid write' crashes.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mTarget = aTarget;
|
|
|
|
|
|
|
|
mContext->gl->fBindTexture(mTarget, mName);
|
|
|
|
|
|
|
|
if (firstTimeThisTextureIsBound) {
|
|
|
|
mFacesCount = (mTarget == LOCAL_GL_TEXTURE_2D) ? 1 : 6;
|
|
|
|
EnsureMaxLevelWithCustomImagesAtLeast(0);
|
|
|
|
SetDontKnowIfNeedFakeBlack();
|
|
|
|
|
|
|
|
// thanks to the WebKit people for finding this out: GL_TEXTURE_WRAP_R is not
|
|
|
|
// present in GLES 2, but is present in GL and it seems as if for cube maps
|
|
|
|
// we need to set it to GL_CLAMP_TO_EDGE to get the expected GLES behavior.
|
|
|
|
if (mTarget == LOCAL_GL_TEXTURE_CUBE_MAP && !mContext->gl->IsGLES2())
|
|
|
|
mContext->gl->fTexParameteri(mTarget, LOCAL_GL_TEXTURE_WRAP_R, LOCAL_GL_CLAMP_TO_EDGE);
|
|
|
|
}
|
2011-02-11 15:11:30 -08:00
|
|
|
|
|
|
|
mHasEverBeenBound = PR_TRUE;
|
2010-08-23 14:03:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetImageInfo(WebGLenum aTarget, WebGLint aLevel,
|
|
|
|
WebGLsizei aWidth, WebGLsizei aHeight,
|
2011-02-24 14:17:34 -08:00
|
|
|
WebGLenum aFormat = 0, WebGLenum aType = 0)
|
|
|
|
{
|
|
|
|
if ( (aTarget == LOCAL_GL_TEXTURE_2D) != (mTarget == LOCAL_GL_TEXTURE_2D) )
|
|
|
|
return;
|
|
|
|
|
|
|
|
size_t face = FaceForTarget(aTarget);
|
2010-08-23 14:03:53 -07:00
|
|
|
|
|
|
|
EnsureMaxLevelWithCustomImagesAtLeast(aLevel);
|
|
|
|
|
|
|
|
ImageInfo& imageInfo = ImageInfoAt(aLevel, face);
|
|
|
|
imageInfo.mWidth = aWidth;
|
|
|
|
imageInfo.mHeight = aHeight;
|
|
|
|
if (aFormat)
|
|
|
|
imageInfo.mFormat = aFormat;
|
|
|
|
if (aType)
|
|
|
|
imageInfo.mType = aType;
|
|
|
|
imageInfo.mIsDefined = PR_TRUE;
|
|
|
|
|
|
|
|
if (aLevel > 0)
|
|
|
|
SetCustomMipmap();
|
|
|
|
|
|
|
|
SetDontKnowIfNeedFakeBlack();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetMinFilter(WebGLenum aMinFilter) {
|
|
|
|
mMinFilter = aMinFilter;
|
|
|
|
SetDontKnowIfNeedFakeBlack();
|
|
|
|
}
|
|
|
|
void SetMagFilter(WebGLenum aMagFilter) {
|
|
|
|
mMagFilter = aMagFilter;
|
|
|
|
SetDontKnowIfNeedFakeBlack();
|
|
|
|
}
|
|
|
|
void SetWrapS(WebGLenum aWrapS) {
|
|
|
|
mWrapS = aWrapS;
|
|
|
|
SetDontKnowIfNeedFakeBlack();
|
|
|
|
}
|
|
|
|
void SetWrapT(WebGLenum aWrapT) {
|
|
|
|
mWrapT = aWrapT;
|
|
|
|
SetDontKnowIfNeedFakeBlack();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetGeneratedMipmap() {
|
|
|
|
if (!mHaveGeneratedMipmap) {
|
|
|
|
mHaveGeneratedMipmap = PR_TRUE;
|
|
|
|
SetDontKnowIfNeedFakeBlack();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetCustomMipmap() {
|
|
|
|
if (mHaveGeneratedMipmap) {
|
|
|
|
// if we were in GeneratedMipmap mode and are now switching to CustomMipmap mode,
|
|
|
|
// we need to compute now all the mipmap image info.
|
|
|
|
|
|
|
|
// since we were in GeneratedMipmap mode, we know that the level 0 images all have the same info,
|
|
|
|
// and are power-of-two.
|
|
|
|
ImageInfo imageInfo = ImageInfoAt(0, 0);
|
|
|
|
NS_ASSERTION(imageInfo.IsPowerOfTwo(), "this texture is NPOT, so how could GenerateMipmap() ever accept it?");
|
|
|
|
|
|
|
|
WebGLsizei size = PR_MAX(imageInfo.mWidth, imageInfo.mHeight);
|
|
|
|
|
|
|
|
// so, the size is a power of two, let's find its log in base 2.
|
|
|
|
size_t maxLevel = 0;
|
|
|
|
for (WebGLsizei n = size; n > 1; n >>= 1)
|
|
|
|
++maxLevel;
|
|
|
|
|
|
|
|
EnsureMaxLevelWithCustomImagesAtLeast(maxLevel);
|
|
|
|
|
|
|
|
for (size_t level = 1; level <= maxLevel; ++level) {
|
|
|
|
// again, since the sizes are powers of two, no need for any max(1,x) computation
|
|
|
|
imageInfo.mWidth >>= 1;
|
|
|
|
imageInfo.mHeight >>= 1;
|
|
|
|
for(size_t face = 0; face < mFacesCount; ++face)
|
|
|
|
ImageInfoAt(level, face) = imageInfo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mHaveGeneratedMipmap = PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-09-02 07:29:41 -07:00
|
|
|
PRBool IsFirstImagePowerOfTwo() const {
|
|
|
|
return ImageInfoAt(0, 0).IsPowerOfTwo();
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool AreAllLevel0ImageInfosEqual() const {
|
|
|
|
for (size_t face = 1; face < mFacesCount; ++face) {
|
|
|
|
if (ImageInfoAt(0, face) != ImageInfoAt(0, 0))
|
2010-08-23 14:03:53 -07:00
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool IsMipmapTexture2DComplete() const {
|
|
|
|
if (mTarget != LOCAL_GL_TEXTURE_2D)
|
|
|
|
return PR_FALSE;
|
2010-09-02 07:29:41 -07:00
|
|
|
if (!ImageInfoAt(0, 0).IsPositive())
|
2010-08-23 14:03:53 -07:00
|
|
|
return PR_FALSE;
|
|
|
|
if (mHaveGeneratedMipmap)
|
|
|
|
return PR_TRUE;
|
|
|
|
return DoesTexture2DMipmapHaveAllLevelsConsistentlyDefined(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool IsCubeComplete() const {
|
|
|
|
if (mTarget != LOCAL_GL_TEXTURE_CUBE_MAP)
|
|
|
|
return PR_FALSE;
|
|
|
|
const ImageInfo &first = ImageInfoAt(0, 0);
|
|
|
|
if (!first.IsPositive() || !first.IsSquare())
|
|
|
|
return PR_FALSE;
|
2010-09-02 07:29:41 -07:00
|
|
|
return AreAllLevel0ImageInfosEqual();
|
2010-08-23 14:03:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
PRBool IsMipmapCubeComplete() const {
|
|
|
|
if (!IsCubeComplete()) // in particular, this checks that this is a cube map
|
|
|
|
return PR_FALSE;
|
|
|
|
for (size_t face = 0; face < mFacesCount; ++face) {
|
|
|
|
if (!DoesTexture2DMipmapHaveAllLevelsConsistentlyDefined(face))
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool NeedFakeBlack() {
|
|
|
|
// handle this case first, it's the generic case
|
|
|
|
if (mFakeBlackStatus == DoNotNeedFakeBlack)
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
if (mFakeBlackStatus == DontKnowIfNeedFakeBlack) {
|
|
|
|
// Determine if the texture needs to be faked as a black texture.
|
|
|
|
// See 3.8.2 Shader Execution in the OpenGL ES 2.0.24 spec.
|
|
|
|
|
2010-10-15 14:50:15 -07:00
|
|
|
for (size_t face = 0; face < mFacesCount; ++face) {
|
2010-12-06 03:34:35 -08:00
|
|
|
if (!ImageInfoAt(0, face).mIsDefined) {
|
|
|
|
// In case of undefined texture image, we don't print any message because this is a very common
|
|
|
|
// and often legitimate case, for example when doing asynchronous texture loading.
|
|
|
|
// An extreme case of this is the photowall google demo.
|
|
|
|
// Exiting early here allows us to avoid making noise on valid webgl code.
|
|
|
|
mFakeBlackStatus = DoNeedFakeBlack;
|
|
|
|
return PR_TRUE;
|
2010-10-15 14:50:15 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-02 07:29:41 -07:00
|
|
|
const char *msg_rendering_as_black
|
|
|
|
= "A texture is going to be rendered as if it were black, as per the OpenGL ES 2.0.24 spec section 3.8.2, "
|
|
|
|
"because it";
|
|
|
|
|
2010-08-23 14:03:53 -07:00
|
|
|
if (mTarget == LOCAL_GL_TEXTURE_2D)
|
|
|
|
{
|
|
|
|
if (DoesMinFilterRequireMipmap())
|
|
|
|
{
|
2010-09-02 07:29:41 -07:00
|
|
|
if (!IsMipmapTexture2DComplete()) {
|
2010-09-13 08:40:01 -07:00
|
|
|
mContext->LogMessageIfVerbose
|
|
|
|
("%s is a 2D texture, with a minification filter requiring a mipmap, "
|
|
|
|
"and is not mipmap complete (as defined in section 3.7.10).", msg_rendering_as_black);
|
2010-09-02 07:29:41 -07:00
|
|
|
mFakeBlackStatus = DoNeedFakeBlack;
|
2011-05-20 12:53:53 -07:00
|
|
|
} else if (!ImageInfoAt(0).IsPowerOfTwo()) {
|
2010-09-13 08:40:01 -07:00
|
|
|
mContext->LogMessageIfVerbose
|
|
|
|
("%s is a 2D texture, with a minification filter requiring a mipmap, "
|
|
|
|
"and either its width or height is not a power of two.", msg_rendering_as_black);
|
2010-08-23 14:03:53 -07:00
|
|
|
mFakeBlackStatus = DoNeedFakeBlack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // no mipmap required
|
|
|
|
{
|
2011-05-20 12:53:53 -07:00
|
|
|
if (!ImageInfoAt(0).IsPositive()) {
|
2010-09-13 08:40:01 -07:00
|
|
|
mContext->LogMessageIfVerbose
|
|
|
|
("%s is a 2D texture and its width or height is equal to zero.",
|
|
|
|
msg_rendering_as_black);
|
2010-09-02 07:29:41 -07:00
|
|
|
mFakeBlackStatus = DoNeedFakeBlack;
|
2011-05-20 12:53:53 -07:00
|
|
|
} else if (!AreBothWrapModesClampToEdge() && !ImageInfoAt(0).IsPowerOfTwo()) {
|
2010-09-13 08:40:01 -07:00
|
|
|
mContext->LogMessageIfVerbose
|
|
|
|
("%s is a 2D texture, with a minification filter not requiring a mipmap, "
|
|
|
|
"with its width or height not a power of two, and with a wrap mode "
|
|
|
|
"different from CLAMP_TO_EDGE.", msg_rendering_as_black);
|
2010-08-23 14:03:53 -07:00
|
|
|
mFakeBlackStatus = DoNeedFakeBlack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-09-02 07:29:41 -07:00
|
|
|
else // cube map
|
2010-08-23 14:03:53 -07:00
|
|
|
{
|
|
|
|
PRBool areAllLevel0ImagesPOT = PR_TRUE;
|
|
|
|
for (size_t face = 0; face < mFacesCount; ++face)
|
|
|
|
areAllLevel0ImagesPOT &= ImageInfoAt(0, face).IsPowerOfTwo();
|
|
|
|
|
|
|
|
if (DoesMinFilterRequireMipmap())
|
|
|
|
{
|
2010-09-02 07:29:41 -07:00
|
|
|
if (!IsMipmapCubeComplete()) {
|
2010-09-13 08:40:01 -07:00
|
|
|
mContext->LogMessageIfVerbose("%s is a cube map texture, with a minification filter requiring a mipmap, "
|
2010-09-02 07:29:41 -07:00
|
|
|
"and is not mipmap cube complete (as defined in section 3.7.10).",
|
|
|
|
msg_rendering_as_black);
|
|
|
|
mFakeBlackStatus = DoNeedFakeBlack;
|
|
|
|
} else if (!areAllLevel0ImagesPOT) {
|
2010-09-13 08:40:01 -07:00
|
|
|
mContext->LogMessageIfVerbose("%s is a cube map texture, with a minification filter requiring a mipmap, "
|
2010-09-02 07:29:41 -07:00
|
|
|
"and either the width or the height of some level 0 image is not a power of two.",
|
|
|
|
msg_rendering_as_black);
|
2010-08-23 14:03:53 -07:00
|
|
|
mFakeBlackStatus = DoNeedFakeBlack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // no mipmap required
|
|
|
|
{
|
2010-09-02 07:29:41 -07:00
|
|
|
if (!IsCubeComplete()) {
|
2010-09-13 08:40:01 -07:00
|
|
|
mContext->LogMessageIfVerbose("%s is a cube map texture, with a minification filter not requiring a mipmap, "
|
2010-09-02 07:29:41 -07:00
|
|
|
"and is not cube complete (as defined in section 3.7.10).",
|
|
|
|
msg_rendering_as_black);
|
|
|
|
mFakeBlackStatus = DoNeedFakeBlack;
|
|
|
|
} else if (!AreBothWrapModesClampToEdge() && !areAllLevel0ImagesPOT) {
|
2010-09-13 08:40:01 -07:00
|
|
|
mContext->LogMessageIfVerbose("%s is a cube map texture, with a minification filter not requiring a mipmap, "
|
2010-09-02 07:29:41 -07:00
|
|
|
"with some level 0 image having width or height not a power of two, and with a wrap mode "
|
|
|
|
"different from CLAMP_TO_EDGE.", msg_rendering_as_black);
|
2010-08-23 14:03:53 -07:00
|
|
|
mFakeBlackStatus = DoNeedFakeBlack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we have exhausted all cases where we do need fakeblack, so if the status is still unknown,
|
|
|
|
// that means that we do NOT need it.
|
|
|
|
if (mFakeBlackStatus == DontKnowIfNeedFakeBlack)
|
|
|
|
mFakeBlackStatus = DoNotNeedFakeBlack;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mFakeBlackStatus == DoNeedFakeBlack;
|
|
|
|
}
|
2009-09-02 17:47:49 -07:00
|
|
|
};
|
|
|
|
|
2010-05-19 13:46:08 -07:00
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(WebGLTexture, WEBGLTEXTURE_PRIVATE_IID)
|
|
|
|
|
2010-06-04 12:03:33 -07:00
|
|
|
#define WEBGLSHADER_PRIVATE_IID \
|
|
|
|
{0x48cce975, 0xd459, 0x4689, {0x83, 0x82, 0x37, 0x82, 0x6e, 0xac, 0xe0, 0xa7}}
|
|
|
|
class WebGLShader :
|
|
|
|
public nsIWebGLShader,
|
2010-06-04 12:03:37 -07:00
|
|
|
public WebGLZeroingObject,
|
|
|
|
public WebGLContextBoundObject
|
2009-09-02 17:47:49 -07:00
|
|
|
{
|
|
|
|
public:
|
2010-06-04 12:03:33 -07:00
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(WEBGLSHADER_PRIVATE_IID)
|
2010-05-19 13:46:08 -07:00
|
|
|
|
2010-06-04 12:03:37 -07:00
|
|
|
WebGLShader(WebGLContext *context, WebGLuint name, WebGLenum stype) :
|
|
|
|
WebGLContextBoundObject(context),
|
2010-07-14 20:52:34 -07:00
|
|
|
mName(name), mDeleted(PR_FALSE), mType(stype),
|
2011-01-05 13:08:53 -08:00
|
|
|
mNeedsTranslation(true), mAttachCount(0)
|
2010-06-04 12:03:33 -07:00
|
|
|
{ }
|
2009-09-02 17:47:49 -07:00
|
|
|
|
|
|
|
void Delete() {
|
|
|
|
if (mDeleted)
|
|
|
|
return;
|
|
|
|
ZeroOwners();
|
|
|
|
mDeleted = PR_TRUE;
|
|
|
|
}
|
2010-06-04 12:03:37 -07:00
|
|
|
|
2011-01-05 13:08:53 -08:00
|
|
|
PRBool Deleted() { return mDeleted && mAttachCount == 0; }
|
2010-05-28 15:52:39 -07:00
|
|
|
WebGLuint GLName() { return mName; }
|
2010-06-04 12:03:33 -07:00
|
|
|
WebGLenum ShaderType() { return mType; }
|
2009-09-02 17:47:49 -07:00
|
|
|
|
2011-01-05 13:08:53 -08:00
|
|
|
PRUint32 AttachCount() { return mAttachCount; }
|
|
|
|
void IncrementAttachCount() { mAttachCount++; }
|
|
|
|
void DecrementAttachCount() { mAttachCount--; }
|
|
|
|
|
2010-07-14 20:52:34 -07:00
|
|
|
void SetSource(const nsCString& src) {
|
|
|
|
// XXX do some quick gzip here maybe -- getting this will be very rare
|
|
|
|
mSource.Assign(src);
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsCString& Source() const { return mSource; }
|
|
|
|
|
|
|
|
void SetNeedsTranslation() { mNeedsTranslation = true; }
|
|
|
|
bool NeedsTranslation() const { return mNeedsTranslation; }
|
|
|
|
|
|
|
|
void SetTranslationSuccess() {
|
|
|
|
mTranslationLog.SetIsVoid(PR_TRUE);
|
|
|
|
mNeedsTranslation = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetTranslationFailure(const nsCString& msg) {
|
|
|
|
mTranslationLog.Assign(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
const nsCString& TranslationLog() const { return mTranslationLog; }
|
|
|
|
|
2009-09-02 17:47:49 -07:00
|
|
|
NS_DECL_ISUPPORTS
|
2010-06-04 12:03:33 -07:00
|
|
|
NS_DECL_NSIWEBGLSHADER
|
2009-09-02 17:47:49 -07:00
|
|
|
protected:
|
2010-05-28 15:52:39 -07:00
|
|
|
WebGLuint mName;
|
2009-09-02 17:47:49 -07:00
|
|
|
PRBool mDeleted;
|
2010-06-04 12:03:33 -07:00
|
|
|
WebGLenum mType;
|
2010-07-14 20:52:34 -07:00
|
|
|
nsCString mSource;
|
|
|
|
nsCString mTranslationLog;
|
|
|
|
bool mNeedsTranslation;
|
2011-01-05 13:08:53 -08:00
|
|
|
PRUint32 mAttachCount;
|
2009-09-02 17:47:49 -07:00
|
|
|
};
|
|
|
|
|
2010-06-04 12:03:33 -07:00
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(WebGLShader, WEBGLSHADER_PRIVATE_IID)
|
2010-05-19 13:46:08 -07:00
|
|
|
|
2010-06-04 12:03:33 -07:00
|
|
|
#define WEBGLPROGRAM_PRIVATE_IID \
|
|
|
|
{0xb3084a5b, 0xa5b4, 0x4ee0, {0xa0, 0xf0, 0xfb, 0xdd, 0x64, 0xaf, 0x8e, 0x82}}
|
|
|
|
class WebGLProgram :
|
|
|
|
public nsIWebGLProgram,
|
2010-06-04 12:03:37 -07:00
|
|
|
public WebGLZeroingObject,
|
|
|
|
public WebGLContextBoundObject
|
2009-09-02 17:47:49 -07:00
|
|
|
{
|
|
|
|
public:
|
2010-06-04 12:03:33 -07:00
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(WEBGLPROGRAM_PRIVATE_IID)
|
2010-05-19 13:46:08 -07:00
|
|
|
|
2010-06-04 12:03:37 -07:00
|
|
|
WebGLProgram(WebGLContext *context, WebGLuint name) :
|
|
|
|
WebGLContextBoundObject(context),
|
2011-01-05 13:08:53 -08:00
|
|
|
mName(name), mDeleted(PR_FALSE), mDeletePending(PR_FALSE),
|
|
|
|
mLinkStatus(PR_FALSE), mGeneration(0),
|
2010-06-10 10:45:00 -07:00
|
|
|
mUniformMaxNameLength(0), mAttribMaxNameLength(0),
|
|
|
|
mUniformCount(0), mAttribCount(0)
|
2010-06-08 14:25:27 -07:00
|
|
|
{
|
|
|
|
mMapUniformLocations.Init();
|
|
|
|
}
|
2009-09-02 17:47:49 -07:00
|
|
|
|
|
|
|
void Delete() {
|
|
|
|
if (mDeleted)
|
|
|
|
return;
|
|
|
|
ZeroOwners();
|
|
|
|
mDeleted = PR_TRUE;
|
|
|
|
}
|
2010-06-04 12:03:33 -07:00
|
|
|
|
2011-01-05 13:08:53 -08:00
|
|
|
void DetachShaders() {
|
|
|
|
for (PRUint32 i = 0; i < mAttachedShaders.Length(); ++i) {
|
2011-05-06 11:44:23 -07:00
|
|
|
if (mAttachedShaders[i])
|
|
|
|
mAttachedShaders[i]->DecrementAttachCount();
|
2011-01-05 13:08:53 -08:00
|
|
|
}
|
|
|
|
mAttachedShaders.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool Deleted() { return mDeleted && !mDeletePending; }
|
|
|
|
void SetDeletePending() { mDeletePending = PR_TRUE; }
|
|
|
|
void ClearDeletePending() { mDeletePending = PR_FALSE; }
|
|
|
|
PRBool HasDeletePending() { return mDeletePending; }
|
|
|
|
|
2010-05-28 15:52:39 -07:00
|
|
|
WebGLuint GLName() { return mName; }
|
2011-05-06 11:44:23 -07:00
|
|
|
const nsTArray<nsRefPtr<WebGLShader> >& AttachedShaders() const { return mAttachedShaders; }
|
2010-06-04 12:03:33 -07:00
|
|
|
PRBool LinkStatus() { return mLinkStatus; }
|
2010-07-03 15:32:19 -07:00
|
|
|
PRUint32 Generation() const { return mGeneration.value(); }
|
2010-06-04 12:03:33 -07:00
|
|
|
void SetLinkStatus(PRBool val) { mLinkStatus = val; }
|
|
|
|
|
|
|
|
PRBool ContainsShader(WebGLShader *shader) {
|
|
|
|
return mAttachedShaders.Contains(shader);
|
|
|
|
}
|
|
|
|
|
|
|
|
// return true if the shader wasn't already attached
|
|
|
|
PRBool AttachShader(WebGLShader *shader) {
|
|
|
|
if (ContainsShader(shader))
|
|
|
|
return PR_FALSE;
|
|
|
|
mAttachedShaders.AppendElement(shader);
|
2011-01-05 13:08:53 -08:00
|
|
|
shader->IncrementAttachCount();
|
2010-06-04 12:03:33 -07:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return true if the shader was found and removed
|
|
|
|
PRBool DetachShader(WebGLShader *shader) {
|
2011-01-05 13:08:53 -08:00
|
|
|
if (mAttachedShaders.RemoveElement(shader)) {
|
|
|
|
shader->DecrementAttachCount();
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
2010-06-04 12:03:33 -07:00
|
|
|
}
|
|
|
|
|
2010-11-16 20:33:04 -08:00
|
|
|
PRBool HasAttachedShaderOfType(GLenum shaderType) {
|
2010-06-04 12:03:33 -07:00
|
|
|
for (PRUint32 i = 0; i < mAttachedShaders.Length(); ++i) {
|
2011-05-06 11:44:23 -07:00
|
|
|
if (mAttachedShaders[i] && mAttachedShaders[i]->ShaderType() == shaderType) {
|
2010-06-04 12:03:33 -07:00
|
|
|
return PR_TRUE;
|
2010-11-16 20:33:04 -08:00
|
|
|
}
|
2010-06-04 12:03:33 -07:00
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
2009-09-02 17:47:49 -07:00
|
|
|
|
2010-11-16 20:33:04 -08:00
|
|
|
PRBool HasBothShaderTypesAttached() {
|
|
|
|
return
|
|
|
|
HasAttachedShaderOfType(LOCAL_GL_VERTEX_SHADER) &&
|
|
|
|
HasAttachedShaderOfType(LOCAL_GL_FRAGMENT_SHADER);
|
|
|
|
}
|
|
|
|
|
2010-06-08 14:25:27 -07:00
|
|
|
PRBool NextGeneration()
|
|
|
|
{
|
2010-07-03 15:32:19 -07:00
|
|
|
if (!(mGeneration+1).valid())
|
2010-06-08 14:25:27 -07:00
|
|
|
return PR_FALSE; // must exit without changing mGeneration
|
2010-07-03 15:32:19 -07:00
|
|
|
++mGeneration;
|
2010-06-08 14:25:27 -07:00
|
|
|
mMapUniformLocations.Clear();
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
2010-11-05 12:57:58 -07:00
|
|
|
|
2010-06-08 14:25:27 -07:00
|
|
|
|
|
|
|
already_AddRefed<WebGLUniformLocation> GetUniformLocationObject(GLint glLocation);
|
|
|
|
|
2010-06-10 10:45:00 -07:00
|
|
|
/* Called only after LinkProgram */
|
|
|
|
PRBool UpdateInfo(gl::GLContext *gl);
|
|
|
|
|
|
|
|
/* Getters for cached program info */
|
|
|
|
WebGLint UniformMaxNameLength() const { return mUniformMaxNameLength; }
|
|
|
|
WebGLint AttribMaxNameLength() const { return mAttribMaxNameLength; }
|
|
|
|
WebGLint UniformCount() const { return mUniformCount; }
|
|
|
|
WebGLint AttribCount() const { return mAttribCount; }
|
|
|
|
bool IsAttribInUse(unsigned i) const { return mAttribsInUse[i]; }
|
|
|
|
|
2009-09-02 17:47:49 -07:00
|
|
|
NS_DECL_ISUPPORTS
|
2010-06-04 12:03:33 -07:00
|
|
|
NS_DECL_NSIWEBGLPROGRAM
|
2009-09-02 17:47:49 -07:00
|
|
|
protected:
|
2010-05-28 15:52:39 -07:00
|
|
|
WebGLuint mName;
|
2010-06-04 12:03:33 -07:00
|
|
|
PRPackedBool mDeleted;
|
2011-01-05 13:08:53 -08:00
|
|
|
PRPackedBool mDeletePending;
|
2010-06-04 12:03:33 -07:00
|
|
|
PRPackedBool mLinkStatus;
|
2011-01-05 13:08:53 -08:00
|
|
|
// attached shaders of the program object
|
2011-05-06 11:44:23 -07:00
|
|
|
nsTArray<nsRefPtr<WebGLShader> > mAttachedShaders;
|
2010-07-03 15:32:19 -07:00
|
|
|
CheckedUint32 mGeneration;
|
2011-01-05 13:08:53 -08:00
|
|
|
|
|
|
|
// post-link data
|
|
|
|
nsRefPtrHashtable<nsUint32HashKey, WebGLUniformLocation> mMapUniformLocations;
|
2010-06-10 10:45:00 -07:00
|
|
|
GLint mUniformMaxNameLength;
|
|
|
|
GLint mAttribMaxNameLength;
|
|
|
|
GLint mUniformCount;
|
|
|
|
GLint mAttribCount;
|
|
|
|
std::vector<bool> mAttribsInUse;
|
2009-09-02 17:47:49 -07:00
|
|
|
};
|
|
|
|
|
2010-06-04 12:03:33 -07:00
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(WebGLProgram, WEBGLPROGRAM_PRIVATE_IID)
|
2010-05-19 13:46:08 -07:00
|
|
|
|
2010-11-05 12:57:58 -07:00
|
|
|
#define WEBGLRENDERBUFFER_PRIVATE_IID \
|
|
|
|
{0x3cbc2067, 0x5831, 0x4e3f, {0xac, 0x52, 0x7e, 0xf4, 0x5c, 0x04, 0xff, 0xae}}
|
|
|
|
class WebGLRenderbuffer :
|
|
|
|
public nsIWebGLRenderbuffer,
|
2010-05-15 05:07:30 -07:00
|
|
|
public WebGLZeroingObject,
|
2010-06-04 12:03:37 -07:00
|
|
|
public WebGLRectangleObject,
|
|
|
|
public WebGLContextBoundObject
|
2009-09-02 17:47:49 -07:00
|
|
|
{
|
|
|
|
public:
|
2010-11-05 12:57:58 -07:00
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(WEBGLRENDERBUFFER_PRIVATE_IID)
|
2010-05-19 13:46:08 -07:00
|
|
|
|
2010-11-05 12:57:58 -07:00
|
|
|
WebGLRenderbuffer(WebGLContext *context, WebGLuint name, WebGLuint secondBufferName = 0) :
|
2010-06-04 12:03:37 -07:00
|
|
|
WebGLContextBoundObject(context),
|
2010-11-05 12:57:58 -07:00
|
|
|
mName(name),
|
|
|
|
mInternalFormat(0),
|
2011-02-11 15:11:30 -08:00
|
|
|
mDeleted(PR_FALSE), mHasEverBeenBound(PR_FALSE), mInitialized(PR_FALSE)
|
2010-06-04 12:03:37 -07:00
|
|
|
{ }
|
2009-09-02 17:47:49 -07:00
|
|
|
|
|
|
|
void Delete() {
|
|
|
|
if (mDeleted)
|
|
|
|
return;
|
|
|
|
ZeroOwners();
|
|
|
|
mDeleted = PR_TRUE;
|
|
|
|
}
|
2010-11-05 12:57:58 -07:00
|
|
|
PRBool Deleted() const { return mDeleted; }
|
2011-02-11 15:11:30 -08:00
|
|
|
PRBool HasEverBeenBound() { return mHasEverBeenBound; }
|
|
|
|
void SetHasEverBeenBound(PRBool x) { mHasEverBeenBound = x; }
|
2010-11-05 12:57:58 -07:00
|
|
|
WebGLuint GLName() const { return mName; }
|
|
|
|
|
|
|
|
PRBool Initialized() const { return mInitialized; }
|
|
|
|
void SetInitialized(PRBool aInitialized) { mInitialized = aInitialized; }
|
|
|
|
|
|
|
|
WebGLenum InternalFormat() const { return mInternalFormat; }
|
|
|
|
void SetInternalFormat(WebGLenum aInternalFormat) { mInternalFormat = aInternalFormat; }
|
2009-09-02 17:47:49 -07:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
2010-11-05 12:57:58 -07:00
|
|
|
NS_DECL_NSIWEBGLRENDERBUFFER
|
|
|
|
|
2009-09-02 17:47:49 -07:00
|
|
|
protected:
|
2010-05-28 15:52:39 -07:00
|
|
|
WebGLuint mName;
|
2010-11-05 12:57:58 -07:00
|
|
|
WebGLenum mInternalFormat;
|
|
|
|
|
2009-09-02 17:47:49 -07:00
|
|
|
PRBool mDeleted;
|
2011-02-11 15:11:30 -08:00
|
|
|
PRBool mHasEverBeenBound;
|
2010-11-05 12:57:58 -07:00
|
|
|
PRBool mInitialized;
|
|
|
|
|
|
|
|
friend class WebGLFramebuffer;
|
2009-09-02 17:47:49 -07:00
|
|
|
};
|
|
|
|
|
2010-11-05 12:57:58 -07:00
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(WebGLRenderbuffer, WEBGLRENDERBUFFER_PRIVATE_IID)
|
2010-05-19 13:46:08 -07:00
|
|
|
|
2010-12-06 03:34:35 -08:00
|
|
|
class WebGLFramebufferAttachment
|
|
|
|
{
|
2011-01-05 13:08:53 -08:00
|
|
|
// deleting a texture or renderbuffer immediately detaches it
|
|
|
|
WebGLObjectRefPtr<WebGLTexture> mTexturePtr;
|
|
|
|
WebGLObjectRefPtr<WebGLRenderbuffer> mRenderbufferPtr;
|
2010-12-06 03:34:35 -08:00
|
|
|
WebGLenum mAttachmentPoint;
|
2011-01-05 13:08:53 -08:00
|
|
|
WebGLint mTextureLevel;
|
|
|
|
WebGLenum mTextureCubeMapFace;
|
2010-12-06 03:34:35 -08:00
|
|
|
|
|
|
|
public:
|
|
|
|
WebGLFramebufferAttachment(WebGLenum aAttachmentPoint)
|
|
|
|
: mAttachmentPoint(aAttachmentPoint)
|
|
|
|
{}
|
|
|
|
|
|
|
|
PRBool IsNull() const {
|
|
|
|
return !mTexturePtr && !mRenderbufferPtr;
|
|
|
|
}
|
|
|
|
|
2010-12-06 03:34:35 -08:00
|
|
|
PRBool HasAlpha() const {
|
|
|
|
WebGLenum format = 0;
|
|
|
|
if (mTexturePtr)
|
|
|
|
format = mTexturePtr->ImageInfoAt(0,0).mFormat;
|
|
|
|
if (mRenderbufferPtr)
|
|
|
|
format = mRenderbufferPtr->InternalFormat();
|
|
|
|
return format == LOCAL_GL_RGBA ||
|
|
|
|
format == LOCAL_GL_LUMINANCE_ALPHA ||
|
|
|
|
format == LOCAL_GL_ALPHA ||
|
|
|
|
format == LOCAL_GL_RGBA4 ||
|
|
|
|
format == LOCAL_GL_RGB5_A1;
|
|
|
|
}
|
|
|
|
|
2011-01-05 13:08:53 -08:00
|
|
|
void SetTexture(WebGLTexture *tex, WebGLint level, WebGLenum face) {
|
2010-12-06 03:34:35 -08:00
|
|
|
mTexturePtr = tex;
|
|
|
|
mRenderbufferPtr = nsnull;
|
2011-01-05 13:08:53 -08:00
|
|
|
mTextureLevel = level;
|
|
|
|
mTextureCubeMapFace = face;
|
2010-12-06 03:34:35 -08:00
|
|
|
}
|
|
|
|
void SetRenderbuffer(WebGLRenderbuffer *rb) {
|
|
|
|
mTexturePtr = nsnull;
|
|
|
|
mRenderbufferPtr = rb;
|
|
|
|
}
|
|
|
|
WebGLTexture *Texture() const {
|
|
|
|
return mTexturePtr.get();
|
|
|
|
}
|
|
|
|
WebGLRenderbuffer *Renderbuffer() const {
|
|
|
|
return mRenderbufferPtr.get();
|
|
|
|
}
|
2011-01-05 13:08:53 -08:00
|
|
|
WebGLint TextureLevel() const {
|
|
|
|
return mTextureLevel;
|
|
|
|
}
|
|
|
|
WebGLenum TextureCubeMapFace() const {
|
|
|
|
return mTextureCubeMapFace;
|
|
|
|
}
|
2010-12-06 03:34:35 -08:00
|
|
|
|
|
|
|
PRBool IsIncompatibleWithAttachmentPoint() const
|
|
|
|
{
|
|
|
|
// textures can only be color textures in WebGL
|
|
|
|
if (mTexturePtr)
|
|
|
|
return mAttachmentPoint != LOCAL_GL_COLOR_ATTACHMENT0;
|
|
|
|
|
|
|
|
if (mRenderbufferPtr) {
|
|
|
|
WebGLenum format = mRenderbufferPtr->InternalFormat();
|
|
|
|
switch (mAttachmentPoint) {
|
|
|
|
case LOCAL_GL_COLOR_ATTACHMENT0:
|
|
|
|
return format != LOCAL_GL_RGB565 &&
|
|
|
|
format != LOCAL_GL_RGB5_A1 &&
|
|
|
|
format != LOCAL_GL_RGBA4;
|
|
|
|
case LOCAL_GL_DEPTH_ATTACHMENT:
|
|
|
|
return format != LOCAL_GL_DEPTH_COMPONENT16;
|
|
|
|
case LOCAL_GL_STENCIL_ATTACHMENT:
|
|
|
|
return format != LOCAL_GL_STENCIL_INDEX8;
|
|
|
|
case LOCAL_GL_DEPTH_STENCIL_ATTACHMENT:
|
|
|
|
return format != LOCAL_GL_DEPTH_STENCIL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_FALSE; // no attachment at all, so no incompatibility
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool HasUninitializedRenderbuffer() const {
|
|
|
|
return mRenderbufferPtr && !mRenderbufferPtr->Initialized();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-11-05 12:57:58 -07:00
|
|
|
#define WEBGLFRAMEBUFFER_PRIVATE_IID \
|
|
|
|
{0x0052a16f, 0x4bc9, 0x4a55, {0x9d, 0xa3, 0x54, 0x95, 0xaa, 0x4e, 0x80, 0xb9}}
|
|
|
|
class WebGLFramebuffer :
|
|
|
|
public nsIWebGLFramebuffer,
|
2010-05-15 05:07:30 -07:00
|
|
|
public WebGLZeroingObject,
|
2010-06-04 12:03:37 -07:00
|
|
|
public WebGLRectangleObject,
|
|
|
|
public WebGLContextBoundObject
|
2009-09-02 17:47:49 -07:00
|
|
|
{
|
|
|
|
public:
|
2010-11-05 12:57:58 -07:00
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(WEBGLFRAMEBUFFER_PRIVATE_IID)
|
2010-05-19 13:46:08 -07:00
|
|
|
|
2010-11-05 12:57:58 -07:00
|
|
|
WebGLFramebuffer(WebGLContext *context, WebGLuint name) :
|
2010-06-04 12:03:37 -07:00
|
|
|
WebGLContextBoundObject(context),
|
2011-02-11 15:11:30 -08:00
|
|
|
mName(name), mDeleted(PR_FALSE), mHasEverBeenBound(PR_FALSE),
|
2010-12-06 03:34:35 -08:00
|
|
|
mColorAttachment(LOCAL_GL_COLOR_ATTACHMENT0),
|
|
|
|
mDepthAttachment(LOCAL_GL_DEPTH_ATTACHMENT),
|
|
|
|
mStencilAttachment(LOCAL_GL_STENCIL_ATTACHMENT),
|
|
|
|
mDepthStencilAttachment(LOCAL_GL_DEPTH_STENCIL_ATTACHMENT)
|
2010-06-04 12:03:37 -07:00
|
|
|
{ }
|
2009-09-02 17:47:49 -07:00
|
|
|
|
|
|
|
void Delete() {
|
|
|
|
if (mDeleted)
|
|
|
|
return;
|
|
|
|
ZeroOwners();
|
|
|
|
mDeleted = PR_TRUE;
|
|
|
|
}
|
|
|
|
PRBool Deleted() { return mDeleted; }
|
2011-02-11 15:11:30 -08:00
|
|
|
PRBool HasEverBeenBound() { return mHasEverBeenBound; }
|
|
|
|
void SetHasEverBeenBound(PRBool x) { mHasEverBeenBound = x; }
|
2010-05-28 15:52:39 -07:00
|
|
|
WebGLuint GLName() { return mName; }
|
2009-09-02 17:47:49 -07:00
|
|
|
|
2010-11-05 12:57:58 -07:00
|
|
|
nsresult FramebufferRenderbuffer(WebGLenum target,
|
|
|
|
WebGLenum attachment,
|
|
|
|
WebGLenum rbtarget,
|
|
|
|
nsIWebGLRenderbuffer *rbobj)
|
|
|
|
{
|
|
|
|
WebGLuint renderbuffername;
|
|
|
|
PRBool isNull;
|
|
|
|
WebGLRenderbuffer *wrb;
|
|
|
|
|
|
|
|
if (!mContext->GetConcreteObjectAndGLName("framebufferRenderbuffer: renderbuffer",
|
|
|
|
rbobj, &wrb, &renderbuffername, &isNull))
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target != LOCAL_GL_FRAMEBUFFER)
|
|
|
|
return mContext->ErrorInvalidEnumInfo("framebufferRenderbuffer: target", target);
|
|
|
|
|
|
|
|
if (rbtarget != LOCAL_GL_RENDERBUFFER)
|
|
|
|
return mContext->ErrorInvalidEnumInfo("framebufferRenderbuffer: renderbuffer target:", rbtarget);
|
|
|
|
|
|
|
|
switch (attachment) {
|
|
|
|
case LOCAL_GL_DEPTH_ATTACHMENT:
|
2010-12-06 03:34:35 -08:00
|
|
|
mDepthAttachment.SetRenderbuffer(wrb);
|
2010-11-05 12:57:58 -07:00
|
|
|
break;
|
|
|
|
case LOCAL_GL_STENCIL_ATTACHMENT:
|
2010-12-06 03:34:35 -08:00
|
|
|
mStencilAttachment.SetRenderbuffer(wrb);
|
2010-11-05 12:57:58 -07:00
|
|
|
break;
|
|
|
|
case LOCAL_GL_DEPTH_STENCIL_ATTACHMENT:
|
2010-12-06 03:34:35 -08:00
|
|
|
mDepthStencilAttachment.SetRenderbuffer(wrb);
|
2010-11-05 12:57:58 -07:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// finish checking that the 'attachment' parameter is among the allowed values
|
2010-12-06 03:34:35 -08:00
|
|
|
if (attachment != LOCAL_GL_COLOR_ATTACHMENT0)
|
2010-11-05 12:57:58 -07:00
|
|
|
return mContext->ErrorInvalidEnumInfo("framebufferRenderbuffer: attachment", attachment);
|
|
|
|
if (!isNull) {
|
2010-11-16 20:33:03 -08:00
|
|
|
// ReadPixels needs alpha and size information, but only
|
|
|
|
// for COLOR_ATTACHMENT0
|
2010-12-06 03:34:35 -08:00
|
|
|
setDimensions(wrb);
|
2010-11-05 12:57:58 -07:00
|
|
|
}
|
2010-12-06 03:34:35 -08:00
|
|
|
mColorAttachment.SetRenderbuffer(wrb);
|
2010-11-05 12:57:58 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
mContext->MakeContextCurrent();
|
2011-01-05 13:08:53 -08:00
|
|
|
if (attachment == LOCAL_GL_DEPTH_STENCIL_ATTACHMENT) {
|
|
|
|
mContext->gl->fFramebufferRenderbuffer(target, LOCAL_GL_DEPTH_ATTACHMENT, rbtarget, renderbuffername);
|
|
|
|
mContext->gl->fFramebufferRenderbuffer(target, LOCAL_GL_STENCIL_ATTACHMENT, rbtarget, renderbuffername);
|
|
|
|
} else {
|
|
|
|
mContext->gl->fFramebufferRenderbuffer(target, attachment, rbtarget, renderbuffername);
|
|
|
|
}
|
2010-11-05 12:57:58 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult FramebufferTexture2D(WebGLenum target,
|
|
|
|
WebGLenum attachment,
|
|
|
|
WebGLenum textarget,
|
|
|
|
nsIWebGLTexture *tobj,
|
|
|
|
WebGLint level)
|
|
|
|
{
|
|
|
|
WebGLuint texturename;
|
|
|
|
PRBool isNull;
|
|
|
|
WebGLTexture *wtex;
|
|
|
|
|
|
|
|
if (!mContext->GetConcreteObjectAndGLName("framebufferTexture2D: texture",
|
|
|
|
tobj, &wtex, &texturename, &isNull))
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (target != LOCAL_GL_FRAMEBUFFER)
|
|
|
|
return mContext->ErrorInvalidEnumInfo("framebufferTexture2D: target", target);
|
|
|
|
|
|
|
|
if (!isNull && textarget != LOCAL_GL_TEXTURE_2D &&
|
|
|
|
(textarget < LOCAL_GL_TEXTURE_CUBE_MAP_POSITIVE_X ||
|
|
|
|
textarget > LOCAL_GL_TEXTURE_CUBE_MAP_NEGATIVE_Z))
|
|
|
|
return mContext->ErrorInvalidEnumInfo("framebufferTexture2D: invalid texture target", textarget);
|
|
|
|
|
|
|
|
if (!isNull && level > 0)
|
|
|
|
return mContext->ErrorInvalidValue("framebufferTexture2D: level must be 0");
|
|
|
|
|
2011-01-05 13:08:53 -08:00
|
|
|
WebGLint face = (textarget == LOCAL_GL_TEXTURE_2D) ? 0 : textarget;
|
2010-11-05 12:57:58 -07:00
|
|
|
switch (attachment) {
|
|
|
|
case LOCAL_GL_DEPTH_ATTACHMENT:
|
2011-01-05 13:08:53 -08:00
|
|
|
mDepthAttachment.SetTexture(wtex, level, face);
|
2010-12-06 03:34:35 -08:00
|
|
|
break;
|
2010-11-05 12:57:58 -07:00
|
|
|
case LOCAL_GL_STENCIL_ATTACHMENT:
|
2011-01-05 13:08:53 -08:00
|
|
|
mStencilAttachment.SetTexture(wtex, level, face);
|
2010-12-06 03:34:35 -08:00
|
|
|
break;
|
2010-11-05 12:57:58 -07:00
|
|
|
case LOCAL_GL_DEPTH_STENCIL_ATTACHMENT:
|
2011-01-05 13:08:53 -08:00
|
|
|
mDepthStencilAttachment.SetTexture(wtex, level, face);
|
2010-11-05 12:57:58 -07:00
|
|
|
break;
|
|
|
|
default:
|
2010-12-06 03:34:35 -08:00
|
|
|
if (attachment != LOCAL_GL_COLOR_ATTACHMENT0)
|
2010-11-05 12:57:58 -07:00
|
|
|
return mContext->ErrorInvalidEnumInfo("framebufferTexture2D: attachment", attachment);
|
2010-11-16 20:33:03 -08:00
|
|
|
|
|
|
|
// keep data for readPixels, function only uses COLOR_ATTACHMENT0
|
2010-12-06 03:34:35 -08:00
|
|
|
setDimensions(wtex);
|
|
|
|
|
2011-01-05 13:08:53 -08:00
|
|
|
mColorAttachment.SetTexture(wtex, level, face);
|
2010-11-05 12:57:58 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
mContext->MakeContextCurrent();
|
2011-01-05 13:08:53 -08:00
|
|
|
if (attachment == LOCAL_GL_DEPTH_STENCIL_ATTACHMENT) {
|
|
|
|
mContext->gl->fFramebufferTexture2D(target, LOCAL_GL_DEPTH_ATTACHMENT, textarget, texturename, level);
|
|
|
|
mContext->gl->fFramebufferTexture2D(target, LOCAL_GL_STENCIL_ATTACHMENT, textarget, texturename, level);
|
|
|
|
} else {
|
|
|
|
mContext->gl->fFramebufferTexture2D(target, attachment, textarget, texturename, level);
|
|
|
|
}
|
2010-11-05 12:57:58 -07:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-12-06 03:34:35 -08:00
|
|
|
PRBool CheckAndInitializeRenderbuffers()
|
2010-11-05 12:57:58 -07:00
|
|
|
{
|
2010-12-06 03:34:35 -08:00
|
|
|
if (HasBadAttachments()) {
|
2010-11-05 12:57:58 -07:00
|
|
|
mContext->SynthesizeGLError(LOCAL_GL_INVALID_FRAMEBUFFER_OPERATION);
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2010-12-06 03:34:35 -08:00
|
|
|
if (mColorAttachment.HasUninitializedRenderbuffer() ||
|
|
|
|
mDepthAttachment.HasUninitializedRenderbuffer() ||
|
|
|
|
mStencilAttachment.HasUninitializedRenderbuffer() ||
|
|
|
|
mDepthStencilAttachment.HasUninitializedRenderbuffer())
|
2010-11-05 12:57:58 -07:00
|
|
|
{
|
|
|
|
InitializeRenderbuffers();
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2010-12-06 03:34:35 -08:00
|
|
|
PRBool HasBadAttachments() const {
|
|
|
|
if (mColorAttachment.IsIncompatibleWithAttachmentPoint() ||
|
|
|
|
mDepthAttachment.IsIncompatibleWithAttachmentPoint() ||
|
|
|
|
mStencilAttachment.IsIncompatibleWithAttachmentPoint() ||
|
|
|
|
mDepthStencilAttachment.IsIncompatibleWithAttachmentPoint())
|
|
|
|
{
|
|
|
|
// some attachment is incompatible with its attachment point
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
else if (int(mDepthAttachment.IsNull()) +
|
|
|
|
int(mStencilAttachment.IsNull()) +
|
|
|
|
int(mDepthStencilAttachment.IsNull()) <= 1)
|
|
|
|
{
|
|
|
|
// has at least two among Depth, Stencil, DepthStencil
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
else return PR_FALSE;
|
2010-11-05 12:57:58 -07:00
|
|
|
}
|
|
|
|
|
2011-01-05 13:08:53 -08:00
|
|
|
const WebGLFramebufferAttachment& ColorAttachment() const {
|
|
|
|
return mColorAttachment;
|
|
|
|
}
|
|
|
|
|
|
|
|
const WebGLFramebufferAttachment& DepthAttachment() const {
|
|
|
|
return mDepthAttachment;
|
|
|
|
}
|
|
|
|
|
|
|
|
const WebGLFramebufferAttachment& StencilAttachment() const {
|
|
|
|
return mStencilAttachment;
|
|
|
|
}
|
|
|
|
|
|
|
|
const WebGLFramebufferAttachment& DepthStencilAttachment() const {
|
|
|
|
return mDepthStencilAttachment;
|
|
|
|
}
|
|
|
|
|
|
|
|
const WebGLFramebufferAttachment& GetAttachment(WebGLenum attachment) const {
|
|
|
|
if (attachment == LOCAL_GL_DEPTH_STENCIL_ATTACHMENT)
|
|
|
|
return mDepthStencilAttachment;
|
|
|
|
if (attachment == LOCAL_GL_DEPTH_ATTACHMENT)
|
|
|
|
return mDepthAttachment;
|
|
|
|
if (attachment == LOCAL_GL_STENCIL_ATTACHMENT)
|
|
|
|
return mStencilAttachment;
|
|
|
|
|
|
|
|
NS_ASSERTION(attachment == LOCAL_GL_COLOR_ATTACHMENT0, "bad attachment!");
|
2010-12-06 03:34:35 -08:00
|
|
|
return mColorAttachment;
|
2010-11-16 20:33:03 -08:00
|
|
|
}
|
|
|
|
|
2010-12-06 03:34:35 -08:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIWEBGLFRAMEBUFFER
|
|
|
|
|
2009-09-02 17:47:49 -07:00
|
|
|
protected:
|
2010-11-05 12:57:58 -07:00
|
|
|
|
|
|
|
// protected because WebGLContext should only call InitializeRenderbuffers
|
|
|
|
void InitializeRenderbuffers()
|
|
|
|
{
|
|
|
|
mContext->MakeContextCurrent();
|
|
|
|
|
|
|
|
if (mContext->gl->fCheckFramebufferStatus(LOCAL_GL_FRAMEBUFFER) != LOCAL_GL_FRAMEBUFFER_COMPLETE)
|
|
|
|
return;
|
|
|
|
|
2010-12-06 03:34:35 -08:00
|
|
|
PRBool initializeColorBuffer = mColorAttachment.HasUninitializedRenderbuffer();
|
|
|
|
PRBool initializeDepthBuffer = mDepthAttachment.HasUninitializedRenderbuffer() ||
|
|
|
|
mDepthStencilAttachment.HasUninitializedRenderbuffer();
|
|
|
|
PRBool initializeStencilBuffer = mStencilAttachment.HasUninitializedRenderbuffer() ||
|
|
|
|
mDepthStencilAttachment.HasUninitializedRenderbuffer();
|
2010-11-05 12:57:58 -07:00
|
|
|
|
2010-12-08 07:56:01 -08:00
|
|
|
realGLboolean savedColorMask[4] = {0};
|
|
|
|
realGLboolean savedDepthMask = 0;
|
2010-11-05 12:57:58 -07:00
|
|
|
GLuint savedStencilMask = 0;
|
2010-12-08 07:56:01 -08:00
|
|
|
GLfloat savedColorClearValue[4] = {0.f};
|
|
|
|
GLfloat savedDepthClearValue = 0.f;
|
2010-11-05 12:57:58 -07:00
|
|
|
GLint savedStencilClearValue = 0;
|
|
|
|
GLuint clearBits = 0;
|
|
|
|
|
|
|
|
realGLboolean wasScissorTestEnabled = mContext->gl->fIsEnabled(LOCAL_GL_SCISSOR_TEST);
|
|
|
|
mContext->gl->fDisable(LOCAL_GL_SCISSOR_TEST);
|
|
|
|
|
|
|
|
realGLboolean wasDitherEnabled = mContext->gl->fIsEnabled(LOCAL_GL_DITHER);
|
|
|
|
mContext->gl->fDisable(LOCAL_GL_DITHER);
|
|
|
|
|
|
|
|
mContext->gl->PushViewportRect(nsIntRect(0,0,width(),height()));
|
|
|
|
|
|
|
|
if (initializeColorBuffer) {
|
|
|
|
mContext->gl->fGetBooleanv(LOCAL_GL_COLOR_WRITEMASK, savedColorMask);
|
|
|
|
mContext->gl->fGetFloatv(LOCAL_GL_COLOR_CLEAR_VALUE, savedColorClearValue);
|
|
|
|
mContext->gl->fColorMask(1, 1, 1, 1);
|
|
|
|
mContext->gl->fClearColor(0.f, 0.f, 0.f, 0.f);
|
|
|
|
clearBits |= LOCAL_GL_COLOR_BUFFER_BIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (initializeDepthBuffer) {
|
|
|
|
mContext->gl->fGetBooleanv(LOCAL_GL_DEPTH_WRITEMASK, &savedDepthMask);
|
|
|
|
mContext->gl->fGetFloatv(LOCAL_GL_DEPTH_CLEAR_VALUE, &savedDepthClearValue);
|
|
|
|
mContext->gl->fDepthMask(1);
|
|
|
|
mContext->gl->fClearDepth(0.f);
|
|
|
|
clearBits |= LOCAL_GL_DEPTH_BUFFER_BIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (initializeStencilBuffer) {
|
|
|
|
mContext->gl->fGetIntegerv(LOCAL_GL_STENCIL_WRITEMASK, reinterpret_cast<GLint*>(&savedStencilMask));
|
|
|
|
mContext->gl->fGetIntegerv(LOCAL_GL_STENCIL_CLEAR_VALUE, &savedStencilClearValue);
|
|
|
|
mContext->gl->fStencilMask(0xffffffff);
|
|
|
|
mContext->gl->fClearStencil(0);
|
|
|
|
clearBits |= LOCAL_GL_STENCIL_BUFFER_BIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
// the one useful line of code
|
|
|
|
mContext->gl->fClear(clearBits);
|
|
|
|
|
|
|
|
if (initializeColorBuffer) {
|
|
|
|
mContext->gl->fColorMask(savedColorMask[0],
|
|
|
|
savedColorMask[1],
|
|
|
|
savedColorMask[2],
|
|
|
|
savedColorMask[3]);
|
|
|
|
mContext->gl->fClearColor(savedColorClearValue[0],
|
|
|
|
savedColorClearValue[1],
|
|
|
|
savedColorClearValue[2],
|
|
|
|
savedColorClearValue[3]);
|
2010-12-06 03:34:35 -08:00
|
|
|
mColorAttachment.Renderbuffer()->SetInitialized(PR_TRUE);
|
2010-11-05 12:57:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (initializeDepthBuffer) {
|
|
|
|
mContext->gl->fDepthMask(savedDepthMask);
|
|
|
|
mContext->gl->fClearDepth(savedDepthClearValue);
|
2010-12-06 03:34:35 -08:00
|
|
|
if (mDepthAttachment.Renderbuffer())
|
|
|
|
mDepthAttachment.Renderbuffer()->SetInitialized(PR_TRUE);
|
2010-11-05 12:57:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (initializeStencilBuffer) {
|
|
|
|
mContext->gl->fStencilMask(savedStencilMask);
|
|
|
|
mContext->gl->fClearStencil(savedStencilClearValue);
|
2010-12-06 03:34:35 -08:00
|
|
|
if (mStencilAttachment.Renderbuffer())
|
|
|
|
mStencilAttachment.Renderbuffer()->SetInitialized(PR_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (initializeDepthBuffer && initializeStencilBuffer) {
|
|
|
|
if (mDepthStencilAttachment.Renderbuffer())
|
|
|
|
mDepthStencilAttachment.Renderbuffer()->SetInitialized(PR_TRUE);
|
2010-11-05 12:57:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mContext->gl->PopViewportRect();
|
|
|
|
|
|
|
|
if (wasDitherEnabled)
|
|
|
|
mContext->gl->fEnable(LOCAL_GL_DITHER);
|
|
|
|
else
|
|
|
|
mContext->gl->fDisable(LOCAL_GL_DITHER);
|
|
|
|
|
|
|
|
if (wasScissorTestEnabled)
|
|
|
|
mContext->gl->fEnable(LOCAL_GL_DITHER);
|
|
|
|
else
|
|
|
|
mContext->gl->fDisable(LOCAL_GL_SCISSOR_TEST);
|
|
|
|
}
|
|
|
|
|
2010-05-28 15:52:39 -07:00
|
|
|
WebGLuint mName;
|
2010-11-16 20:33:03 -08:00
|
|
|
PRPackedBool mDeleted;
|
2011-02-11 15:11:30 -08:00
|
|
|
PRBool mHasEverBeenBound;
|
2010-12-06 03:34:35 -08:00
|
|
|
|
2010-11-05 12:57:58 -07:00
|
|
|
// we only store pointers to attached renderbuffers, not to attached textures, because
|
|
|
|
// we will only need to initialize renderbuffers. Textures are already initialized.
|
2010-12-06 03:34:35 -08:00
|
|
|
WebGLFramebufferAttachment mColorAttachment,
|
|
|
|
mDepthAttachment,
|
|
|
|
mStencilAttachment,
|
|
|
|
mDepthStencilAttachment;
|
2009-09-02 17:47:49 -07:00
|
|
|
};
|
|
|
|
|
2010-11-05 12:57:58 -07:00
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(WebGLFramebuffer, WEBGLFRAMEBUFFER_PRIVATE_IID)
|
2010-05-19 13:46:08 -07:00
|
|
|
|
2010-06-01 23:09:19 -07:00
|
|
|
#define WEBGLUNIFORMLOCATION_PRIVATE_IID \
|
|
|
|
{0x01a8a614, 0xb109, 0x42f1, {0xb4, 0x40, 0x8d, 0x8b, 0x87, 0x0b, 0x43, 0xa7}}
|
|
|
|
class WebGLUniformLocation :
|
|
|
|
public nsIWebGLUniformLocation,
|
2010-06-04 12:03:37 -07:00
|
|
|
public WebGLZeroingObject,
|
|
|
|
public WebGLContextBoundObject
|
2010-06-01 23:09:19 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(WEBGLUNIFORMLOCATION_PRIVATE_IID)
|
|
|
|
|
2010-06-04 12:03:37 -07:00
|
|
|
WebGLUniformLocation(WebGLContext *context, WebGLProgram *program, GLint location) :
|
2010-06-08 14:25:27 -07:00
|
|
|
WebGLContextBoundObject(context), mProgram(program), mProgramGeneration(program->Generation()),
|
|
|
|
mLocation(location) { }
|
2010-06-01 23:09:19 -07:00
|
|
|
|
|
|
|
WebGLProgram *Program() const { return mProgram; }
|
|
|
|
GLint Location() const { return mLocation; }
|
2010-07-03 15:32:19 -07:00
|
|
|
PRUint32 ProgramGeneration() const { return mProgramGeneration; }
|
2010-06-01 23:09:19 -07:00
|
|
|
|
|
|
|
// needed for our generic helpers to check nsIxxx parameters, see GetConcreteObject.
|
|
|
|
PRBool Deleted() { return PR_FALSE; }
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIWEBGLUNIFORMLOCATION
|
|
|
|
protected:
|
|
|
|
WebGLObjectRefPtr<WebGLProgram> mProgram;
|
2010-07-03 15:32:19 -07:00
|
|
|
PRUint32 mProgramGeneration;
|
2010-06-01 23:09:19 -07:00
|
|
|
GLint mLocation;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(WebGLUniformLocation, WEBGLUNIFORMLOCATION_PRIVATE_IID)
|
|
|
|
|
2010-08-23 14:03:49 -07:00
|
|
|
#define WEBGLACTIVEINFO_PRIVATE_IID \
|
|
|
|
{0x90def5ec, 0xc672, 0x4ac3, {0xb8, 0x97, 0x04, 0xa2, 0x6d, 0xda, 0x66, 0xd7}}
|
|
|
|
class WebGLActiveInfo :
|
|
|
|
public nsIWebGLActiveInfo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(WEBGLACTIVEINFO_PRIVATE_IID)
|
|
|
|
|
|
|
|
WebGLActiveInfo(WebGLint size, WebGLenum type, const char *nameptr, PRUint32 namelength) :
|
|
|
|
mDeleted(PR_FALSE),
|
|
|
|
mSize(size),
|
|
|
|
mType(type)
|
|
|
|
{
|
|
|
|
mName.AssignASCII(nameptr, namelength);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Delete() {
|
|
|
|
if (mDeleted)
|
|
|
|
return;
|
|
|
|
mDeleted = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool Deleted() { return mDeleted; }
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIWEBGLACTIVEINFO
|
|
|
|
protected:
|
|
|
|
PRBool mDeleted;
|
|
|
|
WebGLint mSize;
|
|
|
|
WebGLenum mType;
|
|
|
|
nsString mName;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(WebGLActiveInfo, WEBGLACTIVEINFO_PRIVATE_IID)
|
|
|
|
|
2011-05-20 12:53:53 -07:00
|
|
|
#define WEBGLEXTENSION_PRIVATE_IID \
|
|
|
|
{0x457dd0b2, 0x9f77, 0x4c23, {0x95, 0x70, 0x9d, 0x62, 0x65, 0xc1, 0xa4, 0x81}}
|
|
|
|
class WebGLExtension :
|
2011-05-20 12:53:53 -07:00
|
|
|
public nsIWebGLExtension,
|
|
|
|
public WebGLContextBoundObject,
|
|
|
|
public WebGLZeroingObject
|
2011-05-20 12:53:53 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
WebGLExtension(WebGLContext *baseContext)
|
2011-05-20 12:53:53 -07:00
|
|
|
: WebGLContextBoundObject(baseContext)
|
|
|
|
{}
|
2011-05-20 12:53:53 -07:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIWEBGLEXTENSION
|
|
|
|
|
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(WEBGLEXTENSION_PRIVATE_IID)
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(WebGLExtension, WEBGLACTIVEINFO_PRIVATE_IID)
|
|
|
|
|
2010-06-04 12:03:37 -07:00
|
|
|
/**
|
|
|
|
** Template implementations
|
|
|
|
**/
|
|
|
|
|
|
|
|
/* Helper function taking a BaseInterfaceType pointer and check that
|
|
|
|
* it matches the required concrete implementation type (if it's
|
|
|
|
* non-null), that it's not null/deleted unless we allowed it to, and
|
|
|
|
* obtain a pointer to the concrete object.
|
|
|
|
*
|
|
|
|
* By default, null (respectively: deleted) aInterface pointers are
|
|
|
|
* not allowed, but if you pass a non-null isNull (respectively:
|
|
|
|
* isDeleted) pointer, then they become allowed and the value at
|
2010-07-16 07:30:32 -07:00
|
|
|
* isNull (respecively isDeleted) is overwritten.
|
|
|
|
*
|
|
|
|
* If generateErrors is true (which is the default) then upon errors,
|
|
|
|
* GL errors are synthesized and error messages are printed, prepended by
|
|
|
|
* the 'info' string.
|
2010-06-04 12:03:37 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
template<class ConcreteObjectType, class BaseInterfaceType>
|
2010-07-16 07:30:32 -07:00
|
|
|
inline PRBool
|
|
|
|
WebGLContext::GetConcreteObject(const char *info,
|
|
|
|
BaseInterfaceType *aInterface,
|
2010-06-04 12:03:37 -07:00
|
|
|
ConcreteObjectType **aConcreteObject,
|
|
|
|
PRBool *isNull,
|
2010-07-16 07:30:32 -07:00
|
|
|
PRBool *isDeleted,
|
|
|
|
PRBool generateErrors)
|
2010-06-04 12:03:37 -07:00
|
|
|
{
|
|
|
|
if (!aInterface) {
|
|
|
|
if (NS_LIKELY(isNull)) {
|
|
|
|
// non-null isNull means that the caller will accept a null arg
|
|
|
|
*isNull = PR_TRUE;
|
|
|
|
if(isDeleted) *isDeleted = PR_FALSE;
|
|
|
|
*aConcreteObject = 0;
|
|
|
|
return PR_TRUE;
|
|
|
|
} else {
|
2010-07-16 07:30:32 -07:00
|
|
|
if (generateErrors)
|
|
|
|
ErrorInvalidValue("%s: null object passed as argument", info);
|
2010-06-04 12:03:37 -07:00
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isNull)
|
|
|
|
*isNull = PR_FALSE;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<ConcreteObjectType> tmp(do_QueryInterface(aInterface, &rv));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
*aConcreteObject = tmp;
|
|
|
|
|
|
|
|
if (!(*aConcreteObject)->IsCompatibleWithContext(this)) {
|
|
|
|
// the object doesn't belong to this WebGLContext
|
2010-09-13 08:53:52 -07:00
|
|
|
if (generateErrors)
|
2010-07-16 07:30:32 -07:00
|
|
|
ErrorInvalidOperation("%s: object from different WebGL context (or older generation of this one) "
|
|
|
|
"passed as argument", info);
|
2010-06-04 12:03:37 -07:00
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((*aConcreteObject)->Deleted()) {
|
|
|
|
if (NS_LIKELY(isDeleted)) {
|
|
|
|
// non-null isDeleted means that the caller will accept a deleted arg
|
|
|
|
*isDeleted = PR_TRUE;
|
|
|
|
return PR_TRUE;
|
|
|
|
} else {
|
2010-07-16 07:30:32 -07:00
|
|
|
if (generateErrors)
|
2010-09-13 08:53:52 -07:00
|
|
|
ErrorInvalidValue("%s: deleted object passed as argument", info);
|
2010-06-04 12:03:37 -07:00
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isDeleted)
|
|
|
|
*isDeleted = PR_FALSE;
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Same as GetConcreteObject, and in addition gets the GL object name.
|
|
|
|
* Null objects give the name 0.
|
|
|
|
*/
|
|
|
|
template<class ConcreteObjectType, class BaseInterfaceType>
|
2010-07-16 07:30:32 -07:00
|
|
|
inline PRBool
|
|
|
|
WebGLContext::GetConcreteObjectAndGLName(const char *info,
|
|
|
|
BaseInterfaceType *aInterface,
|
2010-06-04 12:03:37 -07:00
|
|
|
ConcreteObjectType **aConcreteObject,
|
|
|
|
WebGLuint *aGLObjectName,
|
|
|
|
PRBool *isNull,
|
|
|
|
PRBool *isDeleted)
|
|
|
|
{
|
2010-07-16 07:30:32 -07:00
|
|
|
PRBool result = GetConcreteObject(info, aInterface, aConcreteObject, isNull, isDeleted);
|
2010-06-04 12:03:37 -07:00
|
|
|
if (result == PR_FALSE) return PR_FALSE;
|
|
|
|
*aGLObjectName = *aConcreteObject ? (*aConcreteObject)->GLName() : 0;
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Same as GetConcreteObjectAndGLName when you don't need the concrete object pointer.
|
|
|
|
*/
|
|
|
|
template<class ConcreteObjectType, class BaseInterfaceType>
|
2010-07-16 07:30:32 -07:00
|
|
|
inline PRBool
|
|
|
|
WebGLContext::GetGLName(const char *info,
|
|
|
|
BaseInterfaceType *aInterface,
|
2010-06-04 12:03:37 -07:00
|
|
|
WebGLuint *aGLObjectName,
|
|
|
|
PRBool *isNull,
|
|
|
|
PRBool *isDeleted)
|
|
|
|
{
|
|
|
|
ConcreteObjectType *aConcreteObject;
|
2010-07-16 07:30:32 -07:00
|
|
|
return GetConcreteObjectAndGLName(info, aInterface, &aConcreteObject, aGLObjectName, isNull, isDeleted);
|
2010-06-04 12:03:37 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Same as GetConcreteObject when you only want to check if the conversion succeeds.
|
|
|
|
*/
|
|
|
|
template<class ConcreteObjectType, class BaseInterfaceType>
|
2010-07-16 07:30:32 -07:00
|
|
|
inline PRBool
|
|
|
|
WebGLContext::CanGetConcreteObject(const char *info,
|
|
|
|
BaseInterfaceType *aInterface,
|
2010-06-04 12:03:37 -07:00
|
|
|
PRBool *isNull,
|
|
|
|
PRBool *isDeleted)
|
|
|
|
{
|
|
|
|
ConcreteObjectType *aConcreteObject;
|
2010-07-16 07:30:32 -07:00
|
|
|
return GetConcreteObject(info, aInterface, &aConcreteObject, isNull, isDeleted, PR_FALSE);
|
2010-06-04 12:03:37 -07:00
|
|
|
}
|
|
|
|
|
2009-09-02 17:47:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|