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