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