blob: 71f32e7d01f89e400f8317b04dbf355228c13251 [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
9#include "libGLESv2/State.h"
10
11#include "libGLESv2/Context.h"
Geoff Lang76b10c92014-09-05 16:28:14 -040012#include "libGLESv2/Caps.h"
Shannon Woods53a94a82014-06-24 15:20:36 -040013#include "libGLESv2/VertexArray.h"
14#include "libGLESv2/Query.h"
15#include "libGLESv2/Framebuffer.h"
16#include "libGLESv2/FramebufferAttachment.h"
17#include "libGLESv2/renderer/RenderTarget.h"
18#include "libGLESv2/formatutils.h"
19
20namespace gl
21{
Geoff Lang76b10c92014-09-05 16:28:14 -040022
Shannon Woods53a94a82014-06-24 15:20:36 -040023State::State()
24{
Shannon Woods2df6a602014-09-26 16:12:07 -040025 mMaxDrawBuffers = 0;
26 mMaxCombinedTextureImageUnits = 0;
Geoff Lang76b10c92014-09-05 16:28:14 -040027}
28
29State::~State()
30{
31 reset();
32}
33
34void State::initialize(const Caps& caps, GLuint clientVersion)
35{
Shannon Woods2df6a602014-09-26 16:12:07 -040036 mMaxDrawBuffers = caps.maxDrawBuffers;
37 mMaxCombinedTextureImageUnits = caps.maxCombinedTextureImageUnits;
Shannon Woods53a94a82014-06-24 15:20:36 -040038
39 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
40
41 mDepthClearValue = 1.0f;
42 mStencilClearValue = 0;
43
44 mRasterizer.rasterizerDiscard = false;
45 mRasterizer.cullFace = false;
46 mRasterizer.cullMode = GL_BACK;
47 mRasterizer.frontFace = GL_CCW;
48 mRasterizer.polygonOffsetFill = false;
49 mRasterizer.polygonOffsetFactor = 0.0f;
50 mRasterizer.polygonOffsetUnits = 0.0f;
51 mRasterizer.pointDrawMode = false;
52 mRasterizer.multiSample = false;
53 mScissorTest = false;
54 mScissor.x = 0;
55 mScissor.y = 0;
56 mScissor.width = 0;
57 mScissor.height = 0;
58
59 mBlend.blend = false;
60 mBlend.sourceBlendRGB = GL_ONE;
61 mBlend.sourceBlendAlpha = GL_ONE;
62 mBlend.destBlendRGB = GL_ZERO;
63 mBlend.destBlendAlpha = GL_ZERO;
64 mBlend.blendEquationRGB = GL_FUNC_ADD;
65 mBlend.blendEquationAlpha = GL_FUNC_ADD;
66 mBlend.sampleAlphaToCoverage = false;
67 mBlend.dither = true;
68
69 mBlendColor.red = 0;
70 mBlendColor.green = 0;
71 mBlendColor.blue = 0;
72 mBlendColor.alpha = 0;
73
74 mDepthStencil.depthTest = false;
75 mDepthStencil.depthFunc = GL_LESS;
76 mDepthStencil.depthMask = true;
77 mDepthStencil.stencilTest = false;
78 mDepthStencil.stencilFunc = GL_ALWAYS;
79 mDepthStencil.stencilMask = -1;
80 mDepthStencil.stencilWritemask = -1;
81 mDepthStencil.stencilBackFunc = GL_ALWAYS;
Geoff Lang76b10c92014-09-05 16:28:14 -040082 mDepthStencil.stencilBackMask = -1;
Shannon Woods53a94a82014-06-24 15:20:36 -040083 mDepthStencil.stencilBackWritemask = -1;
84 mDepthStencil.stencilFail = GL_KEEP;
85 mDepthStencil.stencilPassDepthFail = GL_KEEP;
86 mDepthStencil.stencilPassDepthPass = GL_KEEP;
87 mDepthStencil.stencilBackFail = GL_KEEP;
88 mDepthStencil.stencilBackPassDepthFail = GL_KEEP;
89 mDepthStencil.stencilBackPassDepthPass = GL_KEEP;
90
91 mStencilRef = 0;
92 mStencilBackRef = 0;
93
94 mSampleCoverage = false;
95 mSampleCoverageValue = 1.0f;
96 mSampleCoverageInvert = false;
97 mGenerateMipmapHint = GL_DONT_CARE;
98 mFragmentShaderDerivativeHint = GL_DONT_CARE;
99
100 mLineWidth = 1.0f;
101
102 mViewport.x = 0;
103 mViewport.y = 0;
104 mViewport.width = 0;
105 mViewport.height = 0;
106 mNearZ = 0.0f;
107 mFarZ = 1.0f;
108
109 mBlend.colorMaskRed = true;
110 mBlend.colorMaskGreen = true;
111 mBlend.colorMaskBlue = true;
112 mBlend.colorMaskAlpha = true;
113
Geoff Lang76b10c92014-09-05 16:28:14 -0400114 mActiveSampler = 0;
115
Shannon Woods53a94a82014-06-24 15:20:36 -0400116 const GLfloat defaultFloatValues[] = { 0.0f, 0.0f, 0.0f, 1.0f };
Shannon Woods23e05002014-09-22 19:07:27 -0400117 mVertexAttribCurrentValues.resize(caps.maxVertexAttributes);
118 for (size_t attribIndex = 0; attribIndex < mVertexAttribCurrentValues.size(); ++attribIndex)
Shannon Woods53a94a82014-06-24 15:20:36 -0400119 {
120 mVertexAttribCurrentValues[attribIndex].setFloatValues(defaultFloatValues);
121 }
122
Shannon Woodsf3acaf92014-09-23 18:07:11 -0400123 mUniformBuffers.resize(caps.maxCombinedUniformBlocks);
Shannon Woods8299bb02014-09-26 18:55:43 -0400124 mTransformFeedbackBuffers.resize(caps.maxTransformFeedbackSeparateAttributes);
Shannon Woodsf3acaf92014-09-23 18:07:11 -0400125
Geoff Lang76b10c92014-09-05 16:28:14 -0400126 mSamplerTextures[GL_TEXTURE_2D].resize(caps.maxCombinedTextureImageUnits);
127 mSamplerTextures[GL_TEXTURE_CUBE_MAP].resize(caps.maxCombinedTextureImageUnits);
128 if (clientVersion >= 3)
Shannon Woods53a94a82014-06-24 15:20:36 -0400129 {
Geoff Lang76b10c92014-09-05 16:28:14 -0400130 // TODO: These could also be enabled via extension
131 mSamplerTextures[GL_TEXTURE_2D_ARRAY].resize(caps.maxCombinedTextureImageUnits);
132 mSamplerTextures[GL_TEXTURE_3D].resize(caps.maxCombinedTextureImageUnits);
Shannon Woods53a94a82014-06-24 15:20:36 -0400133 }
134
Geoff Lang76b10c92014-09-05 16:28:14 -0400135 mSamplers.resize(caps.maxCombinedTextureImageUnits);
Shannon Woods53a94a82014-06-24 15:20:36 -0400136
137 mActiveQueries[GL_ANY_SAMPLES_PASSED].set(NULL);
138 mActiveQueries[GL_ANY_SAMPLES_PASSED_CONSERVATIVE].set(NULL);
139 mActiveQueries[GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN].set(NULL);
140
141 mCurrentProgramId = 0;
142 mCurrentProgramBinary.set(NULL);
143
144 mReadFramebuffer = NULL;
145 mDrawFramebuffer = NULL;
146}
147
Geoff Lang76b10c92014-09-05 16:28:14 -0400148void State::reset()
Shannon Woods53a94a82014-06-24 15:20:36 -0400149{
Geoff Lang76b10c92014-09-05 16:28:14 -0400150 for (TextureBindingMap::iterator bindingVec = mSamplerTextures.begin(); bindingVec != mSamplerTextures.end(); bindingVec++)
Shannon Woods53a94a82014-06-24 15:20:36 -0400151 {
Geoff Lang76b10c92014-09-05 16:28:14 -0400152 TextureBindingVector &textureVector = bindingVec->second;
153 for (size_t textureIdx = 0; textureIdx < textureVector.size(); textureIdx++)
Shannon Woods53a94a82014-06-24 15:20:36 -0400154 {
Geoff Lang76b10c92014-09-05 16:28:14 -0400155 textureVector[textureIdx].set(NULL);
Shannon Woods53a94a82014-06-24 15:20:36 -0400156 }
157 }
Geoff Lang76b10c92014-09-05 16:28:14 -0400158 for (size_t samplerIdx = 0; samplerIdx < mSamplers.size(); samplerIdx++)
159 {
160 mSamplers[samplerIdx].set(NULL);
161 }
Shannon Woods53a94a82014-06-24 15:20:36 -0400162
Shannon Woods53a94a82014-06-24 15:20:36 -0400163 mArrayBuffer.set(NULL);
164 mRenderbuffer.set(NULL);
165
166 mTransformFeedback.set(NULL);
167
168 for (State::ActiveQueryMap::iterator i = mActiveQueries.begin(); i != mActiveQueries.end(); i++)
169 {
170 i->second.set(NULL);
171 }
172
173 mGenericUniformBuffer.set(NULL);
Shannon Woodsf3acaf92014-09-23 18:07:11 -0400174 mGenericTransformFeedbackBuffer.set(NULL);
Shannon Woods8299bb02014-09-26 18:55:43 -0400175 for (BufferVector::iterator bufItr = mUniformBuffers.begin(); bufItr != mUniformBuffers.end(); ++bufItr)
Shannon Woods53a94a82014-06-24 15:20:36 -0400176 {
Shannon Woodsf3acaf92014-09-23 18:07:11 -0400177 bufItr->set(NULL);
Shannon Woods53a94a82014-06-24 15:20:36 -0400178 }
179
Shannon Woods8299bb02014-09-26 18:55:43 -0400180 for (BufferVector::iterator bufItr = mTransformFeedbackBuffers.begin(); bufItr != mTransformFeedbackBuffers.end(); ++bufItr)
Shannon Woods53a94a82014-06-24 15:20:36 -0400181 {
Shannon Woods8299bb02014-09-26 18:55:43 -0400182 bufItr->set(NULL);
Shannon Woods53a94a82014-06-24 15:20:36 -0400183 }
184
185 mCopyReadBuffer.set(NULL);
186 mCopyWriteBuffer.set(NULL);
187
188 mPack.pixelBuffer.set(NULL);
189 mUnpack.pixelBuffer.set(NULL);
190}
191
192const RasterizerState &State::getRasterizerState() const
193{
194 return mRasterizer;
195}
196
197const BlendState &State::getBlendState() const
198{
199 return mBlend;
200}
201
202const DepthStencilState &State::getDepthStencilState() const
203{
204 return mDepthStencil;
205}
206
207void State::setClearColor(float red, float green, float blue, float alpha)
208{
209 mColorClearValue.red = red;
210 mColorClearValue.green = green;
211 mColorClearValue.blue = blue;
212 mColorClearValue.alpha = alpha;
213}
214
215void State::setClearDepth(float depth)
216{
217 mDepthClearValue = depth;
218}
219
220void State::setClearStencil(int stencil)
221{
222 mStencilClearValue = stencil;
223}
224
225ClearParameters State::getClearParameters(GLbitfield mask) const
226{
227 ClearParameters clearParams = { 0 };
228 for (unsigned int i = 0; i < ArraySize(clearParams.clearColor); i++)
229 {
230 clearParams.clearColor[i] = false;
231 }
232 clearParams.colorFClearValue = mColorClearValue;
233 clearParams.colorClearType = GL_FLOAT;
234 clearParams.colorMaskRed = mBlend.colorMaskRed;
235 clearParams.colorMaskGreen = mBlend.colorMaskGreen;
236 clearParams.colorMaskBlue = mBlend.colorMaskBlue;
237 clearParams.colorMaskAlpha = mBlend.colorMaskAlpha;
238 clearParams.clearDepth = false;
239 clearParams.depthClearValue = mDepthClearValue;
240 clearParams.clearStencil = false;
241 clearParams.stencilClearValue = mStencilClearValue;
242 clearParams.stencilWriteMask = mDepthStencil.stencilWritemask;
243 clearParams.scissorEnabled = mScissorTest;
244 clearParams.scissor = mScissor;
245
246 const Framebuffer *framebufferObject = getDrawFramebuffer();
247 if (mask & GL_COLOR_BUFFER_BIT)
248 {
249 if (framebufferObject->hasEnabledColorAttachment())
250 {
251 for (unsigned int i = 0; i < ArraySize(clearParams.clearColor); i++)
252 {
253 clearParams.clearColor[i] = true;
254 }
255 }
256 }
257
258 if (mask & GL_DEPTH_BUFFER_BIT)
259 {
260 if (mDepthStencil.depthMask && framebufferObject->getDepthbuffer() != NULL)
261 {
262 clearParams.clearDepth = true;
263 }
264 }
265
266 if (mask & GL_STENCIL_BUFFER_BIT)
267 {
268 if (framebufferObject->getStencilbuffer() != NULL)
269 {
Jamie Madilldd8488d2014-09-03 15:07:21 -0400270 GLenum stencilActualFormat = framebufferObject->getStencilbuffer()->getActualFormat();
271 if (GetInternalFormatInfo(stencilActualFormat).stencilBits > 0)
Shannon Woods53a94a82014-06-24 15:20:36 -0400272 {
273 clearParams.clearStencil = true;
274 }
275 }
276 }
277
278 return clearParams;
279}
280
281void State::setColorMask(bool red, bool green, bool blue, bool alpha)
282{
283 mBlend.colorMaskRed = red;
284 mBlend.colorMaskGreen = green;
285 mBlend.colorMaskBlue = blue;
286 mBlend.colorMaskAlpha = alpha;
287}
288
289void State::setDepthMask(bool mask)
290{
291 mDepthStencil.depthMask = mask;
292}
293
294bool State::isRasterizerDiscardEnabled() const
295{
296 return mRasterizer.rasterizerDiscard;
297}
298
299void State::setRasterizerDiscard(bool enabled)
300{
301 mRasterizer.rasterizerDiscard = enabled;
302}
303
304bool State::isCullFaceEnabled() const
305{
306 return mRasterizer.cullFace;
307}
308
309void State::setCullFace(bool enabled)
310{
311 mRasterizer.cullFace = enabled;
312}
313
314void State::setCullMode(GLenum mode)
315{
316 mRasterizer.cullMode = mode;
317}
318
319void State::setFrontFace(GLenum front)
320{
321 mRasterizer.frontFace = front;
322}
323
324bool State::isDepthTestEnabled() const
325{
326 return mDepthStencil.depthTest;
327}
328
329void State::setDepthTest(bool enabled)
330{
331 mDepthStencil.depthTest = enabled;
332}
333
334void State::setDepthFunc(GLenum depthFunc)
335{
336 mDepthStencil.depthFunc = depthFunc;
337}
338
339void State::setDepthRange(float zNear, float zFar)
340{
341 mNearZ = zNear;
342 mFarZ = zFar;
343}
344
345void State::getDepthRange(float *zNear, float *zFar) const
346{
347 *zNear = mNearZ;
348 *zFar = mFarZ;
349}
350
351bool State::isBlendEnabled() const
352{
353 return mBlend.blend;
354}
355
356void State::setBlend(bool enabled)
357{
358 mBlend.blend = enabled;
359}
360
361void State::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
362{
363 mBlend.sourceBlendRGB = sourceRGB;
364 mBlend.destBlendRGB = destRGB;
365 mBlend.sourceBlendAlpha = sourceAlpha;
366 mBlend.destBlendAlpha = destAlpha;
367}
368
369void State::setBlendColor(float red, float green, float blue, float alpha)
370{
371 mBlendColor.red = red;
372 mBlendColor.green = green;
373 mBlendColor.blue = blue;
374 mBlendColor.alpha = alpha;
375}
376
377void State::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
378{
379 mBlend.blendEquationRGB = rgbEquation;
380 mBlend.blendEquationAlpha = alphaEquation;
381}
382
383const ColorF &State::getBlendColor() const
384{
385 return mBlendColor;
386}
387
388bool State::isStencilTestEnabled() const
389{
390 return mDepthStencil.stencilTest;
391}
392
393void State::setStencilTest(bool enabled)
394{
395 mDepthStencil.stencilTest = enabled;
396}
397
398void State::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
399{
400 mDepthStencil.stencilFunc = stencilFunc;
401 mStencilRef = (stencilRef > 0) ? stencilRef : 0;
402 mDepthStencil.stencilMask = stencilMask;
403}
404
405void State::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
406{
407 mDepthStencil.stencilBackFunc = stencilBackFunc;
408 mStencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
409 mDepthStencil.stencilBackMask = stencilBackMask;
410}
411
412void State::setStencilWritemask(GLuint stencilWritemask)
413{
414 mDepthStencil.stencilWritemask = stencilWritemask;
415}
416
417void State::setStencilBackWritemask(GLuint stencilBackWritemask)
418{
419 mDepthStencil.stencilBackWritemask = stencilBackWritemask;
420}
421
422void State::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
423{
424 mDepthStencil.stencilFail = stencilFail;
425 mDepthStencil.stencilPassDepthFail = stencilPassDepthFail;
426 mDepthStencil.stencilPassDepthPass = stencilPassDepthPass;
427}
428
429void State::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
430{
431 mDepthStencil.stencilBackFail = stencilBackFail;
432 mDepthStencil.stencilBackPassDepthFail = stencilBackPassDepthFail;
433 mDepthStencil.stencilBackPassDepthPass = stencilBackPassDepthPass;
434}
435
436GLint State::getStencilRef() const
437{
438 return mStencilRef;
439}
440
441GLint State::getStencilBackRef() const
442{
443 return mStencilBackRef;
444}
445
446bool State::isPolygonOffsetFillEnabled() const
447{
448 return mRasterizer.polygonOffsetFill;
449}
450
451void State::setPolygonOffsetFill(bool enabled)
452{
453 mRasterizer.polygonOffsetFill = enabled;
454}
455
456void State::setPolygonOffsetParams(GLfloat factor, GLfloat units)
457{
458 // An application can pass NaN values here, so handle this gracefully
459 mRasterizer.polygonOffsetFactor = factor != factor ? 0.0f : factor;
460 mRasterizer.polygonOffsetUnits = units != units ? 0.0f : units;
461}
462
463bool State::isSampleAlphaToCoverageEnabled() const
464{
465 return mBlend.sampleAlphaToCoverage;
466}
467
468void State::setSampleAlphaToCoverage(bool enabled)
469{
470 mBlend.sampleAlphaToCoverage = enabled;
471}
472
473bool State::isSampleCoverageEnabled() const
474{
475 return mSampleCoverage;
476}
477
478void State::setSampleCoverage(bool enabled)
479{
480 mSampleCoverage = enabled;
481}
482
483void State::setSampleCoverageParams(GLclampf value, bool invert)
484{
485 mSampleCoverageValue = value;
486 mSampleCoverageInvert = invert;
487}
488
489void State::getSampleCoverageParams(GLclampf *value, bool *invert)
490{
491 ASSERT(value != NULL && invert != NULL);
492
493 *value = mSampleCoverageValue;
494 *invert = mSampleCoverageInvert;
495}
496
497bool State::isScissorTestEnabled() const
498{
499 return mScissorTest;
500}
501
502void State::setScissorTest(bool enabled)
503{
504 mScissorTest = enabled;
505}
506
507void State::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
508{
509 mScissor.x = x;
510 mScissor.y = y;
511 mScissor.width = width;
512 mScissor.height = height;
513}
514
515const Rectangle &State::getScissor() const
516{
517 return mScissor;
518}
519
520bool State::isDitherEnabled() const
521{
522 return mBlend.dither;
523}
524
525void State::setDither(bool enabled)
526{
527 mBlend.dither = enabled;
528}
529
530void State::setEnableFeature(GLenum feature, bool enabled)
531{
532 switch (feature)
533 {
534 case GL_CULL_FACE: setCullFace(enabled); break;
535 case GL_POLYGON_OFFSET_FILL: setPolygonOffsetFill(enabled); break;
536 case GL_SAMPLE_ALPHA_TO_COVERAGE: setSampleAlphaToCoverage(enabled); break;
537 case GL_SAMPLE_COVERAGE: setSampleCoverage(enabled); break;
538 case GL_SCISSOR_TEST: setScissorTest(enabled); break;
539 case GL_STENCIL_TEST: setStencilTest(enabled); break;
540 case GL_DEPTH_TEST: setDepthTest(enabled); break;
541 case GL_BLEND: setBlend(enabled); break;
542 case GL_DITHER: setDither(enabled); break;
543 case GL_PRIMITIVE_RESTART_FIXED_INDEX: UNIMPLEMENTED(); break;
544 case GL_RASTERIZER_DISCARD: setRasterizerDiscard(enabled); break;
545 default: UNREACHABLE();
546 }
547}
548
549bool State::getEnableFeature(GLenum feature)
550{
551 switch (feature)
552 {
553 case GL_CULL_FACE: return isCullFaceEnabled();
554 case GL_POLYGON_OFFSET_FILL: return isPolygonOffsetFillEnabled();
555 case GL_SAMPLE_ALPHA_TO_COVERAGE: return isSampleAlphaToCoverageEnabled();
556 case GL_SAMPLE_COVERAGE: return isSampleCoverageEnabled();
557 case GL_SCISSOR_TEST: return isScissorTestEnabled();
558 case GL_STENCIL_TEST: return isStencilTestEnabled();
559 case GL_DEPTH_TEST: return isDepthTestEnabled();
560 case GL_BLEND: return isBlendEnabled();
561 case GL_DITHER: return isDitherEnabled();
562 case GL_PRIMITIVE_RESTART_FIXED_INDEX: UNIMPLEMENTED(); return false;
563 case GL_RASTERIZER_DISCARD: return isRasterizerDiscardEnabled();
564 default: UNREACHABLE(); return false;
565 }
566}
567
568void State::setLineWidth(GLfloat width)
569{
570 mLineWidth = width;
571}
572
573void State::setGenerateMipmapHint(GLenum hint)
574{
575 mGenerateMipmapHint = hint;
576}
577
578void State::setFragmentShaderDerivativeHint(GLenum hint)
579{
580 mFragmentShaderDerivativeHint = hint;
581 // TODO: Propagate the hint to shader translator so we can write
582 // ddx, ddx_coarse, or ddx_fine depending on the hint.
583 // Ignore for now. It is valid for implementations to ignore hint.
584}
585
586void State::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
587{
588 mViewport.x = x;
589 mViewport.y = y;
590 mViewport.width = width;
591 mViewport.height = height;
592}
593
594const Rectangle &State::getViewport() const
595{
596 return mViewport;
597}
598
599void State::setActiveSampler(unsigned int active)
600{
601 mActiveSampler = active;
602}
603
604unsigned int State::getActiveSampler() const
605{
606 return mActiveSampler;
607}
608
Geoff Lang76b10c92014-09-05 16:28:14 -0400609void State::setSamplerTexture(GLenum type, Texture *texture)
Shannon Woods53a94a82014-06-24 15:20:36 -0400610{
Geoff Lang76b10c92014-09-05 16:28:14 -0400611 mSamplerTextures[type][mActiveSampler].set(texture);
Shannon Woods53a94a82014-06-24 15:20:36 -0400612}
613
Geoff Lang76b10c92014-09-05 16:28:14 -0400614Texture *State::getSamplerTexture(unsigned int sampler, GLenum type) const
Shannon Woods53a94a82014-06-24 15:20:36 -0400615{
Geoff Lang76b10c92014-09-05 16:28:14 -0400616 const BindingPointer<Texture>& binding = mSamplerTextures.at(type)[sampler];
Shannon Woods53a94a82014-06-24 15:20:36 -0400617
Geoff Lang76b10c92014-09-05 16:28:14 -0400618 if (binding.id() == 0) // Special case: 0 refers to default textures held by Context
Shannon Woods53a94a82014-06-24 15:20:36 -0400619 {
620 return NULL;
621 }
622
Geoff Lang76b10c92014-09-05 16:28:14 -0400623 return binding.get();
Shannon Woods53a94a82014-06-24 15:20:36 -0400624}
625
Geoff Lang76b10c92014-09-05 16:28:14 -0400626GLuint State::getSamplerTextureId(unsigned int sampler, GLenum type) const
Shannon Woods53a94a82014-06-24 15:20:36 -0400627{
Geoff Lang76b10c92014-09-05 16:28:14 -0400628 return mSamplerTextures.at(type)[sampler].id();
Shannon Woods53a94a82014-06-24 15:20:36 -0400629}
630
631void State::detachTexture(GLuint texture)
632{
633 // Textures have a detach method on State rather than a simple
634 // removeBinding, because the zero/null texture objects are managed
635 // separately, and don't have to go through the Context's maps or
636 // the ResourceManager.
637
638 // [OpenGL ES 2.0.24] section 3.8 page 84:
639 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
640 // rebound to texture object zero
641
Geoff Lang76b10c92014-09-05 16:28:14 -0400642 for (TextureBindingMap::iterator bindingVec = mSamplerTextures.begin(); bindingVec != mSamplerTextures.end(); bindingVec++)
Shannon Woods53a94a82014-06-24 15:20:36 -0400643 {
Geoff Lang76b10c92014-09-05 16:28:14 -0400644 TextureBindingVector &textureVector = bindingVec->second;
645 for (size_t textureIdx = 0; textureIdx < textureVector.size(); textureIdx++)
Shannon Woods53a94a82014-06-24 15:20:36 -0400646 {
Geoff Lang76b10c92014-09-05 16:28:14 -0400647 BindingPointer<Texture> &binding = textureVector[textureIdx];
648 if (binding.id() == texture)
Shannon Woods53a94a82014-06-24 15:20:36 -0400649 {
Geoff Lang76b10c92014-09-05 16:28:14 -0400650 binding.set(NULL);
Shannon Woods53a94a82014-06-24 15:20:36 -0400651 }
652 }
653 }
654
655 // [OpenGL ES 2.0.24] section 4.4 page 112:
656 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
657 // as if Texture2DAttachment had been called, with a texture of 0, for each attachment point to which this
658 // image was attached in the currently bound framebuffer.
659
660 if (mReadFramebuffer)
661 {
662 mReadFramebuffer->detachTexture(texture);
663 }
664
665 if (mDrawFramebuffer)
666 {
667 mDrawFramebuffer->detachTexture(texture);
668 }
669}
670
671void State::setSamplerBinding(GLuint textureUnit, Sampler *sampler)
672{
673 mSamplers[textureUnit].set(sampler);
674}
675
676GLuint State::getSamplerId(GLuint textureUnit) const
677{
Geoff Lang76b10c92014-09-05 16:28:14 -0400678 ASSERT(textureUnit < mSamplers.size());
Shannon Woods53a94a82014-06-24 15:20:36 -0400679 return mSamplers[textureUnit].id();
680}
681
682Sampler *State::getSampler(GLuint textureUnit) const
683{
684 return mSamplers[textureUnit].get();
685}
686
687void State::detachSampler(GLuint sampler)
688{
689 // [OpenGL ES 3.0.2] section 3.8.2 pages 123-124:
690 // If a sampler object that is currently bound to one or more texture units is
691 // deleted, it is as though BindSampler is called once for each texture unit to
692 // which the sampler is bound, with unit set to the texture unit and sampler set to zero.
Geoff Lang76b10c92014-09-05 16:28:14 -0400693 for (size_t textureUnit = 0; textureUnit < mSamplers.size(); textureUnit++)
Shannon Woods53a94a82014-06-24 15:20:36 -0400694 {
Geoff Lang76b10c92014-09-05 16:28:14 -0400695 BindingPointer<Sampler> &samplerBinding = mSamplers[textureUnit];
696 if (samplerBinding.id() == sampler)
Shannon Woods53a94a82014-06-24 15:20:36 -0400697 {
Geoff Lang76b10c92014-09-05 16:28:14 -0400698 samplerBinding.set(NULL);
Shannon Woods53a94a82014-06-24 15:20:36 -0400699 }
700 }
701}
702
703void State::setRenderbufferBinding(Renderbuffer *renderbuffer)
704{
705 mRenderbuffer.set(renderbuffer);
706}
707
708GLuint State::getRenderbufferId() const
709{
710 return mRenderbuffer.id();
711}
712
713Renderbuffer *State::getCurrentRenderbuffer()
714{
715 return mRenderbuffer.get();
716}
717
718void State::detachRenderbuffer(GLuint renderbuffer)
719{
720 // [OpenGL ES 2.0.24] section 4.4 page 109:
721 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
722 // had been executed with the target RENDERBUFFER and name of zero.
723
724 if (mRenderbuffer.id() == renderbuffer)
725 {
726 mRenderbuffer.set(NULL);
727 }
728
729 // [OpenGL ES 2.0.24] section 4.4 page 111:
730 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
731 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
732 // point to which this image was attached in the currently bound framebuffer.
733
734 Framebuffer *readFramebuffer = mReadFramebuffer;
735 Framebuffer *drawFramebuffer = mDrawFramebuffer;
736
737 if (readFramebuffer)
738 {
739 readFramebuffer->detachRenderbuffer(renderbuffer);
740 }
741
742 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
743 {
744 drawFramebuffer->detachRenderbuffer(renderbuffer);
745 }
746
747}
748
749void State::setReadFramebufferBinding(Framebuffer *framebuffer)
750{
751 mReadFramebuffer = framebuffer;
752}
753
754void State::setDrawFramebufferBinding(Framebuffer *framebuffer)
755{
756 mDrawFramebuffer = framebuffer;
757}
758
759Framebuffer *State::getTargetFramebuffer(GLenum target) const
760{
761 switch (target)
762 {
763 case GL_READ_FRAMEBUFFER_ANGLE: return mReadFramebuffer;
764 case GL_DRAW_FRAMEBUFFER_ANGLE:
765 case GL_FRAMEBUFFER: return mDrawFramebuffer;
766 default: UNREACHABLE(); return NULL;
767 }
768}
769
770Framebuffer *State::getReadFramebuffer()
771{
772 return mReadFramebuffer;
773}
774
775Framebuffer *State::getDrawFramebuffer()
776{
777 return mDrawFramebuffer;
778}
779
780const Framebuffer *State::getReadFramebuffer() const
781{
782 return mReadFramebuffer;
783}
784
785const Framebuffer *State::getDrawFramebuffer() const
786{
787 return mDrawFramebuffer;
788}
789
790bool State::removeReadFramebufferBinding(GLuint framebuffer)
791{
792 if (mReadFramebuffer->id() == framebuffer)
793 {
794 mReadFramebuffer = NULL;
795 return true;
796 }
797
798 return false;
799}
800
801bool State::removeDrawFramebufferBinding(GLuint framebuffer)
802{
803 if (mDrawFramebuffer->id() == framebuffer)
804 {
805 mDrawFramebuffer = NULL;
806 return true;
807 }
808
809 return false;
810}
811
812void State::setVertexArrayBinding(VertexArray *vertexArray)
813{
814 mVertexArray = vertexArray;
815}
816
817GLuint State::getVertexArrayId() const
818{
819 ASSERT(mVertexArray != NULL);
820 return mVertexArray->id();
821}
822
823VertexArray *State::getVertexArray() const
824{
825 ASSERT(mVertexArray != NULL);
826 return mVertexArray;
827}
828
829bool State::removeVertexArrayBinding(GLuint vertexArray)
830{
831 if (mVertexArray->id() == vertexArray)
832 {
833 mVertexArray = NULL;
834 return true;
835 }
836
837 return false;
838}
839
840void State::setCurrentProgram(GLuint programId, Program *newProgram)
841{
842 mCurrentProgramId = programId; // set new ID before trying to delete program binary; otherwise it will only be flagged for deletion
843 mCurrentProgramBinary.set(NULL);
844
845 if (newProgram)
846 {
847 newProgram->addRef();
848 mCurrentProgramBinary.set(newProgram->getProgramBinary());
849 }
850}
851
852void State::setCurrentProgramBinary(ProgramBinary *binary)
853{
854 mCurrentProgramBinary.set(binary);
855}
856
857GLuint State::getCurrentProgramId() const
858{
859 return mCurrentProgramId;
860}
861
862ProgramBinary *State::getCurrentProgramBinary() const
863{
864 return mCurrentProgramBinary.get();
865}
866
867void State::setTransformFeedbackBinding(TransformFeedback *transformFeedback)
868{
869 mTransformFeedback.set(transformFeedback);
870}
871
872TransformFeedback *State::getCurrentTransformFeedback() const
873{
874 return mTransformFeedback.get();
875}
876
877void State::detachTransformFeedback(GLuint transformFeedback)
878{
879 if (mTransformFeedback.id() == transformFeedback)
880 {
881 mTransformFeedback.set(NULL);
882 }
883}
884
885bool State::isQueryActive() const
886{
887 for (State::ActiveQueryMap::const_iterator i = mActiveQueries.begin();
888 i != mActiveQueries.end(); i++)
889 {
890 if (i->second.get() != NULL)
891 {
892 return true;
893 }
894 }
895
896 return false;
897}
898
899void State::setActiveQuery(GLenum target, Query *query)
900{
901 mActiveQueries[target].set(query);
902}
903
904GLuint State::getActiveQueryId(GLenum target) const
905{
906 const Query *query = getActiveQuery(target);
907 return (query ? query->id() : 0u);
908}
909
910Query *State::getActiveQuery(GLenum target) const
911{
912 // All query types should already exist in the activeQueries map
913 ASSERT(mActiveQueries.find(target) != mActiveQueries.end());
914
915 return mActiveQueries.at(target).get();
916}
917
918void State::setArrayBufferBinding(Buffer *buffer)
919{
920 mArrayBuffer.set(buffer);
921}
922
923GLuint State::getArrayBufferId() const
924{
925 return mArrayBuffer.id();
926}
927
928bool State::removeArrayBufferBinding(GLuint buffer)
929{
930 if (mArrayBuffer.id() == buffer)
931 {
932 mArrayBuffer.set(NULL);
933 return true;
934 }
935
936 return false;
937}
938
939void State::setGenericUniformBufferBinding(Buffer *buffer)
940{
941 mGenericUniformBuffer.set(buffer);
942}
943
944void State::setIndexedUniformBufferBinding(GLuint index, Buffer *buffer, GLintptr offset, GLsizeiptr size)
945{
946 mUniformBuffers[index].set(buffer, offset, size);
947}
948
949GLuint State::getIndexedUniformBufferId(GLuint index) const
950{
Shannon Woodsf3acaf92014-09-23 18:07:11 -0400951 ASSERT(static_cast<size_t>(index) < mUniformBuffers.size());
Shannon Woods53a94a82014-06-24 15:20:36 -0400952
953 return mUniformBuffers[index].id();
954}
955
956Buffer *State::getIndexedUniformBuffer(GLuint index) const
957{
Shannon Woodsf3acaf92014-09-23 18:07:11 -0400958 ASSERT(static_cast<size_t>(index) < mUniformBuffers.size());
Shannon Woods53a94a82014-06-24 15:20:36 -0400959
960 return mUniformBuffers[index].get();
961}
962
963void State::setGenericTransformFeedbackBufferBinding(Buffer *buffer)
964{
965 mGenericTransformFeedbackBuffer.set(buffer);
966}
967
968void State::setIndexedTransformFeedbackBufferBinding(GLuint index, Buffer *buffer, GLintptr offset, GLsizeiptr size)
969{
970 mTransformFeedbackBuffers[index].set(buffer, offset, size);
971}
972
973GLuint State::getIndexedTransformFeedbackBufferId(GLuint index) const
974{
Shannon Woods8299bb02014-09-26 18:55:43 -0400975 ASSERT(static_cast<size_t>(index) < mTransformFeedbackBuffers.size());
Shannon Woods53a94a82014-06-24 15:20:36 -0400976
977 return mTransformFeedbackBuffers[index].id();
978}
979
980Buffer *State::getIndexedTransformFeedbackBuffer(GLuint index) const
981{
Shannon Woods8299bb02014-09-26 18:55:43 -0400982 ASSERT(static_cast<size_t>(index) < mTransformFeedbackBuffers.size());
Shannon Woods53a94a82014-06-24 15:20:36 -0400983
984 return mTransformFeedbackBuffers[index].get();
985}
986
987GLuint State::getIndexedTransformFeedbackBufferOffset(GLuint index) const
988{
Shannon Woods8299bb02014-09-26 18:55:43 -0400989 ASSERT(static_cast<size_t>(index) < mTransformFeedbackBuffers.size());
Shannon Woods53a94a82014-06-24 15:20:36 -0400990
991 return mTransformFeedbackBuffers[index].getOffset();
992}
993
Shannon Woods8299bb02014-09-26 18:55:43 -0400994size_t State::getTransformFeedbackBufferIndexRange() const
995{
996 return mTransformFeedbackBuffers.size();
997}
998
Shannon Woods53a94a82014-06-24 15:20:36 -0400999void State::setCopyReadBufferBinding(Buffer *buffer)
1000{
1001 mCopyReadBuffer.set(buffer);
1002}
1003
1004void State::setCopyWriteBufferBinding(Buffer *buffer)
1005{
1006 mCopyWriteBuffer.set(buffer);
1007}
1008
1009void State::setPixelPackBufferBinding(Buffer *buffer)
1010{
1011 mPack.pixelBuffer.set(buffer);
1012}
1013
1014void State::setPixelUnpackBufferBinding(Buffer *buffer)
1015{
1016 mUnpack.pixelBuffer.set(buffer);
1017}
1018
1019Buffer *State::getTargetBuffer(GLenum target) const
1020{
1021 switch (target)
1022 {
1023 case GL_ARRAY_BUFFER: return mArrayBuffer.get();
1024 case GL_COPY_READ_BUFFER: return mCopyReadBuffer.get();
1025 case GL_COPY_WRITE_BUFFER: return mCopyWriteBuffer.get();
1026 case GL_ELEMENT_ARRAY_BUFFER: return getVertexArray()->getElementArrayBuffer();
1027 case GL_PIXEL_PACK_BUFFER: return mPack.pixelBuffer.get();
1028 case GL_PIXEL_UNPACK_BUFFER: return mUnpack.pixelBuffer.get();
1029 case GL_TRANSFORM_FEEDBACK_BUFFER: return mGenericTransformFeedbackBuffer.get();
1030 case GL_UNIFORM_BUFFER: return mGenericUniformBuffer.get();
1031 default: UNREACHABLE(); return NULL;
1032 }
1033}
1034
1035void State::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
1036{
1037 getVertexArray()->enableAttribute(attribNum, enabled);
1038}
1039
1040void State::setVertexAttribf(GLuint index, const GLfloat values[4])
1041{
Shannon Woods23e05002014-09-22 19:07:27 -04001042 ASSERT(static_cast<size_t>(index) < mVertexAttribCurrentValues.size());
Shannon Woods53a94a82014-06-24 15:20:36 -04001043 mVertexAttribCurrentValues[index].setFloatValues(values);
1044}
1045
1046void State::setVertexAttribu(GLuint index, const GLuint values[4])
1047{
Shannon Woods23e05002014-09-22 19:07:27 -04001048 ASSERT(static_cast<size_t>(index) < mVertexAttribCurrentValues.size());
Shannon Woods53a94a82014-06-24 15:20:36 -04001049 mVertexAttribCurrentValues[index].setUnsignedIntValues(values);
1050}
1051
1052void State::setVertexAttribi(GLuint index, const GLint values[4])
1053{
Shannon Woods23e05002014-09-22 19:07:27 -04001054 ASSERT(static_cast<size_t>(index) < mVertexAttribCurrentValues.size());
Shannon Woods53a94a82014-06-24 15:20:36 -04001055 mVertexAttribCurrentValues[index].setIntValues(values);
1056}
1057
1058void State::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
1059 bool pureInteger, GLsizei stride, const void *pointer)
1060{
1061 getVertexArray()->setAttributeState(attribNum, boundBuffer, size, type, normalized, pureInteger, stride, pointer);
1062}
1063
1064const VertexAttribute &State::getVertexAttribState(unsigned int attribNum) const
1065{
1066 return getVertexArray()->getVertexAttribute(attribNum);
1067}
1068
1069const VertexAttribCurrentValueData &State::getVertexAttribCurrentValue(unsigned int attribNum) const
1070{
Shannon Woods23e05002014-09-22 19:07:27 -04001071 ASSERT(static_cast<size_t>(attribNum) < mVertexAttribCurrentValues.size());
Shannon Woods53a94a82014-06-24 15:20:36 -04001072 return mVertexAttribCurrentValues[attribNum];
1073}
1074
Shannon Woods53a94a82014-06-24 15:20:36 -04001075const void *State::getVertexAttribPointer(unsigned int attribNum) const
1076{
1077 return getVertexArray()->getVertexAttribute(attribNum).pointer;
1078}
1079
1080void State::setPackAlignment(GLint alignment)
1081{
1082 mPack.alignment = alignment;
1083}
1084
1085GLint State::getPackAlignment() const
1086{
1087 return mPack.alignment;
1088}
1089
1090void State::setPackReverseRowOrder(bool reverseRowOrder)
1091{
1092 mPack.reverseRowOrder = reverseRowOrder;
1093}
1094
1095bool State::getPackReverseRowOrder() const
1096{
1097 return mPack.reverseRowOrder;
1098}
1099
1100const PixelPackState &State::getPackState() const
1101{
1102 return mPack;
1103}
1104
1105void State::setUnpackAlignment(GLint alignment)
1106{
1107 mUnpack.alignment = alignment;
1108}
1109
1110GLint State::getUnpackAlignment() const
1111{
1112 return mUnpack.alignment;
1113}
1114
1115const PixelUnpackState &State::getUnpackState() const
1116{
1117 return mUnpack;
1118}
1119
1120void State::getBooleanv(GLenum pname, GLboolean *params)
1121{
1122 switch (pname)
1123 {
1124 case GL_SAMPLE_COVERAGE_INVERT: *params = mSampleCoverageInvert; break;
1125 case GL_DEPTH_WRITEMASK: *params = mDepthStencil.depthMask; break;
1126 case GL_COLOR_WRITEMASK:
1127 params[0] = mBlend.colorMaskRed;
1128 params[1] = mBlend.colorMaskGreen;
1129 params[2] = mBlend.colorMaskBlue;
1130 params[3] = mBlend.colorMaskAlpha;
1131 break;
1132 case GL_CULL_FACE: *params = mRasterizer.cullFace; break;
1133 case GL_POLYGON_OFFSET_FILL: *params = mRasterizer.polygonOffsetFill; break;
1134 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mBlend.sampleAlphaToCoverage; break;
1135 case GL_SAMPLE_COVERAGE: *params = mSampleCoverage; break;
1136 case GL_SCISSOR_TEST: *params = mScissorTest; break;
1137 case GL_STENCIL_TEST: *params = mDepthStencil.stencilTest; break;
1138 case GL_DEPTH_TEST: *params = mDepthStencil.depthTest; break;
1139 case GL_BLEND: *params = mBlend.blend; break;
1140 case GL_DITHER: *params = mBlend.dither; break;
1141 case GL_TRANSFORM_FEEDBACK_ACTIVE: *params = getCurrentTransformFeedback()->isStarted(); break;
1142 case GL_TRANSFORM_FEEDBACK_PAUSED: *params = getCurrentTransformFeedback()->isPaused(); break;
1143 default:
1144 UNREACHABLE();
1145 break;
1146 }
1147}
1148
1149void State::getFloatv(GLenum pname, GLfloat *params)
1150{
1151 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1152 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1153 // GetIntegerv as its native query function. As it would require conversion in any
1154 // case, this should make no difference to the calling application.
1155 switch (pname)
1156 {
1157 case GL_LINE_WIDTH: *params = mLineWidth; break;
1158 case GL_SAMPLE_COVERAGE_VALUE: *params = mSampleCoverageValue; break;
1159 case GL_DEPTH_CLEAR_VALUE: *params = mDepthClearValue; break;
1160 case GL_POLYGON_OFFSET_FACTOR: *params = mRasterizer.polygonOffsetFactor; break;
1161 case GL_POLYGON_OFFSET_UNITS: *params = mRasterizer.polygonOffsetUnits; break;
1162 case GL_DEPTH_RANGE:
1163 params[0] = mNearZ;
1164 params[1] = mFarZ;
1165 break;
1166 case GL_COLOR_CLEAR_VALUE:
1167 params[0] = mColorClearValue.red;
1168 params[1] = mColorClearValue.green;
1169 params[2] = mColorClearValue.blue;
1170 params[3] = mColorClearValue.alpha;
1171 break;
1172 case GL_BLEND_COLOR:
1173 params[0] = mBlendColor.red;
1174 params[1] = mBlendColor.green;
1175 params[2] = mBlendColor.blue;
1176 params[3] = mBlendColor.alpha;
1177 break;
1178 default:
1179 UNREACHABLE();
1180 break;
1181 }
1182}
1183
1184void State::getIntegerv(GLenum pname, GLint *params)
1185{
1186 if (pname >= GL_DRAW_BUFFER0_EXT && pname <= GL_DRAW_BUFFER15_EXT)
1187 {
1188 unsigned int colorAttachment = (pname - GL_DRAW_BUFFER0_EXT);
Shannon Woods2df6a602014-09-26 16:12:07 -04001189 ASSERT(colorAttachment < mMaxDrawBuffers);
Shannon Woods53a94a82014-06-24 15:20:36 -04001190 Framebuffer *framebuffer = mDrawFramebuffer;
1191 *params = framebuffer->getDrawBufferState(colorAttachment);
1192 return;
1193 }
1194
1195 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1196 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1197 // GetIntegerv as its native query function. As it would require conversion in any
1198 // case, this should make no difference to the calling application. You may find it in
1199 // State::getFloatv.
1200 switch (pname)
1201 {
1202 case GL_ARRAY_BUFFER_BINDING: *params = mArrayBuffer.id(); break;
1203 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = getVertexArray()->getElementArrayBufferId(); break;
1204 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1205 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mDrawFramebuffer->id(); break;
1206 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mReadFramebuffer->id(); break;
1207 case GL_RENDERBUFFER_BINDING: *params = mRenderbuffer.id(); break;
1208 case GL_VERTEX_ARRAY_BINDING: *params = mVertexArray->id(); break;
1209 case GL_CURRENT_PROGRAM: *params = mCurrentProgramId; break;
1210 case GL_PACK_ALIGNMENT: *params = mPack.alignment; break;
1211 case GL_PACK_REVERSE_ROW_ORDER_ANGLE: *params = mPack.reverseRowOrder; break;
1212 case GL_UNPACK_ALIGNMENT: *params = mUnpack.alignment; break;
1213 case GL_GENERATE_MIPMAP_HINT: *params = mGenerateMipmapHint; break;
1214 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mFragmentShaderDerivativeHint; break;
1215 case GL_ACTIVE_TEXTURE: *params = (mActiveSampler + GL_TEXTURE0); break;
1216 case GL_STENCIL_FUNC: *params = mDepthStencil.stencilFunc; break;
1217 case GL_STENCIL_REF: *params = mStencilRef; break;
1218 case GL_STENCIL_VALUE_MASK: *params = clampToInt(mDepthStencil.stencilMask); break;
1219 case GL_STENCIL_BACK_FUNC: *params = mDepthStencil.stencilBackFunc; break;
1220 case GL_STENCIL_BACK_REF: *params = mStencilBackRef; break;
1221 case GL_STENCIL_BACK_VALUE_MASK: *params = clampToInt(mDepthStencil.stencilBackMask); break;
1222 case GL_STENCIL_FAIL: *params = mDepthStencil.stencilFail; break;
1223 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mDepthStencil.stencilPassDepthFail; break;
1224 case GL_STENCIL_PASS_DEPTH_PASS: *params = mDepthStencil.stencilPassDepthPass; break;
1225 case GL_STENCIL_BACK_FAIL: *params = mDepthStencil.stencilBackFail; break;
1226 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mDepthStencil.stencilBackPassDepthFail; break;
1227 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mDepthStencil.stencilBackPassDepthPass; break;
1228 case GL_DEPTH_FUNC: *params = mDepthStencil.depthFunc; break;
1229 case GL_BLEND_SRC_RGB: *params = mBlend.sourceBlendRGB; break;
1230 case GL_BLEND_SRC_ALPHA: *params = mBlend.sourceBlendAlpha; break;
1231 case GL_BLEND_DST_RGB: *params = mBlend.destBlendRGB; break;
1232 case GL_BLEND_DST_ALPHA: *params = mBlend.destBlendAlpha; break;
1233 case GL_BLEND_EQUATION_RGB: *params = mBlend.blendEquationRGB; break;
1234 case GL_BLEND_EQUATION_ALPHA: *params = mBlend.blendEquationAlpha; break;
1235 case GL_STENCIL_WRITEMASK: *params = clampToInt(mDepthStencil.stencilWritemask); break;
1236 case GL_STENCIL_BACK_WRITEMASK: *params = clampToInt(mDepthStencil.stencilBackWritemask); break;
1237 case GL_STENCIL_CLEAR_VALUE: *params = mStencilClearValue; break;
1238 case GL_SAMPLE_BUFFERS:
1239 case GL_SAMPLES:
1240 {
1241 gl::Framebuffer *framebuffer = mDrawFramebuffer;
1242 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1243 {
1244 switch (pname)
1245 {
1246 case GL_SAMPLE_BUFFERS:
1247 if (framebuffer->getSamples() != 0)
1248 {
1249 *params = 1;
1250 }
1251 else
1252 {
1253 *params = 0;
1254 }
1255 break;
1256 case GL_SAMPLES:
1257 *params = framebuffer->getSamples();
1258 break;
1259 }
1260 }
1261 else
1262 {
1263 *params = 0;
1264 }
1265 }
1266 break;
1267 case GL_VIEWPORT:
1268 params[0] = mViewport.x;
1269 params[1] = mViewport.y;
1270 params[2] = mViewport.width;
1271 params[3] = mViewport.height;
1272 break;
1273 case GL_SCISSOR_BOX:
1274 params[0] = mScissor.x;
1275 params[1] = mScissor.y;
1276 params[2] = mScissor.width;
1277 params[3] = mScissor.height;
1278 break;
1279 case GL_CULL_FACE_MODE: *params = mRasterizer.cullMode; break;
1280 case GL_FRONT_FACE: *params = mRasterizer.frontFace; break;
1281 case GL_RED_BITS:
1282 case GL_GREEN_BITS:
1283 case GL_BLUE_BITS:
1284 case GL_ALPHA_BITS:
1285 {
1286 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1287 gl::FramebufferAttachment *colorbuffer = framebuffer->getFirstColorbuffer();
1288
1289 if (colorbuffer)
1290 {
1291 switch (pname)
1292 {
1293 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1294 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1295 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1296 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1297 }
1298 }
1299 else
1300 {
1301 *params = 0;
1302 }
1303 }
1304 break;
1305 case GL_DEPTH_BITS:
1306 {
1307 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1308 gl::FramebufferAttachment *depthbuffer = framebuffer->getDepthbuffer();
1309
1310 if (depthbuffer)
1311 {
1312 *params = depthbuffer->getDepthSize();
1313 }
1314 else
1315 {
1316 *params = 0;
1317 }
1318 }
1319 break;
1320 case GL_STENCIL_BITS:
1321 {
1322 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1323 gl::FramebufferAttachment *stencilbuffer = framebuffer->getStencilbuffer();
1324
1325 if (stencilbuffer)
1326 {
1327 *params = stencilbuffer->getStencilSize();
1328 }
1329 else
1330 {
1331 *params = 0;
1332 }
1333 }
1334 break;
1335 case GL_TEXTURE_BINDING_2D:
Shannon Woods2df6a602014-09-26 16:12:07 -04001336 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
Geoff Lang76b10c92014-09-05 16:28:14 -04001337 *params = mSamplerTextures.at(GL_TEXTURE_2D)[mActiveSampler].id();
Shannon Woods53a94a82014-06-24 15:20:36 -04001338 break;
1339 case GL_TEXTURE_BINDING_CUBE_MAP:
Shannon Woods2df6a602014-09-26 16:12:07 -04001340 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
Geoff Lang76b10c92014-09-05 16:28:14 -04001341 *params = mSamplerTextures.at(GL_TEXTURE_CUBE_MAP)[mActiveSampler].id();
Shannon Woods53a94a82014-06-24 15:20:36 -04001342 break;
1343 case GL_TEXTURE_BINDING_3D:
Shannon Woods2df6a602014-09-26 16:12:07 -04001344 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
Geoff Lang76b10c92014-09-05 16:28:14 -04001345 *params = mSamplerTextures.at(GL_TEXTURE_3D)[mActiveSampler].id();
Shannon Woods53a94a82014-06-24 15:20:36 -04001346 break;
1347 case GL_TEXTURE_BINDING_2D_ARRAY:
Shannon Woods2df6a602014-09-26 16:12:07 -04001348 ASSERT(mActiveSampler < mMaxCombinedTextureImageUnits);
Geoff Lang76b10c92014-09-05 16:28:14 -04001349 *params = mSamplerTextures.at(GL_TEXTURE_2D_ARRAY)[mActiveSampler].id();
Shannon Woods53a94a82014-06-24 15:20:36 -04001350 break;
1351 case GL_UNIFORM_BUFFER_BINDING:
1352 *params = mGenericUniformBuffer.id();
1353 break;
1354 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING:
1355 *params = mGenericTransformFeedbackBuffer.id();
1356 break;
1357 case GL_COPY_READ_BUFFER_BINDING:
1358 *params = mCopyReadBuffer.id();
1359 break;
1360 case GL_COPY_WRITE_BUFFER_BINDING:
1361 *params = mCopyWriteBuffer.id();
1362 break;
1363 case GL_PIXEL_PACK_BUFFER_BINDING:
1364 *params = mPack.pixelBuffer.id();
1365 break;
1366 case GL_PIXEL_UNPACK_BUFFER_BINDING:
1367 *params = mUnpack.pixelBuffer.id();
1368 break;
1369 default:
1370 UNREACHABLE();
1371 break;
1372 }
1373}
1374
1375bool State::getIndexedIntegerv(GLenum target, GLuint index, GLint *data)
1376{
1377 switch (target)
1378 {
1379 case GL_TRANSFORM_FEEDBACK_BUFFER_BINDING:
Shannon Woods8299bb02014-09-26 18:55:43 -04001380 if (static_cast<size_t>(index) < mTransformFeedbackBuffers.size())
Shannon Woods53a94a82014-06-24 15:20:36 -04001381 {
1382 *data = mTransformFeedbackBuffers[index].id();
1383 }
1384 break;
1385 case GL_UNIFORM_BUFFER_BINDING:
Shannon Woodsf3acaf92014-09-23 18:07:11 -04001386 if (static_cast<size_t>(index) < mUniformBuffers.size())
Shannon Woods53a94a82014-06-24 15:20:36 -04001387 {
1388 *data = mUniformBuffers[index].id();
1389 }
1390 break;
1391 default:
1392 return false;
1393 }
1394
1395 return true;
1396}
1397
1398bool State::getIndexedInteger64v(GLenum target, GLuint index, GLint64 *data)
1399{
1400 switch (target)
1401 {
1402 case GL_TRANSFORM_FEEDBACK_BUFFER_START:
Shannon Woods8299bb02014-09-26 18:55:43 -04001403 if (static_cast<size_t>(index) < mTransformFeedbackBuffers.size())
Shannon Woods53a94a82014-06-24 15:20:36 -04001404 {
1405 *data = mTransformFeedbackBuffers[index].getOffset();
1406 }
1407 break;
1408 case GL_TRANSFORM_FEEDBACK_BUFFER_SIZE:
Shannon Woods8299bb02014-09-26 18:55:43 -04001409 if (static_cast<size_t>(index) < mTransformFeedbackBuffers.size())
Shannon Woods53a94a82014-06-24 15:20:36 -04001410 {
1411 *data = mTransformFeedbackBuffers[index].getSize();
1412 }
1413 break;
1414 case GL_UNIFORM_BUFFER_START:
Shannon Woodsf3acaf92014-09-23 18:07:11 -04001415 if (static_cast<size_t>(index) < mUniformBuffers.size())
Shannon Woods53a94a82014-06-24 15:20:36 -04001416 {
1417 *data = mUniformBuffers[index].getOffset();
1418 }
1419 break;
1420 case GL_UNIFORM_BUFFER_SIZE:
Shannon Woodsf3acaf92014-09-23 18:07:11 -04001421 if (static_cast<size_t>(index) < mUniformBuffers.size())
Shannon Woods53a94a82014-06-24 15:20:36 -04001422 {
1423 *data = mUniformBuffers[index].getSize();
1424 }
1425 break;
1426 default:
1427 return false;
1428 }
1429
1430 return true;
1431}
1432
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001433bool State::hasMappedBuffer(GLenum target) const
1434{
1435 if (target == GL_ARRAY_BUFFER)
1436 {
Shannon Woods23e05002014-09-22 19:07:27 -04001437 for (size_t attribIndex = 0; attribIndex < mVertexAttribCurrentValues.size(); attribIndex++)
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001438 {
Shannon Woods23e05002014-09-22 19:07:27 -04001439 const gl::VertexAttribute &vertexAttrib = getVertexAttribState(static_cast<unsigned int>(attribIndex));
Jamie Madilld9ba4f72014-08-04 10:47:59 -04001440 gl::Buffer *boundBuffer = vertexAttrib.buffer.get();
1441 if (vertexAttrib.enabled && boundBuffer && boundBuffer->isMapped())
1442 {
1443 return true;
1444 }
1445 }
1446
1447 return false;
1448 }
1449 else
1450 {
1451 Buffer *buffer = getTargetBuffer(target);
1452 return (buffer && buffer->isMapped());
1453 }
1454}
1455
Shannon Woods53a94a82014-06-24 15:20:36 -04001456}