blob: 0de524ac8cf6863897a50fa65e6efee9400f2f5c [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.org8b3c1af2011-10-12 01:21:41 +00001671 IDirect3DSurface9 *renderTarget = NULL;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001672 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001673
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001674 bool renderTargetChanged = false;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001675 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1676 if (renderTargetSerial != mAppliedRenderTargetSerial)
1677 {
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001678 renderTarget = framebufferObject->getRenderTarget();
1679
1680 if (!renderTarget)
1681 {
1682 return false; // Context must be lost
1683 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001684 mDevice->SetRenderTarget(0, renderTarget);
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001685 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001686 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 +00001687 renderTargetChanged = true;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001688 }
1689
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001690 unsigned int depthbufferSerial = 0;
1691 unsigned int stencilbufferSerial = 0;
1692 if (framebufferObject->getDepthbufferType() != GL_NONE)
1693 {
1694 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001695 if (!depthStencil)
1696 {
1697 ERR("Depth stencil pointer unexpectedly null.");
1698 return false;
1699 }
1700
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001701 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1702 }
1703 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1704 {
1705 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001706 if (!depthStencil)
1707 {
1708 ERR("Depth stencil pointer unexpectedly null.");
1709 return false;
1710 }
1711
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001712 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1713 }
1714
1715 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001716 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001717 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001718 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001719 mDevice->SetDepthStencilSurface(depthStencil);
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001720 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001721 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001722 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001723 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001724
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001725 if (!mRenderTargetDescInitialized || renderTargetChanged)
1726 {
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001727 if (!renderTarget)
1728 {
1729 renderTarget = framebufferObject->getRenderTarget();
1730
1731 if (!renderTarget)
1732 {
1733 return false; // Context must be lost
1734 }
1735 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001736 renderTarget->GetDesc(&mRenderTargetDesc);
1737 mRenderTargetDescInitialized = true;
1738 }
1739
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001740 D3DVIEWPORT9 viewport;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001741
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001742 float zNear = clamp01(mState.zNear);
1743 float zFar = clamp01(mState.zFar);
1744
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001745 if (ignoreViewport)
1746 {
1747 viewport.X = 0;
1748 viewport.Y = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001749 viewport.Width = mRenderTargetDesc.Width;
1750 viewport.Height = mRenderTargetDesc.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001751 viewport.MinZ = 0.0f;
1752 viewport.MaxZ = 1.0f;
1753 }
1754 else
1755 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001756 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, mRenderTargetDesc.Height);
1757 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1758 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1759 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width) - static_cast<LONG>(viewport.X));
1760 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 +00001761 viewport.MinZ = zNear;
1762 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001763 }
1764
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001765 if (viewport.Width <= 0 || viewport.Height <= 0)
1766 {
1767 return false; // Nothing to render
1768 }
1769
jbauman@chromium.org241e70d2011-11-03 23:07:05 +00001770 if (renderTargetChanged || !mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001771 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001772 mDevice->SetViewport(&viewport);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001773 mSetViewport = viewport;
1774 mViewportInitialized = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001775 mDxUniformsDirty = true;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001776 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001777
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001778 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001779 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001780 if (mState.scissorTest)
1781 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001782 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, mRenderTargetDesc.Height);
1783 rect.left = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1784 rect.top = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1785 rect.right = clamp(rect.right, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1786 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001787 mDevice->SetScissorRect(&rect);
1788 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001789 }
1790 else
1791 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001792 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001793 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001794
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001795 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001796 }
1797
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001798 if (mState.currentProgram && mDxUniformsDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001799 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001800 Program *programObject = getCurrentProgram();
1801
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001802 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001803 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001804 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001805
daniel@transgaming.com31754962010-11-28 02:02:52 +00001806 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001807 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1808 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1809 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001810 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001811
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001812 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001813 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001814 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001815
daniel@transgaming.com31754962010-11-28 02:02:52 +00001816 GLint depthRange = programObject->getDxDepthRangeLocation();
1817 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1818 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001819 mDxUniformsDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001820 }
1821
1822 return true;
1823}
1824
1825// 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 +00001826void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001827{
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001828 Program *programObject = getCurrentProgram();
1829
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001830 Framebuffer *framebufferObject = getDrawFramebuffer();
1831
1832 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1833
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001834 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001835 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001836 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001837
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001838 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001839 GLint alwaysFront = !isTriangleMode(drawMode);
1840 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1841
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001842 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001843 bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
1844 // Apparently some ATI cards have a bug where a draw with a zero color
1845 // write mask can cause later draws to have incorrect results. Instead,
1846 // set a nonzero color write mask but modify the blend state so that no
1847 // drawing is done.
1848 // http://code.google.com/p/angleproject/issues/detail?id=169
1849
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001850 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001851 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001852 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001853 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001854 mDevice->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001855 }
1856 else
1857 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001858 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001859 }
1860
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001861 mCullStateDirty = false;
1862 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001863
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001864 if (mDepthStateDirty)
1865 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00001866 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001867 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001868 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1869 mDevice->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001870 }
1871 else
1872 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001873 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001874 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001875
1876 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001877 }
1878
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001879 if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
1880 {
1881 mBlendStateDirty = true;
1882 mMaskStateDirty = true;
1883 }
1884
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001885 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001886 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001887 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001888 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001889 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001890
1891 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1892 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1893 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001894 mDevice->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001895 }
1896 else
1897 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001898 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001899 unorm<8>(mState.blendColor.alpha),
1900 unorm<8>(mState.blendColor.alpha),
1901 unorm<8>(mState.blendColor.alpha)));
1902 }
1903
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001904 mDevice->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1905 mDevice->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1906 mDevice->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001907
1908 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1909 mState.destBlendRGB != mState.destBlendAlpha ||
1910 mState.blendEquationRGB != mState.blendEquationAlpha)
1911 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001912 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001913
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001914 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1915 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1916 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001917 }
1918 else
1919 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001920 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001921 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001922 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001923 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001924 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001925 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001926 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001927
1928 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001929 }
1930
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001931 if (mStencilStateDirty || mFrontFaceDirty)
1932 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001933 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001934 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001935 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1936 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001937
1938 // FIXME: Unsupported by D3D9
1939 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1940 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1941 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1942 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1943 mState.stencilRef != mState.stencilBackRef ||
1944 mState.stencilMask != mState.stencilBackMask)
1945 {
1946 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1947 return error(GL_INVALID_OPERATION);
1948 }
1949
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001950 // get the maximum size of the stencil ref
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001951 gl::Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001952 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1953
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001954 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1955 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001956 es2dx::ConvertComparison(mState.stencilFunc));
1957
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001958 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1959 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001960
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001961 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001962 es2dx::ConvertStencilOp(mState.stencilFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001963 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001964 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001965 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001966 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1967
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001968 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1969 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001970 es2dx::ConvertComparison(mState.stencilBackFunc));
1971
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001972 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1973 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001974
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001975 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001976 es2dx::ConvertStencilOp(mState.stencilBackFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001977 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001978 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001979 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001980 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1981 }
1982 else
1983 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001984 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001985 }
1986
1987 mStencilStateDirty = false;
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001988 mFrontFaceDirty = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001989 }
1990
1991 if (mMaskStateDirty)
1992 {
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001993 int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1994 mState.colorMaskBlue, mState.colorMaskAlpha);
1995 if (colorMask == 0 && !zeroColorMaskAllowed)
1996 {
1997 // Enable green channel, but set blending so nothing will be drawn.
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001998 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
1999 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002000
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002001 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
2002 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
2003 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002004 }
2005 else
2006 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002007 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002008 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002009 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002010
2011 mMaskStateDirty = false;
2012 }
2013
2014 if (mPolygonOffsetStateDirty)
2015 {
2016 if (mState.polygonOffsetFill)
2017 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00002018 gl::Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002019 if (depthbuffer)
2020 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002021 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002022 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002023 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002024 }
2025 }
2026 else
2027 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002028 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
2029 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002030 }
2031
2032 mPolygonOffsetStateDirty = false;
2033 }
2034
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002035 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002036 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002037 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002038 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002039 FIXME("Sample alpha to coverage is unimplemented.");
2040 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002041
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002042 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002043 if (mState.sampleCoverage)
2044 {
2045 unsigned int mask = 0;
2046 if (mState.sampleCoverageValue != 0)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002047 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002048 float threshold = 0.5f;
2049
2050 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002051 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002052 mask <<= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002053
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002054 if ((i + 1) * mState.sampleCoverageValue >= threshold)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002055 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002056 threshold += 1.0f;
2057 mask |= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002058 }
2059 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002060 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002061
2062 if (mState.sampleCoverageInvert)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002063 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002064 mask = ~mask;
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002065 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002066
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002067 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002068 }
2069 else
2070 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002071 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002072 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002073
2074 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002075 }
2076
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002077 if (mDitherStateDirty)
2078 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002079 mDevice->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002080
2081 mDitherStateDirty = false;
2082 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002083}
2084
daniel@transgaming.com83921382011-01-08 05:46:00 +00002085GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002086{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002087 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002088
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002089 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002090 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002091 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002092 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002093 }
2094
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002095 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, getCurrentProgram());
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002096}
2097
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002098// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002099GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002100{
daniel@transgaming.com83921382011-01-08 05:46:00 +00002101 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002102
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002103 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002104 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002105 if (indexInfo->serial != mAppliedIBSerial)
2106 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002107 mDevice->SetIndices(indexInfo->indexBuffer);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002108 mAppliedIBSerial = indexInfo->serial;
2109 }
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002110 }
2111
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002112 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002113}
2114
2115// Applies the shaders and shader constants to the Direct3D 9 device
2116void Context::applyShaders()
2117{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002118 Program *programObject = getCurrentProgram();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002119 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002120 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002121 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2122 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2123
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002124 mDevice->SetPixelShader(pixelShader);
2125 mDevice->SetVertexShader(vertexShader);
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002126 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002127 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002128 }
2129
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002130 programObject->applyUniforms();
2131}
2132
2133// Applies the textures and sampler states to the Direct3D 9 device
2134void Context::applyTextures()
2135{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002136 applyTextures(SAMPLER_PIXEL);
2137
2138 if (mSupportsVertexTexture)
2139 {
2140 applyTextures(SAMPLER_VERTEX);
2141 }
2142}
2143
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002144// For each Direct3D 9 sampler of either the pixel or vertex stage,
2145// looks up the corresponding OpenGL texture image unit and texture type,
2146// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002147void Context::applyTextures(SamplerType type)
2148{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002149 Program *programObject = getCurrentProgram();
2150
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002151 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 +00002152 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2153 int d3dSamplerOffset = (type == SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002154 int samplerRange = programObject->getUsedSamplerRange(type);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002155
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002156 for (int samplerIndex = 0; samplerIndex < samplerRange; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002157 {
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002158 int textureUnit = programObject->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00002159 int d3dSampler = samplerIndex + d3dSamplerOffset;
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002160
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002161 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002162 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002163 TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002164
2165 Texture *texture = getSamplerTexture(textureUnit, textureType);
2166
daniel@transgaming.comfbc39522011-11-11 04:10:28 +00002167 if (appliedTextureSerial[samplerIndex] != texture->getTextureSerial() || texture->hasDirtyParameters() || texture->hasDirtyImages())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002168 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002169 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2170
2171 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002172 {
daniel@transgaming.comfbc39522011-11-11 04:10:28 +00002173 if (appliedTextureSerial[samplerIndex] != texture->getTextureSerial() || texture->hasDirtyParameters())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002174 {
2175 GLenum wrapS = texture->getWrapS();
2176 GLenum wrapT = texture->getWrapT();
2177 GLenum minFilter = texture->getMinFilter();
2178 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002179
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002180 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2181 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002182
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002183 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002184 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2185 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002186 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2187 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002188 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002189
daniel@transgaming.comfbc39522011-11-11 04:10:28 +00002190 if (appliedTextureSerial[samplerIndex] != texture->getTextureSerial() || texture->hasDirtyImages())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002191 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002192 mDevice->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002193 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002194 }
2195 else
2196 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002197 mDevice->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002198 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002199
daniel@transgaming.comfbc39522011-11-11 04:10:28 +00002200 appliedTextureSerial[samplerIndex] = texture->getTextureSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002201 texture->resetDirty();
2202 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002203 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002204 else
2205 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002206 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002207 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002208 mDevice->SetTexture(d3dSampler, NULL);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002209 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002210 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002211 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002212 }
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002213
2214 for (int samplerIndex = samplerRange; samplerIndex < samplerCount; samplerIndex++)
2215 {
2216 if (appliedTextureSerial[samplerIndex] != 0)
2217 {
daniel@transgaming.comc5a7b692011-10-26 02:45:44 +00002218 mDevice->SetTexture(samplerIndex + d3dSamplerOffset, NULL);
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002219 appliedTextureSerial[samplerIndex] = 0;
2220 }
2221 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002222}
2223
daniel@transgaming.comb7915a52011-11-12 03:14:20 +00002224void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height,
2225 GLenum format, GLenum type, GLsizei *bufSize, void* pixels)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002226{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002227 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002228
2229 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2230 {
2231 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2232 }
2233
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002234 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2235 {
2236 return error(GL_INVALID_OPERATION);
2237 }
2238
daniel@transgaming.comb7915a52011-11-12 03:14:20 +00002239 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
2240 // sized query sanity check
2241 if (bufSize)
2242 {
2243 int requiredSize = outputPitch * height;
2244 if (requiredSize > *bufSize)
2245 {
2246 return error(GL_INVALID_OPERATION);
2247 }
2248 }
2249
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002250 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002251
2252 if (!renderTarget)
2253 {
2254 return; // Context must be lost, return silently
2255 }
2256
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002257 D3DSURFACE_DESC desc;
2258 renderTarget->GetDesc(&desc);
2259
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002260 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2261 {
2262 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002263 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002264 }
2265
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002266 HRESULT result;
2267 IDirect3DSurface9 *systemSurface = NULL;
2268 bool directToPixels = getPackReverseRowOrder() && getPackAlignment() <= 4 && mDisplay->isD3d9ExDevice() &&
2269 x == 0 && y == 0 && width == desc.Width && height == desc.Height &&
2270 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2271 if (directToPixels)
2272 {
2273 // Use the pixels ptr as a shared handle to write directly into client's memory
2274 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2275 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2276 if (FAILED(result))
2277 {
2278 // Try again without the shared handle
2279 directToPixels = false;
2280 }
2281 }
2282
2283 if (!directToPixels)
2284 {
2285 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2286 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2287 if (FAILED(result))
2288 {
2289 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2290 return error(GL_OUT_OF_MEMORY);
2291 }
2292 }
2293
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002294 result = mDevice->GetRenderTargetData(renderTarget, systemSurface);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002295
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002296 if (FAILED(result))
2297 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002298 systemSurface->Release();
2299
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002300 // It turns out that D3D will sometimes produce more error
2301 // codes than those documented.
2302 if (checkDeviceLost(result))
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002303 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002304 else
2305 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002306 UNREACHABLE();
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002307 return;
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002308 }
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002309
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002310 }
2311
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002312 if (directToPixels)
2313 {
2314 systemSurface->Release();
2315 return;
2316 }
2317
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002318 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002319 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2320 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2321 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2322 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2323 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002324
2325 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2326
2327 if (FAILED(result))
2328 {
2329 UNREACHABLE();
2330 systemSurface->Release();
2331
2332 return; // No sensible error to generate
2333 }
2334
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002335 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002336 unsigned short *dest16 = (unsigned short*)pixels;
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002337
2338 unsigned char *source;
2339 int inputPitch;
2340 if (getPackReverseRowOrder())
2341 {
2342 source = (unsigned char*)lock.pBits;
2343 inputPitch = lock.Pitch;
2344 }
2345 else
2346 {
2347 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2348 inputPitch = -lock.Pitch;
2349 }
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002350
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002351 for (int j = 0; j < rect.bottom - rect.top; j++)
2352 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002353 if (desc.Format == D3DFMT_A8R8G8B8 &&
2354 format == GL_BGRA_EXT &&
2355 type == GL_UNSIGNED_BYTE)
2356 {
2357 // Fast path for EXT_read_format_bgra, given
2358 // an RGBA source buffer. Note that buffers with no
2359 // alpha go through the slow path below.
2360 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002361 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002362 (rect.right - rect.left) * 4);
2363 continue;
2364 }
2365
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002366 for (int i = 0; i < rect.right - rect.left; i++)
2367 {
2368 float r;
2369 float g;
2370 float b;
2371 float a;
2372
2373 switch (desc.Format)
2374 {
2375 case D3DFMT_R5G6B5:
2376 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002377 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002378
2379 a = 1.0f;
2380 b = (rgb & 0x001F) * (1.0f / 0x001F);
2381 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2382 r = (rgb & 0xF800) * (1.0f / 0xF800);
2383 }
2384 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002385 case D3DFMT_A1R5G5B5:
2386 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002387 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002388
2389 a = (argb & 0x8000) ? 1.0f : 0.0f;
2390 b = (argb & 0x001F) * (1.0f / 0x001F);
2391 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2392 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2393 }
2394 break;
2395 case D3DFMT_A8R8G8B8:
2396 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002397 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002398
2399 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2400 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2401 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2402 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2403 }
2404 break;
2405 case D3DFMT_X8R8G8B8:
2406 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002407 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002408
2409 a = 1.0f;
2410 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2411 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2412 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2413 }
2414 break;
2415 case D3DFMT_A2R10G10B10:
2416 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002417 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002418
2419 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2420 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2421 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2422 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2423 }
2424 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002425 case D3DFMT_A32B32G32R32F:
2426 {
2427 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002428 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2429 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2430 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2431 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002432 }
2433 break;
2434 case D3DFMT_A16B16G16R16F:
2435 {
2436 // float formats in D3D are stored rgba, rather than the other way round
2437 float abgr[4];
2438
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002439 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002440
2441 a = abgr[3];
2442 b = abgr[2];
2443 g = abgr[1];
2444 r = abgr[0];
2445 }
2446 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002447 default:
2448 UNIMPLEMENTED(); // FIXME
2449 UNREACHABLE();
2450 }
2451
2452 switch (format)
2453 {
2454 case GL_RGBA:
2455 switch (type)
2456 {
2457 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002458 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2459 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2460 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2461 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002462 break;
2463 default: UNREACHABLE();
2464 }
2465 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002466 case GL_BGRA_EXT:
2467 switch (type)
2468 {
2469 case GL_UNSIGNED_BYTE:
2470 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2471 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2472 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2473 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2474 break;
2475 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2476 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2477 // this type is packed as follows:
2478 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2479 // --------------------------------------------------------------------------------
2480 // | 4th | 3rd | 2nd | 1st component |
2481 // --------------------------------------------------------------------------------
2482 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2483 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2484 ((unsigned short)(15 * a + 0.5f) << 12)|
2485 ((unsigned short)(15 * r + 0.5f) << 8) |
2486 ((unsigned short)(15 * g + 0.5f) << 4) |
2487 ((unsigned short)(15 * b + 0.5f) << 0);
2488 break;
2489 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2490 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2491 // this type is packed as follows:
2492 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2493 // --------------------------------------------------------------------------------
2494 // | 4th | 3rd | 2nd | 1st component |
2495 // --------------------------------------------------------------------------------
2496 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2497 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2498 ((unsigned short)( a + 0.5f) << 15) |
2499 ((unsigned short)(31 * r + 0.5f) << 10) |
2500 ((unsigned short)(31 * g + 0.5f) << 5) |
2501 ((unsigned short)(31 * b + 0.5f) << 0);
2502 break;
2503 default: UNREACHABLE();
2504 }
2505 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002506 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002507 switch (type)
2508 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002509 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002510 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2511 ((unsigned short)(31 * b + 0.5f) << 0) |
2512 ((unsigned short)(63 * g + 0.5f) << 5) |
2513 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002514 break;
2515 default: UNREACHABLE();
2516 }
2517 break;
2518 default: UNREACHABLE();
2519 }
2520 }
2521 }
2522
2523 systemSurface->UnlockRect();
2524
2525 systemSurface->Release();
2526}
2527
2528void Context::clear(GLbitfield mask)
2529{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002530 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002531
2532 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2533 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002534 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002535 }
2536
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002537 DWORD flags = 0;
2538
2539 if (mask & GL_COLOR_BUFFER_BIT)
2540 {
2541 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002542
2543 if (framebufferObject->getColorbufferType() != GL_NONE)
2544 {
2545 flags |= D3DCLEAR_TARGET;
2546 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002547 }
2548
2549 if (mask & GL_DEPTH_BUFFER_BIT)
2550 {
2551 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002552 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002553 {
2554 flags |= D3DCLEAR_ZBUFFER;
2555 }
2556 }
2557
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002558 GLuint stencilUnmasked = 0x0;
2559
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002560 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002561 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002562 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002563 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002564 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002565 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002566 if (!depthStencil)
2567 {
2568 ERR("Depth stencil pointer unexpectedly null.");
2569 return;
2570 }
2571
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002572 D3DSURFACE_DESC desc;
2573 depthStencil->GetDesc(&desc);
2574
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002575 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002576 stencilUnmasked = (0x1 << stencilSize) - 1;
2577
2578 if (stencilUnmasked != 0x0)
2579 {
2580 flags |= D3DCLEAR_STENCIL;
2581 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002582 }
2583 }
2584
2585 if (mask != 0)
2586 {
2587 return error(GL_INVALID_VALUE);
2588 }
2589
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002590 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2591 {
2592 return;
2593 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002594
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002595 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002596 unorm<8>(mState.colorClearValue.red),
2597 unorm<8>(mState.colorClearValue.green),
2598 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002599 float depth = clamp01(mState.depthClearValue);
2600 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002601
2602 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2603
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002604 if (!renderTarget)
2605 {
2606 return; // Context must be lost, return silently
2607 }
2608
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002609 D3DSURFACE_DESC desc;
2610 renderTarget->GetDesc(&desc);
2611
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002612 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002613
2614 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002615 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002616 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002617 !(mState.colorMaskRed && mState.colorMaskGreen &&
2618 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002619
2620 if (needMaskedColorClear || needMaskedStencilClear)
2621 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002622 // State which is altered in all paths from this point to the clear call is saved.
2623 // State which is altered in only some paths will be flagged dirty in the case that
2624 // that path is taken.
2625 HRESULT hr;
2626 if (mMaskedClearSavedState == NULL)
2627 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002628 hr = mDevice->BeginStateBlock();
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002629 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2630
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002631 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2632 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2633 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2634 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2635 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2636 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2637 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2638 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2639 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2640 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2641 mDevice->SetPixelShader(NULL);
2642 mDevice->SetVertexShader(NULL);
2643 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2644 mDevice->SetStreamSource(0, NULL, 0, 0);
2645 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2646 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2647 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2648 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2649 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2650 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2651 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002652
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002653 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002654 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2655 }
2656
2657 ASSERT(mMaskedClearSavedState != NULL);
2658
2659 if (mMaskedClearSavedState != NULL)
2660 {
2661 hr = mMaskedClearSavedState->Capture();
2662 ASSERT(SUCCEEDED(hr));
2663 }
2664
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002665 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2666 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2667 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2668 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2669 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2670 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2671 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2672 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002673
2674 if (flags & D3DCLEAR_TARGET)
2675 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002676 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002677 }
2678 else
2679 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002680 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002681 }
2682
2683 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2684 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002685 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2686 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2687 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2688 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
2689 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
2690 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
2691 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2692 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002693 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002694 }
2695 else
2696 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002697 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002698 }
2699
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002700 mDevice->SetPixelShader(NULL);
2701 mDevice->SetVertexShader(NULL);
2702 mDevice->SetFVF(D3DFVF_XYZRHW);
2703 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2704 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2705 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2706 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2707 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2708 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2709 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002710
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002711 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2712 quad[0][0] = -0.5f;
2713 quad[0][1] = desc.Height - 0.5f;
2714 quad[0][2] = 0.0f;
2715 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002716
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002717 quad[1][0] = desc.Width - 0.5f;
2718 quad[1][1] = desc.Height - 0.5f;
2719 quad[1][2] = 0.0f;
2720 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002721
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002722 quad[2][0] = -0.5f;
2723 quad[2][1] = -0.5f;
2724 quad[2][2] = 0.0f;
2725 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002726
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002727 quad[3][0] = desc.Width - 0.5f;
2728 quad[3][1] = -0.5f;
2729 quad[3][2] = 0.0f;
2730 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002731
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002732 mDisplay->startScene();
2733 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002734
2735 if (flags & D3DCLEAR_ZBUFFER)
2736 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002737 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
2738 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2739 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002740 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002741
2742 if (mMaskedClearSavedState != NULL)
2743 {
2744 mMaskedClearSavedState->Apply();
2745 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002746 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002747 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002748 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002749 mDevice->Clear(0, NULL, flags, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002750 }
2751}
2752
2753void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2754{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002755 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002756 {
2757 return error(GL_INVALID_OPERATION);
2758 }
2759
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002760 D3DPRIMITIVETYPE primitiveType;
2761 int primitiveCount;
2762
2763 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2764 return error(GL_INVALID_ENUM);
2765
2766 if (primitiveCount <= 0)
2767 {
2768 return;
2769 }
2770
2771 if (!applyRenderTarget(false))
2772 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002773 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002774 }
2775
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002776 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002777
daniel@transgaming.com83921382011-01-08 05:46:00 +00002778 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002779 if (err != GL_NO_ERROR)
2780 {
2781 return error(err);
2782 }
2783
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002784 applyShaders();
2785 applyTextures();
2786
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002787 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002788 {
2789 return error(GL_INVALID_OPERATION);
2790 }
2791
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002792 if (!cullSkipsDraw(mode))
2793 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002794 mDisplay->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002795
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002796 mDevice->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002797
2798 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2799 {
daniel@transgaming.com102ca742011-11-24 22:34:14 +00002800 drawClosingLine(0, count - 1, 0);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002801 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002802 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002803}
2804
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002805void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002806{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002807 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002808 {
2809 return error(GL_INVALID_OPERATION);
2810 }
2811
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002812 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002813 {
2814 return error(GL_INVALID_OPERATION);
2815 }
2816
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002817 D3DPRIMITIVETYPE primitiveType;
2818 int primitiveCount;
2819
2820 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2821 return error(GL_INVALID_ENUM);
2822
2823 if (primitiveCount <= 0)
2824 {
2825 return;
2826 }
2827
2828 if (!applyRenderTarget(false))
2829 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002830 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002831 }
2832
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002833 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002834
2835 TranslatedIndexData indexInfo;
2836 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2837 if (err != GL_NO_ERROR)
2838 {
2839 return error(err);
2840 }
2841
daniel@transgaming.com83921382011-01-08 05:46:00 +00002842 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2843 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002844 if (err != GL_NO_ERROR)
2845 {
2846 return error(err);
2847 }
2848
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002849 applyShaders();
2850 applyTextures();
2851
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002852 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002853 {
2854 return error(GL_INVALID_OPERATION);
2855 }
2856
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002857 if (!cullSkipsDraw(mode))
2858 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002859 mDisplay->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002860
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002861 mDevice->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002862
2863 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2864 {
daniel@transgaming.com102ca742011-11-24 22:34:14 +00002865 drawClosingLine(count, type, indices, indexInfo.minIndex);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002866 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002867 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002868}
2869
daniel@transgaming.com0d86aa72011-10-26 02:35:10 +00002870// Implements glFlush when block is false, glFinish when block is true
2871void Context::sync(bool block)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002872{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002873 IDirect3DQuery9 *eventQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002874 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002875
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002876 result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002877 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002878 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002879 ERR("CreateQuery failed hr=%x\n", result);
2880 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2881 {
2882 return error(GL_OUT_OF_MEMORY);
2883 }
2884 ASSERT(false);
2885 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002886 }
2887
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002888 result = eventQuery->Issue(D3DISSUE_END);
2889 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002890 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002891 ERR("eventQuery->Issue(END) failed hr=%x\n", result);
2892 ASSERT(false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002893 eventQuery->Release();
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002894 return;
2895 }
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002896
daniel@transgaming.com0d86aa72011-10-26 02:35:10 +00002897 do
2898 {
2899 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
2900
2901 if(block && result == S_FALSE)
2902 {
2903 // Keep polling, but allow other threads to do something useful first
2904 Sleep(0);
2905 }
2906 }
2907 while(block && result == S_FALSE);
2908
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002909 eventQuery->Release();
2910
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002911 if (checkDeviceLost(result))
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002912 {
2913 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002914 }
2915}
2916
daniel@transgaming.com102ca742011-11-24 22:34:14 +00002917void Context::drawClosingLine(unsigned int first, unsigned int last, int minIndex)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002918{
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002919 IDirect3DIndexBuffer9 *indexBuffer = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002920 bool succeeded = false;
2921 UINT offset;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002922
2923 if (supports32bitIndices())
2924 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002925 const int spaceNeeded = 2 * sizeof(unsigned int);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002926
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002927 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002928 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002929 mClosingIB = new StreamingIndexBuffer(mDevice, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002930 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002931
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002932 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
2933
2934 unsigned int *data = static_cast<unsigned int*>(mClosingIB->map(spaceNeeded, &offset));
2935 if (data)
2936 {
2937 data[0] = last;
2938 data[1] = first;
2939 mClosingIB->unmap();
2940 offset /= 4;
2941 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002942 }
2943 }
2944 else
2945 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002946 const int spaceNeeded = 2 * sizeof(unsigned short);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002947
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002948 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002949 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002950 mClosingIB = new StreamingIndexBuffer(mDevice, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002951 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002952
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002953 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
2954
2955 unsigned short *data = static_cast<unsigned short*>(mClosingIB->map(spaceNeeded, &offset));
2956 if (data)
2957 {
2958 data[0] = last;
2959 data[1] = first;
2960 mClosingIB->unmap();
2961 offset /= 2;
2962 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002963 }
2964 }
2965
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002966 if (succeeded)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002967 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002968 mDevice->SetIndices(mClosingIB->getBuffer());
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002969 mAppliedIBSerial = mClosingIB->getSerial();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002970
daniel@transgaming.com102ca742011-11-24 22:34:14 +00002971 mDevice->DrawIndexedPrimitive(D3DPT_LINELIST, -minIndex, minIndex, last, offset, 1);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002972 }
2973 else
2974 {
2975 ERR("Could not create an index buffer for closing a line loop.");
2976 error(GL_OUT_OF_MEMORY);
2977 }
2978}
2979
daniel@transgaming.com102ca742011-11-24 22:34:14 +00002980void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices, int minIndex)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002981{
2982 unsigned int first = 0;
2983 unsigned int last = 0;
2984
2985 if (mState.elementArrayBuffer.get())
2986 {
2987 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2988 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2989 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2990 }
2991
2992 switch (type)
2993 {
2994 case GL_UNSIGNED_BYTE:
2995 first = static_cast<const GLubyte*>(indices)[0];
2996 last = static_cast<const GLubyte*>(indices)[count - 1];
2997 break;
2998 case GL_UNSIGNED_SHORT:
2999 first = static_cast<const GLushort*>(indices)[0];
3000 last = static_cast<const GLushort*>(indices)[count - 1];
3001 break;
3002 case GL_UNSIGNED_INT:
3003 first = static_cast<const GLuint*>(indices)[0];
3004 last = static_cast<const GLuint*>(indices)[count - 1];
3005 break;
3006 default: UNREACHABLE();
3007 }
3008
daniel@transgaming.com102ca742011-11-24 22:34:14 +00003009 drawClosingLine(first, last, minIndex);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003010}
3011
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003012void Context::recordInvalidEnum()
3013{
3014 mInvalidEnum = true;
3015}
3016
3017void Context::recordInvalidValue()
3018{
3019 mInvalidValue = true;
3020}
3021
3022void Context::recordInvalidOperation()
3023{
3024 mInvalidOperation = true;
3025}
3026
3027void Context::recordOutOfMemory()
3028{
3029 mOutOfMemory = true;
3030}
3031
3032void Context::recordInvalidFramebufferOperation()
3033{
3034 mInvalidFramebufferOperation = true;
3035}
3036
3037// Get one of the recorded errors and clear its flag, if any.
3038// [OpenGL ES 2.0.24] section 2.5 page 13.
3039GLenum Context::getError()
3040{
3041 if (mInvalidEnum)
3042 {
3043 mInvalidEnum = false;
3044
3045 return GL_INVALID_ENUM;
3046 }
3047
3048 if (mInvalidValue)
3049 {
3050 mInvalidValue = false;
3051
3052 return GL_INVALID_VALUE;
3053 }
3054
3055 if (mInvalidOperation)
3056 {
3057 mInvalidOperation = false;
3058
3059 return GL_INVALID_OPERATION;
3060 }
3061
3062 if (mOutOfMemory)
3063 {
3064 mOutOfMemory = false;
3065
3066 return GL_OUT_OF_MEMORY;
3067 }
3068
3069 if (mInvalidFramebufferOperation)
3070 {
3071 mInvalidFramebufferOperation = false;
3072
3073 return GL_INVALID_FRAMEBUFFER_OPERATION;
3074 }
3075
3076 return GL_NO_ERROR;
3077}
3078
daniel@transgaming.com17f548c2011-11-09 17:47:02 +00003079GLenum Context::getResetStatus()
3080{
3081 if (mResetStatus == GL_NO_ERROR)
3082 {
3083 bool lost = mDisplay->testDeviceLost();
3084
3085 if (lost)
3086 {
3087 mDisplay->notifyDeviceLost(); // Sets mResetStatus
3088 }
3089 }
3090
3091 GLenum status = mResetStatus;
3092
3093 if (mResetStatus != GL_NO_ERROR)
3094 {
3095 if (mDisplay->testDeviceResettable())
3096 {
3097 mResetStatus = GL_NO_ERROR;
3098 }
3099 }
3100
3101 return status;
3102}
3103
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00003104bool Context::isResetNotificationEnabled()
3105{
3106 return (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT);
3107}
3108
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003109bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003110{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003111 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003112}
3113
daniel@transgaming.com396c6432010-11-26 16:26:12 +00003114int Context::getMaximumVaryingVectors() const
3115{
3116 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3117}
3118
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003119unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003120{
3121 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3122}
3123
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003124unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003125{
3126 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3127}
3128
daniel@transgaming.com458da142010-11-28 02:03:02 +00003129int Context::getMaximumFragmentUniformVectors() const
3130{
3131 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3132}
3133
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003134int Context::getMaxSupportedSamples() const
3135{
3136 return mMaxSupportedSamples;
3137}
3138
3139int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3140{
3141 if (requested == 0)
3142 {
3143 return requested;
3144 }
3145
3146 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3147 if (itr == mMultiSampleSupport.end())
3148 {
3149 return -1;
3150 }
3151
3152 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3153 {
3154 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3155 {
3156 return i;
3157 }
3158 }
3159
3160 return -1;
3161}
3162
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003163bool Context::supportsEventQueries() const
3164{
3165 return mSupportsEventQueries;
3166}
3167
gman@chromium.org50c526d2011-08-10 05:19:44 +00003168bool Context::supportsDXT1Textures() const
daniel@transgaming.com01868132010-08-24 19:21:17 +00003169{
gman@chromium.org50c526d2011-08-10 05:19:44 +00003170 return mSupportsDXT1Textures;
3171}
3172
3173bool Context::supportsDXT3Textures() const
3174{
3175 return mSupportsDXT3Textures;
3176}
3177
3178bool Context::supportsDXT5Textures() const
3179{
3180 return mSupportsDXT5Textures;
daniel@transgaming.com01868132010-08-24 19:21:17 +00003181}
3182
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003183bool Context::supportsFloat32Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003184{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003185 return mSupportsFloat32Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003186}
3187
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003188bool Context::supportsFloat32LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003189{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003190 return mSupportsFloat32LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003191}
3192
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003193bool Context::supportsFloat32RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003194{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003195 return mSupportsFloat32RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003196}
3197
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003198bool Context::supportsFloat16Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003199{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003200 return mSupportsFloat16Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003201}
3202
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003203bool Context::supportsFloat16LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003204{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003205 return mSupportsFloat16LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003206}
3207
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003208bool Context::supportsFloat16RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003209{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003210 return mSupportsFloat16RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003211}
3212
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003213int Context::getMaximumRenderbufferDimension() const
3214{
3215 return mMaxRenderbufferDimension;
3216}
3217
3218int Context::getMaximumTextureDimension() const
3219{
3220 return mMaxTextureDimension;
3221}
3222
3223int Context::getMaximumCubeTextureDimension() const
3224{
3225 return mMaxCubeTextureDimension;
3226}
3227
3228int Context::getMaximumTextureLevel() const
3229{
3230 return mMaxTextureLevel;
3231}
3232
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003233bool Context::supportsLuminanceTextures() const
3234{
3235 return mSupportsLuminanceTextures;
3236}
3237
3238bool Context::supportsLuminanceAlphaTextures() const
3239{
3240 return mSupportsLuminanceAlphaTextures;
3241}
3242
daniel@transgaming.com83921382011-01-08 05:46:00 +00003243bool Context::supports32bitIndices() const
3244{
3245 return mSupports32bitIndices;
3246}
3247
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003248bool Context::supportsNonPower2Texture() const
3249{
3250 return mSupportsNonPower2Texture;
3251}
3252
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003253void Context::detachBuffer(GLuint buffer)
3254{
3255 // [OpenGL ES 2.0.24] section 2.9 page 22:
3256 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3257 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3258
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003259 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003260 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003261 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003262 }
3263
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003264 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003265 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003266 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003267 }
3268
3269 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3270 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003271 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003272 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003273 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003274 }
3275 }
3276}
3277
3278void Context::detachTexture(GLuint texture)
3279{
3280 // [OpenGL ES 2.0.24] section 3.8 page 84:
3281 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3282 // rebound to texture object zero
3283
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003284 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003285 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003286 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003287 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003288 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003289 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003290 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003291 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003292 }
3293 }
3294
3295 // [OpenGL ES 2.0.24] section 4.4 page 112:
3296 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3297 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3298 // image was attached in the currently bound framebuffer.
3299
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003300 Framebuffer *readFramebuffer = getReadFramebuffer();
3301 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003302
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003303 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003304 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003305 readFramebuffer->detachTexture(texture);
3306 }
3307
3308 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3309 {
3310 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003311 }
3312}
3313
3314void Context::detachFramebuffer(GLuint framebuffer)
3315{
3316 // [OpenGL ES 2.0.24] section 4.4 page 107:
3317 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3318 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3319
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003320 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003321 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003322 bindReadFramebuffer(0);
3323 }
3324
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003325 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003326 {
3327 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003328 }
3329}
3330
3331void Context::detachRenderbuffer(GLuint renderbuffer)
3332{
3333 // [OpenGL ES 2.0.24] section 4.4 page 109:
3334 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3335 // had been executed with the target RENDERBUFFER and name of zero.
3336
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003337 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003338 {
3339 bindRenderbuffer(0);
3340 }
3341
3342 // [OpenGL ES 2.0.24] section 4.4 page 111:
3343 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3344 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3345 // point to which this image was attached in the currently bound framebuffer.
3346
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003347 Framebuffer *readFramebuffer = getReadFramebuffer();
3348 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003349
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003350 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003351 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003352 readFramebuffer->detachRenderbuffer(renderbuffer);
3353 }
3354
3355 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3356 {
3357 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003358 }
3359}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003360
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003361Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003362{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003363 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003364
3365 if (t == NULL)
3366 {
3367 static const GLubyte color[] = { 0, 0, 0, 255 };
3368
3369 switch (type)
3370 {
3371 default:
3372 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003373 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003374
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003375 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003376 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003377 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003378 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003379 t = incomplete2d;
3380 }
3381 break;
3382
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003383 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003384 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003385 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003386
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003387 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3388 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3389 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3390 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3391 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3392 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003393
3394 t = incompleteCube;
3395 }
3396 break;
3397 }
3398
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003399 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003400 }
3401
3402 return t;
3403}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003404
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003405bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003406{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003407 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003408}
3409
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003410bool Context::isTriangleMode(GLenum drawMode)
3411{
3412 switch (drawMode)
3413 {
3414 case GL_TRIANGLES:
3415 case GL_TRIANGLE_FAN:
3416 case GL_TRIANGLE_STRIP:
3417 return true;
3418 case GL_POINTS:
3419 case GL_LINES:
3420 case GL_LINE_LOOP:
3421 case GL_LINE_STRIP:
3422 return false;
3423 default: UNREACHABLE();
3424 }
3425
3426 return false;
3427}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003428
3429void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3430{
3431 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3432
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003433 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3434 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3435 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3436 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003437
daniel@transgaming.com83921382011-01-08 05:46:00 +00003438 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003439}
3440
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003441// keep list sorted in following order
3442// OES extensions
3443// EXT extensions
3444// Vendor extensions
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003445void Context::initExtensionString()
3446{
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003447 mExtensionString = "";
3448
3449 // OES extensions
3450 if (supports32bitIndices())
3451 {
3452 mExtensionString += "GL_OES_element_index_uint ";
3453 }
3454
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003455 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003456 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003457 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003458
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003459 if (supportsFloat16Textures())
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003460 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003461 mExtensionString += "GL_OES_texture_half_float ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003462 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003463 if (supportsFloat16LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003464 {
3465 mExtensionString += "GL_OES_texture_half_float_linear ";
3466 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003467 if (supportsFloat32Textures())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003468 {
3469 mExtensionString += "GL_OES_texture_float ";
3470 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003471 if (supportsFloat32LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003472 {
3473 mExtensionString += "GL_OES_texture_float_linear ";
3474 }
3475
3476 if (supportsNonPower2Texture())
3477 {
3478 mExtensionString += "GL_OES_texture_npot ";
3479 }
3480
3481 // Multi-vendor (EXT) extensions
3482 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com8747f182011-11-09 17:50:38 +00003483 mExtensionString += "GL_EXT_robustness ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003484
gman@chromium.org50c526d2011-08-10 05:19:44 +00003485 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00003486 {
3487 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3488 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003489
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003490 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
daniel@transgaming.com0bd1f2f2011-11-11 04:19:03 +00003491 mExtensionString += "GL_EXT_texture_storage ";
gman@chromium.org50c526d2011-08-10 05:19:44 +00003492
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003493 // ANGLE-specific extensions
3494 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003495 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003496 {
3497 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3498 }
3499
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00003500 mExtensionString += "GL_ANGLE_pack_reverse_row_order ";
3501
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003502 if (supportsDXT3Textures())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003503 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003504 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3505 }
3506 if (supportsDXT5Textures())
3507 {
3508 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003509 }
daniel@transgaming.com97412f72011-11-11 04:19:07 +00003510
3511 mExtensionString += "GL_ANGLE_texture_usage ";
zmo@google.coma574f782011-10-03 21:45:23 +00003512 mExtensionString += "GL_ANGLE_translated_shader_source ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003513
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003514 // Other vendor-specific extensions
3515 if (supportsEventQueries())
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003516 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003517 mExtensionString += "GL_NV_fence ";
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003518 }
3519
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003520 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3521 if (end != std::string::npos)
3522 {
3523 mExtensionString.resize(end+1);
3524 }
3525}
3526
3527const char *Context::getExtensionString() const
3528{
3529 return mExtensionString.c_str();
3530}
3531
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003532void Context::initRendererString()
3533{
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003534 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003535
3536 mRendererString = "ANGLE (";
3537 mRendererString += identifier->Description;
3538 mRendererString += ")";
3539}
3540
3541const char *Context::getRendererString() const
3542{
3543 return mRendererString.c_str();
3544}
3545
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003546void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3547 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3548 GLbitfield mask)
3549{
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003550 Framebuffer *readFramebuffer = getReadFramebuffer();
3551 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3552
3553 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3554 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3555 {
3556 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3557 }
3558
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003559 if (drawFramebuffer->getSamples() != 0)
3560 {
3561 return error(GL_INVALID_OPERATION);
3562 }
3563
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003564 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3565 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3566 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3567 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3568
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003569 RECT sourceRect;
3570 RECT destRect;
3571
3572 if (srcX0 < srcX1)
3573 {
3574 sourceRect.left = srcX0;
3575 sourceRect.right = srcX1;
3576 destRect.left = dstX0;
3577 destRect.right = dstX1;
3578 }
3579 else
3580 {
3581 sourceRect.left = srcX1;
3582 destRect.left = dstX1;
3583 sourceRect.right = srcX0;
3584 destRect.right = dstX0;
3585 }
3586
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003587 if (srcY0 < srcY1)
3588 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003589 sourceRect.top = readBufferHeight - srcY1;
3590 destRect.top = drawBufferHeight - dstY1;
3591 sourceRect.bottom = readBufferHeight - srcY0;
3592 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003593 }
3594 else
3595 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003596 sourceRect.top = readBufferHeight - srcY0;
3597 destRect.top = drawBufferHeight - dstY0;
3598 sourceRect.bottom = readBufferHeight - srcY1;
3599 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003600 }
3601
3602 RECT sourceScissoredRect = sourceRect;
3603 RECT destScissoredRect = destRect;
3604
3605 if (mState.scissorTest)
3606 {
3607 // Only write to parts of the destination framebuffer which pass the scissor test
3608 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3609 // rect will be checked against scissorY, rather than the bottom.
3610 if (destRect.left < mState.scissorX)
3611 {
3612 int xDiff = mState.scissorX - destRect.left;
3613 destScissoredRect.left = mState.scissorX;
3614 sourceScissoredRect.left += xDiff;
3615 }
3616
3617 if (destRect.right > mState.scissorX + mState.scissorWidth)
3618 {
3619 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3620 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3621 sourceScissoredRect.right -= xDiff;
3622 }
3623
3624 if (destRect.top < mState.scissorY)
3625 {
3626 int yDiff = mState.scissorY - destRect.top;
3627 destScissoredRect.top = mState.scissorY;
3628 sourceScissoredRect.top += yDiff;
3629 }
3630
3631 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3632 {
3633 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3634 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3635 sourceScissoredRect.bottom -= yDiff;
3636 }
3637 }
3638
3639 bool blitRenderTarget = false;
3640 bool blitDepthStencil = false;
3641
3642 RECT sourceTrimmedRect = sourceScissoredRect;
3643 RECT destTrimmedRect = destScissoredRect;
3644
3645 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3646 // the actual draw and read surfaces.
3647 if (sourceTrimmedRect.left < 0)
3648 {
3649 int xDiff = 0 - sourceTrimmedRect.left;
3650 sourceTrimmedRect.left = 0;
3651 destTrimmedRect.left += xDiff;
3652 }
3653
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003654 if (sourceTrimmedRect.right > readBufferWidth)
3655 {
3656 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3657 sourceTrimmedRect.right = readBufferWidth;
3658 destTrimmedRect.right -= xDiff;
3659 }
3660
3661 if (sourceTrimmedRect.top < 0)
3662 {
3663 int yDiff = 0 - sourceTrimmedRect.top;
3664 sourceTrimmedRect.top = 0;
3665 destTrimmedRect.top += yDiff;
3666 }
3667
3668 if (sourceTrimmedRect.bottom > readBufferHeight)
3669 {
3670 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3671 sourceTrimmedRect.bottom = readBufferHeight;
3672 destTrimmedRect.bottom -= yDiff;
3673 }
3674
3675 if (destTrimmedRect.left < 0)
3676 {
3677 int xDiff = 0 - destTrimmedRect.left;
3678 destTrimmedRect.left = 0;
3679 sourceTrimmedRect.left += xDiff;
3680 }
3681
3682 if (destTrimmedRect.right > drawBufferWidth)
3683 {
3684 int xDiff = destTrimmedRect.right - drawBufferWidth;
3685 destTrimmedRect.right = drawBufferWidth;
3686 sourceTrimmedRect.right -= xDiff;
3687 }
3688
3689 if (destTrimmedRect.top < 0)
3690 {
3691 int yDiff = 0 - destTrimmedRect.top;
3692 destTrimmedRect.top = 0;
3693 sourceTrimmedRect.top += yDiff;
3694 }
3695
3696 if (destTrimmedRect.bottom > drawBufferHeight)
3697 {
3698 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3699 destTrimmedRect.bottom = drawBufferHeight;
3700 sourceTrimmedRect.bottom -= yDiff;
3701 }
3702
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003703 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003704 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3705 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3706 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3707 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003708 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3709 {
3710 partialBufferCopy = true;
3711 }
3712
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003713 if (mask & GL_COLOR_BUFFER_BIT)
3714 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003715 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3716 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3717 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3718 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3719 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003720 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3721 {
3722 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3723 return error(GL_INVALID_OPERATION);
3724 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003725
3726 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3727 {
3728 return error(GL_INVALID_OPERATION);
3729 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003730
3731 blitRenderTarget = true;
3732
3733 }
3734
3735 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3736 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00003737 Renderbuffer *readDSBuffer = NULL;
3738 Renderbuffer *drawDSBuffer = NULL;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003739
3740 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3741 // both a depth and stencil buffer, it will be the same buffer.
3742
3743 if (mask & GL_DEPTH_BUFFER_BIT)
3744 {
3745 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3746 {
3747 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3748 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3749 {
3750 return error(GL_INVALID_OPERATION);
3751 }
3752
3753 blitDepthStencil = true;
3754 readDSBuffer = readFramebuffer->getDepthbuffer();
3755 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3756 }
3757 }
3758
3759 if (mask & GL_STENCIL_BUFFER_BIT)
3760 {
3761 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3762 {
3763 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3764 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3765 {
3766 return error(GL_INVALID_OPERATION);
3767 }
3768
3769 blitDepthStencil = true;
3770 readDSBuffer = readFramebuffer->getStencilbuffer();
3771 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3772 }
3773 }
3774
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003775 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003776 {
3777 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3778 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3779 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003780
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003781 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3782 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003783 {
3784 return error(GL_INVALID_OPERATION);
3785 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003786 }
3787
3788 if (blitRenderTarget || blitDepthStencil)
3789 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003790 mDisplay->endScene();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003791
3792 if (blitRenderTarget)
3793 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003794 HRESULT result = mDevice->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003795 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3796
3797 if (FAILED(result))
3798 {
3799 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3800 return;
3801 }
3802 }
3803
3804 if (blitDepthStencil)
3805 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003806 HRESULT result = mDevice->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003807
3808 if (FAILED(result))
3809 {
3810 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3811 return;
3812 }
3813 }
3814 }
3815}
3816
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003817VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
3818{
3819 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3820 {
3821 mVertexDeclCache[i].vertexDeclaration = NULL;
3822 mVertexDeclCache[i].lruCount = 0;
3823 }
3824}
3825
3826VertexDeclarationCache::~VertexDeclarationCache()
3827{
3828 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3829 {
3830 if (mVertexDeclCache[i].vertexDeclaration)
3831 {
3832 mVertexDeclCache[i].vertexDeclaration->Release();
3833 }
3834 }
3835}
3836
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003837GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], Program *program)
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003838{
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003839 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
3840 D3DVERTEXELEMENT9 *element = &elements[0];
3841
3842 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3843 {
3844 if (attributes[i].active)
3845 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003846 if (mAppliedVBs[i].serial != attributes[i].serial ||
3847 mAppliedVBs[i].stride != attributes[i].stride ||
3848 mAppliedVBs[i].offset != attributes[i].offset)
3849 {
3850 device->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
3851 mAppliedVBs[i].serial = attributes[i].serial;
3852 mAppliedVBs[i].stride = attributes[i].stride;
3853 mAppliedVBs[i].offset = attributes[i].offset;
3854 }
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003855
3856 element->Stream = i;
3857 element->Offset = 0;
3858 element->Type = attributes[i].type;
3859 element->Method = D3DDECLMETHOD_DEFAULT;
3860 element->Usage = D3DDECLUSAGE_TEXCOORD;
3861 element->UsageIndex = program->getSemanticIndex(i);
3862 element++;
3863 }
3864 }
3865
3866 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
3867 *(element++) = end;
3868
3869 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3870 {
3871 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
3872 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
3873 {
3874 entry->lruCount = ++mMaxLru;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003875 if(entry->vertexDeclaration != mLastSetVDecl)
3876 {
3877 device->SetVertexDeclaration(entry->vertexDeclaration);
3878 mLastSetVDecl = entry->vertexDeclaration;
3879 }
3880
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003881 return GL_NO_ERROR;
3882 }
3883 }
3884
3885 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
3886
3887 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3888 {
3889 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
3890 {
3891 lastCache = &mVertexDeclCache[i];
3892 }
3893 }
3894
3895 if (lastCache->vertexDeclaration != NULL)
3896 {
3897 lastCache->vertexDeclaration->Release();
3898 lastCache->vertexDeclaration = NULL;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003899 // mLastSetVDecl is set to the replacement, so we don't have to worry
3900 // about it.
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003901 }
3902
3903 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
3904 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
3905 device->SetVertexDeclaration(lastCache->vertexDeclaration);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003906 mLastSetVDecl = lastCache->vertexDeclaration;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003907 lastCache->lruCount = ++mMaxLru;
3908
3909 return GL_NO_ERROR;
3910}
3911
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003912void VertexDeclarationCache::markStateDirty()
3913{
3914 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3915 {
3916 mAppliedVBs[i].serial = 0;
3917 }
3918
3919 mLastSetVDecl = NULL;
3920}
3921
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003922}
3923
3924extern "C"
3925{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00003926gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext, bool notifyResets, bool robustAccess)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003927{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00003928 return new gl::Context(config, shareContext, notifyResets, robustAccess);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003929}
3930
3931void glDestroyContext(gl::Context *context)
3932{
3933 delete context;
3934
3935 if (context == gl::getContext())
3936 {
3937 gl::makeCurrent(NULL, NULL, NULL);
3938 }
3939}
3940
3941void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3942{
3943 gl::makeCurrent(context, display, surface);
3944}
3945
3946gl::Context *glGetCurrentContext()
3947{
3948 return gl::getContext();
3949}
3950}