blob: b7bc0de23cd921f31815df588454f09643324ef3 [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/matrix_utils.h"
16#include "common/mathutil.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050017#include "libANGLE/Context.h"
18#include "libANGLE/Caps.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"
Shannon Woods53a94a82014-06-24 15:20:36 -040025
Olli Etuahobbf1c102016-06-28 13:31:33 +030026namespace
27{
28
29GLenum ActiveQueryType(const GLenum type)
30{
31 return (type == GL_ANY_SAMPLES_PASSED_CONSERVATIVE) ? GL_ANY_SAMPLES_PASSED : type;
32}
33
34} // anonymous namepace
35
Shannon Woods53a94a82014-06-24 15:20:36 -040036namespace gl
37{
Geoff Lang76b10c92014-09-05 16:28:14 -040038
Shannon Woods53a94a82014-06-24 15:20:36 -040039State::State()
Jamie Madille79b1e12015-11-04 16:36:37 -050040 : mMaxDrawBuffers(0),
41 mMaxCombinedTextureImageUnits(0),
42 mDepthClearValue(0),
43 mStencilClearValue(0),
44 mScissorTest(false),
45 mSampleCoverage(false),
46 mSampleCoverageValue(0),
47 mSampleCoverageInvert(false),
48 mStencilRef(0),
49 mStencilBackRef(0),
50 mLineWidth(0),
51 mGenerateMipmapHint(GL_NONE),
52 mFragmentShaderDerivativeHint(GL_NONE),
Geoff Langf41a7152016-09-19 15:11:17 -040053 mBindGeneratesResource(true),
Geoff Langfeb8c682017-02-13 16:07:35 -050054 mClientArraysEnabled(true),
Jamie Madille79b1e12015-11-04 16:36:37 -050055 mNearZ(0),
56 mFarZ(0),
57 mReadFramebuffer(nullptr),
58 mDrawFramebuffer(nullptr),
59 mProgram(nullptr),
60 mVertexArray(nullptr),
61 mActiveSampler(0),
Sami Väisänen74c23472016-05-09 17:30:30 +030062 mPrimitiveRestart(false),
63 mMultiSampling(false),
Geoff Lang1d2c41d2016-10-19 16:14:46 -070064 mSampleAlphaToOne(false),
Jamie Madille08a1d32017-03-07 17:24:06 -050065 mFramebufferSRGB(true),
Jamie Madillc43be722017-07-13 16:22:14 -040066 mRobustResourceInit(false),
67 mProgramBinaryCacheEnabled(false)
Shannon Woods53a94a82014-06-24 15:20:36 -040068{
Geoff Lang76b10c92014-09-05 16:28:14 -040069}
70
71State::~State()
72{
Geoff Lang76b10c92014-09-05 16:28:14 -040073}
74
Jamie Madill4928b7c2017-06-20 12:57:39 -040075void State::initialize(const Context *context,
Geoff Langf41a7152016-09-19 15:11:17 -040076 bool debug,
Geoff Langfeb8c682017-02-13 16:07:35 -050077 bool bindGeneratesResource,
Jamie Madille08a1d32017-03-07 17:24:06 -050078 bool clientArraysEnabled,
Jamie Madillc43be722017-07-13 16:22:14 -040079 bool robustResourceInit,
80 bool programBinaryCacheEnabled)
Geoff Lang76b10c92014-09-05 16:28:14 -040081{
Jamie Madill4928b7c2017-06-20 12:57:39 -040082 const Caps &caps = context->getCaps();
83 const Extensions &extensions = context->getExtensions();
84 const Version &clientVersion = context->getClientVersion();
85
Shannon Woods2df6a602014-09-26 16:12:07 -040086 mMaxDrawBuffers = caps.maxDrawBuffers;
87 mMaxCombinedTextureImageUnits = caps.maxCombinedTextureImageUnits;
Shannon Woods53a94a82014-06-24 15:20:36 -040088
Jamie Madillf75ab352015-03-16 10:46:52 -040089 setColorClearValue(0.0f, 0.0f, 0.0f, 0.0f);
Shannon Woods53a94a82014-06-24 15:20:36 -040090
91 mDepthClearValue = 1.0f;
92 mStencilClearValue = 0;
93
Shannon Woods53a94a82014-06-24 15:20:36 -040094 mScissorTest = false;
95 mScissor.x = 0;
96 mScissor.y = 0;
97 mScissor.width = 0;
98 mScissor.height = 0;
99
Shannon Woods53a94a82014-06-24 15:20:36 -0400100 mBlendColor.red = 0;
101 mBlendColor.green = 0;
102 mBlendColor.blue = 0;
103 mBlendColor.alpha = 0;
104
Shannon Woods53a94a82014-06-24 15:20:36 -0400105 mStencilRef = 0;
106 mStencilBackRef = 0;
107
108 mSampleCoverage = false;
109 mSampleCoverageValue = 1.0f;
110 mSampleCoverageInvert = false;
111 mGenerateMipmapHint = GL_DONT_CARE;
112 mFragmentShaderDerivativeHint = GL_DONT_CARE;
113
Geoff Langf41a7152016-09-19 15:11:17 -0400114 mBindGeneratesResource = bindGeneratesResource;
Geoff Langfeb8c682017-02-13 16:07:35 -0500115 mClientArraysEnabled = clientArraysEnabled;
Geoff Langf41a7152016-09-19 15:11:17 -0400116
Shannon Woods53a94a82014-06-24 15:20:36 -0400117 mLineWidth = 1.0f;
118
119 mViewport.x = 0;
120 mViewport.y = 0;
121 mViewport.width = 0;
122 mViewport.height = 0;
123 mNearZ = 0.0f;
124 mFarZ = 1.0f;
125
126 mBlend.colorMaskRed = true;
127 mBlend.colorMaskGreen = true;
128 mBlend.colorMaskBlue = true;
129 mBlend.colorMaskAlpha = true;
130
Geoff Lang76b10c92014-09-05 16:28:14 -0400131 mActiveSampler = 0;
132
Shannon Woods23e05002014-09-22 19:07:27 -0400133 mVertexAttribCurrentValues.resize(caps.maxVertexAttributes);
Shannon Woods53a94a82014-06-24 15:20:36 -0400134
Geoff Lang4dc3af02016-11-18 14:09:27 -0500135 mUniformBuffers.resize(caps.maxUniformBufferBindings);
Shannon Woodsf3acaf92014-09-23 18:07:11 -0400136
Geoff Lang76b10c92014-09-05 16:28:14 -0400137 mSamplerTextures[GL_TEXTURE_2D].resize(caps.maxCombinedTextureImageUnits);
138 mSamplerTextures[GL_TEXTURE_CUBE_MAP].resize(caps.maxCombinedTextureImageUnits);
Geoff Langeb66a6e2016-10-31 13:06:12 -0400139 if (clientVersion >= Version(3, 0))
Shannon Woods53a94a82014-06-24 15:20:36 -0400140 {
Geoff Lang76b10c92014-09-05 16:28:14 -0400141 // TODO: These could also be enabled via extension
142 mSamplerTextures[GL_TEXTURE_2D_ARRAY].resize(caps.maxCombinedTextureImageUnits);
143 mSamplerTextures[GL_TEXTURE_3D].resize(caps.maxCombinedTextureImageUnits);
Shannon Woods53a94a82014-06-24 15:20:36 -0400144 }
Geoff Lang3b573612016-10-31 14:08:10 -0400145 if (clientVersion >= Version(3, 1))
146 {
147 mSamplerTextures[GL_TEXTURE_2D_MULTISAMPLE].resize(caps.maxCombinedTextureImageUnits);
Jiajia Qin6eafb042016-12-27 17:04:07 +0800148
149 mAtomicCounterBuffers.resize(caps.maxAtomicCounterBufferBindings);
Jiajia Qinf546e7d2017-03-27 14:12:59 +0800150 mShaderStorageBuffers.resize(caps.maxShaderStorageBufferBindings);
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800151 mImageUnits.resize(caps.maxImageUnits);
Geoff Lang3b573612016-10-31 14:08:10 -0400152 }
Corentin Wallez13c0dd42017-07-04 18:27:01 -0400153 if (extensions.textureRectangle)
154 {
155 mSamplerTextures[GL_TEXTURE_RECTANGLE_ANGLE].resize(caps.maxCombinedTextureImageUnits);
156 }
Ian Ewellbda75592016-04-18 17:25:54 -0400157 if (extensions.eglImageExternal || extensions.eglStreamConsumerExternal)
158 {
159 mSamplerTextures[GL_TEXTURE_EXTERNAL_OES].resize(caps.maxCombinedTextureImageUnits);
160 }
Jamie Madill81c2e252017-09-09 23:32:46 -0400161 mCompleteTextureCache.resize(caps.maxCombinedTextureImageUnits, nullptr);
162 mCompleteTextureBindings.reserve(caps.maxCombinedTextureImageUnits);
163 for (uint32_t textureIndex = 0; textureIndex < caps.maxCombinedTextureImageUnits;
164 ++textureIndex)
165 {
166 mCompleteTextureBindings.emplace_back(OnAttachmentDirtyBinding(this, textureIndex));
167 }
Shannon Woods53a94a82014-06-24 15:20:36 -0400168
Geoff Lang76b10c92014-09-05 16:28:14 -0400169 mSamplers.resize(caps.maxCombinedTextureImageUnits);
Shannon Woods53a94a82014-06-24 15:20:36 -0400170
Jamie Madill4928b7c2017-06-20 12:57:39 -0400171 mActiveQueries[GL_ANY_SAMPLES_PASSED].set(context, nullptr);
172 mActiveQueries[GL_ANY_SAMPLES_PASSED_CONSERVATIVE].set(context, nullptr);
173 mActiveQueries[GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN].set(context, nullptr);
174 mActiveQueries[GL_TIME_ELAPSED_EXT].set(context, nullptr);
175 mActiveQueries[GL_COMMANDS_COMPLETED_CHROMIUM].set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400176
Ian Ewell3ffd78b2016-01-22 16:09:42 -0500177 mProgram = nullptr;
Shannon Woods53a94a82014-06-24 15:20:36 -0400178
Ian Ewell3ffd78b2016-01-22 16:09:42 -0500179 mReadFramebuffer = nullptr;
180 mDrawFramebuffer = nullptr;
Jamie Madillb4b53c52015-02-03 15:22:48 -0500181
182 mPrimitiveRestart = false;
Geoff Lang70d0f492015-12-10 17:45:46 -0500183
184 mDebug.setOutputEnabled(debug);
185 mDebug.setMaxLoggedMessages(extensions.maxDebugLoggedMessages);
Sami Väisänen74c23472016-05-09 17:30:30 +0300186
187 if (extensions.framebufferMultisample)
188 {
189 mMultiSampling = true;
190 mSampleAlphaToOne = false;
191 }
Sami Väisänena797e062016-05-12 15:23:40 +0300192
193 mCoverageModulation = GL_NONE;
Sami Väisänene45e53b2016-05-25 10:36:04 +0300194
195 angle::Matrix<GLfloat>::setToIdentity(mPathMatrixProj);
196 angle::Matrix<GLfloat>::setToIdentity(mPathMatrixMV);
197 mPathStencilFunc = GL_ALWAYS;
198 mPathStencilRef = 0;
199 mPathStencilMask = std::numeric_limits<GLuint>::max();
Jamie Madille08a1d32017-03-07 17:24:06 -0500200
201 mRobustResourceInit = robustResourceInit;
Jamie Madillc43be722017-07-13 16:22:14 -0400202 mProgramBinaryCacheEnabled = programBinaryCacheEnabled;
Shannon Woods53a94a82014-06-24 15:20:36 -0400203}
204
Jamie Madill6c1f6712017-02-14 19:08:04 -0500205void State::reset(const Context *context)
Shannon Woods53a94a82014-06-24 15:20:36 -0400206{
Jamie Madill8693bdb2017-09-02 15:32:14 -0400207 for (auto &bindingVec : mSamplerTextures)
Shannon Woods53a94a82014-06-24 15:20:36 -0400208 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400209 TextureBindingVector &textureVector = bindingVec.second;
Geoff Lang76b10c92014-09-05 16:28:14 -0400210 for (size_t textureIdx = 0; textureIdx < textureVector.size(); textureIdx++)
Shannon Woods53a94a82014-06-24 15:20:36 -0400211 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400212 textureVector[textureIdx].set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400213 }
214 }
Geoff Lang76b10c92014-09-05 16:28:14 -0400215 for (size_t samplerIdx = 0; samplerIdx < mSamplers.size(); samplerIdx++)
216 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400217 mSamplers[samplerIdx].set(context, nullptr);
Geoff Lang76b10c92014-09-05 16:28:14 -0400218 }
Shannon Woods53a94a82014-06-24 15:20:36 -0400219
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800220 for (auto &imageUnit : mImageUnits)
221 {
222 imageUnit.texture.set(context, nullptr);
223 imageUnit.level = 0;
224 imageUnit.layered = false;
225 imageUnit.layer = 0;
226 imageUnit.access = GL_READ_ONLY;
227 imageUnit.format = GL_R32UI;
228 }
229
Jamie Madill4928b7c2017-06-20 12:57:39 -0400230 mArrayBuffer.set(context, nullptr);
231 mDrawIndirectBuffer.set(context, nullptr);
232 mRenderbuffer.set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400233
Geoff Lang7dd2e102014-11-10 15:19:26 -0500234 if (mProgram)
235 {
Jamie Madill6c1f6712017-02-14 19:08:04 -0500236 mProgram->release(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500237 }
Yunchao Hed7297bf2017-04-19 15:27:10 +0800238 mProgram = nullptr;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500239
Jamie Madill4928b7c2017-06-20 12:57:39 -0400240 mTransformFeedback.set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400241
242 for (State::ActiveQueryMap::iterator i = mActiveQueries.begin(); i != mActiveQueries.end(); i++)
243 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400244 i->second.set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400245 }
246
Jamie Madill4928b7c2017-06-20 12:57:39 -0400247 mGenericUniformBuffer.set(context, nullptr);
Shannon Woods8299bb02014-09-26 18:55:43 -0400248 for (BufferVector::iterator bufItr = mUniformBuffers.begin(); bufItr != mUniformBuffers.end(); ++bufItr)
Shannon Woods53a94a82014-06-24 15:20:36 -0400249 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400250 bufItr->set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400251 }
252
Jamie Madill4928b7c2017-06-20 12:57:39 -0400253 mCopyReadBuffer.set(context, nullptr);
254 mCopyWriteBuffer.set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400255
Jamie Madill4928b7c2017-06-20 12:57:39 -0400256 mPack.pixelBuffer.set(context, nullptr);
257 mUnpack.pixelBuffer.set(context, nullptr);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500258
Jamie Madill4928b7c2017-06-20 12:57:39 -0400259 mGenericAtomicCounterBuffer.set(context, nullptr);
Jiajia Qin6eafb042016-12-27 17:04:07 +0800260 for (auto &buf : mAtomicCounterBuffers)
261 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400262 buf.set(context, nullptr);
Jiajia Qin6eafb042016-12-27 17:04:07 +0800263 }
264
Jamie Madill4928b7c2017-06-20 12:57:39 -0400265 mGenericShaderStorageBuffer.set(context, nullptr);
Jiajia Qinf546e7d2017-03-27 14:12:59 +0800266 for (auto &buf : mShaderStorageBuffers)
267 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400268 buf.set(context, nullptr);
Jiajia Qinf546e7d2017-03-27 14:12:59 +0800269 }
270
Sami Väisänene45e53b2016-05-25 10:36:04 +0300271 angle::Matrix<GLfloat>::setToIdentity(mPathMatrixProj);
272 angle::Matrix<GLfloat>::setToIdentity(mPathMatrixMV);
273 mPathStencilFunc = GL_ALWAYS;
274 mPathStencilRef = 0;
275 mPathStencilMask = std::numeric_limits<GLuint>::max();
276
Jamie Madill1b94d432015-08-07 13:23:23 -0400277 // TODO(jmadill): Is this necessary?
278 setAllDirtyBits();
Shannon Woods53a94a82014-06-24 15:20:36 -0400279}
280
281const RasterizerState &State::getRasterizerState() const
282{
283 return mRasterizer;
284}
285
286const BlendState &State::getBlendState() const
287{
288 return mBlend;
289}
290
291const DepthStencilState &State::getDepthStencilState() const
292{
293 return mDepthStencil;
294}
295
Jamie Madillf75ab352015-03-16 10:46:52 -0400296void State::setColorClearValue(float red, float green, float blue, float alpha)
Shannon Woods53a94a82014-06-24 15:20:36 -0400297{
298 mColorClearValue.red = red;
299 mColorClearValue.green = green;
300 mColorClearValue.blue = blue;
301 mColorClearValue.alpha = alpha;
Jamie Madill1b94d432015-08-07 13:23:23 -0400302 mDirtyBits.set(DIRTY_BIT_CLEAR_COLOR);
Shannon Woods53a94a82014-06-24 15:20:36 -0400303}
304
Jamie Madillf75ab352015-03-16 10:46:52 -0400305void State::setDepthClearValue(float depth)
Shannon Woods53a94a82014-06-24 15:20:36 -0400306{
307 mDepthClearValue = depth;
Jamie Madill1b94d432015-08-07 13:23:23 -0400308 mDirtyBits.set(DIRTY_BIT_CLEAR_DEPTH);
Shannon Woods53a94a82014-06-24 15:20:36 -0400309}
310
Jamie Madillf75ab352015-03-16 10:46:52 -0400311void State::setStencilClearValue(int stencil)
Shannon Woods53a94a82014-06-24 15:20:36 -0400312{
313 mStencilClearValue = stencil;
Jamie Madill1b94d432015-08-07 13:23:23 -0400314 mDirtyBits.set(DIRTY_BIT_CLEAR_STENCIL);
Shannon Woods53a94a82014-06-24 15:20:36 -0400315}
316
Shannon Woods53a94a82014-06-24 15:20:36 -0400317void State::setColorMask(bool red, bool green, bool blue, bool alpha)
318{
319 mBlend.colorMaskRed = red;
320 mBlend.colorMaskGreen = green;
321 mBlend.colorMaskBlue = blue;
322 mBlend.colorMaskAlpha = alpha;
Jamie Madill1b94d432015-08-07 13:23:23 -0400323 mDirtyBits.set(DIRTY_BIT_COLOR_MASK);
Shannon Woods53a94a82014-06-24 15:20:36 -0400324}
325
326void State::setDepthMask(bool mask)
327{
328 mDepthStencil.depthMask = mask;
Jamie Madill1b94d432015-08-07 13:23:23 -0400329 mDirtyBits.set(DIRTY_BIT_DEPTH_MASK);
Shannon Woods53a94a82014-06-24 15:20:36 -0400330}
331
332bool State::isRasterizerDiscardEnabled() const
333{
334 return mRasterizer.rasterizerDiscard;
335}
336
337void State::setRasterizerDiscard(bool enabled)
338{
339 mRasterizer.rasterizerDiscard = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400340 mDirtyBits.set(DIRTY_BIT_RASTERIZER_DISCARD_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400341}
342
343bool State::isCullFaceEnabled() const
344{
345 return mRasterizer.cullFace;
346}
347
348void State::setCullFace(bool enabled)
349{
350 mRasterizer.cullFace = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400351 mDirtyBits.set(DIRTY_BIT_CULL_FACE_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400352}
353
354void State::setCullMode(GLenum mode)
355{
356 mRasterizer.cullMode = mode;
Jamie Madill1b94d432015-08-07 13:23:23 -0400357 mDirtyBits.set(DIRTY_BIT_CULL_FACE);
Shannon Woods53a94a82014-06-24 15:20:36 -0400358}
359
360void State::setFrontFace(GLenum front)
361{
362 mRasterizer.frontFace = front;
Jamie Madill1b94d432015-08-07 13:23:23 -0400363 mDirtyBits.set(DIRTY_BIT_FRONT_FACE);
Shannon Woods53a94a82014-06-24 15:20:36 -0400364}
365
366bool State::isDepthTestEnabled() const
367{
368 return mDepthStencil.depthTest;
369}
370
371void State::setDepthTest(bool enabled)
372{
373 mDepthStencil.depthTest = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400374 mDirtyBits.set(DIRTY_BIT_DEPTH_TEST_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400375}
376
377void State::setDepthFunc(GLenum depthFunc)
378{
379 mDepthStencil.depthFunc = depthFunc;
Jamie Madill1b94d432015-08-07 13:23:23 -0400380 mDirtyBits.set(DIRTY_BIT_DEPTH_FUNC);
Shannon Woods53a94a82014-06-24 15:20:36 -0400381}
382
383void State::setDepthRange(float zNear, float zFar)
384{
385 mNearZ = zNear;
386 mFarZ = zFar;
Jamie Madill1b94d432015-08-07 13:23:23 -0400387 mDirtyBits.set(DIRTY_BIT_DEPTH_RANGE);
Shannon Woods53a94a82014-06-24 15:20:36 -0400388}
389
Geoff Langd42f5b82015-04-16 14:03:29 -0400390float State::getNearPlane() const
Shannon Woods53a94a82014-06-24 15:20:36 -0400391{
Geoff Langd42f5b82015-04-16 14:03:29 -0400392 return mNearZ;
393}
394
395float State::getFarPlane() const
396{
397 return mFarZ;
Shannon Woods53a94a82014-06-24 15:20:36 -0400398}
399
400bool State::isBlendEnabled() const
401{
402 return mBlend.blend;
403}
404
405void State::setBlend(bool enabled)
406{
407 mBlend.blend = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400408 mDirtyBits.set(DIRTY_BIT_BLEND_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400409}
410
411void State::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
412{
413 mBlend.sourceBlendRGB = sourceRGB;
414 mBlend.destBlendRGB = destRGB;
415 mBlend.sourceBlendAlpha = sourceAlpha;
416 mBlend.destBlendAlpha = destAlpha;
Jamie Madill1b94d432015-08-07 13:23:23 -0400417 mDirtyBits.set(DIRTY_BIT_BLEND_FUNCS);
Shannon Woods53a94a82014-06-24 15:20:36 -0400418}
419
420void State::setBlendColor(float red, float green, float blue, float alpha)
421{
422 mBlendColor.red = red;
423 mBlendColor.green = green;
424 mBlendColor.blue = blue;
425 mBlendColor.alpha = alpha;
Jamie Madill1b94d432015-08-07 13:23:23 -0400426 mDirtyBits.set(DIRTY_BIT_BLEND_COLOR);
Shannon Woods53a94a82014-06-24 15:20:36 -0400427}
428
429void State::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
430{
431 mBlend.blendEquationRGB = rgbEquation;
432 mBlend.blendEquationAlpha = alphaEquation;
Jamie Madill1b94d432015-08-07 13:23:23 -0400433 mDirtyBits.set(DIRTY_BIT_BLEND_EQUATIONS);
Shannon Woods53a94a82014-06-24 15:20:36 -0400434}
435
436const ColorF &State::getBlendColor() const
437{
438 return mBlendColor;
439}
440
441bool State::isStencilTestEnabled() const
442{
443 return mDepthStencil.stencilTest;
444}
445
446void State::setStencilTest(bool enabled)
447{
448 mDepthStencil.stencilTest = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400449 mDirtyBits.set(DIRTY_BIT_STENCIL_TEST_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400450}
451
452void State::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
453{
454 mDepthStencil.stencilFunc = stencilFunc;
455 mStencilRef = (stencilRef > 0) ? stencilRef : 0;
456 mDepthStencil.stencilMask = stencilMask;
Jamie Madill1b94d432015-08-07 13:23:23 -0400457 mDirtyBits.set(DIRTY_BIT_STENCIL_FUNCS_FRONT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400458}
459
460void State::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
461{
462 mDepthStencil.stencilBackFunc = stencilBackFunc;
463 mStencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
464 mDepthStencil.stencilBackMask = stencilBackMask;
Jamie Madill1b94d432015-08-07 13:23:23 -0400465 mDirtyBits.set(DIRTY_BIT_STENCIL_FUNCS_BACK);
Shannon Woods53a94a82014-06-24 15:20:36 -0400466}
467
468void State::setStencilWritemask(GLuint stencilWritemask)
469{
470 mDepthStencil.stencilWritemask = stencilWritemask;
Jamie Madill1b94d432015-08-07 13:23:23 -0400471 mDirtyBits.set(DIRTY_BIT_STENCIL_WRITEMASK_FRONT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400472}
473
474void State::setStencilBackWritemask(GLuint stencilBackWritemask)
475{
476 mDepthStencil.stencilBackWritemask = stencilBackWritemask;
Jamie Madill1b94d432015-08-07 13:23:23 -0400477 mDirtyBits.set(DIRTY_BIT_STENCIL_WRITEMASK_BACK);
Shannon Woods53a94a82014-06-24 15:20:36 -0400478}
479
480void State::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
481{
482 mDepthStencil.stencilFail = stencilFail;
483 mDepthStencil.stencilPassDepthFail = stencilPassDepthFail;
484 mDepthStencil.stencilPassDepthPass = stencilPassDepthPass;
Jamie Madill1b94d432015-08-07 13:23:23 -0400485 mDirtyBits.set(DIRTY_BIT_STENCIL_OPS_FRONT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400486}
487
488void State::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
489{
490 mDepthStencil.stencilBackFail = stencilBackFail;
491 mDepthStencil.stencilBackPassDepthFail = stencilBackPassDepthFail;
492 mDepthStencil.stencilBackPassDepthPass = stencilBackPassDepthPass;
Jamie Madill1b94d432015-08-07 13:23:23 -0400493 mDirtyBits.set(DIRTY_BIT_STENCIL_OPS_BACK);
Shannon Woods53a94a82014-06-24 15:20:36 -0400494}
495
496GLint State::getStencilRef() const
497{
498 return mStencilRef;
499}
500
501GLint State::getStencilBackRef() const
502{
503 return mStencilBackRef;
504}
505
506bool State::isPolygonOffsetFillEnabled() const
507{
508 return mRasterizer.polygonOffsetFill;
509}
510
511void State::setPolygonOffsetFill(bool enabled)
512{
Jamie Madill1b94d432015-08-07 13:23:23 -0400513 mRasterizer.polygonOffsetFill = enabled;
514 mDirtyBits.set(DIRTY_BIT_POLYGON_OFFSET_FILL_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400515}
516
517void State::setPolygonOffsetParams(GLfloat factor, GLfloat units)
518{
519 // An application can pass NaN values here, so handle this gracefully
520 mRasterizer.polygonOffsetFactor = factor != factor ? 0.0f : factor;
521 mRasterizer.polygonOffsetUnits = units != units ? 0.0f : units;
Jamie Madill1b94d432015-08-07 13:23:23 -0400522 mDirtyBits.set(DIRTY_BIT_POLYGON_OFFSET);
Shannon Woods53a94a82014-06-24 15:20:36 -0400523}
524
525bool State::isSampleAlphaToCoverageEnabled() const
526{
527 return mBlend.sampleAlphaToCoverage;
528}
529
530void State::setSampleAlphaToCoverage(bool enabled)
531{
532 mBlend.sampleAlphaToCoverage = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400533 mDirtyBits.set(DIRTY_BIT_SAMPLE_ALPHA_TO_COVERAGE_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400534}
535
536bool State::isSampleCoverageEnabled() const
537{
538 return mSampleCoverage;
539}
540
541void State::setSampleCoverage(bool enabled)
542{
543 mSampleCoverage = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400544 mDirtyBits.set(DIRTY_BIT_SAMPLE_COVERAGE_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400545}
546
547void State::setSampleCoverageParams(GLclampf value, bool invert)
548{
549 mSampleCoverageValue = value;
550 mSampleCoverageInvert = invert;
Jamie Madill1b94d432015-08-07 13:23:23 -0400551 mDirtyBits.set(DIRTY_BIT_SAMPLE_COVERAGE);
Shannon Woods53a94a82014-06-24 15:20:36 -0400552}
553
Geoff Lang0fbb6002015-04-16 11:11:53 -0400554GLclampf State::getSampleCoverageValue() const
Shannon Woods53a94a82014-06-24 15:20:36 -0400555{
Geoff Lang0fbb6002015-04-16 11:11:53 -0400556 return mSampleCoverageValue;
557}
Shannon Woods53a94a82014-06-24 15:20:36 -0400558
Geoff Lang0fbb6002015-04-16 11:11:53 -0400559bool State::getSampleCoverageInvert() const
560{
561 return mSampleCoverageInvert;
Shannon Woods53a94a82014-06-24 15:20:36 -0400562}
563
Sami Väisänen74c23472016-05-09 17:30:30 +0300564void State::setSampleAlphaToOne(bool enabled)
565{
566 mSampleAlphaToOne = enabled;
567 mDirtyBits.set(DIRTY_BIT_SAMPLE_ALPHA_TO_ONE);
568}
569
570bool State::isSampleAlphaToOneEnabled() const
571{
572 return mSampleAlphaToOne;
573}
574
575void State::setMultisampling(bool enabled)
576{
577 mMultiSampling = enabled;
578 mDirtyBits.set(DIRTY_BIT_MULTISAMPLING);
579}
580
581bool State::isMultisamplingEnabled() const
582{
583 return mMultiSampling;
584}
585
Shannon Woods53a94a82014-06-24 15:20:36 -0400586bool State::isScissorTestEnabled() const
587{
588 return mScissorTest;
589}
590
591void State::setScissorTest(bool enabled)
592{
593 mScissorTest = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400594 mDirtyBits.set(DIRTY_BIT_SCISSOR_TEST_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400595}
596
597void State::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
598{
599 mScissor.x = x;
600 mScissor.y = y;
601 mScissor.width = width;
602 mScissor.height = height;
Jamie Madill1b94d432015-08-07 13:23:23 -0400603 mDirtyBits.set(DIRTY_BIT_SCISSOR);
Shannon Woods53a94a82014-06-24 15:20:36 -0400604}
605
606const Rectangle &State::getScissor() const
607{
608 return mScissor;
609}
610
611bool State::isDitherEnabled() const
612{
613 return mBlend.dither;
614}
615
616void State::setDither(bool enabled)
617{
618 mBlend.dither = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400619 mDirtyBits.set(DIRTY_BIT_DITHER_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400620}
621
Jamie Madillb4b53c52015-02-03 15:22:48 -0500622bool State::isPrimitiveRestartEnabled() const
623{
624 return mPrimitiveRestart;
625}
626
627void State::setPrimitiveRestart(bool enabled)
628{
629 mPrimitiveRestart = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400630 mDirtyBits.set(DIRTY_BIT_PRIMITIVE_RESTART_ENABLED);
Jamie Madillb4b53c52015-02-03 15:22:48 -0500631}
632
Shannon Woods53a94a82014-06-24 15:20:36 -0400633void State::setEnableFeature(GLenum feature, bool enabled)
634{
635 switch (feature)
636 {
Sami Väisänen74c23472016-05-09 17:30:30 +0300637 case GL_MULTISAMPLE_EXT: setMultisampling(enabled); break;
638 case GL_SAMPLE_ALPHA_TO_ONE_EXT: setSampleAlphaToOne(enabled); break;
Shannon Woods53a94a82014-06-24 15:20:36 -0400639 case GL_CULL_FACE: setCullFace(enabled); break;
640 case GL_POLYGON_OFFSET_FILL: setPolygonOffsetFill(enabled); break;
641 case GL_SAMPLE_ALPHA_TO_COVERAGE: setSampleAlphaToCoverage(enabled); break;
642 case GL_SAMPLE_COVERAGE: setSampleCoverage(enabled); break;
643 case GL_SCISSOR_TEST: setScissorTest(enabled); break;
644 case GL_STENCIL_TEST: setStencilTest(enabled); break;
645 case GL_DEPTH_TEST: setDepthTest(enabled); break;
646 case GL_BLEND: setBlend(enabled); break;
647 case GL_DITHER: setDither(enabled); break;
Jamie Madillb4b53c52015-02-03 15:22:48 -0500648 case GL_PRIMITIVE_RESTART_FIXED_INDEX: setPrimitiveRestart(enabled); break;
Shannon Woods53a94a82014-06-24 15:20:36 -0400649 case GL_RASTERIZER_DISCARD: setRasterizerDiscard(enabled); break;
Geoff Lang3b573612016-10-31 14:08:10 -0400650 case GL_SAMPLE_MASK:
Geoff Lang9f090372016-12-02 10:20:43 -0500651 if (enabled)
652 {
653 // Enabling this feature is not implemented yet.
654 UNIMPLEMENTED();
655 }
Geoff Lang3b573612016-10-31 14:08:10 -0400656 break;
Geoff Lang70d0f492015-12-10 17:45:46 -0500657 case GL_DEBUG_OUTPUT_SYNCHRONOUS:
658 mDebug.setOutputSynchronous(enabled);
659 break;
660 case GL_DEBUG_OUTPUT:
661 mDebug.setOutputEnabled(enabled);
662 break;
Geoff Lang1d2c41d2016-10-19 16:14:46 -0700663 case GL_FRAMEBUFFER_SRGB_EXT:
664 setFramebufferSRGB(enabled);
665 break;
Shannon Woods53a94a82014-06-24 15:20:36 -0400666 default: UNREACHABLE();
667 }
668}
669
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700670bool State::getEnableFeature(GLenum feature) const
Shannon Woods53a94a82014-06-24 15:20:36 -0400671{
672 switch (feature)
673 {
Sami Väisänen74c23472016-05-09 17:30:30 +0300674 case GL_MULTISAMPLE_EXT: return isMultisamplingEnabled();
675 case GL_SAMPLE_ALPHA_TO_ONE_EXT: return isSampleAlphaToOneEnabled();
Shannon Woods53a94a82014-06-24 15:20:36 -0400676 case GL_CULL_FACE: return isCullFaceEnabled();
677 case GL_POLYGON_OFFSET_FILL: return isPolygonOffsetFillEnabled();
678 case GL_SAMPLE_ALPHA_TO_COVERAGE: return isSampleAlphaToCoverageEnabled();
679 case GL_SAMPLE_COVERAGE: return isSampleCoverageEnabled();
680 case GL_SCISSOR_TEST: return isScissorTestEnabled();
681 case GL_STENCIL_TEST: return isStencilTestEnabled();
682 case GL_DEPTH_TEST: return isDepthTestEnabled();
683 case GL_BLEND: return isBlendEnabled();
684 case GL_DITHER: return isDitherEnabled();
Jamie Madillb4b53c52015-02-03 15:22:48 -0500685 case GL_PRIMITIVE_RESTART_FIXED_INDEX: return isPrimitiveRestartEnabled();
Shannon Woods53a94a82014-06-24 15:20:36 -0400686 case GL_RASTERIZER_DISCARD: return isRasterizerDiscardEnabled();
Geoff Langb5e997f2016-12-06 10:55:34 -0500687 case GL_SAMPLE_MASK:
688 UNIMPLEMENTED();
689 return false;
Geoff Lang70d0f492015-12-10 17:45:46 -0500690 case GL_DEBUG_OUTPUT_SYNCHRONOUS:
691 return mDebug.isOutputSynchronous();
692 case GL_DEBUG_OUTPUT:
693 return mDebug.isOutputEnabled();
Geoff Langf41a7152016-09-19 15:11:17 -0400694 case GL_BIND_GENERATES_RESOURCE_CHROMIUM:
695 return isBindGeneratesResourceEnabled();
Geoff Langfeb8c682017-02-13 16:07:35 -0500696 case GL_CLIENT_ARRAYS_ANGLE:
697 return areClientArraysEnabled();
Geoff Lang1d2c41d2016-10-19 16:14:46 -0700698 case GL_FRAMEBUFFER_SRGB_EXT:
699 return getFramebufferSRGB();
Jamie Madille08a1d32017-03-07 17:24:06 -0500700 case GL_CONTEXT_ROBUST_RESOURCE_INITIALIZATION_ANGLE:
701 return mRobustResourceInit;
Jamie Madillc43be722017-07-13 16:22:14 -0400702 case GL_PROGRAM_CACHE_ENABLED_ANGLE:
703 return mProgramBinaryCacheEnabled;
704
705 default:
706 UNREACHABLE();
707 return false;
Shannon Woods53a94a82014-06-24 15:20:36 -0400708 }
709}
710
711void State::setLineWidth(GLfloat width)
712{
713 mLineWidth = width;
Jamie Madill1b94d432015-08-07 13:23:23 -0400714 mDirtyBits.set(DIRTY_BIT_LINE_WIDTH);
Shannon Woods53a94a82014-06-24 15:20:36 -0400715}
716
Geoff Lang4b3f4162015-04-16 13:22:05 -0400717float State::getLineWidth() const
718{
719 return mLineWidth;
720}
721
Shannon Woods53a94a82014-06-24 15:20:36 -0400722void State::setGenerateMipmapHint(GLenum hint)
723{
724 mGenerateMipmapHint = hint;
Jamie Madill1b94d432015-08-07 13:23:23 -0400725 mDirtyBits.set(DIRTY_BIT_GENERATE_MIPMAP_HINT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400726}
727
728void State::setFragmentShaderDerivativeHint(GLenum hint)
729{
730 mFragmentShaderDerivativeHint = hint;
Jamie Madill1b94d432015-08-07 13:23:23 -0400731 mDirtyBits.set(DIRTY_BIT_SHADER_DERIVATIVE_HINT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400732 // TODO: Propagate the hint to shader translator so we can write
733 // ddx, ddx_coarse, or ddx_fine depending on the hint.
734 // Ignore for now. It is valid for implementations to ignore hint.
735}
736
Geoff Langf41a7152016-09-19 15:11:17 -0400737bool State::isBindGeneratesResourceEnabled() const
738{
739 return mBindGeneratesResource;
740}
741
Geoff Langfeb8c682017-02-13 16:07:35 -0500742bool State::areClientArraysEnabled() const
743{
744 return mClientArraysEnabled;
745}
746
Shannon Woods53a94a82014-06-24 15:20:36 -0400747void State::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
748{
749 mViewport.x = x;
750 mViewport.y = y;
751 mViewport.width = width;
752 mViewport.height = height;
Jamie Madill1b94d432015-08-07 13:23:23 -0400753 mDirtyBits.set(DIRTY_BIT_VIEWPORT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400754}
755
756const Rectangle &State::getViewport() const
757{
758 return mViewport;
759}
760
761void State::setActiveSampler(unsigned int active)
762{
763 mActiveSampler = active;
764}
765
766unsigned int State::getActiveSampler() const
767{
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700768 return static_cast<unsigned int>(mActiveSampler);
Shannon Woods53a94a82014-06-24 15:20:36 -0400769}
770
Jamie Madill4928b7c2017-06-20 12:57:39 -0400771void State::setSamplerTexture(const Context *context, GLenum type, Texture *texture)
Shannon Woods53a94a82014-06-24 15:20:36 -0400772{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400773 mSamplerTextures[type][mActiveSampler].set(context, texture);
Jamie Madill81c2e252017-09-09 23:32:46 -0400774 mDirtyBits.set(DIRTY_BIT_TEXTURE_BINDINGS);
775 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
Shannon Woods53a94a82014-06-24 15:20:36 -0400776}
777
Jamie Madillc29968b2016-01-20 11:17:23 -0500778Texture *State::getTargetTexture(GLenum target) const
779{
780 return getSamplerTexture(static_cast<unsigned int>(mActiveSampler), target);
781}
782
Geoff Lang76b10c92014-09-05 16:28:14 -0400783Texture *State::getSamplerTexture(unsigned int sampler, GLenum type) const
Shannon Woods53a94a82014-06-24 15:20:36 -0400784{
Jamie Madill5864ac22015-01-12 14:43:07 -0500785 const auto it = mSamplerTextures.find(type);
786 ASSERT(it != mSamplerTextures.end());
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400787 ASSERT(sampler < it->second.size());
Jamie Madill5864ac22015-01-12 14:43:07 -0500788 return it->second[sampler].get();
Shannon Woods53a94a82014-06-24 15:20:36 -0400789}
790
Geoff Lang76b10c92014-09-05 16:28:14 -0400791GLuint State::getSamplerTextureId(unsigned int sampler, GLenum type) const
Shannon Woods53a94a82014-06-24 15:20:36 -0400792{
Jamie Madill5864ac22015-01-12 14:43:07 -0500793 const auto it = mSamplerTextures.find(type);
794 ASSERT(it != mSamplerTextures.end());
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400795 ASSERT(sampler < it->second.size());
Jamie Madill5864ac22015-01-12 14:43:07 -0500796 return it->second[sampler].id();
Shannon Woods53a94a82014-06-24 15:20:36 -0400797}
798
Jamie Madilla02315b2017-02-23 14:14:47 -0500799void State::detachTexture(const Context *context, const TextureMap &zeroTextures, GLuint texture)
Shannon Woods53a94a82014-06-24 15:20:36 -0400800{
801 // Textures have a detach method on State rather than a simple
802 // removeBinding, because the zero/null texture objects are managed
803 // separately, and don't have to go through the Context's maps or
804 // the ResourceManager.
805
806 // [OpenGL ES 2.0.24] section 3.8 page 84:
807 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
808 // rebound to texture object zero
809
Corentin Walleza2257da2016-04-19 16:43:12 -0400810 for (auto &bindingVec : mSamplerTextures)
Shannon Woods53a94a82014-06-24 15:20:36 -0400811 {
Corentin Walleza2257da2016-04-19 16:43:12 -0400812 GLenum textureType = bindingVec.first;
813 TextureBindingVector &textureVector = bindingVec.second;
Jamie Madill81c2e252017-09-09 23:32:46 -0400814 for (BindingPointer<Texture> &binding : textureVector)
Shannon Woods53a94a82014-06-24 15:20:36 -0400815 {
Geoff Lang76b10c92014-09-05 16:28:14 -0400816 if (binding.id() == texture)
Shannon Woods53a94a82014-06-24 15:20:36 -0400817 {
Jamie Madill5864ac22015-01-12 14:43:07 -0500818 auto it = zeroTextures.find(textureType);
819 ASSERT(it != zeroTextures.end());
Jamie Madille6382c32014-11-07 15:05:26 -0500820 // Zero textures are the "default" textures instead of NULL
Jamie Madill4928b7c2017-06-20 12:57:39 -0400821 binding.set(context, it->second.get());
Jamie Madill81c2e252017-09-09 23:32:46 -0400822 mDirtyBits.set(DIRTY_BIT_TEXTURE_BINDINGS);
Shannon Woods53a94a82014-06-24 15:20:36 -0400823 }
824 }
825 }
826
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800827 for (auto &bindingImageUnit : mImageUnits)
828 {
829 if (bindingImageUnit.texture.id() == texture)
830 {
831 bindingImageUnit.texture.set(context, nullptr);
832 bindingImageUnit.level = 0;
833 bindingImageUnit.layered = false;
834 bindingImageUnit.layer = 0;
835 bindingImageUnit.access = GL_READ_ONLY;
836 bindingImageUnit.format = GL_R32UI;
837 break;
838 }
839 }
840
Shannon Woods53a94a82014-06-24 15:20:36 -0400841 // [OpenGL ES 2.0.24] section 4.4 page 112:
842 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
843 // as if Texture2DAttachment had been called, with a texture of 0, for each attachment point to which this
844 // image was attached in the currently bound framebuffer.
845
Jamie Madill8693bdb2017-09-02 15:32:14 -0400846 if (mReadFramebuffer && mReadFramebuffer->detachTexture(context, texture))
Shannon Woods53a94a82014-06-24 15:20:36 -0400847 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400848 mDirtyObjects.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
Shannon Woods53a94a82014-06-24 15:20:36 -0400849 }
850
Jamie Madill8693bdb2017-09-02 15:32:14 -0400851 if (mDrawFramebuffer && mDrawFramebuffer->detachTexture(context, texture))
Shannon Woods53a94a82014-06-24 15:20:36 -0400852 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400853 mDirtyObjects.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
Shannon Woods53a94a82014-06-24 15:20:36 -0400854 }
855}
856
Jamie Madill4928b7c2017-06-20 12:57:39 -0400857void State::initializeZeroTextures(const Context *context, const TextureMap &zeroTextures)
Jamie Madille6382c32014-11-07 15:05:26 -0500858{
859 for (const auto &zeroTexture : zeroTextures)
860 {
861 auto &samplerTextureArray = mSamplerTextures[zeroTexture.first];
862
863 for (size_t textureUnit = 0; textureUnit < samplerTextureArray.size(); ++textureUnit)
864 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400865 samplerTextureArray[textureUnit].set(context, zeroTexture.second.get());
Jamie Madille6382c32014-11-07 15:05:26 -0500866 }
867 }
868}
869
Jamie Madill4928b7c2017-06-20 12:57:39 -0400870void State::setSamplerBinding(const Context *context, GLuint textureUnit, Sampler *sampler)
Shannon Woods53a94a82014-06-24 15:20:36 -0400871{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400872 mSamplers[textureUnit].set(context, sampler);
Jamie Madill81c2e252017-09-09 23:32:46 -0400873 mDirtyBits.set(DIRTY_BIT_SAMPLER_BINDINGS);
874 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
Shannon Woods53a94a82014-06-24 15:20:36 -0400875}
876
877GLuint State::getSamplerId(GLuint textureUnit) const
878{
Geoff Lang76b10c92014-09-05 16:28:14 -0400879 ASSERT(textureUnit < mSamplers.size());
Shannon Woods53a94a82014-06-24 15:20:36 -0400880 return mSamplers[textureUnit].id();
881}
882
883Sampler *State::getSampler(GLuint textureUnit) const
884{
885 return mSamplers[textureUnit].get();
886}
887
Jamie Madill4928b7c2017-06-20 12:57:39 -0400888void State::detachSampler(const Context *context, GLuint sampler)
Shannon Woods53a94a82014-06-24 15:20:36 -0400889{
890 // [OpenGL ES 3.0.2] section 3.8.2 pages 123-124:
891 // If a sampler object that is currently bound to one or more texture units is
892 // deleted, it is as though BindSampler is called once for each texture unit to
893 // which the sampler is bound, with unit set to the texture unit and sampler set to zero.
Jamie Madill81c2e252017-09-09 23:32:46 -0400894 for (BindingPointer<Sampler> &samplerBinding : mSamplers)
Shannon Woods53a94a82014-06-24 15:20:36 -0400895 {
Geoff Lang76b10c92014-09-05 16:28:14 -0400896 if (samplerBinding.id() == sampler)
Shannon Woods53a94a82014-06-24 15:20:36 -0400897 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400898 samplerBinding.set(context, nullptr);
Jamie Madill81c2e252017-09-09 23:32:46 -0400899 mDirtyBits.set(DIRTY_BIT_SAMPLER_BINDINGS);
Shannon Woods53a94a82014-06-24 15:20:36 -0400900 }
901 }
902}
903
Jamie Madill4928b7c2017-06-20 12:57:39 -0400904void State::setRenderbufferBinding(const Context *context, Renderbuffer *renderbuffer)
Shannon Woods53a94a82014-06-24 15:20:36 -0400905{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400906 mRenderbuffer.set(context, renderbuffer);
Jamie Madill8693bdb2017-09-02 15:32:14 -0400907 mDirtyBits.set(DIRTY_BIT_RENDERBUFFER_BINDING);
Shannon Woods53a94a82014-06-24 15:20:36 -0400908}
909
910GLuint State::getRenderbufferId() const
911{
912 return mRenderbuffer.id();
913}
914
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700915Renderbuffer *State::getCurrentRenderbuffer() const
Shannon Woods53a94a82014-06-24 15:20:36 -0400916{
917 return mRenderbuffer.get();
918}
919
Jamie Madilla02315b2017-02-23 14:14:47 -0500920void State::detachRenderbuffer(const Context *context, GLuint renderbuffer)
Shannon Woods53a94a82014-06-24 15:20:36 -0400921{
922 // [OpenGL ES 2.0.24] section 4.4 page 109:
923 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
924 // had been executed with the target RENDERBUFFER and name of zero.
925
926 if (mRenderbuffer.id() == renderbuffer)
927 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400928 setRenderbufferBinding(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400929 }
930
931 // [OpenGL ES 2.0.24] section 4.4 page 111:
932 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
933 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
934 // point to which this image was attached in the currently bound framebuffer.
935
936 Framebuffer *readFramebuffer = mReadFramebuffer;
937 Framebuffer *drawFramebuffer = mDrawFramebuffer;
938
Jamie Madill8693bdb2017-09-02 15:32:14 -0400939 if (readFramebuffer && readFramebuffer->detachRenderbuffer(context, renderbuffer))
Shannon Woods53a94a82014-06-24 15:20:36 -0400940 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400941 mDirtyObjects.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
Shannon Woods53a94a82014-06-24 15:20:36 -0400942 }
943
944 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
945 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400946 if (drawFramebuffer->detachRenderbuffer(context, renderbuffer))
947 {
948 mDirtyObjects.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
949 }
Shannon Woods53a94a82014-06-24 15:20:36 -0400950 }
951
952}
953
954void State::setReadFramebufferBinding(Framebuffer *framebuffer)
955{
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500956 if (mReadFramebuffer == framebuffer)
957 return;
958
Shannon Woods53a94a82014-06-24 15:20:36 -0400959 mReadFramebuffer = framebuffer;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500960 mDirtyBits.set(DIRTY_BIT_READ_FRAMEBUFFER_BINDING);
961
962 if (mReadFramebuffer && mReadFramebuffer->hasAnyDirtyBit())
963 {
964 mDirtyObjects.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
965 }
Shannon Woods53a94a82014-06-24 15:20:36 -0400966}
967
968void State::setDrawFramebufferBinding(Framebuffer *framebuffer)
969{
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500970 if (mDrawFramebuffer == framebuffer)
971 return;
972
Shannon Woods53a94a82014-06-24 15:20:36 -0400973 mDrawFramebuffer = framebuffer;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500974 mDirtyBits.set(DIRTY_BIT_DRAW_FRAMEBUFFER_BINDING);
975
976 if (mDrawFramebuffer && mDrawFramebuffer->hasAnyDirtyBit())
977 {
978 mDirtyObjects.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
979 }
Shannon Woods53a94a82014-06-24 15:20:36 -0400980}
981
982Framebuffer *State::getTargetFramebuffer(GLenum target) const
983{
984 switch (target)
985 {
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500986 case GL_READ_FRAMEBUFFER_ANGLE:
987 return mReadFramebuffer;
988 case GL_DRAW_FRAMEBUFFER_ANGLE:
989 case GL_FRAMEBUFFER:
990 return mDrawFramebuffer;
991 default:
992 UNREACHABLE();
Yunchao Hef81ce4a2017-04-24 10:49:17 +0800993 return nullptr;
Shannon Woods53a94a82014-06-24 15:20:36 -0400994 }
995}
996
Jamie Madill51f40ec2016-06-15 14:06:00 -0400997Framebuffer *State::getReadFramebuffer() const
Shannon Woods53a94a82014-06-24 15:20:36 -0400998{
999 return mReadFramebuffer;
1000}
1001
Jamie Madill51f40ec2016-06-15 14:06:00 -04001002Framebuffer *State::getDrawFramebuffer() const
Shannon Woods53a94a82014-06-24 15:20:36 -04001003{
1004 return mDrawFramebuffer;
1005}
1006
1007bool State::removeReadFramebufferBinding(GLuint framebuffer)
1008{
Jamie Madill77a72f62015-04-14 11:18:32 -04001009 if (mReadFramebuffer != nullptr &&
1010 mReadFramebuffer->id() == framebuffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001011 {
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001012 setReadFramebufferBinding(nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -04001013 return true;
1014 }
1015
1016 return false;
1017}
1018
1019bool State::removeDrawFramebufferBinding(GLuint framebuffer)
1020{
Jamie Madill77a72f62015-04-14 11:18:32 -04001021 if (mReadFramebuffer != nullptr &&
1022 mDrawFramebuffer->id() == framebuffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001023 {
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001024 setDrawFramebufferBinding(nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -04001025 return true;
1026 }
1027
1028 return false;
1029}
1030
1031void State::setVertexArrayBinding(VertexArray *vertexArray)
1032{
1033 mVertexArray = vertexArray;
Jamie Madill0b9e9032015-08-17 11:51:52 +00001034 mDirtyBits.set(DIRTY_BIT_VERTEX_ARRAY_BINDING);
Jamie Madillc9d442d2016-01-20 11:17:24 -05001035
1036 if (mVertexArray && mVertexArray->hasAnyDirtyBit())
1037 {
1038 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1039 }
Shannon Woods53a94a82014-06-24 15:20:36 -04001040}
1041
1042GLuint State::getVertexArrayId() const
1043{
Yunchao He4f285442017-04-21 12:15:49 +08001044 ASSERT(mVertexArray != nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -04001045 return mVertexArray->id();
1046}
1047
1048VertexArray *State::getVertexArray() const
1049{
Yunchao He4f285442017-04-21 12:15:49 +08001050 ASSERT(mVertexArray != nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -04001051 return mVertexArray;
1052}
1053
1054bool State::removeVertexArrayBinding(GLuint vertexArray)
1055{
1056 if (mVertexArray->id() == vertexArray)
1057 {
Yunchao Hed7297bf2017-04-19 15:27:10 +08001058 mVertexArray = nullptr;
Jamie Madill0b9e9032015-08-17 11:51:52 +00001059 mDirtyBits.set(DIRTY_BIT_VERTEX_ARRAY_BINDING);
Jamie Madillc9d442d2016-01-20 11:17:24 -05001060 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
Shannon Woods53a94a82014-06-24 15:20:36 -04001061 return true;
1062 }
1063
1064 return false;
1065}
1066
Jamie Madill4928b7c2017-06-20 12:57:39 -04001067void State::setElementArrayBuffer(const Context *context, Buffer *buffer)
Shao80957d92017-02-20 21:25:59 +08001068{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001069 getVertexArray()->setElementArrayBuffer(context, buffer);
Shao80957d92017-02-20 21:25:59 +08001070 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1071}
1072
Jamie Madill4928b7c2017-06-20 12:57:39 -04001073void State::bindVertexBuffer(const Context *context,
1074 GLuint bindingIndex,
Shao80957d92017-02-20 21:25:59 +08001075 Buffer *boundBuffer,
1076 GLintptr offset,
1077 GLsizei stride)
1078{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001079 getVertexArray()->bindVertexBuffer(context, bindingIndex, boundBuffer, offset, stride);
Shao80957d92017-02-20 21:25:59 +08001080 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1081}
1082
Shaodde78e82017-05-22 14:13:27 +08001083void State::setVertexAttribBinding(const Context *context, GLuint attribIndex, GLuint bindingIndex)
Shao80957d92017-02-20 21:25:59 +08001084{
Shaodde78e82017-05-22 14:13:27 +08001085 getVertexArray()->setVertexAttribBinding(context, attribIndex, bindingIndex);
Shao80957d92017-02-20 21:25:59 +08001086 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1087}
1088
1089void State::setVertexAttribFormat(GLuint attribIndex,
1090 GLint size,
1091 GLenum type,
1092 bool normalized,
1093 bool pureInteger,
1094 GLuint relativeOffset)
1095{
1096 getVertexArray()->setVertexAttribFormat(attribIndex, size, type, normalized, pureInteger,
1097 relativeOffset);
1098 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1099}
1100
1101void State::setVertexBindingDivisor(GLuint bindingIndex, GLuint divisor)
1102{
1103 getVertexArray()->setVertexBindingDivisor(bindingIndex, divisor);
1104 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1105}
1106
Jamie Madill6c1f6712017-02-14 19:08:04 -05001107void State::setProgram(const Context *context, Program *newProgram)
Shannon Woods53a94a82014-06-24 15:20:36 -04001108{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001109 if (mProgram != newProgram)
Shannon Woods53a94a82014-06-24 15:20:36 -04001110 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001111 if (mProgram)
1112 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001113 mProgram->release(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001114 }
1115
1116 mProgram = newProgram;
1117
1118 if (mProgram)
1119 {
1120 newProgram->addRef();
Jamie Madill81c2e252017-09-09 23:32:46 -04001121 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001122 }
Jamie Madilla779b612017-07-24 11:46:05 -04001123 mDirtyBits.set(DIRTY_BIT_PROGRAM_EXECUTABLE);
1124 mDirtyBits.set(DIRTY_BIT_PROGRAM_BINDING);
Shannon Woods53a94a82014-06-24 15:20:36 -04001125 }
1126}
1127
Geoff Lang7dd2e102014-11-10 15:19:26 -05001128Program *State::getProgram() const
Shannon Woods53a94a82014-06-24 15:20:36 -04001129{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001130 return mProgram;
Shannon Woods53a94a82014-06-24 15:20:36 -04001131}
1132
Jamie Madill4928b7c2017-06-20 12:57:39 -04001133void State::setTransformFeedbackBinding(const Context *context,
1134 TransformFeedback *transformFeedback)
Shannon Woods53a94a82014-06-24 15:20:36 -04001135{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001136 mTransformFeedback.set(context, transformFeedback);
Shannon Woods53a94a82014-06-24 15:20:36 -04001137}
1138
1139TransformFeedback *State::getCurrentTransformFeedback() const
1140{
1141 return mTransformFeedback.get();
1142}
1143
Gregoire Payen de La Garanderie52742022015-02-04 14:55:39 +00001144bool State::isTransformFeedbackActiveUnpaused() const
1145{
Jamie Madill81c2e252017-09-09 23:32:46 -04001146 TransformFeedback *curTransformFeedback = getCurrentTransformFeedback();
Geoff Langbb0a0bb2015-03-27 12:16:57 -04001147 return curTransformFeedback && curTransformFeedback->isActive() && !curTransformFeedback->isPaused();
Gregoire Payen de La Garanderie52742022015-02-04 14:55:39 +00001148}
1149
Jamie Madill4928b7c2017-06-20 12:57:39 -04001150bool State::removeTransformFeedbackBinding(const Context *context, GLuint transformFeedback)
Shannon Woods53a94a82014-06-24 15:20:36 -04001151{
1152 if (mTransformFeedback.id() == transformFeedback)
1153 {
Jamie Madill4928b7c2017-06-20 12:57:39 -04001154 mTransformFeedback.set(context, nullptr);
Corentin Walleza2257da2016-04-19 16:43:12 -04001155 return true;
Shannon Woods53a94a82014-06-24 15:20:36 -04001156 }
Corentin Walleza2257da2016-04-19 16:43:12 -04001157
1158 return false;
Shannon Woods53a94a82014-06-24 15:20:36 -04001159}
1160
Olli Etuahobbf1c102016-06-28 13:31:33 +03001161bool State::isQueryActive(const GLenum type) const
Shannon Woods53a94a82014-06-24 15:20:36 -04001162{
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001163 for (auto &iter : mActiveQueries)
Shannon Woods53a94a82014-06-24 15:20:36 -04001164 {
Olli Etuahobbf1c102016-06-28 13:31:33 +03001165 const Query *query = iter.second.get();
1166 if (query != nullptr && ActiveQueryType(query->getType()) == ActiveQueryType(type))
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001167 {
1168 return true;
1169 }
1170 }
1171
1172 return false;
1173}
1174
1175bool State::isQueryActive(Query *query) const
1176{
1177 for (auto &iter : mActiveQueries)
1178 {
1179 if (iter.second.get() == query)
Shannon Woods53a94a82014-06-24 15:20:36 -04001180 {
1181 return true;
1182 }
1183 }
1184
1185 return false;
1186}
1187
Jamie Madill4928b7c2017-06-20 12:57:39 -04001188void State::setActiveQuery(const Context *context, GLenum target, Query *query)
Shannon Woods53a94a82014-06-24 15:20:36 -04001189{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001190 mActiveQueries[target].set(context, query);
Shannon Woods53a94a82014-06-24 15:20:36 -04001191}
1192
1193GLuint State::getActiveQueryId(GLenum target) const
1194{
1195 const Query *query = getActiveQuery(target);
1196 return (query ? query->id() : 0u);
1197}
1198
1199Query *State::getActiveQuery(GLenum target) const
1200{
Jamie Madill5864ac22015-01-12 14:43:07 -05001201 const auto it = mActiveQueries.find(target);
Shannon Woods53a94a82014-06-24 15:20:36 -04001202
Jamie Madill5864ac22015-01-12 14:43:07 -05001203 // All query types should already exist in the activeQueries map
1204 ASSERT(it != mActiveQueries.end());
1205
1206 return it->second.get();
Shannon Woods53a94a82014-06-24 15:20:36 -04001207}
1208
Jamie Madill4928b7c2017-06-20 12:57:39 -04001209void State::setArrayBufferBinding(const Context *context, Buffer *buffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001210{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001211 mArrayBuffer.set(context, buffer);
Shannon Woods53a94a82014-06-24 15:20:36 -04001212}
1213
1214GLuint State::getArrayBufferId() const
1215{
1216 return mArrayBuffer.id();
1217}
1218
Jamie Madill4928b7c2017-06-20 12:57:39 -04001219void State::setDrawIndirectBufferBinding(const Context *context, Buffer *buffer)
Jiajia Qin9d7d0b12016-11-29 16:30:31 +08001220{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001221 mDrawIndirectBuffer.set(context, buffer);
Jiajia Qin9d7d0b12016-11-29 16:30:31 +08001222 mDirtyBits.set(DIRTY_BIT_DRAW_INDIRECT_BUFFER_BINDING);
1223}
1224
Jamie Madill4928b7c2017-06-20 12:57:39 -04001225void State::setGenericUniformBufferBinding(const Context *context, Buffer *buffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001226{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001227 mGenericUniformBuffer.set(context, buffer);
Shannon Woods53a94a82014-06-24 15:20:36 -04001228}
1229
Jamie Madill4928b7c2017-06-20 12:57:39 -04001230void State::setIndexedUniformBufferBinding(const Context *context,
1231 GLuint index,
1232 Buffer *buffer,
1233 GLintptr offset,
1234 GLsizeiptr size)
Shannon Woods53a94a82014-06-24 15:20:36 -04001235{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001236 mUniformBuffers[index].set(context, buffer, offset, size);
Shannon Woods53a94a82014-06-24 15:20:36 -04001237}
1238
Geoff Lang5d124a62015-09-15 13:03:27 -04001239const OffsetBindingPointer<Buffer> &State::getIndexedUniformBuffer(size_t index) const
Shannon Woods53a94a82014-06-24 15:20:36 -04001240{
Shannon Woodsf3acaf92014-09-23 18:07:11 -04001241 ASSERT(static_cast<size_t>(index) < mUniformBuffers.size());
Geoff Lang5d124a62015-09-15 13:03:27 -04001242 return mUniformBuffers[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001243}
1244
Jamie Madill4928b7c2017-06-20 12:57:39 -04001245void State::setGenericAtomicCounterBufferBinding(const Context *context, Buffer *buffer)
Jiajia Qin6eafb042016-12-27 17:04:07 +08001246{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001247 mGenericAtomicCounterBuffer.set(context, buffer);
Jiajia Qin6eafb042016-12-27 17:04:07 +08001248}
1249
Jamie Madill4928b7c2017-06-20 12:57:39 -04001250void State::setIndexedAtomicCounterBufferBinding(const Context *context,
1251 GLuint index,
Jiajia Qin6eafb042016-12-27 17:04:07 +08001252 Buffer *buffer,
1253 GLintptr offset,
1254 GLsizeiptr size)
1255{
1256 ASSERT(static_cast<size_t>(index) < mAtomicCounterBuffers.size());
Jamie Madill4928b7c2017-06-20 12:57:39 -04001257 mAtomicCounterBuffers[index].set(context, buffer, offset, size);
Jiajia Qin6eafb042016-12-27 17:04:07 +08001258}
1259
1260const OffsetBindingPointer<Buffer> &State::getIndexedAtomicCounterBuffer(size_t index) const
1261{
1262 ASSERT(static_cast<size_t>(index) < mAtomicCounterBuffers.size());
1263 return mAtomicCounterBuffers[index];
1264}
1265
Jamie Madill4928b7c2017-06-20 12:57:39 -04001266void State::setGenericShaderStorageBufferBinding(const Context *context, Buffer *buffer)
Jiajia Qinf546e7d2017-03-27 14:12:59 +08001267{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001268 mGenericShaderStorageBuffer.set(context, buffer);
Jiajia Qinf546e7d2017-03-27 14:12:59 +08001269}
1270
Jamie Madill4928b7c2017-06-20 12:57:39 -04001271void State::setIndexedShaderStorageBufferBinding(const Context *context,
1272 GLuint index,
Jiajia Qinf546e7d2017-03-27 14:12:59 +08001273 Buffer *buffer,
1274 GLintptr offset,
1275 GLsizeiptr size)
1276{
1277 ASSERT(static_cast<size_t>(index) < mShaderStorageBuffers.size());
Jamie Madill4928b7c2017-06-20 12:57:39 -04001278 mShaderStorageBuffers[index].set(context, buffer, offset, size);
Jiajia Qinf546e7d2017-03-27 14:12:59 +08001279}
1280
1281const OffsetBindingPointer<Buffer> &State::getIndexedShaderStorageBuffer(size_t index) const
1282{
1283 ASSERT(static_cast<size_t>(index) < mShaderStorageBuffers.size());
1284 return mShaderStorageBuffers[index];
1285}
1286
Jamie Madill4928b7c2017-06-20 12:57:39 -04001287void State::setCopyReadBufferBinding(const Context *context, Buffer *buffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001288{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001289 mCopyReadBuffer.set(context, buffer);
Shannon Woods53a94a82014-06-24 15:20:36 -04001290}
1291
Jamie Madill4928b7c2017-06-20 12:57:39 -04001292void State::setCopyWriteBufferBinding(const Context *context, Buffer *buffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001293{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001294 mCopyWriteBuffer.set(context, buffer);
Shannon Woods53a94a82014-06-24 15:20:36 -04001295}
1296
Jamie Madill4928b7c2017-06-20 12:57:39 -04001297void State::setPixelPackBufferBinding(const Context *context, Buffer *buffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001298{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001299 mPack.pixelBuffer.set(context, buffer);
Corentin Wallezbbd663a2016-04-20 17:49:17 -04001300 mDirtyBits.set(DIRTY_BIT_PACK_BUFFER_BINDING);
Shannon Woods53a94a82014-06-24 15:20:36 -04001301}
1302
Jamie Madill4928b7c2017-06-20 12:57:39 -04001303void State::setPixelUnpackBufferBinding(const Context *context, Buffer *buffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001304{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001305 mUnpack.pixelBuffer.set(context, buffer);
Corentin Wallezbbd663a2016-04-20 17:49:17 -04001306 mDirtyBits.set(DIRTY_BIT_UNPACK_BUFFER_BINDING);
Shannon Woods53a94a82014-06-24 15:20:36 -04001307}
1308
1309Buffer *State::getTargetBuffer(GLenum target) const
1310{
1311 switch (target)
1312 {
1313 case GL_ARRAY_BUFFER: return mArrayBuffer.get();
1314 case GL_COPY_READ_BUFFER: return mCopyReadBuffer.get();
1315 case GL_COPY_WRITE_BUFFER: return mCopyWriteBuffer.get();
Jamie Madill8e344942015-07-09 14:22:07 -04001316 case GL_ELEMENT_ARRAY_BUFFER: return getVertexArray()->getElementArrayBuffer().get();
Shannon Woods53a94a82014-06-24 15:20:36 -04001317 case GL_PIXEL_PACK_BUFFER: return mPack.pixelBuffer.get();
1318 case GL_PIXEL_UNPACK_BUFFER: return mUnpack.pixelBuffer.get();
Geoff Lang045536b2015-03-27 15:17:18 -04001319 case GL_TRANSFORM_FEEDBACK_BUFFER: return mTransformFeedback->getGenericBuffer().get();
Shannon Woods53a94a82014-06-24 15:20:36 -04001320 case GL_UNIFORM_BUFFER: return mGenericUniformBuffer.get();
Geoff Langb5e997f2016-12-06 10:55:34 -05001321 case GL_ATOMIC_COUNTER_BUFFER:
Jiajia Qin6eafb042016-12-27 17:04:07 +08001322 return mGenericAtomicCounterBuffer.get();
Geoff Langb5e997f2016-12-06 10:55:34 -05001323 case GL_SHADER_STORAGE_BUFFER:
Jiajia Qinf546e7d2017-03-27 14:12:59 +08001324 return mGenericShaderStorageBuffer.get();
Geoff Langb5e997f2016-12-06 10:55:34 -05001325 case GL_DRAW_INDIRECT_BUFFER:
Jiajia Qin9d7d0b12016-11-29 16:30:31 +08001326 return mDrawIndirectBuffer.get();
Yunchao Hef81ce4a2017-04-24 10:49:17 +08001327 default:
1328 UNREACHABLE();
1329 return nullptr;
Shannon Woods53a94a82014-06-24 15:20:36 -04001330 }
1331}
1332
Jamie Madill4928b7c2017-06-20 12:57:39 -04001333void State::detachBuffer(const Context *context, GLuint bufferName)
Yuly Novikov5807a532015-12-03 13:01:22 -05001334{
Jiajia Qinf546e7d2017-03-27 14:12:59 +08001335 BindingPointer<Buffer> *buffers[] = {
1336 &mArrayBuffer, &mGenericAtomicCounterBuffer, &mCopyReadBuffer,
1337 &mCopyWriteBuffer, &mDrawIndirectBuffer, &mPack.pixelBuffer,
1338 &mUnpack.pixelBuffer, &mGenericUniformBuffer, &mGenericShaderStorageBuffer};
Yuly Novikov5807a532015-12-03 13:01:22 -05001339 for (auto buffer : buffers)
1340 {
1341 if (buffer->id() == bufferName)
1342 {
Jamie Madill4928b7c2017-06-20 12:57:39 -04001343 buffer->set(context, nullptr);
Yuly Novikov5807a532015-12-03 13:01:22 -05001344 }
1345 }
1346
1347 TransformFeedback *curTransformFeedback = getCurrentTransformFeedback();
1348 if (curTransformFeedback)
1349 {
Jamie Madill4928b7c2017-06-20 12:57:39 -04001350 curTransformFeedback->detachBuffer(context, bufferName);
Yuly Novikov5807a532015-12-03 13:01:22 -05001351 }
1352
Jamie Madill4928b7c2017-06-20 12:57:39 -04001353 getVertexArray()->detachBuffer(context, bufferName);
Yuly Novikov5807a532015-12-03 13:01:22 -05001354}
1355
Shannon Woods53a94a82014-06-24 15:20:36 -04001356void State::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
1357{
1358 getVertexArray()->enableAttribute(attribNum, enabled);
Jamie Madillc9d442d2016-01-20 11:17:24 -05001359 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
Shannon Woods53a94a82014-06-24 15:20:36 -04001360}
1361
1362void State::setVertexAttribf(GLuint index, const GLfloat values[4])
1363{
Shannon Woods23e05002014-09-22 19:07:27 -04001364 ASSERT(static_cast<size_t>(index) < mVertexAttribCurrentValues.size());
Shannon Woods53a94a82014-06-24 15:20:36 -04001365 mVertexAttribCurrentValues[index].setFloatValues(values);
Jamie Madill1e0bc3a2015-08-11 08:12:21 -04001366 mDirtyBits.set(DIRTY_BIT_CURRENT_VALUE_0 + index);
Shannon Woods53a94a82014-06-24 15:20:36 -04001367}
1368
1369void State::setVertexAttribu(GLuint index, const GLuint values[4])
1370{
Shannon Woods23e05002014-09-22 19:07:27 -04001371 ASSERT(static_cast<size_t>(index) < mVertexAttribCurrentValues.size());
Shannon Woods53a94a82014-06-24 15:20:36 -04001372 mVertexAttribCurrentValues[index].setUnsignedIntValues(values);
Jamie Madill1e0bc3a2015-08-11 08:12:21 -04001373 mDirtyBits.set(DIRTY_BIT_CURRENT_VALUE_0 + index);
Shannon Woods53a94a82014-06-24 15:20:36 -04001374}
1375
1376void State::setVertexAttribi(GLuint index, const GLint values[4])
1377{
Shannon Woods23e05002014-09-22 19:07:27 -04001378 ASSERT(static_cast<size_t>(index) < mVertexAttribCurrentValues.size());
Shannon Woods53a94a82014-06-24 15:20:36 -04001379 mVertexAttribCurrentValues[index].setIntValues(values);
Jamie Madill1e0bc3a2015-08-11 08:12:21 -04001380 mDirtyBits.set(DIRTY_BIT_CURRENT_VALUE_0 + index);
Shannon Woods53a94a82014-06-24 15:20:36 -04001381}
1382
Shaodde78e82017-05-22 14:13:27 +08001383void State::setVertexAttribPointer(const Context *context,
1384 unsigned int attribNum,
1385 Buffer *boundBuffer,
1386 GLint size,
1387 GLenum type,
1388 bool normalized,
1389 bool pureInteger,
1390 GLsizei stride,
1391 const void *pointer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001392{
Shaodde78e82017-05-22 14:13:27 +08001393 getVertexArray()->setVertexAttribPointer(context, attribNum, boundBuffer, size, type,
1394 normalized, pureInteger, stride, pointer);
Jamie Madillc9d442d2016-01-20 11:17:24 -05001395 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
Jamie Madill0b9e9032015-08-17 11:51:52 +00001396}
1397
Shaodde78e82017-05-22 14:13:27 +08001398void State::setVertexAttribDivisor(const Context *context, GLuint index, GLuint divisor)
Jamie Madill0b9e9032015-08-17 11:51:52 +00001399{
Shaodde78e82017-05-22 14:13:27 +08001400 getVertexArray()->setVertexAttribDivisor(context, index, divisor);
Jamie Madillc9d442d2016-01-20 11:17:24 -05001401 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
Shannon Woods53a94a82014-06-24 15:20:36 -04001402}
1403
Jamie Madill6de51852017-04-12 09:53:01 -04001404const VertexAttribCurrentValueData &State::getVertexAttribCurrentValue(size_t attribNum) const
Shannon Woods53a94a82014-06-24 15:20:36 -04001405{
Jamie Madill6de51852017-04-12 09:53:01 -04001406 ASSERT(attribNum < mVertexAttribCurrentValues.size());
Shannon Woods53a94a82014-06-24 15:20:36 -04001407 return mVertexAttribCurrentValues[attribNum];
1408}
1409
Shannon Woods53a94a82014-06-24 15:20:36 -04001410const void *State::getVertexAttribPointer(unsigned int attribNum) const
1411{
1412 return getVertexArray()->getVertexAttribute(attribNum).pointer;
1413}
1414
1415void State::setPackAlignment(GLint alignment)
1416{
1417 mPack.alignment = alignment;
Jamie Madill1b94d432015-08-07 13:23:23 -04001418 mDirtyBits.set(DIRTY_BIT_PACK_ALIGNMENT);
Shannon Woods53a94a82014-06-24 15:20:36 -04001419}
1420
1421GLint State::getPackAlignment() const
1422{
1423 return mPack.alignment;
1424}
1425
1426void State::setPackReverseRowOrder(bool reverseRowOrder)
1427{
1428 mPack.reverseRowOrder = reverseRowOrder;
Jamie Madill1b94d432015-08-07 13:23:23 -04001429 mDirtyBits.set(DIRTY_BIT_PACK_REVERSE_ROW_ORDER);
Shannon Woods53a94a82014-06-24 15:20:36 -04001430}
1431
1432bool State::getPackReverseRowOrder() const
1433{
1434 return mPack.reverseRowOrder;
1435}
1436
Minmin Gongadff67b2015-10-14 10:34:45 -04001437void State::setPackRowLength(GLint rowLength)
1438{
1439 mPack.rowLength = rowLength;
1440 mDirtyBits.set(DIRTY_BIT_PACK_ROW_LENGTH);
1441}
1442
1443GLint State::getPackRowLength() const
1444{
1445 return mPack.rowLength;
1446}
1447
1448void State::setPackSkipRows(GLint skipRows)
1449{
1450 mPack.skipRows = skipRows;
1451 mDirtyBits.set(DIRTY_BIT_PACK_SKIP_ROWS);
1452}
1453
1454GLint State::getPackSkipRows() const
1455{
1456 return mPack.skipRows;
1457}
1458
1459void State::setPackSkipPixels(GLint skipPixels)
1460{
1461 mPack.skipPixels = skipPixels;
1462 mDirtyBits.set(DIRTY_BIT_PACK_SKIP_PIXELS);
1463}
1464
1465GLint State::getPackSkipPixels() const
1466{
1467 return mPack.skipPixels;
1468}
1469
Shannon Woods53a94a82014-06-24 15:20:36 -04001470const PixelPackState &State::getPackState() const
1471{
1472 return mPack;
1473}
1474
Jamie Madill87de3622015-03-16 10:41:44 -04001475PixelPackState &State::getPackState()
1476{
1477 return mPack;
1478}
1479
Shannon Woods53a94a82014-06-24 15:20:36 -04001480void State::setUnpackAlignment(GLint alignment)
1481{
1482 mUnpack.alignment = alignment;
Jamie Madill1b94d432015-08-07 13:23:23 -04001483 mDirtyBits.set(DIRTY_BIT_UNPACK_ALIGNMENT);
Shannon Woods53a94a82014-06-24 15:20:36 -04001484}
1485
1486GLint State::getUnpackAlignment() const
1487{
1488 return mUnpack.alignment;
1489}
1490
Minmin Gongb8aee3b2015-01-27 14:42:36 -08001491void State::setUnpackRowLength(GLint rowLength)
1492{
1493 mUnpack.rowLength = rowLength;
Jamie Madill1b94d432015-08-07 13:23:23 -04001494 mDirtyBits.set(DIRTY_BIT_UNPACK_ROW_LENGTH);
Minmin Gongb8aee3b2015-01-27 14:42:36 -08001495}
1496
1497GLint State::getUnpackRowLength() const
1498{
1499 return mUnpack.rowLength;
1500}
1501
Minmin Gongadff67b2015-10-14 10:34:45 -04001502void State::setUnpackImageHeight(GLint imageHeight)
1503{
1504 mUnpack.imageHeight = imageHeight;
1505 mDirtyBits.set(DIRTY_BIT_UNPACK_IMAGE_HEIGHT);
1506}
1507
1508GLint State::getUnpackImageHeight() const
1509{
1510 return mUnpack.imageHeight;
1511}
1512
1513void State::setUnpackSkipImages(GLint skipImages)
1514{
1515 mUnpack.skipImages = skipImages;
1516 mDirtyBits.set(DIRTY_BIT_UNPACK_SKIP_IMAGES);
1517}
1518
1519GLint State::getUnpackSkipImages() const
1520{
1521 return mUnpack.skipImages;
1522}
1523
1524void State::setUnpackSkipRows(GLint skipRows)
1525{
1526 mUnpack.skipRows = skipRows;
1527 mDirtyBits.set(DIRTY_BIT_UNPACK_SKIP_ROWS);
1528}
1529
1530GLint State::getUnpackSkipRows() const
1531{
1532 return mUnpack.skipRows;
1533}
1534
1535void State::setUnpackSkipPixels(GLint skipPixels)
1536{
1537 mUnpack.skipPixels = skipPixels;
1538 mDirtyBits.set(DIRTY_BIT_UNPACK_SKIP_PIXELS);
1539}
1540
1541GLint State::getUnpackSkipPixels() const
1542{
1543 return mUnpack.skipPixels;
1544}
1545
Shannon Woods53a94a82014-06-24 15:20:36 -04001546const PixelUnpackState &State::getUnpackState() const
1547{
1548 return mUnpack;
1549}
1550
Jamie Madill67102f02015-03-16 10:41:42 -04001551PixelUnpackState &State::getUnpackState()
1552{
1553 return mUnpack;
1554}
1555
Geoff Lang70d0f492015-12-10 17:45:46 -05001556const Debug &State::getDebug() const
1557{
1558 return mDebug;
1559}
1560
1561Debug &State::getDebug()
1562{
1563 return mDebug;
1564}
1565
Sami Väisänena797e062016-05-12 15:23:40 +03001566void State::setCoverageModulation(GLenum components)
1567{
1568 mCoverageModulation = components;
1569 mDirtyBits.set(DIRTY_BIT_COVERAGE_MODULATION);
1570}
1571
1572GLenum State::getCoverageModulation() const
1573{
1574 return mCoverageModulation;
1575}
1576
Sami Väisänene45e53b2016-05-25 10:36:04 +03001577void State::loadPathRenderingMatrix(GLenum matrixMode, const GLfloat *matrix)
1578{
1579 if (matrixMode == GL_PATH_MODELVIEW_CHROMIUM)
1580 {
1581 memcpy(mPathMatrixMV, matrix, 16 * sizeof(GLfloat));
1582 mDirtyBits.set(DIRTY_BIT_PATH_RENDERING_MATRIX_MV);
1583 }
1584 else if (matrixMode == GL_PATH_PROJECTION_CHROMIUM)
1585 {
1586 memcpy(mPathMatrixProj, matrix, 16 * sizeof(GLfloat));
1587 mDirtyBits.set(DIRTY_BIT_PATH_RENDERING_MATRIX_PROJ);
1588 }
1589 else
1590 {
1591 UNREACHABLE();
1592 }
1593}
1594
1595const GLfloat *State::getPathRenderingMatrix(GLenum which) const
1596{
1597 if (which == GL_PATH_MODELVIEW_MATRIX_CHROMIUM)
1598 {
1599 return mPathMatrixMV;
1600 }
1601 else if (which == GL_PATH_PROJECTION_MATRIX_CHROMIUM)
1602 {
1603 return mPathMatrixProj;
1604 }
1605
1606 UNREACHABLE();
1607 return nullptr;
1608}
1609
1610void State::setPathStencilFunc(GLenum func, GLint ref, GLuint mask)
1611{
1612 mPathStencilFunc = func;
1613 mPathStencilRef = ref;
1614 mPathStencilMask = mask;
1615 mDirtyBits.set(DIRTY_BIT_PATH_RENDERING_STENCIL_STATE);
1616}
1617
1618GLenum State::getPathStencilFunc() const
1619{
1620 return mPathStencilFunc;
1621}
1622
1623GLint State::getPathStencilRef() const
1624{
1625 return mPathStencilRef;
1626}
1627
1628GLuint State::getPathStencilMask() const
1629{
1630 return mPathStencilMask;
1631}
1632
Geoff Lang1d2c41d2016-10-19 16:14:46 -07001633void State::setFramebufferSRGB(bool sRGB)
1634{
1635 mFramebufferSRGB = sRGB;
1636 mDirtyBits.set(DIRTY_BIT_FRAMEBUFFER_SRGB);
1637}
1638
1639bool State::getFramebufferSRGB() const
1640{
1641 return mFramebufferSRGB;
1642}
1643
Shannon Woods53a94a82014-06-24 15:20:36 -04001644void State::getBooleanv(GLenum pname, GLboolean *params)
1645{
1646 switch (pname)
1647 {
1648 case GL_SAMPLE_COVERAGE_INVERT: *params = mSampleCoverageInvert; break;
1649 case GL_DEPTH_WRITEMASK: *params = mDepthStencil.depthMask; break;
1650 case GL_COLOR_WRITEMASK:
1651 params[0] = mBlend.colorMaskRed;
1652 params[1] = mBlend.colorMaskGreen;
1653 params[2] = mBlend.colorMaskBlue;
1654 params[3] = mBlend.colorMaskAlpha;
1655 break;
1656 case GL_CULL_FACE: *params = mRasterizer.cullFace; break;
1657 case GL_POLYGON_OFFSET_FILL: *params = mRasterizer.polygonOffsetFill; break;
1658 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mBlend.sampleAlphaToCoverage; break;
1659 case GL_SAMPLE_COVERAGE: *params = mSampleCoverage; break;
1660 case GL_SCISSOR_TEST: *params = mScissorTest; break;
1661 case GL_STENCIL_TEST: *params = mDepthStencil.stencilTest; break;
1662 case GL_DEPTH_TEST: *params = mDepthStencil.depthTest; break;
1663 case GL_BLEND: *params = mBlend.blend; break;
1664 case GL_DITHER: *params = mBlend.dither; break;
Geoff Langbb0a0bb2015-03-27 12:16:57 -04001665 case GL_TRANSFORM_FEEDBACK_ACTIVE: *params = getCurrentTransformFeedback()->isActive() ? GL_TRUE : GL_FALSE; break;
1666 case GL_TRANSFORM_FEEDBACK_PAUSED: *params = getCurrentTransformFeedback()->isPaused() ? GL_TRUE : GL_FALSE; break;
Jamie Madille2cd53d2015-10-27 11:15:46 -04001667 case GL_PRIMITIVE_RESTART_FIXED_INDEX:
1668 *params = mPrimitiveRestart;
1669 break;
Geoff Langab831f02015-12-01 09:39:10 -05001670 case GL_RASTERIZER_DISCARD:
1671 *params = isRasterizerDiscardEnabled() ? GL_TRUE : GL_FALSE;
1672 break;
Geoff Lang70d0f492015-12-10 17:45:46 -05001673 case GL_DEBUG_OUTPUT_SYNCHRONOUS:
1674 *params = mDebug.isOutputSynchronous() ? GL_TRUE : GL_FALSE;
1675 break;
1676 case GL_DEBUG_OUTPUT:
1677 *params = mDebug.isOutputEnabled() ? GL_TRUE : GL_FALSE;
1678 break;
Sami Väisänen74c23472016-05-09 17:30:30 +03001679 case GL_MULTISAMPLE_EXT:
1680 *params = mMultiSampling;
1681 break;
1682 case GL_SAMPLE_ALPHA_TO_ONE_EXT:
1683 *params = mSampleAlphaToOne;
1684 break;
Geoff Langf41a7152016-09-19 15:11:17 -04001685 case GL_BIND_GENERATES_RESOURCE_CHROMIUM:
1686 *params = isBindGeneratesResourceEnabled() ? GL_TRUE : GL_FALSE;
1687 break;
Geoff Langfeb8c682017-02-13 16:07:35 -05001688 case GL_CLIENT_ARRAYS_ANGLE:
1689 *params = areClientArraysEnabled() ? GL_TRUE : GL_FALSE;
1690 break;
Geoff Lang1d2c41d2016-10-19 16:14:46 -07001691 case GL_FRAMEBUFFER_SRGB_EXT:
1692 *params = getFramebufferSRGB() ? GL_TRUE : GL_FALSE;
1693 break;
Jamie Madille08a1d32017-03-07 17:24:06 -05001694 case GL_CONTEXT_ROBUST_RESOURCE_INITIALIZATION_ANGLE:
1695 *params = mRobustResourceInit ? GL_TRUE : GL_FALSE;
1696 break;
Jamie Madillc43be722017-07-13 16:22:14 -04001697 case GL_PROGRAM_CACHE_ENABLED_ANGLE:
1698 *params = mProgramBinaryCacheEnabled ? GL_TRUE : GL_FALSE;
1699 break;
1700
Shannon Woods53a94a82014-06-24 15:20:36 -04001701 default:
1702 UNREACHABLE();
1703 break;
1704 }
1705}
1706
1707void State::getFloatv(GLenum pname, GLfloat *params)
1708{
1709 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1710 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1711 // GetIntegerv as its native query function. As it would require conversion in any
1712 // case, this should make no difference to the calling application.
1713 switch (pname)
1714 {
1715 case GL_LINE_WIDTH: *params = mLineWidth; break;
1716 case GL_SAMPLE_COVERAGE_VALUE: *params = mSampleCoverageValue; break;
1717 case GL_DEPTH_CLEAR_VALUE: *params = mDepthClearValue; break;
1718 case GL_POLYGON_OFFSET_FACTOR: *params = mRasterizer.polygonOffsetFactor; break;
1719 case GL_POLYGON_OFFSET_UNITS: *params = mRasterizer.polygonOffsetUnits; break;
1720 case GL_DEPTH_RANGE:
1721 params[0] = mNearZ;
1722 params[1] = mFarZ;
1723 break;
1724 case GL_COLOR_CLEAR_VALUE:
1725 params[0] = mColorClearValue.red;
1726 params[1] = mColorClearValue.green;
1727 params[2] = mColorClearValue.blue;
1728 params[3] = mColorClearValue.alpha;
1729 break;
1730 case GL_BLEND_COLOR:
1731 params[0] = mBlendColor.red;
1732 params[1] = mBlendColor.green;
1733 params[2] = mBlendColor.blue;
1734 params[3] = mBlendColor.alpha;
1735 break;
Sami Väisänen74c23472016-05-09 17:30:30 +03001736 case GL_MULTISAMPLE_EXT:
1737 *params = static_cast<GLfloat>(mMultiSampling);
1738 break;
1739 case GL_SAMPLE_ALPHA_TO_ONE_EXT:
1740 *params = static_cast<GLfloat>(mSampleAlphaToOne);
Sami Väisänena797e062016-05-12 15:23:40 +03001741 case GL_COVERAGE_MODULATION_CHROMIUM:
Jamie Madille2e406c2016-06-02 13:04:10 -04001742 params[0] = static_cast<GLfloat>(mCoverageModulation);
1743 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001744 default:
1745 UNREACHABLE();
1746 break;
1747 }
1748}
1749
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001750void State::getIntegerv(const Context *context, GLenum pname, GLint *params)
Shannon Woods53a94a82014-06-24 15:20:36 -04001751{
1752 if (pname >= GL_DRAW_BUFFER0_EXT && pname <= GL_DRAW_BUFFER15_EXT)
1753 {
1754 unsigned int colorAttachment = (pname - GL_DRAW_BUFFER0_EXT);
Shannon Woods2df6a602014-09-26 16:12:07 -04001755 ASSERT(colorAttachment < mMaxDrawBuffers);
Shannon Woods53a94a82014-06-24 15:20:36 -04001756 Framebuffer *framebuffer = mDrawFramebuffer;
1757 *params = framebuffer->getDrawBufferState(colorAttachment);
1758 return;
1759 }
1760
1761 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1762 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1763 // GetIntegerv as its native query function. As it would require conversion in any
1764 // case, this should make no difference to the calling application. You may find it in
1765 // State::getFloatv.
1766 switch (pname)
1767 {
1768 case GL_ARRAY_BUFFER_BINDING: *params = mArrayBuffer.id(); break;
Jiajia Qin9d7d0b12016-11-29 16:30:31 +08001769 case GL_DRAW_INDIRECT_BUFFER_BINDING:
1770 *params = mDrawIndirectBuffer.id();
1771 break;
Jamie Madill8e344942015-07-09 14:22:07 -04001772 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = getVertexArray()->getElementArrayBuffer().id(); break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001773 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1774 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mDrawFramebuffer->id(); break;
1775 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mReadFramebuffer->id(); break;
1776 case GL_RENDERBUFFER_BINDING: *params = mRenderbuffer.id(); break;
1777 case GL_VERTEX_ARRAY_BINDING: *params = mVertexArray->id(); break;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001778 case GL_CURRENT_PROGRAM: *params = mProgram ? mProgram->id() : 0; break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001779 case GL_PACK_ALIGNMENT: *params = mPack.alignment; break;
1780 case GL_PACK_REVERSE_ROW_ORDER_ANGLE: *params = mPack.reverseRowOrder; break;
Minmin Gongadff67b2015-10-14 10:34:45 -04001781 case GL_PACK_ROW_LENGTH:
1782 *params = mPack.rowLength;
1783 break;
1784 case GL_PACK_SKIP_ROWS:
1785 *params = mPack.skipRows;
1786 break;
1787 case GL_PACK_SKIP_PIXELS:
1788 *params = mPack.skipPixels;
1789 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001790 case GL_UNPACK_ALIGNMENT: *params = mUnpack.alignment; break;
Minmin Gongb8aee3b2015-01-27 14:42:36 -08001791 case GL_UNPACK_ROW_LENGTH: *params = mUnpack.rowLength; break;
Minmin Gongadff67b2015-10-14 10:34:45 -04001792 case GL_UNPACK_IMAGE_HEIGHT:
1793 *params = mUnpack.imageHeight;
1794 break;
1795 case GL_UNPACK_SKIP_IMAGES:
1796 *params = mUnpack.skipImages;
1797 break;
1798 case GL_UNPACK_SKIP_ROWS:
1799 *params = mUnpack.skipRows;
1800 break;
1801 case GL_UNPACK_SKIP_PIXELS:
1802 *params = mUnpack.skipPixels;
1803 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001804 case GL_GENERATE_MIPMAP_HINT: *params = mGenerateMipmapHint; break;
1805 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mFragmentShaderDerivativeHint; break;
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001806 case GL_ACTIVE_TEXTURE:
1807 *params = (static_cast<GLint>(mActiveSampler) + GL_TEXTURE0);
1808 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001809 case GL_STENCIL_FUNC: *params = mDepthStencil.stencilFunc; break;
1810 case GL_STENCIL_REF: *params = mStencilRef; break;
1811 case GL_STENCIL_VALUE_MASK: *params = clampToInt(mDepthStencil.stencilMask); break;
1812 case GL_STENCIL_BACK_FUNC: *params = mDepthStencil.stencilBackFunc; break;
1813 case GL_STENCIL_BACK_REF: *params = mStencilBackRef; break;
1814 case GL_STENCIL_BACK_VALUE_MASK: *params = clampToInt(mDepthStencil.stencilBackMask); break;
1815 case GL_STENCIL_FAIL: *params = mDepthStencil.stencilFail; break;
1816 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mDepthStencil.stencilPassDepthFail; break;
1817 case GL_STENCIL_PASS_DEPTH_PASS: *params = mDepthStencil.stencilPassDepthPass; break;
1818 case GL_STENCIL_BACK_FAIL: *params = mDepthStencil.stencilBackFail; break;
1819 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mDepthStencil.stencilBackPassDepthFail; break;
1820 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mDepthStencil.stencilBackPassDepthPass; break;
1821 case GL_DEPTH_FUNC: *params = mDepthStencil.depthFunc; break;
1822 case GL_BLEND_SRC_RGB: *params = mBlend.sourceBlendRGB; break;
1823 case GL_BLEND_SRC_ALPHA: *params = mBlend.sourceBlendAlpha; break;
1824 case GL_BLEND_DST_RGB: *params = mBlend.destBlendRGB; break;
1825 case GL_BLEND_DST_ALPHA: *params = mBlend.destBlendAlpha; break;
1826 case GL_BLEND_EQUATION_RGB: *params = mBlend.blendEquationRGB; break;
1827 case GL_BLEND_EQUATION_ALPHA: *params = mBlend.blendEquationAlpha; break;
1828 case GL_STENCIL_WRITEMASK: *params = clampToInt(mDepthStencil.stencilWritemask); break;
1829 case GL_STENCIL_BACK_WRITEMASK: *params = clampToInt(mDepthStencil.stencilBackWritemask); break;
1830 case GL_STENCIL_CLEAR_VALUE: *params = mStencilClearValue; break;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001831 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1832 *params = mReadFramebuffer->getImplementationColorReadType(context);
1833 break;
1834 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1835 *params = mReadFramebuffer->getImplementationColorReadFormat(context);
1836 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001837 case GL_SAMPLE_BUFFERS:
1838 case GL_SAMPLES:
1839 {
Jamie Madill81c2e252017-09-09 23:32:46 -04001840 Framebuffer *framebuffer = mDrawFramebuffer;
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001841 if (framebuffer->checkStatus(context) == GL_FRAMEBUFFER_COMPLETE)
Shannon Woods53a94a82014-06-24 15:20:36 -04001842 {
1843 switch (pname)
1844 {
Jamie Madilla0016b72017-07-14 14:30:46 -04001845 case GL_SAMPLE_BUFFERS:
1846 if (framebuffer->getSamples(context) != 0)
1847 {
1848 *params = 1;
1849 }
1850 else
1851 {
1852 *params = 0;
1853 }
1854 break;
1855 case GL_SAMPLES:
1856 *params = framebuffer->getSamples(context);
1857 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001858 }
1859 }
1860 else
1861 {
1862 *params = 0;
1863 }
1864 }
1865 break;
1866 case GL_VIEWPORT:
1867 params[0] = mViewport.x;
1868 params[1] = mViewport.y;
1869 params[2] = mViewport.width;
1870 params[3] = mViewport.height;
1871 break;
1872 case GL_SCISSOR_BOX:
1873 params[0] = mScissor.x;
1874 params[1] = mScissor.y;
1875 params[2] = mScissor.width;
1876 params[3] = mScissor.height;
1877 break;
1878 case GL_CULL_FACE_MODE: *params = mRasterizer.cullMode; break;
1879 case GL_FRONT_FACE: *params = mRasterizer.frontFace; break;
1880 case GL_RED_BITS:
1881 case GL_GREEN_BITS:
1882 case GL_BLUE_BITS:
1883 case GL_ALPHA_BITS:
1884 {
Jamie Madill81c2e252017-09-09 23:32:46 -04001885 Framebuffer *framebuffer = getDrawFramebuffer();
1886 const FramebufferAttachment *colorbuffer = framebuffer->getFirstColorbuffer();
Shannon Woods53a94a82014-06-24 15:20:36 -04001887
1888 if (colorbuffer)
1889 {
1890 switch (pname)
1891 {
1892 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1893 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1894 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1895 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1896 }
1897 }
1898 else
1899 {
1900 *params = 0;
1901 }
1902 }
1903 break;
1904 case GL_DEPTH_BITS:
1905 {
Jamie Madill81c2e252017-09-09 23:32:46 -04001906 const Framebuffer *framebuffer = getDrawFramebuffer();
1907 const FramebufferAttachment *depthbuffer = framebuffer->getDepthbuffer();
Shannon Woods53a94a82014-06-24 15:20:36 -04001908
1909 if (depthbuffer)
1910 {
1911 *params = depthbuffer->getDepthSize();
1912 }
1913 else
1914 {
1915 *params = 0;
1916 }
1917 }
1918 break;
1919 case GL_STENCIL_BITS:
1920 {
Jamie Madill81c2e252017-09-09 23:32:46 -04001921 const Framebuffer *framebuffer = getDrawFramebuffer();
1922 const FramebufferAttachment *stencilbuffer = framebuffer->getStencilbuffer();
Shannon Woods53a94a82014-06-24 15:20:36 -04001923
1924 if (stencilbuffer)
1925 {
1926 *params = stencilbuffer->getStencilSize();
1927 }
1928 else
1929 {
1930 *params = 0;
1931 }
1932 }
1933 break;
1934 case GL_TEXTURE_BINDING_2D:
Shannon Woods2df6a602014-09-26 16:12:07 -04001935 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001936 *params = getSamplerTextureId(static_cast<unsigned int>(mActiveSampler), GL_TEXTURE_2D);
Shannon Woods53a94a82014-06-24 15:20:36 -04001937 break;
Corentin Wallez13c0dd42017-07-04 18:27:01 -04001938 case GL_TEXTURE_BINDING_RECTANGLE_ANGLE:
1939 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
1940 *params = getSamplerTextureId(static_cast<unsigned int>(mActiveSampler),
1941 GL_TEXTURE_RECTANGLE_ANGLE);
1942 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001943 case GL_TEXTURE_BINDING_CUBE_MAP:
Shannon Woods2df6a602014-09-26 16:12:07 -04001944 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001945 *params =
1946 getSamplerTextureId(static_cast<unsigned int>(mActiveSampler), GL_TEXTURE_CUBE_MAP);
Shannon Woods53a94a82014-06-24 15:20:36 -04001947 break;
1948 case GL_TEXTURE_BINDING_3D:
Shannon Woods2df6a602014-09-26 16:12:07 -04001949 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001950 *params = getSamplerTextureId(static_cast<unsigned int>(mActiveSampler), GL_TEXTURE_3D);
Shannon Woods53a94a82014-06-24 15:20:36 -04001951 break;
1952 case GL_TEXTURE_BINDING_2D_ARRAY:
Shannon Woods2df6a602014-09-26 16:12:07 -04001953 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001954 *params =
1955 getSamplerTextureId(static_cast<unsigned int>(mActiveSampler), GL_TEXTURE_2D_ARRAY);
Shannon Woods53a94a82014-06-24 15:20:36 -04001956 break;
JiangYizhou24fe74c2017-07-06 16:56:50 +08001957 case GL_TEXTURE_BINDING_2D_MULTISAMPLE:
1958 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
1959 *params = getSamplerTextureId(static_cast<unsigned int>(mActiveSampler),
1960 GL_TEXTURE_2D_MULTISAMPLE);
1961 break;
John Bauman18319182016-09-28 14:22:27 -07001962 case GL_TEXTURE_BINDING_EXTERNAL_OES:
1963 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
1964 *params = getSamplerTextureId(static_cast<unsigned int>(mActiveSampler),
1965 GL_TEXTURE_EXTERNAL_OES);
1966 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001967 case GL_UNIFORM_BUFFER_BINDING:
1968 *params = mGenericUniformBuffer.id();
1969 break;
Frank Henigman22581ff2015-11-06 14:25:54 -05001970 case GL_TRANSFORM_FEEDBACK_BINDING:
Frank Henigmanb0f0b812015-11-21 17:49:29 -05001971 *params = mTransformFeedback.id();
Frank Henigman22581ff2015-11-06 14:25:54 -05001972 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001973 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING:
Geoff Lang045536b2015-03-27 15:17:18 -04001974 *params = mTransformFeedback->getGenericBuffer().id();
Shannon Woods53a94a82014-06-24 15:20:36 -04001975 break;
1976 case GL_COPY_READ_BUFFER_BINDING:
1977 *params = mCopyReadBuffer.id();
1978 break;
1979 case GL_COPY_WRITE_BUFFER_BINDING:
1980 *params = mCopyWriteBuffer.id();
1981 break;
1982 case GL_PIXEL_PACK_BUFFER_BINDING:
1983 *params = mPack.pixelBuffer.id();
1984 break;
1985 case GL_PIXEL_UNPACK_BUFFER_BINDING:
1986 *params = mUnpack.pixelBuffer.id();
1987 break;
Olli Etuaho86821db2016-03-04 12:05:47 +02001988 case GL_READ_BUFFER:
1989 *params = mReadFramebuffer->getReadBufferState();
1990 break;
1991 case GL_SAMPLER_BINDING:
1992 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
1993 *params = getSamplerId(static_cast<GLuint>(mActiveSampler));
1994 break;
Geoff Lang70d0f492015-12-10 17:45:46 -05001995 case GL_DEBUG_LOGGED_MESSAGES:
1996 *params = static_cast<GLint>(mDebug.getMessageCount());
1997 break;
1998 case GL_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH:
1999 *params = static_cast<GLint>(mDebug.getNextMessageLength());
2000 break;
2001 case GL_DEBUG_GROUP_STACK_DEPTH:
2002 *params = static_cast<GLint>(mDebug.getGroupStackDepth());
2003 break;
Sami Väisänen74c23472016-05-09 17:30:30 +03002004 case GL_MULTISAMPLE_EXT:
2005 *params = static_cast<GLint>(mMultiSampling);
2006 break;
2007 case GL_SAMPLE_ALPHA_TO_ONE_EXT:
2008 *params = static_cast<GLint>(mSampleAlphaToOne);
Sami Väisänena797e062016-05-12 15:23:40 +03002009 case GL_COVERAGE_MODULATION_CHROMIUM:
2010 *params = static_cast<GLint>(mCoverageModulation);
Sami Väisänen74c23472016-05-09 17:30:30 +03002011 break;
Jiajia Qin6eafb042016-12-27 17:04:07 +08002012 case GL_ATOMIC_COUNTER_BUFFER_BINDING:
2013 *params = mGenericAtomicCounterBuffer.id();
2014 break;
Jiajia Qinf546e7d2017-03-27 14:12:59 +08002015 case GL_SHADER_STORAGE_BUFFER_BINDING:
2016 *params = mGenericShaderStorageBuffer.id();
2017 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002018 default:
2019 UNREACHABLE();
2020 break;
2021 }
2022}
2023
Geoff Lang70d0f492015-12-10 17:45:46 -05002024void State::getPointerv(GLenum pname, void **params) const
2025{
2026 switch (pname)
2027 {
2028 case GL_DEBUG_CALLBACK_FUNCTION:
2029 *params = reinterpret_cast<void *>(mDebug.getCallback());
2030 break;
2031 case GL_DEBUG_CALLBACK_USER_PARAM:
2032 *params = const_cast<void *>(mDebug.getUserParam());
2033 break;
2034 default:
2035 UNREACHABLE();
2036 break;
2037 }
2038}
2039
Martin Radev66fb8202016-07-28 11:45:20 +03002040void State::getIntegeri_v(GLenum target, GLuint index, GLint *data)
Shannon Woods53a94a82014-06-24 15:20:36 -04002041{
2042 switch (target)
2043 {
2044 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002045 ASSERT(static_cast<size_t>(index) < mTransformFeedback->getIndexedBufferCount());
2046 *data = mTransformFeedback->getIndexedBuffer(index).id();
2047 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002048 case GL_UNIFORM_BUFFER_BINDING:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002049 ASSERT(static_cast<size_t>(index) < mUniformBuffers.size());
2050 *data = mUniformBuffers[index].id();
2051 break;
2052 case GL_ATOMIC_COUNTER_BUFFER_BINDING:
2053 ASSERT(static_cast<size_t>(index) < mAtomicCounterBuffers.size());
2054 *data = mAtomicCounterBuffers[index].id();
2055 break;
Jiajia Qinf546e7d2017-03-27 14:12:59 +08002056 case GL_SHADER_STORAGE_BUFFER_BINDING:
2057 ASSERT(static_cast<size_t>(index) < mShaderStorageBuffers.size());
2058 *data = mShaderStorageBuffers[index].id();
2059 break;
Shao80957d92017-02-20 21:25:59 +08002060 case GL_VERTEX_BINDING_BUFFER:
2061 ASSERT(static_cast<size_t>(index) < mVertexArray->getMaxBindings());
Martin Radevdd5f27e2017-06-07 10:17:09 +03002062 *data = mVertexArray->getVertexBinding(index).getBuffer().id();
Shao80957d92017-02-20 21:25:59 +08002063 break;
2064 case GL_VERTEX_BINDING_DIVISOR:
2065 ASSERT(static_cast<size_t>(index) < mVertexArray->getMaxBindings());
Martin Radevdd5f27e2017-06-07 10:17:09 +03002066 *data = mVertexArray->getVertexBinding(index).getDivisor();
Shao80957d92017-02-20 21:25:59 +08002067 break;
2068 case GL_VERTEX_BINDING_OFFSET:
2069 ASSERT(static_cast<size_t>(index) < mVertexArray->getMaxBindings());
Martin Radevdd5f27e2017-06-07 10:17:09 +03002070 *data = static_cast<GLuint>(mVertexArray->getVertexBinding(index).getOffset());
Shao80957d92017-02-20 21:25:59 +08002071 break;
2072 case GL_VERTEX_BINDING_STRIDE:
2073 ASSERT(static_cast<size_t>(index) < mVertexArray->getMaxBindings());
Martin Radevdd5f27e2017-06-07 10:17:09 +03002074 *data = mVertexArray->getVertexBinding(index).getStride();
Shao80957d92017-02-20 21:25:59 +08002075 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002076 default:
Martin Radev66fb8202016-07-28 11:45:20 +03002077 UNREACHABLE();
2078 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002079 }
Shannon Woods53a94a82014-06-24 15:20:36 -04002080}
2081
Martin Radev66fb8202016-07-28 11:45:20 +03002082void State::getInteger64i_v(GLenum target, GLuint index, GLint64 *data)
Shannon Woods53a94a82014-06-24 15:20:36 -04002083{
2084 switch (target)
2085 {
2086 case GL_TRANSFORM_FEEDBACK_BUFFER_START:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002087 ASSERT(static_cast<size_t>(index) < mTransformFeedback->getIndexedBufferCount());
2088 *data = mTransformFeedback->getIndexedBuffer(index).getOffset();
2089 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002090 case GL_TRANSFORM_FEEDBACK_BUFFER_SIZE:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002091 ASSERT(static_cast<size_t>(index) < mTransformFeedback->getIndexedBufferCount());
2092 *data = mTransformFeedback->getIndexedBuffer(index).getSize();
2093 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002094 case GL_UNIFORM_BUFFER_START:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002095 ASSERT(static_cast<size_t>(index) < mUniformBuffers.size());
2096 *data = mUniformBuffers[index].getOffset();
2097 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002098 case GL_UNIFORM_BUFFER_SIZE:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002099 ASSERT(static_cast<size_t>(index) < mUniformBuffers.size());
2100 *data = mUniformBuffers[index].getSize();
2101 break;
2102 case GL_ATOMIC_COUNTER_BUFFER_START:
2103 ASSERT(static_cast<size_t>(index) < mAtomicCounterBuffers.size());
2104 *data = mAtomicCounterBuffers[index].getOffset();
2105 break;
2106 case GL_ATOMIC_COUNTER_BUFFER_SIZE:
2107 ASSERT(static_cast<size_t>(index) < mAtomicCounterBuffers.size());
2108 *data = mAtomicCounterBuffers[index].getSize();
2109 break;
Jiajia Qinf546e7d2017-03-27 14:12:59 +08002110 case GL_SHADER_STORAGE_BUFFER_START:
2111 ASSERT(static_cast<size_t>(index) < mShaderStorageBuffers.size());
2112 *data = mShaderStorageBuffers[index].getOffset();
2113 break;
2114 case GL_SHADER_STORAGE_BUFFER_SIZE:
2115 ASSERT(static_cast<size_t>(index) < mShaderStorageBuffers.size());
2116 *data = mShaderStorageBuffers[index].getSize();
2117 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002118 default:
Martin Radev66fb8202016-07-28 11:45:20 +03002119 UNREACHABLE();
2120 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002121 }
Martin Radev66fb8202016-07-28 11:45:20 +03002122}
Shannon Woods53a94a82014-06-24 15:20:36 -04002123
Martin Radev66fb8202016-07-28 11:45:20 +03002124void State::getBooleani_v(GLenum target, GLuint index, GLboolean *data)
2125{
2126 UNREACHABLE();
Shannon Woods53a94a82014-06-24 15:20:36 -04002127}
2128
Jamie Madilld9ba4f72014-08-04 10:47:59 -04002129bool State::hasMappedBuffer(GLenum target) const
2130{
2131 if (target == GL_ARRAY_BUFFER)
2132 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +08002133 const VertexArray *vao = getVertexArray();
Jamie Madilleea3a6e2015-04-15 10:02:48 -04002134 const auto &vertexAttribs = vao->getVertexAttributes();
Jiawei-Shao2597fb62016-12-09 16:38:02 +08002135 const auto &vertexBindings = vao->getVertexBindings();
Jamie Madill8e344942015-07-09 14:22:07 -04002136 size_t maxEnabledAttrib = vao->getMaxEnabledAttribute();
Jamie Madillaebf9dd2015-04-28 12:39:07 -04002137 for (size_t attribIndex = 0; attribIndex < maxEnabledAttrib; attribIndex++)
Jamie Madilld9ba4f72014-08-04 10:47:59 -04002138 {
Jamie Madill81c2e252017-09-09 23:32:46 -04002139 const VertexAttribute &vertexAttrib = vertexAttribs[attribIndex];
Martin Radevdd5f27e2017-06-07 10:17:09 +03002140 auto *boundBuffer = vertexBindings[vertexAttrib.bindingIndex].getBuffer().get();
Jamie Madilld9ba4f72014-08-04 10:47:59 -04002141 if (vertexAttrib.enabled && boundBuffer && boundBuffer->isMapped())
2142 {
2143 return true;
2144 }
2145 }
2146
2147 return false;
2148 }
2149 else
2150 {
2151 Buffer *buffer = getTargetBuffer(target);
2152 return (buffer && buffer->isMapped());
2153 }
2154}
2155
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002156void State::syncDirtyObjects(const Context *context)
Jamie Madillc9d442d2016-01-20 11:17:24 -05002157{
2158 if (!mDirtyObjects.any())
2159 return;
2160
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002161 syncDirtyObjects(context, mDirtyObjects);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002162}
2163
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002164void State::syncDirtyObjects(const Context *context, const DirtyObjects &bitset)
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002165{
Jamie Madill6de51852017-04-12 09:53:01 -04002166 for (auto dirtyObject : bitset)
Jamie Madillc9d442d2016-01-20 11:17:24 -05002167 {
2168 switch (dirtyObject)
2169 {
2170 case DIRTY_OBJECT_READ_FRAMEBUFFER:
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002171 ASSERT(mReadFramebuffer);
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002172 mReadFramebuffer->syncState(context);
Jamie Madillc9d442d2016-01-20 11:17:24 -05002173 break;
2174 case DIRTY_OBJECT_DRAW_FRAMEBUFFER:
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002175 ASSERT(mDrawFramebuffer);
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002176 mDrawFramebuffer->syncState(context);
Jamie Madillc9d442d2016-01-20 11:17:24 -05002177 break;
2178 case DIRTY_OBJECT_VERTEX_ARRAY:
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002179 ASSERT(mVertexArray);
Jamie Madill06ef36b2017-09-09 23:32:46 -04002180 mVertexArray->syncState(context);
Jamie Madillc9d442d2016-01-20 11:17:24 -05002181 break;
Jamie Madill81c2e252017-09-09 23:32:46 -04002182 case DIRTY_OBJECT_PROGRAM_TEXTURES:
2183 syncProgramTextures(context);
2184 break;
2185
Jamie Madillc9d442d2016-01-20 11:17:24 -05002186 default:
2187 UNREACHABLE();
2188 break;
2189 }
2190 }
2191
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002192 mDirtyObjects &= ~bitset;
2193}
2194
Jamie Madill81c2e252017-09-09 23:32:46 -04002195void State::syncProgramTextures(const Context *context)
2196{
2197 std::fill(mCompleteTextureCache.begin(), mCompleteTextureCache.end(), nullptr);
2198 for (auto &binding : mCompleteTextureBindings)
2199 {
2200 binding.reset();
2201 }
2202
2203 // TODO(jmadill): Fine-grained updates.
2204 if (!mProgram)
2205 {
2206 return;
2207 }
2208
2209 ASSERT(mDirtyObjects[DIRTY_OBJECT_PROGRAM_TEXTURES]);
2210 mDirtyBits.set(DIRTY_BIT_TEXTURE_BINDINGS);
2211
2212 for (const SamplerBinding &samplerBinding : mProgram->getSamplerBindings())
2213 {
2214 if (samplerBinding.unreferenced)
2215 continue;
2216
2217 GLenum textureType = samplerBinding.textureType;
2218 for (GLuint textureUnitIndex : samplerBinding.boundTextureUnits)
2219 {
2220 Texture *texture = getSamplerTexture(textureUnitIndex, textureType);
Jamie Madill06ef36b2017-09-09 23:32:46 -04002221 Sampler *sampler = getSampler(textureUnitIndex);
Jamie Madill81c2e252017-09-09 23:32:46 -04002222
2223 if (texture != nullptr)
2224 {
Jamie Madill81c2e252017-09-09 23:32:46 -04002225 // Mark the texture binding bit as dirty if the texture completeness changes.
2226 // TODO(jmadill): Use specific dirty bit for completeness change.
2227 if (texture->isSamplerComplete(context, sampler))
2228 {
Jamie Madill06ef36b2017-09-09 23:32:46 -04002229 texture->syncState();
Jamie Madill81c2e252017-09-09 23:32:46 -04002230 ASSERT(static_cast<size_t>(textureUnitIndex) < mCompleteTextureCache.size());
2231 ASSERT(mCompleteTextureCache[textureUnitIndex] == nullptr ||
2232 mCompleteTextureCache[textureUnitIndex] == texture);
2233 mCompleteTextureCache[textureUnitIndex] = texture;
2234 }
2235
2236 // Bind the texture unconditionally, to recieve completeness change notifications.
2237 mCompleteTextureBindings[textureUnitIndex].bind(texture->getDirtyChannel());
2238 }
2239
Jamie Madill06ef36b2017-09-09 23:32:46 -04002240 if (sampler != nullptr)
2241 {
2242 sampler->syncState(context);
2243 }
Jamie Madill81c2e252017-09-09 23:32:46 -04002244 }
2245 }
2246}
2247
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002248void State::syncDirtyObject(const Context *context, GLenum target)
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002249{
2250 DirtyObjects localSet;
2251
2252 switch (target)
2253 {
2254 case GL_READ_FRAMEBUFFER:
2255 localSet.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
2256 break;
2257 case GL_DRAW_FRAMEBUFFER:
2258 localSet.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
2259 break;
2260 case GL_FRAMEBUFFER:
2261 localSet.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
2262 localSet.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
2263 break;
2264 case GL_VERTEX_ARRAY:
2265 localSet.set(DIRTY_OBJECT_VERTEX_ARRAY);
2266 break;
Jamie Madill81c2e252017-09-09 23:32:46 -04002267 case GL_TEXTURE:
2268 case GL_SAMPLER:
2269 case GL_PROGRAM:
2270 localSet.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
2271 break;
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002272 }
2273
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002274 syncDirtyObjects(context, localSet);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002275}
2276
2277void State::setObjectDirty(GLenum target)
2278{
2279 switch (target)
2280 {
2281 case GL_READ_FRAMEBUFFER:
2282 mDirtyObjects.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
2283 break;
2284 case GL_DRAW_FRAMEBUFFER:
2285 mDirtyObjects.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
2286 break;
2287 case GL_FRAMEBUFFER:
2288 mDirtyObjects.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
2289 mDirtyObjects.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
2290 break;
2291 case GL_VERTEX_ARRAY:
2292 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
2293 break;
Jamie Madill81c2e252017-09-09 23:32:46 -04002294 case GL_TEXTURE:
2295 case GL_SAMPLER:
Jamie Madill81c2e252017-09-09 23:32:46 -04002296 case GL_PROGRAM:
2297 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
2298 mDirtyBits.set(DIRTY_BIT_TEXTURE_BINDINGS);
2299 break;
Jamie Madilla779b612017-07-24 11:46:05 -04002300 }
2301}
2302
2303void State::onProgramExecutableChange(Program *program)
2304{
2305 // OpenGL Spec:
2306 // "If LinkProgram or ProgramBinary successfully re-links a program object
2307 // that was already in use as a result of a previous call to UseProgram, then the
2308 // generated executable code will be installed as part of the current rendering state."
2309 if (program->isLinked() && mProgram == program)
2310 {
2311 mDirtyBits.set(DIRTY_BIT_PROGRAM_EXECUTABLE);
Jamie Madill81c2e252017-09-09 23:32:46 -04002312 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002313 }
Shannon Woods53a94a82014-06-24 15:20:36 -04002314}
Jamie Madillc9d442d2016-01-20 11:17:24 -05002315
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002316void State::setImageUnit(const Context *context,
2317 GLuint unit,
2318 Texture *texture,
2319 GLint level,
2320 GLboolean layered,
2321 GLint layer,
2322 GLenum access,
2323 GLenum format)
2324{
2325 mImageUnits[unit].texture.set(context, texture);
2326 mImageUnits[unit].level = level;
2327 mImageUnits[unit].layered = layered;
2328 mImageUnits[unit].layer = layer;
2329 mImageUnits[unit].access = access;
2330 mImageUnits[unit].format = format;
2331}
2332
2333const ImageUnit &State::getImageUnit(GLuint unit) const
2334{
2335 return mImageUnits[unit];
2336}
2337
Jamie Madill81c2e252017-09-09 23:32:46 -04002338// Handle a dirty texture event.
2339void State::signal(uint32_t textureIndex)
2340{
2341 // Conservatively assume all textures are dirty.
2342 // TODO(jmadill): More fine-grained update.
2343 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
2344}
2345
Jamie Madillc9d442d2016-01-20 11:17:24 -05002346} // namespace gl