blob: bae4479e06e159048ea460ece25472c13b7b0c84 [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.com09c2c1a2011-04-13 14:57:16 +000041Context::Context(const egl::Config *config, const gl::Context *shareContext) : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000042{
daniel@transgaming.comc941e252011-10-26 02:32:31 +000043 mDisplay = NULL;
44 mDevice = NULL;
45
benvanik@google.com1a233342011-04-28 19:44:39 +000046 mFenceHandleAllocator.setBaseHandle(0);
47
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000048 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000049
daniel@transgaming.com428d1582010-05-04 03:35:25 +000050 mState.depthClearValue = 1.0f;
51 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000052
daniel@transgaming.com428d1582010-05-04 03:35:25 +000053 mState.cullFace = false;
54 mState.cullMode = GL_BACK;
55 mState.frontFace = GL_CCW;
56 mState.depthTest = false;
57 mState.depthFunc = GL_LESS;
58 mState.blend = false;
59 mState.sourceBlendRGB = GL_ONE;
60 mState.sourceBlendAlpha = GL_ONE;
61 mState.destBlendRGB = GL_ZERO;
62 mState.destBlendAlpha = GL_ZERO;
63 mState.blendEquationRGB = GL_FUNC_ADD;
64 mState.blendEquationAlpha = GL_FUNC_ADD;
65 mState.blendColor.red = 0;
66 mState.blendColor.green = 0;
67 mState.blendColor.blue = 0;
68 mState.blendColor.alpha = 0;
69 mState.stencilTest = false;
70 mState.stencilFunc = GL_ALWAYS;
71 mState.stencilRef = 0;
72 mState.stencilMask = -1;
73 mState.stencilWritemask = -1;
74 mState.stencilBackFunc = GL_ALWAYS;
75 mState.stencilBackRef = 0;
76 mState.stencilBackMask = - 1;
77 mState.stencilBackWritemask = -1;
78 mState.stencilFail = GL_KEEP;
79 mState.stencilPassDepthFail = GL_KEEP;
80 mState.stencilPassDepthPass = GL_KEEP;
81 mState.stencilBackFail = GL_KEEP;
82 mState.stencilBackPassDepthFail = GL_KEEP;
83 mState.stencilBackPassDepthPass = GL_KEEP;
84 mState.polygonOffsetFill = false;
85 mState.polygonOffsetFactor = 0.0f;
86 mState.polygonOffsetUnits = 0.0f;
87 mState.sampleAlphaToCoverage = false;
88 mState.sampleCoverage = false;
89 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000090 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000091 mState.scissorTest = false;
92 mState.dither = true;
93 mState.generateMipmapHint = GL_DONT_CARE;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +000094 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095
daniel@transgaming.com428d1582010-05-04 03:35:25 +000096 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000097
daniel@transgaming.com428d1582010-05-04 03:35:25 +000098 mState.viewportX = 0;
99 mState.viewportY = 0;
100 mState.viewportWidth = config->mDisplayMode.Width;
101 mState.viewportHeight = config->mDisplayMode.Height;
102 mState.zNear = 0.0f;
103 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000104
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000105 mState.scissorX = 0;
106 mState.scissorY = 0;
107 mState.scissorWidth = config->mDisplayMode.Width;
108 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000109
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000110 mState.colorMaskRed = true;
111 mState.colorMaskGreen = true;
112 mState.colorMaskBlue = true;
113 mState.colorMaskAlpha = true;
114 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000115
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000116 if (shareContext != NULL)
117 {
118 mResourceManager = shareContext->mResourceManager;
119 mResourceManager->addRef();
120 }
121 else
122 {
123 mResourceManager = new ResourceManager();
124 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000125
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000126 // [OpenGL ES 2.0.24] section 3.7 page 83:
127 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
128 // and cube map texture state vectors respectively associated with them.
129 // In order that access to these initial textures not be lost, they are treated as texture
130 // objects all of whose names are 0.
131
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000132 mTexture2DZero.set(new Texture2D(0));
133 mTextureCubeMapZero.set(new TextureCubeMap(0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000134
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000135 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000136 bindArrayBuffer(0);
137 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000138 bindTextureCubeMap(0);
139 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000140 bindReadFramebuffer(0);
141 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000142 bindRenderbuffer(0);
143
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000144 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000145
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000146 mState.packAlignment = 4;
147 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000148
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000149 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000150 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000151 mBlit = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000152 mClosingIB = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000153
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000154 mInvalidEnum = false;
155 mInvalidValue = false;
156 mInvalidOperation = false;
157 mOutOfMemory = false;
158 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000159
160 mHasBeenCurrent = false;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000161 mContextLost = false;
daniel@transgaming.com17f548c2011-11-09 17:47:02 +0000162 mResetStatus = GL_NO_ERROR;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000163
gman@chromium.org50c526d2011-08-10 05:19:44 +0000164 mSupportsDXT1Textures = false;
165 mSupportsDXT3Textures = false;
166 mSupportsDXT5Textures = false;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000167 mSupportsEventQueries = false;
gman@chromium.org50c526d2011-08-10 05:19:44 +0000168 mNumCompressedTextureFormats = 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000169 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000170 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000171 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000172}
173
174Context::~Context()
175{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000176 if (mState.currentProgram != 0)
177 {
178 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
179 if (programObject)
180 {
181 programObject->release();
182 }
183 mState.currentProgram = 0;
184 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000185
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000186 while (!mFramebufferMap.empty())
187 {
188 deleteFramebuffer(mFramebufferMap.begin()->first);
189 }
190
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000191 while (!mFenceMap.empty())
192 {
193 deleteFence(mFenceMap.begin()->first);
194 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000195
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000196 while (!mMultiSampleSupport.empty())
197 {
198 delete [] mMultiSampleSupport.begin()->second;
199 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
200 }
201
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000202 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000203 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +0000204 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000205 {
206 mState.samplerTexture[type][sampler].set(NULL);
207 }
208 }
209
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000210 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000211 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000212 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000213 }
214
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000215 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
216 {
217 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
218 }
219
220 mState.arrayBuffer.set(NULL);
221 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000222 mState.renderbuffer.set(NULL);
223
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000224 mTexture2DZero.set(NULL);
225 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000226
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000227 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000228 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000229 delete mBlit;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000230 delete mClosingIB;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000231
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000232 if (mMaskedClearSavedState)
233 {
234 mMaskedClearSavedState->Release();
235 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000236
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000237 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000238}
239
240void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
241{
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000242 mDisplay = display;
243 mDevice = mDisplay->getDevice();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000244
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000245 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000246 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000247 mDeviceCaps = mDisplay->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000248
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000249 mVertexDataManager = new VertexDataManager(this, mDevice);
250 mIndexDataManager = new IndexDataManager(this, mDevice);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000251 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000252
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000253 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000254 mSupportsVertexTexture = mDisplay->getVertexTextureSupport();
255 mSupportsNonPower2Texture = mDisplay->getNonPower2TextureSupport();
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000256
257 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
258 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
259 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
260 mMaxRenderbufferDimension = mMaxTextureDimension;
261 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
262 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
263 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
264
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000265 const D3DFORMAT renderBufferFormats[] =
266 {
267 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000268 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000269 D3DFMT_R5G6B5,
270 D3DFMT_D24S8
271 };
272
273 int max = 0;
274 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
275 {
276 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000277 mDisplay->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000278 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
279
280 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
281 {
282 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
283 {
284 max = j;
285 }
286 }
287 }
288
289 mMaxSupportedSamples = max;
290
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000291 mSupportsEventQueries = mDisplay->getEventQuerySupport();
292 mSupportsDXT1Textures = mDisplay->getDXT1TextureSupport();
293 mSupportsDXT3Textures = mDisplay->getDXT3TextureSupport();
294 mSupportsDXT5Textures = mDisplay->getDXT5TextureSupport();
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +0000295 mSupportsFloat32Textures = mDisplay->getFloat32TextureSupport(&mSupportsFloat32LinearFilter, &mSupportsFloat32RenderableTextures);
296 mSupportsFloat16Textures = mDisplay->getFloat16TextureSupport(&mSupportsFloat16LinearFilter, &mSupportsFloat16RenderableTextures);
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000297 mSupportsLuminanceTextures = mDisplay->getLuminanceTextureSupport();
298 mSupportsLuminanceAlphaTextures = mDisplay->getLuminanceAlphaTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000299
daniel@transgaming.com83921382011-01-08 05:46:00 +0000300 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
301
gman@chromium.org50c526d2011-08-10 05:19:44 +0000302 mNumCompressedTextureFormats = 0;
303 if (supportsDXT1Textures())
304 {
305 mNumCompressedTextureFormats += 2;
306 }
307 if (supportsDXT3Textures())
308 {
309 mNumCompressedTextureFormats += 1;
310 }
311 if (supportsDXT5Textures())
312 {
313 mNumCompressedTextureFormats += 1;
314 }
315
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000316 initExtensionString();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +0000317 initRendererString();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000318
319 mState.viewportX = 0;
320 mState.viewportY = 0;
321 mState.viewportWidth = surface->getWidth();
322 mState.viewportHeight = surface->getHeight();
323
324 mState.scissorX = 0;
325 mState.scissorY = 0;
326 mState.scissorWidth = surface->getWidth();
327 mState.scissorHeight = surface->getHeight();
328
329 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000330 }
331
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000332 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
333 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000334 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000335
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000336 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000337 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000338 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000339
340 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000341
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000342 if (defaultRenderTarget)
343 {
344 defaultRenderTarget->Release();
345 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000346
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000347 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000348 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000349 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000350 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000351
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000352 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000353}
354
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000355// 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 +0000356void Context::markAllStateDirty()
357{
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000358 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
359 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +0000360 mAppliedTextureSerialPS[t] = 0;
361 }
362
363 for (int t = 0; t < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; t++)
364 {
365 mAppliedTextureSerialVS[t] = 0;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000366 }
367
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000368 mAppliedProgramSerial = 0;
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000369 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000370 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000371 mAppliedStencilbufferSerial = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000372 mAppliedIBSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000373 mDepthStencilInitialized = false;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000374 mViewportInitialized = false;
375 mRenderTargetDescInitialized = false;
376
377 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000378
379 mClearStateDirty = true;
380 mCullStateDirty = true;
381 mDepthStateDirty = true;
382 mMaskStateDirty = true;
383 mBlendStateDirty = true;
384 mStencilStateDirty = true;
385 mPolygonOffsetStateDirty = true;
386 mScissorStateDirty = true;
387 mSampleStateDirty = true;
388 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000389 mFrontFaceDirty = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +0000390 mDxUniformsDirty = true;
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000391 mCachedCurrentProgram = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000392}
393
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000394void Context::markContextLost()
395{
daniel@transgaming.com17f548c2011-11-09 17:47:02 +0000396 mResetStatus = GL_UNKNOWN_CONTEXT_RESET_EXT;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000397 mContextLost = true;
398}
399
400bool Context::isContextLost()
401{
402 return mContextLost;
403}
404
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000405void Context::setClearColor(float red, float green, float blue, float alpha)
406{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000407 mState.colorClearValue.red = red;
408 mState.colorClearValue.green = green;
409 mState.colorClearValue.blue = blue;
410 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000411}
412
413void Context::setClearDepth(float depth)
414{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000415 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000416}
417
418void Context::setClearStencil(int stencil)
419{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000420 mState.stencilClearValue = stencil;
421}
422
423void Context::setCullFace(bool enabled)
424{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000425 if (mState.cullFace != enabled)
426 {
427 mState.cullFace = enabled;
428 mCullStateDirty = true;
429 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000430}
431
432bool Context::isCullFaceEnabled() const
433{
434 return mState.cullFace;
435}
436
437void Context::setCullMode(GLenum mode)
438{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000439 if (mState.cullMode != mode)
440 {
441 mState.cullMode = mode;
442 mCullStateDirty = true;
443 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000444}
445
446void Context::setFrontFace(GLenum front)
447{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000448 if (mState.frontFace != front)
449 {
450 mState.frontFace = front;
451 mFrontFaceDirty = true;
452 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000453}
454
455void Context::setDepthTest(bool enabled)
456{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000457 if (mState.depthTest != enabled)
458 {
459 mState.depthTest = enabled;
460 mDepthStateDirty = true;
461 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000462}
463
464bool Context::isDepthTestEnabled() const
465{
466 return mState.depthTest;
467}
468
469void Context::setDepthFunc(GLenum depthFunc)
470{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000471 if (mState.depthFunc != depthFunc)
472 {
473 mState.depthFunc = depthFunc;
474 mDepthStateDirty = true;
475 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000476}
477
478void Context::setDepthRange(float zNear, float zFar)
479{
480 mState.zNear = zNear;
481 mState.zFar = zFar;
482}
483
484void Context::setBlend(bool enabled)
485{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000486 if (mState.blend != enabled)
487 {
488 mState.blend = enabled;
489 mBlendStateDirty = true;
490 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000491}
492
493bool Context::isBlendEnabled() const
494{
495 return mState.blend;
496}
497
498void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
499{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000500 if (mState.sourceBlendRGB != sourceRGB ||
501 mState.sourceBlendAlpha != sourceAlpha ||
502 mState.destBlendRGB != destRGB ||
503 mState.destBlendAlpha != destAlpha)
504 {
505 mState.sourceBlendRGB = sourceRGB;
506 mState.destBlendRGB = destRGB;
507 mState.sourceBlendAlpha = sourceAlpha;
508 mState.destBlendAlpha = destAlpha;
509 mBlendStateDirty = true;
510 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000511}
512
513void Context::setBlendColor(float red, float green, float blue, float alpha)
514{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000515 if (mState.blendColor.red != red ||
516 mState.blendColor.green != green ||
517 mState.blendColor.blue != blue ||
518 mState.blendColor.alpha != alpha)
519 {
520 mState.blendColor.red = red;
521 mState.blendColor.green = green;
522 mState.blendColor.blue = blue;
523 mState.blendColor.alpha = alpha;
524 mBlendStateDirty = true;
525 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000526}
527
528void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
529{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000530 if (mState.blendEquationRGB != rgbEquation ||
531 mState.blendEquationAlpha != alphaEquation)
532 {
533 mState.blendEquationRGB = rgbEquation;
534 mState.blendEquationAlpha = alphaEquation;
535 mBlendStateDirty = true;
536 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000537}
538
539void Context::setStencilTest(bool enabled)
540{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000541 if (mState.stencilTest != enabled)
542 {
543 mState.stencilTest = enabled;
544 mStencilStateDirty = true;
545 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000546}
547
548bool Context::isStencilTestEnabled() const
549{
550 return mState.stencilTest;
551}
552
553void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
554{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000555 if (mState.stencilFunc != stencilFunc ||
556 mState.stencilRef != stencilRef ||
557 mState.stencilMask != stencilMask)
558 {
559 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000560 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000561 mState.stencilMask = stencilMask;
562 mStencilStateDirty = true;
563 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000564}
565
566void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
567{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000568 if (mState.stencilBackFunc != stencilBackFunc ||
569 mState.stencilBackRef != stencilBackRef ||
570 mState.stencilBackMask != stencilBackMask)
571 {
572 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000573 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000574 mState.stencilBackMask = stencilBackMask;
575 mStencilStateDirty = true;
576 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000577}
578
579void Context::setStencilWritemask(GLuint stencilWritemask)
580{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000581 if (mState.stencilWritemask != stencilWritemask)
582 {
583 mState.stencilWritemask = stencilWritemask;
584 mStencilStateDirty = true;
585 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000586}
587
588void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
589{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000590 if (mState.stencilBackWritemask != stencilBackWritemask)
591 {
592 mState.stencilBackWritemask = stencilBackWritemask;
593 mStencilStateDirty = true;
594 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000595}
596
597void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
598{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000599 if (mState.stencilFail != stencilFail ||
600 mState.stencilPassDepthFail != stencilPassDepthFail ||
601 mState.stencilPassDepthPass != stencilPassDepthPass)
602 {
603 mState.stencilFail = stencilFail;
604 mState.stencilPassDepthFail = stencilPassDepthFail;
605 mState.stencilPassDepthPass = stencilPassDepthPass;
606 mStencilStateDirty = true;
607 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000608}
609
610void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
611{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000612 if (mState.stencilBackFail != stencilBackFail ||
613 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
614 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
615 {
616 mState.stencilBackFail = stencilBackFail;
617 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
618 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
619 mStencilStateDirty = true;
620 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000621}
622
623void Context::setPolygonOffsetFill(bool enabled)
624{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000625 if (mState.polygonOffsetFill != enabled)
626 {
627 mState.polygonOffsetFill = enabled;
628 mPolygonOffsetStateDirty = true;
629 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000630}
631
632bool Context::isPolygonOffsetFillEnabled() const
633{
634 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000635
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000636}
637
638void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
639{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000640 if (mState.polygonOffsetFactor != factor ||
641 mState.polygonOffsetUnits != units)
642 {
643 mState.polygonOffsetFactor = factor;
644 mState.polygonOffsetUnits = units;
645 mPolygonOffsetStateDirty = true;
646 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000647}
648
649void Context::setSampleAlphaToCoverage(bool enabled)
650{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000651 if (mState.sampleAlphaToCoverage != enabled)
652 {
653 mState.sampleAlphaToCoverage = enabled;
654 mSampleStateDirty = true;
655 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000656}
657
658bool Context::isSampleAlphaToCoverageEnabled() const
659{
660 return mState.sampleAlphaToCoverage;
661}
662
663void Context::setSampleCoverage(bool enabled)
664{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000665 if (mState.sampleCoverage != enabled)
666 {
667 mState.sampleCoverage = enabled;
668 mSampleStateDirty = true;
669 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000670}
671
672bool Context::isSampleCoverageEnabled() const
673{
674 return mState.sampleCoverage;
675}
676
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000677void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000678{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000679 if (mState.sampleCoverageValue != value ||
680 mState.sampleCoverageInvert != invert)
681 {
682 mState.sampleCoverageValue = value;
683 mState.sampleCoverageInvert = invert;
684 mSampleStateDirty = true;
685 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000686}
687
688void Context::setScissorTest(bool enabled)
689{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000690 if (mState.scissorTest != enabled)
691 {
692 mState.scissorTest = enabled;
693 mScissorStateDirty = true;
694 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000695}
696
697bool Context::isScissorTestEnabled() const
698{
699 return mState.scissorTest;
700}
701
702void Context::setDither(bool enabled)
703{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000704 if (mState.dither != enabled)
705 {
706 mState.dither = enabled;
707 mDitherStateDirty = true;
708 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000709}
710
711bool Context::isDitherEnabled() const
712{
713 return mState.dither;
714}
715
716void Context::setLineWidth(GLfloat width)
717{
718 mState.lineWidth = width;
719}
720
721void Context::setGenerateMipmapHint(GLenum hint)
722{
723 mState.generateMipmapHint = hint;
724}
725
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000726void Context::setFragmentShaderDerivativeHint(GLenum hint)
727{
728 mState.fragmentShaderDerivativeHint = hint;
729 // TODO: Propagate the hint to shader translator so we can write
730 // ddx, ddx_coarse, or ddx_fine depending on the hint.
731 // Ignore for now. It is valid for implementations to ignore hint.
732}
733
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000734void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
735{
736 mState.viewportX = x;
737 mState.viewportY = y;
738 mState.viewportWidth = width;
739 mState.viewportHeight = height;
740}
741
742void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
743{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000744 if (mState.scissorX != x || mState.scissorY != y ||
745 mState.scissorWidth != width || mState.scissorHeight != height)
746 {
747 mState.scissorX = x;
748 mState.scissorY = y;
749 mState.scissorWidth = width;
750 mState.scissorHeight = height;
751 mScissorStateDirty = true;
752 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000753}
754
755void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
756{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000757 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
758 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
759 {
760 mState.colorMaskRed = red;
761 mState.colorMaskGreen = green;
762 mState.colorMaskBlue = blue;
763 mState.colorMaskAlpha = alpha;
764 mMaskStateDirty = true;
765 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000766}
767
768void Context::setDepthMask(bool mask)
769{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000770 if (mState.depthMask != mask)
771 {
772 mState.depthMask = mask;
773 mMaskStateDirty = true;
774 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000775}
776
daniel@transgaming.comdfd57022011-05-11 15:37:25 +0000777void Context::setActiveSampler(unsigned int active)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000778{
779 mState.activeSampler = active;
780}
781
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000782GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000783{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000784 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000785}
786
787GLuint Context::getDrawFramebufferHandle() const
788{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000789 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000790}
791
792GLuint Context::getRenderbufferHandle() const
793{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000794 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000795}
796
797GLuint Context::getArrayBufferHandle() const
798{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000799 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000800}
801
daniel@transgaming.com83921382011-01-08 05:46:00 +0000802void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000803{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000804 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000805}
806
daniel@transgaming.com83921382011-01-08 05:46:00 +0000807const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000808{
809 return mState.vertexAttribute[attribNum];
810}
811
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000812void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000813 GLsizei stride, const void *pointer)
814{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000815 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000816 mState.vertexAttribute[attribNum].mSize = size;
817 mState.vertexAttribute[attribNum].mType = type;
818 mState.vertexAttribute[attribNum].mNormalized = normalized;
819 mState.vertexAttribute[attribNum].mStride = stride;
820 mState.vertexAttribute[attribNum].mPointer = pointer;
821}
822
823const void *Context::getVertexAttribPointer(unsigned int attribNum) const
824{
825 return mState.vertexAttribute[attribNum].mPointer;
826}
827
daniel@transgaming.com83921382011-01-08 05:46:00 +0000828const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000829{
830 return mState.vertexAttribute;
831}
832
833void Context::setPackAlignment(GLint alignment)
834{
835 mState.packAlignment = alignment;
836}
837
838GLint Context::getPackAlignment() const
839{
840 return mState.packAlignment;
841}
842
843void Context::setUnpackAlignment(GLint alignment)
844{
845 mState.unpackAlignment = alignment;
846}
847
848GLint Context::getUnpackAlignment() const
849{
850 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851}
852
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853GLuint Context::createBuffer()
854{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000855 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856}
857
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858GLuint Context::createProgram()
859{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000860 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861}
862
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000863GLuint Context::createShader(GLenum type)
864{
865 return mResourceManager->createShader(type);
866}
867
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000868GLuint Context::createTexture()
869{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000870 return mResourceManager->createTexture();
871}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000872
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000873GLuint Context::createRenderbuffer()
874{
875 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000876}
877
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000878// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000879GLuint Context::createFramebuffer()
880{
benvanik@google.com1a233342011-04-28 19:44:39 +0000881 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000882
883 mFramebufferMap[handle] = NULL;
884
885 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000886}
887
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000888GLuint Context::createFence()
889{
benvanik@google.com1a233342011-04-28 19:44:39 +0000890 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000891
892 mFenceMap[handle] = new Fence;
893
894 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000895}
896
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897void Context::deleteBuffer(GLuint buffer)
898{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000899 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000900 {
901 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000902 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000903
904 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000905}
906
907void Context::deleteShader(GLuint shader)
908{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000909 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910}
911
912void Context::deleteProgram(GLuint program)
913{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000914 mResourceManager->deleteProgram(program);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000915 mCachedCurrentProgram = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000916}
917
918void Context::deleteTexture(GLuint texture)
919{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000920 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000921 {
922 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000923 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000924
925 mResourceManager->deleteTexture(texture);
926}
927
928void Context::deleteRenderbuffer(GLuint renderbuffer)
929{
930 if (mResourceManager->getRenderbuffer(renderbuffer))
931 {
932 detachRenderbuffer(renderbuffer);
933 }
934
935 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000936}
937
938void Context::deleteFramebuffer(GLuint framebuffer)
939{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000940 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
941
942 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000943 {
944 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000945
benvanik@google.com1a233342011-04-28 19:44:39 +0000946 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000947 delete framebufferObject->second;
948 mFramebufferMap.erase(framebufferObject);
949 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000950}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000951
952void Context::deleteFence(GLuint fence)
953{
954 FenceMap::iterator fenceObject = mFenceMap.find(fence);
955
956 if (fenceObject != mFenceMap.end())
957 {
benvanik@google.com1a233342011-04-28 19:44:39 +0000958 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000959 delete fenceObject->second;
960 mFenceMap.erase(fenceObject);
961 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000962}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000964Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000965{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000966 return mResourceManager->getBuffer(handle);
967}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000968
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000969Shader *Context::getShader(GLuint handle)
970{
971 return mResourceManager->getShader(handle);
972}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000974Program *Context::getProgram(GLuint handle)
975{
976 return mResourceManager->getProgram(handle);
977}
978
979Texture *Context::getTexture(GLuint handle)
980{
981 return mResourceManager->getTexture(handle);
982}
983
984Renderbuffer *Context::getRenderbuffer(GLuint handle)
985{
986 return mResourceManager->getRenderbuffer(handle);
987}
988
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000989Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000990{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000991 return getFramebuffer(mState.readFramebuffer);
992}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000993
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000994Framebuffer *Context::getDrawFramebuffer()
995{
jbauman@chromium.org040c4db2011-10-13 21:35:52 +0000996 return mBoundDrawFramebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000997}
998
999void Context::bindArrayBuffer(unsigned int buffer)
1000{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001001 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001002
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001003 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001004}
1005
1006void Context::bindElementArrayBuffer(unsigned int buffer)
1007{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001008 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001009
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001010 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001011}
1012
1013void Context::bindTexture2D(GLuint texture)
1014{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001015 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001016
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001017 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001018}
1019
1020void Context::bindTextureCubeMap(GLuint texture)
1021{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001022 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001023
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001024 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001025}
1026
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001027void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001029 if (!getFramebuffer(framebuffer))
1030 {
1031 mFramebufferMap[framebuffer] = new Framebuffer();
1032 }
1033
1034 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001035}
1036
1037void Context::bindDrawFramebuffer(GLuint framebuffer)
1038{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001039 if (!getFramebuffer(framebuffer))
1040 {
1041 mFramebufferMap[framebuffer] = new Framebuffer();
1042 }
1043
1044 mState.drawFramebuffer = framebuffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001045
1046 mBoundDrawFramebuffer = getFramebuffer(framebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001047}
1048
1049void Context::bindRenderbuffer(GLuint renderbuffer)
1050{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001051 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1052
1053 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001054}
1055
1056void Context::useProgram(GLuint program)
1057{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001058 GLuint priorProgram = mState.currentProgram;
1059 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001060
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001061 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001062 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001063 Program *newProgram = mResourceManager->getProgram(program);
1064 Program *oldProgram = mResourceManager->getProgram(priorProgram);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001065 mCachedCurrentProgram = NULL;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001066 mDxUniformsDirty = true;
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001067
1068 if (newProgram)
1069 {
1070 newProgram->addRef();
1071 }
1072
1073 if (oldProgram)
1074 {
1075 oldProgram->release();
1076 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001077 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001078}
1079
1080void Context::setFramebufferZero(Framebuffer *buffer)
1081{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001082 delete mFramebufferMap[0];
1083 mFramebufferMap[0] = buffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001084 if (mState.drawFramebuffer == 0)
1085 {
1086 mBoundDrawFramebuffer = buffer;
1087 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001088}
1089
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001090void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001091{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001092 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1093 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001094}
1095
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001096Framebuffer *Context::getFramebuffer(unsigned int handle)
1097{
1098 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1099
1100 if (framebuffer == mFramebufferMap.end())
1101 {
1102 return NULL;
1103 }
1104 else
1105 {
1106 return framebuffer->second;
1107 }
1108}
1109
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001110Fence *Context::getFence(unsigned int handle)
1111{
1112 FenceMap::iterator fence = mFenceMap.find(handle);
1113
1114 if (fence == mFenceMap.end())
1115 {
1116 return NULL;
1117 }
1118 else
1119 {
1120 return fence->second;
1121 }
1122}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001123
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001124Buffer *Context::getArrayBuffer()
1125{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001126 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001127}
1128
1129Buffer *Context::getElementArrayBuffer()
1130{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001131 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001132}
1133
1134Program *Context::getCurrentProgram()
1135{
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001136 if (!mCachedCurrentProgram)
1137 {
1138 mCachedCurrentProgram = mResourceManager->getProgram(mState.currentProgram);
1139 }
1140 return mCachedCurrentProgram;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001141}
1142
1143Texture2D *Context::getTexture2D()
1144{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001145 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001146}
1147
1148TextureCubeMap *Context::getTextureCubeMap()
1149{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001150 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001151}
1152
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001153Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001154{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001155 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001156
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001157 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001158 {
1159 switch (type)
1160 {
1161 default: UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001162 case TEXTURE_2D: return mTexture2DZero.get();
1163 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001164 }
1165 }
1166
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001167 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001168}
1169
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001170bool Context::getBooleanv(GLenum pname, GLboolean *params)
1171{
1172 switch (pname)
1173 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001174 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1175 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1176 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001177 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001178 params[0] = mState.colorMaskRed;
1179 params[1] = mState.colorMaskGreen;
1180 params[2] = mState.colorMaskBlue;
1181 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001182 break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001183 case GL_CULL_FACE: *params = mState.cullFace; break;
1184 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1185 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1186 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1187 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1188 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1189 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1190 case GL_BLEND: *params = mState.blend; break;
1191 case GL_DITHER: *params = mState.dither; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001192 default:
1193 return false;
1194 }
1195
1196 return true;
1197}
1198
1199bool Context::getFloatv(GLenum pname, GLfloat *params)
1200{
1201 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1202 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1203 // GetIntegerv as its native query function. As it would require conversion in any
1204 // case, this should make no difference to the calling application.
1205 switch (pname)
1206 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001207 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1208 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1209 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1210 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1211 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001212 case GL_ALIASED_LINE_WIDTH_RANGE:
1213 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1214 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1215 break;
1216 case GL_ALIASED_POINT_SIZE_RANGE:
1217 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001218 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 +00001219 break;
1220 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001221 params[0] = mState.zNear;
1222 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001223 break;
1224 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001225 params[0] = mState.colorClearValue.red;
1226 params[1] = mState.colorClearValue.green;
1227 params[2] = mState.colorClearValue.blue;
1228 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001229 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001230 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001231 params[0] = mState.blendColor.red;
1232 params[1] = mState.blendColor.green;
1233 params[2] = mState.blendColor.blue;
1234 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001235 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001236 default:
1237 return false;
1238 }
1239
1240 return true;
1241}
1242
1243bool Context::getIntegerv(GLenum pname, GLint *params)
1244{
1245 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1246 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1247 // GetIntegerv as its native query function. As it would require conversion in any
1248 // case, this should make no difference to the calling application. You may find it in
1249 // Context::getFloatv.
1250 switch (pname)
1251 {
1252 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1253 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001254 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00001255 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1256 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001257 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001258 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001259 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001260 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001261 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001262 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1263 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001264 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1265 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1266 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001267 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001268 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1269 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1270 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1271 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001272 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001273 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1274 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1275 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1276 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1277 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1278 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1279 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1280 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1281 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1282 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1283 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1284 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1285 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1286 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1287 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1288 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1289 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1290 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1291 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1292 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1293 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1294 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1295 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1296 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001297 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1298 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001299 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
gman@chromium.org50c526d2011-08-10 05:19:44 +00001300 params[0] = mNumCompressedTextureFormats;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001301 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001302 case GL_MAX_SAMPLES_ANGLE:
1303 {
1304 GLsizei maxSamples = getMaxSupportedSamples();
1305 if (maxSamples != 0)
1306 {
1307 *params = maxSamples;
1308 }
1309 else
1310 {
1311 return false;
1312 }
1313
1314 break;
1315 }
1316 case GL_SAMPLE_BUFFERS:
1317 case GL_SAMPLES:
1318 {
1319 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1320 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1321 {
1322 switch (pname)
1323 {
1324 case GL_SAMPLE_BUFFERS:
1325 if (framebuffer->getSamples() != 0)
1326 {
1327 *params = 1;
1328 }
1329 else
1330 {
1331 *params = 0;
1332 }
1333 break;
1334 case GL_SAMPLES:
1335 *params = framebuffer->getSamples();
1336 break;
1337 }
1338 }
1339 else
1340 {
1341 *params = 0;
1342 }
1343 }
1344 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001345 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1346 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1347 case GL_MAX_VIEWPORT_DIMS:
1348 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001349 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001350 params[0] = maxDimension;
1351 params[1] = maxDimension;
1352 }
1353 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001354 case GL_COMPRESSED_TEXTURE_FORMATS:
1355 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001356 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00001357 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001358 *params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1359 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1360 }
1361 if (supportsDXT3Textures())
1362 {
1363 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
1364 }
1365 if (supportsDXT5Textures())
1366 {
1367 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001368 }
1369 }
1370 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001371 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001372 params[0] = mState.viewportX;
1373 params[1] = mState.viewportY;
1374 params[2] = mState.viewportWidth;
1375 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001376 break;
1377 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001378 params[0] = mState.scissorX;
1379 params[1] = mState.scissorY;
1380 params[2] = mState.scissorWidth;
1381 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001382 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001383 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1384 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001385 case GL_RED_BITS:
1386 case GL_GREEN_BITS:
1387 case GL_BLUE_BITS:
1388 case GL_ALPHA_BITS:
1389 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001390 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001391 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001392
1393 if (colorbuffer)
1394 {
1395 switch (pname)
1396 {
1397 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1398 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1399 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1400 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1401 }
1402 }
1403 else
1404 {
1405 *params = 0;
1406 }
1407 }
1408 break;
1409 case GL_DEPTH_BITS:
1410 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001411 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001412 gl::Renderbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001413
1414 if (depthbuffer)
1415 {
1416 *params = depthbuffer->getDepthSize();
1417 }
1418 else
1419 {
1420 *params = 0;
1421 }
1422 }
1423 break;
1424 case GL_STENCIL_BITS:
1425 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001426 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001427 gl::Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001428
1429 if (stencilbuffer)
1430 {
1431 *params = stencilbuffer->getStencilSize();
1432 }
1433 else
1434 {
1435 *params = 0;
1436 }
1437 }
1438 break;
1439 case GL_TEXTURE_BINDING_2D:
1440 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001441 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001442 {
1443 error(GL_INVALID_OPERATION);
1444 return false;
1445 }
1446
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001447 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001448 }
1449 break;
1450 case GL_TEXTURE_BINDING_CUBE_MAP:
1451 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001452 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001453 {
1454 error(GL_INVALID_OPERATION);
1455 return false;
1456 }
1457
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001458 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001459 }
1460 break;
1461 default:
1462 return false;
1463 }
1464
1465 return true;
1466}
1467
1468bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1469{
1470 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1471 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1472 // to the fact that it is stored internally as a float, and so would require conversion
1473 // if returned from Context::getIntegerv. Since this conversion is already implemented
1474 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1475 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1476 // application.
1477 switch (pname)
1478 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001479 case GL_COMPRESSED_TEXTURE_FORMATS:
1480 {
1481 *type = GL_INT;
1482 *numParams = mNumCompressedTextureFormats;
1483 }
1484 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001485 case GL_SHADER_BINARY_FORMATS:
1486 {
1487 *type = GL_INT;
1488 *numParams = 0;
1489 }
1490 break;
1491 case GL_MAX_VERTEX_ATTRIBS:
1492 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1493 case GL_MAX_VARYING_VECTORS:
1494 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1495 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1496 case GL_MAX_TEXTURE_IMAGE_UNITS:
1497 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1498 case GL_MAX_RENDERBUFFER_SIZE:
1499 case GL_NUM_SHADER_BINARY_FORMATS:
1500 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1501 case GL_ARRAY_BUFFER_BINDING:
1502 case GL_FRAMEBUFFER_BINDING:
1503 case GL_RENDERBUFFER_BINDING:
1504 case GL_CURRENT_PROGRAM:
1505 case GL_PACK_ALIGNMENT:
1506 case GL_UNPACK_ALIGNMENT:
1507 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001508 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001509 case GL_RED_BITS:
1510 case GL_GREEN_BITS:
1511 case GL_BLUE_BITS:
1512 case GL_ALPHA_BITS:
1513 case GL_DEPTH_BITS:
1514 case GL_STENCIL_BITS:
1515 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1516 case GL_CULL_FACE_MODE:
1517 case GL_FRONT_FACE:
1518 case GL_ACTIVE_TEXTURE:
1519 case GL_STENCIL_FUNC:
1520 case GL_STENCIL_VALUE_MASK:
1521 case GL_STENCIL_REF:
1522 case GL_STENCIL_FAIL:
1523 case GL_STENCIL_PASS_DEPTH_FAIL:
1524 case GL_STENCIL_PASS_DEPTH_PASS:
1525 case GL_STENCIL_BACK_FUNC:
1526 case GL_STENCIL_BACK_VALUE_MASK:
1527 case GL_STENCIL_BACK_REF:
1528 case GL_STENCIL_BACK_FAIL:
1529 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1530 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1531 case GL_DEPTH_FUNC:
1532 case GL_BLEND_SRC_RGB:
1533 case GL_BLEND_SRC_ALPHA:
1534 case GL_BLEND_DST_RGB:
1535 case GL_BLEND_DST_ALPHA:
1536 case GL_BLEND_EQUATION_RGB:
1537 case GL_BLEND_EQUATION_ALPHA:
1538 case GL_STENCIL_WRITEMASK:
1539 case GL_STENCIL_BACK_WRITEMASK:
1540 case GL_STENCIL_CLEAR_VALUE:
1541 case GL_SUBPIXEL_BITS:
1542 case GL_MAX_TEXTURE_SIZE:
1543 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1544 case GL_SAMPLE_BUFFERS:
1545 case GL_SAMPLES:
1546 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1547 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1548 case GL_TEXTURE_BINDING_2D:
1549 case GL_TEXTURE_BINDING_CUBE_MAP:
1550 {
1551 *type = GL_INT;
1552 *numParams = 1;
1553 }
1554 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001555 case GL_MAX_SAMPLES_ANGLE:
1556 {
1557 if (getMaxSupportedSamples() != 0)
1558 {
1559 *type = GL_INT;
1560 *numParams = 1;
1561 }
1562 else
1563 {
1564 return false;
1565 }
1566 }
1567 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001568 case GL_MAX_VIEWPORT_DIMS:
1569 {
1570 *type = GL_INT;
1571 *numParams = 2;
1572 }
1573 break;
1574 case GL_VIEWPORT:
1575 case GL_SCISSOR_BOX:
1576 {
1577 *type = GL_INT;
1578 *numParams = 4;
1579 }
1580 break;
1581 case GL_SHADER_COMPILER:
1582 case GL_SAMPLE_COVERAGE_INVERT:
1583 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001584 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1585 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1586 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1587 case GL_SAMPLE_COVERAGE:
1588 case GL_SCISSOR_TEST:
1589 case GL_STENCIL_TEST:
1590 case GL_DEPTH_TEST:
1591 case GL_BLEND:
1592 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001593 {
1594 *type = GL_BOOL;
1595 *numParams = 1;
1596 }
1597 break;
1598 case GL_COLOR_WRITEMASK:
1599 {
1600 *type = GL_BOOL;
1601 *numParams = 4;
1602 }
1603 break;
1604 case GL_POLYGON_OFFSET_FACTOR:
1605 case GL_POLYGON_OFFSET_UNITS:
1606 case GL_SAMPLE_COVERAGE_VALUE:
1607 case GL_DEPTH_CLEAR_VALUE:
1608 case GL_LINE_WIDTH:
1609 {
1610 *type = GL_FLOAT;
1611 *numParams = 1;
1612 }
1613 break;
1614 case GL_ALIASED_LINE_WIDTH_RANGE:
1615 case GL_ALIASED_POINT_SIZE_RANGE:
1616 case GL_DEPTH_RANGE:
1617 {
1618 *type = GL_FLOAT;
1619 *numParams = 2;
1620 }
1621 break;
1622 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001623 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001624 {
1625 *type = GL_FLOAT;
1626 *numParams = 4;
1627 }
1628 break;
1629 default:
1630 return false;
1631 }
1632
1633 return true;
1634}
1635
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001636// Applies the render target surface, depth stencil surface, viewport rectangle and
1637// scissor rectangle to the Direct3D 9 device
1638bool Context::applyRenderTarget(bool ignoreViewport)
1639{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001640 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001641
1642 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1643 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001644 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001645 }
1646
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001647 IDirect3DSurface9 *renderTarget = NULL;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001648 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001649
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001650 bool renderTargetChanged = false;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001651 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1652 if (renderTargetSerial != mAppliedRenderTargetSerial)
1653 {
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001654 renderTarget = framebufferObject->getRenderTarget();
1655
1656 if (!renderTarget)
1657 {
1658 return false; // Context must be lost
1659 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001660 mDevice->SetRenderTarget(0, renderTarget);
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001661 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001662 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 +00001663 renderTargetChanged = true;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001664 }
1665
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001666 unsigned int depthbufferSerial = 0;
1667 unsigned int stencilbufferSerial = 0;
1668 if (framebufferObject->getDepthbufferType() != GL_NONE)
1669 {
1670 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001671 if (!depthStencil)
1672 {
1673 ERR("Depth stencil pointer unexpectedly null.");
1674 return false;
1675 }
1676
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001677 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1678 }
1679 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1680 {
1681 depthStencil = framebufferObject->getStencilbuffer()->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 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1689 }
1690
1691 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001692 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001693 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001694 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001695 mDevice->SetDepthStencilSurface(depthStencil);
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001696 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001697 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001698 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001699 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001700
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001701 if (!mRenderTargetDescInitialized || renderTargetChanged)
1702 {
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001703 if (!renderTarget)
1704 {
1705 renderTarget = framebufferObject->getRenderTarget();
1706
1707 if (!renderTarget)
1708 {
1709 return false; // Context must be lost
1710 }
1711 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001712 renderTarget->GetDesc(&mRenderTargetDesc);
1713 mRenderTargetDescInitialized = true;
1714 }
1715
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001716 D3DVIEWPORT9 viewport;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001717
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001718 float zNear = clamp01(mState.zNear);
1719 float zFar = clamp01(mState.zFar);
1720
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001721 if (ignoreViewport)
1722 {
1723 viewport.X = 0;
1724 viewport.Y = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001725 viewport.Width = mRenderTargetDesc.Width;
1726 viewport.Height = mRenderTargetDesc.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001727 viewport.MinZ = 0.0f;
1728 viewport.MaxZ = 1.0f;
1729 }
1730 else
1731 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001732 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, mRenderTargetDesc.Height);
1733 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1734 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1735 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width) - static_cast<LONG>(viewport.X));
1736 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 +00001737 viewport.MinZ = zNear;
1738 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001739 }
1740
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001741 if (viewport.Width <= 0 || viewport.Height <= 0)
1742 {
1743 return false; // Nothing to render
1744 }
1745
jbauman@chromium.org241e70d2011-11-03 23:07:05 +00001746 if (renderTargetChanged || !mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001747 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001748 mDevice->SetViewport(&viewport);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001749 mSetViewport = viewport;
1750 mViewportInitialized = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001751 mDxUniformsDirty = true;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001752 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001753
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001754 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001755 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001756 if (mState.scissorTest)
1757 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001758 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, mRenderTargetDesc.Height);
1759 rect.left = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1760 rect.top = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1761 rect.right = clamp(rect.right, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1762 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001763 mDevice->SetScissorRect(&rect);
1764 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001765 }
1766 else
1767 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001768 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001769 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001770
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001771 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001772 }
1773
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001774 if (mState.currentProgram && mDxUniformsDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001775 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001776 Program *programObject = getCurrentProgram();
1777
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001778 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001779 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001780 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001781
daniel@transgaming.com31754962010-11-28 02:02:52 +00001782 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001783 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1784 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1785 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001786 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001787
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001788 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001789 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001790 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001791
daniel@transgaming.com31754962010-11-28 02:02:52 +00001792 GLint depthRange = programObject->getDxDepthRangeLocation();
1793 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1794 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001795 mDxUniformsDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001796 }
1797
1798 return true;
1799}
1800
1801// 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 +00001802void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001803{
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001804 Program *programObject = getCurrentProgram();
1805
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001806 Framebuffer *framebufferObject = getDrawFramebuffer();
1807
1808 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1809
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001810 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001811 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001812 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001813
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001814 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001815 GLint alwaysFront = !isTriangleMode(drawMode);
1816 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1817
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001818 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001819 bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
1820 // Apparently some ATI cards have a bug where a draw with a zero color
1821 // write mask can cause later draws to have incorrect results. Instead,
1822 // set a nonzero color write mask but modify the blend state so that no
1823 // drawing is done.
1824 // http://code.google.com/p/angleproject/issues/detail?id=169
1825
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001826 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001827 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001828 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001829 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001830 mDevice->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001831 }
1832 else
1833 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001834 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001835 }
1836
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001837 mCullStateDirty = false;
1838 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001839
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001840 if (mDepthStateDirty)
1841 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00001842 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001843 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001844 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1845 mDevice->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001846 }
1847 else
1848 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001849 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001850 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001851
1852 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001853 }
1854
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001855 if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
1856 {
1857 mBlendStateDirty = true;
1858 mMaskStateDirty = true;
1859 }
1860
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001861 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001862 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001863 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001864 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001865 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001866
1867 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1868 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1869 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001870 mDevice->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001871 }
1872 else
1873 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001874 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001875 unorm<8>(mState.blendColor.alpha),
1876 unorm<8>(mState.blendColor.alpha),
1877 unorm<8>(mState.blendColor.alpha)));
1878 }
1879
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001880 mDevice->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1881 mDevice->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1882 mDevice->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001883
1884 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1885 mState.destBlendRGB != mState.destBlendAlpha ||
1886 mState.blendEquationRGB != mState.blendEquationAlpha)
1887 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001888 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001889
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001890 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1891 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1892 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001893 }
1894 else
1895 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001896 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001897 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001898 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001899 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001900 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001901 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001902 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001903
1904 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001905 }
1906
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001907 if (mStencilStateDirty || mFrontFaceDirty)
1908 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001909 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001910 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001911 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1912 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001913
1914 // FIXME: Unsupported by D3D9
1915 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1916 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1917 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1918 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1919 mState.stencilRef != mState.stencilBackRef ||
1920 mState.stencilMask != mState.stencilBackMask)
1921 {
1922 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1923 return error(GL_INVALID_OPERATION);
1924 }
1925
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001926 // get the maximum size of the stencil ref
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001927 gl::Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001928 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1929
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001930 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1931 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001932 es2dx::ConvertComparison(mState.stencilFunc));
1933
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001934 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1935 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001936
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001937 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001938 es2dx::ConvertStencilOp(mState.stencilFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001939 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001940 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001941 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001942 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1943
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001944 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1945 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001946 es2dx::ConvertComparison(mState.stencilBackFunc));
1947
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001948 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1949 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001950
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001951 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001952 es2dx::ConvertStencilOp(mState.stencilBackFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001953 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001954 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001955 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001956 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1957 }
1958 else
1959 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001960 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001961 }
1962
1963 mStencilStateDirty = false;
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001964 mFrontFaceDirty = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001965 }
1966
1967 if (mMaskStateDirty)
1968 {
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001969 int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1970 mState.colorMaskBlue, mState.colorMaskAlpha);
1971 if (colorMask == 0 && !zeroColorMaskAllowed)
1972 {
1973 // Enable green channel, but set blending so nothing will be drawn.
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001974 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
1975 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001976
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001977 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
1978 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
1979 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001980 }
1981 else
1982 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001983 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001984 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001985 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001986
1987 mMaskStateDirty = false;
1988 }
1989
1990 if (mPolygonOffsetStateDirty)
1991 {
1992 if (mState.polygonOffsetFill)
1993 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001994 gl::Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001995 if (depthbuffer)
1996 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001997 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001998 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001999 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002000 }
2001 }
2002 else
2003 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002004 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
2005 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002006 }
2007
2008 mPolygonOffsetStateDirty = false;
2009 }
2010
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002011 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002012 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002013 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002014 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002015 FIXME("Sample alpha to coverage is unimplemented.");
2016 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002017
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002018 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002019 if (mState.sampleCoverage)
2020 {
2021 unsigned int mask = 0;
2022 if (mState.sampleCoverageValue != 0)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002023 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002024 float threshold = 0.5f;
2025
2026 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002027 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002028 mask <<= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002029
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002030 if ((i + 1) * mState.sampleCoverageValue >= threshold)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002031 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002032 threshold += 1.0f;
2033 mask |= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002034 }
2035 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002036 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002037
2038 if (mState.sampleCoverageInvert)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002039 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002040 mask = ~mask;
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002041 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002042
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002043 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002044 }
2045 else
2046 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002047 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002048 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002049
2050 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002051 }
2052
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002053 if (mDitherStateDirty)
2054 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002055 mDevice->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002056
2057 mDitherStateDirty = false;
2058 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002059}
2060
daniel@transgaming.com83921382011-01-08 05:46:00 +00002061GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002062{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002063 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002064
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002065 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002066 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002067 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002068 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002069 }
2070
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002071 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, getCurrentProgram());
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002072}
2073
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002074// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002075GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002076{
daniel@transgaming.com83921382011-01-08 05:46:00 +00002077 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002078
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002079 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002080 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002081 if (indexInfo->serial != mAppliedIBSerial)
2082 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002083 mDevice->SetIndices(indexInfo->indexBuffer);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002084 mAppliedIBSerial = indexInfo->serial;
2085 }
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002086 }
2087
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002088 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002089}
2090
2091// Applies the shaders and shader constants to the Direct3D 9 device
2092void Context::applyShaders()
2093{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002094 Program *programObject = getCurrentProgram();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002095 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002096 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002097 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2098 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2099
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002100 mDevice->SetPixelShader(pixelShader);
2101 mDevice->SetVertexShader(vertexShader);
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002102 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002103 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002104 }
2105
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002106 programObject->applyUniforms();
2107}
2108
2109// Applies the textures and sampler states to the Direct3D 9 device
2110void Context::applyTextures()
2111{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002112 applyTextures(SAMPLER_PIXEL);
2113
2114 if (mSupportsVertexTexture)
2115 {
2116 applyTextures(SAMPLER_VERTEX);
2117 }
2118}
2119
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002120// For each Direct3D 9 sampler of either the pixel or vertex stage,
2121// looks up the corresponding OpenGL texture image unit and texture type,
2122// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002123void Context::applyTextures(SamplerType type)
2124{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002125 Program *programObject = getCurrentProgram();
2126
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002127 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 +00002128 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2129 int d3dSamplerOffset = (type == SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002130 int samplerRange = programObject->getUsedSamplerRange(type);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002131
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002132 for (int samplerIndex = 0; samplerIndex < samplerRange; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002133 {
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002134 int textureUnit = programObject->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00002135 int d3dSampler = samplerIndex + d3dSamplerOffset;
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002136
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002137 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002138 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002139 TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002140
2141 Texture *texture = getSamplerTexture(textureUnit, textureType);
2142
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00002143 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->hasDirtyParameters() || texture->hasDirtyImages())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002144 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002145 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2146
2147 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002148 {
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00002149 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->hasDirtyParameters())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002150 {
2151 GLenum wrapS = texture->getWrapS();
2152 GLenum wrapT = texture->getWrapT();
2153 GLenum minFilter = texture->getMinFilter();
2154 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002155
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002156 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2157 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002158
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002159 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002160 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2161 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002162 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2163 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002164 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002165
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00002166 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->hasDirtyImages())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002167 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002168 mDevice->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002169 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002170 }
2171 else
2172 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002173 mDevice->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002174 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002175
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002176 appliedTextureSerial[samplerIndex] = texture->getSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002177 texture->resetDirty();
2178 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002179 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002180 else
2181 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002182 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002183 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002184 mDevice->SetTexture(d3dSampler, NULL);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002185 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002186 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002187 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002188 }
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002189
2190 for (int samplerIndex = samplerRange; samplerIndex < samplerCount; samplerIndex++)
2191 {
2192 if (appliedTextureSerial[samplerIndex] != 0)
2193 {
daniel@transgaming.comc5a7b692011-10-26 02:45:44 +00002194 mDevice->SetTexture(samplerIndex + d3dSamplerOffset, NULL);
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002195 appliedTextureSerial[samplerIndex] = 0;
2196 }
2197 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002198}
2199
2200void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2201{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002202 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002203
2204 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2205 {
2206 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2207 }
2208
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002209 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2210 {
2211 return error(GL_INVALID_OPERATION);
2212 }
2213
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002214 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002215
2216 if (!renderTarget)
2217 {
2218 return; // Context must be lost, return silently
2219 }
2220
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002221 D3DSURFACE_DESC desc;
2222 renderTarget->GetDesc(&desc);
2223
2224 IDirect3DSurface9 *systemSurface;
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002225 HRESULT result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002226
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002227 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002228 {
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002229 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002230 return error(GL_OUT_OF_MEMORY);
2231 }
2232
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002233 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2234 {
2235 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002236 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002237 }
2238
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002239 result = mDevice->GetRenderTargetData(renderTarget, systemSurface);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002240
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002241 if (FAILED(result))
2242 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002243 systemSurface->Release();
2244
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002245 // It turns out that D3D will sometimes produce more error
2246 // codes than those documented.
2247 if (checkDeviceLost(result))
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002248 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002249 else
2250 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002251 UNREACHABLE();
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002252 return;
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002253 }
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002254
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002255 }
2256
2257 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002258 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2259 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2260 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2261 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2262 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002263
2264 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2265
2266 if (FAILED(result))
2267 {
2268 UNREACHABLE();
2269 systemSurface->Release();
2270
2271 return; // No sensible error to generate
2272 }
2273
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002274 unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002275 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002276 unsigned short *dest16 = (unsigned short*)pixels;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002277 int inputPitch = -lock.Pitch;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002278 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002279
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002280 for (int j = 0; j < rect.bottom - rect.top; j++)
2281 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002282 if (desc.Format == D3DFMT_A8R8G8B8 &&
2283 format == GL_BGRA_EXT &&
2284 type == GL_UNSIGNED_BYTE)
2285 {
2286 // Fast path for EXT_read_format_bgra, given
2287 // an RGBA source buffer. Note that buffers with no
2288 // alpha go through the slow path below.
2289 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002290 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002291 (rect.right - rect.left) * 4);
2292 continue;
2293 }
2294
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002295 for (int i = 0; i < rect.right - rect.left; i++)
2296 {
2297 float r;
2298 float g;
2299 float b;
2300 float a;
2301
2302 switch (desc.Format)
2303 {
2304 case D3DFMT_R5G6B5:
2305 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002306 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002307
2308 a = 1.0f;
2309 b = (rgb & 0x001F) * (1.0f / 0x001F);
2310 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2311 r = (rgb & 0xF800) * (1.0f / 0xF800);
2312 }
2313 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002314 case D3DFMT_A1R5G5B5:
2315 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002316 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002317
2318 a = (argb & 0x8000) ? 1.0f : 0.0f;
2319 b = (argb & 0x001F) * (1.0f / 0x001F);
2320 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2321 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2322 }
2323 break;
2324 case D3DFMT_A8R8G8B8:
2325 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002326 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002327
2328 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2329 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2330 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2331 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2332 }
2333 break;
2334 case D3DFMT_X8R8G8B8:
2335 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002336 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002337
2338 a = 1.0f;
2339 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2340 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2341 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2342 }
2343 break;
2344 case D3DFMT_A2R10G10B10:
2345 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002346 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002347
2348 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2349 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2350 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2351 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2352 }
2353 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002354 case D3DFMT_A32B32G32R32F:
2355 {
2356 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002357 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2358 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2359 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2360 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002361 }
2362 break;
2363 case D3DFMT_A16B16G16R16F:
2364 {
2365 // float formats in D3D are stored rgba, rather than the other way round
2366 float abgr[4];
2367
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002368 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002369
2370 a = abgr[3];
2371 b = abgr[2];
2372 g = abgr[1];
2373 r = abgr[0];
2374 }
2375 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002376 default:
2377 UNIMPLEMENTED(); // FIXME
2378 UNREACHABLE();
2379 }
2380
2381 switch (format)
2382 {
2383 case GL_RGBA:
2384 switch (type)
2385 {
2386 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002387 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2388 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2389 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2390 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002391 break;
2392 default: UNREACHABLE();
2393 }
2394 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002395 case GL_BGRA_EXT:
2396 switch (type)
2397 {
2398 case GL_UNSIGNED_BYTE:
2399 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2400 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2401 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2402 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2403 break;
2404 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2405 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2406 // this type is packed as follows:
2407 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2408 // --------------------------------------------------------------------------------
2409 // | 4th | 3rd | 2nd | 1st component |
2410 // --------------------------------------------------------------------------------
2411 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2412 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2413 ((unsigned short)(15 * a + 0.5f) << 12)|
2414 ((unsigned short)(15 * r + 0.5f) << 8) |
2415 ((unsigned short)(15 * g + 0.5f) << 4) |
2416 ((unsigned short)(15 * b + 0.5f) << 0);
2417 break;
2418 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2419 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2420 // this type is packed as follows:
2421 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2422 // --------------------------------------------------------------------------------
2423 // | 4th | 3rd | 2nd | 1st component |
2424 // --------------------------------------------------------------------------------
2425 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2426 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2427 ((unsigned short)( a + 0.5f) << 15) |
2428 ((unsigned short)(31 * r + 0.5f) << 10) |
2429 ((unsigned short)(31 * g + 0.5f) << 5) |
2430 ((unsigned short)(31 * b + 0.5f) << 0);
2431 break;
2432 default: UNREACHABLE();
2433 }
2434 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002435 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002436 switch (type)
2437 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002438 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002439 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2440 ((unsigned short)(31 * b + 0.5f) << 0) |
2441 ((unsigned short)(63 * g + 0.5f) << 5) |
2442 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002443 break;
2444 default: UNREACHABLE();
2445 }
2446 break;
2447 default: UNREACHABLE();
2448 }
2449 }
2450 }
2451
2452 systemSurface->UnlockRect();
2453
2454 systemSurface->Release();
2455}
2456
2457void Context::clear(GLbitfield mask)
2458{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002459 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002460
2461 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2462 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002463 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002464 }
2465
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002466 DWORD flags = 0;
2467
2468 if (mask & GL_COLOR_BUFFER_BIT)
2469 {
2470 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002471
2472 if (framebufferObject->getColorbufferType() != GL_NONE)
2473 {
2474 flags |= D3DCLEAR_TARGET;
2475 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002476 }
2477
2478 if (mask & GL_DEPTH_BUFFER_BIT)
2479 {
2480 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002481 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002482 {
2483 flags |= D3DCLEAR_ZBUFFER;
2484 }
2485 }
2486
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002487 GLuint stencilUnmasked = 0x0;
2488
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002489 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002490 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002491 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002492 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002493 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002494 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002495 if (!depthStencil)
2496 {
2497 ERR("Depth stencil pointer unexpectedly null.");
2498 return;
2499 }
2500
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002501 D3DSURFACE_DESC desc;
2502 depthStencil->GetDesc(&desc);
2503
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002504 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002505 stencilUnmasked = (0x1 << stencilSize) - 1;
2506
2507 if (stencilUnmasked != 0x0)
2508 {
2509 flags |= D3DCLEAR_STENCIL;
2510 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002511 }
2512 }
2513
2514 if (mask != 0)
2515 {
2516 return error(GL_INVALID_VALUE);
2517 }
2518
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002519 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2520 {
2521 return;
2522 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002523
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002524 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002525 unorm<8>(mState.colorClearValue.red),
2526 unorm<8>(mState.colorClearValue.green),
2527 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002528 float depth = clamp01(mState.depthClearValue);
2529 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002530
2531 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2532
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002533 if (!renderTarget)
2534 {
2535 return; // Context must be lost, return silently
2536 }
2537
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002538 D3DSURFACE_DESC desc;
2539 renderTarget->GetDesc(&desc);
2540
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002541 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002542
2543 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002544 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002545 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002546 !(mState.colorMaskRed && mState.colorMaskGreen &&
2547 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002548
2549 if (needMaskedColorClear || needMaskedStencilClear)
2550 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002551 // State which is altered in all paths from this point to the clear call is saved.
2552 // State which is altered in only some paths will be flagged dirty in the case that
2553 // that path is taken.
2554 HRESULT hr;
2555 if (mMaskedClearSavedState == NULL)
2556 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002557 hr = mDevice->BeginStateBlock();
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002558 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2559
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002560 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2561 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2562 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2563 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2564 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2565 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2566 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2567 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2568 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2569 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2570 mDevice->SetPixelShader(NULL);
2571 mDevice->SetVertexShader(NULL);
2572 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2573 mDevice->SetStreamSource(0, NULL, 0, 0);
2574 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2575 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2576 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2577 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2578 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2579 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2580 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002581
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002582 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002583 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2584 }
2585
2586 ASSERT(mMaskedClearSavedState != NULL);
2587
2588 if (mMaskedClearSavedState != NULL)
2589 {
2590 hr = mMaskedClearSavedState->Capture();
2591 ASSERT(SUCCEEDED(hr));
2592 }
2593
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002594 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2595 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2596 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2597 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2598 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2599 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2600 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2601 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002602
2603 if (flags & D3DCLEAR_TARGET)
2604 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002605 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002606 }
2607 else
2608 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002609 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002610 }
2611
2612 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2613 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002614 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2615 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2616 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2617 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
2618 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
2619 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
2620 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2621 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002622 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002623 }
2624 else
2625 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002626 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002627 }
2628
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002629 mDevice->SetPixelShader(NULL);
2630 mDevice->SetVertexShader(NULL);
2631 mDevice->SetFVF(D3DFVF_XYZRHW);
2632 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2633 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2634 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2635 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2636 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2637 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2638 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002639
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002640 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2641 quad[0][0] = -0.5f;
2642 quad[0][1] = desc.Height - 0.5f;
2643 quad[0][2] = 0.0f;
2644 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002645
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002646 quad[1][0] = desc.Width - 0.5f;
2647 quad[1][1] = desc.Height - 0.5f;
2648 quad[1][2] = 0.0f;
2649 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002650
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002651 quad[2][0] = -0.5f;
2652 quad[2][1] = -0.5f;
2653 quad[2][2] = 0.0f;
2654 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002655
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002656 quad[3][0] = desc.Width - 0.5f;
2657 quad[3][1] = -0.5f;
2658 quad[3][2] = 0.0f;
2659 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002660
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002661 mDisplay->startScene();
2662 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002663
2664 if (flags & D3DCLEAR_ZBUFFER)
2665 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002666 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
2667 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2668 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002669 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002670
2671 if (mMaskedClearSavedState != NULL)
2672 {
2673 mMaskedClearSavedState->Apply();
2674 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002675 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002676 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002677 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002678 mDevice->Clear(0, NULL, flags, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002679 }
2680}
2681
2682void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2683{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002684 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002685 {
2686 return error(GL_INVALID_OPERATION);
2687 }
2688
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002689 D3DPRIMITIVETYPE primitiveType;
2690 int primitiveCount;
2691
2692 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2693 return error(GL_INVALID_ENUM);
2694
2695 if (primitiveCount <= 0)
2696 {
2697 return;
2698 }
2699
2700 if (!applyRenderTarget(false))
2701 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002702 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002703 }
2704
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002705 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002706
daniel@transgaming.com83921382011-01-08 05:46:00 +00002707 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002708 if (err != GL_NO_ERROR)
2709 {
2710 return error(err);
2711 }
2712
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002713 applyShaders();
2714 applyTextures();
2715
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002716 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002717 {
2718 return error(GL_INVALID_OPERATION);
2719 }
2720
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002721 if (!cullSkipsDraw(mode))
2722 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002723 mDisplay->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002724
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002725 mDevice->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002726
2727 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2728 {
2729 drawClosingLine(first, first + count - 1);
2730 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002731 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002732}
2733
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002734void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002735{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002736 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002737 {
2738 return error(GL_INVALID_OPERATION);
2739 }
2740
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002741 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002742 {
2743 return error(GL_INVALID_OPERATION);
2744 }
2745
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002746 D3DPRIMITIVETYPE primitiveType;
2747 int primitiveCount;
2748
2749 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2750 return error(GL_INVALID_ENUM);
2751
2752 if (primitiveCount <= 0)
2753 {
2754 return;
2755 }
2756
2757 if (!applyRenderTarget(false))
2758 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002759 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002760 }
2761
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002762 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002763
2764 TranslatedIndexData indexInfo;
2765 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2766 if (err != GL_NO_ERROR)
2767 {
2768 return error(err);
2769 }
2770
daniel@transgaming.com83921382011-01-08 05:46:00 +00002771 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2772 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002773 if (err != GL_NO_ERROR)
2774 {
2775 return error(err);
2776 }
2777
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002778 applyShaders();
2779 applyTextures();
2780
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002781 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002782 {
2783 return error(GL_INVALID_OPERATION);
2784 }
2785
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002786 if (!cullSkipsDraw(mode))
2787 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002788 mDisplay->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002789
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002790 mDevice->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002791
2792 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2793 {
2794 drawClosingLine(count, type, indices);
2795 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002796 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002797}
2798
daniel@transgaming.com0d86aa72011-10-26 02:35:10 +00002799// Implements glFlush when block is false, glFinish when block is true
2800void Context::sync(bool block)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002801{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002802 IDirect3DQuery9 *eventQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002803 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002804
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002805 result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002806 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002807 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002808 ERR("CreateQuery failed hr=%x\n", result);
2809 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2810 {
2811 return error(GL_OUT_OF_MEMORY);
2812 }
2813 ASSERT(false);
2814 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002815 }
2816
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002817 result = eventQuery->Issue(D3DISSUE_END);
2818 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002819 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002820 ERR("eventQuery->Issue(END) failed hr=%x\n", result);
2821 ASSERT(false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002822 eventQuery->Release();
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002823 return;
2824 }
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002825
daniel@transgaming.com0d86aa72011-10-26 02:35:10 +00002826 do
2827 {
2828 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
2829
2830 if(block && result == S_FALSE)
2831 {
2832 // Keep polling, but allow other threads to do something useful first
2833 Sleep(0);
2834 }
2835 }
2836 while(block && result == S_FALSE);
2837
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002838 eventQuery->Release();
2839
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002840 if (checkDeviceLost(result))
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002841 {
2842 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002843 }
2844}
2845
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002846void Context::drawClosingLine(unsigned int first, unsigned int last)
2847{
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002848 IDirect3DIndexBuffer9 *indexBuffer = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002849 bool succeeded = false;
2850 UINT offset;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002851
2852 if (supports32bitIndices())
2853 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002854 const int spaceNeeded = 2 * sizeof(unsigned int);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002855
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002856 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002857 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002858 mClosingIB = new StreamingIndexBuffer(mDevice, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002859 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002860
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002861 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
2862
2863 unsigned int *data = static_cast<unsigned int*>(mClosingIB->map(spaceNeeded, &offset));
2864 if (data)
2865 {
2866 data[0] = last;
2867 data[1] = first;
2868 mClosingIB->unmap();
2869 offset /= 4;
2870 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002871 }
2872 }
2873 else
2874 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002875 const int spaceNeeded = 2 * sizeof(unsigned short);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002876
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002877 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002878 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002879 mClosingIB = new StreamingIndexBuffer(mDevice, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002880 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002881
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002882 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
2883
2884 unsigned short *data = static_cast<unsigned short*>(mClosingIB->map(spaceNeeded, &offset));
2885 if (data)
2886 {
2887 data[0] = last;
2888 data[1] = first;
2889 mClosingIB->unmap();
2890 offset /= 2;
2891 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002892 }
2893 }
2894
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002895 if (succeeded)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002896 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002897 mDevice->SetIndices(mClosingIB->getBuffer());
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002898 mAppliedIBSerial = mClosingIB->getSerial();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002899
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002900 mDevice->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, last, offset, 1);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002901 }
2902 else
2903 {
2904 ERR("Could not create an index buffer for closing a line loop.");
2905 error(GL_OUT_OF_MEMORY);
2906 }
2907}
2908
2909void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices)
2910{
2911 unsigned int first = 0;
2912 unsigned int last = 0;
2913
2914 if (mState.elementArrayBuffer.get())
2915 {
2916 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2917 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2918 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2919 }
2920
2921 switch (type)
2922 {
2923 case GL_UNSIGNED_BYTE:
2924 first = static_cast<const GLubyte*>(indices)[0];
2925 last = static_cast<const GLubyte*>(indices)[count - 1];
2926 break;
2927 case GL_UNSIGNED_SHORT:
2928 first = static_cast<const GLushort*>(indices)[0];
2929 last = static_cast<const GLushort*>(indices)[count - 1];
2930 break;
2931 case GL_UNSIGNED_INT:
2932 first = static_cast<const GLuint*>(indices)[0];
2933 last = static_cast<const GLuint*>(indices)[count - 1];
2934 break;
2935 default: UNREACHABLE();
2936 }
2937
2938 drawClosingLine(first, last);
2939}
2940
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002941void Context::recordInvalidEnum()
2942{
2943 mInvalidEnum = true;
2944}
2945
2946void Context::recordInvalidValue()
2947{
2948 mInvalidValue = true;
2949}
2950
2951void Context::recordInvalidOperation()
2952{
2953 mInvalidOperation = true;
2954}
2955
2956void Context::recordOutOfMemory()
2957{
2958 mOutOfMemory = true;
2959}
2960
2961void Context::recordInvalidFramebufferOperation()
2962{
2963 mInvalidFramebufferOperation = true;
2964}
2965
2966// Get one of the recorded errors and clear its flag, if any.
2967// [OpenGL ES 2.0.24] section 2.5 page 13.
2968GLenum Context::getError()
2969{
2970 if (mInvalidEnum)
2971 {
2972 mInvalidEnum = false;
2973
2974 return GL_INVALID_ENUM;
2975 }
2976
2977 if (mInvalidValue)
2978 {
2979 mInvalidValue = false;
2980
2981 return GL_INVALID_VALUE;
2982 }
2983
2984 if (mInvalidOperation)
2985 {
2986 mInvalidOperation = false;
2987
2988 return GL_INVALID_OPERATION;
2989 }
2990
2991 if (mOutOfMemory)
2992 {
2993 mOutOfMemory = false;
2994
2995 return GL_OUT_OF_MEMORY;
2996 }
2997
2998 if (mInvalidFramebufferOperation)
2999 {
3000 mInvalidFramebufferOperation = false;
3001
3002 return GL_INVALID_FRAMEBUFFER_OPERATION;
3003 }
3004
3005 return GL_NO_ERROR;
3006}
3007
daniel@transgaming.com17f548c2011-11-09 17:47:02 +00003008GLenum Context::getResetStatus()
3009{
3010 if (mResetStatus == GL_NO_ERROR)
3011 {
3012 bool lost = mDisplay->testDeviceLost();
3013
3014 if (lost)
3015 {
3016 mDisplay->notifyDeviceLost(); // Sets mResetStatus
3017 }
3018 }
3019
3020 GLenum status = mResetStatus;
3021
3022 if (mResetStatus != GL_NO_ERROR)
3023 {
3024 if (mDisplay->testDeviceResettable())
3025 {
3026 mResetStatus = GL_NO_ERROR;
3027 }
3028 }
3029
3030 return status;
3031}
3032
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003033bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003034{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003035 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003036}
3037
daniel@transgaming.com396c6432010-11-26 16:26:12 +00003038int Context::getMaximumVaryingVectors() const
3039{
3040 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3041}
3042
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003043unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003044{
3045 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3046}
3047
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003048unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003049{
3050 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3051}
3052
daniel@transgaming.com458da142010-11-28 02:03:02 +00003053int Context::getMaximumFragmentUniformVectors() const
3054{
3055 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3056}
3057
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003058int Context::getMaxSupportedSamples() const
3059{
3060 return mMaxSupportedSamples;
3061}
3062
3063int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3064{
3065 if (requested == 0)
3066 {
3067 return requested;
3068 }
3069
3070 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3071 if (itr == mMultiSampleSupport.end())
3072 {
3073 return -1;
3074 }
3075
3076 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3077 {
3078 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3079 {
3080 return i;
3081 }
3082 }
3083
3084 return -1;
3085}
3086
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003087bool Context::supportsEventQueries() const
3088{
3089 return mSupportsEventQueries;
3090}
3091
gman@chromium.org50c526d2011-08-10 05:19:44 +00003092bool Context::supportsDXT1Textures() const
daniel@transgaming.com01868132010-08-24 19:21:17 +00003093{
gman@chromium.org50c526d2011-08-10 05:19:44 +00003094 return mSupportsDXT1Textures;
3095}
3096
3097bool Context::supportsDXT3Textures() const
3098{
3099 return mSupportsDXT3Textures;
3100}
3101
3102bool Context::supportsDXT5Textures() const
3103{
3104 return mSupportsDXT5Textures;
daniel@transgaming.com01868132010-08-24 19:21:17 +00003105}
3106
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003107bool Context::supportsFloat32Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003108{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003109 return mSupportsFloat32Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003110}
3111
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003112bool Context::supportsFloat32LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003113{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003114 return mSupportsFloat32LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003115}
3116
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003117bool Context::supportsFloat32RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003118{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003119 return mSupportsFloat32RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003120}
3121
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003122bool Context::supportsFloat16Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003123{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003124 return mSupportsFloat16Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003125}
3126
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003127bool Context::supportsFloat16LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003128{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003129 return mSupportsFloat16LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003130}
3131
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003132bool Context::supportsFloat16RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003133{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003134 return mSupportsFloat16RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003135}
3136
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003137int Context::getMaximumRenderbufferDimension() const
3138{
3139 return mMaxRenderbufferDimension;
3140}
3141
3142int Context::getMaximumTextureDimension() const
3143{
3144 return mMaxTextureDimension;
3145}
3146
3147int Context::getMaximumCubeTextureDimension() const
3148{
3149 return mMaxCubeTextureDimension;
3150}
3151
3152int Context::getMaximumTextureLevel() const
3153{
3154 return mMaxTextureLevel;
3155}
3156
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003157bool Context::supportsLuminanceTextures() const
3158{
3159 return mSupportsLuminanceTextures;
3160}
3161
3162bool Context::supportsLuminanceAlphaTextures() const
3163{
3164 return mSupportsLuminanceAlphaTextures;
3165}
3166
daniel@transgaming.com83921382011-01-08 05:46:00 +00003167bool Context::supports32bitIndices() const
3168{
3169 return mSupports32bitIndices;
3170}
3171
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003172bool Context::supportsNonPower2Texture() const
3173{
3174 return mSupportsNonPower2Texture;
3175}
3176
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003177void Context::detachBuffer(GLuint buffer)
3178{
3179 // [OpenGL ES 2.0.24] section 2.9 page 22:
3180 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3181 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3182
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003183 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003184 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003185 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003186 }
3187
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003188 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003189 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003190 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003191 }
3192
3193 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3194 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003195 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003196 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003197 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003198 }
3199 }
3200}
3201
3202void Context::detachTexture(GLuint texture)
3203{
3204 // [OpenGL ES 2.0.24] section 3.8 page 84:
3205 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3206 // rebound to texture object zero
3207
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003208 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003209 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003210 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003211 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003212 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003213 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003214 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003215 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003216 }
3217 }
3218
3219 // [OpenGL ES 2.0.24] section 4.4 page 112:
3220 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3221 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3222 // image was attached in the currently bound framebuffer.
3223
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003224 Framebuffer *readFramebuffer = getReadFramebuffer();
3225 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003226
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003227 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003228 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003229 readFramebuffer->detachTexture(texture);
3230 }
3231
3232 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3233 {
3234 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003235 }
3236}
3237
3238void Context::detachFramebuffer(GLuint framebuffer)
3239{
3240 // [OpenGL ES 2.0.24] section 4.4 page 107:
3241 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3242 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3243
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003244 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003245 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003246 bindReadFramebuffer(0);
3247 }
3248
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003249 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003250 {
3251 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003252 }
3253}
3254
3255void Context::detachRenderbuffer(GLuint renderbuffer)
3256{
3257 // [OpenGL ES 2.0.24] section 4.4 page 109:
3258 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3259 // had been executed with the target RENDERBUFFER and name of zero.
3260
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003261 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003262 {
3263 bindRenderbuffer(0);
3264 }
3265
3266 // [OpenGL ES 2.0.24] section 4.4 page 111:
3267 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3268 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3269 // point to which this image was attached in the currently bound framebuffer.
3270
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003271 Framebuffer *readFramebuffer = getReadFramebuffer();
3272 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003273
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003274 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003275 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003276 readFramebuffer->detachRenderbuffer(renderbuffer);
3277 }
3278
3279 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3280 {
3281 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003282 }
3283}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003284
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003285Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003286{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003287 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003288
3289 if (t == NULL)
3290 {
3291 static const GLubyte color[] = { 0, 0, 0, 255 };
3292
3293 switch (type)
3294 {
3295 default:
3296 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003297 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003298
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003299 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003300 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003301 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003302 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003303 t = incomplete2d;
3304 }
3305 break;
3306
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003307 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003308 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003309 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003310
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003311 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3312 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3313 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3314 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3315 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3316 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003317
3318 t = incompleteCube;
3319 }
3320 break;
3321 }
3322
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003323 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003324 }
3325
3326 return t;
3327}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003328
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003329bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003330{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003331 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003332}
3333
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003334bool Context::isTriangleMode(GLenum drawMode)
3335{
3336 switch (drawMode)
3337 {
3338 case GL_TRIANGLES:
3339 case GL_TRIANGLE_FAN:
3340 case GL_TRIANGLE_STRIP:
3341 return true;
3342 case GL_POINTS:
3343 case GL_LINES:
3344 case GL_LINE_LOOP:
3345 case GL_LINE_STRIP:
3346 return false;
3347 default: UNREACHABLE();
3348 }
3349
3350 return false;
3351}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003352
3353void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3354{
3355 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3356
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003357 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3358 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3359 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3360 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003361
daniel@transgaming.com83921382011-01-08 05:46:00 +00003362 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003363}
3364
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003365// keep list sorted in following order
3366// OES extensions
3367// EXT extensions
3368// Vendor extensions
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003369void Context::initExtensionString()
3370{
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003371 mExtensionString = "";
3372
3373 // OES extensions
3374 if (supports32bitIndices())
3375 {
3376 mExtensionString += "GL_OES_element_index_uint ";
3377 }
3378
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003379 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003380 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003381 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003382
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003383 if (supportsFloat16Textures())
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003384 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003385 mExtensionString += "GL_OES_texture_half_float ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003386 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003387 if (supportsFloat16LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003388 {
3389 mExtensionString += "GL_OES_texture_half_float_linear ";
3390 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003391 if (supportsFloat32Textures())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003392 {
3393 mExtensionString += "GL_OES_texture_float ";
3394 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003395 if (supportsFloat32LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003396 {
3397 mExtensionString += "GL_OES_texture_float_linear ";
3398 }
3399
3400 if (supportsNonPower2Texture())
3401 {
3402 mExtensionString += "GL_OES_texture_npot ";
3403 }
3404
3405 // Multi-vendor (EXT) extensions
3406 mExtensionString += "GL_EXT_read_format_bgra ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003407
gman@chromium.org50c526d2011-08-10 05:19:44 +00003408 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00003409 {
3410 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3411 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003412
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003413 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
gman@chromium.org50c526d2011-08-10 05:19:44 +00003414
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003415 // ANGLE-specific extensions
3416 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003417 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003418 {
3419 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3420 }
3421
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003422 if (supportsDXT3Textures())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003423 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003424 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3425 }
3426 if (supportsDXT5Textures())
3427 {
3428 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003429 }
zmo@google.coma574f782011-10-03 21:45:23 +00003430 mExtensionString += "GL_ANGLE_translated_shader_source ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003431
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003432 // Other vendor-specific extensions
3433 if (supportsEventQueries())
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003434 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003435 mExtensionString += "GL_NV_fence ";
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003436 }
3437
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003438 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3439 if (end != std::string::npos)
3440 {
3441 mExtensionString.resize(end+1);
3442 }
3443}
3444
3445const char *Context::getExtensionString() const
3446{
3447 return mExtensionString.c_str();
3448}
3449
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003450void Context::initRendererString()
3451{
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003452 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003453
3454 mRendererString = "ANGLE (";
3455 mRendererString += identifier->Description;
3456 mRendererString += ")";
3457}
3458
3459const char *Context::getRendererString() const
3460{
3461 return mRendererString.c_str();
3462}
3463
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003464void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3465 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3466 GLbitfield mask)
3467{
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003468 Framebuffer *readFramebuffer = getReadFramebuffer();
3469 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3470
3471 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3472 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3473 {
3474 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3475 }
3476
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003477 if (drawFramebuffer->getSamples() != 0)
3478 {
3479 return error(GL_INVALID_OPERATION);
3480 }
3481
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003482 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3483 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3484 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3485 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3486
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003487 RECT sourceRect;
3488 RECT destRect;
3489
3490 if (srcX0 < srcX1)
3491 {
3492 sourceRect.left = srcX0;
3493 sourceRect.right = srcX1;
3494 destRect.left = dstX0;
3495 destRect.right = dstX1;
3496 }
3497 else
3498 {
3499 sourceRect.left = srcX1;
3500 destRect.left = dstX1;
3501 sourceRect.right = srcX0;
3502 destRect.right = dstX0;
3503 }
3504
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003505 if (srcY0 < srcY1)
3506 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003507 sourceRect.top = readBufferHeight - srcY1;
3508 destRect.top = drawBufferHeight - dstY1;
3509 sourceRect.bottom = readBufferHeight - srcY0;
3510 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003511 }
3512 else
3513 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003514 sourceRect.top = readBufferHeight - srcY0;
3515 destRect.top = drawBufferHeight - dstY0;
3516 sourceRect.bottom = readBufferHeight - srcY1;
3517 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003518 }
3519
3520 RECT sourceScissoredRect = sourceRect;
3521 RECT destScissoredRect = destRect;
3522
3523 if (mState.scissorTest)
3524 {
3525 // Only write to parts of the destination framebuffer which pass the scissor test
3526 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3527 // rect will be checked against scissorY, rather than the bottom.
3528 if (destRect.left < mState.scissorX)
3529 {
3530 int xDiff = mState.scissorX - destRect.left;
3531 destScissoredRect.left = mState.scissorX;
3532 sourceScissoredRect.left += xDiff;
3533 }
3534
3535 if (destRect.right > mState.scissorX + mState.scissorWidth)
3536 {
3537 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3538 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3539 sourceScissoredRect.right -= xDiff;
3540 }
3541
3542 if (destRect.top < mState.scissorY)
3543 {
3544 int yDiff = mState.scissorY - destRect.top;
3545 destScissoredRect.top = mState.scissorY;
3546 sourceScissoredRect.top += yDiff;
3547 }
3548
3549 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3550 {
3551 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3552 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3553 sourceScissoredRect.bottom -= yDiff;
3554 }
3555 }
3556
3557 bool blitRenderTarget = false;
3558 bool blitDepthStencil = false;
3559
3560 RECT sourceTrimmedRect = sourceScissoredRect;
3561 RECT destTrimmedRect = destScissoredRect;
3562
3563 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3564 // the actual draw and read surfaces.
3565 if (sourceTrimmedRect.left < 0)
3566 {
3567 int xDiff = 0 - sourceTrimmedRect.left;
3568 sourceTrimmedRect.left = 0;
3569 destTrimmedRect.left += xDiff;
3570 }
3571
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003572 if (sourceTrimmedRect.right > readBufferWidth)
3573 {
3574 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3575 sourceTrimmedRect.right = readBufferWidth;
3576 destTrimmedRect.right -= xDiff;
3577 }
3578
3579 if (sourceTrimmedRect.top < 0)
3580 {
3581 int yDiff = 0 - sourceTrimmedRect.top;
3582 sourceTrimmedRect.top = 0;
3583 destTrimmedRect.top += yDiff;
3584 }
3585
3586 if (sourceTrimmedRect.bottom > readBufferHeight)
3587 {
3588 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3589 sourceTrimmedRect.bottom = readBufferHeight;
3590 destTrimmedRect.bottom -= yDiff;
3591 }
3592
3593 if (destTrimmedRect.left < 0)
3594 {
3595 int xDiff = 0 - destTrimmedRect.left;
3596 destTrimmedRect.left = 0;
3597 sourceTrimmedRect.left += xDiff;
3598 }
3599
3600 if (destTrimmedRect.right > drawBufferWidth)
3601 {
3602 int xDiff = destTrimmedRect.right - drawBufferWidth;
3603 destTrimmedRect.right = drawBufferWidth;
3604 sourceTrimmedRect.right -= xDiff;
3605 }
3606
3607 if (destTrimmedRect.top < 0)
3608 {
3609 int yDiff = 0 - destTrimmedRect.top;
3610 destTrimmedRect.top = 0;
3611 sourceTrimmedRect.top += yDiff;
3612 }
3613
3614 if (destTrimmedRect.bottom > drawBufferHeight)
3615 {
3616 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3617 destTrimmedRect.bottom = drawBufferHeight;
3618 sourceTrimmedRect.bottom -= yDiff;
3619 }
3620
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003621 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003622 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3623 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3624 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3625 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003626 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3627 {
3628 partialBufferCopy = true;
3629 }
3630
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003631 if (mask & GL_COLOR_BUFFER_BIT)
3632 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003633 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3634 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3635 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3636 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3637 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003638 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3639 {
3640 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3641 return error(GL_INVALID_OPERATION);
3642 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003643
3644 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3645 {
3646 return error(GL_INVALID_OPERATION);
3647 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003648
3649 blitRenderTarget = true;
3650
3651 }
3652
3653 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3654 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00003655 Renderbuffer *readDSBuffer = NULL;
3656 Renderbuffer *drawDSBuffer = NULL;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003657
3658 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3659 // both a depth and stencil buffer, it will be the same buffer.
3660
3661 if (mask & GL_DEPTH_BUFFER_BIT)
3662 {
3663 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3664 {
3665 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3666 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3667 {
3668 return error(GL_INVALID_OPERATION);
3669 }
3670
3671 blitDepthStencil = true;
3672 readDSBuffer = readFramebuffer->getDepthbuffer();
3673 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3674 }
3675 }
3676
3677 if (mask & GL_STENCIL_BUFFER_BIT)
3678 {
3679 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3680 {
3681 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3682 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3683 {
3684 return error(GL_INVALID_OPERATION);
3685 }
3686
3687 blitDepthStencil = true;
3688 readDSBuffer = readFramebuffer->getStencilbuffer();
3689 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3690 }
3691 }
3692
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003693 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003694 {
3695 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3696 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3697 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003698
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003699 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3700 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003701 {
3702 return error(GL_INVALID_OPERATION);
3703 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003704 }
3705
3706 if (blitRenderTarget || blitDepthStencil)
3707 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003708 mDisplay->endScene();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003709
3710 if (blitRenderTarget)
3711 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003712 HRESULT result = mDevice->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003713 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3714
3715 if (FAILED(result))
3716 {
3717 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3718 return;
3719 }
3720 }
3721
3722 if (blitDepthStencil)
3723 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003724 HRESULT result = mDevice->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003725
3726 if (FAILED(result))
3727 {
3728 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3729 return;
3730 }
3731 }
3732 }
3733}
3734
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003735VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
3736{
3737 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3738 {
3739 mVertexDeclCache[i].vertexDeclaration = NULL;
3740 mVertexDeclCache[i].lruCount = 0;
3741 }
3742}
3743
3744VertexDeclarationCache::~VertexDeclarationCache()
3745{
3746 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3747 {
3748 if (mVertexDeclCache[i].vertexDeclaration)
3749 {
3750 mVertexDeclCache[i].vertexDeclaration->Release();
3751 }
3752 }
3753}
3754
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003755GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], Program *program)
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003756{
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003757 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
3758 D3DVERTEXELEMENT9 *element = &elements[0];
3759
3760 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3761 {
3762 if (attributes[i].active)
3763 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003764 if (mAppliedVBs[i].serial != attributes[i].serial ||
3765 mAppliedVBs[i].stride != attributes[i].stride ||
3766 mAppliedVBs[i].offset != attributes[i].offset)
3767 {
3768 device->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
3769 mAppliedVBs[i].serial = attributes[i].serial;
3770 mAppliedVBs[i].stride = attributes[i].stride;
3771 mAppliedVBs[i].offset = attributes[i].offset;
3772 }
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003773
3774 element->Stream = i;
3775 element->Offset = 0;
3776 element->Type = attributes[i].type;
3777 element->Method = D3DDECLMETHOD_DEFAULT;
3778 element->Usage = D3DDECLUSAGE_TEXCOORD;
3779 element->UsageIndex = program->getSemanticIndex(i);
3780 element++;
3781 }
3782 }
3783
3784 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
3785 *(element++) = end;
3786
3787 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3788 {
3789 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
3790 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
3791 {
3792 entry->lruCount = ++mMaxLru;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003793 if(entry->vertexDeclaration != mLastSetVDecl)
3794 {
3795 device->SetVertexDeclaration(entry->vertexDeclaration);
3796 mLastSetVDecl = entry->vertexDeclaration;
3797 }
3798
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003799 return GL_NO_ERROR;
3800 }
3801 }
3802
3803 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
3804
3805 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3806 {
3807 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
3808 {
3809 lastCache = &mVertexDeclCache[i];
3810 }
3811 }
3812
3813 if (lastCache->vertexDeclaration != NULL)
3814 {
3815 lastCache->vertexDeclaration->Release();
3816 lastCache->vertexDeclaration = NULL;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003817 // mLastSetVDecl is set to the replacement, so we don't have to worry
3818 // about it.
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003819 }
3820
3821 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
3822 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
3823 device->SetVertexDeclaration(lastCache->vertexDeclaration);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003824 mLastSetVDecl = lastCache->vertexDeclaration;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003825 lastCache->lruCount = ++mMaxLru;
3826
3827 return GL_NO_ERROR;
3828}
3829
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003830void VertexDeclarationCache::markStateDirty()
3831{
3832 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3833 {
3834 mAppliedVBs[i].serial = 0;
3835 }
3836
3837 mLastSetVDecl = NULL;
3838}
3839
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003840}
3841
3842extern "C"
3843{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003844gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003845{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003846 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003847}
3848
3849void glDestroyContext(gl::Context *context)
3850{
3851 delete context;
3852
3853 if (context == gl::getContext())
3854 {
3855 gl::makeCurrent(NULL, NULL, NULL);
3856 }
3857}
3858
3859void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3860{
3861 gl::makeCurrent(context, display, surface);
3862}
3863
3864gl::Context *glGetCurrentContext()
3865{
3866 return gl::getContext();
3867}
3868}