blob: 9d63e6bfb8f2d280eb16da16d60d843fcd6b823f [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.com5d752f22010-10-07 13:37:20 +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.com5d752f22010-10-07 13:37:20 +0000268
269 mMaxTextureDimension = std::min(std::min((int)mDeviceCaps.MaxTextureWidth, (int)mDeviceCaps.MaxTextureHeight),
270 (int)gl::IMPLEMENTATION_MAX_TEXTURE_SIZE);
271 mMaxCubeTextureDimension = std::min(mMaxTextureDimension, (int)gl::IMPLEMENTATION_MAX_CUBE_MAP_TEXTURE_SIZE);
272 mMaxRenderbufferDimension = mMaxTextureDimension;
273 mMaxTextureLevel = log2(mMaxTextureDimension) + 1;
274 TRACE("MaxTextureDimension=%d, MaxCubeTextureDimension=%d, MaxRenderbufferDimension=%d, MaxTextureLevel=%d",
275 mMaxTextureDimension, mMaxCubeTextureDimension, mMaxRenderbufferDimension, mMaxTextureLevel);
276
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000277 const D3DFORMAT renderBufferFormats[] =
278 {
279 D3DFMT_A8R8G8B8,
daniel@transgaming.com63977542010-08-24 19:21:02 +0000280 D3DFMT_X8R8G8B8,
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000281 D3DFMT_R5G6B5,
282 D3DFMT_D24S8
283 };
284
285 int max = 0;
286 for (int i = 0; i < sizeof(renderBufferFormats) / sizeof(D3DFORMAT); ++i)
287 {
288 bool *multisampleArray = new bool[D3DMULTISAMPLE_16_SAMPLES + 1];
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000289 mDisplay->getMultiSampleSupport(renderBufferFormats[i], multisampleArray);
daniel@transgaming.com1f135d82010-08-24 19:20:36 +0000290 mMultiSampleSupport[renderBufferFormats[i]] = multisampleArray;
291
292 for (int j = D3DMULTISAMPLE_16_SAMPLES; j >= 0; --j)
293 {
294 if (multisampleArray[j] && j != D3DMULTISAMPLE_NONMASKABLE && j > max)
295 {
296 max = j;
297 }
298 }
299 }
300
301 mMaxSupportedSamples = max;
302
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000303 mSupportsEventQueries = mDisplay->getEventQuerySupport();
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000304 mSupportsOcclusionQueries = mDisplay->getOcclusionQuerySupport();
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000305 mSupportsDXT1Textures = mDisplay->getDXT1TextureSupport();
306 mSupportsDXT3Textures = mDisplay->getDXT3TextureSupport();
307 mSupportsDXT5Textures = mDisplay->getDXT5TextureSupport();
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +0000308 mSupportsFloat32Textures = mDisplay->getFloat32TextureSupport(&mSupportsFloat32LinearFilter, &mSupportsFloat32RenderableTextures);
309 mSupportsFloat16Textures = mDisplay->getFloat16TextureSupport(&mSupportsFloat16LinearFilter, &mSupportsFloat16RenderableTextures);
daniel@transgaming.comc941e252011-10-26 02:32:31 +0000310 mSupportsLuminanceTextures = mDisplay->getLuminanceTextureSupport();
311 mSupportsLuminanceAlphaTextures = mDisplay->getLuminanceAlphaTextureSupport();
daniel@transgaming.com01868132010-08-24 19:21:17 +0000312
daniel@transgaming.com83921382011-01-08 05:46:00 +0000313 mSupports32bitIndices = mDeviceCaps.MaxVertexIndex >= (1 << 16);
314
gman@chromium.org50c526d2011-08-10 05:19:44 +0000315 mNumCompressedTextureFormats = 0;
316 if (supportsDXT1Textures())
317 {
318 mNumCompressedTextureFormats += 2;
319 }
320 if (supportsDXT3Textures())
321 {
322 mNumCompressedTextureFormats += 1;
323 }
324 if (supportsDXT5Textures())
325 {
326 mNumCompressedTextureFormats += 1;
327 }
328
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000329 initExtensionString();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +0000330 initRendererString();
daniel@transgaming.comc808c5a2010-05-14 17:31:01 +0000331
332 mState.viewportX = 0;
333 mState.viewportY = 0;
334 mState.viewportWidth = surface->getWidth();
335 mState.viewportHeight = surface->getHeight();
336
337 mState.scissorX = 0;
338 mState.scissorY = 0;
339 mState.scissorWidth = surface->getWidth();
340 mState.scissorHeight = surface->getHeight();
341
342 mHasBeenCurrent = true;
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +0000343 }
344
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000345 // Wrap the existing Direct3D 9 resources into GL objects and assign them to the '0' names
346 IDirect3DSurface9 *defaultRenderTarget = surface->getRenderTarget();
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000347 IDirect3DSurface9 *depthStencil = surface->getDepthStencil();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000348
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000349 Colorbuffer *colorbufferZero = new Colorbuffer(defaultRenderTarget);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +0000350 DepthStencilbuffer *depthStencilbufferZero = new DepthStencilbuffer(depthStencil);
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000351 Framebuffer *framebufferZero = new DefaultFramebuffer(colorbufferZero, depthStencilbufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000352
353 setFramebufferZero(framebufferZero);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +0000355 if (defaultRenderTarget)
356 {
357 defaultRenderTarget->Release();
358 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000359
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000360 if (depthStencil)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000361 {
daniel@transgaming.com0009d622010-03-16 06:23:31 +0000362 depthStencil->Release();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000363 }
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +0000364
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000365 markAllStateDirty();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000366}
367
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000368// 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 +0000369void Context::markAllStateDirty()
370{
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000371 for (int t = 0; t < MAX_TEXTURE_IMAGE_UNITS; t++)
372 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +0000373 mAppliedTextureSerialPS[t] = 0;
374 }
375
376 for (int t = 0; t < MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF; t++)
377 {
378 mAppliedTextureSerialVS[t] = 0;
daniel@transgaming.com38e76e52011-03-21 16:39:10 +0000379 }
380
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +0000381 mAppliedProgramSerial = 0;
daniel@transgaming.com092bd482010-05-12 03:39:36 +0000382 mAppliedRenderTargetSerial = 0;
daniel@transgaming.com339ae702010-05-12 03:40:20 +0000383 mAppliedDepthbufferSerial = 0;
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +0000384 mAppliedStencilbufferSerial = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000385 mAppliedIBSerial = 0;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +0000386 mDepthStencilInitialized = false;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +0000387 mViewportInitialized = false;
388 mRenderTargetDescInitialized = false;
389
390 mVertexDeclarationCache.markStateDirty();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000391
392 mClearStateDirty = true;
393 mCullStateDirty = true;
394 mDepthStateDirty = true;
395 mMaskStateDirty = true;
396 mBlendStateDirty = true;
397 mStencilStateDirty = true;
398 mPolygonOffsetStateDirty = true;
399 mScissorStateDirty = true;
400 mSampleStateDirty = true;
401 mDitherStateDirty = true;
daniel@transgaming.com0d25b002010-07-28 19:21:07 +0000402 mFrontFaceDirty = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +0000403 mDxUniformsDirty = true;
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000404 mCachedCurrentProgram = NULL;
daniel@transgaming.com4fa08332010-05-11 02:29:27 +0000405}
406
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000407void Context::markContextLost()
408{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +0000409 if (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT)
410 mResetStatus = GL_UNKNOWN_CONTEXT_RESET_EXT;
daniel@transgaming.com09fcc9f2011-11-09 17:46:47 +0000411 mContextLost = true;
412}
413
414bool Context::isContextLost()
415{
416 return mContextLost;
417}
418
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000419void Context::setClearColor(float red, float green, float blue, float alpha)
420{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000421 mState.colorClearValue.red = red;
422 mState.colorClearValue.green = green;
423 mState.colorClearValue.blue = blue;
424 mState.colorClearValue.alpha = alpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000425}
426
427void Context::setClearDepth(float depth)
428{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000429 mState.depthClearValue = depth;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000430}
431
432void Context::setClearStencil(int stencil)
433{
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000434 mState.stencilClearValue = stencil;
435}
436
437void Context::setCullFace(bool enabled)
438{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000439 if (mState.cullFace != enabled)
440 {
441 mState.cullFace = enabled;
442 mCullStateDirty = true;
443 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000444}
445
446bool Context::isCullFaceEnabled() const
447{
448 return mState.cullFace;
449}
450
451void Context::setCullMode(GLenum mode)
452{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000453 if (mState.cullMode != mode)
454 {
455 mState.cullMode = mode;
456 mCullStateDirty = true;
457 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000458}
459
460void Context::setFrontFace(GLenum front)
461{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000462 if (mState.frontFace != front)
463 {
464 mState.frontFace = front;
465 mFrontFaceDirty = true;
466 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000467}
468
469void Context::setDepthTest(bool enabled)
470{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000471 if (mState.depthTest != enabled)
472 {
473 mState.depthTest = enabled;
474 mDepthStateDirty = true;
475 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000476}
477
478bool Context::isDepthTestEnabled() const
479{
480 return mState.depthTest;
481}
482
483void Context::setDepthFunc(GLenum depthFunc)
484{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000485 if (mState.depthFunc != depthFunc)
486 {
487 mState.depthFunc = depthFunc;
488 mDepthStateDirty = true;
489 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000490}
491
492void Context::setDepthRange(float zNear, float zFar)
493{
494 mState.zNear = zNear;
495 mState.zFar = zFar;
496}
497
498void Context::setBlend(bool enabled)
499{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000500 if (mState.blend != enabled)
501 {
502 mState.blend = enabled;
503 mBlendStateDirty = true;
504 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000505}
506
507bool Context::isBlendEnabled() const
508{
509 return mState.blend;
510}
511
512void Context::setBlendFactors(GLenum sourceRGB, GLenum destRGB, GLenum sourceAlpha, GLenum destAlpha)
513{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000514 if (mState.sourceBlendRGB != sourceRGB ||
515 mState.sourceBlendAlpha != sourceAlpha ||
516 mState.destBlendRGB != destRGB ||
517 mState.destBlendAlpha != destAlpha)
518 {
519 mState.sourceBlendRGB = sourceRGB;
520 mState.destBlendRGB = destRGB;
521 mState.sourceBlendAlpha = sourceAlpha;
522 mState.destBlendAlpha = destAlpha;
523 mBlendStateDirty = true;
524 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000525}
526
527void Context::setBlendColor(float red, float green, float blue, float alpha)
528{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000529 if (mState.blendColor.red != red ||
530 mState.blendColor.green != green ||
531 mState.blendColor.blue != blue ||
532 mState.blendColor.alpha != alpha)
533 {
534 mState.blendColor.red = red;
535 mState.blendColor.green = green;
536 mState.blendColor.blue = blue;
537 mState.blendColor.alpha = alpha;
538 mBlendStateDirty = true;
539 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000540}
541
542void Context::setBlendEquation(GLenum rgbEquation, GLenum alphaEquation)
543{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000544 if (mState.blendEquationRGB != rgbEquation ||
545 mState.blendEquationAlpha != alphaEquation)
546 {
547 mState.blendEquationRGB = rgbEquation;
548 mState.blendEquationAlpha = alphaEquation;
549 mBlendStateDirty = true;
550 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000551}
552
553void Context::setStencilTest(bool enabled)
554{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000555 if (mState.stencilTest != enabled)
556 {
557 mState.stencilTest = enabled;
558 mStencilStateDirty = true;
559 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000560}
561
562bool Context::isStencilTestEnabled() const
563{
564 return mState.stencilTest;
565}
566
567void Context::setStencilParams(GLenum stencilFunc, GLint stencilRef, GLuint stencilMask)
568{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000569 if (mState.stencilFunc != stencilFunc ||
570 mState.stencilRef != stencilRef ||
571 mState.stencilMask != stencilMask)
572 {
573 mState.stencilFunc = stencilFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000574 mState.stencilRef = (stencilRef > 0) ? stencilRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000575 mState.stencilMask = stencilMask;
576 mStencilStateDirty = true;
577 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000578}
579
580void Context::setStencilBackParams(GLenum stencilBackFunc, GLint stencilBackRef, GLuint stencilBackMask)
581{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000582 if (mState.stencilBackFunc != stencilBackFunc ||
583 mState.stencilBackRef != stencilBackRef ||
584 mState.stencilBackMask != stencilBackMask)
585 {
586 mState.stencilBackFunc = stencilBackFunc;
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +0000587 mState.stencilBackRef = (stencilBackRef > 0) ? stencilBackRef : 0;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000588 mState.stencilBackMask = stencilBackMask;
589 mStencilStateDirty = true;
590 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000591}
592
593void Context::setStencilWritemask(GLuint stencilWritemask)
594{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000595 if (mState.stencilWritemask != stencilWritemask)
596 {
597 mState.stencilWritemask = stencilWritemask;
598 mStencilStateDirty = true;
599 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000600}
601
602void Context::setStencilBackWritemask(GLuint stencilBackWritemask)
603{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000604 if (mState.stencilBackWritemask != stencilBackWritemask)
605 {
606 mState.stencilBackWritemask = stencilBackWritemask;
607 mStencilStateDirty = true;
608 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000609}
610
611void Context::setStencilOperations(GLenum stencilFail, GLenum stencilPassDepthFail, GLenum stencilPassDepthPass)
612{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000613 if (mState.stencilFail != stencilFail ||
614 mState.stencilPassDepthFail != stencilPassDepthFail ||
615 mState.stencilPassDepthPass != stencilPassDepthPass)
616 {
617 mState.stencilFail = stencilFail;
618 mState.stencilPassDepthFail = stencilPassDepthFail;
619 mState.stencilPassDepthPass = stencilPassDepthPass;
620 mStencilStateDirty = true;
621 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000622}
623
624void Context::setStencilBackOperations(GLenum stencilBackFail, GLenum stencilBackPassDepthFail, GLenum stencilBackPassDepthPass)
625{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000626 if (mState.stencilBackFail != stencilBackFail ||
627 mState.stencilBackPassDepthFail != stencilBackPassDepthFail ||
628 mState.stencilBackPassDepthPass != stencilBackPassDepthPass)
629 {
630 mState.stencilBackFail = stencilBackFail;
631 mState.stencilBackPassDepthFail = stencilBackPassDepthFail;
632 mState.stencilBackPassDepthPass = stencilBackPassDepthPass;
633 mStencilStateDirty = true;
634 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000635}
636
637void Context::setPolygonOffsetFill(bool enabled)
638{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000639 if (mState.polygonOffsetFill != enabled)
640 {
641 mState.polygonOffsetFill = enabled;
642 mPolygonOffsetStateDirty = true;
643 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000644}
645
646bool Context::isPolygonOffsetFillEnabled() const
647{
648 return mState.polygonOffsetFill;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000649
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000650}
651
652void Context::setPolygonOffsetParams(GLfloat factor, GLfloat units)
653{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000654 if (mState.polygonOffsetFactor != factor ||
655 mState.polygonOffsetUnits != units)
656 {
657 mState.polygonOffsetFactor = factor;
658 mState.polygonOffsetUnits = units;
659 mPolygonOffsetStateDirty = true;
660 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000661}
662
663void Context::setSampleAlphaToCoverage(bool enabled)
664{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000665 if (mState.sampleAlphaToCoverage != enabled)
666 {
667 mState.sampleAlphaToCoverage = enabled;
668 mSampleStateDirty = true;
669 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000670}
671
672bool Context::isSampleAlphaToCoverageEnabled() const
673{
674 return mState.sampleAlphaToCoverage;
675}
676
677void Context::setSampleCoverage(bool enabled)
678{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000679 if (mState.sampleCoverage != enabled)
680 {
681 mState.sampleCoverage = enabled;
682 mSampleStateDirty = true;
683 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000684}
685
686bool Context::isSampleCoverageEnabled() const
687{
688 return mState.sampleCoverage;
689}
690
daniel@transgaming.coma36f98e2010-05-18 18:51:09 +0000691void Context::setSampleCoverageParams(GLclampf value, bool invert)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000692{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000693 if (mState.sampleCoverageValue != value ||
694 mState.sampleCoverageInvert != invert)
695 {
696 mState.sampleCoverageValue = value;
697 mState.sampleCoverageInvert = invert;
698 mSampleStateDirty = true;
699 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000700}
701
702void Context::setScissorTest(bool enabled)
703{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000704 if (mState.scissorTest != enabled)
705 {
706 mState.scissorTest = enabled;
707 mScissorStateDirty = true;
708 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000709}
710
711bool Context::isScissorTestEnabled() const
712{
713 return mState.scissorTest;
714}
715
716void Context::setDither(bool enabled)
717{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000718 if (mState.dither != enabled)
719 {
720 mState.dither = enabled;
721 mDitherStateDirty = true;
722 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000723}
724
725bool Context::isDitherEnabled() const
726{
727 return mState.dither;
728}
729
730void Context::setLineWidth(GLfloat width)
731{
732 mState.lineWidth = width;
733}
734
735void Context::setGenerateMipmapHint(GLenum hint)
736{
737 mState.generateMipmapHint = hint;
738}
739
alokp@chromium.orgd303ef92010-09-09 17:30:15 +0000740void Context::setFragmentShaderDerivativeHint(GLenum hint)
741{
742 mState.fragmentShaderDerivativeHint = hint;
743 // TODO: Propagate the hint to shader translator so we can write
744 // ddx, ddx_coarse, or ddx_fine depending on the hint.
745 // Ignore for now. It is valid for implementations to ignore hint.
746}
747
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000748void Context::setViewportParams(GLint x, GLint y, GLsizei width, GLsizei height)
749{
750 mState.viewportX = x;
751 mState.viewportY = y;
752 mState.viewportWidth = width;
753 mState.viewportHeight = height;
754}
755
756void Context::setScissorParams(GLint x, GLint y, GLsizei width, GLsizei height)
757{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000758 if (mState.scissorX != x || mState.scissorY != y ||
759 mState.scissorWidth != width || mState.scissorHeight != height)
760 {
761 mState.scissorX = x;
762 mState.scissorY = y;
763 mState.scissorWidth = width;
764 mState.scissorHeight = height;
765 mScissorStateDirty = true;
766 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000767}
768
769void Context::setColorMask(bool red, bool green, bool blue, bool alpha)
770{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000771 if (mState.colorMaskRed != red || mState.colorMaskGreen != green ||
772 mState.colorMaskBlue != blue || mState.colorMaskAlpha != alpha)
773 {
774 mState.colorMaskRed = red;
775 mState.colorMaskGreen = green;
776 mState.colorMaskBlue = blue;
777 mState.colorMaskAlpha = alpha;
778 mMaskStateDirty = true;
779 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000780}
781
782void Context::setDepthMask(bool mask)
783{
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +0000784 if (mState.depthMask != mask)
785 {
786 mState.depthMask = mask;
787 mMaskStateDirty = true;
788 }
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000789}
790
daniel@transgaming.comdfd57022011-05-11 15:37:25 +0000791void Context::setActiveSampler(unsigned int active)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000792{
793 mState.activeSampler = active;
794}
795
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000796GLuint Context::getReadFramebufferHandle() const
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000797{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000798 return mState.readFramebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +0000799}
800
801GLuint Context::getDrawFramebufferHandle() const
802{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000803 return mState.drawFramebuffer;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000804}
805
806GLuint Context::getRenderbufferHandle() const
807{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000808 return mState.renderbuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000809}
810
811GLuint Context::getArrayBufferHandle() const
812{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000813 return mState.arrayBuffer.id();
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000814}
815
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000816GLuint Context::getActiveQuery(GLenum target) const
817{
818 Query *queryObject = NULL;
819
820 switch (target)
821 {
822 case GL_ANY_SAMPLES_PASSED_EXT:
823 queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED].get();
824 break;
825 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
826 queryObject = mState.activeQuery[QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE].get();
827 break;
828 default:
829 ASSERT(false);
830 }
831
832 if (queryObject)
833 {
834 return queryObject->id();
835 }
836 else
837 {
838 return 0;
839 }
840}
841
daniel@transgaming.com83921382011-01-08 05:46:00 +0000842void Context::setEnableVertexAttribArray(unsigned int attribNum, bool enabled)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000843{
daniel@transgaming.com83921382011-01-08 05:46:00 +0000844 mState.vertexAttribute[attribNum].mArrayEnabled = enabled;
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000845}
846
daniel@transgaming.com83921382011-01-08 05:46:00 +0000847const VertexAttribute &Context::getVertexAttribState(unsigned int attribNum)
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000848{
849 return mState.vertexAttribute[attribNum];
850}
851
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000852void Context::setVertexAttribState(unsigned int attribNum, Buffer *boundBuffer, GLint size, GLenum type, bool normalized,
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000853 GLsizei stride, const void *pointer)
854{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +0000855 mState.vertexAttribute[attribNum].mBoundBuffer.set(boundBuffer);
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000856 mState.vertexAttribute[attribNum].mSize = size;
857 mState.vertexAttribute[attribNum].mType = type;
858 mState.vertexAttribute[attribNum].mNormalized = normalized;
859 mState.vertexAttribute[attribNum].mStride = stride;
860 mState.vertexAttribute[attribNum].mPointer = pointer;
861}
862
863const void *Context::getVertexAttribPointer(unsigned int attribNum) const
864{
865 return mState.vertexAttribute[attribNum].mPointer;
866}
867
daniel@transgaming.com83921382011-01-08 05:46:00 +0000868const VertexAttributeArray &Context::getVertexAttributes()
daniel@transgaming.com428d1582010-05-04 03:35:25 +0000869{
870 return mState.vertexAttribute;
871}
872
873void Context::setPackAlignment(GLint alignment)
874{
875 mState.packAlignment = alignment;
876}
877
878GLint Context::getPackAlignment() const
879{
880 return mState.packAlignment;
881}
882
883void Context::setUnpackAlignment(GLint alignment)
884{
885 mState.unpackAlignment = alignment;
886}
887
888GLint Context::getUnpackAlignment() const
889{
890 return mState.unpackAlignment;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000891}
892
bsalomon@google.com56d46ab2011-11-23 14:53:10 +0000893void Context::setPackReverseRowOrder(bool reverseRowOrder)
894{
895 mState.packReverseRowOrder = reverseRowOrder;
896}
897
898bool Context::getPackReverseRowOrder() const
899{
900 return mState.packReverseRowOrder;
901}
902
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903GLuint Context::createBuffer()
904{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000905 return mResourceManager->createBuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000906}
907
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000908GLuint Context::createProgram()
909{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000910 return mResourceManager->createProgram();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000911}
912
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000913GLuint Context::createShader(GLenum type)
914{
915 return mResourceManager->createShader(type);
916}
917
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000918GLuint Context::createTexture()
919{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000920 return mResourceManager->createTexture();
921}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000922
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000923GLuint Context::createRenderbuffer()
924{
925 return mResourceManager->createRenderbuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000926}
927
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000928// Returns an unused framebuffer name
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000929GLuint Context::createFramebuffer()
930{
benvanik@google.com1a233342011-04-28 19:44:39 +0000931 GLuint handle = mFramebufferHandleAllocator.allocate();
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +0000932
933 mFramebufferMap[handle] = NULL;
934
935 return handle;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000936}
937
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000938GLuint Context::createFence()
939{
benvanik@google.com1a233342011-04-28 19:44:39 +0000940 GLuint handle = mFenceHandleAllocator.allocate();
daniel@transgaming.comfe208882010-09-01 15:47:57 +0000941
942 mFenceMap[handle] = new Fence;
943
944 return handle;
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +0000945}
946
daniel@transgaming.com86bdb822012-01-20 18:24:39 +0000947// Returns an unused query name
948GLuint Context::createQuery()
949{
950 GLuint handle = mQueryHandleAllocator.allocate();
951
952 mQueryMap[handle] = NULL;
953
954 return handle;
955}
956
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000957void Context::deleteBuffer(GLuint buffer)
958{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000959 if (mResourceManager->getBuffer(buffer))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960 {
961 detachBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000962 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000963
964 mResourceManager->deleteBuffer(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000965}
966
967void Context::deleteShader(GLuint shader)
968{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000969 mResourceManager->deleteShader(shader);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970}
971
972void Context::deleteProgram(GLuint program)
973{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000974 mResourceManager->deleteProgram(program);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +0000975 mCachedCurrentProgram = NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000976}
977
978void Context::deleteTexture(GLuint texture)
979{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000980 if (mResourceManager->getTexture(texture))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000981 {
982 detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000983 }
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +0000984
985 mResourceManager->deleteTexture(texture);
986}
987
988void Context::deleteRenderbuffer(GLuint renderbuffer)
989{
990 if (mResourceManager->getRenderbuffer(renderbuffer))
991 {
992 detachRenderbuffer(renderbuffer);
993 }
994
995 mResourceManager->deleteRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000996}
997
998void Context::deleteFramebuffer(GLuint framebuffer)
999{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001000 FramebufferMap::iterator framebufferObject = mFramebufferMap.find(framebuffer);
1001
1002 if (framebufferObject != mFramebufferMap.end())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001003 {
1004 detachFramebuffer(framebuffer);
apatrick@chromium.org55255022010-09-11 02:12:47 +00001005
benvanik@google.com1a233342011-04-28 19:44:39 +00001006 mFramebufferHandleAllocator.release(framebufferObject->first);
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001007 delete framebufferObject->second;
1008 mFramebufferMap.erase(framebufferObject);
1009 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001010}
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001011
1012void Context::deleteFence(GLuint fence)
1013{
1014 FenceMap::iterator fenceObject = mFenceMap.find(fence);
1015
1016 if (fenceObject != mFenceMap.end())
1017 {
benvanik@google.com1a233342011-04-28 19:44:39 +00001018 mFenceHandleAllocator.release(fenceObject->first);
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001019 delete fenceObject->second;
1020 mFenceMap.erase(fenceObject);
1021 }
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001022}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001023
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001024void Context::deleteQuery(GLuint query)
1025{
1026 QueryMap::iterator queryObject = mQueryMap.find(query);
1027 if (queryObject != mQueryMap.end())
1028 {
1029 mQueryHandleAllocator.release(queryObject->first);
1030 if (queryObject->second)
1031 {
1032 queryObject->second->release();
1033 }
1034 mQueryMap.erase(queryObject);
1035 }
1036}
1037
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001038Buffer *Context::getBuffer(GLuint handle)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001039{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001040 return mResourceManager->getBuffer(handle);
1041}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001042
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001043Shader *Context::getShader(GLuint handle)
1044{
1045 return mResourceManager->getShader(handle);
1046}
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001047
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001048Program *Context::getProgram(GLuint handle)
1049{
1050 return mResourceManager->getProgram(handle);
1051}
1052
1053Texture *Context::getTexture(GLuint handle)
1054{
1055 return mResourceManager->getTexture(handle);
1056}
1057
1058Renderbuffer *Context::getRenderbuffer(GLuint handle)
1059{
1060 return mResourceManager->getRenderbuffer(handle);
1061}
1062
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001063Framebuffer *Context::getReadFramebuffer()
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001064{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001065 return getFramebuffer(mState.readFramebuffer);
1066}
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001067
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001068Framebuffer *Context::getDrawFramebuffer()
1069{
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001070 return mBoundDrawFramebuffer;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001071}
1072
1073void Context::bindArrayBuffer(unsigned int buffer)
1074{
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001075 mResourceManager->checkBufferAllocation(buffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001076
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001077 mState.arrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001078}
1079
1080void Context::bindElementArrayBuffer(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.elementArrayBuffer.set(getBuffer(buffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001085}
1086
1087void Context::bindTexture2D(GLuint texture)
1088{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001089 mResourceManager->checkTextureAllocation(texture, TEXTURE_2D);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001090
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001091 mState.samplerTexture[TEXTURE_2D][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001092}
1093
1094void Context::bindTextureCubeMap(GLuint texture)
1095{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001096 mResourceManager->checkTextureAllocation(texture, TEXTURE_CUBE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001097
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001098 mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].set(getTexture(texture));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001099}
1100
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001101void Context::bindReadFramebuffer(GLuint framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001102{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001103 if (!getFramebuffer(framebuffer))
1104 {
1105 mFramebufferMap[framebuffer] = new Framebuffer();
1106 }
1107
1108 mState.readFramebuffer = framebuffer;
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001109}
1110
1111void Context::bindDrawFramebuffer(GLuint framebuffer)
1112{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001113 if (!getFramebuffer(framebuffer))
1114 {
1115 mFramebufferMap[framebuffer] = new Framebuffer();
1116 }
1117
1118 mState.drawFramebuffer = framebuffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001119
1120 mBoundDrawFramebuffer = getFramebuffer(framebuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001121}
1122
1123void Context::bindRenderbuffer(GLuint renderbuffer)
1124{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001125 mResourceManager->checkRenderbufferAllocation(renderbuffer);
1126
1127 mState.renderbuffer.set(getRenderbuffer(renderbuffer));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001128}
1129
1130void Context::useProgram(GLuint program)
1131{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001132 GLuint priorProgram = mState.currentProgram;
1133 mState.currentProgram = program; // Must switch before trying to delete, otherwise it only gets flagged.
daniel@transgaming.com71cd8682010-04-29 03:35:25 +00001134
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001135 if (priorProgram != program)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001136 {
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001137 Program *newProgram = mResourceManager->getProgram(program);
1138 Program *oldProgram = mResourceManager->getProgram(priorProgram);
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001139 mCachedCurrentProgram = NULL;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001140 mDxUniformsDirty = true;
daniel@transgaming.comda13f3e2010-07-28 19:20:56 +00001141
1142 if (newProgram)
1143 {
1144 newProgram->addRef();
1145 }
1146
1147 if (oldProgram)
1148 {
1149 oldProgram->release();
1150 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001151 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001152}
1153
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001154void Context::beginQuery(GLenum target, GLuint query)
1155{
1156 // From EXT_occlusion_query_boolean: If BeginQueryEXT is called with an <id>
1157 // of zero, if the active query object name for <target> is non-zero (for the
1158 // targets ANY_SAMPLES_PASSED_EXT and ANY_SAMPLES_PASSED_CONSERVATIVE_EXT, if
1159 // the active query for either target is non-zero), if <id> is the name of an
1160 // existing query object whose type does not match <target>, or if <id> is the
1161 // active query object name for any query type, the error INVALID_OPERATION is
1162 // generated.
1163
1164 // Ensure no other queries are active
1165 // NOTE: If other queries than occlusion are supported, we will need to check
1166 // separately that:
1167 // a) The query ID passed is not the current active query for any target/type
1168 // b) There are no active queries for the requested target (and in the case
1169 // of GL_ANY_SAMPLES_PASSED_EXT and GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT,
1170 // no query may be active for either if glBeginQuery targets either.
1171 for (int i = 0; i < QUERY_TYPE_COUNT; i++)
1172 {
1173 if (mState.activeQuery[i].get() != NULL)
1174 {
1175 return error(GL_INVALID_OPERATION);
1176 }
1177 }
1178
1179 QueryType qType;
1180 switch (target)
1181 {
1182 case GL_ANY_SAMPLES_PASSED_EXT:
1183 qType = QUERY_ANY_SAMPLES_PASSED;
1184 break;
1185 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
1186 qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
1187 break;
1188 default:
1189 ASSERT(false);
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00001190 return;
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001191 }
1192
1193 Query *queryObject = getQuery(query, true, target);
1194
1195 // check that name was obtained with glGenQueries
1196 if (!queryObject)
1197 {
1198 return error(GL_INVALID_OPERATION);
1199 }
1200
1201 // check for type mismatch
1202 if (queryObject->getType() != target)
1203 {
1204 return error(GL_INVALID_OPERATION);
1205 }
1206
1207 // set query as active for specified target
1208 mState.activeQuery[qType].set(queryObject);
1209
1210 // begin query
1211 queryObject->begin();
1212}
1213
1214void Context::endQuery(GLenum target)
1215{
1216 QueryType qType;
1217
1218 switch (target)
1219 {
1220 case GL_ANY_SAMPLES_PASSED_EXT:
1221 qType = QUERY_ANY_SAMPLES_PASSED;
1222 break;
1223 case GL_ANY_SAMPLES_PASSED_CONSERVATIVE_EXT:
1224 qType = QUERY_ANY_SAMPLES_PASSED_CONSERVATIVE;
1225 break;
1226 default:
1227 ASSERT(false);
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00001228 return;
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001229 }
1230
1231 Query *queryObject = mState.activeQuery[qType].get();
1232
1233 if (queryObject == NULL)
1234 {
1235 return error(GL_INVALID_OPERATION);
1236 }
1237
1238 queryObject->end();
1239
1240 mState.activeQuery[qType].set(NULL);
1241}
1242
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001243void Context::setFramebufferZero(Framebuffer *buffer)
1244{
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001245 delete mFramebufferMap[0];
1246 mFramebufferMap[0] = buffer;
jbauman@chromium.org040c4db2011-10-13 21:35:52 +00001247 if (mState.drawFramebuffer == 0)
1248 {
1249 mBoundDrawFramebuffer = buffer;
1250 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001251}
1252
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001253void Context::setRenderbufferStorage(RenderbufferStorage *renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001254{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001255 Renderbuffer *renderbufferObject = mState.renderbuffer.get();
1256 renderbufferObject->setStorage(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001257}
1258
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00001259Framebuffer *Context::getFramebuffer(unsigned int handle)
1260{
1261 FramebufferMap::iterator framebuffer = mFramebufferMap.find(handle);
1262
1263 if (framebuffer == mFramebufferMap.end())
1264 {
1265 return NULL;
1266 }
1267 else
1268 {
1269 return framebuffer->second;
1270 }
1271}
1272
daniel@transgaming.comfe208882010-09-01 15:47:57 +00001273Fence *Context::getFence(unsigned int handle)
1274{
1275 FenceMap::iterator fence = mFenceMap.find(handle);
1276
1277 if (fence == mFenceMap.end())
1278 {
1279 return NULL;
1280 }
1281 else
1282 {
1283 return fence->second;
1284 }
1285}
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00001286
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00001287Query *Context::getQuery(unsigned int handle, bool create, GLenum type)
1288{
1289 QueryMap::iterator query = mQueryMap.find(handle);
1290
1291 if (query == mQueryMap.end())
1292 {
1293 return NULL;
1294 }
1295 else
1296 {
1297 if (!query->second && create)
1298 {
1299 query->second = new Query(handle, type);
1300 query->second->addRef();
1301 }
1302 return query->second;
1303 }
1304}
1305
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001306Buffer *Context::getArrayBuffer()
1307{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001308 return mState.arrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001309}
1310
1311Buffer *Context::getElementArrayBuffer()
1312{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001313 return mState.elementArrayBuffer.get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001314}
1315
1316Program *Context::getCurrentProgram()
1317{
jbauman@chromium.orgc6209852011-10-07 15:19:26 +00001318 if (!mCachedCurrentProgram)
1319 {
1320 mCachedCurrentProgram = mResourceManager->getProgram(mState.currentProgram);
1321 }
1322 return mCachedCurrentProgram;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001323}
1324
1325Texture2D *Context::getTexture2D()
1326{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001327 return static_cast<Texture2D*>(getSamplerTexture(mState.activeSampler, TEXTURE_2D));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001328}
1329
1330TextureCubeMap *Context::getTextureCubeMap()
1331{
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001332 return static_cast<TextureCubeMap*>(getSamplerTexture(mState.activeSampler, TEXTURE_CUBE));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001333}
1334
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001335Texture *Context::getSamplerTexture(unsigned int sampler, TextureType type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001336{
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001337 GLuint texid = mState.samplerTexture[type][sampler].id();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001338
daniel@transgaming.coma5a8a0a2010-11-19 14:55:32 +00001339 if (texid == 0) // Special case: 0 refers to different initial textures based on the target
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001340 {
1341 switch (type)
1342 {
1343 default: UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001344 case TEXTURE_2D: return mTexture2DZero.get();
1345 case TEXTURE_CUBE: return mTextureCubeMapZero.get();
daniel@transgaming.com4195fc42010-04-08 03:51:09 +00001346 }
1347 }
1348
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001349 return mState.samplerTexture[type][sampler].get();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001350}
1351
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001352bool Context::getBooleanv(GLenum pname, GLboolean *params)
1353{
1354 switch (pname)
1355 {
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001356 case GL_SHADER_COMPILER: *params = GL_TRUE; break;
1357 case GL_SAMPLE_COVERAGE_INVERT: *params = mState.sampleCoverageInvert; break;
1358 case GL_DEPTH_WRITEMASK: *params = mState.depthMask; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001359 case GL_COLOR_WRITEMASK:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001360 params[0] = mState.colorMaskRed;
1361 params[1] = mState.colorMaskGreen;
1362 params[2] = mState.colorMaskBlue;
1363 params[3] = mState.colorMaskAlpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001364 break;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001365 case GL_CULL_FACE: *params = mState.cullFace; break;
1366 case GL_POLYGON_OFFSET_FILL: *params = mState.polygonOffsetFill; break;
1367 case GL_SAMPLE_ALPHA_TO_COVERAGE: *params = mState.sampleAlphaToCoverage; break;
1368 case GL_SAMPLE_COVERAGE: *params = mState.sampleCoverage; break;
1369 case GL_SCISSOR_TEST: *params = mState.scissorTest; break;
1370 case GL_STENCIL_TEST: *params = mState.stencilTest; break;
1371 case GL_DEPTH_TEST: *params = mState.depthTest; break;
1372 case GL_BLEND: *params = mState.blend; break;
1373 case GL_DITHER: *params = mState.dither; break;
1374 case GL_CONTEXT_ROBUST_ACCESS_EXT: *params = mRobustAccess ? GL_TRUE : GL_FALSE; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001375 default:
1376 return false;
1377 }
1378
1379 return true;
1380}
1381
1382bool Context::getFloatv(GLenum pname, GLfloat *params)
1383{
1384 // Please note: DEPTH_CLEAR_VALUE is included in our internal getFloatv implementation
1385 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1386 // GetIntegerv as its native query function. As it would require conversion in any
1387 // case, this should make no difference to the calling application.
1388 switch (pname)
1389 {
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001390 case GL_LINE_WIDTH: *params = mState.lineWidth; break;
1391 case GL_SAMPLE_COVERAGE_VALUE: *params = mState.sampleCoverageValue; break;
1392 case GL_DEPTH_CLEAR_VALUE: *params = mState.depthClearValue; break;
1393 case GL_POLYGON_OFFSET_FACTOR: *params = mState.polygonOffsetFactor; break;
1394 case GL_POLYGON_OFFSET_UNITS: *params = mState.polygonOffsetUnits; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001395 case GL_ALIASED_LINE_WIDTH_RANGE:
1396 params[0] = gl::ALIASED_LINE_WIDTH_RANGE_MIN;
1397 params[1] = gl::ALIASED_LINE_WIDTH_RANGE_MAX;
1398 break;
1399 case GL_ALIASED_POINT_SIZE_RANGE:
1400 params[0] = gl::ALIASED_POINT_SIZE_RANGE_MIN;
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00001401 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 +00001402 break;
1403 case GL_DEPTH_RANGE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001404 params[0] = mState.zNear;
1405 params[1] = mState.zFar;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001406 break;
1407 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001408 params[0] = mState.colorClearValue.red;
1409 params[1] = mState.colorClearValue.green;
1410 params[2] = mState.colorClearValue.blue;
1411 params[3] = mState.colorClearValue.alpha;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001412 break;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001413 case GL_BLEND_COLOR:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001414 params[0] = mState.blendColor.red;
1415 params[1] = mState.blendColor.green;
1416 params[2] = mState.blendColor.blue;
1417 params[3] = mState.blendColor.alpha;
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001418 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001419 default:
1420 return false;
1421 }
1422
1423 return true;
1424}
1425
1426bool Context::getIntegerv(GLenum pname, GLint *params)
1427{
1428 // Please note: DEPTH_CLEAR_VALUE is not included in our internal getIntegerv implementation
1429 // because it is stored as a float, despite the fact that the GL ES 2.0 spec names
1430 // GetIntegerv as its native query function. As it would require conversion in any
1431 // case, this should make no difference to the calling application. You may find it in
1432 // Context::getFloatv.
1433 switch (pname)
1434 {
1435 case GL_MAX_VERTEX_ATTRIBS: *params = gl::MAX_VERTEX_ATTRIBS; break;
1436 case GL_MAX_VERTEX_UNIFORM_VECTORS: *params = gl::MAX_VERTEX_UNIFORM_VECTORS; break;
daniel@transgaming.com396c6432010-11-26 16:26:12 +00001437 case GL_MAX_VARYING_VECTORS: *params = getMaximumVaryingVectors(); break;
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00001438 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS: *params = getMaximumCombinedTextureImageUnits(); break;
1439 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS: *params = getMaximumVertexTextureImageUnits(); break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001440 case GL_MAX_TEXTURE_IMAGE_UNITS: *params = gl::MAX_TEXTURE_IMAGE_UNITS; break;
daniel@transgaming.com458da142010-11-28 02:03:02 +00001441 case GL_MAX_FRAGMENT_UNIFORM_VECTORS: *params = getMaximumFragmentUniformVectors(); break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001442 case GL_MAX_RENDERBUFFER_SIZE: *params = getMaximumRenderbufferDimension(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001443 case GL_NUM_SHADER_BINARY_FORMATS: *params = 0; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001444 case GL_SHADER_BINARY_FORMATS: /* no shader binary formats are supported */ break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001445 case GL_ARRAY_BUFFER_BINDING: *params = mState.arrayBuffer.id(); break;
1446 case GL_ELEMENT_ARRAY_BUFFER_BINDING: *params = mState.elementArrayBuffer.id(); break;
daniel@transgaming.com9d7fc1d2010-10-27 15:49:42 +00001447 //case GL_FRAMEBUFFER_BINDING: // now equivalent to GL_DRAW_FRAMEBUFFER_BINDING_ANGLE
1448 case GL_DRAW_FRAMEBUFFER_BINDING_ANGLE: *params = mState.drawFramebuffer; break;
1449 case GL_READ_FRAMEBUFFER_BINDING_ANGLE: *params = mState.readFramebuffer; break;
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00001450 case GL_RENDERBUFFER_BINDING: *params = mState.renderbuffer.id(); break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001451 case GL_CURRENT_PROGRAM: *params = mState.currentProgram; break;
1452 case GL_PACK_ALIGNMENT: *params = mState.packAlignment; break;
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00001453 case GL_PACK_REVERSE_ROW_ORDER_ANGLE: *params = mState.packReverseRowOrder; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001454 case GL_UNPACK_ALIGNMENT: *params = mState.unpackAlignment; break;
1455 case GL_GENERATE_MIPMAP_HINT: *params = mState.generateMipmapHint; break;
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001456 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES: *params = mState.fragmentShaderDerivativeHint; break;
daniel@transgaming.comb28a23b2010-05-20 19:18:06 +00001457 case GL_ACTIVE_TEXTURE: *params = (mState.activeSampler + GL_TEXTURE0); break;
1458 case GL_STENCIL_FUNC: *params = mState.stencilFunc; break;
1459 case GL_STENCIL_REF: *params = mState.stencilRef; break;
1460 case GL_STENCIL_VALUE_MASK: *params = mState.stencilMask; break;
1461 case GL_STENCIL_BACK_FUNC: *params = mState.stencilBackFunc; break;
1462 case GL_STENCIL_BACK_REF: *params = mState.stencilBackRef; break;
1463 case GL_STENCIL_BACK_VALUE_MASK: *params = mState.stencilBackMask; break;
1464 case GL_STENCIL_FAIL: *params = mState.stencilFail; break;
1465 case GL_STENCIL_PASS_DEPTH_FAIL: *params = mState.stencilPassDepthFail; break;
1466 case GL_STENCIL_PASS_DEPTH_PASS: *params = mState.stencilPassDepthPass; break;
1467 case GL_STENCIL_BACK_FAIL: *params = mState.stencilBackFail; break;
1468 case GL_STENCIL_BACK_PASS_DEPTH_FAIL: *params = mState.stencilBackPassDepthFail; break;
1469 case GL_STENCIL_BACK_PASS_DEPTH_PASS: *params = mState.stencilBackPassDepthPass; break;
1470 case GL_DEPTH_FUNC: *params = mState.depthFunc; break;
1471 case GL_BLEND_SRC_RGB: *params = mState.sourceBlendRGB; break;
1472 case GL_BLEND_SRC_ALPHA: *params = mState.sourceBlendAlpha; break;
1473 case GL_BLEND_DST_RGB: *params = mState.destBlendRGB; break;
1474 case GL_BLEND_DST_ALPHA: *params = mState.destBlendAlpha; break;
1475 case GL_BLEND_EQUATION_RGB: *params = mState.blendEquationRGB; break;
1476 case GL_BLEND_EQUATION_ALPHA: *params = mState.blendEquationAlpha; break;
1477 case GL_STENCIL_WRITEMASK: *params = mState.stencilWritemask; break;
1478 case GL_STENCIL_BACK_WRITEMASK: *params = mState.stencilBackWritemask; break;
1479 case GL_STENCIL_CLEAR_VALUE: *params = mState.stencilClearValue; break;
1480 case GL_SUBPIXEL_BITS: *params = 4; break;
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001481 case GL_MAX_TEXTURE_SIZE: *params = getMaximumTextureDimension(); break;
1482 case GL_MAX_CUBE_MAP_TEXTURE_SIZE: *params = getMaximumCubeTextureDimension(); break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001483 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
gman@chromium.org50c526d2011-08-10 05:19:44 +00001484 params[0] = mNumCompressedTextureFormats;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001485 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001486 case GL_MAX_SAMPLES_ANGLE:
1487 {
1488 GLsizei maxSamples = getMaxSupportedSamples();
1489 if (maxSamples != 0)
1490 {
1491 *params = maxSamples;
1492 }
1493 else
1494 {
1495 return false;
1496 }
1497
1498 break;
1499 }
1500 case GL_SAMPLE_BUFFERS:
1501 case GL_SAMPLES:
1502 {
1503 gl::Framebuffer *framebuffer = getDrawFramebuffer();
1504 if (framebuffer->completeness() == GL_FRAMEBUFFER_COMPLETE)
1505 {
1506 switch (pname)
1507 {
1508 case GL_SAMPLE_BUFFERS:
1509 if (framebuffer->getSamples() != 0)
1510 {
1511 *params = 1;
1512 }
1513 else
1514 {
1515 *params = 0;
1516 }
1517 break;
1518 case GL_SAMPLES:
1519 *params = framebuffer->getSamples();
1520 break;
1521 }
1522 }
1523 else
1524 {
1525 *params = 0;
1526 }
1527 }
1528 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001529 case GL_IMPLEMENTATION_COLOR_READ_TYPE: *params = gl::IMPLEMENTATION_COLOR_READ_TYPE; break;
1530 case GL_IMPLEMENTATION_COLOR_READ_FORMAT: *params = gl::IMPLEMENTATION_COLOR_READ_FORMAT; break;
1531 case GL_MAX_VIEWPORT_DIMS:
1532 {
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00001533 int maxDimension = std::max(getMaximumRenderbufferDimension(), getMaximumTextureDimension());
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001534 params[0] = maxDimension;
1535 params[1] = maxDimension;
1536 }
1537 break;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001538 case GL_COMPRESSED_TEXTURE_FORMATS:
1539 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001540 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00001541 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001542 *params++ = GL_COMPRESSED_RGB_S3TC_DXT1_EXT;
1543 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
1544 }
1545 if (supportsDXT3Textures())
1546 {
1547 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT3_ANGLE;
1548 }
1549 if (supportsDXT5Textures())
1550 {
1551 *params++ = GL_COMPRESSED_RGBA_S3TC_DXT5_ANGLE;
daniel@transgaming.com01868132010-08-24 19:21:17 +00001552 }
1553 }
1554 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001555 case GL_VIEWPORT:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001556 params[0] = mState.viewportX;
1557 params[1] = mState.viewportY;
1558 params[2] = mState.viewportWidth;
1559 params[3] = mState.viewportHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001560 break;
1561 case GL_SCISSOR_BOX:
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001562 params[0] = mState.scissorX;
1563 params[1] = mState.scissorY;
1564 params[2] = mState.scissorWidth;
1565 params[3] = mState.scissorHeight;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001566 break;
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001567 case GL_CULL_FACE_MODE: *params = mState.cullMode; break;
1568 case GL_FRONT_FACE: *params = mState.frontFace; break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001569 case GL_RED_BITS:
1570 case GL_GREEN_BITS:
1571 case GL_BLUE_BITS:
1572 case GL_ALPHA_BITS:
1573 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001574 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001575 gl::Renderbuffer *colorbuffer = framebuffer->getColorbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001576
1577 if (colorbuffer)
1578 {
1579 switch (pname)
1580 {
1581 case GL_RED_BITS: *params = colorbuffer->getRedSize(); break;
1582 case GL_GREEN_BITS: *params = colorbuffer->getGreenSize(); break;
1583 case GL_BLUE_BITS: *params = colorbuffer->getBlueSize(); break;
1584 case GL_ALPHA_BITS: *params = colorbuffer->getAlphaSize(); break;
1585 }
1586 }
1587 else
1588 {
1589 *params = 0;
1590 }
1591 }
1592 break;
1593 case GL_DEPTH_BITS:
1594 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001595 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001596 gl::Renderbuffer *depthbuffer = framebuffer->getDepthbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001597
1598 if (depthbuffer)
1599 {
1600 *params = depthbuffer->getDepthSize();
1601 }
1602 else
1603 {
1604 *params = 0;
1605 }
1606 }
1607 break;
1608 case GL_STENCIL_BITS:
1609 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001610 gl::Framebuffer *framebuffer = getDrawFramebuffer();
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00001611 gl::Renderbuffer *stencilbuffer = framebuffer->getStencilbuffer();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001612
1613 if (stencilbuffer)
1614 {
1615 *params = stencilbuffer->getStencilSize();
1616 }
1617 else
1618 {
1619 *params = 0;
1620 }
1621 }
1622 break;
1623 case GL_TEXTURE_BINDING_2D:
1624 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001625 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001626 {
1627 error(GL_INVALID_OPERATION);
1628 return false;
1629 }
1630
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001631 *params = mState.samplerTexture[TEXTURE_2D][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001632 }
1633 break;
1634 case GL_TEXTURE_BINDING_CUBE_MAP:
1635 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00001636 if (mState.activeSampler < 0 || mState.activeSampler > getMaximumCombinedTextureImageUnits() - 1)
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001637 {
1638 error(GL_INVALID_OPERATION);
1639 return false;
1640 }
1641
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00001642 *params = mState.samplerTexture[TEXTURE_CUBE][mState.activeSampler].id();
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001643 }
1644 break;
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001645 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
1646 *params = mResetStrategy;
1647 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001648 default:
1649 return false;
1650 }
1651
1652 return true;
1653}
1654
1655bool Context::getQueryParameterInfo(GLenum pname, GLenum *type, unsigned int *numParams)
1656{
1657 // Please note: the query type returned for DEPTH_CLEAR_VALUE in this implementation
1658 // is FLOAT rather than INT, as would be suggested by the GL ES 2.0 spec. This is due
1659 // to the fact that it is stored internally as a float, and so would require conversion
1660 // if returned from Context::getIntegerv. Since this conversion is already implemented
1661 // in the case that one calls glGetIntegerv to retrieve a float-typed state variable, we
1662 // place DEPTH_CLEAR_VALUE with the floats. This should make no difference to the calling
1663 // application.
1664 switch (pname)
1665 {
gman@chromium.org50c526d2011-08-10 05:19:44 +00001666 case GL_COMPRESSED_TEXTURE_FORMATS:
1667 {
1668 *type = GL_INT;
1669 *numParams = mNumCompressedTextureFormats;
1670 }
1671 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001672 case GL_SHADER_BINARY_FORMATS:
1673 {
1674 *type = GL_INT;
1675 *numParams = 0;
1676 }
1677 break;
1678 case GL_MAX_VERTEX_ATTRIBS:
1679 case GL_MAX_VERTEX_UNIFORM_VECTORS:
1680 case GL_MAX_VARYING_VECTORS:
1681 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
1682 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
1683 case GL_MAX_TEXTURE_IMAGE_UNITS:
1684 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
1685 case GL_MAX_RENDERBUFFER_SIZE:
1686 case GL_NUM_SHADER_BINARY_FORMATS:
1687 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
1688 case GL_ARRAY_BUFFER_BINDING:
1689 case GL_FRAMEBUFFER_BINDING:
1690 case GL_RENDERBUFFER_BINDING:
1691 case GL_CURRENT_PROGRAM:
1692 case GL_PACK_ALIGNMENT:
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00001693 case GL_PACK_REVERSE_ROW_ORDER_ANGLE:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001694 case GL_UNPACK_ALIGNMENT:
1695 case GL_GENERATE_MIPMAP_HINT:
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00001696 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001697 case GL_RED_BITS:
1698 case GL_GREEN_BITS:
1699 case GL_BLUE_BITS:
1700 case GL_ALPHA_BITS:
1701 case GL_DEPTH_BITS:
1702 case GL_STENCIL_BITS:
1703 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
1704 case GL_CULL_FACE_MODE:
1705 case GL_FRONT_FACE:
1706 case GL_ACTIVE_TEXTURE:
1707 case GL_STENCIL_FUNC:
1708 case GL_STENCIL_VALUE_MASK:
1709 case GL_STENCIL_REF:
1710 case GL_STENCIL_FAIL:
1711 case GL_STENCIL_PASS_DEPTH_FAIL:
1712 case GL_STENCIL_PASS_DEPTH_PASS:
1713 case GL_STENCIL_BACK_FUNC:
1714 case GL_STENCIL_BACK_VALUE_MASK:
1715 case GL_STENCIL_BACK_REF:
1716 case GL_STENCIL_BACK_FAIL:
1717 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
1718 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
1719 case GL_DEPTH_FUNC:
1720 case GL_BLEND_SRC_RGB:
1721 case GL_BLEND_SRC_ALPHA:
1722 case GL_BLEND_DST_RGB:
1723 case GL_BLEND_DST_ALPHA:
1724 case GL_BLEND_EQUATION_RGB:
1725 case GL_BLEND_EQUATION_ALPHA:
1726 case GL_STENCIL_WRITEMASK:
1727 case GL_STENCIL_BACK_WRITEMASK:
1728 case GL_STENCIL_CLEAR_VALUE:
1729 case GL_SUBPIXEL_BITS:
1730 case GL_MAX_TEXTURE_SIZE:
1731 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
1732 case GL_SAMPLE_BUFFERS:
1733 case GL_SAMPLES:
1734 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
1735 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
1736 case GL_TEXTURE_BINDING_2D:
1737 case GL_TEXTURE_BINDING_CUBE_MAP:
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001738 case GL_RESET_NOTIFICATION_STRATEGY_EXT:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001739 {
1740 *type = GL_INT;
1741 *numParams = 1;
1742 }
1743 break;
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00001744 case GL_MAX_SAMPLES_ANGLE:
1745 {
1746 if (getMaxSupportedSamples() != 0)
1747 {
1748 *type = GL_INT;
1749 *numParams = 1;
1750 }
1751 else
1752 {
1753 return false;
1754 }
1755 }
1756 break;
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001757 case GL_MAX_VIEWPORT_DIMS:
1758 {
1759 *type = GL_INT;
1760 *numParams = 2;
1761 }
1762 break;
1763 case GL_VIEWPORT:
1764 case GL_SCISSOR_BOX:
1765 {
1766 *type = GL_INT;
1767 *numParams = 4;
1768 }
1769 break;
1770 case GL_SHADER_COMPILER:
1771 case GL_SAMPLE_COVERAGE_INVERT:
1772 case GL_DEPTH_WRITEMASK:
daniel@transgaming.com79f66772010-04-13 03:26:09 +00001773 case GL_CULL_FACE: // CULL_FACE through DITHER are natural to IsEnabled,
1774 case GL_POLYGON_OFFSET_FILL: // but can be retrieved through the Get{Type}v queries.
1775 case GL_SAMPLE_ALPHA_TO_COVERAGE: // For this purpose, they are treated here as bool-natural
1776 case GL_SAMPLE_COVERAGE:
1777 case GL_SCISSOR_TEST:
1778 case GL_STENCIL_TEST:
1779 case GL_DEPTH_TEST:
1780 case GL_BLEND:
1781 case GL_DITHER:
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00001782 case GL_CONTEXT_ROBUST_ACCESS_EXT:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001783 {
1784 *type = GL_BOOL;
1785 *numParams = 1;
1786 }
1787 break;
1788 case GL_COLOR_WRITEMASK:
1789 {
1790 *type = GL_BOOL;
1791 *numParams = 4;
1792 }
1793 break;
1794 case GL_POLYGON_OFFSET_FACTOR:
1795 case GL_POLYGON_OFFSET_UNITS:
1796 case GL_SAMPLE_COVERAGE_VALUE:
1797 case GL_DEPTH_CLEAR_VALUE:
1798 case GL_LINE_WIDTH:
1799 {
1800 *type = GL_FLOAT;
1801 *numParams = 1;
1802 }
1803 break;
1804 case GL_ALIASED_LINE_WIDTH_RANGE:
1805 case GL_ALIASED_POINT_SIZE_RANGE:
1806 case GL_DEPTH_RANGE:
1807 {
1808 *type = GL_FLOAT;
1809 *numParams = 2;
1810 }
1811 break;
1812 case GL_COLOR_CLEAR_VALUE:
daniel@transgaming.comc1641352010-04-26 15:33:36 +00001813 case GL_BLEND_COLOR:
daniel@transgaming.com777f2672010-04-07 03:25:16 +00001814 {
1815 *type = GL_FLOAT;
1816 *numParams = 4;
1817 }
1818 break;
1819 default:
1820 return false;
1821 }
1822
1823 return true;
1824}
1825
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001826// Applies the render target surface, depth stencil surface, viewport rectangle and
1827// scissor rectangle to the Direct3D 9 device
1828bool Context::applyRenderTarget(bool ignoreViewport)
1829{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00001830 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001831
1832 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
1833 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00001834 return error(GL_INVALID_FRAMEBUFFER_OPERATION, false);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001835 }
1836
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001837 bool renderTargetChanged = false;
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001838 unsigned int renderTargetSerial = framebufferObject->getRenderTargetSerial();
1839 if (renderTargetSerial != mAppliedRenderTargetSerial)
1840 {
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001841 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001842 if (!renderTarget)
1843 {
1844 return false; // Context must be lost
1845 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001846 mDevice->SetRenderTarget(0, renderTarget);
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001847 mAppliedRenderTargetSerial = renderTargetSerial;
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001848 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 +00001849 renderTargetChanged = true;
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001850 renderTarget->Release();
daniel@transgaming.com092bd482010-05-12 03:39:36 +00001851 }
1852
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001853 IDirect3DSurface9 *depthStencil = NULL;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001854 unsigned int depthbufferSerial = 0;
1855 unsigned int stencilbufferSerial = 0;
1856 if (framebufferObject->getDepthbufferType() != GL_NONE)
1857 {
1858 depthStencil = framebufferObject->getDepthbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001859 if (!depthStencil)
1860 {
1861 ERR("Depth stencil pointer unexpectedly null.");
1862 return false;
1863 }
1864
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001865 depthbufferSerial = framebufferObject->getDepthbuffer()->getSerial();
1866 }
1867 else if (framebufferObject->getStencilbufferType() != GL_NONE)
1868 {
1869 depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00001870 if (!depthStencil)
1871 {
1872 ERR("Depth stencil pointer unexpectedly null.");
1873 return false;
1874 }
1875
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001876 stencilbufferSerial = framebufferObject->getStencilbuffer()->getSerial();
1877 }
1878
1879 if (depthbufferSerial != mAppliedDepthbufferSerial ||
apatrick@chromium.org85dc42b2010-09-14 03:10:08 +00001880 stencilbufferSerial != mAppliedStencilbufferSerial ||
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001881 !mDepthStencilInitialized)
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001882 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001883 mDevice->SetDepthStencilSurface(depthStencil);
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001884 mAppliedDepthbufferSerial = depthbufferSerial;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00001885 mAppliedStencilbufferSerial = stencilbufferSerial;
vangelis@chromium.orgcf66ebb2010-09-14 22:15:43 +00001886 mDepthStencilInitialized = true;
daniel@transgaming.com339ae702010-05-12 03:40:20 +00001887 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001888
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001889 if (!mRenderTargetDescInitialized || renderTargetChanged)
1890 {
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001891 IDirect3DSurface9 *renderTarget = framebufferObject->getRenderTarget();
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001892 if (!renderTarget)
1893 {
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001894 return false; // Context must be lost
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00001895 }
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001896 renderTarget->GetDesc(&mRenderTargetDesc);
1897 mRenderTargetDescInitialized = true;
daniel@transgaming.coma5798952011-12-13 17:30:43 +00001898 renderTarget->Release();
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001899 }
1900
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001901 D3DVIEWPORT9 viewport;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001902
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001903 float zNear = clamp01(mState.zNear);
1904 float zFar = clamp01(mState.zFar);
1905
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001906 if (ignoreViewport)
1907 {
1908 viewport.X = 0;
1909 viewport.Y = 0;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001910 viewport.Width = mRenderTargetDesc.Width;
1911 viewport.Height = mRenderTargetDesc.Height;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001912 viewport.MinZ = 0.0f;
1913 viewport.MaxZ = 1.0f;
1914 }
1915 else
1916 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001917 RECT rect = transformPixelRect(mState.viewportX, mState.viewportY, mState.viewportWidth, mState.viewportHeight, mRenderTargetDesc.Height);
1918 viewport.X = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1919 viewport.Y = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1920 viewport.Width = clamp(rect.right - rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width) - static_cast<LONG>(viewport.X));
1921 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 +00001922 viewport.MinZ = zNear;
1923 viewport.MaxZ = zFar;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001924 }
1925
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00001926 if (viewport.Width <= 0 || viewport.Height <= 0)
1927 {
1928 return false; // Nothing to render
1929 }
1930
jbauman@chromium.org241e70d2011-11-03 23:07:05 +00001931 if (renderTargetChanged || !mViewportInitialized || memcmp(&viewport, &mSetViewport, sizeof mSetViewport) != 0)
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001932 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001933 mDevice->SetViewport(&viewport);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001934 mSetViewport = viewport;
1935 mViewportInitialized = true;
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001936 mDxUniformsDirty = true;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001937 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001938
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001939 if (mScissorStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001940 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001941 if (mState.scissorTest)
1942 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00001943 RECT rect = transformPixelRect(mState.scissorX, mState.scissorY, mState.scissorWidth, mState.scissorHeight, mRenderTargetDesc.Height);
1944 rect.left = clamp(rect.left, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1945 rect.top = clamp(rect.top, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
1946 rect.right = clamp(rect.right, 0L, static_cast<LONG>(mRenderTargetDesc.Width));
1947 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(mRenderTargetDesc.Height));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001948 mDevice->SetScissorRect(&rect);
1949 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001950 }
1951 else
1952 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00001953 mDevice->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001954 }
daniel@transgaming.combc3699d2010-08-05 14:48:49 +00001955
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00001956 mScissorStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001957 }
1958
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001959 if (mState.currentProgram && mDxUniformsDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001960 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001961 Program *programObject = getCurrentProgram();
1962
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001963 GLint halfPixelSize = programObject->getDxHalfPixelSizeLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001964 GLfloat xy[2] = {1.0f / viewport.Width, -1.0f / viewport.Height};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001965 programObject->setUniform2fv(halfPixelSize, 1, xy);
daniel@transgaming.com86487c22010-03-11 19:41:43 +00001966
daniel@transgaming.comd9a54f92011-12-22 18:32:53 +00001967 // These values are used for computing gl_FragCoord in Program::linkVaryings(). The approach depends on Shader Model 3.0 support.
1968 GLint coord = programObject->getDxCoordLocation();
1969 float h = mSupportsShaderModel3 ? mRenderTargetDesc.Height : mState.viewportHeight / 2.0f;
1970 GLfloat whxy[4] = {mState.viewportWidth / 2.0f, h,
daniel@transgaming.com428d1582010-05-04 03:35:25 +00001971 (float)mState.viewportX + mState.viewportWidth / 2.0f,
1972 (float)mState.viewportY + mState.viewportHeight / 2.0f};
daniel@transgaming.comd9a54f92011-12-22 18:32:53 +00001973 programObject->setUniform4fv(coord, 1, whxy);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001974
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001975 GLint depth = programObject->getDxDepthLocation();
daniel@transgaming.com996675c2010-11-17 13:06:29 +00001976 GLfloat dz[2] = {(zFar - zNear) / 2.0f, (zNear + zFar) / 2.0f};
daniel@transgaming.com31754962010-11-28 02:02:52 +00001977 programObject->setUniform2fv(depth, 1, dz);
daniel@transgaming.com9b5f5442010-03-16 05:43:55 +00001978
daniel@transgaming.com31754962010-11-28 02:02:52 +00001979 GLint depthRange = programObject->getDxDepthRangeLocation();
1980 GLfloat nearFarDiff[3] = {zNear, zFar, zFar - zNear};
1981 programObject->setUniform3fv(depthRange, 1, nearFarDiff);
jbauman@chromium.org54f59ef2011-10-12 17:03:34 +00001982 mDxUniformsDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001983 }
1984
1985 return true;
1986}
1987
1988// 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 +00001989void Context::applyState(GLenum drawMode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001990{
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001991 Program *programObject = getCurrentProgram();
1992
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001993 Framebuffer *framebufferObject = getDrawFramebuffer();
1994
1995 GLenum adjustedFrontFace = adjustWinding(mState.frontFace);
1996
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00001997 GLint frontCCW = programObject->getDxFrontCCWLocation();
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00001998 GLint ccw = (adjustedFrontFace == GL_CCW);
daniel@transgaming.com79b820b2010-03-16 05:48:57 +00001999 programObject->setUniform1iv(frontCCW, 1, &ccw);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002000
daniel@transgaming.com91fd1de2010-05-18 18:51:40 +00002001 GLint pointsOrLines = programObject->getDxPointsOrLinesLocation();
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002002 GLint alwaysFront = !isTriangleMode(drawMode);
2003 programObject->setUniform1iv(pointsOrLines, 1, &alwaysFront);
2004
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002005 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002006 bool zeroColorMaskAllowed = identifier->VendorId != 0x1002;
2007 // Apparently some ATI cards have a bug where a draw with a zero color
2008 // write mask can cause later draws to have incorrect results. Instead,
2009 // set a nonzero color write mask but modify the blend state so that no
2010 // drawing is done.
2011 // http://code.google.com/p/angleproject/issues/detail?id=169
2012
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002013 if (mCullStateDirty || mFrontFaceDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002014 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002015 if (mState.cullFace)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002016 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002017 mDevice->SetRenderState(D3DRS_CULLMODE, es2dx::ConvertCullMode(mState.cullMode, adjustedFrontFace));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002018 }
2019 else
2020 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002021 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002022 }
2023
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002024 mCullStateDirty = false;
2025 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002026
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002027 if (mDepthStateDirty)
2028 {
daniel@transgaming.com317887f2011-05-11 15:26:12 +00002029 if (mState.depthTest)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002030 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002031 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
2032 mDevice->SetRenderState(D3DRS_ZFUNC, es2dx::ConvertComparison(mState.depthFunc));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002033 }
2034 else
2035 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002036 mDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002037 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002038
2039 mDepthStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002040 }
2041
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002042 if (!zeroColorMaskAllowed && (mMaskStateDirty || mBlendStateDirty))
2043 {
2044 mBlendStateDirty = true;
2045 mMaskStateDirty = true;
2046 }
2047
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002048 if (mBlendStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002049 {
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002050 if (mState.blend)
daniel@transgaming.com1436e262010-03-17 03:58:56 +00002051 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002052 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002053
2054 if (mState.sourceBlendRGB != GL_CONSTANT_ALPHA && mState.sourceBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA &&
2055 mState.destBlendRGB != GL_CONSTANT_ALPHA && mState.destBlendRGB != GL_ONE_MINUS_CONSTANT_ALPHA)
2056 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002057 mDevice->SetRenderState(D3DRS_BLENDFACTOR, es2dx::ConvertColor(mState.blendColor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002058 }
2059 else
2060 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002061 mDevice->SetRenderState(D3DRS_BLENDFACTOR, D3DCOLOR_RGBA(unorm<8>(mState.blendColor.alpha),
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002062 unorm<8>(mState.blendColor.alpha),
2063 unorm<8>(mState.blendColor.alpha),
2064 unorm<8>(mState.blendColor.alpha)));
2065 }
2066
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002067 mDevice->SetRenderState(D3DRS_SRCBLEND, es2dx::ConvertBlendFunc(mState.sourceBlendRGB));
2068 mDevice->SetRenderState(D3DRS_DESTBLEND, es2dx::ConvertBlendFunc(mState.destBlendRGB));
2069 mDevice->SetRenderState(D3DRS_BLENDOP, es2dx::ConvertBlendOp(mState.blendEquationRGB));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002070
2071 if (mState.sourceBlendRGB != mState.sourceBlendAlpha ||
2072 mState.destBlendRGB != mState.destBlendAlpha ||
2073 mState.blendEquationRGB != mState.blendEquationAlpha)
2074 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002075 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002076
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002077 mDevice->SetRenderState(D3DRS_SRCBLENDALPHA, es2dx::ConvertBlendFunc(mState.sourceBlendAlpha));
2078 mDevice->SetRenderState(D3DRS_DESTBLENDALPHA, es2dx::ConvertBlendFunc(mState.destBlendAlpha));
2079 mDevice->SetRenderState(D3DRS_BLENDOPALPHA, es2dx::ConvertBlendOp(mState.blendEquationAlpha));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002080 }
2081 else
2082 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002083 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002084 }
daniel@transgaming.com1436e262010-03-17 03:58:56 +00002085 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002086 else
daniel@transgaming.comaede6302010-04-29 03:35:48 +00002087 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002088 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
daniel@transgaming.comaede6302010-04-29 03:35:48 +00002089 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002090
2091 mBlendStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002092 }
2093
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002094 if (mStencilStateDirty || mFrontFaceDirty)
2095 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002096 if (mState.stencilTest && framebufferObject->hasStencil())
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002097 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002098 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2099 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, TRUE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002100
2101 // FIXME: Unsupported by D3D9
2102 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILREF = D3DRS_STENCILREF;
2103 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILMASK = D3DRS_STENCILMASK;
2104 const D3DRENDERSTATETYPE D3DRS_CCW_STENCILWRITEMASK = D3DRS_STENCILWRITEMASK;
2105 if (mState.stencilWritemask != mState.stencilBackWritemask ||
2106 mState.stencilRef != mState.stencilBackRef ||
2107 mState.stencilMask != mState.stencilBackMask)
2108 {
2109 ERR("Separate front/back stencil writemasks, reference values, or stencil mask values are invalid under WebGL.");
2110 return error(GL_INVALID_OPERATION);
2111 }
2112
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00002113 // get the maximum size of the stencil ref
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00002114 gl::Renderbuffer *stencilbuffer = framebufferObject->getStencilbuffer();
daniel@transgaming.comdd7948b2010-06-02 16:12:34 +00002115 GLuint maxStencil = (1 << stencilbuffer->getStencilSize()) - 1;
2116
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002117 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilWritemask);
2118 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002119 es2dx::ConvertComparison(mState.stencilFunc));
2120
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002121 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilRef < (GLint)maxStencil) ? mState.stencilRef : maxStencil);
2122 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002123
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002124 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002125 es2dx::ConvertStencilOp(mState.stencilFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002126 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002127 es2dx::ConvertStencilOp(mState.stencilPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002128 mDevice->SetRenderState(adjustedFrontFace == GL_CCW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002129 es2dx::ConvertStencilOp(mState.stencilPassDepthPass));
2130
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002131 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILWRITEMASK : D3DRS_CCW_STENCILWRITEMASK, mState.stencilBackWritemask);
2132 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFUNC : D3DRS_CCW_STENCILFUNC,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002133 es2dx::ConvertComparison(mState.stencilBackFunc));
2134
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002135 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILREF : D3DRS_CCW_STENCILREF, (mState.stencilBackRef < (GLint)maxStencil) ? mState.stencilBackRef : maxStencil);
2136 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILMASK : D3DRS_CCW_STENCILMASK, mState.stencilBackMask);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002137
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002138 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILFAIL : D3DRS_CCW_STENCILFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002139 es2dx::ConvertStencilOp(mState.stencilBackFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002140 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILZFAIL : D3DRS_CCW_STENCILZFAIL,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002141 es2dx::ConvertStencilOp(mState.stencilBackPassDepthFail));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002142 mDevice->SetRenderState(adjustedFrontFace == GL_CW ? D3DRS_STENCILPASS : D3DRS_CCW_STENCILPASS,
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002143 es2dx::ConvertStencilOp(mState.stencilBackPassDepthPass));
2144 }
2145 else
2146 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002147 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002148 }
2149
2150 mStencilStateDirty = false;
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002151 mFrontFaceDirty = false;
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002152 }
2153
2154 if (mMaskStateDirty)
2155 {
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002156 int colorMask = es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen,
2157 mState.colorMaskBlue, mState.colorMaskAlpha);
2158 if (colorMask == 0 && !zeroColorMaskAllowed)
2159 {
2160 // Enable green channel, but set blending so nothing will be drawn.
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002161 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, D3DCOLORWRITEENABLE_GREEN);
2162 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002163
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002164 mDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_ZERO);
2165 mDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_ONE);
2166 mDevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002167 }
2168 else
2169 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002170 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, colorMask);
jbauman@chromium.org03208d52011-06-15 01:15:24 +00002171 }
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002172 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, mState.depthMask ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002173
2174 mMaskStateDirty = false;
2175 }
2176
2177 if (mPolygonOffsetStateDirty)
2178 {
2179 if (mState.polygonOffsetFill)
2180 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00002181 gl::Renderbuffer *depthbuffer = framebufferObject->getDepthbuffer();
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002182 if (depthbuffer)
2183 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002184 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, *((DWORD*)&mState.polygonOffsetFactor));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002185 float depthBias = ldexp(mState.polygonOffsetUnits, -(int)(depthbuffer->getDepthSize()));
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002186 mDevice->SetRenderState(D3DRS_DEPTHBIAS, *((DWORD*)&depthBias));
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002187 }
2188 }
2189 else
2190 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002191 mDevice->SetRenderState(D3DRS_SLOPESCALEDEPTHBIAS, 0);
2192 mDevice->SetRenderState(D3DRS_DEPTHBIAS, 0);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002193 }
2194
2195 mPolygonOffsetStateDirty = false;
2196 }
2197
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002198 if (mSampleStateDirty)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002199 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002200 if (mState.sampleAlphaToCoverage)
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002201 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002202 FIXME("Sample alpha to coverage is unimplemented.");
2203 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002204
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002205 mDevice->SetRenderState(D3DRS_MULTISAMPLEANTIALIAS, TRUE);
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002206 if (mState.sampleCoverage)
2207 {
2208 unsigned int mask = 0;
2209 if (mState.sampleCoverageValue != 0)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002210 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002211 float threshold = 0.5f;
2212
2213 for (int i = 0; i < framebufferObject->getSamples(); ++i)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002214 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002215 mask <<= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002216
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002217 if ((i + 1) * mState.sampleCoverageValue >= threshold)
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002218 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002219 threshold += 1.0f;
2220 mask |= 1;
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002221 }
2222 }
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002223 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002224
2225 if (mState.sampleCoverageInvert)
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002226 {
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002227 mask = ~mask;
enne@chromium.orgc5f8dea2010-09-21 16:40:30 +00002228 }
daniel@transgaming.com3203c102011-06-08 12:41:32 +00002229
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002230 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, mask);
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00002231 }
2232 else
2233 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002234 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.coma87bdf52010-04-29 03:38:55 +00002235 }
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002236
2237 mSampleStateDirty = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002238 }
2239
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002240 if (mDitherStateDirty)
2241 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002242 mDevice->SetRenderState(D3DRS_DITHERENABLE, mState.dither ? TRUE : FALSE);
daniel@transgaming.coma79f9d12010-05-12 03:40:01 +00002243
2244 mDitherStateDirty = false;
2245 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002246}
2247
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002248GLenum Context::applyVertexBuffer(GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002249{
daniel@transgaming.combaa74512011-04-13 14:56:47 +00002250 TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS];
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002251
daniel@transgaming.com2fc9f902012-01-27 15:39:00 +00002252 GLenum err = mVertexDataManager->prepareVertexData(first, count, attributes, instances);
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002253 if (err != GL_NO_ERROR)
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002254 {
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002255 return err;
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002256 }
2257
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002258 return mVertexDeclarationCache.applyDeclaration(mDevice, attributes, getCurrentProgram(), instances);
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002259}
2260
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002261// Applies the indices and element array bindings to the Direct3D 9 device
daniel@transgaming.comd2820bf2012-01-27 15:38:48 +00002262GLenum Context::applyIndexBuffer(const GLvoid *indices, GLsizei count, GLenum mode, GLenum type, TranslatedIndexData *indexInfo)
daniel@transgaming.com0f7aaf52010-03-11 19:41:38 +00002263{
daniel@transgaming.com83921382011-01-08 05:46:00 +00002264 GLenum err = mIndexDataManager->prepareIndexData(type, count, mState.elementArrayBuffer.get(), indices, indexInfo);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002265
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002266 if (err == GL_NO_ERROR)
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002267 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002268 if (indexInfo->serial != mAppliedIBSerial)
2269 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002270 mDevice->SetIndices(indexInfo->indexBuffer);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002271 mAppliedIBSerial = indexInfo->serial;
2272 }
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00002273 }
2274
daniel@transgaming.com81655a72010-05-20 19:18:17 +00002275 return err;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002276}
2277
2278// Applies the shaders and shader constants to the Direct3D 9 device
2279void Context::applyShaders()
2280{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002281 Program *programObject = getCurrentProgram();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002282 if (programObject->getSerial() != mAppliedProgramSerial)
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002283 {
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00002284 IDirect3DVertexShader9 *vertexShader = programObject->getVertexShader();
2285 IDirect3DPixelShader9 *pixelShader = programObject->getPixelShader();
2286
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002287 mDevice->SetPixelShader(pixelShader);
2288 mDevice->SetVertexShader(vertexShader);
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002289 programObject->dirtyAllUniforms();
daniel@transgaming.coma9eb5da2011-03-21 16:39:16 +00002290 mAppliedProgramSerial = programObject->getSerial();
daniel@transgaming.com4fa08332010-05-11 02:29:27 +00002291 }
2292
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002293 programObject->applyUniforms();
2294}
2295
2296// Applies the textures and sampler states to the Direct3D 9 device
2297void Context::applyTextures()
2298{
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002299 applyTextures(SAMPLER_PIXEL);
2300
2301 if (mSupportsVertexTexture)
2302 {
2303 applyTextures(SAMPLER_VERTEX);
2304 }
2305}
2306
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002307// For each Direct3D 9 sampler of either the pixel or vertex stage,
2308// looks up the corresponding OpenGL texture image unit and texture type,
2309// and sets the texture and its addressing/filtering state (or NULL when inactive).
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002310void Context::applyTextures(SamplerType type)
2311{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002312 Program *programObject = getCurrentProgram();
2313
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002314 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 +00002315 unsigned int *appliedTextureSerial = (type == SAMPLER_PIXEL) ? mAppliedTextureSerialPS : mAppliedTextureSerialVS;
2316 int d3dSamplerOffset = (type == SAMPLER_PIXEL) ? 0 : D3DVERTEXTEXTURESAMPLER0;
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002317 int samplerRange = programObject->getUsedSamplerRange(type);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002318
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002319 for (int samplerIndex = 0; samplerIndex < samplerRange; samplerIndex++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002320 {
daniel@transgaming.com9ba680a2011-05-11 15:37:11 +00002321 int textureUnit = programObject->getSamplerMapping(type, samplerIndex); // OpenGL texture image unit index
jbauman@chromium.org8b3c1af2011-10-12 01:21:41 +00002322 int d3dSampler = samplerIndex + d3dSamplerOffset;
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002323
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002324 if (textureUnit != -1)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002325 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002326 TextureType textureType = programObject->getSamplerTextureType(type, samplerIndex);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002327
2328 Texture *texture = getSamplerTexture(textureUnit, textureType);
2329
daniel@transgaming.comfbc39522011-11-11 04:10:28 +00002330 if (appliedTextureSerial[samplerIndex] != texture->getTextureSerial() || texture->hasDirtyParameters() || texture->hasDirtyImages())
daniel@transgaming.com12d54072010-03-16 06:23:26 +00002331 {
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002332 IDirect3DBaseTexture9 *d3dTexture = texture->getTexture();
2333
2334 if (d3dTexture)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002335 {
daniel@transgaming.comfbc39522011-11-11 04:10:28 +00002336 if (appliedTextureSerial[samplerIndex] != texture->getTextureSerial() || texture->hasDirtyParameters())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002337 {
2338 GLenum wrapS = texture->getWrapS();
2339 GLenum wrapT = texture->getWrapT();
2340 GLenum minFilter = texture->getMinFilter();
2341 GLenum magFilter = texture->getMagFilter();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002342
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002343 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSU, es2dx::ConvertTextureWrap(wrapS));
2344 mDevice->SetSamplerState(d3dSampler, D3DSAMP_ADDRESSV, es2dx::ConvertTextureWrap(wrapT));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002345
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002346 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MAGFILTER, es2dx::ConvertMagFilter(magFilter));
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002347 D3DTEXTUREFILTERTYPE d3dMinFilter, d3dMipFilter;
2348 es2dx::ConvertMinFilter(minFilter, &d3dMinFilter, &d3dMipFilter);
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002349 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MINFILTER, d3dMinFilter);
2350 mDevice->SetSamplerState(d3dSampler, D3DSAMP_MIPFILTER, d3dMipFilter);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002351 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002352
daniel@transgaming.comfbc39522011-11-11 04:10:28 +00002353 if (appliedTextureSerial[samplerIndex] != texture->getTextureSerial() || texture->hasDirtyImages())
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002354 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002355 mDevice->SetTexture(d3dSampler, d3dTexture);
daniel@transgaming.coma06aa872011-03-21 17:22:21 +00002356 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002357 }
2358 else
2359 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002360 mDevice->SetTexture(d3dSampler, getIncompleteTexture(textureType)->getTexture());
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002361 }
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002362
daniel@transgaming.comfbc39522011-11-11 04:10:28 +00002363 appliedTextureSerial[samplerIndex] = texture->getTextureSerial();
daniel@transgaming.com38e76e52011-03-21 16:39:10 +00002364 texture->resetDirty();
2365 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002366 }
daniel@transgaming.com416485f2010-03-16 06:23:23 +00002367 else
2368 {
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002369 if (appliedTextureSerial[samplerIndex] != 0)
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002370 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002371 mDevice->SetTexture(d3dSampler, NULL);
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002372 appliedTextureSerial[samplerIndex] = 0;
daniel@transgaming.com5a0b0a82010-05-12 03:45:07 +00002373 }
daniel@transgaming.comd4a35172011-05-11 15:36:45 +00002374 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002375 }
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002376
2377 for (int samplerIndex = samplerRange; samplerIndex < samplerCount; samplerIndex++)
2378 {
2379 if (appliedTextureSerial[samplerIndex] != 0)
2380 {
daniel@transgaming.comc5a7b692011-10-26 02:45:44 +00002381 mDevice->SetTexture(samplerIndex + d3dSamplerOffset, NULL);
jbauman@chromium.orgb6e72222011-10-18 23:01:46 +00002382 appliedTextureSerial[samplerIndex] = 0;
2383 }
2384 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002385}
2386
daniel@transgaming.comb7915a52011-11-12 03:14:20 +00002387void Context::readPixels(GLint x, GLint y, GLsizei width, GLsizei height,
2388 GLenum format, GLenum type, GLsizei *bufSize, void* pixels)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002389{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002390 Framebuffer *framebuffer = getReadFramebuffer();
daniel@transgaming.combbc57792010-07-28 19:21:05 +00002391
2392 if (framebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
2393 {
2394 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
2395 }
2396
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00002397 if (getReadFramebufferHandle() != 0 && framebuffer->getSamples() != 0)
2398 {
2399 return error(GL_INVALID_OPERATION);
2400 }
2401
daniel@transgaming.comb7915a52011-11-12 03:14:20 +00002402 GLsizei outputPitch = ComputePitch(width, format, type, mState.packAlignment);
2403 // sized query sanity check
2404 if (bufSize)
2405 {
2406 int requiredSize = outputPitch * height;
2407 if (requiredSize > *bufSize)
2408 {
2409 return error(GL_INVALID_OPERATION);
2410 }
2411 }
2412
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002413 IDirect3DSurface9 *renderTarget = framebuffer->getRenderTarget();
daniel@transgaming.comd36c6a02010-08-31 12:15:09 +00002414 if (!renderTarget)
2415 {
2416 return; // Context must be lost, return silently
2417 }
2418
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002419 D3DSURFACE_DESC desc;
2420 renderTarget->GetDesc(&desc);
2421
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002422 if (desc.MultiSampleType != D3DMULTISAMPLE_NONE)
2423 {
2424 UNIMPLEMENTED(); // FIXME: Requires resolve using StretchRect into non-multisampled render target
daniel@transgaming.coma5798952011-12-13 17:30:43 +00002425 renderTarget->Release();
daniel@transgaming.com97b12412011-08-09 13:40:28 +00002426 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002427 }
2428
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002429 HRESULT result;
2430 IDirect3DSurface9 *systemSurface = NULL;
2431 bool directToPixels = getPackReverseRowOrder() && getPackAlignment() <= 4 && mDisplay->isD3d9ExDevice() &&
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00002432 x == 0 && y == 0 && UINT(width) == desc.Width && UINT(height) == desc.Height &&
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002433 desc.Format == D3DFMT_A8R8G8B8 && format == GL_BGRA_EXT && type == GL_UNSIGNED_BYTE;
2434 if (directToPixels)
2435 {
2436 // Use the pixels ptr as a shared handle to write directly into client's memory
2437 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2438 D3DPOOL_SYSTEMMEM, &systemSurface, &pixels);
2439 if (FAILED(result))
2440 {
2441 // Try again without the shared handle
2442 directToPixels = false;
2443 }
2444 }
2445
2446 if (!directToPixels)
2447 {
2448 result = mDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, desc.Format,
2449 D3DPOOL_SYSTEMMEM, &systemSurface, NULL);
2450 if (FAILED(result))
2451 {
2452 ASSERT(result == D3DERR_OUTOFVIDEOMEMORY || result == E_OUTOFMEMORY);
2453 return error(GL_OUT_OF_MEMORY);
2454 }
2455 }
2456
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002457 result = mDevice->GetRenderTargetData(renderTarget, systemSurface);
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00002458 renderTarget->Release();
daniel@transgaming.coma5798952011-12-13 17:30:43 +00002459 renderTarget = NULL;
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00002460
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002461 if (FAILED(result))
2462 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002463 systemSurface->Release();
2464
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002465 // It turns out that D3D will sometimes produce more error
2466 // codes than those documented.
2467 if (checkDeviceLost(result))
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002468 return error(GL_OUT_OF_MEMORY);
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002469 else
2470 {
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002471 UNREACHABLE();
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002472 return;
apatrick@chromium.org6db8cab2010-07-22 20:39:50 +00002473 }
daniel@transgaming.com6f5c5fc2011-11-09 17:46:39 +00002474
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002475 }
2476
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002477 if (directToPixels)
2478 {
2479 systemSurface->Release();
2480 return;
2481 }
2482
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002483 D3DLOCKED_RECT lock;
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002484 RECT rect = transformPixelRect(x, y, width, height, desc.Height);
2485 rect.left = clamp(rect.left, 0L, static_cast<LONG>(desc.Width));
2486 rect.top = clamp(rect.top, 0L, static_cast<LONG>(desc.Height));
2487 rect.right = clamp(rect.right, 0L, static_cast<LONG>(desc.Width));
2488 rect.bottom = clamp(rect.bottom, 0L, static_cast<LONG>(desc.Height));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002489
2490 result = systemSurface->LockRect(&lock, &rect, D3DLOCK_READONLY);
2491
2492 if (FAILED(result))
2493 {
2494 UNREACHABLE();
2495 systemSurface->Release();
2496
2497 return; // No sensible error to generate
2498 }
2499
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002500 unsigned char *dest = (unsigned char*)pixels;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002501 unsigned short *dest16 = (unsigned short*)pixels;
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00002502
2503 unsigned char *source;
2504 int inputPitch;
2505 if (getPackReverseRowOrder())
2506 {
2507 source = (unsigned char*)lock.pBits;
2508 inputPitch = lock.Pitch;
2509 }
2510 else
2511 {
2512 source = ((unsigned char*)lock.pBits) + lock.Pitch * (rect.bottom - rect.top - 1);
2513 inputPitch = -lock.Pitch;
2514 }
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002515
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002516 for (int j = 0; j < rect.bottom - rect.top; j++)
2517 {
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002518 if (desc.Format == D3DFMT_A8R8G8B8 &&
2519 format == GL_BGRA_EXT &&
2520 type == GL_UNSIGNED_BYTE)
2521 {
2522 // Fast path for EXT_read_format_bgra, given
2523 // an RGBA source buffer. Note that buffers with no
2524 // alpha go through the slow path below.
2525 memcpy(dest + j * outputPitch,
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002526 source + j * inputPitch,
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002527 (rect.right - rect.left) * 4);
2528 continue;
2529 }
2530
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002531 for (int i = 0; i < rect.right - rect.left; i++)
2532 {
2533 float r;
2534 float g;
2535 float b;
2536 float a;
2537
2538 switch (desc.Format)
2539 {
2540 case D3DFMT_R5G6B5:
2541 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002542 unsigned short rgb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002543
2544 a = 1.0f;
2545 b = (rgb & 0x001F) * (1.0f / 0x001F);
2546 g = (rgb & 0x07E0) * (1.0f / 0x07E0);
2547 r = (rgb & 0xF800) * (1.0f / 0xF800);
2548 }
2549 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002550 case D3DFMT_A1R5G5B5:
2551 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002552 unsigned short argb = *(unsigned short*)(source + 2 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002553
2554 a = (argb & 0x8000) ? 1.0f : 0.0f;
2555 b = (argb & 0x001F) * (1.0f / 0x001F);
2556 g = (argb & 0x03E0) * (1.0f / 0x03E0);
2557 r = (argb & 0x7C00) * (1.0f / 0x7C00);
2558 }
2559 break;
2560 case D3DFMT_A8R8G8B8:
2561 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002562 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002563
2564 a = (argb & 0xFF000000) * (1.0f / 0xFF000000);
2565 b = (argb & 0x000000FF) * (1.0f / 0x000000FF);
2566 g = (argb & 0x0000FF00) * (1.0f / 0x0000FF00);
2567 r = (argb & 0x00FF0000) * (1.0f / 0x00FF0000);
2568 }
2569 break;
2570 case D3DFMT_X8R8G8B8:
2571 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002572 unsigned int xrgb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002573
2574 a = 1.0f;
2575 b = (xrgb & 0x000000FF) * (1.0f / 0x000000FF);
2576 g = (xrgb & 0x0000FF00) * (1.0f / 0x0000FF00);
2577 r = (xrgb & 0x00FF0000) * (1.0f / 0x00FF0000);
2578 }
2579 break;
2580 case D3DFMT_A2R10G10B10:
2581 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002582 unsigned int argb = *(unsigned int*)(source + 4 * i + j * inputPitch);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002583
2584 a = (argb & 0xC0000000) * (1.0f / 0xC0000000);
2585 b = (argb & 0x000003FF) * (1.0f / 0x000003FF);
2586 g = (argb & 0x000FFC00) * (1.0f / 0x000FFC00);
2587 r = (argb & 0x3FF00000) * (1.0f / 0x3FF00000);
2588 }
2589 break;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002590 case D3DFMT_A32B32G32R32F:
2591 {
2592 // float formats in D3D are stored rgba, rather than the other way round
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002593 r = *((float*)(source + 16 * i + j * inputPitch) + 0);
2594 g = *((float*)(source + 16 * i + j * inputPitch) + 1);
2595 b = *((float*)(source + 16 * i + j * inputPitch) + 2);
2596 a = *((float*)(source + 16 * i + j * inputPitch) + 3);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002597 }
2598 break;
2599 case D3DFMT_A16B16G16R16F:
2600 {
2601 // float formats in D3D are stored rgba, rather than the other way round
2602 float abgr[4];
2603
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00002604 D3DXFloat16To32Array(abgr, (D3DXFLOAT16*)(source + 8 * i + j * inputPitch), 4);
daniel@transgaming.com1297d922010-09-01 15:47:47 +00002605
2606 a = abgr[3];
2607 b = abgr[2];
2608 g = abgr[1];
2609 r = abgr[0];
2610 }
2611 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002612 default:
2613 UNIMPLEMENTED(); // FIXME
2614 UNREACHABLE();
apatrick@chromium.orga1d80592012-01-25 21:52:10 +00002615 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002616 }
2617
2618 switch (format)
2619 {
2620 case GL_RGBA:
2621 switch (type)
2622 {
2623 case GL_UNSIGNED_BYTE:
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002624 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * r + 0.5f);
2625 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2626 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * b + 0.5f);
2627 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002628 break;
2629 default: UNREACHABLE();
2630 }
2631 break;
daniel@transgaming.coma9198d92010-08-08 04:49:56 +00002632 case GL_BGRA_EXT:
2633 switch (type)
2634 {
2635 case GL_UNSIGNED_BYTE:
2636 dest[4 * i + j * outputPitch + 0] = (unsigned char)(255 * b + 0.5f);
2637 dest[4 * i + j * outputPitch + 1] = (unsigned char)(255 * g + 0.5f);
2638 dest[4 * i + j * outputPitch + 2] = (unsigned char)(255 * r + 0.5f);
2639 dest[4 * i + j * outputPitch + 3] = (unsigned char)(255 * a + 0.5f);
2640 break;
2641 case GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT:
2642 // According to the desktop GL spec in the "Transfer of Pixel Rectangles" section
2643 // this type is packed as follows:
2644 // 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
2645 // --------------------------------------------------------------------------------
2646 // | 4th | 3rd | 2nd | 1st component |
2647 // --------------------------------------------------------------------------------
2648 // in the case of BGRA_EXT, B is the first component, G the second, and so forth.
2649 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2650 ((unsigned short)(15 * a + 0.5f) << 12)|
2651 ((unsigned short)(15 * r + 0.5f) << 8) |
2652 ((unsigned short)(15 * g + 0.5f) << 4) |
2653 ((unsigned short)(15 * b + 0.5f) << 0);
2654 break;
2655 case GL_UNSIGNED_SHORT_1_5_5_5_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)( a + 0.5f) << 15) |
2665 ((unsigned short)(31 * r + 0.5f) << 10) |
2666 ((unsigned short)(31 * g + 0.5f) << 5) |
2667 ((unsigned short)(31 * b + 0.5f) << 0);
2668 break;
2669 default: UNREACHABLE();
2670 }
2671 break;
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002672 case GL_RGB: // IMPLEMENTATION_COLOR_READ_FORMAT
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002673 switch (type)
2674 {
daniel@transgaming.comafb23952010-04-13 03:25:54 +00002675 case GL_UNSIGNED_SHORT_5_6_5: // IMPLEMENTATION_COLOR_READ_TYPE
daniel@transgaming.com713914b2010-05-04 03:35:17 +00002676 dest16[i + j * outputPitch / sizeof(unsigned short)] =
2677 ((unsigned short)(31 * b + 0.5f) << 0) |
2678 ((unsigned short)(63 * g + 0.5f) << 5) |
2679 ((unsigned short)(31 * r + 0.5f) << 11);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002680 break;
2681 default: UNREACHABLE();
2682 }
2683 break;
2684 default: UNREACHABLE();
2685 }
2686 }
2687 }
2688
2689 systemSurface->UnlockRect();
2690
2691 systemSurface->Release();
2692}
2693
2694void Context::clear(GLbitfield mask)
2695{
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00002696 Framebuffer *framebufferObject = getDrawFramebuffer();
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002697
2698 if (!framebufferObject || framebufferObject->completeness() != GL_FRAMEBUFFER_COMPLETE)
2699 {
daniel@transgaming.comb5a3a6b2011-03-21 16:38:46 +00002700 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002701 }
2702
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002703 DWORD flags = 0;
2704
2705 if (mask & GL_COLOR_BUFFER_BIT)
2706 {
2707 mask &= ~GL_COLOR_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002708
2709 if (framebufferObject->getColorbufferType() != GL_NONE)
2710 {
2711 flags |= D3DCLEAR_TARGET;
2712 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002713 }
2714
2715 if (mask & GL_DEPTH_BUFFER_BIT)
2716 {
2717 mask &= ~GL_DEPTH_BUFFER_BIT;
daniel@transgaming.comc6f53402010-06-24 13:02:19 +00002718 if (mState.depthMask && framebufferObject->getDepthbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002719 {
2720 flags |= D3DCLEAR_ZBUFFER;
2721 }
2722 }
2723
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002724 GLuint stencilUnmasked = 0x0;
2725
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002726 if (mask & GL_STENCIL_BUFFER_BIT)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002727 {
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002728 mask &= ~GL_STENCIL_BUFFER_BIT;
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002729 if (framebufferObject->getStencilbufferType() != GL_NONE)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002730 {
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002731 IDirect3DSurface9 *depthStencil = framebufferObject->getStencilbuffer()->getDepthStencil();
apatrick@chromium.orgb2bdd062010-10-05 02:24:30 +00002732 if (!depthStencil)
2733 {
2734 ERR("Depth stencil pointer unexpectedly null.");
2735 return;
2736 }
2737
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002738 D3DSURFACE_DESC desc;
2739 depthStencil->GetDesc(&desc);
2740
daniel@transgaming.comd2fd4f22011-02-01 18:49:11 +00002741 unsigned int stencilSize = dx2es::GetStencilSize(desc.Format);
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00002742 stencilUnmasked = (0x1 << stencilSize) - 1;
2743
2744 if (stencilUnmasked != 0x0)
2745 {
2746 flags |= D3DCLEAR_STENCIL;
2747 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002748 }
2749 }
2750
2751 if (mask != 0)
2752 {
2753 return error(GL_INVALID_VALUE);
2754 }
2755
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002756 if (!applyRenderTarget(true)) // Clips the clear to the scissor rectangle but not the viewport
2757 {
2758 return;
2759 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002760
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002761 D3DCOLOR color = D3DCOLOR_ARGB(unorm<8>(mState.colorClearValue.alpha),
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002762 unorm<8>(mState.colorClearValue.red),
2763 unorm<8>(mState.colorClearValue.green),
2764 unorm<8>(mState.colorClearValue.blue));
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002765 float depth = clamp01(mState.depthClearValue);
2766 int stencil = mState.stencilClearValue & 0x000000FF;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002767
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002768 bool alphaUnmasked = (dx2es::GetAlphaSize(mRenderTargetDesc.Format) == 0) || mState.colorMaskAlpha;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002769
2770 const bool needMaskedStencilClear = (flags & D3DCLEAR_STENCIL) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002771 (mState.stencilWritemask & stencilUnmasked) != stencilUnmasked;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002772 const bool needMaskedColorClear = (flags & D3DCLEAR_TARGET) &&
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002773 !(mState.colorMaskRed && mState.colorMaskGreen &&
2774 mState.colorMaskBlue && alphaUnmasked);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002775
2776 if (needMaskedColorClear || needMaskedStencilClear)
2777 {
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002778 // State which is altered in all paths from this point to the clear call is saved.
2779 // State which is altered in only some paths will be flagged dirty in the case that
2780 // that path is taken.
2781 HRESULT hr;
2782 if (mMaskedClearSavedState == NULL)
2783 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002784 hr = mDevice->BeginStateBlock();
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002785 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2786
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002787 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2788 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2789 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2790 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2791 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2792 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2793 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2794 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
2795 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
2796 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
2797 mDevice->SetPixelShader(NULL);
2798 mDevice->SetVertexShader(NULL);
2799 mDevice->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
2800 mDevice->SetStreamSource(0, NULL, 0, 0);
2801 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2802 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2803 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2804 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2805 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2806 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2807 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002808
2809 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2810 {
2811 mDevice->SetStreamSourceFreq(i, 1);
2812 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002813
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002814 hr = mDevice->EndStateBlock(&mMaskedClearSavedState);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002815 ASSERT(SUCCEEDED(hr) || hr == D3DERR_OUTOFVIDEOMEMORY || hr == E_OUTOFMEMORY);
2816 }
2817
2818 ASSERT(mMaskedClearSavedState != NULL);
2819
2820 if (mMaskedClearSavedState != NULL)
2821 {
2822 hr = mMaskedClearSavedState->Capture();
2823 ASSERT(SUCCEEDED(hr));
2824 }
2825
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002826 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, FALSE);
2827 mDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_ALWAYS);
2828 mDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
2829 mDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
2830 mDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
2831 mDevice->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE);
2832 mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
2833 mDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002834
2835 if (flags & D3DCLEAR_TARGET)
2836 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002837 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, es2dx::ConvertColorMask(mState.colorMaskRed, mState.colorMaskGreen, mState.colorMaskBlue, mState.colorMaskAlpha));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002838 }
2839 else
2840 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002841 mDevice->SetRenderState(D3DRS_COLORWRITEENABLE, 0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002842 }
2843
2844 if (stencilUnmasked != 0x0 && (flags & D3DCLEAR_STENCIL))
2845 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002846 mDevice->SetRenderState(D3DRS_STENCILENABLE, TRUE);
2847 mDevice->SetRenderState(D3DRS_TWOSIDEDSTENCILMODE, FALSE);
2848 mDevice->SetRenderState(D3DRS_STENCILFUNC, D3DCMP_ALWAYS);
2849 mDevice->SetRenderState(D3DRS_STENCILREF, stencil);
2850 mDevice->SetRenderState(D3DRS_STENCILWRITEMASK, mState.stencilWritemask);
2851 mDevice->SetRenderState(D3DRS_STENCILFAIL, D3DSTENCILOP_REPLACE);
2852 mDevice->SetRenderState(D3DRS_STENCILZFAIL, D3DSTENCILOP_REPLACE);
2853 mDevice->SetRenderState(D3DRS_STENCILPASS, D3DSTENCILOP_REPLACE);
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002854 mStencilStateDirty = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002855 }
2856 else
2857 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002858 mDevice->SetRenderState(D3DRS_STENCILENABLE, FALSE);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002859 }
2860
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002861 mDevice->SetPixelShader(NULL);
2862 mDevice->SetVertexShader(NULL);
2863 mDevice->SetFVF(D3DFVF_XYZRHW);
2864 mDevice->SetRenderState(D3DRS_SEPARATEALPHABLENDENABLE, TRUE);
2865 mDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
2866 mDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TFACTOR);
2867 mDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
2868 mDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TFACTOR);
2869 mDevice->SetRenderState(D3DRS_TEXTUREFACTOR, color);
2870 mDevice->SetRenderState(D3DRS_MULTISAMPLEMASK, 0xFFFFFFFF);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002871
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002872 for(int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
2873 {
2874 mDevice->SetStreamSourceFreq(i, 1);
2875 }
2876
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002877 float quad[4][4]; // A quadrilateral covering the target, aligned to match the edges
2878 quad[0][0] = -0.5f;
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002879 quad[0][1] = mRenderTargetDesc.Height - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002880 quad[0][2] = 0.0f;
2881 quad[0][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002882
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002883 quad[1][0] = mRenderTargetDesc.Width - 0.5f;
2884 quad[1][1] = mRenderTargetDesc.Height - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002885 quad[1][2] = 0.0f;
2886 quad[1][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002887
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002888 quad[2][0] = -0.5f;
2889 quad[2][1] = -0.5f;
2890 quad[2][2] = 0.0f;
2891 quad[2][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002892
daniel@transgaming.com56397df2011-12-22 19:39:18 +00002893 quad[3][0] = mRenderTargetDesc.Width - 0.5f;
daniel@transgaming.com1615be22011-02-09 16:30:06 +00002894 quad[3][1] = -0.5f;
2895 quad[3][2] = 0.0f;
2896 quad[3][3] = 1.0f;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002897
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002898 mDisplay->startScene();
2899 mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, quad, sizeof(float[4]));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002900
2901 if (flags & D3DCLEAR_ZBUFFER)
2902 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002903 mDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
2904 mDevice->SetRenderState(D3DRS_ZWRITEENABLE, TRUE);
2905 mDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002906 }
daniel@transgaming.com8f05d1a2010-06-07 02:06:03 +00002907
2908 if (mMaskedClearSavedState != NULL)
2909 {
2910 mMaskedClearSavedState->Apply();
2911 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002912 }
daniel@transgaming.com8ede24f2010-05-05 18:47:58 +00002913 else if (flags)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002914 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002915 mDevice->Clear(0, NULL, flags, color, depth, stencil);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002916 }
2917}
2918
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002919void Context::drawArrays(GLenum mode, GLint first, GLsizei count, GLsizei instances)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002920{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002921 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002922 {
2923 return error(GL_INVALID_OPERATION);
2924 }
2925
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002926 D3DPRIMITIVETYPE primitiveType;
2927 int primitiveCount;
2928
2929 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
2930 return error(GL_INVALID_ENUM);
2931
2932 if (primitiveCount <= 0)
2933 {
2934 return;
2935 }
2936
2937 if (!applyRenderTarget(false))
2938 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00002939 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002940 }
2941
daniel@transgaming.com5af64272010-04-15 20:45:12 +00002942 applyState(mode);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002943
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002944 GLenum err = applyVertexBuffer(first, count, instances);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00002945 if (err != GL_NO_ERROR)
2946 {
2947 return error(err);
2948 }
2949
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002950 applyShaders();
2951 applyTextures();
2952
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00002953 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00002954 {
2955 return error(GL_INVALID_OPERATION);
2956 }
2957
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002958 if (!cullSkipsDraw(mode))
2959 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00002960 mDisplay->startScene();
daniel@transgaming.com83921382011-01-08 05:46:00 +00002961
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002962 if (mode == GL_LINE_LOOP)
daniel@transgaming.comf6549452012-01-27 15:39:08 +00002963 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002964 drawLineLoop(count, GL_NONE, NULL, 0);
daniel@transgaming.comf6549452012-01-27 15:39:08 +00002965 }
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002966 else if (instances > 0)
daniel@transgaming.comf6549452012-01-27 15:39:08 +00002967 {
2968 StaticIndexBuffer *countingIB = mIndexDataManager->getCountingIndices(count);
2969 if (countingIB)
2970 {
2971 if (mAppliedIBSerial != countingIB->getSerial())
2972 {
2973 mDevice->SetIndices(countingIB->getBuffer());
2974 mAppliedIBSerial = countingIB->getSerial();
2975 }
2976
2977 mDevice->DrawIndexedPrimitive(primitiveType, 0, 0, count, 0, primitiveCount);
2978 }
2979 else
2980 {
2981 ERR("Could not create a counting index buffer for glDrawArraysInstanced.");
2982 return error(GL_OUT_OF_MEMORY);
2983 }
2984 }
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002985 else // Regular case
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002986 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00002987 mDevice->DrawPrimitive(primitiveType, 0, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00002988 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00002989 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002990}
2991
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00002992void Context::drawElements(GLenum mode, GLsizei count, GLenum type, const GLvoid *indices, GLsizei instances)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002993{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002994 if (!mState.currentProgram)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002995 {
2996 return error(GL_INVALID_OPERATION);
2997 }
2998
daniel@transgaming.com428d1582010-05-04 03:35:25 +00002999 if (!indices && !mState.elementArrayBuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003000 {
3001 return error(GL_INVALID_OPERATION);
3002 }
3003
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003004 D3DPRIMITIVETYPE primitiveType;
3005 int primitiveCount;
3006
3007 if(!es2dx::ConvertPrimitiveType(mode, count, &primitiveType, &primitiveCount))
3008 return error(GL_INVALID_ENUM);
3009
3010 if (primitiveCount <= 0)
3011 {
3012 return;
3013 }
3014
3015 if (!applyRenderTarget(false))
3016 {
daniel@transgaming.combaeb8c52010-05-05 18:50:39 +00003017 return;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003018 }
3019
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003020 applyState(mode);
daniel@transgaming.com41d8dd82010-05-12 03:45:03 +00003021
3022 TranslatedIndexData indexInfo;
3023 GLenum err = applyIndexBuffer(indices, count, mode, type, &indexInfo);
3024 if (err != GL_NO_ERROR)
3025 {
3026 return error(err);
3027 }
3028
daniel@transgaming.com83921382011-01-08 05:46:00 +00003029 GLsizei vertexCount = indexInfo.maxIndex - indexInfo.minIndex + 1;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00003030 err = applyVertexBuffer(indexInfo.minIndex, vertexCount, instances);
daniel@transgaming.com838bcea2010-05-20 19:17:42 +00003031 if (err != GL_NO_ERROR)
3032 {
3033 return error(err);
3034 }
3035
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003036 applyShaders();
3037 applyTextures();
3038
daniel@transgaming.comf494c9c2011-05-11 15:37:05 +00003039 if (!getCurrentProgram()->validateSamplers(false))
daniel@transgaming.comc3a0e942010-04-29 03:35:45 +00003040 {
3041 return error(GL_INVALID_OPERATION);
3042 }
3043
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003044 if (!cullSkipsDraw(mode))
3045 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003046 mDisplay->startScene();
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003047
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003048 if (mode == GL_LINE_LOOP)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003049 {
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003050 drawLineLoop(count, type, indices, indexInfo.minIndex);
3051 }
3052 else
3053 {
3054 mDevice->DrawIndexedPrimitive(primitiveType, -(INT)indexInfo.minIndex, indexInfo.minIndex, vertexCount, indexInfo.startIndex, primitiveCount);
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003055 }
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003056 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003057}
3058
daniel@transgaming.com0d86aa72011-10-26 02:35:10 +00003059// Implements glFlush when block is false, glFinish when block is true
3060void Context::sync(bool block)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003061{
apatrick@chromium.orga5ddde92012-01-10 23:00:07 +00003062 mDisplay->sync(block);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003063}
3064
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003065void Context::drawLineLoop(GLsizei count, GLenum type, const GLvoid *indices, int minIndex)
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003066{
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003067 // Get the raw indices for an indexed draw
3068 if (type != GL_NONE && mState.elementArrayBuffer.get())
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003069 {
3070 Buffer *indexBuffer = mState.elementArrayBuffer.get();
3071 intptr_t offset = reinterpret_cast<intptr_t>(indices);
3072 indices = static_cast<const GLubyte*>(indexBuffer->data()) + offset;
3073 }
3074
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003075 UINT startIndex = 0;
3076 bool succeeded = false;
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003077
daniel@transgaming.com6c4b5e02012-01-27 15:39:12 +00003078 if (supports32bitIndices())
3079 {
3080 const int spaceNeeded = (count + 1) * sizeof(unsigned int);
3081
3082 if (!mLineLoopIB)
3083 {
3084 mLineLoopIB = new StreamingIndexBuffer(mDevice, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX32);
3085 }
3086
3087 if (mLineLoopIB)
3088 {
3089 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_INT);
3090
3091 UINT offset = 0;
3092 unsigned int *data = static_cast<unsigned int*>(mLineLoopIB->map(spaceNeeded, &offset));
3093 startIndex = offset / 4;
3094
3095 if (data)
3096 {
3097 switch (type)
3098 {
3099 case GL_NONE: // Non-indexed draw
3100 for (int i = 0; i < count; i++)
3101 {
3102 data[i] = i;
3103 }
3104 data[count] = 0;
3105 break;
3106 case GL_UNSIGNED_BYTE:
3107 for (int i = 0; i < count; i++)
3108 {
3109 data[i] = static_cast<const GLubyte*>(indices)[i];
3110 }
3111 data[count] = static_cast<const GLubyte*>(indices)[0];
3112 break;
3113 case GL_UNSIGNED_SHORT:
3114 for (int i = 0; i < count; i++)
3115 {
3116 data[i] = static_cast<const GLushort*>(indices)[i];
3117 }
3118 data[count] = static_cast<const GLushort*>(indices)[0];
3119 break;
3120 case GL_UNSIGNED_INT:
3121 for (int i = 0; i < count; i++)
3122 {
3123 data[i] = static_cast<const GLuint*>(indices)[i];
3124 }
3125 data[count] = static_cast<const GLuint*>(indices)[0];
3126 break;
3127 default: UNREACHABLE();
3128 }
3129
3130 mLineLoopIB->unmap();
3131 succeeded = true;
3132 }
3133 }
3134 }
3135 else
3136 {
3137 const int spaceNeeded = (count + 1) * sizeof(unsigned short);
3138
3139 if (!mLineLoopIB)
3140 {
3141 mLineLoopIB = new StreamingIndexBuffer(mDevice, INITIAL_INDEX_BUFFER_SIZE, D3DFMT_INDEX16);
3142 }
3143
3144 if (mLineLoopIB)
3145 {
3146 mLineLoopIB->reserveSpace(spaceNeeded, GL_UNSIGNED_SHORT);
3147
3148 UINT offset = 0;
3149 unsigned short *data = static_cast<unsigned short*>(mLineLoopIB->map(spaceNeeded, &offset));
3150 startIndex = offset / 2;
3151
3152 if (data)
3153 {
3154 switch (type)
3155 {
3156 case GL_NONE: // Non-indexed draw
3157 for (int i = 0; i < count; i++)
3158 {
3159 data[i] = i;
3160 }
3161 data[count] = 0;
3162 break;
3163 case GL_UNSIGNED_BYTE:
3164 for (int i = 0; i < count; i++)
3165 {
3166 data[i] = static_cast<const GLubyte*>(indices)[i];
3167 }
3168 data[count] = static_cast<const GLubyte*>(indices)[0];
3169 break;
3170 case GL_UNSIGNED_SHORT:
3171 for (int i = 0; i < count; i++)
3172 {
3173 data[i] = static_cast<const GLushort*>(indices)[i];
3174 }
3175 data[count] = static_cast<const GLushort*>(indices)[0];
3176 break;
3177 case GL_UNSIGNED_INT:
3178 for (int i = 0; i < count; i++)
3179 {
3180 data[i] = static_cast<const GLuint*>(indices)[i];
3181 }
3182 data[count] = static_cast<const GLuint*>(indices)[0];
3183 break;
3184 default: UNREACHABLE();
3185 }
3186
3187 mLineLoopIB->unmap();
3188 succeeded = true;
3189 }
3190 }
3191 }
3192
3193 if (succeeded)
3194 {
3195 if (mAppliedIBSerial != mLineLoopIB->getSerial())
3196 {
3197 mDevice->SetIndices(mLineLoopIB->getBuffer());
3198 mAppliedIBSerial = mLineLoopIB->getSerial();
3199 }
3200
3201 mDevice->DrawIndexedPrimitive(D3DPT_LINESTRIP, -minIndex, minIndex, count, startIndex, count);
3202 }
3203 else
3204 {
3205 ERR("Could not create a looping index buffer for GL_LINE_LOOP.");
3206 return error(GL_OUT_OF_MEMORY);
3207 }
daniel@transgaming.comddcd7372011-01-08 05:46:33 +00003208}
3209
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003210void Context::recordInvalidEnum()
3211{
3212 mInvalidEnum = true;
3213}
3214
3215void Context::recordInvalidValue()
3216{
3217 mInvalidValue = true;
3218}
3219
3220void Context::recordInvalidOperation()
3221{
3222 mInvalidOperation = true;
3223}
3224
3225void Context::recordOutOfMemory()
3226{
3227 mOutOfMemory = true;
3228}
3229
3230void Context::recordInvalidFramebufferOperation()
3231{
3232 mInvalidFramebufferOperation = true;
3233}
3234
3235// Get one of the recorded errors and clear its flag, if any.
3236// [OpenGL ES 2.0.24] section 2.5 page 13.
3237GLenum Context::getError()
3238{
3239 if (mInvalidEnum)
3240 {
3241 mInvalidEnum = false;
3242
3243 return GL_INVALID_ENUM;
3244 }
3245
3246 if (mInvalidValue)
3247 {
3248 mInvalidValue = false;
3249
3250 return GL_INVALID_VALUE;
3251 }
3252
3253 if (mInvalidOperation)
3254 {
3255 mInvalidOperation = false;
3256
3257 return GL_INVALID_OPERATION;
3258 }
3259
3260 if (mOutOfMemory)
3261 {
3262 mOutOfMemory = false;
3263
3264 return GL_OUT_OF_MEMORY;
3265 }
3266
3267 if (mInvalidFramebufferOperation)
3268 {
3269 mInvalidFramebufferOperation = false;
3270
3271 return GL_INVALID_FRAMEBUFFER_OPERATION;
3272 }
3273
3274 return GL_NO_ERROR;
3275}
3276
daniel@transgaming.com17f548c2011-11-09 17:47:02 +00003277GLenum Context::getResetStatus()
3278{
3279 if (mResetStatus == GL_NO_ERROR)
3280 {
3281 bool lost = mDisplay->testDeviceLost();
3282
3283 if (lost)
3284 {
3285 mDisplay->notifyDeviceLost(); // Sets mResetStatus
3286 }
3287 }
3288
3289 GLenum status = mResetStatus;
3290
3291 if (mResetStatus != GL_NO_ERROR)
3292 {
3293 if (mDisplay->testDeviceResettable())
3294 {
3295 mResetStatus = GL_NO_ERROR;
3296 }
3297 }
3298
3299 return status;
3300}
3301
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00003302bool Context::isResetNotificationEnabled()
3303{
3304 return (mResetStrategy == GL_LOSE_CONTEXT_ON_RESET_EXT);
3305}
3306
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003307bool Context::supportsShaderModel3() const
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003308{
daniel@transgaming.combe5a0862010-07-28 19:20:37 +00003309 return mSupportsShaderModel3;
daniel@transgaming.com296ca9c2010-04-03 20:56:07 +00003310}
3311
daniel@transgaming.com396c6432010-11-26 16:26:12 +00003312int Context::getMaximumVaryingVectors() const
3313{
3314 return mSupportsShaderModel3 ? MAX_VARYING_VECTORS_SM3 : MAX_VARYING_VECTORS_SM2;
3315}
3316
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003317unsigned int Context::getMaximumVertexTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003318{
3319 return mSupportsVertexTexture ? MAX_VERTEX_TEXTURE_IMAGE_UNITS_VTF : 0;
3320}
3321
daniel@transgaming.comdfd57022011-05-11 15:37:25 +00003322unsigned int Context::getMaximumCombinedTextureImageUnits() const
daniel@transgaming.comaf29cac2011-05-11 15:36:31 +00003323{
3324 return MAX_TEXTURE_IMAGE_UNITS + getMaximumVertexTextureImageUnits();
3325}
3326
daniel@transgaming.com458da142010-11-28 02:03:02 +00003327int Context::getMaximumFragmentUniformVectors() const
3328{
3329 return mSupportsShaderModel3 ? MAX_FRAGMENT_UNIFORM_VECTORS_SM3 : MAX_FRAGMENT_UNIFORM_VECTORS_SM2;
3330}
3331
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003332int Context::getMaxSupportedSamples() const
3333{
3334 return mMaxSupportedSamples;
3335}
3336
3337int Context::getNearestSupportedSamples(D3DFORMAT format, int requested) const
3338{
3339 if (requested == 0)
3340 {
3341 return requested;
3342 }
3343
3344 std::map<D3DFORMAT, bool *>::const_iterator itr = mMultiSampleSupport.find(format);
3345 if (itr == mMultiSampleSupport.end())
3346 {
3347 return -1;
3348 }
3349
3350 for (int i = requested; i <= D3DMULTISAMPLE_16_SAMPLES; ++i)
3351 {
3352 if (itr->second[i] && i != D3DMULTISAMPLE_NONMASKABLE)
3353 {
3354 return i;
3355 }
3356 }
3357
3358 return -1;
3359}
3360
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003361bool Context::supportsEventQueries() const
3362{
3363 return mSupportsEventQueries;
3364}
3365
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00003366bool Context::supportsOcclusionQueries() const
3367{
3368 return mSupportsOcclusionQueries;
3369}
3370
gman@chromium.org50c526d2011-08-10 05:19:44 +00003371bool Context::supportsDXT1Textures() const
daniel@transgaming.com01868132010-08-24 19:21:17 +00003372{
gman@chromium.org50c526d2011-08-10 05:19:44 +00003373 return mSupportsDXT1Textures;
3374}
3375
3376bool Context::supportsDXT3Textures() const
3377{
3378 return mSupportsDXT3Textures;
3379}
3380
3381bool Context::supportsDXT5Textures() const
3382{
3383 return mSupportsDXT5Textures;
daniel@transgaming.com01868132010-08-24 19:21:17 +00003384}
3385
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003386bool Context::supportsFloat32Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003387{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003388 return mSupportsFloat32Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003389}
3390
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003391bool Context::supportsFloat32LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003392{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003393 return mSupportsFloat32LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003394}
3395
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003396bool Context::supportsFloat32RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003397{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003398 return mSupportsFloat32RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003399}
3400
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003401bool Context::supportsFloat16Textures() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003402{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003403 return mSupportsFloat16Textures;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003404}
3405
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003406bool Context::supportsFloat16LinearFilter() const
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003407{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003408 return mSupportsFloat16LinearFilter;
daniel@transgaming.com0a337e92010-08-28 17:38:27 +00003409}
3410
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003411bool Context::supportsFloat16RenderableTextures() const
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003412{
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003413 return mSupportsFloat16RenderableTextures;
daniel@transgaming.com1297d922010-09-01 15:47:47 +00003414}
3415
daniel@transgaming.com5d752f22010-10-07 13:37:20 +00003416int Context::getMaximumRenderbufferDimension() const
3417{
3418 return mMaxRenderbufferDimension;
3419}
3420
3421int Context::getMaximumTextureDimension() const
3422{
3423 return mMaxTextureDimension;
3424}
3425
3426int Context::getMaximumCubeTextureDimension() const
3427{
3428 return mMaxCubeTextureDimension;
3429}
3430
3431int Context::getMaximumTextureLevel() const
3432{
3433 return mMaxTextureLevel;
3434}
3435
daniel@transgaming.comed828e52010-10-15 17:57:30 +00003436bool Context::supportsLuminanceTextures() const
3437{
3438 return mSupportsLuminanceTextures;
3439}
3440
3441bool Context::supportsLuminanceAlphaTextures() const
3442{
3443 return mSupportsLuminanceAlphaTextures;
3444}
3445
daniel@transgaming.com83921382011-01-08 05:46:00 +00003446bool Context::supports32bitIndices() const
3447{
3448 return mSupports32bitIndices;
3449}
3450
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003451bool Context::supportsNonPower2Texture() const
3452{
3453 return mSupportsNonPower2Texture;
3454}
3455
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003456void Context::detachBuffer(GLuint buffer)
3457{
3458 // [OpenGL ES 2.0.24] section 2.9 page 22:
3459 // If a buffer object is deleted while it is bound, all bindings to that object in the current context
3460 // (i.e. in the thread that called Delete-Buffers) are reset to zero.
3461
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003462 if (mState.arrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003463 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003464 mState.arrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003465 }
3466
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003467 if (mState.elementArrayBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003468 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003469 mState.elementArrayBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003470 }
3471
3472 for (int attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
3473 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003474 if (mState.vertexAttribute[attribute].mBoundBuffer.id() == buffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003475 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003476 mState.vertexAttribute[attribute].mBoundBuffer.set(NULL);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003477 }
3478 }
3479}
3480
3481void Context::detachTexture(GLuint texture)
3482{
3483 // [OpenGL ES 2.0.24] section 3.8 page 84:
3484 // If a texture object is deleted, it is as if all texture units which are bound to that texture object are
3485 // rebound to texture object zero
3486
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003487 for (int type = 0; type < TEXTURE_TYPE_COUNT; type++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003488 {
daniel@transgaming.com3f74c7a2011-05-11 15:36:51 +00003489 for (int sampler = 0; sampler < MAX_COMBINED_TEXTURE_IMAGE_UNITS_VTF; sampler++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003490 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003491 if (mState.samplerTexture[type][sampler].id() == texture)
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003492 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003493 mState.samplerTexture[type][sampler].set(NULL);
daniel@transgaming.com416485f2010-03-16 06:23:23 +00003494 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003495 }
3496 }
3497
3498 // [OpenGL ES 2.0.24] section 4.4 page 112:
3499 // If a texture object is deleted while its image is attached to the currently bound framebuffer, then it is
3500 // as if FramebufferTexture2D had been called, with a texture of 0, for each attachment point to which this
3501 // image was attached in the currently bound framebuffer.
3502
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003503 Framebuffer *readFramebuffer = getReadFramebuffer();
3504 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003505
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003506 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003507 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003508 readFramebuffer->detachTexture(texture);
3509 }
3510
3511 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3512 {
3513 drawFramebuffer->detachTexture(texture);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003514 }
3515}
3516
3517void Context::detachFramebuffer(GLuint framebuffer)
3518{
3519 // [OpenGL ES 2.0.24] section 4.4 page 107:
3520 // If a framebuffer that is currently bound to the target FRAMEBUFFER is deleted, it is as though
3521 // BindFramebuffer had been executed with the target of FRAMEBUFFER and framebuffer of zero.
3522
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003523 if (mState.readFramebuffer == framebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003524 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003525 bindReadFramebuffer(0);
3526 }
3527
apatrick@chromium.orgff8bdfb2010-09-15 17:27:49 +00003528 if (mState.drawFramebuffer == framebuffer)
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003529 {
3530 bindDrawFramebuffer(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003531 }
3532}
3533
3534void Context::detachRenderbuffer(GLuint renderbuffer)
3535{
3536 // [OpenGL ES 2.0.24] section 4.4 page 109:
3537 // If a renderbuffer that is currently bound to RENDERBUFFER is deleted, it is as though BindRenderbuffer
3538 // had been executed with the target RENDERBUFFER and name of zero.
3539
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003540 if (mState.renderbuffer.id() == renderbuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003541 {
3542 bindRenderbuffer(0);
3543 }
3544
3545 // [OpenGL ES 2.0.24] section 4.4 page 111:
3546 // If a renderbuffer object is deleted while its image is attached to the currently bound framebuffer,
3547 // then it is as if FramebufferRenderbuffer had been called, with a renderbuffer of 0, for each attachment
3548 // point to which this image was attached in the currently bound framebuffer.
3549
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003550 Framebuffer *readFramebuffer = getReadFramebuffer();
3551 Framebuffer *drawFramebuffer = getDrawFramebuffer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003552
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003553 if (readFramebuffer)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003554 {
daniel@transgaming.coma27ff1e2010-08-24 19:20:11 +00003555 readFramebuffer->detachRenderbuffer(renderbuffer);
3556 }
3557
3558 if (drawFramebuffer && drawFramebuffer != readFramebuffer)
3559 {
3560 drawFramebuffer->detachRenderbuffer(renderbuffer);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003561 }
3562}
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003563
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003564Texture *Context::getIncompleteTexture(TextureType type)
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003565{
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003566 Texture *t = mIncompleteTextures[type].get();
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003567
3568 if (t == NULL)
3569 {
3570 static const GLubyte color[] = { 0, 0, 0, 255 };
3571
3572 switch (type)
3573 {
3574 default:
3575 UNREACHABLE();
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003576 // default falls through to TEXTURE_2D
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003577
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003578 case TEXTURE_2D:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003579 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003580 Texture2D *incomplete2d = new Texture2D(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003581 incomplete2d->setImage(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003582 t = incomplete2d;
3583 }
3584 break;
3585
daniel@transgaming.com0e64dd62011-05-11 15:36:37 +00003586 case TEXTURE_CUBE:
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003587 {
daniel@transgaming.com9ecb9f92010-07-28 19:21:12 +00003588 TextureCubeMap *incompleteCube = new TextureCubeMap(Texture::INCOMPLETE_TEXTURE_ID);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003589
daniel@transgaming.com8a0a2db2011-03-21 16:38:20 +00003590 incompleteCube->setImagePosX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3591 incompleteCube->setImageNegX(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3592 incompleteCube->setImagePosY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3593 incompleteCube->setImageNegY(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3594 incompleteCube->setImagePosZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
3595 incompleteCube->setImageNegZ(0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, 1, color);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003596
3597 t = incompleteCube;
3598 }
3599 break;
3600 }
3601
apatrick@chromium.org4e3bad42010-09-15 17:31:48 +00003602 mIncompleteTextures[type].set(t);
daniel@transgaming.com12d54072010-03-16 06:23:26 +00003603 }
3604
3605 return t;
3606}
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003607
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003608bool Context::cullSkipsDraw(GLenum drawMode)
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003609{
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003610 return mState.cullFace && mState.cullMode == GL_FRONT_AND_BACK && isTriangleMode(drawMode);
daniel@transgaming.comace5e662010-03-21 04:31:20 +00003611}
3612
daniel@transgaming.com5af64272010-04-15 20:45:12 +00003613bool Context::isTriangleMode(GLenum drawMode)
3614{
3615 switch (drawMode)
3616 {
3617 case GL_TRIANGLES:
3618 case GL_TRIANGLE_FAN:
3619 case GL_TRIANGLE_STRIP:
3620 return true;
3621 case GL_POINTS:
3622 case GL_LINES:
3623 case GL_LINE_LOOP:
3624 case GL_LINE_STRIP:
3625 return false;
3626 default: UNREACHABLE();
3627 }
3628
3629 return false;
3630}
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003631
3632void Context::setVertexAttrib(GLuint index, const GLfloat *values)
3633{
3634 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3635
daniel@transgaming.com428d1582010-05-04 03:35:25 +00003636 mState.vertexAttribute[index].mCurrentValue[0] = values[0];
3637 mState.vertexAttribute[index].mCurrentValue[1] = values[1];
3638 mState.vertexAttribute[index].mCurrentValue[2] = values[2];
3639 mState.vertexAttribute[index].mCurrentValue[3] = values[3];
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003640
daniel@transgaming.com83921382011-01-08 05:46:00 +00003641 mVertexDataManager->dirtyCurrentValue(index);
daniel@transgaming.come4b08c82010-04-20 18:53:06 +00003642}
3643
daniel@transgaming.comd2820bf2012-01-27 15:38:48 +00003644void Context::setVertexAttribDivisor(GLuint index, GLuint divisor)
3645{
3646 ASSERT(index < gl::MAX_VERTEX_ATTRIBS);
3647
3648 mState.vertexAttribute[index].mDivisor = divisor;
3649}
3650
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003651// keep list sorted in following order
3652// OES extensions
3653// EXT extensions
3654// Vendor extensions
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003655void Context::initExtensionString()
3656{
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003657 mExtensionString = "";
3658
3659 // OES extensions
3660 if (supports32bitIndices())
3661 {
3662 mExtensionString += "GL_OES_element_index_uint ";
3663 }
3664
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003665 mExtensionString += "GL_OES_packed_depth_stencil ";
daniel@transgaming.comd36c2972010-08-24 19:21:07 +00003666 mExtensionString += "GL_OES_rgb8_rgba8 ";
alokp@chromium.orgd303ef92010-09-09 17:30:15 +00003667 mExtensionString += "GL_OES_standard_derivatives ";
daniel@transgaming.comcdacc8e2010-07-28 19:20:50 +00003668
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003669 if (supportsFloat16Textures())
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003670 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003671 mExtensionString += "GL_OES_texture_half_float ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003672 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003673 if (supportsFloat16LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003674 {
3675 mExtensionString += "GL_OES_texture_half_float_linear ";
3676 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003677 if (supportsFloat32Textures())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003678 {
3679 mExtensionString += "GL_OES_texture_float ";
3680 }
daniel@transgaming.combbeffbb2011-11-09 17:46:11 +00003681 if (supportsFloat32LinearFilter())
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003682 {
3683 mExtensionString += "GL_OES_texture_float_linear ";
3684 }
3685
3686 if (supportsNonPower2Texture())
3687 {
3688 mExtensionString += "GL_OES_texture_npot ";
3689 }
3690
3691 // Multi-vendor (EXT) extensions
daniel@transgaming.com86bdb822012-01-20 18:24:39 +00003692 if (supportsOcclusionQueries())
3693 {
3694 mExtensionString += "GL_EXT_occlusion_query_boolean ";
3695 }
3696
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003697 mExtensionString += "GL_EXT_read_format_bgra ";
daniel@transgaming.com8747f182011-11-09 17:50:38 +00003698 mExtensionString += "GL_EXT_robustness ";
apatrick@chromium.orgd3bd0ad2010-08-30 18:55:36 +00003699
gman@chromium.org50c526d2011-08-10 05:19:44 +00003700 if (supportsDXT1Textures())
daniel@transgaming.com01868132010-08-24 19:21:17 +00003701 {
3702 mExtensionString += "GL_EXT_texture_compression_dxt1 ";
3703 }
daniel@transgaming.comd470a1b2010-08-24 19:20:48 +00003704
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003705 mExtensionString += "GL_EXT_texture_format_BGRA8888 ";
daniel@transgaming.comdf363722011-12-16 23:29:53 +00003706 mExtensionString += "GL_EXT_texture_storage ";
gman@chromium.org50c526d2011-08-10 05:19:44 +00003707
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003708 // ANGLE-specific extensions
3709 mExtensionString += "GL_ANGLE_framebuffer_blit ";
daniel@transgaming.com3ea20e72010-08-24 19:20:58 +00003710 if (getMaxSupportedSamples() != 0)
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003711 {
3712 mExtensionString += "GL_ANGLE_framebuffer_multisample ";
3713 }
3714
bsalomon@google.com56d46ab2011-11-23 14:53:10 +00003715 mExtensionString += "GL_ANGLE_pack_reverse_row_order ";
3716
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003717 if (supportsDXT3Textures())
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003718 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003719 mExtensionString += "GL_ANGLE_texture_compression_dxt3 ";
3720 }
3721 if (supportsDXT5Textures())
3722 {
3723 mExtensionString += "GL_ANGLE_texture_compression_dxt5 ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003724 }
daniel@transgaming.com97412f72011-11-11 04:19:07 +00003725
3726 mExtensionString += "GL_ANGLE_texture_usage ";
zmo@google.coma574f782011-10-03 21:45:23 +00003727 mExtensionString += "GL_ANGLE_translated_shader_source ";
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003728
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003729 // Other vendor-specific extensions
3730 if (supportsEventQueries())
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003731 {
daniel@transgaming.com8440e3f2011-09-26 18:25:12 +00003732 mExtensionString += "GL_NV_fence ";
daniel@transgaming.com4f9ef0d2011-05-30 23:51:19 +00003733 }
3734
daniel@transgaming.com3e4c6002010-05-05 18:50:13 +00003735 std::string::size_type end = mExtensionString.find_last_not_of(' ');
3736 if (end != std::string::npos)
3737 {
3738 mExtensionString.resize(end+1);
3739 }
3740}
3741
3742const char *Context::getExtensionString() const
3743{
3744 return mExtensionString.c_str();
3745}
3746
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003747void Context::initRendererString()
3748{
daniel@transgaming.comc941e252011-10-26 02:32:31 +00003749 D3DADAPTER_IDENTIFIER9 *identifier = mDisplay->getAdapterIdentifier();
daniel@transgaming.comc23ff642011-08-16 20:28:45 +00003750
3751 mRendererString = "ANGLE (";
3752 mRendererString += identifier->Description;
3753 mRendererString += ")";
3754}
3755
3756const char *Context::getRendererString() const
3757{
3758 return mRendererString.c_str();
3759}
3760
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003761void Context::blitFramebuffer(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
3762 GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
3763 GLbitfield mask)
3764{
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003765 Framebuffer *readFramebuffer = getReadFramebuffer();
3766 Framebuffer *drawFramebuffer = getDrawFramebuffer();
3767
3768 if (!readFramebuffer || readFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE ||
3769 !drawFramebuffer || drawFramebuffer->completeness() != GL_FRAMEBUFFER_COMPLETE)
3770 {
3771 return error(GL_INVALID_FRAMEBUFFER_OPERATION);
3772 }
3773
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003774 if (drawFramebuffer->getSamples() != 0)
3775 {
3776 return error(GL_INVALID_OPERATION);
3777 }
3778
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003779 int readBufferWidth = readFramebuffer->getColorbuffer()->getWidth();
3780 int readBufferHeight = readFramebuffer->getColorbuffer()->getHeight();
3781 int drawBufferWidth = drawFramebuffer->getColorbuffer()->getWidth();
3782 int drawBufferHeight = drawFramebuffer->getColorbuffer()->getHeight();
3783
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003784 RECT sourceRect;
3785 RECT destRect;
3786
3787 if (srcX0 < srcX1)
3788 {
3789 sourceRect.left = srcX0;
3790 sourceRect.right = srcX1;
3791 destRect.left = dstX0;
3792 destRect.right = dstX1;
3793 }
3794 else
3795 {
3796 sourceRect.left = srcX1;
3797 destRect.left = dstX1;
3798 sourceRect.right = srcX0;
3799 destRect.right = dstX0;
3800 }
3801
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003802 if (srcY0 < srcY1)
3803 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003804 sourceRect.top = readBufferHeight - srcY1;
3805 destRect.top = drawBufferHeight - dstY1;
3806 sourceRect.bottom = readBufferHeight - srcY0;
3807 destRect.bottom = drawBufferHeight - dstY0;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003808 }
3809 else
3810 {
apatrick@chromium.orgb31f5322011-01-19 19:02:52 +00003811 sourceRect.top = readBufferHeight - srcY0;
3812 destRect.top = drawBufferHeight - dstY0;
3813 sourceRect.bottom = readBufferHeight - srcY1;
3814 destRect.bottom = drawBufferHeight - dstY1;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003815 }
3816
3817 RECT sourceScissoredRect = sourceRect;
3818 RECT destScissoredRect = destRect;
3819
3820 if (mState.scissorTest)
3821 {
3822 // Only write to parts of the destination framebuffer which pass the scissor test
3823 // Please note: the destRect is now in D3D-style coordinates, so the *top* of the
3824 // rect will be checked against scissorY, rather than the bottom.
3825 if (destRect.left < mState.scissorX)
3826 {
3827 int xDiff = mState.scissorX - destRect.left;
3828 destScissoredRect.left = mState.scissorX;
3829 sourceScissoredRect.left += xDiff;
3830 }
3831
3832 if (destRect.right > mState.scissorX + mState.scissorWidth)
3833 {
3834 int xDiff = destRect.right - (mState.scissorX + mState.scissorWidth);
3835 destScissoredRect.right = mState.scissorX + mState.scissorWidth;
3836 sourceScissoredRect.right -= xDiff;
3837 }
3838
3839 if (destRect.top < mState.scissorY)
3840 {
3841 int yDiff = mState.scissorY - destRect.top;
3842 destScissoredRect.top = mState.scissorY;
3843 sourceScissoredRect.top += yDiff;
3844 }
3845
3846 if (destRect.bottom > mState.scissorY + mState.scissorHeight)
3847 {
3848 int yDiff = destRect.bottom - (mState.scissorY + mState.scissorHeight);
3849 destScissoredRect.bottom = mState.scissorY + mState.scissorHeight;
3850 sourceScissoredRect.bottom -= yDiff;
3851 }
3852 }
3853
3854 bool blitRenderTarget = false;
3855 bool blitDepthStencil = false;
3856
3857 RECT sourceTrimmedRect = sourceScissoredRect;
3858 RECT destTrimmedRect = destScissoredRect;
3859
3860 // The source & destination rectangles also may need to be trimmed if they fall out of the bounds of
3861 // the actual draw and read surfaces.
3862 if (sourceTrimmedRect.left < 0)
3863 {
3864 int xDiff = 0 - sourceTrimmedRect.left;
3865 sourceTrimmedRect.left = 0;
3866 destTrimmedRect.left += xDiff;
3867 }
3868
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003869 if (sourceTrimmedRect.right > readBufferWidth)
3870 {
3871 int xDiff = sourceTrimmedRect.right - readBufferWidth;
3872 sourceTrimmedRect.right = readBufferWidth;
3873 destTrimmedRect.right -= xDiff;
3874 }
3875
3876 if (sourceTrimmedRect.top < 0)
3877 {
3878 int yDiff = 0 - sourceTrimmedRect.top;
3879 sourceTrimmedRect.top = 0;
3880 destTrimmedRect.top += yDiff;
3881 }
3882
3883 if (sourceTrimmedRect.bottom > readBufferHeight)
3884 {
3885 int yDiff = sourceTrimmedRect.bottom - readBufferHeight;
3886 sourceTrimmedRect.bottom = readBufferHeight;
3887 destTrimmedRect.bottom -= yDiff;
3888 }
3889
3890 if (destTrimmedRect.left < 0)
3891 {
3892 int xDiff = 0 - destTrimmedRect.left;
3893 destTrimmedRect.left = 0;
3894 sourceTrimmedRect.left += xDiff;
3895 }
3896
3897 if (destTrimmedRect.right > drawBufferWidth)
3898 {
3899 int xDiff = destTrimmedRect.right - drawBufferWidth;
3900 destTrimmedRect.right = drawBufferWidth;
3901 sourceTrimmedRect.right -= xDiff;
3902 }
3903
3904 if (destTrimmedRect.top < 0)
3905 {
3906 int yDiff = 0 - destTrimmedRect.top;
3907 destTrimmedRect.top = 0;
3908 sourceTrimmedRect.top += yDiff;
3909 }
3910
3911 if (destTrimmedRect.bottom > drawBufferHeight)
3912 {
3913 int yDiff = destTrimmedRect.bottom - drawBufferHeight;
3914 destTrimmedRect.bottom = drawBufferHeight;
3915 sourceTrimmedRect.bottom -= yDiff;
3916 }
3917
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003918 bool partialBufferCopy = false;
daniel@transgaming.com3aba7332011-01-14 15:08:35 +00003919 if (sourceTrimmedRect.bottom - sourceTrimmedRect.top < readBufferHeight ||
3920 sourceTrimmedRect.right - sourceTrimmedRect.left < readBufferWidth ||
3921 destTrimmedRect.bottom - destTrimmedRect.top < drawBufferHeight ||
3922 destTrimmedRect.right - destTrimmedRect.left < drawBufferWidth ||
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003923 sourceTrimmedRect.top != 0 || destTrimmedRect.top != 0 || sourceTrimmedRect.left != 0 || destTrimmedRect.left != 0)
3924 {
3925 partialBufferCopy = true;
3926 }
3927
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003928 if (mask & GL_COLOR_BUFFER_BIT)
3929 {
enne@chromium.org0fa74632010-09-21 16:18:52 +00003930 const bool validReadType = readFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3931 readFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3932 const bool validDrawType = drawFramebuffer->getColorbufferType() == GL_TEXTURE_2D ||
3933 drawFramebuffer->getColorbufferType() == GL_RENDERBUFFER;
3934 if (!validReadType || !validDrawType ||
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003935 readFramebuffer->getColorbuffer()->getD3DFormat() != drawFramebuffer->getColorbuffer()->getD3DFormat())
3936 {
3937 ERR("Color buffer format conversion in BlitFramebufferANGLE not supported by this implementation");
3938 return error(GL_INVALID_OPERATION);
3939 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003940
3941 if (partialBufferCopy && readFramebuffer->getSamples() != 0)
3942 {
3943 return error(GL_INVALID_OPERATION);
3944 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003945
3946 blitRenderTarget = true;
3947
3948 }
3949
3950 if (mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
3951 {
daniel@transgaming.comd14558a2011-11-09 17:46:18 +00003952 Renderbuffer *readDSBuffer = NULL;
3953 Renderbuffer *drawDSBuffer = NULL;
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003954
3955 // We support OES_packed_depth_stencil, and do not support a separately attached depth and stencil buffer, so if we have
3956 // both a depth and stencil buffer, it will be the same buffer.
3957
3958 if (mask & GL_DEPTH_BUFFER_BIT)
3959 {
3960 if (readFramebuffer->getDepthbuffer() && drawFramebuffer->getDepthbuffer())
3961 {
3962 if (readFramebuffer->getDepthbufferType() != drawFramebuffer->getDepthbufferType() ||
3963 readFramebuffer->getDepthbuffer()->getD3DFormat() != drawFramebuffer->getDepthbuffer()->getD3DFormat())
3964 {
3965 return error(GL_INVALID_OPERATION);
3966 }
3967
3968 blitDepthStencil = true;
3969 readDSBuffer = readFramebuffer->getDepthbuffer();
3970 drawDSBuffer = drawFramebuffer->getDepthbuffer();
3971 }
3972 }
3973
3974 if (mask & GL_STENCIL_BUFFER_BIT)
3975 {
3976 if (readFramebuffer->getStencilbuffer() && drawFramebuffer->getStencilbuffer())
3977 {
3978 if (readFramebuffer->getStencilbufferType() != drawFramebuffer->getStencilbufferType() ||
3979 readFramebuffer->getStencilbuffer()->getD3DFormat() != drawFramebuffer->getStencilbuffer()->getD3DFormat())
3980 {
3981 return error(GL_INVALID_OPERATION);
3982 }
3983
3984 blitDepthStencil = true;
3985 readDSBuffer = readFramebuffer->getStencilbuffer();
3986 drawDSBuffer = drawFramebuffer->getStencilbuffer();
3987 }
3988 }
3989
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003990 if (partialBufferCopy)
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00003991 {
3992 ERR("Only whole-buffer depth and stencil blits are supported by this implementation.");
3993 return error(GL_INVALID_OPERATION); // only whole-buffer copies are permitted
3994 }
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003995
daniel@transgaming.com97446d22010-08-24 19:20:54 +00003996 if ((drawDSBuffer && drawDSBuffer->getSamples() != 0) ||
3997 (readDSBuffer && readDSBuffer->getSamples() != 0))
daniel@transgaming.com1f135d82010-08-24 19:20:36 +00003998 {
3999 return error(GL_INVALID_OPERATION);
4000 }
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004001 }
4002
4003 if (blitRenderTarget || blitDepthStencil)
4004 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00004005 mDisplay->endScene();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004006
4007 if (blitRenderTarget)
4008 {
apatrick@chromium.orgfebbea82011-12-07 19:10:16 +00004009 IDirect3DSurface9* readRenderTarget = readFramebuffer->getRenderTarget();
4010 IDirect3DSurface9* drawRenderTarget = drawFramebuffer->getRenderTarget();
4011
4012 HRESULT result = mDevice->StretchRect(readRenderTarget, &sourceTrimmedRect,
4013 drawRenderTarget, &destTrimmedRect, D3DTEXF_NONE);
4014
4015 readRenderTarget->Release();
4016 drawRenderTarget->Release();
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004017
4018 if (FAILED(result))
4019 {
4020 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
4021 return;
4022 }
4023 }
4024
4025 if (blitDepthStencil)
4026 {
daniel@transgaming.comc941e252011-10-26 02:32:31 +00004027 HRESULT result = mDevice->StretchRect(readFramebuffer->getDepthStencil(), NULL, drawFramebuffer->getDepthStencil(), NULL, D3DTEXF_NONE);
daniel@transgaming.com4cbc5902010-08-24 19:20:26 +00004028
4029 if (FAILED(result))
4030 {
4031 ERR("BlitFramebufferANGLE failed: StretchRect returned %x.", result);
4032 return;
4033 }
4034 }
4035 }
4036}
4037
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004038VertexDeclarationCache::VertexDeclarationCache() : mMaxLru(0)
4039{
4040 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4041 {
4042 mVertexDeclCache[i].vertexDeclaration = NULL;
4043 mVertexDeclCache[i].lruCount = 0;
4044 }
4045}
4046
4047VertexDeclarationCache::~VertexDeclarationCache()
4048{
4049 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4050 {
4051 if (mVertexDeclCache[i].vertexDeclaration)
4052 {
4053 mVertexDeclCache[i].vertexDeclaration->Release();
4054 }
4055 }
4056}
4057
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004058GLenum VertexDeclarationCache::applyDeclaration(IDirect3DDevice9 *device, TranslatedAttribute attributes[], Program *program, GLsizei instances)
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004059{
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004060 int indexedAttribute = MAX_VERTEX_ATTRIBS;
4061
4062 if (instances > 0)
4063 {
4064 // Find an indexed attribute to be mapped to D3D stream 0
4065 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4066 {
4067 if (attributes[i].active && attributes[i].divisor == 0)
4068 {
4069 indexedAttribute = i;
4070 break;
4071 }
4072 }
4073
4074 if (indexedAttribute == MAX_VERTEX_ATTRIBS)
4075 {
4076 return GL_INVALID_OPERATION;
4077 }
4078 }
4079
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004080 D3DVERTEXELEMENT9 elements[MAX_VERTEX_ATTRIBS + 1];
4081 D3DVERTEXELEMENT9 *element = &elements[0];
4082
4083 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4084 {
4085 if (attributes[i].active)
4086 {
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004087 int stream = i;
4088
4089 if (instances > 0)
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004090 {
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004091 if (i == indexedAttribute)
4092 {
4093 stream = 0;
4094 }
4095 else if (i == 0)
4096 {
4097 stream = indexedAttribute;
4098 }
4099
4100 UINT frequency = 1;
4101
4102 if (attributes[i].divisor == 0)
4103 {
4104 frequency = D3DSTREAMSOURCE_INDEXEDDATA | instances;
4105 }
4106 else
4107 {
4108 frequency = D3DSTREAMSOURCE_INSTANCEDATA | attributes[i].divisor;
4109 }
4110
4111 device->SetStreamSourceFreq(stream, frequency);
4112 mInstancingEnabled = true;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004113 }
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004114
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004115 if (mAppliedVBs[stream].serial != attributes[i].serial ||
4116 mAppliedVBs[stream].stride != attributes[i].stride ||
4117 mAppliedVBs[stream].offset != attributes[i].offset)
4118 {
4119 device->SetStreamSource(stream, attributes[i].vertexBuffer, attributes[i].offset, attributes[i].stride);
4120 mAppliedVBs[stream].serial = attributes[i].serial;
4121 mAppliedVBs[stream].stride = attributes[i].stride;
4122 mAppliedVBs[stream].offset = attributes[i].offset;
4123 }
4124
4125 element->Stream = stream;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004126 element->Offset = 0;
4127 element->Type = attributes[i].type;
4128 element->Method = D3DDECLMETHOD_DEFAULT;
4129 element->Usage = D3DDECLUSAGE_TEXCOORD;
4130 element->UsageIndex = program->getSemanticIndex(i);
4131 element++;
4132 }
4133 }
4134
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004135 if (instances == 0)
4136 {
4137 if (mInstancingEnabled)
4138 {
4139 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4140 {
4141 device->SetStreamSourceFreq(i, 1);
4142 }
4143
4144 mInstancingEnabled = false;
4145 }
4146 }
4147
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004148 static const D3DVERTEXELEMENT9 end = D3DDECL_END();
4149 *(element++) = end;
4150
4151 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4152 {
4153 VertexDeclCacheEntry *entry = &mVertexDeclCache[i];
4154 if (memcmp(entry->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9)) == 0 && entry->vertexDeclaration)
4155 {
4156 entry->lruCount = ++mMaxLru;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004157 if(entry->vertexDeclaration != mLastSetVDecl)
4158 {
4159 device->SetVertexDeclaration(entry->vertexDeclaration);
4160 mLastSetVDecl = entry->vertexDeclaration;
4161 }
4162
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004163 return GL_NO_ERROR;
4164 }
4165 }
4166
4167 VertexDeclCacheEntry *lastCache = mVertexDeclCache;
4168
4169 for (int i = 0; i < NUM_VERTEX_DECL_CACHE_ENTRIES; i++)
4170 {
4171 if (mVertexDeclCache[i].lruCount < lastCache->lruCount)
4172 {
4173 lastCache = &mVertexDeclCache[i];
4174 }
4175 }
4176
4177 if (lastCache->vertexDeclaration != NULL)
4178 {
4179 lastCache->vertexDeclaration->Release();
4180 lastCache->vertexDeclaration = NULL;
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004181 // mLastSetVDecl is set to the replacement, so we don't have to worry
4182 // about it.
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004183 }
4184
4185 memcpy(lastCache->cachedElements, elements, (element - elements) * sizeof(D3DVERTEXELEMENT9));
4186 device->CreateVertexDeclaration(elements, &lastCache->vertexDeclaration);
4187 device->SetVertexDeclaration(lastCache->vertexDeclaration);
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004188 mLastSetVDecl = lastCache->vertexDeclaration;
daniel@transgaming.com09c2c1a2011-04-13 14:57:16 +00004189 lastCache->lruCount = ++mMaxLru;
4190
4191 return GL_NO_ERROR;
4192}
4193
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004194void VertexDeclarationCache::markStateDirty()
4195{
4196 for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
4197 {
4198 mAppliedVBs[i].serial = 0;
4199 }
4200
4201 mLastSetVDecl = NULL;
daniel@transgaming.com8ca9c6e2012-01-27 15:38:54 +00004202 mInstancingEnabled = true; // Forces it to be disabled when not used
jbauman@chromium.orgd8f3faa2011-09-02 01:10:47 +00004203}
4204
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004205}
4206
4207extern "C"
4208{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00004209gl::Context *glCreateContext(const egl::Config *config, const gl::Context *shareContext, bool notifyResets, bool robustAccess)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004210{
daniel@transgaming.com4ff960d2011-11-09 17:47:09 +00004211 return new gl::Context(config, shareContext, notifyResets, robustAccess);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00004212}
4213
4214void glDestroyContext(gl::Context *context)
4215{
4216 delete context;
4217
4218 if (context == gl::getContext())
4219 {
4220 gl::makeCurrent(NULL, NULL, NULL);
4221 }
4222}
4223
4224void glMakeCurrent(gl::Context *context, egl::Display *display, egl::Surface *surface)
4225{
4226 gl::makeCurrent(context, display, surface);
4227}
4228
4229gl::Context *glGetCurrentContext()
4230{
4231 return gl::getContext();
4232}
4233}