blob: 46fbe5cb71404559aef95ad707dc655d5be79b7d [file] [log] [blame]
Shannon Woods53a94a82014-06-24 15:20:36 -04001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// State.cpp: Implements the State class, encapsulating raw GL state.
8
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/State.h"
Shannon Woods53a94a82014-06-24 15:20:36 -040010
Sami Väisänene45e53b2016-05-25 10:36:04 +030011#include <limits>
12#include <string.h>
13
Jamie Madill20e005b2017-04-07 14:19:22 -040014#include "common/bitset_utils.h"
Sami Väisänene45e53b2016-05-25 10:36:04 +030015#include "common/mathutil.h"
jchen10a99ed552017-09-22 08:10:32 +080016#include "common/matrix_utils.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050017#include "libANGLE/Caps.h"
jchen10a99ed552017-09-22 08:10:32 +080018#include "libANGLE/Context.h"
Geoff Lang70d0f492015-12-10 17:45:46 -050019#include "libANGLE/Debug.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050020#include "libANGLE/Framebuffer.h"
21#include "libANGLE/FramebufferAttachment.h"
22#include "libANGLE/Query.h"
23#include "libANGLE/VertexArray.h"
24#include "libANGLE/formatutils.h"
jchen10a99ed552017-09-22 08:10:32 +080025#include "libANGLE/queryconversions.h"
Geoff Lang4751aab2017-10-30 15:14:52 -040026#include "libANGLE/renderer/ContextImpl.h"
Shannon Woods53a94a82014-06-24 15:20:36 -040027
Olli Etuahobbf1c102016-06-28 13:31:33 +030028namespace
29{
30
31GLenum ActiveQueryType(const GLenum type)
32{
33 return (type == GL_ANY_SAMPLES_PASSED_CONSERVATIVE) ? GL_ANY_SAMPLES_PASSED : type;
34}
35
36} // anonymous namepace
37
Shannon Woods53a94a82014-06-24 15:20:36 -040038namespace gl
39{
Geoff Lang76b10c92014-09-05 16:28:14 -040040
Shannon Woods53a94a82014-06-24 15:20:36 -040041State::State()
Jamie Madille79b1e12015-11-04 16:36:37 -050042 : mMaxDrawBuffers(0),
43 mMaxCombinedTextureImageUnits(0),
44 mDepthClearValue(0),
45 mStencilClearValue(0),
46 mScissorTest(false),
47 mSampleCoverage(false),
48 mSampleCoverageValue(0),
49 mSampleCoverageInvert(false),
Jiawei Shaodb342272017-09-27 10:21:45 +080050 mSampleMask(false),
51 mMaxSampleMaskWords(0),
Jamie Madille79b1e12015-11-04 16:36:37 -050052 mStencilRef(0),
53 mStencilBackRef(0),
54 mLineWidth(0),
55 mGenerateMipmapHint(GL_NONE),
56 mFragmentShaderDerivativeHint(GL_NONE),
Geoff Langf41a7152016-09-19 15:11:17 -040057 mBindGeneratesResource(true),
Geoff Langfeb8c682017-02-13 16:07:35 -050058 mClientArraysEnabled(true),
Jamie Madille79b1e12015-11-04 16:36:37 -050059 mNearZ(0),
60 mFarZ(0),
61 mReadFramebuffer(nullptr),
62 mDrawFramebuffer(nullptr),
63 mProgram(nullptr),
64 mVertexArray(nullptr),
65 mActiveSampler(0),
Sami Väisänen74c23472016-05-09 17:30:30 +030066 mPrimitiveRestart(false),
67 mMultiSampling(false),
Geoff Lang1d2c41d2016-10-19 16:14:46 -070068 mSampleAlphaToOne(false),
Jamie Madille08a1d32017-03-07 17:24:06 -050069 mFramebufferSRGB(true),
Jamie Madillc43be722017-07-13 16:22:14 -040070 mRobustResourceInit(false),
71 mProgramBinaryCacheEnabled(false)
Shannon Woods53a94a82014-06-24 15:20:36 -040072{
Geoff Lang76b10c92014-09-05 16:28:14 -040073}
74
75State::~State()
76{
Geoff Lang76b10c92014-09-05 16:28:14 -040077}
78
Jamie Madill4928b7c2017-06-20 12:57:39 -040079void State::initialize(const Context *context,
Geoff Langf41a7152016-09-19 15:11:17 -040080 bool debug,
Geoff Langfeb8c682017-02-13 16:07:35 -050081 bool bindGeneratesResource,
Jamie Madille08a1d32017-03-07 17:24:06 -050082 bool clientArraysEnabled,
Jamie Madillc43be722017-07-13 16:22:14 -040083 bool robustResourceInit,
84 bool programBinaryCacheEnabled)
Geoff Lang76b10c92014-09-05 16:28:14 -040085{
Jamie Madill4928b7c2017-06-20 12:57:39 -040086 const Caps &caps = context->getCaps();
87 const Extensions &extensions = context->getExtensions();
Geoff Lang4751aab2017-10-30 15:14:52 -040088 const Extensions &nativeExtensions = context->getImplementation()->getNativeExtensions();
Jamie Madill4928b7c2017-06-20 12:57:39 -040089 const Version &clientVersion = context->getClientVersion();
90
Shannon Woods2df6a602014-09-26 16:12:07 -040091 mMaxDrawBuffers = caps.maxDrawBuffers;
92 mMaxCombinedTextureImageUnits = caps.maxCombinedTextureImageUnits;
Shannon Woods53a94a82014-06-24 15:20:36 -040093
Jamie Madillf75ab352015-03-16 10:46:52 -040094 setColorClearValue(0.0f, 0.0f, 0.0f, 0.0f);
Shannon Woods53a94a82014-06-24 15:20:36 -040095
96 mDepthClearValue = 1.0f;
97 mStencilClearValue = 0;
98
Shannon Woods53a94a82014-06-24 15:20:36 -040099 mScissorTest = false;
100 mScissor.x = 0;
101 mScissor.y = 0;
102 mScissor.width = 0;
103 mScissor.height = 0;
104
Shannon Woods53a94a82014-06-24 15:20:36 -0400105 mBlendColor.red = 0;
106 mBlendColor.green = 0;
107 mBlendColor.blue = 0;
108 mBlendColor.alpha = 0;
109
Shannon Woods53a94a82014-06-24 15:20:36 -0400110 mStencilRef = 0;
111 mStencilBackRef = 0;
112
113 mSampleCoverage = false;
114 mSampleCoverageValue = 1.0f;
115 mSampleCoverageInvert = false;
Jiawei Shaodb342272017-09-27 10:21:45 +0800116
117 mMaxSampleMaskWords = caps.maxSampleMaskWords;
118 mSampleMask = false;
119 mSampleMaskValues.fill(~GLbitfield(0));
120
Shannon Woods53a94a82014-06-24 15:20:36 -0400121 mGenerateMipmapHint = GL_DONT_CARE;
122 mFragmentShaderDerivativeHint = GL_DONT_CARE;
123
Geoff Langf41a7152016-09-19 15:11:17 -0400124 mBindGeneratesResource = bindGeneratesResource;
Geoff Langfeb8c682017-02-13 16:07:35 -0500125 mClientArraysEnabled = clientArraysEnabled;
Geoff Langf41a7152016-09-19 15:11:17 -0400126
Shannon Woods53a94a82014-06-24 15:20:36 -0400127 mLineWidth = 1.0f;
128
129 mViewport.x = 0;
130 mViewport.y = 0;
131 mViewport.width = 0;
132 mViewport.height = 0;
133 mNearZ = 0.0f;
134 mFarZ = 1.0f;
135
136 mBlend.colorMaskRed = true;
137 mBlend.colorMaskGreen = true;
138 mBlend.colorMaskBlue = true;
139 mBlend.colorMaskAlpha = true;
140
Geoff Lang76b10c92014-09-05 16:28:14 -0400141 mActiveSampler = 0;
142
Shannon Woods23e05002014-09-22 19:07:27 -0400143 mVertexAttribCurrentValues.resize(caps.maxVertexAttributes);
Shannon Woods53a94a82014-06-24 15:20:36 -0400144
Geoff Lang4dc3af02016-11-18 14:09:27 -0500145 mUniformBuffers.resize(caps.maxUniformBufferBindings);
Shannon Woodsf3acaf92014-09-23 18:07:11 -0400146
Geoff Lang76b10c92014-09-05 16:28:14 -0400147 mSamplerTextures[GL_TEXTURE_2D].resize(caps.maxCombinedTextureImageUnits);
148 mSamplerTextures[GL_TEXTURE_CUBE_MAP].resize(caps.maxCombinedTextureImageUnits);
Geoff Langeb66a6e2016-10-31 13:06:12 -0400149 if (clientVersion >= Version(3, 0))
Shannon Woods53a94a82014-06-24 15:20:36 -0400150 {
Geoff Lang76b10c92014-09-05 16:28:14 -0400151 // TODO: These could also be enabled via extension
152 mSamplerTextures[GL_TEXTURE_2D_ARRAY].resize(caps.maxCombinedTextureImageUnits);
153 mSamplerTextures[GL_TEXTURE_3D].resize(caps.maxCombinedTextureImageUnits);
Shannon Woods53a94a82014-06-24 15:20:36 -0400154 }
Geoff Lang3b573612016-10-31 14:08:10 -0400155 if (clientVersion >= Version(3, 1))
156 {
157 mSamplerTextures[GL_TEXTURE_2D_MULTISAMPLE].resize(caps.maxCombinedTextureImageUnits);
Jiajia Qin6eafb042016-12-27 17:04:07 +0800158
159 mAtomicCounterBuffers.resize(caps.maxAtomicCounterBufferBindings);
Jiajia Qinf546e7d2017-03-27 14:12:59 +0800160 mShaderStorageBuffers.resize(caps.maxShaderStorageBufferBindings);
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800161 mImageUnits.resize(caps.maxImageUnits);
Geoff Lang3b573612016-10-31 14:08:10 -0400162 }
Geoff Lang4751aab2017-10-30 15:14:52 -0400163 if (nativeExtensions.textureRectangle)
Corentin Wallez13c0dd42017-07-04 18:27:01 -0400164 {
165 mSamplerTextures[GL_TEXTURE_RECTANGLE_ANGLE].resize(caps.maxCombinedTextureImageUnits);
166 }
Geoff Lang4751aab2017-10-30 15:14:52 -0400167 if (nativeExtensions.eglImageExternal || nativeExtensions.eglStreamConsumerExternal)
Ian Ewellbda75592016-04-18 17:25:54 -0400168 {
169 mSamplerTextures[GL_TEXTURE_EXTERNAL_OES].resize(caps.maxCombinedTextureImageUnits);
170 }
Jamie Madill81c2e252017-09-09 23:32:46 -0400171 mCompleteTextureCache.resize(caps.maxCombinedTextureImageUnits, nullptr);
172 mCompleteTextureBindings.reserve(caps.maxCombinedTextureImageUnits);
173 for (uint32_t textureIndex = 0; textureIndex < caps.maxCombinedTextureImageUnits;
174 ++textureIndex)
175 {
176 mCompleteTextureBindings.emplace_back(OnAttachmentDirtyBinding(this, textureIndex));
177 }
Shannon Woods53a94a82014-06-24 15:20:36 -0400178
Geoff Lang76b10c92014-09-05 16:28:14 -0400179 mSamplers.resize(caps.maxCombinedTextureImageUnits);
Shannon Woods53a94a82014-06-24 15:20:36 -0400180
Jamie Madill4928b7c2017-06-20 12:57:39 -0400181 mActiveQueries[GL_ANY_SAMPLES_PASSED].set(context, nullptr);
182 mActiveQueries[GL_ANY_SAMPLES_PASSED_CONSERVATIVE].set(context, nullptr);
183 mActiveQueries[GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN].set(context, nullptr);
184 mActiveQueries[GL_TIME_ELAPSED_EXT].set(context, nullptr);
185 mActiveQueries[GL_COMMANDS_COMPLETED_CHROMIUM].set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400186
Ian Ewell3ffd78b2016-01-22 16:09:42 -0500187 mProgram = nullptr;
Shannon Woods53a94a82014-06-24 15:20:36 -0400188
Ian Ewell3ffd78b2016-01-22 16:09:42 -0500189 mReadFramebuffer = nullptr;
190 mDrawFramebuffer = nullptr;
Jamie Madillb4b53c52015-02-03 15:22:48 -0500191
192 mPrimitiveRestart = false;
Geoff Lang70d0f492015-12-10 17:45:46 -0500193
194 mDebug.setOutputEnabled(debug);
195 mDebug.setMaxLoggedMessages(extensions.maxDebugLoggedMessages);
Sami Väisänen74c23472016-05-09 17:30:30 +0300196
Geoff Lang488130e2017-09-27 13:53:11 -0400197 mMultiSampling = true;
198 mSampleAlphaToOne = false;
Sami Väisänena797e062016-05-12 15:23:40 +0300199
200 mCoverageModulation = GL_NONE;
Sami Väisänene45e53b2016-05-25 10:36:04 +0300201
202 angle::Matrix<GLfloat>::setToIdentity(mPathMatrixProj);
203 angle::Matrix<GLfloat>::setToIdentity(mPathMatrixMV);
204 mPathStencilFunc = GL_ALWAYS;
205 mPathStencilRef = 0;
206 mPathStencilMask = std::numeric_limits<GLuint>::max();
Jamie Madille08a1d32017-03-07 17:24:06 -0500207
208 mRobustResourceInit = robustResourceInit;
Jamie Madillc43be722017-07-13 16:22:14 -0400209 mProgramBinaryCacheEnabled = programBinaryCacheEnabled;
Shannon Woods53a94a82014-06-24 15:20:36 -0400210}
211
Jamie Madill6c1f6712017-02-14 19:08:04 -0500212void State::reset(const Context *context)
Shannon Woods53a94a82014-06-24 15:20:36 -0400213{
Jamie Madill8693bdb2017-09-02 15:32:14 -0400214 for (auto &bindingVec : mSamplerTextures)
Shannon Woods53a94a82014-06-24 15:20:36 -0400215 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400216 TextureBindingVector &textureVector = bindingVec.second;
Geoff Lang76b10c92014-09-05 16:28:14 -0400217 for (size_t textureIdx = 0; textureIdx < textureVector.size(); textureIdx++)
Shannon Woods53a94a82014-06-24 15:20:36 -0400218 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400219 textureVector[textureIdx].set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400220 }
221 }
Geoff Lang76b10c92014-09-05 16:28:14 -0400222 for (size_t samplerIdx = 0; samplerIdx < mSamplers.size(); samplerIdx++)
223 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400224 mSamplers[samplerIdx].set(context, nullptr);
Geoff Lang76b10c92014-09-05 16:28:14 -0400225 }
Shannon Woods53a94a82014-06-24 15:20:36 -0400226
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800227 for (auto &imageUnit : mImageUnits)
228 {
229 imageUnit.texture.set(context, nullptr);
230 imageUnit.level = 0;
231 imageUnit.layered = false;
232 imageUnit.layer = 0;
233 imageUnit.access = GL_READ_ONLY;
234 imageUnit.format = GL_R32UI;
235 }
236
Jamie Madill4928b7c2017-06-20 12:57:39 -0400237 mArrayBuffer.set(context, nullptr);
238 mDrawIndirectBuffer.set(context, nullptr);
239 mRenderbuffer.set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400240
Geoff Lang7dd2e102014-11-10 15:19:26 -0500241 if (mProgram)
242 {
Jamie Madill6c1f6712017-02-14 19:08:04 -0500243 mProgram->release(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500244 }
Yunchao Hed7297bf2017-04-19 15:27:10 +0800245 mProgram = nullptr;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500246
Yunchao Hea336b902017-08-02 16:05:21 +0800247 mProgramPipeline.set(context, nullptr);
248
Jamie Madill4928b7c2017-06-20 12:57:39 -0400249 mTransformFeedback.set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400250
251 for (State::ActiveQueryMap::iterator i = mActiveQueries.begin(); i != mActiveQueries.end(); i++)
252 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400253 i->second.set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400254 }
255
Jamie Madill4928b7c2017-06-20 12:57:39 -0400256 mGenericUniformBuffer.set(context, nullptr);
Shannon Woods8299bb02014-09-26 18:55:43 -0400257 for (BufferVector::iterator bufItr = mUniformBuffers.begin(); bufItr != mUniformBuffers.end(); ++bufItr)
Shannon Woods53a94a82014-06-24 15:20:36 -0400258 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400259 bufItr->set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400260 }
261
Jamie Madill4928b7c2017-06-20 12:57:39 -0400262 mCopyReadBuffer.set(context, nullptr);
263 mCopyWriteBuffer.set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400264
Jamie Madill4928b7c2017-06-20 12:57:39 -0400265 mPack.pixelBuffer.set(context, nullptr);
266 mUnpack.pixelBuffer.set(context, nullptr);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500267
Jamie Madill4928b7c2017-06-20 12:57:39 -0400268 mGenericAtomicCounterBuffer.set(context, nullptr);
Jiajia Qin6eafb042016-12-27 17:04:07 +0800269 for (auto &buf : mAtomicCounterBuffers)
270 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400271 buf.set(context, nullptr);
Jiajia Qin6eafb042016-12-27 17:04:07 +0800272 }
273
Jamie Madill4928b7c2017-06-20 12:57:39 -0400274 mGenericShaderStorageBuffer.set(context, nullptr);
Jiajia Qinf546e7d2017-03-27 14:12:59 +0800275 for (auto &buf : mShaderStorageBuffers)
276 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400277 buf.set(context, nullptr);
Jiajia Qinf546e7d2017-03-27 14:12:59 +0800278 }
279
Sami Väisänene45e53b2016-05-25 10:36:04 +0300280 angle::Matrix<GLfloat>::setToIdentity(mPathMatrixProj);
281 angle::Matrix<GLfloat>::setToIdentity(mPathMatrixMV);
282 mPathStencilFunc = GL_ALWAYS;
283 mPathStencilRef = 0;
284 mPathStencilMask = std::numeric_limits<GLuint>::max();
285
Jamie Madill1b94d432015-08-07 13:23:23 -0400286 // TODO(jmadill): Is this necessary?
287 setAllDirtyBits();
Shannon Woods53a94a82014-06-24 15:20:36 -0400288}
289
290const RasterizerState &State::getRasterizerState() const
291{
292 return mRasterizer;
293}
294
295const BlendState &State::getBlendState() const
296{
297 return mBlend;
298}
299
300const DepthStencilState &State::getDepthStencilState() const
301{
302 return mDepthStencil;
303}
304
Jamie Madillf75ab352015-03-16 10:46:52 -0400305void State::setColorClearValue(float red, float green, float blue, float alpha)
Shannon Woods53a94a82014-06-24 15:20:36 -0400306{
307 mColorClearValue.red = red;
308 mColorClearValue.green = green;
309 mColorClearValue.blue = blue;
310 mColorClearValue.alpha = alpha;
Jamie Madill1b94d432015-08-07 13:23:23 -0400311 mDirtyBits.set(DIRTY_BIT_CLEAR_COLOR);
Shannon Woods53a94a82014-06-24 15:20:36 -0400312}
313
Jamie Madillf75ab352015-03-16 10:46:52 -0400314void State::setDepthClearValue(float depth)
Shannon Woods53a94a82014-06-24 15:20:36 -0400315{
316 mDepthClearValue = depth;
Jamie Madill1b94d432015-08-07 13:23:23 -0400317 mDirtyBits.set(DIRTY_BIT_CLEAR_DEPTH);
Shannon Woods53a94a82014-06-24 15:20:36 -0400318}
319
Jamie Madillf75ab352015-03-16 10:46:52 -0400320void State::setStencilClearValue(int stencil)
Shannon Woods53a94a82014-06-24 15:20:36 -0400321{
322 mStencilClearValue = stencil;
Jamie Madill1b94d432015-08-07 13:23:23 -0400323 mDirtyBits.set(DIRTY_BIT_CLEAR_STENCIL);
Shannon Woods53a94a82014-06-24 15:20:36 -0400324}
325
Shannon Woods53a94a82014-06-24 15:20:36 -0400326void State::setColorMask(bool red, bool green, bool blue, bool alpha)
327{
328 mBlend.colorMaskRed = red;
329 mBlend.colorMaskGreen = green;
330 mBlend.colorMaskBlue = blue;
331 mBlend.colorMaskAlpha = alpha;
Jamie Madill1b94d432015-08-07 13:23:23 -0400332 mDirtyBits.set(DIRTY_BIT_COLOR_MASK);
Shannon Woods53a94a82014-06-24 15:20:36 -0400333}
334
335void State::setDepthMask(bool mask)
336{
337 mDepthStencil.depthMask = mask;
Jamie Madill1b94d432015-08-07 13:23:23 -0400338 mDirtyBits.set(DIRTY_BIT_DEPTH_MASK);
Shannon Woods53a94a82014-06-24 15:20:36 -0400339}
340
341bool State::isRasterizerDiscardEnabled() const
342{
343 return mRasterizer.rasterizerDiscard;
344}
345
346void State::setRasterizerDiscard(bool enabled)
347{
348 mRasterizer.rasterizerDiscard = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400349 mDirtyBits.set(DIRTY_BIT_RASTERIZER_DISCARD_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400350}
351
352bool State::isCullFaceEnabled() const
353{
354 return mRasterizer.cullFace;
355}
356
357void State::setCullFace(bool enabled)
358{
359 mRasterizer.cullFace = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400360 mDirtyBits.set(DIRTY_BIT_CULL_FACE_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400361}
362
Corentin Wallez2e568cf2017-09-18 17:05:22 -0400363void State::setCullMode(CullFaceMode mode)
Shannon Woods53a94a82014-06-24 15:20:36 -0400364{
365 mRasterizer.cullMode = mode;
Jamie Madill1b94d432015-08-07 13:23:23 -0400366 mDirtyBits.set(DIRTY_BIT_CULL_FACE);
Shannon Woods53a94a82014-06-24 15:20:36 -0400367}
368
369void State::setFrontFace(GLenum front)
370{
371 mRasterizer.frontFace = front;
Jamie Madill1b94d432015-08-07 13:23:23 -0400372 mDirtyBits.set(DIRTY_BIT_FRONT_FACE);
Shannon Woods53a94a82014-06-24 15:20:36 -0400373}
374
375bool State::isDepthTestEnabled() const
376{
377 return mDepthStencil.depthTest;
378}
379
380void State::setDepthTest(bool enabled)
381{
382 mDepthStencil.depthTest = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400383 mDirtyBits.set(DIRTY_BIT_DEPTH_TEST_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400384}
385
386void State::setDepthFunc(GLenum depthFunc)
387{
388 mDepthStencil.depthFunc = depthFunc;
Jamie Madill1b94d432015-08-07 13:23:23 -0400389 mDirtyBits.set(DIRTY_BIT_DEPTH_FUNC);
Shannon Woods53a94a82014-06-24 15:20:36 -0400390}
391
392void State::setDepthRange(float zNear, float zFar)
393{
394 mNearZ = zNear;
395 mFarZ = zFar;
Jamie Madill1b94d432015-08-07 13:23:23 -0400396 mDirtyBits.set(DIRTY_BIT_DEPTH_RANGE);
Shannon Woods53a94a82014-06-24 15:20:36 -0400397}
398
Geoff Langd42f5b82015-04-16 14:03:29 -0400399float State::getNearPlane() const
Shannon Woods53a94a82014-06-24 15:20:36 -0400400{
Geoff Langd42f5b82015-04-16 14:03:29 -0400401 return mNearZ;
402}
403
404float State::getFarPlane() const
405{
406 return mFarZ;
Shannon Woods53a94a82014-06-24 15:20:36 -0400407}
408
409bool State::isBlendEnabled() const
410{
411 return mBlend.blend;
412}
413
414void State::setBlend(bool enabled)
415{
416 mBlend.blend = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400417 mDirtyBits.set(DIRTY_BIT_BLEND_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400418}
419
420void State::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
421{
422 mBlend.sourceBlendRGB = sourceRGB;
423 mBlend.destBlendRGB = destRGB;
424 mBlend.sourceBlendAlpha = sourceAlpha;
425 mBlend.destBlendAlpha = destAlpha;
Jamie Madill1b94d432015-08-07 13:23:23 -0400426 mDirtyBits.set(DIRTY_BIT_BLEND_FUNCS);
Shannon Woods53a94a82014-06-24 15:20:36 -0400427}
428
429void State::setBlendColor(float red, float green, float blue, float alpha)
430{
431 mBlendColor.red = red;
432 mBlendColor.green = green;
433 mBlendColor.blue = blue;
434 mBlendColor.alpha = alpha;
Jamie Madill1b94d432015-08-07 13:23:23 -0400435 mDirtyBits.set(DIRTY_BIT_BLEND_COLOR);
Shannon Woods53a94a82014-06-24 15:20:36 -0400436}
437
438void State::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
439{
440 mBlend.blendEquationRGB = rgbEquation;
441 mBlend.blendEquationAlpha = alphaEquation;
Jamie Madill1b94d432015-08-07 13:23:23 -0400442 mDirtyBits.set(DIRTY_BIT_BLEND_EQUATIONS);
Shannon Woods53a94a82014-06-24 15:20:36 -0400443}
444
445const ColorF &State::getBlendColor() const
446{
447 return mBlendColor;
448}
449
450bool State::isStencilTestEnabled() const
451{
452 return mDepthStencil.stencilTest;
453}
454
455void State::setStencilTest(bool enabled)
456{
457 mDepthStencil.stencilTest = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400458 mDirtyBits.set(DIRTY_BIT_STENCIL_TEST_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400459}
460
461void State::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
462{
463 mDepthStencil.stencilFunc = stencilFunc;
464 mStencilRef = (stencilRef > 0) ? stencilRef : 0;
465 mDepthStencil.stencilMask = stencilMask;
Jamie Madill1b94d432015-08-07 13:23:23 -0400466 mDirtyBits.set(DIRTY_BIT_STENCIL_FUNCS_FRONT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400467}
468
469void State::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
470{
471 mDepthStencil.stencilBackFunc = stencilBackFunc;
472 mStencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
473 mDepthStencil.stencilBackMask = stencilBackMask;
Jamie Madill1b94d432015-08-07 13:23:23 -0400474 mDirtyBits.set(DIRTY_BIT_STENCIL_FUNCS_BACK);
Shannon Woods53a94a82014-06-24 15:20:36 -0400475}
476
477void State::setStencilWritemask(GLuint stencilWritemask)
478{
479 mDepthStencil.stencilWritemask = stencilWritemask;
Jamie Madill1b94d432015-08-07 13:23:23 -0400480 mDirtyBits.set(DIRTY_BIT_STENCIL_WRITEMASK_FRONT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400481}
482
483void State::setStencilBackWritemask(GLuint stencilBackWritemask)
484{
485 mDepthStencil.stencilBackWritemask = stencilBackWritemask;
Jamie Madill1b94d432015-08-07 13:23:23 -0400486 mDirtyBits.set(DIRTY_BIT_STENCIL_WRITEMASK_BACK);
Shannon Woods53a94a82014-06-24 15:20:36 -0400487}
488
489void State::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
490{
491 mDepthStencil.stencilFail = stencilFail;
492 mDepthStencil.stencilPassDepthFail = stencilPassDepthFail;
493 mDepthStencil.stencilPassDepthPass = stencilPassDepthPass;
Jamie Madill1b94d432015-08-07 13:23:23 -0400494 mDirtyBits.set(DIRTY_BIT_STENCIL_OPS_FRONT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400495}
496
497void State::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
498{
499 mDepthStencil.stencilBackFail = stencilBackFail;
500 mDepthStencil.stencilBackPassDepthFail = stencilBackPassDepthFail;
501 mDepthStencil.stencilBackPassDepthPass = stencilBackPassDepthPass;
Jamie Madill1b94d432015-08-07 13:23:23 -0400502 mDirtyBits.set(DIRTY_BIT_STENCIL_OPS_BACK);
Shannon Woods53a94a82014-06-24 15:20:36 -0400503}
504
505GLint State::getStencilRef() const
506{
507 return mStencilRef;
508}
509
510GLint State::getStencilBackRef() const
511{
512 return mStencilBackRef;
513}
514
515bool State::isPolygonOffsetFillEnabled() const
516{
517 return mRasterizer.polygonOffsetFill;
518}
519
520void State::setPolygonOffsetFill(bool enabled)
521{
Jamie Madill1b94d432015-08-07 13:23:23 -0400522 mRasterizer.polygonOffsetFill = enabled;
523 mDirtyBits.set(DIRTY_BIT_POLYGON_OFFSET_FILL_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400524}
525
526void State::setPolygonOffsetParams(GLfloat factor, GLfloat units)
527{
528 // An application can pass NaN values here, so handle this gracefully
529 mRasterizer.polygonOffsetFactor = factor != factor ? 0.0f : factor;
530 mRasterizer.polygonOffsetUnits = units != units ? 0.0f : units;
Jamie Madill1b94d432015-08-07 13:23:23 -0400531 mDirtyBits.set(DIRTY_BIT_POLYGON_OFFSET);
Shannon Woods53a94a82014-06-24 15:20:36 -0400532}
533
534bool State::isSampleAlphaToCoverageEnabled() const
535{
536 return mBlend.sampleAlphaToCoverage;
537}
538
539void State::setSampleAlphaToCoverage(bool enabled)
540{
541 mBlend.sampleAlphaToCoverage = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400542 mDirtyBits.set(DIRTY_BIT_SAMPLE_ALPHA_TO_COVERAGE_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400543}
544
545bool State::isSampleCoverageEnabled() const
546{
547 return mSampleCoverage;
548}
549
550void State::setSampleCoverage(bool enabled)
551{
552 mSampleCoverage = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400553 mDirtyBits.set(DIRTY_BIT_SAMPLE_COVERAGE_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400554}
555
556void State::setSampleCoverageParams(GLclampf value, bool invert)
557{
558 mSampleCoverageValue = value;
559 mSampleCoverageInvert = invert;
Jamie Madill1b94d432015-08-07 13:23:23 -0400560 mDirtyBits.set(DIRTY_BIT_SAMPLE_COVERAGE);
Shannon Woods53a94a82014-06-24 15:20:36 -0400561}
562
Geoff Lang0fbb6002015-04-16 11:11:53 -0400563GLclampf State::getSampleCoverageValue() const
Shannon Woods53a94a82014-06-24 15:20:36 -0400564{
Geoff Lang0fbb6002015-04-16 11:11:53 -0400565 return mSampleCoverageValue;
566}
Shannon Woods53a94a82014-06-24 15:20:36 -0400567
Geoff Lang0fbb6002015-04-16 11:11:53 -0400568bool State::getSampleCoverageInvert() const
569{
570 return mSampleCoverageInvert;
Shannon Woods53a94a82014-06-24 15:20:36 -0400571}
572
Jiawei Shaodb342272017-09-27 10:21:45 +0800573bool State::isSampleMaskEnabled() const
574{
575 return mSampleMask;
576}
577
578void State::setSampleMaskEnabled(bool enabled)
579{
580 mSampleMask = enabled;
581 mDirtyBits.set(DIRTY_BIT_SAMPLE_MASK_ENABLED);
582}
583
584void State::setSampleMaskParams(GLuint maskNumber, GLbitfield mask)
585{
586 ASSERT(maskNumber < mMaxSampleMaskWords);
587 mSampleMaskValues[maskNumber] = mask;
588 mDirtyBits.set(DIRTY_BIT_SAMPLE_MASK_WORD_0 + maskNumber);
589}
590
591GLbitfield State::getSampleMaskWord(GLuint maskNumber) const
592{
593 ASSERT(maskNumber < mMaxSampleMaskWords);
594 return mSampleMaskValues[maskNumber];
595}
596
597GLuint State::getMaxSampleMaskWords() const
598{
599 return mMaxSampleMaskWords;
600}
601
Sami Väisänen74c23472016-05-09 17:30:30 +0300602void State::setSampleAlphaToOne(bool enabled)
603{
604 mSampleAlphaToOne = enabled;
605 mDirtyBits.set(DIRTY_BIT_SAMPLE_ALPHA_TO_ONE);
606}
607
608bool State::isSampleAlphaToOneEnabled() const
609{
610 return mSampleAlphaToOne;
611}
612
613void State::setMultisampling(bool enabled)
614{
615 mMultiSampling = enabled;
616 mDirtyBits.set(DIRTY_BIT_MULTISAMPLING);
617}
618
619bool State::isMultisamplingEnabled() const
620{
621 return mMultiSampling;
622}
623
Shannon Woods53a94a82014-06-24 15:20:36 -0400624bool State::isScissorTestEnabled() const
625{
626 return mScissorTest;
627}
628
629void State::setScissorTest(bool enabled)
630{
631 mScissorTest = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400632 mDirtyBits.set(DIRTY_BIT_SCISSOR_TEST_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400633}
634
635void State::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
636{
637 mScissor.x = x;
638 mScissor.y = y;
639 mScissor.width = width;
640 mScissor.height = height;
Jamie Madill1b94d432015-08-07 13:23:23 -0400641 mDirtyBits.set(DIRTY_BIT_SCISSOR);
Shannon Woods53a94a82014-06-24 15:20:36 -0400642}
643
644const Rectangle &State::getScissor() const
645{
646 return mScissor;
647}
648
649bool State::isDitherEnabled() const
650{
651 return mBlend.dither;
652}
653
654void State::setDither(bool enabled)
655{
656 mBlend.dither = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400657 mDirtyBits.set(DIRTY_BIT_DITHER_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400658}
659
Jamie Madillb4b53c52015-02-03 15:22:48 -0500660bool State::isPrimitiveRestartEnabled() const
661{
662 return mPrimitiveRestart;
663}
664
665void State::setPrimitiveRestart(bool enabled)
666{
667 mPrimitiveRestart = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400668 mDirtyBits.set(DIRTY_BIT_PRIMITIVE_RESTART_ENABLED);
Jamie Madillb4b53c52015-02-03 15:22:48 -0500669}
670
Shannon Woods53a94a82014-06-24 15:20:36 -0400671void State::setEnableFeature(GLenum feature, bool enabled)
672{
673 switch (feature)
674 {
Sami Väisänen74c23472016-05-09 17:30:30 +0300675 case GL_MULTISAMPLE_EXT: setMultisampling(enabled); break;
676 case GL_SAMPLE_ALPHA_TO_ONE_EXT: setSampleAlphaToOne(enabled); break;
Shannon Woods53a94a82014-06-24 15:20:36 -0400677 case GL_CULL_FACE: setCullFace(enabled); break;
678 case GL_POLYGON_OFFSET_FILL: setPolygonOffsetFill(enabled); break;
679 case GL_SAMPLE_ALPHA_TO_COVERAGE: setSampleAlphaToCoverage(enabled); break;
680 case GL_SAMPLE_COVERAGE: setSampleCoverage(enabled); break;
681 case GL_SCISSOR_TEST: setScissorTest(enabled); break;
682 case GL_STENCIL_TEST: setStencilTest(enabled); break;
683 case GL_DEPTH_TEST: setDepthTest(enabled); break;
684 case GL_BLEND: setBlend(enabled); break;
685 case GL_DITHER: setDither(enabled); break;
Jamie Madillb4b53c52015-02-03 15:22:48 -0500686 case GL_PRIMITIVE_RESTART_FIXED_INDEX: setPrimitiveRestart(enabled); break;
Shannon Woods53a94a82014-06-24 15:20:36 -0400687 case GL_RASTERIZER_DISCARD: setRasterizerDiscard(enabled); break;
Geoff Lang3b573612016-10-31 14:08:10 -0400688 case GL_SAMPLE_MASK:
Jiawei Shaodb342272017-09-27 10:21:45 +0800689 setSampleMaskEnabled(enabled);
Geoff Lang3b573612016-10-31 14:08:10 -0400690 break;
Geoff Lang70d0f492015-12-10 17:45:46 -0500691 case GL_DEBUG_OUTPUT_SYNCHRONOUS:
692 mDebug.setOutputSynchronous(enabled);
693 break;
694 case GL_DEBUG_OUTPUT:
695 mDebug.setOutputEnabled(enabled);
696 break;
Geoff Lang1d2c41d2016-10-19 16:14:46 -0700697 case GL_FRAMEBUFFER_SRGB_EXT:
698 setFramebufferSRGB(enabled);
699 break;
Shannon Woods53a94a82014-06-24 15:20:36 -0400700 default: UNREACHABLE();
701 }
702}
703
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700704bool State::getEnableFeature(GLenum feature) const
Shannon Woods53a94a82014-06-24 15:20:36 -0400705{
706 switch (feature)
707 {
Sami Väisänen74c23472016-05-09 17:30:30 +0300708 case GL_MULTISAMPLE_EXT: return isMultisamplingEnabled();
709 case GL_SAMPLE_ALPHA_TO_ONE_EXT: return isSampleAlphaToOneEnabled();
Shannon Woods53a94a82014-06-24 15:20:36 -0400710 case GL_CULL_FACE: return isCullFaceEnabled();
711 case GL_POLYGON_OFFSET_FILL: return isPolygonOffsetFillEnabled();
712 case GL_SAMPLE_ALPHA_TO_COVERAGE: return isSampleAlphaToCoverageEnabled();
713 case GL_SAMPLE_COVERAGE: return isSampleCoverageEnabled();
714 case GL_SCISSOR_TEST: return isScissorTestEnabled();
715 case GL_STENCIL_TEST: return isStencilTestEnabled();
716 case GL_DEPTH_TEST: return isDepthTestEnabled();
717 case GL_BLEND: return isBlendEnabled();
718 case GL_DITHER: return isDitherEnabled();
Jamie Madillb4b53c52015-02-03 15:22:48 -0500719 case GL_PRIMITIVE_RESTART_FIXED_INDEX: return isPrimitiveRestartEnabled();
Shannon Woods53a94a82014-06-24 15:20:36 -0400720 case GL_RASTERIZER_DISCARD: return isRasterizerDiscardEnabled();
Geoff Langb5e997f2016-12-06 10:55:34 -0500721 case GL_SAMPLE_MASK:
Jiawei Shaodb342272017-09-27 10:21:45 +0800722 return isSampleMaskEnabled();
Geoff Lang70d0f492015-12-10 17:45:46 -0500723 case GL_DEBUG_OUTPUT_SYNCHRONOUS:
724 return mDebug.isOutputSynchronous();
725 case GL_DEBUG_OUTPUT:
726 return mDebug.isOutputEnabled();
Geoff Langf41a7152016-09-19 15:11:17 -0400727 case GL_BIND_GENERATES_RESOURCE_CHROMIUM:
728 return isBindGeneratesResourceEnabled();
Geoff Langfeb8c682017-02-13 16:07:35 -0500729 case GL_CLIENT_ARRAYS_ANGLE:
730 return areClientArraysEnabled();
Geoff Lang1d2c41d2016-10-19 16:14:46 -0700731 case GL_FRAMEBUFFER_SRGB_EXT:
732 return getFramebufferSRGB();
Geoff Langb433e872017-10-05 14:01:47 -0400733 case GL_ROBUST_RESOURCE_INITIALIZATION_ANGLE:
Jamie Madille08a1d32017-03-07 17:24:06 -0500734 return mRobustResourceInit;
Jamie Madillc43be722017-07-13 16:22:14 -0400735 case GL_PROGRAM_CACHE_ENABLED_ANGLE:
736 return mProgramBinaryCacheEnabled;
737
738 default:
739 UNREACHABLE();
740 return false;
Shannon Woods53a94a82014-06-24 15:20:36 -0400741 }
742}
743
744void State::setLineWidth(GLfloat width)
745{
746 mLineWidth = width;
Jamie Madill1b94d432015-08-07 13:23:23 -0400747 mDirtyBits.set(DIRTY_BIT_LINE_WIDTH);
Shannon Woods53a94a82014-06-24 15:20:36 -0400748}
749
Geoff Lang4b3f4162015-04-16 13:22:05 -0400750float State::getLineWidth() const
751{
752 return mLineWidth;
753}
754
Shannon Woods53a94a82014-06-24 15:20:36 -0400755void State::setGenerateMipmapHint(GLenum hint)
756{
757 mGenerateMipmapHint = hint;
Jamie Madill1b94d432015-08-07 13:23:23 -0400758 mDirtyBits.set(DIRTY_BIT_GENERATE_MIPMAP_HINT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400759}
760
761void State::setFragmentShaderDerivativeHint(GLenum hint)
762{
763 mFragmentShaderDerivativeHint = hint;
Jamie Madill1b94d432015-08-07 13:23:23 -0400764 mDirtyBits.set(DIRTY_BIT_SHADER_DERIVATIVE_HINT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400765 // TODO: Propagate the hint to shader translator so we can write
766 // ddx, ddx_coarse, or ddx_fine depending on the hint.
767 // Ignore for now. It is valid for implementations to ignore hint.
768}
769
Geoff Langf41a7152016-09-19 15:11:17 -0400770bool State::isBindGeneratesResourceEnabled() const
771{
772 return mBindGeneratesResource;
773}
774
Geoff Langfeb8c682017-02-13 16:07:35 -0500775bool State::areClientArraysEnabled() const
776{
777 return mClientArraysEnabled;
778}
779
Shannon Woods53a94a82014-06-24 15:20:36 -0400780void State::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
781{
782 mViewport.x = x;
783 mViewport.y = y;
784 mViewport.width = width;
785 mViewport.height = height;
Jamie Madill1b94d432015-08-07 13:23:23 -0400786 mDirtyBits.set(DIRTY_BIT_VIEWPORT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400787}
788
789const Rectangle &State::getViewport() const
790{
791 return mViewport;
792}
793
794void State::setActiveSampler(unsigned int active)
795{
796 mActiveSampler = active;
797}
798
799unsigned int State::getActiveSampler() const
800{
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700801 return static_cast<unsigned int>(mActiveSampler);
Shannon Woods53a94a82014-06-24 15:20:36 -0400802}
803
Jamie Madill4928b7c2017-06-20 12:57:39 -0400804void State::setSamplerTexture(const Context *context, GLenum type, Texture *texture)
Shannon Woods53a94a82014-06-24 15:20:36 -0400805{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400806 mSamplerTextures[type][mActiveSampler].set(context, texture);
Jamie Madill81c2e252017-09-09 23:32:46 -0400807 mDirtyBits.set(DIRTY_BIT_TEXTURE_BINDINGS);
808 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
Shannon Woods53a94a82014-06-24 15:20:36 -0400809}
810
Jamie Madillc29968b2016-01-20 11:17:23 -0500811Texture *State::getTargetTexture(GLenum target) const
812{
813 return getSamplerTexture(static_cast<unsigned int>(mActiveSampler), target);
814}
815
Geoff Lang76b10c92014-09-05 16:28:14 -0400816Texture *State::getSamplerTexture(unsigned int sampler, GLenum type) const
Shannon Woods53a94a82014-06-24 15:20:36 -0400817{
Jamie Madill5864ac22015-01-12 14:43:07 -0500818 const auto it = mSamplerTextures.find(type);
819 ASSERT(it != mSamplerTextures.end());
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400820 ASSERT(sampler < it->second.size());
Jamie Madill5864ac22015-01-12 14:43:07 -0500821 return it->second[sampler].get();
Shannon Woods53a94a82014-06-24 15:20:36 -0400822}
823
Geoff Lang76b10c92014-09-05 16:28:14 -0400824GLuint State::getSamplerTextureId(unsigned int sampler, GLenum type) const
Shannon Woods53a94a82014-06-24 15:20:36 -0400825{
Jamie Madill5864ac22015-01-12 14:43:07 -0500826 const auto it = mSamplerTextures.find(type);
827 ASSERT(it != mSamplerTextures.end());
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400828 ASSERT(sampler < it->second.size());
Jamie Madill5864ac22015-01-12 14:43:07 -0500829 return it->second[sampler].id();
Shannon Woods53a94a82014-06-24 15:20:36 -0400830}
831
Jamie Madilla02315b2017-02-23 14:14:47 -0500832void State::detachTexture(const Context *context, const TextureMap &zeroTextures, GLuint texture)
Shannon Woods53a94a82014-06-24 15:20:36 -0400833{
834 // Textures have a detach method on State rather than a simple
835 // removeBinding, because the zero/null texture objects are managed
836 // separately, and don't have to go through the Context's maps or
837 // the ResourceManager.
838
839 // [OpenGL ES 2.0.24] section 3.8 page 84:
840 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
841 // rebound to texture object zero
842
Corentin Walleza2257da2016-04-19 16:43:12 -0400843 for (auto &bindingVec : mSamplerTextures)
Shannon Woods53a94a82014-06-24 15:20:36 -0400844 {
Corentin Walleza2257da2016-04-19 16:43:12 -0400845 GLenum textureType = bindingVec.first;
846 TextureBindingVector &textureVector = bindingVec.second;
Jamie Madill81c2e252017-09-09 23:32:46 -0400847 for (BindingPointer<Texture> &binding : textureVector)
Shannon Woods53a94a82014-06-24 15:20:36 -0400848 {
Geoff Lang76b10c92014-09-05 16:28:14 -0400849 if (binding.id() == texture)
Shannon Woods53a94a82014-06-24 15:20:36 -0400850 {
Jamie Madill5864ac22015-01-12 14:43:07 -0500851 auto it = zeroTextures.find(textureType);
852 ASSERT(it != zeroTextures.end());
Jamie Madille6382c32014-11-07 15:05:26 -0500853 // Zero textures are the "default" textures instead of NULL
Jamie Madill4928b7c2017-06-20 12:57:39 -0400854 binding.set(context, it->second.get());
Jamie Madill81c2e252017-09-09 23:32:46 -0400855 mDirtyBits.set(DIRTY_BIT_TEXTURE_BINDINGS);
Shannon Woods53a94a82014-06-24 15:20:36 -0400856 }
857 }
858 }
859
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800860 for (auto &bindingImageUnit : mImageUnits)
861 {
862 if (bindingImageUnit.texture.id() == texture)
863 {
864 bindingImageUnit.texture.set(context, nullptr);
865 bindingImageUnit.level = 0;
866 bindingImageUnit.layered = false;
867 bindingImageUnit.layer = 0;
868 bindingImageUnit.access = GL_READ_ONLY;
869 bindingImageUnit.format = GL_R32UI;
870 break;
871 }
872 }
873
Shannon Woods53a94a82014-06-24 15:20:36 -0400874 // [OpenGL ES 2.0.24] section 4.4 page 112:
875 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
876 // as if Texture2DAttachment had been called, with a texture of 0, for each attachment point to which this
877 // image was attached in the currently bound framebuffer.
878
Jamie Madill8693bdb2017-09-02 15:32:14 -0400879 if (mReadFramebuffer && mReadFramebuffer->detachTexture(context, texture))
Shannon Woods53a94a82014-06-24 15:20:36 -0400880 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400881 mDirtyObjects.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
Shannon Woods53a94a82014-06-24 15:20:36 -0400882 }
883
Jamie Madill8693bdb2017-09-02 15:32:14 -0400884 if (mDrawFramebuffer && mDrawFramebuffer->detachTexture(context, texture))
Shannon Woods53a94a82014-06-24 15:20:36 -0400885 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400886 mDirtyObjects.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
Shannon Woods53a94a82014-06-24 15:20:36 -0400887 }
888}
889
Jamie Madill4928b7c2017-06-20 12:57:39 -0400890void State::initializeZeroTextures(const Context *context, const TextureMap &zeroTextures)
Jamie Madille6382c32014-11-07 15:05:26 -0500891{
892 for (const auto &zeroTexture : zeroTextures)
893 {
894 auto &samplerTextureArray = mSamplerTextures[zeroTexture.first];
895
896 for (size_t textureUnit = 0; textureUnit < samplerTextureArray.size(); ++textureUnit)
897 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400898 samplerTextureArray[textureUnit].set(context, zeroTexture.second.get());
Jamie Madille6382c32014-11-07 15:05:26 -0500899 }
900 }
901}
902
Jamie Madill4928b7c2017-06-20 12:57:39 -0400903void State::setSamplerBinding(const Context *context, GLuint textureUnit, Sampler *sampler)
Shannon Woods53a94a82014-06-24 15:20:36 -0400904{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400905 mSamplers[textureUnit].set(context, sampler);
Jamie Madill81c2e252017-09-09 23:32:46 -0400906 mDirtyBits.set(DIRTY_BIT_SAMPLER_BINDINGS);
907 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
Shannon Woods53a94a82014-06-24 15:20:36 -0400908}
909
910GLuint State::getSamplerId(GLuint textureUnit) const
911{
Geoff Lang76b10c92014-09-05 16:28:14 -0400912 ASSERT(textureUnit < mSamplers.size());
Shannon Woods53a94a82014-06-24 15:20:36 -0400913 return mSamplers[textureUnit].id();
914}
915
916Sampler *State::getSampler(GLuint textureUnit) const
917{
918 return mSamplers[textureUnit].get();
919}
920
Jamie Madill4928b7c2017-06-20 12:57:39 -0400921void State::detachSampler(const Context *context, GLuint sampler)
Shannon Woods53a94a82014-06-24 15:20:36 -0400922{
923 // [OpenGL ES 3.0.2] section 3.8.2 pages 123-124:
924 // If a sampler object that is currently bound to one or more texture units is
925 // deleted, it is as though BindSampler is called once for each texture unit to
926 // which the sampler is bound, with unit set to the texture unit and sampler set to zero.
Jamie Madill81c2e252017-09-09 23:32:46 -0400927 for (BindingPointer<Sampler> &samplerBinding : mSamplers)
Shannon Woods53a94a82014-06-24 15:20:36 -0400928 {
Geoff Lang76b10c92014-09-05 16:28:14 -0400929 if (samplerBinding.id() == sampler)
Shannon Woods53a94a82014-06-24 15:20:36 -0400930 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400931 samplerBinding.set(context, nullptr);
Jamie Madill81c2e252017-09-09 23:32:46 -0400932 mDirtyBits.set(DIRTY_BIT_SAMPLER_BINDINGS);
Shannon Woods53a94a82014-06-24 15:20:36 -0400933 }
934 }
935}
936
Jamie Madill4928b7c2017-06-20 12:57:39 -0400937void State::setRenderbufferBinding(const Context *context, Renderbuffer *renderbuffer)
Shannon Woods53a94a82014-06-24 15:20:36 -0400938{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400939 mRenderbuffer.set(context, renderbuffer);
Jamie Madill8693bdb2017-09-02 15:32:14 -0400940 mDirtyBits.set(DIRTY_BIT_RENDERBUFFER_BINDING);
Shannon Woods53a94a82014-06-24 15:20:36 -0400941}
942
943GLuint State::getRenderbufferId() const
944{
945 return mRenderbuffer.id();
946}
947
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700948Renderbuffer *State::getCurrentRenderbuffer() const
Shannon Woods53a94a82014-06-24 15:20:36 -0400949{
950 return mRenderbuffer.get();
951}
952
Jamie Madilla02315b2017-02-23 14:14:47 -0500953void State::detachRenderbuffer(const Context *context, GLuint renderbuffer)
Shannon Woods53a94a82014-06-24 15:20:36 -0400954{
955 // [OpenGL ES 2.0.24] section 4.4 page 109:
956 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
957 // had been executed with the target RENDERBUFFER and name of zero.
958
959 if (mRenderbuffer.id() == renderbuffer)
960 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400961 setRenderbufferBinding(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400962 }
963
964 // [OpenGL ES 2.0.24] section 4.4 page 111:
965 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
966 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
967 // point to which this image was attached in the currently bound framebuffer.
968
969 Framebuffer *readFramebuffer = mReadFramebuffer;
970 Framebuffer *drawFramebuffer = mDrawFramebuffer;
971
Jamie Madill8693bdb2017-09-02 15:32:14 -0400972 if (readFramebuffer && readFramebuffer->detachRenderbuffer(context, renderbuffer))
Shannon Woods53a94a82014-06-24 15:20:36 -0400973 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400974 mDirtyObjects.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
Shannon Woods53a94a82014-06-24 15:20:36 -0400975 }
976
977 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
978 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400979 if (drawFramebuffer->detachRenderbuffer(context, renderbuffer))
980 {
981 mDirtyObjects.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
982 }
Shannon Woods53a94a82014-06-24 15:20:36 -0400983 }
984
985}
986
987void State::setReadFramebufferBinding(Framebuffer *framebuffer)
988{
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500989 if (mReadFramebuffer == framebuffer)
990 return;
991
Shannon Woods53a94a82014-06-24 15:20:36 -0400992 mReadFramebuffer = framebuffer;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500993 mDirtyBits.set(DIRTY_BIT_READ_FRAMEBUFFER_BINDING);
994
995 if (mReadFramebuffer && mReadFramebuffer->hasAnyDirtyBit())
996 {
997 mDirtyObjects.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
998 }
Shannon Woods53a94a82014-06-24 15:20:36 -0400999}
1000
1001void State::setDrawFramebufferBinding(Framebuffer *framebuffer)
1002{
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001003 if (mDrawFramebuffer == framebuffer)
1004 return;
1005
Shannon Woods53a94a82014-06-24 15:20:36 -04001006 mDrawFramebuffer = framebuffer;
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001007 mDirtyBits.set(DIRTY_BIT_DRAW_FRAMEBUFFER_BINDING);
1008
1009 if (mDrawFramebuffer && mDrawFramebuffer->hasAnyDirtyBit())
1010 {
1011 mDirtyObjects.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
1012 }
Shannon Woods53a94a82014-06-24 15:20:36 -04001013}
1014
1015Framebuffer *State::getTargetFramebuffer(GLenum target) const
1016{
1017 switch (target)
1018 {
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001019 case GL_READ_FRAMEBUFFER_ANGLE:
1020 return mReadFramebuffer;
1021 case GL_DRAW_FRAMEBUFFER_ANGLE:
1022 case GL_FRAMEBUFFER:
1023 return mDrawFramebuffer;
1024 default:
1025 UNREACHABLE();
Yunchao Hef81ce4a2017-04-24 10:49:17 +08001026 return nullptr;
Shannon Woods53a94a82014-06-24 15:20:36 -04001027 }
1028}
1029
Jamie Madill51f40ec2016-06-15 14:06:00 -04001030Framebuffer *State::getReadFramebuffer() const
Shannon Woods53a94a82014-06-24 15:20:36 -04001031{
1032 return mReadFramebuffer;
1033}
1034
Jamie Madill51f40ec2016-06-15 14:06:00 -04001035Framebuffer *State::getDrawFramebuffer() const
Shannon Woods53a94a82014-06-24 15:20:36 -04001036{
1037 return mDrawFramebuffer;
1038}
1039
1040bool State::removeReadFramebufferBinding(GLuint framebuffer)
1041{
Jamie Madill77a72f62015-04-14 11:18:32 -04001042 if (mReadFramebuffer != nullptr &&
1043 mReadFramebuffer->id() == framebuffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001044 {
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001045 setReadFramebufferBinding(nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -04001046 return true;
1047 }
1048
1049 return false;
1050}
1051
1052bool State::removeDrawFramebufferBinding(GLuint framebuffer)
1053{
Jamie Madill77a72f62015-04-14 11:18:32 -04001054 if (mReadFramebuffer != nullptr &&
1055 mDrawFramebuffer->id() == framebuffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001056 {
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001057 setDrawFramebufferBinding(nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -04001058 return true;
1059 }
1060
1061 return false;
1062}
1063
1064void State::setVertexArrayBinding(VertexArray *vertexArray)
1065{
1066 mVertexArray = vertexArray;
Jamie Madill0b9e9032015-08-17 11:51:52 +00001067 mDirtyBits.set(DIRTY_BIT_VERTEX_ARRAY_BINDING);
Jamie Madillc9d442d2016-01-20 11:17:24 -05001068
1069 if (mVertexArray && mVertexArray->hasAnyDirtyBit())
1070 {
1071 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1072 }
Shannon Woods53a94a82014-06-24 15:20:36 -04001073}
1074
1075GLuint State::getVertexArrayId() const
1076{
Yunchao He4f285442017-04-21 12:15:49 +08001077 ASSERT(mVertexArray != nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -04001078 return mVertexArray->id();
1079}
1080
1081VertexArray *State::getVertexArray() const
1082{
Yunchao He4f285442017-04-21 12:15:49 +08001083 ASSERT(mVertexArray != nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -04001084 return mVertexArray;
1085}
1086
1087bool State::removeVertexArrayBinding(GLuint vertexArray)
1088{
1089 if (mVertexArray->id() == vertexArray)
1090 {
Yunchao Hed7297bf2017-04-19 15:27:10 +08001091 mVertexArray = nullptr;
Jamie Madill0b9e9032015-08-17 11:51:52 +00001092 mDirtyBits.set(DIRTY_BIT_VERTEX_ARRAY_BINDING);
Jamie Madillc9d442d2016-01-20 11:17:24 -05001093 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
Shannon Woods53a94a82014-06-24 15:20:36 -04001094 return true;
1095 }
1096
1097 return false;
1098}
1099
Jamie Madill4928b7c2017-06-20 12:57:39 -04001100void State::setElementArrayBuffer(const Context *context, Buffer *buffer)
Shao80957d92017-02-20 21:25:59 +08001101{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001102 getVertexArray()->setElementArrayBuffer(context, buffer);
Shao80957d92017-02-20 21:25:59 +08001103 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1104}
1105
Jamie Madill4928b7c2017-06-20 12:57:39 -04001106void State::bindVertexBuffer(const Context *context,
1107 GLuint bindingIndex,
Shao80957d92017-02-20 21:25:59 +08001108 Buffer *boundBuffer,
1109 GLintptr offset,
1110 GLsizei stride)
1111{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001112 getVertexArray()->bindVertexBuffer(context, bindingIndex, boundBuffer, offset, stride);
Shao80957d92017-02-20 21:25:59 +08001113 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1114}
1115
Shaodde78e82017-05-22 14:13:27 +08001116void State::setVertexAttribBinding(const Context *context, GLuint attribIndex, GLuint bindingIndex)
Shao80957d92017-02-20 21:25:59 +08001117{
Shaodde78e82017-05-22 14:13:27 +08001118 getVertexArray()->setVertexAttribBinding(context, attribIndex, bindingIndex);
Shao80957d92017-02-20 21:25:59 +08001119 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1120}
1121
1122void State::setVertexAttribFormat(GLuint attribIndex,
1123 GLint size,
1124 GLenum type,
1125 bool normalized,
1126 bool pureInteger,
1127 GLuint relativeOffset)
1128{
1129 getVertexArray()->setVertexAttribFormat(attribIndex, size, type, normalized, pureInteger,
1130 relativeOffset);
1131 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1132}
1133
1134void State::setVertexBindingDivisor(GLuint bindingIndex, GLuint divisor)
1135{
1136 getVertexArray()->setVertexBindingDivisor(bindingIndex, divisor);
1137 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1138}
1139
Jamie Madill6c1f6712017-02-14 19:08:04 -05001140void State::setProgram(const Context *context, Program *newProgram)
Shannon Woods53a94a82014-06-24 15:20:36 -04001141{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001142 if (mProgram != newProgram)
Shannon Woods53a94a82014-06-24 15:20:36 -04001143 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001144 if (mProgram)
1145 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001146 mProgram->release(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001147 }
1148
1149 mProgram = newProgram;
1150
1151 if (mProgram)
1152 {
1153 newProgram->addRef();
Jamie Madill81c2e252017-09-09 23:32:46 -04001154 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001155 }
Jamie Madilla779b612017-07-24 11:46:05 -04001156 mDirtyBits.set(DIRTY_BIT_PROGRAM_EXECUTABLE);
1157 mDirtyBits.set(DIRTY_BIT_PROGRAM_BINDING);
Shannon Woods53a94a82014-06-24 15:20:36 -04001158 }
1159}
1160
Geoff Lang7dd2e102014-11-10 15:19:26 -05001161Program *State::getProgram() const
Shannon Woods53a94a82014-06-24 15:20:36 -04001162{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001163 return mProgram;
Shannon Woods53a94a82014-06-24 15:20:36 -04001164}
1165
Jamie Madill4928b7c2017-06-20 12:57:39 -04001166void State::setTransformFeedbackBinding(const Context *context,
1167 TransformFeedback *transformFeedback)
Shannon Woods53a94a82014-06-24 15:20:36 -04001168{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001169 mTransformFeedback.set(context, transformFeedback);
Shannon Woods53a94a82014-06-24 15:20:36 -04001170}
1171
1172TransformFeedback *State::getCurrentTransformFeedback() const
1173{
1174 return mTransformFeedback.get();
1175}
1176
Gregoire Payen de La Garanderie52742022015-02-04 14:55:39 +00001177bool State::isTransformFeedbackActiveUnpaused() const
1178{
Jamie Madill81c2e252017-09-09 23:32:46 -04001179 TransformFeedback *curTransformFeedback = getCurrentTransformFeedback();
Geoff Langbb0a0bb2015-03-27 12:16:57 -04001180 return curTransformFeedback && curTransformFeedback->isActive() && !curTransformFeedback->isPaused();
Gregoire Payen de La Garanderie52742022015-02-04 14:55:39 +00001181}
1182
Jamie Madill4928b7c2017-06-20 12:57:39 -04001183bool State::removeTransformFeedbackBinding(const Context *context, GLuint transformFeedback)
Shannon Woods53a94a82014-06-24 15:20:36 -04001184{
1185 if (mTransformFeedback.id() == transformFeedback)
1186 {
Jamie Madill4928b7c2017-06-20 12:57:39 -04001187 mTransformFeedback.set(context, nullptr);
Corentin Walleza2257da2016-04-19 16:43:12 -04001188 return true;
Shannon Woods53a94a82014-06-24 15:20:36 -04001189 }
Corentin Walleza2257da2016-04-19 16:43:12 -04001190
1191 return false;
Shannon Woods53a94a82014-06-24 15:20:36 -04001192}
1193
Yunchao Hea336b902017-08-02 16:05:21 +08001194void State::setProgramPipelineBinding(const Context *context, ProgramPipeline *pipeline)
1195{
1196 mProgramPipeline.set(context, pipeline);
1197}
1198
1199void State::detachProgramPipeline(const Context *context, GLuint pipeline)
1200{
1201 mProgramPipeline.set(context, nullptr);
1202}
1203
Olli Etuahobbf1c102016-06-28 13:31:33 +03001204bool State::isQueryActive(const GLenum type) const
Shannon Woods53a94a82014-06-24 15:20:36 -04001205{
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001206 for (auto &iter : mActiveQueries)
Shannon Woods53a94a82014-06-24 15:20:36 -04001207 {
Olli Etuahobbf1c102016-06-28 13:31:33 +03001208 const Query *query = iter.second.get();
1209 if (query != nullptr && ActiveQueryType(query->getType()) == ActiveQueryType(type))
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001210 {
1211 return true;
1212 }
1213 }
1214
1215 return false;
1216}
1217
1218bool State::isQueryActive(Query *query) const
1219{
1220 for (auto &iter : mActiveQueries)
1221 {
1222 if (iter.second.get() == query)
Shannon Woods53a94a82014-06-24 15:20:36 -04001223 {
1224 return true;
1225 }
1226 }
1227
1228 return false;
1229}
1230
Jamie Madill4928b7c2017-06-20 12:57:39 -04001231void State::setActiveQuery(const Context *context, GLenum target, Query *query)
Shannon Woods53a94a82014-06-24 15:20:36 -04001232{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001233 mActiveQueries[target].set(context, query);
Shannon Woods53a94a82014-06-24 15:20:36 -04001234}
1235
1236GLuint State::getActiveQueryId(GLenum target) const
1237{
1238 const Query *query = getActiveQuery(target);
1239 return (query ? query->id() : 0u);
1240}
1241
1242Query *State::getActiveQuery(GLenum target) const
1243{
Jamie Madill5864ac22015-01-12 14:43:07 -05001244 const auto it = mActiveQueries.find(target);
Shannon Woods53a94a82014-06-24 15:20:36 -04001245
Jamie Madill5864ac22015-01-12 14:43:07 -05001246 // All query types should already exist in the activeQueries map
1247 ASSERT(it != mActiveQueries.end());
1248
1249 return it->second.get();
Shannon Woods53a94a82014-06-24 15:20:36 -04001250}
1251
Jamie Madill4928b7c2017-06-20 12:57:39 -04001252void State::setArrayBufferBinding(const Context *context, Buffer *buffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001253{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001254 mArrayBuffer.set(context, buffer);
Shannon Woods53a94a82014-06-24 15:20:36 -04001255}
1256
1257GLuint State::getArrayBufferId() const
1258{
1259 return mArrayBuffer.id();
1260}
1261
Jamie Madill4928b7c2017-06-20 12:57:39 -04001262void State::setDrawIndirectBufferBinding(const Context *context, Buffer *buffer)
Jiajia Qin9d7d0b12016-11-29 16:30:31 +08001263{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001264 mDrawIndirectBuffer.set(context, buffer);
Jiajia Qin9d7d0b12016-11-29 16:30:31 +08001265 mDirtyBits.set(DIRTY_BIT_DRAW_INDIRECT_BUFFER_BINDING);
1266}
1267
Jamie Madill4928b7c2017-06-20 12:57:39 -04001268void State::setGenericUniformBufferBinding(const Context *context, Buffer *buffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001269{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001270 mGenericUniformBuffer.set(context, buffer);
Shannon Woods53a94a82014-06-24 15:20:36 -04001271}
1272
Jamie Madill4928b7c2017-06-20 12:57:39 -04001273void State::setIndexedUniformBufferBinding(const Context *context,
1274 GLuint index,
1275 Buffer *buffer,
1276 GLintptr offset,
1277 GLsizeiptr size)
Shannon Woods53a94a82014-06-24 15:20:36 -04001278{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001279 mUniformBuffers[index].set(context, buffer, offset, size);
Shannon Woods53a94a82014-06-24 15:20:36 -04001280}
1281
Geoff Lang5d124a62015-09-15 13:03:27 -04001282const OffsetBindingPointer<Buffer> &State::getIndexedUniformBuffer(size_t index) const
Shannon Woods53a94a82014-06-24 15:20:36 -04001283{
Shannon Woodsf3acaf92014-09-23 18:07:11 -04001284 ASSERT(static_cast<size_t>(index) < mUniformBuffers.size());
Geoff Lang5d124a62015-09-15 13:03:27 -04001285 return mUniformBuffers[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001286}
1287
Jamie Madill4928b7c2017-06-20 12:57:39 -04001288void State::setGenericAtomicCounterBufferBinding(const Context *context, Buffer *buffer)
Jiajia Qin6eafb042016-12-27 17:04:07 +08001289{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001290 mGenericAtomicCounterBuffer.set(context, buffer);
Jiajia Qin6eafb042016-12-27 17:04:07 +08001291}
1292
Jamie Madill4928b7c2017-06-20 12:57:39 -04001293void State::setIndexedAtomicCounterBufferBinding(const Context *context,
1294 GLuint index,
Jiajia Qin6eafb042016-12-27 17:04:07 +08001295 Buffer *buffer,
1296 GLintptr offset,
1297 GLsizeiptr size)
1298{
1299 ASSERT(static_cast<size_t>(index) < mAtomicCounterBuffers.size());
Jamie Madill4928b7c2017-06-20 12:57:39 -04001300 mAtomicCounterBuffers[index].set(context, buffer, offset, size);
Jiajia Qin6eafb042016-12-27 17:04:07 +08001301}
1302
1303const OffsetBindingPointer<Buffer> &State::getIndexedAtomicCounterBuffer(size_t index) const
1304{
1305 ASSERT(static_cast<size_t>(index) < mAtomicCounterBuffers.size());
1306 return mAtomicCounterBuffers[index];
1307}
1308
Jamie Madill4928b7c2017-06-20 12:57:39 -04001309void State::setGenericShaderStorageBufferBinding(const Context *context, Buffer *buffer)
Jiajia Qinf546e7d2017-03-27 14:12:59 +08001310{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001311 mGenericShaderStorageBuffer.set(context, buffer);
Jiajia Qinf546e7d2017-03-27 14:12:59 +08001312}
1313
Jamie Madill4928b7c2017-06-20 12:57:39 -04001314void State::setIndexedShaderStorageBufferBinding(const Context *context,
1315 GLuint index,
Jiajia Qinf546e7d2017-03-27 14:12:59 +08001316 Buffer *buffer,
1317 GLintptr offset,
1318 GLsizeiptr size)
1319{
1320 ASSERT(static_cast<size_t>(index) < mShaderStorageBuffers.size());
Jamie Madill4928b7c2017-06-20 12:57:39 -04001321 mShaderStorageBuffers[index].set(context, buffer, offset, size);
Jiajia Qinf546e7d2017-03-27 14:12:59 +08001322}
1323
1324const OffsetBindingPointer<Buffer> &State::getIndexedShaderStorageBuffer(size_t index) const
1325{
1326 ASSERT(static_cast<size_t>(index) < mShaderStorageBuffers.size());
1327 return mShaderStorageBuffers[index];
1328}
1329
Jamie Madill4928b7c2017-06-20 12:57:39 -04001330void State::setCopyReadBufferBinding(const Context *context, Buffer *buffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001331{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001332 mCopyReadBuffer.set(context, buffer);
Shannon Woods53a94a82014-06-24 15:20:36 -04001333}
1334
Jamie Madill4928b7c2017-06-20 12:57:39 -04001335void State::setCopyWriteBufferBinding(const Context *context, Buffer *buffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001336{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001337 mCopyWriteBuffer.set(context, buffer);
Shannon Woods53a94a82014-06-24 15:20:36 -04001338}
1339
Jamie Madill4928b7c2017-06-20 12:57:39 -04001340void State::setPixelPackBufferBinding(const Context *context, Buffer *buffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001341{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001342 mPack.pixelBuffer.set(context, buffer);
Corentin Wallezbbd663a2016-04-20 17:49:17 -04001343 mDirtyBits.set(DIRTY_BIT_PACK_BUFFER_BINDING);
Shannon Woods53a94a82014-06-24 15:20:36 -04001344}
1345
Jamie Madill4928b7c2017-06-20 12:57:39 -04001346void State::setPixelUnpackBufferBinding(const Context *context, Buffer *buffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001347{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001348 mUnpack.pixelBuffer.set(context, buffer);
Corentin Wallezbbd663a2016-04-20 17:49:17 -04001349 mDirtyBits.set(DIRTY_BIT_UNPACK_BUFFER_BINDING);
Shannon Woods53a94a82014-06-24 15:20:36 -04001350}
1351
1352Buffer *State::getTargetBuffer(GLenum target) const
1353{
1354 switch (target)
1355 {
1356 case GL_ARRAY_BUFFER: return mArrayBuffer.get();
1357 case GL_COPY_READ_BUFFER: return mCopyReadBuffer.get();
1358 case GL_COPY_WRITE_BUFFER: return mCopyWriteBuffer.get();
Jamie Madill8e344942015-07-09 14:22:07 -04001359 case GL_ELEMENT_ARRAY_BUFFER: return getVertexArray()->getElementArrayBuffer().get();
Shannon Woods53a94a82014-06-24 15:20:36 -04001360 case GL_PIXEL_PACK_BUFFER: return mPack.pixelBuffer.get();
1361 case GL_PIXEL_UNPACK_BUFFER: return mUnpack.pixelBuffer.get();
Geoff Lang045536b2015-03-27 15:17:18 -04001362 case GL_TRANSFORM_FEEDBACK_BUFFER: return mTransformFeedback->getGenericBuffer().get();
Shannon Woods53a94a82014-06-24 15:20:36 -04001363 case GL_UNIFORM_BUFFER: return mGenericUniformBuffer.get();
Geoff Langb5e997f2016-12-06 10:55:34 -05001364 case GL_ATOMIC_COUNTER_BUFFER:
Jiajia Qin6eafb042016-12-27 17:04:07 +08001365 return mGenericAtomicCounterBuffer.get();
Geoff Langb5e997f2016-12-06 10:55:34 -05001366 case GL_SHADER_STORAGE_BUFFER:
Jiajia Qinf546e7d2017-03-27 14:12:59 +08001367 return mGenericShaderStorageBuffer.get();
Geoff Langb5e997f2016-12-06 10:55:34 -05001368 case GL_DRAW_INDIRECT_BUFFER:
Jiajia Qin9d7d0b12016-11-29 16:30:31 +08001369 return mDrawIndirectBuffer.get();
Yunchao Hef81ce4a2017-04-24 10:49:17 +08001370 default:
1371 UNREACHABLE();
1372 return nullptr;
Shannon Woods53a94a82014-06-24 15:20:36 -04001373 }
1374}
1375
Jamie Madill4928b7c2017-06-20 12:57:39 -04001376void State::detachBuffer(const Context *context, GLuint bufferName)
Yuly Novikov5807a532015-12-03 13:01:22 -05001377{
Jiajia Qinf546e7d2017-03-27 14:12:59 +08001378 BindingPointer<Buffer> *buffers[] = {
1379 &mArrayBuffer, &mGenericAtomicCounterBuffer, &mCopyReadBuffer,
1380 &mCopyWriteBuffer, &mDrawIndirectBuffer, &mPack.pixelBuffer,
1381 &mUnpack.pixelBuffer, &mGenericUniformBuffer, &mGenericShaderStorageBuffer};
Yuly Novikov5807a532015-12-03 13:01:22 -05001382 for (auto buffer : buffers)
1383 {
1384 if (buffer->id() == bufferName)
1385 {
Jamie Madill4928b7c2017-06-20 12:57:39 -04001386 buffer->set(context, nullptr);
Yuly Novikov5807a532015-12-03 13:01:22 -05001387 }
1388 }
1389
1390 TransformFeedback *curTransformFeedback = getCurrentTransformFeedback();
1391 if (curTransformFeedback)
1392 {
Jamie Madill4928b7c2017-06-20 12:57:39 -04001393 curTransformFeedback->detachBuffer(context, bufferName);
Yuly Novikov5807a532015-12-03 13:01:22 -05001394 }
1395
Jamie Madill4928b7c2017-06-20 12:57:39 -04001396 getVertexArray()->detachBuffer(context, bufferName);
Yuly Novikov5807a532015-12-03 13:01:22 -05001397}
1398
Shannon Woods53a94a82014-06-24 15:20:36 -04001399void State::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
1400{
1401 getVertexArray()->enableAttribute(attribNum, enabled);
Jamie Madillc9d442d2016-01-20 11:17:24 -05001402 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
Shannon Woods53a94a82014-06-24 15:20:36 -04001403}
1404
1405void State::setVertexAttribf(GLuint index, const GLfloat values[4])
1406{
Shannon Woods23e05002014-09-22 19:07:27 -04001407 ASSERT(static_cast<size_t>(index) < mVertexAttribCurrentValues.size());
Shannon Woods53a94a82014-06-24 15:20:36 -04001408 mVertexAttribCurrentValues[index].setFloatValues(values);
Jamie Madill1e0bc3a2015-08-11 08:12:21 -04001409 mDirtyBits.set(DIRTY_BIT_CURRENT_VALUE_0 + index);
Shannon Woods53a94a82014-06-24 15:20:36 -04001410}
1411
1412void State::setVertexAttribu(GLuint index, const GLuint values[4])
1413{
Shannon Woods23e05002014-09-22 19:07:27 -04001414 ASSERT(static_cast<size_t>(index) < mVertexAttribCurrentValues.size());
Shannon Woods53a94a82014-06-24 15:20:36 -04001415 mVertexAttribCurrentValues[index].setUnsignedIntValues(values);
Jamie Madill1e0bc3a2015-08-11 08:12:21 -04001416 mDirtyBits.set(DIRTY_BIT_CURRENT_VALUE_0 + index);
Shannon Woods53a94a82014-06-24 15:20:36 -04001417}
1418
1419void State::setVertexAttribi(GLuint index, const GLint values[4])
1420{
Shannon Woods23e05002014-09-22 19:07:27 -04001421 ASSERT(static_cast<size_t>(index) < mVertexAttribCurrentValues.size());
Shannon Woods53a94a82014-06-24 15:20:36 -04001422 mVertexAttribCurrentValues[index].setIntValues(values);
Jamie Madill1e0bc3a2015-08-11 08:12:21 -04001423 mDirtyBits.set(DIRTY_BIT_CURRENT_VALUE_0 + index);
Shannon Woods53a94a82014-06-24 15:20:36 -04001424}
1425
Shaodde78e82017-05-22 14:13:27 +08001426void State::setVertexAttribPointer(const Context *context,
1427 unsigned int attribNum,
1428 Buffer *boundBuffer,
1429 GLint size,
1430 GLenum type,
1431 bool normalized,
1432 bool pureInteger,
1433 GLsizei stride,
1434 const void *pointer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001435{
Shaodde78e82017-05-22 14:13:27 +08001436 getVertexArray()->setVertexAttribPointer(context, attribNum, boundBuffer, size, type,
1437 normalized, pureInteger, stride, pointer);
Jamie Madillc9d442d2016-01-20 11:17:24 -05001438 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
Jamie Madill0b9e9032015-08-17 11:51:52 +00001439}
1440
Shaodde78e82017-05-22 14:13:27 +08001441void State::setVertexAttribDivisor(const Context *context, GLuint index, GLuint divisor)
Jamie Madill0b9e9032015-08-17 11:51:52 +00001442{
Shaodde78e82017-05-22 14:13:27 +08001443 getVertexArray()->setVertexAttribDivisor(context, index, divisor);
Jamie Madillc9d442d2016-01-20 11:17:24 -05001444 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
Shannon Woods53a94a82014-06-24 15:20:36 -04001445}
1446
Jamie Madill6de51852017-04-12 09:53:01 -04001447const VertexAttribCurrentValueData &State::getVertexAttribCurrentValue(size_t attribNum) const
Shannon Woods53a94a82014-06-24 15:20:36 -04001448{
Jamie Madill6de51852017-04-12 09:53:01 -04001449 ASSERT(attribNum < mVertexAttribCurrentValues.size());
Shannon Woods53a94a82014-06-24 15:20:36 -04001450 return mVertexAttribCurrentValues[attribNum];
1451}
1452
Shannon Woods53a94a82014-06-24 15:20:36 -04001453const void *State::getVertexAttribPointer(unsigned int attribNum) const
1454{
1455 return getVertexArray()->getVertexAttribute(attribNum).pointer;
1456}
1457
1458void State::setPackAlignment(GLint alignment)
1459{
1460 mPack.alignment = alignment;
Jamie Madill1b94d432015-08-07 13:23:23 -04001461 mDirtyBits.set(DIRTY_BIT_PACK_ALIGNMENT);
Shannon Woods53a94a82014-06-24 15:20:36 -04001462}
1463
1464GLint State::getPackAlignment() const
1465{
1466 return mPack.alignment;
1467}
1468
1469void State::setPackReverseRowOrder(bool reverseRowOrder)
1470{
1471 mPack.reverseRowOrder = reverseRowOrder;
Jamie Madill1b94d432015-08-07 13:23:23 -04001472 mDirtyBits.set(DIRTY_BIT_PACK_REVERSE_ROW_ORDER);
Shannon Woods53a94a82014-06-24 15:20:36 -04001473}
1474
1475bool State::getPackReverseRowOrder() const
1476{
1477 return mPack.reverseRowOrder;
1478}
1479
Minmin Gongadff67b2015-10-14 10:34:45 -04001480void State::setPackRowLength(GLint rowLength)
1481{
1482 mPack.rowLength = rowLength;
1483 mDirtyBits.set(DIRTY_BIT_PACK_ROW_LENGTH);
1484}
1485
1486GLint State::getPackRowLength() const
1487{
1488 return mPack.rowLength;
1489}
1490
1491void State::setPackSkipRows(GLint skipRows)
1492{
1493 mPack.skipRows = skipRows;
1494 mDirtyBits.set(DIRTY_BIT_PACK_SKIP_ROWS);
1495}
1496
1497GLint State::getPackSkipRows() const
1498{
1499 return mPack.skipRows;
1500}
1501
1502void State::setPackSkipPixels(GLint skipPixels)
1503{
1504 mPack.skipPixels = skipPixels;
1505 mDirtyBits.set(DIRTY_BIT_PACK_SKIP_PIXELS);
1506}
1507
1508GLint State::getPackSkipPixels() const
1509{
1510 return mPack.skipPixels;
1511}
1512
Shannon Woods53a94a82014-06-24 15:20:36 -04001513const PixelPackState &State::getPackState() const
1514{
1515 return mPack;
1516}
1517
Jamie Madill87de3622015-03-16 10:41:44 -04001518PixelPackState &State::getPackState()
1519{
1520 return mPack;
1521}
1522
Shannon Woods53a94a82014-06-24 15:20:36 -04001523void State::setUnpackAlignment(GLint alignment)
1524{
1525 mUnpack.alignment = alignment;
Jamie Madill1b94d432015-08-07 13:23:23 -04001526 mDirtyBits.set(DIRTY_BIT_UNPACK_ALIGNMENT);
Shannon Woods53a94a82014-06-24 15:20:36 -04001527}
1528
1529GLint State::getUnpackAlignment() const
1530{
1531 return mUnpack.alignment;
1532}
1533
Minmin Gongb8aee3b2015-01-27 14:42:36 -08001534void State::setUnpackRowLength(GLint rowLength)
1535{
1536 mUnpack.rowLength = rowLength;
Jamie Madill1b94d432015-08-07 13:23:23 -04001537 mDirtyBits.set(DIRTY_BIT_UNPACK_ROW_LENGTH);
Minmin Gongb8aee3b2015-01-27 14:42:36 -08001538}
1539
1540GLint State::getUnpackRowLength() const
1541{
1542 return mUnpack.rowLength;
1543}
1544
Minmin Gongadff67b2015-10-14 10:34:45 -04001545void State::setUnpackImageHeight(GLint imageHeight)
1546{
1547 mUnpack.imageHeight = imageHeight;
1548 mDirtyBits.set(DIRTY_BIT_UNPACK_IMAGE_HEIGHT);
1549}
1550
1551GLint State::getUnpackImageHeight() const
1552{
1553 return mUnpack.imageHeight;
1554}
1555
1556void State::setUnpackSkipImages(GLint skipImages)
1557{
1558 mUnpack.skipImages = skipImages;
1559 mDirtyBits.set(DIRTY_BIT_UNPACK_SKIP_IMAGES);
1560}
1561
1562GLint State::getUnpackSkipImages() const
1563{
1564 return mUnpack.skipImages;
1565}
1566
1567void State::setUnpackSkipRows(GLint skipRows)
1568{
1569 mUnpack.skipRows = skipRows;
1570 mDirtyBits.set(DIRTY_BIT_UNPACK_SKIP_ROWS);
1571}
1572
1573GLint State::getUnpackSkipRows() const
1574{
1575 return mUnpack.skipRows;
1576}
1577
1578void State::setUnpackSkipPixels(GLint skipPixels)
1579{
1580 mUnpack.skipPixels = skipPixels;
1581 mDirtyBits.set(DIRTY_BIT_UNPACK_SKIP_PIXELS);
1582}
1583
1584GLint State::getUnpackSkipPixels() const
1585{
1586 return mUnpack.skipPixels;
1587}
1588
Shannon Woods53a94a82014-06-24 15:20:36 -04001589const PixelUnpackState &State::getUnpackState() const
1590{
1591 return mUnpack;
1592}
1593
Jamie Madill67102f02015-03-16 10:41:42 -04001594PixelUnpackState &State::getUnpackState()
1595{
1596 return mUnpack;
1597}
1598
Geoff Lang70d0f492015-12-10 17:45:46 -05001599const Debug &State::getDebug() const
1600{
1601 return mDebug;
1602}
1603
1604Debug &State::getDebug()
1605{
1606 return mDebug;
1607}
1608
Sami Väisänena797e062016-05-12 15:23:40 +03001609void State::setCoverageModulation(GLenum components)
1610{
1611 mCoverageModulation = components;
1612 mDirtyBits.set(DIRTY_BIT_COVERAGE_MODULATION);
1613}
1614
1615GLenum State::getCoverageModulation() const
1616{
1617 return mCoverageModulation;
1618}
1619
Sami Väisänene45e53b2016-05-25 10:36:04 +03001620void State::loadPathRenderingMatrix(GLenum matrixMode, const GLfloat *matrix)
1621{
1622 if (matrixMode == GL_PATH_MODELVIEW_CHROMIUM)
1623 {
1624 memcpy(mPathMatrixMV, matrix, 16 * sizeof(GLfloat));
1625 mDirtyBits.set(DIRTY_BIT_PATH_RENDERING_MATRIX_MV);
1626 }
1627 else if (matrixMode == GL_PATH_PROJECTION_CHROMIUM)
1628 {
1629 memcpy(mPathMatrixProj, matrix, 16 * sizeof(GLfloat));
1630 mDirtyBits.set(DIRTY_BIT_PATH_RENDERING_MATRIX_PROJ);
1631 }
1632 else
1633 {
1634 UNREACHABLE();
1635 }
1636}
1637
1638const GLfloat *State::getPathRenderingMatrix(GLenum which) const
1639{
1640 if (which == GL_PATH_MODELVIEW_MATRIX_CHROMIUM)
1641 {
1642 return mPathMatrixMV;
1643 }
1644 else if (which == GL_PATH_PROJECTION_MATRIX_CHROMIUM)
1645 {
1646 return mPathMatrixProj;
1647 }
1648
1649 UNREACHABLE();
1650 return nullptr;
1651}
1652
1653void State::setPathStencilFunc(GLenum func, GLint ref, GLuint mask)
1654{
1655 mPathStencilFunc = func;
1656 mPathStencilRef = ref;
1657 mPathStencilMask = mask;
1658 mDirtyBits.set(DIRTY_BIT_PATH_RENDERING_STENCIL_STATE);
1659}
1660
1661GLenum State::getPathStencilFunc() const
1662{
1663 return mPathStencilFunc;
1664}
1665
1666GLint State::getPathStencilRef() const
1667{
1668 return mPathStencilRef;
1669}
1670
1671GLuint State::getPathStencilMask() const
1672{
1673 return mPathStencilMask;
1674}
1675
Geoff Lang1d2c41d2016-10-19 16:14:46 -07001676void State::setFramebufferSRGB(bool sRGB)
1677{
1678 mFramebufferSRGB = sRGB;
1679 mDirtyBits.set(DIRTY_BIT_FRAMEBUFFER_SRGB);
1680}
1681
1682bool State::getFramebufferSRGB() const
1683{
1684 return mFramebufferSRGB;
1685}
1686
Shannon Woods53a94a82014-06-24 15:20:36 -04001687void State::getBooleanv(GLenum pname, GLboolean *params)
1688{
1689 switch (pname)
1690 {
1691 case GL_SAMPLE_COVERAGE_INVERT: *params = mSampleCoverageInvert; break;
1692 case GL_DEPTH_WRITEMASK: *params = mDepthStencil.depthMask; break;
1693 case GL_COLOR_WRITEMASK:
1694 params[0] = mBlend.colorMaskRed;
1695 params[1] = mBlend.colorMaskGreen;
1696 params[2] = mBlend.colorMaskBlue;
1697 params[3] = mBlend.colorMaskAlpha;
1698 break;
Corentin Wallez2e568cf2017-09-18 17:05:22 -04001699 case GL_CULL_FACE:
1700 *params = mRasterizer.cullFace;
1701 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001702 case GL_POLYGON_OFFSET_FILL: *params = mRasterizer.polygonOffsetFill; break;
1703 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mBlend.sampleAlphaToCoverage; break;
1704 case GL_SAMPLE_COVERAGE: *params = mSampleCoverage; break;
Jiawei Shaodb342272017-09-27 10:21:45 +08001705 case GL_SAMPLE_MASK:
1706 *params = mSampleMask;
1707 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001708 case GL_SCISSOR_TEST: *params = mScissorTest; break;
1709 case GL_STENCIL_TEST: *params = mDepthStencil.stencilTest; break;
1710 case GL_DEPTH_TEST: *params = mDepthStencil.depthTest; break;
1711 case GL_BLEND: *params = mBlend.blend; break;
1712 case GL_DITHER: *params = mBlend.dither; break;
Geoff Langbb0a0bb2015-03-27 12:16:57 -04001713 case GL_TRANSFORM_FEEDBACK_ACTIVE: *params = getCurrentTransformFeedback()->isActive() ? GL_TRUE : GL_FALSE; break;
1714 case GL_TRANSFORM_FEEDBACK_PAUSED: *params = getCurrentTransformFeedback()->isPaused() ? GL_TRUE : GL_FALSE; break;
Jamie Madille2cd53d2015-10-27 11:15:46 -04001715 case GL_PRIMITIVE_RESTART_FIXED_INDEX:
1716 *params = mPrimitiveRestart;
1717 break;
Geoff Langab831f02015-12-01 09:39:10 -05001718 case GL_RASTERIZER_DISCARD:
1719 *params = isRasterizerDiscardEnabled() ? GL_TRUE : GL_FALSE;
1720 break;
Geoff Lang70d0f492015-12-10 17:45:46 -05001721 case GL_DEBUG_OUTPUT_SYNCHRONOUS:
1722 *params = mDebug.isOutputSynchronous() ? GL_TRUE : GL_FALSE;
1723 break;
1724 case GL_DEBUG_OUTPUT:
1725 *params = mDebug.isOutputEnabled() ? GL_TRUE : GL_FALSE;
1726 break;
Sami Väisänen74c23472016-05-09 17:30:30 +03001727 case GL_MULTISAMPLE_EXT:
1728 *params = mMultiSampling;
1729 break;
1730 case GL_SAMPLE_ALPHA_TO_ONE_EXT:
1731 *params = mSampleAlphaToOne;
1732 break;
Geoff Langf41a7152016-09-19 15:11:17 -04001733 case GL_BIND_GENERATES_RESOURCE_CHROMIUM:
1734 *params = isBindGeneratesResourceEnabled() ? GL_TRUE : GL_FALSE;
1735 break;
Geoff Langfeb8c682017-02-13 16:07:35 -05001736 case GL_CLIENT_ARRAYS_ANGLE:
1737 *params = areClientArraysEnabled() ? GL_TRUE : GL_FALSE;
1738 break;
Geoff Lang1d2c41d2016-10-19 16:14:46 -07001739 case GL_FRAMEBUFFER_SRGB_EXT:
1740 *params = getFramebufferSRGB() ? GL_TRUE : GL_FALSE;
1741 break;
Geoff Langb433e872017-10-05 14:01:47 -04001742 case GL_ROBUST_RESOURCE_INITIALIZATION_ANGLE:
Jamie Madille08a1d32017-03-07 17:24:06 -05001743 *params = mRobustResourceInit ? GL_TRUE : GL_FALSE;
1744 break;
Jamie Madillc43be722017-07-13 16:22:14 -04001745 case GL_PROGRAM_CACHE_ENABLED_ANGLE:
1746 *params = mProgramBinaryCacheEnabled ? GL_TRUE : GL_FALSE;
1747 break;
1748
Shannon Woods53a94a82014-06-24 15:20:36 -04001749 default:
1750 UNREACHABLE();
1751 break;
1752 }
1753}
1754
1755void State::getFloatv(GLenum pname, GLfloat *params)
1756{
1757 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1758 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1759 // GetIntegerv as its native query function. As it would require conversion in any
1760 // case, this should make no difference to the calling application.
1761 switch (pname)
1762 {
1763 case GL_LINE_WIDTH: *params = mLineWidth; break;
1764 case GL_SAMPLE_COVERAGE_VALUE: *params = mSampleCoverageValue; break;
1765 case GL_DEPTH_CLEAR_VALUE: *params = mDepthClearValue; break;
1766 case GL_POLYGON_OFFSET_FACTOR: *params = mRasterizer.polygonOffsetFactor; break;
1767 case GL_POLYGON_OFFSET_UNITS: *params = mRasterizer.polygonOffsetUnits; break;
1768 case GL_DEPTH_RANGE:
1769 params[0] = mNearZ;
1770 params[1] = mFarZ;
1771 break;
1772 case GL_COLOR_CLEAR_VALUE:
1773 params[0] = mColorClearValue.red;
1774 params[1] = mColorClearValue.green;
1775 params[2] = mColorClearValue.blue;
1776 params[3] = mColorClearValue.alpha;
1777 break;
1778 case GL_BLEND_COLOR:
1779 params[0] = mBlendColor.red;
1780 params[1] = mBlendColor.green;
1781 params[2] = mBlendColor.blue;
1782 params[3] = mBlendColor.alpha;
1783 break;
Sami Väisänen74c23472016-05-09 17:30:30 +03001784 case GL_MULTISAMPLE_EXT:
1785 *params = static_cast<GLfloat>(mMultiSampling);
1786 break;
1787 case GL_SAMPLE_ALPHA_TO_ONE_EXT:
1788 *params = static_cast<GLfloat>(mSampleAlphaToOne);
Sami Väisänena797e062016-05-12 15:23:40 +03001789 case GL_COVERAGE_MODULATION_CHROMIUM:
Jamie Madille2e406c2016-06-02 13:04:10 -04001790 params[0] = static_cast<GLfloat>(mCoverageModulation);
1791 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001792 default:
1793 UNREACHABLE();
1794 break;
1795 }
1796}
1797
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001798void State::getIntegerv(const Context *context, GLenum pname, GLint *params)
Shannon Woods53a94a82014-06-24 15:20:36 -04001799{
1800 if (pname >= GL_DRAW_BUFFER0_EXT && pname <= GL_DRAW_BUFFER15_EXT)
1801 {
1802 unsigned int colorAttachment = (pname - GL_DRAW_BUFFER0_EXT);
Shannon Woods2df6a602014-09-26 16:12:07 -04001803 ASSERT(colorAttachment < mMaxDrawBuffers);
Shannon Woods53a94a82014-06-24 15:20:36 -04001804 Framebuffer *framebuffer = mDrawFramebuffer;
1805 *params = framebuffer->getDrawBufferState(colorAttachment);
1806 return;
1807 }
1808
1809 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1810 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1811 // GetIntegerv as its native query function. As it would require conversion in any
1812 // case, this should make no difference to the calling application. You may find it in
1813 // State::getFloatv.
1814 switch (pname)
1815 {
1816 case GL_ARRAY_BUFFER_BINDING: *params = mArrayBuffer.id(); break;
Jiajia Qin9d7d0b12016-11-29 16:30:31 +08001817 case GL_DRAW_INDIRECT_BUFFER_BINDING:
1818 *params = mDrawIndirectBuffer.id();
1819 break;
Jamie Madill8e344942015-07-09 14:22:07 -04001820 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = getVertexArray()->getElementArrayBuffer().id(); break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001821 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1822 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mDrawFramebuffer->id(); break;
1823 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mReadFramebuffer->id(); break;
1824 case GL_RENDERBUFFER_BINDING: *params = mRenderbuffer.id(); break;
1825 case GL_VERTEX_ARRAY_BINDING: *params = mVertexArray->id(); break;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001826 case GL_CURRENT_PROGRAM: *params = mProgram ? mProgram->id() : 0; break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001827 case GL_PACK_ALIGNMENT: *params = mPack.alignment; break;
1828 case GL_PACK_REVERSE_ROW_ORDER_ANGLE: *params = mPack.reverseRowOrder; break;
Minmin Gongadff67b2015-10-14 10:34:45 -04001829 case GL_PACK_ROW_LENGTH:
1830 *params = mPack.rowLength;
1831 break;
1832 case GL_PACK_SKIP_ROWS:
1833 *params = mPack.skipRows;
1834 break;
1835 case GL_PACK_SKIP_PIXELS:
1836 *params = mPack.skipPixels;
1837 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001838 case GL_UNPACK_ALIGNMENT: *params = mUnpack.alignment; break;
Minmin Gongb8aee3b2015-01-27 14:42:36 -08001839 case GL_UNPACK_ROW_LENGTH: *params = mUnpack.rowLength; break;
Minmin Gongadff67b2015-10-14 10:34:45 -04001840 case GL_UNPACK_IMAGE_HEIGHT:
1841 *params = mUnpack.imageHeight;
1842 break;
1843 case GL_UNPACK_SKIP_IMAGES:
1844 *params = mUnpack.skipImages;
1845 break;
1846 case GL_UNPACK_SKIP_ROWS:
1847 *params = mUnpack.skipRows;
1848 break;
1849 case GL_UNPACK_SKIP_PIXELS:
1850 *params = mUnpack.skipPixels;
1851 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001852 case GL_GENERATE_MIPMAP_HINT: *params = mGenerateMipmapHint; break;
1853 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mFragmentShaderDerivativeHint; break;
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001854 case GL_ACTIVE_TEXTURE:
1855 *params = (static_cast<GLint>(mActiveSampler) + GL_TEXTURE0);
1856 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001857 case GL_STENCIL_FUNC: *params = mDepthStencil.stencilFunc; break;
1858 case GL_STENCIL_REF: *params = mStencilRef; break;
jchen10a99ed552017-09-22 08:10:32 +08001859 case GL_STENCIL_VALUE_MASK:
1860 *params = CastMaskValue(context, mDepthStencil.stencilMask);
1861 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001862 case GL_STENCIL_BACK_FUNC: *params = mDepthStencil.stencilBackFunc; break;
1863 case GL_STENCIL_BACK_REF: *params = mStencilBackRef; break;
jchen10a99ed552017-09-22 08:10:32 +08001864 case GL_STENCIL_BACK_VALUE_MASK:
1865 *params = CastMaskValue(context, mDepthStencil.stencilBackMask);
1866 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001867 case GL_STENCIL_FAIL: *params = mDepthStencil.stencilFail; break;
1868 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mDepthStencil.stencilPassDepthFail; break;
1869 case GL_STENCIL_PASS_DEPTH_PASS: *params = mDepthStencil.stencilPassDepthPass; break;
1870 case GL_STENCIL_BACK_FAIL: *params = mDepthStencil.stencilBackFail; break;
1871 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mDepthStencil.stencilBackPassDepthFail; break;
1872 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mDepthStencil.stencilBackPassDepthPass; break;
1873 case GL_DEPTH_FUNC: *params = mDepthStencil.depthFunc; break;
1874 case GL_BLEND_SRC_RGB: *params = mBlend.sourceBlendRGB; break;
1875 case GL_BLEND_SRC_ALPHA: *params = mBlend.sourceBlendAlpha; break;
1876 case GL_BLEND_DST_RGB: *params = mBlend.destBlendRGB; break;
1877 case GL_BLEND_DST_ALPHA: *params = mBlend.destBlendAlpha; break;
1878 case GL_BLEND_EQUATION_RGB: *params = mBlend.blendEquationRGB; break;
1879 case GL_BLEND_EQUATION_ALPHA: *params = mBlend.blendEquationAlpha; break;
jchen10a99ed552017-09-22 08:10:32 +08001880 case GL_STENCIL_WRITEMASK:
1881 *params = CastMaskValue(context, mDepthStencil.stencilWritemask);
1882 break;
1883 case GL_STENCIL_BACK_WRITEMASK:
1884 *params = CastMaskValue(context, mDepthStencil.stencilBackWritemask);
1885 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001886 case GL_STENCIL_CLEAR_VALUE: *params = mStencilClearValue; break;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001887 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1888 *params = mReadFramebuffer->getImplementationColorReadType(context);
1889 break;
1890 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1891 *params = mReadFramebuffer->getImplementationColorReadFormat(context);
1892 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001893 case GL_SAMPLE_BUFFERS:
1894 case GL_SAMPLES:
1895 {
Jamie Madill81c2e252017-09-09 23:32:46 -04001896 Framebuffer *framebuffer = mDrawFramebuffer;
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001897 if (framebuffer->checkStatus(context) == GL_FRAMEBUFFER_COMPLETE)
Shannon Woods53a94a82014-06-24 15:20:36 -04001898 {
1899 switch (pname)
1900 {
Jamie Madilla0016b72017-07-14 14:30:46 -04001901 case GL_SAMPLE_BUFFERS:
1902 if (framebuffer->getSamples(context) != 0)
1903 {
1904 *params = 1;
1905 }
1906 else
1907 {
1908 *params = 0;
1909 }
1910 break;
1911 case GL_SAMPLES:
1912 *params = framebuffer->getSamples(context);
1913 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001914 }
1915 }
1916 else
1917 {
1918 *params = 0;
1919 }
1920 }
1921 break;
1922 case GL_VIEWPORT:
1923 params[0] = mViewport.x;
1924 params[1] = mViewport.y;
1925 params[2] = mViewport.width;
1926 params[3] = mViewport.height;
1927 break;
1928 case GL_SCISSOR_BOX:
1929 params[0] = mScissor.x;
1930 params[1] = mScissor.y;
1931 params[2] = mScissor.width;
1932 params[3] = mScissor.height;
1933 break;
Corentin Wallez2e568cf2017-09-18 17:05:22 -04001934 case GL_CULL_FACE_MODE:
1935 *params = ToGLenum(mRasterizer.cullMode);
1936 break;
1937 case GL_FRONT_FACE:
1938 *params = mRasterizer.frontFace;
1939 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001940 case GL_RED_BITS:
1941 case GL_GREEN_BITS:
1942 case GL_BLUE_BITS:
1943 case GL_ALPHA_BITS:
1944 {
Jamie Madill81c2e252017-09-09 23:32:46 -04001945 Framebuffer *framebuffer = getDrawFramebuffer();
1946 const FramebufferAttachment *colorbuffer = framebuffer->getFirstColorbuffer();
Shannon Woods53a94a82014-06-24 15:20:36 -04001947
1948 if (colorbuffer)
1949 {
1950 switch (pname)
1951 {
1952 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1953 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1954 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1955 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1956 }
1957 }
1958 else
1959 {
1960 *params = 0;
1961 }
1962 }
1963 break;
1964 case GL_DEPTH_BITS:
1965 {
Jamie Madill81c2e252017-09-09 23:32:46 -04001966 const Framebuffer *framebuffer = getDrawFramebuffer();
1967 const FramebufferAttachment *depthbuffer = framebuffer->getDepthbuffer();
Shannon Woods53a94a82014-06-24 15:20:36 -04001968
1969 if (depthbuffer)
1970 {
1971 *params = depthbuffer->getDepthSize();
1972 }
1973 else
1974 {
1975 *params = 0;
1976 }
1977 }
1978 break;
1979 case GL_STENCIL_BITS:
1980 {
Jamie Madill81c2e252017-09-09 23:32:46 -04001981 const Framebuffer *framebuffer = getDrawFramebuffer();
1982 const FramebufferAttachment *stencilbuffer = framebuffer->getStencilbuffer();
Shannon Woods53a94a82014-06-24 15:20:36 -04001983
1984 if (stencilbuffer)
1985 {
1986 *params = stencilbuffer->getStencilSize();
1987 }
1988 else
1989 {
1990 *params = 0;
1991 }
1992 }
1993 break;
1994 case GL_TEXTURE_BINDING_2D:
Shannon Woods2df6a602014-09-26 16:12:07 -04001995 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001996 *params = getSamplerTextureId(static_cast<unsigned int>(mActiveSampler), GL_TEXTURE_2D);
Shannon Woods53a94a82014-06-24 15:20:36 -04001997 break;
Corentin Wallez13c0dd42017-07-04 18:27:01 -04001998 case GL_TEXTURE_BINDING_RECTANGLE_ANGLE:
1999 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
2000 *params = getSamplerTextureId(static_cast<unsigned int>(mActiveSampler),
2001 GL_TEXTURE_RECTANGLE_ANGLE);
2002 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002003 case GL_TEXTURE_BINDING_CUBE_MAP:
Shannon Woods2df6a602014-09-26 16:12:07 -04002004 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002005 *params =
2006 getSamplerTextureId(static_cast<unsigned int>(mActiveSampler), GL_TEXTURE_CUBE_MAP);
Shannon Woods53a94a82014-06-24 15:20:36 -04002007 break;
2008 case GL_TEXTURE_BINDING_3D:
Shannon Woods2df6a602014-09-26 16:12:07 -04002009 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002010 *params = getSamplerTextureId(static_cast<unsigned int>(mActiveSampler), GL_TEXTURE_3D);
Shannon Woods53a94a82014-06-24 15:20:36 -04002011 break;
2012 case GL_TEXTURE_BINDING_2D_ARRAY:
Shannon Woods2df6a602014-09-26 16:12:07 -04002013 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002014 *params =
2015 getSamplerTextureId(static_cast<unsigned int>(mActiveSampler), GL_TEXTURE_2D_ARRAY);
Shannon Woods53a94a82014-06-24 15:20:36 -04002016 break;
JiangYizhou24fe74c2017-07-06 16:56:50 +08002017 case GL_TEXTURE_BINDING_2D_MULTISAMPLE:
2018 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
2019 *params = getSamplerTextureId(static_cast<unsigned int>(mActiveSampler),
2020 GL_TEXTURE_2D_MULTISAMPLE);
2021 break;
John Bauman18319182016-09-28 14:22:27 -07002022 case GL_TEXTURE_BINDING_EXTERNAL_OES:
2023 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
2024 *params = getSamplerTextureId(static_cast<unsigned int>(mActiveSampler),
2025 GL_TEXTURE_EXTERNAL_OES);
2026 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002027 case GL_UNIFORM_BUFFER_BINDING:
2028 *params = mGenericUniformBuffer.id();
2029 break;
Frank Henigman22581ff2015-11-06 14:25:54 -05002030 case GL_TRANSFORM_FEEDBACK_BINDING:
Frank Henigmanb0f0b812015-11-21 17:49:29 -05002031 *params = mTransformFeedback.id();
Frank Henigman22581ff2015-11-06 14:25:54 -05002032 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002033 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING:
Geoff Lang045536b2015-03-27 15:17:18 -04002034 *params = mTransformFeedback->getGenericBuffer().id();
Shannon Woods53a94a82014-06-24 15:20:36 -04002035 break;
2036 case GL_COPY_READ_BUFFER_BINDING:
2037 *params = mCopyReadBuffer.id();
2038 break;
2039 case GL_COPY_WRITE_BUFFER_BINDING:
2040 *params = mCopyWriteBuffer.id();
2041 break;
2042 case GL_PIXEL_PACK_BUFFER_BINDING:
2043 *params = mPack.pixelBuffer.id();
2044 break;
2045 case GL_PIXEL_UNPACK_BUFFER_BINDING:
2046 *params = mUnpack.pixelBuffer.id();
2047 break;
Olli Etuaho86821db2016-03-04 12:05:47 +02002048 case GL_READ_BUFFER:
2049 *params = mReadFramebuffer->getReadBufferState();
2050 break;
2051 case GL_SAMPLER_BINDING:
2052 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
2053 *params = getSamplerId(static_cast<GLuint>(mActiveSampler));
2054 break;
Geoff Lang70d0f492015-12-10 17:45:46 -05002055 case GL_DEBUG_LOGGED_MESSAGES:
2056 *params = static_cast<GLint>(mDebug.getMessageCount());
2057 break;
2058 case GL_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH:
2059 *params = static_cast<GLint>(mDebug.getNextMessageLength());
2060 break;
2061 case GL_DEBUG_GROUP_STACK_DEPTH:
2062 *params = static_cast<GLint>(mDebug.getGroupStackDepth());
2063 break;
Sami Väisänen74c23472016-05-09 17:30:30 +03002064 case GL_MULTISAMPLE_EXT:
2065 *params = static_cast<GLint>(mMultiSampling);
2066 break;
2067 case GL_SAMPLE_ALPHA_TO_ONE_EXT:
2068 *params = static_cast<GLint>(mSampleAlphaToOne);
Sami Väisänena797e062016-05-12 15:23:40 +03002069 case GL_COVERAGE_MODULATION_CHROMIUM:
2070 *params = static_cast<GLint>(mCoverageModulation);
Sami Väisänen74c23472016-05-09 17:30:30 +03002071 break;
Jiajia Qin6eafb042016-12-27 17:04:07 +08002072 case GL_ATOMIC_COUNTER_BUFFER_BINDING:
2073 *params = mGenericAtomicCounterBuffer.id();
2074 break;
Jiajia Qinf546e7d2017-03-27 14:12:59 +08002075 case GL_SHADER_STORAGE_BUFFER_BINDING:
2076 *params = mGenericShaderStorageBuffer.id();
2077 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002078 default:
2079 UNREACHABLE();
2080 break;
2081 }
2082}
2083
Geoff Lang70d0f492015-12-10 17:45:46 -05002084void State::getPointerv(GLenum pname, void **params) const
2085{
2086 switch (pname)
2087 {
2088 case GL_DEBUG_CALLBACK_FUNCTION:
2089 *params = reinterpret_cast<void *>(mDebug.getCallback());
2090 break;
2091 case GL_DEBUG_CALLBACK_USER_PARAM:
2092 *params = const_cast<void *>(mDebug.getUserParam());
2093 break;
2094 default:
2095 UNREACHABLE();
2096 break;
2097 }
2098}
2099
Martin Radev66fb8202016-07-28 11:45:20 +03002100void State::getIntegeri_v(GLenum target, GLuint index, GLint *data)
Shannon Woods53a94a82014-06-24 15:20:36 -04002101{
2102 switch (target)
2103 {
2104 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002105 ASSERT(static_cast<size_t>(index) < mTransformFeedback->getIndexedBufferCount());
2106 *data = mTransformFeedback->getIndexedBuffer(index).id();
2107 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002108 case GL_UNIFORM_BUFFER_BINDING:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002109 ASSERT(static_cast<size_t>(index) < mUniformBuffers.size());
2110 *data = mUniformBuffers[index].id();
2111 break;
2112 case GL_ATOMIC_COUNTER_BUFFER_BINDING:
2113 ASSERT(static_cast<size_t>(index) < mAtomicCounterBuffers.size());
2114 *data = mAtomicCounterBuffers[index].id();
2115 break;
Jiajia Qinf546e7d2017-03-27 14:12:59 +08002116 case GL_SHADER_STORAGE_BUFFER_BINDING:
2117 ASSERT(static_cast<size_t>(index) < mShaderStorageBuffers.size());
2118 *data = mShaderStorageBuffers[index].id();
2119 break;
Shao80957d92017-02-20 21:25:59 +08002120 case GL_VERTEX_BINDING_BUFFER:
2121 ASSERT(static_cast<size_t>(index) < mVertexArray->getMaxBindings());
Martin Radevdd5f27e2017-06-07 10:17:09 +03002122 *data = mVertexArray->getVertexBinding(index).getBuffer().id();
Shao80957d92017-02-20 21:25:59 +08002123 break;
2124 case GL_VERTEX_BINDING_DIVISOR:
2125 ASSERT(static_cast<size_t>(index) < mVertexArray->getMaxBindings());
Martin Radevdd5f27e2017-06-07 10:17:09 +03002126 *data = mVertexArray->getVertexBinding(index).getDivisor();
Shao80957d92017-02-20 21:25:59 +08002127 break;
2128 case GL_VERTEX_BINDING_OFFSET:
2129 ASSERT(static_cast<size_t>(index) < mVertexArray->getMaxBindings());
Martin Radevdd5f27e2017-06-07 10:17:09 +03002130 *data = static_cast<GLuint>(mVertexArray->getVertexBinding(index).getOffset());
Shao80957d92017-02-20 21:25:59 +08002131 break;
2132 case GL_VERTEX_BINDING_STRIDE:
2133 ASSERT(static_cast<size_t>(index) < mVertexArray->getMaxBindings());
Martin Radevdd5f27e2017-06-07 10:17:09 +03002134 *data = mVertexArray->getVertexBinding(index).getStride();
Shao80957d92017-02-20 21:25:59 +08002135 break;
Jiawei Shaodb342272017-09-27 10:21:45 +08002136 case GL_SAMPLE_MASK_VALUE:
2137 ASSERT(static_cast<size_t>(index) < mSampleMaskValues.size());
2138 *data = mSampleMaskValues[index];
2139 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002140 default:
Martin Radev66fb8202016-07-28 11:45:20 +03002141 UNREACHABLE();
2142 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002143 }
Shannon Woods53a94a82014-06-24 15:20:36 -04002144}
2145
Martin Radev66fb8202016-07-28 11:45:20 +03002146void State::getInteger64i_v(GLenum target, GLuint index, GLint64 *data)
Shannon Woods53a94a82014-06-24 15:20:36 -04002147{
2148 switch (target)
2149 {
2150 case GL_TRANSFORM_FEEDBACK_BUFFER_START:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002151 ASSERT(static_cast<size_t>(index) < mTransformFeedback->getIndexedBufferCount());
2152 *data = mTransformFeedback->getIndexedBuffer(index).getOffset();
2153 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002154 case GL_TRANSFORM_FEEDBACK_BUFFER_SIZE:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002155 ASSERT(static_cast<size_t>(index) < mTransformFeedback->getIndexedBufferCount());
2156 *data = mTransformFeedback->getIndexedBuffer(index).getSize();
2157 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002158 case GL_UNIFORM_BUFFER_START:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002159 ASSERT(static_cast<size_t>(index) < mUniformBuffers.size());
2160 *data = mUniformBuffers[index].getOffset();
2161 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002162 case GL_UNIFORM_BUFFER_SIZE:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002163 ASSERT(static_cast<size_t>(index) < mUniformBuffers.size());
2164 *data = mUniformBuffers[index].getSize();
2165 break;
2166 case GL_ATOMIC_COUNTER_BUFFER_START:
2167 ASSERT(static_cast<size_t>(index) < mAtomicCounterBuffers.size());
2168 *data = mAtomicCounterBuffers[index].getOffset();
2169 break;
2170 case GL_ATOMIC_COUNTER_BUFFER_SIZE:
2171 ASSERT(static_cast<size_t>(index) < mAtomicCounterBuffers.size());
2172 *data = mAtomicCounterBuffers[index].getSize();
2173 break;
Jiajia Qinf546e7d2017-03-27 14:12:59 +08002174 case GL_SHADER_STORAGE_BUFFER_START:
2175 ASSERT(static_cast<size_t>(index) < mShaderStorageBuffers.size());
2176 *data = mShaderStorageBuffers[index].getOffset();
2177 break;
2178 case GL_SHADER_STORAGE_BUFFER_SIZE:
2179 ASSERT(static_cast<size_t>(index) < mShaderStorageBuffers.size());
2180 *data = mShaderStorageBuffers[index].getSize();
2181 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002182 default:
Martin Radev66fb8202016-07-28 11:45:20 +03002183 UNREACHABLE();
2184 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002185 }
Martin Radev66fb8202016-07-28 11:45:20 +03002186}
Shannon Woods53a94a82014-06-24 15:20:36 -04002187
Martin Radev66fb8202016-07-28 11:45:20 +03002188void State::getBooleani_v(GLenum target, GLuint index, GLboolean *data)
2189{
2190 UNREACHABLE();
Shannon Woods53a94a82014-06-24 15:20:36 -04002191}
2192
Jamie Madilld9ba4f72014-08-04 10:47:59 -04002193bool State::hasMappedBuffer(GLenum target) const
2194{
2195 if (target == GL_ARRAY_BUFFER)
2196 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +08002197 const VertexArray *vao = getVertexArray();
Jamie Madilleea3a6e2015-04-15 10:02:48 -04002198 const auto &vertexAttribs = vao->getVertexAttributes();
Jiawei-Shao2597fb62016-12-09 16:38:02 +08002199 const auto &vertexBindings = vao->getVertexBindings();
Jamie Madill8e344942015-07-09 14:22:07 -04002200 size_t maxEnabledAttrib = vao->getMaxEnabledAttribute();
Jamie Madillaebf9dd2015-04-28 12:39:07 -04002201 for (size_t attribIndex = 0; attribIndex < maxEnabledAttrib; attribIndex++)
Jamie Madilld9ba4f72014-08-04 10:47:59 -04002202 {
Jamie Madill81c2e252017-09-09 23:32:46 -04002203 const VertexAttribute &vertexAttrib = vertexAttribs[attribIndex];
Martin Radevdd5f27e2017-06-07 10:17:09 +03002204 auto *boundBuffer = vertexBindings[vertexAttrib.bindingIndex].getBuffer().get();
Jamie Madilld9ba4f72014-08-04 10:47:59 -04002205 if (vertexAttrib.enabled && boundBuffer && boundBuffer->isMapped())
2206 {
2207 return true;
2208 }
2209 }
2210
2211 return false;
2212 }
2213 else
2214 {
2215 Buffer *buffer = getTargetBuffer(target);
2216 return (buffer && buffer->isMapped());
2217 }
2218}
2219
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002220void State::syncDirtyObjects(const Context *context)
Jamie Madillc9d442d2016-01-20 11:17:24 -05002221{
2222 if (!mDirtyObjects.any())
2223 return;
2224
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002225 syncDirtyObjects(context, mDirtyObjects);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002226}
2227
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002228void State::syncDirtyObjects(const Context *context, const DirtyObjects &bitset)
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002229{
Jamie Madill6de51852017-04-12 09:53:01 -04002230 for (auto dirtyObject : bitset)
Jamie Madillc9d442d2016-01-20 11:17:24 -05002231 {
2232 switch (dirtyObject)
2233 {
2234 case DIRTY_OBJECT_READ_FRAMEBUFFER:
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002235 ASSERT(mReadFramebuffer);
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002236 mReadFramebuffer->syncState(context);
Jamie Madillc9d442d2016-01-20 11:17:24 -05002237 break;
2238 case DIRTY_OBJECT_DRAW_FRAMEBUFFER:
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002239 ASSERT(mDrawFramebuffer);
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002240 mDrawFramebuffer->syncState(context);
Jamie Madillc9d442d2016-01-20 11:17:24 -05002241 break;
2242 case DIRTY_OBJECT_VERTEX_ARRAY:
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002243 ASSERT(mVertexArray);
Jamie Madill06ef36b2017-09-09 23:32:46 -04002244 mVertexArray->syncState(context);
Jamie Madillc9d442d2016-01-20 11:17:24 -05002245 break;
Jamie Madill81c2e252017-09-09 23:32:46 -04002246 case DIRTY_OBJECT_PROGRAM_TEXTURES:
2247 syncProgramTextures(context);
2248 break;
2249
Jamie Madillc9d442d2016-01-20 11:17:24 -05002250 default:
2251 UNREACHABLE();
2252 break;
2253 }
2254 }
2255
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002256 mDirtyObjects &= ~bitset;
2257}
2258
Jamie Madill81c2e252017-09-09 23:32:46 -04002259void State::syncProgramTextures(const Context *context)
2260{
Jamie Madill81c2e252017-09-09 23:32:46 -04002261 // TODO(jmadill): Fine-grained updates.
2262 if (!mProgram)
2263 {
2264 return;
2265 }
2266
2267 ASSERT(mDirtyObjects[DIRTY_OBJECT_PROGRAM_TEXTURES]);
2268 mDirtyBits.set(DIRTY_BIT_TEXTURE_BINDINGS);
2269
Jamie Madill0f80ed82017-09-19 00:24:56 -04002270 ActiveTextureMask newActiveTextures;
2271
Jamie Madill81c2e252017-09-09 23:32:46 -04002272 for (const SamplerBinding &samplerBinding : mProgram->getSamplerBindings())
2273 {
2274 if (samplerBinding.unreferenced)
2275 continue;
2276
2277 GLenum textureType = samplerBinding.textureType;
2278 for (GLuint textureUnitIndex : samplerBinding.boundTextureUnits)
2279 {
2280 Texture *texture = getSamplerTexture(textureUnitIndex, textureType);
Jamie Madill06ef36b2017-09-09 23:32:46 -04002281 Sampler *sampler = getSampler(textureUnitIndex);
Jamie Madill0f80ed82017-09-19 00:24:56 -04002282 ASSERT(static_cast<size_t>(textureUnitIndex) < mCompleteTextureCache.size());
2283 ASSERT(static_cast<size_t>(textureUnitIndex) < newActiveTextures.size());
Jamie Madill81c2e252017-09-09 23:32:46 -04002284
Jamie Madill42975642017-10-12 12:31:51 -04002285 ASSERT(texture);
Jamie Madill81c2e252017-09-09 23:32:46 -04002286
Jamie Madill42975642017-10-12 12:31:51 -04002287 // Mark the texture binding bit as dirty if the texture completeness changes.
2288 // TODO(jmadill): Use specific dirty bit for completeness change.
2289 if (texture->isSamplerComplete(context, sampler) &&
2290 !mDrawFramebuffer->hasTextureAttachment(texture))
2291 {
2292 texture->syncState();
2293 mCompleteTextureCache[textureUnitIndex] = texture;
Jamie Madill81c2e252017-09-09 23:32:46 -04002294 }
Jamie Madill42975642017-10-12 12:31:51 -04002295 else
2296 {
2297 mCompleteTextureCache[textureUnitIndex] = nullptr;
2298 }
2299
2300 // Bind the texture unconditionally, to recieve completeness change notifications.
2301 mCompleteTextureBindings[textureUnitIndex].bind(texture->getDirtyChannel());
2302 newActiveTextures.set(textureUnitIndex);
2303 mCompleteTexturesMask.set(textureUnitIndex);
Jamie Madill81c2e252017-09-09 23:32:46 -04002304
Jamie Madill06ef36b2017-09-09 23:32:46 -04002305 if (sampler != nullptr)
2306 {
2307 sampler->syncState(context);
2308 }
Jamie Madill81c2e252017-09-09 23:32:46 -04002309 }
2310 }
Jamie Madill0f80ed82017-09-19 00:24:56 -04002311
2312 // Unset now missing textures.
2313 ActiveTextureMask negativeMask = mCompleteTexturesMask & ~newActiveTextures;
2314 if (negativeMask.any())
2315 {
2316 for (auto textureIndex : negativeMask)
2317 {
2318 mCompleteTextureBindings[textureIndex].reset();
2319 mCompleteTextureCache[textureIndex] = nullptr;
2320 mCompleteTexturesMask.reset(textureIndex);
2321 }
2322 }
Jamie Madill81c2e252017-09-09 23:32:46 -04002323}
2324
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002325void State::syncDirtyObject(const Context *context, GLenum target)
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002326{
2327 DirtyObjects localSet;
2328
2329 switch (target)
2330 {
2331 case GL_READ_FRAMEBUFFER:
2332 localSet.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
2333 break;
2334 case GL_DRAW_FRAMEBUFFER:
2335 localSet.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
2336 break;
2337 case GL_FRAMEBUFFER:
2338 localSet.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
2339 localSet.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
2340 break;
2341 case GL_VERTEX_ARRAY:
2342 localSet.set(DIRTY_OBJECT_VERTEX_ARRAY);
2343 break;
Jamie Madill81c2e252017-09-09 23:32:46 -04002344 case GL_TEXTURE:
2345 case GL_SAMPLER:
2346 case GL_PROGRAM:
2347 localSet.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
2348 break;
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002349 }
2350
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002351 syncDirtyObjects(context, localSet);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002352}
2353
2354void State::setObjectDirty(GLenum target)
2355{
2356 switch (target)
2357 {
2358 case GL_READ_FRAMEBUFFER:
2359 mDirtyObjects.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
2360 break;
2361 case GL_DRAW_FRAMEBUFFER:
2362 mDirtyObjects.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
2363 break;
2364 case GL_FRAMEBUFFER:
2365 mDirtyObjects.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
2366 mDirtyObjects.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
2367 break;
2368 case GL_VERTEX_ARRAY:
2369 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
2370 break;
Jamie Madill81c2e252017-09-09 23:32:46 -04002371 case GL_TEXTURE:
2372 case GL_SAMPLER:
Jamie Madill81c2e252017-09-09 23:32:46 -04002373 case GL_PROGRAM:
2374 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
2375 mDirtyBits.set(DIRTY_BIT_TEXTURE_BINDINGS);
2376 break;
Jamie Madilla779b612017-07-24 11:46:05 -04002377 }
2378}
2379
2380void State::onProgramExecutableChange(Program *program)
2381{
2382 // OpenGL Spec:
2383 // "If LinkProgram or ProgramBinary successfully re-links a program object
2384 // that was already in use as a result of a previous call to UseProgram, then the
2385 // generated executable code will be installed as part of the current rendering state."
2386 if (program->isLinked() && mProgram == program)
2387 {
2388 mDirtyBits.set(DIRTY_BIT_PROGRAM_EXECUTABLE);
Jamie Madill81c2e252017-09-09 23:32:46 -04002389 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002390 }
Shannon Woods53a94a82014-06-24 15:20:36 -04002391}
Jamie Madillc9d442d2016-01-20 11:17:24 -05002392
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002393void State::setImageUnit(const Context *context,
2394 GLuint unit,
2395 Texture *texture,
2396 GLint level,
2397 GLboolean layered,
2398 GLint layer,
2399 GLenum access,
2400 GLenum format)
2401{
2402 mImageUnits[unit].texture.set(context, texture);
2403 mImageUnits[unit].level = level;
2404 mImageUnits[unit].layered = layered;
2405 mImageUnits[unit].layer = layer;
2406 mImageUnits[unit].access = access;
2407 mImageUnits[unit].format = format;
2408}
2409
2410const ImageUnit &State::getImageUnit(GLuint unit) const
2411{
2412 return mImageUnits[unit];
2413}
2414
Jamie Madill81c2e252017-09-09 23:32:46 -04002415// Handle a dirty texture event.
Jamie Madill05b35b22017-10-03 09:01:44 -04002416void State::signal(size_t textureIndex, InitState initState)
Jamie Madill81c2e252017-09-09 23:32:46 -04002417{
2418 // Conservatively assume all textures are dirty.
2419 // TODO(jmadill): More fine-grained update.
2420 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
2421}
2422
Jamie Madill05b35b22017-10-03 09:01:44 -04002423Error State::clearUnclearedActiveTextures(const Context *context)
2424{
2425 if (!mRobustResourceInit)
2426 {
2427 return NoError();
2428 }
2429
2430 // TODO(jmadill): Investigate improving the speed here.
2431 for (Texture *texture : mCompleteTextureCache)
2432 {
2433 if (texture)
2434 {
2435 ANGLE_TRY(texture->ensureInitialized(context));
2436 }
2437 }
2438 return NoError();
2439}
2440
Jamie Madillc9d442d2016-01-20 11:17:24 -05002441} // namespace gl