blob: a1093700b9f62efa412735aed38041a47b9ca3a2 [file] [log] [blame]
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001//
2// Copyright (c) 2002-2012 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// Context.cpp: Implements the gl::Context class, managing all GL state and performing
8// rendering operations. It is the GLES2 specific implementation of EGLContext.
9
10#include "libGLESv2/Context.h"
11
12#include <algorithm>
13
14#include "libEGL/Display.h"
15
16#include "libGLESv2/main.h"
17#include "libGLESv2/mathutil.h"
18#include "libGLESv2/utilities.h"
19#include "libGLESv2/Blit.h"
20#include "libGLESv2/ResourceManager.h"
21#include "libGLESv2/Buffer.h"
22#include "libGLESv2/Fence.h"
23#include "libGLESv2/FrameBuffer.h"
24#include "libGLESv2/Program.h"
25#include "libGLESv2/ProgramBinary.h"
26#include "libGLESv2/Query.h"
27#include "libGLESv2/RenderBuffer.h"
28#include "libGLESv2/Shader.h"
29#include "libGLESv2/Texture.h"
30#include "libGLESv2/VertexDataManager.h"
31#include "libGLESv2/IndexDataManager.h"
32
33#undef near
34#undef far
35
36namespace gl
37{
38Context::Context(const egl::Config *config, const gl::Context *shareContext, bool notifyResets, bool robustAccess) : mConfig(config)
39{
40 ASSERT(robustAccess == false); // Unimplemented
41
42 mDisplay = NULL;
43 mDevice = NULL;
44
45 mFenceHandleAllocator.setBaseHandle(0);
46
47 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
48
49 mState.depthClearValue = 1.0f;
50 mState.stencilClearValue = 0;
51
52 mState.cullFace = false;
53 mState.cullMode = GL_BACK;
54 mState.frontFace = GL_CCW;
55 mState.depthTest = false;
56 mState.depthFunc = GL_LESS;
57 mState.blend = false;
58 mState.sourceBlendRGB = GL_ONE;
59 mState.sourceBlendAlpha = GL_ONE;
60 mState.destBlendRGB = GL_ZERO;
61 mState.destBlendAlpha = GL_ZERO;
62 mState.blendEquationRGB = GL_FUNC_ADD;
63 mState.blendEquationAlpha = GL_FUNC_ADD;
64 mState.blendColor.red = 0;
65 mState.blendColor.green = 0;
66 mState.blendColor.blue = 0;
67 mState.blendColor.alpha = 0;
68 mState.stencilTest = false;
69 mState.stencilFunc = GL_ALWAYS;
70 mState.stencilRef = 0;
71 mState.stencilMask = -1;
72 mState.stencilWritemask = -1;
73 mState.stencilBackFunc = GL_ALWAYS;
74 mState.stencilBackRef = 0;
75 mState.stencilBackMask = - 1;
76 mState.stencilBackWritemask = -1;
77 mState.stencilFail = GL_KEEP;
78 mState.stencilPassDepthFail = GL_KEEP;
79 mState.stencilPassDepthPass = GL_KEEP;
80 mState.stencilBackFail = GL_KEEP;
81 mState.stencilBackPassDepthFail = GL_KEEP;
82 mState.stencilBackPassDepthPass = GL_KEEP;
83 mState.polygonOffsetFill = false;
84 mState.polygonOffsetFactor = 0.0f;
85 mState.polygonOffsetUnits = 0.0f;
86 mState.sampleAlphaToCoverage = false;
87 mState.sampleCoverage = false;
88 mState.sampleCoverageValue = 1.0f;
89 mState.sampleCoverageInvert = false;
90 mState.scissorTest = false;
91 mState.dither = true;
92 mState.generateMipmapHint = GL_DONT_CARE;
93 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
94
95 mState.lineWidth = 1.0f;
96
97 mState.viewportX = 0;
98 mState.viewportY = 0;
99 mState.viewportWidth = config->mDisplayMode.Width;
100 mState.viewportHeight = config->mDisplayMode.Height;
101 mState.zNear = 0.0f;
102 mState.zFar = 1.0f;
103
104 mState.scissorX = 0;
105 mState.scissorY = 0;
106 mState.scissorWidth = config->mDisplayMode.Width;
107 mState.scissorHeight = config->mDisplayMode.Height;
108
109 mState.colorMaskRed = true;
110 mState.colorMaskGreen = true;
111 mState.colorMaskBlue = true;
112 mState.colorMaskAlpha = true;
113 mState.depthMask = true;
114
115 if (shareContext != NULL)
116 {
117 mResourceManager = shareContext->mResourceManager;
118 mResourceManager->addRef();
119 }
120 else
121 {
122 mResourceManager = new ResourceManager();
123 }
124
125 // [OpenGL ES 2.0.24] section 3.7 page 83:
126 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
127 // and cube map texture state vectors respectively associated with them.
128 // In order that access to these initial textures not be lost, they are treated as texture
129 // objects all of whose names are 0.
130
131 mTexture2DZero.set(new Texture2D(0));
132 mTextureCubeMapZero.set(new TextureCubeMap(0));
133
134 mState.activeSampler = 0;
135 bindArrayBuffer(0);
136 bindElementArrayBuffer(0);
137 bindTextureCubeMap(0);
138 bindTexture2D(0);
139 bindReadFramebuffer(0);
140 bindDrawFramebuffer(0);
141 bindRenderbuffer(0);
142
143 mState.currentProgram = 0;
144
145 mState.packAlignment = 4;
146 mState.unpackAlignment = 4;
147 mState.packReverseRowOrder = false;
148
149 mVertexDataManager = NULL;
150 mIndexDataManager = NULL;
151 mBlit = NULL;
152 mLineLoopIB = NULL;
153
154 mInvalidEnum = false;
155 mInvalidValue = false;
156 mInvalidOperation = false;
157 mOutOfMemory = false;
158 mInvalidFramebufferOperation = false;
159
160 mHasBeenCurrent = false;
161 mContextLost = false;
162 mResetStatus = GL_NO_ERROR;
163 mResetStrategy = (notifyResets ? GL_LOSE_CONTEXT_ON_RESET_EXT : GL_NO_RESET_NOTIFICATION_EXT);
164 mRobustAccess = robustAccess;
165
166 mSupportsDXT1Textures = false;
167 mSupportsDXT3Textures = false;
168 mSupportsDXT5Textures = false;
169 mSupportsEventQueries = false;
170 mSupportsOcclusionQueries = false;
171 mNumCompressedTextureFormats = 0;
172 mMaxSupportedSamples = 0;
173 mMaskedClearSavedState = NULL;
174 markAllStateDirty();
175}
176
177Context::~Context()
178{
179 if (mState.currentProgram != 0)
180 {
181 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
182 if (programObject)
183 {
184 programObject->release();
185 }
186 mState.currentProgram = 0;
187 }
188
189 while (!mFramebufferMap.empty())
190 {
191 deleteFramebuffer(mFramebufferMap.begin()->first);
192 }
193
194 while (!mFenceMap.empty())
195 {
196 deleteFence(mFenceMap.begin()->first);
197 }
198
199 while (!mQueryMap.empty())
200 {
201 deleteQuery(mQueryMap.begin()->first);
202 }
203
204 while (!mMultiSampleSupport.empty())
205 {
206 delete [] mMultiSampleSupport.begin()->second;
207 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
208 }
209
210 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
211 {
212 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
213 {
214 mState.samplerTexture[type][sampler].set(NULL);
215 }
216 }
217
218 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
219 {
220 mIncompleteTextures[type].set(NULL);
221 }
222
223 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
224 {
225 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
226 }
227
228 for (int i = 0; i < QUERY_TYPE_COUNT; i++)
229 {
230 mState.activeQuery[i].set(NULL);
231 }
232
233 mState.arrayBuffer.set(NULL);
234 mState.elementArrayBuffer.set(NULL);
235 mState.renderbuffer.set(NULL);
236
237 mTexture2DZero.set(NULL);
238 mTextureCubeMapZero.set(NULL);
239
240 delete mVertexDataManager;
241 delete mIndexDataManager;
242 delete mBlit;
243 delete mLineLoopIB;
244
245 if (mMaskedClearSavedState)
246 {
247 mMaskedClearSavedState->Release();
248 }
249
250 mResourceManager->release();
251}
252
253void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
254{
255 mDisplay = display;
256 mDevice = mDisplay->getDevice();
257
258 if (!mHasBeenCurrent)
259 {
260 mDeviceCaps = mDisplay->getDeviceCaps();
261
262 mVertexDataManager = new VertexDataManager(this, mDevice);
263 mIndexDataManager = new IndexDataManager(this, mDevice);
264 mBlit = new Blit(this);
265
266 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
267 mMaximumPointSize = mDeviceCaps.MaxPointSize;
268 mSupportsVertexTexture = mDisplay->getVertexTextureSupport();
269 mSupportsNonPower2Texture = mDisplay->getNonPower2TextureSupport();
270 mSupportsInstancing = mDisplay->getInstancingSupport();
271
272 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
273 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
274 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
275 mMaxRenderbufferDimension = mMaxTextureDimension;
276 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
daniel@transgaming.com07ab8412012-07-12 15:17:09 +0000277 mMaxTextureAnisotropy = mDisplay->getTextureFilterAnisotropySupport();
278 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d, MaxTextureAnisotropy=%f",
279 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel, mMaxTextureAnisotropy);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000280
281 const D3DFORMAT renderBufferFormats[] =
282 {
283 D3DFMT_A8R8G8B8,
284 D3DFMT_X8R8G8B8,
285 D3DFMT_R5G6B5,
286 D3DFMT_D24S8
287 };
288
289 int max = 0;
290 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
291 {
292 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
293 mDisplay->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
294 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
295
296 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
297 {
298 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
299 {
300 max = j;
301 }
302 }
303 }
304
305 mMaxSupportedSamples = max;
306
307 mSupportsEventQueries = mDisplay->getEventQuerySupport();
308 mSupportsOcclusionQueries = mDisplay->getOcclusionQuerySupport();
309 mSupportsDXT1Textures = mDisplay->getDXT1TextureSupport();
310 mSupportsDXT3Textures = mDisplay->getDXT3TextureSupport();
311 mSupportsDXT5Textures = mDisplay->getDXT5TextureSupport();
312 mSupportsFloat32Textures = mDisplay->getFloat32TextureSupport(&mSupportsFloat32LinearFilter, &mSupportsFloat32RenderableTextures);
313 mSupportsFloat16Textures = mDisplay->getFloat16TextureSupport(&mSupportsFloat16LinearFilter, &mSupportsFloat16RenderableTextures);
314 mSupportsLuminanceTextures = mDisplay->getLuminanceTextureSupport();
315 mSupportsLuminanceAlphaTextures = mDisplay->getLuminanceAlphaTextureSupport();
316 mSupportsDepthTextures = mDisplay->getDepthTextureSupport();
daniel@transgaming.com07ab8412012-07-12 15:17:09 +0000317 mSupportsTextureFilterAnisotropy = mMaxTextureAnisotropy >= 2.0f;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000318
319 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
320
321 mNumCompressedTextureFormats = 0;
322 if (supportsDXT1Textures())
323 {
324 mNumCompressedTextureFormats += 2;
325 }
326 if (supportsDXT3Textures())
327 {
328 mNumCompressedTextureFormats += 1;
329 }
330 if (supportsDXT5Textures())
331 {
332 mNumCompressedTextureFormats += 1;
333 }
334
335 initExtensionString();
336 initRendererString();
337
338 mState.viewportX = 0;
339 mState.viewportY = 0;
340 mState.viewportWidth = surface->getWidth();
341 mState.viewportHeight = surface->getHeight();
342
343 mState.scissorX = 0;
344 mState.scissorY = 0;
345 mState.scissorWidth = surface->getWidth();
346 mState.scissorHeight = surface->getHeight();
347
348 mHasBeenCurrent = true;
349 }
350
351 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
352 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
353 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
354
355 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
356 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
357 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
358
359 setFramebufferZero(framebufferZero);
360
361 if (defaultRenderTarget)
362 {
363 defaultRenderTarget->Release();
364 }
365
366 if (depthStencil)
367 {
368 depthStencil->Release();
369 }
370
371 markAllStateDirty();
372}
373
374// This function will set all of the state-related dirty flags, so that all state is set during next pre-draw.
375void Context::markAllStateDirty()
376{
377 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
378 {
379 mAppliedTextureSerialPS[t] = 0;
380 }
381
382 for (int t = 0; t < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; t++)
383 {
384 mAppliedTextureSerialVS[t] = 0;
385 }
386
daniel@transgaming.come6af4f92012-07-24 18:31:31 +0000387 mAppliedProgramBinarySerial = 0;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000388 mAppliedRenderTargetSerial = 0;
389 mAppliedDepthbufferSerial = 0;
390 mAppliedStencilbufferSerial = 0;
391 mAppliedIBSerial = 0;
392 mDepthStencilInitialized = false;
393 mViewportInitialized = false;
394 mRenderTargetDescInitialized = false;
395
396 mVertexDeclarationCache.markStateDirty();
397
398 mClearStateDirty = true;
399 mCullStateDirty = true;
400 mDepthStateDirty = true;
401 mMaskStateDirty = true;
402 mBlendStateDirty = true;
403 mStencilStateDirty = true;
404 mPolygonOffsetStateDirty = true;
405 mScissorStateDirty = true;
406 mSampleStateDirty = true;
407 mDitherStateDirty = true;
408 mFrontFaceDirty = true;
409 mDxUniformsDirty = true;
daniel@transgaming.com62a28462012-07-24 18:33:59 +0000410 mCachedCurrentProgramBinary = NULL;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000411}
412
413void Context::markDxUniformsDirty()
414{
415 mDxUniformsDirty = true;
416}
417
418void Context::markContextLost()
419{
420 if (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT)
421 mResetStatus = GL_UNKNOWN_CONTEXT_RESET_EXT;
422 mContextLost = true;
423}
424
425bool Context::isContextLost()
426{
427 return mContextLost;
428}
429
430void Context::setClearColor(float red, float green, float blue, float alpha)
431{
432 mState.colorClearValue.red = red;
433 mState.colorClearValue.green = green;
434 mState.colorClearValue.blue = blue;
435 mState.colorClearValue.alpha = alpha;
436}
437
438void Context::setClearDepth(float depth)
439{
440 mState.depthClearValue = depth;
441}
442
443void Context::setClearStencil(int stencil)
444{
445 mState.stencilClearValue = stencil;
446}
447
448void Context::setCullFace(bool enabled)
449{
450 if (mState.cullFace != enabled)
451 {
452 mState.cullFace = enabled;
453 mCullStateDirty = true;
454 }
455}
456
457bool Context::isCullFaceEnabled() const
458{
459 return mState.cullFace;
460}
461
462void Context::setCullMode(GLenum mode)
463{
464 if (mState.cullMode != mode)
465 {
466 mState.cullMode = mode;
467 mCullStateDirty = true;
468 }
469}
470
471void Context::setFrontFace(GLenum front)
472{
473 if (mState.frontFace != front)
474 {
475 mState.frontFace = front;
476 mFrontFaceDirty = true;
477 }
478}
479
480void Context::setDepthTest(bool enabled)
481{
482 if (mState.depthTest != enabled)
483 {
484 mState.depthTest = enabled;
485 mDepthStateDirty = true;
486 }
487}
488
489bool Context::isDepthTestEnabled() const
490{
491 return mState.depthTest;
492}
493
494void Context::setDepthFunc(GLenum depthFunc)
495{
496 if (mState.depthFunc != depthFunc)
497 {
498 mState.depthFunc = depthFunc;
499 mDepthStateDirty = true;
500 }
501}
502
503void Context::setDepthRange(float zNear, float zFar)
504{
505 mState.zNear = zNear;
506 mState.zFar = zFar;
507}
508
509void Context::setBlend(bool enabled)
510{
511 if (mState.blend != enabled)
512 {
513 mState.blend = enabled;
514 mBlendStateDirty = true;
515 }
516}
517
518bool Context::isBlendEnabled() const
519{
520 return mState.blend;
521}
522
523void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
524{
525 if (mState.sourceBlendRGB != sourceRGB ||
526 mState.sourceBlendAlpha != sourceAlpha ||
527 mState.destBlendRGB != destRGB ||
528 mState.destBlendAlpha != destAlpha)
529 {
530 mState.sourceBlendRGB = sourceRGB;
531 mState.destBlendRGB = destRGB;
532 mState.sourceBlendAlpha = sourceAlpha;
533 mState.destBlendAlpha = destAlpha;
534 mBlendStateDirty = true;
535 }
536}
537
538void Context::setBlendColor(float red, float green, float blue, float alpha)
539{
540 if (mState.blendColor.red != red ||
541 mState.blendColor.green != green ||
542 mState.blendColor.blue != blue ||
543 mState.blendColor.alpha != alpha)
544 {
545 mState.blendColor.red = red;
546 mState.blendColor.green = green;
547 mState.blendColor.blue = blue;
548 mState.blendColor.alpha = alpha;
549 mBlendStateDirty = true;
550 }
551}
552
553void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
554{
555 if (mState.blendEquationRGB != rgbEquation ||
556 mState.blendEquationAlpha != alphaEquation)
557 {
558 mState.blendEquationRGB = rgbEquation;
559 mState.blendEquationAlpha = alphaEquation;
560 mBlendStateDirty = true;
561 }
562}
563
564void Context::setStencilTest(bool enabled)
565{
566 if (mState.stencilTest != enabled)
567 {
568 mState.stencilTest = enabled;
569 mStencilStateDirty = true;
570 }
571}
572
573bool Context::isStencilTestEnabled() const
574{
575 return mState.stencilTest;
576}
577
578void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
579{
580 if (mState.stencilFunc != stencilFunc ||
581 mState.stencilRef != stencilRef ||
582 mState.stencilMask != stencilMask)
583 {
584 mState.stencilFunc = stencilFunc;
585 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
586 mState.stencilMask = stencilMask;
587 mStencilStateDirty = true;
588 }
589}
590
591void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
592{
593 if (mState.stencilBackFunc != stencilBackFunc ||
594 mState.stencilBackRef != stencilBackRef ||
595 mState.stencilBackMask != stencilBackMask)
596 {
597 mState.stencilBackFunc = stencilBackFunc;
598 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
599 mState.stencilBackMask = stencilBackMask;
600 mStencilStateDirty = true;
601 }
602}
603
604void Context::setStencilWritemask(GLuint stencilWritemask)
605{
606 if (mState.stencilWritemask != stencilWritemask)
607 {
608 mState.stencilWritemask = stencilWritemask;
609 mStencilStateDirty = true;
610 }
611}
612
613void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
614{
615 if (mState.stencilBackWritemask != stencilBackWritemask)
616 {
617 mState.stencilBackWritemask = stencilBackWritemask;
618 mStencilStateDirty = true;
619 }
620}
621
622void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
623{
624 if (mState.stencilFail != stencilFail ||
625 mState.stencilPassDepthFail != stencilPassDepthFail ||
626 mState.stencilPassDepthPass != stencilPassDepthPass)
627 {
628 mState.stencilFail = stencilFail;
629 mState.stencilPassDepthFail = stencilPassDepthFail;
630 mState.stencilPassDepthPass = stencilPassDepthPass;
631 mStencilStateDirty = true;
632 }
633}
634
635void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
636{
637 if (mState.stencilBackFail != stencilBackFail ||
638 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
639 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
640 {
641 mState.stencilBackFail = stencilBackFail;
642 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
643 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
644 mStencilStateDirty = true;
645 }
646}
647
648void Context::setPolygonOffsetFill(bool enabled)
649{
650 if (mState.polygonOffsetFill != enabled)
651 {
652 mState.polygonOffsetFill = enabled;
653 mPolygonOffsetStateDirty = true;
654 }
655}
656
657bool Context::isPolygonOffsetFillEnabled() const
658{
659 return mState.polygonOffsetFill;
660
661}
662
663void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
664{
665 if (mState.polygonOffsetFactor != factor ||
666 mState.polygonOffsetUnits != units)
667 {
668 mState.polygonOffsetFactor = factor;
669 mState.polygonOffsetUnits = units;
670 mPolygonOffsetStateDirty = true;
671 }
672}
673
674void Context::setSampleAlphaToCoverage(bool enabled)
675{
676 if (mState.sampleAlphaToCoverage != enabled)
677 {
678 mState.sampleAlphaToCoverage = enabled;
679 mSampleStateDirty = true;
680 }
681}
682
683bool Context::isSampleAlphaToCoverageEnabled() const
684{
685 return mState.sampleAlphaToCoverage;
686}
687
688void Context::setSampleCoverage(bool enabled)
689{
690 if (mState.sampleCoverage != enabled)
691 {
692 mState.sampleCoverage = enabled;
693 mSampleStateDirty = true;
694 }
695}
696
697bool Context::isSampleCoverageEnabled() const
698{
699 return mState.sampleCoverage;
700}
701
702void Context::setSampleCoverageParams(GLclampf value, bool invert)
703{
704 if (mState.sampleCoverageValue != value ||
705 mState.sampleCoverageInvert != invert)
706 {
707 mState.sampleCoverageValue = value;
708 mState.sampleCoverageInvert = invert;
709 mSampleStateDirty = true;
710 }
711}
712
713void Context::setScissorTest(bool enabled)
714{
715 if (mState.scissorTest != enabled)
716 {
717 mState.scissorTest = enabled;
718 mScissorStateDirty = true;
719 }
720}
721
722bool Context::isScissorTestEnabled() const
723{
724 return mState.scissorTest;
725}
726
727void Context::setDither(bool enabled)
728{
729 if (mState.dither != enabled)
730 {
731 mState.dither = enabled;
732 mDitherStateDirty = true;
733 }
734}
735
736bool Context::isDitherEnabled() const
737{
738 return mState.dither;
739}
740
741void Context::setLineWidth(GLfloat width)
742{
743 mState.lineWidth = width;
744}
745
746void Context::setGenerateMipmapHint(GLenum hint)
747{
748 mState.generateMipmapHint = hint;
749}
750
751void Context::setFragmentShaderDerivativeHint(GLenum hint)
752{
753 mState.fragmentShaderDerivativeHint = hint;
754 // TODO: Propagate the hint to shader translator so we can write
755 // ddx, ddx_coarse, or ddx_fine depending on the hint.
756 // Ignore for now. It is valid for implementations to ignore hint.
757}
758
759void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
760{
761 mState.viewportX = x;
762 mState.viewportY = y;
763 mState.viewportWidth = width;
764 mState.viewportHeight = height;
765}
766
767void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
768{
769 if (mState.scissorX != x || mState.scissorY != y ||
770 mState.scissorWidth != width || mState.scissorHeight != height)
771 {
772 mState.scissorX = x;
773 mState.scissorY = y;
774 mState.scissorWidth = width;
775 mState.scissorHeight = height;
776 mScissorStateDirty = true;
777 }
778}
779
780void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
781{
782 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
783 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
784 {
785 mState.colorMaskRed = red;
786 mState.colorMaskGreen = green;
787 mState.colorMaskBlue = blue;
788 mState.colorMaskAlpha = alpha;
789 mMaskStateDirty = true;
790 }
791}
792
793void Context::setDepthMask(bool mask)
794{
795 if (mState.depthMask != mask)
796 {
797 mState.depthMask = mask;
798 mMaskStateDirty = true;
799 }
800}
801
802void Context::setActiveSampler(unsigned int active)
803{
804 mState.activeSampler = active;
805}
806
807GLuint Context::getReadFramebufferHandle() const
808{
809 return mState.readFramebuffer;
810}
811
812GLuint Context::getDrawFramebufferHandle() const
813{
814 return mState.drawFramebuffer;
815}
816
817GLuint Context::getRenderbufferHandle() const
818{
819 return mState.renderbuffer.id();
820}
821
822GLuint Context::getArrayBufferHandle() const
823{
824 return mState.arrayBuffer.id();
825}
826
827GLuint Context::getActiveQuery(GLenum target) const
828{
829 Query *queryObject = NULL;
830
831 switch (target)
832 {
833 case GL_ANY_SAMPLES_PASSED_EXT:
834 queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED].get();
835 break;
836 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
837 queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE].get();
838 break;
839 default:
840 ASSERT(false);
841 }
842
843 if (queryObject)
844 {
845 return queryObject->id();
846 }
847 else
848 {
849 return 0;
850 }
851}
852
853void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
854{
855 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
856}
857
858const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
859{
860 return mState.vertexAttribute[attribNum];
861}
862
863void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
864 GLsizei stride, const void *pointer)
865{
866 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
867 mState.vertexAttribute[attribNum].mSize = size;
868 mState.vertexAttribute[attribNum].mType = type;
869 mState.vertexAttribute[attribNum].mNormalized = normalized;
870 mState.vertexAttribute[attribNum].mStride = stride;
871 mState.vertexAttribute[attribNum].mPointer = pointer;
872}
873
874const void *Context::getVertexAttribPointer(unsigned int attribNum) const
875{
876 return mState.vertexAttribute[attribNum].mPointer;
877}
878
879const VertexAttributeArray &Context::getVertexAttributes()
880{
881 return mState.vertexAttribute;
882}
883
884void Context::setPackAlignment(GLint alignment)
885{
886 mState.packAlignment = alignment;
887}
888
889GLint Context::getPackAlignment() const
890{
891 return mState.packAlignment;
892}
893
894void Context::setUnpackAlignment(GLint alignment)
895{
896 mState.unpackAlignment = alignment;
897}
898
899GLint Context::getUnpackAlignment() const
900{
901 return mState.unpackAlignment;
902}
903
904void Context::setPackReverseRowOrder(bool reverseRowOrder)
905{
906 mState.packReverseRowOrder = reverseRowOrder;
907}
908
909bool Context::getPackReverseRowOrder() const
910{
911 return mState.packReverseRowOrder;
912}
913
914GLuint Context::createBuffer()
915{
916 return mResourceManager->createBuffer();
917}
918
919GLuint Context::createProgram()
920{
921 return mResourceManager->createProgram();
922}
923
924GLuint Context::createShader(GLenum type)
925{
926 return mResourceManager->createShader(type);
927}
928
929GLuint Context::createTexture()
930{
931 return mResourceManager->createTexture();
932}
933
934GLuint Context::createRenderbuffer()
935{
936 return mResourceManager->createRenderbuffer();
937}
938
939// Returns an unused framebuffer name
940GLuint Context::createFramebuffer()
941{
942 GLuint handle = mFramebufferHandleAllocator.allocate();
943
944 mFramebufferMap[handle] = NULL;
945
946 return handle;
947}
948
949GLuint Context::createFence()
950{
951 GLuint handle = mFenceHandleAllocator.allocate();
952
953 mFenceMap[handle] = new Fence(mDisplay);
954
955 return handle;
956}
957
958// Returns an unused query name
959GLuint Context::createQuery()
960{
961 GLuint handle = mQueryHandleAllocator.allocate();
962
963 mQueryMap[handle] = NULL;
964
965 return handle;
966}
967
968void Context::deleteBuffer(GLuint buffer)
969{
970 if (mResourceManager->getBuffer(buffer))
971 {
972 detachBuffer(buffer);
973 }
974
975 mResourceManager->deleteBuffer(buffer);
976}
977
978void Context::deleteShader(GLuint shader)
979{
980 mResourceManager->deleteShader(shader);
981}
982
983void Context::deleteProgram(GLuint program)
984{
985 mResourceManager->deleteProgram(program);
daniel@transgaming.com62a28462012-07-24 18:33:59 +0000986 mCachedCurrentProgramBinary = NULL;
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000987}
988
989void Context::deleteTexture(GLuint texture)
990{
991 if (mResourceManager->getTexture(texture))
992 {
993 detachTexture(texture);
994 }
995
996 mResourceManager->deleteTexture(texture);
997}
998
999void Context::deleteRenderbuffer(GLuint renderbuffer)
1000{
1001 if (mResourceManager->getRenderbuffer(renderbuffer))
1002 {
1003 detachRenderbuffer(renderbuffer);
1004 }
1005
1006 mResourceManager->deleteRenderbuffer(renderbuffer);
1007}
1008
1009void Context::deleteFramebuffer(GLuint framebuffer)
1010{
1011 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
1012
1013 if (framebufferObject != mFramebufferMap.end())
1014 {
1015 detachFramebuffer(framebuffer);
1016
1017 mFramebufferHandleAllocator.release(framebufferObject->first);
1018 delete framebufferObject->second;
1019 mFramebufferMap.erase(framebufferObject);
1020 }
1021}
1022
1023void Context::deleteFence(GLuint fence)
1024{
1025 FenceMap::iterator fenceObject = mFenceMap.find(fence);
1026
1027 if (fenceObject != mFenceMap.end())
1028 {
1029 mFenceHandleAllocator.release(fenceObject->first);
1030 delete fenceObject->second;
1031 mFenceMap.erase(fenceObject);
1032 }
1033}
1034
1035void Context::deleteQuery(GLuint query)
1036{
1037 QueryMap::iterator queryObject = mQueryMap.find(query);
1038 if (queryObject != mQueryMap.end())
1039 {
1040 mQueryHandleAllocator.release(queryObject->first);
1041 if (queryObject->second)
1042 {
1043 queryObject->second->release();
1044 }
1045 mQueryMap.erase(queryObject);
1046 }
1047}
1048
1049Buffer *Context::getBuffer(GLuint handle)
1050{
1051 return mResourceManager->getBuffer(handle);
1052}
1053
1054Shader *Context::getShader(GLuint handle)
1055{
1056 return mResourceManager->getShader(handle);
1057}
1058
1059Program *Context::getProgram(GLuint handle)
1060{
1061 return mResourceManager->getProgram(handle);
1062}
1063
1064Texture *Context::getTexture(GLuint handle)
1065{
1066 return mResourceManager->getTexture(handle);
1067}
1068
1069Renderbuffer *Context::getRenderbuffer(GLuint handle)
1070{
1071 return mResourceManager->getRenderbuffer(handle);
1072}
1073
1074Framebuffer *Context::getReadFramebuffer()
1075{
1076 return getFramebuffer(mState.readFramebuffer);
1077}
1078
1079Framebuffer *Context::getDrawFramebuffer()
1080{
1081 return mBoundDrawFramebuffer;
1082}
1083
1084void Context::bindArrayBuffer(unsigned int buffer)
1085{
1086 mResourceManager->checkBufferAllocation(buffer);
1087
1088 mState.arrayBuffer.set(getBuffer(buffer));
1089}
1090
1091void Context::bindElementArrayBuffer(unsigned int buffer)
1092{
1093 mResourceManager->checkBufferAllocation(buffer);
1094
1095 mState.elementArrayBuffer.set(getBuffer(buffer));
1096}
1097
1098void Context::bindTexture2D(GLuint texture)
1099{
1100 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
1101
1102 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
1103}
1104
1105void Context::bindTextureCubeMap(GLuint texture)
1106{
1107 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
1108
1109 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
1110}
1111
1112void Context::bindReadFramebuffer(GLuint framebuffer)
1113{
1114 if (!getFramebuffer(framebuffer))
1115 {
1116 mFramebufferMap[framebuffer] = new Framebuffer();
1117 }
1118
1119 mState.readFramebuffer = framebuffer;
1120}
1121
1122void Context::bindDrawFramebuffer(GLuint framebuffer)
1123{
1124 if (!getFramebuffer(framebuffer))
1125 {
1126 mFramebufferMap[framebuffer] = new Framebuffer();
1127 }
1128
1129 mState.drawFramebuffer = framebuffer;
1130
1131 mBoundDrawFramebuffer = getFramebuffer(framebuffer);
1132}
1133
1134void Context::bindRenderbuffer(GLuint renderbuffer)
1135{
1136 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1137
1138 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
1139}
1140
1141void Context::useProgram(GLuint program)
1142{
1143 GLuint priorProgram = mState.currentProgram;
1144 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
1145
1146 if (priorProgram != program)
1147 {
1148 Program *newProgram = mResourceManager->getProgram(program);
1149 Program *oldProgram = mResourceManager->getProgram(priorProgram);
daniel@transgaming.com62a28462012-07-24 18:33:59 +00001150 mCachedCurrentProgramBinary = NULL;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001151 mDxUniformsDirty = true;
1152
1153 if (newProgram)
1154 {
1155 newProgram->addRef();
1156 }
1157
1158 if (oldProgram)
1159 {
1160 oldProgram->release();
1161 }
1162 }
1163}
1164
1165void Context::beginQuery(GLenum target, GLuint query)
1166{
1167 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
1168 // of zero, if the active query object name for <target> is non-zero (for the
1169 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
1170 // the active query for either target is non-zero), if <id> is the name of an
1171 // existing query object whose type does not match <target>, or if <id> is the
1172 // active query object name for any query type, the error INVALID_OPERATION is
1173 // generated.
1174
1175 // Ensure no other queries are active
1176 // NOTE: If other queries than occlusion are supported, we will need to check
1177 // separately that:
1178 // a) The query ID passed is not the current active query for any target/type
1179 // b) There are no active queries for the requested target (and in the case
1180 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
1181 // no query may be active for either if glBeginQuery targets either.
1182 for (int i = 0; i < QUERY_TYPE_COUNT; i++)
1183 {
1184 if (mState.activeQuery[i].get() != NULL)
1185 {
1186 return error(GL_INVALID_OPERATION);
1187 }
1188 }
1189
1190 QueryType qType;
1191 switch (target)
1192 {
1193 case GL_ANY_SAMPLES_PASSED_EXT:
1194 qType = QUERY_ANY_SAMPLES_PASSED;
1195 break;
1196 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
1197 qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
1198 break;
1199 default:
1200 ASSERT(false);
1201 return;
1202 }
1203
1204 Query *queryObject = getQuery(query, true, target);
1205
1206 // check that name was obtained with glGenQueries
1207 if (!queryObject)
1208 {
1209 return error(GL_INVALID_OPERATION);
1210 }
1211
1212 // check for type mismatch
1213 if (queryObject->getType() != target)
1214 {
1215 return error(GL_INVALID_OPERATION);
1216 }
1217
1218 // set query as active for specified target
1219 mState.activeQuery[qType].set(queryObject);
1220
1221 // begin query
1222 queryObject->begin();
1223}
1224
1225void Context::endQuery(GLenum target)
1226{
1227 QueryType qType;
1228
1229 switch (target)
1230 {
1231 case GL_ANY_SAMPLES_PASSED_EXT:
1232 qType = QUERY_ANY_SAMPLES_PASSED;
1233 break;
1234 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
1235 qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
1236 break;
1237 default:
1238 ASSERT(false);
1239 return;
1240 }
1241
1242 Query *queryObject = mState.activeQuery[qType].get();
1243
1244 if (queryObject == NULL)
1245 {
1246 return error(GL_INVALID_OPERATION);
1247 }
1248
1249 queryObject->end();
1250
1251 mState.activeQuery[qType].set(NULL);
1252}
1253
1254void Context::setFramebufferZero(Framebuffer *buffer)
1255{
1256 delete mFramebufferMap[0];
1257 mFramebufferMap[0] = buffer;
1258 if (mState.drawFramebuffer == 0)
1259 {
1260 mBoundDrawFramebuffer = buffer;
1261 }
1262}
1263
1264void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
1265{
1266 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1267 renderbufferObject->setStorage(renderbuffer);
1268}
1269
1270Framebuffer *Context::getFramebuffer(unsigned int handle)
1271{
1272 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1273
1274 if (framebuffer == mFramebufferMap.end())
1275 {
1276 return NULL;
1277 }
1278 else
1279 {
1280 return framebuffer->second;
1281 }
1282}
1283
1284Fence *Context::getFence(unsigned int handle)
1285{
1286 FenceMap::iterator fence = mFenceMap.find(handle);
1287
1288 if (fence == mFenceMap.end())
1289 {
1290 return NULL;
1291 }
1292 else
1293 {
1294 return fence->second;
1295 }
1296}
1297
1298Query *Context::getQuery(unsigned int handle, bool create, GLenum type)
1299{
1300 QueryMap::iterator query = mQueryMap.find(handle);
1301
1302 if (query == mQueryMap.end())
1303 {
1304 return NULL;
1305 }
1306 else
1307 {
1308 if (!query->second && create)
1309 {
1310 query->second = new Query(handle, type);
1311 query->second->addRef();
1312 }
1313 return query->second;
1314 }
1315}
1316
1317Buffer *Context::getArrayBuffer()
1318{
1319 return mState.arrayBuffer.get();
1320}
1321
1322Buffer *Context::getElementArrayBuffer()
1323{
1324 return mState.elementArrayBuffer.get();
1325}
1326
daniel@transgaming.com62a28462012-07-24 18:33:59 +00001327ProgramBinary *Context::getCurrentProgramBinary()
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001328{
daniel@transgaming.com62a28462012-07-24 18:33:59 +00001329 if (!mCachedCurrentProgramBinary)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001330 {
daniel@transgaming.com62a28462012-07-24 18:33:59 +00001331 Program *program = mResourceManager->getProgram(mState.currentProgram);
1332 mCachedCurrentProgramBinary = program->getProgramBinary();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001333 }
daniel@transgaming.com62a28462012-07-24 18:33:59 +00001334 return mCachedCurrentProgramBinary;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001335}
1336
1337Texture2D *Context::getTexture2D()
1338{
1339 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
1340}
1341
1342TextureCubeMap *Context::getTextureCubeMap()
1343{
1344 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
1345}
1346
1347Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
1348{
1349 GLuint texid = mState.samplerTexture[type][sampler].id();
1350
1351 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
1352 {
1353 switch (type)
1354 {
1355 default: UNREACHABLE();
1356 case TEXTURE_2D: return mTexture2DZero.get();
1357 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
1358 }
1359 }
1360
1361 return mState.samplerTexture[type][sampler].get();
1362}
1363
1364bool Context::getBooleanv(GLenum pname, GLboolean *params)
1365{
1366 switch (pname)
1367 {
1368 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1369 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1370 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
1371 case GL_COLOR_WRITEMASK:
1372 params[0] = mState.colorMaskRed;
1373 params[1] = mState.colorMaskGreen;
1374 params[2] = mState.colorMaskBlue;
1375 params[3] = mState.colorMaskAlpha;
1376 break;
1377 case GL_CULL_FACE: *params = mState.cullFace; break;
1378 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1379 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1380 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1381 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1382 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1383 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1384 case GL_BLEND: *params = mState.blend; break;
1385 case GL_DITHER: *params = mState.dither; break;
1386 case GL_CONTEXT_ROBUST_ACCESS_EXT: *params = mRobustAccess ? GL_TRUE : GL_FALSE; break;
1387 default:
1388 return false;
1389 }
1390
1391 return true;
1392}
1393
1394bool Context::getFloatv(GLenum pname, GLfloat *params)
1395{
1396 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1397 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1398 // GetIntegerv as its native query function. As it would require conversion in any
1399 // case, this should make no difference to the calling application.
1400 switch (pname)
1401 {
1402 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1403 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1404 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1405 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1406 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
1407 case GL_ALIASED_LINE_WIDTH_RANGE:
1408 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1409 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1410 break;
1411 case GL_ALIASED_POINT_SIZE_RANGE:
1412 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
1413 params[1] = getMaximumPointSize();
1414 break;
1415 case GL_DEPTH_RANGE:
1416 params[0] = mState.zNear;
1417 params[1] = mState.zFar;
1418 break;
1419 case GL_COLOR_CLEAR_VALUE:
1420 params[0] = mState.colorClearValue.red;
1421 params[1] = mState.colorClearValue.green;
1422 params[2] = mState.colorClearValue.blue;
1423 params[3] = mState.colorClearValue.alpha;
1424 break;
1425 case GL_BLEND_COLOR:
1426 params[0] = mState.blendColor.red;
1427 params[1] = mState.blendColor.green;
1428 params[2] = mState.blendColor.blue;
1429 params[3] = mState.blendColor.alpha;
1430 break;
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00001431 case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
1432 if (!supportsTextureFilterAnisotropy())
1433 {
1434 return false;
1435 }
1436 *params = mMaxTextureAnisotropy;
1437 break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001438 default:
1439 return false;
1440 }
1441
1442 return true;
1443}
1444
1445bool Context::getIntegerv(GLenum pname, GLint *params)
1446{
1447 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1448 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1449 // GetIntegerv as its native query function. As it would require conversion in any
1450 // case, this should make no difference to the calling application. You may find it in
1451 // Context::getFloatv.
1452 switch (pname)
1453 {
1454 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1455 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
1456 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
1457 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1458 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
1459 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
1460 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
1461 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
1462 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
1463 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
1464 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1465 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
1466 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1467 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1468 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
1469 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
1470 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1471 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1472 case GL_PACK_REVERSE_ROW_ORDER_ANGLE: *params = mState.packReverseRowOrder; break;
1473 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1474 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
1475 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
1476 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1477 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1478 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1479 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1480 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1481 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1482 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1483 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1484 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1485 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1486 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1487 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1488 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1489 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1490 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1491 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1492 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1493 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1494 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1495 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1496 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1497 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1498 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1499 case GL_SUBPIXEL_BITS: *params = 4; break;
1500 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1501 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
1502 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1503 params[0] = mNumCompressedTextureFormats;
1504 break;
1505 case GL_MAX_SAMPLES_ANGLE:
1506 {
1507 GLsizei maxSamples = getMaxSupportedSamples();
1508 if (maxSamples != 0)
1509 {
1510 *params = maxSamples;
1511 }
1512 else
1513 {
1514 return false;
1515 }
1516
1517 break;
1518 }
1519 case GL_SAMPLE_BUFFERS:
1520 case GL_SAMPLES:
1521 {
1522 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1523 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1524 {
1525 switch (pname)
1526 {
1527 case GL_SAMPLE_BUFFERS:
1528 if (framebuffer->getSamples() != 0)
1529 {
1530 *params = 1;
1531 }
1532 else
1533 {
1534 *params = 0;
1535 }
1536 break;
1537 case GL_SAMPLES:
1538 *params = framebuffer->getSamples();
1539 break;
1540 }
1541 }
1542 else
1543 {
1544 *params = 0;
1545 }
1546 }
1547 break;
1548 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1549 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1550 case GL_MAX_VIEWPORT_DIMS:
1551 {
1552 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
1553 params[0] = maxDimension;
1554 params[1] = maxDimension;
1555 }
1556 break;
1557 case GL_COMPRESSED_TEXTURE_FORMATS:
1558 {
1559 if (supportsDXT1Textures())
1560 {
1561 *params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1562 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1563 }
1564 if (supportsDXT3Textures())
1565 {
1566 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
1567 }
1568 if (supportsDXT5Textures())
1569 {
1570 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
1571 }
1572 }
1573 break;
1574 case GL_VIEWPORT:
1575 params[0] = mState.viewportX;
1576 params[1] = mState.viewportY;
1577 params[2] = mState.viewportWidth;
1578 params[3] = mState.viewportHeight;
1579 break;
1580 case GL_SCISSOR_BOX:
1581 params[0] = mState.scissorX;
1582 params[1] = mState.scissorY;
1583 params[2] = mState.scissorWidth;
1584 params[3] = mState.scissorHeight;
1585 break;
1586 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1587 case GL_FRONT_FACE: *params = mState.frontFace; break;
1588 case GL_RED_BITS:
1589 case GL_GREEN_BITS:
1590 case GL_BLUE_BITS:
1591 case GL_ALPHA_BITS:
1592 {
1593 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1594 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
1595
1596 if (colorbuffer)
1597 {
1598 switch (pname)
1599 {
1600 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1601 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1602 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1603 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1604 }
1605 }
1606 else
1607 {
1608 *params = 0;
1609 }
1610 }
1611 break;
1612 case GL_DEPTH_BITS:
1613 {
1614 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1615 gl::Renderbuffer *depthbuffer = framebuffer->getDepthbuffer();
1616
1617 if (depthbuffer)
1618 {
1619 *params = depthbuffer->getDepthSize();
1620 }
1621 else
1622 {
1623 *params = 0;
1624 }
1625 }
1626 break;
1627 case GL_STENCIL_BITS:
1628 {
1629 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1630 gl::Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer();
1631
1632 if (stencilbuffer)
1633 {
1634 *params = stencilbuffer->getStencilSize();
1635 }
1636 else
1637 {
1638 *params = 0;
1639 }
1640 }
1641 break;
1642 case GL_TEXTURE_BINDING_2D:
1643 {
1644 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
1645 {
1646 error(GL_INVALID_OPERATION);
1647 return false;
1648 }
1649
1650 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
1651 }
1652 break;
1653 case GL_TEXTURE_BINDING_CUBE_MAP:
1654 {
1655 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
1656 {
1657 error(GL_INVALID_OPERATION);
1658 return false;
1659 }
1660
1661 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
1662 }
1663 break;
1664 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
1665 *params = mResetStrategy;
1666 break;
1667 case GL_NUM_PROGRAM_BINARY_FORMATS_OES:
1668 *params = 1;
1669 break;
1670 case GL_PROGRAM_BINARY_FORMATS_OES:
1671 *params = GL_PROGRAM_BINARY_ANGLE;
1672 break;
1673 default:
1674 return false;
1675 }
1676
1677 return true;
1678}
1679
1680bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1681{
1682 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1683 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1684 // to the fact that it is stored internally as a float, and so would require conversion
1685 // if returned from Context::getIntegerv. Since this conversion is already implemented
1686 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1687 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1688 // application.
1689 switch (pname)
1690 {
1691 case GL_COMPRESSED_TEXTURE_FORMATS:
1692 {
1693 *type = GL_INT;
1694 *numParams = mNumCompressedTextureFormats;
1695 }
1696 break;
1697 case GL_SHADER_BINARY_FORMATS:
1698 {
1699 *type = GL_INT;
1700 *numParams = 0;
1701 }
1702 break;
1703 case GL_MAX_VERTEX_ATTRIBS:
1704 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1705 case GL_MAX_VARYING_VECTORS:
1706 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1707 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1708 case GL_MAX_TEXTURE_IMAGE_UNITS:
1709 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1710 case GL_MAX_RENDERBUFFER_SIZE:
1711 case GL_NUM_SHADER_BINARY_FORMATS:
1712 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1713 case GL_ARRAY_BUFFER_BINDING:
1714 case GL_FRAMEBUFFER_BINDING:
1715 case GL_RENDERBUFFER_BINDING:
1716 case GL_CURRENT_PROGRAM:
1717 case GL_PACK_ALIGNMENT:
1718 case GL_PACK_REVERSE_ROW_ORDER_ANGLE:
1719 case GL_UNPACK_ALIGNMENT:
1720 case GL_GENERATE_MIPMAP_HINT:
1721 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
1722 case GL_RED_BITS:
1723 case GL_GREEN_BITS:
1724 case GL_BLUE_BITS:
1725 case GL_ALPHA_BITS:
1726 case GL_DEPTH_BITS:
1727 case GL_STENCIL_BITS:
1728 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1729 case GL_CULL_FACE_MODE:
1730 case GL_FRONT_FACE:
1731 case GL_ACTIVE_TEXTURE:
1732 case GL_STENCIL_FUNC:
1733 case GL_STENCIL_VALUE_MASK:
1734 case GL_STENCIL_REF:
1735 case GL_STENCIL_FAIL:
1736 case GL_STENCIL_PASS_DEPTH_FAIL:
1737 case GL_STENCIL_PASS_DEPTH_PASS:
1738 case GL_STENCIL_BACK_FUNC:
1739 case GL_STENCIL_BACK_VALUE_MASK:
1740 case GL_STENCIL_BACK_REF:
1741 case GL_STENCIL_BACK_FAIL:
1742 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1743 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1744 case GL_DEPTH_FUNC:
1745 case GL_BLEND_SRC_RGB:
1746 case GL_BLEND_SRC_ALPHA:
1747 case GL_BLEND_DST_RGB:
1748 case GL_BLEND_DST_ALPHA:
1749 case GL_BLEND_EQUATION_RGB:
1750 case GL_BLEND_EQUATION_ALPHA:
1751 case GL_STENCIL_WRITEMASK:
1752 case GL_STENCIL_BACK_WRITEMASK:
1753 case GL_STENCIL_CLEAR_VALUE:
1754 case GL_SUBPIXEL_BITS:
1755 case GL_MAX_TEXTURE_SIZE:
1756 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1757 case GL_SAMPLE_BUFFERS:
1758 case GL_SAMPLES:
1759 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1760 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1761 case GL_TEXTURE_BINDING_2D:
1762 case GL_TEXTURE_BINDING_CUBE_MAP:
1763 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
1764 case GL_NUM_PROGRAM_BINARY_FORMATS_OES:
1765 case GL_PROGRAM_BINARY_FORMATS_OES:
1766 {
1767 *type = GL_INT;
1768 *numParams = 1;
1769 }
1770 break;
1771 case GL_MAX_SAMPLES_ANGLE:
1772 {
1773 if (getMaxSupportedSamples() != 0)
1774 {
1775 *type = GL_INT;
1776 *numParams = 1;
1777 }
1778 else
1779 {
1780 return false;
1781 }
1782 }
1783 break;
1784 case GL_MAX_VIEWPORT_DIMS:
1785 {
1786 *type = GL_INT;
1787 *numParams = 2;
1788 }
1789 break;
1790 case GL_VIEWPORT:
1791 case GL_SCISSOR_BOX:
1792 {
1793 *type = GL_INT;
1794 *numParams = 4;
1795 }
1796 break;
1797 case GL_SHADER_COMPILER:
1798 case GL_SAMPLE_COVERAGE_INVERT:
1799 case GL_DEPTH_WRITEMASK:
1800 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1801 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1802 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1803 case GL_SAMPLE_COVERAGE:
1804 case GL_SCISSOR_TEST:
1805 case GL_STENCIL_TEST:
1806 case GL_DEPTH_TEST:
1807 case GL_BLEND:
1808 case GL_DITHER:
1809 case GL_CONTEXT_ROBUST_ACCESS_EXT:
1810 {
1811 *type = GL_BOOL;
1812 *numParams = 1;
1813 }
1814 break;
1815 case GL_COLOR_WRITEMASK:
1816 {
1817 *type = GL_BOOL;
1818 *numParams = 4;
1819 }
1820 break;
1821 case GL_POLYGON_OFFSET_FACTOR:
1822 case GL_POLYGON_OFFSET_UNITS:
1823 case GL_SAMPLE_COVERAGE_VALUE:
1824 case GL_DEPTH_CLEAR_VALUE:
1825 case GL_LINE_WIDTH:
1826 {
1827 *type = GL_FLOAT;
1828 *numParams = 1;
1829 }
1830 break;
1831 case GL_ALIASED_LINE_WIDTH_RANGE:
1832 case GL_ALIASED_POINT_SIZE_RANGE:
1833 case GL_DEPTH_RANGE:
1834 {
1835 *type = GL_FLOAT;
1836 *numParams = 2;
1837 }
1838 break;
1839 case GL_COLOR_CLEAR_VALUE:
1840 case GL_BLEND_COLOR:
1841 {
1842 *type = GL_FLOAT;
1843 *numParams = 4;
1844 }
1845 break;
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00001846 case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
1847 if (!supportsTextureFilterAnisotropy())
1848 {
1849 return false;
1850 }
1851 *type = GL_FLOAT;
1852 *numParams = 1;
1853 break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001854 default:
1855 return false;
1856 }
1857
1858 return true;
1859}
1860
1861// Applies the render target surface, depth stencil surface, viewport rectangle and
1862// scissor rectangle to the Direct3D 9 device
1863bool Context::applyRenderTarget(bool ignoreViewport)
1864{
1865 Framebuffer *framebufferObject = getDrawFramebuffer();
1866
1867 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1868 {
1869 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
1870 }
1871
1872 // if there is no color attachment we must synthesize a NULL colorattachment
1873 // to keep the D3D runtime happy. This should only be possible if depth texturing.
1874 Renderbuffer *renderbufferObject = NULL;
1875 if (framebufferObject->getColorbufferType() != GL_NONE)
1876 {
1877 renderbufferObject = framebufferObject->getColorbuffer();
1878 }
1879 else
1880 {
1881 renderbufferObject = framebufferObject->getNullColorbuffer();
1882 }
1883 if (!renderbufferObject)
1884 {
1885 ERR("unable to locate renderbuffer for FBO.");
1886 return false;
1887 }
1888
1889 bool renderTargetChanged = false;
1890 unsigned int renderTargetSerial = renderbufferObject->getSerial();
1891 if (renderTargetSerial != mAppliedRenderTargetSerial)
1892 {
1893 IDirect3DSurface9 *renderTarget = renderbufferObject->getRenderTarget();
1894 if (!renderTarget)
1895 {
1896 ERR("render target pointer unexpectedly null.");
1897 return false; // Context must be lost
1898 }
1899 mDevice->SetRenderTarget(0, renderTarget);
1900 mAppliedRenderTargetSerial = renderTargetSerial;
1901 mScissorStateDirty = true; // Scissor area must be clamped to render target's size-- this is different for different render targets.
1902 renderTargetChanged = true;
1903 renderTarget->Release();
1904 }
1905
1906 IDirect3DSurface9 *depthStencil = NULL;
1907 unsigned int depthbufferSerial = 0;
1908 unsigned int stencilbufferSerial = 0;
1909 if (framebufferObject->getDepthbufferType() != GL_NONE)
1910 {
1911 Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
1912 depthStencil = depthbuffer->getDepthStencil();
1913 if (!depthStencil)
1914 {
1915 ERR("Depth stencil pointer unexpectedly null.");
1916 return false;
1917 }
1918
1919 depthbufferSerial = depthbuffer->getSerial();
1920 }
1921 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1922 {
1923 Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
1924 depthStencil = stencilbuffer->getDepthStencil();
1925 if (!depthStencil)
1926 {
1927 ERR("Depth stencil pointer unexpectedly null.");
1928 return false;
1929 }
1930
1931 stencilbufferSerial = stencilbuffer->getSerial();
1932 }
1933
1934 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1935 stencilbufferSerial != mAppliedStencilbufferSerial ||
1936 !mDepthStencilInitialized)
1937 {
1938 mDevice->SetDepthStencilSurface(depthStencil);
1939 mAppliedDepthbufferSerial = depthbufferSerial;
1940 mAppliedStencilbufferSerial = stencilbufferSerial;
1941 mDepthStencilInitialized = true;
1942 }
1943
1944 if (depthStencil)
1945 {
1946 depthStencil->Release();
1947 }
1948
1949 if (!mRenderTargetDescInitialized || renderTargetChanged)
1950 {
1951 IDirect3DSurface9 *renderTarget = renderbufferObject->getRenderTarget();
1952 if (!renderTarget)
1953 {
1954 return false; // Context must be lost
1955 }
1956 renderTarget->GetDesc(&mRenderTargetDesc);
1957 mRenderTargetDescInitialized = true;
1958 renderTarget->Release();
1959 }
1960
1961 D3DVIEWPORT9 viewport;
1962
1963 float zNear = clamp01(mState.zNear);
1964 float zFar = clamp01(mState.zFar);
1965
1966 if (ignoreViewport)
1967 {
1968 viewport.X = 0;
1969 viewport.Y = 0;
1970 viewport.Width = mRenderTargetDesc.Width;
1971 viewport.Height = mRenderTargetDesc.Height;
1972 viewport.MinZ = 0.0f;
1973 viewport.MaxZ = 1.0f;
1974 }
1975 else
1976 {
1977 viewport.X = clamp(mState.viewportX, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1978 viewport.Y = clamp(mState.viewportY, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1979 viewport.Width = clamp(mState.viewportWidth, 0L, static_cast<LONG>(mRenderTargetDesc.Width) - static_cast<LONG>(viewport.X));
1980 viewport.Height = clamp(mState.viewportHeight, 0L, static_cast<LONG>(mRenderTargetDesc.Height) - static_cast<LONG>(viewport.Y));
1981 viewport.MinZ = zNear;
1982 viewport.MaxZ = zFar;
1983 }
1984
1985 if (viewport.Width <= 0 || viewport.Height <= 0)
1986 {
1987 return false; // Nothing to render
1988 }
1989
1990 if (renderTargetChanged || !mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
1991 {
1992 mDevice->SetViewport(&viewport);
1993 mSetViewport = viewport;
1994 mViewportInitialized = true;
1995 mDxUniformsDirty = true;
1996 }
1997
1998 if (mScissorStateDirty)
1999 {
2000 if (mState.scissorTest)
2001 {
2002 RECT rect;
2003 rect.left = clamp(mState.scissorX, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
2004 rect.top = clamp(mState.scissorY, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
2005 rect.right = clamp(mState.scissorX + mState.scissorWidth, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
2006 rect.bottom = clamp(mState.scissorY + mState.scissorHeight, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
2007 mDevice->SetScissorRect(&rect);
2008 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
2009 }
2010 else
2011 {
2012 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
2013 }
2014
2015 mScissorStateDirty = false;
2016 }
2017
2018 if (mState.currentProgram && mDxUniformsDirty)
2019 {
daniel@transgaming.com62a28462012-07-24 18:33:59 +00002020 ProgramBinary *programBinary = getCurrentProgramBinary();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002021
2022 GLint halfPixelSize = programBinary->getDxHalfPixelSizeLocation();
2023 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
2024 programBinary->setUniform2fv(halfPixelSize, 1, xy);
2025
2026 // These values are used for computing gl_FragCoord in Program::linkVaryings().
2027 GLint coord = programBinary->getDxCoordLocation();
2028 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
2029 (float)mState.viewportX + mState.viewportWidth / 2.0f,
2030 (float)mState.viewportY + mState.viewportHeight / 2.0f};
2031 programBinary->setUniform4fv(coord, 1, whxy);
2032
2033 GLint depth = programBinary->getDxDepthLocation();
2034 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
2035 programBinary->setUniform2fv(depth, 1, dz);
2036
2037 GLint depthRange = programBinary->getDxDepthRangeLocation();
2038 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
2039 programBinary->setUniform3fv(depthRange, 1, nearFarDiff);
2040 mDxUniformsDirty = false;
2041 }
2042
2043 return true;
2044}
2045
2046// Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc) to the Direct3D 9 device
2047void Context::applyState(GLenum drawMode)
2048{
daniel@transgaming.com62a28462012-07-24 18:33:59 +00002049 ProgramBinary *programBinary = getCurrentProgramBinary();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002050
2051 Framebuffer *framebufferObject = getDrawFramebuffer();
2052
2053 GLint frontCCW = programBinary->getDxFrontCCWLocation();
2054 GLint ccw = (mState.frontFace == GL_CCW);
2055 programBinary->setUniform1iv(frontCCW, 1, &ccw);
2056
2057 GLint pointsOrLines = programBinary->getDxPointsOrLinesLocation();
2058 GLint alwaysFront = !isTriangleMode(drawMode);
2059 programBinary->setUniform1iv(pointsOrLines, 1, &alwaysFront);
2060
2061 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
2062 bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
2063 // Apparently some ATI cards have a bug where a draw with a zero color
2064 // write mask can cause later draws to have incorrect results. Instead,
2065 // set a nonzero color write mask but modify the blend state so that no
2066 // drawing is done.
2067 // http://code.google.com/p/angleproject/issues/detail?id=169
2068
2069 if (mCullStateDirty || mFrontFaceDirty)
2070 {
2071 if (mState.cullFace)
2072 {
2073 mDevice->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
2074 }
2075 else
2076 {
2077 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2078 }
2079
2080 mCullStateDirty = false;
2081 }
2082
2083 if (mDepthStateDirty)
2084 {
2085 if (mState.depthTest)
2086 {
2087 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
2088 mDevice->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
2089 }
2090 else
2091 {
2092 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
2093 }
2094
2095 mDepthStateDirty = false;
2096 }
2097
2098 if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
2099 {
2100 mBlendStateDirty = true;
2101 mMaskStateDirty = true;
2102 }
2103
2104 if (mBlendStateDirty)
2105 {
2106 if (mState.blend)
2107 {
2108 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
2109
2110 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
2111 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
2112 {
2113 mDevice->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
2114 }
2115 else
2116 {
2117 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
2118 unorm<8>(mState.blendColor.alpha),
2119 unorm<8>(mState.blendColor.alpha),
2120 unorm<8>(mState.blendColor.alpha)));
2121 }
2122
2123 mDevice->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
2124 mDevice->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
2125 mDevice->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
2126
2127 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
2128 mState.destBlendRGB != mState.destBlendAlpha ||
2129 mState.blendEquationRGB != mState.blendEquationAlpha)
2130 {
2131 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2132
2133 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
2134 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
2135 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
2136 }
2137 else
2138 {
2139 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
2140 }
2141 }
2142 else
2143 {
2144 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2145 }
2146
2147 mBlendStateDirty = false;
2148 }
2149
2150 if (mStencilStateDirty || mFrontFaceDirty)
2151 {
2152 if (mState.stencilTest && framebufferObject->hasStencil())
2153 {
2154 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2155 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
2156
2157 // FIXME: Unsupported by D3D9
2158 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
2159 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
2160 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
2161 if (mState.stencilWritemask != mState.stencilBackWritemask ||
2162 mState.stencilRef != mState.stencilBackRef ||
2163 mState.stencilMask != mState.stencilBackMask)
2164 {
2165 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
2166 return error(GL_INVALID_OPERATION);
2167 }
2168
2169 // get the maximum size of the stencil ref
2170 gl::Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
2171 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
2172
2173 mDevice->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
2174 mDevice->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
2175 es2dx::ConvertComparison(mState.stencilFunc));
2176
2177 mDevice->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
2178 mDevice->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
2179
2180 mDevice->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
2181 es2dx::ConvertStencilOp(mState.stencilFail));
2182 mDevice->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
2183 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
2184 mDevice->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
2185 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
2186
2187 mDevice->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
2188 mDevice->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
2189 es2dx::ConvertComparison(mState.stencilBackFunc));
2190
2191 mDevice->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
2192 mDevice->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
2193
2194 mDevice->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
2195 es2dx::ConvertStencilOp(mState.stencilBackFail));
2196 mDevice->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
2197 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
2198 mDevice->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
2199 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
2200 }
2201 else
2202 {
2203 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2204 }
2205
2206 mStencilStateDirty = false;
2207 mFrontFaceDirty = false;
2208 }
2209
2210 if (mMaskStateDirty)
2211 {
2212 int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
2213 mState.colorMaskBlue, mState.colorMaskAlpha);
2214 if (colorMask == 0 && !zeroColorMaskAllowed)
2215 {
2216 // Enable green channel, but set blending so nothing will be drawn.
2217 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
2218 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
2219
2220 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
2221 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
2222 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
2223 }
2224 else
2225 {
2226 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
2227 }
2228 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
2229
2230 mMaskStateDirty = false;
2231 }
2232
2233 if (mPolygonOffsetStateDirty)
2234 {
2235 if (mState.polygonOffsetFill)
2236 {
2237 gl::Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
2238 if (depthbuffer)
2239 {
2240 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
2241 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
2242 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
2243 }
2244 }
2245 else
2246 {
2247 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
2248 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
2249 }
2250
2251 mPolygonOffsetStateDirty = false;
2252 }
2253
2254 if (mSampleStateDirty)
2255 {
2256 if (mState.sampleAlphaToCoverage)
2257 {
2258 FIXME("Sample alpha to coverage is unimplemented.");
2259 }
2260
2261 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
2262 if (mState.sampleCoverage)
2263 {
2264 unsigned int mask = 0;
2265 if (mState.sampleCoverageValue != 0)
2266 {
2267 float threshold = 0.5f;
2268
2269 for (int i = 0; i < framebufferObject->getSamples(); ++i)
2270 {
2271 mask <<= 1;
2272
2273 if ((i + 1) * mState.sampleCoverageValue >= threshold)
2274 {
2275 threshold += 1.0f;
2276 mask |= 1;
2277 }
2278 }
2279 }
2280
2281 if (mState.sampleCoverageInvert)
2282 {
2283 mask = ~mask;
2284 }
2285
2286 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
2287 }
2288 else
2289 {
2290 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
2291 }
2292
2293 mSampleStateDirty = false;
2294 }
2295
2296 if (mDitherStateDirty)
2297 {
2298 mDevice->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
2299
2300 mDitherStateDirty = false;
2301 }
2302}
2303
2304GLenum Context::applyVertexBuffer(GLint first, GLsizei count, GLsizei instances, GLsizei *repeatDraw)
2305{
2306 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
2307
2308 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes, instances);
2309 if (err != GL_NO_ERROR)
2310 {
2311 return err;
2312 }
2313
daniel@transgaming.com62a28462012-07-24 18:33:59 +00002314 ProgramBinary *programBinary = getCurrentProgramBinary();
daniel@transgaming.com5ae3ccc2012-07-24 18:29:38 +00002315 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, repeatDraw);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002316}
2317
2318// Applies the indices and element array bindings to the Direct3D 9 device
2319GLenum Context::applyIndexBuffer(const GLvoid *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
2320{
2321 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
2322
2323 if (err == GL_NO_ERROR)
2324 {
2325 if (indexInfo->serial != mAppliedIBSerial)
2326 {
2327 mDevice->SetIndices(indexInfo->indexBuffer);
2328 mAppliedIBSerial = indexInfo->serial;
2329 }
2330 }
2331
2332 return err;
2333}
2334
2335// Applies the shaders and shader constants to the Direct3D 9 device
2336void Context::applyShaders()
2337{
daniel@transgaming.com62a28462012-07-24 18:33:59 +00002338 ProgramBinary *programBinary = getCurrentProgramBinary();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002339
daniel@transgaming.come6af4f92012-07-24 18:31:31 +00002340 if (programBinary->getSerial() != mAppliedProgramBinarySerial)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002341 {
2342 IDirect3DVertexShader9 *vertexShader = programBinary->getVertexShader();
2343 IDirect3DPixelShader9 *pixelShader = programBinary->getPixelShader();
2344
2345 mDevice->SetPixelShader(pixelShader);
2346 mDevice->SetVertexShader(vertexShader);
2347 programBinary->dirtyAllUniforms();
daniel@transgaming.come6af4f92012-07-24 18:31:31 +00002348 mAppliedProgramBinarySerial = programBinary->getSerial();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002349 }
2350
2351 programBinary->applyUniforms();
2352}
2353
2354// Applies the textures and sampler states to the Direct3D 9 device
2355void Context::applyTextures()
2356{
2357 applyTextures(SAMPLER_PIXEL);
2358
2359 if (mSupportsVertexTexture)
2360 {
2361 applyTextures(SAMPLER_VERTEX);
2362 }
2363}
2364
2365// For each Direct3D 9 sampler of either the pixel or vertex stage,
2366// looks up the corresponding OpenGL texture image unit and texture type,
2367// and sets the texture and its addressing/filtering state (or NULL when inactive).
2368void Context::applyTextures(SamplerType type)
2369{
daniel@transgaming.com62a28462012-07-24 18:33:59 +00002370 ProgramBinary *programBinary = getCurrentProgramBinary();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002371
2372 int samplerCount = (type == SAMPLER_PIXEL) ? MAX_TEXTURE_IMAGE_UNITS : MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; // Range of Direct3D 9 samplers of given sampler type
2373 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2374 int d3dSamplerOffset = (type == SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
2375 int samplerRange = programBinary->getUsedSamplerRange(type);
2376
2377 for (int samplerIndex = 0; samplerIndex < samplerRange; samplerIndex++)
2378 {
2379 int textureUnit = programBinary->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
2380 int d3dSampler = samplerIndex + d3dSamplerOffset;
2381
2382 if (textureUnit != -1)
2383 {
2384 TextureType textureType = programBinary->getSamplerTextureType(type, samplerIndex);
2385
2386 Texture *texture = getSamplerTexture(textureUnit, textureType);
2387 unsigned int texSerial = texture->getTextureSerial();
2388
2389 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyParameters() || texture->hasDirtyImages())
2390 {
2391 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2392
2393 if (d3dTexture)
2394 {
2395 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyParameters())
2396 {
2397 GLenum wrapS = texture->getWrapS();
2398 GLenum wrapT = texture->getWrapT();
2399 GLenum minFilter = texture->getMinFilter();
2400 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00002401 float maxAnisotropy = texture->getMaxAnisotropy();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002402
2403 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2404 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
2405
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00002406 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter, maxAnisotropy));
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002407 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00002408 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter, maxAnisotropy);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002409 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2410 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
jbauman@chromium.org68715282012-07-12 23:28:41 +00002411 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, texture->getLodOffset());
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00002412
2413 if (supportsTextureFilterAnisotropy())
2414 {
2415 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)maxAnisotropy);
2416 }
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002417 }
2418
2419 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyImages())
2420 {
2421 mDevice->SetTexture(d3dSampler, d3dTexture);
2422 }
2423 }
2424 else
2425 {
2426 mDevice->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
2427 }
2428
2429 appliedTextureSerial[samplerIndex] = texSerial;
2430 texture->resetDirty();
2431 }
2432 }
2433 else
2434 {
2435 if (appliedTextureSerial[samplerIndex] != 0)
2436 {
2437 mDevice->SetTexture(d3dSampler, NULL);
2438 appliedTextureSerial[samplerIndex] = 0;
2439 }
2440 }
2441 }
2442
2443 for (int samplerIndex = samplerRange; samplerIndex < samplerCount; samplerIndex++)
2444 {
2445 if (appliedTextureSerial[samplerIndex] != 0)
2446 {
2447 mDevice->SetTexture(samplerIndex + d3dSamplerOffset, NULL);
2448 appliedTextureSerial[samplerIndex] = 0;
2449 }
2450 }
2451}
2452
2453void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height,
2454 GLenum format, GLenum type, GLsizei *bufSize, void* pixels)
2455{
2456 Framebuffer *framebuffer = getReadFramebuffer();
2457
2458 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2459 {
2460 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2461 }
2462
2463 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2464 {
2465 return error(GL_INVALID_OPERATION);
2466 }
2467
2468 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
2469 // sized query sanity check
2470 if (bufSize)
2471 {
2472 int requiredSize = outputPitch * height;
2473 if (requiredSize > *bufSize)
2474 {
2475 return error(GL_INVALID_OPERATION);
2476 }
2477 }
2478
2479 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
2480 if (!renderTarget)
2481 {
2482 return; // Context must be lost, return silently
2483 }
2484
2485 D3DSURFACE_DESC desc;
2486 renderTarget->GetDesc(&desc);
2487
2488 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2489 {
2490 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2491 renderTarget->Release();
2492 return error(GL_OUT_OF_MEMORY);
2493 }
2494
2495 HRESULT result;
2496 IDirect3DSurface9 *systemSurface = NULL;
2497 bool directToPixels = !getPackReverseRowOrder() && getPackAlignment() <= 4 && mDisplay->isD3d9ExDevice() &&
2498 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
2499 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2500 if (directToPixels)
2501 {
2502 // Use the pixels ptr as a shared handle to write directly into client's memory
2503 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2504 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2505 if (FAILED(result))
2506 {
2507 // Try again without the shared handle
2508 directToPixels = false;
2509 }
2510 }
2511
2512 if (!directToPixels)
2513 {
2514 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2515 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2516 if (FAILED(result))
2517 {
2518 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2519 renderTarget->Release();
2520 return error(GL_OUT_OF_MEMORY);
2521 }
2522 }
2523
2524 result = mDevice->GetRenderTargetData(renderTarget, systemSurface);
2525 renderTarget->Release();
2526 renderTarget = NULL;
2527
2528 if (FAILED(result))
2529 {
2530 systemSurface->Release();
2531
2532 // It turns out that D3D will sometimes produce more error
2533 // codes than those documented.
2534 if (checkDeviceLost(result))
2535 return error(GL_OUT_OF_MEMORY);
2536 else
2537 {
2538 UNREACHABLE();
2539 return;
2540 }
2541
2542 }
2543
2544 if (directToPixels)
2545 {
2546 systemSurface->Release();
2547 return;
2548 }
2549
2550 RECT rect;
2551 rect.left = clamp(x, 0L, static_cast<LONG>(desc.Width));
2552 rect.top = clamp(y, 0L, static_cast<LONG>(desc.Height));
2553 rect.right = clamp(x + width, 0L, static_cast<LONG>(desc.Width));
2554 rect.bottom = clamp(y + height, 0L, static_cast<LONG>(desc.Height));
2555
2556 D3DLOCKED_RECT lock;
2557 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2558
2559 if (FAILED(result))
2560 {
2561 UNREACHABLE();
2562 systemSurface->Release();
2563
2564 return; // No sensible error to generate
2565 }
2566
2567 unsigned char *dest = (unsigned char*)pixels;
2568 unsigned short *dest16 = (unsigned short*)pixels;
2569
2570 unsigned char *source;
2571 int inputPitch;
2572 if (getPackReverseRowOrder())
2573 {
2574 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2575 inputPitch = -lock.Pitch;
2576 }
2577 else
2578 {
2579 source = (unsigned char*)lock.pBits;
2580 inputPitch = lock.Pitch;
2581 }
2582
2583 for (int j = 0; j < rect.bottom - rect.top; j++)
2584 {
2585 if (desc.Format == D3DFMT_A8R8G8B8 &&
2586 format == GL_BGRA_EXT &&
2587 type == GL_UNSIGNED_BYTE)
2588 {
2589 // Fast path for EXT_read_format_bgra, given
2590 // an RGBA source buffer. Note that buffers with no
2591 // alpha go through the slow path below.
2592 memcpy(dest + j * outputPitch,
2593 source + j * inputPitch,
2594 (rect.right - rect.left) * 4);
2595 continue;
2596 }
2597
2598 for (int i = 0; i < rect.right - rect.left; i++)
2599 {
2600 float r;
2601 float g;
2602 float b;
2603 float a;
2604
2605 switch (desc.Format)
2606 {
2607 case D3DFMT_R5G6B5:
2608 {
2609 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2610
2611 a = 1.0f;
2612 b = (rgb & 0x001F) * (1.0f / 0x001F);
2613 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2614 r = (rgb & 0xF800) * (1.0f / 0xF800);
2615 }
2616 break;
2617 case D3DFMT_A1R5G5B5:
2618 {
2619 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2620
2621 a = (argb & 0x8000) ? 1.0f : 0.0f;
2622 b = (argb & 0x001F) * (1.0f / 0x001F);
2623 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2624 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2625 }
2626 break;
2627 case D3DFMT_A8R8G8B8:
2628 {
2629 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2630
2631 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2632 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2633 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2634 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2635 }
2636 break;
2637 case D3DFMT_X8R8G8B8:
2638 {
2639 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2640
2641 a = 1.0f;
2642 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2643 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2644 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2645 }
2646 break;
2647 case D3DFMT_A2R10G10B10:
2648 {
2649 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2650
2651 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2652 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2653 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2654 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2655 }
2656 break;
2657 case D3DFMT_A32B32G32R32F:
2658 {
2659 // float formats in D3D are stored rgba, rather than the other way round
2660 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2661 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2662 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2663 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
2664 }
2665 break;
2666 case D3DFMT_A16B16G16R16F:
2667 {
2668 // float formats in D3D are stored rgba, rather than the other way round
2669 float abgr[4];
2670
2671 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
2672
2673 a = abgr[3];
2674 b = abgr[2];
2675 g = abgr[1];
2676 r = abgr[0];
2677 }
2678 break;
2679 default:
2680 UNIMPLEMENTED(); // FIXME
2681 UNREACHABLE();
2682 return;
2683 }
2684
2685 switch (format)
2686 {
2687 case GL_RGBA:
2688 switch (type)
2689 {
2690 case GL_UNSIGNED_BYTE:
2691 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2692 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2693 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2694 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2695 break;
2696 default: UNREACHABLE();
2697 }
2698 break;
2699 case GL_BGRA_EXT:
2700 switch (type)
2701 {
2702 case GL_UNSIGNED_BYTE:
2703 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2704 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2705 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2706 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2707 break;
2708 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2709 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2710 // this type is packed as follows:
2711 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2712 // --------------------------------------------------------------------------------
2713 // | 4th | 3rd | 2nd | 1st component |
2714 // --------------------------------------------------------------------------------
2715 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2716 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2717 ((unsigned short)(15 * a + 0.5f) << 12)|
2718 ((unsigned short)(15 * r + 0.5f) << 8) |
2719 ((unsigned short)(15 * g + 0.5f) << 4) |
2720 ((unsigned short)(15 * b + 0.5f) << 0);
2721 break;
2722 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2723 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2724 // this type is packed as follows:
2725 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2726 // --------------------------------------------------------------------------------
2727 // | 4th | 3rd | 2nd | 1st component |
2728 // --------------------------------------------------------------------------------
2729 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2730 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2731 ((unsigned short)( a + 0.5f) << 15) |
2732 ((unsigned short)(31 * r + 0.5f) << 10) |
2733 ((unsigned short)(31 * g + 0.5f) << 5) |
2734 ((unsigned short)(31 * b + 0.5f) << 0);
2735 break;
2736 default: UNREACHABLE();
2737 }
2738 break;
2739 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
2740 switch (type)
2741 {
2742 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
2743 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2744 ((unsigned short)(31 * b + 0.5f) << 0) |
2745 ((unsigned short)(63 * g + 0.5f) << 5) |
2746 ((unsigned short)(31 * r + 0.5f) << 11);
2747 break;
2748 default: UNREACHABLE();
2749 }
2750 break;
2751 default: UNREACHABLE();
2752 }
2753 }
2754 }
2755
2756 systemSurface->UnlockRect();
2757
2758 systemSurface->Release();
2759}
2760
2761void Context::clear(GLbitfield mask)
2762{
2763 Framebuffer *framebufferObject = getDrawFramebuffer();
2764
2765 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2766 {
2767 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2768 }
2769
2770 DWORD flags = 0;
2771
2772 if (mask & GL_COLOR_BUFFER_BIT)
2773 {
2774 mask &= ~GL_COLOR_BUFFER_BIT;
2775
2776 if (framebufferObject->getColorbufferType() != GL_NONE)
2777 {
2778 flags |= D3DCLEAR_TARGET;
2779 }
2780 }
2781
2782 if (mask & GL_DEPTH_BUFFER_BIT)
2783 {
2784 mask &= ~GL_DEPTH_BUFFER_BIT;
2785 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
2786 {
2787 flags |= D3DCLEAR_ZBUFFER;
2788 }
2789 }
2790
2791 GLuint stencilUnmasked = 0x0;
2792
2793 if (mask & GL_STENCIL_BUFFER_BIT)
2794 {
2795 mask &= ~GL_STENCIL_BUFFER_BIT;
2796 if (framebufferObject->getStencilbufferType() != GL_NONE)
2797 {
2798 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
2799 if (!depthStencil)
2800 {
2801 ERR("Depth stencil pointer unexpectedly null.");
2802 return;
2803 }
2804
2805 D3DSURFACE_DESC desc;
2806 depthStencil->GetDesc(&desc);
2807 depthStencil->Release();
2808
2809 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
2810 stencilUnmasked = (0x1 << stencilSize) - 1;
2811
2812 if (stencilUnmasked != 0x0)
2813 {
2814 flags |= D3DCLEAR_STENCIL;
2815 }
2816 }
2817 }
2818
2819 if (mask != 0)
2820 {
2821 return error(GL_INVALID_VALUE);
2822 }
2823
2824 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2825 {
2826 return;
2827 }
2828
2829 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2830 unorm<8>(mState.colorClearValue.red),
2831 unorm<8>(mState.colorClearValue.green),
2832 unorm<8>(mState.colorClearValue.blue));
2833 float depth = clamp01(mState.depthClearValue);
2834 int stencil = mState.stencilClearValue & 0x000000FF;
2835
2836 bool alphaUnmasked = (dx2es::GetAlphaSize(mRenderTargetDesc.Format) == 0) || mState.colorMaskAlpha;
2837
2838 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
2839 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
2840 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
2841 !(mState.colorMaskRed && mState.colorMaskGreen &&
2842 mState.colorMaskBlue && alphaUnmasked);
2843
2844 if (needMaskedColorClear || needMaskedStencilClear)
2845 {
2846 // State which is altered in all paths from this point to the clear call is saved.
2847 // State which is altered in only some paths will be flagged dirty in the case that
2848 // that path is taken.
2849 HRESULT hr;
2850 if (mMaskedClearSavedState == NULL)
2851 {
2852 hr = mDevice->BeginStateBlock();
2853 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2854
2855 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2856 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2857 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2858 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2859 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2860 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2861 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2862 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2863 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2864 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2865 mDevice->SetPixelShader(NULL);
2866 mDevice->SetVertexShader(NULL);
2867 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2868 mDevice->SetStreamSource(0, NULL, 0, 0);
2869 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2870 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2871 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2872 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2873 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2874 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2875 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
2876
2877 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2878 {
2879 mDevice->SetStreamSourceFreq(i, 1);
2880 }
2881
2882 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
2883 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2884 }
2885
2886 ASSERT(mMaskedClearSavedState != NULL);
2887
2888 if (mMaskedClearSavedState != NULL)
2889 {
2890 hr = mMaskedClearSavedState->Capture();
2891 ASSERT(SUCCEEDED(hr));
2892 }
2893
2894 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2895 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2896 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2897 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2898 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2899 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2900 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2901 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2902
2903 if (flags & D3DCLEAR_TARGET)
2904 {
2905 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
2906 }
2907 else
2908 {
2909 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2910 }
2911
2912 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2913 {
2914 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2915 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2916 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2917 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
2918 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
2919 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
2920 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2921 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
2922 mStencilStateDirty = true;
2923 }
2924 else
2925 {
2926 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2927 }
2928
2929 mDevice->SetPixelShader(NULL);
2930 mDevice->SetVertexShader(NULL);
2931 mDevice->SetFVF(D3DFVF_XYZRHW);
2932 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2933 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2934 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2935 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2936 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2937 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2938 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
2939
2940 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2941 {
2942 mDevice->SetStreamSourceFreq(i, 1);
2943 }
2944
2945 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2946 quad[0][0] = -0.5f;
2947 quad[0][1] = mRenderTargetDesc.Height - 0.5f;
2948 quad[0][2] = 0.0f;
2949 quad[0][3] = 1.0f;
2950
2951 quad[1][0] = mRenderTargetDesc.Width - 0.5f;
2952 quad[1][1] = mRenderTargetDesc.Height - 0.5f;
2953 quad[1][2] = 0.0f;
2954 quad[1][3] = 1.0f;
2955
2956 quad[2][0] = -0.5f;
2957 quad[2][1] = -0.5f;
2958 quad[2][2] = 0.0f;
2959 quad[2][3] = 1.0f;
2960
2961 quad[3][0] = mRenderTargetDesc.Width - 0.5f;
2962 quad[3][1] = -0.5f;
2963 quad[3][2] = 0.0f;
2964 quad[3][3] = 1.0f;
2965
2966 mDisplay->startScene();
2967 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
2968
2969 if (flags & D3DCLEAR_ZBUFFER)
2970 {
2971 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
2972 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2973 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2974 }
2975
2976 if (mMaskedClearSavedState != NULL)
2977 {
2978 mMaskedClearSavedState->Apply();
2979 }
2980 }
2981 else if (flags)
2982 {
2983 mDevice->Clear(0, NULL, flags, color, depth, stencil);
2984 }
2985}
2986
2987void Context::drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instances)
2988{
2989 if (!mState.currentProgram)
2990 {
2991 return error(GL_INVALID_OPERATION);
2992 }
2993
2994 D3DPRIMITIVETYPE primitiveType;
2995 int primitiveCount;
2996
2997 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2998 return error(GL_INVALID_ENUM);
2999
3000 if (primitiveCount <= 0)
3001 {
3002 return;
3003 }
3004
3005 if (!applyRenderTarget(false))
3006 {
3007 return;
3008 }
3009
3010 applyState(mode);
3011
3012 GLsizei repeatDraw = 1;
3013 GLenum err = applyVertexBuffer(first, count, instances, &repeatDraw);
3014 if (err != GL_NO_ERROR)
3015 {
3016 return error(err);
3017 }
3018
3019 applyShaders();
3020 applyTextures();
3021
daniel@transgaming.com62a28462012-07-24 18:33:59 +00003022 if (!getCurrentProgramBinary()->validateSamplers(NULL))
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003023 {
3024 return error(GL_INVALID_OPERATION);
3025 }
3026
3027 if (!cullSkipsDraw(mode))
3028 {
3029 mDisplay->startScene();
3030
3031 if (mode == GL_LINE_LOOP)
3032 {
3033 drawLineLoop(count, GL_NONE, NULL, 0);
3034 }
3035 else if (instances > 0)
3036 {
3037 StaticIndexBuffer *countingIB = mIndexDataManager->getCountingIndices(count);
3038 if (countingIB)
3039 {
3040 if (mAppliedIBSerial != countingIB->getSerial())
3041 {
3042 mDevice->SetIndices(countingIB->getBuffer());
3043 mAppliedIBSerial = countingIB->getSerial();
3044 }
3045
3046 for (int i = 0; i < repeatDraw; i++)
3047 {
3048 mDevice->DrawIndexedPrimitive(primitiveType, 0, 0, count, 0, primitiveCount);
3049 }
3050 }
3051 else
3052 {
3053 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
3054 return error(GL_OUT_OF_MEMORY);
3055 }
3056 }
3057 else // Regular case
3058 {
3059 mDevice->DrawPrimitive(primitiveType, 0, primitiveCount);
3060 }
3061 }
3062}
3063
3064void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, GLsizei instances)
3065{
3066 if (!mState.currentProgram)
3067 {
3068 return error(GL_INVALID_OPERATION);
3069 }
3070
3071 if (!indices && !mState.elementArrayBuffer)
3072 {
3073 return error(GL_INVALID_OPERATION);
3074 }
3075
3076 D3DPRIMITIVETYPE primitiveType;
3077 int primitiveCount;
3078
3079 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
3080 return error(GL_INVALID_ENUM);
3081
3082 if (primitiveCount <= 0)
3083 {
3084 return;
3085 }
3086
3087 if (!applyRenderTarget(false))
3088 {
3089 return;
3090 }
3091
3092 applyState(mode);
3093
3094 TranslatedIndexData indexInfo;
3095 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
3096 if (err != GL_NO_ERROR)
3097 {
3098 return error(err);
3099 }
3100
3101 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
3102 GLsizei repeatDraw = 1;
3103 err = applyVertexBuffer(indexInfo.minIndex, vertexCount, instances, &repeatDraw);
3104 if (err != GL_NO_ERROR)
3105 {
3106 return error(err);
3107 }
3108
3109 applyShaders();
3110 applyTextures();
3111
daniel@transgaming.com62a28462012-07-24 18:33:59 +00003112 if (!getCurrentProgramBinary()->validateSamplers(false))
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003113 {
3114 return error(GL_INVALID_OPERATION);
3115 }
3116
3117 if (!cullSkipsDraw(mode))
3118 {
3119 mDisplay->startScene();
3120
3121 if (mode == GL_LINE_LOOP)
3122 {
3123 drawLineLoop(count, type, indices, indexInfo.minIndex);
3124 }
3125 else
3126 {
3127 for (int i = 0; i < repeatDraw; i++)
3128 {
3129 mDevice->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
3130 }
3131 }
3132 }
3133}
3134
3135// Implements glFlush when block is false, glFinish when block is true
3136void Context::sync(bool block)
3137{
3138 mDisplay->sync(block);
3139}
3140
3141void Context::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex)
3142{
3143 // Get the raw indices for an indexed draw
3144 if (type != GL_NONE && mState.elementArrayBuffer.get())
3145 {
3146 Buffer *indexBuffer = mState.elementArrayBuffer.get();
3147 intptr_t offset = reinterpret_cast<intptr_t>(indices);
3148 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
3149 }
3150
3151 UINT startIndex = 0;
3152 bool succeeded = false;
3153
3154 if (supports32bitIndices())
3155 {
3156 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
3157
3158 if (!mLineLoopIB)
3159 {
3160 mLineLoopIB = new StreamingIndexBuffer(mDevice, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
3161 }
3162
3163 if (mLineLoopIB)
3164 {
3165 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
3166
3167 UINT offset = 0;
3168 unsigned int *data = static_cast<unsigned int*>(mLineLoopIB->map(spaceNeeded, &offset));
3169 startIndex = offset / 4;
3170
3171 if (data)
3172 {
3173 switch (type)
3174 {
3175 case GL_NONE: // Non-indexed draw
3176 for (int i = 0; i < count; i++)
3177 {
3178 data[i] = i;
3179 }
3180 data[count] = 0;
3181 break;
3182 case GL_UNSIGNED_BYTE:
3183 for (int i = 0; i < count; i++)
3184 {
3185 data[i] = static_cast<const GLubyte*>(indices)[i];
3186 }
3187 data[count] = static_cast<const GLubyte*>(indices)[0];
3188 break;
3189 case GL_UNSIGNED_SHORT:
3190 for (int i = 0; i < count; i++)
3191 {
3192 data[i] = static_cast<const GLushort*>(indices)[i];
3193 }
3194 data[count] = static_cast<const GLushort*>(indices)[0];
3195 break;
3196 case GL_UNSIGNED_INT:
3197 for (int i = 0; i < count; i++)
3198 {
3199 data[i] = static_cast<const GLuint*>(indices)[i];
3200 }
3201 data[count] = static_cast<const GLuint*>(indices)[0];
3202 break;
3203 default: UNREACHABLE();
3204 }
3205
3206 mLineLoopIB->unmap();
3207 succeeded = true;
3208 }
3209 }
3210 }
3211 else
3212 {
3213 const int spaceNeeded = (count + 1) * sizeof(unsigned short);
3214
3215 if (!mLineLoopIB)
3216 {
3217 mLineLoopIB = new StreamingIndexBuffer(mDevice, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
3218 }
3219
3220 if (mLineLoopIB)
3221 {
3222 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
3223
3224 UINT offset = 0;
3225 unsigned short *data = static_cast<unsigned short*>(mLineLoopIB->map(spaceNeeded, &offset));
3226 startIndex = offset / 2;
3227
3228 if (data)
3229 {
3230 switch (type)
3231 {
3232 case GL_NONE: // Non-indexed draw
3233 for (int i = 0; i < count; i++)
3234 {
3235 data[i] = i;
3236 }
3237 data[count] = 0;
3238 break;
3239 case GL_UNSIGNED_BYTE:
3240 for (int i = 0; i < count; i++)
3241 {
3242 data[i] = static_cast<const GLubyte*>(indices)[i];
3243 }
3244 data[count] = static_cast<const GLubyte*>(indices)[0];
3245 break;
3246 case GL_UNSIGNED_SHORT:
3247 for (int i = 0; i < count; i++)
3248 {
3249 data[i] = static_cast<const GLushort*>(indices)[i];
3250 }
3251 data[count] = static_cast<const GLushort*>(indices)[0];
3252 break;
3253 case GL_UNSIGNED_INT:
3254 for (int i = 0; i < count; i++)
3255 {
3256 data[i] = static_cast<const GLuint*>(indices)[i];
3257 }
3258 data[count] = static_cast<const GLuint*>(indices)[0];
3259 break;
3260 default: UNREACHABLE();
3261 }
3262
3263 mLineLoopIB->unmap();
3264 succeeded = true;
3265 }
3266 }
3267 }
3268
3269 if (succeeded)
3270 {
3271 if (mAppliedIBSerial != mLineLoopIB->getSerial())
3272 {
3273 mDevice->SetIndices(mLineLoopIB->getBuffer());
3274 mAppliedIBSerial = mLineLoopIB->getSerial();
3275 }
3276
3277 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
3278 }
3279 else
3280 {
3281 ERR("Could not create a looping index buffer for GL_LINE_LOOP.");
3282 return error(GL_OUT_OF_MEMORY);
3283 }
3284}
3285
3286void Context::recordInvalidEnum()
3287{
3288 mInvalidEnum = true;
3289}
3290
3291void Context::recordInvalidValue()
3292{
3293 mInvalidValue = true;
3294}
3295
3296void Context::recordInvalidOperation()
3297{
3298 mInvalidOperation = true;
3299}
3300
3301void Context::recordOutOfMemory()
3302{
3303 mOutOfMemory = true;
3304}
3305
3306void Context::recordInvalidFramebufferOperation()
3307{
3308 mInvalidFramebufferOperation = true;
3309}
3310
3311// Get one of the recorded errors and clear its flag, if any.
3312// [OpenGL ES 2.0.24] section 2.5 page 13.
3313GLenum Context::getError()
3314{
3315 if (mInvalidEnum)
3316 {
3317 mInvalidEnum = false;
3318
3319 return GL_INVALID_ENUM;
3320 }
3321
3322 if (mInvalidValue)
3323 {
3324 mInvalidValue = false;
3325
3326 return GL_INVALID_VALUE;
3327 }
3328
3329 if (mInvalidOperation)
3330 {
3331 mInvalidOperation = false;
3332
3333 return GL_INVALID_OPERATION;
3334 }
3335
3336 if (mOutOfMemory)
3337 {
3338 mOutOfMemory = false;
3339
3340 return GL_OUT_OF_MEMORY;
3341 }
3342
3343 if (mInvalidFramebufferOperation)
3344 {
3345 mInvalidFramebufferOperation = false;
3346
3347 return GL_INVALID_FRAMEBUFFER_OPERATION;
3348 }
3349
3350 return GL_NO_ERROR;
3351}
3352
3353GLenum Context::getResetStatus()
3354{
3355 if (mResetStatus == GL_NO_ERROR)
3356 {
3357 bool lost = mDisplay->testDeviceLost();
3358
3359 if (lost)
3360 {
3361 mDisplay->notifyDeviceLost(); // Sets mResetStatus
3362 }
3363 }
3364
3365 GLenum status = mResetStatus;
3366
3367 if (mResetStatus != GL_NO_ERROR)
3368 {
3369 if (mDisplay->testDeviceResettable())
3370 {
3371 mResetStatus = GL_NO_ERROR;
3372 }
3373 }
3374
3375 return status;
3376}
3377
3378bool Context::isResetNotificationEnabled()
3379{
3380 return (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT);
3381}
3382
3383bool Context::supportsShaderModel3() const
3384{
3385 return mSupportsShaderModel3;
3386}
3387
3388float Context::getMaximumPointSize() const
3389{
3390 return mSupportsShaderModel3 ? mMaximumPointSize : ALIASED_POINT_SIZE_RANGE_MAX_SM2;
3391}
3392
3393int Context::getMaximumVaryingVectors() const
3394{
3395 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3396}
3397
3398unsigned int Context::getMaximumVertexTextureImageUnits() const
3399{
3400 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3401}
3402
3403unsigned int Context::getMaximumCombinedTextureImageUnits() const
3404{
3405 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3406}
3407
3408int Context::getMaximumFragmentUniformVectors() const
3409{
3410 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3411}
3412
3413int Context::getMaxSupportedSamples() const
3414{
3415 return mMaxSupportedSamples;
3416}
3417
3418int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3419{
3420 if (requested == 0)
3421 {
3422 return requested;
3423 }
3424
3425 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3426 if (itr == mMultiSampleSupport.end())
3427 {
3428 return -1;
3429 }
3430
3431 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3432 {
3433 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3434 {
3435 return i;
3436 }
3437 }
3438
3439 return -1;
3440}
3441
3442bool Context::supportsEventQueries() const
3443{
3444 return mSupportsEventQueries;
3445}
3446
3447bool Context::supportsOcclusionQueries() const
3448{
3449 return mSupportsOcclusionQueries;
3450}
3451
3452bool Context::supportsDXT1Textures() const
3453{
3454 return mSupportsDXT1Textures;
3455}
3456
3457bool Context::supportsDXT3Textures() const
3458{
3459 return mSupportsDXT3Textures;
3460}
3461
3462bool Context::supportsDXT5Textures() const
3463{
3464 return mSupportsDXT5Textures;
3465}
3466
3467bool Context::supportsFloat32Textures() const
3468{
3469 return mSupportsFloat32Textures;
3470}
3471
3472bool Context::supportsFloat32LinearFilter() const
3473{
3474 return mSupportsFloat32LinearFilter;
3475}
3476
3477bool Context::supportsFloat32RenderableTextures() const
3478{
3479 return mSupportsFloat32RenderableTextures;
3480}
3481
3482bool Context::supportsFloat16Textures() const
3483{
3484 return mSupportsFloat16Textures;
3485}
3486
3487bool Context::supportsFloat16LinearFilter() const
3488{
3489 return mSupportsFloat16LinearFilter;
3490}
3491
3492bool Context::supportsFloat16RenderableTextures() const
3493{
3494 return mSupportsFloat16RenderableTextures;
3495}
3496
3497int Context::getMaximumRenderbufferDimension() const
3498{
3499 return mMaxRenderbufferDimension;
3500}
3501
3502int Context::getMaximumTextureDimension() const
3503{
3504 return mMaxTextureDimension;
3505}
3506
3507int Context::getMaximumCubeTextureDimension() const
3508{
3509 return mMaxCubeTextureDimension;
3510}
3511
3512int Context::getMaximumTextureLevel() const
3513{
3514 return mMaxTextureLevel;
3515}
3516
3517bool Context::supportsLuminanceTextures() const
3518{
3519 return mSupportsLuminanceTextures;
3520}
3521
3522bool Context::supportsLuminanceAlphaTextures() const
3523{
3524 return mSupportsLuminanceAlphaTextures;
3525}
3526
3527bool Context::supportsDepthTextures() const
3528{
3529 return mSupportsDepthTextures;
3530}
3531
3532bool Context::supports32bitIndices() const
3533{
3534 return mSupports32bitIndices;
3535}
3536
3537bool Context::supportsNonPower2Texture() const
3538{
3539 return mSupportsNonPower2Texture;
3540}
3541
3542bool Context::supportsInstancing() const
3543{
3544 return mSupportsInstancing;
3545}
3546
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00003547bool Context::supportsTextureFilterAnisotropy() const
3548{
3549 return mSupportsTextureFilterAnisotropy;
3550}
3551
3552float Context::getTextureMaxAnisotropy() const
3553{
3554 return mMaxTextureAnisotropy;
3555}
3556
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003557void Context::detachBuffer(GLuint buffer)
3558{
3559 // [OpenGL ES 2.0.24] section 2.9 page 22:
3560 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3561 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3562
3563 if (mState.arrayBuffer.id() == buffer)
3564 {
3565 mState.arrayBuffer.set(NULL);
3566 }
3567
3568 if (mState.elementArrayBuffer.id() == buffer)
3569 {
3570 mState.elementArrayBuffer.set(NULL);
3571 }
3572
3573 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3574 {
3575 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
3576 {
3577 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
3578 }
3579 }
3580}
3581
3582void Context::detachTexture(GLuint texture)
3583{
3584 // [OpenGL ES 2.0.24] section 3.8 page 84:
3585 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3586 // rebound to texture object zero
3587
3588 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
3589 {
3590 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
3591 {
3592 if (mState.samplerTexture[type][sampler].id() == texture)
3593 {
3594 mState.samplerTexture[type][sampler].set(NULL);
3595 }
3596 }
3597 }
3598
3599 // [OpenGL ES 2.0.24] section 4.4 page 112:
3600 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3601 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3602 // image was attached in the currently bound framebuffer.
3603
3604 Framebuffer *readFramebuffer = getReadFramebuffer();
3605 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3606
3607 if (readFramebuffer)
3608 {
3609 readFramebuffer->detachTexture(texture);
3610 }
3611
3612 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3613 {
3614 drawFramebuffer->detachTexture(texture);
3615 }
3616}
3617
3618void Context::detachFramebuffer(GLuint framebuffer)
3619{
3620 // [OpenGL ES 2.0.24] section 4.4 page 107:
3621 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3622 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3623
3624 if (mState.readFramebuffer == framebuffer)
3625 {
3626 bindReadFramebuffer(0);
3627 }
3628
3629 if (mState.drawFramebuffer == framebuffer)
3630 {
3631 bindDrawFramebuffer(0);
3632 }
3633}
3634
3635void Context::detachRenderbuffer(GLuint renderbuffer)
3636{
3637 // [OpenGL ES 2.0.24] section 4.4 page 109:
3638 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3639 // had been executed with the target RENDERBUFFER and name of zero.
3640
3641 if (mState.renderbuffer.id() == renderbuffer)
3642 {
3643 bindRenderbuffer(0);
3644 }
3645
3646 // [OpenGL ES 2.0.24] section 4.4 page 111:
3647 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3648 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3649 // point to which this image was attached in the currently bound framebuffer.
3650
3651 Framebuffer *readFramebuffer = getReadFramebuffer();
3652 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3653
3654 if (readFramebuffer)
3655 {
3656 readFramebuffer->detachRenderbuffer(renderbuffer);
3657 }
3658
3659 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3660 {
3661 drawFramebuffer->detachRenderbuffer(renderbuffer);
3662 }
3663}
3664
3665Texture *Context::getIncompleteTexture(TextureType type)
3666{
3667 Texture *t = mIncompleteTextures[type].get();
3668
3669 if (t == NULL)
3670 {
3671 static const GLubyte color[] = { 0, 0, 0, 255 };
3672
3673 switch (type)
3674 {
3675 default:
3676 UNREACHABLE();
3677 // default falls through to TEXTURE_2D
3678
3679 case TEXTURE_2D:
3680 {
3681 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
3682 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3683 t = incomplete2d;
3684 }
3685 break;
3686
3687 case TEXTURE_CUBE:
3688 {
3689 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
3690
3691 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3692 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3693 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3694 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3695 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3696 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3697
3698 t = incompleteCube;
3699 }
3700 break;
3701 }
3702
3703 mIncompleteTextures[type].set(t);
3704 }
3705
3706 return t;
3707}
3708
3709bool Context::cullSkipsDraw(GLenum drawMode)
3710{
3711 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
3712}
3713
3714bool Context::isTriangleMode(GLenum drawMode)
3715{
3716 switch (drawMode)
3717 {
3718 case GL_TRIANGLES:
3719 case GL_TRIANGLE_FAN:
3720 case GL_TRIANGLE_STRIP:
3721 return true;
3722 case GL_POINTS:
3723 case GL_LINES:
3724 case GL_LINE_LOOP:
3725 case GL_LINE_STRIP:
3726 return false;
3727 default: UNREACHABLE();
3728 }
3729
3730 return false;
3731}
3732
3733void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3734{
3735 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3736
3737 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3738 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3739 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3740 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
3741
3742 mVertexDataManager->dirtyCurrentValue(index);
3743}
3744
3745void Context::setVertexAttribDivisor(GLuint index, GLuint divisor)
3746{
3747 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3748
3749 mState.vertexAttribute[index].mDivisor = divisor;
3750}
3751
3752// keep list sorted in following order
3753// OES extensions
3754// EXT extensions
3755// Vendor extensions
3756void Context::initExtensionString()
3757{
3758 mExtensionString = "";
3759
3760 // OES extensions
3761 if (supports32bitIndices())
3762 {
3763 mExtensionString += "GL_OES_element_index_uint ";
3764 }
3765
3766 mExtensionString += "GL_OES_packed_depth_stencil ";
3767 mExtensionString += "GL_OES_get_program_binary ";
3768 mExtensionString += "GL_OES_rgb8_rgba8 ";
3769 mExtensionString += "GL_OES_standard_derivatives ";
3770
3771 if (supportsFloat16Textures())
3772 {
3773 mExtensionString += "GL_OES_texture_half_float ";
3774 }
3775 if (supportsFloat16LinearFilter())
3776 {
3777 mExtensionString += "GL_OES_texture_half_float_linear ";
3778 }
3779 if (supportsFloat32Textures())
3780 {
3781 mExtensionString += "GL_OES_texture_float ";
3782 }
3783 if (supportsFloat32LinearFilter())
3784 {
3785 mExtensionString += "GL_OES_texture_float_linear ";
3786 }
3787
3788 if (supportsNonPower2Texture())
3789 {
3790 mExtensionString += "GL_OES_texture_npot ";
3791 }
3792
3793 // Multi-vendor (EXT) extensions
3794 if (supportsOcclusionQueries())
3795 {
3796 mExtensionString += "GL_EXT_occlusion_query_boolean ";
3797 }
3798
3799 mExtensionString += "GL_EXT_read_format_bgra ";
3800 mExtensionString += "GL_EXT_robustness ";
3801
3802 if (supportsDXT1Textures())
3803 {
3804 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3805 }
3806
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00003807 if (supportsTextureFilterAnisotropy())
3808 {
3809 mExtensionString += "GL_EXT_texture_filter_anisotropic ";
3810 }
3811
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003812 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3813 mExtensionString += "GL_EXT_texture_storage ";
3814
3815 // ANGLE-specific extensions
3816 if (supportsDepthTextures())
3817 {
3818 mExtensionString += "GL_ANGLE_depth_texture ";
3819 }
3820
3821 mExtensionString += "GL_ANGLE_framebuffer_blit ";
3822 if (getMaxSupportedSamples() != 0)
3823 {
3824 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3825 }
3826
3827 if (supportsInstancing())
3828 {
3829 mExtensionString += "GL_ANGLE_instanced_arrays ";
3830 }
3831
3832 mExtensionString += "GL_ANGLE_pack_reverse_row_order ";
3833
3834 if (supportsDXT3Textures())
3835 {
3836 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3837 }
3838 if (supportsDXT5Textures())
3839 {
3840 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
3841 }
3842
3843 mExtensionString += "GL_ANGLE_texture_usage ";
3844 mExtensionString += "GL_ANGLE_translated_shader_source ";
3845
3846 // Other vendor-specific extensions
3847 if (supportsEventQueries())
3848 {
3849 mExtensionString += "GL_NV_fence ";
3850 }
3851
3852 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3853 if (end != std::string::npos)
3854 {
3855 mExtensionString.resize(end+1);
3856 }
3857}
3858
3859const char *Context::getExtensionString() const
3860{
3861 return mExtensionString.c_str();
3862}
3863
3864void Context::initRendererString()
3865{
3866 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
3867
3868 mRendererString = "ANGLE (";
3869 mRendererString += identifier->Description;
3870 mRendererString += ")";
3871}
3872
3873const char *Context::getRendererString() const
3874{
3875 return mRendererString.c_str();
3876}
3877
3878void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3879 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3880 GLbitfield mask)
3881{
3882 Framebuffer *readFramebuffer = getReadFramebuffer();
3883 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3884
3885 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3886 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3887 {
3888 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3889 }
3890
3891 if (drawFramebuffer->getSamples() != 0)
3892 {
3893 return error(GL_INVALID_OPERATION);
3894 }
3895
3896 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3897 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3898 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3899 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3900
3901 RECT sourceRect;
3902 RECT destRect;
3903
3904 if (srcX0 < srcX1)
3905 {
3906 sourceRect.left = srcX0;
3907 sourceRect.right = srcX1;
3908 destRect.left = dstX0;
3909 destRect.right = dstX1;
3910 }
3911 else
3912 {
3913 sourceRect.left = srcX1;
3914 destRect.left = dstX1;
3915 sourceRect.right = srcX0;
3916 destRect.right = dstX0;
3917 }
3918
3919 if (srcY0 < srcY1)
3920 {
3921 sourceRect.bottom = srcY1;
3922 destRect.bottom = dstY1;
3923 sourceRect.top = srcY0;
3924 destRect.top = dstY0;
3925 }
3926 else
3927 {
3928 sourceRect.bottom = srcY0;
3929 destRect.bottom = dstY0;
3930 sourceRect.top = srcY1;
3931 destRect.top = dstY1;
3932 }
3933
3934 RECT sourceScissoredRect = sourceRect;
3935 RECT destScissoredRect = destRect;
3936
3937 if (mState.scissorTest)
3938 {
3939 // Only write to parts of the destination framebuffer which pass the scissor test
3940 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3941 // rect will be checked against scissorY, rather than the bottom.
3942 if (destRect.left < mState.scissorX)
3943 {
3944 int xDiff = mState.scissorX - destRect.left;
3945 destScissoredRect.left = mState.scissorX;
3946 sourceScissoredRect.left += xDiff;
3947 }
3948
3949 if (destRect.right > mState.scissorX + mState.scissorWidth)
3950 {
3951 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3952 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3953 sourceScissoredRect.right -= xDiff;
3954 }
3955
3956 if (destRect.top < mState.scissorY)
3957 {
3958 int yDiff = mState.scissorY - destRect.top;
3959 destScissoredRect.top = mState.scissorY;
3960 sourceScissoredRect.top += yDiff;
3961 }
3962
3963 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3964 {
3965 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3966 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3967 sourceScissoredRect.bottom -= yDiff;
3968 }
3969 }
3970
3971 bool blitRenderTarget = false;
3972 bool blitDepthStencil = false;
3973
3974 RECT sourceTrimmedRect = sourceScissoredRect;
3975 RECT destTrimmedRect = destScissoredRect;
3976
3977 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3978 // the actual draw and read surfaces.
3979 if (sourceTrimmedRect.left < 0)
3980 {
3981 int xDiff = 0 - sourceTrimmedRect.left;
3982 sourceTrimmedRect.left = 0;
3983 destTrimmedRect.left += xDiff;
3984 }
3985
3986 if (sourceTrimmedRect.right > readBufferWidth)
3987 {
3988 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3989 sourceTrimmedRect.right = readBufferWidth;
3990 destTrimmedRect.right -= xDiff;
3991 }
3992
3993 if (sourceTrimmedRect.top < 0)
3994 {
3995 int yDiff = 0 - sourceTrimmedRect.top;
3996 sourceTrimmedRect.top = 0;
3997 destTrimmedRect.top += yDiff;
3998 }
3999
4000 if (sourceTrimmedRect.bottom > readBufferHeight)
4001 {
4002 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
4003 sourceTrimmedRect.bottom = readBufferHeight;
4004 destTrimmedRect.bottom -= yDiff;
4005 }
4006
4007 if (destTrimmedRect.left < 0)
4008 {
4009 int xDiff = 0 - destTrimmedRect.left;
4010 destTrimmedRect.left = 0;
4011 sourceTrimmedRect.left += xDiff;
4012 }
4013
4014 if (destTrimmedRect.right > drawBufferWidth)
4015 {
4016 int xDiff = destTrimmedRect.right - drawBufferWidth;
4017 destTrimmedRect.right = drawBufferWidth;
4018 sourceTrimmedRect.right -= xDiff;
4019 }
4020
4021 if (destTrimmedRect.top < 0)
4022 {
4023 int yDiff = 0 - destTrimmedRect.top;
4024 destTrimmedRect.top = 0;
4025 sourceTrimmedRect.top += yDiff;
4026 }
4027
4028 if (destTrimmedRect.bottom > drawBufferHeight)
4029 {
4030 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
4031 destTrimmedRect.bottom = drawBufferHeight;
4032 sourceTrimmedRect.bottom -= yDiff;
4033 }
4034
4035 bool partialBufferCopy = false;
4036 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
4037 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
4038 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
4039 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
4040 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
4041 {
4042 partialBufferCopy = true;
4043 }
4044
4045 if (mask & GL_COLOR_BUFFER_BIT)
4046 {
4047 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
4048 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
4049 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
4050 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
4051 if (!validReadType || !validDrawType ||
4052 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
4053 {
4054 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
4055 return error(GL_INVALID_OPERATION);
4056 }
4057
4058 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
4059 {
4060 return error(GL_INVALID_OPERATION);
4061 }
4062
4063 blitRenderTarget = true;
4064
4065 }
4066
4067 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
4068 {
4069 Renderbuffer *readDSBuffer = NULL;
4070 Renderbuffer *drawDSBuffer = NULL;
4071
4072 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
4073 // both a depth and stencil buffer, it will be the same buffer.
4074
4075 if (mask & GL_DEPTH_BUFFER_BIT)
4076 {
4077 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
4078 {
4079 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
4080 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
4081 {
4082 return error(GL_INVALID_OPERATION);
4083 }
4084
4085 blitDepthStencil = true;
4086 readDSBuffer = readFramebuffer->getDepthbuffer();
4087 drawDSBuffer = drawFramebuffer->getDepthbuffer();
4088 }
4089 }
4090
4091 if (mask & GL_STENCIL_BUFFER_BIT)
4092 {
4093 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
4094 {
4095 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
4096 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
4097 {
4098 return error(GL_INVALID_OPERATION);
4099 }
4100
4101 blitDepthStencil = true;
4102 readDSBuffer = readFramebuffer->getStencilbuffer();
4103 drawDSBuffer = drawFramebuffer->getStencilbuffer();
4104 }
4105 }
4106
4107 if (partialBufferCopy)
4108 {
4109 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
4110 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
4111 }
4112
4113 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
4114 (readDSBuffer && readDSBuffer->getSamples() != 0))
4115 {
4116 return error(GL_INVALID_OPERATION);
4117 }
4118 }
4119
4120 if (blitRenderTarget || blitDepthStencil)
4121 {
4122 mDisplay->endScene();
4123
4124 if (blitRenderTarget)
4125 {
4126 IDirect3DSurface9* readRenderTarget = readFramebuffer->getRenderTarget();
4127 IDirect3DSurface9* drawRenderTarget = drawFramebuffer->getRenderTarget();
4128
4129 HRESULT result = mDevice->StretchRect(readRenderTarget, &sourceTrimmedRect,
4130 drawRenderTarget, &destTrimmedRect, D3DTEXF_NONE);
4131
4132 readRenderTarget->Release();
4133 drawRenderTarget->Release();
4134
4135 if (FAILED(result))
4136 {
4137 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
4138 return;
4139 }
4140 }
4141
4142 if (blitDepthStencil)
4143 {
4144 IDirect3DSurface9* readDepthStencil = readFramebuffer->getDepthStencil();
4145 IDirect3DSurface9* drawDepthStencil = drawFramebuffer->getDepthStencil();
4146
4147 HRESULT result = mDevice->StretchRect(readDepthStencil, NULL, drawDepthStencil, NULL, D3DTEXF_NONE);
4148
4149 readDepthStencil->Release();
4150 drawDepthStencil->Release();
4151
4152 if (FAILED(result))
4153 {
4154 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
4155 return;
4156 }
4157 }
4158 }
4159}
4160
4161VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
4162{
4163 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4164 {
4165 mVertexDeclCache[i].vertexDeclaration = NULL;
4166 mVertexDeclCache[i].lruCount = 0;
4167 }
4168}
4169
4170VertexDeclarationCache::~VertexDeclarationCache()
4171{
4172 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4173 {
4174 if (mVertexDeclCache[i].vertexDeclaration)
4175 {
4176 mVertexDeclCache[i].vertexDeclaration->Release();
4177 }
4178 }
4179}
4180
daniel@transgaming.com5ae3ccc2012-07-24 18:29:38 +00004181GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], ProgramBinary *programBinary, GLsizei instances, GLsizei *repeatDraw)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00004182{
4183 *repeatDraw = 1;
4184
4185 int indexedAttribute = MAX_VERTEX_ATTRIBS;
4186 int instancedAttribute = MAX_VERTEX_ATTRIBS;
4187
4188 if (instances > 0)
4189 {
4190 // Find an indexed attribute to be mapped to D3D stream 0
4191 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4192 {
4193 if (attributes[i].active)
4194 {
4195 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
4196 {
4197 if (attributes[i].divisor == 0)
4198 {
4199 indexedAttribute = i;
4200 }
4201 }
4202 else if (instancedAttribute == MAX_VERTEX_ATTRIBS)
4203 {
4204 if (attributes[i].divisor != 0)
4205 {
4206 instancedAttribute = i;
4207 }
4208 }
4209 else break; // Found both an indexed and instanced attribute
4210 }
4211 }
4212
4213 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
4214 {
4215 return GL_INVALID_OPERATION;
4216 }
4217 }
4218
4219 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
4220 D3DVERTEXELEMENT9 *element = &elements[0];
4221
apatrick@chromium.org144f2802012-07-12 01:42:34 +00004222 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4223 {
4224 if (attributes[i].active)
4225 {
4226 int stream = i;
4227
4228 if (instances > 0)
4229 {
4230 // Due to a bug on ATI cards we can't enable instancing when none of the attributes are instanced.
4231 if (instancedAttribute == MAX_VERTEX_ATTRIBS)
4232 {
4233 *repeatDraw = instances;
4234 }
4235 else
4236 {
4237 if (i == indexedAttribute)
4238 {
4239 stream = 0;
4240 }
4241 else if (i == 0)
4242 {
4243 stream = indexedAttribute;
4244 }
4245
4246 UINT frequency = 1;
4247
4248 if (attributes[i].divisor == 0)
4249 {
4250 frequency = D3DSTREAMSOURCE_INDEXEDDATA | instances;
4251 }
4252 else
4253 {
4254 frequency = D3DSTREAMSOURCE_INSTANCEDATA | attributes[i].divisor;
4255 }
4256
4257 device->SetStreamSourceFreq(stream, frequency);
4258 mInstancingEnabled = true;
4259 }
4260 }
4261
4262 if (mAppliedVBs[stream].serial != attributes[i].serial ||
4263 mAppliedVBs[stream].stride != attributes[i].stride ||
4264 mAppliedVBs[stream].offset != attributes[i].offset)
4265 {
4266 device->SetStreamSource(stream, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
4267 mAppliedVBs[stream].serial = attributes[i].serial;
4268 mAppliedVBs[stream].stride = attributes[i].stride;
4269 mAppliedVBs[stream].offset = attributes[i].offset;
4270 }
4271
4272 element->Stream = stream;
4273 element->Offset = 0;
4274 element->Type = attributes[i].type;
4275 element->Method = D3DDECLMETHOD_DEFAULT;
4276 element->Usage = D3DDECLUSAGE_TEXCOORD;
4277 element->UsageIndex = programBinary->getSemanticIndex(i);
4278 element++;
4279 }
4280 }
4281
4282 if (instances == 0 || instancedAttribute == MAX_VERTEX_ATTRIBS)
4283 {
4284 if (mInstancingEnabled)
4285 {
4286 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4287 {
4288 device->SetStreamSourceFreq(i, 1);
4289 }
4290
4291 mInstancingEnabled = false;
4292 }
4293 }
4294
4295 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
4296 *(element++) = end;
4297
4298 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4299 {
4300 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
4301 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
4302 {
4303 entry->lruCount = ++mMaxLru;
4304 if(entry->vertexDeclaration != mLastSetVDecl)
4305 {
4306 device->SetVertexDeclaration(entry->vertexDeclaration);
4307 mLastSetVDecl = entry->vertexDeclaration;
4308 }
4309
4310 return GL_NO_ERROR;
4311 }
4312 }
4313
4314 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
4315
4316 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4317 {
4318 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
4319 {
4320 lastCache = &mVertexDeclCache[i];
4321 }
4322 }
4323
4324 if (lastCache->vertexDeclaration != NULL)
4325 {
4326 lastCache->vertexDeclaration->Release();
4327 lastCache->vertexDeclaration = NULL;
4328 // mLastSetVDecl is set to the replacement, so we don't have to worry
4329 // about it.
4330 }
4331
4332 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
4333 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
4334 device->SetVertexDeclaration(lastCache->vertexDeclaration);
4335 mLastSetVDecl = lastCache->vertexDeclaration;
4336 lastCache->lruCount = ++mMaxLru;
4337
4338 return GL_NO_ERROR;
4339}
4340
4341void VertexDeclarationCache::markStateDirty()
4342{
4343 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4344 {
4345 mAppliedVBs[i].serial = 0;
4346 }
4347
4348 mLastSetVDecl = NULL;
4349 mInstancingEnabled = true; // Forces it to be disabled when not used
4350}
4351
4352}
4353
4354extern "C"
4355{
4356gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext, bool notifyResets, bool robustAccess)
4357{
4358 return new gl::Context(config, shareContext, notifyResets, robustAccess);
4359}
4360
4361void glDestroyContext(gl::Context *context)
4362{
4363 delete context;
4364
4365 if (context == gl::getContext())
4366 {
4367 gl::makeCurrent(NULL, NULL, NULL);
4368 }
4369}
4370
4371void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
4372{
4373 gl::makeCurrent(context, display, surface);
4374}
4375
4376gl::Context *glGetCurrentContext()
4377{
4378 return gl::getContext();
4379}
4380}