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/GLES1Renderer.h"
15 #include "libANGLE/renderer/ContextImpl.h"
16 
17 #define ANGLE_HANDLE_ERR(X) \
18     (void)(X);              \
19     return;
20 #define ANGLE_CONTEXT_TRY(EXPR) ANGLE_TRY_TEMPLATE(EXPR, ANGLE_HANDLE_ERR)
21 
22 namespace gl
23 {
24 constexpr angle::PackedEnumMap<PrimitiveMode, GLsizei> kMinimumPrimitiveCounts = {{
25     {PrimitiveMode::Points, 1},
26     {PrimitiveMode::Lines, 2},
27     {PrimitiveMode::LineLoop, 2},
28     {PrimitiveMode::LineStrip, 2},
29     {PrimitiveMode::Triangles, 3},
30     {PrimitiveMode::TriangleStrip, 3},
31     {PrimitiveMode::TriangleFan, 3},
32     {PrimitiveMode::LinesAdjacency, 2},
33     {PrimitiveMode::LineStripAdjacency, 2},
34     {PrimitiveMode::TrianglesAdjacency, 3},
35     {PrimitiveMode::TriangleStripAdjacency, 3},
36 }};
37 
MarkTransformFeedbackBufferUsage(const Context * context,GLsizei count,GLsizei instanceCount)38 ANGLE_INLINE void MarkTransformFeedbackBufferUsage(const Context *context,
39                                                    GLsizei count,
40                                                    GLsizei instanceCount)
41 {
42     if (context->getStateCache().isTransformFeedbackActiveUnpaused())
43     {
44         TransformFeedback *transformFeedback = context->getState().getCurrentTransformFeedback();
45         transformFeedback->onVerticesDrawn(context, count, instanceCount);
46     }
47 }
48 
MarkShaderStorageUsage(const Context * context)49 ANGLE_INLINE void MarkShaderStorageUsage(const Context *context)
50 {
51     for (size_t index : context->getStateCache().getActiveShaderStorageBufferIndices())
52     {
53         Buffer *buffer = context->getState().getIndexedShaderStorageBuffer(index).get();
54         if (buffer)
55         {
56             buffer->onDataChanged();
57         }
58     }
59 
60     for (size_t index : context->getStateCache().getActiveImageUnitIndices())
61     {
62         const ImageUnit &imageUnit = context->getState().getImageUnit(index);
63         const Texture *texture     = imageUnit.texture.get();
64         if (texture)
65         {
66             texture->onStateChange(angle::SubjectMessage::ContentsChanged);
67         }
68     }
69 }
70 
71 // Return true if the draw is a no-op, else return false.
72 //  If there is no active program for the vertex or fragment shader stages, the results of vertex
73 //  and fragment shader execution will respectively be undefined. However, this is not
74 //  an error. ANGLE will treat this as a no-op.
75 //  A no-op draw occurs if the count of vertices is less than the minimum required to
76 //  have a valid primitive for this mode (0 for points, 0-1 for lines, 0-2 for tris).
noopDraw(PrimitiveMode mode,GLsizei count)77 ANGLE_INLINE bool Context::noopDraw(PrimitiveMode mode, GLsizei count) const
78 {
79     if (!mStateCache.getCanDraw())
80     {
81         return true;
82     }
83 
84     return count < kMinimumPrimitiveCounts[mode];
85 }
86 
syncDirtyBits()87 ANGLE_INLINE angle::Result Context::syncDirtyBits()
88 {
89     const State::DirtyBits &dirtyBits = mState.getDirtyBits();
90     ANGLE_TRY(mImplementation->syncState(this, dirtyBits, mAllDirtyBits));
91     mState.clearDirtyBits();
92     return angle::Result::Continue;
93 }
94 
syncDirtyBits(const State::DirtyBits & bitMask)95 ANGLE_INLINE angle::Result Context::syncDirtyBits(const State::DirtyBits &bitMask)
96 {
97     const State::DirtyBits &dirtyBits = (mState.getDirtyBits() & bitMask);
98     ANGLE_TRY(mImplementation->syncState(this, dirtyBits, bitMask));
99     mState.clearDirtyBits(dirtyBits);
100     return angle::Result::Continue;
101 }
102 
syncDirtyObjects(const State::DirtyObjects & objectMask,Command command)103 ANGLE_INLINE angle::Result Context::syncDirtyObjects(const State::DirtyObjects &objectMask,
104                                                      Command command)
105 {
106     return mState.syncDirtyObjects(this, objectMask, command);
107 }
108 
prepareForDraw(PrimitiveMode mode)109 ANGLE_INLINE angle::Result Context::prepareForDraw(PrimitiveMode mode)
110 {
111     if (mGLES1Renderer)
112     {
113         ANGLE_TRY(mGLES1Renderer->prepareForDraw(mode, this, &mState));
114     }
115 
116     ANGLE_TRY(syncDirtyObjects(mDrawDirtyObjects, Command::Draw));
117     ASSERT(!isRobustResourceInitEnabled() ||
118            !mState.getDrawFramebuffer()->hasResourceThatNeedsInit());
119     return syncDirtyBits();
120 }
121 
drawArrays(PrimitiveMode mode,GLint first,GLsizei count)122 ANGLE_INLINE void Context::drawArrays(PrimitiveMode mode, GLint first, GLsizei count)
123 {
124     // No-op if count draws no primitives for given mode
125     if (noopDraw(mode, count))
126     {
127         return;
128     }
129 
130     ANGLE_CONTEXT_TRY(prepareForDraw(mode));
131     ANGLE_CONTEXT_TRY(mImplementation->drawArrays(this, mode, first, count));
132     MarkTransformFeedbackBufferUsage(this, count, 1);
133 }
134 
drawElements(PrimitiveMode mode,GLsizei count,DrawElementsType type,const void * indices)135 ANGLE_INLINE void Context::drawElements(PrimitiveMode mode,
136                                         GLsizei count,
137                                         DrawElementsType type,
138                                         const void *indices)
139 {
140     // No-op if count draws no primitives for given mode
141     if (noopDraw(mode, count))
142     {
143         return;
144     }
145 
146     ANGLE_CONTEXT_TRY(prepareForDraw(mode));
147     ANGLE_CONTEXT_TRY(mImplementation->drawElements(this, mode, count, type, indices));
148 }
149 
onBufferBindingChange(Context * context)150 ANGLE_INLINE void StateCache::onBufferBindingChange(Context *context)
151 {
152     updateBasicDrawStatesError();
153     updateBasicDrawElementsError();
154 }
155 
bindBuffer(BufferBinding target,BufferID buffer)156 ANGLE_INLINE void Context::bindBuffer(BufferBinding target, BufferID buffer)
157 {
158     Buffer *bufferObject =
159         mState.mBufferManager->checkBufferAllocation(mImplementation.get(), buffer);
160     mState.setBufferBinding(this, target, bufferObject);
161     mStateCache.onBufferBindingChange(this);
162 }
163 
164 }  // namespace gl
165 
166 #endif  // LIBANGLE_CONTEXT_INL_H_
167