Backed out 4 changesets (bug 1858627) for causing clipboard/paste failures. CLOSED...
[gecko.git] / dom / canvas / WebGLProgram.h
blob615c19ce9932b37923611df2b6d168537b906358
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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 WEBGL_PROGRAM_H_
7 #define WEBGL_PROGRAM_H_
9 #include <bitset>
10 #include <map>
11 #include <memory>
12 #include <set>
13 #include <vector>
15 #include "mozilla/RefPtr.h"
16 #include "mozilla/Vector.h"
17 #include "mozilla/WeakPtr.h"
19 #include "CacheInvalidator.h"
20 #include "WebGLObjectModel.h"
21 #include "WebGLTypes.h"
23 namespace mozilla {
24 class ErrorResult;
25 class WebGLContext;
26 class WebGLProgram;
27 class WebGLShader;
29 namespace dom {
30 template <typename>
31 struct Nullable;
32 class OwningUnsignedLongOrUint32ArrayOrBoolean;
33 template <typename>
34 class Sequence;
35 } // namespace dom
37 namespace webgl {
39 enum class TextureBaseType : uint8_t;
41 struct UniformBlockInfo final {
42 const ActiveUniformBlockInfo& info;
43 const IndexedBufferBinding* binding = nullptr;
46 struct FragOutputInfo final {
47 const uint8_t loc;
48 const std::string userName;
49 const std::string mappedName;
50 const TextureBaseType baseType;
53 struct CachedDrawFetchLimits final {
54 uint64_t maxVerts = UINT64_MAX;
55 uint64_t maxInstances = UINT64_MAX;
56 std::vector<BufferAndIndex> usedBuffers;
58 CachedDrawFetchLimits() = default;
59 explicit CachedDrawFetchLimits(const CachedDrawFetchLimits&) = delete;
60 CachedDrawFetchLimits(CachedDrawFetchLimits&&) = default;
62 CachedDrawFetchLimits& operator=(CachedDrawFetchLimits&&) = default;
65 // -
67 void UniformAs1fv(gl::GLContext& gl, GLint location, GLsizei count,
68 bool transpose, const void* any);
70 struct ActiveUniformValidationInfo final {
71 const ActiveUniformInfo& info;
72 bool isArray = false;
73 uint8_t channelsPerElem = 0;
74 decltype(&UniformAs1fv) pfn = nullptr;
76 static ActiveUniformValidationInfo Make(const ActiveUniformInfo&);
79 struct SamplerUniformInfo final {
80 const nsTArray<RefPtr<WebGLTexture>>& texListForType;
81 // = const decltype(WebGLContext::mBound2DTextures)&
82 const webgl::TextureBaseType texBaseType;
83 const bool isShadowSampler;
84 Vector<uint8_t, 8> texUnits = decltype(texUnits)();
87 struct LocationInfo final {
88 const ActiveUniformValidationInfo info;
89 const uint32_t indexIntoUniform;
90 SamplerUniformInfo* const samplerInfo;
92 auto PrettyName() const {
93 auto ret = info.info.name;
94 if (info.isArray) {
95 ret += "[";
96 ret += std::to_string(indexIntoUniform);
97 ret += "]";
99 return ret;
103 // -
105 struct LinkedProgramInfo final : public RefCounted<LinkedProgramInfo>,
106 public SupportsWeakPtr,
107 public CacheInvalidator {
108 friend class mozilla::WebGLProgram;
110 MOZ_DECLARE_REFCOUNTED_TYPENAME(LinkedProgramInfo)
112 //////
114 WebGLProgram* const prog;
115 const GLenum transformFeedbackBufferMode;
117 std::bitset<kMaxDrawBuffers> hasOutput = 0;
118 std::unordered_map<uint8_t, const FragOutputInfo> fragOutputs;
119 uint8_t zLayerCount = 1;
121 mutable std::vector<size_t> componentsPerTFVert;
123 bool attrib0Active = false;
124 GLint webgl_gl_VertexID_Offset = -1; // Location
126 // -
128 std::map<std::string, std::string> nameMap;
129 webgl::LinkActiveInfo active;
131 std::vector<std::unique_ptr<SamplerUniformInfo>> samplerUniforms;
132 std::unordered_map<uint32_t, LocationInfo> locationMap;
134 mutable std::vector<UniformBlockInfo> uniformBlocks;
136 //////
138 private:
139 mutable CachedDrawFetchLimits mScratchFetchLimits;
141 public:
142 const CachedDrawFetchLimits* GetDrawFetchLimits() const;
144 //////
146 explicit LinkedProgramInfo(WebGLProgram* prog);
147 ~LinkedProgramInfo();
150 } // namespace webgl
152 class WebGLProgram final : public WebGLContextBoundObject {
153 friend class WebGLTransformFeedback;
154 friend struct webgl::LinkedProgramInfo;
156 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(WebGLProgram, override)
158 public:
159 explicit WebGLProgram(WebGLContext* webgl);
161 void Delete();
163 // GL funcs
164 void AttachShader(WebGLShader& shader);
165 void BindAttribLocation(GLuint index, const std::string& name);
166 void DetachShader(const WebGLShader& shader);
167 void UniformBlockBinding(GLuint uniformBlockIndex,
168 GLuint uniformBlockBinding) const;
170 void LinkProgram();
171 bool UseProgram() const;
172 bool ValidateProgram() const;
174 ////////////////
176 void TransformFeedbackVaryings(const std::vector<std::string>& varyings,
177 GLenum bufferMode);
179 bool IsLinked() const { return mMostRecentLinkInfo; }
181 const webgl::LinkedProgramInfo* LinkInfo() const {
182 return mMostRecentLinkInfo.get();
185 const auto& VertShader() const { return mVertShader; }
186 const auto& FragShader() const { return mFragShader; }
188 const auto& LinkLog() const { return mLinkLog; }
190 private:
191 ~WebGLProgram();
193 void LinkAndUpdate();
194 bool ValidateForLink();
195 bool ValidateAfterTentativeLink(std::string* const out_linkLog) const;
197 public:
198 const GLuint mGLName;
200 private:
201 RefPtr<WebGLShader> mVertShader;
202 RefPtr<WebGLShader> mFragShader;
203 size_t mNumActiveTFOs;
205 std::map<std::string, GLuint> mNextLink_BoundAttribLocs;
207 std::vector<std::string> mNextLink_TransformFeedbackVaryings;
208 GLenum mNextLink_TransformFeedbackBufferMode;
210 std::string mLinkLog;
211 RefPtr<const webgl::LinkedProgramInfo> mMostRecentLinkInfo;
214 } // namespace mozilla
216 #endif // WEBGL_PROGRAM_H_