blob: f5260be521ea7bebf6e7a583bd3e82bcf69cdb04 [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
194 if (extensions.framebufferMultisample)
195 {
196 mMultiSampling = true;
197 mSampleAlphaToOne = false;
198 }
Sami Väisänena797e062016-05-12 15:23:40 +0300199
200 mCoverageModulation = GL_NONE;
Sami Väisänene45e53b2016-05-25 10:36:04 +0300201
202 angle::Matrix<GLfloat>::setToIdentity(mPathMatrixProj);
203 angle::Matrix<GLfloat>::setToIdentity(mPathMatrixMV);
204 mPathStencilFunc = GL_ALWAYS;
205 mPathStencilRef = 0;
206 mPathStencilMask = std::numeric_limits<GLuint>::max();
Jamie Madille08a1d32017-03-07 17:24:06 -0500207
208 mRobustResourceInit = robustResourceInit;
Jamie Madillc43be722017-07-13 16:22:14 -0400209 mProgramBinaryCacheEnabled = programBinaryCacheEnabled;
Shannon Woods53a94a82014-06-24 15:20:36 -0400210}
211
Jamie Madill6c1f6712017-02-14 19:08:04 -0500212void State::reset(const Context *context)
Shannon Woods53a94a82014-06-24 15:20:36 -0400213{
Jamie Madill8693bdb2017-09-02 15:32:14 -0400214 for (auto &bindingVec : mSamplerTextures)
Shannon Woods53a94a82014-06-24 15:20:36 -0400215 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400216 TextureBindingVector &textureVector = bindingVec.second;
Geoff Lang76b10c92014-09-05 16:28:14 -0400217 for (size_t textureIdx = 0; textureIdx < textureVector.size(); textureIdx++)
Shannon Woods53a94a82014-06-24 15:20:36 -0400218 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400219 textureVector[textureIdx].set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400220 }
221 }
Geoff Lang76b10c92014-09-05 16:28:14 -0400222 for (size_t samplerIdx = 0; samplerIdx < mSamplers.size(); samplerIdx++)
223 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400224 mSamplers[samplerIdx].set(context, nullptr);
Geoff Lang76b10c92014-09-05 16:28:14 -0400225 }
Shannon Woods53a94a82014-06-24 15:20:36 -0400226
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800227 for (auto &imageUnit : mImageUnits)
228 {
229 imageUnit.texture.set(context, nullptr);
230 imageUnit.level = 0;
231 imageUnit.layered = false;
232 imageUnit.layer = 0;
233 imageUnit.access = GL_READ_ONLY;
234 imageUnit.format = GL_R32UI;
235 }
236
Jamie Madill4928b7c2017-06-20 12:57:39 -0400237 mArrayBuffer.set(context, nullptr);
238 mDrawIndirectBuffer.set(context, nullptr);
239 mRenderbuffer.set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400240
Geoff Lang7dd2e102014-11-10 15:19:26 -0500241 if (mProgram)
242 {
Jamie Madill6c1f6712017-02-14 19:08:04 -0500243 mProgram->release(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500244 }
Yunchao Hed7297bf2017-04-19 15:27:10 +0800245 mProgram = nullptr;
Geoff Lang7dd2e102014-11-10 15:19:26 -0500246
Yunchao Hea336b902017-08-02 16:05:21 +0800247 mProgramPipeline.set(context, nullptr);
248
Jamie Madill4928b7c2017-06-20 12:57:39 -0400249 mTransformFeedback.set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400250
251 for (State::ActiveQueryMap::iterator i = mActiveQueries.begin(); i != mActiveQueries.end(); i++)
252 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400253 i->second.set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400254 }
255
Jamie Madill4928b7c2017-06-20 12:57:39 -0400256 mGenericUniformBuffer.set(context, nullptr);
Shannon Woods8299bb02014-09-26 18:55:43 -0400257 for (BufferVector::iterator bufItr = mUniformBuffers.begin(); bufItr != mUniformBuffers.end(); ++bufItr)
Shannon Woods53a94a82014-06-24 15:20:36 -0400258 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400259 bufItr->set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400260 }
261
Jamie Madill4928b7c2017-06-20 12:57:39 -0400262 mCopyReadBuffer.set(context, nullptr);
263 mCopyWriteBuffer.set(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400264
Jamie Madill4928b7c2017-06-20 12:57:39 -0400265 mPack.pixelBuffer.set(context, nullptr);
266 mUnpack.pixelBuffer.set(context, nullptr);
Geoff Lang7dd2e102014-11-10 15:19:26 -0500267
Jamie Madill4928b7c2017-06-20 12:57:39 -0400268 mGenericAtomicCounterBuffer.set(context, nullptr);
Jiajia Qin6eafb042016-12-27 17:04:07 +0800269 for (auto &buf : mAtomicCounterBuffers)
270 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400271 buf.set(context, nullptr);
Jiajia Qin6eafb042016-12-27 17:04:07 +0800272 }
273
Jamie Madill4928b7c2017-06-20 12:57:39 -0400274 mGenericShaderStorageBuffer.set(context, nullptr);
Jiajia Qinf546e7d2017-03-27 14:12:59 +0800275 for (auto &buf : mShaderStorageBuffers)
276 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400277 buf.set(context, nullptr);
Jiajia Qinf546e7d2017-03-27 14:12:59 +0800278 }
279
Sami Väisänene45e53b2016-05-25 10:36:04 +0300280 angle::Matrix<GLfloat>::setToIdentity(mPathMatrixProj);
281 angle::Matrix<GLfloat>::setToIdentity(mPathMatrixMV);
282 mPathStencilFunc = GL_ALWAYS;
283 mPathStencilRef = 0;
284 mPathStencilMask = std::numeric_limits<GLuint>::max();
285
Jamie Madill1b94d432015-08-07 13:23:23 -0400286 // TODO(jmadill): Is this necessary?
287 setAllDirtyBits();
Shannon Woods53a94a82014-06-24 15:20:36 -0400288}
289
290const RasterizerState &State::getRasterizerState() const
291{
292 return mRasterizer;
293}
294
295const BlendState &State::getBlendState() const
296{
297 return mBlend;
298}
299
300const DepthStencilState &State::getDepthStencilState() const
301{
302 return mDepthStencil;
303}
304
Jamie Madillf75ab352015-03-16 10:46:52 -0400305void State::setColorClearValue(float red, float green, float blue, float alpha)
Shannon Woods53a94a82014-06-24 15:20:36 -0400306{
307 mColorClearValue.red = red;
308 mColorClearValue.green = green;
309 mColorClearValue.blue = blue;
310 mColorClearValue.alpha = alpha;
Jamie Madill1b94d432015-08-07 13:23:23 -0400311 mDirtyBits.set(DIRTY_BIT_CLEAR_COLOR);
Shannon Woods53a94a82014-06-24 15:20:36 -0400312}
313
Jamie Madillf75ab352015-03-16 10:46:52 -0400314void State::setDepthClearValue(float depth)
Shannon Woods53a94a82014-06-24 15:20:36 -0400315{
316 mDepthClearValue = depth;
Jamie Madill1b94d432015-08-07 13:23:23 -0400317 mDirtyBits.set(DIRTY_BIT_CLEAR_DEPTH);
Shannon Woods53a94a82014-06-24 15:20:36 -0400318}
319
Jamie Madillf75ab352015-03-16 10:46:52 -0400320void State::setStencilClearValue(int stencil)
Shannon Woods53a94a82014-06-24 15:20:36 -0400321{
322 mStencilClearValue = stencil;
Jamie Madill1b94d432015-08-07 13:23:23 -0400323 mDirtyBits.set(DIRTY_BIT_CLEAR_STENCIL);
Shannon Woods53a94a82014-06-24 15:20:36 -0400324}
325
Shannon Woods53a94a82014-06-24 15:20:36 -0400326void State::setColorMask(bool red, bool green, bool blue, bool alpha)
327{
328 mBlend.colorMaskRed = red;
329 mBlend.colorMaskGreen = green;
330 mBlend.colorMaskBlue = blue;
331 mBlend.colorMaskAlpha = alpha;
Jamie Madill1b94d432015-08-07 13:23:23 -0400332 mDirtyBits.set(DIRTY_BIT_COLOR_MASK);
Shannon Woods53a94a82014-06-24 15:20:36 -0400333}
334
335void State::setDepthMask(bool mask)
336{
337 mDepthStencil.depthMask = mask;
Jamie Madill1b94d432015-08-07 13:23:23 -0400338 mDirtyBits.set(DIRTY_BIT_DEPTH_MASK);
Shannon Woods53a94a82014-06-24 15:20:36 -0400339}
340
341bool State::isRasterizerDiscardEnabled() const
342{
343 return mRasterizer.rasterizerDiscard;
344}
345
346void State::setRasterizerDiscard(bool enabled)
347{
348 mRasterizer.rasterizerDiscard = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400349 mDirtyBits.set(DIRTY_BIT_RASTERIZER_DISCARD_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400350}
351
352bool State::isCullFaceEnabled() const
353{
354 return mRasterizer.cullFace;
355}
356
357void State::setCullFace(bool enabled)
358{
359 mRasterizer.cullFace = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400360 mDirtyBits.set(DIRTY_BIT_CULL_FACE_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400361}
362
363void State::setCullMode(GLenum mode)
364{
365 mRasterizer.cullMode = mode;
Jamie Madill1b94d432015-08-07 13:23:23 -0400366 mDirtyBits.set(DIRTY_BIT_CULL_FACE);
Shannon Woods53a94a82014-06-24 15:20:36 -0400367}
368
369void State::setFrontFace(GLenum front)
370{
371 mRasterizer.frontFace = front;
Jamie Madill1b94d432015-08-07 13:23:23 -0400372 mDirtyBits.set(DIRTY_BIT_FRONT_FACE);
Shannon Woods53a94a82014-06-24 15:20:36 -0400373}
374
375bool State::isDepthTestEnabled() const
376{
377 return mDepthStencil.depthTest;
378}
379
380void State::setDepthTest(bool enabled)
381{
382 mDepthStencil.depthTest = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400383 mDirtyBits.set(DIRTY_BIT_DEPTH_TEST_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400384}
385
386void State::setDepthFunc(GLenum depthFunc)
387{
388 mDepthStencil.depthFunc = depthFunc;
Jamie Madill1b94d432015-08-07 13:23:23 -0400389 mDirtyBits.set(DIRTY_BIT_DEPTH_FUNC);
Shannon Woods53a94a82014-06-24 15:20:36 -0400390}
391
392void State::setDepthRange(float zNear, float zFar)
393{
394 mNearZ = zNear;
395 mFarZ = zFar;
Jamie Madill1b94d432015-08-07 13:23:23 -0400396 mDirtyBits.set(DIRTY_BIT_DEPTH_RANGE);
Shannon Woods53a94a82014-06-24 15:20:36 -0400397}
398
Geoff Langd42f5b82015-04-16 14:03:29 -0400399float State::getNearPlane() const
Shannon Woods53a94a82014-06-24 15:20:36 -0400400{
Geoff Langd42f5b82015-04-16 14:03:29 -0400401 return mNearZ;
402}
403
404float State::getFarPlane() const
405{
406 return mFarZ;
Shannon Woods53a94a82014-06-24 15:20:36 -0400407}
408
409bool State::isBlendEnabled() const
410{
411 return mBlend.blend;
412}
413
414void State::setBlend(bool enabled)
415{
416 mBlend.blend = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400417 mDirtyBits.set(DIRTY_BIT_BLEND_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400418}
419
420void State::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
421{
422 mBlend.sourceBlendRGB = sourceRGB;
423 mBlend.destBlendRGB = destRGB;
424 mBlend.sourceBlendAlpha = sourceAlpha;
425 mBlend.destBlendAlpha = destAlpha;
Jamie Madill1b94d432015-08-07 13:23:23 -0400426 mDirtyBits.set(DIRTY_BIT_BLEND_FUNCS);
Shannon Woods53a94a82014-06-24 15:20:36 -0400427}
428
429void State::setBlendColor(float red, float green, float blue, float alpha)
430{
431 mBlendColor.red = red;
432 mBlendColor.green = green;
433 mBlendColor.blue = blue;
434 mBlendColor.alpha = alpha;
Jamie Madill1b94d432015-08-07 13:23:23 -0400435 mDirtyBits.set(DIRTY_BIT_BLEND_COLOR);
Shannon Woods53a94a82014-06-24 15:20:36 -0400436}
437
438void State::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
439{
440 mBlend.blendEquationRGB = rgbEquation;
441 mBlend.blendEquationAlpha = alphaEquation;
Jamie Madill1b94d432015-08-07 13:23:23 -0400442 mDirtyBits.set(DIRTY_BIT_BLEND_EQUATIONS);
Shannon Woods53a94a82014-06-24 15:20:36 -0400443}
444
445const ColorF &State::getBlendColor() const
446{
447 return mBlendColor;
448}
449
450bool State::isStencilTestEnabled() const
451{
452 return mDepthStencil.stencilTest;
453}
454
455void State::setStencilTest(bool enabled)
456{
457 mDepthStencil.stencilTest = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400458 mDirtyBits.set(DIRTY_BIT_STENCIL_TEST_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400459}
460
461void State::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
462{
463 mDepthStencil.stencilFunc = stencilFunc;
464 mStencilRef = (stencilRef > 0) ? stencilRef : 0;
465 mDepthStencil.stencilMask = stencilMask;
Jamie Madill1b94d432015-08-07 13:23:23 -0400466 mDirtyBits.set(DIRTY_BIT_STENCIL_FUNCS_FRONT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400467}
468
469void State::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
470{
471 mDepthStencil.stencilBackFunc = stencilBackFunc;
472 mStencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
473 mDepthStencil.stencilBackMask = stencilBackMask;
Jamie Madill1b94d432015-08-07 13:23:23 -0400474 mDirtyBits.set(DIRTY_BIT_STENCIL_FUNCS_BACK);
Shannon Woods53a94a82014-06-24 15:20:36 -0400475}
476
477void State::setStencilWritemask(GLuint stencilWritemask)
478{
479 mDepthStencil.stencilWritemask = stencilWritemask;
Jamie Madill1b94d432015-08-07 13:23:23 -0400480 mDirtyBits.set(DIRTY_BIT_STENCIL_WRITEMASK_FRONT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400481}
482
483void State::setStencilBackWritemask(GLuint stencilBackWritemask)
484{
485 mDepthStencil.stencilBackWritemask = stencilBackWritemask;
Jamie Madill1b94d432015-08-07 13:23:23 -0400486 mDirtyBits.set(DIRTY_BIT_STENCIL_WRITEMASK_BACK);
Shannon Woods53a94a82014-06-24 15:20:36 -0400487}
488
489void State::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
490{
491 mDepthStencil.stencilFail = stencilFail;
492 mDepthStencil.stencilPassDepthFail = stencilPassDepthFail;
493 mDepthStencil.stencilPassDepthPass = stencilPassDepthPass;
Jamie Madill1b94d432015-08-07 13:23:23 -0400494 mDirtyBits.set(DIRTY_BIT_STENCIL_OPS_FRONT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400495}
496
497void State::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
498{
499 mDepthStencil.stencilBackFail = stencilBackFail;
500 mDepthStencil.stencilBackPassDepthFail = stencilBackPassDepthFail;
501 mDepthStencil.stencilBackPassDepthPass = stencilBackPassDepthPass;
Jamie Madill1b94d432015-08-07 13:23:23 -0400502 mDirtyBits.set(DIRTY_BIT_STENCIL_OPS_BACK);
Shannon Woods53a94a82014-06-24 15:20:36 -0400503}
504
505GLint State::getStencilRef() const
506{
507 return mStencilRef;
508}
509
510GLint State::getStencilBackRef() const
511{
512 return mStencilBackRef;
513}
514
515bool State::isPolygonOffsetFillEnabled() const
516{
517 return mRasterizer.polygonOffsetFill;
518}
519
520void State::setPolygonOffsetFill(bool enabled)
521{
Jamie Madill1b94d432015-08-07 13:23:23 -0400522 mRasterizer.polygonOffsetFill = enabled;
523 mDirtyBits.set(DIRTY_BIT_POLYGON_OFFSET_FILL_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400524}
525
526void State::setPolygonOffsetParams(GLfloat factor, GLfloat units)
527{
528 // An application can pass NaN values here, so handle this gracefully
529 mRasterizer.polygonOffsetFactor = factor != factor ? 0.0f : factor;
530 mRasterizer.polygonOffsetUnits = units != units ? 0.0f : units;
Jamie Madill1b94d432015-08-07 13:23:23 -0400531 mDirtyBits.set(DIRTY_BIT_POLYGON_OFFSET);
Shannon Woods53a94a82014-06-24 15:20:36 -0400532}
533
534bool State::isSampleAlphaToCoverageEnabled() const
535{
536 return mBlend.sampleAlphaToCoverage;
537}
538
539void State::setSampleAlphaToCoverage(bool enabled)
540{
541 mBlend.sampleAlphaToCoverage = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400542 mDirtyBits.set(DIRTY_BIT_SAMPLE_ALPHA_TO_COVERAGE_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400543}
544
545bool State::isSampleCoverageEnabled() const
546{
547 return mSampleCoverage;
548}
549
550void State::setSampleCoverage(bool enabled)
551{
552 mSampleCoverage = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400553 mDirtyBits.set(DIRTY_BIT_SAMPLE_COVERAGE_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400554}
555
556void State::setSampleCoverageParams(GLclampf value, bool invert)
557{
558 mSampleCoverageValue = value;
559 mSampleCoverageInvert = invert;
Jamie Madill1b94d432015-08-07 13:23:23 -0400560 mDirtyBits.set(DIRTY_BIT_SAMPLE_COVERAGE);
Shannon Woods53a94a82014-06-24 15:20:36 -0400561}
562
Geoff Lang0fbb6002015-04-16 11:11:53 -0400563GLclampf State::getSampleCoverageValue() const
Shannon Woods53a94a82014-06-24 15:20:36 -0400564{
Geoff Lang0fbb6002015-04-16 11:11:53 -0400565 return mSampleCoverageValue;
566}
Shannon Woods53a94a82014-06-24 15:20:36 -0400567
Geoff Lang0fbb6002015-04-16 11:11:53 -0400568bool State::getSampleCoverageInvert() const
569{
570 return mSampleCoverageInvert;
Shannon Woods53a94a82014-06-24 15:20:36 -0400571}
572
Jiawei Shaodb342272017-09-27 10:21:45 +0800573bool State::isSampleMaskEnabled() const
574{
575 return mSampleMask;
576}
577
578void State::setSampleMaskEnabled(bool enabled)
579{
580 mSampleMask = enabled;
581 mDirtyBits.set(DIRTY_BIT_SAMPLE_MASK_ENABLED);
582}
583
584void State::setSampleMaskParams(GLuint maskNumber, GLbitfield mask)
585{
586 ASSERT(maskNumber < mMaxSampleMaskWords);
587 mSampleMaskValues[maskNumber] = mask;
588 mDirtyBits.set(DIRTY_BIT_SAMPLE_MASK_WORD_0 + maskNumber);
589}
590
591GLbitfield State::getSampleMaskWord(GLuint maskNumber) const
592{
593 ASSERT(maskNumber < mMaxSampleMaskWords);
594 return mSampleMaskValues[maskNumber];
595}
596
597GLuint State::getMaxSampleMaskWords() const
598{
599 return mMaxSampleMaskWords;
600}
601
Sami Väisänen74c23472016-05-09 17:30:30 +0300602void State::setSampleAlphaToOne(bool enabled)
603{
604 mSampleAlphaToOne = enabled;
605 mDirtyBits.set(DIRTY_BIT_SAMPLE_ALPHA_TO_ONE);
606}
607
608bool State::isSampleAlphaToOneEnabled() const
609{
610 return mSampleAlphaToOne;
611}
612
613void State::setMultisampling(bool enabled)
614{
615 mMultiSampling = enabled;
616 mDirtyBits.set(DIRTY_BIT_MULTISAMPLING);
617}
618
619bool State::isMultisamplingEnabled() const
620{
621 return mMultiSampling;
622}
623
Shannon Woods53a94a82014-06-24 15:20:36 -0400624bool State::isScissorTestEnabled() const
625{
626 return mScissorTest;
627}
628
629void State::setScissorTest(bool enabled)
630{
631 mScissorTest = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400632 mDirtyBits.set(DIRTY_BIT_SCISSOR_TEST_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400633}
634
635void State::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
636{
637 mScissor.x = x;
638 mScissor.y = y;
639 mScissor.width = width;
640 mScissor.height = height;
Jamie Madill1b94d432015-08-07 13:23:23 -0400641 mDirtyBits.set(DIRTY_BIT_SCISSOR);
Shannon Woods53a94a82014-06-24 15:20:36 -0400642}
643
644const Rectangle &State::getScissor() const
645{
646 return mScissor;
647}
648
649bool State::isDitherEnabled() const
650{
651 return mBlend.dither;
652}
653
654void State::setDither(bool enabled)
655{
656 mBlend.dither = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400657 mDirtyBits.set(DIRTY_BIT_DITHER_ENABLED);
Shannon Woods53a94a82014-06-24 15:20:36 -0400658}
659
Jamie Madillb4b53c52015-02-03 15:22:48 -0500660bool State::isPrimitiveRestartEnabled() const
661{
662 return mPrimitiveRestart;
663}
664
665void State::setPrimitiveRestart(bool enabled)
666{
667 mPrimitiveRestart = enabled;
Jamie Madill1b94d432015-08-07 13:23:23 -0400668 mDirtyBits.set(DIRTY_BIT_PRIMITIVE_RESTART_ENABLED);
Jamie Madillb4b53c52015-02-03 15:22:48 -0500669}
670
Shannon Woods53a94a82014-06-24 15:20:36 -0400671void State::setEnableFeature(GLenum feature, bool enabled)
672{
673 switch (feature)
674 {
Sami Väisänen74c23472016-05-09 17:30:30 +0300675 case GL_MULTISAMPLE_EXT: setMultisampling(enabled); break;
676 case GL_SAMPLE_ALPHA_TO_ONE_EXT: setSampleAlphaToOne(enabled); break;
Shannon Woods53a94a82014-06-24 15:20:36 -0400677 case GL_CULL_FACE: setCullFace(enabled); break;
678 case GL_POLYGON_OFFSET_FILL: setPolygonOffsetFill(enabled); break;
679 case GL_SAMPLE_ALPHA_TO_COVERAGE: setSampleAlphaToCoverage(enabled); break;
680 case GL_SAMPLE_COVERAGE: setSampleCoverage(enabled); break;
681 case GL_SCISSOR_TEST: setScissorTest(enabled); break;
682 case GL_STENCIL_TEST: setStencilTest(enabled); break;
683 case GL_DEPTH_TEST: setDepthTest(enabled); break;
684 case GL_BLEND: setBlend(enabled); break;
685 case GL_DITHER: setDither(enabled); break;
Jamie Madillb4b53c52015-02-03 15:22:48 -0500686 case GL_PRIMITIVE_RESTART_FIXED_INDEX: setPrimitiveRestart(enabled); break;
Shannon Woods53a94a82014-06-24 15:20:36 -0400687 case GL_RASTERIZER_DISCARD: setRasterizerDiscard(enabled); break;
Geoff Lang3b573612016-10-31 14:08:10 -0400688 case GL_SAMPLE_MASK:
Jiawei Shaodb342272017-09-27 10:21:45 +0800689 setSampleMaskEnabled(enabled);
Geoff Lang3b573612016-10-31 14:08:10 -0400690 break;
Geoff Lang70d0f492015-12-10 17:45:46 -0500691 case GL_DEBUG_OUTPUT_SYNCHRONOUS:
692 mDebug.setOutputSynchronous(enabled);
693 break;
694 case GL_DEBUG_OUTPUT:
695 mDebug.setOutputEnabled(enabled);
696 break;
Geoff Lang1d2c41d2016-10-19 16:14:46 -0700697 case GL_FRAMEBUFFER_SRGB_EXT:
698 setFramebufferSRGB(enabled);
699 break;
Shannon Woods53a94a82014-06-24 15:20:36 -0400700 default: UNREACHABLE();
701 }
702}
703
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700704bool State::getEnableFeature(GLenum feature) const
Shannon Woods53a94a82014-06-24 15:20:36 -0400705{
706 switch (feature)
707 {
Sami Väisänen74c23472016-05-09 17:30:30 +0300708 case GL_MULTISAMPLE_EXT: return isMultisamplingEnabled();
709 case GL_SAMPLE_ALPHA_TO_ONE_EXT: return isSampleAlphaToOneEnabled();
Shannon Woods53a94a82014-06-24 15:20:36 -0400710 case GL_CULL_FACE: return isCullFaceEnabled();
711 case GL_POLYGON_OFFSET_FILL: return isPolygonOffsetFillEnabled();
712 case GL_SAMPLE_ALPHA_TO_COVERAGE: return isSampleAlphaToCoverageEnabled();
713 case GL_SAMPLE_COVERAGE: return isSampleCoverageEnabled();
714 case GL_SCISSOR_TEST: return isScissorTestEnabled();
715 case GL_STENCIL_TEST: return isStencilTestEnabled();
716 case GL_DEPTH_TEST: return isDepthTestEnabled();
717 case GL_BLEND: return isBlendEnabled();
718 case GL_DITHER: return isDitherEnabled();
Jamie Madillb4b53c52015-02-03 15:22:48 -0500719 case GL_PRIMITIVE_RESTART_FIXED_INDEX: return isPrimitiveRestartEnabled();
Shannon Woods53a94a82014-06-24 15:20:36 -0400720 case GL_RASTERIZER_DISCARD: return isRasterizerDiscardEnabled();
Geoff Langb5e997f2016-12-06 10:55:34 -0500721 case GL_SAMPLE_MASK:
Jiawei Shaodb342272017-09-27 10:21:45 +0800722 return isSampleMaskEnabled();
Geoff Lang70d0f492015-12-10 17:45:46 -0500723 case GL_DEBUG_OUTPUT_SYNCHRONOUS:
724 return mDebug.isOutputSynchronous();
725 case GL_DEBUG_OUTPUT:
726 return mDebug.isOutputEnabled();
Geoff Langf41a7152016-09-19 15:11:17 -0400727 case GL_BIND_GENERATES_RESOURCE_CHROMIUM:
728 return isBindGeneratesResourceEnabled();
Geoff Langfeb8c682017-02-13 16:07:35 -0500729 case GL_CLIENT_ARRAYS_ANGLE:
730 return areClientArraysEnabled();
Geoff Lang1d2c41d2016-10-19 16:14:46 -0700731 case GL_FRAMEBUFFER_SRGB_EXT:
732 return getFramebufferSRGB();
Jamie Madille08a1d32017-03-07 17:24:06 -0500733 case GL_CONTEXT_ROBUST_RESOURCE_INITIALIZATION_ANGLE:
734 return mRobustResourceInit;
Jamie Madillc43be722017-07-13 16:22:14 -0400735 case GL_PROGRAM_CACHE_ENABLED_ANGLE:
736 return mProgramBinaryCacheEnabled;
737
738 default:
739 UNREACHABLE();
740 return false;
Shannon Woods53a94a82014-06-24 15:20:36 -0400741 }
742}
743
744void State::setLineWidth(GLfloat width)
745{
746 mLineWidth = width;
Jamie Madill1b94d432015-08-07 13:23:23 -0400747 mDirtyBits.set(DIRTY_BIT_LINE_WIDTH);
Shannon Woods53a94a82014-06-24 15:20:36 -0400748}
749
Geoff Lang4b3f4162015-04-16 13:22:05 -0400750float State::getLineWidth() const
751{
752 return mLineWidth;
753}
754
Shannon Woods53a94a82014-06-24 15:20:36 -0400755void State::setGenerateMipmapHint(GLenum hint)
756{
757 mGenerateMipmapHint = hint;
Jamie Madill1b94d432015-08-07 13:23:23 -0400758 mDirtyBits.set(DIRTY_BIT_GENERATE_MIPMAP_HINT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400759}
760
761void State::setFragmentShaderDerivativeHint(GLenum hint)
762{
763 mFragmentShaderDerivativeHint = hint;
Jamie Madill1b94d432015-08-07 13:23:23 -0400764 mDirtyBits.set(DIRTY_BIT_SHADER_DERIVATIVE_HINT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400765 // TODO: Propagate the hint to shader translator so we can write
766 // ddx, ddx_coarse, or ddx_fine depending on the hint.
767 // Ignore for now. It is valid for implementations to ignore hint.
768}
769
Geoff Langf41a7152016-09-19 15:11:17 -0400770bool State::isBindGeneratesResourceEnabled() const
771{
772 return mBindGeneratesResource;
773}
774
Geoff Langfeb8c682017-02-13 16:07:35 -0500775bool State::areClientArraysEnabled() const
776{
777 return mClientArraysEnabled;
778}
779
Shannon Woods53a94a82014-06-24 15:20:36 -0400780void State::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
781{
782 mViewport.x = x;
783 mViewport.y = y;
784 mViewport.width = width;
785 mViewport.height = height;
Jamie Madill1b94d432015-08-07 13:23:23 -0400786 mDirtyBits.set(DIRTY_BIT_VIEWPORT);
Shannon Woods53a94a82014-06-24 15:20:36 -0400787}
788
789const Rectangle &State::getViewport() const
790{
791 return mViewport;
792}
793
794void State::setActiveSampler(unsigned int active)
795{
796 mActiveSampler = active;
797}
798
799unsigned int State::getActiveSampler() const
800{
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700801 return static_cast<unsigned int>(mActiveSampler);
Shannon Woods53a94a82014-06-24 15:20:36 -0400802}
803
Jamie Madill4928b7c2017-06-20 12:57:39 -0400804void State::setSamplerTexture(const Context *context, GLenum type, Texture *texture)
Shannon Woods53a94a82014-06-24 15:20:36 -0400805{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400806 mSamplerTextures[type][mActiveSampler].set(context, texture);
Jamie Madill81c2e252017-09-09 23:32:46 -0400807 mDirtyBits.set(DIRTY_BIT_TEXTURE_BINDINGS);
808 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
Shannon Woods53a94a82014-06-24 15:20:36 -0400809}
810
Jamie Madillc29968b2016-01-20 11:17:23 -0500811Texture *State::getTargetTexture(GLenum target) const
812{
813 return getSamplerTexture(static_cast<unsigned int>(mActiveSampler), target);
814}
815
Geoff Lang76b10c92014-09-05 16:28:14 -0400816Texture *State::getSamplerTexture(unsigned int sampler, GLenum type) const
Shannon Woods53a94a82014-06-24 15:20:36 -0400817{
Jamie Madill5864ac22015-01-12 14:43:07 -0500818 const auto it = mSamplerTextures.find(type);
819 ASSERT(it != mSamplerTextures.end());
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400820 ASSERT(sampler < it->second.size());
Jamie Madill5864ac22015-01-12 14:43:07 -0500821 return it->second[sampler].get();
Shannon Woods53a94a82014-06-24 15:20:36 -0400822}
823
Geoff Lang76b10c92014-09-05 16:28:14 -0400824GLuint State::getSamplerTextureId(unsigned int sampler, GLenum type) const
Shannon Woods53a94a82014-06-24 15:20:36 -0400825{
Jamie Madill5864ac22015-01-12 14:43:07 -0500826 const auto it = mSamplerTextures.find(type);
827 ASSERT(it != mSamplerTextures.end());
Jamie Madill3d3d2f22015-09-23 16:47:51 -0400828 ASSERT(sampler < it->second.size());
Jamie Madill5864ac22015-01-12 14:43:07 -0500829 return it->second[sampler].id();
Shannon Woods53a94a82014-06-24 15:20:36 -0400830}
831
Jamie Madilla02315b2017-02-23 14:14:47 -0500832void State::detachTexture(const Context *context, const TextureMap &zeroTextures, GLuint texture)
Shannon Woods53a94a82014-06-24 15:20:36 -0400833{
834 // Textures have a detach method on State rather than a simple
835 // removeBinding, because the zero/null texture objects are managed
836 // separately, and don't have to go through the Context's maps or
837 // the ResourceManager.
838
839 // [OpenGL ES 2.0.24] section 3.8 page 84:
840 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
841 // rebound to texture object zero
842
Corentin Walleza2257da2016-04-19 16:43:12 -0400843 for (auto &bindingVec : mSamplerTextures)
Shannon Woods53a94a82014-06-24 15:20:36 -0400844 {
Corentin Walleza2257da2016-04-19 16:43:12 -0400845 GLenum textureType = bindingVec.first;
846 TextureBindingVector &textureVector = bindingVec.second;
Jamie Madill81c2e252017-09-09 23:32:46 -0400847 for (BindingPointer<Texture> &binding : textureVector)
Shannon Woods53a94a82014-06-24 15:20:36 -0400848 {
Geoff Lang76b10c92014-09-05 16:28:14 -0400849 if (binding.id() == texture)
Shannon Woods53a94a82014-06-24 15:20:36 -0400850 {
Jamie Madill5864ac22015-01-12 14:43:07 -0500851 auto it = zeroTextures.find(textureType);
852 ASSERT(it != zeroTextures.end());
Jamie Madille6382c32014-11-07 15:05:26 -0500853 // Zero textures are the "default" textures instead of NULL
Jamie Madill4928b7c2017-06-20 12:57:39 -0400854 binding.set(context, it->second.get());
Jamie Madill81c2e252017-09-09 23:32:46 -0400855 mDirtyBits.set(DIRTY_BIT_TEXTURE_BINDINGS);
Shannon Woods53a94a82014-06-24 15:20:36 -0400856 }
857 }
858 }
859
Xinghua Cao65ec0b22017-03-28 16:10:52 +0800860 for (auto &bindingImageUnit : mImageUnits)
861 {
862 if (bindingImageUnit.texture.id() == texture)
863 {
864 bindingImageUnit.texture.set(context, nullptr);
865 bindingImageUnit.level = 0;
866 bindingImageUnit.layered = false;
867 bindingImageUnit.layer = 0;
868 bindingImageUnit.access = GL_READ_ONLY;
869 bindingImageUnit.format = GL_R32UI;
870 break;
871 }
872 }
873
Shannon Woods53a94a82014-06-24 15:20:36 -0400874 // [OpenGL ES 2.0.24] section 4.4 page 112:
875 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
876 // as if Texture2DAttachment had been called, with a texture of 0, for each attachment point to which this
877 // image was attached in the currently bound framebuffer.
878
Jamie Madill8693bdb2017-09-02 15:32:14 -0400879 if (mReadFramebuffer && mReadFramebuffer->detachTexture(context, texture))
Shannon Woods53a94a82014-06-24 15:20:36 -0400880 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400881 mDirtyObjects.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
Shannon Woods53a94a82014-06-24 15:20:36 -0400882 }
883
Jamie Madill8693bdb2017-09-02 15:32:14 -0400884 if (mDrawFramebuffer && mDrawFramebuffer->detachTexture(context, texture))
Shannon Woods53a94a82014-06-24 15:20:36 -0400885 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400886 mDirtyObjects.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
Shannon Woods53a94a82014-06-24 15:20:36 -0400887 }
888}
889
Jamie Madill4928b7c2017-06-20 12:57:39 -0400890void State::initializeZeroTextures(const Context *context, const TextureMap &zeroTextures)
Jamie Madille6382c32014-11-07 15:05:26 -0500891{
892 for (const auto &zeroTexture : zeroTextures)
893 {
894 auto &samplerTextureArray = mSamplerTextures[zeroTexture.first];
895
896 for (size_t textureUnit = 0; textureUnit < samplerTextureArray.size(); ++textureUnit)
897 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400898 samplerTextureArray[textureUnit].set(context, zeroTexture.second.get());
Jamie Madille6382c32014-11-07 15:05:26 -0500899 }
900 }
901}
902
Jamie Madill4928b7c2017-06-20 12:57:39 -0400903void State::setSamplerBinding(const Context *context, GLuint textureUnit, Sampler *sampler)
Shannon Woods53a94a82014-06-24 15:20:36 -0400904{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400905 mSamplers[textureUnit].set(context, sampler);
Jamie Madill81c2e252017-09-09 23:32:46 -0400906 mDirtyBits.set(DIRTY_BIT_SAMPLER_BINDINGS);
907 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
Shannon Woods53a94a82014-06-24 15:20:36 -0400908}
909
910GLuint State::getSamplerId(GLuint textureUnit) const
911{
Geoff Lang76b10c92014-09-05 16:28:14 -0400912 ASSERT(textureUnit < mSamplers.size());
Shannon Woods53a94a82014-06-24 15:20:36 -0400913 return mSamplers[textureUnit].id();
914}
915
916Sampler *State::getSampler(GLuint textureUnit) const
917{
918 return mSamplers[textureUnit].get();
919}
920
Jamie Madill4928b7c2017-06-20 12:57:39 -0400921void State::detachSampler(const Context *context, GLuint sampler)
Shannon Woods53a94a82014-06-24 15:20:36 -0400922{
923 // [OpenGL ES 3.0.2] section 3.8.2 pages 123-124:
924 // If a sampler object that is currently bound to one or more texture units is
925 // deleted, it is as though BindSampler is called once for each texture unit to
926 // which the sampler is bound, with unit set to the texture unit and sampler set to zero.
Jamie Madill81c2e252017-09-09 23:32:46 -0400927 for (BindingPointer<Sampler> &samplerBinding : mSamplers)
Shannon Woods53a94a82014-06-24 15:20:36 -0400928 {
Geoff Lang76b10c92014-09-05 16:28:14 -0400929 if (samplerBinding.id() == sampler)
Shannon Woods53a94a82014-06-24 15:20:36 -0400930 {
Jamie Madill4928b7c2017-06-20 12:57:39 -0400931 samplerBinding.set(context, nullptr);
Jamie Madill81c2e252017-09-09 23:32:46 -0400932 mDirtyBits.set(DIRTY_BIT_SAMPLER_BINDINGS);
Shannon Woods53a94a82014-06-24 15:20:36 -0400933 }
934 }
935}
936
Jamie Madill4928b7c2017-06-20 12:57:39 -0400937void State::setRenderbufferBinding(const Context *context, Renderbuffer *renderbuffer)
Shannon Woods53a94a82014-06-24 15:20:36 -0400938{
Jamie Madill4928b7c2017-06-20 12:57:39 -0400939 mRenderbuffer.set(context, renderbuffer);
Jamie Madill8693bdb2017-09-02 15:32:14 -0400940 mDirtyBits.set(DIRTY_BIT_RENDERBUFFER_BINDING);
Shannon Woods53a94a82014-06-24 15:20:36 -0400941}
942
943GLuint State::getRenderbufferId() const
944{
945 return mRenderbuffer.id();
946}
947
Jamie Madilldfde6ab2016-06-09 07:07:18 -0700948Renderbuffer *State::getCurrentRenderbuffer() const
Shannon Woods53a94a82014-06-24 15:20:36 -0400949{
950 return mRenderbuffer.get();
951}
952
Jamie Madilla02315b2017-02-23 14:14:47 -0500953void State::detachRenderbuffer(const Context *context, GLuint renderbuffer)
Shannon Woods53a94a82014-06-24 15:20:36 -0400954{
955 // [OpenGL ES 2.0.24] section 4.4 page 109:
956 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
957 // had been executed with the target RENDERBUFFER and name of zero.
958
959 if (mRenderbuffer.id() == renderbuffer)
960 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400961 setRenderbufferBinding(context, nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -0400962 }
963
964 // [OpenGL ES 2.0.24] section 4.4 page 111:
965 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
966 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
967 // point to which this image was attached in the currently bound framebuffer.
968
969 Framebuffer *readFramebuffer = mReadFramebuffer;
970 Framebuffer *drawFramebuffer = mDrawFramebuffer;
971
Jamie Madill8693bdb2017-09-02 15:32:14 -0400972 if (readFramebuffer && readFramebuffer->detachRenderbuffer(context, renderbuffer))
Shannon Woods53a94a82014-06-24 15:20:36 -0400973 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400974 mDirtyObjects.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
Shannon Woods53a94a82014-06-24 15:20:36 -0400975 }
976
977 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
978 {
Jamie Madill8693bdb2017-09-02 15:32:14 -0400979 if (drawFramebuffer->detachRenderbuffer(context, renderbuffer))
980 {
981 mDirtyObjects.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
982 }
Shannon Woods53a94a82014-06-24 15:20:36 -0400983 }
984
985}
986
987void State::setReadFramebufferBinding(Framebuffer *framebuffer)
988{
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500989 if (mReadFramebuffer == framebuffer)
990 return;
991
Shannon Woods53a94a82014-06-24 15:20:36 -0400992 mReadFramebuffer = framebuffer;
Jamie Madill60ec6ea2016-01-22 15:27:19 -0500993 mDirtyBits.set(DIRTY_BIT_READ_FRAMEBUFFER_BINDING);
994
995 if (mReadFramebuffer && mReadFramebuffer->hasAnyDirtyBit())
996 {
997 mDirtyObjects.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
998 }
Shannon Woods53a94a82014-06-24 15:20:36 -0400999}
1000
1001void State::setDrawFramebufferBinding(Framebuffer *framebuffer)
1002{
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001003 if (mDrawFramebuffer == framebuffer)
1004 return;
1005
Shannon Woods53a94a82014-06-24 15:20:36 -04001006 mDrawFramebuffer = framebuffer;
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001007 mDirtyBits.set(DIRTY_BIT_DRAW_FRAMEBUFFER_BINDING);
1008
1009 if (mDrawFramebuffer && mDrawFramebuffer->hasAnyDirtyBit())
1010 {
1011 mDirtyObjects.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
1012 }
Shannon Woods53a94a82014-06-24 15:20:36 -04001013}
1014
1015Framebuffer *State::getTargetFramebuffer(GLenum target) const
1016{
1017 switch (target)
1018 {
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001019 case GL_READ_FRAMEBUFFER_ANGLE:
1020 return mReadFramebuffer;
1021 case GL_DRAW_FRAMEBUFFER_ANGLE:
1022 case GL_FRAMEBUFFER:
1023 return mDrawFramebuffer;
1024 default:
1025 UNREACHABLE();
Yunchao Hef81ce4a2017-04-24 10:49:17 +08001026 return nullptr;
Shannon Woods53a94a82014-06-24 15:20:36 -04001027 }
1028}
1029
Jamie Madill51f40ec2016-06-15 14:06:00 -04001030Framebuffer *State::getReadFramebuffer() const
Shannon Woods53a94a82014-06-24 15:20:36 -04001031{
1032 return mReadFramebuffer;
1033}
1034
Jamie Madill51f40ec2016-06-15 14:06:00 -04001035Framebuffer *State::getDrawFramebuffer() const
Shannon Woods53a94a82014-06-24 15:20:36 -04001036{
1037 return mDrawFramebuffer;
1038}
1039
1040bool State::removeReadFramebufferBinding(GLuint framebuffer)
1041{
Jamie Madill77a72f62015-04-14 11:18:32 -04001042 if (mReadFramebuffer != nullptr &&
1043 mReadFramebuffer->id() == framebuffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001044 {
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001045 setReadFramebufferBinding(nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -04001046 return true;
1047 }
1048
1049 return false;
1050}
1051
1052bool State::removeDrawFramebufferBinding(GLuint framebuffer)
1053{
Jamie Madill77a72f62015-04-14 11:18:32 -04001054 if (mReadFramebuffer != nullptr &&
1055 mDrawFramebuffer->id() == framebuffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001056 {
Jamie Madill60ec6ea2016-01-22 15:27:19 -05001057 setDrawFramebufferBinding(nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -04001058 return true;
1059 }
1060
1061 return false;
1062}
1063
1064void State::setVertexArrayBinding(VertexArray *vertexArray)
1065{
1066 mVertexArray = vertexArray;
Jamie Madill0b9e9032015-08-17 11:51:52 +00001067 mDirtyBits.set(DIRTY_BIT_VERTEX_ARRAY_BINDING);
Jamie Madillc9d442d2016-01-20 11:17:24 -05001068
1069 if (mVertexArray && mVertexArray->hasAnyDirtyBit())
1070 {
1071 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1072 }
Shannon Woods53a94a82014-06-24 15:20:36 -04001073}
1074
1075GLuint State::getVertexArrayId() const
1076{
Yunchao He4f285442017-04-21 12:15:49 +08001077 ASSERT(mVertexArray != nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -04001078 return mVertexArray->id();
1079}
1080
1081VertexArray *State::getVertexArray() const
1082{
Yunchao He4f285442017-04-21 12:15:49 +08001083 ASSERT(mVertexArray != nullptr);
Shannon Woods53a94a82014-06-24 15:20:36 -04001084 return mVertexArray;
1085}
1086
1087bool State::removeVertexArrayBinding(GLuint vertexArray)
1088{
1089 if (mVertexArray->id() == vertexArray)
1090 {
Yunchao Hed7297bf2017-04-19 15:27:10 +08001091 mVertexArray = nullptr;
Jamie Madill0b9e9032015-08-17 11:51:52 +00001092 mDirtyBits.set(DIRTY_BIT_VERTEX_ARRAY_BINDING);
Jamie Madillc9d442d2016-01-20 11:17:24 -05001093 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
Shannon Woods53a94a82014-06-24 15:20:36 -04001094 return true;
1095 }
1096
1097 return false;
1098}
1099
Jamie Madill4928b7c2017-06-20 12:57:39 -04001100void State::setElementArrayBuffer(const Context *context, Buffer *buffer)
Shao80957d92017-02-20 21:25:59 +08001101{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001102 getVertexArray()->setElementArrayBuffer(context, buffer);
Shao80957d92017-02-20 21:25:59 +08001103 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1104}
1105
Jamie Madill4928b7c2017-06-20 12:57:39 -04001106void State::bindVertexBuffer(const Context *context,
1107 GLuint bindingIndex,
Shao80957d92017-02-20 21:25:59 +08001108 Buffer *boundBuffer,
1109 GLintptr offset,
1110 GLsizei stride)
1111{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001112 getVertexArray()->bindVertexBuffer(context, bindingIndex, boundBuffer, offset, stride);
Shao80957d92017-02-20 21:25:59 +08001113 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1114}
1115
Shaodde78e82017-05-22 14:13:27 +08001116void State::setVertexAttribBinding(const Context *context, GLuint attribIndex, GLuint bindingIndex)
Shao80957d92017-02-20 21:25:59 +08001117{
Shaodde78e82017-05-22 14:13:27 +08001118 getVertexArray()->setVertexAttribBinding(context, attribIndex, bindingIndex);
Shao80957d92017-02-20 21:25:59 +08001119 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1120}
1121
1122void State::setVertexAttribFormat(GLuint attribIndex,
1123 GLint size,
1124 GLenum type,
1125 bool normalized,
1126 bool pureInteger,
1127 GLuint relativeOffset)
1128{
1129 getVertexArray()->setVertexAttribFormat(attribIndex, size, type, normalized, pureInteger,
1130 relativeOffset);
1131 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1132}
1133
1134void State::setVertexBindingDivisor(GLuint bindingIndex, GLuint divisor)
1135{
1136 getVertexArray()->setVertexBindingDivisor(bindingIndex, divisor);
1137 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
1138}
1139
Jamie Madill6c1f6712017-02-14 19:08:04 -05001140void State::setProgram(const Context *context, Program *newProgram)
Shannon Woods53a94a82014-06-24 15:20:36 -04001141{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001142 if (mProgram != newProgram)
Shannon Woods53a94a82014-06-24 15:20:36 -04001143 {
Geoff Lang7dd2e102014-11-10 15:19:26 -05001144 if (mProgram)
1145 {
Jamie Madill6c1f6712017-02-14 19:08:04 -05001146 mProgram->release(context);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001147 }
1148
1149 mProgram = newProgram;
1150
1151 if (mProgram)
1152 {
1153 newProgram->addRef();
Jamie Madill81c2e252017-09-09 23:32:46 -04001154 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
Geoff Lang7dd2e102014-11-10 15:19:26 -05001155 }
Jamie Madilla779b612017-07-24 11:46:05 -04001156 mDirtyBits.set(DIRTY_BIT_PROGRAM_EXECUTABLE);
1157 mDirtyBits.set(DIRTY_BIT_PROGRAM_BINDING);
Shannon Woods53a94a82014-06-24 15:20:36 -04001158 }
1159}
1160
Geoff Lang7dd2e102014-11-10 15:19:26 -05001161Program *State::getProgram() const
Shannon Woods53a94a82014-06-24 15:20:36 -04001162{
Geoff Lang7dd2e102014-11-10 15:19:26 -05001163 return mProgram;
Shannon Woods53a94a82014-06-24 15:20:36 -04001164}
1165
Jamie Madill4928b7c2017-06-20 12:57:39 -04001166void State::setTransformFeedbackBinding(const Context *context,
1167 TransformFeedback *transformFeedback)
Shannon Woods53a94a82014-06-24 15:20:36 -04001168{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001169 mTransformFeedback.set(context, transformFeedback);
Shannon Woods53a94a82014-06-24 15:20:36 -04001170}
1171
1172TransformFeedback *State::getCurrentTransformFeedback() const
1173{
1174 return mTransformFeedback.get();
1175}
1176
Gregoire Payen de La Garanderie52742022015-02-04 14:55:39 +00001177bool State::isTransformFeedbackActiveUnpaused() const
1178{
Jamie Madill81c2e252017-09-09 23:32:46 -04001179 TransformFeedback *curTransformFeedback = getCurrentTransformFeedback();
Geoff Langbb0a0bb2015-03-27 12:16:57 -04001180 return curTransformFeedback && curTransformFeedback->isActive() && !curTransformFeedback->isPaused();
Gregoire Payen de La Garanderie52742022015-02-04 14:55:39 +00001181}
1182
Jamie Madill4928b7c2017-06-20 12:57:39 -04001183bool State::removeTransformFeedbackBinding(const Context *context, GLuint transformFeedback)
Shannon Woods53a94a82014-06-24 15:20:36 -04001184{
1185 if (mTransformFeedback.id() == transformFeedback)
1186 {
Jamie Madill4928b7c2017-06-20 12:57:39 -04001187 mTransformFeedback.set(context, nullptr);
Corentin Walleza2257da2016-04-19 16:43:12 -04001188 return true;
Shannon Woods53a94a82014-06-24 15:20:36 -04001189 }
Corentin Walleza2257da2016-04-19 16:43:12 -04001190
1191 return false;
Shannon Woods53a94a82014-06-24 15:20:36 -04001192}
1193
Yunchao Hea336b902017-08-02 16:05:21 +08001194void State::setProgramPipelineBinding(const Context *context, ProgramPipeline *pipeline)
1195{
1196 mProgramPipeline.set(context, pipeline);
1197}
1198
1199void State::detachProgramPipeline(const Context *context, GLuint pipeline)
1200{
1201 mProgramPipeline.set(context, nullptr);
1202}
1203
Olli Etuahobbf1c102016-06-28 13:31:33 +03001204bool State::isQueryActive(const GLenum type) const
Shannon Woods53a94a82014-06-24 15:20:36 -04001205{
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001206 for (auto &iter : mActiveQueries)
Shannon Woods53a94a82014-06-24 15:20:36 -04001207 {
Olli Etuahobbf1c102016-06-28 13:31:33 +03001208 const Query *query = iter.second.get();
1209 if (query != nullptr && ActiveQueryType(query->getType()) == ActiveQueryType(type))
Ian Ewell3ffd78b2016-01-22 16:09:42 -05001210 {
1211 return true;
1212 }
1213 }
1214
1215 return false;
1216}
1217
1218bool State::isQueryActive(Query *query) const
1219{
1220 for (auto &iter : mActiveQueries)
1221 {
1222 if (iter.second.get() == query)
Shannon Woods53a94a82014-06-24 15:20:36 -04001223 {
1224 return true;
1225 }
1226 }
1227
1228 return false;
1229}
1230
Jamie Madill4928b7c2017-06-20 12:57:39 -04001231void State::setActiveQuery(const Context *context, GLenum target, Query *query)
Shannon Woods53a94a82014-06-24 15:20:36 -04001232{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001233 mActiveQueries[target].set(context, query);
Shannon Woods53a94a82014-06-24 15:20:36 -04001234}
1235
1236GLuint State::getActiveQueryId(GLenum target) const
1237{
1238 const Query *query = getActiveQuery(target);
1239 return (query ? query->id() : 0u);
1240}
1241
1242Query *State::getActiveQuery(GLenum target) const
1243{
Jamie Madill5864ac22015-01-12 14:43:07 -05001244 const auto it = mActiveQueries.find(target);
Shannon Woods53a94a82014-06-24 15:20:36 -04001245
Jamie Madill5864ac22015-01-12 14:43:07 -05001246 // All query types should already exist in the activeQueries map
1247 ASSERT(it != mActiveQueries.end());
1248
1249 return it->second.get();
Shannon Woods53a94a82014-06-24 15:20:36 -04001250}
1251
Jamie Madill4928b7c2017-06-20 12:57:39 -04001252void State::setArrayBufferBinding(const Context *context, Buffer *buffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001253{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001254 mArrayBuffer.set(context, buffer);
Shannon Woods53a94a82014-06-24 15:20:36 -04001255}
1256
1257GLuint State::getArrayBufferId() const
1258{
1259 return mArrayBuffer.id();
1260}
1261
Jamie Madill4928b7c2017-06-20 12:57:39 -04001262void State::setDrawIndirectBufferBinding(const Context *context, Buffer *buffer)
Jiajia Qin9d7d0b12016-11-29 16:30:31 +08001263{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001264 mDrawIndirectBuffer.set(context, buffer);
Jiajia Qin9d7d0b12016-11-29 16:30:31 +08001265 mDirtyBits.set(DIRTY_BIT_DRAW_INDIRECT_BUFFER_BINDING);
1266}
1267
Jamie Madill4928b7c2017-06-20 12:57:39 -04001268void State::setGenericUniformBufferBinding(const Context *context, Buffer *buffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001269{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001270 mGenericUniformBuffer.set(context, buffer);
Shannon Woods53a94a82014-06-24 15:20:36 -04001271}
1272
Jamie Madill4928b7c2017-06-20 12:57:39 -04001273void State::setIndexedUniformBufferBinding(const Context *context,
1274 GLuint index,
1275 Buffer *buffer,
1276 GLintptr offset,
1277 GLsizeiptr size)
Shannon Woods53a94a82014-06-24 15:20:36 -04001278{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001279 mUniformBuffers[index].set(context, buffer, offset, size);
Shannon Woods53a94a82014-06-24 15:20:36 -04001280}
1281
Geoff Lang5d124a62015-09-15 13:03:27 -04001282const OffsetBindingPointer<Buffer> &State::getIndexedUniformBuffer(size_t index) const
Shannon Woods53a94a82014-06-24 15:20:36 -04001283{
Shannon Woodsf3acaf92014-09-23 18:07:11 -04001284 ASSERT(static_cast<size_t>(index) < mUniformBuffers.size());
Geoff Lang5d124a62015-09-15 13:03:27 -04001285 return mUniformBuffers[index];
Gregoire Payen de La Garanderie68694e92015-03-24 14:03:37 +00001286}
1287
Jamie Madill4928b7c2017-06-20 12:57:39 -04001288void State::setGenericAtomicCounterBufferBinding(const Context *context, Buffer *buffer)
Jiajia Qin6eafb042016-12-27 17:04:07 +08001289{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001290 mGenericAtomicCounterBuffer.set(context, buffer);
Jiajia Qin6eafb042016-12-27 17:04:07 +08001291}
1292
Jamie Madill4928b7c2017-06-20 12:57:39 -04001293void State::setIndexedAtomicCounterBufferBinding(const Context *context,
1294 GLuint index,
Jiajia Qin6eafb042016-12-27 17:04:07 +08001295 Buffer *buffer,
1296 GLintptr offset,
1297 GLsizeiptr size)
1298{
1299 ASSERT(static_cast<size_t>(index) < mAtomicCounterBuffers.size());
Jamie Madill4928b7c2017-06-20 12:57:39 -04001300 mAtomicCounterBuffers[index].set(context, buffer, offset, size);
Jiajia Qin6eafb042016-12-27 17:04:07 +08001301}
1302
1303const OffsetBindingPointer<Buffer> &State::getIndexedAtomicCounterBuffer(size_t index) const
1304{
1305 ASSERT(static_cast<size_t>(index) < mAtomicCounterBuffers.size());
1306 return mAtomicCounterBuffers[index];
1307}
1308
Jamie Madill4928b7c2017-06-20 12:57:39 -04001309void State::setGenericShaderStorageBufferBinding(const Context *context, Buffer *buffer)
Jiajia Qinf546e7d2017-03-27 14:12:59 +08001310{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001311 mGenericShaderStorageBuffer.set(context, buffer);
Jiajia Qinf546e7d2017-03-27 14:12:59 +08001312}
1313
Jamie Madill4928b7c2017-06-20 12:57:39 -04001314void State::setIndexedShaderStorageBufferBinding(const Context *context,
1315 GLuint index,
Jiajia Qinf546e7d2017-03-27 14:12:59 +08001316 Buffer *buffer,
1317 GLintptr offset,
1318 GLsizeiptr size)
1319{
1320 ASSERT(static_cast<size_t>(index) < mShaderStorageBuffers.size());
Jamie Madill4928b7c2017-06-20 12:57:39 -04001321 mShaderStorageBuffers[index].set(context, buffer, offset, size);
Jiajia Qinf546e7d2017-03-27 14:12:59 +08001322}
1323
1324const OffsetBindingPointer<Buffer> &State::getIndexedShaderStorageBuffer(size_t index) const
1325{
1326 ASSERT(static_cast<size_t>(index) < mShaderStorageBuffers.size());
1327 return mShaderStorageBuffers[index];
1328}
1329
Jamie Madill4928b7c2017-06-20 12:57:39 -04001330void State::setCopyReadBufferBinding(const Context *context, Buffer *buffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001331{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001332 mCopyReadBuffer.set(context, buffer);
Shannon Woods53a94a82014-06-24 15:20:36 -04001333}
1334
Jamie Madill4928b7c2017-06-20 12:57:39 -04001335void State::setCopyWriteBufferBinding(const Context *context, Buffer *buffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001336{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001337 mCopyWriteBuffer.set(context, buffer);
Shannon Woods53a94a82014-06-24 15:20:36 -04001338}
1339
Jamie Madill4928b7c2017-06-20 12:57:39 -04001340void State::setPixelPackBufferBinding(const Context *context, Buffer *buffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001341{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001342 mPack.pixelBuffer.set(context, buffer);
Corentin Wallezbbd663a2016-04-20 17:49:17 -04001343 mDirtyBits.set(DIRTY_BIT_PACK_BUFFER_BINDING);
Shannon Woods53a94a82014-06-24 15:20:36 -04001344}
1345
Jamie Madill4928b7c2017-06-20 12:57:39 -04001346void State::setPixelUnpackBufferBinding(const Context *context, Buffer *buffer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001347{
Jamie Madill4928b7c2017-06-20 12:57:39 -04001348 mUnpack.pixelBuffer.set(context, buffer);
Corentin Wallezbbd663a2016-04-20 17:49:17 -04001349 mDirtyBits.set(DIRTY_BIT_UNPACK_BUFFER_BINDING);
Shannon Woods53a94a82014-06-24 15:20:36 -04001350}
1351
1352Buffer *State::getTargetBuffer(GLenum target) const
1353{
1354 switch (target)
1355 {
1356 case GL_ARRAY_BUFFER: return mArrayBuffer.get();
1357 case GL_COPY_READ_BUFFER: return mCopyReadBuffer.get();
1358 case GL_COPY_WRITE_BUFFER: return mCopyWriteBuffer.get();
Jamie Madill8e344942015-07-09 14:22:07 -04001359 case GL_ELEMENT_ARRAY_BUFFER: return getVertexArray()->getElementArrayBuffer().get();
Shannon Woods53a94a82014-06-24 15:20:36 -04001360 case GL_PIXEL_PACK_BUFFER: return mPack.pixelBuffer.get();
1361 case GL_PIXEL_UNPACK_BUFFER: return mUnpack.pixelBuffer.get();
Geoff Lang045536b2015-03-27 15:17:18 -04001362 case GL_TRANSFORM_FEEDBACK_BUFFER: return mTransformFeedback->getGenericBuffer().get();
Shannon Woods53a94a82014-06-24 15:20:36 -04001363 case GL_UNIFORM_BUFFER: return mGenericUniformBuffer.get();
Geoff Langb5e997f2016-12-06 10:55:34 -05001364 case GL_ATOMIC_COUNTER_BUFFER:
Jiajia Qin6eafb042016-12-27 17:04:07 +08001365 return mGenericAtomicCounterBuffer.get();
Geoff Langb5e997f2016-12-06 10:55:34 -05001366 case GL_SHADER_STORAGE_BUFFER:
Jiajia Qinf546e7d2017-03-27 14:12:59 +08001367 return mGenericShaderStorageBuffer.get();
Geoff Langb5e997f2016-12-06 10:55:34 -05001368 case GL_DRAW_INDIRECT_BUFFER:
Jiajia Qin9d7d0b12016-11-29 16:30:31 +08001369 return mDrawIndirectBuffer.get();
Yunchao Hef81ce4a2017-04-24 10:49:17 +08001370 default:
1371 UNREACHABLE();
1372 return nullptr;
Shannon Woods53a94a82014-06-24 15:20:36 -04001373 }
1374}
1375
Jamie Madill4928b7c2017-06-20 12:57:39 -04001376void State::detachBuffer(const Context *context, GLuint bufferName)
Yuly Novikov5807a532015-12-03 13:01:22 -05001377{
Jiajia Qinf546e7d2017-03-27 14:12:59 +08001378 BindingPointer<Buffer> *buffers[] = {
1379 &mArrayBuffer, &mGenericAtomicCounterBuffer, &mCopyReadBuffer,
1380 &mCopyWriteBuffer, &mDrawIndirectBuffer, &mPack.pixelBuffer,
1381 &mUnpack.pixelBuffer, &mGenericUniformBuffer, &mGenericShaderStorageBuffer};
Yuly Novikov5807a532015-12-03 13:01:22 -05001382 for (auto buffer : buffers)
1383 {
1384 if (buffer->id() == bufferName)
1385 {
Jamie Madill4928b7c2017-06-20 12:57:39 -04001386 buffer->set(context, nullptr);
Yuly Novikov5807a532015-12-03 13:01:22 -05001387 }
1388 }
1389
1390 TransformFeedback *curTransformFeedback = getCurrentTransformFeedback();
1391 if (curTransformFeedback)
1392 {
Jamie Madill4928b7c2017-06-20 12:57:39 -04001393 curTransformFeedback->detachBuffer(context, bufferName);
Yuly Novikov5807a532015-12-03 13:01:22 -05001394 }
1395
Jamie Madill4928b7c2017-06-20 12:57:39 -04001396 getVertexArray()->detachBuffer(context, bufferName);
Yuly Novikov5807a532015-12-03 13:01:22 -05001397}
1398
Shannon Woods53a94a82014-06-24 15:20:36 -04001399void State::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
1400{
1401 getVertexArray()->enableAttribute(attribNum, enabled);
Jamie Madillc9d442d2016-01-20 11:17:24 -05001402 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
Shannon Woods53a94a82014-06-24 15:20:36 -04001403}
1404
1405void State::setVertexAttribf(GLuint index, const GLfloat values[4])
1406{
Shannon Woods23e05002014-09-22 19:07:27 -04001407 ASSERT(static_cast<size_t>(index) < mVertexAttribCurrentValues.size());
Shannon Woods53a94a82014-06-24 15:20:36 -04001408 mVertexAttribCurrentValues[index].setFloatValues(values);
Jamie Madill1e0bc3a2015-08-11 08:12:21 -04001409 mDirtyBits.set(DIRTY_BIT_CURRENT_VALUE_0 + index);
Shannon Woods53a94a82014-06-24 15:20:36 -04001410}
1411
1412void State::setVertexAttribu(GLuint index, const GLuint values[4])
1413{
Shannon Woods23e05002014-09-22 19:07:27 -04001414 ASSERT(static_cast<size_t>(index) < mVertexAttribCurrentValues.size());
Shannon Woods53a94a82014-06-24 15:20:36 -04001415 mVertexAttribCurrentValues[index].setUnsignedIntValues(values);
Jamie Madill1e0bc3a2015-08-11 08:12:21 -04001416 mDirtyBits.set(DIRTY_BIT_CURRENT_VALUE_0 + index);
Shannon Woods53a94a82014-06-24 15:20:36 -04001417}
1418
1419void State::setVertexAttribi(GLuint index, const GLint values[4])
1420{
Shannon Woods23e05002014-09-22 19:07:27 -04001421 ASSERT(static_cast<size_t>(index) < mVertexAttribCurrentValues.size());
Shannon Woods53a94a82014-06-24 15:20:36 -04001422 mVertexAttribCurrentValues[index].setIntValues(values);
Jamie Madill1e0bc3a2015-08-11 08:12:21 -04001423 mDirtyBits.set(DIRTY_BIT_CURRENT_VALUE_0 + index);
Shannon Woods53a94a82014-06-24 15:20:36 -04001424}
1425
Shaodde78e82017-05-22 14:13:27 +08001426void State::setVertexAttribPointer(const Context *context,
1427 unsigned int attribNum,
1428 Buffer *boundBuffer,
1429 GLint size,
1430 GLenum type,
1431 bool normalized,
1432 bool pureInteger,
1433 GLsizei stride,
1434 const void *pointer)
Shannon Woods53a94a82014-06-24 15:20:36 -04001435{
Shaodde78e82017-05-22 14:13:27 +08001436 getVertexArray()->setVertexAttribPointer(context, attribNum, boundBuffer, size, type,
1437 normalized, pureInteger, stride, pointer);
Jamie Madillc9d442d2016-01-20 11:17:24 -05001438 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
Jamie Madill0b9e9032015-08-17 11:51:52 +00001439}
1440
Shaodde78e82017-05-22 14:13:27 +08001441void State::setVertexAttribDivisor(const Context *context, GLuint index, GLuint divisor)
Jamie Madill0b9e9032015-08-17 11:51:52 +00001442{
Shaodde78e82017-05-22 14:13:27 +08001443 getVertexArray()->setVertexAttribDivisor(context, index, divisor);
Jamie Madillc9d442d2016-01-20 11:17:24 -05001444 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
Shannon Woods53a94a82014-06-24 15:20:36 -04001445}
1446
Jamie Madill6de51852017-04-12 09:53:01 -04001447const VertexAttribCurrentValueData &State::getVertexAttribCurrentValue(size_t attribNum) const
Shannon Woods53a94a82014-06-24 15:20:36 -04001448{
Jamie Madill6de51852017-04-12 09:53:01 -04001449 ASSERT(attribNum < mVertexAttribCurrentValues.size());
Shannon Woods53a94a82014-06-24 15:20:36 -04001450 return mVertexAttribCurrentValues[attribNum];
1451}
1452
Shannon Woods53a94a82014-06-24 15:20:36 -04001453const void *State::getVertexAttribPointer(unsigned int attribNum) const
1454{
1455 return getVertexArray()->getVertexAttribute(attribNum).pointer;
1456}
1457
1458void State::setPackAlignment(GLint alignment)
1459{
1460 mPack.alignment = alignment;
Jamie Madill1b94d432015-08-07 13:23:23 -04001461 mDirtyBits.set(DIRTY_BIT_PACK_ALIGNMENT);
Shannon Woods53a94a82014-06-24 15:20:36 -04001462}
1463
1464GLint State::getPackAlignment() const
1465{
1466 return mPack.alignment;
1467}
1468
1469void State::setPackReverseRowOrder(bool reverseRowOrder)
1470{
1471 mPack.reverseRowOrder = reverseRowOrder;
Jamie Madill1b94d432015-08-07 13:23:23 -04001472 mDirtyBits.set(DIRTY_BIT_PACK_REVERSE_ROW_ORDER);
Shannon Woods53a94a82014-06-24 15:20:36 -04001473}
1474
1475bool State::getPackReverseRowOrder() const
1476{
1477 return mPack.reverseRowOrder;
1478}
1479
Minmin Gongadff67b2015-10-14 10:34:45 -04001480void State::setPackRowLength(GLint rowLength)
1481{
1482 mPack.rowLength = rowLength;
1483 mDirtyBits.set(DIRTY_BIT_PACK_ROW_LENGTH);
1484}
1485
1486GLint State::getPackRowLength() const
1487{
1488 return mPack.rowLength;
1489}
1490
1491void State::setPackSkipRows(GLint skipRows)
1492{
1493 mPack.skipRows = skipRows;
1494 mDirtyBits.set(DIRTY_BIT_PACK_SKIP_ROWS);
1495}
1496
1497GLint State::getPackSkipRows() const
1498{
1499 return mPack.skipRows;
1500}
1501
1502void State::setPackSkipPixels(GLint skipPixels)
1503{
1504 mPack.skipPixels = skipPixels;
1505 mDirtyBits.set(DIRTY_BIT_PACK_SKIP_PIXELS);
1506}
1507
1508GLint State::getPackSkipPixels() const
1509{
1510 return mPack.skipPixels;
1511}
1512
Shannon Woods53a94a82014-06-24 15:20:36 -04001513const PixelPackState &State::getPackState() const
1514{
1515 return mPack;
1516}
1517
Jamie Madill87de3622015-03-16 10:41:44 -04001518PixelPackState &State::getPackState()
1519{
1520 return mPack;
1521}
1522
Shannon Woods53a94a82014-06-24 15:20:36 -04001523void State::setUnpackAlignment(GLint alignment)
1524{
1525 mUnpack.alignment = alignment;
Jamie Madill1b94d432015-08-07 13:23:23 -04001526 mDirtyBits.set(DIRTY_BIT_UNPACK_ALIGNMENT);
Shannon Woods53a94a82014-06-24 15:20:36 -04001527}
1528
1529GLint State::getUnpackAlignment() const
1530{
1531 return mUnpack.alignment;
1532}
1533
Minmin Gongb8aee3b2015-01-27 14:42:36 -08001534void State::setUnpackRowLength(GLint rowLength)
1535{
1536 mUnpack.rowLength = rowLength;
Jamie Madill1b94d432015-08-07 13:23:23 -04001537 mDirtyBits.set(DIRTY_BIT_UNPACK_ROW_LENGTH);
Minmin Gongb8aee3b2015-01-27 14:42:36 -08001538}
1539
1540GLint State::getUnpackRowLength() const
1541{
1542 return mUnpack.rowLength;
1543}
1544
Minmin Gongadff67b2015-10-14 10:34:45 -04001545void State::setUnpackImageHeight(GLint imageHeight)
1546{
1547 mUnpack.imageHeight = imageHeight;
1548 mDirtyBits.set(DIRTY_BIT_UNPACK_IMAGE_HEIGHT);
1549}
1550
1551GLint State::getUnpackImageHeight() const
1552{
1553 return mUnpack.imageHeight;
1554}
1555
1556void State::setUnpackSkipImages(GLint skipImages)
1557{
1558 mUnpack.skipImages = skipImages;
1559 mDirtyBits.set(DIRTY_BIT_UNPACK_SKIP_IMAGES);
1560}
1561
1562GLint State::getUnpackSkipImages() const
1563{
1564 return mUnpack.skipImages;
1565}
1566
1567void State::setUnpackSkipRows(GLint skipRows)
1568{
1569 mUnpack.skipRows = skipRows;
1570 mDirtyBits.set(DIRTY_BIT_UNPACK_SKIP_ROWS);
1571}
1572
1573GLint State::getUnpackSkipRows() const
1574{
1575 return mUnpack.skipRows;
1576}
1577
1578void State::setUnpackSkipPixels(GLint skipPixels)
1579{
1580 mUnpack.skipPixels = skipPixels;
1581 mDirtyBits.set(DIRTY_BIT_UNPACK_SKIP_PIXELS);
1582}
1583
1584GLint State::getUnpackSkipPixels() const
1585{
1586 return mUnpack.skipPixels;
1587}
1588
Shannon Woods53a94a82014-06-24 15:20:36 -04001589const PixelUnpackState &State::getUnpackState() const
1590{
1591 return mUnpack;
1592}
1593
Jamie Madill67102f02015-03-16 10:41:42 -04001594PixelUnpackState &State::getUnpackState()
1595{
1596 return mUnpack;
1597}
1598
Geoff Lang70d0f492015-12-10 17:45:46 -05001599const Debug &State::getDebug() const
1600{
1601 return mDebug;
1602}
1603
1604Debug &State::getDebug()
1605{
1606 return mDebug;
1607}
1608
Sami Väisänena797e062016-05-12 15:23:40 +03001609void State::setCoverageModulation(GLenum components)
1610{
1611 mCoverageModulation = components;
1612 mDirtyBits.set(DIRTY_BIT_COVERAGE_MODULATION);
1613}
1614
1615GLenum State::getCoverageModulation() const
1616{
1617 return mCoverageModulation;
1618}
1619
Sami Väisänene45e53b2016-05-25 10:36:04 +03001620void State::loadPathRenderingMatrix(GLenum matrixMode, const GLfloat *matrix)
1621{
1622 if (matrixMode == GL_PATH_MODELVIEW_CHROMIUM)
1623 {
1624 memcpy(mPathMatrixMV, matrix, 16 * sizeof(GLfloat));
1625 mDirtyBits.set(DIRTY_BIT_PATH_RENDERING_MATRIX_MV);
1626 }
1627 else if (matrixMode == GL_PATH_PROJECTION_CHROMIUM)
1628 {
1629 memcpy(mPathMatrixProj, matrix, 16 * sizeof(GLfloat));
1630 mDirtyBits.set(DIRTY_BIT_PATH_RENDERING_MATRIX_PROJ);
1631 }
1632 else
1633 {
1634 UNREACHABLE();
1635 }
1636}
1637
1638const GLfloat *State::getPathRenderingMatrix(GLenum which) const
1639{
1640 if (which == GL_PATH_MODELVIEW_MATRIX_CHROMIUM)
1641 {
1642 return mPathMatrixMV;
1643 }
1644 else if (which == GL_PATH_PROJECTION_MATRIX_CHROMIUM)
1645 {
1646 return mPathMatrixProj;
1647 }
1648
1649 UNREACHABLE();
1650 return nullptr;
1651}
1652
1653void State::setPathStencilFunc(GLenum func, GLint ref, GLuint mask)
1654{
1655 mPathStencilFunc = func;
1656 mPathStencilRef = ref;
1657 mPathStencilMask = mask;
1658 mDirtyBits.set(DIRTY_BIT_PATH_RENDERING_STENCIL_STATE);
1659}
1660
1661GLenum State::getPathStencilFunc() const
1662{
1663 return mPathStencilFunc;
1664}
1665
1666GLint State::getPathStencilRef() const
1667{
1668 return mPathStencilRef;
1669}
1670
1671GLuint State::getPathStencilMask() const
1672{
1673 return mPathStencilMask;
1674}
1675
Geoff Lang1d2c41d2016-10-19 16:14:46 -07001676void State::setFramebufferSRGB(bool sRGB)
1677{
1678 mFramebufferSRGB = sRGB;
1679 mDirtyBits.set(DIRTY_BIT_FRAMEBUFFER_SRGB);
1680}
1681
1682bool State::getFramebufferSRGB() const
1683{
1684 return mFramebufferSRGB;
1685}
1686
Shannon Woods53a94a82014-06-24 15:20:36 -04001687void State::getBooleanv(GLenum pname, GLboolean *params)
1688{
1689 switch (pname)
1690 {
1691 case GL_SAMPLE_COVERAGE_INVERT: *params = mSampleCoverageInvert; break;
1692 case GL_DEPTH_WRITEMASK: *params = mDepthStencil.depthMask; break;
1693 case GL_COLOR_WRITEMASK:
1694 params[0] = mBlend.colorMaskRed;
1695 params[1] = mBlend.colorMaskGreen;
1696 params[2] = mBlend.colorMaskBlue;
1697 params[3] = mBlend.colorMaskAlpha;
1698 break;
1699 case GL_CULL_FACE: *params = mRasterizer.cullFace; break;
1700 case GL_POLYGON_OFFSET_FILL: *params = mRasterizer.polygonOffsetFill; break;
1701 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mBlend.sampleAlphaToCoverage; break;
1702 case GL_SAMPLE_COVERAGE: *params = mSampleCoverage; break;
Jiawei Shaodb342272017-09-27 10:21:45 +08001703 case GL_SAMPLE_MASK:
1704 *params = mSampleMask;
1705 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001706 case GL_SCISSOR_TEST: *params = mScissorTest; break;
1707 case GL_STENCIL_TEST: *params = mDepthStencil.stencilTest; break;
1708 case GL_DEPTH_TEST: *params = mDepthStencil.depthTest; break;
1709 case GL_BLEND: *params = mBlend.blend; break;
1710 case GL_DITHER: *params = mBlend.dither; break;
Geoff Langbb0a0bb2015-03-27 12:16:57 -04001711 case GL_TRANSFORM_FEEDBACK_ACTIVE: *params = getCurrentTransformFeedback()->isActive() ? GL_TRUE : GL_FALSE; break;
1712 case GL_TRANSFORM_FEEDBACK_PAUSED: *params = getCurrentTransformFeedback()->isPaused() ? GL_TRUE : GL_FALSE; break;
Jamie Madille2cd53d2015-10-27 11:15:46 -04001713 case GL_PRIMITIVE_RESTART_FIXED_INDEX:
1714 *params = mPrimitiveRestart;
1715 break;
Geoff Langab831f02015-12-01 09:39:10 -05001716 case GL_RASTERIZER_DISCARD:
1717 *params = isRasterizerDiscardEnabled() ? GL_TRUE : GL_FALSE;
1718 break;
Geoff Lang70d0f492015-12-10 17:45:46 -05001719 case GL_DEBUG_OUTPUT_SYNCHRONOUS:
1720 *params = mDebug.isOutputSynchronous() ? GL_TRUE : GL_FALSE;
1721 break;
1722 case GL_DEBUG_OUTPUT:
1723 *params = mDebug.isOutputEnabled() ? GL_TRUE : GL_FALSE;
1724 break;
Sami Väisänen74c23472016-05-09 17:30:30 +03001725 case GL_MULTISAMPLE_EXT:
1726 *params = mMultiSampling;
1727 break;
1728 case GL_SAMPLE_ALPHA_TO_ONE_EXT:
1729 *params = mSampleAlphaToOne;
1730 break;
Geoff Langf41a7152016-09-19 15:11:17 -04001731 case GL_BIND_GENERATES_RESOURCE_CHROMIUM:
1732 *params = isBindGeneratesResourceEnabled() ? GL_TRUE : GL_FALSE;
1733 break;
Geoff Langfeb8c682017-02-13 16:07:35 -05001734 case GL_CLIENT_ARRAYS_ANGLE:
1735 *params = areClientArraysEnabled() ? GL_TRUE : GL_FALSE;
1736 break;
Geoff Lang1d2c41d2016-10-19 16:14:46 -07001737 case GL_FRAMEBUFFER_SRGB_EXT:
1738 *params = getFramebufferSRGB() ? GL_TRUE : GL_FALSE;
1739 break;
Jamie Madille08a1d32017-03-07 17:24:06 -05001740 case GL_CONTEXT_ROBUST_RESOURCE_INITIALIZATION_ANGLE:
1741 *params = mRobustResourceInit ? GL_TRUE : GL_FALSE;
1742 break;
Jamie Madillc43be722017-07-13 16:22:14 -04001743 case GL_PROGRAM_CACHE_ENABLED_ANGLE:
1744 *params = mProgramBinaryCacheEnabled ? GL_TRUE : GL_FALSE;
1745 break;
1746
Shannon Woods53a94a82014-06-24 15:20:36 -04001747 default:
1748 UNREACHABLE();
1749 break;
1750 }
1751}
1752
1753void State::getFloatv(GLenum pname, GLfloat *params)
1754{
1755 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1756 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1757 // GetIntegerv as its native query function. As it would require conversion in any
1758 // case, this should make no difference to the calling application.
1759 switch (pname)
1760 {
1761 case GL_LINE_WIDTH: *params = mLineWidth; break;
1762 case GL_SAMPLE_COVERAGE_VALUE: *params = mSampleCoverageValue; break;
1763 case GL_DEPTH_CLEAR_VALUE: *params = mDepthClearValue; break;
1764 case GL_POLYGON_OFFSET_FACTOR: *params = mRasterizer.polygonOffsetFactor; break;
1765 case GL_POLYGON_OFFSET_UNITS: *params = mRasterizer.polygonOffsetUnits; break;
1766 case GL_DEPTH_RANGE:
1767 params[0] = mNearZ;
1768 params[1] = mFarZ;
1769 break;
1770 case GL_COLOR_CLEAR_VALUE:
1771 params[0] = mColorClearValue.red;
1772 params[1] = mColorClearValue.green;
1773 params[2] = mColorClearValue.blue;
1774 params[3] = mColorClearValue.alpha;
1775 break;
1776 case GL_BLEND_COLOR:
1777 params[0] = mBlendColor.red;
1778 params[1] = mBlendColor.green;
1779 params[2] = mBlendColor.blue;
1780 params[3] = mBlendColor.alpha;
1781 break;
Sami Väisänen74c23472016-05-09 17:30:30 +03001782 case GL_MULTISAMPLE_EXT:
1783 *params = static_cast<GLfloat>(mMultiSampling);
1784 break;
1785 case GL_SAMPLE_ALPHA_TO_ONE_EXT:
1786 *params = static_cast<GLfloat>(mSampleAlphaToOne);
Sami Väisänena797e062016-05-12 15:23:40 +03001787 case GL_COVERAGE_MODULATION_CHROMIUM:
Jamie Madille2e406c2016-06-02 13:04:10 -04001788 params[0] = static_cast<GLfloat>(mCoverageModulation);
1789 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001790 default:
1791 UNREACHABLE();
1792 break;
1793 }
1794}
1795
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001796void State::getIntegerv(const Context *context, GLenum pname, GLint *params)
Shannon Woods53a94a82014-06-24 15:20:36 -04001797{
1798 if (pname >= GL_DRAW_BUFFER0_EXT && pname <= GL_DRAW_BUFFER15_EXT)
1799 {
1800 unsigned int colorAttachment = (pname - GL_DRAW_BUFFER0_EXT);
Shannon Woods2df6a602014-09-26 16:12:07 -04001801 ASSERT(colorAttachment < mMaxDrawBuffers);
Shannon Woods53a94a82014-06-24 15:20:36 -04001802 Framebuffer *framebuffer = mDrawFramebuffer;
1803 *params = framebuffer->getDrawBufferState(colorAttachment);
1804 return;
1805 }
1806
1807 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1808 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1809 // GetIntegerv as its native query function. As it would require conversion in any
1810 // case, this should make no difference to the calling application. You may find it in
1811 // State::getFloatv.
1812 switch (pname)
1813 {
1814 case GL_ARRAY_BUFFER_BINDING: *params = mArrayBuffer.id(); break;
Jiajia Qin9d7d0b12016-11-29 16:30:31 +08001815 case GL_DRAW_INDIRECT_BUFFER_BINDING:
1816 *params = mDrawIndirectBuffer.id();
1817 break;
Jamie Madill8e344942015-07-09 14:22:07 -04001818 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = getVertexArray()->getElementArrayBuffer().id(); break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001819 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1820 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mDrawFramebuffer->id(); break;
1821 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mReadFramebuffer->id(); break;
1822 case GL_RENDERBUFFER_BINDING: *params = mRenderbuffer.id(); break;
1823 case GL_VERTEX_ARRAY_BINDING: *params = mVertexArray->id(); break;
Geoff Lang7dd2e102014-11-10 15:19:26 -05001824 case GL_CURRENT_PROGRAM: *params = mProgram ? mProgram->id() : 0; break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001825 case GL_PACK_ALIGNMENT: *params = mPack.alignment; break;
1826 case GL_PACK_REVERSE_ROW_ORDER_ANGLE: *params = mPack.reverseRowOrder; break;
Minmin Gongadff67b2015-10-14 10:34:45 -04001827 case GL_PACK_ROW_LENGTH:
1828 *params = mPack.rowLength;
1829 break;
1830 case GL_PACK_SKIP_ROWS:
1831 *params = mPack.skipRows;
1832 break;
1833 case GL_PACK_SKIP_PIXELS:
1834 *params = mPack.skipPixels;
1835 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001836 case GL_UNPACK_ALIGNMENT: *params = mUnpack.alignment; break;
Minmin Gongb8aee3b2015-01-27 14:42:36 -08001837 case GL_UNPACK_ROW_LENGTH: *params = mUnpack.rowLength; break;
Minmin Gongadff67b2015-10-14 10:34:45 -04001838 case GL_UNPACK_IMAGE_HEIGHT:
1839 *params = mUnpack.imageHeight;
1840 break;
1841 case GL_UNPACK_SKIP_IMAGES:
1842 *params = mUnpack.skipImages;
1843 break;
1844 case GL_UNPACK_SKIP_ROWS:
1845 *params = mUnpack.skipRows;
1846 break;
1847 case GL_UNPACK_SKIP_PIXELS:
1848 *params = mUnpack.skipPixels;
1849 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001850 case GL_GENERATE_MIPMAP_HINT: *params = mGenerateMipmapHint; break;
1851 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mFragmentShaderDerivativeHint; break;
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001852 case GL_ACTIVE_TEXTURE:
1853 *params = (static_cast<GLint>(mActiveSampler) + GL_TEXTURE0);
1854 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001855 case GL_STENCIL_FUNC: *params = mDepthStencil.stencilFunc; break;
1856 case GL_STENCIL_REF: *params = mStencilRef; break;
1857 case GL_STENCIL_VALUE_MASK: *params = clampToInt(mDepthStencil.stencilMask); break;
1858 case GL_STENCIL_BACK_FUNC: *params = mDepthStencil.stencilBackFunc; break;
1859 case GL_STENCIL_BACK_REF: *params = mStencilBackRef; break;
1860 case GL_STENCIL_BACK_VALUE_MASK: *params = clampToInt(mDepthStencil.stencilBackMask); break;
1861 case GL_STENCIL_FAIL: *params = mDepthStencil.stencilFail; break;
1862 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mDepthStencil.stencilPassDepthFail; break;
1863 case GL_STENCIL_PASS_DEPTH_PASS: *params = mDepthStencil.stencilPassDepthPass; break;
1864 case GL_STENCIL_BACK_FAIL: *params = mDepthStencil.stencilBackFail; break;
1865 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mDepthStencil.stencilBackPassDepthFail; break;
1866 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mDepthStencil.stencilBackPassDepthPass; break;
1867 case GL_DEPTH_FUNC: *params = mDepthStencil.depthFunc; break;
1868 case GL_BLEND_SRC_RGB: *params = mBlend.sourceBlendRGB; break;
1869 case GL_BLEND_SRC_ALPHA: *params = mBlend.sourceBlendAlpha; break;
1870 case GL_BLEND_DST_RGB: *params = mBlend.destBlendRGB; break;
1871 case GL_BLEND_DST_ALPHA: *params = mBlend.destBlendAlpha; break;
1872 case GL_BLEND_EQUATION_RGB: *params = mBlend.blendEquationRGB; break;
1873 case GL_BLEND_EQUATION_ALPHA: *params = mBlend.blendEquationAlpha; break;
1874 case GL_STENCIL_WRITEMASK: *params = clampToInt(mDepthStencil.stencilWritemask); break;
1875 case GL_STENCIL_BACK_WRITEMASK: *params = clampToInt(mDepthStencil.stencilBackWritemask); break;
1876 case GL_STENCIL_CLEAR_VALUE: *params = mStencilClearValue; break;
Jamie Madill4928b7c2017-06-20 12:57:39 -04001877 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1878 *params = mReadFramebuffer->getImplementationColorReadType(context);
1879 break;
1880 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1881 *params = mReadFramebuffer->getImplementationColorReadFormat(context);
1882 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001883 case GL_SAMPLE_BUFFERS:
1884 case GL_SAMPLES:
1885 {
Jamie Madill81c2e252017-09-09 23:32:46 -04001886 Framebuffer *framebuffer = mDrawFramebuffer;
Jamie Madilldd43e6c2017-03-24 14:18:49 -04001887 if (framebuffer->checkStatus(context) == GL_FRAMEBUFFER_COMPLETE)
Shannon Woods53a94a82014-06-24 15:20:36 -04001888 {
1889 switch (pname)
1890 {
Jamie Madilla0016b72017-07-14 14:30:46 -04001891 case GL_SAMPLE_BUFFERS:
1892 if (framebuffer->getSamples(context) != 0)
1893 {
1894 *params = 1;
1895 }
1896 else
1897 {
1898 *params = 0;
1899 }
1900 break;
1901 case GL_SAMPLES:
1902 *params = framebuffer->getSamples(context);
1903 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001904 }
1905 }
1906 else
1907 {
1908 *params = 0;
1909 }
1910 }
1911 break;
1912 case GL_VIEWPORT:
1913 params[0] = mViewport.x;
1914 params[1] = mViewport.y;
1915 params[2] = mViewport.width;
1916 params[3] = mViewport.height;
1917 break;
1918 case GL_SCISSOR_BOX:
1919 params[0] = mScissor.x;
1920 params[1] = mScissor.y;
1921 params[2] = mScissor.width;
1922 params[3] = mScissor.height;
1923 break;
1924 case GL_CULL_FACE_MODE: *params = mRasterizer.cullMode; break;
1925 case GL_FRONT_FACE: *params = mRasterizer.frontFace; break;
1926 case GL_RED_BITS:
1927 case GL_GREEN_BITS:
1928 case GL_BLUE_BITS:
1929 case GL_ALPHA_BITS:
1930 {
Jamie Madill81c2e252017-09-09 23:32:46 -04001931 Framebuffer *framebuffer = getDrawFramebuffer();
1932 const FramebufferAttachment *colorbuffer = framebuffer->getFirstColorbuffer();
Shannon Woods53a94a82014-06-24 15:20:36 -04001933
1934 if (colorbuffer)
1935 {
1936 switch (pname)
1937 {
1938 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1939 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1940 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1941 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1942 }
1943 }
1944 else
1945 {
1946 *params = 0;
1947 }
1948 }
1949 break;
1950 case GL_DEPTH_BITS:
1951 {
Jamie Madill81c2e252017-09-09 23:32:46 -04001952 const Framebuffer *framebuffer = getDrawFramebuffer();
1953 const FramebufferAttachment *depthbuffer = framebuffer->getDepthbuffer();
Shannon Woods53a94a82014-06-24 15:20:36 -04001954
1955 if (depthbuffer)
1956 {
1957 *params = depthbuffer->getDepthSize();
1958 }
1959 else
1960 {
1961 *params = 0;
1962 }
1963 }
1964 break;
1965 case GL_STENCIL_BITS:
1966 {
Jamie Madill81c2e252017-09-09 23:32:46 -04001967 const Framebuffer *framebuffer = getDrawFramebuffer();
1968 const FramebufferAttachment *stencilbuffer = framebuffer->getStencilbuffer();
Shannon Woods53a94a82014-06-24 15:20:36 -04001969
1970 if (stencilbuffer)
1971 {
1972 *params = stencilbuffer->getStencilSize();
1973 }
1974 else
1975 {
1976 *params = 0;
1977 }
1978 }
1979 break;
1980 case GL_TEXTURE_BINDING_2D:
Shannon Woods2df6a602014-09-26 16:12:07 -04001981 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001982 *params = getSamplerTextureId(static_cast<unsigned int>(mActiveSampler), GL_TEXTURE_2D);
Shannon Woods53a94a82014-06-24 15:20:36 -04001983 break;
Corentin Wallez13c0dd42017-07-04 18:27:01 -04001984 case GL_TEXTURE_BINDING_RECTANGLE_ANGLE:
1985 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
1986 *params = getSamplerTextureId(static_cast<unsigned int>(mActiveSampler),
1987 GL_TEXTURE_RECTANGLE_ANGLE);
1988 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04001989 case GL_TEXTURE_BINDING_CUBE_MAP:
Shannon Woods2df6a602014-09-26 16:12:07 -04001990 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001991 *params =
1992 getSamplerTextureId(static_cast<unsigned int>(mActiveSampler), GL_TEXTURE_CUBE_MAP);
Shannon Woods53a94a82014-06-24 15:20:36 -04001993 break;
1994 case GL_TEXTURE_BINDING_3D:
Shannon Woods2df6a602014-09-26 16:12:07 -04001995 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07001996 *params = getSamplerTextureId(static_cast<unsigned int>(mActiveSampler), GL_TEXTURE_3D);
Shannon Woods53a94a82014-06-24 15:20:36 -04001997 break;
1998 case GL_TEXTURE_BINDING_2D_ARRAY:
Shannon Woods2df6a602014-09-26 16:12:07 -04001999 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
Cooper Partin4d61f7e2015-08-12 10:56:50 -07002000 *params =
2001 getSamplerTextureId(static_cast<unsigned int>(mActiveSampler), GL_TEXTURE_2D_ARRAY);
Shannon Woods53a94a82014-06-24 15:20:36 -04002002 break;
JiangYizhou24fe74c2017-07-06 16:56:50 +08002003 case GL_TEXTURE_BINDING_2D_MULTISAMPLE:
2004 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
2005 *params = getSamplerTextureId(static_cast<unsigned int>(mActiveSampler),
2006 GL_TEXTURE_2D_MULTISAMPLE);
2007 break;
John Bauman18319182016-09-28 14:22:27 -07002008 case GL_TEXTURE_BINDING_EXTERNAL_OES:
2009 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
2010 *params = getSamplerTextureId(static_cast<unsigned int>(mActiveSampler),
2011 GL_TEXTURE_EXTERNAL_OES);
2012 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002013 case GL_UNIFORM_BUFFER_BINDING:
2014 *params = mGenericUniformBuffer.id();
2015 break;
Frank Henigman22581ff2015-11-06 14:25:54 -05002016 case GL_TRANSFORM_FEEDBACK_BINDING:
Frank Henigmanb0f0b812015-11-21 17:49:29 -05002017 *params = mTransformFeedback.id();
Frank Henigman22581ff2015-11-06 14:25:54 -05002018 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002019 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING:
Geoff Lang045536b2015-03-27 15:17:18 -04002020 *params = mTransformFeedback->getGenericBuffer().id();
Shannon Woods53a94a82014-06-24 15:20:36 -04002021 break;
2022 case GL_COPY_READ_BUFFER_BINDING:
2023 *params = mCopyReadBuffer.id();
2024 break;
2025 case GL_COPY_WRITE_BUFFER_BINDING:
2026 *params = mCopyWriteBuffer.id();
2027 break;
2028 case GL_PIXEL_PACK_BUFFER_BINDING:
2029 *params = mPack.pixelBuffer.id();
2030 break;
2031 case GL_PIXEL_UNPACK_BUFFER_BINDING:
2032 *params = mUnpack.pixelBuffer.id();
2033 break;
Olli Etuaho86821db2016-03-04 12:05:47 +02002034 case GL_READ_BUFFER:
2035 *params = mReadFramebuffer->getReadBufferState();
2036 break;
2037 case GL_SAMPLER_BINDING:
2038 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
2039 *params = getSamplerId(static_cast<GLuint>(mActiveSampler));
2040 break;
Geoff Lang70d0f492015-12-10 17:45:46 -05002041 case GL_DEBUG_LOGGED_MESSAGES:
2042 *params = static_cast<GLint>(mDebug.getMessageCount());
2043 break;
2044 case GL_DEBUG_NEXT_LOGGED_MESSAGE_LENGTH:
2045 *params = static_cast<GLint>(mDebug.getNextMessageLength());
2046 break;
2047 case GL_DEBUG_GROUP_STACK_DEPTH:
2048 *params = static_cast<GLint>(mDebug.getGroupStackDepth());
2049 break;
Sami Väisänen74c23472016-05-09 17:30:30 +03002050 case GL_MULTISAMPLE_EXT:
2051 *params = static_cast<GLint>(mMultiSampling);
2052 break;
2053 case GL_SAMPLE_ALPHA_TO_ONE_EXT:
2054 *params = static_cast<GLint>(mSampleAlphaToOne);
Sami Väisänena797e062016-05-12 15:23:40 +03002055 case GL_COVERAGE_MODULATION_CHROMIUM:
2056 *params = static_cast<GLint>(mCoverageModulation);
Sami Väisänen74c23472016-05-09 17:30:30 +03002057 break;
Jiajia Qin6eafb042016-12-27 17:04:07 +08002058 case GL_ATOMIC_COUNTER_BUFFER_BINDING:
2059 *params = mGenericAtomicCounterBuffer.id();
2060 break;
Jiajia Qinf546e7d2017-03-27 14:12:59 +08002061 case GL_SHADER_STORAGE_BUFFER_BINDING:
2062 *params = mGenericShaderStorageBuffer.id();
2063 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002064 default:
2065 UNREACHABLE();
2066 break;
2067 }
2068}
2069
Geoff Lang70d0f492015-12-10 17:45:46 -05002070void State::getPointerv(GLenum pname, void **params) const
2071{
2072 switch (pname)
2073 {
2074 case GL_DEBUG_CALLBACK_FUNCTION:
2075 *params = reinterpret_cast<void *>(mDebug.getCallback());
2076 break;
2077 case GL_DEBUG_CALLBACK_USER_PARAM:
2078 *params = const_cast<void *>(mDebug.getUserParam());
2079 break;
2080 default:
2081 UNREACHABLE();
2082 break;
2083 }
2084}
2085
Martin Radev66fb8202016-07-28 11:45:20 +03002086void State::getIntegeri_v(GLenum target, GLuint index, GLint *data)
Shannon Woods53a94a82014-06-24 15:20:36 -04002087{
2088 switch (target)
2089 {
2090 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002091 ASSERT(static_cast<size_t>(index) < mTransformFeedback->getIndexedBufferCount());
2092 *data = mTransformFeedback->getIndexedBuffer(index).id();
2093 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002094 case GL_UNIFORM_BUFFER_BINDING:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002095 ASSERT(static_cast<size_t>(index) < mUniformBuffers.size());
2096 *data = mUniformBuffers[index].id();
2097 break;
2098 case GL_ATOMIC_COUNTER_BUFFER_BINDING:
2099 ASSERT(static_cast<size_t>(index) < mAtomicCounterBuffers.size());
2100 *data = mAtomicCounterBuffers[index].id();
2101 break;
Jiajia Qinf546e7d2017-03-27 14:12:59 +08002102 case GL_SHADER_STORAGE_BUFFER_BINDING:
2103 ASSERT(static_cast<size_t>(index) < mShaderStorageBuffers.size());
2104 *data = mShaderStorageBuffers[index].id();
2105 break;
Shao80957d92017-02-20 21:25:59 +08002106 case GL_VERTEX_BINDING_BUFFER:
2107 ASSERT(static_cast<size_t>(index) < mVertexArray->getMaxBindings());
Martin Radevdd5f27e2017-06-07 10:17:09 +03002108 *data = mVertexArray->getVertexBinding(index).getBuffer().id();
Shao80957d92017-02-20 21:25:59 +08002109 break;
2110 case GL_VERTEX_BINDING_DIVISOR:
2111 ASSERT(static_cast<size_t>(index) < mVertexArray->getMaxBindings());
Martin Radevdd5f27e2017-06-07 10:17:09 +03002112 *data = mVertexArray->getVertexBinding(index).getDivisor();
Shao80957d92017-02-20 21:25:59 +08002113 break;
2114 case GL_VERTEX_BINDING_OFFSET:
2115 ASSERT(static_cast<size_t>(index) < mVertexArray->getMaxBindings());
Martin Radevdd5f27e2017-06-07 10:17:09 +03002116 *data = static_cast<GLuint>(mVertexArray->getVertexBinding(index).getOffset());
Shao80957d92017-02-20 21:25:59 +08002117 break;
2118 case GL_VERTEX_BINDING_STRIDE:
2119 ASSERT(static_cast<size_t>(index) < mVertexArray->getMaxBindings());
Martin Radevdd5f27e2017-06-07 10:17:09 +03002120 *data = mVertexArray->getVertexBinding(index).getStride();
Shao80957d92017-02-20 21:25:59 +08002121 break;
Jiawei Shaodb342272017-09-27 10:21:45 +08002122 case GL_SAMPLE_MASK_VALUE:
2123 ASSERT(static_cast<size_t>(index) < mSampleMaskValues.size());
2124 *data = mSampleMaskValues[index];
2125 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002126 default:
Martin Radev66fb8202016-07-28 11:45:20 +03002127 UNREACHABLE();
2128 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002129 }
Shannon Woods53a94a82014-06-24 15:20:36 -04002130}
2131
Martin Radev66fb8202016-07-28 11:45:20 +03002132void State::getInteger64i_v(GLenum target, GLuint index, GLint64 *data)
Shannon Woods53a94a82014-06-24 15:20:36 -04002133{
2134 switch (target)
2135 {
2136 case GL_TRANSFORM_FEEDBACK_BUFFER_START:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002137 ASSERT(static_cast<size_t>(index) < mTransformFeedback->getIndexedBufferCount());
2138 *data = mTransformFeedback->getIndexedBuffer(index).getOffset();
2139 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002140 case GL_TRANSFORM_FEEDBACK_BUFFER_SIZE:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002141 ASSERT(static_cast<size_t>(index) < mTransformFeedback->getIndexedBufferCount());
2142 *data = mTransformFeedback->getIndexedBuffer(index).getSize();
2143 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002144 case GL_UNIFORM_BUFFER_START:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002145 ASSERT(static_cast<size_t>(index) < mUniformBuffers.size());
2146 *data = mUniformBuffers[index].getOffset();
2147 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002148 case GL_UNIFORM_BUFFER_SIZE:
Jiajia Qin6eafb042016-12-27 17:04:07 +08002149 ASSERT(static_cast<size_t>(index) < mUniformBuffers.size());
2150 *data = mUniformBuffers[index].getSize();
2151 break;
2152 case GL_ATOMIC_COUNTER_BUFFER_START:
2153 ASSERT(static_cast<size_t>(index) < mAtomicCounterBuffers.size());
2154 *data = mAtomicCounterBuffers[index].getOffset();
2155 break;
2156 case GL_ATOMIC_COUNTER_BUFFER_SIZE:
2157 ASSERT(static_cast<size_t>(index) < mAtomicCounterBuffers.size());
2158 *data = mAtomicCounterBuffers[index].getSize();
2159 break;
Jiajia Qinf546e7d2017-03-27 14:12:59 +08002160 case GL_SHADER_STORAGE_BUFFER_START:
2161 ASSERT(static_cast<size_t>(index) < mShaderStorageBuffers.size());
2162 *data = mShaderStorageBuffers[index].getOffset();
2163 break;
2164 case GL_SHADER_STORAGE_BUFFER_SIZE:
2165 ASSERT(static_cast<size_t>(index) < mShaderStorageBuffers.size());
2166 *data = mShaderStorageBuffers[index].getSize();
2167 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002168 default:
Martin Radev66fb8202016-07-28 11:45:20 +03002169 UNREACHABLE();
2170 break;
Shannon Woods53a94a82014-06-24 15:20:36 -04002171 }
Martin Radev66fb8202016-07-28 11:45:20 +03002172}
Shannon Woods53a94a82014-06-24 15:20:36 -04002173
Martin Radev66fb8202016-07-28 11:45:20 +03002174void State::getBooleani_v(GLenum target, GLuint index, GLboolean *data)
2175{
2176 UNREACHABLE();
Shannon Woods53a94a82014-06-24 15:20:36 -04002177}
2178
Jamie Madilld9ba4f72014-08-04 10:47:59 -04002179bool State::hasMappedBuffer(GLenum target) const
2180{
2181 if (target == GL_ARRAY_BUFFER)
2182 {
Jiawei-Shao2597fb62016-12-09 16:38:02 +08002183 const VertexArray *vao = getVertexArray();
Jamie Madilleea3a6e2015-04-15 10:02:48 -04002184 const auto &vertexAttribs = vao->getVertexAttributes();
Jiawei-Shao2597fb62016-12-09 16:38:02 +08002185 const auto &vertexBindings = vao->getVertexBindings();
Jamie Madill8e344942015-07-09 14:22:07 -04002186 size_t maxEnabledAttrib = vao->getMaxEnabledAttribute();
Jamie Madillaebf9dd2015-04-28 12:39:07 -04002187 for (size_t attribIndex = 0; attribIndex < maxEnabledAttrib; attribIndex++)
Jamie Madilld9ba4f72014-08-04 10:47:59 -04002188 {
Jamie Madill81c2e252017-09-09 23:32:46 -04002189 const VertexAttribute &vertexAttrib = vertexAttribs[attribIndex];
Martin Radevdd5f27e2017-06-07 10:17:09 +03002190 auto *boundBuffer = vertexBindings[vertexAttrib.bindingIndex].getBuffer().get();
Jamie Madilld9ba4f72014-08-04 10:47:59 -04002191 if (vertexAttrib.enabled && boundBuffer && boundBuffer->isMapped())
2192 {
2193 return true;
2194 }
2195 }
2196
2197 return false;
2198 }
2199 else
2200 {
2201 Buffer *buffer = getTargetBuffer(target);
2202 return (buffer && buffer->isMapped());
2203 }
2204}
2205
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002206void State::syncDirtyObjects(const Context *context)
Jamie Madillc9d442d2016-01-20 11:17:24 -05002207{
2208 if (!mDirtyObjects.any())
2209 return;
2210
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002211 syncDirtyObjects(context, mDirtyObjects);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002212}
2213
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002214void State::syncDirtyObjects(const Context *context, const DirtyObjects &bitset)
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002215{
Jamie Madill6de51852017-04-12 09:53:01 -04002216 for (auto dirtyObject : bitset)
Jamie Madillc9d442d2016-01-20 11:17:24 -05002217 {
2218 switch (dirtyObject)
2219 {
2220 case DIRTY_OBJECT_READ_FRAMEBUFFER:
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002221 ASSERT(mReadFramebuffer);
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002222 mReadFramebuffer->syncState(context);
Jamie Madillc9d442d2016-01-20 11:17:24 -05002223 break;
2224 case DIRTY_OBJECT_DRAW_FRAMEBUFFER:
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002225 ASSERT(mDrawFramebuffer);
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002226 mDrawFramebuffer->syncState(context);
Jamie Madillc9d442d2016-01-20 11:17:24 -05002227 break;
2228 case DIRTY_OBJECT_VERTEX_ARRAY:
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002229 ASSERT(mVertexArray);
Jamie Madill06ef36b2017-09-09 23:32:46 -04002230 mVertexArray->syncState(context);
Jamie Madillc9d442d2016-01-20 11:17:24 -05002231 break;
Jamie Madill81c2e252017-09-09 23:32:46 -04002232 case DIRTY_OBJECT_PROGRAM_TEXTURES:
2233 syncProgramTextures(context);
2234 break;
2235
Jamie Madillc9d442d2016-01-20 11:17:24 -05002236 default:
2237 UNREACHABLE();
2238 break;
2239 }
2240 }
2241
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002242 mDirtyObjects &= ~bitset;
2243}
2244
Jamie Madill81c2e252017-09-09 23:32:46 -04002245void State::syncProgramTextures(const Context *context)
2246{
Jamie Madill81c2e252017-09-09 23:32:46 -04002247 // TODO(jmadill): Fine-grained updates.
2248 if (!mProgram)
2249 {
2250 return;
2251 }
2252
2253 ASSERT(mDirtyObjects[DIRTY_OBJECT_PROGRAM_TEXTURES]);
2254 mDirtyBits.set(DIRTY_BIT_TEXTURE_BINDINGS);
2255
Jamie Madill0f80ed82017-09-19 00:24:56 -04002256 ActiveTextureMask newActiveTextures;
2257
Jamie Madill81c2e252017-09-09 23:32:46 -04002258 for (const SamplerBinding &samplerBinding : mProgram->getSamplerBindings())
2259 {
2260 if (samplerBinding.unreferenced)
2261 continue;
2262
2263 GLenum textureType = samplerBinding.textureType;
2264 for (GLuint textureUnitIndex : samplerBinding.boundTextureUnits)
2265 {
2266 Texture *texture = getSamplerTexture(textureUnitIndex, textureType);
Jamie Madill06ef36b2017-09-09 23:32:46 -04002267 Sampler *sampler = getSampler(textureUnitIndex);
Jamie Madill0f80ed82017-09-19 00:24:56 -04002268 ASSERT(static_cast<size_t>(textureUnitIndex) < mCompleteTextureCache.size());
2269 ASSERT(static_cast<size_t>(textureUnitIndex) < newActiveTextures.size());
Jamie Madill81c2e252017-09-09 23:32:46 -04002270
2271 if (texture != nullptr)
2272 {
Jamie Madill81c2e252017-09-09 23:32:46 -04002273 // Mark the texture binding bit as dirty if the texture completeness changes.
2274 // TODO(jmadill): Use specific dirty bit for completeness change.
2275 if (texture->isSamplerComplete(context, sampler))
2276 {
Jamie Madill06ef36b2017-09-09 23:32:46 -04002277 texture->syncState();
Jamie Madill81c2e252017-09-09 23:32:46 -04002278 mCompleteTextureCache[textureUnitIndex] = texture;
2279 }
Jamie Madill0f80ed82017-09-19 00:24:56 -04002280 else
2281 {
2282 mCompleteTextureCache[textureUnitIndex] = nullptr;
2283 }
Jamie Madill81c2e252017-09-09 23:32:46 -04002284
2285 // Bind the texture unconditionally, to recieve completeness change notifications.
2286 mCompleteTextureBindings[textureUnitIndex].bind(texture->getDirtyChannel());
Jamie Madill0f80ed82017-09-19 00:24:56 -04002287 newActiveTextures.set(textureUnitIndex);
2288 mCompleteTexturesMask.set(textureUnitIndex);
Jamie Madill81c2e252017-09-09 23:32:46 -04002289 }
2290
Jamie Madill06ef36b2017-09-09 23:32:46 -04002291 if (sampler != nullptr)
2292 {
2293 sampler->syncState(context);
2294 }
Jamie Madill81c2e252017-09-09 23:32:46 -04002295 }
2296 }
Jamie Madill0f80ed82017-09-19 00:24:56 -04002297
2298 // Unset now missing textures.
2299 ActiveTextureMask negativeMask = mCompleteTexturesMask & ~newActiveTextures;
2300 if (negativeMask.any())
2301 {
2302 for (auto textureIndex : negativeMask)
2303 {
2304 mCompleteTextureBindings[textureIndex].reset();
2305 mCompleteTextureCache[textureIndex] = nullptr;
2306 mCompleteTexturesMask.reset(textureIndex);
2307 }
2308 }
Jamie Madill81c2e252017-09-09 23:32:46 -04002309}
2310
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002311void State::syncDirtyObject(const Context *context, GLenum target)
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002312{
2313 DirtyObjects localSet;
2314
2315 switch (target)
2316 {
2317 case GL_READ_FRAMEBUFFER:
2318 localSet.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
2319 break;
2320 case GL_DRAW_FRAMEBUFFER:
2321 localSet.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
2322 break;
2323 case GL_FRAMEBUFFER:
2324 localSet.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
2325 localSet.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
2326 break;
2327 case GL_VERTEX_ARRAY:
2328 localSet.set(DIRTY_OBJECT_VERTEX_ARRAY);
2329 break;
Jamie Madill81c2e252017-09-09 23:32:46 -04002330 case GL_TEXTURE:
2331 case GL_SAMPLER:
2332 case GL_PROGRAM:
2333 localSet.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
2334 break;
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002335 }
2336
Jamie Madilldd43e6c2017-03-24 14:18:49 -04002337 syncDirtyObjects(context, localSet);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002338}
2339
2340void State::setObjectDirty(GLenum target)
2341{
2342 switch (target)
2343 {
2344 case GL_READ_FRAMEBUFFER:
2345 mDirtyObjects.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
2346 break;
2347 case GL_DRAW_FRAMEBUFFER:
2348 mDirtyObjects.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
2349 break;
2350 case GL_FRAMEBUFFER:
2351 mDirtyObjects.set(DIRTY_OBJECT_READ_FRAMEBUFFER);
2352 mDirtyObjects.set(DIRTY_OBJECT_DRAW_FRAMEBUFFER);
2353 break;
2354 case GL_VERTEX_ARRAY:
2355 mDirtyObjects.set(DIRTY_OBJECT_VERTEX_ARRAY);
2356 break;
Jamie Madill81c2e252017-09-09 23:32:46 -04002357 case GL_TEXTURE:
2358 case GL_SAMPLER:
Jamie Madill81c2e252017-09-09 23:32:46 -04002359 case GL_PROGRAM:
2360 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
2361 mDirtyBits.set(DIRTY_BIT_TEXTURE_BINDINGS);
2362 break;
Jamie Madilla779b612017-07-24 11:46:05 -04002363 }
2364}
2365
2366void State::onProgramExecutableChange(Program *program)
2367{
2368 // OpenGL Spec:
2369 // "If LinkProgram or ProgramBinary successfully re-links a program object
2370 // that was already in use as a result of a previous call to UseProgram, then the
2371 // generated executable code will be installed as part of the current rendering state."
2372 if (program->isLinked() && mProgram == program)
2373 {
2374 mDirtyBits.set(DIRTY_BIT_PROGRAM_EXECUTABLE);
Jamie Madill81c2e252017-09-09 23:32:46 -04002375 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
Jamie Madill60ec6ea2016-01-22 15:27:19 -05002376 }
Shannon Woods53a94a82014-06-24 15:20:36 -04002377}
Jamie Madillc9d442d2016-01-20 11:17:24 -05002378
Xinghua Cao65ec0b22017-03-28 16:10:52 +08002379void State::setImageUnit(const Context *context,
2380 GLuint unit,
2381 Texture *texture,
2382 GLint level,
2383 GLboolean layered,
2384 GLint layer,
2385 GLenum access,
2386 GLenum format)
2387{
2388 mImageUnits[unit].texture.set(context, texture);
2389 mImageUnits[unit].level = level;
2390 mImageUnits[unit].layered = layered;
2391 mImageUnits[unit].layer = layer;
2392 mImageUnits[unit].access = access;
2393 mImageUnits[unit].format = format;
2394}
2395
2396const ImageUnit &State::getImageUnit(GLuint unit) const
2397{
2398 return mImageUnits[unit];
2399}
2400
Jamie Madill81c2e252017-09-09 23:32:46 -04002401// Handle a dirty texture event.
2402void State::signal(uint32_t textureIndex)
2403{
2404 // Conservatively assume all textures are dirty.
2405 // TODO(jmadill): More fine-grained update.
2406 mDirtyObjects.set(DIRTY_OBJECT_PROGRAM_TEXTURES);
2407}
2408
Jamie Madillc9d442d2016-01-20 11:17:24 -05002409} // namespace gl