xref: /aosp_15_r20/external/angle/src/libANGLE/Context.inl.h (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2018 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // Context.inl.h: Defines inline functions of gl::Context class
8 // Has to be included after libANGLE/Context.h when using one
9 // of the defined functions
10 
11 #ifndef LIBANGLE_CONTEXT_INL_H_
12 #define LIBANGLE_CONTEXT_INL_H_
13 
14 #include "libANGLE/Context.h"
15 #include "libANGLE/GLES1Renderer.h"
16 #include "libANGLE/renderer/ContextImpl.h"
17 
18 #define ANGLE_HANDLE_ERR(X) \
19     (void)(X);              \
20     return;
21 #define ANGLE_CONTEXT_TRY(EXPR) ANGLE_TRY_TEMPLATE(EXPR, ANGLE_HANDLE_ERR)
22 
23 namespace gl
24 {
25 constexpr angle::PackedEnumMap<PrimitiveMode, GLsizei> kMinimumPrimitiveCounts = {{
26     {PrimitiveMode::Points, 1},
27     {PrimitiveMode::Lines, 2},
28     {PrimitiveMode::LineLoop, 2},
29     {PrimitiveMode::LineStrip, 2},
30     {PrimitiveMode::Triangles, 3},
31     {PrimitiveMode::TriangleStrip, 3},
32     {PrimitiveMode::TriangleFan, 3},
33     {PrimitiveMode::LinesAdjacency, 2},
34     {PrimitiveMode::LineStripAdjacency, 2},
35     {PrimitiveMode::TrianglesAdjacency, 3},
36     {PrimitiveMode::TriangleStripAdjacency, 3},
37 }};
38 
MarkTransformFeedbackBufferUsage(const Context * context,GLsizei count,GLsizei instanceCount)39 ANGLE_INLINE void MarkTransformFeedbackBufferUsage(const Context *context,
40                                                    GLsizei count,
41                                                    GLsizei instanceCount)
42 {
43     if (context->getStateCache().isTransformFeedbackActiveUnpaused())
44     {
45         TransformFeedback *transformFeedback = context->getState().getCurrentTransformFeedback();
46         transformFeedback->onVerticesDrawn(context, count, instanceCount);
47     }
48 }
49 
MarkShaderStorageUsage(const Context * context)50 ANGLE_INLINE void MarkShaderStorageUsage(const Context *context)
51 {
52     for (size_t index : context->getStateCache().getActiveShaderStorageBufferIndices())
53     {
54         Buffer *buffer = context->getState().getIndexedShaderStorageBuffer(index).get();
55         if (buffer)
56         {
57             buffer->onDataChanged();
58         }
59     }
60 
61     for (size_t index : context->getStateCache().getActiveImageUnitIndices())
62     {
63         const ImageUnit &imageUnit = context->getState().getImageUnit(index);
64         const Texture *texture     = imageUnit.texture.get();
65         if (texture)
66         {
67             texture->onStateChange(angle::SubjectMessage::ContentsChanged);
68         }
69     }
70 }
71 
72 // Return true if the draw is a no-op, else return false.
73 //  If there is no active program for the vertex or fragment shader stages, the results of vertex
74 //  and fragment shader execution will respectively be undefined. However, this is not
75 //  an error. ANGLE will treat this as a no-op.
76 //  A no-op draw occurs if the count of vertices is less than the minimum required to
77 //  have a valid primitive for this mode (0 for points, 0-1 for lines, 0-2 for tris).
noopDraw(PrimitiveMode mode,GLsizei count)78 ANGLE_INLINE bool Context::noopDraw(PrimitiveMode mode, GLsizei count) const
79 {
80     // Make sure any pending link is done before checking whether draw is allowed.
81     mState.ensureNoPendingLink(this);
82 
83     if (!mStateCache.getCanDraw())
84     {
85         return true;
86     }
87 
88     return count < kMinimumPrimitiveCounts[mode];
89 }
90 
noopMultiDraw(GLsizei drawcount)91 ANGLE_INLINE bool Context::noopMultiDraw(GLsizei drawcount) const
92 {
93     return drawcount == 0 || !mStateCache.getCanDraw();
94 }
95 
syncAllDirtyBits(Command command)96 ANGLE_INLINE angle::Result Context::syncAllDirtyBits(Command command)
97 {
98     constexpr state::DirtyBits kAllDirtyBits                 = state::DirtyBits().set();
99     constexpr state::ExtendedDirtyBits kAllExtendedDirtyBits = state::ExtendedDirtyBits().set();
100     const state::DirtyBits dirtyBits                         = mState.getDirtyBits();
101     const state::ExtendedDirtyBits extendedDirtyBits         = mState.getExtendedDirtyBits();
102     ANGLE_TRY(mImplementation->syncState(this, dirtyBits, kAllDirtyBits, extendedDirtyBits,
103                                          kAllExtendedDirtyBits, command));
104     mState.clearDirtyBits();
105     mState.clearExtendedDirtyBits();
106     return angle::Result::Continue;
107 }
108 
syncDirtyBits(const state::DirtyBits bitMask,const state::ExtendedDirtyBits extendedBitMask,Command command)109 ANGLE_INLINE angle::Result Context::syncDirtyBits(const state::DirtyBits bitMask,
110                                                   const state::ExtendedDirtyBits extendedBitMask,
111                                                   Command command)
112 {
113     const state::DirtyBits dirtyBits = (mState.getDirtyBits() & bitMask);
114     const state::ExtendedDirtyBits extendedDirtyBits =
115         (mState.getExtendedDirtyBits() & extendedBitMask);
116     ANGLE_TRY(mImplementation->syncState(this, dirtyBits, bitMask, extendedDirtyBits,
117                                          extendedBitMask, command));
118     mState.clearDirtyBits(dirtyBits);
119     mState.clearExtendedDirtyBits(extendedDirtyBits);
120     return angle::Result::Continue;
121 }
122 
syncDirtyObjects(const state::DirtyObjects & objectMask,Command command)123 ANGLE_INLINE angle::Result Context::syncDirtyObjects(const state::DirtyObjects &objectMask,
124                                                      Command command)
125 {
126     return mState.syncDirtyObjects(this, objectMask, command);
127 }
128 
prepareForDraw(PrimitiveMode mode)129 ANGLE_INLINE angle::Result Context::prepareForDraw(PrimitiveMode mode)
130 {
131     if (mGLES1Renderer)
132     {
133         ANGLE_TRY(mGLES1Renderer->prepareForDraw(mode, this, &mState, getMutableGLES1State()));
134     }
135 
136     ANGLE_TRY(syncDirtyObjects(mDrawDirtyObjects, Command::Draw));
137     ASSERT(!isRobustResourceInitEnabled() ||
138            !mState.getDrawFramebuffer()->hasResourceThatNeedsInit());
139     return syncAllDirtyBits(Command::Draw);
140 }
141 
drawArrays(PrimitiveMode mode,GLint first,GLsizei count)142 ANGLE_INLINE void Context::drawArrays(PrimitiveMode mode, GLint first, GLsizei count)
143 {
144     // No-op if count draws no primitives for given mode
145     if (noopDraw(mode, count))
146     {
147         ANGLE_CONTEXT_TRY(mImplementation->handleNoopDrawEvent());
148         return;
149     }
150 
151     ANGLE_CONTEXT_TRY(prepareForDraw(mode));
152     ANGLE_CONTEXT_TRY(mImplementation->drawArrays(this, mode, first, count));
153     MarkTransformFeedbackBufferUsage(this, count, 1);
154 }
155 
drawElements(PrimitiveMode mode,GLsizei count,DrawElementsType type,const void * indices)156 ANGLE_INLINE void Context::drawElements(PrimitiveMode mode,
157                                         GLsizei count,
158                                         DrawElementsType type,
159                                         const void *indices)
160 {
161     // No-op if count draws no primitives for given mode
162     if (noopDraw(mode, count))
163     {
164         ANGLE_CONTEXT_TRY(mImplementation->handleNoopDrawEvent());
165         return;
166     }
167 
168     ANGLE_CONTEXT_TRY(prepareForDraw(mode));
169     ANGLE_CONTEXT_TRY(mImplementation->drawElements(this, mode, count, type, indices));
170 }
171 
onBufferBindingChange(Context * context)172 ANGLE_INLINE void StateCache::onBufferBindingChange(Context *context)
173 {
174     updateBasicDrawStatesError();
175     updateBasicDrawElementsError();
176 }
177 
bindBuffer(BufferBinding target,BufferID buffer)178 ANGLE_INLINE void Context::bindBuffer(BufferBinding target, BufferID buffer)
179 {
180     Buffer *bufferObject =
181         mState.mBufferManager->checkBufferAllocation(mImplementation.get(), buffer);
182 
183     // Early return if rebinding the same buffer
184     if (bufferObject == mState.getTargetBuffer(target))
185     {
186         return;
187     }
188 
189     mState.setBufferBinding(this, target, bufferObject);
190     mStateCache.onBufferBindingChange(this);
191 
192     if (bufferObject)
193     {
194         bufferObject->onBind(this, target);
195     }
196 }
197 
198 }  // namespace gl
199 
200 #endif  // LIBANGLE_CONTEXT_INL_H_
201