Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/. */
5 :
6 : #include "WebGLTransformFeedback.h"
7 :
8 : #include "GLContext.h"
9 : #include "mozilla/dom/WebGL2RenderingContextBinding.h"
10 : #include "WebGL2Context.h"
11 : #include "WebGLProgram.h"
12 :
13 : namespace mozilla {
14 :
15 0 : WebGLTransformFeedback::WebGLTransformFeedback(WebGLContext* webgl, GLuint tf)
16 : : WebGLRefCountedObject(webgl)
17 : , mGLName(tf)
18 0 : , mIndexedBindings(webgl->mGLMaxTransformFeedbackSeparateAttribs)
19 : , mIsPaused(false)
20 0 : , mIsActive(false)
21 : {
22 0 : mContext->mTransformFeedbacks.insertBack(this);
23 0 : }
24 :
25 0 : WebGLTransformFeedback::~WebGLTransformFeedback()
26 : {
27 0 : DeleteOnce();
28 0 : }
29 :
30 : void
31 0 : WebGLTransformFeedback::Delete()
32 : {
33 0 : if (mGLName) {
34 0 : mContext->MakeContextCurrent();
35 0 : mContext->gl->fDeleteTransformFeedbacks(1, &mGLName);
36 : }
37 0 : removeFrom(mContext->mTransformFeedbacks);
38 0 : }
39 :
40 : ////////////////////////////////////////
41 :
42 : void
43 0 : WebGLTransformFeedback::BeginTransformFeedback(GLenum primMode)
44 : {
45 0 : const char funcName[] = "beginTransformFeedback";
46 :
47 0 : if (mIsActive)
48 0 : return mContext->ErrorInvalidOperation("%s: Already active.", funcName);
49 :
50 0 : switch (primMode) {
51 : case LOCAL_GL_POINTS:
52 : case LOCAL_GL_LINES:
53 : case LOCAL_GL_TRIANGLES:
54 0 : break;
55 : default:
56 0 : mContext->ErrorInvalidEnum("%s: `primitiveMode` must be one of POINTS, LINES, or"
57 : " TRIANGLES.",
58 0 : funcName);
59 0 : return;
60 : }
61 :
62 0 : const auto& prog = mContext->mCurrentProgram;
63 0 : if (!prog ||
64 0 : !prog->IsLinked() ||
65 0 : !prog->LinkInfo()->componentsPerTFVert.size())
66 : {
67 0 : mContext->ErrorInvalidOperation("%s: Current program not valid for transform"
68 : " feedback.",
69 0 : funcName);
70 0 : return;
71 : }
72 :
73 0 : const auto& linkInfo = prog->LinkInfo();
74 0 : const auto& componentsPerTFVert = linkInfo->componentsPerTFVert;
75 :
76 0 : size_t minVertCapacity = SIZE_MAX;
77 0 : for (size_t i = 0; i < componentsPerTFVert.size(); i++) {
78 0 : const auto& indexedBinding = mIndexedBindings[i];
79 0 : const auto& componentsPerVert = componentsPerTFVert[i];
80 :
81 0 : const auto& buffer = indexedBinding.mBufferBinding;
82 0 : if (!buffer) {
83 0 : mContext->ErrorInvalidOperation("%s: No buffer attached to required transform"
84 : " feedback index %u.",
85 0 : funcName, (uint32_t)i);
86 0 : return;
87 : }
88 :
89 0 : const size_t vertCapacity = buffer->ByteLength() / 4 / componentsPerVert;
90 0 : minVertCapacity = std::min(minVertCapacity, vertCapacity);
91 : }
92 :
93 : ////
94 :
95 0 : const auto& gl = mContext->gl;
96 0 : gl->MakeCurrent();
97 0 : gl->fBeginTransformFeedback(primMode);
98 :
99 : ////
100 :
101 0 : mIsActive = true;
102 0 : MOZ_ASSERT(!mIsPaused);
103 :
104 0 : mActive_Program = prog;
105 0 : mActive_PrimMode = primMode;
106 0 : mActive_VertPosition = 0;
107 0 : mActive_VertCapacity = minVertCapacity;
108 :
109 : ////
110 :
111 0 : mActive_Program->mNumActiveTFOs++;
112 : }
113 :
114 :
115 : void
116 0 : WebGLTransformFeedback::EndTransformFeedback()
117 : {
118 0 : const char funcName[] = "endTransformFeedback";
119 :
120 0 : if (!mIsActive)
121 0 : return mContext->ErrorInvalidOperation("%s: Not active.", funcName);
122 :
123 : ////
124 :
125 0 : const auto& gl = mContext->gl;
126 0 : gl->MakeCurrent();
127 0 : gl->fEndTransformFeedback();
128 :
129 : ////
130 :
131 0 : mIsActive = false;
132 0 : mIsPaused = false;
133 :
134 : ////
135 :
136 0 : mActive_Program->mNumActiveTFOs--;
137 : }
138 :
139 : void
140 0 : WebGLTransformFeedback::PauseTransformFeedback()
141 : {
142 0 : const char funcName[] = "pauseTransformFeedback";
143 :
144 0 : if (!mIsActive ||
145 0 : mIsPaused)
146 : {
147 0 : mContext->ErrorInvalidOperation("%s: Not active or is paused.", funcName);
148 0 : return;
149 : }
150 :
151 : ////
152 :
153 0 : const auto& gl = mContext->gl;
154 0 : gl->MakeCurrent();
155 0 : gl->fPauseTransformFeedback();
156 :
157 : ////
158 :
159 0 : mIsPaused = true;
160 : }
161 :
162 : void
163 0 : WebGLTransformFeedback::ResumeTransformFeedback()
164 : {
165 0 : const char funcName[] = "resumeTransformFeedback";
166 :
167 0 : if (!mIsPaused)
168 0 : return mContext->ErrorInvalidOperation("%s: Not paused.", funcName);
169 :
170 0 : if (mContext->mCurrentProgram != mActive_Program) {
171 0 : mContext->ErrorInvalidOperation("%s: Active program differs from original.",
172 0 : funcName);
173 0 : return;
174 : }
175 :
176 : ////
177 :
178 0 : const auto& gl = mContext->gl;
179 0 : gl->MakeCurrent();
180 0 : gl->fResumeTransformFeedback();
181 :
182 : ////
183 :
184 0 : MOZ_ASSERT(mIsActive);
185 0 : mIsPaused = false;
186 : }
187 :
188 : ////////////////////////////////////////
189 :
190 : void
191 0 : WebGLTransformFeedback::AddBufferBindCounts(int8_t addVal) const
192 : {
193 0 : const GLenum target = LOCAL_GL_TRANSFORM_FEEDBACK_BUFFER;
194 0 : WebGLBuffer::AddBindCount(target, mGenericBufferBinding.get(), addVal);
195 0 : for (const auto& binding : mIndexedBindings) {
196 0 : WebGLBuffer::AddBindCount(target, binding.mBufferBinding.get(), addVal);
197 : }
198 0 : }
199 :
200 : ////////////////////////////////////////
201 :
202 : JSObject*
203 0 : WebGLTransformFeedback::WrapObject(JSContext* cx, JS::Handle<JSObject*> givenProto)
204 : {
205 0 : return dom::WebGLTransformFeedbackBinding::Wrap(cx, this, givenProto);
206 : }
207 :
208 0 : NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(WebGLTransformFeedback, AddRef)
209 0 : NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(WebGLTransformFeedback, Release)
210 0 : NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(WebGLTransformFeedback,
211 : mGenericBufferBinding,
212 : mIndexedBindings,
213 : mActive_Program)
214 :
215 : } // namespace mozilla
|