blob: cef8badfa3c9dd45344835eab2797a125af3538f [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00002// Copyright (c) 2002-2011 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
7// Context.cpp: Implements the gl::Context class, managing all GL state and performing
8// rendering operations. It is the GLES2 specific implementation of EGLContext.
9
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000010#include "libGLESv2/Context.h"
daniel@transgaming.com16973022010-03-11 19:22:19 +000011
12#include <algorithm>
13
alokp@chromium.orgea0e1af2010-03-22 19:33:14 +000014#include "libEGL/Display.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000015
16#include "libGLESv2/main.h"
17#include "libGLESv2/mathutil.h"
18#include "libGLESv2/utilities.h"
19#include "libGLESv2/Blit.h"
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +000020#include "libGLESv2/ResourceManager.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000021#include "libGLESv2/Buffer.h"
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +000022#include "libGLESv2/Fence.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000023#include "libGLESv2/FrameBuffer.h"
24#include "libGLESv2/Program.h"
25#include "libGLESv2/RenderBuffer.h"
26#include "libGLESv2/Shader.h"
27#include "libGLESv2/Texture.h"
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +000028#include "libGLESv2/VertexDataManager.h"
29#include "libGLESv2/IndexDataManager.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030
daniel@transgaming.com86487c22010-03-11 19:41:43 +000031#undef near
32#undef far
33
jbauman@chromium.org399c35f2011-04-28 23:19:51 +000034namespace
35{
36 enum { CLOSING_INDEX_BUFFER_SIZE = 4096 };
37}
38
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000039namespace gl
40{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +000041Context::Context(const egl::Config *config, const gl::Context *shareContext, bool notifyResets, bool robustAccess) : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000042{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +000043 ASSERT(robustAccess == false); // Unimplemented
44
daniel@transgaming.comc941e252011-10-26 02:32:31 +000045 mDisplay = NULL;
46 mDevice = NULL;
47
benvanik@google.com1a233342011-04-28 19:44:39 +000048 mFenceHandleAllocator.setBaseHandle(0);
49
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000050 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000051
daniel@transgaming.com428d1582010-05-04 03:35:25 +000052 mState.depthClearValue = 1.0f;
53 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000054
daniel@transgaming.com428d1582010-05-04 03:35:25 +000055 mState.cullFace = false;
56 mState.cullMode = GL_BACK;
57 mState.frontFace = GL_CCW;
58 mState.depthTest = false;
59 mState.depthFunc = GL_LESS;
60 mState.blend = false;
61 mState.sourceBlendRGB = GL_ONE;
62 mState.sourceBlendAlpha = GL_ONE;
63 mState.destBlendRGB = GL_ZERO;
64 mState.destBlendAlpha = GL_ZERO;
65 mState.blendEquationRGB = GL_FUNC_ADD;
66 mState.blendEquationAlpha = GL_FUNC_ADD;
67 mState.blendColor.red = 0;
68 mState.blendColor.green = 0;
69 mState.blendColor.blue = 0;
70 mState.blendColor.alpha = 0;
71 mState.stencilTest = false;
72 mState.stencilFunc = GL_ALWAYS;
73 mState.stencilRef = 0;
74 mState.stencilMask = -1;
75 mState.stencilWritemask = -1;
76 mState.stencilBackFunc = GL_ALWAYS;
77 mState.stencilBackRef = 0;
78 mState.stencilBackMask = - 1;
79 mState.stencilBackWritemask = -1;
80 mState.stencilFail = GL_KEEP;
81 mState.stencilPassDepthFail = GL_KEEP;
82 mState.stencilPassDepthPass = GL_KEEP;
83 mState.stencilBackFail = GL_KEEP;
84 mState.stencilBackPassDepthFail = GL_KEEP;
85 mState.stencilBackPassDepthPass = GL_KEEP;
86 mState.polygonOffsetFill = false;
87 mState.polygonOffsetFactor = 0.0f;
88 mState.polygonOffsetUnits = 0.0f;
89 mState.sampleAlphaToCoverage = false;
90 mState.sampleCoverage = false;
91 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000092 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000093 mState.scissorTest = false;
94 mState.dither = true;
95 mState.generateMipmapHint = GL_DONT_CARE;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +000096 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000097
daniel@transgaming.com428d1582010-05-04 03:35:25 +000098 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000099
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000100 mState.viewportX = 0;
101 mState.viewportY = 0;
102 mState.viewportWidth = config->mDisplayMode.Width;
103 mState.viewportHeight = config->mDisplayMode.Height;
104 mState.zNear = 0.0f;
105 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000107 mState.scissorX = 0;
108 mState.scissorY = 0;
109 mState.scissorWidth = config->mDisplayMode.Width;
110 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000111
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000112 mState.colorMaskRed = true;
113 mState.colorMaskGreen = true;
114 mState.colorMaskBlue = true;
115 mState.colorMaskAlpha = true;
116 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000117
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000118 if (shareContext != NULL)
119 {
120 mResourceManager = shareContext->mResourceManager;
121 mResourceManager->addRef();
122 }
123 else
124 {
125 mResourceManager = new ResourceManager();
126 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000127
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000128 // [OpenGL ES 2.0.24] section 3.7 page 83:
129 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
130 // and cube map texture state vectors respectively associated with them.
131 // In order that access to these initial textures not be lost, they are treated as texture
132 // objects all of whose names are 0.
133
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000134 mTexture2DZero.set(new Texture2D(0));
135 mTextureCubeMapZero.set(new TextureCubeMap(0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000137 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000138 bindArrayBuffer(0);
139 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000140 bindTextureCubeMap(0);
141 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000142 bindReadFramebuffer(0);
143 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000144 bindRenderbuffer(0);
145
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000146 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000147
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000148 mState.packAlignment = 4;
149 mState.unpackAlignment = 4;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000150
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000151 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000152 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000153 mBlit = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000154 mClosingIB = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000155
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000156 mInvalidEnum = false;
157 mInvalidValue = false;
158 mInvalidOperation = false;
159 mOutOfMemory = false;
160 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000161
162 mHasBeenCurrent = false;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000163 mContextLost = false;
daniel@transgaming.com17f548c2011-11-09 17:47:02 +0000164 mResetStatus = GL_NO_ERROR;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +0000165 mResetStrategy = (notifyResets ? GL_LOSE_CONTEXT_ON_RESET_EXT : GL_NO_RESET_NOTIFICATION_EXT);
166 mRobustAccess = robustAccess;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000167
gman@chromium.org50c526d2011-08-10 05:19:44 +0000168 mSupportsDXT1Textures = false;
169 mSupportsDXT3Textures = false;
170 mSupportsDXT5Textures = false;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000171 mSupportsEventQueries = false;
gman@chromium.org50c526d2011-08-10 05:19:44 +0000172 mNumCompressedTextureFormats = 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000173 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000174 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000175 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000176}
177
178Context::~Context()
179{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000180 if (mState.currentProgram != 0)
181 {
182 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
183 if (programObject)
184 {
185 programObject->release();
186 }
187 mState.currentProgram = 0;
188 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000189
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000190 while (!mFramebufferMap.empty())
191 {
192 deleteFramebuffer(mFramebufferMap.begin()->first);
193 }
194
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000195 while (!mFenceMap.empty())
196 {
197 deleteFence(mFenceMap.begin()->first);
198 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000199
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000200 while (!mMultiSampleSupport.empty())
201 {
202 delete [] mMultiSampleSupport.begin()->second;
203 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
204 }
205
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000206 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000207 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +0000208 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000209 {
210 mState.samplerTexture[type][sampler].set(NULL);
211 }
212 }
213
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000214 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000215 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000216 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000217 }
218
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000219 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
220 {
221 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
222 }
223
224 mState.arrayBuffer.set(NULL);
225 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000226 mState.renderbuffer.set(NULL);
227
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000228 mTexture2DZero.set(NULL);
229 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000230
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000231 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000232 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000233 delete mBlit;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +0000234 delete mClosingIB;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000235
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000236 if (mMaskedClearSavedState)
237 {
238 mMaskedClearSavedState->Release();
239 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000240
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000241 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000242}
243
244void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
245{
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000246 mDisplay = display;
247 mDevice = mDisplay->getDevice();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000248
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000249 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000250 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000251 mDeviceCaps = mDisplay->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000252
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000253 mVertexDataManager = new VertexDataManager(this, mDevice);
254 mIndexDataManager = new IndexDataManager(this, mDevice);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000255 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000256
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000257 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion == D3DPS_VERSION(3, 0);
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000258 mSupportsVertexTexture = mDisplay->getVertexTextureSupport();
259 mSupportsNonPower2Texture = mDisplay->getNonPower2TextureSupport();
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000260
261 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
262 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
263 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
264 mMaxRenderbufferDimension = mMaxTextureDimension;
265 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
266 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
267 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
268
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000269 const D3DFORMAT renderBufferFormats[] =
270 {
271 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000272 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000273 D3DFMT_R5G6B5,
274 D3DFMT_D24S8
275 };
276
277 int max = 0;
278 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
279 {
280 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000281 mDisplay->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000282 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
283
284 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
285 {
286 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
287 {
288 max = j;
289 }
290 }
291 }
292
293 mMaxSupportedSamples = max;
294
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000295 mSupportsEventQueries = mDisplay->getEventQuerySupport();
296 mSupportsDXT1Textures = mDisplay->getDXT1TextureSupport();
297 mSupportsDXT3Textures = mDisplay->getDXT3TextureSupport();
298 mSupportsDXT5Textures = mDisplay->getDXT5TextureSupport();
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +0000299 mSupportsFloat32Textures = mDisplay->getFloat32TextureSupport(&mSupportsFloat32LinearFilter, &mSupportsFloat32RenderableTextures);
300 mSupportsFloat16Textures = mDisplay->getFloat16TextureSupport(&mSupportsFloat16LinearFilter, &mSupportsFloat16RenderableTextures);
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000301 mSupportsLuminanceTextures = mDisplay->getLuminanceTextureSupport();
302 mSupportsLuminanceAlphaTextures = mDisplay->getLuminanceAlphaTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000303
daniel@transgaming.com83921382011-01-08 05:46:00 +0000304 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
305
gman@chromium.org50c526d2011-08-10 05:19:44 +0000306 mNumCompressedTextureFormats = 0;
307 if (supportsDXT1Textures())
308 {
309 mNumCompressedTextureFormats += 2;
310 }
311 if (supportsDXT3Textures())
312 {
313 mNumCompressedTextureFormats += 1;
314 }
315 if (supportsDXT5Textures())
316 {
317 mNumCompressedTextureFormats += 1;
318 }
319
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000320 initExtensionString();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +0000321 initRendererString();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000322
323 mState.viewportX = 0;
324 mState.viewportY = 0;
325 mState.viewportWidth = surface->getWidth();
326 mState.viewportHeight = surface->getHeight();
327
328 mState.scissorX = 0;
329 mState.scissorY = 0;
330 mState.scissorWidth = surface->getWidth();
331 mState.scissorHeight = surface->getHeight();
332
333 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000334 }
335
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000336 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
337 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000338 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000339
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000340 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000341 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000342 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000343
344 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000345
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000346 if (defaultRenderTarget)
347 {
348 defaultRenderTarget->Release();
349 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000350
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000351 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000352 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000353 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000355
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000356 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000357}
358
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000359// This function will set all of the state-related dirty flags, so that all state is set during next pre-draw.
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000360void Context::markAllStateDirty()
361{
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000362 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
363 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +0000364 mAppliedTextureSerialPS[t] = 0;
365 }
366
367 for (int t = 0; t < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; t++)
368 {
369 mAppliedTextureSerialVS[t] = 0;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000370 }
371
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000372 mAppliedProgramSerial = 0;
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000373 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000374 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000375 mAppliedStencilbufferSerial = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000376 mAppliedIBSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000377 mDepthStencilInitialized = false;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000378 mViewportInitialized = false;
379 mRenderTargetDescInitialized = false;
380
381 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000382
383 mClearStateDirty = true;
384 mCullStateDirty = true;
385 mDepthStateDirty = true;
386 mMaskStateDirty = true;
387 mBlendStateDirty = true;
388 mStencilStateDirty = true;
389 mPolygonOffsetStateDirty = true;
390 mScissorStateDirty = true;
391 mSampleStateDirty = true;
392 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000393 mFrontFaceDirty = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +0000394 mDxUniformsDirty = true;
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000395 mCachedCurrentProgram = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000396}
397
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000398void Context::markContextLost()
399{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +0000400 if (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT)
401 mResetStatus = GL_UNKNOWN_CONTEXT_RESET_EXT;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000402 mContextLost = true;
403}
404
405bool Context::isContextLost()
406{
407 return mContextLost;
408}
409
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000410void Context::setClearColor(float red, float green, float blue, float alpha)
411{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000412 mState.colorClearValue.red = red;
413 mState.colorClearValue.green = green;
414 mState.colorClearValue.blue = blue;
415 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000416}
417
418void Context::setClearDepth(float depth)
419{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000420 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000421}
422
423void Context::setClearStencil(int stencil)
424{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000425 mState.stencilClearValue = stencil;
426}
427
428void Context::setCullFace(bool enabled)
429{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000430 if (mState.cullFace != enabled)
431 {
432 mState.cullFace = enabled;
433 mCullStateDirty = true;
434 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000435}
436
437bool Context::isCullFaceEnabled() const
438{
439 return mState.cullFace;
440}
441
442void Context::setCullMode(GLenum mode)
443{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000444 if (mState.cullMode != mode)
445 {
446 mState.cullMode = mode;
447 mCullStateDirty = true;
448 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000449}
450
451void Context::setFrontFace(GLenum front)
452{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000453 if (mState.frontFace != front)
454 {
455 mState.frontFace = front;
456 mFrontFaceDirty = true;
457 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000458}
459
460void Context::setDepthTest(bool enabled)
461{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000462 if (mState.depthTest != enabled)
463 {
464 mState.depthTest = enabled;
465 mDepthStateDirty = true;
466 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000467}
468
469bool Context::isDepthTestEnabled() const
470{
471 return mState.depthTest;
472}
473
474void Context::setDepthFunc(GLenum depthFunc)
475{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000476 if (mState.depthFunc != depthFunc)
477 {
478 mState.depthFunc = depthFunc;
479 mDepthStateDirty = true;
480 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000481}
482
483void Context::setDepthRange(float zNear, float zFar)
484{
485 mState.zNear = zNear;
486 mState.zFar = zFar;
487}
488
489void Context::setBlend(bool enabled)
490{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000491 if (mState.blend != enabled)
492 {
493 mState.blend = enabled;
494 mBlendStateDirty = true;
495 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000496}
497
498bool Context::isBlendEnabled() const
499{
500 return mState.blend;
501}
502
503void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
504{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000505 if (mState.sourceBlendRGB != sourceRGB ||
506 mState.sourceBlendAlpha != sourceAlpha ||
507 mState.destBlendRGB != destRGB ||
508 mState.destBlendAlpha != destAlpha)
509 {
510 mState.sourceBlendRGB = sourceRGB;
511 mState.destBlendRGB = destRGB;
512 mState.sourceBlendAlpha = sourceAlpha;
513 mState.destBlendAlpha = destAlpha;
514 mBlendStateDirty = true;
515 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000516}
517
518void Context::setBlendColor(float red, float green, float blue, float alpha)
519{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000520 if (mState.blendColor.red != red ||
521 mState.blendColor.green != green ||
522 mState.blendColor.blue != blue ||
523 mState.blendColor.alpha != alpha)
524 {
525 mState.blendColor.red = red;
526 mState.blendColor.green = green;
527 mState.blendColor.blue = blue;
528 mState.blendColor.alpha = alpha;
529 mBlendStateDirty = true;
530 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000531}
532
533void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
534{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000535 if (mState.blendEquationRGB != rgbEquation ||
536 mState.blendEquationAlpha != alphaEquation)
537 {
538 mState.blendEquationRGB = rgbEquation;
539 mState.blendEquationAlpha = alphaEquation;
540 mBlendStateDirty = true;
541 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000542}
543
544void Context::setStencilTest(bool enabled)
545{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000546 if (mState.stencilTest != enabled)
547 {
548 mState.stencilTest = enabled;
549 mStencilStateDirty = true;
550 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000551}
552
553bool Context::isStencilTestEnabled() const
554{
555 return mState.stencilTest;
556}
557
558void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
559{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000560 if (mState.stencilFunc != stencilFunc ||
561 mState.stencilRef != stencilRef ||
562 mState.stencilMask != stencilMask)
563 {
564 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000565 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000566 mState.stencilMask = stencilMask;
567 mStencilStateDirty = true;
568 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000569}
570
571void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
572{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000573 if (mState.stencilBackFunc != stencilBackFunc ||
574 mState.stencilBackRef != stencilBackRef ||
575 mState.stencilBackMask != stencilBackMask)
576 {
577 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000578 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000579 mState.stencilBackMask = stencilBackMask;
580 mStencilStateDirty = true;
581 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000582}
583
584void Context::setStencilWritemask(GLuint stencilWritemask)
585{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000586 if (mState.stencilWritemask != stencilWritemask)
587 {
588 mState.stencilWritemask = stencilWritemask;
589 mStencilStateDirty = true;
590 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000591}
592
593void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
594{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000595 if (mState.stencilBackWritemask != stencilBackWritemask)
596 {
597 mState.stencilBackWritemask = stencilBackWritemask;
598 mStencilStateDirty = true;
599 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000600}
601
602void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
603{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000604 if (mState.stencilFail != stencilFail ||
605 mState.stencilPassDepthFail != stencilPassDepthFail ||
606 mState.stencilPassDepthPass != stencilPassDepthPass)
607 {
608 mState.stencilFail = stencilFail;
609 mState.stencilPassDepthFail = stencilPassDepthFail;
610 mState.stencilPassDepthPass = stencilPassDepthPass;
611 mStencilStateDirty = true;
612 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000613}
614
615void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
616{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000617 if (mState.stencilBackFail != stencilBackFail ||
618 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
619 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
620 {
621 mState.stencilBackFail = stencilBackFail;
622 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
623 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
624 mStencilStateDirty = true;
625 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000626}
627
628void Context::setPolygonOffsetFill(bool enabled)
629{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000630 if (mState.polygonOffsetFill != enabled)
631 {
632 mState.polygonOffsetFill = enabled;
633 mPolygonOffsetStateDirty = true;
634 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000635}
636
637bool Context::isPolygonOffsetFillEnabled() const
638{
639 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000640
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000641}
642
643void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
644{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000645 if (mState.polygonOffsetFactor != factor ||
646 mState.polygonOffsetUnits != units)
647 {
648 mState.polygonOffsetFactor = factor;
649 mState.polygonOffsetUnits = units;
650 mPolygonOffsetStateDirty = true;
651 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000652}
653
654void Context::setSampleAlphaToCoverage(bool enabled)
655{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000656 if (mState.sampleAlphaToCoverage != enabled)
657 {
658 mState.sampleAlphaToCoverage = enabled;
659 mSampleStateDirty = true;
660 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000661}
662
663bool Context::isSampleAlphaToCoverageEnabled() const
664{
665 return mState.sampleAlphaToCoverage;
666}
667
668void Context::setSampleCoverage(bool enabled)
669{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000670 if (mState.sampleCoverage != enabled)
671 {
672 mState.sampleCoverage = enabled;
673 mSampleStateDirty = true;
674 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000675}
676
677bool Context::isSampleCoverageEnabled() const
678{
679 return mState.sampleCoverage;
680}
681
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000682void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000683{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000684 if (mState.sampleCoverageValue != value ||
685 mState.sampleCoverageInvert != invert)
686 {
687 mState.sampleCoverageValue = value;
688 mState.sampleCoverageInvert = invert;
689 mSampleStateDirty = true;
690 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000691}
692
693void Context::setScissorTest(bool enabled)
694{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000695 if (mState.scissorTest != enabled)
696 {
697 mState.scissorTest = enabled;
698 mScissorStateDirty = true;
699 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000700}
701
702bool Context::isScissorTestEnabled() const
703{
704 return mState.scissorTest;
705}
706
707void Context::setDither(bool enabled)
708{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000709 if (mState.dither != enabled)
710 {
711 mState.dither = enabled;
712 mDitherStateDirty = true;
713 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000714}
715
716bool Context::isDitherEnabled() const
717{
718 return mState.dither;
719}
720
721void Context::setLineWidth(GLfloat width)
722{
723 mState.lineWidth = width;
724}
725
726void Context::setGenerateMipmapHint(GLenum hint)
727{
728 mState.generateMipmapHint = hint;
729}
730
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000731void Context::setFragmentShaderDerivativeHint(GLenum hint)
732{
733 mState.fragmentShaderDerivativeHint = hint;
734 // TODO: Propagate the hint to shader translator so we can write
735 // ddx, ddx_coarse, or ddx_fine depending on the hint.
736 // Ignore for now. It is valid for implementations to ignore hint.
737}
738
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000739void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
740{
741 mState.viewportX = x;
742 mState.viewportY = y;
743 mState.viewportWidth = width;
744 mState.viewportHeight = height;
745}
746
747void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
748{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000749 if (mState.scissorX != x || mState.scissorY != y ||
750 mState.scissorWidth != width || mState.scissorHeight != height)
751 {
752 mState.scissorX = x;
753 mState.scissorY = y;
754 mState.scissorWidth = width;
755 mState.scissorHeight = height;
756 mScissorStateDirty = true;
757 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000758}
759
760void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
761{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000762 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
763 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
764 {
765 mState.colorMaskRed = red;
766 mState.colorMaskGreen = green;
767 mState.colorMaskBlue = blue;
768 mState.colorMaskAlpha = alpha;
769 mMaskStateDirty = true;
770 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000771}
772
773void Context::setDepthMask(bool mask)
774{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000775 if (mState.depthMask != mask)
776 {
777 mState.depthMask = mask;
778 mMaskStateDirty = true;
779 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000780}
781
daniel@transgaming.comdfd57022011-05-11 15:37:25 +0000782void Context::setActiveSampler(unsigned int active)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000783{
784 mState.activeSampler = active;
785}
786
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000787GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000788{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000789 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000790}
791
792GLuint Context::getDrawFramebufferHandle() const
793{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000794 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000795}
796
797GLuint Context::getRenderbufferHandle() const
798{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000799 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000800}
801
802GLuint Context::getArrayBufferHandle() const
803{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000804 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000805}
806
daniel@transgaming.com83921382011-01-08 05:46:00 +0000807void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000808{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000809 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000810}
811
daniel@transgaming.com83921382011-01-08 05:46:00 +0000812const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000813{
814 return mState.vertexAttribute[attribNum];
815}
816
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000817void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000818 GLsizei stride, const void *pointer)
819{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000820 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000821 mState.vertexAttribute[attribNum].mSize = size;
822 mState.vertexAttribute[attribNum].mType = type;
823 mState.vertexAttribute[attribNum].mNormalized = normalized;
824 mState.vertexAttribute[attribNum].mStride = stride;
825 mState.vertexAttribute[attribNum].mPointer = pointer;
826}
827
828const void *Context::getVertexAttribPointer(unsigned int attribNum) const
829{
830 return mState.vertexAttribute[attribNum].mPointer;
831}
832
daniel@transgaming.com83921382011-01-08 05:46:00 +0000833const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000834{
835 return mState.vertexAttribute;
836}
837
838void Context::setPackAlignment(GLint alignment)
839{
840 mState.packAlignment = alignment;
841}
842
843GLint Context::getPackAlignment() const
844{
845 return mState.packAlignment;
846}
847
848void Context::setUnpackAlignment(GLint alignment)
849{
850 mState.unpackAlignment = alignment;
851}
852
853GLint Context::getUnpackAlignment() const
854{
855 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856}
857
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858GLuint Context::createBuffer()
859{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000860 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861}
862
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000863GLuint Context::createProgram()
864{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000865 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000866}
867
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000868GLuint Context::createShader(GLenum type)
869{
870 return mResourceManager->createShader(type);
871}
872
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873GLuint Context::createTexture()
874{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000875 return mResourceManager->createTexture();
876}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000878GLuint Context::createRenderbuffer()
879{
880 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000881}
882
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000883// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000884GLuint Context::createFramebuffer()
885{
benvanik@google.com1a233342011-04-28 19:44:39 +0000886 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000887
888 mFramebufferMap[handle] = NULL;
889
890 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000891}
892
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000893GLuint Context::createFence()
894{
benvanik@google.com1a233342011-04-28 19:44:39 +0000895 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000896
897 mFenceMap[handle] = new Fence;
898
899 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000900}
901
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000902void Context::deleteBuffer(GLuint buffer)
903{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000904 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000905 {
906 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000907 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000908
909 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910}
911
912void Context::deleteShader(GLuint shader)
913{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000914 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000915}
916
917void Context::deleteProgram(GLuint program)
918{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000919 mResourceManager->deleteProgram(program);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000920 mCachedCurrentProgram = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000921}
922
923void Context::deleteTexture(GLuint texture)
924{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000925 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000926 {
927 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000928 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000929
930 mResourceManager->deleteTexture(texture);
931}
932
933void Context::deleteRenderbuffer(GLuint renderbuffer)
934{
935 if (mResourceManager->getRenderbuffer(renderbuffer))
936 {
937 detachRenderbuffer(renderbuffer);
938 }
939
940 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000941}
942
943void Context::deleteFramebuffer(GLuint framebuffer)
944{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000945 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
946
947 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000948 {
949 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +0000950
benvanik@google.com1a233342011-04-28 19:44:39 +0000951 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000952 delete framebufferObject->second;
953 mFramebufferMap.erase(framebufferObject);
954 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000955}
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000956
957void Context::deleteFence(GLuint fence)
958{
959 FenceMap::iterator fenceObject = mFenceMap.find(fence);
960
961 if (fenceObject != mFenceMap.end())
962 {
benvanik@google.com1a233342011-04-28 19:44:39 +0000963 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000964 delete fenceObject->second;
965 mFenceMap.erase(fenceObject);
966 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000967}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000968
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000969Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000971 return mResourceManager->getBuffer(handle);
972}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000974Shader *Context::getShader(GLuint handle)
975{
976 return mResourceManager->getShader(handle);
977}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000978
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000979Program *Context::getProgram(GLuint handle)
980{
981 return mResourceManager->getProgram(handle);
982}
983
984Texture *Context::getTexture(GLuint handle)
985{
986 return mResourceManager->getTexture(handle);
987}
988
989Renderbuffer *Context::getRenderbuffer(GLuint handle)
990{
991 return mResourceManager->getRenderbuffer(handle);
992}
993
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000994Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000995{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000996 return getFramebuffer(mState.readFramebuffer);
997}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000998
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000999Framebuffer *Context::getDrawFramebuffer()
1000{
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001001 return mBoundDrawFramebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001002}
1003
1004void Context::bindArrayBuffer(unsigned int buffer)
1005{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001006 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001007
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001008 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001009}
1010
1011void Context::bindElementArrayBuffer(unsigned int buffer)
1012{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001013 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001014
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001015 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001016}
1017
1018void Context::bindTexture2D(GLuint texture)
1019{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001020 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001021
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001022 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001023}
1024
1025void Context::bindTextureCubeMap(GLuint texture)
1026{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001027 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001029 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030}
1031
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001032void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001033{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001034 if (!getFramebuffer(framebuffer))
1035 {
1036 mFramebufferMap[framebuffer] = new Framebuffer();
1037 }
1038
1039 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001040}
1041
1042void Context::bindDrawFramebuffer(GLuint framebuffer)
1043{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001044 if (!getFramebuffer(framebuffer))
1045 {
1046 mFramebufferMap[framebuffer] = new Framebuffer();
1047 }
1048
1049 mState.drawFramebuffer = framebuffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001050
1051 mBoundDrawFramebuffer = getFramebuffer(framebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001052}
1053
1054void Context::bindRenderbuffer(GLuint renderbuffer)
1055{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001056 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1057
1058 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001059}
1060
1061void Context::useProgram(GLuint program)
1062{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001063 GLuint priorProgram = mState.currentProgram;
1064 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001065
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001066 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001067 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001068 Program *newProgram = mResourceManager->getProgram(program);
1069 Program *oldProgram = mResourceManager->getProgram(priorProgram);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001070 mCachedCurrentProgram = NULL;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001071 mDxUniformsDirty = true;
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001072
1073 if (newProgram)
1074 {
1075 newProgram->addRef();
1076 }
1077
1078 if (oldProgram)
1079 {
1080 oldProgram->release();
1081 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001082 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001083}
1084
1085void Context::setFramebufferZero(Framebuffer *buffer)
1086{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001087 delete mFramebufferMap[0];
1088 mFramebufferMap[0] = buffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001089 if (mState.drawFramebuffer == 0)
1090 {
1091 mBoundDrawFramebuffer = buffer;
1092 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001093}
1094
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001095void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001096{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001097 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1098 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001099}
1100
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001101Framebuffer *Context::getFramebuffer(unsigned int handle)
1102{
1103 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1104
1105 if (framebuffer == mFramebufferMap.end())
1106 {
1107 return NULL;
1108 }
1109 else
1110 {
1111 return framebuffer->second;
1112 }
1113}
1114
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001115Fence *Context::getFence(unsigned int handle)
1116{
1117 FenceMap::iterator fence = mFenceMap.find(handle);
1118
1119 if (fence == mFenceMap.end())
1120 {
1121 return NULL;
1122 }
1123 else
1124 {
1125 return fence->second;
1126 }
1127}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001128
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001129Buffer *Context::getArrayBuffer()
1130{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001131 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001132}
1133
1134Buffer *Context::getElementArrayBuffer()
1135{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001136 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001137}
1138
1139Program *Context::getCurrentProgram()
1140{
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001141 if (!mCachedCurrentProgram)
1142 {
1143 mCachedCurrentProgram = mResourceManager->getProgram(mState.currentProgram);
1144 }
1145 return mCachedCurrentProgram;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001146}
1147
1148Texture2D *Context::getTexture2D()
1149{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001150 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001151}
1152
1153TextureCubeMap *Context::getTextureCubeMap()
1154{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001155 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001156}
1157
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001158Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001159{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001160 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001161
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001162 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001163 {
1164 switch (type)
1165 {
1166 default: UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001167 case TEXTURE_2D: return mTexture2DZero.get();
1168 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001169 }
1170 }
1171
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001172 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001173}
1174
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001175bool Context::getBooleanv(GLenum pname, GLboolean *params)
1176{
1177 switch (pname)
1178 {
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001179 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1180 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1181 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001182 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001183 params[0] = mState.colorMaskRed;
1184 params[1] = mState.colorMaskGreen;
1185 params[2] = mState.colorMaskBlue;
1186 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001187 break;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001188 case GL_CULL_FACE: *params = mState.cullFace; break;
1189 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1190 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1191 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1192 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1193 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1194 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1195 case GL_BLEND: *params = mState.blend; break;
1196 case GL_DITHER: *params = mState.dither; break;
1197 case GL_CONTEXT_ROBUST_ACCESS_EXT: *params = mRobustAccess ? GL_TRUE : GL_FALSE; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001198 default:
1199 return false;
1200 }
1201
1202 return true;
1203}
1204
1205bool Context::getFloatv(GLenum pname, GLfloat *params)
1206{
1207 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1208 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1209 // GetIntegerv as its native query function. As it would require conversion in any
1210 // case, this should make no difference to the calling application.
1211 switch (pname)
1212 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001213 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1214 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1215 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1216 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1217 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001218 case GL_ALIASED_LINE_WIDTH_RANGE:
1219 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1220 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1221 break;
1222 case GL_ALIASED_POINT_SIZE_RANGE:
1223 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001224 params[1] = supportsShaderModel3() ? gl::ALIASED_POINT_SIZE_RANGE_MAX_SM3 : gl::ALIASED_POINT_SIZE_RANGE_MAX_SM2;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001225 break;
1226 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001227 params[0] = mState.zNear;
1228 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001229 break;
1230 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001231 params[0] = mState.colorClearValue.red;
1232 params[1] = mState.colorClearValue.green;
1233 params[2] = mState.colorClearValue.blue;
1234 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001235 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001236 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001237 params[0] = mState.blendColor.red;
1238 params[1] = mState.blendColor.green;
1239 params[2] = mState.blendColor.blue;
1240 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001241 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001242 default:
1243 return false;
1244 }
1245
1246 return true;
1247}
1248
1249bool Context::getIntegerv(GLenum pname, GLint *params)
1250{
1251 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1252 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1253 // GetIntegerv as its native query function. As it would require conversion in any
1254 // case, this should make no difference to the calling application. You may find it in
1255 // Context::getFloatv.
1256 switch (pname)
1257 {
1258 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1259 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001260 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00001261 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1262 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001263 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001264 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001265 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001266 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001267 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001268 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1269 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001270 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1271 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1272 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001273 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001274 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1275 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
1276 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1277 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001278 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001279 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1280 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1281 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1282 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1283 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1284 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1285 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1286 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1287 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1288 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1289 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1290 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1291 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1292 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1293 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1294 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1295 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1296 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1297 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1298 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1299 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1300 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1301 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1302 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001303 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1304 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001305 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
gman@chromium.org50c526d2011-08-10 05:19:44 +00001306 params[0] = mNumCompressedTextureFormats;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001307 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001308 case GL_MAX_SAMPLES_ANGLE:
1309 {
1310 GLsizei maxSamples = getMaxSupportedSamples();
1311 if (maxSamples != 0)
1312 {
1313 *params = maxSamples;
1314 }
1315 else
1316 {
1317 return false;
1318 }
1319
1320 break;
1321 }
1322 case GL_SAMPLE_BUFFERS:
1323 case GL_SAMPLES:
1324 {
1325 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1326 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1327 {
1328 switch (pname)
1329 {
1330 case GL_SAMPLE_BUFFERS:
1331 if (framebuffer->getSamples() != 0)
1332 {
1333 *params = 1;
1334 }
1335 else
1336 {
1337 *params = 0;
1338 }
1339 break;
1340 case GL_SAMPLES:
1341 *params = framebuffer->getSamples();
1342 break;
1343 }
1344 }
1345 else
1346 {
1347 *params = 0;
1348 }
1349 }
1350 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001351 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1352 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1353 case GL_MAX_VIEWPORT_DIMS:
1354 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001355 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001356 params[0] = maxDimension;
1357 params[1] = maxDimension;
1358 }
1359 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001360 case GL_COMPRESSED_TEXTURE_FORMATS:
1361 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001362 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00001363 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001364 *params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1365 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1366 }
1367 if (supportsDXT3Textures())
1368 {
1369 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
1370 }
1371 if (supportsDXT5Textures())
1372 {
1373 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001374 }
1375 }
1376 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001377 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001378 params[0] = mState.viewportX;
1379 params[1] = mState.viewportY;
1380 params[2] = mState.viewportWidth;
1381 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001382 break;
1383 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001384 params[0] = mState.scissorX;
1385 params[1] = mState.scissorY;
1386 params[2] = mState.scissorWidth;
1387 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001388 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001389 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1390 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001391 case GL_RED_BITS:
1392 case GL_GREEN_BITS:
1393 case GL_BLUE_BITS:
1394 case GL_ALPHA_BITS:
1395 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001396 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001397 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001398
1399 if (colorbuffer)
1400 {
1401 switch (pname)
1402 {
1403 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1404 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1405 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1406 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1407 }
1408 }
1409 else
1410 {
1411 *params = 0;
1412 }
1413 }
1414 break;
1415 case GL_DEPTH_BITS:
1416 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001417 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001418 gl::Renderbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001419
1420 if (depthbuffer)
1421 {
1422 *params = depthbuffer->getDepthSize();
1423 }
1424 else
1425 {
1426 *params = 0;
1427 }
1428 }
1429 break;
1430 case GL_STENCIL_BITS:
1431 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001432 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001433 gl::Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001434
1435 if (stencilbuffer)
1436 {
1437 *params = stencilbuffer->getStencilSize();
1438 }
1439 else
1440 {
1441 *params = 0;
1442 }
1443 }
1444 break;
1445 case GL_TEXTURE_BINDING_2D:
1446 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001447 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001448 {
1449 error(GL_INVALID_OPERATION);
1450 return false;
1451 }
1452
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001453 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001454 }
1455 break;
1456 case GL_TEXTURE_BINDING_CUBE_MAP:
1457 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001458 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001459 {
1460 error(GL_INVALID_OPERATION);
1461 return false;
1462 }
1463
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001464 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001465 }
1466 break;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001467 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
1468 *params = mResetStrategy;
1469 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001470 default:
1471 return false;
1472 }
1473
1474 return true;
1475}
1476
1477bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1478{
1479 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1480 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1481 // to the fact that it is stored internally as a float, and so would require conversion
1482 // if returned from Context::getIntegerv. Since this conversion is already implemented
1483 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1484 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1485 // application.
1486 switch (pname)
1487 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001488 case GL_COMPRESSED_TEXTURE_FORMATS:
1489 {
1490 *type = GL_INT;
1491 *numParams = mNumCompressedTextureFormats;
1492 }
1493 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001494 case GL_SHADER_BINARY_FORMATS:
1495 {
1496 *type = GL_INT;
1497 *numParams = 0;
1498 }
1499 break;
1500 case GL_MAX_VERTEX_ATTRIBS:
1501 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1502 case GL_MAX_VARYING_VECTORS:
1503 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1504 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1505 case GL_MAX_TEXTURE_IMAGE_UNITS:
1506 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1507 case GL_MAX_RENDERBUFFER_SIZE:
1508 case GL_NUM_SHADER_BINARY_FORMATS:
1509 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1510 case GL_ARRAY_BUFFER_BINDING:
1511 case GL_FRAMEBUFFER_BINDING:
1512 case GL_RENDERBUFFER_BINDING:
1513 case GL_CURRENT_PROGRAM:
1514 case GL_PACK_ALIGNMENT:
1515 case GL_UNPACK_ALIGNMENT:
1516 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001517 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001518 case GL_RED_BITS:
1519 case GL_GREEN_BITS:
1520 case GL_BLUE_BITS:
1521 case GL_ALPHA_BITS:
1522 case GL_DEPTH_BITS:
1523 case GL_STENCIL_BITS:
1524 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1525 case GL_CULL_FACE_MODE:
1526 case GL_FRONT_FACE:
1527 case GL_ACTIVE_TEXTURE:
1528 case GL_STENCIL_FUNC:
1529 case GL_STENCIL_VALUE_MASK:
1530 case GL_STENCIL_REF:
1531 case GL_STENCIL_FAIL:
1532 case GL_STENCIL_PASS_DEPTH_FAIL:
1533 case GL_STENCIL_PASS_DEPTH_PASS:
1534 case GL_STENCIL_BACK_FUNC:
1535 case GL_STENCIL_BACK_VALUE_MASK:
1536 case GL_STENCIL_BACK_REF:
1537 case GL_STENCIL_BACK_FAIL:
1538 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1539 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1540 case GL_DEPTH_FUNC:
1541 case GL_BLEND_SRC_RGB:
1542 case GL_BLEND_SRC_ALPHA:
1543 case GL_BLEND_DST_RGB:
1544 case GL_BLEND_DST_ALPHA:
1545 case GL_BLEND_EQUATION_RGB:
1546 case GL_BLEND_EQUATION_ALPHA:
1547 case GL_STENCIL_WRITEMASK:
1548 case GL_STENCIL_BACK_WRITEMASK:
1549 case GL_STENCIL_CLEAR_VALUE:
1550 case GL_SUBPIXEL_BITS:
1551 case GL_MAX_TEXTURE_SIZE:
1552 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1553 case GL_SAMPLE_BUFFERS:
1554 case GL_SAMPLES:
1555 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1556 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1557 case GL_TEXTURE_BINDING_2D:
1558 case GL_TEXTURE_BINDING_CUBE_MAP:
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001559 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001560 {
1561 *type = GL_INT;
1562 *numParams = 1;
1563 }
1564 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001565 case GL_MAX_SAMPLES_ANGLE:
1566 {
1567 if (getMaxSupportedSamples() != 0)
1568 {
1569 *type = GL_INT;
1570 *numParams = 1;
1571 }
1572 else
1573 {
1574 return false;
1575 }
1576 }
1577 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001578 case GL_MAX_VIEWPORT_DIMS:
1579 {
1580 *type = GL_INT;
1581 *numParams = 2;
1582 }
1583 break;
1584 case GL_VIEWPORT:
1585 case GL_SCISSOR_BOX:
1586 {
1587 *type = GL_INT;
1588 *numParams = 4;
1589 }
1590 break;
1591 case GL_SHADER_COMPILER:
1592 case GL_SAMPLE_COVERAGE_INVERT:
1593 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001594 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1595 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1596 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1597 case GL_SAMPLE_COVERAGE:
1598 case GL_SCISSOR_TEST:
1599 case GL_STENCIL_TEST:
1600 case GL_DEPTH_TEST:
1601 case GL_BLEND:
1602 case GL_DITHER:
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001603 case GL_CONTEXT_ROBUST_ACCESS_EXT:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001604 {
1605 *type = GL_BOOL;
1606 *numParams = 1;
1607 }
1608 break;
1609 case GL_COLOR_WRITEMASK:
1610 {
1611 *type = GL_BOOL;
1612 *numParams = 4;
1613 }
1614 break;
1615 case GL_POLYGON_OFFSET_FACTOR:
1616 case GL_POLYGON_OFFSET_UNITS:
1617 case GL_SAMPLE_COVERAGE_VALUE:
1618 case GL_DEPTH_CLEAR_VALUE:
1619 case GL_LINE_WIDTH:
1620 {
1621 *type = GL_FLOAT;
1622 *numParams = 1;
1623 }
1624 break;
1625 case GL_ALIASED_LINE_WIDTH_RANGE:
1626 case GL_ALIASED_POINT_SIZE_RANGE:
1627 case GL_DEPTH_RANGE:
1628 {
1629 *type = GL_FLOAT;
1630 *numParams = 2;
1631 }
1632 break;
1633 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001634 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001635 {
1636 *type = GL_FLOAT;
1637 *numParams = 4;
1638 }
1639 break;
1640 default:
1641 return false;
1642 }
1643
1644 return true;
1645}
1646
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001647// Applies the render target surface, depth stencil surface, viewport rectangle and
1648// scissor rectangle to the Direct3D 9 device
1649bool Context::applyRenderTarget(bool ignoreViewport)
1650{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001651 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001652
1653 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1654 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001655 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001656 }
1657
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001658 IDirect3DSurface9 *renderTarget = NULL;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001659 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001660
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001661 bool renderTargetChanged = false;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001662 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1663 if (renderTargetSerial != mAppliedRenderTargetSerial)
1664 {
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001665 renderTarget = framebufferObject->getRenderTarget();
1666
1667 if (!renderTarget)
1668 {
1669 return false; // Context must be lost
1670 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001671 mDevice->SetRenderTarget(0, renderTarget);
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001672 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001673 mScissorStateDirty = true; // Scissor area must be clamped to render target's size-- this is different for different render targets.
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001674 renderTargetChanged = true;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001675 }
1676
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001677 unsigned int depthbufferSerial = 0;
1678 unsigned int stencilbufferSerial = 0;
1679 if (framebufferObject->getDepthbufferType() != GL_NONE)
1680 {
1681 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001682 if (!depthStencil)
1683 {
1684 ERR("Depth stencil pointer unexpectedly null.");
1685 return false;
1686 }
1687
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001688 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1689 }
1690 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1691 {
1692 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001693 if (!depthStencil)
1694 {
1695 ERR("Depth stencil pointer unexpectedly null.");
1696 return false;
1697 }
1698
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001699 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1700 }
1701
1702 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001703 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001704 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001705 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001706 mDevice->SetDepthStencilSurface(depthStencil);
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001707 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001708 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001709 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001710 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001711
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001712 if (!mRenderTargetDescInitialized || renderTargetChanged)
1713 {
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001714 if (!renderTarget)
1715 {
1716 renderTarget = framebufferObject->getRenderTarget();
1717
1718 if (!renderTarget)
1719 {
1720 return false; // Context must be lost
1721 }
1722 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001723 renderTarget->GetDesc(&mRenderTargetDesc);
1724 mRenderTargetDescInitialized = true;
1725 }
1726
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001727 D3DVIEWPORT9 viewport;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001728
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001729 float zNear = clamp01(mState.zNear);
1730 float zFar = clamp01(mState.zFar);
1731
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001732 if (ignoreViewport)
1733 {
1734 viewport.X = 0;
1735 viewport.Y = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001736 viewport.Width = mRenderTargetDesc.Width;
1737 viewport.Height = mRenderTargetDesc.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001738 viewport.MinZ = 0.0f;
1739 viewport.MaxZ = 1.0f;
1740 }
1741 else
1742 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001743 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, mRenderTargetDesc.Height);
1744 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1745 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1746 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width) - static_cast<LONG>(viewport.X));
1747 viewport.Height = clamp(rect.bottom - rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height) - static_cast<LONG>(viewport.Y));
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001748 viewport.MinZ = zNear;
1749 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001750 }
1751
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001752 if (viewport.Width <= 0 || viewport.Height <= 0)
1753 {
1754 return false; // Nothing to render
1755 }
1756
jbauman@chromium.org241e70d2011-11-03 23:07:05 +00001757 if (renderTargetChanged || !mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001758 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001759 mDevice->SetViewport(&viewport);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001760 mSetViewport = viewport;
1761 mViewportInitialized = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001762 mDxUniformsDirty = true;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001763 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001764
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001765 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001766 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001767 if (mState.scissorTest)
1768 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001769 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, mRenderTargetDesc.Height);
1770 rect.left = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1771 rect.top = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1772 rect.right = clamp(rect.right, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1773 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001774 mDevice->SetScissorRect(&rect);
1775 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001776 }
1777 else
1778 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001779 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001780 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001781
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001782 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001783 }
1784
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001785 if (mState.currentProgram && mDxUniformsDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001786 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001787 Program *programObject = getCurrentProgram();
1788
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001789 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001790 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001791 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001792
daniel@transgaming.com31754962010-11-28 02:02:52 +00001793 GLint viewport = programObject->getDxViewportLocation();
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001794 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, mState.viewportHeight / 2.0f,
1795 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1796 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001797 programObject->setUniform4fv(viewport, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001798
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001799 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001800 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001801 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001802
daniel@transgaming.com31754962010-11-28 02:02:52 +00001803 GLint depthRange = programObject->getDxDepthRangeLocation();
1804 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1805 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001806 mDxUniformsDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001807 }
1808
1809 return true;
1810}
1811
1812// Applies the fixed-function state (culling, depth test, alpha blending, stenciling, etc) to the Direct3D 9 device
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001813void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001814{
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001815 Program *programObject = getCurrentProgram();
1816
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001817 Framebuffer *framebufferObject = getDrawFramebuffer();
1818
1819 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1820
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001821 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001822 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001823 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001824
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001825 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00001826 GLint alwaysFront = !isTriangleMode(drawMode);
1827 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
1828
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001829 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001830 bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
1831 // Apparently some ATI cards have a bug where a draw with a zero color
1832 // write mask can cause later draws to have incorrect results. Instead,
1833 // set a nonzero color write mask but modify the blend state so that no
1834 // drawing is done.
1835 // http://code.google.com/p/angleproject/issues/detail?id=169
1836
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001837 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001838 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001839 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001840 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001841 mDevice->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001842 }
1843 else
1844 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001845 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001846 }
1847
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001848 mCullStateDirty = false;
1849 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001850
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001851 if (mDepthStateDirty)
1852 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00001853 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001854 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001855 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
1856 mDevice->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001857 }
1858 else
1859 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001860 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001861 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001862
1863 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001864 }
1865
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001866 if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
1867 {
1868 mBlendStateDirty = true;
1869 mMaskStateDirty = true;
1870 }
1871
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001872 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001873 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001874 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001875 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001876 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001877
1878 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
1879 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
1880 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001881 mDevice->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001882 }
1883 else
1884 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001885 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001886 unorm<8>(mState.blendColor.alpha),
1887 unorm<8>(mState.blendColor.alpha),
1888 unorm<8>(mState.blendColor.alpha)));
1889 }
1890
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001891 mDevice->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
1892 mDevice->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
1893 mDevice->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001894
1895 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
1896 mState.destBlendRGB != mState.destBlendAlpha ||
1897 mState.blendEquationRGB != mState.blendEquationAlpha)
1898 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001899 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001900
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001901 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
1902 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
1903 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001904 }
1905 else
1906 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001907 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001908 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00001909 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001910 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001911 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001912 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00001913 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001914
1915 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001916 }
1917
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001918 if (mStencilStateDirty || mFrontFaceDirty)
1919 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001920 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001921 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001922 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
1923 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001924
1925 // FIXME: Unsupported by D3D9
1926 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
1927 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
1928 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
1929 if (mState.stencilWritemask != mState.stencilBackWritemask ||
1930 mState.stencilRef != mState.stencilBackRef ||
1931 mState.stencilMask != mState.stencilBackMask)
1932 {
1933 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
1934 return error(GL_INVALID_OPERATION);
1935 }
1936
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001937 // get the maximum size of the stencil ref
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001938 gl::Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00001939 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
1940
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001941 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
1942 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001943 es2dx::ConvertComparison(mState.stencilFunc));
1944
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001945 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
1946 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001947
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001948 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001949 es2dx::ConvertStencilOp(mState.stencilFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001950 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001951 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001952 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001953 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
1954
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001955 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
1956 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001957 es2dx::ConvertComparison(mState.stencilBackFunc));
1958
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001959 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
1960 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001961
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001962 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001963 es2dx::ConvertStencilOp(mState.stencilBackFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001964 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001965 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001966 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001967 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
1968 }
1969 else
1970 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001971 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001972 }
1973
1974 mStencilStateDirty = false;
daniel@transgaming.com3203c102011-06-08 12:41:32 +00001975 mFrontFaceDirty = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001976 }
1977
1978 if (mMaskStateDirty)
1979 {
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001980 int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
1981 mState.colorMaskBlue, mState.colorMaskAlpha);
1982 if (colorMask == 0 && !zeroColorMaskAllowed)
1983 {
1984 // Enable green channel, but set blending so nothing will be drawn.
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001985 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
1986 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001987
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001988 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
1989 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
1990 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001991 }
1992 else
1993 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001994 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00001995 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001996 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001997
1998 mMaskStateDirty = false;
1999 }
2000
2001 if (mPolygonOffsetStateDirty)
2002 {
2003 if (mState.polygonOffsetFill)
2004 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00002005 gl::Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002006 if (depthbuffer)
2007 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002008 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002009 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002010 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002011 }
2012 }
2013 else
2014 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002015 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
2016 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002017 }
2018
2019 mPolygonOffsetStateDirty = false;
2020 }
2021
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002022 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002023 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002024 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002025 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002026 FIXME("Sample alpha to coverage is unimplemented.");
2027 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002028
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002029 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002030 if (mState.sampleCoverage)
2031 {
2032 unsigned int mask = 0;
2033 if (mState.sampleCoverageValue != 0)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002034 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002035 float threshold = 0.5f;
2036
2037 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002038 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002039 mask <<= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002040
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002041 if ((i + 1) * mState.sampleCoverageValue >= threshold)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002042 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002043 threshold += 1.0f;
2044 mask |= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002045 }
2046 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002047 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002048
2049 if (mState.sampleCoverageInvert)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002050 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002051 mask = ~mask;
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002052 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002053
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002054 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002055 }
2056 else
2057 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002058 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002059 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002060
2061 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002062 }
2063
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002064 if (mDitherStateDirty)
2065 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002066 mDevice->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002067
2068 mDitherStateDirty = false;
2069 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002070}
2071
daniel@transgaming.com83921382011-01-08 05:46:00 +00002072GLenum Context::applyVertexBuffer(GLint first, GLsizei count)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002073{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002074 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002075
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002076 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002077 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002078 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002079 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002080 }
2081
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002082 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, getCurrentProgram());
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002083}
2084
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002085// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002086GLenum Context::applyIndexBuffer(const void *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002087{
daniel@transgaming.com83921382011-01-08 05:46:00 +00002088 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002089
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002090 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002091 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002092 if (indexInfo->serial != mAppliedIBSerial)
2093 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002094 mDevice->SetIndices(indexInfo->indexBuffer);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002095 mAppliedIBSerial = indexInfo->serial;
2096 }
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002097 }
2098
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002099 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002100}
2101
2102// Applies the shaders and shader constants to the Direct3D 9 device
2103void Context::applyShaders()
2104{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002105 Program *programObject = getCurrentProgram();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002106 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002107 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002108 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2109 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2110
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002111 mDevice->SetPixelShader(pixelShader);
2112 mDevice->SetVertexShader(vertexShader);
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002113 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002114 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002115 }
2116
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002117 programObject->applyUniforms();
2118}
2119
2120// Applies the textures and sampler states to the Direct3D 9 device
2121void Context::applyTextures()
2122{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002123 applyTextures(SAMPLER_PIXEL);
2124
2125 if (mSupportsVertexTexture)
2126 {
2127 applyTextures(SAMPLER_VERTEX);
2128 }
2129}
2130
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002131// For each Direct3D 9 sampler of either the pixel or vertex stage,
2132// looks up the corresponding OpenGL texture image unit and texture type,
2133// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002134void Context::applyTextures(SamplerType type)
2135{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002136 Program *programObject = getCurrentProgram();
2137
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002138 int samplerCount = (type == SAMPLER_PIXEL) ? MAX_TEXTURE_IMAGE_UNITS : MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; // Range of Direct3D 9 samplers of given sampler type
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00002139 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2140 int d3dSamplerOffset = (type == SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002141 int samplerRange = programObject->getUsedSamplerRange(type);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002142
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002143 for (int samplerIndex = 0; samplerIndex < samplerRange; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002144 {
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002145 int textureUnit = programObject->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00002146 int d3dSampler = samplerIndex + d3dSamplerOffset;
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002147
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002148 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002149 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002150 TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002151
2152 Texture *texture = getSamplerTexture(textureUnit, textureType);
2153
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00002154 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->hasDirtyParameters() || texture->hasDirtyImages())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002155 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002156 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2157
2158 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002159 {
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00002160 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->hasDirtyParameters())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002161 {
2162 GLenum wrapS = texture->getWrapS();
2163 GLenum wrapT = texture->getWrapT();
2164 GLenum minFilter = texture->getMinFilter();
2165 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002166
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002167 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2168 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002169
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002170 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002171 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2172 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002173 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2174 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002175 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002176
daniel@transgaming.com0da803b2011-11-09 17:44:58 +00002177 if (appliedTextureSerial[samplerIndex] != texture->getSerial() || texture->hasDirtyImages())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002178 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002179 mDevice->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002180 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002181 }
2182 else
2183 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002184 mDevice->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002185 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002186
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002187 appliedTextureSerial[samplerIndex] = texture->getSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002188 texture->resetDirty();
2189 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002190 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002191 else
2192 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002193 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002194 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002195 mDevice->SetTexture(d3dSampler, NULL);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002196 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002197 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002198 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002199 }
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002200
2201 for (int samplerIndex = samplerRange; samplerIndex < samplerCount; samplerIndex++)
2202 {
2203 if (appliedTextureSerial[samplerIndex] != 0)
2204 {
daniel@transgaming.comc5a7b692011-10-26 02:45:44 +00002205 mDevice->SetTexture(samplerIndex + d3dSamplerOffset, NULL);
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002206 appliedTextureSerial[samplerIndex] = 0;
2207 }
2208 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002209}
2210
2211void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, void* pixels)
2212{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002213 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002214
2215 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2216 {
2217 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2218 }
2219
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002220 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2221 {
2222 return error(GL_INVALID_OPERATION);
2223 }
2224
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002225 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002226
2227 if (!renderTarget)
2228 {
2229 return; // Context must be lost, return silently
2230 }
2231
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002232 D3DSURFACE_DESC desc;
2233 renderTarget->GetDesc(&desc);
2234
2235 IDirect3DSurface9 *systemSurface;
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002236 HRESULT result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format, D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002237
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002238 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002239 {
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002240 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002241 return error(GL_OUT_OF_MEMORY);
2242 }
2243
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002244 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2245 {
2246 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002247 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002248 }
2249
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002250 result = mDevice->GetRenderTargetData(renderTarget, systemSurface);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002251
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002252 if (FAILED(result))
2253 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002254 systemSurface->Release();
2255
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002256 // It turns out that D3D will sometimes produce more error
2257 // codes than those documented.
2258 if (checkDeviceLost(result))
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002259 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002260 else
2261 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002262 UNREACHABLE();
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002263 return;
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002264 }
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002265
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002266 }
2267
2268 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002269 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2270 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2271 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2272 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2273 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002274
2275 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2276
2277 if (FAILED(result))
2278 {
2279 UNREACHABLE();
2280 systemSurface->Release();
2281
2282 return; // No sensible error to generate
2283 }
2284
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002285 unsigned char *source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002286 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002287 unsigned short *dest16 = (unsigned short*)pixels;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002288 int inputPitch = -lock.Pitch;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002289 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002290
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002291 for (int j = 0; j < rect.bottom - rect.top; j++)
2292 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002293 if (desc.Format == D3DFMT_A8R8G8B8 &&
2294 format == GL_BGRA_EXT &&
2295 type == GL_UNSIGNED_BYTE)
2296 {
2297 // Fast path for EXT_read_format_bgra, given
2298 // an RGBA source buffer. Note that buffers with no
2299 // alpha go through the slow path below.
2300 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002301 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002302 (rect.right - rect.left) * 4);
2303 continue;
2304 }
2305
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002306 for (int i = 0; i < rect.right - rect.left; i++)
2307 {
2308 float r;
2309 float g;
2310 float b;
2311 float a;
2312
2313 switch (desc.Format)
2314 {
2315 case D3DFMT_R5G6B5:
2316 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002317 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002318
2319 a = 1.0f;
2320 b = (rgb & 0x001F) * (1.0f / 0x001F);
2321 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2322 r = (rgb & 0xF800) * (1.0f / 0xF800);
2323 }
2324 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002325 case D3DFMT_A1R5G5B5:
2326 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002327 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002328
2329 a = (argb & 0x8000) ? 1.0f : 0.0f;
2330 b = (argb & 0x001F) * (1.0f / 0x001F);
2331 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2332 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2333 }
2334 break;
2335 case D3DFMT_A8R8G8B8:
2336 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002337 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002338
2339 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2340 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2341 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2342 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2343 }
2344 break;
2345 case D3DFMT_X8R8G8B8:
2346 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002347 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002348
2349 a = 1.0f;
2350 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2351 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2352 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2353 }
2354 break;
2355 case D3DFMT_A2R10G10B10:
2356 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002357 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002358
2359 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2360 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2361 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2362 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2363 }
2364 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002365 case D3DFMT_A32B32G32R32F:
2366 {
2367 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002368 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2369 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2370 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2371 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002372 }
2373 break;
2374 case D3DFMT_A16B16G16R16F:
2375 {
2376 // float formats in D3D are stored rgba, rather than the other way round
2377 float abgr[4];
2378
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002379 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002380
2381 a = abgr[3];
2382 b = abgr[2];
2383 g = abgr[1];
2384 r = abgr[0];
2385 }
2386 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002387 default:
2388 UNIMPLEMENTED(); // FIXME
2389 UNREACHABLE();
2390 }
2391
2392 switch (format)
2393 {
2394 case GL_RGBA:
2395 switch (type)
2396 {
2397 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002398 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2399 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2400 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2401 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002402 break;
2403 default: UNREACHABLE();
2404 }
2405 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002406 case GL_BGRA_EXT:
2407 switch (type)
2408 {
2409 case GL_UNSIGNED_BYTE:
2410 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2411 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2412 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2413 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2414 break;
2415 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2416 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2417 // this type is packed as follows:
2418 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2419 // --------------------------------------------------------------------------------
2420 // | 4th | 3rd | 2nd | 1st component |
2421 // --------------------------------------------------------------------------------
2422 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2423 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2424 ((unsigned short)(15 * a + 0.5f) << 12)|
2425 ((unsigned short)(15 * r + 0.5f) << 8) |
2426 ((unsigned short)(15 * g + 0.5f) << 4) |
2427 ((unsigned short)(15 * b + 0.5f) << 0);
2428 break;
2429 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2430 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2431 // this type is packed as follows:
2432 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2433 // --------------------------------------------------------------------------------
2434 // | 4th | 3rd | 2nd | 1st component |
2435 // --------------------------------------------------------------------------------
2436 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2437 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2438 ((unsigned short)( a + 0.5f) << 15) |
2439 ((unsigned short)(31 * r + 0.5f) << 10) |
2440 ((unsigned short)(31 * g + 0.5f) << 5) |
2441 ((unsigned short)(31 * b + 0.5f) << 0);
2442 break;
2443 default: UNREACHABLE();
2444 }
2445 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002446 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002447 switch (type)
2448 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002449 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002450 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2451 ((unsigned short)(31 * b + 0.5f) << 0) |
2452 ((unsigned short)(63 * g + 0.5f) << 5) |
2453 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002454 break;
2455 default: UNREACHABLE();
2456 }
2457 break;
2458 default: UNREACHABLE();
2459 }
2460 }
2461 }
2462
2463 systemSurface->UnlockRect();
2464
2465 systemSurface->Release();
2466}
2467
2468void Context::clear(GLbitfield mask)
2469{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002470 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002471
2472 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2473 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002474 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002475 }
2476
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002477 DWORD flags = 0;
2478
2479 if (mask & GL_COLOR_BUFFER_BIT)
2480 {
2481 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002482
2483 if (framebufferObject->getColorbufferType() != GL_NONE)
2484 {
2485 flags |= D3DCLEAR_TARGET;
2486 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002487 }
2488
2489 if (mask & GL_DEPTH_BUFFER_BIT)
2490 {
2491 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002492 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002493 {
2494 flags |= D3DCLEAR_ZBUFFER;
2495 }
2496 }
2497
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002498 GLuint stencilUnmasked = 0x0;
2499
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002500 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002501 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002502 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002503 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002504 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002505 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002506 if (!depthStencil)
2507 {
2508 ERR("Depth stencil pointer unexpectedly null.");
2509 return;
2510 }
2511
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002512 D3DSURFACE_DESC desc;
2513 depthStencil->GetDesc(&desc);
2514
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002515 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002516 stencilUnmasked = (0x1 << stencilSize) - 1;
2517
2518 if (stencilUnmasked != 0x0)
2519 {
2520 flags |= D3DCLEAR_STENCIL;
2521 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002522 }
2523 }
2524
2525 if (mask != 0)
2526 {
2527 return error(GL_INVALID_VALUE);
2528 }
2529
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002530 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2531 {
2532 return;
2533 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002534
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002535 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002536 unorm<8>(mState.colorClearValue.red),
2537 unorm<8>(mState.colorClearValue.green),
2538 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002539 float depth = clamp01(mState.depthClearValue);
2540 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002541
2542 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
2543
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002544 if (!renderTarget)
2545 {
2546 return; // Context must be lost, return silently
2547 }
2548
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002549 D3DSURFACE_DESC desc;
2550 renderTarget->GetDesc(&desc);
2551
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002552 bool alphaUnmasked = (dx2es::GetAlphaSize(desc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002553
2554 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002555 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002556 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002557 !(mState.colorMaskRed && mState.colorMaskGreen &&
2558 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002559
2560 if (needMaskedColorClear || needMaskedStencilClear)
2561 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002562 // State which is altered in all paths from this point to the clear call is saved.
2563 // State which is altered in only some paths will be flagged dirty in the case that
2564 // that path is taken.
2565 HRESULT hr;
2566 if (mMaskedClearSavedState == NULL)
2567 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002568 hr = mDevice->BeginStateBlock();
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002569 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2570
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002571 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2572 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2573 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2574 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2575 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2576 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2577 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2578 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2579 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2580 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2581 mDevice->SetPixelShader(NULL);
2582 mDevice->SetVertexShader(NULL);
2583 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2584 mDevice->SetStreamSource(0, NULL, 0, 0);
2585 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2586 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2587 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2588 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2589 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2590 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2591 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002592
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002593 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002594 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2595 }
2596
2597 ASSERT(mMaskedClearSavedState != NULL);
2598
2599 if (mMaskedClearSavedState != NULL)
2600 {
2601 hr = mMaskedClearSavedState->Capture();
2602 ASSERT(SUCCEEDED(hr));
2603 }
2604
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002605 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2606 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2607 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2608 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2609 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2610 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2611 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2612 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002613
2614 if (flags & D3DCLEAR_TARGET)
2615 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002616 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002617 }
2618 else
2619 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002620 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002621 }
2622
2623 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2624 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002625 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2626 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2627 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2628 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
2629 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
2630 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
2631 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2632 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002633 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002634 }
2635 else
2636 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002637 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002638 }
2639
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002640 mDevice->SetPixelShader(NULL);
2641 mDevice->SetVertexShader(NULL);
2642 mDevice->SetFVF(D3DFVF_XYZRHW);
2643 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2644 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2645 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2646 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2647 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2648 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2649 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002650
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002651 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2652 quad[0][0] = -0.5f;
2653 quad[0][1] = desc.Height - 0.5f;
2654 quad[0][2] = 0.0f;
2655 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002656
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002657 quad[1][0] = desc.Width - 0.5f;
2658 quad[1][1] = desc.Height - 0.5f;
2659 quad[1][2] = 0.0f;
2660 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002661
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002662 quad[2][0] = -0.5f;
2663 quad[2][1] = -0.5f;
2664 quad[2][2] = 0.0f;
2665 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002666
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002667 quad[3][0] = desc.Width - 0.5f;
2668 quad[3][1] = -0.5f;
2669 quad[3][2] = 0.0f;
2670 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002671
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002672 mDisplay->startScene();
2673 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002674
2675 if (flags & D3DCLEAR_ZBUFFER)
2676 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002677 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
2678 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2679 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002680 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002681
2682 if (mMaskedClearSavedState != NULL)
2683 {
2684 mMaskedClearSavedState->Apply();
2685 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002686 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002687 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002688 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002689 mDevice->Clear(0, NULL, flags, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002690 }
2691}
2692
2693void Context::drawArrays(GLenum mode, GLint first, GLsizei count)
2694{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002695 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002696 {
2697 return error(GL_INVALID_OPERATION);
2698 }
2699
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002700 D3DPRIMITIVETYPE primitiveType;
2701 int primitiveCount;
2702
2703 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2704 return error(GL_INVALID_ENUM);
2705
2706 if (primitiveCount <= 0)
2707 {
2708 return;
2709 }
2710
2711 if (!applyRenderTarget(false))
2712 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002713 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002714 }
2715
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002716 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002717
daniel@transgaming.com83921382011-01-08 05:46:00 +00002718 GLenum err = applyVertexBuffer(first, count);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002719 if (err != GL_NO_ERROR)
2720 {
2721 return error(err);
2722 }
2723
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002724 applyShaders();
2725 applyTextures();
2726
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002727 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002728 {
2729 return error(GL_INVALID_OPERATION);
2730 }
2731
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002732 if (!cullSkipsDraw(mode))
2733 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002734 mDisplay->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002735
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002736 mDevice->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002737
2738 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2739 {
2740 drawClosingLine(first, first + count - 1);
2741 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002742 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002743}
2744
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002745void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const void *indices)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002746{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002747 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002748 {
2749 return error(GL_INVALID_OPERATION);
2750 }
2751
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002752 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002753 {
2754 return error(GL_INVALID_OPERATION);
2755 }
2756
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002757 D3DPRIMITIVETYPE primitiveType;
2758 int primitiveCount;
2759
2760 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2761 return error(GL_INVALID_ENUM);
2762
2763 if (primitiveCount <= 0)
2764 {
2765 return;
2766 }
2767
2768 if (!applyRenderTarget(false))
2769 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002770 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002771 }
2772
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002773 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002774
2775 TranslatedIndexData indexInfo;
2776 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
2777 if (err != GL_NO_ERROR)
2778 {
2779 return error(err);
2780 }
2781
daniel@transgaming.com83921382011-01-08 05:46:00 +00002782 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
2783 err = applyVertexBuffer(indexInfo.minIndex, vertexCount);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002784 if (err != GL_NO_ERROR)
2785 {
2786 return error(err);
2787 }
2788
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002789 applyShaders();
2790 applyTextures();
2791
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002792 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002793 {
2794 return error(GL_INVALID_OPERATION);
2795 }
2796
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002797 if (!cullSkipsDraw(mode))
2798 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002799 mDisplay->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002800
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002801 mDevice->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002802
2803 if (mode == GL_LINE_LOOP) // Draw the last segment separately
2804 {
2805 drawClosingLine(count, type, indices);
2806 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002807 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002808}
2809
daniel@transgaming.com0d86aa72011-10-26 02:35:10 +00002810// Implements glFlush when block is false, glFinish when block is true
2811void Context::sync(bool block)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002812{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002813 IDirect3DQuery9 *eventQuery = NULL;
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002814 HRESULT result;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002815
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002816 result = mDevice->CreateQuery(D3DQUERYTYPE_EVENT, &eventQuery);
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002817 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002818 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002819 ERR("CreateQuery failed hr=%x\n", result);
2820 if (result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY)
2821 {
2822 return error(GL_OUT_OF_MEMORY);
2823 }
2824 ASSERT(false);
2825 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002826 }
2827
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002828 result = eventQuery->Issue(D3DISSUE_END);
2829 if (FAILED(result))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002830 {
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002831 ERR("eventQuery->Issue(END) failed hr=%x\n", result);
2832 ASSERT(false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002833 eventQuery->Release();
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002834 return;
2835 }
apatrick@chromium.org575e7912010-08-25 18:07:12 +00002836
daniel@transgaming.com0d86aa72011-10-26 02:35:10 +00002837 do
2838 {
2839 result = eventQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
2840
2841 if(block && result == S_FALSE)
2842 {
2843 // Keep polling, but allow other threads to do something useful first
2844 Sleep(0);
2845 }
2846 }
2847 while(block && result == S_FALSE);
2848
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002849 eventQuery->Release();
2850
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002851 if (checkDeviceLost(result))
daniel@transgaming.com18b7b5b2011-05-17 18:34:18 +00002852 {
2853 error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002854 }
2855}
2856
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002857void Context::drawClosingLine(unsigned int first, unsigned int last)
2858{
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002859 IDirect3DIndexBuffer9 *indexBuffer = NULL;
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002860 bool succeeded = false;
2861 UINT offset;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002862
2863 if (supports32bitIndices())
2864 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002865 const int spaceNeeded = 2 * sizeof(unsigned int);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002866
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002867 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002868 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002869 mClosingIB = new StreamingIndexBuffer(mDevice, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002870 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002871
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002872 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
2873
2874 unsigned int *data = static_cast<unsigned int*>(mClosingIB->map(spaceNeeded, &offset));
2875 if (data)
2876 {
2877 data[0] = last;
2878 data[1] = first;
2879 mClosingIB->unmap();
2880 offset /= 4;
2881 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002882 }
2883 }
2884 else
2885 {
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002886 const int spaceNeeded = 2 * sizeof(unsigned short);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002887
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002888 if (!mClosingIB)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002889 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002890 mClosingIB = new StreamingIndexBuffer(mDevice, CLOSING_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002891 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002892
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002893 mClosingIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
2894
2895 unsigned short *data = static_cast<unsigned short*>(mClosingIB->map(spaceNeeded, &offset));
2896 if (data)
2897 {
2898 data[0] = last;
2899 data[1] = first;
2900 mClosingIB->unmap();
2901 offset /= 2;
2902 succeeded = true;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002903 }
2904 }
2905
jbauman@chromium.org399c35f2011-04-28 23:19:51 +00002906 if (succeeded)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002907 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002908 mDevice->SetIndices(mClosingIB->getBuffer());
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002909 mAppliedIBSerial = mClosingIB->getSerial();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002910
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002911 mDevice->DrawIndexedPrimitive(D3DPT_LINELIST, 0, 0, last, offset, 1);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002912 }
2913 else
2914 {
2915 ERR("Could not create an index buffer for closing a line loop.");
2916 error(GL_OUT_OF_MEMORY);
2917 }
2918}
2919
2920void Context::drawClosingLine(GLsizei count, GLenum type, const void *indices)
2921{
2922 unsigned int first = 0;
2923 unsigned int last = 0;
2924
2925 if (mState.elementArrayBuffer.get())
2926 {
2927 Buffer *indexBuffer = mState.elementArrayBuffer.get();
2928 intptr_t offset = reinterpret_cast<intptr_t>(indices);
2929 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
2930 }
2931
2932 switch (type)
2933 {
2934 case GL_UNSIGNED_BYTE:
2935 first = static_cast<const GLubyte*>(indices)[0];
2936 last = static_cast<const GLubyte*>(indices)[count - 1];
2937 break;
2938 case GL_UNSIGNED_SHORT:
2939 first = static_cast<const GLushort*>(indices)[0];
2940 last = static_cast<const GLushort*>(indices)[count - 1];
2941 break;
2942 case GL_UNSIGNED_INT:
2943 first = static_cast<const GLuint*>(indices)[0];
2944 last = static_cast<const GLuint*>(indices)[count - 1];
2945 break;
2946 default: UNREACHABLE();
2947 }
2948
2949 drawClosingLine(first, last);
2950}
2951
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002952void Context::recordInvalidEnum()
2953{
2954 mInvalidEnum = true;
2955}
2956
2957void Context::recordInvalidValue()
2958{
2959 mInvalidValue = true;
2960}
2961
2962void Context::recordInvalidOperation()
2963{
2964 mInvalidOperation = true;
2965}
2966
2967void Context::recordOutOfMemory()
2968{
2969 mOutOfMemory = true;
2970}
2971
2972void Context::recordInvalidFramebufferOperation()
2973{
2974 mInvalidFramebufferOperation = true;
2975}
2976
2977// Get one of the recorded errors and clear its flag, if any.
2978// [OpenGL ES 2.0.24] section 2.5 page 13.
2979GLenum Context::getError()
2980{
2981 if (mInvalidEnum)
2982 {
2983 mInvalidEnum = false;
2984
2985 return GL_INVALID_ENUM;
2986 }
2987
2988 if (mInvalidValue)
2989 {
2990 mInvalidValue = false;
2991
2992 return GL_INVALID_VALUE;
2993 }
2994
2995 if (mInvalidOperation)
2996 {
2997 mInvalidOperation = false;
2998
2999 return GL_INVALID_OPERATION;
3000 }
3001
3002 if (mOutOfMemory)
3003 {
3004 mOutOfMemory = false;
3005
3006 return GL_OUT_OF_MEMORY;
3007 }
3008
3009 if (mInvalidFramebufferOperation)
3010 {
3011 mInvalidFramebufferOperation = false;
3012
3013 return GL_INVALID_FRAMEBUFFER_OPERATION;
3014 }
3015
3016 return GL_NO_ERROR;
3017}
3018
daniel@transgaming.com17f548c2011-11-09 17:47:02 +00003019GLenum Context::getResetStatus()
3020{
3021 if (mResetStatus == GL_NO_ERROR)
3022 {
3023 bool lost = mDisplay->testDeviceLost();
3024
3025 if (lost)
3026 {
3027 mDisplay->notifyDeviceLost(); // Sets mResetStatus
3028 }
3029 }
3030
3031 GLenum status = mResetStatus;
3032
3033 if (mResetStatus != GL_NO_ERROR)
3034 {
3035 if (mDisplay->testDeviceResettable())
3036 {
3037 mResetStatus = GL_NO_ERROR;
3038 }
3039 }
3040
3041 return status;
3042}
3043
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00003044bool Context::isResetNotificationEnabled()
3045{
3046 return (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT);
3047}
3048
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003049bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003050{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003051 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003052}
3053
daniel@transgaming.com396c6432010-11-26 16:26:12 +00003054int Context::getMaximumVaryingVectors() const
3055{
3056 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3057}
3058
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003059unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003060{
3061 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3062}
3063
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003064unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003065{
3066 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3067}
3068
daniel@transgaming.com458da142010-11-28 02:03:02 +00003069int Context::getMaximumFragmentUniformVectors() const
3070{
3071 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3072}
3073
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003074int Context::getMaxSupportedSamples() const
3075{
3076 return mMaxSupportedSamples;
3077}
3078
3079int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3080{
3081 if (requested == 0)
3082 {
3083 return requested;
3084 }
3085
3086 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3087 if (itr == mMultiSampleSupport.end())
3088 {
3089 return -1;
3090 }
3091
3092 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3093 {
3094 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3095 {
3096 return i;
3097 }
3098 }
3099
3100 return -1;
3101}
3102
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003103bool Context::supportsEventQueries() const
3104{
3105 return mSupportsEventQueries;
3106}
3107
gman@chromium.org50c526d2011-08-10 05:19:44 +00003108bool Context::supportsDXT1Textures() const
daniel@transgaming.com01868132010-08-24 19:21:17 +00003109{
gman@chromium.org50c526d2011-08-10 05:19:44 +00003110 return mSupportsDXT1Textures;
3111}
3112
3113bool Context::supportsDXT3Textures() const
3114{
3115 return mSupportsDXT3Textures;
3116}
3117
3118bool Context::supportsDXT5Textures() const
3119{
3120 return mSupportsDXT5Textures;
daniel@transgaming.com01868132010-08-24 19:21:17 +00003121}
3122
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003123bool Context::supportsFloat32Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003124{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003125 return mSupportsFloat32Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003126}
3127
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003128bool Context::supportsFloat32LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003129{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003130 return mSupportsFloat32LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003131}
3132
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003133bool Context::supportsFloat32RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003134{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003135 return mSupportsFloat32RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003136}
3137
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003138bool Context::supportsFloat16Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003139{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003140 return mSupportsFloat16Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003141}
3142
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003143bool Context::supportsFloat16LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003144{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003145 return mSupportsFloat16LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003146}
3147
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003148bool Context::supportsFloat16RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003149{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003150 return mSupportsFloat16RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003151}
3152
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003153int Context::getMaximumRenderbufferDimension() const
3154{
3155 return mMaxRenderbufferDimension;
3156}
3157
3158int Context::getMaximumTextureDimension() const
3159{
3160 return mMaxTextureDimension;
3161}
3162
3163int Context::getMaximumCubeTextureDimension() const
3164{
3165 return mMaxCubeTextureDimension;
3166}
3167
3168int Context::getMaximumTextureLevel() const
3169{
3170 return mMaxTextureLevel;
3171}
3172
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003173bool Context::supportsLuminanceTextures() const
3174{
3175 return mSupportsLuminanceTextures;
3176}
3177
3178bool Context::supportsLuminanceAlphaTextures() const
3179{
3180 return mSupportsLuminanceAlphaTextures;
3181}
3182
daniel@transgaming.com83921382011-01-08 05:46:00 +00003183bool Context::supports32bitIndices() const
3184{
3185 return mSupports32bitIndices;
3186}
3187
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003188bool Context::supportsNonPower2Texture() const
3189{
3190 return mSupportsNonPower2Texture;
3191}
3192
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003193void Context::detachBuffer(GLuint buffer)
3194{
3195 // [OpenGL ES 2.0.24] section 2.9 page 22:
3196 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3197 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3198
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003199 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003200 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003201 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003202 }
3203
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003204 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003205 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003206 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003207 }
3208
3209 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3210 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003211 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003212 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003213 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003214 }
3215 }
3216}
3217
3218void Context::detachTexture(GLuint texture)
3219{
3220 // [OpenGL ES 2.0.24] section 3.8 page 84:
3221 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3222 // rebound to texture object zero
3223
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003224 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003225 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003226 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003227 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003228 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003229 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003230 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003231 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003232 }
3233 }
3234
3235 // [OpenGL ES 2.0.24] section 4.4 page 112:
3236 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3237 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3238 // image was attached in the currently bound framebuffer.
3239
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003240 Framebuffer *readFramebuffer = getReadFramebuffer();
3241 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003242
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003243 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003244 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003245 readFramebuffer->detachTexture(texture);
3246 }
3247
3248 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3249 {
3250 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003251 }
3252}
3253
3254void Context::detachFramebuffer(GLuint framebuffer)
3255{
3256 // [OpenGL ES 2.0.24] section 4.4 page 107:
3257 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3258 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3259
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003260 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003261 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003262 bindReadFramebuffer(0);
3263 }
3264
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003265 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003266 {
3267 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003268 }
3269}
3270
3271void Context::detachRenderbuffer(GLuint renderbuffer)
3272{
3273 // [OpenGL ES 2.0.24] section 4.4 page 109:
3274 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3275 // had been executed with the target RENDERBUFFER and name of zero.
3276
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003277 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003278 {
3279 bindRenderbuffer(0);
3280 }
3281
3282 // [OpenGL ES 2.0.24] section 4.4 page 111:
3283 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3284 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3285 // point to which this image was attached in the currently bound framebuffer.
3286
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003287 Framebuffer *readFramebuffer = getReadFramebuffer();
3288 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003289
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003290 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003291 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003292 readFramebuffer->detachRenderbuffer(renderbuffer);
3293 }
3294
3295 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3296 {
3297 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003298 }
3299}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003300
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003301Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003302{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003303 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003304
3305 if (t == NULL)
3306 {
3307 static const GLubyte color[] = { 0, 0, 0, 255 };
3308
3309 switch (type)
3310 {
3311 default:
3312 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003313 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003314
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003315 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003316 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003317 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003318 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003319 t = incomplete2d;
3320 }
3321 break;
3322
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003323 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003324 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003325 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003326
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003327 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3328 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3329 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3330 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3331 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3332 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003333
3334 t = incompleteCube;
3335 }
3336 break;
3337 }
3338
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003339 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003340 }
3341
3342 return t;
3343}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003344
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003345bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003346{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003347 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003348}
3349
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003350bool Context::isTriangleMode(GLenum drawMode)
3351{
3352 switch (drawMode)
3353 {
3354 case GL_TRIANGLES:
3355 case GL_TRIANGLE_FAN:
3356 case GL_TRIANGLE_STRIP:
3357 return true;
3358 case GL_POINTS:
3359 case GL_LINES:
3360 case GL_LINE_LOOP:
3361 case GL_LINE_STRIP:
3362 return false;
3363 default: UNREACHABLE();
3364 }
3365
3366 return false;
3367}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003368
3369void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3370{
3371 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3372
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003373 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3374 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3375 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3376 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003377
daniel@transgaming.com83921382011-01-08 05:46:00 +00003378 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003379}
3380
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003381// keep list sorted in following order
3382// OES extensions
3383// EXT extensions
3384// Vendor extensions
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003385void Context::initExtensionString()
3386{
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003387 mExtensionString = "";
3388
3389 // OES extensions
3390 if (supports32bitIndices())
3391 {
3392 mExtensionString += "GL_OES_element_index_uint ";
3393 }
3394
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003395 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003396 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003397 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003398
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003399 if (supportsFloat16Textures())
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003400 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003401 mExtensionString += "GL_OES_texture_half_float ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003402 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003403 if (supportsFloat16LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003404 {
3405 mExtensionString += "GL_OES_texture_half_float_linear ";
3406 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003407 if (supportsFloat32Textures())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003408 {
3409 mExtensionString += "GL_OES_texture_float ";
3410 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003411 if (supportsFloat32LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003412 {
3413 mExtensionString += "GL_OES_texture_float_linear ";
3414 }
3415
3416 if (supportsNonPower2Texture())
3417 {
3418 mExtensionString += "GL_OES_texture_npot ";
3419 }
3420
3421 // Multi-vendor (EXT) extensions
3422 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com8747f182011-11-09 17:50:38 +00003423 mExtensionString += "GL_EXT_robustness ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003424
gman@chromium.org50c526d2011-08-10 05:19:44 +00003425 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00003426 {
3427 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3428 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003429
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003430 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
gman@chromium.org50c526d2011-08-10 05:19:44 +00003431
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003432 // ANGLE-specific extensions
3433 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003434 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003435 {
3436 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3437 }
3438
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003439 if (supportsDXT3Textures())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003440 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003441 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3442 }
3443 if (supportsDXT5Textures())
3444 {
3445 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003446 }
zmo@google.coma574f782011-10-03 21:45:23 +00003447 mExtensionString += "GL_ANGLE_translated_shader_source ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003448
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003449 // Other vendor-specific extensions
3450 if (supportsEventQueries())
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003451 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003452 mExtensionString += "GL_NV_fence ";
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003453 }
3454
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003455 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3456 if (end != std::string::npos)
3457 {
3458 mExtensionString.resize(end+1);
3459 }
3460}
3461
3462const char *Context::getExtensionString() const
3463{
3464 return mExtensionString.c_str();
3465}
3466
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003467void Context::initRendererString()
3468{
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003469 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003470
3471 mRendererString = "ANGLE (";
3472 mRendererString += identifier->Description;
3473 mRendererString += ")";
3474}
3475
3476const char *Context::getRendererString() const
3477{
3478 return mRendererString.c_str();
3479}
3480
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003481void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3482 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3483 GLbitfield mask)
3484{
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003485 Framebuffer *readFramebuffer = getReadFramebuffer();
3486 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3487
3488 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3489 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3490 {
3491 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3492 }
3493
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003494 if (drawFramebuffer->getSamples() != 0)
3495 {
3496 return error(GL_INVALID_OPERATION);
3497 }
3498
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003499 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3500 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3501 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3502 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3503
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003504 RECT sourceRect;
3505 RECT destRect;
3506
3507 if (srcX0 < srcX1)
3508 {
3509 sourceRect.left = srcX0;
3510 sourceRect.right = srcX1;
3511 destRect.left = dstX0;
3512 destRect.right = dstX1;
3513 }
3514 else
3515 {
3516 sourceRect.left = srcX1;
3517 destRect.left = dstX1;
3518 sourceRect.right = srcX0;
3519 destRect.right = dstX0;
3520 }
3521
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003522 if (srcY0 < srcY1)
3523 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003524 sourceRect.top = readBufferHeight - srcY1;
3525 destRect.top = drawBufferHeight - dstY1;
3526 sourceRect.bottom = readBufferHeight - srcY0;
3527 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003528 }
3529 else
3530 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003531 sourceRect.top = readBufferHeight - srcY0;
3532 destRect.top = drawBufferHeight - dstY0;
3533 sourceRect.bottom = readBufferHeight - srcY1;
3534 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003535 }
3536
3537 RECT sourceScissoredRect = sourceRect;
3538 RECT destScissoredRect = destRect;
3539
3540 if (mState.scissorTest)
3541 {
3542 // Only write to parts of the destination framebuffer which pass the scissor test
3543 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3544 // rect will be checked against scissorY, rather than the bottom.
3545 if (destRect.left < mState.scissorX)
3546 {
3547 int xDiff = mState.scissorX - destRect.left;
3548 destScissoredRect.left = mState.scissorX;
3549 sourceScissoredRect.left += xDiff;
3550 }
3551
3552 if (destRect.right > mState.scissorX + mState.scissorWidth)
3553 {
3554 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3555 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3556 sourceScissoredRect.right -= xDiff;
3557 }
3558
3559 if (destRect.top < mState.scissorY)
3560 {
3561 int yDiff = mState.scissorY - destRect.top;
3562 destScissoredRect.top = mState.scissorY;
3563 sourceScissoredRect.top += yDiff;
3564 }
3565
3566 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3567 {
3568 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3569 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3570 sourceScissoredRect.bottom -= yDiff;
3571 }
3572 }
3573
3574 bool blitRenderTarget = false;
3575 bool blitDepthStencil = false;
3576
3577 RECT sourceTrimmedRect = sourceScissoredRect;
3578 RECT destTrimmedRect = destScissoredRect;
3579
3580 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3581 // the actual draw and read surfaces.
3582 if (sourceTrimmedRect.left < 0)
3583 {
3584 int xDiff = 0 - sourceTrimmedRect.left;
3585 sourceTrimmedRect.left = 0;
3586 destTrimmedRect.left += xDiff;
3587 }
3588
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003589 if (sourceTrimmedRect.right > readBufferWidth)
3590 {
3591 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3592 sourceTrimmedRect.right = readBufferWidth;
3593 destTrimmedRect.right -= xDiff;
3594 }
3595
3596 if (sourceTrimmedRect.top < 0)
3597 {
3598 int yDiff = 0 - sourceTrimmedRect.top;
3599 sourceTrimmedRect.top = 0;
3600 destTrimmedRect.top += yDiff;
3601 }
3602
3603 if (sourceTrimmedRect.bottom > readBufferHeight)
3604 {
3605 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3606 sourceTrimmedRect.bottom = readBufferHeight;
3607 destTrimmedRect.bottom -= yDiff;
3608 }
3609
3610 if (destTrimmedRect.left < 0)
3611 {
3612 int xDiff = 0 - destTrimmedRect.left;
3613 destTrimmedRect.left = 0;
3614 sourceTrimmedRect.left += xDiff;
3615 }
3616
3617 if (destTrimmedRect.right > drawBufferWidth)
3618 {
3619 int xDiff = destTrimmedRect.right - drawBufferWidth;
3620 destTrimmedRect.right = drawBufferWidth;
3621 sourceTrimmedRect.right -= xDiff;
3622 }
3623
3624 if (destTrimmedRect.top < 0)
3625 {
3626 int yDiff = 0 - destTrimmedRect.top;
3627 destTrimmedRect.top = 0;
3628 sourceTrimmedRect.top += yDiff;
3629 }
3630
3631 if (destTrimmedRect.bottom > drawBufferHeight)
3632 {
3633 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3634 destTrimmedRect.bottom = drawBufferHeight;
3635 sourceTrimmedRect.bottom -= yDiff;
3636 }
3637
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003638 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003639 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3640 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3641 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3642 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003643 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3644 {
3645 partialBufferCopy = true;
3646 }
3647
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003648 if (mask & GL_COLOR_BUFFER_BIT)
3649 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003650 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3651 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3652 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3653 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3654 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003655 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3656 {
3657 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3658 return error(GL_INVALID_OPERATION);
3659 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003660
3661 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3662 {
3663 return error(GL_INVALID_OPERATION);
3664 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003665
3666 blitRenderTarget = true;
3667
3668 }
3669
3670 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3671 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00003672 Renderbuffer *readDSBuffer = NULL;
3673 Renderbuffer *drawDSBuffer = NULL;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003674
3675 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3676 // both a depth and stencil buffer, it will be the same buffer.
3677
3678 if (mask & GL_DEPTH_BUFFER_BIT)
3679 {
3680 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3681 {
3682 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3683 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3684 {
3685 return error(GL_INVALID_OPERATION);
3686 }
3687
3688 blitDepthStencil = true;
3689 readDSBuffer = readFramebuffer->getDepthbuffer();
3690 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3691 }
3692 }
3693
3694 if (mask & GL_STENCIL_BUFFER_BIT)
3695 {
3696 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3697 {
3698 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3699 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3700 {
3701 return error(GL_INVALID_OPERATION);
3702 }
3703
3704 blitDepthStencil = true;
3705 readDSBuffer = readFramebuffer->getStencilbuffer();
3706 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3707 }
3708 }
3709
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003710 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003711 {
3712 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3713 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3714 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003715
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003716 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3717 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003718 {
3719 return error(GL_INVALID_OPERATION);
3720 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003721 }
3722
3723 if (blitRenderTarget || blitDepthStencil)
3724 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003725 mDisplay->endScene();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003726
3727 if (blitRenderTarget)
3728 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003729 HRESULT result = mDevice->StretchRect(readFramebuffer->getRenderTarget(), &sourceTrimmedRect,
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003730 drawFramebuffer->getRenderTarget(), &destTrimmedRect, D3DTEXF_NONE);
3731
3732 if (FAILED(result))
3733 {
3734 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3735 return;
3736 }
3737 }
3738
3739 if (blitDepthStencil)
3740 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003741 HRESULT result = mDevice->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003742
3743 if (FAILED(result))
3744 {
3745 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
3746 return;
3747 }
3748 }
3749 }
3750}
3751
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003752VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
3753{
3754 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3755 {
3756 mVertexDeclCache[i].vertexDeclaration = NULL;
3757 mVertexDeclCache[i].lruCount = 0;
3758 }
3759}
3760
3761VertexDeclarationCache::~VertexDeclarationCache()
3762{
3763 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3764 {
3765 if (mVertexDeclCache[i].vertexDeclaration)
3766 {
3767 mVertexDeclCache[i].vertexDeclaration->Release();
3768 }
3769 }
3770}
3771
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003772GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], Program *program)
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003773{
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003774 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
3775 D3DVERTEXELEMENT9 *element = &elements[0];
3776
3777 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3778 {
3779 if (attributes[i].active)
3780 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003781 if (mAppliedVBs[i].serial != attributes[i].serial ||
3782 mAppliedVBs[i].stride != attributes[i].stride ||
3783 mAppliedVBs[i].offset != attributes[i].offset)
3784 {
3785 device->SetStreamSource(i, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
3786 mAppliedVBs[i].serial = attributes[i].serial;
3787 mAppliedVBs[i].stride = attributes[i].stride;
3788 mAppliedVBs[i].offset = attributes[i].offset;
3789 }
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003790
3791 element->Stream = i;
3792 element->Offset = 0;
3793 element->Type = attributes[i].type;
3794 element->Method = D3DDECLMETHOD_DEFAULT;
3795 element->Usage = D3DDECLUSAGE_TEXCOORD;
3796 element->UsageIndex = program->getSemanticIndex(i);
3797 element++;
3798 }
3799 }
3800
3801 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
3802 *(element++) = end;
3803
3804 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3805 {
3806 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
3807 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
3808 {
3809 entry->lruCount = ++mMaxLru;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003810 if(entry->vertexDeclaration != mLastSetVDecl)
3811 {
3812 device->SetVertexDeclaration(entry->vertexDeclaration);
3813 mLastSetVDecl = entry->vertexDeclaration;
3814 }
3815
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003816 return GL_NO_ERROR;
3817 }
3818 }
3819
3820 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
3821
3822 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
3823 {
3824 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
3825 {
3826 lastCache = &mVertexDeclCache[i];
3827 }
3828 }
3829
3830 if (lastCache->vertexDeclaration != NULL)
3831 {
3832 lastCache->vertexDeclaration->Release();
3833 lastCache->vertexDeclaration = NULL;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003834 // mLastSetVDecl is set to the replacement, so we don't have to worry
3835 // about it.
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003836 }
3837
3838 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
3839 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
3840 device->SetVertexDeclaration(lastCache->vertexDeclaration);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003841 mLastSetVDecl = lastCache->vertexDeclaration;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00003842 lastCache->lruCount = ++mMaxLru;
3843
3844 return GL_NO_ERROR;
3845}
3846
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00003847void VertexDeclarationCache::markStateDirty()
3848{
3849 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
3850 {
3851 mAppliedVBs[i].serial = 0;
3852 }
3853
3854 mLastSetVDecl = NULL;
3855}
3856
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003857}
3858
3859extern "C"
3860{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00003861gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext, bool notifyResets, bool robustAccess)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003862{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00003863 return new gl::Context(config, shareContext, notifyResets, robustAccess);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003864}
3865
3866void glDestroyContext(gl::Context *context)
3867{
3868 delete context;
3869
3870 if (context == gl::getContext())
3871 {
3872 gl::makeCurrent(NULL, NULL, NULL);
3873 }
3874}
3875
3876void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
3877{
3878 gl::makeCurrent(context, display, surface);
3879}
3880
3881gl::Context *glGetCurrentContext()
3882{
3883 return gl::getContext();
3884}
3885}