blob: 3387c71f125b873602f173fb9c56b42983dd8a1c [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00002// Copyright (c) 2002-2011 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"
25#include "libGLESv2/RenderBuffer.h"
26#include "libGLESv2/Shader.h"
27#include "libGLESv2/Texture.h"
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +000028#include "libGLESv2/VertexDataManager.h"
29#include "libGLESv2/IndexDataManager.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030
daniel@transgaming.com86487c22010-03-11 19:41:43 +000031#undef near
32#undef far
33
jbauman@chromium.org399c35f2011-04-28 23:19:51 +000034namespace
35{
36 enum { CLOSING_INDEX_BUFFER_SIZE = 4096 };
37}
38
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000039namespace gl
40{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +000041Context::Context(const egl::Config *config, const gl::Context *shareContext, bool notifyResets, bool robustAccess) : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000042{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +000043 ASSERT(robustAccess == false); // Unimplemented
44
daniel@transgaming.comc941e252011-10-26 02:32:31 +000045 mDisplay = NULL;
46 mDevice = NULL;
47
benvanik@google.com1a233342011-04-28 19:44:39 +000048 mFenceHandleAllocator.setBaseHandle(0);
49
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000050 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000051
daniel@transgaming.com428d1582010-05-04 03:35:25 +000052 mState.depthClearValue = 1.0f;
53 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000054
daniel@transgaming.com428d1582010-05-04 03:35:25 +000055 mState.cullFace = false;
56 mState.cullMode = GL_BACK;
57 mState.frontFace = GL_CCW;
58 mState.depthTest = false;
59 mState.depthFunc = GL_LESS;
60 mState.blend = false;
61 mState.sourceBlendRGB = GL_ONE;
62 mState.sourceBlendAlpha = GL_ONE;
63 mState.destBlendRGB = GL_ZERO;
64 mState.destBlendAlpha = GL_ZERO;
65 mState.blendEquationRGB = GL_FUNC_ADD;
66 mState.blendEquationAlpha = GL_FUNC_ADD;
67 mState.blendColor.red = 0;
68 mState.blendColor.green = 0;
69 mState.blendColor.blue = 0;
70 mState.blendColor.alpha = 0;
71 mState.stencilTest = false;
72 mState.stencilFunc = GL_ALWAYS;
73 mState.stencilRef = 0;
74 mState.stencilMask = -1;
75 mState.stencilWritemask = -1;
76 mState.stencilBackFunc = GL_ALWAYS;
77 mState.stencilBackRef = 0;
78 mState.stencilBackMask = - 1;
79 mState.stencilBackWritemask = -1;
80 mState.stencilFail = GL_KEEP;
81 mState.stencilPassDepthFail = GL_KEEP;
82 mState.stencilPassDepthPass = GL_KEEP;
83 mState.stencilBackFail = GL_KEEP;
84 mState.stencilBackPassDepthFail = GL_KEEP;
85 mState.stencilBackPassDepthPass = GL_KEEP;
86 mState.polygonOffsetFill = false;
87 mState.polygonOffsetFactor = 0.0f;
88 mState.polygonOffsetUnits = 0.0f;
89 mState.sampleAlphaToCoverage = false;
90 mState.sampleCoverage = false;
91 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000092 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000093 mState.scissorTest = false;
94 mState.dither = true;
95 mState.generateMipmapHint = GL_DONT_CARE;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +000096 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000097
daniel@transgaming.com428d1582010-05-04 03:35:25 +000098 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000099
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000100 mState.viewportX = 0;
101 mState.viewportY = 0;
102 mState.viewportWidth = config->mDisplayMode.Width;
103 mState.viewportHeight = config->mDisplayMode.Height;
104 mState.zNear = 0.0f;
105 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000107 mState.scissorX = 0;
108 mState.scissorY = 0;
109 mState.scissorWidth = config->mDisplayMode.Width;
110 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000111
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000112 mState.colorMaskRed = true;
113 mState.colorMaskGreen = true;
114 mState.colorMaskBlue = true;
115 mState.colorMaskAlpha = true;
116 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000117
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000118 if (shareContext != NULL)
119 {
120 mResourceManager = shareContext->mResourceManager;
121 mResourceManager->addRef();
122 }
123 else
124 {
125 mResourceManager = new ResourceManager();
126 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000127
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000128 // [OpenGL ES 2.0.24] section 3.7 page 83:
129 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
130 // and cube map texture state vectors respectively associated with them.
131 // In order that access to these initial textures not be lost, they are treated as texture
132 // objects all of whose names are 0.
133
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000134 mTexture2DZero.set(new Texture2D(0));
135 mTextureCubeMapZero.set(new TextureCubeMap(0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000137 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000138 bindArrayBuffer(0);
139 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000140 bindTextureCubeMap(0);
141 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000142 bindReadFramebuffer(0);
143 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000144 bindRenderbuffer(0);
145
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000146 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000147
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000148 mState.packAlignment = 4;
149 mState.unpackAlignment = 4;
bsalomon@google.com56d46ab2011-11-23 14:53:10 +0000150 mState.packReverseRowOrder = false;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000151
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000152 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000153 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000154 mBlit = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000155 mClosingIB = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000156
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000157 mInvalidEnum = false;
158 mInvalidValue = false;
159 mInvalidOperation = false;
160 mOutOfMemory = false;
161 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000162
163 mHasBeenCurrent = false;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000164 mContextLost = false;
daniel@transgaming.com17f548c2011-11-09 17:47:02 +0000165 mResetStatus = GL_NO_ERROR;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +0000166 mResetStrategy = (notifyResets ? GL_LOSE_CONTEXT_ON_RESET_EXT : GL_NO_RESET_NOTIFICATION_EXT);
167 mRobustAccess = robustAccess;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000168
gman@chromium.org50c526d2011-08-10 05:19:44 +0000169 mSupportsDXT1Textures = false;
170 mSupportsDXT3Textures = false;
171 mSupportsDXT5Textures = false;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000172 mSupportsEventQueries = false;
gman@chromium.org50c526d2011-08-10 05:19:44 +0000173 mNumCompressedTextureFormats = 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000174 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000175 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000176 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000177}
178
179Context::~Context()
180{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000181 if (mState.currentProgram != 0)
182 {
183 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
184 if (programObject)
185 {
186 programObject->release();
187 }
188 mState.currentProgram = 0;
189 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000190
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000191 while (!mFramebufferMap.empty())
192 {
193 deleteFramebuffer(mFramebufferMap.begin()->first);
194 }
195
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000196 while (!mFenceMap.empty())
197 {
198 deleteFence(mFenceMap.begin()->first);
199 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000200
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000201 while (!mMultiSampleSupport.empty())
202 {
203 delete [] mMultiSampleSupport.begin()->second;
204 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
205 }
206
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000207 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000208 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +0000209 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000210 {
211 mState.samplerTexture[type][sampler].set(NULL);
212 }
213 }
214
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000215 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000216 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000217 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000218 }
219
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000220 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
221 {
222 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
223 }
224
225 mState.arrayBuffer.set(NULL);
226 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000227 mState.renderbuffer.set(NULL);
228
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000229 mTexture2DZero.set(NULL);
230 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000231
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000232 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000233 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000234 delete mBlit;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000235 delete mClosingIB;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000236
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000237 if (mMaskedClearSavedState)
238 {
239 mMaskedClearSavedState->Release();
240 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000241
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000242 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000243}
244
245void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
246{
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000247 mDisplay = display;
248 mDevice = mDisplay->getDevice();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000249
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000250 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000251 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000252 mDeviceCaps = mDisplay->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000253
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000254 mVertexDataManager = new VertexDataManager(this, mDevice);
255 mIndexDataManager = new IndexDataManager(this, mDevice);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000256 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000257
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000258 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000259 mSupportsVertexTexture = mDisplay->getVertexTextureSupport();
260 mSupportsNonPower2Texture = mDisplay->getNonPower2TextureSupport();
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000261
262 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
263 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
264 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
265 mMaxRenderbufferDimension = mMaxTextureDimension;
266 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
267 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
268 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
269
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000270 const D3DFORMAT renderBufferFormats[] =
271 {
272 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000273 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000274 D3DFMT_R5G6B5,
275 D3DFMT_D24S8
276 };
277
278 int max = 0;
279 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
280 {
281 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000282 mDisplay->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000283 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
284
285 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
286 {
287 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
288 {
289 max = j;
290 }
291 }
292 }
293
294 mMaxSupportedSamples = max;
295
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000296 mSupportsEventQueries = mDisplay->getEventQuerySupport();
297 mSupportsDXT1Textures = mDisplay->getDXT1TextureSupport();
298 mSupportsDXT3Textures = mDisplay->getDXT3TextureSupport();
299 mSupportsDXT5Textures = mDisplay->getDXT5TextureSupport();
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +0000300 mSupportsFloat32Textures = mDisplay->getFloat32TextureSupport(&mSupportsFloat32LinearFilter, &mSupportsFloat32RenderableTextures);
301 mSupportsFloat16Textures = mDisplay->getFloat16TextureSupport(&mSupportsFloat16LinearFilter, &mSupportsFloat16RenderableTextures);
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000302 mSupportsLuminanceTextures = mDisplay->getLuminanceTextureSupport();
303 mSupportsLuminanceAlphaTextures = mDisplay->getLuminanceAlphaTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000304
daniel@transgaming.com83921382011-01-08 05:46:00 +0000305 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
306
gman@chromium.org50c526d2011-08-10 05:19:44 +0000307 mNumCompressedTextureFormats = 0;
308 if (supportsDXT1Textures())
309 {
310 mNumCompressedTextureFormats += 2;
311 }
312 if (supportsDXT3Textures())
313 {
314 mNumCompressedTextureFormats += 1;
315 }
316 if (supportsDXT5Textures())
317 {
318 mNumCompressedTextureFormats += 1;
319 }
320
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000321 initExtensionString();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +0000322 initRendererString();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000323
324 mState.viewportX = 0;
325 mState.viewportY = 0;
326 mState.viewportWidth = surface->getWidth();
327 mState.viewportHeight = surface->getHeight();
328
329 mState.scissorX = 0;
330 mState.scissorY = 0;
331 mState.scissorWidth = surface->getWidth();
332 mState.scissorHeight = surface->getHeight();
333
334 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000335 }
336
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000337 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
338 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000339 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000340
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000341 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000342 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000343 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000344
345 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000346
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000347 if (defaultRenderTarget)
348 {
349 defaultRenderTarget->Release();
350 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000351
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000352 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000353 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000354 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000355 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000356
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000357 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000358}
359
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000360// 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 +0000361void Context::markAllStateDirty()
362{
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000363 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
364 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +0000365 mAppliedTextureSerialPS[t] = 0;
366 }
367
368 for (int t = 0; t < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; t++)
369 {
370 mAppliedTextureSerialVS[t] = 0;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000371 }
372
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000373 mAppliedProgramSerial = 0;
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000374 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000375 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000376 mAppliedStencilbufferSerial = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000377 mAppliedIBSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000378 mDepthStencilInitialized = false;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000379 mViewportInitialized = false;
380 mRenderTargetDescInitialized = false;
381
382 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000383
384 mClearStateDirty = true;
385 mCullStateDirty = true;
386 mDepthStateDirty = true;
387 mMaskStateDirty = true;
388 mBlendStateDirty = true;
389 mStencilStateDirty = true;
390 mPolygonOffsetStateDirty = true;
391 mScissorStateDirty = true;
392 mSampleStateDirty = true;
393 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000394 mFrontFaceDirty = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +0000395 mDxUniformsDirty = true;
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000396 mCachedCurrentProgram = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000397}
398
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000399void Context::markContextLost()
400{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +0000401 if (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT)
402 mResetStatus = GL_UNKNOWN_CONTEXT_RESET_EXT;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000403 mContextLost = true;
404}
405
406bool Context::isContextLost()
407{
408 return mContextLost;
409}
410
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000411void Context::setClearColor(float red, float green, float blue, float alpha)
412{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000413 mState.colorClearValue.red = red;
414 mState.colorClearValue.green = green;
415 mState.colorClearValue.blue = blue;
416 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000417}
418
419void Context::setClearDepth(float depth)
420{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000421 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000422}
423
424void Context::setClearStencil(int stencil)
425{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000426 mState.stencilClearValue = stencil;
427}
428
429void Context::setCullFace(bool enabled)
430{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000431 if (mState.cullFace != enabled)
432 {
433 mState.cullFace = enabled;
434 mCullStateDirty = true;
435 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000436}
437
438bool Context::isCullFaceEnabled() const
439{
440 return mState.cullFace;
441}
442
443void Context::setCullMode(GLenum mode)
444{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000445 if (mState.cullMode != mode)
446 {
447 mState.cullMode = mode;
448 mCullStateDirty = true;
449 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000450}
451
452void Context::setFrontFace(GLenum front)
453{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000454 if (mState.frontFace != front)
455 {
456 mState.frontFace = front;
457 mFrontFaceDirty = true;
458 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000459}
460
461void Context::setDepthTest(bool enabled)
462{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000463 if (mState.depthTest != enabled)
464 {
465 mState.depthTest = enabled;
466 mDepthStateDirty = true;
467 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000468}
469
470bool Context::isDepthTestEnabled() const
471{
472 return mState.depthTest;
473}
474
475void Context::setDepthFunc(GLenum depthFunc)
476{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000477 if (mState.depthFunc != depthFunc)
478 {
479 mState.depthFunc = depthFunc;
480 mDepthStateDirty = true;
481 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000482}
483
484void Context::setDepthRange(float zNear, float zFar)
485{
486 mState.zNear = zNear;
487 mState.zFar = zFar;
488}
489
490void Context::setBlend(bool enabled)
491{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000492 if (mState.blend != enabled)
493 {
494 mState.blend = enabled;
495 mBlendStateDirty = true;
496 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000497}
498
499bool Context::isBlendEnabled() const
500{
501 return mState.blend;
502}
503
504void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
505{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000506 if (mState.sourceBlendRGB != sourceRGB ||
507 mState.sourceBlendAlpha != sourceAlpha ||
508 mState.destBlendRGB != destRGB ||
509 mState.destBlendAlpha != destAlpha)
510 {
511 mState.sourceBlendRGB = sourceRGB;
512 mState.destBlendRGB = destRGB;
513 mState.sourceBlendAlpha = sourceAlpha;
514 mState.destBlendAlpha = destAlpha;
515 mBlendStateDirty = true;
516 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000517}
518
519void Context::setBlendColor(float red, float green, float blue, float alpha)
520{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000521 if (mState.blendColor.red != red ||
522 mState.blendColor.green != green ||
523 mState.blendColor.blue != blue ||
524 mState.blendColor.alpha != alpha)
525 {
526 mState.blendColor.red = red;
527 mState.blendColor.green = green;
528 mState.blendColor.blue = blue;
529 mState.blendColor.alpha = alpha;
530 mBlendStateDirty = true;
531 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000532}
533
534void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
535{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000536 if (mState.blendEquationRGB != rgbEquation ||
537 mState.blendEquationAlpha != alphaEquation)
538 {
539 mState.blendEquationRGB = rgbEquation;
540 mState.blendEquationAlpha = alphaEquation;
541 mBlendStateDirty = true;
542 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000543}
544
545void Context::setStencilTest(bool enabled)
546{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000547 if (mState.stencilTest != enabled)
548 {
549 mState.stencilTest = enabled;
550 mStencilStateDirty = true;
551 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000552}
553
554bool Context::isStencilTestEnabled() const
555{
556 return mState.stencilTest;
557}
558
559void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
560{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000561 if (mState.stencilFunc != stencilFunc ||
562 mState.stencilRef != stencilRef ||
563 mState.stencilMask != stencilMask)
564 {
565 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000566 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000567 mState.stencilMask = stencilMask;
568 mStencilStateDirty = true;
569 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000570}
571
572void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
573{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000574 if (mState.stencilBackFunc != stencilBackFunc ||
575 mState.stencilBackRef != stencilBackRef ||
576 mState.stencilBackMask != stencilBackMask)
577 {
578 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000579 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000580 mState.stencilBackMask = stencilBackMask;
581 mStencilStateDirty = true;
582 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000583}
584
585void Context::setStencilWritemask(GLuint stencilWritemask)
586{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000587 if (mState.stencilWritemask != stencilWritemask)
588 {
589 mState.stencilWritemask = stencilWritemask;
590 mStencilStateDirty = true;
591 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000592}
593
594void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
595{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000596 if (mState.stencilBackWritemask != stencilBackWritemask)
597 {
598 mState.stencilBackWritemask = stencilBackWritemask;
599 mStencilStateDirty = true;
600 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000601}
602
603void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
604{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000605 if (mState.stencilFail != stencilFail ||
606 mState.stencilPassDepthFail != stencilPassDepthFail ||
607 mState.stencilPassDepthPass != stencilPassDepthPass)
608 {
609 mState.stencilFail = stencilFail;
610 mState.stencilPassDepthFail = stencilPassDepthFail;
611 mState.stencilPassDepthPass = stencilPassDepthPass;
612 mStencilStateDirty = true;
613 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000614}
615
616void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
617{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000618 if (mState.stencilBackFail != stencilBackFail ||
619 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
620 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
621 {
622 mState.stencilBackFail = stencilBackFail;
623 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
624 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
625 mStencilStateDirty = true;
626 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000627}
628
629void Context::setPolygonOffsetFill(bool enabled)
630{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000631 if (mState.polygonOffsetFill != enabled)
632 {
633 mState.polygonOffsetFill = enabled;
634 mPolygonOffsetStateDirty = true;
635 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000636}
637
638bool Context::isPolygonOffsetFillEnabled() const
639{
640 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000641
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000642}
643
644void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
645{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000646 if (mState.polygonOffsetFactor != factor ||
647 mState.polygonOffsetUnits != units)
648 {
649 mState.polygonOffsetFactor = factor;
650 mState.polygonOffsetUnits = units;
651 mPolygonOffsetStateDirty = true;
652 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000653}
654
655void Context::setSampleAlphaToCoverage(bool enabled)
656{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000657 if (mState.sampleAlphaToCoverage != enabled)
658 {
659 mState.sampleAlphaToCoverage = enabled;
660 mSampleStateDirty = true;
661 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000662}
663
664bool Context::isSampleAlphaToCoverageEnabled() const
665{
666 return mState.sampleAlphaToCoverage;
667}
668
669void Context::setSampleCoverage(bool enabled)
670{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000671 if (mState.sampleCoverage != enabled)
672 {
673 mState.sampleCoverage = enabled;
674 mSampleStateDirty = true;
675 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000676}
677
678bool Context::isSampleCoverageEnabled() const
679{
680 return mState.sampleCoverage;
681}
682
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000683void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000684{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000685 if (mState.sampleCoverageValue != value ||
686 mState.sampleCoverageInvert != invert)
687 {
688 mState.sampleCoverageValue = value;
689 mState.sampleCoverageInvert = invert;
690 mSampleStateDirty = true;
691 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000692}
693
694void Context::setScissorTest(bool enabled)
695{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000696 if (mState.scissorTest != enabled)
697 {
698 mState.scissorTest = enabled;
699 mScissorStateDirty = true;
700 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000701}
702
703bool Context::isScissorTestEnabled() const
704{
705 return mState.scissorTest;
706}
707
708void Context::setDither(bool enabled)
709{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000710 if (mState.dither != enabled)
711 {
712 mState.dither = enabled;
713 mDitherStateDirty = true;
714 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000715}
716
717bool Context::isDitherEnabled() const
718{
719 return mState.dither;
720}
721
722void Context::setLineWidth(GLfloat width)
723{
724 mState.lineWidth = width;
725}
726
727void Context::setGenerateMipmapHint(GLenum hint)
728{
729 mState.generateMipmapHint = hint;
730}
731
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000732void Context::setFragmentShaderDerivativeHint(GLenum hint)
733{
734 mState.fragmentShaderDerivativeHint = hint;
735 // TODO: Propagate the hint to shader translator so we can write
736 // ddx, ddx_coarse, or ddx_fine depending on the hint.
737 // Ignore for now. It is valid for implementations to ignore hint.
738}
739
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000740void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
741{
742 mState.viewportX = x;
743 mState.viewportY = y;
744 mState.viewportWidth = width;
745 mState.viewportHeight = height;
746}
747
748void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
749{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000750 if (mState.scissorX != x || mState.scissorY != y ||
751 mState.scissorWidth != width || mState.scissorHeight != height)
752 {
753 mState.scissorX = x;
754 mState.scissorY = y;
755 mState.scissorWidth = width;
756 mState.scissorHeight = height;
757 mScissorStateDirty = true;
758 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000759}
760
761void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
762{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000763 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
764 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
765 {
766 mState.colorMaskRed = red;
767 mState.colorMaskGreen = green;
768 mState.colorMaskBlue = blue;
769 mState.colorMaskAlpha = alpha;
770 mMaskStateDirty = true;
771 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000772}
773
774void Context::setDepthMask(bool mask)
775{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000776 if (mState.depthMask != mask)
777 {
778 mState.depthMask = mask;
779 mMaskStateDirty = true;
780 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000781}
782
daniel@transgaming.comdfd57022011-05-11 15:37:25 +0000783void Context::setActiveSampler(unsigned int active)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000784{
785 mState.activeSampler = active;
786}
787
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000788GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000789{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000790 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000791}
792
793GLuint Context::getDrawFramebufferHandle() const
794{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000795 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000796}
797
798GLuint Context::getRenderbufferHandle() const
799{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000800 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000801}
802
803GLuint Context::getArrayBufferHandle() const
804{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000805 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000806}
807
daniel@transgaming.com83921382011-01-08 05:46:00 +0000808void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000809{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000810 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000811}
812
daniel@transgaming.com83921382011-01-08 05:46:00 +0000813const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000814{
815 return mState.vertexAttribute[attribNum];
816}
817
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000818void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000819 GLsizei stride, const void *pointer)
820{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000821 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000822 mState.vertexAttribute[attribNum].mSize = size;
823 mState.vertexAttribute[attribNum].mType = type;
824 mState.vertexAttribute[attribNum].mNormalized = normalized;
825 mState.vertexAttribute[attribNum].mStride = stride;
826 mState.vertexAttribute[attribNum].mPointer = pointer;
827}
828
829const void *Context::getVertexAttribPointer(unsigned int attribNum) const
830{
831 return mState.vertexAttribute[attribNum].mPointer;
832}
833
daniel@transgaming.com83921382011-01-08 05:46:00 +0000834const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000835{
836 return mState.vertexAttribute;
837}
838
839void Context::setPackAlignment(GLint alignment)
840{
841 mState.packAlignment = alignment;
842}
843
844GLint Context::getPackAlignment() const
845{
846 return mState.packAlignment;
847}
848
849void Context::setUnpackAlignment(GLint alignment)
850{
851 mState.unpackAlignment = alignment;
852}
853
854GLint Context::getUnpackAlignment() const
855{
856 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857}
858
bsalomon@google.com56d46ab2011-11-23 14:53:10 +0000859void Context::setPackReverseRowOrder(bool reverseRowOrder)
860{
861 mState.packReverseRowOrder = reverseRowOrder;
862}
863
864bool Context::getPackReverseRowOrder() const
865{
866 return mState.packReverseRowOrder;
867}
868
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000869GLuint Context::createBuffer()
870{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000871 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000872}
873
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000874GLuint Context::createProgram()
875{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000876 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877}
878
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000879GLuint Context::createShader(GLenum type)
880{
881 return mResourceManager->createShader(type);
882}
883
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000884GLuint Context::createTexture()
885{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000886 return mResourceManager->createTexture();
887}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000888
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000889GLuint Context::createRenderbuffer()
890{
891 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892}
893
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000894// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000895GLuint Context::createFramebuffer()
896{
benvanik@google.com1a233342011-04-28 19:44:39 +0000897 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000898
899 mFramebufferMap[handle] = NULL;
900
901 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000902}
903
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000904GLuint Context::createFence()
905{
benvanik@google.com1a233342011-04-28 19:44:39 +0000906 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000907
908 mFenceMap[handle] = new Fence;
909
910 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000911}
912
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000913void Context::deleteBuffer(GLuint buffer)
914{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000915 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000916 {
917 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000918 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000919
920 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000921}
922
923void Context::deleteShader(GLuint shader)
924{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000925 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000926}
927
928void Context::deleteProgram(GLuint program)
929{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000930 mResourceManager->deleteProgram(program);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000931 mCachedCurrentProgram = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932}
933
934void Context::deleteTexture(GLuint texture)
935{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000936 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000937 {
938 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000939 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000940
941 mResourceManager->deleteTexture(texture);
942}
943
944void Context::deleteRenderbuffer(GLuint renderbuffer)
945{
946 if (mResourceManager->getRenderbuffer(renderbuffer))
947 {
948 detachRenderbuffer(renderbuffer);
949 }
950
951 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000952}
953
954void Context::deleteFramebuffer(GLuint framebuffer)
955{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000956 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
957
958 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000959 {
960 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000961
benvanik@google.com1a233342011-04-28 19:44:39 +0000962 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000963 delete framebufferObject->second;
964 mFramebufferMap.erase(framebufferObject);
965 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000967
968void Context::deleteFence(GLuint fence)
969{
970 FenceMap::iterator fenceObject = mFenceMap.find(fence);
971
972 if (fenceObject != mFenceMap.end())
973 {
benvanik@google.com1a233342011-04-28 19:44:39 +0000974 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000975 delete fenceObject->second;
976 mFenceMap.erase(fenceObject);
977 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000978}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000979
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000980Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000981{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000982 return mResourceManager->getBuffer(handle);
983}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000984
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000985Shader *Context::getShader(GLuint handle)
986{
987 return mResourceManager->getShader(handle);
988}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000989
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000990Program *Context::getProgram(GLuint handle)
991{
992 return mResourceManager->getProgram(handle);
993}
994
995Texture *Context::getTexture(GLuint handle)
996{
997 return mResourceManager->getTexture(handle);
998}
999
1000Renderbuffer *Context::getRenderbuffer(GLuint handle)
1001{
1002 return mResourceManager->getRenderbuffer(handle);
1003}
1004
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001005Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001006{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001007 return getFramebuffer(mState.readFramebuffer);
1008}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001009
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001010Framebuffer *Context::getDrawFramebuffer()
1011{
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001012 return mBoundDrawFramebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001013}
1014
1015void Context::bindArrayBuffer(unsigned int buffer)
1016{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001017 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001018
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001019 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001020}
1021
1022void Context::bindElementArrayBuffer(unsigned int buffer)
1023{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001024 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001025
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001026 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001027}
1028
1029void Context::bindTexture2D(GLuint texture)
1030{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001031 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001032
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001033 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001034}
1035
1036void Context::bindTextureCubeMap(GLuint texture)
1037{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001038 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001039
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001040 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001041}
1042
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001043void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001044{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001045 if (!getFramebuffer(framebuffer))
1046 {
1047 mFramebufferMap[framebuffer] = new Framebuffer();
1048 }
1049
1050 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001051}
1052
1053void Context::bindDrawFramebuffer(GLuint framebuffer)
1054{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001055 if (!getFramebuffer(framebuffer))
1056 {
1057 mFramebufferMap[framebuffer] = new Framebuffer();
1058 }
1059
1060 mState.drawFramebuffer = framebuffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001061
1062 mBoundDrawFramebuffer = getFramebuffer(framebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001063}
1064
1065void Context::bindRenderbuffer(GLuint renderbuffer)
1066{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001067 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1068
1069 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070}
1071
1072void Context::useProgram(GLuint program)
1073{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001074 GLuint priorProgram = mState.currentProgram;
1075 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001076
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001077 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001078 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001079 Program *newProgram = mResourceManager->getProgram(program);
1080 Program *oldProgram = mResourceManager->getProgram(priorProgram);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001081 mCachedCurrentProgram = NULL;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001082 mDxUniformsDirty = true;
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001083
1084 if (newProgram)
1085 {
1086 newProgram->addRef();
1087 }
1088
1089 if (oldProgram)
1090 {
1091 oldProgram->release();
1092 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001093 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001094}
1095
1096void Context::setFramebufferZero(Framebuffer *buffer)
1097{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001098 delete mFramebufferMap[0];
1099 mFramebufferMap[0] = buffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001100 if (mState.drawFramebuffer == 0)
1101 {
1102 mBoundDrawFramebuffer = buffer;
1103 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001104}
1105
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001106void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001107{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001108 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1109 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001110}
1111
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001112Framebuffer *Context::getFramebuffer(unsigned int handle)
1113{
1114 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1115
1116 if (framebuffer == mFramebufferMap.end())
1117 {
1118 return NULL;
1119 }
1120 else
1121 {
1122 return framebuffer->second;
1123 }
1124}
1125
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001126Fence *Context::getFence(unsigned int handle)
1127{
1128 FenceMap::iterator fence = mFenceMap.find(handle);
1129
1130 if (fence == mFenceMap.end())
1131 {
1132 return NULL;
1133 }
1134 else
1135 {
1136 return fence->second;
1137 }
1138}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001139
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001140Buffer *Context::getArrayBuffer()
1141{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001142 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001143}
1144
1145Buffer *Context::getElementArrayBuffer()
1146{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001147 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001148}
1149
1150Program *Context::getCurrentProgram()
1151{
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001152 if (!mCachedCurrentProgram)
1153 {
1154 mCachedCurrentProgram = mResourceManager->getProgram(mState.currentProgram);
1155 }
1156 return mCachedCurrentProgram;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001157}
1158
1159Texture2D *Context::getTexture2D()
1160{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001161 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001162}
1163
1164TextureCubeMap *Context::getTextureCubeMap()
1165{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001166 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001167}
1168
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001169Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001170{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001171 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001172
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001173 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001174 {
1175 switch (type)
1176 {
1177 default: UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001178 case TEXTURE_2D: return mTexture2DZero.get();
1179 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001180 }
1181 }
1182
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001183 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001184}
1185
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001186bool Context::getBooleanv(GLenum pname, GLboolean *params)
1187{
1188 switch (pname)
1189 {
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001190 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1191 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1192 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001193 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001194 params[0] = mState.colorMaskRed;
1195 params[1] = mState.colorMaskGreen;
1196 params[2] = mState.colorMaskBlue;
1197 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001198 break;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001199 case GL_CULL_FACE: *params = mState.cullFace; break;
1200 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1201 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1202 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1203 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1204 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1205 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1206 case GL_BLEND: *params = mState.blend; break;
1207 case GL_DITHER: *params = mState.dither; break;
1208 case GL_CONTEXT_ROBUST_ACCESS_EXT: *params = mRobustAccess ? GL_TRUE : GL_FALSE; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001209 default:
1210 return false;
1211 }
1212
1213 return true;
1214}
1215
1216bool Context::getFloatv(GLenum pname, GLfloat *params)
1217{
1218 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1219 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1220 // GetIntegerv as its native query function. As it would require conversion in any
1221 // case, this should make no difference to the calling application.
1222 switch (pname)
1223 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001224 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1225 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1226 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1227 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1228 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001229 case GL_ALIASED_LINE_WIDTH_RANGE:
1230 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1231 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1232 break;
1233 case GL_ALIASED_POINT_SIZE_RANGE:
1234 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001235 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 +00001236 break;
1237 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001238 params[0] = mState.zNear;
1239 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001240 break;
1241 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001242 params[0] = mState.colorClearValue.red;
1243 params[1] = mState.colorClearValue.green;
1244 params[2] = mState.colorClearValue.blue;
1245 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001246 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001247 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001248 params[0] = mState.blendColor.red;
1249 params[1] = mState.blendColor.green;
1250 params[2] = mState.blendColor.blue;
1251 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001252 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001253 default:
1254 return false;
1255 }
1256
1257 return true;
1258}
1259
1260bool Context::getIntegerv(GLenum pname, GLint *params)
1261{
1262 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1263 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1264 // GetIntegerv as its native query function. As it would require conversion in any
1265 // case, this should make no difference to the calling application. You may find it in
1266 // Context::getFloatv.
1267 switch (pname)
1268 {
1269 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1270 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001271 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00001272 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1273 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001274 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001275 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001276 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001277 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001278 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001279 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1280 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001281 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1282 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1283 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001284 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001285 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1286 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00001287 case GL_PACK_REVERSE_ROW_ORDER_ANGLE: *params = mState.packReverseRowOrder; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001288 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1289 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001290 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001291 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1292 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1293 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1294 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1295 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1296 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1297 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1298 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1299 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1300 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1301 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1302 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1303 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1304 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1305 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1306 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1307 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1308 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1309 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1310 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1311 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1312 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1313 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1314 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001315 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1316 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001317 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
gman@chromium.org50c526d2011-08-10 05:19:44 +00001318 params[0] = mNumCompressedTextureFormats;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001319 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001320 case GL_MAX_SAMPLES_ANGLE:
1321 {
1322 GLsizei maxSamples = getMaxSupportedSamples();
1323 if (maxSamples != 0)
1324 {
1325 *params = maxSamples;
1326 }
1327 else
1328 {
1329 return false;
1330 }
1331
1332 break;
1333 }
1334 case GL_SAMPLE_BUFFERS:
1335 case GL_SAMPLES:
1336 {
1337 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1338 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1339 {
1340 switch (pname)
1341 {
1342 case GL_SAMPLE_BUFFERS:
1343 if (framebuffer->getSamples() != 0)
1344 {
1345 *params = 1;
1346 }
1347 else
1348 {
1349 *params = 0;
1350 }
1351 break;
1352 case GL_SAMPLES:
1353 *params = framebuffer->getSamples();
1354 break;
1355 }
1356 }
1357 else
1358 {
1359 *params = 0;
1360 }
1361 }
1362 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001363 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1364 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1365 case GL_MAX_VIEWPORT_DIMS:
1366 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001367 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001368 params[0] = maxDimension;
1369 params[1] = maxDimension;
1370 }
1371 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001372 case GL_COMPRESSED_TEXTURE_FORMATS:
1373 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001374 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00001375 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001376 *params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1377 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1378 }
1379 if (supportsDXT3Textures())
1380 {
1381 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
1382 }
1383 if (supportsDXT5Textures())
1384 {
1385 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001386 }
1387 }
1388 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001389 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001390 params[0] = mState.viewportX;
1391 params[1] = mState.viewportY;
1392 params[2] = mState.viewportWidth;
1393 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001394 break;
1395 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001396 params[0] = mState.scissorX;
1397 params[1] = mState.scissorY;
1398 params[2] = mState.scissorWidth;
1399 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001400 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001401 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1402 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001403 case GL_RED_BITS:
1404 case GL_GREEN_BITS:
1405 case GL_BLUE_BITS:
1406 case GL_ALPHA_BITS:
1407 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001408 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001409 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001410
1411 if (colorbuffer)
1412 {
1413 switch (pname)
1414 {
1415 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1416 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1417 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1418 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1419 }
1420 }
1421 else
1422 {
1423 *params = 0;
1424 }
1425 }
1426 break;
1427 case GL_DEPTH_BITS:
1428 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001429 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001430 gl::Renderbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001431
1432 if (depthbuffer)
1433 {
1434 *params = depthbuffer->getDepthSize();
1435 }
1436 else
1437 {
1438 *params = 0;
1439 }
1440 }
1441 break;
1442 case GL_STENCIL_BITS:
1443 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001444 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001445 gl::Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001446
1447 if (stencilbuffer)
1448 {
1449 *params = stencilbuffer->getStencilSize();
1450 }
1451 else
1452 {
1453 *params = 0;
1454 }
1455 }
1456 break;
1457 case GL_TEXTURE_BINDING_2D:
1458 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001459 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001460 {
1461 error(GL_INVALID_OPERATION);
1462 return false;
1463 }
1464
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001465 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001466 }
1467 break;
1468 case GL_TEXTURE_BINDING_CUBE_MAP:
1469 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001470 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001471 {
1472 error(GL_INVALID_OPERATION);
1473 return false;
1474 }
1475
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001476 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001477 }
1478 break;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001479 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
1480 *params = mResetStrategy;
1481 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001482 default:
1483 return false;
1484 }
1485
1486 return true;
1487}
1488
1489bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1490{
1491 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1492 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1493 // to the fact that it is stored internally as a float, and so would require conversion
1494 // if returned from Context::getIntegerv. Since this conversion is already implemented
1495 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1496 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1497 // application.
1498 switch (pname)
1499 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001500 case GL_COMPRESSED_TEXTURE_FORMATS:
1501 {
1502 *type = GL_INT;
1503 *numParams = mNumCompressedTextureFormats;
1504 }
1505 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001506 case GL_SHADER_BINARY_FORMATS:
1507 {
1508 *type = GL_INT;
1509 *numParams = 0;
1510 }
1511 break;
1512 case GL_MAX_VERTEX_ATTRIBS:
1513 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1514 case GL_MAX_VARYING_VECTORS:
1515 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1516 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1517 case GL_MAX_TEXTURE_IMAGE_UNITS:
1518 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1519 case GL_MAX_RENDERBUFFER_SIZE:
1520 case GL_NUM_SHADER_BINARY_FORMATS:
1521 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1522 case GL_ARRAY_BUFFER_BINDING:
1523 case GL_FRAMEBUFFER_BINDING:
1524 case GL_RENDERBUFFER_BINDING:
1525 case GL_CURRENT_PROGRAM:
1526 case GL_PACK_ALIGNMENT:
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00001527 case GL_PACK_REVERSE_ROW_ORDER_ANGLE:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001528 case GL_UNPACK_ALIGNMENT:
1529 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001530 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001531 case GL_RED_BITS:
1532 case GL_GREEN_BITS:
1533 case GL_BLUE_BITS:
1534 case GL_ALPHA_BITS:
1535 case GL_DEPTH_BITS:
1536 case GL_STENCIL_BITS:
1537 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1538 case GL_CULL_FACE_MODE:
1539 case GL_FRONT_FACE:
1540 case GL_ACTIVE_TEXTURE:
1541 case GL_STENCIL_FUNC:
1542 case GL_STENCIL_VALUE_MASK:
1543 case GL_STENCIL_REF:
1544 case GL_STENCIL_FAIL:
1545 case GL_STENCIL_PASS_DEPTH_FAIL:
1546 case GL_STENCIL_PASS_DEPTH_PASS:
1547 case GL_STENCIL_BACK_FUNC:
1548 case GL_STENCIL_BACK_VALUE_MASK:
1549 case GL_STENCIL_BACK_REF:
1550 case GL_STENCIL_BACK_FAIL:
1551 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1552 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1553 case GL_DEPTH_FUNC:
1554 case GL_BLEND_SRC_RGB:
1555 case GL_BLEND_SRC_ALPHA:
1556 case GL_BLEND_DST_RGB:
1557 case GL_BLEND_DST_ALPHA:
1558 case GL_BLEND_EQUATION_RGB:
1559 case GL_BLEND_EQUATION_ALPHA:
1560 case GL_STENCIL_WRITEMASK:
1561 case GL_STENCIL_BACK_WRITEMASK:
1562 case GL_STENCIL_CLEAR_VALUE:
1563 case GL_SUBPIXEL_BITS:
1564 case GL_MAX_TEXTURE_SIZE:
1565 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1566 case GL_SAMPLE_BUFFERS:
1567 case GL_SAMPLES:
1568 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1569 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1570 case GL_TEXTURE_BINDING_2D:
1571 case GL_TEXTURE_BINDING_CUBE_MAP:
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001572 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001573 {
1574 *type = GL_INT;
1575 *numParams = 1;
1576 }
1577 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001578 case GL_MAX_SAMPLES_ANGLE:
1579 {
1580 if (getMaxSupportedSamples() != 0)
1581 {
1582 *type = GL_INT;
1583 *numParams = 1;
1584 }
1585 else
1586 {
1587 return false;
1588 }
1589 }
1590 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001591 case GL_MAX_VIEWPORT_DIMS:
1592 {
1593 *type = GL_INT;
1594 *numParams = 2;
1595 }
1596 break;
1597 case GL_VIEWPORT:
1598 case GL_SCISSOR_BOX:
1599 {
1600 *type = GL_INT;
1601 *numParams = 4;
1602 }
1603 break;
1604 case GL_SHADER_COMPILER:
1605 case GL_SAMPLE_COVERAGE_INVERT:
1606 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001607 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1608 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1609 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1610 case GL_SAMPLE_COVERAGE:
1611 case GL_SCISSOR_TEST:
1612 case GL_STENCIL_TEST:
1613 case GL_DEPTH_TEST:
1614 case GL_BLEND:
1615 case GL_DITHER:
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001616 case GL_CONTEXT_ROBUST_ACCESS_EXT:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001617 {
1618 *type = GL_BOOL;
1619 *numParams = 1;
1620 }
1621 break;
1622 case GL_COLOR_WRITEMASK:
1623 {
1624 *type = GL_BOOL;
1625 *numParams = 4;
1626 }
1627 break;
1628 case GL_POLYGON_OFFSET_FACTOR:
1629 case GL_POLYGON_OFFSET_UNITS:
1630 case GL_SAMPLE_COVERAGE_VALUE:
1631 case GL_DEPTH_CLEAR_VALUE:
1632 case GL_LINE_WIDTH:
1633 {
1634 *type = GL_FLOAT;
1635 *numParams = 1;
1636 }
1637 break;
1638 case GL_ALIASED_LINE_WIDTH_RANGE:
1639 case GL_ALIASED_POINT_SIZE_RANGE:
1640 case GL_DEPTH_RANGE:
1641 {
1642 *type = GL_FLOAT;
1643 *numParams = 2;
1644 }
1645 break;
1646 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001647 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001648 {
1649 *type = GL_FLOAT;
1650 *numParams = 4;
1651 }
1652 break;
1653 default:
1654 return false;
1655 }
1656
1657 return true;
1658}
1659
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001660// Applies the render target surface, depth stencil surface, viewport rectangle and
1661// scissor rectangle to the Direct3D 9 device
1662bool Context::applyRenderTarget(bool ignoreViewport)
1663{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001664 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001665
1666 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1667 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001668 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001669 }
1670
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001671 bool renderTargetChanged = false;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001672 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1673 if (renderTargetSerial != mAppliedRenderTargetSerial)
1674 {
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001675 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001676 if (!renderTarget)
1677 {
1678 return false; // Context must be lost
1679 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001680 mDevice->SetRenderTarget(0, renderTarget);
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001681 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001682 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 +00001683 renderTargetChanged = true;
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001684 renderTarget->Release();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001685 }
1686
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001687 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001688 unsigned int depthbufferSerial = 0;
1689 unsigned int stencilbufferSerial = 0;
1690 if (framebufferObject->getDepthbufferType() != GL_NONE)
1691 {
1692 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001693 if (!depthStencil)
1694 {
1695 ERR("Depth stencil pointer unexpectedly null.");
1696 return false;
1697 }
1698
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001699 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1700 }
1701 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1702 {
1703 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001704 if (!depthStencil)
1705 {
1706 ERR("Depth stencil pointer unexpectedly null.");
1707 return false;
1708 }
1709
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001710 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1711 }
1712
1713 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001714 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001715 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001716 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001717 mDevice->SetDepthStencilSurface(depthStencil);
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001718 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001719 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001720 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001721 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001722
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001723 if (!mRenderTargetDescInitialized || renderTargetChanged)
1724 {
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001725 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001726 if (!renderTarget)
1727 {
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001728 return false; // Context must be lost
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001729 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001730 renderTarget->GetDesc(&mRenderTargetDesc);
1731 mRenderTargetDescInitialized = true;
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001732 renderTarget->Release();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001733 }
1734
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735 D3DVIEWPORT9 viewport;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001736
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001737 float zNear = clamp01(mState.zNear);
1738 float zFar = clamp01(mState.zFar);
1739
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001740 if (ignoreViewport)
1741 {
1742 viewport.X = 0;
1743 viewport.Y = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001744 viewport.Width = mRenderTargetDesc.Width;
1745 viewport.Height = mRenderTargetDesc.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001746 viewport.MinZ = 0.0f;
1747 viewport.MaxZ = 1.0f;
1748 }
1749 else
1750 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001751 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, mRenderTargetDesc.Height);
1752 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1753 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1754 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width) - static_cast<LONG>(viewport.X));
1755 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 +00001756 viewport.MinZ = zNear;
1757 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001758 }
1759
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001760 if (viewport.Width <= 0 || viewport.Height <= 0)
1761 {
1762 return false; // Nothing to render
1763 }
1764
jbauman@chromium.org241e70d2011-11-03 23:07:05 +00001765 if (renderTargetChanged || !mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001766 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001767 mDevice->SetViewport(&viewport);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001768 mSetViewport = viewport;
1769 mViewportInitialized = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001770 mDxUniformsDirty = true;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001771 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001772
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001773 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001774 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001775 if (mState.scissorTest)
1776 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001777 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, mRenderTargetDesc.Height);
1778 rect.left = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1779 rect.top = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1780 rect.right = clamp(rect.right, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1781 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001782 mDevice->SetScissorRect(&rect);
1783 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001784 }
1785 else
1786 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001787 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001788 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001789
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001790 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001791 }
1792
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001793 if (mState.currentProgram && mDxUniformsDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001794 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001795 Program *programObject = getCurrentProgram();
1796
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001797 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001798 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001799 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001800
daniel@transgaming.com31754962010-11-28 02:02:52 +00001801 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001802 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1803 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1804 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001805 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001806
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001807 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001808 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001809 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001810
daniel@transgaming.com31754962010-11-28 02:02:52 +00001811 GLint depthRange = programObject->getDxDepthRangeLocation();
1812 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1813 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001814 mDxUniformsDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001815 }
1816
1817 return true;
1818}
1819
1820// 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 +00001821void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001822{
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001823 Program *programObject = getCurrentProgram();
1824
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001825 Framebuffer *framebufferObject = getDrawFramebuffer();
1826
1827 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1828
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001829 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001830 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001831 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001832
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001833 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001834 GLint alwaysFront = !isTriangleMode(drawMode);
1835 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1836
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001837 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001838 bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
1839 // Apparently some ATI cards have a bug where a draw with a zero color
1840 // write mask can cause later draws to have incorrect results. Instead,
1841 // set a nonzero color write mask but modify the blend state so that no
1842 // drawing is done.
1843 // http://code.google.com/p/angleproject/issues/detail?id=169
1844
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001845 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001846 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001847 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001848 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001849 mDevice->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001850 }
1851 else
1852 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001853 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001854 }
1855
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001856 mCullStateDirty = false;
1857 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001858
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001859 if (mDepthStateDirty)
1860 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00001861 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001862 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001863 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1864 mDevice->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001865 }
1866 else
1867 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001868 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001869 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001870
1871 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001872 }
1873
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001874 if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
1875 {
1876 mBlendStateDirty = true;
1877 mMaskStateDirty = true;
1878 }
1879
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001880 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001881 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001882 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001883 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001884 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001885
1886 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1887 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1888 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001889 mDevice->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001890 }
1891 else
1892 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001893 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001894 unorm<8>(mState.blendColor.alpha),
1895 unorm<8>(mState.blendColor.alpha),
1896 unorm<8>(mState.blendColor.alpha)));
1897 }
1898
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001899 mDevice->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1900 mDevice->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1901 mDevice->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001902
1903 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1904 mState.destBlendRGB != mState.destBlendAlpha ||
1905 mState.blendEquationRGB != mState.blendEquationAlpha)
1906 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001907 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001908
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001909 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1910 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1911 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001912 }
1913 else
1914 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001915 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001916 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001917 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001918 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001919 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001920 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001921 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001922
1923 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001924 }
1925
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001926 if (mStencilStateDirty || mFrontFaceDirty)
1927 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001928 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001929 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001930 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1931 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001932
1933 // FIXME: Unsupported by D3D9
1934 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1935 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1936 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1937 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1938 mState.stencilRef != mState.stencilBackRef ||
1939 mState.stencilMask != mState.stencilBackMask)
1940 {
1941 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1942 return error(GL_INVALID_OPERATION);
1943 }
1944
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001945 // get the maximum size of the stencil ref
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001946 gl::Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001947 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1948
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001949 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1950 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001951 es2dx::ConvertComparison(mState.stencilFunc));
1952
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001953 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1954 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001955
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001956 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001957 es2dx::ConvertStencilOp(mState.stencilFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001958 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001959 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001960 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001961 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1962
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001963 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1964 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001965 es2dx::ConvertComparison(mState.stencilBackFunc));
1966
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001967 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1968 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001969
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001970 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001971 es2dx::ConvertStencilOp(mState.stencilBackFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001972 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001973 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001974 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001975 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1976 }
1977 else
1978 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001979 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001980 }
1981
1982 mStencilStateDirty = false;
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001983 mFrontFaceDirty = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001984 }
1985
1986 if (mMaskStateDirty)
1987 {
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001988 int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1989 mState.colorMaskBlue, mState.colorMaskAlpha);
1990 if (colorMask == 0 && !zeroColorMaskAllowed)
1991 {
1992 // Enable green channel, but set blending so nothing will be drawn.
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001993 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
1994 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001995
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001996 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
1997 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
1998 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001999 }
2000 else
2001 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002002 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002003 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002004 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002005
2006 mMaskStateDirty = false;
2007 }
2008
2009 if (mPolygonOffsetStateDirty)
2010 {
2011 if (mState.polygonOffsetFill)
2012 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00002013 gl::Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002014 if (depthbuffer)
2015 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002016 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002017 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002018 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002019 }
2020 }
2021 else
2022 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002023 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
2024 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002025 }
2026
2027 mPolygonOffsetStateDirty = false;
2028 }
2029
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002030 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002031 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002032 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002033 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002034 FIXME("Sample alpha to coverage is unimplemented.");
2035 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002036
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002037 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002038 if (mState.sampleCoverage)
2039 {
2040 unsigned int mask = 0;
2041 if (mState.sampleCoverageValue != 0)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002042 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002043 float threshold = 0.5f;
2044
2045 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002046 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002047 mask <<= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002048
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002049 if ((i + 1) * mState.sampleCoverageValue >= threshold)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002050 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002051 threshold += 1.0f;
2052 mask |= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002053 }
2054 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002055 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002056
2057 if (mState.sampleCoverageInvert)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002058 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002059 mask = ~mask;
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002060 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002061
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002062 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002063 }
2064 else
2065 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002066 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002067 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002068
2069 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002070 }
2071
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002072 if (mDitherStateDirty)
2073 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002074 mDevice->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002075
2076 mDitherStateDirty = false;
2077 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002078}
2079
daniel@transgaming.com83921382011-01-08 05:46:00 +00002080GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002081{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002082 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002083
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002084 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002085 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002086 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002087 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002088 }
2089
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002090 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, getCurrentProgram());
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002091}
2092
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002093// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002094GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002095{
daniel@transgaming.com83921382011-01-08 05:46:00 +00002096 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002097
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002098 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002099 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002100 if (indexInfo->serial != mAppliedIBSerial)
2101 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002102 mDevice->SetIndices(indexInfo->indexBuffer);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002103 mAppliedIBSerial = indexInfo->serial;
2104 }
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002105 }
2106
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002107 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002108}
2109
2110// Applies the shaders and shader constants to the Direct3D 9 device
2111void Context::applyShaders()
2112{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002113 Program *programObject = getCurrentProgram();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002114 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002115 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002116 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2117 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2118
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002119 mDevice->SetPixelShader(pixelShader);
2120 mDevice->SetVertexShader(vertexShader);
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002121 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002122 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002123 }
2124
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002125 programObject->applyUniforms();
2126}
2127
2128// Applies the textures and sampler states to the Direct3D 9 device
2129void Context::applyTextures()
2130{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002131 applyTextures(SAMPLER_PIXEL);
2132
2133 if (mSupportsVertexTexture)
2134 {
2135 applyTextures(SAMPLER_VERTEX);
2136 }
2137}
2138
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002139// For each Direct3D 9 sampler of either the pixel or vertex stage,
2140// looks up the corresponding OpenGL texture image unit and texture type,
2141// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002142void Context::applyTextures(SamplerType type)
2143{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002144 Program *programObject = getCurrentProgram();
2145
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002146 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 +00002147 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2148 int d3dSamplerOffset = (type == SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002149 int samplerRange = programObject->getUsedSamplerRange(type);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002150
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002151 for (int samplerIndex = 0; samplerIndex < samplerRange; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002152 {
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002153 int textureUnit = programObject->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00002154 int d3dSampler = samplerIndex + d3dSamplerOffset;
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002155
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002156 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002157 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002158 TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002159
2160 Texture *texture = getSamplerTexture(textureUnit, textureType);
2161
daniel@transgaming.comfbc39522011-11-11 04:10:28 +00002162 if (appliedTextureSerial[samplerIndex] != texture->getTextureSerial() || texture->hasDirtyParameters() || texture->hasDirtyImages())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002163 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002164 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2165
2166 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002167 {
daniel@transgaming.comfbc39522011-11-11 04:10:28 +00002168 if (appliedTextureSerial[samplerIndex] != texture->getTextureSerial() || texture->hasDirtyParameters())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002169 {
2170 GLenum wrapS = texture->getWrapS();
2171 GLenum wrapT = texture->getWrapT();
2172 GLenum minFilter = texture->getMinFilter();
2173 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002174
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002175 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2176 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002177
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002178 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002179 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2180 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002181 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2182 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002183 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002184
daniel@transgaming.comfbc39522011-11-11 04:10:28 +00002185 if (appliedTextureSerial[samplerIndex] != texture->getTextureSerial() || texture->hasDirtyImages())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002186 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002187 mDevice->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002188 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002189 }
2190 else
2191 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002192 mDevice->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002193 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002194
daniel@transgaming.comfbc39522011-11-11 04:10:28 +00002195 appliedTextureSerial[samplerIndex] = texture->getTextureSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002196 texture->resetDirty();
2197 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002198 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002199 else
2200 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002201 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002202 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002203 mDevice->SetTexture(d3dSampler, NULL);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002204 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002205 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002206 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002207 }
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002208
2209 for (int samplerIndex = samplerRange; samplerIndex < samplerCount; samplerIndex++)
2210 {
2211 if (appliedTextureSerial[samplerIndex] != 0)
2212 {
daniel@transgaming.comc5a7b692011-10-26 02:45:44 +00002213 mDevice->SetTexture(samplerIndex + d3dSamplerOffset, NULL);
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002214 appliedTextureSerial[samplerIndex] = 0;
2215 }
2216 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002217}
2218
daniel@transgaming.comb7915a52011-11-12 03:14:20 +00002219void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height,
2220 GLenum format, GLenum type, GLsizei *bufSize, void* pixels)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002221{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002222 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002223
2224 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2225 {
2226 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2227 }
2228
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002229 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2230 {
2231 return error(GL_INVALID_OPERATION);
2232 }
2233
daniel@transgaming.comb7915a52011-11-12 03:14:20 +00002234 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
2235 // sized query sanity check
2236 if (bufSize)
2237 {
2238 int requiredSize = outputPitch * height;
2239 if (requiredSize > *bufSize)
2240 {
2241 return error(GL_INVALID_OPERATION);
2242 }
2243 }
2244
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002245 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002246 if (!renderTarget)
2247 {
2248 return; // Context must be lost, return silently
2249 }
2250
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002251 D3DSURFACE_DESC desc;
2252 renderTarget->GetDesc(&desc);
2253
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002254 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2255 {
2256 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.coma5798952011-12-13 17:30:43 +00002257 renderTarget->Release();
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002258 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002259 }
2260
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002261 HRESULT result;
2262 IDirect3DSurface9 *systemSurface = NULL;
2263 bool directToPixels = getPackReverseRowOrder() && getPackAlignment() <= 4 && mDisplay->isD3d9ExDevice() &&
2264 x == 0 && y == 0 && width == desc.Width && height == desc.Height &&
2265 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2266 if (directToPixels)
2267 {
2268 // Use the pixels ptr as a shared handle to write directly into client's memory
2269 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2270 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2271 if (FAILED(result))
2272 {
2273 // Try again without the shared handle
2274 directToPixels = false;
2275 }
2276 }
2277
2278 if (!directToPixels)
2279 {
2280 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2281 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2282 if (FAILED(result))
2283 {
2284 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2285 return error(GL_OUT_OF_MEMORY);
2286 }
2287 }
2288
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002289 result = mDevice->GetRenderTargetData(renderTarget, systemSurface);
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00002290 renderTarget->Release();
daniel@transgaming.coma5798952011-12-13 17:30:43 +00002291 renderTarget = NULL;
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00002292
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002293 if (FAILED(result))
2294 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002295 systemSurface->Release();
2296
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002297 // It turns out that D3D will sometimes produce more error
2298 // codes than those documented.
2299 if (checkDeviceLost(result))
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002300 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002301 else
2302 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002303 UNREACHABLE();
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002304 return;
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002305 }
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002306
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002307 }
2308
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002309 if (directToPixels)
2310 {
2311 systemSurface->Release();
2312 return;
2313 }
2314
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002315 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002316 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2317 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2318 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2319 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2320 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002321
2322 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2323
2324 if (FAILED(result))
2325 {
2326 UNREACHABLE();
2327 systemSurface->Release();
2328
2329 return; // No sensible error to generate
2330 }
2331
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002332 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002333 unsigned short *dest16 = (unsigned short*)pixels;
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002334
2335 unsigned char *source;
2336 int inputPitch;
2337 if (getPackReverseRowOrder())
2338 {
2339 source = (unsigned char*)lock.pBits;
2340 inputPitch = lock.Pitch;
2341 }
2342 else
2343 {
2344 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2345 inputPitch = -lock.Pitch;
2346 }
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002347
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002348 for (int j = 0; j < rect.bottom - rect.top; j++)
2349 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002350 if (desc.Format == D3DFMT_A8R8G8B8 &&
2351 format == GL_BGRA_EXT &&
2352 type == GL_UNSIGNED_BYTE)
2353 {
2354 // Fast path for EXT_read_format_bgra, given
2355 // an RGBA source buffer. Note that buffers with no
2356 // alpha go through the slow path below.
2357 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002358 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002359 (rect.right - rect.left) * 4);
2360 continue;
2361 }
2362
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002363 for (int i = 0; i < rect.right - rect.left; i++)
2364 {
2365 float r;
2366 float g;
2367 float b;
2368 float a;
2369
2370 switch (desc.Format)
2371 {
2372 case D3DFMT_R5G6B5:
2373 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002374 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002375
2376 a = 1.0f;
2377 b = (rgb & 0x001F) * (1.0f / 0x001F);
2378 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2379 r = (rgb & 0xF800) * (1.0f / 0xF800);
2380 }
2381 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002382 case D3DFMT_A1R5G5B5:
2383 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002384 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002385
2386 a = (argb & 0x8000) ? 1.0f : 0.0f;
2387 b = (argb & 0x001F) * (1.0f / 0x001F);
2388 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2389 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2390 }
2391 break;
2392 case D3DFMT_A8R8G8B8:
2393 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002394 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002395
2396 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2397 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2398 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2399 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2400 }
2401 break;
2402 case D3DFMT_X8R8G8B8:
2403 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002404 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002405
2406 a = 1.0f;
2407 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2408 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2409 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2410 }
2411 break;
2412 case D3DFMT_A2R10G10B10:
2413 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002414 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002415
2416 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2417 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2418 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2419 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2420 }
2421 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002422 case D3DFMT_A32B32G32R32F:
2423 {
2424 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002425 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2426 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2427 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2428 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002429 }
2430 break;
2431 case D3DFMT_A16B16G16R16F:
2432 {
2433 // float formats in D3D are stored rgba, rather than the other way round
2434 float abgr[4];
2435
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002436 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002437
2438 a = abgr[3];
2439 b = abgr[2];
2440 g = abgr[1];
2441 r = abgr[0];
2442 }
2443 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002444 default:
2445 UNIMPLEMENTED(); // FIXME
2446 UNREACHABLE();
2447 }
2448
2449 switch (format)
2450 {
2451 case GL_RGBA:
2452 switch (type)
2453 {
2454 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002455 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2456 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2457 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2458 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002459 break;
2460 default: UNREACHABLE();
2461 }
2462 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002463 case GL_BGRA_EXT:
2464 switch (type)
2465 {
2466 case GL_UNSIGNED_BYTE:
2467 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2468 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2469 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2470 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2471 break;
2472 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2473 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2474 // this type is packed as follows:
2475 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2476 // --------------------------------------------------------------------------------
2477 // | 4th | 3rd | 2nd | 1st component |
2478 // --------------------------------------------------------------------------------
2479 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2480 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2481 ((unsigned short)(15 * a + 0.5f) << 12)|
2482 ((unsigned short)(15 * r + 0.5f) << 8) |
2483 ((unsigned short)(15 * g + 0.5f) << 4) |
2484 ((unsigned short)(15 * b + 0.5f) << 0);
2485 break;
2486 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2487 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2488 // this type is packed as follows:
2489 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2490 // --------------------------------------------------------------------------------
2491 // | 4th | 3rd | 2nd | 1st component |
2492 // --------------------------------------------------------------------------------
2493 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2494 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2495 ((unsigned short)( a + 0.5f) << 15) |
2496 ((unsigned short)(31 * r + 0.5f) << 10) |
2497 ((unsigned short)(31 * g + 0.5f) << 5) |
2498 ((unsigned short)(31 * b + 0.5f) << 0);
2499 break;
2500 default: UNREACHABLE();
2501 }
2502 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002503 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002504 switch (type)
2505 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002506 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002507 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2508 ((unsigned short)(31 * b + 0.5f) << 0) |
2509 ((unsigned short)(63 * g + 0.5f) << 5) |
2510 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002511 break;
2512 default: UNREACHABLE();
2513 }
2514 break;
2515 default: UNREACHABLE();
2516 }
2517 }
2518 }
2519
2520 systemSurface->UnlockRect();
2521
2522 systemSurface->Release();
2523}
2524
2525void Context::clear(GLbitfield mask)
2526{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002527 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002528
2529 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2530 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002531 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002532 }
2533
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002534 DWORD flags = 0;
2535
2536 if (mask & GL_COLOR_BUFFER_BIT)
2537 {
2538 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002539
2540 if (framebufferObject->getColorbufferType() != GL_NONE)
2541 {
2542 flags |= D3DCLEAR_TARGET;
2543 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002544 }
2545
2546 if (mask & GL_DEPTH_BUFFER_BIT)
2547 {
2548 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002549 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002550 {
2551 flags |= D3DCLEAR_ZBUFFER;
2552 }
2553 }
2554
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002555 GLuint stencilUnmasked = 0x0;
2556
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002557 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002558 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002559 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002560 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002561 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002562 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002563 if (!depthStencil)
2564 {
2565 ERR("Depth stencil pointer unexpectedly null.");
2566 return;
2567 }
2568
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002569 D3DSURFACE_DESC desc;
2570 depthStencil->GetDesc(&desc);
2571
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002572 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002573 stencilUnmasked = (0x1 << stencilSize) - 1;
2574
2575 if (stencilUnmasked != 0x0)
2576 {
2577 flags |= D3DCLEAR_STENCIL;
2578 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002579 }
2580 }
2581
2582 if (mask != 0)
2583 {
2584 return error(GL_INVALID_VALUE);
2585 }
2586
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002587 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2588 {
2589 return;
2590 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002591
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002592 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002593 unorm<8>(mState.colorClearValue.red),
2594 unorm<8>(mState.colorClearValue.green),
2595 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002596 float depth = clamp01(mState.depthClearValue);
2597 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002598
2599 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002600 if (!renderTarget)
2601 {
2602 return; // Context must be lost, return silently
2603 }
2604
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002605 D3DSURFACE_DESC desc;
2606 renderTarget->GetDesc(&desc);
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00002607 renderTarget->Release();
daniel@transgaming.coma5798952011-12-13 17:30:43 +00002608 renderTarget = NULL;
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00002609
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002610 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002611
2612 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002613 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002614 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002615 !(mState.colorMaskRed && mState.colorMaskGreen &&
2616 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002617
2618 if (needMaskedColorClear || needMaskedStencilClear)
2619 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002620 // State which is altered in all paths from this point to the clear call is saved.
2621 // State which is altered in only some paths will be flagged dirty in the case that
2622 // that path is taken.
2623 HRESULT hr;
2624 if (mMaskedClearSavedState == NULL)
2625 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002626 hr = mDevice->BeginStateBlock();
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002627 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2628
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002629 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2630 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2631 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2632 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2633 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2634 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2635 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2636 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2637 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2638 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2639 mDevice->SetPixelShader(NULL);
2640 mDevice->SetVertexShader(NULL);
2641 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2642 mDevice->SetStreamSource(0, NULL, 0, 0);
2643 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2644 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2645 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2646 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2647 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2648 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2649 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002650
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002651 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002652 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2653 }
2654
2655 ASSERT(mMaskedClearSavedState != NULL);
2656
2657 if (mMaskedClearSavedState != NULL)
2658 {
2659 hr = mMaskedClearSavedState->Capture();
2660 ASSERT(SUCCEEDED(hr));
2661 }
2662
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002663 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2664 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2665 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2666 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2667 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2668 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2669 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2670 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002671
2672 if (flags & D3DCLEAR_TARGET)
2673 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002674 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002675 }
2676 else
2677 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002678 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002679 }
2680
2681 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2682 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002683 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2684 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2685 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2686 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
2687 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
2688 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
2689 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2690 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002691 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002692 }
2693 else
2694 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002695 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002696 }
2697
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002698 mDevice->SetPixelShader(NULL);
2699 mDevice->SetVertexShader(NULL);
2700 mDevice->SetFVF(D3DFVF_XYZRHW);
2701 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2702 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2703 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2704 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2705 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2706 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2707 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002708
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002709 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2710 quad[0][0] = -0.5f;
2711 quad[0][1] = desc.Height - 0.5f;
2712 quad[0][2] = 0.0f;
2713 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002714
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002715 quad[1][0] = desc.Width - 0.5f;
2716 quad[1][1] = desc.Height - 0.5f;
2717 quad[1][2] = 0.0f;
2718 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002719
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002720 quad[2][0] = -0.5f;
2721 quad[2][1] = -0.5f;
2722 quad[2][2] = 0.0f;
2723 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002724
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002725 quad[3][0] = desc.Width - 0.5f;
2726 quad[3][1] = -0.5f;
2727 quad[3][2] = 0.0f;
2728 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002729
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002730 mDisplay->startScene();
2731 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002732
2733 if (flags & D3DCLEAR_ZBUFFER)
2734 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002735 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
2736 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2737 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002738 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002739
2740 if (mMaskedClearSavedState != NULL)
2741 {
2742 mMaskedClearSavedState->Apply();
2743 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002744 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002745 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002746 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002747 mDevice->Clear(0, NULL, flags, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002748 }
2749}
2750
2751void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2752{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002753 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002754 {
2755 return error(GL_INVALID_OPERATION);
2756 }
2757
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002758 D3DPRIMITIVETYPE primitiveType;
2759 int primitiveCount;
2760
2761 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2762 return error(GL_INVALID_ENUM);
2763
2764 if (primitiveCount <= 0)
2765 {
2766 return;
2767 }
2768
2769 if (!applyRenderTarget(false))
2770 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002771 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002772 }
2773
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002774 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002775
daniel@transgaming.com83921382011-01-08 05:46:00 +00002776 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002777 if (err != GL_NO_ERROR)
2778 {
2779 return error(err);
2780 }
2781
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002782 applyShaders();
2783 applyTextures();
2784
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002785 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002786 {
2787 return error(GL_INVALID_OPERATION);
2788 }
2789
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002790 if (!cullSkipsDraw(mode))
2791 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002792 mDisplay->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002793
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002794 mDevice->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002795
2796 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2797 {
daniel@transgaming.com102ca742011-11-24 22:34:14 +00002798 drawClosingLine(0, count - 1, 0);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002799 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002800 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002801}
2802
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002803void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002804{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002805 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002806 {
2807 return error(GL_INVALID_OPERATION);
2808 }
2809
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002810 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002811 {
2812 return error(GL_INVALID_OPERATION);
2813 }
2814
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002815 D3DPRIMITIVETYPE primitiveType;
2816 int primitiveCount;
2817
2818 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2819 return error(GL_INVALID_ENUM);
2820
2821 if (primitiveCount <= 0)
2822 {
2823 return;
2824 }
2825
2826 if (!applyRenderTarget(false))
2827 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002828 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002829 }
2830
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002831 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002832
2833 TranslatedIndexData indexInfo;
2834 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2835 if (err != GL_NO_ERROR)
2836 {
2837 return error(err);
2838 }
2839
daniel@transgaming.com83921382011-01-08 05:46:00 +00002840 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2841 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002842 if (err != GL_NO_ERROR)
2843 {
2844 return error(err);
2845 }
2846
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002847 applyShaders();
2848 applyTextures();
2849
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002850 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002851 {
2852 return error(GL_INVALID_OPERATION);
2853 }
2854
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002855 if (!cullSkipsDraw(mode))
2856 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002857 mDisplay->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002858
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002859 mDevice->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002860
2861 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2862 {
daniel@transgaming.com102ca742011-11-24 22:34:14 +00002863 drawClosingLine(count, type, indices, indexInfo.minIndex);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002864 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002865 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002866}
2867
daniel@transgaming.com0d86aa72011-10-26 02:35:10 +00002868// Implements glFlush when block is false, glFinish when block is true
2869void Context::sync(bool block)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002870{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002871 IDirect3DQuery9 *eventQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002872 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002873
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002874 result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002875 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002876 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002877 ERR("CreateQuery failed hr=%x\n", result);
2878 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2879 {
2880 return error(GL_OUT_OF_MEMORY);
2881 }
2882 ASSERT(false);
2883 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002884 }
2885
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002886 result = eventQuery->Issue(D3DISSUE_END);
2887 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002888 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002889 ERR("eventQuery->Issue(END) failed hr=%x\n", result);
2890 ASSERT(false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002891 eventQuery->Release();
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002892 return;
2893 }
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002894
daniel@transgaming.com0d86aa72011-10-26 02:35:10 +00002895 do
2896 {
2897 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
2898
2899 if(block && result == S_FALSE)
2900 {
2901 // Keep polling, but allow other threads to do something useful first
2902 Sleep(0);
2903 }
2904 }
2905 while(block && result == S_FALSE);
2906
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002907 eventQuery->Release();
2908
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002909 if (checkDeviceLost(result))
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002910 {
2911 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002912 }
2913}
2914
daniel@transgaming.com102ca742011-11-24 22:34:14 +00002915void Context::drawClosingLine(unsigned int first, unsigned int last, int minIndex)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002916{
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002917 IDirect3DIndexBuffer9 *indexBuffer = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002918 bool succeeded = false;
2919 UINT offset;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002920
2921 if (supports32bitIndices())
2922 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002923 const int spaceNeeded = 2 * sizeof(unsigned int);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002924
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002925 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002926 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002927 mClosingIB = new StreamingIndexBuffer(mDevice, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002928 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002929
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002930 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
2931
2932 unsigned int *data = static_cast<unsigned int*>(mClosingIB->map(spaceNeeded, &offset));
2933 if (data)
2934 {
2935 data[0] = last;
2936 data[1] = first;
2937 mClosingIB->unmap();
2938 offset /= 4;
2939 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002940 }
2941 }
2942 else
2943 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002944 const int spaceNeeded = 2 * sizeof(unsigned short);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002945
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002946 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002947 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002948 mClosingIB = new StreamingIndexBuffer(mDevice, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002949 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002950
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002951 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
2952
2953 unsigned short *data = static_cast<unsigned short*>(mClosingIB->map(spaceNeeded, &offset));
2954 if (data)
2955 {
2956 data[0] = last;
2957 data[1] = first;
2958 mClosingIB->unmap();
2959 offset /= 2;
2960 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002961 }
2962 }
2963
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002964 if (succeeded)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002965 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002966 mDevice->SetIndices(mClosingIB->getBuffer());
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002967 mAppliedIBSerial = mClosingIB->getSerial();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002968
daniel@transgaming.com102ca742011-11-24 22:34:14 +00002969 mDevice->DrawIndexedPrimitive(D3DPT_LINELIST, -minIndex, minIndex, last, offset, 1);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002970 }
2971 else
2972 {
2973 ERR("Could not create an index buffer for closing a line loop.");
2974 error(GL_OUT_OF_MEMORY);
2975 }
2976}
2977
daniel@transgaming.com102ca742011-11-24 22:34:14 +00002978void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices, int minIndex)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002979{
2980 unsigned int first = 0;
2981 unsigned int last = 0;
2982
2983 if (mState.elementArrayBuffer.get())
2984 {
2985 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2986 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2987 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2988 }
2989
2990 switch (type)
2991 {
2992 case GL_UNSIGNED_BYTE:
2993 first = static_cast<const GLubyte*>(indices)[0];
2994 last = static_cast<const GLubyte*>(indices)[count - 1];
2995 break;
2996 case GL_UNSIGNED_SHORT:
2997 first = static_cast<const GLushort*>(indices)[0];
2998 last = static_cast<const GLushort*>(indices)[count - 1];
2999 break;
3000 case GL_UNSIGNED_INT:
3001 first = static_cast<const GLuint*>(indices)[0];
3002 last = static_cast<const GLuint*>(indices)[count - 1];
3003 break;
3004 default: UNREACHABLE();
3005 }
3006
daniel@transgaming.com102ca742011-11-24 22:34:14 +00003007 drawClosingLine(first, last, minIndex);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003008}
3009
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003010void Context::recordInvalidEnum()
3011{
3012 mInvalidEnum = true;
3013}
3014
3015void Context::recordInvalidValue()
3016{
3017 mInvalidValue = true;
3018}
3019
3020void Context::recordInvalidOperation()
3021{
3022 mInvalidOperation = true;
3023}
3024
3025void Context::recordOutOfMemory()
3026{
3027 mOutOfMemory = true;
3028}
3029
3030void Context::recordInvalidFramebufferOperation()
3031{
3032 mInvalidFramebufferOperation = true;
3033}
3034
3035// Get one of the recorded errors and clear its flag, if any.
3036// [OpenGL ES 2.0.24] section 2.5 page 13.
3037GLenum Context::getError()
3038{
3039 if (mInvalidEnum)
3040 {
3041 mInvalidEnum = false;
3042
3043 return GL_INVALID_ENUM;
3044 }
3045
3046 if (mInvalidValue)
3047 {
3048 mInvalidValue = false;
3049
3050 return GL_INVALID_VALUE;
3051 }
3052
3053 if (mInvalidOperation)
3054 {
3055 mInvalidOperation = false;
3056
3057 return GL_INVALID_OPERATION;
3058 }
3059
3060 if (mOutOfMemory)
3061 {
3062 mOutOfMemory = false;
3063
3064 return GL_OUT_OF_MEMORY;
3065 }
3066
3067 if (mInvalidFramebufferOperation)
3068 {
3069 mInvalidFramebufferOperation = false;
3070
3071 return GL_INVALID_FRAMEBUFFER_OPERATION;
3072 }
3073
3074 return GL_NO_ERROR;
3075}
3076
daniel@transgaming.com17f548c2011-11-09 17:47:02 +00003077GLenum Context::getResetStatus()
3078{
3079 if (mResetStatus == GL_NO_ERROR)
3080 {
3081 bool lost = mDisplay->testDeviceLost();
3082
3083 if (lost)
3084 {
3085 mDisplay->notifyDeviceLost(); // Sets mResetStatus
3086 }
3087 }
3088
3089 GLenum status = mResetStatus;
3090
3091 if (mResetStatus != GL_NO_ERROR)
3092 {
3093 if (mDisplay->testDeviceResettable())
3094 {
3095 mResetStatus = GL_NO_ERROR;
3096 }
3097 }
3098
3099 return status;
3100}
3101
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00003102bool Context::isResetNotificationEnabled()
3103{
3104 return (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT);
3105}
3106
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003107bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003108{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003109 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003110}
3111
daniel@transgaming.com396c6432010-11-26 16:26:12 +00003112int Context::getMaximumVaryingVectors() const
3113{
3114 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3115}
3116
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003117unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003118{
3119 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3120}
3121
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003122unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003123{
3124 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3125}
3126
daniel@transgaming.com458da142010-11-28 02:03:02 +00003127int Context::getMaximumFragmentUniformVectors() const
3128{
3129 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3130}
3131
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003132int Context::getMaxSupportedSamples() const
3133{
3134 return mMaxSupportedSamples;
3135}
3136
3137int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3138{
3139 if (requested == 0)
3140 {
3141 return requested;
3142 }
3143
3144 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3145 if (itr == mMultiSampleSupport.end())
3146 {
3147 return -1;
3148 }
3149
3150 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3151 {
3152 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3153 {
3154 return i;
3155 }
3156 }
3157
3158 return -1;
3159}
3160
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003161bool Context::supportsEventQueries() const
3162{
3163 return mSupportsEventQueries;
3164}
3165
gman@chromium.org50c526d2011-08-10 05:19:44 +00003166bool Context::supportsDXT1Textures() const
daniel@transgaming.com01868132010-08-24 19:21:17 +00003167{
gman@chromium.org50c526d2011-08-10 05:19:44 +00003168 return mSupportsDXT1Textures;
3169}
3170
3171bool Context::supportsDXT3Textures() const
3172{
3173 return mSupportsDXT3Textures;
3174}
3175
3176bool Context::supportsDXT5Textures() const
3177{
3178 return mSupportsDXT5Textures;
daniel@transgaming.com01868132010-08-24 19:21:17 +00003179}
3180
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003181bool Context::supportsFloat32Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003182{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003183 return mSupportsFloat32Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003184}
3185
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003186bool Context::supportsFloat32LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003187{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003188 return mSupportsFloat32LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003189}
3190
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003191bool Context::supportsFloat32RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003192{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003193 return mSupportsFloat32RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003194}
3195
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003196bool Context::supportsFloat16Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003197{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003198 return mSupportsFloat16Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003199}
3200
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003201bool Context::supportsFloat16LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003202{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003203 return mSupportsFloat16LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003204}
3205
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003206bool Context::supportsFloat16RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003207{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003208 return mSupportsFloat16RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003209}
3210
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003211int Context::getMaximumRenderbufferDimension() const
3212{
3213 return mMaxRenderbufferDimension;
3214}
3215
3216int Context::getMaximumTextureDimension() const
3217{
3218 return mMaxTextureDimension;
3219}
3220
3221int Context::getMaximumCubeTextureDimension() const
3222{
3223 return mMaxCubeTextureDimension;
3224}
3225
3226int Context::getMaximumTextureLevel() const
3227{
3228 return mMaxTextureLevel;
3229}
3230
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003231bool Context::supportsLuminanceTextures() const
3232{
3233 return mSupportsLuminanceTextures;
3234}
3235
3236bool Context::supportsLuminanceAlphaTextures() const
3237{
3238 return mSupportsLuminanceAlphaTextures;
3239}
3240
daniel@transgaming.com83921382011-01-08 05:46:00 +00003241bool Context::supports32bitIndices() const
3242{
3243 return mSupports32bitIndices;
3244}
3245
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003246bool Context::supportsNonPower2Texture() const
3247{
3248 return mSupportsNonPower2Texture;
3249}
3250
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003251void Context::detachBuffer(GLuint buffer)
3252{
3253 // [OpenGL ES 2.0.24] section 2.9 page 22:
3254 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3255 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3256
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003257 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003258 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003259 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003260 }
3261
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003262 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003263 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003264 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003265 }
3266
3267 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3268 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003269 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003270 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003271 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003272 }
3273 }
3274}
3275
3276void Context::detachTexture(GLuint texture)
3277{
3278 // [OpenGL ES 2.0.24] section 3.8 page 84:
3279 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3280 // rebound to texture object zero
3281
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003282 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003283 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003284 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003285 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003286 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003287 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003288 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003289 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003290 }
3291 }
3292
3293 // [OpenGL ES 2.0.24] section 4.4 page 112:
3294 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3295 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3296 // image was attached in the currently bound framebuffer.
3297
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003298 Framebuffer *readFramebuffer = getReadFramebuffer();
3299 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003300
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003301 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003302 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003303 readFramebuffer->detachTexture(texture);
3304 }
3305
3306 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3307 {
3308 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003309 }
3310}
3311
3312void Context::detachFramebuffer(GLuint framebuffer)
3313{
3314 // [OpenGL ES 2.0.24] section 4.4 page 107:
3315 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3316 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3317
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003318 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003319 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003320 bindReadFramebuffer(0);
3321 }
3322
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003323 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003324 {
3325 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003326 }
3327}
3328
3329void Context::detachRenderbuffer(GLuint renderbuffer)
3330{
3331 // [OpenGL ES 2.0.24] section 4.4 page 109:
3332 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3333 // had been executed with the target RENDERBUFFER and name of zero.
3334
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003335 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003336 {
3337 bindRenderbuffer(0);
3338 }
3339
3340 // [OpenGL ES 2.0.24] section 4.4 page 111:
3341 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3342 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3343 // point to which this image was attached in the currently bound framebuffer.
3344
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003345 Framebuffer *readFramebuffer = getReadFramebuffer();
3346 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003347
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003348 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003349 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003350 readFramebuffer->detachRenderbuffer(renderbuffer);
3351 }
3352
3353 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3354 {
3355 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003356 }
3357}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003358
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003359Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003360{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003361 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003362
3363 if (t == NULL)
3364 {
3365 static const GLubyte color[] = { 0, 0, 0, 255 };
3366
3367 switch (type)
3368 {
3369 default:
3370 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003371 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003372
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003373 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003374 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003375 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003376 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003377 t = incomplete2d;
3378 }
3379 break;
3380
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003381 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003382 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003383 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003384
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003385 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3386 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3387 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3388 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3389 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3390 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003391
3392 t = incompleteCube;
3393 }
3394 break;
3395 }
3396
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003397 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003398 }
3399
3400 return t;
3401}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003402
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003403bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003404{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003405 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003406}
3407
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003408bool Context::isTriangleMode(GLenum drawMode)
3409{
3410 switch (drawMode)
3411 {
3412 case GL_TRIANGLES:
3413 case GL_TRIANGLE_FAN:
3414 case GL_TRIANGLE_STRIP:
3415 return true;
3416 case GL_POINTS:
3417 case GL_LINES:
3418 case GL_LINE_LOOP:
3419 case GL_LINE_STRIP:
3420 return false;
3421 default: UNREACHABLE();
3422 }
3423
3424 return false;
3425}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003426
3427void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3428{
3429 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3430
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003431 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3432 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3433 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3434 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003435
daniel@transgaming.com83921382011-01-08 05:46:00 +00003436 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003437}
3438
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003439// keep list sorted in following order
3440// OES extensions
3441// EXT extensions
3442// Vendor extensions
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003443void Context::initExtensionString()
3444{
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003445 mExtensionString = "";
3446
3447 // OES extensions
3448 if (supports32bitIndices())
3449 {
3450 mExtensionString += "GL_OES_element_index_uint ";
3451 }
3452
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003453 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003454 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003455 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003456
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003457 if (supportsFloat16Textures())
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003458 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003459 mExtensionString += "GL_OES_texture_half_float ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003460 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003461 if (supportsFloat16LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003462 {
3463 mExtensionString += "GL_OES_texture_half_float_linear ";
3464 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003465 if (supportsFloat32Textures())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003466 {
3467 mExtensionString += "GL_OES_texture_float ";
3468 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003469 if (supportsFloat32LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003470 {
3471 mExtensionString += "GL_OES_texture_float_linear ";
3472 }
3473
3474 if (supportsNonPower2Texture())
3475 {
3476 mExtensionString += "GL_OES_texture_npot ";
3477 }
3478
3479 // Multi-vendor (EXT) extensions
3480 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com8747f182011-11-09 17:50:38 +00003481 mExtensionString += "GL_EXT_robustness ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003482
gman@chromium.org50c526d2011-08-10 05:19:44 +00003483 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00003484 {
3485 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3486 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003487
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003488 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
vangelis@google.com29217fa2011-12-13 01:46:18 +00003489 // Temporarily disabled until
3490 // http://code.google.com/p/angleproject/issues/detail?id=266
3491 // is resolved.
3492 // mExtensionString += "GL_EXT_texture_storage ";
gman@chromium.org50c526d2011-08-10 05:19:44 +00003493
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003494 // ANGLE-specific extensions
3495 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003496 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003497 {
3498 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3499 }
3500
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00003501 mExtensionString += "GL_ANGLE_pack_reverse_row_order ";
3502
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003503 if (supportsDXT3Textures())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003504 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003505 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3506 }
3507 if (supportsDXT5Textures())
3508 {
3509 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003510 }
daniel@transgaming.com97412f72011-11-11 04:19:07 +00003511
3512 mExtensionString += "GL_ANGLE_texture_usage ";
zmo@google.coma574f782011-10-03 21:45:23 +00003513 mExtensionString += "GL_ANGLE_translated_shader_source ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003514
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003515 // Other vendor-specific extensions
3516 if (supportsEventQueries())
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003517 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003518 mExtensionString += "GL_NV_fence ";
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003519 }
3520
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003521 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3522 if (end != std::string::npos)
3523 {
3524 mExtensionString.resize(end+1);
3525 }
3526}
3527
3528const char *Context::getExtensionString() const
3529{
3530 return mExtensionString.c_str();
3531}
3532
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003533void Context::initRendererString()
3534{
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003535 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003536
3537 mRendererString = "ANGLE (";
3538 mRendererString += identifier->Description;
3539 mRendererString += ")";
3540}
3541
3542const char *Context::getRendererString() const
3543{
3544 return mRendererString.c_str();
3545}
3546
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003547void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3548 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3549 GLbitfield mask)
3550{
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003551 Framebuffer *readFramebuffer = getReadFramebuffer();
3552 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3553
3554 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3555 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3556 {
3557 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3558 }
3559
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003560 if (drawFramebuffer->getSamples() != 0)
3561 {
3562 return error(GL_INVALID_OPERATION);
3563 }
3564
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003565 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3566 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3567 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3568 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3569
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003570 RECT sourceRect;
3571 RECT destRect;
3572
3573 if (srcX0 < srcX1)
3574 {
3575 sourceRect.left = srcX0;
3576 sourceRect.right = srcX1;
3577 destRect.left = dstX0;
3578 destRect.right = dstX1;
3579 }
3580 else
3581 {
3582 sourceRect.left = srcX1;
3583 destRect.left = dstX1;
3584 sourceRect.right = srcX0;
3585 destRect.right = dstX0;
3586 }
3587
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003588 if (srcY0 < srcY1)
3589 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003590 sourceRect.top = readBufferHeight - srcY1;
3591 destRect.top = drawBufferHeight - dstY1;
3592 sourceRect.bottom = readBufferHeight - srcY0;
3593 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003594 }
3595 else
3596 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003597 sourceRect.top = readBufferHeight - srcY0;
3598 destRect.top = drawBufferHeight - dstY0;
3599 sourceRect.bottom = readBufferHeight - srcY1;
3600 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003601 }
3602
3603 RECT sourceScissoredRect = sourceRect;
3604 RECT destScissoredRect = destRect;
3605
3606 if (mState.scissorTest)
3607 {
3608 // Only write to parts of the destination framebuffer which pass the scissor test
3609 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3610 // rect will be checked against scissorY, rather than the bottom.
3611 if (destRect.left < mState.scissorX)
3612 {
3613 int xDiff = mState.scissorX - destRect.left;
3614 destScissoredRect.left = mState.scissorX;
3615 sourceScissoredRect.left += xDiff;
3616 }
3617
3618 if (destRect.right > mState.scissorX + mState.scissorWidth)
3619 {
3620 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3621 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3622 sourceScissoredRect.right -= xDiff;
3623 }
3624
3625 if (destRect.top < mState.scissorY)
3626 {
3627 int yDiff = mState.scissorY - destRect.top;
3628 destScissoredRect.top = mState.scissorY;
3629 sourceScissoredRect.top += yDiff;
3630 }
3631
3632 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3633 {
3634 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3635 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3636 sourceScissoredRect.bottom -= yDiff;
3637 }
3638 }
3639
3640 bool blitRenderTarget = false;
3641 bool blitDepthStencil = false;
3642
3643 RECT sourceTrimmedRect = sourceScissoredRect;
3644 RECT destTrimmedRect = destScissoredRect;
3645
3646 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3647 // the actual draw and read surfaces.
3648 if (sourceTrimmedRect.left < 0)
3649 {
3650 int xDiff = 0 - sourceTrimmedRect.left;
3651 sourceTrimmedRect.left = 0;
3652 destTrimmedRect.left += xDiff;
3653 }
3654
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003655 if (sourceTrimmedRect.right > readBufferWidth)
3656 {
3657 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3658 sourceTrimmedRect.right = readBufferWidth;
3659 destTrimmedRect.right -= xDiff;
3660 }
3661
3662 if (sourceTrimmedRect.top < 0)
3663 {
3664 int yDiff = 0 - sourceTrimmedRect.top;
3665 sourceTrimmedRect.top = 0;
3666 destTrimmedRect.top += yDiff;
3667 }
3668
3669 if (sourceTrimmedRect.bottom > readBufferHeight)
3670 {
3671 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3672 sourceTrimmedRect.bottom = readBufferHeight;
3673 destTrimmedRect.bottom -= yDiff;
3674 }
3675
3676 if (destTrimmedRect.left < 0)
3677 {
3678 int xDiff = 0 - destTrimmedRect.left;
3679 destTrimmedRect.left = 0;
3680 sourceTrimmedRect.left += xDiff;
3681 }
3682
3683 if (destTrimmedRect.right > drawBufferWidth)
3684 {
3685 int xDiff = destTrimmedRect.right - drawBufferWidth;
3686 destTrimmedRect.right = drawBufferWidth;
3687 sourceTrimmedRect.right -= xDiff;
3688 }
3689
3690 if (destTrimmedRect.top < 0)
3691 {
3692 int yDiff = 0 - destTrimmedRect.top;
3693 destTrimmedRect.top = 0;
3694 sourceTrimmedRect.top += yDiff;
3695 }
3696
3697 if (destTrimmedRect.bottom > drawBufferHeight)
3698 {
3699 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3700 destTrimmedRect.bottom = drawBufferHeight;
3701 sourceTrimmedRect.bottom -= yDiff;
3702 }
3703
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003704 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003705 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3706 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3707 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3708 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003709 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3710 {
3711 partialBufferCopy = true;
3712 }
3713
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003714 if (mask & GL_COLOR_BUFFER_BIT)
3715 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003716 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3717 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3718 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3719 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3720 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003721 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3722 {
3723 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3724 return error(GL_INVALID_OPERATION);
3725 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003726
3727 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3728 {
3729 return error(GL_INVALID_OPERATION);
3730 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003731
3732 blitRenderTarget = true;
3733
3734 }
3735
3736 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3737 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00003738 Renderbuffer *readDSBuffer = NULL;
3739 Renderbuffer *drawDSBuffer = NULL;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003740
3741 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3742 // both a depth and stencil buffer, it will be the same buffer.
3743
3744 if (mask & GL_DEPTH_BUFFER_BIT)
3745 {
3746 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3747 {
3748 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3749 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3750 {
3751 return error(GL_INVALID_OPERATION);
3752 }
3753
3754 blitDepthStencil = true;
3755 readDSBuffer = readFramebuffer->getDepthbuffer();
3756 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3757 }
3758 }
3759
3760 if (mask & GL_STENCIL_BUFFER_BIT)
3761 {
3762 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3763 {
3764 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3765 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3766 {
3767 return error(GL_INVALID_OPERATION);
3768 }
3769
3770 blitDepthStencil = true;
3771 readDSBuffer = readFramebuffer->getStencilbuffer();
3772 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3773 }
3774 }
3775
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003776 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003777 {
3778 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3779 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3780 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003781
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003782 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3783 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003784 {
3785 return error(GL_INVALID_OPERATION);
3786 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003787 }
3788
3789 if (blitRenderTarget || blitDepthStencil)
3790 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003791 mDisplay->endScene();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003792
3793 if (blitRenderTarget)
3794 {
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00003795 IDirect3DSurface9* readRenderTarget = readFramebuffer->getRenderTarget();
3796 IDirect3DSurface9* drawRenderTarget = drawFramebuffer->getRenderTarget();
3797
3798 HRESULT result = mDevice->StretchRect(readRenderTarget, &sourceTrimmedRect,
3799 drawRenderTarget, &destTrimmedRect, D3DTEXF_NONE);
3800
3801 readRenderTarget->Release();
3802 drawRenderTarget->Release();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003803
3804 if (FAILED(result))
3805 {
3806 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3807 return;
3808 }
3809 }
3810
3811 if (blitDepthStencil)
3812 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003813 HRESULT result = mDevice->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003814
3815 if (FAILED(result))
3816 {
3817 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3818 return;
3819 }
3820 }
3821 }
3822}
3823
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003824VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
3825{
3826 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3827 {
3828 mVertexDeclCache[i].vertexDeclaration = NULL;
3829 mVertexDeclCache[i].lruCount = 0;
3830 }
3831}
3832
3833VertexDeclarationCache::~VertexDeclarationCache()
3834{
3835 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3836 {
3837 if (mVertexDeclCache[i].vertexDeclaration)
3838 {
3839 mVertexDeclCache[i].vertexDeclaration->Release();
3840 }
3841 }
3842}
3843
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003844GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], Program *program)
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003845{
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003846 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
3847 D3DVERTEXELEMENT9 *element = &elements[0];
3848
3849 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3850 {
3851 if (attributes[i].active)
3852 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003853 if (mAppliedVBs[i].serial != attributes[i].serial ||
3854 mAppliedVBs[i].stride != attributes[i].stride ||
3855 mAppliedVBs[i].offset != attributes[i].offset)
3856 {
3857 device->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
3858 mAppliedVBs[i].serial = attributes[i].serial;
3859 mAppliedVBs[i].stride = attributes[i].stride;
3860 mAppliedVBs[i].offset = attributes[i].offset;
3861 }
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003862
3863 element->Stream = i;
3864 element->Offset = 0;
3865 element->Type = attributes[i].type;
3866 element->Method = D3DDECLMETHOD_DEFAULT;
3867 element->Usage = D3DDECLUSAGE_TEXCOORD;
3868 element->UsageIndex = program->getSemanticIndex(i);
3869 element++;
3870 }
3871 }
3872
3873 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
3874 *(element++) = end;
3875
3876 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3877 {
3878 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
3879 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
3880 {
3881 entry->lruCount = ++mMaxLru;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003882 if(entry->vertexDeclaration != mLastSetVDecl)
3883 {
3884 device->SetVertexDeclaration(entry->vertexDeclaration);
3885 mLastSetVDecl = entry->vertexDeclaration;
3886 }
3887
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003888 return GL_NO_ERROR;
3889 }
3890 }
3891
3892 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
3893
3894 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3895 {
3896 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
3897 {
3898 lastCache = &mVertexDeclCache[i];
3899 }
3900 }
3901
3902 if (lastCache->vertexDeclaration != NULL)
3903 {
3904 lastCache->vertexDeclaration->Release();
3905 lastCache->vertexDeclaration = NULL;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003906 // mLastSetVDecl is set to the replacement, so we don't have to worry
3907 // about it.
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003908 }
3909
3910 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
3911 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
3912 device->SetVertexDeclaration(lastCache->vertexDeclaration);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003913 mLastSetVDecl = lastCache->vertexDeclaration;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003914 lastCache->lruCount = ++mMaxLru;
3915
3916 return GL_NO_ERROR;
3917}
3918
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003919void VertexDeclarationCache::markStateDirty()
3920{
3921 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3922 {
3923 mAppliedVBs[i].serial = 0;
3924 }
3925
3926 mLastSetVDecl = NULL;
3927}
3928
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003929}
3930
3931extern "C"
3932{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00003933gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext, bool notifyResets, bool robustAccess)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003934{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00003935 return new gl::Context(config, shareContext, notifyResets, robustAccess);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003936}
3937
3938void glDestroyContext(gl::Context *context)
3939{
3940 delete context;
3941
3942 if (context == gl::getContext())
3943 {
3944 gl::makeCurrent(NULL, NULL, NULL);
3945 }
3946}
3947
3948void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3949{
3950 gl::makeCurrent(context, display, surface);
3951}
3952
3953gl::Context *glGetCurrentContext()
3954{
3955 return gl::getContext();
3956}
3957}