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_
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"
31 class OwningUnsignedLongOrUint32ArrayOrBoolean
;
38 enum class TextureBaseType
: uint8_t;
40 struct UniformBlockInfo final
{
41 const ActiveUniformBlockInfo
& info
;
42 const IndexedBufferBinding
* binding
= nullptr;
45 struct FragOutputInfo final
{
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;
66 void UniformAs1fv(gl::GLContext
& gl
, GLint location
, GLsizei count
,
67 bool transpose
, const void* any
);
69 struct ActiveUniformValidationInfo final
{
70 const ActiveUniformInfo
& info
;
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
;
94 ret
+= std::to_string(indexIntoUniform
);
103 struct LinkedProgramInfo final
: public RefCounted
<LinkedProgramInfo
>,
104 public SupportsWeakPtr
,
105 public CacheInvalidator
{
106 friend class mozilla::WebGLProgram
;
108 MOZ_DECLARE_REFCOUNTED_TYPENAME(LinkedProgramInfo
)
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
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
;
137 mutable CachedDrawFetchLimits mScratchFetchLimits
;
140 const CachedDrawFetchLimits
* GetDrawFetchLimits() const;
144 explicit LinkedProgramInfo(WebGLProgram
* prog
);
145 ~LinkedProgramInfo();
150 class WebGLProgram final
: public WebGLContextBoundObject
{
151 friend class WebGLTransformFeedback
;
152 friend struct webgl::LinkedProgramInfo
;
154 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(WebGLProgram
, override
)
157 explicit WebGLProgram(WebGLContext
* webgl
);
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;
169 bool UseProgram() const;
170 bool ValidateProgram() const;
174 void TransformFeedbackVaryings(const std::vector
<std::string
>& varyings
,
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
; }
191 void LinkAndUpdate();
192 bool ValidateForLink();
193 bool ValidateAfterTentativeLink(std::string
* const out_linkLog
) const;
196 const GLuint mGLName
;
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_