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