Bug 1850713: remove duplicated setting of early hint preloader id in `ScriptLoader...
[gecko.git] / dom / canvas / WebGLProgram.h
blobf247c9090adf59b253dbdabc7f1b1120664b1be0
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 <set>
12 #include <vector>
14 #include "mozilla/RefPtr.h"
15 #include "mozilla/Vector.h"
16 #include "mozilla/WeakPtr.h"
18 #include "CacheInvalidator.h"
19 #include "WebGLContext.h"
20 #include "WebGLObjectModel.h"
22 namespace mozilla {
23 class ErrorResult;
24 class WebGLContext;
25 class WebGLProgram;
26 class WebGLShader;
28 namespace dom {
29 template <typename>
30 struct Nullable;
31 class OwningUnsignedLongOrUint32ArrayOrBoolean;
32 template <typename>
33 class Sequence;
34 } // namespace dom
36 namespace webgl {
38 enum class TextureBaseType : uint8_t;
40 struct UniformBlockInfo final {
41 const ActiveUniformBlockInfo& info;
42 const IndexedBufferBinding* binding = nullptr;
45 struct FragOutputInfo final {
46 const uint8_t loc;
47 const std::string userName;
48 const std::string mappedName;
49 const TextureBaseType baseType;
52 struct CachedDrawFetchLimits final {
53 uint64_t maxVerts = UINT64_MAX;
54 uint64_t maxInstances = UINT64_MAX;
55 std::vector<BufferAndIndex> usedBuffers;
57 CachedDrawFetchLimits() = default;
58 explicit CachedDrawFetchLimits(const CachedDrawFetchLimits&) = delete;
59 CachedDrawFetchLimits(CachedDrawFetchLimits&&) = default;
61 CachedDrawFetchLimits& operator=(CachedDrawFetchLimits&&) = default;
64 // -
66 void UniformAs1fv(gl::GLContext& gl, GLint location, GLsizei count,
67 bool transpose, const void* any);
69 struct ActiveUniformValidationInfo final {
70 const ActiveUniformInfo& info;
71 bool isArray = false;
72 uint8_t channelsPerElem = 0;
73 decltype(&UniformAs1fv) pfn = nullptr;
75 static ActiveUniformValidationInfo Make(const ActiveUniformInfo&);
78 struct SamplerUniformInfo final {
79 const decltype(WebGLContext::mBound2DTextures)& texListForType;
80 const webgl::TextureBaseType texBaseType;
81 const bool isShadowSampler;
82 Vector<uint8_t, 8> texUnits = decltype(texUnits)();
85 struct LocationInfo final {
86 const ActiveUniformValidationInfo info;
87 const uint32_t indexIntoUniform;
88 SamplerUniformInfo* const samplerInfo;
90 auto PrettyName() const {
91 auto ret = info.info.name;
92 if (info.isArray) {
93 ret += "[";
94 ret += std::to_string(indexIntoUniform);
95 ret += "]";
97 return ret;
101 // -
103 struct LinkedProgramInfo final : public RefCounted<LinkedProgramInfo>,
104 public SupportsWeakPtr,
105 public CacheInvalidator {
106 friend class mozilla::WebGLProgram;
108 MOZ_DECLARE_REFCOUNTED_TYPENAME(LinkedProgramInfo)
110 //////
112 WebGLProgram* const prog;
113 const GLenum transformFeedbackBufferMode;
115 std::bitset<kMaxDrawBuffers> hasOutput = 0;
116 std::unordered_map<uint8_t, const FragOutputInfo> fragOutputs;
117 uint8_t zLayerCount = 1;
119 mutable std::vector<size_t> componentsPerTFVert;
121 bool attrib0Active = false;
122 GLint webgl_gl_VertexID_Offset = -1; // Location
124 // -
126 std::map<std::string, std::string> nameMap;
127 webgl::LinkActiveInfo active;
129 std::vector<std::unique_ptr<SamplerUniformInfo>> samplerUniforms;
130 std::unordered_map<uint32_t, LocationInfo> locationMap;
132 mutable std::vector<UniformBlockInfo> uniformBlocks;
134 //////
136 private:
137 mutable CachedDrawFetchLimits mScratchFetchLimits;
139 public:
140 const CachedDrawFetchLimits* GetDrawFetchLimits() const;
142 //////
144 explicit LinkedProgramInfo(WebGLProgram* prog);
145 ~LinkedProgramInfo();
148 } // namespace webgl
150 class WebGLProgram final : public WebGLContextBoundObject {
151 friend class WebGLTransformFeedback;
152 friend struct webgl::LinkedProgramInfo;
154 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(WebGLProgram, override)
156 public:
157 explicit WebGLProgram(WebGLContext* webgl);
159 void Delete();
161 // GL funcs
162 void AttachShader(WebGLShader& shader);
163 void BindAttribLocation(GLuint index, const std::string& name);
164 void DetachShader(const WebGLShader& shader);
165 void UniformBlockBinding(GLuint uniformBlockIndex,
166 GLuint uniformBlockBinding) const;
168 void LinkProgram();
169 bool UseProgram() const;
170 bool ValidateProgram() const;
172 ////////////////
174 void TransformFeedbackVaryings(const std::vector<std::string>& varyings,
175 GLenum bufferMode);
177 bool IsLinked() const { return mMostRecentLinkInfo; }
179 const webgl::LinkedProgramInfo* LinkInfo() const {
180 return mMostRecentLinkInfo.get();
183 const auto& VertShader() const { return mVertShader; }
184 const auto& FragShader() const { return mFragShader; }
186 const auto& LinkLog() const { return mLinkLog; }
188 private:
189 ~WebGLProgram();
191 void LinkAndUpdate();
192 bool ValidateForLink();
193 bool ValidateAfterTentativeLink(std::string* const out_linkLog) const;
195 public:
196 const GLuint mGLName;
198 private:
199 RefPtr<WebGLShader> mVertShader;
200 RefPtr<WebGLShader> mFragShader;
201 size_t mNumActiveTFOs;
203 std::map<std::string, GLuint> mNextLink_BoundAttribLocs;
205 std::vector<std::string> mNextLink_TransformFeedbackVaryings;
206 GLenum mNextLink_TransformFeedbackBufferMode;
208 std::string mLinkLog;
209 RefPtr<const webgl::LinkedProgramInfo> mMostRecentLinkInfo;
212 } // namespace mozilla
214 #endif // WEBGL_PROGRAM_H_