blob: 37cc8f7d78c7d1a8356efe9dce515dcbef36e1b3 [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.com4fa08332010-05-11 02:29:27 +0000162
gman@chromium.org50c526d2011-08-10 05:19:44 +0000163 mSupportsDXT1Textures = false;
164 mSupportsDXT3Textures = false;
165 mSupportsDXT5Textures = false;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000166 mSupportsEventQueries = false;
gman@chromium.org50c526d2011-08-10 05:19:44 +0000167 mNumCompressedTextureFormats = 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000168 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000169 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000170 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000171}
172
173Context::~Context()
174{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000175 if (mState.currentProgram != 0)
176 {
177 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
178 if (programObject)
179 {
180 programObject->release();
181 }
182 mState.currentProgram = 0;
183 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000184
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000185 while (!mFramebufferMap.empty())
186 {
187 deleteFramebuffer(mFramebufferMap.begin()->first);
188 }
189
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000190 while (!mFenceMap.empty())
191 {
192 deleteFence(mFenceMap.begin()->first);
193 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000194
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000195 while (!mMultiSampleSupport.empty())
196 {
197 delete [] mMultiSampleSupport.begin()->second;
198 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
199 }
200
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000201 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000202 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +0000203 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000204 {
205 mState.samplerTexture[type][sampler].set(NULL);
206 }
207 }
208
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000209 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000210 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000211 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000212 }
213
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000214 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
215 {
216 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
217 }
218
219 mState.arrayBuffer.set(NULL);
220 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000221 mState.renderbuffer.set(NULL);
222
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000223 mTexture2DZero.set(NULL);
224 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000225
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000226 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000227 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000228 delete mBlit;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000229 delete mClosingIB;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000230
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000231 if (mMaskedClearSavedState)
232 {
233 mMaskedClearSavedState->Release();
234 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000235
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000236 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000237}
238
239void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
240{
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000241 mDisplay = display;
242 mDevice = mDisplay->getDevice();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000243
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000244 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000245 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000246 mDeviceCaps = mDisplay->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000247
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000248 mVertexDataManager = new VertexDataManager(this, mDevice);
249 mIndexDataManager = new IndexDataManager(this, mDevice);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000250 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000251
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000252 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000253 mSupportsVertexTexture = mDisplay->getVertexTextureSupport();
254 mSupportsNonPower2Texture = mDisplay->getNonPower2TextureSupport();
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000255
256 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
257 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
258 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
259 mMaxRenderbufferDimension = mMaxTextureDimension;
260 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
261 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
262 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
263
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000264 const D3DFORMAT renderBufferFormats[] =
265 {
266 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000267 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000268 D3DFMT_R5G6B5,
269 D3DFMT_D24S8
270 };
271
272 int max = 0;
273 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
274 {
275 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000276 mDisplay->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000277 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
278
279 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
280 {
281 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
282 {
283 max = j;
284 }
285 }
286 }
287
288 mMaxSupportedSamples = max;
289
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000290 mSupportsEventQueries = mDisplay->getEventQuerySupport();
291 mSupportsDXT1Textures = mDisplay->getDXT1TextureSupport();
292 mSupportsDXT3Textures = mDisplay->getDXT3TextureSupport();
293 mSupportsDXT5Textures = mDisplay->getDXT5TextureSupport();
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +0000294 mSupportsFloat32Textures = mDisplay->getFloat32TextureSupport(&mSupportsFloat32LinearFilter, &mSupportsFloat32RenderableTextures);
295 mSupportsFloat16Textures = mDisplay->getFloat16TextureSupport(&mSupportsFloat16LinearFilter, &mSupportsFloat16RenderableTextures);
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000296 mSupportsLuminanceTextures = mDisplay->getLuminanceTextureSupport();
297 mSupportsLuminanceAlphaTextures = mDisplay->getLuminanceAlphaTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000298
daniel@transgaming.com83921382011-01-08 05:46:00 +0000299 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
300
gman@chromium.org50c526d2011-08-10 05:19:44 +0000301 mNumCompressedTextureFormats = 0;
302 if (supportsDXT1Textures())
303 {
304 mNumCompressedTextureFormats += 2;
305 }
306 if (supportsDXT3Textures())
307 {
308 mNumCompressedTextureFormats += 1;
309 }
310 if (supportsDXT5Textures())
311 {
312 mNumCompressedTextureFormats += 1;
313 }
314
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000315 initExtensionString();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +0000316 initRendererString();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000317
318 mState.viewportX = 0;
319 mState.viewportY = 0;
320 mState.viewportWidth = surface->getWidth();
321 mState.viewportHeight = surface->getHeight();
322
323 mState.scissorX = 0;
324 mState.scissorY = 0;
325 mState.scissorWidth = surface->getWidth();
326 mState.scissorHeight = surface->getHeight();
327
328 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000329 }
330
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000331 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
332 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000333 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000334
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000335 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000336 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000337 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000338
339 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000340
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000341 if (defaultRenderTarget)
342 {
343 defaultRenderTarget->Release();
344 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000345
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000346 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000347 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000348 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000349 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000350
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000351 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000352}
353
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000354// 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 +0000355void Context::markAllStateDirty()
356{
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000357 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
358 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +0000359 mAppliedTextureSerialPS[t] = 0;
360 }
361
362 for (int t = 0; t < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; t++)
363 {
364 mAppliedTextureSerialVS[t] = 0;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000365 }
366
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000367 mAppliedProgramSerial = 0;
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000368 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000369 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000370 mAppliedStencilbufferSerial = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000371 mAppliedIBSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000372 mDepthStencilInitialized = false;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000373 mViewportInitialized = false;
374 mRenderTargetDescInitialized = false;
375
376 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000377
378 mClearStateDirty = true;
379 mCullStateDirty = true;
380 mDepthStateDirty = true;
381 mMaskStateDirty = true;
382 mBlendStateDirty = true;
383 mStencilStateDirty = true;
384 mPolygonOffsetStateDirty = true;
385 mScissorStateDirty = true;
386 mSampleStateDirty = true;
387 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000388 mFrontFaceDirty = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +0000389 mDxUniformsDirty = true;
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000390 mCachedCurrentProgram = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000391}
392
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000393void Context::markContextLost()
394{
395 mContextLost = true;
396}
397
398bool Context::isContextLost()
399{
400 return mContextLost;
401}
402
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403void Context::setClearColor(float red, float green, float blue, float alpha)
404{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000405 mState.colorClearValue.red = red;
406 mState.colorClearValue.green = green;
407 mState.colorClearValue.blue = blue;
408 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000409}
410
411void Context::setClearDepth(float depth)
412{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000413 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000414}
415
416void Context::setClearStencil(int stencil)
417{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000418 mState.stencilClearValue = stencil;
419}
420
421void Context::setCullFace(bool enabled)
422{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000423 if (mState.cullFace != enabled)
424 {
425 mState.cullFace = enabled;
426 mCullStateDirty = true;
427 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000428}
429
430bool Context::isCullFaceEnabled() const
431{
432 return mState.cullFace;
433}
434
435void Context::setCullMode(GLenum mode)
436{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000437 if (mState.cullMode != mode)
438 {
439 mState.cullMode = mode;
440 mCullStateDirty = true;
441 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000442}
443
444void Context::setFrontFace(GLenum front)
445{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000446 if (mState.frontFace != front)
447 {
448 mState.frontFace = front;
449 mFrontFaceDirty = true;
450 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000451}
452
453void Context::setDepthTest(bool enabled)
454{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000455 if (mState.depthTest != enabled)
456 {
457 mState.depthTest = enabled;
458 mDepthStateDirty = true;
459 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000460}
461
462bool Context::isDepthTestEnabled() const
463{
464 return mState.depthTest;
465}
466
467void Context::setDepthFunc(GLenum depthFunc)
468{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000469 if (mState.depthFunc != depthFunc)
470 {
471 mState.depthFunc = depthFunc;
472 mDepthStateDirty = true;
473 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000474}
475
476void Context::setDepthRange(float zNear, float zFar)
477{
478 mState.zNear = zNear;
479 mState.zFar = zFar;
480}
481
482void Context::setBlend(bool enabled)
483{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000484 if (mState.blend != enabled)
485 {
486 mState.blend = enabled;
487 mBlendStateDirty = true;
488 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000489}
490
491bool Context::isBlendEnabled() const
492{
493 return mState.blend;
494}
495
496void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
497{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000498 if (mState.sourceBlendRGB != sourceRGB ||
499 mState.sourceBlendAlpha != sourceAlpha ||
500 mState.destBlendRGB != destRGB ||
501 mState.destBlendAlpha != destAlpha)
502 {
503 mState.sourceBlendRGB = sourceRGB;
504 mState.destBlendRGB = destRGB;
505 mState.sourceBlendAlpha = sourceAlpha;
506 mState.destBlendAlpha = destAlpha;
507 mBlendStateDirty = true;
508 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000509}
510
511void Context::setBlendColor(float red, float green, float blue, float alpha)
512{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000513 if (mState.blendColor.red != red ||
514 mState.blendColor.green != green ||
515 mState.blendColor.blue != blue ||
516 mState.blendColor.alpha != alpha)
517 {
518 mState.blendColor.red = red;
519 mState.blendColor.green = green;
520 mState.blendColor.blue = blue;
521 mState.blendColor.alpha = alpha;
522 mBlendStateDirty = true;
523 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000524}
525
526void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
527{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000528 if (mState.blendEquationRGB != rgbEquation ||
529 mState.blendEquationAlpha != alphaEquation)
530 {
531 mState.blendEquationRGB = rgbEquation;
532 mState.blendEquationAlpha = alphaEquation;
533 mBlendStateDirty = true;
534 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000535}
536
537void Context::setStencilTest(bool enabled)
538{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000539 if (mState.stencilTest != enabled)
540 {
541 mState.stencilTest = enabled;
542 mStencilStateDirty = true;
543 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000544}
545
546bool Context::isStencilTestEnabled() const
547{
548 return mState.stencilTest;
549}
550
551void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
552{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000553 if (mState.stencilFunc != stencilFunc ||
554 mState.stencilRef != stencilRef ||
555 mState.stencilMask != stencilMask)
556 {
557 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000558 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000559 mState.stencilMask = stencilMask;
560 mStencilStateDirty = true;
561 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000562}
563
564void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
565{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000566 if (mState.stencilBackFunc != stencilBackFunc ||
567 mState.stencilBackRef != stencilBackRef ||
568 mState.stencilBackMask != stencilBackMask)
569 {
570 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000571 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000572 mState.stencilBackMask = stencilBackMask;
573 mStencilStateDirty = true;
574 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000575}
576
577void Context::setStencilWritemask(GLuint stencilWritemask)
578{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000579 if (mState.stencilWritemask != stencilWritemask)
580 {
581 mState.stencilWritemask = stencilWritemask;
582 mStencilStateDirty = true;
583 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000584}
585
586void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
587{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000588 if (mState.stencilBackWritemask != stencilBackWritemask)
589 {
590 mState.stencilBackWritemask = stencilBackWritemask;
591 mStencilStateDirty = true;
592 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000593}
594
595void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
596{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000597 if (mState.stencilFail != stencilFail ||
598 mState.stencilPassDepthFail != stencilPassDepthFail ||
599 mState.stencilPassDepthPass != stencilPassDepthPass)
600 {
601 mState.stencilFail = stencilFail;
602 mState.stencilPassDepthFail = stencilPassDepthFail;
603 mState.stencilPassDepthPass = stencilPassDepthPass;
604 mStencilStateDirty = true;
605 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000606}
607
608void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
609{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000610 if (mState.stencilBackFail != stencilBackFail ||
611 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
612 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
613 {
614 mState.stencilBackFail = stencilBackFail;
615 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
616 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
617 mStencilStateDirty = true;
618 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000619}
620
621void Context::setPolygonOffsetFill(bool enabled)
622{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000623 if (mState.polygonOffsetFill != enabled)
624 {
625 mState.polygonOffsetFill = enabled;
626 mPolygonOffsetStateDirty = true;
627 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000628}
629
630bool Context::isPolygonOffsetFillEnabled() const
631{
632 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000633
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000634}
635
636void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
637{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000638 if (mState.polygonOffsetFactor != factor ||
639 mState.polygonOffsetUnits != units)
640 {
641 mState.polygonOffsetFactor = factor;
642 mState.polygonOffsetUnits = units;
643 mPolygonOffsetStateDirty = true;
644 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000645}
646
647void Context::setSampleAlphaToCoverage(bool enabled)
648{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000649 if (mState.sampleAlphaToCoverage != enabled)
650 {
651 mState.sampleAlphaToCoverage = enabled;
652 mSampleStateDirty = true;
653 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000654}
655
656bool Context::isSampleAlphaToCoverageEnabled() const
657{
658 return mState.sampleAlphaToCoverage;
659}
660
661void Context::setSampleCoverage(bool enabled)
662{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000663 if (mState.sampleCoverage != enabled)
664 {
665 mState.sampleCoverage = enabled;
666 mSampleStateDirty = true;
667 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000668}
669
670bool Context::isSampleCoverageEnabled() const
671{
672 return mState.sampleCoverage;
673}
674
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000675void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000676{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000677 if (mState.sampleCoverageValue != value ||
678 mState.sampleCoverageInvert != invert)
679 {
680 mState.sampleCoverageValue = value;
681 mState.sampleCoverageInvert = invert;
682 mSampleStateDirty = true;
683 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000684}
685
686void Context::setScissorTest(bool enabled)
687{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000688 if (mState.scissorTest != enabled)
689 {
690 mState.scissorTest = enabled;
691 mScissorStateDirty = true;
692 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000693}
694
695bool Context::isScissorTestEnabled() const
696{
697 return mState.scissorTest;
698}
699
700void Context::setDither(bool enabled)
701{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000702 if (mState.dither != enabled)
703 {
704 mState.dither = enabled;
705 mDitherStateDirty = true;
706 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000707}
708
709bool Context::isDitherEnabled() const
710{
711 return mState.dither;
712}
713
714void Context::setLineWidth(GLfloat width)
715{
716 mState.lineWidth = width;
717}
718
719void Context::setGenerateMipmapHint(GLenum hint)
720{
721 mState.generateMipmapHint = hint;
722}
723
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000724void Context::setFragmentShaderDerivativeHint(GLenum hint)
725{
726 mState.fragmentShaderDerivativeHint = hint;
727 // TODO: Propagate the hint to shader translator so we can write
728 // ddx, ddx_coarse, or ddx_fine depending on the hint.
729 // Ignore for now. It is valid for implementations to ignore hint.
730}
731
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000732void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
733{
734 mState.viewportX = x;
735 mState.viewportY = y;
736 mState.viewportWidth = width;
737 mState.viewportHeight = height;
738}
739
740void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
741{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000742 if (mState.scissorX != x || mState.scissorY != y ||
743 mState.scissorWidth != width || mState.scissorHeight != height)
744 {
745 mState.scissorX = x;
746 mState.scissorY = y;
747 mState.scissorWidth = width;
748 mState.scissorHeight = height;
749 mScissorStateDirty = true;
750 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000751}
752
753void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
754{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000755 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
756 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
757 {
758 mState.colorMaskRed = red;
759 mState.colorMaskGreen = green;
760 mState.colorMaskBlue = blue;
761 mState.colorMaskAlpha = alpha;
762 mMaskStateDirty = true;
763 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000764}
765
766void Context::setDepthMask(bool mask)
767{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000768 if (mState.depthMask != mask)
769 {
770 mState.depthMask = mask;
771 mMaskStateDirty = true;
772 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000773}
774
daniel@transgaming.comdfd57022011-05-11 15:37:25 +0000775void Context::setActiveSampler(unsigned int active)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000776{
777 mState.activeSampler = active;
778}
779
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000780GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000781{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000782 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000783}
784
785GLuint Context::getDrawFramebufferHandle() const
786{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000787 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000788}
789
790GLuint Context::getRenderbufferHandle() const
791{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000792 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000793}
794
795GLuint Context::getArrayBufferHandle() const
796{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000797 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000798}
799
daniel@transgaming.com83921382011-01-08 05:46:00 +0000800void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000801{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000802 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000803}
804
daniel@transgaming.com83921382011-01-08 05:46:00 +0000805const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000806{
807 return mState.vertexAttribute[attribNum];
808}
809
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000810void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000811 GLsizei stride, const void *pointer)
812{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000813 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000814 mState.vertexAttribute[attribNum].mSize = size;
815 mState.vertexAttribute[attribNum].mType = type;
816 mState.vertexAttribute[attribNum].mNormalized = normalized;
817 mState.vertexAttribute[attribNum].mStride = stride;
818 mState.vertexAttribute[attribNum].mPointer = pointer;
819}
820
821const void *Context::getVertexAttribPointer(unsigned int attribNum) const
822{
823 return mState.vertexAttribute[attribNum].mPointer;
824}
825
daniel@transgaming.com83921382011-01-08 05:46:00 +0000826const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000827{
828 return mState.vertexAttribute;
829}
830
831void Context::setPackAlignment(GLint alignment)
832{
833 mState.packAlignment = alignment;
834}
835
836GLint Context::getPackAlignment() const
837{
838 return mState.packAlignment;
839}
840
841void Context::setUnpackAlignment(GLint alignment)
842{
843 mState.unpackAlignment = alignment;
844}
845
846GLint Context::getUnpackAlignment() const
847{
848 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000849}
850
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851GLuint Context::createBuffer()
852{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000853 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000854}
855
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856GLuint Context::createProgram()
857{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000858 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000859}
860
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000861GLuint Context::createShader(GLenum type)
862{
863 return mResourceManager->createShader(type);
864}
865
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000866GLuint Context::createTexture()
867{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000868 return mResourceManager->createTexture();
869}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000871GLuint Context::createRenderbuffer()
872{
873 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000874}
875
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000876// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877GLuint Context::createFramebuffer()
878{
benvanik@google.com1a233342011-04-28 19:44:39 +0000879 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000880
881 mFramebufferMap[handle] = NULL;
882
883 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000884}
885
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000886GLuint Context::createFence()
887{
benvanik@google.com1a233342011-04-28 19:44:39 +0000888 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000889
890 mFenceMap[handle] = new Fence;
891
892 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000893}
894
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000895void Context::deleteBuffer(GLuint buffer)
896{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000897 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000898 {
899 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000900 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000901
902 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903}
904
905void Context::deleteShader(GLuint shader)
906{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000907 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000908}
909
910void Context::deleteProgram(GLuint program)
911{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000912 mResourceManager->deleteProgram(program);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000913 mCachedCurrentProgram = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000914}
915
916void Context::deleteTexture(GLuint texture)
917{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000918 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000919 {
920 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000921 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000922
923 mResourceManager->deleteTexture(texture);
924}
925
926void Context::deleteRenderbuffer(GLuint renderbuffer)
927{
928 if (mResourceManager->getRenderbuffer(renderbuffer))
929 {
930 detachRenderbuffer(renderbuffer);
931 }
932
933 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000934}
935
936void Context::deleteFramebuffer(GLuint framebuffer)
937{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000938 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
939
940 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000941 {
942 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000943
benvanik@google.com1a233342011-04-28 19:44:39 +0000944 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000945 delete framebufferObject->second;
946 mFramebufferMap.erase(framebufferObject);
947 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000948}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000949
950void Context::deleteFence(GLuint fence)
951{
952 FenceMap::iterator fenceObject = mFenceMap.find(fence);
953
954 if (fenceObject != mFenceMap.end())
955 {
benvanik@google.com1a233342011-04-28 19:44:39 +0000956 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000957 delete fenceObject->second;
958 mFenceMap.erase(fenceObject);
959 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000960}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000961
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000962Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000964 return mResourceManager->getBuffer(handle);
965}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000967Shader *Context::getShader(GLuint handle)
968{
969 return mResourceManager->getShader(handle);
970}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000972Program *Context::getProgram(GLuint handle)
973{
974 return mResourceManager->getProgram(handle);
975}
976
977Texture *Context::getTexture(GLuint handle)
978{
979 return mResourceManager->getTexture(handle);
980}
981
982Renderbuffer *Context::getRenderbuffer(GLuint handle)
983{
984 return mResourceManager->getRenderbuffer(handle);
985}
986
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000987Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000988{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000989 return getFramebuffer(mState.readFramebuffer);
990}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000991
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000992Framebuffer *Context::getDrawFramebuffer()
993{
jbauman@chromium.org040c4db2011-10-13 21:35:52 +0000994 return mBoundDrawFramebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000995}
996
997void Context::bindArrayBuffer(unsigned int buffer)
998{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000999 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001000
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001001 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001002}
1003
1004void Context::bindElementArrayBuffer(unsigned int buffer)
1005{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001006 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001007
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001008 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001009}
1010
1011void Context::bindTexture2D(GLuint texture)
1012{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001013 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001014
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001015 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001016}
1017
1018void Context::bindTextureCubeMap(GLuint texture)
1019{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001020 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001021
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001022 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001023}
1024
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001025void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001026{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001027 if (!getFramebuffer(framebuffer))
1028 {
1029 mFramebufferMap[framebuffer] = new Framebuffer();
1030 }
1031
1032 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001033}
1034
1035void Context::bindDrawFramebuffer(GLuint framebuffer)
1036{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001037 if (!getFramebuffer(framebuffer))
1038 {
1039 mFramebufferMap[framebuffer] = new Framebuffer();
1040 }
1041
1042 mState.drawFramebuffer = framebuffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001043
1044 mBoundDrawFramebuffer = getFramebuffer(framebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001045}
1046
1047void Context::bindRenderbuffer(GLuint renderbuffer)
1048{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001049 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1050
1051 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001052}
1053
1054void Context::useProgram(GLuint program)
1055{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001056 GLuint priorProgram = mState.currentProgram;
1057 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001058
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001059 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001060 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001061 Program *newProgram = mResourceManager->getProgram(program);
1062 Program *oldProgram = mResourceManager->getProgram(priorProgram);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001063 mCachedCurrentProgram = NULL;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001064 mDxUniformsDirty = true;
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001065
1066 if (newProgram)
1067 {
1068 newProgram->addRef();
1069 }
1070
1071 if (oldProgram)
1072 {
1073 oldProgram->release();
1074 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001075 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001076}
1077
1078void Context::setFramebufferZero(Framebuffer *buffer)
1079{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001080 delete mFramebufferMap[0];
1081 mFramebufferMap[0] = buffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001082 if (mState.drawFramebuffer == 0)
1083 {
1084 mBoundDrawFramebuffer = buffer;
1085 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001086}
1087
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001088void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001089{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001090 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1091 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001092}
1093
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001094Framebuffer *Context::getFramebuffer(unsigned int handle)
1095{
1096 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1097
1098 if (framebuffer == mFramebufferMap.end())
1099 {
1100 return NULL;
1101 }
1102 else
1103 {
1104 return framebuffer->second;
1105 }
1106}
1107
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001108Fence *Context::getFence(unsigned int handle)
1109{
1110 FenceMap::iterator fence = mFenceMap.find(handle);
1111
1112 if (fence == mFenceMap.end())
1113 {
1114 return NULL;
1115 }
1116 else
1117 {
1118 return fence->second;
1119 }
1120}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001121
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001122Buffer *Context::getArrayBuffer()
1123{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001124 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001125}
1126
1127Buffer *Context::getElementArrayBuffer()
1128{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001129 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001130}
1131
1132Program *Context::getCurrentProgram()
1133{
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001134 if (!mCachedCurrentProgram)
1135 {
1136 mCachedCurrentProgram = mResourceManager->getProgram(mState.currentProgram);
1137 }
1138 return mCachedCurrentProgram;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001139}
1140
1141Texture2D *Context::getTexture2D()
1142{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001143 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001144}
1145
1146TextureCubeMap *Context::getTextureCubeMap()
1147{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001148 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001149}
1150
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001151Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001152{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001153 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001154
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001155 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001156 {
1157 switch (type)
1158 {
1159 default: UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001160 case TEXTURE_2D: return mTexture2DZero.get();
1161 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001162 }
1163 }
1164
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001165 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001166}
1167
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001168bool Context::getBooleanv(GLenum pname, GLboolean *params)
1169{
1170 switch (pname)
1171 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001172 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1173 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1174 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001175 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001176 params[0] = mState.colorMaskRed;
1177 params[1] = mState.colorMaskGreen;
1178 params[2] = mState.colorMaskBlue;
1179 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001180 break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001181 case GL_CULL_FACE: *params = mState.cullFace; break;
1182 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1183 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1184 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1185 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1186 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1187 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1188 case GL_BLEND: *params = mState.blend; break;
1189 case GL_DITHER: *params = mState.dither; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001190 default:
1191 return false;
1192 }
1193
1194 return true;
1195}
1196
1197bool Context::getFloatv(GLenum pname, GLfloat *params)
1198{
1199 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1200 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1201 // GetIntegerv as its native query function. As it would require conversion in any
1202 // case, this should make no difference to the calling application.
1203 switch (pname)
1204 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001205 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1206 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1207 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1208 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1209 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001210 case GL_ALIASED_LINE_WIDTH_RANGE:
1211 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1212 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1213 break;
1214 case GL_ALIASED_POINT_SIZE_RANGE:
1215 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001216 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 +00001217 break;
1218 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001219 params[0] = mState.zNear;
1220 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001221 break;
1222 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001223 params[0] = mState.colorClearValue.red;
1224 params[1] = mState.colorClearValue.green;
1225 params[2] = mState.colorClearValue.blue;
1226 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001227 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001228 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001229 params[0] = mState.blendColor.red;
1230 params[1] = mState.blendColor.green;
1231 params[2] = mState.blendColor.blue;
1232 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001233 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001234 default:
1235 return false;
1236 }
1237
1238 return true;
1239}
1240
1241bool Context::getIntegerv(GLenum pname, GLint *params)
1242{
1243 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1244 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1245 // GetIntegerv as its native query function. As it would require conversion in any
1246 // case, this should make no difference to the calling application. You may find it in
1247 // Context::getFloatv.
1248 switch (pname)
1249 {
1250 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1251 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001252 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00001253 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1254 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001255 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001256 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001257 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001258 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001259 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001260 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1261 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001262 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1263 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1264 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001265 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001266 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1267 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1268 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1269 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001270 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001271 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1272 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1273 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1274 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1275 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1276 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1277 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1278 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1279 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1280 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1281 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1282 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1283 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1284 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1285 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1286 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1287 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1288 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1289 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1290 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1291 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1292 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1293 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1294 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001295 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1296 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001297 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
gman@chromium.org50c526d2011-08-10 05:19:44 +00001298 params[0] = mNumCompressedTextureFormats;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001299 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001300 case GL_MAX_SAMPLES_ANGLE:
1301 {
1302 GLsizei maxSamples = getMaxSupportedSamples();
1303 if (maxSamples != 0)
1304 {
1305 *params = maxSamples;
1306 }
1307 else
1308 {
1309 return false;
1310 }
1311
1312 break;
1313 }
1314 case GL_SAMPLE_BUFFERS:
1315 case GL_SAMPLES:
1316 {
1317 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1318 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1319 {
1320 switch (pname)
1321 {
1322 case GL_SAMPLE_BUFFERS:
1323 if (framebuffer->getSamples() != 0)
1324 {
1325 *params = 1;
1326 }
1327 else
1328 {
1329 *params = 0;
1330 }
1331 break;
1332 case GL_SAMPLES:
1333 *params = framebuffer->getSamples();
1334 break;
1335 }
1336 }
1337 else
1338 {
1339 *params = 0;
1340 }
1341 }
1342 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001343 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1344 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1345 case GL_MAX_VIEWPORT_DIMS:
1346 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001347 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001348 params[0] = maxDimension;
1349 params[1] = maxDimension;
1350 }
1351 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001352 case GL_COMPRESSED_TEXTURE_FORMATS:
1353 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001354 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00001355 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001356 *params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1357 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1358 }
1359 if (supportsDXT3Textures())
1360 {
1361 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
1362 }
1363 if (supportsDXT5Textures())
1364 {
1365 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001366 }
1367 }
1368 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001369 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001370 params[0] = mState.viewportX;
1371 params[1] = mState.viewportY;
1372 params[2] = mState.viewportWidth;
1373 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001374 break;
1375 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001376 params[0] = mState.scissorX;
1377 params[1] = mState.scissorY;
1378 params[2] = mState.scissorWidth;
1379 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001380 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001381 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1382 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001383 case GL_RED_BITS:
1384 case GL_GREEN_BITS:
1385 case GL_BLUE_BITS:
1386 case GL_ALPHA_BITS:
1387 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001388 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001389 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001390
1391 if (colorbuffer)
1392 {
1393 switch (pname)
1394 {
1395 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1396 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1397 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1398 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1399 }
1400 }
1401 else
1402 {
1403 *params = 0;
1404 }
1405 }
1406 break;
1407 case GL_DEPTH_BITS:
1408 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001409 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001410 gl::Renderbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001411
1412 if (depthbuffer)
1413 {
1414 *params = depthbuffer->getDepthSize();
1415 }
1416 else
1417 {
1418 *params = 0;
1419 }
1420 }
1421 break;
1422 case GL_STENCIL_BITS:
1423 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001424 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001425 gl::Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001426
1427 if (stencilbuffer)
1428 {
1429 *params = stencilbuffer->getStencilSize();
1430 }
1431 else
1432 {
1433 *params = 0;
1434 }
1435 }
1436 break;
1437 case GL_TEXTURE_BINDING_2D:
1438 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001439 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001440 {
1441 error(GL_INVALID_OPERATION);
1442 return false;
1443 }
1444
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001445 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001446 }
1447 break;
1448 case GL_TEXTURE_BINDING_CUBE_MAP:
1449 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001450 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001451 {
1452 error(GL_INVALID_OPERATION);
1453 return false;
1454 }
1455
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001456 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001457 }
1458 break;
1459 default:
1460 return false;
1461 }
1462
1463 return true;
1464}
1465
1466bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1467{
1468 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1469 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1470 // to the fact that it is stored internally as a float, and so would require conversion
1471 // if returned from Context::getIntegerv. Since this conversion is already implemented
1472 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1473 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1474 // application.
1475 switch (pname)
1476 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001477 case GL_COMPRESSED_TEXTURE_FORMATS:
1478 {
1479 *type = GL_INT;
1480 *numParams = mNumCompressedTextureFormats;
1481 }
1482 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001483 case GL_SHADER_BINARY_FORMATS:
1484 {
1485 *type = GL_INT;
1486 *numParams = 0;
1487 }
1488 break;
1489 case GL_MAX_VERTEX_ATTRIBS:
1490 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1491 case GL_MAX_VARYING_VECTORS:
1492 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1493 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1494 case GL_MAX_TEXTURE_IMAGE_UNITS:
1495 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1496 case GL_MAX_RENDERBUFFER_SIZE:
1497 case GL_NUM_SHADER_BINARY_FORMATS:
1498 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1499 case GL_ARRAY_BUFFER_BINDING:
1500 case GL_FRAMEBUFFER_BINDING:
1501 case GL_RENDERBUFFER_BINDING:
1502 case GL_CURRENT_PROGRAM:
1503 case GL_PACK_ALIGNMENT:
1504 case GL_UNPACK_ALIGNMENT:
1505 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001506 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001507 case GL_RED_BITS:
1508 case GL_GREEN_BITS:
1509 case GL_BLUE_BITS:
1510 case GL_ALPHA_BITS:
1511 case GL_DEPTH_BITS:
1512 case GL_STENCIL_BITS:
1513 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1514 case GL_CULL_FACE_MODE:
1515 case GL_FRONT_FACE:
1516 case GL_ACTIVE_TEXTURE:
1517 case GL_STENCIL_FUNC:
1518 case GL_STENCIL_VALUE_MASK:
1519 case GL_STENCIL_REF:
1520 case GL_STENCIL_FAIL:
1521 case GL_STENCIL_PASS_DEPTH_FAIL:
1522 case GL_STENCIL_PASS_DEPTH_PASS:
1523 case GL_STENCIL_BACK_FUNC:
1524 case GL_STENCIL_BACK_VALUE_MASK:
1525 case GL_STENCIL_BACK_REF:
1526 case GL_STENCIL_BACK_FAIL:
1527 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1528 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1529 case GL_DEPTH_FUNC:
1530 case GL_BLEND_SRC_RGB:
1531 case GL_BLEND_SRC_ALPHA:
1532 case GL_BLEND_DST_RGB:
1533 case GL_BLEND_DST_ALPHA:
1534 case GL_BLEND_EQUATION_RGB:
1535 case GL_BLEND_EQUATION_ALPHA:
1536 case GL_STENCIL_WRITEMASK:
1537 case GL_STENCIL_BACK_WRITEMASK:
1538 case GL_STENCIL_CLEAR_VALUE:
1539 case GL_SUBPIXEL_BITS:
1540 case GL_MAX_TEXTURE_SIZE:
1541 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1542 case GL_SAMPLE_BUFFERS:
1543 case GL_SAMPLES:
1544 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1545 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1546 case GL_TEXTURE_BINDING_2D:
1547 case GL_TEXTURE_BINDING_CUBE_MAP:
1548 {
1549 *type = GL_INT;
1550 *numParams = 1;
1551 }
1552 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001553 case GL_MAX_SAMPLES_ANGLE:
1554 {
1555 if (getMaxSupportedSamples() != 0)
1556 {
1557 *type = GL_INT;
1558 *numParams = 1;
1559 }
1560 else
1561 {
1562 return false;
1563 }
1564 }
1565 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001566 case GL_MAX_VIEWPORT_DIMS:
1567 {
1568 *type = GL_INT;
1569 *numParams = 2;
1570 }
1571 break;
1572 case GL_VIEWPORT:
1573 case GL_SCISSOR_BOX:
1574 {
1575 *type = GL_INT;
1576 *numParams = 4;
1577 }
1578 break;
1579 case GL_SHADER_COMPILER:
1580 case GL_SAMPLE_COVERAGE_INVERT:
1581 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001582 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1583 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1584 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1585 case GL_SAMPLE_COVERAGE:
1586 case GL_SCISSOR_TEST:
1587 case GL_STENCIL_TEST:
1588 case GL_DEPTH_TEST:
1589 case GL_BLEND:
1590 case GL_DITHER:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001591 {
1592 *type = GL_BOOL;
1593 *numParams = 1;
1594 }
1595 break;
1596 case GL_COLOR_WRITEMASK:
1597 {
1598 *type = GL_BOOL;
1599 *numParams = 4;
1600 }
1601 break;
1602 case GL_POLYGON_OFFSET_FACTOR:
1603 case GL_POLYGON_OFFSET_UNITS:
1604 case GL_SAMPLE_COVERAGE_VALUE:
1605 case GL_DEPTH_CLEAR_VALUE:
1606 case GL_LINE_WIDTH:
1607 {
1608 *type = GL_FLOAT;
1609 *numParams = 1;
1610 }
1611 break;
1612 case GL_ALIASED_LINE_WIDTH_RANGE:
1613 case GL_ALIASED_POINT_SIZE_RANGE:
1614 case GL_DEPTH_RANGE:
1615 {
1616 *type = GL_FLOAT;
1617 *numParams = 2;
1618 }
1619 break;
1620 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001621 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001622 {
1623 *type = GL_FLOAT;
1624 *numParams = 4;
1625 }
1626 break;
1627 default:
1628 return false;
1629 }
1630
1631 return true;
1632}
1633
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001634// Applies the render target surface, depth stencil surface, viewport rectangle and
1635// scissor rectangle to the Direct3D 9 device
1636bool Context::applyRenderTarget(bool ignoreViewport)
1637{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001638 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001639
1640 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1641 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001642 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001643 }
1644
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001645 IDirect3DSurface9 *renderTarget = NULL;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001646 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001647
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001648 bool renderTargetChanged = false;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001649 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1650 if (renderTargetSerial != mAppliedRenderTargetSerial)
1651 {
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001652 renderTarget = framebufferObject->getRenderTarget();
1653
1654 if (!renderTarget)
1655 {
1656 return false; // Context must be lost
1657 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001658 mDevice->SetRenderTarget(0, renderTarget);
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001659 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001660 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 +00001661 renderTargetChanged = true;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001662 }
1663
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001664 unsigned int depthbufferSerial = 0;
1665 unsigned int stencilbufferSerial = 0;
1666 if (framebufferObject->getDepthbufferType() != GL_NONE)
1667 {
1668 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001669 if (!depthStencil)
1670 {
1671 ERR("Depth stencil pointer unexpectedly null.");
1672 return false;
1673 }
1674
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001675 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1676 }
1677 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1678 {
1679 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001680 if (!depthStencil)
1681 {
1682 ERR("Depth stencil pointer unexpectedly null.");
1683 return false;
1684 }
1685
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001686 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1687 }
1688
1689 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001690 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001691 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001692 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001693 mDevice->SetDepthStencilSurface(depthStencil);
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001694 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001695 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001696 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001697 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001698
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001699 if (!mRenderTargetDescInitialized || renderTargetChanged)
1700 {
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001701 if (!renderTarget)
1702 {
1703 renderTarget = framebufferObject->getRenderTarget();
1704
1705 if (!renderTarget)
1706 {
1707 return false; // Context must be lost
1708 }
1709 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001710 renderTarget->GetDesc(&mRenderTargetDesc);
1711 mRenderTargetDescInitialized = true;
1712 }
1713
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001714 D3DVIEWPORT9 viewport;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001715
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001716 float zNear = clamp01(mState.zNear);
1717 float zFar = clamp01(mState.zFar);
1718
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001719 if (ignoreViewport)
1720 {
1721 viewport.X = 0;
1722 viewport.Y = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001723 viewport.Width = mRenderTargetDesc.Width;
1724 viewport.Height = mRenderTargetDesc.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001725 viewport.MinZ = 0.0f;
1726 viewport.MaxZ = 1.0f;
1727 }
1728 else
1729 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001730 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, mRenderTargetDesc.Height);
1731 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1732 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1733 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width) - static_cast<LONG>(viewport.X));
1734 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 +00001735 viewport.MinZ = zNear;
1736 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001737 }
1738
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001739 if (viewport.Width <= 0 || viewport.Height <= 0)
1740 {
1741 return false; // Nothing to render
1742 }
1743
jbauman@chromium.org241e70d2011-11-03 23:07:05 +00001744 if (renderTargetChanged || !mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001745 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001746 mDevice->SetViewport(&viewport);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001747 mSetViewport = viewport;
1748 mViewportInitialized = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001749 mDxUniformsDirty = true;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001750 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001751
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001752 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001753 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001754 if (mState.scissorTest)
1755 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001756 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, mRenderTargetDesc.Height);
1757 rect.left = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1758 rect.top = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1759 rect.right = clamp(rect.right, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1760 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001761 mDevice->SetScissorRect(&rect);
1762 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001763 }
1764 else
1765 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001766 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001767 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001768
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001769 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001770 }
1771
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001772 if (mState.currentProgram && mDxUniformsDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001773 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001774 Program *programObject = getCurrentProgram();
1775
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001776 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001777 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001778 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001779
daniel@transgaming.com31754962010-11-28 02:02:52 +00001780 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001781 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1782 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1783 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001784 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001785
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001786 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001787 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001788 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001789
daniel@transgaming.com31754962010-11-28 02:02:52 +00001790 GLint depthRange = programObject->getDxDepthRangeLocation();
1791 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1792 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001793 mDxUniformsDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001794 }
1795
1796 return true;
1797}
1798
1799// 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 +00001800void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001801{
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001802 Program *programObject = getCurrentProgram();
1803
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001804 Framebuffer *framebufferObject = getDrawFramebuffer();
1805
1806 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1807
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001808 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001809 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001810 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001811
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001812 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001813 GLint alwaysFront = !isTriangleMode(drawMode);
1814 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1815
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001816 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001817 bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
1818 // Apparently some ATI cards have a bug where a draw with a zero color
1819 // write mask can cause later draws to have incorrect results. Instead,
1820 // set a nonzero color write mask but modify the blend state so that no
1821 // drawing is done.
1822 // http://code.google.com/p/angleproject/issues/detail?id=169
1823
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001824 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001825 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001826 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001827 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001828 mDevice->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001829 }
1830 else
1831 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001832 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001833 }
1834
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001835 mCullStateDirty = false;
1836 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001837
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001838 if (mDepthStateDirty)
1839 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00001840 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001841 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001842 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1843 mDevice->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001844 }
1845 else
1846 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001847 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001848 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001849
1850 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001851 }
1852
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001853 if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
1854 {
1855 mBlendStateDirty = true;
1856 mMaskStateDirty = true;
1857 }
1858
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001859 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001860 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001861 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001862 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001863 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001864
1865 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1866 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1867 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001868 mDevice->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001869 }
1870 else
1871 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001872 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001873 unorm<8>(mState.blendColor.alpha),
1874 unorm<8>(mState.blendColor.alpha),
1875 unorm<8>(mState.blendColor.alpha)));
1876 }
1877
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001878 mDevice->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1879 mDevice->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1880 mDevice->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001881
1882 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1883 mState.destBlendRGB != mState.destBlendAlpha ||
1884 mState.blendEquationRGB != mState.blendEquationAlpha)
1885 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001886 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001887
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001888 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1889 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1890 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001891 }
1892 else
1893 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001894 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001895 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001896 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001897 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001898 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001899 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001900 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001901
1902 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001903 }
1904
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001905 if (mStencilStateDirty || mFrontFaceDirty)
1906 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001907 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001908 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001909 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1910 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001911
1912 // FIXME: Unsupported by D3D9
1913 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1914 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1915 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1916 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1917 mState.stencilRef != mState.stencilBackRef ||
1918 mState.stencilMask != mState.stencilBackMask)
1919 {
1920 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1921 return error(GL_INVALID_OPERATION);
1922 }
1923
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001924 // get the maximum size of the stencil ref
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001925 gl::Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001926 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1927
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001928 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1929 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001930 es2dx::ConvertComparison(mState.stencilFunc));
1931
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001932 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1933 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001934
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001935 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001936 es2dx::ConvertStencilOp(mState.stencilFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001937 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001938 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001939 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001940 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1941
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001942 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1943 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001944 es2dx::ConvertComparison(mState.stencilBackFunc));
1945
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001946 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1947 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001948
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001949 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001950 es2dx::ConvertStencilOp(mState.stencilBackFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001951 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001952 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001953 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001954 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1955 }
1956 else
1957 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001958 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001959 }
1960
1961 mStencilStateDirty = false;
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001962 mFrontFaceDirty = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001963 }
1964
1965 if (mMaskStateDirty)
1966 {
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001967 int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1968 mState.colorMaskBlue, mState.colorMaskAlpha);
1969 if (colorMask == 0 && !zeroColorMaskAllowed)
1970 {
1971 // Enable green channel, but set blending so nothing will be drawn.
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001972 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
1973 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001974
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001975 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
1976 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
1977 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001978 }
1979 else
1980 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001981 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001982 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001983 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001984
1985 mMaskStateDirty = false;
1986 }
1987
1988 if (mPolygonOffsetStateDirty)
1989 {
1990 if (mState.polygonOffsetFill)
1991 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001992 gl::Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001993 if (depthbuffer)
1994 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001995 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001996 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001997 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001998 }
1999 }
2000 else
2001 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002002 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
2003 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002004 }
2005
2006 mPolygonOffsetStateDirty = false;
2007 }
2008
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002009 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002010 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002011 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002012 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002013 FIXME("Sample alpha to coverage is unimplemented.");
2014 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002015
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002016 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002017 if (mState.sampleCoverage)
2018 {
2019 unsigned int mask = 0;
2020 if (mState.sampleCoverageValue != 0)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002021 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002022 float threshold = 0.5f;
2023
2024 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002025 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002026 mask <<= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002027
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002028 if ((i + 1) * mState.sampleCoverageValue >= threshold)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002029 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002030 threshold += 1.0f;
2031 mask |= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002032 }
2033 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002034 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002035
2036 if (mState.sampleCoverageInvert)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002037 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002038 mask = ~mask;
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002039 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002040
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002041 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002042 }
2043 else
2044 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002045 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002046 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002047
2048 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002049 }
2050
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002051 if (mDitherStateDirty)
2052 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002053 mDevice->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002054
2055 mDitherStateDirty = false;
2056 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002057}
2058
daniel@transgaming.com83921382011-01-08 05:46:00 +00002059GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002060{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002061 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002062
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002063 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002064 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002065 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002066 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002067 }
2068
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002069 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, getCurrentProgram());
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002070}
2071
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002072// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002073GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002074{
daniel@transgaming.com83921382011-01-08 05:46:00 +00002075 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002076
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002077 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002078 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002079 if (indexInfo->serial != mAppliedIBSerial)
2080 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002081 mDevice->SetIndices(indexInfo->indexBuffer);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002082 mAppliedIBSerial = indexInfo->serial;
2083 }
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002084 }
2085
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002086 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002087}
2088
2089// Applies the shaders and shader constants to the Direct3D 9 device
2090void Context::applyShaders()
2091{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002092 Program *programObject = getCurrentProgram();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002093 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002094 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002095 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2096 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2097
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002098 mDevice->SetPixelShader(pixelShader);
2099 mDevice->SetVertexShader(vertexShader);
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002100 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002101 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002102 }
2103
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002104 programObject->applyUniforms();
2105}
2106
2107// Applies the textures and sampler states to the Direct3D 9 device
2108void Context::applyTextures()
2109{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002110 applyTextures(SAMPLER_PIXEL);
2111
2112 if (mSupportsVertexTexture)
2113 {
2114 applyTextures(SAMPLER_VERTEX);
2115 }
2116}
2117
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002118// For each Direct3D 9 sampler of either the pixel or vertex stage,
2119// looks up the corresponding OpenGL texture image unit and texture type,
2120// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002121void Context::applyTextures(SamplerType type)
2122{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002123 Program *programObject = getCurrentProgram();
2124
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002125 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 +00002126 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2127 int d3dSamplerOffset = (type == SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002128 int samplerRange = programObject->getUsedSamplerRange(type);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002129
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002130 for (int samplerIndex = 0; samplerIndex < samplerRange; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002131 {
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002132 int textureUnit = programObject->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00002133 int d3dSampler = samplerIndex + d3dSamplerOffset;
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002134
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002135 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002136 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002137 TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002138
2139 Texture *texture = getSamplerTexture(textureUnit, textureType);
2140
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00002141 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->hasDirtyParameters() || texture->hasDirtyImages())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002142 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002143 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2144
2145 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002146 {
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00002147 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->hasDirtyParameters())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002148 {
2149 GLenum wrapS = texture->getWrapS();
2150 GLenum wrapT = texture->getWrapT();
2151 GLenum minFilter = texture->getMinFilter();
2152 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002153
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002154 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2155 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002156
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002157 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002158 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2159 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002160 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2161 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002162 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002163
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00002164 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->hasDirtyImages())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002165 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002166 mDevice->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002167 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002168 }
2169 else
2170 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002171 mDevice->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002172 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002173
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002174 appliedTextureSerial[samplerIndex] = texture->getSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002175 texture->resetDirty();
2176 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002177 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002178 else
2179 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002180 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002181 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002182 mDevice->SetTexture(d3dSampler, NULL);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002183 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002184 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002185 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002186 }
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002187
2188 for (int samplerIndex = samplerRange; samplerIndex < samplerCount; samplerIndex++)
2189 {
2190 if (appliedTextureSerial[samplerIndex] != 0)
2191 {
daniel@transgaming.comc5a7b692011-10-26 02:45:44 +00002192 mDevice->SetTexture(samplerIndex + d3dSamplerOffset, NULL);
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002193 appliedTextureSerial[samplerIndex] = 0;
2194 }
2195 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002196}
2197
2198void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2199{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002200 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002201
2202 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2203 {
2204 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2205 }
2206
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002207 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2208 {
2209 return error(GL_INVALID_OPERATION);
2210 }
2211
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002212 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002213
2214 if (!renderTarget)
2215 {
2216 return; // Context must be lost, return silently
2217 }
2218
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002219 D3DSURFACE_DESC desc;
2220 renderTarget->GetDesc(&desc);
2221
2222 IDirect3DSurface9 *systemSurface;
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002223 HRESULT result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002224
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002225 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002226 {
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002227 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002228 return error(GL_OUT_OF_MEMORY);
2229 }
2230
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002231 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2232 {
2233 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002234 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002235 }
2236
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002237 result = mDevice->GetRenderTargetData(renderTarget, systemSurface);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002238
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002239 if (FAILED(result))
2240 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002241 systemSurface->Release();
2242
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002243 // It turns out that D3D will sometimes produce more error
2244 // codes than those documented.
2245 if (checkDeviceLost(result))
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002246 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002247 else
2248 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002249 UNREACHABLE();
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002250 return;
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002251 }
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002252
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002253 }
2254
2255 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002256 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2257 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2258 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2259 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2260 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002261
2262 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2263
2264 if (FAILED(result))
2265 {
2266 UNREACHABLE();
2267 systemSurface->Release();
2268
2269 return; // No sensible error to generate
2270 }
2271
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002272 unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002273 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002274 unsigned short *dest16 = (unsigned short*)pixels;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002275 int inputPitch = -lock.Pitch;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002276 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002277
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002278 for (int j = 0; j < rect.bottom - rect.top; j++)
2279 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002280 if (desc.Format == D3DFMT_A8R8G8B8 &&
2281 format == GL_BGRA_EXT &&
2282 type == GL_UNSIGNED_BYTE)
2283 {
2284 // Fast path for EXT_read_format_bgra, given
2285 // an RGBA source buffer. Note that buffers with no
2286 // alpha go through the slow path below.
2287 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002288 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002289 (rect.right - rect.left) * 4);
2290 continue;
2291 }
2292
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002293 for (int i = 0; i < rect.right - rect.left; i++)
2294 {
2295 float r;
2296 float g;
2297 float b;
2298 float a;
2299
2300 switch (desc.Format)
2301 {
2302 case D3DFMT_R5G6B5:
2303 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002304 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002305
2306 a = 1.0f;
2307 b = (rgb & 0x001F) * (1.0f / 0x001F);
2308 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2309 r = (rgb & 0xF800) * (1.0f / 0xF800);
2310 }
2311 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002312 case D3DFMT_A1R5G5B5:
2313 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002314 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002315
2316 a = (argb & 0x8000) ? 1.0f : 0.0f;
2317 b = (argb & 0x001F) * (1.0f / 0x001F);
2318 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2319 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2320 }
2321 break;
2322 case D3DFMT_A8R8G8B8:
2323 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002324 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002325
2326 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2327 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2328 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2329 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2330 }
2331 break;
2332 case D3DFMT_X8R8G8B8:
2333 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002334 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002335
2336 a = 1.0f;
2337 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2338 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2339 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2340 }
2341 break;
2342 case D3DFMT_A2R10G10B10:
2343 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002344 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002345
2346 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2347 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2348 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2349 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2350 }
2351 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002352 case D3DFMT_A32B32G32R32F:
2353 {
2354 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002355 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2356 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2357 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2358 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002359 }
2360 break;
2361 case D3DFMT_A16B16G16R16F:
2362 {
2363 // float formats in D3D are stored rgba, rather than the other way round
2364 float abgr[4];
2365
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002366 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002367
2368 a = abgr[3];
2369 b = abgr[2];
2370 g = abgr[1];
2371 r = abgr[0];
2372 }
2373 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002374 default:
2375 UNIMPLEMENTED(); // FIXME
2376 UNREACHABLE();
2377 }
2378
2379 switch (format)
2380 {
2381 case GL_RGBA:
2382 switch (type)
2383 {
2384 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002385 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2386 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2387 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2388 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002389 break;
2390 default: UNREACHABLE();
2391 }
2392 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002393 case GL_BGRA_EXT:
2394 switch (type)
2395 {
2396 case GL_UNSIGNED_BYTE:
2397 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2398 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2399 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2400 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2401 break;
2402 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2403 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2404 // this type is packed as follows:
2405 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2406 // --------------------------------------------------------------------------------
2407 // | 4th | 3rd | 2nd | 1st component |
2408 // --------------------------------------------------------------------------------
2409 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2410 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2411 ((unsigned short)(15 * a + 0.5f) << 12)|
2412 ((unsigned short)(15 * r + 0.5f) << 8) |
2413 ((unsigned short)(15 * g + 0.5f) << 4) |
2414 ((unsigned short)(15 * b + 0.5f) << 0);
2415 break;
2416 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2417 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2418 // this type is packed as follows:
2419 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2420 // --------------------------------------------------------------------------------
2421 // | 4th | 3rd | 2nd | 1st component |
2422 // --------------------------------------------------------------------------------
2423 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2424 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2425 ((unsigned short)( a + 0.5f) << 15) |
2426 ((unsigned short)(31 * r + 0.5f) << 10) |
2427 ((unsigned short)(31 * g + 0.5f) << 5) |
2428 ((unsigned short)(31 * b + 0.5f) << 0);
2429 break;
2430 default: UNREACHABLE();
2431 }
2432 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002433 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002434 switch (type)
2435 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002436 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002437 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2438 ((unsigned short)(31 * b + 0.5f) << 0) |
2439 ((unsigned short)(63 * g + 0.5f) << 5) |
2440 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002441 break;
2442 default: UNREACHABLE();
2443 }
2444 break;
2445 default: UNREACHABLE();
2446 }
2447 }
2448 }
2449
2450 systemSurface->UnlockRect();
2451
2452 systemSurface->Release();
2453}
2454
2455void Context::clear(GLbitfield mask)
2456{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002457 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002458
2459 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2460 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002461 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002462 }
2463
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002464 DWORD flags = 0;
2465
2466 if (mask & GL_COLOR_BUFFER_BIT)
2467 {
2468 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002469
2470 if (framebufferObject->getColorbufferType() != GL_NONE)
2471 {
2472 flags |= D3DCLEAR_TARGET;
2473 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002474 }
2475
2476 if (mask & GL_DEPTH_BUFFER_BIT)
2477 {
2478 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002479 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002480 {
2481 flags |= D3DCLEAR_ZBUFFER;
2482 }
2483 }
2484
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002485 GLuint stencilUnmasked = 0x0;
2486
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002487 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002488 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002489 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002490 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002491 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002492 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002493 if (!depthStencil)
2494 {
2495 ERR("Depth stencil pointer unexpectedly null.");
2496 return;
2497 }
2498
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002499 D3DSURFACE_DESC desc;
2500 depthStencil->GetDesc(&desc);
2501
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002502 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002503 stencilUnmasked = (0x1 << stencilSize) - 1;
2504
2505 if (stencilUnmasked != 0x0)
2506 {
2507 flags |= D3DCLEAR_STENCIL;
2508 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002509 }
2510 }
2511
2512 if (mask != 0)
2513 {
2514 return error(GL_INVALID_VALUE);
2515 }
2516
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002517 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2518 {
2519 return;
2520 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002521
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002522 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002523 unorm<8>(mState.colorClearValue.red),
2524 unorm<8>(mState.colorClearValue.green),
2525 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002526 float depth = clamp01(mState.depthClearValue);
2527 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002528
2529 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2530
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002531 if (!renderTarget)
2532 {
2533 return; // Context must be lost, return silently
2534 }
2535
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002536 D3DSURFACE_DESC desc;
2537 renderTarget->GetDesc(&desc);
2538
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002539 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002540
2541 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002542 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002543 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002544 !(mState.colorMaskRed && mState.colorMaskGreen &&
2545 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002546
2547 if (needMaskedColorClear || needMaskedStencilClear)
2548 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002549 // State which is altered in all paths from this point to the clear call is saved.
2550 // State which is altered in only some paths will be flagged dirty in the case that
2551 // that path is taken.
2552 HRESULT hr;
2553 if (mMaskedClearSavedState == NULL)
2554 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002555 hr = mDevice->BeginStateBlock();
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002556 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2557
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002558 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2559 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2560 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2561 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2562 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2563 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2564 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2565 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2566 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2567 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2568 mDevice->SetPixelShader(NULL);
2569 mDevice->SetVertexShader(NULL);
2570 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2571 mDevice->SetStreamSource(0, NULL, 0, 0);
2572 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2573 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2574 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2575 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2576 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2577 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2578 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002579
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002580 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002581 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2582 }
2583
2584 ASSERT(mMaskedClearSavedState != NULL);
2585
2586 if (mMaskedClearSavedState != NULL)
2587 {
2588 hr = mMaskedClearSavedState->Capture();
2589 ASSERT(SUCCEEDED(hr));
2590 }
2591
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002592 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2593 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2594 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2595 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2596 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2597 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2598 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2599 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002600
2601 if (flags & D3DCLEAR_TARGET)
2602 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002603 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002604 }
2605 else
2606 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002607 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002608 }
2609
2610 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2611 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002612 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2613 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2614 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2615 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
2616 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
2617 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
2618 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2619 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002620 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002621 }
2622 else
2623 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002624 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002625 }
2626
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002627 mDevice->SetPixelShader(NULL);
2628 mDevice->SetVertexShader(NULL);
2629 mDevice->SetFVF(D3DFVF_XYZRHW);
2630 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2631 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2632 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2633 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2634 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2635 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2636 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002637
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002638 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2639 quad[0][0] = -0.5f;
2640 quad[0][1] = desc.Height - 0.5f;
2641 quad[0][2] = 0.0f;
2642 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002643
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002644 quad[1][0] = desc.Width - 0.5f;
2645 quad[1][1] = desc.Height - 0.5f;
2646 quad[1][2] = 0.0f;
2647 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002648
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002649 quad[2][0] = -0.5f;
2650 quad[2][1] = -0.5f;
2651 quad[2][2] = 0.0f;
2652 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002653
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002654 quad[3][0] = desc.Width - 0.5f;
2655 quad[3][1] = -0.5f;
2656 quad[3][2] = 0.0f;
2657 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002658
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002659 mDisplay->startScene();
2660 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002661
2662 if (flags & D3DCLEAR_ZBUFFER)
2663 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002664 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
2665 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2666 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002667 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002668
2669 if (mMaskedClearSavedState != NULL)
2670 {
2671 mMaskedClearSavedState->Apply();
2672 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002673 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002674 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002675 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002676 mDevice->Clear(0, NULL, flags, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002677 }
2678}
2679
2680void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2681{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002682 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002683 {
2684 return error(GL_INVALID_OPERATION);
2685 }
2686
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002687 D3DPRIMITIVETYPE primitiveType;
2688 int primitiveCount;
2689
2690 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2691 return error(GL_INVALID_ENUM);
2692
2693 if (primitiveCount <= 0)
2694 {
2695 return;
2696 }
2697
2698 if (!applyRenderTarget(false))
2699 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002700 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002701 }
2702
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002703 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002704
daniel@transgaming.com83921382011-01-08 05:46:00 +00002705 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002706 if (err != GL_NO_ERROR)
2707 {
2708 return error(err);
2709 }
2710
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002711 applyShaders();
2712 applyTextures();
2713
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002714 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002715 {
2716 return error(GL_INVALID_OPERATION);
2717 }
2718
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002719 if (!cullSkipsDraw(mode))
2720 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002721 mDisplay->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002722
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002723 mDevice->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002724
2725 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2726 {
2727 drawClosingLine(first, first + count - 1);
2728 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002729 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002730}
2731
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002732void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002733{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002734 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002735 {
2736 return error(GL_INVALID_OPERATION);
2737 }
2738
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002739 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002740 {
2741 return error(GL_INVALID_OPERATION);
2742 }
2743
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002744 D3DPRIMITIVETYPE primitiveType;
2745 int primitiveCount;
2746
2747 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2748 return error(GL_INVALID_ENUM);
2749
2750 if (primitiveCount <= 0)
2751 {
2752 return;
2753 }
2754
2755 if (!applyRenderTarget(false))
2756 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002757 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002758 }
2759
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002760 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002761
2762 TranslatedIndexData indexInfo;
2763 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2764 if (err != GL_NO_ERROR)
2765 {
2766 return error(err);
2767 }
2768
daniel@transgaming.com83921382011-01-08 05:46:00 +00002769 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2770 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002771 if (err != GL_NO_ERROR)
2772 {
2773 return error(err);
2774 }
2775
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002776 applyShaders();
2777 applyTextures();
2778
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002779 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002780 {
2781 return error(GL_INVALID_OPERATION);
2782 }
2783
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002784 if (!cullSkipsDraw(mode))
2785 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002786 mDisplay->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002787
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002788 mDevice->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002789
2790 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2791 {
2792 drawClosingLine(count, type, indices);
2793 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002794 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002795}
2796
daniel@transgaming.com0d86aa72011-10-26 02:35:10 +00002797// Implements glFlush when block is false, glFinish when block is true
2798void Context::sync(bool block)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002799{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002800 IDirect3DQuery9 *eventQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002801 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002802
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002803 result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002804 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002805 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002806 ERR("CreateQuery failed hr=%x\n", result);
2807 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2808 {
2809 return error(GL_OUT_OF_MEMORY);
2810 }
2811 ASSERT(false);
2812 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002813 }
2814
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002815 result = eventQuery->Issue(D3DISSUE_END);
2816 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002817 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002818 ERR("eventQuery->Issue(END) failed hr=%x\n", result);
2819 ASSERT(false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002820 eventQuery->Release();
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002821 return;
2822 }
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002823
daniel@transgaming.com0d86aa72011-10-26 02:35:10 +00002824 do
2825 {
2826 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
2827
2828 if(block && result == S_FALSE)
2829 {
2830 // Keep polling, but allow other threads to do something useful first
2831 Sleep(0);
2832 }
2833 }
2834 while(block && result == S_FALSE);
2835
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002836 eventQuery->Release();
2837
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002838 if (checkDeviceLost(result))
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002839 {
2840 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002841 }
2842}
2843
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002844void Context::drawClosingLine(unsigned int first, unsigned int last)
2845{
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002846 IDirect3DIndexBuffer9 *indexBuffer = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002847 bool succeeded = false;
2848 UINT offset;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002849
2850 if (supports32bitIndices())
2851 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002852 const int spaceNeeded = 2 * sizeof(unsigned int);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002853
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002854 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002855 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002856 mClosingIB = new StreamingIndexBuffer(mDevice, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002857 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002858
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002859 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
2860
2861 unsigned int *data = static_cast<unsigned int*>(mClosingIB->map(spaceNeeded, &offset));
2862 if (data)
2863 {
2864 data[0] = last;
2865 data[1] = first;
2866 mClosingIB->unmap();
2867 offset /= 4;
2868 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002869 }
2870 }
2871 else
2872 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002873 const int spaceNeeded = 2 * sizeof(unsigned short);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002874
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002875 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002876 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002877 mClosingIB = new StreamingIndexBuffer(mDevice, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002878 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002879
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002880 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
2881
2882 unsigned short *data = static_cast<unsigned short*>(mClosingIB->map(spaceNeeded, &offset));
2883 if (data)
2884 {
2885 data[0] = last;
2886 data[1] = first;
2887 mClosingIB->unmap();
2888 offset /= 2;
2889 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002890 }
2891 }
2892
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002893 if (succeeded)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002894 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002895 mDevice->SetIndices(mClosingIB->getBuffer());
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002896 mAppliedIBSerial = mClosingIB->getSerial();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002897
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002898 mDevice->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, last, offset, 1);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002899 }
2900 else
2901 {
2902 ERR("Could not create an index buffer for closing a line loop.");
2903 error(GL_OUT_OF_MEMORY);
2904 }
2905}
2906
2907void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices)
2908{
2909 unsigned int first = 0;
2910 unsigned int last = 0;
2911
2912 if (mState.elementArrayBuffer.get())
2913 {
2914 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2915 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2916 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2917 }
2918
2919 switch (type)
2920 {
2921 case GL_UNSIGNED_BYTE:
2922 first = static_cast<const GLubyte*>(indices)[0];
2923 last = static_cast<const GLubyte*>(indices)[count - 1];
2924 break;
2925 case GL_UNSIGNED_SHORT:
2926 first = static_cast<const GLushort*>(indices)[0];
2927 last = static_cast<const GLushort*>(indices)[count - 1];
2928 break;
2929 case GL_UNSIGNED_INT:
2930 first = static_cast<const GLuint*>(indices)[0];
2931 last = static_cast<const GLuint*>(indices)[count - 1];
2932 break;
2933 default: UNREACHABLE();
2934 }
2935
2936 drawClosingLine(first, last);
2937}
2938
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002939void Context::recordInvalidEnum()
2940{
2941 mInvalidEnum = true;
2942}
2943
2944void Context::recordInvalidValue()
2945{
2946 mInvalidValue = true;
2947}
2948
2949void Context::recordInvalidOperation()
2950{
2951 mInvalidOperation = true;
2952}
2953
2954void Context::recordOutOfMemory()
2955{
2956 mOutOfMemory = true;
2957}
2958
2959void Context::recordInvalidFramebufferOperation()
2960{
2961 mInvalidFramebufferOperation = true;
2962}
2963
2964// Get one of the recorded errors and clear its flag, if any.
2965// [OpenGL ES 2.0.24] section 2.5 page 13.
2966GLenum Context::getError()
2967{
2968 if (mInvalidEnum)
2969 {
2970 mInvalidEnum = false;
2971
2972 return GL_INVALID_ENUM;
2973 }
2974
2975 if (mInvalidValue)
2976 {
2977 mInvalidValue = false;
2978
2979 return GL_INVALID_VALUE;
2980 }
2981
2982 if (mInvalidOperation)
2983 {
2984 mInvalidOperation = false;
2985
2986 return GL_INVALID_OPERATION;
2987 }
2988
2989 if (mOutOfMemory)
2990 {
2991 mOutOfMemory = false;
2992
2993 return GL_OUT_OF_MEMORY;
2994 }
2995
2996 if (mInvalidFramebufferOperation)
2997 {
2998 mInvalidFramebufferOperation = false;
2999
3000 return GL_INVALID_FRAMEBUFFER_OPERATION;
3001 }
3002
3003 return GL_NO_ERROR;
3004}
3005
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003006bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003007{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003008 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003009}
3010
daniel@transgaming.com396c6432010-11-26 16:26:12 +00003011int Context::getMaximumVaryingVectors() const
3012{
3013 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3014}
3015
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003016unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003017{
3018 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3019}
3020
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003021unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003022{
3023 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3024}
3025
daniel@transgaming.com458da142010-11-28 02:03:02 +00003026int Context::getMaximumFragmentUniformVectors() const
3027{
3028 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3029}
3030
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003031int Context::getMaxSupportedSamples() const
3032{
3033 return mMaxSupportedSamples;
3034}
3035
3036int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3037{
3038 if (requested == 0)
3039 {
3040 return requested;
3041 }
3042
3043 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3044 if (itr == mMultiSampleSupport.end())
3045 {
3046 return -1;
3047 }
3048
3049 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3050 {
3051 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3052 {
3053 return i;
3054 }
3055 }
3056
3057 return -1;
3058}
3059
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003060bool Context::supportsEventQueries() const
3061{
3062 return mSupportsEventQueries;
3063}
3064
gman@chromium.org50c526d2011-08-10 05:19:44 +00003065bool Context::supportsDXT1Textures() const
daniel@transgaming.com01868132010-08-24 19:21:17 +00003066{
gman@chromium.org50c526d2011-08-10 05:19:44 +00003067 return mSupportsDXT1Textures;
3068}
3069
3070bool Context::supportsDXT3Textures() const
3071{
3072 return mSupportsDXT3Textures;
3073}
3074
3075bool Context::supportsDXT5Textures() const
3076{
3077 return mSupportsDXT5Textures;
daniel@transgaming.com01868132010-08-24 19:21:17 +00003078}
3079
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003080bool Context::supportsFloat32Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003081{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003082 return mSupportsFloat32Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003083}
3084
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003085bool Context::supportsFloat32LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003086{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003087 return mSupportsFloat32LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003088}
3089
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003090bool Context::supportsFloat32RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003091{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003092 return mSupportsFloat32RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003093}
3094
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003095bool Context::supportsFloat16Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003096{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003097 return mSupportsFloat16Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003098}
3099
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003100bool Context::supportsFloat16LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003101{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003102 return mSupportsFloat16LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003103}
3104
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003105bool Context::supportsFloat16RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003106{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003107 return mSupportsFloat16RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003108}
3109
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003110int Context::getMaximumRenderbufferDimension() const
3111{
3112 return mMaxRenderbufferDimension;
3113}
3114
3115int Context::getMaximumTextureDimension() const
3116{
3117 return mMaxTextureDimension;
3118}
3119
3120int Context::getMaximumCubeTextureDimension() const
3121{
3122 return mMaxCubeTextureDimension;
3123}
3124
3125int Context::getMaximumTextureLevel() const
3126{
3127 return mMaxTextureLevel;
3128}
3129
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003130bool Context::supportsLuminanceTextures() const
3131{
3132 return mSupportsLuminanceTextures;
3133}
3134
3135bool Context::supportsLuminanceAlphaTextures() const
3136{
3137 return mSupportsLuminanceAlphaTextures;
3138}
3139
daniel@transgaming.com83921382011-01-08 05:46:00 +00003140bool Context::supports32bitIndices() const
3141{
3142 return mSupports32bitIndices;
3143}
3144
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003145bool Context::supportsNonPower2Texture() const
3146{
3147 return mSupportsNonPower2Texture;
3148}
3149
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003150void Context::detachBuffer(GLuint buffer)
3151{
3152 // [OpenGL ES 2.0.24] section 2.9 page 22:
3153 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3154 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3155
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003156 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003157 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003158 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003159 }
3160
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003161 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003162 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003163 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003164 }
3165
3166 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3167 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003168 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003169 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003170 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003171 }
3172 }
3173}
3174
3175void Context::detachTexture(GLuint texture)
3176{
3177 // [OpenGL ES 2.0.24] section 3.8 page 84:
3178 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3179 // rebound to texture object zero
3180
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003181 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003182 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003183 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003184 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003185 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003186 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003187 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003188 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003189 }
3190 }
3191
3192 // [OpenGL ES 2.0.24] section 4.4 page 112:
3193 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3194 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3195 // image was attached in the currently bound framebuffer.
3196
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003197 Framebuffer *readFramebuffer = getReadFramebuffer();
3198 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003199
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003200 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003201 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003202 readFramebuffer->detachTexture(texture);
3203 }
3204
3205 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3206 {
3207 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003208 }
3209}
3210
3211void Context::detachFramebuffer(GLuint framebuffer)
3212{
3213 // [OpenGL ES 2.0.24] section 4.4 page 107:
3214 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3215 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3216
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003217 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003218 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003219 bindReadFramebuffer(0);
3220 }
3221
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003222 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003223 {
3224 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003225 }
3226}
3227
3228void Context::detachRenderbuffer(GLuint renderbuffer)
3229{
3230 // [OpenGL ES 2.0.24] section 4.4 page 109:
3231 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3232 // had been executed with the target RENDERBUFFER and name of zero.
3233
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003234 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003235 {
3236 bindRenderbuffer(0);
3237 }
3238
3239 // [OpenGL ES 2.0.24] section 4.4 page 111:
3240 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3241 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3242 // point to which this image was attached in the currently bound framebuffer.
3243
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003244 Framebuffer *readFramebuffer = getReadFramebuffer();
3245 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003246
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003247 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003248 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003249 readFramebuffer->detachRenderbuffer(renderbuffer);
3250 }
3251
3252 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3253 {
3254 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003255 }
3256}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003257
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003258Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003259{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003260 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003261
3262 if (t == NULL)
3263 {
3264 static const GLubyte color[] = { 0, 0, 0, 255 };
3265
3266 switch (type)
3267 {
3268 default:
3269 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003270 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003271
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003272 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003273 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003274 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003275 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003276 t = incomplete2d;
3277 }
3278 break;
3279
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003280 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003281 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003282 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003283
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003284 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3285 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3286 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3287 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3288 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3289 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003290
3291 t = incompleteCube;
3292 }
3293 break;
3294 }
3295
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003296 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003297 }
3298
3299 return t;
3300}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003301
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003302bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003303{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003304 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003305}
3306
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003307bool Context::isTriangleMode(GLenum drawMode)
3308{
3309 switch (drawMode)
3310 {
3311 case GL_TRIANGLES:
3312 case GL_TRIANGLE_FAN:
3313 case GL_TRIANGLE_STRIP:
3314 return true;
3315 case GL_POINTS:
3316 case GL_LINES:
3317 case GL_LINE_LOOP:
3318 case GL_LINE_STRIP:
3319 return false;
3320 default: UNREACHABLE();
3321 }
3322
3323 return false;
3324}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003325
3326void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3327{
3328 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3329
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003330 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3331 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3332 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3333 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003334
daniel@transgaming.com83921382011-01-08 05:46:00 +00003335 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003336}
3337
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003338// keep list sorted in following order
3339// OES extensions
3340// EXT extensions
3341// Vendor extensions
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003342void Context::initExtensionString()
3343{
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003344 mExtensionString = "";
3345
3346 // OES extensions
3347 if (supports32bitIndices())
3348 {
3349 mExtensionString += "GL_OES_element_index_uint ";
3350 }
3351
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003352 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003353 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003354 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003355
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003356 if (supportsFloat16Textures())
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003357 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003358 mExtensionString += "GL_OES_texture_half_float ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003359 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003360 if (supportsFloat16LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003361 {
3362 mExtensionString += "GL_OES_texture_half_float_linear ";
3363 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003364 if (supportsFloat32Textures())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003365 {
3366 mExtensionString += "GL_OES_texture_float ";
3367 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003368 if (supportsFloat32LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003369 {
3370 mExtensionString += "GL_OES_texture_float_linear ";
3371 }
3372
3373 if (supportsNonPower2Texture())
3374 {
3375 mExtensionString += "GL_OES_texture_npot ";
3376 }
3377
3378 // Multi-vendor (EXT) extensions
3379 mExtensionString += "GL_EXT_read_format_bgra ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003380
gman@chromium.org50c526d2011-08-10 05:19:44 +00003381 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00003382 {
3383 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3384 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003385
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003386 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
gman@chromium.org50c526d2011-08-10 05:19:44 +00003387
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003388 // ANGLE-specific extensions
3389 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003390 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003391 {
3392 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3393 }
3394
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003395 if (supportsDXT3Textures())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003396 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003397 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3398 }
3399 if (supportsDXT5Textures())
3400 {
3401 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003402 }
zmo@google.coma574f782011-10-03 21:45:23 +00003403 mExtensionString += "GL_ANGLE_translated_shader_source ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003404
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003405 // Other vendor-specific extensions
3406 if (supportsEventQueries())
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003407 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003408 mExtensionString += "GL_NV_fence ";
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003409 }
3410
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003411 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3412 if (end != std::string::npos)
3413 {
3414 mExtensionString.resize(end+1);
3415 }
3416}
3417
3418const char *Context::getExtensionString() const
3419{
3420 return mExtensionString.c_str();
3421}
3422
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003423void Context::initRendererString()
3424{
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003425 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003426
3427 mRendererString = "ANGLE (";
3428 mRendererString += identifier->Description;
3429 mRendererString += ")";
3430}
3431
3432const char *Context::getRendererString() const
3433{
3434 return mRendererString.c_str();
3435}
3436
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003437void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3438 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3439 GLbitfield mask)
3440{
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003441 Framebuffer *readFramebuffer = getReadFramebuffer();
3442 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3443
3444 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3445 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3446 {
3447 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3448 }
3449
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003450 if (drawFramebuffer->getSamples() != 0)
3451 {
3452 return error(GL_INVALID_OPERATION);
3453 }
3454
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003455 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3456 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3457 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3458 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3459
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003460 RECT sourceRect;
3461 RECT destRect;
3462
3463 if (srcX0 < srcX1)
3464 {
3465 sourceRect.left = srcX0;
3466 sourceRect.right = srcX1;
3467 destRect.left = dstX0;
3468 destRect.right = dstX1;
3469 }
3470 else
3471 {
3472 sourceRect.left = srcX1;
3473 destRect.left = dstX1;
3474 sourceRect.right = srcX0;
3475 destRect.right = dstX0;
3476 }
3477
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003478 if (srcY0 < srcY1)
3479 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003480 sourceRect.top = readBufferHeight - srcY1;
3481 destRect.top = drawBufferHeight - dstY1;
3482 sourceRect.bottom = readBufferHeight - srcY0;
3483 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003484 }
3485 else
3486 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003487 sourceRect.top = readBufferHeight - srcY0;
3488 destRect.top = drawBufferHeight - dstY0;
3489 sourceRect.bottom = readBufferHeight - srcY1;
3490 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003491 }
3492
3493 RECT sourceScissoredRect = sourceRect;
3494 RECT destScissoredRect = destRect;
3495
3496 if (mState.scissorTest)
3497 {
3498 // Only write to parts of the destination framebuffer which pass the scissor test
3499 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3500 // rect will be checked against scissorY, rather than the bottom.
3501 if (destRect.left < mState.scissorX)
3502 {
3503 int xDiff = mState.scissorX - destRect.left;
3504 destScissoredRect.left = mState.scissorX;
3505 sourceScissoredRect.left += xDiff;
3506 }
3507
3508 if (destRect.right > mState.scissorX + mState.scissorWidth)
3509 {
3510 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3511 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3512 sourceScissoredRect.right -= xDiff;
3513 }
3514
3515 if (destRect.top < mState.scissorY)
3516 {
3517 int yDiff = mState.scissorY - destRect.top;
3518 destScissoredRect.top = mState.scissorY;
3519 sourceScissoredRect.top += yDiff;
3520 }
3521
3522 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3523 {
3524 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3525 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3526 sourceScissoredRect.bottom -= yDiff;
3527 }
3528 }
3529
3530 bool blitRenderTarget = false;
3531 bool blitDepthStencil = false;
3532
3533 RECT sourceTrimmedRect = sourceScissoredRect;
3534 RECT destTrimmedRect = destScissoredRect;
3535
3536 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3537 // the actual draw and read surfaces.
3538 if (sourceTrimmedRect.left < 0)
3539 {
3540 int xDiff = 0 - sourceTrimmedRect.left;
3541 sourceTrimmedRect.left = 0;
3542 destTrimmedRect.left += xDiff;
3543 }
3544
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003545 if (sourceTrimmedRect.right > readBufferWidth)
3546 {
3547 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3548 sourceTrimmedRect.right = readBufferWidth;
3549 destTrimmedRect.right -= xDiff;
3550 }
3551
3552 if (sourceTrimmedRect.top < 0)
3553 {
3554 int yDiff = 0 - sourceTrimmedRect.top;
3555 sourceTrimmedRect.top = 0;
3556 destTrimmedRect.top += yDiff;
3557 }
3558
3559 if (sourceTrimmedRect.bottom > readBufferHeight)
3560 {
3561 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3562 sourceTrimmedRect.bottom = readBufferHeight;
3563 destTrimmedRect.bottom -= yDiff;
3564 }
3565
3566 if (destTrimmedRect.left < 0)
3567 {
3568 int xDiff = 0 - destTrimmedRect.left;
3569 destTrimmedRect.left = 0;
3570 sourceTrimmedRect.left += xDiff;
3571 }
3572
3573 if (destTrimmedRect.right > drawBufferWidth)
3574 {
3575 int xDiff = destTrimmedRect.right - drawBufferWidth;
3576 destTrimmedRect.right = drawBufferWidth;
3577 sourceTrimmedRect.right -= xDiff;
3578 }
3579
3580 if (destTrimmedRect.top < 0)
3581 {
3582 int yDiff = 0 - destTrimmedRect.top;
3583 destTrimmedRect.top = 0;
3584 sourceTrimmedRect.top += yDiff;
3585 }
3586
3587 if (destTrimmedRect.bottom > drawBufferHeight)
3588 {
3589 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3590 destTrimmedRect.bottom = drawBufferHeight;
3591 sourceTrimmedRect.bottom -= yDiff;
3592 }
3593
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003594 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003595 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3596 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3597 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3598 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003599 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3600 {
3601 partialBufferCopy = true;
3602 }
3603
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003604 if (mask & GL_COLOR_BUFFER_BIT)
3605 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003606 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3607 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3608 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3609 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3610 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003611 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3612 {
3613 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3614 return error(GL_INVALID_OPERATION);
3615 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003616
3617 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3618 {
3619 return error(GL_INVALID_OPERATION);
3620 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003621
3622 blitRenderTarget = true;
3623
3624 }
3625
3626 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3627 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00003628 Renderbuffer *readDSBuffer = NULL;
3629 Renderbuffer *drawDSBuffer = NULL;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003630
3631 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3632 // both a depth and stencil buffer, it will be the same buffer.
3633
3634 if (mask & GL_DEPTH_BUFFER_BIT)
3635 {
3636 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3637 {
3638 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3639 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3640 {
3641 return error(GL_INVALID_OPERATION);
3642 }
3643
3644 blitDepthStencil = true;
3645 readDSBuffer = readFramebuffer->getDepthbuffer();
3646 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3647 }
3648 }
3649
3650 if (mask & GL_STENCIL_BUFFER_BIT)
3651 {
3652 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3653 {
3654 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3655 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3656 {
3657 return error(GL_INVALID_OPERATION);
3658 }
3659
3660 blitDepthStencil = true;
3661 readDSBuffer = readFramebuffer->getStencilbuffer();
3662 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3663 }
3664 }
3665
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003666 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003667 {
3668 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3669 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3670 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003671
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003672 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3673 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003674 {
3675 return error(GL_INVALID_OPERATION);
3676 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003677 }
3678
3679 if (blitRenderTarget || blitDepthStencil)
3680 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003681 mDisplay->endScene();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003682
3683 if (blitRenderTarget)
3684 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003685 HRESULT result = mDevice->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003686 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3687
3688 if (FAILED(result))
3689 {
3690 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3691 return;
3692 }
3693 }
3694
3695 if (blitDepthStencil)
3696 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003697 HRESULT result = mDevice->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003698
3699 if (FAILED(result))
3700 {
3701 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3702 return;
3703 }
3704 }
3705 }
3706}
3707
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003708VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
3709{
3710 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3711 {
3712 mVertexDeclCache[i].vertexDeclaration = NULL;
3713 mVertexDeclCache[i].lruCount = 0;
3714 }
3715}
3716
3717VertexDeclarationCache::~VertexDeclarationCache()
3718{
3719 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3720 {
3721 if (mVertexDeclCache[i].vertexDeclaration)
3722 {
3723 mVertexDeclCache[i].vertexDeclaration->Release();
3724 }
3725 }
3726}
3727
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003728GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], Program *program)
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003729{
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003730 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
3731 D3DVERTEXELEMENT9 *element = &elements[0];
3732
3733 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3734 {
3735 if (attributes[i].active)
3736 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003737 if (mAppliedVBs[i].serial != attributes[i].serial ||
3738 mAppliedVBs[i].stride != attributes[i].stride ||
3739 mAppliedVBs[i].offset != attributes[i].offset)
3740 {
3741 device->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
3742 mAppliedVBs[i].serial = attributes[i].serial;
3743 mAppliedVBs[i].stride = attributes[i].stride;
3744 mAppliedVBs[i].offset = attributes[i].offset;
3745 }
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003746
3747 element->Stream = i;
3748 element->Offset = 0;
3749 element->Type = attributes[i].type;
3750 element->Method = D3DDECLMETHOD_DEFAULT;
3751 element->Usage = D3DDECLUSAGE_TEXCOORD;
3752 element->UsageIndex = program->getSemanticIndex(i);
3753 element++;
3754 }
3755 }
3756
3757 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
3758 *(element++) = end;
3759
3760 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3761 {
3762 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
3763 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
3764 {
3765 entry->lruCount = ++mMaxLru;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003766 if(entry->vertexDeclaration != mLastSetVDecl)
3767 {
3768 device->SetVertexDeclaration(entry->vertexDeclaration);
3769 mLastSetVDecl = entry->vertexDeclaration;
3770 }
3771
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003772 return GL_NO_ERROR;
3773 }
3774 }
3775
3776 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
3777
3778 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3779 {
3780 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
3781 {
3782 lastCache = &mVertexDeclCache[i];
3783 }
3784 }
3785
3786 if (lastCache->vertexDeclaration != NULL)
3787 {
3788 lastCache->vertexDeclaration->Release();
3789 lastCache->vertexDeclaration = NULL;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003790 // mLastSetVDecl is set to the replacement, so we don't have to worry
3791 // about it.
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003792 }
3793
3794 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
3795 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
3796 device->SetVertexDeclaration(lastCache->vertexDeclaration);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003797 mLastSetVDecl = lastCache->vertexDeclaration;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003798 lastCache->lruCount = ++mMaxLru;
3799
3800 return GL_NO_ERROR;
3801}
3802
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003803void VertexDeclarationCache::markStateDirty()
3804{
3805 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3806 {
3807 mAppliedVBs[i].serial = 0;
3808 }
3809
3810 mLastSetVDecl = NULL;
3811}
3812
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003813}
3814
3815extern "C"
3816{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003817gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003818{
daniel@transgaming.com0d25b002010-07-28 19:21:07 +00003819 return new gl::Context(config, shareContext);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003820}
3821
3822void glDestroyContext(gl::Context *context)
3823{
3824 delete context;
3825
3826 if (context == gl::getContext())
3827 {
3828 gl::makeCurrent(NULL, NULL, NULL);
3829 }
3830}
3831
3832void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3833{
3834 gl::makeCurrent(context, display, surface);
3835}
3836
3837gl::Context *glGetCurrentContext()
3838{
3839 return gl::getContext();
3840}
3841}