|
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/. */ |
|
5 |
|
6 #ifndef WEBGLPROGRAM_H_ |
|
7 #define WEBGLPROGRAM_H_ |
|
8 |
|
9 #include "WebGLObjectModel.h" |
|
10 |
|
11 #include "nsWrapperCache.h" |
|
12 |
|
13 #include "mozilla/LinkedList.h" |
|
14 #include "mozilla/CheckedInt.h" |
|
15 #include <map> |
|
16 |
|
17 namespace mozilla { |
|
18 |
|
19 class WebGLShader; |
|
20 class WebGLUniformInfo; |
|
21 |
|
22 typedef nsDataHashtable<nsCStringHashKey, nsCString> CStringMap; |
|
23 typedef nsDataHashtable<nsCStringHashKey, WebGLUniformInfo> CStringToUniformInfoMap; |
|
24 |
|
25 class WebGLProgram MOZ_FINAL |
|
26 : public nsWrapperCache |
|
27 , public WebGLRefCountedObject<WebGLProgram> |
|
28 , public LinkedListElement<WebGLProgram> |
|
29 , public WebGLContextBoundObject |
|
30 { |
|
31 public: |
|
32 WebGLProgram(WebGLContext *context); |
|
33 |
|
34 ~WebGLProgram() { |
|
35 DeleteOnce(); |
|
36 } |
|
37 |
|
38 void Delete(); |
|
39 |
|
40 void DetachShaders() { |
|
41 mAttachedShaders.Clear(); |
|
42 } |
|
43 |
|
44 GLuint GLName() { return mGLName; } |
|
45 const nsTArray<WebGLRefPtr<WebGLShader> >& AttachedShaders() const { return mAttachedShaders; } |
|
46 bool LinkStatus() { return mLinkStatus; } |
|
47 uint32_t Generation() const { return mGeneration.value(); } |
|
48 void SetLinkStatus(bool val) { mLinkStatus = val; } |
|
49 |
|
50 bool ContainsShader(WebGLShader *shader) { |
|
51 return mAttachedShaders.Contains(shader); |
|
52 } |
|
53 |
|
54 // return true if the shader wasn't already attached |
|
55 bool AttachShader(WebGLShader *shader); |
|
56 |
|
57 // return true if the shader was found and removed |
|
58 bool DetachShader(WebGLShader *shader); |
|
59 |
|
60 bool HasAttachedShaderOfType(GLenum shaderType); |
|
61 |
|
62 bool HasBothShaderTypesAttached() { |
|
63 return |
|
64 HasAttachedShaderOfType(LOCAL_GL_VERTEX_SHADER) && |
|
65 HasAttachedShaderOfType(LOCAL_GL_FRAGMENT_SHADER); |
|
66 } |
|
67 |
|
68 bool HasBadShaderAttached(); |
|
69 |
|
70 size_t UpperBoundNumSamplerUniforms(); |
|
71 |
|
72 bool NextGeneration() |
|
73 { |
|
74 if (!(mGeneration + 1).isValid()) |
|
75 return false; // must exit without changing mGeneration |
|
76 ++mGeneration; |
|
77 return true; |
|
78 } |
|
79 |
|
80 /* Called only after LinkProgram */ |
|
81 bool UpdateInfo(); |
|
82 |
|
83 /* Getters for cached program info */ |
|
84 bool IsAttribInUse(unsigned i) const { return mAttribsInUse[i]; } |
|
85 |
|
86 /* Maps identifier |name| to the mapped identifier |*mappedName| |
|
87 * Both are ASCII strings. |
|
88 */ |
|
89 void MapIdentifier(const nsACString& name, nsCString *mappedName); |
|
90 |
|
91 /* Un-maps mapped identifier |name| to the original identifier |*reverseMappedName| |
|
92 * Both are ASCII strings. |
|
93 */ |
|
94 void ReverseMapIdentifier(const nsACString& name, nsCString *reverseMappedName); |
|
95 |
|
96 /* Returns the uniform array size (or 1 if the uniform is not an array) of |
|
97 * the uniform with given mapped identifier. |
|
98 * |
|
99 * Note: the input string |name| is the mapped identifier, not the original identifier. |
|
100 */ |
|
101 WebGLUniformInfo GetUniformInfoForMappedIdentifier(const nsACString& name); |
|
102 |
|
103 WebGLContext *GetParentObject() const { |
|
104 return Context(); |
|
105 } |
|
106 |
|
107 virtual JSObject* WrapObject(JSContext *cx) MOZ_OVERRIDE; |
|
108 |
|
109 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(WebGLProgram) |
|
110 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(WebGLProgram) |
|
111 |
|
112 // public post-link data |
|
113 std::map<GLint, nsCString> mActiveAttribMap; |
|
114 |
|
115 protected: |
|
116 |
|
117 GLuint mGLName; |
|
118 bool mLinkStatus; |
|
119 // attached shaders of the program object |
|
120 nsTArray<WebGLRefPtr<WebGLShader> > mAttachedShaders; |
|
121 CheckedUint32 mGeneration; |
|
122 |
|
123 // post-link data |
|
124 FallibleTArray<bool> mAttribsInUse; |
|
125 nsAutoPtr<CStringMap> mIdentifierMap, mIdentifierReverseMap; |
|
126 nsAutoPtr<CStringToUniformInfoMap> mUniformInfoMap; |
|
127 int mAttribMaxNameLength; |
|
128 }; |
|
129 |
|
130 } // namespace mozilla |
|
131 |
|
132 #endif |