1 //
2 // Copyright 2014 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 #ifndef LIBANGLE_TRANSFORM_FEEDBACK_H_
8 #define LIBANGLE_TRANSFORM_FEEDBACK_H_
9 
10 #include "libANGLE/RefCountObject.h"
11 
12 #include "common/PackedEnums.h"
13 #include "common/angleutils.h"
14 #include "libANGLE/Debug.h"
15 
16 #include "angle_gl.h"
17 
18 namespace rx
19 {
20 class GLImplFactory;
21 class TransformFeedbackImpl;
22 class TransformFeedbackGL;
23 }  // namespace rx
24 
25 namespace gl
26 {
27 class Buffer;
28 struct Caps;
29 class Context;
30 class Program;
31 
32 class TransformFeedbackState final : angle::NonCopyable
33 {
34   public:
35     TransformFeedbackState(size_t maxIndexedBuffers);
36     ~TransformFeedbackState();
37 
38     const OffsetBindingPointer<Buffer> &getIndexedBuffer(size_t idx) const;
39     const std::vector<OffsetBindingPointer<Buffer>> &getIndexedBuffers() const;
getBoundProgram()40     const Program *getBoundProgram() const { return mProgram; }
getVerticesDrawn()41     GLsizeiptr getVerticesDrawn() const { return mVerticesDrawn; }
42     GLsizeiptr getPrimitivesDrawn() const;
43 
44   private:
45     friend class TransformFeedback;
46 
47     std::string mLabel;
48 
49     bool mActive;
50     PrimitiveMode mPrimitiveMode;
51     bool mPaused;
52     GLsizeiptr mVerticesDrawn;
53     GLsizeiptr mVertexCapacity;
54 
55     Program *mProgram;
56 
57     std::vector<OffsetBindingPointer<Buffer>> mIndexedBuffers;
58 };
59 
60 class TransformFeedback final : public RefCountObject<TransformFeedbackID>, public LabeledObject
61 {
62   public:
63     TransformFeedback(rx::GLImplFactory *implFactory, TransformFeedbackID id, const Caps &caps);
64     ~TransformFeedback() override;
65     void onDestroy(const Context *context) override;
66 
67     void setLabel(const Context *context, const std::string &label) override;
68     const std::string &getLabel() const override;
69 
70     angle::Result begin(const Context *context, PrimitiveMode primitiveMode, Program *program);
71     angle::Result end(const Context *context);
72     angle::Result pause(const Context *context);
73     angle::Result resume(const Context *context);
74 
isActive()75     bool isActive() const { return mState.mActive; }
76 
77     bool isPaused() const;
78     PrimitiveMode getPrimitiveMode() const;
79     // Validates that the vertices produced by a draw call will fit in the bound transform feedback
80     // buffers.
81     bool checkBufferSpaceForDraw(GLsizei count, GLsizei primcount) const;
82     // This must be called after each draw call when transform feedback is enabled to keep track of
83     // how many vertices have been written to the buffers. This information is needed by
84     // checkBufferSpaceForDraw because each draw call appends vertices to the buffers starting just
85     // after the last vertex of the previous draw call.
86     void onVerticesDrawn(const Context *context, GLsizei count, GLsizei primcount);
87 
88     bool hasBoundProgram(ShaderProgramID program) const;
89 
90     angle::Result bindIndexedBuffer(const Context *context,
91                                     size_t index,
92                                     Buffer *buffer,
93                                     size_t offset,
94                                     size_t size);
95     const OffsetBindingPointer<Buffer> &getIndexedBuffer(size_t index) const;
96     size_t getIndexedBufferCount() const;
97     const std::vector<OffsetBindingPointer<Buffer>> &getIndexedBuffers() const;
98 
getVerticesDrawn()99     GLsizeiptr getVerticesDrawn() const { return mState.getVerticesDrawn(); }
getPrimitivesDrawn()100     GLsizeiptr getPrimitivesDrawn() const { return mState.getPrimitivesDrawn(); }
101 
102     // Returns true if any buffer bound to this object is also bound to another target.
103     bool buffersBoundForOtherUse() const;
104 
105     angle::Result detachBuffer(const Context *context, BufferID bufferID);
106 
107     rx::TransformFeedbackImpl *getImplementation() const;
108 
109     void onBindingChanged(const Context *context, bool bound);
110 
111   private:
112     void bindProgram(const Context *context, Program *program);
113 
114     TransformFeedbackState mState;
115     rx::TransformFeedbackImpl *mImplementation;
116 };
117 
118 }  // namespace gl
119 
120 #endif  // LIBANGLE_TRANSFORM_FEEDBACK_H_
121