blob: a816b0c377488e3868bf5ef7e12838476a89f16f [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002// Copyright (c) 2002-2012 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"
apatrick@chromium.orgea09f9b2012-06-08 00:45:32 +000025#include "libGLESv2/ProgramBinary.h"
daniel@transgaming.com86bdb822012-01-20 18:24:39 +000026#include "libGLESv2/Query.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000027#include "libGLESv2/RenderBuffer.h"
28#include "libGLESv2/Shader.h"
29#include "libGLESv2/Texture.h"
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +000030#include "libGLESv2/VertexDataManager.h"
31#include "libGLESv2/IndexDataManager.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032
daniel@transgaming.com86487c22010-03-11 19:41:43 +000033#undef near
34#undef far
35
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000036namespace gl
37{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +000038Context::Context(const egl::Config *config, const gl::Context *shareContext, bool notifyResets, bool robustAccess) : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000039{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +000040 ASSERT(robustAccess == false); // Unimplemented
41
daniel@transgaming.comc941e252011-10-26 02:32:31 +000042 mDisplay = NULL;
43 mDevice = NULL;
44
benvanik@google.com1a233342011-04-28 19:44:39 +000045 mFenceHandleAllocator.setBaseHandle(0);
46
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000047 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000048
daniel@transgaming.com428d1582010-05-04 03:35:25 +000049 mState.depthClearValue = 1.0f;
50 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000051
daniel@transgaming.com428d1582010-05-04 03:35:25 +000052 mState.cullFace = false;
53 mState.cullMode = GL_BACK;
54 mState.frontFace = GL_CCW;
55 mState.depthTest = false;
56 mState.depthFunc = GL_LESS;
57 mState.blend = false;
58 mState.sourceBlendRGB = GL_ONE;
59 mState.sourceBlendAlpha = GL_ONE;
60 mState.destBlendRGB = GL_ZERO;
61 mState.destBlendAlpha = GL_ZERO;
62 mState.blendEquationRGB = GL_FUNC_ADD;
63 mState.blendEquationAlpha = GL_FUNC_ADD;
64 mState.blendColor.red = 0;
65 mState.blendColor.green = 0;
66 mState.blendColor.blue = 0;
67 mState.blendColor.alpha = 0;
68 mState.stencilTest = false;
69 mState.stencilFunc = GL_ALWAYS;
70 mState.stencilRef = 0;
71 mState.stencilMask = -1;
72 mState.stencilWritemask = -1;
73 mState.stencilBackFunc = GL_ALWAYS;
74 mState.stencilBackRef = 0;
75 mState.stencilBackMask = - 1;
76 mState.stencilBackWritemask = -1;
77 mState.stencilFail = GL_KEEP;
78 mState.stencilPassDepthFail = GL_KEEP;
79 mState.stencilPassDepthPass = GL_KEEP;
80 mState.stencilBackFail = GL_KEEP;
81 mState.stencilBackPassDepthFail = GL_KEEP;
82 mState.stencilBackPassDepthPass = GL_KEEP;
83 mState.polygonOffsetFill = false;
84 mState.polygonOffsetFactor = 0.0f;
85 mState.polygonOffsetUnits = 0.0f;
86 mState.sampleAlphaToCoverage = false;
87 mState.sampleCoverage = false;
88 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000089 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000090 mState.scissorTest = false;
91 mState.dither = true;
92 mState.generateMipmapHint = GL_DONT_CARE;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +000093 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000094
daniel@transgaming.com428d1582010-05-04 03:35:25 +000095 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000096
daniel@transgaming.com428d1582010-05-04 03:35:25 +000097 mState.viewportX = 0;
98 mState.viewportY = 0;
99 mState.viewportWidth = config->mDisplayMode.Width;
100 mState.viewportHeight = config->mDisplayMode.Height;
101 mState.zNear = 0.0f;
102 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000103
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000104 mState.scissorX = 0;
105 mState.scissorY = 0;
106 mState.scissorWidth = config->mDisplayMode.Width;
107 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000108
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000109 mState.colorMaskRed = true;
110 mState.colorMaskGreen = true;
111 mState.colorMaskBlue = true;
112 mState.colorMaskAlpha = true;
113 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000114
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000115 if (shareContext != NULL)
116 {
117 mResourceManager = shareContext->mResourceManager;
118 mResourceManager->addRef();
119 }
120 else
121 {
122 mResourceManager = new ResourceManager();
123 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000124
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000125 // [OpenGL ES 2.0.24] section 3.7 page 83:
126 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
127 // and cube map texture state vectors respectively associated with them.
128 // In order that access to these initial textures not be lost, they are treated as texture
129 // objects all of whose names are 0.
130
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000131 mTexture2DZero.set(new Texture2D(0));
132 mTextureCubeMapZero.set(new TextureCubeMap(0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000133
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000134 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000135 bindArrayBuffer(0);
136 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000137 bindTextureCubeMap(0);
138 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000139 bindReadFramebuffer(0);
140 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000141 bindRenderbuffer(0);
142
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000143 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000144
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000145 mState.packAlignment = 4;
146 mState.unpackAlignment = 4;
bsalomon@google.com56d46ab2011-11-23 14:53:10 +0000147 mState.packReverseRowOrder = false;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000148
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000149 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000150 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000151 mBlit = NULL;
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +0000152 mLineLoopIB = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000153
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000154 mInvalidEnum = false;
155 mInvalidValue = false;
156 mInvalidOperation = false;
157 mOutOfMemory = false;
158 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000159
160 mHasBeenCurrent = false;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000161 mContextLost = false;
daniel@transgaming.com17f548c2011-11-09 17:47:02 +0000162 mResetStatus = GL_NO_ERROR;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +0000163 mResetStrategy = (notifyResets ? GL_LOSE_CONTEXT_ON_RESET_EXT : GL_NO_RESET_NOTIFICATION_EXT);
164 mRobustAccess = robustAccess;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000165
gman@chromium.org50c526d2011-08-10 05:19:44 +0000166 mSupportsDXT1Textures = false;
167 mSupportsDXT3Textures = false;
168 mSupportsDXT5Textures = false;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000169 mSupportsEventQueries = false;
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000170 mSupportsOcclusionQueries = false;
gman@chromium.org50c526d2011-08-10 05:19:44 +0000171 mNumCompressedTextureFormats = 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000172 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000173 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000174 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000175}
176
177Context::~Context()
178{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000179 if (mState.currentProgram != 0)
180 {
181 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
182 if (programObject)
183 {
184 programObject->release();
185 }
186 mState.currentProgram = 0;
187 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000188
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000189 while (!mFramebufferMap.empty())
190 {
191 deleteFramebuffer(mFramebufferMap.begin()->first);
192 }
193
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000194 while (!mFenceMap.empty())
195 {
196 deleteFence(mFenceMap.begin()->first);
197 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000198
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000199 while (!mQueryMap.empty())
200 {
201 deleteQuery(mQueryMap.begin()->first);
202 }
203
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000204 while (!mMultiSampleSupport.empty())
205 {
206 delete [] mMultiSampleSupport.begin()->second;
207 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
208 }
209
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000210 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000211 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +0000212 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000213 {
214 mState.samplerTexture[type][sampler].set(NULL);
215 }
216 }
217
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000218 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000219 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000220 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000221 }
222
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000223 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
224 {
225 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
226 }
227
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000228 for (int i = 0; i < QUERY_TYPE_COUNT; i++)
229 {
230 mState.activeQuery[i].set(NULL);
231 }
232
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000233 mState.arrayBuffer.set(NULL);
234 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000235 mState.renderbuffer.set(NULL);
236
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000237 mTexture2DZero.set(NULL);
238 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000239
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000240 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000241 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000242 delete mBlit;
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +0000243 delete mLineLoopIB;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000244
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000245 if (mMaskedClearSavedState)
246 {
247 mMaskedClearSavedState->Release();
248 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000249
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000250 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000251}
252
253void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
254{
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000255 mDisplay = display;
256 mDevice = mDisplay->getDevice();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000257
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000258 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000259 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000260 mDeviceCaps = mDisplay->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000261
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000262 mVertexDataManager = new VertexDataManager(this, mDevice);
263 mIndexDataManager = new IndexDataManager(this, mDevice);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000264 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000265
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +0000266 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000267 mSupportsVertexTexture = mDisplay->getVertexTextureSupport();
268 mSupportsNonPower2Texture = mDisplay->getNonPower2TextureSupport();
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +0000269 mSupportsInstancing = mDisplay->getInstancingSupport();
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000270
271 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
272 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
273 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
274 mMaxRenderbufferDimension = mMaxTextureDimension;
275 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
276 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
277 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
278
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000279 const D3DFORMAT renderBufferFormats[] =
280 {
281 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000282 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000283 D3DFMT_R5G6B5,
284 D3DFMT_D24S8
285 };
286
287 int max = 0;
288 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
289 {
290 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000291 mDisplay->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000292 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
293
294 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
295 {
296 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
297 {
298 max = j;
299 }
300 }
301 }
302
303 mMaxSupportedSamples = max;
304
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000305 mSupportsEventQueries = mDisplay->getEventQuerySupport();
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000306 mSupportsOcclusionQueries = mDisplay->getOcclusionQuerySupport();
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000307 mSupportsDXT1Textures = mDisplay->getDXT1TextureSupport();
308 mSupportsDXT3Textures = mDisplay->getDXT3TextureSupport();
309 mSupportsDXT5Textures = mDisplay->getDXT5TextureSupport();
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +0000310 mSupportsFloat32Textures = mDisplay->getFloat32TextureSupport(&mSupportsFloat32LinearFilter, &mSupportsFloat32RenderableTextures);
311 mSupportsFloat16Textures = mDisplay->getFloat16TextureSupport(&mSupportsFloat16LinearFilter, &mSupportsFloat16RenderableTextures);
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000312 mSupportsLuminanceTextures = mDisplay->getLuminanceTextureSupport();
313 mSupportsLuminanceAlphaTextures = mDisplay->getLuminanceAlphaTextureSupport();
daniel@transgaming.com1c49f792012-05-31 01:14:02 +0000314 mSupportsDepthTextures = mDisplay->getDepthTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000315
daniel@transgaming.com83921382011-01-08 05:46:00 +0000316 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
317
gman@chromium.org50c526d2011-08-10 05:19:44 +0000318 mNumCompressedTextureFormats = 0;
319 if (supportsDXT1Textures())
320 {
321 mNumCompressedTextureFormats += 2;
322 }
323 if (supportsDXT3Textures())
324 {
325 mNumCompressedTextureFormats += 1;
326 }
327 if (supportsDXT5Textures())
328 {
329 mNumCompressedTextureFormats += 1;
330 }
331
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000332 initExtensionString();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +0000333 initRendererString();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000334
335 mState.viewportX = 0;
336 mState.viewportY = 0;
337 mState.viewportWidth = surface->getWidth();
338 mState.viewportHeight = surface->getHeight();
339
340 mState.scissorX = 0;
341 mState.scissorY = 0;
342 mState.scissorWidth = surface->getWidth();
343 mState.scissorHeight = surface->getHeight();
344
345 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000346 }
347
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000348 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
349 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000350 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000351
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000352 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000353 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000354 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000355
356 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000357
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000358 if (defaultRenderTarget)
359 {
360 defaultRenderTarget->Release();
361 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000362
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000363 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000364 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000365 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000366 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000367
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000368 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000369}
370
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000371// 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 +0000372void Context::markAllStateDirty()
373{
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000374 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
375 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +0000376 mAppliedTextureSerialPS[t] = 0;
377 }
378
379 for (int t = 0; t < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; t++)
380 {
381 mAppliedTextureSerialVS[t] = 0;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000382 }
383
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000384 mAppliedProgramSerial = 0;
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000385 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000386 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000387 mAppliedStencilbufferSerial = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000388 mAppliedIBSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000389 mDepthStencilInitialized = false;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000390 mViewportInitialized = false;
391 mRenderTargetDescInitialized = false;
392
393 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000394
395 mClearStateDirty = true;
396 mCullStateDirty = true;
397 mDepthStateDirty = true;
398 mMaskStateDirty = true;
399 mBlendStateDirty = true;
400 mStencilStateDirty = true;
401 mPolygonOffsetStateDirty = true;
402 mScissorStateDirty = true;
403 mSampleStateDirty = true;
404 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000405 mFrontFaceDirty = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +0000406 mDxUniformsDirty = true;
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000407 mCachedCurrentProgram = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000408}
409
daniel@transgaming.com11399d52012-04-28 00:35:14 +0000410void Context::markDxUniformsDirty()
411{
412 mDxUniformsDirty = true;
413}
414
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000415void Context::markContextLost()
416{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +0000417 if (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT)
418 mResetStatus = GL_UNKNOWN_CONTEXT_RESET_EXT;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000419 mContextLost = true;
420}
421
422bool Context::isContextLost()
423{
424 return mContextLost;
425}
426
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000427void Context::setClearColor(float red, float green, float blue, float alpha)
428{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000429 mState.colorClearValue.red = red;
430 mState.colorClearValue.green = green;
431 mState.colorClearValue.blue = blue;
432 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000433}
434
435void Context::setClearDepth(float depth)
436{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000437 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000438}
439
440void Context::setClearStencil(int stencil)
441{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000442 mState.stencilClearValue = stencil;
443}
444
445void Context::setCullFace(bool enabled)
446{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000447 if (mState.cullFace != enabled)
448 {
449 mState.cullFace = enabled;
450 mCullStateDirty = true;
451 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000452}
453
454bool Context::isCullFaceEnabled() const
455{
456 return mState.cullFace;
457}
458
459void Context::setCullMode(GLenum mode)
460{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000461 if (mState.cullMode != mode)
462 {
463 mState.cullMode = mode;
464 mCullStateDirty = true;
465 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000466}
467
468void Context::setFrontFace(GLenum front)
469{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000470 if (mState.frontFace != front)
471 {
472 mState.frontFace = front;
473 mFrontFaceDirty = true;
474 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000475}
476
477void Context::setDepthTest(bool enabled)
478{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000479 if (mState.depthTest != enabled)
480 {
481 mState.depthTest = enabled;
482 mDepthStateDirty = true;
483 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000484}
485
486bool Context::isDepthTestEnabled() const
487{
488 return mState.depthTest;
489}
490
491void Context::setDepthFunc(GLenum depthFunc)
492{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000493 if (mState.depthFunc != depthFunc)
494 {
495 mState.depthFunc = depthFunc;
496 mDepthStateDirty = true;
497 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000498}
499
500void Context::setDepthRange(float zNear, float zFar)
501{
502 mState.zNear = zNear;
503 mState.zFar = zFar;
504}
505
506void Context::setBlend(bool enabled)
507{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000508 if (mState.blend != enabled)
509 {
510 mState.blend = enabled;
511 mBlendStateDirty = true;
512 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000513}
514
515bool Context::isBlendEnabled() const
516{
517 return mState.blend;
518}
519
520void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
521{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000522 if (mState.sourceBlendRGB != sourceRGB ||
523 mState.sourceBlendAlpha != sourceAlpha ||
524 mState.destBlendRGB != destRGB ||
525 mState.destBlendAlpha != destAlpha)
526 {
527 mState.sourceBlendRGB = sourceRGB;
528 mState.destBlendRGB = destRGB;
529 mState.sourceBlendAlpha = sourceAlpha;
530 mState.destBlendAlpha = destAlpha;
531 mBlendStateDirty = true;
532 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000533}
534
535void Context::setBlendColor(float red, float green, float blue, float alpha)
536{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000537 if (mState.blendColor.red != red ||
538 mState.blendColor.green != green ||
539 mState.blendColor.blue != blue ||
540 mState.blendColor.alpha != alpha)
541 {
542 mState.blendColor.red = red;
543 mState.blendColor.green = green;
544 mState.blendColor.blue = blue;
545 mState.blendColor.alpha = alpha;
546 mBlendStateDirty = true;
547 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000548}
549
550void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
551{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000552 if (mState.blendEquationRGB != rgbEquation ||
553 mState.blendEquationAlpha != alphaEquation)
554 {
555 mState.blendEquationRGB = rgbEquation;
556 mState.blendEquationAlpha = alphaEquation;
557 mBlendStateDirty = true;
558 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000559}
560
561void Context::setStencilTest(bool enabled)
562{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000563 if (mState.stencilTest != enabled)
564 {
565 mState.stencilTest = enabled;
566 mStencilStateDirty = true;
567 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000568}
569
570bool Context::isStencilTestEnabled() const
571{
572 return mState.stencilTest;
573}
574
575void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
576{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000577 if (mState.stencilFunc != stencilFunc ||
578 mState.stencilRef != stencilRef ||
579 mState.stencilMask != stencilMask)
580 {
581 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000582 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000583 mState.stencilMask = stencilMask;
584 mStencilStateDirty = true;
585 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000586}
587
588void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
589{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000590 if (mState.stencilBackFunc != stencilBackFunc ||
591 mState.stencilBackRef != stencilBackRef ||
592 mState.stencilBackMask != stencilBackMask)
593 {
594 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000595 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000596 mState.stencilBackMask = stencilBackMask;
597 mStencilStateDirty = true;
598 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000599}
600
601void Context::setStencilWritemask(GLuint stencilWritemask)
602{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000603 if (mState.stencilWritemask != stencilWritemask)
604 {
605 mState.stencilWritemask = stencilWritemask;
606 mStencilStateDirty = true;
607 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000608}
609
610void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
611{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000612 if (mState.stencilBackWritemask != stencilBackWritemask)
613 {
614 mState.stencilBackWritemask = stencilBackWritemask;
615 mStencilStateDirty = true;
616 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000617}
618
619void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
620{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000621 if (mState.stencilFail != stencilFail ||
622 mState.stencilPassDepthFail != stencilPassDepthFail ||
623 mState.stencilPassDepthPass != stencilPassDepthPass)
624 {
625 mState.stencilFail = stencilFail;
626 mState.stencilPassDepthFail = stencilPassDepthFail;
627 mState.stencilPassDepthPass = stencilPassDepthPass;
628 mStencilStateDirty = true;
629 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000630}
631
632void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
633{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000634 if (mState.stencilBackFail != stencilBackFail ||
635 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
636 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
637 {
638 mState.stencilBackFail = stencilBackFail;
639 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
640 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
641 mStencilStateDirty = true;
642 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000643}
644
645void Context::setPolygonOffsetFill(bool enabled)
646{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000647 if (mState.polygonOffsetFill != enabled)
648 {
649 mState.polygonOffsetFill = enabled;
650 mPolygonOffsetStateDirty = true;
651 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000652}
653
654bool Context::isPolygonOffsetFillEnabled() const
655{
656 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000657
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000658}
659
660void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
661{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000662 if (mState.polygonOffsetFactor != factor ||
663 mState.polygonOffsetUnits != units)
664 {
665 mState.polygonOffsetFactor = factor;
666 mState.polygonOffsetUnits = units;
667 mPolygonOffsetStateDirty = true;
668 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000669}
670
671void Context::setSampleAlphaToCoverage(bool enabled)
672{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000673 if (mState.sampleAlphaToCoverage != enabled)
674 {
675 mState.sampleAlphaToCoverage = enabled;
676 mSampleStateDirty = true;
677 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000678}
679
680bool Context::isSampleAlphaToCoverageEnabled() const
681{
682 return mState.sampleAlphaToCoverage;
683}
684
685void Context::setSampleCoverage(bool enabled)
686{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000687 if (mState.sampleCoverage != enabled)
688 {
689 mState.sampleCoverage = enabled;
690 mSampleStateDirty = true;
691 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000692}
693
694bool Context::isSampleCoverageEnabled() const
695{
696 return mState.sampleCoverage;
697}
698
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000699void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000700{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000701 if (mState.sampleCoverageValue != value ||
702 mState.sampleCoverageInvert != invert)
703 {
704 mState.sampleCoverageValue = value;
705 mState.sampleCoverageInvert = invert;
706 mSampleStateDirty = true;
707 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000708}
709
710void Context::setScissorTest(bool enabled)
711{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000712 if (mState.scissorTest != enabled)
713 {
714 mState.scissorTest = enabled;
715 mScissorStateDirty = true;
716 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000717}
718
719bool Context::isScissorTestEnabled() const
720{
721 return mState.scissorTest;
722}
723
724void Context::setDither(bool enabled)
725{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000726 if (mState.dither != enabled)
727 {
728 mState.dither = enabled;
729 mDitherStateDirty = true;
730 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000731}
732
733bool Context::isDitherEnabled() const
734{
735 return mState.dither;
736}
737
738void Context::setLineWidth(GLfloat width)
739{
740 mState.lineWidth = width;
741}
742
743void Context::setGenerateMipmapHint(GLenum hint)
744{
745 mState.generateMipmapHint = hint;
746}
747
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000748void Context::setFragmentShaderDerivativeHint(GLenum hint)
749{
750 mState.fragmentShaderDerivativeHint = hint;
751 // TODO: Propagate the hint to shader translator so we can write
752 // ddx, ddx_coarse, or ddx_fine depending on the hint.
753 // Ignore for now. It is valid for implementations to ignore hint.
754}
755
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000756void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
757{
758 mState.viewportX = x;
759 mState.viewportY = y;
760 mState.viewportWidth = width;
761 mState.viewportHeight = height;
762}
763
764void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
765{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000766 if (mState.scissorX != x || mState.scissorY != y ||
767 mState.scissorWidth != width || mState.scissorHeight != height)
768 {
769 mState.scissorX = x;
770 mState.scissorY = y;
771 mState.scissorWidth = width;
772 mState.scissorHeight = height;
773 mScissorStateDirty = true;
774 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000775}
776
777void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
778{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000779 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
780 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
781 {
782 mState.colorMaskRed = red;
783 mState.colorMaskGreen = green;
784 mState.colorMaskBlue = blue;
785 mState.colorMaskAlpha = alpha;
786 mMaskStateDirty = true;
787 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000788}
789
790void Context::setDepthMask(bool mask)
791{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000792 if (mState.depthMask != mask)
793 {
794 mState.depthMask = mask;
795 mMaskStateDirty = true;
796 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000797}
798
daniel@transgaming.comdfd57022011-05-11 15:37:25 +0000799void Context::setActiveSampler(unsigned int active)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000800{
801 mState.activeSampler = active;
802}
803
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000804GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000805{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000806 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000807}
808
809GLuint Context::getDrawFramebufferHandle() const
810{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000811 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000812}
813
814GLuint Context::getRenderbufferHandle() const
815{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000816 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000817}
818
819GLuint Context::getArrayBufferHandle() const
820{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000821 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000822}
823
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000824GLuint Context::getActiveQuery(GLenum target) const
825{
826 Query *queryObject = NULL;
827
828 switch (target)
829 {
830 case GL_ANY_SAMPLES_PASSED_EXT:
831 queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED].get();
832 break;
833 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
834 queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE].get();
835 break;
836 default:
837 ASSERT(false);
838 }
839
840 if (queryObject)
841 {
842 return queryObject->id();
843 }
844 else
845 {
846 return 0;
847 }
848}
849
daniel@transgaming.com83921382011-01-08 05:46:00 +0000850void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000851{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000852 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000853}
854
daniel@transgaming.com83921382011-01-08 05:46:00 +0000855const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000856{
857 return mState.vertexAttribute[attribNum];
858}
859
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000860void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000861 GLsizei stride, const void *pointer)
862{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000863 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000864 mState.vertexAttribute[attribNum].mSize = size;
865 mState.vertexAttribute[attribNum].mType = type;
866 mState.vertexAttribute[attribNum].mNormalized = normalized;
867 mState.vertexAttribute[attribNum].mStride = stride;
868 mState.vertexAttribute[attribNum].mPointer = pointer;
869}
870
871const void *Context::getVertexAttribPointer(unsigned int attribNum) const
872{
873 return mState.vertexAttribute[attribNum].mPointer;
874}
875
daniel@transgaming.com83921382011-01-08 05:46:00 +0000876const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000877{
878 return mState.vertexAttribute;
879}
880
881void Context::setPackAlignment(GLint alignment)
882{
883 mState.packAlignment = alignment;
884}
885
886GLint Context::getPackAlignment() const
887{
888 return mState.packAlignment;
889}
890
891void Context::setUnpackAlignment(GLint alignment)
892{
893 mState.unpackAlignment = alignment;
894}
895
896GLint Context::getUnpackAlignment() const
897{
898 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000899}
900
bsalomon@google.com56d46ab2011-11-23 14:53:10 +0000901void Context::setPackReverseRowOrder(bool reverseRowOrder)
902{
903 mState.packReverseRowOrder = reverseRowOrder;
904}
905
906bool Context::getPackReverseRowOrder() const
907{
908 return mState.packReverseRowOrder;
909}
910
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000911GLuint Context::createBuffer()
912{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000913 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000914}
915
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000916GLuint Context::createProgram()
917{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000918 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000919}
920
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000921GLuint Context::createShader(GLenum type)
922{
923 return mResourceManager->createShader(type);
924}
925
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000926GLuint Context::createTexture()
927{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000928 return mResourceManager->createTexture();
929}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000930
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000931GLuint Context::createRenderbuffer()
932{
933 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000934}
935
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000936// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000937GLuint Context::createFramebuffer()
938{
benvanik@google.com1a233342011-04-28 19:44:39 +0000939 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000940
941 mFramebufferMap[handle] = NULL;
942
943 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000944}
945
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000946GLuint Context::createFence()
947{
benvanik@google.com1a233342011-04-28 19:44:39 +0000948 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000949
apatrick@chromium.org563c0a52012-03-23 21:18:42 +0000950 mFenceMap[handle] = new Fence(mDisplay);
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000951
952 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000953}
954
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000955// Returns an unused query name
956GLuint Context::createQuery()
957{
958 GLuint handle = mQueryHandleAllocator.allocate();
959
960 mQueryMap[handle] = NULL;
961
962 return handle;
963}
964
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000965void Context::deleteBuffer(GLuint buffer)
966{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000967 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000968 {
969 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000971
972 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973}
974
975void Context::deleteShader(GLuint shader)
976{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000977 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000978}
979
980void Context::deleteProgram(GLuint program)
981{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000982 mResourceManager->deleteProgram(program);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000983 mCachedCurrentProgram = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000984}
985
986void Context::deleteTexture(GLuint texture)
987{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000988 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000989 {
990 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000991 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000992
993 mResourceManager->deleteTexture(texture);
994}
995
996void Context::deleteRenderbuffer(GLuint renderbuffer)
997{
998 if (mResourceManager->getRenderbuffer(renderbuffer))
999 {
1000 detachRenderbuffer(renderbuffer);
1001 }
1002
1003 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001004}
1005
1006void Context::deleteFramebuffer(GLuint framebuffer)
1007{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001008 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
1009
1010 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001011 {
1012 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +00001013
benvanik@google.com1a233342011-04-28 19:44:39 +00001014 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001015 delete framebufferObject->second;
1016 mFramebufferMap.erase(framebufferObject);
1017 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001018}
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001019
1020void Context::deleteFence(GLuint fence)
1021{
1022 FenceMap::iterator fenceObject = mFenceMap.find(fence);
1023
1024 if (fenceObject != mFenceMap.end())
1025 {
benvanik@google.com1a233342011-04-28 19:44:39 +00001026 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001027 delete fenceObject->second;
1028 mFenceMap.erase(fenceObject);
1029 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001030}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001031
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001032void Context::deleteQuery(GLuint query)
1033{
1034 QueryMap::iterator queryObject = mQueryMap.find(query);
1035 if (queryObject != mQueryMap.end())
1036 {
1037 mQueryHandleAllocator.release(queryObject->first);
1038 if (queryObject->second)
1039 {
1040 queryObject->second->release();
1041 }
1042 mQueryMap.erase(queryObject);
1043 }
1044}
1045
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001046Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001047{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001048 return mResourceManager->getBuffer(handle);
1049}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001050
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001051Shader *Context::getShader(GLuint handle)
1052{
1053 return mResourceManager->getShader(handle);
1054}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001055
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001056Program *Context::getProgram(GLuint handle)
1057{
1058 return mResourceManager->getProgram(handle);
1059}
1060
1061Texture *Context::getTexture(GLuint handle)
1062{
1063 return mResourceManager->getTexture(handle);
1064}
1065
1066Renderbuffer *Context::getRenderbuffer(GLuint handle)
1067{
1068 return mResourceManager->getRenderbuffer(handle);
1069}
1070
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001071Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001072{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001073 return getFramebuffer(mState.readFramebuffer);
1074}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001075
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001076Framebuffer *Context::getDrawFramebuffer()
1077{
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001078 return mBoundDrawFramebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001079}
1080
1081void Context::bindArrayBuffer(unsigned int buffer)
1082{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001083 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001084
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001085 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001086}
1087
1088void Context::bindElementArrayBuffer(unsigned int buffer)
1089{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001090 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001091
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001092 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001093}
1094
1095void Context::bindTexture2D(GLuint texture)
1096{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001097 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001098
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001099 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001100}
1101
1102void Context::bindTextureCubeMap(GLuint texture)
1103{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001104 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001105
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001106 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001107}
1108
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001109void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001110{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001111 if (!getFramebuffer(framebuffer))
1112 {
1113 mFramebufferMap[framebuffer] = new Framebuffer();
1114 }
1115
1116 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001117}
1118
1119void Context::bindDrawFramebuffer(GLuint framebuffer)
1120{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001121 if (!getFramebuffer(framebuffer))
1122 {
1123 mFramebufferMap[framebuffer] = new Framebuffer();
1124 }
1125
1126 mState.drawFramebuffer = framebuffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001127
1128 mBoundDrawFramebuffer = getFramebuffer(framebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001129}
1130
1131void Context::bindRenderbuffer(GLuint renderbuffer)
1132{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001133 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1134
1135 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001136}
1137
1138void Context::useProgram(GLuint program)
1139{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001140 GLuint priorProgram = mState.currentProgram;
1141 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001142
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001143 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001144 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001145 Program *newProgram = mResourceManager->getProgram(program);
1146 Program *oldProgram = mResourceManager->getProgram(priorProgram);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001147 mCachedCurrentProgram = NULL;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001148 mDxUniformsDirty = true;
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001149
1150 if (newProgram)
1151 {
1152 newProgram->addRef();
1153 }
1154
1155 if (oldProgram)
1156 {
1157 oldProgram->release();
1158 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001159 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001160}
1161
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001162void Context::beginQuery(GLenum target, GLuint query)
1163{
1164 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
1165 // of zero, if the active query object name for <target> is non-zero (for the
1166 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
1167 // the active query for either target is non-zero), if <id> is the name of an
1168 // existing query object whose type does not match <target>, or if <id> is the
1169 // active query object name for any query type, the error INVALID_OPERATION is
1170 // generated.
1171
1172 // Ensure no other queries are active
1173 // NOTE: If other queries than occlusion are supported, we will need to check
1174 // separately that:
1175 // a) The query ID passed is not the current active query for any target/type
1176 // b) There are no active queries for the requested target (and in the case
1177 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
1178 // no query may be active for either if glBeginQuery targets either.
1179 for (int i = 0; i < QUERY_TYPE_COUNT; i++)
1180 {
1181 if (mState.activeQuery[i].get() != NULL)
1182 {
1183 return error(GL_INVALID_OPERATION);
1184 }
1185 }
1186
1187 QueryType qType;
1188 switch (target)
1189 {
1190 case GL_ANY_SAMPLES_PASSED_EXT:
1191 qType = QUERY_ANY_SAMPLES_PASSED;
1192 break;
1193 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
1194 qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
1195 break;
1196 default:
1197 ASSERT(false);
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00001198 return;
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001199 }
1200
1201 Query *queryObject = getQuery(query, true, target);
1202
1203 // check that name was obtained with glGenQueries
1204 if (!queryObject)
1205 {
1206 return error(GL_INVALID_OPERATION);
1207 }
1208
1209 // check for type mismatch
1210 if (queryObject->getType() != target)
1211 {
1212 return error(GL_INVALID_OPERATION);
1213 }
1214
1215 // set query as active for specified target
1216 mState.activeQuery[qType].set(queryObject);
1217
1218 // begin query
1219 queryObject->begin();
1220}
1221
1222void Context::endQuery(GLenum target)
1223{
1224 QueryType qType;
1225
1226 switch (target)
1227 {
1228 case GL_ANY_SAMPLES_PASSED_EXT:
1229 qType = QUERY_ANY_SAMPLES_PASSED;
1230 break;
1231 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
1232 qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
1233 break;
1234 default:
1235 ASSERT(false);
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00001236 return;
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001237 }
1238
1239 Query *queryObject = mState.activeQuery[qType].get();
1240
1241 if (queryObject == NULL)
1242 {
1243 return error(GL_INVALID_OPERATION);
1244 }
1245
1246 queryObject->end();
1247
1248 mState.activeQuery[qType].set(NULL);
1249}
1250
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001251void Context::setFramebufferZero(Framebuffer *buffer)
1252{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001253 delete mFramebufferMap[0];
1254 mFramebufferMap[0] = buffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001255 if (mState.drawFramebuffer == 0)
1256 {
1257 mBoundDrawFramebuffer = buffer;
1258 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001259}
1260
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001261void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001262{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001263 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1264 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001265}
1266
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001267Framebuffer *Context::getFramebuffer(unsigned int handle)
1268{
1269 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1270
1271 if (framebuffer == mFramebufferMap.end())
1272 {
1273 return NULL;
1274 }
1275 else
1276 {
1277 return framebuffer->second;
1278 }
1279}
1280
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001281Fence *Context::getFence(unsigned int handle)
1282{
1283 FenceMap::iterator fence = mFenceMap.find(handle);
1284
1285 if (fence == mFenceMap.end())
1286 {
1287 return NULL;
1288 }
1289 else
1290 {
1291 return fence->second;
1292 }
1293}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001294
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001295Query *Context::getQuery(unsigned int handle, bool create, GLenum type)
1296{
1297 QueryMap::iterator query = mQueryMap.find(handle);
1298
1299 if (query == mQueryMap.end())
1300 {
1301 return NULL;
1302 }
1303 else
1304 {
1305 if (!query->second && create)
1306 {
1307 query->second = new Query(handle, type);
1308 query->second->addRef();
1309 }
1310 return query->second;
1311 }
1312}
1313
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001314Buffer *Context::getArrayBuffer()
1315{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001316 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001317}
1318
1319Buffer *Context::getElementArrayBuffer()
1320{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001321 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001322}
1323
1324Program *Context::getCurrentProgram()
1325{
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001326 if (!mCachedCurrentProgram)
1327 {
1328 mCachedCurrentProgram = mResourceManager->getProgram(mState.currentProgram);
1329 }
1330 return mCachedCurrentProgram;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001331}
1332
1333Texture2D *Context::getTexture2D()
1334{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001335 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001336}
1337
1338TextureCubeMap *Context::getTextureCubeMap()
1339{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001340 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001341}
1342
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001343Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001344{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001345 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001346
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001347 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001348 {
1349 switch (type)
1350 {
1351 default: UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001352 case TEXTURE_2D: return mTexture2DZero.get();
1353 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001354 }
1355 }
1356
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001357 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001358}
1359
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001360bool Context::getBooleanv(GLenum pname, GLboolean *params)
1361{
1362 switch (pname)
1363 {
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001364 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1365 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1366 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001367 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001368 params[0] = mState.colorMaskRed;
1369 params[1] = mState.colorMaskGreen;
1370 params[2] = mState.colorMaskBlue;
1371 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001372 break;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001373 case GL_CULL_FACE: *params = mState.cullFace; break;
1374 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1375 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1376 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1377 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1378 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1379 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1380 case GL_BLEND: *params = mState.blend; break;
1381 case GL_DITHER: *params = mState.dither; break;
1382 case GL_CONTEXT_ROBUST_ACCESS_EXT: *params = mRobustAccess ? GL_TRUE : GL_FALSE; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001383 default:
1384 return false;
1385 }
1386
1387 return true;
1388}
1389
1390bool Context::getFloatv(GLenum pname, GLfloat *params)
1391{
1392 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1393 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1394 // GetIntegerv as its native query function. As it would require conversion in any
1395 // case, this should make no difference to the calling application.
1396 switch (pname)
1397 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001398 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1399 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1400 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1401 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1402 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001403 case GL_ALIASED_LINE_WIDTH_RANGE:
1404 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1405 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1406 break;
1407 case GL_ALIASED_POINT_SIZE_RANGE:
1408 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001409 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 +00001410 break;
1411 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001412 params[0] = mState.zNear;
1413 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001414 break;
1415 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001416 params[0] = mState.colorClearValue.red;
1417 params[1] = mState.colorClearValue.green;
1418 params[2] = mState.colorClearValue.blue;
1419 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001420 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001421 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001422 params[0] = mState.blendColor.red;
1423 params[1] = mState.blendColor.green;
1424 params[2] = mState.blendColor.blue;
1425 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001426 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001427 default:
1428 return false;
1429 }
1430
1431 return true;
1432}
1433
1434bool Context::getIntegerv(GLenum pname, GLint *params)
1435{
1436 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1437 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1438 // GetIntegerv as its native query function. As it would require conversion in any
1439 // case, this should make no difference to the calling application. You may find it in
1440 // Context::getFloatv.
1441 switch (pname)
1442 {
1443 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1444 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001445 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00001446 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1447 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001448 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001449 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001450 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001451 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001452 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001453 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1454 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001455 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1456 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1457 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001458 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001459 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1460 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00001461 case GL_PACK_REVERSE_ROW_ORDER_ANGLE: *params = mState.packReverseRowOrder; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001462 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1463 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001464 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001465 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1466 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1467 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1468 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1469 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1470 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1471 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1472 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1473 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1474 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1475 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1476 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1477 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1478 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1479 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1480 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1481 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1482 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1483 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1484 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1485 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1486 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1487 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1488 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001489 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1490 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001491 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
gman@chromium.org50c526d2011-08-10 05:19:44 +00001492 params[0] = mNumCompressedTextureFormats;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001493 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001494 case GL_MAX_SAMPLES_ANGLE:
1495 {
1496 GLsizei maxSamples = getMaxSupportedSamples();
1497 if (maxSamples != 0)
1498 {
1499 *params = maxSamples;
1500 }
1501 else
1502 {
1503 return false;
1504 }
1505
1506 break;
1507 }
1508 case GL_SAMPLE_BUFFERS:
1509 case GL_SAMPLES:
1510 {
1511 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1512 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1513 {
1514 switch (pname)
1515 {
1516 case GL_SAMPLE_BUFFERS:
1517 if (framebuffer->getSamples() != 0)
1518 {
1519 *params = 1;
1520 }
1521 else
1522 {
1523 *params = 0;
1524 }
1525 break;
1526 case GL_SAMPLES:
1527 *params = framebuffer->getSamples();
1528 break;
1529 }
1530 }
1531 else
1532 {
1533 *params = 0;
1534 }
1535 }
1536 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001537 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1538 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1539 case GL_MAX_VIEWPORT_DIMS:
1540 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001541 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001542 params[0] = maxDimension;
1543 params[1] = maxDimension;
1544 }
1545 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001546 case GL_COMPRESSED_TEXTURE_FORMATS:
1547 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001548 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00001549 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001550 *params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1551 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1552 }
1553 if (supportsDXT3Textures())
1554 {
1555 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
1556 }
1557 if (supportsDXT5Textures())
1558 {
1559 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001560 }
1561 }
1562 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001563 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001564 params[0] = mState.viewportX;
1565 params[1] = mState.viewportY;
1566 params[2] = mState.viewportWidth;
1567 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001568 break;
1569 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001570 params[0] = mState.scissorX;
1571 params[1] = mState.scissorY;
1572 params[2] = mState.scissorWidth;
1573 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001574 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001575 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1576 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001577 case GL_RED_BITS:
1578 case GL_GREEN_BITS:
1579 case GL_BLUE_BITS:
1580 case GL_ALPHA_BITS:
1581 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001582 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001583 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001584
1585 if (colorbuffer)
1586 {
1587 switch (pname)
1588 {
1589 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1590 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1591 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1592 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1593 }
1594 }
1595 else
1596 {
1597 *params = 0;
1598 }
1599 }
1600 break;
1601 case GL_DEPTH_BITS:
1602 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001603 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001604 gl::Renderbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001605
1606 if (depthbuffer)
1607 {
1608 *params = depthbuffer->getDepthSize();
1609 }
1610 else
1611 {
1612 *params = 0;
1613 }
1614 }
1615 break;
1616 case GL_STENCIL_BITS:
1617 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001618 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001619 gl::Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001620
1621 if (stencilbuffer)
1622 {
1623 *params = stencilbuffer->getStencilSize();
1624 }
1625 else
1626 {
1627 *params = 0;
1628 }
1629 }
1630 break;
1631 case GL_TEXTURE_BINDING_2D:
1632 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001633 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001634 {
1635 error(GL_INVALID_OPERATION);
1636 return false;
1637 }
1638
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001639 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001640 }
1641 break;
1642 case GL_TEXTURE_BINDING_CUBE_MAP:
1643 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001644 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001645 {
1646 error(GL_INVALID_OPERATION);
1647 return false;
1648 }
1649
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001650 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001651 }
1652 break;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001653 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
1654 *params = mResetStrategy;
1655 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001656 default:
1657 return false;
1658 }
1659
1660 return true;
1661}
1662
1663bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1664{
1665 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1666 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1667 // to the fact that it is stored internally as a float, and so would require conversion
1668 // if returned from Context::getIntegerv. Since this conversion is already implemented
1669 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1670 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1671 // application.
1672 switch (pname)
1673 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001674 case GL_COMPRESSED_TEXTURE_FORMATS:
1675 {
1676 *type = GL_INT;
1677 *numParams = mNumCompressedTextureFormats;
1678 }
1679 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001680 case GL_SHADER_BINARY_FORMATS:
1681 {
1682 *type = GL_INT;
1683 *numParams = 0;
1684 }
1685 break;
1686 case GL_MAX_VERTEX_ATTRIBS:
1687 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1688 case GL_MAX_VARYING_VECTORS:
1689 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1690 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1691 case GL_MAX_TEXTURE_IMAGE_UNITS:
1692 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1693 case GL_MAX_RENDERBUFFER_SIZE:
1694 case GL_NUM_SHADER_BINARY_FORMATS:
1695 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1696 case GL_ARRAY_BUFFER_BINDING:
1697 case GL_FRAMEBUFFER_BINDING:
1698 case GL_RENDERBUFFER_BINDING:
1699 case GL_CURRENT_PROGRAM:
1700 case GL_PACK_ALIGNMENT:
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00001701 case GL_PACK_REVERSE_ROW_ORDER_ANGLE:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001702 case GL_UNPACK_ALIGNMENT:
1703 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001704 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001705 case GL_RED_BITS:
1706 case GL_GREEN_BITS:
1707 case GL_BLUE_BITS:
1708 case GL_ALPHA_BITS:
1709 case GL_DEPTH_BITS:
1710 case GL_STENCIL_BITS:
1711 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1712 case GL_CULL_FACE_MODE:
1713 case GL_FRONT_FACE:
1714 case GL_ACTIVE_TEXTURE:
1715 case GL_STENCIL_FUNC:
1716 case GL_STENCIL_VALUE_MASK:
1717 case GL_STENCIL_REF:
1718 case GL_STENCIL_FAIL:
1719 case GL_STENCIL_PASS_DEPTH_FAIL:
1720 case GL_STENCIL_PASS_DEPTH_PASS:
1721 case GL_STENCIL_BACK_FUNC:
1722 case GL_STENCIL_BACK_VALUE_MASK:
1723 case GL_STENCIL_BACK_REF:
1724 case GL_STENCIL_BACK_FAIL:
1725 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1726 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1727 case GL_DEPTH_FUNC:
1728 case GL_BLEND_SRC_RGB:
1729 case GL_BLEND_SRC_ALPHA:
1730 case GL_BLEND_DST_RGB:
1731 case GL_BLEND_DST_ALPHA:
1732 case GL_BLEND_EQUATION_RGB:
1733 case GL_BLEND_EQUATION_ALPHA:
1734 case GL_STENCIL_WRITEMASK:
1735 case GL_STENCIL_BACK_WRITEMASK:
1736 case GL_STENCIL_CLEAR_VALUE:
1737 case GL_SUBPIXEL_BITS:
1738 case GL_MAX_TEXTURE_SIZE:
1739 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1740 case GL_SAMPLE_BUFFERS:
1741 case GL_SAMPLES:
1742 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1743 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1744 case GL_TEXTURE_BINDING_2D:
1745 case GL_TEXTURE_BINDING_CUBE_MAP:
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001746 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001747 {
1748 *type = GL_INT;
1749 *numParams = 1;
1750 }
1751 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001752 case GL_MAX_SAMPLES_ANGLE:
1753 {
1754 if (getMaxSupportedSamples() != 0)
1755 {
1756 *type = GL_INT;
1757 *numParams = 1;
1758 }
1759 else
1760 {
1761 return false;
1762 }
1763 }
1764 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001765 case GL_MAX_VIEWPORT_DIMS:
1766 {
1767 *type = GL_INT;
1768 *numParams = 2;
1769 }
1770 break;
1771 case GL_VIEWPORT:
1772 case GL_SCISSOR_BOX:
1773 {
1774 *type = GL_INT;
1775 *numParams = 4;
1776 }
1777 break;
1778 case GL_SHADER_COMPILER:
1779 case GL_SAMPLE_COVERAGE_INVERT:
1780 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001781 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1782 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1783 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1784 case GL_SAMPLE_COVERAGE:
1785 case GL_SCISSOR_TEST:
1786 case GL_STENCIL_TEST:
1787 case GL_DEPTH_TEST:
1788 case GL_BLEND:
1789 case GL_DITHER:
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001790 case GL_CONTEXT_ROBUST_ACCESS_EXT:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001791 {
1792 *type = GL_BOOL;
1793 *numParams = 1;
1794 }
1795 break;
1796 case GL_COLOR_WRITEMASK:
1797 {
1798 *type = GL_BOOL;
1799 *numParams = 4;
1800 }
1801 break;
1802 case GL_POLYGON_OFFSET_FACTOR:
1803 case GL_POLYGON_OFFSET_UNITS:
1804 case GL_SAMPLE_COVERAGE_VALUE:
1805 case GL_DEPTH_CLEAR_VALUE:
1806 case GL_LINE_WIDTH:
1807 {
1808 *type = GL_FLOAT;
1809 *numParams = 1;
1810 }
1811 break;
1812 case GL_ALIASED_LINE_WIDTH_RANGE:
1813 case GL_ALIASED_POINT_SIZE_RANGE:
1814 case GL_DEPTH_RANGE:
1815 {
1816 *type = GL_FLOAT;
1817 *numParams = 2;
1818 }
1819 break;
1820 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001821 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001822 {
1823 *type = GL_FLOAT;
1824 *numParams = 4;
1825 }
1826 break;
1827 default:
1828 return false;
1829 }
1830
1831 return true;
1832}
1833
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001834// Applies the render target surface, depth stencil surface, viewport rectangle and
1835// scissor rectangle to the Direct3D 9 device
1836bool Context::applyRenderTarget(bool ignoreViewport)
1837{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001838 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001839
1840 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1841 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001842 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001843 }
1844
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001845 // if there is no color attachment we must synthesize a NULL colorattachment
1846 // to keep the D3D runtime happy. This should only be possible if depth texturing.
1847 Renderbuffer *renderbufferObject = NULL;
1848 if (framebufferObject->getColorbufferType() != GL_NONE)
1849 {
1850 renderbufferObject = framebufferObject->getColorbuffer();
1851 }
1852 else
1853 {
1854 renderbufferObject = framebufferObject->getNullColorbuffer();
1855 }
1856 if (!renderbufferObject)
1857 {
1858 ERR("unable to locate renderbuffer for FBO.");
1859 return false;
1860 }
1861
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001862 bool renderTargetChanged = false;
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001863 unsigned int renderTargetSerial = renderbufferObject->getSerial();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001864 if (renderTargetSerial != mAppliedRenderTargetSerial)
1865 {
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001866 IDirect3DSurface9 *renderTarget = renderbufferObject->getRenderTarget();
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001867 if (!renderTarget)
1868 {
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001869 ERR("render target pointer unexpectedly null.");
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001870 return false; // Context must be lost
1871 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001872 mDevice->SetRenderTarget(0, renderTarget);
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001873 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001874 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 +00001875 renderTargetChanged = true;
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001876 renderTarget->Release();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001877 }
1878
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001879 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001880 unsigned int depthbufferSerial = 0;
1881 unsigned int stencilbufferSerial = 0;
1882 if (framebufferObject->getDepthbufferType() != GL_NONE)
1883 {
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001884 Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
1885 depthStencil = depthbuffer->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001886 if (!depthStencil)
1887 {
1888 ERR("Depth stencil pointer unexpectedly null.");
1889 return false;
1890 }
1891
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001892 depthbufferSerial = depthbuffer->getSerial();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001893 }
1894 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1895 {
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001896 Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
1897 depthStencil = stencilbuffer->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001898 if (!depthStencil)
1899 {
1900 ERR("Depth stencil pointer unexpectedly null.");
1901 return false;
1902 }
1903
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001904 stencilbufferSerial = stencilbuffer->getSerial();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001905 }
1906
1907 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001908 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001909 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001910 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001911 mDevice->SetDepthStencilSurface(depthStencil);
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001912 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001913 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001914 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001915 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001916
daniel@transgaming.com63e6afe2012-05-31 01:14:42 +00001917 if (depthStencil)
1918 {
1919 depthStencil->Release();
1920 }
1921
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001922 if (!mRenderTargetDescInitialized || renderTargetChanged)
1923 {
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001924 IDirect3DSurface9 *renderTarget = renderbufferObject->getRenderTarget();
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001925 if (!renderTarget)
1926 {
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001927 return false; // Context must be lost
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001928 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001929 renderTarget->GetDesc(&mRenderTargetDesc);
1930 mRenderTargetDescInitialized = true;
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001931 renderTarget->Release();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001932 }
1933
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001934 D3DVIEWPORT9 viewport;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001935
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001936 float zNear = clamp01(mState.zNear);
1937 float zFar = clamp01(mState.zFar);
1938
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001939 if (ignoreViewport)
1940 {
1941 viewport.X = 0;
1942 viewport.Y = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001943 viewport.Width = mRenderTargetDesc.Width;
1944 viewport.Height = mRenderTargetDesc.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001945 viewport.MinZ = 0.0f;
1946 viewport.MaxZ = 1.0f;
1947 }
1948 else
1949 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001950 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, mRenderTargetDesc.Height);
1951 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1952 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1953 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width) - static_cast<LONG>(viewport.X));
1954 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 +00001955 viewport.MinZ = zNear;
1956 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001957 }
1958
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001959 if (viewport.Width <= 0 || viewport.Height <= 0)
1960 {
1961 return false; // Nothing to render
1962 }
1963
jbauman@chromium.org241e70d2011-11-03 23:07:05 +00001964 if (renderTargetChanged || !mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001965 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001966 mDevice->SetViewport(&viewport);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001967 mSetViewport = viewport;
1968 mViewportInitialized = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001969 mDxUniformsDirty = true;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001970 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001971
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001972 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001973 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001974 if (mState.scissorTest)
1975 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001976 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, mRenderTargetDesc.Height);
1977 rect.left = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1978 rect.top = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1979 rect.right = clamp(rect.right, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1980 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001981 mDevice->SetScissorRect(&rect);
1982 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001983 }
1984 else
1985 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001986 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001987 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001988
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001989 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001990 }
1991
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001992 if (mState.currentProgram && mDxUniformsDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001993 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001994 Program *programObject = getCurrentProgram();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001995 ProgramBinary *programBinary = programObject->getProgramBinary();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001996
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001997 GLint halfPixelSize = programBinary->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001998 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00001999 programBinary->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00002000
daniel@transgaming.comd9a54f92011-12-22 18:32:53 +00002001 // These values are used for computing gl_FragCoord in Program::linkVaryings(). The approach depends on Shader Model 3.0 support.
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002002 GLint coord = programBinary->getDxCoordLocation();
daniel@transgaming.comd9a54f92011-12-22 18:32:53 +00002003 float h = mSupportsShaderModel3 ? mRenderTargetDesc.Height : mState.viewportHeight / 2.0f;
2004 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, h,
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002005 (float)mState.viewportX + mState.viewportWidth / 2.0f,
2006 (float)mState.viewportY + mState.viewportHeight / 2.0f};
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002007 programBinary->setUniform4fv(coord, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00002008
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002009 GLint depth = programBinary->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00002010 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002011 programBinary->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00002012
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002013 GLint depthRange = programBinary->getDxDepthRangeLocation();
daniel@transgaming.com31754962010-11-28 02:02:52 +00002014 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002015 programBinary->setUniform3fv(depthRange, 1, nearFarDiff);
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00002016 mDxUniformsDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002017 }
2018
2019 return true;
2020}
2021
2022// 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 +00002023void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002024{
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00002025 Program *programObject = getCurrentProgram();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002026 ProgramBinary *programBinary = programObject->getProgramBinary();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00002027
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002028 Framebuffer *framebufferObject = getDrawFramebuffer();
2029
2030 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
2031
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002032 GLint frontCCW = programBinary->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002033 GLint ccw = (adjustedFrontFace == GL_CCW);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002034 programBinary->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002035
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002036 GLint pointsOrLines = programBinary->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002037 GLint alwaysFront = !isTriangleMode(drawMode);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002038 programBinary->setUniform1iv(pointsOrLines, 1, &alwaysFront);
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002039
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002040 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002041 bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
2042 // Apparently some ATI cards have a bug where a draw with a zero color
2043 // write mask can cause later draws to have incorrect results. Instead,
2044 // set a nonzero color write mask but modify the blend state so that no
2045 // drawing is done.
2046 // http://code.google.com/p/angleproject/issues/detail?id=169
2047
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002048 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002049 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002050 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002051 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002052 mDevice->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002053 }
2054 else
2055 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002056 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002057 }
2058
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002059 mCullStateDirty = false;
2060 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002061
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002062 if (mDepthStateDirty)
2063 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00002064 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002065 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002066 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
2067 mDevice->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002068 }
2069 else
2070 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002071 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002072 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002073
2074 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002075 }
2076
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002077 if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
2078 {
2079 mBlendStateDirty = true;
2080 mMaskStateDirty = true;
2081 }
2082
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002083 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002084 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002085 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00002086 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002087 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002088
2089 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
2090 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
2091 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002092 mDevice->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002093 }
2094 else
2095 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002096 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002097 unorm<8>(mState.blendColor.alpha),
2098 unorm<8>(mState.blendColor.alpha),
2099 unorm<8>(mState.blendColor.alpha)));
2100 }
2101
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002102 mDevice->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
2103 mDevice->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
2104 mDevice->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002105
2106 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
2107 mState.destBlendRGB != mState.destBlendAlpha ||
2108 mState.blendEquationRGB != mState.blendEquationAlpha)
2109 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002110 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002111
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002112 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
2113 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
2114 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002115 }
2116 else
2117 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002118 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002119 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00002120 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002121 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00002122 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002123 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00002124 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002125
2126 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002127 }
2128
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002129 if (mStencilStateDirty || mFrontFaceDirty)
2130 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002131 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002132 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002133 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2134 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002135
2136 // FIXME: Unsupported by D3D9
2137 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
2138 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
2139 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
2140 if (mState.stencilWritemask != mState.stencilBackWritemask ||
2141 mState.stencilRef != mState.stencilBackRef ||
2142 mState.stencilMask != mState.stencilBackMask)
2143 {
2144 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
2145 return error(GL_INVALID_OPERATION);
2146 }
2147
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00002148 // get the maximum size of the stencil ref
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00002149 gl::Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00002150 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
2151
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002152 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
2153 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002154 es2dx::ConvertComparison(mState.stencilFunc));
2155
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002156 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
2157 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002158
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002159 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002160 es2dx::ConvertStencilOp(mState.stencilFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002161 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002162 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002163 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002164 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
2165
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002166 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
2167 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002168 es2dx::ConvertComparison(mState.stencilBackFunc));
2169
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002170 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
2171 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002172
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002173 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002174 es2dx::ConvertStencilOp(mState.stencilBackFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002175 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002176 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002177 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002178 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
2179 }
2180 else
2181 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002182 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002183 }
2184
2185 mStencilStateDirty = false;
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002186 mFrontFaceDirty = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002187 }
2188
2189 if (mMaskStateDirty)
2190 {
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002191 int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
2192 mState.colorMaskBlue, mState.colorMaskAlpha);
2193 if (colorMask == 0 && !zeroColorMaskAllowed)
2194 {
2195 // Enable green channel, but set blending so nothing will be drawn.
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002196 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
2197 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002198
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002199 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
2200 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
2201 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002202 }
2203 else
2204 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002205 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002206 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002207 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002208
2209 mMaskStateDirty = false;
2210 }
2211
2212 if (mPolygonOffsetStateDirty)
2213 {
2214 if (mState.polygonOffsetFill)
2215 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00002216 gl::Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002217 if (depthbuffer)
2218 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002219 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002220 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002221 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002222 }
2223 }
2224 else
2225 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002226 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
2227 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002228 }
2229
2230 mPolygonOffsetStateDirty = false;
2231 }
2232
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002233 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002234 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002235 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002236 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002237 FIXME("Sample alpha to coverage is unimplemented.");
2238 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002239
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002240 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002241 if (mState.sampleCoverage)
2242 {
2243 unsigned int mask = 0;
2244 if (mState.sampleCoverageValue != 0)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002245 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002246 float threshold = 0.5f;
2247
2248 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002249 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002250 mask <<= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002251
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002252 if ((i + 1) * mState.sampleCoverageValue >= threshold)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002253 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002254 threshold += 1.0f;
2255 mask |= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002256 }
2257 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002258 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002259
2260 if (mState.sampleCoverageInvert)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002261 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002262 mask = ~mask;
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002263 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002264
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002265 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002266 }
2267 else
2268 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002269 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002270 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002271
2272 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002273 }
2274
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002275 if (mDitherStateDirty)
2276 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002277 mDevice->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002278
2279 mDitherStateDirty = false;
2280 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002281}
2282
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002283GLenum Context::applyVertexBuffer(GLint first, GLsizei count, GLsizei instances, GLsizei *repeatDraw)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002284{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002285 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002286
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +00002287 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes, instances);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002288 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002289 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002290 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002291 }
2292
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002293 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, getCurrentProgram(), instances, repeatDraw);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002294}
2295
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002296// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.comd2820bf2012-01-27 15:38:48 +00002297GLenum Context::applyIndexBuffer(const GLvoid *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002298{
daniel@transgaming.com83921382011-01-08 05:46:00 +00002299 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002300
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002301 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002302 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002303 if (indexInfo->serial != mAppliedIBSerial)
2304 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002305 mDevice->SetIndices(indexInfo->indexBuffer);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002306 mAppliedIBSerial = indexInfo->serial;
2307 }
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002308 }
2309
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002310 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002311}
2312
2313// Applies the shaders and shader constants to the Direct3D 9 device
2314void Context::applyShaders()
2315{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002316 Program *programObject = getCurrentProgram();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002317 ProgramBinary *programBinary = programObject->getProgramBinary();
2318
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002319 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002320 {
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002321 IDirect3DVertexShader9 *vertexShader = programBinary->getVertexShader();
2322 IDirect3DPixelShader9 *pixelShader = programBinary->getPixelShader();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002323
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002324 mDevice->SetPixelShader(pixelShader);
2325 mDevice->SetVertexShader(vertexShader);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002326 programBinary->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002327 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002328 }
2329
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002330 programBinary->applyUniforms();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002331}
2332
2333// Applies the textures and sampler states to the Direct3D 9 device
2334void Context::applyTextures()
2335{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002336 applyTextures(SAMPLER_PIXEL);
2337
2338 if (mSupportsVertexTexture)
2339 {
2340 applyTextures(SAMPLER_VERTEX);
2341 }
2342}
2343
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002344// For each Direct3D 9 sampler of either the pixel or vertex stage,
2345// looks up the corresponding OpenGL texture image unit and texture type,
2346// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002347void Context::applyTextures(SamplerType type)
2348{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002349 Program *programObject = getCurrentProgram();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002350 ProgramBinary *programBinary = programObject->getProgramBinary();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002351
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002352 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 +00002353 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2354 int d3dSamplerOffset = (type == SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002355 int samplerRange = programBinary->getUsedSamplerRange(type);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002356
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002357 for (int samplerIndex = 0; samplerIndex < samplerRange; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002358 {
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002359 int textureUnit = programBinary->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00002360 int d3dSampler = samplerIndex + d3dSamplerOffset;
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002361
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002362 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002363 {
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002364 TextureType textureType = programBinary->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002365
2366 Texture *texture = getSamplerTexture(textureUnit, textureType);
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002367 unsigned int texSerial = texture->getTextureSerial();
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002368
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002369 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyParameters() || texture->hasDirtyImages())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002370 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002371 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2372
2373 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002374 {
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002375 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyParameters())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002376 {
2377 GLenum wrapS = texture->getWrapS();
2378 GLenum wrapT = texture->getWrapT();
2379 GLenum minFilter = texture->getMinFilter();
2380 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002381
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002382 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2383 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002384
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002385 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002386 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2387 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002388 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2389 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002390 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002391
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002392 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyImages())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002393 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002394 mDevice->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002395 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002396 }
2397 else
2398 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002399 mDevice->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002400 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002401
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002402 appliedTextureSerial[samplerIndex] = texSerial;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002403 texture->resetDirty();
2404 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002405 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002406 else
2407 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002408 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002409 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002410 mDevice->SetTexture(d3dSampler, NULL);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002411 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002412 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002413 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002414 }
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002415
2416 for (int samplerIndex = samplerRange; samplerIndex < samplerCount; samplerIndex++)
2417 {
2418 if (appliedTextureSerial[samplerIndex] != 0)
2419 {
daniel@transgaming.comc5a7b692011-10-26 02:45:44 +00002420 mDevice->SetTexture(samplerIndex + d3dSamplerOffset, NULL);
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002421 appliedTextureSerial[samplerIndex] = 0;
2422 }
2423 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002424}
2425
daniel@transgaming.comb7915a52011-11-12 03:14:20 +00002426void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height,
2427 GLenum format, GLenum type, GLsizei *bufSize, void* pixels)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002428{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002429 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002430
2431 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2432 {
2433 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2434 }
2435
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002436 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2437 {
2438 return error(GL_INVALID_OPERATION);
2439 }
2440
daniel@transgaming.comb7915a52011-11-12 03:14:20 +00002441 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
2442 // sized query sanity check
2443 if (bufSize)
2444 {
2445 int requiredSize = outputPitch * height;
2446 if (requiredSize > *bufSize)
2447 {
2448 return error(GL_INVALID_OPERATION);
2449 }
2450 }
2451
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002452 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002453 if (!renderTarget)
2454 {
2455 return; // Context must be lost, return silently
2456 }
2457
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002458 D3DSURFACE_DESC desc;
2459 renderTarget->GetDesc(&desc);
2460
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002461 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2462 {
2463 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.coma5798952011-12-13 17:30:43 +00002464 renderTarget->Release();
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002465 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002466 }
2467
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002468 HRESULT result;
2469 IDirect3DSurface9 *systemSurface = NULL;
2470 bool directToPixels = getPackReverseRowOrder() && getPackAlignment() <= 4 && mDisplay->isD3d9ExDevice() &&
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00002471 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002472 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2473 if (directToPixels)
2474 {
2475 // Use the pixels ptr as a shared handle to write directly into client's memory
2476 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2477 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2478 if (FAILED(result))
2479 {
2480 // Try again without the shared handle
2481 directToPixels = false;
2482 }
2483 }
2484
2485 if (!directToPixels)
2486 {
2487 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2488 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2489 if (FAILED(result))
2490 {
2491 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com63e6afe2012-05-31 01:14:42 +00002492 renderTarget->Release();
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002493 return error(GL_OUT_OF_MEMORY);
2494 }
2495 }
2496
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002497 result = mDevice->GetRenderTargetData(renderTarget, systemSurface);
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00002498 renderTarget->Release();
daniel@transgaming.coma5798952011-12-13 17:30:43 +00002499 renderTarget = NULL;
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00002500
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002501 if (FAILED(result))
2502 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002503 systemSurface->Release();
2504
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002505 // It turns out that D3D will sometimes produce more error
2506 // codes than those documented.
2507 if (checkDeviceLost(result))
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002508 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002509 else
2510 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002511 UNREACHABLE();
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002512 return;
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002513 }
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002514
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002515 }
2516
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002517 if (directToPixels)
2518 {
2519 systemSurface->Release();
2520 return;
2521 }
2522
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002523 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002524 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2525 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2526 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2527 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2528 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002529
2530 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2531
2532 if (FAILED(result))
2533 {
2534 UNREACHABLE();
2535 systemSurface->Release();
2536
2537 return; // No sensible error to generate
2538 }
2539
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002540 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002541 unsigned short *dest16 = (unsigned short*)pixels;
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002542
2543 unsigned char *source;
2544 int inputPitch;
2545 if (getPackReverseRowOrder())
2546 {
2547 source = (unsigned char*)lock.pBits;
2548 inputPitch = lock.Pitch;
2549 }
2550 else
2551 {
2552 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2553 inputPitch = -lock.Pitch;
2554 }
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002555
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002556 for (int j = 0; j < rect.bottom - rect.top; j++)
2557 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002558 if (desc.Format == D3DFMT_A8R8G8B8 &&
2559 format == GL_BGRA_EXT &&
2560 type == GL_UNSIGNED_BYTE)
2561 {
2562 // Fast path for EXT_read_format_bgra, given
2563 // an RGBA source buffer. Note that buffers with no
2564 // alpha go through the slow path below.
2565 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002566 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002567 (rect.right - rect.left) * 4);
2568 continue;
2569 }
2570
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002571 for (int i = 0; i < rect.right - rect.left; i++)
2572 {
2573 float r;
2574 float g;
2575 float b;
2576 float a;
2577
2578 switch (desc.Format)
2579 {
2580 case D3DFMT_R5G6B5:
2581 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002582 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002583
2584 a = 1.0f;
2585 b = (rgb & 0x001F) * (1.0f / 0x001F);
2586 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2587 r = (rgb & 0xF800) * (1.0f / 0xF800);
2588 }
2589 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002590 case D3DFMT_A1R5G5B5:
2591 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002592 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002593
2594 a = (argb & 0x8000) ? 1.0f : 0.0f;
2595 b = (argb & 0x001F) * (1.0f / 0x001F);
2596 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2597 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2598 }
2599 break;
2600 case D3DFMT_A8R8G8B8:
2601 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002602 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002603
2604 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2605 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2606 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2607 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2608 }
2609 break;
2610 case D3DFMT_X8R8G8B8:
2611 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002612 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002613
2614 a = 1.0f;
2615 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2616 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2617 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2618 }
2619 break;
2620 case D3DFMT_A2R10G10B10:
2621 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002622 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002623
2624 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2625 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2626 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2627 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2628 }
2629 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002630 case D3DFMT_A32B32G32R32F:
2631 {
2632 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002633 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2634 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2635 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2636 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002637 }
2638 break;
2639 case D3DFMT_A16B16G16R16F:
2640 {
2641 // float formats in D3D are stored rgba, rather than the other way round
2642 float abgr[4];
2643
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002644 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002645
2646 a = abgr[3];
2647 b = abgr[2];
2648 g = abgr[1];
2649 r = abgr[0];
2650 }
2651 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002652 default:
2653 UNIMPLEMENTED(); // FIXME
2654 UNREACHABLE();
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00002655 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002656 }
2657
2658 switch (format)
2659 {
2660 case GL_RGBA:
2661 switch (type)
2662 {
2663 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002664 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2665 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2666 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2667 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002668 break;
2669 default: UNREACHABLE();
2670 }
2671 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002672 case GL_BGRA_EXT:
2673 switch (type)
2674 {
2675 case GL_UNSIGNED_BYTE:
2676 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2677 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2678 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2679 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2680 break;
2681 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2682 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2683 // this type is packed as follows:
2684 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2685 // --------------------------------------------------------------------------------
2686 // | 4th | 3rd | 2nd | 1st component |
2687 // --------------------------------------------------------------------------------
2688 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2689 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2690 ((unsigned short)(15 * a + 0.5f) << 12)|
2691 ((unsigned short)(15 * r + 0.5f) << 8) |
2692 ((unsigned short)(15 * g + 0.5f) << 4) |
2693 ((unsigned short)(15 * b + 0.5f) << 0);
2694 break;
2695 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2696 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2697 // this type is packed as follows:
2698 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2699 // --------------------------------------------------------------------------------
2700 // | 4th | 3rd | 2nd | 1st component |
2701 // --------------------------------------------------------------------------------
2702 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2703 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2704 ((unsigned short)( a + 0.5f) << 15) |
2705 ((unsigned short)(31 * r + 0.5f) << 10) |
2706 ((unsigned short)(31 * g + 0.5f) << 5) |
2707 ((unsigned short)(31 * b + 0.5f) << 0);
2708 break;
2709 default: UNREACHABLE();
2710 }
2711 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002712 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002713 switch (type)
2714 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002715 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002716 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2717 ((unsigned short)(31 * b + 0.5f) << 0) |
2718 ((unsigned short)(63 * g + 0.5f) << 5) |
2719 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002720 break;
2721 default: UNREACHABLE();
2722 }
2723 break;
2724 default: UNREACHABLE();
2725 }
2726 }
2727 }
2728
2729 systemSurface->UnlockRect();
2730
2731 systemSurface->Release();
2732}
2733
2734void Context::clear(GLbitfield mask)
2735{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002736 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002737
2738 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2739 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002740 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002741 }
2742
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002743 DWORD flags = 0;
2744
2745 if (mask & GL_COLOR_BUFFER_BIT)
2746 {
2747 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002748
2749 if (framebufferObject->getColorbufferType() != GL_NONE)
2750 {
2751 flags |= D3DCLEAR_TARGET;
2752 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002753 }
2754
2755 if (mask & GL_DEPTH_BUFFER_BIT)
2756 {
2757 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002758 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002759 {
2760 flags |= D3DCLEAR_ZBUFFER;
2761 }
2762 }
2763
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002764 GLuint stencilUnmasked = 0x0;
2765
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002766 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002767 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002768 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002769 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002770 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002771 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002772 if (!depthStencil)
2773 {
2774 ERR("Depth stencil pointer unexpectedly null.");
2775 return;
2776 }
2777
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002778 D3DSURFACE_DESC desc;
2779 depthStencil->GetDesc(&desc);
daniel@transgaming.com63e6afe2012-05-31 01:14:42 +00002780 depthStencil->Release();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002781
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002782 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002783 stencilUnmasked = (0x1 << stencilSize) - 1;
2784
2785 if (stencilUnmasked != 0x0)
2786 {
2787 flags |= D3DCLEAR_STENCIL;
2788 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002789 }
2790 }
2791
2792 if (mask != 0)
2793 {
2794 return error(GL_INVALID_VALUE);
2795 }
2796
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002797 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2798 {
2799 return;
2800 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002801
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002802 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002803 unorm<8>(mState.colorClearValue.red),
2804 unorm<8>(mState.colorClearValue.green),
2805 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002806 float depth = clamp01(mState.depthClearValue);
2807 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002808
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002809 bool alphaUnmasked = (dx2es::GetAlphaSize(mRenderTargetDesc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002810
2811 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002812 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002813 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002814 !(mState.colorMaskRed && mState.colorMaskGreen &&
2815 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002816
2817 if (needMaskedColorClear || needMaskedStencilClear)
2818 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002819 // State which is altered in all paths from this point to the clear call is saved.
2820 // State which is altered in only some paths will be flagged dirty in the case that
2821 // that path is taken.
2822 HRESULT hr;
2823 if (mMaskedClearSavedState == NULL)
2824 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002825 hr = mDevice->BeginStateBlock();
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002826 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2827
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002828 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2829 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2830 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2831 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2832 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2833 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2834 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2835 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2836 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2837 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2838 mDevice->SetPixelShader(NULL);
2839 mDevice->SetVertexShader(NULL);
2840 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2841 mDevice->SetStreamSource(0, NULL, 0, 0);
2842 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2843 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2844 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2845 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2846 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2847 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2848 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002849
2850 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2851 {
2852 mDevice->SetStreamSourceFreq(i, 1);
2853 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002854
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002855 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002856 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2857 }
2858
2859 ASSERT(mMaskedClearSavedState != NULL);
2860
2861 if (mMaskedClearSavedState != NULL)
2862 {
2863 hr = mMaskedClearSavedState->Capture();
2864 ASSERT(SUCCEEDED(hr));
2865 }
2866
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002867 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2868 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2869 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2870 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2871 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2872 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2873 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2874 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002875
2876 if (flags & D3DCLEAR_TARGET)
2877 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002878 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002879 }
2880 else
2881 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002882 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002883 }
2884
2885 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2886 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002887 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2888 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2889 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2890 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
2891 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
2892 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
2893 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2894 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002895 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002896 }
2897 else
2898 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002899 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002900 }
2901
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002902 mDevice->SetPixelShader(NULL);
2903 mDevice->SetVertexShader(NULL);
2904 mDevice->SetFVF(D3DFVF_XYZRHW);
2905 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2906 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2907 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2908 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2909 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2910 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2911 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002912
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002913 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2914 {
2915 mDevice->SetStreamSourceFreq(i, 1);
2916 }
2917
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002918 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2919 quad[0][0] = -0.5f;
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002920 quad[0][1] = mRenderTargetDesc.Height - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002921 quad[0][2] = 0.0f;
2922 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002923
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002924 quad[1][0] = mRenderTargetDesc.Width - 0.5f;
2925 quad[1][1] = mRenderTargetDesc.Height - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002926 quad[1][2] = 0.0f;
2927 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002928
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002929 quad[2][0] = -0.5f;
2930 quad[2][1] = -0.5f;
2931 quad[2][2] = 0.0f;
2932 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002933
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002934 quad[3][0] = mRenderTargetDesc.Width - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002935 quad[3][1] = -0.5f;
2936 quad[3][2] = 0.0f;
2937 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002938
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002939 mDisplay->startScene();
2940 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002941
2942 if (flags & D3DCLEAR_ZBUFFER)
2943 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002944 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
2945 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2946 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002947 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002948
2949 if (mMaskedClearSavedState != NULL)
2950 {
2951 mMaskedClearSavedState->Apply();
2952 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002953 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002954 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002955 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002956 mDevice->Clear(0, NULL, flags, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002957 }
2958}
2959
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002960void Context::drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002961{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002962 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002963 {
2964 return error(GL_INVALID_OPERATION);
2965 }
2966
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002967 D3DPRIMITIVETYPE primitiveType;
2968 int primitiveCount;
2969
2970 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2971 return error(GL_INVALID_ENUM);
2972
2973 if (primitiveCount <= 0)
2974 {
2975 return;
2976 }
2977
2978 if (!applyRenderTarget(false))
2979 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002980 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002981 }
2982
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002983 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002984
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002985 GLsizei repeatDraw = 1;
2986 GLenum err = applyVertexBuffer(first, count, instances, &repeatDraw);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002987 if (err != GL_NO_ERROR)
2988 {
2989 return error(err);
2990 }
2991
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002992 applyShaders();
2993 applyTextures();
2994
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002995 if (!getCurrentProgram()->getProgramBinary()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002996 {
2997 return error(GL_INVALID_OPERATION);
2998 }
2999
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003000 if (!cullSkipsDraw(mode))
3001 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003002 mDisplay->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00003003
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003004 if (mode == GL_LINE_LOOP)
daniel@transgaming.comf6549452012-01-27 15:39:08 +00003005 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003006 drawLineLoop(count, GL_NONE, NULL, 0);
daniel@transgaming.comf6549452012-01-27 15:39:08 +00003007 }
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003008 else if (instances > 0)
daniel@transgaming.comf6549452012-01-27 15:39:08 +00003009 {
3010 StaticIndexBuffer *countingIB = mIndexDataManager->getCountingIndices(count);
3011 if (countingIB)
3012 {
3013 if (mAppliedIBSerial != countingIB->getSerial())
3014 {
3015 mDevice->SetIndices(countingIB->getBuffer());
3016 mAppliedIBSerial = countingIB->getSerial();
3017 }
3018
daniel@transgaming.comd6449312012-01-27 15:39:32 +00003019 for (int i = 0; i < repeatDraw; i++)
3020 {
3021 mDevice->DrawIndexedPrimitive(primitiveType, 0, 0, count, 0, primitiveCount);
3022 }
daniel@transgaming.comf6549452012-01-27 15:39:08 +00003023 }
3024 else
3025 {
3026 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
3027 return error(GL_OUT_OF_MEMORY);
3028 }
3029 }
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003030 else // Regular case
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003031 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003032 mDevice->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003033 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003034 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003035}
3036
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00003037void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, GLsizei instances)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003038{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003039 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003040 {
3041 return error(GL_INVALID_OPERATION);
3042 }
3043
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003044 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003045 {
3046 return error(GL_INVALID_OPERATION);
3047 }
3048
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003049 D3DPRIMITIVETYPE primitiveType;
3050 int primitiveCount;
3051
3052 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
3053 return error(GL_INVALID_ENUM);
3054
3055 if (primitiveCount <= 0)
3056 {
3057 return;
3058 }
3059
3060 if (!applyRenderTarget(false))
3061 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00003062 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003063 }
3064
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003065 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00003066
3067 TranslatedIndexData indexInfo;
3068 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
3069 if (err != GL_NO_ERROR)
3070 {
3071 return error(err);
3072 }
3073
daniel@transgaming.com83921382011-01-08 05:46:00 +00003074 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
daniel@transgaming.comd6449312012-01-27 15:39:32 +00003075 GLsizei repeatDraw = 1;
3076 err = applyVertexBuffer(indexInfo.minIndex, vertexCount, instances, &repeatDraw);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00003077 if (err != GL_NO_ERROR)
3078 {
3079 return error(err);
3080 }
3081
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003082 applyShaders();
3083 applyTextures();
3084
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00003085 if (!getCurrentProgram()->getProgramBinary()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00003086 {
3087 return error(GL_INVALID_OPERATION);
3088 }
3089
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003090 if (!cullSkipsDraw(mode))
3091 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003092 mDisplay->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003093
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003094 if (mode == GL_LINE_LOOP)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003095 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003096 drawLineLoop(count, type, indices, indexInfo.minIndex);
3097 }
3098 else
3099 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00003100 for (int i = 0; i < repeatDraw; i++)
3101 {
3102 mDevice->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
3103 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003104 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003105 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003106}
3107
daniel@transgaming.com0d86aa72011-10-26 02:35:10 +00003108// Implements glFlush when block is false, glFinish when block is true
3109void Context::sync(bool block)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003110{
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +00003111 mDisplay->sync(block);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003112}
3113
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003114void Context::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003115{
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003116 // Get the raw indices for an indexed draw
3117 if (type != GL_NONE && mState.elementArrayBuffer.get())
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003118 {
3119 Buffer *indexBuffer = mState.elementArrayBuffer.get();
3120 intptr_t offset = reinterpret_cast<intptr_t>(indices);
3121 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
3122 }
3123
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003124 UINT startIndex = 0;
3125 bool succeeded = false;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003126
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003127 if (supports32bitIndices())
3128 {
3129 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
3130
3131 if (!mLineLoopIB)
3132 {
3133 mLineLoopIB = new StreamingIndexBuffer(mDevice, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
3134 }
3135
3136 if (mLineLoopIB)
3137 {
3138 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
3139
3140 UINT offset = 0;
3141 unsigned int *data = static_cast<unsigned int*>(mLineLoopIB->map(spaceNeeded, &offset));
3142 startIndex = offset / 4;
3143
3144 if (data)
3145 {
3146 switch (type)
3147 {
3148 case GL_NONE: // Non-indexed draw
3149 for (int i = 0; i < count; i++)
3150 {
3151 data[i] = i;
3152 }
3153 data[count] = 0;
3154 break;
3155 case GL_UNSIGNED_BYTE:
3156 for (int i = 0; i < count; i++)
3157 {
3158 data[i] = static_cast<const GLubyte*>(indices)[i];
3159 }
3160 data[count] = static_cast<const GLubyte*>(indices)[0];
3161 break;
3162 case GL_UNSIGNED_SHORT:
3163 for (int i = 0; i < count; i++)
3164 {
3165 data[i] = static_cast<const GLushort*>(indices)[i];
3166 }
3167 data[count] = static_cast<const GLushort*>(indices)[0];
3168 break;
3169 case GL_UNSIGNED_INT:
3170 for (int i = 0; i < count; i++)
3171 {
3172 data[i] = static_cast<const GLuint*>(indices)[i];
3173 }
3174 data[count] = static_cast<const GLuint*>(indices)[0];
3175 break;
3176 default: UNREACHABLE();
3177 }
3178
3179 mLineLoopIB->unmap();
3180 succeeded = true;
3181 }
3182 }
3183 }
3184 else
3185 {
3186 const int spaceNeeded = (count + 1) * sizeof(unsigned short);
3187
3188 if (!mLineLoopIB)
3189 {
3190 mLineLoopIB = new StreamingIndexBuffer(mDevice, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
3191 }
3192
3193 if (mLineLoopIB)
3194 {
3195 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
3196
3197 UINT offset = 0;
3198 unsigned short *data = static_cast<unsigned short*>(mLineLoopIB->map(spaceNeeded, &offset));
3199 startIndex = offset / 2;
3200
3201 if (data)
3202 {
3203 switch (type)
3204 {
3205 case GL_NONE: // Non-indexed draw
3206 for (int i = 0; i < count; i++)
3207 {
3208 data[i] = i;
3209 }
3210 data[count] = 0;
3211 break;
3212 case GL_UNSIGNED_BYTE:
3213 for (int i = 0; i < count; i++)
3214 {
3215 data[i] = static_cast<const GLubyte*>(indices)[i];
3216 }
3217 data[count] = static_cast<const GLubyte*>(indices)[0];
3218 break;
3219 case GL_UNSIGNED_SHORT:
3220 for (int i = 0; i < count; i++)
3221 {
3222 data[i] = static_cast<const GLushort*>(indices)[i];
3223 }
3224 data[count] = static_cast<const GLushort*>(indices)[0];
3225 break;
3226 case GL_UNSIGNED_INT:
3227 for (int i = 0; i < count; i++)
3228 {
3229 data[i] = static_cast<const GLuint*>(indices)[i];
3230 }
3231 data[count] = static_cast<const GLuint*>(indices)[0];
3232 break;
3233 default: UNREACHABLE();
3234 }
3235
3236 mLineLoopIB->unmap();
3237 succeeded = true;
3238 }
3239 }
3240 }
3241
3242 if (succeeded)
3243 {
3244 if (mAppliedIBSerial != mLineLoopIB->getSerial())
3245 {
3246 mDevice->SetIndices(mLineLoopIB->getBuffer());
3247 mAppliedIBSerial = mLineLoopIB->getSerial();
3248 }
3249
3250 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
3251 }
3252 else
3253 {
3254 ERR("Could not create a looping index buffer for GL_LINE_LOOP.");
3255 return error(GL_OUT_OF_MEMORY);
3256 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003257}
3258
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003259void Context::recordInvalidEnum()
3260{
3261 mInvalidEnum = true;
3262}
3263
3264void Context::recordInvalidValue()
3265{
3266 mInvalidValue = true;
3267}
3268
3269void Context::recordInvalidOperation()
3270{
3271 mInvalidOperation = true;
3272}
3273
3274void Context::recordOutOfMemory()
3275{
3276 mOutOfMemory = true;
3277}
3278
3279void Context::recordInvalidFramebufferOperation()
3280{
3281 mInvalidFramebufferOperation = true;
3282}
3283
3284// Get one of the recorded errors and clear its flag, if any.
3285// [OpenGL ES 2.0.24] section 2.5 page 13.
3286GLenum Context::getError()
3287{
3288 if (mInvalidEnum)
3289 {
3290 mInvalidEnum = false;
3291
3292 return GL_INVALID_ENUM;
3293 }
3294
3295 if (mInvalidValue)
3296 {
3297 mInvalidValue = false;
3298
3299 return GL_INVALID_VALUE;
3300 }
3301
3302 if (mInvalidOperation)
3303 {
3304 mInvalidOperation = false;
3305
3306 return GL_INVALID_OPERATION;
3307 }
3308
3309 if (mOutOfMemory)
3310 {
3311 mOutOfMemory = false;
3312
3313 return GL_OUT_OF_MEMORY;
3314 }
3315
3316 if (mInvalidFramebufferOperation)
3317 {
3318 mInvalidFramebufferOperation = false;
3319
3320 return GL_INVALID_FRAMEBUFFER_OPERATION;
3321 }
3322
3323 return GL_NO_ERROR;
3324}
3325
daniel@transgaming.com17f548c2011-11-09 17:47:02 +00003326GLenum Context::getResetStatus()
3327{
3328 if (mResetStatus == GL_NO_ERROR)
3329 {
3330 bool lost = mDisplay->testDeviceLost();
3331
3332 if (lost)
3333 {
3334 mDisplay->notifyDeviceLost(); // Sets mResetStatus
3335 }
3336 }
3337
3338 GLenum status = mResetStatus;
3339
3340 if (mResetStatus != GL_NO_ERROR)
3341 {
3342 if (mDisplay->testDeviceResettable())
3343 {
3344 mResetStatus = GL_NO_ERROR;
3345 }
3346 }
3347
3348 return status;
3349}
3350
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00003351bool Context::isResetNotificationEnabled()
3352{
3353 return (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT);
3354}
3355
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003356bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003357{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003358 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003359}
3360
daniel@transgaming.com396c6432010-11-26 16:26:12 +00003361int Context::getMaximumVaryingVectors() const
3362{
3363 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3364}
3365
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003366unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003367{
3368 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3369}
3370
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003371unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003372{
3373 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3374}
3375
daniel@transgaming.com458da142010-11-28 02:03:02 +00003376int Context::getMaximumFragmentUniformVectors() const
3377{
3378 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3379}
3380
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003381int Context::getMaxSupportedSamples() const
3382{
3383 return mMaxSupportedSamples;
3384}
3385
3386int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3387{
3388 if (requested == 0)
3389 {
3390 return requested;
3391 }
3392
3393 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3394 if (itr == mMultiSampleSupport.end())
3395 {
3396 return -1;
3397 }
3398
3399 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3400 {
3401 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3402 {
3403 return i;
3404 }
3405 }
3406
3407 return -1;
3408}
3409
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003410bool Context::supportsEventQueries() const
3411{
3412 return mSupportsEventQueries;
3413}
3414
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00003415bool Context::supportsOcclusionQueries() const
3416{
3417 return mSupportsOcclusionQueries;
3418}
3419
gman@chromium.org50c526d2011-08-10 05:19:44 +00003420bool Context::supportsDXT1Textures() const
daniel@transgaming.com01868132010-08-24 19:21:17 +00003421{
gman@chromium.org50c526d2011-08-10 05:19:44 +00003422 return mSupportsDXT1Textures;
3423}
3424
3425bool Context::supportsDXT3Textures() const
3426{
3427 return mSupportsDXT3Textures;
3428}
3429
3430bool Context::supportsDXT5Textures() const
3431{
3432 return mSupportsDXT5Textures;
daniel@transgaming.com01868132010-08-24 19:21:17 +00003433}
3434
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003435bool Context::supportsFloat32Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003436{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003437 return mSupportsFloat32Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003438}
3439
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003440bool Context::supportsFloat32LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003441{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003442 return mSupportsFloat32LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003443}
3444
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003445bool Context::supportsFloat32RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003446{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003447 return mSupportsFloat32RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003448}
3449
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003450bool Context::supportsFloat16Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003451{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003452 return mSupportsFloat16Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003453}
3454
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003455bool Context::supportsFloat16LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003456{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003457 return mSupportsFloat16LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003458}
3459
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003460bool Context::supportsFloat16RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003461{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003462 return mSupportsFloat16RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003463}
3464
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003465int Context::getMaximumRenderbufferDimension() const
3466{
3467 return mMaxRenderbufferDimension;
3468}
3469
3470int Context::getMaximumTextureDimension() const
3471{
3472 return mMaxTextureDimension;
3473}
3474
3475int Context::getMaximumCubeTextureDimension() const
3476{
3477 return mMaxCubeTextureDimension;
3478}
3479
3480int Context::getMaximumTextureLevel() const
3481{
3482 return mMaxTextureLevel;
3483}
3484
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003485bool Context::supportsLuminanceTextures() const
3486{
3487 return mSupportsLuminanceTextures;
3488}
3489
3490bool Context::supportsLuminanceAlphaTextures() const
3491{
3492 return mSupportsLuminanceAlphaTextures;
3493}
3494
daniel@transgaming.com1c49f792012-05-31 01:14:02 +00003495bool Context::supportsDepthTextures() const
3496{
3497 return mSupportsDepthTextures;
3498}
3499
daniel@transgaming.com83921382011-01-08 05:46:00 +00003500bool Context::supports32bitIndices() const
3501{
3502 return mSupports32bitIndices;
3503}
3504
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003505bool Context::supportsNonPower2Texture() const
3506{
3507 return mSupportsNonPower2Texture;
3508}
3509
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +00003510bool Context::supportsInstancing() const
3511{
3512 return mSupportsInstancing;
3513}
3514
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003515void Context::detachBuffer(GLuint buffer)
3516{
3517 // [OpenGL ES 2.0.24] section 2.9 page 22:
3518 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3519 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3520
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003521 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003522 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003523 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003524 }
3525
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003526 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003527 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003528 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003529 }
3530
3531 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3532 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003533 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003534 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003535 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003536 }
3537 }
3538}
3539
3540void Context::detachTexture(GLuint texture)
3541{
3542 // [OpenGL ES 2.0.24] section 3.8 page 84:
3543 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3544 // rebound to texture object zero
3545
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003546 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003547 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003548 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003549 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003550 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003551 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003552 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003553 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003554 }
3555 }
3556
3557 // [OpenGL ES 2.0.24] section 4.4 page 112:
3558 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3559 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3560 // image was attached in the currently bound framebuffer.
3561
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003562 Framebuffer *readFramebuffer = getReadFramebuffer();
3563 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003564
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003565 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003566 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003567 readFramebuffer->detachTexture(texture);
3568 }
3569
3570 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3571 {
3572 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003573 }
3574}
3575
3576void Context::detachFramebuffer(GLuint framebuffer)
3577{
3578 // [OpenGL ES 2.0.24] section 4.4 page 107:
3579 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3580 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3581
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003582 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003583 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003584 bindReadFramebuffer(0);
3585 }
3586
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003587 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003588 {
3589 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003590 }
3591}
3592
3593void Context::detachRenderbuffer(GLuint renderbuffer)
3594{
3595 // [OpenGL ES 2.0.24] section 4.4 page 109:
3596 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3597 // had been executed with the target RENDERBUFFER and name of zero.
3598
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003599 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003600 {
3601 bindRenderbuffer(0);
3602 }
3603
3604 // [OpenGL ES 2.0.24] section 4.4 page 111:
3605 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3606 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3607 // point to which this image was attached in the currently bound framebuffer.
3608
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003609 Framebuffer *readFramebuffer = getReadFramebuffer();
3610 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003611
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003612 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003613 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003614 readFramebuffer->detachRenderbuffer(renderbuffer);
3615 }
3616
3617 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3618 {
3619 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003620 }
3621}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003622
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003623Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003624{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003625 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003626
3627 if (t == NULL)
3628 {
3629 static const GLubyte color[] = { 0, 0, 0, 255 };
3630
3631 switch (type)
3632 {
3633 default:
3634 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003635 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003636
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003637 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003638 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003639 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003640 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003641 t = incomplete2d;
3642 }
3643 break;
3644
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003645 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003646 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003647 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003648
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003649 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3650 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3651 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3652 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3653 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3654 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003655
3656 t = incompleteCube;
3657 }
3658 break;
3659 }
3660
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003661 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003662 }
3663
3664 return t;
3665}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003666
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003667bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003668{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003669 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003670}
3671
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003672bool Context::isTriangleMode(GLenum drawMode)
3673{
3674 switch (drawMode)
3675 {
3676 case GL_TRIANGLES:
3677 case GL_TRIANGLE_FAN:
3678 case GL_TRIANGLE_STRIP:
3679 return true;
3680 case GL_POINTS:
3681 case GL_LINES:
3682 case GL_LINE_LOOP:
3683 case GL_LINE_STRIP:
3684 return false;
3685 default: UNREACHABLE();
3686 }
3687
3688 return false;
3689}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003690
3691void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3692{
3693 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3694
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003695 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3696 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3697 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3698 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003699
daniel@transgaming.com83921382011-01-08 05:46:00 +00003700 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003701}
3702
daniel@transgaming.comd2820bf2012-01-27 15:38:48 +00003703void Context::setVertexAttribDivisor(GLuint index, GLuint divisor)
3704{
3705 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3706
3707 mState.vertexAttribute[index].mDivisor = divisor;
3708}
3709
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003710// keep list sorted in following order
3711// OES extensions
3712// EXT extensions
3713// Vendor extensions
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003714void Context::initExtensionString()
3715{
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003716 mExtensionString = "";
3717
3718 // OES extensions
3719 if (supports32bitIndices())
3720 {
3721 mExtensionString += "GL_OES_element_index_uint ";
3722 }
3723
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003724 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003725 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003726 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003727
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003728 if (supportsFloat16Textures())
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003729 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003730 mExtensionString += "GL_OES_texture_half_float ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003731 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003732 if (supportsFloat16LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003733 {
3734 mExtensionString += "GL_OES_texture_half_float_linear ";
3735 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003736 if (supportsFloat32Textures())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003737 {
3738 mExtensionString += "GL_OES_texture_float ";
3739 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003740 if (supportsFloat32LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003741 {
3742 mExtensionString += "GL_OES_texture_float_linear ";
3743 }
3744
3745 if (supportsNonPower2Texture())
3746 {
3747 mExtensionString += "GL_OES_texture_npot ";
3748 }
3749
3750 // Multi-vendor (EXT) extensions
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00003751 if (supportsOcclusionQueries())
3752 {
3753 mExtensionString += "GL_EXT_occlusion_query_boolean ";
3754 }
3755
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003756 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com8747f182011-11-09 17:50:38 +00003757 mExtensionString += "GL_EXT_robustness ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003758
gman@chromium.org50c526d2011-08-10 05:19:44 +00003759 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00003760 {
3761 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3762 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003763
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003764 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
daniel@transgaming.comdf363722011-12-16 23:29:53 +00003765 mExtensionString += "GL_EXT_texture_storage ";
gman@chromium.org50c526d2011-08-10 05:19:44 +00003766
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003767 // ANGLE-specific extensions
daniel@transgaming.com92d620c2012-05-31 01:16:31 +00003768 if (supportsDepthTextures())
3769 {
3770 mExtensionString += "GL_ANGLE_depth_texture ";
3771 }
3772
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003773 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003774 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003775 {
3776 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3777 }
3778
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +00003779 if (supportsInstancing())
daniel@transgaming.comdce02fd2012-01-27 15:39:51 +00003780 {
3781 mExtensionString += "GL_ANGLE_instanced_arrays ";
3782 }
3783
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00003784 mExtensionString += "GL_ANGLE_pack_reverse_row_order ";
3785
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003786 if (supportsDXT3Textures())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003787 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003788 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3789 }
3790 if (supportsDXT5Textures())
3791 {
3792 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003793 }
daniel@transgaming.com97412f72011-11-11 04:19:07 +00003794
3795 mExtensionString += "GL_ANGLE_texture_usage ";
zmo@google.coma574f782011-10-03 21:45:23 +00003796 mExtensionString += "GL_ANGLE_translated_shader_source ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003797
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003798 // Other vendor-specific extensions
3799 if (supportsEventQueries())
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003800 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003801 mExtensionString += "GL_NV_fence ";
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003802 }
3803
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003804 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3805 if (end != std::string::npos)
3806 {
3807 mExtensionString.resize(end+1);
3808 }
3809}
3810
3811const char *Context::getExtensionString() const
3812{
3813 return mExtensionString.c_str();
3814}
3815
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003816void Context::initRendererString()
3817{
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003818 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003819
3820 mRendererString = "ANGLE (";
3821 mRendererString += identifier->Description;
3822 mRendererString += ")";
3823}
3824
3825const char *Context::getRendererString() const
3826{
3827 return mRendererString.c_str();
3828}
3829
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003830void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3831 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3832 GLbitfield mask)
3833{
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003834 Framebuffer *readFramebuffer = getReadFramebuffer();
3835 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3836
3837 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3838 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3839 {
3840 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3841 }
3842
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003843 if (drawFramebuffer->getSamples() != 0)
3844 {
3845 return error(GL_INVALID_OPERATION);
3846 }
3847
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003848 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3849 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3850 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3851 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3852
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003853 RECT sourceRect;
3854 RECT destRect;
3855
3856 if (srcX0 < srcX1)
3857 {
3858 sourceRect.left = srcX0;
3859 sourceRect.right = srcX1;
3860 destRect.left = dstX0;
3861 destRect.right = dstX1;
3862 }
3863 else
3864 {
3865 sourceRect.left = srcX1;
3866 destRect.left = dstX1;
3867 sourceRect.right = srcX0;
3868 destRect.right = dstX0;
3869 }
3870
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003871 if (srcY0 < srcY1)
3872 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003873 sourceRect.top = readBufferHeight - srcY1;
3874 destRect.top = drawBufferHeight - dstY1;
3875 sourceRect.bottom = readBufferHeight - srcY0;
3876 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003877 }
3878 else
3879 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003880 sourceRect.top = readBufferHeight - srcY0;
3881 destRect.top = drawBufferHeight - dstY0;
3882 sourceRect.bottom = readBufferHeight - srcY1;
3883 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003884 }
3885
3886 RECT sourceScissoredRect = sourceRect;
3887 RECT destScissoredRect = destRect;
3888
3889 if (mState.scissorTest)
3890 {
3891 // Only write to parts of the destination framebuffer which pass the scissor test
3892 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3893 // rect will be checked against scissorY, rather than the bottom.
3894 if (destRect.left < mState.scissorX)
3895 {
3896 int xDiff = mState.scissorX - destRect.left;
3897 destScissoredRect.left = mState.scissorX;
3898 sourceScissoredRect.left += xDiff;
3899 }
3900
3901 if (destRect.right > mState.scissorX + mState.scissorWidth)
3902 {
3903 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3904 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3905 sourceScissoredRect.right -= xDiff;
3906 }
3907
3908 if (destRect.top < mState.scissorY)
3909 {
3910 int yDiff = mState.scissorY - destRect.top;
3911 destScissoredRect.top = mState.scissorY;
3912 sourceScissoredRect.top += yDiff;
3913 }
3914
3915 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3916 {
3917 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3918 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3919 sourceScissoredRect.bottom -= yDiff;
3920 }
3921 }
3922
3923 bool blitRenderTarget = false;
3924 bool blitDepthStencil = false;
3925
3926 RECT sourceTrimmedRect = sourceScissoredRect;
3927 RECT destTrimmedRect = destScissoredRect;
3928
3929 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3930 // the actual draw and read surfaces.
3931 if (sourceTrimmedRect.left < 0)
3932 {
3933 int xDiff = 0 - sourceTrimmedRect.left;
3934 sourceTrimmedRect.left = 0;
3935 destTrimmedRect.left += xDiff;
3936 }
3937
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003938 if (sourceTrimmedRect.right > readBufferWidth)
3939 {
3940 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3941 sourceTrimmedRect.right = readBufferWidth;
3942 destTrimmedRect.right -= xDiff;
3943 }
3944
3945 if (sourceTrimmedRect.top < 0)
3946 {
3947 int yDiff = 0 - sourceTrimmedRect.top;
3948 sourceTrimmedRect.top = 0;
3949 destTrimmedRect.top += yDiff;
3950 }
3951
3952 if (sourceTrimmedRect.bottom > readBufferHeight)
3953 {
3954 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3955 sourceTrimmedRect.bottom = readBufferHeight;
3956 destTrimmedRect.bottom -= yDiff;
3957 }
3958
3959 if (destTrimmedRect.left < 0)
3960 {
3961 int xDiff = 0 - destTrimmedRect.left;
3962 destTrimmedRect.left = 0;
3963 sourceTrimmedRect.left += xDiff;
3964 }
3965
3966 if (destTrimmedRect.right > drawBufferWidth)
3967 {
3968 int xDiff = destTrimmedRect.right - drawBufferWidth;
3969 destTrimmedRect.right = drawBufferWidth;
3970 sourceTrimmedRect.right -= xDiff;
3971 }
3972
3973 if (destTrimmedRect.top < 0)
3974 {
3975 int yDiff = 0 - destTrimmedRect.top;
3976 destTrimmedRect.top = 0;
3977 sourceTrimmedRect.top += yDiff;
3978 }
3979
3980 if (destTrimmedRect.bottom > drawBufferHeight)
3981 {
3982 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3983 destTrimmedRect.bottom = drawBufferHeight;
3984 sourceTrimmedRect.bottom -= yDiff;
3985 }
3986
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003987 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003988 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3989 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3990 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3991 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003992 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3993 {
3994 partialBufferCopy = true;
3995 }
3996
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003997 if (mask & GL_COLOR_BUFFER_BIT)
3998 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003999 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
4000 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
4001 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
4002 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
4003 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004004 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
4005 {
4006 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
4007 return error(GL_INVALID_OPERATION);
4008 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004009
4010 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
4011 {
4012 return error(GL_INVALID_OPERATION);
4013 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004014
4015 blitRenderTarget = true;
4016
4017 }
4018
4019 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
4020 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00004021 Renderbuffer *readDSBuffer = NULL;
4022 Renderbuffer *drawDSBuffer = NULL;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004023
4024 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
4025 // both a depth and stencil buffer, it will be the same buffer.
4026
4027 if (mask & GL_DEPTH_BUFFER_BIT)
4028 {
4029 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
4030 {
4031 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
4032 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
4033 {
4034 return error(GL_INVALID_OPERATION);
4035 }
4036
4037 blitDepthStencil = true;
4038 readDSBuffer = readFramebuffer->getDepthbuffer();
4039 drawDSBuffer = drawFramebuffer->getDepthbuffer();
4040 }
4041 }
4042
4043 if (mask & GL_STENCIL_BUFFER_BIT)
4044 {
4045 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
4046 {
4047 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
4048 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
4049 {
4050 return error(GL_INVALID_OPERATION);
4051 }
4052
4053 blitDepthStencil = true;
4054 readDSBuffer = readFramebuffer->getStencilbuffer();
4055 drawDSBuffer = drawFramebuffer->getStencilbuffer();
4056 }
4057 }
4058
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004059 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004060 {
4061 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
4062 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
4063 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004064
daniel@transgaming.com97446d22010-08-24 19:20:54 +00004065 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
4066 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004067 {
4068 return error(GL_INVALID_OPERATION);
4069 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004070 }
4071
4072 if (blitRenderTarget || blitDepthStencil)
4073 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00004074 mDisplay->endScene();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004075
4076 if (blitRenderTarget)
4077 {
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00004078 IDirect3DSurface9* readRenderTarget = readFramebuffer->getRenderTarget();
4079 IDirect3DSurface9* drawRenderTarget = drawFramebuffer->getRenderTarget();
4080
4081 HRESULT result = mDevice->StretchRect(readRenderTarget, &sourceTrimmedRect,
4082 drawRenderTarget, &destTrimmedRect, D3DTEXF_NONE);
4083
4084 readRenderTarget->Release();
4085 drawRenderTarget->Release();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004086
4087 if (FAILED(result))
4088 {
4089 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
4090 return;
4091 }
4092 }
4093
4094 if (blitDepthStencil)
4095 {
daniel@transgaming.com63e6afe2012-05-31 01:14:42 +00004096 IDirect3DSurface9* readDepthStencil = readFramebuffer->getDepthStencil();
4097 IDirect3DSurface9* drawDepthStencil = drawFramebuffer->getDepthStencil();
4098
4099 HRESULT result = mDevice->StretchRect(readDepthStencil, NULL, drawDepthStencil, NULL, D3DTEXF_NONE);
4100
4101 readDepthStencil->Release();
4102 drawDepthStencil->Release();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004103
4104 if (FAILED(result))
4105 {
4106 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
4107 return;
4108 }
4109 }
4110 }
4111}
4112
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004113VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
4114{
4115 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4116 {
4117 mVertexDeclCache[i].vertexDeclaration = NULL;
4118 mVertexDeclCache[i].lruCount = 0;
4119 }
4120}
4121
4122VertexDeclarationCache::~VertexDeclarationCache()
4123{
4124 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4125 {
4126 if (mVertexDeclCache[i].vertexDeclaration)
4127 {
4128 mVertexDeclCache[i].vertexDeclaration->Release();
4129 }
4130 }
4131}
4132
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004133GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], Program *program, GLsizei instances, GLsizei *repeatDraw)
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004134{
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004135 *repeatDraw = 1;
4136
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004137 int indexedAttribute = MAX_VERTEX_ATTRIBS;
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004138 int instancedAttribute = MAX_VERTEX_ATTRIBS;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004139
4140 if (instances > 0)
4141 {
4142 // Find an indexed attribute to be mapped to D3D stream 0
4143 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4144 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004145 if (attributes[i].active)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004146 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004147 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
4148 {
4149 if (attributes[i].divisor == 0)
4150 {
4151 indexedAttribute = i;
4152 }
4153 }
4154 else if (instancedAttribute == MAX_VERTEX_ATTRIBS)
4155 {
4156 if (attributes[i].divisor != 0)
4157 {
4158 instancedAttribute = i;
4159 }
4160 }
4161 else break; // Found both an indexed and instanced attribute
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004162 }
4163 }
4164
4165 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
4166 {
4167 return GL_INVALID_OPERATION;
4168 }
4169 }
4170
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004171 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
4172 D3DVERTEXELEMENT9 *element = &elements[0];
4173
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00004174 ProgramBinary *programBinary = program->getProgramBinary();
4175
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004176 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4177 {
4178 if (attributes[i].active)
4179 {
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004180 int stream = i;
4181
4182 if (instances > 0)
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004183 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004184 // Due to a bug on ATI cards we can't enable instancing when none of the attributes are instanced.
4185 if (instancedAttribute == MAX_VERTEX_ATTRIBS)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004186 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004187 *repeatDraw = instances;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004188 }
4189 else
4190 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004191 if (i == indexedAttribute)
4192 {
4193 stream = 0;
4194 }
4195 else if (i == 0)
4196 {
4197 stream = indexedAttribute;
4198 }
4199
4200 UINT frequency = 1;
4201
4202 if (attributes[i].divisor == 0)
4203 {
4204 frequency = D3DSTREAMSOURCE_INDEXEDDATA | instances;
4205 }
4206 else
4207 {
4208 frequency = D3DSTREAMSOURCE_INSTANCEDATA | attributes[i].divisor;
4209 }
4210
4211 device->SetStreamSourceFreq(stream, frequency);
4212 mInstancingEnabled = true;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004213 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004214 }
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004215
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004216 if (mAppliedVBs[stream].serial != attributes[i].serial ||
4217 mAppliedVBs[stream].stride != attributes[i].stride ||
4218 mAppliedVBs[stream].offset != attributes[i].offset)
4219 {
4220 device->SetStreamSource(stream, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
4221 mAppliedVBs[stream].serial = attributes[i].serial;
4222 mAppliedVBs[stream].stride = attributes[i].stride;
4223 mAppliedVBs[stream].offset = attributes[i].offset;
4224 }
4225
4226 element->Stream = stream;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004227 element->Offset = 0;
4228 element->Type = attributes[i].type;
4229 element->Method = D3DDECLMETHOD_DEFAULT;
4230 element->Usage = D3DDECLUSAGE_TEXCOORD;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00004231 element->UsageIndex = programBinary->getSemanticIndex(i);
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004232 element++;
4233 }
4234 }
4235
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004236 if (instances == 0 || instancedAttribute == MAX_VERTEX_ATTRIBS)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004237 {
4238 if (mInstancingEnabled)
4239 {
4240 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4241 {
4242 device->SetStreamSourceFreq(i, 1);
4243 }
4244
4245 mInstancingEnabled = false;
4246 }
4247 }
4248
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004249 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
4250 *(element++) = end;
4251
4252 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4253 {
4254 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
4255 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
4256 {
4257 entry->lruCount = ++mMaxLru;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004258 if(entry->vertexDeclaration != mLastSetVDecl)
4259 {
4260 device->SetVertexDeclaration(entry->vertexDeclaration);
4261 mLastSetVDecl = entry->vertexDeclaration;
4262 }
4263
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004264 return GL_NO_ERROR;
4265 }
4266 }
4267
4268 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
4269
4270 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4271 {
4272 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
4273 {
4274 lastCache = &mVertexDeclCache[i];
4275 }
4276 }
4277
4278 if (lastCache->vertexDeclaration != NULL)
4279 {
4280 lastCache->vertexDeclaration->Release();
4281 lastCache->vertexDeclaration = NULL;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004282 // mLastSetVDecl is set to the replacement, so we don't have to worry
4283 // about it.
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004284 }
4285
4286 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
4287 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
4288 device->SetVertexDeclaration(lastCache->vertexDeclaration);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004289 mLastSetVDecl = lastCache->vertexDeclaration;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004290 lastCache->lruCount = ++mMaxLru;
4291
4292 return GL_NO_ERROR;
4293}
4294
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004295void VertexDeclarationCache::markStateDirty()
4296{
4297 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4298 {
4299 mAppliedVBs[i].serial = 0;
4300 }
4301
4302 mLastSetVDecl = NULL;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004303 mInstancingEnabled = true; // Forces it to be disabled when not used
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004304}
4305
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004306}
4307
4308extern "C"
4309{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00004310gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext, bool notifyResets, bool robustAccess)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004311{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00004312 return new gl::Context(config, shareContext, notifyResets, robustAccess);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004313}
4314
4315void glDestroyContext(gl::Context *context)
4316{
4317 delete context;
4318
4319 if (context == gl::getContext())
4320 {
4321 gl::makeCurrent(NULL, NULL, NULL);
4322 }
4323}
4324
4325void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
4326{
4327 gl::makeCurrent(context, display, surface);
4328}
4329
4330gl::Context *glGetCurrentContext()
4331{
4332 return gl::getContext();
4333}
4334}