blob: 78cee5035e3778eb487ae7b09337ab04931c4636 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002// Copyright (c) 2002-2012 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// 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
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000010#include "libGLESv2/Context.h"
daniel@transgaming.com16973022010-03-11 19:22:19 +000011
12#include <algorithm>
13
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000014#include "libEGL/Display.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000015
16#include "libGLESv2/main.h"
17#include "libGLESv2/mathutil.h"
18#include "libGLESv2/utilities.h"
19#include "libGLESv2/Blit.h"
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +000020#include "libGLESv2/ResourceManager.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000021#include "libGLESv2/Buffer.h"
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +000022#include "libGLESv2/Fence.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000023#include "libGLESv2/FrameBuffer.h"
24#include "libGLESv2/Program.h"
daniel@transgaming.com86bdb822012-01-20 18:24:39 +000025#include "libGLESv2/Query.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000026#include "libGLESv2/RenderBuffer.h"
27#include "libGLESv2/Shader.h"
28#include "libGLESv2/Texture.h"
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +000029#include "libGLESv2/VertexDataManager.h"
30#include "libGLESv2/IndexDataManager.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031
daniel@transgaming.com86487c22010-03-11 19:41:43 +000032#undef near
33#undef far
34
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000035namespace gl
36{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +000037Context::Context(const egl::Config *config, const gl::Context *shareContext, bool notifyResets, bool robustAccess) : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000038{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +000039 ASSERT(robustAccess == false); // Unimplemented
40
daniel@transgaming.comc941e252011-10-26 02:32:31 +000041 mDisplay = NULL;
42 mDevice = NULL;
43
benvanik@google.com1a233342011-04-28 19:44:39 +000044 mFenceHandleAllocator.setBaseHandle(0);
45
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000046 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000047
daniel@transgaming.com428d1582010-05-04 03:35:25 +000048 mState.depthClearValue = 1.0f;
49 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000050
daniel@transgaming.com428d1582010-05-04 03:35:25 +000051 mState.cullFace = false;
52 mState.cullMode = GL_BACK;
53 mState.frontFace = GL_CCW;
54 mState.depthTest = false;
55 mState.depthFunc = GL_LESS;
56 mState.blend = false;
57 mState.sourceBlendRGB = GL_ONE;
58 mState.sourceBlendAlpha = GL_ONE;
59 mState.destBlendRGB = GL_ZERO;
60 mState.destBlendAlpha = GL_ZERO;
61 mState.blendEquationRGB = GL_FUNC_ADD;
62 mState.blendEquationAlpha = GL_FUNC_ADD;
63 mState.blendColor.red = 0;
64 mState.blendColor.green = 0;
65 mState.blendColor.blue = 0;
66 mState.blendColor.alpha = 0;
67 mState.stencilTest = false;
68 mState.stencilFunc = GL_ALWAYS;
69 mState.stencilRef = 0;
70 mState.stencilMask = -1;
71 mState.stencilWritemask = -1;
72 mState.stencilBackFunc = GL_ALWAYS;
73 mState.stencilBackRef = 0;
74 mState.stencilBackMask = - 1;
75 mState.stencilBackWritemask = -1;
76 mState.stencilFail = GL_KEEP;
77 mState.stencilPassDepthFail = GL_KEEP;
78 mState.stencilPassDepthPass = GL_KEEP;
79 mState.stencilBackFail = GL_KEEP;
80 mState.stencilBackPassDepthFail = GL_KEEP;
81 mState.stencilBackPassDepthPass = GL_KEEP;
82 mState.polygonOffsetFill = false;
83 mState.polygonOffsetFactor = 0.0f;
84 mState.polygonOffsetUnits = 0.0f;
85 mState.sampleAlphaToCoverage = false;
86 mState.sampleCoverage = false;
87 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000088 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000089 mState.scissorTest = false;
90 mState.dither = true;
91 mState.generateMipmapHint = GL_DONT_CARE;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +000092 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000093
daniel@transgaming.com428d1582010-05-04 03:35:25 +000094 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000095
daniel@transgaming.com428d1582010-05-04 03:35:25 +000096 mState.viewportX = 0;
97 mState.viewportY = 0;
98 mState.viewportWidth = config->mDisplayMode.Width;
99 mState.viewportHeight = config->mDisplayMode.Height;
100 mState.zNear = 0.0f;
101 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000103 mState.scissorX = 0;
104 mState.scissorY = 0;
105 mState.scissorWidth = config->mDisplayMode.Width;
106 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000107
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000108 mState.colorMaskRed = true;
109 mState.colorMaskGreen = true;
110 mState.colorMaskBlue = true;
111 mState.colorMaskAlpha = true;
112 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000113
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000114 if (shareContext != NULL)
115 {
116 mResourceManager = shareContext->mResourceManager;
117 mResourceManager->addRef();
118 }
119 else
120 {
121 mResourceManager = new ResourceManager();
122 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000123
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000124 // [OpenGL ES 2.0.24] section 3.7 page 83:
125 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
126 // and cube map texture state vectors respectively associated with them.
127 // In order that access to these initial textures not be lost, they are treated as texture
128 // objects all of whose names are 0.
129
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000130 mTexture2DZero.set(new Texture2D(0));
131 mTextureCubeMapZero.set(new TextureCubeMap(0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000132
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000133 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000134 bindArrayBuffer(0);
135 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136 bindTextureCubeMap(0);
137 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000138 bindReadFramebuffer(0);
139 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000140 bindRenderbuffer(0);
141
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000142 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000143
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000144 mState.packAlignment = 4;
145 mState.unpackAlignment = 4;
bsalomon@google.com56d46ab2011-11-23 14:53:10 +0000146 mState.packReverseRowOrder = false;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000147
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000148 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000149 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000150 mBlit = NULL;
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +0000151 mLineLoopIB = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000152
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000153 mInvalidEnum = false;
154 mInvalidValue = false;
155 mInvalidOperation = false;
156 mOutOfMemory = false;
157 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000158
159 mHasBeenCurrent = false;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000160 mContextLost = false;
daniel@transgaming.com17f548c2011-11-09 17:47:02 +0000161 mResetStatus = GL_NO_ERROR;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +0000162 mResetStrategy = (notifyResets ? GL_LOSE_CONTEXT_ON_RESET_EXT : GL_NO_RESET_NOTIFICATION_EXT);
163 mRobustAccess = robustAccess;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000164
gman@chromium.org50c526d2011-08-10 05:19:44 +0000165 mSupportsDXT1Textures = false;
166 mSupportsDXT3Textures = false;
167 mSupportsDXT5Textures = false;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000168 mSupportsEventQueries = false;
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000169 mSupportsOcclusionQueries = false;
gman@chromium.org50c526d2011-08-10 05:19:44 +0000170 mNumCompressedTextureFormats = 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000171 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000172 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000173 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000174}
175
176Context::~Context()
177{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000178 if (mState.currentProgram != 0)
179 {
180 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
181 if (programObject)
182 {
183 programObject->release();
184 }
185 mState.currentProgram = 0;
186 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000187
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000188 while (!mFramebufferMap.empty())
189 {
190 deleteFramebuffer(mFramebufferMap.begin()->first);
191 }
192
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000193 while (!mFenceMap.empty())
194 {
195 deleteFence(mFenceMap.begin()->first);
196 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000197
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000198 while (!mQueryMap.empty())
199 {
200 deleteQuery(mQueryMap.begin()->first);
201 }
202
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000203 while (!mMultiSampleSupport.empty())
204 {
205 delete [] mMultiSampleSupport.begin()->second;
206 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
207 }
208
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000209 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000210 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +0000211 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000212 {
213 mState.samplerTexture[type][sampler].set(NULL);
214 }
215 }
216
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000217 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000218 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000219 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000220 }
221
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000222 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
223 {
224 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
225 }
226
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000227 for (int i = 0; i < QUERY_TYPE_COUNT; i++)
228 {
229 mState.activeQuery[i].set(NULL);
230 }
231
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000232 mState.arrayBuffer.set(NULL);
233 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000234 mState.renderbuffer.set(NULL);
235
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000236 mTexture2DZero.set(NULL);
237 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000238
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000239 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000240 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000241 delete mBlit;
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +0000242 delete mLineLoopIB;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000243
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000244 if (mMaskedClearSavedState)
245 {
246 mMaskedClearSavedState->Release();
247 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000248
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000249 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000250}
251
252void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
253{
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000254 mDisplay = display;
255 mDevice = mDisplay->getDevice();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000256
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000257 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000258 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000259 mDeviceCaps = mDisplay->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000260
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000261 mVertexDataManager = new VertexDataManager(this, mDevice);
262 mIndexDataManager = new IndexDataManager(this, mDevice);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000263 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000264
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +0000265 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000266 mSupportsVertexTexture = mDisplay->getVertexTextureSupport();
267 mSupportsNonPower2Texture = mDisplay->getNonPower2TextureSupport();
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +0000268 mSupportsInstancing = mDisplay->getInstancingSupport();
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000269
270 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
271 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
272 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
273 mMaxRenderbufferDimension = mMaxTextureDimension;
274 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
275 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
276 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
277
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000278 const D3DFORMAT renderBufferFormats[] =
279 {
280 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000281 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000282 D3DFMT_R5G6B5,
283 D3DFMT_D24S8
284 };
285
286 int max = 0;
287 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
288 {
289 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000290 mDisplay->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000291 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
292
293 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
294 {
295 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
296 {
297 max = j;
298 }
299 }
300 }
301
302 mMaxSupportedSamples = max;
303
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000304 mSupportsEventQueries = mDisplay->getEventQuerySupport();
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000305 mSupportsOcclusionQueries = mDisplay->getOcclusionQuerySupport();
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000306 mSupportsDXT1Textures = mDisplay->getDXT1TextureSupport();
307 mSupportsDXT3Textures = mDisplay->getDXT3TextureSupport();
308 mSupportsDXT5Textures = mDisplay->getDXT5TextureSupport();
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +0000309 mSupportsFloat32Textures = mDisplay->getFloat32TextureSupport(&mSupportsFloat32LinearFilter, &mSupportsFloat32RenderableTextures);
310 mSupportsFloat16Textures = mDisplay->getFloat16TextureSupport(&mSupportsFloat16LinearFilter, &mSupportsFloat16RenderableTextures);
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000311 mSupportsLuminanceTextures = mDisplay->getLuminanceTextureSupport();
312 mSupportsLuminanceAlphaTextures = mDisplay->getLuminanceAlphaTextureSupport();
daniel@transgaming.com1c49f792012-05-31 01:14:02 +0000313 mSupportsDepthTextures = mDisplay->getDepthTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000314
daniel@transgaming.com83921382011-01-08 05:46:00 +0000315 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
316
gman@chromium.org50c526d2011-08-10 05:19:44 +0000317 mNumCompressedTextureFormats = 0;
318 if (supportsDXT1Textures())
319 {
320 mNumCompressedTextureFormats += 2;
321 }
322 if (supportsDXT3Textures())
323 {
324 mNumCompressedTextureFormats += 1;
325 }
326 if (supportsDXT5Textures())
327 {
328 mNumCompressedTextureFormats += 1;
329 }
330
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000331 initExtensionString();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +0000332 initRendererString();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000333
334 mState.viewportX = 0;
335 mState.viewportY = 0;
336 mState.viewportWidth = surface->getWidth();
337 mState.viewportHeight = surface->getHeight();
338
339 mState.scissorX = 0;
340 mState.scissorY = 0;
341 mState.scissorWidth = surface->getWidth();
342 mState.scissorHeight = surface->getHeight();
343
344 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000345 }
346
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000347 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
348 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000349 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000350
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000351 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000352 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000353 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354
355 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000356
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000357 if (defaultRenderTarget)
358 {
359 defaultRenderTarget->Release();
360 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000361
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000362 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000363 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000364 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000365 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000366
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000367 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368}
369
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000370// This function will set all of the state-related dirty flags, so that all state is set during next pre-draw.
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000371void Context::markAllStateDirty()
372{
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000373 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
374 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +0000375 mAppliedTextureSerialPS[t] = 0;
376 }
377
378 for (int t = 0; t < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; t++)
379 {
380 mAppliedTextureSerialVS[t] = 0;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000381 }
382
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000383 mAppliedProgramSerial = 0;
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000384 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000385 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000386 mAppliedStencilbufferSerial = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000387 mAppliedIBSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000388 mDepthStencilInitialized = false;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000389 mViewportInitialized = false;
390 mRenderTargetDescInitialized = false;
391
392 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000393
394 mClearStateDirty = true;
395 mCullStateDirty = true;
396 mDepthStateDirty = true;
397 mMaskStateDirty = true;
398 mBlendStateDirty = true;
399 mStencilStateDirty = true;
400 mPolygonOffsetStateDirty = true;
401 mScissorStateDirty = true;
402 mSampleStateDirty = true;
403 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000404 mFrontFaceDirty = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +0000405 mDxUniformsDirty = true;
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000406 mCachedCurrentProgram = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000407}
408
daniel@transgaming.com11399d52012-04-28 00:35:14 +0000409void Context::markDxUniformsDirty()
410{
411 mDxUniformsDirty = true;
412}
413
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000414void Context::markContextLost()
415{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +0000416 if (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT)
417 mResetStatus = GL_UNKNOWN_CONTEXT_RESET_EXT;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000418 mContextLost = true;
419}
420
421bool Context::isContextLost()
422{
423 return mContextLost;
424}
425
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000426void Context::setClearColor(float red, float green, float blue, float alpha)
427{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000428 mState.colorClearValue.red = red;
429 mState.colorClearValue.green = green;
430 mState.colorClearValue.blue = blue;
431 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000432}
433
434void Context::setClearDepth(float depth)
435{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000436 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000437}
438
439void Context::setClearStencil(int stencil)
440{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000441 mState.stencilClearValue = stencil;
442}
443
444void Context::setCullFace(bool enabled)
445{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000446 if (mState.cullFace != enabled)
447 {
448 mState.cullFace = enabled;
449 mCullStateDirty = true;
450 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000451}
452
453bool Context::isCullFaceEnabled() const
454{
455 return mState.cullFace;
456}
457
458void Context::setCullMode(GLenum mode)
459{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000460 if (mState.cullMode != mode)
461 {
462 mState.cullMode = mode;
463 mCullStateDirty = true;
464 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000465}
466
467void Context::setFrontFace(GLenum front)
468{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000469 if (mState.frontFace != front)
470 {
471 mState.frontFace = front;
472 mFrontFaceDirty = true;
473 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000474}
475
476void Context::setDepthTest(bool enabled)
477{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000478 if (mState.depthTest != enabled)
479 {
480 mState.depthTest = enabled;
481 mDepthStateDirty = true;
482 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000483}
484
485bool Context::isDepthTestEnabled() const
486{
487 return mState.depthTest;
488}
489
490void Context::setDepthFunc(GLenum depthFunc)
491{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000492 if (mState.depthFunc != depthFunc)
493 {
494 mState.depthFunc = depthFunc;
495 mDepthStateDirty = true;
496 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000497}
498
499void Context::setDepthRange(float zNear, float zFar)
500{
501 mState.zNear = zNear;
502 mState.zFar = zFar;
503}
504
505void Context::setBlend(bool enabled)
506{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000507 if (mState.blend != enabled)
508 {
509 mState.blend = enabled;
510 mBlendStateDirty = true;
511 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000512}
513
514bool Context::isBlendEnabled() const
515{
516 return mState.blend;
517}
518
519void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
520{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000521 if (mState.sourceBlendRGB != sourceRGB ||
522 mState.sourceBlendAlpha != sourceAlpha ||
523 mState.destBlendRGB != destRGB ||
524 mState.destBlendAlpha != destAlpha)
525 {
526 mState.sourceBlendRGB = sourceRGB;
527 mState.destBlendRGB = destRGB;
528 mState.sourceBlendAlpha = sourceAlpha;
529 mState.destBlendAlpha = destAlpha;
530 mBlendStateDirty = true;
531 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000532}
533
534void Context::setBlendColor(float red, float green, float blue, float alpha)
535{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000536 if (mState.blendColor.red != red ||
537 mState.blendColor.green != green ||
538 mState.blendColor.blue != blue ||
539 mState.blendColor.alpha != alpha)
540 {
541 mState.blendColor.red = red;
542 mState.blendColor.green = green;
543 mState.blendColor.blue = blue;
544 mState.blendColor.alpha = alpha;
545 mBlendStateDirty = true;
546 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000547}
548
549void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
550{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000551 if (mState.blendEquationRGB != rgbEquation ||
552 mState.blendEquationAlpha != alphaEquation)
553 {
554 mState.blendEquationRGB = rgbEquation;
555 mState.blendEquationAlpha = alphaEquation;
556 mBlendStateDirty = true;
557 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000558}
559
560void Context::setStencilTest(bool enabled)
561{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000562 if (mState.stencilTest != enabled)
563 {
564 mState.stencilTest = enabled;
565 mStencilStateDirty = true;
566 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000567}
568
569bool Context::isStencilTestEnabled() const
570{
571 return mState.stencilTest;
572}
573
574void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
575{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000576 if (mState.stencilFunc != stencilFunc ||
577 mState.stencilRef != stencilRef ||
578 mState.stencilMask != stencilMask)
579 {
580 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000581 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000582 mState.stencilMask = stencilMask;
583 mStencilStateDirty = true;
584 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000585}
586
587void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
588{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000589 if (mState.stencilBackFunc != stencilBackFunc ||
590 mState.stencilBackRef != stencilBackRef ||
591 mState.stencilBackMask != stencilBackMask)
592 {
593 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000594 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000595 mState.stencilBackMask = stencilBackMask;
596 mStencilStateDirty = true;
597 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000598}
599
600void Context::setStencilWritemask(GLuint stencilWritemask)
601{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000602 if (mState.stencilWritemask != stencilWritemask)
603 {
604 mState.stencilWritemask = stencilWritemask;
605 mStencilStateDirty = true;
606 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000607}
608
609void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
610{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000611 if (mState.stencilBackWritemask != stencilBackWritemask)
612 {
613 mState.stencilBackWritemask = stencilBackWritemask;
614 mStencilStateDirty = true;
615 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000616}
617
618void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
619{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000620 if (mState.stencilFail != stencilFail ||
621 mState.stencilPassDepthFail != stencilPassDepthFail ||
622 mState.stencilPassDepthPass != stencilPassDepthPass)
623 {
624 mState.stencilFail = stencilFail;
625 mState.stencilPassDepthFail = stencilPassDepthFail;
626 mState.stencilPassDepthPass = stencilPassDepthPass;
627 mStencilStateDirty = true;
628 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000629}
630
631void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
632{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000633 if (mState.stencilBackFail != stencilBackFail ||
634 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
635 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
636 {
637 mState.stencilBackFail = stencilBackFail;
638 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
639 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
640 mStencilStateDirty = true;
641 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000642}
643
644void Context::setPolygonOffsetFill(bool enabled)
645{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000646 if (mState.polygonOffsetFill != enabled)
647 {
648 mState.polygonOffsetFill = enabled;
649 mPolygonOffsetStateDirty = true;
650 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000651}
652
653bool Context::isPolygonOffsetFillEnabled() const
654{
655 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000656
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000657}
658
659void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
660{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000661 if (mState.polygonOffsetFactor != factor ||
662 mState.polygonOffsetUnits != units)
663 {
664 mState.polygonOffsetFactor = factor;
665 mState.polygonOffsetUnits = units;
666 mPolygonOffsetStateDirty = true;
667 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000668}
669
670void Context::setSampleAlphaToCoverage(bool enabled)
671{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000672 if (mState.sampleAlphaToCoverage != enabled)
673 {
674 mState.sampleAlphaToCoverage = enabled;
675 mSampleStateDirty = true;
676 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000677}
678
679bool Context::isSampleAlphaToCoverageEnabled() const
680{
681 return mState.sampleAlphaToCoverage;
682}
683
684void Context::setSampleCoverage(bool enabled)
685{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000686 if (mState.sampleCoverage != enabled)
687 {
688 mState.sampleCoverage = enabled;
689 mSampleStateDirty = true;
690 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000691}
692
693bool Context::isSampleCoverageEnabled() const
694{
695 return mState.sampleCoverage;
696}
697
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000698void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000699{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000700 if (mState.sampleCoverageValue != value ||
701 mState.sampleCoverageInvert != invert)
702 {
703 mState.sampleCoverageValue = value;
704 mState.sampleCoverageInvert = invert;
705 mSampleStateDirty = true;
706 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000707}
708
709void Context::setScissorTest(bool enabled)
710{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000711 if (mState.scissorTest != enabled)
712 {
713 mState.scissorTest = enabled;
714 mScissorStateDirty = true;
715 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000716}
717
718bool Context::isScissorTestEnabled() const
719{
720 return mState.scissorTest;
721}
722
723void Context::setDither(bool enabled)
724{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000725 if (mState.dither != enabled)
726 {
727 mState.dither = enabled;
728 mDitherStateDirty = true;
729 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000730}
731
732bool Context::isDitherEnabled() const
733{
734 return mState.dither;
735}
736
737void Context::setLineWidth(GLfloat width)
738{
739 mState.lineWidth = width;
740}
741
742void Context::setGenerateMipmapHint(GLenum hint)
743{
744 mState.generateMipmapHint = hint;
745}
746
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000747void Context::setFragmentShaderDerivativeHint(GLenum hint)
748{
749 mState.fragmentShaderDerivativeHint = hint;
750 // TODO: Propagate the hint to shader translator so we can write
751 // ddx, ddx_coarse, or ddx_fine depending on the hint.
752 // Ignore for now. It is valid for implementations to ignore hint.
753}
754
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000755void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
756{
757 mState.viewportX = x;
758 mState.viewportY = y;
759 mState.viewportWidth = width;
760 mState.viewportHeight = height;
761}
762
763void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
764{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000765 if (mState.scissorX != x || mState.scissorY != y ||
766 mState.scissorWidth != width || mState.scissorHeight != height)
767 {
768 mState.scissorX = x;
769 mState.scissorY = y;
770 mState.scissorWidth = width;
771 mState.scissorHeight = height;
772 mScissorStateDirty = true;
773 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000774}
775
776void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
777{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000778 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
779 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
780 {
781 mState.colorMaskRed = red;
782 mState.colorMaskGreen = green;
783 mState.colorMaskBlue = blue;
784 mState.colorMaskAlpha = alpha;
785 mMaskStateDirty = true;
786 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000787}
788
789void Context::setDepthMask(bool mask)
790{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000791 if (mState.depthMask != mask)
792 {
793 mState.depthMask = mask;
794 mMaskStateDirty = true;
795 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000796}
797
daniel@transgaming.comdfd57022011-05-11 15:37:25 +0000798void Context::setActiveSampler(unsigned int active)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000799{
800 mState.activeSampler = active;
801}
802
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000803GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000804{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000805 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000806}
807
808GLuint Context::getDrawFramebufferHandle() const
809{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000810 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000811}
812
813GLuint Context::getRenderbufferHandle() const
814{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000815 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000816}
817
818GLuint Context::getArrayBufferHandle() const
819{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000820 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000821}
822
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000823GLuint Context::getActiveQuery(GLenum target) const
824{
825 Query *queryObject = NULL;
826
827 switch (target)
828 {
829 case GL_ANY_SAMPLES_PASSED_EXT:
830 queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED].get();
831 break;
832 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
833 queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE].get();
834 break;
835 default:
836 ASSERT(false);
837 }
838
839 if (queryObject)
840 {
841 return queryObject->id();
842 }
843 else
844 {
845 return 0;
846 }
847}
848
daniel@transgaming.com83921382011-01-08 05:46:00 +0000849void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000850{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000851 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000852}
853
daniel@transgaming.com83921382011-01-08 05:46:00 +0000854const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000855{
856 return mState.vertexAttribute[attribNum];
857}
858
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000859void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000860 GLsizei stride, const void *pointer)
861{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000862 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000863 mState.vertexAttribute[attribNum].mSize = size;
864 mState.vertexAttribute[attribNum].mType = type;
865 mState.vertexAttribute[attribNum].mNormalized = normalized;
866 mState.vertexAttribute[attribNum].mStride = stride;
867 mState.vertexAttribute[attribNum].mPointer = pointer;
868}
869
870const void *Context::getVertexAttribPointer(unsigned int attribNum) const
871{
872 return mState.vertexAttribute[attribNum].mPointer;
873}
874
daniel@transgaming.com83921382011-01-08 05:46:00 +0000875const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000876{
877 return mState.vertexAttribute;
878}
879
880void Context::setPackAlignment(GLint alignment)
881{
882 mState.packAlignment = alignment;
883}
884
885GLint Context::getPackAlignment() const
886{
887 return mState.packAlignment;
888}
889
890void Context::setUnpackAlignment(GLint alignment)
891{
892 mState.unpackAlignment = alignment;
893}
894
895GLint Context::getUnpackAlignment() const
896{
897 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000898}
899
bsalomon@google.com56d46ab2011-11-23 14:53:10 +0000900void Context::setPackReverseRowOrder(bool reverseRowOrder)
901{
902 mState.packReverseRowOrder = reverseRowOrder;
903}
904
905bool Context::getPackReverseRowOrder() const
906{
907 return mState.packReverseRowOrder;
908}
909
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910GLuint Context::createBuffer()
911{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000912 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000913}
914
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000915GLuint Context::createProgram()
916{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000917 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000918}
919
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000920GLuint Context::createShader(GLenum type)
921{
922 return mResourceManager->createShader(type);
923}
924
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000925GLuint Context::createTexture()
926{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000927 return mResourceManager->createTexture();
928}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000929
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000930GLuint Context::createRenderbuffer()
931{
932 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000933}
934
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000935// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000936GLuint Context::createFramebuffer()
937{
benvanik@google.com1a233342011-04-28 19:44:39 +0000938 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000939
940 mFramebufferMap[handle] = NULL;
941
942 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000943}
944
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000945GLuint Context::createFence()
946{
benvanik@google.com1a233342011-04-28 19:44:39 +0000947 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000948
apatrick@chromium.org563c0a52012-03-23 21:18:42 +0000949 mFenceMap[handle] = new Fence(mDisplay);
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000950
951 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000952}
953
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000954// Returns an unused query name
955GLuint Context::createQuery()
956{
957 GLuint handle = mQueryHandleAllocator.allocate();
958
959 mQueryMap[handle] = NULL;
960
961 return handle;
962}
963
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964void Context::deleteBuffer(GLuint buffer)
965{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000966 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000967 {
968 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000969 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000970
971 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972}
973
974void Context::deleteShader(GLuint shader)
975{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000976 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000977}
978
979void Context::deleteProgram(GLuint program)
980{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000981 mResourceManager->deleteProgram(program);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000982 mCachedCurrentProgram = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000983}
984
985void Context::deleteTexture(GLuint texture)
986{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000987 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000988 {
989 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000990 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000991
992 mResourceManager->deleteTexture(texture);
993}
994
995void Context::deleteRenderbuffer(GLuint renderbuffer)
996{
997 if (mResourceManager->getRenderbuffer(renderbuffer))
998 {
999 detachRenderbuffer(renderbuffer);
1000 }
1001
1002 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001003}
1004
1005void Context::deleteFramebuffer(GLuint framebuffer)
1006{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001007 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
1008
1009 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001010 {
1011 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +00001012
benvanik@google.com1a233342011-04-28 19:44:39 +00001013 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001014 delete framebufferObject->second;
1015 mFramebufferMap.erase(framebufferObject);
1016 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001017}
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001018
1019void Context::deleteFence(GLuint fence)
1020{
1021 FenceMap::iterator fenceObject = mFenceMap.find(fence);
1022
1023 if (fenceObject != mFenceMap.end())
1024 {
benvanik@google.com1a233342011-04-28 19:44:39 +00001025 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001026 delete fenceObject->second;
1027 mFenceMap.erase(fenceObject);
1028 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001029}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001031void Context::deleteQuery(GLuint query)
1032{
1033 QueryMap::iterator queryObject = mQueryMap.find(query);
1034 if (queryObject != mQueryMap.end())
1035 {
1036 mQueryHandleAllocator.release(queryObject->first);
1037 if (queryObject->second)
1038 {
1039 queryObject->second->release();
1040 }
1041 mQueryMap.erase(queryObject);
1042 }
1043}
1044
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001045Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001046{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001047 return mResourceManager->getBuffer(handle);
1048}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001049
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001050Shader *Context::getShader(GLuint handle)
1051{
1052 return mResourceManager->getShader(handle);
1053}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001054
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001055Program *Context::getProgram(GLuint handle)
1056{
1057 return mResourceManager->getProgram(handle);
1058}
1059
1060Texture *Context::getTexture(GLuint handle)
1061{
1062 return mResourceManager->getTexture(handle);
1063}
1064
1065Renderbuffer *Context::getRenderbuffer(GLuint handle)
1066{
1067 return mResourceManager->getRenderbuffer(handle);
1068}
1069
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001070Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001071{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001072 return getFramebuffer(mState.readFramebuffer);
1073}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001074
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001075Framebuffer *Context::getDrawFramebuffer()
1076{
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001077 return mBoundDrawFramebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001078}
1079
1080void Context::bindArrayBuffer(unsigned int buffer)
1081{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001082 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001083
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001084 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001085}
1086
1087void Context::bindElementArrayBuffer(unsigned int buffer)
1088{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001089 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001090
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001091 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001092}
1093
1094void Context::bindTexture2D(GLuint texture)
1095{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001096 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001097
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001098 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001099}
1100
1101void Context::bindTextureCubeMap(GLuint texture)
1102{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001103 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001104
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001105 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001106}
1107
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001108void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001109{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001110 if (!getFramebuffer(framebuffer))
1111 {
1112 mFramebufferMap[framebuffer] = new Framebuffer();
1113 }
1114
1115 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001116}
1117
1118void Context::bindDrawFramebuffer(GLuint framebuffer)
1119{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001120 if (!getFramebuffer(framebuffer))
1121 {
1122 mFramebufferMap[framebuffer] = new Framebuffer();
1123 }
1124
1125 mState.drawFramebuffer = framebuffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001126
1127 mBoundDrawFramebuffer = getFramebuffer(framebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001128}
1129
1130void Context::bindRenderbuffer(GLuint renderbuffer)
1131{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001132 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1133
1134 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001135}
1136
1137void Context::useProgram(GLuint program)
1138{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001139 GLuint priorProgram = mState.currentProgram;
1140 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001141
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001142 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001143 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001144 Program *newProgram = mResourceManager->getProgram(program);
1145 Program *oldProgram = mResourceManager->getProgram(priorProgram);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001146 mCachedCurrentProgram = NULL;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001147 mDxUniformsDirty = true;
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001148
1149 if (newProgram)
1150 {
1151 newProgram->addRef();
1152 }
1153
1154 if (oldProgram)
1155 {
1156 oldProgram->release();
1157 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001158 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001159}
1160
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001161void Context::beginQuery(GLenum target, GLuint query)
1162{
1163 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
1164 // of zero, if the active query object name for <target> is non-zero (for the
1165 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
1166 // the active query for either target is non-zero), if <id> is the name of an
1167 // existing query object whose type does not match <target>, or if <id> is the
1168 // active query object name for any query type, the error INVALID_OPERATION is
1169 // generated.
1170
1171 // Ensure no other queries are active
1172 // NOTE: If other queries than occlusion are supported, we will need to check
1173 // separately that:
1174 // a) The query ID passed is not the current active query for any target/type
1175 // b) There are no active queries for the requested target (and in the case
1176 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
1177 // no query may be active for either if glBeginQuery targets either.
1178 for (int i = 0; i < QUERY_TYPE_COUNT; i++)
1179 {
1180 if (mState.activeQuery[i].get() != NULL)
1181 {
1182 return error(GL_INVALID_OPERATION);
1183 }
1184 }
1185
1186 QueryType qType;
1187 switch (target)
1188 {
1189 case GL_ANY_SAMPLES_PASSED_EXT:
1190 qType = QUERY_ANY_SAMPLES_PASSED;
1191 break;
1192 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
1193 qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
1194 break;
1195 default:
1196 ASSERT(false);
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00001197 return;
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001198 }
1199
1200 Query *queryObject = getQuery(query, true, target);
1201
1202 // check that name was obtained with glGenQueries
1203 if (!queryObject)
1204 {
1205 return error(GL_INVALID_OPERATION);
1206 }
1207
1208 // check for type mismatch
1209 if (queryObject->getType() != target)
1210 {
1211 return error(GL_INVALID_OPERATION);
1212 }
1213
1214 // set query as active for specified target
1215 mState.activeQuery[qType].set(queryObject);
1216
1217 // begin query
1218 queryObject->begin();
1219}
1220
1221void Context::endQuery(GLenum target)
1222{
1223 QueryType qType;
1224
1225 switch (target)
1226 {
1227 case GL_ANY_SAMPLES_PASSED_EXT:
1228 qType = QUERY_ANY_SAMPLES_PASSED;
1229 break;
1230 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
1231 qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
1232 break;
1233 default:
1234 ASSERT(false);
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00001235 return;
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001236 }
1237
1238 Query *queryObject = mState.activeQuery[qType].get();
1239
1240 if (queryObject == NULL)
1241 {
1242 return error(GL_INVALID_OPERATION);
1243 }
1244
1245 queryObject->end();
1246
1247 mState.activeQuery[qType].set(NULL);
1248}
1249
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001250void Context::setFramebufferZero(Framebuffer *buffer)
1251{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001252 delete mFramebufferMap[0];
1253 mFramebufferMap[0] = buffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001254 if (mState.drawFramebuffer == 0)
1255 {
1256 mBoundDrawFramebuffer = buffer;
1257 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001258}
1259
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001260void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001261{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001262 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1263 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001264}
1265
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001266Framebuffer *Context::getFramebuffer(unsigned int handle)
1267{
1268 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1269
1270 if (framebuffer == mFramebufferMap.end())
1271 {
1272 return NULL;
1273 }
1274 else
1275 {
1276 return framebuffer->second;
1277 }
1278}
1279
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001280Fence *Context::getFence(unsigned int handle)
1281{
1282 FenceMap::iterator fence = mFenceMap.find(handle);
1283
1284 if (fence == mFenceMap.end())
1285 {
1286 return NULL;
1287 }
1288 else
1289 {
1290 return fence->second;
1291 }
1292}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001293
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001294Query *Context::getQuery(unsigned int handle, bool create, GLenum type)
1295{
1296 QueryMap::iterator query = mQueryMap.find(handle);
1297
1298 if (query == mQueryMap.end())
1299 {
1300 return NULL;
1301 }
1302 else
1303 {
1304 if (!query->second && create)
1305 {
1306 query->second = new Query(handle, type);
1307 query->second->addRef();
1308 }
1309 return query->second;
1310 }
1311}
1312
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001313Buffer *Context::getArrayBuffer()
1314{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001315 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001316}
1317
1318Buffer *Context::getElementArrayBuffer()
1319{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001320 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001321}
1322
1323Program *Context::getCurrentProgram()
1324{
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001325 if (!mCachedCurrentProgram)
1326 {
1327 mCachedCurrentProgram = mResourceManager->getProgram(mState.currentProgram);
1328 }
1329 return mCachedCurrentProgram;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001330}
1331
1332Texture2D *Context::getTexture2D()
1333{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001334 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001335}
1336
1337TextureCubeMap *Context::getTextureCubeMap()
1338{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001339 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001340}
1341
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001342Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001343{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001344 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001345
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001346 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001347 {
1348 switch (type)
1349 {
1350 default: UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001351 case TEXTURE_2D: return mTexture2DZero.get();
1352 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001353 }
1354 }
1355
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001356 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001357}
1358
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001359bool Context::getBooleanv(GLenum pname, GLboolean *params)
1360{
1361 switch (pname)
1362 {
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001363 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1364 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1365 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001366 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001367 params[0] = mState.colorMaskRed;
1368 params[1] = mState.colorMaskGreen;
1369 params[2] = mState.colorMaskBlue;
1370 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001371 break;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001372 case GL_CULL_FACE: *params = mState.cullFace; break;
1373 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1374 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1375 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1376 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1377 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1378 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1379 case GL_BLEND: *params = mState.blend; break;
1380 case GL_DITHER: *params = mState.dither; break;
1381 case GL_CONTEXT_ROBUST_ACCESS_EXT: *params = mRobustAccess ? GL_TRUE : GL_FALSE; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001382 default:
1383 return false;
1384 }
1385
1386 return true;
1387}
1388
1389bool Context::getFloatv(GLenum pname, GLfloat *params)
1390{
1391 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1392 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1393 // GetIntegerv as its native query function. As it would require conversion in any
1394 // case, this should make no difference to the calling application.
1395 switch (pname)
1396 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001397 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1398 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1399 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1400 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1401 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001402 case GL_ALIASED_LINE_WIDTH_RANGE:
1403 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1404 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1405 break;
1406 case GL_ALIASED_POINT_SIZE_RANGE:
1407 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001408 params[1] = supportsShaderModel3() ? gl::ALIASED_POINT_SIZE_RANGE_MAX_SM3 : gl::ALIASED_POINT_SIZE_RANGE_MAX_SM2;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001409 break;
1410 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001411 params[0] = mState.zNear;
1412 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001413 break;
1414 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001415 params[0] = mState.colorClearValue.red;
1416 params[1] = mState.colorClearValue.green;
1417 params[2] = mState.colorClearValue.blue;
1418 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001419 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001420 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001421 params[0] = mState.blendColor.red;
1422 params[1] = mState.blendColor.green;
1423 params[2] = mState.blendColor.blue;
1424 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001425 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001426 default:
1427 return false;
1428 }
1429
1430 return true;
1431}
1432
1433bool Context::getIntegerv(GLenum pname, GLint *params)
1434{
1435 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1436 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1437 // GetIntegerv as its native query function. As it would require conversion in any
1438 // case, this should make no difference to the calling application. You may find it in
1439 // Context::getFloatv.
1440 switch (pname)
1441 {
1442 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1443 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001444 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00001445 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1446 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001447 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001448 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001449 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001450 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001451 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001452 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1453 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001454 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1455 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1456 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001457 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001458 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1459 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00001460 case GL_PACK_REVERSE_ROW_ORDER_ANGLE: *params = mState.packReverseRowOrder; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001461 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1462 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001463 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001464 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1465 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1466 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1467 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1468 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1469 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1470 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1471 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1472 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1473 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1474 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1475 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1476 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1477 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1478 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1479 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1480 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1481 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1482 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1483 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1484 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1485 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1486 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1487 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001488 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1489 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001490 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
gman@chromium.org50c526d2011-08-10 05:19:44 +00001491 params[0] = mNumCompressedTextureFormats;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001492 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001493 case GL_MAX_SAMPLES_ANGLE:
1494 {
1495 GLsizei maxSamples = getMaxSupportedSamples();
1496 if (maxSamples != 0)
1497 {
1498 *params = maxSamples;
1499 }
1500 else
1501 {
1502 return false;
1503 }
1504
1505 break;
1506 }
1507 case GL_SAMPLE_BUFFERS:
1508 case GL_SAMPLES:
1509 {
1510 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1511 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1512 {
1513 switch (pname)
1514 {
1515 case GL_SAMPLE_BUFFERS:
1516 if (framebuffer->getSamples() != 0)
1517 {
1518 *params = 1;
1519 }
1520 else
1521 {
1522 *params = 0;
1523 }
1524 break;
1525 case GL_SAMPLES:
1526 *params = framebuffer->getSamples();
1527 break;
1528 }
1529 }
1530 else
1531 {
1532 *params = 0;
1533 }
1534 }
1535 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001536 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1537 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1538 case GL_MAX_VIEWPORT_DIMS:
1539 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001540 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001541 params[0] = maxDimension;
1542 params[1] = maxDimension;
1543 }
1544 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001545 case GL_COMPRESSED_TEXTURE_FORMATS:
1546 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001547 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00001548 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001549 *params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1550 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1551 }
1552 if (supportsDXT3Textures())
1553 {
1554 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
1555 }
1556 if (supportsDXT5Textures())
1557 {
1558 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001559 }
1560 }
1561 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001562 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001563 params[0] = mState.viewportX;
1564 params[1] = mState.viewportY;
1565 params[2] = mState.viewportWidth;
1566 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001567 break;
1568 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001569 params[0] = mState.scissorX;
1570 params[1] = mState.scissorY;
1571 params[2] = mState.scissorWidth;
1572 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001573 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001574 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1575 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001576 case GL_RED_BITS:
1577 case GL_GREEN_BITS:
1578 case GL_BLUE_BITS:
1579 case GL_ALPHA_BITS:
1580 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001581 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001582 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001583
1584 if (colorbuffer)
1585 {
1586 switch (pname)
1587 {
1588 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1589 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1590 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1591 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1592 }
1593 }
1594 else
1595 {
1596 *params = 0;
1597 }
1598 }
1599 break;
1600 case GL_DEPTH_BITS:
1601 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001602 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001603 gl::Renderbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001604
1605 if (depthbuffer)
1606 {
1607 *params = depthbuffer->getDepthSize();
1608 }
1609 else
1610 {
1611 *params = 0;
1612 }
1613 }
1614 break;
1615 case GL_STENCIL_BITS:
1616 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001617 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001618 gl::Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001619
1620 if (stencilbuffer)
1621 {
1622 *params = stencilbuffer->getStencilSize();
1623 }
1624 else
1625 {
1626 *params = 0;
1627 }
1628 }
1629 break;
1630 case GL_TEXTURE_BINDING_2D:
1631 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001632 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001633 {
1634 error(GL_INVALID_OPERATION);
1635 return false;
1636 }
1637
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001638 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001639 }
1640 break;
1641 case GL_TEXTURE_BINDING_CUBE_MAP:
1642 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001643 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001644 {
1645 error(GL_INVALID_OPERATION);
1646 return false;
1647 }
1648
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001649 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001650 }
1651 break;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001652 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
1653 *params = mResetStrategy;
1654 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001655 default:
1656 return false;
1657 }
1658
1659 return true;
1660}
1661
1662bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1663{
1664 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1665 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1666 // to the fact that it is stored internally as a float, and so would require conversion
1667 // if returned from Context::getIntegerv. Since this conversion is already implemented
1668 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1669 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1670 // application.
1671 switch (pname)
1672 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001673 case GL_COMPRESSED_TEXTURE_FORMATS:
1674 {
1675 *type = GL_INT;
1676 *numParams = mNumCompressedTextureFormats;
1677 }
1678 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001679 case GL_SHADER_BINARY_FORMATS:
1680 {
1681 *type = GL_INT;
1682 *numParams = 0;
1683 }
1684 break;
1685 case GL_MAX_VERTEX_ATTRIBS:
1686 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1687 case GL_MAX_VARYING_VECTORS:
1688 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1689 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1690 case GL_MAX_TEXTURE_IMAGE_UNITS:
1691 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1692 case GL_MAX_RENDERBUFFER_SIZE:
1693 case GL_NUM_SHADER_BINARY_FORMATS:
1694 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1695 case GL_ARRAY_BUFFER_BINDING:
1696 case GL_FRAMEBUFFER_BINDING:
1697 case GL_RENDERBUFFER_BINDING:
1698 case GL_CURRENT_PROGRAM:
1699 case GL_PACK_ALIGNMENT:
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00001700 case GL_PACK_REVERSE_ROW_ORDER_ANGLE:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001701 case GL_UNPACK_ALIGNMENT:
1702 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001703 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001704 case GL_RED_BITS:
1705 case GL_GREEN_BITS:
1706 case GL_BLUE_BITS:
1707 case GL_ALPHA_BITS:
1708 case GL_DEPTH_BITS:
1709 case GL_STENCIL_BITS:
1710 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1711 case GL_CULL_FACE_MODE:
1712 case GL_FRONT_FACE:
1713 case GL_ACTIVE_TEXTURE:
1714 case GL_STENCIL_FUNC:
1715 case GL_STENCIL_VALUE_MASK:
1716 case GL_STENCIL_REF:
1717 case GL_STENCIL_FAIL:
1718 case GL_STENCIL_PASS_DEPTH_FAIL:
1719 case GL_STENCIL_PASS_DEPTH_PASS:
1720 case GL_STENCIL_BACK_FUNC:
1721 case GL_STENCIL_BACK_VALUE_MASK:
1722 case GL_STENCIL_BACK_REF:
1723 case GL_STENCIL_BACK_FAIL:
1724 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1725 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1726 case GL_DEPTH_FUNC:
1727 case GL_BLEND_SRC_RGB:
1728 case GL_BLEND_SRC_ALPHA:
1729 case GL_BLEND_DST_RGB:
1730 case GL_BLEND_DST_ALPHA:
1731 case GL_BLEND_EQUATION_RGB:
1732 case GL_BLEND_EQUATION_ALPHA:
1733 case GL_STENCIL_WRITEMASK:
1734 case GL_STENCIL_BACK_WRITEMASK:
1735 case GL_STENCIL_CLEAR_VALUE:
1736 case GL_SUBPIXEL_BITS:
1737 case GL_MAX_TEXTURE_SIZE:
1738 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1739 case GL_SAMPLE_BUFFERS:
1740 case GL_SAMPLES:
1741 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1742 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1743 case GL_TEXTURE_BINDING_2D:
1744 case GL_TEXTURE_BINDING_CUBE_MAP:
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001745 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001746 {
1747 *type = GL_INT;
1748 *numParams = 1;
1749 }
1750 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001751 case GL_MAX_SAMPLES_ANGLE:
1752 {
1753 if (getMaxSupportedSamples() != 0)
1754 {
1755 *type = GL_INT;
1756 *numParams = 1;
1757 }
1758 else
1759 {
1760 return false;
1761 }
1762 }
1763 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001764 case GL_MAX_VIEWPORT_DIMS:
1765 {
1766 *type = GL_INT;
1767 *numParams = 2;
1768 }
1769 break;
1770 case GL_VIEWPORT:
1771 case GL_SCISSOR_BOX:
1772 {
1773 *type = GL_INT;
1774 *numParams = 4;
1775 }
1776 break;
1777 case GL_SHADER_COMPILER:
1778 case GL_SAMPLE_COVERAGE_INVERT:
1779 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001780 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1781 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1782 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1783 case GL_SAMPLE_COVERAGE:
1784 case GL_SCISSOR_TEST:
1785 case GL_STENCIL_TEST:
1786 case GL_DEPTH_TEST:
1787 case GL_BLEND:
1788 case GL_DITHER:
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001789 case GL_CONTEXT_ROBUST_ACCESS_EXT:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001790 {
1791 *type = GL_BOOL;
1792 *numParams = 1;
1793 }
1794 break;
1795 case GL_COLOR_WRITEMASK:
1796 {
1797 *type = GL_BOOL;
1798 *numParams = 4;
1799 }
1800 break;
1801 case GL_POLYGON_OFFSET_FACTOR:
1802 case GL_POLYGON_OFFSET_UNITS:
1803 case GL_SAMPLE_COVERAGE_VALUE:
1804 case GL_DEPTH_CLEAR_VALUE:
1805 case GL_LINE_WIDTH:
1806 {
1807 *type = GL_FLOAT;
1808 *numParams = 1;
1809 }
1810 break;
1811 case GL_ALIASED_LINE_WIDTH_RANGE:
1812 case GL_ALIASED_POINT_SIZE_RANGE:
1813 case GL_DEPTH_RANGE:
1814 {
1815 *type = GL_FLOAT;
1816 *numParams = 2;
1817 }
1818 break;
1819 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001820 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001821 {
1822 *type = GL_FLOAT;
1823 *numParams = 4;
1824 }
1825 break;
1826 default:
1827 return false;
1828 }
1829
1830 return true;
1831}
1832
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001833// Applies the render target surface, depth stencil surface, viewport rectangle and
1834// scissor rectangle to the Direct3D 9 device
1835bool Context::applyRenderTarget(bool ignoreViewport)
1836{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001837 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001838
1839 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1840 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001841 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001842 }
1843
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001844 bool renderTargetChanged = false;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001845 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1846 if (renderTargetSerial != mAppliedRenderTargetSerial)
1847 {
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001848 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001849 if (!renderTarget)
1850 {
1851 return false; // Context must be lost
1852 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001853 mDevice->SetRenderTarget(0, renderTarget);
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001854 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001855 mScissorStateDirty = true; // Scissor area must be clamped to render target's size-- this is different for different render targets.
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001856 renderTargetChanged = true;
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001857 renderTarget->Release();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001858 }
1859
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001860 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001861 unsigned int depthbufferSerial = 0;
1862 unsigned int stencilbufferSerial = 0;
1863 if (framebufferObject->getDepthbufferType() != GL_NONE)
1864 {
1865 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001866 if (!depthStencil)
1867 {
1868 ERR("Depth stencil pointer unexpectedly null.");
1869 return false;
1870 }
1871
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001872 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1873 }
1874 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1875 {
1876 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001877 if (!depthStencil)
1878 {
1879 ERR("Depth stencil pointer unexpectedly null.");
1880 return false;
1881 }
1882
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001883 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1884 }
1885
1886 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001887 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001888 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001889 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001890 mDevice->SetDepthStencilSurface(depthStencil);
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001891 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001892 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001893 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001894 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001895
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001896 if (!mRenderTargetDescInitialized || renderTargetChanged)
1897 {
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001898 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001899 if (!renderTarget)
1900 {
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001901 return false; // Context must be lost
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001902 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001903 renderTarget->GetDesc(&mRenderTargetDesc);
1904 mRenderTargetDescInitialized = true;
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001905 renderTarget->Release();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001906 }
1907
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001908 D3DVIEWPORT9 viewport;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001909
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001910 float zNear = clamp01(mState.zNear);
1911 float zFar = clamp01(mState.zFar);
1912
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001913 if (ignoreViewport)
1914 {
1915 viewport.X = 0;
1916 viewport.Y = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001917 viewport.Width = mRenderTargetDesc.Width;
1918 viewport.Height = mRenderTargetDesc.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001919 viewport.MinZ = 0.0f;
1920 viewport.MaxZ = 1.0f;
1921 }
1922 else
1923 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001924 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, mRenderTargetDesc.Height);
1925 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1926 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1927 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width) - static_cast<LONG>(viewport.X));
1928 viewport.Height = clamp(rect.bottom - rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height) - static_cast<LONG>(viewport.Y));
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001929 viewport.MinZ = zNear;
1930 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001931 }
1932
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001933 if (viewport.Width <= 0 || viewport.Height <= 0)
1934 {
1935 return false; // Nothing to render
1936 }
1937
jbauman@chromium.org241e70d2011-11-03 23:07:05 +00001938 if (renderTargetChanged || !mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001939 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001940 mDevice->SetViewport(&viewport);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001941 mSetViewport = viewport;
1942 mViewportInitialized = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001943 mDxUniformsDirty = true;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001944 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001945
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001946 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001947 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001948 if (mState.scissorTest)
1949 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001950 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, mRenderTargetDesc.Height);
1951 rect.left = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1952 rect.top = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1953 rect.right = clamp(rect.right, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1954 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001955 mDevice->SetScissorRect(&rect);
1956 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001957 }
1958 else
1959 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001960 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001961 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001962
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001963 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001964 }
1965
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001966 if (mState.currentProgram && mDxUniformsDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001967 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001968 Program *programObject = getCurrentProgram();
1969
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001970 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001971 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001972 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001973
daniel@transgaming.comd9a54f92011-12-22 18:32:53 +00001974 // These values are used for computing gl_FragCoord in Program::linkVaryings(). The approach depends on Shader Model 3.0 support.
1975 GLint coord = programObject->getDxCoordLocation();
1976 float h = mSupportsShaderModel3 ? mRenderTargetDesc.Height : mState.viewportHeight / 2.0f;
1977 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, h,
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001978 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1979 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.comd9a54f92011-12-22 18:32:53 +00001980 programObject->setUniform4fv(coord, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001981
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001982 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001983 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001984 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001985
daniel@transgaming.com31754962010-11-28 02:02:52 +00001986 GLint depthRange = programObject->getDxDepthRangeLocation();
1987 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1988 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001989 mDxUniformsDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001990 }
1991
1992 return true;
1993}
1994
1995// Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc) to the Direct3D 9 device
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001996void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001997{
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001998 Program *programObject = getCurrentProgram();
1999
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002000 Framebuffer *framebufferObject = getDrawFramebuffer();
2001
2002 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
2003
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00002004 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002005 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00002006 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002007
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00002008 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002009 GLint alwaysFront = !isTriangleMode(drawMode);
2010 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
2011
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002012 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002013 bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
2014 // Apparently some ATI cards have a bug where a draw with a zero color
2015 // write mask can cause later draws to have incorrect results. Instead,
2016 // set a nonzero color write mask but modify the blend state so that no
2017 // drawing is done.
2018 // http://code.google.com/p/angleproject/issues/detail?id=169
2019
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002020 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002021 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002022 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002023 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002024 mDevice->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002025 }
2026 else
2027 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002028 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002029 }
2030
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002031 mCullStateDirty = false;
2032 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002033
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002034 if (mDepthStateDirty)
2035 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00002036 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002037 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002038 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
2039 mDevice->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002040 }
2041 else
2042 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002043 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002044 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002045
2046 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002047 }
2048
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002049 if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
2050 {
2051 mBlendStateDirty = true;
2052 mMaskStateDirty = true;
2053 }
2054
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002055 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002056 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002057 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00002058 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002059 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002060
2061 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
2062 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
2063 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002064 mDevice->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002065 }
2066 else
2067 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002068 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002069 unorm<8>(mState.blendColor.alpha),
2070 unorm<8>(mState.blendColor.alpha),
2071 unorm<8>(mState.blendColor.alpha)));
2072 }
2073
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002074 mDevice->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
2075 mDevice->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
2076 mDevice->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002077
2078 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
2079 mState.destBlendRGB != mState.destBlendAlpha ||
2080 mState.blendEquationRGB != mState.blendEquationAlpha)
2081 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002082 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002083
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002084 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
2085 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
2086 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002087 }
2088 else
2089 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002090 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002091 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00002092 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002093 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00002094 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002095 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00002096 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002097
2098 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002099 }
2100
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002101 if (mStencilStateDirty || mFrontFaceDirty)
2102 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002103 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002104 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002105 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2106 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002107
2108 // FIXME: Unsupported by D3D9
2109 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
2110 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
2111 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
2112 if (mState.stencilWritemask != mState.stencilBackWritemask ||
2113 mState.stencilRef != mState.stencilBackRef ||
2114 mState.stencilMask != mState.stencilBackMask)
2115 {
2116 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
2117 return error(GL_INVALID_OPERATION);
2118 }
2119
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00002120 // get the maximum size of the stencil ref
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00002121 gl::Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00002122 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
2123
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002124 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
2125 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002126 es2dx::ConvertComparison(mState.stencilFunc));
2127
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002128 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
2129 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002130
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002131 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002132 es2dx::ConvertStencilOp(mState.stencilFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002133 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002134 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002135 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002136 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
2137
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002138 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
2139 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002140 es2dx::ConvertComparison(mState.stencilBackFunc));
2141
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002142 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
2143 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002144
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002145 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002146 es2dx::ConvertStencilOp(mState.stencilBackFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002147 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002148 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002149 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002150 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
2151 }
2152 else
2153 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002154 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002155 }
2156
2157 mStencilStateDirty = false;
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002158 mFrontFaceDirty = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002159 }
2160
2161 if (mMaskStateDirty)
2162 {
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002163 int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
2164 mState.colorMaskBlue, mState.colorMaskAlpha);
2165 if (colorMask == 0 && !zeroColorMaskAllowed)
2166 {
2167 // Enable green channel, but set blending so nothing will be drawn.
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002168 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
2169 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002170
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002171 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
2172 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
2173 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002174 }
2175 else
2176 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002177 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002178 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002179 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002180
2181 mMaskStateDirty = false;
2182 }
2183
2184 if (mPolygonOffsetStateDirty)
2185 {
2186 if (mState.polygonOffsetFill)
2187 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00002188 gl::Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002189 if (depthbuffer)
2190 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002191 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002192 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002193 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002194 }
2195 }
2196 else
2197 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002198 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
2199 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002200 }
2201
2202 mPolygonOffsetStateDirty = false;
2203 }
2204
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002205 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002206 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002207 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002208 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002209 FIXME("Sample alpha to coverage is unimplemented.");
2210 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002211
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002212 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002213 if (mState.sampleCoverage)
2214 {
2215 unsigned int mask = 0;
2216 if (mState.sampleCoverageValue != 0)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002217 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002218 float threshold = 0.5f;
2219
2220 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002221 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002222 mask <<= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002223
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002224 if ((i + 1) * mState.sampleCoverageValue >= threshold)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002225 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002226 threshold += 1.0f;
2227 mask |= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002228 }
2229 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002230 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002231
2232 if (mState.sampleCoverageInvert)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002233 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002234 mask = ~mask;
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002235 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002236
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002237 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002238 }
2239 else
2240 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002241 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002242 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002243
2244 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002245 }
2246
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002247 if (mDitherStateDirty)
2248 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002249 mDevice->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002250
2251 mDitherStateDirty = false;
2252 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002253}
2254
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002255GLenum Context::applyVertexBuffer(GLint first, GLsizei count, GLsizei instances, GLsizei *repeatDraw)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002256{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002257 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002258
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +00002259 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes, instances);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002260 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002261 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002262 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002263 }
2264
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002265 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, getCurrentProgram(), instances, repeatDraw);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002266}
2267
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002268// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.comd2820bf2012-01-27 15:38:48 +00002269GLenum Context::applyIndexBuffer(const GLvoid *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002270{
daniel@transgaming.com83921382011-01-08 05:46:00 +00002271 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002272
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002273 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002274 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002275 if (indexInfo->serial != mAppliedIBSerial)
2276 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002277 mDevice->SetIndices(indexInfo->indexBuffer);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002278 mAppliedIBSerial = indexInfo->serial;
2279 }
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002280 }
2281
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002282 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002283}
2284
2285// Applies the shaders and shader constants to the Direct3D 9 device
2286void Context::applyShaders()
2287{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002288 Program *programObject = getCurrentProgram();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002289 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002290 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002291 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2292 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2293
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002294 mDevice->SetPixelShader(pixelShader);
2295 mDevice->SetVertexShader(vertexShader);
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002296 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002297 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002298 }
2299
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002300 programObject->applyUniforms();
2301}
2302
2303// Applies the textures and sampler states to the Direct3D 9 device
2304void Context::applyTextures()
2305{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002306 applyTextures(SAMPLER_PIXEL);
2307
2308 if (mSupportsVertexTexture)
2309 {
2310 applyTextures(SAMPLER_VERTEX);
2311 }
2312}
2313
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002314// For each Direct3D 9 sampler of either the pixel or vertex stage,
2315// looks up the corresponding OpenGL texture image unit and texture type,
2316// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002317void Context::applyTextures(SamplerType type)
2318{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002319 Program *programObject = getCurrentProgram();
2320
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002321 int samplerCount = (type == SAMPLER_PIXEL) ? MAX_TEXTURE_IMAGE_UNITS : MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; // Range of Direct3D 9 samplers of given sampler type
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00002322 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2323 int d3dSamplerOffset = (type == SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002324 int samplerRange = programObject->getUsedSamplerRange(type);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002325
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002326 for (int samplerIndex = 0; samplerIndex < samplerRange; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002327 {
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002328 int textureUnit = programObject->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00002329 int d3dSampler = samplerIndex + d3dSamplerOffset;
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002330
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002331 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002332 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002333 TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002334
2335 Texture *texture = getSamplerTexture(textureUnit, textureType);
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002336 unsigned int texSerial = texture->getTextureSerial();
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002337
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002338 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyParameters() || texture->hasDirtyImages())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002339 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002340 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2341
2342 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002343 {
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002344 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyParameters())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002345 {
2346 GLenum wrapS = texture->getWrapS();
2347 GLenum wrapT = texture->getWrapT();
2348 GLenum minFilter = texture->getMinFilter();
2349 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002350
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002351 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2352 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002353
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002354 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002355 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2356 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002357 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2358 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002359 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002360
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002361 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyImages())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002362 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002363 mDevice->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002364 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002365 }
2366 else
2367 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002368 mDevice->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002369 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002370
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002371 appliedTextureSerial[samplerIndex] = texSerial;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002372 texture->resetDirty();
2373 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002374 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002375 else
2376 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002377 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002378 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002379 mDevice->SetTexture(d3dSampler, NULL);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002380 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002381 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002382 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002383 }
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002384
2385 for (int samplerIndex = samplerRange; samplerIndex < samplerCount; samplerIndex++)
2386 {
2387 if (appliedTextureSerial[samplerIndex] != 0)
2388 {
daniel@transgaming.comc5a7b692011-10-26 02:45:44 +00002389 mDevice->SetTexture(samplerIndex + d3dSamplerOffset, NULL);
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002390 appliedTextureSerial[samplerIndex] = 0;
2391 }
2392 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002393}
2394
daniel@transgaming.comb7915a52011-11-12 03:14:20 +00002395void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height,
2396 GLenum format, GLenum type, GLsizei *bufSize, void* pixels)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002397{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002398 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002399
2400 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2401 {
2402 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2403 }
2404
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002405 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2406 {
2407 return error(GL_INVALID_OPERATION);
2408 }
2409
daniel@transgaming.comb7915a52011-11-12 03:14:20 +00002410 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
2411 // sized query sanity check
2412 if (bufSize)
2413 {
2414 int requiredSize = outputPitch * height;
2415 if (requiredSize > *bufSize)
2416 {
2417 return error(GL_INVALID_OPERATION);
2418 }
2419 }
2420
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002421 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002422 if (!renderTarget)
2423 {
2424 return; // Context must be lost, return silently
2425 }
2426
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002427 D3DSURFACE_DESC desc;
2428 renderTarget->GetDesc(&desc);
2429
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002430 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2431 {
2432 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.coma5798952011-12-13 17:30:43 +00002433 renderTarget->Release();
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002434 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002435 }
2436
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002437 HRESULT result;
2438 IDirect3DSurface9 *systemSurface = NULL;
2439 bool directToPixels = getPackReverseRowOrder() && getPackAlignment() <= 4 && mDisplay->isD3d9ExDevice() &&
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00002440 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002441 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2442 if (directToPixels)
2443 {
2444 // Use the pixels ptr as a shared handle to write directly into client's memory
2445 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2446 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2447 if (FAILED(result))
2448 {
2449 // Try again without the shared handle
2450 directToPixels = false;
2451 }
2452 }
2453
2454 if (!directToPixels)
2455 {
2456 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2457 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2458 if (FAILED(result))
2459 {
2460 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2461 return error(GL_OUT_OF_MEMORY);
2462 }
2463 }
2464
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002465 result = mDevice->GetRenderTargetData(renderTarget, systemSurface);
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00002466 renderTarget->Release();
daniel@transgaming.coma5798952011-12-13 17:30:43 +00002467 renderTarget = NULL;
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00002468
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002469 if (FAILED(result))
2470 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002471 systemSurface->Release();
2472
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002473 // It turns out that D3D will sometimes produce more error
2474 // codes than those documented.
2475 if (checkDeviceLost(result))
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002476 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002477 else
2478 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002479 UNREACHABLE();
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002480 return;
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002481 }
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002482
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002483 }
2484
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002485 if (directToPixels)
2486 {
2487 systemSurface->Release();
2488 return;
2489 }
2490
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002491 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002492 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2493 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2494 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2495 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2496 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002497
2498 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2499
2500 if (FAILED(result))
2501 {
2502 UNREACHABLE();
2503 systemSurface->Release();
2504
2505 return; // No sensible error to generate
2506 }
2507
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002508 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002509 unsigned short *dest16 = (unsigned short*)pixels;
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002510
2511 unsigned char *source;
2512 int inputPitch;
2513 if (getPackReverseRowOrder())
2514 {
2515 source = (unsigned char*)lock.pBits;
2516 inputPitch = lock.Pitch;
2517 }
2518 else
2519 {
2520 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2521 inputPitch = -lock.Pitch;
2522 }
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002523
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002524 for (int j = 0; j < rect.bottom - rect.top; j++)
2525 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002526 if (desc.Format == D3DFMT_A8R8G8B8 &&
2527 format == GL_BGRA_EXT &&
2528 type == GL_UNSIGNED_BYTE)
2529 {
2530 // Fast path for EXT_read_format_bgra, given
2531 // an RGBA source buffer. Note that buffers with no
2532 // alpha go through the slow path below.
2533 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002534 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002535 (rect.right - rect.left) * 4);
2536 continue;
2537 }
2538
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002539 for (int i = 0; i < rect.right - rect.left; i++)
2540 {
2541 float r;
2542 float g;
2543 float b;
2544 float a;
2545
2546 switch (desc.Format)
2547 {
2548 case D3DFMT_R5G6B5:
2549 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002550 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002551
2552 a = 1.0f;
2553 b = (rgb & 0x001F) * (1.0f / 0x001F);
2554 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2555 r = (rgb & 0xF800) * (1.0f / 0xF800);
2556 }
2557 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002558 case D3DFMT_A1R5G5B5:
2559 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002560 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002561
2562 a = (argb & 0x8000) ? 1.0f : 0.0f;
2563 b = (argb & 0x001F) * (1.0f / 0x001F);
2564 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2565 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2566 }
2567 break;
2568 case D3DFMT_A8R8G8B8:
2569 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002570 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002571
2572 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2573 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2574 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2575 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2576 }
2577 break;
2578 case D3DFMT_X8R8G8B8:
2579 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002580 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002581
2582 a = 1.0f;
2583 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2584 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2585 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2586 }
2587 break;
2588 case D3DFMT_A2R10G10B10:
2589 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002590 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002591
2592 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2593 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2594 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2595 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2596 }
2597 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002598 case D3DFMT_A32B32G32R32F:
2599 {
2600 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002601 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2602 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2603 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2604 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002605 }
2606 break;
2607 case D3DFMT_A16B16G16R16F:
2608 {
2609 // float formats in D3D are stored rgba, rather than the other way round
2610 float abgr[4];
2611
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002612 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002613
2614 a = abgr[3];
2615 b = abgr[2];
2616 g = abgr[1];
2617 r = abgr[0];
2618 }
2619 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002620 default:
2621 UNIMPLEMENTED(); // FIXME
2622 UNREACHABLE();
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00002623 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002624 }
2625
2626 switch (format)
2627 {
2628 case GL_RGBA:
2629 switch (type)
2630 {
2631 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002632 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2633 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2634 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2635 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002636 break;
2637 default: UNREACHABLE();
2638 }
2639 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002640 case GL_BGRA_EXT:
2641 switch (type)
2642 {
2643 case GL_UNSIGNED_BYTE:
2644 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2645 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2646 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2647 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2648 break;
2649 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2650 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2651 // this type is packed as follows:
2652 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2653 // --------------------------------------------------------------------------------
2654 // | 4th | 3rd | 2nd | 1st component |
2655 // --------------------------------------------------------------------------------
2656 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2657 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2658 ((unsigned short)(15 * a + 0.5f) << 12)|
2659 ((unsigned short)(15 * r + 0.5f) << 8) |
2660 ((unsigned short)(15 * g + 0.5f) << 4) |
2661 ((unsigned short)(15 * b + 0.5f) << 0);
2662 break;
2663 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2664 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2665 // this type is packed as follows:
2666 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2667 // --------------------------------------------------------------------------------
2668 // | 4th | 3rd | 2nd | 1st component |
2669 // --------------------------------------------------------------------------------
2670 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2671 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2672 ((unsigned short)( a + 0.5f) << 15) |
2673 ((unsigned short)(31 * r + 0.5f) << 10) |
2674 ((unsigned short)(31 * g + 0.5f) << 5) |
2675 ((unsigned short)(31 * b + 0.5f) << 0);
2676 break;
2677 default: UNREACHABLE();
2678 }
2679 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002680 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002681 switch (type)
2682 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002683 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002684 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2685 ((unsigned short)(31 * b + 0.5f) << 0) |
2686 ((unsigned short)(63 * g + 0.5f) << 5) |
2687 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002688 break;
2689 default: UNREACHABLE();
2690 }
2691 break;
2692 default: UNREACHABLE();
2693 }
2694 }
2695 }
2696
2697 systemSurface->UnlockRect();
2698
2699 systemSurface->Release();
2700}
2701
2702void Context::clear(GLbitfield mask)
2703{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002704 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002705
2706 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2707 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002708 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002709 }
2710
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002711 DWORD flags = 0;
2712
2713 if (mask & GL_COLOR_BUFFER_BIT)
2714 {
2715 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002716
2717 if (framebufferObject->getColorbufferType() != GL_NONE)
2718 {
2719 flags |= D3DCLEAR_TARGET;
2720 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002721 }
2722
2723 if (mask & GL_DEPTH_BUFFER_BIT)
2724 {
2725 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002726 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002727 {
2728 flags |= D3DCLEAR_ZBUFFER;
2729 }
2730 }
2731
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002732 GLuint stencilUnmasked = 0x0;
2733
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002734 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002735 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002736 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002737 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002738 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002739 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002740 if (!depthStencil)
2741 {
2742 ERR("Depth stencil pointer unexpectedly null.");
2743 return;
2744 }
2745
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002746 D3DSURFACE_DESC desc;
2747 depthStencil->GetDesc(&desc);
2748
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002749 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002750 stencilUnmasked = (0x1 << stencilSize) - 1;
2751
2752 if (stencilUnmasked != 0x0)
2753 {
2754 flags |= D3DCLEAR_STENCIL;
2755 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002756 }
2757 }
2758
2759 if (mask != 0)
2760 {
2761 return error(GL_INVALID_VALUE);
2762 }
2763
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002764 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2765 {
2766 return;
2767 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002768
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002769 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002770 unorm<8>(mState.colorClearValue.red),
2771 unorm<8>(mState.colorClearValue.green),
2772 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002773 float depth = clamp01(mState.depthClearValue);
2774 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002775
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002776 bool alphaUnmasked = (dx2es::GetAlphaSize(mRenderTargetDesc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002777
2778 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002779 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002780 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002781 !(mState.colorMaskRed && mState.colorMaskGreen &&
2782 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002783
2784 if (needMaskedColorClear || needMaskedStencilClear)
2785 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002786 // State which is altered in all paths from this point to the clear call is saved.
2787 // State which is altered in only some paths will be flagged dirty in the case that
2788 // that path is taken.
2789 HRESULT hr;
2790 if (mMaskedClearSavedState == NULL)
2791 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002792 hr = mDevice->BeginStateBlock();
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002793 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2794
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002795 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2796 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2797 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2798 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2799 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2800 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2801 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2802 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2803 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2804 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2805 mDevice->SetPixelShader(NULL);
2806 mDevice->SetVertexShader(NULL);
2807 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2808 mDevice->SetStreamSource(0, NULL, 0, 0);
2809 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2810 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2811 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2812 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2813 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2814 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2815 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002816
2817 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2818 {
2819 mDevice->SetStreamSourceFreq(i, 1);
2820 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002821
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002822 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002823 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2824 }
2825
2826 ASSERT(mMaskedClearSavedState != NULL);
2827
2828 if (mMaskedClearSavedState != NULL)
2829 {
2830 hr = mMaskedClearSavedState->Capture();
2831 ASSERT(SUCCEEDED(hr));
2832 }
2833
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002834 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2835 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2836 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2837 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2838 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2839 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2840 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2841 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002842
2843 if (flags & D3DCLEAR_TARGET)
2844 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002845 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002846 }
2847 else
2848 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002849 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002850 }
2851
2852 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2853 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002854 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2855 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2856 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2857 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
2858 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
2859 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
2860 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2861 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002862 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002863 }
2864 else
2865 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002866 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002867 }
2868
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002869 mDevice->SetPixelShader(NULL);
2870 mDevice->SetVertexShader(NULL);
2871 mDevice->SetFVF(D3DFVF_XYZRHW);
2872 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2873 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2874 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2875 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2876 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2877 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2878 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002879
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002880 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2881 {
2882 mDevice->SetStreamSourceFreq(i, 1);
2883 }
2884
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002885 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2886 quad[0][0] = -0.5f;
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002887 quad[0][1] = mRenderTargetDesc.Height - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002888 quad[0][2] = 0.0f;
2889 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002890
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002891 quad[1][0] = mRenderTargetDesc.Width - 0.5f;
2892 quad[1][1] = mRenderTargetDesc.Height - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002893 quad[1][2] = 0.0f;
2894 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002895
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002896 quad[2][0] = -0.5f;
2897 quad[2][1] = -0.5f;
2898 quad[2][2] = 0.0f;
2899 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002900
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002901 quad[3][0] = mRenderTargetDesc.Width - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002902 quad[3][1] = -0.5f;
2903 quad[3][2] = 0.0f;
2904 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002905
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002906 mDisplay->startScene();
2907 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002908
2909 if (flags & D3DCLEAR_ZBUFFER)
2910 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002911 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
2912 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2913 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002914 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002915
2916 if (mMaskedClearSavedState != NULL)
2917 {
2918 mMaskedClearSavedState->Apply();
2919 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002920 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002921 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002922 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002923 mDevice->Clear(0, NULL, flags, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002924 }
2925}
2926
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002927void Context::drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002928{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002929 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002930 {
2931 return error(GL_INVALID_OPERATION);
2932 }
2933
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002934 D3DPRIMITIVETYPE primitiveType;
2935 int primitiveCount;
2936
2937 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2938 return error(GL_INVALID_ENUM);
2939
2940 if (primitiveCount <= 0)
2941 {
2942 return;
2943 }
2944
2945 if (!applyRenderTarget(false))
2946 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002947 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002948 }
2949
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002950 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002951
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002952 GLsizei repeatDraw = 1;
2953 GLenum err = applyVertexBuffer(first, count, instances, &repeatDraw);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002954 if (err != GL_NO_ERROR)
2955 {
2956 return error(err);
2957 }
2958
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002959 applyShaders();
2960 applyTextures();
2961
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002962 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002963 {
2964 return error(GL_INVALID_OPERATION);
2965 }
2966
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002967 if (!cullSkipsDraw(mode))
2968 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002969 mDisplay->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002970
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002971 if (mode == GL_LINE_LOOP)
daniel@transgaming.comf6549452012-01-27 15:39:08 +00002972 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002973 drawLineLoop(count, GL_NONE, NULL, 0);
daniel@transgaming.comf6549452012-01-27 15:39:08 +00002974 }
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002975 else if (instances > 0)
daniel@transgaming.comf6549452012-01-27 15:39:08 +00002976 {
2977 StaticIndexBuffer *countingIB = mIndexDataManager->getCountingIndices(count);
2978 if (countingIB)
2979 {
2980 if (mAppliedIBSerial != countingIB->getSerial())
2981 {
2982 mDevice->SetIndices(countingIB->getBuffer());
2983 mAppliedIBSerial = countingIB->getSerial();
2984 }
2985
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002986 for (int i = 0; i < repeatDraw; i++)
2987 {
2988 mDevice->DrawIndexedPrimitive(primitiveType, 0, 0, count, 0, primitiveCount);
2989 }
daniel@transgaming.comf6549452012-01-27 15:39:08 +00002990 }
2991 else
2992 {
2993 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
2994 return error(GL_OUT_OF_MEMORY);
2995 }
2996 }
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002997 else // Regular case
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002998 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002999 mDevice->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003000 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003001 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003002}
3003
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00003004void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, GLsizei instances)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003005{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003006 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003007 {
3008 return error(GL_INVALID_OPERATION);
3009 }
3010
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003011 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003012 {
3013 return error(GL_INVALID_OPERATION);
3014 }
3015
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003016 D3DPRIMITIVETYPE primitiveType;
3017 int primitiveCount;
3018
3019 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
3020 return error(GL_INVALID_ENUM);
3021
3022 if (primitiveCount <= 0)
3023 {
3024 return;
3025 }
3026
3027 if (!applyRenderTarget(false))
3028 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00003029 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003030 }
3031
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003032 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00003033
3034 TranslatedIndexData indexInfo;
3035 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
3036 if (err != GL_NO_ERROR)
3037 {
3038 return error(err);
3039 }
3040
daniel@transgaming.com83921382011-01-08 05:46:00 +00003041 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
daniel@transgaming.comd6449312012-01-27 15:39:32 +00003042 GLsizei repeatDraw = 1;
3043 err = applyVertexBuffer(indexInfo.minIndex, vertexCount, instances, &repeatDraw);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00003044 if (err != GL_NO_ERROR)
3045 {
3046 return error(err);
3047 }
3048
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003049 applyShaders();
3050 applyTextures();
3051
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00003052 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00003053 {
3054 return error(GL_INVALID_OPERATION);
3055 }
3056
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003057 if (!cullSkipsDraw(mode))
3058 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003059 mDisplay->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003060
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003061 if (mode == GL_LINE_LOOP)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003062 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003063 drawLineLoop(count, type, indices, indexInfo.minIndex);
3064 }
3065 else
3066 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00003067 for (int i = 0; i < repeatDraw; i++)
3068 {
3069 mDevice->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
3070 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003071 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003072 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003073}
3074
daniel@transgaming.com0d86aa72011-10-26 02:35:10 +00003075// Implements glFlush when block is false, glFinish when block is true
3076void Context::sync(bool block)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003077{
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +00003078 mDisplay->sync(block);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003079}
3080
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003081void Context::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003082{
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003083 // Get the raw indices for an indexed draw
3084 if (type != GL_NONE && mState.elementArrayBuffer.get())
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003085 {
3086 Buffer *indexBuffer = mState.elementArrayBuffer.get();
3087 intptr_t offset = reinterpret_cast<intptr_t>(indices);
3088 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
3089 }
3090
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003091 UINT startIndex = 0;
3092 bool succeeded = false;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003093
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003094 if (supports32bitIndices())
3095 {
3096 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
3097
3098 if (!mLineLoopIB)
3099 {
3100 mLineLoopIB = new StreamingIndexBuffer(mDevice, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
3101 }
3102
3103 if (mLineLoopIB)
3104 {
3105 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
3106
3107 UINT offset = 0;
3108 unsigned int *data = static_cast<unsigned int*>(mLineLoopIB->map(spaceNeeded, &offset));
3109 startIndex = offset / 4;
3110
3111 if (data)
3112 {
3113 switch (type)
3114 {
3115 case GL_NONE: // Non-indexed draw
3116 for (int i = 0; i < count; i++)
3117 {
3118 data[i] = i;
3119 }
3120 data[count] = 0;
3121 break;
3122 case GL_UNSIGNED_BYTE:
3123 for (int i = 0; i < count; i++)
3124 {
3125 data[i] = static_cast<const GLubyte*>(indices)[i];
3126 }
3127 data[count] = static_cast<const GLubyte*>(indices)[0];
3128 break;
3129 case GL_UNSIGNED_SHORT:
3130 for (int i = 0; i < count; i++)
3131 {
3132 data[i] = static_cast<const GLushort*>(indices)[i];
3133 }
3134 data[count] = static_cast<const GLushort*>(indices)[0];
3135 break;
3136 case GL_UNSIGNED_INT:
3137 for (int i = 0; i < count; i++)
3138 {
3139 data[i] = static_cast<const GLuint*>(indices)[i];
3140 }
3141 data[count] = static_cast<const GLuint*>(indices)[0];
3142 break;
3143 default: UNREACHABLE();
3144 }
3145
3146 mLineLoopIB->unmap();
3147 succeeded = true;
3148 }
3149 }
3150 }
3151 else
3152 {
3153 const int spaceNeeded = (count + 1) * sizeof(unsigned short);
3154
3155 if (!mLineLoopIB)
3156 {
3157 mLineLoopIB = new StreamingIndexBuffer(mDevice, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
3158 }
3159
3160 if (mLineLoopIB)
3161 {
3162 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
3163
3164 UINT offset = 0;
3165 unsigned short *data = static_cast<unsigned short*>(mLineLoopIB->map(spaceNeeded, &offset));
3166 startIndex = offset / 2;
3167
3168 if (data)
3169 {
3170 switch (type)
3171 {
3172 case GL_NONE: // Non-indexed draw
3173 for (int i = 0; i < count; i++)
3174 {
3175 data[i] = i;
3176 }
3177 data[count] = 0;
3178 break;
3179 case GL_UNSIGNED_BYTE:
3180 for (int i = 0; i < count; i++)
3181 {
3182 data[i] = static_cast<const GLubyte*>(indices)[i];
3183 }
3184 data[count] = static_cast<const GLubyte*>(indices)[0];
3185 break;
3186 case GL_UNSIGNED_SHORT:
3187 for (int i = 0; i < count; i++)
3188 {
3189 data[i] = static_cast<const GLushort*>(indices)[i];
3190 }
3191 data[count] = static_cast<const GLushort*>(indices)[0];
3192 break;
3193 case GL_UNSIGNED_INT:
3194 for (int i = 0; i < count; i++)
3195 {
3196 data[i] = static_cast<const GLuint*>(indices)[i];
3197 }
3198 data[count] = static_cast<const GLuint*>(indices)[0];
3199 break;
3200 default: UNREACHABLE();
3201 }
3202
3203 mLineLoopIB->unmap();
3204 succeeded = true;
3205 }
3206 }
3207 }
3208
3209 if (succeeded)
3210 {
3211 if (mAppliedIBSerial != mLineLoopIB->getSerial())
3212 {
3213 mDevice->SetIndices(mLineLoopIB->getBuffer());
3214 mAppliedIBSerial = mLineLoopIB->getSerial();
3215 }
3216
3217 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
3218 }
3219 else
3220 {
3221 ERR("Could not create a looping index buffer for GL_LINE_LOOP.");
3222 return error(GL_OUT_OF_MEMORY);
3223 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003224}
3225
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003226void Context::recordInvalidEnum()
3227{
3228 mInvalidEnum = true;
3229}
3230
3231void Context::recordInvalidValue()
3232{
3233 mInvalidValue = true;
3234}
3235
3236void Context::recordInvalidOperation()
3237{
3238 mInvalidOperation = true;
3239}
3240
3241void Context::recordOutOfMemory()
3242{
3243 mOutOfMemory = true;
3244}
3245
3246void Context::recordInvalidFramebufferOperation()
3247{
3248 mInvalidFramebufferOperation = true;
3249}
3250
3251// Get one of the recorded errors and clear its flag, if any.
3252// [OpenGL ES 2.0.24] section 2.5 page 13.
3253GLenum Context::getError()
3254{
3255 if (mInvalidEnum)
3256 {
3257 mInvalidEnum = false;
3258
3259 return GL_INVALID_ENUM;
3260 }
3261
3262 if (mInvalidValue)
3263 {
3264 mInvalidValue = false;
3265
3266 return GL_INVALID_VALUE;
3267 }
3268
3269 if (mInvalidOperation)
3270 {
3271 mInvalidOperation = false;
3272
3273 return GL_INVALID_OPERATION;
3274 }
3275
3276 if (mOutOfMemory)
3277 {
3278 mOutOfMemory = false;
3279
3280 return GL_OUT_OF_MEMORY;
3281 }
3282
3283 if (mInvalidFramebufferOperation)
3284 {
3285 mInvalidFramebufferOperation = false;
3286
3287 return GL_INVALID_FRAMEBUFFER_OPERATION;
3288 }
3289
3290 return GL_NO_ERROR;
3291}
3292
daniel@transgaming.com17f548c2011-11-09 17:47:02 +00003293GLenum Context::getResetStatus()
3294{
3295 if (mResetStatus == GL_NO_ERROR)
3296 {
3297 bool lost = mDisplay->testDeviceLost();
3298
3299 if (lost)
3300 {
3301 mDisplay->notifyDeviceLost(); // Sets mResetStatus
3302 }
3303 }
3304
3305 GLenum status = mResetStatus;
3306
3307 if (mResetStatus != GL_NO_ERROR)
3308 {
3309 if (mDisplay->testDeviceResettable())
3310 {
3311 mResetStatus = GL_NO_ERROR;
3312 }
3313 }
3314
3315 return status;
3316}
3317
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00003318bool Context::isResetNotificationEnabled()
3319{
3320 return (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT);
3321}
3322
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003323bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003324{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003325 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003326}
3327
daniel@transgaming.com396c6432010-11-26 16:26:12 +00003328int Context::getMaximumVaryingVectors() const
3329{
3330 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3331}
3332
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003333unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003334{
3335 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3336}
3337
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003338unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003339{
3340 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3341}
3342
daniel@transgaming.com458da142010-11-28 02:03:02 +00003343int Context::getMaximumFragmentUniformVectors() const
3344{
3345 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3346}
3347
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003348int Context::getMaxSupportedSamples() const
3349{
3350 return mMaxSupportedSamples;
3351}
3352
3353int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3354{
3355 if (requested == 0)
3356 {
3357 return requested;
3358 }
3359
3360 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3361 if (itr == mMultiSampleSupport.end())
3362 {
3363 return -1;
3364 }
3365
3366 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3367 {
3368 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3369 {
3370 return i;
3371 }
3372 }
3373
3374 return -1;
3375}
3376
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003377bool Context::supportsEventQueries() const
3378{
3379 return mSupportsEventQueries;
3380}
3381
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00003382bool Context::supportsOcclusionQueries() const
3383{
3384 return mSupportsOcclusionQueries;
3385}
3386
gman@chromium.org50c526d2011-08-10 05:19:44 +00003387bool Context::supportsDXT1Textures() const
daniel@transgaming.com01868132010-08-24 19:21:17 +00003388{
gman@chromium.org50c526d2011-08-10 05:19:44 +00003389 return mSupportsDXT1Textures;
3390}
3391
3392bool Context::supportsDXT3Textures() const
3393{
3394 return mSupportsDXT3Textures;
3395}
3396
3397bool Context::supportsDXT5Textures() const
3398{
3399 return mSupportsDXT5Textures;
daniel@transgaming.com01868132010-08-24 19:21:17 +00003400}
3401
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003402bool Context::supportsFloat32Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003403{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003404 return mSupportsFloat32Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003405}
3406
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003407bool Context::supportsFloat32LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003408{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003409 return mSupportsFloat32LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003410}
3411
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003412bool Context::supportsFloat32RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003413{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003414 return mSupportsFloat32RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003415}
3416
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003417bool Context::supportsFloat16Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003418{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003419 return mSupportsFloat16Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003420}
3421
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003422bool Context::supportsFloat16LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003423{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003424 return mSupportsFloat16LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003425}
3426
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003427bool Context::supportsFloat16RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003428{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003429 return mSupportsFloat16RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003430}
3431
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003432int Context::getMaximumRenderbufferDimension() const
3433{
3434 return mMaxRenderbufferDimension;
3435}
3436
3437int Context::getMaximumTextureDimension() const
3438{
3439 return mMaxTextureDimension;
3440}
3441
3442int Context::getMaximumCubeTextureDimension() const
3443{
3444 return mMaxCubeTextureDimension;
3445}
3446
3447int Context::getMaximumTextureLevel() const
3448{
3449 return mMaxTextureLevel;
3450}
3451
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003452bool Context::supportsLuminanceTextures() const
3453{
3454 return mSupportsLuminanceTextures;
3455}
3456
3457bool Context::supportsLuminanceAlphaTextures() const
3458{
3459 return mSupportsLuminanceAlphaTextures;
3460}
3461
daniel@transgaming.com1c49f792012-05-31 01:14:02 +00003462bool Context::supportsDepthTextures() const
3463{
3464 return mSupportsDepthTextures;
3465}
3466
daniel@transgaming.com83921382011-01-08 05:46:00 +00003467bool Context::supports32bitIndices() const
3468{
3469 return mSupports32bitIndices;
3470}
3471
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003472bool Context::supportsNonPower2Texture() const
3473{
3474 return mSupportsNonPower2Texture;
3475}
3476
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +00003477bool Context::supportsInstancing() const
3478{
3479 return mSupportsInstancing;
3480}
3481
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003482void Context::detachBuffer(GLuint buffer)
3483{
3484 // [OpenGL ES 2.0.24] section 2.9 page 22:
3485 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3486 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3487
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003488 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003489 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003490 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003491 }
3492
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003493 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003494 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003495 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003496 }
3497
3498 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3499 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003500 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003501 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003502 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003503 }
3504 }
3505}
3506
3507void Context::detachTexture(GLuint texture)
3508{
3509 // [OpenGL ES 2.0.24] section 3.8 page 84:
3510 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3511 // rebound to texture object zero
3512
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003513 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003514 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003515 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003516 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003517 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003518 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003519 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003520 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003521 }
3522 }
3523
3524 // [OpenGL ES 2.0.24] section 4.4 page 112:
3525 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3526 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3527 // image was attached in the currently bound framebuffer.
3528
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003529 Framebuffer *readFramebuffer = getReadFramebuffer();
3530 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003531
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003532 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003533 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003534 readFramebuffer->detachTexture(texture);
3535 }
3536
3537 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3538 {
3539 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003540 }
3541}
3542
3543void Context::detachFramebuffer(GLuint framebuffer)
3544{
3545 // [OpenGL ES 2.0.24] section 4.4 page 107:
3546 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3547 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3548
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003549 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003550 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003551 bindReadFramebuffer(0);
3552 }
3553
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003554 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003555 {
3556 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003557 }
3558}
3559
3560void Context::detachRenderbuffer(GLuint renderbuffer)
3561{
3562 // [OpenGL ES 2.0.24] section 4.4 page 109:
3563 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3564 // had been executed with the target RENDERBUFFER and name of zero.
3565
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003566 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003567 {
3568 bindRenderbuffer(0);
3569 }
3570
3571 // [OpenGL ES 2.0.24] section 4.4 page 111:
3572 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3573 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3574 // point to which this image was attached in the currently bound framebuffer.
3575
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003576 Framebuffer *readFramebuffer = getReadFramebuffer();
3577 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003578
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003579 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003580 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003581 readFramebuffer->detachRenderbuffer(renderbuffer);
3582 }
3583
3584 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3585 {
3586 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003587 }
3588}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003589
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003590Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003591{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003592 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003593
3594 if (t == NULL)
3595 {
3596 static const GLubyte color[] = { 0, 0, 0, 255 };
3597
3598 switch (type)
3599 {
3600 default:
3601 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003602 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003603
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003604 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003605 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003606 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003607 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003608 t = incomplete2d;
3609 }
3610 break;
3611
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003612 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003613 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003614 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003615
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003616 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3617 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3618 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3619 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3620 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3621 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003622
3623 t = incompleteCube;
3624 }
3625 break;
3626 }
3627
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003628 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003629 }
3630
3631 return t;
3632}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003633
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003634bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003635{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003636 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003637}
3638
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003639bool Context::isTriangleMode(GLenum drawMode)
3640{
3641 switch (drawMode)
3642 {
3643 case GL_TRIANGLES:
3644 case GL_TRIANGLE_FAN:
3645 case GL_TRIANGLE_STRIP:
3646 return true;
3647 case GL_POINTS:
3648 case GL_LINES:
3649 case GL_LINE_LOOP:
3650 case GL_LINE_STRIP:
3651 return false;
3652 default: UNREACHABLE();
3653 }
3654
3655 return false;
3656}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003657
3658void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3659{
3660 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3661
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003662 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3663 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3664 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3665 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003666
daniel@transgaming.com83921382011-01-08 05:46:00 +00003667 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003668}
3669
daniel@transgaming.comd2820bf2012-01-27 15:38:48 +00003670void Context::setVertexAttribDivisor(GLuint index, GLuint divisor)
3671{
3672 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3673
3674 mState.vertexAttribute[index].mDivisor = divisor;
3675}
3676
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003677// keep list sorted in following order
3678// OES extensions
3679// EXT extensions
3680// Vendor extensions
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003681void Context::initExtensionString()
3682{
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003683 mExtensionString = "";
3684
3685 // OES extensions
3686 if (supports32bitIndices())
3687 {
3688 mExtensionString += "GL_OES_element_index_uint ";
3689 }
3690
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003691 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003692 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003693 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003694
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003695 if (supportsFloat16Textures())
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003696 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003697 mExtensionString += "GL_OES_texture_half_float ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003698 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003699 if (supportsFloat16LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003700 {
3701 mExtensionString += "GL_OES_texture_half_float_linear ";
3702 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003703 if (supportsFloat32Textures())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003704 {
3705 mExtensionString += "GL_OES_texture_float ";
3706 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003707 if (supportsFloat32LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003708 {
3709 mExtensionString += "GL_OES_texture_float_linear ";
3710 }
3711
3712 if (supportsNonPower2Texture())
3713 {
3714 mExtensionString += "GL_OES_texture_npot ";
3715 }
3716
3717 // Multi-vendor (EXT) extensions
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00003718 if (supportsOcclusionQueries())
3719 {
3720 mExtensionString += "GL_EXT_occlusion_query_boolean ";
3721 }
3722
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003723 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com8747f182011-11-09 17:50:38 +00003724 mExtensionString += "GL_EXT_robustness ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003725
gman@chromium.org50c526d2011-08-10 05:19:44 +00003726 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00003727 {
3728 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3729 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003730
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003731 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
daniel@transgaming.comdf363722011-12-16 23:29:53 +00003732 mExtensionString += "GL_EXT_texture_storage ";
gman@chromium.org50c526d2011-08-10 05:19:44 +00003733
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003734 // ANGLE-specific extensions
3735 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003736 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003737 {
3738 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3739 }
3740
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +00003741 if (supportsInstancing())
daniel@transgaming.comdce02fd2012-01-27 15:39:51 +00003742 {
3743 mExtensionString += "GL_ANGLE_instanced_arrays ";
3744 }
3745
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00003746 mExtensionString += "GL_ANGLE_pack_reverse_row_order ";
3747
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003748 if (supportsDXT3Textures())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003749 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003750 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3751 }
3752 if (supportsDXT5Textures())
3753 {
3754 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003755 }
daniel@transgaming.com97412f72011-11-11 04:19:07 +00003756
3757 mExtensionString += "GL_ANGLE_texture_usage ";
zmo@google.coma574f782011-10-03 21:45:23 +00003758 mExtensionString += "GL_ANGLE_translated_shader_source ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003759
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003760 // Other vendor-specific extensions
3761 if (supportsEventQueries())
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003762 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003763 mExtensionString += "GL_NV_fence ";
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003764 }
3765
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003766 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3767 if (end != std::string::npos)
3768 {
3769 mExtensionString.resize(end+1);
3770 }
3771}
3772
3773const char *Context::getExtensionString() const
3774{
3775 return mExtensionString.c_str();
3776}
3777
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003778void Context::initRendererString()
3779{
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003780 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003781
3782 mRendererString = "ANGLE (";
3783 mRendererString += identifier->Description;
3784 mRendererString += ")";
3785}
3786
3787const char *Context::getRendererString() const
3788{
3789 return mRendererString.c_str();
3790}
3791
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003792void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3793 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3794 GLbitfield mask)
3795{
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003796 Framebuffer *readFramebuffer = getReadFramebuffer();
3797 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3798
3799 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3800 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3801 {
3802 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3803 }
3804
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003805 if (drawFramebuffer->getSamples() != 0)
3806 {
3807 return error(GL_INVALID_OPERATION);
3808 }
3809
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003810 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3811 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3812 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3813 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3814
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003815 RECT sourceRect;
3816 RECT destRect;
3817
3818 if (srcX0 < srcX1)
3819 {
3820 sourceRect.left = srcX0;
3821 sourceRect.right = srcX1;
3822 destRect.left = dstX0;
3823 destRect.right = dstX1;
3824 }
3825 else
3826 {
3827 sourceRect.left = srcX1;
3828 destRect.left = dstX1;
3829 sourceRect.right = srcX0;
3830 destRect.right = dstX0;
3831 }
3832
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003833 if (srcY0 < srcY1)
3834 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003835 sourceRect.top = readBufferHeight - srcY1;
3836 destRect.top = drawBufferHeight - dstY1;
3837 sourceRect.bottom = readBufferHeight - srcY0;
3838 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003839 }
3840 else
3841 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003842 sourceRect.top = readBufferHeight - srcY0;
3843 destRect.top = drawBufferHeight - dstY0;
3844 sourceRect.bottom = readBufferHeight - srcY1;
3845 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003846 }
3847
3848 RECT sourceScissoredRect = sourceRect;
3849 RECT destScissoredRect = destRect;
3850
3851 if (mState.scissorTest)
3852 {
3853 // Only write to parts of the destination framebuffer which pass the scissor test
3854 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3855 // rect will be checked against scissorY, rather than the bottom.
3856 if (destRect.left < mState.scissorX)
3857 {
3858 int xDiff = mState.scissorX - destRect.left;
3859 destScissoredRect.left = mState.scissorX;
3860 sourceScissoredRect.left += xDiff;
3861 }
3862
3863 if (destRect.right > mState.scissorX + mState.scissorWidth)
3864 {
3865 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3866 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3867 sourceScissoredRect.right -= xDiff;
3868 }
3869
3870 if (destRect.top < mState.scissorY)
3871 {
3872 int yDiff = mState.scissorY - destRect.top;
3873 destScissoredRect.top = mState.scissorY;
3874 sourceScissoredRect.top += yDiff;
3875 }
3876
3877 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3878 {
3879 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3880 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3881 sourceScissoredRect.bottom -= yDiff;
3882 }
3883 }
3884
3885 bool blitRenderTarget = false;
3886 bool blitDepthStencil = false;
3887
3888 RECT sourceTrimmedRect = sourceScissoredRect;
3889 RECT destTrimmedRect = destScissoredRect;
3890
3891 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3892 // the actual draw and read surfaces.
3893 if (sourceTrimmedRect.left < 0)
3894 {
3895 int xDiff = 0 - sourceTrimmedRect.left;
3896 sourceTrimmedRect.left = 0;
3897 destTrimmedRect.left += xDiff;
3898 }
3899
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003900 if (sourceTrimmedRect.right > readBufferWidth)
3901 {
3902 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3903 sourceTrimmedRect.right = readBufferWidth;
3904 destTrimmedRect.right -= xDiff;
3905 }
3906
3907 if (sourceTrimmedRect.top < 0)
3908 {
3909 int yDiff = 0 - sourceTrimmedRect.top;
3910 sourceTrimmedRect.top = 0;
3911 destTrimmedRect.top += yDiff;
3912 }
3913
3914 if (sourceTrimmedRect.bottom > readBufferHeight)
3915 {
3916 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3917 sourceTrimmedRect.bottom = readBufferHeight;
3918 destTrimmedRect.bottom -= yDiff;
3919 }
3920
3921 if (destTrimmedRect.left < 0)
3922 {
3923 int xDiff = 0 - destTrimmedRect.left;
3924 destTrimmedRect.left = 0;
3925 sourceTrimmedRect.left += xDiff;
3926 }
3927
3928 if (destTrimmedRect.right > drawBufferWidth)
3929 {
3930 int xDiff = destTrimmedRect.right - drawBufferWidth;
3931 destTrimmedRect.right = drawBufferWidth;
3932 sourceTrimmedRect.right -= xDiff;
3933 }
3934
3935 if (destTrimmedRect.top < 0)
3936 {
3937 int yDiff = 0 - destTrimmedRect.top;
3938 destTrimmedRect.top = 0;
3939 sourceTrimmedRect.top += yDiff;
3940 }
3941
3942 if (destTrimmedRect.bottom > drawBufferHeight)
3943 {
3944 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3945 destTrimmedRect.bottom = drawBufferHeight;
3946 sourceTrimmedRect.bottom -= yDiff;
3947 }
3948
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003949 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003950 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3951 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3952 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3953 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003954 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3955 {
3956 partialBufferCopy = true;
3957 }
3958
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003959 if (mask & GL_COLOR_BUFFER_BIT)
3960 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003961 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3962 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3963 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3964 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3965 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003966 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3967 {
3968 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3969 return error(GL_INVALID_OPERATION);
3970 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003971
3972 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3973 {
3974 return error(GL_INVALID_OPERATION);
3975 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003976
3977 blitRenderTarget = true;
3978
3979 }
3980
3981 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3982 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00003983 Renderbuffer *readDSBuffer = NULL;
3984 Renderbuffer *drawDSBuffer = NULL;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003985
3986 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3987 // both a depth and stencil buffer, it will be the same buffer.
3988
3989 if (mask & GL_DEPTH_BUFFER_BIT)
3990 {
3991 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3992 {
3993 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3994 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3995 {
3996 return error(GL_INVALID_OPERATION);
3997 }
3998
3999 blitDepthStencil = true;
4000 readDSBuffer = readFramebuffer->getDepthbuffer();
4001 drawDSBuffer = drawFramebuffer->getDepthbuffer();
4002 }
4003 }
4004
4005 if (mask & GL_STENCIL_BUFFER_BIT)
4006 {
4007 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
4008 {
4009 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
4010 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
4011 {
4012 return error(GL_INVALID_OPERATION);
4013 }
4014
4015 blitDepthStencil = true;
4016 readDSBuffer = readFramebuffer->getStencilbuffer();
4017 drawDSBuffer = drawFramebuffer->getStencilbuffer();
4018 }
4019 }
4020
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004021 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004022 {
4023 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
4024 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
4025 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004026
daniel@transgaming.com97446d22010-08-24 19:20:54 +00004027 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
4028 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004029 {
4030 return error(GL_INVALID_OPERATION);
4031 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004032 }
4033
4034 if (blitRenderTarget || blitDepthStencil)
4035 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00004036 mDisplay->endScene();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004037
4038 if (blitRenderTarget)
4039 {
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00004040 IDirect3DSurface9* readRenderTarget = readFramebuffer->getRenderTarget();
4041 IDirect3DSurface9* drawRenderTarget = drawFramebuffer->getRenderTarget();
4042
4043 HRESULT result = mDevice->StretchRect(readRenderTarget, &sourceTrimmedRect,
4044 drawRenderTarget, &destTrimmedRect, D3DTEXF_NONE);
4045
4046 readRenderTarget->Release();
4047 drawRenderTarget->Release();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004048
4049 if (FAILED(result))
4050 {
4051 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
4052 return;
4053 }
4054 }
4055
4056 if (blitDepthStencil)
4057 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00004058 HRESULT result = mDevice->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004059
4060 if (FAILED(result))
4061 {
4062 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
4063 return;
4064 }
4065 }
4066 }
4067}
4068
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004069VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
4070{
4071 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4072 {
4073 mVertexDeclCache[i].vertexDeclaration = NULL;
4074 mVertexDeclCache[i].lruCount = 0;
4075 }
4076}
4077
4078VertexDeclarationCache::~VertexDeclarationCache()
4079{
4080 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4081 {
4082 if (mVertexDeclCache[i].vertexDeclaration)
4083 {
4084 mVertexDeclCache[i].vertexDeclaration->Release();
4085 }
4086 }
4087}
4088
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004089GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], Program *program, GLsizei instances, GLsizei *repeatDraw)
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004090{
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004091 *repeatDraw = 1;
4092
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004093 int indexedAttribute = MAX_VERTEX_ATTRIBS;
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004094 int instancedAttribute = MAX_VERTEX_ATTRIBS;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004095
4096 if (instances > 0)
4097 {
4098 // Find an indexed attribute to be mapped to D3D stream 0
4099 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4100 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004101 if (attributes[i].active)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004102 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004103 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
4104 {
4105 if (attributes[i].divisor == 0)
4106 {
4107 indexedAttribute = i;
4108 }
4109 }
4110 else if (instancedAttribute == MAX_VERTEX_ATTRIBS)
4111 {
4112 if (attributes[i].divisor != 0)
4113 {
4114 instancedAttribute = i;
4115 }
4116 }
4117 else break; // Found both an indexed and instanced attribute
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004118 }
4119 }
4120
4121 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
4122 {
4123 return GL_INVALID_OPERATION;
4124 }
4125 }
4126
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004127 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
4128 D3DVERTEXELEMENT9 *element = &elements[0];
4129
4130 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4131 {
4132 if (attributes[i].active)
4133 {
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004134 int stream = i;
4135
4136 if (instances > 0)
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004137 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004138 // Due to a bug on ATI cards we can't enable instancing when none of the attributes are instanced.
4139 if (instancedAttribute == MAX_VERTEX_ATTRIBS)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004140 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004141 *repeatDraw = instances;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004142 }
4143 else
4144 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004145 if (i == indexedAttribute)
4146 {
4147 stream = 0;
4148 }
4149 else if (i == 0)
4150 {
4151 stream = indexedAttribute;
4152 }
4153
4154 UINT frequency = 1;
4155
4156 if (attributes[i].divisor == 0)
4157 {
4158 frequency = D3DSTREAMSOURCE_INDEXEDDATA | instances;
4159 }
4160 else
4161 {
4162 frequency = D3DSTREAMSOURCE_INSTANCEDATA | attributes[i].divisor;
4163 }
4164
4165 device->SetStreamSourceFreq(stream, frequency);
4166 mInstancingEnabled = true;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004167 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004168 }
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004169
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004170 if (mAppliedVBs[stream].serial != attributes[i].serial ||
4171 mAppliedVBs[stream].stride != attributes[i].stride ||
4172 mAppliedVBs[stream].offset != attributes[i].offset)
4173 {
4174 device->SetStreamSource(stream, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
4175 mAppliedVBs[stream].serial = attributes[i].serial;
4176 mAppliedVBs[stream].stride = attributes[i].stride;
4177 mAppliedVBs[stream].offset = attributes[i].offset;
4178 }
4179
4180 element->Stream = stream;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004181 element->Offset = 0;
4182 element->Type = attributes[i].type;
4183 element->Method = D3DDECLMETHOD_DEFAULT;
4184 element->Usage = D3DDECLUSAGE_TEXCOORD;
4185 element->UsageIndex = program->getSemanticIndex(i);
4186 element++;
4187 }
4188 }
4189
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004190 if (instances == 0 || instancedAttribute == MAX_VERTEX_ATTRIBS)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004191 {
4192 if (mInstancingEnabled)
4193 {
4194 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4195 {
4196 device->SetStreamSourceFreq(i, 1);
4197 }
4198
4199 mInstancingEnabled = false;
4200 }
4201 }
4202
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004203 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
4204 *(element++) = end;
4205
4206 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4207 {
4208 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
4209 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
4210 {
4211 entry->lruCount = ++mMaxLru;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004212 if(entry->vertexDeclaration != mLastSetVDecl)
4213 {
4214 device->SetVertexDeclaration(entry->vertexDeclaration);
4215 mLastSetVDecl = entry->vertexDeclaration;
4216 }
4217
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004218 return GL_NO_ERROR;
4219 }
4220 }
4221
4222 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
4223
4224 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4225 {
4226 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
4227 {
4228 lastCache = &mVertexDeclCache[i];
4229 }
4230 }
4231
4232 if (lastCache->vertexDeclaration != NULL)
4233 {
4234 lastCache->vertexDeclaration->Release();
4235 lastCache->vertexDeclaration = NULL;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004236 // mLastSetVDecl is set to the replacement, so we don't have to worry
4237 // about it.
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004238 }
4239
4240 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
4241 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
4242 device->SetVertexDeclaration(lastCache->vertexDeclaration);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004243 mLastSetVDecl = lastCache->vertexDeclaration;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004244 lastCache->lruCount = ++mMaxLru;
4245
4246 return GL_NO_ERROR;
4247}
4248
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004249void VertexDeclarationCache::markStateDirty()
4250{
4251 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4252 {
4253 mAppliedVBs[i].serial = 0;
4254 }
4255
4256 mLastSetVDecl = NULL;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004257 mInstancingEnabled = true; // Forces it to be disabled when not used
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004258}
4259
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004260}
4261
4262extern "C"
4263{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00004264gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext, bool notifyResets, bool robustAccess)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004265{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00004266 return new gl::Context(config, shareContext, notifyResets, robustAccess);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004267}
4268
4269void glDestroyContext(gl::Context *context)
4270{
4271 delete context;
4272
4273 if (context == gl::getContext())
4274 {
4275 gl::makeCurrent(NULL, NULL, NULL);
4276 }
4277}
4278
4279void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
4280{
4281 gl::makeCurrent(context, display, surface);
4282}
4283
4284gl::Context *glGetCurrentContext()
4285{
4286 return gl::getContext();
4287}
4288}