1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef WEBGLPROGRAM_H_
7 #define WEBGLPROGRAM_H_
9 #include "WebGLObjectModel.h"
13 #include "mozilla/CheckedInt.h"
14 #include "mozilla/LinkedList.h"
15 #include "nsWrapperCache.h"
16 #include "WebGLShader.h"
17 #include "WebGLUniformInfo.h"
22 struct WebGLUniformInfo
;
24 typedef nsDataHashtable
<nsCStringHashKey
, nsCString
> CStringMap
;
25 typedef nsDataHashtable
<nsCStringHashKey
, WebGLUniformInfo
> CStringToUniformInfoMap
;
27 class WebGLProgram MOZ_FINAL
28 : public nsWrapperCache
29 , public WebGLRefCountedObject
<WebGLProgram
>
30 , public LinkedListElement
<WebGLProgram
>
31 , public WebGLContextBoundObject
34 WebGLProgram(WebGLContext
*context
);
38 void DetachShaders() {
39 mAttachedShaders
.Clear();
42 GLuint
GLName() { return mGLName
; }
43 const nsTArray
<WebGLRefPtr
<WebGLShader
> >& AttachedShaders() const { return mAttachedShaders
; }
44 bool LinkStatus() { return mLinkStatus
; }
45 uint32_t Generation() const { return mGeneration
.value(); }
46 void SetLinkStatus(bool val
) { mLinkStatus
= val
; }
48 bool ContainsShader(WebGLShader
*shader
) {
49 return mAttachedShaders
.Contains(shader
);
52 // return true if the shader wasn't already attached
53 bool AttachShader(WebGLShader
*shader
);
55 // return true if the shader was found and removed
56 bool DetachShader(WebGLShader
*shader
);
58 bool HasAttachedShaderOfType(GLenum shaderType
);
60 bool HasBothShaderTypesAttached() {
62 HasAttachedShaderOfType(LOCAL_GL_VERTEX_SHADER
) &&
63 HasAttachedShaderOfType(LOCAL_GL_FRAGMENT_SHADER
);
66 bool HasBadShaderAttached();
68 size_t UpperBoundNumSamplerUniforms();
72 if (!(mGeneration
+ 1).isValid())
73 return false; // must exit without changing mGeneration
78 /* Called only after LinkProgram */
81 /* Getters for cached program info */
82 bool IsAttribInUse(unsigned i
) const { return mAttribsInUse
[i
]; }
84 /* Maps identifier |name| to the mapped identifier |*mappedName|
85 * Both are ASCII strings.
87 void MapIdentifier(const nsACString
& name
, nsCString
*mappedName
);
89 /* Un-maps mapped identifier |name| to the original identifier |*reverseMappedName|
90 * Both are ASCII strings.
92 void ReverseMapIdentifier(const nsACString
& name
, nsCString
*reverseMappedName
);
94 /* Returns the uniform array size (or 1 if the uniform is not an array) of
95 * the uniform with given mapped identifier.
97 * Note: the input string |name| is the mapped identifier, not the original identifier.
99 WebGLUniformInfo
GetUniformInfoForMappedIdentifier(const nsACString
& name
);
101 WebGLContext
*GetParentObject() const {
105 virtual JSObject
* WrapObject(JSContext
*cx
) MOZ_OVERRIDE
;
107 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(WebGLProgram
)
108 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(WebGLProgram
)
110 // public post-link data
111 std::map
<GLint
, nsCString
> mActiveAttribMap
;
113 static uint64_t IdentifierHashFunction(const char *ident
, size_t size
);
114 static void HashMapIdentifier(const nsACString
& name
, nsCString
*hashedName
);
123 // attached shaders of the program object
124 nsTArray
<WebGLRefPtr
<WebGLShader
> > mAttachedShaders
;
125 CheckedUint32 mGeneration
;
128 FallibleTArray
<bool> mAttribsInUse
;
129 nsAutoPtr
<CStringMap
> mIdentifierMap
, mIdentifierReverseMap
;
130 nsAutoPtr
<CStringToUniformInfoMap
> mUniformInfoMap
;
131 int mAttribMaxNameLength
;
134 } // namespace mozilla