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_
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"
32 class OwningUnsignedLongOrUint32ArrayOrBoolean
;
39 enum class TextureBaseType
: uint8_t;
41 struct UniformBlockInfo final
{
42 const ActiveUniformBlockInfo
& info
;
43 const IndexedBufferBinding
* binding
= nullptr;
46 struct FragOutputInfo final
{
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;
67 void UniformAs1fv(gl::GLContext
& gl
, GLint location
, GLsizei count
,
68 bool transpose
, const void* any
);
70 struct ActiveUniformValidationInfo final
{
71 const ActiveUniformInfo
& info
;
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
;
96 ret
+= std::to_string(indexIntoUniform
);
105 struct LinkedProgramInfo final
: public RefCounted
<LinkedProgramInfo
>,
106 public SupportsWeakPtr
,
107 public CacheInvalidator
{
108 friend class mozilla::WebGLProgram
;
110 MOZ_DECLARE_REFCOUNTED_TYPENAME(LinkedProgramInfo
)
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
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
;
139 mutable CachedDrawFetchLimits mScratchFetchLimits
;
142 const CachedDrawFetchLimits
* GetDrawFetchLimits() const;
146 explicit LinkedProgramInfo(WebGLProgram
* prog
);
147 ~LinkedProgramInfo();
152 class WebGLProgram final
: public WebGLContextBoundObject
{
153 friend class WebGLTransformFeedback
;
154 friend struct webgl::LinkedProgramInfo
;
156 MOZ_DECLARE_REFCOUNTED_VIRTUAL_TYPENAME(WebGLProgram
, override
)
159 explicit WebGLProgram(WebGLContext
* webgl
);
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;
171 bool UseProgram() const;
172 bool ValidateProgram() const;
176 void TransformFeedbackVaryings(const std::vector
<std::string
>& varyings
,
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
; }
193 void LinkAndUpdate();
194 bool ValidateForLink();
195 bool ValidateAfterTentativeLink(std::string
* const out_linkLog
) const;
198 const GLuint mGLName
;
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_