blob: 1aed4365dd098309f237fd387c711401265ef844 [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);
apatrick@chromium.org144f2802012-07-12 01:42:34 +0000986}
987
988void Context::deleteTexture(GLuint texture)
989{
990 if (mResourceManager->getTexture(texture))
991 {
992 detachTexture(texture);
993 }
994
995 mResourceManager->deleteTexture(texture);
996}
997
998void Context::deleteRenderbuffer(GLuint renderbuffer)
999{
1000 if (mResourceManager->getRenderbuffer(renderbuffer))
1001 {
1002 detachRenderbuffer(renderbuffer);
1003 }
1004
1005 mResourceManager->deleteRenderbuffer(renderbuffer);
1006}
1007
1008void Context::deleteFramebuffer(GLuint framebuffer)
1009{
1010 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
1011
1012 if (framebufferObject != mFramebufferMap.end())
1013 {
1014 detachFramebuffer(framebuffer);
1015
1016 mFramebufferHandleAllocator.release(framebufferObject->first);
1017 delete framebufferObject->second;
1018 mFramebufferMap.erase(framebufferObject);
1019 }
1020}
1021
1022void Context::deleteFence(GLuint fence)
1023{
1024 FenceMap::iterator fenceObject = mFenceMap.find(fence);
1025
1026 if (fenceObject != mFenceMap.end())
1027 {
1028 mFenceHandleAllocator.release(fenceObject->first);
1029 delete fenceObject->second;
1030 mFenceMap.erase(fenceObject);
1031 }
1032}
1033
1034void Context::deleteQuery(GLuint query)
1035{
1036 QueryMap::iterator queryObject = mQueryMap.find(query);
1037 if (queryObject != mQueryMap.end())
1038 {
1039 mQueryHandleAllocator.release(queryObject->first);
1040 if (queryObject->second)
1041 {
1042 queryObject->second->release();
1043 }
1044 mQueryMap.erase(queryObject);
1045 }
1046}
1047
1048Buffer *Context::getBuffer(GLuint handle)
1049{
1050 return mResourceManager->getBuffer(handle);
1051}
1052
1053Shader *Context::getShader(GLuint handle)
1054{
1055 return mResourceManager->getShader(handle);
1056}
1057
1058Program *Context::getProgram(GLuint handle)
1059{
1060 return mResourceManager->getProgram(handle);
1061}
1062
1063Texture *Context::getTexture(GLuint handle)
1064{
1065 return mResourceManager->getTexture(handle);
1066}
1067
1068Renderbuffer *Context::getRenderbuffer(GLuint handle)
1069{
1070 return mResourceManager->getRenderbuffer(handle);
1071}
1072
1073Framebuffer *Context::getReadFramebuffer()
1074{
1075 return getFramebuffer(mState.readFramebuffer);
1076}
1077
1078Framebuffer *Context::getDrawFramebuffer()
1079{
1080 return mBoundDrawFramebuffer;
1081}
1082
1083void Context::bindArrayBuffer(unsigned int buffer)
1084{
1085 mResourceManager->checkBufferAllocation(buffer);
1086
1087 mState.arrayBuffer.set(getBuffer(buffer));
1088}
1089
1090void Context::bindElementArrayBuffer(unsigned int buffer)
1091{
1092 mResourceManager->checkBufferAllocation(buffer);
1093
1094 mState.elementArrayBuffer.set(getBuffer(buffer));
1095}
1096
1097void Context::bindTexture2D(GLuint texture)
1098{
1099 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
1100
1101 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
1102}
1103
1104void Context::bindTextureCubeMap(GLuint texture)
1105{
1106 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
1107
1108 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
1109}
1110
1111void Context::bindReadFramebuffer(GLuint framebuffer)
1112{
1113 if (!getFramebuffer(framebuffer))
1114 {
1115 mFramebufferMap[framebuffer] = new Framebuffer();
1116 }
1117
1118 mState.readFramebuffer = framebuffer;
1119}
1120
1121void Context::bindDrawFramebuffer(GLuint framebuffer)
1122{
1123 if (!getFramebuffer(framebuffer))
1124 {
1125 mFramebufferMap[framebuffer] = new Framebuffer();
1126 }
1127
1128 mState.drawFramebuffer = framebuffer;
1129
1130 mBoundDrawFramebuffer = getFramebuffer(framebuffer);
1131}
1132
1133void Context::bindRenderbuffer(GLuint renderbuffer)
1134{
1135 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1136
1137 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
1138}
1139
1140void Context::useProgram(GLuint program)
1141{
1142 GLuint priorProgram = mState.currentProgram;
1143 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
1144
1145 if (priorProgram != program)
1146 {
1147 Program *newProgram = mResourceManager->getProgram(program);
1148 Program *oldProgram = mResourceManager->getProgram(priorProgram);
daniel@transgaming.com62a28462012-07-24 18:33:59 +00001149 mCachedCurrentProgramBinary = NULL;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001150 mDxUniformsDirty = true;
1151
1152 if (newProgram)
1153 {
1154 newProgram->addRef();
1155 }
1156
1157 if (oldProgram)
1158 {
1159 oldProgram->release();
1160 }
1161 }
1162}
1163
1164void Context::beginQuery(GLenum target, GLuint query)
1165{
1166 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
1167 // of zero, if the active query object name for <target> is non-zero (for the
1168 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
1169 // the active query for either target is non-zero), if <id> is the name of an
1170 // existing query object whose type does not match <target>, or if <id> is the
1171 // active query object name for any query type, the error INVALID_OPERATION is
1172 // generated.
1173
1174 // Ensure no other queries are active
1175 // NOTE: If other queries than occlusion are supported, we will need to check
1176 // separately that:
1177 // a) The query ID passed is not the current active query for any target/type
1178 // b) There are no active queries for the requested target (and in the case
1179 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
1180 // no query may be active for either if glBeginQuery targets either.
1181 for (int i = 0; i < QUERY_TYPE_COUNT; i++)
1182 {
1183 if (mState.activeQuery[i].get() != NULL)
1184 {
1185 return error(GL_INVALID_OPERATION);
1186 }
1187 }
1188
1189 QueryType qType;
1190 switch (target)
1191 {
1192 case GL_ANY_SAMPLES_PASSED_EXT:
1193 qType = QUERY_ANY_SAMPLES_PASSED;
1194 break;
1195 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
1196 qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
1197 break;
1198 default:
1199 ASSERT(false);
1200 return;
1201 }
1202
1203 Query *queryObject = getQuery(query, true, target);
1204
1205 // check that name was obtained with glGenQueries
1206 if (!queryObject)
1207 {
1208 return error(GL_INVALID_OPERATION);
1209 }
1210
1211 // check for type mismatch
1212 if (queryObject->getType() != target)
1213 {
1214 return error(GL_INVALID_OPERATION);
1215 }
1216
1217 // set query as active for specified target
1218 mState.activeQuery[qType].set(queryObject);
1219
1220 // begin query
1221 queryObject->begin();
1222}
1223
1224void Context::endQuery(GLenum target)
1225{
1226 QueryType qType;
1227
1228 switch (target)
1229 {
1230 case GL_ANY_SAMPLES_PASSED_EXT:
1231 qType = QUERY_ANY_SAMPLES_PASSED;
1232 break;
1233 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
1234 qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
1235 break;
1236 default:
1237 ASSERT(false);
1238 return;
1239 }
1240
1241 Query *queryObject = mState.activeQuery[qType].get();
1242
1243 if (queryObject == NULL)
1244 {
1245 return error(GL_INVALID_OPERATION);
1246 }
1247
1248 queryObject->end();
1249
1250 mState.activeQuery[qType].set(NULL);
1251}
1252
1253void Context::setFramebufferZero(Framebuffer *buffer)
1254{
1255 delete mFramebufferMap[0];
1256 mFramebufferMap[0] = buffer;
1257 if (mState.drawFramebuffer == 0)
1258 {
1259 mBoundDrawFramebuffer = buffer;
1260 }
1261}
1262
1263void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
1264{
1265 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1266 renderbufferObject->setStorage(renderbuffer);
1267}
1268
1269Framebuffer *Context::getFramebuffer(unsigned int handle)
1270{
1271 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1272
1273 if (framebuffer == mFramebufferMap.end())
1274 {
1275 return NULL;
1276 }
1277 else
1278 {
1279 return framebuffer->second;
1280 }
1281}
1282
1283Fence *Context::getFence(unsigned int handle)
1284{
1285 FenceMap::iterator fence = mFenceMap.find(handle);
1286
1287 if (fence == mFenceMap.end())
1288 {
1289 return NULL;
1290 }
1291 else
1292 {
1293 return fence->second;
1294 }
1295}
1296
1297Query *Context::getQuery(unsigned int handle, bool create, GLenum type)
1298{
1299 QueryMap::iterator query = mQueryMap.find(handle);
1300
1301 if (query == mQueryMap.end())
1302 {
1303 return NULL;
1304 }
1305 else
1306 {
1307 if (!query->second && create)
1308 {
1309 query->second = new Query(handle, type);
1310 query->second->addRef();
1311 }
1312 return query->second;
1313 }
1314}
1315
1316Buffer *Context::getArrayBuffer()
1317{
1318 return mState.arrayBuffer.get();
1319}
1320
1321Buffer *Context::getElementArrayBuffer()
1322{
1323 return mState.elementArrayBuffer.get();
1324}
1325
daniel@transgaming.com62a28462012-07-24 18:33:59 +00001326ProgramBinary *Context::getCurrentProgramBinary()
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001327{
daniel@transgaming.com62a28462012-07-24 18:33:59 +00001328 if (!mCachedCurrentProgramBinary)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001329 {
daniel@transgaming.com62a28462012-07-24 18:33:59 +00001330 Program *program = mResourceManager->getProgram(mState.currentProgram);
1331 mCachedCurrentProgramBinary = program->getProgramBinary();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001332 }
daniel@transgaming.com62a28462012-07-24 18:33:59 +00001333 return mCachedCurrentProgramBinary;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001334}
1335
1336Texture2D *Context::getTexture2D()
1337{
1338 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
1339}
1340
1341TextureCubeMap *Context::getTextureCubeMap()
1342{
1343 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
1344}
1345
1346Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
1347{
1348 GLuint texid = mState.samplerTexture[type][sampler].id();
1349
1350 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
1351 {
1352 switch (type)
1353 {
1354 default: UNREACHABLE();
1355 case TEXTURE_2D: return mTexture2DZero.get();
1356 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
1357 }
1358 }
1359
1360 return mState.samplerTexture[type][sampler].get();
1361}
1362
1363bool Context::getBooleanv(GLenum pname, GLboolean *params)
1364{
1365 switch (pname)
1366 {
1367 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1368 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1369 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
1370 case GL_COLOR_WRITEMASK:
1371 params[0] = mState.colorMaskRed;
1372 params[1] = mState.colorMaskGreen;
1373 params[2] = mState.colorMaskBlue;
1374 params[3] = mState.colorMaskAlpha;
1375 break;
1376 case GL_CULL_FACE: *params = mState.cullFace; break;
1377 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1378 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1379 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1380 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1381 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1382 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1383 case GL_BLEND: *params = mState.blend; break;
1384 case GL_DITHER: *params = mState.dither; break;
1385 case GL_CONTEXT_ROBUST_ACCESS_EXT: *params = mRobustAccess ? GL_TRUE : GL_FALSE; break;
1386 default:
1387 return false;
1388 }
1389
1390 return true;
1391}
1392
1393bool Context::getFloatv(GLenum pname, GLfloat *params)
1394{
1395 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1396 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1397 // GetIntegerv as its native query function. As it would require conversion in any
1398 // case, this should make no difference to the calling application.
1399 switch (pname)
1400 {
1401 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1402 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1403 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1404 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1405 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
1406 case GL_ALIASED_LINE_WIDTH_RANGE:
1407 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1408 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1409 break;
1410 case GL_ALIASED_POINT_SIZE_RANGE:
1411 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
1412 params[1] = getMaximumPointSize();
1413 break;
1414 case GL_DEPTH_RANGE:
1415 params[0] = mState.zNear;
1416 params[1] = mState.zFar;
1417 break;
1418 case GL_COLOR_CLEAR_VALUE:
1419 params[0] = mState.colorClearValue.red;
1420 params[1] = mState.colorClearValue.green;
1421 params[2] = mState.colorClearValue.blue;
1422 params[3] = mState.colorClearValue.alpha;
1423 break;
1424 case GL_BLEND_COLOR:
1425 params[0] = mState.blendColor.red;
1426 params[1] = mState.blendColor.green;
1427 params[2] = mState.blendColor.blue;
1428 params[3] = mState.blendColor.alpha;
1429 break;
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00001430 case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
1431 if (!supportsTextureFilterAnisotropy())
1432 {
1433 return false;
1434 }
1435 *params = mMaxTextureAnisotropy;
1436 break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001437 default:
1438 return false;
1439 }
1440
1441 return true;
1442}
1443
1444bool Context::getIntegerv(GLenum pname, GLint *params)
1445{
1446 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1447 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1448 // GetIntegerv as its native query function. As it would require conversion in any
1449 // case, this should make no difference to the calling application. You may find it in
1450 // Context::getFloatv.
1451 switch (pname)
1452 {
1453 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1454 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
1455 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
1456 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1457 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
1458 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
1459 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
1460 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
1461 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
1462 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
1463 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1464 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
1465 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1466 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1467 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
1468 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
1469 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1470 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1471 case GL_PACK_REVERSE_ROW_ORDER_ANGLE: *params = mState.packReverseRowOrder; break;
1472 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1473 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
1474 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
1475 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1476 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1477 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1478 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1479 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1480 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1481 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1482 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1483 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1484 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1485 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1486 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1487 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1488 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1489 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1490 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1491 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1492 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1493 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1494 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1495 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1496 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1497 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1498 case GL_SUBPIXEL_BITS: *params = 4; break;
1499 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1500 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
1501 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1502 params[0] = mNumCompressedTextureFormats;
1503 break;
1504 case GL_MAX_SAMPLES_ANGLE:
1505 {
1506 GLsizei maxSamples = getMaxSupportedSamples();
1507 if (maxSamples != 0)
1508 {
1509 *params = maxSamples;
1510 }
1511 else
1512 {
1513 return false;
1514 }
1515
1516 break;
1517 }
1518 case GL_SAMPLE_BUFFERS:
1519 case GL_SAMPLES:
1520 {
1521 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1522 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1523 {
1524 switch (pname)
1525 {
1526 case GL_SAMPLE_BUFFERS:
1527 if (framebuffer->getSamples() != 0)
1528 {
1529 *params = 1;
1530 }
1531 else
1532 {
1533 *params = 0;
1534 }
1535 break;
1536 case GL_SAMPLES:
1537 *params = framebuffer->getSamples();
1538 break;
1539 }
1540 }
1541 else
1542 {
1543 *params = 0;
1544 }
1545 }
1546 break;
1547 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1548 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1549 case GL_MAX_VIEWPORT_DIMS:
1550 {
1551 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
1552 params[0] = maxDimension;
1553 params[1] = maxDimension;
1554 }
1555 break;
1556 case GL_COMPRESSED_TEXTURE_FORMATS:
1557 {
1558 if (supportsDXT1Textures())
1559 {
1560 *params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1561 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1562 }
1563 if (supportsDXT3Textures())
1564 {
1565 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
1566 }
1567 if (supportsDXT5Textures())
1568 {
1569 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
1570 }
1571 }
1572 break;
1573 case GL_VIEWPORT:
1574 params[0] = mState.viewportX;
1575 params[1] = mState.viewportY;
1576 params[2] = mState.viewportWidth;
1577 params[3] = mState.viewportHeight;
1578 break;
1579 case GL_SCISSOR_BOX:
1580 params[0] = mState.scissorX;
1581 params[1] = mState.scissorY;
1582 params[2] = mState.scissorWidth;
1583 params[3] = mState.scissorHeight;
1584 break;
1585 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1586 case GL_FRONT_FACE: *params = mState.frontFace; break;
1587 case GL_RED_BITS:
1588 case GL_GREEN_BITS:
1589 case GL_BLUE_BITS:
1590 case GL_ALPHA_BITS:
1591 {
1592 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1593 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
1594
1595 if (colorbuffer)
1596 {
1597 switch (pname)
1598 {
1599 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1600 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1601 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1602 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1603 }
1604 }
1605 else
1606 {
1607 *params = 0;
1608 }
1609 }
1610 break;
1611 case GL_DEPTH_BITS:
1612 {
1613 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1614 gl::Renderbuffer *depthbuffer = framebuffer->getDepthbuffer();
1615
1616 if (depthbuffer)
1617 {
1618 *params = depthbuffer->getDepthSize();
1619 }
1620 else
1621 {
1622 *params = 0;
1623 }
1624 }
1625 break;
1626 case GL_STENCIL_BITS:
1627 {
1628 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1629 gl::Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer();
1630
1631 if (stencilbuffer)
1632 {
1633 *params = stencilbuffer->getStencilSize();
1634 }
1635 else
1636 {
1637 *params = 0;
1638 }
1639 }
1640 break;
1641 case GL_TEXTURE_BINDING_2D:
1642 {
1643 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
1644 {
1645 error(GL_INVALID_OPERATION);
1646 return false;
1647 }
1648
1649 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
1650 }
1651 break;
1652 case GL_TEXTURE_BINDING_CUBE_MAP:
1653 {
1654 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
1655 {
1656 error(GL_INVALID_OPERATION);
1657 return false;
1658 }
1659
1660 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
1661 }
1662 break;
1663 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
1664 *params = mResetStrategy;
1665 break;
1666 case GL_NUM_PROGRAM_BINARY_FORMATS_OES:
1667 *params = 1;
1668 break;
1669 case GL_PROGRAM_BINARY_FORMATS_OES:
1670 *params = GL_PROGRAM_BINARY_ANGLE;
1671 break;
1672 default:
1673 return false;
1674 }
1675
1676 return true;
1677}
1678
1679bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1680{
1681 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1682 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1683 // to the fact that it is stored internally as a float, and so would require conversion
1684 // if returned from Context::getIntegerv. Since this conversion is already implemented
1685 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1686 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1687 // application.
1688 switch (pname)
1689 {
1690 case GL_COMPRESSED_TEXTURE_FORMATS:
1691 {
1692 *type = GL_INT;
1693 *numParams = mNumCompressedTextureFormats;
1694 }
1695 break;
1696 case GL_SHADER_BINARY_FORMATS:
1697 {
1698 *type = GL_INT;
1699 *numParams = 0;
1700 }
1701 break;
1702 case GL_MAX_VERTEX_ATTRIBS:
1703 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1704 case GL_MAX_VARYING_VECTORS:
1705 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1706 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1707 case GL_MAX_TEXTURE_IMAGE_UNITS:
1708 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1709 case GL_MAX_RENDERBUFFER_SIZE:
1710 case GL_NUM_SHADER_BINARY_FORMATS:
1711 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1712 case GL_ARRAY_BUFFER_BINDING:
1713 case GL_FRAMEBUFFER_BINDING:
1714 case GL_RENDERBUFFER_BINDING:
1715 case GL_CURRENT_PROGRAM:
1716 case GL_PACK_ALIGNMENT:
1717 case GL_PACK_REVERSE_ROW_ORDER_ANGLE:
1718 case GL_UNPACK_ALIGNMENT:
1719 case GL_GENERATE_MIPMAP_HINT:
1720 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
1721 case GL_RED_BITS:
1722 case GL_GREEN_BITS:
1723 case GL_BLUE_BITS:
1724 case GL_ALPHA_BITS:
1725 case GL_DEPTH_BITS:
1726 case GL_STENCIL_BITS:
1727 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1728 case GL_CULL_FACE_MODE:
1729 case GL_FRONT_FACE:
1730 case GL_ACTIVE_TEXTURE:
1731 case GL_STENCIL_FUNC:
1732 case GL_STENCIL_VALUE_MASK:
1733 case GL_STENCIL_REF:
1734 case GL_STENCIL_FAIL:
1735 case GL_STENCIL_PASS_DEPTH_FAIL:
1736 case GL_STENCIL_PASS_DEPTH_PASS:
1737 case GL_STENCIL_BACK_FUNC:
1738 case GL_STENCIL_BACK_VALUE_MASK:
1739 case GL_STENCIL_BACK_REF:
1740 case GL_STENCIL_BACK_FAIL:
1741 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1742 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1743 case GL_DEPTH_FUNC:
1744 case GL_BLEND_SRC_RGB:
1745 case GL_BLEND_SRC_ALPHA:
1746 case GL_BLEND_DST_RGB:
1747 case GL_BLEND_DST_ALPHA:
1748 case GL_BLEND_EQUATION_RGB:
1749 case GL_BLEND_EQUATION_ALPHA:
1750 case GL_STENCIL_WRITEMASK:
1751 case GL_STENCIL_BACK_WRITEMASK:
1752 case GL_STENCIL_CLEAR_VALUE:
1753 case GL_SUBPIXEL_BITS:
1754 case GL_MAX_TEXTURE_SIZE:
1755 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1756 case GL_SAMPLE_BUFFERS:
1757 case GL_SAMPLES:
1758 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1759 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1760 case GL_TEXTURE_BINDING_2D:
1761 case GL_TEXTURE_BINDING_CUBE_MAP:
1762 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
1763 case GL_NUM_PROGRAM_BINARY_FORMATS_OES:
1764 case GL_PROGRAM_BINARY_FORMATS_OES:
1765 {
1766 *type = GL_INT;
1767 *numParams = 1;
1768 }
1769 break;
1770 case GL_MAX_SAMPLES_ANGLE:
1771 {
1772 if (getMaxSupportedSamples() != 0)
1773 {
1774 *type = GL_INT;
1775 *numParams = 1;
1776 }
1777 else
1778 {
1779 return false;
1780 }
1781 }
1782 break;
1783 case GL_MAX_VIEWPORT_DIMS:
1784 {
1785 *type = GL_INT;
1786 *numParams = 2;
1787 }
1788 break;
1789 case GL_VIEWPORT:
1790 case GL_SCISSOR_BOX:
1791 {
1792 *type = GL_INT;
1793 *numParams = 4;
1794 }
1795 break;
1796 case GL_SHADER_COMPILER:
1797 case GL_SAMPLE_COVERAGE_INVERT:
1798 case GL_DEPTH_WRITEMASK:
1799 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1800 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1801 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1802 case GL_SAMPLE_COVERAGE:
1803 case GL_SCISSOR_TEST:
1804 case GL_STENCIL_TEST:
1805 case GL_DEPTH_TEST:
1806 case GL_BLEND:
1807 case GL_DITHER:
1808 case GL_CONTEXT_ROBUST_ACCESS_EXT:
1809 {
1810 *type = GL_BOOL;
1811 *numParams = 1;
1812 }
1813 break;
1814 case GL_COLOR_WRITEMASK:
1815 {
1816 *type = GL_BOOL;
1817 *numParams = 4;
1818 }
1819 break;
1820 case GL_POLYGON_OFFSET_FACTOR:
1821 case GL_POLYGON_OFFSET_UNITS:
1822 case GL_SAMPLE_COVERAGE_VALUE:
1823 case GL_DEPTH_CLEAR_VALUE:
1824 case GL_LINE_WIDTH:
1825 {
1826 *type = GL_FLOAT;
1827 *numParams = 1;
1828 }
1829 break;
1830 case GL_ALIASED_LINE_WIDTH_RANGE:
1831 case GL_ALIASED_POINT_SIZE_RANGE:
1832 case GL_DEPTH_RANGE:
1833 {
1834 *type = GL_FLOAT;
1835 *numParams = 2;
1836 }
1837 break;
1838 case GL_COLOR_CLEAR_VALUE:
1839 case GL_BLEND_COLOR:
1840 {
1841 *type = GL_FLOAT;
1842 *numParams = 4;
1843 }
1844 break;
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00001845 case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
1846 if (!supportsTextureFilterAnisotropy())
1847 {
1848 return false;
1849 }
1850 *type = GL_FLOAT;
1851 *numParams = 1;
1852 break;
apatrick@chromium.org144f2802012-07-12 01:42:34 +00001853 default:
1854 return false;
1855 }
1856
1857 return true;
1858}
1859
1860// Applies the render target surface, depth stencil surface, viewport rectangle and
1861// scissor rectangle to the Direct3D 9 device
1862bool Context::applyRenderTarget(bool ignoreViewport)
1863{
1864 Framebuffer *framebufferObject = getDrawFramebuffer();
1865
1866 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1867 {
1868 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
1869 }
1870
1871 // if there is no color attachment we must synthesize a NULL colorattachment
1872 // to keep the D3D runtime happy. This should only be possible if depth texturing.
1873 Renderbuffer *renderbufferObject = NULL;
1874 if (framebufferObject->getColorbufferType() != GL_NONE)
1875 {
1876 renderbufferObject = framebufferObject->getColorbuffer();
1877 }
1878 else
1879 {
1880 renderbufferObject = framebufferObject->getNullColorbuffer();
1881 }
1882 if (!renderbufferObject)
1883 {
1884 ERR("unable to locate renderbuffer for FBO.");
1885 return false;
1886 }
1887
1888 bool renderTargetChanged = false;
1889 unsigned int renderTargetSerial = renderbufferObject->getSerial();
1890 if (renderTargetSerial != mAppliedRenderTargetSerial)
1891 {
1892 IDirect3DSurface9 *renderTarget = renderbufferObject->getRenderTarget();
1893 if (!renderTarget)
1894 {
1895 ERR("render target pointer unexpectedly null.");
1896 return false; // Context must be lost
1897 }
1898 mDevice->SetRenderTarget(0, renderTarget);
1899 mAppliedRenderTargetSerial = renderTargetSerial;
1900 mScissorStateDirty = true; // Scissor area must be clamped to render target's size-- this is different for different render targets.
1901 renderTargetChanged = true;
1902 renderTarget->Release();
1903 }
1904
1905 IDirect3DSurface9 *depthStencil = NULL;
1906 unsigned int depthbufferSerial = 0;
1907 unsigned int stencilbufferSerial = 0;
1908 if (framebufferObject->getDepthbufferType() != GL_NONE)
1909 {
1910 Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
1911 depthStencil = depthbuffer->getDepthStencil();
1912 if (!depthStencil)
1913 {
1914 ERR("Depth stencil pointer unexpectedly null.");
1915 return false;
1916 }
1917
1918 depthbufferSerial = depthbuffer->getSerial();
1919 }
1920 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1921 {
1922 Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
1923 depthStencil = stencilbuffer->getDepthStencil();
1924 if (!depthStencil)
1925 {
1926 ERR("Depth stencil pointer unexpectedly null.");
1927 return false;
1928 }
1929
1930 stencilbufferSerial = stencilbuffer->getSerial();
1931 }
1932
1933 if (depthbufferSerial != mAppliedDepthbufferSerial ||
1934 stencilbufferSerial != mAppliedStencilbufferSerial ||
1935 !mDepthStencilInitialized)
1936 {
1937 mDevice->SetDepthStencilSurface(depthStencil);
1938 mAppliedDepthbufferSerial = depthbufferSerial;
1939 mAppliedStencilbufferSerial = stencilbufferSerial;
1940 mDepthStencilInitialized = true;
1941 }
1942
1943 if (depthStencil)
1944 {
1945 depthStencil->Release();
1946 }
1947
1948 if (!mRenderTargetDescInitialized || renderTargetChanged)
1949 {
1950 IDirect3DSurface9 *renderTarget = renderbufferObject->getRenderTarget();
1951 if (!renderTarget)
1952 {
1953 return false; // Context must be lost
1954 }
1955 renderTarget->GetDesc(&mRenderTargetDesc);
1956 mRenderTargetDescInitialized = true;
1957 renderTarget->Release();
1958 }
1959
1960 D3DVIEWPORT9 viewport;
1961
1962 float zNear = clamp01(mState.zNear);
1963 float zFar = clamp01(mState.zFar);
1964
1965 if (ignoreViewport)
1966 {
1967 viewport.X = 0;
1968 viewport.Y = 0;
1969 viewport.Width = mRenderTargetDesc.Width;
1970 viewport.Height = mRenderTargetDesc.Height;
1971 viewport.MinZ = 0.0f;
1972 viewport.MaxZ = 1.0f;
1973 }
1974 else
1975 {
1976 viewport.X = clamp(mState.viewportX, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1977 viewport.Y = clamp(mState.viewportY, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1978 viewport.Width = clamp(mState.viewportWidth, 0L, static_cast<LONG>(mRenderTargetDesc.Width) - static_cast<LONG>(viewport.X));
1979 viewport.Height = clamp(mState.viewportHeight, 0L, static_cast<LONG>(mRenderTargetDesc.Height) - static_cast<LONG>(viewport.Y));
1980 viewport.MinZ = zNear;
1981 viewport.MaxZ = zFar;
1982 }
1983
1984 if (viewport.Width <= 0 || viewport.Height <= 0)
1985 {
1986 return false; // Nothing to render
1987 }
1988
1989 if (renderTargetChanged || !mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
1990 {
1991 mDevice->SetViewport(&viewport);
1992 mSetViewport = viewport;
1993 mViewportInitialized = true;
1994 mDxUniformsDirty = true;
1995 }
1996
1997 if (mScissorStateDirty)
1998 {
1999 if (mState.scissorTest)
2000 {
2001 RECT rect;
2002 rect.left = clamp(mState.scissorX, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
2003 rect.top = clamp(mState.scissorY, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
2004 rect.right = clamp(mState.scissorX + mState.scissorWidth, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
2005 rect.bottom = clamp(mState.scissorY + mState.scissorHeight, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
2006 mDevice->SetScissorRect(&rect);
2007 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
2008 }
2009 else
2010 {
2011 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
2012 }
2013
2014 mScissorStateDirty = false;
2015 }
2016
2017 if (mState.currentProgram && mDxUniformsDirty)
2018 {
daniel@transgaming.com62a28462012-07-24 18:33:59 +00002019 ProgramBinary *programBinary = getCurrentProgramBinary();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002020
2021 GLint halfPixelSize = programBinary->getDxHalfPixelSizeLocation();
2022 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
2023 programBinary->setUniform2fv(halfPixelSize, 1, xy);
2024
2025 // These values are used for computing gl_FragCoord in Program::linkVaryings().
2026 GLint coord = programBinary->getDxCoordLocation();
2027 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
2028 (float)mState.viewportX + mState.viewportWidth / 2.0f,
2029 (float)mState.viewportY + mState.viewportHeight / 2.0f};
2030 programBinary->setUniform4fv(coord, 1, whxy);
2031
2032 GLint depth = programBinary->getDxDepthLocation();
2033 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
2034 programBinary->setUniform2fv(depth, 1, dz);
2035
2036 GLint depthRange = programBinary->getDxDepthRangeLocation();
2037 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
2038 programBinary->setUniform3fv(depthRange, 1, nearFarDiff);
2039 mDxUniformsDirty = false;
2040 }
2041
2042 return true;
2043}
2044
2045// Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc) to the Direct3D 9 device
2046void Context::applyState(GLenum drawMode)
2047{
daniel@transgaming.com62a28462012-07-24 18:33:59 +00002048 ProgramBinary *programBinary = getCurrentProgramBinary();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002049
2050 Framebuffer *framebufferObject = getDrawFramebuffer();
2051
2052 GLint frontCCW = programBinary->getDxFrontCCWLocation();
2053 GLint ccw = (mState.frontFace == GL_CCW);
2054 programBinary->setUniform1iv(frontCCW, 1, &ccw);
2055
2056 GLint pointsOrLines = programBinary->getDxPointsOrLinesLocation();
2057 GLint alwaysFront = !isTriangleMode(drawMode);
2058 programBinary->setUniform1iv(pointsOrLines, 1, &alwaysFront);
2059
2060 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
2061 bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
2062 // Apparently some ATI cards have a bug where a draw with a zero color
2063 // write mask can cause later draws to have incorrect results. Instead,
2064 // set a nonzero color write mask but modify the blend state so that no
2065 // drawing is done.
2066 // http://code.google.com/p/angleproject/issues/detail?id=169
2067
2068 if (mCullStateDirty || mFrontFaceDirty)
2069 {
2070 if (mState.cullFace)
2071 {
2072 mDevice->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, mState.frontFace));
2073 }
2074 else
2075 {
2076 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2077 }
2078
2079 mCullStateDirty = false;
2080 }
2081
2082 if (mDepthStateDirty)
2083 {
2084 if (mState.depthTest)
2085 {
2086 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
2087 mDevice->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
2088 }
2089 else
2090 {
2091 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
2092 }
2093
2094 mDepthStateDirty = false;
2095 }
2096
2097 if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
2098 {
2099 mBlendStateDirty = true;
2100 mMaskStateDirty = true;
2101 }
2102
2103 if (mBlendStateDirty)
2104 {
2105 if (mState.blend)
2106 {
2107 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
2108
2109 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
2110 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
2111 {
2112 mDevice->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
2113 }
2114 else
2115 {
2116 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
2117 unorm<8>(mState.blendColor.alpha),
2118 unorm<8>(mState.blendColor.alpha),
2119 unorm<8>(mState.blendColor.alpha)));
2120 }
2121
2122 mDevice->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
2123 mDevice->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
2124 mDevice->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
2125
2126 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
2127 mState.destBlendRGB != mState.destBlendAlpha ||
2128 mState.blendEquationRGB != mState.blendEquationAlpha)
2129 {
2130 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2131
2132 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
2133 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
2134 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
2135 }
2136 else
2137 {
2138 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
2139 }
2140 }
2141 else
2142 {
2143 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2144 }
2145
2146 mBlendStateDirty = false;
2147 }
2148
2149 if (mStencilStateDirty || mFrontFaceDirty)
2150 {
2151 if (mState.stencilTest && framebufferObject->hasStencil())
2152 {
2153 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2154 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
2155
2156 // FIXME: Unsupported by D3D9
2157 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
2158 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
2159 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
2160 if (mState.stencilWritemask != mState.stencilBackWritemask ||
2161 mState.stencilRef != mState.stencilBackRef ||
2162 mState.stencilMask != mState.stencilBackMask)
2163 {
2164 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
2165 return error(GL_INVALID_OPERATION);
2166 }
2167
2168 // get the maximum size of the stencil ref
2169 gl::Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
2170 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
2171
2172 mDevice->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
2173 mDevice->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
2174 es2dx::ConvertComparison(mState.stencilFunc));
2175
2176 mDevice->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
2177 mDevice->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
2178
2179 mDevice->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
2180 es2dx::ConvertStencilOp(mState.stencilFail));
2181 mDevice->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
2182 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
2183 mDevice->SetRenderState(mState.frontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
2184 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
2185
2186 mDevice->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
2187 mDevice->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
2188 es2dx::ConvertComparison(mState.stencilBackFunc));
2189
2190 mDevice->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
2191 mDevice->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
2192
2193 mDevice->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
2194 es2dx::ConvertStencilOp(mState.stencilBackFail));
2195 mDevice->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
2196 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
2197 mDevice->SetRenderState(mState.frontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
2198 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
2199 }
2200 else
2201 {
2202 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2203 }
2204
2205 mStencilStateDirty = false;
2206 mFrontFaceDirty = false;
2207 }
2208
2209 if (mMaskStateDirty)
2210 {
2211 int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
2212 mState.colorMaskBlue, mState.colorMaskAlpha);
2213 if (colorMask == 0 && !zeroColorMaskAllowed)
2214 {
2215 // Enable green channel, but set blending so nothing will be drawn.
2216 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
2217 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
2218
2219 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
2220 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
2221 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
2222 }
2223 else
2224 {
2225 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
2226 }
2227 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
2228
2229 mMaskStateDirty = false;
2230 }
2231
2232 if (mPolygonOffsetStateDirty)
2233 {
2234 if (mState.polygonOffsetFill)
2235 {
2236 gl::Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
2237 if (depthbuffer)
2238 {
2239 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
2240 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
2241 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
2242 }
2243 }
2244 else
2245 {
2246 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
2247 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
2248 }
2249
2250 mPolygonOffsetStateDirty = false;
2251 }
2252
2253 if (mSampleStateDirty)
2254 {
2255 if (mState.sampleAlphaToCoverage)
2256 {
2257 FIXME("Sample alpha to coverage is unimplemented.");
2258 }
2259
2260 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
2261 if (mState.sampleCoverage)
2262 {
2263 unsigned int mask = 0;
2264 if (mState.sampleCoverageValue != 0)
2265 {
2266 float threshold = 0.5f;
2267
2268 for (int i = 0; i < framebufferObject->getSamples(); ++i)
2269 {
2270 mask <<= 1;
2271
2272 if ((i + 1) * mState.sampleCoverageValue >= threshold)
2273 {
2274 threshold += 1.0f;
2275 mask |= 1;
2276 }
2277 }
2278 }
2279
2280 if (mState.sampleCoverageInvert)
2281 {
2282 mask = ~mask;
2283 }
2284
2285 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
2286 }
2287 else
2288 {
2289 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
2290 }
2291
2292 mSampleStateDirty = false;
2293 }
2294
2295 if (mDitherStateDirty)
2296 {
2297 mDevice->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
2298
2299 mDitherStateDirty = false;
2300 }
2301}
2302
2303GLenum Context::applyVertexBuffer(GLint first, GLsizei count, GLsizei instances, GLsizei *repeatDraw)
2304{
2305 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
2306
2307 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes, instances);
2308 if (err != GL_NO_ERROR)
2309 {
2310 return err;
2311 }
2312
daniel@transgaming.com62a28462012-07-24 18:33:59 +00002313 ProgramBinary *programBinary = getCurrentProgramBinary();
daniel@transgaming.com5ae3ccc2012-07-24 18:29:38 +00002314 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, programBinary, instances, repeatDraw);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002315}
2316
2317// Applies the indices and element array bindings to the Direct3D 9 device
2318GLenum Context::applyIndexBuffer(const GLvoid *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
2319{
2320 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
2321
2322 if (err == GL_NO_ERROR)
2323 {
2324 if (indexInfo->serial != mAppliedIBSerial)
2325 {
2326 mDevice->SetIndices(indexInfo->indexBuffer);
2327 mAppliedIBSerial = indexInfo->serial;
2328 }
2329 }
2330
2331 return err;
2332}
2333
2334// Applies the shaders and shader constants to the Direct3D 9 device
2335void Context::applyShaders()
2336{
daniel@transgaming.com62a28462012-07-24 18:33:59 +00002337 ProgramBinary *programBinary = getCurrentProgramBinary();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002338
daniel@transgaming.come6af4f92012-07-24 18:31:31 +00002339 if (programBinary->getSerial() != mAppliedProgramBinarySerial)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002340 {
2341 IDirect3DVertexShader9 *vertexShader = programBinary->getVertexShader();
2342 IDirect3DPixelShader9 *pixelShader = programBinary->getPixelShader();
2343
2344 mDevice->SetPixelShader(pixelShader);
2345 mDevice->SetVertexShader(vertexShader);
2346 programBinary->dirtyAllUniforms();
daniel@transgaming.come6af4f92012-07-24 18:31:31 +00002347 mAppliedProgramBinarySerial = programBinary->getSerial();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002348 }
2349
2350 programBinary->applyUniforms();
2351}
2352
2353// Applies the textures and sampler states to the Direct3D 9 device
2354void Context::applyTextures()
2355{
2356 applyTextures(SAMPLER_PIXEL);
2357
2358 if (mSupportsVertexTexture)
2359 {
2360 applyTextures(SAMPLER_VERTEX);
2361 }
2362}
2363
2364// For each Direct3D 9 sampler of either the pixel or vertex stage,
2365// looks up the corresponding OpenGL texture image unit and texture type,
2366// and sets the texture and its addressing/filtering state (or NULL when inactive).
2367void Context::applyTextures(SamplerType type)
2368{
daniel@transgaming.com62a28462012-07-24 18:33:59 +00002369 ProgramBinary *programBinary = getCurrentProgramBinary();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002370
2371 int samplerCount = (type == SAMPLER_PIXEL) ? MAX_TEXTURE_IMAGE_UNITS : MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; // Range of Direct3D 9 samplers of given sampler type
2372 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2373 int d3dSamplerOffset = (type == SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
2374 int samplerRange = programBinary->getUsedSamplerRange(type);
2375
2376 for (int samplerIndex = 0; samplerIndex < samplerRange; samplerIndex++)
2377 {
2378 int textureUnit = programBinary->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
2379 int d3dSampler = samplerIndex + d3dSamplerOffset;
2380
2381 if (textureUnit != -1)
2382 {
2383 TextureType textureType = programBinary->getSamplerTextureType(type, samplerIndex);
2384
2385 Texture *texture = getSamplerTexture(textureUnit, textureType);
2386 unsigned int texSerial = texture->getTextureSerial();
2387
2388 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyParameters() || texture->hasDirtyImages())
2389 {
2390 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2391
2392 if (d3dTexture)
2393 {
2394 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyParameters())
2395 {
2396 GLenum wrapS = texture->getWrapS();
2397 GLenum wrapT = texture->getWrapT();
2398 GLenum minFilter = texture->getMinFilter();
2399 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00002400 float maxAnisotropy = texture->getMaxAnisotropy();
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002401
2402 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2403 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
2404
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00002405 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter, maxAnisotropy));
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002406 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00002407 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter, maxAnisotropy);
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002408 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2409 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
jbauman@chromium.org68715282012-07-12 23:28:41 +00002410 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXMIPLEVEL, texture->getLodOffset());
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00002411
2412 if (supportsTextureFilterAnisotropy())
2413 {
2414 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAXANISOTROPY, (DWORD)maxAnisotropy);
2415 }
apatrick@chromium.org144f2802012-07-12 01:42:34 +00002416 }
2417
2418 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyImages())
2419 {
2420 mDevice->SetTexture(d3dSampler, d3dTexture);
2421 }
2422 }
2423 else
2424 {
2425 mDevice->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
2426 }
2427
2428 appliedTextureSerial[samplerIndex] = texSerial;
2429 texture->resetDirty();
2430 }
2431 }
2432 else
2433 {
2434 if (appliedTextureSerial[samplerIndex] != 0)
2435 {
2436 mDevice->SetTexture(d3dSampler, NULL);
2437 appliedTextureSerial[samplerIndex] = 0;
2438 }
2439 }
2440 }
2441
2442 for (int samplerIndex = samplerRange; samplerIndex < samplerCount; samplerIndex++)
2443 {
2444 if (appliedTextureSerial[samplerIndex] != 0)
2445 {
2446 mDevice->SetTexture(samplerIndex + d3dSamplerOffset, NULL);
2447 appliedTextureSerial[samplerIndex] = 0;
2448 }
2449 }
2450}
2451
2452void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height,
2453 GLenum format, GLenum type, GLsizei *bufSize, void* pixels)
2454{
2455 Framebuffer *framebuffer = getReadFramebuffer();
2456
2457 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2458 {
2459 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2460 }
2461
2462 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2463 {
2464 return error(GL_INVALID_OPERATION);
2465 }
2466
2467 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
2468 // sized query sanity check
2469 if (bufSize)
2470 {
2471 int requiredSize = outputPitch * height;
2472 if (requiredSize > *bufSize)
2473 {
2474 return error(GL_INVALID_OPERATION);
2475 }
2476 }
2477
2478 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
2479 if (!renderTarget)
2480 {
2481 return; // Context must be lost, return silently
2482 }
2483
2484 D3DSURFACE_DESC desc;
2485 renderTarget->GetDesc(&desc);
2486
2487 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2488 {
2489 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
2490 renderTarget->Release();
2491 return error(GL_OUT_OF_MEMORY);
2492 }
2493
2494 HRESULT result;
2495 IDirect3DSurface9 *systemSurface = NULL;
2496 bool directToPixels = !getPackReverseRowOrder() && getPackAlignment() <= 4 && mDisplay->isD3d9ExDevice() &&
2497 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
2498 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2499 if (directToPixels)
2500 {
2501 // Use the pixels ptr as a shared handle to write directly into client's memory
2502 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2503 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2504 if (FAILED(result))
2505 {
2506 // Try again without the shared handle
2507 directToPixels = false;
2508 }
2509 }
2510
2511 if (!directToPixels)
2512 {
2513 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2514 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2515 if (FAILED(result))
2516 {
2517 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2518 renderTarget->Release();
2519 return error(GL_OUT_OF_MEMORY);
2520 }
2521 }
2522
2523 result = mDevice->GetRenderTargetData(renderTarget, systemSurface);
2524 renderTarget->Release();
2525 renderTarget = NULL;
2526
2527 if (FAILED(result))
2528 {
2529 systemSurface->Release();
2530
2531 // It turns out that D3D will sometimes produce more error
2532 // codes than those documented.
2533 if (checkDeviceLost(result))
2534 return error(GL_OUT_OF_MEMORY);
2535 else
2536 {
2537 UNREACHABLE();
2538 return;
2539 }
2540
2541 }
2542
2543 if (directToPixels)
2544 {
2545 systemSurface->Release();
2546 return;
2547 }
2548
2549 RECT rect;
2550 rect.left = clamp(x, 0L, static_cast<LONG>(desc.Width));
2551 rect.top = clamp(y, 0L, static_cast<LONG>(desc.Height));
2552 rect.right = clamp(x + width, 0L, static_cast<LONG>(desc.Width));
2553 rect.bottom = clamp(y + height, 0L, static_cast<LONG>(desc.Height));
2554
2555 D3DLOCKED_RECT lock;
2556 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2557
2558 if (FAILED(result))
2559 {
2560 UNREACHABLE();
2561 systemSurface->Release();
2562
2563 return; // No sensible error to generate
2564 }
2565
2566 unsigned char *dest = (unsigned char*)pixels;
2567 unsigned short *dest16 = (unsigned short*)pixels;
2568
2569 unsigned char *source;
2570 int inputPitch;
2571 if (getPackReverseRowOrder())
2572 {
2573 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2574 inputPitch = -lock.Pitch;
2575 }
2576 else
2577 {
2578 source = (unsigned char*)lock.pBits;
2579 inputPitch = lock.Pitch;
2580 }
2581
2582 for (int j = 0; j < rect.bottom - rect.top; j++)
2583 {
2584 if (desc.Format == D3DFMT_A8R8G8B8 &&
2585 format == GL_BGRA_EXT &&
2586 type == GL_UNSIGNED_BYTE)
2587 {
2588 // Fast path for EXT_read_format_bgra, given
2589 // an RGBA source buffer. Note that buffers with no
2590 // alpha go through the slow path below.
2591 memcpy(dest + j * outputPitch,
2592 source + j * inputPitch,
2593 (rect.right - rect.left) * 4);
2594 continue;
2595 }
2596
2597 for (int i = 0; i < rect.right - rect.left; i++)
2598 {
2599 float r;
2600 float g;
2601 float b;
2602 float a;
2603
2604 switch (desc.Format)
2605 {
2606 case D3DFMT_R5G6B5:
2607 {
2608 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2609
2610 a = 1.0f;
2611 b = (rgb & 0x001F) * (1.0f / 0x001F);
2612 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2613 r = (rgb & 0xF800) * (1.0f / 0xF800);
2614 }
2615 break;
2616 case D3DFMT_A1R5G5B5:
2617 {
2618 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
2619
2620 a = (argb & 0x8000) ? 1.0f : 0.0f;
2621 b = (argb & 0x001F) * (1.0f / 0x001F);
2622 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2623 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2624 }
2625 break;
2626 case D3DFMT_A8R8G8B8:
2627 {
2628 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2629
2630 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2631 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2632 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2633 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2634 }
2635 break;
2636 case D3DFMT_X8R8G8B8:
2637 {
2638 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2639
2640 a = 1.0f;
2641 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2642 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2643 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2644 }
2645 break;
2646 case D3DFMT_A2R10G10B10:
2647 {
2648 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
2649
2650 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2651 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2652 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2653 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2654 }
2655 break;
2656 case D3DFMT_A32B32G32R32F:
2657 {
2658 // float formats in D3D are stored rgba, rather than the other way round
2659 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2660 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2661 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2662 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
2663 }
2664 break;
2665 case D3DFMT_A16B16G16R16F:
2666 {
2667 // float formats in D3D are stored rgba, rather than the other way round
2668 float abgr[4];
2669
2670 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
2671
2672 a = abgr[3];
2673 b = abgr[2];
2674 g = abgr[1];
2675 r = abgr[0];
2676 }
2677 break;
2678 default:
2679 UNIMPLEMENTED(); // FIXME
2680 UNREACHABLE();
2681 return;
2682 }
2683
2684 switch (format)
2685 {
2686 case GL_RGBA:
2687 switch (type)
2688 {
2689 case GL_UNSIGNED_BYTE:
2690 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2691 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2692 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2693 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2694 break;
2695 default: UNREACHABLE();
2696 }
2697 break;
2698 case GL_BGRA_EXT:
2699 switch (type)
2700 {
2701 case GL_UNSIGNED_BYTE:
2702 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2703 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2704 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2705 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2706 break;
2707 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2708 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2709 // this type is packed as follows:
2710 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2711 // --------------------------------------------------------------------------------
2712 // | 4th | 3rd | 2nd | 1st component |
2713 // --------------------------------------------------------------------------------
2714 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2715 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2716 ((unsigned short)(15 * a + 0.5f) << 12)|
2717 ((unsigned short)(15 * r + 0.5f) << 8) |
2718 ((unsigned short)(15 * g + 0.5f) << 4) |
2719 ((unsigned short)(15 * b + 0.5f) << 0);
2720 break;
2721 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2722 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2723 // this type is packed as follows:
2724 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2725 // --------------------------------------------------------------------------------
2726 // | 4th | 3rd | 2nd | 1st component |
2727 // --------------------------------------------------------------------------------
2728 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2729 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2730 ((unsigned short)( a + 0.5f) << 15) |
2731 ((unsigned short)(31 * r + 0.5f) << 10) |
2732 ((unsigned short)(31 * g + 0.5f) << 5) |
2733 ((unsigned short)(31 * b + 0.5f) << 0);
2734 break;
2735 default: UNREACHABLE();
2736 }
2737 break;
2738 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
2739 switch (type)
2740 {
2741 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
2742 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2743 ((unsigned short)(31 * b + 0.5f) << 0) |
2744 ((unsigned short)(63 * g + 0.5f) << 5) |
2745 ((unsigned short)(31 * r + 0.5f) << 11);
2746 break;
2747 default: UNREACHABLE();
2748 }
2749 break;
2750 default: UNREACHABLE();
2751 }
2752 }
2753 }
2754
2755 systemSurface->UnlockRect();
2756
2757 systemSurface->Release();
2758}
2759
2760void Context::clear(GLbitfield mask)
2761{
2762 Framebuffer *framebufferObject = getDrawFramebuffer();
2763
2764 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2765 {
2766 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2767 }
2768
2769 DWORD flags = 0;
2770
2771 if (mask & GL_COLOR_BUFFER_BIT)
2772 {
2773 mask &= ~GL_COLOR_BUFFER_BIT;
2774
2775 if (framebufferObject->getColorbufferType() != GL_NONE)
2776 {
2777 flags |= D3DCLEAR_TARGET;
2778 }
2779 }
2780
2781 if (mask & GL_DEPTH_BUFFER_BIT)
2782 {
2783 mask &= ~GL_DEPTH_BUFFER_BIT;
2784 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
2785 {
2786 flags |= D3DCLEAR_ZBUFFER;
2787 }
2788 }
2789
2790 GLuint stencilUnmasked = 0x0;
2791
2792 if (mask & GL_STENCIL_BUFFER_BIT)
2793 {
2794 mask &= ~GL_STENCIL_BUFFER_BIT;
2795 if (framebufferObject->getStencilbufferType() != GL_NONE)
2796 {
2797 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
2798 if (!depthStencil)
2799 {
2800 ERR("Depth stencil pointer unexpectedly null.");
2801 return;
2802 }
2803
2804 D3DSURFACE_DESC desc;
2805 depthStencil->GetDesc(&desc);
2806 depthStencil->Release();
2807
2808 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
2809 stencilUnmasked = (0x1 << stencilSize) - 1;
2810
2811 if (stencilUnmasked != 0x0)
2812 {
2813 flags |= D3DCLEAR_STENCIL;
2814 }
2815 }
2816 }
2817
2818 if (mask != 0)
2819 {
2820 return error(GL_INVALID_VALUE);
2821 }
2822
2823 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2824 {
2825 return;
2826 }
2827
2828 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
2829 unorm<8>(mState.colorClearValue.red),
2830 unorm<8>(mState.colorClearValue.green),
2831 unorm<8>(mState.colorClearValue.blue));
2832 float depth = clamp01(mState.depthClearValue);
2833 int stencil = mState.stencilClearValue & 0x000000FF;
2834
2835 bool alphaUnmasked = (dx2es::GetAlphaSize(mRenderTargetDesc.Format) == 0) || mState.colorMaskAlpha;
2836
2837 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
2838 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
2839 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
2840 !(mState.colorMaskRed && mState.colorMaskGreen &&
2841 mState.colorMaskBlue && alphaUnmasked);
2842
2843 if (needMaskedColorClear || needMaskedStencilClear)
2844 {
2845 // State which is altered in all paths from this point to the clear call is saved.
2846 // State which is altered in only some paths will be flagged dirty in the case that
2847 // that path is taken.
2848 HRESULT hr;
2849 if (mMaskedClearSavedState == NULL)
2850 {
2851 hr = mDevice->BeginStateBlock();
2852 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2853
2854 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2855 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2856 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2857 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2858 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2859 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2860 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2861 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2862 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2863 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2864 mDevice->SetPixelShader(NULL);
2865 mDevice->SetVertexShader(NULL);
2866 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2867 mDevice->SetStreamSource(0, NULL, 0, 0);
2868 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2869 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2870 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2871 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2872 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2873 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2874 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
2875
2876 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2877 {
2878 mDevice->SetStreamSourceFreq(i, 1);
2879 }
2880
2881 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
2882 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2883 }
2884
2885 ASSERT(mMaskedClearSavedState != NULL);
2886
2887 if (mMaskedClearSavedState != NULL)
2888 {
2889 hr = mMaskedClearSavedState->Capture();
2890 ASSERT(SUCCEEDED(hr));
2891 }
2892
2893 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2894 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2895 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2896 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2897 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2898 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2899 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2900 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2901
2902 if (flags & D3DCLEAR_TARGET)
2903 {
2904 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
2905 }
2906 else
2907 {
2908 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2909 }
2910
2911 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2912 {
2913 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2914 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2915 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2916 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
2917 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
2918 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
2919 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2920 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
2921 mStencilStateDirty = true;
2922 }
2923 else
2924 {
2925 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2926 }
2927
2928 mDevice->SetPixelShader(NULL);
2929 mDevice->SetVertexShader(NULL);
2930 mDevice->SetFVF(D3DFVF_XYZRHW);
2931 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2932 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2933 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2934 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2935 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2936 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2937 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
2938
2939 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2940 {
2941 mDevice->SetStreamSourceFreq(i, 1);
2942 }
2943
2944 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2945 quad[0][0] = -0.5f;
2946 quad[0][1] = mRenderTargetDesc.Height - 0.5f;
2947 quad[0][2] = 0.0f;
2948 quad[0][3] = 1.0f;
2949
2950 quad[1][0] = mRenderTargetDesc.Width - 0.5f;
2951 quad[1][1] = mRenderTargetDesc.Height - 0.5f;
2952 quad[1][2] = 0.0f;
2953 quad[1][3] = 1.0f;
2954
2955 quad[2][0] = -0.5f;
2956 quad[2][1] = -0.5f;
2957 quad[2][2] = 0.0f;
2958 quad[2][3] = 1.0f;
2959
2960 quad[3][0] = mRenderTargetDesc.Width - 0.5f;
2961 quad[3][1] = -0.5f;
2962 quad[3][2] = 0.0f;
2963 quad[3][3] = 1.0f;
2964
2965 mDisplay->startScene();
2966 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
2967
2968 if (flags & D3DCLEAR_ZBUFFER)
2969 {
2970 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
2971 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2972 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
2973 }
2974
2975 if (mMaskedClearSavedState != NULL)
2976 {
2977 mMaskedClearSavedState->Apply();
2978 }
2979 }
2980 else if (flags)
2981 {
2982 mDevice->Clear(0, NULL, flags, color, depth, stencil);
2983 }
2984}
2985
2986void Context::drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instances)
2987{
2988 if (!mState.currentProgram)
2989 {
2990 return error(GL_INVALID_OPERATION);
2991 }
2992
2993 D3DPRIMITIVETYPE primitiveType;
2994 int primitiveCount;
2995
2996 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2997 return error(GL_INVALID_ENUM);
2998
2999 if (primitiveCount <= 0)
3000 {
3001 return;
3002 }
3003
3004 if (!applyRenderTarget(false))
3005 {
3006 return;
3007 }
3008
3009 applyState(mode);
3010
3011 GLsizei repeatDraw = 1;
3012 GLenum err = applyVertexBuffer(first, count, instances, &repeatDraw);
3013 if (err != GL_NO_ERROR)
3014 {
3015 return error(err);
3016 }
3017
3018 applyShaders();
3019 applyTextures();
3020
daniel@transgaming.com62a28462012-07-24 18:33:59 +00003021 if (!getCurrentProgramBinary()->validateSamplers(NULL))
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003022 {
3023 return error(GL_INVALID_OPERATION);
3024 }
3025
3026 if (!cullSkipsDraw(mode))
3027 {
3028 mDisplay->startScene();
3029
3030 if (mode == GL_LINE_LOOP)
3031 {
3032 drawLineLoop(count, GL_NONE, NULL, 0);
3033 }
3034 else if (instances > 0)
3035 {
3036 StaticIndexBuffer *countingIB = mIndexDataManager->getCountingIndices(count);
3037 if (countingIB)
3038 {
3039 if (mAppliedIBSerial != countingIB->getSerial())
3040 {
3041 mDevice->SetIndices(countingIB->getBuffer());
3042 mAppliedIBSerial = countingIB->getSerial();
3043 }
3044
3045 for (int i = 0; i < repeatDraw; i++)
3046 {
3047 mDevice->DrawIndexedPrimitive(primitiveType, 0, 0, count, 0, primitiveCount);
3048 }
3049 }
3050 else
3051 {
3052 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
3053 return error(GL_OUT_OF_MEMORY);
3054 }
3055 }
3056 else // Regular case
3057 {
3058 mDevice->DrawPrimitive(primitiveType, 0, primitiveCount);
3059 }
3060 }
3061}
3062
3063void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, GLsizei instances)
3064{
3065 if (!mState.currentProgram)
3066 {
3067 return error(GL_INVALID_OPERATION);
3068 }
3069
3070 if (!indices && !mState.elementArrayBuffer)
3071 {
3072 return error(GL_INVALID_OPERATION);
3073 }
3074
3075 D3DPRIMITIVETYPE primitiveType;
3076 int primitiveCount;
3077
3078 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
3079 return error(GL_INVALID_ENUM);
3080
3081 if (primitiveCount <= 0)
3082 {
3083 return;
3084 }
3085
3086 if (!applyRenderTarget(false))
3087 {
3088 return;
3089 }
3090
3091 applyState(mode);
3092
3093 TranslatedIndexData indexInfo;
3094 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
3095 if (err != GL_NO_ERROR)
3096 {
3097 return error(err);
3098 }
3099
3100 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
3101 GLsizei repeatDraw = 1;
3102 err = applyVertexBuffer(indexInfo.minIndex, vertexCount, instances, &repeatDraw);
3103 if (err != GL_NO_ERROR)
3104 {
3105 return error(err);
3106 }
3107
3108 applyShaders();
3109 applyTextures();
3110
daniel@transgaming.com62a28462012-07-24 18:33:59 +00003111 if (!getCurrentProgramBinary()->validateSamplers(false))
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003112 {
3113 return error(GL_INVALID_OPERATION);
3114 }
3115
3116 if (!cullSkipsDraw(mode))
3117 {
3118 mDisplay->startScene();
3119
3120 if (mode == GL_LINE_LOOP)
3121 {
3122 drawLineLoop(count, type, indices, indexInfo.minIndex);
3123 }
3124 else
3125 {
3126 for (int i = 0; i < repeatDraw; i++)
3127 {
3128 mDevice->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
3129 }
3130 }
3131 }
3132}
3133
3134// Implements glFlush when block is false, glFinish when block is true
3135void Context::sync(bool block)
3136{
3137 mDisplay->sync(block);
3138}
3139
3140void Context::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex)
3141{
3142 // Get the raw indices for an indexed draw
3143 if (type != GL_NONE && mState.elementArrayBuffer.get())
3144 {
3145 Buffer *indexBuffer = mState.elementArrayBuffer.get();
3146 intptr_t offset = reinterpret_cast<intptr_t>(indices);
3147 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
3148 }
3149
3150 UINT startIndex = 0;
3151 bool succeeded = false;
3152
3153 if (supports32bitIndices())
3154 {
3155 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
3156
3157 if (!mLineLoopIB)
3158 {
3159 mLineLoopIB = new StreamingIndexBuffer(mDevice, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
3160 }
3161
3162 if (mLineLoopIB)
3163 {
3164 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
3165
3166 UINT offset = 0;
3167 unsigned int *data = static_cast<unsigned int*>(mLineLoopIB->map(spaceNeeded, &offset));
3168 startIndex = offset / 4;
3169
3170 if (data)
3171 {
3172 switch (type)
3173 {
3174 case GL_NONE: // Non-indexed draw
3175 for (int i = 0; i < count; i++)
3176 {
3177 data[i] = i;
3178 }
3179 data[count] = 0;
3180 break;
3181 case GL_UNSIGNED_BYTE:
3182 for (int i = 0; i < count; i++)
3183 {
3184 data[i] = static_cast<const GLubyte*>(indices)[i];
3185 }
3186 data[count] = static_cast<const GLubyte*>(indices)[0];
3187 break;
3188 case GL_UNSIGNED_SHORT:
3189 for (int i = 0; i < count; i++)
3190 {
3191 data[i] = static_cast<const GLushort*>(indices)[i];
3192 }
3193 data[count] = static_cast<const GLushort*>(indices)[0];
3194 break;
3195 case GL_UNSIGNED_INT:
3196 for (int i = 0; i < count; i++)
3197 {
3198 data[i] = static_cast<const GLuint*>(indices)[i];
3199 }
3200 data[count] = static_cast<const GLuint*>(indices)[0];
3201 break;
3202 default: UNREACHABLE();
3203 }
3204
3205 mLineLoopIB->unmap();
3206 succeeded = true;
3207 }
3208 }
3209 }
3210 else
3211 {
3212 const int spaceNeeded = (count + 1) * sizeof(unsigned short);
3213
3214 if (!mLineLoopIB)
3215 {
3216 mLineLoopIB = new StreamingIndexBuffer(mDevice, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
3217 }
3218
3219 if (mLineLoopIB)
3220 {
3221 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
3222
3223 UINT offset = 0;
3224 unsigned short *data = static_cast<unsigned short*>(mLineLoopIB->map(spaceNeeded, &offset));
3225 startIndex = offset / 2;
3226
3227 if (data)
3228 {
3229 switch (type)
3230 {
3231 case GL_NONE: // Non-indexed draw
3232 for (int i = 0; i < count; i++)
3233 {
3234 data[i] = i;
3235 }
3236 data[count] = 0;
3237 break;
3238 case GL_UNSIGNED_BYTE:
3239 for (int i = 0; i < count; i++)
3240 {
3241 data[i] = static_cast<const GLubyte*>(indices)[i];
3242 }
3243 data[count] = static_cast<const GLubyte*>(indices)[0];
3244 break;
3245 case GL_UNSIGNED_SHORT:
3246 for (int i = 0; i < count; i++)
3247 {
3248 data[i] = static_cast<const GLushort*>(indices)[i];
3249 }
3250 data[count] = static_cast<const GLushort*>(indices)[0];
3251 break;
3252 case GL_UNSIGNED_INT:
3253 for (int i = 0; i < count; i++)
3254 {
3255 data[i] = static_cast<const GLuint*>(indices)[i];
3256 }
3257 data[count] = static_cast<const GLuint*>(indices)[0];
3258 break;
3259 default: UNREACHABLE();
3260 }
3261
3262 mLineLoopIB->unmap();
3263 succeeded = true;
3264 }
3265 }
3266 }
3267
3268 if (succeeded)
3269 {
3270 if (mAppliedIBSerial != mLineLoopIB->getSerial())
3271 {
3272 mDevice->SetIndices(mLineLoopIB->getBuffer());
3273 mAppliedIBSerial = mLineLoopIB->getSerial();
3274 }
3275
3276 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
3277 }
3278 else
3279 {
3280 ERR("Could not create a looping index buffer for GL_LINE_LOOP.");
3281 return error(GL_OUT_OF_MEMORY);
3282 }
3283}
3284
3285void Context::recordInvalidEnum()
3286{
3287 mInvalidEnum = true;
3288}
3289
3290void Context::recordInvalidValue()
3291{
3292 mInvalidValue = true;
3293}
3294
3295void Context::recordInvalidOperation()
3296{
3297 mInvalidOperation = true;
3298}
3299
3300void Context::recordOutOfMemory()
3301{
3302 mOutOfMemory = true;
3303}
3304
3305void Context::recordInvalidFramebufferOperation()
3306{
3307 mInvalidFramebufferOperation = true;
3308}
3309
3310// Get one of the recorded errors and clear its flag, if any.
3311// [OpenGL ES 2.0.24] section 2.5 page 13.
3312GLenum Context::getError()
3313{
3314 if (mInvalidEnum)
3315 {
3316 mInvalidEnum = false;
3317
3318 return GL_INVALID_ENUM;
3319 }
3320
3321 if (mInvalidValue)
3322 {
3323 mInvalidValue = false;
3324
3325 return GL_INVALID_VALUE;
3326 }
3327
3328 if (mInvalidOperation)
3329 {
3330 mInvalidOperation = false;
3331
3332 return GL_INVALID_OPERATION;
3333 }
3334
3335 if (mOutOfMemory)
3336 {
3337 mOutOfMemory = false;
3338
3339 return GL_OUT_OF_MEMORY;
3340 }
3341
3342 if (mInvalidFramebufferOperation)
3343 {
3344 mInvalidFramebufferOperation = false;
3345
3346 return GL_INVALID_FRAMEBUFFER_OPERATION;
3347 }
3348
3349 return GL_NO_ERROR;
3350}
3351
3352GLenum Context::getResetStatus()
3353{
3354 if (mResetStatus == GL_NO_ERROR)
3355 {
3356 bool lost = mDisplay->testDeviceLost();
3357
3358 if (lost)
3359 {
3360 mDisplay->notifyDeviceLost(); // Sets mResetStatus
3361 }
3362 }
3363
3364 GLenum status = mResetStatus;
3365
3366 if (mResetStatus != GL_NO_ERROR)
3367 {
3368 if (mDisplay->testDeviceResettable())
3369 {
3370 mResetStatus = GL_NO_ERROR;
3371 }
3372 }
3373
3374 return status;
3375}
3376
3377bool Context::isResetNotificationEnabled()
3378{
3379 return (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT);
3380}
3381
3382bool Context::supportsShaderModel3() const
3383{
3384 return mSupportsShaderModel3;
3385}
3386
3387float Context::getMaximumPointSize() const
3388{
3389 return mSupportsShaderModel3 ? mMaximumPointSize : ALIASED_POINT_SIZE_RANGE_MAX_SM2;
3390}
3391
3392int Context::getMaximumVaryingVectors() const
3393{
3394 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3395}
3396
3397unsigned int Context::getMaximumVertexTextureImageUnits() const
3398{
3399 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3400}
3401
3402unsigned int Context::getMaximumCombinedTextureImageUnits() const
3403{
3404 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3405}
3406
3407int Context::getMaximumFragmentUniformVectors() const
3408{
3409 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3410}
3411
3412int Context::getMaxSupportedSamples() const
3413{
3414 return mMaxSupportedSamples;
3415}
3416
3417int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3418{
3419 if (requested == 0)
3420 {
3421 return requested;
3422 }
3423
3424 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3425 if (itr == mMultiSampleSupport.end())
3426 {
3427 return -1;
3428 }
3429
3430 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3431 {
3432 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3433 {
3434 return i;
3435 }
3436 }
3437
3438 return -1;
3439}
3440
3441bool Context::supportsEventQueries() const
3442{
3443 return mSupportsEventQueries;
3444}
3445
3446bool Context::supportsOcclusionQueries() const
3447{
3448 return mSupportsOcclusionQueries;
3449}
3450
3451bool Context::supportsDXT1Textures() const
3452{
3453 return mSupportsDXT1Textures;
3454}
3455
3456bool Context::supportsDXT3Textures() const
3457{
3458 return mSupportsDXT3Textures;
3459}
3460
3461bool Context::supportsDXT5Textures() const
3462{
3463 return mSupportsDXT5Textures;
3464}
3465
3466bool Context::supportsFloat32Textures() const
3467{
3468 return mSupportsFloat32Textures;
3469}
3470
3471bool Context::supportsFloat32LinearFilter() const
3472{
3473 return mSupportsFloat32LinearFilter;
3474}
3475
3476bool Context::supportsFloat32RenderableTextures() const
3477{
3478 return mSupportsFloat32RenderableTextures;
3479}
3480
3481bool Context::supportsFloat16Textures() const
3482{
3483 return mSupportsFloat16Textures;
3484}
3485
3486bool Context::supportsFloat16LinearFilter() const
3487{
3488 return mSupportsFloat16LinearFilter;
3489}
3490
3491bool Context::supportsFloat16RenderableTextures() const
3492{
3493 return mSupportsFloat16RenderableTextures;
3494}
3495
3496int Context::getMaximumRenderbufferDimension() const
3497{
3498 return mMaxRenderbufferDimension;
3499}
3500
3501int Context::getMaximumTextureDimension() const
3502{
3503 return mMaxTextureDimension;
3504}
3505
3506int Context::getMaximumCubeTextureDimension() const
3507{
3508 return mMaxCubeTextureDimension;
3509}
3510
3511int Context::getMaximumTextureLevel() const
3512{
3513 return mMaxTextureLevel;
3514}
3515
3516bool Context::supportsLuminanceTextures() const
3517{
3518 return mSupportsLuminanceTextures;
3519}
3520
3521bool Context::supportsLuminanceAlphaTextures() const
3522{
3523 return mSupportsLuminanceAlphaTextures;
3524}
3525
3526bool Context::supportsDepthTextures() const
3527{
3528 return mSupportsDepthTextures;
3529}
3530
3531bool Context::supports32bitIndices() const
3532{
3533 return mSupports32bitIndices;
3534}
3535
3536bool Context::supportsNonPower2Texture() const
3537{
3538 return mSupportsNonPower2Texture;
3539}
3540
3541bool Context::supportsInstancing() const
3542{
3543 return mSupportsInstancing;
3544}
3545
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00003546bool Context::supportsTextureFilterAnisotropy() const
3547{
3548 return mSupportsTextureFilterAnisotropy;
3549}
3550
3551float Context::getTextureMaxAnisotropy() const
3552{
3553 return mMaxTextureAnisotropy;
3554}
3555
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003556void Context::detachBuffer(GLuint buffer)
3557{
3558 // [OpenGL ES 2.0.24] section 2.9 page 22:
3559 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3560 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3561
3562 if (mState.arrayBuffer.id() == buffer)
3563 {
3564 mState.arrayBuffer.set(NULL);
3565 }
3566
3567 if (mState.elementArrayBuffer.id() == buffer)
3568 {
3569 mState.elementArrayBuffer.set(NULL);
3570 }
3571
3572 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3573 {
3574 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
3575 {
3576 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
3577 }
3578 }
3579}
3580
3581void Context::detachTexture(GLuint texture)
3582{
3583 // [OpenGL ES 2.0.24] section 3.8 page 84:
3584 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3585 // rebound to texture object zero
3586
3587 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
3588 {
3589 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
3590 {
3591 if (mState.samplerTexture[type][sampler].id() == texture)
3592 {
3593 mState.samplerTexture[type][sampler].set(NULL);
3594 }
3595 }
3596 }
3597
3598 // [OpenGL ES 2.0.24] section 4.4 page 112:
3599 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3600 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3601 // image was attached in the currently bound framebuffer.
3602
3603 Framebuffer *readFramebuffer = getReadFramebuffer();
3604 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3605
3606 if (readFramebuffer)
3607 {
3608 readFramebuffer->detachTexture(texture);
3609 }
3610
3611 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3612 {
3613 drawFramebuffer->detachTexture(texture);
3614 }
3615}
3616
3617void Context::detachFramebuffer(GLuint framebuffer)
3618{
3619 // [OpenGL ES 2.0.24] section 4.4 page 107:
3620 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3621 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3622
3623 if (mState.readFramebuffer == framebuffer)
3624 {
3625 bindReadFramebuffer(0);
3626 }
3627
3628 if (mState.drawFramebuffer == framebuffer)
3629 {
3630 bindDrawFramebuffer(0);
3631 }
3632}
3633
3634void Context::detachRenderbuffer(GLuint renderbuffer)
3635{
3636 // [OpenGL ES 2.0.24] section 4.4 page 109:
3637 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3638 // had been executed with the target RENDERBUFFER and name of zero.
3639
3640 if (mState.renderbuffer.id() == renderbuffer)
3641 {
3642 bindRenderbuffer(0);
3643 }
3644
3645 // [OpenGL ES 2.0.24] section 4.4 page 111:
3646 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3647 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3648 // point to which this image was attached in the currently bound framebuffer.
3649
3650 Framebuffer *readFramebuffer = getReadFramebuffer();
3651 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3652
3653 if (readFramebuffer)
3654 {
3655 readFramebuffer->detachRenderbuffer(renderbuffer);
3656 }
3657
3658 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3659 {
3660 drawFramebuffer->detachRenderbuffer(renderbuffer);
3661 }
3662}
3663
3664Texture *Context::getIncompleteTexture(TextureType type)
3665{
3666 Texture *t = mIncompleteTextures[type].get();
3667
3668 if (t == NULL)
3669 {
3670 static const GLubyte color[] = { 0, 0, 0, 255 };
3671
3672 switch (type)
3673 {
3674 default:
3675 UNREACHABLE();
3676 // default falls through to TEXTURE_2D
3677
3678 case TEXTURE_2D:
3679 {
3680 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
3681 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3682 t = incomplete2d;
3683 }
3684 break;
3685
3686 case TEXTURE_CUBE:
3687 {
3688 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
3689
3690 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3691 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3692 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3693 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3694 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3695 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3696
3697 t = incompleteCube;
3698 }
3699 break;
3700 }
3701
3702 mIncompleteTextures[type].set(t);
3703 }
3704
3705 return t;
3706}
3707
3708bool Context::cullSkipsDraw(GLenum drawMode)
3709{
3710 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
3711}
3712
3713bool Context::isTriangleMode(GLenum drawMode)
3714{
3715 switch (drawMode)
3716 {
3717 case GL_TRIANGLES:
3718 case GL_TRIANGLE_FAN:
3719 case GL_TRIANGLE_STRIP:
3720 return true;
3721 case GL_POINTS:
3722 case GL_LINES:
3723 case GL_LINE_LOOP:
3724 case GL_LINE_STRIP:
3725 return false;
3726 default: UNREACHABLE();
3727 }
3728
3729 return false;
3730}
3731
3732void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3733{
3734 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3735
3736 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3737 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3738 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3739 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
3740
3741 mVertexDataManager->dirtyCurrentValue(index);
3742}
3743
3744void Context::setVertexAttribDivisor(GLuint index, GLuint divisor)
3745{
3746 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3747
3748 mState.vertexAttribute[index].mDivisor = divisor;
3749}
3750
3751// keep list sorted in following order
3752// OES extensions
3753// EXT extensions
3754// Vendor extensions
3755void Context::initExtensionString()
3756{
3757 mExtensionString = "";
3758
3759 // OES extensions
3760 if (supports32bitIndices())
3761 {
3762 mExtensionString += "GL_OES_element_index_uint ";
3763 }
3764
3765 mExtensionString += "GL_OES_packed_depth_stencil ";
3766 mExtensionString += "GL_OES_get_program_binary ";
3767 mExtensionString += "GL_OES_rgb8_rgba8 ";
3768 mExtensionString += "GL_OES_standard_derivatives ";
3769
3770 if (supportsFloat16Textures())
3771 {
3772 mExtensionString += "GL_OES_texture_half_float ";
3773 }
3774 if (supportsFloat16LinearFilter())
3775 {
3776 mExtensionString += "GL_OES_texture_half_float_linear ";
3777 }
3778 if (supportsFloat32Textures())
3779 {
3780 mExtensionString += "GL_OES_texture_float ";
3781 }
3782 if (supportsFloat32LinearFilter())
3783 {
3784 mExtensionString += "GL_OES_texture_float_linear ";
3785 }
3786
3787 if (supportsNonPower2Texture())
3788 {
3789 mExtensionString += "GL_OES_texture_npot ";
3790 }
3791
3792 // Multi-vendor (EXT) extensions
3793 if (supportsOcclusionQueries())
3794 {
3795 mExtensionString += "GL_EXT_occlusion_query_boolean ";
3796 }
3797
3798 mExtensionString += "GL_EXT_read_format_bgra ";
3799 mExtensionString += "GL_EXT_robustness ";
3800
3801 if (supportsDXT1Textures())
3802 {
3803 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3804 }
3805
daniel@transgaming.com07ab8412012-07-12 15:17:09 +00003806 if (supportsTextureFilterAnisotropy())
3807 {
3808 mExtensionString += "GL_EXT_texture_filter_anisotropic ";
3809 }
3810
apatrick@chromium.org144f2802012-07-12 01:42:34 +00003811 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
3812 mExtensionString += "GL_EXT_texture_storage ";
3813
3814 // ANGLE-specific extensions
3815 if (supportsDepthTextures())
3816 {
3817 mExtensionString += "GL_ANGLE_depth_texture ";
3818 }
3819
3820 mExtensionString += "GL_ANGLE_framebuffer_blit ";
3821 if (getMaxSupportedSamples() != 0)
3822 {
3823 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3824 }
3825
3826 if (supportsInstancing())
3827 {
3828 mExtensionString += "GL_ANGLE_instanced_arrays ";
3829 }
3830
3831 mExtensionString += "GL_ANGLE_pack_reverse_row_order ";
3832
3833 if (supportsDXT3Textures())
3834 {
3835 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3836 }
3837 if (supportsDXT5Textures())
3838 {
3839 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
3840 }
3841
3842 mExtensionString += "GL_ANGLE_texture_usage ";
3843 mExtensionString += "GL_ANGLE_translated_shader_source ";
3844
3845 // Other vendor-specific extensions
3846 if (supportsEventQueries())
3847 {
3848 mExtensionString += "GL_NV_fence ";
3849 }
3850
3851 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3852 if (end != std::string::npos)
3853 {
3854 mExtensionString.resize(end+1);
3855 }
3856}
3857
3858const char *Context::getExtensionString() const
3859{
3860 return mExtensionString.c_str();
3861}
3862
3863void Context::initRendererString()
3864{
3865 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
3866
3867 mRendererString = "ANGLE (";
3868 mRendererString += identifier->Description;
3869 mRendererString += ")";
3870}
3871
3872const char *Context::getRendererString() const
3873{
3874 return mRendererString.c_str();
3875}
3876
3877void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3878 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3879 GLbitfield mask)
3880{
3881 Framebuffer *readFramebuffer = getReadFramebuffer();
3882 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3883
3884 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3885 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3886 {
3887 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3888 }
3889
3890 if (drawFramebuffer->getSamples() != 0)
3891 {
3892 return error(GL_INVALID_OPERATION);
3893 }
3894
3895 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3896 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3897 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3898 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3899
3900 RECT sourceRect;
3901 RECT destRect;
3902
3903 if (srcX0 < srcX1)
3904 {
3905 sourceRect.left = srcX0;
3906 sourceRect.right = srcX1;
3907 destRect.left = dstX0;
3908 destRect.right = dstX1;
3909 }
3910 else
3911 {
3912 sourceRect.left = srcX1;
3913 destRect.left = dstX1;
3914 sourceRect.right = srcX0;
3915 destRect.right = dstX0;
3916 }
3917
3918 if (srcY0 < srcY1)
3919 {
3920 sourceRect.bottom = srcY1;
3921 destRect.bottom = dstY1;
3922 sourceRect.top = srcY0;
3923 destRect.top = dstY0;
3924 }
3925 else
3926 {
3927 sourceRect.bottom = srcY0;
3928 destRect.bottom = dstY0;
3929 sourceRect.top = srcY1;
3930 destRect.top = dstY1;
3931 }
3932
3933 RECT sourceScissoredRect = sourceRect;
3934 RECT destScissoredRect = destRect;
3935
3936 if (mState.scissorTest)
3937 {
3938 // Only write to parts of the destination framebuffer which pass the scissor test
3939 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3940 // rect will be checked against scissorY, rather than the bottom.
3941 if (destRect.left < mState.scissorX)
3942 {
3943 int xDiff = mState.scissorX - destRect.left;
3944 destScissoredRect.left = mState.scissorX;
3945 sourceScissoredRect.left += xDiff;
3946 }
3947
3948 if (destRect.right > mState.scissorX + mState.scissorWidth)
3949 {
3950 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3951 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3952 sourceScissoredRect.right -= xDiff;
3953 }
3954
3955 if (destRect.top < mState.scissorY)
3956 {
3957 int yDiff = mState.scissorY - destRect.top;
3958 destScissoredRect.top = mState.scissorY;
3959 sourceScissoredRect.top += yDiff;
3960 }
3961
3962 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3963 {
3964 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3965 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3966 sourceScissoredRect.bottom -= yDiff;
3967 }
3968 }
3969
3970 bool blitRenderTarget = false;
3971 bool blitDepthStencil = false;
3972
3973 RECT sourceTrimmedRect = sourceScissoredRect;
3974 RECT destTrimmedRect = destScissoredRect;
3975
3976 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3977 // the actual draw and read surfaces.
3978 if (sourceTrimmedRect.left < 0)
3979 {
3980 int xDiff = 0 - sourceTrimmedRect.left;
3981 sourceTrimmedRect.left = 0;
3982 destTrimmedRect.left += xDiff;
3983 }
3984
3985 if (sourceTrimmedRect.right > readBufferWidth)
3986 {
3987 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3988 sourceTrimmedRect.right = readBufferWidth;
3989 destTrimmedRect.right -= xDiff;
3990 }
3991
3992 if (sourceTrimmedRect.top < 0)
3993 {
3994 int yDiff = 0 - sourceTrimmedRect.top;
3995 sourceTrimmedRect.top = 0;
3996 destTrimmedRect.top += yDiff;
3997 }
3998
3999 if (sourceTrimmedRect.bottom > readBufferHeight)
4000 {
4001 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
4002 sourceTrimmedRect.bottom = readBufferHeight;
4003 destTrimmedRect.bottom -= yDiff;
4004 }
4005
4006 if (destTrimmedRect.left < 0)
4007 {
4008 int xDiff = 0 - destTrimmedRect.left;
4009 destTrimmedRect.left = 0;
4010 sourceTrimmedRect.left += xDiff;
4011 }
4012
4013 if (destTrimmedRect.right > drawBufferWidth)
4014 {
4015 int xDiff = destTrimmedRect.right - drawBufferWidth;
4016 destTrimmedRect.right = drawBufferWidth;
4017 sourceTrimmedRect.right -= xDiff;
4018 }
4019
4020 if (destTrimmedRect.top < 0)
4021 {
4022 int yDiff = 0 - destTrimmedRect.top;
4023 destTrimmedRect.top = 0;
4024 sourceTrimmedRect.top += yDiff;
4025 }
4026
4027 if (destTrimmedRect.bottom > drawBufferHeight)
4028 {
4029 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
4030 destTrimmedRect.bottom = drawBufferHeight;
4031 sourceTrimmedRect.bottom -= yDiff;
4032 }
4033
4034 bool partialBufferCopy = false;
4035 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
4036 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
4037 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
4038 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
4039 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
4040 {
4041 partialBufferCopy = true;
4042 }
4043
4044 if (mask & GL_COLOR_BUFFER_BIT)
4045 {
4046 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
4047 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
4048 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
4049 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
4050 if (!validReadType || !validDrawType ||
4051 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
4052 {
4053 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
4054 return error(GL_INVALID_OPERATION);
4055 }
4056
4057 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
4058 {
4059 return error(GL_INVALID_OPERATION);
4060 }
4061
4062 blitRenderTarget = true;
4063
4064 }
4065
4066 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
4067 {
4068 Renderbuffer *readDSBuffer = NULL;
4069 Renderbuffer *drawDSBuffer = NULL;
4070
4071 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
4072 // both a depth and stencil buffer, it will be the same buffer.
4073
4074 if (mask & GL_DEPTH_BUFFER_BIT)
4075 {
4076 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
4077 {
4078 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
4079 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
4080 {
4081 return error(GL_INVALID_OPERATION);
4082 }
4083
4084 blitDepthStencil = true;
4085 readDSBuffer = readFramebuffer->getDepthbuffer();
4086 drawDSBuffer = drawFramebuffer->getDepthbuffer();
4087 }
4088 }
4089
4090 if (mask & GL_STENCIL_BUFFER_BIT)
4091 {
4092 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
4093 {
4094 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
4095 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
4096 {
4097 return error(GL_INVALID_OPERATION);
4098 }
4099
4100 blitDepthStencil = true;
4101 readDSBuffer = readFramebuffer->getStencilbuffer();
4102 drawDSBuffer = drawFramebuffer->getStencilbuffer();
4103 }
4104 }
4105
4106 if (partialBufferCopy)
4107 {
4108 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
4109 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
4110 }
4111
4112 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
4113 (readDSBuffer && readDSBuffer->getSamples() != 0))
4114 {
4115 return error(GL_INVALID_OPERATION);
4116 }
4117 }
4118
4119 if (blitRenderTarget || blitDepthStencil)
4120 {
4121 mDisplay->endScene();
4122
4123 if (blitRenderTarget)
4124 {
4125 IDirect3DSurface9* readRenderTarget = readFramebuffer->getRenderTarget();
4126 IDirect3DSurface9* drawRenderTarget = drawFramebuffer->getRenderTarget();
4127
4128 HRESULT result = mDevice->StretchRect(readRenderTarget, &sourceTrimmedRect,
4129 drawRenderTarget, &destTrimmedRect, D3DTEXF_NONE);
4130
4131 readRenderTarget->Release();
4132 drawRenderTarget->Release();
4133
4134 if (FAILED(result))
4135 {
4136 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
4137 return;
4138 }
4139 }
4140
4141 if (blitDepthStencil)
4142 {
4143 IDirect3DSurface9* readDepthStencil = readFramebuffer->getDepthStencil();
4144 IDirect3DSurface9* drawDepthStencil = drawFramebuffer->getDepthStencil();
4145
4146 HRESULT result = mDevice->StretchRect(readDepthStencil, NULL, drawDepthStencil, NULL, D3DTEXF_NONE);
4147
4148 readDepthStencil->Release();
4149 drawDepthStencil->Release();
4150
4151 if (FAILED(result))
4152 {
4153 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
4154 return;
4155 }
4156 }
4157 }
4158}
4159
4160VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
4161{
4162 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4163 {
4164 mVertexDeclCache[i].vertexDeclaration = NULL;
4165 mVertexDeclCache[i].lruCount = 0;
4166 }
4167}
4168
4169VertexDeclarationCache::~VertexDeclarationCache()
4170{
4171 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4172 {
4173 if (mVertexDeclCache[i].vertexDeclaration)
4174 {
4175 mVertexDeclCache[i].vertexDeclaration->Release();
4176 }
4177 }
4178}
4179
daniel@transgaming.com5ae3ccc2012-07-24 18:29:38 +00004180GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], ProgramBinary *programBinary, GLsizei instances, GLsizei *repeatDraw)
apatrick@chromium.org144f2802012-07-12 01:42:34 +00004181{
4182 *repeatDraw = 1;
4183
4184 int indexedAttribute = MAX_VERTEX_ATTRIBS;
4185 int instancedAttribute = MAX_VERTEX_ATTRIBS;
4186
4187 if (instances > 0)
4188 {
4189 // Find an indexed attribute to be mapped to D3D stream 0
4190 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4191 {
4192 if (attributes[i].active)
4193 {
4194 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
4195 {
4196 if (attributes[i].divisor == 0)
4197 {
4198 indexedAttribute = i;
4199 }
4200 }
4201 else if (instancedAttribute == MAX_VERTEX_ATTRIBS)
4202 {
4203 if (attributes[i].divisor != 0)
4204 {
4205 instancedAttribute = i;
4206 }
4207 }
4208 else break; // Found both an indexed and instanced attribute
4209 }
4210 }
4211
4212 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
4213 {
4214 return GL_INVALID_OPERATION;
4215 }
4216 }
4217
4218 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
4219 D3DVERTEXELEMENT9 *element = &elements[0];
4220
apatrick@chromium.org144f2802012-07-12 01:42:34 +00004221 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4222 {
4223 if (attributes[i].active)
4224 {
4225 int stream = i;
4226
4227 if (instances > 0)
4228 {
4229 // Due to a bug on ATI cards we can't enable instancing when none of the attributes are instanced.
4230 if (instancedAttribute == MAX_VERTEX_ATTRIBS)
4231 {
4232 *repeatDraw = instances;
4233 }
4234 else
4235 {
4236 if (i == indexedAttribute)
4237 {
4238 stream = 0;
4239 }
4240 else if (i == 0)
4241 {
4242 stream = indexedAttribute;
4243 }
4244
4245 UINT frequency = 1;
4246
4247 if (attributes[i].divisor == 0)
4248 {
4249 frequency = D3DSTREAMSOURCE_INDEXEDDATA | instances;
4250 }
4251 else
4252 {
4253 frequency = D3DSTREAMSOURCE_INSTANCEDATA | attributes[i].divisor;
4254 }
4255
4256 device->SetStreamSourceFreq(stream, frequency);
4257 mInstancingEnabled = true;
4258 }
4259 }
4260
4261 if (mAppliedVBs[stream].serial != attributes[i].serial ||
4262 mAppliedVBs[stream].stride != attributes[i].stride ||
4263 mAppliedVBs[stream].offset != attributes[i].offset)
4264 {
4265 device->SetStreamSource(stream, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
4266 mAppliedVBs[stream].serial = attributes[i].serial;
4267 mAppliedVBs[stream].stride = attributes[i].stride;
4268 mAppliedVBs[stream].offset = attributes[i].offset;
4269 }
4270
4271 element->Stream = stream;
4272 element->Offset = 0;
4273 element->Type = attributes[i].type;
4274 element->Method = D3DDECLMETHOD_DEFAULT;
4275 element->Usage = D3DDECLUSAGE_TEXCOORD;
4276 element->UsageIndex = programBinary->getSemanticIndex(i);
4277 element++;
4278 }
4279 }
4280
4281 if (instances == 0 || instancedAttribute == MAX_VERTEX_ATTRIBS)
4282 {
4283 if (mInstancingEnabled)
4284 {
4285 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4286 {
4287 device->SetStreamSourceFreq(i, 1);
4288 }
4289
4290 mInstancingEnabled = false;
4291 }
4292 }
4293
4294 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
4295 *(element++) = end;
4296
4297 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4298 {
4299 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
4300 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
4301 {
4302 entry->lruCount = ++mMaxLru;
4303 if(entry->vertexDeclaration != mLastSetVDecl)
4304 {
4305 device->SetVertexDeclaration(entry->vertexDeclaration);
4306 mLastSetVDecl = entry->vertexDeclaration;
4307 }
4308
4309 return GL_NO_ERROR;
4310 }
4311 }
4312
4313 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
4314
4315 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4316 {
4317 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
4318 {
4319 lastCache = &mVertexDeclCache[i];
4320 }
4321 }
4322
4323 if (lastCache->vertexDeclaration != NULL)
4324 {
4325 lastCache->vertexDeclaration->Release();
4326 lastCache->vertexDeclaration = NULL;
4327 // mLastSetVDecl is set to the replacement, so we don't have to worry
4328 // about it.
4329 }
4330
4331 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
4332 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
4333 device->SetVertexDeclaration(lastCache->vertexDeclaration);
4334 mLastSetVDecl = lastCache->vertexDeclaration;
4335 lastCache->lruCount = ++mMaxLru;
4336
4337 return GL_NO_ERROR;
4338}
4339
4340void VertexDeclarationCache::markStateDirty()
4341{
4342 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4343 {
4344 mAppliedVBs[i].serial = 0;
4345 }
4346
4347 mLastSetVDecl = NULL;
4348 mInstancingEnabled = true; // Forces it to be disabled when not used
4349}
4350
4351}
4352
4353extern "C"
4354{
4355gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext, bool notifyResets, bool robustAccess)
4356{
4357 return new gl::Context(config, shareContext, notifyResets, robustAccess);
4358}
4359
4360void glDestroyContext(gl::Context *context)
4361{
4362 delete context;
4363
4364 if (context == gl::getContext())
4365 {
4366 gl::makeCurrent(NULL, NULL, NULL);
4367 }
4368}
4369
4370void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
4371{
4372 gl::makeCurrent(context, display, surface);
4373}
4374
4375gl::Context *glGetCurrentContext()
4376{
4377 return gl::getContext();
4378}
4379}