Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/canvas/WebGLProgram.h
Line
Count
Source (jump to first uncovered line)
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 WEBGL_PROGRAM_H_
7
#define WEBGL_PROGRAM_H_
8
9
#include <map>
10
#include <set>
11
#include <string>
12
#include <vector>
13
14
#include "mozilla/LinkedList.h"
15
#include "mozilla/RefPtr.h"
16
#include "mozilla/WeakPtr.h"
17
#include "nsString.h"
18
#include "nsWrapperCache.h"
19
20
#include "CacheMap.h"
21
#include "WebGLContext.h"
22
#include "WebGLObjectModel.h"
23
24
namespace mozilla {
25
class ErrorResult;
26
class WebGLActiveInfo;
27
class WebGLProgram;
28
class WebGLShader;
29
class WebGLUniformLocation;
30
31
namespace dom {
32
template<typename> struct Nullable;
33
class OwningUnsignedLongOrUint32ArrayOrBoolean;
34
template<typename> class Sequence;
35
} // namespace dom
36
37
namespace webgl {
38
39
struct AttribInfo final
40
{
41
    const RefPtr<WebGLActiveInfo> mActiveInfo;
42
    const GLint mLoc; // -1 for active built-ins
43
    const GLenum mBaseType;
44
};
45
46
struct UniformInfo final
47
{
48
    typedef decltype(WebGLContext::mBound2DTextures) TexListT;
49
50
    const RefPtr<WebGLActiveInfo> mActiveInfo;
51
    const TexListT* const mSamplerTexList;
52
    std::vector<uint32_t> mSamplerValues;
53
54
protected:
55
    static const TexListT*
56
    GetTexList(WebGLActiveInfo* activeInfo);
57
58
public:
59
    explicit UniformInfo(WebGLActiveInfo* activeInfo);
60
};
61
62
struct UniformBlockInfo final
63
{
64
    const nsCString mUserName;
65
    const nsCString mMappedName;
66
    const uint32_t mDataSize;
67
68
    const IndexedBufferBinding* mBinding;
69
70
    UniformBlockInfo(WebGLContext* webgl, const nsACString& userName,
71
                     const nsACString& mappedName, uint32_t dataSize)
72
        : mUserName(userName)
73
        , mMappedName(mappedName)
74
        , mDataSize(dataSize)
75
        , mBinding(&webgl->mIndexedUniformBufferBindings[0])
76
0
    { }
77
};
78
79
struct CachedDrawFetchLimits final {
80
    uint64_t maxVerts;
81
    uint64_t maxInstances;
82
};
83
84
struct LinkedProgramInfo final
85
    : public RefCounted<LinkedProgramInfo>
86
    , public SupportsWeakPtr<LinkedProgramInfo>
87
{
88
    friend class mozilla::WebGLProgram;
89
90
    MOZ_DECLARE_REFCOUNTED_TYPENAME(LinkedProgramInfo)
91
    MOZ_DECLARE_WEAKREFERENCE_TYPENAME(LinkedProgramInfo)
92
93
    //////
94
95
    WebGLProgram* const prog;
96
    const GLenum transformFeedbackBufferMode;
97
98
    std::vector<AttribInfo> attribs;
99
    std::vector<UniformInfo*> uniforms; // Owns its contents.
100
    std::vector<UniformBlockInfo*> uniformBlocks; // Owns its contents.
101
    std::vector<RefPtr<WebGLActiveInfo>> transformFeedbackVaryings;
102
103
    // Needed for draw call validation.
104
    std::vector<UniformInfo*> uniformSamplers;
105
106
    mutable std::vector<size_t> componentsPerTFVert;
107
108
    bool attrib0Active;
109
110
    //////
111
112
    // The maps for the frag data names to the translated names.
113
    std::map<nsCString, const nsCString> fragDataMap;
114
115
    //////
116
117
    mutable CacheMap<const WebGLVertexArray*,
118
                     CachedDrawFetchLimits> mDrawFetchCache;
119
120
    const CachedDrawFetchLimits* GetDrawFetchLimits() const;
121
122
    //////
123
124
    explicit LinkedProgramInfo(WebGLProgram* prog);
125
    ~LinkedProgramInfo();
126
127
    bool FindAttrib(const nsCString& userName, const AttribInfo** const out_info) const;
128
    bool FindUniform(const nsCString& userName, nsCString* const out_mappedName,
129
                     size_t* const out_arrayIndex, UniformInfo** const out_info) const;
130
    bool MapFragDataName(const nsCString& userName,
131
                         nsCString* const out_mappedName) const;
132
};
133
134
} // namespace webgl
135
136
class WebGLProgram final
137
    : public nsWrapperCache
138
    , public WebGLRefCountedObject<WebGLProgram>
139
    , public LinkedListElement<WebGLProgram>
140
{
141
    friend class WebGLTransformFeedback;
142
    friend struct webgl::LinkedProgramInfo;
143
144
public:
145
    NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(WebGLProgram)
146
    NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(WebGLProgram)
147
148
    explicit WebGLProgram(WebGLContext* webgl);
149
150
    void Delete();
151
152
    // GL funcs
153
    void AttachShader(WebGLShader* shader);
154
    void BindAttribLocation(GLuint index, const nsAString& name);
155
    void DetachShader(const WebGLShader* shader);
156
    already_AddRefed<WebGLActiveInfo> GetActiveAttrib(GLuint index) const;
157
    already_AddRefed<WebGLActiveInfo> GetActiveUniform(GLuint index) const;
158
    void GetAttachedShaders(nsTArray<RefPtr<WebGLShader>>* const out) const;
159
    GLint GetAttribLocation(const nsAString& name) const;
160
    GLint GetFragDataLocation(const nsAString& name) const;
161
    void GetProgramInfoLog(nsAString* const out) const;
162
    JS::Value GetProgramParameter(GLenum pname) const;
163
    GLuint GetUniformBlockIndex(const nsAString& name) const;
164
    void GetActiveUniformBlockName(GLuint uniformBlockIndex, nsAString& name) const;
165
    JS::Value GetActiveUniformBlockParam(GLuint uniformBlockIndex, GLenum pname) const;
166
    JS::Value GetActiveUniformBlockActiveUniforms(JSContext* cx, GLuint uniformBlockIndex,
167
                                                  ErrorResult* const out_error) const;
168
    already_AddRefed<WebGLUniformLocation> GetUniformLocation(const nsAString& name) const;
169
    void GetUniformIndices(const dom::Sequence<nsString>& uniformNames,
170
                           dom::Nullable< nsTArray<GLuint> >& retval) const;
171
    void UniformBlockBinding(GLuint uniformBlockIndex, GLuint uniformBlockBinding) const;
172
173
    void LinkProgram();
174
    bool UseProgram() const;
175
    void ValidateProgram() const;
176
177
    ////////////////
178
179
    bool FindAttribUserNameByMappedName(const nsACString& mappedName,
180
                                        nsCString* const out_userName) const;
181
    bool FindVaryingByMappedName(const nsACString& mappedName,
182
                                 nsCString* const out_userName,
183
                                 bool* const out_isArray) const;
184
    bool FindUniformByMappedName(const nsACString& mappedName,
185
                                 nsCString* const out_userName,
186
                                 bool* const out_isArray) const;
187
    bool UnmapUniformBlockName(const nsCString& mappedName,
188
                               nsCString* const out_userName) const;
189
190
    void TransformFeedbackVaryings(const dom::Sequence<nsString>& varyings,
191
                                   GLenum bufferMode);
192
    already_AddRefed<WebGLActiveInfo> GetTransformFeedbackVarying(GLuint index) const;
193
194
    void EnumerateFragOutputs(std::map<nsCString, const nsCString> &out_FragOutputs) const;
195
196
0
    bool IsLinked() const { return mMostRecentLinkInfo; }
197
198
0
    const webgl::LinkedProgramInfo* LinkInfo() const {
199
0
        return mMostRecentLinkInfo.get();
200
0
    }
201
202
0
    WebGLContext* GetParentObject() const {
203
0
        return mContext;
204
0
    }
205
206
    virtual JSObject* WrapObject(JSContext* js, JS::Handle<JSObject*> givenProto) override;
207
208
private:
209
    ~WebGLProgram();
210
211
    void LinkAndUpdate();
212
    bool ValidateForLink();
213
    bool ValidateAfterTentativeLink(nsCString* const out_linkLog) const;
214
215
public:
216
    const GLuint mGLName;
217
218
private:
219
    WebGLRefPtr<WebGLShader> mVertShader;
220
    WebGLRefPtr<WebGLShader> mFragShader;
221
    size_t mNumActiveTFOs;
222
223
    std::map<nsCString, GLuint> mNextLink_BoundAttribLocs;
224
225
    std::vector<nsString> mNextLink_TransformFeedbackVaryings;
226
    GLenum mNextLink_TransformFeedbackBufferMode;
227
228
    nsCString mLinkLog;
229
    RefPtr<const webgl::LinkedProgramInfo> mMostRecentLinkInfo;
230
};
231
232
} // namespace mozilla
233
234
#endif // WEBGL_PROGRAM_H_