blob: 32029f6daf46e3ad48cc0d21c9f84acadc848a61 [file] [log] [blame]
Shannon Woods53a94a82014-06-24 15:20:36 -04001//
2// Copyright (c) 2014 The ANGLE Project Authors. All rights reserved.
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// State.cpp: Implements the State class, encapsulating raw GL state.
8
Geoff Lang2b5420c2014-11-19 14:20:15 -05009#include "libANGLE/State.h"
Shannon Woods53a94a82014-06-24 15:20:36 -040010
Sami Väisänene45e53b2016-05-25 10:36:04 +030011#include <limits>
12#include <string.h>
13
Jamie Madill20e005b2017-04-07 14:19:22 -040014#include "common/bitset_utils.h"
Sami Väisänene45e53b2016-05-25 10:36:04 +030015#include "common/matrix_utils.h"
16#include "common/mathutil.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050017#include "libANGLE/Context.h"
18#include "libANGLE/Caps.h"
Geoff Lang70d0f492015-12-10 17:45:46 -050019#include "libANGLE/Debug.h"
Geoff Lang2b5420c2014-11-19 14:20:15 -050020#include "libANGLE/Framebuffer.h"
21#include "libANGLE/FramebufferAttachment.h"
22#include "libANGLE/Query.h"
23#include "libANGLE/VertexArray.h"
24#include "libANGLE/formatutils.h"
Shannon Woods53a94a82014-06-24 15:20:36 -040025
Olli Etuahobbf1c102016-06-28 13:31:33 +030026namespace
27{
28
29GLenum ActiveQueryType(const GLenum type)
30{
31 return (type == GL_ANY_SAMPLES_PASSED_CONSERVATIVE) ? GL_ANY_SAMPLES_PASSED : type;
32}
33
34} // anonymous namepace
35
Shannon Woods53a94a82014-06-24 15:20:36 -040036namespace gl
37{
Geoff Lang76b10c92014-09-05 16:28:14 -040038
Shannon Woods53a94a82014-06-24 15:20:36 -040039State::State()
Jamie Madille79b1e12015-11-04 16:36:37 -050040 : mMaxDrawBuffers(0),
41 mMaxCombinedTextureImageUnits(0),
42 mDepthClearValue(0),
43 mStencilClearValue(0),
44 mScissorTest(false),
45 mSampleCoverage(false),
46 mSampleCoverageValue(0),
47 mSampleCoverageInvert(false),
Jiawei Shaodb342272017-09-27 10:21:45 +080048 mSampleMask(false),
49 mMaxSampleMaskWords(0),
Jamie Madille79b1e12015-11-04 16:36:37 -050050 mStencilRef(0),
51 mStencilBackRef(0),
52 mLineWidth(0),
53 mGenerateMipmapHint(GL_NONE),
54 mFragmentShaderDerivativeHint(GL_NONE),
Geoff Langf41a7152016-09-19 15:11:17 -040055 mBindGeneratesResource(true),
Geoff Langfeb8c682017-02-13 16:07:35 -050056 mClientArraysEnabled(true),
Jamie Madille79b1e12015-11-04 16:36:37 -050057 mNearZ(0),
58 mFarZ(0),
59 mReadFramebuffer(nullptr),
60 mDrawFramebuffer(nullptr),
61 mProgram(nullptr),
62 mVertexArray(nullptr),
63 mActiveSampler(0),
Sami Väisänen74c23472016-05-09 17:30:30 +030064 mPrimitiveRestart(false),
65 mMultiSampling(false),
Geoff Lang1d2c41d2016-10-19 16:14:46 -070066 mSampleAlphaToOne(false),
Jamie Madille08a1d32017-03-07 17:24:06 -050067 mFramebufferSRGB(true),
Jamie Madillc43be722017-07-13 16:22:14 -040068 mRobustResourceInit(false),
69 mProgramBinaryCacheEnabled(false)
Shannon Woods53a94a82014-06-24 15:20:36 -040070{
Geoff Lang76b10c92014-09-05 16:28:14 -040071}
72
73State::~State()
74{
Geoff Lang76b10c92014-09-05 16:28:14 -040075}
76
Jamie Madill4928b7c2017-06-20 12:57:39 -040077void State::initialize(const Context *context,
Geoff Langf41a7152016-09-19 15:11:17 -040078 bool debug,
Geoff Langfeb8c682017-02-13 16:07:35 -050079 bool bindGeneratesResource,
Jamie Madille08a1d32017-03-07 17:24:06 -050080 bool clientArraysEnabled,
Jamie Madillc43be722017-07-13 16:22:14 -040081 bool robustResourceInit,
82 bool programBinaryCacheEnabled)
Geoff Lang76b10c92014-09-05 16:28:14 -040083{
Jamie Madill4928b7c2017-06-20 12:57:39 -040084 const Caps &caps = context->getCaps();
85 const Extensions &extensions = context->getExtensions();
86 const Version &clientVersion = context->getClientVersion();
87
Shannon Woods2df6a602014-09-26 16:12:07 -040088 mMaxDrawBuffers = caps.maxDrawBuffers;
89 mMaxCombinedTextureImageUnits = caps.maxCombinedTextureImageUnits;
Shannon Woods53a94a82014-06-24 15:20:36 -040090
Jamie Madillf75ab352015-03-16 10:46:52 -040091 setColorClearValue(0.0f, 0.0f, 0.0f, 0.0f);
Shannon Woods53a94a82014-06-24 15:20:36 -040092
93 mDepthClearValue = 1.0f;
94 mStencilClearValue = 0;
95
Shannon Woods53a94a82014-06-24 15:20:36 -040096 mScissorTest = false;
97 mScissor.x = 0;
98 mScissor.y = 0;
99 mScissor.width = 0;
100 mScissor.height = 0;
101
Shannon Woods53a94a82014-06-24 15:20:36 -0400102 mBlendColor.red = 0;
103 mBlendColor.green = 0;
104 mBlendColor.blue = 0;
105 mBlendColor.alpha = 0;
106
Shannon Woods53a94a82014-06-24 15:20:36 -0400107 mStencilRef = 0;
108 mStencilBackRef = 0;
109
110 mSampleCoverage = false;
111 mSampleCoverageValue = 1.0f;
112 mSampleCoverageInvert = false;
Jiawei Shaodb342272017-09-27 10:21:45 +0800113
114 mMaxSampleMaskWords = caps.maxSampleMaskWords;
115 mSampleMask = false;
116 mSampleMaskValues.fill(~GLbitfield(0));
117
Shannon Woods53a94a82014-06-24 15:20:36 -0400118 mGenerateMipmapHint = GL_DONT_CARE;
119 mFragmentShaderDerivativeHint = GL_DONT_CARE;
120
Geoff Langf41a7152016-09-19 15:11:17 -0400121 mBindGeneratesResource = bindGeneratesResource;
Geoff Langfeb8c682017-02-13 16:07:35 -0500122 mClientArraysEnabled = clientArraysEnabled;
Geoff Langf41a7152016-09-19 15:11:17 -0400123
Shannon Woods53a94a82014-06-24 15:20:36 -0400124 mLineWidth = 1.0f;
125
126 mViewport.x = 0;
127 mViewport.y = 0;
128 mViewport.width = 0;
129 mViewport.height = 0;
130 mNearZ = 0.0f;
131 mFarZ = 1.0f;
132
133 mBlend.colorMaskRed = true;
134 mBlend.colorMaskGreen = true;
135 mBlend.colorMaskBlue = true;
136 mBlend.colorMaskAlpha = true;
137
Geoff Lang76b10c92014-09-05 16:28:14 -0400138 mActiveSampler = 0;
139
Shannon Woods23e05002014-09-22 19:07:27 -0400140 mVertexAttribCurrentValues.resize(caps.maxVertexAttributes);
Shannon Woods53a94a82014-06-24 15:20:36 -0400141
Geoff Lang4dc3af02016-11-18 14:09:27 -0500142 mUniformBuffers.resize(caps.maxUniformBufferBindings);
Shannon Woodsf3acaf92014-09-23 18:07:11 -0400143
Geoff Lang76b10c92014-09-05 16:28:14 -0400144 mSamplerTextures[GL_TEXTURE_2D].resize(caps.maxCombinedTextureImageUnits);
145 mSamplerTextures[GL_TEXTURE_CUBE_MAP].resize(caps.maxCombinedTextureImageUnits);
Geoff Langeb66a6e2016-10-31 13:06:12 -0400146 if (clientVersion >= Version(3, 0))
Shannon Woods53a94a82014-06-24 15:20:36 -0400147 {
Geoff Lang76b10c92014-09-05 16:28:14 -0400148 // TODO: These could also be enabled via extension
149 mSamplerTextures[GL_TEXTURE_2D_ARRAY].resize(caps.maxCombinedTextureImageUnits);
150 mSamplerTextures[GL_TEXTURE_3D].resize(caps.maxCombinedTextureImageUnits);
Shannon Woods53a94a82014-06-24 15:20:36 -0400151 }
Geoff Lang3b573612016-10-31 14:08:10 -0400152 if (clientVersion >= Version(3, 1))
153 {
154 mSamplerTextures[GL_TEXTURE_2D_MULTISAMPLE].resize(caps.maxCombinedTextureImageUnits);
Jiajia Qin6eafb042016-12-27 17:04:07 +0800155
156 mAtomicCounterBuffers.resize(caps.maxAtomicCounterBufferBindings);
Jiajia Qinf546e7d2017-03-27 14:12:59 +0800157 mShaderStorageBuffers.resize(caps.maxShaderStorageBufferBindings);
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800158 mImageUnits.resize(caps.maxImageUnits);
Geoff Lang3b573612016-10-31 14:08:10 -0400159 }
Corentin Wallez13c0dd42017-07-04 18:27:01 -0400160 if (extensions.textureRectangle)
161 {
162 mSamplerTextures[GL_TEXTURE_RECTANGLE_ANGLE].resize(caps.maxCombinedTextureImageUnits);
163 }
Ian Ewellbda75592016-04-18 17:25:54 -0400164 if (extensions.eglImageExternal || extensions.eglStreamConsumerExternal)
165 {
166 mSamplerTextures[GL_TEXTURE_EXTERNAL_OES].resize(caps.maxCombinedTextureImageUnits);
167 }
Jamie Madill81c2e252017-09-09 23:32:46 -0400168 mCompleteTextureCache.resize(caps.maxCombinedTextureImageUnits, nullptr);
169 mCompleteTextureBindings.reserve(caps.maxCombinedTextureImageUnits);
170 for (uint32_t textureIndex = 0; textureIndex < caps.maxCombinedTextureImageUnits;
171 ++textureIndex)
172 {
173 mCompleteTextureBindings.emplace_back(OnAttachmentDirtyBinding(this, textureIndex));
174 }
Shannon Woods53a94a82014-06-24 15:20:36 -0400175
Geoff Lang76b10c92014-09-05 16:28:14 -0400176 mSamplers.resize(caps.maxCombinedTextureImageUnits);
Shannon Woods53a94a82014-06-24 15:20:36 -0400177
Jamie Madill4928b7c2017-06-20 12:57:39 -0400178 mActiveQueries[GL_ANY_SAMPLES_PASSED].set(context, nullptr);
179 mActiveQueries[GL_ANY_SAMPLES_PASSED_CONSERVATIVE].set(context, nullptr);
180 mActiveQueries[GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN].set(context, nullptr);
181 mActiveQueries[GL_TIME_ELAPSED_EXT].set(context, nullptr);
182 mActiveQueries[GL_COMMANDS_COMPLETED_CHROMIUM].set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400183
Ian Ewell3ffd78b2016-01-22 16:09:42 -0500184 mProgram = nullptr;
Shannon Woods53a94a82014-06-24 15:20:36 -0400185
Ian Ewell3ffd78b2016-01-22 16:09:42 -0500186 mReadFramebuffer = nullptr;
187 mDrawFramebuffer = nullptr;
Jamie Madillb4b53c52015-02-03 15:22:48 -0500188
189 mPrimitiveRestart = false;
Geoff Lang70d0f492015-12-10 17:45:46 -0500190
191 mDebug.setOutputEnabled(debug);
192 mDebug.setMaxLoggedMessages(extensions.maxDebugLoggedMessages);
Sami Väisänen74c23472016-05-09 17:30:30 +0300193
Geoff Lang488130e2017-09-27 13:53:11 -0400194 mMultiSampling = true;
195 mSampleAlphaToOne = false;
Sami Väisänena797e062016-05-12 15:23:40 +0300196
197 mCoverageModulation = GL_NONE;
Sami Väisänene45e53b2016-05-25 10:36:04 +0300198
199 angle::Matrix<GLfloat>::setToIdentity(mPathMatrixProj);
200 angle::Matrix<GLfloat>::setToIdentity(mPathMatrixMV);
201 mPathStencilFunc = GL_ALWAYS;
202 mPathStencilRef = 0;
203 mPathStencilMask = std::numeric_limits<GLuint>::max();
Jamie Madille08a1d32017-03-07 17:24:06 -0500204
205 mRobustResourceInit = robustResourceInit;
Jamie Madillc43be722017-07-13 16:22:14 -0400206 mProgramBinaryCacheEnabled = programBinaryCacheEnabled;
Shannon Woods53a94a82014-06-24 15:20:36 -0400207}
208
Jamie Madill6c1f6712017-02-14 19:08:04 -0500209void State::reset(const Context *context)
Shannon Woods53a94a82014-06-24 15:20:36 -0400210{
Jamie Madill8693bdb2017-09-02 15:32:14 -0400211 for (auto &bindingVec : mSamplerTextures)
Shannon Woods53a94a82014-06-24 15:20:36 -0400212 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400213 TextureBindingVector &textureVector = bindingVec.second;
Geoff Lang76b10c92014-09-05 16:28:14 -0400214 for (size_t textureIdx = 0; textureIdx < textureVector.size(); textureIdx++)
Shannon Woods53a94a82014-06-24 15:20:36 -0400215 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400216 textureVector[textureIdx].set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400217 }
218 }
Geoff Lang76b10c92014-09-05 16:28:14 -0400219 for (size_t samplerIdx = 0; samplerIdx < mSamplers.size(); samplerIdx++)
220 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400221 mSamplers[samplerIdx].set(context, nullptr);
Geoff Lang76b10c92014-09-05 16:28:14 -0400222 }
Shannon Woods53a94a82014-06-24 15:20:36 -0400223
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800224 for (auto &imageUnit : mImageUnits)
225 {
226 imageUnit.texture.set(context, nullptr);
227 imageUnit.level = 0;
228 imageUnit.layered = false;
229 imageUnit.layer = 0;
230 imageUnit.access = GL_READ_ONLY;
231 imageUnit.format = GL_R32UI;
232 }
233
Jamie Madill4928b7c2017-06-20 12:57:39 -0400234 mArrayBuffer.set(context, nullptr);
235 mDrawIndirectBuffer.set(context, nullptr);
236 mRenderbuffer.set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400237
Geoff Lang7dd2e102014-11-10 15:19:26 -0500238 if (mProgram)
239 {
Jamie Madill6c1f6712017-02-14 19:08:04 -0500240 mProgram->release(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500241 }
Yunchao Hed7297bf2017-04-19 15:27:10 +0800242 mProgram = nullptr;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500243
Yunchao Hea336b902017-08-02 16:05:21 +0800244 mProgramPipeline.set(context, nullptr);
245
Jamie Madill4928b7c2017-06-20 12:57:39 -0400246 mTransformFeedback.set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400247
248 for (State::ActiveQueryMap::iterator i = mActiveQueries.begin(); i != mActiveQueries.end(); i++)
249 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400250 i->second.set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400251 }
252
Jamie Madill4928b7c2017-06-20 12:57:39 -0400253 mGenericUniformBuffer.set(context, nullptr);
Shannon Woods8299bb02014-09-26 18:55:43 -0400254 for (BufferVector::iterator bufItr = mUniformBuffers.begin(); bufItr != mUniformBuffers.end(); ++bufItr)
Shannon Woods53a94a82014-06-24 15:20:36 -0400255 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400256 bufItr->set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400257 }
258
Jamie Madill4928b7c2017-06-20 12:57:39 -0400259 mCopyReadBuffer.set(context, nullptr);
260 mCopyWriteBuffer.set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400261
Jamie Madill4928b7c2017-06-20 12:57:39 -0400262 mPack.pixelBuffer.set(context, nullptr);
263 mUnpack.pixelBuffer.set(context, nullptr);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500264
Jamie Madill4928b7c2017-06-20 12:57:39 -0400265 mGenericAtomicCounterBuffer.set(context, nullptr);
Jiajia Qin6eafb042016-12-27 17:04:07 +0800266 for (auto &buf : mAtomicCounterBuffers)
267 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400268 buf.set(context, nullptr);
Jiajia Qin6eafb042016-12-27 17:04:07 +0800269 }
270
Jamie Madill4928b7c2017-06-20 12:57:39 -0400271 mGenericShaderStorageBuffer.set(context, nullptr);
Jiajia Qinf546e7d2017-03-27 14:12:59 +0800272 for (auto &buf : mShaderStorageBuffers)
273 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400274 buf.set(context, nullptr);
Jiajia Qinf546e7d2017-03-27 14:12:59 +0800275 }
276
Sami Väisänene45e53b2016-05-25 10:36:04 +0300277 angle::Matrix<GLfloat>::setToIdentity(mPathMatrixProj);
278 angle::Matrix<GLfloat>::setToIdentity(mPathMatrixMV);
279 mPathStencilFunc = GL_ALWAYS;
280 mPathStencilRef = 0;
281 mPathStencilMask = std::numeric_limits<GLuint>::max();
282
Jamie Madill1b94d432015-08-07 13:23:23 -0400283 // TODO(jmadill): Is this necessary?
284 setAllDirtyBits();
Shannon Woods53a94a82014-06-24 15:20:36 -0400285}
286
287const RasterizerState &State::getRasterizerState() const
288{
289 return mRasterizer;
290}
291
292const BlendState &State::getBlendState() const
293{
294 return mBlend;
295}
296
297const DepthStencilState &State::getDepthStencilState() const
298{
299 return mDepthStencil;
300}
301
Jamie Madillf75ab352015-03-16 10:46:52 -0400302void State::setColorClearValue(float red, float green, float blue, float alpha)
Shannon Woods53a94a82014-06-24 15:20:36 -0400303{
304 mColorClearValue.red = red;
305 mColorClearValue.green = green;
306 mColorClearValue.blue = blue;
307 mColorClearValue.alpha = alpha;
Jamie Madill1b94d432015-08-07 13:23:23 -0400308 mDirtyBits.set(DIRTY_BIT_CLEAR_COLOR);
Shannon Woods53a94a82014-06-24 15:20:36 -0400309}
310
Jamie Madillf75ab352015-03-16 10:46:52 -0400311void State::setDepthClearValue(float depth)
Shannon Woods53a94a82014-06-24 15:20:36 -0400312{
313 mDepthClearValue = depth;
Jamie Madill1b94d432015-08-07 13:23:23 -0400314 mDirtyBits.set(DIRTY_BIT_CLEAR_DEPTH);
Shannon Woods53a94a82014-06-24 15:20:36 -0400315}
316
Jamie Madillf75ab352015-03-16 10:46:52 -0400317void State::setStencilClearValue(int stencil)
Shannon Woods53a94a82014-06-24 15:20:36 -0400318{
319 mStencilClearValue = stencil;
Jamie Madill1b94d432015-08-07 13:23:23 -0400320 mDirtyBits.set(DIRTY_BIT_CLEAR_STENCIL);
Shannon Woods53a94a82014-06-24 15:20:36 -0400321}
322
Shannon Woods53a94a82014-06-24 15:20:36 -0400323void State::setColorMask(bool red, bool green, bool blue, bool alpha)
324{
325 mBlend.colorMaskRed = red;
326 mBlend.colorMaskGreen = green;
327 mBlend.colorMaskBlue = blue;
328 mBlend.colorMaskAlpha = alpha;
Jamie Madill1b94d432015-08-07 13:23:23 -0400329 mDirtyBits.set(DIRTY_BIT_COLOR_MASK);
Shannon Woods53a94a82014-06-24 15:20:36 -0400330}
331
332void State::setDepthMask(bool mask)
333{
334 mDepthStencil.depthMask = mask;
Jamie Madill1b94d432015-08-07 13:23:23 -0400335 mDirtyBits.set(DIRTY_BIT_DEPTH_MASK);
Shannon Woods53a94a82014-06-24 15:20:36 -0400336}
337
338bool State::isRasterizerDiscardEnabled() const
339{
340 return mRasterizer.rasterizerDiscard;
341}
342
343void State::setRasterizerDiscard(bool enabled)
344{
345 mRasterizer.rasterizerDiscard = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400346 mDirtyBits.set(DIRTY_BIT_RASTERIZER_DISCARD_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400347}
348
349bool State::isCullFaceEnabled() const
350{
351 return mRasterizer.cullFace;
352}
353
354void State::setCullFace(bool enabled)
355{
356 mRasterizer.cullFace = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400357 mDirtyBits.set(DIRTY_BIT_CULL_FACE_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400358}
359
360void State::setCullMode(GLenum mode)
361{
362 mRasterizer.cullMode = mode;
Jamie Madill1b94d432015-08-07 13:23:23 -0400363 mDirtyBits.set(DIRTY_BIT_CULL_FACE);
Shannon Woods53a94a82014-06-24 15:20:36 -0400364}
365
366void State::setFrontFace(GLenum front)
367{
368 mRasterizer.frontFace = front;
Jamie Madill1b94d432015-08-07 13:23:23 -0400369 mDirtyBits.set(DIRTY_BIT_FRONT_FACE);
Shannon Woods53a94a82014-06-24 15:20:36 -0400370}
371
372bool State::isDepthTestEnabled() const
373{
374 return mDepthStencil.depthTest;
375}
376
377void State::setDepthTest(bool enabled)
378{
379 mDepthStencil.depthTest = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400380 mDirtyBits.set(DIRTY_BIT_DEPTH_TEST_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400381}
382
383void State::setDepthFunc(GLenum depthFunc)
384{
385 mDepthStencil.depthFunc = depthFunc;
Jamie Madill1b94d432015-08-07 13:23:23 -0400386 mDirtyBits.set(DIRTY_BIT_DEPTH_FUNC);
Shannon Woods53a94a82014-06-24 15:20:36 -0400387}
388
389void State::setDepthRange(float zNear, float zFar)
390{
391 mNearZ = zNear;
392 mFarZ = zFar;
Jamie Madill1b94d432015-08-07 13:23:23 -0400393 mDirtyBits.set(DIRTY_BIT_DEPTH_RANGE);
Shannon Woods53a94a82014-06-24 15:20:36 -0400394}
395
Geoff Langd42f5b82015-04-16 14:03:29 -0400396float State::getNearPlane() const
Shannon Woods53a94a82014-06-24 15:20:36 -0400397{
Geoff Langd42f5b82015-04-16 14:03:29 -0400398 return mNearZ;
399}
400
401float State::getFarPlane() const
402{
403 return mFarZ;
Shannon Woods53a94a82014-06-24 15:20:36 -0400404}
405
406bool State::isBlendEnabled() const
407{
408 return mBlend.blend;
409}
410
411void State::setBlend(bool enabled)
412{
413 mBlend.blend = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400414 mDirtyBits.set(DIRTY_BIT_BLEND_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400415}
416
417void State::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
418{
419 mBlend.sourceBlendRGB = sourceRGB;
420 mBlend.destBlendRGB = destRGB;
421 mBlend.sourceBlendAlpha = sourceAlpha;
422 mBlend.destBlendAlpha = destAlpha;
Jamie Madill1b94d432015-08-07 13:23:23 -0400423 mDirtyBits.set(DIRTY_BIT_BLEND_FUNCS);
Shannon Woods53a94a82014-06-24 15:20:36 -0400424}
425
426void State::setBlendColor(float red, float green, float blue, float alpha)
427{
428 mBlendColor.red = red;
429 mBlendColor.green = green;
430 mBlendColor.blue = blue;
431 mBlendColor.alpha = alpha;
Jamie Madill1b94d432015-08-07 13:23:23 -0400432 mDirtyBits.set(DIRTY_BIT_BLEND_COLOR);
Shannon Woods53a94a82014-06-24 15:20:36 -0400433}
434
435void State::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
436{
437 mBlend.blendEquationRGB = rgbEquation;
438 mBlend.blendEquationAlpha = alphaEquation;
Jamie Madill1b94d432015-08-07 13:23:23 -0400439 mDirtyBits.set(DIRTY_BIT_BLEND_EQUATIONS);
Shannon Woods53a94a82014-06-24 15:20:36 -0400440}
441
442const ColorF &State::getBlendColor() const
443{
444 return mBlendColor;
445}
446
447bool State::isStencilTestEnabled() const
448{
449 return mDepthStencil.stencilTest;
450}
451
452void State::setStencilTest(bool enabled)
453{
454 mDepthStencil.stencilTest = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400455 mDirtyBits.set(DIRTY_BIT_STENCIL_TEST_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400456}
457
458void State::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
459{
460 mDepthStencil.stencilFunc = stencilFunc;
461 mStencilRef = (stencilRef > 0) ? stencilRef : 0;
462 mDepthStencil.stencilMask = stencilMask;
Jamie Madill1b94d432015-08-07 13:23:23 -0400463 mDirtyBits.set(DIRTY_BIT_STENCIL_FUNCS_FRONT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400464}
465
466void State::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
467{
468 mDepthStencil.stencilBackFunc = stencilBackFunc;
469 mStencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
470 mDepthStencil.stencilBackMask = stencilBackMask;
Jamie Madill1b94d432015-08-07 13:23:23 -0400471 mDirtyBits.set(DIRTY_BIT_STENCIL_FUNCS_BACK);
Shannon Woods53a94a82014-06-24 15:20:36 -0400472}
473
474void State::setStencilWritemask(GLuint stencilWritemask)
475{
476 mDepthStencil.stencilWritemask = stencilWritemask;
Jamie Madill1b94d432015-08-07 13:23:23 -0400477 mDirtyBits.set(DIRTY_BIT_STENCIL_WRITEMASK_FRONT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400478}
479
480void State::setStencilBackWritemask(GLuint stencilBackWritemask)
481{
482 mDepthStencil.stencilBackWritemask = stencilBackWritemask;
Jamie Madill1b94d432015-08-07 13:23:23 -0400483 mDirtyBits.set(DIRTY_BIT_STENCIL_WRITEMASK_BACK);
Shannon Woods53a94a82014-06-24 15:20:36 -0400484}
485
486void State::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
487{
488 mDepthStencil.stencilFail = stencilFail;
489 mDepthStencil.stencilPassDepthFail = stencilPassDepthFail;
490 mDepthStencil.stencilPassDepthPass = stencilPassDepthPass;
Jamie Madill1b94d432015-08-07 13:23:23 -0400491 mDirtyBits.set(DIRTY_BIT_STENCIL_OPS_FRONT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400492}
493
494void State::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
495{
496 mDepthStencil.stencilBackFail = stencilBackFail;
497 mDepthStencil.stencilBackPassDepthFail = stencilBackPassDepthFail;
498 mDepthStencil.stencilBackPassDepthPass = stencilBackPassDepthPass;
Jamie Madill1b94d432015-08-07 13:23:23 -0400499 mDirtyBits.set(DIRTY_BIT_STENCIL_OPS_BACK);
Shannon Woods53a94a82014-06-24 15:20:36 -0400500}
501
502GLint State::getStencilRef() const
503{
504 return mStencilRef;
505}
506
507GLint State::getStencilBackRef() const
508{
509 return mStencilBackRef;
510}
511
512bool State::isPolygonOffsetFillEnabled() const
513{
514 return mRasterizer.polygonOffsetFill;
515}
516
517void State::setPolygonOffsetFill(bool enabled)
518{
Jamie Madill1b94d432015-08-07 13:23:23 -0400519 mRasterizer.polygonOffsetFill = enabled;
520 mDirtyBits.set(DIRTY_BIT_POLYGON_OFFSET_FILL_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400521}
522
523void State::setPolygonOffsetParams(GLfloat factor, GLfloat units)
524{
525 // An application can pass NaN values here, so handle this gracefully
526 mRasterizer.polygonOffsetFactor = factor != factor ? 0.0f : factor;
527 mRasterizer.polygonOffsetUnits = units != units ? 0.0f : units;
Jamie Madill1b94d432015-08-07 13:23:23 -0400528 mDirtyBits.set(DIRTY_BIT_POLYGON_OFFSET);
Shannon Woods53a94a82014-06-24 15:20:36 -0400529}
530
531bool State::isSampleAlphaToCoverageEnabled() const
532{
533 return mBlend.sampleAlphaToCoverage;
534}
535
536void State::setSampleAlphaToCoverage(bool enabled)
537{
538 mBlend.sampleAlphaToCoverage = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400539 mDirtyBits.set(DIRTY_BIT_SAMPLE_ALPHA_TO_COVERAGE_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400540}
541
542bool State::isSampleCoverageEnabled() const
543{
544 return mSampleCoverage;
545}
546
547void State::setSampleCoverage(bool enabled)
548{
549 mSampleCoverage = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400550 mDirtyBits.set(DIRTY_BIT_SAMPLE_COVERAGE_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400551}
552
553void State::setSampleCoverageParams(GLclampf value, bool invert)
554{
555 mSampleCoverageValue = value;
556 mSampleCoverageInvert = invert;
Jamie Madill1b94d432015-08-07 13:23:23 -0400557 mDirtyBits.set(DIRTY_BIT_SAMPLE_COVERAGE);
Shannon Woods53a94a82014-06-24 15:20:36 -0400558}
559
Geoff Lang0fbb6002015-04-16 11:11:53 -0400560GLclampf State::getSampleCoverageValue() const
Shannon Woods53a94a82014-06-24 15:20:36 -0400561{
Geoff Lang0fbb6002015-04-16 11:11:53 -0400562 return mSampleCoverageValue;
563}
Shannon Woods53a94a82014-06-24 15:20:36 -0400564
Geoff Lang0fbb6002015-04-16 11:11:53 -0400565bool State::getSampleCoverageInvert() const
566{
567 return mSampleCoverageInvert;
Shannon Woods53a94a82014-06-24 15:20:36 -0400568}
569
Jiawei Shaodb342272017-09-27 10:21:45 +0800570bool State::isSampleMaskEnabled() const
571{
572 return mSampleMask;
573}
574
575void State::setSampleMaskEnabled(bool enabled)
576{
577 mSampleMask = enabled;
578 mDirtyBits.set(DIRTY_BIT_SAMPLE_MASK_ENABLED);
579}
580
581void State::setSampleMaskParams(GLuint maskNumber, GLbitfield mask)
582{
583 ASSERT(maskNumber < mMaxSampleMaskWords);
584 mSampleMaskValues[maskNumber] = mask;
585 mDirtyBits.set(DIRTY_BIT_SAMPLE_MASK_WORD_0 + maskNumber);
586}
587
588GLbitfield State::getSampleMaskWord(GLuint maskNumber) const
589{
590 ASSERT(maskNumber < mMaxSampleMaskWords);
591 return mSampleMaskValues[maskNumber];
592}
593
594GLuint State::getMaxSampleMaskWords() const
595{
596 return mMaxSampleMaskWords;
597}
598
Sami Väisänen74c23472016-05-09 17:30:30 +0300599void State::setSampleAlphaToOne(bool enabled)
600{
601 mSampleAlphaToOne = enabled;
602 mDirtyBits.set(DIRTY_BIT_SAMPLE_ALPHA_TO_ONE);
603}
604
605bool State::isSampleAlphaToOneEnabled() const
606{
607 return mSampleAlphaToOne;
608}
609
610void State::setMultisampling(bool enabled)
611{
612 mMultiSampling = enabled;
613 mDirtyBits.set(DIRTY_BIT_MULTISAMPLING);
614}
615
616bool State::isMultisamplingEnabled() const
617{
618 return mMultiSampling;
619}
620
Shannon Woods53a94a82014-06-24 15:20:36 -0400621bool State::isScissorTestEnabled() const
622{
623 return mScissorTest;
624}
625
626void State::setScissorTest(bool enabled)
627{
628 mScissorTest = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400629 mDirtyBits.set(DIRTY_BIT_SCISSOR_TEST_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400630}
631
632void State::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
633{
634 mScissor.x = x;
635 mScissor.y = y;
636 mScissor.width = width;
637 mScissor.height = height;
Jamie Madill1b94d432015-08-07 13:23:23 -0400638 mDirtyBits.set(DIRTY_BIT_SCISSOR);
Shannon Woods53a94a82014-06-24 15:20:36 -0400639}
640
641const Rectangle &State::getScissor() const
642{
643 return mScissor;
644}
645
646bool State::isDitherEnabled() const
647{
648 return mBlend.dither;
649}
650
651void State::setDither(bool enabled)
652{
653 mBlend.dither = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400654 mDirtyBits.set(DIRTY_BIT_DITHER_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400655}
656
Jamie Madillb4b53c52015-02-03 15:22:48 -0500657bool State::isPrimitiveRestartEnabled() const
658{
659 return mPrimitiveRestart;
660}
661
662void State::setPrimitiveRestart(bool enabled)
663{
664 mPrimitiveRestart = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400665 mDirtyBits.set(DIRTY_BIT_PRIMITIVE_RESTART_ENABLED);
Jamie Madillb4b53c52015-02-03 15:22:48 -0500666}
667
Shannon Woods53a94a82014-06-24 15:20:36 -0400668void State::setEnableFeature(GLenum feature, bool enabled)
669{
670 switch (feature)
671 {
Sami Väisänen74c23472016-05-09 17:30:30 +0300672 case GL_MULTISAMPLE_EXT: setMultisampling(enabled); break;
673 case GL_SAMPLE_ALPHA_TO_ONE_EXT: setSampleAlphaToOne(enabled); break;
Shannon Woods53a94a82014-06-24 15:20:36 -0400674 case GL_CULL_FACE: setCullFace(enabled); break;
675 case GL_POLYGON_OFFSET_FILL: setPolygonOffsetFill(enabled); break;
676 case GL_SAMPLE_ALPHA_TO_COVERAGE: setSampleAlphaToCoverage(enabled); break;
677 case GL_SAMPLE_COVERAGE: setSampleCoverage(enabled); break;
678 case GL_SCISSOR_TEST: setScissorTest(enabled); break;
679 case GL_STENCIL_TEST: setStencilTest(enabled); break;
680 case GL_DEPTH_TEST: setDepthTest(enabled); break;
681 case GL_BLEND: setBlend(enabled); break;
682 case GL_DITHER: setDither(enabled); break;
Jamie Madillb4b53c52015-02-03 15:22:48 -0500683 case GL_PRIMITIVE_RESTART_FIXED_INDEX: setPrimitiveRestart(enabled); break;
Shannon Woods53a94a82014-06-24 15:20:36 -0400684 case GL_RASTERIZER_DISCARD: setRasterizerDiscard(enabled); break;
Geoff Lang3b573612016-10-31 14:08:10 -0400685 case GL_SAMPLE_MASK:
Jiawei Shaodb342272017-09-27 10:21:45 +0800686 setSampleMaskEnabled(enabled);
Geoff Lang3b573612016-10-31 14:08:10 -0400687 break;
Geoff Lang70d0f492015-12-10 17:45:46 -0500688 case GL_DEBUG_OUTPUT_SYNCHRONOUS:
689 mDebug.setOutputSynchronous(enabled);
690 break;
691 case GL_DEBUG_OUTPUT:
692 mDebug.setOutputEnabled(enabled);
693 break;
Geoff Lang1d2c41d2016-10-19 16:14:46 -0700694 case GL_FRAMEBUFFER_SRGB_EXT:
695 setFramebufferSRGB(enabled);
696 break;
Shannon Woods53a94a82014-06-24 15:20:36 -0400697 default: UNREACHABLE();
698 }
699}
700
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700701bool State::getEnableFeature(GLenum feature) const
Shannon Woods53a94a82014-06-24 15:20:36 -0400702{
703 switch (feature)
704 {
Sami Väisänen74c23472016-05-09 17:30:30 +0300705 case GL_MULTISAMPLE_EXT: return isMultisamplingEnabled();
706 case GL_SAMPLE_ALPHA_TO_ONE_EXT: return isSampleAlphaToOneEnabled();
Shannon Woods53a94a82014-06-24 15:20:36 -0400707 case GL_CULL_FACE: return isCullFaceEnabled();
708 case GL_POLYGON_OFFSET_FILL: return isPolygonOffsetFillEnabled();
709 case GL_SAMPLE_ALPHA_TO_COVERAGE: return isSampleAlphaToCoverageEnabled();
710 case GL_SAMPLE_COVERAGE: return isSampleCoverageEnabled();
711 case GL_SCISSOR_TEST: return isScissorTestEnabled();
712 case GL_STENCIL_TEST: return isStencilTestEnabled();
713 case GL_DEPTH_TEST: return isDepthTestEnabled();
714 case GL_BLEND: return isBlendEnabled();
715 case GL_DITHER: return isDitherEnabled();
Jamie Madillb4b53c52015-02-03 15:22:48 -0500716 case GL_PRIMITIVE_RESTART_FIXED_INDEX: return isPrimitiveRestartEnabled();
Shannon Woods53a94a82014-06-24 15:20:36 -0400717 case GL_RASTERIZER_DISCARD: return isRasterizerDiscardEnabled();
Geoff Langb5e997f2016-12-06 10:55:34 -0500718 case GL_SAMPLE_MASK:
Jiawei Shaodb342272017-09-27 10:21:45 +0800719 return isSampleMaskEnabled();
Geoff Lang70d0f492015-12-10 17:45:46 -0500720 case GL_DEBUG_OUTPUT_SYNCHRONOUS:
721 return mDebug.isOutputSynchronous();
722 case GL_DEBUG_OUTPUT:
723 return mDebug.isOutputEnabled();
Geoff Langf41a7152016-09-19 15:11:17 -0400724 case GL_BIND_GENERATES_RESOURCE_CHROMIUM:
725 return isBindGeneratesResourceEnabled();
Geoff Langfeb8c682017-02-13 16:07:35 -0500726 case GL_CLIENT_ARRAYS_ANGLE:
727 return areClientArraysEnabled();
Geoff Lang1d2c41d2016-10-19 16:14:46 -0700728 case GL_FRAMEBUFFER_SRGB_EXT:
729 return getFramebufferSRGB();
Jamie Madille08a1d32017-03-07 17:24:06 -0500730 case GL_CONTEXT_ROBUST_RESOURCE_INITIALIZATION_ANGLE:
731 return mRobustResourceInit;
Jamie Madillc43be722017-07-13 16:22:14 -0400732 case GL_PROGRAM_CACHE_ENABLED_ANGLE:
733 return mProgramBinaryCacheEnabled;
734
735 default:
736 UNREACHABLE();
737 return false;
Shannon Woods53a94a82014-06-24 15:20:36 -0400738 }
739}
740
741void State::setLineWidth(GLfloat width)
742{
743 mLineWidth = width;
Jamie Madill1b94d432015-08-07 13:23:23 -0400744 mDirtyBits.set(DIRTY_BIT_LINE_WIDTH);
Shannon Woods53a94a82014-06-24 15:20:36 -0400745}
746
Geoff Lang4b3f4162015-04-16 13:22:05 -0400747float State::getLineWidth() const
748{
749 return mLineWidth;
750}
751
Shannon Woods53a94a82014-06-24 15:20:36 -0400752void State::setGenerateMipmapHint(GLenum hint)
753{
754 mGenerateMipmapHint = hint;
Jamie Madill1b94d432015-08-07 13:23:23 -0400755 mDirtyBits.set(DIRTY_BIT_GENERATE_MIPMAP_HINT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400756}
757
758void State::setFragmentShaderDerivativeHint(GLenum hint)
759{
760 mFragmentShaderDerivativeHint = hint;
Jamie Madill1b94d432015-08-07 13:23:23 -0400761 mDirtyBits.set(DIRTY_BIT_SHADER_DERIVATIVE_HINT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400762 // TODO: Propagate the hint to shader translator so we can write
763 // ddx, ddx_coarse, or ddx_fine depending on the hint.
764 // Ignore for now. It is valid for implementations to ignore hint.
765}
766
Geoff Langf41a7152016-09-19 15:11:17 -0400767bool State::isBindGeneratesResourceEnabled() const
768{
769 return mBindGeneratesResource;
770}
771
Geoff Langfeb8c682017-02-13 16:07:35 -0500772bool State::areClientArraysEnabled() const
773{
774 return mClientArraysEnabled;
775}
776
Shannon Woods53a94a82014-06-24 15:20:36 -0400777void State::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
778{
779 mViewport.x = x;
780 mViewport.y = y;
781 mViewport.width = width;
782 mViewport.height = height;
Jamie Madill1b94d432015-08-07 13:23:23 -0400783 mDirtyBits.set(DIRTY_BIT_VIEWPORT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400784}
785
786const Rectangle &State::getViewport() const
787{
788 return mViewport;
789}
790
791void State::setActiveSampler(unsigned int active)
792{
793 mActiveSampler = active;
794}
795
796unsigned int State::getActiveSampler() const
797{
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700798 return static_cast<unsigned int>(mActiveSampler);
Shannon Woods53a94a82014-06-24 15:20:36 -0400799}
800
Jamie Madill4928b7c2017-06-20 12:57:39 -0400801void State::setSamplerTexture(const Context *context, GLenum type, Texture *texture)
Shannon Woods53a94a82014-06-24 15:20:36 -0400802{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400803 mSamplerTextures[type][mActiveSampler].set(context, texture);
Jamie Madill81c2e252017-09-09 23:32:46 -0400804 mDirtyBits.set(DIRTY_BIT_TEXTURE_BINDINGS);
805 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
Shannon Woods53a94a82014-06-24 15:20:36 -0400806}
807
Jamie Madillc29968b2016-01-20 11:17:23 -0500808Texture *State::getTargetTexture(GLenum target) const
809{
810 return getSamplerTexture(static_cast<unsigned int>(mActiveSampler), target);
811}
812
Geoff Lang76b10c92014-09-05 16:28:14 -0400813Texture *State::getSamplerTexture(unsigned int sampler, GLenum type) const
Shannon Woods53a94a82014-06-24 15:20:36 -0400814{
Jamie Madill5864ac22015-01-12 14:43:07 -0500815 const auto it = mSamplerTextures.find(type);
816 ASSERT(it != mSamplerTextures.end());
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400817 ASSERT(sampler < it->second.size());
Jamie Madill5864ac22015-01-12 14:43:07 -0500818 return it->second[sampler].get();
Shannon Woods53a94a82014-06-24 15:20:36 -0400819}
820
Geoff Lang76b10c92014-09-05 16:28:14 -0400821GLuint State::getSamplerTextureId(unsigned int sampler, GLenum type) const
Shannon Woods53a94a82014-06-24 15:20:36 -0400822{
Jamie Madill5864ac22015-01-12 14:43:07 -0500823 const auto it = mSamplerTextures.find(type);
824 ASSERT(it != mSamplerTextures.end());
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400825 ASSERT(sampler < it->second.size());
Jamie Madill5864ac22015-01-12 14:43:07 -0500826 return it->second[sampler].id();
Shannon Woods53a94a82014-06-24 15:20:36 -0400827}
828
Jamie Madilla02315b2017-02-23 14:14:47 -0500829void State::detachTexture(const Context *context, const TextureMap &zeroTextures, GLuint texture)
Shannon Woods53a94a82014-06-24 15:20:36 -0400830{
831 // Textures have a detach method on State rather than a simple
832 // removeBinding, because the zero/null texture objects are managed
833 // separately, and don't have to go through the Context's maps or
834 // the ResourceManager.
835
836 // [OpenGL ES 2.0.24] section 3.8 page 84:
837 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
838 // rebound to texture object zero
839
Corentin Walleza2257da2016-04-19 16:43:12 -0400840 for (auto &bindingVec : mSamplerTextures)
Shannon Woods53a94a82014-06-24 15:20:36 -0400841 {
Corentin Walleza2257da2016-04-19 16:43:12 -0400842 GLenum textureType = bindingVec.first;
843 TextureBindingVector &textureVector = bindingVec.second;
Jamie Madill81c2e252017-09-09 23:32:46 -0400844 for (BindingPointer<Texture> &binding : textureVector)
Shannon Woods53a94a82014-06-24 15:20:36 -0400845 {
Geoff Lang76b10c92014-09-05 16:28:14 -0400846 if (binding.id() == texture)
Shannon Woods53a94a82014-06-24 15:20:36 -0400847 {
Jamie Madill5864ac22015-01-12 14:43:07 -0500848 auto it = zeroTextures.find(textureType);
849 ASSERT(it != zeroTextures.end());
Jamie Madille6382c32014-11-07 15:05:26 -0500850 // Zero textures are the "default" textures instead of NULL
Jamie Madill4928b7c2017-06-20 12:57:39 -0400851 binding.set(context, it->second.get());
Jamie Madill81c2e252017-09-09 23:32:46 -0400852 mDirtyBits.set(DIRTY_BIT_TEXTURE_BINDINGS);
Shannon Woods53a94a82014-06-24 15:20:36 -0400853 }
854 }
855 }
856
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800857 for (auto &bindingImageUnit : mImageUnits)
858 {
859 if (bindingImageUnit.texture.id() == texture)
860 {
861 bindingImageUnit.texture.set(context, nullptr);
862 bindingImageUnit.level = 0;
863 bindingImageUnit.layered = false;
864 bindingImageUnit.layer = 0;
865 bindingImageUnit.access = GL_READ_ONLY;
866 bindingImageUnit.format = GL_R32UI;
867 break;
868 }
869 }
870
Shannon Woods53a94a82014-06-24 15:20:36 -0400871 // [OpenGL ES 2.0.24] section 4.4 page 112:
872 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
873 // as if Texture2DAttachment had been called, with a texture of 0, for each attachment point to which this
874 // image was attached in the currently bound framebuffer.
875
Jamie Madill8693bdb2017-09-02 15:32:14 -0400876 if (mReadFramebuffer && mReadFramebuffer->detachTexture(context, texture))
Shannon Woods53a94a82014-06-24 15:20:36 -0400877 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400878 mDirtyObjects.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
Shannon Woods53a94a82014-06-24 15:20:36 -0400879 }
880
Jamie Madill8693bdb2017-09-02 15:32:14 -0400881 if (mDrawFramebuffer && mDrawFramebuffer->detachTexture(context, texture))
Shannon Woods53a94a82014-06-24 15:20:36 -0400882 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400883 mDirtyObjects.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
Shannon Woods53a94a82014-06-24 15:20:36 -0400884 }
885}
886
Jamie Madill4928b7c2017-06-20 12:57:39 -0400887void State::initializeZeroTextures(const Context *context, const TextureMap &zeroTextures)
Jamie Madille6382c32014-11-07 15:05:26 -0500888{
889 for (const auto &zeroTexture : zeroTextures)
890 {
891 auto &samplerTextureArray = mSamplerTextures[zeroTexture.first];
892
893 for (size_t textureUnit = 0; textureUnit < samplerTextureArray.size(); ++textureUnit)
894 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400895 samplerTextureArray[textureUnit].set(context, zeroTexture.second.get());
Jamie Madille6382c32014-11-07 15:05:26 -0500896 }
897 }
898}
899
Jamie Madill4928b7c2017-06-20 12:57:39 -0400900void State::setSamplerBinding(const Context *context, GLuint textureUnit, Sampler *sampler)
Shannon Woods53a94a82014-06-24 15:20:36 -0400901{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400902 mSamplers[textureUnit].set(context, sampler);
Jamie Madill81c2e252017-09-09 23:32:46 -0400903 mDirtyBits.set(DIRTY_BIT_SAMPLER_BINDINGS);
904 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
Shannon Woods53a94a82014-06-24 15:20:36 -0400905}
906
907GLuint State::getSamplerId(GLuint textureUnit) const
908{
Geoff Lang76b10c92014-09-05 16:28:14 -0400909 ASSERT(textureUnit < mSamplers.size());
Shannon Woods53a94a82014-06-24 15:20:36 -0400910 return mSamplers[textureUnit].id();
911}
912
913Sampler *State::getSampler(GLuint textureUnit) const
914{
915 return mSamplers[textureUnit].get();
916}
917
Jamie Madill4928b7c2017-06-20 12:57:39 -0400918void State::detachSampler(const Context *context, GLuint sampler)
Shannon Woods53a94a82014-06-24 15:20:36 -0400919{
920 // [OpenGL ES 3.0.2] section 3.8.2 pages 123-124:
921 // If a sampler object that is currently bound to one or more texture units is
922 // deleted, it is as though BindSampler is called once for each texture unit to
923 // which the sampler is bound, with unit set to the texture unit and sampler set to zero.
Jamie Madill81c2e252017-09-09 23:32:46 -0400924 for (BindingPointer<Sampler> &samplerBinding : mSamplers)
Shannon Woods53a94a82014-06-24 15:20:36 -0400925 {
Geoff Lang76b10c92014-09-05 16:28:14 -0400926 if (samplerBinding.id() == sampler)
Shannon Woods53a94a82014-06-24 15:20:36 -0400927 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400928 samplerBinding.set(context, nullptr);
Jamie Madill81c2e252017-09-09 23:32:46 -0400929 mDirtyBits.set(DIRTY_BIT_SAMPLER_BINDINGS);
Shannon Woods53a94a82014-06-24 15:20:36 -0400930 }
931 }
932}
933
Jamie Madill4928b7c2017-06-20 12:57:39 -0400934void State::setRenderbufferBinding(const Context *context, Renderbuffer *renderbuffer)
Shannon Woods53a94a82014-06-24 15:20:36 -0400935{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400936 mRenderbuffer.set(context, renderbuffer);
Jamie Madill8693bdb2017-09-02 15:32:14 -0400937 mDirtyBits.set(DIRTY_BIT_RENDERBUFFER_BINDING);
Shannon Woods53a94a82014-06-24 15:20:36 -0400938}
939
940GLuint State::getRenderbufferId() const
941{
942 return mRenderbuffer.id();
943}
944
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700945Renderbuffer *State::getCurrentRenderbuffer() const
Shannon Woods53a94a82014-06-24 15:20:36 -0400946{
947 return mRenderbuffer.get();
948}
949
Jamie Madilla02315b2017-02-23 14:14:47 -0500950void State::detachRenderbuffer(const Context *context, GLuint renderbuffer)
Shannon Woods53a94a82014-06-24 15:20:36 -0400951{
952 // [OpenGL ES 2.0.24] section 4.4 page 109:
953 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
954 // had been executed with the target RENDERBUFFER and name of zero.
955
956 if (mRenderbuffer.id() == renderbuffer)
957 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400958 setRenderbufferBinding(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400959 }
960
961 // [OpenGL ES 2.0.24] section 4.4 page 111:
962 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
963 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
964 // point to which this image was attached in the currently bound framebuffer.
965
966 Framebuffer *readFramebuffer = mReadFramebuffer;
967 Framebuffer *drawFramebuffer = mDrawFramebuffer;
968
Jamie Madill8693bdb2017-09-02 15:32:14 -0400969 if (readFramebuffer && readFramebuffer->detachRenderbuffer(context, renderbuffer))
Shannon Woods53a94a82014-06-24 15:20:36 -0400970 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400971 mDirtyObjects.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
Shannon Woods53a94a82014-06-24 15:20:36 -0400972 }
973
974 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
975 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400976 if (drawFramebuffer->detachRenderbuffer(context, renderbuffer))
977 {
978 mDirtyObjects.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
979 }
Shannon Woods53a94a82014-06-24 15:20:36 -0400980 }
981
982}
983
984void State::setReadFramebufferBinding(Framebuffer *framebuffer)
985{
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500986 if (mReadFramebuffer == framebuffer)
987 return;
988
Shannon Woods53a94a82014-06-24 15:20:36 -0400989 mReadFramebuffer = framebuffer;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500990 mDirtyBits.set(DIRTY_BIT_READ_FRAMEBUFFER_BINDING);
991
992 if (mReadFramebuffer && mReadFramebuffer->hasAnyDirtyBit())
993 {
994 mDirtyObjects.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
995 }
Shannon Woods53a94a82014-06-24 15:20:36 -0400996}
997
998void State::setDrawFramebufferBinding(Framebuffer *framebuffer)
999{
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001000 if (mDrawFramebuffer == framebuffer)
1001 return;
1002
Shannon Woods53a94a82014-06-24 15:20:36 -04001003 mDrawFramebuffer = framebuffer;
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001004 mDirtyBits.set(DIRTY_BIT_DRAW_FRAMEBUFFER_BINDING);
1005
1006 if (mDrawFramebuffer && mDrawFramebuffer->hasAnyDirtyBit())
1007 {
1008 mDirtyObjects.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
1009 }
Shannon Woods53a94a82014-06-24 15:20:36 -04001010}
1011
1012Framebuffer *State::getTargetFramebuffer(GLenum target) const
1013{
1014 switch (target)
1015 {
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001016 case GL_READ_FRAMEBUFFER_ANGLE:
1017 return mReadFramebuffer;
1018 case GL_DRAW_FRAMEBUFFER_ANGLE:
1019 case GL_FRAMEBUFFER:
1020 return mDrawFramebuffer;
1021 default:
1022 UNREACHABLE();
Yunchao Hef81ce4a2017-04-24 10:49:17 +08001023 return nullptr;
Shannon Woods53a94a82014-06-24 15:20:36 -04001024 }
1025}
1026
Jamie Madill51f40ec2016-06-15 14:06:00 -04001027Framebuffer *State::getReadFramebuffer() const
Shannon Woods53a94a82014-06-24 15:20:36 -04001028{
1029 return mReadFramebuffer;
1030}
1031
Jamie Madill51f40ec2016-06-15 14:06:00 -04001032Framebuffer *State::getDrawFramebuffer() const
Shannon Woods53a94a82014-06-24 15:20:36 -04001033{
1034 return mDrawFramebuffer;
1035}
1036
1037bool State::removeReadFramebufferBinding(GLuint framebuffer)
1038{
Jamie Madill77a72f62015-04-14 11:18:32 -04001039 if (mReadFramebuffer != nullptr &&
1040 mReadFramebuffer->id() == framebuffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001041 {
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001042 setReadFramebufferBinding(nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -04001043 return true;
1044 }
1045
1046 return false;
1047}
1048
1049bool State::removeDrawFramebufferBinding(GLuint framebuffer)
1050{
Jamie Madill77a72f62015-04-14 11:18:32 -04001051 if (mReadFramebuffer != nullptr &&
1052 mDrawFramebuffer->id() == framebuffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001053 {
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001054 setDrawFramebufferBinding(nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -04001055 return true;
1056 }
1057
1058 return false;
1059}
1060
1061void State::setVertexArrayBinding(VertexArray *vertexArray)
1062{
1063 mVertexArray = vertexArray;
Jamie Madill0b9e9032015-08-17 11:51:52 +00001064 mDirtyBits.set(DIRTY_BIT_VERTEX_ARRAY_BINDING);
Jamie Madillc9d442d2016-01-20 11:17:24 -05001065
1066 if (mVertexArray && mVertexArray->hasAnyDirtyBit())
1067 {
1068 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1069 }
Shannon Woods53a94a82014-06-24 15:20:36 -04001070}
1071
1072GLuint State::getVertexArrayId() const
1073{
Yunchao He4f285442017-04-21 12:15:49 +08001074 ASSERT(mVertexArray != nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -04001075 return mVertexArray->id();
1076}
1077
1078VertexArray *State::getVertexArray() const
1079{
Yunchao He4f285442017-04-21 12:15:49 +08001080 ASSERT(mVertexArray != nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -04001081 return mVertexArray;
1082}
1083
1084bool State::removeVertexArrayBinding(GLuint vertexArray)
1085{
1086 if (mVertexArray->id() == vertexArray)
1087 {
Yunchao Hed7297bf2017-04-19 15:27:10 +08001088 mVertexArray = nullptr;
Jamie Madill0b9e9032015-08-17 11:51:52 +00001089 mDirtyBits.set(DIRTY_BIT_VERTEX_ARRAY_BINDING);
Jamie Madillc9d442d2016-01-20 11:17:24 -05001090 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
Shannon Woods53a94a82014-06-24 15:20:36 -04001091 return true;
1092 }
1093
1094 return false;
1095}
1096
Jamie Madill4928b7c2017-06-20 12:57:39 -04001097void State::setElementArrayBuffer(const Context *context, Buffer *buffer)
Shao80957d92017-02-20 21:25:59 +08001098{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001099 getVertexArray()->setElementArrayBuffer(context, buffer);
Shao80957d92017-02-20 21:25:59 +08001100 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1101}
1102
Jamie Madill4928b7c2017-06-20 12:57:39 -04001103void State::bindVertexBuffer(const Context *context,
1104 GLuint bindingIndex,
Shao80957d92017-02-20 21:25:59 +08001105 Buffer *boundBuffer,
1106 GLintptr offset,
1107 GLsizei stride)
1108{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001109 getVertexArray()->bindVertexBuffer(context, bindingIndex, boundBuffer, offset, stride);
Shao80957d92017-02-20 21:25:59 +08001110 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1111}
1112
Shaodde78e82017-05-22 14:13:27 +08001113void State::setVertexAttribBinding(const Context *context, GLuint attribIndex, GLuint bindingIndex)
Shao80957d92017-02-20 21:25:59 +08001114{
Shaodde78e82017-05-22 14:13:27 +08001115 getVertexArray()->setVertexAttribBinding(context, attribIndex, bindingIndex);
Shao80957d92017-02-20 21:25:59 +08001116 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1117}
1118
1119void State::setVertexAttribFormat(GLuint attribIndex,
1120 GLint size,
1121 GLenum type,
1122 bool normalized,
1123 bool pureInteger,
1124 GLuint relativeOffset)
1125{
1126 getVertexArray()->setVertexAttribFormat(attribIndex, size, type, normalized, pureInteger,
1127 relativeOffset);
1128 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1129}
1130
1131void State::setVertexBindingDivisor(GLuint bindingIndex, GLuint divisor)
1132{
1133 getVertexArray()->setVertexBindingDivisor(bindingIndex, divisor);
1134 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1135}
1136
Jamie Madill6c1f6712017-02-14 19:08:04 -05001137void State::setProgram(const Context *context, Program *newProgram)
Shannon Woods53a94a82014-06-24 15:20:36 -04001138{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001139 if (mProgram != newProgram)
Shannon Woods53a94a82014-06-24 15:20:36 -04001140 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001141 if (mProgram)
1142 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001143 mProgram->release(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001144 }
1145
1146 mProgram = newProgram;
1147
1148 if (mProgram)
1149 {
1150 newProgram->addRef();
Jamie Madill81c2e252017-09-09 23:32:46 -04001151 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001152 }
Jamie Madilla779b612017-07-24 11:46:05 -04001153 mDirtyBits.set(DIRTY_BIT_PROGRAM_EXECUTABLE);
1154 mDirtyBits.set(DIRTY_BIT_PROGRAM_BINDING);
Shannon Woods53a94a82014-06-24 15:20:36 -04001155 }
1156}
1157
Geoff Lang7dd2e102014-11-10 15:19:26 -05001158Program *State::getProgram() const
Shannon Woods53a94a82014-06-24 15:20:36 -04001159{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001160 return mProgram;
Shannon Woods53a94a82014-06-24 15:20:36 -04001161}
1162
Jamie Madill4928b7c2017-06-20 12:57:39 -04001163void State::setTransformFeedbackBinding(const Context *context,
1164 TransformFeedback *transformFeedback)
Shannon Woods53a94a82014-06-24 15:20:36 -04001165{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001166 mTransformFeedback.set(context, transformFeedback);
Shannon Woods53a94a82014-06-24 15:20:36 -04001167}
1168
1169TransformFeedback *State::getCurrentTransformFeedback() const
1170{
1171 return mTransformFeedback.get();
1172}
1173
Gregoire Payen de La Garanderie52742022015-02-04 14:55:39 +00001174bool State::isTransformFeedbackActiveUnpaused() const
1175{
Jamie Madill81c2e252017-09-09 23:32:46 -04001176 TransformFeedback *curTransformFeedback = getCurrentTransformFeedback();
Geoff Langbb0a0bb2015-03-27 12:16:57 -04001177 return curTransformFeedback && curTransformFeedback->isActive() && !curTransformFeedback->isPaused();
Gregoire Payen de La Garanderie52742022015-02-04 14:55:39 +00001178}
1179
Jamie Madill4928b7c2017-06-20 12:57:39 -04001180bool State::removeTransformFeedbackBinding(const Context *context, GLuint transformFeedback)
Shannon Woods53a94a82014-06-24 15:20:36 -04001181{
1182 if (mTransformFeedback.id() == transformFeedback)
1183 {
Jamie Madill4928b7c2017-06-20 12:57:39 -04001184 mTransformFeedback.set(context, nullptr);
Corentin Walleza2257da2016-04-19 16:43:12 -04001185 return true;
Shannon Woods53a94a82014-06-24 15:20:36 -04001186 }
Corentin Walleza2257da2016-04-19 16:43:12 -04001187
1188 return false;
Shannon Woods53a94a82014-06-24 15:20:36 -04001189}
1190
Yunchao Hea336b902017-08-02 16:05:21 +08001191void State::setProgramPipelineBinding(const Context *context, ProgramPipeline *pipeline)
1192{
1193 mProgramPipeline.set(context, pipeline);
1194}
1195
1196void State::detachProgramPipeline(const Context *context, GLuint pipeline)
1197{
1198 mProgramPipeline.set(context, nullptr);
1199}
1200
Olli Etuahobbf1c102016-06-28 13:31:33 +03001201bool State::isQueryActive(const GLenum type) const
Shannon Woods53a94a82014-06-24 15:20:36 -04001202{
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001203 for (auto &iter : mActiveQueries)
Shannon Woods53a94a82014-06-24 15:20:36 -04001204 {
Olli Etuahobbf1c102016-06-28 13:31:33 +03001205 const Query *query = iter.second.get();
1206 if (query != nullptr && ActiveQueryType(query->getType()) == ActiveQueryType(type))
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001207 {
1208 return true;
1209 }
1210 }
1211
1212 return false;
1213}
1214
1215bool State::isQueryActive(Query *query) const
1216{
1217 for (auto &iter : mActiveQueries)
1218 {
1219 if (iter.second.get() == query)
Shannon Woods53a94a82014-06-24 15:20:36 -04001220 {
1221 return true;
1222 }
1223 }
1224
1225 return false;
1226}
1227
Jamie Madill4928b7c2017-06-20 12:57:39 -04001228void State::setActiveQuery(const Context *context, GLenum target, Query *query)
Shannon Woods53a94a82014-06-24 15:20:36 -04001229{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001230 mActiveQueries[target].set(context, query);
Shannon Woods53a94a82014-06-24 15:20:36 -04001231}
1232
1233GLuint State::getActiveQueryId(GLenum target) const
1234{
1235 const Query *query = getActiveQuery(target);
1236 return (query ? query->id() : 0u);
1237}
1238
1239Query *State::getActiveQuery(GLenum target) const
1240{
Jamie Madill5864ac22015-01-12 14:43:07 -05001241 const auto it = mActiveQueries.find(target);
Shannon Woods53a94a82014-06-24 15:20:36 -04001242
Jamie Madill5864ac22015-01-12 14:43:07 -05001243 // All query types should already exist in the activeQueries map
1244 ASSERT(it != mActiveQueries.end());
1245
1246 return it->second.get();
Shannon Woods53a94a82014-06-24 15:20:36 -04001247}
1248
Jamie Madill4928b7c2017-06-20 12:57:39 -04001249void State::setArrayBufferBinding(const Context *context, Buffer *buffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001250{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001251 mArrayBuffer.set(context, buffer);
Shannon Woods53a94a82014-06-24 15:20:36 -04001252}
1253
1254GLuint State::getArrayBufferId() const
1255{
1256 return mArrayBuffer.id();
1257}
1258
Jamie Madill4928b7c2017-06-20 12:57:39 -04001259void State::setDrawIndirectBufferBinding(const Context *context, Buffer *buffer)
Jiajia Qin9d7d0b12016-11-29 16:30:31 +08001260{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001261 mDrawIndirectBuffer.set(context, buffer);
Jiajia Qin9d7d0b12016-11-29 16:30:31 +08001262 mDirtyBits.set(DIRTY_BIT_DRAW_INDIRECT_BUFFER_BINDING);
1263}
1264
Jamie Madill4928b7c2017-06-20 12:57:39 -04001265void State::setGenericUniformBufferBinding(const Context *context, Buffer *buffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001266{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001267 mGenericUniformBuffer.set(context, buffer);
Shannon Woods53a94a82014-06-24 15:20:36 -04001268}
1269
Jamie Madill4928b7c2017-06-20 12:57:39 -04001270void State::setIndexedUniformBufferBinding(const Context *context,
1271 GLuint index,
1272 Buffer *buffer,
1273 GLintptr offset,
1274 GLsizeiptr size)
Shannon Woods53a94a82014-06-24 15:20:36 -04001275{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001276 mUniformBuffers[index].set(context, buffer, offset, size);
Shannon Woods53a94a82014-06-24 15:20:36 -04001277}
1278
Geoff Lang5d124a62015-09-15 13:03:27 -04001279const OffsetBindingPointer<Buffer> &State::getIndexedUniformBuffer(size_t index) const
Shannon Woods53a94a82014-06-24 15:20:36 -04001280{
Shannon Woodsf3acaf92014-09-23 18:07:11 -04001281 ASSERT(static_cast<size_t>(index) < mUniformBuffers.size());
Geoff Lang5d124a62015-09-15 13:03:27 -04001282 return mUniformBuffers[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001283}
1284
Jamie Madill4928b7c2017-06-20 12:57:39 -04001285void State::setGenericAtomicCounterBufferBinding(const Context *context, Buffer *buffer)
Jiajia Qin6eafb042016-12-27 17:04:07 +08001286{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001287 mGenericAtomicCounterBuffer.set(context, buffer);
Jiajia Qin6eafb042016-12-27 17:04:07 +08001288}
1289
Jamie Madill4928b7c2017-06-20 12:57:39 -04001290void State::setIndexedAtomicCounterBufferBinding(const Context *context,
1291 GLuint index,
Jiajia Qin6eafb042016-12-27 17:04:07 +08001292 Buffer *buffer,
1293 GLintptr offset,
1294 GLsizeiptr size)
1295{
1296 ASSERT(static_cast<size_t>(index) < mAtomicCounterBuffers.size());
Jamie Madill4928b7c2017-06-20 12:57:39 -04001297 mAtomicCounterBuffers[index].set(context, buffer, offset, size);
Jiajia Qin6eafb042016-12-27 17:04:07 +08001298}
1299
1300const OffsetBindingPointer<Buffer> &State::getIndexedAtomicCounterBuffer(size_t index) const
1301{
1302 ASSERT(static_cast<size_t>(index) < mAtomicCounterBuffers.size());
1303 return mAtomicCounterBuffers[index];
1304}
1305
Jamie Madill4928b7c2017-06-20 12:57:39 -04001306void State::setGenericShaderStorageBufferBinding(const Context *context, Buffer *buffer)
Jiajia Qinf546e7d2017-03-27 14:12:59 +08001307{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001308 mGenericShaderStorageBuffer.set(context, buffer);
Jiajia Qinf546e7d2017-03-27 14:12:59 +08001309}
1310
Jamie Madill4928b7c2017-06-20 12:57:39 -04001311void State::setIndexedShaderStorageBufferBinding(const Context *context,
1312 GLuint index,
Jiajia Qinf546e7d2017-03-27 14:12:59 +08001313 Buffer *buffer,
1314 GLintptr offset,
1315 GLsizeiptr size)
1316{
1317 ASSERT(static_cast<size_t>(index) < mShaderStorageBuffers.size());
Jamie Madill4928b7c2017-06-20 12:57:39 -04001318 mShaderStorageBuffers[index].set(context, buffer, offset, size);
Jiajia Qinf546e7d2017-03-27 14:12:59 +08001319}
1320
1321const OffsetBindingPointer<Buffer> &State::getIndexedShaderStorageBuffer(size_t index) const
1322{
1323 ASSERT(static_cast<size_t>(index) < mShaderStorageBuffers.size());
1324 return mShaderStorageBuffers[index];
1325}
1326
Jamie Madill4928b7c2017-06-20 12:57:39 -04001327void State::setCopyReadBufferBinding(const Context *context, Buffer *buffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001328{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001329 mCopyReadBuffer.set(context, buffer);
Shannon Woods53a94a82014-06-24 15:20:36 -04001330}
1331
Jamie Madill4928b7c2017-06-20 12:57:39 -04001332void State::setCopyWriteBufferBinding(const Context *context, Buffer *buffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001333{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001334 mCopyWriteBuffer.set(context, buffer);
Shannon Woods53a94a82014-06-24 15:20:36 -04001335}
1336
Jamie Madill4928b7c2017-06-20 12:57:39 -04001337void State::setPixelPackBufferBinding(const Context *context, Buffer *buffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001338{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001339 mPack.pixelBuffer.set(context, buffer);
Corentin Wallezbbd663a2016-04-20 17:49:17 -04001340 mDirtyBits.set(DIRTY_BIT_PACK_BUFFER_BINDING);
Shannon Woods53a94a82014-06-24 15:20:36 -04001341}
1342
Jamie Madill4928b7c2017-06-20 12:57:39 -04001343void State::setPixelUnpackBufferBinding(const Context *context, Buffer *buffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001344{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001345 mUnpack.pixelBuffer.set(context, buffer);
Corentin Wallezbbd663a2016-04-20 17:49:17 -04001346 mDirtyBits.set(DIRTY_BIT_UNPACK_BUFFER_BINDING);
Shannon Woods53a94a82014-06-24 15:20:36 -04001347}
1348
1349Buffer *State::getTargetBuffer(GLenum target) const
1350{
1351 switch (target)
1352 {
1353 case GL_ARRAY_BUFFER: return mArrayBuffer.get();
1354 case GL_COPY_READ_BUFFER: return mCopyReadBuffer.get();
1355 case GL_COPY_WRITE_BUFFER: return mCopyWriteBuffer.get();
Jamie Madill8e344942015-07-09 14:22:07 -04001356 case GL_ELEMENT_ARRAY_BUFFER: return getVertexArray()->getElementArrayBuffer().get();
Shannon Woods53a94a82014-06-24 15:20:36 -04001357 case GL_PIXEL_PACK_BUFFER: return mPack.pixelBuffer.get();
1358 case GL_PIXEL_UNPACK_BUFFER: return mUnpack.pixelBuffer.get();
Geoff Lang045536b2015-03-27 15:17:18 -04001359 case GL_TRANSFORM_FEEDBACK_BUFFER: return mTransformFeedback->getGenericBuffer().get();
Shannon Woods53a94a82014-06-24 15:20:36 -04001360 case GL_UNIFORM_BUFFER: return mGenericUniformBuffer.get();
Geoff Langb5e997f2016-12-06 10:55:34 -05001361 case GL_ATOMIC_COUNTER_BUFFER:
Jiajia Qin6eafb042016-12-27 17:04:07 +08001362 return mGenericAtomicCounterBuffer.get();
Geoff Langb5e997f2016-12-06 10:55:34 -05001363 case GL_SHADER_STORAGE_BUFFER:
Jiajia Qinf546e7d2017-03-27 14:12:59 +08001364 return mGenericShaderStorageBuffer.get();
Geoff Langb5e997f2016-12-06 10:55:34 -05001365 case GL_DRAW_INDIRECT_BUFFER:
Jiajia Qin9d7d0b12016-11-29 16:30:31 +08001366 return mDrawIndirectBuffer.get();
Yunchao Hef81ce4a2017-04-24 10:49:17 +08001367 default:
1368 UNREACHABLE();
1369 return nullptr;
Shannon Woods53a94a82014-06-24 15:20:36 -04001370 }
1371}
1372
Jamie Madill4928b7c2017-06-20 12:57:39 -04001373void State::detachBuffer(const Context *context, GLuint bufferName)
Yuly Novikov5807a532015-12-03 13:01:22 -05001374{
Jiajia Qinf546e7d2017-03-27 14:12:59 +08001375 BindingPointer<Buffer> *buffers[] = {
1376 &mArrayBuffer, &mGenericAtomicCounterBuffer, &mCopyReadBuffer,
1377 &mCopyWriteBuffer, &mDrawIndirectBuffer, &mPack.pixelBuffer,
1378 &mUnpack.pixelBuffer, &mGenericUniformBuffer, &mGenericShaderStorageBuffer};
Yuly Novikov5807a532015-12-03 13:01:22 -05001379 for (auto buffer : buffers)
1380 {
1381 if (buffer->id() == bufferName)
1382 {
Jamie Madill4928b7c2017-06-20 12:57:39 -04001383 buffer->set(context, nullptr);
Yuly Novikov5807a532015-12-03 13:01:22 -05001384 }
1385 }
1386
1387 TransformFeedback *curTransformFeedback = getCurrentTransformFeedback();
1388 if (curTransformFeedback)
1389 {
Jamie Madill4928b7c2017-06-20 12:57:39 -04001390 curTransformFeedback->detachBuffer(context, bufferName);
Yuly Novikov5807a532015-12-03 13:01:22 -05001391 }
1392
Jamie Madill4928b7c2017-06-20 12:57:39 -04001393 getVertexArray()->detachBuffer(context, bufferName);
Yuly Novikov5807a532015-12-03 13:01:22 -05001394}
1395
Shannon Woods53a94a82014-06-24 15:20:36 -04001396void State::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
1397{
1398 getVertexArray()->enableAttribute(attribNum, enabled);
Jamie Madillc9d442d2016-01-20 11:17:24 -05001399 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
Shannon Woods53a94a82014-06-24 15:20:36 -04001400}
1401
1402void State::setVertexAttribf(GLuint index, const GLfloat values[4])
1403{
Shannon Woods23e05002014-09-22 19:07:27 -04001404 ASSERT(static_cast<size_t>(index) < mVertexAttribCurrentValues.size());
Shannon Woods53a94a82014-06-24 15:20:36 -04001405 mVertexAttribCurrentValues[index].setFloatValues(values);
Jamie Madill1e0bc3a2015-08-11 08:12:21 -04001406 mDirtyBits.set(DIRTY_BIT_CURRENT_VALUE_0 + index);
Shannon Woods53a94a82014-06-24 15:20:36 -04001407}
1408
1409void State::setVertexAttribu(GLuint index, const GLuint values[4])
1410{
Shannon Woods23e05002014-09-22 19:07:27 -04001411 ASSERT(static_cast<size_t>(index) < mVertexAttribCurrentValues.size());
Shannon Woods53a94a82014-06-24 15:20:36 -04001412 mVertexAttribCurrentValues[index].setUnsignedIntValues(values);
Jamie Madill1e0bc3a2015-08-11 08:12:21 -04001413 mDirtyBits.set(DIRTY_BIT_CURRENT_VALUE_0 + index);
Shannon Woods53a94a82014-06-24 15:20:36 -04001414}
1415
1416void State::setVertexAttribi(GLuint index, const GLint values[4])
1417{
Shannon Woods23e05002014-09-22 19:07:27 -04001418 ASSERT(static_cast<size_t>(index) < mVertexAttribCurrentValues.size());
Shannon Woods53a94a82014-06-24 15:20:36 -04001419 mVertexAttribCurrentValues[index].setIntValues(values);
Jamie Madill1e0bc3a2015-08-11 08:12:21 -04001420 mDirtyBits.set(DIRTY_BIT_CURRENT_VALUE_0 + index);
Shannon Woods53a94a82014-06-24 15:20:36 -04001421}
1422
Shaodde78e82017-05-22 14:13:27 +08001423void State::setVertexAttribPointer(const Context *context,
1424 unsigned int attribNum,
1425 Buffer *boundBuffer,
1426 GLint size,
1427 GLenum type,
1428 bool normalized,
1429 bool pureInteger,
1430 GLsizei stride,
1431 const void *pointer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001432{
Shaodde78e82017-05-22 14:13:27 +08001433 getVertexArray()->setVertexAttribPointer(context, attribNum, boundBuffer, size, type,
1434 normalized, pureInteger, stride, pointer);
Jamie Madillc9d442d2016-01-20 11:17:24 -05001435 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
Jamie Madill0b9e9032015-08-17 11:51:52 +00001436}
1437
Shaodde78e82017-05-22 14:13:27 +08001438void State::setVertexAttribDivisor(const Context *context, GLuint index, GLuint divisor)
Jamie Madill0b9e9032015-08-17 11:51:52 +00001439{
Shaodde78e82017-05-22 14:13:27 +08001440 getVertexArray()->setVertexAttribDivisor(context, index, divisor);
Jamie Madillc9d442d2016-01-20 11:17:24 -05001441 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
Shannon Woods53a94a82014-06-24 15:20:36 -04001442}
1443
Jamie Madill6de51852017-04-12 09:53:01 -04001444const VertexAttribCurrentValueData &State::getVertexAttribCurrentValue(size_t attribNum) const
Shannon Woods53a94a82014-06-24 15:20:36 -04001445{
Jamie Madill6de51852017-04-12 09:53:01 -04001446 ASSERT(attribNum < mVertexAttribCurrentValues.size());
Shannon Woods53a94a82014-06-24 15:20:36 -04001447 return mVertexAttribCurrentValues[attribNum];
1448}
1449
Shannon Woods53a94a82014-06-24 15:20:36 -04001450const void *State::getVertexAttribPointer(unsigned int attribNum) const
1451{
1452 return getVertexArray()->getVertexAttribute(attribNum).pointer;
1453}
1454
1455void State::setPackAlignment(GLint alignment)
1456{
1457 mPack.alignment = alignment;
Jamie Madill1b94d432015-08-07 13:23:23 -04001458 mDirtyBits.set(DIRTY_BIT_PACK_ALIGNMENT);
Shannon Woods53a94a82014-06-24 15:20:36 -04001459}
1460
1461GLint State::getPackAlignment() const
1462{
1463 return mPack.alignment;
1464}
1465
1466void State::setPackReverseRowOrder(bool reverseRowOrder)
1467{
1468 mPack.reverseRowOrder = reverseRowOrder;
Jamie Madill1b94d432015-08-07 13:23:23 -04001469 mDirtyBits.set(DIRTY_BIT_PACK_REVERSE_ROW_ORDER);
Shannon Woods53a94a82014-06-24 15:20:36 -04001470}
1471
1472bool State::getPackReverseRowOrder() const
1473{
1474 return mPack.reverseRowOrder;
1475}
1476
Minmin Gongadff67b2015-10-14 10:34:45 -04001477void State::setPackRowLength(GLint rowLength)
1478{
1479 mPack.rowLength = rowLength;
1480 mDirtyBits.set(DIRTY_BIT_PACK_ROW_LENGTH);
1481}
1482
1483GLint State::getPackRowLength() const
1484{
1485 return mPack.rowLength;
1486}
1487
1488void State::setPackSkipRows(GLint skipRows)
1489{
1490 mPack.skipRows = skipRows;
1491 mDirtyBits.set(DIRTY_BIT_PACK_SKIP_ROWS);
1492}
1493
1494GLint State::getPackSkipRows() const
1495{
1496 return mPack.skipRows;
1497}
1498
1499void State::setPackSkipPixels(GLint skipPixels)
1500{
1501 mPack.skipPixels = skipPixels;
1502 mDirtyBits.set(DIRTY_BIT_PACK_SKIP_PIXELS);
1503}
1504
1505GLint State::getPackSkipPixels() const
1506{
1507 return mPack.skipPixels;
1508}
1509
Shannon Woods53a94a82014-06-24 15:20:36 -04001510const PixelPackState &State::getPackState() const
1511{
1512 return mPack;
1513}
1514
Jamie Madill87de3622015-03-16 10:41:44 -04001515PixelPackState &State::getPackState()
1516{
1517 return mPack;
1518}
1519
Shannon Woods53a94a82014-06-24 15:20:36 -04001520void State::setUnpackAlignment(GLint alignment)
1521{
1522 mUnpack.alignment = alignment;
Jamie Madill1b94d432015-08-07 13:23:23 -04001523 mDirtyBits.set(DIRTY_BIT_UNPACK_ALIGNMENT);
Shannon Woods53a94a82014-06-24 15:20:36 -04001524}
1525
1526GLint State::getUnpackAlignment() const
1527{
1528 return mUnpack.alignment;
1529}
1530
Minmin Gongb8aee3b2015-01-27 14:42:36 -08001531void State::setUnpackRowLength(GLint rowLength)
1532{
1533 mUnpack.rowLength = rowLength;
Jamie Madill1b94d432015-08-07 13:23:23 -04001534 mDirtyBits.set(DIRTY_BIT_UNPACK_ROW_LENGTH);
Minmin Gongb8aee3b2015-01-27 14:42:36 -08001535}
1536
1537GLint State::getUnpackRowLength() const
1538{
1539 return mUnpack.rowLength;
1540}
1541
Minmin Gongadff67b2015-10-14 10:34:45 -04001542void State::setUnpackImageHeight(GLint imageHeight)
1543{
1544 mUnpack.imageHeight = imageHeight;
1545 mDirtyBits.set(DIRTY_BIT_UNPACK_IMAGE_HEIGHT);
1546}
1547
1548GLint State::getUnpackImageHeight() const
1549{
1550 return mUnpack.imageHeight;
1551}
1552
1553void State::setUnpackSkipImages(GLint skipImages)
1554{
1555 mUnpack.skipImages = skipImages;
1556 mDirtyBits.set(DIRTY_BIT_UNPACK_SKIP_IMAGES);
1557}
1558
1559GLint State::getUnpackSkipImages() const
1560{
1561 return mUnpack.skipImages;
1562}
1563
1564void State::setUnpackSkipRows(GLint skipRows)
1565{
1566 mUnpack.skipRows = skipRows;
1567 mDirtyBits.set(DIRTY_BIT_UNPACK_SKIP_ROWS);
1568}
1569
1570GLint State::getUnpackSkipRows() const
1571{
1572 return mUnpack.skipRows;
1573}
1574
1575void State::setUnpackSkipPixels(GLint skipPixels)
1576{
1577 mUnpack.skipPixels = skipPixels;
1578 mDirtyBits.set(DIRTY_BIT_UNPACK_SKIP_PIXELS);
1579}
1580
1581GLint State::getUnpackSkipPixels() const
1582{
1583 return mUnpack.skipPixels;
1584}
1585
Shannon Woods53a94a82014-06-24 15:20:36 -04001586const PixelUnpackState &State::getUnpackState() const
1587{
1588 return mUnpack;
1589}
1590
Jamie Madill67102f02015-03-16 10:41:42 -04001591PixelUnpackState &State::getUnpackState()
1592{
1593 return mUnpack;
1594}
1595
Geoff Lang70d0f492015-12-10 17:45:46 -05001596const Debug &State::getDebug() const
1597{
1598 return mDebug;
1599}
1600
1601Debug &State::getDebug()
1602{
1603 return mDebug;
1604}
1605
Sami Väisänena797e062016-05-12 15:23:40 +03001606void State::setCoverageModulation(GLenum components)
1607{
1608 mCoverageModulation = components;
1609 mDirtyBits.set(DIRTY_BIT_COVERAGE_MODULATION);
1610}
1611
1612GLenum State::getCoverageModulation() const
1613{
1614 return mCoverageModulation;
1615}
1616
Sami Väisänene45e53b2016-05-25 10:36:04 +03001617void State::loadPathRenderingMatrix(GLenum matrixMode, const GLfloat *matrix)
1618{
1619 if (matrixMode == GL_PATH_MODELVIEW_CHROMIUM)
1620 {
1621 memcpy(mPathMatrixMV, matrix, 16 * sizeof(GLfloat));
1622 mDirtyBits.set(DIRTY_BIT_PATH_RENDERING_MATRIX_MV);
1623 }
1624 else if (matrixMode == GL_PATH_PROJECTION_CHROMIUM)
1625 {
1626 memcpy(mPathMatrixProj, matrix, 16 * sizeof(GLfloat));
1627 mDirtyBits.set(DIRTY_BIT_PATH_RENDERING_MATRIX_PROJ);
1628 }
1629 else
1630 {
1631 UNREACHABLE();
1632 }
1633}
1634
1635const GLfloat *State::getPathRenderingMatrix(GLenum which) const
1636{
1637 if (which == GL_PATH_MODELVIEW_MATRIX_CHROMIUM)
1638 {
1639 return mPathMatrixMV;
1640 }
1641 else if (which == GL_PATH_PROJECTION_MATRIX_CHROMIUM)
1642 {
1643 return mPathMatrixProj;
1644 }
1645
1646 UNREACHABLE();
1647 return nullptr;
1648}
1649
1650void State::setPathStencilFunc(GLenum func, GLint ref, GLuint mask)
1651{
1652 mPathStencilFunc = func;
1653 mPathStencilRef = ref;
1654 mPathStencilMask = mask;
1655 mDirtyBits.set(DIRTY_BIT_PATH_RENDERING_STENCIL_STATE);
1656}
1657
1658GLenum State::getPathStencilFunc() const
1659{
1660 return mPathStencilFunc;
1661}
1662
1663GLint State::getPathStencilRef() const
1664{
1665 return mPathStencilRef;
1666}
1667
1668GLuint State::getPathStencilMask() const
1669{
1670 return mPathStencilMask;
1671}
1672
Geoff Lang1d2c41d2016-10-19 16:14:46 -07001673void State::setFramebufferSRGB(bool sRGB)
1674{
1675 mFramebufferSRGB = sRGB;
1676 mDirtyBits.set(DIRTY_BIT_FRAMEBUFFER_SRGB);
1677}
1678
1679bool State::getFramebufferSRGB() const
1680{
1681 return mFramebufferSRGB;
1682}
1683
Shannon Woods53a94a82014-06-24 15:20:36 -04001684void State::getBooleanv(GLenum pname, GLboolean *params)
1685{
1686 switch (pname)
1687 {
1688 case GL_SAMPLE_COVERAGE_INVERT: *params = mSampleCoverageInvert; break;
1689 case GL_DEPTH_WRITEMASK: *params = mDepthStencil.depthMask; break;
1690 case GL_COLOR_WRITEMASK:
1691 params[0] = mBlend.colorMaskRed;
1692 params[1] = mBlend.colorMaskGreen;
1693 params[2] = mBlend.colorMaskBlue;
1694 params[3] = mBlend.colorMaskAlpha;
1695 break;
1696 case GL_CULL_FACE: *params = mRasterizer.cullFace; break;
1697 case GL_POLYGON_OFFSET_FILL: *params = mRasterizer.polygonOffsetFill; break;
1698 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mBlend.sampleAlphaToCoverage; break;
1699 case GL_SAMPLE_COVERAGE: *params = mSampleCoverage; break;
Jiawei Shaodb342272017-09-27 10:21:45 +08001700 case GL_SAMPLE_MASK:
1701 *params = mSampleMask;
1702 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001703 case GL_SCISSOR_TEST: *params = mScissorTest; break;
1704 case GL_STENCIL_TEST: *params = mDepthStencil.stencilTest; break;
1705 case GL_DEPTH_TEST: *params = mDepthStencil.depthTest; break;
1706 case GL_BLEND: *params = mBlend.blend; break;
1707 case GL_DITHER: *params = mBlend.dither; break;
Geoff Langbb0a0bb2015-03-27 12:16:57 -04001708 case GL_TRANSFORM_FEEDBACK_ACTIVE: *params = getCurrentTransformFeedback()->isActive() ? GL_TRUE : GL_FALSE; break;
1709 case GL_TRANSFORM_FEEDBACK_PAUSED: *params = getCurrentTransformFeedback()->isPaused() ? GL_TRUE : GL_FALSE; break;
Jamie Madille2cd53d2015-10-27 11:15:46 -04001710 case GL_PRIMITIVE_RESTART_FIXED_INDEX:
1711 *params = mPrimitiveRestart;
1712 break;
Geoff Langab831f02015-12-01 09:39:10 -05001713 case GL_RASTERIZER_DISCARD:
1714 *params = isRasterizerDiscardEnabled() ? GL_TRUE : GL_FALSE;
1715 break;
Geoff Lang70d0f492015-12-10 17:45:46 -05001716 case GL_DEBUG_OUTPUT_SYNCHRONOUS:
1717 *params = mDebug.isOutputSynchronous() ? GL_TRUE : GL_FALSE;
1718 break;
1719 case GL_DEBUG_OUTPUT:
1720 *params = mDebug.isOutputEnabled() ? GL_TRUE : GL_FALSE;
1721 break;
Sami Väisänen74c23472016-05-09 17:30:30 +03001722 case GL_MULTISAMPLE_EXT:
1723 *params = mMultiSampling;
1724 break;
1725 case GL_SAMPLE_ALPHA_TO_ONE_EXT:
1726 *params = mSampleAlphaToOne;
1727 break;
Geoff Langf41a7152016-09-19 15:11:17 -04001728 case GL_BIND_GENERATES_RESOURCE_CHROMIUM:
1729 *params = isBindGeneratesResourceEnabled() ? GL_TRUE : GL_FALSE;
1730 break;
Geoff Langfeb8c682017-02-13 16:07:35 -05001731 case GL_CLIENT_ARRAYS_ANGLE:
1732 *params = areClientArraysEnabled() ? GL_TRUE : GL_FALSE;
1733 break;
Geoff Lang1d2c41d2016-10-19 16:14:46 -07001734 case GL_FRAMEBUFFER_SRGB_EXT:
1735 *params = getFramebufferSRGB() ? GL_TRUE : GL_FALSE;
1736 break;
Jamie Madille08a1d32017-03-07 17:24:06 -05001737 case GL_CONTEXT_ROBUST_RESOURCE_INITIALIZATION_ANGLE:
1738 *params = mRobustResourceInit ? GL_TRUE : GL_FALSE;
1739 break;
Jamie Madillc43be722017-07-13 16:22:14 -04001740 case GL_PROGRAM_CACHE_ENABLED_ANGLE:
1741 *params = mProgramBinaryCacheEnabled ? GL_TRUE : GL_FALSE;
1742 break;
1743
Shannon Woods53a94a82014-06-24 15:20:36 -04001744 default:
1745 UNREACHABLE();
1746 break;
1747 }
1748}
1749
1750void State::getFloatv(GLenum pname, GLfloat *params)
1751{
1752 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1753 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1754 // GetIntegerv as its native query function. As it would require conversion in any
1755 // case, this should make no difference to the calling application.
1756 switch (pname)
1757 {
1758 case GL_LINE_WIDTH: *params = mLineWidth; break;
1759 case GL_SAMPLE_COVERAGE_VALUE: *params = mSampleCoverageValue; break;
1760 case GL_DEPTH_CLEAR_VALUE: *params = mDepthClearValue; break;
1761 case GL_POLYGON_OFFSET_FACTOR: *params = mRasterizer.polygonOffsetFactor; break;
1762 case GL_POLYGON_OFFSET_UNITS: *params = mRasterizer.polygonOffsetUnits; break;
1763 case GL_DEPTH_RANGE:
1764 params[0] = mNearZ;
1765 params[1] = mFarZ;
1766 break;
1767 case GL_COLOR_CLEAR_VALUE:
1768 params[0] = mColorClearValue.red;
1769 params[1] = mColorClearValue.green;
1770 params[2] = mColorClearValue.blue;
1771 params[3] = mColorClearValue.alpha;
1772 break;
1773 case GL_BLEND_COLOR:
1774 params[0] = mBlendColor.red;
1775 params[1] = mBlendColor.green;
1776 params[2] = mBlendColor.blue;
1777 params[3] = mBlendColor.alpha;
1778 break;
Sami Väisänen74c23472016-05-09 17:30:30 +03001779 case GL_MULTISAMPLE_EXT:
1780 *params = static_cast<GLfloat>(mMultiSampling);
1781 break;
1782 case GL_SAMPLE_ALPHA_TO_ONE_EXT:
1783 *params = static_cast<GLfloat>(mSampleAlphaToOne);
Sami Väisänena797e062016-05-12 15:23:40 +03001784 case GL_COVERAGE_MODULATION_CHROMIUM:
Jamie Madille2e406c2016-06-02 13:04:10 -04001785 params[0] = static_cast<GLfloat>(mCoverageModulation);
1786 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001787 default:
1788 UNREACHABLE();
1789 break;
1790 }
1791}
1792
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001793void State::getIntegerv(const Context *context, GLenum pname, GLint *params)
Shannon Woods53a94a82014-06-24 15:20:36 -04001794{
1795 if (pname >= GL_DRAW_BUFFER0_EXT && pname <= GL_DRAW_BUFFER15_EXT)
1796 {
1797 unsigned int colorAttachment = (pname - GL_DRAW_BUFFER0_EXT);
Shannon Woods2df6a602014-09-26 16:12:07 -04001798 ASSERT(colorAttachment < mMaxDrawBuffers);
Shannon Woods53a94a82014-06-24 15:20:36 -04001799 Framebuffer *framebuffer = mDrawFramebuffer;
1800 *params = framebuffer->getDrawBufferState(colorAttachment);
1801 return;
1802 }
1803
1804 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1805 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1806 // GetIntegerv as its native query function. As it would require conversion in any
1807 // case, this should make no difference to the calling application. You may find it in
1808 // State::getFloatv.
1809 switch (pname)
1810 {
1811 case GL_ARRAY_BUFFER_BINDING: *params = mArrayBuffer.id(); break;
Jiajia Qin9d7d0b12016-11-29 16:30:31 +08001812 case GL_DRAW_INDIRECT_BUFFER_BINDING:
1813 *params = mDrawIndirectBuffer.id();
1814 break;
Jamie Madill8e344942015-07-09 14:22:07 -04001815 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = getVertexArray()->getElementArrayBuffer().id(); break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001816 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1817 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mDrawFramebuffer->id(); break;
1818 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mReadFramebuffer->id(); break;
1819 case GL_RENDERBUFFER_BINDING: *params = mRenderbuffer.id(); break;
1820 case GL_VERTEX_ARRAY_BINDING: *params = mVertexArray->id(); break;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001821 case GL_CURRENT_PROGRAM: *params = mProgram ? mProgram->id() : 0; break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001822 case GL_PACK_ALIGNMENT: *params = mPack.alignment; break;
1823 case GL_PACK_REVERSE_ROW_ORDER_ANGLE: *params = mPack.reverseRowOrder; break;
Minmin Gongadff67b2015-10-14 10:34:45 -04001824 case GL_PACK_ROW_LENGTH:
1825 *params = mPack.rowLength;
1826 break;
1827 case GL_PACK_SKIP_ROWS:
1828 *params = mPack.skipRows;
1829 break;
1830 case GL_PACK_SKIP_PIXELS:
1831 *params = mPack.skipPixels;
1832 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001833 case GL_UNPACK_ALIGNMENT: *params = mUnpack.alignment; break;
Minmin Gongb8aee3b2015-01-27 14:42:36 -08001834 case GL_UNPACK_ROW_LENGTH: *params = mUnpack.rowLength; break;
Minmin Gongadff67b2015-10-14 10:34:45 -04001835 case GL_UNPACK_IMAGE_HEIGHT:
1836 *params = mUnpack.imageHeight;
1837 break;
1838 case GL_UNPACK_SKIP_IMAGES:
1839 *params = mUnpack.skipImages;
1840 break;
1841 case GL_UNPACK_SKIP_ROWS:
1842 *params = mUnpack.skipRows;
1843 break;
1844 case GL_UNPACK_SKIP_PIXELS:
1845 *params = mUnpack.skipPixels;
1846 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001847 case GL_GENERATE_MIPMAP_HINT: *params = mGenerateMipmapHint; break;
1848 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mFragmentShaderDerivativeHint; break;
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001849 case GL_ACTIVE_TEXTURE:
1850 *params = (static_cast<GLint>(mActiveSampler) + GL_TEXTURE0);
1851 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001852 case GL_STENCIL_FUNC: *params = mDepthStencil.stencilFunc; break;
1853 case GL_STENCIL_REF: *params = mStencilRef; break;
1854 case GL_STENCIL_VALUE_MASK: *params = clampToInt(mDepthStencil.stencilMask); break;
1855 case GL_STENCIL_BACK_FUNC: *params = mDepthStencil.stencilBackFunc; break;
1856 case GL_STENCIL_BACK_REF: *params = mStencilBackRef; break;
1857 case GL_STENCIL_BACK_VALUE_MASK: *params = clampToInt(mDepthStencil.stencilBackMask); break;
1858 case GL_STENCIL_FAIL: *params = mDepthStencil.stencilFail; break;
1859 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mDepthStencil.stencilPassDepthFail; break;
1860 case GL_STENCIL_PASS_DEPTH_PASS: *params = mDepthStencil.stencilPassDepthPass; break;
1861 case GL_STENCIL_BACK_FAIL: *params = mDepthStencil.stencilBackFail; break;
1862 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mDepthStencil.stencilBackPassDepthFail; break;
1863 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mDepthStencil.stencilBackPassDepthPass; break;
1864 case GL_DEPTH_FUNC: *params = mDepthStencil.depthFunc; break;
1865 case GL_BLEND_SRC_RGB: *params = mBlend.sourceBlendRGB; break;
1866 case GL_BLEND_SRC_ALPHA: *params = mBlend.sourceBlendAlpha; break;
1867 case GL_BLEND_DST_RGB: *params = mBlend.destBlendRGB; break;
1868 case GL_BLEND_DST_ALPHA: *params = mBlend.destBlendAlpha; break;
1869 case GL_BLEND_EQUATION_RGB: *params = mBlend.blendEquationRGB; break;
1870 case GL_BLEND_EQUATION_ALPHA: *params = mBlend.blendEquationAlpha; break;
1871 case GL_STENCIL_WRITEMASK: *params = clampToInt(mDepthStencil.stencilWritemask); break;
1872 case GL_STENCIL_BACK_WRITEMASK: *params = clampToInt(mDepthStencil.stencilBackWritemask); break;
1873 case GL_STENCIL_CLEAR_VALUE: *params = mStencilClearValue; break;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001874 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1875 *params = mReadFramebuffer->getImplementationColorReadType(context);
1876 break;
1877 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1878 *params = mReadFramebuffer->getImplementationColorReadFormat(context);
1879 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001880 case GL_SAMPLE_BUFFERS:
1881 case GL_SAMPLES:
1882 {
Jamie Madill81c2e252017-09-09 23:32:46 -04001883 Framebuffer *framebuffer = mDrawFramebuffer;
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001884 if (framebuffer->checkStatus(context) == GL_FRAMEBUFFER_COMPLETE)
Shannon Woods53a94a82014-06-24 15:20:36 -04001885 {
1886 switch (pname)
1887 {
Jamie Madilla0016b72017-07-14 14:30:46 -04001888 case GL_SAMPLE_BUFFERS:
1889 if (framebuffer->getSamples(context) != 0)
1890 {
1891 *params = 1;
1892 }
1893 else
1894 {
1895 *params = 0;
1896 }
1897 break;
1898 case GL_SAMPLES:
1899 *params = framebuffer->getSamples(context);
1900 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001901 }
1902 }
1903 else
1904 {
1905 *params = 0;
1906 }
1907 }
1908 break;
1909 case GL_VIEWPORT:
1910 params[0] = mViewport.x;
1911 params[1] = mViewport.y;
1912 params[2] = mViewport.width;
1913 params[3] = mViewport.height;
1914 break;
1915 case GL_SCISSOR_BOX:
1916 params[0] = mScissor.x;
1917 params[1] = mScissor.y;
1918 params[2] = mScissor.width;
1919 params[3] = mScissor.height;
1920 break;
1921 case GL_CULL_FACE_MODE: *params = mRasterizer.cullMode; break;
1922 case GL_FRONT_FACE: *params = mRasterizer.frontFace; break;
1923 case GL_RED_BITS:
1924 case GL_GREEN_BITS:
1925 case GL_BLUE_BITS:
1926 case GL_ALPHA_BITS:
1927 {
Jamie Madill81c2e252017-09-09 23:32:46 -04001928 Framebuffer *framebuffer = getDrawFramebuffer();
1929 const FramebufferAttachment *colorbuffer = framebuffer->getFirstColorbuffer();
Shannon Woods53a94a82014-06-24 15:20:36 -04001930
1931 if (colorbuffer)
1932 {
1933 switch (pname)
1934 {
1935 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1936 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1937 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1938 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1939 }
1940 }
1941 else
1942 {
1943 *params = 0;
1944 }
1945 }
1946 break;
1947 case GL_DEPTH_BITS:
1948 {
Jamie Madill81c2e252017-09-09 23:32:46 -04001949 const Framebuffer *framebuffer = getDrawFramebuffer();
1950 const FramebufferAttachment *depthbuffer = framebuffer->getDepthbuffer();
Shannon Woods53a94a82014-06-24 15:20:36 -04001951
1952 if (depthbuffer)
1953 {
1954 *params = depthbuffer->getDepthSize();
1955 }
1956 else
1957 {
1958 *params = 0;
1959 }
1960 }
1961 break;
1962 case GL_STENCIL_BITS:
1963 {
Jamie Madill81c2e252017-09-09 23:32:46 -04001964 const Framebuffer *framebuffer = getDrawFramebuffer();
1965 const FramebufferAttachment *stencilbuffer = framebuffer->getStencilbuffer();
Shannon Woods53a94a82014-06-24 15:20:36 -04001966
1967 if (stencilbuffer)
1968 {
1969 *params = stencilbuffer->getStencilSize();
1970 }
1971 else
1972 {
1973 *params = 0;
1974 }
1975 }
1976 break;
1977 case GL_TEXTURE_BINDING_2D:
Shannon Woods2df6a602014-09-26 16:12:07 -04001978 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001979 *params = getSamplerTextureId(static_cast<unsigned int>(mActiveSampler), GL_TEXTURE_2D);
Shannon Woods53a94a82014-06-24 15:20:36 -04001980 break;
Corentin Wallez13c0dd42017-07-04 18:27:01 -04001981 case GL_TEXTURE_BINDING_RECTANGLE_ANGLE:
1982 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
1983 *params = getSamplerTextureId(static_cast<unsigned int>(mActiveSampler),
1984 GL_TEXTURE_RECTANGLE_ANGLE);
1985 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001986 case GL_TEXTURE_BINDING_CUBE_MAP:
Shannon Woods2df6a602014-09-26 16:12:07 -04001987 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001988 *params =
1989 getSamplerTextureId(static_cast<unsigned int>(mActiveSampler), GL_TEXTURE_CUBE_MAP);
Shannon Woods53a94a82014-06-24 15:20:36 -04001990 break;
1991 case GL_TEXTURE_BINDING_3D:
Shannon Woods2df6a602014-09-26 16:12:07 -04001992 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001993 *params = getSamplerTextureId(static_cast<unsigned int>(mActiveSampler), GL_TEXTURE_3D);
Shannon Woods53a94a82014-06-24 15:20:36 -04001994 break;
1995 case GL_TEXTURE_BINDING_2D_ARRAY:
Shannon Woods2df6a602014-09-26 16:12:07 -04001996 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001997 *params =
1998 getSamplerTextureId(static_cast<unsigned int>(mActiveSampler), GL_TEXTURE_2D_ARRAY);
Shannon Woods53a94a82014-06-24 15:20:36 -04001999 break;
JiangYizhou24fe74c2017-07-06 16:56:50 +08002000 case GL_TEXTURE_BINDING_2D_MULTISAMPLE:
2001 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
2002 *params = getSamplerTextureId(static_cast<unsigned int>(mActiveSampler),
2003 GL_TEXTURE_2D_MULTISAMPLE);
2004 break;
John Bauman18319182016-09-28 14:22:27 -07002005 case GL_TEXTURE_BINDING_EXTERNAL_OES:
2006 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
2007 *params = getSamplerTextureId(static_cast<unsigned int>(mActiveSampler),
2008 GL_TEXTURE_EXTERNAL_OES);
2009 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002010 case GL_UNIFORM_BUFFER_BINDING:
2011 *params = mGenericUniformBuffer.id();
2012 break;
Frank Henigman22581ff2015-11-06 14:25:54 -05002013 case GL_TRANSFORM_FEEDBACK_BINDING:
Frank Henigmanb0f0b812015-11-21 17:49:29 -05002014 *params = mTransformFeedback.id();
Frank Henigman22581ff2015-11-06 14:25:54 -05002015 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002016 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING:
Geoff Lang045536b2015-03-27 15:17:18 -04002017 *params = mTransformFeedback->getGenericBuffer().id();
Shannon Woods53a94a82014-06-24 15:20:36 -04002018 break;
2019 case GL_COPY_READ_BUFFER_BINDING:
2020 *params = mCopyReadBuffer.id();
2021 break;
2022 case GL_COPY_WRITE_BUFFER_BINDING:
2023 *params = mCopyWriteBuffer.id();
2024 break;
2025 case GL_PIXEL_PACK_BUFFER_BINDING:
2026 *params = mPack.pixelBuffer.id();
2027 break;
2028 case GL_PIXEL_UNPACK_BUFFER_BINDING:
2029 *params = mUnpack.pixelBuffer.id();
2030 break;
Olli Etuaho86821db2016-03-04 12:05:47 +02002031 case GL_READ_BUFFER:
2032 *params = mReadFramebuffer->getReadBufferState();
2033 break;
2034 case GL_SAMPLER_BINDING:
2035 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
2036 *params = getSamplerId(static_cast<GLuint>(mActiveSampler));
2037 break;
Geoff Lang70d0f492015-12-10 17:45:46 -05002038 case GL_DEBUG_LOGGED_MESSAGES:
2039 *params = static_cast<GLint>(mDebug.getMessageCount());
2040 break;
2041 case GL_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH:
2042 *params = static_cast<GLint>(mDebug.getNextMessageLength());
2043 break;
2044 case GL_DEBUG_GROUP_STACK_DEPTH:
2045 *params = static_cast<GLint>(mDebug.getGroupStackDepth());
2046 break;
Sami Väisänen74c23472016-05-09 17:30:30 +03002047 case GL_MULTISAMPLE_EXT:
2048 *params = static_cast<GLint>(mMultiSampling);
2049 break;
2050 case GL_SAMPLE_ALPHA_TO_ONE_EXT:
2051 *params = static_cast<GLint>(mSampleAlphaToOne);
Sami Väisänena797e062016-05-12 15:23:40 +03002052 case GL_COVERAGE_MODULATION_CHROMIUM:
2053 *params = static_cast<GLint>(mCoverageModulation);
Sami Väisänen74c23472016-05-09 17:30:30 +03002054 break;
Jiajia Qin6eafb042016-12-27 17:04:07 +08002055 case GL_ATOMIC_COUNTER_BUFFER_BINDING:
2056 *params = mGenericAtomicCounterBuffer.id();
2057 break;
Jiajia Qinf546e7d2017-03-27 14:12:59 +08002058 case GL_SHADER_STORAGE_BUFFER_BINDING:
2059 *params = mGenericShaderStorageBuffer.id();
2060 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002061 default:
2062 UNREACHABLE();
2063 break;
2064 }
2065}
2066
Geoff Lang70d0f492015-12-10 17:45:46 -05002067void State::getPointerv(GLenum pname, void **params) const
2068{
2069 switch (pname)
2070 {
2071 case GL_DEBUG_CALLBACK_FUNCTION:
2072 *params = reinterpret_cast<void *>(mDebug.getCallback());
2073 break;
2074 case GL_DEBUG_CALLBACK_USER_PARAM:
2075 *params = const_cast<void *>(mDebug.getUserParam());
2076 break;
2077 default:
2078 UNREACHABLE();
2079 break;
2080 }
2081}
2082
Martin Radev66fb8202016-07-28 11:45:20 +03002083void State::getIntegeri_v(GLenum target, GLuint index, GLint *data)
Shannon Woods53a94a82014-06-24 15:20:36 -04002084{
2085 switch (target)
2086 {
2087 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002088 ASSERT(static_cast<size_t>(index) < mTransformFeedback->getIndexedBufferCount());
2089 *data = mTransformFeedback->getIndexedBuffer(index).id();
2090 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002091 case GL_UNIFORM_BUFFER_BINDING:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002092 ASSERT(static_cast<size_t>(index) < mUniformBuffers.size());
2093 *data = mUniformBuffers[index].id();
2094 break;
2095 case GL_ATOMIC_COUNTER_BUFFER_BINDING:
2096 ASSERT(static_cast<size_t>(index) < mAtomicCounterBuffers.size());
2097 *data = mAtomicCounterBuffers[index].id();
2098 break;
Jiajia Qinf546e7d2017-03-27 14:12:59 +08002099 case GL_SHADER_STORAGE_BUFFER_BINDING:
2100 ASSERT(static_cast<size_t>(index) < mShaderStorageBuffers.size());
2101 *data = mShaderStorageBuffers[index].id();
2102 break;
Shao80957d92017-02-20 21:25:59 +08002103 case GL_VERTEX_BINDING_BUFFER:
2104 ASSERT(static_cast<size_t>(index) < mVertexArray->getMaxBindings());
Martin Radevdd5f27e2017-06-07 10:17:09 +03002105 *data = mVertexArray->getVertexBinding(index).getBuffer().id();
Shao80957d92017-02-20 21:25:59 +08002106 break;
2107 case GL_VERTEX_BINDING_DIVISOR:
2108 ASSERT(static_cast<size_t>(index) < mVertexArray->getMaxBindings());
Martin Radevdd5f27e2017-06-07 10:17:09 +03002109 *data = mVertexArray->getVertexBinding(index).getDivisor();
Shao80957d92017-02-20 21:25:59 +08002110 break;
2111 case GL_VERTEX_BINDING_OFFSET:
2112 ASSERT(static_cast<size_t>(index) < mVertexArray->getMaxBindings());
Martin Radevdd5f27e2017-06-07 10:17:09 +03002113 *data = static_cast<GLuint>(mVertexArray->getVertexBinding(index).getOffset());
Shao80957d92017-02-20 21:25:59 +08002114 break;
2115 case GL_VERTEX_BINDING_STRIDE:
2116 ASSERT(static_cast<size_t>(index) < mVertexArray->getMaxBindings());
Martin Radevdd5f27e2017-06-07 10:17:09 +03002117 *data = mVertexArray->getVertexBinding(index).getStride();
Shao80957d92017-02-20 21:25:59 +08002118 break;
Jiawei Shaodb342272017-09-27 10:21:45 +08002119 case GL_SAMPLE_MASK_VALUE:
2120 ASSERT(static_cast<size_t>(index) < mSampleMaskValues.size());
2121 *data = mSampleMaskValues[index];
2122 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002123 default:
Martin Radev66fb8202016-07-28 11:45:20 +03002124 UNREACHABLE();
2125 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002126 }
Shannon Woods53a94a82014-06-24 15:20:36 -04002127}
2128
Martin Radev66fb8202016-07-28 11:45:20 +03002129void State::getInteger64i_v(GLenum target, GLuint index, GLint64 *data)
Shannon Woods53a94a82014-06-24 15:20:36 -04002130{
2131 switch (target)
2132 {
2133 case GL_TRANSFORM_FEEDBACK_BUFFER_START:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002134 ASSERT(static_cast<size_t>(index) < mTransformFeedback->getIndexedBufferCount());
2135 *data = mTransformFeedback->getIndexedBuffer(index).getOffset();
2136 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002137 case GL_TRANSFORM_FEEDBACK_BUFFER_SIZE:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002138 ASSERT(static_cast<size_t>(index) < mTransformFeedback->getIndexedBufferCount());
2139 *data = mTransformFeedback->getIndexedBuffer(index).getSize();
2140 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002141 case GL_UNIFORM_BUFFER_START:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002142 ASSERT(static_cast<size_t>(index) < mUniformBuffers.size());
2143 *data = mUniformBuffers[index].getOffset();
2144 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002145 case GL_UNIFORM_BUFFER_SIZE:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002146 ASSERT(static_cast<size_t>(index) < mUniformBuffers.size());
2147 *data = mUniformBuffers[index].getSize();
2148 break;
2149 case GL_ATOMIC_COUNTER_BUFFER_START:
2150 ASSERT(static_cast<size_t>(index) < mAtomicCounterBuffers.size());
2151 *data = mAtomicCounterBuffers[index].getOffset();
2152 break;
2153 case GL_ATOMIC_COUNTER_BUFFER_SIZE:
2154 ASSERT(static_cast<size_t>(index) < mAtomicCounterBuffers.size());
2155 *data = mAtomicCounterBuffers[index].getSize();
2156 break;
Jiajia Qinf546e7d2017-03-27 14:12:59 +08002157 case GL_SHADER_STORAGE_BUFFER_START:
2158 ASSERT(static_cast<size_t>(index) < mShaderStorageBuffers.size());
2159 *data = mShaderStorageBuffers[index].getOffset();
2160 break;
2161 case GL_SHADER_STORAGE_BUFFER_SIZE:
2162 ASSERT(static_cast<size_t>(index) < mShaderStorageBuffers.size());
2163 *data = mShaderStorageBuffers[index].getSize();
2164 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002165 default:
Martin Radev66fb8202016-07-28 11:45:20 +03002166 UNREACHABLE();
2167 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002168 }
Martin Radev66fb8202016-07-28 11:45:20 +03002169}
Shannon Woods53a94a82014-06-24 15:20:36 -04002170
Martin Radev66fb8202016-07-28 11:45:20 +03002171void State::getBooleani_v(GLenum target, GLuint index, GLboolean *data)
2172{
2173 UNREACHABLE();
Shannon Woods53a94a82014-06-24 15:20:36 -04002174}
2175
Jamie Madilld9ba4f72014-08-04 10:47:59 -04002176bool State::hasMappedBuffer(GLenum target) const
2177{
2178 if (target == GL_ARRAY_BUFFER)
2179 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +08002180 const VertexArray *vao = getVertexArray();
Jamie Madilleea3a6e2015-04-15 10:02:48 -04002181 const auto &vertexAttribs = vao->getVertexAttributes();
Jiawei-Shao2597fb62016-12-09 16:38:02 +08002182 const auto &vertexBindings = vao->getVertexBindings();
Jamie Madill8e344942015-07-09 14:22:07 -04002183 size_t maxEnabledAttrib = vao->getMaxEnabledAttribute();
Jamie Madillaebf9dd2015-04-28 12:39:07 -04002184 for (size_t attribIndex = 0; attribIndex < maxEnabledAttrib; attribIndex++)
Jamie Madilld9ba4f72014-08-04 10:47:59 -04002185 {
Jamie Madill81c2e252017-09-09 23:32:46 -04002186 const VertexAttribute &vertexAttrib = vertexAttribs[attribIndex];
Martin Radevdd5f27e2017-06-07 10:17:09 +03002187 auto *boundBuffer = vertexBindings[vertexAttrib.bindingIndex].getBuffer().get();
Jamie Madilld9ba4f72014-08-04 10:47:59 -04002188 if (vertexAttrib.enabled && boundBuffer && boundBuffer->isMapped())
2189 {
2190 return true;
2191 }
2192 }
2193
2194 return false;
2195 }
2196 else
2197 {
2198 Buffer *buffer = getTargetBuffer(target);
2199 return (buffer && buffer->isMapped());
2200 }
2201}
2202
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002203void State::syncDirtyObjects(const Context *context)
Jamie Madillc9d442d2016-01-20 11:17:24 -05002204{
2205 if (!mDirtyObjects.any())
2206 return;
2207
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002208 syncDirtyObjects(context, mDirtyObjects);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002209}
2210
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002211void State::syncDirtyObjects(const Context *context, const DirtyObjects &bitset)
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002212{
Jamie Madill6de51852017-04-12 09:53:01 -04002213 for (auto dirtyObject : bitset)
Jamie Madillc9d442d2016-01-20 11:17:24 -05002214 {
2215 switch (dirtyObject)
2216 {
2217 case DIRTY_OBJECT_READ_FRAMEBUFFER:
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002218 ASSERT(mReadFramebuffer);
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002219 mReadFramebuffer->syncState(context);
Jamie Madillc9d442d2016-01-20 11:17:24 -05002220 break;
2221 case DIRTY_OBJECT_DRAW_FRAMEBUFFER:
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002222 ASSERT(mDrawFramebuffer);
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002223 mDrawFramebuffer->syncState(context);
Jamie Madillc9d442d2016-01-20 11:17:24 -05002224 break;
2225 case DIRTY_OBJECT_VERTEX_ARRAY:
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002226 ASSERT(mVertexArray);
Jamie Madill06ef36b2017-09-09 23:32:46 -04002227 mVertexArray->syncState(context);
Jamie Madillc9d442d2016-01-20 11:17:24 -05002228 break;
Jamie Madill81c2e252017-09-09 23:32:46 -04002229 case DIRTY_OBJECT_PROGRAM_TEXTURES:
2230 syncProgramTextures(context);
2231 break;
2232
Jamie Madillc9d442d2016-01-20 11:17:24 -05002233 default:
2234 UNREACHABLE();
2235 break;
2236 }
2237 }
2238
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002239 mDirtyObjects &= ~bitset;
2240}
2241
Jamie Madill81c2e252017-09-09 23:32:46 -04002242void State::syncProgramTextures(const Context *context)
2243{
Jamie Madill81c2e252017-09-09 23:32:46 -04002244 // TODO(jmadill): Fine-grained updates.
2245 if (!mProgram)
2246 {
2247 return;
2248 }
2249
2250 ASSERT(mDirtyObjects[DIRTY_OBJECT_PROGRAM_TEXTURES]);
2251 mDirtyBits.set(DIRTY_BIT_TEXTURE_BINDINGS);
2252
Jamie Madill0f80ed82017-09-19 00:24:56 -04002253 ActiveTextureMask newActiveTextures;
2254
Jamie Madill81c2e252017-09-09 23:32:46 -04002255 for (const SamplerBinding &samplerBinding : mProgram->getSamplerBindings())
2256 {
2257 if (samplerBinding.unreferenced)
2258 continue;
2259
2260 GLenum textureType = samplerBinding.textureType;
2261 for (GLuint textureUnitIndex : samplerBinding.boundTextureUnits)
2262 {
2263 Texture *texture = getSamplerTexture(textureUnitIndex, textureType);
Jamie Madill06ef36b2017-09-09 23:32:46 -04002264 Sampler *sampler = getSampler(textureUnitIndex);
Jamie Madill0f80ed82017-09-19 00:24:56 -04002265 ASSERT(static_cast<size_t>(textureUnitIndex) < mCompleteTextureCache.size());
2266 ASSERT(static_cast<size_t>(textureUnitIndex) < newActiveTextures.size());
Jamie Madill81c2e252017-09-09 23:32:46 -04002267
2268 if (texture != nullptr)
2269 {
Jamie Madill81c2e252017-09-09 23:32:46 -04002270 // Mark the texture binding bit as dirty if the texture completeness changes.
2271 // TODO(jmadill): Use specific dirty bit for completeness change.
2272 if (texture->isSamplerComplete(context, sampler))
2273 {
Jamie Madill06ef36b2017-09-09 23:32:46 -04002274 texture->syncState();
Jamie Madill81c2e252017-09-09 23:32:46 -04002275 mCompleteTextureCache[textureUnitIndex] = texture;
2276 }
Jamie Madill0f80ed82017-09-19 00:24:56 -04002277 else
2278 {
2279 mCompleteTextureCache[textureUnitIndex] = nullptr;
2280 }
Jamie Madill81c2e252017-09-09 23:32:46 -04002281
2282 // Bind the texture unconditionally, to recieve completeness change notifications.
2283 mCompleteTextureBindings[textureUnitIndex].bind(texture->getDirtyChannel());
Jamie Madill0f80ed82017-09-19 00:24:56 -04002284 newActiveTextures.set(textureUnitIndex);
2285 mCompleteTexturesMask.set(textureUnitIndex);
Jamie Madill81c2e252017-09-09 23:32:46 -04002286 }
2287
Jamie Madill06ef36b2017-09-09 23:32:46 -04002288 if (sampler != nullptr)
2289 {
2290 sampler->syncState(context);
2291 }
Jamie Madill81c2e252017-09-09 23:32:46 -04002292 }
2293 }
Jamie Madill0f80ed82017-09-19 00:24:56 -04002294
2295 // Unset now missing textures.
2296 ActiveTextureMask negativeMask = mCompleteTexturesMask & ~newActiveTextures;
2297 if (negativeMask.any())
2298 {
2299 for (auto textureIndex : negativeMask)
2300 {
2301 mCompleteTextureBindings[textureIndex].reset();
2302 mCompleteTextureCache[textureIndex] = nullptr;
2303 mCompleteTexturesMask.reset(textureIndex);
2304 }
2305 }
Jamie Madill81c2e252017-09-09 23:32:46 -04002306}
2307
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002308void State::syncDirtyObject(const Context *context, GLenum target)
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002309{
2310 DirtyObjects localSet;
2311
2312 switch (target)
2313 {
2314 case GL_READ_FRAMEBUFFER:
2315 localSet.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
2316 break;
2317 case GL_DRAW_FRAMEBUFFER:
2318 localSet.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
2319 break;
2320 case GL_FRAMEBUFFER:
2321 localSet.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
2322 localSet.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
2323 break;
2324 case GL_VERTEX_ARRAY:
2325 localSet.set(DIRTY_OBJECT_VERTEX_ARRAY);
2326 break;
Jamie Madill81c2e252017-09-09 23:32:46 -04002327 case GL_TEXTURE:
2328 case GL_SAMPLER:
2329 case GL_PROGRAM:
2330 localSet.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
2331 break;
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002332 }
2333
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002334 syncDirtyObjects(context, localSet);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002335}
2336
2337void State::setObjectDirty(GLenum target)
2338{
2339 switch (target)
2340 {
2341 case GL_READ_FRAMEBUFFER:
2342 mDirtyObjects.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
2343 break;
2344 case GL_DRAW_FRAMEBUFFER:
2345 mDirtyObjects.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
2346 break;
2347 case GL_FRAMEBUFFER:
2348 mDirtyObjects.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
2349 mDirtyObjects.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
2350 break;
2351 case GL_VERTEX_ARRAY:
2352 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
2353 break;
Jamie Madill81c2e252017-09-09 23:32:46 -04002354 case GL_TEXTURE:
2355 case GL_SAMPLER:
Jamie Madill81c2e252017-09-09 23:32:46 -04002356 case GL_PROGRAM:
2357 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
2358 mDirtyBits.set(DIRTY_BIT_TEXTURE_BINDINGS);
2359 break;
Jamie Madilla779b612017-07-24 11:46:05 -04002360 }
2361}
2362
2363void State::onProgramExecutableChange(Program *program)
2364{
2365 // OpenGL Spec:
2366 // "If LinkProgram or ProgramBinary successfully re-links a program object
2367 // that was already in use as a result of a previous call to UseProgram, then the
2368 // generated executable code will be installed as part of the current rendering state."
2369 if (program->isLinked() && mProgram == program)
2370 {
2371 mDirtyBits.set(DIRTY_BIT_PROGRAM_EXECUTABLE);
Jamie Madill81c2e252017-09-09 23:32:46 -04002372 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002373 }
Shannon Woods53a94a82014-06-24 15:20:36 -04002374}
Jamie Madillc9d442d2016-01-20 11:17:24 -05002375
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002376void State::setImageUnit(const Context *context,
2377 GLuint unit,
2378 Texture *texture,
2379 GLint level,
2380 GLboolean layered,
2381 GLint layer,
2382 GLenum access,
2383 GLenum format)
2384{
2385 mImageUnits[unit].texture.set(context, texture);
2386 mImageUnits[unit].level = level;
2387 mImageUnits[unit].layered = layered;
2388 mImageUnits[unit].layer = layer;
2389 mImageUnits[unit].access = access;
2390 mImageUnits[unit].format = format;
2391}
2392
2393const ImageUnit &State::getImageUnit(GLuint unit) const
2394{
2395 return mImageUnits[unit];
2396}
2397
Jamie Madill81c2e252017-09-09 23:32:46 -04002398// Handle a dirty texture event.
Jamie Madill05b35b22017-10-03 09:01:44 -04002399void State::signal(size_t textureIndex, InitState initState)
Jamie Madill81c2e252017-09-09 23:32:46 -04002400{
2401 // Conservatively assume all textures are dirty.
2402 // TODO(jmadill): More fine-grained update.
2403 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
2404}
2405
Jamie Madill05b35b22017-10-03 09:01:44 -04002406Error State::clearUnclearedActiveTextures(const Context *context)
2407{
2408 if (!mRobustResourceInit)
2409 {
2410 return NoError();
2411 }
2412
2413 // TODO(jmadill): Investigate improving the speed here.
2414 for (Texture *texture : mCompleteTextureCache)
2415 {
2416 if (texture)
2417 {
2418 ANGLE_TRY(texture->ensureInitialized(context));
2419 }
2420 }
2421 return NoError();
2422}
2423
Jamie Madillc9d442d2016-01-20 11:17:24 -05002424} // namespace gl