blob: 820f640bc90c69305b10b65d009c7098e7153bdd [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;
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001656 case GL_NUM_PROGRAM_BINARY_FORMATS_OES:
1657 *params = 1;
1658 break;
1659 case GL_PROGRAM_BINARY_FORMATS_OES:
1660 *params = GL_PROGRAM_BINARY_ANGLE;
1661 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001662 default:
1663 return false;
1664 }
1665
1666 return true;
1667}
1668
1669bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1670{
1671 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1672 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1673 // to the fact that it is stored internally as a float, and so would require conversion
1674 // if returned from Context::getIntegerv. Since this conversion is already implemented
1675 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1676 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1677 // application.
1678 switch (pname)
1679 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001680 case GL_COMPRESSED_TEXTURE_FORMATS:
1681 {
1682 *type = GL_INT;
1683 *numParams = mNumCompressedTextureFormats;
1684 }
1685 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001686 case GL_SHADER_BINARY_FORMATS:
1687 {
1688 *type = GL_INT;
1689 *numParams = 0;
1690 }
1691 break;
1692 case GL_MAX_VERTEX_ATTRIBS:
1693 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1694 case GL_MAX_VARYING_VECTORS:
1695 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1696 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1697 case GL_MAX_TEXTURE_IMAGE_UNITS:
1698 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1699 case GL_MAX_RENDERBUFFER_SIZE:
1700 case GL_NUM_SHADER_BINARY_FORMATS:
1701 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1702 case GL_ARRAY_BUFFER_BINDING:
1703 case GL_FRAMEBUFFER_BINDING:
1704 case GL_RENDERBUFFER_BINDING:
1705 case GL_CURRENT_PROGRAM:
1706 case GL_PACK_ALIGNMENT:
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00001707 case GL_PACK_REVERSE_ROW_ORDER_ANGLE:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001708 case GL_UNPACK_ALIGNMENT:
1709 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001710 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001711 case GL_RED_BITS:
1712 case GL_GREEN_BITS:
1713 case GL_BLUE_BITS:
1714 case GL_ALPHA_BITS:
1715 case GL_DEPTH_BITS:
1716 case GL_STENCIL_BITS:
1717 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1718 case GL_CULL_FACE_MODE:
1719 case GL_FRONT_FACE:
1720 case GL_ACTIVE_TEXTURE:
1721 case GL_STENCIL_FUNC:
1722 case GL_STENCIL_VALUE_MASK:
1723 case GL_STENCIL_REF:
1724 case GL_STENCIL_FAIL:
1725 case GL_STENCIL_PASS_DEPTH_FAIL:
1726 case GL_STENCIL_PASS_DEPTH_PASS:
1727 case GL_STENCIL_BACK_FUNC:
1728 case GL_STENCIL_BACK_VALUE_MASK:
1729 case GL_STENCIL_BACK_REF:
1730 case GL_STENCIL_BACK_FAIL:
1731 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1732 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1733 case GL_DEPTH_FUNC:
1734 case GL_BLEND_SRC_RGB:
1735 case GL_BLEND_SRC_ALPHA:
1736 case GL_BLEND_DST_RGB:
1737 case GL_BLEND_DST_ALPHA:
1738 case GL_BLEND_EQUATION_RGB:
1739 case GL_BLEND_EQUATION_ALPHA:
1740 case GL_STENCIL_WRITEMASK:
1741 case GL_STENCIL_BACK_WRITEMASK:
1742 case GL_STENCIL_CLEAR_VALUE:
1743 case GL_SUBPIXEL_BITS:
1744 case GL_MAX_TEXTURE_SIZE:
1745 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1746 case GL_SAMPLE_BUFFERS:
1747 case GL_SAMPLES:
1748 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1749 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1750 case GL_TEXTURE_BINDING_2D:
1751 case GL_TEXTURE_BINDING_CUBE_MAP:
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001752 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00001753 case GL_NUM_PROGRAM_BINARY_FORMATS_OES:
1754 case GL_PROGRAM_BINARY_FORMATS_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001755 {
1756 *type = GL_INT;
1757 *numParams = 1;
1758 }
1759 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001760 case GL_MAX_SAMPLES_ANGLE:
1761 {
1762 if (getMaxSupportedSamples() != 0)
1763 {
1764 *type = GL_INT;
1765 *numParams = 1;
1766 }
1767 else
1768 {
1769 return false;
1770 }
1771 }
1772 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001773 case GL_MAX_VIEWPORT_DIMS:
1774 {
1775 *type = GL_INT;
1776 *numParams = 2;
1777 }
1778 break;
1779 case GL_VIEWPORT:
1780 case GL_SCISSOR_BOX:
1781 {
1782 *type = GL_INT;
1783 *numParams = 4;
1784 }
1785 break;
1786 case GL_SHADER_COMPILER:
1787 case GL_SAMPLE_COVERAGE_INVERT:
1788 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001789 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1790 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1791 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1792 case GL_SAMPLE_COVERAGE:
1793 case GL_SCISSOR_TEST:
1794 case GL_STENCIL_TEST:
1795 case GL_DEPTH_TEST:
1796 case GL_BLEND:
1797 case GL_DITHER:
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001798 case GL_CONTEXT_ROBUST_ACCESS_EXT:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001799 {
1800 *type = GL_BOOL;
1801 *numParams = 1;
1802 }
1803 break;
1804 case GL_COLOR_WRITEMASK:
1805 {
1806 *type = GL_BOOL;
1807 *numParams = 4;
1808 }
1809 break;
1810 case GL_POLYGON_OFFSET_FACTOR:
1811 case GL_POLYGON_OFFSET_UNITS:
1812 case GL_SAMPLE_COVERAGE_VALUE:
1813 case GL_DEPTH_CLEAR_VALUE:
1814 case GL_LINE_WIDTH:
1815 {
1816 *type = GL_FLOAT;
1817 *numParams = 1;
1818 }
1819 break;
1820 case GL_ALIASED_LINE_WIDTH_RANGE:
1821 case GL_ALIASED_POINT_SIZE_RANGE:
1822 case GL_DEPTH_RANGE:
1823 {
1824 *type = GL_FLOAT;
1825 *numParams = 2;
1826 }
1827 break;
1828 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001829 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001830 {
1831 *type = GL_FLOAT;
1832 *numParams = 4;
1833 }
1834 break;
1835 default:
1836 return false;
1837 }
1838
1839 return true;
1840}
1841
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001842// Applies the render target surface, depth stencil surface, viewport rectangle and
1843// scissor rectangle to the Direct3D 9 device
1844bool Context::applyRenderTarget(bool ignoreViewport)
1845{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001846 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001847
1848 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1849 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001850 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001851 }
1852
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001853 // if there is no color attachment we must synthesize a NULL colorattachment
1854 // to keep the D3D runtime happy. This should only be possible if depth texturing.
1855 Renderbuffer *renderbufferObject = NULL;
1856 if (framebufferObject->getColorbufferType() != GL_NONE)
1857 {
1858 renderbufferObject = framebufferObject->getColorbuffer();
1859 }
1860 else
1861 {
1862 renderbufferObject = framebufferObject->getNullColorbuffer();
1863 }
1864 if (!renderbufferObject)
1865 {
1866 ERR("unable to locate renderbuffer for FBO.");
1867 return false;
1868 }
1869
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001870 bool renderTargetChanged = false;
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001871 unsigned int renderTargetSerial = renderbufferObject->getSerial();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001872 if (renderTargetSerial != mAppliedRenderTargetSerial)
1873 {
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001874 IDirect3DSurface9 *renderTarget = renderbufferObject->getRenderTarget();
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001875 if (!renderTarget)
1876 {
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001877 ERR("render target pointer unexpectedly null.");
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001878 return false; // Context must be lost
1879 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001880 mDevice->SetRenderTarget(0, renderTarget);
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001881 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001882 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 +00001883 renderTargetChanged = true;
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001884 renderTarget->Release();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001885 }
1886
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001887 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001888 unsigned int depthbufferSerial = 0;
1889 unsigned int stencilbufferSerial = 0;
1890 if (framebufferObject->getDepthbufferType() != GL_NONE)
1891 {
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001892 Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
1893 depthStencil = depthbuffer->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001894 if (!depthStencil)
1895 {
1896 ERR("Depth stencil pointer unexpectedly null.");
1897 return false;
1898 }
1899
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001900 depthbufferSerial = depthbuffer->getSerial();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001901 }
1902 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1903 {
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001904 Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
1905 depthStencil = stencilbuffer->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001906 if (!depthStencil)
1907 {
1908 ERR("Depth stencil pointer unexpectedly null.");
1909 return false;
1910 }
1911
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001912 stencilbufferSerial = stencilbuffer->getSerial();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001913 }
1914
1915 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001916 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001917 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001918 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001919 mDevice->SetDepthStencilSurface(depthStencil);
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001920 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001921 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001922 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001923 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001924
daniel@transgaming.com63e6afe2012-05-31 01:14:42 +00001925 if (depthStencil)
1926 {
1927 depthStencil->Release();
1928 }
1929
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001930 if (!mRenderTargetDescInitialized || renderTargetChanged)
1931 {
daniel@transgaming.com68145c62012-05-31 01:14:46 +00001932 IDirect3DSurface9 *renderTarget = renderbufferObject->getRenderTarget();
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001933 if (!renderTarget)
1934 {
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001935 return false; // Context must be lost
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001936 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001937 renderTarget->GetDesc(&mRenderTargetDesc);
1938 mRenderTargetDescInitialized = true;
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001939 renderTarget->Release();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001940 }
1941
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001942 D3DVIEWPORT9 viewport;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001943
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001944 float zNear = clamp01(mState.zNear);
1945 float zFar = clamp01(mState.zFar);
1946
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001947 if (ignoreViewport)
1948 {
1949 viewport.X = 0;
1950 viewport.Y = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001951 viewport.Width = mRenderTargetDesc.Width;
1952 viewport.Height = mRenderTargetDesc.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001953 viewport.MinZ = 0.0f;
1954 viewport.MaxZ = 1.0f;
1955 }
1956 else
1957 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001958 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, mRenderTargetDesc.Height);
1959 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1960 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1961 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width) - static_cast<LONG>(viewport.X));
1962 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 +00001963 viewport.MinZ = zNear;
1964 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001965 }
1966
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001967 if (viewport.Width <= 0 || viewport.Height <= 0)
1968 {
1969 return false; // Nothing to render
1970 }
1971
jbauman@chromium.org241e70d2011-11-03 23:07:05 +00001972 if (renderTargetChanged || !mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001973 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001974 mDevice->SetViewport(&viewport);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001975 mSetViewport = viewport;
1976 mViewportInitialized = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001977 mDxUniformsDirty = true;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001978 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001979
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001980 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001981 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001982 if (mState.scissorTest)
1983 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001984 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, mRenderTargetDesc.Height);
1985 rect.left = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1986 rect.top = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1987 rect.right = clamp(rect.right, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1988 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001989 mDevice->SetScissorRect(&rect);
1990 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001991 }
1992 else
1993 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001994 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001995 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001996
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001997 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001998 }
1999
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00002000 if (mState.currentProgram && mDxUniformsDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002001 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002002 Program *programObject = getCurrentProgram();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002003 ProgramBinary *programBinary = programObject->getProgramBinary();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002004
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002005 GLint halfPixelSize = programBinary->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002006 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002007 programBinary->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00002008
daniel@transgaming.comd9a54f92011-12-22 18:32:53 +00002009 // 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 +00002010 GLint coord = programBinary->getDxCoordLocation();
daniel@transgaming.comd9a54f92011-12-22 18:32:53 +00002011 float h = mSupportsShaderModel3 ? mRenderTargetDesc.Height : mState.viewportHeight / 2.0f;
2012 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, h,
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002013 (float)mState.viewportX + mState.viewportWidth / 2.0f,
2014 (float)mState.viewportY + mState.viewportHeight / 2.0f};
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002015 programBinary->setUniform4fv(coord, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00002016
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002017 GLint depth = programBinary->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00002018 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002019 programBinary->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00002020
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002021 GLint depthRange = programBinary->getDxDepthRangeLocation();
daniel@transgaming.com31754962010-11-28 02:02:52 +00002022 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002023 programBinary->setUniform3fv(depthRange, 1, nearFarDiff);
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00002024 mDxUniformsDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002025 }
2026
2027 return true;
2028}
2029
2030// 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 +00002031void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002032{
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00002033 Program *programObject = getCurrentProgram();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002034 ProgramBinary *programBinary = programObject->getProgramBinary();
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00002035
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002036 Framebuffer *framebufferObject = getDrawFramebuffer();
2037
2038 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
2039
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002040 GLint frontCCW = programBinary->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002041 GLint ccw = (adjustedFrontFace == GL_CCW);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002042 programBinary->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002043
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002044 GLint pointsOrLines = programBinary->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002045 GLint alwaysFront = !isTriangleMode(drawMode);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002046 programBinary->setUniform1iv(pointsOrLines, 1, &alwaysFront);
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002047
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002048 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002049 bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
2050 // Apparently some ATI cards have a bug where a draw with a zero color
2051 // write mask can cause later draws to have incorrect results. Instead,
2052 // set a nonzero color write mask but modify the blend state so that no
2053 // drawing is done.
2054 // http://code.google.com/p/angleproject/issues/detail?id=169
2055
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002056 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002057 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002058 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002059 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002060 mDevice->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002061 }
2062 else
2063 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002064 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002065 }
2066
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002067 mCullStateDirty = false;
2068 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002069
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002070 if (mDepthStateDirty)
2071 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00002072 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002073 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002074 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
2075 mDevice->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002076 }
2077 else
2078 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002079 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002080 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002081
2082 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002083 }
2084
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002085 if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
2086 {
2087 mBlendStateDirty = true;
2088 mMaskStateDirty = true;
2089 }
2090
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002091 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002092 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002093 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00002094 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002095 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002096
2097 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
2098 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
2099 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002100 mDevice->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002101 }
2102 else
2103 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002104 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002105 unorm<8>(mState.blendColor.alpha),
2106 unorm<8>(mState.blendColor.alpha),
2107 unorm<8>(mState.blendColor.alpha)));
2108 }
2109
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002110 mDevice->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
2111 mDevice->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
2112 mDevice->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002113
2114 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
2115 mState.destBlendRGB != mState.destBlendAlpha ||
2116 mState.blendEquationRGB != mState.blendEquationAlpha)
2117 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002118 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002119
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002120 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
2121 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
2122 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002123 }
2124 else
2125 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002126 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002127 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00002128 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002129 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00002130 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002131 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00002132 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002133
2134 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002135 }
2136
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002137 if (mStencilStateDirty || mFrontFaceDirty)
2138 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002139 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002140 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002141 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2142 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002143
2144 // FIXME: Unsupported by D3D9
2145 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
2146 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
2147 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
2148 if (mState.stencilWritemask != mState.stencilBackWritemask ||
2149 mState.stencilRef != mState.stencilBackRef ||
2150 mState.stencilMask != mState.stencilBackMask)
2151 {
2152 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
2153 return error(GL_INVALID_OPERATION);
2154 }
2155
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00002156 // get the maximum size of the stencil ref
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00002157 gl::Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00002158 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
2159
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002160 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
2161 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002162 es2dx::ConvertComparison(mState.stencilFunc));
2163
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002164 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
2165 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002166
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002167 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002168 es2dx::ConvertStencilOp(mState.stencilFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002169 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002170 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002171 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002172 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
2173
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002174 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
2175 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002176 es2dx::ConvertComparison(mState.stencilBackFunc));
2177
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002178 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
2179 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002180
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002181 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002182 es2dx::ConvertStencilOp(mState.stencilBackFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002183 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002184 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002185 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002186 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
2187 }
2188 else
2189 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002190 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002191 }
2192
2193 mStencilStateDirty = false;
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002194 mFrontFaceDirty = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002195 }
2196
2197 if (mMaskStateDirty)
2198 {
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002199 int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
2200 mState.colorMaskBlue, mState.colorMaskAlpha);
2201 if (colorMask == 0 && !zeroColorMaskAllowed)
2202 {
2203 // Enable green channel, but set blending so nothing will be drawn.
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002204 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
2205 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002206
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002207 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
2208 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
2209 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002210 }
2211 else
2212 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002213 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002214 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002215 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002216
2217 mMaskStateDirty = false;
2218 }
2219
2220 if (mPolygonOffsetStateDirty)
2221 {
2222 if (mState.polygonOffsetFill)
2223 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00002224 gl::Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002225 if (depthbuffer)
2226 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002227 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002228 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002229 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002230 }
2231 }
2232 else
2233 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002234 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
2235 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002236 }
2237
2238 mPolygonOffsetStateDirty = false;
2239 }
2240
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002241 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002242 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002243 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002244 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002245 FIXME("Sample alpha to coverage is unimplemented.");
2246 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002247
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002248 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002249 if (mState.sampleCoverage)
2250 {
2251 unsigned int mask = 0;
2252 if (mState.sampleCoverageValue != 0)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002253 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002254 float threshold = 0.5f;
2255
2256 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002257 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002258 mask <<= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002259
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002260 if ((i + 1) * mState.sampleCoverageValue >= threshold)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002261 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002262 threshold += 1.0f;
2263 mask |= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002264 }
2265 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002266 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002267
2268 if (mState.sampleCoverageInvert)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002269 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002270 mask = ~mask;
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002271 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002272
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002273 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002274 }
2275 else
2276 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002277 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002278 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002279
2280 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002281 }
2282
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002283 if (mDitherStateDirty)
2284 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002285 mDevice->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002286
2287 mDitherStateDirty = false;
2288 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002289}
2290
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002291GLenum Context::applyVertexBuffer(GLint first, GLsizei count, GLsizei instances, GLsizei *repeatDraw)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002292{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002293 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002294
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +00002295 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes, instances);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002296 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002297 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002298 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002299 }
2300
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002301 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, getCurrentProgram(), instances, repeatDraw);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002302}
2303
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002304// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.comd2820bf2012-01-27 15:38:48 +00002305GLenum Context::applyIndexBuffer(const GLvoid *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002306{
daniel@transgaming.com83921382011-01-08 05:46:00 +00002307 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002308
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002309 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002310 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002311 if (indexInfo->serial != mAppliedIBSerial)
2312 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002313 mDevice->SetIndices(indexInfo->indexBuffer);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002314 mAppliedIBSerial = indexInfo->serial;
2315 }
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002316 }
2317
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002318 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002319}
2320
2321// Applies the shaders and shader constants to the Direct3D 9 device
2322void Context::applyShaders()
2323{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002324 Program *programObject = getCurrentProgram();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002325 ProgramBinary *programBinary = programObject->getProgramBinary();
2326
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002327 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002328 {
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002329 IDirect3DVertexShader9 *vertexShader = programBinary->getVertexShader();
2330 IDirect3DPixelShader9 *pixelShader = programBinary->getPixelShader();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002331
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002332 mDevice->SetPixelShader(pixelShader);
2333 mDevice->SetVertexShader(vertexShader);
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002334 programBinary->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002335 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002336 }
2337
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002338 programBinary->applyUniforms();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002339}
2340
2341// Applies the textures and sampler states to the Direct3D 9 device
2342void Context::applyTextures()
2343{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002344 applyTextures(SAMPLER_PIXEL);
2345
2346 if (mSupportsVertexTexture)
2347 {
2348 applyTextures(SAMPLER_VERTEX);
2349 }
2350}
2351
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002352// For each Direct3D 9 sampler of either the pixel or vertex stage,
2353// looks up the corresponding OpenGL texture image unit and texture type,
2354// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002355void Context::applyTextures(SamplerType type)
2356{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002357 Program *programObject = getCurrentProgram();
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002358 ProgramBinary *programBinary = programObject->getProgramBinary();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002359
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002360 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 +00002361 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2362 int d3dSamplerOffset = (type == SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002363 int samplerRange = programBinary->getUsedSamplerRange(type);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002364
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002365 for (int samplerIndex = 0; samplerIndex < samplerRange; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002366 {
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002367 int textureUnit = programBinary->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00002368 int d3dSampler = samplerIndex + d3dSamplerOffset;
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002369
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002370 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002371 {
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00002372 TextureType textureType = programBinary->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002373
2374 Texture *texture = getSamplerTexture(textureUnit, textureType);
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002375 unsigned int texSerial = texture->getTextureSerial();
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002376
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002377 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyParameters() || texture->hasDirtyImages())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002378 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002379 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2380
2381 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002382 {
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002383 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyParameters())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002384 {
2385 GLenum wrapS = texture->getWrapS();
2386 GLenum wrapT = texture->getWrapT();
2387 GLenum minFilter = texture->getMinFilter();
2388 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002389
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002390 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2391 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002392
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002393 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002394 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2395 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002396 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2397 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002398 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002399
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002400 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyImages())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002401 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002402 mDevice->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002403 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002404 }
2405 else
2406 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002407 mDevice->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002408 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002409
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002410 appliedTextureSerial[samplerIndex] = texSerial;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002411 texture->resetDirty();
2412 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002413 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002414 else
2415 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002416 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002417 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002418 mDevice->SetTexture(d3dSampler, NULL);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002419 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002420 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002421 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002422 }
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002423
2424 for (int samplerIndex = samplerRange; samplerIndex < samplerCount; samplerIndex++)
2425 {
2426 if (appliedTextureSerial[samplerIndex] != 0)
2427 {
daniel@transgaming.comc5a7b692011-10-26 02:45:44 +00002428 mDevice->SetTexture(samplerIndex + d3dSamplerOffset, NULL);
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002429 appliedTextureSerial[samplerIndex] = 0;
2430 }
2431 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002432}
2433
daniel@transgaming.comb7915a52011-11-12 03:14:20 +00002434void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height,
2435 GLenum format, GLenum type, GLsizei *bufSize, void* pixels)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002436{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002437 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002438
2439 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2440 {
2441 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2442 }
2443
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002444 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2445 {
2446 return error(GL_INVALID_OPERATION);
2447 }
2448
daniel@transgaming.comb7915a52011-11-12 03:14:20 +00002449 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
2450 // sized query sanity check
2451 if (bufSize)
2452 {
2453 int requiredSize = outputPitch * height;
2454 if (requiredSize > *bufSize)
2455 {
2456 return error(GL_INVALID_OPERATION);
2457 }
2458 }
2459
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002460 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002461 if (!renderTarget)
2462 {
2463 return; // Context must be lost, return silently
2464 }
2465
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002466 D3DSURFACE_DESC desc;
2467 renderTarget->GetDesc(&desc);
2468
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002469 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2470 {
2471 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.coma5798952011-12-13 17:30:43 +00002472 renderTarget->Release();
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002473 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002474 }
2475
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002476 HRESULT result;
2477 IDirect3DSurface9 *systemSurface = NULL;
2478 bool directToPixels = getPackReverseRowOrder() && getPackAlignment() <= 4 && mDisplay->isD3d9ExDevice() &&
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00002479 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002480 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2481 if (directToPixels)
2482 {
2483 // Use the pixels ptr as a shared handle to write directly into client's memory
2484 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2485 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2486 if (FAILED(result))
2487 {
2488 // Try again without the shared handle
2489 directToPixels = false;
2490 }
2491 }
2492
2493 if (!directToPixels)
2494 {
2495 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2496 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2497 if (FAILED(result))
2498 {
2499 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com63e6afe2012-05-31 01:14:42 +00002500 renderTarget->Release();
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002501 return error(GL_OUT_OF_MEMORY);
2502 }
2503 }
2504
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002505 result = mDevice->GetRenderTargetData(renderTarget, systemSurface);
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00002506 renderTarget->Release();
daniel@transgaming.coma5798952011-12-13 17:30:43 +00002507 renderTarget = NULL;
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00002508
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002509 if (FAILED(result))
2510 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002511 systemSurface->Release();
2512
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002513 // It turns out that D3D will sometimes produce more error
2514 // codes than those documented.
2515 if (checkDeviceLost(result))
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002516 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002517 else
2518 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002519 UNREACHABLE();
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002520 return;
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002521 }
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002522
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002523 }
2524
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002525 if (directToPixels)
2526 {
2527 systemSurface->Release();
2528 return;
2529 }
2530
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002531 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002532 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2533 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2534 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2535 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2536 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002537
2538 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2539
2540 if (FAILED(result))
2541 {
2542 UNREACHABLE();
2543 systemSurface->Release();
2544
2545 return; // No sensible error to generate
2546 }
2547
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002548 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002549 unsigned short *dest16 = (unsigned short*)pixels;
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002550
2551 unsigned char *source;
2552 int inputPitch;
2553 if (getPackReverseRowOrder())
2554 {
2555 source = (unsigned char*)lock.pBits;
2556 inputPitch = lock.Pitch;
2557 }
2558 else
2559 {
2560 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2561 inputPitch = -lock.Pitch;
2562 }
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002563
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002564 for (int j = 0; j < rect.bottom - rect.top; j++)
2565 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002566 if (desc.Format == D3DFMT_A8R8G8B8 &&
2567 format == GL_BGRA_EXT &&
2568 type == GL_UNSIGNED_BYTE)
2569 {
2570 // Fast path for EXT_read_format_bgra, given
2571 // an RGBA source buffer. Note that buffers with no
2572 // alpha go through the slow path below.
2573 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002574 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002575 (rect.right - rect.left) * 4);
2576 continue;
2577 }
2578
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002579 for (int i = 0; i < rect.right - rect.left; i++)
2580 {
2581 float r;
2582 float g;
2583 float b;
2584 float a;
2585
2586 switch (desc.Format)
2587 {
2588 case D3DFMT_R5G6B5:
2589 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002590 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002591
2592 a = 1.0f;
2593 b = (rgb & 0x001F) * (1.0f / 0x001F);
2594 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2595 r = (rgb & 0xF800) * (1.0f / 0xF800);
2596 }
2597 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002598 case D3DFMT_A1R5G5B5:
2599 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002600 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002601
2602 a = (argb & 0x8000) ? 1.0f : 0.0f;
2603 b = (argb & 0x001F) * (1.0f / 0x001F);
2604 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2605 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2606 }
2607 break;
2608 case D3DFMT_A8R8G8B8:
2609 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002610 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002611
2612 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2613 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2614 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2615 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2616 }
2617 break;
2618 case D3DFMT_X8R8G8B8:
2619 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002620 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002621
2622 a = 1.0f;
2623 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2624 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2625 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2626 }
2627 break;
2628 case D3DFMT_A2R10G10B10:
2629 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002630 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002631
2632 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2633 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2634 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2635 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2636 }
2637 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002638 case D3DFMT_A32B32G32R32F:
2639 {
2640 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002641 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2642 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2643 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2644 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002645 }
2646 break;
2647 case D3DFMT_A16B16G16R16F:
2648 {
2649 // float formats in D3D are stored rgba, rather than the other way round
2650 float abgr[4];
2651
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002652 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002653
2654 a = abgr[3];
2655 b = abgr[2];
2656 g = abgr[1];
2657 r = abgr[0];
2658 }
2659 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002660 default:
2661 UNIMPLEMENTED(); // FIXME
2662 UNREACHABLE();
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00002663 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002664 }
2665
2666 switch (format)
2667 {
2668 case GL_RGBA:
2669 switch (type)
2670 {
2671 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002672 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2673 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2674 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2675 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002676 break;
2677 default: UNREACHABLE();
2678 }
2679 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002680 case GL_BGRA_EXT:
2681 switch (type)
2682 {
2683 case GL_UNSIGNED_BYTE:
2684 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2685 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2686 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2687 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2688 break;
2689 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2690 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2691 // this type is packed as follows:
2692 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2693 // --------------------------------------------------------------------------------
2694 // | 4th | 3rd | 2nd | 1st component |
2695 // --------------------------------------------------------------------------------
2696 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2697 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2698 ((unsigned short)(15 * a + 0.5f) << 12)|
2699 ((unsigned short)(15 * r + 0.5f) << 8) |
2700 ((unsigned short)(15 * g + 0.5f) << 4) |
2701 ((unsigned short)(15 * b + 0.5f) << 0);
2702 break;
2703 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2704 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2705 // this type is packed as follows:
2706 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2707 // --------------------------------------------------------------------------------
2708 // | 4th | 3rd | 2nd | 1st component |
2709 // --------------------------------------------------------------------------------
2710 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2711 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2712 ((unsigned short)( a + 0.5f) << 15) |
2713 ((unsigned short)(31 * r + 0.5f) << 10) |
2714 ((unsigned short)(31 * g + 0.5f) << 5) |
2715 ((unsigned short)(31 * b + 0.5f) << 0);
2716 break;
2717 default: UNREACHABLE();
2718 }
2719 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002720 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002721 switch (type)
2722 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002723 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002724 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2725 ((unsigned short)(31 * b + 0.5f) << 0) |
2726 ((unsigned short)(63 * g + 0.5f) << 5) |
2727 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002728 break;
2729 default: UNREACHABLE();
2730 }
2731 break;
2732 default: UNREACHABLE();
2733 }
2734 }
2735 }
2736
2737 systemSurface->UnlockRect();
2738
2739 systemSurface->Release();
2740}
2741
2742void Context::clear(GLbitfield mask)
2743{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002744 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002745
2746 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2747 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002748 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002749 }
2750
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002751 DWORD flags = 0;
2752
2753 if (mask & GL_COLOR_BUFFER_BIT)
2754 {
2755 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002756
2757 if (framebufferObject->getColorbufferType() != GL_NONE)
2758 {
2759 flags |= D3DCLEAR_TARGET;
2760 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002761 }
2762
2763 if (mask & GL_DEPTH_BUFFER_BIT)
2764 {
2765 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002766 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002767 {
2768 flags |= D3DCLEAR_ZBUFFER;
2769 }
2770 }
2771
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002772 GLuint stencilUnmasked = 0x0;
2773
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002774 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002775 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002776 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002777 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002778 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002779 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002780 if (!depthStencil)
2781 {
2782 ERR("Depth stencil pointer unexpectedly null.");
2783 return;
2784 }
2785
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002786 D3DSURFACE_DESC desc;
2787 depthStencil->GetDesc(&desc);
daniel@transgaming.com63e6afe2012-05-31 01:14:42 +00002788 depthStencil->Release();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002789
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002790 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002791 stencilUnmasked = (0x1 << stencilSize) - 1;
2792
2793 if (stencilUnmasked != 0x0)
2794 {
2795 flags |= D3DCLEAR_STENCIL;
2796 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002797 }
2798 }
2799
2800 if (mask != 0)
2801 {
2802 return error(GL_INVALID_VALUE);
2803 }
2804
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002805 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2806 {
2807 return;
2808 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002809
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002810 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002811 unorm<8>(mState.colorClearValue.red),
2812 unorm<8>(mState.colorClearValue.green),
2813 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002814 float depth = clamp01(mState.depthClearValue);
2815 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002816
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002817 bool alphaUnmasked = (dx2es::GetAlphaSize(mRenderTargetDesc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002818
2819 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002820 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002821 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002822 !(mState.colorMaskRed && mState.colorMaskGreen &&
2823 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002824
2825 if (needMaskedColorClear || needMaskedStencilClear)
2826 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002827 // State which is altered in all paths from this point to the clear call is saved.
2828 // State which is altered in only some paths will be flagged dirty in the case that
2829 // that path is taken.
2830 HRESULT hr;
2831 if (mMaskedClearSavedState == NULL)
2832 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002833 hr = mDevice->BeginStateBlock();
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002834 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2835
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002836 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2837 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2838 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2839 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2840 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2841 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2842 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2843 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2844 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2845 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2846 mDevice->SetPixelShader(NULL);
2847 mDevice->SetVertexShader(NULL);
2848 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2849 mDevice->SetStreamSource(0, NULL, 0, 0);
2850 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2851 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2852 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2853 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2854 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2855 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2856 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002857
2858 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2859 {
2860 mDevice->SetStreamSourceFreq(i, 1);
2861 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002862
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002863 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002864 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2865 }
2866
2867 ASSERT(mMaskedClearSavedState != NULL);
2868
2869 if (mMaskedClearSavedState != NULL)
2870 {
2871 hr = mMaskedClearSavedState->Capture();
2872 ASSERT(SUCCEEDED(hr));
2873 }
2874
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002875 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2876 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2877 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2878 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2879 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2880 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2881 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2882 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002883
2884 if (flags & D3DCLEAR_TARGET)
2885 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002886 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002887 }
2888 else
2889 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002890 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002891 }
2892
2893 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2894 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002895 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2896 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2897 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2898 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
2899 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
2900 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
2901 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2902 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002903 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002904 }
2905 else
2906 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002907 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002908 }
2909
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002910 mDevice->SetPixelShader(NULL);
2911 mDevice->SetVertexShader(NULL);
2912 mDevice->SetFVF(D3DFVF_XYZRHW);
2913 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2914 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2915 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2916 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2917 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2918 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2919 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002920
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002921 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2922 {
2923 mDevice->SetStreamSourceFreq(i, 1);
2924 }
2925
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002926 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2927 quad[0][0] = -0.5f;
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002928 quad[0][1] = mRenderTargetDesc.Height - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002929 quad[0][2] = 0.0f;
2930 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002931
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002932 quad[1][0] = mRenderTargetDesc.Width - 0.5f;
2933 quad[1][1] = mRenderTargetDesc.Height - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002934 quad[1][2] = 0.0f;
2935 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002936
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002937 quad[2][0] = -0.5f;
2938 quad[2][1] = -0.5f;
2939 quad[2][2] = 0.0f;
2940 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002941
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002942 quad[3][0] = mRenderTargetDesc.Width - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002943 quad[3][1] = -0.5f;
2944 quad[3][2] = 0.0f;
2945 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002946
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002947 mDisplay->startScene();
2948 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002949
2950 if (flags & D3DCLEAR_ZBUFFER)
2951 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002952 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
2953 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2954 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002955 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002956
2957 if (mMaskedClearSavedState != NULL)
2958 {
2959 mMaskedClearSavedState->Apply();
2960 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002961 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002962 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002963 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002964 mDevice->Clear(0, NULL, flags, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002965 }
2966}
2967
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002968void Context::drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002969{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002970 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002971 {
2972 return error(GL_INVALID_OPERATION);
2973 }
2974
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002975 D3DPRIMITIVETYPE primitiveType;
2976 int primitiveCount;
2977
2978 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2979 return error(GL_INVALID_ENUM);
2980
2981 if (primitiveCount <= 0)
2982 {
2983 return;
2984 }
2985
2986 if (!applyRenderTarget(false))
2987 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002988 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002989 }
2990
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002991 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002992
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002993 GLsizei repeatDraw = 1;
2994 GLenum err = applyVertexBuffer(first, count, instances, &repeatDraw);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002995 if (err != GL_NO_ERROR)
2996 {
2997 return error(err);
2998 }
2999
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003000 applyShaders();
3001 applyTextures();
3002
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00003003 if (!getCurrentProgram()->getProgramBinary()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00003004 {
3005 return error(GL_INVALID_OPERATION);
3006 }
3007
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003008 if (!cullSkipsDraw(mode))
3009 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003010 mDisplay->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00003011
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003012 if (mode == GL_LINE_LOOP)
daniel@transgaming.comf6549452012-01-27 15:39:08 +00003013 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003014 drawLineLoop(count, GL_NONE, NULL, 0);
daniel@transgaming.comf6549452012-01-27 15:39:08 +00003015 }
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003016 else if (instances > 0)
daniel@transgaming.comf6549452012-01-27 15:39:08 +00003017 {
3018 StaticIndexBuffer *countingIB = mIndexDataManager->getCountingIndices(count);
3019 if (countingIB)
3020 {
3021 if (mAppliedIBSerial != countingIB->getSerial())
3022 {
3023 mDevice->SetIndices(countingIB->getBuffer());
3024 mAppliedIBSerial = countingIB->getSerial();
3025 }
3026
daniel@transgaming.comd6449312012-01-27 15:39:32 +00003027 for (int i = 0; i < repeatDraw; i++)
3028 {
3029 mDevice->DrawIndexedPrimitive(primitiveType, 0, 0, count, 0, primitiveCount);
3030 }
daniel@transgaming.comf6549452012-01-27 15:39:08 +00003031 }
3032 else
3033 {
3034 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
3035 return error(GL_OUT_OF_MEMORY);
3036 }
3037 }
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003038 else // Regular case
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003039 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003040 mDevice->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003041 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003042 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003043}
3044
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00003045void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, GLsizei instances)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003046{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003047 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003048 {
3049 return error(GL_INVALID_OPERATION);
3050 }
3051
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003052 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003053 {
3054 return error(GL_INVALID_OPERATION);
3055 }
3056
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003057 D3DPRIMITIVETYPE primitiveType;
3058 int primitiveCount;
3059
3060 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
3061 return error(GL_INVALID_ENUM);
3062
3063 if (primitiveCount <= 0)
3064 {
3065 return;
3066 }
3067
3068 if (!applyRenderTarget(false))
3069 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00003070 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003071 }
3072
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003073 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00003074
3075 TranslatedIndexData indexInfo;
3076 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
3077 if (err != GL_NO_ERROR)
3078 {
3079 return error(err);
3080 }
3081
daniel@transgaming.com83921382011-01-08 05:46:00 +00003082 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
daniel@transgaming.comd6449312012-01-27 15:39:32 +00003083 GLsizei repeatDraw = 1;
3084 err = applyVertexBuffer(indexInfo.minIndex, vertexCount, instances, &repeatDraw);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00003085 if (err != GL_NO_ERROR)
3086 {
3087 return error(err);
3088 }
3089
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003090 applyShaders();
3091 applyTextures();
3092
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00003093 if (!getCurrentProgram()->getProgramBinary()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00003094 {
3095 return error(GL_INVALID_OPERATION);
3096 }
3097
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003098 if (!cullSkipsDraw(mode))
3099 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003100 mDisplay->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003101
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003102 if (mode == GL_LINE_LOOP)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003103 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003104 drawLineLoop(count, type, indices, indexInfo.minIndex);
3105 }
3106 else
3107 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00003108 for (int i = 0; i < repeatDraw; i++)
3109 {
3110 mDevice->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
3111 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003112 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003113 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003114}
3115
daniel@transgaming.com0d86aa72011-10-26 02:35:10 +00003116// Implements glFlush when block is false, glFinish when block is true
3117void Context::sync(bool block)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003118{
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +00003119 mDisplay->sync(block);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003120}
3121
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003122void Context::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003123{
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003124 // Get the raw indices for an indexed draw
3125 if (type != GL_NONE && mState.elementArrayBuffer.get())
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003126 {
3127 Buffer *indexBuffer = mState.elementArrayBuffer.get();
3128 intptr_t offset = reinterpret_cast<intptr_t>(indices);
3129 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
3130 }
3131
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003132 UINT startIndex = 0;
3133 bool succeeded = false;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003134
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003135 if (supports32bitIndices())
3136 {
3137 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
3138
3139 if (!mLineLoopIB)
3140 {
3141 mLineLoopIB = new StreamingIndexBuffer(mDevice, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
3142 }
3143
3144 if (mLineLoopIB)
3145 {
3146 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
3147
3148 UINT offset = 0;
3149 unsigned int *data = static_cast<unsigned int*>(mLineLoopIB->map(spaceNeeded, &offset));
3150 startIndex = offset / 4;
3151
3152 if (data)
3153 {
3154 switch (type)
3155 {
3156 case GL_NONE: // Non-indexed draw
3157 for (int i = 0; i < count; i++)
3158 {
3159 data[i] = i;
3160 }
3161 data[count] = 0;
3162 break;
3163 case GL_UNSIGNED_BYTE:
3164 for (int i = 0; i < count; i++)
3165 {
3166 data[i] = static_cast<const GLubyte*>(indices)[i];
3167 }
3168 data[count] = static_cast<const GLubyte*>(indices)[0];
3169 break;
3170 case GL_UNSIGNED_SHORT:
3171 for (int i = 0; i < count; i++)
3172 {
3173 data[i] = static_cast<const GLushort*>(indices)[i];
3174 }
3175 data[count] = static_cast<const GLushort*>(indices)[0];
3176 break;
3177 case GL_UNSIGNED_INT:
3178 for (int i = 0; i < count; i++)
3179 {
3180 data[i] = static_cast<const GLuint*>(indices)[i];
3181 }
3182 data[count] = static_cast<const GLuint*>(indices)[0];
3183 break;
3184 default: UNREACHABLE();
3185 }
3186
3187 mLineLoopIB->unmap();
3188 succeeded = true;
3189 }
3190 }
3191 }
3192 else
3193 {
3194 const int spaceNeeded = (count + 1) * sizeof(unsigned short);
3195
3196 if (!mLineLoopIB)
3197 {
3198 mLineLoopIB = new StreamingIndexBuffer(mDevice, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
3199 }
3200
3201 if (mLineLoopIB)
3202 {
3203 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
3204
3205 UINT offset = 0;
3206 unsigned short *data = static_cast<unsigned short*>(mLineLoopIB->map(spaceNeeded, &offset));
3207 startIndex = offset / 2;
3208
3209 if (data)
3210 {
3211 switch (type)
3212 {
3213 case GL_NONE: // Non-indexed draw
3214 for (int i = 0; i < count; i++)
3215 {
3216 data[i] = i;
3217 }
3218 data[count] = 0;
3219 break;
3220 case GL_UNSIGNED_BYTE:
3221 for (int i = 0; i < count; i++)
3222 {
3223 data[i] = static_cast<const GLubyte*>(indices)[i];
3224 }
3225 data[count] = static_cast<const GLubyte*>(indices)[0];
3226 break;
3227 case GL_UNSIGNED_SHORT:
3228 for (int i = 0; i < count; i++)
3229 {
3230 data[i] = static_cast<const GLushort*>(indices)[i];
3231 }
3232 data[count] = static_cast<const GLushort*>(indices)[0];
3233 break;
3234 case GL_UNSIGNED_INT:
3235 for (int i = 0; i < count; i++)
3236 {
3237 data[i] = static_cast<const GLuint*>(indices)[i];
3238 }
3239 data[count] = static_cast<const GLuint*>(indices)[0];
3240 break;
3241 default: UNREACHABLE();
3242 }
3243
3244 mLineLoopIB->unmap();
3245 succeeded = true;
3246 }
3247 }
3248 }
3249
3250 if (succeeded)
3251 {
3252 if (mAppliedIBSerial != mLineLoopIB->getSerial())
3253 {
3254 mDevice->SetIndices(mLineLoopIB->getBuffer());
3255 mAppliedIBSerial = mLineLoopIB->getSerial();
3256 }
3257
3258 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
3259 }
3260 else
3261 {
3262 ERR("Could not create a looping index buffer for GL_LINE_LOOP.");
3263 return error(GL_OUT_OF_MEMORY);
3264 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003265}
3266
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003267void Context::recordInvalidEnum()
3268{
3269 mInvalidEnum = true;
3270}
3271
3272void Context::recordInvalidValue()
3273{
3274 mInvalidValue = true;
3275}
3276
3277void Context::recordInvalidOperation()
3278{
3279 mInvalidOperation = true;
3280}
3281
3282void Context::recordOutOfMemory()
3283{
3284 mOutOfMemory = true;
3285}
3286
3287void Context::recordInvalidFramebufferOperation()
3288{
3289 mInvalidFramebufferOperation = true;
3290}
3291
3292// Get one of the recorded errors and clear its flag, if any.
3293// [OpenGL ES 2.0.24] section 2.5 page 13.
3294GLenum Context::getError()
3295{
3296 if (mInvalidEnum)
3297 {
3298 mInvalidEnum = false;
3299
3300 return GL_INVALID_ENUM;
3301 }
3302
3303 if (mInvalidValue)
3304 {
3305 mInvalidValue = false;
3306
3307 return GL_INVALID_VALUE;
3308 }
3309
3310 if (mInvalidOperation)
3311 {
3312 mInvalidOperation = false;
3313
3314 return GL_INVALID_OPERATION;
3315 }
3316
3317 if (mOutOfMemory)
3318 {
3319 mOutOfMemory = false;
3320
3321 return GL_OUT_OF_MEMORY;
3322 }
3323
3324 if (mInvalidFramebufferOperation)
3325 {
3326 mInvalidFramebufferOperation = false;
3327
3328 return GL_INVALID_FRAMEBUFFER_OPERATION;
3329 }
3330
3331 return GL_NO_ERROR;
3332}
3333
daniel@transgaming.com17f548c2011-11-09 17:47:02 +00003334GLenum Context::getResetStatus()
3335{
3336 if (mResetStatus == GL_NO_ERROR)
3337 {
3338 bool lost = mDisplay->testDeviceLost();
3339
3340 if (lost)
3341 {
3342 mDisplay->notifyDeviceLost(); // Sets mResetStatus
3343 }
3344 }
3345
3346 GLenum status = mResetStatus;
3347
3348 if (mResetStatus != GL_NO_ERROR)
3349 {
3350 if (mDisplay->testDeviceResettable())
3351 {
3352 mResetStatus = GL_NO_ERROR;
3353 }
3354 }
3355
3356 return status;
3357}
3358
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00003359bool Context::isResetNotificationEnabled()
3360{
3361 return (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT);
3362}
3363
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003364bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003365{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003366 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003367}
3368
daniel@transgaming.com396c6432010-11-26 16:26:12 +00003369int Context::getMaximumVaryingVectors() const
3370{
3371 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3372}
3373
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003374unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003375{
3376 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3377}
3378
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003379unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003380{
3381 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3382}
3383
daniel@transgaming.com458da142010-11-28 02:03:02 +00003384int Context::getMaximumFragmentUniformVectors() const
3385{
3386 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3387}
3388
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003389int Context::getMaxSupportedSamples() const
3390{
3391 return mMaxSupportedSamples;
3392}
3393
3394int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3395{
3396 if (requested == 0)
3397 {
3398 return requested;
3399 }
3400
3401 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3402 if (itr == mMultiSampleSupport.end())
3403 {
3404 return -1;
3405 }
3406
3407 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3408 {
3409 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3410 {
3411 return i;
3412 }
3413 }
3414
3415 return -1;
3416}
3417
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003418bool Context::supportsEventQueries() const
3419{
3420 return mSupportsEventQueries;
3421}
3422
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00003423bool Context::supportsOcclusionQueries() const
3424{
3425 return mSupportsOcclusionQueries;
3426}
3427
gman@chromium.org50c526d2011-08-10 05:19:44 +00003428bool Context::supportsDXT1Textures() const
daniel@transgaming.com01868132010-08-24 19:21:17 +00003429{
gman@chromium.org50c526d2011-08-10 05:19:44 +00003430 return mSupportsDXT1Textures;
3431}
3432
3433bool Context::supportsDXT3Textures() const
3434{
3435 return mSupportsDXT3Textures;
3436}
3437
3438bool Context::supportsDXT5Textures() const
3439{
3440 return mSupportsDXT5Textures;
daniel@transgaming.com01868132010-08-24 19:21:17 +00003441}
3442
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003443bool Context::supportsFloat32Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003444{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003445 return mSupportsFloat32Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003446}
3447
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003448bool Context::supportsFloat32LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003449{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003450 return mSupportsFloat32LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003451}
3452
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003453bool Context::supportsFloat32RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003454{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003455 return mSupportsFloat32RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003456}
3457
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003458bool Context::supportsFloat16Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003459{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003460 return mSupportsFloat16Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003461}
3462
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003463bool Context::supportsFloat16LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003464{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003465 return mSupportsFloat16LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003466}
3467
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003468bool Context::supportsFloat16RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003469{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003470 return mSupportsFloat16RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003471}
3472
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003473int Context::getMaximumRenderbufferDimension() const
3474{
3475 return mMaxRenderbufferDimension;
3476}
3477
3478int Context::getMaximumTextureDimension() const
3479{
3480 return mMaxTextureDimension;
3481}
3482
3483int Context::getMaximumCubeTextureDimension() const
3484{
3485 return mMaxCubeTextureDimension;
3486}
3487
3488int Context::getMaximumTextureLevel() const
3489{
3490 return mMaxTextureLevel;
3491}
3492
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003493bool Context::supportsLuminanceTextures() const
3494{
3495 return mSupportsLuminanceTextures;
3496}
3497
3498bool Context::supportsLuminanceAlphaTextures() const
3499{
3500 return mSupportsLuminanceAlphaTextures;
3501}
3502
daniel@transgaming.com1c49f792012-05-31 01:14:02 +00003503bool Context::supportsDepthTextures() const
3504{
3505 return mSupportsDepthTextures;
3506}
3507
daniel@transgaming.com83921382011-01-08 05:46:00 +00003508bool Context::supports32bitIndices() const
3509{
3510 return mSupports32bitIndices;
3511}
3512
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003513bool Context::supportsNonPower2Texture() const
3514{
3515 return mSupportsNonPower2Texture;
3516}
3517
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +00003518bool Context::supportsInstancing() const
3519{
3520 return mSupportsInstancing;
3521}
3522
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003523void Context::detachBuffer(GLuint buffer)
3524{
3525 // [OpenGL ES 2.0.24] section 2.9 page 22:
3526 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3527 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3528
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003529 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003530 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003531 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003532 }
3533
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003534 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003535 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003536 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003537 }
3538
3539 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3540 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003541 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003542 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003543 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003544 }
3545 }
3546}
3547
3548void Context::detachTexture(GLuint texture)
3549{
3550 // [OpenGL ES 2.0.24] section 3.8 page 84:
3551 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3552 // rebound to texture object zero
3553
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003554 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003555 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003556 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003557 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003558 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003559 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003560 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003561 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003562 }
3563 }
3564
3565 // [OpenGL ES 2.0.24] section 4.4 page 112:
3566 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3567 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3568 // image was attached in the currently bound framebuffer.
3569
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003570 Framebuffer *readFramebuffer = getReadFramebuffer();
3571 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003572
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003573 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003574 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003575 readFramebuffer->detachTexture(texture);
3576 }
3577
3578 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3579 {
3580 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003581 }
3582}
3583
3584void Context::detachFramebuffer(GLuint framebuffer)
3585{
3586 // [OpenGL ES 2.0.24] section 4.4 page 107:
3587 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3588 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3589
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003590 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003591 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003592 bindReadFramebuffer(0);
3593 }
3594
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003595 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003596 {
3597 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003598 }
3599}
3600
3601void Context::detachRenderbuffer(GLuint renderbuffer)
3602{
3603 // [OpenGL ES 2.0.24] section 4.4 page 109:
3604 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3605 // had been executed with the target RENDERBUFFER and name of zero.
3606
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003607 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003608 {
3609 bindRenderbuffer(0);
3610 }
3611
3612 // [OpenGL ES 2.0.24] section 4.4 page 111:
3613 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3614 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3615 // point to which this image was attached in the currently bound framebuffer.
3616
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003617 Framebuffer *readFramebuffer = getReadFramebuffer();
3618 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003619
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003620 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003621 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003622 readFramebuffer->detachRenderbuffer(renderbuffer);
3623 }
3624
3625 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3626 {
3627 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003628 }
3629}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003630
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003631Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003632{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003633 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003634
3635 if (t == NULL)
3636 {
3637 static const GLubyte color[] = { 0, 0, 0, 255 };
3638
3639 switch (type)
3640 {
3641 default:
3642 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003643 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003644
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003645 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003646 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003647 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003648 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003649 t = incomplete2d;
3650 }
3651 break;
3652
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003653 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003654 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003655 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003656
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003657 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3658 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3659 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3660 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3661 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3662 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003663
3664 t = incompleteCube;
3665 }
3666 break;
3667 }
3668
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003669 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003670 }
3671
3672 return t;
3673}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003674
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003675bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003676{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003677 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003678}
3679
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003680bool Context::isTriangleMode(GLenum drawMode)
3681{
3682 switch (drawMode)
3683 {
3684 case GL_TRIANGLES:
3685 case GL_TRIANGLE_FAN:
3686 case GL_TRIANGLE_STRIP:
3687 return true;
3688 case GL_POINTS:
3689 case GL_LINES:
3690 case GL_LINE_LOOP:
3691 case GL_LINE_STRIP:
3692 return false;
3693 default: UNREACHABLE();
3694 }
3695
3696 return false;
3697}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003698
3699void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3700{
3701 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3702
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003703 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3704 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3705 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3706 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003707
daniel@transgaming.com83921382011-01-08 05:46:00 +00003708 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003709}
3710
daniel@transgaming.comd2820bf2012-01-27 15:38:48 +00003711void Context::setVertexAttribDivisor(GLuint index, GLuint divisor)
3712{
3713 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3714
3715 mState.vertexAttribute[index].mDivisor = divisor;
3716}
3717
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003718// keep list sorted in following order
3719// OES extensions
3720// EXT extensions
3721// Vendor extensions
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003722void Context::initExtensionString()
3723{
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003724 mExtensionString = "";
3725
3726 // OES extensions
3727 if (supports32bitIndices())
3728 {
3729 mExtensionString += "GL_OES_element_index_uint ";
3730 }
3731
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003732 mExtensionString += "GL_OES_packed_depth_stencil ";
apatrick@chromium.org3ce8dbc2012-06-08 17:52:30 +00003733 //mExtensionString += "GL_OES_get_program_binary ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003734 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003735 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003736
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003737 if (supportsFloat16Textures())
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003738 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003739 mExtensionString += "GL_OES_texture_half_float ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003740 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003741 if (supportsFloat16LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003742 {
3743 mExtensionString += "GL_OES_texture_half_float_linear ";
3744 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003745 if (supportsFloat32Textures())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003746 {
3747 mExtensionString += "GL_OES_texture_float ";
3748 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003749 if (supportsFloat32LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003750 {
3751 mExtensionString += "GL_OES_texture_float_linear ";
3752 }
3753
3754 if (supportsNonPower2Texture())
3755 {
3756 mExtensionString += "GL_OES_texture_npot ";
3757 }
3758
3759 // Multi-vendor (EXT) extensions
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00003760 if (supportsOcclusionQueries())
3761 {
3762 mExtensionString += "GL_EXT_occlusion_query_boolean ";
3763 }
3764
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003765 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com8747f182011-11-09 17:50:38 +00003766 mExtensionString += "GL_EXT_robustness ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003767
gman@chromium.org50c526d2011-08-10 05:19:44 +00003768 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00003769 {
3770 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3771 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003772
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003773 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
daniel@transgaming.comdf363722011-12-16 23:29:53 +00003774 mExtensionString += "GL_EXT_texture_storage ";
gman@chromium.org50c526d2011-08-10 05:19:44 +00003775
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003776 // ANGLE-specific extensions
daniel@transgaming.com92d620c2012-05-31 01:16:31 +00003777 if (supportsDepthTextures())
3778 {
3779 mExtensionString += "GL_ANGLE_depth_texture ";
3780 }
3781
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003782 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003783 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003784 {
3785 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3786 }
3787
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +00003788 if (supportsInstancing())
daniel@transgaming.comdce02fd2012-01-27 15:39:51 +00003789 {
3790 mExtensionString += "GL_ANGLE_instanced_arrays ";
3791 }
3792
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00003793 mExtensionString += "GL_ANGLE_pack_reverse_row_order ";
3794
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003795 if (supportsDXT3Textures())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003796 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003797 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3798 }
3799 if (supportsDXT5Textures())
3800 {
3801 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003802 }
daniel@transgaming.com97412f72011-11-11 04:19:07 +00003803
3804 mExtensionString += "GL_ANGLE_texture_usage ";
zmo@google.coma574f782011-10-03 21:45:23 +00003805 mExtensionString += "GL_ANGLE_translated_shader_source ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003806
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003807 // Other vendor-specific extensions
3808 if (supportsEventQueries())
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003809 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003810 mExtensionString += "GL_NV_fence ";
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003811 }
3812
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003813 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3814 if (end != std::string::npos)
3815 {
3816 mExtensionString.resize(end+1);
3817 }
3818}
3819
3820const char *Context::getExtensionString() const
3821{
3822 return mExtensionString.c_str();
3823}
3824
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003825void Context::initRendererString()
3826{
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003827 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003828
3829 mRendererString = "ANGLE (";
3830 mRendererString += identifier->Description;
3831 mRendererString += ")";
3832}
3833
3834const char *Context::getRendererString() const
3835{
3836 return mRendererString.c_str();
3837}
3838
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003839void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3840 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3841 GLbitfield mask)
3842{
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003843 Framebuffer *readFramebuffer = getReadFramebuffer();
3844 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3845
3846 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3847 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3848 {
3849 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3850 }
3851
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003852 if (drawFramebuffer->getSamples() != 0)
3853 {
3854 return error(GL_INVALID_OPERATION);
3855 }
3856
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003857 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3858 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3859 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3860 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3861
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003862 RECT sourceRect;
3863 RECT destRect;
3864
3865 if (srcX0 < srcX1)
3866 {
3867 sourceRect.left = srcX0;
3868 sourceRect.right = srcX1;
3869 destRect.left = dstX0;
3870 destRect.right = dstX1;
3871 }
3872 else
3873 {
3874 sourceRect.left = srcX1;
3875 destRect.left = dstX1;
3876 sourceRect.right = srcX0;
3877 destRect.right = dstX0;
3878 }
3879
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003880 if (srcY0 < srcY1)
3881 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003882 sourceRect.top = readBufferHeight - srcY1;
3883 destRect.top = drawBufferHeight - dstY1;
3884 sourceRect.bottom = readBufferHeight - srcY0;
3885 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003886 }
3887 else
3888 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003889 sourceRect.top = readBufferHeight - srcY0;
3890 destRect.top = drawBufferHeight - dstY0;
3891 sourceRect.bottom = readBufferHeight - srcY1;
3892 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003893 }
3894
3895 RECT sourceScissoredRect = sourceRect;
3896 RECT destScissoredRect = destRect;
3897
3898 if (mState.scissorTest)
3899 {
3900 // Only write to parts of the destination framebuffer which pass the scissor test
3901 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3902 // rect will be checked against scissorY, rather than the bottom.
3903 if (destRect.left < mState.scissorX)
3904 {
3905 int xDiff = mState.scissorX - destRect.left;
3906 destScissoredRect.left = mState.scissorX;
3907 sourceScissoredRect.left += xDiff;
3908 }
3909
3910 if (destRect.right > mState.scissorX + mState.scissorWidth)
3911 {
3912 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3913 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3914 sourceScissoredRect.right -= xDiff;
3915 }
3916
3917 if (destRect.top < mState.scissorY)
3918 {
3919 int yDiff = mState.scissorY - destRect.top;
3920 destScissoredRect.top = mState.scissorY;
3921 sourceScissoredRect.top += yDiff;
3922 }
3923
3924 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3925 {
3926 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3927 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3928 sourceScissoredRect.bottom -= yDiff;
3929 }
3930 }
3931
3932 bool blitRenderTarget = false;
3933 bool blitDepthStencil = false;
3934
3935 RECT sourceTrimmedRect = sourceScissoredRect;
3936 RECT destTrimmedRect = destScissoredRect;
3937
3938 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3939 // the actual draw and read surfaces.
3940 if (sourceTrimmedRect.left < 0)
3941 {
3942 int xDiff = 0 - sourceTrimmedRect.left;
3943 sourceTrimmedRect.left = 0;
3944 destTrimmedRect.left += xDiff;
3945 }
3946
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003947 if (sourceTrimmedRect.right > readBufferWidth)
3948 {
3949 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3950 sourceTrimmedRect.right = readBufferWidth;
3951 destTrimmedRect.right -= xDiff;
3952 }
3953
3954 if (sourceTrimmedRect.top < 0)
3955 {
3956 int yDiff = 0 - sourceTrimmedRect.top;
3957 sourceTrimmedRect.top = 0;
3958 destTrimmedRect.top += yDiff;
3959 }
3960
3961 if (sourceTrimmedRect.bottom > readBufferHeight)
3962 {
3963 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3964 sourceTrimmedRect.bottom = readBufferHeight;
3965 destTrimmedRect.bottom -= yDiff;
3966 }
3967
3968 if (destTrimmedRect.left < 0)
3969 {
3970 int xDiff = 0 - destTrimmedRect.left;
3971 destTrimmedRect.left = 0;
3972 sourceTrimmedRect.left += xDiff;
3973 }
3974
3975 if (destTrimmedRect.right > drawBufferWidth)
3976 {
3977 int xDiff = destTrimmedRect.right - drawBufferWidth;
3978 destTrimmedRect.right = drawBufferWidth;
3979 sourceTrimmedRect.right -= xDiff;
3980 }
3981
3982 if (destTrimmedRect.top < 0)
3983 {
3984 int yDiff = 0 - destTrimmedRect.top;
3985 destTrimmedRect.top = 0;
3986 sourceTrimmedRect.top += yDiff;
3987 }
3988
3989 if (destTrimmedRect.bottom > drawBufferHeight)
3990 {
3991 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3992 destTrimmedRect.bottom = drawBufferHeight;
3993 sourceTrimmedRect.bottom -= yDiff;
3994 }
3995
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003996 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003997 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3998 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3999 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
4000 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004001 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
4002 {
4003 partialBufferCopy = true;
4004 }
4005
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004006 if (mask & GL_COLOR_BUFFER_BIT)
4007 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00004008 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
4009 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
4010 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
4011 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
4012 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004013 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
4014 {
4015 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
4016 return error(GL_INVALID_OPERATION);
4017 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004018
4019 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
4020 {
4021 return error(GL_INVALID_OPERATION);
4022 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004023
4024 blitRenderTarget = true;
4025
4026 }
4027
4028 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
4029 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00004030 Renderbuffer *readDSBuffer = NULL;
4031 Renderbuffer *drawDSBuffer = NULL;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004032
4033 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
4034 // both a depth and stencil buffer, it will be the same buffer.
4035
4036 if (mask & GL_DEPTH_BUFFER_BIT)
4037 {
4038 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
4039 {
4040 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
4041 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
4042 {
4043 return error(GL_INVALID_OPERATION);
4044 }
4045
4046 blitDepthStencil = true;
4047 readDSBuffer = readFramebuffer->getDepthbuffer();
4048 drawDSBuffer = drawFramebuffer->getDepthbuffer();
4049 }
4050 }
4051
4052 if (mask & GL_STENCIL_BUFFER_BIT)
4053 {
4054 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
4055 {
4056 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
4057 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
4058 {
4059 return error(GL_INVALID_OPERATION);
4060 }
4061
4062 blitDepthStencil = true;
4063 readDSBuffer = readFramebuffer->getStencilbuffer();
4064 drawDSBuffer = drawFramebuffer->getStencilbuffer();
4065 }
4066 }
4067
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004068 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004069 {
4070 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
4071 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
4072 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004073
daniel@transgaming.com97446d22010-08-24 19:20:54 +00004074 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
4075 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004076 {
4077 return error(GL_INVALID_OPERATION);
4078 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004079 }
4080
4081 if (blitRenderTarget || blitDepthStencil)
4082 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00004083 mDisplay->endScene();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004084
4085 if (blitRenderTarget)
4086 {
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00004087 IDirect3DSurface9* readRenderTarget = readFramebuffer->getRenderTarget();
4088 IDirect3DSurface9* drawRenderTarget = drawFramebuffer->getRenderTarget();
4089
4090 HRESULT result = mDevice->StretchRect(readRenderTarget, &sourceTrimmedRect,
4091 drawRenderTarget, &destTrimmedRect, D3DTEXF_NONE);
4092
4093 readRenderTarget->Release();
4094 drawRenderTarget->Release();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004095
4096 if (FAILED(result))
4097 {
4098 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
4099 return;
4100 }
4101 }
4102
4103 if (blitDepthStencil)
4104 {
daniel@transgaming.com63e6afe2012-05-31 01:14:42 +00004105 IDirect3DSurface9* readDepthStencil = readFramebuffer->getDepthStencil();
4106 IDirect3DSurface9* drawDepthStencil = drawFramebuffer->getDepthStencil();
4107
4108 HRESULT result = mDevice->StretchRect(readDepthStencil, NULL, drawDepthStencil, NULL, D3DTEXF_NONE);
4109
4110 readDepthStencil->Release();
4111 drawDepthStencil->Release();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004112
4113 if (FAILED(result))
4114 {
4115 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
4116 return;
4117 }
4118 }
4119 }
4120}
4121
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004122VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
4123{
4124 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4125 {
4126 mVertexDeclCache[i].vertexDeclaration = NULL;
4127 mVertexDeclCache[i].lruCount = 0;
4128 }
4129}
4130
4131VertexDeclarationCache::~VertexDeclarationCache()
4132{
4133 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4134 {
4135 if (mVertexDeclCache[i].vertexDeclaration)
4136 {
4137 mVertexDeclCache[i].vertexDeclaration->Release();
4138 }
4139 }
4140}
4141
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004142GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], Program *program, GLsizei instances, GLsizei *repeatDraw)
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004143{
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004144 *repeatDraw = 1;
4145
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004146 int indexedAttribute = MAX_VERTEX_ATTRIBS;
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004147 int instancedAttribute = MAX_VERTEX_ATTRIBS;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004148
4149 if (instances > 0)
4150 {
4151 // Find an indexed attribute to be mapped to D3D stream 0
4152 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4153 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004154 if (attributes[i].active)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004155 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004156 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
4157 {
4158 if (attributes[i].divisor == 0)
4159 {
4160 indexedAttribute = i;
4161 }
4162 }
4163 else if (instancedAttribute == MAX_VERTEX_ATTRIBS)
4164 {
4165 if (attributes[i].divisor != 0)
4166 {
4167 instancedAttribute = i;
4168 }
4169 }
4170 else break; // Found both an indexed and instanced attribute
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004171 }
4172 }
4173
4174 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
4175 {
4176 return GL_INVALID_OPERATION;
4177 }
4178 }
4179
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004180 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
4181 D3DVERTEXELEMENT9 *element = &elements[0];
4182
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00004183 ProgramBinary *programBinary = program->getProgramBinary();
4184
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004185 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4186 {
4187 if (attributes[i].active)
4188 {
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004189 int stream = i;
4190
4191 if (instances > 0)
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004192 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004193 // Due to a bug on ATI cards we can't enable instancing when none of the attributes are instanced.
4194 if (instancedAttribute == MAX_VERTEX_ATTRIBS)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004195 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004196 *repeatDraw = instances;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004197 }
4198 else
4199 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004200 if (i == indexedAttribute)
4201 {
4202 stream = 0;
4203 }
4204 else if (i == 0)
4205 {
4206 stream = indexedAttribute;
4207 }
4208
4209 UINT frequency = 1;
4210
4211 if (attributes[i].divisor == 0)
4212 {
4213 frequency = D3DSTREAMSOURCE_INDEXEDDATA | instances;
4214 }
4215 else
4216 {
4217 frequency = D3DSTREAMSOURCE_INSTANCEDATA | attributes[i].divisor;
4218 }
4219
4220 device->SetStreamSourceFreq(stream, frequency);
4221 mInstancingEnabled = true;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004222 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004223 }
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004224
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004225 if (mAppliedVBs[stream].serial != attributes[i].serial ||
4226 mAppliedVBs[stream].stride != attributes[i].stride ||
4227 mAppliedVBs[stream].offset != attributes[i].offset)
4228 {
4229 device->SetStreamSource(stream, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
4230 mAppliedVBs[stream].serial = attributes[i].serial;
4231 mAppliedVBs[stream].stride = attributes[i].stride;
4232 mAppliedVBs[stream].offset = attributes[i].offset;
4233 }
4234
4235 element->Stream = stream;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004236 element->Offset = 0;
4237 element->Type = attributes[i].type;
4238 element->Method = D3DDECLMETHOD_DEFAULT;
4239 element->Usage = D3DDECLUSAGE_TEXCOORD;
apatrick@chromium.orge2a59bb2012-06-07 21:09:53 +00004240 element->UsageIndex = programBinary->getSemanticIndex(i);
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004241 element++;
4242 }
4243 }
4244
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004245 if (instances == 0 || instancedAttribute == MAX_VERTEX_ATTRIBS)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004246 {
4247 if (mInstancingEnabled)
4248 {
4249 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4250 {
4251 device->SetStreamSourceFreq(i, 1);
4252 }
4253
4254 mInstancingEnabled = false;
4255 }
4256 }
4257
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004258 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
4259 *(element++) = end;
4260
4261 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4262 {
4263 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
4264 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
4265 {
4266 entry->lruCount = ++mMaxLru;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004267 if(entry->vertexDeclaration != mLastSetVDecl)
4268 {
4269 device->SetVertexDeclaration(entry->vertexDeclaration);
4270 mLastSetVDecl = entry->vertexDeclaration;
4271 }
4272
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004273 return GL_NO_ERROR;
4274 }
4275 }
4276
4277 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
4278
4279 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4280 {
4281 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
4282 {
4283 lastCache = &mVertexDeclCache[i];
4284 }
4285 }
4286
4287 if (lastCache->vertexDeclaration != NULL)
4288 {
4289 lastCache->vertexDeclaration->Release();
4290 lastCache->vertexDeclaration = NULL;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004291 // mLastSetVDecl is set to the replacement, so we don't have to worry
4292 // about it.
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004293 }
4294
4295 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
4296 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
4297 device->SetVertexDeclaration(lastCache->vertexDeclaration);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004298 mLastSetVDecl = lastCache->vertexDeclaration;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004299 lastCache->lruCount = ++mMaxLru;
4300
4301 return GL_NO_ERROR;
4302}
4303
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004304void VertexDeclarationCache::markStateDirty()
4305{
4306 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4307 {
4308 mAppliedVBs[i].serial = 0;
4309 }
4310
4311 mLastSetVDecl = NULL;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004312 mInstancingEnabled = true; // Forces it to be disabled when not used
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004313}
4314
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004315}
4316
4317extern "C"
4318{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00004319gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext, bool notifyResets, bool robustAccess)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004320{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00004321 return new gl::Context(config, shareContext, notifyResets, robustAccess);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004322}
4323
4324void glDestroyContext(gl::Context *context)
4325{
4326 delete context;
4327
4328 if (context == gl::getContext())
4329 {
4330 gl::makeCurrent(NULL, NULL, NULL);
4331 }
4332}
4333
4334void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
4335{
4336 gl::makeCurrent(context, display, surface);
4337}
4338
4339gl::Context *glGetCurrentContext()
4340{
4341 return gl::getContext();
4342}
4343}