blob: b3951302c65f6b682549655d060232a47fd92658 [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"
daniel@transgaming.com86bdb822012-01-20 18:24:39 +000025#include "libGLESv2/Query.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000026#include "libGLESv2/RenderBuffer.h"
27#include "libGLESv2/Shader.h"
28#include "libGLESv2/Texture.h"
daniel@transgaming.com8fd34bd2011-02-18 02:52:14 +000029#include "libGLESv2/VertexDataManager.h"
30#include "libGLESv2/IndexDataManager.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031
daniel@transgaming.com86487c22010-03-11 19:41:43 +000032#undef near
33#undef far
34
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000035namespace gl
36{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +000037Context::Context(const egl::Config *config, const gl::Context *shareContext, bool notifyResets, bool robustAccess) : mConfig(config)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000038{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +000039 ASSERT(robustAccess == false); // Unimplemented
40
daniel@transgaming.comc941e252011-10-26 02:32:31 +000041 mDisplay = NULL;
42 mDevice = NULL;
43
benvanik@google.com1a233342011-04-28 19:44:39 +000044 mFenceHandleAllocator.setBaseHandle(0);
45
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000046 setClearColor(0.0f, 0.0f, 0.0f, 0.0f);
daniel@transgaming.com092bd482010-05-12 03:39:36 +000047
daniel@transgaming.com428d1582010-05-04 03:35:25 +000048 mState.depthClearValue = 1.0f;
49 mState.stencilClearValue = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000050
daniel@transgaming.com428d1582010-05-04 03:35:25 +000051 mState.cullFace = false;
52 mState.cullMode = GL_BACK;
53 mState.frontFace = GL_CCW;
54 mState.depthTest = false;
55 mState.depthFunc = GL_LESS;
56 mState.blend = false;
57 mState.sourceBlendRGB = GL_ONE;
58 mState.sourceBlendAlpha = GL_ONE;
59 mState.destBlendRGB = GL_ZERO;
60 mState.destBlendAlpha = GL_ZERO;
61 mState.blendEquationRGB = GL_FUNC_ADD;
62 mState.blendEquationAlpha = GL_FUNC_ADD;
63 mState.blendColor.red = 0;
64 mState.blendColor.green = 0;
65 mState.blendColor.blue = 0;
66 mState.blendColor.alpha = 0;
67 mState.stencilTest = false;
68 mState.stencilFunc = GL_ALWAYS;
69 mState.stencilRef = 0;
70 mState.stencilMask = -1;
71 mState.stencilWritemask = -1;
72 mState.stencilBackFunc = GL_ALWAYS;
73 mState.stencilBackRef = 0;
74 mState.stencilBackMask = - 1;
75 mState.stencilBackWritemask = -1;
76 mState.stencilFail = GL_KEEP;
77 mState.stencilPassDepthFail = GL_KEEP;
78 mState.stencilPassDepthPass = GL_KEEP;
79 mState.stencilBackFail = GL_KEEP;
80 mState.stencilBackPassDepthFail = GL_KEEP;
81 mState.stencilBackPassDepthPass = GL_KEEP;
82 mState.polygonOffsetFill = false;
83 mState.polygonOffsetFactor = 0.0f;
84 mState.polygonOffsetUnits = 0.0f;
85 mState.sampleAlphaToCoverage = false;
86 mState.sampleCoverage = false;
87 mState.sampleCoverageValue = 1.0f;
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +000088 mState.sampleCoverageInvert = false;
daniel@transgaming.com428d1582010-05-04 03:35:25 +000089 mState.scissorTest = false;
90 mState.dither = true;
91 mState.generateMipmapHint = GL_DONT_CARE;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +000092 mState.fragmentShaderDerivativeHint = GL_DONT_CARE;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000093
daniel@transgaming.com428d1582010-05-04 03:35:25 +000094 mState.lineWidth = 1.0f;
daniel@transgaming.com32e58cd2010-03-24 09:44:10 +000095
daniel@transgaming.com428d1582010-05-04 03:35:25 +000096 mState.viewportX = 0;
97 mState.viewportY = 0;
98 mState.viewportWidth = config->mDisplayMode.Width;
99 mState.viewportHeight = config->mDisplayMode.Height;
100 mState.zNear = 0.0f;
101 mState.zFar = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000103 mState.scissorX = 0;
104 mState.scissorY = 0;
105 mState.scissorWidth = config->mDisplayMode.Width;
106 mState.scissorHeight = config->mDisplayMode.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000107
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000108 mState.colorMaskRed = true;
109 mState.colorMaskGreen = true;
110 mState.colorMaskBlue = true;
111 mState.colorMaskAlpha = true;
112 mState.depthMask = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000113
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000114 if (shareContext != NULL)
115 {
116 mResourceManager = shareContext->mResourceManager;
117 mResourceManager->addRef();
118 }
119 else
120 {
121 mResourceManager = new ResourceManager();
122 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000123
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000124 // [OpenGL ES 2.0.24] section 3.7 page 83:
125 // In the initial state, TEXTURE_2D and TEXTURE_CUBE_MAP have twodimensional
126 // and cube map texture state vectors respectively associated with them.
127 // In order that access to these initial textures not be lost, they are treated as texture
128 // objects all of whose names are 0.
129
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000130 mTexture2DZero.set(new Texture2D(0));
131 mTextureCubeMapZero.set(new TextureCubeMap(0));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000132
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000133 mState.activeSampler = 0;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000134 bindArrayBuffer(0);
135 bindElementArrayBuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136 bindTextureCubeMap(0);
137 bindTexture2D(0);
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000138 bindReadFramebuffer(0);
139 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000140 bindRenderbuffer(0);
141
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000142 mState.currentProgram = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000143
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000144 mState.packAlignment = 4;
145 mState.unpackAlignment = 4;
bsalomon@google.com56d46ab2011-11-23 14:53:10 +0000146 mState.packReverseRowOrder = false;
daniel@transgaming.com3489e3a2010-03-21 04:31:11 +0000147
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000148 mVertexDataManager = NULL;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000149 mIndexDataManager = NULL;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000150 mBlit = NULL;
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +0000151 mLineLoopIB = NULL;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000152
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000153 mInvalidEnum = false;
154 mInvalidValue = false;
155 mInvalidOperation = false;
156 mOutOfMemory = false;
157 mInvalidFramebufferOperation = false;
daniel@transgaming.com159acdf2010-03-21 04:31:24 +0000158
159 mHasBeenCurrent = false;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000160 mContextLost = false;
daniel@transgaming.com17f548c2011-11-09 17:47:02 +0000161 mResetStatus = GL_NO_ERROR;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +0000162 mResetStrategy = (notifyResets ? GL_LOSE_CONTEXT_ON_RESET_EXT : GL_NO_RESET_NOTIFICATION_EXT);
163 mRobustAccess = robustAccess;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000164
gman@chromium.org50c526d2011-08-10 05:19:44 +0000165 mSupportsDXT1Textures = false;
166 mSupportsDXT3Textures = false;
167 mSupportsDXT5Textures = false;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000168 mSupportsEventQueries = false;
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000169 mSupportsOcclusionQueries = false;
gman@chromium.org50c526d2011-08-10 05:19:44 +0000170 mNumCompressedTextureFormats = 0;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000171 mMaxSupportedSamples = 0;
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000172 mMaskedClearSavedState = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000173 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000174}
175
176Context::~Context()
177{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000178 if (mState.currentProgram != 0)
179 {
180 Program *programObject = mResourceManager->getProgram(mState.currentProgram);
181 if (programObject)
182 {
183 programObject->release();
184 }
185 mState.currentProgram = 0;
186 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000187
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000188 while (!mFramebufferMap.empty())
189 {
190 deleteFramebuffer(mFramebufferMap.begin()->first);
191 }
192
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000193 while (!mFenceMap.empty())
194 {
195 deleteFence(mFenceMap.begin()->first);
196 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000197
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000198 while (!mQueryMap.empty())
199 {
200 deleteQuery(mQueryMap.begin()->first);
201 }
202
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000203 while (!mMultiSampleSupport.empty())
204 {
205 delete [] mMultiSampleSupport.begin()->second;
206 mMultiSampleSupport.erase(mMultiSampleSupport.begin());
207 }
208
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000209 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000210 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +0000211 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000212 {
213 mState.samplerTexture[type][sampler].set(NULL);
214 }
215 }
216
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +0000217 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000218 {
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000219 mIncompleteTextures[type].set(NULL);
daniel@transgaming.com12d54072010-03-16 06:23:26 +0000220 }
221
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000222 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
223 {
224 mState.vertexAttribute[i].mBoundBuffer.set(NULL);
225 }
226
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000227 for (int i = 0; i < QUERY_TYPE_COUNT; i++)
228 {
229 mState.activeQuery[i].set(NULL);
230 }
231
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000232 mState.arrayBuffer.set(NULL);
233 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000234 mState.renderbuffer.set(NULL);
235
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +0000236 mTexture2DZero.set(NULL);
237 mTextureCubeMapZero.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000238
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000239 delete mVertexDataManager;
daniel@transgaming.comf8b58a02010-03-26 04:08:45 +0000240 delete mIndexDataManager;
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000241 delete mBlit;
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +0000242 delete mLineLoopIB;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000243
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +0000244 if (mMaskedClearSavedState)
245 {
246 mMaskedClearSavedState->Release();
247 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000248
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000249 mResourceManager->release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000250}
251
252void Context::makeCurrent(egl::Display *display, egl::Surface *surface)
253{
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000254 mDisplay = display;
255 mDevice = mDisplay->getDevice();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000256
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000257 if (!mHasBeenCurrent)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000258 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000259 mDeviceCaps = mDisplay->getDeviceCaps();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000260
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000261 mVertexDataManager = new VertexDataManager(this, mDevice);
262 mIndexDataManager = new IndexDataManager(this, mDevice);
daniel@transgaming.comb8c28ed2010-04-13 03:26:32 +0000263 mBlit = new Blit(this);
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000264
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +0000265 mSupportsShaderModel3 = mDeviceCaps.PixelShaderVersion >= D3DPS_VERSION(3, 0);
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000266 mSupportsVertexTexture = mDisplay->getVertexTextureSupport();
267 mSupportsNonPower2Texture = mDisplay->getNonPower2TextureSupport();
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +0000268 mSupportsInstancing = mDisplay->getInstancingSupport();
daniel@transgaming.com5d752f22010-10-07 13:37:20 +0000269
270 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
271 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
272 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
273 mMaxRenderbufferDimension = mMaxTextureDimension;
274 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
275 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
276 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
277
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000278 const D3DFORMAT renderBufferFormats[] =
279 {
280 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000281 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000282 D3DFMT_R5G6B5,
283 D3DFMT_D24S8
284 };
285
286 int max = 0;
287 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
288 {
289 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000290 mDisplay->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000291 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
292
293 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
294 {
295 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
296 {
297 max = j;
298 }
299 }
300 }
301
302 mMaxSupportedSamples = max;
303
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000304 mSupportsEventQueries = mDisplay->getEventQuerySupport();
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000305 mSupportsOcclusionQueries = mDisplay->getOcclusionQuerySupport();
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000306 mSupportsDXT1Textures = mDisplay->getDXT1TextureSupport();
307 mSupportsDXT3Textures = mDisplay->getDXT3TextureSupport();
308 mSupportsDXT5Textures = mDisplay->getDXT5TextureSupport();
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +0000309 mSupportsFloat32Textures = mDisplay->getFloat32TextureSupport(&mSupportsFloat32LinearFilter, &mSupportsFloat32RenderableTextures);
310 mSupportsFloat16Textures = mDisplay->getFloat16TextureSupport(&mSupportsFloat16LinearFilter, &mSupportsFloat16RenderableTextures);
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000311 mSupportsLuminanceTextures = mDisplay->getLuminanceTextureSupport();
312 mSupportsLuminanceAlphaTextures = mDisplay->getLuminanceAlphaTextureSupport();
daniel@transgaming.com1c49f792012-05-31 01:14:02 +0000313 mSupportsDepthTextures = mDisplay->getDepthTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000314
daniel@transgaming.com83921382011-01-08 05:46:00 +0000315 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
316
gman@chromium.org50c526d2011-08-10 05:19:44 +0000317 mNumCompressedTextureFormats = 0;
318 if (supportsDXT1Textures())
319 {
320 mNumCompressedTextureFormats += 2;
321 }
322 if (supportsDXT3Textures())
323 {
324 mNumCompressedTextureFormats += 1;
325 }
326 if (supportsDXT5Textures())
327 {
328 mNumCompressedTextureFormats += 1;
329 }
330
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000331 initExtensionString();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +0000332 initRendererString();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000333
334 mState.viewportX = 0;
335 mState.viewportY = 0;
336 mState.viewportWidth = surface->getWidth();
337 mState.viewportHeight = surface->getHeight();
338
339 mState.scissorX = 0;
340 mState.scissorY = 0;
341 mState.scissorWidth = surface->getWidth();
342 mState.scissorHeight = surface->getHeight();
343
344 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000345 }
346
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000347 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
348 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000349 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000350
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000351 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000352 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000353 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354
355 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000356
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000357 if (defaultRenderTarget)
358 {
359 defaultRenderTarget->Release();
360 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000361
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000362 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000363 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000364 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000365 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000366
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000367 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368}
369
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000370// 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 +0000371void Context::markAllStateDirty()
372{
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000373 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
374 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +0000375 mAppliedTextureSerialPS[t] = 0;
376 }
377
378 for (int t = 0; t < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; t++)
379 {
380 mAppliedTextureSerialVS[t] = 0;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000381 }
382
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000383 mAppliedProgramSerial = 0;
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000384 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000385 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000386 mAppliedStencilbufferSerial = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000387 mAppliedIBSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000388 mDepthStencilInitialized = false;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000389 mViewportInitialized = false;
390 mRenderTargetDescInitialized = false;
391
392 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000393
394 mClearStateDirty = true;
395 mCullStateDirty = true;
396 mDepthStateDirty = true;
397 mMaskStateDirty = true;
398 mBlendStateDirty = true;
399 mStencilStateDirty = true;
400 mPolygonOffsetStateDirty = true;
401 mScissorStateDirty = true;
402 mSampleStateDirty = true;
403 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000404 mFrontFaceDirty = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +0000405 mDxUniformsDirty = true;
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000406 mCachedCurrentProgram = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000407}
408
daniel@transgaming.com11399d52012-04-28 00:35:14 +0000409void Context::markDxUniformsDirty()
410{
411 mDxUniformsDirty = true;
412}
413
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000414void Context::markContextLost()
415{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +0000416 if (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT)
417 mResetStatus = GL_UNKNOWN_CONTEXT_RESET_EXT;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000418 mContextLost = true;
419}
420
421bool Context::isContextLost()
422{
423 return mContextLost;
424}
425
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000426void Context::setClearColor(float red, float green, float blue, float alpha)
427{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000428 mState.colorClearValue.red = red;
429 mState.colorClearValue.green = green;
430 mState.colorClearValue.blue = blue;
431 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000432}
433
434void Context::setClearDepth(float depth)
435{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000436 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000437}
438
439void Context::setClearStencil(int stencil)
440{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000441 mState.stencilClearValue = stencil;
442}
443
444void Context::setCullFace(bool enabled)
445{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000446 if (mState.cullFace != enabled)
447 {
448 mState.cullFace = enabled;
449 mCullStateDirty = true;
450 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000451}
452
453bool Context::isCullFaceEnabled() const
454{
455 return mState.cullFace;
456}
457
458void Context::setCullMode(GLenum mode)
459{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000460 if (mState.cullMode != mode)
461 {
462 mState.cullMode = mode;
463 mCullStateDirty = true;
464 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000465}
466
467void Context::setFrontFace(GLenum front)
468{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000469 if (mState.frontFace != front)
470 {
471 mState.frontFace = front;
472 mFrontFaceDirty = true;
473 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000474}
475
476void Context::setDepthTest(bool enabled)
477{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000478 if (mState.depthTest != enabled)
479 {
480 mState.depthTest = enabled;
481 mDepthStateDirty = true;
482 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000483}
484
485bool Context::isDepthTestEnabled() const
486{
487 return mState.depthTest;
488}
489
490void Context::setDepthFunc(GLenum depthFunc)
491{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000492 if (mState.depthFunc != depthFunc)
493 {
494 mState.depthFunc = depthFunc;
495 mDepthStateDirty = true;
496 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000497}
498
499void Context::setDepthRange(float zNear, float zFar)
500{
501 mState.zNear = zNear;
502 mState.zFar = zFar;
503}
504
505void Context::setBlend(bool enabled)
506{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000507 if (mState.blend != enabled)
508 {
509 mState.blend = enabled;
510 mBlendStateDirty = true;
511 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000512}
513
514bool Context::isBlendEnabled() const
515{
516 return mState.blend;
517}
518
519void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
520{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000521 if (mState.sourceBlendRGB != sourceRGB ||
522 mState.sourceBlendAlpha != sourceAlpha ||
523 mState.destBlendRGB != destRGB ||
524 mState.destBlendAlpha != destAlpha)
525 {
526 mState.sourceBlendRGB = sourceRGB;
527 mState.destBlendRGB = destRGB;
528 mState.sourceBlendAlpha = sourceAlpha;
529 mState.destBlendAlpha = destAlpha;
530 mBlendStateDirty = true;
531 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000532}
533
534void Context::setBlendColor(float red, float green, float blue, float alpha)
535{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000536 if (mState.blendColor.red != red ||
537 mState.blendColor.green != green ||
538 mState.blendColor.blue != blue ||
539 mState.blendColor.alpha != alpha)
540 {
541 mState.blendColor.red = red;
542 mState.blendColor.green = green;
543 mState.blendColor.blue = blue;
544 mState.blendColor.alpha = alpha;
545 mBlendStateDirty = true;
546 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000547}
548
549void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
550{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000551 if (mState.blendEquationRGB != rgbEquation ||
552 mState.blendEquationAlpha != alphaEquation)
553 {
554 mState.blendEquationRGB = rgbEquation;
555 mState.blendEquationAlpha = alphaEquation;
556 mBlendStateDirty = true;
557 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000558}
559
560void Context::setStencilTest(bool enabled)
561{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000562 if (mState.stencilTest != enabled)
563 {
564 mState.stencilTest = enabled;
565 mStencilStateDirty = true;
566 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000567}
568
569bool Context::isStencilTestEnabled() const
570{
571 return mState.stencilTest;
572}
573
574void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
575{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000576 if (mState.stencilFunc != stencilFunc ||
577 mState.stencilRef != stencilRef ||
578 mState.stencilMask != stencilMask)
579 {
580 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000581 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000582 mState.stencilMask = stencilMask;
583 mStencilStateDirty = true;
584 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000585}
586
587void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
588{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000589 if (mState.stencilBackFunc != stencilBackFunc ||
590 mState.stencilBackRef != stencilBackRef ||
591 mState.stencilBackMask != stencilBackMask)
592 {
593 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000594 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000595 mState.stencilBackMask = stencilBackMask;
596 mStencilStateDirty = true;
597 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000598}
599
600void Context::setStencilWritemask(GLuint stencilWritemask)
601{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000602 if (mState.stencilWritemask != stencilWritemask)
603 {
604 mState.stencilWritemask = stencilWritemask;
605 mStencilStateDirty = true;
606 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000607}
608
609void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
610{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000611 if (mState.stencilBackWritemask != stencilBackWritemask)
612 {
613 mState.stencilBackWritemask = stencilBackWritemask;
614 mStencilStateDirty = true;
615 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000616}
617
618void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
619{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000620 if (mState.stencilFail != stencilFail ||
621 mState.stencilPassDepthFail != stencilPassDepthFail ||
622 mState.stencilPassDepthPass != stencilPassDepthPass)
623 {
624 mState.stencilFail = stencilFail;
625 mState.stencilPassDepthFail = stencilPassDepthFail;
626 mState.stencilPassDepthPass = stencilPassDepthPass;
627 mStencilStateDirty = true;
628 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000629}
630
631void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
632{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000633 if (mState.stencilBackFail != stencilBackFail ||
634 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
635 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
636 {
637 mState.stencilBackFail = stencilBackFail;
638 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
639 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
640 mStencilStateDirty = true;
641 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000642}
643
644void Context::setPolygonOffsetFill(bool enabled)
645{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000646 if (mState.polygonOffsetFill != enabled)
647 {
648 mState.polygonOffsetFill = enabled;
649 mPolygonOffsetStateDirty = true;
650 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000651}
652
653bool Context::isPolygonOffsetFillEnabled() const
654{
655 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000656
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000657}
658
659void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
660{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000661 if (mState.polygonOffsetFactor != factor ||
662 mState.polygonOffsetUnits != units)
663 {
664 mState.polygonOffsetFactor = factor;
665 mState.polygonOffsetUnits = units;
666 mPolygonOffsetStateDirty = true;
667 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000668}
669
670void Context::setSampleAlphaToCoverage(bool enabled)
671{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000672 if (mState.sampleAlphaToCoverage != enabled)
673 {
674 mState.sampleAlphaToCoverage = enabled;
675 mSampleStateDirty = true;
676 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000677}
678
679bool Context::isSampleAlphaToCoverageEnabled() const
680{
681 return mState.sampleAlphaToCoverage;
682}
683
684void Context::setSampleCoverage(bool enabled)
685{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000686 if (mState.sampleCoverage != enabled)
687 {
688 mState.sampleCoverage = enabled;
689 mSampleStateDirty = true;
690 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000691}
692
693bool Context::isSampleCoverageEnabled() const
694{
695 return mState.sampleCoverage;
696}
697
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000698void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000699{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000700 if (mState.sampleCoverageValue != value ||
701 mState.sampleCoverageInvert != invert)
702 {
703 mState.sampleCoverageValue = value;
704 mState.sampleCoverageInvert = invert;
705 mSampleStateDirty = true;
706 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000707}
708
709void Context::setScissorTest(bool enabled)
710{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000711 if (mState.scissorTest != enabled)
712 {
713 mState.scissorTest = enabled;
714 mScissorStateDirty = true;
715 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000716}
717
718bool Context::isScissorTestEnabled() const
719{
720 return mState.scissorTest;
721}
722
723void Context::setDither(bool enabled)
724{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000725 if (mState.dither != enabled)
726 {
727 mState.dither = enabled;
728 mDitherStateDirty = true;
729 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000730}
731
732bool Context::isDitherEnabled() const
733{
734 return mState.dither;
735}
736
737void Context::setLineWidth(GLfloat width)
738{
739 mState.lineWidth = width;
740}
741
742void Context::setGenerateMipmapHint(GLenum hint)
743{
744 mState.generateMipmapHint = hint;
745}
746
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000747void Context::setFragmentShaderDerivativeHint(GLenum hint)
748{
749 mState.fragmentShaderDerivativeHint = hint;
750 // TODO: Propagate the hint to shader translator so we can write
751 // ddx, ddx_coarse, or ddx_fine depending on the hint.
752 // Ignore for now. It is valid for implementations to ignore hint.
753}
754
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000755void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
756{
757 mState.viewportX = x;
758 mState.viewportY = y;
759 mState.viewportWidth = width;
760 mState.viewportHeight = height;
761}
762
763void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
764{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000765 if (mState.scissorX != x || mState.scissorY != y ||
766 mState.scissorWidth != width || mState.scissorHeight != height)
767 {
768 mState.scissorX = x;
769 mState.scissorY = y;
770 mState.scissorWidth = width;
771 mState.scissorHeight = height;
772 mScissorStateDirty = true;
773 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000774}
775
776void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
777{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000778 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
779 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
780 {
781 mState.colorMaskRed = red;
782 mState.colorMaskGreen = green;
783 mState.colorMaskBlue = blue;
784 mState.colorMaskAlpha = alpha;
785 mMaskStateDirty = true;
786 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000787}
788
789void Context::setDepthMask(bool mask)
790{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000791 if (mState.depthMask != mask)
792 {
793 mState.depthMask = mask;
794 mMaskStateDirty = true;
795 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000796}
797
daniel@transgaming.comdfd57022011-05-11 15:37:25 +0000798void Context::setActiveSampler(unsigned int active)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000799{
800 mState.activeSampler = active;
801}
802
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000803GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000804{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000805 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000806}
807
808GLuint Context::getDrawFramebufferHandle() const
809{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000810 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000811}
812
813GLuint Context::getRenderbufferHandle() const
814{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000815 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000816}
817
818GLuint Context::getArrayBufferHandle() const
819{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000820 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000821}
822
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000823GLuint Context::getActiveQuery(GLenum target) const
824{
825 Query *queryObject = NULL;
826
827 switch (target)
828 {
829 case GL_ANY_SAMPLES_PASSED_EXT:
830 queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED].get();
831 break;
832 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
833 queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE].get();
834 break;
835 default:
836 ASSERT(false);
837 }
838
839 if (queryObject)
840 {
841 return queryObject->id();
842 }
843 else
844 {
845 return 0;
846 }
847}
848
daniel@transgaming.com83921382011-01-08 05:46:00 +0000849void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000850{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000851 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000852}
853
daniel@transgaming.com83921382011-01-08 05:46:00 +0000854const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000855{
856 return mState.vertexAttribute[attribNum];
857}
858
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000859void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000860 GLsizei stride, const void *pointer)
861{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000862 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000863 mState.vertexAttribute[attribNum].mSize = size;
864 mState.vertexAttribute[attribNum].mType = type;
865 mState.vertexAttribute[attribNum].mNormalized = normalized;
866 mState.vertexAttribute[attribNum].mStride = stride;
867 mState.vertexAttribute[attribNum].mPointer = pointer;
868}
869
870const void *Context::getVertexAttribPointer(unsigned int attribNum) const
871{
872 return mState.vertexAttribute[attribNum].mPointer;
873}
874
daniel@transgaming.com83921382011-01-08 05:46:00 +0000875const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000876{
877 return mState.vertexAttribute;
878}
879
880void Context::setPackAlignment(GLint alignment)
881{
882 mState.packAlignment = alignment;
883}
884
885GLint Context::getPackAlignment() const
886{
887 return mState.packAlignment;
888}
889
890void Context::setUnpackAlignment(GLint alignment)
891{
892 mState.unpackAlignment = alignment;
893}
894
895GLint Context::getUnpackAlignment() const
896{
897 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000898}
899
bsalomon@google.com56d46ab2011-11-23 14:53:10 +0000900void Context::setPackReverseRowOrder(bool reverseRowOrder)
901{
902 mState.packReverseRowOrder = reverseRowOrder;
903}
904
905bool Context::getPackReverseRowOrder() const
906{
907 return mState.packReverseRowOrder;
908}
909
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910GLuint Context::createBuffer()
911{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000912 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000913}
914
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000915GLuint Context::createProgram()
916{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000917 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000918}
919
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000920GLuint Context::createShader(GLenum type)
921{
922 return mResourceManager->createShader(type);
923}
924
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000925GLuint Context::createTexture()
926{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000927 return mResourceManager->createTexture();
928}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000929
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000930GLuint Context::createRenderbuffer()
931{
932 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000933}
934
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000935// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000936GLuint Context::createFramebuffer()
937{
benvanik@google.com1a233342011-04-28 19:44:39 +0000938 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000939
940 mFramebufferMap[handle] = NULL;
941
942 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000943}
944
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000945GLuint Context::createFence()
946{
benvanik@google.com1a233342011-04-28 19:44:39 +0000947 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000948
apatrick@chromium.org563c0a52012-03-23 21:18:42 +0000949 mFenceMap[handle] = new Fence(mDisplay);
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000950
951 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000952}
953
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000954// Returns an unused query name
955GLuint Context::createQuery()
956{
957 GLuint handle = mQueryHandleAllocator.allocate();
958
959 mQueryMap[handle] = NULL;
960
961 return handle;
962}
963
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964void Context::deleteBuffer(GLuint buffer)
965{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000966 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000967 {
968 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000969 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000970
971 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972}
973
974void Context::deleteShader(GLuint shader)
975{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000976 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000977}
978
979void Context::deleteProgram(GLuint program)
980{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000981 mResourceManager->deleteProgram(program);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000982 mCachedCurrentProgram = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000983}
984
985void Context::deleteTexture(GLuint texture)
986{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000987 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000988 {
989 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000990 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000991
992 mResourceManager->deleteTexture(texture);
993}
994
995void Context::deleteRenderbuffer(GLuint renderbuffer)
996{
997 if (mResourceManager->getRenderbuffer(renderbuffer))
998 {
999 detachRenderbuffer(renderbuffer);
1000 }
1001
1002 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001003}
1004
1005void Context::deleteFramebuffer(GLuint framebuffer)
1006{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001007 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
1008
1009 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001010 {
1011 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +00001012
benvanik@google.com1a233342011-04-28 19:44:39 +00001013 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001014 delete framebufferObject->second;
1015 mFramebufferMap.erase(framebufferObject);
1016 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001017}
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001018
1019void Context::deleteFence(GLuint fence)
1020{
1021 FenceMap::iterator fenceObject = mFenceMap.find(fence);
1022
1023 if (fenceObject != mFenceMap.end())
1024 {
benvanik@google.com1a233342011-04-28 19:44:39 +00001025 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001026 delete fenceObject->second;
1027 mFenceMap.erase(fenceObject);
1028 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001029}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001031void Context::deleteQuery(GLuint query)
1032{
1033 QueryMap::iterator queryObject = mQueryMap.find(query);
1034 if (queryObject != mQueryMap.end())
1035 {
1036 mQueryHandleAllocator.release(queryObject->first);
1037 if (queryObject->second)
1038 {
1039 queryObject->second->release();
1040 }
1041 mQueryMap.erase(queryObject);
1042 }
1043}
1044
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001045Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001046{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001047 return mResourceManager->getBuffer(handle);
1048}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001049
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001050Shader *Context::getShader(GLuint handle)
1051{
1052 return mResourceManager->getShader(handle);
1053}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001054
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001055Program *Context::getProgram(GLuint handle)
1056{
1057 return mResourceManager->getProgram(handle);
1058}
1059
1060Texture *Context::getTexture(GLuint handle)
1061{
1062 return mResourceManager->getTexture(handle);
1063}
1064
1065Renderbuffer *Context::getRenderbuffer(GLuint handle)
1066{
1067 return mResourceManager->getRenderbuffer(handle);
1068}
1069
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001070Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001071{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001072 return getFramebuffer(mState.readFramebuffer);
1073}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001074
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001075Framebuffer *Context::getDrawFramebuffer()
1076{
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001077 return mBoundDrawFramebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001078}
1079
1080void Context::bindArrayBuffer(unsigned int buffer)
1081{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001082 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001083
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001084 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001085}
1086
1087void Context::bindElementArrayBuffer(unsigned int buffer)
1088{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001089 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001090
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001091 mState.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001092}
1093
1094void Context::bindTexture2D(GLuint texture)
1095{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001096 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001097
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001098 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001099}
1100
1101void Context::bindTextureCubeMap(GLuint texture)
1102{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001103 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001104
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001105 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001106}
1107
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001108void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001109{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001110 if (!getFramebuffer(framebuffer))
1111 {
1112 mFramebufferMap[framebuffer] = new Framebuffer();
1113 }
1114
1115 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001116}
1117
1118void Context::bindDrawFramebuffer(GLuint framebuffer)
1119{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001120 if (!getFramebuffer(framebuffer))
1121 {
1122 mFramebufferMap[framebuffer] = new Framebuffer();
1123 }
1124
1125 mState.drawFramebuffer = framebuffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001126
1127 mBoundDrawFramebuffer = getFramebuffer(framebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001128}
1129
1130void Context::bindRenderbuffer(GLuint renderbuffer)
1131{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001132 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1133
1134 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001135}
1136
1137void Context::useProgram(GLuint program)
1138{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001139 GLuint priorProgram = mState.currentProgram;
1140 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001141
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001142 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001143 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001144 Program *newProgram = mResourceManager->getProgram(program);
1145 Program *oldProgram = mResourceManager->getProgram(priorProgram);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001146 mCachedCurrentProgram = NULL;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001147 mDxUniformsDirty = true;
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001148
1149 if (newProgram)
1150 {
1151 newProgram->addRef();
1152 }
1153
1154 if (oldProgram)
1155 {
1156 oldProgram->release();
1157 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001158 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001159}
1160
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001161void Context::beginQuery(GLenum target, GLuint query)
1162{
1163 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
1164 // of zero, if the active query object name for <target> is non-zero (for the
1165 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
1166 // the active query for either target is non-zero), if <id> is the name of an
1167 // existing query object whose type does not match <target>, or if <id> is the
1168 // active query object name for any query type, the error INVALID_OPERATION is
1169 // generated.
1170
1171 // Ensure no other queries are active
1172 // NOTE: If other queries than occlusion are supported, we will need to check
1173 // separately that:
1174 // a) The query ID passed is not the current active query for any target/type
1175 // b) There are no active queries for the requested target (and in the case
1176 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
1177 // no query may be active for either if glBeginQuery targets either.
1178 for (int i = 0; i < QUERY_TYPE_COUNT; i++)
1179 {
1180 if (mState.activeQuery[i].get() != NULL)
1181 {
1182 return error(GL_INVALID_OPERATION);
1183 }
1184 }
1185
1186 QueryType qType;
1187 switch (target)
1188 {
1189 case GL_ANY_SAMPLES_PASSED_EXT:
1190 qType = QUERY_ANY_SAMPLES_PASSED;
1191 break;
1192 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
1193 qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
1194 break;
1195 default:
1196 ASSERT(false);
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00001197 return;
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001198 }
1199
1200 Query *queryObject = getQuery(query, true, target);
1201
1202 // check that name was obtained with glGenQueries
1203 if (!queryObject)
1204 {
1205 return error(GL_INVALID_OPERATION);
1206 }
1207
1208 // check for type mismatch
1209 if (queryObject->getType() != target)
1210 {
1211 return error(GL_INVALID_OPERATION);
1212 }
1213
1214 // set query as active for specified target
1215 mState.activeQuery[qType].set(queryObject);
1216
1217 // begin query
1218 queryObject->begin();
1219}
1220
1221void Context::endQuery(GLenum target)
1222{
1223 QueryType qType;
1224
1225 switch (target)
1226 {
1227 case GL_ANY_SAMPLES_PASSED_EXT:
1228 qType = QUERY_ANY_SAMPLES_PASSED;
1229 break;
1230 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
1231 qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
1232 break;
1233 default:
1234 ASSERT(false);
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00001235 return;
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001236 }
1237
1238 Query *queryObject = mState.activeQuery[qType].get();
1239
1240 if (queryObject == NULL)
1241 {
1242 return error(GL_INVALID_OPERATION);
1243 }
1244
1245 queryObject->end();
1246
1247 mState.activeQuery[qType].set(NULL);
1248}
1249
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001250void Context::setFramebufferZero(Framebuffer *buffer)
1251{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001252 delete mFramebufferMap[0];
1253 mFramebufferMap[0] = buffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001254 if (mState.drawFramebuffer == 0)
1255 {
1256 mBoundDrawFramebuffer = buffer;
1257 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001258}
1259
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001260void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001261{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001262 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1263 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001264}
1265
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001266Framebuffer *Context::getFramebuffer(unsigned int handle)
1267{
1268 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1269
1270 if (framebuffer == mFramebufferMap.end())
1271 {
1272 return NULL;
1273 }
1274 else
1275 {
1276 return framebuffer->second;
1277 }
1278}
1279
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001280Fence *Context::getFence(unsigned int handle)
1281{
1282 FenceMap::iterator fence = mFenceMap.find(handle);
1283
1284 if (fence == mFenceMap.end())
1285 {
1286 return NULL;
1287 }
1288 else
1289 {
1290 return fence->second;
1291 }
1292}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001293
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001294Query *Context::getQuery(unsigned int handle, bool create, GLenum type)
1295{
1296 QueryMap::iterator query = mQueryMap.find(handle);
1297
1298 if (query == mQueryMap.end())
1299 {
1300 return NULL;
1301 }
1302 else
1303 {
1304 if (!query->second && create)
1305 {
1306 query->second = new Query(handle, type);
1307 query->second->addRef();
1308 }
1309 return query->second;
1310 }
1311}
1312
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001313Buffer *Context::getArrayBuffer()
1314{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001315 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001316}
1317
1318Buffer *Context::getElementArrayBuffer()
1319{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001320 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001321}
1322
1323Program *Context::getCurrentProgram()
1324{
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001325 if (!mCachedCurrentProgram)
1326 {
1327 mCachedCurrentProgram = mResourceManager->getProgram(mState.currentProgram);
1328 }
1329 return mCachedCurrentProgram;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001330}
1331
1332Texture2D *Context::getTexture2D()
1333{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001334 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001335}
1336
1337TextureCubeMap *Context::getTextureCubeMap()
1338{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001339 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001340}
1341
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001342Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001343{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001344 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001345
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001346 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001347 {
1348 switch (type)
1349 {
1350 default: UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001351 case TEXTURE_2D: return mTexture2DZero.get();
1352 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001353 }
1354 }
1355
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001356 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001357}
1358
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001359bool Context::getBooleanv(GLenum pname, GLboolean *params)
1360{
1361 switch (pname)
1362 {
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001363 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1364 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1365 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001366 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001367 params[0] = mState.colorMaskRed;
1368 params[1] = mState.colorMaskGreen;
1369 params[2] = mState.colorMaskBlue;
1370 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001371 break;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001372 case GL_CULL_FACE: *params = mState.cullFace; break;
1373 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1374 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1375 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1376 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1377 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1378 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1379 case GL_BLEND: *params = mState.blend; break;
1380 case GL_DITHER: *params = mState.dither; break;
1381 case GL_CONTEXT_ROBUST_ACCESS_EXT: *params = mRobustAccess ? GL_TRUE : GL_FALSE; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001382 default:
1383 return false;
1384 }
1385
1386 return true;
1387}
1388
1389bool Context::getFloatv(GLenum pname, GLfloat *params)
1390{
1391 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1392 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1393 // GetIntegerv as its native query function. As it would require conversion in any
1394 // case, this should make no difference to the calling application.
1395 switch (pname)
1396 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001397 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1398 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1399 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1400 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1401 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001402 case GL_ALIASED_LINE_WIDTH_RANGE:
1403 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1404 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1405 break;
1406 case GL_ALIASED_POINT_SIZE_RANGE:
1407 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001408 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 +00001409 break;
1410 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001411 params[0] = mState.zNear;
1412 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001413 break;
1414 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001415 params[0] = mState.colorClearValue.red;
1416 params[1] = mState.colorClearValue.green;
1417 params[2] = mState.colorClearValue.blue;
1418 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001419 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001420 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001421 params[0] = mState.blendColor.red;
1422 params[1] = mState.blendColor.green;
1423 params[2] = mState.blendColor.blue;
1424 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001425 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001426 default:
1427 return false;
1428 }
1429
1430 return true;
1431}
1432
1433bool Context::getIntegerv(GLenum pname, GLint *params)
1434{
1435 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1436 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1437 // GetIntegerv as its native query function. As it would require conversion in any
1438 // case, this should make no difference to the calling application. You may find it in
1439 // Context::getFloatv.
1440 switch (pname)
1441 {
1442 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1443 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001444 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00001445 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1446 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001447 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001448 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001449 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001450 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001451 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001452 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1453 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001454 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1455 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1456 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001457 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001458 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1459 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00001460 case GL_PACK_REVERSE_ROW_ORDER_ANGLE: *params = mState.packReverseRowOrder; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001461 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1462 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001463 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001464 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1465 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1466 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1467 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1468 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1469 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1470 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1471 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1472 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1473 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1474 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1475 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1476 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1477 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1478 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1479 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1480 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1481 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1482 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1483 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1484 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1485 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1486 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1487 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001488 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1489 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001490 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
gman@chromium.org50c526d2011-08-10 05:19:44 +00001491 params[0] = mNumCompressedTextureFormats;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001492 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001493 case GL_MAX_SAMPLES_ANGLE:
1494 {
1495 GLsizei maxSamples = getMaxSupportedSamples();
1496 if (maxSamples != 0)
1497 {
1498 *params = maxSamples;
1499 }
1500 else
1501 {
1502 return false;
1503 }
1504
1505 break;
1506 }
1507 case GL_SAMPLE_BUFFERS:
1508 case GL_SAMPLES:
1509 {
1510 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1511 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1512 {
1513 switch (pname)
1514 {
1515 case GL_SAMPLE_BUFFERS:
1516 if (framebuffer->getSamples() != 0)
1517 {
1518 *params = 1;
1519 }
1520 else
1521 {
1522 *params = 0;
1523 }
1524 break;
1525 case GL_SAMPLES:
1526 *params = framebuffer->getSamples();
1527 break;
1528 }
1529 }
1530 else
1531 {
1532 *params = 0;
1533 }
1534 }
1535 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001536 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1537 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1538 case GL_MAX_VIEWPORT_DIMS:
1539 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001540 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001541 params[0] = maxDimension;
1542 params[1] = maxDimension;
1543 }
1544 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001545 case GL_COMPRESSED_TEXTURE_FORMATS:
1546 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001547 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00001548 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001549 *params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1550 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1551 }
1552 if (supportsDXT3Textures())
1553 {
1554 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
1555 }
1556 if (supportsDXT5Textures())
1557 {
1558 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001559 }
1560 }
1561 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001562 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001563 params[0] = mState.viewportX;
1564 params[1] = mState.viewportY;
1565 params[2] = mState.viewportWidth;
1566 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001567 break;
1568 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001569 params[0] = mState.scissorX;
1570 params[1] = mState.scissorY;
1571 params[2] = mState.scissorWidth;
1572 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001573 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001574 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1575 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001576 case GL_RED_BITS:
1577 case GL_GREEN_BITS:
1578 case GL_BLUE_BITS:
1579 case GL_ALPHA_BITS:
1580 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001581 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001582 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001583
1584 if (colorbuffer)
1585 {
1586 switch (pname)
1587 {
1588 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1589 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1590 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1591 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1592 }
1593 }
1594 else
1595 {
1596 *params = 0;
1597 }
1598 }
1599 break;
1600 case GL_DEPTH_BITS:
1601 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001602 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001603 gl::Renderbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001604
1605 if (depthbuffer)
1606 {
1607 *params = depthbuffer->getDepthSize();
1608 }
1609 else
1610 {
1611 *params = 0;
1612 }
1613 }
1614 break;
1615 case GL_STENCIL_BITS:
1616 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001617 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001618 gl::Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001619
1620 if (stencilbuffer)
1621 {
1622 *params = stencilbuffer->getStencilSize();
1623 }
1624 else
1625 {
1626 *params = 0;
1627 }
1628 }
1629 break;
1630 case GL_TEXTURE_BINDING_2D:
1631 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001632 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001633 {
1634 error(GL_INVALID_OPERATION);
1635 return false;
1636 }
1637
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001638 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001639 }
1640 break;
1641 case GL_TEXTURE_BINDING_CUBE_MAP:
1642 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001643 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001644 {
1645 error(GL_INVALID_OPERATION);
1646 return false;
1647 }
1648
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001649 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001650 }
1651 break;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001652 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
1653 *params = mResetStrategy;
1654 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001655 default:
1656 return false;
1657 }
1658
1659 return true;
1660}
1661
1662bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1663{
1664 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1665 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1666 // to the fact that it is stored internally as a float, and so would require conversion
1667 // if returned from Context::getIntegerv. Since this conversion is already implemented
1668 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1669 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1670 // application.
1671 switch (pname)
1672 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001673 case GL_COMPRESSED_TEXTURE_FORMATS:
1674 {
1675 *type = GL_INT;
1676 *numParams = mNumCompressedTextureFormats;
1677 }
1678 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001679 case GL_SHADER_BINARY_FORMATS:
1680 {
1681 *type = GL_INT;
1682 *numParams = 0;
1683 }
1684 break;
1685 case GL_MAX_VERTEX_ATTRIBS:
1686 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1687 case GL_MAX_VARYING_VECTORS:
1688 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1689 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1690 case GL_MAX_TEXTURE_IMAGE_UNITS:
1691 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1692 case GL_MAX_RENDERBUFFER_SIZE:
1693 case GL_NUM_SHADER_BINARY_FORMATS:
1694 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1695 case GL_ARRAY_BUFFER_BINDING:
1696 case GL_FRAMEBUFFER_BINDING:
1697 case GL_RENDERBUFFER_BINDING:
1698 case GL_CURRENT_PROGRAM:
1699 case GL_PACK_ALIGNMENT:
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00001700 case GL_PACK_REVERSE_ROW_ORDER_ANGLE:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001701 case GL_UNPACK_ALIGNMENT:
1702 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001703 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001704 case GL_RED_BITS:
1705 case GL_GREEN_BITS:
1706 case GL_BLUE_BITS:
1707 case GL_ALPHA_BITS:
1708 case GL_DEPTH_BITS:
1709 case GL_STENCIL_BITS:
1710 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1711 case GL_CULL_FACE_MODE:
1712 case GL_FRONT_FACE:
1713 case GL_ACTIVE_TEXTURE:
1714 case GL_STENCIL_FUNC:
1715 case GL_STENCIL_VALUE_MASK:
1716 case GL_STENCIL_REF:
1717 case GL_STENCIL_FAIL:
1718 case GL_STENCIL_PASS_DEPTH_FAIL:
1719 case GL_STENCIL_PASS_DEPTH_PASS:
1720 case GL_STENCIL_BACK_FUNC:
1721 case GL_STENCIL_BACK_VALUE_MASK:
1722 case GL_STENCIL_BACK_REF:
1723 case GL_STENCIL_BACK_FAIL:
1724 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1725 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1726 case GL_DEPTH_FUNC:
1727 case GL_BLEND_SRC_RGB:
1728 case GL_BLEND_SRC_ALPHA:
1729 case GL_BLEND_DST_RGB:
1730 case GL_BLEND_DST_ALPHA:
1731 case GL_BLEND_EQUATION_RGB:
1732 case GL_BLEND_EQUATION_ALPHA:
1733 case GL_STENCIL_WRITEMASK:
1734 case GL_STENCIL_BACK_WRITEMASK:
1735 case GL_STENCIL_CLEAR_VALUE:
1736 case GL_SUBPIXEL_BITS:
1737 case GL_MAX_TEXTURE_SIZE:
1738 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1739 case GL_SAMPLE_BUFFERS:
1740 case GL_SAMPLES:
1741 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1742 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1743 case GL_TEXTURE_BINDING_2D:
1744 case GL_TEXTURE_BINDING_CUBE_MAP:
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001745 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001746 {
1747 *type = GL_INT;
1748 *numParams = 1;
1749 }
1750 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001751 case GL_MAX_SAMPLES_ANGLE:
1752 {
1753 if (getMaxSupportedSamples() != 0)
1754 {
1755 *type = GL_INT;
1756 *numParams = 1;
1757 }
1758 else
1759 {
1760 return false;
1761 }
1762 }
1763 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001764 case GL_MAX_VIEWPORT_DIMS:
1765 {
1766 *type = GL_INT;
1767 *numParams = 2;
1768 }
1769 break;
1770 case GL_VIEWPORT:
1771 case GL_SCISSOR_BOX:
1772 {
1773 *type = GL_INT;
1774 *numParams = 4;
1775 }
1776 break;
1777 case GL_SHADER_COMPILER:
1778 case GL_SAMPLE_COVERAGE_INVERT:
1779 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001780 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1781 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1782 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1783 case GL_SAMPLE_COVERAGE:
1784 case GL_SCISSOR_TEST:
1785 case GL_STENCIL_TEST:
1786 case GL_DEPTH_TEST:
1787 case GL_BLEND:
1788 case GL_DITHER:
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001789 case GL_CONTEXT_ROBUST_ACCESS_EXT:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001790 {
1791 *type = GL_BOOL;
1792 *numParams = 1;
1793 }
1794 break;
1795 case GL_COLOR_WRITEMASK:
1796 {
1797 *type = GL_BOOL;
1798 *numParams = 4;
1799 }
1800 break;
1801 case GL_POLYGON_OFFSET_FACTOR:
1802 case GL_POLYGON_OFFSET_UNITS:
1803 case GL_SAMPLE_COVERAGE_VALUE:
1804 case GL_DEPTH_CLEAR_VALUE:
1805 case GL_LINE_WIDTH:
1806 {
1807 *type = GL_FLOAT;
1808 *numParams = 1;
1809 }
1810 break;
1811 case GL_ALIASED_LINE_WIDTH_RANGE:
1812 case GL_ALIASED_POINT_SIZE_RANGE:
1813 case GL_DEPTH_RANGE:
1814 {
1815 *type = GL_FLOAT;
1816 *numParams = 2;
1817 }
1818 break;
1819 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001820 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001821 {
1822 *type = GL_FLOAT;
1823 *numParams = 4;
1824 }
1825 break;
1826 default:
1827 return false;
1828 }
1829
1830 return true;
1831}
1832
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001833// Applies the render target surface, depth stencil surface, viewport rectangle and
1834// scissor rectangle to the Direct3D 9 device
1835bool Context::applyRenderTarget(bool ignoreViewport)
1836{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001837 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001838
1839 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1840 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001841 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001842 }
1843
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001844 bool renderTargetChanged = false;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001845 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1846 if (renderTargetSerial != mAppliedRenderTargetSerial)
1847 {
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001848 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001849 if (!renderTarget)
1850 {
1851 return false; // Context must be lost
1852 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001853 mDevice->SetRenderTarget(0, renderTarget);
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001854 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001855 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 +00001856 renderTargetChanged = true;
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001857 renderTarget->Release();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001858 }
1859
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001860 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001861 unsigned int depthbufferSerial = 0;
1862 unsigned int stencilbufferSerial = 0;
1863 if (framebufferObject->getDepthbufferType() != GL_NONE)
1864 {
1865 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001866 if (!depthStencil)
1867 {
1868 ERR("Depth stencil pointer unexpectedly null.");
1869 return false;
1870 }
1871
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001872 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1873 }
1874 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1875 {
1876 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001877 if (!depthStencil)
1878 {
1879 ERR("Depth stencil pointer unexpectedly null.");
1880 return false;
1881 }
1882
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001883 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1884 }
1885
1886 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001887 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001888 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001889 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001890 mDevice->SetDepthStencilSurface(depthStencil);
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001891 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001892 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001893 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001894 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001895
daniel@transgaming.com63e6afe2012-05-31 01:14:42 +00001896 if (depthStencil)
1897 {
1898 depthStencil->Release();
1899 }
1900
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001901 if (!mRenderTargetDescInitialized || renderTargetChanged)
1902 {
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001903 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001904 if (!renderTarget)
1905 {
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001906 return false; // Context must be lost
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001907 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001908 renderTarget->GetDesc(&mRenderTargetDesc);
1909 mRenderTargetDescInitialized = true;
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001910 renderTarget->Release();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001911 }
1912
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001913 D3DVIEWPORT9 viewport;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001914
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001915 float zNear = clamp01(mState.zNear);
1916 float zFar = clamp01(mState.zFar);
1917
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001918 if (ignoreViewport)
1919 {
1920 viewport.X = 0;
1921 viewport.Y = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001922 viewport.Width = mRenderTargetDesc.Width;
1923 viewport.Height = mRenderTargetDesc.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001924 viewport.MinZ = 0.0f;
1925 viewport.MaxZ = 1.0f;
1926 }
1927 else
1928 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001929 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, mRenderTargetDesc.Height);
1930 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1931 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1932 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width) - static_cast<LONG>(viewport.X));
1933 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 +00001934 viewport.MinZ = zNear;
1935 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001936 }
1937
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001938 if (viewport.Width <= 0 || viewport.Height <= 0)
1939 {
1940 return false; // Nothing to render
1941 }
1942
jbauman@chromium.org241e70d2011-11-03 23:07:05 +00001943 if (renderTargetChanged || !mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001944 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001945 mDevice->SetViewport(&viewport);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001946 mSetViewport = viewport;
1947 mViewportInitialized = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001948 mDxUniformsDirty = true;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001949 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001950
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001951 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001952 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001953 if (mState.scissorTest)
1954 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001955 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, mRenderTargetDesc.Height);
1956 rect.left = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1957 rect.top = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1958 rect.right = clamp(rect.right, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1959 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001960 mDevice->SetScissorRect(&rect);
1961 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001962 }
1963 else
1964 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001965 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001966 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001967
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001968 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001969 }
1970
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001971 if (mState.currentProgram && mDxUniformsDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001972 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001973 Program *programObject = getCurrentProgram();
1974
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001975 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001976 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001977 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001978
daniel@transgaming.comd9a54f92011-12-22 18:32:53 +00001979 // These values are used for computing gl_FragCoord in Program::linkVaryings(). The approach depends on Shader Model 3.0 support.
1980 GLint coord = programObject->getDxCoordLocation();
1981 float h = mSupportsShaderModel3 ? mRenderTargetDesc.Height : mState.viewportHeight / 2.0f;
1982 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, h,
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001983 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1984 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.comd9a54f92011-12-22 18:32:53 +00001985 programObject->setUniform4fv(coord, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001986
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001987 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001988 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001989 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001990
daniel@transgaming.com31754962010-11-28 02:02:52 +00001991 GLint depthRange = programObject->getDxDepthRangeLocation();
1992 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1993 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001994 mDxUniformsDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001995 }
1996
1997 return true;
1998}
1999
2000// 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 +00002001void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002002{
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00002003 Program *programObject = getCurrentProgram();
2004
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002005 Framebuffer *framebufferObject = getDrawFramebuffer();
2006
2007 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
2008
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00002009 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002010 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00002011 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002012
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00002013 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002014 GLint alwaysFront = !isTriangleMode(drawMode);
2015 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
2016
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002017 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002018 bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
2019 // Apparently some ATI cards have a bug where a draw with a zero color
2020 // write mask can cause later draws to have incorrect results. Instead,
2021 // set a nonzero color write mask but modify the blend state so that no
2022 // drawing is done.
2023 // http://code.google.com/p/angleproject/issues/detail?id=169
2024
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002025 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002026 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002027 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002028 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002029 mDevice->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002030 }
2031 else
2032 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002033 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002034 }
2035
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002036 mCullStateDirty = false;
2037 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002038
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002039 if (mDepthStateDirty)
2040 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00002041 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002042 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002043 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
2044 mDevice->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002045 }
2046 else
2047 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002048 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002049 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002050
2051 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002052 }
2053
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002054 if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
2055 {
2056 mBlendStateDirty = true;
2057 mMaskStateDirty = true;
2058 }
2059
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002060 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002061 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002062 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00002063 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002064 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002065
2066 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
2067 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
2068 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002069 mDevice->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002070 }
2071 else
2072 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002073 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002074 unorm<8>(mState.blendColor.alpha),
2075 unorm<8>(mState.blendColor.alpha),
2076 unorm<8>(mState.blendColor.alpha)));
2077 }
2078
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002079 mDevice->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
2080 mDevice->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
2081 mDevice->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002082
2083 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
2084 mState.destBlendRGB != mState.destBlendAlpha ||
2085 mState.blendEquationRGB != mState.blendEquationAlpha)
2086 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002087 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002088
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002089 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
2090 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
2091 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002092 }
2093 else
2094 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002095 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002096 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00002097 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002098 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00002099 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002100 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00002101 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002102
2103 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002104 }
2105
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002106 if (mStencilStateDirty || mFrontFaceDirty)
2107 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002108 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002109 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002110 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2111 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002112
2113 // FIXME: Unsupported by D3D9
2114 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
2115 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
2116 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
2117 if (mState.stencilWritemask != mState.stencilBackWritemask ||
2118 mState.stencilRef != mState.stencilBackRef ||
2119 mState.stencilMask != mState.stencilBackMask)
2120 {
2121 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
2122 return error(GL_INVALID_OPERATION);
2123 }
2124
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00002125 // get the maximum size of the stencil ref
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00002126 gl::Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00002127 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
2128
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002129 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
2130 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002131 es2dx::ConvertComparison(mState.stencilFunc));
2132
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002133 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
2134 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002135
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002136 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002137 es2dx::ConvertStencilOp(mState.stencilFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002138 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002139 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002140 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002141 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
2142
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002143 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
2144 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002145 es2dx::ConvertComparison(mState.stencilBackFunc));
2146
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002147 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
2148 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002149
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002150 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002151 es2dx::ConvertStencilOp(mState.stencilBackFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002152 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002153 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002154 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002155 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
2156 }
2157 else
2158 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002159 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002160 }
2161
2162 mStencilStateDirty = false;
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002163 mFrontFaceDirty = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002164 }
2165
2166 if (mMaskStateDirty)
2167 {
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002168 int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
2169 mState.colorMaskBlue, mState.colorMaskAlpha);
2170 if (colorMask == 0 && !zeroColorMaskAllowed)
2171 {
2172 // Enable green channel, but set blending so nothing will be drawn.
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002173 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
2174 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002175
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002176 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
2177 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
2178 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002179 }
2180 else
2181 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002182 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002183 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002184 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002185
2186 mMaskStateDirty = false;
2187 }
2188
2189 if (mPolygonOffsetStateDirty)
2190 {
2191 if (mState.polygonOffsetFill)
2192 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00002193 gl::Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002194 if (depthbuffer)
2195 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002196 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002197 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002198 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002199 }
2200 }
2201 else
2202 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002203 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
2204 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002205 }
2206
2207 mPolygonOffsetStateDirty = false;
2208 }
2209
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002210 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002211 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002212 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002213 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002214 FIXME("Sample alpha to coverage is unimplemented.");
2215 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002216
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002217 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002218 if (mState.sampleCoverage)
2219 {
2220 unsigned int mask = 0;
2221 if (mState.sampleCoverageValue != 0)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002222 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002223 float threshold = 0.5f;
2224
2225 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002226 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002227 mask <<= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002228
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002229 if ((i + 1) * mState.sampleCoverageValue >= threshold)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002230 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002231 threshold += 1.0f;
2232 mask |= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002233 }
2234 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002235 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002236
2237 if (mState.sampleCoverageInvert)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002238 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002239 mask = ~mask;
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002240 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002241
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002242 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002243 }
2244 else
2245 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002246 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002247 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002248
2249 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002250 }
2251
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002252 if (mDitherStateDirty)
2253 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002254 mDevice->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002255
2256 mDitherStateDirty = false;
2257 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002258}
2259
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002260GLenum Context::applyVertexBuffer(GLint first, GLsizei count, GLsizei instances, GLsizei *repeatDraw)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002261{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002262 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002263
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +00002264 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes, instances);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002265 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002266 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002267 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002268 }
2269
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002270 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, getCurrentProgram(), instances, repeatDraw);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002271}
2272
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002273// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.comd2820bf2012-01-27 15:38:48 +00002274GLenum Context::applyIndexBuffer(const GLvoid *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002275{
daniel@transgaming.com83921382011-01-08 05:46:00 +00002276 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002277
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002278 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002279 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002280 if (indexInfo->serial != mAppliedIBSerial)
2281 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002282 mDevice->SetIndices(indexInfo->indexBuffer);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002283 mAppliedIBSerial = indexInfo->serial;
2284 }
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002285 }
2286
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002287 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002288}
2289
2290// Applies the shaders and shader constants to the Direct3D 9 device
2291void Context::applyShaders()
2292{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002293 Program *programObject = getCurrentProgram();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002294 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002295 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002296 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2297 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2298
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002299 mDevice->SetPixelShader(pixelShader);
2300 mDevice->SetVertexShader(vertexShader);
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002301 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002302 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002303 }
2304
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002305 programObject->applyUniforms();
2306}
2307
2308// Applies the textures and sampler states to the Direct3D 9 device
2309void Context::applyTextures()
2310{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002311 applyTextures(SAMPLER_PIXEL);
2312
2313 if (mSupportsVertexTexture)
2314 {
2315 applyTextures(SAMPLER_VERTEX);
2316 }
2317}
2318
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002319// For each Direct3D 9 sampler of either the pixel or vertex stage,
2320// looks up the corresponding OpenGL texture image unit and texture type,
2321// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002322void Context::applyTextures(SamplerType type)
2323{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002324 Program *programObject = getCurrentProgram();
2325
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002326 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 +00002327 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2328 int d3dSamplerOffset = (type == SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002329 int samplerRange = programObject->getUsedSamplerRange(type);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002330
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002331 for (int samplerIndex = 0; samplerIndex < samplerRange; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002332 {
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002333 int textureUnit = programObject->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00002334 int d3dSampler = samplerIndex + d3dSamplerOffset;
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002335
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002336 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002337 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002338 TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002339
2340 Texture *texture = getSamplerTexture(textureUnit, textureType);
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002341 unsigned int texSerial = texture->getTextureSerial();
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002342
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002343 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyParameters() || texture->hasDirtyImages())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002344 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002345 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2346
2347 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002348 {
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002349 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyParameters())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002350 {
2351 GLenum wrapS = texture->getWrapS();
2352 GLenum wrapT = texture->getWrapT();
2353 GLenum minFilter = texture->getMinFilter();
2354 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002355
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002356 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2357 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002358
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002359 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002360 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2361 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002362 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2363 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002364 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002365
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002366 if (appliedTextureSerial[samplerIndex] != texSerial || texture->hasDirtyImages())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002367 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002368 mDevice->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002369 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002370 }
2371 else
2372 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002373 mDevice->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002374 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002375
daniel@transgaming.com56c62632012-05-09 15:42:45 +00002376 appliedTextureSerial[samplerIndex] = texSerial;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002377 texture->resetDirty();
2378 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002379 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002380 else
2381 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002382 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002383 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002384 mDevice->SetTexture(d3dSampler, NULL);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002385 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002386 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002387 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002388 }
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002389
2390 for (int samplerIndex = samplerRange; samplerIndex < samplerCount; samplerIndex++)
2391 {
2392 if (appliedTextureSerial[samplerIndex] != 0)
2393 {
daniel@transgaming.comc5a7b692011-10-26 02:45:44 +00002394 mDevice->SetTexture(samplerIndex + d3dSamplerOffset, NULL);
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002395 appliedTextureSerial[samplerIndex] = 0;
2396 }
2397 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002398}
2399
daniel@transgaming.comb7915a52011-11-12 03:14:20 +00002400void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height,
2401 GLenum format, GLenum type, GLsizei *bufSize, void* pixels)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002402{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002403 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002404
2405 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2406 {
2407 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2408 }
2409
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002410 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2411 {
2412 return error(GL_INVALID_OPERATION);
2413 }
2414
daniel@transgaming.comb7915a52011-11-12 03:14:20 +00002415 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
2416 // sized query sanity check
2417 if (bufSize)
2418 {
2419 int requiredSize = outputPitch * height;
2420 if (requiredSize > *bufSize)
2421 {
2422 return error(GL_INVALID_OPERATION);
2423 }
2424 }
2425
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002426 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002427 if (!renderTarget)
2428 {
2429 return; // Context must be lost, return silently
2430 }
2431
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002432 D3DSURFACE_DESC desc;
2433 renderTarget->GetDesc(&desc);
2434
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002435 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2436 {
2437 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.coma5798952011-12-13 17:30:43 +00002438 renderTarget->Release();
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002439 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002440 }
2441
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002442 HRESULT result;
2443 IDirect3DSurface9 *systemSurface = NULL;
2444 bool directToPixels = getPackReverseRowOrder() && getPackAlignment() <= 4 && mDisplay->isD3d9ExDevice() &&
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00002445 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002446 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2447 if (directToPixels)
2448 {
2449 // Use the pixels ptr as a shared handle to write directly into client's memory
2450 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2451 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2452 if (FAILED(result))
2453 {
2454 // Try again without the shared handle
2455 directToPixels = false;
2456 }
2457 }
2458
2459 if (!directToPixels)
2460 {
2461 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2462 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2463 if (FAILED(result))
2464 {
2465 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
daniel@transgaming.com63e6afe2012-05-31 01:14:42 +00002466 renderTarget->Release();
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002467 return error(GL_OUT_OF_MEMORY);
2468 }
2469 }
2470
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002471 result = mDevice->GetRenderTargetData(renderTarget, systemSurface);
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00002472 renderTarget->Release();
daniel@transgaming.coma5798952011-12-13 17:30:43 +00002473 renderTarget = NULL;
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00002474
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002475 if (FAILED(result))
2476 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002477 systemSurface->Release();
2478
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002479 // It turns out that D3D will sometimes produce more error
2480 // codes than those documented.
2481 if (checkDeviceLost(result))
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002482 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002483 else
2484 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002485 UNREACHABLE();
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002486 return;
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002487 }
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002488
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002489 }
2490
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002491 if (directToPixels)
2492 {
2493 systemSurface->Release();
2494 return;
2495 }
2496
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002497 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002498 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2499 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2500 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2501 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2502 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002503
2504 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2505
2506 if (FAILED(result))
2507 {
2508 UNREACHABLE();
2509 systemSurface->Release();
2510
2511 return; // No sensible error to generate
2512 }
2513
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002514 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002515 unsigned short *dest16 = (unsigned short*)pixels;
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002516
2517 unsigned char *source;
2518 int inputPitch;
2519 if (getPackReverseRowOrder())
2520 {
2521 source = (unsigned char*)lock.pBits;
2522 inputPitch = lock.Pitch;
2523 }
2524 else
2525 {
2526 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2527 inputPitch = -lock.Pitch;
2528 }
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002529
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002530 for (int j = 0; j < rect.bottom - rect.top; j++)
2531 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002532 if (desc.Format == D3DFMT_A8R8G8B8 &&
2533 format == GL_BGRA_EXT &&
2534 type == GL_UNSIGNED_BYTE)
2535 {
2536 // Fast path for EXT_read_format_bgra, given
2537 // an RGBA source buffer. Note that buffers with no
2538 // alpha go through the slow path below.
2539 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002540 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002541 (rect.right - rect.left) * 4);
2542 continue;
2543 }
2544
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002545 for (int i = 0; i < rect.right - rect.left; i++)
2546 {
2547 float r;
2548 float g;
2549 float b;
2550 float a;
2551
2552 switch (desc.Format)
2553 {
2554 case D3DFMT_R5G6B5:
2555 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002556 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002557
2558 a = 1.0f;
2559 b = (rgb & 0x001F) * (1.0f / 0x001F);
2560 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2561 r = (rgb & 0xF800) * (1.0f / 0xF800);
2562 }
2563 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002564 case D3DFMT_A1R5G5B5:
2565 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002566 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002567
2568 a = (argb & 0x8000) ? 1.0f : 0.0f;
2569 b = (argb & 0x001F) * (1.0f / 0x001F);
2570 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2571 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2572 }
2573 break;
2574 case D3DFMT_A8R8G8B8:
2575 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002576 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002577
2578 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2579 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2580 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2581 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2582 }
2583 break;
2584 case D3DFMT_X8R8G8B8:
2585 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002586 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002587
2588 a = 1.0f;
2589 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2590 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2591 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2592 }
2593 break;
2594 case D3DFMT_A2R10G10B10:
2595 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002596 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002597
2598 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2599 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2600 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2601 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2602 }
2603 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002604 case D3DFMT_A32B32G32R32F:
2605 {
2606 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002607 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2608 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2609 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2610 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002611 }
2612 break;
2613 case D3DFMT_A16B16G16R16F:
2614 {
2615 // float formats in D3D are stored rgba, rather than the other way round
2616 float abgr[4];
2617
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002618 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002619
2620 a = abgr[3];
2621 b = abgr[2];
2622 g = abgr[1];
2623 r = abgr[0];
2624 }
2625 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002626 default:
2627 UNIMPLEMENTED(); // FIXME
2628 UNREACHABLE();
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00002629 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002630 }
2631
2632 switch (format)
2633 {
2634 case GL_RGBA:
2635 switch (type)
2636 {
2637 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002638 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2639 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2640 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2641 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002642 break;
2643 default: UNREACHABLE();
2644 }
2645 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002646 case GL_BGRA_EXT:
2647 switch (type)
2648 {
2649 case GL_UNSIGNED_BYTE:
2650 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2651 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2652 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2653 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2654 break;
2655 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2656 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2657 // this type is packed as follows:
2658 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2659 // --------------------------------------------------------------------------------
2660 // | 4th | 3rd | 2nd | 1st component |
2661 // --------------------------------------------------------------------------------
2662 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2663 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2664 ((unsigned short)(15 * a + 0.5f) << 12)|
2665 ((unsigned short)(15 * r + 0.5f) << 8) |
2666 ((unsigned short)(15 * g + 0.5f) << 4) |
2667 ((unsigned short)(15 * b + 0.5f) << 0);
2668 break;
2669 case GL_UNSIGNED_SHORT_1_5_5_5_REV_EXT:
2670 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2671 // this type is packed as follows:
2672 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2673 // --------------------------------------------------------------------------------
2674 // | 4th | 3rd | 2nd | 1st component |
2675 // --------------------------------------------------------------------------------
2676 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2677 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2678 ((unsigned short)( a + 0.5f) << 15) |
2679 ((unsigned short)(31 * r + 0.5f) << 10) |
2680 ((unsigned short)(31 * g + 0.5f) << 5) |
2681 ((unsigned short)(31 * b + 0.5f) << 0);
2682 break;
2683 default: UNREACHABLE();
2684 }
2685 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002686 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002687 switch (type)
2688 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002689 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002690 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2691 ((unsigned short)(31 * b + 0.5f) << 0) |
2692 ((unsigned short)(63 * g + 0.5f) << 5) |
2693 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002694 break;
2695 default: UNREACHABLE();
2696 }
2697 break;
2698 default: UNREACHABLE();
2699 }
2700 }
2701 }
2702
2703 systemSurface->UnlockRect();
2704
2705 systemSurface->Release();
2706}
2707
2708void Context::clear(GLbitfield mask)
2709{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002710 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002711
2712 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2713 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002714 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002715 }
2716
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002717 DWORD flags = 0;
2718
2719 if (mask & GL_COLOR_BUFFER_BIT)
2720 {
2721 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002722
2723 if (framebufferObject->getColorbufferType() != GL_NONE)
2724 {
2725 flags |= D3DCLEAR_TARGET;
2726 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002727 }
2728
2729 if (mask & GL_DEPTH_BUFFER_BIT)
2730 {
2731 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002732 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002733 {
2734 flags |= D3DCLEAR_ZBUFFER;
2735 }
2736 }
2737
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002738 GLuint stencilUnmasked = 0x0;
2739
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002740 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002741 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002742 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002743 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002744 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002745 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002746 if (!depthStencil)
2747 {
2748 ERR("Depth stencil pointer unexpectedly null.");
2749 return;
2750 }
2751
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002752 D3DSURFACE_DESC desc;
2753 depthStencil->GetDesc(&desc);
daniel@transgaming.com63e6afe2012-05-31 01:14:42 +00002754 depthStencil->Release();
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002755
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002756 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002757 stencilUnmasked = (0x1 << stencilSize) - 1;
2758
2759 if (stencilUnmasked != 0x0)
2760 {
2761 flags |= D3DCLEAR_STENCIL;
2762 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002763 }
2764 }
2765
2766 if (mask != 0)
2767 {
2768 return error(GL_INVALID_VALUE);
2769 }
2770
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002771 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2772 {
2773 return;
2774 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002775
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002776 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002777 unorm<8>(mState.colorClearValue.red),
2778 unorm<8>(mState.colorClearValue.green),
2779 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002780 float depth = clamp01(mState.depthClearValue);
2781 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002782
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002783 bool alphaUnmasked = (dx2es::GetAlphaSize(mRenderTargetDesc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002784
2785 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002786 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002787 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002788 !(mState.colorMaskRed && mState.colorMaskGreen &&
2789 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002790
2791 if (needMaskedColorClear || needMaskedStencilClear)
2792 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002793 // State which is altered in all paths from this point to the clear call is saved.
2794 // State which is altered in only some paths will be flagged dirty in the case that
2795 // that path is taken.
2796 HRESULT hr;
2797 if (mMaskedClearSavedState == NULL)
2798 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002799 hr = mDevice->BeginStateBlock();
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002800 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2801
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002802 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2803 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2804 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2805 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2806 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2807 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2808 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2809 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2810 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2811 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2812 mDevice->SetPixelShader(NULL);
2813 mDevice->SetVertexShader(NULL);
2814 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2815 mDevice->SetStreamSource(0, NULL, 0, 0);
2816 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2817 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2818 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2819 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2820 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2821 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2822 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002823
2824 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2825 {
2826 mDevice->SetStreamSourceFreq(i, 1);
2827 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002828
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002829 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002830 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2831 }
2832
2833 ASSERT(mMaskedClearSavedState != NULL);
2834
2835 if (mMaskedClearSavedState != NULL)
2836 {
2837 hr = mMaskedClearSavedState->Capture();
2838 ASSERT(SUCCEEDED(hr));
2839 }
2840
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002841 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2842 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2843 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2844 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2845 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2846 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2847 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2848 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002849
2850 if (flags & D3DCLEAR_TARGET)
2851 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002852 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002853 }
2854 else
2855 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002856 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002857 }
2858
2859 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2860 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002861 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2862 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2863 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2864 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
2865 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
2866 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
2867 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2868 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002869 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002870 }
2871 else
2872 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002873 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002874 }
2875
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002876 mDevice->SetPixelShader(NULL);
2877 mDevice->SetVertexShader(NULL);
2878 mDevice->SetFVF(D3DFVF_XYZRHW);
2879 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2880 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2881 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2882 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2883 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2884 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2885 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002886
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002887 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2888 {
2889 mDevice->SetStreamSourceFreq(i, 1);
2890 }
2891
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002892 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2893 quad[0][0] = -0.5f;
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002894 quad[0][1] = mRenderTargetDesc.Height - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002895 quad[0][2] = 0.0f;
2896 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002897
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002898 quad[1][0] = mRenderTargetDesc.Width - 0.5f;
2899 quad[1][1] = mRenderTargetDesc.Height - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002900 quad[1][2] = 0.0f;
2901 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002902
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002903 quad[2][0] = -0.5f;
2904 quad[2][1] = -0.5f;
2905 quad[2][2] = 0.0f;
2906 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002907
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002908 quad[3][0] = mRenderTargetDesc.Width - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002909 quad[3][1] = -0.5f;
2910 quad[3][2] = 0.0f;
2911 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002912
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002913 mDisplay->startScene();
2914 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002915
2916 if (flags & D3DCLEAR_ZBUFFER)
2917 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002918 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
2919 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2920 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002921 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002922
2923 if (mMaskedClearSavedState != NULL)
2924 {
2925 mMaskedClearSavedState->Apply();
2926 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002927 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002928 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002929 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002930 mDevice->Clear(0, NULL, flags, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002931 }
2932}
2933
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002934void Context::drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002935{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002936 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002937 {
2938 return error(GL_INVALID_OPERATION);
2939 }
2940
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002941 D3DPRIMITIVETYPE primitiveType;
2942 int primitiveCount;
2943
2944 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2945 return error(GL_INVALID_ENUM);
2946
2947 if (primitiveCount <= 0)
2948 {
2949 return;
2950 }
2951
2952 if (!applyRenderTarget(false))
2953 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002954 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002955 }
2956
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002957 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002958
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002959 GLsizei repeatDraw = 1;
2960 GLenum err = applyVertexBuffer(first, count, instances, &repeatDraw);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002961 if (err != GL_NO_ERROR)
2962 {
2963 return error(err);
2964 }
2965
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002966 applyShaders();
2967 applyTextures();
2968
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002969 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002970 {
2971 return error(GL_INVALID_OPERATION);
2972 }
2973
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002974 if (!cullSkipsDraw(mode))
2975 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002976 mDisplay->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002977
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002978 if (mode == GL_LINE_LOOP)
daniel@transgaming.comf6549452012-01-27 15:39:08 +00002979 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002980 drawLineLoop(count, GL_NONE, NULL, 0);
daniel@transgaming.comf6549452012-01-27 15:39:08 +00002981 }
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002982 else if (instances > 0)
daniel@transgaming.comf6549452012-01-27 15:39:08 +00002983 {
2984 StaticIndexBuffer *countingIB = mIndexDataManager->getCountingIndices(count);
2985 if (countingIB)
2986 {
2987 if (mAppliedIBSerial != countingIB->getSerial())
2988 {
2989 mDevice->SetIndices(countingIB->getBuffer());
2990 mAppliedIBSerial = countingIB->getSerial();
2991 }
2992
daniel@transgaming.comd6449312012-01-27 15:39:32 +00002993 for (int i = 0; i < repeatDraw; i++)
2994 {
2995 mDevice->DrawIndexedPrimitive(primitiveType, 0, 0, count, 0, primitiveCount);
2996 }
daniel@transgaming.comf6549452012-01-27 15:39:08 +00002997 }
2998 else
2999 {
3000 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
3001 return error(GL_OUT_OF_MEMORY);
3002 }
3003 }
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003004 else // Regular case
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003005 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003006 mDevice->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003007 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003008 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003009}
3010
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00003011void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, GLsizei instances)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003012{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003013 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003014 {
3015 return error(GL_INVALID_OPERATION);
3016 }
3017
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003018 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003019 {
3020 return error(GL_INVALID_OPERATION);
3021 }
3022
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003023 D3DPRIMITIVETYPE primitiveType;
3024 int primitiveCount;
3025
3026 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
3027 return error(GL_INVALID_ENUM);
3028
3029 if (primitiveCount <= 0)
3030 {
3031 return;
3032 }
3033
3034 if (!applyRenderTarget(false))
3035 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00003036 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003037 }
3038
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003039 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00003040
3041 TranslatedIndexData indexInfo;
3042 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
3043 if (err != GL_NO_ERROR)
3044 {
3045 return error(err);
3046 }
3047
daniel@transgaming.com83921382011-01-08 05:46:00 +00003048 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
daniel@transgaming.comd6449312012-01-27 15:39:32 +00003049 GLsizei repeatDraw = 1;
3050 err = applyVertexBuffer(indexInfo.minIndex, vertexCount, instances, &repeatDraw);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00003051 if (err != GL_NO_ERROR)
3052 {
3053 return error(err);
3054 }
3055
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003056 applyShaders();
3057 applyTextures();
3058
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00003059 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00003060 {
3061 return error(GL_INVALID_OPERATION);
3062 }
3063
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003064 if (!cullSkipsDraw(mode))
3065 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003066 mDisplay->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003067
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003068 if (mode == GL_LINE_LOOP)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003069 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003070 drawLineLoop(count, type, indices, indexInfo.minIndex);
3071 }
3072 else
3073 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00003074 for (int i = 0; i < repeatDraw; i++)
3075 {
3076 mDevice->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
3077 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003078 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003079 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003080}
3081
daniel@transgaming.com0d86aa72011-10-26 02:35:10 +00003082// Implements glFlush when block is false, glFinish when block is true
3083void Context::sync(bool block)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003084{
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +00003085 mDisplay->sync(block);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003086}
3087
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003088void Context::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003089{
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003090 // Get the raw indices for an indexed draw
3091 if (type != GL_NONE && mState.elementArrayBuffer.get())
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003092 {
3093 Buffer *indexBuffer = mState.elementArrayBuffer.get();
3094 intptr_t offset = reinterpret_cast<intptr_t>(indices);
3095 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
3096 }
3097
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003098 UINT startIndex = 0;
3099 bool succeeded = false;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003100
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003101 if (supports32bitIndices())
3102 {
3103 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
3104
3105 if (!mLineLoopIB)
3106 {
3107 mLineLoopIB = new StreamingIndexBuffer(mDevice, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
3108 }
3109
3110 if (mLineLoopIB)
3111 {
3112 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
3113
3114 UINT offset = 0;
3115 unsigned int *data = static_cast<unsigned int*>(mLineLoopIB->map(spaceNeeded, &offset));
3116 startIndex = offset / 4;
3117
3118 if (data)
3119 {
3120 switch (type)
3121 {
3122 case GL_NONE: // Non-indexed draw
3123 for (int i = 0; i < count; i++)
3124 {
3125 data[i] = i;
3126 }
3127 data[count] = 0;
3128 break;
3129 case GL_UNSIGNED_BYTE:
3130 for (int i = 0; i < count; i++)
3131 {
3132 data[i] = static_cast<const GLubyte*>(indices)[i];
3133 }
3134 data[count] = static_cast<const GLubyte*>(indices)[0];
3135 break;
3136 case GL_UNSIGNED_SHORT:
3137 for (int i = 0; i < count; i++)
3138 {
3139 data[i] = static_cast<const GLushort*>(indices)[i];
3140 }
3141 data[count] = static_cast<const GLushort*>(indices)[0];
3142 break;
3143 case GL_UNSIGNED_INT:
3144 for (int i = 0; i < count; i++)
3145 {
3146 data[i] = static_cast<const GLuint*>(indices)[i];
3147 }
3148 data[count] = static_cast<const GLuint*>(indices)[0];
3149 break;
3150 default: UNREACHABLE();
3151 }
3152
3153 mLineLoopIB->unmap();
3154 succeeded = true;
3155 }
3156 }
3157 }
3158 else
3159 {
3160 const int spaceNeeded = (count + 1) * sizeof(unsigned short);
3161
3162 if (!mLineLoopIB)
3163 {
3164 mLineLoopIB = new StreamingIndexBuffer(mDevice, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
3165 }
3166
3167 if (mLineLoopIB)
3168 {
3169 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
3170
3171 UINT offset = 0;
3172 unsigned short *data = static_cast<unsigned short*>(mLineLoopIB->map(spaceNeeded, &offset));
3173 startIndex = offset / 2;
3174
3175 if (data)
3176 {
3177 switch (type)
3178 {
3179 case GL_NONE: // Non-indexed draw
3180 for (int i = 0; i < count; i++)
3181 {
3182 data[i] = i;
3183 }
3184 data[count] = 0;
3185 break;
3186 case GL_UNSIGNED_BYTE:
3187 for (int i = 0; i < count; i++)
3188 {
3189 data[i] = static_cast<const GLubyte*>(indices)[i];
3190 }
3191 data[count] = static_cast<const GLubyte*>(indices)[0];
3192 break;
3193 case GL_UNSIGNED_SHORT:
3194 for (int i = 0; i < count; i++)
3195 {
3196 data[i] = static_cast<const GLushort*>(indices)[i];
3197 }
3198 data[count] = static_cast<const GLushort*>(indices)[0];
3199 break;
3200 case GL_UNSIGNED_INT:
3201 for (int i = 0; i < count; i++)
3202 {
3203 data[i] = static_cast<const GLuint*>(indices)[i];
3204 }
3205 data[count] = static_cast<const GLuint*>(indices)[0];
3206 break;
3207 default: UNREACHABLE();
3208 }
3209
3210 mLineLoopIB->unmap();
3211 succeeded = true;
3212 }
3213 }
3214 }
3215
3216 if (succeeded)
3217 {
3218 if (mAppliedIBSerial != mLineLoopIB->getSerial())
3219 {
3220 mDevice->SetIndices(mLineLoopIB->getBuffer());
3221 mAppliedIBSerial = mLineLoopIB->getSerial();
3222 }
3223
3224 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
3225 }
3226 else
3227 {
3228 ERR("Could not create a looping index buffer for GL_LINE_LOOP.");
3229 return error(GL_OUT_OF_MEMORY);
3230 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003231}
3232
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003233void Context::recordInvalidEnum()
3234{
3235 mInvalidEnum = true;
3236}
3237
3238void Context::recordInvalidValue()
3239{
3240 mInvalidValue = true;
3241}
3242
3243void Context::recordInvalidOperation()
3244{
3245 mInvalidOperation = true;
3246}
3247
3248void Context::recordOutOfMemory()
3249{
3250 mOutOfMemory = true;
3251}
3252
3253void Context::recordInvalidFramebufferOperation()
3254{
3255 mInvalidFramebufferOperation = true;
3256}
3257
3258// Get one of the recorded errors and clear its flag, if any.
3259// [OpenGL ES 2.0.24] section 2.5 page 13.
3260GLenum Context::getError()
3261{
3262 if (mInvalidEnum)
3263 {
3264 mInvalidEnum = false;
3265
3266 return GL_INVALID_ENUM;
3267 }
3268
3269 if (mInvalidValue)
3270 {
3271 mInvalidValue = false;
3272
3273 return GL_INVALID_VALUE;
3274 }
3275
3276 if (mInvalidOperation)
3277 {
3278 mInvalidOperation = false;
3279
3280 return GL_INVALID_OPERATION;
3281 }
3282
3283 if (mOutOfMemory)
3284 {
3285 mOutOfMemory = false;
3286
3287 return GL_OUT_OF_MEMORY;
3288 }
3289
3290 if (mInvalidFramebufferOperation)
3291 {
3292 mInvalidFramebufferOperation = false;
3293
3294 return GL_INVALID_FRAMEBUFFER_OPERATION;
3295 }
3296
3297 return GL_NO_ERROR;
3298}
3299
daniel@transgaming.com17f548c2011-11-09 17:47:02 +00003300GLenum Context::getResetStatus()
3301{
3302 if (mResetStatus == GL_NO_ERROR)
3303 {
3304 bool lost = mDisplay->testDeviceLost();
3305
3306 if (lost)
3307 {
3308 mDisplay->notifyDeviceLost(); // Sets mResetStatus
3309 }
3310 }
3311
3312 GLenum status = mResetStatus;
3313
3314 if (mResetStatus != GL_NO_ERROR)
3315 {
3316 if (mDisplay->testDeviceResettable())
3317 {
3318 mResetStatus = GL_NO_ERROR;
3319 }
3320 }
3321
3322 return status;
3323}
3324
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00003325bool Context::isResetNotificationEnabled()
3326{
3327 return (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT);
3328}
3329
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003330bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003331{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003332 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003333}
3334
daniel@transgaming.com396c6432010-11-26 16:26:12 +00003335int Context::getMaximumVaryingVectors() const
3336{
3337 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3338}
3339
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003340unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003341{
3342 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3343}
3344
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003345unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003346{
3347 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3348}
3349
daniel@transgaming.com458da142010-11-28 02:03:02 +00003350int Context::getMaximumFragmentUniformVectors() const
3351{
3352 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3353}
3354
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003355int Context::getMaxSupportedSamples() const
3356{
3357 return mMaxSupportedSamples;
3358}
3359
3360int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3361{
3362 if (requested == 0)
3363 {
3364 return requested;
3365 }
3366
3367 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3368 if (itr == mMultiSampleSupport.end())
3369 {
3370 return -1;
3371 }
3372
3373 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3374 {
3375 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3376 {
3377 return i;
3378 }
3379 }
3380
3381 return -1;
3382}
3383
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003384bool Context::supportsEventQueries() const
3385{
3386 return mSupportsEventQueries;
3387}
3388
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00003389bool Context::supportsOcclusionQueries() const
3390{
3391 return mSupportsOcclusionQueries;
3392}
3393
gman@chromium.org50c526d2011-08-10 05:19:44 +00003394bool Context::supportsDXT1Textures() const
daniel@transgaming.com01868132010-08-24 19:21:17 +00003395{
gman@chromium.org50c526d2011-08-10 05:19:44 +00003396 return mSupportsDXT1Textures;
3397}
3398
3399bool Context::supportsDXT3Textures() const
3400{
3401 return mSupportsDXT3Textures;
3402}
3403
3404bool Context::supportsDXT5Textures() const
3405{
3406 return mSupportsDXT5Textures;
daniel@transgaming.com01868132010-08-24 19:21:17 +00003407}
3408
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003409bool Context::supportsFloat32Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003410{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003411 return mSupportsFloat32Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003412}
3413
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003414bool Context::supportsFloat32LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003415{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003416 return mSupportsFloat32LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003417}
3418
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003419bool Context::supportsFloat32RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003420{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003421 return mSupportsFloat32RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003422}
3423
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003424bool Context::supportsFloat16Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003425{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003426 return mSupportsFloat16Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003427}
3428
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003429bool Context::supportsFloat16LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003430{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003431 return mSupportsFloat16LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003432}
3433
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003434bool Context::supportsFloat16RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003435{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003436 return mSupportsFloat16RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003437}
3438
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003439int Context::getMaximumRenderbufferDimension() const
3440{
3441 return mMaxRenderbufferDimension;
3442}
3443
3444int Context::getMaximumTextureDimension() const
3445{
3446 return mMaxTextureDimension;
3447}
3448
3449int Context::getMaximumCubeTextureDimension() const
3450{
3451 return mMaxCubeTextureDimension;
3452}
3453
3454int Context::getMaximumTextureLevel() const
3455{
3456 return mMaxTextureLevel;
3457}
3458
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003459bool Context::supportsLuminanceTextures() const
3460{
3461 return mSupportsLuminanceTextures;
3462}
3463
3464bool Context::supportsLuminanceAlphaTextures() const
3465{
3466 return mSupportsLuminanceAlphaTextures;
3467}
3468
daniel@transgaming.com1c49f792012-05-31 01:14:02 +00003469bool Context::supportsDepthTextures() const
3470{
3471 return mSupportsDepthTextures;
3472}
3473
daniel@transgaming.com83921382011-01-08 05:46:00 +00003474bool Context::supports32bitIndices() const
3475{
3476 return mSupports32bitIndices;
3477}
3478
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003479bool Context::supportsNonPower2Texture() const
3480{
3481 return mSupportsNonPower2Texture;
3482}
3483
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +00003484bool Context::supportsInstancing() const
3485{
3486 return mSupportsInstancing;
3487}
3488
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003489void Context::detachBuffer(GLuint buffer)
3490{
3491 // [OpenGL ES 2.0.24] section 2.9 page 22:
3492 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3493 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3494
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003495 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003496 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003497 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003498 }
3499
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003500 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003501 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003502 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003503 }
3504
3505 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3506 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003507 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003508 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003509 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003510 }
3511 }
3512}
3513
3514void Context::detachTexture(GLuint texture)
3515{
3516 // [OpenGL ES 2.0.24] section 3.8 page 84:
3517 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3518 // rebound to texture object zero
3519
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003520 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003521 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003522 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003523 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003524 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003525 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003526 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003527 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003528 }
3529 }
3530
3531 // [OpenGL ES 2.0.24] section 4.4 page 112:
3532 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3533 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3534 // image was attached in the currently bound framebuffer.
3535
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003536 Framebuffer *readFramebuffer = getReadFramebuffer();
3537 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003538
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003539 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003540 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003541 readFramebuffer->detachTexture(texture);
3542 }
3543
3544 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3545 {
3546 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003547 }
3548}
3549
3550void Context::detachFramebuffer(GLuint framebuffer)
3551{
3552 // [OpenGL ES 2.0.24] section 4.4 page 107:
3553 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3554 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3555
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003556 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003557 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003558 bindReadFramebuffer(0);
3559 }
3560
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003561 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003562 {
3563 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003564 }
3565}
3566
3567void Context::detachRenderbuffer(GLuint renderbuffer)
3568{
3569 // [OpenGL ES 2.0.24] section 4.4 page 109:
3570 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3571 // had been executed with the target RENDERBUFFER and name of zero.
3572
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003573 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003574 {
3575 bindRenderbuffer(0);
3576 }
3577
3578 // [OpenGL ES 2.0.24] section 4.4 page 111:
3579 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3580 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3581 // point to which this image was attached in the currently bound framebuffer.
3582
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003583 Framebuffer *readFramebuffer = getReadFramebuffer();
3584 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003585
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003586 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003587 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003588 readFramebuffer->detachRenderbuffer(renderbuffer);
3589 }
3590
3591 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3592 {
3593 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003594 }
3595}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003596
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003597Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003598{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003599 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003600
3601 if (t == NULL)
3602 {
3603 static const GLubyte color[] = { 0, 0, 0, 255 };
3604
3605 switch (type)
3606 {
3607 default:
3608 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003609 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003610
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003611 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003612 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003613 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003614 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003615 t = incomplete2d;
3616 }
3617 break;
3618
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003619 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003620 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003621 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003622
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003623 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3624 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3625 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3626 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3627 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3628 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003629
3630 t = incompleteCube;
3631 }
3632 break;
3633 }
3634
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003635 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003636 }
3637
3638 return t;
3639}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003640
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003641bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003642{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003643 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003644}
3645
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003646bool Context::isTriangleMode(GLenum drawMode)
3647{
3648 switch (drawMode)
3649 {
3650 case GL_TRIANGLES:
3651 case GL_TRIANGLE_FAN:
3652 case GL_TRIANGLE_STRIP:
3653 return true;
3654 case GL_POINTS:
3655 case GL_LINES:
3656 case GL_LINE_LOOP:
3657 case GL_LINE_STRIP:
3658 return false;
3659 default: UNREACHABLE();
3660 }
3661
3662 return false;
3663}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003664
3665void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3666{
3667 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3668
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003669 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3670 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3671 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3672 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003673
daniel@transgaming.com83921382011-01-08 05:46:00 +00003674 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003675}
3676
daniel@transgaming.comd2820bf2012-01-27 15:38:48 +00003677void Context::setVertexAttribDivisor(GLuint index, GLuint divisor)
3678{
3679 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3680
3681 mState.vertexAttribute[index].mDivisor = divisor;
3682}
3683
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003684// keep list sorted in following order
3685// OES extensions
3686// EXT extensions
3687// Vendor extensions
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003688void Context::initExtensionString()
3689{
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003690 mExtensionString = "";
3691
3692 // OES extensions
3693 if (supports32bitIndices())
3694 {
3695 mExtensionString += "GL_OES_element_index_uint ";
3696 }
3697
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003698 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003699 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003700 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003701
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003702 if (supportsFloat16Textures())
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003703 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003704 mExtensionString += "GL_OES_texture_half_float ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003705 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003706 if (supportsFloat16LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003707 {
3708 mExtensionString += "GL_OES_texture_half_float_linear ";
3709 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003710 if (supportsFloat32Textures())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003711 {
3712 mExtensionString += "GL_OES_texture_float ";
3713 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003714 if (supportsFloat32LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003715 {
3716 mExtensionString += "GL_OES_texture_float_linear ";
3717 }
3718
3719 if (supportsNonPower2Texture())
3720 {
3721 mExtensionString += "GL_OES_texture_npot ";
3722 }
3723
3724 // Multi-vendor (EXT) extensions
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00003725 if (supportsOcclusionQueries())
3726 {
3727 mExtensionString += "GL_EXT_occlusion_query_boolean ";
3728 }
3729
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003730 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com8747f182011-11-09 17:50:38 +00003731 mExtensionString += "GL_EXT_robustness ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003732
gman@chromium.org50c526d2011-08-10 05:19:44 +00003733 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00003734 {
3735 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3736 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003737
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003738 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
daniel@transgaming.comdf363722011-12-16 23:29:53 +00003739 mExtensionString += "GL_EXT_texture_storage ";
gman@chromium.org50c526d2011-08-10 05:19:44 +00003740
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003741 // ANGLE-specific extensions
3742 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003743 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003744 {
3745 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3746 }
3747
daniel@transgaming.comc6f7f9d2012-01-27 15:40:00 +00003748 if (supportsInstancing())
daniel@transgaming.comdce02fd2012-01-27 15:39:51 +00003749 {
3750 mExtensionString += "GL_ANGLE_instanced_arrays ";
3751 }
3752
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00003753 mExtensionString += "GL_ANGLE_pack_reverse_row_order ";
3754
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003755 if (supportsDXT3Textures())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003756 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003757 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3758 }
3759 if (supportsDXT5Textures())
3760 {
3761 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003762 }
daniel@transgaming.com97412f72011-11-11 04:19:07 +00003763
3764 mExtensionString += "GL_ANGLE_texture_usage ";
zmo@google.coma574f782011-10-03 21:45:23 +00003765 mExtensionString += "GL_ANGLE_translated_shader_source ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003766
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003767 // Other vendor-specific extensions
3768 if (supportsEventQueries())
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003769 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003770 mExtensionString += "GL_NV_fence ";
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003771 }
3772
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003773 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3774 if (end != std::string::npos)
3775 {
3776 mExtensionString.resize(end+1);
3777 }
3778}
3779
3780const char *Context::getExtensionString() const
3781{
3782 return mExtensionString.c_str();
3783}
3784
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003785void Context::initRendererString()
3786{
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003787 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003788
3789 mRendererString = "ANGLE (";
3790 mRendererString += identifier->Description;
3791 mRendererString += ")";
3792}
3793
3794const char *Context::getRendererString() const
3795{
3796 return mRendererString.c_str();
3797}
3798
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003799void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3800 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3801 GLbitfield mask)
3802{
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003803 Framebuffer *readFramebuffer = getReadFramebuffer();
3804 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3805
3806 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3807 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3808 {
3809 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3810 }
3811
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003812 if (drawFramebuffer->getSamples() != 0)
3813 {
3814 return error(GL_INVALID_OPERATION);
3815 }
3816
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003817 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3818 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3819 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3820 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3821
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003822 RECT sourceRect;
3823 RECT destRect;
3824
3825 if (srcX0 < srcX1)
3826 {
3827 sourceRect.left = srcX0;
3828 sourceRect.right = srcX1;
3829 destRect.left = dstX0;
3830 destRect.right = dstX1;
3831 }
3832 else
3833 {
3834 sourceRect.left = srcX1;
3835 destRect.left = dstX1;
3836 sourceRect.right = srcX0;
3837 destRect.right = dstX0;
3838 }
3839
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003840 if (srcY0 < srcY1)
3841 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003842 sourceRect.top = readBufferHeight - srcY1;
3843 destRect.top = drawBufferHeight - dstY1;
3844 sourceRect.bottom = readBufferHeight - srcY0;
3845 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003846 }
3847 else
3848 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003849 sourceRect.top = readBufferHeight - srcY0;
3850 destRect.top = drawBufferHeight - dstY0;
3851 sourceRect.bottom = readBufferHeight - srcY1;
3852 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003853 }
3854
3855 RECT sourceScissoredRect = sourceRect;
3856 RECT destScissoredRect = destRect;
3857
3858 if (mState.scissorTest)
3859 {
3860 // Only write to parts of the destination framebuffer which pass the scissor test
3861 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3862 // rect will be checked against scissorY, rather than the bottom.
3863 if (destRect.left < mState.scissorX)
3864 {
3865 int xDiff = mState.scissorX - destRect.left;
3866 destScissoredRect.left = mState.scissorX;
3867 sourceScissoredRect.left += xDiff;
3868 }
3869
3870 if (destRect.right > mState.scissorX + mState.scissorWidth)
3871 {
3872 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3873 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3874 sourceScissoredRect.right -= xDiff;
3875 }
3876
3877 if (destRect.top < mState.scissorY)
3878 {
3879 int yDiff = mState.scissorY - destRect.top;
3880 destScissoredRect.top = mState.scissorY;
3881 sourceScissoredRect.top += yDiff;
3882 }
3883
3884 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3885 {
3886 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3887 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3888 sourceScissoredRect.bottom -= yDiff;
3889 }
3890 }
3891
3892 bool blitRenderTarget = false;
3893 bool blitDepthStencil = false;
3894
3895 RECT sourceTrimmedRect = sourceScissoredRect;
3896 RECT destTrimmedRect = destScissoredRect;
3897
3898 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3899 // the actual draw and read surfaces.
3900 if (sourceTrimmedRect.left < 0)
3901 {
3902 int xDiff = 0 - sourceTrimmedRect.left;
3903 sourceTrimmedRect.left = 0;
3904 destTrimmedRect.left += xDiff;
3905 }
3906
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003907 if (sourceTrimmedRect.right > readBufferWidth)
3908 {
3909 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3910 sourceTrimmedRect.right = readBufferWidth;
3911 destTrimmedRect.right -= xDiff;
3912 }
3913
3914 if (sourceTrimmedRect.top < 0)
3915 {
3916 int yDiff = 0 - sourceTrimmedRect.top;
3917 sourceTrimmedRect.top = 0;
3918 destTrimmedRect.top += yDiff;
3919 }
3920
3921 if (sourceTrimmedRect.bottom > readBufferHeight)
3922 {
3923 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3924 sourceTrimmedRect.bottom = readBufferHeight;
3925 destTrimmedRect.bottom -= yDiff;
3926 }
3927
3928 if (destTrimmedRect.left < 0)
3929 {
3930 int xDiff = 0 - destTrimmedRect.left;
3931 destTrimmedRect.left = 0;
3932 sourceTrimmedRect.left += xDiff;
3933 }
3934
3935 if (destTrimmedRect.right > drawBufferWidth)
3936 {
3937 int xDiff = destTrimmedRect.right - drawBufferWidth;
3938 destTrimmedRect.right = drawBufferWidth;
3939 sourceTrimmedRect.right -= xDiff;
3940 }
3941
3942 if (destTrimmedRect.top < 0)
3943 {
3944 int yDiff = 0 - destTrimmedRect.top;
3945 destTrimmedRect.top = 0;
3946 sourceTrimmedRect.top += yDiff;
3947 }
3948
3949 if (destTrimmedRect.bottom > drawBufferHeight)
3950 {
3951 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3952 destTrimmedRect.bottom = drawBufferHeight;
3953 sourceTrimmedRect.bottom -= yDiff;
3954 }
3955
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003956 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003957 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3958 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3959 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3960 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003961 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3962 {
3963 partialBufferCopy = true;
3964 }
3965
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003966 if (mask & GL_COLOR_BUFFER_BIT)
3967 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003968 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3969 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3970 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3971 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3972 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003973 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3974 {
3975 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3976 return error(GL_INVALID_OPERATION);
3977 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003978
3979 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3980 {
3981 return error(GL_INVALID_OPERATION);
3982 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003983
3984 blitRenderTarget = true;
3985
3986 }
3987
3988 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3989 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00003990 Renderbuffer *readDSBuffer = NULL;
3991 Renderbuffer *drawDSBuffer = NULL;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003992
3993 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3994 // both a depth and stencil buffer, it will be the same buffer.
3995
3996 if (mask & GL_DEPTH_BUFFER_BIT)
3997 {
3998 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3999 {
4000 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
4001 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
4002 {
4003 return error(GL_INVALID_OPERATION);
4004 }
4005
4006 blitDepthStencil = true;
4007 readDSBuffer = readFramebuffer->getDepthbuffer();
4008 drawDSBuffer = drawFramebuffer->getDepthbuffer();
4009 }
4010 }
4011
4012 if (mask & GL_STENCIL_BUFFER_BIT)
4013 {
4014 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
4015 {
4016 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
4017 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
4018 {
4019 return error(GL_INVALID_OPERATION);
4020 }
4021
4022 blitDepthStencil = true;
4023 readDSBuffer = readFramebuffer->getStencilbuffer();
4024 drawDSBuffer = drawFramebuffer->getStencilbuffer();
4025 }
4026 }
4027
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004028 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004029 {
4030 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
4031 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
4032 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004033
daniel@transgaming.com97446d22010-08-24 19:20:54 +00004034 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
4035 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00004036 {
4037 return error(GL_INVALID_OPERATION);
4038 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004039 }
4040
4041 if (blitRenderTarget || blitDepthStencil)
4042 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00004043 mDisplay->endScene();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004044
4045 if (blitRenderTarget)
4046 {
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00004047 IDirect3DSurface9* readRenderTarget = readFramebuffer->getRenderTarget();
4048 IDirect3DSurface9* drawRenderTarget = drawFramebuffer->getRenderTarget();
4049
4050 HRESULT result = mDevice->StretchRect(readRenderTarget, &sourceTrimmedRect,
4051 drawRenderTarget, &destTrimmedRect, D3DTEXF_NONE);
4052
4053 readRenderTarget->Release();
4054 drawRenderTarget->Release();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004055
4056 if (FAILED(result))
4057 {
4058 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
4059 return;
4060 }
4061 }
4062
4063 if (blitDepthStencil)
4064 {
daniel@transgaming.com63e6afe2012-05-31 01:14:42 +00004065 IDirect3DSurface9* readDepthStencil = readFramebuffer->getDepthStencil();
4066 IDirect3DSurface9* drawDepthStencil = drawFramebuffer->getDepthStencil();
4067
4068 HRESULT result = mDevice->StretchRect(readDepthStencil, NULL, drawDepthStencil, NULL, D3DTEXF_NONE);
4069
4070 readDepthStencil->Release();
4071 drawDepthStencil->Release();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004072
4073 if (FAILED(result))
4074 {
4075 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
4076 return;
4077 }
4078 }
4079 }
4080}
4081
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004082VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
4083{
4084 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4085 {
4086 mVertexDeclCache[i].vertexDeclaration = NULL;
4087 mVertexDeclCache[i].lruCount = 0;
4088 }
4089}
4090
4091VertexDeclarationCache::~VertexDeclarationCache()
4092{
4093 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4094 {
4095 if (mVertexDeclCache[i].vertexDeclaration)
4096 {
4097 mVertexDeclCache[i].vertexDeclaration->Release();
4098 }
4099 }
4100}
4101
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004102GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], Program *program, GLsizei instances, GLsizei *repeatDraw)
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004103{
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004104 *repeatDraw = 1;
4105
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004106 int indexedAttribute = MAX_VERTEX_ATTRIBS;
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004107 int instancedAttribute = MAX_VERTEX_ATTRIBS;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004108
4109 if (instances > 0)
4110 {
4111 // Find an indexed attribute to be mapped to D3D stream 0
4112 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4113 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004114 if (attributes[i].active)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004115 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004116 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
4117 {
4118 if (attributes[i].divisor == 0)
4119 {
4120 indexedAttribute = i;
4121 }
4122 }
4123 else if (instancedAttribute == MAX_VERTEX_ATTRIBS)
4124 {
4125 if (attributes[i].divisor != 0)
4126 {
4127 instancedAttribute = i;
4128 }
4129 }
4130 else break; // Found both an indexed and instanced attribute
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004131 }
4132 }
4133
4134 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
4135 {
4136 return GL_INVALID_OPERATION;
4137 }
4138 }
4139
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004140 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
4141 D3DVERTEXELEMENT9 *element = &elements[0];
4142
4143 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4144 {
4145 if (attributes[i].active)
4146 {
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004147 int stream = i;
4148
4149 if (instances > 0)
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004150 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004151 // Due to a bug on ATI cards we can't enable instancing when none of the attributes are instanced.
4152 if (instancedAttribute == MAX_VERTEX_ATTRIBS)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004153 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004154 *repeatDraw = instances;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004155 }
4156 else
4157 {
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004158 if (i == indexedAttribute)
4159 {
4160 stream = 0;
4161 }
4162 else if (i == 0)
4163 {
4164 stream = indexedAttribute;
4165 }
4166
4167 UINT frequency = 1;
4168
4169 if (attributes[i].divisor == 0)
4170 {
4171 frequency = D3DSTREAMSOURCE_INDEXEDDATA | instances;
4172 }
4173 else
4174 {
4175 frequency = D3DSTREAMSOURCE_INSTANCEDATA | attributes[i].divisor;
4176 }
4177
4178 device->SetStreamSourceFreq(stream, frequency);
4179 mInstancingEnabled = true;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004180 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004181 }
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004182
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004183 if (mAppliedVBs[stream].serial != attributes[i].serial ||
4184 mAppliedVBs[stream].stride != attributes[i].stride ||
4185 mAppliedVBs[stream].offset != attributes[i].offset)
4186 {
4187 device->SetStreamSource(stream, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
4188 mAppliedVBs[stream].serial = attributes[i].serial;
4189 mAppliedVBs[stream].stride = attributes[i].stride;
4190 mAppliedVBs[stream].offset = attributes[i].offset;
4191 }
4192
4193 element->Stream = stream;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004194 element->Offset = 0;
4195 element->Type = attributes[i].type;
4196 element->Method = D3DDECLMETHOD_DEFAULT;
4197 element->Usage = D3DDECLUSAGE_TEXCOORD;
4198 element->UsageIndex = program->getSemanticIndex(i);
4199 element++;
4200 }
4201 }
4202
daniel@transgaming.comd6449312012-01-27 15:39:32 +00004203 if (instances == 0 || instancedAttribute == MAX_VERTEX_ATTRIBS)
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004204 {
4205 if (mInstancingEnabled)
4206 {
4207 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4208 {
4209 device->SetStreamSourceFreq(i, 1);
4210 }
4211
4212 mInstancingEnabled = false;
4213 }
4214 }
4215
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004216 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
4217 *(element++) = end;
4218
4219 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4220 {
4221 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
4222 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
4223 {
4224 entry->lruCount = ++mMaxLru;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004225 if(entry->vertexDeclaration != mLastSetVDecl)
4226 {
4227 device->SetVertexDeclaration(entry->vertexDeclaration);
4228 mLastSetVDecl = entry->vertexDeclaration;
4229 }
4230
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004231 return GL_NO_ERROR;
4232 }
4233 }
4234
4235 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
4236
4237 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4238 {
4239 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
4240 {
4241 lastCache = &mVertexDeclCache[i];
4242 }
4243 }
4244
4245 if (lastCache->vertexDeclaration != NULL)
4246 {
4247 lastCache->vertexDeclaration->Release();
4248 lastCache->vertexDeclaration = NULL;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004249 // mLastSetVDecl is set to the replacement, so we don't have to worry
4250 // about it.
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004251 }
4252
4253 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
4254 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
4255 device->SetVertexDeclaration(lastCache->vertexDeclaration);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004256 mLastSetVDecl = lastCache->vertexDeclaration;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004257 lastCache->lruCount = ++mMaxLru;
4258
4259 return GL_NO_ERROR;
4260}
4261
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004262void VertexDeclarationCache::markStateDirty()
4263{
4264 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4265 {
4266 mAppliedVBs[i].serial = 0;
4267 }
4268
4269 mLastSetVDecl = NULL;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004270 mInstancingEnabled = true; // Forces it to be disabled when not used
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004271}
4272
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004273}
4274
4275extern "C"
4276{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00004277gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext, bool notifyResets, bool robustAccess)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004278{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00004279 return new gl::Context(config, shareContext, notifyResets, robustAccess);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004280}
4281
4282void glDestroyContext(gl::Context *context)
4283{
4284 delete context;
4285
4286 if (context == gl::getContext())
4287 {
4288 gl::makeCurrent(NULL, NULL, NULL);
4289 }
4290}
4291
4292void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
4293{
4294 gl::makeCurrent(context, display, surface);
4295}
4296
4297gl::Context *glGetCurrentContext()
4298{
4299 return gl::getContext();
4300}
4301}