blob: 2bd651f7c61a6d57c3a88ff3307aff4667f362f5 [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;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000150
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000151 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000152 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000153 mBlit = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000154 mClosingIB = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000155
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000156 mInvalidEnum = false;
157 mInvalidValue = false;
158 mInvalidOperation = false;
159 mOutOfMemory = false;
160 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000161
162 mHasBeenCurrent = false;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000163 mContextLost = false;
daniel@transgaming.com17f548c2011-11-09 17:47:02 +0000164 mResetStatus = GL_NO_ERROR;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +0000165 mResetStrategy = (notifyResets ? GL_LOSE_CONTEXT_ON_RESET_EXT : GL_NO_RESET_NOTIFICATION_EXT);
166 mRobustAccess = robustAccess;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000167
gman@chromium.org50c526d2011-08-10 05:19:44 +0000168 mSupportsDXT1Textures = false;
169 mSupportsDXT3Textures = false;
170 mSupportsDXT5Textures = false;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000171 mSupportsEventQueries = false;
gman@chromium.org50c526d2011-08-10 05:19:44 +0000172 mNumCompressedTextureFormats = 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000173 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000174 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000175 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000176}
177
178Context::~Context()
179{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000180 if (mState.currentProgram != 0)
181 {
182 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
183 if (programObject)
184 {
185 programObject->release();
186 }
187 mState.currentProgram = 0;
188 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000189
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000190 while (!mFramebufferMap.empty())
191 {
192 deleteFramebuffer(mFramebufferMap.begin()->first);
193 }
194
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000195 while (!mFenceMap.empty())
196 {
197 deleteFence(mFenceMap.begin()->first);
198 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000199
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000200 while (!mMultiSampleSupport.empty())
201 {
202 delete [] mMultiSampleSupport.begin()->second;
203 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
204 }
205
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000206 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000207 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +0000208 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000209 {
210 mState.samplerTexture[type][sampler].set(NULL);
211 }
212 }
213
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000214 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000215 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000216 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000217 }
218
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000219 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
220 {
221 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
222 }
223
224 mState.arrayBuffer.set(NULL);
225 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000226 mState.renderbuffer.set(NULL);
227
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000228 mTexture2DZero.set(NULL);
229 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000230
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000231 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000232 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000233 delete mBlit;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000234 delete mClosingIB;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000235
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000236 if (mMaskedClearSavedState)
237 {
238 mMaskedClearSavedState->Release();
239 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000240
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000241 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000242}
243
244void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
245{
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000246 mDisplay = display;
247 mDevice = mDisplay->getDevice();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000248
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000249 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000250 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000251 mDeviceCaps = mDisplay->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000252
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000253 mVertexDataManager = new VertexDataManager(this, mDevice);
254 mIndexDataManager = new IndexDataManager(this, mDevice);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000255 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000256
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000257 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000258 mSupportsVertexTexture = mDisplay->getVertexTextureSupport();
259 mSupportsNonPower2Texture = mDisplay->getNonPower2TextureSupport();
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000260
261 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
262 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
263 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
264 mMaxRenderbufferDimension = mMaxTextureDimension;
265 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
266 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
267 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
268
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000269 const D3DFORMAT renderBufferFormats[] =
270 {
271 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000272 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000273 D3DFMT_R5G6B5,
274 D3DFMT_D24S8
275 };
276
277 int max = 0;
278 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
279 {
280 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000281 mDisplay->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000282 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
283
284 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
285 {
286 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
287 {
288 max = j;
289 }
290 }
291 }
292
293 mMaxSupportedSamples = max;
294
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000295 mSupportsEventQueries = mDisplay->getEventQuerySupport();
296 mSupportsDXT1Textures = mDisplay->getDXT1TextureSupport();
297 mSupportsDXT3Textures = mDisplay->getDXT3TextureSupport();
298 mSupportsDXT5Textures = mDisplay->getDXT5TextureSupport();
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +0000299 mSupportsFloat32Textures = mDisplay->getFloat32TextureSupport(&mSupportsFloat32LinearFilter, &mSupportsFloat32RenderableTextures);
300 mSupportsFloat16Textures = mDisplay->getFloat16TextureSupport(&mSupportsFloat16LinearFilter, &mSupportsFloat16RenderableTextures);
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000301 mSupportsLuminanceTextures = mDisplay->getLuminanceTextureSupport();
302 mSupportsLuminanceAlphaTextures = mDisplay->getLuminanceAlphaTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000303
daniel@transgaming.com83921382011-01-08 05:46:00 +0000304 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
305
gman@chromium.org50c526d2011-08-10 05:19:44 +0000306 mNumCompressedTextureFormats = 0;
307 if (supportsDXT1Textures())
308 {
309 mNumCompressedTextureFormats += 2;
310 }
311 if (supportsDXT3Textures())
312 {
313 mNumCompressedTextureFormats += 1;
314 }
315 if (supportsDXT5Textures())
316 {
317 mNumCompressedTextureFormats += 1;
318 }
319
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000320 initExtensionString();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +0000321 initRendererString();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000322
323 mState.viewportX = 0;
324 mState.viewportY = 0;
325 mState.viewportWidth = surface->getWidth();
326 mState.viewportHeight = surface->getHeight();
327
328 mState.scissorX = 0;
329 mState.scissorY = 0;
330 mState.scissorWidth = surface->getWidth();
331 mState.scissorHeight = surface->getHeight();
332
333 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000334 }
335
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000336 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
337 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000338 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000339
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000340 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000341 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000342 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000343
344 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000345
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000346 if (defaultRenderTarget)
347 {
348 defaultRenderTarget->Release();
349 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000350
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000351 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000352 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000353 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000355
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000356 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000357}
358
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000359// 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 +0000360void Context::markAllStateDirty()
361{
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000362 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
363 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +0000364 mAppliedTextureSerialPS[t] = 0;
365 }
366
367 for (int t = 0; t < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; t++)
368 {
369 mAppliedTextureSerialVS[t] = 0;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000370 }
371
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000372 mAppliedProgramSerial = 0;
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000373 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000374 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000375 mAppliedStencilbufferSerial = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000376 mAppliedIBSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000377 mDepthStencilInitialized = false;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000378 mViewportInitialized = false;
379 mRenderTargetDescInitialized = false;
380
381 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000382
383 mClearStateDirty = true;
384 mCullStateDirty = true;
385 mDepthStateDirty = true;
386 mMaskStateDirty = true;
387 mBlendStateDirty = true;
388 mStencilStateDirty = true;
389 mPolygonOffsetStateDirty = true;
390 mScissorStateDirty = true;
391 mSampleStateDirty = true;
392 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000393 mFrontFaceDirty = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +0000394 mDxUniformsDirty = true;
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000395 mCachedCurrentProgram = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000396}
397
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000398void Context::markContextLost()
399{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +0000400 if (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT)
401 mResetStatus = GL_UNKNOWN_CONTEXT_RESET_EXT;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000402 mContextLost = true;
403}
404
405bool Context::isContextLost()
406{
407 return mContextLost;
408}
409
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000410void Context::setClearColor(float red, float green, float blue, float alpha)
411{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000412 mState.colorClearValue.red = red;
413 mState.colorClearValue.green = green;
414 mState.colorClearValue.blue = blue;
415 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000416}
417
418void Context::setClearDepth(float depth)
419{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000420 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000421}
422
423void Context::setClearStencil(int stencil)
424{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000425 mState.stencilClearValue = stencil;
426}
427
428void Context::setCullFace(bool enabled)
429{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000430 if (mState.cullFace != enabled)
431 {
432 mState.cullFace = enabled;
433 mCullStateDirty = true;
434 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000435}
436
437bool Context::isCullFaceEnabled() const
438{
439 return mState.cullFace;
440}
441
442void Context::setCullMode(GLenum mode)
443{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000444 if (mState.cullMode != mode)
445 {
446 mState.cullMode = mode;
447 mCullStateDirty = true;
448 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000449}
450
451void Context::setFrontFace(GLenum front)
452{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000453 if (mState.frontFace != front)
454 {
455 mState.frontFace = front;
456 mFrontFaceDirty = true;
457 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000458}
459
460void Context::setDepthTest(bool enabled)
461{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000462 if (mState.depthTest != enabled)
463 {
464 mState.depthTest = enabled;
465 mDepthStateDirty = true;
466 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000467}
468
469bool Context::isDepthTestEnabled() const
470{
471 return mState.depthTest;
472}
473
474void Context::setDepthFunc(GLenum depthFunc)
475{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000476 if (mState.depthFunc != depthFunc)
477 {
478 mState.depthFunc = depthFunc;
479 mDepthStateDirty = true;
480 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000481}
482
483void Context::setDepthRange(float zNear, float zFar)
484{
485 mState.zNear = zNear;
486 mState.zFar = zFar;
487}
488
489void Context::setBlend(bool enabled)
490{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000491 if (mState.blend != enabled)
492 {
493 mState.blend = enabled;
494 mBlendStateDirty = true;
495 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000496}
497
498bool Context::isBlendEnabled() const
499{
500 return mState.blend;
501}
502
503void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
504{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000505 if (mState.sourceBlendRGB != sourceRGB ||
506 mState.sourceBlendAlpha != sourceAlpha ||
507 mState.destBlendRGB != destRGB ||
508 mState.destBlendAlpha != destAlpha)
509 {
510 mState.sourceBlendRGB = sourceRGB;
511 mState.destBlendRGB = destRGB;
512 mState.sourceBlendAlpha = sourceAlpha;
513 mState.destBlendAlpha = destAlpha;
514 mBlendStateDirty = true;
515 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000516}
517
518void Context::setBlendColor(float red, float green, float blue, float alpha)
519{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000520 if (mState.blendColor.red != red ||
521 mState.blendColor.green != green ||
522 mState.blendColor.blue != blue ||
523 mState.blendColor.alpha != alpha)
524 {
525 mState.blendColor.red = red;
526 mState.blendColor.green = green;
527 mState.blendColor.blue = blue;
528 mState.blendColor.alpha = alpha;
529 mBlendStateDirty = true;
530 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000531}
532
533void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
534{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000535 if (mState.blendEquationRGB != rgbEquation ||
536 mState.blendEquationAlpha != alphaEquation)
537 {
538 mState.blendEquationRGB = rgbEquation;
539 mState.blendEquationAlpha = alphaEquation;
540 mBlendStateDirty = true;
541 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000542}
543
544void Context::setStencilTest(bool enabled)
545{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000546 if (mState.stencilTest != enabled)
547 {
548 mState.stencilTest = enabled;
549 mStencilStateDirty = true;
550 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000551}
552
553bool Context::isStencilTestEnabled() const
554{
555 return mState.stencilTest;
556}
557
558void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
559{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000560 if (mState.stencilFunc != stencilFunc ||
561 mState.stencilRef != stencilRef ||
562 mState.stencilMask != stencilMask)
563 {
564 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000565 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000566 mState.stencilMask = stencilMask;
567 mStencilStateDirty = true;
568 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000569}
570
571void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
572{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000573 if (mState.stencilBackFunc != stencilBackFunc ||
574 mState.stencilBackRef != stencilBackRef ||
575 mState.stencilBackMask != stencilBackMask)
576 {
577 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000578 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000579 mState.stencilBackMask = stencilBackMask;
580 mStencilStateDirty = true;
581 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000582}
583
584void Context::setStencilWritemask(GLuint stencilWritemask)
585{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000586 if (mState.stencilWritemask != stencilWritemask)
587 {
588 mState.stencilWritemask = stencilWritemask;
589 mStencilStateDirty = true;
590 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000591}
592
593void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
594{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000595 if (mState.stencilBackWritemask != stencilBackWritemask)
596 {
597 mState.stencilBackWritemask = stencilBackWritemask;
598 mStencilStateDirty = true;
599 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000600}
601
602void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
603{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000604 if (mState.stencilFail != stencilFail ||
605 mState.stencilPassDepthFail != stencilPassDepthFail ||
606 mState.stencilPassDepthPass != stencilPassDepthPass)
607 {
608 mState.stencilFail = stencilFail;
609 mState.stencilPassDepthFail = stencilPassDepthFail;
610 mState.stencilPassDepthPass = stencilPassDepthPass;
611 mStencilStateDirty = true;
612 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000613}
614
615void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
616{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000617 if (mState.stencilBackFail != stencilBackFail ||
618 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
619 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
620 {
621 mState.stencilBackFail = stencilBackFail;
622 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
623 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
624 mStencilStateDirty = true;
625 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000626}
627
628void Context::setPolygonOffsetFill(bool enabled)
629{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000630 if (mState.polygonOffsetFill != enabled)
631 {
632 mState.polygonOffsetFill = enabled;
633 mPolygonOffsetStateDirty = true;
634 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000635}
636
637bool Context::isPolygonOffsetFillEnabled() const
638{
639 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000640
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000641}
642
643void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
644{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000645 if (mState.polygonOffsetFactor != factor ||
646 mState.polygonOffsetUnits != units)
647 {
648 mState.polygonOffsetFactor = factor;
649 mState.polygonOffsetUnits = units;
650 mPolygonOffsetStateDirty = true;
651 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000652}
653
654void Context::setSampleAlphaToCoverage(bool enabled)
655{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000656 if (mState.sampleAlphaToCoverage != enabled)
657 {
658 mState.sampleAlphaToCoverage = enabled;
659 mSampleStateDirty = true;
660 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000661}
662
663bool Context::isSampleAlphaToCoverageEnabled() const
664{
665 return mState.sampleAlphaToCoverage;
666}
667
668void Context::setSampleCoverage(bool enabled)
669{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000670 if (mState.sampleCoverage != enabled)
671 {
672 mState.sampleCoverage = enabled;
673 mSampleStateDirty = true;
674 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000675}
676
677bool Context::isSampleCoverageEnabled() const
678{
679 return mState.sampleCoverage;
680}
681
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000682void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000683{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000684 if (mState.sampleCoverageValue != value ||
685 mState.sampleCoverageInvert != invert)
686 {
687 mState.sampleCoverageValue = value;
688 mState.sampleCoverageInvert = invert;
689 mSampleStateDirty = true;
690 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000691}
692
693void Context::setScissorTest(bool enabled)
694{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000695 if (mState.scissorTest != enabled)
696 {
697 mState.scissorTest = enabled;
698 mScissorStateDirty = true;
699 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000700}
701
702bool Context::isScissorTestEnabled() const
703{
704 return mState.scissorTest;
705}
706
707void Context::setDither(bool enabled)
708{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000709 if (mState.dither != enabled)
710 {
711 mState.dither = enabled;
712 mDitherStateDirty = true;
713 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000714}
715
716bool Context::isDitherEnabled() const
717{
718 return mState.dither;
719}
720
721void Context::setLineWidth(GLfloat width)
722{
723 mState.lineWidth = width;
724}
725
726void Context::setGenerateMipmapHint(GLenum hint)
727{
728 mState.generateMipmapHint = hint;
729}
730
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000731void Context::setFragmentShaderDerivativeHint(GLenum hint)
732{
733 mState.fragmentShaderDerivativeHint = hint;
734 // TODO: Propagate the hint to shader translator so we can write
735 // ddx, ddx_coarse, or ddx_fine depending on the hint.
736 // Ignore for now. It is valid for implementations to ignore hint.
737}
738
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000739void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
740{
741 mState.viewportX = x;
742 mState.viewportY = y;
743 mState.viewportWidth = width;
744 mState.viewportHeight = height;
745}
746
747void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
748{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000749 if (mState.scissorX != x || mState.scissorY != y ||
750 mState.scissorWidth != width || mState.scissorHeight != height)
751 {
752 mState.scissorX = x;
753 mState.scissorY = y;
754 mState.scissorWidth = width;
755 mState.scissorHeight = height;
756 mScissorStateDirty = true;
757 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000758}
759
760void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
761{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000762 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
763 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
764 {
765 mState.colorMaskRed = red;
766 mState.colorMaskGreen = green;
767 mState.colorMaskBlue = blue;
768 mState.colorMaskAlpha = alpha;
769 mMaskStateDirty = true;
770 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000771}
772
773void Context::setDepthMask(bool mask)
774{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000775 if (mState.depthMask != mask)
776 {
777 mState.depthMask = mask;
778 mMaskStateDirty = true;
779 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000780}
781
daniel@transgaming.comdfd57022011-05-11 15:37:25 +0000782void Context::setActiveSampler(unsigned int active)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000783{
784 mState.activeSampler = active;
785}
786
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000787GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000788{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000789 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000790}
791
792GLuint Context::getDrawFramebufferHandle() const
793{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000794 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000795}
796
797GLuint Context::getRenderbufferHandle() const
798{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000799 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000800}
801
802GLuint Context::getArrayBufferHandle() const
803{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000804 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000805}
806
daniel@transgaming.com83921382011-01-08 05:46:00 +0000807void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000808{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000809 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000810}
811
daniel@transgaming.com83921382011-01-08 05:46:00 +0000812const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000813{
814 return mState.vertexAttribute[attribNum];
815}
816
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000817void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000818 GLsizei stride, const void *pointer)
819{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000820 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000821 mState.vertexAttribute[attribNum].mSize = size;
822 mState.vertexAttribute[attribNum].mType = type;
823 mState.vertexAttribute[attribNum].mNormalized = normalized;
824 mState.vertexAttribute[attribNum].mStride = stride;
825 mState.vertexAttribute[attribNum].mPointer = pointer;
826}
827
828const void *Context::getVertexAttribPointer(unsigned int attribNum) const
829{
830 return mState.vertexAttribute[attribNum].mPointer;
831}
832
daniel@transgaming.com83921382011-01-08 05:46:00 +0000833const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000834{
835 return mState.vertexAttribute;
836}
837
838void Context::setPackAlignment(GLint alignment)
839{
840 mState.packAlignment = alignment;
841}
842
843GLint Context::getPackAlignment() const
844{
845 return mState.packAlignment;
846}
847
848void Context::setUnpackAlignment(GLint alignment)
849{
850 mState.unpackAlignment = alignment;
851}
852
853GLint Context::getUnpackAlignment() const
854{
855 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856}
857
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858GLuint Context::createBuffer()
859{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000860 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861}
862
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000863GLuint Context::createProgram()
864{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000865 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000866}
867
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000868GLuint Context::createShader(GLenum type)
869{
870 return mResourceManager->createShader(type);
871}
872
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873GLuint Context::createTexture()
874{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000875 return mResourceManager->createTexture();
876}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000878GLuint Context::createRenderbuffer()
879{
880 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000881}
882
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000883// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000884GLuint Context::createFramebuffer()
885{
benvanik@google.com1a233342011-04-28 19:44:39 +0000886 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000887
888 mFramebufferMap[handle] = NULL;
889
890 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000891}
892
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000893GLuint Context::createFence()
894{
benvanik@google.com1a233342011-04-28 19:44:39 +0000895 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000896
897 mFenceMap[handle] = new Fence;
898
899 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000900}
901
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000902void Context::deleteBuffer(GLuint buffer)
903{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000904 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000905 {
906 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000907 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000908
909 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910}
911
912void Context::deleteShader(GLuint shader)
913{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000914 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000915}
916
917void Context::deleteProgram(GLuint program)
918{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000919 mResourceManager->deleteProgram(program);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000920 mCachedCurrentProgram = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000921}
922
923void Context::deleteTexture(GLuint texture)
924{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000925 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000926 {
927 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000928 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000929
930 mResourceManager->deleteTexture(texture);
931}
932
933void Context::deleteRenderbuffer(GLuint renderbuffer)
934{
935 if (mResourceManager->getRenderbuffer(renderbuffer))
936 {
937 detachRenderbuffer(renderbuffer);
938 }
939
940 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000941}
942
943void Context::deleteFramebuffer(GLuint framebuffer)
944{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000945 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
946
947 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000948 {
949 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000950
benvanik@google.com1a233342011-04-28 19:44:39 +0000951 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000952 delete framebufferObject->second;
953 mFramebufferMap.erase(framebufferObject);
954 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000955}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000956
957void Context::deleteFence(GLuint fence)
958{
959 FenceMap::iterator fenceObject = mFenceMap.find(fence);
960
961 if (fenceObject != mFenceMap.end())
962 {
benvanik@google.com1a233342011-04-28 19:44:39 +0000963 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000964 delete fenceObject->second;
965 mFenceMap.erase(fenceObject);
966 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000967}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000968
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000969Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000971 return mResourceManager->getBuffer(handle);
972}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000974Shader *Context::getShader(GLuint handle)
975{
976 return mResourceManager->getShader(handle);
977}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000978
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000979Program *Context::getProgram(GLuint handle)
980{
981 return mResourceManager->getProgram(handle);
982}
983
984Texture *Context::getTexture(GLuint handle)
985{
986 return mResourceManager->getTexture(handle);
987}
988
989Renderbuffer *Context::getRenderbuffer(GLuint handle)
990{
991 return mResourceManager->getRenderbuffer(handle);
992}
993
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000994Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000995{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000996 return getFramebuffer(mState.readFramebuffer);
997}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000998
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000999Framebuffer *Context::getDrawFramebuffer()
1000{
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001001 return mBoundDrawFramebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001002}
1003
1004void Context::bindArrayBuffer(unsigned int buffer)
1005{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001006 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001007
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001008 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001009}
1010
1011void Context::bindElementArrayBuffer(unsigned int buffer)
1012{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001013 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001014
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001015 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001016}
1017
1018void Context::bindTexture2D(GLuint texture)
1019{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001020 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001021
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001022 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001023}
1024
1025void Context::bindTextureCubeMap(GLuint texture)
1026{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001027 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001029 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030}
1031
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001032void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001033{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001034 if (!getFramebuffer(framebuffer))
1035 {
1036 mFramebufferMap[framebuffer] = new Framebuffer();
1037 }
1038
1039 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001040}
1041
1042void Context::bindDrawFramebuffer(GLuint framebuffer)
1043{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001044 if (!getFramebuffer(framebuffer))
1045 {
1046 mFramebufferMap[framebuffer] = new Framebuffer();
1047 }
1048
1049 mState.drawFramebuffer = framebuffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001050
1051 mBoundDrawFramebuffer = getFramebuffer(framebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001052}
1053
1054void Context::bindRenderbuffer(GLuint renderbuffer)
1055{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001056 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1057
1058 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001059}
1060
1061void Context::useProgram(GLuint program)
1062{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001063 GLuint priorProgram = mState.currentProgram;
1064 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001065
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001066 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001067 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001068 Program *newProgram = mResourceManager->getProgram(program);
1069 Program *oldProgram = mResourceManager->getProgram(priorProgram);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001070 mCachedCurrentProgram = NULL;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001071 mDxUniformsDirty = true;
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001072
1073 if (newProgram)
1074 {
1075 newProgram->addRef();
1076 }
1077
1078 if (oldProgram)
1079 {
1080 oldProgram->release();
1081 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001082 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001083}
1084
1085void Context::setFramebufferZero(Framebuffer *buffer)
1086{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001087 delete mFramebufferMap[0];
1088 mFramebufferMap[0] = buffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001089 if (mState.drawFramebuffer == 0)
1090 {
1091 mBoundDrawFramebuffer = buffer;
1092 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001093}
1094
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001095void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001096{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001097 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1098 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001099}
1100
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001101Framebuffer *Context::getFramebuffer(unsigned int handle)
1102{
1103 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1104
1105 if (framebuffer == mFramebufferMap.end())
1106 {
1107 return NULL;
1108 }
1109 else
1110 {
1111 return framebuffer->second;
1112 }
1113}
1114
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001115Fence *Context::getFence(unsigned int handle)
1116{
1117 FenceMap::iterator fence = mFenceMap.find(handle);
1118
1119 if (fence == mFenceMap.end())
1120 {
1121 return NULL;
1122 }
1123 else
1124 {
1125 return fence->second;
1126 }
1127}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001128
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001129Buffer *Context::getArrayBuffer()
1130{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001131 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001132}
1133
1134Buffer *Context::getElementArrayBuffer()
1135{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001136 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001137}
1138
1139Program *Context::getCurrentProgram()
1140{
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001141 if (!mCachedCurrentProgram)
1142 {
1143 mCachedCurrentProgram = mResourceManager->getProgram(mState.currentProgram);
1144 }
1145 return mCachedCurrentProgram;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001146}
1147
1148Texture2D *Context::getTexture2D()
1149{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001150 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001151}
1152
1153TextureCubeMap *Context::getTextureCubeMap()
1154{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001155 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001156}
1157
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001158Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001159{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001160 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001161
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001162 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001163 {
1164 switch (type)
1165 {
1166 default: UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001167 case TEXTURE_2D: return mTexture2DZero.get();
1168 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001169 }
1170 }
1171
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001172 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001173}
1174
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001175bool Context::getBooleanv(GLenum pname, GLboolean *params)
1176{
1177 switch (pname)
1178 {
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001179 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1180 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1181 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001182 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001183 params[0] = mState.colorMaskRed;
1184 params[1] = mState.colorMaskGreen;
1185 params[2] = mState.colorMaskBlue;
1186 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001187 break;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001188 case GL_CULL_FACE: *params = mState.cullFace; break;
1189 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1190 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1191 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1192 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1193 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1194 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1195 case GL_BLEND: *params = mState.blend; break;
1196 case GL_DITHER: *params = mState.dither; break;
1197 case GL_CONTEXT_ROBUST_ACCESS_EXT: *params = mRobustAccess ? GL_TRUE : GL_FALSE; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001198 default:
1199 return false;
1200 }
1201
1202 return true;
1203}
1204
1205bool Context::getFloatv(GLenum pname, GLfloat *params)
1206{
1207 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1208 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1209 // GetIntegerv as its native query function. As it would require conversion in any
1210 // case, this should make no difference to the calling application.
1211 switch (pname)
1212 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001213 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1214 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1215 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1216 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1217 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001218 case GL_ALIASED_LINE_WIDTH_RANGE:
1219 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1220 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1221 break;
1222 case GL_ALIASED_POINT_SIZE_RANGE:
1223 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001224 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 +00001225 break;
1226 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001227 params[0] = mState.zNear;
1228 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001229 break;
1230 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001231 params[0] = mState.colorClearValue.red;
1232 params[1] = mState.colorClearValue.green;
1233 params[2] = mState.colorClearValue.blue;
1234 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001235 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001236 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001237 params[0] = mState.blendColor.red;
1238 params[1] = mState.blendColor.green;
1239 params[2] = mState.blendColor.blue;
1240 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001241 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001242 default:
1243 return false;
1244 }
1245
1246 return true;
1247}
1248
1249bool Context::getIntegerv(GLenum pname, GLint *params)
1250{
1251 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1252 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1253 // GetIntegerv as its native query function. As it would require conversion in any
1254 // case, this should make no difference to the calling application. You may find it in
1255 // Context::getFloatv.
1256 switch (pname)
1257 {
1258 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1259 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001260 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00001261 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1262 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001263 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001264 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001265 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001266 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001267 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001268 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1269 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001270 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1271 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1272 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001273 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001274 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1275 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1276 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1277 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001278 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001279 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1280 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1281 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1282 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1283 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1284 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1285 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1286 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1287 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1288 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1289 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1290 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1291 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1292 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1293 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1294 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1295 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1296 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1297 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1298 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1299 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1300 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1301 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1302 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001303 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1304 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001305 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
gman@chromium.org50c526d2011-08-10 05:19:44 +00001306 params[0] = mNumCompressedTextureFormats;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001307 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001308 case GL_MAX_SAMPLES_ANGLE:
1309 {
1310 GLsizei maxSamples = getMaxSupportedSamples();
1311 if (maxSamples != 0)
1312 {
1313 *params = maxSamples;
1314 }
1315 else
1316 {
1317 return false;
1318 }
1319
1320 break;
1321 }
1322 case GL_SAMPLE_BUFFERS:
1323 case GL_SAMPLES:
1324 {
1325 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1326 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1327 {
1328 switch (pname)
1329 {
1330 case GL_SAMPLE_BUFFERS:
1331 if (framebuffer->getSamples() != 0)
1332 {
1333 *params = 1;
1334 }
1335 else
1336 {
1337 *params = 0;
1338 }
1339 break;
1340 case GL_SAMPLES:
1341 *params = framebuffer->getSamples();
1342 break;
1343 }
1344 }
1345 else
1346 {
1347 *params = 0;
1348 }
1349 }
1350 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001351 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1352 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1353 case GL_MAX_VIEWPORT_DIMS:
1354 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001355 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001356 params[0] = maxDimension;
1357 params[1] = maxDimension;
1358 }
1359 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001360 case GL_COMPRESSED_TEXTURE_FORMATS:
1361 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001362 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00001363 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001364 *params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1365 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1366 }
1367 if (supportsDXT3Textures())
1368 {
1369 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
1370 }
1371 if (supportsDXT5Textures())
1372 {
1373 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001374 }
1375 }
1376 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001377 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001378 params[0] = mState.viewportX;
1379 params[1] = mState.viewportY;
1380 params[2] = mState.viewportWidth;
1381 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001382 break;
1383 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001384 params[0] = mState.scissorX;
1385 params[1] = mState.scissorY;
1386 params[2] = mState.scissorWidth;
1387 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001388 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001389 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1390 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001391 case GL_RED_BITS:
1392 case GL_GREEN_BITS:
1393 case GL_BLUE_BITS:
1394 case GL_ALPHA_BITS:
1395 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001396 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001397 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001398
1399 if (colorbuffer)
1400 {
1401 switch (pname)
1402 {
1403 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1404 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1405 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1406 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1407 }
1408 }
1409 else
1410 {
1411 *params = 0;
1412 }
1413 }
1414 break;
1415 case GL_DEPTH_BITS:
1416 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001417 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001418 gl::Renderbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001419
1420 if (depthbuffer)
1421 {
1422 *params = depthbuffer->getDepthSize();
1423 }
1424 else
1425 {
1426 *params = 0;
1427 }
1428 }
1429 break;
1430 case GL_STENCIL_BITS:
1431 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001432 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001433 gl::Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001434
1435 if (stencilbuffer)
1436 {
1437 *params = stencilbuffer->getStencilSize();
1438 }
1439 else
1440 {
1441 *params = 0;
1442 }
1443 }
1444 break;
1445 case GL_TEXTURE_BINDING_2D:
1446 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001447 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001448 {
1449 error(GL_INVALID_OPERATION);
1450 return false;
1451 }
1452
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001453 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001454 }
1455 break;
1456 case GL_TEXTURE_BINDING_CUBE_MAP:
1457 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001458 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001459 {
1460 error(GL_INVALID_OPERATION);
1461 return false;
1462 }
1463
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001464 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001465 }
1466 break;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001467 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
1468 *params = mResetStrategy;
1469 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001470 default:
1471 return false;
1472 }
1473
1474 return true;
1475}
1476
1477bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1478{
1479 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1480 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1481 // to the fact that it is stored internally as a float, and so would require conversion
1482 // if returned from Context::getIntegerv. Since this conversion is already implemented
1483 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1484 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1485 // application.
1486 switch (pname)
1487 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001488 case GL_COMPRESSED_TEXTURE_FORMATS:
1489 {
1490 *type = GL_INT;
1491 *numParams = mNumCompressedTextureFormats;
1492 }
1493 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001494 case GL_SHADER_BINARY_FORMATS:
1495 {
1496 *type = GL_INT;
1497 *numParams = 0;
1498 }
1499 break;
1500 case GL_MAX_VERTEX_ATTRIBS:
1501 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1502 case GL_MAX_VARYING_VECTORS:
1503 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1504 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1505 case GL_MAX_TEXTURE_IMAGE_UNITS:
1506 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1507 case GL_MAX_RENDERBUFFER_SIZE:
1508 case GL_NUM_SHADER_BINARY_FORMATS:
1509 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1510 case GL_ARRAY_BUFFER_BINDING:
1511 case GL_FRAMEBUFFER_BINDING:
1512 case GL_RENDERBUFFER_BINDING:
1513 case GL_CURRENT_PROGRAM:
1514 case GL_PACK_ALIGNMENT:
1515 case GL_UNPACK_ALIGNMENT:
1516 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001517 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001518 case GL_RED_BITS:
1519 case GL_GREEN_BITS:
1520 case GL_BLUE_BITS:
1521 case GL_ALPHA_BITS:
1522 case GL_DEPTH_BITS:
1523 case GL_STENCIL_BITS:
1524 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1525 case GL_CULL_FACE_MODE:
1526 case GL_FRONT_FACE:
1527 case GL_ACTIVE_TEXTURE:
1528 case GL_STENCIL_FUNC:
1529 case GL_STENCIL_VALUE_MASK:
1530 case GL_STENCIL_REF:
1531 case GL_STENCIL_FAIL:
1532 case GL_STENCIL_PASS_DEPTH_FAIL:
1533 case GL_STENCIL_PASS_DEPTH_PASS:
1534 case GL_STENCIL_BACK_FUNC:
1535 case GL_STENCIL_BACK_VALUE_MASK:
1536 case GL_STENCIL_BACK_REF:
1537 case GL_STENCIL_BACK_FAIL:
1538 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1539 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1540 case GL_DEPTH_FUNC:
1541 case GL_BLEND_SRC_RGB:
1542 case GL_BLEND_SRC_ALPHA:
1543 case GL_BLEND_DST_RGB:
1544 case GL_BLEND_DST_ALPHA:
1545 case GL_BLEND_EQUATION_RGB:
1546 case GL_BLEND_EQUATION_ALPHA:
1547 case GL_STENCIL_WRITEMASK:
1548 case GL_STENCIL_BACK_WRITEMASK:
1549 case GL_STENCIL_CLEAR_VALUE:
1550 case GL_SUBPIXEL_BITS:
1551 case GL_MAX_TEXTURE_SIZE:
1552 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1553 case GL_SAMPLE_BUFFERS:
1554 case GL_SAMPLES:
1555 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1556 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1557 case GL_TEXTURE_BINDING_2D:
1558 case GL_TEXTURE_BINDING_CUBE_MAP:
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001559 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001560 {
1561 *type = GL_INT;
1562 *numParams = 1;
1563 }
1564 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001565 case GL_MAX_SAMPLES_ANGLE:
1566 {
1567 if (getMaxSupportedSamples() != 0)
1568 {
1569 *type = GL_INT;
1570 *numParams = 1;
1571 }
1572 else
1573 {
1574 return false;
1575 }
1576 }
1577 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001578 case GL_MAX_VIEWPORT_DIMS:
1579 {
1580 *type = GL_INT;
1581 *numParams = 2;
1582 }
1583 break;
1584 case GL_VIEWPORT:
1585 case GL_SCISSOR_BOX:
1586 {
1587 *type = GL_INT;
1588 *numParams = 4;
1589 }
1590 break;
1591 case GL_SHADER_COMPILER:
1592 case GL_SAMPLE_COVERAGE_INVERT:
1593 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001594 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1595 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1596 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1597 case GL_SAMPLE_COVERAGE:
1598 case GL_SCISSOR_TEST:
1599 case GL_STENCIL_TEST:
1600 case GL_DEPTH_TEST:
1601 case GL_BLEND:
1602 case GL_DITHER:
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001603 case GL_CONTEXT_ROBUST_ACCESS_EXT:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001604 {
1605 *type = GL_BOOL;
1606 *numParams = 1;
1607 }
1608 break;
1609 case GL_COLOR_WRITEMASK:
1610 {
1611 *type = GL_BOOL;
1612 *numParams = 4;
1613 }
1614 break;
1615 case GL_POLYGON_OFFSET_FACTOR:
1616 case GL_POLYGON_OFFSET_UNITS:
1617 case GL_SAMPLE_COVERAGE_VALUE:
1618 case GL_DEPTH_CLEAR_VALUE:
1619 case GL_LINE_WIDTH:
1620 {
1621 *type = GL_FLOAT;
1622 *numParams = 1;
1623 }
1624 break;
1625 case GL_ALIASED_LINE_WIDTH_RANGE:
1626 case GL_ALIASED_POINT_SIZE_RANGE:
1627 case GL_DEPTH_RANGE:
1628 {
1629 *type = GL_FLOAT;
1630 *numParams = 2;
1631 }
1632 break;
1633 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001634 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001635 {
1636 *type = GL_FLOAT;
1637 *numParams = 4;
1638 }
1639 break;
1640 default:
1641 return false;
1642 }
1643
1644 return true;
1645}
1646
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001647// Applies the render target surface, depth stencil surface, viewport rectangle and
1648// scissor rectangle to the Direct3D 9 device
1649bool Context::applyRenderTarget(bool ignoreViewport)
1650{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001651 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001652
1653 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1654 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001655 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001656 }
1657
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001658 IDirect3DSurface9 *renderTarget = NULL;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001659 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001660
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001661 bool renderTargetChanged = false;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001662 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1663 if (renderTargetSerial != mAppliedRenderTargetSerial)
1664 {
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001665 renderTarget = framebufferObject->getRenderTarget();
1666
1667 if (!renderTarget)
1668 {
1669 return false; // Context must be lost
1670 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001671 mDevice->SetRenderTarget(0, renderTarget);
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001672 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001673 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 +00001674 renderTargetChanged = true;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001675 }
1676
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001677 unsigned int depthbufferSerial = 0;
1678 unsigned int stencilbufferSerial = 0;
1679 if (framebufferObject->getDepthbufferType() != GL_NONE)
1680 {
1681 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001682 if (!depthStencil)
1683 {
1684 ERR("Depth stencil pointer unexpectedly null.");
1685 return false;
1686 }
1687
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001688 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1689 }
1690 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1691 {
1692 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001693 if (!depthStencil)
1694 {
1695 ERR("Depth stencil pointer unexpectedly null.");
1696 return false;
1697 }
1698
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001699 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1700 }
1701
1702 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001703 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001704 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001705 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001706 mDevice->SetDepthStencilSurface(depthStencil);
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001707 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001708 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001709 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001710 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001711
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001712 if (!mRenderTargetDescInitialized || renderTargetChanged)
1713 {
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001714 if (!renderTarget)
1715 {
1716 renderTarget = framebufferObject->getRenderTarget();
1717
1718 if (!renderTarget)
1719 {
1720 return false; // Context must be lost
1721 }
1722 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001723 renderTarget->GetDesc(&mRenderTargetDesc);
1724 mRenderTargetDescInitialized = true;
1725 }
1726
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001727 D3DVIEWPORT9 viewport;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001728
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001729 float zNear = clamp01(mState.zNear);
1730 float zFar = clamp01(mState.zFar);
1731
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001732 if (ignoreViewport)
1733 {
1734 viewport.X = 0;
1735 viewport.Y = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001736 viewport.Width = mRenderTargetDesc.Width;
1737 viewport.Height = mRenderTargetDesc.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001738 viewport.MinZ = 0.0f;
1739 viewport.MaxZ = 1.0f;
1740 }
1741 else
1742 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001743 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, mRenderTargetDesc.Height);
1744 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1745 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1746 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width) - static_cast<LONG>(viewport.X));
1747 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 +00001748 viewport.MinZ = zNear;
1749 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001750 }
1751
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001752 if (viewport.Width <= 0 || viewport.Height <= 0)
1753 {
1754 return false; // Nothing to render
1755 }
1756
jbauman@chromium.org241e70d2011-11-03 23:07:05 +00001757 if (renderTargetChanged || !mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001758 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001759 mDevice->SetViewport(&viewport);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001760 mSetViewport = viewport;
1761 mViewportInitialized = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001762 mDxUniformsDirty = true;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001763 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001764
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001765 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001766 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001767 if (mState.scissorTest)
1768 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001769 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, mRenderTargetDesc.Height);
1770 rect.left = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1771 rect.top = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1772 rect.right = clamp(rect.right, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1773 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001774 mDevice->SetScissorRect(&rect);
1775 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001776 }
1777 else
1778 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001779 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001780 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001781
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001782 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001783 }
1784
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001785 if (mState.currentProgram && mDxUniformsDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001786 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001787 Program *programObject = getCurrentProgram();
1788
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001789 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001790 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001791 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001792
daniel@transgaming.com31754962010-11-28 02:02:52 +00001793 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001794 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1795 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1796 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001797 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001798
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001799 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001800 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001801 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001802
daniel@transgaming.com31754962010-11-28 02:02:52 +00001803 GLint depthRange = programObject->getDxDepthRangeLocation();
1804 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1805 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001806 mDxUniformsDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001807 }
1808
1809 return true;
1810}
1811
1812// 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 +00001813void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001814{
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001815 Program *programObject = getCurrentProgram();
1816
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001817 Framebuffer *framebufferObject = getDrawFramebuffer();
1818
1819 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1820
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001821 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001822 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001823 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001824
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001825 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001826 GLint alwaysFront = !isTriangleMode(drawMode);
1827 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1828
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001829 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001830 bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
1831 // Apparently some ATI cards have a bug where a draw with a zero color
1832 // write mask can cause later draws to have incorrect results. Instead,
1833 // set a nonzero color write mask but modify the blend state so that no
1834 // drawing is done.
1835 // http://code.google.com/p/angleproject/issues/detail?id=169
1836
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001837 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001838 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001839 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001840 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001841 mDevice->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001842 }
1843 else
1844 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001845 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001846 }
1847
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001848 mCullStateDirty = false;
1849 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001850
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001851 if (mDepthStateDirty)
1852 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00001853 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001854 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001855 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1856 mDevice->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001857 }
1858 else
1859 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001860 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001861 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001862
1863 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001864 }
1865
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001866 if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
1867 {
1868 mBlendStateDirty = true;
1869 mMaskStateDirty = true;
1870 }
1871
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001872 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001873 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001874 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001875 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001876 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001877
1878 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1879 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1880 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001881 mDevice->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001882 }
1883 else
1884 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001885 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001886 unorm<8>(mState.blendColor.alpha),
1887 unorm<8>(mState.blendColor.alpha),
1888 unorm<8>(mState.blendColor.alpha)));
1889 }
1890
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001891 mDevice->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1892 mDevice->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1893 mDevice->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001894
1895 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1896 mState.destBlendRGB != mState.destBlendAlpha ||
1897 mState.blendEquationRGB != mState.blendEquationAlpha)
1898 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001899 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001900
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001901 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1902 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1903 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001904 }
1905 else
1906 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001907 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001908 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001909 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001910 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001911 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001912 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001913 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001914
1915 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001916 }
1917
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001918 if (mStencilStateDirty || mFrontFaceDirty)
1919 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001920 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001921 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001922 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1923 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001924
1925 // FIXME: Unsupported by D3D9
1926 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1927 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1928 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1929 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1930 mState.stencilRef != mState.stencilBackRef ||
1931 mState.stencilMask != mState.stencilBackMask)
1932 {
1933 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1934 return error(GL_INVALID_OPERATION);
1935 }
1936
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001937 // get the maximum size of the stencil ref
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001938 gl::Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001939 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1940
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001941 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1942 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001943 es2dx::ConvertComparison(mState.stencilFunc));
1944
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001945 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1946 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001947
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001948 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001949 es2dx::ConvertStencilOp(mState.stencilFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001950 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001951 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001952 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001953 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1954
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001955 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1956 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001957 es2dx::ConvertComparison(mState.stencilBackFunc));
1958
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001959 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1960 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001961
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001962 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001963 es2dx::ConvertStencilOp(mState.stencilBackFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001964 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001965 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001966 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001967 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1968 }
1969 else
1970 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001971 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001972 }
1973
1974 mStencilStateDirty = false;
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001975 mFrontFaceDirty = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001976 }
1977
1978 if (mMaskStateDirty)
1979 {
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001980 int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1981 mState.colorMaskBlue, mState.colorMaskAlpha);
1982 if (colorMask == 0 && !zeroColorMaskAllowed)
1983 {
1984 // Enable green channel, but set blending so nothing will be drawn.
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001985 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
1986 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001987
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001988 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
1989 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
1990 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001991 }
1992 else
1993 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001994 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001995 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001996 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001997
1998 mMaskStateDirty = false;
1999 }
2000
2001 if (mPolygonOffsetStateDirty)
2002 {
2003 if (mState.polygonOffsetFill)
2004 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00002005 gl::Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002006 if (depthbuffer)
2007 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002008 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002009 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002010 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002011 }
2012 }
2013 else
2014 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002015 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
2016 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002017 }
2018
2019 mPolygonOffsetStateDirty = false;
2020 }
2021
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002022 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002023 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002024 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002025 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002026 FIXME("Sample alpha to coverage is unimplemented.");
2027 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002028
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002029 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002030 if (mState.sampleCoverage)
2031 {
2032 unsigned int mask = 0;
2033 if (mState.sampleCoverageValue != 0)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002034 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002035 float threshold = 0.5f;
2036
2037 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002038 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002039 mask <<= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002040
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002041 if ((i + 1) * mState.sampleCoverageValue >= threshold)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002042 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002043 threshold += 1.0f;
2044 mask |= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002045 }
2046 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002047 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002048
2049 if (mState.sampleCoverageInvert)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002050 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002051 mask = ~mask;
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002052 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002053
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002054 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002055 }
2056 else
2057 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002058 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002059 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002060
2061 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002062 }
2063
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002064 if (mDitherStateDirty)
2065 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002066 mDevice->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002067
2068 mDitherStateDirty = false;
2069 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002070}
2071
daniel@transgaming.com83921382011-01-08 05:46:00 +00002072GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002073{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002074 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002075
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002076 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002077 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002078 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002079 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002080 }
2081
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002082 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, getCurrentProgram());
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002083}
2084
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002085// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002086GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002087{
daniel@transgaming.com83921382011-01-08 05:46:00 +00002088 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002089
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002090 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002091 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002092 if (indexInfo->serial != mAppliedIBSerial)
2093 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002094 mDevice->SetIndices(indexInfo->indexBuffer);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002095 mAppliedIBSerial = indexInfo->serial;
2096 }
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002097 }
2098
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002099 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002100}
2101
2102// Applies the shaders and shader constants to the Direct3D 9 device
2103void Context::applyShaders()
2104{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002105 Program *programObject = getCurrentProgram();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002106 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002107 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002108 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2109 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2110
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002111 mDevice->SetPixelShader(pixelShader);
2112 mDevice->SetVertexShader(vertexShader);
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002113 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002114 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002115 }
2116
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002117 programObject->applyUniforms();
2118}
2119
2120// Applies the textures and sampler states to the Direct3D 9 device
2121void Context::applyTextures()
2122{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002123 applyTextures(SAMPLER_PIXEL);
2124
2125 if (mSupportsVertexTexture)
2126 {
2127 applyTextures(SAMPLER_VERTEX);
2128 }
2129}
2130
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002131// For each Direct3D 9 sampler of either the pixel or vertex stage,
2132// looks up the corresponding OpenGL texture image unit and texture type,
2133// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002134void Context::applyTextures(SamplerType type)
2135{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002136 Program *programObject = getCurrentProgram();
2137
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002138 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 +00002139 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2140 int d3dSamplerOffset = (type == SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002141 int samplerRange = programObject->getUsedSamplerRange(type);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002142
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002143 for (int samplerIndex = 0; samplerIndex < samplerRange; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002144 {
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002145 int textureUnit = programObject->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00002146 int d3dSampler = samplerIndex + d3dSamplerOffset;
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002147
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002148 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002149 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002150 TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002151
2152 Texture *texture = getSamplerTexture(textureUnit, textureType);
2153
daniel@transgaming.comfbc39522011-11-11 04:10:28 +00002154 if (appliedTextureSerial[samplerIndex] != texture->getTextureSerial() || texture->hasDirtyParameters() || texture->hasDirtyImages())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002155 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002156 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2157
2158 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002159 {
daniel@transgaming.comfbc39522011-11-11 04:10:28 +00002160 if (appliedTextureSerial[samplerIndex] != texture->getTextureSerial() || texture->hasDirtyParameters())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002161 {
2162 GLenum wrapS = texture->getWrapS();
2163 GLenum wrapT = texture->getWrapT();
2164 GLenum minFilter = texture->getMinFilter();
2165 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002166
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002167 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2168 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002169
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002170 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002171 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2172 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002173 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2174 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002175 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002176
daniel@transgaming.comfbc39522011-11-11 04:10:28 +00002177 if (appliedTextureSerial[samplerIndex] != texture->getTextureSerial() || texture->hasDirtyImages())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002178 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002179 mDevice->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002180 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002181 }
2182 else
2183 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002184 mDevice->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002185 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002186
daniel@transgaming.comfbc39522011-11-11 04:10:28 +00002187 appliedTextureSerial[samplerIndex] = texture->getTextureSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002188 texture->resetDirty();
2189 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002190 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002191 else
2192 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002193 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002194 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002195 mDevice->SetTexture(d3dSampler, NULL);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002196 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002197 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002198 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002199 }
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002200
2201 for (int samplerIndex = samplerRange; samplerIndex < samplerCount; samplerIndex++)
2202 {
2203 if (appliedTextureSerial[samplerIndex] != 0)
2204 {
daniel@transgaming.comc5a7b692011-10-26 02:45:44 +00002205 mDevice->SetTexture(samplerIndex + d3dSamplerOffset, NULL);
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002206 appliedTextureSerial[samplerIndex] = 0;
2207 }
2208 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002209}
2210
daniel@transgaming.comb7915a52011-11-12 03:14:20 +00002211void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height,
2212 GLenum format, GLenum type, GLsizei *bufSize, void* pixels)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002213{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002214 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002215
2216 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2217 {
2218 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2219 }
2220
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002221 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2222 {
2223 return error(GL_INVALID_OPERATION);
2224 }
2225
daniel@transgaming.comb7915a52011-11-12 03:14:20 +00002226 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
2227 // sized query sanity check
2228 if (bufSize)
2229 {
2230 int requiredSize = outputPitch * height;
2231 if (requiredSize > *bufSize)
2232 {
2233 return error(GL_INVALID_OPERATION);
2234 }
2235 }
2236
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002237 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002238
2239 if (!renderTarget)
2240 {
2241 return; // Context must be lost, return silently
2242 }
2243
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002244 D3DSURFACE_DESC desc;
2245 renderTarget->GetDesc(&desc);
2246
2247 IDirect3DSurface9 *systemSurface;
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002248 HRESULT result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002249
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002250 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002251 {
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002252 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002253 return error(GL_OUT_OF_MEMORY);
2254 }
2255
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002256 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2257 {
2258 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002259 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002260 }
2261
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002262 result = mDevice->GetRenderTargetData(renderTarget, systemSurface);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002263
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002264 if (FAILED(result))
2265 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002266 systemSurface->Release();
2267
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002268 // It turns out that D3D will sometimes produce more error
2269 // codes than those documented.
2270 if (checkDeviceLost(result))
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002271 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002272 else
2273 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002274 UNREACHABLE();
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002275 return;
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002276 }
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002277
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002278 }
2279
2280 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002281 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2282 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2283 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2284 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2285 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002286
2287 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2288
2289 if (FAILED(result))
2290 {
2291 UNREACHABLE();
2292 systemSurface->Release();
2293
2294 return; // No sensible error to generate
2295 }
2296
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002297 unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002298 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002299 unsigned short *dest16 = (unsigned short*)pixels;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002300 int inputPitch = -lock.Pitch;
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002301
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002302 for (int j = 0; j < rect.bottom - rect.top; j++)
2303 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002304 if (desc.Format == D3DFMT_A8R8G8B8 &&
2305 format == GL_BGRA_EXT &&
2306 type == GL_UNSIGNED_BYTE)
2307 {
2308 // Fast path for EXT_read_format_bgra, given
2309 // an RGBA source buffer. Note that buffers with no
2310 // alpha go through the slow path below.
2311 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002312 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002313 (rect.right - rect.left) * 4);
2314 continue;
2315 }
2316
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002317 for (int i = 0; i < rect.right - rect.left; i++)
2318 {
2319 float r;
2320 float g;
2321 float b;
2322 float a;
2323
2324 switch (desc.Format)
2325 {
2326 case D3DFMT_R5G6B5:
2327 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002328 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002329
2330 a = 1.0f;
2331 b = (rgb & 0x001F) * (1.0f / 0x001F);
2332 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2333 r = (rgb & 0xF800) * (1.0f / 0xF800);
2334 }
2335 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002336 case D3DFMT_A1R5G5B5:
2337 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002338 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002339
2340 a = (argb & 0x8000) ? 1.0f : 0.0f;
2341 b = (argb & 0x001F) * (1.0f / 0x001F);
2342 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2343 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2344 }
2345 break;
2346 case D3DFMT_A8R8G8B8:
2347 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002348 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002349
2350 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2351 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2352 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2353 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2354 }
2355 break;
2356 case D3DFMT_X8R8G8B8:
2357 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002358 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002359
2360 a = 1.0f;
2361 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2362 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2363 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2364 }
2365 break;
2366 case D3DFMT_A2R10G10B10:
2367 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002368 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002369
2370 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2371 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2372 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2373 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2374 }
2375 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002376 case D3DFMT_A32B32G32R32F:
2377 {
2378 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002379 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2380 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2381 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2382 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002383 }
2384 break;
2385 case D3DFMT_A16B16G16R16F:
2386 {
2387 // float formats in D3D are stored rgba, rather than the other way round
2388 float abgr[4];
2389
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002390 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002391
2392 a = abgr[3];
2393 b = abgr[2];
2394 g = abgr[1];
2395 r = abgr[0];
2396 }
2397 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002398 default:
2399 UNIMPLEMENTED(); // FIXME
2400 UNREACHABLE();
2401 }
2402
2403 switch (format)
2404 {
2405 case GL_RGBA:
2406 switch (type)
2407 {
2408 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002409 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2410 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2411 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2412 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002413 break;
2414 default: UNREACHABLE();
2415 }
2416 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002417 case GL_BGRA_EXT:
2418 switch (type)
2419 {
2420 case GL_UNSIGNED_BYTE:
2421 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2422 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2423 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2424 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2425 break;
2426 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2427 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2428 // this type is packed as follows:
2429 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2430 // --------------------------------------------------------------------------------
2431 // | 4th | 3rd | 2nd | 1st component |
2432 // --------------------------------------------------------------------------------
2433 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2434 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2435 ((unsigned short)(15 * a + 0.5f) << 12)|
2436 ((unsigned short)(15 * r + 0.5f) << 8) |
2437 ((unsigned short)(15 * g + 0.5f) << 4) |
2438 ((unsigned short)(15 * b + 0.5f) << 0);
2439 break;
2440 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2441 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2442 // this type is packed as follows:
2443 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2444 // --------------------------------------------------------------------------------
2445 // | 4th | 3rd | 2nd | 1st component |
2446 // --------------------------------------------------------------------------------
2447 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2448 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2449 ((unsigned short)( a + 0.5f) << 15) |
2450 ((unsigned short)(31 * r + 0.5f) << 10) |
2451 ((unsigned short)(31 * g + 0.5f) << 5) |
2452 ((unsigned short)(31 * b + 0.5f) << 0);
2453 break;
2454 default: UNREACHABLE();
2455 }
2456 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002457 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002458 switch (type)
2459 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002460 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002461 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2462 ((unsigned short)(31 * b + 0.5f) << 0) |
2463 ((unsigned short)(63 * g + 0.5f) << 5) |
2464 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002465 break;
2466 default: UNREACHABLE();
2467 }
2468 break;
2469 default: UNREACHABLE();
2470 }
2471 }
2472 }
2473
2474 systemSurface->UnlockRect();
2475
2476 systemSurface->Release();
2477}
2478
2479void Context::clear(GLbitfield mask)
2480{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002481 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002482
2483 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2484 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002485 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002486 }
2487
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002488 DWORD flags = 0;
2489
2490 if (mask & GL_COLOR_BUFFER_BIT)
2491 {
2492 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002493
2494 if (framebufferObject->getColorbufferType() != GL_NONE)
2495 {
2496 flags |= D3DCLEAR_TARGET;
2497 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002498 }
2499
2500 if (mask & GL_DEPTH_BUFFER_BIT)
2501 {
2502 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002503 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002504 {
2505 flags |= D3DCLEAR_ZBUFFER;
2506 }
2507 }
2508
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002509 GLuint stencilUnmasked = 0x0;
2510
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002511 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002512 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002513 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002514 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002515 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002516 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002517 if (!depthStencil)
2518 {
2519 ERR("Depth stencil pointer unexpectedly null.");
2520 return;
2521 }
2522
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002523 D3DSURFACE_DESC desc;
2524 depthStencil->GetDesc(&desc);
2525
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002526 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002527 stencilUnmasked = (0x1 << stencilSize) - 1;
2528
2529 if (stencilUnmasked != 0x0)
2530 {
2531 flags |= D3DCLEAR_STENCIL;
2532 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002533 }
2534 }
2535
2536 if (mask != 0)
2537 {
2538 return error(GL_INVALID_VALUE);
2539 }
2540
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002541 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2542 {
2543 return;
2544 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002545
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002546 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002547 unorm<8>(mState.colorClearValue.red),
2548 unorm<8>(mState.colorClearValue.green),
2549 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002550 float depth = clamp01(mState.depthClearValue);
2551 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002552
2553 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2554
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002555 if (!renderTarget)
2556 {
2557 return; // Context must be lost, return silently
2558 }
2559
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002560 D3DSURFACE_DESC desc;
2561 renderTarget->GetDesc(&desc);
2562
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002563 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002564
2565 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002566 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002567 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002568 !(mState.colorMaskRed && mState.colorMaskGreen &&
2569 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002570
2571 if (needMaskedColorClear || needMaskedStencilClear)
2572 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002573 // State which is altered in all paths from this point to the clear call is saved.
2574 // State which is altered in only some paths will be flagged dirty in the case that
2575 // that path is taken.
2576 HRESULT hr;
2577 if (mMaskedClearSavedState == NULL)
2578 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002579 hr = mDevice->BeginStateBlock();
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002580 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2581
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002582 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2583 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2584 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2585 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2586 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2587 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2588 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2589 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2590 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2591 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2592 mDevice->SetPixelShader(NULL);
2593 mDevice->SetVertexShader(NULL);
2594 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2595 mDevice->SetStreamSource(0, NULL, 0, 0);
2596 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2597 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2598 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2599 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2600 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2601 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2602 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002603
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002604 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002605 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2606 }
2607
2608 ASSERT(mMaskedClearSavedState != NULL);
2609
2610 if (mMaskedClearSavedState != NULL)
2611 {
2612 hr = mMaskedClearSavedState->Capture();
2613 ASSERT(SUCCEEDED(hr));
2614 }
2615
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002616 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2617 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2618 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2619 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2620 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2621 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2622 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2623 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002624
2625 if (flags & D3DCLEAR_TARGET)
2626 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002627 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002628 }
2629 else
2630 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002631 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002632 }
2633
2634 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2635 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002636 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2637 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2638 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2639 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
2640 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
2641 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
2642 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2643 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002644 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002645 }
2646 else
2647 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002648 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002649 }
2650
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002651 mDevice->SetPixelShader(NULL);
2652 mDevice->SetVertexShader(NULL);
2653 mDevice->SetFVF(D3DFVF_XYZRHW);
2654 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2655 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2656 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2657 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2658 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2659 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2660 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002661
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002662 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2663 quad[0][0] = -0.5f;
2664 quad[0][1] = desc.Height - 0.5f;
2665 quad[0][2] = 0.0f;
2666 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002667
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002668 quad[1][0] = desc.Width - 0.5f;
2669 quad[1][1] = desc.Height - 0.5f;
2670 quad[1][2] = 0.0f;
2671 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002672
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002673 quad[2][0] = -0.5f;
2674 quad[2][1] = -0.5f;
2675 quad[2][2] = 0.0f;
2676 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002677
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002678 quad[3][0] = desc.Width - 0.5f;
2679 quad[3][1] = -0.5f;
2680 quad[3][2] = 0.0f;
2681 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002682
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002683 mDisplay->startScene();
2684 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002685
2686 if (flags & D3DCLEAR_ZBUFFER)
2687 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002688 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
2689 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2690 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002691 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002692
2693 if (mMaskedClearSavedState != NULL)
2694 {
2695 mMaskedClearSavedState->Apply();
2696 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002697 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002698 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002699 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002700 mDevice->Clear(0, NULL, flags, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002701 }
2702}
2703
2704void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2705{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002706 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002707 {
2708 return error(GL_INVALID_OPERATION);
2709 }
2710
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002711 D3DPRIMITIVETYPE primitiveType;
2712 int primitiveCount;
2713
2714 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2715 return error(GL_INVALID_ENUM);
2716
2717 if (primitiveCount <= 0)
2718 {
2719 return;
2720 }
2721
2722 if (!applyRenderTarget(false))
2723 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002724 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002725 }
2726
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002727 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002728
daniel@transgaming.com83921382011-01-08 05:46:00 +00002729 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002730 if (err != GL_NO_ERROR)
2731 {
2732 return error(err);
2733 }
2734
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002735 applyShaders();
2736 applyTextures();
2737
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002738 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002739 {
2740 return error(GL_INVALID_OPERATION);
2741 }
2742
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002743 if (!cullSkipsDraw(mode))
2744 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002745 mDisplay->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002746
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002747 mDevice->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002748
2749 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2750 {
2751 drawClosingLine(first, first + count - 1);
2752 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002753 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002754}
2755
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002756void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002757{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002758 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002759 {
2760 return error(GL_INVALID_OPERATION);
2761 }
2762
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002763 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002764 {
2765 return error(GL_INVALID_OPERATION);
2766 }
2767
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002768 D3DPRIMITIVETYPE primitiveType;
2769 int primitiveCount;
2770
2771 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2772 return error(GL_INVALID_ENUM);
2773
2774 if (primitiveCount <= 0)
2775 {
2776 return;
2777 }
2778
2779 if (!applyRenderTarget(false))
2780 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002781 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002782 }
2783
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002784 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002785
2786 TranslatedIndexData indexInfo;
2787 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2788 if (err != GL_NO_ERROR)
2789 {
2790 return error(err);
2791 }
2792
daniel@transgaming.com83921382011-01-08 05:46:00 +00002793 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2794 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002795 if (err != GL_NO_ERROR)
2796 {
2797 return error(err);
2798 }
2799
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002800 applyShaders();
2801 applyTextures();
2802
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002803 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002804 {
2805 return error(GL_INVALID_OPERATION);
2806 }
2807
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002808 if (!cullSkipsDraw(mode))
2809 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002810 mDisplay->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002811
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002812 mDevice->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002813
2814 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2815 {
2816 drawClosingLine(count, type, indices);
2817 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002818 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002819}
2820
daniel@transgaming.com0d86aa72011-10-26 02:35:10 +00002821// Implements glFlush when block is false, glFinish when block is true
2822void Context::sync(bool block)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002823{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002824 IDirect3DQuery9 *eventQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002825 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002826
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002827 result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002828 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002829 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002830 ERR("CreateQuery failed hr=%x\n", result);
2831 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2832 {
2833 return error(GL_OUT_OF_MEMORY);
2834 }
2835 ASSERT(false);
2836 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002837 }
2838
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002839 result = eventQuery->Issue(D3DISSUE_END);
2840 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002841 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002842 ERR("eventQuery->Issue(END) failed hr=%x\n", result);
2843 ASSERT(false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002844 eventQuery->Release();
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002845 return;
2846 }
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002847
daniel@transgaming.com0d86aa72011-10-26 02:35:10 +00002848 do
2849 {
2850 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
2851
2852 if(block && result == S_FALSE)
2853 {
2854 // Keep polling, but allow other threads to do something useful first
2855 Sleep(0);
2856 }
2857 }
2858 while(block && result == S_FALSE);
2859
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002860 eventQuery->Release();
2861
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002862 if (checkDeviceLost(result))
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002863 {
2864 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002865 }
2866}
2867
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002868void Context::drawClosingLine(unsigned int first, unsigned int last)
2869{
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002870 IDirect3DIndexBuffer9 *indexBuffer = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002871 bool succeeded = false;
2872 UINT offset;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002873
2874 if (supports32bitIndices())
2875 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002876 const int spaceNeeded = 2 * sizeof(unsigned int);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002877
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002878 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002879 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002880 mClosingIB = new StreamingIndexBuffer(mDevice, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002881 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002882
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002883 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
2884
2885 unsigned int *data = static_cast<unsigned int*>(mClosingIB->map(spaceNeeded, &offset));
2886 if (data)
2887 {
2888 data[0] = last;
2889 data[1] = first;
2890 mClosingIB->unmap();
2891 offset /= 4;
2892 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002893 }
2894 }
2895 else
2896 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002897 const int spaceNeeded = 2 * sizeof(unsigned short);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002898
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002899 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002900 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002901 mClosingIB = new StreamingIndexBuffer(mDevice, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002902 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002903
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002904 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
2905
2906 unsigned short *data = static_cast<unsigned short*>(mClosingIB->map(spaceNeeded, &offset));
2907 if (data)
2908 {
2909 data[0] = last;
2910 data[1] = first;
2911 mClosingIB->unmap();
2912 offset /= 2;
2913 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002914 }
2915 }
2916
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002917 if (succeeded)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002918 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002919 mDevice->SetIndices(mClosingIB->getBuffer());
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002920 mAppliedIBSerial = mClosingIB->getSerial();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002921
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002922 mDevice->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, last, offset, 1);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002923 }
2924 else
2925 {
2926 ERR("Could not create an index buffer for closing a line loop.");
2927 error(GL_OUT_OF_MEMORY);
2928 }
2929}
2930
2931void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices)
2932{
2933 unsigned int first = 0;
2934 unsigned int last = 0;
2935
2936 if (mState.elementArrayBuffer.get())
2937 {
2938 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2939 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2940 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2941 }
2942
2943 switch (type)
2944 {
2945 case GL_UNSIGNED_BYTE:
2946 first = static_cast<const GLubyte*>(indices)[0];
2947 last = static_cast<const GLubyte*>(indices)[count - 1];
2948 break;
2949 case GL_UNSIGNED_SHORT:
2950 first = static_cast<const GLushort*>(indices)[0];
2951 last = static_cast<const GLushort*>(indices)[count - 1];
2952 break;
2953 case GL_UNSIGNED_INT:
2954 first = static_cast<const GLuint*>(indices)[0];
2955 last = static_cast<const GLuint*>(indices)[count - 1];
2956 break;
2957 default: UNREACHABLE();
2958 }
2959
2960 drawClosingLine(first, last);
2961}
2962
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002963void Context::recordInvalidEnum()
2964{
2965 mInvalidEnum = true;
2966}
2967
2968void Context::recordInvalidValue()
2969{
2970 mInvalidValue = true;
2971}
2972
2973void Context::recordInvalidOperation()
2974{
2975 mInvalidOperation = true;
2976}
2977
2978void Context::recordOutOfMemory()
2979{
2980 mOutOfMemory = true;
2981}
2982
2983void Context::recordInvalidFramebufferOperation()
2984{
2985 mInvalidFramebufferOperation = true;
2986}
2987
2988// Get one of the recorded errors and clear its flag, if any.
2989// [OpenGL ES 2.0.24] section 2.5 page 13.
2990GLenum Context::getError()
2991{
2992 if (mInvalidEnum)
2993 {
2994 mInvalidEnum = false;
2995
2996 return GL_INVALID_ENUM;
2997 }
2998
2999 if (mInvalidValue)
3000 {
3001 mInvalidValue = false;
3002
3003 return GL_INVALID_VALUE;
3004 }
3005
3006 if (mInvalidOperation)
3007 {
3008 mInvalidOperation = false;
3009
3010 return GL_INVALID_OPERATION;
3011 }
3012
3013 if (mOutOfMemory)
3014 {
3015 mOutOfMemory = false;
3016
3017 return GL_OUT_OF_MEMORY;
3018 }
3019
3020 if (mInvalidFramebufferOperation)
3021 {
3022 mInvalidFramebufferOperation = false;
3023
3024 return GL_INVALID_FRAMEBUFFER_OPERATION;
3025 }
3026
3027 return GL_NO_ERROR;
3028}
3029
daniel@transgaming.com17f548c2011-11-09 17:47:02 +00003030GLenum Context::getResetStatus()
3031{
3032 if (mResetStatus == GL_NO_ERROR)
3033 {
3034 bool lost = mDisplay->testDeviceLost();
3035
3036 if (lost)
3037 {
3038 mDisplay->notifyDeviceLost(); // Sets mResetStatus
3039 }
3040 }
3041
3042 GLenum status = mResetStatus;
3043
3044 if (mResetStatus != GL_NO_ERROR)
3045 {
3046 if (mDisplay->testDeviceResettable())
3047 {
3048 mResetStatus = GL_NO_ERROR;
3049 }
3050 }
3051
3052 return status;
3053}
3054
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00003055bool Context::isResetNotificationEnabled()
3056{
3057 return (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT);
3058}
3059
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003060bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003061{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003062 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003063}
3064
daniel@transgaming.com396c6432010-11-26 16:26:12 +00003065int Context::getMaximumVaryingVectors() const
3066{
3067 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3068}
3069
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003070unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003071{
3072 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3073}
3074
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003075unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003076{
3077 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3078}
3079
daniel@transgaming.com458da142010-11-28 02:03:02 +00003080int Context::getMaximumFragmentUniformVectors() const
3081{
3082 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3083}
3084
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003085int Context::getMaxSupportedSamples() const
3086{
3087 return mMaxSupportedSamples;
3088}
3089
3090int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3091{
3092 if (requested == 0)
3093 {
3094 return requested;
3095 }
3096
3097 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3098 if (itr == mMultiSampleSupport.end())
3099 {
3100 return -1;
3101 }
3102
3103 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3104 {
3105 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3106 {
3107 return i;
3108 }
3109 }
3110
3111 return -1;
3112}
3113
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003114bool Context::supportsEventQueries() const
3115{
3116 return mSupportsEventQueries;
3117}
3118
gman@chromium.org50c526d2011-08-10 05:19:44 +00003119bool Context::supportsDXT1Textures() const
daniel@transgaming.com01868132010-08-24 19:21:17 +00003120{
gman@chromium.org50c526d2011-08-10 05:19:44 +00003121 return mSupportsDXT1Textures;
3122}
3123
3124bool Context::supportsDXT3Textures() const
3125{
3126 return mSupportsDXT3Textures;
3127}
3128
3129bool Context::supportsDXT5Textures() const
3130{
3131 return mSupportsDXT5Textures;
daniel@transgaming.com01868132010-08-24 19:21:17 +00003132}
3133
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003134bool Context::supportsFloat32Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003135{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003136 return mSupportsFloat32Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003137}
3138
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003139bool Context::supportsFloat32LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003140{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003141 return mSupportsFloat32LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003142}
3143
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003144bool Context::supportsFloat32RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003145{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003146 return mSupportsFloat32RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003147}
3148
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003149bool Context::supportsFloat16Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003150{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003151 return mSupportsFloat16Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003152}
3153
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003154bool Context::supportsFloat16LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003155{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003156 return mSupportsFloat16LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003157}
3158
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003159bool Context::supportsFloat16RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003160{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003161 return mSupportsFloat16RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003162}
3163
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003164int Context::getMaximumRenderbufferDimension() const
3165{
3166 return mMaxRenderbufferDimension;
3167}
3168
3169int Context::getMaximumTextureDimension() const
3170{
3171 return mMaxTextureDimension;
3172}
3173
3174int Context::getMaximumCubeTextureDimension() const
3175{
3176 return mMaxCubeTextureDimension;
3177}
3178
3179int Context::getMaximumTextureLevel() const
3180{
3181 return mMaxTextureLevel;
3182}
3183
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003184bool Context::supportsLuminanceTextures() const
3185{
3186 return mSupportsLuminanceTextures;
3187}
3188
3189bool Context::supportsLuminanceAlphaTextures() const
3190{
3191 return mSupportsLuminanceAlphaTextures;
3192}
3193
daniel@transgaming.com83921382011-01-08 05:46:00 +00003194bool Context::supports32bitIndices() const
3195{
3196 return mSupports32bitIndices;
3197}
3198
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003199bool Context::supportsNonPower2Texture() const
3200{
3201 return mSupportsNonPower2Texture;
3202}
3203
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003204void Context::detachBuffer(GLuint buffer)
3205{
3206 // [OpenGL ES 2.0.24] section 2.9 page 22:
3207 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3208 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3209
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003210 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003211 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003212 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003213 }
3214
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003215 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003216 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003217 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003218 }
3219
3220 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3221 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003222 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003223 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003224 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003225 }
3226 }
3227}
3228
3229void Context::detachTexture(GLuint texture)
3230{
3231 // [OpenGL ES 2.0.24] section 3.8 page 84:
3232 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3233 // rebound to texture object zero
3234
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003235 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003236 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003237 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003238 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003239 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003240 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003241 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003242 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003243 }
3244 }
3245
3246 // [OpenGL ES 2.0.24] section 4.4 page 112:
3247 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3248 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3249 // image was attached in the currently bound framebuffer.
3250
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003251 Framebuffer *readFramebuffer = getReadFramebuffer();
3252 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003253
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003254 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003255 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003256 readFramebuffer->detachTexture(texture);
3257 }
3258
3259 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3260 {
3261 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003262 }
3263}
3264
3265void Context::detachFramebuffer(GLuint framebuffer)
3266{
3267 // [OpenGL ES 2.0.24] section 4.4 page 107:
3268 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3269 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3270
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003271 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003272 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003273 bindReadFramebuffer(0);
3274 }
3275
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003276 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003277 {
3278 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003279 }
3280}
3281
3282void Context::detachRenderbuffer(GLuint renderbuffer)
3283{
3284 // [OpenGL ES 2.0.24] section 4.4 page 109:
3285 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3286 // had been executed with the target RENDERBUFFER and name of zero.
3287
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003288 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003289 {
3290 bindRenderbuffer(0);
3291 }
3292
3293 // [OpenGL ES 2.0.24] section 4.4 page 111:
3294 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3295 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3296 // point to which this image was attached in the currently bound framebuffer.
3297
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003298 Framebuffer *readFramebuffer = getReadFramebuffer();
3299 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003300
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003301 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003302 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003303 readFramebuffer->detachRenderbuffer(renderbuffer);
3304 }
3305
3306 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3307 {
3308 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003309 }
3310}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003311
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003312Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003313{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003314 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003315
3316 if (t == NULL)
3317 {
3318 static const GLubyte color[] = { 0, 0, 0, 255 };
3319
3320 switch (type)
3321 {
3322 default:
3323 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003324 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003325
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003326 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003327 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003328 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003329 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003330 t = incomplete2d;
3331 }
3332 break;
3333
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003334 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003335 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003336 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003337
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003338 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3339 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3340 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3341 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3342 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3343 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003344
3345 t = incompleteCube;
3346 }
3347 break;
3348 }
3349
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003350 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003351 }
3352
3353 return t;
3354}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003355
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003356bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003357{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003358 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003359}
3360
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003361bool Context::isTriangleMode(GLenum drawMode)
3362{
3363 switch (drawMode)
3364 {
3365 case GL_TRIANGLES:
3366 case GL_TRIANGLE_FAN:
3367 case GL_TRIANGLE_STRIP:
3368 return true;
3369 case GL_POINTS:
3370 case GL_LINES:
3371 case GL_LINE_LOOP:
3372 case GL_LINE_STRIP:
3373 return false;
3374 default: UNREACHABLE();
3375 }
3376
3377 return false;
3378}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003379
3380void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3381{
3382 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3383
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003384 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3385 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3386 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3387 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003388
daniel@transgaming.com83921382011-01-08 05:46:00 +00003389 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003390}
3391
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003392// keep list sorted in following order
3393// OES extensions
3394// EXT extensions
3395// Vendor extensions
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003396void Context::initExtensionString()
3397{
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003398 mExtensionString = "";
3399
3400 // OES extensions
3401 if (supports32bitIndices())
3402 {
3403 mExtensionString += "GL_OES_element_index_uint ";
3404 }
3405
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003406 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003407 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003408 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003409
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003410 if (supportsFloat16Textures())
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003411 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003412 mExtensionString += "GL_OES_texture_half_float ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003413 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003414 if (supportsFloat16LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003415 {
3416 mExtensionString += "GL_OES_texture_half_float_linear ";
3417 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003418 if (supportsFloat32Textures())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003419 {
3420 mExtensionString += "GL_OES_texture_float ";
3421 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003422 if (supportsFloat32LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003423 {
3424 mExtensionString += "GL_OES_texture_float_linear ";
3425 }
3426
3427 if (supportsNonPower2Texture())
3428 {
3429 mExtensionString += "GL_OES_texture_npot ";
3430 }
3431
3432 // Multi-vendor (EXT) extensions
3433 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com8747f182011-11-09 17:50:38 +00003434 mExtensionString += "GL_EXT_robustness ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003435
gman@chromium.org50c526d2011-08-10 05:19:44 +00003436 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00003437 {
3438 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3439 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003440
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003441 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
daniel@transgaming.com0bd1f2f2011-11-11 04:19:03 +00003442 mExtensionString += "GL_EXT_texture_storage ";
gman@chromium.org50c526d2011-08-10 05:19:44 +00003443
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003444 // ANGLE-specific extensions
3445 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003446 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003447 {
3448 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3449 }
3450
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003451 if (supportsDXT3Textures())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003452 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003453 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3454 }
3455 if (supportsDXT5Textures())
3456 {
3457 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003458 }
daniel@transgaming.com97412f72011-11-11 04:19:07 +00003459
3460 mExtensionString += "GL_ANGLE_texture_usage ";
zmo@google.coma574f782011-10-03 21:45:23 +00003461 mExtensionString += "GL_ANGLE_translated_shader_source ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003462
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003463 // Other vendor-specific extensions
3464 if (supportsEventQueries())
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003465 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003466 mExtensionString += "GL_NV_fence ";
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003467 }
3468
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003469 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3470 if (end != std::string::npos)
3471 {
3472 mExtensionString.resize(end+1);
3473 }
3474}
3475
3476const char *Context::getExtensionString() const
3477{
3478 return mExtensionString.c_str();
3479}
3480
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003481void Context::initRendererString()
3482{
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003483 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003484
3485 mRendererString = "ANGLE (";
3486 mRendererString += identifier->Description;
3487 mRendererString += ")";
3488}
3489
3490const char *Context::getRendererString() const
3491{
3492 return mRendererString.c_str();
3493}
3494
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003495void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3496 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3497 GLbitfield mask)
3498{
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003499 Framebuffer *readFramebuffer = getReadFramebuffer();
3500 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3501
3502 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3503 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3504 {
3505 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3506 }
3507
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003508 if (drawFramebuffer->getSamples() != 0)
3509 {
3510 return error(GL_INVALID_OPERATION);
3511 }
3512
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003513 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3514 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3515 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3516 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3517
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003518 RECT sourceRect;
3519 RECT destRect;
3520
3521 if (srcX0 < srcX1)
3522 {
3523 sourceRect.left = srcX0;
3524 sourceRect.right = srcX1;
3525 destRect.left = dstX0;
3526 destRect.right = dstX1;
3527 }
3528 else
3529 {
3530 sourceRect.left = srcX1;
3531 destRect.left = dstX1;
3532 sourceRect.right = srcX0;
3533 destRect.right = dstX0;
3534 }
3535
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003536 if (srcY0 < srcY1)
3537 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003538 sourceRect.top = readBufferHeight - srcY1;
3539 destRect.top = drawBufferHeight - dstY1;
3540 sourceRect.bottom = readBufferHeight - srcY0;
3541 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003542 }
3543 else
3544 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003545 sourceRect.top = readBufferHeight - srcY0;
3546 destRect.top = drawBufferHeight - dstY0;
3547 sourceRect.bottom = readBufferHeight - srcY1;
3548 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003549 }
3550
3551 RECT sourceScissoredRect = sourceRect;
3552 RECT destScissoredRect = destRect;
3553
3554 if (mState.scissorTest)
3555 {
3556 // Only write to parts of the destination framebuffer which pass the scissor test
3557 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3558 // rect will be checked against scissorY, rather than the bottom.
3559 if (destRect.left < mState.scissorX)
3560 {
3561 int xDiff = mState.scissorX - destRect.left;
3562 destScissoredRect.left = mState.scissorX;
3563 sourceScissoredRect.left += xDiff;
3564 }
3565
3566 if (destRect.right > mState.scissorX + mState.scissorWidth)
3567 {
3568 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3569 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3570 sourceScissoredRect.right -= xDiff;
3571 }
3572
3573 if (destRect.top < mState.scissorY)
3574 {
3575 int yDiff = mState.scissorY - destRect.top;
3576 destScissoredRect.top = mState.scissorY;
3577 sourceScissoredRect.top += yDiff;
3578 }
3579
3580 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3581 {
3582 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3583 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3584 sourceScissoredRect.bottom -= yDiff;
3585 }
3586 }
3587
3588 bool blitRenderTarget = false;
3589 bool blitDepthStencil = false;
3590
3591 RECT sourceTrimmedRect = sourceScissoredRect;
3592 RECT destTrimmedRect = destScissoredRect;
3593
3594 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3595 // the actual draw and read surfaces.
3596 if (sourceTrimmedRect.left < 0)
3597 {
3598 int xDiff = 0 - sourceTrimmedRect.left;
3599 sourceTrimmedRect.left = 0;
3600 destTrimmedRect.left += xDiff;
3601 }
3602
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003603 if (sourceTrimmedRect.right > readBufferWidth)
3604 {
3605 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3606 sourceTrimmedRect.right = readBufferWidth;
3607 destTrimmedRect.right -= xDiff;
3608 }
3609
3610 if (sourceTrimmedRect.top < 0)
3611 {
3612 int yDiff = 0 - sourceTrimmedRect.top;
3613 sourceTrimmedRect.top = 0;
3614 destTrimmedRect.top += yDiff;
3615 }
3616
3617 if (sourceTrimmedRect.bottom > readBufferHeight)
3618 {
3619 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3620 sourceTrimmedRect.bottom = readBufferHeight;
3621 destTrimmedRect.bottom -= yDiff;
3622 }
3623
3624 if (destTrimmedRect.left < 0)
3625 {
3626 int xDiff = 0 - destTrimmedRect.left;
3627 destTrimmedRect.left = 0;
3628 sourceTrimmedRect.left += xDiff;
3629 }
3630
3631 if (destTrimmedRect.right > drawBufferWidth)
3632 {
3633 int xDiff = destTrimmedRect.right - drawBufferWidth;
3634 destTrimmedRect.right = drawBufferWidth;
3635 sourceTrimmedRect.right -= xDiff;
3636 }
3637
3638 if (destTrimmedRect.top < 0)
3639 {
3640 int yDiff = 0 - destTrimmedRect.top;
3641 destTrimmedRect.top = 0;
3642 sourceTrimmedRect.top += yDiff;
3643 }
3644
3645 if (destTrimmedRect.bottom > drawBufferHeight)
3646 {
3647 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3648 destTrimmedRect.bottom = drawBufferHeight;
3649 sourceTrimmedRect.bottom -= yDiff;
3650 }
3651
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003652 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003653 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3654 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3655 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3656 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003657 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3658 {
3659 partialBufferCopy = true;
3660 }
3661
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003662 if (mask & GL_COLOR_BUFFER_BIT)
3663 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003664 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3665 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3666 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3667 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3668 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003669 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3670 {
3671 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3672 return error(GL_INVALID_OPERATION);
3673 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003674
3675 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3676 {
3677 return error(GL_INVALID_OPERATION);
3678 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003679
3680 blitRenderTarget = true;
3681
3682 }
3683
3684 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3685 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00003686 Renderbuffer *readDSBuffer = NULL;
3687 Renderbuffer *drawDSBuffer = NULL;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003688
3689 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3690 // both a depth and stencil buffer, it will be the same buffer.
3691
3692 if (mask & GL_DEPTH_BUFFER_BIT)
3693 {
3694 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3695 {
3696 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3697 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3698 {
3699 return error(GL_INVALID_OPERATION);
3700 }
3701
3702 blitDepthStencil = true;
3703 readDSBuffer = readFramebuffer->getDepthbuffer();
3704 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3705 }
3706 }
3707
3708 if (mask & GL_STENCIL_BUFFER_BIT)
3709 {
3710 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3711 {
3712 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3713 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3714 {
3715 return error(GL_INVALID_OPERATION);
3716 }
3717
3718 blitDepthStencil = true;
3719 readDSBuffer = readFramebuffer->getStencilbuffer();
3720 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3721 }
3722 }
3723
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003724 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003725 {
3726 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3727 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3728 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003729
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003730 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3731 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003732 {
3733 return error(GL_INVALID_OPERATION);
3734 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003735 }
3736
3737 if (blitRenderTarget || blitDepthStencil)
3738 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003739 mDisplay->endScene();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003740
3741 if (blitRenderTarget)
3742 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003743 HRESULT result = mDevice->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003744 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3745
3746 if (FAILED(result))
3747 {
3748 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3749 return;
3750 }
3751 }
3752
3753 if (blitDepthStencil)
3754 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003755 HRESULT result = mDevice->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003756
3757 if (FAILED(result))
3758 {
3759 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3760 return;
3761 }
3762 }
3763 }
3764}
3765
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003766VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
3767{
3768 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3769 {
3770 mVertexDeclCache[i].vertexDeclaration = NULL;
3771 mVertexDeclCache[i].lruCount = 0;
3772 }
3773}
3774
3775VertexDeclarationCache::~VertexDeclarationCache()
3776{
3777 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3778 {
3779 if (mVertexDeclCache[i].vertexDeclaration)
3780 {
3781 mVertexDeclCache[i].vertexDeclaration->Release();
3782 }
3783 }
3784}
3785
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003786GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], Program *program)
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003787{
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003788 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
3789 D3DVERTEXELEMENT9 *element = &elements[0];
3790
3791 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3792 {
3793 if (attributes[i].active)
3794 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003795 if (mAppliedVBs[i].serial != attributes[i].serial ||
3796 mAppliedVBs[i].stride != attributes[i].stride ||
3797 mAppliedVBs[i].offset != attributes[i].offset)
3798 {
3799 device->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
3800 mAppliedVBs[i].serial = attributes[i].serial;
3801 mAppliedVBs[i].stride = attributes[i].stride;
3802 mAppliedVBs[i].offset = attributes[i].offset;
3803 }
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003804
3805 element->Stream = i;
3806 element->Offset = 0;
3807 element->Type = attributes[i].type;
3808 element->Method = D3DDECLMETHOD_DEFAULT;
3809 element->Usage = D3DDECLUSAGE_TEXCOORD;
3810 element->UsageIndex = program->getSemanticIndex(i);
3811 element++;
3812 }
3813 }
3814
3815 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
3816 *(element++) = end;
3817
3818 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3819 {
3820 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
3821 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
3822 {
3823 entry->lruCount = ++mMaxLru;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003824 if(entry->vertexDeclaration != mLastSetVDecl)
3825 {
3826 device->SetVertexDeclaration(entry->vertexDeclaration);
3827 mLastSetVDecl = entry->vertexDeclaration;
3828 }
3829
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003830 return GL_NO_ERROR;
3831 }
3832 }
3833
3834 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
3835
3836 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3837 {
3838 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
3839 {
3840 lastCache = &mVertexDeclCache[i];
3841 }
3842 }
3843
3844 if (lastCache->vertexDeclaration != NULL)
3845 {
3846 lastCache->vertexDeclaration->Release();
3847 lastCache->vertexDeclaration = NULL;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003848 // mLastSetVDecl is set to the replacement, so we don't have to worry
3849 // about it.
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003850 }
3851
3852 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
3853 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
3854 device->SetVertexDeclaration(lastCache->vertexDeclaration);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003855 mLastSetVDecl = lastCache->vertexDeclaration;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003856 lastCache->lruCount = ++mMaxLru;
3857
3858 return GL_NO_ERROR;
3859}
3860
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003861void VertexDeclarationCache::markStateDirty()
3862{
3863 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3864 {
3865 mAppliedVBs[i].serial = 0;
3866 }
3867
3868 mLastSetVDecl = NULL;
3869}
3870
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003871}
3872
3873extern "C"
3874{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00003875gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext, bool notifyResets, bool robustAccess)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003876{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00003877 return new gl::Context(config, shareContext, notifyResets, robustAccess);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003878}
3879
3880void glDestroyContext(gl::Context *context)
3881{
3882 delete context;
3883
3884 if (context == gl::getContext())
3885 {
3886 gl::makeCurrent(NULL, NULL, NULL);
3887 }
3888}
3889
3890void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3891{
3892 gl::makeCurrent(context, display, surface);
3893}
3894
3895gl::Context *glGetCurrentContext()
3896{
3897 return gl::getContext();
3898}
3899}