blob: 1b33c68d826d20c7b03b9e67c62c090457629f56 [file] [log] [blame]
Shannon Woods53a94a82014-06-24 15:20:36 -04001//
2// Copyright (c) 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// State.cpp: Implements the State class, encapsulating raw GL state.
8
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/State.h"
Shannon Woods53a94a82014-06-24 15:20:36 -040010
Sami Väisänene45e53b2016-05-25 10:36:04 +030011#include <limits>
12#include <string.h>
13
Jamie Madill20e005b2017-04-07 14:19:22 -040014#include "common/bitset_utils.h"
Sami Väisänene45e53b2016-05-25 10:36:04 +030015#include "common/mathutil.h"
jchen10a99ed552017-09-22 08:10:32 +080016#include "common/matrix_utils.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050017#include "libANGLE/Caps.h"
jchen10a99ed552017-09-22 08:10:32 +080018#include "libANGLE/Context.h"
Geoff Lang70d0f492015-12-10 17:45:46 -050019#include "libANGLE/Debug.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050020#include "libANGLE/Framebuffer.h"
21#include "libANGLE/FramebufferAttachment.h"
22#include "libANGLE/Query.h"
23#include "libANGLE/VertexArray.h"
24#include "libANGLE/formatutils.h"
jchen10a99ed552017-09-22 08:10:32 +080025#include "libANGLE/queryconversions.h"
Geoff Lang4751aab2017-10-30 15:14:52 -040026#include "libANGLE/renderer/ContextImpl.h"
Shannon Woods53a94a82014-06-24 15:20:36 -040027
Olli Etuahobbf1c102016-06-28 13:31:33 +030028namespace
29{
30
31GLenum ActiveQueryType(const GLenum type)
32{
33 return (type == GL_ANY_SAMPLES_PASSED_CONSERVATIVE) ? GL_ANY_SAMPLES_PASSED : type;
34}
35
36} // anonymous namepace
37
Shannon Woods53a94a82014-06-24 15:20:36 -040038namespace gl
39{
Geoff Lang76b10c92014-09-05 16:28:14 -040040
Shannon Woods53a94a82014-06-24 15:20:36 -040041State::State()
Jamie Madille79b1e12015-11-04 16:36:37 -050042 : mMaxDrawBuffers(0),
43 mMaxCombinedTextureImageUnits(0),
44 mDepthClearValue(0),
45 mStencilClearValue(0),
46 mScissorTest(false),
47 mSampleCoverage(false),
48 mSampleCoverageValue(0),
49 mSampleCoverageInvert(false),
Jiawei Shaodb342272017-09-27 10:21:45 +080050 mSampleMask(false),
51 mMaxSampleMaskWords(0),
Jamie Madille79b1e12015-11-04 16:36:37 -050052 mStencilRef(0),
53 mStencilBackRef(0),
54 mLineWidth(0),
55 mGenerateMipmapHint(GL_NONE),
56 mFragmentShaderDerivativeHint(GL_NONE),
Geoff Langf41a7152016-09-19 15:11:17 -040057 mBindGeneratesResource(true),
Geoff Langfeb8c682017-02-13 16:07:35 -050058 mClientArraysEnabled(true),
Jamie Madille79b1e12015-11-04 16:36:37 -050059 mNearZ(0),
60 mFarZ(0),
61 mReadFramebuffer(nullptr),
62 mDrawFramebuffer(nullptr),
63 mProgram(nullptr),
64 mVertexArray(nullptr),
65 mActiveSampler(0),
Sami Väisänen74c23472016-05-09 17:30:30 +030066 mPrimitiveRestart(false),
67 mMultiSampling(false),
Geoff Lang1d2c41d2016-10-19 16:14:46 -070068 mSampleAlphaToOne(false),
Jamie Madille08a1d32017-03-07 17:24:06 -050069 mFramebufferSRGB(true),
Jamie Madillc43be722017-07-13 16:22:14 -040070 mRobustResourceInit(false),
71 mProgramBinaryCacheEnabled(false)
Shannon Woods53a94a82014-06-24 15:20:36 -040072{
Geoff Lang76b10c92014-09-05 16:28:14 -040073}
74
75State::~State()
76{
Geoff Lang76b10c92014-09-05 16:28:14 -040077}
78
Jamie Madill4928b7c2017-06-20 12:57:39 -040079void State::initialize(const Context *context,
Geoff Langf41a7152016-09-19 15:11:17 -040080 bool debug,
Geoff Langfeb8c682017-02-13 16:07:35 -050081 bool bindGeneratesResource,
Jamie Madille08a1d32017-03-07 17:24:06 -050082 bool clientArraysEnabled,
Jamie Madillc43be722017-07-13 16:22:14 -040083 bool robustResourceInit,
84 bool programBinaryCacheEnabled)
Geoff Lang76b10c92014-09-05 16:28:14 -040085{
Jamie Madill4928b7c2017-06-20 12:57:39 -040086 const Caps &caps = context->getCaps();
87 const Extensions &extensions = context->getExtensions();
Geoff Lang4751aab2017-10-30 15:14:52 -040088 const Extensions &nativeExtensions = context->getImplementation()->getNativeExtensions();
Jamie Madill4928b7c2017-06-20 12:57:39 -040089 const Version &clientVersion = context->getClientVersion();
90
Shannon Woods2df6a602014-09-26 16:12:07 -040091 mMaxDrawBuffers = caps.maxDrawBuffers;
92 mMaxCombinedTextureImageUnits = caps.maxCombinedTextureImageUnits;
Shannon Woods53a94a82014-06-24 15:20:36 -040093
Jamie Madillf75ab352015-03-16 10:46:52 -040094 setColorClearValue(0.0f, 0.0f, 0.0f, 0.0f);
Shannon Woods53a94a82014-06-24 15:20:36 -040095
96 mDepthClearValue = 1.0f;
97 mStencilClearValue = 0;
98
Shannon Woods53a94a82014-06-24 15:20:36 -040099 mScissorTest = false;
100 mScissor.x = 0;
101 mScissor.y = 0;
102 mScissor.width = 0;
103 mScissor.height = 0;
104
Shannon Woods53a94a82014-06-24 15:20:36 -0400105 mBlendColor.red = 0;
106 mBlendColor.green = 0;
107 mBlendColor.blue = 0;
108 mBlendColor.alpha = 0;
109
Shannon Woods53a94a82014-06-24 15:20:36 -0400110 mStencilRef = 0;
111 mStencilBackRef = 0;
112
113 mSampleCoverage = false;
114 mSampleCoverageValue = 1.0f;
115 mSampleCoverageInvert = false;
Jiawei Shaodb342272017-09-27 10:21:45 +0800116
117 mMaxSampleMaskWords = caps.maxSampleMaskWords;
118 mSampleMask = false;
119 mSampleMaskValues.fill(~GLbitfield(0));
120
Shannon Woods53a94a82014-06-24 15:20:36 -0400121 mGenerateMipmapHint = GL_DONT_CARE;
122 mFragmentShaderDerivativeHint = GL_DONT_CARE;
123
Geoff Langf41a7152016-09-19 15:11:17 -0400124 mBindGeneratesResource = bindGeneratesResource;
Geoff Langfeb8c682017-02-13 16:07:35 -0500125 mClientArraysEnabled = clientArraysEnabled;
Geoff Langf41a7152016-09-19 15:11:17 -0400126
Shannon Woods53a94a82014-06-24 15:20:36 -0400127 mLineWidth = 1.0f;
128
129 mViewport.x = 0;
130 mViewport.y = 0;
131 mViewport.width = 0;
132 mViewport.height = 0;
133 mNearZ = 0.0f;
134 mFarZ = 1.0f;
135
136 mBlend.colorMaskRed = true;
137 mBlend.colorMaskGreen = true;
138 mBlend.colorMaskBlue = true;
139 mBlend.colorMaskAlpha = true;
140
Geoff Lang76b10c92014-09-05 16:28:14 -0400141 mActiveSampler = 0;
142
Shannon Woods23e05002014-09-22 19:07:27 -0400143 mVertexAttribCurrentValues.resize(caps.maxVertexAttributes);
Shannon Woods53a94a82014-06-24 15:20:36 -0400144
Geoff Lang4dc3af02016-11-18 14:09:27 -0500145 mUniformBuffers.resize(caps.maxUniformBufferBindings);
Shannon Woodsf3acaf92014-09-23 18:07:11 -0400146
Geoff Lang76b10c92014-09-05 16:28:14 -0400147 mSamplerTextures[GL_TEXTURE_2D].resize(caps.maxCombinedTextureImageUnits);
148 mSamplerTextures[GL_TEXTURE_CUBE_MAP].resize(caps.maxCombinedTextureImageUnits);
Geoff Langeb66a6e2016-10-31 13:06:12 -0400149 if (clientVersion >= Version(3, 0))
Shannon Woods53a94a82014-06-24 15:20:36 -0400150 {
Geoff Lang76b10c92014-09-05 16:28:14 -0400151 // TODO: These could also be enabled via extension
152 mSamplerTextures[GL_TEXTURE_2D_ARRAY].resize(caps.maxCombinedTextureImageUnits);
153 mSamplerTextures[GL_TEXTURE_3D].resize(caps.maxCombinedTextureImageUnits);
Shannon Woods53a94a82014-06-24 15:20:36 -0400154 }
Geoff Lang3b573612016-10-31 14:08:10 -0400155 if (clientVersion >= Version(3, 1))
156 {
157 mSamplerTextures[GL_TEXTURE_2D_MULTISAMPLE].resize(caps.maxCombinedTextureImageUnits);
Jiajia Qin6eafb042016-12-27 17:04:07 +0800158
159 mAtomicCounterBuffers.resize(caps.maxAtomicCounterBufferBindings);
Jiajia Qinf546e7d2017-03-27 14:12:59 +0800160 mShaderStorageBuffers.resize(caps.maxShaderStorageBufferBindings);
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800161 mImageUnits.resize(caps.maxImageUnits);
Geoff Lang3b573612016-10-31 14:08:10 -0400162 }
Geoff Lang4751aab2017-10-30 15:14:52 -0400163 if (nativeExtensions.textureRectangle)
Corentin Wallez13c0dd42017-07-04 18:27:01 -0400164 {
165 mSamplerTextures[GL_TEXTURE_RECTANGLE_ANGLE].resize(caps.maxCombinedTextureImageUnits);
166 }
Geoff Lang4751aab2017-10-30 15:14:52 -0400167 if (nativeExtensions.eglImageExternal || nativeExtensions.eglStreamConsumerExternal)
Ian Ewellbda75592016-04-18 17:25:54 -0400168 {
169 mSamplerTextures[GL_TEXTURE_EXTERNAL_OES].resize(caps.maxCombinedTextureImageUnits);
170 }
Jamie Madill81c2e252017-09-09 23:32:46 -0400171 mCompleteTextureCache.resize(caps.maxCombinedTextureImageUnits, nullptr);
172 mCompleteTextureBindings.reserve(caps.maxCombinedTextureImageUnits);
173 for (uint32_t textureIndex = 0; textureIndex < caps.maxCombinedTextureImageUnits;
174 ++textureIndex)
175 {
176 mCompleteTextureBindings.emplace_back(OnAttachmentDirtyBinding(this, textureIndex));
177 }
Shannon Woods53a94a82014-06-24 15:20:36 -0400178
Geoff Lang76b10c92014-09-05 16:28:14 -0400179 mSamplers.resize(caps.maxCombinedTextureImageUnits);
Shannon Woods53a94a82014-06-24 15:20:36 -0400180
Jamie Madill4928b7c2017-06-20 12:57:39 -0400181 mActiveQueries[GL_ANY_SAMPLES_PASSED].set(context, nullptr);
182 mActiveQueries[GL_ANY_SAMPLES_PASSED_CONSERVATIVE].set(context, nullptr);
183 mActiveQueries[GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN].set(context, nullptr);
184 mActiveQueries[GL_TIME_ELAPSED_EXT].set(context, nullptr);
185 mActiveQueries[GL_COMMANDS_COMPLETED_CHROMIUM].set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400186
Ian Ewell3ffd78b2016-01-22 16:09:42 -0500187 mProgram = nullptr;
Shannon Woods53a94a82014-06-24 15:20:36 -0400188
Ian Ewell3ffd78b2016-01-22 16:09:42 -0500189 mReadFramebuffer = nullptr;
190 mDrawFramebuffer = nullptr;
Jamie Madillb4b53c52015-02-03 15:22:48 -0500191
192 mPrimitiveRestart = false;
Geoff Lang70d0f492015-12-10 17:45:46 -0500193
194 mDebug.setOutputEnabled(debug);
195 mDebug.setMaxLoggedMessages(extensions.maxDebugLoggedMessages);
Sami Väisänen74c23472016-05-09 17:30:30 +0300196
Geoff Lang488130e2017-09-27 13:53:11 -0400197 mMultiSampling = true;
198 mSampleAlphaToOne = false;
Sami Väisänena797e062016-05-12 15:23:40 +0300199
200 mCoverageModulation = GL_NONE;
Sami Väisänene45e53b2016-05-25 10:36:04 +0300201
202 angle::Matrix<GLfloat>::setToIdentity(mPathMatrixProj);
203 angle::Matrix<GLfloat>::setToIdentity(mPathMatrixMV);
204 mPathStencilFunc = GL_ALWAYS;
205 mPathStencilRef = 0;
206 mPathStencilMask = std::numeric_limits<GLuint>::max();
Jamie Madille08a1d32017-03-07 17:24:06 -0500207
208 mRobustResourceInit = robustResourceInit;
Jamie Madillc43be722017-07-13 16:22:14 -0400209 mProgramBinaryCacheEnabled = programBinaryCacheEnabled;
Shannon Woods53a94a82014-06-24 15:20:36 -0400210}
211
Jamie Madill6c1f6712017-02-14 19:08:04 -0500212void State::reset(const Context *context)
Shannon Woods53a94a82014-06-24 15:20:36 -0400213{
Jamie Madill8693bdb2017-09-02 15:32:14 -0400214 for (auto &bindingVec : mSamplerTextures)
Shannon Woods53a94a82014-06-24 15:20:36 -0400215 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400216 TextureBindingVector &textureVector = bindingVec.second;
Geoff Lang76b10c92014-09-05 16:28:14 -0400217 for (size_t textureIdx = 0; textureIdx < textureVector.size(); textureIdx++)
Shannon Woods53a94a82014-06-24 15:20:36 -0400218 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400219 textureVector[textureIdx].set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400220 }
221 }
Geoff Lang76b10c92014-09-05 16:28:14 -0400222 for (size_t samplerIdx = 0; samplerIdx < mSamplers.size(); samplerIdx++)
223 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400224 mSamplers[samplerIdx].set(context, nullptr);
Geoff Lang76b10c92014-09-05 16:28:14 -0400225 }
Shannon Woods53a94a82014-06-24 15:20:36 -0400226
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800227 for (auto &imageUnit : mImageUnits)
228 {
229 imageUnit.texture.set(context, nullptr);
230 imageUnit.level = 0;
231 imageUnit.layered = false;
232 imageUnit.layer = 0;
233 imageUnit.access = GL_READ_ONLY;
234 imageUnit.format = GL_R32UI;
235 }
236
Jamie Madill4928b7c2017-06-20 12:57:39 -0400237 mRenderbuffer.set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400238
Corentin Wallez336129f2017-10-17 15:55:40 -0400239 for (auto type : angle::AllEnums<BufferBinding>())
240 {
241 mBoundBuffers[type].set(context, nullptr);
242 }
243
Geoff Lang7dd2e102014-11-10 15:19:26 -0500244 if (mProgram)
245 {
Jamie Madill6c1f6712017-02-14 19:08:04 -0500246 mProgram->release(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500247 }
Yunchao Hed7297bf2017-04-19 15:27:10 +0800248 mProgram = nullptr;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500249
Yunchao Hea336b902017-08-02 16:05:21 +0800250 mProgramPipeline.set(context, nullptr);
251
Jamie Madill4928b7c2017-06-20 12:57:39 -0400252 mTransformFeedback.set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400253
254 for (State::ActiveQueryMap::iterator i = mActiveQueries.begin(); i != mActiveQueries.end(); i++)
255 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400256 i->second.set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400257 }
258
Corentin Wallez336129f2017-10-17 15:55:40 -0400259 for (auto &buf : mUniformBuffers)
Shannon Woods53a94a82014-06-24 15:20:36 -0400260 {
Corentin Wallez336129f2017-10-17 15:55:40 -0400261 buf.set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400262 }
263
Jiajia Qin6eafb042016-12-27 17:04:07 +0800264 for (auto &buf : mAtomicCounterBuffers)
265 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400266 buf.set(context, nullptr);
Jiajia Qin6eafb042016-12-27 17:04:07 +0800267 }
268
Jiajia Qinf546e7d2017-03-27 14:12:59 +0800269 for (auto &buf : mShaderStorageBuffers)
270 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400271 buf.set(context, nullptr);
Jiajia Qinf546e7d2017-03-27 14:12:59 +0800272 }
273
Sami Väisänene45e53b2016-05-25 10:36:04 +0300274 angle::Matrix<GLfloat>::setToIdentity(mPathMatrixProj);
275 angle::Matrix<GLfloat>::setToIdentity(mPathMatrixMV);
276 mPathStencilFunc = GL_ALWAYS;
277 mPathStencilRef = 0;
278 mPathStencilMask = std::numeric_limits<GLuint>::max();
279
Jamie Madill1b94d432015-08-07 13:23:23 -0400280 // TODO(jmadill): Is this necessary?
281 setAllDirtyBits();
Shannon Woods53a94a82014-06-24 15:20:36 -0400282}
283
284const RasterizerState &State::getRasterizerState() const
285{
286 return mRasterizer;
287}
288
289const BlendState &State::getBlendState() const
290{
291 return mBlend;
292}
293
294const DepthStencilState &State::getDepthStencilState() const
295{
296 return mDepthStencil;
297}
298
Jamie Madillf75ab352015-03-16 10:46:52 -0400299void State::setColorClearValue(float red, float green, float blue, float alpha)
Shannon Woods53a94a82014-06-24 15:20:36 -0400300{
301 mColorClearValue.red = red;
302 mColorClearValue.green = green;
303 mColorClearValue.blue = blue;
304 mColorClearValue.alpha = alpha;
Jamie Madill1b94d432015-08-07 13:23:23 -0400305 mDirtyBits.set(DIRTY_BIT_CLEAR_COLOR);
Shannon Woods53a94a82014-06-24 15:20:36 -0400306}
307
Jamie Madillf75ab352015-03-16 10:46:52 -0400308void State::setDepthClearValue(float depth)
Shannon Woods53a94a82014-06-24 15:20:36 -0400309{
310 mDepthClearValue = depth;
Jamie Madill1b94d432015-08-07 13:23:23 -0400311 mDirtyBits.set(DIRTY_BIT_CLEAR_DEPTH);
Shannon Woods53a94a82014-06-24 15:20:36 -0400312}
313
Jamie Madillf75ab352015-03-16 10:46:52 -0400314void State::setStencilClearValue(int stencil)
Shannon Woods53a94a82014-06-24 15:20:36 -0400315{
316 mStencilClearValue = stencil;
Jamie Madill1b94d432015-08-07 13:23:23 -0400317 mDirtyBits.set(DIRTY_BIT_CLEAR_STENCIL);
Shannon Woods53a94a82014-06-24 15:20:36 -0400318}
319
Shannon Woods53a94a82014-06-24 15:20:36 -0400320void State::setColorMask(bool red, bool green, bool blue, bool alpha)
321{
322 mBlend.colorMaskRed = red;
323 mBlend.colorMaskGreen = green;
324 mBlend.colorMaskBlue = blue;
325 mBlend.colorMaskAlpha = alpha;
Jamie Madill1b94d432015-08-07 13:23:23 -0400326 mDirtyBits.set(DIRTY_BIT_COLOR_MASK);
Shannon Woods53a94a82014-06-24 15:20:36 -0400327}
328
329void State::setDepthMask(bool mask)
330{
331 mDepthStencil.depthMask = mask;
Jamie Madill1b94d432015-08-07 13:23:23 -0400332 mDirtyBits.set(DIRTY_BIT_DEPTH_MASK);
Shannon Woods53a94a82014-06-24 15:20:36 -0400333}
334
335bool State::isRasterizerDiscardEnabled() const
336{
337 return mRasterizer.rasterizerDiscard;
338}
339
340void State::setRasterizerDiscard(bool enabled)
341{
342 mRasterizer.rasterizerDiscard = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400343 mDirtyBits.set(DIRTY_BIT_RASTERIZER_DISCARD_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400344}
345
346bool State::isCullFaceEnabled() const
347{
348 return mRasterizer.cullFace;
349}
350
351void State::setCullFace(bool enabled)
352{
353 mRasterizer.cullFace = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400354 mDirtyBits.set(DIRTY_BIT_CULL_FACE_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400355}
356
Corentin Wallez2e568cf2017-09-18 17:05:22 -0400357void State::setCullMode(CullFaceMode mode)
Shannon Woods53a94a82014-06-24 15:20:36 -0400358{
359 mRasterizer.cullMode = mode;
Jamie Madill1b94d432015-08-07 13:23:23 -0400360 mDirtyBits.set(DIRTY_BIT_CULL_FACE);
Shannon Woods53a94a82014-06-24 15:20:36 -0400361}
362
363void State::setFrontFace(GLenum front)
364{
365 mRasterizer.frontFace = front;
Jamie Madill1b94d432015-08-07 13:23:23 -0400366 mDirtyBits.set(DIRTY_BIT_FRONT_FACE);
Shannon Woods53a94a82014-06-24 15:20:36 -0400367}
368
369bool State::isDepthTestEnabled() const
370{
371 return mDepthStencil.depthTest;
372}
373
374void State::setDepthTest(bool enabled)
375{
376 mDepthStencil.depthTest = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400377 mDirtyBits.set(DIRTY_BIT_DEPTH_TEST_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400378}
379
380void State::setDepthFunc(GLenum depthFunc)
381{
382 mDepthStencil.depthFunc = depthFunc;
Jamie Madill1b94d432015-08-07 13:23:23 -0400383 mDirtyBits.set(DIRTY_BIT_DEPTH_FUNC);
Shannon Woods53a94a82014-06-24 15:20:36 -0400384}
385
386void State::setDepthRange(float zNear, float zFar)
387{
388 mNearZ = zNear;
389 mFarZ = zFar;
Jamie Madill1b94d432015-08-07 13:23:23 -0400390 mDirtyBits.set(DIRTY_BIT_DEPTH_RANGE);
Shannon Woods53a94a82014-06-24 15:20:36 -0400391}
392
Geoff Langd42f5b82015-04-16 14:03:29 -0400393float State::getNearPlane() const
Shannon Woods53a94a82014-06-24 15:20:36 -0400394{
Geoff Langd42f5b82015-04-16 14:03:29 -0400395 return mNearZ;
396}
397
398float State::getFarPlane() const
399{
400 return mFarZ;
Shannon Woods53a94a82014-06-24 15:20:36 -0400401}
402
403bool State::isBlendEnabled() const
404{
405 return mBlend.blend;
406}
407
408void State::setBlend(bool enabled)
409{
410 mBlend.blend = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400411 mDirtyBits.set(DIRTY_BIT_BLEND_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400412}
413
414void State::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
415{
416 mBlend.sourceBlendRGB = sourceRGB;
417 mBlend.destBlendRGB = destRGB;
418 mBlend.sourceBlendAlpha = sourceAlpha;
419 mBlend.destBlendAlpha = destAlpha;
Jamie Madill1b94d432015-08-07 13:23:23 -0400420 mDirtyBits.set(DIRTY_BIT_BLEND_FUNCS);
Shannon Woods53a94a82014-06-24 15:20:36 -0400421}
422
423void State::setBlendColor(float red, float green, float blue, float alpha)
424{
425 mBlendColor.red = red;
426 mBlendColor.green = green;
427 mBlendColor.blue = blue;
428 mBlendColor.alpha = alpha;
Jamie Madill1b94d432015-08-07 13:23:23 -0400429 mDirtyBits.set(DIRTY_BIT_BLEND_COLOR);
Shannon Woods53a94a82014-06-24 15:20:36 -0400430}
431
432void State::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
433{
434 mBlend.blendEquationRGB = rgbEquation;
435 mBlend.blendEquationAlpha = alphaEquation;
Jamie Madill1b94d432015-08-07 13:23:23 -0400436 mDirtyBits.set(DIRTY_BIT_BLEND_EQUATIONS);
Shannon Woods53a94a82014-06-24 15:20:36 -0400437}
438
439const ColorF &State::getBlendColor() const
440{
441 return mBlendColor;
442}
443
444bool State::isStencilTestEnabled() const
445{
446 return mDepthStencil.stencilTest;
447}
448
449void State::setStencilTest(bool enabled)
450{
451 mDepthStencil.stencilTest = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400452 mDirtyBits.set(DIRTY_BIT_STENCIL_TEST_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400453}
454
455void State::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
456{
457 mDepthStencil.stencilFunc = stencilFunc;
458 mStencilRef = (stencilRef > 0) ? stencilRef : 0;
459 mDepthStencil.stencilMask = stencilMask;
Jamie Madill1b94d432015-08-07 13:23:23 -0400460 mDirtyBits.set(DIRTY_BIT_STENCIL_FUNCS_FRONT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400461}
462
463void State::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
464{
465 mDepthStencil.stencilBackFunc = stencilBackFunc;
466 mStencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
467 mDepthStencil.stencilBackMask = stencilBackMask;
Jamie Madill1b94d432015-08-07 13:23:23 -0400468 mDirtyBits.set(DIRTY_BIT_STENCIL_FUNCS_BACK);
Shannon Woods53a94a82014-06-24 15:20:36 -0400469}
470
471void State::setStencilWritemask(GLuint stencilWritemask)
472{
473 mDepthStencil.stencilWritemask = stencilWritemask;
Jamie Madill1b94d432015-08-07 13:23:23 -0400474 mDirtyBits.set(DIRTY_BIT_STENCIL_WRITEMASK_FRONT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400475}
476
477void State::setStencilBackWritemask(GLuint stencilBackWritemask)
478{
479 mDepthStencil.stencilBackWritemask = stencilBackWritemask;
Jamie Madill1b94d432015-08-07 13:23:23 -0400480 mDirtyBits.set(DIRTY_BIT_STENCIL_WRITEMASK_BACK);
Shannon Woods53a94a82014-06-24 15:20:36 -0400481}
482
483void State::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
484{
485 mDepthStencil.stencilFail = stencilFail;
486 mDepthStencil.stencilPassDepthFail = stencilPassDepthFail;
487 mDepthStencil.stencilPassDepthPass = stencilPassDepthPass;
Jamie Madill1b94d432015-08-07 13:23:23 -0400488 mDirtyBits.set(DIRTY_BIT_STENCIL_OPS_FRONT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400489}
490
491void State::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
492{
493 mDepthStencil.stencilBackFail = stencilBackFail;
494 mDepthStencil.stencilBackPassDepthFail = stencilBackPassDepthFail;
495 mDepthStencil.stencilBackPassDepthPass = stencilBackPassDepthPass;
Jamie Madill1b94d432015-08-07 13:23:23 -0400496 mDirtyBits.set(DIRTY_BIT_STENCIL_OPS_BACK);
Shannon Woods53a94a82014-06-24 15:20:36 -0400497}
498
499GLint State::getStencilRef() const
500{
501 return mStencilRef;
502}
503
504GLint State::getStencilBackRef() const
505{
506 return mStencilBackRef;
507}
508
509bool State::isPolygonOffsetFillEnabled() const
510{
511 return mRasterizer.polygonOffsetFill;
512}
513
514void State::setPolygonOffsetFill(bool enabled)
515{
Jamie Madill1b94d432015-08-07 13:23:23 -0400516 mRasterizer.polygonOffsetFill = enabled;
517 mDirtyBits.set(DIRTY_BIT_POLYGON_OFFSET_FILL_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400518}
519
520void State::setPolygonOffsetParams(GLfloat factor, GLfloat units)
521{
522 // An application can pass NaN values here, so handle this gracefully
523 mRasterizer.polygonOffsetFactor = factor != factor ? 0.0f : factor;
524 mRasterizer.polygonOffsetUnits = units != units ? 0.0f : units;
Jamie Madill1b94d432015-08-07 13:23:23 -0400525 mDirtyBits.set(DIRTY_BIT_POLYGON_OFFSET);
Shannon Woods53a94a82014-06-24 15:20:36 -0400526}
527
528bool State::isSampleAlphaToCoverageEnabled() const
529{
530 return mBlend.sampleAlphaToCoverage;
531}
532
533void State::setSampleAlphaToCoverage(bool enabled)
534{
535 mBlend.sampleAlphaToCoverage = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400536 mDirtyBits.set(DIRTY_BIT_SAMPLE_ALPHA_TO_COVERAGE_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400537}
538
539bool State::isSampleCoverageEnabled() const
540{
541 return mSampleCoverage;
542}
543
544void State::setSampleCoverage(bool enabled)
545{
546 mSampleCoverage = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400547 mDirtyBits.set(DIRTY_BIT_SAMPLE_COVERAGE_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400548}
549
550void State::setSampleCoverageParams(GLclampf value, bool invert)
551{
552 mSampleCoverageValue = value;
553 mSampleCoverageInvert = invert;
Jamie Madill1b94d432015-08-07 13:23:23 -0400554 mDirtyBits.set(DIRTY_BIT_SAMPLE_COVERAGE);
Shannon Woods53a94a82014-06-24 15:20:36 -0400555}
556
Geoff Lang0fbb6002015-04-16 11:11:53 -0400557GLclampf State::getSampleCoverageValue() const
Shannon Woods53a94a82014-06-24 15:20:36 -0400558{
Geoff Lang0fbb6002015-04-16 11:11:53 -0400559 return mSampleCoverageValue;
560}
Shannon Woods53a94a82014-06-24 15:20:36 -0400561
Geoff Lang0fbb6002015-04-16 11:11:53 -0400562bool State::getSampleCoverageInvert() const
563{
564 return mSampleCoverageInvert;
Shannon Woods53a94a82014-06-24 15:20:36 -0400565}
566
Jiawei Shaodb342272017-09-27 10:21:45 +0800567bool State::isSampleMaskEnabled() const
568{
569 return mSampleMask;
570}
571
572void State::setSampleMaskEnabled(bool enabled)
573{
574 mSampleMask = enabled;
575 mDirtyBits.set(DIRTY_BIT_SAMPLE_MASK_ENABLED);
576}
577
578void State::setSampleMaskParams(GLuint maskNumber, GLbitfield mask)
579{
580 ASSERT(maskNumber < mMaxSampleMaskWords);
581 mSampleMaskValues[maskNumber] = mask;
Jamie Madillc67323a2017-11-02 23:11:41 -0400582 // TODO(jmadill): Use a child dirty bit if we ever use more than two words.
583 mDirtyBits.set(DIRTY_BIT_SAMPLE_MASK);
Jiawei Shaodb342272017-09-27 10:21:45 +0800584}
585
586GLbitfield State::getSampleMaskWord(GLuint maskNumber) const
587{
588 ASSERT(maskNumber < mMaxSampleMaskWords);
589 return mSampleMaskValues[maskNumber];
590}
591
592GLuint State::getMaxSampleMaskWords() const
593{
594 return mMaxSampleMaskWords;
595}
596
Sami Väisänen74c23472016-05-09 17:30:30 +0300597void State::setSampleAlphaToOne(bool enabled)
598{
599 mSampleAlphaToOne = enabled;
600 mDirtyBits.set(DIRTY_BIT_SAMPLE_ALPHA_TO_ONE);
601}
602
603bool State::isSampleAlphaToOneEnabled() const
604{
605 return mSampleAlphaToOne;
606}
607
608void State::setMultisampling(bool enabled)
609{
610 mMultiSampling = enabled;
611 mDirtyBits.set(DIRTY_BIT_MULTISAMPLING);
612}
613
614bool State::isMultisamplingEnabled() const
615{
616 return mMultiSampling;
617}
618
Shannon Woods53a94a82014-06-24 15:20:36 -0400619bool State::isScissorTestEnabled() const
620{
621 return mScissorTest;
622}
623
624void State::setScissorTest(bool enabled)
625{
626 mScissorTest = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400627 mDirtyBits.set(DIRTY_BIT_SCISSOR_TEST_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400628}
629
630void State::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
631{
632 mScissor.x = x;
633 mScissor.y = y;
634 mScissor.width = width;
635 mScissor.height = height;
Jamie Madill1b94d432015-08-07 13:23:23 -0400636 mDirtyBits.set(DIRTY_BIT_SCISSOR);
Shannon Woods53a94a82014-06-24 15:20:36 -0400637}
638
639const Rectangle &State::getScissor() const
640{
641 return mScissor;
642}
643
644bool State::isDitherEnabled() const
645{
646 return mBlend.dither;
647}
648
649void State::setDither(bool enabled)
650{
651 mBlend.dither = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400652 mDirtyBits.set(DIRTY_BIT_DITHER_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400653}
654
Jamie Madillb4b53c52015-02-03 15:22:48 -0500655bool State::isPrimitiveRestartEnabled() const
656{
657 return mPrimitiveRestart;
658}
659
660void State::setPrimitiveRestart(bool enabled)
661{
662 mPrimitiveRestart = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400663 mDirtyBits.set(DIRTY_BIT_PRIMITIVE_RESTART_ENABLED);
Jamie Madillb4b53c52015-02-03 15:22:48 -0500664}
665
Shannon Woods53a94a82014-06-24 15:20:36 -0400666void State::setEnableFeature(GLenum feature, bool enabled)
667{
668 switch (feature)
669 {
Sami Väisänen74c23472016-05-09 17:30:30 +0300670 case GL_MULTISAMPLE_EXT: setMultisampling(enabled); break;
671 case GL_SAMPLE_ALPHA_TO_ONE_EXT: setSampleAlphaToOne(enabled); break;
Shannon Woods53a94a82014-06-24 15:20:36 -0400672 case GL_CULL_FACE: setCullFace(enabled); break;
673 case GL_POLYGON_OFFSET_FILL: setPolygonOffsetFill(enabled); break;
674 case GL_SAMPLE_ALPHA_TO_COVERAGE: setSampleAlphaToCoverage(enabled); break;
675 case GL_SAMPLE_COVERAGE: setSampleCoverage(enabled); break;
676 case GL_SCISSOR_TEST: setScissorTest(enabled); break;
677 case GL_STENCIL_TEST: setStencilTest(enabled); break;
678 case GL_DEPTH_TEST: setDepthTest(enabled); break;
679 case GL_BLEND: setBlend(enabled); break;
680 case GL_DITHER: setDither(enabled); break;
Jamie Madillb4b53c52015-02-03 15:22:48 -0500681 case GL_PRIMITIVE_RESTART_FIXED_INDEX: setPrimitiveRestart(enabled); break;
Shannon Woods53a94a82014-06-24 15:20:36 -0400682 case GL_RASTERIZER_DISCARD: setRasterizerDiscard(enabled); break;
Geoff Lang3b573612016-10-31 14:08:10 -0400683 case GL_SAMPLE_MASK:
Jiawei Shaodb342272017-09-27 10:21:45 +0800684 setSampleMaskEnabled(enabled);
Geoff Lang3b573612016-10-31 14:08:10 -0400685 break;
Geoff Lang70d0f492015-12-10 17:45:46 -0500686 case GL_DEBUG_OUTPUT_SYNCHRONOUS:
687 mDebug.setOutputSynchronous(enabled);
688 break;
689 case GL_DEBUG_OUTPUT:
690 mDebug.setOutputEnabled(enabled);
691 break;
Geoff Lang1d2c41d2016-10-19 16:14:46 -0700692 case GL_FRAMEBUFFER_SRGB_EXT:
693 setFramebufferSRGB(enabled);
694 break;
Shannon Woods53a94a82014-06-24 15:20:36 -0400695 default: UNREACHABLE();
696 }
697}
698
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700699bool State::getEnableFeature(GLenum feature) const
Shannon Woods53a94a82014-06-24 15:20:36 -0400700{
701 switch (feature)
702 {
Sami Väisänen74c23472016-05-09 17:30:30 +0300703 case GL_MULTISAMPLE_EXT: return isMultisamplingEnabled();
704 case GL_SAMPLE_ALPHA_TO_ONE_EXT: return isSampleAlphaToOneEnabled();
Shannon Woods53a94a82014-06-24 15:20:36 -0400705 case GL_CULL_FACE: return isCullFaceEnabled();
706 case GL_POLYGON_OFFSET_FILL: return isPolygonOffsetFillEnabled();
707 case GL_SAMPLE_ALPHA_TO_COVERAGE: return isSampleAlphaToCoverageEnabled();
708 case GL_SAMPLE_COVERAGE: return isSampleCoverageEnabled();
709 case GL_SCISSOR_TEST: return isScissorTestEnabled();
710 case GL_STENCIL_TEST: return isStencilTestEnabled();
711 case GL_DEPTH_TEST: return isDepthTestEnabled();
712 case GL_BLEND: return isBlendEnabled();
713 case GL_DITHER: return isDitherEnabled();
Jamie Madillb4b53c52015-02-03 15:22:48 -0500714 case GL_PRIMITIVE_RESTART_FIXED_INDEX: return isPrimitiveRestartEnabled();
Shannon Woods53a94a82014-06-24 15:20:36 -0400715 case GL_RASTERIZER_DISCARD: return isRasterizerDiscardEnabled();
Geoff Langb5e997f2016-12-06 10:55:34 -0500716 case GL_SAMPLE_MASK:
Jiawei Shaodb342272017-09-27 10:21:45 +0800717 return isSampleMaskEnabled();
Geoff Lang70d0f492015-12-10 17:45:46 -0500718 case GL_DEBUG_OUTPUT_SYNCHRONOUS:
719 return mDebug.isOutputSynchronous();
720 case GL_DEBUG_OUTPUT:
721 return mDebug.isOutputEnabled();
Geoff Langf41a7152016-09-19 15:11:17 -0400722 case GL_BIND_GENERATES_RESOURCE_CHROMIUM:
723 return isBindGeneratesResourceEnabled();
Geoff Langfeb8c682017-02-13 16:07:35 -0500724 case GL_CLIENT_ARRAYS_ANGLE:
725 return areClientArraysEnabled();
Geoff Lang1d2c41d2016-10-19 16:14:46 -0700726 case GL_FRAMEBUFFER_SRGB_EXT:
727 return getFramebufferSRGB();
Geoff Langb433e872017-10-05 14:01:47 -0400728 case GL_ROBUST_RESOURCE_INITIALIZATION_ANGLE:
Jamie Madille08a1d32017-03-07 17:24:06 -0500729 return mRobustResourceInit;
Jamie Madillc43be722017-07-13 16:22:14 -0400730 case GL_PROGRAM_CACHE_ENABLED_ANGLE:
731 return mProgramBinaryCacheEnabled;
732
733 default:
734 UNREACHABLE();
735 return false;
Shannon Woods53a94a82014-06-24 15:20:36 -0400736 }
737}
738
739void State::setLineWidth(GLfloat width)
740{
741 mLineWidth = width;
Jamie Madill1b94d432015-08-07 13:23:23 -0400742 mDirtyBits.set(DIRTY_BIT_LINE_WIDTH);
Shannon Woods53a94a82014-06-24 15:20:36 -0400743}
744
Geoff Lang4b3f4162015-04-16 13:22:05 -0400745float State::getLineWidth() const
746{
747 return mLineWidth;
748}
749
Shannon Woods53a94a82014-06-24 15:20:36 -0400750void State::setGenerateMipmapHint(GLenum hint)
751{
752 mGenerateMipmapHint = hint;
Jamie Madill1b94d432015-08-07 13:23:23 -0400753 mDirtyBits.set(DIRTY_BIT_GENERATE_MIPMAP_HINT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400754}
755
756void State::setFragmentShaderDerivativeHint(GLenum hint)
757{
758 mFragmentShaderDerivativeHint = hint;
Jamie Madill1b94d432015-08-07 13:23:23 -0400759 mDirtyBits.set(DIRTY_BIT_SHADER_DERIVATIVE_HINT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400760 // TODO: Propagate the hint to shader translator so we can write
761 // ddx, ddx_coarse, or ddx_fine depending on the hint.
762 // Ignore for now. It is valid for implementations to ignore hint.
763}
764
Geoff Langf41a7152016-09-19 15:11:17 -0400765bool State::isBindGeneratesResourceEnabled() const
766{
767 return mBindGeneratesResource;
768}
769
Geoff Langfeb8c682017-02-13 16:07:35 -0500770bool State::areClientArraysEnabled() const
771{
772 return mClientArraysEnabled;
773}
774
Shannon Woods53a94a82014-06-24 15:20:36 -0400775void State::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
776{
777 mViewport.x = x;
778 mViewport.y = y;
779 mViewport.width = width;
780 mViewport.height = height;
Jamie Madill1b94d432015-08-07 13:23:23 -0400781 mDirtyBits.set(DIRTY_BIT_VIEWPORT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400782}
783
784const Rectangle &State::getViewport() const
785{
786 return mViewport;
787}
788
789void State::setActiveSampler(unsigned int active)
790{
791 mActiveSampler = active;
792}
793
794unsigned int State::getActiveSampler() const
795{
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700796 return static_cast<unsigned int>(mActiveSampler);
Shannon Woods53a94a82014-06-24 15:20:36 -0400797}
798
Jamie Madill4928b7c2017-06-20 12:57:39 -0400799void State::setSamplerTexture(const Context *context, GLenum type, Texture *texture)
Shannon Woods53a94a82014-06-24 15:20:36 -0400800{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400801 mSamplerTextures[type][mActiveSampler].set(context, texture);
Jamie Madill81c2e252017-09-09 23:32:46 -0400802 mDirtyBits.set(DIRTY_BIT_TEXTURE_BINDINGS);
803 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
Shannon Woods53a94a82014-06-24 15:20:36 -0400804}
805
Jamie Madillc29968b2016-01-20 11:17:23 -0500806Texture *State::getTargetTexture(GLenum target) const
807{
808 return getSamplerTexture(static_cast<unsigned int>(mActiveSampler), target);
809}
810
Geoff Lang76b10c92014-09-05 16:28:14 -0400811Texture *State::getSamplerTexture(unsigned int sampler, GLenum type) const
Shannon Woods53a94a82014-06-24 15:20:36 -0400812{
Jamie Madill5864ac22015-01-12 14:43:07 -0500813 const auto it = mSamplerTextures.find(type);
814 ASSERT(it != mSamplerTextures.end());
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400815 ASSERT(sampler < it->second.size());
Jamie Madill5864ac22015-01-12 14:43:07 -0500816 return it->second[sampler].get();
Shannon Woods53a94a82014-06-24 15:20:36 -0400817}
818
Geoff Lang76b10c92014-09-05 16:28:14 -0400819GLuint State::getSamplerTextureId(unsigned int sampler, GLenum type) const
Shannon Woods53a94a82014-06-24 15:20:36 -0400820{
Jamie Madill5864ac22015-01-12 14:43:07 -0500821 const auto it = mSamplerTextures.find(type);
822 ASSERT(it != mSamplerTextures.end());
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400823 ASSERT(sampler < it->second.size());
Jamie Madill5864ac22015-01-12 14:43:07 -0500824 return it->second[sampler].id();
Shannon Woods53a94a82014-06-24 15:20:36 -0400825}
826
Jamie Madilla02315b2017-02-23 14:14:47 -0500827void State::detachTexture(const Context *context, const TextureMap &zeroTextures, GLuint texture)
Shannon Woods53a94a82014-06-24 15:20:36 -0400828{
829 // Textures have a detach method on State rather than a simple
830 // removeBinding, because the zero/null texture objects are managed
831 // separately, and don't have to go through the Context's maps or
832 // the ResourceManager.
833
834 // [OpenGL ES 2.0.24] section 3.8 page 84:
835 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
836 // rebound to texture object zero
837
Corentin Walleza2257da2016-04-19 16:43:12 -0400838 for (auto &bindingVec : mSamplerTextures)
Shannon Woods53a94a82014-06-24 15:20:36 -0400839 {
Corentin Walleza2257da2016-04-19 16:43:12 -0400840 GLenum textureType = bindingVec.first;
841 TextureBindingVector &textureVector = bindingVec.second;
Jamie Madill81c2e252017-09-09 23:32:46 -0400842 for (BindingPointer<Texture> &binding : textureVector)
Shannon Woods53a94a82014-06-24 15:20:36 -0400843 {
Geoff Lang76b10c92014-09-05 16:28:14 -0400844 if (binding.id() == texture)
Shannon Woods53a94a82014-06-24 15:20:36 -0400845 {
Jamie Madill5864ac22015-01-12 14:43:07 -0500846 auto it = zeroTextures.find(textureType);
847 ASSERT(it != zeroTextures.end());
Jamie Madille6382c32014-11-07 15:05:26 -0500848 // Zero textures are the "default" textures instead of NULL
Jamie Madill4928b7c2017-06-20 12:57:39 -0400849 binding.set(context, it->second.get());
Jamie Madill81c2e252017-09-09 23:32:46 -0400850 mDirtyBits.set(DIRTY_BIT_TEXTURE_BINDINGS);
Shannon Woods53a94a82014-06-24 15:20:36 -0400851 }
852 }
853 }
854
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800855 for (auto &bindingImageUnit : mImageUnits)
856 {
857 if (bindingImageUnit.texture.id() == texture)
858 {
859 bindingImageUnit.texture.set(context, nullptr);
860 bindingImageUnit.level = 0;
861 bindingImageUnit.layered = false;
862 bindingImageUnit.layer = 0;
863 bindingImageUnit.access = GL_READ_ONLY;
864 bindingImageUnit.format = GL_R32UI;
865 break;
866 }
867 }
868
Shannon Woods53a94a82014-06-24 15:20:36 -0400869 // [OpenGL ES 2.0.24] section 4.4 page 112:
870 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
871 // as if Texture2DAttachment had been called, with a texture of 0, for each attachment point to which this
872 // image was attached in the currently bound framebuffer.
873
Jamie Madill8693bdb2017-09-02 15:32:14 -0400874 if (mReadFramebuffer && mReadFramebuffer->detachTexture(context, texture))
Shannon Woods53a94a82014-06-24 15:20:36 -0400875 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400876 mDirtyObjects.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
Shannon Woods53a94a82014-06-24 15:20:36 -0400877 }
878
Jamie Madill8693bdb2017-09-02 15:32:14 -0400879 if (mDrawFramebuffer && mDrawFramebuffer->detachTexture(context, texture))
Shannon Woods53a94a82014-06-24 15:20:36 -0400880 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400881 mDirtyObjects.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
Shannon Woods53a94a82014-06-24 15:20:36 -0400882 }
883}
884
Jamie Madill4928b7c2017-06-20 12:57:39 -0400885void State::initializeZeroTextures(const Context *context, const TextureMap &zeroTextures)
Jamie Madille6382c32014-11-07 15:05:26 -0500886{
887 for (const auto &zeroTexture : zeroTextures)
888 {
889 auto &samplerTextureArray = mSamplerTextures[zeroTexture.first];
890
891 for (size_t textureUnit = 0; textureUnit < samplerTextureArray.size(); ++textureUnit)
892 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400893 samplerTextureArray[textureUnit].set(context, zeroTexture.second.get());
Jamie Madille6382c32014-11-07 15:05:26 -0500894 }
895 }
896}
897
Jamie Madill4928b7c2017-06-20 12:57:39 -0400898void State::setSamplerBinding(const Context *context, GLuint textureUnit, Sampler *sampler)
Shannon Woods53a94a82014-06-24 15:20:36 -0400899{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400900 mSamplers[textureUnit].set(context, sampler);
Jamie Madill81c2e252017-09-09 23:32:46 -0400901 mDirtyBits.set(DIRTY_BIT_SAMPLER_BINDINGS);
902 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
Shannon Woods53a94a82014-06-24 15:20:36 -0400903}
904
905GLuint State::getSamplerId(GLuint textureUnit) const
906{
Geoff Lang76b10c92014-09-05 16:28:14 -0400907 ASSERT(textureUnit < mSamplers.size());
Shannon Woods53a94a82014-06-24 15:20:36 -0400908 return mSamplers[textureUnit].id();
909}
910
911Sampler *State::getSampler(GLuint textureUnit) const
912{
913 return mSamplers[textureUnit].get();
914}
915
Jamie Madill4928b7c2017-06-20 12:57:39 -0400916void State::detachSampler(const Context *context, GLuint sampler)
Shannon Woods53a94a82014-06-24 15:20:36 -0400917{
918 // [OpenGL ES 3.0.2] section 3.8.2 pages 123-124:
919 // If a sampler object that is currently bound to one or more texture units is
920 // deleted, it is as though BindSampler is called once for each texture unit to
921 // which the sampler is bound, with unit set to the texture unit and sampler set to zero.
Jamie Madill81c2e252017-09-09 23:32:46 -0400922 for (BindingPointer<Sampler> &samplerBinding : mSamplers)
Shannon Woods53a94a82014-06-24 15:20:36 -0400923 {
Geoff Lang76b10c92014-09-05 16:28:14 -0400924 if (samplerBinding.id() == sampler)
Shannon Woods53a94a82014-06-24 15:20:36 -0400925 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400926 samplerBinding.set(context, nullptr);
Jamie Madill81c2e252017-09-09 23:32:46 -0400927 mDirtyBits.set(DIRTY_BIT_SAMPLER_BINDINGS);
Shannon Woods53a94a82014-06-24 15:20:36 -0400928 }
929 }
930}
931
Jamie Madill4928b7c2017-06-20 12:57:39 -0400932void State::setRenderbufferBinding(const Context *context, Renderbuffer *renderbuffer)
Shannon Woods53a94a82014-06-24 15:20:36 -0400933{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400934 mRenderbuffer.set(context, renderbuffer);
Jamie Madill8693bdb2017-09-02 15:32:14 -0400935 mDirtyBits.set(DIRTY_BIT_RENDERBUFFER_BINDING);
Shannon Woods53a94a82014-06-24 15:20:36 -0400936}
937
938GLuint State::getRenderbufferId() const
939{
940 return mRenderbuffer.id();
941}
942
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700943Renderbuffer *State::getCurrentRenderbuffer() const
Shannon Woods53a94a82014-06-24 15:20:36 -0400944{
945 return mRenderbuffer.get();
946}
947
Jamie Madilla02315b2017-02-23 14:14:47 -0500948void State::detachRenderbuffer(const Context *context, GLuint renderbuffer)
Shannon Woods53a94a82014-06-24 15:20:36 -0400949{
950 // [OpenGL ES 2.0.24] section 4.4 page 109:
951 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
952 // had been executed with the target RENDERBUFFER and name of zero.
953
954 if (mRenderbuffer.id() == renderbuffer)
955 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400956 setRenderbufferBinding(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400957 }
958
959 // [OpenGL ES 2.0.24] section 4.4 page 111:
960 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
961 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
962 // point to which this image was attached in the currently bound framebuffer.
963
964 Framebuffer *readFramebuffer = mReadFramebuffer;
965 Framebuffer *drawFramebuffer = mDrawFramebuffer;
966
Jamie Madill8693bdb2017-09-02 15:32:14 -0400967 if (readFramebuffer && readFramebuffer->detachRenderbuffer(context, renderbuffer))
Shannon Woods53a94a82014-06-24 15:20:36 -0400968 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400969 mDirtyObjects.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
Shannon Woods53a94a82014-06-24 15:20:36 -0400970 }
971
972 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
973 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400974 if (drawFramebuffer->detachRenderbuffer(context, renderbuffer))
975 {
976 mDirtyObjects.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
977 }
Shannon Woods53a94a82014-06-24 15:20:36 -0400978 }
979
980}
981
982void State::setReadFramebufferBinding(Framebuffer *framebuffer)
983{
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500984 if (mReadFramebuffer == framebuffer)
985 return;
986
Shannon Woods53a94a82014-06-24 15:20:36 -0400987 mReadFramebuffer = framebuffer;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500988 mDirtyBits.set(DIRTY_BIT_READ_FRAMEBUFFER_BINDING);
989
990 if (mReadFramebuffer && mReadFramebuffer->hasAnyDirtyBit())
991 {
992 mDirtyObjects.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
993 }
Shannon Woods53a94a82014-06-24 15:20:36 -0400994}
995
996void State::setDrawFramebufferBinding(Framebuffer *framebuffer)
997{
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500998 if (mDrawFramebuffer == framebuffer)
999 return;
1000
Shannon Woods53a94a82014-06-24 15:20:36 -04001001 mDrawFramebuffer = framebuffer;
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001002 mDirtyBits.set(DIRTY_BIT_DRAW_FRAMEBUFFER_BINDING);
1003
1004 if (mDrawFramebuffer && mDrawFramebuffer->hasAnyDirtyBit())
1005 {
1006 mDirtyObjects.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
1007 }
Shannon Woods53a94a82014-06-24 15:20:36 -04001008}
1009
1010Framebuffer *State::getTargetFramebuffer(GLenum target) const
1011{
1012 switch (target)
1013 {
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001014 case GL_READ_FRAMEBUFFER_ANGLE:
1015 return mReadFramebuffer;
1016 case GL_DRAW_FRAMEBUFFER_ANGLE:
1017 case GL_FRAMEBUFFER:
1018 return mDrawFramebuffer;
1019 default:
1020 UNREACHABLE();
Yunchao Hef81ce4a2017-04-24 10:49:17 +08001021 return nullptr;
Shannon Woods53a94a82014-06-24 15:20:36 -04001022 }
1023}
1024
Jamie Madill51f40ec2016-06-15 14:06:00 -04001025Framebuffer *State::getReadFramebuffer() const
Shannon Woods53a94a82014-06-24 15:20:36 -04001026{
1027 return mReadFramebuffer;
1028}
1029
Jamie Madill51f40ec2016-06-15 14:06:00 -04001030Framebuffer *State::getDrawFramebuffer() const
Shannon Woods53a94a82014-06-24 15:20:36 -04001031{
1032 return mDrawFramebuffer;
1033}
1034
1035bool State::removeReadFramebufferBinding(GLuint framebuffer)
1036{
Jamie Madill77a72f62015-04-14 11:18:32 -04001037 if (mReadFramebuffer != nullptr &&
1038 mReadFramebuffer->id() == framebuffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001039 {
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001040 setReadFramebufferBinding(nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -04001041 return true;
1042 }
1043
1044 return false;
1045}
1046
1047bool State::removeDrawFramebufferBinding(GLuint framebuffer)
1048{
Jamie Madill77a72f62015-04-14 11:18:32 -04001049 if (mReadFramebuffer != nullptr &&
1050 mDrawFramebuffer->id() == framebuffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001051 {
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001052 setDrawFramebufferBinding(nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -04001053 return true;
1054 }
1055
1056 return false;
1057}
1058
1059void State::setVertexArrayBinding(VertexArray *vertexArray)
1060{
1061 mVertexArray = vertexArray;
Jamie Madill0b9e9032015-08-17 11:51:52 +00001062 mDirtyBits.set(DIRTY_BIT_VERTEX_ARRAY_BINDING);
Jamie Madillc9d442d2016-01-20 11:17:24 -05001063
1064 if (mVertexArray && mVertexArray->hasAnyDirtyBit())
1065 {
1066 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1067 }
Shannon Woods53a94a82014-06-24 15:20:36 -04001068}
1069
1070GLuint State::getVertexArrayId() const
1071{
Yunchao He4f285442017-04-21 12:15:49 +08001072 ASSERT(mVertexArray != nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -04001073 return mVertexArray->id();
1074}
1075
1076VertexArray *State::getVertexArray() const
1077{
Yunchao He4f285442017-04-21 12:15:49 +08001078 ASSERT(mVertexArray != nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -04001079 return mVertexArray;
1080}
1081
1082bool State::removeVertexArrayBinding(GLuint vertexArray)
1083{
1084 if (mVertexArray->id() == vertexArray)
1085 {
Yunchao Hed7297bf2017-04-19 15:27:10 +08001086 mVertexArray = nullptr;
Jamie Madill0b9e9032015-08-17 11:51:52 +00001087 mDirtyBits.set(DIRTY_BIT_VERTEX_ARRAY_BINDING);
Jamie Madillc9d442d2016-01-20 11:17:24 -05001088 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
Shannon Woods53a94a82014-06-24 15:20:36 -04001089 return true;
1090 }
1091
1092 return false;
1093}
1094
Jamie Madill4928b7c2017-06-20 12:57:39 -04001095void State::bindVertexBuffer(const Context *context,
1096 GLuint bindingIndex,
Shao80957d92017-02-20 21:25:59 +08001097 Buffer *boundBuffer,
1098 GLintptr offset,
1099 GLsizei stride)
1100{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001101 getVertexArray()->bindVertexBuffer(context, bindingIndex, boundBuffer, offset, stride);
Shao80957d92017-02-20 21:25:59 +08001102 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1103}
1104
Shaodde78e82017-05-22 14:13:27 +08001105void State::setVertexAttribBinding(const Context *context, GLuint attribIndex, GLuint bindingIndex)
Shao80957d92017-02-20 21:25:59 +08001106{
Shaodde78e82017-05-22 14:13:27 +08001107 getVertexArray()->setVertexAttribBinding(context, attribIndex, bindingIndex);
Shao80957d92017-02-20 21:25:59 +08001108 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1109}
1110
1111void State::setVertexAttribFormat(GLuint attribIndex,
1112 GLint size,
1113 GLenum type,
1114 bool normalized,
1115 bool pureInteger,
1116 GLuint relativeOffset)
1117{
1118 getVertexArray()->setVertexAttribFormat(attribIndex, size, type, normalized, pureInteger,
1119 relativeOffset);
1120 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1121}
1122
1123void State::setVertexBindingDivisor(GLuint bindingIndex, GLuint divisor)
1124{
1125 getVertexArray()->setVertexBindingDivisor(bindingIndex, divisor);
1126 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1127}
1128
Jamie Madill6c1f6712017-02-14 19:08:04 -05001129void State::setProgram(const Context *context, Program *newProgram)
Shannon Woods53a94a82014-06-24 15:20:36 -04001130{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001131 if (mProgram != newProgram)
Shannon Woods53a94a82014-06-24 15:20:36 -04001132 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001133 if (mProgram)
1134 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001135 mProgram->release(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001136 }
1137
1138 mProgram = newProgram;
1139
1140 if (mProgram)
1141 {
1142 newProgram->addRef();
Jamie Madill81c2e252017-09-09 23:32:46 -04001143 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001144 }
Jamie Madilla779b612017-07-24 11:46:05 -04001145 mDirtyBits.set(DIRTY_BIT_PROGRAM_EXECUTABLE);
1146 mDirtyBits.set(DIRTY_BIT_PROGRAM_BINDING);
Shannon Woods53a94a82014-06-24 15:20:36 -04001147 }
1148}
1149
Geoff Lang7dd2e102014-11-10 15:19:26 -05001150Program *State::getProgram() const
Shannon Woods53a94a82014-06-24 15:20:36 -04001151{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001152 return mProgram;
Shannon Woods53a94a82014-06-24 15:20:36 -04001153}
1154
Jamie Madill4928b7c2017-06-20 12:57:39 -04001155void State::setTransformFeedbackBinding(const Context *context,
1156 TransformFeedback *transformFeedback)
Shannon Woods53a94a82014-06-24 15:20:36 -04001157{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001158 mTransformFeedback.set(context, transformFeedback);
Shannon Woods53a94a82014-06-24 15:20:36 -04001159}
1160
1161TransformFeedback *State::getCurrentTransformFeedback() const
1162{
1163 return mTransformFeedback.get();
1164}
1165
Gregoire Payen de La Garanderie52742022015-02-04 14:55:39 +00001166bool State::isTransformFeedbackActiveUnpaused() const
1167{
Jamie Madill81c2e252017-09-09 23:32:46 -04001168 TransformFeedback *curTransformFeedback = getCurrentTransformFeedback();
Geoff Langbb0a0bb2015-03-27 12:16:57 -04001169 return curTransformFeedback && curTransformFeedback->isActive() && !curTransformFeedback->isPaused();
Gregoire Payen de La Garanderie52742022015-02-04 14:55:39 +00001170}
1171
Jamie Madill4928b7c2017-06-20 12:57:39 -04001172bool State::removeTransformFeedbackBinding(const Context *context, GLuint transformFeedback)
Shannon Woods53a94a82014-06-24 15:20:36 -04001173{
1174 if (mTransformFeedback.id() == transformFeedback)
1175 {
Jamie Madill4928b7c2017-06-20 12:57:39 -04001176 mTransformFeedback.set(context, nullptr);
Corentin Walleza2257da2016-04-19 16:43:12 -04001177 return true;
Shannon Woods53a94a82014-06-24 15:20:36 -04001178 }
Corentin Walleza2257da2016-04-19 16:43:12 -04001179
1180 return false;
Shannon Woods53a94a82014-06-24 15:20:36 -04001181}
1182
Yunchao Hea336b902017-08-02 16:05:21 +08001183void State::setProgramPipelineBinding(const Context *context, ProgramPipeline *pipeline)
1184{
1185 mProgramPipeline.set(context, pipeline);
1186}
1187
1188void State::detachProgramPipeline(const Context *context, GLuint pipeline)
1189{
1190 mProgramPipeline.set(context, nullptr);
1191}
1192
Olli Etuahobbf1c102016-06-28 13:31:33 +03001193bool State::isQueryActive(const GLenum type) const
Shannon Woods53a94a82014-06-24 15:20:36 -04001194{
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001195 for (auto &iter : mActiveQueries)
Shannon Woods53a94a82014-06-24 15:20:36 -04001196 {
Olli Etuahobbf1c102016-06-28 13:31:33 +03001197 const Query *query = iter.second.get();
1198 if (query != nullptr && ActiveQueryType(query->getType()) == ActiveQueryType(type))
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001199 {
1200 return true;
1201 }
1202 }
1203
1204 return false;
1205}
1206
1207bool State::isQueryActive(Query *query) const
1208{
1209 for (auto &iter : mActiveQueries)
1210 {
1211 if (iter.second.get() == query)
Shannon Woods53a94a82014-06-24 15:20:36 -04001212 {
1213 return true;
1214 }
1215 }
1216
1217 return false;
1218}
1219
Jamie Madill4928b7c2017-06-20 12:57:39 -04001220void State::setActiveQuery(const Context *context, GLenum target, Query *query)
Shannon Woods53a94a82014-06-24 15:20:36 -04001221{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001222 mActiveQueries[target].set(context, query);
Shannon Woods53a94a82014-06-24 15:20:36 -04001223}
1224
1225GLuint State::getActiveQueryId(GLenum target) const
1226{
1227 const Query *query = getActiveQuery(target);
1228 return (query ? query->id() : 0u);
1229}
1230
1231Query *State::getActiveQuery(GLenum target) const
1232{
Jamie Madill5864ac22015-01-12 14:43:07 -05001233 const auto it = mActiveQueries.find(target);
Shannon Woods53a94a82014-06-24 15:20:36 -04001234
Jamie Madill5864ac22015-01-12 14:43:07 -05001235 // All query types should already exist in the activeQueries map
1236 ASSERT(it != mActiveQueries.end());
1237
1238 return it->second.get();
Shannon Woods53a94a82014-06-24 15:20:36 -04001239}
1240
Corentin Wallez336129f2017-10-17 15:55:40 -04001241void State::setBufferBinding(const Context *context, BufferBinding target, Buffer *buffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001242{
Corentin Wallez336129f2017-10-17 15:55:40 -04001243 switch (target)
1244 {
1245 case BufferBinding::PixelPack:
Corentin Wallezd5d30992017-11-13 10:21:52 -08001246 mBoundBuffers[target].set(context, buffer);
Corentin Wallez336129f2017-10-17 15:55:40 -04001247 mDirtyBits.set(DIRTY_BIT_PACK_BUFFER_BINDING);
1248 break;
1249 case BufferBinding::PixelUnpack:
Corentin Wallezd5d30992017-11-13 10:21:52 -08001250 mBoundBuffers[target].set(context, buffer);
Corentin Wallez336129f2017-10-17 15:55:40 -04001251 mDirtyBits.set(DIRTY_BIT_UNPACK_BUFFER_BINDING);
1252 break;
1253 case BufferBinding::DrawIndirect:
Corentin Wallezd5d30992017-11-13 10:21:52 -08001254 mBoundBuffers[target].set(context, buffer);
Corentin Wallez336129f2017-10-17 15:55:40 -04001255 mDirtyBits.set(DIRTY_BIT_DRAW_INDIRECT_BUFFER_BINDING);
1256 break;
1257 case BufferBinding::TransformFeedback:
1258 if (mTransformFeedback.get() != nullptr)
1259 {
1260 mTransformFeedback->bindGenericBuffer(context, buffer);
1261 }
1262 break;
1263 case BufferBinding::ElementArray:
1264 getVertexArray()->setElementArrayBuffer(context, buffer);
1265 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1266 break;
1267 default:
Corentin Wallezd5d30992017-11-13 10:21:52 -08001268 mBoundBuffers[target].set(context, buffer);
Corentin Wallez336129f2017-10-17 15:55:40 -04001269 break;
1270 }
Shannon Woods53a94a82014-06-24 15:20:36 -04001271}
Corentin Wallez336129f2017-10-17 15:55:40 -04001272void State::setIndexedBufferBinding(const Context *context,
1273 BufferBinding target,
1274 GLuint index,
1275 Buffer *buffer,
1276 GLintptr offset,
1277 GLsizeiptr size)
Jiajia Qin9d7d0b12016-11-29 16:30:31 +08001278{
Corentin Wallez336129f2017-10-17 15:55:40 -04001279 setBufferBinding(context, target, buffer);
Jiajia Qin9d7d0b12016-11-29 16:30:31 +08001280
Corentin Wallez336129f2017-10-17 15:55:40 -04001281 switch (target)
1282 {
1283 case BufferBinding::TransformFeedback:
1284 mTransformFeedback->bindIndexedBuffer(context, index, buffer, offset, size);
1285 break;
1286 case BufferBinding::Uniform:
1287 mUniformBuffers[index].set(context, buffer, offset, size);
1288 break;
1289 case BufferBinding::AtomicCounter:
1290 mAtomicCounterBuffers[index].set(context, buffer, offset, size);
1291 break;
1292 case BufferBinding::ShaderStorage:
1293 mShaderStorageBuffers[index].set(context, buffer, offset, size);
1294 break;
1295 default:
1296 UNREACHABLE();
1297 break;
1298 }
Shannon Woods53a94a82014-06-24 15:20:36 -04001299}
1300
Geoff Lang5d124a62015-09-15 13:03:27 -04001301const OffsetBindingPointer<Buffer> &State::getIndexedUniformBuffer(size_t index) const
Shannon Woods53a94a82014-06-24 15:20:36 -04001302{
Shannon Woodsf3acaf92014-09-23 18:07:11 -04001303 ASSERT(static_cast<size_t>(index) < mUniformBuffers.size());
Geoff Lang5d124a62015-09-15 13:03:27 -04001304 return mUniformBuffers[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001305}
1306
Jiajia Qin6eafb042016-12-27 17:04:07 +08001307const OffsetBindingPointer<Buffer> &State::getIndexedAtomicCounterBuffer(size_t index) const
1308{
1309 ASSERT(static_cast<size_t>(index) < mAtomicCounterBuffers.size());
1310 return mAtomicCounterBuffers[index];
1311}
1312
Jiajia Qinf546e7d2017-03-27 14:12:59 +08001313const OffsetBindingPointer<Buffer> &State::getIndexedShaderStorageBuffer(size_t index) const
1314{
1315 ASSERT(static_cast<size_t>(index) < mShaderStorageBuffers.size());
1316 return mShaderStorageBuffers[index];
1317}
1318
Corentin Wallez336129f2017-10-17 15:55:40 -04001319Buffer *State::getTargetBuffer(BufferBinding target) const
Shannon Woods53a94a82014-06-24 15:20:36 -04001320{
1321 switch (target)
1322 {
Corentin Wallez336129f2017-10-17 15:55:40 -04001323 case BufferBinding::ElementArray:
1324 return getVertexArray()->getElementArrayBuffer().get();
1325 case BufferBinding::TransformFeedback:
1326 return mTransformFeedback->getGenericBuffer().get();
1327 default:
1328 return mBoundBuffers[target].get();
Shannon Woods53a94a82014-06-24 15:20:36 -04001329 }
1330}
1331
Jamie Madill4928b7c2017-06-20 12:57:39 -04001332void State::detachBuffer(const Context *context, GLuint bufferName)
Yuly Novikov5807a532015-12-03 13:01:22 -05001333{
Corentin Wallez336129f2017-10-17 15:55:40 -04001334 for (auto &buffer : mBoundBuffers)
Yuly Novikov5807a532015-12-03 13:01:22 -05001335 {
Corentin Wallez336129f2017-10-17 15:55:40 -04001336 if (buffer.id() == bufferName)
Yuly Novikov5807a532015-12-03 13:01:22 -05001337 {
Corentin Wallez336129f2017-10-17 15:55:40 -04001338 buffer.set(context, nullptr);
Yuly Novikov5807a532015-12-03 13:01:22 -05001339 }
1340 }
1341
1342 TransformFeedback *curTransformFeedback = getCurrentTransformFeedback();
1343 if (curTransformFeedback)
1344 {
Jamie Madill4928b7c2017-06-20 12:57:39 -04001345 curTransformFeedback->detachBuffer(context, bufferName);
Yuly Novikov5807a532015-12-03 13:01:22 -05001346 }
1347
Jamie Madill4928b7c2017-06-20 12:57:39 -04001348 getVertexArray()->detachBuffer(context, bufferName);
Yuly Novikov5807a532015-12-03 13:01:22 -05001349}
1350
Shannon Woods53a94a82014-06-24 15:20:36 -04001351void State::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
1352{
1353 getVertexArray()->enableAttribute(attribNum, enabled);
Jamie Madillc9d442d2016-01-20 11:17:24 -05001354 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
Shannon Woods53a94a82014-06-24 15:20:36 -04001355}
1356
1357void State::setVertexAttribf(GLuint index, const GLfloat values[4])
1358{
Shannon Woods23e05002014-09-22 19:07:27 -04001359 ASSERT(static_cast<size_t>(index) < mVertexAttribCurrentValues.size());
Shannon Woods53a94a82014-06-24 15:20:36 -04001360 mVertexAttribCurrentValues[index].setFloatValues(values);
Jamie Madillc67323a2017-11-02 23:11:41 -04001361 mDirtyBits.set(DIRTY_BIT_CURRENT_VALUES);
1362 mDirtyCurrentValues.set(index);
Shannon Woods53a94a82014-06-24 15:20:36 -04001363}
1364
1365void State::setVertexAttribu(GLuint index, const GLuint values[4])
1366{
Shannon Woods23e05002014-09-22 19:07:27 -04001367 ASSERT(static_cast<size_t>(index) < mVertexAttribCurrentValues.size());
Shannon Woods53a94a82014-06-24 15:20:36 -04001368 mVertexAttribCurrentValues[index].setUnsignedIntValues(values);
Jamie Madillc67323a2017-11-02 23:11:41 -04001369 mDirtyBits.set(DIRTY_BIT_CURRENT_VALUES);
1370 mDirtyCurrentValues.set(index);
Shannon Woods53a94a82014-06-24 15:20:36 -04001371}
1372
1373void State::setVertexAttribi(GLuint index, const GLint values[4])
1374{
Shannon Woods23e05002014-09-22 19:07:27 -04001375 ASSERT(static_cast<size_t>(index) < mVertexAttribCurrentValues.size());
Shannon Woods53a94a82014-06-24 15:20:36 -04001376 mVertexAttribCurrentValues[index].setIntValues(values);
Jamie Madillc67323a2017-11-02 23:11:41 -04001377 mDirtyBits.set(DIRTY_BIT_CURRENT_VALUES);
1378 mDirtyCurrentValues.set(index);
Shannon Woods53a94a82014-06-24 15:20:36 -04001379}
1380
Shaodde78e82017-05-22 14:13:27 +08001381void State::setVertexAttribPointer(const Context *context,
1382 unsigned int attribNum,
1383 Buffer *boundBuffer,
1384 GLint size,
1385 GLenum type,
1386 bool normalized,
1387 bool pureInteger,
1388 GLsizei stride,
1389 const void *pointer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001390{
Shaodde78e82017-05-22 14:13:27 +08001391 getVertexArray()->setVertexAttribPointer(context, attribNum, boundBuffer, size, type,
1392 normalized, pureInteger, stride, pointer);
Jamie Madillc9d442d2016-01-20 11:17:24 -05001393 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
Jamie Madill0b9e9032015-08-17 11:51:52 +00001394}
1395
Shaodde78e82017-05-22 14:13:27 +08001396void State::setVertexAttribDivisor(const Context *context, GLuint index, GLuint divisor)
Jamie Madill0b9e9032015-08-17 11:51:52 +00001397{
Shaodde78e82017-05-22 14:13:27 +08001398 getVertexArray()->setVertexAttribDivisor(context, index, divisor);
Jamie Madillc9d442d2016-01-20 11:17:24 -05001399 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
Shannon Woods53a94a82014-06-24 15:20:36 -04001400}
1401
Jamie Madill6de51852017-04-12 09:53:01 -04001402const VertexAttribCurrentValueData &State::getVertexAttribCurrentValue(size_t attribNum) const
Shannon Woods53a94a82014-06-24 15:20:36 -04001403{
Jamie Madill6de51852017-04-12 09:53:01 -04001404 ASSERT(attribNum < mVertexAttribCurrentValues.size());
Shannon Woods53a94a82014-06-24 15:20:36 -04001405 return mVertexAttribCurrentValues[attribNum];
1406}
1407
Jamie Madillcac94a92017-11-10 10:09:32 -05001408const std::vector<VertexAttribCurrentValueData> &State::getVertexAttribCurrentValues() const
1409{
1410 return mVertexAttribCurrentValues;
1411}
1412
Shannon Woods53a94a82014-06-24 15:20:36 -04001413const void *State::getVertexAttribPointer(unsigned int attribNum) const
1414{
1415 return getVertexArray()->getVertexAttribute(attribNum).pointer;
1416}
1417
1418void State::setPackAlignment(GLint alignment)
1419{
1420 mPack.alignment = alignment;
Jamie Madillc67323a2017-11-02 23:11:41 -04001421 mDirtyBits.set(DIRTY_BIT_PACK_STATE);
Shannon Woods53a94a82014-06-24 15:20:36 -04001422}
1423
1424GLint State::getPackAlignment() const
1425{
1426 return mPack.alignment;
1427}
1428
1429void State::setPackReverseRowOrder(bool reverseRowOrder)
1430{
1431 mPack.reverseRowOrder = reverseRowOrder;
Jamie Madillc67323a2017-11-02 23:11:41 -04001432 mDirtyBits.set(DIRTY_BIT_PACK_STATE);
Shannon Woods53a94a82014-06-24 15:20:36 -04001433}
1434
1435bool State::getPackReverseRowOrder() const
1436{
1437 return mPack.reverseRowOrder;
1438}
1439
Minmin Gongadff67b2015-10-14 10:34:45 -04001440void State::setPackRowLength(GLint rowLength)
1441{
1442 mPack.rowLength = rowLength;
Jamie Madillc67323a2017-11-02 23:11:41 -04001443 mDirtyBits.set(DIRTY_BIT_PACK_STATE);
Minmin Gongadff67b2015-10-14 10:34:45 -04001444}
1445
1446GLint State::getPackRowLength() const
1447{
1448 return mPack.rowLength;
1449}
1450
1451void State::setPackSkipRows(GLint skipRows)
1452{
1453 mPack.skipRows = skipRows;
Jamie Madillc67323a2017-11-02 23:11:41 -04001454 mDirtyBits.set(DIRTY_BIT_PACK_STATE);
Minmin Gongadff67b2015-10-14 10:34:45 -04001455}
1456
1457GLint State::getPackSkipRows() const
1458{
1459 return mPack.skipRows;
1460}
1461
1462void State::setPackSkipPixels(GLint skipPixels)
1463{
1464 mPack.skipPixels = skipPixels;
Jamie Madillc67323a2017-11-02 23:11:41 -04001465 mDirtyBits.set(DIRTY_BIT_PACK_STATE);
Minmin Gongadff67b2015-10-14 10:34:45 -04001466}
1467
1468GLint State::getPackSkipPixels() const
1469{
1470 return mPack.skipPixels;
1471}
1472
Shannon Woods53a94a82014-06-24 15:20:36 -04001473const PixelPackState &State::getPackState() const
1474{
1475 return mPack;
1476}
1477
Jamie Madill87de3622015-03-16 10:41:44 -04001478PixelPackState &State::getPackState()
1479{
1480 return mPack;
1481}
1482
Shannon Woods53a94a82014-06-24 15:20:36 -04001483void State::setUnpackAlignment(GLint alignment)
1484{
1485 mUnpack.alignment = alignment;
Jamie Madillc67323a2017-11-02 23:11:41 -04001486 mDirtyBits.set(DIRTY_BIT_UNPACK_STATE);
Shannon Woods53a94a82014-06-24 15:20:36 -04001487}
1488
1489GLint State::getUnpackAlignment() const
1490{
1491 return mUnpack.alignment;
1492}
1493
Minmin Gongb8aee3b2015-01-27 14:42:36 -08001494void State::setUnpackRowLength(GLint rowLength)
1495{
1496 mUnpack.rowLength = rowLength;
Jamie Madillc67323a2017-11-02 23:11:41 -04001497 mDirtyBits.set(DIRTY_BIT_UNPACK_STATE);
Minmin Gongb8aee3b2015-01-27 14:42:36 -08001498}
1499
1500GLint State::getUnpackRowLength() const
1501{
1502 return mUnpack.rowLength;
1503}
1504
Minmin Gongadff67b2015-10-14 10:34:45 -04001505void State::setUnpackImageHeight(GLint imageHeight)
1506{
1507 mUnpack.imageHeight = imageHeight;
Jamie Madillc67323a2017-11-02 23:11:41 -04001508 mDirtyBits.set(DIRTY_BIT_UNPACK_STATE);
Minmin Gongadff67b2015-10-14 10:34:45 -04001509}
1510
1511GLint State::getUnpackImageHeight() const
1512{
1513 return mUnpack.imageHeight;
1514}
1515
1516void State::setUnpackSkipImages(GLint skipImages)
1517{
1518 mUnpack.skipImages = skipImages;
Jamie Madillc67323a2017-11-02 23:11:41 -04001519 mDirtyBits.set(DIRTY_BIT_UNPACK_STATE);
Minmin Gongadff67b2015-10-14 10:34:45 -04001520}
1521
1522GLint State::getUnpackSkipImages() const
1523{
1524 return mUnpack.skipImages;
1525}
1526
1527void State::setUnpackSkipRows(GLint skipRows)
1528{
1529 mUnpack.skipRows = skipRows;
Jamie Madillc67323a2017-11-02 23:11:41 -04001530 mDirtyBits.set(DIRTY_BIT_UNPACK_STATE);
Minmin Gongadff67b2015-10-14 10:34:45 -04001531}
1532
1533GLint State::getUnpackSkipRows() const
1534{
1535 return mUnpack.skipRows;
1536}
1537
1538void State::setUnpackSkipPixels(GLint skipPixels)
1539{
1540 mUnpack.skipPixels = skipPixels;
Jamie Madillc67323a2017-11-02 23:11:41 -04001541 mDirtyBits.set(DIRTY_BIT_UNPACK_STATE);
Minmin Gongadff67b2015-10-14 10:34:45 -04001542}
1543
1544GLint State::getUnpackSkipPixels() const
1545{
1546 return mUnpack.skipPixels;
1547}
1548
Shannon Woods53a94a82014-06-24 15:20:36 -04001549const PixelUnpackState &State::getUnpackState() const
1550{
1551 return mUnpack;
1552}
1553
Jamie Madill67102f02015-03-16 10:41:42 -04001554PixelUnpackState &State::getUnpackState()
1555{
1556 return mUnpack;
1557}
1558
Geoff Lang70d0f492015-12-10 17:45:46 -05001559const Debug &State::getDebug() const
1560{
1561 return mDebug;
1562}
1563
1564Debug &State::getDebug()
1565{
1566 return mDebug;
1567}
1568
Sami Väisänena797e062016-05-12 15:23:40 +03001569void State::setCoverageModulation(GLenum components)
1570{
1571 mCoverageModulation = components;
1572 mDirtyBits.set(DIRTY_BIT_COVERAGE_MODULATION);
1573}
1574
1575GLenum State::getCoverageModulation() const
1576{
1577 return mCoverageModulation;
1578}
1579
Sami Väisänene45e53b2016-05-25 10:36:04 +03001580void State::loadPathRenderingMatrix(GLenum matrixMode, const GLfloat *matrix)
1581{
1582 if (matrixMode == GL_PATH_MODELVIEW_CHROMIUM)
1583 {
1584 memcpy(mPathMatrixMV, matrix, 16 * sizeof(GLfloat));
1585 mDirtyBits.set(DIRTY_BIT_PATH_RENDERING_MATRIX_MV);
1586 }
1587 else if (matrixMode == GL_PATH_PROJECTION_CHROMIUM)
1588 {
1589 memcpy(mPathMatrixProj, matrix, 16 * sizeof(GLfloat));
1590 mDirtyBits.set(DIRTY_BIT_PATH_RENDERING_MATRIX_PROJ);
1591 }
1592 else
1593 {
1594 UNREACHABLE();
1595 }
1596}
1597
1598const GLfloat *State::getPathRenderingMatrix(GLenum which) const
1599{
1600 if (which == GL_PATH_MODELVIEW_MATRIX_CHROMIUM)
1601 {
1602 return mPathMatrixMV;
1603 }
1604 else if (which == GL_PATH_PROJECTION_MATRIX_CHROMIUM)
1605 {
1606 return mPathMatrixProj;
1607 }
1608
1609 UNREACHABLE();
1610 return nullptr;
1611}
1612
1613void State::setPathStencilFunc(GLenum func, GLint ref, GLuint mask)
1614{
1615 mPathStencilFunc = func;
1616 mPathStencilRef = ref;
1617 mPathStencilMask = mask;
1618 mDirtyBits.set(DIRTY_BIT_PATH_RENDERING_STENCIL_STATE);
1619}
1620
1621GLenum State::getPathStencilFunc() const
1622{
1623 return mPathStencilFunc;
1624}
1625
1626GLint State::getPathStencilRef() const
1627{
1628 return mPathStencilRef;
1629}
1630
1631GLuint State::getPathStencilMask() const
1632{
1633 return mPathStencilMask;
1634}
1635
Geoff Lang1d2c41d2016-10-19 16:14:46 -07001636void State::setFramebufferSRGB(bool sRGB)
1637{
1638 mFramebufferSRGB = sRGB;
1639 mDirtyBits.set(DIRTY_BIT_FRAMEBUFFER_SRGB);
1640}
1641
1642bool State::getFramebufferSRGB() const
1643{
1644 return mFramebufferSRGB;
1645}
1646
Shannon Woods53a94a82014-06-24 15:20:36 -04001647void State::getBooleanv(GLenum pname, GLboolean *params)
1648{
1649 switch (pname)
1650 {
1651 case GL_SAMPLE_COVERAGE_INVERT: *params = mSampleCoverageInvert; break;
1652 case GL_DEPTH_WRITEMASK: *params = mDepthStencil.depthMask; break;
1653 case GL_COLOR_WRITEMASK:
1654 params[0] = mBlend.colorMaskRed;
1655 params[1] = mBlend.colorMaskGreen;
1656 params[2] = mBlend.colorMaskBlue;
1657 params[3] = mBlend.colorMaskAlpha;
1658 break;
Corentin Wallez2e568cf2017-09-18 17:05:22 -04001659 case GL_CULL_FACE:
1660 *params = mRasterizer.cullFace;
1661 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001662 case GL_POLYGON_OFFSET_FILL: *params = mRasterizer.polygonOffsetFill; break;
1663 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mBlend.sampleAlphaToCoverage; break;
1664 case GL_SAMPLE_COVERAGE: *params = mSampleCoverage; break;
Jiawei Shaodb342272017-09-27 10:21:45 +08001665 case GL_SAMPLE_MASK:
1666 *params = mSampleMask;
1667 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001668 case GL_SCISSOR_TEST: *params = mScissorTest; break;
1669 case GL_STENCIL_TEST: *params = mDepthStencil.stencilTest; break;
1670 case GL_DEPTH_TEST: *params = mDepthStencil.depthTest; break;
1671 case GL_BLEND: *params = mBlend.blend; break;
1672 case GL_DITHER: *params = mBlend.dither; break;
Geoff Langbb0a0bb2015-03-27 12:16:57 -04001673 case GL_TRANSFORM_FEEDBACK_ACTIVE: *params = getCurrentTransformFeedback()->isActive() ? GL_TRUE : GL_FALSE; break;
1674 case GL_TRANSFORM_FEEDBACK_PAUSED: *params = getCurrentTransformFeedback()->isPaused() ? GL_TRUE : GL_FALSE; break;
Jamie Madille2cd53d2015-10-27 11:15:46 -04001675 case GL_PRIMITIVE_RESTART_FIXED_INDEX:
1676 *params = mPrimitiveRestart;
1677 break;
Geoff Langab831f02015-12-01 09:39:10 -05001678 case GL_RASTERIZER_DISCARD:
1679 *params = isRasterizerDiscardEnabled() ? GL_TRUE : GL_FALSE;
1680 break;
Geoff Lang70d0f492015-12-10 17:45:46 -05001681 case GL_DEBUG_OUTPUT_SYNCHRONOUS:
1682 *params = mDebug.isOutputSynchronous() ? GL_TRUE : GL_FALSE;
1683 break;
1684 case GL_DEBUG_OUTPUT:
1685 *params = mDebug.isOutputEnabled() ? GL_TRUE : GL_FALSE;
1686 break;
Sami Väisänen74c23472016-05-09 17:30:30 +03001687 case GL_MULTISAMPLE_EXT:
1688 *params = mMultiSampling;
1689 break;
1690 case GL_SAMPLE_ALPHA_TO_ONE_EXT:
1691 *params = mSampleAlphaToOne;
1692 break;
Geoff Langf41a7152016-09-19 15:11:17 -04001693 case GL_BIND_GENERATES_RESOURCE_CHROMIUM:
1694 *params = isBindGeneratesResourceEnabled() ? GL_TRUE : GL_FALSE;
1695 break;
Geoff Langfeb8c682017-02-13 16:07:35 -05001696 case GL_CLIENT_ARRAYS_ANGLE:
1697 *params = areClientArraysEnabled() ? GL_TRUE : GL_FALSE;
1698 break;
Geoff Lang1d2c41d2016-10-19 16:14:46 -07001699 case GL_FRAMEBUFFER_SRGB_EXT:
1700 *params = getFramebufferSRGB() ? GL_TRUE : GL_FALSE;
1701 break;
Geoff Langb433e872017-10-05 14:01:47 -04001702 case GL_ROBUST_RESOURCE_INITIALIZATION_ANGLE:
Jamie Madille08a1d32017-03-07 17:24:06 -05001703 *params = mRobustResourceInit ? GL_TRUE : GL_FALSE;
1704 break;
Jamie Madillc43be722017-07-13 16:22:14 -04001705 case GL_PROGRAM_CACHE_ENABLED_ANGLE:
1706 *params = mProgramBinaryCacheEnabled ? GL_TRUE : GL_FALSE;
1707 break;
1708
Shannon Woods53a94a82014-06-24 15:20:36 -04001709 default:
1710 UNREACHABLE();
1711 break;
1712 }
1713}
1714
1715void State::getFloatv(GLenum pname, GLfloat *params)
1716{
1717 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1718 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1719 // GetIntegerv as its native query function. As it would require conversion in any
1720 // case, this should make no difference to the calling application.
1721 switch (pname)
1722 {
1723 case GL_LINE_WIDTH: *params = mLineWidth; break;
1724 case GL_SAMPLE_COVERAGE_VALUE: *params = mSampleCoverageValue; break;
1725 case GL_DEPTH_CLEAR_VALUE: *params = mDepthClearValue; break;
1726 case GL_POLYGON_OFFSET_FACTOR: *params = mRasterizer.polygonOffsetFactor; break;
1727 case GL_POLYGON_OFFSET_UNITS: *params = mRasterizer.polygonOffsetUnits; break;
1728 case GL_DEPTH_RANGE:
1729 params[0] = mNearZ;
1730 params[1] = mFarZ;
1731 break;
1732 case GL_COLOR_CLEAR_VALUE:
1733 params[0] = mColorClearValue.red;
1734 params[1] = mColorClearValue.green;
1735 params[2] = mColorClearValue.blue;
1736 params[3] = mColorClearValue.alpha;
1737 break;
1738 case GL_BLEND_COLOR:
1739 params[0] = mBlendColor.red;
1740 params[1] = mBlendColor.green;
1741 params[2] = mBlendColor.blue;
1742 params[3] = mBlendColor.alpha;
1743 break;
Sami Väisänen74c23472016-05-09 17:30:30 +03001744 case GL_MULTISAMPLE_EXT:
1745 *params = static_cast<GLfloat>(mMultiSampling);
1746 break;
1747 case GL_SAMPLE_ALPHA_TO_ONE_EXT:
1748 *params = static_cast<GLfloat>(mSampleAlphaToOne);
Sami Väisänena797e062016-05-12 15:23:40 +03001749 case GL_COVERAGE_MODULATION_CHROMIUM:
Jamie Madille2e406c2016-06-02 13:04:10 -04001750 params[0] = static_cast<GLfloat>(mCoverageModulation);
1751 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001752 default:
1753 UNREACHABLE();
1754 break;
1755 }
1756}
1757
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001758void State::getIntegerv(const Context *context, GLenum pname, GLint *params)
Shannon Woods53a94a82014-06-24 15:20:36 -04001759{
1760 if (pname >= GL_DRAW_BUFFER0_EXT && pname <= GL_DRAW_BUFFER15_EXT)
1761 {
1762 unsigned int colorAttachment = (pname - GL_DRAW_BUFFER0_EXT);
Shannon Woods2df6a602014-09-26 16:12:07 -04001763 ASSERT(colorAttachment < mMaxDrawBuffers);
Shannon Woods53a94a82014-06-24 15:20:36 -04001764 Framebuffer *framebuffer = mDrawFramebuffer;
1765 *params = framebuffer->getDrawBufferState(colorAttachment);
1766 return;
1767 }
1768
1769 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1770 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1771 // GetIntegerv as its native query function. As it would require conversion in any
1772 // case, this should make no difference to the calling application. You may find it in
1773 // State::getFloatv.
1774 switch (pname)
1775 {
Corentin Wallez336129f2017-10-17 15:55:40 -04001776 case GL_ARRAY_BUFFER_BINDING:
1777 *params = mBoundBuffers[BufferBinding::Array].id();
1778 break;
1779 case GL_DRAW_INDIRECT_BUFFER_BINDING:
1780 *params = mBoundBuffers[BufferBinding::DrawIndirect].id();
1781 break;
1782 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1783 *params = getVertexArray()->getElementArrayBuffer().id();
1784 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001785 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1786 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mDrawFramebuffer->id(); break;
1787 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mReadFramebuffer->id(); break;
1788 case GL_RENDERBUFFER_BINDING: *params = mRenderbuffer.id(); break;
1789 case GL_VERTEX_ARRAY_BINDING: *params = mVertexArray->id(); break;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001790 case GL_CURRENT_PROGRAM: *params = mProgram ? mProgram->id() : 0; break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001791 case GL_PACK_ALIGNMENT: *params = mPack.alignment; break;
1792 case GL_PACK_REVERSE_ROW_ORDER_ANGLE: *params = mPack.reverseRowOrder; break;
Minmin Gongadff67b2015-10-14 10:34:45 -04001793 case GL_PACK_ROW_LENGTH:
1794 *params = mPack.rowLength;
1795 break;
1796 case GL_PACK_SKIP_ROWS:
1797 *params = mPack.skipRows;
1798 break;
1799 case GL_PACK_SKIP_PIXELS:
1800 *params = mPack.skipPixels;
1801 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001802 case GL_UNPACK_ALIGNMENT: *params = mUnpack.alignment; break;
Minmin Gongb8aee3b2015-01-27 14:42:36 -08001803 case GL_UNPACK_ROW_LENGTH: *params = mUnpack.rowLength; break;
Minmin Gongadff67b2015-10-14 10:34:45 -04001804 case GL_UNPACK_IMAGE_HEIGHT:
1805 *params = mUnpack.imageHeight;
1806 break;
1807 case GL_UNPACK_SKIP_IMAGES:
1808 *params = mUnpack.skipImages;
1809 break;
1810 case GL_UNPACK_SKIP_ROWS:
1811 *params = mUnpack.skipRows;
1812 break;
1813 case GL_UNPACK_SKIP_PIXELS:
1814 *params = mUnpack.skipPixels;
1815 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001816 case GL_GENERATE_MIPMAP_HINT: *params = mGenerateMipmapHint; break;
1817 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mFragmentShaderDerivativeHint; break;
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001818 case GL_ACTIVE_TEXTURE:
1819 *params = (static_cast<GLint>(mActiveSampler) + GL_TEXTURE0);
1820 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001821 case GL_STENCIL_FUNC: *params = mDepthStencil.stencilFunc; break;
1822 case GL_STENCIL_REF: *params = mStencilRef; break;
jchen10a99ed552017-09-22 08:10:32 +08001823 case GL_STENCIL_VALUE_MASK:
1824 *params = CastMaskValue(context, mDepthStencil.stencilMask);
1825 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001826 case GL_STENCIL_BACK_FUNC: *params = mDepthStencil.stencilBackFunc; break;
1827 case GL_STENCIL_BACK_REF: *params = mStencilBackRef; break;
jchen10a99ed552017-09-22 08:10:32 +08001828 case GL_STENCIL_BACK_VALUE_MASK:
1829 *params = CastMaskValue(context, mDepthStencil.stencilBackMask);
1830 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001831 case GL_STENCIL_FAIL: *params = mDepthStencil.stencilFail; break;
1832 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mDepthStencil.stencilPassDepthFail; break;
1833 case GL_STENCIL_PASS_DEPTH_PASS: *params = mDepthStencil.stencilPassDepthPass; break;
1834 case GL_STENCIL_BACK_FAIL: *params = mDepthStencil.stencilBackFail; break;
1835 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mDepthStencil.stencilBackPassDepthFail; break;
1836 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mDepthStencil.stencilBackPassDepthPass; break;
1837 case GL_DEPTH_FUNC: *params = mDepthStencil.depthFunc; break;
1838 case GL_BLEND_SRC_RGB: *params = mBlend.sourceBlendRGB; break;
1839 case GL_BLEND_SRC_ALPHA: *params = mBlend.sourceBlendAlpha; break;
1840 case GL_BLEND_DST_RGB: *params = mBlend.destBlendRGB; break;
1841 case GL_BLEND_DST_ALPHA: *params = mBlend.destBlendAlpha; break;
1842 case GL_BLEND_EQUATION_RGB: *params = mBlend.blendEquationRGB; break;
1843 case GL_BLEND_EQUATION_ALPHA: *params = mBlend.blendEquationAlpha; break;
jchen10a99ed552017-09-22 08:10:32 +08001844 case GL_STENCIL_WRITEMASK:
1845 *params = CastMaskValue(context, mDepthStencil.stencilWritemask);
1846 break;
1847 case GL_STENCIL_BACK_WRITEMASK:
1848 *params = CastMaskValue(context, mDepthStencil.stencilBackWritemask);
1849 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001850 case GL_STENCIL_CLEAR_VALUE: *params = mStencilClearValue; break;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001851 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1852 *params = mReadFramebuffer->getImplementationColorReadType(context);
1853 break;
1854 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1855 *params = mReadFramebuffer->getImplementationColorReadFormat(context);
1856 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001857 case GL_SAMPLE_BUFFERS:
1858 case GL_SAMPLES:
1859 {
Jamie Madill81c2e252017-09-09 23:32:46 -04001860 Framebuffer *framebuffer = mDrawFramebuffer;
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001861 if (framebuffer->checkStatus(context) == GL_FRAMEBUFFER_COMPLETE)
Shannon Woods53a94a82014-06-24 15:20:36 -04001862 {
1863 switch (pname)
1864 {
Jamie Madilla0016b72017-07-14 14:30:46 -04001865 case GL_SAMPLE_BUFFERS:
1866 if (framebuffer->getSamples(context) != 0)
1867 {
1868 *params = 1;
1869 }
1870 else
1871 {
1872 *params = 0;
1873 }
1874 break;
1875 case GL_SAMPLES:
1876 *params = framebuffer->getSamples(context);
1877 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001878 }
1879 }
1880 else
1881 {
1882 *params = 0;
1883 }
1884 }
1885 break;
1886 case GL_VIEWPORT:
1887 params[0] = mViewport.x;
1888 params[1] = mViewport.y;
1889 params[2] = mViewport.width;
1890 params[3] = mViewport.height;
1891 break;
1892 case GL_SCISSOR_BOX:
1893 params[0] = mScissor.x;
1894 params[1] = mScissor.y;
1895 params[2] = mScissor.width;
1896 params[3] = mScissor.height;
1897 break;
Corentin Wallez2e568cf2017-09-18 17:05:22 -04001898 case GL_CULL_FACE_MODE:
1899 *params = ToGLenum(mRasterizer.cullMode);
1900 break;
1901 case GL_FRONT_FACE:
1902 *params = mRasterizer.frontFace;
1903 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001904 case GL_RED_BITS:
1905 case GL_GREEN_BITS:
1906 case GL_BLUE_BITS:
1907 case GL_ALPHA_BITS:
1908 {
Jamie Madill81c2e252017-09-09 23:32:46 -04001909 Framebuffer *framebuffer = getDrawFramebuffer();
1910 const FramebufferAttachment *colorbuffer = framebuffer->getFirstColorbuffer();
Shannon Woods53a94a82014-06-24 15:20:36 -04001911
1912 if (colorbuffer)
1913 {
1914 switch (pname)
1915 {
1916 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1917 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1918 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1919 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1920 }
1921 }
1922 else
1923 {
1924 *params = 0;
1925 }
1926 }
1927 break;
1928 case GL_DEPTH_BITS:
1929 {
Jamie Madill81c2e252017-09-09 23:32:46 -04001930 const Framebuffer *framebuffer = getDrawFramebuffer();
1931 const FramebufferAttachment *depthbuffer = framebuffer->getDepthbuffer();
Shannon Woods53a94a82014-06-24 15:20:36 -04001932
1933 if (depthbuffer)
1934 {
1935 *params = depthbuffer->getDepthSize();
1936 }
1937 else
1938 {
1939 *params = 0;
1940 }
1941 }
1942 break;
1943 case GL_STENCIL_BITS:
1944 {
Jamie Madill81c2e252017-09-09 23:32:46 -04001945 const Framebuffer *framebuffer = getDrawFramebuffer();
1946 const FramebufferAttachment *stencilbuffer = framebuffer->getStencilbuffer();
Shannon Woods53a94a82014-06-24 15:20:36 -04001947
1948 if (stencilbuffer)
1949 {
1950 *params = stencilbuffer->getStencilSize();
1951 }
1952 else
1953 {
1954 *params = 0;
1955 }
1956 }
1957 break;
1958 case GL_TEXTURE_BINDING_2D:
Shannon Woods2df6a602014-09-26 16:12:07 -04001959 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001960 *params = getSamplerTextureId(static_cast<unsigned int>(mActiveSampler), GL_TEXTURE_2D);
Shannon Woods53a94a82014-06-24 15:20:36 -04001961 break;
Corentin Wallez13c0dd42017-07-04 18:27:01 -04001962 case GL_TEXTURE_BINDING_RECTANGLE_ANGLE:
1963 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
1964 *params = getSamplerTextureId(static_cast<unsigned int>(mActiveSampler),
1965 GL_TEXTURE_RECTANGLE_ANGLE);
1966 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001967 case GL_TEXTURE_BINDING_CUBE_MAP:
Shannon Woods2df6a602014-09-26 16:12:07 -04001968 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001969 *params =
1970 getSamplerTextureId(static_cast<unsigned int>(mActiveSampler), GL_TEXTURE_CUBE_MAP);
Shannon Woods53a94a82014-06-24 15:20:36 -04001971 break;
1972 case GL_TEXTURE_BINDING_3D:
Shannon Woods2df6a602014-09-26 16:12:07 -04001973 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001974 *params = getSamplerTextureId(static_cast<unsigned int>(mActiveSampler), GL_TEXTURE_3D);
Shannon Woods53a94a82014-06-24 15:20:36 -04001975 break;
1976 case GL_TEXTURE_BINDING_2D_ARRAY:
Shannon Woods2df6a602014-09-26 16:12:07 -04001977 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001978 *params =
1979 getSamplerTextureId(static_cast<unsigned int>(mActiveSampler), GL_TEXTURE_2D_ARRAY);
Shannon Woods53a94a82014-06-24 15:20:36 -04001980 break;
JiangYizhou24fe74c2017-07-06 16:56:50 +08001981 case GL_TEXTURE_BINDING_2D_MULTISAMPLE:
1982 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
1983 *params = getSamplerTextureId(static_cast<unsigned int>(mActiveSampler),
1984 GL_TEXTURE_2D_MULTISAMPLE);
1985 break;
John Bauman18319182016-09-28 14:22:27 -07001986 case GL_TEXTURE_BINDING_EXTERNAL_OES:
1987 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
1988 *params = getSamplerTextureId(static_cast<unsigned int>(mActiveSampler),
1989 GL_TEXTURE_EXTERNAL_OES);
1990 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001991 case GL_UNIFORM_BUFFER_BINDING:
Corentin Wallez336129f2017-10-17 15:55:40 -04001992 *params = mBoundBuffers[BufferBinding::Uniform].id();
1993 break;
Frank Henigman22581ff2015-11-06 14:25:54 -05001994 case GL_TRANSFORM_FEEDBACK_BINDING:
Frank Henigmanb0f0b812015-11-21 17:49:29 -05001995 *params = mTransformFeedback.id();
Frank Henigman22581ff2015-11-06 14:25:54 -05001996 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001997 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING:
Corentin Wallez336129f2017-10-17 15:55:40 -04001998 ASSERT(mTransformFeedback.get() != nullptr);
1999 *params = mTransformFeedback->getGenericBuffer().id();
2000 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002001 case GL_COPY_READ_BUFFER_BINDING:
Corentin Wallez336129f2017-10-17 15:55:40 -04002002 *params = mBoundBuffers[BufferBinding::CopyRead].id();
2003 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002004 case GL_COPY_WRITE_BUFFER_BINDING:
Corentin Wallez336129f2017-10-17 15:55:40 -04002005 *params = mBoundBuffers[BufferBinding::CopyWrite].id();
2006 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002007 case GL_PIXEL_PACK_BUFFER_BINDING:
Corentin Wallez336129f2017-10-17 15:55:40 -04002008 *params = mBoundBuffers[BufferBinding::PixelPack].id();
Corentin Wallezcda6af12017-10-30 19:20:37 -04002009 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002010 case GL_PIXEL_UNPACK_BUFFER_BINDING:
Corentin Wallez336129f2017-10-17 15:55:40 -04002011 *params = mBoundBuffers[BufferBinding::PixelUnpack].id();
Corentin Wallezcda6af12017-10-30 19:20:37 -04002012 break;
Olli Etuaho86821db2016-03-04 12:05:47 +02002013 case GL_READ_BUFFER:
2014 *params = mReadFramebuffer->getReadBufferState();
2015 break;
2016 case GL_SAMPLER_BINDING:
2017 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
2018 *params = getSamplerId(static_cast<GLuint>(mActiveSampler));
2019 break;
Geoff Lang70d0f492015-12-10 17:45:46 -05002020 case GL_DEBUG_LOGGED_MESSAGES:
2021 *params = static_cast<GLint>(mDebug.getMessageCount());
2022 break;
2023 case GL_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH:
2024 *params = static_cast<GLint>(mDebug.getNextMessageLength());
2025 break;
2026 case GL_DEBUG_GROUP_STACK_DEPTH:
2027 *params = static_cast<GLint>(mDebug.getGroupStackDepth());
2028 break;
Sami Väisänen74c23472016-05-09 17:30:30 +03002029 case GL_MULTISAMPLE_EXT:
2030 *params = static_cast<GLint>(mMultiSampling);
2031 break;
2032 case GL_SAMPLE_ALPHA_TO_ONE_EXT:
2033 *params = static_cast<GLint>(mSampleAlphaToOne);
Sami Väisänena797e062016-05-12 15:23:40 +03002034 case GL_COVERAGE_MODULATION_CHROMIUM:
2035 *params = static_cast<GLint>(mCoverageModulation);
Sami Väisänen74c23472016-05-09 17:30:30 +03002036 break;
Jiajia Qin6eafb042016-12-27 17:04:07 +08002037 case GL_ATOMIC_COUNTER_BUFFER_BINDING:
Corentin Wallez336129f2017-10-17 15:55:40 -04002038 *params = mBoundBuffers[BufferBinding::AtomicCounter].id();
Jiajia Qin6eafb042016-12-27 17:04:07 +08002039 break;
Jiajia Qinf546e7d2017-03-27 14:12:59 +08002040 case GL_SHADER_STORAGE_BUFFER_BINDING:
Corentin Wallez336129f2017-10-17 15:55:40 -04002041 *params = mBoundBuffers[BufferBinding::ShaderStorage].id();
Jiajia Qinf546e7d2017-03-27 14:12:59 +08002042 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002043 default:
2044 UNREACHABLE();
2045 break;
2046 }
2047}
2048
Geoff Lang70d0f492015-12-10 17:45:46 -05002049void State::getPointerv(GLenum pname, void **params) const
2050{
2051 switch (pname)
2052 {
2053 case GL_DEBUG_CALLBACK_FUNCTION:
2054 *params = reinterpret_cast<void *>(mDebug.getCallback());
2055 break;
2056 case GL_DEBUG_CALLBACK_USER_PARAM:
2057 *params = const_cast<void *>(mDebug.getUserParam());
2058 break;
2059 default:
2060 UNREACHABLE();
2061 break;
2062 }
2063}
2064
Martin Radev66fb8202016-07-28 11:45:20 +03002065void State::getIntegeri_v(GLenum target, GLuint index, GLint *data)
Shannon Woods53a94a82014-06-24 15:20:36 -04002066{
2067 switch (target)
2068 {
2069 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002070 ASSERT(static_cast<size_t>(index) < mTransformFeedback->getIndexedBufferCount());
2071 *data = mTransformFeedback->getIndexedBuffer(index).id();
2072 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002073 case GL_UNIFORM_BUFFER_BINDING:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002074 ASSERT(static_cast<size_t>(index) < mUniformBuffers.size());
2075 *data = mUniformBuffers[index].id();
2076 break;
2077 case GL_ATOMIC_COUNTER_BUFFER_BINDING:
2078 ASSERT(static_cast<size_t>(index) < mAtomicCounterBuffers.size());
2079 *data = mAtomicCounterBuffers[index].id();
2080 break;
Jiajia Qinf546e7d2017-03-27 14:12:59 +08002081 case GL_SHADER_STORAGE_BUFFER_BINDING:
2082 ASSERT(static_cast<size_t>(index) < mShaderStorageBuffers.size());
2083 *data = mShaderStorageBuffers[index].id();
2084 break;
Shao80957d92017-02-20 21:25:59 +08002085 case GL_VERTEX_BINDING_BUFFER:
2086 ASSERT(static_cast<size_t>(index) < mVertexArray->getMaxBindings());
Martin Radevdd5f27e2017-06-07 10:17:09 +03002087 *data = mVertexArray->getVertexBinding(index).getBuffer().id();
Shao80957d92017-02-20 21:25:59 +08002088 break;
2089 case GL_VERTEX_BINDING_DIVISOR:
2090 ASSERT(static_cast<size_t>(index) < mVertexArray->getMaxBindings());
Martin Radevdd5f27e2017-06-07 10:17:09 +03002091 *data = mVertexArray->getVertexBinding(index).getDivisor();
Shao80957d92017-02-20 21:25:59 +08002092 break;
2093 case GL_VERTEX_BINDING_OFFSET:
2094 ASSERT(static_cast<size_t>(index) < mVertexArray->getMaxBindings());
Martin Radevdd5f27e2017-06-07 10:17:09 +03002095 *data = static_cast<GLuint>(mVertexArray->getVertexBinding(index).getOffset());
Shao80957d92017-02-20 21:25:59 +08002096 break;
2097 case GL_VERTEX_BINDING_STRIDE:
2098 ASSERT(static_cast<size_t>(index) < mVertexArray->getMaxBindings());
Martin Radevdd5f27e2017-06-07 10:17:09 +03002099 *data = mVertexArray->getVertexBinding(index).getStride();
Shao80957d92017-02-20 21:25:59 +08002100 break;
Jiawei Shaodb342272017-09-27 10:21:45 +08002101 case GL_SAMPLE_MASK_VALUE:
2102 ASSERT(static_cast<size_t>(index) < mSampleMaskValues.size());
2103 *data = mSampleMaskValues[index];
2104 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002105 default:
Martin Radev66fb8202016-07-28 11:45:20 +03002106 UNREACHABLE();
2107 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002108 }
Shannon Woods53a94a82014-06-24 15:20:36 -04002109}
2110
Martin Radev66fb8202016-07-28 11:45:20 +03002111void State::getInteger64i_v(GLenum target, GLuint index, GLint64 *data)
Shannon Woods53a94a82014-06-24 15:20:36 -04002112{
2113 switch (target)
2114 {
2115 case GL_TRANSFORM_FEEDBACK_BUFFER_START:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002116 ASSERT(static_cast<size_t>(index) < mTransformFeedback->getIndexedBufferCount());
2117 *data = mTransformFeedback->getIndexedBuffer(index).getOffset();
2118 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002119 case GL_TRANSFORM_FEEDBACK_BUFFER_SIZE:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002120 ASSERT(static_cast<size_t>(index) < mTransformFeedback->getIndexedBufferCount());
2121 *data = mTransformFeedback->getIndexedBuffer(index).getSize();
2122 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002123 case GL_UNIFORM_BUFFER_START:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002124 ASSERT(static_cast<size_t>(index) < mUniformBuffers.size());
2125 *data = mUniformBuffers[index].getOffset();
2126 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002127 case GL_UNIFORM_BUFFER_SIZE:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002128 ASSERT(static_cast<size_t>(index) < mUniformBuffers.size());
2129 *data = mUniformBuffers[index].getSize();
2130 break;
2131 case GL_ATOMIC_COUNTER_BUFFER_START:
2132 ASSERT(static_cast<size_t>(index) < mAtomicCounterBuffers.size());
2133 *data = mAtomicCounterBuffers[index].getOffset();
2134 break;
2135 case GL_ATOMIC_COUNTER_BUFFER_SIZE:
2136 ASSERT(static_cast<size_t>(index) < mAtomicCounterBuffers.size());
2137 *data = mAtomicCounterBuffers[index].getSize();
2138 break;
Jiajia Qinf546e7d2017-03-27 14:12:59 +08002139 case GL_SHADER_STORAGE_BUFFER_START:
2140 ASSERT(static_cast<size_t>(index) < mShaderStorageBuffers.size());
2141 *data = mShaderStorageBuffers[index].getOffset();
2142 break;
2143 case GL_SHADER_STORAGE_BUFFER_SIZE:
2144 ASSERT(static_cast<size_t>(index) < mShaderStorageBuffers.size());
2145 *data = mShaderStorageBuffers[index].getSize();
2146 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002147 default:
Martin Radev66fb8202016-07-28 11:45:20 +03002148 UNREACHABLE();
2149 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002150 }
Martin Radev66fb8202016-07-28 11:45:20 +03002151}
Shannon Woods53a94a82014-06-24 15:20:36 -04002152
Martin Radev66fb8202016-07-28 11:45:20 +03002153void State::getBooleani_v(GLenum target, GLuint index, GLboolean *data)
2154{
2155 UNREACHABLE();
Shannon Woods53a94a82014-06-24 15:20:36 -04002156}
2157
Corentin Wallez336129f2017-10-17 15:55:40 -04002158bool State::hasMappedBuffer(BufferBinding target) const
Jamie Madilld9ba4f72014-08-04 10:47:59 -04002159{
Corentin Wallez336129f2017-10-17 15:55:40 -04002160 if (target == BufferBinding::Array)
Jamie Madilld9ba4f72014-08-04 10:47:59 -04002161 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +08002162 const VertexArray *vao = getVertexArray();
Jamie Madilleea3a6e2015-04-15 10:02:48 -04002163 const auto &vertexAttribs = vao->getVertexAttributes();
Jiawei-Shao2597fb62016-12-09 16:38:02 +08002164 const auto &vertexBindings = vao->getVertexBindings();
Jamie Madill8e344942015-07-09 14:22:07 -04002165 size_t maxEnabledAttrib = vao->getMaxEnabledAttribute();
Jamie Madillaebf9dd2015-04-28 12:39:07 -04002166 for (size_t attribIndex = 0; attribIndex < maxEnabledAttrib; attribIndex++)
Jamie Madilld9ba4f72014-08-04 10:47:59 -04002167 {
Jamie Madill81c2e252017-09-09 23:32:46 -04002168 const VertexAttribute &vertexAttrib = vertexAttribs[attribIndex];
Martin Radevdd5f27e2017-06-07 10:17:09 +03002169 auto *boundBuffer = vertexBindings[vertexAttrib.bindingIndex].getBuffer().get();
Jamie Madilld9ba4f72014-08-04 10:47:59 -04002170 if (vertexAttrib.enabled && boundBuffer && boundBuffer->isMapped())
2171 {
2172 return true;
2173 }
2174 }
2175
2176 return false;
2177 }
2178 else
2179 {
2180 Buffer *buffer = getTargetBuffer(target);
2181 return (buffer && buffer->isMapped());
2182 }
2183}
2184
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002185void State::syncDirtyObjects(const Context *context)
Jamie Madillc9d442d2016-01-20 11:17:24 -05002186{
2187 if (!mDirtyObjects.any())
2188 return;
2189
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002190 syncDirtyObjects(context, mDirtyObjects);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002191}
2192
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002193void State::syncDirtyObjects(const Context *context, const DirtyObjects &bitset)
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002194{
Jamie Madill6de51852017-04-12 09:53:01 -04002195 for (auto dirtyObject : bitset)
Jamie Madillc9d442d2016-01-20 11:17:24 -05002196 {
2197 switch (dirtyObject)
2198 {
2199 case DIRTY_OBJECT_READ_FRAMEBUFFER:
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002200 ASSERT(mReadFramebuffer);
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002201 mReadFramebuffer->syncState(context);
Jamie Madillc9d442d2016-01-20 11:17:24 -05002202 break;
2203 case DIRTY_OBJECT_DRAW_FRAMEBUFFER:
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002204 ASSERT(mDrawFramebuffer);
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002205 mDrawFramebuffer->syncState(context);
Jamie Madillc9d442d2016-01-20 11:17:24 -05002206 break;
2207 case DIRTY_OBJECT_VERTEX_ARRAY:
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002208 ASSERT(mVertexArray);
Jamie Madill06ef36b2017-09-09 23:32:46 -04002209 mVertexArray->syncState(context);
Jamie Madillc9d442d2016-01-20 11:17:24 -05002210 break;
Jamie Madill81c2e252017-09-09 23:32:46 -04002211 case DIRTY_OBJECT_PROGRAM_TEXTURES:
2212 syncProgramTextures(context);
2213 break;
2214
Jamie Madillc9d442d2016-01-20 11:17:24 -05002215 default:
2216 UNREACHABLE();
2217 break;
2218 }
2219 }
2220
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002221 mDirtyObjects &= ~bitset;
2222}
2223
Jamie Madill81c2e252017-09-09 23:32:46 -04002224void State::syncProgramTextures(const Context *context)
2225{
Jamie Madill81c2e252017-09-09 23:32:46 -04002226 // TODO(jmadill): Fine-grained updates.
2227 if (!mProgram)
2228 {
2229 return;
2230 }
2231
2232 ASSERT(mDirtyObjects[DIRTY_OBJECT_PROGRAM_TEXTURES]);
2233 mDirtyBits.set(DIRTY_BIT_TEXTURE_BINDINGS);
2234
Jamie Madill0f80ed82017-09-19 00:24:56 -04002235 ActiveTextureMask newActiveTextures;
2236
Jamie Madill81c2e252017-09-09 23:32:46 -04002237 for (const SamplerBinding &samplerBinding : mProgram->getSamplerBindings())
2238 {
2239 if (samplerBinding.unreferenced)
2240 continue;
2241
2242 GLenum textureType = samplerBinding.textureType;
2243 for (GLuint textureUnitIndex : samplerBinding.boundTextureUnits)
2244 {
2245 Texture *texture = getSamplerTexture(textureUnitIndex, textureType);
Jamie Madill06ef36b2017-09-09 23:32:46 -04002246 Sampler *sampler = getSampler(textureUnitIndex);
Jamie Madill0f80ed82017-09-19 00:24:56 -04002247 ASSERT(static_cast<size_t>(textureUnitIndex) < mCompleteTextureCache.size());
2248 ASSERT(static_cast<size_t>(textureUnitIndex) < newActiveTextures.size());
Jamie Madill81c2e252017-09-09 23:32:46 -04002249
Jamie Madill42975642017-10-12 12:31:51 -04002250 ASSERT(texture);
Jamie Madill81c2e252017-09-09 23:32:46 -04002251
Jamie Madill42975642017-10-12 12:31:51 -04002252 // Mark the texture binding bit as dirty if the texture completeness changes.
2253 // TODO(jmadill): Use specific dirty bit for completeness change.
2254 if (texture->isSamplerComplete(context, sampler) &&
2255 !mDrawFramebuffer->hasTextureAttachment(texture))
2256 {
2257 texture->syncState();
2258 mCompleteTextureCache[textureUnitIndex] = texture;
Jamie Madill81c2e252017-09-09 23:32:46 -04002259 }
Jamie Madill42975642017-10-12 12:31:51 -04002260 else
2261 {
2262 mCompleteTextureCache[textureUnitIndex] = nullptr;
2263 }
2264
2265 // Bind the texture unconditionally, to recieve completeness change notifications.
2266 mCompleteTextureBindings[textureUnitIndex].bind(texture->getDirtyChannel());
Jamie Madilla59fc192017-11-02 12:57:58 -04002267 mActiveTexturesMask.set(textureUnitIndex);
Jamie Madill42975642017-10-12 12:31:51 -04002268 newActiveTextures.set(textureUnitIndex);
Jamie Madill81c2e252017-09-09 23:32:46 -04002269
Jamie Madill06ef36b2017-09-09 23:32:46 -04002270 if (sampler != nullptr)
2271 {
2272 sampler->syncState(context);
2273 }
Jamie Madill81c2e252017-09-09 23:32:46 -04002274 }
2275 }
Jamie Madill0f80ed82017-09-19 00:24:56 -04002276
2277 // Unset now missing textures.
Jamie Madilla59fc192017-11-02 12:57:58 -04002278 ActiveTextureMask negativeMask = mActiveTexturesMask & ~newActiveTextures;
Jamie Madill0f80ed82017-09-19 00:24:56 -04002279 if (negativeMask.any())
2280 {
2281 for (auto textureIndex : negativeMask)
2282 {
2283 mCompleteTextureBindings[textureIndex].reset();
2284 mCompleteTextureCache[textureIndex] = nullptr;
Jamie Madilla59fc192017-11-02 12:57:58 -04002285 mActiveTexturesMask.reset(textureIndex);
Jamie Madill0f80ed82017-09-19 00:24:56 -04002286 }
2287 }
Jamie Madill81c2e252017-09-09 23:32:46 -04002288}
2289
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002290void State::syncDirtyObject(const Context *context, GLenum target)
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002291{
2292 DirtyObjects localSet;
2293
2294 switch (target)
2295 {
2296 case GL_READ_FRAMEBUFFER:
2297 localSet.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
2298 break;
2299 case GL_DRAW_FRAMEBUFFER:
2300 localSet.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
2301 break;
2302 case GL_FRAMEBUFFER:
2303 localSet.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
2304 localSet.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
2305 break;
2306 case GL_VERTEX_ARRAY:
2307 localSet.set(DIRTY_OBJECT_VERTEX_ARRAY);
2308 break;
Jamie Madill81c2e252017-09-09 23:32:46 -04002309 case GL_TEXTURE:
2310 case GL_SAMPLER:
2311 case GL_PROGRAM:
2312 localSet.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
2313 break;
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002314 }
2315
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002316 syncDirtyObjects(context, localSet);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002317}
2318
2319void State::setObjectDirty(GLenum target)
2320{
2321 switch (target)
2322 {
2323 case GL_READ_FRAMEBUFFER:
2324 mDirtyObjects.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
2325 break;
2326 case GL_DRAW_FRAMEBUFFER:
2327 mDirtyObjects.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
2328 break;
2329 case GL_FRAMEBUFFER:
2330 mDirtyObjects.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
2331 mDirtyObjects.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
2332 break;
2333 case GL_VERTEX_ARRAY:
2334 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
2335 break;
Jamie Madill81c2e252017-09-09 23:32:46 -04002336 case GL_TEXTURE:
2337 case GL_SAMPLER:
Jamie Madill81c2e252017-09-09 23:32:46 -04002338 case GL_PROGRAM:
2339 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
2340 mDirtyBits.set(DIRTY_BIT_TEXTURE_BINDINGS);
2341 break;
Jamie Madilla779b612017-07-24 11:46:05 -04002342 }
2343}
2344
2345void State::onProgramExecutableChange(Program *program)
2346{
2347 // OpenGL Spec:
2348 // "If LinkProgram or ProgramBinary successfully re-links a program object
2349 // that was already in use as a result of a previous call to UseProgram, then the
2350 // generated executable code will be installed as part of the current rendering state."
2351 if (program->isLinked() && mProgram == program)
2352 {
2353 mDirtyBits.set(DIRTY_BIT_PROGRAM_EXECUTABLE);
Jamie Madill81c2e252017-09-09 23:32:46 -04002354 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002355 }
Shannon Woods53a94a82014-06-24 15:20:36 -04002356}
Jamie Madillc9d442d2016-01-20 11:17:24 -05002357
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002358void State::setImageUnit(const Context *context,
2359 GLuint unit,
2360 Texture *texture,
2361 GLint level,
2362 GLboolean layered,
2363 GLint layer,
2364 GLenum access,
2365 GLenum format)
2366{
2367 mImageUnits[unit].texture.set(context, texture);
2368 mImageUnits[unit].level = level;
2369 mImageUnits[unit].layered = layered;
2370 mImageUnits[unit].layer = layer;
2371 mImageUnits[unit].access = access;
2372 mImageUnits[unit].format = format;
2373}
2374
2375const ImageUnit &State::getImageUnit(GLuint unit) const
2376{
2377 return mImageUnits[unit];
2378}
2379
Jamie Madill81c2e252017-09-09 23:32:46 -04002380// Handle a dirty texture event.
Jamie Madill05b35b22017-10-03 09:01:44 -04002381void State::signal(size_t textureIndex, InitState initState)
Jamie Madill81c2e252017-09-09 23:32:46 -04002382{
2383 // Conservatively assume all textures are dirty.
2384 // TODO(jmadill): More fine-grained update.
2385 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
2386}
2387
Jamie Madill05b35b22017-10-03 09:01:44 -04002388Error State::clearUnclearedActiveTextures(const Context *context)
2389{
Jamie Madilla59fc192017-11-02 12:57:58 -04002390 ASSERT(mRobustResourceInit);
Jamie Madill05b35b22017-10-03 09:01:44 -04002391
Jamie Madilla59fc192017-11-02 12:57:58 -04002392 for (auto textureIndex : mActiveTexturesMask)
Jamie Madill05b35b22017-10-03 09:01:44 -04002393 {
Jamie Madilla59fc192017-11-02 12:57:58 -04002394 Texture *texture = mCompleteTextureCache[textureIndex];
Jamie Madill05b35b22017-10-03 09:01:44 -04002395 if (texture)
2396 {
2397 ANGLE_TRY(texture->ensureInitialized(context));
2398 }
2399 }
Jamie Madilla59fc192017-11-02 12:57:58 -04002400
Jamie Madill05b35b22017-10-03 09:01:44 -04002401 return NoError();
2402}
2403
Jamie Madillc67323a2017-11-02 23:11:41 -04002404AttributesMask State::getAndResetDirtyCurrentValues() const
2405{
2406 AttributesMask retVal = mDirtyCurrentValues;
2407 mDirtyCurrentValues.reset();
2408 return retVal;
2409}
2410
Jamie Madillc9d442d2016-01-20 11:17:24 -05002411} // namespace gl